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 550927265bcSEric 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 } 562927265bcSEric 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) { 781d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 782d26c638cSNicolas Dichtel 783e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 784d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 785d26c638cSNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 786d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 787d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 788d26c638cSNicolas Dichtel 789e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 790f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 791f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 792f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 793f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 794013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 795c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 7960187bdfbSBen Hutchings rtnl_unlock(); 797c8fecf22SPavel Emelyanov 798013d97e9SFrancesco Ruggeri if (newf) 7997b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 800b325fddbSStephen Hemminger return 1; 801c8fecf22SPavel Emelyanov } 80235103d11SAndy Gospodarek 80335103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 80435103d11SAndy Gospodarek { 80535103d11SAndy Gospodarek struct net_device *dev; 80635103d11SAndy Gospodarek struct inet6_dev *idev; 80735103d11SAndy Gospodarek 80835103d11SAndy Gospodarek for_each_netdev(net, dev) { 80935103d11SAndy Gospodarek idev = __in6_dev_get(dev); 81035103d11SAndy Gospodarek if (idev) { 81135103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 81235103d11SAndy Gospodarek 81335103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 81435103d11SAndy Gospodarek if (changed) 81535103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 81635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 81735103d11SAndy Gospodarek dev->ifindex, 81835103d11SAndy Gospodarek &idev->cnf); 81935103d11SAndy Gospodarek } 82035103d11SAndy Gospodarek } 82135103d11SAndy Gospodarek } 82235103d11SAndy Gospodarek 82335103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 82435103d11SAndy Gospodarek { 82535103d11SAndy Gospodarek struct net *net; 82635103d11SAndy Gospodarek int old; 82735103d11SAndy Gospodarek 82835103d11SAndy Gospodarek if (!rtnl_trylock()) 82935103d11SAndy Gospodarek return restart_syscall(); 83035103d11SAndy Gospodarek 83135103d11SAndy Gospodarek net = (struct net *)table->extra2; 83235103d11SAndy Gospodarek old = *p; 83335103d11SAndy Gospodarek *p = newf; 83435103d11SAndy Gospodarek 83535103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 83635103d11SAndy Gospodarek if ((!newf) ^ (!old)) 83735103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 83835103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 83935103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 84035103d11SAndy Gospodarek net->ipv6.devconf_dflt); 84135103d11SAndy Gospodarek rtnl_unlock(); 84235103d11SAndy Gospodarek return 0; 84335103d11SAndy Gospodarek } 84435103d11SAndy Gospodarek 84535103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 84635103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 84735103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 84835103d11SAndy Gospodarek if ((!newf) ^ (!old)) 84935103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 85035103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 85135103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 85235103d11SAndy Gospodarek net->ipv6.devconf_all); 85335103d11SAndy Gospodarek } 85435103d11SAndy Gospodarek rtnl_unlock(); 85535103d11SAndy Gospodarek 85635103d11SAndy Gospodarek return 1; 85735103d11SAndy Gospodarek } 85835103d11SAndy Gospodarek 8591da177e4SLinus Torvalds #endif 8601da177e4SLinus Torvalds 8615c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8621da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 8631da177e4SLinus Torvalds { 864c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 865547b792cSIlpo Järvinen 8661da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 86791df42beSJoe Perches pr_debug("%s\n", __func__); 8681da177e4SLinus Torvalds #endif 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds in6_dev_put(ifp->idev); 8711da177e4SLinus Torvalds 872c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 873c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 874c15b1ccaSHannes Frederic Sowa ifp); 8751da177e4SLinus Torvalds 876e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 877f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 8781da177e4SLinus Torvalds return; 8791da177e4SLinus Torvalds } 88094e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 8811da177e4SLinus Torvalds 882e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 885e55ffac6SBrian Haley static void 886e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 887e55ffac6SBrian Haley { 888502a2ffdSstephen hemminger struct list_head *p; 8898a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 890e55ffac6SBrian Haley 891e55ffac6SBrian Haley /* 892e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 893e55ffac6SBrian Haley * global before linklocal. 894e55ffac6SBrian Haley */ 895502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 896502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 897502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 8988a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 899e55ffac6SBrian Haley break; 900e55ffac6SBrian Haley } 901e55ffac6SBrian Haley 902b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 903e55ffac6SBrian Haley } 904e55ffac6SBrian Haley 905ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 9063eb84f49SYOSHIFUJI Hideaki { 907ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 9083eb84f49SYOSHIFUJI Hideaki } 9093eb84f49SYOSHIFUJI Hideaki 9101da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds static struct inet6_ifaddr * 9133f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 9143f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 9158a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 9161da177e4SLinus Torvalds { 9171da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 9181da177e4SLinus Torvalds struct rt6_info *rt; 9193a88a81dSstephen hemminger unsigned int hash; 9201da177e4SLinus Torvalds int err = 0; 921d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 922d68b8270SYOSHIFUJI Hideaki 923d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 924d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 925d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 926d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 927d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9281da177e4SLinus Torvalds 9298814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9301da177e4SLinus Torvalds if (idev->dead) { 9311da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9321da177e4SLinus Torvalds goto out2; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 93556d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9369bdd8d40SBrian Haley err = -EACCES; 9379bdd8d40SBrian Haley goto out2; 9389bdd8d40SBrian Haley } 9399bdd8d40SBrian Haley 9405c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 943c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 944ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 9451da177e4SLinus Torvalds err = -EEXIST; 9461da177e4SLinus Torvalds goto out; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 949322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 9501da177e4SLinus Torvalds 95163159f29SIan Morris if (!ifa) { 952ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 9531da177e4SLinus Torvalds err = -ENOBUFS; 9541da177e4SLinus Torvalds goto out; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9578f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 9581da177e4SLinus Torvalds if (IS_ERR(rt)) { 9591da177e4SLinus Torvalds err = PTR_ERR(rt); 9601da177e4SLinus Torvalds goto out; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 963bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 964bba24896SJiri Pirko 9654e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 9663f8f5298SJiri Pirko if (peer_addr) 9673f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 970c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 971c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 9721da177e4SLinus Torvalds ifa->scope = scope; 9731da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 9741da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 9758a226b2cSJiri Benc ifa->valid_lft = valid_lft; 9768a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 9771da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 978617fe29dSDaniel Borkmann ifa->tokenized = false; 9791da177e4SLinus Torvalds 98057f5f544SKeir Fraser ifa->rt = rt; 98157f5f544SKeir Fraser 9821da177e4SLinus Torvalds ifa->idev = idev; 9831da177e4SLinus Torvalds in6_dev_hold(idev); 9841da177e4SLinus Torvalds /* For caller */ 9851da177e4SLinus Torvalds in6_ifa_hold(ifa); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds /* Add to big hash table */ 988ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 9891da177e4SLinus Torvalds 9905c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 9915c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds write_lock(&idev->lock); 9941da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 995e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 998372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 9991da177e4SLinus Torvalds in6_ifa_hold(ifa); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds in6_ifa_hold(ifa); 10031da177e4SLinus Torvalds write_unlock(&idev->lock); 10041da177e4SLinus Torvalds out2: 10058814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10061da177e4SLinus Torvalds 1007fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 1008f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 10091da177e4SLinus Torvalds else { 10101da177e4SLinus Torvalds kfree(ifa); 10111da177e4SLinus Torvalds ifa = ERR_PTR(err); 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds return ifa; 10151da177e4SLinus Torvalds out: 10165c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10171da177e4SLinus Torvalds goto out2; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10205b84efecSThomas Haller enum cleanup_prefix_rt_t { 10215b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10225b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10235b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10245b84efecSThomas Haller }; 10255b84efecSThomas Haller 10265b84efecSThomas Haller /* 10275b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10285b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10295b84efecSThomas Haller * constants. 10305b84efecSThomas Haller * 10315b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 10325b84efecSThomas Haller * prefix is managed by its own lifetime. 10335b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 10345b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 10355b84efecSThomas Haller * 4) if there are still other permanent address(es), 10365b84efecSThomas Haller * corresponding prefix is still permanent. 10375b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 10385b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 10395b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 10405b84efecSThomas Haller * longest valid lifetime among the corresponding 10415b84efecSThomas Haller * addresses on the device. 10425b84efecSThomas Haller * Note: subsequent RA will update lifetime. 10435b84efecSThomas Haller **/ 10445b84efecSThomas Haller static enum cleanup_prefix_rt_t 10455b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 10465b84efecSThomas Haller { 10475b84efecSThomas Haller struct inet6_ifaddr *ifa; 10485b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 10495b84efecSThomas Haller unsigned long lifetime; 10505b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 10515b84efecSThomas Haller 10525b84efecSThomas Haller *expires = jiffies; 10535b84efecSThomas Haller 10545b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 10555b84efecSThomas Haller if (ifa == ifp) 10565b84efecSThomas Haller continue; 10575b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 10585b84efecSThomas Haller ifp->prefix_len)) 10595b84efecSThomas Haller continue; 10605b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 10615b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 10625b84efecSThomas Haller 10635b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 10645b84efecSThomas Haller 10655b84efecSThomas Haller spin_lock(&ifa->lock); 10665b84efecSThomas Haller 10675b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 10685b84efecSThomas Haller /* 10695b84efecSThomas Haller * Note: Because this address is 10705b84efecSThomas Haller * not permanent, lifetime < 10715b84efecSThomas Haller * LONG_MAX / HZ here. 10725b84efecSThomas Haller */ 10735b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 10745b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 10755b84efecSThomas Haller spin_unlock(&ifa->lock); 10765b84efecSThomas Haller } 10775b84efecSThomas Haller 10785b84efecSThomas Haller return action; 10795b84efecSThomas Haller } 10805b84efecSThomas Haller 10815b84efecSThomas Haller static void 10825b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 10835b84efecSThomas Haller { 10845b84efecSThomas Haller struct rt6_info *rt; 10855b84efecSThomas Haller 10865b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 10875b84efecSThomas Haller ifp->prefix_len, 10885b84efecSThomas Haller ifp->idev->dev, 10895b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 10905b84efecSThomas Haller if (rt) { 10915b84efecSThomas Haller if (del_rt) 10925b84efecSThomas Haller ip6_del_rt(rt); 10935b84efecSThomas Haller else { 10945b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 10955b84efecSThomas Haller rt6_set_expires(rt, expires); 10965b84efecSThomas Haller ip6_rt_put(rt); 10975b84efecSThomas Haller } 10985b84efecSThomas Haller } 10995b84efecSThomas Haller } 11005b84efecSThomas Haller 11015b84efecSThomas Haller 11021da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 11051da177e4SLinus Torvalds { 11064c5ff6a6SHerbert Xu int state; 11075b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 11085b84efecSThomas Haller unsigned long expires; 11091da177e4SLinus Torvalds 1110c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1111c15b1ccaSHannes Frederic Sowa 11128e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 11134c5ff6a6SHerbert Xu state = ifp->state; 1114e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 11158e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 11164c5ff6a6SHerbert Xu 11174c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 11184c5ff6a6SHerbert Xu goto out; 11191da177e4SLinus Torvalds 11205c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 11215c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11225c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11231da177e4SLinus Torvalds 11245b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11255d9efa7eSDavid S. Miller 11261da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1127372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11281da177e4SLinus Torvalds if (ifp->ifpub) { 11291da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11301da177e4SLinus Torvalds ifp->ifpub = NULL; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds __in6_ifa_put(ifp); 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11355b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 11365b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 11375b84efecSThomas Haller 1138502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 11391da177e4SLinus Torvalds __in6_ifa_put(ifp); 1140502a2ffdSstephen hemminger 11415b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 11421da177e4SLinus Torvalds 1143c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1144b2238566SAndrey Vagin 11451da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 11461da177e4SLinus Torvalds 1147f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 11481da177e4SLinus Torvalds 11495b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 11505b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 11515b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 1154c3968a85SDaniel Walter /* clean up prefsrc entries */ 1155c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 11564c5ff6a6SHerbert Xu out: 11571da177e4SLinus Torvalds in6_ifa_put(ifp); 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 11611da177e4SLinus Torvalds { 11621da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 11631da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 116476f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1165eac55bf9SBenoit Boissinot unsigned long regen_advance; 11661da177e4SLinus Torvalds int tmp_plen; 11671da177e4SLinus Torvalds int ret = 0; 116895c385b4SNeil Horman u32 addr_flags; 116976f793e3SLorenzo Colitti unsigned long now = jiffies; 11701da177e4SLinus Torvalds 117153bd6749SJiri Pirko write_lock_bh(&idev->lock); 11721da177e4SLinus Torvalds if (ift) { 11731da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 11741da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 11751da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 11761da177e4SLinus Torvalds tmpaddr = &addr; 11771da177e4SLinus Torvalds } else { 11781da177e4SLinus Torvalds tmpaddr = NULL; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds retry: 11811da177e4SLinus Torvalds in6_dev_hold(idev); 11821da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 118353bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1184f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 11851da177e4SLinus Torvalds in6_dev_put(idev); 11861da177e4SLinus Torvalds ret = -1; 11871da177e4SLinus Torvalds goto out; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 11901da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 11911da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 11921da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 119353bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1194f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1195f3213831SJoe Perches __func__); 11961da177e4SLinus Torvalds in6_dev_put(idev); 11971da177e4SLinus Torvalds ret = -1; 11981da177e4SLinus Torvalds goto out; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds in6_ifa_hold(ifp); 12011da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1202eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 12031da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 120476f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 12051da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 12061da177e4SLinus Torvalds ifp->valid_lft, 12077a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 12081da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 12091da177e4SLinus Torvalds ifp->prefered_lft, 12107a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 1211784e2710SBen Hutchings idev->cnf.max_desync_factor); 12121da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 12131da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 12141da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12151da177e4SLinus Torvalds 1216eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1217eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 12181f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 121953bd6749SJiri Pirko write_unlock_bh(&idev->lock); 122095c385b4SNeil Horman 1221eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1222eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1223eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1224eac55bf9SBenoit Boissinot * Preferred Lifetime. 1225ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1226ecab6701SHeiner Kallweit * temporary addresses being generated. 1227eac55bf9SBenoit Boissinot */ 1228ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1229ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1230eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1231eac55bf9SBenoit Boissinot in6_dev_put(idev); 1232eac55bf9SBenoit Boissinot ret = -1; 1233eac55bf9SBenoit Boissinot goto out; 1234eac55bf9SBenoit Boissinot } 1235eac55bf9SBenoit Boissinot 123695c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 123795c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 123895c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 123995c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 124095c385b4SNeil Horman 12414b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 12428a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 12434b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 12444b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 12451da177e4SLinus Torvalds in6_ifa_put(ifp); 12461da177e4SLinus Torvalds in6_dev_put(idev); 1247f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 12481da177e4SLinus Torvalds tmpaddr = &addr; 124953bd6749SJiri Pirko write_lock_bh(&idev->lock); 12501da177e4SLinus Torvalds goto retry; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12541da177e4SLinus Torvalds ift->ifpub = ifp; 125576f793e3SLorenzo Colitti ift->cstamp = now; 12561da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 12571da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12581da177e4SLinus Torvalds 1259cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 12601da177e4SLinus Torvalds in6_ifa_put(ift); 12611da177e4SLinus Torvalds in6_dev_put(idev); 12621da177e4SLinus Torvalds out: 12631da177e4SLinus Torvalds return ret; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* 1267072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 12681da177e4SLinus Torvalds */ 1269a9b05723SYOSHIFUJI Hideaki enum { 1270a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1271a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1272a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1273a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1274a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1275a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1276a9b05723SYOSHIFUJI Hideaki #endif 1277a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1278a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1279a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1280a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1281a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 12827fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 12837fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 12847fd2561eSErik Kline #endif 1285a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1286072047e4SYOSHIFUJI Hideaki }; 1287072047e4SYOSHIFUJI Hideaki 1288a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1289a9b05723SYOSHIFUJI Hideaki int rule; 1290a9b05723SYOSHIFUJI Hideaki int addr_type; 1291a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1292a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1293a9b05723SYOSHIFUJI Hideaki int scopedist; 1294a9b05723SYOSHIFUJI Hideaki int matchlen; 1295a9b05723SYOSHIFUJI Hideaki }; 1296a9b05723SYOSHIFUJI Hideaki 1297a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 12989acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1299a9b05723SYOSHIFUJI Hideaki int ifindex; 1300a9b05723SYOSHIFUJI Hideaki int scope; 1301a9b05723SYOSHIFUJI Hideaki int label; 13027cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1303a9b05723SYOSHIFUJI Hideaki }; 1304072047e4SYOSHIFUJI Hideaki 1305b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 13061da177e4SLinus Torvalds { 130745bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1308072047e4SYOSHIFUJI Hideaki return 1; 1309072047e4SYOSHIFUJI Hideaki return 0; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13127fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 13137fd2561eSErik Kline { 13147fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13157fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 13167fd2561eSErik Kline #else 13177fd2561eSErik Kline return false; 13187fd2561eSErik Kline #endif 13197fd2561eSErik Kline } 13207fd2561eSErik Kline 13213de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 13223de23255SBenjamin Thery struct ipv6_saddr_score *score, 1323a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1324a9b05723SYOSHIFUJI Hideaki int i) 1325a9b05723SYOSHIFUJI Hideaki { 1326a9b05723SYOSHIFUJI Hideaki int ret; 1327a9b05723SYOSHIFUJI Hideaki 1328a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1329a9b05723SYOSHIFUJI Hideaki switch (i) { 1330a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1331a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1332a9b05723SYOSHIFUJI Hideaki break; 1333a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1334a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1335a9b05723SYOSHIFUJI Hideaki break; 1336a9b05723SYOSHIFUJI Hideaki default: 1337a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1338a9b05723SYOSHIFUJI Hideaki } 1339a9b05723SYOSHIFUJI Hideaki goto out; 1340a9b05723SYOSHIFUJI Hideaki } 1341a9b05723SYOSHIFUJI Hideaki 1342a9b05723SYOSHIFUJI Hideaki switch (i) { 1343a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1344a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1345a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1346a9b05723SYOSHIFUJI Hideaki break; 1347a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1348a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1349a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1350a9b05723SYOSHIFUJI Hideaki break; 1351a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1352a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1353a9b05723SYOSHIFUJI Hideaki * 1354a9b05723SYOSHIFUJI Hideaki * ret 1355a9b05723SYOSHIFUJI Hideaki * ^ 1356a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1357a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1358a9b05723SYOSHIFUJI Hideaki * | 1359a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1360a9b05723SYOSHIFUJI Hideaki * B-d | \ 1361a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1362db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1363a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1364a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1365a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1366a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1367a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1368a9b05723SYOSHIFUJI Hideaki * 1369a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1370a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1371a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1372a9b05723SYOSHIFUJI Hideaki */ 1373a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1374a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1375a9b05723SYOSHIFUJI Hideaki ret = -ret; 1376a9b05723SYOSHIFUJI Hideaki else 1377a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1378a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1379a9b05723SYOSHIFUJI Hideaki break; 1380a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 13817fd2561eSErik Kline { 1382a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 13837fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 13847fd2561eSErik Kline 13857fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 13867fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1387a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 13887fd2561eSErik Kline !(score->ifa->flags & avoid); 1389a9b05723SYOSHIFUJI Hideaki break; 13907fd2561eSErik Kline } 1391a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1392a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 13937cbca67cSYOSHIFUJI Hideaki { 1394a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 13957cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 13967cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1397a9b05723SYOSHIFUJI Hideaki break; 13987cbca67cSYOSHIFUJI Hideaki } 1399a9b05723SYOSHIFUJI Hideaki #endif 1400a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1401a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1402a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1403a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1404a9b05723SYOSHIFUJI Hideaki break; 1405a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1406a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 14073de23255SBenjamin Thery ret = ipv6_addr_label(net, 14083de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1409a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1410a9b05723SYOSHIFUJI Hideaki break; 1411a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 14127cbca67cSYOSHIFUJI Hideaki { 1413a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 141425985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1415a9b05723SYOSHIFUJI Hideaki */ 14167cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 14177cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 14187cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 14197cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1420a9b05723SYOSHIFUJI Hideaki break; 14217cbca67cSYOSHIFUJI Hideaki } 1422a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1423a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1424a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1425a9b05723SYOSHIFUJI Hideaki */ 1426a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1427a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1428a9b05723SYOSHIFUJI Hideaki break; 1429a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1430a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 143191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 143291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 143391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 143491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1435a9b05723SYOSHIFUJI Hideaki break; 14367fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14377fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 14387fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 14397fd2561eSErik Kline * preferred addresses. 14407fd2561eSErik Kline */ 14417fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 14427fd2561eSErik Kline break; 14437fd2561eSErik Kline #endif 1444a9b05723SYOSHIFUJI Hideaki default: 1445a9b05723SYOSHIFUJI Hideaki ret = 0; 1446a9b05723SYOSHIFUJI Hideaki } 1447a9b05723SYOSHIFUJI Hideaki 1448a9b05723SYOSHIFUJI Hideaki if (ret) 1449a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1450a9b05723SYOSHIFUJI Hideaki score->rule = i; 1451a9b05723SYOSHIFUJI Hideaki out: 1452a9b05723SYOSHIFUJI Hideaki return ret; 1453a9b05723SYOSHIFUJI Hideaki } 1454a9b05723SYOSHIFUJI Hideaki 1455c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 14569131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 14579131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1458c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1459c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 14601da177e4SLinus Torvalds { 1461c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1462072047e4SYOSHIFUJI Hideaki 14631da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1464502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1465a9b05723SYOSHIFUJI Hideaki int i; 1466072047e4SYOSHIFUJI Hideaki 1467a9b05723SYOSHIFUJI Hideaki /* 14686b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 14696b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 14706b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 147195c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 14726b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1473072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1474072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1475072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1476072047e4SYOSHIFUJI Hideaki */ 1477a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1478a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 14796b3ae80aSYOSHIFUJI Hideaki continue; 1480a9b05723SYOSHIFUJI Hideaki 1481a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1482a9b05723SYOSHIFUJI Hideaki 1483a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1484a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1485ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 14869131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 14871da177e4SLinus Torvalds continue; 1488072047e4SYOSHIFUJI Hideaki } 1489072047e4SYOSHIFUJI Hideaki 1490a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1491a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1492072047e4SYOSHIFUJI Hideaki 1493a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1494a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1495072047e4SYOSHIFUJI Hideaki 14969131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 14979131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1498072047e4SYOSHIFUJI Hideaki 1499a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1500a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1501a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1502a9b05723SYOSHIFUJI Hideaki /* 1503a9b05723SYOSHIFUJI Hideaki * special case: 1504a9b05723SYOSHIFUJI Hideaki * each remaining entry 1505a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1506a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1507a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1508a9b05723SYOSHIFUJI Hideaki * values. 1509072047e4SYOSHIFUJI Hideaki */ 15109131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1511072047e4SYOSHIFUJI Hideaki } 1512a9b05723SYOSHIFUJI Hideaki break; 1513a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1514a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1515a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 151699cd07a5SJuha-Matti Tapio 1517a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1518072047e4SYOSHIFUJI Hideaki 1519a0bffffcSIlpo Järvinen swap(hiscore, score); 1520c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1521a9b05723SYOSHIFUJI Hideaki 1522a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1523a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1524a9b05723SYOSHIFUJI Hideaki 1525a9b05723SYOSHIFUJI Hideaki break; 15261da177e4SLinus Torvalds } 1527a9b05723SYOSHIFUJI Hideaki } 1528a9b05723SYOSHIFUJI Hideaki } 15299131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 15301da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1531c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 15321da177e4SLinus Torvalds } 15339131f3deSYOSHIFUJI Hideaki/吉藤英明 1534afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1535afbac601SDavid Ahern const struct net_device *dst_dev, 1536afbac601SDavid Ahern const struct net_device *master, 1537afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1538afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1539afbac601SDavid Ahern int hiscore_idx) 1540afbac601SDavid Ahern { 1541afbac601SDavid Ahern struct inet6_dev *idev; 1542afbac601SDavid Ahern 1543afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1544afbac601SDavid Ahern if (idev) 1545afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1546afbac601SDavid Ahern scores, hiscore_idx); 1547afbac601SDavid Ahern 1548afbac601SDavid Ahern idev = __in6_dev_get(master); 1549afbac601SDavid Ahern if (idev) 1550afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1551afbac601SDavid Ahern scores, hiscore_idx); 1552afbac601SDavid Ahern 1553afbac601SDavid Ahern return hiscore_idx; 1554afbac601SDavid Ahern } 1555afbac601SDavid Ahern 15569131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 15579131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 15589131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 15599131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1560c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 15619131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 15629131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 15639131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 15649131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 15659131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1566c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 15679131f3deSYOSHIFUJI Hideaki/吉藤英明 15689131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 15699131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 15709131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 15719131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 15729131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 15739131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 15749131f3deSYOSHIFUJI Hideaki/吉藤英明 1575c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1576c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 15779131f3deSYOSHIFUJI Hideaki/吉藤英明 15789131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 15799131f3deSYOSHIFUJI Hideaki/吉藤英明 15809131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 15819131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 15829131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 15839131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15849131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 15859131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 15869131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 15879131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 15889131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15899131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 15909131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 15913985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 15923985e8a3SErik Kline * be the set of unicast addresses assigned to the 15933985e8a3SErik Kline * interface that will be used to send to the destination 15943985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 15959131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 15969131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 15979131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 15983985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 15993985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 16003985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 16019131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 16029131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16039131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16049131f3deSYOSHIFUJI Hideaki/吉藤英明 16059131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1606c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1607c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16089131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1609afbac601SDavid Ahern const struct net_device *master; 1610afbac601SDavid Ahern int master_idx = 0; 1611afbac601SDavid Ahern 1612afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1613afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1614afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1615afbac601SDavid Ahern */ 1616afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1617afbac601SDavid Ahern if (master) { 1618afbac601SDavid Ahern master_idx = master->ifindex; 1619afbac601SDavid Ahern 1620afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1621afbac601SDavid Ahern master, &dst, 1622afbac601SDavid Ahern scores, hiscore_idx); 1623afbac601SDavid Ahern 1624afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1625afbac601SDavid Ahern goto out; 1626afbac601SDavid Ahern } 1627afbac601SDavid Ahern 16289131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1629afbac601SDavid Ahern /* only consider addresses on devices in the 1630afbac601SDavid Ahern * same L3 domain 1631afbac601SDavid Ahern */ 1632afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1633afbac601SDavid Ahern continue; 16349131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 16359131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 16369131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1637c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16389131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16399131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1640afbac601SDavid Ahern 1641afbac601SDavid Ahern out: 16428814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16431da177e4SLinus Torvalds 1644c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1645a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1646072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 16471da177e4SLinus Torvalds 16484e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1649a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1650072047e4SYOSHIFUJI Hideaki return 0; 16511da177e4SLinus Torvalds } 16525e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 16531da177e4SLinus Torvalds 16548965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1655479840ffSJiri Pirko u32 banned_flags) 1656b7b1bfceSHannes Frederic Sowa { 1657b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1658b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1659b7b1bfceSHannes Frederic Sowa 1660602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1661602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1662602582caSHannes Frederic Sowa break; 1663b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1664b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1665b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1666b7b1bfceSHannes Frederic Sowa err = 0; 1667b7b1bfceSHannes Frederic Sowa break; 1668b7b1bfceSHannes Frederic Sowa } 1669b7b1bfceSHannes Frederic Sowa } 1670b7b1bfceSHannes Frederic Sowa return err; 1671b7b1bfceSHannes Frederic Sowa } 1672b7b1bfceSHannes Frederic Sowa 167395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1674479840ffSJiri Pirko u32 banned_flags) 16751da177e4SLinus Torvalds { 16761da177e4SLinus Torvalds struct inet6_dev *idev; 16771da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 16781da177e4SLinus Torvalds 16798814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1680e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1681e21e8467SStephen Hemminger if (idev) { 16821da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1683b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 16841da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16851da177e4SLinus Torvalds } 16868814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16871da177e4SLinus Torvalds return err; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 16911da177e4SLinus Torvalds { 16921da177e4SLinus Torvalds int cnt = 0; 16931da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1696502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 16971da177e4SLinus Torvalds cnt++; 16981da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16991da177e4SLinus Torvalds return cnt; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 1702b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 17032a7851bfSFlorian Westphal const struct net_device *dev, int strict) 17041da177e4SLinus Torvalds { 1705c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1706c58da4c6SErik Kline } 1707c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1708c58da4c6SErik Kline 1709c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1710c58da4c6SErik Kline const struct net_device *dev, int strict, 1711c58da4c6SErik Kline u32 banned_flags) 1712c58da4c6SErik Kline { 1713eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1714ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1715c58da4c6SErik Kline u32 ifp_flags; 17161da177e4SLinus Torvalds 17175c578aedSstephen hemminger rcu_read_lock_bh(); 1718b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1719878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1720bfeade08SDaniel Lezcano continue; 1721c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1722c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1723c58da4c6SErik Kline */ 1724c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1725c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1726c58da4c6SErik Kline : ifp->flags; 17271da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1728c58da4c6SErik Kline !(ifp_flags&banned_flags) && 172963159f29SIan Morris (!dev || ifp->idev->dev == dev || 1730eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 17315c578aedSstephen hemminger rcu_read_unlock_bh(); 1732eedf042aSStephen Hemminger return 1; 1733eedf042aSStephen Hemminger } 1734eedf042aSStephen Hemminger } 17355c578aedSstephen hemminger 1736eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1737eedf042aSStephen Hemminger return 0; 17381da177e4SLinus Torvalds } 1739c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 17407159039aSYOSHIFUJI Hideaki 17413e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 174206bfe655SDaniel Lezcano struct net_device *dev) 17431da177e4SLinus Torvalds { 1744ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 17451da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 17461da177e4SLinus Torvalds 1747b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1748878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 174906bfe655SDaniel Lezcano continue; 17501da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 175163159f29SIan Morris if (!dev || ifp->idev->dev == dev) 17523e81c6daSDavid S. Miller return true; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds } 17553e81c6daSDavid S. Miller return false; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17587df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 17597df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 17607df37ff3SCatalin\(ux\) M. BOIE */ 17617df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 17627df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 17637df37ff3SCatalin\(ux\) M. BOIE { 17647df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 17657df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 17667df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 17677df37ff3SCatalin\(ux\) M. BOIE 17687df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 17697df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 17707df37ff3SCatalin\(ux\) M. BOIE if (idev) { 17717df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 17727df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 17737df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 17747df37ff3SCatalin\(ux\) M. BOIE if (ret) 17757df37ff3SCatalin\(ux\) M. BOIE break; 17767df37ff3SCatalin\(ux\) M. BOIE } 17777df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 17787df37ff3SCatalin\(ux\) M. BOIE } 17797df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 17807df37ff3SCatalin\(ux\) M. BOIE 17817df37ff3SCatalin\(ux\) M. BOIE return ret; 17827df37ff3SCatalin\(ux\) M. BOIE } 17837df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 17847df37ff3SCatalin\(ux\) M. BOIE 1785b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 178652eeeb84SYOSHIFUJI Hideaki { 178752eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 178852eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 178952eeeb84SYOSHIFUJI Hideaki int onlink; 179052eeeb84SYOSHIFUJI Hideaki 179152eeeb84SYOSHIFUJI Hideaki onlink = 0; 179252eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 179352eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 179452eeeb84SYOSHIFUJI Hideaki if (idev) { 179552eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1796502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 179752eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 179852eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 179952eeeb84SYOSHIFUJI Hideaki if (onlink) 180052eeeb84SYOSHIFUJI Hideaki break; 180152eeeb84SYOSHIFUJI Hideaki } 180252eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 180352eeeb84SYOSHIFUJI Hideaki } 180452eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 180552eeeb84SYOSHIFUJI Hideaki return onlink; 180652eeeb84SYOSHIFUJI Hideaki } 180752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 180852eeeb84SYOSHIFUJI Hideaki 18099acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 18101cab3da6SDaniel Lezcano struct net_device *dev, int strict) 18111da177e4SLinus Torvalds { 1812b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1813ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 18141da177e4SLinus Torvalds 18155c578aedSstephen hemminger rcu_read_lock_bh(); 1816b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1817878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 18181cab3da6SDaniel Lezcano continue; 18191da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 182063159f29SIan Morris if (!dev || ifp->idev->dev == dev || 18211da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1822b79d1d54SDavid S. Miller result = ifp; 18231da177e4SLinus Torvalds in6_ifa_hold(ifp); 18241da177e4SLinus Torvalds break; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds } 18285c578aedSstephen hemminger rcu_read_unlock_bh(); 18291da177e4SLinus Torvalds 1830b79d1d54SDavid S. Miller return result; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 18341da177e4SLinus Torvalds 1835cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 18361da177e4SLinus Torvalds { 18373d171f39SLubomir Rintel if (dad_failed) 18383d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 18393d171f39SLubomir Rintel 18401da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 18411da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1842c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 18431da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 18441da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1845e2577a06SHerbert Xu if (dad_failed) 1846e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 18471da177e4SLinus Torvalds in6_ifa_put(ifp); 18481da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 18491da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 18501da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 18511da177e4SLinus Torvalds ifpub = ifp->ifpub; 18521da177e4SLinus Torvalds if (ifpub) { 18531da177e4SLinus Torvalds in6_ifa_hold(ifpub); 18541da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18551da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 18561da177e4SLinus Torvalds in6_ifa_put(ifpub); 18571da177e4SLinus Torvalds } else { 18581da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds ipv6_del_addr(ifp); 1861c15b1ccaSHannes Frederic Sowa } else { 18621da177e4SLinus Torvalds ipv6_del_addr(ifp); 18631da177e4SLinus Torvalds } 1864c15b1ccaSHannes Frederic Sowa } 18651da177e4SLinus Torvalds 1866f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1867f2344a13SHerbert Xu { 1868f2344a13SHerbert Xu int err = -ENOENT; 1869f2344a13SHerbert Xu 18708e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1871f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1872f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1873f2344a13SHerbert Xu err = 0; 1874f2344a13SHerbert Xu } 18758e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1876f2344a13SHerbert Xu 1877f2344a13SHerbert Xu return err; 1878f2344a13SHerbert Xu } 1879f2344a13SHerbert Xu 18803c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 18813c21edbdSYOSHIFUJI Hideaki { 18821b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 18831855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 18849bdd8d40SBrian Haley 1885853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1886853dc2e0SUrsula Braun in6_ifa_put(ifp); 1887f2344a13SHerbert Xu return; 1888853dc2e0SUrsula Braun } 1889f2344a13SHerbert Xu 1890e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 18910522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 18929bdd8d40SBrian Haley 18935f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18941b34be74SYOSHIFUJI Hideaki 18955f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 18965f40ef77SHannes Frederic Sowa int scope = ifp->scope; 18975f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 18985f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 18995f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 19005f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 19015f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 19025f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 19035f40ef77SHannes Frederic Sowa 19041855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 19055f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 19065f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19075f40ef77SHannes Frederic Sowa goto errdad; 19085f40ef77SHannes Frederic Sowa } 19095f40ef77SHannes Frederic Sowa 19105f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 19115f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 19125f40ef77SHannes Frederic Sowa idev)) 19135f40ef77SHannes Frederic Sowa goto errdad; 19145f40ef77SHannes Frederic Sowa 19155f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 19165f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 19175f40ef77SHannes Frederic Sowa 19185f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 19195f40ef77SHannes Frederic Sowa 19205f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 19215f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 19225f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 19235f40ef77SHannes Frederic Sowa goto lock_errdad; 19245f40ef77SHannes Frederic Sowa 19255f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 19265f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19275f40ef77SHannes Frederic Sowa 19285f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 19295f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 19305f40ef77SHannes Frederic Sowa preferred_lft); 19315f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 19325f40ef77SHannes Frederic Sowa goto lock_errdad; 19335f40ef77SHannes Frederic Sowa 19345f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 19355f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 19365f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 19375f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 19385f40ef77SHannes Frederic Sowa 19391855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 19405f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 19415f40ef77SHannes Frederic Sowa lock_errdad: 19425f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 19431b34be74SYOSHIFUJI Hideaki } 19441b34be74SYOSHIFUJI Hideaki 19455f40ef77SHannes Frederic Sowa errdad: 1946c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 1947c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 19488e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1949c15b1ccaSHannes Frederic Sowa 1950c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 1951751eb6b6SWei Yongjun in6_ifa_put(ifp); 19523c21edbdSYOSHIFUJI Hideaki } 19531da177e4SLinus Torvalds 1954a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 1955a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 1956b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 19571da177e4SLinus Torvalds { 19581da177e4SLinus Torvalds struct in6_addr maddr; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19611da177e4SLinus Torvalds return; 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19641da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 1967a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 1968b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 19691da177e4SLinus Torvalds { 19701da177e4SLinus Torvalds struct in6_addr maddr; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19731da177e4SLinus Torvalds return; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19761da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 1979a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 198020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 19811da177e4SLinus Torvalds { 19821da177e4SLinus Torvalds struct in6_addr addr; 1983c15b1ccaSHannes Frederic Sowa 198488ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 19852bda8a0cSBjørn Mork return; 19861da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19871da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19881da177e4SLinus Torvalds return; 1989013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 1992a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 199320380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 19941da177e4SLinus Torvalds { 19951da177e4SLinus Torvalds struct in6_addr addr; 1996c15b1ccaSHannes Frederic Sowa 199788ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 199832019e65SYOSHIFUJI Hideaki return; 19991da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20001da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20011da177e4SLinus Torvalds return; 20021da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 200506a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 200606a4c1c5Salex.bluesman.smirnov@gmail.com { 20075241c2d7SAlexander Aring if (dev->addr_len != EUI64_ADDR_LEN) 200806a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 20095241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 20105e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 201106a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 201206a4c1c5Salex.bluesman.smirnov@gmail.com } 201306a4c1c5Salex.bluesman.smirnov@gmail.com 2014cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2015cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2016cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2017cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2018cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2019cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2020cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2021cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2022cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2023cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2024cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2025cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2026cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2027cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2028073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2029073a8e0eSYOSHIFUJI Hideaki { 20301da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 20311da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 20321da177e4SLinus Torvalds return -1; 20331da177e4SLinus Torvalds memset(eui, 0, 7); 20341da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 20351da177e4SLinus Torvalds return 0; 2036073a8e0eSYOSHIFUJI Hideaki } 2037073a8e0eSYOSHIFUJI Hideaki 2038073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2039073a8e0eSYOSHIFUJI Hideaki { 20401da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 20411da177e4SLinus Torvalds return -1; 20421da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 20431da177e4SLinus Torvalds eui[0] |= 2; 20441da177e4SLinus Torvalds return 0; 20451da177e4SLinus Torvalds } 2046073a8e0eSYOSHIFUJI Hideaki 2047c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2048dfd982baSYOSHIFUJI Hideaki { 20499af28511SSascha Hlusiak if (addr == 0) 20509af28511SSascha Hlusiak return -1; 2051dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2052dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2053dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2054dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2055dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2056dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2057dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2058dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2059dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2060dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2061dfd982baSYOSHIFUJI Hideaki return 0; 2062dfd982baSYOSHIFUJI Hideaki } 2063dfd982baSYOSHIFUJI Hideaki 2064dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2065dfd982baSYOSHIFUJI Hideaki { 2066dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2067dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2068dfd982baSYOSHIFUJI Hideaki return -1; 2069dfd982baSYOSHIFUJI Hideaki } 2070dfd982baSYOSHIFUJI Hideaki 2071aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2072aee80b54Sstephen hemminger { 2073aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2074aee80b54Sstephen hemminger } 2075aee80b54Sstephen hemminger 2076e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2077e837735eSNicolas Dichtel { 2078e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2079e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2080e837735eSNicolas Dichtel eui[3] = 0xFF; 2081e837735eSNicolas Dichtel eui[4] = 0xFE; 2082e837735eSNicolas Dichtel eui[0] ^= 2; 2083e837735eSNicolas Dichtel return 0; 2084e837735eSNicolas Dichtel } 2085e837735eSNicolas Dichtel 2086073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2087073a8e0eSYOSHIFUJI Hideaki { 2088073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2089073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2090073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2091073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2092073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2093073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2094073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2095073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2096c7dc89c0SFred L. Templin case ARPHRD_SIT: 2097dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2098aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2099aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2100e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 210106a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 2102cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2103cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2104e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 2105e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2106073a8e0eSYOSHIFUJI Hideaki } 21071da177e4SLinus Torvalds return -1; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 21111da177e4SLinus Torvalds { 21121da177e4SLinus Torvalds int err = -1; 21131da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2116602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2117602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2118602582caSHannes Frederic Sowa break; 21191da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 21201da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 21211da177e4SLinus Torvalds err = 0; 21221da177e4SLinus Torvalds break; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21261da177e4SLinus Torvalds return err; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 2130eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 21311da177e4SLinus Torvalds { 21321da177e4SLinus Torvalds regen: 2133955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 21341da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds /* 21371da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 21381da177e4SLinus Torvalds * check if generated address is not inappropriate 21391da177e4SLinus Torvalds * 21401da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 21411da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2142c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 21431da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 21441da177e4SLinus Torvalds * - value 0 21451da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 21461da177e4SLinus Torvalds */ 21471da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 21481da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 21491da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 21501da177e4SLinus Torvalds goto regen; 21511da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 21521da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 21531da177e4SLinus Torvalds goto regen; 21541da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 21551da177e4SLinus Torvalds goto regen; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 21601da177e4SLinus Torvalds { 21611da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 21621da177e4SLinus Torvalds unsigned long expires; 21631da177e4SLinus Torvalds 21648814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 21651da177e4SLinus Torvalds write_lock_bh(&idev->lock); 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds if (idev->dead) 21681da177e4SLinus Torvalds goto out; 21691da177e4SLinus Torvalds 2170eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds expires = jiffies + 21731da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 21741f9248e5SJiri Pirko idev->cnf.regen_max_retry * idev->cnf.dad_transmits * 21751f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - 2176784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 21771da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 2178f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 2179f3213831SJoe Perches __func__, idev->dev->name); 21801da177e4SLinus Torvalds goto out; 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 21841da177e4SLinus Torvalds in6_dev_hold(idev); 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds out: 21871da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 21888814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 21891da177e4SLinus Torvalds in6_dev_put(idev); 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 2192eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 21938e5e8f30SEldad Zack { 21941da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 2195eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds /* 21991da177e4SLinus Torvalds * Add prefix route. 22001da177e4SLinus Torvalds */ 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds static void 22031da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2204e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 22051da177e4SLinus Torvalds { 220686872cb5SThomas Graf struct fib6_config cfg = { 2207ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 220886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 220986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 221086872cb5SThomas Graf .fc_expires = expires, 221186872cb5SThomas Graf .fc_dst_len = plen, 221286872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2213c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2214f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 221586872cb5SThomas Graf }; 22161da177e4SLinus Torvalds 22174e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 22201da177e4SLinus Torvalds This thing is done here expecting that the whole 22211da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 22221da177e4SLinus Torvalds */ 222307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 22241da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 222586872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 22260be669bbSJoerg Roedel #endif 22271da177e4SLinus Torvalds 222886872cb5SThomas Graf ip6_route_add(&cfg); 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds 223114ef37b6SAndreas Hofmeister 223214ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 223314ef37b6SAndreas Hofmeister int plen, 223414ef37b6SAndreas Hofmeister const struct net_device *dev, 223514ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 223614ef37b6SAndreas Hofmeister { 223714ef37b6SAndreas Hofmeister struct fib6_node *fn; 223814ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 223914ef37b6SAndreas Hofmeister struct fib6_table *table; 2240ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 224114ef37b6SAndreas Hofmeister 2242ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 224363159f29SIan Morris if (!table) 224414ef37b6SAndreas Hofmeister return NULL; 224514ef37b6SAndreas Hofmeister 22465744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 224714ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 224814ef37b6SAndreas Hofmeister if (!fn) 224914ef37b6SAndreas Hofmeister goto out; 22501f56a01fSMartin KaFai Lau 22511f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 225214ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2253d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 225414ef37b6SAndreas Hofmeister continue; 225514ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 225614ef37b6SAndreas Hofmeister continue; 225785da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 225814ef37b6SAndreas Hofmeister continue; 225914ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 226014ef37b6SAndreas Hofmeister break; 226114ef37b6SAndreas Hofmeister } 226214ef37b6SAndreas Hofmeister out: 22635744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 226414ef37b6SAndreas Hofmeister return rt; 226514ef37b6SAndreas Hofmeister } 226614ef37b6SAndreas Hofmeister 226714ef37b6SAndreas Hofmeister 22681da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 22711da177e4SLinus Torvalds { 227286872cb5SThomas Graf struct fib6_config cfg = { 2273ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 227486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 227586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 227686872cb5SThomas Graf .fc_dst_len = 8, 227786872cb5SThomas Graf .fc_flags = RTF_UP, 2278c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 227986872cb5SThomas Graf }; 22801da177e4SLinus Torvalds 228186872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 228286872cb5SThomas Graf 228386872cb5SThomas Graf ip6_route_add(&cfg); 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 22871da177e4SLinus Torvalds { 22881da177e4SLinus Torvalds struct inet6_dev *idev; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds ASSERT_RTNL(); 22911da177e4SLinus Torvalds 2292e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2293e21e8467SStephen Hemminger if (!idev) 229464e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 229564e724f6SBrian Haley 229664e724f6SBrian Haley if (idev->cnf.disable_ipv6) 229764e724f6SBrian Haley return ERR_PTR(-EACCES); 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds /* Add default multicast route */ 2300ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 23011da177e4SLinus Torvalds addrconf_add_mroute(dev); 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds return idev; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 230653bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 230753bd6749SJiri Pirko struct inet6_ifaddr *ifp, 230853bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 230953bd6749SJiri Pirko bool create, unsigned long now) 231053bd6749SJiri Pirko { 231153bd6749SJiri Pirko u32 flags; 231253bd6749SJiri Pirko struct inet6_ifaddr *ift; 231353bd6749SJiri Pirko 231453bd6749SJiri Pirko read_lock_bh(&idev->lock); 231553bd6749SJiri Pirko /* update all temporary addresses in the list */ 231653bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 231753bd6749SJiri Pirko int age, max_valid, max_prefered; 231853bd6749SJiri Pirko 231953bd6749SJiri Pirko if (ifp != ift->ifpub) 232053bd6749SJiri Pirko continue; 232153bd6749SJiri Pirko 232253bd6749SJiri Pirko /* RFC 4941 section 3.3: 232353bd6749SJiri Pirko * If a received option will extend the lifetime of a public 232453bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 232553bd6749SJiri Pirko * be extended, subject to the overall constraint that no 232653bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 232753bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 232853bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 232953bd6749SJiri Pirko */ 233053bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 233153bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 233253bd6749SJiri Pirko if (max_valid < 0) 233353bd6749SJiri Pirko max_valid = 0; 233453bd6749SJiri Pirko 233553bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 233653bd6749SJiri Pirko idev->cnf.max_desync_factor - age; 233753bd6749SJiri Pirko if (max_prefered < 0) 233853bd6749SJiri Pirko max_prefered = 0; 233953bd6749SJiri Pirko 234053bd6749SJiri Pirko if (valid_lft > max_valid) 234153bd6749SJiri Pirko valid_lft = max_valid; 234253bd6749SJiri Pirko 234353bd6749SJiri Pirko if (prefered_lft > max_prefered) 234453bd6749SJiri Pirko prefered_lft = max_prefered; 234553bd6749SJiri Pirko 234653bd6749SJiri Pirko spin_lock(&ift->lock); 234753bd6749SJiri Pirko flags = ift->flags; 234853bd6749SJiri Pirko ift->valid_lft = valid_lft; 234953bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 235053bd6749SJiri Pirko ift->tstamp = now; 235153bd6749SJiri Pirko if (prefered_lft > 0) 235253bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 235353bd6749SJiri Pirko 235453bd6749SJiri Pirko spin_unlock(&ift->lock); 235553bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 235653bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 235753bd6749SJiri Pirko } 235853bd6749SJiri Pirko 235953bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 236053bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 236153bd6749SJiri Pirko /* When a new public address is created as described 236253bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 236353bd6749SJiri Pirko * Also create a temporary address if it's enabled but 236453bd6749SJiri Pirko * no temporary address currently exists. 236553bd6749SJiri Pirko */ 236653bd6749SJiri Pirko read_unlock_bh(&idev->lock); 236753bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 236853bd6749SJiri Pirko } else { 236953bd6749SJiri Pirko read_unlock_bh(&idev->lock); 237053bd6749SJiri Pirko } 237153bd6749SJiri Pirko } 237253bd6749SJiri Pirko 2373cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2374cc9da6ccSBjørn Mork { 2375cc9da6ccSBjørn Mork return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2376cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2377cc9da6ccSBjørn Mork } 2378cc9da6ccSBjørn Mork 2379cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 23804f672235SAlexander Aring const struct prefix_info *pinfo, 23814f672235SAlexander Aring struct inet6_dev *in6_dev, 2382cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2383cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 23844f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 23854f672235SAlexander Aring { 23864f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 23874f672235SAlexander Aring int create = 0, update_lft = 0; 23884f672235SAlexander Aring 23894f672235SAlexander Aring if (!ifp && valid_lft) { 23904f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 23914f672235SAlexander Aring 23924f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 23934f672235SAlexander Aring if (in6_dev->cnf.optimistic_dad && 23944f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 23954f672235SAlexander Aring addr_flags |= IFA_F_OPTIMISTIC; 23964f672235SAlexander Aring #endif 23974f672235SAlexander Aring 23984f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 23994f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 24004f672235SAlexander Aring */ 24014f672235SAlexander Aring if (!max_addresses || 24024f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 24034f672235SAlexander Aring ifp = ipv6_add_addr(in6_dev, addr, NULL, 24044f672235SAlexander Aring pinfo->prefix_len, 24054f672235SAlexander Aring addr_type&IPV6_ADDR_SCOPE_MASK, 24064f672235SAlexander Aring addr_flags, valid_lft, 24074f672235SAlexander Aring prefered_lft); 24084f672235SAlexander Aring 24094f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 24104f672235SAlexander Aring return -1; 24114f672235SAlexander Aring 24124f672235SAlexander Aring update_lft = 0; 24134f672235SAlexander Aring create = 1; 24144f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24154f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 24164f672235SAlexander Aring ifp->cstamp = jiffies; 24174f672235SAlexander Aring ifp->tokenized = tokenized; 24184f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24194f672235SAlexander Aring addrconf_dad_start(ifp); 24204f672235SAlexander Aring } 24214f672235SAlexander Aring 24224f672235SAlexander Aring if (ifp) { 24234f672235SAlexander Aring u32 flags; 24244f672235SAlexander Aring unsigned long now; 24254f672235SAlexander Aring u32 stored_lft; 24264f672235SAlexander Aring 24274f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 24284f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24294f672235SAlexander Aring now = jiffies; 24304f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 24314f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 24324f672235SAlexander Aring else 24334f672235SAlexander Aring stored_lft = 0; 24344f672235SAlexander Aring if (!update_lft && !create && stored_lft) { 24354f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 24364f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 24374f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 24384f672235SAlexander Aring 24394f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 24404f672235SAlexander Aring * "Note that the preferred lifetime of the 24414f672235SAlexander Aring * corresponding address is always reset to 24424f672235SAlexander Aring * the Preferred Lifetime in the received 24434f672235SAlexander Aring * Prefix Information option, regardless of 24444f672235SAlexander Aring * whether the valid lifetime is also reset or 24454f672235SAlexander Aring * ignored." 24464f672235SAlexander Aring * 24474f672235SAlexander Aring * So we should always update prefered_lft here. 24484f672235SAlexander Aring */ 24494f672235SAlexander Aring update_lft = 1; 24504f672235SAlexander Aring } 24514f672235SAlexander Aring 24524f672235SAlexander Aring if (update_lft) { 24534f672235SAlexander Aring ifp->valid_lft = valid_lft; 24544f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 24554f672235SAlexander Aring ifp->tstamp = now; 24564f672235SAlexander Aring flags = ifp->flags; 24574f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 24584f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24594f672235SAlexander Aring 24604f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 24614f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 24624f672235SAlexander Aring } else 24634f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24644f672235SAlexander Aring 24654f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 24664f672235SAlexander Aring create, now); 24674f672235SAlexander Aring 24684f672235SAlexander Aring in6_ifa_put(ifp); 24694f672235SAlexander Aring addrconf_verify(); 24704f672235SAlexander Aring } 24714f672235SAlexander Aring 24724f672235SAlexander Aring return 0; 24734f672235SAlexander Aring } 2474cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 24754f672235SAlexander Aring 2476e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 24771da177e4SLinus Torvalds { 24781da177e4SLinus Torvalds struct prefix_info *pinfo; 24791da177e4SLinus Torvalds __u32 valid_lft; 24801da177e4SLinus Torvalds __u32 prefered_lft; 24814f672235SAlexander Aring int addr_type, err; 248264236f3fSHannes Frederic Sowa u32 addr_flags = 0; 24831da177e4SLinus Torvalds struct inet6_dev *in6_dev; 248456d417b1SBrian Haley struct net *net = dev_net(dev); 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2489ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 24901da177e4SLinus Torvalds return; 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds /* 24941da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 24951da177e4SLinus Torvalds */ 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 25001da177e4SLinus Torvalds return; 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 25031da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2506e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 25071da177e4SLinus Torvalds return; 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 25111da177e4SLinus Torvalds 251263159f29SIan Morris if (!in6_dev) { 2513e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2514e87cc472SJoe Perches dev->name); 25151da177e4SLinus Torvalds return; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds /* 25191da177e4SLinus Torvalds * Two things going on here: 25201da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 25211da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 25221da177e4SLinus Torvalds */ 25231da177e4SLinus Torvalds 25244bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 25254bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 25264bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 25274bed72e4SYOSHIFUJI Hideaki 25281da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 25296f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 25306f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 25316f704992SYOSHIFUJI Hideaki * not good. 25321da177e4SLinus Torvalds */ 25334bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 25344bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 25354bed72e4SYOSHIFUJI Hideaki else 25364bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 25373dd4bc68SYOSHIFUJI Hideaki 25384bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 25394bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 25401da177e4SLinus Torvalds 254114ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 254214ef37b6SAndreas Hofmeister pinfo->prefix_len, 254314ef37b6SAndreas Hofmeister dev, 254414ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 254514ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 25461da177e4SLinus Torvalds 254714ef37b6SAndreas Hofmeister if (rt) { 25486f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 25491da177e4SLinus Torvalds if (valid_lft == 0) { 2550e0a1ad73SThomas Graf ip6_del_rt(rt); 25511da177e4SLinus Torvalds rt = NULL; 25524bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 25536f704992SYOSHIFUJI Hideaki /* not infinity */ 25541716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 25556f704992SYOSHIFUJI Hideaki } else { 25561716a961SGao feng rt6_clean_expires(rt); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds } else if (valid_lft) { 25596f704992SYOSHIFUJI Hideaki clock_t expires = 0; 25604bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 25614bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 25626f704992SYOSHIFUJI Hideaki /* not infinity */ 25636f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 25646f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 25656f704992SYOSHIFUJI Hideaki } 25661da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 25676f704992SYOSHIFUJI Hideaki dev, expires, flags); 25681da177e4SLinus Torvalds } 256994e187c0SAmerigo Wang ip6_rt_put(rt); 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 25751da177e4SLinus Torvalds struct in6_addr addr; 2576f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 25791da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2580f53adae4SDaniel Borkmann 2581f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2582f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2583f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2584f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2585f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2586617fe29dSDaniel Borkmann tokenized = true; 2587cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2588622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2589622c81d5SHannes Frederic Sowa in6_dev)) { 259064236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2591622c81d5SHannes Frederic Sowa goto ok; 2592f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 25931da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 25944f672235SAlexander Aring goto put; 2595f997c55cSAlexander Aring } else { 2596f997c55cSAlexander Aring dev_addr_generated = true; 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds goto ok; 25991da177e4SLinus Torvalds } 2600e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 26011da177e4SLinus Torvalds pinfo->prefix_len); 26024f672235SAlexander Aring goto put; 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds ok: 26054f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 26064f672235SAlexander Aring &addr, addr_type, 26074f672235SAlexander Aring addr_flags, sllao, 26084f672235SAlexander Aring tokenized, valid_lft, 26098a226b2cSJiri Benc prefered_lft); 26104f672235SAlexander Aring if (err) 26114f672235SAlexander Aring goto put; 2612f997c55cSAlexander Aring 2613f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2614f997c55cSAlexander Aring * successful which will be notified. 2615f997c55cSAlexander Aring */ 2616f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2617f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2618f997c55cSAlexander Aring tokenized, valid_lft, 2619f997c55cSAlexander Aring prefered_lft, 2620f997c55cSAlexander Aring dev_addr_generated); 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 26234f672235SAlexander Aring put: 26241da177e4SLinus Torvalds in6_dev_put(in6_dev); 26251da177e4SLinus Torvalds } 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds /* 26281da177e4SLinus Torvalds * Set destination address. 26291da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 26301da177e4SLinus Torvalds * device. 26311da177e4SLinus Torvalds */ 2632af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 26331da177e4SLinus Torvalds { 26341da177e4SLinus Torvalds struct in6_ifreq ireq; 26351da177e4SLinus Torvalds struct net_device *dev; 26361da177e4SLinus Torvalds int err = -EINVAL; 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds rtnl_lock(); 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds err = -EFAULT; 26411da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 26421da177e4SLinus Torvalds goto err_exit; 26431da177e4SLinus Torvalds 2644af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds err = -ENODEV; 264763159f29SIan Morris if (!dev) 26481da177e4SLinus Torvalds goto err_exit; 26491da177e4SLinus Torvalds 265007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 26511da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 26525bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26531da177e4SLinus Torvalds struct ifreq ifr; 26541da177e4SLinus Torvalds struct ip_tunnel_parm p; 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 26571da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 26581da177e4SLinus Torvalds goto err_exit; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 26611da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 26621da177e4SLinus Torvalds p.iph.saddr = 0; 26631da177e4SLinus Torvalds p.iph.version = 4; 26641da177e4SLinus Torvalds p.iph.ihl = 5; 26651da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 26661da177e4SLinus Torvalds p.iph.ttl = 64; 2667d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 26681da177e4SLinus Torvalds 26695bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 26705bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 26715bc3eb7eSStephen Hemminger 26725bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 26735bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 26741da177e4SLinus Torvalds set_fs(oldfs); 26755bc3eb7eSStephen Hemminger } else 26765bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds if (err == 0) { 26791da177e4SLinus Torvalds err = -ENOBUFS; 2680af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2681af284937SDaniel Lezcano if (!dev) 26821da177e4SLinus Torvalds goto err_exit; 26831da177e4SLinus Torvalds err = dev_open(dev); 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds } 26860be669bbSJoerg Roedel #endif 26871da177e4SLinus Torvalds 26881da177e4SLinus Torvalds err_exit: 26891da177e4SLinus Torvalds rtnl_unlock(); 26901da177e4SLinus Torvalds return err; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds 269393a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 269493a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 269593a714d6SMadhu Challa { 269693a714d6SMadhu Challa int ret; 269793a714d6SMadhu Challa 269893a714d6SMadhu Challa ASSERT_RTNL(); 269993a714d6SMadhu Challa 270093a714d6SMadhu Challa lock_sock(sk); 270193a714d6SMadhu Challa if (join) 270254ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 270393a714d6SMadhu Challa else 270454ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 270593a714d6SMadhu Challa release_sock(sk); 270693a714d6SMadhu Challa 270793a714d6SMadhu Challa return ret; 270893a714d6SMadhu Challa } 270993a714d6SMadhu Challa 27101da177e4SLinus Torvalds /* 27111da177e4SLinus Torvalds * Manual configuration of address on an interface 27121da177e4SLinus Torvalds */ 2713479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2714479840ffSJiri Pirko const struct in6_addr *pfx, 2715caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2716479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2717479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 27181da177e4SLinus Torvalds { 27191da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27201da177e4SLinus Torvalds struct inet6_dev *idev; 27211da177e4SLinus Torvalds struct net_device *dev; 272293a714d6SMadhu Challa unsigned long timeout; 272393a714d6SMadhu Challa clock_t expires; 27241da177e4SLinus Torvalds int scope; 27256f704992SYOSHIFUJI Hideaki u32 flags; 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds ASSERT_RTNL(); 27281da177e4SLinus Torvalds 272924ef0da7SThomas Graf if (plen > 128) 273024ef0da7SThomas Graf return -EINVAL; 273124ef0da7SThomas Graf 27320778769dSNoriaki TAKAMIYA /* check the lifetime */ 27330778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 27340778769dSNoriaki TAKAMIYA return -EINVAL; 27350778769dSNoriaki TAKAMIYA 273653bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 273753bd6749SJiri Pirko return -EINVAL; 273853bd6749SJiri Pirko 2739af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2740af284937SDaniel Lezcano if (!dev) 27411da177e4SLinus Torvalds return -ENODEV; 27421da177e4SLinus Torvalds 274364e724f6SBrian Haley idev = addrconf_add_dev(dev); 274464e724f6SBrian Haley if (IS_ERR(idev)) 274564e724f6SBrian Haley return PTR_ERR(idev); 27461da177e4SLinus Torvalds 274793a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 274893a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 274993a714d6SMadhu Challa true, pfx, ifindex); 275093a714d6SMadhu Challa 275193a714d6SMadhu Challa if (ret < 0) 275293a714d6SMadhu Challa return ret; 275393a714d6SMadhu Challa } 275493a714d6SMadhu Challa 27551da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 27561da177e4SLinus Torvalds 27574bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 27584bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27594bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 27604bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 27616f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 27624bed72e4SYOSHIFUJI Hideaki } else { 27634bed72e4SYOSHIFUJI Hideaki expires = 0; 27644bed72e4SYOSHIFUJI Hideaki flags = 0; 27654bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 27666f704992SYOSHIFUJI Hideaki } 27670778769dSNoriaki TAKAMIYA 27684bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 27694bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27704bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 27710778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 27724bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 27734bed72e4SYOSHIFUJI Hideaki } 27740778769dSNoriaki TAKAMIYA 27758a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 27768a226b2cSJiri Benc valid_lft, prefered_lft); 27770778769dSNoriaki TAKAMIYA 27781da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2779761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 278046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 27816f704992SYOSHIFUJI Hideaki expires, flags); 2782761aac73SThomas Haller } 2783761aac73SThomas Haller 278495c385b4SNeil Horman /* 278595c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 278695c385b4SNeil Horman * that the Optimistic flag should not be set for 278795c385b4SNeil Horman * manually configured addresses 278895c385b4SNeil Horman */ 2789cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 279053bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 279153bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 279253bd6749SJiri Pirko true, jiffies); 27931da177e4SLinus Torvalds in6_ifa_put(ifp); 2794c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 27951da177e4SLinus Torvalds return 0; 279693a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 279793a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 279893a714d6SMadhu Challa false, pfx, ifindex); 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds return PTR_ERR(ifp); 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 28046046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 28056046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28081da177e4SLinus Torvalds struct inet6_dev *idev; 28091da177e4SLinus Torvalds struct net_device *dev; 28101da177e4SLinus Torvalds 281124ef0da7SThomas Graf if (plen > 128) 281224ef0da7SThomas Graf return -EINVAL; 281324ef0da7SThomas Graf 2814af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2815af284937SDaniel Lezcano if (!dev) 28161da177e4SLinus Torvalds return -ENODEV; 28171da177e4SLinus Torvalds 2818e5d08d71SIan Morris idev = __in6_dev_get(dev); 281963159f29SIan Morris if (!idev) 28201da177e4SLinus Torvalds return -ENXIO; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2823502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 28241da177e4SLinus Torvalds if (ifp->prefix_len == plen && 28251da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 28261da177e4SLinus Torvalds in6_ifa_hold(ifp); 28271da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28281da177e4SLinus Torvalds 28296046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 28306046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 28316046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 28326046d5b4SHeiner Kallweit jiffies); 28331da177e4SLinus Torvalds ipv6_del_addr(ifp); 28346046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 283593a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 283693a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 283793a714d6SMadhu Challa false, pfx, dev->ifindex); 283893a714d6SMadhu Challa } 28391da177e4SLinus Torvalds return 0; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28431da177e4SLinus Torvalds return -EADDRNOTAVAIL; 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds 2847af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 28481da177e4SLinus Torvalds { 28491da177e4SLinus Torvalds struct in6_ifreq ireq; 28501da177e4SLinus Torvalds int err; 28511da177e4SLinus Torvalds 2852af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28531da177e4SLinus Torvalds return -EPERM; 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28561da177e4SLinus Torvalds return -EFAULT; 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds rtnl_lock(); 2859caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2860af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2861af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 28621da177e4SLinus Torvalds rtnl_unlock(); 28631da177e4SLinus Torvalds return err; 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 2866af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds struct in6_ifreq ireq; 28691da177e4SLinus Torvalds int err; 28701da177e4SLinus Torvalds 2871af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28721da177e4SLinus Torvalds return -EPERM; 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28751da177e4SLinus Torvalds return -EFAULT; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds rtnl_lock(); 28786046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2879af284937SDaniel Lezcano ireq.ifr6_prefixlen); 28801da177e4SLinus Torvalds rtnl_unlock(); 28811da177e4SLinus Torvalds return err; 28821da177e4SLinus Torvalds } 28831da177e4SLinus Torvalds 2884b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2885b5f348e5SIlpo Järvinen int plen, int scope) 2886b5f348e5SIlpo Järvinen { 2887b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2888b5f348e5SIlpo Järvinen 28898a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 289007edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 289107edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2892b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2893b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2894b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2895b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2896b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2897b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2898b5f348e5SIlpo Järvinen } 2899b5f348e5SIlpo Järvinen } 2900b5f348e5SIlpo Järvinen 290107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 29021da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 29031da177e4SLinus Torvalds { 29041da177e4SLinus Torvalds struct in6_addr addr; 29051da177e4SLinus Torvalds struct net_device *dev; 2906c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2907929c9cf3SNicolas Dichtel int scope, plen; 2908f0e2acfaSNicolas Dichtel u32 pflags = 0; 29091da177e4SLinus Torvalds 29101da177e4SLinus Torvalds ASSERT_RTNL(); 29111da177e4SLinus Torvalds 29121da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 29131da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 29141da177e4SLinus Torvalds 29151da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 29161da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 29171da177e4SLinus Torvalds scope = IFA_LINK; 2918929c9cf3SNicolas Dichtel plen = 64; 29191da177e4SLinus Torvalds } else { 29201da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2921929c9cf3SNicolas Dichtel plen = 96; 2922f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 29231da177e4SLinus Torvalds } 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2926929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2927f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 29281da177e4SLinus Torvalds return; 29291da177e4SLinus Torvalds } 29301da177e4SLinus Torvalds 29316fda7350SBenjamin Thery for_each_netdev(net, dev) { 2932e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 29331da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 29341da177e4SLinus Torvalds struct in_ifaddr *ifa; 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds int flag = scope; 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 29431da177e4SLinus Torvalds continue; 29441da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 29451da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 29461da177e4SLinus Torvalds continue; 29471da177e4SLinus Torvalds flag |= IFA_HOST; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 2950b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 2951f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 2952f0e2acfaSNicolas Dichtel pflags); 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds } 29570be669bbSJoerg Roedel #endif 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 29601da177e4SLinus Torvalds { 29611da177e4SLinus Torvalds struct inet6_dev *idev; 296225fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 296325fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 296425fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds /* ::1 */ 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds ASSERT_RTNL(); 29691da177e4SLinus Torvalds 2970e5d08d71SIan Morris idev = ipv6_find_idev(dev); 297163159f29SIan Morris if (!idev) { 297291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 29731da177e4SLinus Torvalds return; 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 2976b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 297725fb6ca4SBalakumaran Kannan 297825fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 297925fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 298025fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 298125fb6ca4SBalakumaran Kannan continue; 298225fb6ca4SBalakumaran Kannan 298325fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 298425fb6ca4SBalakumaran Kannan if (!idev) 298525fb6ca4SBalakumaran Kannan continue; 298625fb6ca4SBalakumaran Kannan 298725fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 298825fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 298925fb6ca4SBalakumaran Kannan 299025fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 299125fb6ca4SBalakumaran Kannan continue; 299225fb6ca4SBalakumaran Kannan 299333d99113SGao feng if (sp_ifa->rt) { 299433d99113SGao feng /* This dst has been added to garbage list when 299533d99113SGao feng * lo device down, release this obsolete dst and 299633d99113SGao feng * reallocate a new router for ifa. 299733d99113SGao feng */ 299833d99113SGao feng if (sp_ifa->rt->dst.obsolete > 0) { 299933d99113SGao feng ip6_rt_put(sp_ifa->rt); 300033d99113SGao feng sp_ifa->rt = NULL; 300133d99113SGao feng } else { 3002a881ae1fSGao feng continue; 300333d99113SGao feng } 300433d99113SGao feng } 3005a881ae1fSGao feng 300657ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 300725fb6ca4SBalakumaran Kannan 300825fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 3009534c8779SGao feng if (!IS_ERR(sp_rt)) { 3010534c8779SGao feng sp_ifa->rt = sp_rt; 301125fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 301225fb6ca4SBalakumaran Kannan } 3013534c8779SGao feng } 301425fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 301525fb6ca4SBalakumaran Kannan } 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 30182ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 301964236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 30201da177e4SLinus Torvalds { 30211da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 302264236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 30231da177e4SLinus Torvalds 302495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 302595c385b4SNeil Horman if (idev->cnf.optimistic_dad && 3026702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 302795c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 302895c385b4SNeil Horman #endif 302995c385b4SNeil Horman 303007edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 303107edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 30321da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 303346d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 3034cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 30351da177e4SLinus Torvalds in6_ifa_put(ifp); 30361da177e4SLinus Torvalds } 30371da177e4SLinus Torvalds } 30382ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 30391da177e4SLinus Torvalds 3040622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3041622c81d5SHannes Frederic Sowa { 3042622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3043622c81d5SHannes Frederic Sowa return true; 3044622c81d5SHannes Frederic Sowa 3045622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3046622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3047622c81d5SHannes Frederic Sowa return true; 3048622c81d5SHannes Frederic Sowa 3049622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3050622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3051622c81d5SHannes Frederic Sowa return true; 3052622c81d5SHannes Frederic Sowa 3053622c81d5SHannes Frederic Sowa return false; 3054622c81d5SHannes Frederic Sowa } 3055622c81d5SHannes Frederic Sowa 3056622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3057622c81d5SHannes Frederic Sowa u8 dad_count, 3058622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3059622c81d5SHannes Frederic Sowa { 3060622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3061622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3062622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3063622c81d5SHannes Frederic Sowa 3064622c81d5SHannes Frederic Sowa static union { 3065622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3066622c81d5SHannes Frederic Sowa struct { 3067622c81d5SHannes Frederic Sowa struct in6_addr secret; 3068ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3069622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3070622c81d5SHannes Frederic Sowa u8 dad_count; 3071622c81d5SHannes Frederic Sowa } __packed; 3072622c81d5SHannes Frederic Sowa } data; 3073622c81d5SHannes Frederic Sowa 3074622c81d5SHannes Frederic Sowa struct in6_addr secret; 3075622c81d5SHannes Frederic Sowa struct in6_addr temp; 3076622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3077622c81d5SHannes Frederic Sowa 3078622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3079622c81d5SHannes Frederic Sowa 3080622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3081622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3082622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3083622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3084622c81d5SHannes Frederic Sowa else 3085622c81d5SHannes Frederic Sowa return -1; 3086622c81d5SHannes Frederic Sowa 3087622c81d5SHannes Frederic Sowa retry: 3088622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3089622c81d5SHannes Frederic Sowa 3090622c81d5SHannes Frederic Sowa sha_init(digest); 3091622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3092622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3093622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3094ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3095ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3096622c81d5SHannes Frederic Sowa data.secret = secret; 3097622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3098622c81d5SHannes Frederic Sowa 3099622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3100622c81d5SHannes Frederic Sowa 3101622c81d5SHannes Frederic Sowa temp = *address; 3102ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3103ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3104622c81d5SHannes Frederic Sowa 3105622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3106622c81d5SHannes Frederic Sowa 3107622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3108622c81d5SHannes Frederic Sowa dad_count++; 31091855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3110622c81d5SHannes Frederic Sowa return -1; 3111622c81d5SHannes Frederic Sowa goto retry; 3112622c81d5SHannes Frederic Sowa } 3113622c81d5SHannes Frederic Sowa 3114622c81d5SHannes Frederic Sowa *address = temp; 3115622c81d5SHannes Frederic Sowa return 0; 3116622c81d5SHannes Frederic Sowa } 3117622c81d5SHannes Frederic Sowa 3118cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3119cc9da6ccSBjørn Mork { 3120cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3121cc9da6ccSBjørn Mork 3122cc9da6ccSBjørn Mork if (s->initialized) 3123cc9da6ccSBjørn Mork return; 3124cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3125cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3126cc9da6ccSBjørn Mork s->initialized = true; 3127cc9da6ccSBjørn Mork } 3128cc9da6ccSBjørn Mork 3129bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3130bc91b0f0SJiri Pirko { 3131bc91b0f0SJiri Pirko struct in6_addr addr; 3132bc91b0f0SJiri Pirko 3133ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3134ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3135ca254490SDavid Ahern return; 3136ca254490SDavid Ahern 3137bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3138622c81d5SHannes Frederic Sowa 3139cc9da6ccSBjørn Mork switch (idev->addr_gen_mode) { 3140cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3141cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3142cc9da6ccSBjørn Mork /* fallthrough */ 3143cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3144622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 314564236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 314664236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3147622c81d5SHannes Frederic Sowa else if (prefix_route) 3148622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3149cc9da6ccSBjørn Mork break; 3150cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3151bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3152bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3153bc91b0f0SJiri Pirko * couldn't generate one. 3154bc91b0f0SJiri Pirko */ 3155bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 315664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3157bc91b0f0SJiri Pirko else if (prefix_route) 3158bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3159cc9da6ccSBjørn Mork break; 3160cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3161cc9da6ccSBjørn Mork default: 3162cc9da6ccSBjørn Mork /* will not add any link local address */ 3163cc9da6ccSBjørn Mork break; 3164bc91b0f0SJiri Pirko } 3165bc91b0f0SJiri Pirko } 3166bc91b0f0SJiri Pirko 31671da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 31681da177e4SLinus Torvalds { 31691da177e4SLinus Torvalds struct inet6_dev *idev; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds ASSERT_RTNL(); 31721da177e4SLinus Torvalds 317374235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 317474235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 317574235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 317606a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3177e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3178e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3179cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 3180cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 318174235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 318274235a25SHerbert Xu return; 318374235a25SHerbert Xu } 318474235a25SHerbert Xu 31851da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 318664e724f6SBrian Haley if (IS_ERR(idev)) 31871da177e4SLinus Torvalds return; 31881da177e4SLinus Torvalds 3189cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3190cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3191cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3192cc9da6ccSBjørn Mork idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3193cc9da6ccSBjørn Mork 3194bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 31951da177e4SLinus Torvalds } 31961da177e4SLinus Torvalds 319707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 31981da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 31991da177e4SLinus Torvalds { 32001da177e4SLinus Torvalds struct inet6_dev *idev; 32011da177e4SLinus Torvalds 32021da177e4SLinus Torvalds ASSERT_RTNL(); 32031da177e4SLinus Torvalds 32041da177e4SLinus Torvalds /* 32051da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 32061da177e4SLinus Torvalds * addresses... we should configure all of 32071da177e4SLinus Torvalds * our v4 addrs in the tunnel 32081da177e4SLinus Torvalds */ 32091da177e4SLinus Torvalds 3210e5d08d71SIan Morris idev = ipv6_find_idev(dev); 321163159f29SIan Morris if (!idev) { 321291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 32131da177e4SLinus Torvalds return; 32141da177e4SLinus Torvalds } 32151da177e4SLinus Torvalds 3216c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3217bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3218c7dc89c0SFred L. Templin return; 3219c7dc89c0SFred L. Templin } 3220c7dc89c0SFred L. Templin 32211da177e4SLinus Torvalds sit_add_v4_addrs(idev); 32221da177e4SLinus Torvalds 322362b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 32241da177e4SLinus Torvalds addrconf_add_mroute(dev); 32251da177e4SLinus Torvalds } 32260be669bbSJoerg Roedel #endif 32271da177e4SLinus Torvalds 322807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3229aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3230aee80b54Sstephen hemminger { 3231aee80b54Sstephen hemminger struct inet6_dev *idev; 3232aee80b54Sstephen hemminger 3233aee80b54Sstephen hemminger ASSERT_RTNL(); 3234aee80b54Sstephen hemminger 3235e5d08d71SIan Morris idev = ipv6_find_idev(dev); 323663159f29SIan Morris if (!idev) { 323791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3238aee80b54Sstephen hemminger return; 3239aee80b54Sstephen hemminger } 3240aee80b54Sstephen hemminger 3241bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3242d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3243d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3244aee80b54Sstephen hemminger } 3245aee80b54Sstephen hemminger #endif 3246aee80b54Sstephen hemminger 3247f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev, 3248f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3249f1705ec1SDavid Ahern { 3250f1705ec1SDavid Ahern if (!ifp->rt) { 3251f1705ec1SDavid Ahern struct rt6_info *rt; 3252f1705ec1SDavid Ahern 3253f1705ec1SDavid Ahern rt = addrconf_dst_alloc(idev, &ifp->addr, false); 3254f1705ec1SDavid Ahern if (unlikely(IS_ERR(rt))) 3255f1705ec1SDavid Ahern return PTR_ERR(rt); 3256f1705ec1SDavid Ahern 3257f1705ec1SDavid Ahern ifp->rt = rt; 3258f1705ec1SDavid Ahern } 3259f1705ec1SDavid Ahern 3260f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3261f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3262f1705ec1SDavid Ahern idev->dev, 0, 0); 3263f1705ec1SDavid Ahern } 3264f1705ec1SDavid Ahern 3265f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3266f1705ec1SDavid Ahern 3267f1705ec1SDavid Ahern return 0; 3268f1705ec1SDavid Ahern } 3269f1705ec1SDavid Ahern 3270f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev) 3271f1705ec1SDavid Ahern { 3272f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3273f1705ec1SDavid Ahern struct inet6_dev *idev; 3274f1705ec1SDavid Ahern 3275f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3276f1705ec1SDavid Ahern if (!idev) 3277f1705ec1SDavid Ahern return; 3278f1705ec1SDavid Ahern 3279f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3280f1705ec1SDavid Ahern 3281f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3282f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3283f1705ec1SDavid Ahern fixup_permanent_addr(idev, ifp) < 0) { 3284f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3285f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3286f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3287f1705ec1SDavid Ahern 3288f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3289f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3290f1705ec1SDavid Ahern } 3291f1705ec1SDavid Ahern } 3292f1705ec1SDavid Ahern 3293f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3294f1705ec1SDavid Ahern } 3295f1705ec1SDavid Ahern 32961da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3297351638e7SJiri Pirko void *ptr) 32981da177e4SLinus Torvalds { 3299351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 33004f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3301748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 3302c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3303b217d616SHerbert Xu int err; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds switch (event) { 330645ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 330774235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 330845ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3309a317a2f1SWANG Cong if (IS_ERR(idev)) 3310a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 331145ba9dd2SYOSHIFUJI Hideaki } 331245ba9dd2SYOSHIFUJI Hideaki break; 3313bcdd553fSStephen Hemminger 3314b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3315b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3316b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 3317b7b0b1d2SAlexander Duyck addrconf_ifdown(dev, 1); 3318b7b0b1d2SAlexander Duyck break; 3319b7b0b1d2SAlexander Duyck } 3320b7b0b1d2SAlexander Duyck 3321b7b0b1d2SAlexander Duyck if (idev) { 3322b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3323b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3324b7b0b1d2SAlexander Duyck break; 3325b7b0b1d2SAlexander Duyck } 3326b7b0b1d2SAlexander Duyck 3327b7b0b1d2SAlexander Duyck /* allocate new idev */ 3328b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3329b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3330b7b0b1d2SAlexander Duyck break; 3331b7b0b1d2SAlexander Duyck 3332b7b0b1d2SAlexander Duyck /* device is still not ready */ 3333b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3334b7b0b1d2SAlexander Duyck break; 3335b7b0b1d2SAlexander Duyck 3336b7b0b1d2SAlexander Duyck run_pending = 1; 3337b7b0b1d2SAlexander Duyck 3338b7b0b1d2SAlexander Duyck /* fall through */ 3339b7b0b1d2SAlexander Duyck 33401da177e4SLinus Torvalds case NETDEV_UP: 33413c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3342c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3343c2edacf8SJay Vosburgh break; 3344c2edacf8SJay Vosburgh 33453ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 33463ce62a84SWANG Cong break; 33473ce62a84SWANG Cong 33483c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 334938bd10c4SDavid Ahern /* restore routes for permanent addresses */ 335038bd10c4SDavid Ahern addrconf_permanent_addr(dev); 335138bd10c4SDavid Ahern 3352f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33533c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3354f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 33553c21edbdSYOSHIFUJI Hideaki dev->name); 33563c21edbdSYOSHIFUJI Hideaki break; 33573c21edbdSYOSHIFUJI Hideaki } 335899081049SKristian Slavov 3359d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3360d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3361d31c7b8fSEvgeniy Polyakov 3362a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 336399081049SKristian Slavov idev->if_flags |= IF_READY; 3364e3ec6cfcSBenjamin Thery run_pending = 1; 3365e3ec6cfcSBenjamin Thery } 3366b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 3367f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33683c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 33693c21edbdSYOSHIFUJI Hideaki break; 33703c21edbdSYOSHIFUJI Hideaki } 33713c21edbdSYOSHIFUJI Hideaki 33723c21edbdSYOSHIFUJI Hideaki if (idev) { 3373bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 33743c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 33753c21edbdSYOSHIFUJI Hideaki break; 33763c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 33773c21edbdSYOSHIFUJI Hideaki } 33783c21edbdSYOSHIFUJI Hideaki 3379f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 33803c21edbdSYOSHIFUJI Hideaki dev->name); 33813c21edbdSYOSHIFUJI Hideaki 3382c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 33833c21edbdSYOSHIFUJI Hideaki } 33843c21edbdSYOSHIFUJI Hideaki 33851da177e4SLinus Torvalds switch (dev->type) { 338607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33871da177e4SLinus Torvalds case ARPHRD_SIT: 33881da177e4SLinus Torvalds addrconf_sit_config(dev); 33891da177e4SLinus Torvalds break; 33900be669bbSJoerg Roedel #endif 339107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3392aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3393aee80b54Sstephen hemminger addrconf_gre_config(dev); 3394aee80b54Sstephen hemminger break; 3395aee80b54Sstephen hemminger #endif 33961da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 33971da177e4SLinus Torvalds init_loopback(dev); 33981da177e4SLinus Torvalds break; 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds default: 34011da177e4SLinus Torvalds addrconf_dev_config(dev); 34021da177e4SLinus Torvalds break; 34033ff50b79SStephen Hemminger } 3404bcdd553fSStephen Hemminger 3405a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3406c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3407c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3408c5e33bddSYOSHIFUJI Hideaki 3409bcdd553fSStephen Hemminger /* 3410bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3411bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3412bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 34131da177e4SLinus Torvalds */ 3414bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3415bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 34161da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 34171da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 34181da177e4SLinus Torvalds } 34191da177e4SLinus Torvalds idev->tstamp = jiffies; 34201da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3421bcdd553fSStephen Hemminger 3422bcdd553fSStephen Hemminger /* 3423bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3424bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 34251da177e4SLinus Torvalds */ 34261da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3427bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 34281da177e4SLinus Torvalds } 34291da177e4SLinus Torvalds break; 34301da177e4SLinus Torvalds 34311da177e4SLinus Torvalds case NETDEV_DOWN: 34321da177e4SLinus Torvalds case NETDEV_UNREGISTER: 34331da177e4SLinus Torvalds /* 34341da177e4SLinus Torvalds * Remove all addresses from this interface. 34351da177e4SLinus Torvalds */ 34361da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 34371da177e4SLinus Torvalds break; 34383c21edbdSYOSHIFUJI Hideaki 34391da177e4SLinus Torvalds case NETDEV_CHANGENAME: 34401da177e4SLinus Torvalds if (idev) { 34415632c515SStephen Hemminger snmp6_unregister_dev(idev); 3442408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3443a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3444b217d616SHerbert Xu if (err) 3445b217d616SHerbert Xu return notifier_from_errno(err); 3446a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3447a317a2f1SWANG Cong if (err) { 3448a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3449a317a2f1SWANG Cong return notifier_from_errno(err); 3450a317a2f1SWANG Cong } 34515632c515SStephen Hemminger } 34521da177e4SLinus Torvalds break; 3453bcdd553fSStephen Hemminger 345493d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 345593d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 34563ef0952cSAndrew Lunn if (idev) 345793d9b7d7SJiri Pirko addrconf_type_change(dev, event); 345875c78500SMoni Shoua break; 34594f7f34eaSDavid Ahern 34604f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 34614f7f34eaSDavid Ahern info = ptr; 34624f7f34eaSDavid Ahern 34634f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 34644f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 34654f7f34eaSDavid Ahern */ 34664f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 34674f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 34683ff50b79SStephen Hemminger } 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds return NOTIFY_OK; 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds 34731da177e4SLinus Torvalds /* 34741da177e4SLinus Torvalds * addrconf module should be notified of a device going up 34751da177e4SLinus Torvalds */ 34761da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 34771da177e4SLinus Torvalds .notifier_call = addrconf_notify, 34781da177e4SLinus Torvalds }; 34791da177e4SLinus Torvalds 348093d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 348175c78500SMoni Shoua { 348275c78500SMoni Shoua struct inet6_dev *idev; 348375c78500SMoni Shoua ASSERT_RTNL(); 348475c78500SMoni Shoua 348575c78500SMoni Shoua idev = __in6_dev_get(dev); 348675c78500SMoni Shoua 348793d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 348875c78500SMoni Shoua ipv6_mc_remap(idev); 348993d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 349075c78500SMoni Shoua ipv6_mc_unmap(idev); 349175c78500SMoni Shoua } 349275c78500SMoni Shoua 349370af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 349470af921dSDavid Ahern { 349570af921dSDavid Ahern return ipv6_addr_type(addr) & 349670af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 349770af921dSDavid Ahern } 349870af921dSDavid Ahern 34991da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 35001da177e4SLinus Torvalds { 3501c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3502502a2ffdSstephen hemminger struct inet6_dev *idev; 3503f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 3504f1705ec1SDavid Ahern struct list_head del_list; 3505f1705ec1SDavid Ahern int _keep_addr; 3506f1705ec1SDavid Ahern bool keep_addr; 350773a8bd74SDavid S. Miller int state, i; 35081da177e4SLinus Torvalds 35091da177e4SLinus Torvalds ASSERT_RTNL(); 35101da177e4SLinus Torvalds 3511f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 351273a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 35131da177e4SLinus Torvalds 35141da177e4SLinus Torvalds idev = __in6_dev_get(dev); 351563159f29SIan Morris if (!idev) 35161da177e4SLinus Torvalds return -ENODEV; 35171da177e4SLinus Torvalds 3518bcdd553fSStephen Hemminger /* 3519bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3520bcdd553fSStephen Hemminger * Do not dev_put! 35211da177e4SLinus Torvalds */ 3522439e2385SDenis V. Lunev if (how) { 35231da177e4SLinus Torvalds idev->dead = 1; 35248814c4b5SYOSHIFUJI Hideaki 35258814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3526a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 35271da177e4SLinus Torvalds 35281da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 35291da177e4SLinus Torvalds snmp6_unregister_dev(idev); 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds } 35321da177e4SLinus Torvalds 3533f1705ec1SDavid Ahern /* aggregate the system setting and interface setting */ 3534f1705ec1SDavid Ahern _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3535f1705ec1SDavid Ahern if (!_keep_addr) 3536f1705ec1SDavid Ahern _keep_addr = idev->cnf.keep_addr_on_down; 3537f1705ec1SDavid Ahern 3538f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3539f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3540f1705ec1SDavid Ahern */ 3541bc561632SMike Manning keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6); 3542f1705ec1SDavid Ahern 354373a8bd74SDavid S. Miller /* Step 2: clear hash table */ 354473a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 354573a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 354673a8bd74SDavid S. Miller 354773a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 354873a8bd74SDavid S. Miller restart: 3549b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 355073a8bd74SDavid S. Miller if (ifa->idev == idev) { 3551c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3552f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3553f1705ec1SDavid Ahern * address is retained on a down event 3554f1705ec1SDavid Ahern */ 3555f1705ec1SDavid Ahern if (!keep_addr || 355670af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 355770af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3558f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 355973a8bd74SDavid S. Miller goto restart; 356073a8bd74SDavid S. Miller } 356173a8bd74SDavid S. Miller } 3562f1705ec1SDavid Ahern } 356373a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 356473a8bd74SDavid S. Miller } 356573a8bd74SDavid S. Miller 35661da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35671da177e4SLinus Torvalds 3568b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3569b7b1bfceSHannes Frederic Sowa 3570bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3571439e2385SDenis V. Lunev if (!how) 35723c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 35731da177e4SLinus Torvalds 3574439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 35751da177e4SLinus Torvalds in6_dev_put(idev); 35761da177e4SLinus Torvalds 3577bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3578372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3579372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3580372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3581372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 35821da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 35831da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds if (ifa->ifpub) { 35861da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 35871da177e4SLinus Torvalds ifa->ifpub = NULL; 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 35901da177e4SLinus Torvalds in6_ifa_put(ifa); 35911da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35921da177e4SLinus Torvalds } 35938f37ada5Sstephen hemminger 3594f1705ec1SDavid Ahern /* re-combine the user config with event to determine if permanent 3595f1705ec1SDavid Ahern * addresses are to be removed from the interface list 3596f1705ec1SDavid Ahern */ 3597bc561632SMike Manning keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6); 3598f1705ec1SDavid Ahern 3599f1705ec1SDavid Ahern INIT_LIST_HEAD(&del_list); 3600f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 360138bd10c4SDavid Ahern struct rt6_info *rt = NULL; 360238bd10c4SDavid Ahern 3603c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 360484e8b803Sstephen hemminger 36054c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 36068e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3607f1705ec1SDavid Ahern 360870af921dSDavid Ahern if (keep_addr && (ifa->flags & IFA_F_PERMANENT) && 360970af921dSDavid Ahern !addr_is_local(&ifa->addr)) { 3610f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3611f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 3612f1705ec1SDavid Ahern ifa->state = 0; 3613f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3614f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 361538bd10c4SDavid Ahern 361638bd10c4SDavid Ahern rt = ifa->rt; 361738bd10c4SDavid Ahern ifa->rt = NULL; 3618f1705ec1SDavid Ahern } else { 36194c5ff6a6SHerbert Xu state = ifa->state; 36204c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3621f1705ec1SDavid Ahern 3622c882219aSWei Yongjun list_move(&ifa->if_list, &del_list); 3623f1705ec1SDavid Ahern } 3624f1705ec1SDavid Ahern 36258e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 36264c5ff6a6SHerbert Xu 362738bd10c4SDavid Ahern if (rt) 362838bd10c4SDavid Ahern ip6_del_rt(rt); 362938bd10c4SDavid Ahern 36309d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 36311da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3632f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3633ea06f717SMike Manning } else { 3634ea06f717SMike Manning if (idev->cnf.forwarding) 3635ea06f717SMike Manning addrconf_leave_anycast(ifa); 3636ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 36372de79570SLorenzo Colitti } 363873a8bd74SDavid S. Miller 36391da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36401da177e4SLinus Torvalds } 36418f37ada5Sstephen hemminger 36421da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 36431da177e4SLinus Torvalds 3644f1705ec1SDavid Ahern /* now clean up addresses to be removed */ 3645f1705ec1SDavid Ahern while (!list_empty(&del_list)) { 3646f1705ec1SDavid Ahern ifa = list_first_entry(&del_list, 3647f1705ec1SDavid Ahern struct inet6_ifaddr, if_list); 3648f1705ec1SDavid Ahern list_del(&ifa->if_list); 3649f1705ec1SDavid Ahern 3650f1705ec1SDavid Ahern in6_ifa_put(ifa); 3651f1705ec1SDavid Ahern } 3652f1705ec1SDavid Ahern 3653381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3654381f4dcaSSabrina Dubroca if (how) { 3655381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 36561da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3657381f4dcaSSabrina Dubroca } else { 36581da177e4SLinus Torvalds ipv6_mc_down(idev); 3659381f4dcaSSabrina Dubroca } 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds idev->tstamp = jiffies; 36621da177e4SLinus Torvalds 3663bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3664439e2385SDenis V. Lunev if (how) { 3665408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 36661da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 36671da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 36681da177e4SLinus Torvalds in6_dev_put(idev); 36691da177e4SLinus Torvalds } 36701da177e4SLinus Torvalds return 0; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 36741da177e4SLinus Torvalds { 3675b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3676caf92bc4SCong Wang struct net_device *dev = idev->dev; 3677b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 36781da177e4SLinus Torvalds 3679b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 36805b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 36811da177e4SLinus Torvalds goto out; 36821da177e4SLinus Torvalds 36839ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 36841da177e4SLinus Torvalds goto out; 36855b2a1953Sstephen hemminger 36865b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 36875b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 36885b2a1953Sstephen hemminger goto out; 36891da177e4SLinus Torvalds 3690b7b1bfceSHannes Frederic Sowa if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3691caf92bc4SCong Wang write_unlock(&idev->lock); 3692caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3693caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3694b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3695b7b1bfceSHannes Frederic Sowa else 3696caf92bc4SCong Wang goto put; 3697b7b1bfceSHannes Frederic Sowa 3698caf92bc4SCong Wang write_lock(&idev->lock); 36991da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3700b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3701b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 37025b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 37035b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 37041da177e4SLinus Torvalds } else { 37051da177e4SLinus Torvalds /* 37061da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 37071da177e4SLinus Torvalds * assumption any longer. 37081da177e4SLinus Torvalds */ 370991df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds out: 3713b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3714caf92bc4SCong Wang put: 3715b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 37161da177e4SLinus Torvalds } 37171da177e4SLinus Torvalds 37181da177e4SLinus Torvalds /* 37191da177e4SLinus Torvalds * Duplicate Address Detection 37201da177e4SLinus Torvalds */ 37213c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 37223c21edbdSYOSHIFUJI Hideaki { 37233c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 37243c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 37253c21edbdSYOSHIFUJI Hideaki 372695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 372795c385b4SNeil Horman rand_num = 0; 372895c385b4SNeil Horman else 372963862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 373095c385b4SNeil Horman 3731b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3732c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 37333c21edbdSYOSHIFUJI Hideaki } 37343c21edbdSYOSHIFUJI Hideaki 3735c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 37361da177e4SLinus Torvalds { 37371da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 37381da177e4SLinus Torvalds struct net_device *dev = idev->dev; 373916186a82Ssubashab@codeaurora.org bool notify = false; 37401da177e4SLinus Torvalds 37411da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 37421da177e4SLinus Torvalds 374363862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 37441da177e4SLinus Torvalds 37451da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3746622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3747e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 37481da177e4SLinus Torvalds goto out; 37491da177e4SLinus Torvalds 37501da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 37511b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 375255ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 375355ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3754cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 375521809fafSstephen hemminger spin_unlock(&ifp->lock); 37561da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds addrconf_dad_completed(ifp); 37591da177e4SLinus Torvalds return; 37601da177e4SLinus Torvalds } 37611da177e4SLinus Torvalds 37626732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 376321809fafSstephen hemminger spin_unlock(&ifp->lock); 37646732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 37653c21edbdSYOSHIFUJI Hideaki /* 3766590a9887SMasatake YAMATO * If the device is not ready: 37673c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 37683c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 37693c21edbdSYOSHIFUJI Hideaki */ 37703c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3771cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 37726732badeSYOSHIFUJI Hideaki return; 37733c21edbdSYOSHIFUJI Hideaki } 377495c385b4SNeil Horman 377595c385b4SNeil Horman /* 377695c385b4SNeil Horman * Optimistic nodes can start receiving 377795c385b4SNeil Horman * Frames right away 377895c385b4SNeil Horman */ 37797fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 378095c385b4SNeil Horman ip6_ins_rt(ifp->rt); 37817fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 37827fd2561eSErik Kline /* Because optimistic nodes can use this address, 37837fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 37847fd2561eSErik Kline */ 378516186a82Ssubashab@codeaurora.org notify = true; 37867fd2561eSErik Kline } 37877fd2561eSErik Kline } 378895c385b4SNeil Horman 37896732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 37901da177e4SLinus Torvalds out: 3791622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 37921da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 379316186a82Ssubashab@codeaurora.org if (notify) 379416186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 37951da177e4SLinus Torvalds } 37961da177e4SLinus Torvalds 3797c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 37981da177e4SLinus Torvalds { 3799c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3800c15b1ccaSHannes Frederic Sowa 38018e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3802c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3803c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3804c15b1ccaSHannes Frederic Sowa begin_dad = true; 3805c15b1ccaSHannes Frederic Sowa } 38068e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3807c15b1ccaSHannes Frederic Sowa 3808c15b1ccaSHannes Frederic Sowa if (begin_dad) 3809c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3810c15b1ccaSHannes Frederic Sowa } 3811c15b1ccaSHannes Frederic Sowa 3812c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3813c15b1ccaSHannes Frederic Sowa { 3814c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3815c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3816c15b1ccaSHannes Frederic Sowa dad_work); 38171da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38181da177e4SLinus Torvalds struct in6_addr mcaddr; 381985b51b12SMike Manning bool disable_ipv6 = false; 38201da177e4SLinus Torvalds 3821c15b1ccaSHannes Frederic Sowa enum { 3822c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3823c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3824c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3825c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3826c15b1ccaSHannes Frederic Sowa 3827c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3828c15b1ccaSHannes Frederic Sowa 38298e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3830c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3831c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3832c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3833c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3834c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3835c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 383685b51b12SMike Manning 383785b51b12SMike Manning if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 && 383885b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 383985b51b12SMike Manning struct in6_addr addr; 384085b51b12SMike Manning 384185b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 384285b51b12SMike Manning addr.s6_addr32[1] = 0; 384385b51b12SMike Manning 384485b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 384585b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 384685b51b12SMike Manning /* DAD failed for link-local based on MAC */ 384785b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 384885b51b12SMike Manning 384985b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 385085b51b12SMike Manning ifp->idev->dev->name); 385185b51b12SMike Manning disable_ipv6 = true; 385285b51b12SMike Manning } 385385b51b12SMike Manning } 3854c15b1ccaSHannes Frederic Sowa } 38558e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3856c15b1ccaSHannes Frederic Sowa 3857c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3858c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3859c15b1ccaSHannes Frederic Sowa goto out; 3860c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3861751eb6b6SWei Yongjun in6_ifa_hold(ifp); 3862c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 386385b51b12SMike Manning if (disable_ipv6) 386485b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 3865c15b1ccaSHannes Frederic Sowa goto out; 3866c15b1ccaSHannes Frederic Sowa } 3867c15b1ccaSHannes Frederic Sowa 3868b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3869f2344a13SHerbert Xu goto out; 3870f2344a13SHerbert Xu 3871c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3872122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3873c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38741da177e4SLinus Torvalds goto out; 38751da177e4SLinus Torvalds } 387621809fafSstephen hemminger 387721809fafSstephen hemminger spin_lock(&ifp->lock); 3878622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3879622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3880c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3881622ccdf1SHerbert Xu goto out; 3882622ccdf1SHerbert Xu } 3883622ccdf1SHerbert Xu 3884b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 38851da177e4SLinus Torvalds /* 38861da177e4SLinus Torvalds * DAD was successful 38871da177e4SLinus Torvalds */ 38881da177e4SLinus Torvalds 3889cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 389021809fafSstephen hemminger spin_unlock(&ifp->lock); 3891c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds addrconf_dad_completed(ifp); 38941da177e4SLinus Torvalds 38951da177e4SLinus Torvalds goto out; 38961da177e4SLinus Torvalds } 38971da177e4SLinus Torvalds 3898b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3899c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 39001f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 390121809fafSstephen hemminger spin_unlock(&ifp->lock); 3902c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39031da177e4SLinus Torvalds 39041da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 39051da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3906304d888bSNicolas Dichtel ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any); 39071da177e4SLinus Torvalds out: 39081da177e4SLinus Torvalds in6_ifa_put(ifp); 3909c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 39101da177e4SLinus Torvalds } 39111da177e4SLinus Torvalds 391211ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 391311ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 391411ffff75SHannes Frederic Sowa { 391511ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 391611ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 391711ffff75SHannes Frederic Sowa 3918602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3919602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 3920602582caSHannes Frederic Sowa break; 392111ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 392211ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 392311ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 392411ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 392511ffff75SHannes Frederic Sowa return false; 392611ffff75SHannes Frederic Sowa } 392711ffff75SHannes Frederic Sowa return true; 392811ffff75SHannes Frederic Sowa } 392911ffff75SHannes Frederic Sowa 39301da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 39311da177e4SLinus Torvalds { 39321da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3933b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3934b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3935b7b1bfceSHannes Frederic Sowa 3936c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 39371da177e4SLinus Torvalds 39381da177e4SLinus Torvalds /* 39391da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 39401da177e4SLinus Torvalds */ 39411da177e4SLinus Torvalds 39421da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 39431da177e4SLinus Torvalds 3944026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3945026359bcSTore Anderson router advertisements, start sending router solicitations. 39461da177e4SLinus Torvalds */ 39471da177e4SLinus Torvalds 39481ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 394911ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3950b173ee48SHannes Frederic Sowa send_rs = send_mld && 3951b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 3952b173ee48SHannes Frederic Sowa ifp->idev->cnf.rtr_solicits > 0 && 3953b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 39541ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 39551ec047ebSHannes Frederic Sowa 3956b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 3957b173ee48SHannes Frederic Sowa * Resend with proper ll now. 3958b173ee48SHannes Frederic Sowa */ 3959b173ee48SHannes Frederic Sowa if (send_mld) 3960b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 3961b173ee48SHannes Frederic Sowa 39621ec047ebSHannes Frederic Sowa if (send_rs) { 39631da177e4SLinus Torvalds /* 39641da177e4SLinus Torvalds * If a host as already performed a random delay 39651da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 39661da177e4SLinus Torvalds * to delay again before sending the first RS 39671da177e4SLinus Torvalds */ 39681ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3969b7b1bfceSHannes Frederic Sowa return; 39701ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 39711da177e4SLinus Torvalds 3972b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3973b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3974b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 39751da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3976b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3977b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3978b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3979b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 39801da177e4SLinus Torvalds } 39811da177e4SLinus Torvalds } 39821da177e4SLinus Torvalds 3983e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3984e21e8467SStephen Hemminger { 3985c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3986c5e33bddSYOSHIFUJI Hideaki 3987c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3988502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 398921809fafSstephen hemminger spin_lock(&ifp->lock); 3990f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3991f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3992c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3993f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3994c5e33bddSYOSHIFUJI Hideaki } 3995c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3996c5e33bddSYOSHIFUJI Hideaki } 3997c5e33bddSYOSHIFUJI Hideaki 39981da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 39991da177e4SLinus Torvalds struct if6_iter_state { 40003c40090aSDaniel Lezcano struct seq_net_private p; 40011da177e4SLinus Torvalds int bucket; 40021d578303SMihai Maruseac int offset; 40031da177e4SLinus Torvalds }; 40041da177e4SLinus Torvalds 40051d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 40061da177e4SLinus Torvalds { 40071da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 40081da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 40091218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 40101d578303SMihai Maruseac int p = 0; 40111da177e4SLinus Torvalds 40121d578303SMihai Maruseac /* initial bucket if pos is 0 */ 40131d578303SMihai Maruseac if (pos == 0) { 40141d578303SMihai Maruseac state->bucket = 0; 40151d578303SMihai Maruseac state->offset = 0; 40161d578303SMihai Maruseac } 40171d578303SMihai Maruseac 40181d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4019b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 40201d578303SMihai Maruseac addr_lst) { 40219f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40229f0d3c27SEric Dumazet continue; 40231d578303SMihai Maruseac /* sync with offset */ 40241d578303SMihai Maruseac if (p < state->offset) { 40251d578303SMihai Maruseac p++; 40261d578303SMihai Maruseac continue; 40271d578303SMihai Maruseac } 40281d578303SMihai Maruseac state->offset++; 40291da177e4SLinus Torvalds return ifa; 40301da177e4SLinus Torvalds } 40311d578303SMihai Maruseac 40321d578303SMihai Maruseac /* prepare for next bucket */ 40331d578303SMihai Maruseac state->offset = 0; 40341d578303SMihai Maruseac p = 0; 40351d578303SMihai Maruseac } 4036c2e21293Sstephen hemminger return NULL; 4037c2e21293Sstephen hemminger } 40381da177e4SLinus Torvalds 4039c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4040c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 40411da177e4SLinus Torvalds { 40421da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 40431218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 40441da177e4SLinus Torvalds 4045b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 40469f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40479f0d3c27SEric Dumazet continue; 40481d578303SMihai Maruseac state->offset++; 40491da177e4SLinus Torvalds return ifa; 40501d578303SMihai Maruseac } 40511da177e4SLinus Torvalds 4052c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 40531d578303SMihai Maruseac state->offset = 0; 4054b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 4055c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 40569f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40579f0d3c27SEric Dumazet continue; 40581d578303SMihai Maruseac state->offset++; 4059c2e21293Sstephen hemminger return ifa; 4060c2e21293Sstephen hemminger } 4061c2e21293Sstephen hemminger } 4062c2e21293Sstephen hemminger 4063c2e21293Sstephen hemminger return NULL; 4064c2e21293Sstephen hemminger } 4065c2e21293Sstephen hemminger 40661da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 40674f70eccaSEric Dumazet __acquires(rcu_bh) 40681da177e4SLinus Torvalds { 40695c578aedSstephen hemminger rcu_read_lock_bh(); 40701d578303SMihai Maruseac return if6_get_first(seq, *pos); 40711da177e4SLinus Torvalds } 40721da177e4SLinus Torvalds 40731da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 40741da177e4SLinus Torvalds { 40751da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 40781da177e4SLinus Torvalds ++*pos; 40791da177e4SLinus Torvalds return ifa; 40801da177e4SLinus Torvalds } 40811da177e4SLinus Torvalds 40821da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 40834f70eccaSEric Dumazet __releases(rcu_bh) 40841da177e4SLinus Torvalds { 40855c578aedSstephen hemminger rcu_read_unlock_bh(); 40861da177e4SLinus Torvalds } 40871da177e4SLinus Torvalds 40881da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 40891da177e4SLinus Torvalds { 40901da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4091971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4092b071195dSHarvey Harrison &ifp->addr, 40931da177e4SLinus Torvalds ifp->idev->dev->ifindex, 40941da177e4SLinus Torvalds ifp->prefix_len, 40951da177e4SLinus Torvalds ifp->scope, 4096971a351cSJiri Pirko (u8) ifp->flags, 40971da177e4SLinus Torvalds ifp->idev->dev->name); 40981da177e4SLinus Torvalds return 0; 40991da177e4SLinus Torvalds } 41001da177e4SLinus Torvalds 410156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 41021da177e4SLinus Torvalds .start = if6_seq_start, 41031da177e4SLinus Torvalds .next = if6_seq_next, 41041da177e4SLinus Torvalds .show = if6_seq_show, 41051da177e4SLinus Torvalds .stop = if6_seq_stop, 41061da177e4SLinus Torvalds }; 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 41091da177e4SLinus Torvalds { 41103c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4111cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 41121da177e4SLinus Torvalds } 41131da177e4SLinus Torvalds 41149a32144eSArjan van de Ven static const struct file_operations if6_fops = { 41151da177e4SLinus Torvalds .owner = THIS_MODULE, 41161da177e4SLinus Torvalds .open = if6_seq_open, 41171da177e4SLinus Torvalds .read = seq_read, 41181da177e4SLinus Torvalds .llseek = seq_lseek, 41193c40090aSDaniel Lezcano .release = seq_release_net, 41201da177e4SLinus Torvalds }; 41211da177e4SLinus Torvalds 41222c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 41231da177e4SLinus Torvalds { 4124d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 41251da177e4SLinus Torvalds return -ENOMEM; 41261da177e4SLinus Torvalds return 0; 41271da177e4SLinus Torvalds } 41281da177e4SLinus Torvalds 41292c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 41303c40090aSDaniel Lezcano { 4131ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 41323c40090aSDaniel Lezcano } 41333c40090aSDaniel Lezcano 41343c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 41353c40090aSDaniel Lezcano .init = if6_proc_net_init, 41363c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 41373c40090aSDaniel Lezcano }; 41383c40090aSDaniel Lezcano 41393c40090aSDaniel Lezcano int __init if6_proc_init(void) 41403c40090aSDaniel Lezcano { 41413c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 41423c40090aSDaniel Lezcano } 41433c40090aSDaniel Lezcano 41441da177e4SLinus Torvalds void if6_proc_exit(void) 41451da177e4SLinus Torvalds { 41463c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 41471da177e4SLinus Torvalds } 41481da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 41491da177e4SLinus Torvalds 415007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 41513b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4152b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 41533b9f9a1cSNoriaki TAKAMIYA { 41543b9f9a1cSNoriaki TAKAMIYA int ret = 0; 4155c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 4156ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 4157c2e21293Sstephen hemminger 41585c578aedSstephen hemminger rcu_read_lock_bh(); 4159b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 4160878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4161389f6612SDaniel Lezcano continue; 4162caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 41633b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 41643b9f9a1cSNoriaki TAKAMIYA ret = 1; 41653b9f9a1cSNoriaki TAKAMIYA break; 41663b9f9a1cSNoriaki TAKAMIYA } 41673b9f9a1cSNoriaki TAKAMIYA } 41685c578aedSstephen hemminger rcu_read_unlock_bh(); 41693b9f9a1cSNoriaki TAKAMIYA return ret; 41703b9f9a1cSNoriaki TAKAMIYA } 41713b9f9a1cSNoriaki TAKAMIYA #endif 41723b9f9a1cSNoriaki TAKAMIYA 41731da177e4SLinus Torvalds /* 41741da177e4SLinus Torvalds * Periodic address status verification 41751da177e4SLinus Torvalds */ 41761da177e4SLinus Torvalds 4177c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 41781da177e4SLinus Torvalds { 4179b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 41801da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 41811da177e4SLinus Torvalds int i; 41821da177e4SLinus Torvalds 4183c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4184c15b1ccaSHannes Frederic Sowa 41855c578aedSstephen hemminger rcu_read_lock_bh(); 41861da177e4SLinus Torvalds now = jiffies; 4187b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 41881da177e4SLinus Torvalds 4189c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 41901da177e4SLinus Torvalds 41911da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 41921da177e4SLinus Torvalds restart: 4193c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 41941da177e4SLinus Torvalds unsigned long age; 41951da177e4SLinus Torvalds 4196fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4197fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4198fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4199fad8da3eSYasushi Asano */ 4200fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4201fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 42021da177e4SLinus Torvalds continue; 42031da177e4SLinus Torvalds 42041da177e4SLinus Torvalds spin_lock(&ifp->lock); 4205b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4206b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 42071da177e4SLinus Torvalds 42088f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 42098f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 42101da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42111da177e4SLinus Torvalds in6_ifa_hold(ifp); 42121da177e4SLinus Torvalds ipv6_del_addr(ifp); 42131da177e4SLinus Torvalds goto restart; 42148f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 42158f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 42168f27ebb9SYOSHIFUJI Hideaki continue; 42171da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4218a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 42191da177e4SLinus Torvalds int deprecate = 0; 42201da177e4SLinus Torvalds 42211da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 42221da177e4SLinus Torvalds deprecate = 1; 42231da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 42241da177e4SLinus Torvalds } 42251da177e4SLinus Torvalds 4226fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4227fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 42281da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 42291da177e4SLinus Torvalds 42301da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42311da177e4SLinus Torvalds 42321da177e4SLinus Torvalds if (deprecate) { 42331da177e4SLinus Torvalds in6_ifa_hold(ifp); 42341da177e4SLinus Torvalds 42351da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 42361da177e4SLinus Torvalds in6_ifa_put(ifp); 42371da177e4SLinus Torvalds goto restart; 42381da177e4SLinus Torvalds } 42391da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 42401da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 424188949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 424288949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 42431f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 424488949cf4Sstephen hemminger 42451da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 42461da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 42471da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 42481da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 42491da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 42501da177e4SLinus Torvalds ifp->regen_count++; 42511da177e4SLinus Torvalds in6_ifa_hold(ifp); 42521da177e4SLinus Torvalds in6_ifa_hold(ifpub); 42531da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42545c578aedSstephen hemminger 4255291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4256291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4257291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 42581da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 42591da177e4SLinus Torvalds in6_ifa_put(ifpub); 42601da177e4SLinus Torvalds in6_ifa_put(ifp); 42611da177e4SLinus Torvalds goto restart; 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 42641da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 42651da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42661da177e4SLinus Torvalds } else { 42671da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 42681da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 42691da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 42701da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42711da177e4SLinus Torvalds } 42721da177e4SLinus Torvalds } 42731da177e4SLinus Torvalds } 42741da177e4SLinus Torvalds 4275b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4276b2db7564SYOSHIFUJI Hideaki next_sched = next; 4277b2db7564SYOSHIFUJI Hideaki 4278b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4279b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4280b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4281b2db7564SYOSHIFUJI Hideaki 4282b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4283b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4284b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4285b2db7564SYOSHIFUJI Hideaki 4286ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4287ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4288c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 42895c578aedSstephen hemminger rcu_read_unlock_bh(); 42901da177e4SLinus Torvalds } 42911da177e4SLinus Torvalds 4292c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4293c15b1ccaSHannes Frederic Sowa { 4294c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4295c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4296c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4297c15b1ccaSHannes Frederic Sowa } 4298c15b1ccaSHannes Frederic Sowa 4299c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4300c15b1ccaSHannes Frederic Sowa { 4301c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4302c15b1ccaSHannes Frederic Sowa } 4303c15b1ccaSHannes Frederic Sowa 4304caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4305caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4306461d8837SThomas Graf { 4307461d8837SThomas Graf struct in6_addr *pfx = NULL; 4308461d8837SThomas Graf 4309caeaba79SNicolas Dichtel *peer_pfx = NULL; 4310caeaba79SNicolas Dichtel 4311461d8837SThomas Graf if (addr) 4312461d8837SThomas Graf pfx = nla_data(addr); 4313461d8837SThomas Graf 4314461d8837SThomas Graf if (local) { 4315461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4316caeaba79SNicolas Dichtel *peer_pfx = pfx; 4317461d8837SThomas Graf pfx = nla_data(local); 4318461d8837SThomas Graf } 4319461d8837SThomas Graf 4320461d8837SThomas Graf return pfx; 4321461d8837SThomas Graf } 4322461d8837SThomas Graf 4323ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4324461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4325461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4326461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4327479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4328461d8837SThomas Graf }; 4329461d8837SThomas Graf 43301da177e4SLinus Torvalds static int 4331661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 43321da177e4SLinus Torvalds { 43333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4334b933f716SThomas Graf struct ifaddrmsg *ifm; 4335b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4336caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 43376046d5b4SHeiner Kallweit u32 ifa_flags; 4338b933f716SThomas Graf int err; 43391da177e4SLinus Torvalds 4340b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4341b933f716SThomas Graf if (err < 0) 4342b933f716SThomas Graf return err; 4343b933f716SThomas Graf 4344b933f716SThomas Graf ifm = nlmsg_data(nlh); 4345caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 434663159f29SIan Morris if (!pfx) 43471da177e4SLinus Torvalds return -EINVAL; 43481da177e4SLinus Torvalds 43496046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 43506046d5b4SHeiner Kallweit 43516046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 43526046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 43536046d5b4SHeiner Kallweit 43546046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 43556046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 43561da177e4SLinus Torvalds } 43571da177e4SLinus Torvalds 4358479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 435955ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4360081bba5bSNoriaki TAKAMIYA { 43616f704992SYOSHIFUJI Hideaki u32 flags; 43626f704992SYOSHIFUJI Hideaki clock_t expires; 43634bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 436453bd6749SJiri Pirko bool was_managetempaddr; 43655b84efecSThomas Haller bool had_prefixroute; 436646d48046SYOSHIFUJI Hideaki 4367c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4368c15b1ccaSHannes Frederic Sowa 4369081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4370081bba5bSNoriaki TAKAMIYA return -EINVAL; 4371081bba5bSNoriaki TAKAMIYA 437253bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 437353bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 437453bd6749SJiri Pirko return -EINVAL; 437553bd6749SJiri Pirko 43764bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 43774bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43784bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 43794bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 43806f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 43814bed72e4SYOSHIFUJI Hideaki } else { 43824bed72e4SYOSHIFUJI Hideaki expires = 0; 43834bed72e4SYOSHIFUJI Hideaki flags = 0; 43844bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 43856f704992SYOSHIFUJI Hideaki } 4386081bba5bSNoriaki TAKAMIYA 43874bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 43884bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43894bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 439055ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 43914bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 43924bed72e4SYOSHIFUJI Hideaki } 4393081bba5bSNoriaki TAKAMIYA 4394081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 439553bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 43965b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 43975b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 439853bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4399761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4400761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 440153bd6749SJiri Pirko ifp->flags |= ifa_flags; 4402081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4403081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4404081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4405081bba5bSNoriaki TAKAMIYA 4406081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4407081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4408081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4409081bba5bSNoriaki TAKAMIYA 4410761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 441146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 44126f704992SYOSHIFUJI Hideaki expires, flags); 44135b84efecSThomas Haller } else if (had_prefixroute) { 44145b84efecSThomas Haller enum cleanup_prefix_rt_t action; 44155b84efecSThomas Haller unsigned long rt_expires; 44165b84efecSThomas Haller 44175b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 44185b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 44195b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 44205b84efecSThomas Haller 44215b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 44225b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 44235b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 44245b84efecSThomas Haller } 4425761aac73SThomas Haller } 442653bd6749SJiri Pirko 442753bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 442853bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 442953bd6749SJiri Pirko valid_lft = prefered_lft = 0; 443053bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 443153bd6749SJiri Pirko !was_managetempaddr, jiffies); 443253bd6749SJiri Pirko } 443353bd6749SJiri Pirko 4434c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4435081bba5bSNoriaki TAKAMIYA 4436081bba5bSNoriaki TAKAMIYA return 0; 4437081bba5bSNoriaki TAKAMIYA } 4438081bba5bSNoriaki TAKAMIYA 4439081bba5bSNoriaki TAKAMIYA static int 4440661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 44411da177e4SLinus Torvalds { 44423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4443461d8837SThomas Graf struct ifaddrmsg *ifm; 4444461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4445caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 44467198f8ceSThomas Graf struct inet6_ifaddr *ifa; 44477198f8ceSThomas Graf struct net_device *dev; 444855ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4449479840ffSJiri Pirko u32 ifa_flags; 4450461d8837SThomas Graf int err; 44511da177e4SLinus Torvalds 4452461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4453461d8837SThomas Graf if (err < 0) 4454461d8837SThomas Graf return err; 4455461d8837SThomas Graf 4456461d8837SThomas Graf ifm = nlmsg_data(nlh); 4457caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 445863159f29SIan Morris if (!pfx) 44591da177e4SLinus Torvalds return -EINVAL; 44601da177e4SLinus Torvalds 4461461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 44620778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4463461d8837SThomas Graf 4464461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 44650778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4466461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4467461d8837SThomas Graf } else { 4468461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4469461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 44700778769dSNoriaki TAKAMIYA } 44710778769dSNoriaki TAKAMIYA 4472af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 447363159f29SIan Morris if (!dev) 44747198f8ceSThomas Graf return -ENODEV; 4475081bba5bSNoriaki TAKAMIYA 4476479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4477479840ffSJiri Pirko 447855ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4479761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 448093a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 448155ebaef1SNoriaki TAKAMIYA 44821cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 448363159f29SIan Morris if (!ifa) { 44847198f8ceSThomas Graf /* 44857198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4486db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 44877198f8ceSThomas Graf */ 4488caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4489af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4490af284937SDaniel Lezcano preferred_lft, valid_lft); 44911da177e4SLinus Torvalds } 44921da177e4SLinus Torvalds 44937198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 44947198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 44957198f8ceSThomas Graf err = -EEXIST; 44967198f8ceSThomas Graf else 449755ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 44987198f8ceSThomas Graf 44997198f8ceSThomas Graf in6_ifa_put(ifa); 45007198f8ceSThomas Graf 45017198f8ceSThomas Graf return err; 45027198f8ceSThomas Graf } 45037198f8ceSThomas Graf 4504479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4505101bb229SThomas Graf u8 scope, int ifindex) 4506101bb229SThomas Graf { 4507101bb229SThomas Graf struct ifaddrmsg *ifm; 4508101bb229SThomas Graf 4509101bb229SThomas Graf ifm = nlmsg_data(nlh); 4510101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4511101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4512101bb229SThomas Graf ifm->ifa_flags = flags; 4513101bb229SThomas Graf ifm->ifa_scope = scope; 4514101bb229SThomas Graf ifm->ifa_index = ifindex; 4515101bb229SThomas Graf } 4516101bb229SThomas Graf 451785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 451885486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 451985486af0SThomas Graf { 452085486af0SThomas Graf struct ifa_cacheinfo ci; 452185486af0SThomas Graf 452218a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 452318a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 452485486af0SThomas Graf ci.ifa_prefered = preferred; 452585486af0SThomas Graf ci.ifa_valid = valid; 452685486af0SThomas Graf 452785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 452885486af0SThomas Graf } 452985486af0SThomas Graf 4530101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4531101bb229SThomas Graf { 4532101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4533101bb229SThomas Graf return RT_SCOPE_HOST; 4534101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4535101bb229SThomas Graf return RT_SCOPE_LINK; 4536101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4537101bb229SThomas Graf return RT_SCOPE_SITE; 4538101bb229SThomas Graf else 4539101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4540101bb229SThomas Graf } 4541101bb229SThomas Graf 45420ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 45430ab6803bSThomas Graf { 4544339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4545caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4546339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4547479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4548479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 45490ab6803bSThomas Graf } 4550c5396a31SYOSHIFUJI Hideaki 45511da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 455215e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 45531da177e4SLinus Torvalds { 45541da177e4SLinus Torvalds struct nlmsghdr *nlh; 455585486af0SThomas Graf u32 preferred, valid; 45561da177e4SLinus Torvalds 455715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 455863159f29SIan Morris if (!nlh) 455926932566SPatrick McHardy return -EMSGSIZE; 45600ab6803bSThomas Graf 4561101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4562101bb229SThomas Graf ifa->idev->dev->ifindex); 4563101bb229SThomas Graf 4564fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4565fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 456685486af0SThomas Graf preferred = ifa->prefered_lft; 456785486af0SThomas Graf valid = ifa->valid_lft; 456885486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 45691da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4570a1faa698SJens Rosenboom if (preferred > tval) 457185486af0SThomas Graf preferred -= tval; 4572a1faa698SJens Rosenboom else 4573a1faa698SJens Rosenboom preferred = 0; 4574f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4575f56619fcSBen Hutchings if (valid > tval) 457685486af0SThomas Graf valid -= tval; 4577f56619fcSBen Hutchings else 4578f56619fcSBen Hutchings valid = 0; 4579f56619fcSBen Hutchings } 45801da177e4SLinus Torvalds } 45811da177e4SLinus Torvalds } else { 458285486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 458385486af0SThomas Graf valid = INFINITY_LIFE_TIME; 45841da177e4SLinus Torvalds } 458585486af0SThomas Graf 45867996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4587930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4588930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4589caeaba79SNicolas Dichtel goto error; 4590caeaba79SNicolas Dichtel } else 4591930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4592caeaba79SNicolas Dichtel goto error; 4593caeaba79SNicolas Dichtel 4594caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4595caeaba79SNicolas Dichtel goto error; 459685486af0SThomas Graf 4597479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4598479840ffSJiri Pirko goto error; 4599479840ffSJiri Pirko 4600053c095aSJohannes Berg nlmsg_end(skb, nlh); 4601053c095aSJohannes Berg return 0; 4602caeaba79SNicolas Dichtel 4603caeaba79SNicolas Dichtel error: 4604caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4605caeaba79SNicolas Dichtel return -EMSGSIZE; 46061da177e4SLinus Torvalds } 46071da177e4SLinus Torvalds 46081da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 460915e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 46101da177e4SLinus Torvalds { 46111da177e4SLinus Torvalds struct nlmsghdr *nlh; 4612101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4613101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 46141da177e4SLinus Torvalds 46151da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4616101bb229SThomas Graf scope = RT_SCOPE_SITE; 4617101bb229SThomas Graf 461815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 461963159f29SIan Morris if (!nlh) 462026932566SPatrick McHardy return -EMSGSIZE; 46210ab6803bSThomas Graf 4622101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4623930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 46240ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 462526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 462626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 462726932566SPatrick McHardy return -EMSGSIZE; 462826932566SPatrick McHardy } 462985486af0SThomas Graf 4630053c095aSJohannes Berg nlmsg_end(skb, nlh); 4631053c095aSJohannes Berg return 0; 46321da177e4SLinus Torvalds } 46331da177e4SLinus Torvalds 46341da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 463515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 46361da177e4SLinus Torvalds { 46371da177e4SLinus Torvalds struct nlmsghdr *nlh; 4638101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4639101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 46401da177e4SLinus Torvalds 46411da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4642101bb229SThomas Graf scope = RT_SCOPE_SITE; 4643101bb229SThomas Graf 464415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 464563159f29SIan Morris if (!nlh) 464626932566SPatrick McHardy return -EMSGSIZE; 46470ab6803bSThomas Graf 4648101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4649930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 46500ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 465126932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 465226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 465326932566SPatrick McHardy return -EMSGSIZE; 465426932566SPatrick McHardy } 465585486af0SThomas Graf 4656053c095aSJohannes Berg nlmsg_end(skb, nlh); 4657053c095aSJohannes Berg return 0; 46581da177e4SLinus Torvalds } 46591da177e4SLinus Torvalds 4660e21e8467SStephen Hemminger enum addr_type_t { 46611da177e4SLinus Torvalds UNICAST_ADDR, 46621da177e4SLinus Torvalds MULTICAST_ADDR, 46631da177e4SLinus Torvalds ANYCAST_ADDR, 46641da177e4SLinus Torvalds }; 46651da177e4SLinus Torvalds 4666234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4667234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4668234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4669234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 46701da177e4SLinus Torvalds { 46711da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 46721da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4673234b27c3SEric Dumazet int err = 1; 4674234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 46751da177e4SLinus Torvalds 46761da177e4SLinus Torvalds read_lock_bh(&idev->lock); 46771da177e4SLinus Torvalds switch (type) { 4678502a2ffdSstephen hemminger case UNICAST_ADDR: { 4679502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4680502a2ffdSstephen hemminger 4681ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4682502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4683502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 46841da177e4SLinus Torvalds continue; 46855d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 468615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46875d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46885d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 46895d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4690053c095aSJohannes Berg if (err < 0) 4691bcd32326SEric Dumazet break; 469263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 46931da177e4SLinus Torvalds } 46941da177e4SLinus Torvalds break; 4695502a2ffdSstephen hemminger } 46961da177e4SLinus Torvalds case MULTICAST_ADDR: 46971da177e4SLinus Torvalds /* multicast address */ 46981da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 46991da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 47001da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 47011da177e4SLinus Torvalds continue; 47025d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 470315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 47045d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47055d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 47065d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4707053c095aSJohannes Berg if (err < 0) 4708bcd32326SEric Dumazet break; 47091da177e4SLinus Torvalds } 47101da177e4SLinus Torvalds break; 47111da177e4SLinus Torvalds case ANYCAST_ADDR: 47121da177e4SLinus Torvalds /* anycast address */ 47131da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 47141da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 47151da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 47161da177e4SLinus Torvalds continue; 47175d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 471815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 47195d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47205d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 47215d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4722053c095aSJohannes Berg if (err < 0) 4723bcd32326SEric Dumazet break; 47241da177e4SLinus Torvalds } 47251da177e4SLinus Torvalds break; 47261da177e4SLinus Torvalds default: 47271da177e4SLinus Torvalds break; 47281da177e4SLinus Torvalds } 47291da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4730234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4731234b27c3SEric Dumazet return err; 4732234b27c3SEric Dumazet } 47335d5619b4SYOSHIFUJI Hideaki 4734234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4735234b27c3SEric Dumazet enum addr_type_t type) 4736234b27c3SEric Dumazet { 4737234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4738234b27c3SEric Dumazet int h, s_h; 4739234b27c3SEric Dumazet int idx, ip_idx; 4740234b27c3SEric Dumazet int s_idx, s_ip_idx; 4741234b27c3SEric Dumazet struct net_device *dev; 4742234b27c3SEric Dumazet struct inet6_dev *idev; 4743234b27c3SEric Dumazet struct hlist_head *head; 4744234b27c3SEric Dumazet 4745234b27c3SEric Dumazet s_h = cb->args[0]; 4746234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4747234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4748234b27c3SEric Dumazet 4749234b27c3SEric Dumazet rcu_read_lock(); 475063998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4751234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4752234b27c3SEric Dumazet idx = 0; 4753234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4754b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4755234b27c3SEric Dumazet if (idx < s_idx) 4756234b27c3SEric Dumazet goto cont; 47574b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4758234b27c3SEric Dumazet s_ip_idx = 0; 4759234b27c3SEric Dumazet ip_idx = 0; 4760e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4761e21e8467SStephen Hemminger if (!idev) 4762234b27c3SEric Dumazet goto cont; 4763234b27c3SEric Dumazet 4764234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 47657b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4766234b27c3SEric Dumazet goto done; 47677562f876SPavel Emelianov cont: 47687562f876SPavel Emelianov idx++; 47691da177e4SLinus Torvalds } 4770234b27c3SEric Dumazet } 4771234b27c3SEric Dumazet done: 4772234b27c3SEric Dumazet rcu_read_unlock(); 4773234b27c3SEric Dumazet cb->args[0] = h; 4774234b27c3SEric Dumazet cb->args[1] = idx; 4775234b27c3SEric Dumazet cb->args[2] = ip_idx; 4776234b27c3SEric Dumazet 47771da177e4SLinus Torvalds return skb->len; 47781da177e4SLinus Torvalds } 47791da177e4SLinus Torvalds 47801da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 47811da177e4SLinus Torvalds { 47821da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4783b854272bSDenis V. Lunev 47841da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47851da177e4SLinus Torvalds } 47861da177e4SLinus Torvalds 47871da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 47881da177e4SLinus Torvalds { 47891da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4790b854272bSDenis V. Lunev 47911da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47921da177e4SLinus Torvalds } 47931da177e4SLinus Torvalds 47941da177e4SLinus Torvalds 47951da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 47961da177e4SLinus Torvalds { 47971da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4798b854272bSDenis V. Lunev 47991da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 48001da177e4SLinus Torvalds } 48011da177e4SLinus Torvalds 4802661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 48036c223828SNoriaki TAKAMIYA { 48043b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 48051b29fc2cSThomas Graf struct ifaddrmsg *ifm; 48061b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4807caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 48086c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 48096c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 48106c223828SNoriaki TAKAMIYA struct sk_buff *skb; 48116c223828SNoriaki TAKAMIYA int err; 48126c223828SNoriaki TAKAMIYA 48131b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 48141b29fc2cSThomas Graf if (err < 0) 48151b29fc2cSThomas Graf goto errout; 48166c223828SNoriaki TAKAMIYA 4817caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 481863159f29SIan Morris if (!addr) { 48191b29fc2cSThomas Graf err = -EINVAL; 48201b29fc2cSThomas Graf goto errout; 48211b29fc2cSThomas Graf } 48221b29fc2cSThomas Graf 48231b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 48246c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 48256fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 48266c223828SNoriaki TAKAMIYA 4827e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4828e21e8467SStephen Hemminger if (!ifa) { 48291b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 48301b29fc2cSThomas Graf goto errout; 48316c223828SNoriaki TAKAMIYA } 48326c223828SNoriaki TAKAMIYA 4833e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4834e21e8467SStephen Hemminger if (!skb) { 48351b29fc2cSThomas Graf err = -ENOBUFS; 48361b29fc2cSThomas Graf goto errout_ifa; 48371b29fc2cSThomas Graf } 48381b29fc2cSThomas Graf 483915e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 48406c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 484126932566SPatrick McHardy if (err < 0) { 484226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 484326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 484426932566SPatrick McHardy kfree_skb(skb); 484526932566SPatrick McHardy goto errout_ifa; 484626932566SPatrick McHardy } 484715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 48481b29fc2cSThomas Graf errout_ifa: 48496c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 48501b29fc2cSThomas Graf errout: 48516c223828SNoriaki TAKAMIYA return err; 48526c223828SNoriaki TAKAMIYA } 48536c223828SNoriaki TAKAMIYA 48541da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 48551da177e4SLinus Torvalds { 48561da177e4SLinus Torvalds struct sk_buff *skb; 4857c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 48585d620266SThomas Graf int err = -ENOBUFS; 48591da177e4SLinus Torvalds 48600ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 486163159f29SIan Morris if (!skb) 48625d620266SThomas Graf goto errout; 48635d620266SThomas Graf 48645d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 486526932566SPatrick McHardy if (err < 0) { 486626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 486726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 486826932566SPatrick McHardy kfree_skb(skb); 486926932566SPatrick McHardy goto errout; 487026932566SPatrick McHardy } 48711ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 48721ce85fe4SPablo Neira Ayuso return; 48735d620266SThomas Graf errout: 48745d620266SThomas Graf if (err < 0) 48756fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 48761da177e4SLinus Torvalds } 48771da177e4SLinus Torvalds 4878b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 48791da177e4SLinus Torvalds __s32 *array, int bytes) 48801da177e4SLinus Torvalds { 488104561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 488204561c1fSThomas Graf 48831da177e4SLinus Torvalds memset(array, 0, bytes); 48841da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 48851da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 48861da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 48871da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 48881da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 48891da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 48901da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 48911da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 489293908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 489393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 489493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 489593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 48961da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4897fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4898fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4899fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4900fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 49011da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 49021da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 49031da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 49041da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 49051da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 49061da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 490765f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 49088013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 4909c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4910930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4911930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 491293908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 491393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4914fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 491509c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 491609c884d4SYOSHIFUJI Hideaki #endif 4917930d6ff2SYOSHIFUJI Hideaki #endif 4918fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 49190bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 492095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 492195c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 49227fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 492395c385b4SNeil Horman #endif 49247bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 49257bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 49267bc570c8SYOSHIFUJI Hideaki #endif 4927778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 49281b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4929c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 49305cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4931b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4932d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4933c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 493435103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 49353d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 49363985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 4937abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 49387a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 4939f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 49401da177e4SLinus Torvalds } 49411da177e4SLinus Torvalds 4942b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4943b382b191SThomas Graf { 4944b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4945b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4946b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4947b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4948f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4949f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4950b382b191SThomas Graf } 4951b382b191SThomas Graf 4952339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4953339bf98fSThomas Graf { 4954339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4955339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4956339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4957339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4958339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 49590344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 4960b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4961339bf98fSThomas Graf } 4962c5396a31SYOSHIFUJI Hideaki 4963be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 4964*aca05671SJia He int bytes) 49657f7d9a6bSHerbert Xu { 49667f7d9a6bSHerbert Xu int i; 4967*aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 49687f7d9a6bSHerbert Xu BUG_ON(pad < 0); 49697f7d9a6bSHerbert Xu 49707f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 4971*aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 4972*aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 4973be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 49747f7d9a6bSHerbert Xu 4975*aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 49767f7d9a6bSHerbert Xu } 49777f7d9a6bSHerbert Xu 4978698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 4979a3a77372SRaghavendra K T int bytes, size_t syncpoff) 49804ce3c183SEric Dumazet { 4981a3a77372SRaghavendra K T int i, c; 4982a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 4983a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 4984a3a77372SRaghavendra K T 49854ce3c183SEric Dumazet BUG_ON(pad < 0); 49864ce3c183SEric Dumazet 4987a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 4988a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 49894ce3c183SEric Dumazet 4990a3a77372SRaghavendra K T for_each_possible_cpu(c) { 4991a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 4992a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 4993a3a77372SRaghavendra K T } 4994a3a77372SRaghavendra K T 4995a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 4996a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 49974ce3c183SEric Dumazet } 49984ce3c183SEric Dumazet 49997f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 50007f7d9a6bSHerbert Xu int bytes) 50017f7d9a6bSHerbert Xu { 50027f7d9a6bSHerbert Xu switch (attrtype) { 50037f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5004a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5005a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 50067f7d9a6bSHerbert Xu break; 50077f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5008*aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 50097f7d9a6bSHerbert Xu break; 50107f7d9a6bSHerbert Xu } 50117f7d9a6bSHerbert Xu } 50127f7d9a6bSHerbert Xu 5013d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5014d5566fd7SSowmini Varadhan u32 ext_filter_mask) 50151da177e4SLinus Torvalds { 5016bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 50171da177e4SLinus Torvalds struct ifla_cacheinfo ci; 50181da177e4SLinus Torvalds 5019c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5020c78679e8SDavid S. Miller goto nla_put_failure; 50211da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 502224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 502324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 50241f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5025c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5026c78679e8SDavid S. Miller goto nla_put_failure; 5027bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 502863159f29SIan Morris if (!nla) 502904561c1fSThomas Graf goto nla_put_failure; 5030bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 50311da177e4SLinus Torvalds 5032bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5033bf99f1bdSYOSHIFUJI Hideaki 5034d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5035d5566fd7SSowmini Varadhan return 0; 5036d5566fd7SSowmini Varadhan 5037bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 503863159f29SIan Morris if (!nla) 5039bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5040bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5041bf99f1bdSYOSHIFUJI Hideaki 5042bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 504363159f29SIan Morris if (!nla) 5044bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5045bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 50461da177e4SLinus Torvalds 5047f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 504863159f29SIan Morris if (!nla) 5049f53adae4SDaniel Borkmann goto nla_put_failure; 5050bc91b0f0SJiri Pirko 5051bc91b0f0SJiri Pirko if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 5052bc91b0f0SJiri Pirko goto nla_put_failure; 5053bc91b0f0SJiri Pirko 5054f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5055f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5056f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5057f53adae4SDaniel Borkmann 5058b382b191SThomas Graf return 0; 5059b382b191SThomas Graf 5060b382b191SThomas Graf nla_put_failure: 5061b382b191SThomas Graf return -EMSGSIZE; 5062b382b191SThomas Graf } 5063b382b191SThomas Graf 5064b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5065b1974ed0SArad, Ronen u32 ext_filter_mask) 5066b382b191SThomas Graf { 5067b382b191SThomas Graf if (!__in6_dev_get(dev)) 5068b382b191SThomas Graf return 0; 5069b382b191SThomas Graf 5070b382b191SThomas Graf return inet6_ifla6_size(); 5071b382b191SThomas Graf } 5072b382b191SThomas Graf 5073d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5074d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5075b382b191SThomas Graf { 5076b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5077b382b191SThomas Graf 5078b382b191SThomas Graf if (!idev) 5079b382b191SThomas Graf return -ENODATA; 5080b382b191SThomas Graf 5081d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5082b382b191SThomas Graf return -EMSGSIZE; 5083b382b191SThomas Graf 5084b382b191SThomas Graf return 0; 5085b382b191SThomas Graf } 5086b382b191SThomas Graf 5087f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5088f53adae4SDaniel Borkmann { 5089f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5090f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 509147e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5092dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5093f53adae4SDaniel Borkmann 5094c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5095c15b1ccaSHannes Frederic Sowa 509663159f29SIan Morris if (!token) 5097f53adae4SDaniel Borkmann return -EINVAL; 5098f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5099f53adae4SDaniel Borkmann return -EINVAL; 5100f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5101f53adae4SDaniel Borkmann return -EINVAL; 5102f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 5103f53adae4SDaniel Borkmann return -EINVAL; 5104f53adae4SDaniel Borkmann 5105f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5106f53adae4SDaniel Borkmann 5107f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5108f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5109f53adae4SDaniel Borkmann 5110f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5111f53adae4SDaniel Borkmann 511247e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 511347e27d5eSDaniel Borkmann if (clear_token) 511447e27d5eSDaniel Borkmann goto update_lft; 511547e27d5eSDaniel Borkmann 5116dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5117dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5118dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5119fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5120fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5121fc403832SDaniel Borkmann */ 5122f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5123fc403832SDaniel Borkmann update_rs = true; 5124fc403832SDaniel Borkmann } 5125f53adae4SDaniel Borkmann 512647e27d5eSDaniel Borkmann update_lft: 5127f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5128fc403832SDaniel Borkmann 512977ecaaceSHannes Frederic Sowa if (update_rs) { 5130f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 513177ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 513277ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 513377ecaaceSHannes Frederic Sowa } 5134f53adae4SDaniel Borkmann 5135f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5136f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5137f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5138617fe29dSDaniel Borkmann if (ifp->tokenized) { 5139f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5140f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5141f53adae4SDaniel Borkmann } 5142f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5143f53adae4SDaniel Borkmann } 5144f53adae4SDaniel Borkmann 5145f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5146b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5147c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5148f53adae4SDaniel Borkmann return 0; 5149f53adae4SDaniel Borkmann } 5150f53adae4SDaniel Borkmann 515111b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 515211b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 515311b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 515411b1f828SDaniel Borkmann }; 515511b1f828SDaniel Borkmann 515611b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 515711b1f828SDaniel Borkmann const struct nlattr *nla) 515811b1f828SDaniel Borkmann { 515911b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 516011b1f828SDaniel Borkmann 516111b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 516211b1f828SDaniel Borkmann return -EAFNOSUPPORT; 516311b1f828SDaniel Borkmann 516411b1f828SDaniel Borkmann return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 516511b1f828SDaniel Borkmann } 516611b1f828SDaniel Borkmann 5167f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5168f53adae4SDaniel Borkmann { 5169f53adae4SDaniel Borkmann int err = -EINVAL; 5170f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5171f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5172f53adae4SDaniel Borkmann 5173f53adae4SDaniel Borkmann if (!idev) 5174f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5175f53adae4SDaniel Borkmann 5176f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 5177f53adae4SDaniel Borkmann BUG(); 5178f53adae4SDaniel Borkmann 5179bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5180f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5181bc91b0f0SJiri Pirko if (err) 5182bc91b0f0SJiri Pirko return err; 5183bc91b0f0SJiri Pirko } 5184bc91b0f0SJiri Pirko 5185bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5186bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5187bc91b0f0SJiri Pirko 5188bc91b0f0SJiri Pirko if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5189622c81d5SHannes Frederic Sowa mode != IN6_ADDR_GEN_MODE_NONE && 5190cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5191cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_RANDOM) 5192bc91b0f0SJiri Pirko return -EINVAL; 5193622c81d5SHannes Frederic Sowa 5194622c81d5SHannes Frederic Sowa if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5195622c81d5SHannes Frederic Sowa !idev->cnf.stable_secret.initialized && 5196622c81d5SHannes Frederic Sowa !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) 5197622c81d5SHannes Frederic Sowa return -EINVAL; 5198622c81d5SHannes Frederic Sowa 5199bc91b0f0SJiri Pirko idev->addr_gen_mode = mode; 5200bc91b0f0SJiri Pirko err = 0; 5201bc91b0f0SJiri Pirko } 5202f53adae4SDaniel Borkmann 5203f53adae4SDaniel Borkmann return err; 5204f53adae4SDaniel Borkmann } 5205f53adae4SDaniel Borkmann 5206b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 520715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5208b382b191SThomas Graf { 5209b382b191SThomas Graf struct net_device *dev = idev->dev; 5210b382b191SThomas Graf struct ifinfomsg *hdr; 5211b382b191SThomas Graf struct nlmsghdr *nlh; 5212b382b191SThomas Graf void *protoinfo; 5213b382b191SThomas Graf 521415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 521563159f29SIan Morris if (!nlh) 5216b382b191SThomas Graf return -EMSGSIZE; 5217b382b191SThomas Graf 5218b382b191SThomas Graf hdr = nlmsg_data(nlh); 5219b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5220b382b191SThomas Graf hdr->__ifi_pad = 0; 5221b382b191SThomas Graf hdr->ifi_type = dev->type; 5222b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5223b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5224b382b191SThomas Graf hdr->ifi_change = 0; 5225b382b191SThomas Graf 5226c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5227c78679e8SDavid S. Miller (dev->addr_len && 5228c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5229c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5230a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 52310344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 52320344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 52330344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5234c78679e8SDavid S. Miller goto nla_put_failure; 5235b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 523663159f29SIan Morris if (!protoinfo) 5237b382b191SThomas Graf goto nla_put_failure; 5238b382b191SThomas Graf 5239d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5240b382b191SThomas Graf goto nla_put_failure; 5241b382b191SThomas Graf 524204561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5243053c095aSJohannes Berg nlmsg_end(skb, nlh); 5244053c095aSJohannes Berg return 0; 52451da177e4SLinus Torvalds 524604561c1fSThomas Graf nla_put_failure: 524726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 524826932566SPatrick McHardy return -EMSGSIZE; 52491da177e4SLinus Torvalds } 52501da177e4SLinus Torvalds 52511da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 52521da177e4SLinus Torvalds { 52533b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 525484d2697dSEric Dumazet int h, s_h; 5255434a8a58SDavid S. Miller int idx = 0, s_idx; 52561da177e4SLinus Torvalds struct net_device *dev; 52571da177e4SLinus Torvalds struct inet6_dev *idev; 525884d2697dSEric Dumazet struct hlist_head *head; 52591da177e4SLinus Torvalds 526084d2697dSEric Dumazet s_h = cb->args[0]; 526184d2697dSEric Dumazet s_idx = cb->args[1]; 526284d2697dSEric Dumazet 526384d2697dSEric Dumazet rcu_read_lock(); 526484d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 52657562f876SPavel Emelianov idx = 0; 526684d2697dSEric Dumazet head = &net->dev_index_head[h]; 5267b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 52681da177e4SLinus Torvalds if (idx < s_idx) 52697562f876SPavel Emelianov goto cont; 527084d2697dSEric Dumazet idev = __in6_dev_get(dev); 527184d2697dSEric Dumazet if (!idev) 52727562f876SPavel Emelianov goto cont; 527384d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 527415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 527584d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5276053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 527784d2697dSEric Dumazet goto out; 52787562f876SPavel Emelianov cont: 52797562f876SPavel Emelianov idx++; 52801da177e4SLinus Torvalds } 528184d2697dSEric Dumazet } 528284d2697dSEric Dumazet out: 528384d2697dSEric Dumazet rcu_read_unlock(); 528484d2697dSEric Dumazet cb->args[1] = idx; 528584d2697dSEric Dumazet cb->args[0] = h; 52861da177e4SLinus Torvalds 52871da177e4SLinus Torvalds return skb->len; 52881da177e4SLinus Torvalds } 52891da177e4SLinus Torvalds 52901da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 52911da177e4SLinus Torvalds { 52921da177e4SLinus Torvalds struct sk_buff *skb; 5293c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 52948d7a76c9SThomas Graf int err = -ENOBUFS; 52951da177e4SLinus Torvalds 5296339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 529763159f29SIan Morris if (!skb) 52988d7a76c9SThomas Graf goto errout; 52998d7a76c9SThomas Graf 53008d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 530126932566SPatrick McHardy if (err < 0) { 530226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 530326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 530426932566SPatrick McHardy kfree_skb(skb); 530526932566SPatrick McHardy goto errout; 530626932566SPatrick McHardy } 53075f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 53081ce85fe4SPablo Neira Ayuso return; 53098d7a76c9SThomas Graf errout: 53108d7a76c9SThomas Graf if (err < 0) 53115f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 53121da177e4SLinus Torvalds } 53131da177e4SLinus Torvalds 5314339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5315339bf98fSThomas Graf { 5316339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5317339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5318339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5319339bf98fSThomas Graf } 5320c5396a31SYOSHIFUJI Hideaki 53211da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 532215e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5323b6544c0bSJamal Hadi Salim int event, unsigned int flags) 53241da177e4SLinus Torvalds { 53251da177e4SLinus Torvalds struct prefixmsg *pmsg; 53261da177e4SLinus Torvalds struct nlmsghdr *nlh; 53271da177e4SLinus Torvalds struct prefix_cacheinfo ci; 53281da177e4SLinus Torvalds 532915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 533063159f29SIan Morris if (!nlh) 533126932566SPatrick McHardy return -EMSGSIZE; 53326051e2f4SThomas Graf 53336051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 53341da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 53358a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 53368a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 53371da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 53381da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 53391da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 53408a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 53411da177e4SLinus Torvalds pmsg->prefix_flags = 0; 53421da177e4SLinus Torvalds if (pinfo->onlink) 53431da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 53441da177e4SLinus Torvalds if (pinfo->autoconf) 53451da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 53461da177e4SLinus Torvalds 5347c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5348c78679e8SDavid S. Miller goto nla_put_failure; 53491da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 53501da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5351c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5352c78679e8SDavid S. Miller goto nla_put_failure; 5353053c095aSJohannes Berg nlmsg_end(skb, nlh); 5354053c095aSJohannes Berg return 0; 53551da177e4SLinus Torvalds 53566051e2f4SThomas Graf nla_put_failure: 535726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 535826932566SPatrick McHardy return -EMSGSIZE; 53591da177e4SLinus Torvalds } 53601da177e4SLinus Torvalds 53611da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 53621da177e4SLinus Torvalds struct prefix_info *pinfo) 53631da177e4SLinus Torvalds { 53641da177e4SLinus Torvalds struct sk_buff *skb; 5365c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 53668c384bfaSThomas Graf int err = -ENOBUFS; 53671da177e4SLinus Torvalds 5368339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 536963159f29SIan Morris if (!skb) 53708c384bfaSThomas Graf goto errout; 53718c384bfaSThomas Graf 53728c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 537326932566SPatrick McHardy if (err < 0) { 537426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 537526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 537626932566SPatrick McHardy kfree_skb(skb); 537726932566SPatrick McHardy goto errout; 537826932566SPatrick McHardy } 53791ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 53801ce85fe4SPablo Neira Ayuso return; 53818c384bfaSThomas Graf errout: 53828c384bfaSThomas Graf if (err < 0) 53836fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 53841da177e4SLinus Torvalds } 53851da177e4SLinus Torvalds 53861da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 53871da177e4SLinus Torvalds { 538863998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 538963998ac2SNicolas Dichtel 5390c15b1ccaSHannes Frederic Sowa if (event) 5391c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5392c15b1ccaSHannes Frederic Sowa 53931da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 53941da177e4SLinus Torvalds 53951da177e4SLinus Torvalds switch (event) { 53961da177e4SLinus Torvalds case RTM_NEWADDR: 539795c385b4SNeil Horman /* 539895c385b4SNeil Horman * If the address was optimistic 539995c385b4SNeil Horman * we inserted the route at the start of 540095c385b4SNeil Horman * our DAD process, so we don't need 540195c385b4SNeil Horman * to do it again 540295c385b4SNeil Horman */ 540395c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 540440e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 54051da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 54061da177e4SLinus Torvalds addrconf_join_anycast(ifp); 54077996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5408caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5409caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 54101da177e4SLinus Torvalds break; 54111da177e4SLinus Torvalds case RTM_DELADDR: 54121da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 54131da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 54141da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 54157996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5416caeaba79SNicolas Dichtel struct rt6_info *rt; 5417caeaba79SNicolas Dichtel 5418e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5419e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 54208e3d5be7SMartin KaFai Lau if (rt) 54218e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5422caeaba79SNicolas Dichtel } 542338bd10c4SDavid Ahern if (ifp->rt) { 5424d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 54258e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 542638bd10c4SDavid Ahern } 5427705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 54281da177e4SLinus Torvalds break; 54291da177e4SLinus Torvalds } 543063998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 54311da177e4SLinus Torvalds } 54321da177e4SLinus Torvalds 54331da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 54341da177e4SLinus Torvalds { 54358814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 54361da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 54371da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 54388814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 54391da177e4SLinus Torvalds } 54401da177e4SLinus Torvalds 54411da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 54421da177e4SLinus Torvalds 54431da177e4SLinus Torvalds static 5444fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 54451da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 54461da177e4SLinus Torvalds { 54471da177e4SLinus Torvalds int *valp = ctl->data; 54481da177e4SLinus Torvalds int val = *valp; 544988af182eSEric W. Biederman loff_t pos = *ppos; 5450fe2c6338SJoe Perches struct ctl_table lctl; 54511da177e4SLinus Torvalds int ret; 54521da177e4SLinus Torvalds 5453013d97e9SFrancesco Ruggeri /* 5454013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5455013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5456013d97e9SFrancesco Ruggeri */ 5457013d97e9SFrancesco Ruggeri lctl = *ctl; 5458013d97e9SFrancesco Ruggeri lctl.data = &val; 5459013d97e9SFrancesco Ruggeri 5460013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 54611da177e4SLinus Torvalds 5462c8fecf22SPavel Emelyanov if (write) 5463b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 546488af182eSEric W. Biederman if (ret) 546588af182eSEric W. Biederman *ppos = pos; 54661da177e4SLinus Torvalds return ret; 54671da177e4SLinus Torvalds } 54681da177e4SLinus Torvalds 546977751427SMarcelo Leitner static 5470d6df198dSPhil Sutter int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write, 5471d6df198dSPhil Sutter void __user *buffer, size_t *lenp, loff_t *ppos) 5472d6df198dSPhil Sutter { 5473d6df198dSPhil Sutter struct ctl_table lctl; 5474d6df198dSPhil Sutter int min_hl = 1, max_hl = 255; 5475d6df198dSPhil Sutter 5476d6df198dSPhil Sutter lctl = *ctl; 5477d6df198dSPhil Sutter lctl.extra1 = &min_hl; 5478d6df198dSPhil Sutter lctl.extra2 = &max_hl; 5479d6df198dSPhil Sutter 5480d6df198dSPhil Sutter return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 5481d6df198dSPhil Sutter } 5482d6df198dSPhil Sutter 5483d6df198dSPhil Sutter static 548477751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 548577751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 548677751427SMarcelo Leitner { 548777751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 548877751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 548977751427SMarcelo Leitner struct ctl_table lctl; 549077751427SMarcelo Leitner 549177751427SMarcelo Leitner lctl = *ctl; 549277751427SMarcelo Leitner lctl.extra1 = &min_mtu; 549377751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 549477751427SMarcelo Leitner 549577751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 549677751427SMarcelo Leitner } 549777751427SMarcelo Leitner 549856d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 549956d417b1SBrian Haley { 550075538c2bSCong Wang struct netdev_notifier_info info; 550175538c2bSCong Wang 550256d417b1SBrian Haley if (!idev || !idev->dev) 550356d417b1SBrian Haley return; 550456d417b1SBrian Haley 550575538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 550656d417b1SBrian Haley if (idev->cnf.disable_ipv6) 550775538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 550856d417b1SBrian Haley else 550975538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 551056d417b1SBrian Haley } 551156d417b1SBrian Haley 551256d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 551356d417b1SBrian Haley { 551456d417b1SBrian Haley struct net_device *dev; 551556d417b1SBrian Haley struct inet6_dev *idev; 551656d417b1SBrian Haley 551756d417b1SBrian Haley rcu_read_lock(); 5518c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 551956d417b1SBrian Haley idev = __in6_dev_get(dev); 552056d417b1SBrian Haley if (idev) { 552156d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 552256d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 552356d417b1SBrian Haley if (changed) 552456d417b1SBrian Haley dev_disable_change(idev); 552556d417b1SBrian Haley } 552656d417b1SBrian Haley } 5527c6d14c84SEric Dumazet rcu_read_unlock(); 552856d417b1SBrian Haley } 552956d417b1SBrian Haley 5530013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 553156d417b1SBrian Haley { 553256d417b1SBrian Haley struct net *net; 5533013d97e9SFrancesco Ruggeri int old; 5534013d97e9SFrancesco Ruggeri 5535013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5536013d97e9SFrancesco Ruggeri return restart_syscall(); 553756d417b1SBrian Haley 553856d417b1SBrian Haley net = (struct net *)table->extra2; 5539013d97e9SFrancesco Ruggeri old = *p; 5540013d97e9SFrancesco Ruggeri *p = newf; 554156d417b1SBrian Haley 5542013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5543013d97e9SFrancesco Ruggeri rtnl_unlock(); 554456d417b1SBrian Haley return 0; 554588af182eSEric W. Biederman } 554656d417b1SBrian Haley 554756d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 554856d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 554956d417b1SBrian Haley addrconf_disable_change(net, newf); 5550013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 555156d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 555256d417b1SBrian Haley 555356d417b1SBrian Haley rtnl_unlock(); 555456d417b1SBrian Haley return 0; 555556d417b1SBrian Haley } 555656d417b1SBrian Haley 555756d417b1SBrian Haley static 5558fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 555956d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 556056d417b1SBrian Haley { 556156d417b1SBrian Haley int *valp = ctl->data; 556256d417b1SBrian Haley int val = *valp; 556388af182eSEric W. Biederman loff_t pos = *ppos; 5564fe2c6338SJoe Perches struct ctl_table lctl; 556556d417b1SBrian Haley int ret; 556656d417b1SBrian Haley 5567013d97e9SFrancesco Ruggeri /* 5568013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5569013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5570013d97e9SFrancesco Ruggeri */ 5571013d97e9SFrancesco Ruggeri lctl = *ctl; 5572013d97e9SFrancesco Ruggeri lctl.data = &val; 5573013d97e9SFrancesco Ruggeri 5574013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 557556d417b1SBrian Haley 557656d417b1SBrian Haley if (write) 557756d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 557888af182eSEric W. Biederman if (ret) 557988af182eSEric W. Biederman *ppos = pos; 558056d417b1SBrian Haley return ret; 558156d417b1SBrian Haley } 558256d417b1SBrian Haley 5583c92d5491Sstephen hemminger static 5584c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5585c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5586c92d5491Sstephen hemminger { 5587c92d5491Sstephen hemminger int *valp = ctl->data; 5588c92d5491Sstephen hemminger int ret; 5589c92d5491Sstephen hemminger int old, new; 5590c92d5491Sstephen hemminger 5591c92d5491Sstephen hemminger old = *valp; 5592c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5593c92d5491Sstephen hemminger new = *valp; 5594c92d5491Sstephen hemminger 5595c92d5491Sstephen hemminger if (write && old != new) { 5596c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5597c92d5491Sstephen hemminger 5598c92d5491Sstephen hemminger if (!rtnl_trylock()) 5599c92d5491Sstephen hemminger return restart_syscall(); 5600c92d5491Sstephen hemminger 5601c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5602c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5603c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5604c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5605c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5606c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5607c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5608c92d5491Sstephen hemminger net->ipv6.devconf_all); 5609c92d5491Sstephen hemminger else { 5610c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5611c92d5491Sstephen hemminger 5612c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5613c92d5491Sstephen hemminger idev->dev->ifindex, 5614c92d5491Sstephen hemminger &idev->cnf); 5615c92d5491Sstephen hemminger } 5616c92d5491Sstephen hemminger rtnl_unlock(); 5617c92d5491Sstephen hemminger } 5618c92d5491Sstephen hemminger 5619c92d5491Sstephen hemminger return ret; 5620c92d5491Sstephen hemminger } 5621c92d5491Sstephen hemminger 56223d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 56233d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 56243d1bec99SHannes Frederic Sowa loff_t *ppos) 56253d1bec99SHannes Frederic Sowa { 56263d1bec99SHannes Frederic Sowa int err; 56273d1bec99SHannes Frederic Sowa struct in6_addr addr; 56283d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 56293d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5630622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 56313d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 56323d1bec99SHannes Frederic Sowa 5633622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5634622c81d5SHannes Frederic Sowa return -EIO; 5635622c81d5SHannes Frederic Sowa 56363d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 56373d1bec99SHannes Frederic Sowa lctl.data = str; 56383d1bec99SHannes Frederic Sowa 56393d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 56403d1bec99SHannes Frederic Sowa return restart_syscall(); 56413d1bec99SHannes Frederic Sowa 56423d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 56433d1bec99SHannes Frederic Sowa err = -EIO; 56443d1bec99SHannes Frederic Sowa goto out; 56453d1bec99SHannes Frederic Sowa } 56463d1bec99SHannes Frederic Sowa 56475449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 56483d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 56493d1bec99SHannes Frederic Sowa err = -EIO; 56503d1bec99SHannes Frederic Sowa goto out; 56513d1bec99SHannes Frederic Sowa } 56523d1bec99SHannes Frederic Sowa 56533d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 56543d1bec99SHannes Frederic Sowa if (err || !write) 56553d1bec99SHannes Frederic Sowa goto out; 56563d1bec99SHannes Frederic Sowa 56573d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 56583d1bec99SHannes Frederic Sowa err = -EIO; 56593d1bec99SHannes Frederic Sowa goto out; 56603d1bec99SHannes Frederic Sowa } 56613d1bec99SHannes Frederic Sowa 56623d1bec99SHannes Frederic Sowa secret->initialized = true; 56633d1bec99SHannes Frederic Sowa secret->secret = addr; 56643d1bec99SHannes Frederic Sowa 5665622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5666622c81d5SHannes Frederic Sowa struct net_device *dev; 5667622c81d5SHannes Frederic Sowa 5668622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5669622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5670622c81d5SHannes Frederic Sowa 5671622c81d5SHannes Frederic Sowa if (idev) { 5672622c81d5SHannes Frederic Sowa idev->addr_gen_mode = 5673622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5674622c81d5SHannes Frederic Sowa } 5675622c81d5SHannes Frederic Sowa } 5676622c81d5SHannes Frederic Sowa } else { 5677622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5678622c81d5SHannes Frederic Sowa 5679622c81d5SHannes Frederic Sowa idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5680622c81d5SHannes Frederic Sowa } 5681622c81d5SHannes Frederic Sowa 56823d1bec99SHannes Frederic Sowa out: 56833d1bec99SHannes Frederic Sowa rtnl_unlock(); 56843d1bec99SHannes Frederic Sowa 56853d1bec99SHannes Frederic Sowa return err; 56863d1bec99SHannes Frederic Sowa } 5687c92d5491Sstephen hemminger 568835103d11SAndy Gospodarek static 568935103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 569035103d11SAndy Gospodarek int write, 569135103d11SAndy Gospodarek void __user *buffer, 569235103d11SAndy Gospodarek size_t *lenp, 569335103d11SAndy Gospodarek loff_t *ppos) 569435103d11SAndy Gospodarek { 569535103d11SAndy Gospodarek int *valp = ctl->data; 569635103d11SAndy Gospodarek int val = *valp; 569735103d11SAndy Gospodarek loff_t pos = *ppos; 569835103d11SAndy Gospodarek struct ctl_table lctl; 569935103d11SAndy Gospodarek int ret; 570035103d11SAndy Gospodarek 570135103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 570235103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 570335103d11SAndy Gospodarek */ 570435103d11SAndy Gospodarek lctl = *ctl; 570535103d11SAndy Gospodarek lctl.data = &val; 570635103d11SAndy Gospodarek 570735103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 570835103d11SAndy Gospodarek 570935103d11SAndy Gospodarek if (write) 571035103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 571135103d11SAndy Gospodarek if (ret) 571235103d11SAndy Gospodarek *ppos = pos; 571335103d11SAndy Gospodarek return ret; 571435103d11SAndy Gospodarek } 571535103d11SAndy Gospodarek 5716607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 57171da177e4SLinus Torvalds { 57181da177e4SLinus Torvalds .procname = "forwarding", 57191da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 57201da177e4SLinus Torvalds .maxlen = sizeof(int), 57211da177e4SLinus Torvalds .mode = 0644, 57226d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 57231da177e4SLinus Torvalds }, 57241da177e4SLinus Torvalds { 57251da177e4SLinus Torvalds .procname = "hop_limit", 57261da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 57271da177e4SLinus Torvalds .maxlen = sizeof(int), 57281da177e4SLinus Torvalds .mode = 0644, 5729d6df198dSPhil Sutter .proc_handler = addrconf_sysctl_hop_limit, 57301da177e4SLinus Torvalds }, 57311da177e4SLinus Torvalds { 57321da177e4SLinus Torvalds .procname = "mtu", 57331da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 57341da177e4SLinus Torvalds .maxlen = sizeof(int), 57351da177e4SLinus Torvalds .mode = 0644, 573677751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 57371da177e4SLinus Torvalds }, 57381da177e4SLinus Torvalds { 57391da177e4SLinus Torvalds .procname = "accept_ra", 57401da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 57411da177e4SLinus Torvalds .maxlen = sizeof(int), 57421da177e4SLinus Torvalds .mode = 0644, 57436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57441da177e4SLinus Torvalds }, 57451da177e4SLinus Torvalds { 57461da177e4SLinus Torvalds .procname = "accept_redirects", 57471da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 57481da177e4SLinus Torvalds .maxlen = sizeof(int), 57491da177e4SLinus Torvalds .mode = 0644, 57506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57511da177e4SLinus Torvalds }, 57521da177e4SLinus Torvalds { 57531da177e4SLinus Torvalds .procname = "autoconf", 57541da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 57551da177e4SLinus Torvalds .maxlen = sizeof(int), 57561da177e4SLinus Torvalds .mode = 0644, 57576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57581da177e4SLinus Torvalds }, 57591da177e4SLinus Torvalds { 57601da177e4SLinus Torvalds .procname = "dad_transmits", 57611da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 57621da177e4SLinus Torvalds .maxlen = sizeof(int), 57631da177e4SLinus Torvalds .mode = 0644, 57646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57651da177e4SLinus Torvalds }, 57661da177e4SLinus Torvalds { 57671da177e4SLinus Torvalds .procname = "router_solicitations", 57681da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 57691da177e4SLinus Torvalds .maxlen = sizeof(int), 57701da177e4SLinus Torvalds .mode = 0644, 57716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57721da177e4SLinus Torvalds }, 57731da177e4SLinus Torvalds { 57741da177e4SLinus Torvalds .procname = "router_solicitation_interval", 57751da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 57761da177e4SLinus Torvalds .maxlen = sizeof(int), 57771da177e4SLinus Torvalds .mode = 0644, 57786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57791da177e4SLinus Torvalds }, 57801da177e4SLinus Torvalds { 57811da177e4SLinus Torvalds .procname = "router_solicitation_delay", 57821da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 57831da177e4SLinus Torvalds .maxlen = sizeof(int), 57841da177e4SLinus Torvalds .mode = 0644, 57856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57861da177e4SLinus Torvalds }, 57871da177e4SLinus Torvalds { 57881da177e4SLinus Torvalds .procname = "force_mld_version", 57891da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 57901da177e4SLinus Torvalds .maxlen = sizeof(int), 57911da177e4SLinus Torvalds .mode = 0644, 57926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57931da177e4SLinus Torvalds }, 5794fc4eba58SHannes Frederic Sowa { 5795fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 5796fc4eba58SHannes Frederic Sowa .data = 5797fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 5798fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5799fc4eba58SHannes Frederic Sowa .mode = 0644, 5800fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5801fc4eba58SHannes Frederic Sowa }, 5802fc4eba58SHannes Frederic Sowa { 5803fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 5804fc4eba58SHannes Frederic Sowa .data = 5805fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 5806fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5807fc4eba58SHannes Frederic Sowa .mode = 0644, 5808fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5809fc4eba58SHannes Frederic Sowa }, 58101da177e4SLinus Torvalds { 58111da177e4SLinus Torvalds .procname = "use_tempaddr", 58121da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 58131da177e4SLinus Torvalds .maxlen = sizeof(int), 58141da177e4SLinus Torvalds .mode = 0644, 58156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58161da177e4SLinus Torvalds }, 58171da177e4SLinus Torvalds { 58181da177e4SLinus Torvalds .procname = "temp_valid_lft", 58191da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 58201da177e4SLinus Torvalds .maxlen = sizeof(int), 58211da177e4SLinus Torvalds .mode = 0644, 58226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58231da177e4SLinus Torvalds }, 58241da177e4SLinus Torvalds { 58251da177e4SLinus Torvalds .procname = "temp_prefered_lft", 58261da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 58271da177e4SLinus Torvalds .maxlen = sizeof(int), 58281da177e4SLinus Torvalds .mode = 0644, 58296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58301da177e4SLinus Torvalds }, 58311da177e4SLinus Torvalds { 58321da177e4SLinus Torvalds .procname = "regen_max_retry", 58331da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 58341da177e4SLinus Torvalds .maxlen = sizeof(int), 58351da177e4SLinus Torvalds .mode = 0644, 58366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58371da177e4SLinus Torvalds }, 58381da177e4SLinus Torvalds { 58391da177e4SLinus Torvalds .procname = "max_desync_factor", 58401da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 58411da177e4SLinus Torvalds .maxlen = sizeof(int), 58421da177e4SLinus Torvalds .mode = 0644, 58436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58441da177e4SLinus Torvalds }, 58451da177e4SLinus Torvalds { 58461da177e4SLinus Torvalds .procname = "max_addresses", 58471da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 58481da177e4SLinus Torvalds .maxlen = sizeof(int), 58491da177e4SLinus Torvalds .mode = 0644, 58506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58511da177e4SLinus Torvalds }, 58521da177e4SLinus Torvalds { 585365f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 585465f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 585565f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 585665f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 58576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 585865f5c7c1SYOSHIFUJI Hideaki }, 585965f5c7c1SYOSHIFUJI Hideaki { 58608013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 58618013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 58628013d1d7SHangbin Liu .maxlen = sizeof(int), 58638013d1d7SHangbin Liu .mode = 0644, 58648013d1d7SHangbin Liu .proc_handler = proc_dointvec, 58658013d1d7SHangbin Liu }, 58668013d1d7SHangbin Liu { 5867c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 5868c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 5869c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5870c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 58716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5872c4fd30ebSYOSHIFUJI Hideaki }, 5873930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5874930d6ff2SYOSHIFUJI Hideaki { 5875930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 5876930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 5877930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5878930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 58796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5880930d6ff2SYOSHIFUJI Hideaki }, 588152e16356SYOSHIFUJI Hideaki { 588252e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 588352e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 588452e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 588552e16356SYOSHIFUJI Hideaki .mode = 0644, 58866d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 588752e16356SYOSHIFUJI Hideaki }, 5888fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 588909c884d4SYOSHIFUJI Hideaki { 589009c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 589109c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 589209c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 589309c884d4SYOSHIFUJI Hideaki .mode = 0644, 58946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 589509c884d4SYOSHIFUJI Hideaki }, 589609c884d4SYOSHIFUJI Hideaki #endif 5897930d6ff2SYOSHIFUJI Hideaki #endif 5898c4fd30ebSYOSHIFUJI Hideaki { 5899fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 5900fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 5901fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5902fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 5903c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 5904fbea49e1SYOSHIFUJI Hideaki }, 5905fbea49e1SYOSHIFUJI Hideaki { 59060bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 59070bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 59080bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 59090bcbc926SYOSHIFUJI Hideaki .mode = 0644, 59106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59110bcbc926SYOSHIFUJI Hideaki }, 591295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 591395c385b4SNeil Horman { 591495c385b4SNeil Horman .procname = "optimistic_dad", 591595c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 591695c385b4SNeil Horman .maxlen = sizeof(int), 591795c385b4SNeil Horman .mode = 0644, 59186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 591995c385b4SNeil Horman }, 59207fd2561eSErik Kline { 59217fd2561eSErik Kline .procname = "use_optimistic", 59227fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 59237fd2561eSErik Kline .maxlen = sizeof(int), 59247fd2561eSErik Kline .mode = 0644, 59257fd2561eSErik Kline .proc_handler = proc_dointvec, 59267fd2561eSErik Kline }, 592795c385b4SNeil Horman #endif 59287bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 59297bc570c8SYOSHIFUJI Hideaki { 59307bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 59317bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 59327bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5933a4e6db07SDavid S. Miller .mode = 0444, 59346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59357bc570c8SYOSHIFUJI Hideaki }, 59367bc570c8SYOSHIFUJI Hideaki #endif 59370bcbc926SYOSHIFUJI Hideaki { 5938778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 5939778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 5940778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5941778d80beSYOSHIFUJI Hideaki .mode = 0644, 594256d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 5943778d80beSYOSHIFUJI Hideaki }, 5944778d80beSYOSHIFUJI Hideaki { 59451b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 59461b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 59471b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 59481b34be74SYOSHIFUJI Hideaki .mode = 0644, 59496d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59501b34be74SYOSHIFUJI Hideaki }, 59511b34be74SYOSHIFUJI Hideaki { 5952f7734fdfSOctavian Purdila .procname = "force_tllao", 5953f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 5954f7734fdfSOctavian Purdila .maxlen = sizeof(int), 5955f7734fdfSOctavian Purdila .mode = 0644, 5956f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 5957f7734fdfSOctavian Purdila }, 5958f7734fdfSOctavian Purdila { 59595cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 59605cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 59615cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 59625cb04436SHannes Frederic Sowa .mode = 0644, 59635cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 59645cb04436SHannes Frederic Sowa }, 59655cb04436SHannes Frederic Sowa { 5966b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 5967b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 5968b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 5969b800c3b9SHannes Frederic Sowa .mode = 0644, 5970b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 5971b800c3b9SHannes Frederic Sowa }, 5972b800c3b9SHannes Frederic Sowa { 5973d9333196SBen Greear .procname = "accept_ra_from_local", 5974d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 5975d9333196SBen Greear .maxlen = sizeof(int), 5976d9333196SBen Greear .mode = 0644, 5977d9333196SBen Greear .proc_handler = proc_dointvec, 5978d9333196SBen Greear }, 5979d9333196SBen Greear { 5980c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 5981c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 5982c2943f14SHarout Hedeshian .maxlen = sizeof(int), 5983c2943f14SHarout Hedeshian .mode = 0644, 5984c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 5985c2943f14SHarout Hedeshian }, 5986c2943f14SHarout Hedeshian { 59873d1bec99SHannes Frederic Sowa .procname = "stable_secret", 59883d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 59893d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 59903d1bec99SHannes Frederic Sowa .mode = 0600, 59913d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 59923d1bec99SHannes Frederic Sowa }, 59933d1bec99SHannes Frederic Sowa { 59943985e8a3SErik Kline .procname = "use_oif_addrs_only", 59953985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 59963985e8a3SErik Kline .maxlen = sizeof(int), 59973985e8a3SErik Kline .mode = 0644, 59983985e8a3SErik Kline .proc_handler = proc_dointvec, 599935103d11SAndy Gospodarek }, 600035103d11SAndy Gospodarek { 600135103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 600235103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 600335103d11SAndy Gospodarek .maxlen = sizeof(int), 600435103d11SAndy Gospodarek .mode = 0644, 600535103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 60063985e8a3SErik Kline }, 60073985e8a3SErik Kline { 6008abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6009abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6010abbc3043SJohannes Berg .maxlen = sizeof(int), 6011abbc3043SJohannes Berg .mode = 0644, 6012abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6013abbc3043SJohannes Berg }, 6014abbc3043SJohannes Berg { 60157a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 60167a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 60177a02bf89SJohannes Berg .maxlen = sizeof(int), 60187a02bf89SJohannes Berg .mode = 0644, 60197a02bf89SJohannes Berg .proc_handler = proc_dointvec, 60207a02bf89SJohannes Berg }, 60217a02bf89SJohannes Berg { 6022f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6023f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6024f1705ec1SDavid Ahern .maxlen = sizeof(int), 6025f1705ec1SDavid Ahern .mode = 0644, 6026f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6027f1705ec1SDavid Ahern 6028f1705ec1SDavid Ahern }, 6029f1705ec1SDavid Ahern { 6030f8572d8fSEric W. Biederman /* sentinel */ 60311da177e4SLinus Torvalds } 60321da177e4SLinus Torvalds }; 60331da177e4SLinus Torvalds 6034bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6035f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 60361da177e4SLinus Torvalds { 603729c994e3SNicolas Dichtel int i, ifindex; 6038607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 60396105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 60401dab6222SPavel Emelyanov 6041607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6042607ea7cdSKonstantin Khlebnikov if (!table) 6043f68635e6SPavel Emelyanov goto out; 6044f68635e6SPavel Emelyanov 6045607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6046607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6047607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6048607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 60491da177e4SLinus Torvalds } 60501da177e4SLinus Torvalds 60516105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 60521da177e4SLinus Torvalds 6053607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6054607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 60556105e293SEric W. Biederman goto free; 6056f68635e6SPavel Emelyanov 605729c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 605829c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 605929c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 606029c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 606129c994e3SNicolas Dichtel else 606229c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 606329c994e3SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p); 606495897312SPavel Emelyanov return 0; 60651da177e4SLinus Torvalds 60661da177e4SLinus Torvalds free: 6067607ea7cdSKonstantin Khlebnikov kfree(table); 6068f68635e6SPavel Emelyanov out: 606995897312SPavel Emelyanov return -ENOBUFS; 60701da177e4SLinus Torvalds } 60711da177e4SLinus Torvalds 6072408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 6073f52295a9SPavel Emelyanov { 6074607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6075f52295a9SPavel Emelyanov 6076607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6077408c4768SPavel Emelyanov return; 6078408c4768SPavel Emelyanov 6079607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6080607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6081607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6082607ea7cdSKonstantin Khlebnikov kfree(table); 60831da177e4SLinus Torvalds } 6084408c4768SPavel Emelyanov 6085a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6086408c4768SPavel Emelyanov { 6087a317a2f1SWANG Cong int err; 6088a317a2f1SWANG Cong 6089a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6090a317a2f1SWANG Cong return -EINVAL; 6091a317a2f1SWANG Cong 6092a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6093f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6094a317a2f1SWANG Cong if (err) 6095a317a2f1SWANG Cong return err; 6096a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6097f8572d8fSEric W. Biederman idev, &idev->cnf); 6098a317a2f1SWANG Cong if (err) 6099a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6100a317a2f1SWANG Cong 6101a317a2f1SWANG Cong return err; 6102408c4768SPavel Emelyanov } 6103408c4768SPavel Emelyanov 6104408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6105408c4768SPavel Emelyanov { 6106408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 6107408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 61081da177e4SLinus Torvalds } 61091da177e4SLinus Torvalds 61101da177e4SLinus Torvalds 61111da177e4SLinus Torvalds #endif 61121da177e4SLinus Torvalds 61132c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6114e0da5a48SPavel Emelyanov { 6115a79ca223SHong Zhiguo int err = -ENOMEM; 6116e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6117e0da5a48SPavel Emelyanov 6118a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 611963159f29SIan Morris if (!all) 6120e0da5a48SPavel Emelyanov goto err_alloc_all; 6121e0da5a48SPavel Emelyanov 6122a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 612363159f29SIan Morris if (!dflt) 6124e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6125a79ca223SHong Zhiguo 612656d417b1SBrian Haley /* these will be inherited by all namespaces */ 612756d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 612856d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6129e0da5a48SPavel Emelyanov 61303d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 61313d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 61323d1bec99SHannes Frederic Sowa 6133e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6134e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6135e0da5a48SPavel Emelyanov 6136e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6137f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6138e0da5a48SPavel Emelyanov if (err < 0) 6139e0da5a48SPavel Emelyanov goto err_reg_all; 6140e0da5a48SPavel Emelyanov 6141f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6142e0da5a48SPavel Emelyanov if (err < 0) 6143e0da5a48SPavel Emelyanov goto err_reg_dflt; 6144e0da5a48SPavel Emelyanov #endif 6145e0da5a48SPavel Emelyanov return 0; 6146e0da5a48SPavel Emelyanov 6147e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6148e0da5a48SPavel Emelyanov err_reg_dflt: 6149e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 6150e0da5a48SPavel Emelyanov err_reg_all: 6151e0da5a48SPavel Emelyanov kfree(dflt); 6152e0da5a48SPavel Emelyanov #endif 6153e0da5a48SPavel Emelyanov err_alloc_dflt: 6154e0da5a48SPavel Emelyanov kfree(all); 6155e0da5a48SPavel Emelyanov err_alloc_all: 6156e0da5a48SPavel Emelyanov return err; 6157e0da5a48SPavel Emelyanov } 6158e0da5a48SPavel Emelyanov 61592c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6160e0da5a48SPavel Emelyanov { 6161e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6162e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 6163e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 6164e0da5a48SPavel Emelyanov #endif 6165e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6166e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6167e0da5a48SPavel Emelyanov } 6168e0da5a48SPavel Emelyanov 6169e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6170e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6171e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6172e0da5a48SPavel Emelyanov }; 6173e0da5a48SPavel Emelyanov 6174207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6175b382b191SThomas Graf .family = AF_INET6, 6176b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6177b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 617811b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6179f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6180b382b191SThomas Graf }; 6181b382b191SThomas Graf 61821da177e4SLinus Torvalds /* 61831da177e4SLinus Torvalds * Init / cleanup code 61841da177e4SLinus Torvalds */ 61851da177e4SLinus Torvalds 61861da177e4SLinus Torvalds int __init addrconf_init(void) 61871da177e4SLinus Torvalds { 6188a317a2f1SWANG Cong struct inet6_dev *idev; 6189c2e21293Sstephen hemminger int i, err; 61902a8cc6c8SYOSHIFUJI Hideaki 6191e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6192e21e8467SStephen Hemminger if (err < 0) { 6193f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6194f3213831SJoe Perches __func__, err); 61952cc6d2bfSNeil Horman goto out; 61962a8cc6c8SYOSHIFUJI Hideaki } 61971da177e4SLinus Torvalds 61982cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 61992cc6d2bfSNeil Horman if (err < 0) 62002cc6d2bfSNeil Horman goto out_addrlabel; 6201e0da5a48SPavel Emelyanov 6202c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6203c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6204c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6205c15b1ccaSHannes Frederic Sowa goto out_nowq; 6206c15b1ccaSHannes Frederic Sowa } 6207c15b1ccaSHannes Frederic Sowa 62081da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 62091da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 62101da177e4SLinus Torvalds * before it can bring up and give link-local addresses 62111da177e4SLinus Torvalds * to other devices which are up. 62121da177e4SLinus Torvalds * 62131da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 62141da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 62151da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 62161da177e4SLinus Torvalds * So this causes the notifier registry below to try and 62171da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 62181da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 62191da177e4SLinus Torvalds * devices to fail to get a link-local address. 62201da177e4SLinus Torvalds * 62211da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 62221da177e4SLinus Torvalds * loopback_dev first by hand. 62231da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 62241da177e4SLinus Torvalds * device and it being up should be removed. 62251da177e4SLinus Torvalds */ 62261da177e4SLinus Torvalds rtnl_lock(); 6227a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 62281da177e4SLinus Torvalds rtnl_unlock(); 6229a317a2f1SWANG Cong if (IS_ERR(idev)) { 6230a317a2f1SWANG Cong err = PTR_ERR(idev); 6231e0da5a48SPavel Emelyanov goto errlo; 6232a317a2f1SWANG Cong } 62331da177e4SLinus Torvalds 6234c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6235c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6236c2e21293Sstephen hemminger 62371da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 62381da177e4SLinus Torvalds 6239c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6240c127ea2cSThomas Graf 62413678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6242b382b191SThomas Graf 6243c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 6244c7ac8679SGreg Rose NULL); 6245c127ea2cSThomas Graf if (err < 0) 6246c127ea2cSThomas Graf goto errout; 6247c127ea2cSThomas Graf 6248c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 6249c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 6250c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 6251c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 6252c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 6253c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 6254c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 6255c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 6256c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 625776f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 62587a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 6259c127ea2cSThomas Graf 62602a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 62612a8cc6c8SYOSHIFUJI Hideaki 62621da177e4SLinus Torvalds return 0; 6263c127ea2cSThomas Graf errout: 6264b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6265c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6266e0da5a48SPavel Emelyanov errlo: 6267c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6268c15b1ccaSHannes Frederic Sowa out_nowq: 6269e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 62702cc6d2bfSNeil Horman out_addrlabel: 62712cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 62722cc6d2bfSNeil Horman out: 6273c127ea2cSThomas Graf return err; 62741da177e4SLinus Torvalds } 62751da177e4SLinus Torvalds 627609f7709fSDaniel Lezcano void addrconf_cleanup(void) 62771da177e4SLinus Torvalds { 6278176c39afSDaniel Lezcano struct net_device *dev; 62791da177e4SLinus Torvalds int i; 62801da177e4SLinus Torvalds 62811da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6282e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 62832cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 62841da177e4SLinus Torvalds 62851da177e4SLinus Torvalds rtnl_lock(); 62861da177e4SLinus Torvalds 6287b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 6288b382b191SThomas Graf 6289176c39afSDaniel Lezcano /* clean dev list */ 6290176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6291176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6292176c39afSDaniel Lezcano continue; 6293176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6294176c39afSDaniel Lezcano } 6295176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6296176c39afSDaniel Lezcano 62971da177e4SLinus Torvalds /* 62981da177e4SLinus Torvalds * Check hash table. 62991da177e4SLinus Torvalds */ 63005c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6301c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6302c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 63035c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6304c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 63051da177e4SLinus Torvalds rtnl_unlock(); 6306c15b1ccaSHannes Frederic Sowa 6307c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 63081da177e4SLinus Torvalds } 6309