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 7306a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.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> 841da177e4SLinus Torvalds #include <linux/if_tunnel.h> 851da177e4SLinus Torvalds #include <linux/rtnetlink.h> 86f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 871da177e4SLinus Torvalds #include <linux/random.h> 88e21e8467SStephen Hemminger #include <linux/uaccess.h> 897f7d9a6bSHerbert Xu #include <asm/unaligned.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #include <linux/proc_fs.h> 921da177e4SLinus Torvalds #include <linux/seq_file.h> 93bc3b2d7fSPaul Gortmaker #include <linux/export.h> 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 961da177e4SLinus Torvalds #define ACONF_DEBUG 2 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 99ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 1001da177e4SLinus Torvalds #else 101ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) 1021da177e4SLinus Torvalds #endif 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10518a31e1eSThomas Graf 1063d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 1073d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 1083d1bec99SHannes Frederic Sowa 10918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 11018a31e1eSThomas Graf { 11118a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11218a31e1eSThomas Graf } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 115a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 116408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 117408c4768SPavel Emelyanov #else 118a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 119408c4768SPavel Emelyanov { 120a317a2f1SWANG Cong return 0; 121408c4768SPavel Emelyanov } 122408c4768SPavel Emelyanov 123408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 124408c4768SPavel Emelyanov { 125408c4768SPavel Emelyanov } 1261da177e4SLinus Torvalds #endif 1271da177e4SLinus Torvalds 128eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev); 129eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1301da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1311da177e4SLinus Torvalds 1321b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1331da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 134622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 135622c81d5SHannes Frederic Sowa u8 dad_count, 136622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * Configured unicast address hash table 1401da177e4SLinus Torvalds */ 141c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1425c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1431da177e4SLinus Torvalds 144c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 145c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 146c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1471da177e4SLinus Torvalds 148c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 149c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1521da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1531da177e4SLinus Torvalds 15493d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15575c78500SMoni Shoua unsigned long event); 1561da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1571da177e4SLinus Torvalds 15821caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 15921caa662SRomain Kuntz int plen, 16021caa662SRomain Kuntz const struct net_device *dev, 16121caa662SRomain Kuntz u32 flags, u32 noflags); 16221caa662SRomain Kuntz 163cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 164c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 1651da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 166c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1671da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1681da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1691da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1721da177e4SLinus Torvalds struct prefix_info *pinfo); 1733e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 17406bfe655SDaniel Lezcano struct net_device *dev); 1751da177e4SLinus Torvalds 176888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1771da177e4SLinus Torvalds .forwarding = 0, 1781da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1791da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1801da177e4SLinus Torvalds .accept_ra = 1, 1811da177e4SLinus Torvalds .accept_redirects = 1, 1821da177e4SLinus Torvalds .autoconf = 1, 1831da177e4SLinus Torvalds .force_mld_version = 0, 184fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 185fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 1861da177e4SLinus Torvalds .dad_transmits = 1, 1871da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1881da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1891da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1901da177e4SLinus Torvalds .use_tempaddr = 0, 1911da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1921da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1931da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1941da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1951da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19665f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 197d9333196SBen Greear .accept_ra_from_local = 0, 1988013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 199c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 200930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 201930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 20252e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 20309c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 20409c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 20509c884d4SYOSHIFUJI Hideaki #endif 206930d6ff2SYOSHIFUJI Hideaki #endif 207fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2080bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 209778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2101b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 211b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 212c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2133d1bec99SHannes Frederic Sowa .stable_secret = { 2143d1bec99SHannes Frederic Sowa .initialized = false, 2153985e8a3SErik Kline }, 2163985e8a3SErik Kline .use_oif_addrs_only = 0, 21735103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 2181da177e4SLinus Torvalds }; 2191da177e4SLinus Torvalds 220ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2211da177e4SLinus Torvalds .forwarding = 0, 2221da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2231da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2241da177e4SLinus Torvalds .accept_ra = 1, 2251da177e4SLinus Torvalds .accept_redirects = 1, 2261da177e4SLinus Torvalds .autoconf = 1, 227fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 228fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 229fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2301da177e4SLinus Torvalds .dad_transmits = 1, 2311da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2321da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2331da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2341da177e4SLinus Torvalds .use_tempaddr = 0, 2351da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2361da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2371da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2381da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2391da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 24065f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 241d9333196SBen Greear .accept_ra_from_local = 0, 2428013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 243c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 244930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 245930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 24652e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 24709c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 24809c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 24909c884d4SYOSHIFUJI Hideaki #endif 250930d6ff2SYOSHIFUJI Hideaki #endif 251fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2520bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 253778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2541b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 255b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 256c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2573d1bec99SHannes Frederic Sowa .stable_secret = { 2583d1bec99SHannes Frederic Sowa .initialized = false, 2593d1bec99SHannes Frederic Sowa }, 2603985e8a3SErik Kline .use_oif_addrs_only = 0, 26135103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 2621da177e4SLinus Torvalds }; 2631da177e4SLinus Torvalds 264f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 26505297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 266f24e3d65SMitsuru Chinen { 26705297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 268f24e3d65SMitsuru Chinen } 269f24e3d65SMitsuru Chinen 270b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 2711da177e4SLinus Torvalds { 272b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 273b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 274b7b1bfceSHannes Frederic Sowa } 275b7b1bfceSHannes Frederic Sowa 276c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 277b7b1bfceSHannes Frederic Sowa { 278c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 2791da177e4SLinus Torvalds __in6_ifa_put(ifp); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 282b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 2831da177e4SLinus Torvalds unsigned long when) 2841da177e4SLinus Torvalds { 285b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 286b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 287b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 2881da177e4SLinus Torvalds } 289b7b1bfceSHannes Frederic Sowa 290c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 291c15b1ccaSHannes Frederic Sowa unsigned long delay) 292b7b1bfceSHannes Frederic Sowa { 293c15b1ccaSHannes Frederic Sowa if (!delayed_work_pending(&ifp->dad_work)) 294b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 295c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2987f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2997f7d9a6bSHerbert Xu { 300827da44cSJohn Stultz int i; 301827da44cSJohn Stultz 302698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 303698365faSWANG Cong if (!idev->stats.ipv6) 3047f7d9a6bSHerbert Xu goto err_ip; 305827da44cSJohn Stultz 306827da44cSJohn Stultz for_each_possible_cpu(i) { 307827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 308698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 309827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 310827da44cSJohn Stultz } 311827da44cSJohn Stultz 312827da44cSJohn Stultz 313be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 314be281e55SEric Dumazet GFP_KERNEL); 315be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3167f7d9a6bSHerbert Xu goto err_icmp; 317be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 318be281e55SEric Dumazet GFP_KERNEL); 319be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 32014878f75SDavid L Stevens goto err_icmpmsg; 3217f7d9a6bSHerbert Xu 3227f7d9a6bSHerbert Xu return 0; 3237f7d9a6bSHerbert Xu 32414878f75SDavid L Stevens err_icmpmsg: 325be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3267f7d9a6bSHerbert Xu err_icmp: 327698365faSWANG Cong free_percpu(idev->stats.ipv6); 3287f7d9a6bSHerbert Xu err_ip: 329aaf70ec7SPavel Emelyanov return -ENOMEM; 3307f7d9a6bSHerbert Xu } 3317f7d9a6bSHerbert Xu 3321da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds struct inet6_dev *ndev; 335a317a2f1SWANG Cong int err = -ENOMEM; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds ASSERT_RTNL(); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 340a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3411da177e4SLinus Torvalds 342322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 34363159f29SIan Morris if (!ndev) 344a317a2f1SWANG Cong return ERR_PTR(err); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3471da177e4SLinus Torvalds ndev->dev = dev; 348502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 349b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 350b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 351c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3521da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3531da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3541da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 35563159f29SIan Morris if (!ndev->nd_parms) { 3561da177e4SLinus Torvalds kfree(ndev); 357a317a2f1SWANG Cong return ERR_PTR(err); 3581da177e4SLinus Torvalds } 3590187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3600187bdfbSBen Hutchings dev_disable_lro(dev); 3611da177e4SLinus Torvalds /* We refer to the device */ 3621da177e4SLinus Torvalds dev_hold(dev); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 365ba3542e1Sdingtianhong ADBG(KERN_WARNING 366f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 367ba3542e1Sdingtianhong __func__, dev->name); 3681da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3698603e33dSRoy Li dev_put(dev); 3708603e33dSRoy Li kfree(ndev); 371a317a2f1SWANG Cong return ERR_PTR(err); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 375ba3542e1Sdingtianhong ADBG(KERN_WARNING 376f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 377ba3542e1Sdingtianhong __func__, dev->name); 378a317a2f1SWANG Cong goto err_release; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* One reference from device. We must do this before 3821da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds in6_dev_hold(ndev); 3851da177e4SLinus Torvalds 3861b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 3871b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 3881b34be74SYOSHIFUJI Hideaki 38907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 390c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 391f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 392c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 393c7dc89c0SFred L. Templin } 394b077d7abSYOSHIFUJI Hideaki #endif 395b077d7abSYOSHIFUJI Hideaki 396372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 3971da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 3981da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 3991da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4009625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4011da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4021da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4030be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4041da177e4SLinus Torvalds } else { 4051da177e4SLinus Torvalds in6_dev_hold(ndev); 4061da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4071da177e4SLinus Torvalds } 4085d9efa7eSDavid S. Miller 409914faa14SDaniel Borkmann ndev->token = in6addr_any; 4101da177e4SLinus Torvalds 411f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 41253aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 41353aadcc9SHerbert Xu 4141da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4151da177e4SLinus Torvalds ndev->tstamp = jiffies; 416a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 417a317a2f1SWANG Cong if (err) { 418a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 419a317a2f1SWANG Cong del_timer(&ndev->regen_timer); 420a317a2f1SWANG Cong goto err_release; 421a317a2f1SWANG Cong } 42230c4cf57SDavid L Stevens /* protected by rtnl_lock */ 423cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 424d88ae4ccSYOSHIFUJI Hideaki 4252c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4262c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4272c5e8933SHannes Frederic Sowa 428d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 429f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 430d88ae4ccSYOSHIFUJI Hideaki 431d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4328b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 433d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 434d6ddef9eSLi Wei 4351da177e4SLinus Torvalds return ndev; 436a317a2f1SWANG Cong 437a317a2f1SWANG Cong err_release: 438a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 439a317a2f1SWANG Cong ndev->dead = 1; 440a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 441a317a2f1SWANG Cong return ERR_PTR(err); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 444cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds struct inet6_dev *idev; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds ASSERT_RTNL(); 4491da177e4SLinus Torvalds 450e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 451e21e8467SStephen Hemminger if (!idev) { 452e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 453a317a2f1SWANG Cong if (IS_ERR(idev)) 4541da177e4SLinus Torvalds return NULL; 4551da177e4SLinus Torvalds } 456c5e33bddSYOSHIFUJI Hideaki 4571da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4581da177e4SLinus Torvalds ipv6_mc_up(idev); 4591da177e4SLinus Torvalds return idev; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 462f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 463f3a1bfb1SNicolas Dichtel { 464f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 465f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 466f3a1bfb1SNicolas Dichtel 46776f8f6cbSNicolas Dichtel /* type -1 is used for ALL */ 46876f8f6cbSNicolas Dichtel if (type == -1 || type == NETCONFA_FORWARDING) 469f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 470b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 471d67b8c61SNicolas Dichtel if (type == -1 || type == NETCONFA_MC_FORWARDING) 472d67b8c61SNicolas Dichtel size += nla_total_size(4); 473b1afce95SDavid S. Miller #endif 474c92d5491Sstephen hemminger if (type == -1 || type == NETCONFA_PROXY_NEIGH) 475c92d5491Sstephen hemminger size += nla_total_size(4); 476f3a1bfb1SNicolas Dichtel 47735103d11SAndy Gospodarek if (type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 47835103d11SAndy Gospodarek size += nla_total_size(4); 47935103d11SAndy Gospodarek 480f3a1bfb1SNicolas Dichtel return size; 481f3a1bfb1SNicolas Dichtel } 482f3a1bfb1SNicolas Dichtel 483f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 484f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 485f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 486f3a1bfb1SNicolas Dichtel int type) 487f3a1bfb1SNicolas Dichtel { 488f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 489f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 490f3a1bfb1SNicolas Dichtel 491f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 492f3a1bfb1SNicolas Dichtel flags); 49363159f29SIan Morris if (!nlh) 494f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 495f3a1bfb1SNicolas Dichtel 496f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 497f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 498f3a1bfb1SNicolas Dichtel 499f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 500f3a1bfb1SNicolas Dichtel goto nla_put_failure; 501f3a1bfb1SNicolas Dichtel 50276f8f6cbSNicolas Dichtel /* type -1 is used for ALL */ 50376f8f6cbSNicolas Dichtel if ((type == -1 || type == NETCONFA_FORWARDING) && 504f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 505f3a1bfb1SNicolas Dichtel goto nla_put_failure; 506b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 507d67b8c61SNicolas Dichtel if ((type == -1 || type == NETCONFA_MC_FORWARDING) && 508d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 509d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 510d67b8c61SNicolas Dichtel goto nla_put_failure; 511b1afce95SDavid S. Miller #endif 512c92d5491Sstephen hemminger if ((type == -1 || type == NETCONFA_PROXY_NEIGH) && 513c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 514c92d5491Sstephen hemminger goto nla_put_failure; 515c92d5491Sstephen hemminger 51635103d11SAndy Gospodarek if ((type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 51735103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 51835103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 51935103d11SAndy Gospodarek goto nla_put_failure; 52035103d11SAndy Gospodarek 521053c095aSJohannes Berg nlmsg_end(skb, nlh); 522053c095aSJohannes Berg return 0; 523f3a1bfb1SNicolas Dichtel 524f3a1bfb1SNicolas Dichtel nla_put_failure: 525f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 526f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 527f3a1bfb1SNicolas Dichtel } 528f3a1bfb1SNicolas Dichtel 529d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 530f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf) 531f3a1bfb1SNicolas Dichtel { 532f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 533f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 534f3a1bfb1SNicolas Dichtel 535f3a1bfb1SNicolas Dichtel skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); 53663159f29SIan Morris if (!skb) 537f3a1bfb1SNicolas Dichtel goto errout; 538f3a1bfb1SNicolas Dichtel 539f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 540f3a1bfb1SNicolas Dichtel RTM_NEWNETCONF, 0, type); 541f3a1bfb1SNicolas Dichtel if (err < 0) { 542f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 543f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 544f3a1bfb1SNicolas Dichtel kfree_skb(skb); 545f3a1bfb1SNicolas Dichtel goto errout; 546f3a1bfb1SNicolas Dichtel } 547f3a1bfb1SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); 548f3a1bfb1SNicolas Dichtel return; 549f3a1bfb1SNicolas Dichtel errout: 550f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 551f3a1bfb1SNicolas Dichtel } 552f3a1bfb1SNicolas Dichtel 55376f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 55476f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 55576f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 556c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 55735103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 55876f8f6cbSNicolas Dichtel }; 55976f8f6cbSNicolas Dichtel 56076f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 561661d2967SThomas Graf struct nlmsghdr *nlh) 56276f8f6cbSNicolas Dichtel { 56376f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 56476f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 56576f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 56676f8f6cbSNicolas Dichtel struct sk_buff *skb; 56776f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 56876f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 56976f8f6cbSNicolas Dichtel struct net_device *dev; 57076f8f6cbSNicolas Dichtel int ifindex; 57176f8f6cbSNicolas Dichtel int err; 57276f8f6cbSNicolas Dichtel 57376f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 57476f8f6cbSNicolas Dichtel devconf_ipv6_policy); 57576f8f6cbSNicolas Dichtel if (err < 0) 57676f8f6cbSNicolas Dichtel goto errout; 57776f8f6cbSNicolas Dichtel 57876f8f6cbSNicolas Dichtel err = EINVAL; 57976f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 58076f8f6cbSNicolas Dichtel goto errout; 58176f8f6cbSNicolas Dichtel 58276f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 58376f8f6cbSNicolas Dichtel switch (ifindex) { 58476f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 58576f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 58676f8f6cbSNicolas Dichtel break; 58776f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 58876f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 58976f8f6cbSNicolas Dichtel break; 59076f8f6cbSNicolas Dichtel default: 59176f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 59263159f29SIan Morris if (!dev) 59376f8f6cbSNicolas Dichtel goto errout; 59476f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 59563159f29SIan Morris if (!in6_dev) 59676f8f6cbSNicolas Dichtel goto errout; 59776f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 59876f8f6cbSNicolas Dichtel break; 59976f8f6cbSNicolas Dichtel } 60076f8f6cbSNicolas Dichtel 60176f8f6cbSNicolas Dichtel err = -ENOBUFS; 60276f8f6cbSNicolas Dichtel skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC); 60363159f29SIan Morris if (!skb) 60476f8f6cbSNicolas Dichtel goto errout; 60576f8f6cbSNicolas Dichtel 60676f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 60776f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 60876f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 60976f8f6cbSNicolas Dichtel -1); 61076f8f6cbSNicolas Dichtel if (err < 0) { 61176f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 61276f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 61376f8f6cbSNicolas Dichtel kfree_skb(skb); 61476f8f6cbSNicolas Dichtel goto errout; 61576f8f6cbSNicolas Dichtel } 61676f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 61776f8f6cbSNicolas Dichtel errout: 61876f8f6cbSNicolas Dichtel return err; 61976f8f6cbSNicolas Dichtel } 62076f8f6cbSNicolas Dichtel 6217a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6227a674200SNicolas Dichtel struct netlink_callback *cb) 6237a674200SNicolas Dichtel { 6247a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6257a674200SNicolas Dichtel int h, s_h; 6267a674200SNicolas Dichtel int idx, s_idx; 6277a674200SNicolas Dichtel struct net_device *dev; 6287a674200SNicolas Dichtel struct inet6_dev *idev; 6297a674200SNicolas Dichtel struct hlist_head *head; 6307a674200SNicolas Dichtel 6317a674200SNicolas Dichtel s_h = cb->args[0]; 6327a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6337a674200SNicolas Dichtel 6347a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6357a674200SNicolas Dichtel idx = 0; 6367a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6377a674200SNicolas Dichtel rcu_read_lock(); 63863998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 63963998ac2SNicolas Dichtel net->dev_base_seq; 6407a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6417a674200SNicolas Dichtel if (idx < s_idx) 6427a674200SNicolas Dichtel goto cont; 6437a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6447a674200SNicolas Dichtel if (!idev) 6457a674200SNicolas Dichtel goto cont; 6467a674200SNicolas Dichtel 6477a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6487a674200SNicolas Dichtel &idev->cnf, 6497a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6507a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6517a674200SNicolas Dichtel RTM_NEWNETCONF, 6527a674200SNicolas Dichtel NLM_F_MULTI, 653053c095aSJohannes Berg -1) < 0) { 6547a674200SNicolas Dichtel rcu_read_unlock(); 6557a674200SNicolas Dichtel goto done; 6567a674200SNicolas Dichtel } 65763998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 6587a674200SNicolas Dichtel cont: 6597a674200SNicolas Dichtel idx++; 6607a674200SNicolas Dichtel } 6617a674200SNicolas Dichtel rcu_read_unlock(); 6627a674200SNicolas Dichtel } 6637a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 6647a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 6657a674200SNicolas Dichtel net->ipv6.devconf_all, 6667a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6677a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6687a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 669053c095aSJohannes Berg -1) < 0) 6707a674200SNicolas Dichtel goto done; 6717a674200SNicolas Dichtel else 6727a674200SNicolas Dichtel h++; 6737a674200SNicolas Dichtel } 6747a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 6757a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 6767a674200SNicolas Dichtel net->ipv6.devconf_dflt, 6777a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6787a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6797a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 680053c095aSJohannes Berg -1) < 0) 6817a674200SNicolas Dichtel goto done; 6827a674200SNicolas Dichtel else 6837a674200SNicolas Dichtel h++; 6847a674200SNicolas Dichtel } 6857a674200SNicolas Dichtel done: 6867a674200SNicolas Dichtel cb->args[0] = h; 6877a674200SNicolas Dichtel cb->args[1] = idx; 6887a674200SNicolas Dichtel 6897a674200SNicolas Dichtel return skb->len; 6907a674200SNicolas Dichtel } 6917a674200SNicolas Dichtel 6921da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 6931da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds struct net_device *dev; 6961da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds if (!idev) 6991da177e4SLinus Torvalds return; 7001da177e4SLinus Torvalds dev = idev->dev; 7010187bdfbSBen Hutchings if (idev->cnf.forwarding) 7020187bdfbSBen Hutchings dev_disable_lro(dev); 7031a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7042c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 705f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7062c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7072c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7082c5e8933SHannes Frederic Sowa } else { 709f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7102c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7112c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7122c5e8933SHannes Frederic Sowa } 7131da177e4SLinus Torvalds } 714502a2ffdSstephen hemminger 715502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7162c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7172c12a74cSMichal Wrobel continue; 7181da177e4SLinus Torvalds if (idev->cnf.forwarding) 7191da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7201da177e4SLinus Torvalds else 7211da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7221da177e4SLinus Torvalds } 723f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 724f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds 728e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct net_device *dev; 7311da177e4SLinus Torvalds struct inet6_dev *idev; 7321da177e4SLinus Torvalds 7334acd4945SBen Hutchings for_each_netdev(net, dev) { 7341da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7351da177e4SLinus Torvalds if (idev) { 736e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 737e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7381da177e4SLinus Torvalds if (changed) 7391da177e4SLinus Torvalds dev_forward_change(idev); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds } 743c8fecf22SPavel Emelyanov 744013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 745c8fecf22SPavel Emelyanov { 746bff16c2fSPavel Emelyanov struct net *net; 747013d97e9SFrancesco Ruggeri int old; 748013d97e9SFrancesco Ruggeri 749013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 750013d97e9SFrancesco Ruggeri return restart_syscall(); 751bff16c2fSPavel Emelyanov 752bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 753013d97e9SFrancesco Ruggeri old = *p; 754013d97e9SFrancesco Ruggeri *p = newf; 755c8fecf22SPavel Emelyanov 756013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 757f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 758f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 759f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 760f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 761013d97e9SFrancesco Ruggeri rtnl_unlock(); 762013d97e9SFrancesco Ruggeri return 0; 76388af182eSEric W. Biederman } 764b325fddbSStephen Hemminger 765e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 766e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 767e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 768f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 769f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 770f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 771f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 772013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 773c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 7740187bdfbSBen Hutchings rtnl_unlock(); 775c8fecf22SPavel Emelyanov 776013d97e9SFrancesco Ruggeri if (newf) 7777b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 778b325fddbSStephen Hemminger return 1; 779c8fecf22SPavel Emelyanov } 78035103d11SAndy Gospodarek 78135103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 78235103d11SAndy Gospodarek { 78335103d11SAndy Gospodarek struct net_device *dev; 78435103d11SAndy Gospodarek struct inet6_dev *idev; 78535103d11SAndy Gospodarek 78635103d11SAndy Gospodarek for_each_netdev(net, dev) { 78735103d11SAndy Gospodarek idev = __in6_dev_get(dev); 78835103d11SAndy Gospodarek if (idev) { 78935103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 79035103d11SAndy Gospodarek 79135103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 79235103d11SAndy Gospodarek if (changed) 79335103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 79435103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 79535103d11SAndy Gospodarek dev->ifindex, 79635103d11SAndy Gospodarek &idev->cnf); 79735103d11SAndy Gospodarek } 79835103d11SAndy Gospodarek } 79935103d11SAndy Gospodarek } 80035103d11SAndy Gospodarek 80135103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 80235103d11SAndy Gospodarek { 80335103d11SAndy Gospodarek struct net *net; 80435103d11SAndy Gospodarek int old; 80535103d11SAndy Gospodarek 80635103d11SAndy Gospodarek if (!rtnl_trylock()) 80735103d11SAndy Gospodarek return restart_syscall(); 80835103d11SAndy Gospodarek 80935103d11SAndy Gospodarek net = (struct net *)table->extra2; 81035103d11SAndy Gospodarek old = *p; 81135103d11SAndy Gospodarek *p = newf; 81235103d11SAndy Gospodarek 81335103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 81435103d11SAndy Gospodarek if ((!newf) ^ (!old)) 81535103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 81635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 81735103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 81835103d11SAndy Gospodarek net->ipv6.devconf_dflt); 81935103d11SAndy Gospodarek rtnl_unlock(); 82035103d11SAndy Gospodarek return 0; 82135103d11SAndy Gospodarek } 82235103d11SAndy Gospodarek 82335103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 82435103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 82535103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 82635103d11SAndy Gospodarek if ((!newf) ^ (!old)) 82735103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 82835103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 82935103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 83035103d11SAndy Gospodarek net->ipv6.devconf_all); 83135103d11SAndy Gospodarek } 83235103d11SAndy Gospodarek rtnl_unlock(); 83335103d11SAndy Gospodarek 83435103d11SAndy Gospodarek return 1; 83535103d11SAndy Gospodarek } 83635103d11SAndy Gospodarek 8371da177e4SLinus Torvalds #endif 8381da177e4SLinus Torvalds 8395c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8401da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 8411da177e4SLinus Torvalds { 842c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 843547b792cSIlpo Järvinen 8441da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 84591df42beSJoe Perches pr_debug("%s\n", __func__); 8461da177e4SLinus Torvalds #endif 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds in6_dev_put(ifp->idev); 8491da177e4SLinus Torvalds 850c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 851c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 852c15b1ccaSHannes Frederic Sowa ifp); 8531da177e4SLinus Torvalds 854e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 855f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 8561da177e4SLinus Torvalds return; 8571da177e4SLinus Torvalds } 85894e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 8591da177e4SLinus Torvalds 860e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 863e55ffac6SBrian Haley static void 864e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 865e55ffac6SBrian Haley { 866502a2ffdSstephen hemminger struct list_head *p; 8678a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 868e55ffac6SBrian Haley 869e55ffac6SBrian Haley /* 870e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 871e55ffac6SBrian Haley * global before linklocal. 872e55ffac6SBrian Haley */ 873502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 874502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 875502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 8768a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 877e55ffac6SBrian Haley break; 878e55ffac6SBrian Haley } 879e55ffac6SBrian Haley 880b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 881e55ffac6SBrian Haley } 882e55ffac6SBrian Haley 883ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 8843eb84f49SYOSHIFUJI Hideaki { 885ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 8863eb84f49SYOSHIFUJI Hideaki } 8873eb84f49SYOSHIFUJI Hideaki 8881da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds static struct inet6_ifaddr * 8913f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 8923f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 8938a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 8961da177e4SLinus Torvalds struct rt6_info *rt; 8973a88a81dSstephen hemminger unsigned int hash; 8981da177e4SLinus Torvalds int err = 0; 899d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 900d68b8270SYOSHIFUJI Hideaki 901d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 902d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 903d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 904d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 905d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9061da177e4SLinus Torvalds 9078814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9081da177e4SLinus Torvalds if (idev->dead) { 9091da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9101da177e4SLinus Torvalds goto out2; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 91356d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9149bdd8d40SBrian Haley err = -EACCES; 9159bdd8d40SBrian Haley goto out2; 9169bdd8d40SBrian Haley } 9179bdd8d40SBrian Haley 9185c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 921c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 922ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 9231da177e4SLinus Torvalds err = -EEXIST; 9241da177e4SLinus Torvalds goto out; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 927322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 9281da177e4SLinus Torvalds 92963159f29SIan Morris if (!ifa) { 930ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 9311da177e4SLinus Torvalds err = -ENOBUFS; 9321da177e4SLinus Torvalds goto out; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 9358f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 9361da177e4SLinus Torvalds if (IS_ERR(rt)) { 9371da177e4SLinus Torvalds err = PTR_ERR(rt); 9381da177e4SLinus Torvalds goto out; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 941bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 942bba24896SJiri Pirko 9434e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 9443f8f5298SJiri Pirko if (peer_addr) 9453f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 948c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 949c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 9501da177e4SLinus Torvalds ifa->scope = scope; 9511da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 9521da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 9538a226b2cSJiri Benc ifa->valid_lft = valid_lft; 9548a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 9551da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 956617fe29dSDaniel Borkmann ifa->tokenized = false; 9571da177e4SLinus Torvalds 95857f5f544SKeir Fraser ifa->rt = rt; 95957f5f544SKeir Fraser 9601da177e4SLinus Torvalds ifa->idev = idev; 9611da177e4SLinus Torvalds in6_dev_hold(idev); 9621da177e4SLinus Torvalds /* For caller */ 9631da177e4SLinus Torvalds in6_ifa_hold(ifa); 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds /* Add to big hash table */ 966ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 9671da177e4SLinus Torvalds 9685c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 9695c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds write_lock(&idev->lock); 9721da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 973e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 976372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 9771da177e4SLinus Torvalds in6_ifa_hold(ifa); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds in6_ifa_hold(ifa); 9811da177e4SLinus Torvalds write_unlock(&idev->lock); 9821da177e4SLinus Torvalds out2: 9838814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 9841da177e4SLinus Torvalds 985fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 986f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 9871da177e4SLinus Torvalds else { 9881da177e4SLinus Torvalds kfree(ifa); 9891da177e4SLinus Torvalds ifa = ERR_PTR(err); 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds return ifa; 9931da177e4SLinus Torvalds out: 9945c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9951da177e4SLinus Torvalds goto out2; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9985b84efecSThomas Haller enum cleanup_prefix_rt_t { 9995b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10005b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10015b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10025b84efecSThomas Haller }; 10035b84efecSThomas Haller 10045b84efecSThomas Haller /* 10055b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10065b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10075b84efecSThomas Haller * constants. 10085b84efecSThomas Haller * 10095b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 10105b84efecSThomas Haller * prefix is managed by its own lifetime. 10115b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 10125b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 10135b84efecSThomas Haller * 4) if there are still other permanent address(es), 10145b84efecSThomas Haller * corresponding prefix is still permanent. 10155b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 10165b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 10175b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 10185b84efecSThomas Haller * longest valid lifetime among the corresponding 10195b84efecSThomas Haller * addresses on the device. 10205b84efecSThomas Haller * Note: subsequent RA will update lifetime. 10215b84efecSThomas Haller **/ 10225b84efecSThomas Haller static enum cleanup_prefix_rt_t 10235b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 10245b84efecSThomas Haller { 10255b84efecSThomas Haller struct inet6_ifaddr *ifa; 10265b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 10275b84efecSThomas Haller unsigned long lifetime; 10285b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 10295b84efecSThomas Haller 10305b84efecSThomas Haller *expires = jiffies; 10315b84efecSThomas Haller 10325b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 10335b84efecSThomas Haller if (ifa == ifp) 10345b84efecSThomas Haller continue; 10355b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 10365b84efecSThomas Haller ifp->prefix_len)) 10375b84efecSThomas Haller continue; 10385b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 10395b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 10405b84efecSThomas Haller 10415b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 10425b84efecSThomas Haller 10435b84efecSThomas Haller spin_lock(&ifa->lock); 10445b84efecSThomas Haller 10455b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 10465b84efecSThomas Haller /* 10475b84efecSThomas Haller * Note: Because this address is 10485b84efecSThomas Haller * not permanent, lifetime < 10495b84efecSThomas Haller * LONG_MAX / HZ here. 10505b84efecSThomas Haller */ 10515b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 10525b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 10535b84efecSThomas Haller spin_unlock(&ifa->lock); 10545b84efecSThomas Haller } 10555b84efecSThomas Haller 10565b84efecSThomas Haller return action; 10575b84efecSThomas Haller } 10585b84efecSThomas Haller 10595b84efecSThomas Haller static void 10605b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 10615b84efecSThomas Haller { 10625b84efecSThomas Haller struct rt6_info *rt; 10635b84efecSThomas Haller 10645b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 10655b84efecSThomas Haller ifp->prefix_len, 10665b84efecSThomas Haller ifp->idev->dev, 10675b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 10685b84efecSThomas Haller if (rt) { 10695b84efecSThomas Haller if (del_rt) 10705b84efecSThomas Haller ip6_del_rt(rt); 10715b84efecSThomas Haller else { 10725b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 10735b84efecSThomas Haller rt6_set_expires(rt, expires); 10745b84efecSThomas Haller ip6_rt_put(rt); 10755b84efecSThomas Haller } 10765b84efecSThomas Haller } 10775b84efecSThomas Haller } 10785b84efecSThomas Haller 10795b84efecSThomas Haller 10801da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 10831da177e4SLinus Torvalds { 10844c5ff6a6SHerbert Xu int state; 10855b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 10865b84efecSThomas Haller unsigned long expires; 10871da177e4SLinus Torvalds 1088c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1089c15b1ccaSHannes Frederic Sowa 10908e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 10914c5ff6a6SHerbert Xu state = ifp->state; 1092e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 10938e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 10944c5ff6a6SHerbert Xu 10954c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 10964c5ff6a6SHerbert Xu goto out; 10971da177e4SLinus Torvalds 10985c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 10995c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11005c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11011da177e4SLinus Torvalds 11025b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11035d9efa7eSDavid S. Miller 11041da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1105372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11061da177e4SLinus Torvalds if (ifp->ifpub) { 11071da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11081da177e4SLinus Torvalds ifp->ifpub = NULL; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds __in6_ifa_put(ifp); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11135b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 11145b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 11155b84efecSThomas Haller 1116502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 11171da177e4SLinus Torvalds __in6_ifa_put(ifp); 1118502a2ffdSstephen hemminger 11195b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 11201da177e4SLinus Torvalds 1121c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1122b2238566SAndrey Vagin 11231da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 11241da177e4SLinus Torvalds 1125f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 11261da177e4SLinus Torvalds 11275b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 11285b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 11295b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 1132c3968a85SDaniel Walter /* clean up prefsrc entries */ 1133c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 11344c5ff6a6SHerbert Xu out: 11351da177e4SLinus Torvalds in6_ifa_put(ifp); 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 11391da177e4SLinus Torvalds { 11401da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 11411da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 114276f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1143eac55bf9SBenoit Boissinot unsigned long regen_advance; 11441da177e4SLinus Torvalds int tmp_plen; 11451da177e4SLinus Torvalds int ret = 0; 114695c385b4SNeil Horman u32 addr_flags; 114776f793e3SLorenzo Colitti unsigned long now = jiffies; 11481da177e4SLinus Torvalds 114953bd6749SJiri Pirko write_lock_bh(&idev->lock); 11501da177e4SLinus Torvalds if (ift) { 11511da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 11521da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 11531da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 11541da177e4SLinus Torvalds tmpaddr = &addr; 11551da177e4SLinus Torvalds } else { 11561da177e4SLinus Torvalds tmpaddr = NULL; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds retry: 11591da177e4SLinus Torvalds in6_dev_hold(idev); 11601da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 116153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1162f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 11631da177e4SLinus Torvalds in6_dev_put(idev); 11641da177e4SLinus Torvalds ret = -1; 11651da177e4SLinus Torvalds goto out; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 11681da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 11691da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 11701da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 117153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1172f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1173f3213831SJoe Perches __func__); 11741da177e4SLinus Torvalds in6_dev_put(idev); 11751da177e4SLinus Torvalds ret = -1; 11761da177e4SLinus Torvalds goto out; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds in6_ifa_hold(ifp); 11791da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1180eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 11811da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 118276f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 11831da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 11841da177e4SLinus Torvalds ifp->valid_lft, 11857a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 11861da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 11871da177e4SLinus Torvalds ifp->prefered_lft, 11887a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 1189784e2710SBen Hutchings idev->cnf.max_desync_factor); 11901da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 11911da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 11921da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 11931da177e4SLinus Torvalds 1194eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1195eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 11961f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 119753bd6749SJiri Pirko write_unlock_bh(&idev->lock); 119895c385b4SNeil Horman 1199eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1200eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1201eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1202eac55bf9SBenoit Boissinot * Preferred Lifetime. 1203ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1204ecab6701SHeiner Kallweit * temporary addresses being generated. 1205eac55bf9SBenoit Boissinot */ 1206ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1207ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1208eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1209eac55bf9SBenoit Boissinot in6_dev_put(idev); 1210eac55bf9SBenoit Boissinot ret = -1; 1211eac55bf9SBenoit Boissinot goto out; 1212eac55bf9SBenoit Boissinot } 1213eac55bf9SBenoit Boissinot 121495c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 121595c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 121695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 121795c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 121895c385b4SNeil Horman 12194b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 12208a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 12214b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 12224b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 12231da177e4SLinus Torvalds in6_ifa_put(ifp); 12241da177e4SLinus Torvalds in6_dev_put(idev); 1225f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 12261da177e4SLinus Torvalds tmpaddr = &addr; 122753bd6749SJiri Pirko write_lock_bh(&idev->lock); 12281da177e4SLinus Torvalds goto retry; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12321da177e4SLinus Torvalds ift->ifpub = ifp; 123376f793e3SLorenzo Colitti ift->cstamp = now; 12341da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 12351da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12361da177e4SLinus Torvalds 1237cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 12381da177e4SLinus Torvalds in6_ifa_put(ift); 12391da177e4SLinus Torvalds in6_dev_put(idev); 12401da177e4SLinus Torvalds out: 12411da177e4SLinus Torvalds return ret; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* 1245072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 12461da177e4SLinus Torvalds */ 1247a9b05723SYOSHIFUJI Hideaki enum { 1248a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1249a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1250a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1251a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1252a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1253a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1254a9b05723SYOSHIFUJI Hideaki #endif 1255a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1256a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1257a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1258a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1259a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 12607fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 12617fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 12627fd2561eSErik Kline #endif 1263a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1264072047e4SYOSHIFUJI Hideaki }; 1265072047e4SYOSHIFUJI Hideaki 1266a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1267a9b05723SYOSHIFUJI Hideaki int rule; 1268a9b05723SYOSHIFUJI Hideaki int addr_type; 1269a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1270a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1271a9b05723SYOSHIFUJI Hideaki int scopedist; 1272a9b05723SYOSHIFUJI Hideaki int matchlen; 1273a9b05723SYOSHIFUJI Hideaki }; 1274a9b05723SYOSHIFUJI Hideaki 1275a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 12769acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1277a9b05723SYOSHIFUJI Hideaki int ifindex; 1278a9b05723SYOSHIFUJI Hideaki int scope; 1279a9b05723SYOSHIFUJI Hideaki int label; 12807cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1281a9b05723SYOSHIFUJI Hideaki }; 1282072047e4SYOSHIFUJI Hideaki 1283b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 12841da177e4SLinus Torvalds { 128545bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1286072047e4SYOSHIFUJI Hideaki return 1; 1287072047e4SYOSHIFUJI Hideaki return 0; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12907fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 12917fd2561eSErik Kline { 12927fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 12937fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 12947fd2561eSErik Kline #else 12957fd2561eSErik Kline return false; 12967fd2561eSErik Kline #endif 12977fd2561eSErik Kline } 12987fd2561eSErik Kline 12993de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 13003de23255SBenjamin Thery struct ipv6_saddr_score *score, 1301a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1302a9b05723SYOSHIFUJI Hideaki int i) 1303a9b05723SYOSHIFUJI Hideaki { 1304a9b05723SYOSHIFUJI Hideaki int ret; 1305a9b05723SYOSHIFUJI Hideaki 1306a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1307a9b05723SYOSHIFUJI Hideaki switch (i) { 1308a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1309a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1310a9b05723SYOSHIFUJI Hideaki break; 1311a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1312a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1313a9b05723SYOSHIFUJI Hideaki break; 1314a9b05723SYOSHIFUJI Hideaki default: 1315a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1316a9b05723SYOSHIFUJI Hideaki } 1317a9b05723SYOSHIFUJI Hideaki goto out; 1318a9b05723SYOSHIFUJI Hideaki } 1319a9b05723SYOSHIFUJI Hideaki 1320a9b05723SYOSHIFUJI Hideaki switch (i) { 1321a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1322a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1323a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1324a9b05723SYOSHIFUJI Hideaki break; 1325a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1326a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1327a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1328a9b05723SYOSHIFUJI Hideaki break; 1329a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1330a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1331a9b05723SYOSHIFUJI Hideaki * 1332a9b05723SYOSHIFUJI Hideaki * ret 1333a9b05723SYOSHIFUJI Hideaki * ^ 1334a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1335a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1336a9b05723SYOSHIFUJI Hideaki * | 1337a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1338a9b05723SYOSHIFUJI Hideaki * B-d | \ 1339a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1340db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1341a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1342a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1343a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1344a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1345a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1346a9b05723SYOSHIFUJI Hideaki * 1347a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1348a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1349a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1350a9b05723SYOSHIFUJI Hideaki */ 1351a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1352a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1353a9b05723SYOSHIFUJI Hideaki ret = -ret; 1354a9b05723SYOSHIFUJI Hideaki else 1355a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1356a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1357a9b05723SYOSHIFUJI Hideaki break; 1358a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 13597fd2561eSErik Kline { 1360a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 13617fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 13627fd2561eSErik Kline 13637fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 13647fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1365a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 13667fd2561eSErik Kline !(score->ifa->flags & avoid); 1367a9b05723SYOSHIFUJI Hideaki break; 13687fd2561eSErik Kline } 1369a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1370a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 13717cbca67cSYOSHIFUJI Hideaki { 1372a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 13737cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 13747cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1375a9b05723SYOSHIFUJI Hideaki break; 13767cbca67cSYOSHIFUJI Hideaki } 1377a9b05723SYOSHIFUJI Hideaki #endif 1378a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1379a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1380a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1381a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1382a9b05723SYOSHIFUJI Hideaki break; 1383a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1384a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 13853de23255SBenjamin Thery ret = ipv6_addr_label(net, 13863de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1387a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1388a9b05723SYOSHIFUJI Hideaki break; 1389a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 13907cbca67cSYOSHIFUJI Hideaki { 1391a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 139225985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1393a9b05723SYOSHIFUJI Hideaki */ 13947cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 13957cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 13967cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 13977cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1398a9b05723SYOSHIFUJI Hideaki break; 13997cbca67cSYOSHIFUJI Hideaki } 1400a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1401a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1402a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1403a9b05723SYOSHIFUJI Hideaki */ 1404a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1405a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1406a9b05723SYOSHIFUJI Hideaki break; 1407a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1408a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 140991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 141091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 141191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 141291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1413a9b05723SYOSHIFUJI Hideaki break; 14147fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14157fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 14167fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 14177fd2561eSErik Kline * preferred addresses. 14187fd2561eSErik Kline */ 14197fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 14207fd2561eSErik Kline break; 14217fd2561eSErik Kline #endif 1422a9b05723SYOSHIFUJI Hideaki default: 1423a9b05723SYOSHIFUJI Hideaki ret = 0; 1424a9b05723SYOSHIFUJI Hideaki } 1425a9b05723SYOSHIFUJI Hideaki 1426a9b05723SYOSHIFUJI Hideaki if (ret) 1427a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1428a9b05723SYOSHIFUJI Hideaki score->rule = i; 1429a9b05723SYOSHIFUJI Hideaki out: 1430a9b05723SYOSHIFUJI Hideaki return ret; 1431a9b05723SYOSHIFUJI Hideaki } 1432a9b05723SYOSHIFUJI Hideaki 1433c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 14349131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 14359131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1436c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1437c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 14381da177e4SLinus Torvalds { 1439c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1440072047e4SYOSHIFUJI Hideaki 14411da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1442502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1443a9b05723SYOSHIFUJI Hideaki int i; 1444072047e4SYOSHIFUJI Hideaki 1445a9b05723SYOSHIFUJI Hideaki /* 14466b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 14476b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 14486b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 144995c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 14506b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1451072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1452072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1453072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1454072047e4SYOSHIFUJI Hideaki */ 1455a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1456a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 14576b3ae80aSYOSHIFUJI Hideaki continue; 1458a9b05723SYOSHIFUJI Hideaki 1459a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1460a9b05723SYOSHIFUJI Hideaki 1461a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1462a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1463ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 14649131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 14651da177e4SLinus Torvalds continue; 1466072047e4SYOSHIFUJI Hideaki } 1467072047e4SYOSHIFUJI Hideaki 1468a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1469a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1470072047e4SYOSHIFUJI Hideaki 1471a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1472a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1473072047e4SYOSHIFUJI Hideaki 14749131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 14759131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1476072047e4SYOSHIFUJI Hideaki 1477a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1478a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1479a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1480a9b05723SYOSHIFUJI Hideaki /* 1481a9b05723SYOSHIFUJI Hideaki * special case: 1482a9b05723SYOSHIFUJI Hideaki * each remaining entry 1483a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1484a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1485a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1486a9b05723SYOSHIFUJI Hideaki * values. 1487072047e4SYOSHIFUJI Hideaki */ 14889131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1489072047e4SYOSHIFUJI Hideaki } 1490a9b05723SYOSHIFUJI Hideaki break; 1491a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1492a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1493a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 149499cd07a5SJuha-Matti Tapio 1495a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1496072047e4SYOSHIFUJI Hideaki 1497a0bffffcSIlpo Järvinen swap(hiscore, score); 1498c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1499a9b05723SYOSHIFUJI Hideaki 1500a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1501a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1502a9b05723SYOSHIFUJI Hideaki 1503a9b05723SYOSHIFUJI Hideaki break; 15041da177e4SLinus Torvalds } 1505a9b05723SYOSHIFUJI Hideaki } 1506a9b05723SYOSHIFUJI Hideaki } 15079131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 15081da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1509c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 15101da177e4SLinus Torvalds } 15119131f3deSYOSHIFUJI Hideaki/吉藤英明 15129131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 15139131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 15149131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 15159131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1516c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 15179131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 15189131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 15199131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 15209131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 15219131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1522c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 15239131f3deSYOSHIFUJI Hideaki/吉藤英明 15249131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 15259131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 15269131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 15279131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 15289131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 15299131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 15309131f3deSYOSHIFUJI Hideaki/吉藤英明 1531c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1532c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 15339131f3deSYOSHIFUJI Hideaki/吉藤英明 15349131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 15359131f3deSYOSHIFUJI Hideaki/吉藤英明 15369131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 15379131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 15389131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 15399131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15409131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 15419131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 15429131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 15439131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 15449131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15459131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 15469131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 15473985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 15483985e8a3SErik Kline * be the set of unicast addresses assigned to the 15493985e8a3SErik Kline * interface that will be used to send to the destination 15503985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 15519131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 15529131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 15539131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 15543985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 15553985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 15563985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 15579131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 15589131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15599131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15609131f3deSYOSHIFUJI Hideaki/吉藤英明 15619131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1562c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1563c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 15649131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 15659131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 15669131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 15679131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 15689131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1569c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 15709131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15719131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15728814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 15731da177e4SLinus Torvalds 1574c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1575a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1576072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 15771da177e4SLinus Torvalds 15784e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1579a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1580072047e4SYOSHIFUJI Hideaki return 0; 15811da177e4SLinus Torvalds } 15825e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 15831da177e4SLinus Torvalds 15848965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1585479840ffSJiri Pirko u32 banned_flags) 1586b7b1bfceSHannes Frederic Sowa { 1587b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1588b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1589b7b1bfceSHannes Frederic Sowa 1590602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1591602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1592602582caSHannes Frederic Sowa break; 1593b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1594b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1595b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1596b7b1bfceSHannes Frederic Sowa err = 0; 1597b7b1bfceSHannes Frederic Sowa break; 1598b7b1bfceSHannes Frederic Sowa } 1599b7b1bfceSHannes Frederic Sowa } 1600b7b1bfceSHannes Frederic Sowa return err; 1601b7b1bfceSHannes Frederic Sowa } 1602b7b1bfceSHannes Frederic Sowa 160395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1604479840ffSJiri Pirko u32 banned_flags) 16051da177e4SLinus Torvalds { 16061da177e4SLinus Torvalds struct inet6_dev *idev; 16071da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 16081da177e4SLinus Torvalds 16098814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1610e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1611e21e8467SStephen Hemminger if (idev) { 16121da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1613b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 16141da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16151da177e4SLinus Torvalds } 16168814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16171da177e4SLinus Torvalds return err; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 16211da177e4SLinus Torvalds { 16221da177e4SLinus Torvalds int cnt = 0; 16231da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1626502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 16271da177e4SLinus Torvalds cnt++; 16281da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16291da177e4SLinus Torvalds return cnt; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 1632b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 16332a7851bfSFlorian Westphal const struct net_device *dev, int strict) 16341da177e4SLinus Torvalds { 1635c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1636c58da4c6SErik Kline } 1637c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1638c58da4c6SErik Kline 1639c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1640c58da4c6SErik Kline const struct net_device *dev, int strict, 1641c58da4c6SErik Kline u32 banned_flags) 1642c58da4c6SErik Kline { 1643eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1644ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1645c58da4c6SErik Kline u32 ifp_flags; 16461da177e4SLinus Torvalds 16475c578aedSstephen hemminger rcu_read_lock_bh(); 1648b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1649878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1650bfeade08SDaniel Lezcano continue; 1651c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1652c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1653c58da4c6SErik Kline */ 1654c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1655c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1656c58da4c6SErik Kline : ifp->flags; 16571da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1658c58da4c6SErik Kline !(ifp_flags&banned_flags) && 165963159f29SIan Morris (!dev || ifp->idev->dev == dev || 1660eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 16615c578aedSstephen hemminger rcu_read_unlock_bh(); 1662eedf042aSStephen Hemminger return 1; 1663eedf042aSStephen Hemminger } 1664eedf042aSStephen Hemminger } 16655c578aedSstephen hemminger 1666eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1667eedf042aSStephen Hemminger return 0; 16681da177e4SLinus Torvalds } 1669c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 16707159039aSYOSHIFUJI Hideaki 16713e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 167206bfe655SDaniel Lezcano struct net_device *dev) 16731da177e4SLinus Torvalds { 1674ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 16751da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16761da177e4SLinus Torvalds 1677b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1678878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 167906bfe655SDaniel Lezcano continue; 16801da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 168163159f29SIan Morris if (!dev || ifp->idev->dev == dev) 16823e81c6daSDavid S. Miller return true; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds } 16853e81c6daSDavid S. Miller return false; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds 16887df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 16897df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 16907df37ff3SCatalin\(ux\) M. BOIE */ 16917df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 16927df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 16937df37ff3SCatalin\(ux\) M. BOIE { 16947df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 16957df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 16967df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 16977df37ff3SCatalin\(ux\) M. BOIE 16987df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 16997df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 17007df37ff3SCatalin\(ux\) M. BOIE if (idev) { 17017df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 17027df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 17037df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 17047df37ff3SCatalin\(ux\) M. BOIE if (ret) 17057df37ff3SCatalin\(ux\) M. BOIE break; 17067df37ff3SCatalin\(ux\) M. BOIE } 17077df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 17087df37ff3SCatalin\(ux\) M. BOIE } 17097df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 17107df37ff3SCatalin\(ux\) M. BOIE 17117df37ff3SCatalin\(ux\) M. BOIE return ret; 17127df37ff3SCatalin\(ux\) M. BOIE } 17137df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 17147df37ff3SCatalin\(ux\) M. BOIE 1715b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 171652eeeb84SYOSHIFUJI Hideaki { 171752eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 171852eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 171952eeeb84SYOSHIFUJI Hideaki int onlink; 172052eeeb84SYOSHIFUJI Hideaki 172152eeeb84SYOSHIFUJI Hideaki onlink = 0; 172252eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 172352eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 172452eeeb84SYOSHIFUJI Hideaki if (idev) { 172552eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1726502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 172752eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 172852eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 172952eeeb84SYOSHIFUJI Hideaki if (onlink) 173052eeeb84SYOSHIFUJI Hideaki break; 173152eeeb84SYOSHIFUJI Hideaki } 173252eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 173352eeeb84SYOSHIFUJI Hideaki } 173452eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 173552eeeb84SYOSHIFUJI Hideaki return onlink; 173652eeeb84SYOSHIFUJI Hideaki } 173752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 173852eeeb84SYOSHIFUJI Hideaki 17399acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 17401cab3da6SDaniel Lezcano struct net_device *dev, int strict) 17411da177e4SLinus Torvalds { 1742b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1743ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 17441da177e4SLinus Torvalds 17455c578aedSstephen hemminger rcu_read_lock_bh(); 1746b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1747878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 17481cab3da6SDaniel Lezcano continue; 17491da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 175063159f29SIan Morris if (!dev || ifp->idev->dev == dev || 17511da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1752b79d1d54SDavid S. Miller result = ifp; 17531da177e4SLinus Torvalds in6_ifa_hold(ifp); 17541da177e4SLinus Torvalds break; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds } 17585c578aedSstephen hemminger rcu_read_unlock_bh(); 17591da177e4SLinus Torvalds 1760b79d1d54SDavid S. Miller return result; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 17641da177e4SLinus Torvalds 1765cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 17681da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1769c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 17701da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1771cc411d0bSBrian Haley if (dad_failed) 1772cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 17731da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1774e2577a06SHerbert Xu if (dad_failed) 1775e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 17761da177e4SLinus Torvalds in6_ifa_put(ifp); 17771da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 17781da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 17791da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 17801da177e4SLinus Torvalds ifpub = ifp->ifpub; 17811da177e4SLinus Torvalds if (ifpub) { 17821da177e4SLinus Torvalds in6_ifa_hold(ifpub); 17831da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 17841da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 17851da177e4SLinus Torvalds in6_ifa_put(ifpub); 17861da177e4SLinus Torvalds } else { 17871da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds ipv6_del_addr(ifp); 1790c15b1ccaSHannes Frederic Sowa } else { 17911da177e4SLinus Torvalds ipv6_del_addr(ifp); 17921da177e4SLinus Torvalds } 1793c15b1ccaSHannes Frederic Sowa } 17941da177e4SLinus Torvalds 1795f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1796f2344a13SHerbert Xu { 1797f2344a13SHerbert Xu int err = -ENOENT; 1798f2344a13SHerbert Xu 17998e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1800f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1801f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1802f2344a13SHerbert Xu err = 0; 1803f2344a13SHerbert Xu } 18048e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1805f2344a13SHerbert Xu 1806f2344a13SHerbert Xu return err; 1807f2344a13SHerbert Xu } 1808f2344a13SHerbert Xu 18093c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 18103c21edbdSYOSHIFUJI Hideaki { 18115f40ef77SHannes Frederic Sowa struct in6_addr addr; 18121b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 18131855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 18149bdd8d40SBrian Haley 1815853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1816853dc2e0SUrsula Braun in6_ifa_put(ifp); 1817f2344a13SHerbert Xu return; 1818853dc2e0SUrsula Braun } 1819f2344a13SHerbert Xu 1820e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 18210522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 18229bdd8d40SBrian Haley 18235f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18241b34be74SYOSHIFUJI Hideaki 18255f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 18265f40ef77SHannes Frederic Sowa int scope = ifp->scope; 18275f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 18285f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 18295f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 18305f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 18315f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 18325f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 18335f40ef77SHannes Frederic Sowa 18341855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 18355f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 18365f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 18375f40ef77SHannes Frederic Sowa goto errdad; 18385f40ef77SHannes Frederic Sowa } 18395f40ef77SHannes Frederic Sowa 18405f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 18415f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 18425f40ef77SHannes Frederic Sowa idev)) 18435f40ef77SHannes Frederic Sowa goto errdad; 18445f40ef77SHannes Frederic Sowa 18455f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 18465f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 18475f40ef77SHannes Frederic Sowa 18485f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 18495f40ef77SHannes Frederic Sowa 18505f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 18515f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 18525f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 18535f40ef77SHannes Frederic Sowa goto lock_errdad; 18545f40ef77SHannes Frederic Sowa 18555f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 18565f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 18575f40ef77SHannes Frederic Sowa 18585f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 18595f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 18605f40ef77SHannes Frederic Sowa preferred_lft); 18615f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 18625f40ef77SHannes Frederic Sowa goto lock_errdad; 18635f40ef77SHannes Frederic Sowa 18645f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 18655f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 18665f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 18675f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 18685f40ef77SHannes Frederic Sowa 18691855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 18705f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 18715f40ef77SHannes Frederic Sowa lock_errdad: 18725f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18735f40ef77SHannes Frederic Sowa } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 18741b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 18751b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 18761b34be74SYOSHIFUJI Hideaki 18771b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 18781b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 18791b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 18801b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 18819bdd8d40SBrian Haley 1882f3213831SJoe Perches pr_info("%s: IPv6 being disabled!\n", 18839bdd8d40SBrian Haley ifp->idev->dev->name); 18841b34be74SYOSHIFUJI Hideaki } 18851b34be74SYOSHIFUJI Hideaki } 18861b34be74SYOSHIFUJI Hideaki 18875f40ef77SHannes Frederic Sowa errdad: 1888c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 1889c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 18908e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1891c15b1ccaSHannes Frederic Sowa 1892c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 18933c21edbdSYOSHIFUJI Hideaki } 18941da177e4SLinus Torvalds 1895a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 1896a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 1897b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 18981da177e4SLinus Torvalds { 18991da177e4SLinus Torvalds struct in6_addr maddr; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19021da177e4SLinus Torvalds return; 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19051da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 1908a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 1909b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 19101da177e4SLinus Torvalds { 19111da177e4SLinus Torvalds struct in6_addr maddr; 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19141da177e4SLinus Torvalds return; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19171da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 1920a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 192120380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 19221da177e4SLinus Torvalds { 19231da177e4SLinus Torvalds struct in6_addr addr; 1924c15b1ccaSHannes Frederic Sowa 192588ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 19262bda8a0cSBjørn Mork return; 19271da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19281da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19291da177e4SLinus Torvalds return; 1930013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 1933a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 193420380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 19351da177e4SLinus Torvalds { 19361da177e4SLinus Torvalds struct in6_addr addr; 1937c15b1ccaSHannes Frederic Sowa 193888ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 193932019e65SYOSHIFUJI Hideaki return; 19401da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19411da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19421da177e4SLinus Torvalds return; 19431da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 194606a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 194706a4c1c5Salex.bluesman.smirnov@gmail.com { 194806a4c1c5Salex.bluesman.smirnov@gmail.com if (dev->addr_len != IEEE802154_ADDR_LEN) 194906a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 195006a4c1c5Salex.bluesman.smirnov@gmail.com memcpy(eui, dev->dev_addr, 8); 19515e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 195206a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 195306a4c1c5Salex.bluesman.smirnov@gmail.com } 195406a4c1c5Salex.bluesman.smirnov@gmail.com 1955cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 1956cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 1957cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 1958cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1959cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 1960cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 1961cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1962cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 1963cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1964cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 1965cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 1966cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 1967cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 1968cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1969073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1970073a8e0eSYOSHIFUJI Hideaki { 19711da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 19721da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 19731da177e4SLinus Torvalds return -1; 19741da177e4SLinus Torvalds memset(eui, 0, 7); 19751da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 19761da177e4SLinus Torvalds return 0; 1977073a8e0eSYOSHIFUJI Hideaki } 1978073a8e0eSYOSHIFUJI Hideaki 1979073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1980073a8e0eSYOSHIFUJI Hideaki { 19811da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 19821da177e4SLinus Torvalds return -1; 19831da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 19841da177e4SLinus Torvalds eui[0] |= 2; 19851da177e4SLinus Torvalds return 0; 19861da177e4SLinus Torvalds } 1987073a8e0eSYOSHIFUJI Hideaki 1988c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1989dfd982baSYOSHIFUJI Hideaki { 19909af28511SSascha Hlusiak if (addr == 0) 19919af28511SSascha Hlusiak return -1; 1992dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1993dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1994dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1995dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1996dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1997dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1998dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1999dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2000dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2001dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2002dfd982baSYOSHIFUJI Hideaki return 0; 2003dfd982baSYOSHIFUJI Hideaki } 2004dfd982baSYOSHIFUJI Hideaki 2005dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2006dfd982baSYOSHIFUJI Hideaki { 2007dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2008dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2009dfd982baSYOSHIFUJI Hideaki return -1; 2010dfd982baSYOSHIFUJI Hideaki } 2011dfd982baSYOSHIFUJI Hideaki 2012aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2013aee80b54Sstephen hemminger { 2014aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2015aee80b54Sstephen hemminger } 2016aee80b54Sstephen hemminger 2017e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2018e837735eSNicolas Dichtel { 2019e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2020e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2021e837735eSNicolas Dichtel eui[3] = 0xFF; 2022e837735eSNicolas Dichtel eui[4] = 0xFE; 2023e837735eSNicolas Dichtel eui[0] ^= 2; 2024e837735eSNicolas Dichtel return 0; 2025e837735eSNicolas Dichtel } 2026e837735eSNicolas Dichtel 2027073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2028073a8e0eSYOSHIFUJI Hideaki { 2029073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2030073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2031073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2032073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2033073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2034073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2035073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2036073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2037c7dc89c0SFred L. Templin case ARPHRD_SIT: 2038dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2039aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2040aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2041e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 204206a4c1c5Salex.bluesman.smirnov@gmail.com case ARPHRD_IEEE802154: 204306a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 2044cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2045cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2046e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 2047e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2048073a8e0eSYOSHIFUJI Hideaki } 20491da177e4SLinus Torvalds return -1; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 20531da177e4SLinus Torvalds { 20541da177e4SLinus Torvalds int err = -1; 20551da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2058602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2059602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2060602582caSHannes Frederic Sowa break; 20611da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 20621da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 20631da177e4SLinus Torvalds err = 0; 20641da177e4SLinus Torvalds break; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20681da177e4SLinus Torvalds return err; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 2072eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds regen: 2075955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 20761da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds /* 20791da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 20801da177e4SLinus Torvalds * check if generated address is not inappropriate 20811da177e4SLinus Torvalds * 20821da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 20831da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2084c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 20851da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 20861da177e4SLinus Torvalds * - value 0 20871da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 20881da177e4SLinus Torvalds */ 20891da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 20901da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 20911da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 20921da177e4SLinus Torvalds goto regen; 20931da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 20941da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 20951da177e4SLinus Torvalds goto regen; 20961da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 20971da177e4SLinus Torvalds goto regen; 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 21021da177e4SLinus Torvalds { 21031da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 21041da177e4SLinus Torvalds unsigned long expires; 21051da177e4SLinus Torvalds 21068814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 21071da177e4SLinus Torvalds write_lock_bh(&idev->lock); 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds if (idev->dead) 21101da177e4SLinus Torvalds goto out; 21111da177e4SLinus Torvalds 2112eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds expires = jiffies + 21151da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 21161f9248e5SJiri Pirko idev->cnf.regen_max_retry * idev->cnf.dad_transmits * 21171f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - 2118784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 21191da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 2120f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 2121f3213831SJoe Perches __func__, idev->dev->name); 21221da177e4SLinus Torvalds goto out; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 21261da177e4SLinus Torvalds in6_dev_hold(idev); 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds out: 21291da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 21308814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 21311da177e4SLinus Torvalds in6_dev_put(idev); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 2134eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 21358e5e8f30SEldad Zack { 21361da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 2137eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds /* 21411da177e4SLinus Torvalds * Add prefix route. 21421da177e4SLinus Torvalds */ 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds static void 21451da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2146e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 21471da177e4SLinus Torvalds { 214886872cb5SThomas Graf struct fib6_config cfg = { 214986872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 215086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 215186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 215286872cb5SThomas Graf .fc_expires = expires, 215386872cb5SThomas Graf .fc_dst_len = plen, 215486872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2155c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2156f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 215786872cb5SThomas Graf }; 21581da177e4SLinus Torvalds 21594e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 21621da177e4SLinus Torvalds This thing is done here expecting that the whole 21631da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 21641da177e4SLinus Torvalds */ 216507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 21661da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 216786872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 21680be669bbSJoerg Roedel #endif 21691da177e4SLinus Torvalds 217086872cb5SThomas Graf ip6_route_add(&cfg); 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 217314ef37b6SAndreas Hofmeister 217414ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 217514ef37b6SAndreas Hofmeister int plen, 217614ef37b6SAndreas Hofmeister const struct net_device *dev, 217714ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 217814ef37b6SAndreas Hofmeister { 217914ef37b6SAndreas Hofmeister struct fib6_node *fn; 218014ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 218114ef37b6SAndreas Hofmeister struct fib6_table *table; 218214ef37b6SAndreas Hofmeister 218314ef37b6SAndreas Hofmeister table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); 218463159f29SIan Morris if (!table) 218514ef37b6SAndreas Hofmeister return NULL; 218614ef37b6SAndreas Hofmeister 21875744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 218814ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 218914ef37b6SAndreas Hofmeister if (!fn) 219014ef37b6SAndreas Hofmeister goto out; 21911f56a01fSMartin KaFai Lau 21921f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 219314ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2194d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 219514ef37b6SAndreas Hofmeister continue; 219614ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 219714ef37b6SAndreas Hofmeister continue; 219885da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 219914ef37b6SAndreas Hofmeister continue; 220014ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 220114ef37b6SAndreas Hofmeister break; 220214ef37b6SAndreas Hofmeister } 220314ef37b6SAndreas Hofmeister out: 22045744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 220514ef37b6SAndreas Hofmeister return rt; 220614ef37b6SAndreas Hofmeister } 220714ef37b6SAndreas Hofmeister 220814ef37b6SAndreas Hofmeister 22091da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 22121da177e4SLinus Torvalds { 221386872cb5SThomas Graf struct fib6_config cfg = { 221486872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 221586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 221686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 221786872cb5SThomas Graf .fc_dst_len = 8, 221886872cb5SThomas Graf .fc_flags = RTF_UP, 2219c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 222086872cb5SThomas Graf }; 22211da177e4SLinus Torvalds 222286872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 222386872cb5SThomas Graf 222486872cb5SThomas Graf ip6_route_add(&cfg); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds struct inet6_dev *idev; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds ASSERT_RTNL(); 22321da177e4SLinus Torvalds 2233e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2234e21e8467SStephen Hemminger if (!idev) 223564e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 223664e724f6SBrian Haley 223764e724f6SBrian Haley if (idev->cnf.disable_ipv6) 223864e724f6SBrian Haley return ERR_PTR(-EACCES); 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds /* Add default multicast route */ 22414af04abaSLi Wei if (!(dev->flags & IFF_LOOPBACK)) 22421da177e4SLinus Torvalds addrconf_add_mroute(dev); 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds return idev; 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 224753bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 224853bd6749SJiri Pirko struct inet6_ifaddr *ifp, 224953bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 225053bd6749SJiri Pirko bool create, unsigned long now) 225153bd6749SJiri Pirko { 225253bd6749SJiri Pirko u32 flags; 225353bd6749SJiri Pirko struct inet6_ifaddr *ift; 225453bd6749SJiri Pirko 225553bd6749SJiri Pirko read_lock_bh(&idev->lock); 225653bd6749SJiri Pirko /* update all temporary addresses in the list */ 225753bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 225853bd6749SJiri Pirko int age, max_valid, max_prefered; 225953bd6749SJiri Pirko 226053bd6749SJiri Pirko if (ifp != ift->ifpub) 226153bd6749SJiri Pirko continue; 226253bd6749SJiri Pirko 226353bd6749SJiri Pirko /* RFC 4941 section 3.3: 226453bd6749SJiri Pirko * If a received option will extend the lifetime of a public 226553bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 226653bd6749SJiri Pirko * be extended, subject to the overall constraint that no 226753bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 226853bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 226953bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 227053bd6749SJiri Pirko */ 227153bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 227253bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 227353bd6749SJiri Pirko if (max_valid < 0) 227453bd6749SJiri Pirko max_valid = 0; 227553bd6749SJiri Pirko 227653bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 227753bd6749SJiri Pirko idev->cnf.max_desync_factor - age; 227853bd6749SJiri Pirko if (max_prefered < 0) 227953bd6749SJiri Pirko max_prefered = 0; 228053bd6749SJiri Pirko 228153bd6749SJiri Pirko if (valid_lft > max_valid) 228253bd6749SJiri Pirko valid_lft = max_valid; 228353bd6749SJiri Pirko 228453bd6749SJiri Pirko if (prefered_lft > max_prefered) 228553bd6749SJiri Pirko prefered_lft = max_prefered; 228653bd6749SJiri Pirko 228753bd6749SJiri Pirko spin_lock(&ift->lock); 228853bd6749SJiri Pirko flags = ift->flags; 228953bd6749SJiri Pirko ift->valid_lft = valid_lft; 229053bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 229153bd6749SJiri Pirko ift->tstamp = now; 229253bd6749SJiri Pirko if (prefered_lft > 0) 229353bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 229453bd6749SJiri Pirko 229553bd6749SJiri Pirko spin_unlock(&ift->lock); 229653bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 229753bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 229853bd6749SJiri Pirko } 229953bd6749SJiri Pirko 230053bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 230153bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 230253bd6749SJiri Pirko /* When a new public address is created as described 230353bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 230453bd6749SJiri Pirko * Also create a temporary address if it's enabled but 230553bd6749SJiri Pirko * no temporary address currently exists. 230653bd6749SJiri Pirko */ 230753bd6749SJiri Pirko read_unlock_bh(&idev->lock); 230853bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 230953bd6749SJiri Pirko } else { 231053bd6749SJiri Pirko read_unlock_bh(&idev->lock); 231153bd6749SJiri Pirko } 231253bd6749SJiri Pirko } 231353bd6749SJiri Pirko 2314e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 23151da177e4SLinus Torvalds { 23161da177e4SLinus Torvalds struct prefix_info *pinfo; 23171da177e4SLinus Torvalds __u32 valid_lft; 23181da177e4SLinus Torvalds __u32 prefered_lft; 23191da177e4SLinus Torvalds int addr_type; 232064236f3fSHannes Frederic Sowa u32 addr_flags = 0; 23211da177e4SLinus Torvalds struct inet6_dev *in6_dev; 232256d417b1SBrian Haley struct net *net = dev_net(dev); 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2327ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 23281da177e4SLinus Torvalds return; 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds /* 23321da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 23331da177e4SLinus Torvalds */ 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 23381da177e4SLinus Torvalds return; 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 23411da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2344e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 23451da177e4SLinus Torvalds return; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 23491da177e4SLinus Torvalds 235063159f29SIan Morris if (!in6_dev) { 2351e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2352e87cc472SJoe Perches dev->name); 23531da177e4SLinus Torvalds return; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds /* 23571da177e4SLinus Torvalds * Two things going on here: 23581da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 23591da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 23601da177e4SLinus Torvalds */ 23611da177e4SLinus Torvalds 23624bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 23634bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 23644bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 23654bed72e4SYOSHIFUJI Hideaki 23661da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 23676f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 23686f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 23696f704992SYOSHIFUJI Hideaki * not good. 23701da177e4SLinus Torvalds */ 23714bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 23724bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 23734bed72e4SYOSHIFUJI Hideaki else 23744bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 23753dd4bc68SYOSHIFUJI Hideaki 23764bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 23774bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 23781da177e4SLinus Torvalds 237914ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 238014ef37b6SAndreas Hofmeister pinfo->prefix_len, 238114ef37b6SAndreas Hofmeister dev, 238214ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 238314ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 23841da177e4SLinus Torvalds 238514ef37b6SAndreas Hofmeister if (rt) { 23866f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 23871da177e4SLinus Torvalds if (valid_lft == 0) { 2388e0a1ad73SThomas Graf ip6_del_rt(rt); 23891da177e4SLinus Torvalds rt = NULL; 23904bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 23916f704992SYOSHIFUJI Hideaki /* not infinity */ 23921716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 23936f704992SYOSHIFUJI Hideaki } else { 23941716a961SGao feng rt6_clean_expires(rt); 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds } else if (valid_lft) { 23976f704992SYOSHIFUJI Hideaki clock_t expires = 0; 23984bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 23994bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 24006f704992SYOSHIFUJI Hideaki /* not infinity */ 24016f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 24026f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 24036f704992SYOSHIFUJI Hideaki } 24041da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 24056f704992SYOSHIFUJI Hideaki dev, expires, flags); 24061da177e4SLinus Torvalds } 240794e187c0SAmerigo Wang ip6_rt_put(rt); 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 24131da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 24141da177e4SLinus Torvalds struct in6_addr addr; 24151da177e4SLinus Torvalds int create = 0, update_lft = 0; 2416617fe29dSDaniel Borkmann bool tokenized = false; 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 24191da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2420f53adae4SDaniel Borkmann 2421f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2422f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2423f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2424f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2425f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2426617fe29dSDaniel Borkmann tokenized = true; 2427622c81d5SHannes Frederic Sowa } else if (in6_dev->addr_gen_mode == 2428622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 2429622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2430622c81d5SHannes Frederic Sowa in6_dev)) { 243164236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2432622c81d5SHannes Frederic Sowa goto ok; 2433f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 24341da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 24351da177e4SLinus Torvalds in6_dev_put(in6_dev); 24361da177e4SLinus Torvalds return; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds goto ok; 24391da177e4SLinus Torvalds } 2440e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 24411da177e4SLinus Torvalds pinfo->prefix_len); 24421da177e4SLinus Torvalds in6_dev_put(in6_dev); 24431da177e4SLinus Torvalds return; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds ok: 24461da177e4SLinus Torvalds 244753b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 24481da177e4SLinus Torvalds 244963159f29SIan Morris if (!ifp && valid_lft) { 24501da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 245195c385b4SNeil Horman 245295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 245395c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 2454e6bff995SNeil Horman !net->ipv6.devconf_all->forwarding && sllao) 245595c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 245695c385b4SNeil Horman #endif 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 24591da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 24601da177e4SLinus Torvalds */ 24611da177e4SLinus Torvalds if (!max_addresses || 24621da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 24633f8f5298SJiri Pirko ifp = ipv6_add_addr(in6_dev, &addr, NULL, 24643f8f5298SJiri Pirko pinfo->prefix_len, 246595c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 24668a226b2cSJiri Benc addr_flags, valid_lft, 24678a226b2cSJiri Benc prefered_lft); 24681da177e4SLinus Torvalds 24693f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 if (IS_ERR_OR_NULL(ifp)) { 24701da177e4SLinus Torvalds in6_dev_put(in6_dev); 24711da177e4SLinus Torvalds return; 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 24748a226b2cSJiri Benc update_lft = 0; 24758a226b2cSJiri Benc create = 1; 2476c15b1ccaSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2477c15b1ccaSHannes Frederic Sowa ifp->flags |= IFA_F_MANAGETEMPADDR; 24781da177e4SLinus Torvalds ifp->cstamp = jiffies; 2479617fe29dSDaniel Borkmann ifp->tokenized = tokenized; 2480c15b1ccaSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2481cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds if (ifp) { 2485479840ffSJiri Pirko u32 flags; 24861da177e4SLinus Torvalds unsigned long now; 24871da177e4SLinus Torvalds u32 stored_lft; 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 24908e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 24911da177e4SLinus Torvalds now = jiffies; 24921da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 24931da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 24941da177e4SLinus Torvalds else 24951da177e4SLinus Torvalds stored_lft = 0; 24968a226b2cSJiri Benc if (!update_lft && !create && stored_lft) { 24979451a304SFabian Frederick const u32 minimum_lft = min_t(u32, 24989451a304SFabian Frederick stored_lft, MIN_VALID_LIFETIME); 2499c9d55d5bSPaul Marks valid_lft = max(valid_lft, minimum_lft); 2500c9d55d5bSPaul Marks 2501c9d55d5bSPaul Marks /* RFC4862 Section 5.5.3e: 2502c9d55d5bSPaul Marks * "Note that the preferred lifetime of the 2503c9d55d5bSPaul Marks * corresponding address is always reset to 2504c9d55d5bSPaul Marks * the Preferred Lifetime in the received 2505c9d55d5bSPaul Marks * Prefix Information option, regardless of 2506c9d55d5bSPaul Marks * whether the valid lifetime is also reset or 2507a1ed0526SBrian Haley * ignored." 2508a1ed0526SBrian Haley * 2509c9d55d5bSPaul Marks * So we should always update prefered_lft here. 2510a1ed0526SBrian Haley */ 2511a1ed0526SBrian Haley update_lft = 1; 2512a1ed0526SBrian Haley } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds if (update_lft) { 25151da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 25161da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 25171da177e4SLinus Torvalds ifp->tstamp = now; 25181da177e4SLinus Torvalds flags = ifp->flags; 25191da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 25208e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 25231da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 25241da177e4SLinus Torvalds } else 25258e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 25261da177e4SLinus Torvalds 252753bd6749SJiri Pirko manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 252853bd6749SJiri Pirko create, now); 25295d9efa7eSDavid S. Miller 25301da177e4SLinus Torvalds in6_ifa_put(ifp); 2531c15b1ccaSHannes Frederic Sowa addrconf_verify(); 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 25351da177e4SLinus Torvalds in6_dev_put(in6_dev); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds /* 25391da177e4SLinus Torvalds * Set destination address. 25401da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 25411da177e4SLinus Torvalds * device. 25421da177e4SLinus Torvalds */ 2543af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 25441da177e4SLinus Torvalds { 25451da177e4SLinus Torvalds struct in6_ifreq ireq; 25461da177e4SLinus Torvalds struct net_device *dev; 25471da177e4SLinus Torvalds int err = -EINVAL; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds rtnl_lock(); 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds err = -EFAULT; 25521da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 25531da177e4SLinus Torvalds goto err_exit; 25541da177e4SLinus Torvalds 2555af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds err = -ENODEV; 255863159f29SIan Morris if (!dev) 25591da177e4SLinus Torvalds goto err_exit; 25601da177e4SLinus Torvalds 256107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 25621da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 25635bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 25641da177e4SLinus Torvalds struct ifreq ifr; 25651da177e4SLinus Torvalds struct ip_tunnel_parm p; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25681da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 25691da177e4SLinus Torvalds goto err_exit; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 25721da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 25731da177e4SLinus Torvalds p.iph.saddr = 0; 25741da177e4SLinus Torvalds p.iph.version = 4; 25751da177e4SLinus Torvalds p.iph.ihl = 5; 25761da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 25771da177e4SLinus Torvalds p.iph.ttl = 64; 2578d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 25791da177e4SLinus Torvalds 25805bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 25815bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 25825bc3eb7eSStephen Hemminger 25835bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 25845bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 25851da177e4SLinus Torvalds set_fs(oldfs); 25865bc3eb7eSStephen Hemminger } else 25875bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds if (err == 0) { 25901da177e4SLinus Torvalds err = -ENOBUFS; 2591af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2592af284937SDaniel Lezcano if (!dev) 25931da177e4SLinus Torvalds goto err_exit; 25941da177e4SLinus Torvalds err = dev_open(dev); 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds } 25970be669bbSJoerg Roedel #endif 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds err_exit: 26001da177e4SLinus Torvalds rtnl_unlock(); 26011da177e4SLinus Torvalds return err; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds 260493a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 260593a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 260693a714d6SMadhu Challa { 260793a714d6SMadhu Challa int ret; 260893a714d6SMadhu Challa 260993a714d6SMadhu Challa ASSERT_RTNL(); 261093a714d6SMadhu Challa 261193a714d6SMadhu Challa lock_sock(sk); 261293a714d6SMadhu Challa if (join) 261354ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 261493a714d6SMadhu Challa else 261554ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 261693a714d6SMadhu Challa release_sock(sk); 261793a714d6SMadhu Challa 261893a714d6SMadhu Challa return ret; 261993a714d6SMadhu Challa } 262093a714d6SMadhu Challa 26211da177e4SLinus Torvalds /* 26221da177e4SLinus Torvalds * Manual configuration of address on an interface 26231da177e4SLinus Torvalds */ 2624479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2625479840ffSJiri Pirko const struct in6_addr *pfx, 2626caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2627479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2628479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 26291da177e4SLinus Torvalds { 26301da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 26311da177e4SLinus Torvalds struct inet6_dev *idev; 26321da177e4SLinus Torvalds struct net_device *dev; 263393a714d6SMadhu Challa unsigned long timeout; 263493a714d6SMadhu Challa clock_t expires; 26351da177e4SLinus Torvalds int scope; 26366f704992SYOSHIFUJI Hideaki u32 flags; 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds ASSERT_RTNL(); 26391da177e4SLinus Torvalds 264024ef0da7SThomas Graf if (plen > 128) 264124ef0da7SThomas Graf return -EINVAL; 264224ef0da7SThomas Graf 26430778769dSNoriaki TAKAMIYA /* check the lifetime */ 26440778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 26450778769dSNoriaki TAKAMIYA return -EINVAL; 26460778769dSNoriaki TAKAMIYA 264753bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 264853bd6749SJiri Pirko return -EINVAL; 264953bd6749SJiri Pirko 2650af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2651af284937SDaniel Lezcano if (!dev) 26521da177e4SLinus Torvalds return -ENODEV; 26531da177e4SLinus Torvalds 265464e724f6SBrian Haley idev = addrconf_add_dev(dev); 265564e724f6SBrian Haley if (IS_ERR(idev)) 265664e724f6SBrian Haley return PTR_ERR(idev); 26571da177e4SLinus Torvalds 265893a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 265993a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 266093a714d6SMadhu Challa true, pfx, ifindex); 266193a714d6SMadhu Challa 266293a714d6SMadhu Challa if (ret < 0) 266393a714d6SMadhu Challa return ret; 266493a714d6SMadhu Challa } 266593a714d6SMadhu Challa 26661da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 26671da177e4SLinus Torvalds 26684bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 26694bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 26704bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 26714bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 26726f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 26734bed72e4SYOSHIFUJI Hideaki } else { 26744bed72e4SYOSHIFUJI Hideaki expires = 0; 26754bed72e4SYOSHIFUJI Hideaki flags = 0; 26764bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 26776f704992SYOSHIFUJI Hideaki } 26780778769dSNoriaki TAKAMIYA 26794bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 26804bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 26814bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 26820778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 26834bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 26844bed72e4SYOSHIFUJI Hideaki } 26850778769dSNoriaki TAKAMIYA 26868a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 26878a226b2cSJiri Benc valid_lft, prefered_lft); 26880778769dSNoriaki TAKAMIYA 26891da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2690761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 269146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 26926f704992SYOSHIFUJI Hideaki expires, flags); 2693761aac73SThomas Haller } 2694761aac73SThomas Haller 269595c385b4SNeil Horman /* 269695c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 269795c385b4SNeil Horman * that the Optimistic flag should not be set for 269895c385b4SNeil Horman * manually configured addresses 269995c385b4SNeil Horman */ 2700cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 270153bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 270253bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 270353bd6749SJiri Pirko true, jiffies); 27041da177e4SLinus Torvalds in6_ifa_put(ifp); 2705c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 27061da177e4SLinus Torvalds return 0; 270793a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 270893a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 270993a714d6SMadhu Challa false, pfx, ifindex); 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds return PTR_ERR(ifp); 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds 27156046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 27166046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 27171da177e4SLinus Torvalds { 27181da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27191da177e4SLinus Torvalds struct inet6_dev *idev; 27201da177e4SLinus Torvalds struct net_device *dev; 27211da177e4SLinus Torvalds 272224ef0da7SThomas Graf if (plen > 128) 272324ef0da7SThomas Graf return -EINVAL; 272424ef0da7SThomas Graf 2725af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2726af284937SDaniel Lezcano if (!dev) 27271da177e4SLinus Torvalds return -ENODEV; 27281da177e4SLinus Torvalds 2729e5d08d71SIan Morris idev = __in6_dev_get(dev); 273063159f29SIan Morris if (!idev) 27311da177e4SLinus Torvalds return -ENXIO; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2734502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 27351da177e4SLinus Torvalds if (ifp->prefix_len == plen && 27361da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 27371da177e4SLinus Torvalds in6_ifa_hold(ifp); 27381da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27391da177e4SLinus Torvalds 27406046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 27416046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 27426046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 27436046d5b4SHeiner Kallweit jiffies); 27441da177e4SLinus Torvalds ipv6_del_addr(ifp); 27456046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 274693a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 274793a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 274893a714d6SMadhu Challa false, pfx, dev->ifindex); 274993a714d6SMadhu Challa } 27501da177e4SLinus Torvalds return 0; 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27541da177e4SLinus Torvalds return -EADDRNOTAVAIL; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds 2758af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 27591da177e4SLinus Torvalds { 27601da177e4SLinus Torvalds struct in6_ifreq ireq; 27611da177e4SLinus Torvalds int err; 27621da177e4SLinus Torvalds 2763af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 27641da177e4SLinus Torvalds return -EPERM; 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27671da177e4SLinus Torvalds return -EFAULT; 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds rtnl_lock(); 2770caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2771af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2772af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 27731da177e4SLinus Torvalds rtnl_unlock(); 27741da177e4SLinus Torvalds return err; 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 2777af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds struct in6_ifreq ireq; 27801da177e4SLinus Torvalds int err; 27811da177e4SLinus Torvalds 2782af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 27831da177e4SLinus Torvalds return -EPERM; 27841da177e4SLinus Torvalds 27851da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27861da177e4SLinus Torvalds return -EFAULT; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds rtnl_lock(); 27896046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2790af284937SDaniel Lezcano ireq.ifr6_prefixlen); 27911da177e4SLinus Torvalds rtnl_unlock(); 27921da177e4SLinus Torvalds return err; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 2795b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2796b5f348e5SIlpo Järvinen int plen, int scope) 2797b5f348e5SIlpo Järvinen { 2798b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2799b5f348e5SIlpo Järvinen 28008a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 280107edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 280207edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2803b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2804b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2805b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2806b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2807b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2808b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2809b5f348e5SIlpo Järvinen } 2810b5f348e5SIlpo Järvinen } 2811b5f348e5SIlpo Järvinen 281207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 28131da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 28141da177e4SLinus Torvalds { 28151da177e4SLinus Torvalds struct in6_addr addr; 28161da177e4SLinus Torvalds struct net_device *dev; 2817c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2818929c9cf3SNicolas Dichtel int scope, plen; 2819f0e2acfaSNicolas Dichtel u32 pflags = 0; 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds ASSERT_RTNL(); 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 28241da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 28271da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 28281da177e4SLinus Torvalds scope = IFA_LINK; 2829929c9cf3SNicolas Dichtel plen = 64; 28301da177e4SLinus Torvalds } else { 28311da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2832929c9cf3SNicolas Dichtel plen = 96; 2833f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2837929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2838f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 28391da177e4SLinus Torvalds return; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 28426fda7350SBenjamin Thery for_each_netdev(net, dev) { 2843e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 28441da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 28451da177e4SLinus Torvalds struct in_ifaddr *ifa; 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds int flag = scope; 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 28541da177e4SLinus Torvalds continue; 28551da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 28561da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 28571da177e4SLinus Torvalds continue; 28581da177e4SLinus Torvalds flag |= IFA_HOST; 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds 2861b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 2862f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 2863f0e2acfaSNicolas Dichtel pflags); 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds } 28680be669bbSJoerg Roedel #endif 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 28711da177e4SLinus Torvalds { 28721da177e4SLinus Torvalds struct inet6_dev *idev; 287325fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 287425fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 287525fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds /* ::1 */ 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds ASSERT_RTNL(); 28801da177e4SLinus Torvalds 2881e5d08d71SIan Morris idev = ipv6_find_idev(dev); 288263159f29SIan Morris if (!idev) { 288391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 28841da177e4SLinus Torvalds return; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 2887b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 288825fb6ca4SBalakumaran Kannan 288925fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 289025fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 289125fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 289225fb6ca4SBalakumaran Kannan continue; 289325fb6ca4SBalakumaran Kannan 289425fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 289525fb6ca4SBalakumaran Kannan if (!idev) 289625fb6ca4SBalakumaran Kannan continue; 289725fb6ca4SBalakumaran Kannan 289825fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 289925fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 290025fb6ca4SBalakumaran Kannan 290125fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 290225fb6ca4SBalakumaran Kannan continue; 290325fb6ca4SBalakumaran Kannan 290433d99113SGao feng if (sp_ifa->rt) { 290533d99113SGao feng /* This dst has been added to garbage list when 290633d99113SGao feng * lo device down, release this obsolete dst and 290733d99113SGao feng * reallocate a new router for ifa. 290833d99113SGao feng */ 290933d99113SGao feng if (sp_ifa->rt->dst.obsolete > 0) { 291033d99113SGao feng ip6_rt_put(sp_ifa->rt); 291133d99113SGao feng sp_ifa->rt = NULL; 291233d99113SGao feng } else { 2913a881ae1fSGao feng continue; 291433d99113SGao feng } 291533d99113SGao feng } 2916a881ae1fSGao feng 291757ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 291825fb6ca4SBalakumaran Kannan 291925fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 2920534c8779SGao feng if (!IS_ERR(sp_rt)) { 2921534c8779SGao feng sp_ifa->rt = sp_rt; 292225fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 292325fb6ca4SBalakumaran Kannan } 2924534c8779SGao feng } 292525fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 292625fb6ca4SBalakumaran Kannan } 29271da177e4SLinus Torvalds } 29281da177e4SLinus Torvalds 292964236f3fSHannes Frederic Sowa static void addrconf_add_linklocal(struct inet6_dev *idev, 293064236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 293364236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 29341da177e4SLinus Torvalds 293595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 293695c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2937702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 293895c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 293995c385b4SNeil Horman #endif 294095c385b4SNeil Horman 294107edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 294207edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 29431da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 294446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2945cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 29461da177e4SLinus Torvalds in6_ifa_put(ifp); 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 2950622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 2951622c81d5SHannes Frederic Sowa { 2952622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 2953622c81d5SHannes Frederic Sowa return true; 2954622c81d5SHannes Frederic Sowa 2955622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 2956622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 2957622c81d5SHannes Frederic Sowa return true; 2958622c81d5SHannes Frederic Sowa 2959622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 2960622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 2961622c81d5SHannes Frederic Sowa return true; 2962622c81d5SHannes Frederic Sowa 2963622c81d5SHannes Frederic Sowa return false; 2964622c81d5SHannes Frederic Sowa } 2965622c81d5SHannes Frederic Sowa 2966622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 2967622c81d5SHannes Frederic Sowa u8 dad_count, 2968622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 2969622c81d5SHannes Frederic Sowa { 2970622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 2971622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 2972622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 2973622c81d5SHannes Frederic Sowa 2974622c81d5SHannes Frederic Sowa static union { 2975622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 2976622c81d5SHannes Frederic Sowa struct { 2977622c81d5SHannes Frederic Sowa struct in6_addr secret; 2978ff40217eSHannes Frederic Sowa __be32 prefix[2]; 2979622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 2980622c81d5SHannes Frederic Sowa u8 dad_count; 2981622c81d5SHannes Frederic Sowa } __packed; 2982622c81d5SHannes Frederic Sowa } data; 2983622c81d5SHannes Frederic Sowa 2984622c81d5SHannes Frederic Sowa struct in6_addr secret; 2985622c81d5SHannes Frederic Sowa struct in6_addr temp; 2986622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 2987622c81d5SHannes Frederic Sowa 2988622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 2989622c81d5SHannes Frederic Sowa 2990622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 2991622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 2992622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 2993622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 2994622c81d5SHannes Frederic Sowa else 2995622c81d5SHannes Frederic Sowa return -1; 2996622c81d5SHannes Frederic Sowa 2997622c81d5SHannes Frederic Sowa retry: 2998622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 2999622c81d5SHannes Frederic Sowa 3000622c81d5SHannes Frederic Sowa sha_init(digest); 3001622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3002622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3003622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3004ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3005ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3006622c81d5SHannes Frederic Sowa data.secret = secret; 3007622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3008622c81d5SHannes Frederic Sowa 3009622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3010622c81d5SHannes Frederic Sowa 3011622c81d5SHannes Frederic Sowa temp = *address; 3012ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3013ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3014622c81d5SHannes Frederic Sowa 3015622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3016622c81d5SHannes Frederic Sowa 3017622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3018622c81d5SHannes Frederic Sowa dad_count++; 30191855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3020622c81d5SHannes Frederic Sowa return -1; 3021622c81d5SHannes Frederic Sowa goto retry; 3022622c81d5SHannes Frederic Sowa } 3023622c81d5SHannes Frederic Sowa 3024622c81d5SHannes Frederic Sowa *address = temp; 3025622c81d5SHannes Frederic Sowa return 0; 3026622c81d5SHannes Frederic Sowa } 3027622c81d5SHannes Frederic Sowa 3028bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3029bc91b0f0SJiri Pirko { 3030bc91b0f0SJiri Pirko struct in6_addr addr; 3031bc91b0f0SJiri Pirko 3032bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3033622c81d5SHannes Frederic Sowa 3034622c81d5SHannes Frederic Sowa if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) { 3035622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 303664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 303764236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3038622c81d5SHannes Frederic Sowa else if (prefix_route) 3039622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3040622c81d5SHannes Frederic Sowa } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) { 3041bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3042bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3043bc91b0f0SJiri Pirko * couldn't generate one. 3044bc91b0f0SJiri Pirko */ 3045bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 304664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3047bc91b0f0SJiri Pirko else if (prefix_route) 3048bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3049bc91b0f0SJiri Pirko } 3050bc91b0f0SJiri Pirko } 3051bc91b0f0SJiri Pirko 30521da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 30531da177e4SLinus Torvalds { 30541da177e4SLinus Torvalds struct inet6_dev *idev; 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds ASSERT_RTNL(); 30571da177e4SLinus Torvalds 305874235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 305974235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 306074235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 306106a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3062cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 (dev->type != ARPHRD_IEEE802154) && 3063e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3064e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3065e74bccb8SJukka Rissanen (dev->type != ARPHRD_6LOWPAN)) { 306674235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 306774235a25SHerbert Xu return; 306874235a25SHerbert Xu } 306974235a25SHerbert Xu 30701da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 307164e724f6SBrian Haley if (IS_ERR(idev)) 30721da177e4SLinus Torvalds return; 30731da177e4SLinus Torvalds 3074bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 30751da177e4SLinus Torvalds } 30761da177e4SLinus Torvalds 307707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30781da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 30791da177e4SLinus Torvalds { 30801da177e4SLinus Torvalds struct inet6_dev *idev; 30811da177e4SLinus Torvalds 30821da177e4SLinus Torvalds ASSERT_RTNL(); 30831da177e4SLinus Torvalds 30841da177e4SLinus Torvalds /* 30851da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 30861da177e4SLinus Torvalds * addresses... we should configure all of 30871da177e4SLinus Torvalds * our v4 addrs in the tunnel 30881da177e4SLinus Torvalds */ 30891da177e4SLinus Torvalds 3090e5d08d71SIan Morris idev = ipv6_find_idev(dev); 309163159f29SIan Morris if (!idev) { 309291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 30931da177e4SLinus Torvalds return; 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 3096c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3097bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3098c7dc89c0SFred L. Templin return; 3099c7dc89c0SFred L. Templin } 3100c7dc89c0SFred L. Templin 31011da177e4SLinus Torvalds sit_add_v4_addrs(idev); 31021da177e4SLinus Torvalds 310362b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 31041da177e4SLinus Torvalds addrconf_add_mroute(dev); 31051da177e4SLinus Torvalds } 31060be669bbSJoerg Roedel #endif 31071da177e4SLinus Torvalds 310807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3109aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3110aee80b54Sstephen hemminger { 3111aee80b54Sstephen hemminger struct inet6_dev *idev; 3112aee80b54Sstephen hemminger 3113aee80b54Sstephen hemminger ASSERT_RTNL(); 3114aee80b54Sstephen hemminger 3115e5d08d71SIan Morris idev = ipv6_find_idev(dev); 311663159f29SIan Morris if (!idev) { 311791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3118aee80b54Sstephen hemminger return; 3119aee80b54Sstephen hemminger } 3120aee80b54Sstephen hemminger 3121bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3122*d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3123*d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3124aee80b54Sstephen hemminger } 3125aee80b54Sstephen hemminger #endif 3126aee80b54Sstephen hemminger 31271da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3128351638e7SJiri Pirko void *ptr) 31291da177e4SLinus Torvalds { 3130351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3131748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 3132c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3133b217d616SHerbert Xu int err; 31341da177e4SLinus Torvalds 31351da177e4SLinus Torvalds switch (event) { 313645ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 313774235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 313845ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3139a317a2f1SWANG Cong if (IS_ERR(idev)) 3140a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 314145ba9dd2SYOSHIFUJI Hideaki } 314245ba9dd2SYOSHIFUJI Hideaki break; 3143bcdd553fSStephen Hemminger 31441da177e4SLinus Torvalds case NETDEV_UP: 31453c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3146c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3147c2edacf8SJay Vosburgh break; 3148c2edacf8SJay Vosburgh 31493ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 31503ce62a84SWANG Cong break; 31513ce62a84SWANG Cong 31523c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 3153f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 31543c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3155f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 31563c21edbdSYOSHIFUJI Hideaki dev->name); 31573c21edbdSYOSHIFUJI Hideaki break; 31583c21edbdSYOSHIFUJI Hideaki } 315999081049SKristian Slavov 3160d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3161d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3162d31c7b8fSEvgeniy Polyakov 3163a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 316499081049SKristian Slavov idev->if_flags |= IF_READY; 3165e3ec6cfcSBenjamin Thery run_pending = 1; 3166e3ec6cfcSBenjamin Thery } 31673c21edbdSYOSHIFUJI Hideaki } else { 3168f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 31693c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 31703c21edbdSYOSHIFUJI Hideaki break; 31713c21edbdSYOSHIFUJI Hideaki } 31723c21edbdSYOSHIFUJI Hideaki 31733c21edbdSYOSHIFUJI Hideaki if (idev) { 3174bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 31753c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 31763c21edbdSYOSHIFUJI Hideaki break; 31773c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 31783c21edbdSYOSHIFUJI Hideaki } 31793c21edbdSYOSHIFUJI Hideaki 3180f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 31813c21edbdSYOSHIFUJI Hideaki dev->name); 31823c21edbdSYOSHIFUJI Hideaki 3183c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 31843c21edbdSYOSHIFUJI Hideaki } 31853c21edbdSYOSHIFUJI Hideaki 31861da177e4SLinus Torvalds switch (dev->type) { 318707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 31881da177e4SLinus Torvalds case ARPHRD_SIT: 31891da177e4SLinus Torvalds addrconf_sit_config(dev); 31901da177e4SLinus Torvalds break; 31910be669bbSJoerg Roedel #endif 319207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3193aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3194aee80b54Sstephen hemminger addrconf_gre_config(dev); 3195aee80b54Sstephen hemminger break; 3196aee80b54Sstephen hemminger #endif 31971da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 31981da177e4SLinus Torvalds init_loopback(dev); 31991da177e4SLinus Torvalds break; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds default: 32021da177e4SLinus Torvalds addrconf_dev_config(dev); 32031da177e4SLinus Torvalds break; 32043ff50b79SStephen Hemminger } 3205bcdd553fSStephen Hemminger 3206a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3207c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3208c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3209c5e33bddSYOSHIFUJI Hideaki 3210bcdd553fSStephen Hemminger /* 3211bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3212bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3213bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 32141da177e4SLinus Torvalds */ 3215bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3216bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 32171da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 32181da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds idev->tstamp = jiffies; 32211da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3222bcdd553fSStephen Hemminger 3223bcdd553fSStephen Hemminger /* 3224bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3225bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 32261da177e4SLinus Torvalds */ 32271da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3228bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds break; 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 32331da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 32341da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 32351da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 32361da177e4SLinus Torvalds break; 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 3239d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 3240d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3241a317a2f1SWANG Cong if (!IS_ERR(idev)) 3242d31c7b8fSEvgeniy Polyakov break; 3243d31c7b8fSEvgeniy Polyakov } 3244d31c7b8fSEvgeniy Polyakov 3245bcdd553fSStephen Hemminger /* 3246db9c7c39Sstephen hemminger * if MTU under IPV6_MIN_MTU. 3247bcdd553fSStephen Hemminger * Stop IPv6 on this interface. 3248bcdd553fSStephen Hemminger */ 32491da177e4SLinus Torvalds 32501da177e4SLinus Torvalds case NETDEV_DOWN: 32511da177e4SLinus Torvalds case NETDEV_UNREGISTER: 32521da177e4SLinus Torvalds /* 32531da177e4SLinus Torvalds * Remove all addresses from this interface. 32541da177e4SLinus Torvalds */ 32551da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 32561da177e4SLinus Torvalds break; 32573c21edbdSYOSHIFUJI Hideaki 32581da177e4SLinus Torvalds case NETDEV_CHANGENAME: 32591da177e4SLinus Torvalds if (idev) { 32605632c515SStephen Hemminger snmp6_unregister_dev(idev); 3261408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3262a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3263b217d616SHerbert Xu if (err) 3264b217d616SHerbert Xu return notifier_from_errno(err); 3265a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3266a317a2f1SWANG Cong if (err) { 3267a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3268a317a2f1SWANG Cong return notifier_from_errno(err); 3269a317a2f1SWANG Cong } 32705632c515SStephen Hemminger } 32711da177e4SLinus Torvalds break; 3272bcdd553fSStephen Hemminger 327393d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 327493d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 327593d9b7d7SJiri Pirko addrconf_type_change(dev, event); 327675c78500SMoni Shoua break; 32773ff50b79SStephen Hemminger } 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds return NOTIFY_OK; 32801da177e4SLinus Torvalds } 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds /* 32831da177e4SLinus Torvalds * addrconf module should be notified of a device going up 32841da177e4SLinus Torvalds */ 32851da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 32861da177e4SLinus Torvalds .notifier_call = addrconf_notify, 32871da177e4SLinus Torvalds }; 32881da177e4SLinus Torvalds 328993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 329075c78500SMoni Shoua { 329175c78500SMoni Shoua struct inet6_dev *idev; 329275c78500SMoni Shoua ASSERT_RTNL(); 329375c78500SMoni Shoua 329475c78500SMoni Shoua idev = __in6_dev_get(dev); 329575c78500SMoni Shoua 329693d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 329775c78500SMoni Shoua ipv6_mc_remap(idev); 329893d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 329975c78500SMoni Shoua ipv6_mc_unmap(idev); 330075c78500SMoni Shoua } 330175c78500SMoni Shoua 33021da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 33031da177e4SLinus Torvalds { 3304c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3305502a2ffdSstephen hemminger struct inet6_dev *idev; 3306502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 330773a8bd74SDavid S. Miller int state, i; 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds ASSERT_RTNL(); 33101da177e4SLinus Torvalds 3311f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 331273a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds idev = __in6_dev_get(dev); 331563159f29SIan Morris if (!idev) 33161da177e4SLinus Torvalds return -ENODEV; 33171da177e4SLinus Torvalds 3318bcdd553fSStephen Hemminger /* 3319bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3320bcdd553fSStephen Hemminger * Do not dev_put! 33211da177e4SLinus Torvalds */ 3322439e2385SDenis V. Lunev if (how) { 33231da177e4SLinus Torvalds idev->dead = 1; 33248814c4b5SYOSHIFUJI Hideaki 33258814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3326a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 33271da177e4SLinus Torvalds 33281da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 33291da177e4SLinus Torvalds snmp6_unregister_dev(idev); 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds } 33321da177e4SLinus Torvalds 333373a8bd74SDavid S. Miller /* Step 2: clear hash table */ 333473a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 333573a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 333673a8bd74SDavid S. Miller 333773a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 333873a8bd74SDavid S. Miller restart: 3339b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 334073a8bd74SDavid S. Miller if (ifa->idev == idev) { 334173a8bd74SDavid S. Miller hlist_del_init_rcu(&ifa->addr_lst); 3342c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 334373a8bd74SDavid S. Miller goto restart; 334473a8bd74SDavid S. Miller } 334573a8bd74SDavid S. Miller } 334673a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 334773a8bd74SDavid S. Miller } 334873a8bd74SDavid S. Miller 33491da177e4SLinus Torvalds write_lock_bh(&idev->lock); 33501da177e4SLinus Torvalds 3351b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3352b7b1bfceSHannes Frederic Sowa 3353bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3354439e2385SDenis V. Lunev if (!how) 33553c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 33561da177e4SLinus Torvalds 3357439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 33581da177e4SLinus Torvalds in6_dev_put(idev); 33591da177e4SLinus Torvalds 3360bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3361372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3362372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3363372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3364372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 33651da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 33661da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds if (ifa->ifpub) { 33691da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 33701da177e4SLinus Torvalds ifa->ifpub = NULL; 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 33731da177e4SLinus Torvalds in6_ifa_put(ifa); 33741da177e4SLinus Torvalds write_lock_bh(&idev->lock); 33751da177e4SLinus Torvalds } 33768f37ada5Sstephen hemminger 3377502a2ffdSstephen hemminger while (!list_empty(&idev->addr_list)) { 3378502a2ffdSstephen hemminger ifa = list_first_entry(&idev->addr_list, 3379502a2ffdSstephen hemminger struct inet6_ifaddr, if_list); 3380c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 338184e8b803Sstephen hemminger 3382502a2ffdSstephen hemminger list_del(&ifa->if_list); 33831da177e4SLinus Torvalds 33844c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 338573a8bd74SDavid S. Miller 33868e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 33874c5ff6a6SHerbert Xu state = ifa->state; 33884c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 33898e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 33904c5ff6a6SHerbert Xu 33919d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 33921da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3393f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 33942de79570SLorenzo Colitti } 33959d82ca98SJohn Fastabend in6_ifa_put(ifa); 339673a8bd74SDavid S. Miller 33971da177e4SLinus Torvalds write_lock_bh(&idev->lock); 33981da177e4SLinus Torvalds } 33998f37ada5Sstephen hemminger 34001da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 34011da177e4SLinus Torvalds 3402381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3403381f4dcaSSabrina Dubroca if (how) { 3404381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 34051da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3406381f4dcaSSabrina Dubroca } else { 34071da177e4SLinus Torvalds ipv6_mc_down(idev); 3408381f4dcaSSabrina Dubroca } 34091da177e4SLinus Torvalds 34101da177e4SLinus Torvalds idev->tstamp = jiffies; 34111da177e4SLinus Torvalds 3412bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3413439e2385SDenis V. Lunev if (how) { 3414408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 34151da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 34161da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 34171da177e4SLinus Torvalds in6_dev_put(idev); 34181da177e4SLinus Torvalds } 34191da177e4SLinus Torvalds return 0; 34201da177e4SLinus Torvalds } 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 34231da177e4SLinus Torvalds { 3424b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3425caf92bc4SCong Wang struct net_device *dev = idev->dev; 3426b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 34271da177e4SLinus Torvalds 3428b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 34295b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 34301da177e4SLinus Torvalds goto out; 34311da177e4SLinus Torvalds 34329ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 34331da177e4SLinus Torvalds goto out; 34345b2a1953Sstephen hemminger 34355b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 34365b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 34375b2a1953Sstephen hemminger goto out; 34381da177e4SLinus Torvalds 3439b7b1bfceSHannes Frederic Sowa if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3440caf92bc4SCong Wang write_unlock(&idev->lock); 3441caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3442caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3443b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3444b7b1bfceSHannes Frederic Sowa else 3445caf92bc4SCong Wang goto put; 3446b7b1bfceSHannes Frederic Sowa 3447caf92bc4SCong Wang write_lock(&idev->lock); 34481da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3449b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3450b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 34515b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 34525b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 34531da177e4SLinus Torvalds } else { 34541da177e4SLinus Torvalds /* 34551da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 34561da177e4SLinus Torvalds * assumption any longer. 34571da177e4SLinus Torvalds */ 345891df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 34591da177e4SLinus Torvalds } 34601da177e4SLinus Torvalds 34611da177e4SLinus Torvalds out: 3462b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3463caf92bc4SCong Wang put: 3464b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 34651da177e4SLinus Torvalds } 34661da177e4SLinus Torvalds 34671da177e4SLinus Torvalds /* 34681da177e4SLinus Torvalds * Duplicate Address Detection 34691da177e4SLinus Torvalds */ 34703c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 34713c21edbdSYOSHIFUJI Hideaki { 34723c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 34733c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 34743c21edbdSYOSHIFUJI Hideaki 347595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 347695c385b4SNeil Horman rand_num = 0; 347795c385b4SNeil Horman else 347863862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 347995c385b4SNeil Horman 3480b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3481c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 34823c21edbdSYOSHIFUJI Hideaki } 34833c21edbdSYOSHIFUJI Hideaki 3484c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 34851da177e4SLinus Torvalds { 34861da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 34871da177e4SLinus Torvalds struct net_device *dev = idev->dev; 34881da177e4SLinus Torvalds 34891da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 34901da177e4SLinus Torvalds 349163862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 34921da177e4SLinus Torvalds 34931da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3494622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3495e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 34961da177e4SLinus Torvalds goto out; 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 34991b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 350055ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 350155ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3502cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 350321809fafSstephen hemminger spin_unlock(&ifp->lock); 35041da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds addrconf_dad_completed(ifp); 35071da177e4SLinus Torvalds return; 35081da177e4SLinus Torvalds } 35091da177e4SLinus Torvalds 35106732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 351121809fafSstephen hemminger spin_unlock(&ifp->lock); 35126732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 35133c21edbdSYOSHIFUJI Hideaki /* 3514590a9887SMasatake YAMATO * If the device is not ready: 35153c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 35163c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 35173c21edbdSYOSHIFUJI Hideaki */ 35183c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3519cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 35206732badeSYOSHIFUJI Hideaki return; 35213c21edbdSYOSHIFUJI Hideaki } 352295c385b4SNeil Horman 352395c385b4SNeil Horman /* 352495c385b4SNeil Horman * Optimistic nodes can start receiving 352595c385b4SNeil Horman * Frames right away 352695c385b4SNeil Horman */ 35277fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 352895c385b4SNeil Horman ip6_ins_rt(ifp->rt); 35297fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 35307fd2561eSErik Kline /* Because optimistic nodes can use this address, 35317fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 35327fd2561eSErik Kline */ 35337fd2561eSErik Kline ipv6_ifa_notify(RTM_NEWADDR, ifp); 35347fd2561eSErik Kline } 35357fd2561eSErik Kline } 353695c385b4SNeil Horman 35376732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 35381da177e4SLinus Torvalds out: 3539622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 35401da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 35411da177e4SLinus Torvalds } 35421da177e4SLinus Torvalds 3543c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 35441da177e4SLinus Torvalds { 3545c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3546c15b1ccaSHannes Frederic Sowa 35478e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3548c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3549c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3550c15b1ccaSHannes Frederic Sowa begin_dad = true; 3551c15b1ccaSHannes Frederic Sowa } 35528e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3553c15b1ccaSHannes Frederic Sowa 3554c15b1ccaSHannes Frederic Sowa if (begin_dad) 3555c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3556c15b1ccaSHannes Frederic Sowa } 3557c15b1ccaSHannes Frederic Sowa 3558c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3559c15b1ccaSHannes Frederic Sowa { 3560c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3561c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3562c15b1ccaSHannes Frederic Sowa dad_work); 35631da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 35641da177e4SLinus Torvalds struct in6_addr mcaddr; 35651da177e4SLinus Torvalds 3566c15b1ccaSHannes Frederic Sowa enum { 3567c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3568c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3569c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3570c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3571c15b1ccaSHannes Frederic Sowa 3572c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3573c15b1ccaSHannes Frederic Sowa 35748e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3575c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3576c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3577c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3578c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3579c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3580c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 3581c15b1ccaSHannes Frederic Sowa } 35828e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3583c15b1ccaSHannes Frederic Sowa 3584c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3585c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3586c15b1ccaSHannes Frederic Sowa goto out; 3587c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3588c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 3589c15b1ccaSHannes Frederic Sowa goto out; 3590c15b1ccaSHannes Frederic Sowa } 3591c15b1ccaSHannes Frederic Sowa 3592b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3593f2344a13SHerbert Xu goto out; 3594f2344a13SHerbert Xu 3595c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3596122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3597c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 35981da177e4SLinus Torvalds goto out; 35991da177e4SLinus Torvalds } 360021809fafSstephen hemminger 360121809fafSstephen hemminger spin_lock(&ifp->lock); 3602622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3603622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3604c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3605622ccdf1SHerbert Xu goto out; 3606622ccdf1SHerbert Xu } 3607622ccdf1SHerbert Xu 3608b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 36091da177e4SLinus Torvalds /* 36101da177e4SLinus Torvalds * DAD was successful 36111da177e4SLinus Torvalds */ 36121da177e4SLinus Torvalds 3613cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 361421809fafSstephen hemminger spin_unlock(&ifp->lock); 3615c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds addrconf_dad_completed(ifp); 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds goto out; 36201da177e4SLinus Torvalds } 36211da177e4SLinus Torvalds 3622b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3623c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 36241f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 362521809fafSstephen hemminger spin_unlock(&ifp->lock); 3626c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 36271da177e4SLinus Torvalds 36281da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 36291da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3630ab450605SJiri Benc ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any, NULL); 36311da177e4SLinus Torvalds out: 36321da177e4SLinus Torvalds in6_ifa_put(ifp); 3633c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 36341da177e4SLinus Torvalds } 36351da177e4SLinus Torvalds 363611ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 363711ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 363811ffff75SHannes Frederic Sowa { 363911ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 364011ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 364111ffff75SHannes Frederic Sowa 3642602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3643602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 3644602582caSHannes Frederic Sowa break; 364511ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 364611ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 364711ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 364811ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 364911ffff75SHannes Frederic Sowa return false; 365011ffff75SHannes Frederic Sowa } 365111ffff75SHannes Frederic Sowa return true; 365211ffff75SHannes Frederic Sowa } 365311ffff75SHannes Frederic Sowa 36541da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 36551da177e4SLinus Torvalds { 36561da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3657b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3658b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3659b7b1bfceSHannes Frederic Sowa 3660c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds /* 36631da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 36641da177e4SLinus Torvalds */ 36651da177e4SLinus Torvalds 36661da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 36671da177e4SLinus Torvalds 3668026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3669026359bcSTore Anderson router advertisements, start sending router solicitations. 36701da177e4SLinus Torvalds */ 36711da177e4SLinus Torvalds 36721ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 367311ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3674b173ee48SHannes Frederic Sowa send_rs = send_mld && 3675b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 3676b173ee48SHannes Frederic Sowa ifp->idev->cnf.rtr_solicits > 0 && 3677b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 36781ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 36791ec047ebSHannes Frederic Sowa 3680b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 3681b173ee48SHannes Frederic Sowa * Resend with proper ll now. 3682b173ee48SHannes Frederic Sowa */ 3683b173ee48SHannes Frederic Sowa if (send_mld) 3684b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 3685b173ee48SHannes Frederic Sowa 36861ec047ebSHannes Frederic Sowa if (send_rs) { 36871da177e4SLinus Torvalds /* 36881da177e4SLinus Torvalds * If a host as already performed a random delay 36891da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 36901da177e4SLinus Torvalds * to delay again before sending the first RS 36911da177e4SLinus Torvalds */ 36921ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3693b7b1bfceSHannes Frederic Sowa return; 36941ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 36951da177e4SLinus Torvalds 3696b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3697b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3698b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 36991da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3700b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3701b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3702b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3703b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 37041da177e4SLinus Torvalds } 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 3707e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3708e21e8467SStephen Hemminger { 3709c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3710c5e33bddSYOSHIFUJI Hideaki 3711c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3712502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 371321809fafSstephen hemminger spin_lock(&ifp->lock); 3714f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3715f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3716c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3717f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3718c5e33bddSYOSHIFUJI Hideaki } 3719c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3720c5e33bddSYOSHIFUJI Hideaki } 3721c5e33bddSYOSHIFUJI Hideaki 37221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 37231da177e4SLinus Torvalds struct if6_iter_state { 37243c40090aSDaniel Lezcano struct seq_net_private p; 37251da177e4SLinus Torvalds int bucket; 37261d578303SMihai Maruseac int offset; 37271da177e4SLinus Torvalds }; 37281da177e4SLinus Torvalds 37291d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 37301da177e4SLinus Torvalds { 37311da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 37321da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 37331218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 37341d578303SMihai Maruseac int p = 0; 37351da177e4SLinus Torvalds 37361d578303SMihai Maruseac /* initial bucket if pos is 0 */ 37371d578303SMihai Maruseac if (pos == 0) { 37381d578303SMihai Maruseac state->bucket = 0; 37391d578303SMihai Maruseac state->offset = 0; 37401d578303SMihai Maruseac } 37411d578303SMihai Maruseac 37421d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3743b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 37441d578303SMihai Maruseac addr_lst) { 37459f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 37469f0d3c27SEric Dumazet continue; 37471d578303SMihai Maruseac /* sync with offset */ 37481d578303SMihai Maruseac if (p < state->offset) { 37491d578303SMihai Maruseac p++; 37501d578303SMihai Maruseac continue; 37511d578303SMihai Maruseac } 37521d578303SMihai Maruseac state->offset++; 37531da177e4SLinus Torvalds return ifa; 37541da177e4SLinus Torvalds } 37551d578303SMihai Maruseac 37561d578303SMihai Maruseac /* prepare for next bucket */ 37571d578303SMihai Maruseac state->offset = 0; 37581d578303SMihai Maruseac p = 0; 37591d578303SMihai Maruseac } 3760c2e21293Sstephen hemminger return NULL; 3761c2e21293Sstephen hemminger } 37621da177e4SLinus Torvalds 3763c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3764c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 37651da177e4SLinus Torvalds { 37661da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 37671218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 37681da177e4SLinus Torvalds 3769b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 37709f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 37719f0d3c27SEric Dumazet continue; 37721d578303SMihai Maruseac state->offset++; 37731da177e4SLinus Torvalds return ifa; 37741d578303SMihai Maruseac } 37751da177e4SLinus Torvalds 3776c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 37771d578303SMihai Maruseac state->offset = 0; 3778b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 3779c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 37809f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 37819f0d3c27SEric Dumazet continue; 37821d578303SMihai Maruseac state->offset++; 3783c2e21293Sstephen hemminger return ifa; 3784c2e21293Sstephen hemminger } 3785c2e21293Sstephen hemminger } 3786c2e21293Sstephen hemminger 3787c2e21293Sstephen hemminger return NULL; 3788c2e21293Sstephen hemminger } 3789c2e21293Sstephen hemminger 37901da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 37914f70eccaSEric Dumazet __acquires(rcu_bh) 37921da177e4SLinus Torvalds { 37935c578aedSstephen hemminger rcu_read_lock_bh(); 37941d578303SMihai Maruseac return if6_get_first(seq, *pos); 37951da177e4SLinus Torvalds } 37961da177e4SLinus Torvalds 37971da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 37981da177e4SLinus Torvalds { 37991da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 38021da177e4SLinus Torvalds ++*pos; 38031da177e4SLinus Torvalds return ifa; 38041da177e4SLinus Torvalds } 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 38074f70eccaSEric Dumazet __releases(rcu_bh) 38081da177e4SLinus Torvalds { 38095c578aedSstephen hemminger rcu_read_unlock_bh(); 38101da177e4SLinus Torvalds } 38111da177e4SLinus Torvalds 38121da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 38131da177e4SLinus Torvalds { 38141da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 3815971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3816b071195dSHarvey Harrison &ifp->addr, 38171da177e4SLinus Torvalds ifp->idev->dev->ifindex, 38181da177e4SLinus Torvalds ifp->prefix_len, 38191da177e4SLinus Torvalds ifp->scope, 3820971a351cSJiri Pirko (u8) ifp->flags, 38211da177e4SLinus Torvalds ifp->idev->dev->name); 38221da177e4SLinus Torvalds return 0; 38231da177e4SLinus Torvalds } 38241da177e4SLinus Torvalds 382556b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 38261da177e4SLinus Torvalds .start = if6_seq_start, 38271da177e4SLinus Torvalds .next = if6_seq_next, 38281da177e4SLinus Torvalds .show = if6_seq_show, 38291da177e4SLinus Torvalds .stop = if6_seq_stop, 38301da177e4SLinus Torvalds }; 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 38331da177e4SLinus Torvalds { 38343c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3835cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 38361da177e4SLinus Torvalds } 38371da177e4SLinus Torvalds 38389a32144eSArjan van de Ven static const struct file_operations if6_fops = { 38391da177e4SLinus Torvalds .owner = THIS_MODULE, 38401da177e4SLinus Torvalds .open = if6_seq_open, 38411da177e4SLinus Torvalds .read = seq_read, 38421da177e4SLinus Torvalds .llseek = seq_lseek, 38433c40090aSDaniel Lezcano .release = seq_release_net, 38441da177e4SLinus Torvalds }; 38451da177e4SLinus Torvalds 38462c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 38471da177e4SLinus Torvalds { 3848d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 38491da177e4SLinus Torvalds return -ENOMEM; 38501da177e4SLinus Torvalds return 0; 38511da177e4SLinus Torvalds } 38521da177e4SLinus Torvalds 38532c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 38543c40090aSDaniel Lezcano { 3855ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 38563c40090aSDaniel Lezcano } 38573c40090aSDaniel Lezcano 38583c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 38593c40090aSDaniel Lezcano .init = if6_proc_net_init, 38603c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 38613c40090aSDaniel Lezcano }; 38623c40090aSDaniel Lezcano 38633c40090aSDaniel Lezcano int __init if6_proc_init(void) 38643c40090aSDaniel Lezcano { 38653c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 38663c40090aSDaniel Lezcano } 38673c40090aSDaniel Lezcano 38681da177e4SLinus Torvalds void if6_proc_exit(void) 38691da177e4SLinus Torvalds { 38703c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 38711da177e4SLinus Torvalds } 38721da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 38731da177e4SLinus Torvalds 387407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 38753b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3876b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 38773b9f9a1cSNoriaki TAKAMIYA { 38783b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3879c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3880ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 3881c2e21293Sstephen hemminger 38825c578aedSstephen hemminger rcu_read_lock_bh(); 3883b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 3884878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3885389f6612SDaniel Lezcano continue; 3886caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 38873b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 38883b9f9a1cSNoriaki TAKAMIYA ret = 1; 38893b9f9a1cSNoriaki TAKAMIYA break; 38903b9f9a1cSNoriaki TAKAMIYA } 38913b9f9a1cSNoriaki TAKAMIYA } 38925c578aedSstephen hemminger rcu_read_unlock_bh(); 38933b9f9a1cSNoriaki TAKAMIYA return ret; 38943b9f9a1cSNoriaki TAKAMIYA } 38953b9f9a1cSNoriaki TAKAMIYA #endif 38963b9f9a1cSNoriaki TAKAMIYA 38971da177e4SLinus Torvalds /* 38981da177e4SLinus Torvalds * Periodic address status verification 38991da177e4SLinus Torvalds */ 39001da177e4SLinus Torvalds 3901c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 39021da177e4SLinus Torvalds { 3903b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 39041da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 39051da177e4SLinus Torvalds int i; 39061da177e4SLinus Torvalds 3907c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 3908c15b1ccaSHannes Frederic Sowa 39095c578aedSstephen hemminger rcu_read_lock_bh(); 39101da177e4SLinus Torvalds now = jiffies; 3911b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 39121da177e4SLinus Torvalds 3913c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 39141da177e4SLinus Torvalds 39151da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 39161da177e4SLinus Torvalds restart: 3917c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 39181da177e4SLinus Torvalds unsigned long age; 39191da177e4SLinus Torvalds 3920fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 3921fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 3922fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 3923fad8da3eSYasushi Asano */ 3924fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 3925fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 39261da177e4SLinus Torvalds continue; 39271da177e4SLinus Torvalds 39281da177e4SLinus Torvalds spin_lock(&ifp->lock); 3929b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3930b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 39311da177e4SLinus Torvalds 39328f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 39338f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 39341da177e4SLinus Torvalds spin_unlock(&ifp->lock); 39351da177e4SLinus Torvalds in6_ifa_hold(ifp); 39361da177e4SLinus Torvalds ipv6_del_addr(ifp); 39371da177e4SLinus Torvalds goto restart; 39388f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 39398f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 39408f27ebb9SYOSHIFUJI Hideaki continue; 39411da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3942a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 39431da177e4SLinus Torvalds int deprecate = 0; 39441da177e4SLinus Torvalds 39451da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 39461da177e4SLinus Torvalds deprecate = 1; 39471da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 39481da177e4SLinus Torvalds } 39491da177e4SLinus Torvalds 3950fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 3951fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 39521da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 39551da177e4SLinus Torvalds 39561da177e4SLinus Torvalds if (deprecate) { 39571da177e4SLinus Torvalds in6_ifa_hold(ifp); 39581da177e4SLinus Torvalds 39591da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 39601da177e4SLinus Torvalds in6_ifa_put(ifp); 39611da177e4SLinus Torvalds goto restart; 39621da177e4SLinus Torvalds } 39631da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 39641da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 396588949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 396688949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 39671f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 396888949cf4Sstephen hemminger 39691da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 39701da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 39711da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 39721da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 39731da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 39741da177e4SLinus Torvalds ifp->regen_count++; 39751da177e4SLinus Torvalds in6_ifa_hold(ifp); 39761da177e4SLinus Torvalds in6_ifa_hold(ifpub); 39771da177e4SLinus Torvalds spin_unlock(&ifp->lock); 39785c578aedSstephen hemminger 3979291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3980291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3981291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 39821da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 39831da177e4SLinus Torvalds in6_ifa_put(ifpub); 39841da177e4SLinus Torvalds in6_ifa_put(ifp); 39851da177e4SLinus Torvalds goto restart; 39861da177e4SLinus Torvalds } 39871da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 39881da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 39891da177e4SLinus Torvalds spin_unlock(&ifp->lock); 39901da177e4SLinus Torvalds } else { 39911da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 39921da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 39931da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 39941da177e4SLinus Torvalds spin_unlock(&ifp->lock); 39951da177e4SLinus Torvalds } 39961da177e4SLinus Torvalds } 39971da177e4SLinus Torvalds } 39981da177e4SLinus Torvalds 3999b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4000b2db7564SYOSHIFUJI Hideaki next_sched = next; 4001b2db7564SYOSHIFUJI Hideaki 4002b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4003b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4004b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4005b2db7564SYOSHIFUJI Hideaki 4006b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4007b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4008b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4009b2db7564SYOSHIFUJI Hideaki 4010ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4011ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4012c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 40135c578aedSstephen hemminger rcu_read_unlock_bh(); 40141da177e4SLinus Torvalds } 40151da177e4SLinus Torvalds 4016c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4017c15b1ccaSHannes Frederic Sowa { 4018c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4019c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4020c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4021c15b1ccaSHannes Frederic Sowa } 4022c15b1ccaSHannes Frederic Sowa 4023c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4024c15b1ccaSHannes Frederic Sowa { 4025c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4026c15b1ccaSHannes Frederic Sowa } 4027c15b1ccaSHannes Frederic Sowa 4028caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4029caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4030461d8837SThomas Graf { 4031461d8837SThomas Graf struct in6_addr *pfx = NULL; 4032461d8837SThomas Graf 4033caeaba79SNicolas Dichtel *peer_pfx = NULL; 4034caeaba79SNicolas Dichtel 4035461d8837SThomas Graf if (addr) 4036461d8837SThomas Graf pfx = nla_data(addr); 4037461d8837SThomas Graf 4038461d8837SThomas Graf if (local) { 4039461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4040caeaba79SNicolas Dichtel *peer_pfx = pfx; 4041461d8837SThomas Graf pfx = nla_data(local); 4042461d8837SThomas Graf } 4043461d8837SThomas Graf 4044461d8837SThomas Graf return pfx; 4045461d8837SThomas Graf } 4046461d8837SThomas Graf 4047ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4048461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4049461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4050461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4051479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4052461d8837SThomas Graf }; 4053461d8837SThomas Graf 40541da177e4SLinus Torvalds static int 4055661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 40561da177e4SLinus Torvalds { 40573b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4058b933f716SThomas Graf struct ifaddrmsg *ifm; 4059b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4060caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 40616046d5b4SHeiner Kallweit u32 ifa_flags; 4062b933f716SThomas Graf int err; 40631da177e4SLinus Torvalds 4064b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4065b933f716SThomas Graf if (err < 0) 4066b933f716SThomas Graf return err; 4067b933f716SThomas Graf 4068b933f716SThomas Graf ifm = nlmsg_data(nlh); 4069caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 407063159f29SIan Morris if (!pfx) 40711da177e4SLinus Torvalds return -EINVAL; 40721da177e4SLinus Torvalds 40736046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 40746046d5b4SHeiner Kallweit 40756046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 40766046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 40776046d5b4SHeiner Kallweit 40786046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 40796046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 40801da177e4SLinus Torvalds } 40811da177e4SLinus Torvalds 4082479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 408355ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4084081bba5bSNoriaki TAKAMIYA { 40856f704992SYOSHIFUJI Hideaki u32 flags; 40866f704992SYOSHIFUJI Hideaki clock_t expires; 40874bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 408853bd6749SJiri Pirko bool was_managetempaddr; 40895b84efecSThomas Haller bool had_prefixroute; 409046d48046SYOSHIFUJI Hideaki 4091c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4092c15b1ccaSHannes Frederic Sowa 4093081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4094081bba5bSNoriaki TAKAMIYA return -EINVAL; 4095081bba5bSNoriaki TAKAMIYA 409653bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 409753bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 409853bd6749SJiri Pirko return -EINVAL; 409953bd6749SJiri Pirko 41004bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 41014bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 41024bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 41034bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 41046f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 41054bed72e4SYOSHIFUJI Hideaki } else { 41064bed72e4SYOSHIFUJI Hideaki expires = 0; 41074bed72e4SYOSHIFUJI Hideaki flags = 0; 41084bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 41096f704992SYOSHIFUJI Hideaki } 4110081bba5bSNoriaki TAKAMIYA 41114bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 41124bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 41134bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 411455ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 41154bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 41164bed72e4SYOSHIFUJI Hideaki } 4117081bba5bSNoriaki TAKAMIYA 4118081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 411953bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 41205b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 41215b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 412253bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4123761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4124761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 412553bd6749SJiri Pirko ifp->flags |= ifa_flags; 4126081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4127081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4128081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4129081bba5bSNoriaki TAKAMIYA 4130081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4131081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4132081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4133081bba5bSNoriaki TAKAMIYA 4134761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 413546d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 41366f704992SYOSHIFUJI Hideaki expires, flags); 41375b84efecSThomas Haller } else if (had_prefixroute) { 41385b84efecSThomas Haller enum cleanup_prefix_rt_t action; 41395b84efecSThomas Haller unsigned long rt_expires; 41405b84efecSThomas Haller 41415b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 41425b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 41435b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 41445b84efecSThomas Haller 41455b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 41465b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 41475b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 41485b84efecSThomas Haller } 4149761aac73SThomas Haller } 415053bd6749SJiri Pirko 415153bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 415253bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 415353bd6749SJiri Pirko valid_lft = prefered_lft = 0; 415453bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 415553bd6749SJiri Pirko !was_managetempaddr, jiffies); 415653bd6749SJiri Pirko } 415753bd6749SJiri Pirko 4158c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4159081bba5bSNoriaki TAKAMIYA 4160081bba5bSNoriaki TAKAMIYA return 0; 4161081bba5bSNoriaki TAKAMIYA } 4162081bba5bSNoriaki TAKAMIYA 4163081bba5bSNoriaki TAKAMIYA static int 4164661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 41651da177e4SLinus Torvalds { 41663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4167461d8837SThomas Graf struct ifaddrmsg *ifm; 4168461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4169caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 41707198f8ceSThomas Graf struct inet6_ifaddr *ifa; 41717198f8ceSThomas Graf struct net_device *dev; 417255ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4173479840ffSJiri Pirko u32 ifa_flags; 4174461d8837SThomas Graf int err; 41751da177e4SLinus Torvalds 4176461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4177461d8837SThomas Graf if (err < 0) 4178461d8837SThomas Graf return err; 4179461d8837SThomas Graf 4180461d8837SThomas Graf ifm = nlmsg_data(nlh); 4181caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 418263159f29SIan Morris if (!pfx) 41831da177e4SLinus Torvalds return -EINVAL; 41841da177e4SLinus Torvalds 4185461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 41860778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4187461d8837SThomas Graf 4188461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 41890778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4190461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4191461d8837SThomas Graf } else { 4192461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4193461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 41940778769dSNoriaki TAKAMIYA } 41950778769dSNoriaki TAKAMIYA 4196af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 419763159f29SIan Morris if (!dev) 41987198f8ceSThomas Graf return -ENODEV; 4199081bba5bSNoriaki TAKAMIYA 4200479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4201479840ffSJiri Pirko 420255ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4203761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 420493a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 420555ebaef1SNoriaki TAKAMIYA 42061cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 420763159f29SIan Morris if (!ifa) { 42087198f8ceSThomas Graf /* 42097198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4210db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 42117198f8ceSThomas Graf */ 4212caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4213af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4214af284937SDaniel Lezcano preferred_lft, valid_lft); 42151da177e4SLinus Torvalds } 42161da177e4SLinus Torvalds 42177198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 42187198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 42197198f8ceSThomas Graf err = -EEXIST; 42207198f8ceSThomas Graf else 422155ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 42227198f8ceSThomas Graf 42237198f8ceSThomas Graf in6_ifa_put(ifa); 42247198f8ceSThomas Graf 42257198f8ceSThomas Graf return err; 42267198f8ceSThomas Graf } 42277198f8ceSThomas Graf 4228479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4229101bb229SThomas Graf u8 scope, int ifindex) 4230101bb229SThomas Graf { 4231101bb229SThomas Graf struct ifaddrmsg *ifm; 4232101bb229SThomas Graf 4233101bb229SThomas Graf ifm = nlmsg_data(nlh); 4234101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4235101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4236101bb229SThomas Graf ifm->ifa_flags = flags; 4237101bb229SThomas Graf ifm->ifa_scope = scope; 4238101bb229SThomas Graf ifm->ifa_index = ifindex; 4239101bb229SThomas Graf } 4240101bb229SThomas Graf 424185486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 424285486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 424385486af0SThomas Graf { 424485486af0SThomas Graf struct ifa_cacheinfo ci; 424585486af0SThomas Graf 424618a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 424718a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 424885486af0SThomas Graf ci.ifa_prefered = preferred; 424985486af0SThomas Graf ci.ifa_valid = valid; 425085486af0SThomas Graf 425185486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 425285486af0SThomas Graf } 425385486af0SThomas Graf 4254101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4255101bb229SThomas Graf { 4256101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4257101bb229SThomas Graf return RT_SCOPE_HOST; 4258101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4259101bb229SThomas Graf return RT_SCOPE_LINK; 4260101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4261101bb229SThomas Graf return RT_SCOPE_SITE; 4262101bb229SThomas Graf else 4263101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4264101bb229SThomas Graf } 4265101bb229SThomas Graf 42660ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 42670ab6803bSThomas Graf { 4268339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4269caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4270339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4271479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4272479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 42730ab6803bSThomas Graf } 4274c5396a31SYOSHIFUJI Hideaki 42751da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 427615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 42771da177e4SLinus Torvalds { 42781da177e4SLinus Torvalds struct nlmsghdr *nlh; 427985486af0SThomas Graf u32 preferred, valid; 42801da177e4SLinus Torvalds 428115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 428263159f29SIan Morris if (!nlh) 428326932566SPatrick McHardy return -EMSGSIZE; 42840ab6803bSThomas Graf 4285101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4286101bb229SThomas Graf ifa->idev->dev->ifindex); 4287101bb229SThomas Graf 4288fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4289fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 429085486af0SThomas Graf preferred = ifa->prefered_lft; 429185486af0SThomas Graf valid = ifa->valid_lft; 429285486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 42931da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4294a1faa698SJens Rosenboom if (preferred > tval) 429585486af0SThomas Graf preferred -= tval; 4296a1faa698SJens Rosenboom else 4297a1faa698SJens Rosenboom preferred = 0; 4298f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4299f56619fcSBen Hutchings if (valid > tval) 430085486af0SThomas Graf valid -= tval; 4301f56619fcSBen Hutchings else 4302f56619fcSBen Hutchings valid = 0; 4303f56619fcSBen Hutchings } 43041da177e4SLinus Torvalds } 43051da177e4SLinus Torvalds } else { 430685486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 430785486af0SThomas Graf valid = INFINITY_LIFE_TIME; 43081da177e4SLinus Torvalds } 430985486af0SThomas Graf 43107996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4311930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4312930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4313caeaba79SNicolas Dichtel goto error; 4314caeaba79SNicolas Dichtel } else 4315930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4316caeaba79SNicolas Dichtel goto error; 4317caeaba79SNicolas Dichtel 4318caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4319caeaba79SNicolas Dichtel goto error; 432085486af0SThomas Graf 4321479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4322479840ffSJiri Pirko goto error; 4323479840ffSJiri Pirko 4324053c095aSJohannes Berg nlmsg_end(skb, nlh); 4325053c095aSJohannes Berg return 0; 4326caeaba79SNicolas Dichtel 4327caeaba79SNicolas Dichtel error: 4328caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4329caeaba79SNicolas Dichtel return -EMSGSIZE; 43301da177e4SLinus Torvalds } 43311da177e4SLinus Torvalds 43321da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 433315e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 43341da177e4SLinus Torvalds { 43351da177e4SLinus Torvalds struct nlmsghdr *nlh; 4336101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4337101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4340101bb229SThomas Graf scope = RT_SCOPE_SITE; 4341101bb229SThomas Graf 434215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 434363159f29SIan Morris if (!nlh) 434426932566SPatrick McHardy return -EMSGSIZE; 43450ab6803bSThomas Graf 4346101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4347930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 43480ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 434926932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 435026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 435126932566SPatrick McHardy return -EMSGSIZE; 435226932566SPatrick McHardy } 435385486af0SThomas Graf 4354053c095aSJohannes Berg nlmsg_end(skb, nlh); 4355053c095aSJohannes Berg return 0; 43561da177e4SLinus Torvalds } 43571da177e4SLinus Torvalds 43581da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 435915e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 43601da177e4SLinus Torvalds { 43611da177e4SLinus Torvalds struct nlmsghdr *nlh; 4362101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4363101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 43641da177e4SLinus Torvalds 43651da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4366101bb229SThomas Graf scope = RT_SCOPE_SITE; 4367101bb229SThomas Graf 436815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 436963159f29SIan Morris if (!nlh) 437026932566SPatrick McHardy return -EMSGSIZE; 43710ab6803bSThomas Graf 4372101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4373930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 43740ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 437526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 437626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 437726932566SPatrick McHardy return -EMSGSIZE; 437826932566SPatrick McHardy } 437985486af0SThomas Graf 4380053c095aSJohannes Berg nlmsg_end(skb, nlh); 4381053c095aSJohannes Berg return 0; 43821da177e4SLinus Torvalds } 43831da177e4SLinus Torvalds 4384e21e8467SStephen Hemminger enum addr_type_t { 43851da177e4SLinus Torvalds UNICAST_ADDR, 43861da177e4SLinus Torvalds MULTICAST_ADDR, 43871da177e4SLinus Torvalds ANYCAST_ADDR, 43881da177e4SLinus Torvalds }; 43891da177e4SLinus Torvalds 4390234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4391234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4392234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4393234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 43941da177e4SLinus Torvalds { 43951da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 43961da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4397234b27c3SEric Dumazet int err = 1; 4398234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 43991da177e4SLinus Torvalds 44001da177e4SLinus Torvalds read_lock_bh(&idev->lock); 44011da177e4SLinus Torvalds switch (type) { 4402502a2ffdSstephen hemminger case UNICAST_ADDR: { 4403502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4404502a2ffdSstephen hemminger 4405ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4406502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4407502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 44081da177e4SLinus Torvalds continue; 44095d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 441015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 44115d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 44125d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 44135d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4414053c095aSJohannes Berg if (err < 0) 4415bcd32326SEric Dumazet break; 441663998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 44171da177e4SLinus Torvalds } 44181da177e4SLinus Torvalds break; 4419502a2ffdSstephen hemminger } 44201da177e4SLinus Torvalds case MULTICAST_ADDR: 44211da177e4SLinus Torvalds /* multicast address */ 44221da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 44231da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 44241da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 44251da177e4SLinus Torvalds continue; 44265d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 442715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 44285d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 44295d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 44305d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4431053c095aSJohannes Berg if (err < 0) 4432bcd32326SEric Dumazet break; 44331da177e4SLinus Torvalds } 44341da177e4SLinus Torvalds break; 44351da177e4SLinus Torvalds case ANYCAST_ADDR: 44361da177e4SLinus Torvalds /* anycast address */ 44371da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 44381da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 44391da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 44401da177e4SLinus Torvalds continue; 44415d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 444215e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 44435d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 44445d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 44455d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4446053c095aSJohannes Berg if (err < 0) 4447bcd32326SEric Dumazet break; 44481da177e4SLinus Torvalds } 44491da177e4SLinus Torvalds break; 44501da177e4SLinus Torvalds default: 44511da177e4SLinus Torvalds break; 44521da177e4SLinus Torvalds } 44531da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4454234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4455234b27c3SEric Dumazet return err; 4456234b27c3SEric Dumazet } 44575d5619b4SYOSHIFUJI Hideaki 4458234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4459234b27c3SEric Dumazet enum addr_type_t type) 4460234b27c3SEric Dumazet { 4461234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4462234b27c3SEric Dumazet int h, s_h; 4463234b27c3SEric Dumazet int idx, ip_idx; 4464234b27c3SEric Dumazet int s_idx, s_ip_idx; 4465234b27c3SEric Dumazet struct net_device *dev; 4466234b27c3SEric Dumazet struct inet6_dev *idev; 4467234b27c3SEric Dumazet struct hlist_head *head; 4468234b27c3SEric Dumazet 4469234b27c3SEric Dumazet s_h = cb->args[0]; 4470234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4471234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4472234b27c3SEric Dumazet 4473234b27c3SEric Dumazet rcu_read_lock(); 447463998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4475234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4476234b27c3SEric Dumazet idx = 0; 4477234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4478b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4479234b27c3SEric Dumazet if (idx < s_idx) 4480234b27c3SEric Dumazet goto cont; 44814b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4482234b27c3SEric Dumazet s_ip_idx = 0; 4483234b27c3SEric Dumazet ip_idx = 0; 4484e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4485e21e8467SStephen Hemminger if (!idev) 4486234b27c3SEric Dumazet goto cont; 4487234b27c3SEric Dumazet 4488234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 44897b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4490234b27c3SEric Dumazet goto done; 44917562f876SPavel Emelianov cont: 44927562f876SPavel Emelianov idx++; 44931da177e4SLinus Torvalds } 4494234b27c3SEric Dumazet } 4495234b27c3SEric Dumazet done: 4496234b27c3SEric Dumazet rcu_read_unlock(); 4497234b27c3SEric Dumazet cb->args[0] = h; 4498234b27c3SEric Dumazet cb->args[1] = idx; 4499234b27c3SEric Dumazet cb->args[2] = ip_idx; 4500234b27c3SEric Dumazet 45011da177e4SLinus Torvalds return skb->len; 45021da177e4SLinus Torvalds } 45031da177e4SLinus Torvalds 45041da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 45051da177e4SLinus Torvalds { 45061da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4507b854272bSDenis V. Lunev 45081da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 45091da177e4SLinus Torvalds } 45101da177e4SLinus Torvalds 45111da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 45121da177e4SLinus Torvalds { 45131da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4514b854272bSDenis V. Lunev 45151da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 45161da177e4SLinus Torvalds } 45171da177e4SLinus Torvalds 45181da177e4SLinus Torvalds 45191da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 45201da177e4SLinus Torvalds { 45211da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4522b854272bSDenis V. Lunev 45231da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 45241da177e4SLinus Torvalds } 45251da177e4SLinus Torvalds 4526661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 45276c223828SNoriaki TAKAMIYA { 45283b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 45291b29fc2cSThomas Graf struct ifaddrmsg *ifm; 45301b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4531caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 45326c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 45336c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 45346c223828SNoriaki TAKAMIYA struct sk_buff *skb; 45356c223828SNoriaki TAKAMIYA int err; 45366c223828SNoriaki TAKAMIYA 45371b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 45381b29fc2cSThomas Graf if (err < 0) 45391b29fc2cSThomas Graf goto errout; 45406c223828SNoriaki TAKAMIYA 4541caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 454263159f29SIan Morris if (!addr) { 45431b29fc2cSThomas Graf err = -EINVAL; 45441b29fc2cSThomas Graf goto errout; 45451b29fc2cSThomas Graf } 45461b29fc2cSThomas Graf 45471b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 45486c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 45496fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 45506c223828SNoriaki TAKAMIYA 4551e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4552e21e8467SStephen Hemminger if (!ifa) { 45531b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 45541b29fc2cSThomas Graf goto errout; 45556c223828SNoriaki TAKAMIYA } 45566c223828SNoriaki TAKAMIYA 4557e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4558e21e8467SStephen Hemminger if (!skb) { 45591b29fc2cSThomas Graf err = -ENOBUFS; 45601b29fc2cSThomas Graf goto errout_ifa; 45611b29fc2cSThomas Graf } 45621b29fc2cSThomas Graf 456315e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 45646c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 456526932566SPatrick McHardy if (err < 0) { 456626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 456726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 456826932566SPatrick McHardy kfree_skb(skb); 456926932566SPatrick McHardy goto errout_ifa; 457026932566SPatrick McHardy } 457115e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 45721b29fc2cSThomas Graf errout_ifa: 45736c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 45741b29fc2cSThomas Graf errout: 45756c223828SNoriaki TAKAMIYA return err; 45766c223828SNoriaki TAKAMIYA } 45776c223828SNoriaki TAKAMIYA 45781da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 45791da177e4SLinus Torvalds { 45801da177e4SLinus Torvalds struct sk_buff *skb; 4581c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 45825d620266SThomas Graf int err = -ENOBUFS; 45831da177e4SLinus Torvalds 45840ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 458563159f29SIan Morris if (!skb) 45865d620266SThomas Graf goto errout; 45875d620266SThomas Graf 45885d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 458926932566SPatrick McHardy if (err < 0) { 459026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 459126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 459226932566SPatrick McHardy kfree_skb(skb); 459326932566SPatrick McHardy goto errout; 459426932566SPatrick McHardy } 45951ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 45961ce85fe4SPablo Neira Ayuso return; 45975d620266SThomas Graf errout: 45985d620266SThomas Graf if (err < 0) 45996fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 46001da177e4SLinus Torvalds } 46011da177e4SLinus Torvalds 4602b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 46031da177e4SLinus Torvalds __s32 *array, int bytes) 46041da177e4SLinus Torvalds { 460504561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 460604561c1fSThomas Graf 46071da177e4SLinus Torvalds memset(array, 0, bytes); 46081da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 46091da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 46101da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 46111da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 46121da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 46131da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 46141da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 46151da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 461693908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 461793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 461893908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 461993908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 46201da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4621fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4622fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4623fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4624fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 46251da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 46261da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 46271da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 46281da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 46291da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 46301da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 463165f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 46328013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 4633c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4634930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4635930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 463693908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 463793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4638fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 463909c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 464009c884d4SYOSHIFUJI Hideaki #endif 4641930d6ff2SYOSHIFUJI Hideaki #endif 4642fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 46430bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 464495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 464595c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 46467fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 464795c385b4SNeil Horman #endif 46487bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 46497bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 46507bc570c8SYOSHIFUJI Hideaki #endif 4651778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 46521b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4653c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 46545cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4655b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4656d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4657c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 465835103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 46593d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 46603985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 46611da177e4SLinus Torvalds } 46621da177e4SLinus Torvalds 4663b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4664b382b191SThomas Graf { 4665b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4666b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4667b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4668b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4669f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4670f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4671b382b191SThomas Graf } 4672b382b191SThomas Graf 4673339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4674339bf98fSThomas Graf { 4675339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4676339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4677339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4678339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4679339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 46800344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 4681b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4682339bf98fSThomas Graf } 4683c5396a31SYOSHIFUJI Hideaki 4684be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 46857d720c3eSTejun Heo int items, int bytes) 46867f7d9a6bSHerbert Xu { 46877f7d9a6bSHerbert Xu int i; 46887f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 46897f7d9a6bSHerbert Xu BUG_ON(pad < 0); 46907f7d9a6bSHerbert Xu 46917f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 46927f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 46937f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 4694be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 46957f7d9a6bSHerbert Xu 46967f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 46977f7d9a6bSHerbert Xu } 46987f7d9a6bSHerbert Xu 4699698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 4700a3a77372SRaghavendra K T int bytes, size_t syncpoff) 47014ce3c183SEric Dumazet { 4702a3a77372SRaghavendra K T int i, c; 4703a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 4704a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 4705a3a77372SRaghavendra K T 47064ce3c183SEric Dumazet BUG_ON(pad < 0); 47074ce3c183SEric Dumazet 4708a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 4709a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 47104ce3c183SEric Dumazet 4711a3a77372SRaghavendra K T for_each_possible_cpu(c) { 4712a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 4713a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 4714a3a77372SRaghavendra K T } 4715a3a77372SRaghavendra K T 4716a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 4717a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 47184ce3c183SEric Dumazet } 47194ce3c183SEric Dumazet 47207f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 47217f7d9a6bSHerbert Xu int bytes) 47227f7d9a6bSHerbert Xu { 47237f7d9a6bSHerbert Xu switch (attrtype) { 47247f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 4725a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 4726a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 47277f7d9a6bSHerbert Xu break; 47287f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 4729be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 47307f7d9a6bSHerbert Xu break; 47317f7d9a6bSHerbert Xu } 47327f7d9a6bSHerbert Xu } 47337f7d9a6bSHerbert Xu 4734b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 47351da177e4SLinus Torvalds { 4736bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 47371da177e4SLinus Torvalds struct ifla_cacheinfo ci; 47381da177e4SLinus Torvalds 4739c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 4740c78679e8SDavid S. Miller goto nla_put_failure; 47411da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 474224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 474324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 47441f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 4745c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4746c78679e8SDavid S. Miller goto nla_put_failure; 4747bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 474863159f29SIan Morris if (!nla) 474904561c1fSThomas Graf goto nla_put_failure; 4750bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 47511da177e4SLinus Torvalds 4752bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 4753bf99f1bdSYOSHIFUJI Hideaki 4754bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 475563159f29SIan Morris if (!nla) 4756bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4757bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4758bf99f1bdSYOSHIFUJI Hideaki 4759bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 476063159f29SIan Morris if (!nla) 4761bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4762bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 47631da177e4SLinus Torvalds 4764f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 476563159f29SIan Morris if (!nla) 4766f53adae4SDaniel Borkmann goto nla_put_failure; 4767bc91b0f0SJiri Pirko 4768bc91b0f0SJiri Pirko if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 4769bc91b0f0SJiri Pirko goto nla_put_failure; 4770bc91b0f0SJiri Pirko 4771f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 4772f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 4773f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 4774f53adae4SDaniel Borkmann 4775b382b191SThomas Graf return 0; 4776b382b191SThomas Graf 4777b382b191SThomas Graf nla_put_failure: 4778b382b191SThomas Graf return -EMSGSIZE; 4779b382b191SThomas Graf } 4780b382b191SThomas Graf 4781b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev) 4782b382b191SThomas Graf { 4783b382b191SThomas Graf if (!__in6_dev_get(dev)) 4784b382b191SThomas Graf return 0; 4785b382b191SThomas Graf 4786b382b191SThomas Graf return inet6_ifla6_size(); 4787b382b191SThomas Graf } 4788b382b191SThomas Graf 4789b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 4790b382b191SThomas Graf { 4791b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 4792b382b191SThomas Graf 4793b382b191SThomas Graf if (!idev) 4794b382b191SThomas Graf return -ENODATA; 4795b382b191SThomas Graf 4796b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4797b382b191SThomas Graf return -EMSGSIZE; 4798b382b191SThomas Graf 4799b382b191SThomas Graf return 0; 4800b382b191SThomas Graf } 4801b382b191SThomas Graf 4802f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 4803f53adae4SDaniel Borkmann { 4804f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 4805f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 4806fc403832SDaniel Borkmann bool update_rs = false; 4807dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 4808f53adae4SDaniel Borkmann 4809c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4810c15b1ccaSHannes Frederic Sowa 481163159f29SIan Morris if (!token) 4812f53adae4SDaniel Borkmann return -EINVAL; 4813f53adae4SDaniel Borkmann if (ipv6_addr_any(token)) 4814f53adae4SDaniel Borkmann return -EINVAL; 4815f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 4816f53adae4SDaniel Borkmann return -EINVAL; 4817f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 4818f53adae4SDaniel Borkmann return -EINVAL; 4819f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 4820f53adae4SDaniel Borkmann return -EINVAL; 4821f53adae4SDaniel Borkmann 4822f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4823f53adae4SDaniel Borkmann 4824f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 4825f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 4826f53adae4SDaniel Borkmann 4827f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 4828f53adae4SDaniel Borkmann 4829dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 4830dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 4831dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 4832fc403832SDaniel Borkmann 4833fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 4834fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 4835fc403832SDaniel Borkmann */ 4836f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 4837fc403832SDaniel Borkmann update_rs = true; 4838fc403832SDaniel Borkmann } 4839f53adae4SDaniel Borkmann 4840f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4841fc403832SDaniel Borkmann 484277ecaaceSHannes Frederic Sowa if (update_rs) { 4843f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 484477ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 484577ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 484677ecaaceSHannes Frederic Sowa } 4847f53adae4SDaniel Borkmann 4848f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 4849f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 4850f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 4851617fe29dSDaniel Borkmann if (ifp->tokenized) { 4852f53adae4SDaniel Borkmann ifp->valid_lft = 0; 4853f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 4854f53adae4SDaniel Borkmann } 4855f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 4856f53adae4SDaniel Borkmann } 4857f53adae4SDaniel Borkmann 4858f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 4859b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 4860c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4861f53adae4SDaniel Borkmann return 0; 4862f53adae4SDaniel Borkmann } 4863f53adae4SDaniel Borkmann 486411b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 486511b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 486611b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 486711b1f828SDaniel Borkmann }; 486811b1f828SDaniel Borkmann 486911b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 487011b1f828SDaniel Borkmann const struct nlattr *nla) 487111b1f828SDaniel Borkmann { 487211b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 487311b1f828SDaniel Borkmann 487411b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 487511b1f828SDaniel Borkmann return -EAFNOSUPPORT; 487611b1f828SDaniel Borkmann 487711b1f828SDaniel Borkmann return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 487811b1f828SDaniel Borkmann } 487911b1f828SDaniel Borkmann 4880f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 4881f53adae4SDaniel Borkmann { 4882f53adae4SDaniel Borkmann int err = -EINVAL; 4883f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 4884f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 4885f53adae4SDaniel Borkmann 4886f53adae4SDaniel Borkmann if (!idev) 4887f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 4888f53adae4SDaniel Borkmann 4889f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 4890f53adae4SDaniel Borkmann BUG(); 4891f53adae4SDaniel Borkmann 4892bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 4893f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 4894bc91b0f0SJiri Pirko if (err) 4895bc91b0f0SJiri Pirko return err; 4896bc91b0f0SJiri Pirko } 4897bc91b0f0SJiri Pirko 4898bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 4899bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 4900bc91b0f0SJiri Pirko 4901bc91b0f0SJiri Pirko if (mode != IN6_ADDR_GEN_MODE_EUI64 && 4902622c81d5SHannes Frederic Sowa mode != IN6_ADDR_GEN_MODE_NONE && 4903622c81d5SHannes Frederic Sowa mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY) 4904bc91b0f0SJiri Pirko return -EINVAL; 4905622c81d5SHannes Frederic Sowa 4906622c81d5SHannes Frederic Sowa if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 4907622c81d5SHannes Frederic Sowa !idev->cnf.stable_secret.initialized && 4908622c81d5SHannes Frederic Sowa !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) 4909622c81d5SHannes Frederic Sowa return -EINVAL; 4910622c81d5SHannes Frederic Sowa 4911bc91b0f0SJiri Pirko idev->addr_gen_mode = mode; 4912bc91b0f0SJiri Pirko err = 0; 4913bc91b0f0SJiri Pirko } 4914f53adae4SDaniel Borkmann 4915f53adae4SDaniel Borkmann return err; 4916f53adae4SDaniel Borkmann } 4917f53adae4SDaniel Borkmann 4918b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 491915e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 4920b382b191SThomas Graf { 4921b382b191SThomas Graf struct net_device *dev = idev->dev; 4922b382b191SThomas Graf struct ifinfomsg *hdr; 4923b382b191SThomas Graf struct nlmsghdr *nlh; 4924b382b191SThomas Graf void *protoinfo; 4925b382b191SThomas Graf 492615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 492763159f29SIan Morris if (!nlh) 4928b382b191SThomas Graf return -EMSGSIZE; 4929b382b191SThomas Graf 4930b382b191SThomas Graf hdr = nlmsg_data(nlh); 4931b382b191SThomas Graf hdr->ifi_family = AF_INET6; 4932b382b191SThomas Graf hdr->__ifi_pad = 0; 4933b382b191SThomas Graf hdr->ifi_type = dev->type; 4934b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 4935b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 4936b382b191SThomas Graf hdr->ifi_change = 0; 4937b382b191SThomas Graf 4938c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4939c78679e8SDavid S. Miller (dev->addr_len && 4940c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4941c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4942a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 49430344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 49440344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 49450344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 4946c78679e8SDavid S. Miller goto nla_put_failure; 4947b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 494863159f29SIan Morris if (!protoinfo) 4949b382b191SThomas Graf goto nla_put_failure; 4950b382b191SThomas Graf 4951b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4952b382b191SThomas Graf goto nla_put_failure; 4953b382b191SThomas Graf 495404561c1fSThomas Graf nla_nest_end(skb, protoinfo); 4955053c095aSJohannes Berg nlmsg_end(skb, nlh); 4956053c095aSJohannes Berg return 0; 49571da177e4SLinus Torvalds 495804561c1fSThomas Graf nla_put_failure: 495926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 496026932566SPatrick McHardy return -EMSGSIZE; 49611da177e4SLinus Torvalds } 49621da177e4SLinus Torvalds 49631da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 49641da177e4SLinus Torvalds { 49653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 496684d2697dSEric Dumazet int h, s_h; 4967434a8a58SDavid S. Miller int idx = 0, s_idx; 49681da177e4SLinus Torvalds struct net_device *dev; 49691da177e4SLinus Torvalds struct inet6_dev *idev; 497084d2697dSEric Dumazet struct hlist_head *head; 49711da177e4SLinus Torvalds 497284d2697dSEric Dumazet s_h = cb->args[0]; 497384d2697dSEric Dumazet s_idx = cb->args[1]; 497484d2697dSEric Dumazet 497584d2697dSEric Dumazet rcu_read_lock(); 497684d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 49777562f876SPavel Emelianov idx = 0; 497884d2697dSEric Dumazet head = &net->dev_index_head[h]; 4979b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 49801da177e4SLinus Torvalds if (idx < s_idx) 49817562f876SPavel Emelianov goto cont; 498284d2697dSEric Dumazet idev = __in6_dev_get(dev); 498384d2697dSEric Dumazet if (!idev) 49847562f876SPavel Emelianov goto cont; 498584d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 498615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 498784d2697dSEric Dumazet cb->nlh->nlmsg_seq, 4988053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 498984d2697dSEric Dumazet goto out; 49907562f876SPavel Emelianov cont: 49917562f876SPavel Emelianov idx++; 49921da177e4SLinus Torvalds } 499384d2697dSEric Dumazet } 499484d2697dSEric Dumazet out: 499584d2697dSEric Dumazet rcu_read_unlock(); 499684d2697dSEric Dumazet cb->args[1] = idx; 499784d2697dSEric Dumazet cb->args[0] = h; 49981da177e4SLinus Torvalds 49991da177e4SLinus Torvalds return skb->len; 50001da177e4SLinus Torvalds } 50011da177e4SLinus Torvalds 50021da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 50031da177e4SLinus Torvalds { 50041da177e4SLinus Torvalds struct sk_buff *skb; 5005c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 50068d7a76c9SThomas Graf int err = -ENOBUFS; 50071da177e4SLinus Torvalds 5008339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 500963159f29SIan Morris if (!skb) 50108d7a76c9SThomas Graf goto errout; 50118d7a76c9SThomas Graf 50128d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 501326932566SPatrick McHardy if (err < 0) { 501426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 501526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 501626932566SPatrick McHardy kfree_skb(skb); 501726932566SPatrick McHardy goto errout; 501826932566SPatrick McHardy } 50195f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 50201ce85fe4SPablo Neira Ayuso return; 50218d7a76c9SThomas Graf errout: 50228d7a76c9SThomas Graf if (err < 0) 50235f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 50241da177e4SLinus Torvalds } 50251da177e4SLinus Torvalds 5026339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5027339bf98fSThomas Graf { 5028339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5029339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5030339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5031339bf98fSThomas Graf } 5032c5396a31SYOSHIFUJI Hideaki 50331da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 503415e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5035b6544c0bSJamal Hadi Salim int event, unsigned int flags) 50361da177e4SLinus Torvalds { 50371da177e4SLinus Torvalds struct prefixmsg *pmsg; 50381da177e4SLinus Torvalds struct nlmsghdr *nlh; 50391da177e4SLinus Torvalds struct prefix_cacheinfo ci; 50401da177e4SLinus Torvalds 504115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 504263159f29SIan Morris if (!nlh) 504326932566SPatrick McHardy return -EMSGSIZE; 50446051e2f4SThomas Graf 50456051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 50461da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 50478a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 50488a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 50491da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 50501da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 50511da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 50528a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 50531da177e4SLinus Torvalds pmsg->prefix_flags = 0; 50541da177e4SLinus Torvalds if (pinfo->onlink) 50551da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 50561da177e4SLinus Torvalds if (pinfo->autoconf) 50571da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 50581da177e4SLinus Torvalds 5059c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5060c78679e8SDavid S. Miller goto nla_put_failure; 50611da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 50621da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5063c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5064c78679e8SDavid S. Miller goto nla_put_failure; 5065053c095aSJohannes Berg nlmsg_end(skb, nlh); 5066053c095aSJohannes Berg return 0; 50671da177e4SLinus Torvalds 50686051e2f4SThomas Graf nla_put_failure: 506926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 507026932566SPatrick McHardy return -EMSGSIZE; 50711da177e4SLinus Torvalds } 50721da177e4SLinus Torvalds 50731da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 50741da177e4SLinus Torvalds struct prefix_info *pinfo) 50751da177e4SLinus Torvalds { 50761da177e4SLinus Torvalds struct sk_buff *skb; 5077c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 50788c384bfaSThomas Graf int err = -ENOBUFS; 50791da177e4SLinus Torvalds 5080339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 508163159f29SIan Morris if (!skb) 50828c384bfaSThomas Graf goto errout; 50838c384bfaSThomas Graf 50848c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 508526932566SPatrick McHardy if (err < 0) { 508626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 508726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 508826932566SPatrick McHardy kfree_skb(skb); 508926932566SPatrick McHardy goto errout; 509026932566SPatrick McHardy } 50911ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 50921ce85fe4SPablo Neira Ayuso return; 50938c384bfaSThomas Graf errout: 50948c384bfaSThomas Graf if (err < 0) 50956fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 50961da177e4SLinus Torvalds } 50971da177e4SLinus Torvalds 50981da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 50991da177e4SLinus Torvalds { 510063998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 510163998ac2SNicolas Dichtel 5102c15b1ccaSHannes Frederic Sowa if (event) 5103c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5104c15b1ccaSHannes Frederic Sowa 51051da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 51061da177e4SLinus Torvalds 51071da177e4SLinus Torvalds switch (event) { 51081da177e4SLinus Torvalds case RTM_NEWADDR: 510995c385b4SNeil Horman /* 511095c385b4SNeil Horman * If the address was optimistic 511195c385b4SNeil Horman * we inserted the route at the start of 511295c385b4SNeil Horman * our DAD process, so we don't need 511395c385b4SNeil Horman * to do it again 511495c385b4SNeil Horman */ 511595c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 511640e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 51171da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 51181da177e4SLinus Torvalds addrconf_join_anycast(ifp); 51197996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5120caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5121caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 51221da177e4SLinus Torvalds break; 51231da177e4SLinus Torvalds case RTM_DELADDR: 51241da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 51251da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 51261da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 51277996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5128caeaba79SNicolas Dichtel struct rt6_info *rt; 5129caeaba79SNicolas Dichtel 5130e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5131e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 51328e3d5be7SMartin KaFai Lau if (rt) 51338e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5134caeaba79SNicolas Dichtel } 5135d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 513693fa159aSstephen hemminger 51378e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 5138705f1c86SHannes Frederic Sowa 5139705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 51401da177e4SLinus Torvalds break; 51411da177e4SLinus Torvalds } 514263998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 51431da177e4SLinus Torvalds } 51441da177e4SLinus Torvalds 51451da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 51461da177e4SLinus Torvalds { 51478814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 51481da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 51491da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 51508814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 51511da177e4SLinus Torvalds } 51521da177e4SLinus Torvalds 51531da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 51541da177e4SLinus Torvalds 51551da177e4SLinus Torvalds static 5156fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 51571da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 51581da177e4SLinus Torvalds { 51591da177e4SLinus Torvalds int *valp = ctl->data; 51601da177e4SLinus Torvalds int val = *valp; 516188af182eSEric W. Biederman loff_t pos = *ppos; 5162fe2c6338SJoe Perches struct ctl_table lctl; 51631da177e4SLinus Torvalds int ret; 51641da177e4SLinus Torvalds 5165013d97e9SFrancesco Ruggeri /* 5166013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5167013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5168013d97e9SFrancesco Ruggeri */ 5169013d97e9SFrancesco Ruggeri lctl = *ctl; 5170013d97e9SFrancesco Ruggeri lctl.data = &val; 5171013d97e9SFrancesco Ruggeri 5172013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 51731da177e4SLinus Torvalds 5174c8fecf22SPavel Emelyanov if (write) 5175b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 517688af182eSEric W. Biederman if (ret) 517788af182eSEric W. Biederman *ppos = pos; 51781da177e4SLinus Torvalds return ret; 51791da177e4SLinus Torvalds } 51801da177e4SLinus Torvalds 518177751427SMarcelo Leitner static 518277751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 518377751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 518477751427SMarcelo Leitner { 518577751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 518677751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 518777751427SMarcelo Leitner struct ctl_table lctl; 518877751427SMarcelo Leitner 518977751427SMarcelo Leitner lctl = *ctl; 519077751427SMarcelo Leitner lctl.extra1 = &min_mtu; 519177751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 519277751427SMarcelo Leitner 519377751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 519477751427SMarcelo Leitner } 519577751427SMarcelo Leitner 519656d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 519756d417b1SBrian Haley { 519875538c2bSCong Wang struct netdev_notifier_info info; 519975538c2bSCong Wang 520056d417b1SBrian Haley if (!idev || !idev->dev) 520156d417b1SBrian Haley return; 520256d417b1SBrian Haley 520375538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 520456d417b1SBrian Haley if (idev->cnf.disable_ipv6) 520575538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 520656d417b1SBrian Haley else 520775538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 520856d417b1SBrian Haley } 520956d417b1SBrian Haley 521056d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 521156d417b1SBrian Haley { 521256d417b1SBrian Haley struct net_device *dev; 521356d417b1SBrian Haley struct inet6_dev *idev; 521456d417b1SBrian Haley 521556d417b1SBrian Haley rcu_read_lock(); 5216c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 521756d417b1SBrian Haley idev = __in6_dev_get(dev); 521856d417b1SBrian Haley if (idev) { 521956d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 522056d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 522156d417b1SBrian Haley if (changed) 522256d417b1SBrian Haley dev_disable_change(idev); 522356d417b1SBrian Haley } 522456d417b1SBrian Haley } 5225c6d14c84SEric Dumazet rcu_read_unlock(); 522656d417b1SBrian Haley } 522756d417b1SBrian Haley 5228013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 522956d417b1SBrian Haley { 523056d417b1SBrian Haley struct net *net; 5231013d97e9SFrancesco Ruggeri int old; 5232013d97e9SFrancesco Ruggeri 5233013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5234013d97e9SFrancesco Ruggeri return restart_syscall(); 523556d417b1SBrian Haley 523656d417b1SBrian Haley net = (struct net *)table->extra2; 5237013d97e9SFrancesco Ruggeri old = *p; 5238013d97e9SFrancesco Ruggeri *p = newf; 523956d417b1SBrian Haley 5240013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5241013d97e9SFrancesco Ruggeri rtnl_unlock(); 524256d417b1SBrian Haley return 0; 524388af182eSEric W. Biederman } 524456d417b1SBrian Haley 524556d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 524656d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 524756d417b1SBrian Haley addrconf_disable_change(net, newf); 5248013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 524956d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 525056d417b1SBrian Haley 525156d417b1SBrian Haley rtnl_unlock(); 525256d417b1SBrian Haley return 0; 525356d417b1SBrian Haley } 525456d417b1SBrian Haley 525556d417b1SBrian Haley static 5256fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 525756d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 525856d417b1SBrian Haley { 525956d417b1SBrian Haley int *valp = ctl->data; 526056d417b1SBrian Haley int val = *valp; 526188af182eSEric W. Biederman loff_t pos = *ppos; 5262fe2c6338SJoe Perches struct ctl_table lctl; 526356d417b1SBrian Haley int ret; 526456d417b1SBrian Haley 5265013d97e9SFrancesco Ruggeri /* 5266013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5267013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5268013d97e9SFrancesco Ruggeri */ 5269013d97e9SFrancesco Ruggeri lctl = *ctl; 5270013d97e9SFrancesco Ruggeri lctl.data = &val; 5271013d97e9SFrancesco Ruggeri 5272013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 527356d417b1SBrian Haley 527456d417b1SBrian Haley if (write) 527556d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 527688af182eSEric W. Biederman if (ret) 527788af182eSEric W. Biederman *ppos = pos; 527856d417b1SBrian Haley return ret; 527956d417b1SBrian Haley } 528056d417b1SBrian Haley 5281c92d5491Sstephen hemminger static 5282c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5283c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5284c92d5491Sstephen hemminger { 5285c92d5491Sstephen hemminger int *valp = ctl->data; 5286c92d5491Sstephen hemminger int ret; 5287c92d5491Sstephen hemminger int old, new; 5288c92d5491Sstephen hemminger 5289c92d5491Sstephen hemminger old = *valp; 5290c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5291c92d5491Sstephen hemminger new = *valp; 5292c92d5491Sstephen hemminger 5293c92d5491Sstephen hemminger if (write && old != new) { 5294c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5295c92d5491Sstephen hemminger 5296c92d5491Sstephen hemminger if (!rtnl_trylock()) 5297c92d5491Sstephen hemminger return restart_syscall(); 5298c92d5491Sstephen hemminger 5299c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5300c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5301c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5302c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5303c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5304c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5305c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5306c92d5491Sstephen hemminger net->ipv6.devconf_all); 5307c92d5491Sstephen hemminger else { 5308c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5309c92d5491Sstephen hemminger 5310c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5311c92d5491Sstephen hemminger idev->dev->ifindex, 5312c92d5491Sstephen hemminger &idev->cnf); 5313c92d5491Sstephen hemminger } 5314c92d5491Sstephen hemminger rtnl_unlock(); 5315c92d5491Sstephen hemminger } 5316c92d5491Sstephen hemminger 5317c92d5491Sstephen hemminger return ret; 5318c92d5491Sstephen hemminger } 5319c92d5491Sstephen hemminger 53203d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 53213d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 53223d1bec99SHannes Frederic Sowa loff_t *ppos) 53233d1bec99SHannes Frederic Sowa { 53243d1bec99SHannes Frederic Sowa int err; 53253d1bec99SHannes Frederic Sowa struct in6_addr addr; 53263d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 53273d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5328622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 53293d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 53303d1bec99SHannes Frederic Sowa 5331622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5332622c81d5SHannes Frederic Sowa return -EIO; 5333622c81d5SHannes Frederic Sowa 53343d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 53353d1bec99SHannes Frederic Sowa lctl.data = str; 53363d1bec99SHannes Frederic Sowa 53373d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 53383d1bec99SHannes Frederic Sowa return restart_syscall(); 53393d1bec99SHannes Frederic Sowa 53403d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 53413d1bec99SHannes Frederic Sowa err = -EIO; 53423d1bec99SHannes Frederic Sowa goto out; 53433d1bec99SHannes Frederic Sowa } 53443d1bec99SHannes Frederic Sowa 53453d1bec99SHannes Frederic Sowa if (!write) { 53463d1bec99SHannes Frederic Sowa err = snprintf(str, sizeof(str), "%pI6", 53473d1bec99SHannes Frederic Sowa &secret->secret); 53483d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 53493d1bec99SHannes Frederic Sowa err = -EIO; 53503d1bec99SHannes Frederic Sowa goto out; 53513d1bec99SHannes Frederic Sowa } 53523d1bec99SHannes Frederic Sowa } 53533d1bec99SHannes Frederic Sowa 53543d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 53553d1bec99SHannes Frederic Sowa if (err || !write) 53563d1bec99SHannes Frederic Sowa goto out; 53573d1bec99SHannes Frederic Sowa 53583d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 53593d1bec99SHannes Frederic Sowa err = -EIO; 53603d1bec99SHannes Frederic Sowa goto out; 53613d1bec99SHannes Frederic Sowa } 53623d1bec99SHannes Frederic Sowa 53633d1bec99SHannes Frederic Sowa secret->initialized = true; 53643d1bec99SHannes Frederic Sowa secret->secret = addr; 53653d1bec99SHannes Frederic Sowa 5366622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5367622c81d5SHannes Frederic Sowa struct net_device *dev; 5368622c81d5SHannes Frederic Sowa 5369622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5370622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5371622c81d5SHannes Frederic Sowa 5372622c81d5SHannes Frederic Sowa if (idev) { 5373622c81d5SHannes Frederic Sowa idev->addr_gen_mode = 5374622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5375622c81d5SHannes Frederic Sowa } 5376622c81d5SHannes Frederic Sowa } 5377622c81d5SHannes Frederic Sowa } else { 5378622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5379622c81d5SHannes Frederic Sowa 5380622c81d5SHannes Frederic Sowa idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5381622c81d5SHannes Frederic Sowa } 5382622c81d5SHannes Frederic Sowa 53833d1bec99SHannes Frederic Sowa out: 53843d1bec99SHannes Frederic Sowa rtnl_unlock(); 53853d1bec99SHannes Frederic Sowa 53863d1bec99SHannes Frederic Sowa return err; 53873d1bec99SHannes Frederic Sowa } 5388c92d5491Sstephen hemminger 538935103d11SAndy Gospodarek static 539035103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 539135103d11SAndy Gospodarek int write, 539235103d11SAndy Gospodarek void __user *buffer, 539335103d11SAndy Gospodarek size_t *lenp, 539435103d11SAndy Gospodarek loff_t *ppos) 539535103d11SAndy Gospodarek { 539635103d11SAndy Gospodarek int *valp = ctl->data; 539735103d11SAndy Gospodarek int val = *valp; 539835103d11SAndy Gospodarek loff_t pos = *ppos; 539935103d11SAndy Gospodarek struct ctl_table lctl; 540035103d11SAndy Gospodarek int ret; 540135103d11SAndy Gospodarek 540235103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 540335103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 540435103d11SAndy Gospodarek */ 540535103d11SAndy Gospodarek lctl = *ctl; 540635103d11SAndy Gospodarek lctl.data = &val; 540735103d11SAndy Gospodarek 540835103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 540935103d11SAndy Gospodarek 541035103d11SAndy Gospodarek if (write) 541135103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 541235103d11SAndy Gospodarek if (ret) 541335103d11SAndy Gospodarek *ppos = pos; 541435103d11SAndy Gospodarek return ret; 541535103d11SAndy Gospodarek } 541635103d11SAndy Gospodarek 54171da177e4SLinus Torvalds static struct addrconf_sysctl_table 54181da177e4SLinus Torvalds { 54191da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 5420fe2c6338SJoe Perches struct ctl_table addrconf_vars[DEVCONF_MAX+1]; 5421ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 54221da177e4SLinus Torvalds .sysctl_header = NULL, 54231da177e4SLinus Torvalds .addrconf_vars = { 54241da177e4SLinus Torvalds { 54251da177e4SLinus Torvalds .procname = "forwarding", 54261da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 54271da177e4SLinus Torvalds .maxlen = sizeof(int), 54281da177e4SLinus Torvalds .mode = 0644, 54296d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 54301da177e4SLinus Torvalds }, 54311da177e4SLinus Torvalds { 54321da177e4SLinus Torvalds .procname = "hop_limit", 54331da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 54341da177e4SLinus Torvalds .maxlen = sizeof(int), 54351da177e4SLinus Torvalds .mode = 0644, 54361da177e4SLinus Torvalds .proc_handler = proc_dointvec, 54371da177e4SLinus Torvalds }, 54381da177e4SLinus Torvalds { 54391da177e4SLinus Torvalds .procname = "mtu", 54401da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 54411da177e4SLinus Torvalds .maxlen = sizeof(int), 54421da177e4SLinus Torvalds .mode = 0644, 544377751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 54441da177e4SLinus Torvalds }, 54451da177e4SLinus Torvalds { 54461da177e4SLinus Torvalds .procname = "accept_ra", 54471da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 54481da177e4SLinus Torvalds .maxlen = sizeof(int), 54491da177e4SLinus Torvalds .mode = 0644, 54506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 54511da177e4SLinus Torvalds }, 54521da177e4SLinus Torvalds { 54531da177e4SLinus Torvalds .procname = "accept_redirects", 54541da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 54551da177e4SLinus Torvalds .maxlen = sizeof(int), 54561da177e4SLinus Torvalds .mode = 0644, 54576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 54581da177e4SLinus Torvalds }, 54591da177e4SLinus Torvalds { 54601da177e4SLinus Torvalds .procname = "autoconf", 54611da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 54621da177e4SLinus Torvalds .maxlen = sizeof(int), 54631da177e4SLinus Torvalds .mode = 0644, 54646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 54651da177e4SLinus Torvalds }, 54661da177e4SLinus Torvalds { 54671da177e4SLinus Torvalds .procname = "dad_transmits", 54681da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 54691da177e4SLinus Torvalds .maxlen = sizeof(int), 54701da177e4SLinus Torvalds .mode = 0644, 54716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 54721da177e4SLinus Torvalds }, 54731da177e4SLinus Torvalds { 54741da177e4SLinus Torvalds .procname = "router_solicitations", 54751da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 54761da177e4SLinus Torvalds .maxlen = sizeof(int), 54771da177e4SLinus Torvalds .mode = 0644, 54786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 54791da177e4SLinus Torvalds }, 54801da177e4SLinus Torvalds { 54811da177e4SLinus Torvalds .procname = "router_solicitation_interval", 54821da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 54831da177e4SLinus Torvalds .maxlen = sizeof(int), 54841da177e4SLinus Torvalds .mode = 0644, 54856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 54861da177e4SLinus Torvalds }, 54871da177e4SLinus Torvalds { 54881da177e4SLinus Torvalds .procname = "router_solicitation_delay", 54891da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 54901da177e4SLinus Torvalds .maxlen = sizeof(int), 54911da177e4SLinus Torvalds .mode = 0644, 54926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 54931da177e4SLinus Torvalds }, 54941da177e4SLinus Torvalds { 54951da177e4SLinus Torvalds .procname = "force_mld_version", 54961da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 54971da177e4SLinus Torvalds .maxlen = sizeof(int), 54981da177e4SLinus Torvalds .mode = 0644, 54996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55001da177e4SLinus Torvalds }, 5501fc4eba58SHannes Frederic Sowa { 5502fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 5503fc4eba58SHannes Frederic Sowa .data = 5504fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 5505fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5506fc4eba58SHannes Frederic Sowa .mode = 0644, 5507fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5508fc4eba58SHannes Frederic Sowa }, 5509fc4eba58SHannes Frederic Sowa { 5510fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 5511fc4eba58SHannes Frederic Sowa .data = 5512fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 5513fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5514fc4eba58SHannes Frederic Sowa .mode = 0644, 5515fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5516fc4eba58SHannes Frederic Sowa }, 55171da177e4SLinus Torvalds { 55181da177e4SLinus Torvalds .procname = "use_tempaddr", 55191da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 55201da177e4SLinus Torvalds .maxlen = sizeof(int), 55211da177e4SLinus Torvalds .mode = 0644, 55226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55231da177e4SLinus Torvalds }, 55241da177e4SLinus Torvalds { 55251da177e4SLinus Torvalds .procname = "temp_valid_lft", 55261da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 55271da177e4SLinus Torvalds .maxlen = sizeof(int), 55281da177e4SLinus Torvalds .mode = 0644, 55296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55301da177e4SLinus Torvalds }, 55311da177e4SLinus Torvalds { 55321da177e4SLinus Torvalds .procname = "temp_prefered_lft", 55331da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 55341da177e4SLinus Torvalds .maxlen = sizeof(int), 55351da177e4SLinus Torvalds .mode = 0644, 55366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55371da177e4SLinus Torvalds }, 55381da177e4SLinus Torvalds { 55391da177e4SLinus Torvalds .procname = "regen_max_retry", 55401da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 55411da177e4SLinus Torvalds .maxlen = sizeof(int), 55421da177e4SLinus Torvalds .mode = 0644, 55436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55441da177e4SLinus Torvalds }, 55451da177e4SLinus Torvalds { 55461da177e4SLinus Torvalds .procname = "max_desync_factor", 55471da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 55481da177e4SLinus Torvalds .maxlen = sizeof(int), 55491da177e4SLinus Torvalds .mode = 0644, 55506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55511da177e4SLinus Torvalds }, 55521da177e4SLinus Torvalds { 55531da177e4SLinus Torvalds .procname = "max_addresses", 55541da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 55551da177e4SLinus Torvalds .maxlen = sizeof(int), 55561da177e4SLinus Torvalds .mode = 0644, 55576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 55581da177e4SLinus Torvalds }, 55591da177e4SLinus Torvalds { 556065f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 556165f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 556265f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 556365f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 55646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 556565f5c7c1SYOSHIFUJI Hideaki }, 556665f5c7c1SYOSHIFUJI Hideaki { 55678013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 55688013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 55698013d1d7SHangbin Liu .maxlen = sizeof(int), 55708013d1d7SHangbin Liu .mode = 0644, 55718013d1d7SHangbin Liu .proc_handler = proc_dointvec, 55728013d1d7SHangbin Liu }, 55738013d1d7SHangbin Liu { 5574c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 5575c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 5576c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5577c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 55786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5579c4fd30ebSYOSHIFUJI Hideaki }, 5580930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5581930d6ff2SYOSHIFUJI Hideaki { 5582930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 5583930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 5584930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5585930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 55866d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5587930d6ff2SYOSHIFUJI Hideaki }, 558852e16356SYOSHIFUJI Hideaki { 558952e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 559052e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 559152e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 559252e16356SYOSHIFUJI Hideaki .mode = 0644, 55936d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 559452e16356SYOSHIFUJI Hideaki }, 5595fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 559609c884d4SYOSHIFUJI Hideaki { 559709c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 559809c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 559909c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 560009c884d4SYOSHIFUJI Hideaki .mode = 0644, 56016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 560209c884d4SYOSHIFUJI Hideaki }, 560309c884d4SYOSHIFUJI Hideaki #endif 5604930d6ff2SYOSHIFUJI Hideaki #endif 5605c4fd30ebSYOSHIFUJI Hideaki { 5606fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 5607fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 5608fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5609fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 5610c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 5611fbea49e1SYOSHIFUJI Hideaki }, 5612fbea49e1SYOSHIFUJI Hideaki { 56130bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 56140bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 56150bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 56160bcbc926SYOSHIFUJI Hideaki .mode = 0644, 56176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56180bcbc926SYOSHIFUJI Hideaki }, 561995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 562095c385b4SNeil Horman { 562195c385b4SNeil Horman .procname = "optimistic_dad", 562295c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 562395c385b4SNeil Horman .maxlen = sizeof(int), 562495c385b4SNeil Horman .mode = 0644, 56256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 562695c385b4SNeil Horman 562795c385b4SNeil Horman }, 56287fd2561eSErik Kline { 56297fd2561eSErik Kline .procname = "use_optimistic", 56307fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 56317fd2561eSErik Kline .maxlen = sizeof(int), 56327fd2561eSErik Kline .mode = 0644, 56337fd2561eSErik Kline .proc_handler = proc_dointvec, 56347fd2561eSErik Kline 56357fd2561eSErik Kline }, 563695c385b4SNeil Horman #endif 56377bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 56387bc570c8SYOSHIFUJI Hideaki { 56397bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 56407bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 56417bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5642a4e6db07SDavid S. Miller .mode = 0444, 56436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56447bc570c8SYOSHIFUJI Hideaki }, 56457bc570c8SYOSHIFUJI Hideaki #endif 56460bcbc926SYOSHIFUJI Hideaki { 5647778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 5648778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 5649778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5650778d80beSYOSHIFUJI Hideaki .mode = 0644, 565156d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 5652778d80beSYOSHIFUJI Hideaki }, 5653778d80beSYOSHIFUJI Hideaki { 56541b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 56551b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 56561b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 56571b34be74SYOSHIFUJI Hideaki .mode = 0644, 56586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56591b34be74SYOSHIFUJI Hideaki }, 56601b34be74SYOSHIFUJI Hideaki { 5661f7734fdfSOctavian Purdila .procname = "force_tllao", 5662f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 5663f7734fdfSOctavian Purdila .maxlen = sizeof(int), 5664f7734fdfSOctavian Purdila .mode = 0644, 5665f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 5666f7734fdfSOctavian Purdila }, 5667f7734fdfSOctavian Purdila { 56685cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 56695cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 56705cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 56715cb04436SHannes Frederic Sowa .mode = 0644, 56725cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 56735cb04436SHannes Frederic Sowa }, 56745cb04436SHannes Frederic Sowa { 5675b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 5676b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 5677b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 5678b800c3b9SHannes Frederic Sowa .mode = 0644, 5679b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 5680b800c3b9SHannes Frederic Sowa }, 5681b800c3b9SHannes Frederic Sowa { 5682d9333196SBen Greear .procname = "accept_ra_from_local", 5683d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 5684d9333196SBen Greear .maxlen = sizeof(int), 5685d9333196SBen Greear .mode = 0644, 5686d9333196SBen Greear .proc_handler = proc_dointvec, 5687d9333196SBen Greear }, 5688d9333196SBen Greear { 5689c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 5690c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 5691c2943f14SHarout Hedeshian .maxlen = sizeof(int), 5692c2943f14SHarout Hedeshian .mode = 0644, 5693c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 5694c2943f14SHarout Hedeshian }, 5695c2943f14SHarout Hedeshian { 56963d1bec99SHannes Frederic Sowa .procname = "stable_secret", 56973d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 56983d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 56993d1bec99SHannes Frederic Sowa .mode = 0600, 57003d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 57013d1bec99SHannes Frederic Sowa }, 57023d1bec99SHannes Frederic Sowa { 57033985e8a3SErik Kline .procname = "use_oif_addrs_only", 57043985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 57053985e8a3SErik Kline .maxlen = sizeof(int), 57063985e8a3SErik Kline .mode = 0644, 57073985e8a3SErik Kline .proc_handler = proc_dointvec, 570835103d11SAndy Gospodarek }, 570935103d11SAndy Gospodarek { 571035103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 571135103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 571235103d11SAndy Gospodarek .maxlen = sizeof(int), 571335103d11SAndy Gospodarek .mode = 0644, 571435103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 57153985e8a3SErik Kline }, 57163985e8a3SErik Kline { 5717f8572d8fSEric W. Biederman /* sentinel */ 57181da177e4SLinus Torvalds } 57191da177e4SLinus Torvalds }, 57201da177e4SLinus Torvalds }; 57211da177e4SLinus Torvalds 5722bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 5723f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 57241da177e4SLinus Torvalds { 57251da177e4SLinus Torvalds int i; 57261da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 57276105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 57281dab6222SPavel Emelyanov 5729af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 573063159f29SIan Morris if (!t) 5731f68635e6SPavel Emelyanov goto out; 5732f68635e6SPavel Emelyanov 57331da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 57341da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 57351da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 5736bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 57371da177e4SLinus Torvalds } 57381da177e4SLinus Torvalds 57396105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 57401da177e4SLinus Torvalds 57416105e293SEric W. Biederman t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 574263159f29SIan Morris if (!t->sysctl_header) 57436105e293SEric W. Biederman goto free; 5744f68635e6SPavel Emelyanov 57451da177e4SLinus Torvalds p->sysctl = t; 574695897312SPavel Emelyanov return 0; 57471da177e4SLinus Torvalds 57481da177e4SLinus Torvalds free: 57491da177e4SLinus Torvalds kfree(t); 5750f68635e6SPavel Emelyanov out: 575195897312SPavel Emelyanov return -ENOBUFS; 57521da177e4SLinus Torvalds } 57531da177e4SLinus Torvalds 5754408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 5755f52295a9SPavel Emelyanov { 5756408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 5757f52295a9SPavel Emelyanov 575863159f29SIan Morris if (!p->sysctl) 5759408c4768SPavel Emelyanov return; 5760408c4768SPavel Emelyanov 5761408c4768SPavel Emelyanov t = p->sysctl; 57621da177e4SLinus Torvalds p->sysctl = NULL; 5763ff538818SLucian Adrian Grijincu unregister_net_sysctl_table(t->sysctl_header); 57641da177e4SLinus Torvalds kfree(t); 57651da177e4SLinus Torvalds } 5766408c4768SPavel Emelyanov 5767a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 5768408c4768SPavel Emelyanov { 5769a317a2f1SWANG Cong int err; 5770a317a2f1SWANG Cong 5771a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 5772a317a2f1SWANG Cong return -EINVAL; 5773a317a2f1SWANG Cong 5774a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 5775f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 5776a317a2f1SWANG Cong if (err) 5777a317a2f1SWANG Cong return err; 5778a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 5779f8572d8fSEric W. Biederman idev, &idev->cnf); 5780a317a2f1SWANG Cong if (err) 5781a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 5782a317a2f1SWANG Cong 5783a317a2f1SWANG Cong return err; 5784408c4768SPavel Emelyanov } 5785408c4768SPavel Emelyanov 5786408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 5787408c4768SPavel Emelyanov { 5788408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 5789408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 57901da177e4SLinus Torvalds } 57911da177e4SLinus Torvalds 57921da177e4SLinus Torvalds 57931da177e4SLinus Torvalds #endif 57941da177e4SLinus Torvalds 57952c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 5796e0da5a48SPavel Emelyanov { 5797a79ca223SHong Zhiguo int err = -ENOMEM; 5798e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 5799e0da5a48SPavel Emelyanov 5800a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 580163159f29SIan Morris if (!all) 5802e0da5a48SPavel Emelyanov goto err_alloc_all; 5803e0da5a48SPavel Emelyanov 5804a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 580563159f29SIan Morris if (!dflt) 5806e0da5a48SPavel Emelyanov goto err_alloc_dflt; 5807a79ca223SHong Zhiguo 580856d417b1SBrian Haley /* these will be inherited by all namespaces */ 580956d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 581056d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 5811e0da5a48SPavel Emelyanov 58123d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 58133d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 58143d1bec99SHannes Frederic Sowa 5815e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 5816e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 5817e0da5a48SPavel Emelyanov 5818e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5819f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 5820e0da5a48SPavel Emelyanov if (err < 0) 5821e0da5a48SPavel Emelyanov goto err_reg_all; 5822e0da5a48SPavel Emelyanov 5823f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 5824e0da5a48SPavel Emelyanov if (err < 0) 5825e0da5a48SPavel Emelyanov goto err_reg_dflt; 5826e0da5a48SPavel Emelyanov #endif 5827e0da5a48SPavel Emelyanov return 0; 5828e0da5a48SPavel Emelyanov 5829e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5830e0da5a48SPavel Emelyanov err_reg_dflt: 5831e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 5832e0da5a48SPavel Emelyanov err_reg_all: 5833e0da5a48SPavel Emelyanov kfree(dflt); 5834e0da5a48SPavel Emelyanov #endif 5835e0da5a48SPavel Emelyanov err_alloc_dflt: 5836e0da5a48SPavel Emelyanov kfree(all); 5837e0da5a48SPavel Emelyanov err_alloc_all: 5838e0da5a48SPavel Emelyanov return err; 5839e0da5a48SPavel Emelyanov } 5840e0da5a48SPavel Emelyanov 58412c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 5842e0da5a48SPavel Emelyanov { 5843e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5844e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 5845e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 5846e0da5a48SPavel Emelyanov #endif 5847e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 5848e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 5849e0da5a48SPavel Emelyanov } 5850e0da5a48SPavel Emelyanov 5851e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 5852e0da5a48SPavel Emelyanov .init = addrconf_init_net, 5853e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 5854e0da5a48SPavel Emelyanov }; 5855e0da5a48SPavel Emelyanov 5856207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 5857b382b191SThomas Graf .family = AF_INET6, 5858b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 5859b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 586011b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 5861f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 5862b382b191SThomas Graf }; 5863b382b191SThomas Graf 58641da177e4SLinus Torvalds /* 58651da177e4SLinus Torvalds * Init / cleanup code 58661da177e4SLinus Torvalds */ 58671da177e4SLinus Torvalds 58681da177e4SLinus Torvalds int __init addrconf_init(void) 58691da177e4SLinus Torvalds { 5870a317a2f1SWANG Cong struct inet6_dev *idev; 5871c2e21293Sstephen hemminger int i, err; 58722a8cc6c8SYOSHIFUJI Hideaki 5873e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 5874e21e8467SStephen Hemminger if (err < 0) { 5875f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 5876f3213831SJoe Perches __func__, err); 58772cc6d2bfSNeil Horman goto out; 58782a8cc6c8SYOSHIFUJI Hideaki } 58791da177e4SLinus Torvalds 58802cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 58812cc6d2bfSNeil Horman if (err < 0) 58822cc6d2bfSNeil Horman goto out_addrlabel; 5883e0da5a48SPavel Emelyanov 5884c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 5885c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 5886c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 5887c15b1ccaSHannes Frederic Sowa goto out_nowq; 5888c15b1ccaSHannes Frederic Sowa } 5889c15b1ccaSHannes Frederic Sowa 58901da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 58911da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 58921da177e4SLinus Torvalds * before it can bring up and give link-local addresses 58931da177e4SLinus Torvalds * to other devices which are up. 58941da177e4SLinus Torvalds * 58951da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 58961da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 58971da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 58981da177e4SLinus Torvalds * So this causes the notifier registry below to try and 58991da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 59001da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 59011da177e4SLinus Torvalds * devices to fail to get a link-local address. 59021da177e4SLinus Torvalds * 59031da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 59041da177e4SLinus Torvalds * loopback_dev first by hand. 59051da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 59061da177e4SLinus Torvalds * device and it being up should be removed. 59071da177e4SLinus Torvalds */ 59081da177e4SLinus Torvalds rtnl_lock(); 5909a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 59101da177e4SLinus Torvalds rtnl_unlock(); 5911a317a2f1SWANG Cong if (IS_ERR(idev)) { 5912a317a2f1SWANG Cong err = PTR_ERR(idev); 5913e0da5a48SPavel Emelyanov goto errlo; 5914a317a2f1SWANG Cong } 59151da177e4SLinus Torvalds 5916c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5917c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 5918c2e21293Sstephen hemminger 59191da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 59201da177e4SLinus Torvalds 5921c15b1ccaSHannes Frederic Sowa addrconf_verify(); 5922c127ea2cSThomas Graf 59233678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 5924b382b191SThomas Graf 5925c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 5926c7ac8679SGreg Rose NULL); 5927c127ea2cSThomas Graf if (err < 0) 5928c127ea2cSThomas Graf goto errout; 5929c127ea2cSThomas Graf 5930c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 5931c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 5932c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 5933c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 5934c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 5935c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 5936c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 5937c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 5938c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 593976f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 59407a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 5941c127ea2cSThomas Graf 59422a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 59432a8cc6c8SYOSHIFUJI Hideaki 59441da177e4SLinus Torvalds return 0; 5945c127ea2cSThomas Graf errout: 5946b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 5947c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 5948e0da5a48SPavel Emelyanov errlo: 5949c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 5950c15b1ccaSHannes Frederic Sowa out_nowq: 5951e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 59522cc6d2bfSNeil Horman out_addrlabel: 59532cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 59542cc6d2bfSNeil Horman out: 5955c127ea2cSThomas Graf return err; 59561da177e4SLinus Torvalds } 59571da177e4SLinus Torvalds 595809f7709fSDaniel Lezcano void addrconf_cleanup(void) 59591da177e4SLinus Torvalds { 5960176c39afSDaniel Lezcano struct net_device *dev; 59611da177e4SLinus Torvalds int i; 59621da177e4SLinus Torvalds 59631da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 5964e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 59652cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 59661da177e4SLinus Torvalds 59671da177e4SLinus Torvalds rtnl_lock(); 59681da177e4SLinus Torvalds 5969b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 5970b382b191SThomas Graf 5971176c39afSDaniel Lezcano /* clean dev list */ 5972176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 5973176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 5974176c39afSDaniel Lezcano continue; 5975176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 5976176c39afSDaniel Lezcano } 5977176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 5978176c39afSDaniel Lezcano 59791da177e4SLinus Torvalds /* 59801da177e4SLinus Torvalds * Check hash table. 59811da177e4SLinus Torvalds */ 59825c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 5983c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5984c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 59855c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 5986c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 59871da177e4SLinus Torvalds rtnl_unlock(); 5988c15b1ccaSHannes Frederic Sowa 5989c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 59901da177e4SLinus Torvalds } 5991