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 */ 107bd11f074SMaciej Żenczykowski u64 tmp = (900000 + prandom_u32() % 200001) * (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 */ 115bd11f074SMaciej Żenczykowski u64 tmp = (1900000 + prandom_u32() % 200001) * (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 */ 119bd11f074SMaciej Żenczykowski tmp = (900000 + prandom_u32() % 200001) * (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 */ 4018c727003SEric Dumazet dev_hold_track(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); 4078c727003SEric Dumazet dev_put_track(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 1037e66d1172SEric Dumazet spin_lock(&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 1047e66d1172SEric Dumazet spin_unlock(&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 1142f3d9832eSDavid Ahern rcu_read_lock_bh(); 11431da177e4SLinus Torvalds 1144f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1145f3d9832eSDavid Ahern if (err < 0) { 1146f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1147f3d9832eSDavid Ahern goto out; 1148f3d9832eSDavid Ahern } 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds write_lock(&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); 11611da177e4SLinus Torvalds write_unlock(&idev->lock); 1162f3d9832eSDavid Ahern 11638814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 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 31301da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 313161e18ce7SStephen Suryaputra if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE) 313261e18ce7SStephen Suryaputra return; 313361e18ce7SStephen Suryaputra 31341da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 31351da177e4SLinus Torvalds scope = IFA_LINK; 3136929c9cf3SNicolas Dichtel plen = 64; 31371da177e4SLinus Torvalds } else { 31381da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3139929c9cf3SNicolas Dichtel plen = 96; 3140f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 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 */ 3323744fdc82SJarod Wilson if (idev->dev->flags & IFF_SLAVE) 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 3450afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3451afb1d4b5SDavid Ahern struct inet6_dev *idev, 3452f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3453f1705ec1SDavid Ahern { 345493c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 34558048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 34568048ced9SDavid Ahern * case regenerate the host route. 34578048ced9SDavid Ahern */ 345893c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3459360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3460f1705ec1SDavid Ahern 3461360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3462acb54e3cSDavid Ahern GFP_ATOMIC); 3463360a9887SDavid Ahern if (IS_ERR(f6i)) 3464360a9887SDavid Ahern return PTR_ERR(f6i); 3465f1705ec1SDavid Ahern 34668048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 34678048ced9SDavid Ahern spin_lock(&ifp->lock); 34688048ced9SDavid Ahern prev = ifp->rt; 3469360a9887SDavid Ahern ifp->rt = f6i; 34708048ced9SDavid Ahern spin_unlock(&ifp->lock); 34718048ced9SDavid Ahern 347293531c67SDavid Ahern fib6_info_release(prev); 3473f1705ec1SDavid Ahern } 3474f1705ec1SDavid Ahern 3475f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3476f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 34778308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 34788308f3ffSDavid Ahern GFP_ATOMIC); 3479f1705ec1SDavid Ahern } 3480f1705ec1SDavid Ahern 34816d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3482f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3483f1705ec1SDavid Ahern 3484f1705ec1SDavid Ahern return 0; 3485f1705ec1SDavid Ahern } 3486f1705ec1SDavid Ahern 3487afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3488f1705ec1SDavid Ahern { 3489f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3490f1705ec1SDavid Ahern struct inet6_dev *idev; 3491f1705ec1SDavid Ahern 3492f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3493f1705ec1SDavid Ahern if (!idev) 3494f1705ec1SDavid Ahern return; 3495f1705ec1SDavid Ahern 3496f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3497f1705ec1SDavid Ahern 3498f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3499f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3500afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3501f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3502e669b869SEric Dumazet in6_ifa_hold(ifp); 3503f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3504f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3505f1705ec1SDavid Ahern 3506f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3507f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3508f1705ec1SDavid Ahern } 3509f1705ec1SDavid Ahern } 3510f1705ec1SDavid Ahern 3511f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3512f1705ec1SDavid Ahern } 3513f1705ec1SDavid Ahern 35141da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3515351638e7SJiri Pirko void *ptr) 35161da177e4SLinus Torvalds { 3517351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3518896585d4SHangbin Liu struct netdev_notifier_change_info *change_info; 35194f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3520748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 352160abc0beSWANG Cong struct net *net = dev_net(dev); 3522c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3523b217d616SHerbert Xu int err; 35241da177e4SLinus Torvalds 35251da177e4SLinus Torvalds switch (event) { 352645ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 352774235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 352845ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3529a317a2f1SWANG Cong if (IS_ERR(idev)) 3530a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 353145ba9dd2SYOSHIFUJI Hideaki } 353245ba9dd2SYOSHIFUJI Hideaki break; 3533bcdd553fSStephen Hemminger 3534b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3535b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3536b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 353760abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3538b7b0b1d2SAlexander Duyck break; 3539b7b0b1d2SAlexander Duyck } 3540b7b0b1d2SAlexander Duyck 3541b7b0b1d2SAlexander Duyck if (idev) { 3542b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3543b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3544b7b0b1d2SAlexander Duyck break; 3545b7b0b1d2SAlexander Duyck } 3546b7b0b1d2SAlexander Duyck 3547b7b0b1d2SAlexander Duyck /* allocate new idev */ 3548b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3549b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3550b7b0b1d2SAlexander Duyck break; 3551b7b0b1d2SAlexander Duyck 3552b7b0b1d2SAlexander Duyck /* device is still not ready */ 3553b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3554b7b0b1d2SAlexander Duyck break; 3555b7b0b1d2SAlexander Duyck 3556b7b0b1d2SAlexander Duyck run_pending = 1; 3557a8eceea8SJoe Perches fallthrough; 35581da177e4SLinus Torvalds case NETDEV_UP: 35593c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3560c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3561c2edacf8SJay Vosburgh break; 3562c2edacf8SJay Vosburgh 35633ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 35643ce62a84SWANG Cong break; 35653ce62a84SWANG Cong 35663c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 356738bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3568afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 356938bd10c4SDavid Ahern 35701f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35713c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 35727c62b8ddSLubomir Rintel pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 35733c21edbdSYOSHIFUJI Hideaki dev->name); 35743c21edbdSYOSHIFUJI Hideaki break; 35753c21edbdSYOSHIFUJI Hideaki } 357699081049SKristian Slavov 3577d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3578d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3579d31c7b8fSEvgeniy Polyakov 3580a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 358199081049SKristian Slavov idev->if_flags |= IF_READY; 3582e3ec6cfcSBenjamin Thery run_pending = 1; 3583e3ec6cfcSBenjamin Thery } 3584b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 35851f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35863c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 358727c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 35883c21edbdSYOSHIFUJI Hideaki break; 35893c21edbdSYOSHIFUJI Hideaki } 35903c21edbdSYOSHIFUJI Hideaki 3591896585d4SHangbin Liu if (!IS_ERR_OR_NULL(idev)) { 3592a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3593a088d1d7SLinus Lüssing /* device is already configured - 3594a088d1d7SLinus Lüssing * but resend MLD reports, we might 3595a088d1d7SLinus Lüssing * have roamed and need to update 3596a088d1d7SLinus Lüssing * multicast snooping switches 3597a088d1d7SLinus Lüssing */ 3598a088d1d7SLinus Lüssing ipv6_mc_up(idev); 3599896585d4SHangbin Liu change_info = ptr; 3600896585d4SHangbin Liu if (change_info->flags_changed & IFF_NOARP) 3601896585d4SHangbin Liu addrconf_dad_run(idev, true); 360227c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 36033c21edbdSYOSHIFUJI Hideaki break; 3604a088d1d7SLinus Lüssing } 36053c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 36063c21edbdSYOSHIFUJI Hideaki } 36073c21edbdSYOSHIFUJI Hideaki 3608f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 36093c21edbdSYOSHIFUJI Hideaki dev->name); 36103c21edbdSYOSHIFUJI Hideaki 3611c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 36123c21edbdSYOSHIFUJI Hideaki } 36133c21edbdSYOSHIFUJI Hideaki 36141da177e4SLinus Torvalds switch (dev->type) { 361507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 36161da177e4SLinus Torvalds case ARPHRD_SIT: 36171da177e4SLinus Torvalds addrconf_sit_config(dev); 36181da177e4SLinus Torvalds break; 36190be669bbSJoerg Roedel #endif 3620e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3621e5dd7294SAntonio Quartulli case ARPHRD_IP6GRE: 3622aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3623aee80b54Sstephen hemminger addrconf_gre_config(dev); 3624aee80b54Sstephen hemminger break; 3625aee80b54Sstephen hemminger #endif 36261da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 36271da177e4SLinus Torvalds init_loopback(dev); 36281da177e4SLinus Torvalds break; 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds default: 36311da177e4SLinus Torvalds addrconf_dev_config(dev); 36321da177e4SLinus Torvalds break; 36333ff50b79SStephen Hemminger } 3634bcdd553fSStephen Hemminger 3635a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3636c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3637896585d4SHangbin Liu addrconf_dad_run(idev, false); 3638c5e33bddSYOSHIFUJI Hideaki 36392127d95aSIdo Schimmel /* Device has an address by now */ 36402127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 36412127d95aSIdo Schimmel 3642bcdd553fSStephen Hemminger /* 3643bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3644bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3645bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 36461da177e4SLinus Torvalds */ 3647bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3648bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 36491da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 36501da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 36511da177e4SLinus Torvalds } 36521da177e4SLinus Torvalds idev->tstamp = jiffies; 36531da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3654bcdd553fSStephen Hemminger 3655bcdd553fSStephen Hemminger /* 3656bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3657bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 36581da177e4SLinus Torvalds */ 36591da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 366060abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 36611da177e4SLinus Torvalds } 36621da177e4SLinus Torvalds break; 36631da177e4SLinus Torvalds 36641da177e4SLinus Torvalds case NETDEV_DOWN: 36651da177e4SLinus Torvalds case NETDEV_UNREGISTER: 36661da177e4SLinus Torvalds /* 36671da177e4SLinus Torvalds * Remove all addresses from this interface. 36681da177e4SLinus Torvalds */ 36691da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 36701da177e4SLinus Torvalds break; 36713c21edbdSYOSHIFUJI Hideaki 36721da177e4SLinus Torvalds case NETDEV_CHANGENAME: 36731da177e4SLinus Torvalds if (idev) { 36745632c515SStephen Hemminger snmp6_unregister_dev(idev); 3675408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3676a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3677b217d616SHerbert Xu if (err) 3678b217d616SHerbert Xu return notifier_from_errno(err); 3679a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3680a317a2f1SWANG Cong if (err) { 3681a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3682a317a2f1SWANG Cong return notifier_from_errno(err); 3683a317a2f1SWANG Cong } 36845632c515SStephen Hemminger } 36851da177e4SLinus Torvalds break; 3686bcdd553fSStephen Hemminger 368793d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 368893d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 36893ef0952cSAndrew Lunn if (idev) 369093d9b7d7SJiri Pirko addrconf_type_change(dev, event); 369175c78500SMoni Shoua break; 36924f7f34eaSDavid Ahern 36934f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 36944f7f34eaSDavid Ahern info = ptr; 36954f7f34eaSDavid Ahern 36964f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 36974f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 36984f7f34eaSDavid Ahern */ 36994f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 3700ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, false); 37013ff50b79SStephen Hemminger } 37021da177e4SLinus Torvalds 37031da177e4SLinus Torvalds return NOTIFY_OK; 37041da177e4SLinus Torvalds } 37051da177e4SLinus Torvalds 37061da177e4SLinus Torvalds /* 37071da177e4SLinus Torvalds * addrconf module should be notified of a device going up 37081da177e4SLinus Torvalds */ 37091da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 37101da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3711242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 37121da177e4SLinus Torvalds }; 37131da177e4SLinus Torvalds 371493d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 371575c78500SMoni Shoua { 371675c78500SMoni Shoua struct inet6_dev *idev; 371775c78500SMoni Shoua ASSERT_RTNL(); 371875c78500SMoni Shoua 371975c78500SMoni Shoua idev = __in6_dev_get(dev); 372075c78500SMoni Shoua 372193d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 372275c78500SMoni Shoua ipv6_mc_remap(idev); 372393d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 372475c78500SMoni Shoua ipv6_mc_unmap(idev); 372575c78500SMoni Shoua } 372675c78500SMoni Shoua 372770af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 372870af921dSDavid Ahern { 372970af921dSDavid Ahern return ipv6_addr_type(addr) & 373070af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 373170af921dSDavid Ahern } 373270af921dSDavid Ahern 3733ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister) 37341da177e4SLinus Torvalds { 3735ae79dbf6SFlorent Fourcot unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN; 3736c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3737502a2ffdSstephen hemminger struct inet6_dev *idev; 373851454ea4SNiels Dossche struct inet6_ifaddr *ifa; 373951454ea4SNiels Dossche LIST_HEAD(tmp_addr_list); 37400aef78aaSIvan Vecera bool keep_addr = false; 37419995b408Sj.nixdorf@avm.de bool was_ready; 374273a8bd74SDavid S. Miller int state, i; 37431da177e4SLinus Torvalds 37441da177e4SLinus Torvalds ASSERT_RTNL(); 37451da177e4SLinus Torvalds 37464c981e28SIdo Schimmel rt6_disable_ip(dev, event); 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds idev = __in6_dev_get(dev); 374963159f29SIan Morris if (!idev) 37501da177e4SLinus Torvalds return -ENODEV; 37511da177e4SLinus Torvalds 3752bcdd553fSStephen Hemminger /* 3753bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3754bcdd553fSStephen Hemminger * Do not dev_put! 37551da177e4SLinus Torvalds */ 3756ae79dbf6SFlorent Fourcot if (unregister) { 37571da177e4SLinus Torvalds idev->dead = 1; 37588814c4b5SYOSHIFUJI Hideaki 37598814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3760a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 37631da177e4SLinus Torvalds snmp6_unregister_dev(idev); 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds } 37661da177e4SLinus Torvalds 3767f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3768f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3769f1705ec1SDavid Ahern */ 3770ae79dbf6SFlorent Fourcot if (!unregister && !idev->cnf.disable_ipv6) { 37710aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 37720aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 37730aef78aaSIvan Vecera 37740aef78aaSIvan Vecera if (!_keep_addr) 37750aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 37760aef78aaSIvan Vecera 37770aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 37780aef78aaSIvan Vecera } 3779f1705ec1SDavid Ahern 378073a8bd74SDavid S. Miller /* Step 2: clear hash table */ 378173a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3782e66d1172SEric Dumazet struct hlist_head *h = &net->ipv6.inet6_addr_lst[i]; 378373a8bd74SDavid S. Miller 3784e66d1172SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock); 378573a8bd74SDavid S. Miller restart: 3786b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 378773a8bd74SDavid S. Miller if (ifa->idev == idev) { 3788c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3789f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3790f1705ec1SDavid Ahern * address is retained on a down event 3791f1705ec1SDavid Ahern */ 3792f1705ec1SDavid Ahern if (!keep_addr || 379370af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 379470af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3795f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 379673a8bd74SDavid S. Miller goto restart; 379773a8bd74SDavid S. Miller } 379873a8bd74SDavid S. Miller } 3799f1705ec1SDavid Ahern } 3800e66d1172SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 380173a8bd74SDavid S. Miller } 380273a8bd74SDavid S. Miller 38031da177e4SLinus Torvalds write_lock_bh(&idev->lock); 38041da177e4SLinus Torvalds 3805b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3806b7b1bfceSHannes Frederic Sowa 38079995b408Sj.nixdorf@avm.de /* Step 2: clear flags for stateless addrconf, repeated down 38089995b408Sj.nixdorf@avm.de * detection 38099995b408Sj.nixdorf@avm.de */ 38109995b408Sj.nixdorf@avm.de was_ready = idev->if_flags & IF_READY; 3811ae79dbf6SFlorent Fourcot if (!unregister) 38123c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 38131da177e4SLinus Torvalds 3814bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3815372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3816372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3817372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3818372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 38191da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 38201da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds if (ifa->ifpub) { 38231da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 38241da177e4SLinus Torvalds ifa->ifpub = NULL; 38251da177e4SLinus Torvalds } 38261da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 38271da177e4SLinus Torvalds in6_ifa_put(ifa); 38281da177e4SLinus Torvalds write_lock_bh(&idev->lock); 38291da177e4SLinus Torvalds } 38308f37ada5Sstephen hemminger 383151454ea4SNiels Dossche list_for_each_entry(ifa, &idev->addr_list, if_list) 383251454ea4SNiels Dossche list_add_tail(&ifa->if_list_aux, &tmp_addr_list); 383351454ea4SNiels Dossche write_unlock_bh(&idev->lock); 383451454ea4SNiels Dossche 383551454ea4SNiels Dossche while (!list_empty(&tmp_addr_list)) { 38368d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3837a2d6cbb0SRabin Vincent bool keep; 383838bd10c4SDavid Ahern 383951454ea4SNiels Dossche ifa = list_first_entry(&tmp_addr_list, 384051454ea4SNiels Dossche struct inet6_ifaddr, if_list_aux); 384151454ea4SNiels Dossche list_del(&ifa->if_list_aux); 384251454ea4SNiels Dossche 3843c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 384484e8b803Sstephen hemminger 3845a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3846a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3847a2d6cbb0SRabin Vincent 38488e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3849f1705ec1SDavid Ahern 3850a2d6cbb0SRabin Vincent if (keep) { 3851f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3852f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 38536d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3854f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3855f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 385638bd10c4SDavid Ahern 385738bd10c4SDavid Ahern rt = ifa->rt; 385838bd10c4SDavid Ahern ifa->rt = NULL; 3859f1705ec1SDavid Ahern } else { 38604c5ff6a6SHerbert Xu state = ifa->state; 38614c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3862f1705ec1SDavid Ahern } 3863f1705ec1SDavid Ahern 38648e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 38654c5ff6a6SHerbert Xu 386638bd10c4SDavid Ahern if (rt) 386711dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 386838bd10c4SDavid Ahern 38699d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 38701da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3871f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3872ea06f717SMike Manning } else { 3873ea06f717SMike Manning if (idev->cnf.forwarding) 3874ea06f717SMike Manning addrconf_leave_anycast(ifa); 3875ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 38762de79570SLorenzo Colitti } 387773a8bd74SDavid S. Miller 38788ef802aaSEric Dumazet if (!keep) { 387951454ea4SNiels Dossche write_lock_bh(&idev->lock); 38808ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 388151454ea4SNiels Dossche write_unlock_bh(&idev->lock); 38828ef802aaSEric Dumazet in6_ifa_put(ifa); 38838ef802aaSEric Dumazet } 38841da177e4SLinus Torvalds } 38858f37ada5Sstephen hemminger 3886381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3887ae79dbf6SFlorent Fourcot if (unregister) { 3888381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 38891da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 38909995b408Sj.nixdorf@avm.de } else if (was_ready) { 38911da177e4SLinus Torvalds ipv6_mc_down(idev); 3892381f4dcaSSabrina Dubroca } 38931da177e4SLinus Torvalds 38941da177e4SLinus Torvalds idev->tstamp = jiffies; 389549b99da2SRocco Yue idev->ra_mtu = 0; 38961da177e4SLinus Torvalds 3897bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3898ae79dbf6SFlorent Fourcot if (unregister) { 3899408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 39001da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 39011da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 39021da177e4SLinus Torvalds in6_dev_put(idev); 39031da177e4SLinus Torvalds } 39041da177e4SLinus Torvalds return 0; 39051da177e4SLinus Torvalds } 39061da177e4SLinus Torvalds 3907e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 39081da177e4SLinus Torvalds { 3909e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3910caf92bc4SCong Wang struct net_device *dev = idev->dev; 3911b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 39121da177e4SLinus Torvalds 3913b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 39145b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 39151da177e4SLinus Torvalds goto out; 39161da177e4SLinus Torvalds 39179ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 39181da177e4SLinus Torvalds goto out; 39195b2a1953Sstephen hemminger 39205b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 39215b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 39225b2a1953Sstephen hemminger goto out; 39231da177e4SLinus Torvalds 3924bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3925caf92bc4SCong Wang write_unlock(&idev->lock); 3926caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3927caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3928b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3929b7b1bfceSHannes Frederic Sowa else 3930caf92bc4SCong Wang goto put; 3931b7b1bfceSHannes Frederic Sowa 3932caf92bc4SCong Wang write_lock(&idev->lock); 3933bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3934bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 39351da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3936b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3937b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 39385b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3939bd11f074SMaciej Żenczykowski idev->rs_interval); 39401da177e4SLinus Torvalds } else { 39411da177e4SLinus Torvalds /* 39421da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 39431da177e4SLinus Torvalds * assumption any longer. 39441da177e4SLinus Torvalds */ 394591df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 39461da177e4SLinus Torvalds } 39471da177e4SLinus Torvalds 39481da177e4SLinus Torvalds out: 3949b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3950caf92bc4SCong Wang put: 3951b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 39521da177e4SLinus Torvalds } 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds /* 39551da177e4SLinus Torvalds * Duplicate Address Detection 39561da177e4SLinus Torvalds */ 39573c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 39583c21edbdSYOSHIFUJI Hideaki { 39593c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 39603c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3961adc176c5SErik Nordmark u64 nonce; 39623c21edbdSYOSHIFUJI Hideaki 396395c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 396495c385b4SNeil Horman rand_num = 0; 396595c385b4SNeil Horman else 396663862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 396795c385b4SNeil Horman 3968adc176c5SErik Nordmark nonce = 0; 3969adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3970adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3971adc176c5SErik Nordmark do 3972adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3973adc176c5SErik Nordmark while (nonce == 0); 3974adc176c5SErik Nordmark } 3975adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3976b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3977c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 39783c21edbdSYOSHIFUJI Hideaki } 39793c21edbdSYOSHIFUJI Hideaki 3980c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 39811da177e4SLinus Torvalds { 39821da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 39831da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3984764d3be6SPaolo Abeni bool bump_id, notify = false; 3985afb1d4b5SDavid Ahern struct net *net; 39861da177e4SLinus Torvalds 39871da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 39881da177e4SLinus Torvalds 39891da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3990622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3991e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 39921da177e4SLinus Torvalds goto out; 39931da177e4SLinus Torvalds 3994afb1d4b5SDavid Ahern net = dev_net(dev); 39951da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3996afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3997a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 399855ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 399955ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 4000c76fe2d9SDavid Ahern bool send_na = false; 4001c76fe2d9SDavid Ahern 4002c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4003c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4004c76fe2d9SDavid Ahern send_na = true; 4005764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4006cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 400721809fafSstephen hemminger spin_unlock(&ifp->lock); 40081da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 40091da177e4SLinus Torvalds 4010c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40111da177e4SLinus Torvalds return; 40121da177e4SLinus Torvalds } 40131da177e4SLinus Torvalds 40146732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 401521809fafSstephen hemminger spin_unlock(&ifp->lock); 40166732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 40173c21edbdSYOSHIFUJI Hideaki /* 4018590a9887SMasatake YAMATO * If the device is not ready: 40193c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 40203c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 40213c21edbdSYOSHIFUJI Hideaki */ 40223c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 4023cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 40246732badeSYOSHIFUJI Hideaki return; 40253c21edbdSYOSHIFUJI Hideaki } 402695c385b4SNeil Horman 402795c385b4SNeil Horman /* 402895c385b4SNeil Horman * Optimistic nodes can start receiving 402995c385b4SNeil Horman * Frames right away 403095c385b4SNeil Horman */ 40317fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 4032afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 4033afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 40347fd2561eSErik Kline /* Because optimistic nodes can use this address, 40357fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 40367fd2561eSErik Kline */ 403716186a82Ssubashab@codeaurora.org notify = true; 40387fd2561eSErik Kline } 40397fd2561eSErik Kline } 404095c385b4SNeil Horman 40416732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 40421da177e4SLinus Torvalds out: 4043622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 40441da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 404516186a82Ssubashab@codeaurora.org if (notify) 404616186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 40471da177e4SLinus Torvalds } 40481da177e4SLinus Torvalds 4049c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 40501da177e4SLinus Torvalds { 4051c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 4052c15b1ccaSHannes Frederic Sowa 40538e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4054c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 4055c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 4056c15b1ccaSHannes Frederic Sowa begin_dad = true; 4057c15b1ccaSHannes Frederic Sowa } 40588e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4059c15b1ccaSHannes Frederic Sowa 4060c15b1ccaSHannes Frederic Sowa if (begin_dad) 4061c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 4062c15b1ccaSHannes Frederic Sowa } 4063c15b1ccaSHannes Frederic Sowa 4064c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 4065c15b1ccaSHannes Frederic Sowa { 4066c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 4067c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 4068c15b1ccaSHannes Frederic Sowa dad_work); 40691da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 4070764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 40711da177e4SLinus Torvalds struct in6_addr mcaddr; 40721da177e4SLinus Torvalds 4073c15b1ccaSHannes Frederic Sowa enum { 4074c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 4075c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 4076c15b1ccaSHannes Frederic Sowa DAD_ABORT, 4077c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 4078c15b1ccaSHannes Frederic Sowa 4079c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4080c15b1ccaSHannes Frederic Sowa 40818e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4082c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 4083c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 4084c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 4085c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 4086c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 4087c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 408885b51b12SMike Manning 408935e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 409035e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 409135e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 409285b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 409385b51b12SMike Manning struct in6_addr addr; 409485b51b12SMike Manning 409585b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 409685b51b12SMike Manning addr.s6_addr32[1] = 0; 409785b51b12SMike Manning 409885b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 409985b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 410085b51b12SMike Manning /* DAD failed for link-local based on MAC */ 410185b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 410285b51b12SMike Manning 410385b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 410485b51b12SMike Manning ifp->idev->dev->name); 410585b51b12SMike Manning disable_ipv6 = true; 410685b51b12SMike Manning } 410785b51b12SMike Manning } 4108c15b1ccaSHannes Frederic Sowa } 41098e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4110c15b1ccaSHannes Frederic Sowa 4111c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4112c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4113c15b1ccaSHannes Frederic Sowa goto out; 4114c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4115751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4116c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 411785b51b12SMike Manning if (disable_ipv6) 4118ae79dbf6SFlorent Fourcot addrconf_ifdown(idev->dev, false); 4119c15b1ccaSHannes Frederic Sowa goto out; 4120c15b1ccaSHannes Frederic Sowa } 4121c15b1ccaSHannes Frederic Sowa 4122b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4123f2344a13SHerbert Xu goto out; 4124f2344a13SHerbert Xu 4125c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4126122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4127c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41281da177e4SLinus Torvalds goto out; 41291da177e4SLinus Torvalds } 413021809fafSstephen hemminger 413121809fafSstephen hemminger spin_lock(&ifp->lock); 4132622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4133622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4134c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4135622ccdf1SHerbert Xu goto out; 4136622ccdf1SHerbert Xu } 4137622ccdf1SHerbert Xu 4138b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4139c76fe2d9SDavid Ahern bool send_na = false; 4140c76fe2d9SDavid Ahern 41411da177e4SLinus Torvalds /* 41421da177e4SLinus Torvalds * DAD was successful 41431da177e4SLinus Torvalds */ 41441da177e4SLinus Torvalds 4145c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4146c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4147c76fe2d9SDavid Ahern send_na = true; 4148764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4149cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 415021809fafSstephen hemminger spin_unlock(&ifp->lock); 4151c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41521da177e4SLinus Torvalds 4153c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 41541da177e4SLinus Torvalds 41551da177e4SLinus Torvalds goto out; 41561da177e4SLinus Torvalds } 41571da177e4SLinus Torvalds 4158b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4159c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 416019e16d22SHangbin Liu max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), 416119e16d22SHangbin Liu HZ/100)); 416221809fafSstephen hemminger spin_unlock(&ifp->lock); 4163c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41641da177e4SLinus Torvalds 41651da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 41661da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4167adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4168adc176c5SErik Nordmark ifp->dad_nonce); 41691da177e4SLinus Torvalds out: 41701da177e4SLinus Torvalds in6_ifa_put(ifp); 4171c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 41721da177e4SLinus Torvalds } 41731da177e4SLinus Torvalds 417411ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 417511ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 417611ffff75SHannes Frederic Sowa { 417711ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 417811ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 417911ffff75SHannes Frederic Sowa 4180602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4181602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4182602582caSHannes Frederic Sowa break; 418311ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 418411ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 418511ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 418611ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 418711ffff75SHannes Frederic Sowa return false; 418811ffff75SHannes Frederic Sowa } 418911ffff75SHannes Frederic Sowa return true; 419011ffff75SHannes Frederic Sowa } 419111ffff75SHannes Frederic Sowa 4192c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4193c76fe2d9SDavid Ahern bool send_na) 41941da177e4SLinus Torvalds { 41951da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4196b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4197b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4198b7b1bfceSHannes Frederic Sowa 4199c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 42001da177e4SLinus Torvalds 42011da177e4SLinus Torvalds /* 42021da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 42031da177e4SLinus Torvalds */ 42041da177e4SLinus Torvalds 42051da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 42061da177e4SLinus Torvalds 4207026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4208026359bcSTore Anderson router advertisements, start sending router solicitations. 42091da177e4SLinus Torvalds */ 42101da177e4SLinus Torvalds 42111ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 421211ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4213b173ee48SHannes Frederic Sowa send_rs = send_mld && 4214b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4215bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4216b52e1cceSjianghaoran (dev->flags & IFF_LOOPBACK) == 0 && 4217b52e1cceSjianghaoran (dev->type != ARPHRD_TUNNEL); 42181ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 42191ec047ebSHannes Frederic Sowa 4220b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4221b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4222b173ee48SHannes Frederic Sowa */ 4223b173ee48SHannes Frederic Sowa if (send_mld) 4224b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4225b173ee48SHannes Frederic Sowa 4226c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4227c76fe2d9SDavid Ahern if (send_na && 4228c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4229c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4230c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4231c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4232c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4233c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4234c76fe2d9SDavid Ahern } 4235c76fe2d9SDavid Ahern 42361ec047ebSHannes Frederic Sowa if (send_rs) { 42371da177e4SLinus Torvalds /* 42381da177e4SLinus Torvalds * If a host as already performed a random delay 42391da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 42401da177e4SLinus Torvalds * to delay again before sending the first RS 42411da177e4SLinus Torvalds */ 42421ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4243b7b1bfceSHannes Frederic Sowa return; 42441ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 42451da177e4SLinus Torvalds 4246b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4247b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4248bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4249bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4250b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 42511da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4252bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4253b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4254b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 42551da177e4SLinus Torvalds } 4256764d3be6SPaolo Abeni 4257764d3be6SPaolo Abeni if (bump_id) 4258764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4259a11a7f71SMarcus Huewe 4260a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4261a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4262a11a7f71SMarcus Huewe */ 4263a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 42648805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev)); 42651da177e4SLinus Torvalds } 42661da177e4SLinus Torvalds 4267896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart) 4268e21e8467SStephen Hemminger { 4269c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4270c5e33bddSYOSHIFUJI Hideaki 4271c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4272502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 427321809fafSstephen hemminger spin_lock(&ifp->lock); 4274896585d4SHangbin Liu if ((ifp->flags & IFA_F_TENTATIVE && 4275896585d4SHangbin Liu ifp->state == INET6_IFADDR_STATE_DAD) || restart) { 4276896585d4SHangbin Liu if (restart) 4277896585d4SHangbin Liu ifp->state = INET6_IFADDR_STATE_PREDAD; 4278c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4279896585d4SHangbin Liu } 4280f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4281c5e33bddSYOSHIFUJI Hideaki } 4282c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4283c5e33bddSYOSHIFUJI Hideaki } 4284c5e33bddSYOSHIFUJI Hideaki 42851da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 42861da177e4SLinus Torvalds struct if6_iter_state { 42873c40090aSDaniel Lezcano struct seq_net_private p; 42881da177e4SLinus Torvalds int bucket; 42891d578303SMihai Maruseac int offset; 42901da177e4SLinus Torvalds }; 42911da177e4SLinus Torvalds 42921d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 42931da177e4SLinus Torvalds { 42941da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42951218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4296a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 42971d578303SMihai Maruseac int p = 0; 42981da177e4SLinus Torvalds 42991d578303SMihai Maruseac /* initial bucket if pos is 0 */ 43001d578303SMihai Maruseac if (pos == 0) { 43011d578303SMihai Maruseac state->bucket = 0; 43021d578303SMihai Maruseac state->offset = 0; 43031d578303SMihai Maruseac } 43041d578303SMihai Maruseac 43051d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4306e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket], 43071d578303SMihai Maruseac addr_lst) { 43081d578303SMihai Maruseac /* sync with offset */ 43091d578303SMihai Maruseac if (p < state->offset) { 43101d578303SMihai Maruseac p++; 43111d578303SMihai Maruseac continue; 43121d578303SMihai Maruseac } 43131da177e4SLinus Torvalds return ifa; 43141da177e4SLinus Torvalds } 43151d578303SMihai Maruseac 43161d578303SMihai Maruseac /* prepare for next bucket */ 43171d578303SMihai Maruseac state->offset = 0; 43181d578303SMihai Maruseac p = 0; 43191d578303SMihai Maruseac } 4320c2e21293Sstephen hemminger return NULL; 4321c2e21293Sstephen hemminger } 43221da177e4SLinus Torvalds 4323c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4324c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 43251da177e4SLinus Torvalds { 43261da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 43271218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 43281da177e4SLinus Torvalds 4329a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 43301d578303SMihai Maruseac state->offset++; 43311da177e4SLinus Torvalds return ifa; 43321d578303SMihai Maruseac } 43331da177e4SLinus Torvalds 43341d578303SMihai Maruseac state->offset = 0; 433586f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) { 4336a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4337e66d1172SEric Dumazet &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) { 4338c2e21293Sstephen hemminger return ifa; 4339c2e21293Sstephen hemminger } 4340c2e21293Sstephen hemminger } 4341c2e21293Sstephen hemminger 4342c2e21293Sstephen hemminger return NULL; 4343c2e21293Sstephen hemminger } 4344c2e21293Sstephen hemminger 43451da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4346a5c1d98fSEric Dumazet __acquires(rcu) 43471da177e4SLinus Torvalds { 4348a5c1d98fSEric Dumazet rcu_read_lock(); 43491d578303SMihai Maruseac return if6_get_first(seq, *pos); 43501da177e4SLinus Torvalds } 43511da177e4SLinus Torvalds 43521da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 43531da177e4SLinus Torvalds { 43541da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 43551da177e4SLinus Torvalds 43561da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 43571da177e4SLinus Torvalds ++*pos; 43581da177e4SLinus Torvalds return ifa; 43591da177e4SLinus Torvalds } 43601da177e4SLinus Torvalds 43611da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4362a5c1d98fSEric Dumazet __releases(rcu) 43631da177e4SLinus Torvalds { 4364a5c1d98fSEric Dumazet rcu_read_unlock(); 43651da177e4SLinus Torvalds } 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 43681da177e4SLinus Torvalds { 43691da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4370971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4371b071195dSHarvey Harrison &ifp->addr, 43721da177e4SLinus Torvalds ifp->idev->dev->ifindex, 43731da177e4SLinus Torvalds ifp->prefix_len, 43741da177e4SLinus Torvalds ifp->scope, 4375971a351cSJiri Pirko (u8) ifp->flags, 43761da177e4SLinus Torvalds ifp->idev->dev->name); 43771da177e4SLinus Torvalds return 0; 43781da177e4SLinus Torvalds } 43791da177e4SLinus Torvalds 438056b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 43811da177e4SLinus Torvalds .start = if6_seq_start, 43821da177e4SLinus Torvalds .next = if6_seq_next, 43831da177e4SLinus Torvalds .show = if6_seq_show, 43841da177e4SLinus Torvalds .stop = if6_seq_stop, 43851da177e4SLinus Torvalds }; 43861da177e4SLinus Torvalds 43872c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 43881da177e4SLinus Torvalds { 4389c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4390c3506372SChristoph Hellwig sizeof(struct if6_iter_state))) 43911da177e4SLinus Torvalds return -ENOMEM; 43921da177e4SLinus Torvalds return 0; 43931da177e4SLinus Torvalds } 43941da177e4SLinus Torvalds 43952c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 43963c40090aSDaniel Lezcano { 4397ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 43983c40090aSDaniel Lezcano } 43993c40090aSDaniel Lezcano 44003c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 44013c40090aSDaniel Lezcano .init = if6_proc_net_init, 44023c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 44033c40090aSDaniel Lezcano }; 44043c40090aSDaniel Lezcano 44053c40090aSDaniel Lezcano int __init if6_proc_init(void) 44063c40090aSDaniel Lezcano { 44073c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 44083c40090aSDaniel Lezcano } 44093c40090aSDaniel Lezcano 44101da177e4SLinus Torvalds void if6_proc_exit(void) 44111da177e4SLinus Torvalds { 44123c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 44131da177e4SLinus Torvalds } 44141da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 44151da177e4SLinus Torvalds 441607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 44173b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4418b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 44193b9f9a1cSNoriaki TAKAMIYA { 44203f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4421c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 44223f27fb23SEric Dumazet int ret = 0; 4423c2e21293Sstephen hemminger 44244e5f47abSEric Dumazet rcu_read_lock(); 4425e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4426caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 44273b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 44283b9f9a1cSNoriaki TAKAMIYA ret = 1; 44293b9f9a1cSNoriaki TAKAMIYA break; 44303b9f9a1cSNoriaki TAKAMIYA } 44313b9f9a1cSNoriaki TAKAMIYA } 44324e5f47abSEric Dumazet rcu_read_unlock(); 44333b9f9a1cSNoriaki TAKAMIYA return ret; 44343b9f9a1cSNoriaki TAKAMIYA } 44353b9f9a1cSNoriaki TAKAMIYA #endif 44363b9f9a1cSNoriaki TAKAMIYA 4437f37c6059SAlexander Aring /* RFC6554 has some algorithm to avoid loops in segment routing by 4438f37c6059SAlexander Aring * checking if the segments contains any of a local interface address. 4439f37c6059SAlexander Aring * 4440f37c6059SAlexander Aring * Quote: 4441f37c6059SAlexander Aring * 4442f37c6059SAlexander Aring * To detect loops in the SRH, a router MUST determine if the SRH 4443f37c6059SAlexander Aring * includes multiple addresses assigned to any interface on that router. 4444f37c6059SAlexander Aring * If such addresses appear more than once and are separated by at least 4445f37c6059SAlexander Aring * one address not assigned to that router. 4446f37c6059SAlexander Aring */ 4447f37c6059SAlexander Aring int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs, 4448f37c6059SAlexander Aring unsigned char nsegs) 4449f37c6059SAlexander Aring { 4450f37c6059SAlexander Aring const struct in6_addr *addr; 4451f37c6059SAlexander Aring int i, ret = 0, found = 0; 4452f37c6059SAlexander Aring struct inet6_ifaddr *ifp; 4453f37c6059SAlexander Aring bool separated = false; 4454f37c6059SAlexander Aring unsigned int hash; 4455f37c6059SAlexander Aring bool hash_found; 4456f37c6059SAlexander Aring 4457f37c6059SAlexander Aring rcu_read_lock(); 4458f37c6059SAlexander Aring for (i = 0; i < nsegs; i++) { 4459f37c6059SAlexander Aring addr = &segs[i]; 4460f37c6059SAlexander Aring hash = inet6_addr_hash(net, addr); 4461f37c6059SAlexander Aring 4462f37c6059SAlexander Aring hash_found = false; 4463e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4464f37c6059SAlexander Aring 4465f37c6059SAlexander Aring if (ipv6_addr_equal(&ifp->addr, addr)) { 4466f37c6059SAlexander Aring hash_found = true; 4467f37c6059SAlexander Aring break; 4468f37c6059SAlexander Aring } 4469f37c6059SAlexander Aring } 4470f37c6059SAlexander Aring 4471f37c6059SAlexander Aring if (hash_found) { 4472f37c6059SAlexander Aring if (found > 1 && separated) { 4473f37c6059SAlexander Aring ret = 1; 4474f37c6059SAlexander Aring break; 4475f37c6059SAlexander Aring } 4476f37c6059SAlexander Aring 4477f37c6059SAlexander Aring separated = false; 4478f37c6059SAlexander Aring found++; 4479f37c6059SAlexander Aring } else { 4480f37c6059SAlexander Aring separated = true; 4481f37c6059SAlexander Aring } 4482f37c6059SAlexander Aring } 4483f37c6059SAlexander Aring rcu_read_unlock(); 4484f37c6059SAlexander Aring 4485f37c6059SAlexander Aring return ret; 4486f37c6059SAlexander Aring } 4487f37c6059SAlexander Aring 44881da177e4SLinus Torvalds /* 44891da177e4SLinus Torvalds * Periodic address status verification 44901da177e4SLinus Torvalds */ 44911da177e4SLinus Torvalds 44928805d13fSEric Dumazet static void addrconf_verify_rtnl(struct net *net) 44931da177e4SLinus Torvalds { 4494b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 44951da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 44961da177e4SLinus Torvalds int i; 44971da177e4SLinus Torvalds 4498c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4499c15b1ccaSHannes Frederic Sowa 45005c578aedSstephen hemminger rcu_read_lock_bh(); 45011da177e4SLinus Torvalds now = jiffies; 4502b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 45031da177e4SLinus Torvalds 45048805d13fSEric Dumazet cancel_delayed_work(&net->ipv6.addr_chk_work); 45051da177e4SLinus Torvalds 45061da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 45071da177e4SLinus Torvalds restart: 4508e66d1172SEric Dumazet hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) { 45091da177e4SLinus Torvalds unsigned long age; 45101da177e4SLinus Torvalds 4511fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4512fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4513fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4514fad8da3eSYasushi Asano */ 4515fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4516fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 45171da177e4SLinus Torvalds continue; 45181da177e4SLinus Torvalds 45191da177e4SLinus Torvalds spin_lock(&ifp->lock); 4520b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4521b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 45221da177e4SLinus Torvalds 45238f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 45248f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 45251da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45261da177e4SLinus Torvalds in6_ifa_hold(ifp); 4527aa8caa76STaehee Yoo rcu_read_unlock_bh(); 45281da177e4SLinus Torvalds ipv6_del_addr(ifp); 4529aa8caa76STaehee Yoo rcu_read_lock_bh(); 45301da177e4SLinus Torvalds goto restart; 45318f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 45328f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 45338f27ebb9SYOSHIFUJI Hideaki continue; 45341da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4535a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 45361da177e4SLinus Torvalds int deprecate = 0; 45371da177e4SLinus Torvalds 45381da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 45391da177e4SLinus Torvalds deprecate = 1; 45401da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 45411da177e4SLinus Torvalds } 45421da177e4SLinus Torvalds 4543fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4544fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 45451da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 45461da177e4SLinus Torvalds 45471da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45481da177e4SLinus Torvalds 45491da177e4SLinus Torvalds if (deprecate) { 45501da177e4SLinus Torvalds in6_ifa_hold(ifp); 45511da177e4SLinus Torvalds 45521da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 45531da177e4SLinus Torvalds in6_ifa_put(ifp); 45541da177e4SLinus Torvalds goto restart; 45551da177e4SLinus Torvalds } 45561da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 45571da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 455888949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 455988949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 456019e16d22SHangbin Liu max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ; 456188949cf4Sstephen hemminger 45621da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 45631da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 45641da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 45651da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 45661da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 45671da177e4SLinus Torvalds ifp->regen_count++; 45681da177e4SLinus Torvalds in6_ifa_hold(ifp); 45691da177e4SLinus Torvalds in6_ifa_hold(ifpub); 45701da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45715c578aedSstephen hemminger 4572291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4573291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4574291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4575e64e469bSEric Dumazet rcu_read_unlock_bh(); 4576969c5464SFernando Gont ipv6_create_tempaddr(ifpub, true); 45771da177e4SLinus Torvalds in6_ifa_put(ifpub); 45781da177e4SLinus Torvalds in6_ifa_put(ifp); 4579e64e469bSEric Dumazet rcu_read_lock_bh(); 45801da177e4SLinus Torvalds goto restart; 45811da177e4SLinus Torvalds } 45821da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 45831da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 45841da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45851da177e4SLinus Torvalds } else { 45861da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 45871da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 45881da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 45891da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45901da177e4SLinus Torvalds } 45911da177e4SLinus Torvalds } 45921da177e4SLinus Torvalds } 45931da177e4SLinus Torvalds 4594b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4595b2db7564SYOSHIFUJI Hideaki next_sched = next; 4596b2db7564SYOSHIFUJI Hideaki 4597b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4598b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4599b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4600b2db7564SYOSHIFUJI Hideaki 4601b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4602b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4603b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4604b2db7564SYOSHIFUJI Hideaki 4605e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4606ba3542e1Sdingtianhong now, next, next_sec, next_sched); 46078805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now); 46085c578aedSstephen hemminger rcu_read_unlock_bh(); 46091da177e4SLinus Torvalds } 46101da177e4SLinus Torvalds 4611c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4612c15b1ccaSHannes Frederic Sowa { 46138805d13fSEric Dumazet struct net *net = container_of(to_delayed_work(w), struct net, 46148805d13fSEric Dumazet ipv6.addr_chk_work); 46158805d13fSEric Dumazet 4616c15b1ccaSHannes Frederic Sowa rtnl_lock(); 46178805d13fSEric Dumazet addrconf_verify_rtnl(net); 4618c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4619c15b1ccaSHannes Frederic Sowa } 4620c15b1ccaSHannes Frederic Sowa 46218805d13fSEric Dumazet static void addrconf_verify(struct net *net) 4622c15b1ccaSHannes Frederic Sowa { 46238805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0); 4624c15b1ccaSHannes Frederic Sowa } 4625c15b1ccaSHannes Frederic Sowa 4626caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4627caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4628461d8837SThomas Graf { 4629461d8837SThomas Graf struct in6_addr *pfx = NULL; 4630461d8837SThomas Graf 4631caeaba79SNicolas Dichtel *peer_pfx = NULL; 4632caeaba79SNicolas Dichtel 4633461d8837SThomas Graf if (addr) 4634461d8837SThomas Graf pfx = nla_data(addr); 4635461d8837SThomas Graf 4636461d8837SThomas Graf if (local) { 4637461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4638caeaba79SNicolas Dichtel *peer_pfx = pfx; 4639461d8837SThomas Graf pfx = nla_data(local); 4640461d8837SThomas Graf } 4641461d8837SThomas Graf 4642461d8837SThomas Graf return pfx; 4643461d8837SThomas Graf } 4644461d8837SThomas Graf 4645ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4646461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4647461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4648461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4649479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 46508308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 46516ecf4c37SChristian Brauner [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 465247f0bd50SJacques de Laval [IFA_PROTO] = { .type = NLA_U8 }, 4653461d8837SThomas Graf }; 4654461d8837SThomas Graf 46551da177e4SLinus Torvalds static int 4656c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4657c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46581da177e4SLinus Torvalds { 46593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4660b933f716SThomas Graf struct ifaddrmsg *ifm; 4661b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4662caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 46636046d5b4SHeiner Kallweit u32 ifa_flags; 4664b933f716SThomas Graf int err; 46651da177e4SLinus Torvalds 46668cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 46678cb08174SJohannes Berg ifa_ipv6_policy, extack); 4668b933f716SThomas Graf if (err < 0) 4669b933f716SThomas Graf return err; 4670b933f716SThomas Graf 4671b933f716SThomas Graf ifm = nlmsg_data(nlh); 4672caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 467363159f29SIan Morris if (!pfx) 46741da177e4SLinus Torvalds return -EINVAL; 46751da177e4SLinus Torvalds 46766046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 46776046d5b4SHeiner Kallweit 46786046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 46796046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 46806046d5b4SHeiner Kallweit 46816046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 46826046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 46831da177e4SLinus Torvalds } 46841da177e4SLinus Torvalds 46858308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 468661794012SHangbin Liu unsigned long expires, u32 flags, 468761794012SHangbin Liu bool modify_peer) 46888308f3ffSDavid Ahern { 46898308f3ffSDavid Ahern struct fib6_info *f6i; 4690e7c7faa9SDavid Ahern u32 prio; 46918308f3ffSDavid Ahern 469261794012SHangbin Liu f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 469361794012SHangbin Liu ifp->prefix_len, 46942b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true); 46958308f3ffSDavid Ahern if (!f6i) 46968308f3ffSDavid Ahern return -ENOENT; 46978308f3ffSDavid Ahern 4698e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4699e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) { 4700e7c7faa9SDavid Ahern /* delete old one */ 470111dd74b3SRoopa Prabhu ip6_del_rt(dev_net(ifp->idev->dev), f6i, false); 4702e7c7faa9SDavid Ahern 47038308f3ffSDavid Ahern /* add new one */ 470461794012SHangbin Liu addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 470561794012SHangbin Liu ifp->prefix_len, 47068308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 47078308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 47088308f3ffSDavid Ahern } else { 47098308f3ffSDavid Ahern if (!expires) 47108308f3ffSDavid Ahern fib6_clean_expires(f6i); 47118308f3ffSDavid Ahern else 47128308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 47138308f3ffSDavid Ahern 47148308f3ffSDavid Ahern fib6_info_release(f6i); 47158308f3ffSDavid Ahern } 47168308f3ffSDavid Ahern 47178308f3ffSDavid Ahern return 0; 47188308f3ffSDavid Ahern } 47198308f3ffSDavid Ahern 47208805d13fSEric Dumazet static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp, 47218805d13fSEric Dumazet struct ifa6_config *cfg) 4722081bba5bSNoriaki TAKAMIYA { 47236f704992SYOSHIFUJI Hideaki u32 flags; 47246f704992SYOSHIFUJI Hideaki clock_t expires; 47254bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 472653bd6749SJiri Pirko bool was_managetempaddr; 47275b84efecSThomas Haller bool had_prefixroute; 4728d0098e4cSHangbin Liu bool new_peer = false; 472946d48046SYOSHIFUJI Hideaki 4730c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4731c15b1ccaSHannes Frederic Sowa 4732d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4733081bba5bSNoriaki TAKAMIYA return -EINVAL; 4734081bba5bSNoriaki TAKAMIYA 4735d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 473653bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 473753bd6749SJiri Pirko return -EINVAL; 473853bd6749SJiri Pirko 4739f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4740d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4741f1c02cfbSSabrina Dubroca 4742d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 47434bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 47444bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4745d169a1f8SDavid Ahern cfg->valid_lft = timeout; 47466f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 47474bed72e4SYOSHIFUJI Hideaki } else { 47484bed72e4SYOSHIFUJI Hideaki expires = 0; 47494bed72e4SYOSHIFUJI Hideaki flags = 0; 4750d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 47516f704992SYOSHIFUJI Hideaki } 4752081bba5bSNoriaki TAKAMIYA 4753d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 47544bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 47554bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4756d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4757d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 47584bed72e4SYOSHIFUJI Hideaki } 4759081bba5bSNoriaki TAKAMIYA 4760d0098e4cSHangbin Liu if (cfg->peer_pfx && 4761d0098e4cSHangbin Liu memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) { 4762d0098e4cSHangbin Liu if (!ipv6_addr_any(&ifp->peer_addr)) 4763d0098e4cSHangbin Liu cleanup_prefix_route(ifp, expires, true, true); 4764d0098e4cSHangbin Liu new_peer = true; 4765d0098e4cSHangbin Liu } 4766d0098e4cSHangbin Liu 4767081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 476853bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 47695b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 47705b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 477153bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4772761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4773761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4774d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4775081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4776d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4777d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 477847f0bd50SJacques de Laval ifp->ifa_proto = cfg->ifa_proto; 4779081bba5bSNoriaki TAKAMIYA 47808308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 47818308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 4782081bba5bSNoriaki TAKAMIYA 4783d0098e4cSHangbin Liu if (new_peer) 4784d0098e4cSHangbin Liu ifp->peer_addr = *cfg->peer_pfx; 4785d0098e4cSHangbin Liu 4786081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4787081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4788081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4789081bba5bSNoriaki TAKAMIYA 4790d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 47918308f3ffSDavid Ahern int rc = -ENOENT; 47928308f3ffSDavid Ahern 47938308f3ffSDavid Ahern if (had_prefixroute) 479461794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, false); 47958308f3ffSDavid Ahern 47968308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 47978308f3ffSDavid Ahern if (rc == -ENOENT) { 4798acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 47998308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 48008308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 48018308f3ffSDavid Ahern } 480261794012SHangbin Liu 480361794012SHangbin Liu if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr)) 480461794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, true); 480561794012SHangbin Liu 480661794012SHangbin Liu if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) { 480761794012SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len, 480861794012SHangbin Liu ifp->rt_priority, ifp->idev->dev, 480961794012SHangbin Liu expires, flags, GFP_KERNEL); 481061794012SHangbin Liu } 48115b84efecSThomas Haller } else if (had_prefixroute) { 48125b84efecSThomas Haller enum cleanup_prefix_rt_t action; 48135b84efecSThomas Haller unsigned long rt_expires; 48145b84efecSThomas Haller 48155b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 48165b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 48175b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 48185b84efecSThomas Haller 48195b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 48205b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 4821d0098e4cSHangbin Liu action == CLEANUP_PREFIX_RT_DEL, false); 48225b84efecSThomas Haller } 4823761aac73SThomas Haller } 482453bd6749SJiri Pirko 482553bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4826d169a1f8SDavid Ahern if (was_managetempaddr && 4827d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4828d169a1f8SDavid Ahern cfg->valid_lft = 0; 4829d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4830d169a1f8SDavid Ahern } 4831d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4832d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4833d169a1f8SDavid Ahern jiffies); 483453bd6749SJiri Pirko } 483553bd6749SJiri Pirko 48368805d13fSEric Dumazet addrconf_verify_rtnl(net); 4837081bba5bSNoriaki TAKAMIYA 4838081bba5bSNoriaki TAKAMIYA return 0; 4839081bba5bSNoriaki TAKAMIYA } 4840081bba5bSNoriaki TAKAMIYA 4841081bba5bSNoriaki TAKAMIYA static int 4842c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4843c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 48441da177e4SLinus Torvalds { 48453b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4846461d8837SThomas Graf struct ifaddrmsg *ifm; 4847461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 484819b1518cSDavid Ahern struct in6_addr *peer_pfx; 48497198f8ceSThomas Graf struct inet6_ifaddr *ifa; 48507198f8ceSThomas Graf struct net_device *dev; 4851f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 485219b1518cSDavid Ahern struct ifa6_config cfg; 4853461d8837SThomas Graf int err; 48541da177e4SLinus Torvalds 48558cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 48568cb08174SJohannes Berg ifa_ipv6_policy, extack); 4857461d8837SThomas Graf if (err < 0) 4858461d8837SThomas Graf return err; 4859461d8837SThomas Graf 486019b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 486119b1518cSDavid Ahern 4862461d8837SThomas Graf ifm = nlmsg_data(nlh); 486319b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 486419b1518cSDavid Ahern if (!cfg.pfx) 48651da177e4SLinus Torvalds return -EINVAL; 48661da177e4SLinus Torvalds 486719b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 486819b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 48698308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 48708308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 48718308f3ffSDavid Ahern 487247f0bd50SJacques de Laval if (tb[IFA_PROTO]) 487347f0bd50SJacques de Laval cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]); 487447f0bd50SJacques de Laval 487519b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 487619b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 487719b1518cSDavid Ahern 4878461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 48790778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4880461d8837SThomas Graf 4881461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 488219b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 488319b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 48840778769dSNoriaki TAKAMIYA } 48850778769dSNoriaki TAKAMIYA 4886af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 488763159f29SIan Morris if (!dev) 48887198f8ceSThomas Graf return -ENODEV; 4889081bba5bSNoriaki TAKAMIYA 489019b1518cSDavid Ahern if (tb[IFA_FLAGS]) 489119b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 489219b1518cSDavid Ahern else 489319b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4894479840ffSJiri Pirko 489555ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 489619b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 489719b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 489819b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4899f1c02cfbSSabrina Dubroca 4900f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4901db0b99f5SSabrina Dubroca if (IS_ERR(idev)) 4902db0b99f5SSabrina Dubroca return PTR_ERR(idev); 4903f1c02cfbSSabrina Dubroca 4904f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 490519b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4906f1c02cfbSSabrina Dubroca 490719b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 490819b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4909f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4910f1c02cfbSSabrina Dubroca return -EINVAL; 4911f1c02cfbSSabrina Dubroca } 491255ebaef1SNoriaki TAKAMIYA 491319b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 491463159f29SIan Morris if (!ifa) { 49157198f8ceSThomas Graf /* 49167198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4917db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 49187198f8ceSThomas Graf */ 491919b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 49201da177e4SLinus Torvalds } 49211da177e4SLinus Torvalds 49227198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 49237198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 49247198f8ceSThomas Graf err = -EEXIST; 49257198f8ceSThomas Graf else 49268805d13fSEric Dumazet err = inet6_addr_modify(net, ifa, &cfg); 49277198f8ceSThomas Graf 49287198f8ceSThomas Graf in6_ifa_put(ifa); 49297198f8ceSThomas Graf 49307198f8ceSThomas Graf return err; 49317198f8ceSThomas Graf } 49327198f8ceSThomas Graf 4933479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4934101bb229SThomas Graf u8 scope, int ifindex) 4935101bb229SThomas Graf { 4936101bb229SThomas Graf struct ifaddrmsg *ifm; 4937101bb229SThomas Graf 4938101bb229SThomas Graf ifm = nlmsg_data(nlh); 4939101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4940101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4941101bb229SThomas Graf ifm->ifa_flags = flags; 4942101bb229SThomas Graf ifm->ifa_scope = scope; 4943101bb229SThomas Graf ifm->ifa_index = ifindex; 4944101bb229SThomas Graf } 4945101bb229SThomas Graf 494685486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 494785486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 494885486af0SThomas Graf { 494985486af0SThomas Graf struct ifa_cacheinfo ci; 495085486af0SThomas Graf 495118a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 495218a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 495385486af0SThomas Graf ci.ifa_prefered = preferred; 495485486af0SThomas Graf ci.ifa_valid = valid; 495585486af0SThomas Graf 495685486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 495785486af0SThomas Graf } 495885486af0SThomas Graf 4959101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4960101bb229SThomas Graf { 4961101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4962101bb229SThomas Graf return RT_SCOPE_HOST; 4963101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4964101bb229SThomas Graf return RT_SCOPE_LINK; 4965101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4966101bb229SThomas Graf return RT_SCOPE_SITE; 4967101bb229SThomas Graf else 4968101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4969101bb229SThomas Graf } 4970101bb229SThomas Graf 49710ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 49720ab6803bSThomas Graf { 4973339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4974caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4975339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4976479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 49778308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 497847f0bd50SJacques de Laval + nla_total_size(1) /* IFA_PROTO */ 49798308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 49800ab6803bSThomas Graf } 4981c5396a31SYOSHIFUJI Hideaki 49826ba1e6e8SDavid Ahern enum addr_type_t { 49836ba1e6e8SDavid Ahern UNICAST_ADDR, 49846ba1e6e8SDavid Ahern MULTICAST_ADDR, 49856ba1e6e8SDavid Ahern ANYCAST_ADDR, 49866ba1e6e8SDavid Ahern }; 49876ba1e6e8SDavid Ahern 4988203651b6SChristian Brauner struct inet6_fill_args { 4989203651b6SChristian Brauner u32 portid; 4990203651b6SChristian Brauner u32 seq; 4991203651b6SChristian Brauner int event; 4992203651b6SChristian Brauner unsigned int flags; 4993203651b6SChristian Brauner int netnsid; 49946371a71fSDavid Ahern int ifindex; 49956ba1e6e8SDavid Ahern enum addr_type_t type; 4996203651b6SChristian Brauner }; 4997203651b6SChristian Brauner 49981da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 4999203651b6SChristian Brauner struct inet6_fill_args *args) 50001da177e4SLinus Torvalds { 50011da177e4SLinus Torvalds struct nlmsghdr *nlh; 500285486af0SThomas Graf u32 preferred, valid; 50031da177e4SLinus Torvalds 5004203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5005203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 500663159f29SIan Morris if (!nlh) 500726932566SPatrick McHardy return -EMSGSIZE; 50080ab6803bSThomas Graf 5009101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 5010101bb229SThomas Graf ifa->idev->dev->ifindex); 5011101bb229SThomas Graf 5012203651b6SChristian Brauner if (args->netnsid >= 0 && 5013203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 50146ecf4c37SChristian Brauner goto error; 50156ecf4c37SChristian Brauner 50166c0d8833SNiels Dossche spin_lock_bh(&ifa->lock); 5017fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 5018fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 501985486af0SThomas Graf preferred = ifa->prefered_lft; 502085486af0SThomas Graf valid = ifa->valid_lft; 502185486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 50221da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 5023a1faa698SJens Rosenboom if (preferred > tval) 502485486af0SThomas Graf preferred -= tval; 5025a1faa698SJens Rosenboom else 5026a1faa698SJens Rosenboom preferred = 0; 5027f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 5028f56619fcSBen Hutchings if (valid > tval) 502985486af0SThomas Graf valid -= tval; 5030f56619fcSBen Hutchings else 5031f56619fcSBen Hutchings valid = 0; 5032f56619fcSBen Hutchings } 50331da177e4SLinus Torvalds } 50341da177e4SLinus Torvalds } else { 503585486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 503685486af0SThomas Graf valid = INFINITY_LIFE_TIME; 50371da177e4SLinus Torvalds } 50386c0d8833SNiels Dossche spin_unlock_bh(&ifa->lock); 503985486af0SThomas Graf 50407996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 5041930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 5042930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 5043caeaba79SNicolas Dichtel goto error; 5044caeaba79SNicolas Dichtel } else 5045930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 5046caeaba79SNicolas Dichtel goto error; 5047caeaba79SNicolas Dichtel 50488308f3ffSDavid Ahern if (ifa->rt_priority && 50498308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 50508308f3ffSDavid Ahern goto error; 50518308f3ffSDavid Ahern 5052caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 5053caeaba79SNicolas Dichtel goto error; 505485486af0SThomas Graf 5055479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 5056479840ffSJiri Pirko goto error; 5057479840ffSJiri Pirko 505847f0bd50SJacques de Laval if (ifa->ifa_proto && 505947f0bd50SJacques de Laval nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) 506047f0bd50SJacques de Laval goto error; 506147f0bd50SJacques de Laval 5062053c095aSJohannes Berg nlmsg_end(skb, nlh); 5063053c095aSJohannes Berg return 0; 5064caeaba79SNicolas Dichtel 5065caeaba79SNicolas Dichtel error: 5066caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 5067caeaba79SNicolas Dichtel return -EMSGSIZE; 50681da177e4SLinus Torvalds } 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 5071203651b6SChristian Brauner struct inet6_fill_args *args) 50721da177e4SLinus Torvalds { 50731da177e4SLinus Torvalds struct nlmsghdr *nlh; 5074101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 5075101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 50761da177e4SLinus Torvalds 50771da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 5078101bb229SThomas Graf scope = RT_SCOPE_SITE; 5079101bb229SThomas Graf 5080203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5081203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 508263159f29SIan Morris if (!nlh) 508326932566SPatrick McHardy return -EMSGSIZE; 50840ab6803bSThomas Graf 5085203651b6SChristian Brauner if (args->netnsid >= 0 && 5086ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5087ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh); 50886ecf4c37SChristian Brauner return -EMSGSIZE; 5089ceb736e1SZhang Qilong } 50906ecf4c37SChristian Brauner 5091101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5092930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 50930ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 509426932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 509526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 509626932566SPatrick McHardy return -EMSGSIZE; 509726932566SPatrick McHardy } 509885486af0SThomas Graf 5099053c095aSJohannes Berg nlmsg_end(skb, nlh); 5100053c095aSJohannes Berg return 0; 51011da177e4SLinus Torvalds } 51021da177e4SLinus Torvalds 51031da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 5104203651b6SChristian Brauner struct inet6_fill_args *args) 51051da177e4SLinus Torvalds { 51069ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 51079ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 51081da177e4SLinus Torvalds struct nlmsghdr *nlh; 5109101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 51101da177e4SLinus Torvalds 51111da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 5112101bb229SThomas Graf scope = RT_SCOPE_SITE; 5113101bb229SThomas Graf 5114203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5115203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 511663159f29SIan Morris if (!nlh) 511726932566SPatrick McHardy return -EMSGSIZE; 51180ab6803bSThomas Graf 5119203651b6SChristian Brauner if (args->netnsid >= 0 && 5120ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5121ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh); 51226ecf4c37SChristian Brauner return -EMSGSIZE; 5123ceb736e1SZhang Qilong } 51246ecf4c37SChristian Brauner 5125101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5126930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 51270ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 512826932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 512926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 513026932566SPatrick McHardy return -EMSGSIZE; 513126932566SPatrick McHardy } 513285486af0SThomas Graf 5133053c095aSJohannes Berg nlmsg_end(skb, nlh); 5134053c095aSJohannes Berg return 0; 51351da177e4SLinus Torvalds } 51361da177e4SLinus Torvalds 5137234b27c3SEric Dumazet /* called with rcu_read_lock() */ 5138234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 5139fe884c2bSDavid Ahern struct netlink_callback *cb, int s_ip_idx, 51406ba1e6e8SDavid Ahern struct inet6_fill_args *fillargs) 51411da177e4SLinus Torvalds { 51421da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 51431da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 5144fe884c2bSDavid Ahern int ip_idx = 0; 5145234b27c3SEric Dumazet int err = 1; 51461da177e4SLinus Torvalds 51471da177e4SLinus Torvalds read_lock_bh(&idev->lock); 51486ba1e6e8SDavid Ahern switch (fillargs->type) { 5149502a2ffdSstephen hemminger case UNICAST_ADDR: { 5150502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 51516ba1e6e8SDavid Ahern fillargs->event = RTM_NEWADDR; 5152502a2ffdSstephen hemminger 5153ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 5154502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 51554ba4c566SDavid Ahern if (ip_idx < s_ip_idx) 51564ba4c566SDavid Ahern goto next; 51576ba1e6e8SDavid Ahern err = inet6_fill_ifaddr(skb, ifa, fillargs); 5158053c095aSJohannes Berg if (err < 0) 5159bcd32326SEric Dumazet break; 516063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 51614ba4c566SDavid Ahern next: 51624ba4c566SDavid Ahern ip_idx++; 51631da177e4SLinus Torvalds } 51641da177e4SLinus Torvalds break; 5165502a2ffdSstephen hemminger } 51661da177e4SLinus Torvalds case MULTICAST_ADDR: 516788e2ca30STaehee Yoo read_unlock_bh(&idev->lock); 51686ba1e6e8SDavid Ahern fillargs->event = RTM_GETMULTICAST; 5169203651b6SChristian Brauner 51701da177e4SLinus Torvalds /* multicast address */ 5171*4e43e64dSEric Dumazet for (ifmca = rtnl_dereference(idev->mc_list); 517288e2ca30STaehee Yoo ifmca; 5173*4e43e64dSEric Dumazet ifmca = rtnl_dereference(ifmca->next), ip_idx++) { 51741da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 51751da177e4SLinus Torvalds continue; 51766ba1e6e8SDavid Ahern err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 5177053c095aSJohannes Berg if (err < 0) 5178bcd32326SEric Dumazet break; 51791da177e4SLinus Torvalds } 518088e2ca30STaehee Yoo read_lock_bh(&idev->lock); 51811da177e4SLinus Torvalds break; 51821da177e4SLinus Torvalds case ANYCAST_ADDR: 51836ba1e6e8SDavid Ahern fillargs->event = RTM_GETANYCAST; 51841da177e4SLinus Torvalds /* anycast address */ 51851da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 51861da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 51871da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 51881da177e4SLinus Torvalds continue; 51896ba1e6e8SDavid Ahern err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5190053c095aSJohannes Berg if (err < 0) 5191bcd32326SEric Dumazet break; 51921da177e4SLinus Torvalds } 51931da177e4SLinus Torvalds break; 51941da177e4SLinus Torvalds default: 51951da177e4SLinus Torvalds break; 51961da177e4SLinus Torvalds } 51971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 5198fe884c2bSDavid Ahern cb->args[2] = ip_idx; 5199234b27c3SEric Dumazet return err; 5200234b27c3SEric Dumazet } 52015d5619b4SYOSHIFUJI Hideaki 5202ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5203ed6eff11SDavid Ahern struct inet6_fill_args *fillargs, 5204ed6eff11SDavid Ahern struct net **tgt_net, struct sock *sk, 52056371a71fSDavid Ahern struct netlink_callback *cb) 5206ed6eff11SDavid Ahern { 52076371a71fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5208ed6eff11SDavid Ahern struct nlattr *tb[IFA_MAX+1]; 5209ed6eff11SDavid Ahern struct ifaddrmsg *ifm; 5210ed6eff11SDavid Ahern int err, i; 5211ed6eff11SDavid Ahern 5212ed6eff11SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5213ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5214ed6eff11SDavid Ahern return -EINVAL; 5215ed6eff11SDavid Ahern } 5216ed6eff11SDavid Ahern 5217ed6eff11SDavid Ahern ifm = nlmsg_data(nlh); 5218ed6eff11SDavid Ahern if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5219ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5220ed6eff11SDavid Ahern return -EINVAL; 5221ed6eff11SDavid Ahern } 52226371a71fSDavid Ahern 52236371a71fSDavid Ahern fillargs->ifindex = ifm->ifa_index; 52246371a71fSDavid Ahern if (fillargs->ifindex) { 52256371a71fSDavid Ahern cb->answer_flags |= NLM_F_DUMP_FILTERED; 52266371a71fSDavid Ahern fillargs->flags |= NLM_F_DUMP_FILTERED; 5227ed6eff11SDavid Ahern } 5228ed6eff11SDavid Ahern 52298cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5230ed6eff11SDavid Ahern ifa_ipv6_policy, extack); 5231ed6eff11SDavid Ahern if (err < 0) 5232ed6eff11SDavid Ahern return err; 5233ed6eff11SDavid Ahern 5234ed6eff11SDavid Ahern for (i = 0; i <= IFA_MAX; ++i) { 5235ed6eff11SDavid Ahern if (!tb[i]) 5236ed6eff11SDavid Ahern continue; 5237ed6eff11SDavid Ahern 5238ed6eff11SDavid Ahern if (i == IFA_TARGET_NETNSID) { 5239ed6eff11SDavid Ahern struct net *net; 5240ed6eff11SDavid Ahern 5241ed6eff11SDavid Ahern fillargs->netnsid = nla_get_s32(tb[i]); 5242ed6eff11SDavid Ahern net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5243ed6eff11SDavid Ahern if (IS_ERR(net)) { 5244bf4cc40eSBjørn Mork fillargs->netnsid = -1; 5245ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5246ed6eff11SDavid Ahern return PTR_ERR(net); 5247ed6eff11SDavid Ahern } 5248ed6eff11SDavid Ahern *tgt_net = net; 5249ed6eff11SDavid Ahern } else { 5250ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5251ed6eff11SDavid Ahern return -EINVAL; 5252ed6eff11SDavid Ahern } 5253ed6eff11SDavid Ahern } 5254ed6eff11SDavid Ahern 5255ed6eff11SDavid Ahern return 0; 5256ed6eff11SDavid Ahern } 5257ed6eff11SDavid Ahern 5258234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5259234b27c3SEric Dumazet enum addr_type_t type) 5260234b27c3SEric Dumazet { 5261ed6eff11SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 52626ba1e6e8SDavid Ahern struct inet6_fill_args fillargs = { 52636ba1e6e8SDavid Ahern .portid = NETLINK_CB(cb->skb).portid, 52646ba1e6e8SDavid Ahern .seq = cb->nlh->nlmsg_seq, 52656ba1e6e8SDavid Ahern .flags = NLM_F_MULTI, 52666ba1e6e8SDavid Ahern .netnsid = -1, 52676ba1e6e8SDavid Ahern .type = type, 52686ba1e6e8SDavid Ahern }; 526987117bafSRocco Yue struct net *tgt_net = sock_net(skb->sk); 5270fe884c2bSDavid Ahern int idx, s_idx, s_ip_idx; 5271234b27c3SEric Dumazet int h, s_h; 5272234b27c3SEric Dumazet struct net_device *dev; 5273234b27c3SEric Dumazet struct inet6_dev *idev; 5274234b27c3SEric Dumazet struct hlist_head *head; 5275242afaa6SDavid Ahern int err = 0; 5276234b27c3SEric Dumazet 5277234b27c3SEric Dumazet s_h = cb->args[0]; 5278234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 5279fe884c2bSDavid Ahern s_ip_idx = cb->args[2]; 5280234b27c3SEric Dumazet 5281ed6eff11SDavid Ahern if (cb->strict_check) { 5282ed6eff11SDavid Ahern err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 52836371a71fSDavid Ahern skb->sk, cb); 5284ed6eff11SDavid Ahern if (err < 0) 5285242afaa6SDavid Ahern goto put_tgt_net; 52866371a71fSDavid Ahern 5287242afaa6SDavid Ahern err = 0; 52886371a71fSDavid Ahern if (fillargs.ifindex) { 52896371a71fSDavid Ahern dev = __dev_get_by_index(tgt_net, fillargs.ifindex); 5290242afaa6SDavid Ahern if (!dev) { 5291242afaa6SDavid Ahern err = -ENODEV; 5292242afaa6SDavid Ahern goto put_tgt_net; 5293242afaa6SDavid Ahern } 52946371a71fSDavid Ahern idev = __in6_dev_get(dev); 52956371a71fSDavid Ahern if (idev) { 52966371a71fSDavid Ahern err = in6_dump_addrs(idev, skb, cb, s_ip_idx, 52976371a71fSDavid Ahern &fillargs); 52981518039fSJakub Kicinski if (err > 0) 52991518039fSJakub Kicinski err = 0; 53006371a71fSDavid Ahern } 53016371a71fSDavid Ahern goto put_tgt_net; 53026371a71fSDavid Ahern } 53036ecf4c37SChristian Brauner } 53046ecf4c37SChristian Brauner 5305234b27c3SEric Dumazet rcu_read_lock(); 53066ecf4c37SChristian Brauner cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; 5307234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5308234b27c3SEric Dumazet idx = 0; 53096ecf4c37SChristian Brauner head = &tgt_net->dev_index_head[h]; 5310b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5311234b27c3SEric Dumazet if (idx < s_idx) 5312234b27c3SEric Dumazet goto cont; 53134b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5314234b27c3SEric Dumazet s_ip_idx = 0; 5315e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5316e21e8467SStephen Hemminger if (!idev) 5317234b27c3SEric Dumazet goto cont; 5318234b27c3SEric Dumazet 5319fe884c2bSDavid Ahern if (in6_dump_addrs(idev, skb, cb, s_ip_idx, 53206ba1e6e8SDavid Ahern &fillargs) < 0) 5321234b27c3SEric Dumazet goto done; 53227562f876SPavel Emelianov cont: 53237562f876SPavel Emelianov idx++; 53241da177e4SLinus Torvalds } 5325234b27c3SEric Dumazet } 5326234b27c3SEric Dumazet done: 5327234b27c3SEric Dumazet rcu_read_unlock(); 5328234b27c3SEric Dumazet cb->args[0] = h; 5329234b27c3SEric Dumazet cb->args[1] = idx; 53306371a71fSDavid Ahern put_tgt_net: 53316ba1e6e8SDavid Ahern if (fillargs.netnsid >= 0) 53326ecf4c37SChristian Brauner put_net(tgt_net); 5333234b27c3SEric Dumazet 53347c1e8a38SArthur Gautier return skb->len ? : err; 53351da177e4SLinus Torvalds } 53361da177e4SLinus Torvalds 53371da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 53381da177e4SLinus Torvalds { 53391da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5340b854272bSDenis V. Lunev 53411da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53421da177e4SLinus Torvalds } 53431da177e4SLinus Torvalds 53441da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 53451da177e4SLinus Torvalds { 53461da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5347b854272bSDenis V. Lunev 53481da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53491da177e4SLinus Torvalds } 53501da177e4SLinus Torvalds 53511da177e4SLinus Torvalds 53521da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 53531da177e4SLinus Torvalds { 53541da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5355b854272bSDenis V. Lunev 53561da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53571da177e4SLinus Torvalds } 53581da177e4SLinus Torvalds 53594b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, 53604b1373deSJakub Kicinski const struct nlmsghdr *nlh, 53614b1373deSJakub Kicinski struct nlattr **tb, 53624b1373deSJakub Kicinski struct netlink_ext_ack *extack) 53634b1373deSJakub Kicinski { 53644b1373deSJakub Kicinski struct ifaddrmsg *ifm; 53654b1373deSJakub Kicinski int i, err; 53664b1373deSJakub Kicinski 53674b1373deSJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 53684b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); 53694b1373deSJakub Kicinski return -EINVAL; 53704b1373deSJakub Kicinski } 53714b1373deSJakub Kicinski 53722beb6d29SHangbin Liu if (!netlink_strict_get_check(skb)) 53732beb6d29SHangbin Liu return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 53742beb6d29SHangbin Liu ifa_ipv6_policy, extack); 53752beb6d29SHangbin Liu 53764b1373deSJakub Kicinski ifm = nlmsg_data(nlh); 53774b1373deSJakub Kicinski if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 53784b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); 53794b1373deSJakub Kicinski return -EINVAL; 53804b1373deSJakub Kicinski } 53814b1373deSJakub Kicinski 53828cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 53834b1373deSJakub Kicinski ifa_ipv6_policy, extack); 53844b1373deSJakub Kicinski if (err) 53854b1373deSJakub Kicinski return err; 53864b1373deSJakub Kicinski 53874b1373deSJakub Kicinski for (i = 0; i <= IFA_MAX; i++) { 53884b1373deSJakub Kicinski if (!tb[i]) 53894b1373deSJakub Kicinski continue; 53904b1373deSJakub Kicinski 53914b1373deSJakub Kicinski switch (i) { 53924b1373deSJakub Kicinski case IFA_TARGET_NETNSID: 53934b1373deSJakub Kicinski case IFA_ADDRESS: 53944b1373deSJakub Kicinski case IFA_LOCAL: 53954b1373deSJakub Kicinski break; 53964b1373deSJakub Kicinski default: 53974b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); 53984b1373deSJakub Kicinski return -EINVAL; 53994b1373deSJakub Kicinski } 54004b1373deSJakub Kicinski } 54014b1373deSJakub Kicinski 54024b1373deSJakub Kicinski return 0; 54034b1373deSJakub Kicinski } 54044b1373deSJakub Kicinski 5405c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5406c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 54076c223828SNoriaki TAKAMIYA { 540887117bafSRocco Yue struct net *tgt_net = sock_net(in_skb->sk); 5409203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5410203651b6SChristian Brauner .portid = NETLINK_CB(in_skb).portid, 5411203651b6SChristian Brauner .seq = nlh->nlmsg_seq, 5412203651b6SChristian Brauner .event = RTM_NEWADDR, 5413203651b6SChristian Brauner .flags = 0, 5414203651b6SChristian Brauner .netnsid = -1, 5415203651b6SChristian Brauner }; 54161b29fc2cSThomas Graf struct ifaddrmsg *ifm; 54171b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5418caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 54196c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 54206c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 54216c223828SNoriaki TAKAMIYA struct sk_buff *skb; 54226c223828SNoriaki TAKAMIYA int err; 54236c223828SNoriaki TAKAMIYA 54244b1373deSJakub Kicinski err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); 54251b29fc2cSThomas Graf if (err < 0) 5426c24675f8SFlorian Westphal return err; 54276c223828SNoriaki TAKAMIYA 54286ecf4c37SChristian Brauner if (tb[IFA_TARGET_NETNSID]) { 5429203651b6SChristian Brauner fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 54306ecf4c37SChristian Brauner 54316ecf4c37SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5432203651b6SChristian Brauner fillargs.netnsid); 54336ecf4c37SChristian Brauner if (IS_ERR(tgt_net)) 54346ecf4c37SChristian Brauner return PTR_ERR(tgt_net); 54356ecf4c37SChristian Brauner } 54366ecf4c37SChristian Brauner 5437caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5438c24675f8SFlorian Westphal if (!addr) 5439c24675f8SFlorian Westphal return -EINVAL; 54401b29fc2cSThomas Graf 54411b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 54426c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 54436ecf4c37SChristian Brauner dev = dev_get_by_index(tgt_net, ifm->ifa_index); 54446c223828SNoriaki TAKAMIYA 54456ecf4c37SChristian Brauner ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5446e21e8467SStephen Hemminger if (!ifa) { 54471b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 54481b29fc2cSThomas Graf goto errout; 54496c223828SNoriaki TAKAMIYA } 54506c223828SNoriaki TAKAMIYA 5451e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5452e21e8467SStephen Hemminger if (!skb) { 54531b29fc2cSThomas Graf err = -ENOBUFS; 54541b29fc2cSThomas Graf goto errout_ifa; 54551b29fc2cSThomas Graf } 54561b29fc2cSThomas Graf 5457203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 545826932566SPatrick McHardy if (err < 0) { 545926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 546026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 546126932566SPatrick McHardy kfree_skb(skb); 546226932566SPatrick McHardy goto errout_ifa; 546326932566SPatrick McHardy } 54646ecf4c37SChristian Brauner err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 54651b29fc2cSThomas Graf errout_ifa: 54666c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 54671b29fc2cSThomas Graf errout: 5468c24675f8SFlorian Westphal dev_put(dev); 5469203651b6SChristian Brauner if (fillargs.netnsid >= 0) 54706ecf4c37SChristian Brauner put_net(tgt_net); 54716ecf4c37SChristian Brauner 54726c223828SNoriaki TAKAMIYA return err; 54736c223828SNoriaki TAKAMIYA } 54746c223828SNoriaki TAKAMIYA 54751da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 54761da177e4SLinus Torvalds { 54771da177e4SLinus Torvalds struct sk_buff *skb; 5478c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 5479203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5480203651b6SChristian Brauner .portid = 0, 5481203651b6SChristian Brauner .seq = 0, 5482203651b6SChristian Brauner .event = event, 5483203651b6SChristian Brauner .flags = 0, 5484203651b6SChristian Brauner .netnsid = -1, 5485203651b6SChristian Brauner }; 54865d620266SThomas Graf int err = -ENOBUFS; 54871da177e4SLinus Torvalds 54880ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 548963159f29SIan Morris if (!skb) 54905d620266SThomas Graf goto errout; 54915d620266SThomas Graf 5492203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 549326932566SPatrick McHardy if (err < 0) { 549426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 549526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 549626932566SPatrick McHardy kfree_skb(skb); 549726932566SPatrick McHardy goto errout; 549826932566SPatrick McHardy } 54991ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 55001ce85fe4SPablo Neira Ayuso return; 55015d620266SThomas Graf errout: 55025d620266SThomas Graf if (err < 0) 55036fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 55041da177e4SLinus Torvalds } 55051da177e4SLinus Torvalds 5506b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 55071da177e4SLinus Torvalds __s32 *array, int bytes) 55081da177e4SLinus Torvalds { 550904561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 551004561c1fSThomas Graf 55111da177e4SLinus Torvalds memset(array, 0, bytes); 55121da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 55131da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 55141da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 55151da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 55161da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 55171da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 55181da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 55191da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 552093908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 552193908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5522bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5523bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 552493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 552593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 55261da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5527fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5528fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5529fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5530fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 55311da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 55321da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 55331da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 55341da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 55351da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 55361da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 553765f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 55386b2e04bcSPraveen Chaudhary array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric; 55398013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5540c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5541930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5542930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 554393908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 554493908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5545fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5546bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 554709c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 554809c884d4SYOSHIFUJI Hideaki #endif 5549930d6ff2SYOSHIFUJI Hideaki #endif 5550fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 55510bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 555295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 555395c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 55547fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 555595c385b4SNeil Horman #endif 55567bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 5557145c7a79SEric Dumazet array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding); 55587bc570c8SYOSHIFUJI Hideaki #endif 5559778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 55601b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5561c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 55625cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5563b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5564d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5565c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 556635103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 55673d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 55683985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5569abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 55707a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5571f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 55721ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5573bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5574bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5575bf355b8dSDavid Lebrun #endif 5576adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5577d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5578df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 55792210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 55808610c7c6SAlexander Aring array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled; 55819ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled; 55829ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID] = cnf->ioam6_id; 55839ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide; 558418ac597aSJames Prestwood array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier; 55853e0b8f52SArun Ajith S array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na; 55861da177e4SLinus Torvalds } 55871da177e4SLinus Torvalds 5588b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5589b382b191SThomas Graf { 5590b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5591b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5592b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5593b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5594f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5595bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5596bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 559749b99da2SRocco Yue + nla_total_size(4) /* IFLA_INET6_RA_MTU */ 5598bdd72f41SSabrina Dubroca + 0; 5599b382b191SThomas Graf } 5600b382b191SThomas Graf 5601339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5602339bf98fSThomas Graf { 5603339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5604339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5605339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5606339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5607339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 56080344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5609b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5610339bf98fSThomas Graf } 5611c5396a31SYOSHIFUJI Hideaki 5612be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5613aca05671SJia He int bytes) 56147f7d9a6bSHerbert Xu { 56157f7d9a6bSHerbert Xu int i; 5616aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 56177f7d9a6bSHerbert Xu BUG_ON(pad < 0); 56187f7d9a6bSHerbert Xu 56197f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5620aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5621aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5622be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 56237f7d9a6bSHerbert Xu 5624aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 56257f7d9a6bSHerbert Xu } 56267f7d9a6bSHerbert Xu 5627698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5628a3a77372SRaghavendra K T int bytes, size_t syncpoff) 56294ce3c183SEric Dumazet { 5630a3a77372SRaghavendra K T int i, c; 5631a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5632a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5633a3a77372SRaghavendra K T 56344ce3c183SEric Dumazet BUG_ON(pad < 0); 56354ce3c183SEric Dumazet 5636a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5637a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 56384ce3c183SEric Dumazet 5639a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5640a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5641a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5642a3a77372SRaghavendra K T } 5643a3a77372SRaghavendra K T 5644a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5645a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 56464ce3c183SEric Dumazet } 56474ce3c183SEric Dumazet 56487f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 56497f7d9a6bSHerbert Xu int bytes) 56507f7d9a6bSHerbert Xu { 56517f7d9a6bSHerbert Xu switch (attrtype) { 56527f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5653a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5654a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 56557f7d9a6bSHerbert Xu break; 56567f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5657aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 56587f7d9a6bSHerbert Xu break; 56597f7d9a6bSHerbert Xu } 56607f7d9a6bSHerbert Xu } 56617f7d9a6bSHerbert Xu 5662d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5663d5566fd7SSowmini Varadhan u32 ext_filter_mask) 56641da177e4SLinus Torvalds { 5665bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 56661da177e4SLinus Torvalds struct ifla_cacheinfo ci; 56671da177e4SLinus Torvalds 5668c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5669c78679e8SDavid S. Miller goto nla_put_failure; 56701da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 567124912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 567224912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 56731f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5674c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5675c78679e8SDavid S. Miller goto nla_put_failure; 5676bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 567763159f29SIan Morris if (!nla) 567804561c1fSThomas Graf goto nla_put_failure; 5679bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 56801da177e4SLinus Torvalds 5681bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5682bf99f1bdSYOSHIFUJI Hideaki 5683d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5684d5566fd7SSowmini Varadhan return 0; 5685d5566fd7SSowmini Varadhan 5686bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 568763159f29SIan Morris if (!nla) 5688bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5689bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5690bf99f1bdSYOSHIFUJI Hideaki 5691bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 569263159f29SIan Morris if (!nla) 5693bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5694bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 56951da177e4SLinus Torvalds 5696f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 569763159f29SIan Morris if (!nla) 5698f53adae4SDaniel Borkmann goto nla_put_failure; 5699f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5700f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5701f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5702f53adae4SDaniel Borkmann 57030d7982ceSNicolas Dichtel if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 57040d7982ceSNicolas Dichtel goto nla_put_failure; 57050d7982ceSNicolas Dichtel 570649b99da2SRocco Yue if (idev->ra_mtu && 570749b99da2SRocco Yue nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu)) 570849b99da2SRocco Yue goto nla_put_failure; 570949b99da2SRocco Yue 5710b382b191SThomas Graf return 0; 5711b382b191SThomas Graf 5712b382b191SThomas Graf nla_put_failure: 5713b382b191SThomas Graf return -EMSGSIZE; 5714b382b191SThomas Graf } 5715b382b191SThomas Graf 5716b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5717b1974ed0SArad, Ronen u32 ext_filter_mask) 5718b382b191SThomas Graf { 5719b382b191SThomas Graf if (!__in6_dev_get(dev)) 5720b382b191SThomas Graf return 0; 5721b382b191SThomas Graf 5722b382b191SThomas Graf return inet6_ifla6_size(); 5723b382b191SThomas Graf } 5724b382b191SThomas Graf 5725d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5726d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5727b382b191SThomas Graf { 5728b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5729b382b191SThomas Graf 5730b382b191SThomas Graf if (!idev) 5731b382b191SThomas Graf return -ENODATA; 5732b382b191SThomas Graf 5733d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5734b382b191SThomas Graf return -EMSGSIZE; 5735b382b191SThomas Graf 5736b382b191SThomas Graf return 0; 5737b382b191SThomas Graf } 5738b382b191SThomas Graf 57393583a4e8SStephen Hemminger static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token, 57403583a4e8SStephen Hemminger struct netlink_ext_ack *extack) 5741f53adae4SDaniel Borkmann { 5742f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5743f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 574447e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5745dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5746f53adae4SDaniel Borkmann 5747c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5748c15b1ccaSHannes Frederic Sowa 574963159f29SIan Morris if (!token) 5750f53adae4SDaniel Borkmann return -EINVAL; 57513583a4e8SStephen Hemminger 57523583a4e8SStephen Hemminger if (dev->flags & IFF_LOOPBACK) { 57533583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, "Device is loopback"); 5754f53adae4SDaniel Borkmann return -EINVAL; 57553583a4e8SStephen Hemminger } 57563583a4e8SStephen Hemminger 57573583a4e8SStephen Hemminger if (dev->flags & IFF_NOARP) { 57583583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, 57593583a4e8SStephen Hemminger "Device does not do neighbour discovery"); 5760f53adae4SDaniel Borkmann return -EINVAL; 57613583a4e8SStephen Hemminger } 57623583a4e8SStephen Hemminger 57633583a4e8SStephen Hemminger if (!ipv6_accept_ra(idev)) { 57643583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, 57653583a4e8SStephen Hemminger "Router advertisement is disabled on device"); 5766f53adae4SDaniel Borkmann return -EINVAL; 57673583a4e8SStephen Hemminger } 57683583a4e8SStephen Hemminger 57693583a4e8SStephen Hemminger if (idev->cnf.rtr_solicits == 0) { 57703583a4e8SStephen Hemminger NL_SET_ERR_MSG(extack, 57713583a4e8SStephen Hemminger "Router solicitation is disabled on device"); 57723583a4e8SStephen Hemminger return -EINVAL; 57733583a4e8SStephen Hemminger } 5774f53adae4SDaniel Borkmann 5775f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5776f53adae4SDaniel Borkmann 5777f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5778f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5779f53adae4SDaniel Borkmann 5780f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5781f53adae4SDaniel Borkmann 578247e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 578347e27d5eSDaniel Borkmann if (clear_token) 578447e27d5eSDaniel Borkmann goto update_lft; 578547e27d5eSDaniel Borkmann 5786dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5787dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5788dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5789fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5790fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5791fc403832SDaniel Borkmann */ 5792f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5793fc403832SDaniel Borkmann update_rs = true; 5794fc403832SDaniel Borkmann } 5795f53adae4SDaniel Borkmann 579647e27d5eSDaniel Borkmann update_lft: 5797f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5798fc403832SDaniel Borkmann 579977ecaaceSHannes Frederic Sowa if (update_rs) { 5800f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5801bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5802bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 580377ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5804bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 580577ecaaceSHannes Frederic Sowa } 5806f53adae4SDaniel Borkmann 5807f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5808f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5809f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5810617fe29dSDaniel Borkmann if (ifp->tokenized) { 5811f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5812f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5813f53adae4SDaniel Borkmann } 5814f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5815f53adae4SDaniel Borkmann } 5816f53adae4SDaniel Borkmann 5817f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5818b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 58198805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev)); 5820f53adae4SDaniel Borkmann return 0; 5821f53adae4SDaniel Borkmann } 5822f53adae4SDaniel Borkmann 582311b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 582411b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 582511b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 582649b99da2SRocco Yue [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT, 582749b99da2SRocco Yue .reject_message = 582849b99da2SRocco Yue "IFLA_INET6_RA_MTU can not be set" }, 582911b1f828SDaniel Borkmann }; 583011b1f828SDaniel Borkmann 5831d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5832d35a00b8SFelix Jia { 5833d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5834d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5835d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5836d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5837d35a00b8SFelix Jia return -EINVAL; 5838d35a00b8SFelix Jia return 1; 5839d35a00b8SFelix Jia } 5840d35a00b8SFelix Jia 5841d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5842d35a00b8SFelix Jia int mode) 5843d35a00b8SFelix Jia { 5844d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5845d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5846d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5847d35a00b8SFelix Jia return -EINVAL; 5848d35a00b8SFelix Jia return 1; 5849d35a00b8SFelix Jia } 5850d35a00b8SFelix Jia 58517dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev, 58528679c31eSRocco Yue const struct nlattr *nla, 58538679c31eSRocco Yue struct netlink_ext_ack *extack) 5854f53adae4SDaniel Borkmann { 5855f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 58567dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = NULL; 58577dc2bccaSMaxim Mikityanskiy int err; 5858f53adae4SDaniel Borkmann 58597dc2bccaSMaxim Mikityanskiy if (dev) { 58607dc2bccaSMaxim Mikityanskiy idev = __in6_dev_get(dev); 5861f53adae4SDaniel Borkmann if (!idev) 5862f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 58637dc2bccaSMaxim Mikityanskiy } 58647dc2bccaSMaxim Mikityanskiy 58657dc2bccaSMaxim Mikityanskiy err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, 58668679c31eSRocco Yue inet6_af_policy, extack); 58677dc2bccaSMaxim Mikityanskiy if (err) 58687dc2bccaSMaxim Mikityanskiy return err; 58697dc2bccaSMaxim Mikityanskiy 58707dc2bccaSMaxim Mikityanskiy if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) 58717dc2bccaSMaxim Mikityanskiy return -EINVAL; 58727dc2bccaSMaxim Mikityanskiy 58737dc2bccaSMaxim Mikityanskiy if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 58747dc2bccaSMaxim Mikityanskiy u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 58757dc2bccaSMaxim Mikityanskiy 58767dc2bccaSMaxim Mikityanskiy if (check_addr_gen_mode(mode) < 0) 58777dc2bccaSMaxim Mikityanskiy return -EINVAL; 58787dc2bccaSMaxim Mikityanskiy if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) 58797dc2bccaSMaxim Mikityanskiy return -EINVAL; 58807dc2bccaSMaxim Mikityanskiy } 58817dc2bccaSMaxim Mikityanskiy 58827dc2bccaSMaxim Mikityanskiy return 0; 58837dc2bccaSMaxim Mikityanskiy } 58847dc2bccaSMaxim Mikityanskiy 58853583a4e8SStephen Hemminger static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla, 58863583a4e8SStephen Hemminger struct netlink_ext_ack *extack) 58877dc2bccaSMaxim Mikityanskiy { 58887dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = __in6_dev_get(dev); 58897dc2bccaSMaxim Mikityanskiy struct nlattr *tb[IFLA_INET6_MAX + 1]; 58907dc2bccaSMaxim Mikityanskiy int err; 5891f53adae4SDaniel Borkmann 5892db3fa271SEric Dumazet if (!idev) 5893db3fa271SEric Dumazet return -EAFNOSUPPORT; 5894db3fa271SEric Dumazet 58958cb08174SJohannes Berg if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 58965ac6b198SZheng Yongjun return -EINVAL; 5897f53adae4SDaniel Borkmann 5898bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 58993583a4e8SStephen Hemminger err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]), 59003583a4e8SStephen Hemminger extack); 5901bc91b0f0SJiri Pirko if (err) 5902bc91b0f0SJiri Pirko return err; 5903bc91b0f0SJiri Pirko } 5904bc91b0f0SJiri Pirko 5905bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5906bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5907bc91b0f0SJiri Pirko 5908d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5909bc91b0f0SJiri Pirko } 5910f53adae4SDaniel Borkmann 59117dc2bccaSMaxim Mikityanskiy return 0; 5912f53adae4SDaniel Borkmann } 5913f53adae4SDaniel Borkmann 5914b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 591515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5916b382b191SThomas Graf { 5917b382b191SThomas Graf struct net_device *dev = idev->dev; 5918b382b191SThomas Graf struct ifinfomsg *hdr; 5919b382b191SThomas Graf struct nlmsghdr *nlh; 5920b382b191SThomas Graf void *protoinfo; 5921b382b191SThomas Graf 592215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 592363159f29SIan Morris if (!nlh) 5924b382b191SThomas Graf return -EMSGSIZE; 5925b382b191SThomas Graf 5926b382b191SThomas Graf hdr = nlmsg_data(nlh); 5927b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5928b382b191SThomas Graf hdr->__ifi_pad = 0; 5929b382b191SThomas Graf hdr->ifi_type = dev->type; 5930b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5931b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5932b382b191SThomas Graf hdr->ifi_change = 0; 5933b382b191SThomas Graf 5934c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5935c78679e8SDavid S. Miller (dev->addr_len && 5936c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5937c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5938a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 59390344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 59400344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 59410344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5942c78679e8SDavid S. Miller goto nla_put_failure; 5943ae0be8deSMichal Kubecek protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); 594463159f29SIan Morris if (!protoinfo) 5945b382b191SThomas Graf goto nla_put_failure; 5946b382b191SThomas Graf 5947d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5948b382b191SThomas Graf goto nla_put_failure; 5949b382b191SThomas Graf 595004561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5951053c095aSJohannes Berg nlmsg_end(skb, nlh); 5952053c095aSJohannes Berg return 0; 59531da177e4SLinus Torvalds 595404561c1fSThomas Graf nla_put_failure: 595526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 595626932566SPatrick McHardy return -EMSGSIZE; 59571da177e4SLinus Torvalds } 59581da177e4SLinus Torvalds 5959786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 5960786e0007SDavid Ahern struct netlink_ext_ack *extack) 5961786e0007SDavid Ahern { 5962786e0007SDavid Ahern struct ifinfomsg *ifm; 5963786e0007SDavid Ahern 5964786e0007SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5965786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 5966786e0007SDavid Ahern return -EINVAL; 5967786e0007SDavid Ahern } 5968786e0007SDavid Ahern 5969786e0007SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 5970786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 5971786e0007SDavid Ahern return -EINVAL; 5972786e0007SDavid Ahern } 5973786e0007SDavid Ahern 5974786e0007SDavid Ahern ifm = nlmsg_data(nlh); 5975786e0007SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 5976786e0007SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 5977786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 5978786e0007SDavid Ahern return -EINVAL; 5979786e0007SDavid Ahern } 5980786e0007SDavid Ahern 5981786e0007SDavid Ahern return 0; 5982786e0007SDavid Ahern } 5983786e0007SDavid Ahern 59841da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 59851da177e4SLinus Torvalds { 59863b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 598784d2697dSEric Dumazet int h, s_h; 5988434a8a58SDavid S. Miller int idx = 0, s_idx; 59891da177e4SLinus Torvalds struct net_device *dev; 59901da177e4SLinus Torvalds struct inet6_dev *idev; 599184d2697dSEric Dumazet struct hlist_head *head; 59921da177e4SLinus Torvalds 5993786e0007SDavid Ahern /* only requests using strict checking can pass data to 5994786e0007SDavid Ahern * influence the dump 5995786e0007SDavid Ahern */ 5996786e0007SDavid Ahern if (cb->strict_check) { 5997786e0007SDavid Ahern int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 5998786e0007SDavid Ahern 5999786e0007SDavid Ahern if (err < 0) 6000786e0007SDavid Ahern return err; 6001786e0007SDavid Ahern } 6002786e0007SDavid Ahern 600384d2697dSEric Dumazet s_h = cb->args[0]; 600484d2697dSEric Dumazet s_idx = cb->args[1]; 600584d2697dSEric Dumazet 600684d2697dSEric Dumazet rcu_read_lock(); 600784d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 60087562f876SPavel Emelianov idx = 0; 600984d2697dSEric Dumazet head = &net->dev_index_head[h]; 6010b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 60111da177e4SLinus Torvalds if (idx < s_idx) 60127562f876SPavel Emelianov goto cont; 601384d2697dSEric Dumazet idev = __in6_dev_get(dev); 601484d2697dSEric Dumazet if (!idev) 60157562f876SPavel Emelianov goto cont; 601684d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 601715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 601884d2697dSEric Dumazet cb->nlh->nlmsg_seq, 6019053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 602084d2697dSEric Dumazet goto out; 60217562f876SPavel Emelianov cont: 60227562f876SPavel Emelianov idx++; 60231da177e4SLinus Torvalds } 602484d2697dSEric Dumazet } 602584d2697dSEric Dumazet out: 602684d2697dSEric Dumazet rcu_read_unlock(); 602784d2697dSEric Dumazet cb->args[1] = idx; 602884d2697dSEric Dumazet cb->args[0] = h; 60291da177e4SLinus Torvalds 60301da177e4SLinus Torvalds return skb->len; 60311da177e4SLinus Torvalds } 60321da177e4SLinus Torvalds 60331da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 60341da177e4SLinus Torvalds { 60351da177e4SLinus Torvalds struct sk_buff *skb; 6036c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 60378d7a76c9SThomas Graf int err = -ENOBUFS; 60381da177e4SLinus Torvalds 6039339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 604063159f29SIan Morris if (!skb) 60418d7a76c9SThomas Graf goto errout; 60428d7a76c9SThomas Graf 60438d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 604426932566SPatrick McHardy if (err < 0) { 604526932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 604626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 604726932566SPatrick McHardy kfree_skb(skb); 604826932566SPatrick McHardy goto errout; 604926932566SPatrick McHardy } 60505f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 60511ce85fe4SPablo Neira Ayuso return; 60528d7a76c9SThomas Graf errout: 60538d7a76c9SThomas Graf if (err < 0) 60545f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 60551da177e4SLinus Torvalds } 60561da177e4SLinus Torvalds 6057339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 6058339bf98fSThomas Graf { 6059339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 6060339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 6061339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 6062339bf98fSThomas Graf } 6063c5396a31SYOSHIFUJI Hideaki 60641da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 606515e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 6066b6544c0bSJamal Hadi Salim int event, unsigned int flags) 60671da177e4SLinus Torvalds { 60681da177e4SLinus Torvalds struct prefixmsg *pmsg; 60691da177e4SLinus Torvalds struct nlmsghdr *nlh; 60701da177e4SLinus Torvalds struct prefix_cacheinfo ci; 60711da177e4SLinus Torvalds 607215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 607363159f29SIan Morris if (!nlh) 607426932566SPatrick McHardy return -EMSGSIZE; 60756051e2f4SThomas Graf 60766051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 60771da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 60788a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 60798a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 60801da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 60811da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 60821da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 60838a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 60841da177e4SLinus Torvalds pmsg->prefix_flags = 0; 60851da177e4SLinus Torvalds if (pinfo->onlink) 60861da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 60871da177e4SLinus Torvalds if (pinfo->autoconf) 60881da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 60891da177e4SLinus Torvalds 6090c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 6091c78679e8SDavid S. Miller goto nla_put_failure; 60921da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 60931da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 6094c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 6095c78679e8SDavid S. Miller goto nla_put_failure; 6096053c095aSJohannes Berg nlmsg_end(skb, nlh); 6097053c095aSJohannes Berg return 0; 60981da177e4SLinus Torvalds 60996051e2f4SThomas Graf nla_put_failure: 610026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 610126932566SPatrick McHardy return -EMSGSIZE; 61021da177e4SLinus Torvalds } 61031da177e4SLinus Torvalds 61041da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 61051da177e4SLinus Torvalds struct prefix_info *pinfo) 61061da177e4SLinus Torvalds { 61071da177e4SLinus Torvalds struct sk_buff *skb; 6108c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 61098c384bfaSThomas Graf int err = -ENOBUFS; 61101da177e4SLinus Torvalds 6111339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 611263159f29SIan Morris if (!skb) 61138c384bfaSThomas Graf goto errout; 61148c384bfaSThomas Graf 61158c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 611626932566SPatrick McHardy if (err < 0) { 611726932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 611826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 611926932566SPatrick McHardy kfree_skb(skb); 612026932566SPatrick McHardy goto errout; 612126932566SPatrick McHardy } 61221ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 61231ce85fe4SPablo Neira Ayuso return; 61248c384bfaSThomas Graf errout: 61258c384bfaSThomas Graf if (err < 0) 61266fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 61271da177e4SLinus Torvalds } 61281da177e4SLinus Torvalds 61291da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 61301da177e4SLinus Torvalds { 613163998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 613263998ac2SNicolas Dichtel 6133c15b1ccaSHannes Frederic Sowa if (event) 6134c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 6135c15b1ccaSHannes Frederic Sowa 61361da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 61371da177e4SLinus Torvalds 61381da177e4SLinus Torvalds switch (event) { 61391da177e4SLinus Torvalds case RTM_NEWADDR: 614095c385b4SNeil Horman /* 61412d819d25SDavid Ahern * If the address was optimistic we inserted the route at the 61422d819d25SDavid Ahern * start of our DAD process, so we don't need to do it again. 61432d819d25SDavid Ahern * If the device was taken down in the middle of the DAD 61442d819d25SDavid Ahern * cycle there is a race where we could get here without a 61452d819d25SDavid Ahern * host route, so nothing to insert. That will be fixed when 61462d819d25SDavid Ahern * the device is brought up. 614795c385b4SNeil Horman */ 61482d819d25SDavid Ahern if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) { 6149afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 61502d819d25SDavid Ahern } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) { 61512d819d25SDavid Ahern pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n", 61522d819d25SDavid Ahern &ifp->addr, ifp->idev->dev->name); 61532d819d25SDavid Ahern } 61542d819d25SDavid Ahern 61551da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 61561da177e4SLinus Torvalds addrconf_join_anycast(ifp); 61577996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 615807758eb9SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, 128, 615907758eb9SHangbin Liu ifp->rt_priority, ifp->idev->dev, 616007758eb9SHangbin Liu 0, 0, GFP_ATOMIC); 61611da177e4SLinus Torvalds break; 61621da177e4SLinus Torvalds case RTM_DELADDR: 61631da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 61641da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 61651da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 61667996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 61678d1c802bSDavid Ahern struct fib6_info *rt; 6168caeaba79SNicolas Dichtel 6169e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 61702b2450caSDavid Ahern ifp->idev->dev, 0, 0, 61712b2450caSDavid Ahern false); 61728e3d5be7SMartin KaFai Lau if (rt) 617311dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 6174caeaba79SNicolas Dichtel } 617538bd10c4SDavid Ahern if (ifp->rt) { 617611dd74b3SRoopa Prabhu ip6_del_rt(net, ifp->rt, false); 617793531c67SDavid Ahern ifp->rt = NULL; 617838bd10c4SDavid Ahern } 6179705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 61801da177e4SLinus Torvalds break; 61811da177e4SLinus Torvalds } 618263998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 61831da177e4SLinus Torvalds } 61841da177e4SLinus Torvalds 61851da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 61861da177e4SLinus Torvalds { 61871da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 61881da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 61891da177e4SLinus Torvalds } 61901da177e4SLinus Torvalds 61911da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 61921da177e4SLinus Torvalds 619332927393SChristoph Hellwig static int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 619432927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 61951da177e4SLinus Torvalds { 61961da177e4SLinus Torvalds int *valp = ctl->data; 61971da177e4SLinus Torvalds int val = *valp; 619888af182eSEric W. Biederman loff_t pos = *ppos; 6199fe2c6338SJoe Perches struct ctl_table lctl; 62001da177e4SLinus Torvalds int ret; 62011da177e4SLinus Torvalds 6202013d97e9SFrancesco Ruggeri /* 6203013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 6204013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6205013d97e9SFrancesco Ruggeri */ 6206013d97e9SFrancesco Ruggeri lctl = *ctl; 6207013d97e9SFrancesco Ruggeri lctl.data = &val; 6208013d97e9SFrancesco Ruggeri 6209013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 62101da177e4SLinus Torvalds 6211c8fecf22SPavel Emelyanov if (write) 6212b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 621388af182eSEric W. Biederman if (ret) 621488af182eSEric W. Biederman *ppos = pos; 62151da177e4SLinus Torvalds return ret; 62161da177e4SLinus Torvalds } 62171da177e4SLinus Torvalds 621832927393SChristoph Hellwig static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 621932927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 622077751427SMarcelo Leitner { 622177751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 622277751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 622377751427SMarcelo Leitner struct ctl_table lctl; 622477751427SMarcelo Leitner 622577751427SMarcelo Leitner lctl = *ctl; 622677751427SMarcelo Leitner lctl.extra1 = &min_mtu; 622777751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 622877751427SMarcelo Leitner 622977751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 623077751427SMarcelo Leitner } 623177751427SMarcelo Leitner 623256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 623356d417b1SBrian Haley { 623475538c2bSCong Wang struct netdev_notifier_info info; 623575538c2bSCong Wang 623656d417b1SBrian Haley if (!idev || !idev->dev) 623756d417b1SBrian Haley return; 623856d417b1SBrian Haley 623975538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 624056d417b1SBrian Haley if (idev->cnf.disable_ipv6) 624175538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 624256d417b1SBrian Haley else 624375538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 624456d417b1SBrian Haley } 624556d417b1SBrian Haley 624656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 624756d417b1SBrian Haley { 624856d417b1SBrian Haley struct net_device *dev; 624956d417b1SBrian Haley struct inet6_dev *idev; 625056d417b1SBrian Haley 625103e4deffSKefeng Wang for_each_netdev(net, dev) { 625256d417b1SBrian Haley idev = __in6_dev_get(dev); 625356d417b1SBrian Haley if (idev) { 625456d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 625556d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 625656d417b1SBrian Haley if (changed) 625756d417b1SBrian Haley dev_disable_change(idev); 625856d417b1SBrian Haley } 625956d417b1SBrian Haley } 626056d417b1SBrian Haley } 626156d417b1SBrian Haley 6262013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 626356d417b1SBrian Haley { 626456d417b1SBrian Haley struct net *net; 6265013d97e9SFrancesco Ruggeri int old; 6266013d97e9SFrancesco Ruggeri 6267013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 6268013d97e9SFrancesco Ruggeri return restart_syscall(); 626956d417b1SBrian Haley 627056d417b1SBrian Haley net = (struct net *)table->extra2; 6271013d97e9SFrancesco Ruggeri old = *p; 6272013d97e9SFrancesco Ruggeri *p = newf; 627356d417b1SBrian Haley 6274013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 6275013d97e9SFrancesco Ruggeri rtnl_unlock(); 627656d417b1SBrian Haley return 0; 627788af182eSEric W. Biederman } 627856d417b1SBrian Haley 627956d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 628056d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 628156d417b1SBrian Haley addrconf_disable_change(net, newf); 6282013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 628356d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 628456d417b1SBrian Haley 628556d417b1SBrian Haley rtnl_unlock(); 628656d417b1SBrian Haley return 0; 628756d417b1SBrian Haley } 628856d417b1SBrian Haley 628932927393SChristoph Hellwig static int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 629032927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 629156d417b1SBrian Haley { 629256d417b1SBrian Haley int *valp = ctl->data; 629356d417b1SBrian Haley int val = *valp; 629488af182eSEric W. Biederman loff_t pos = *ppos; 6295fe2c6338SJoe Perches struct ctl_table lctl; 629656d417b1SBrian Haley int ret; 629756d417b1SBrian Haley 6298013d97e9SFrancesco Ruggeri /* 6299013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 6300013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6301013d97e9SFrancesco Ruggeri */ 6302013d97e9SFrancesco Ruggeri lctl = *ctl; 6303013d97e9SFrancesco Ruggeri lctl.data = &val; 6304013d97e9SFrancesco Ruggeri 6305013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 630656d417b1SBrian Haley 630756d417b1SBrian Haley if (write) 630856d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 630988af182eSEric W. Biederman if (ret) 631088af182eSEric W. Biederman *ppos = pos; 631156d417b1SBrian Haley return ret; 631256d417b1SBrian Haley } 631356d417b1SBrian Haley 631432927393SChristoph Hellwig static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 631532927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 6316c92d5491Sstephen hemminger { 6317c92d5491Sstephen hemminger int *valp = ctl->data; 6318c92d5491Sstephen hemminger int ret; 6319c92d5491Sstephen hemminger int old, new; 6320c92d5491Sstephen hemminger 6321c92d5491Sstephen hemminger old = *valp; 6322c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6323c92d5491Sstephen hemminger new = *valp; 6324c92d5491Sstephen hemminger 6325c92d5491Sstephen hemminger if (write && old != new) { 6326c92d5491Sstephen hemminger struct net *net = ctl->extra2; 6327c92d5491Sstephen hemminger 6328c92d5491Sstephen hemminger if (!rtnl_trylock()) 6329c92d5491Sstephen hemminger return restart_syscall(); 6330c92d5491Sstephen hemminger 6331c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 633285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 633385b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6334c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 6335c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 6336c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 633785b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 633885b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6339c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 6340c92d5491Sstephen hemminger net->ipv6.devconf_all); 6341c92d5491Sstephen hemminger else { 6342c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 6343c92d5491Sstephen hemminger 634485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 634585b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6346c92d5491Sstephen hemminger idev->dev->ifindex, 6347c92d5491Sstephen hemminger &idev->cnf); 6348c92d5491Sstephen hemminger } 6349c92d5491Sstephen hemminger rtnl_unlock(); 6350c92d5491Sstephen hemminger } 6351c92d5491Sstephen hemminger 6352c92d5491Sstephen hemminger return ret; 6353c92d5491Sstephen hemminger } 6354c92d5491Sstephen hemminger 6355d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 635632927393SChristoph Hellwig void *buffer, size_t *lenp, 6357d35a00b8SFelix Jia loff_t *ppos) 6358d35a00b8SFelix Jia { 6359d35a00b8SFelix Jia int ret = 0; 6360c6dbf7aaSSabrina Dubroca u32 new_val; 6361d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6362d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 6363c6dbf7aaSSabrina Dubroca struct ctl_table tmp = { 6364c6dbf7aaSSabrina Dubroca .data = &new_val, 6365c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val), 6366c6dbf7aaSSabrina Dubroca .mode = ctl->mode, 6367c6dbf7aaSSabrina Dubroca }; 6368d35a00b8SFelix Jia 63698c171d6cSFelix Jia if (!rtnl_trylock()) 63708c171d6cSFelix Jia return restart_syscall(); 63718c171d6cSFelix Jia 6372c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data); 6373c6dbf7aaSSabrina Dubroca 6374c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6375c6dbf7aaSSabrina Dubroca if (ret != 0) 6376c6dbf7aaSSabrina Dubroca goto out; 6377d35a00b8SFelix Jia 6378d35a00b8SFelix Jia if (write) { 63798c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 63808c171d6cSFelix Jia ret = -EINVAL; 63818c171d6cSFelix Jia goto out; 63828c171d6cSFelix Jia } 6383d35a00b8SFelix Jia 6384c6dbf7aaSSabrina Dubroca if (idev) { 63858c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 63868c171d6cSFelix Jia ret = -EINVAL; 63878c171d6cSFelix Jia goto out; 63888c171d6cSFelix Jia } 6389d35a00b8SFelix Jia 6390d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 6391d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 6392d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 63938c171d6cSFelix Jia } 6394f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6395f24c5987SSabrina Dubroca struct net_device *dev; 6396f24c5987SSabrina Dubroca 6397f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val; 6398f24c5987SSabrina Dubroca for_each_netdev(net, dev) { 6399f24c5987SSabrina Dubroca idev = __in6_dev_get(dev); 6400f24c5987SSabrina Dubroca if (idev && 6401f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) { 6402f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val; 6403f24c5987SSabrina Dubroca addrconf_dev_config(idev->dev); 6404f24c5987SSabrina Dubroca } 6405f24c5987SSabrina Dubroca } 64068c171d6cSFelix Jia } 6407c6dbf7aaSSabrina Dubroca 6408c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val; 64098c171d6cSFelix Jia } 64108c171d6cSFelix Jia 64118c171d6cSFelix Jia out: 6412d35a00b8SFelix Jia rtnl_unlock(); 6413d35a00b8SFelix Jia 6414d35a00b8SFelix Jia return ret; 6415d35a00b8SFelix Jia } 6416d35a00b8SFelix Jia 64173d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 641832927393SChristoph Hellwig void *buffer, size_t *lenp, 64193d1bec99SHannes Frederic Sowa loff_t *ppos) 64203d1bec99SHannes Frederic Sowa { 64213d1bec99SHannes Frederic Sowa int err; 64223d1bec99SHannes Frederic Sowa struct in6_addr addr; 64233d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 64243d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 6425622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 64263d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 64273d1bec99SHannes Frederic Sowa 6428622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6429622c81d5SHannes Frederic Sowa return -EIO; 6430622c81d5SHannes Frederic Sowa 64313d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 64323d1bec99SHannes Frederic Sowa lctl.data = str; 64333d1bec99SHannes Frederic Sowa 64343d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 64353d1bec99SHannes Frederic Sowa return restart_syscall(); 64363d1bec99SHannes Frederic Sowa 64373d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 64383d1bec99SHannes Frederic Sowa err = -EIO; 64393d1bec99SHannes Frederic Sowa goto out; 64403d1bec99SHannes Frederic Sowa } 64413d1bec99SHannes Frederic Sowa 64425449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 64433d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 64443d1bec99SHannes Frederic Sowa err = -EIO; 64453d1bec99SHannes Frederic Sowa goto out; 64463d1bec99SHannes Frederic Sowa } 64473d1bec99SHannes Frederic Sowa 64483d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 64493d1bec99SHannes Frederic Sowa if (err || !write) 64503d1bec99SHannes Frederic Sowa goto out; 64513d1bec99SHannes Frederic Sowa 64523d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 64533d1bec99SHannes Frederic Sowa err = -EIO; 64543d1bec99SHannes Frederic Sowa goto out; 64553d1bec99SHannes Frederic Sowa } 64563d1bec99SHannes Frederic Sowa 64573d1bec99SHannes Frederic Sowa secret->initialized = true; 64583d1bec99SHannes Frederic Sowa secret->secret = addr; 64593d1bec99SHannes Frederic Sowa 6460622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6461622c81d5SHannes Frederic Sowa struct net_device *dev; 6462622c81d5SHannes Frederic Sowa 6463622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 6464622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 6465622c81d5SHannes Frederic Sowa 6466622c81d5SHannes Frederic Sowa if (idev) { 6467d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 6468622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6469622c81d5SHannes Frederic Sowa } 6470622c81d5SHannes Frederic Sowa } 6471622c81d5SHannes Frederic Sowa } else { 6472622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6473622c81d5SHannes Frederic Sowa 6474d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6475622c81d5SHannes Frederic Sowa } 6476622c81d5SHannes Frederic Sowa 64773d1bec99SHannes Frederic Sowa out: 64783d1bec99SHannes Frederic Sowa rtnl_unlock(); 64793d1bec99SHannes Frederic Sowa 64803d1bec99SHannes Frederic Sowa return err; 64813d1bec99SHannes Frederic Sowa } 6482c92d5491Sstephen hemminger 648335103d11SAndy Gospodarek static 648435103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 648532927393SChristoph Hellwig int write, void *buffer, 648635103d11SAndy Gospodarek size_t *lenp, 648735103d11SAndy Gospodarek loff_t *ppos) 648835103d11SAndy Gospodarek { 648935103d11SAndy Gospodarek int *valp = ctl->data; 649035103d11SAndy Gospodarek int val = *valp; 649135103d11SAndy Gospodarek loff_t pos = *ppos; 649235103d11SAndy Gospodarek struct ctl_table lctl; 649335103d11SAndy Gospodarek int ret; 649435103d11SAndy Gospodarek 649535103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 649635103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 649735103d11SAndy Gospodarek */ 649835103d11SAndy Gospodarek lctl = *ctl; 649935103d11SAndy Gospodarek lctl.data = &val; 650035103d11SAndy Gospodarek 650135103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 650235103d11SAndy Gospodarek 650335103d11SAndy Gospodarek if (write) 650435103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 650535103d11SAndy Gospodarek if (ret) 650635103d11SAndy Gospodarek *ppos = pos; 650735103d11SAndy Gospodarek return ret; 650835103d11SAndy Gospodarek } 650935103d11SAndy Gospodarek 6510df789fe7SDavid Forster static 6511df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6512df789fe7SDavid Forster { 6513df789fe7SDavid Forster if (rt) { 6514df789fe7SDavid Forster if (action) 6515df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6516df789fe7SDavid Forster else 6517df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6518df789fe7SDavid Forster } 6519df789fe7SDavid Forster } 6520df789fe7SDavid Forster 6521df789fe7SDavid Forster static 6522df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6523df789fe7SDavid Forster { 6524df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6525df789fe7SDavid Forster 6526df789fe7SDavid Forster read_lock_bh(&idev->lock); 6527df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6528df789fe7SDavid Forster spin_lock(&ifa->lock); 6529df789fe7SDavid Forster if (ifa->rt) { 6530f88d8ea6SDavid Ahern /* host routes only use builtin fib6_nh */ 65311cf844c7SDavid Ahern struct fib6_nh *nh = ifa->rt->fib6_nh; 6532df789fe7SDavid Forster int cpu; 6533df789fe7SDavid Forster 653466f5d6ceSWei Wang rcu_read_lock(); 65353b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6536f40b6ae2SDavid Ahern if (nh->rt6i_pcpu) { 6537df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6538df789fe7SDavid Forster struct rt6_info **rtp; 6539df789fe7SDavid Forster 6540f40b6ae2SDavid Ahern rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); 6541df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6542df789fe7SDavid Forster } 6543df789fe7SDavid Forster } 654466f5d6ceSWei Wang rcu_read_unlock(); 6545df789fe7SDavid Forster } 6546df789fe7SDavid Forster spin_unlock(&ifa->lock); 6547df789fe7SDavid Forster } 6548df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6549df789fe7SDavid Forster } 6550df789fe7SDavid Forster 6551df789fe7SDavid Forster static 6552df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6553df789fe7SDavid Forster { 6554df789fe7SDavid Forster struct inet6_dev *idev; 6555df789fe7SDavid Forster struct net *net; 6556df789fe7SDavid Forster 6557df789fe7SDavid Forster if (!rtnl_trylock()) 6558df789fe7SDavid Forster return restart_syscall(); 6559df789fe7SDavid Forster 6560df789fe7SDavid Forster *valp = val; 6561df789fe7SDavid Forster 6562df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6563df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6564df789fe7SDavid Forster rtnl_unlock(); 6565df789fe7SDavid Forster return 0; 6566df789fe7SDavid Forster } 6567df789fe7SDavid Forster 6568df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6569df789fe7SDavid Forster struct net_device *dev; 6570df789fe7SDavid Forster 6571df789fe7SDavid Forster for_each_netdev(net, dev) { 6572df789fe7SDavid Forster idev = __in6_dev_get(dev); 6573df789fe7SDavid Forster if (idev) 6574df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6575df789fe7SDavid Forster } 6576df789fe7SDavid Forster } else { 6577df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6578df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6579df789fe7SDavid Forster } 6580df789fe7SDavid Forster 6581df789fe7SDavid Forster rtnl_unlock(); 6582df789fe7SDavid Forster return 0; 6583df789fe7SDavid Forster } 6584df789fe7SDavid Forster 658532927393SChristoph Hellwig static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 658632927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 6587df789fe7SDavid Forster { 6588df789fe7SDavid Forster int *valp = ctl->data; 6589df789fe7SDavid Forster int val = *valp; 6590df789fe7SDavid Forster loff_t pos = *ppos; 6591df789fe7SDavid Forster struct ctl_table lctl; 6592df789fe7SDavid Forster int ret; 6593df789fe7SDavid Forster 6594df789fe7SDavid Forster lctl = *ctl; 6595df789fe7SDavid Forster lctl.data = &val; 6596df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6597df789fe7SDavid Forster 6598df789fe7SDavid Forster if (write && (*valp != val)) 6599df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6600df789fe7SDavid Forster 6601df789fe7SDavid Forster if (ret) 6602df789fe7SDavid Forster *ppos = pos; 6603df789fe7SDavid Forster 6604df789fe7SDavid Forster return ret; 6605df789fe7SDavid Forster } 6606df789fe7SDavid Forster 6607cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 6608cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6609176f716cSMatthieu Baerts static u32 ioam6_if_id_max = U16_MAX; 6610cb9e684eSMaciej Żenczykowski 6611607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 66121da177e4SLinus Torvalds { 66131da177e4SLinus Torvalds .procname = "forwarding", 66141da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 66151da177e4SLinus Torvalds .maxlen = sizeof(int), 66161da177e4SLinus Torvalds .mode = 0644, 66176d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 66181da177e4SLinus Torvalds }, 66191da177e4SLinus Torvalds { 66201da177e4SLinus Torvalds .procname = "hop_limit", 66211da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 66221da177e4SLinus Torvalds .maxlen = sizeof(int), 66231da177e4SLinus Torvalds .mode = 0644, 6624cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6625eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ONE, 6626cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 66271da177e4SLinus Torvalds }, 66281da177e4SLinus Torvalds { 66291da177e4SLinus Torvalds .procname = "mtu", 66301da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 66311da177e4SLinus Torvalds .maxlen = sizeof(int), 66321da177e4SLinus Torvalds .mode = 0644, 663377751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 66341da177e4SLinus Torvalds }, 66351da177e4SLinus Torvalds { 66361da177e4SLinus Torvalds .procname = "accept_ra", 66371da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 66381da177e4SLinus Torvalds .maxlen = sizeof(int), 66391da177e4SLinus Torvalds .mode = 0644, 66406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66411da177e4SLinus Torvalds }, 66421da177e4SLinus Torvalds { 66431da177e4SLinus Torvalds .procname = "accept_redirects", 66441da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 66451da177e4SLinus Torvalds .maxlen = sizeof(int), 66461da177e4SLinus Torvalds .mode = 0644, 66476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66481da177e4SLinus Torvalds }, 66491da177e4SLinus Torvalds { 66501da177e4SLinus Torvalds .procname = "autoconf", 66511da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 66521da177e4SLinus Torvalds .maxlen = sizeof(int), 66531da177e4SLinus Torvalds .mode = 0644, 66546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66551da177e4SLinus Torvalds }, 66561da177e4SLinus Torvalds { 66571da177e4SLinus Torvalds .procname = "dad_transmits", 66581da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 66591da177e4SLinus Torvalds .maxlen = sizeof(int), 66601da177e4SLinus Torvalds .mode = 0644, 66616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66621da177e4SLinus Torvalds }, 66631da177e4SLinus Torvalds { 66641da177e4SLinus Torvalds .procname = "router_solicitations", 66651da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 66661da177e4SLinus Torvalds .maxlen = sizeof(int), 66671da177e4SLinus Torvalds .mode = 0644, 6668cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6669cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 66701da177e4SLinus Torvalds }, 66711da177e4SLinus Torvalds { 66721da177e4SLinus Torvalds .procname = "router_solicitation_interval", 66731da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 66741da177e4SLinus Torvalds .maxlen = sizeof(int), 66751da177e4SLinus Torvalds .mode = 0644, 66766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 66771da177e4SLinus Torvalds }, 66781da177e4SLinus Torvalds { 6679bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6680bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6681bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6682bd11f074SMaciej Żenczykowski .mode = 0644, 6683bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6684bd11f074SMaciej Żenczykowski }, 6685bd11f074SMaciej Żenczykowski { 66861da177e4SLinus Torvalds .procname = "router_solicitation_delay", 66871da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 66881da177e4SLinus Torvalds .maxlen = sizeof(int), 66891da177e4SLinus Torvalds .mode = 0644, 66906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 66911da177e4SLinus Torvalds }, 66921da177e4SLinus Torvalds { 66931da177e4SLinus Torvalds .procname = "force_mld_version", 66941da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 66951da177e4SLinus Torvalds .maxlen = sizeof(int), 66961da177e4SLinus Torvalds .mode = 0644, 66976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66981da177e4SLinus Torvalds }, 6699fc4eba58SHannes Frederic Sowa { 6700fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6701fc4eba58SHannes Frederic Sowa .data = 6702fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6703fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6704fc4eba58SHannes Frederic Sowa .mode = 0644, 6705fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6706fc4eba58SHannes Frederic Sowa }, 6707fc4eba58SHannes Frederic Sowa { 6708fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6709fc4eba58SHannes Frederic Sowa .data = 6710fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6711fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6712fc4eba58SHannes Frederic Sowa .mode = 0644, 6713fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6714fc4eba58SHannes Frederic Sowa }, 67151da177e4SLinus Torvalds { 67161da177e4SLinus Torvalds .procname = "use_tempaddr", 67171da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 67181da177e4SLinus Torvalds .maxlen = sizeof(int), 67191da177e4SLinus Torvalds .mode = 0644, 67206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67211da177e4SLinus Torvalds }, 67221da177e4SLinus Torvalds { 67231da177e4SLinus Torvalds .procname = "temp_valid_lft", 67241da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 67251da177e4SLinus Torvalds .maxlen = sizeof(int), 67261da177e4SLinus Torvalds .mode = 0644, 67276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67281da177e4SLinus Torvalds }, 67291da177e4SLinus Torvalds { 67301da177e4SLinus Torvalds .procname = "temp_prefered_lft", 67311da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 67321da177e4SLinus Torvalds .maxlen = sizeof(int), 67331da177e4SLinus Torvalds .mode = 0644, 67346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67351da177e4SLinus Torvalds }, 67361da177e4SLinus Torvalds { 67371da177e4SLinus Torvalds .procname = "regen_max_retry", 67381da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 67391da177e4SLinus Torvalds .maxlen = sizeof(int), 67401da177e4SLinus Torvalds .mode = 0644, 67416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67421da177e4SLinus Torvalds }, 67431da177e4SLinus Torvalds { 67441da177e4SLinus Torvalds .procname = "max_desync_factor", 67451da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 67461da177e4SLinus Torvalds .maxlen = sizeof(int), 67471da177e4SLinus Torvalds .mode = 0644, 67486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67491da177e4SLinus Torvalds }, 67501da177e4SLinus Torvalds { 67511da177e4SLinus Torvalds .procname = "max_addresses", 67521da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 67531da177e4SLinus Torvalds .maxlen = sizeof(int), 67541da177e4SLinus Torvalds .mode = 0644, 67556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67561da177e4SLinus Torvalds }, 67571da177e4SLinus Torvalds { 675865f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 675965f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 676065f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 676165f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 67626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 676365f5c7c1SYOSHIFUJI Hideaki }, 676465f5c7c1SYOSHIFUJI Hideaki { 67656b2e04bcSPraveen Chaudhary .procname = "ra_defrtr_metric", 67666b2e04bcSPraveen Chaudhary .data = &ipv6_devconf.ra_defrtr_metric, 67676b2e04bcSPraveen Chaudhary .maxlen = sizeof(u32), 67686b2e04bcSPraveen Chaudhary .mode = 0644, 67696b2e04bcSPraveen Chaudhary .proc_handler = proc_douintvec_minmax, 67706b2e04bcSPraveen Chaudhary .extra1 = (void *)SYSCTL_ONE, 67716b2e04bcSPraveen Chaudhary }, 67726b2e04bcSPraveen Chaudhary { 67738013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 67748013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 67758013d1d7SHangbin Liu .maxlen = sizeof(int), 67768013d1d7SHangbin Liu .mode = 0644, 67778013d1d7SHangbin Liu .proc_handler = proc_dointvec, 67788013d1d7SHangbin Liu }, 67798013d1d7SHangbin Liu { 6780c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6781c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6782c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6783c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 67846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6785c4fd30ebSYOSHIFUJI Hideaki }, 6786930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6787930d6ff2SYOSHIFUJI Hideaki { 6788930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6789930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6790930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6791930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 67926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6793930d6ff2SYOSHIFUJI Hideaki }, 679452e16356SYOSHIFUJI Hideaki { 679552e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 679652e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 679752e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 679852e16356SYOSHIFUJI Hideaki .mode = 0644, 67996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 680052e16356SYOSHIFUJI Hideaki }, 6801fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 680209c884d4SYOSHIFUJI Hideaki { 6803bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6804bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6805bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6806bbea124bSJoel Scherpelz .mode = 0644, 6807bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6808bbea124bSJoel Scherpelz }, 6809bbea124bSJoel Scherpelz { 681009c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 681109c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 681209c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 681309c884d4SYOSHIFUJI Hideaki .mode = 0644, 68146d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 681509c884d4SYOSHIFUJI Hideaki }, 681609c884d4SYOSHIFUJI Hideaki #endif 6817930d6ff2SYOSHIFUJI Hideaki #endif 6818c4fd30ebSYOSHIFUJI Hideaki { 6819fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6820fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6821fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6822fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6823c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6824fbea49e1SYOSHIFUJI Hideaki }, 6825fbea49e1SYOSHIFUJI Hideaki { 68260bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 68270bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 68280bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 68290bcbc926SYOSHIFUJI Hideaki .mode = 0644, 68306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68310bcbc926SYOSHIFUJI Hideaki }, 683295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 683395c385b4SNeil Horman { 683495c385b4SNeil Horman .procname = "optimistic_dad", 683595c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 683695c385b4SNeil Horman .maxlen = sizeof(int), 683795c385b4SNeil Horman .mode = 0644, 68386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 683995c385b4SNeil Horman }, 68407fd2561eSErik Kline { 68417fd2561eSErik Kline .procname = "use_optimistic", 68427fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 68437fd2561eSErik Kline .maxlen = sizeof(int), 68447fd2561eSErik Kline .mode = 0644, 68457fd2561eSErik Kline .proc_handler = proc_dointvec, 68467fd2561eSErik Kline }, 684795c385b4SNeil Horman #endif 68487bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 68497bc570c8SYOSHIFUJI Hideaki { 68507bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 68517bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 68527bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6853a4e6db07SDavid S. Miller .mode = 0444, 68546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68557bc570c8SYOSHIFUJI Hideaki }, 68567bc570c8SYOSHIFUJI Hideaki #endif 68570bcbc926SYOSHIFUJI Hideaki { 6858778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6859778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6860778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6861778d80beSYOSHIFUJI Hideaki .mode = 0644, 686256d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6863778d80beSYOSHIFUJI Hideaki }, 6864778d80beSYOSHIFUJI Hideaki { 68651b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 68661b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 68671b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 68681b34be74SYOSHIFUJI Hideaki .mode = 0644, 68696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68701b34be74SYOSHIFUJI Hideaki }, 68711b34be74SYOSHIFUJI Hideaki { 6872f7734fdfSOctavian Purdila .procname = "force_tllao", 6873f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6874f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6875f7734fdfSOctavian Purdila .mode = 0644, 6876f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6877f7734fdfSOctavian Purdila }, 6878f7734fdfSOctavian Purdila { 68795cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 68805cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 68815cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 68825cb04436SHannes Frederic Sowa .mode = 0644, 68835cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 68845cb04436SHannes Frederic Sowa }, 68855cb04436SHannes Frederic Sowa { 6886b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6887b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6888b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6889b800c3b9SHannes Frederic Sowa .mode = 0644, 6890b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6891b800c3b9SHannes Frederic Sowa }, 6892b800c3b9SHannes Frederic Sowa { 6893d9333196SBen Greear .procname = "accept_ra_from_local", 6894d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6895d9333196SBen Greear .maxlen = sizeof(int), 6896d9333196SBen Greear .mode = 0644, 6897d9333196SBen Greear .proc_handler = proc_dointvec, 6898d9333196SBen Greear }, 6899d9333196SBen Greear { 6900c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6901c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6902c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6903c2943f14SHarout Hedeshian .mode = 0644, 6904c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6905c2943f14SHarout Hedeshian }, 6906c2943f14SHarout Hedeshian { 69073d1bec99SHannes Frederic Sowa .procname = "stable_secret", 69083d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 69093d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 69103d1bec99SHannes Frederic Sowa .mode = 0600, 69113d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 69123d1bec99SHannes Frederic Sowa }, 69133d1bec99SHannes Frederic Sowa { 69143985e8a3SErik Kline .procname = "use_oif_addrs_only", 69153985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 69163985e8a3SErik Kline .maxlen = sizeof(int), 69173985e8a3SErik Kline .mode = 0644, 69183985e8a3SErik Kline .proc_handler = proc_dointvec, 691935103d11SAndy Gospodarek }, 692035103d11SAndy Gospodarek { 692135103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 692235103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 692335103d11SAndy Gospodarek .maxlen = sizeof(int), 692435103d11SAndy Gospodarek .mode = 0644, 692535103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 69263985e8a3SErik Kline }, 69273985e8a3SErik Kline { 6928abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6929abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6930abbc3043SJohannes Berg .maxlen = sizeof(int), 6931abbc3043SJohannes Berg .mode = 0644, 6932abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6933abbc3043SJohannes Berg }, 6934abbc3043SJohannes Berg { 69357a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 69367a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 69377a02bf89SJohannes Berg .maxlen = sizeof(int), 69387a02bf89SJohannes Berg .mode = 0644, 69397a02bf89SJohannes Berg .proc_handler = proc_dointvec, 69407a02bf89SJohannes Berg }, 69417a02bf89SJohannes Berg { 6942f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6943f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6944f1705ec1SDavid Ahern .maxlen = sizeof(int), 6945f1705ec1SDavid Ahern .mode = 0644, 6946f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6947f1705ec1SDavid Ahern 6948f1705ec1SDavid Ahern }, 6949f1705ec1SDavid Ahern { 69501ababebaSDavid Lebrun .procname = "seg6_enabled", 69511ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 69521ababebaSDavid Lebrun .maxlen = sizeof(int), 69531ababebaSDavid Lebrun .mode = 0644, 69541ababebaSDavid Lebrun .proc_handler = proc_dointvec, 69551ababebaSDavid Lebrun }, 6956bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6957bf355b8dSDavid Lebrun { 6958bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6959bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6960bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6961bf355b8dSDavid Lebrun .mode = 0644, 6962bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6963bf355b8dSDavid Lebrun }, 6964bf355b8dSDavid Lebrun #endif 69651ababebaSDavid Lebrun { 6966adc176c5SErik Nordmark .procname = "enhanced_dad", 6967adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6968adc176c5SErik Nordmark .maxlen = sizeof(int), 6969adc176c5SErik Nordmark .mode = 0644, 6970adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6971adc176c5SErik Nordmark }, 6972adc176c5SErik Nordmark { 6973d35a00b8SFelix Jia .procname = "addr_gen_mode", 6974d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6975d35a00b8SFelix Jia .maxlen = sizeof(int), 6976d35a00b8SFelix Jia .mode = 0644, 6977d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6978d35a00b8SFelix Jia }, 6979d35a00b8SFelix Jia { 6980df789fe7SDavid Forster .procname = "disable_policy", 6981df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6982df789fe7SDavid Forster .maxlen = sizeof(int), 6983df789fe7SDavid Forster .mode = 0644, 6984df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6985df789fe7SDavid Forster }, 6986df789fe7SDavid Forster { 69872210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 69882210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 69892210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 69902210d6b2SMaciej Żenczykowski .mode = 0644, 69912210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6992eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ZERO, 69932210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 69942210d6b2SMaciej Żenczykowski }, 69952210d6b2SMaciej Żenczykowski { 69968610c7c6SAlexander Aring .procname = "rpl_seg_enabled", 69978610c7c6SAlexander Aring .data = &ipv6_devconf.rpl_seg_enabled, 69988610c7c6SAlexander Aring .maxlen = sizeof(int), 69998610c7c6SAlexander Aring .mode = 0644, 70008610c7c6SAlexander Aring .proc_handler = proc_dointvec, 70018610c7c6SAlexander Aring }, 70028610c7c6SAlexander Aring { 70039ee11f0fSJustin Iurman .procname = "ioam6_enabled", 70049ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_enabled, 70059ee11f0fSJustin Iurman .maxlen = sizeof(u8), 70069ee11f0fSJustin Iurman .mode = 0644, 70079ee11f0fSJustin Iurman .proc_handler = proc_dou8vec_minmax, 70089ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO, 70099ee11f0fSJustin Iurman .extra2 = (void *)SYSCTL_ONE, 70109ee11f0fSJustin Iurman }, 70119ee11f0fSJustin Iurman { 70129ee11f0fSJustin Iurman .procname = "ioam6_id", 70139ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id, 70149ee11f0fSJustin Iurman .maxlen = sizeof(u32), 70159ee11f0fSJustin Iurman .mode = 0644, 70169ee11f0fSJustin Iurman .proc_handler = proc_douintvec_minmax, 70179ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO, 70189ee11f0fSJustin Iurman .extra2 = (void *)&ioam6_if_id_max, 70199ee11f0fSJustin Iurman }, 70209ee11f0fSJustin Iurman { 70219ee11f0fSJustin Iurman .procname = "ioam6_id_wide", 70229ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id_wide, 70239ee11f0fSJustin Iurman .maxlen = sizeof(u32), 70249ee11f0fSJustin Iurman .mode = 0644, 70259ee11f0fSJustin Iurman .proc_handler = proc_douintvec, 70269ee11f0fSJustin Iurman }, 70279ee11f0fSJustin Iurman { 702818ac597aSJames Prestwood .procname = "ndisc_evict_nocarrier", 702918ac597aSJames Prestwood .data = &ipv6_devconf.ndisc_evict_nocarrier, 703018ac597aSJames Prestwood .maxlen = sizeof(u8), 703118ac597aSJames Prestwood .mode = 0644, 703218ac597aSJames Prestwood .proc_handler = proc_dou8vec_minmax, 703318ac597aSJames Prestwood .extra1 = (void *)SYSCTL_ZERO, 703418ac597aSJames Prestwood .extra2 = (void *)SYSCTL_ONE, 703518ac597aSJames Prestwood }, 703618ac597aSJames Prestwood { 70373e0b8f52SArun Ajith S .procname = "accept_untracked_na", 70383e0b8f52SArun Ajith S .data = &ipv6_devconf.accept_untracked_na, 7039f9a2fb73SArun Ajith S .maxlen = sizeof(int), 7040f9a2fb73SArun Ajith S .mode = 0644, 7041d09d3ec0SArun Ajith S .proc_handler = proc_dointvec_minmax, 7042f9a2fb73SArun Ajith S .extra1 = (void *)SYSCTL_ZERO, 7043f9a2fb73SArun Ajith S .extra2 = (void *)SYSCTL_ONE, 7044f9a2fb73SArun Ajith S }, 7045f9a2fb73SArun Ajith S { 7046f8572d8fSEric W. Biederman /* sentinel */ 70471da177e4SLinus Torvalds } 70481da177e4SLinus Torvalds }; 70491da177e4SLinus Torvalds 7050bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 7051f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 70521da177e4SLinus Torvalds { 705329c994e3SNicolas Dichtel int i, ifindex; 7054607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 70556105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 70561dab6222SPavel Emelyanov 7057425b9c7fSVasily Averin table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT); 7058607ea7cdSKonstantin Khlebnikov if (!table) 7059f68635e6SPavel Emelyanov goto out; 7060f68635e6SPavel Emelyanov 7061607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 7062607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 7063cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 7064cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 7065cb9e684eSMaciej Żenczykowski * usable. 7066cb9e684eSMaciej Żenczykowski */ 7067cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 7068607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 7069607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 70701da177e4SLinus Torvalds } 7071cb9e684eSMaciej Żenczykowski } 70721da177e4SLinus Torvalds 70736105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 70741da177e4SLinus Torvalds 7075607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 7076607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 70776105e293SEric W. Biederman goto free; 7078f68635e6SPavel Emelyanov 707929c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 708029c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 708129c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 708229c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 708329c994e3SNicolas Dichtel else 708429c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 708585b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 708685b3daadSDavid Ahern ifindex, p); 708795897312SPavel Emelyanov return 0; 70881da177e4SLinus Torvalds 70891da177e4SLinus Torvalds free: 7090607ea7cdSKonstantin Khlebnikov kfree(table); 7091f68635e6SPavel Emelyanov out: 709295897312SPavel Emelyanov return -ENOBUFS; 70931da177e4SLinus Torvalds } 70941da177e4SLinus Torvalds 709523452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 709623452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 7097f52295a9SPavel Emelyanov { 7098607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 7099f52295a9SPavel Emelyanov 7100607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 7101408c4768SPavel Emelyanov return; 7102408c4768SPavel Emelyanov 7103607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 7104607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 7105607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 7106607ea7cdSKonstantin Khlebnikov kfree(table); 710723452170SDavid Ahern 710823452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 71091da177e4SLinus Torvalds } 7110408c4768SPavel Emelyanov 7111a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 7112408c4768SPavel Emelyanov { 7113a317a2f1SWANG Cong int err; 7114a317a2f1SWANG Cong 7115a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 7116a317a2f1SWANG Cong return -EINVAL; 7117a317a2f1SWANG Cong 7118a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 7119f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 7120a317a2f1SWANG Cong if (err) 7121a317a2f1SWANG Cong return err; 7122a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 7123f8572d8fSEric W. Biederman idev, &idev->cnf); 7124a317a2f1SWANG Cong if (err) 7125a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 7126a317a2f1SWANG Cong 7127a317a2f1SWANG Cong return err; 7128408c4768SPavel Emelyanov } 7129408c4768SPavel Emelyanov 7130408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 7131408c4768SPavel Emelyanov { 713223452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 713323452170SDavid Ahern idev->dev->ifindex); 7134408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 71351da177e4SLinus Torvalds } 71361da177e4SLinus Torvalds 71371da177e4SLinus Torvalds 71381da177e4SLinus Torvalds #endif 71391da177e4SLinus Torvalds 71402c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 7141e0da5a48SPavel Emelyanov { 7142a79ca223SHong Zhiguo int err = -ENOMEM; 7143e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 7144e0da5a48SPavel Emelyanov 714521a216a8SEric Dumazet spin_lock_init(&net->ipv6.addrconf_hash_lock); 71468805d13fSEric Dumazet INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work); 714721a216a8SEric Dumazet net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE, 714821a216a8SEric Dumazet sizeof(struct hlist_head), 714921a216a8SEric Dumazet GFP_KERNEL); 715021a216a8SEric Dumazet if (!net->ipv6.inet6_addr_lst) 715121a216a8SEric Dumazet goto err_alloc_addr; 715221a216a8SEric Dumazet 7153a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 715463159f29SIan Morris if (!all) 7155e0da5a48SPavel Emelyanov goto err_alloc_all; 7156e0da5a48SPavel Emelyanov 7157a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 715863159f29SIan Morris if (!dflt) 7159e0da5a48SPavel Emelyanov goto err_alloc_dflt; 7160a79ca223SHong Zhiguo 7161a154d5d8SArnd Bergmann if (IS_ENABLED(CONFIG_SYSCTL) && 71629efd6a3cSNicolas Dichtel !net_eq(net, &init_net)) { 71639efd6a3cSNicolas Dichtel switch (sysctl_devconf_inherit_init_net) { 71649efd6a3cSNicolas Dichtel case 1: /* copy from init_net */ 71659efd6a3cSNicolas Dichtel memcpy(all, init_net.ipv6.devconf_all, 71669efd6a3cSNicolas Dichtel sizeof(ipv6_devconf)); 71679efd6a3cSNicolas Dichtel memcpy(dflt, init_net.ipv6.devconf_dflt, 71689efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt)); 71699efd6a3cSNicolas Dichtel break; 71709efd6a3cSNicolas Dichtel case 3: /* copy from the current netns */ 71719efd6a3cSNicolas Dichtel memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all, 71729efd6a3cSNicolas Dichtel sizeof(ipv6_devconf)); 71739efd6a3cSNicolas Dichtel memcpy(dflt, 71749efd6a3cSNicolas Dichtel current->nsproxy->net_ns->ipv6.devconf_dflt, 71759efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt)); 71769efd6a3cSNicolas Dichtel break; 71779efd6a3cSNicolas Dichtel case 0: 71789efd6a3cSNicolas Dichtel case 2: 71799efd6a3cSNicolas Dichtel /* use compiled values */ 71809efd6a3cSNicolas Dichtel break; 71819efd6a3cSNicolas Dichtel } 7182856c395cSCong Wang } 7183856c395cSCong Wang 718456d417b1SBrian Haley /* these will be inherited by all namespaces */ 718556d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 718656d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 7187e0da5a48SPavel Emelyanov 71883d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 71893d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 71903d1bec99SHannes Frederic Sowa 7191e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 7192e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 7193e0da5a48SPavel Emelyanov 7194e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 7195f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 7196e0da5a48SPavel Emelyanov if (err < 0) 7197e0da5a48SPavel Emelyanov goto err_reg_all; 7198e0da5a48SPavel Emelyanov 7199f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 7200e0da5a48SPavel Emelyanov if (err < 0) 7201e0da5a48SPavel Emelyanov goto err_reg_dflt; 7202e0da5a48SPavel Emelyanov #endif 7203e0da5a48SPavel Emelyanov return 0; 7204e0da5a48SPavel Emelyanov 7205e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 7206e0da5a48SPavel Emelyanov err_reg_dflt: 720723452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 7208e0da5a48SPavel Emelyanov err_reg_all: 7209e0da5a48SPavel Emelyanov kfree(dflt); 7210e0da5a48SPavel Emelyanov #endif 7211e0da5a48SPavel Emelyanov err_alloc_dflt: 7212e0da5a48SPavel Emelyanov kfree(all); 7213e0da5a48SPavel Emelyanov err_alloc_all: 721421a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst); 721521a216a8SEric Dumazet err_alloc_addr: 7216e0da5a48SPavel Emelyanov return err; 7217e0da5a48SPavel Emelyanov } 7218e0da5a48SPavel Emelyanov 72192c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 7220e0da5a48SPavel Emelyanov { 722121a216a8SEric Dumazet int i; 722221a216a8SEric Dumazet 7223e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 722423452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 722523452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 722623452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 722723452170SDavid Ahern NETCONFA_IFINDEX_ALL); 7228e0da5a48SPavel Emelyanov #endif 7229e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 72307d9b1b57SEric Dumazet net->ipv6.devconf_dflt = NULL; 7231e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 72327d9b1b57SEric Dumazet net->ipv6.devconf_all = NULL; 723321a216a8SEric Dumazet 7234be6b41c1SEric Dumazet cancel_delayed_work_sync(&net->ipv6.addr_chk_work); 723521a216a8SEric Dumazet /* 723621a216a8SEric Dumazet * Check hash table, then free it. 723721a216a8SEric Dumazet */ 723821a216a8SEric Dumazet for (i = 0; i < IN6_ADDR_HSIZE; i++) 723921a216a8SEric Dumazet WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i])); 724021a216a8SEric Dumazet 724121a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst); 724221a216a8SEric Dumazet net->ipv6.inet6_addr_lst = NULL; 7243e0da5a48SPavel Emelyanov } 7244e0da5a48SPavel Emelyanov 7245e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 7246e0da5a48SPavel Emelyanov .init = addrconf_init_net, 7247e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 7248e0da5a48SPavel Emelyanov }; 7249e0da5a48SPavel Emelyanov 7250207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 7251b382b191SThomas Graf .family = AF_INET6, 7252b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 7253b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 725411b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 7255f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 7256b382b191SThomas Graf }; 7257b382b191SThomas Graf 72581da177e4SLinus Torvalds /* 72591da177e4SLinus Torvalds * Init / cleanup code 72601da177e4SLinus Torvalds */ 72611da177e4SLinus Torvalds 72621da177e4SLinus Torvalds int __init addrconf_init(void) 72631da177e4SLinus Torvalds { 7264a317a2f1SWANG Cong struct inet6_dev *idev; 7265e66d1172SEric Dumazet int err; 72662a8cc6c8SYOSHIFUJI Hideaki 7267e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 7268e21e8467SStephen Hemminger if (err < 0) { 7269f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 7270f3213831SJoe Perches __func__, err); 72712cc6d2bfSNeil Horman goto out; 72722a8cc6c8SYOSHIFUJI Hideaki } 72731da177e4SLinus Torvalds 72742cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 72752cc6d2bfSNeil Horman if (err < 0) 72762cc6d2bfSNeil Horman goto out_addrlabel; 7277e0da5a48SPavel Emelyanov 7278c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 7279c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 7280c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 7281c15b1ccaSHannes Frederic Sowa goto out_nowq; 7282c15b1ccaSHannes Frederic Sowa } 7283c15b1ccaSHannes Frederic Sowa 72841da177e4SLinus Torvalds rtnl_lock(); 7285e5f80fcfSEric Dumazet idev = ipv6_add_dev(blackhole_netdev); 72861da177e4SLinus Torvalds rtnl_unlock(); 7287a317a2f1SWANG Cong if (IS_ERR(idev)) { 7288a317a2f1SWANG Cong err = PTR_ERR(idev); 7289e0da5a48SPavel Emelyanov goto errlo; 7290a317a2f1SWANG Cong } 72911da177e4SLinus Torvalds 72922f460933SWANG Cong ip6_route_init_special_entries(); 72932f460933SWANG Cong 72941da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 72951da177e4SLinus Torvalds 72968805d13fSEric Dumazet addrconf_verify(&init_net); 7297c127ea2cSThomas Graf 72983678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 7299b382b191SThomas Graf 730016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 730116feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 7302c127ea2cSThomas Graf if (err < 0) 7303c127ea2cSThomas Graf goto errout; 7304c127ea2cSThomas Graf 730516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 730616feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 730716feebcfSFlorian Westphal if (err < 0) 730816feebcfSFlorian Westphal goto errout; 730916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 731016feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 731116feebcfSFlorian Westphal if (err < 0) 731216feebcfSFlorian Westphal goto errout; 731316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 731416feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 731516feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 731616feebcfSFlorian Westphal if (err < 0) 731716feebcfSFlorian Westphal goto errout; 731816feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 731916feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 732016feebcfSFlorian Westphal if (err < 0) 732116feebcfSFlorian Westphal goto errout; 732216feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 732316feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 732416feebcfSFlorian Westphal if (err < 0) 732516feebcfSFlorian Westphal goto errout; 732616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 732716feebcfSFlorian Westphal inet6_netconf_get_devconf, 732816feebcfSFlorian Westphal inet6_netconf_dump_devconf, 732916feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 733016feebcfSFlorian Westphal if (err < 0) 733116feebcfSFlorian Westphal goto errout; 7332a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 7333a3fde2adSFlorian Westphal if (err < 0) 7334a3fde2adSFlorian Westphal goto errout; 73352a8cc6c8SYOSHIFUJI Hideaki 73361da177e4SLinus Torvalds return 0; 7337c127ea2cSThomas Graf errout: 733816feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 7339b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 7340c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 7341e0da5a48SPavel Emelyanov errlo: 7342c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 7343c15b1ccaSHannes Frederic Sowa out_nowq: 7344e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 73452cc6d2bfSNeil Horman out_addrlabel: 73462cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 73472cc6d2bfSNeil Horman out: 7348c127ea2cSThomas Graf return err; 73491da177e4SLinus Torvalds } 73501da177e4SLinus Torvalds 735109f7709fSDaniel Lezcano void addrconf_cleanup(void) 73521da177e4SLinus Torvalds { 7353176c39afSDaniel Lezcano struct net_device *dev; 73541da177e4SLinus Torvalds 73551da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 7356e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 73572cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 73581da177e4SLinus Torvalds 73595c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 73601da177e4SLinus Torvalds 73615c45121dSFlorian Westphal rtnl_lock(); 7362b382b191SThomas Graf 7363176c39afSDaniel Lezcano /* clean dev list */ 7364176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 7365176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 7366176c39afSDaniel Lezcano continue; 7367ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, true); 7368176c39afSDaniel Lezcano } 7369ae79dbf6SFlorent Fourcot addrconf_ifdown(init_net.loopback_dev, true); 7370176c39afSDaniel Lezcano 73711da177e4SLinus Torvalds rtnl_unlock(); 7372c15b1ccaSHannes Frederic Sowa 7373c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 73741da177e4SLinus Torvalds } 7375