11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 Address [auto]configuration 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 101da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 111da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 121da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds /* 161da177e4SLinus Torvalds * Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 191da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 201da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 211da177e4SLinus Torvalds * unload. 221da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 231da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 241da177e4SLinus Torvalds * packets. 251da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 281da177e4SLinus Torvalds * address validation timer. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 301da177e4SLinus Torvalds * support. 311da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 321da177e4SLinus Torvalds * address on a same interface. 331da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 341da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 351da177e4SLinus Torvalds * seq_file. 36b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 37b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 38b1cacb68SYOSHIFUJI Hideaki * status etc. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 42f3213831SJoe Perches 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 45a0bffffcSIlpo Järvinen #include <linux/kernel.h> 461da177e4SLinus Torvalds #include <linux/socket.h> 471da177e4SLinus Torvalds #include <linux/sockios.h> 481da177e4SLinus Torvalds #include <linux/net.h> 493d1bec99SHannes Frederic Sowa #include <linux/inet.h> 501da177e4SLinus Torvalds #include <linux/in6.h> 511da177e4SLinus Torvalds #include <linux/netdevice.h> 521823730fSThomas Graf #include <linux/if_addr.h> 531da177e4SLinus Torvalds #include <linux/if_arp.h> 541da177e4SLinus Torvalds #include <linux/if_arcnet.h> 551da177e4SLinus Torvalds #include <linux/if_infiniband.h> 561da177e4SLinus Torvalds #include <linux/route.h> 571da177e4SLinus Torvalds #include <linux/inetdevice.h> 581da177e4SLinus Torvalds #include <linux/init.h> 595a0e3ad6STejun Heo #include <linux/slab.h> 601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 611da177e4SLinus Torvalds #include <linux/sysctl.h> 621da177e4SLinus Torvalds #endif 634fc268d2SRandy Dunlap #include <linux/capability.h> 641da177e4SLinus Torvalds #include <linux/delay.h> 651da177e4SLinus Torvalds #include <linux/notifier.h> 66543537bdSPaulo Marques #include <linux/string.h> 67ddbe5032SEric Dumazet #include <linux/hash.h> 681da177e4SLinus Torvalds 69457c4cbcSEric W. Biederman #include <net/net_namespace.h> 701da177e4SLinus Torvalds #include <net/sock.h> 711da177e4SLinus Torvalds #include <net/snmp.h> 721da177e4SLinus Torvalds 735241c2d7SAlexander Aring #include <net/6lowpan.h> 74cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 751da177e4SLinus Torvalds #include <net/ipv6.h> 761da177e4SLinus Torvalds #include <net/protocol.h> 771da177e4SLinus Torvalds #include <net/ndisc.h> 781da177e4SLinus Torvalds #include <net/ip6_route.h> 791da177e4SLinus Torvalds #include <net/addrconf.h> 801da177e4SLinus Torvalds #include <net/tcp.h> 811da177e4SLinus Torvalds #include <net/ip.h> 825d620266SThomas Graf #include <net/netlink.h> 83f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 84ca254490SDavid Ahern #include <net/l3mdev.h> 851da177e4SLinus Torvalds #include <linux/if_tunnel.h> 861da177e4SLinus Torvalds #include <linux/rtnetlink.h> 87f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 881da177e4SLinus Torvalds #include <linux/random.h> 89e21e8467SStephen Hemminger #include <linux/uaccess.h> 907f7d9a6bSHerbert Xu #include <asm/unaligned.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include <linux/proc_fs.h> 931da177e4SLinus Torvalds #include <linux/seq_file.h> 94bc3b2d7fSPaul Gortmaker #include <linux/export.h> 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 971da177e4SLinus Torvalds #define ACONF_DEBUG 2 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 100ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 1011da177e4SLinus Torvalds #else 102ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10618a31e1eSThomas Graf 1073d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 1083d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 1093d1bec99SHannes Frederic Sowa 11018a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 11118a31e1eSThomas Graf { 11218a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11318a31e1eSThomas Graf } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 116a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 117408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 118408c4768SPavel Emelyanov #else 119a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 120408c4768SPavel Emelyanov { 121a317a2f1SWANG Cong return 0; 122408c4768SPavel Emelyanov } 123408c4768SPavel Emelyanov 124408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 125408c4768SPavel Emelyanov { 126408c4768SPavel Emelyanov } 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 129eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev); 130eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1311da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1321da177e4SLinus Torvalds 1331b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1341da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 135622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 136622c81d5SHannes Frederic Sowa u8 dad_count, 137622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * Configured unicast address hash table 1411da177e4SLinus Torvalds */ 142c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1435c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1441da177e4SLinus Torvalds 145c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 146c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 147c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1481da177e4SLinus Torvalds 149c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 150c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1531da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1541da177e4SLinus Torvalds 15593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15675c78500SMoni Shoua unsigned long event); 1571da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1581da177e4SLinus Torvalds 15921caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 16021caa662SRomain Kuntz int plen, 16121caa662SRomain Kuntz const struct net_device *dev, 16221caa662SRomain Kuntz u32 flags, u32 noflags); 16321caa662SRomain Kuntz 164cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 165c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 1661da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 167c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1681da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1691da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1701da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1731da177e4SLinus Torvalds struct prefix_info *pinfo); 1743e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 17506bfe655SDaniel Lezcano struct net_device *dev); 1761da177e4SLinus Torvalds 177888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1781da177e4SLinus Torvalds .forwarding = 0, 1791da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1801da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1811da177e4SLinus Torvalds .accept_ra = 1, 1821da177e4SLinus Torvalds .accept_redirects = 1, 1831da177e4SLinus Torvalds .autoconf = 1, 1841da177e4SLinus Torvalds .force_mld_version = 0, 185fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 186fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 1871da177e4SLinus Torvalds .dad_transmits = 1, 1881da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1891da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1901da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1911da177e4SLinus Torvalds .use_tempaddr = 0, 1921da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1931da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1941da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1951da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1961da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19765f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 198d9333196SBen Greear .accept_ra_from_local = 0, 1998013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 200c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 201930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 202930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 20352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 20409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 20509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 20609c884d4SYOSHIFUJI Hideaki #endif 207930d6ff2SYOSHIFUJI Hideaki #endif 208fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2090bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 210778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2111b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 212b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 213c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2143d1bec99SHannes Frederic Sowa .stable_secret = { 2153d1bec99SHannes Frederic Sowa .initialized = false, 2163985e8a3SErik Kline }, 2173985e8a3SErik Kline .use_oif_addrs_only = 0, 21835103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 219f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2201da177e4SLinus Torvalds }; 2211da177e4SLinus Torvalds 222ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2231da177e4SLinus Torvalds .forwarding = 0, 2241da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2251da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2261da177e4SLinus Torvalds .accept_ra = 1, 2271da177e4SLinus Torvalds .accept_redirects = 1, 2281da177e4SLinus Torvalds .autoconf = 1, 229fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 230fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 231fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2321da177e4SLinus Torvalds .dad_transmits = 1, 2331da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2341da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2351da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2361da177e4SLinus Torvalds .use_tempaddr = 0, 2371da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2381da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2391da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2401da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2411da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 24265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 243d9333196SBen Greear .accept_ra_from_local = 0, 2448013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 245c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 246930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 247930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 24852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 24909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 25009c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 25109c884d4SYOSHIFUJI Hideaki #endif 252930d6ff2SYOSHIFUJI Hideaki #endif 253fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2540bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 255778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2561b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 257b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 258c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2593d1bec99SHannes Frederic Sowa .stable_secret = { 2603d1bec99SHannes Frederic Sowa .initialized = false, 2613d1bec99SHannes Frederic Sowa }, 2623985e8a3SErik Kline .use_oif_addrs_only = 0, 26335103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 264f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2651da177e4SLinus Torvalds }; 2661da177e4SLinus Torvalds 267f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 26805297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 269f24e3d65SMitsuru Chinen { 27005297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 271f24e3d65SMitsuru Chinen } 272f24e3d65SMitsuru Chinen 273b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 2741da177e4SLinus Torvalds { 275b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 276b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 277b7b1bfceSHannes Frederic Sowa } 278b7b1bfceSHannes Frederic Sowa 279c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 280b7b1bfceSHannes Frederic Sowa { 281c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 2821da177e4SLinus Torvalds __in6_ifa_put(ifp); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 285b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 2861da177e4SLinus Torvalds unsigned long when) 2871da177e4SLinus Torvalds { 288b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 289b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 290b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 2911da177e4SLinus Torvalds } 292b7b1bfceSHannes Frederic Sowa 293c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 294c15b1ccaSHannes Frederic Sowa unsigned long delay) 295b7b1bfceSHannes Frederic Sowa { 296c15b1ccaSHannes Frederic Sowa if (!delayed_work_pending(&ifp->dad_work)) 297b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 298c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3017f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3027f7d9a6bSHerbert Xu { 303827da44cSJohn Stultz int i; 304827da44cSJohn Stultz 305698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 306698365faSWANG Cong if (!idev->stats.ipv6) 3077f7d9a6bSHerbert Xu goto err_ip; 308827da44cSJohn Stultz 309827da44cSJohn Stultz for_each_possible_cpu(i) { 310827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 311698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 312827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 313827da44cSJohn Stultz } 314827da44cSJohn Stultz 315827da44cSJohn Stultz 316be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 317be281e55SEric Dumazet GFP_KERNEL); 318be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3197f7d9a6bSHerbert Xu goto err_icmp; 320be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 321be281e55SEric Dumazet GFP_KERNEL); 322be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 32314878f75SDavid L Stevens goto err_icmpmsg; 3247f7d9a6bSHerbert Xu 3257f7d9a6bSHerbert Xu return 0; 3267f7d9a6bSHerbert Xu 32714878f75SDavid L Stevens err_icmpmsg: 328be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3297f7d9a6bSHerbert Xu err_icmp: 330698365faSWANG Cong free_percpu(idev->stats.ipv6); 3317f7d9a6bSHerbert Xu err_ip: 332aaf70ec7SPavel Emelyanov return -ENOMEM; 3337f7d9a6bSHerbert Xu } 3347f7d9a6bSHerbert Xu 3351da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct inet6_dev *ndev; 338a317a2f1SWANG Cong int err = -ENOMEM; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds ASSERT_RTNL(); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 343a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3441da177e4SLinus Torvalds 345322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 34663159f29SIan Morris if (!ndev) 347a317a2f1SWANG Cong return ERR_PTR(err); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3501da177e4SLinus Torvalds ndev->dev = dev; 351502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 352b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 353b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 354c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3559b29c696SHannes Frederic Sowa 3569b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 3579b29c696SHannes Frederic Sowa ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3589b29c696SHannes Frederic Sowa else 3599b29c696SHannes Frederic Sowa ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64; 3609b29c696SHannes Frederic Sowa 3611da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3621da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 36363159f29SIan Morris if (!ndev->nd_parms) { 3641da177e4SLinus Torvalds kfree(ndev); 365a317a2f1SWANG Cong return ERR_PTR(err); 3661da177e4SLinus Torvalds } 3670187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3680187bdfbSBen Hutchings dev_disable_lro(dev); 3691da177e4SLinus Torvalds /* We refer to the device */ 3701da177e4SLinus Torvalds dev_hold(dev); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 373ba3542e1Sdingtianhong ADBG(KERN_WARNING 374f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 375ba3542e1Sdingtianhong __func__, dev->name); 3761da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3778603e33dSRoy Li dev_put(dev); 3788603e33dSRoy Li kfree(ndev); 379a317a2f1SWANG Cong return ERR_PTR(err); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 383ba3542e1Sdingtianhong ADBG(KERN_WARNING 384f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 385ba3542e1Sdingtianhong __func__, dev->name); 386a317a2f1SWANG Cong goto err_release; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* One reference from device. We must do this before 3901da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds in6_dev_hold(ndev); 3931da177e4SLinus Torvalds 3941b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 3951b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 3961b34be74SYOSHIFUJI Hideaki 39707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 398c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 399f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 400c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 401c7dc89c0SFred L. Templin } 402b077d7abSYOSHIFUJI Hideaki #endif 403b077d7abSYOSHIFUJI Hideaki 404372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4051da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4061da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4071da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4089625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4091da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4101da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4110be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4121da177e4SLinus Torvalds } else { 4131da177e4SLinus Torvalds in6_dev_hold(ndev); 4141da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4151da177e4SLinus Torvalds } 4165d9efa7eSDavid S. Miller 417914faa14SDaniel Borkmann ndev->token = in6addr_any; 4181da177e4SLinus Torvalds 419f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 42053aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 42153aadcc9SHerbert Xu 4221da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4231da177e4SLinus Torvalds ndev->tstamp = jiffies; 424a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 425a317a2f1SWANG Cong if (err) { 426a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 427a317a2f1SWANG Cong del_timer(&ndev->regen_timer); 4282a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 429a317a2f1SWANG Cong goto err_release; 430a317a2f1SWANG Cong } 43130c4cf57SDavid L Stevens /* protected by rtnl_lock */ 432cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 433d88ae4ccSYOSHIFUJI Hideaki 4342c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4352c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4362c5e8933SHannes Frederic Sowa 437d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 438f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 439d88ae4ccSYOSHIFUJI Hideaki 440d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4418b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 442d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 443d6ddef9eSLi Wei 4441da177e4SLinus Torvalds return ndev; 445a317a2f1SWANG Cong 446a317a2f1SWANG Cong err_release: 447a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 448a317a2f1SWANG Cong ndev->dead = 1; 449a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 450a317a2f1SWANG Cong return ERR_PTR(err); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 453cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds struct inet6_dev *idev; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds ASSERT_RTNL(); 4581da177e4SLinus Torvalds 459e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 460e21e8467SStephen Hemminger if (!idev) { 461e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 462a317a2f1SWANG Cong if (IS_ERR(idev)) 4631da177e4SLinus Torvalds return NULL; 4641da177e4SLinus Torvalds } 465c5e33bddSYOSHIFUJI Hideaki 4661da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4671da177e4SLinus Torvalds ipv6_mc_up(idev); 4681da177e4SLinus Torvalds return idev; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 472f3a1bfb1SNicolas Dichtel { 473f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 474f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 475136ba622SZhang Shengju bool all = false; 476f3a1bfb1SNicolas Dichtel 477136ba622SZhang Shengju if (type == NETCONFA_ALL) 478136ba622SZhang Shengju all = true; 479136ba622SZhang Shengju 480136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 481f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 482b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 483136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 484d67b8c61SNicolas Dichtel size += nla_total_size(4); 485b1afce95SDavid S. Miller #endif 486136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 487c92d5491Sstephen hemminger size += nla_total_size(4); 488f3a1bfb1SNicolas Dichtel 489136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 49035103d11SAndy Gospodarek size += nla_total_size(4); 49135103d11SAndy Gospodarek 492f3a1bfb1SNicolas Dichtel return size; 493f3a1bfb1SNicolas Dichtel } 494f3a1bfb1SNicolas Dichtel 495f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 496f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 497f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 498f3a1bfb1SNicolas Dichtel int type) 499f3a1bfb1SNicolas Dichtel { 500f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 501f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 502136ba622SZhang Shengju bool all = false; 503f3a1bfb1SNicolas Dichtel 504f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 505f3a1bfb1SNicolas Dichtel flags); 50663159f29SIan Morris if (!nlh) 507f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 508f3a1bfb1SNicolas Dichtel 509136ba622SZhang Shengju if (type == NETCONFA_ALL) 510136ba622SZhang Shengju all = true; 511136ba622SZhang Shengju 512f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 513f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 514f3a1bfb1SNicolas Dichtel 515f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 516f3a1bfb1SNicolas Dichtel goto nla_put_failure; 517f3a1bfb1SNicolas Dichtel 518136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 519f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 520f3a1bfb1SNicolas Dichtel goto nla_put_failure; 521b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 522136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 523d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 524d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 525d67b8c61SNicolas Dichtel goto nla_put_failure; 526b1afce95SDavid S. Miller #endif 527136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 528c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 529c92d5491Sstephen hemminger goto nla_put_failure; 530c92d5491Sstephen hemminger 531136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 53235103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 53335103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 53435103d11SAndy Gospodarek goto nla_put_failure; 53535103d11SAndy Gospodarek 536053c095aSJohannes Berg nlmsg_end(skb, nlh); 537053c095aSJohannes Berg return 0; 538f3a1bfb1SNicolas Dichtel 539f3a1bfb1SNicolas Dichtel nla_put_failure: 540f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 541f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 542f3a1bfb1SNicolas Dichtel } 543f3a1bfb1SNicolas Dichtel 544d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 545f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf) 546f3a1bfb1SNicolas Dichtel { 547f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 548f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 549f3a1bfb1SNicolas Dichtel 550*927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 55163159f29SIan Morris if (!skb) 552f3a1bfb1SNicolas Dichtel goto errout; 553f3a1bfb1SNicolas Dichtel 554f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 555f3a1bfb1SNicolas Dichtel RTM_NEWNETCONF, 0, type); 556f3a1bfb1SNicolas Dichtel if (err < 0) { 557f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 558f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 559f3a1bfb1SNicolas Dichtel kfree_skb(skb); 560f3a1bfb1SNicolas Dichtel goto errout; 561f3a1bfb1SNicolas Dichtel } 562*927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 563f3a1bfb1SNicolas Dichtel return; 564f3a1bfb1SNicolas Dichtel errout: 565f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 566f3a1bfb1SNicolas Dichtel } 567f3a1bfb1SNicolas Dichtel 56876f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 56976f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 57076f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 571c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 57235103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 57376f8f6cbSNicolas Dichtel }; 57476f8f6cbSNicolas Dichtel 57576f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 576661d2967SThomas Graf struct nlmsghdr *nlh) 57776f8f6cbSNicolas Dichtel { 57876f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 57976f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 58076f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 58176f8f6cbSNicolas Dichtel struct sk_buff *skb; 58276f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 58376f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 58476f8f6cbSNicolas Dichtel struct net_device *dev; 58576f8f6cbSNicolas Dichtel int ifindex; 58676f8f6cbSNicolas Dichtel int err; 58776f8f6cbSNicolas Dichtel 58876f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 58976f8f6cbSNicolas Dichtel devconf_ipv6_policy); 59076f8f6cbSNicolas Dichtel if (err < 0) 59176f8f6cbSNicolas Dichtel goto errout; 59276f8f6cbSNicolas Dichtel 593a97eb33fSAnton Protopopov err = -EINVAL; 59476f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 59576f8f6cbSNicolas Dichtel goto errout; 59676f8f6cbSNicolas Dichtel 59776f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 59876f8f6cbSNicolas Dichtel switch (ifindex) { 59976f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 60076f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 60176f8f6cbSNicolas Dichtel break; 60276f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 60376f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 60476f8f6cbSNicolas Dichtel break; 60576f8f6cbSNicolas Dichtel default: 60676f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 60763159f29SIan Morris if (!dev) 60876f8f6cbSNicolas Dichtel goto errout; 60976f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 61063159f29SIan Morris if (!in6_dev) 61176f8f6cbSNicolas Dichtel goto errout; 61276f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 61376f8f6cbSNicolas Dichtel break; 61476f8f6cbSNicolas Dichtel } 61576f8f6cbSNicolas Dichtel 61676f8f6cbSNicolas Dichtel err = -ENOBUFS; 617136ba622SZhang Shengju skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC); 61863159f29SIan Morris if (!skb) 61976f8f6cbSNicolas Dichtel goto errout; 62076f8f6cbSNicolas Dichtel 62176f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 62276f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 62376f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 624136ba622SZhang Shengju NETCONFA_ALL); 62576f8f6cbSNicolas Dichtel if (err < 0) { 62676f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 62776f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 62876f8f6cbSNicolas Dichtel kfree_skb(skb); 62976f8f6cbSNicolas Dichtel goto errout; 63076f8f6cbSNicolas Dichtel } 63176f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 63276f8f6cbSNicolas Dichtel errout: 63376f8f6cbSNicolas Dichtel return err; 63476f8f6cbSNicolas Dichtel } 63576f8f6cbSNicolas Dichtel 6367a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6377a674200SNicolas Dichtel struct netlink_callback *cb) 6387a674200SNicolas Dichtel { 6397a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6407a674200SNicolas Dichtel int h, s_h; 6417a674200SNicolas Dichtel int idx, s_idx; 6427a674200SNicolas Dichtel struct net_device *dev; 6437a674200SNicolas Dichtel struct inet6_dev *idev; 6447a674200SNicolas Dichtel struct hlist_head *head; 6457a674200SNicolas Dichtel 6467a674200SNicolas Dichtel s_h = cb->args[0]; 6477a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6487a674200SNicolas Dichtel 6497a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6507a674200SNicolas Dichtel idx = 0; 6517a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6527a674200SNicolas Dichtel rcu_read_lock(); 65363998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 65463998ac2SNicolas Dichtel net->dev_base_seq; 6557a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6567a674200SNicolas Dichtel if (idx < s_idx) 6577a674200SNicolas Dichtel goto cont; 6587a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6597a674200SNicolas Dichtel if (!idev) 6607a674200SNicolas Dichtel goto cont; 6617a674200SNicolas Dichtel 6627a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6637a674200SNicolas Dichtel &idev->cnf, 6647a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6657a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6667a674200SNicolas Dichtel RTM_NEWNETCONF, 6677a674200SNicolas Dichtel NLM_F_MULTI, 668136ba622SZhang Shengju NETCONFA_ALL) < 0) { 6697a674200SNicolas Dichtel rcu_read_unlock(); 6707a674200SNicolas Dichtel goto done; 6717a674200SNicolas Dichtel } 67263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 6737a674200SNicolas Dichtel cont: 6747a674200SNicolas Dichtel idx++; 6757a674200SNicolas Dichtel } 6767a674200SNicolas Dichtel rcu_read_unlock(); 6777a674200SNicolas Dichtel } 6787a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 6797a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 6807a674200SNicolas Dichtel net->ipv6.devconf_all, 6817a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6827a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6837a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 684136ba622SZhang Shengju NETCONFA_ALL) < 0) 6857a674200SNicolas Dichtel goto done; 6867a674200SNicolas Dichtel else 6877a674200SNicolas Dichtel h++; 6887a674200SNicolas Dichtel } 6897a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 6907a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 6917a674200SNicolas Dichtel net->ipv6.devconf_dflt, 6927a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6937a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6947a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 695136ba622SZhang Shengju NETCONFA_ALL) < 0) 6967a674200SNicolas Dichtel goto done; 6977a674200SNicolas Dichtel else 6987a674200SNicolas Dichtel h++; 6997a674200SNicolas Dichtel } 7007a674200SNicolas Dichtel done: 7017a674200SNicolas Dichtel cb->args[0] = h; 7027a674200SNicolas Dichtel cb->args[1] = idx; 7037a674200SNicolas Dichtel 7047a674200SNicolas Dichtel return skb->len; 7057a674200SNicolas Dichtel } 7067a674200SNicolas Dichtel 7071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7081da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct net_device *dev; 7111da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds if (!idev) 7141da177e4SLinus Torvalds return; 7151da177e4SLinus Torvalds dev = idev->dev; 7160187bdfbSBen Hutchings if (idev->cnf.forwarding) 7170187bdfbSBen Hutchings dev_disable_lro(dev); 7181a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7192c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 720f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7212c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7222c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7232c5e8933SHannes Frederic Sowa } else { 724f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7252c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7262c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7272c5e8933SHannes Frederic Sowa } 7281da177e4SLinus Torvalds } 729502a2ffdSstephen hemminger 730502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7312c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7322c12a74cSMichal Wrobel continue; 7331da177e4SLinus Torvalds if (idev->cnf.forwarding) 7341da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7351da177e4SLinus Torvalds else 7361da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7371da177e4SLinus Torvalds } 738f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 739f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds 743e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds struct net_device *dev; 7461da177e4SLinus Torvalds struct inet6_dev *idev; 7471da177e4SLinus Torvalds 7484acd4945SBen Hutchings for_each_netdev(net, dev) { 7491da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7501da177e4SLinus Torvalds if (idev) { 751e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 752e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7531da177e4SLinus Torvalds if (changed) 7541da177e4SLinus Torvalds dev_forward_change(idev); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds } 758c8fecf22SPavel Emelyanov 759013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 760c8fecf22SPavel Emelyanov { 761bff16c2fSPavel Emelyanov struct net *net; 762013d97e9SFrancesco Ruggeri int old; 763013d97e9SFrancesco Ruggeri 764013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 765013d97e9SFrancesco Ruggeri return restart_syscall(); 766bff16c2fSPavel Emelyanov 767bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 768013d97e9SFrancesco Ruggeri old = *p; 769013d97e9SFrancesco Ruggeri *p = newf; 770c8fecf22SPavel Emelyanov 771013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 772f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 773f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 774f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 775f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 776013d97e9SFrancesco Ruggeri rtnl_unlock(); 777013d97e9SFrancesco Ruggeri return 0; 77888af182eSEric W. Biederman } 779b325fddbSStephen Hemminger 780e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 781e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 782e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 783f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 784f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 785f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 786f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 787013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 788c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 7890187bdfbSBen Hutchings rtnl_unlock(); 790c8fecf22SPavel Emelyanov 791013d97e9SFrancesco Ruggeri if (newf) 7927b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 793b325fddbSStephen Hemminger return 1; 794c8fecf22SPavel Emelyanov } 79535103d11SAndy Gospodarek 79635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 79735103d11SAndy Gospodarek { 79835103d11SAndy Gospodarek struct net_device *dev; 79935103d11SAndy Gospodarek struct inet6_dev *idev; 80035103d11SAndy Gospodarek 80135103d11SAndy Gospodarek for_each_netdev(net, dev) { 80235103d11SAndy Gospodarek idev = __in6_dev_get(dev); 80335103d11SAndy Gospodarek if (idev) { 80435103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 80535103d11SAndy Gospodarek 80635103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 80735103d11SAndy Gospodarek if (changed) 80835103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 80935103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 81035103d11SAndy Gospodarek dev->ifindex, 81135103d11SAndy Gospodarek &idev->cnf); 81235103d11SAndy Gospodarek } 81335103d11SAndy Gospodarek } 81435103d11SAndy Gospodarek } 81535103d11SAndy Gospodarek 81635103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 81735103d11SAndy Gospodarek { 81835103d11SAndy Gospodarek struct net *net; 81935103d11SAndy Gospodarek int old; 82035103d11SAndy Gospodarek 82135103d11SAndy Gospodarek if (!rtnl_trylock()) 82235103d11SAndy Gospodarek return restart_syscall(); 82335103d11SAndy Gospodarek 82435103d11SAndy Gospodarek net = (struct net *)table->extra2; 82535103d11SAndy Gospodarek old = *p; 82635103d11SAndy Gospodarek *p = newf; 82735103d11SAndy Gospodarek 82835103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 82935103d11SAndy Gospodarek if ((!newf) ^ (!old)) 83035103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 83135103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 83235103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 83335103d11SAndy Gospodarek net->ipv6.devconf_dflt); 83435103d11SAndy Gospodarek rtnl_unlock(); 83535103d11SAndy Gospodarek return 0; 83635103d11SAndy Gospodarek } 83735103d11SAndy Gospodarek 83835103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 83935103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 84035103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 84135103d11SAndy Gospodarek if ((!newf) ^ (!old)) 84235103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 84335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 84435103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 84535103d11SAndy Gospodarek net->ipv6.devconf_all); 84635103d11SAndy Gospodarek } 84735103d11SAndy Gospodarek rtnl_unlock(); 84835103d11SAndy Gospodarek 84935103d11SAndy Gospodarek return 1; 85035103d11SAndy Gospodarek } 85135103d11SAndy Gospodarek 8521da177e4SLinus Torvalds #endif 8531da177e4SLinus Torvalds 8545c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8551da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 8561da177e4SLinus Torvalds { 857c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 858547b792cSIlpo Järvinen 8591da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 86091df42beSJoe Perches pr_debug("%s\n", __func__); 8611da177e4SLinus Torvalds #endif 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds in6_dev_put(ifp->idev); 8641da177e4SLinus Torvalds 865c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 866c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 867c15b1ccaSHannes Frederic Sowa ifp); 8681da177e4SLinus Torvalds 869e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 870f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 8711da177e4SLinus Torvalds return; 8721da177e4SLinus Torvalds } 87394e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 8741da177e4SLinus Torvalds 875e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 878e55ffac6SBrian Haley static void 879e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 880e55ffac6SBrian Haley { 881502a2ffdSstephen hemminger struct list_head *p; 8828a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 883e55ffac6SBrian Haley 884e55ffac6SBrian Haley /* 885e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 886e55ffac6SBrian Haley * global before linklocal. 887e55ffac6SBrian Haley */ 888502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 889502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 890502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 8918a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 892e55ffac6SBrian Haley break; 893e55ffac6SBrian Haley } 894e55ffac6SBrian Haley 895b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 896e55ffac6SBrian Haley } 897e55ffac6SBrian Haley 898ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 8993eb84f49SYOSHIFUJI Hideaki { 900ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 9013eb84f49SYOSHIFUJI Hideaki } 9023eb84f49SYOSHIFUJI Hideaki 9031da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds static struct inet6_ifaddr * 9063f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 9073f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 9088a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 9111da177e4SLinus Torvalds struct rt6_info *rt; 9123a88a81dSstephen hemminger unsigned int hash; 9131da177e4SLinus Torvalds int err = 0; 914d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 915d68b8270SYOSHIFUJI Hideaki 916d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 917d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 918d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 919d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 920d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9211da177e4SLinus Torvalds 9228814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9231da177e4SLinus Torvalds if (idev->dead) { 9241da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9251da177e4SLinus Torvalds goto out2; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 92856d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9299bdd8d40SBrian Haley err = -EACCES; 9309bdd8d40SBrian Haley goto out2; 9319bdd8d40SBrian Haley } 9329bdd8d40SBrian Haley 9335c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 936c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 937ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 9381da177e4SLinus Torvalds err = -EEXIST; 9391da177e4SLinus Torvalds goto out; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 942322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 9431da177e4SLinus Torvalds 94463159f29SIan Morris if (!ifa) { 945ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 9461da177e4SLinus Torvalds err = -ENOBUFS; 9471da177e4SLinus Torvalds goto out; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9508f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 9511da177e4SLinus Torvalds if (IS_ERR(rt)) { 9521da177e4SLinus Torvalds err = PTR_ERR(rt); 9531da177e4SLinus Torvalds goto out; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 956bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 957bba24896SJiri Pirko 9584e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 9593f8f5298SJiri Pirko if (peer_addr) 9603f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 963c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 964c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 9651da177e4SLinus Torvalds ifa->scope = scope; 9661da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 9671da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 9688a226b2cSJiri Benc ifa->valid_lft = valid_lft; 9698a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 9701da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 971617fe29dSDaniel Borkmann ifa->tokenized = false; 9721da177e4SLinus Torvalds 97357f5f544SKeir Fraser ifa->rt = rt; 97457f5f544SKeir Fraser 9751da177e4SLinus Torvalds ifa->idev = idev; 9761da177e4SLinus Torvalds in6_dev_hold(idev); 9771da177e4SLinus Torvalds /* For caller */ 9781da177e4SLinus Torvalds in6_ifa_hold(ifa); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* Add to big hash table */ 981ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 9821da177e4SLinus Torvalds 9835c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 9845c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds write_lock(&idev->lock); 9871da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 988e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 991372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 9921da177e4SLinus Torvalds in6_ifa_hold(ifa); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds in6_ifa_hold(ifa); 9961da177e4SLinus Torvalds write_unlock(&idev->lock); 9971da177e4SLinus Torvalds out2: 9988814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 9991da177e4SLinus Torvalds 1000fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 1001f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 10021da177e4SLinus Torvalds else { 10031da177e4SLinus Torvalds kfree(ifa); 10041da177e4SLinus Torvalds ifa = ERR_PTR(err); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds return ifa; 10081da177e4SLinus Torvalds out: 10095c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10101da177e4SLinus Torvalds goto out2; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10135b84efecSThomas Haller enum cleanup_prefix_rt_t { 10145b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10155b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10165b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10175b84efecSThomas Haller }; 10185b84efecSThomas Haller 10195b84efecSThomas Haller /* 10205b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10215b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10225b84efecSThomas Haller * constants. 10235b84efecSThomas Haller * 10245b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 10255b84efecSThomas Haller * prefix is managed by its own lifetime. 10265b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 10275b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 10285b84efecSThomas Haller * 4) if there are still other permanent address(es), 10295b84efecSThomas Haller * corresponding prefix is still permanent. 10305b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 10315b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 10325b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 10335b84efecSThomas Haller * longest valid lifetime among the corresponding 10345b84efecSThomas Haller * addresses on the device. 10355b84efecSThomas Haller * Note: subsequent RA will update lifetime. 10365b84efecSThomas Haller **/ 10375b84efecSThomas Haller static enum cleanup_prefix_rt_t 10385b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 10395b84efecSThomas Haller { 10405b84efecSThomas Haller struct inet6_ifaddr *ifa; 10415b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 10425b84efecSThomas Haller unsigned long lifetime; 10435b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 10445b84efecSThomas Haller 10455b84efecSThomas Haller *expires = jiffies; 10465b84efecSThomas Haller 10475b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 10485b84efecSThomas Haller if (ifa == ifp) 10495b84efecSThomas Haller continue; 10505b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 10515b84efecSThomas Haller ifp->prefix_len)) 10525b84efecSThomas Haller continue; 10535b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 10545b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 10555b84efecSThomas Haller 10565b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 10575b84efecSThomas Haller 10585b84efecSThomas Haller spin_lock(&ifa->lock); 10595b84efecSThomas Haller 10605b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 10615b84efecSThomas Haller /* 10625b84efecSThomas Haller * Note: Because this address is 10635b84efecSThomas Haller * not permanent, lifetime < 10645b84efecSThomas Haller * LONG_MAX / HZ here. 10655b84efecSThomas Haller */ 10665b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 10675b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 10685b84efecSThomas Haller spin_unlock(&ifa->lock); 10695b84efecSThomas Haller } 10705b84efecSThomas Haller 10715b84efecSThomas Haller return action; 10725b84efecSThomas Haller } 10735b84efecSThomas Haller 10745b84efecSThomas Haller static void 10755b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 10765b84efecSThomas Haller { 10775b84efecSThomas Haller struct rt6_info *rt; 10785b84efecSThomas Haller 10795b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 10805b84efecSThomas Haller ifp->prefix_len, 10815b84efecSThomas Haller ifp->idev->dev, 10825b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 10835b84efecSThomas Haller if (rt) { 10845b84efecSThomas Haller if (del_rt) 10855b84efecSThomas Haller ip6_del_rt(rt); 10865b84efecSThomas Haller else { 10875b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 10885b84efecSThomas Haller rt6_set_expires(rt, expires); 10895b84efecSThomas Haller ip6_rt_put(rt); 10905b84efecSThomas Haller } 10915b84efecSThomas Haller } 10925b84efecSThomas Haller } 10935b84efecSThomas Haller 10945b84efecSThomas Haller 10951da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 10981da177e4SLinus Torvalds { 10994c5ff6a6SHerbert Xu int state; 11005b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 11015b84efecSThomas Haller unsigned long expires; 11021da177e4SLinus Torvalds 1103c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1104c15b1ccaSHannes Frederic Sowa 11058e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 11064c5ff6a6SHerbert Xu state = ifp->state; 1107e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 11088e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 11094c5ff6a6SHerbert Xu 11104c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 11114c5ff6a6SHerbert Xu goto out; 11121da177e4SLinus Torvalds 11135c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 11145c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11155c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11161da177e4SLinus Torvalds 11175b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11185d9efa7eSDavid S. Miller 11191da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1120372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11211da177e4SLinus Torvalds if (ifp->ifpub) { 11221da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11231da177e4SLinus Torvalds ifp->ifpub = NULL; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds __in6_ifa_put(ifp); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11285b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 11295b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 11305b84efecSThomas Haller 1131502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 11321da177e4SLinus Torvalds __in6_ifa_put(ifp); 1133502a2ffdSstephen hemminger 11345b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 11351da177e4SLinus Torvalds 1136c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1137b2238566SAndrey Vagin 11381da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 11391da177e4SLinus Torvalds 1140f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 11411da177e4SLinus Torvalds 11425b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 11435b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 11445b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 1147c3968a85SDaniel Walter /* clean up prefsrc entries */ 1148c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 11494c5ff6a6SHerbert Xu out: 11501da177e4SLinus Torvalds in6_ifa_put(ifp); 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 11561da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 115776f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1158eac55bf9SBenoit Boissinot unsigned long regen_advance; 11591da177e4SLinus Torvalds int tmp_plen; 11601da177e4SLinus Torvalds int ret = 0; 116195c385b4SNeil Horman u32 addr_flags; 116276f793e3SLorenzo Colitti unsigned long now = jiffies; 11631da177e4SLinus Torvalds 116453bd6749SJiri Pirko write_lock_bh(&idev->lock); 11651da177e4SLinus Torvalds if (ift) { 11661da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 11671da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 11681da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 11691da177e4SLinus Torvalds tmpaddr = &addr; 11701da177e4SLinus Torvalds } else { 11711da177e4SLinus Torvalds tmpaddr = NULL; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds retry: 11741da177e4SLinus Torvalds in6_dev_hold(idev); 11751da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 117653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1177f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 11781da177e4SLinus Torvalds in6_dev_put(idev); 11791da177e4SLinus Torvalds ret = -1; 11801da177e4SLinus Torvalds goto out; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 11831da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 11841da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 11851da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 118653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1187f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1188f3213831SJoe Perches __func__); 11891da177e4SLinus Torvalds in6_dev_put(idev); 11901da177e4SLinus Torvalds ret = -1; 11911da177e4SLinus Torvalds goto out; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds in6_ifa_hold(ifp); 11941da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1195eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 11961da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 119776f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 11981da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 11991da177e4SLinus Torvalds ifp->valid_lft, 12007a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 12011da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 12021da177e4SLinus Torvalds ifp->prefered_lft, 12037a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 1204784e2710SBen Hutchings idev->cnf.max_desync_factor); 12051da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 12061da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 12071da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12081da177e4SLinus Torvalds 1209eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1210eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 12111f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 121253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 121395c385b4SNeil Horman 1214eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1215eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1216eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1217eac55bf9SBenoit Boissinot * Preferred Lifetime. 1218ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1219ecab6701SHeiner Kallweit * temporary addresses being generated. 1220eac55bf9SBenoit Boissinot */ 1221ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1222ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1223eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1224eac55bf9SBenoit Boissinot in6_dev_put(idev); 1225eac55bf9SBenoit Boissinot ret = -1; 1226eac55bf9SBenoit Boissinot goto out; 1227eac55bf9SBenoit Boissinot } 1228eac55bf9SBenoit Boissinot 122995c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 123095c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 123195c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 123295c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 123395c385b4SNeil Horman 12344b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 12358a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 12364b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 12374b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 12381da177e4SLinus Torvalds in6_ifa_put(ifp); 12391da177e4SLinus Torvalds in6_dev_put(idev); 1240f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 12411da177e4SLinus Torvalds tmpaddr = &addr; 124253bd6749SJiri Pirko write_lock_bh(&idev->lock); 12431da177e4SLinus Torvalds goto retry; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12471da177e4SLinus Torvalds ift->ifpub = ifp; 124876f793e3SLorenzo Colitti ift->cstamp = now; 12491da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 12501da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12511da177e4SLinus Torvalds 1252cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 12531da177e4SLinus Torvalds in6_ifa_put(ift); 12541da177e4SLinus Torvalds in6_dev_put(idev); 12551da177e4SLinus Torvalds out: 12561da177e4SLinus Torvalds return ret; 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds /* 1260072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 12611da177e4SLinus Torvalds */ 1262a9b05723SYOSHIFUJI Hideaki enum { 1263a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1264a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1265a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1266a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1267a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1268a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1269a9b05723SYOSHIFUJI Hideaki #endif 1270a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1271a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1272a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1273a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1274a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 12757fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 12767fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 12777fd2561eSErik Kline #endif 1278a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1279072047e4SYOSHIFUJI Hideaki }; 1280072047e4SYOSHIFUJI Hideaki 1281a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1282a9b05723SYOSHIFUJI Hideaki int rule; 1283a9b05723SYOSHIFUJI Hideaki int addr_type; 1284a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1285a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1286a9b05723SYOSHIFUJI Hideaki int scopedist; 1287a9b05723SYOSHIFUJI Hideaki int matchlen; 1288a9b05723SYOSHIFUJI Hideaki }; 1289a9b05723SYOSHIFUJI Hideaki 1290a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 12919acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1292a9b05723SYOSHIFUJI Hideaki int ifindex; 1293a9b05723SYOSHIFUJI Hideaki int scope; 1294a9b05723SYOSHIFUJI Hideaki int label; 12957cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1296a9b05723SYOSHIFUJI Hideaki }; 1297072047e4SYOSHIFUJI Hideaki 1298b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 12991da177e4SLinus Torvalds { 130045bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1301072047e4SYOSHIFUJI Hideaki return 1; 1302072047e4SYOSHIFUJI Hideaki return 0; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13057fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 13067fd2561eSErik Kline { 13077fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13087fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 13097fd2561eSErik Kline #else 13107fd2561eSErik Kline return false; 13117fd2561eSErik Kline #endif 13127fd2561eSErik Kline } 13137fd2561eSErik Kline 13143de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 13153de23255SBenjamin Thery struct ipv6_saddr_score *score, 1316a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1317a9b05723SYOSHIFUJI Hideaki int i) 1318a9b05723SYOSHIFUJI Hideaki { 1319a9b05723SYOSHIFUJI Hideaki int ret; 1320a9b05723SYOSHIFUJI Hideaki 1321a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1322a9b05723SYOSHIFUJI Hideaki switch (i) { 1323a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1324a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1325a9b05723SYOSHIFUJI Hideaki break; 1326a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1327a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1328a9b05723SYOSHIFUJI Hideaki break; 1329a9b05723SYOSHIFUJI Hideaki default: 1330a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1331a9b05723SYOSHIFUJI Hideaki } 1332a9b05723SYOSHIFUJI Hideaki goto out; 1333a9b05723SYOSHIFUJI Hideaki } 1334a9b05723SYOSHIFUJI Hideaki 1335a9b05723SYOSHIFUJI Hideaki switch (i) { 1336a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1337a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1338a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1339a9b05723SYOSHIFUJI Hideaki break; 1340a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1341a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1342a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1343a9b05723SYOSHIFUJI Hideaki break; 1344a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1345a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1346a9b05723SYOSHIFUJI Hideaki * 1347a9b05723SYOSHIFUJI Hideaki * ret 1348a9b05723SYOSHIFUJI Hideaki * ^ 1349a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1350a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1351a9b05723SYOSHIFUJI Hideaki * | 1352a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1353a9b05723SYOSHIFUJI Hideaki * B-d | \ 1354a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1355db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1356a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1357a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1358a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1359a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1360a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1361a9b05723SYOSHIFUJI Hideaki * 1362a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1363a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1364a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1365a9b05723SYOSHIFUJI Hideaki */ 1366a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1367a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1368a9b05723SYOSHIFUJI Hideaki ret = -ret; 1369a9b05723SYOSHIFUJI Hideaki else 1370a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1371a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1372a9b05723SYOSHIFUJI Hideaki break; 1373a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 13747fd2561eSErik Kline { 1375a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 13767fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 13777fd2561eSErik Kline 13787fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 13797fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1380a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 13817fd2561eSErik Kline !(score->ifa->flags & avoid); 1382a9b05723SYOSHIFUJI Hideaki break; 13837fd2561eSErik Kline } 1384a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1385a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 13867cbca67cSYOSHIFUJI Hideaki { 1387a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 13887cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 13897cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1390a9b05723SYOSHIFUJI Hideaki break; 13917cbca67cSYOSHIFUJI Hideaki } 1392a9b05723SYOSHIFUJI Hideaki #endif 1393a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1394a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1395a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1396a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1397a9b05723SYOSHIFUJI Hideaki break; 1398a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1399a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 14003de23255SBenjamin Thery ret = ipv6_addr_label(net, 14013de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1402a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1403a9b05723SYOSHIFUJI Hideaki break; 1404a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 14057cbca67cSYOSHIFUJI Hideaki { 1406a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 140725985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1408a9b05723SYOSHIFUJI Hideaki */ 14097cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 14107cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 14117cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 14127cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1413a9b05723SYOSHIFUJI Hideaki break; 14147cbca67cSYOSHIFUJI Hideaki } 1415a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1416a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1417a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1418a9b05723SYOSHIFUJI Hideaki */ 1419a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1420a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1421a9b05723SYOSHIFUJI Hideaki break; 1422a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1423a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 142491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 142591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 142691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 142791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1428a9b05723SYOSHIFUJI Hideaki break; 14297fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14307fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 14317fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 14327fd2561eSErik Kline * preferred addresses. 14337fd2561eSErik Kline */ 14347fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 14357fd2561eSErik Kline break; 14367fd2561eSErik Kline #endif 1437a9b05723SYOSHIFUJI Hideaki default: 1438a9b05723SYOSHIFUJI Hideaki ret = 0; 1439a9b05723SYOSHIFUJI Hideaki } 1440a9b05723SYOSHIFUJI Hideaki 1441a9b05723SYOSHIFUJI Hideaki if (ret) 1442a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1443a9b05723SYOSHIFUJI Hideaki score->rule = i; 1444a9b05723SYOSHIFUJI Hideaki out: 1445a9b05723SYOSHIFUJI Hideaki return ret; 1446a9b05723SYOSHIFUJI Hideaki } 1447a9b05723SYOSHIFUJI Hideaki 1448c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 14499131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 14509131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1451c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1452c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 14531da177e4SLinus Torvalds { 1454c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1455072047e4SYOSHIFUJI Hideaki 14561da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1457502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1458a9b05723SYOSHIFUJI Hideaki int i; 1459072047e4SYOSHIFUJI Hideaki 1460a9b05723SYOSHIFUJI Hideaki /* 14616b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 14626b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 14636b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 146495c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 14656b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1466072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1467072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1468072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1469072047e4SYOSHIFUJI Hideaki */ 1470a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1471a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 14726b3ae80aSYOSHIFUJI Hideaki continue; 1473a9b05723SYOSHIFUJI Hideaki 1474a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1475a9b05723SYOSHIFUJI Hideaki 1476a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1477a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1478ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 14799131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 14801da177e4SLinus Torvalds continue; 1481072047e4SYOSHIFUJI Hideaki } 1482072047e4SYOSHIFUJI Hideaki 1483a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1484a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1485072047e4SYOSHIFUJI Hideaki 1486a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1487a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1488072047e4SYOSHIFUJI Hideaki 14899131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 14909131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1491072047e4SYOSHIFUJI Hideaki 1492a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1493a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1494a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1495a9b05723SYOSHIFUJI Hideaki /* 1496a9b05723SYOSHIFUJI Hideaki * special case: 1497a9b05723SYOSHIFUJI Hideaki * each remaining entry 1498a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1499a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1500a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1501a9b05723SYOSHIFUJI Hideaki * values. 1502072047e4SYOSHIFUJI Hideaki */ 15039131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1504072047e4SYOSHIFUJI Hideaki } 1505a9b05723SYOSHIFUJI Hideaki break; 1506a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1507a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1508a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 150999cd07a5SJuha-Matti Tapio 1510a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1511072047e4SYOSHIFUJI Hideaki 1512a0bffffcSIlpo Järvinen swap(hiscore, score); 1513c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1514a9b05723SYOSHIFUJI Hideaki 1515a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1516a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1517a9b05723SYOSHIFUJI Hideaki 1518a9b05723SYOSHIFUJI Hideaki break; 15191da177e4SLinus Torvalds } 1520a9b05723SYOSHIFUJI Hideaki } 1521a9b05723SYOSHIFUJI Hideaki } 15229131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 15231da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1524c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 15251da177e4SLinus Torvalds } 15269131f3deSYOSHIFUJI Hideaki/吉藤英明 1527afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1528afbac601SDavid Ahern const struct net_device *dst_dev, 1529afbac601SDavid Ahern const struct net_device *master, 1530afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1531afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1532afbac601SDavid Ahern int hiscore_idx) 1533afbac601SDavid Ahern { 1534afbac601SDavid Ahern struct inet6_dev *idev; 1535afbac601SDavid Ahern 1536afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1537afbac601SDavid Ahern if (idev) 1538afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1539afbac601SDavid Ahern scores, hiscore_idx); 1540afbac601SDavid Ahern 1541afbac601SDavid Ahern idev = __in6_dev_get(master); 1542afbac601SDavid Ahern if (idev) 1543afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1544afbac601SDavid Ahern scores, hiscore_idx); 1545afbac601SDavid Ahern 1546afbac601SDavid Ahern return hiscore_idx; 1547afbac601SDavid Ahern } 1548afbac601SDavid Ahern 15499131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 15509131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 15519131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 15529131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1553c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 15549131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 15559131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 15569131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 15579131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 15589131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1559c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 15609131f3deSYOSHIFUJI Hideaki/吉藤英明 15619131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 15629131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 15639131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 15649131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 15659131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 15669131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 15679131f3deSYOSHIFUJI Hideaki/吉藤英明 1568c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1569c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 15709131f3deSYOSHIFUJI Hideaki/吉藤英明 15719131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 15729131f3deSYOSHIFUJI Hideaki/吉藤英明 15739131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 15749131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 15759131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 15769131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15779131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 15789131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 15799131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 15809131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 15819131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15829131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 15839131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 15843985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 15853985e8a3SErik Kline * be the set of unicast addresses assigned to the 15863985e8a3SErik Kline * interface that will be used to send to the destination 15873985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 15889131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 15899131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 15909131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 15913985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 15923985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 15933985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 15949131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 15959131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15969131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15979131f3deSYOSHIFUJI Hideaki/吉藤英明 15989131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1599c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1600c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16019131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1602afbac601SDavid Ahern const struct net_device *master; 1603afbac601SDavid Ahern int master_idx = 0; 1604afbac601SDavid Ahern 1605afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1606afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1607afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1608afbac601SDavid Ahern */ 1609afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1610afbac601SDavid Ahern if (master) { 1611afbac601SDavid Ahern master_idx = master->ifindex; 1612afbac601SDavid Ahern 1613afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1614afbac601SDavid Ahern master, &dst, 1615afbac601SDavid Ahern scores, hiscore_idx); 1616afbac601SDavid Ahern 1617afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1618afbac601SDavid Ahern goto out; 1619afbac601SDavid Ahern } 1620afbac601SDavid Ahern 16219131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1622afbac601SDavid Ahern /* only consider addresses on devices in the 1623afbac601SDavid Ahern * same L3 domain 1624afbac601SDavid Ahern */ 1625afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1626afbac601SDavid Ahern continue; 16279131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 16289131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 16299131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1630c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16319131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16329131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1633afbac601SDavid Ahern 1634afbac601SDavid Ahern out: 16358814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16361da177e4SLinus Torvalds 1637c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1638a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1639072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 16401da177e4SLinus Torvalds 16414e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1642a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1643072047e4SYOSHIFUJI Hideaki return 0; 16441da177e4SLinus Torvalds } 16455e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 16461da177e4SLinus Torvalds 16478965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1648479840ffSJiri Pirko u32 banned_flags) 1649b7b1bfceSHannes Frederic Sowa { 1650b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1651b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1652b7b1bfceSHannes Frederic Sowa 1653602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1654602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1655602582caSHannes Frederic Sowa break; 1656b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1657b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1658b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1659b7b1bfceSHannes Frederic Sowa err = 0; 1660b7b1bfceSHannes Frederic Sowa break; 1661b7b1bfceSHannes Frederic Sowa } 1662b7b1bfceSHannes Frederic Sowa } 1663b7b1bfceSHannes Frederic Sowa return err; 1664b7b1bfceSHannes Frederic Sowa } 1665b7b1bfceSHannes Frederic Sowa 166695c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1667479840ffSJiri Pirko u32 banned_flags) 16681da177e4SLinus Torvalds { 16691da177e4SLinus Torvalds struct inet6_dev *idev; 16701da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 16711da177e4SLinus Torvalds 16728814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1673e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1674e21e8467SStephen Hemminger if (idev) { 16751da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1676b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 16771da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16781da177e4SLinus Torvalds } 16798814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16801da177e4SLinus Torvalds return err; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 16841da177e4SLinus Torvalds { 16851da177e4SLinus Torvalds int cnt = 0; 16861da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1689502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 16901da177e4SLinus Torvalds cnt++; 16911da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16921da177e4SLinus Torvalds return cnt; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 1695b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 16962a7851bfSFlorian Westphal const struct net_device *dev, int strict) 16971da177e4SLinus Torvalds { 1698c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1699c58da4c6SErik Kline } 1700c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1701c58da4c6SErik Kline 1702c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1703c58da4c6SErik Kline const struct net_device *dev, int strict, 1704c58da4c6SErik Kline u32 banned_flags) 1705c58da4c6SErik Kline { 1706eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1707ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1708c58da4c6SErik Kline u32 ifp_flags; 17091da177e4SLinus Torvalds 17105c578aedSstephen hemminger rcu_read_lock_bh(); 1711b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1712878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1713bfeade08SDaniel Lezcano continue; 1714c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1715c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1716c58da4c6SErik Kline */ 1717c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1718c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1719c58da4c6SErik Kline : ifp->flags; 17201da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1721c58da4c6SErik Kline !(ifp_flags&banned_flags) && 172263159f29SIan Morris (!dev || ifp->idev->dev == dev || 1723eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 17245c578aedSstephen hemminger rcu_read_unlock_bh(); 1725eedf042aSStephen Hemminger return 1; 1726eedf042aSStephen Hemminger } 1727eedf042aSStephen Hemminger } 17285c578aedSstephen hemminger 1729eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1730eedf042aSStephen Hemminger return 0; 17311da177e4SLinus Torvalds } 1732c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 17337159039aSYOSHIFUJI Hideaki 17343e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 173506bfe655SDaniel Lezcano struct net_device *dev) 17361da177e4SLinus Torvalds { 1737ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 17381da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 17391da177e4SLinus Torvalds 1740b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1741878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 174206bfe655SDaniel Lezcano continue; 17431da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 174463159f29SIan Morris if (!dev || ifp->idev->dev == dev) 17453e81c6daSDavid S. Miller return true; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds } 17483e81c6daSDavid S. Miller return false; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17517df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 17527df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 17537df37ff3SCatalin\(ux\) M. BOIE */ 17547df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 17557df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 17567df37ff3SCatalin\(ux\) M. BOIE { 17577df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 17587df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 17597df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 17607df37ff3SCatalin\(ux\) M. BOIE 17617df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 17627df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 17637df37ff3SCatalin\(ux\) M. BOIE if (idev) { 17647df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 17657df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 17667df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 17677df37ff3SCatalin\(ux\) M. BOIE if (ret) 17687df37ff3SCatalin\(ux\) M. BOIE break; 17697df37ff3SCatalin\(ux\) M. BOIE } 17707df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 17717df37ff3SCatalin\(ux\) M. BOIE } 17727df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 17737df37ff3SCatalin\(ux\) M. BOIE 17747df37ff3SCatalin\(ux\) M. BOIE return ret; 17757df37ff3SCatalin\(ux\) M. BOIE } 17767df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 17777df37ff3SCatalin\(ux\) M. BOIE 1778b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 177952eeeb84SYOSHIFUJI Hideaki { 178052eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 178152eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 178252eeeb84SYOSHIFUJI Hideaki int onlink; 178352eeeb84SYOSHIFUJI Hideaki 178452eeeb84SYOSHIFUJI Hideaki onlink = 0; 178552eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 178652eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 178752eeeb84SYOSHIFUJI Hideaki if (idev) { 178852eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1789502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 179052eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 179152eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 179252eeeb84SYOSHIFUJI Hideaki if (onlink) 179352eeeb84SYOSHIFUJI Hideaki break; 179452eeeb84SYOSHIFUJI Hideaki } 179552eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 179652eeeb84SYOSHIFUJI Hideaki } 179752eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 179852eeeb84SYOSHIFUJI Hideaki return onlink; 179952eeeb84SYOSHIFUJI Hideaki } 180052eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 180152eeeb84SYOSHIFUJI Hideaki 18029acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 18031cab3da6SDaniel Lezcano struct net_device *dev, int strict) 18041da177e4SLinus Torvalds { 1805b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1806ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 18071da177e4SLinus Torvalds 18085c578aedSstephen hemminger rcu_read_lock_bh(); 1809b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1810878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 18111cab3da6SDaniel Lezcano continue; 18121da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 181363159f29SIan Morris if (!dev || ifp->idev->dev == dev || 18141da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1815b79d1d54SDavid S. Miller result = ifp; 18161da177e4SLinus Torvalds in6_ifa_hold(ifp); 18171da177e4SLinus Torvalds break; 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds } 18215c578aedSstephen hemminger rcu_read_unlock_bh(); 18221da177e4SLinus Torvalds 1823b79d1d54SDavid S. Miller return result; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 18271da177e4SLinus Torvalds 1828cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 18291da177e4SLinus Torvalds { 18303d171f39SLubomir Rintel if (dad_failed) 18313d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 18323d171f39SLubomir Rintel 18331da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 18341da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1835c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 18361da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 18371da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1838e2577a06SHerbert Xu if (dad_failed) 1839e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 18401da177e4SLinus Torvalds in6_ifa_put(ifp); 18411da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 18421da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 18431da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 18441da177e4SLinus Torvalds ifpub = ifp->ifpub; 18451da177e4SLinus Torvalds if (ifpub) { 18461da177e4SLinus Torvalds in6_ifa_hold(ifpub); 18471da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18481da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 18491da177e4SLinus Torvalds in6_ifa_put(ifpub); 18501da177e4SLinus Torvalds } else { 18511da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds ipv6_del_addr(ifp); 1854c15b1ccaSHannes Frederic Sowa } else { 18551da177e4SLinus Torvalds ipv6_del_addr(ifp); 18561da177e4SLinus Torvalds } 1857c15b1ccaSHannes Frederic Sowa } 18581da177e4SLinus Torvalds 1859f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1860f2344a13SHerbert Xu { 1861f2344a13SHerbert Xu int err = -ENOENT; 1862f2344a13SHerbert Xu 18638e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1864f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1865f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1866f2344a13SHerbert Xu err = 0; 1867f2344a13SHerbert Xu } 18688e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1869f2344a13SHerbert Xu 1870f2344a13SHerbert Xu return err; 1871f2344a13SHerbert Xu } 1872f2344a13SHerbert Xu 18733c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 18743c21edbdSYOSHIFUJI Hideaki { 18755f40ef77SHannes Frederic Sowa struct in6_addr addr; 18761b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 18771855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 18789bdd8d40SBrian Haley 1879853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1880853dc2e0SUrsula Braun in6_ifa_put(ifp); 1881f2344a13SHerbert Xu return; 1882853dc2e0SUrsula Braun } 1883f2344a13SHerbert Xu 1884e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 18850522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 18869bdd8d40SBrian Haley 18875f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18881b34be74SYOSHIFUJI Hideaki 18895f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 18905f40ef77SHannes Frederic Sowa int scope = ifp->scope; 18915f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 18925f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 18935f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 18945f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 18955f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 18965f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 18975f40ef77SHannes Frederic Sowa 18981855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 18995f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 19005f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19015f40ef77SHannes Frederic Sowa goto errdad; 19025f40ef77SHannes Frederic Sowa } 19035f40ef77SHannes Frederic Sowa 19045f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 19055f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 19065f40ef77SHannes Frederic Sowa idev)) 19075f40ef77SHannes Frederic Sowa goto errdad; 19085f40ef77SHannes Frederic Sowa 19095f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 19105f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 19115f40ef77SHannes Frederic Sowa 19125f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 19135f40ef77SHannes Frederic Sowa 19145f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 19155f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 19165f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 19175f40ef77SHannes Frederic Sowa goto lock_errdad; 19185f40ef77SHannes Frederic Sowa 19195f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 19205f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19215f40ef77SHannes Frederic Sowa 19225f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 19235f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 19245f40ef77SHannes Frederic Sowa preferred_lft); 19255f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 19265f40ef77SHannes Frederic Sowa goto lock_errdad; 19275f40ef77SHannes Frederic Sowa 19285f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 19295f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 19305f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 19315f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 19325f40ef77SHannes Frederic Sowa 19331855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 19345f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 19355f40ef77SHannes Frederic Sowa lock_errdad: 19365f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 19375f40ef77SHannes Frederic Sowa } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 19381b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 19391b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 19401b34be74SYOSHIFUJI Hideaki 19411b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 19421b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 19431b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 19441b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 19459bdd8d40SBrian Haley 1946f3213831SJoe Perches pr_info("%s: IPv6 being disabled!\n", 19479bdd8d40SBrian Haley ifp->idev->dev->name); 19481b34be74SYOSHIFUJI Hideaki } 19491b34be74SYOSHIFUJI Hideaki } 19501b34be74SYOSHIFUJI Hideaki 19515f40ef77SHannes Frederic Sowa errdad: 1952c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 1953c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 19548e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1955c15b1ccaSHannes Frederic Sowa 1956c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 19573c21edbdSYOSHIFUJI Hideaki } 19581da177e4SLinus Torvalds 1959a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 1960a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 1961b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 19621da177e4SLinus Torvalds { 19631da177e4SLinus Torvalds struct in6_addr maddr; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19661da177e4SLinus Torvalds return; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19691da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds 1972a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 1973b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 19741da177e4SLinus Torvalds { 19751da177e4SLinus Torvalds struct in6_addr maddr; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19781da177e4SLinus Torvalds return; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19811da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds 1984a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 198520380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 19861da177e4SLinus Torvalds { 19871da177e4SLinus Torvalds struct in6_addr addr; 1988c15b1ccaSHannes Frederic Sowa 198988ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 19902bda8a0cSBjørn Mork return; 19911da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19921da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19931da177e4SLinus Torvalds return; 1994013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 1997a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 199820380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 19991da177e4SLinus Torvalds { 20001da177e4SLinus Torvalds struct in6_addr addr; 2001c15b1ccaSHannes Frederic Sowa 200288ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 200332019e65SYOSHIFUJI Hideaki return; 20041da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20051da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20061da177e4SLinus Torvalds return; 20071da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 201006a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 201106a4c1c5Salex.bluesman.smirnov@gmail.com { 20125241c2d7SAlexander Aring if (dev->addr_len != EUI64_ADDR_LEN) 201306a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 20145241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 20155e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 201606a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 201706a4c1c5Salex.bluesman.smirnov@gmail.com } 201806a4c1c5Salex.bluesman.smirnov@gmail.com 2019cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2020cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2021cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2022cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2023cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2024cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2025cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2026cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2027cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2028cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2029cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2030cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2031cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2032cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2033073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2034073a8e0eSYOSHIFUJI Hideaki { 20351da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 20361da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 20371da177e4SLinus Torvalds return -1; 20381da177e4SLinus Torvalds memset(eui, 0, 7); 20391da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 20401da177e4SLinus Torvalds return 0; 2041073a8e0eSYOSHIFUJI Hideaki } 2042073a8e0eSYOSHIFUJI Hideaki 2043073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2044073a8e0eSYOSHIFUJI Hideaki { 20451da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 20461da177e4SLinus Torvalds return -1; 20471da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 20481da177e4SLinus Torvalds eui[0] |= 2; 20491da177e4SLinus Torvalds return 0; 20501da177e4SLinus Torvalds } 2051073a8e0eSYOSHIFUJI Hideaki 2052c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2053dfd982baSYOSHIFUJI Hideaki { 20549af28511SSascha Hlusiak if (addr == 0) 20559af28511SSascha Hlusiak return -1; 2056dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2057dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2058dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2059dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2060dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2061dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2062dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2063dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2064dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2065dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2066dfd982baSYOSHIFUJI Hideaki return 0; 2067dfd982baSYOSHIFUJI Hideaki } 2068dfd982baSYOSHIFUJI Hideaki 2069dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2070dfd982baSYOSHIFUJI Hideaki { 2071dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2072dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2073dfd982baSYOSHIFUJI Hideaki return -1; 2074dfd982baSYOSHIFUJI Hideaki } 2075dfd982baSYOSHIFUJI Hideaki 2076aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2077aee80b54Sstephen hemminger { 2078aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2079aee80b54Sstephen hemminger } 2080aee80b54Sstephen hemminger 2081e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2082e837735eSNicolas Dichtel { 2083e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2084e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2085e837735eSNicolas Dichtel eui[3] = 0xFF; 2086e837735eSNicolas Dichtel eui[4] = 0xFE; 2087e837735eSNicolas Dichtel eui[0] ^= 2; 2088e837735eSNicolas Dichtel return 0; 2089e837735eSNicolas Dichtel } 2090e837735eSNicolas Dichtel 2091073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2092073a8e0eSYOSHIFUJI Hideaki { 2093073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2094073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2095073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2096073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2097073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2098073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2099073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2100073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2101c7dc89c0SFred L. Templin case ARPHRD_SIT: 2102dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2103aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2104aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2105e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 210606a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 2107cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2108cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2109e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 2110e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2111073a8e0eSYOSHIFUJI Hideaki } 21121da177e4SLinus Torvalds return -1; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 21161da177e4SLinus Torvalds { 21171da177e4SLinus Torvalds int err = -1; 21181da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2121602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2122602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2123602582caSHannes Frederic Sowa break; 21241da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 21251da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 21261da177e4SLinus Torvalds err = 0; 21271da177e4SLinus Torvalds break; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21311da177e4SLinus Torvalds return err; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 2135eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds regen: 2138955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 21391da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds /* 21421da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 21431da177e4SLinus Torvalds * check if generated address is not inappropriate 21441da177e4SLinus Torvalds * 21451da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 21461da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2147c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 21481da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 21491da177e4SLinus Torvalds * - value 0 21501da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 21511da177e4SLinus Torvalds */ 21521da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 21531da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 21541da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 21551da177e4SLinus Torvalds goto regen; 21561da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 21571da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 21581da177e4SLinus Torvalds goto regen; 21591da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 21601da177e4SLinus Torvalds goto regen; 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 21671da177e4SLinus Torvalds unsigned long expires; 21681da177e4SLinus Torvalds 21698814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 21701da177e4SLinus Torvalds write_lock_bh(&idev->lock); 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds if (idev->dead) 21731da177e4SLinus Torvalds goto out; 21741da177e4SLinus Torvalds 2175eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds expires = jiffies + 21781da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 21791f9248e5SJiri Pirko idev->cnf.regen_max_retry * idev->cnf.dad_transmits * 21801f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - 2181784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 21821da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 2183f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 2184f3213831SJoe Perches __func__, idev->dev->name); 21851da177e4SLinus Torvalds goto out; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 21891da177e4SLinus Torvalds in6_dev_hold(idev); 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds out: 21921da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 21938814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 21941da177e4SLinus Torvalds in6_dev_put(idev); 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds 2197eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 21988e5e8f30SEldad Zack { 21991da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 2200eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds /* 22041da177e4SLinus Torvalds * Add prefix route. 22051da177e4SLinus Torvalds */ 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds static void 22081da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2209e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 22101da177e4SLinus Torvalds { 221186872cb5SThomas Graf struct fib6_config cfg = { 2212ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 221386872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 221486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 221586872cb5SThomas Graf .fc_expires = expires, 221686872cb5SThomas Graf .fc_dst_len = plen, 221786872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2218c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2219f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 222086872cb5SThomas Graf }; 22211da177e4SLinus Torvalds 22224e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 22251da177e4SLinus Torvalds This thing is done here expecting that the whole 22261da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 22271da177e4SLinus Torvalds */ 222807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 22291da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 223086872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 22310be669bbSJoerg Roedel #endif 22321da177e4SLinus Torvalds 223386872cb5SThomas Graf ip6_route_add(&cfg); 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 223614ef37b6SAndreas Hofmeister 223714ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 223814ef37b6SAndreas Hofmeister int plen, 223914ef37b6SAndreas Hofmeister const struct net_device *dev, 224014ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 224114ef37b6SAndreas Hofmeister { 224214ef37b6SAndreas Hofmeister struct fib6_node *fn; 224314ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 224414ef37b6SAndreas Hofmeister struct fib6_table *table; 2245ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 224614ef37b6SAndreas Hofmeister 2247ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 224863159f29SIan Morris if (!table) 224914ef37b6SAndreas Hofmeister return NULL; 225014ef37b6SAndreas Hofmeister 22515744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 225214ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 225314ef37b6SAndreas Hofmeister if (!fn) 225414ef37b6SAndreas Hofmeister goto out; 22551f56a01fSMartin KaFai Lau 22561f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 225714ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2258d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 225914ef37b6SAndreas Hofmeister continue; 226014ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 226114ef37b6SAndreas Hofmeister continue; 226285da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 226314ef37b6SAndreas Hofmeister continue; 226414ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 226514ef37b6SAndreas Hofmeister break; 226614ef37b6SAndreas Hofmeister } 226714ef37b6SAndreas Hofmeister out: 22685744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 226914ef37b6SAndreas Hofmeister return rt; 227014ef37b6SAndreas Hofmeister } 227114ef37b6SAndreas Hofmeister 227214ef37b6SAndreas Hofmeister 22731da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 22761da177e4SLinus Torvalds { 227786872cb5SThomas Graf struct fib6_config cfg = { 2278ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 227986872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 228086872cb5SThomas Graf .fc_ifindex = dev->ifindex, 228186872cb5SThomas Graf .fc_dst_len = 8, 228286872cb5SThomas Graf .fc_flags = RTF_UP, 2283c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 228486872cb5SThomas Graf }; 22851da177e4SLinus Torvalds 228686872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 228786872cb5SThomas Graf 228886872cb5SThomas Graf ip6_route_add(&cfg); 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 22921da177e4SLinus Torvalds { 22931da177e4SLinus Torvalds struct inet6_dev *idev; 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds ASSERT_RTNL(); 22961da177e4SLinus Torvalds 2297e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2298e21e8467SStephen Hemminger if (!idev) 229964e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 230064e724f6SBrian Haley 230164e724f6SBrian Haley if (idev->cnf.disable_ipv6) 230264e724f6SBrian Haley return ERR_PTR(-EACCES); 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds /* Add default multicast route */ 2305ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 23061da177e4SLinus Torvalds addrconf_add_mroute(dev); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds return idev; 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds 231153bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 231253bd6749SJiri Pirko struct inet6_ifaddr *ifp, 231353bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 231453bd6749SJiri Pirko bool create, unsigned long now) 231553bd6749SJiri Pirko { 231653bd6749SJiri Pirko u32 flags; 231753bd6749SJiri Pirko struct inet6_ifaddr *ift; 231853bd6749SJiri Pirko 231953bd6749SJiri Pirko read_lock_bh(&idev->lock); 232053bd6749SJiri Pirko /* update all temporary addresses in the list */ 232153bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 232253bd6749SJiri Pirko int age, max_valid, max_prefered; 232353bd6749SJiri Pirko 232453bd6749SJiri Pirko if (ifp != ift->ifpub) 232553bd6749SJiri Pirko continue; 232653bd6749SJiri Pirko 232753bd6749SJiri Pirko /* RFC 4941 section 3.3: 232853bd6749SJiri Pirko * If a received option will extend the lifetime of a public 232953bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 233053bd6749SJiri Pirko * be extended, subject to the overall constraint that no 233153bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 233253bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 233353bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 233453bd6749SJiri Pirko */ 233553bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 233653bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 233753bd6749SJiri Pirko if (max_valid < 0) 233853bd6749SJiri Pirko max_valid = 0; 233953bd6749SJiri Pirko 234053bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 234153bd6749SJiri Pirko idev->cnf.max_desync_factor - age; 234253bd6749SJiri Pirko if (max_prefered < 0) 234353bd6749SJiri Pirko max_prefered = 0; 234453bd6749SJiri Pirko 234553bd6749SJiri Pirko if (valid_lft > max_valid) 234653bd6749SJiri Pirko valid_lft = max_valid; 234753bd6749SJiri Pirko 234853bd6749SJiri Pirko if (prefered_lft > max_prefered) 234953bd6749SJiri Pirko prefered_lft = max_prefered; 235053bd6749SJiri Pirko 235153bd6749SJiri Pirko spin_lock(&ift->lock); 235253bd6749SJiri Pirko flags = ift->flags; 235353bd6749SJiri Pirko ift->valid_lft = valid_lft; 235453bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 235553bd6749SJiri Pirko ift->tstamp = now; 235653bd6749SJiri Pirko if (prefered_lft > 0) 235753bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 235853bd6749SJiri Pirko 235953bd6749SJiri Pirko spin_unlock(&ift->lock); 236053bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 236153bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 236253bd6749SJiri Pirko } 236353bd6749SJiri Pirko 236453bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 236553bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 236653bd6749SJiri Pirko /* When a new public address is created as described 236753bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 236853bd6749SJiri Pirko * Also create a temporary address if it's enabled but 236953bd6749SJiri Pirko * no temporary address currently exists. 237053bd6749SJiri Pirko */ 237153bd6749SJiri Pirko read_unlock_bh(&idev->lock); 237253bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 237353bd6749SJiri Pirko } else { 237453bd6749SJiri Pirko read_unlock_bh(&idev->lock); 237553bd6749SJiri Pirko } 237653bd6749SJiri Pirko } 237753bd6749SJiri Pirko 2378cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2379cc9da6ccSBjørn Mork { 2380cc9da6ccSBjørn Mork return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2381cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2382cc9da6ccSBjørn Mork } 2383cc9da6ccSBjørn Mork 2384cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 23854f672235SAlexander Aring const struct prefix_info *pinfo, 23864f672235SAlexander Aring struct inet6_dev *in6_dev, 2387cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2388cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 23894f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 23904f672235SAlexander Aring { 23914f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 23924f672235SAlexander Aring int create = 0, update_lft = 0; 23934f672235SAlexander Aring 23944f672235SAlexander Aring if (!ifp && valid_lft) { 23954f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 23964f672235SAlexander Aring 23974f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 23984f672235SAlexander Aring if (in6_dev->cnf.optimistic_dad && 23994f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 24004f672235SAlexander Aring addr_flags |= IFA_F_OPTIMISTIC; 24014f672235SAlexander Aring #endif 24024f672235SAlexander Aring 24034f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 24044f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 24054f672235SAlexander Aring */ 24064f672235SAlexander Aring if (!max_addresses || 24074f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 24084f672235SAlexander Aring ifp = ipv6_add_addr(in6_dev, addr, NULL, 24094f672235SAlexander Aring pinfo->prefix_len, 24104f672235SAlexander Aring addr_type&IPV6_ADDR_SCOPE_MASK, 24114f672235SAlexander Aring addr_flags, valid_lft, 24124f672235SAlexander Aring prefered_lft); 24134f672235SAlexander Aring 24144f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 24154f672235SAlexander Aring return -1; 24164f672235SAlexander Aring 24174f672235SAlexander Aring update_lft = 0; 24184f672235SAlexander Aring create = 1; 24194f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24204f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 24214f672235SAlexander Aring ifp->cstamp = jiffies; 24224f672235SAlexander Aring ifp->tokenized = tokenized; 24234f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24244f672235SAlexander Aring addrconf_dad_start(ifp); 24254f672235SAlexander Aring } 24264f672235SAlexander Aring 24274f672235SAlexander Aring if (ifp) { 24284f672235SAlexander Aring u32 flags; 24294f672235SAlexander Aring unsigned long now; 24304f672235SAlexander Aring u32 stored_lft; 24314f672235SAlexander Aring 24324f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 24334f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24344f672235SAlexander Aring now = jiffies; 24354f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 24364f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 24374f672235SAlexander Aring else 24384f672235SAlexander Aring stored_lft = 0; 24394f672235SAlexander Aring if (!update_lft && !create && stored_lft) { 24404f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 24414f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 24424f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 24434f672235SAlexander Aring 24444f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 24454f672235SAlexander Aring * "Note that the preferred lifetime of the 24464f672235SAlexander Aring * corresponding address is always reset to 24474f672235SAlexander Aring * the Preferred Lifetime in the received 24484f672235SAlexander Aring * Prefix Information option, regardless of 24494f672235SAlexander Aring * whether the valid lifetime is also reset or 24504f672235SAlexander Aring * ignored." 24514f672235SAlexander Aring * 24524f672235SAlexander Aring * So we should always update prefered_lft here. 24534f672235SAlexander Aring */ 24544f672235SAlexander Aring update_lft = 1; 24554f672235SAlexander Aring } 24564f672235SAlexander Aring 24574f672235SAlexander Aring if (update_lft) { 24584f672235SAlexander Aring ifp->valid_lft = valid_lft; 24594f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 24604f672235SAlexander Aring ifp->tstamp = now; 24614f672235SAlexander Aring flags = ifp->flags; 24624f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 24634f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24644f672235SAlexander Aring 24654f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 24664f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 24674f672235SAlexander Aring } else 24684f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24694f672235SAlexander Aring 24704f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 24714f672235SAlexander Aring create, now); 24724f672235SAlexander Aring 24734f672235SAlexander Aring in6_ifa_put(ifp); 24744f672235SAlexander Aring addrconf_verify(); 24754f672235SAlexander Aring } 24764f672235SAlexander Aring 24774f672235SAlexander Aring return 0; 24784f672235SAlexander Aring } 2479cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 24804f672235SAlexander Aring 2481e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 24821da177e4SLinus Torvalds { 24831da177e4SLinus Torvalds struct prefix_info *pinfo; 24841da177e4SLinus Torvalds __u32 valid_lft; 24851da177e4SLinus Torvalds __u32 prefered_lft; 24864f672235SAlexander Aring int addr_type, err; 248764236f3fSHannes Frederic Sowa u32 addr_flags = 0; 24881da177e4SLinus Torvalds struct inet6_dev *in6_dev; 248956d417b1SBrian Haley struct net *net = dev_net(dev); 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2494ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 24951da177e4SLinus Torvalds return; 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds /* 24991da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 25001da177e4SLinus Torvalds */ 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 25051da177e4SLinus Torvalds return; 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 25081da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2511e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 25121da177e4SLinus Torvalds return; 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 25161da177e4SLinus Torvalds 251763159f29SIan Morris if (!in6_dev) { 2518e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2519e87cc472SJoe Perches dev->name); 25201da177e4SLinus Torvalds return; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds /* 25241da177e4SLinus Torvalds * Two things going on here: 25251da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 25261da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 25271da177e4SLinus Torvalds */ 25281da177e4SLinus Torvalds 25294bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 25304bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 25314bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 25324bed72e4SYOSHIFUJI Hideaki 25331da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 25346f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 25356f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 25366f704992SYOSHIFUJI Hideaki * not good. 25371da177e4SLinus Torvalds */ 25384bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 25394bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 25404bed72e4SYOSHIFUJI Hideaki else 25414bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 25423dd4bc68SYOSHIFUJI Hideaki 25434bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 25444bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 25451da177e4SLinus Torvalds 254614ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 254714ef37b6SAndreas Hofmeister pinfo->prefix_len, 254814ef37b6SAndreas Hofmeister dev, 254914ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 255014ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 25511da177e4SLinus Torvalds 255214ef37b6SAndreas Hofmeister if (rt) { 25536f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 25541da177e4SLinus Torvalds if (valid_lft == 0) { 2555e0a1ad73SThomas Graf ip6_del_rt(rt); 25561da177e4SLinus Torvalds rt = NULL; 25574bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 25586f704992SYOSHIFUJI Hideaki /* not infinity */ 25591716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 25606f704992SYOSHIFUJI Hideaki } else { 25611716a961SGao feng rt6_clean_expires(rt); 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds } else if (valid_lft) { 25646f704992SYOSHIFUJI Hideaki clock_t expires = 0; 25654bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 25664bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 25676f704992SYOSHIFUJI Hideaki /* not infinity */ 25686f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 25696f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 25706f704992SYOSHIFUJI Hideaki } 25711da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 25726f704992SYOSHIFUJI Hideaki dev, expires, flags); 25731da177e4SLinus Torvalds } 257494e187c0SAmerigo Wang ip6_rt_put(rt); 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 25801da177e4SLinus Torvalds struct in6_addr addr; 2581f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 25841da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2585f53adae4SDaniel Borkmann 2586f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2587f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2588f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2589f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2590f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2591617fe29dSDaniel Borkmann tokenized = true; 2592cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2593622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2594622c81d5SHannes Frederic Sowa in6_dev)) { 259564236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2596622c81d5SHannes Frederic Sowa goto ok; 2597f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 25981da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 25994f672235SAlexander Aring goto put; 2600f997c55cSAlexander Aring } else { 2601f997c55cSAlexander Aring dev_addr_generated = true; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds goto ok; 26041da177e4SLinus Torvalds } 2605e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 26061da177e4SLinus Torvalds pinfo->prefix_len); 26074f672235SAlexander Aring goto put; 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds ok: 26104f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 26114f672235SAlexander Aring &addr, addr_type, 26124f672235SAlexander Aring addr_flags, sllao, 26134f672235SAlexander Aring tokenized, valid_lft, 26148a226b2cSJiri Benc prefered_lft); 26154f672235SAlexander Aring if (err) 26164f672235SAlexander Aring goto put; 2617f997c55cSAlexander Aring 2618f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2619f997c55cSAlexander Aring * successful which will be notified. 2620f997c55cSAlexander Aring */ 2621f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2622f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2623f997c55cSAlexander Aring tokenized, valid_lft, 2624f997c55cSAlexander Aring prefered_lft, 2625f997c55cSAlexander Aring dev_addr_generated); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 26284f672235SAlexander Aring put: 26291da177e4SLinus Torvalds in6_dev_put(in6_dev); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds /* 26331da177e4SLinus Torvalds * Set destination address. 26341da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 26351da177e4SLinus Torvalds * device. 26361da177e4SLinus Torvalds */ 2637af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 26381da177e4SLinus Torvalds { 26391da177e4SLinus Torvalds struct in6_ifreq ireq; 26401da177e4SLinus Torvalds struct net_device *dev; 26411da177e4SLinus Torvalds int err = -EINVAL; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds rtnl_lock(); 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds err = -EFAULT; 26461da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 26471da177e4SLinus Torvalds goto err_exit; 26481da177e4SLinus Torvalds 2649af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds err = -ENODEV; 265263159f29SIan Morris if (!dev) 26531da177e4SLinus Torvalds goto err_exit; 26541da177e4SLinus Torvalds 265507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 26561da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 26575bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26581da177e4SLinus Torvalds struct ifreq ifr; 26591da177e4SLinus Torvalds struct ip_tunnel_parm p; 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 26621da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 26631da177e4SLinus Torvalds goto err_exit; 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 26661da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 26671da177e4SLinus Torvalds p.iph.saddr = 0; 26681da177e4SLinus Torvalds p.iph.version = 4; 26691da177e4SLinus Torvalds p.iph.ihl = 5; 26701da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 26711da177e4SLinus Torvalds p.iph.ttl = 64; 2672d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 26731da177e4SLinus Torvalds 26745bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 26755bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 26765bc3eb7eSStephen Hemminger 26775bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 26785bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 26791da177e4SLinus Torvalds set_fs(oldfs); 26805bc3eb7eSStephen Hemminger } else 26815bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds if (err == 0) { 26841da177e4SLinus Torvalds err = -ENOBUFS; 2685af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2686af284937SDaniel Lezcano if (!dev) 26871da177e4SLinus Torvalds goto err_exit; 26881da177e4SLinus Torvalds err = dev_open(dev); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds } 26910be669bbSJoerg Roedel #endif 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds err_exit: 26941da177e4SLinus Torvalds rtnl_unlock(); 26951da177e4SLinus Torvalds return err; 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 269893a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 269993a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 270093a714d6SMadhu Challa { 270193a714d6SMadhu Challa int ret; 270293a714d6SMadhu Challa 270393a714d6SMadhu Challa ASSERT_RTNL(); 270493a714d6SMadhu Challa 270593a714d6SMadhu Challa lock_sock(sk); 270693a714d6SMadhu Challa if (join) 270754ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 270893a714d6SMadhu Challa else 270954ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 271093a714d6SMadhu Challa release_sock(sk); 271193a714d6SMadhu Challa 271293a714d6SMadhu Challa return ret; 271393a714d6SMadhu Challa } 271493a714d6SMadhu Challa 27151da177e4SLinus Torvalds /* 27161da177e4SLinus Torvalds * Manual configuration of address on an interface 27171da177e4SLinus Torvalds */ 2718479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2719479840ffSJiri Pirko const struct in6_addr *pfx, 2720caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2721479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2722479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 27231da177e4SLinus Torvalds { 27241da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27251da177e4SLinus Torvalds struct inet6_dev *idev; 27261da177e4SLinus Torvalds struct net_device *dev; 272793a714d6SMadhu Challa unsigned long timeout; 272893a714d6SMadhu Challa clock_t expires; 27291da177e4SLinus Torvalds int scope; 27306f704992SYOSHIFUJI Hideaki u32 flags; 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds ASSERT_RTNL(); 27331da177e4SLinus Torvalds 273424ef0da7SThomas Graf if (plen > 128) 273524ef0da7SThomas Graf return -EINVAL; 273624ef0da7SThomas Graf 27370778769dSNoriaki TAKAMIYA /* check the lifetime */ 27380778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 27390778769dSNoriaki TAKAMIYA return -EINVAL; 27400778769dSNoriaki TAKAMIYA 274153bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 274253bd6749SJiri Pirko return -EINVAL; 274353bd6749SJiri Pirko 2744af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2745af284937SDaniel Lezcano if (!dev) 27461da177e4SLinus Torvalds return -ENODEV; 27471da177e4SLinus Torvalds 274864e724f6SBrian Haley idev = addrconf_add_dev(dev); 274964e724f6SBrian Haley if (IS_ERR(idev)) 275064e724f6SBrian Haley return PTR_ERR(idev); 27511da177e4SLinus Torvalds 275293a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 275393a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 275493a714d6SMadhu Challa true, pfx, ifindex); 275593a714d6SMadhu Challa 275693a714d6SMadhu Challa if (ret < 0) 275793a714d6SMadhu Challa return ret; 275893a714d6SMadhu Challa } 275993a714d6SMadhu Challa 27601da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 27611da177e4SLinus Torvalds 27624bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 27634bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27644bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 27654bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 27666f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 27674bed72e4SYOSHIFUJI Hideaki } else { 27684bed72e4SYOSHIFUJI Hideaki expires = 0; 27694bed72e4SYOSHIFUJI Hideaki flags = 0; 27704bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 27716f704992SYOSHIFUJI Hideaki } 27720778769dSNoriaki TAKAMIYA 27734bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 27744bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27754bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 27760778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 27774bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 27784bed72e4SYOSHIFUJI Hideaki } 27790778769dSNoriaki TAKAMIYA 27808a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 27818a226b2cSJiri Benc valid_lft, prefered_lft); 27820778769dSNoriaki TAKAMIYA 27831da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2784761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 278546d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 27866f704992SYOSHIFUJI Hideaki expires, flags); 2787761aac73SThomas Haller } 2788761aac73SThomas Haller 278995c385b4SNeil Horman /* 279095c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 279195c385b4SNeil Horman * that the Optimistic flag should not be set for 279295c385b4SNeil Horman * manually configured addresses 279395c385b4SNeil Horman */ 2794cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 279553bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 279653bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 279753bd6749SJiri Pirko true, jiffies); 27981da177e4SLinus Torvalds in6_ifa_put(ifp); 2799c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 28001da177e4SLinus Torvalds return 0; 280193a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 280293a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 280393a714d6SMadhu Challa false, pfx, ifindex); 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds return PTR_ERR(ifp); 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds 28096046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 28106046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28131da177e4SLinus Torvalds struct inet6_dev *idev; 28141da177e4SLinus Torvalds struct net_device *dev; 28151da177e4SLinus Torvalds 281624ef0da7SThomas Graf if (plen > 128) 281724ef0da7SThomas Graf return -EINVAL; 281824ef0da7SThomas Graf 2819af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2820af284937SDaniel Lezcano if (!dev) 28211da177e4SLinus Torvalds return -ENODEV; 28221da177e4SLinus Torvalds 2823e5d08d71SIan Morris idev = __in6_dev_get(dev); 282463159f29SIan Morris if (!idev) 28251da177e4SLinus Torvalds return -ENXIO; 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2828502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 28291da177e4SLinus Torvalds if (ifp->prefix_len == plen && 28301da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 28311da177e4SLinus Torvalds in6_ifa_hold(ifp); 28321da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28331da177e4SLinus Torvalds 28346046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 28356046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 28366046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 28376046d5b4SHeiner Kallweit jiffies); 28381da177e4SLinus Torvalds ipv6_del_addr(ifp); 28396046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 284093a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 284193a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 284293a714d6SMadhu Challa false, pfx, dev->ifindex); 284393a714d6SMadhu Challa } 28441da177e4SLinus Torvalds return 0; 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28481da177e4SLinus Torvalds return -EADDRNOTAVAIL; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds 2852af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 28531da177e4SLinus Torvalds { 28541da177e4SLinus Torvalds struct in6_ifreq ireq; 28551da177e4SLinus Torvalds int err; 28561da177e4SLinus Torvalds 2857af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28581da177e4SLinus Torvalds return -EPERM; 28591da177e4SLinus Torvalds 28601da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28611da177e4SLinus Torvalds return -EFAULT; 28621da177e4SLinus Torvalds 28631da177e4SLinus Torvalds rtnl_lock(); 2864caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2865af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2866af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 28671da177e4SLinus Torvalds rtnl_unlock(); 28681da177e4SLinus Torvalds return err; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 2871af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds struct in6_ifreq ireq; 28741da177e4SLinus Torvalds int err; 28751da177e4SLinus Torvalds 2876af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28771da177e4SLinus Torvalds return -EPERM; 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28801da177e4SLinus Torvalds return -EFAULT; 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds rtnl_lock(); 28836046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2884af284937SDaniel Lezcano ireq.ifr6_prefixlen); 28851da177e4SLinus Torvalds rtnl_unlock(); 28861da177e4SLinus Torvalds return err; 28871da177e4SLinus Torvalds } 28881da177e4SLinus Torvalds 2889b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2890b5f348e5SIlpo Järvinen int plen, int scope) 2891b5f348e5SIlpo Järvinen { 2892b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2893b5f348e5SIlpo Järvinen 28948a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 289507edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 289607edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2897b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2898b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2899b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2900b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2901b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2902b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2903b5f348e5SIlpo Järvinen } 2904b5f348e5SIlpo Järvinen } 2905b5f348e5SIlpo Järvinen 290607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 29071da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 29081da177e4SLinus Torvalds { 29091da177e4SLinus Torvalds struct in6_addr addr; 29101da177e4SLinus Torvalds struct net_device *dev; 2911c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2912929c9cf3SNicolas Dichtel int scope, plen; 2913f0e2acfaSNicolas Dichtel u32 pflags = 0; 29141da177e4SLinus Torvalds 29151da177e4SLinus Torvalds ASSERT_RTNL(); 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 29181da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 29211da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 29221da177e4SLinus Torvalds scope = IFA_LINK; 2923929c9cf3SNicolas Dichtel plen = 64; 29241da177e4SLinus Torvalds } else { 29251da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2926929c9cf3SNicolas Dichtel plen = 96; 2927f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2931929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2932f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 29331da177e4SLinus Torvalds return; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 29366fda7350SBenjamin Thery for_each_netdev(net, dev) { 2937e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 29381da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 29391da177e4SLinus Torvalds struct in_ifaddr *ifa; 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds int flag = scope; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 29441da177e4SLinus Torvalds 29451da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 29481da177e4SLinus Torvalds continue; 29491da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 29501da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 29511da177e4SLinus Torvalds continue; 29521da177e4SLinus Torvalds flag |= IFA_HOST; 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds 2955b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 2956f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 2957f0e2acfaSNicolas Dichtel pflags); 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds } 29620be669bbSJoerg Roedel #endif 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 29651da177e4SLinus Torvalds { 29661da177e4SLinus Torvalds struct inet6_dev *idev; 296725fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 296825fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 296925fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds /* ::1 */ 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds ASSERT_RTNL(); 29741da177e4SLinus Torvalds 2975e5d08d71SIan Morris idev = ipv6_find_idev(dev); 297663159f29SIan Morris if (!idev) { 297791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 29781da177e4SLinus Torvalds return; 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 2981b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 298225fb6ca4SBalakumaran Kannan 298325fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 298425fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 298525fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 298625fb6ca4SBalakumaran Kannan continue; 298725fb6ca4SBalakumaran Kannan 298825fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 298925fb6ca4SBalakumaran Kannan if (!idev) 299025fb6ca4SBalakumaran Kannan continue; 299125fb6ca4SBalakumaran Kannan 299225fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 299325fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 299425fb6ca4SBalakumaran Kannan 299525fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 299625fb6ca4SBalakumaran Kannan continue; 299725fb6ca4SBalakumaran Kannan 299833d99113SGao feng if (sp_ifa->rt) { 299933d99113SGao feng /* This dst has been added to garbage list when 300033d99113SGao feng * lo device down, release this obsolete dst and 300133d99113SGao feng * reallocate a new router for ifa. 300233d99113SGao feng */ 300333d99113SGao feng if (sp_ifa->rt->dst.obsolete > 0) { 300433d99113SGao feng ip6_rt_put(sp_ifa->rt); 300533d99113SGao feng sp_ifa->rt = NULL; 300633d99113SGao feng } else { 3007a881ae1fSGao feng continue; 300833d99113SGao feng } 300933d99113SGao feng } 3010a881ae1fSGao feng 301157ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 301225fb6ca4SBalakumaran Kannan 301325fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 3014534c8779SGao feng if (!IS_ERR(sp_rt)) { 3015534c8779SGao feng sp_ifa->rt = sp_rt; 301625fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 301725fb6ca4SBalakumaran Kannan } 3018534c8779SGao feng } 301925fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 302025fb6ca4SBalakumaran Kannan } 30211da177e4SLinus Torvalds } 30221da177e4SLinus Torvalds 30232ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 302464236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 30251da177e4SLinus Torvalds { 30261da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 302764236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 30281da177e4SLinus Torvalds 302995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 303095c385b4SNeil Horman if (idev->cnf.optimistic_dad && 3031702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 303295c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 303395c385b4SNeil Horman #endif 303495c385b4SNeil Horman 303507edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 303607edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 30371da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 303846d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 3039cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 30401da177e4SLinus Torvalds in6_ifa_put(ifp); 30411da177e4SLinus Torvalds } 30421da177e4SLinus Torvalds } 30432ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 30441da177e4SLinus Torvalds 3045622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3046622c81d5SHannes Frederic Sowa { 3047622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3048622c81d5SHannes Frederic Sowa return true; 3049622c81d5SHannes Frederic Sowa 3050622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3051622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3052622c81d5SHannes Frederic Sowa return true; 3053622c81d5SHannes Frederic Sowa 3054622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3055622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3056622c81d5SHannes Frederic Sowa return true; 3057622c81d5SHannes Frederic Sowa 3058622c81d5SHannes Frederic Sowa return false; 3059622c81d5SHannes Frederic Sowa } 3060622c81d5SHannes Frederic Sowa 3061622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3062622c81d5SHannes Frederic Sowa u8 dad_count, 3063622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3064622c81d5SHannes Frederic Sowa { 3065622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3066622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3067622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3068622c81d5SHannes Frederic Sowa 3069622c81d5SHannes Frederic Sowa static union { 3070622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3071622c81d5SHannes Frederic Sowa struct { 3072622c81d5SHannes Frederic Sowa struct in6_addr secret; 3073ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3074622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3075622c81d5SHannes Frederic Sowa u8 dad_count; 3076622c81d5SHannes Frederic Sowa } __packed; 3077622c81d5SHannes Frederic Sowa } data; 3078622c81d5SHannes Frederic Sowa 3079622c81d5SHannes Frederic Sowa struct in6_addr secret; 3080622c81d5SHannes Frederic Sowa struct in6_addr temp; 3081622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3082622c81d5SHannes Frederic Sowa 3083622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3084622c81d5SHannes Frederic Sowa 3085622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3086622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3087622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3088622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3089622c81d5SHannes Frederic Sowa else 3090622c81d5SHannes Frederic Sowa return -1; 3091622c81d5SHannes Frederic Sowa 3092622c81d5SHannes Frederic Sowa retry: 3093622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3094622c81d5SHannes Frederic Sowa 3095622c81d5SHannes Frederic Sowa sha_init(digest); 3096622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3097622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3098622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3099ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3100ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3101622c81d5SHannes Frederic Sowa data.secret = secret; 3102622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3103622c81d5SHannes Frederic Sowa 3104622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3105622c81d5SHannes Frederic Sowa 3106622c81d5SHannes Frederic Sowa temp = *address; 3107ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3108ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3109622c81d5SHannes Frederic Sowa 3110622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3111622c81d5SHannes Frederic Sowa 3112622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3113622c81d5SHannes Frederic Sowa dad_count++; 31141855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3115622c81d5SHannes Frederic Sowa return -1; 3116622c81d5SHannes Frederic Sowa goto retry; 3117622c81d5SHannes Frederic Sowa } 3118622c81d5SHannes Frederic Sowa 3119622c81d5SHannes Frederic Sowa *address = temp; 3120622c81d5SHannes Frederic Sowa return 0; 3121622c81d5SHannes Frederic Sowa } 3122622c81d5SHannes Frederic Sowa 3123cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3124cc9da6ccSBjørn Mork { 3125cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3126cc9da6ccSBjørn Mork 3127cc9da6ccSBjørn Mork if (s->initialized) 3128cc9da6ccSBjørn Mork return; 3129cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3130cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3131cc9da6ccSBjørn Mork s->initialized = true; 3132cc9da6ccSBjørn Mork } 3133cc9da6ccSBjørn Mork 3134bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3135bc91b0f0SJiri Pirko { 3136bc91b0f0SJiri Pirko struct in6_addr addr; 3137bc91b0f0SJiri Pirko 3138ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3139ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3140ca254490SDavid Ahern return; 3141ca254490SDavid Ahern 3142bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3143622c81d5SHannes Frederic Sowa 3144cc9da6ccSBjørn Mork switch (idev->addr_gen_mode) { 3145cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3146cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3147cc9da6ccSBjørn Mork /* fallthrough */ 3148cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3149622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 315064236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 315164236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3152622c81d5SHannes Frederic Sowa else if (prefix_route) 3153622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3154cc9da6ccSBjørn Mork break; 3155cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3156bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3157bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3158bc91b0f0SJiri Pirko * couldn't generate one. 3159bc91b0f0SJiri Pirko */ 3160bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 316164236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3162bc91b0f0SJiri Pirko else if (prefix_route) 3163bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3164cc9da6ccSBjørn Mork break; 3165cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3166cc9da6ccSBjørn Mork default: 3167cc9da6ccSBjørn Mork /* will not add any link local address */ 3168cc9da6ccSBjørn Mork break; 3169bc91b0f0SJiri Pirko } 3170bc91b0f0SJiri Pirko } 3171bc91b0f0SJiri Pirko 31721da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 31731da177e4SLinus Torvalds { 31741da177e4SLinus Torvalds struct inet6_dev *idev; 31751da177e4SLinus Torvalds 31761da177e4SLinus Torvalds ASSERT_RTNL(); 31771da177e4SLinus Torvalds 317874235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 317974235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 318074235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 318106a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3182e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3183e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3184cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 3185cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 318674235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 318774235a25SHerbert Xu return; 318874235a25SHerbert Xu } 318974235a25SHerbert Xu 31901da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 319164e724f6SBrian Haley if (IS_ERR(idev)) 31921da177e4SLinus Torvalds return; 31931da177e4SLinus Torvalds 3194cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3195cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3196cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3197cc9da6ccSBjørn Mork idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3198cc9da6ccSBjørn Mork 3199bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 320207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 32031da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 32041da177e4SLinus Torvalds { 32051da177e4SLinus Torvalds struct inet6_dev *idev; 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds ASSERT_RTNL(); 32081da177e4SLinus Torvalds 32091da177e4SLinus Torvalds /* 32101da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 32111da177e4SLinus Torvalds * addresses... we should configure all of 32121da177e4SLinus Torvalds * our v4 addrs in the tunnel 32131da177e4SLinus Torvalds */ 32141da177e4SLinus Torvalds 3215e5d08d71SIan Morris idev = ipv6_find_idev(dev); 321663159f29SIan Morris if (!idev) { 321791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 32181da177e4SLinus Torvalds return; 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds 3221c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3222bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3223c7dc89c0SFred L. Templin return; 3224c7dc89c0SFred L. Templin } 3225c7dc89c0SFred L. Templin 32261da177e4SLinus Torvalds sit_add_v4_addrs(idev); 32271da177e4SLinus Torvalds 322862b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 32291da177e4SLinus Torvalds addrconf_add_mroute(dev); 32301da177e4SLinus Torvalds } 32310be669bbSJoerg Roedel #endif 32321da177e4SLinus Torvalds 323307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3234aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3235aee80b54Sstephen hemminger { 3236aee80b54Sstephen hemminger struct inet6_dev *idev; 3237aee80b54Sstephen hemminger 3238aee80b54Sstephen hemminger ASSERT_RTNL(); 3239aee80b54Sstephen hemminger 3240e5d08d71SIan Morris idev = ipv6_find_idev(dev); 324163159f29SIan Morris if (!idev) { 324291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3243aee80b54Sstephen hemminger return; 3244aee80b54Sstephen hemminger } 3245aee80b54Sstephen hemminger 3246bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3247d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3248d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3249aee80b54Sstephen hemminger } 3250aee80b54Sstephen hemminger #endif 3251aee80b54Sstephen hemminger 3252f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev, 3253f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3254f1705ec1SDavid Ahern { 3255f1705ec1SDavid Ahern if (!ifp->rt) { 3256f1705ec1SDavid Ahern struct rt6_info *rt; 3257f1705ec1SDavid Ahern 3258f1705ec1SDavid Ahern rt = addrconf_dst_alloc(idev, &ifp->addr, false); 3259f1705ec1SDavid Ahern if (unlikely(IS_ERR(rt))) 3260f1705ec1SDavid Ahern return PTR_ERR(rt); 3261f1705ec1SDavid Ahern 3262f1705ec1SDavid Ahern ifp->rt = rt; 3263f1705ec1SDavid Ahern } 3264f1705ec1SDavid Ahern 3265f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3266f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3267f1705ec1SDavid Ahern idev->dev, 0, 0); 3268f1705ec1SDavid Ahern } 3269f1705ec1SDavid Ahern 3270f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3271f1705ec1SDavid Ahern 3272f1705ec1SDavid Ahern return 0; 3273f1705ec1SDavid Ahern } 3274f1705ec1SDavid Ahern 3275f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev) 3276f1705ec1SDavid Ahern { 3277f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3278f1705ec1SDavid Ahern struct inet6_dev *idev; 3279f1705ec1SDavid Ahern 3280f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3281f1705ec1SDavid Ahern if (!idev) 3282f1705ec1SDavid Ahern return; 3283f1705ec1SDavid Ahern 3284f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3285f1705ec1SDavid Ahern 3286f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3287f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3288f1705ec1SDavid Ahern fixup_permanent_addr(idev, ifp) < 0) { 3289f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3290f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3291f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3292f1705ec1SDavid Ahern 3293f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3294f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3295f1705ec1SDavid Ahern } 3296f1705ec1SDavid Ahern } 3297f1705ec1SDavid Ahern 3298f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3299f1705ec1SDavid Ahern } 3300f1705ec1SDavid Ahern 33011da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3302351638e7SJiri Pirko void *ptr) 33031da177e4SLinus Torvalds { 3304351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 33054f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3306748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 3307c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3308b217d616SHerbert Xu int err; 33091da177e4SLinus Torvalds 33101da177e4SLinus Torvalds switch (event) { 331145ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 331274235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 331345ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3314a317a2f1SWANG Cong if (IS_ERR(idev)) 3315a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 331645ba9dd2SYOSHIFUJI Hideaki } 331745ba9dd2SYOSHIFUJI Hideaki break; 3318bcdd553fSStephen Hemminger 3319b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3320b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3321b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 3322b7b0b1d2SAlexander Duyck addrconf_ifdown(dev, 1); 3323b7b0b1d2SAlexander Duyck break; 3324b7b0b1d2SAlexander Duyck } 3325b7b0b1d2SAlexander Duyck 3326b7b0b1d2SAlexander Duyck if (idev) { 3327b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3328b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3329b7b0b1d2SAlexander Duyck break; 3330b7b0b1d2SAlexander Duyck } 3331b7b0b1d2SAlexander Duyck 3332b7b0b1d2SAlexander Duyck /* allocate new idev */ 3333b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3334b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3335b7b0b1d2SAlexander Duyck break; 3336b7b0b1d2SAlexander Duyck 3337b7b0b1d2SAlexander Duyck /* device is still not ready */ 3338b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3339b7b0b1d2SAlexander Duyck break; 3340b7b0b1d2SAlexander Duyck 3341b7b0b1d2SAlexander Duyck run_pending = 1; 3342b7b0b1d2SAlexander Duyck 3343b7b0b1d2SAlexander Duyck /* fall through */ 3344b7b0b1d2SAlexander Duyck 33451da177e4SLinus Torvalds case NETDEV_UP: 33463c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3347c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3348c2edacf8SJay Vosburgh break; 3349c2edacf8SJay Vosburgh 33503ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 33513ce62a84SWANG Cong break; 33523ce62a84SWANG Cong 33533c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 335438bd10c4SDavid Ahern /* restore routes for permanent addresses */ 335538bd10c4SDavid Ahern addrconf_permanent_addr(dev); 335638bd10c4SDavid Ahern 3357f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33583c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3359f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 33603c21edbdSYOSHIFUJI Hideaki dev->name); 33613c21edbdSYOSHIFUJI Hideaki break; 33623c21edbdSYOSHIFUJI Hideaki } 336399081049SKristian Slavov 3364d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3365d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3366d31c7b8fSEvgeniy Polyakov 3367a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 336899081049SKristian Slavov idev->if_flags |= IF_READY; 3369e3ec6cfcSBenjamin Thery run_pending = 1; 3370e3ec6cfcSBenjamin Thery } 3371b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 3372f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33733c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 33743c21edbdSYOSHIFUJI Hideaki break; 33753c21edbdSYOSHIFUJI Hideaki } 33763c21edbdSYOSHIFUJI Hideaki 33773c21edbdSYOSHIFUJI Hideaki if (idev) { 3378bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 33793c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 33803c21edbdSYOSHIFUJI Hideaki break; 33813c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 33823c21edbdSYOSHIFUJI Hideaki } 33833c21edbdSYOSHIFUJI Hideaki 3384f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 33853c21edbdSYOSHIFUJI Hideaki dev->name); 33863c21edbdSYOSHIFUJI Hideaki 3387c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 33883c21edbdSYOSHIFUJI Hideaki } 33893c21edbdSYOSHIFUJI Hideaki 33901da177e4SLinus Torvalds switch (dev->type) { 339107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33921da177e4SLinus Torvalds case ARPHRD_SIT: 33931da177e4SLinus Torvalds addrconf_sit_config(dev); 33941da177e4SLinus Torvalds break; 33950be669bbSJoerg Roedel #endif 339607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3397aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3398aee80b54Sstephen hemminger addrconf_gre_config(dev); 3399aee80b54Sstephen hemminger break; 3400aee80b54Sstephen hemminger #endif 34011da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 34021da177e4SLinus Torvalds init_loopback(dev); 34031da177e4SLinus Torvalds break; 34041da177e4SLinus Torvalds 34051da177e4SLinus Torvalds default: 34061da177e4SLinus Torvalds addrconf_dev_config(dev); 34071da177e4SLinus Torvalds break; 34083ff50b79SStephen Hemminger } 3409bcdd553fSStephen Hemminger 3410a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3411c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3412c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3413c5e33bddSYOSHIFUJI Hideaki 3414bcdd553fSStephen Hemminger /* 3415bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3416bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3417bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 34181da177e4SLinus Torvalds */ 3419bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3420bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 34211da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 34221da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds idev->tstamp = jiffies; 34251da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3426bcdd553fSStephen Hemminger 3427bcdd553fSStephen Hemminger /* 3428bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3429bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 34301da177e4SLinus Torvalds */ 34311da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3432bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 34331da177e4SLinus Torvalds } 34341da177e4SLinus Torvalds break; 34351da177e4SLinus Torvalds 34361da177e4SLinus Torvalds case NETDEV_DOWN: 34371da177e4SLinus Torvalds case NETDEV_UNREGISTER: 34381da177e4SLinus Torvalds /* 34391da177e4SLinus Torvalds * Remove all addresses from this interface. 34401da177e4SLinus Torvalds */ 34411da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 34421da177e4SLinus Torvalds break; 34433c21edbdSYOSHIFUJI Hideaki 34441da177e4SLinus Torvalds case NETDEV_CHANGENAME: 34451da177e4SLinus Torvalds if (idev) { 34465632c515SStephen Hemminger snmp6_unregister_dev(idev); 3447408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3448a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3449b217d616SHerbert Xu if (err) 3450b217d616SHerbert Xu return notifier_from_errno(err); 3451a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3452a317a2f1SWANG Cong if (err) { 3453a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3454a317a2f1SWANG Cong return notifier_from_errno(err); 3455a317a2f1SWANG Cong } 34565632c515SStephen Hemminger } 34571da177e4SLinus Torvalds break; 3458bcdd553fSStephen Hemminger 345993d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 346093d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 34613ef0952cSAndrew Lunn if (idev) 346293d9b7d7SJiri Pirko addrconf_type_change(dev, event); 346375c78500SMoni Shoua break; 34644f7f34eaSDavid Ahern 34654f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 34664f7f34eaSDavid Ahern info = ptr; 34674f7f34eaSDavid Ahern 34684f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 34694f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 34704f7f34eaSDavid Ahern */ 34714f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 34724f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 34733ff50b79SStephen Hemminger } 34741da177e4SLinus Torvalds 34751da177e4SLinus Torvalds return NOTIFY_OK; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 34781da177e4SLinus Torvalds /* 34791da177e4SLinus Torvalds * addrconf module should be notified of a device going up 34801da177e4SLinus Torvalds */ 34811da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 34821da177e4SLinus Torvalds .notifier_call = addrconf_notify, 34831da177e4SLinus Torvalds }; 34841da177e4SLinus Torvalds 348593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 348675c78500SMoni Shoua { 348775c78500SMoni Shoua struct inet6_dev *idev; 348875c78500SMoni Shoua ASSERT_RTNL(); 348975c78500SMoni Shoua 349075c78500SMoni Shoua idev = __in6_dev_get(dev); 349175c78500SMoni Shoua 349293d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 349375c78500SMoni Shoua ipv6_mc_remap(idev); 349493d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 349575c78500SMoni Shoua ipv6_mc_unmap(idev); 349675c78500SMoni Shoua } 349775c78500SMoni Shoua 349870af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 349970af921dSDavid Ahern { 350070af921dSDavid Ahern return ipv6_addr_type(addr) & 350170af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 350270af921dSDavid Ahern } 350370af921dSDavid Ahern 35041da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 35051da177e4SLinus Torvalds { 3506c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3507502a2ffdSstephen hemminger struct inet6_dev *idev; 3508f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 3509f1705ec1SDavid Ahern struct list_head del_list; 3510f1705ec1SDavid Ahern int _keep_addr; 3511f1705ec1SDavid Ahern bool keep_addr; 351273a8bd74SDavid S. Miller int state, i; 35131da177e4SLinus Torvalds 35141da177e4SLinus Torvalds ASSERT_RTNL(); 35151da177e4SLinus Torvalds 3516f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 351773a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 35181da177e4SLinus Torvalds 35191da177e4SLinus Torvalds idev = __in6_dev_get(dev); 352063159f29SIan Morris if (!idev) 35211da177e4SLinus Torvalds return -ENODEV; 35221da177e4SLinus Torvalds 3523bcdd553fSStephen Hemminger /* 3524bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3525bcdd553fSStephen Hemminger * Do not dev_put! 35261da177e4SLinus Torvalds */ 3527439e2385SDenis V. Lunev if (how) { 35281da177e4SLinus Torvalds idev->dead = 1; 35298814c4b5SYOSHIFUJI Hideaki 35308814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3531a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 35321da177e4SLinus Torvalds 35331da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 35341da177e4SLinus Torvalds snmp6_unregister_dev(idev); 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds } 35371da177e4SLinus Torvalds 3538f1705ec1SDavid Ahern /* aggregate the system setting and interface setting */ 3539f1705ec1SDavid Ahern _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3540f1705ec1SDavid Ahern if (!_keep_addr) 3541f1705ec1SDavid Ahern _keep_addr = idev->cnf.keep_addr_on_down; 3542f1705ec1SDavid Ahern 3543f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3544f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3545f1705ec1SDavid Ahern */ 3546f1705ec1SDavid Ahern keep_addr = !(how || _keep_addr <= 0); 3547f1705ec1SDavid Ahern 354873a8bd74SDavid S. Miller /* Step 2: clear hash table */ 354973a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 355073a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 355173a8bd74SDavid S. Miller 355273a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 355373a8bd74SDavid S. Miller restart: 3554b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 355573a8bd74SDavid S. Miller if (ifa->idev == idev) { 3556c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3557f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3558f1705ec1SDavid Ahern * address is retained on a down event 3559f1705ec1SDavid Ahern */ 3560f1705ec1SDavid Ahern if (!keep_addr || 356170af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 356270af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3563f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 356473a8bd74SDavid S. Miller goto restart; 356573a8bd74SDavid S. Miller } 356673a8bd74SDavid S. Miller } 3567f1705ec1SDavid Ahern } 356873a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 356973a8bd74SDavid S. Miller } 357073a8bd74SDavid S. Miller 35711da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35721da177e4SLinus Torvalds 3573b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3574b7b1bfceSHannes Frederic Sowa 3575bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3576439e2385SDenis V. Lunev if (!how) 35773c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 35781da177e4SLinus Torvalds 3579439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 35801da177e4SLinus Torvalds in6_dev_put(idev); 35811da177e4SLinus Torvalds 3582bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3583372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3584372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3585372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3586372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 35871da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 35881da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds if (ifa->ifpub) { 35911da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 35921da177e4SLinus Torvalds ifa->ifpub = NULL; 35931da177e4SLinus Torvalds } 35941da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 35951da177e4SLinus Torvalds in6_ifa_put(ifa); 35961da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35971da177e4SLinus Torvalds } 35988f37ada5Sstephen hemminger 3599f1705ec1SDavid Ahern /* re-combine the user config with event to determine if permanent 3600f1705ec1SDavid Ahern * addresses are to be removed from the interface list 3601f1705ec1SDavid Ahern */ 3602f1705ec1SDavid Ahern keep_addr = (!how && _keep_addr > 0); 3603f1705ec1SDavid Ahern 3604f1705ec1SDavid Ahern INIT_LIST_HEAD(&del_list); 3605f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 360638bd10c4SDavid Ahern struct rt6_info *rt = NULL; 360738bd10c4SDavid Ahern 3608c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 360984e8b803Sstephen hemminger 36104c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 36118e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3612f1705ec1SDavid Ahern 361370af921dSDavid Ahern if (keep_addr && (ifa->flags & IFA_F_PERMANENT) && 361470af921dSDavid Ahern !addr_is_local(&ifa->addr)) { 3615f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3616f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 3617f1705ec1SDavid Ahern ifa->state = 0; 3618f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3619f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 362038bd10c4SDavid Ahern 362138bd10c4SDavid Ahern rt = ifa->rt; 362238bd10c4SDavid Ahern ifa->rt = NULL; 3623f1705ec1SDavid Ahern } else { 36244c5ff6a6SHerbert Xu state = ifa->state; 36254c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3626f1705ec1SDavid Ahern 3627f1705ec1SDavid Ahern list_del(&ifa->if_list); 3628f1705ec1SDavid Ahern list_add(&ifa->if_list, &del_list); 3629f1705ec1SDavid Ahern } 3630f1705ec1SDavid Ahern 36318e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 36324c5ff6a6SHerbert Xu 363338bd10c4SDavid Ahern if (rt) 363438bd10c4SDavid Ahern ip6_del_rt(rt); 363538bd10c4SDavid Ahern 36369d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 36371da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3638f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 36392de79570SLorenzo Colitti } 364073a8bd74SDavid S. Miller 36411da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36421da177e4SLinus Torvalds } 36438f37ada5Sstephen hemminger 36441da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 36451da177e4SLinus Torvalds 3646f1705ec1SDavid Ahern /* now clean up addresses to be removed */ 3647f1705ec1SDavid Ahern while (!list_empty(&del_list)) { 3648f1705ec1SDavid Ahern ifa = list_first_entry(&del_list, 3649f1705ec1SDavid Ahern struct inet6_ifaddr, if_list); 3650f1705ec1SDavid Ahern list_del(&ifa->if_list); 3651f1705ec1SDavid Ahern 3652f1705ec1SDavid Ahern in6_ifa_put(ifa); 3653f1705ec1SDavid Ahern } 3654f1705ec1SDavid Ahern 3655381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3656381f4dcaSSabrina Dubroca if (how) { 3657381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 36581da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3659381f4dcaSSabrina Dubroca } else { 36601da177e4SLinus Torvalds ipv6_mc_down(idev); 3661381f4dcaSSabrina Dubroca } 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds idev->tstamp = jiffies; 36641da177e4SLinus Torvalds 3665bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3666439e2385SDenis V. Lunev if (how) { 3667408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 36681da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 36691da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 36701da177e4SLinus Torvalds in6_dev_put(idev); 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds return 0; 36731da177e4SLinus Torvalds } 36741da177e4SLinus Torvalds 36751da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 36761da177e4SLinus Torvalds { 3677b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3678caf92bc4SCong Wang struct net_device *dev = idev->dev; 3679b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 36801da177e4SLinus Torvalds 3681b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 36825b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 36831da177e4SLinus Torvalds goto out; 36841da177e4SLinus Torvalds 36859ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 36861da177e4SLinus Torvalds goto out; 36875b2a1953Sstephen hemminger 36885b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 36895b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 36905b2a1953Sstephen hemminger goto out; 36911da177e4SLinus Torvalds 3692b7b1bfceSHannes Frederic Sowa if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3693caf92bc4SCong Wang write_unlock(&idev->lock); 3694caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3695caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3696b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3697b7b1bfceSHannes Frederic Sowa else 3698caf92bc4SCong Wang goto put; 3699b7b1bfceSHannes Frederic Sowa 3700caf92bc4SCong Wang write_lock(&idev->lock); 37011da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3702b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3703b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 37045b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 37055b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 37061da177e4SLinus Torvalds } else { 37071da177e4SLinus Torvalds /* 37081da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 37091da177e4SLinus Torvalds * assumption any longer. 37101da177e4SLinus Torvalds */ 371191df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 37121da177e4SLinus Torvalds } 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds out: 3715b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3716caf92bc4SCong Wang put: 3717b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 37181da177e4SLinus Torvalds } 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds /* 37211da177e4SLinus Torvalds * Duplicate Address Detection 37221da177e4SLinus Torvalds */ 37233c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 37243c21edbdSYOSHIFUJI Hideaki { 37253c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 37263c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 37273c21edbdSYOSHIFUJI Hideaki 372895c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 372995c385b4SNeil Horman rand_num = 0; 373095c385b4SNeil Horman else 373163862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 373295c385b4SNeil Horman 3733b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3734c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 37353c21edbdSYOSHIFUJI Hideaki } 37363c21edbdSYOSHIFUJI Hideaki 3737c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 37381da177e4SLinus Torvalds { 37391da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 37401da177e4SLinus Torvalds struct net_device *dev = idev->dev; 374116186a82Ssubashab@codeaurora.org bool notify = false; 37421da177e4SLinus Torvalds 37431da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 37441da177e4SLinus Torvalds 374563862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 37461da177e4SLinus Torvalds 37471da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3748622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3749e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 37501da177e4SLinus Torvalds goto out; 37511da177e4SLinus Torvalds 37521da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 37531b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 375455ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 375555ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3756cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 375721809fafSstephen hemminger spin_unlock(&ifp->lock); 37581da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 37591da177e4SLinus Torvalds 37601da177e4SLinus Torvalds addrconf_dad_completed(ifp); 37611da177e4SLinus Torvalds return; 37621da177e4SLinus Torvalds } 37631da177e4SLinus Torvalds 37646732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 376521809fafSstephen hemminger spin_unlock(&ifp->lock); 37666732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 37673c21edbdSYOSHIFUJI Hideaki /* 3768590a9887SMasatake YAMATO * If the device is not ready: 37693c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 37703c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 37713c21edbdSYOSHIFUJI Hideaki */ 37723c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3773cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 37746732badeSYOSHIFUJI Hideaki return; 37753c21edbdSYOSHIFUJI Hideaki } 377695c385b4SNeil Horman 377795c385b4SNeil Horman /* 377895c385b4SNeil Horman * Optimistic nodes can start receiving 377995c385b4SNeil Horman * Frames right away 378095c385b4SNeil Horman */ 37817fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 378295c385b4SNeil Horman ip6_ins_rt(ifp->rt); 37837fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 37847fd2561eSErik Kline /* Because optimistic nodes can use this address, 37857fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 37867fd2561eSErik Kline */ 378716186a82Ssubashab@codeaurora.org notify = true; 37887fd2561eSErik Kline } 37897fd2561eSErik Kline } 379095c385b4SNeil Horman 37916732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 37921da177e4SLinus Torvalds out: 3793622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 37941da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 379516186a82Ssubashab@codeaurora.org if (notify) 379616186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 37971da177e4SLinus Torvalds } 37981da177e4SLinus Torvalds 3799c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 38001da177e4SLinus Torvalds { 3801c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3802c15b1ccaSHannes Frederic Sowa 38038e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3804c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3805c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3806c15b1ccaSHannes Frederic Sowa begin_dad = true; 3807c15b1ccaSHannes Frederic Sowa } 38088e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3809c15b1ccaSHannes Frederic Sowa 3810c15b1ccaSHannes Frederic Sowa if (begin_dad) 3811c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3812c15b1ccaSHannes Frederic Sowa } 3813c15b1ccaSHannes Frederic Sowa 3814c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3815c15b1ccaSHannes Frederic Sowa { 3816c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3817c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3818c15b1ccaSHannes Frederic Sowa dad_work); 38191da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38201da177e4SLinus Torvalds struct in6_addr mcaddr; 38211da177e4SLinus Torvalds 3822c15b1ccaSHannes Frederic Sowa enum { 3823c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3824c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3825c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3826c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3827c15b1ccaSHannes Frederic Sowa 3828c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3829c15b1ccaSHannes Frederic Sowa 38308e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3831c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3832c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3833c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3834c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3835c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3836c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 3837c15b1ccaSHannes Frederic Sowa } 38388e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3839c15b1ccaSHannes Frederic Sowa 3840c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3841c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3842c15b1ccaSHannes Frederic Sowa goto out; 3843c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3844c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 3845c15b1ccaSHannes Frederic Sowa goto out; 3846c15b1ccaSHannes Frederic Sowa } 3847c15b1ccaSHannes Frederic Sowa 3848b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3849f2344a13SHerbert Xu goto out; 3850f2344a13SHerbert Xu 3851c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3852122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3853c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38541da177e4SLinus Torvalds goto out; 38551da177e4SLinus Torvalds } 385621809fafSstephen hemminger 385721809fafSstephen hemminger spin_lock(&ifp->lock); 3858622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3859622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3860c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3861622ccdf1SHerbert Xu goto out; 3862622ccdf1SHerbert Xu } 3863622ccdf1SHerbert Xu 3864b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 38651da177e4SLinus Torvalds /* 38661da177e4SLinus Torvalds * DAD was successful 38671da177e4SLinus Torvalds */ 38681da177e4SLinus Torvalds 3869cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 387021809fafSstephen hemminger spin_unlock(&ifp->lock); 3871c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38721da177e4SLinus Torvalds 38731da177e4SLinus Torvalds addrconf_dad_completed(ifp); 38741da177e4SLinus Torvalds 38751da177e4SLinus Torvalds goto out; 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds 3878b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3879c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 38801f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 388121809fafSstephen hemminger spin_unlock(&ifp->lock); 3882c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38831da177e4SLinus Torvalds 38841da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 38851da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3886304d888bSNicolas Dichtel ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any); 38871da177e4SLinus Torvalds out: 38881da177e4SLinus Torvalds in6_ifa_put(ifp); 3889c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 38901da177e4SLinus Torvalds } 38911da177e4SLinus Torvalds 389211ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 389311ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 389411ffff75SHannes Frederic Sowa { 389511ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 389611ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 389711ffff75SHannes Frederic Sowa 3898602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3899602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 3900602582caSHannes Frederic Sowa break; 390111ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 390211ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 390311ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 390411ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 390511ffff75SHannes Frederic Sowa return false; 390611ffff75SHannes Frederic Sowa } 390711ffff75SHannes Frederic Sowa return true; 390811ffff75SHannes Frederic Sowa } 390911ffff75SHannes Frederic Sowa 39101da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 39111da177e4SLinus Torvalds { 39121da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3913b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3914b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3915b7b1bfceSHannes Frederic Sowa 3916c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 39171da177e4SLinus Torvalds 39181da177e4SLinus Torvalds /* 39191da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 39201da177e4SLinus Torvalds */ 39211da177e4SLinus Torvalds 39221da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 39231da177e4SLinus Torvalds 3924026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3925026359bcSTore Anderson router advertisements, start sending router solicitations. 39261da177e4SLinus Torvalds */ 39271da177e4SLinus Torvalds 39281ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 392911ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3930b173ee48SHannes Frederic Sowa send_rs = send_mld && 3931b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 3932b173ee48SHannes Frederic Sowa ifp->idev->cnf.rtr_solicits > 0 && 3933b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 39341ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 39351ec047ebSHannes Frederic Sowa 3936b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 3937b173ee48SHannes Frederic Sowa * Resend with proper ll now. 3938b173ee48SHannes Frederic Sowa */ 3939b173ee48SHannes Frederic Sowa if (send_mld) 3940b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 3941b173ee48SHannes Frederic Sowa 39421ec047ebSHannes Frederic Sowa if (send_rs) { 39431da177e4SLinus Torvalds /* 39441da177e4SLinus Torvalds * If a host as already performed a random delay 39451da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 39461da177e4SLinus Torvalds * to delay again before sending the first RS 39471da177e4SLinus Torvalds */ 39481ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3949b7b1bfceSHannes Frederic Sowa return; 39501ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 39511da177e4SLinus Torvalds 3952b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3953b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3954b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 39551da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3956b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3957b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3958b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3959b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 39601da177e4SLinus Torvalds } 39611da177e4SLinus Torvalds } 39621da177e4SLinus Torvalds 3963e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3964e21e8467SStephen Hemminger { 3965c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3966c5e33bddSYOSHIFUJI Hideaki 3967c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3968502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 396921809fafSstephen hemminger spin_lock(&ifp->lock); 3970f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3971f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3972c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3973f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3974c5e33bddSYOSHIFUJI Hideaki } 3975c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3976c5e33bddSYOSHIFUJI Hideaki } 3977c5e33bddSYOSHIFUJI Hideaki 39781da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 39791da177e4SLinus Torvalds struct if6_iter_state { 39803c40090aSDaniel Lezcano struct seq_net_private p; 39811da177e4SLinus Torvalds int bucket; 39821d578303SMihai Maruseac int offset; 39831da177e4SLinus Torvalds }; 39841da177e4SLinus Torvalds 39851d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 39861da177e4SLinus Torvalds { 39871da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 39881da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 39891218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 39901d578303SMihai Maruseac int p = 0; 39911da177e4SLinus Torvalds 39921d578303SMihai Maruseac /* initial bucket if pos is 0 */ 39931d578303SMihai Maruseac if (pos == 0) { 39941d578303SMihai Maruseac state->bucket = 0; 39951d578303SMihai Maruseac state->offset = 0; 39961d578303SMihai Maruseac } 39971d578303SMihai Maruseac 39981d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3999b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 40001d578303SMihai Maruseac addr_lst) { 40019f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40029f0d3c27SEric Dumazet continue; 40031d578303SMihai Maruseac /* sync with offset */ 40041d578303SMihai Maruseac if (p < state->offset) { 40051d578303SMihai Maruseac p++; 40061d578303SMihai Maruseac continue; 40071d578303SMihai Maruseac } 40081d578303SMihai Maruseac state->offset++; 40091da177e4SLinus Torvalds return ifa; 40101da177e4SLinus Torvalds } 40111d578303SMihai Maruseac 40121d578303SMihai Maruseac /* prepare for next bucket */ 40131d578303SMihai Maruseac state->offset = 0; 40141d578303SMihai Maruseac p = 0; 40151d578303SMihai Maruseac } 4016c2e21293Sstephen hemminger return NULL; 4017c2e21293Sstephen hemminger } 40181da177e4SLinus Torvalds 4019c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4020c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 40211da177e4SLinus Torvalds { 40221da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 40231218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 40241da177e4SLinus Torvalds 4025b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 40269f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40279f0d3c27SEric Dumazet continue; 40281d578303SMihai Maruseac state->offset++; 40291da177e4SLinus Torvalds return ifa; 40301d578303SMihai Maruseac } 40311da177e4SLinus Torvalds 4032c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 40331d578303SMihai Maruseac state->offset = 0; 4034b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 4035c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 40369f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40379f0d3c27SEric Dumazet continue; 40381d578303SMihai Maruseac state->offset++; 4039c2e21293Sstephen hemminger return ifa; 4040c2e21293Sstephen hemminger } 4041c2e21293Sstephen hemminger } 4042c2e21293Sstephen hemminger 4043c2e21293Sstephen hemminger return NULL; 4044c2e21293Sstephen hemminger } 4045c2e21293Sstephen hemminger 40461da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 40474f70eccaSEric Dumazet __acquires(rcu_bh) 40481da177e4SLinus Torvalds { 40495c578aedSstephen hemminger rcu_read_lock_bh(); 40501d578303SMihai Maruseac return if6_get_first(seq, *pos); 40511da177e4SLinus Torvalds } 40521da177e4SLinus Torvalds 40531da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 40541da177e4SLinus Torvalds { 40551da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 40561da177e4SLinus Torvalds 40571da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 40581da177e4SLinus Torvalds ++*pos; 40591da177e4SLinus Torvalds return ifa; 40601da177e4SLinus Torvalds } 40611da177e4SLinus Torvalds 40621da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 40634f70eccaSEric Dumazet __releases(rcu_bh) 40641da177e4SLinus Torvalds { 40655c578aedSstephen hemminger rcu_read_unlock_bh(); 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 40691da177e4SLinus Torvalds { 40701da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4071971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4072b071195dSHarvey Harrison &ifp->addr, 40731da177e4SLinus Torvalds ifp->idev->dev->ifindex, 40741da177e4SLinus Torvalds ifp->prefix_len, 40751da177e4SLinus Torvalds ifp->scope, 4076971a351cSJiri Pirko (u8) ifp->flags, 40771da177e4SLinus Torvalds ifp->idev->dev->name); 40781da177e4SLinus Torvalds return 0; 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds 408156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 40821da177e4SLinus Torvalds .start = if6_seq_start, 40831da177e4SLinus Torvalds .next = if6_seq_next, 40841da177e4SLinus Torvalds .show = if6_seq_show, 40851da177e4SLinus Torvalds .stop = if6_seq_stop, 40861da177e4SLinus Torvalds }; 40871da177e4SLinus Torvalds 40881da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 40891da177e4SLinus Torvalds { 40903c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4091cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 40921da177e4SLinus Torvalds } 40931da177e4SLinus Torvalds 40949a32144eSArjan van de Ven static const struct file_operations if6_fops = { 40951da177e4SLinus Torvalds .owner = THIS_MODULE, 40961da177e4SLinus Torvalds .open = if6_seq_open, 40971da177e4SLinus Torvalds .read = seq_read, 40981da177e4SLinus Torvalds .llseek = seq_lseek, 40993c40090aSDaniel Lezcano .release = seq_release_net, 41001da177e4SLinus Torvalds }; 41011da177e4SLinus Torvalds 41022c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 41031da177e4SLinus Torvalds { 4104d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 41051da177e4SLinus Torvalds return -ENOMEM; 41061da177e4SLinus Torvalds return 0; 41071da177e4SLinus Torvalds } 41081da177e4SLinus Torvalds 41092c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 41103c40090aSDaniel Lezcano { 4111ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 41123c40090aSDaniel Lezcano } 41133c40090aSDaniel Lezcano 41143c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 41153c40090aSDaniel Lezcano .init = if6_proc_net_init, 41163c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 41173c40090aSDaniel Lezcano }; 41183c40090aSDaniel Lezcano 41193c40090aSDaniel Lezcano int __init if6_proc_init(void) 41203c40090aSDaniel Lezcano { 41213c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 41223c40090aSDaniel Lezcano } 41233c40090aSDaniel Lezcano 41241da177e4SLinus Torvalds void if6_proc_exit(void) 41251da177e4SLinus Torvalds { 41263c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 41271da177e4SLinus Torvalds } 41281da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 41291da177e4SLinus Torvalds 413007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 41313b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4132b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 41333b9f9a1cSNoriaki TAKAMIYA { 41343b9f9a1cSNoriaki TAKAMIYA int ret = 0; 4135c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 4136ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 4137c2e21293Sstephen hemminger 41385c578aedSstephen hemminger rcu_read_lock_bh(); 4139b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 4140878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4141389f6612SDaniel Lezcano continue; 4142caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 41433b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 41443b9f9a1cSNoriaki TAKAMIYA ret = 1; 41453b9f9a1cSNoriaki TAKAMIYA break; 41463b9f9a1cSNoriaki TAKAMIYA } 41473b9f9a1cSNoriaki TAKAMIYA } 41485c578aedSstephen hemminger rcu_read_unlock_bh(); 41493b9f9a1cSNoriaki TAKAMIYA return ret; 41503b9f9a1cSNoriaki TAKAMIYA } 41513b9f9a1cSNoriaki TAKAMIYA #endif 41523b9f9a1cSNoriaki TAKAMIYA 41531da177e4SLinus Torvalds /* 41541da177e4SLinus Torvalds * Periodic address status verification 41551da177e4SLinus Torvalds */ 41561da177e4SLinus Torvalds 4157c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 41581da177e4SLinus Torvalds { 4159b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 41601da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 41611da177e4SLinus Torvalds int i; 41621da177e4SLinus Torvalds 4163c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4164c15b1ccaSHannes Frederic Sowa 41655c578aedSstephen hemminger rcu_read_lock_bh(); 41661da177e4SLinus Torvalds now = jiffies; 4167b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 41681da177e4SLinus Torvalds 4169c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 41701da177e4SLinus Torvalds 41711da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 41721da177e4SLinus Torvalds restart: 4173c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 41741da177e4SLinus Torvalds unsigned long age; 41751da177e4SLinus Torvalds 4176fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4177fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4178fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4179fad8da3eSYasushi Asano */ 4180fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4181fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 41821da177e4SLinus Torvalds continue; 41831da177e4SLinus Torvalds 41841da177e4SLinus Torvalds spin_lock(&ifp->lock); 4185b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4186b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 41871da177e4SLinus Torvalds 41888f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 41898f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 41901da177e4SLinus Torvalds spin_unlock(&ifp->lock); 41911da177e4SLinus Torvalds in6_ifa_hold(ifp); 41921da177e4SLinus Torvalds ipv6_del_addr(ifp); 41931da177e4SLinus Torvalds goto restart; 41948f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 41958f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 41968f27ebb9SYOSHIFUJI Hideaki continue; 41971da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4198a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 41991da177e4SLinus Torvalds int deprecate = 0; 42001da177e4SLinus Torvalds 42011da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 42021da177e4SLinus Torvalds deprecate = 1; 42031da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 42041da177e4SLinus Torvalds } 42051da177e4SLinus Torvalds 4206fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4207fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 42081da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 42091da177e4SLinus Torvalds 42101da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42111da177e4SLinus Torvalds 42121da177e4SLinus Torvalds if (deprecate) { 42131da177e4SLinus Torvalds in6_ifa_hold(ifp); 42141da177e4SLinus Torvalds 42151da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 42161da177e4SLinus Torvalds in6_ifa_put(ifp); 42171da177e4SLinus Torvalds goto restart; 42181da177e4SLinus Torvalds } 42191da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 42201da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 422188949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 422288949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 42231f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 422488949cf4Sstephen hemminger 42251da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 42261da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 42271da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 42281da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 42291da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 42301da177e4SLinus Torvalds ifp->regen_count++; 42311da177e4SLinus Torvalds in6_ifa_hold(ifp); 42321da177e4SLinus Torvalds in6_ifa_hold(ifpub); 42331da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42345c578aedSstephen hemminger 4235291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4236291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4237291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 42381da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 42391da177e4SLinus Torvalds in6_ifa_put(ifpub); 42401da177e4SLinus Torvalds in6_ifa_put(ifp); 42411da177e4SLinus Torvalds goto restart; 42421da177e4SLinus Torvalds } 42431da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 42441da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 42451da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42461da177e4SLinus Torvalds } else { 42471da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 42481da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 42491da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 42501da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42511da177e4SLinus Torvalds } 42521da177e4SLinus Torvalds } 42531da177e4SLinus Torvalds } 42541da177e4SLinus Torvalds 4255b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4256b2db7564SYOSHIFUJI Hideaki next_sched = next; 4257b2db7564SYOSHIFUJI Hideaki 4258b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4259b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4260b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4261b2db7564SYOSHIFUJI Hideaki 4262b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4263b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4264b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4265b2db7564SYOSHIFUJI Hideaki 4266ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4267ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4268c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 42695c578aedSstephen hemminger rcu_read_unlock_bh(); 42701da177e4SLinus Torvalds } 42711da177e4SLinus Torvalds 4272c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4273c15b1ccaSHannes Frederic Sowa { 4274c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4275c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4276c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4277c15b1ccaSHannes Frederic Sowa } 4278c15b1ccaSHannes Frederic Sowa 4279c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4280c15b1ccaSHannes Frederic Sowa { 4281c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4282c15b1ccaSHannes Frederic Sowa } 4283c15b1ccaSHannes Frederic Sowa 4284caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4285caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4286461d8837SThomas Graf { 4287461d8837SThomas Graf struct in6_addr *pfx = NULL; 4288461d8837SThomas Graf 4289caeaba79SNicolas Dichtel *peer_pfx = NULL; 4290caeaba79SNicolas Dichtel 4291461d8837SThomas Graf if (addr) 4292461d8837SThomas Graf pfx = nla_data(addr); 4293461d8837SThomas Graf 4294461d8837SThomas Graf if (local) { 4295461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4296caeaba79SNicolas Dichtel *peer_pfx = pfx; 4297461d8837SThomas Graf pfx = nla_data(local); 4298461d8837SThomas Graf } 4299461d8837SThomas Graf 4300461d8837SThomas Graf return pfx; 4301461d8837SThomas Graf } 4302461d8837SThomas Graf 4303ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4304461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4305461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4306461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4307479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4308461d8837SThomas Graf }; 4309461d8837SThomas Graf 43101da177e4SLinus Torvalds static int 4311661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 43121da177e4SLinus Torvalds { 43133b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4314b933f716SThomas Graf struct ifaddrmsg *ifm; 4315b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4316caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 43176046d5b4SHeiner Kallweit u32 ifa_flags; 4318b933f716SThomas Graf int err; 43191da177e4SLinus Torvalds 4320b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4321b933f716SThomas Graf if (err < 0) 4322b933f716SThomas Graf return err; 4323b933f716SThomas Graf 4324b933f716SThomas Graf ifm = nlmsg_data(nlh); 4325caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 432663159f29SIan Morris if (!pfx) 43271da177e4SLinus Torvalds return -EINVAL; 43281da177e4SLinus Torvalds 43296046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 43306046d5b4SHeiner Kallweit 43316046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 43326046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 43336046d5b4SHeiner Kallweit 43346046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 43356046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 43361da177e4SLinus Torvalds } 43371da177e4SLinus Torvalds 4338479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 433955ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4340081bba5bSNoriaki TAKAMIYA { 43416f704992SYOSHIFUJI Hideaki u32 flags; 43426f704992SYOSHIFUJI Hideaki clock_t expires; 43434bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 434453bd6749SJiri Pirko bool was_managetempaddr; 43455b84efecSThomas Haller bool had_prefixroute; 434646d48046SYOSHIFUJI Hideaki 4347c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4348c15b1ccaSHannes Frederic Sowa 4349081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4350081bba5bSNoriaki TAKAMIYA return -EINVAL; 4351081bba5bSNoriaki TAKAMIYA 435253bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 435353bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 435453bd6749SJiri Pirko return -EINVAL; 435553bd6749SJiri Pirko 43564bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 43574bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43584bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 43594bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 43606f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 43614bed72e4SYOSHIFUJI Hideaki } else { 43624bed72e4SYOSHIFUJI Hideaki expires = 0; 43634bed72e4SYOSHIFUJI Hideaki flags = 0; 43644bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 43656f704992SYOSHIFUJI Hideaki } 4366081bba5bSNoriaki TAKAMIYA 43674bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 43684bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43694bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 437055ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 43714bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 43724bed72e4SYOSHIFUJI Hideaki } 4373081bba5bSNoriaki TAKAMIYA 4374081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 437553bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 43765b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 43775b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 437853bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4379761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4380761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 438153bd6749SJiri Pirko ifp->flags |= ifa_flags; 4382081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4383081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4384081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4385081bba5bSNoriaki TAKAMIYA 4386081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4387081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4388081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4389081bba5bSNoriaki TAKAMIYA 4390761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 439146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 43926f704992SYOSHIFUJI Hideaki expires, flags); 43935b84efecSThomas Haller } else if (had_prefixroute) { 43945b84efecSThomas Haller enum cleanup_prefix_rt_t action; 43955b84efecSThomas Haller unsigned long rt_expires; 43965b84efecSThomas Haller 43975b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 43985b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 43995b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 44005b84efecSThomas Haller 44015b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 44025b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 44035b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 44045b84efecSThomas Haller } 4405761aac73SThomas Haller } 440653bd6749SJiri Pirko 440753bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 440853bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 440953bd6749SJiri Pirko valid_lft = prefered_lft = 0; 441053bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 441153bd6749SJiri Pirko !was_managetempaddr, jiffies); 441253bd6749SJiri Pirko } 441353bd6749SJiri Pirko 4414c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4415081bba5bSNoriaki TAKAMIYA 4416081bba5bSNoriaki TAKAMIYA return 0; 4417081bba5bSNoriaki TAKAMIYA } 4418081bba5bSNoriaki TAKAMIYA 4419081bba5bSNoriaki TAKAMIYA static int 4420661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 44211da177e4SLinus Torvalds { 44223b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4423461d8837SThomas Graf struct ifaddrmsg *ifm; 4424461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4425caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 44267198f8ceSThomas Graf struct inet6_ifaddr *ifa; 44277198f8ceSThomas Graf struct net_device *dev; 442855ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4429479840ffSJiri Pirko u32 ifa_flags; 4430461d8837SThomas Graf int err; 44311da177e4SLinus Torvalds 4432461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4433461d8837SThomas Graf if (err < 0) 4434461d8837SThomas Graf return err; 4435461d8837SThomas Graf 4436461d8837SThomas Graf ifm = nlmsg_data(nlh); 4437caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 443863159f29SIan Morris if (!pfx) 44391da177e4SLinus Torvalds return -EINVAL; 44401da177e4SLinus Torvalds 4441461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 44420778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4443461d8837SThomas Graf 4444461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 44450778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4446461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4447461d8837SThomas Graf } else { 4448461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4449461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 44500778769dSNoriaki TAKAMIYA } 44510778769dSNoriaki TAKAMIYA 4452af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 445363159f29SIan Morris if (!dev) 44547198f8ceSThomas Graf return -ENODEV; 4455081bba5bSNoriaki TAKAMIYA 4456479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4457479840ffSJiri Pirko 445855ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4459761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 446093a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 446155ebaef1SNoriaki TAKAMIYA 44621cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 446363159f29SIan Morris if (!ifa) { 44647198f8ceSThomas Graf /* 44657198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4466db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 44677198f8ceSThomas Graf */ 4468caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4469af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4470af284937SDaniel Lezcano preferred_lft, valid_lft); 44711da177e4SLinus Torvalds } 44721da177e4SLinus Torvalds 44737198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 44747198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 44757198f8ceSThomas Graf err = -EEXIST; 44767198f8ceSThomas Graf else 447755ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 44787198f8ceSThomas Graf 44797198f8ceSThomas Graf in6_ifa_put(ifa); 44807198f8ceSThomas Graf 44817198f8ceSThomas Graf return err; 44827198f8ceSThomas Graf } 44837198f8ceSThomas Graf 4484479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4485101bb229SThomas Graf u8 scope, int ifindex) 4486101bb229SThomas Graf { 4487101bb229SThomas Graf struct ifaddrmsg *ifm; 4488101bb229SThomas Graf 4489101bb229SThomas Graf ifm = nlmsg_data(nlh); 4490101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4491101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4492101bb229SThomas Graf ifm->ifa_flags = flags; 4493101bb229SThomas Graf ifm->ifa_scope = scope; 4494101bb229SThomas Graf ifm->ifa_index = ifindex; 4495101bb229SThomas Graf } 4496101bb229SThomas Graf 449785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 449885486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 449985486af0SThomas Graf { 450085486af0SThomas Graf struct ifa_cacheinfo ci; 450185486af0SThomas Graf 450218a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 450318a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 450485486af0SThomas Graf ci.ifa_prefered = preferred; 450585486af0SThomas Graf ci.ifa_valid = valid; 450685486af0SThomas Graf 450785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 450885486af0SThomas Graf } 450985486af0SThomas Graf 4510101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4511101bb229SThomas Graf { 4512101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4513101bb229SThomas Graf return RT_SCOPE_HOST; 4514101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4515101bb229SThomas Graf return RT_SCOPE_LINK; 4516101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4517101bb229SThomas Graf return RT_SCOPE_SITE; 4518101bb229SThomas Graf else 4519101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4520101bb229SThomas Graf } 4521101bb229SThomas Graf 45220ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 45230ab6803bSThomas Graf { 4524339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4525caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4526339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4527479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4528479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 45290ab6803bSThomas Graf } 4530c5396a31SYOSHIFUJI Hideaki 45311da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 453215e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 45331da177e4SLinus Torvalds { 45341da177e4SLinus Torvalds struct nlmsghdr *nlh; 453585486af0SThomas Graf u32 preferred, valid; 45361da177e4SLinus Torvalds 453715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 453863159f29SIan Morris if (!nlh) 453926932566SPatrick McHardy return -EMSGSIZE; 45400ab6803bSThomas Graf 4541101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4542101bb229SThomas Graf ifa->idev->dev->ifindex); 4543101bb229SThomas Graf 4544fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4545fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 454685486af0SThomas Graf preferred = ifa->prefered_lft; 454785486af0SThomas Graf valid = ifa->valid_lft; 454885486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 45491da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4550a1faa698SJens Rosenboom if (preferred > tval) 455185486af0SThomas Graf preferred -= tval; 4552a1faa698SJens Rosenboom else 4553a1faa698SJens Rosenboom preferred = 0; 4554f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4555f56619fcSBen Hutchings if (valid > tval) 455685486af0SThomas Graf valid -= tval; 4557f56619fcSBen Hutchings else 4558f56619fcSBen Hutchings valid = 0; 4559f56619fcSBen Hutchings } 45601da177e4SLinus Torvalds } 45611da177e4SLinus Torvalds } else { 456285486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 456385486af0SThomas Graf valid = INFINITY_LIFE_TIME; 45641da177e4SLinus Torvalds } 456585486af0SThomas Graf 45667996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4567930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4568930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4569caeaba79SNicolas Dichtel goto error; 4570caeaba79SNicolas Dichtel } else 4571930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4572caeaba79SNicolas Dichtel goto error; 4573caeaba79SNicolas Dichtel 4574caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4575caeaba79SNicolas Dichtel goto error; 457685486af0SThomas Graf 4577479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4578479840ffSJiri Pirko goto error; 4579479840ffSJiri Pirko 4580053c095aSJohannes Berg nlmsg_end(skb, nlh); 4581053c095aSJohannes Berg return 0; 4582caeaba79SNicolas Dichtel 4583caeaba79SNicolas Dichtel error: 4584caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4585caeaba79SNicolas Dichtel return -EMSGSIZE; 45861da177e4SLinus Torvalds } 45871da177e4SLinus Torvalds 45881da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 458915e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 45901da177e4SLinus Torvalds { 45911da177e4SLinus Torvalds struct nlmsghdr *nlh; 4592101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4593101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 45941da177e4SLinus Torvalds 45951da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4596101bb229SThomas Graf scope = RT_SCOPE_SITE; 4597101bb229SThomas Graf 459815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 459963159f29SIan Morris if (!nlh) 460026932566SPatrick McHardy return -EMSGSIZE; 46010ab6803bSThomas Graf 4602101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4603930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 46040ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 460526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 460626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 460726932566SPatrick McHardy return -EMSGSIZE; 460826932566SPatrick McHardy } 460985486af0SThomas Graf 4610053c095aSJohannes Berg nlmsg_end(skb, nlh); 4611053c095aSJohannes Berg return 0; 46121da177e4SLinus Torvalds } 46131da177e4SLinus Torvalds 46141da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 461515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 46161da177e4SLinus Torvalds { 46171da177e4SLinus Torvalds struct nlmsghdr *nlh; 4618101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4619101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4622101bb229SThomas Graf scope = RT_SCOPE_SITE; 4623101bb229SThomas Graf 462415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 462563159f29SIan Morris if (!nlh) 462626932566SPatrick McHardy return -EMSGSIZE; 46270ab6803bSThomas Graf 4628101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4629930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 46300ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 463126932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 463226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 463326932566SPatrick McHardy return -EMSGSIZE; 463426932566SPatrick McHardy } 463585486af0SThomas Graf 4636053c095aSJohannes Berg nlmsg_end(skb, nlh); 4637053c095aSJohannes Berg return 0; 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds 4640e21e8467SStephen Hemminger enum addr_type_t { 46411da177e4SLinus Torvalds UNICAST_ADDR, 46421da177e4SLinus Torvalds MULTICAST_ADDR, 46431da177e4SLinus Torvalds ANYCAST_ADDR, 46441da177e4SLinus Torvalds }; 46451da177e4SLinus Torvalds 4646234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4647234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4648234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4649234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 46501da177e4SLinus Torvalds { 46511da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 46521da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4653234b27c3SEric Dumazet int err = 1; 4654234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 46551da177e4SLinus Torvalds 46561da177e4SLinus Torvalds read_lock_bh(&idev->lock); 46571da177e4SLinus Torvalds switch (type) { 4658502a2ffdSstephen hemminger case UNICAST_ADDR: { 4659502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4660502a2ffdSstephen hemminger 4661ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4662502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4663502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 46641da177e4SLinus Torvalds continue; 46655d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 466615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46675d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46685d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 46695d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4670053c095aSJohannes Berg if (err < 0) 4671bcd32326SEric Dumazet break; 467263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 46731da177e4SLinus Torvalds } 46741da177e4SLinus Torvalds break; 4675502a2ffdSstephen hemminger } 46761da177e4SLinus Torvalds case MULTICAST_ADDR: 46771da177e4SLinus Torvalds /* multicast address */ 46781da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 46791da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 46801da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 46811da177e4SLinus Torvalds continue; 46825d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 468315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46845d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46855d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 46865d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4687053c095aSJohannes Berg if (err < 0) 4688bcd32326SEric Dumazet break; 46891da177e4SLinus Torvalds } 46901da177e4SLinus Torvalds break; 46911da177e4SLinus Torvalds case ANYCAST_ADDR: 46921da177e4SLinus Torvalds /* anycast address */ 46931da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 46941da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 46951da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 46961da177e4SLinus Torvalds continue; 46975d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 469815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46995d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47005d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 47015d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4702053c095aSJohannes Berg if (err < 0) 4703bcd32326SEric Dumazet break; 47041da177e4SLinus Torvalds } 47051da177e4SLinus Torvalds break; 47061da177e4SLinus Torvalds default: 47071da177e4SLinus Torvalds break; 47081da177e4SLinus Torvalds } 47091da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4710234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4711234b27c3SEric Dumazet return err; 4712234b27c3SEric Dumazet } 47135d5619b4SYOSHIFUJI Hideaki 4714234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4715234b27c3SEric Dumazet enum addr_type_t type) 4716234b27c3SEric Dumazet { 4717234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4718234b27c3SEric Dumazet int h, s_h; 4719234b27c3SEric Dumazet int idx, ip_idx; 4720234b27c3SEric Dumazet int s_idx, s_ip_idx; 4721234b27c3SEric Dumazet struct net_device *dev; 4722234b27c3SEric Dumazet struct inet6_dev *idev; 4723234b27c3SEric Dumazet struct hlist_head *head; 4724234b27c3SEric Dumazet 4725234b27c3SEric Dumazet s_h = cb->args[0]; 4726234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4727234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4728234b27c3SEric Dumazet 4729234b27c3SEric Dumazet rcu_read_lock(); 473063998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4731234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4732234b27c3SEric Dumazet idx = 0; 4733234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4734b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4735234b27c3SEric Dumazet if (idx < s_idx) 4736234b27c3SEric Dumazet goto cont; 47374b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4738234b27c3SEric Dumazet s_ip_idx = 0; 4739234b27c3SEric Dumazet ip_idx = 0; 4740e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4741e21e8467SStephen Hemminger if (!idev) 4742234b27c3SEric Dumazet goto cont; 4743234b27c3SEric Dumazet 4744234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 47457b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4746234b27c3SEric Dumazet goto done; 47477562f876SPavel Emelianov cont: 47487562f876SPavel Emelianov idx++; 47491da177e4SLinus Torvalds } 4750234b27c3SEric Dumazet } 4751234b27c3SEric Dumazet done: 4752234b27c3SEric Dumazet rcu_read_unlock(); 4753234b27c3SEric Dumazet cb->args[0] = h; 4754234b27c3SEric Dumazet cb->args[1] = idx; 4755234b27c3SEric Dumazet cb->args[2] = ip_idx; 4756234b27c3SEric Dumazet 47571da177e4SLinus Torvalds return skb->len; 47581da177e4SLinus Torvalds } 47591da177e4SLinus Torvalds 47601da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 47611da177e4SLinus Torvalds { 47621da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4763b854272bSDenis V. Lunev 47641da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47651da177e4SLinus Torvalds } 47661da177e4SLinus Torvalds 47671da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 47681da177e4SLinus Torvalds { 47691da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4770b854272bSDenis V. Lunev 47711da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47721da177e4SLinus Torvalds } 47731da177e4SLinus Torvalds 47741da177e4SLinus Torvalds 47751da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 47761da177e4SLinus Torvalds { 47771da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4778b854272bSDenis V. Lunev 47791da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47801da177e4SLinus Torvalds } 47811da177e4SLinus Torvalds 4782661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 47836c223828SNoriaki TAKAMIYA { 47843b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 47851b29fc2cSThomas Graf struct ifaddrmsg *ifm; 47861b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4787caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 47886c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 47896c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 47906c223828SNoriaki TAKAMIYA struct sk_buff *skb; 47916c223828SNoriaki TAKAMIYA int err; 47926c223828SNoriaki TAKAMIYA 47931b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 47941b29fc2cSThomas Graf if (err < 0) 47951b29fc2cSThomas Graf goto errout; 47966c223828SNoriaki TAKAMIYA 4797caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 479863159f29SIan Morris if (!addr) { 47991b29fc2cSThomas Graf err = -EINVAL; 48001b29fc2cSThomas Graf goto errout; 48011b29fc2cSThomas Graf } 48021b29fc2cSThomas Graf 48031b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 48046c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 48056fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 48066c223828SNoriaki TAKAMIYA 4807e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4808e21e8467SStephen Hemminger if (!ifa) { 48091b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 48101b29fc2cSThomas Graf goto errout; 48116c223828SNoriaki TAKAMIYA } 48126c223828SNoriaki TAKAMIYA 4813e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4814e21e8467SStephen Hemminger if (!skb) { 48151b29fc2cSThomas Graf err = -ENOBUFS; 48161b29fc2cSThomas Graf goto errout_ifa; 48171b29fc2cSThomas Graf } 48181b29fc2cSThomas Graf 481915e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 48206c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 482126932566SPatrick McHardy if (err < 0) { 482226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 482326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 482426932566SPatrick McHardy kfree_skb(skb); 482526932566SPatrick McHardy goto errout_ifa; 482626932566SPatrick McHardy } 482715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 48281b29fc2cSThomas Graf errout_ifa: 48296c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 48301b29fc2cSThomas Graf errout: 48316c223828SNoriaki TAKAMIYA return err; 48326c223828SNoriaki TAKAMIYA } 48336c223828SNoriaki TAKAMIYA 48341da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 48351da177e4SLinus Torvalds { 48361da177e4SLinus Torvalds struct sk_buff *skb; 4837c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 48385d620266SThomas Graf int err = -ENOBUFS; 48391da177e4SLinus Torvalds 48400ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 484163159f29SIan Morris if (!skb) 48425d620266SThomas Graf goto errout; 48435d620266SThomas Graf 48445d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 484526932566SPatrick McHardy if (err < 0) { 484626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 484726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 484826932566SPatrick McHardy kfree_skb(skb); 484926932566SPatrick McHardy goto errout; 485026932566SPatrick McHardy } 48511ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 48521ce85fe4SPablo Neira Ayuso return; 48535d620266SThomas Graf errout: 48545d620266SThomas Graf if (err < 0) 48556fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 4858b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 48591da177e4SLinus Torvalds __s32 *array, int bytes) 48601da177e4SLinus Torvalds { 486104561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 486204561c1fSThomas Graf 48631da177e4SLinus Torvalds memset(array, 0, bytes); 48641da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 48651da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 48661da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 48671da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 48681da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 48691da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 48701da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 48711da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 487293908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 487393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 487493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 487593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 48761da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4877fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4878fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4879fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4880fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 48811da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 48821da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 48831da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 48841da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 48851da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 48861da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 488765f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 48888013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 4889c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4890930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4891930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 489293908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 489393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4894fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 489509c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 489609c884d4SYOSHIFUJI Hideaki #endif 4897930d6ff2SYOSHIFUJI Hideaki #endif 4898fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 48990bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 490095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 490195c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 49027fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 490395c385b4SNeil Horman #endif 49047bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 49057bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 49067bc570c8SYOSHIFUJI Hideaki #endif 4907778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 49081b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4909c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 49105cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4911b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4912d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4913c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 491435103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 49153d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 49163985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 4917abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 49187a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 4919f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 49201da177e4SLinus Torvalds } 49211da177e4SLinus Torvalds 4922b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4923b382b191SThomas Graf { 4924b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4925b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4926b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4927b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4928f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4929f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4930b382b191SThomas Graf } 4931b382b191SThomas Graf 4932339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4933339bf98fSThomas Graf { 4934339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4935339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4936339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4937339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4938339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 49390344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 4940b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4941339bf98fSThomas Graf } 4942c5396a31SYOSHIFUJI Hideaki 4943be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 49447d720c3eSTejun Heo int items, int bytes) 49457f7d9a6bSHerbert Xu { 49467f7d9a6bSHerbert Xu int i; 49477f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 49487f7d9a6bSHerbert Xu BUG_ON(pad < 0); 49497f7d9a6bSHerbert Xu 49507f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 49517f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 49527f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 4953be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 49547f7d9a6bSHerbert Xu 49557f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 49567f7d9a6bSHerbert Xu } 49577f7d9a6bSHerbert Xu 4958698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 4959a3a77372SRaghavendra K T int bytes, size_t syncpoff) 49604ce3c183SEric Dumazet { 4961a3a77372SRaghavendra K T int i, c; 4962a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 4963a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 4964a3a77372SRaghavendra K T 49654ce3c183SEric Dumazet BUG_ON(pad < 0); 49664ce3c183SEric Dumazet 4967a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 4968a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 49694ce3c183SEric Dumazet 4970a3a77372SRaghavendra K T for_each_possible_cpu(c) { 4971a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 4972a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 4973a3a77372SRaghavendra K T } 4974a3a77372SRaghavendra K T 4975a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 4976a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 49774ce3c183SEric Dumazet } 49784ce3c183SEric Dumazet 49797f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 49807f7d9a6bSHerbert Xu int bytes) 49817f7d9a6bSHerbert Xu { 49827f7d9a6bSHerbert Xu switch (attrtype) { 49837f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 4984a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 4985a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 49867f7d9a6bSHerbert Xu break; 49877f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 4988be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 49897f7d9a6bSHerbert Xu break; 49907f7d9a6bSHerbert Xu } 49917f7d9a6bSHerbert Xu } 49927f7d9a6bSHerbert Xu 4993d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 4994d5566fd7SSowmini Varadhan u32 ext_filter_mask) 49951da177e4SLinus Torvalds { 4996bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 49971da177e4SLinus Torvalds struct ifla_cacheinfo ci; 49981da177e4SLinus Torvalds 4999c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5000c78679e8SDavid S. Miller goto nla_put_failure; 50011da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 500224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 500324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 50041f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5005c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5006c78679e8SDavid S. Miller goto nla_put_failure; 5007bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 500863159f29SIan Morris if (!nla) 500904561c1fSThomas Graf goto nla_put_failure; 5010bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 50111da177e4SLinus Torvalds 5012bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5013bf99f1bdSYOSHIFUJI Hideaki 5014d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5015d5566fd7SSowmini Varadhan return 0; 5016d5566fd7SSowmini Varadhan 5017bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 501863159f29SIan Morris if (!nla) 5019bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5020bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5021bf99f1bdSYOSHIFUJI Hideaki 5022bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 502363159f29SIan Morris if (!nla) 5024bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5025bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 50261da177e4SLinus Torvalds 5027f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 502863159f29SIan Morris if (!nla) 5029f53adae4SDaniel Borkmann goto nla_put_failure; 5030bc91b0f0SJiri Pirko 5031bc91b0f0SJiri Pirko if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 5032bc91b0f0SJiri Pirko goto nla_put_failure; 5033bc91b0f0SJiri Pirko 5034f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5035f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5036f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5037f53adae4SDaniel Borkmann 5038b382b191SThomas Graf return 0; 5039b382b191SThomas Graf 5040b382b191SThomas Graf nla_put_failure: 5041b382b191SThomas Graf return -EMSGSIZE; 5042b382b191SThomas Graf } 5043b382b191SThomas Graf 5044b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5045b1974ed0SArad, Ronen u32 ext_filter_mask) 5046b382b191SThomas Graf { 5047b382b191SThomas Graf if (!__in6_dev_get(dev)) 5048b382b191SThomas Graf return 0; 5049b382b191SThomas Graf 5050b382b191SThomas Graf return inet6_ifla6_size(); 5051b382b191SThomas Graf } 5052b382b191SThomas Graf 5053d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5054d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5055b382b191SThomas Graf { 5056b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5057b382b191SThomas Graf 5058b382b191SThomas Graf if (!idev) 5059b382b191SThomas Graf return -ENODATA; 5060b382b191SThomas Graf 5061d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5062b382b191SThomas Graf return -EMSGSIZE; 5063b382b191SThomas Graf 5064b382b191SThomas Graf return 0; 5065b382b191SThomas Graf } 5066b382b191SThomas Graf 5067f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5068f53adae4SDaniel Borkmann { 5069f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5070f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 507147e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5072dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5073f53adae4SDaniel Borkmann 5074c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5075c15b1ccaSHannes Frederic Sowa 507663159f29SIan Morris if (!token) 5077f53adae4SDaniel Borkmann return -EINVAL; 5078f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5079f53adae4SDaniel Borkmann return -EINVAL; 5080f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5081f53adae4SDaniel Borkmann return -EINVAL; 5082f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 5083f53adae4SDaniel Borkmann return -EINVAL; 5084f53adae4SDaniel Borkmann 5085f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5086f53adae4SDaniel Borkmann 5087f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5088f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5089f53adae4SDaniel Borkmann 5090f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5091f53adae4SDaniel Borkmann 509247e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 509347e27d5eSDaniel Borkmann if (clear_token) 509447e27d5eSDaniel Borkmann goto update_lft; 509547e27d5eSDaniel Borkmann 5096dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5097dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5098dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5099fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5100fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5101fc403832SDaniel Borkmann */ 5102f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5103fc403832SDaniel Borkmann update_rs = true; 5104fc403832SDaniel Borkmann } 5105f53adae4SDaniel Borkmann 510647e27d5eSDaniel Borkmann update_lft: 5107f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5108fc403832SDaniel Borkmann 510977ecaaceSHannes Frederic Sowa if (update_rs) { 5110f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 511177ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 511277ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 511377ecaaceSHannes Frederic Sowa } 5114f53adae4SDaniel Borkmann 5115f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5116f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5117f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5118617fe29dSDaniel Borkmann if (ifp->tokenized) { 5119f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5120f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5121f53adae4SDaniel Borkmann } 5122f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5123f53adae4SDaniel Borkmann } 5124f53adae4SDaniel Borkmann 5125f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5126b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5127c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5128f53adae4SDaniel Borkmann return 0; 5129f53adae4SDaniel Borkmann } 5130f53adae4SDaniel Borkmann 513111b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 513211b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 513311b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 513411b1f828SDaniel Borkmann }; 513511b1f828SDaniel Borkmann 513611b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 513711b1f828SDaniel Borkmann const struct nlattr *nla) 513811b1f828SDaniel Borkmann { 513911b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 514011b1f828SDaniel Borkmann 514111b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 514211b1f828SDaniel Borkmann return -EAFNOSUPPORT; 514311b1f828SDaniel Borkmann 514411b1f828SDaniel Borkmann return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 514511b1f828SDaniel Borkmann } 514611b1f828SDaniel Borkmann 5147f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5148f53adae4SDaniel Borkmann { 5149f53adae4SDaniel Borkmann int err = -EINVAL; 5150f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5151f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5152f53adae4SDaniel Borkmann 5153f53adae4SDaniel Borkmann if (!idev) 5154f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5155f53adae4SDaniel Borkmann 5156f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 5157f53adae4SDaniel Borkmann BUG(); 5158f53adae4SDaniel Borkmann 5159bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5160f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5161bc91b0f0SJiri Pirko if (err) 5162bc91b0f0SJiri Pirko return err; 5163bc91b0f0SJiri Pirko } 5164bc91b0f0SJiri Pirko 5165bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5166bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5167bc91b0f0SJiri Pirko 5168bc91b0f0SJiri Pirko if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5169622c81d5SHannes Frederic Sowa mode != IN6_ADDR_GEN_MODE_NONE && 5170cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5171cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_RANDOM) 5172bc91b0f0SJiri Pirko return -EINVAL; 5173622c81d5SHannes Frederic Sowa 5174622c81d5SHannes Frederic Sowa if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5175622c81d5SHannes Frederic Sowa !idev->cnf.stable_secret.initialized && 5176622c81d5SHannes Frederic Sowa !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) 5177622c81d5SHannes Frederic Sowa return -EINVAL; 5178622c81d5SHannes Frederic Sowa 5179bc91b0f0SJiri Pirko idev->addr_gen_mode = mode; 5180bc91b0f0SJiri Pirko err = 0; 5181bc91b0f0SJiri Pirko } 5182f53adae4SDaniel Borkmann 5183f53adae4SDaniel Borkmann return err; 5184f53adae4SDaniel Borkmann } 5185f53adae4SDaniel Borkmann 5186b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 518715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5188b382b191SThomas Graf { 5189b382b191SThomas Graf struct net_device *dev = idev->dev; 5190b382b191SThomas Graf struct ifinfomsg *hdr; 5191b382b191SThomas Graf struct nlmsghdr *nlh; 5192b382b191SThomas Graf void *protoinfo; 5193b382b191SThomas Graf 519415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 519563159f29SIan Morris if (!nlh) 5196b382b191SThomas Graf return -EMSGSIZE; 5197b382b191SThomas Graf 5198b382b191SThomas Graf hdr = nlmsg_data(nlh); 5199b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5200b382b191SThomas Graf hdr->__ifi_pad = 0; 5201b382b191SThomas Graf hdr->ifi_type = dev->type; 5202b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5203b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5204b382b191SThomas Graf hdr->ifi_change = 0; 5205b382b191SThomas Graf 5206c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5207c78679e8SDavid S. Miller (dev->addr_len && 5208c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5209c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5210a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 52110344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 52120344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 52130344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5214c78679e8SDavid S. Miller goto nla_put_failure; 5215b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 521663159f29SIan Morris if (!protoinfo) 5217b382b191SThomas Graf goto nla_put_failure; 5218b382b191SThomas Graf 5219d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5220b382b191SThomas Graf goto nla_put_failure; 5221b382b191SThomas Graf 522204561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5223053c095aSJohannes Berg nlmsg_end(skb, nlh); 5224053c095aSJohannes Berg return 0; 52251da177e4SLinus Torvalds 522604561c1fSThomas Graf nla_put_failure: 522726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 522826932566SPatrick McHardy return -EMSGSIZE; 52291da177e4SLinus Torvalds } 52301da177e4SLinus Torvalds 52311da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 52321da177e4SLinus Torvalds { 52333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 523484d2697dSEric Dumazet int h, s_h; 5235434a8a58SDavid S. Miller int idx = 0, s_idx; 52361da177e4SLinus Torvalds struct net_device *dev; 52371da177e4SLinus Torvalds struct inet6_dev *idev; 523884d2697dSEric Dumazet struct hlist_head *head; 52391da177e4SLinus Torvalds 524084d2697dSEric Dumazet s_h = cb->args[0]; 524184d2697dSEric Dumazet s_idx = cb->args[1]; 524284d2697dSEric Dumazet 524384d2697dSEric Dumazet rcu_read_lock(); 524484d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 52457562f876SPavel Emelianov idx = 0; 524684d2697dSEric Dumazet head = &net->dev_index_head[h]; 5247b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 52481da177e4SLinus Torvalds if (idx < s_idx) 52497562f876SPavel Emelianov goto cont; 525084d2697dSEric Dumazet idev = __in6_dev_get(dev); 525184d2697dSEric Dumazet if (!idev) 52527562f876SPavel Emelianov goto cont; 525384d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 525415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 525584d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5256053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 525784d2697dSEric Dumazet goto out; 52587562f876SPavel Emelianov cont: 52597562f876SPavel Emelianov idx++; 52601da177e4SLinus Torvalds } 526184d2697dSEric Dumazet } 526284d2697dSEric Dumazet out: 526384d2697dSEric Dumazet rcu_read_unlock(); 526484d2697dSEric Dumazet cb->args[1] = idx; 526584d2697dSEric Dumazet cb->args[0] = h; 52661da177e4SLinus Torvalds 52671da177e4SLinus Torvalds return skb->len; 52681da177e4SLinus Torvalds } 52691da177e4SLinus Torvalds 52701da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 52711da177e4SLinus Torvalds { 52721da177e4SLinus Torvalds struct sk_buff *skb; 5273c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 52748d7a76c9SThomas Graf int err = -ENOBUFS; 52751da177e4SLinus Torvalds 5276339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 527763159f29SIan Morris if (!skb) 52788d7a76c9SThomas Graf goto errout; 52798d7a76c9SThomas Graf 52808d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 528126932566SPatrick McHardy if (err < 0) { 528226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 528326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 528426932566SPatrick McHardy kfree_skb(skb); 528526932566SPatrick McHardy goto errout; 528626932566SPatrick McHardy } 52875f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 52881ce85fe4SPablo Neira Ayuso return; 52898d7a76c9SThomas Graf errout: 52908d7a76c9SThomas Graf if (err < 0) 52915f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 52921da177e4SLinus Torvalds } 52931da177e4SLinus Torvalds 5294339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5295339bf98fSThomas Graf { 5296339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5297339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5298339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5299339bf98fSThomas Graf } 5300c5396a31SYOSHIFUJI Hideaki 53011da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 530215e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5303b6544c0bSJamal Hadi Salim int event, unsigned int flags) 53041da177e4SLinus Torvalds { 53051da177e4SLinus Torvalds struct prefixmsg *pmsg; 53061da177e4SLinus Torvalds struct nlmsghdr *nlh; 53071da177e4SLinus Torvalds struct prefix_cacheinfo ci; 53081da177e4SLinus Torvalds 530915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 531063159f29SIan Morris if (!nlh) 531126932566SPatrick McHardy return -EMSGSIZE; 53126051e2f4SThomas Graf 53136051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 53141da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 53158a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 53168a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 53171da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 53181da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 53191da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 53208a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 53211da177e4SLinus Torvalds pmsg->prefix_flags = 0; 53221da177e4SLinus Torvalds if (pinfo->onlink) 53231da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 53241da177e4SLinus Torvalds if (pinfo->autoconf) 53251da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 53261da177e4SLinus Torvalds 5327c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5328c78679e8SDavid S. Miller goto nla_put_failure; 53291da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 53301da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5331c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5332c78679e8SDavid S. Miller goto nla_put_failure; 5333053c095aSJohannes Berg nlmsg_end(skb, nlh); 5334053c095aSJohannes Berg return 0; 53351da177e4SLinus Torvalds 53366051e2f4SThomas Graf nla_put_failure: 533726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 533826932566SPatrick McHardy return -EMSGSIZE; 53391da177e4SLinus Torvalds } 53401da177e4SLinus Torvalds 53411da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 53421da177e4SLinus Torvalds struct prefix_info *pinfo) 53431da177e4SLinus Torvalds { 53441da177e4SLinus Torvalds struct sk_buff *skb; 5345c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 53468c384bfaSThomas Graf int err = -ENOBUFS; 53471da177e4SLinus Torvalds 5348339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 534963159f29SIan Morris if (!skb) 53508c384bfaSThomas Graf goto errout; 53518c384bfaSThomas Graf 53528c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 535326932566SPatrick McHardy if (err < 0) { 535426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 535526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 535626932566SPatrick McHardy kfree_skb(skb); 535726932566SPatrick McHardy goto errout; 535826932566SPatrick McHardy } 53591ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 53601ce85fe4SPablo Neira Ayuso return; 53618c384bfaSThomas Graf errout: 53628c384bfaSThomas Graf if (err < 0) 53636fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 53641da177e4SLinus Torvalds } 53651da177e4SLinus Torvalds 53661da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 53671da177e4SLinus Torvalds { 536863998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 536963998ac2SNicolas Dichtel 5370c15b1ccaSHannes Frederic Sowa if (event) 5371c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5372c15b1ccaSHannes Frederic Sowa 53731da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 53741da177e4SLinus Torvalds 53751da177e4SLinus Torvalds switch (event) { 53761da177e4SLinus Torvalds case RTM_NEWADDR: 537795c385b4SNeil Horman /* 537895c385b4SNeil Horman * If the address was optimistic 537995c385b4SNeil Horman * we inserted the route at the start of 538095c385b4SNeil Horman * our DAD process, so we don't need 538195c385b4SNeil Horman * to do it again 538295c385b4SNeil Horman */ 538395c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 538440e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 53851da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 53861da177e4SLinus Torvalds addrconf_join_anycast(ifp); 53877996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5388caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5389caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 53901da177e4SLinus Torvalds break; 53911da177e4SLinus Torvalds case RTM_DELADDR: 53921da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 53931da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 53941da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 53957996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5396caeaba79SNicolas Dichtel struct rt6_info *rt; 5397caeaba79SNicolas Dichtel 5398e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5399e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 54008e3d5be7SMartin KaFai Lau if (rt) 54018e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5402caeaba79SNicolas Dichtel } 540338bd10c4SDavid Ahern if (ifp->rt) { 5404d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 54058e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 540638bd10c4SDavid Ahern } 5407705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 54081da177e4SLinus Torvalds break; 54091da177e4SLinus Torvalds } 541063998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 54111da177e4SLinus Torvalds } 54121da177e4SLinus Torvalds 54131da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 54141da177e4SLinus Torvalds { 54158814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 54161da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 54171da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 54188814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 54191da177e4SLinus Torvalds } 54201da177e4SLinus Torvalds 54211da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 54221da177e4SLinus Torvalds 54231da177e4SLinus Torvalds static 5424fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 54251da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 54261da177e4SLinus Torvalds { 54271da177e4SLinus Torvalds int *valp = ctl->data; 54281da177e4SLinus Torvalds int val = *valp; 542988af182eSEric W. Biederman loff_t pos = *ppos; 5430fe2c6338SJoe Perches struct ctl_table lctl; 54311da177e4SLinus Torvalds int ret; 54321da177e4SLinus Torvalds 5433013d97e9SFrancesco Ruggeri /* 5434013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5435013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5436013d97e9SFrancesco Ruggeri */ 5437013d97e9SFrancesco Ruggeri lctl = *ctl; 5438013d97e9SFrancesco Ruggeri lctl.data = &val; 5439013d97e9SFrancesco Ruggeri 5440013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 54411da177e4SLinus Torvalds 5442c8fecf22SPavel Emelyanov if (write) 5443b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 544488af182eSEric W. Biederman if (ret) 544588af182eSEric W. Biederman *ppos = pos; 54461da177e4SLinus Torvalds return ret; 54471da177e4SLinus Torvalds } 54481da177e4SLinus Torvalds 544977751427SMarcelo Leitner static 5450d6df198dSPhil Sutter int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write, 5451d6df198dSPhil Sutter void __user *buffer, size_t *lenp, loff_t *ppos) 5452d6df198dSPhil Sutter { 5453d6df198dSPhil Sutter struct ctl_table lctl; 5454d6df198dSPhil Sutter int min_hl = 1, max_hl = 255; 5455d6df198dSPhil Sutter 5456d6df198dSPhil Sutter lctl = *ctl; 5457d6df198dSPhil Sutter lctl.extra1 = &min_hl; 5458d6df198dSPhil Sutter lctl.extra2 = &max_hl; 5459d6df198dSPhil Sutter 5460d6df198dSPhil Sutter return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 5461d6df198dSPhil Sutter } 5462d6df198dSPhil Sutter 5463d6df198dSPhil Sutter static 546477751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 546577751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 546677751427SMarcelo Leitner { 546777751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 546877751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 546977751427SMarcelo Leitner struct ctl_table lctl; 547077751427SMarcelo Leitner 547177751427SMarcelo Leitner lctl = *ctl; 547277751427SMarcelo Leitner lctl.extra1 = &min_mtu; 547377751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 547477751427SMarcelo Leitner 547577751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 547677751427SMarcelo Leitner } 547777751427SMarcelo Leitner 547856d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 547956d417b1SBrian Haley { 548075538c2bSCong Wang struct netdev_notifier_info info; 548175538c2bSCong Wang 548256d417b1SBrian Haley if (!idev || !idev->dev) 548356d417b1SBrian Haley return; 548456d417b1SBrian Haley 548575538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 548656d417b1SBrian Haley if (idev->cnf.disable_ipv6) 548775538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 548856d417b1SBrian Haley else 548975538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 549056d417b1SBrian Haley } 549156d417b1SBrian Haley 549256d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 549356d417b1SBrian Haley { 549456d417b1SBrian Haley struct net_device *dev; 549556d417b1SBrian Haley struct inet6_dev *idev; 549656d417b1SBrian Haley 549756d417b1SBrian Haley rcu_read_lock(); 5498c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 549956d417b1SBrian Haley idev = __in6_dev_get(dev); 550056d417b1SBrian Haley if (idev) { 550156d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 550256d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 550356d417b1SBrian Haley if (changed) 550456d417b1SBrian Haley dev_disable_change(idev); 550556d417b1SBrian Haley } 550656d417b1SBrian Haley } 5507c6d14c84SEric Dumazet rcu_read_unlock(); 550856d417b1SBrian Haley } 550956d417b1SBrian Haley 5510013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 551156d417b1SBrian Haley { 551256d417b1SBrian Haley struct net *net; 5513013d97e9SFrancesco Ruggeri int old; 5514013d97e9SFrancesco Ruggeri 5515013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5516013d97e9SFrancesco Ruggeri return restart_syscall(); 551756d417b1SBrian Haley 551856d417b1SBrian Haley net = (struct net *)table->extra2; 5519013d97e9SFrancesco Ruggeri old = *p; 5520013d97e9SFrancesco Ruggeri *p = newf; 552156d417b1SBrian Haley 5522013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5523013d97e9SFrancesco Ruggeri rtnl_unlock(); 552456d417b1SBrian Haley return 0; 552588af182eSEric W. Biederman } 552656d417b1SBrian Haley 552756d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 552856d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 552956d417b1SBrian Haley addrconf_disable_change(net, newf); 5530013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 553156d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 553256d417b1SBrian Haley 553356d417b1SBrian Haley rtnl_unlock(); 553456d417b1SBrian Haley return 0; 553556d417b1SBrian Haley } 553656d417b1SBrian Haley 553756d417b1SBrian Haley static 5538fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 553956d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 554056d417b1SBrian Haley { 554156d417b1SBrian Haley int *valp = ctl->data; 554256d417b1SBrian Haley int val = *valp; 554388af182eSEric W. Biederman loff_t pos = *ppos; 5544fe2c6338SJoe Perches struct ctl_table lctl; 554556d417b1SBrian Haley int ret; 554656d417b1SBrian Haley 5547013d97e9SFrancesco Ruggeri /* 5548013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5549013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5550013d97e9SFrancesco Ruggeri */ 5551013d97e9SFrancesco Ruggeri lctl = *ctl; 5552013d97e9SFrancesco Ruggeri lctl.data = &val; 5553013d97e9SFrancesco Ruggeri 5554013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 555556d417b1SBrian Haley 555656d417b1SBrian Haley if (write) 555756d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 555888af182eSEric W. Biederman if (ret) 555988af182eSEric W. Biederman *ppos = pos; 556056d417b1SBrian Haley return ret; 556156d417b1SBrian Haley } 556256d417b1SBrian Haley 5563c92d5491Sstephen hemminger static 5564c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5565c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5566c92d5491Sstephen hemminger { 5567c92d5491Sstephen hemminger int *valp = ctl->data; 5568c92d5491Sstephen hemminger int ret; 5569c92d5491Sstephen hemminger int old, new; 5570c92d5491Sstephen hemminger 5571c92d5491Sstephen hemminger old = *valp; 5572c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5573c92d5491Sstephen hemminger new = *valp; 5574c92d5491Sstephen hemminger 5575c92d5491Sstephen hemminger if (write && old != new) { 5576c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5577c92d5491Sstephen hemminger 5578c92d5491Sstephen hemminger if (!rtnl_trylock()) 5579c92d5491Sstephen hemminger return restart_syscall(); 5580c92d5491Sstephen hemminger 5581c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5582c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5583c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5584c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5585c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5586c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5587c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5588c92d5491Sstephen hemminger net->ipv6.devconf_all); 5589c92d5491Sstephen hemminger else { 5590c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5591c92d5491Sstephen hemminger 5592c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5593c92d5491Sstephen hemminger idev->dev->ifindex, 5594c92d5491Sstephen hemminger &idev->cnf); 5595c92d5491Sstephen hemminger } 5596c92d5491Sstephen hemminger rtnl_unlock(); 5597c92d5491Sstephen hemminger } 5598c92d5491Sstephen hemminger 5599c92d5491Sstephen hemminger return ret; 5600c92d5491Sstephen hemminger } 5601c92d5491Sstephen hemminger 56023d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 56033d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 56043d1bec99SHannes Frederic Sowa loff_t *ppos) 56053d1bec99SHannes Frederic Sowa { 56063d1bec99SHannes Frederic Sowa int err; 56073d1bec99SHannes Frederic Sowa struct in6_addr addr; 56083d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 56093d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5610622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 56113d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 56123d1bec99SHannes Frederic Sowa 5613622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5614622c81d5SHannes Frederic Sowa return -EIO; 5615622c81d5SHannes Frederic Sowa 56163d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 56173d1bec99SHannes Frederic Sowa lctl.data = str; 56183d1bec99SHannes Frederic Sowa 56193d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 56203d1bec99SHannes Frederic Sowa return restart_syscall(); 56213d1bec99SHannes Frederic Sowa 56223d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 56233d1bec99SHannes Frederic Sowa err = -EIO; 56243d1bec99SHannes Frederic Sowa goto out; 56253d1bec99SHannes Frederic Sowa } 56263d1bec99SHannes Frederic Sowa 56275449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 56283d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 56293d1bec99SHannes Frederic Sowa err = -EIO; 56303d1bec99SHannes Frederic Sowa goto out; 56313d1bec99SHannes Frederic Sowa } 56323d1bec99SHannes Frederic Sowa 56333d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 56343d1bec99SHannes Frederic Sowa if (err || !write) 56353d1bec99SHannes Frederic Sowa goto out; 56363d1bec99SHannes Frederic Sowa 56373d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 56383d1bec99SHannes Frederic Sowa err = -EIO; 56393d1bec99SHannes Frederic Sowa goto out; 56403d1bec99SHannes Frederic Sowa } 56413d1bec99SHannes Frederic Sowa 56423d1bec99SHannes Frederic Sowa secret->initialized = true; 56433d1bec99SHannes Frederic Sowa secret->secret = addr; 56443d1bec99SHannes Frederic Sowa 5645622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5646622c81d5SHannes Frederic Sowa struct net_device *dev; 5647622c81d5SHannes Frederic Sowa 5648622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5649622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5650622c81d5SHannes Frederic Sowa 5651622c81d5SHannes Frederic Sowa if (idev) { 5652622c81d5SHannes Frederic Sowa idev->addr_gen_mode = 5653622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5654622c81d5SHannes Frederic Sowa } 5655622c81d5SHannes Frederic Sowa } 5656622c81d5SHannes Frederic Sowa } else { 5657622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5658622c81d5SHannes Frederic Sowa 5659622c81d5SHannes Frederic Sowa idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5660622c81d5SHannes Frederic Sowa } 5661622c81d5SHannes Frederic Sowa 56623d1bec99SHannes Frederic Sowa out: 56633d1bec99SHannes Frederic Sowa rtnl_unlock(); 56643d1bec99SHannes Frederic Sowa 56653d1bec99SHannes Frederic Sowa return err; 56663d1bec99SHannes Frederic Sowa } 5667c92d5491Sstephen hemminger 566835103d11SAndy Gospodarek static 566935103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 567035103d11SAndy Gospodarek int write, 567135103d11SAndy Gospodarek void __user *buffer, 567235103d11SAndy Gospodarek size_t *lenp, 567335103d11SAndy Gospodarek loff_t *ppos) 567435103d11SAndy Gospodarek { 567535103d11SAndy Gospodarek int *valp = ctl->data; 567635103d11SAndy Gospodarek int val = *valp; 567735103d11SAndy Gospodarek loff_t pos = *ppos; 567835103d11SAndy Gospodarek struct ctl_table lctl; 567935103d11SAndy Gospodarek int ret; 568035103d11SAndy Gospodarek 568135103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 568235103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 568335103d11SAndy Gospodarek */ 568435103d11SAndy Gospodarek lctl = *ctl; 568535103d11SAndy Gospodarek lctl.data = &val; 568635103d11SAndy Gospodarek 568735103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 568835103d11SAndy Gospodarek 568935103d11SAndy Gospodarek if (write) 569035103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 569135103d11SAndy Gospodarek if (ret) 569235103d11SAndy Gospodarek *ppos = pos; 569335103d11SAndy Gospodarek return ret; 569435103d11SAndy Gospodarek } 569535103d11SAndy Gospodarek 5696607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 56971da177e4SLinus Torvalds { 56981da177e4SLinus Torvalds .procname = "forwarding", 56991da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 57001da177e4SLinus Torvalds .maxlen = sizeof(int), 57011da177e4SLinus Torvalds .mode = 0644, 57026d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 57031da177e4SLinus Torvalds }, 57041da177e4SLinus Torvalds { 57051da177e4SLinus Torvalds .procname = "hop_limit", 57061da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 57071da177e4SLinus Torvalds .maxlen = sizeof(int), 57081da177e4SLinus Torvalds .mode = 0644, 5709d6df198dSPhil Sutter .proc_handler = addrconf_sysctl_hop_limit, 57101da177e4SLinus Torvalds }, 57111da177e4SLinus Torvalds { 57121da177e4SLinus Torvalds .procname = "mtu", 57131da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 57141da177e4SLinus Torvalds .maxlen = sizeof(int), 57151da177e4SLinus Torvalds .mode = 0644, 571677751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 57171da177e4SLinus Torvalds }, 57181da177e4SLinus Torvalds { 57191da177e4SLinus Torvalds .procname = "accept_ra", 57201da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 57211da177e4SLinus Torvalds .maxlen = sizeof(int), 57221da177e4SLinus Torvalds .mode = 0644, 57236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57241da177e4SLinus Torvalds }, 57251da177e4SLinus Torvalds { 57261da177e4SLinus Torvalds .procname = "accept_redirects", 57271da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 57281da177e4SLinus Torvalds .maxlen = sizeof(int), 57291da177e4SLinus Torvalds .mode = 0644, 57306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57311da177e4SLinus Torvalds }, 57321da177e4SLinus Torvalds { 57331da177e4SLinus Torvalds .procname = "autoconf", 57341da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 57351da177e4SLinus Torvalds .maxlen = sizeof(int), 57361da177e4SLinus Torvalds .mode = 0644, 57376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57381da177e4SLinus Torvalds }, 57391da177e4SLinus Torvalds { 57401da177e4SLinus Torvalds .procname = "dad_transmits", 57411da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 57421da177e4SLinus Torvalds .maxlen = sizeof(int), 57431da177e4SLinus Torvalds .mode = 0644, 57446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57451da177e4SLinus Torvalds }, 57461da177e4SLinus Torvalds { 57471da177e4SLinus Torvalds .procname = "router_solicitations", 57481da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 57491da177e4SLinus Torvalds .maxlen = sizeof(int), 57501da177e4SLinus Torvalds .mode = 0644, 57516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57521da177e4SLinus Torvalds }, 57531da177e4SLinus Torvalds { 57541da177e4SLinus Torvalds .procname = "router_solicitation_interval", 57551da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 57561da177e4SLinus Torvalds .maxlen = sizeof(int), 57571da177e4SLinus Torvalds .mode = 0644, 57586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57591da177e4SLinus Torvalds }, 57601da177e4SLinus Torvalds { 57611da177e4SLinus Torvalds .procname = "router_solicitation_delay", 57621da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 57631da177e4SLinus Torvalds .maxlen = sizeof(int), 57641da177e4SLinus Torvalds .mode = 0644, 57656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57661da177e4SLinus Torvalds }, 57671da177e4SLinus Torvalds { 57681da177e4SLinus Torvalds .procname = "force_mld_version", 57691da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 57701da177e4SLinus Torvalds .maxlen = sizeof(int), 57711da177e4SLinus Torvalds .mode = 0644, 57726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57731da177e4SLinus Torvalds }, 5774fc4eba58SHannes Frederic Sowa { 5775fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 5776fc4eba58SHannes Frederic Sowa .data = 5777fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 5778fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5779fc4eba58SHannes Frederic Sowa .mode = 0644, 5780fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5781fc4eba58SHannes Frederic Sowa }, 5782fc4eba58SHannes Frederic Sowa { 5783fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 5784fc4eba58SHannes Frederic Sowa .data = 5785fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 5786fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5787fc4eba58SHannes Frederic Sowa .mode = 0644, 5788fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5789fc4eba58SHannes Frederic Sowa }, 57901da177e4SLinus Torvalds { 57911da177e4SLinus Torvalds .procname = "use_tempaddr", 57921da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 57931da177e4SLinus Torvalds .maxlen = sizeof(int), 57941da177e4SLinus Torvalds .mode = 0644, 57956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57961da177e4SLinus Torvalds }, 57971da177e4SLinus Torvalds { 57981da177e4SLinus Torvalds .procname = "temp_valid_lft", 57991da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 58001da177e4SLinus Torvalds .maxlen = sizeof(int), 58011da177e4SLinus Torvalds .mode = 0644, 58026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58031da177e4SLinus Torvalds }, 58041da177e4SLinus Torvalds { 58051da177e4SLinus Torvalds .procname = "temp_prefered_lft", 58061da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 58071da177e4SLinus Torvalds .maxlen = sizeof(int), 58081da177e4SLinus Torvalds .mode = 0644, 58096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58101da177e4SLinus Torvalds }, 58111da177e4SLinus Torvalds { 58121da177e4SLinus Torvalds .procname = "regen_max_retry", 58131da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 58141da177e4SLinus Torvalds .maxlen = sizeof(int), 58151da177e4SLinus Torvalds .mode = 0644, 58166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58171da177e4SLinus Torvalds }, 58181da177e4SLinus Torvalds { 58191da177e4SLinus Torvalds .procname = "max_desync_factor", 58201da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 58211da177e4SLinus Torvalds .maxlen = sizeof(int), 58221da177e4SLinus Torvalds .mode = 0644, 58236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58241da177e4SLinus Torvalds }, 58251da177e4SLinus Torvalds { 58261da177e4SLinus Torvalds .procname = "max_addresses", 58271da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 58281da177e4SLinus Torvalds .maxlen = sizeof(int), 58291da177e4SLinus Torvalds .mode = 0644, 58306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58311da177e4SLinus Torvalds }, 58321da177e4SLinus Torvalds { 583365f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 583465f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 583565f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 583665f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 58376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 583865f5c7c1SYOSHIFUJI Hideaki }, 583965f5c7c1SYOSHIFUJI Hideaki { 58408013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 58418013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 58428013d1d7SHangbin Liu .maxlen = sizeof(int), 58438013d1d7SHangbin Liu .mode = 0644, 58448013d1d7SHangbin Liu .proc_handler = proc_dointvec, 58458013d1d7SHangbin Liu }, 58468013d1d7SHangbin Liu { 5847c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 5848c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 5849c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5850c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 58516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5852c4fd30ebSYOSHIFUJI Hideaki }, 5853930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5854930d6ff2SYOSHIFUJI Hideaki { 5855930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 5856930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 5857930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5858930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 58596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5860930d6ff2SYOSHIFUJI Hideaki }, 586152e16356SYOSHIFUJI Hideaki { 586252e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 586352e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 586452e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 586552e16356SYOSHIFUJI Hideaki .mode = 0644, 58666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 586752e16356SYOSHIFUJI Hideaki }, 5868fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 586909c884d4SYOSHIFUJI Hideaki { 587009c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 587109c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 587209c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 587309c884d4SYOSHIFUJI Hideaki .mode = 0644, 58746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 587509c884d4SYOSHIFUJI Hideaki }, 587609c884d4SYOSHIFUJI Hideaki #endif 5877930d6ff2SYOSHIFUJI Hideaki #endif 5878c4fd30ebSYOSHIFUJI Hideaki { 5879fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 5880fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 5881fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5882fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 5883c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 5884fbea49e1SYOSHIFUJI Hideaki }, 5885fbea49e1SYOSHIFUJI Hideaki { 58860bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 58870bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 58880bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 58890bcbc926SYOSHIFUJI Hideaki .mode = 0644, 58906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58910bcbc926SYOSHIFUJI Hideaki }, 589295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 589395c385b4SNeil Horman { 589495c385b4SNeil Horman .procname = "optimistic_dad", 589595c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 589695c385b4SNeil Horman .maxlen = sizeof(int), 589795c385b4SNeil Horman .mode = 0644, 58986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 589995c385b4SNeil Horman }, 59007fd2561eSErik Kline { 59017fd2561eSErik Kline .procname = "use_optimistic", 59027fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 59037fd2561eSErik Kline .maxlen = sizeof(int), 59047fd2561eSErik Kline .mode = 0644, 59057fd2561eSErik Kline .proc_handler = proc_dointvec, 59067fd2561eSErik Kline }, 590795c385b4SNeil Horman #endif 59087bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 59097bc570c8SYOSHIFUJI Hideaki { 59107bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 59117bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 59127bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5913a4e6db07SDavid S. Miller .mode = 0444, 59146d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59157bc570c8SYOSHIFUJI Hideaki }, 59167bc570c8SYOSHIFUJI Hideaki #endif 59170bcbc926SYOSHIFUJI Hideaki { 5918778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 5919778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 5920778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5921778d80beSYOSHIFUJI Hideaki .mode = 0644, 592256d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 5923778d80beSYOSHIFUJI Hideaki }, 5924778d80beSYOSHIFUJI Hideaki { 59251b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 59261b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 59271b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 59281b34be74SYOSHIFUJI Hideaki .mode = 0644, 59296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59301b34be74SYOSHIFUJI Hideaki }, 59311b34be74SYOSHIFUJI Hideaki { 5932f7734fdfSOctavian Purdila .procname = "force_tllao", 5933f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 5934f7734fdfSOctavian Purdila .maxlen = sizeof(int), 5935f7734fdfSOctavian Purdila .mode = 0644, 5936f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 5937f7734fdfSOctavian Purdila }, 5938f7734fdfSOctavian Purdila { 59395cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 59405cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 59415cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 59425cb04436SHannes Frederic Sowa .mode = 0644, 59435cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 59445cb04436SHannes Frederic Sowa }, 59455cb04436SHannes Frederic Sowa { 5946b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 5947b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 5948b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 5949b800c3b9SHannes Frederic Sowa .mode = 0644, 5950b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 5951b800c3b9SHannes Frederic Sowa }, 5952b800c3b9SHannes Frederic Sowa { 5953d9333196SBen Greear .procname = "accept_ra_from_local", 5954d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 5955d9333196SBen Greear .maxlen = sizeof(int), 5956d9333196SBen Greear .mode = 0644, 5957d9333196SBen Greear .proc_handler = proc_dointvec, 5958d9333196SBen Greear }, 5959d9333196SBen Greear { 5960c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 5961c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 5962c2943f14SHarout Hedeshian .maxlen = sizeof(int), 5963c2943f14SHarout Hedeshian .mode = 0644, 5964c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 5965c2943f14SHarout Hedeshian }, 5966c2943f14SHarout Hedeshian { 59673d1bec99SHannes Frederic Sowa .procname = "stable_secret", 59683d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 59693d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 59703d1bec99SHannes Frederic Sowa .mode = 0600, 59713d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 59723d1bec99SHannes Frederic Sowa }, 59733d1bec99SHannes Frederic Sowa { 59743985e8a3SErik Kline .procname = "use_oif_addrs_only", 59753985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 59763985e8a3SErik Kline .maxlen = sizeof(int), 59773985e8a3SErik Kline .mode = 0644, 59783985e8a3SErik Kline .proc_handler = proc_dointvec, 597935103d11SAndy Gospodarek }, 598035103d11SAndy Gospodarek { 598135103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 598235103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 598335103d11SAndy Gospodarek .maxlen = sizeof(int), 598435103d11SAndy Gospodarek .mode = 0644, 598535103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 59863985e8a3SErik Kline }, 59873985e8a3SErik Kline { 5988abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 5989abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 5990abbc3043SJohannes Berg .maxlen = sizeof(int), 5991abbc3043SJohannes Berg .mode = 0644, 5992abbc3043SJohannes Berg .proc_handler = proc_dointvec, 5993abbc3043SJohannes Berg }, 5994abbc3043SJohannes Berg { 59957a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 59967a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 59977a02bf89SJohannes Berg .maxlen = sizeof(int), 59987a02bf89SJohannes Berg .mode = 0644, 59997a02bf89SJohannes Berg .proc_handler = proc_dointvec, 60007a02bf89SJohannes Berg }, 60017a02bf89SJohannes Berg { 6002f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6003f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6004f1705ec1SDavid Ahern .maxlen = sizeof(int), 6005f1705ec1SDavid Ahern .mode = 0644, 6006f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6007f1705ec1SDavid Ahern 6008f1705ec1SDavid Ahern }, 6009f1705ec1SDavid Ahern { 6010f8572d8fSEric W. Biederman /* sentinel */ 60111da177e4SLinus Torvalds } 60121da177e4SLinus Torvalds }; 60131da177e4SLinus Torvalds 6014bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6015f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 60161da177e4SLinus Torvalds { 60171da177e4SLinus Torvalds int i; 6018607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 60196105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 60201dab6222SPavel Emelyanov 6021607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6022607ea7cdSKonstantin Khlebnikov if (!table) 6023f68635e6SPavel Emelyanov goto out; 6024f68635e6SPavel Emelyanov 6025607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6026607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6027607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6028607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 60291da177e4SLinus Torvalds } 60301da177e4SLinus Torvalds 60316105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 60321da177e4SLinus Torvalds 6033607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6034607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 60356105e293SEric W. Biederman goto free; 6036f68635e6SPavel Emelyanov 603795897312SPavel Emelyanov return 0; 60381da177e4SLinus Torvalds 60391da177e4SLinus Torvalds free: 6040607ea7cdSKonstantin Khlebnikov kfree(table); 6041f68635e6SPavel Emelyanov out: 604295897312SPavel Emelyanov return -ENOBUFS; 60431da177e4SLinus Torvalds } 60441da177e4SLinus Torvalds 6045408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 6046f52295a9SPavel Emelyanov { 6047607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6048f52295a9SPavel Emelyanov 6049607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6050408c4768SPavel Emelyanov return; 6051408c4768SPavel Emelyanov 6052607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6053607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6054607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6055607ea7cdSKonstantin Khlebnikov kfree(table); 60561da177e4SLinus Torvalds } 6057408c4768SPavel Emelyanov 6058a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6059408c4768SPavel Emelyanov { 6060a317a2f1SWANG Cong int err; 6061a317a2f1SWANG Cong 6062a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6063a317a2f1SWANG Cong return -EINVAL; 6064a317a2f1SWANG Cong 6065a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6066f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6067a317a2f1SWANG Cong if (err) 6068a317a2f1SWANG Cong return err; 6069a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6070f8572d8fSEric W. Biederman idev, &idev->cnf); 6071a317a2f1SWANG Cong if (err) 6072a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6073a317a2f1SWANG Cong 6074a317a2f1SWANG Cong return err; 6075408c4768SPavel Emelyanov } 6076408c4768SPavel Emelyanov 6077408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6078408c4768SPavel Emelyanov { 6079408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 6080408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 60811da177e4SLinus Torvalds } 60821da177e4SLinus Torvalds 60831da177e4SLinus Torvalds 60841da177e4SLinus Torvalds #endif 60851da177e4SLinus Torvalds 60862c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6087e0da5a48SPavel Emelyanov { 6088a79ca223SHong Zhiguo int err = -ENOMEM; 6089e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6090e0da5a48SPavel Emelyanov 6091a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 609263159f29SIan Morris if (!all) 6093e0da5a48SPavel Emelyanov goto err_alloc_all; 6094e0da5a48SPavel Emelyanov 6095a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 609663159f29SIan Morris if (!dflt) 6097e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6098a79ca223SHong Zhiguo 609956d417b1SBrian Haley /* these will be inherited by all namespaces */ 610056d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 610156d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6102e0da5a48SPavel Emelyanov 61033d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 61043d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 61053d1bec99SHannes Frederic Sowa 6106e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6107e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6108e0da5a48SPavel Emelyanov 6109e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6110f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6111e0da5a48SPavel Emelyanov if (err < 0) 6112e0da5a48SPavel Emelyanov goto err_reg_all; 6113e0da5a48SPavel Emelyanov 6114f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6115e0da5a48SPavel Emelyanov if (err < 0) 6116e0da5a48SPavel Emelyanov goto err_reg_dflt; 6117e0da5a48SPavel Emelyanov #endif 6118e0da5a48SPavel Emelyanov return 0; 6119e0da5a48SPavel Emelyanov 6120e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6121e0da5a48SPavel Emelyanov err_reg_dflt: 6122e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 6123e0da5a48SPavel Emelyanov err_reg_all: 6124e0da5a48SPavel Emelyanov kfree(dflt); 6125e0da5a48SPavel Emelyanov #endif 6126e0da5a48SPavel Emelyanov err_alloc_dflt: 6127e0da5a48SPavel Emelyanov kfree(all); 6128e0da5a48SPavel Emelyanov err_alloc_all: 6129e0da5a48SPavel Emelyanov return err; 6130e0da5a48SPavel Emelyanov } 6131e0da5a48SPavel Emelyanov 61322c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6133e0da5a48SPavel Emelyanov { 6134e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6135e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 6136e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 6137e0da5a48SPavel Emelyanov #endif 6138e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6139e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6140e0da5a48SPavel Emelyanov } 6141e0da5a48SPavel Emelyanov 6142e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6143e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6144e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6145e0da5a48SPavel Emelyanov }; 6146e0da5a48SPavel Emelyanov 6147207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6148b382b191SThomas Graf .family = AF_INET6, 6149b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6150b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 615111b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6152f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6153b382b191SThomas Graf }; 6154b382b191SThomas Graf 61551da177e4SLinus Torvalds /* 61561da177e4SLinus Torvalds * Init / cleanup code 61571da177e4SLinus Torvalds */ 61581da177e4SLinus Torvalds 61591da177e4SLinus Torvalds int __init addrconf_init(void) 61601da177e4SLinus Torvalds { 6161a317a2f1SWANG Cong struct inet6_dev *idev; 6162c2e21293Sstephen hemminger int i, err; 61632a8cc6c8SYOSHIFUJI Hideaki 6164e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6165e21e8467SStephen Hemminger if (err < 0) { 6166f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6167f3213831SJoe Perches __func__, err); 61682cc6d2bfSNeil Horman goto out; 61692a8cc6c8SYOSHIFUJI Hideaki } 61701da177e4SLinus Torvalds 61712cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 61722cc6d2bfSNeil Horman if (err < 0) 61732cc6d2bfSNeil Horman goto out_addrlabel; 6174e0da5a48SPavel Emelyanov 6175c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6176c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6177c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6178c15b1ccaSHannes Frederic Sowa goto out_nowq; 6179c15b1ccaSHannes Frederic Sowa } 6180c15b1ccaSHannes Frederic Sowa 61811da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 61821da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 61831da177e4SLinus Torvalds * before it can bring up and give link-local addresses 61841da177e4SLinus Torvalds * to other devices which are up. 61851da177e4SLinus Torvalds * 61861da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 61871da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 61881da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 61891da177e4SLinus Torvalds * So this causes the notifier registry below to try and 61901da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 61911da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 61921da177e4SLinus Torvalds * devices to fail to get a link-local address. 61931da177e4SLinus Torvalds * 61941da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 61951da177e4SLinus Torvalds * loopback_dev first by hand. 61961da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 61971da177e4SLinus Torvalds * device and it being up should be removed. 61981da177e4SLinus Torvalds */ 61991da177e4SLinus Torvalds rtnl_lock(); 6200a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 62011da177e4SLinus Torvalds rtnl_unlock(); 6202a317a2f1SWANG Cong if (IS_ERR(idev)) { 6203a317a2f1SWANG Cong err = PTR_ERR(idev); 6204e0da5a48SPavel Emelyanov goto errlo; 6205a317a2f1SWANG Cong } 62061da177e4SLinus Torvalds 6207c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6208c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6209c2e21293Sstephen hemminger 62101da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 62111da177e4SLinus Torvalds 6212c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6213c127ea2cSThomas Graf 62143678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6215b382b191SThomas Graf 6216c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 6217c7ac8679SGreg Rose NULL); 6218c127ea2cSThomas Graf if (err < 0) 6219c127ea2cSThomas Graf goto errout; 6220c127ea2cSThomas Graf 6221c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 6222c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 6223c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 6224c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 6225c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 6226c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 6227c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 6228c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 6229c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 623076f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 62317a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 6232c127ea2cSThomas Graf 62332a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 62342a8cc6c8SYOSHIFUJI Hideaki 62351da177e4SLinus Torvalds return 0; 6236c127ea2cSThomas Graf errout: 6237b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6238c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6239e0da5a48SPavel Emelyanov errlo: 6240c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6241c15b1ccaSHannes Frederic Sowa out_nowq: 6242e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 62432cc6d2bfSNeil Horman out_addrlabel: 62442cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 62452cc6d2bfSNeil Horman out: 6246c127ea2cSThomas Graf return err; 62471da177e4SLinus Torvalds } 62481da177e4SLinus Torvalds 624909f7709fSDaniel Lezcano void addrconf_cleanup(void) 62501da177e4SLinus Torvalds { 6251176c39afSDaniel Lezcano struct net_device *dev; 62521da177e4SLinus Torvalds int i; 62531da177e4SLinus Torvalds 62541da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6255e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 62562cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 62571da177e4SLinus Torvalds 62581da177e4SLinus Torvalds rtnl_lock(); 62591da177e4SLinus Torvalds 6260b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 6261b382b191SThomas Graf 6262176c39afSDaniel Lezcano /* clean dev list */ 6263176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6264176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6265176c39afSDaniel Lezcano continue; 6266176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6267176c39afSDaniel Lezcano } 6268176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6269176c39afSDaniel Lezcano 62701da177e4SLinus Torvalds /* 62711da177e4SLinus Torvalds * Check hash table. 62721da177e4SLinus Torvalds */ 62735c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6274c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6275c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 62765c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6277c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 62781da177e4SLinus Torvalds rtnl_unlock(); 6279c15b1ccaSHannes Frederic Sowa 6280c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 62811da177e4SLinus Torvalds } 6282