Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 Address [auto]configuration |
| 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * Changes: |
| 17 | * |
| 18 | * Janos Farkas : delete timer on ifdown |
| 19 | * <chexum@bankinf.banki.hu> |
| 20 | * Andi Kleen : kill double kfree on module |
| 21 | * unload. |
| 22 | * Maciej W. Rozycki : FDDI support |
| 23 | * sekiya@USAGI : Don't send too many RS |
| 24 | * packets. |
| 25 | * yoshfuji@USAGI : Fixed interval between DAD |
| 26 | * packets. |
| 27 | * YOSHIFUJI Hideaki @USAGI : improved accuracy of |
| 28 | * address validation timer. |
| 29 | * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) |
| 30 | * support. |
| 31 | * Yuji SEKIYA @USAGI : Don't assign a same IPv6 |
| 32 | * address on a same interface. |
| 33 | * YOSHIFUJI Hideaki @USAGI : ARCnet support |
| 34 | * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to |
| 35 | * seq_file. |
YOSHIFUJI Hideaki | b1cacb6 | 2005-11-08 09:38:12 -0800 | [diff] [blame] | 36 | * YOSHIFUJI Hideaki @USAGI : improved source address |
| 37 | * selection; consider scope, |
| 38 | * status etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | */ |
| 40 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 41 | #define pr_fmt(fmt) "IPv6: " fmt |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/errno.h> |
| 44 | #include <linux/types.h> |
Ilpo Järvinen | a0bffff | 2009-03-21 13:36:17 -0700 | [diff] [blame] | 45 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/socket.h> |
| 47 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/net.h> |
| 49 | #include <linux/in6.h> |
| 50 | #include <linux/netdevice.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 51 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/if_arp.h> |
| 53 | #include <linux/if_arcnet.h> |
| 54 | #include <linux/if_infiniband.h> |
| 55 | #include <linux/route.h> |
| 56 | #include <linux/inetdevice.h> |
| 57 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 58 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_SYSCTL |
| 60 | #include <linux/sysctl.h> |
| 61 | #endif |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 62 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/delay.h> |
| 64 | #include <linux/notifier.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 65 | #include <linux/string.h> |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 66 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 68 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <net/sock.h> |
| 70 | #include <net/snmp.h> |
| 71 | |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 72 | #include <net/af_ieee802154.h> |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 73 | #include <net/firewire.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include <net/ipv6.h> |
| 75 | #include <net/protocol.h> |
| 76 | #include <net/ndisc.h> |
| 77 | #include <net/ip6_route.h> |
| 78 | #include <net/addrconf.h> |
| 79 | #include <net/tcp.h> |
| 80 | #include <net/ip.h> |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 81 | #include <net/netlink.h> |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 82 | #include <net/pkt_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <linux/if_tunnel.h> |
| 84 | #include <linux/rtnetlink.h> |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 85 | #include <linux/netconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | #ifdef CONFIG_IPV6_PRIVACY |
| 88 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #endif |
| 90 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 91 | #include <linux/uaccess.h> |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 92 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | #include <linux/proc_fs.h> |
| 95 | #include <linux/seq_file.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 96 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* Set to 3 to get tracing... */ |
| 99 | #define ACONF_DEBUG 2 |
| 100 | |
| 101 | #if ACONF_DEBUG >= 3 |
| 102 | #define ADBG(x) printk x |
| 103 | #else |
| 104 | #define ADBG(x) |
| 105 | #endif |
| 106 | |
| 107 | #define INFINITY_LIFE_TIME 0xFFFFFFFF |
Thomas Graf | 18a31e1 | 2010-11-17 04:12:02 +0000 | [diff] [blame] | 108 | |
| 109 | static inline u32 cstamp_delta(unsigned long cstamp) |
| 110 | { |
| 111 | return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | #ifdef CONFIG_SYSCTL |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 115 | static void addrconf_sysctl_register(struct inet6_dev *idev); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 116 | static void addrconf_sysctl_unregister(struct inet6_dev *idev); |
| 117 | #else |
| 118 | static inline void addrconf_sysctl_register(struct inet6_dev *idev) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) |
| 123 | { |
| 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | #ifdef CONFIG_IPV6_PRIVACY |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 128 | static void __ipv6_regen_rndid(struct inet6_dev *idev); |
| 129 | static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static void ipv6_regen_rndid(unsigned long data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif |
| 132 | |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 133 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static int ipv6_count_addresses(struct inet6_dev *idev); |
| 135 | |
| 136 | /* |
| 137 | * Configured unicast address hash table |
| 138 | */ |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 139 | static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 140 | static DEFINE_SPINLOCK(addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static void addrconf_verify(unsigned long); |
| 143 | |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 144 | static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | static DEFINE_SPINLOCK(addrconf_verify_lock); |
| 146 | |
| 147 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp); |
| 148 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); |
| 149 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 150 | static void addrconf_type_change(struct net_device *dev, |
| 151 | unsigned long event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | static int addrconf_ifdown(struct net_device *dev, int how); |
| 153 | |
Romain Kuntz | 21caa66 | 2013-01-09 21:06:03 +0000 | [diff] [blame] | 154 | static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, |
| 155 | int plen, |
| 156 | const struct net_device *dev, |
| 157 | u32 flags, u32 noflags); |
| 158 | |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 159 | static void addrconf_dad_start(struct inet6_ifaddr *ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | static void addrconf_dad_timer(unsigned long data); |
| 161 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 162 | static void addrconf_dad_run(struct inet6_dev *idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | static void addrconf_rs_timer(unsigned long data); |
| 164 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); |
| 165 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); |
| 166 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 167 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | struct prefix_info *pinfo); |
David S. Miller | 3e81c6d | 2010-03-20 16:18:00 -0700 | [diff] [blame] | 169 | static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, |
| 170 | struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 172 | static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Adrian Bunk | 888c848 | 2008-07-22 14:21:58 -0700 | [diff] [blame] | 174 | static struct ipv6_devconf ipv6_devconf __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | .forwarding = 0, |
| 176 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, |
| 177 | .mtu6 = IPV6_MIN_MTU, |
| 178 | .accept_ra = 1, |
| 179 | .accept_redirects = 1, |
| 180 | .autoconf = 1, |
| 181 | .force_mld_version = 0, |
| 182 | .dad_transmits = 1, |
| 183 | .rtr_solicits = MAX_RTR_SOLICITATIONS, |
| 184 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, |
| 185 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, |
| 186 | #ifdef CONFIG_IPV6_PRIVACY |
| 187 | .use_tempaddr = 0, |
| 188 | .temp_valid_lft = TEMP_VALID_LIFETIME, |
| 189 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, |
| 190 | .regen_max_retry = REGEN_MAX_RETRY, |
| 191 | .max_desync_factor = MAX_DESYNC_FACTOR, |
| 192 | #endif |
| 193 | .max_addresses = IPV6_MAX_ADDRESSES, |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 194 | .accept_ra_defrtr = 1, |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 195 | .accept_ra_pinfo = 1, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 196 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 197 | .accept_ra_rtr_pref = 1, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 198 | .rtr_probe_interval = 60 * HZ, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 199 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 200 | .accept_ra_rt_info_max_plen = 0, |
| 201 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 202 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 203 | .proxy_ndp = 0, |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 204 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 205 | .disable_ipv6 = 0, |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 206 | .accept_dad = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 209 | static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | .forwarding = 0, |
| 211 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, |
| 212 | .mtu6 = IPV6_MIN_MTU, |
| 213 | .accept_ra = 1, |
| 214 | .accept_redirects = 1, |
| 215 | .autoconf = 1, |
| 216 | .dad_transmits = 1, |
| 217 | .rtr_solicits = MAX_RTR_SOLICITATIONS, |
| 218 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, |
| 219 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, |
| 220 | #ifdef CONFIG_IPV6_PRIVACY |
| 221 | .use_tempaddr = 0, |
| 222 | .temp_valid_lft = TEMP_VALID_LIFETIME, |
| 223 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, |
| 224 | .regen_max_retry = REGEN_MAX_RETRY, |
| 225 | .max_desync_factor = MAX_DESYNC_FACTOR, |
| 226 | #endif |
| 227 | .max_addresses = IPV6_MAX_ADDRESSES, |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 228 | .accept_ra_defrtr = 1, |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 229 | .accept_ra_pinfo = 1, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 230 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 231 | .accept_ra_rtr_pref = 1, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 232 | .rtr_probe_interval = 60 * HZ, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 233 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 234 | .accept_ra_rt_info_max_plen = 0, |
| 235 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 236 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 237 | .proxy_ndp = 0, |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 238 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 239 | .disable_ipv6 = 0, |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 240 | .accept_dad = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 246 | const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; |
| 247 | const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 248 | const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT; |
| 249 | const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT; |
| 250 | const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 252 | /* Check if a valid qdisc is available */ |
David S. Miller | 0529794 | 2008-07-08 23:01:27 -0700 | [diff] [blame] | 253 | static inline bool addrconf_qdisc_ok(const struct net_device *dev) |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 254 | { |
David S. Miller | 0529794 | 2008-07-08 23:01:27 -0700 | [diff] [blame] | 255 | return !qdisc_tx_is_noop(dev); |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | static void addrconf_del_timer(struct inet6_ifaddr *ifp) |
| 259 | { |
| 260 | if (del_timer(&ifp->timer)) |
| 261 | __in6_ifa_put(ifp); |
| 262 | } |
| 263 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 264 | enum addrconf_timer_t { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | AC_NONE, |
| 266 | AC_DAD, |
| 267 | AC_RS, |
| 268 | }; |
| 269 | |
| 270 | static void addrconf_mod_timer(struct inet6_ifaddr *ifp, |
| 271 | enum addrconf_timer_t what, |
| 272 | unsigned long when) |
| 273 | { |
| 274 | if (!del_timer(&ifp->timer)) |
| 275 | in6_ifa_hold(ifp); |
| 276 | |
| 277 | switch (what) { |
| 278 | case AC_DAD: |
| 279 | ifp->timer.function = addrconf_dad_timer; |
| 280 | break; |
| 281 | case AC_RS: |
| 282 | ifp->timer.function = addrconf_rs_timer; |
| 283 | break; |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 284 | default: |
| 285 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | ifp->timer.expires = jiffies + when; |
| 288 | add_timer(&ifp->timer); |
| 289 | } |
| 290 | |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 291 | static int snmp6_alloc_dev(struct inet6_dev *idev) |
| 292 | { |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 293 | if (snmp_mib_init((void __percpu **)idev->stats.ipv6, |
Eric Dumazet | 1823e4c8 | 2010-06-22 20:58:41 +0000 | [diff] [blame] | 294 | sizeof(struct ipstats_mib), |
| 295 | __alignof__(struct ipstats_mib)) < 0) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 296 | goto err_ip; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 297 | idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), |
| 298 | GFP_KERNEL); |
| 299 | if (!idev->stats.icmpv6dev) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 300 | goto err_icmp; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 301 | idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), |
| 302 | GFP_KERNEL); |
| 303 | if (!idev->stats.icmpv6msgdev) |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 304 | goto err_icmpmsg; |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 308 | err_icmpmsg: |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 309 | kfree(idev->stats.icmpv6dev); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 310 | err_icmp: |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 311 | snmp_mib_free((void __percpu **)idev->stats.ipv6); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 312 | err_ip: |
Pavel Emelyanov | aaf70ec | 2007-10-17 21:25:32 -0700 | [diff] [blame] | 313 | return -ENOMEM; |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Pavel Emelyanov | 16910b9 | 2007-10-17 21:23:43 -0700 | [diff] [blame] | 316 | static void snmp6_free_dev(struct inet6_dev *idev) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 317 | { |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 318 | kfree(idev->stats.icmpv6msgdev); |
| 319 | kfree(idev->stats.icmpv6dev); |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 320 | snmp_mib_free((void __percpu **)idev->stats.ipv6); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* Nobody refers to this device, we may destroy it. */ |
| 324 | |
| 325 | void in6_dev_finish_destroy(struct inet6_dev *idev) |
| 326 | { |
| 327 | struct net_device *dev = idev->dev; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 328 | |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 329 | WARN_ON(!list_empty(&idev->addr_list)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 330 | WARN_ON(idev->mc_list != NULL); |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | #ifdef NET_REFCNT_DEBUG |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 333 | pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | #endif |
| 335 | dev_put(dev); |
| 336 | if (!idev->dead) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 337 | pr_warn("Freeing alive inet6 device %p\n", idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | snmp6_free_dev(idev); |
Lai Jiangshan | 38f57d1 | 2011-03-15 17:59:14 +0800 | [diff] [blame] | 341 | kfree_rcu(idev, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 343 | EXPORT_SYMBOL(in6_dev_finish_destroy); |
| 344 | |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 345 | static struct inet6_dev *ipv6_add_dev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | struct inet6_dev *ndev; |
| 348 | |
| 349 | ASSERT_RTNL(); |
| 350 | |
| 351 | if (dev->mtu < IPV6_MIN_MTU) |
| 352 | return NULL; |
| 353 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 354 | ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 356 | if (ndev == NULL) |
| 357 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 359 | rwlock_init(&ndev->lock); |
| 360 | ndev->dev = dev; |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 361 | INIT_LIST_HEAD(&ndev->addr_list); |
| 362 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 363 | memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 364 | ndev->cnf.mtu6 = dev->mtu; |
| 365 | ndev->cnf.sysctl = NULL; |
| 366 | ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); |
| 367 | if (ndev->nd_parms == NULL) { |
| 368 | kfree(ndev); |
| 369 | return NULL; |
| 370 | } |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 371 | if (ndev->cnf.forwarding) |
| 372 | dev_disable_lro(dev); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 373 | /* We refer to the device */ |
| 374 | dev_hold(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 376 | if (snmp6_alloc_dev(ndev) < 0) { |
| 377 | ADBG((KERN_WARNING |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 378 | "%s: cannot allocate memory for statistics; dev=%s.\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 379 | __func__, dev->name)); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 380 | neigh_parms_release(&nd_tbl, ndev->nd_parms); |
Roy Li | 8603e33 | 2011-09-20 15:10:16 -0400 | [diff] [blame] | 381 | dev_put(dev); |
| 382 | kfree(ndev); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 386 | if (snmp6_register_dev(ndev) < 0) { |
| 387 | ADBG((KERN_WARNING |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 388 | "%s: cannot create /proc/net/dev_snmp6/%s\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 389 | __func__, dev->name)); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 390 | neigh_parms_release(&nd_tbl, ndev->nd_parms); |
| 391 | ndev->dead = 1; |
| 392 | in6_dev_finish_destroy(ndev); |
| 393 | return NULL; |
| 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 396 | /* One reference from device. We must do this before |
| 397 | * we invoke __ipv6_regen_rndid(). |
| 398 | */ |
| 399 | in6_dev_hold(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 401 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 402 | ndev->cnf.accept_dad = -1; |
| 403 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 404 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
YOSHIFUJI Hideaki | b077d7a | 2008-04-13 23:42:18 -0700 | [diff] [blame] | 405 | if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 406 | pr_info("%s: Disabled Multicast RS\n", dev->name); |
YOSHIFUJI Hideaki | b077d7a | 2008-04-13 23:42:18 -0700 | [diff] [blame] | 407 | ndev->cnf.rtr_solicits = 0; |
| 408 | } |
| 409 | #endif |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | #ifdef CONFIG_IPV6_PRIVACY |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 412 | INIT_LIST_HEAD(&ndev->tempaddr_list); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 413 | setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 414 | if ((dev->flags&IFF_LOOPBACK) || |
| 415 | dev->type == ARPHRD_TUNNEL || |
YOSHIFUJI Hideaki | 9625ed7 | 2008-04-13 23:47:11 -0700 | [diff] [blame] | 416 | dev->type == ARPHRD_TUNNEL6 || |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 417 | dev->type == ARPHRD_SIT || |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 418 | dev->type == ARPHRD_NONE) { |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 419 | ndev->cnf.use_tempaddr = -1; |
| 420 | } else { |
| 421 | in6_dev_hold(ndev); |
| 422 | ipv6_regen_rndid((unsigned long) ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 424 | #endif |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 425 | memset(ndev->token.s6_addr, 0, sizeof(ndev->token.s6_addr)); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 426 | |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 427 | if (netif_running(dev) && addrconf_qdisc_ok(dev)) |
Herbert Xu | 53aadcc | 2007-03-27 14:31:52 -0700 | [diff] [blame] | 428 | ndev->if_flags |= IF_READY; |
| 429 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 430 | ipv6_mc_init_dev(ndev); |
| 431 | ndev->tstamp = jiffies; |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 432 | addrconf_sysctl_register(ndev); |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 433 | /* protected by rtnl_lock */ |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 434 | rcu_assign_pointer(dev->ip6_ptr, ndev); |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 435 | |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 436 | /* Join interface-local all-node multicast group */ |
| 437 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); |
| 438 | |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 439 | /* Join all-node multicast group */ |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 440 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 441 | |
Li Wei | d6ddef9 | 2012-03-05 14:45:17 +0000 | [diff] [blame] | 442 | /* Join all-router multicast group if forwarding is set */ |
Li Wei | 8b2aaed | 2012-03-07 14:58:07 +0000 | [diff] [blame] | 443 | if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) |
Li Wei | d6ddef9 | 2012-03-05 14:45:17 +0000 | [diff] [blame] | 444 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | return ndev; |
| 447 | } |
| 448 | |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 449 | static struct inet6_dev *ipv6_find_idev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | struct inet6_dev *idev; |
| 452 | |
| 453 | ASSERT_RTNL(); |
| 454 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 455 | idev = __in6_dev_get(dev); |
| 456 | if (!idev) { |
| 457 | idev = ipv6_add_dev(dev); |
| 458 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return NULL; |
| 460 | } |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (dev->flags&IFF_UP) |
| 463 | ipv6_mc_up(idev); |
| 464 | return idev; |
| 465 | } |
| 466 | |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 467 | static int inet6_netconf_msgsize_devconf(int type) |
| 468 | { |
| 469 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) |
| 470 | + nla_total_size(4); /* NETCONFA_IFINDEX */ |
| 471 | |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 472 | /* type -1 is used for ALL */ |
| 473 | if (type == -1 || type == NETCONFA_FORWARDING) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 474 | size += nla_total_size(4); |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 475 | #ifdef CONFIG_IPV6_MROUTE |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 476 | if (type == -1 || type == NETCONFA_MC_FORWARDING) |
| 477 | size += nla_total_size(4); |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 478 | #endif |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 479 | |
| 480 | return size; |
| 481 | } |
| 482 | |
| 483 | static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, |
| 484 | struct ipv6_devconf *devconf, u32 portid, |
| 485 | u32 seq, int event, unsigned int flags, |
| 486 | int type) |
| 487 | { |
| 488 | struct nlmsghdr *nlh; |
| 489 | struct netconfmsg *ncm; |
| 490 | |
| 491 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), |
| 492 | flags); |
| 493 | if (nlh == NULL) |
| 494 | return -EMSGSIZE; |
| 495 | |
| 496 | ncm = nlmsg_data(nlh); |
| 497 | ncm->ncm_family = AF_INET6; |
| 498 | |
| 499 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) |
| 500 | goto nla_put_failure; |
| 501 | |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 502 | /* type -1 is used for ALL */ |
| 503 | if ((type == -1 || type == NETCONFA_FORWARDING) && |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 504 | nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) |
| 505 | goto nla_put_failure; |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 506 | #ifdef CONFIG_IPV6_MROUTE |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 507 | if ((type == -1 || type == NETCONFA_MC_FORWARDING) && |
| 508 | nla_put_s32(skb, NETCONFA_MC_FORWARDING, |
| 509 | devconf->mc_forwarding) < 0) |
| 510 | goto nla_put_failure; |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 511 | #endif |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 512 | return nlmsg_end(skb, nlh); |
| 513 | |
| 514 | nla_put_failure: |
| 515 | nlmsg_cancel(skb, nlh); |
| 516 | return -EMSGSIZE; |
| 517 | } |
| 518 | |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 519 | void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, |
| 520 | struct ipv6_devconf *devconf) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 521 | { |
| 522 | struct sk_buff *skb; |
| 523 | int err = -ENOBUFS; |
| 524 | |
| 525 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); |
| 526 | if (skb == NULL) |
| 527 | goto errout; |
| 528 | |
| 529 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, |
| 530 | RTM_NEWNETCONF, 0, type); |
| 531 | if (err < 0) { |
| 532 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ |
| 533 | WARN_ON(err == -EMSGSIZE); |
| 534 | kfree_skb(skb); |
| 535 | goto errout; |
| 536 | } |
| 537 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); |
| 538 | return; |
| 539 | errout: |
Cong Ding | bd77902 | 2012-12-18 12:08:56 +0000 | [diff] [blame] | 540 | rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 543 | static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { |
| 544 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, |
| 545 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, |
| 546 | }; |
| 547 | |
| 548 | static int inet6_netconf_get_devconf(struct sk_buff *in_skb, |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 549 | struct nlmsghdr *nlh) |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 550 | { |
| 551 | struct net *net = sock_net(in_skb->sk); |
| 552 | struct nlattr *tb[NETCONFA_MAX+1]; |
| 553 | struct netconfmsg *ncm; |
| 554 | struct sk_buff *skb; |
| 555 | struct ipv6_devconf *devconf; |
| 556 | struct inet6_dev *in6_dev; |
| 557 | struct net_device *dev; |
| 558 | int ifindex; |
| 559 | int err; |
| 560 | |
| 561 | err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, |
| 562 | devconf_ipv6_policy); |
| 563 | if (err < 0) |
| 564 | goto errout; |
| 565 | |
| 566 | err = EINVAL; |
| 567 | if (!tb[NETCONFA_IFINDEX]) |
| 568 | goto errout; |
| 569 | |
| 570 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); |
| 571 | switch (ifindex) { |
| 572 | case NETCONFA_IFINDEX_ALL: |
| 573 | devconf = net->ipv6.devconf_all; |
| 574 | break; |
| 575 | case NETCONFA_IFINDEX_DEFAULT: |
| 576 | devconf = net->ipv6.devconf_dflt; |
| 577 | break; |
| 578 | default: |
| 579 | dev = __dev_get_by_index(net, ifindex); |
| 580 | if (dev == NULL) |
| 581 | goto errout; |
| 582 | in6_dev = __in6_dev_get(dev); |
| 583 | if (in6_dev == NULL) |
| 584 | goto errout; |
| 585 | devconf = &in6_dev->cnf; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | err = -ENOBUFS; |
| 590 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC); |
| 591 | if (skb == NULL) |
| 592 | goto errout; |
| 593 | |
| 594 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, |
| 595 | NETLINK_CB(in_skb).portid, |
| 596 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, |
| 597 | -1); |
| 598 | if (err < 0) { |
| 599 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ |
| 600 | WARN_ON(err == -EMSGSIZE); |
| 601 | kfree_skb(skb); |
| 602 | goto errout; |
| 603 | } |
| 604 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 605 | errout: |
| 606 | return err; |
| 607 | } |
| 608 | |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 609 | static int inet6_netconf_dump_devconf(struct sk_buff *skb, |
| 610 | struct netlink_callback *cb) |
| 611 | { |
| 612 | struct net *net = sock_net(skb->sk); |
| 613 | int h, s_h; |
| 614 | int idx, s_idx; |
| 615 | struct net_device *dev; |
| 616 | struct inet6_dev *idev; |
| 617 | struct hlist_head *head; |
| 618 | |
| 619 | s_h = cb->args[0]; |
| 620 | s_idx = idx = cb->args[1]; |
| 621 | |
| 622 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 623 | idx = 0; |
| 624 | head = &net->dev_index_head[h]; |
| 625 | rcu_read_lock(); |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 626 | cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ |
| 627 | net->dev_base_seq; |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 628 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
| 629 | if (idx < s_idx) |
| 630 | goto cont; |
| 631 | idev = __in6_dev_get(dev); |
| 632 | if (!idev) |
| 633 | goto cont; |
| 634 | |
| 635 | if (inet6_netconf_fill_devconf(skb, dev->ifindex, |
| 636 | &idev->cnf, |
| 637 | NETLINK_CB(cb->skb).portid, |
| 638 | cb->nlh->nlmsg_seq, |
| 639 | RTM_NEWNETCONF, |
| 640 | NLM_F_MULTI, |
| 641 | -1) <= 0) { |
| 642 | rcu_read_unlock(); |
| 643 | goto done; |
| 644 | } |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 645 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 646 | cont: |
| 647 | idx++; |
| 648 | } |
| 649 | rcu_read_unlock(); |
| 650 | } |
| 651 | if (h == NETDEV_HASHENTRIES) { |
| 652 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, |
| 653 | net->ipv6.devconf_all, |
| 654 | NETLINK_CB(cb->skb).portid, |
| 655 | cb->nlh->nlmsg_seq, |
| 656 | RTM_NEWNETCONF, NLM_F_MULTI, |
| 657 | -1) <= 0) |
| 658 | goto done; |
| 659 | else |
| 660 | h++; |
| 661 | } |
| 662 | if (h == NETDEV_HASHENTRIES + 1) { |
| 663 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, |
| 664 | net->ipv6.devconf_dflt, |
| 665 | NETLINK_CB(cb->skb).portid, |
| 666 | cb->nlh->nlmsg_seq, |
| 667 | RTM_NEWNETCONF, NLM_F_MULTI, |
| 668 | -1) <= 0) |
| 669 | goto done; |
| 670 | else |
| 671 | h++; |
| 672 | } |
| 673 | done: |
| 674 | cb->args[0] = h; |
| 675 | cb->args[1] = idx; |
| 676 | |
| 677 | return skb->len; |
| 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | #ifdef CONFIG_SYSCTL |
| 681 | static void dev_forward_change(struct inet6_dev *idev) |
| 682 | { |
| 683 | struct net_device *dev; |
| 684 | struct inet6_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | if (!idev) |
| 687 | return; |
| 688 | dev = idev->dev; |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 689 | if (idev->cnf.forwarding) |
| 690 | dev_disable_lro(dev); |
Amerigo Wang | 1a94083 | 2012-10-28 17:43:53 +0000 | [diff] [blame] | 691 | if (dev->flags & IFF_MULTICAST) { |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 692 | if (idev->cnf.forwarding) { |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 693 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 694 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); |
| 695 | ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); |
| 696 | } else { |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 697 | ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 698 | ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); |
| 699 | ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); |
| 700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 702 | |
| 703 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
Michal Wrobel | 2c12a74 | 2007-02-26 15:36:10 -0800 | [diff] [blame] | 704 | if (ifa->flags&IFA_F_TENTATIVE) |
| 705 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | if (idev->cnf.forwarding) |
| 707 | addrconf_join_anycast(ifa); |
| 708 | else |
| 709 | addrconf_leave_anycast(ifa); |
| 710 | } |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 711 | inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, |
| 712 | dev->ifindex, &idev->cnf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 716 | static void addrconf_forward_change(struct net *net, __s32 newf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
| 718 | struct net_device *dev; |
| 719 | struct inet6_dev *idev; |
| 720 | |
Ben Hutchings | 4acd494 | 2012-08-14 08:54:51 +0000 | [diff] [blame] | 721 | for_each_netdev(net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | idev = __in6_dev_get(dev); |
| 723 | if (idev) { |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 724 | int changed = (!idev->cnf.forwarding) ^ (!newf); |
| 725 | idev->cnf.forwarding = newf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (changed) |
| 727 | dev_forward_change(idev); |
| 728 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 731 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 732 | static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 733 | { |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 734 | struct net *net; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 735 | int old; |
| 736 | |
| 737 | if (!rtnl_trylock()) |
| 738 | return restart_syscall(); |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 739 | |
| 740 | net = (struct net *)table->extra2; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 741 | old = *p; |
| 742 | *p = newf; |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 743 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 744 | if (p == &net->ipv6.devconf_dflt->forwarding) { |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 745 | if ((!newf) ^ (!old)) |
| 746 | inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, |
| 747 | NETCONFA_IFINDEX_DEFAULT, |
| 748 | net->ipv6.devconf_dflt); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 749 | rtnl_unlock(); |
| 750 | return 0; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 751 | } |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 752 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 753 | if (p == &net->ipv6.devconf_all->forwarding) { |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 754 | net->ipv6.devconf_dflt->forwarding = newf; |
| 755 | addrconf_forward_change(net, newf); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 756 | if ((!newf) ^ (!old)) |
| 757 | inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, |
| 758 | NETCONFA_IFINDEX_ALL, |
| 759 | net->ipv6.devconf_all); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 760 | } else if ((!newf) ^ (!old)) |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 761 | dev_forward_change((struct inet6_dev *)table->extra1); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 762 | rtnl_unlock(); |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 763 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 764 | if (newf) |
Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 765 | rt6_purge_dflt_routers(net); |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 766 | return 1; |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | #endif |
| 769 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 770 | /* Nobody refers to this ifaddr, destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) |
| 772 | { |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 773 | WARN_ON(!hlist_unhashed(&ifp->addr_lst)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | #ifdef NET_REFCNT_DEBUG |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 776 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | #endif |
| 778 | |
| 779 | in6_dev_put(ifp->idev); |
| 780 | |
| 781 | if (del_timer(&ifp->timer)) |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 782 | pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 784 | if (ifp->state != INET6_IFADDR_STATE_DEAD) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 785 | pr_warn("Freeing alive inet6 address %p\n", ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | return; |
| 787 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 788 | ip6_rt_put(ifp->rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Lai Jiangshan | e578598 | 2011-03-15 18:00:14 +0800 | [diff] [blame] | 790 | kfree_rcu(ifp, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 793 | static void |
| 794 | ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) |
| 795 | { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 796 | struct list_head *p; |
YOSHIFUJI Hideaki | 8a6ce0c | 2006-07-11 13:05:30 -0700 | [diff] [blame] | 797 | int ifp_scope = ipv6_addr_src_scope(&ifp->addr); |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * Each device address list is sorted in order of scope - |
| 801 | * global before linklocal. |
| 802 | */ |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 803 | list_for_each(p, &idev->addr_list) { |
| 804 | struct inet6_ifaddr *ifa |
| 805 | = list_entry(p, struct inet6_ifaddr, if_list); |
YOSHIFUJI Hideaki | 8a6ce0c | 2006-07-11 13:05:30 -0700 | [diff] [blame] | 806 | if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | |
David S. Miller | b54c9b9 | 2010-03-25 21:25:30 -0700 | [diff] [blame] | 810 | list_add_tail(&ifp->if_list, p); |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 813 | static u32 inet6_addr_hash(const struct in6_addr *addr) |
YOSHIFUJI Hideaki | 3eb84f4 | 2008-04-10 15:42:08 +0900 | [diff] [blame] | 814 | { |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 815 | return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); |
YOSHIFUJI Hideaki | 3eb84f4 | 2008-04-10 15:42:08 +0900 | [diff] [blame] | 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* On success it returns ifp with increased reference count */ |
| 819 | |
| 820 | static struct inet6_ifaddr * |
| 821 | ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, |
Jamal Hadi Salim | e431b8c | 2005-06-18 22:55:31 -0700 | [diff] [blame] | 822 | int scope, u32 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
| 824 | struct inet6_ifaddr *ifa = NULL; |
| 825 | struct rt6_info *rt; |
stephen hemminger | 3a88a81 | 2010-03-17 20:31:12 +0000 | [diff] [blame] | 826 | unsigned int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | int err = 0; |
YOSHIFUJI Hideaki | d68b827 | 2008-06-25 16:26:47 +0900 | [diff] [blame] | 828 | int addr_type = ipv6_addr_type(addr); |
| 829 | |
| 830 | if (addr_type == IPV6_ADDR_ANY || |
| 831 | addr_type & IPV6_ADDR_MULTICAST || |
| 832 | (!(idev->dev->flags & IFF_LOOPBACK) && |
| 833 | addr_type & IPV6_ADDR_LOOPBACK)) |
| 834 | return ERR_PTR(-EADDRNOTAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 836 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | if (idev->dead) { |
| 838 | err = -ENODEV; /*XXX*/ |
| 839 | goto out2; |
| 840 | } |
| 841 | |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 842 | if (idev->cnf.disable_ipv6) { |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 843 | err = -EACCES; |
| 844 | goto out2; |
| 845 | } |
| 846 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 847 | spin_lock(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | /* Ignore adding duplicate addresses on an interface */ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 850 | if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | ADBG(("ipv6_add_addr: already assigned\n")); |
| 852 | err = -EEXIST; |
| 853 | goto out; |
| 854 | } |
| 855 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 856 | ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | if (ifa == NULL) { |
| 859 | ADBG(("ipv6_add_addr: malloc failed\n")); |
| 860 | err = -ENOBUFS; |
| 861 | goto out; |
| 862 | } |
| 863 | |
David S. Miller | 8f03151 | 2011-12-06 16:48:14 -0500 | [diff] [blame] | 864 | rt = addrconf_dst_alloc(idev, addr, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (IS_ERR(rt)) { |
| 866 | err = PTR_ERR(rt); |
| 867 | goto out; |
| 868 | } |
| 869 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 870 | ifa->addr = *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | spin_lock_init(&ifa->lock); |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 873 | spin_lock_init(&ifa->state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | init_timer(&ifa->timer); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 875 | INIT_HLIST_NODE(&ifa->addr_lst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | ifa->timer.data = (unsigned long) ifa; |
| 877 | ifa->scope = scope; |
| 878 | ifa->prefix_len = pfxlen; |
| 879 | ifa->flags = flags | IFA_F_TENTATIVE; |
| 880 | ifa->cstamp = ifa->tstamp = jiffies; |
| 881 | |
Keir Fraser | 57f5f54 | 2006-08-29 02:43:49 -0700 | [diff] [blame] | 882 | ifa->rt = rt; |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | ifa->idev = idev; |
| 885 | in6_dev_hold(idev); |
| 886 | /* For caller */ |
| 887 | in6_ifa_hold(ifa); |
| 888 | |
| 889 | /* Add to big hash table */ |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 890 | hash = inet6_addr_hash(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 892 | hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 893 | spin_unlock(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | write_lock(&idev->lock); |
| 896 | /* Add to inet6_dev unicast addr list. */ |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 897 | ipv6_link_dev_addr(idev, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | #ifdef CONFIG_IPV6_PRIVACY |
| 900 | if (ifa->flags&IFA_F_TEMPORARY) { |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 901 | list_add(&ifa->tmp_list, &idev->tempaddr_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | in6_ifa_hold(ifa); |
| 903 | } |
| 904 | #endif |
| 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | in6_ifa_hold(ifa); |
| 907 | write_unlock(&idev->lock); |
| 908 | out2: |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 909 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
YOSHIFUJI Hideaki | fd92833 | 2005-04-19 22:27:09 -0700 | [diff] [blame] | 911 | if (likely(err == 0)) |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 912 | atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | else { |
| 914 | kfree(ifa); |
| 915 | ifa = ERR_PTR(err); |
| 916 | } |
| 917 | |
| 918 | return ifa; |
| 919 | out: |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 920 | spin_unlock(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | goto out2; |
| 922 | } |
| 923 | |
| 924 | /* This function wants to get referenced ifp and releases it before return */ |
| 925 | |
| 926 | static void ipv6_del_addr(struct inet6_ifaddr *ifp) |
| 927 | { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 928 | struct inet6_ifaddr *ifa, *ifn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | struct inet6_dev *idev = ifp->idev; |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 930 | int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | int deleted = 0, onlink = 0; |
| 932 | unsigned long expires = jiffies; |
| 933 | |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 934 | spin_lock_bh(&ifp->state_lock); |
| 935 | state = ifp->state; |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 936 | ifp->state = INET6_IFADDR_STATE_DEAD; |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 937 | spin_unlock_bh(&ifp->state_lock); |
| 938 | |
| 939 | if (state == INET6_IFADDR_STATE_DEAD) |
| 940 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 942 | spin_lock_bh(&addrconf_hash_lock); |
| 943 | hlist_del_init_rcu(&ifp->addr_lst); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 944 | spin_unlock_bh(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | write_lock_bh(&idev->lock); |
| 947 | #ifdef CONFIG_IPV6_PRIVACY |
| 948 | if (ifp->flags&IFA_F_TEMPORARY) { |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 949 | list_del(&ifp->tmp_list); |
| 950 | if (ifp->ifpub) { |
| 951 | in6_ifa_put(ifp->ifpub); |
| 952 | ifp->ifpub = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 954 | __in6_ifa_put(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
| 956 | #endif |
| 957 | |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 958 | list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | if (ifa == ifp) { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 960 | list_del_init(&ifp->if_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | __in6_ifa_put(ifp); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) |
| 964 | break; |
| 965 | deleted = 1; |
Kristian Slavov | 1d14280 | 2005-12-21 18:47:24 -0800 | [diff] [blame] | 966 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } else if (ifp->flags & IFA_F_PERMANENT) { |
| 968 | if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, |
| 969 | ifp->prefix_len)) { |
| 970 | if (ifa->flags & IFA_F_PERMANENT) { |
| 971 | onlink = 1; |
| 972 | if (deleted) |
| 973 | break; |
| 974 | } else { |
| 975 | unsigned long lifetime; |
| 976 | |
| 977 | if (!onlink) |
| 978 | onlink = -1; |
| 979 | |
| 980 | spin_lock(&ifa->lock); |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 981 | |
| 982 | lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); |
| 983 | /* |
| 984 | * Note: Because this address is |
| 985 | * not permanent, lifetime < |
| 986 | * LONG_MAX / HZ here. |
| 987 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | if (time_before(expires, |
| 989 | ifa->tstamp + lifetime * HZ)) |
| 990 | expires = ifa->tstamp + lifetime * HZ; |
| 991 | spin_unlock(&ifa->lock); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | write_unlock_bh(&idev->lock); |
| 997 | |
Andrey Vagin | b223856 | 2008-07-08 15:13:31 -0700 | [diff] [blame] | 998 | addrconf_del_timer(ifp); |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | ipv6_ifa_notify(RTM_DELADDR, ifp); |
| 1001 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1002 | atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | /* |
| 1005 | * Purge or update corresponding prefix |
| 1006 | * |
| 1007 | * 1) we don't purge prefix here if address was not permanent. |
| 1008 | * prefix is managed by its own lifetime. |
| 1009 | * 2) if there're no addresses, delete prefix. |
| 1010 | * 3) if there're still other permanent address(es), |
| 1011 | * corresponding prefix is still permanent. |
| 1012 | * 4) otherwise, update prefix lifetime to the |
| 1013 | * longest valid lifetime among the corresponding |
| 1014 | * addresses on the device. |
| 1015 | * Note: subsequent RA will update lifetime. |
| 1016 | * |
| 1017 | * --yoshfuji |
| 1018 | */ |
| 1019 | if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { |
| 1020 | struct in6_addr prefix; |
| 1021 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
Nicolas Dichtel | 64c6d08 | 2012-09-26 00:04:55 +0000 | [diff] [blame] | 1023 | ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); |
Nicolas Dichtel | 64c6d08 | 2012-09-26 00:04:55 +0000 | [diff] [blame] | 1024 | |
Romain Kuntz | 21caa66 | 2013-01-09 21:06:03 +0000 | [diff] [blame] | 1025 | rt = addrconf_get_prefix_route(&prefix, |
| 1026 | ifp->prefix_len, |
| 1027 | ifp->idev->dev, |
| 1028 | 0, RTF_GATEWAY | RTF_DEFAULT); |
| 1029 | |
| 1030 | if (rt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (onlink == 0) { |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1032 | ip6_del_rt(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | rt = NULL; |
| 1034 | } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1035 | rt6_set_expires(rt, expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 1038 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 1041 | /* clean up prefsrc entries */ |
| 1042 | rt6_remove_prefsrc(ifp); |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 1043 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | in6_ifa_put(ifp); |
| 1045 | } |
| 1046 | |
| 1047 | #ifdef CONFIG_IPV6_PRIVACY |
| 1048 | static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) |
| 1049 | { |
| 1050 | struct inet6_dev *idev = ifp->idev; |
| 1051 | struct in6_addr addr, *tmpaddr; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1052 | unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1053 | unsigned long regen_advance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | int tmp_plen; |
| 1055 | int ret = 0; |
| 1056 | int max_addresses; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1057 | u32 addr_flags; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1058 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | write_lock(&idev->lock); |
| 1061 | if (ift) { |
| 1062 | spin_lock_bh(&ift->lock); |
| 1063 | memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); |
| 1064 | spin_unlock_bh(&ift->lock); |
| 1065 | tmpaddr = &addr; |
| 1066 | } else { |
| 1067 | tmpaddr = NULL; |
| 1068 | } |
| 1069 | retry: |
| 1070 | in6_dev_hold(idev); |
| 1071 | if (idev->cnf.use_tempaddr <= 0) { |
| 1072 | write_unlock(&idev->lock); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1073 | pr_info("%s: use_tempaddr is disabled\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | in6_dev_put(idev); |
| 1075 | ret = -1; |
| 1076 | goto out; |
| 1077 | } |
| 1078 | spin_lock_bh(&ifp->lock); |
| 1079 | if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { |
| 1080 | idev->cnf.use_tempaddr = -1; /*XXX*/ |
| 1081 | spin_unlock_bh(&ifp->lock); |
| 1082 | write_unlock(&idev->lock); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1083 | pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", |
| 1084 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | in6_dev_put(idev); |
| 1086 | ret = -1; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | in6_ifa_hold(ifp); |
| 1090 | memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 1091 | __ipv6_try_regen_rndid(idev, tmpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | memcpy(&addr.s6_addr[8], idev->rndid, 8); |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1093 | age = (now - ifp->tstamp) / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | tmp_valid_lft = min_t(__u32, |
| 1095 | ifp->valid_lft, |
Glenn Wurster | 7a876b0 | 2010-09-27 07:10:10 +0000 | [diff] [blame] | 1096 | idev->cnf.temp_valid_lft + age); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1097 | tmp_prefered_lft = min_t(__u32, |
| 1098 | ifp->prefered_lft, |
Glenn Wurster | 7a876b0 | 2010-09-27 07:10:10 +0000 | [diff] [blame] | 1099 | idev->cnf.temp_prefered_lft + age - |
Ben Hutchings | 784e271 | 2010-06-26 11:42:55 +0000 | [diff] [blame] | 1100 | idev->cnf.max_desync_factor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | tmp_plen = ifp->prefix_len; |
| 1102 | max_addresses = idev->cnf.max_addresses; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | tmp_tstamp = ifp->tstamp; |
| 1104 | spin_unlock_bh(&ifp->lock); |
| 1105 | |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1106 | regen_advance = idev->cnf.regen_max_retry * |
| 1107 | idev->cnf.dad_transmits * |
| 1108 | idev->nd_parms->retrans_time / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | write_unlock(&idev->lock); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1110 | |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1111 | /* A temporary address is created only if this calculated Preferred |
| 1112 | * Lifetime is greater than REGEN_ADVANCE time units. In particular, |
| 1113 | * an implementation must not create a temporary address with a zero |
| 1114 | * Preferred Lifetime. |
| 1115 | */ |
| 1116 | if (tmp_prefered_lft <= regen_advance) { |
| 1117 | in6_ifa_put(ifp); |
| 1118 | in6_dev_put(idev); |
| 1119 | ret = -1; |
| 1120 | goto out; |
| 1121 | } |
| 1122 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1123 | addr_flags = IFA_F_TEMPORARY; |
| 1124 | /* set in addrconf_prefix_rcv() */ |
| 1125 | if (ifp->flags & IFA_F_OPTIMISTIC) |
| 1126 | addr_flags |= IFA_F_OPTIMISTIC; |
| 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | ift = !max_addresses || |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1129 | ipv6_count_addresses(idev) < max_addresses ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | ipv6_add_addr(idev, &addr, tmp_plen, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1131 | ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, |
| 1132 | addr_flags) : NULL; |
YOSHIFUJI Hideaki / 吉藤英明 | 3f0d2ba | 2013-01-22 06:32:54 +0000 | [diff] [blame] | 1133 | if (IS_ERR_OR_NULL(ift)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | in6_ifa_put(ifp); |
| 1135 | in6_dev_put(idev); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1136 | pr_info("%s: retry temporary address regeneration\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | tmpaddr = &addr; |
| 1138 | write_lock(&idev->lock); |
| 1139 | goto retry; |
| 1140 | } |
| 1141 | |
| 1142 | spin_lock_bh(&ift->lock); |
| 1143 | ift->ifpub = ifp; |
| 1144 | ift->valid_lft = tmp_valid_lft; |
| 1145 | ift->prefered_lft = tmp_prefered_lft; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1146 | ift->cstamp = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | ift->tstamp = tmp_tstamp; |
| 1148 | spin_unlock_bh(&ift->lock); |
| 1149 | |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 1150 | addrconf_dad_start(ift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | in6_ifa_put(ift); |
| 1152 | in6_dev_put(idev); |
| 1153 | out: |
| 1154 | return ret; |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
| 1158 | /* |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1159 | * Choose an appropriate source address (RFC3484) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | */ |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1161 | enum { |
| 1162 | IPV6_SADDR_RULE_INIT = 0, |
| 1163 | IPV6_SADDR_RULE_LOCAL, |
| 1164 | IPV6_SADDR_RULE_SCOPE, |
| 1165 | IPV6_SADDR_RULE_PREFERRED, |
| 1166 | #ifdef CONFIG_IPV6_MIP6 |
| 1167 | IPV6_SADDR_RULE_HOA, |
| 1168 | #endif |
| 1169 | IPV6_SADDR_RULE_OIF, |
| 1170 | IPV6_SADDR_RULE_LABEL, |
| 1171 | #ifdef CONFIG_IPV6_PRIVACY |
| 1172 | IPV6_SADDR_RULE_PRIVACY, |
| 1173 | #endif |
| 1174 | IPV6_SADDR_RULE_ORCHID, |
| 1175 | IPV6_SADDR_RULE_PREFIX, |
| 1176 | IPV6_SADDR_RULE_MAX |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1177 | }; |
| 1178 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1179 | struct ipv6_saddr_score { |
| 1180 | int rule; |
| 1181 | int addr_type; |
| 1182 | struct inet6_ifaddr *ifa; |
| 1183 | DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); |
| 1184 | int scopedist; |
| 1185 | int matchlen; |
| 1186 | }; |
| 1187 | |
| 1188 | struct ipv6_saddr_dst { |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1189 | const struct in6_addr *addr; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1190 | int ifindex; |
| 1191 | int scope; |
| 1192 | int label; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1193 | unsigned int prefs; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1194 | }; |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1195 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 1196 | static inline int ipv6_saddr_preferred(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | { |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 1198 | if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1199 | return 1; |
| 1200 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1203 | static int ipv6_get_saddr_eval(struct net *net, |
| 1204 | struct ipv6_saddr_score *score, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1205 | struct ipv6_saddr_dst *dst, |
| 1206 | int i) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | |
| 1210 | if (i <= score->rule) { |
| 1211 | switch (i) { |
| 1212 | case IPV6_SADDR_RULE_SCOPE: |
| 1213 | ret = score->scopedist; |
| 1214 | break; |
| 1215 | case IPV6_SADDR_RULE_PREFIX: |
| 1216 | ret = score->matchlen; |
| 1217 | break; |
| 1218 | default: |
| 1219 | ret = !!test_bit(i, score->scorebits); |
| 1220 | } |
| 1221 | goto out; |
| 1222 | } |
| 1223 | |
| 1224 | switch (i) { |
| 1225 | case IPV6_SADDR_RULE_INIT: |
| 1226 | /* Rule 0: remember if hiscore is not ready yet */ |
| 1227 | ret = !!score->ifa; |
| 1228 | break; |
| 1229 | case IPV6_SADDR_RULE_LOCAL: |
| 1230 | /* Rule 1: Prefer same address */ |
| 1231 | ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); |
| 1232 | break; |
| 1233 | case IPV6_SADDR_RULE_SCOPE: |
| 1234 | /* Rule 2: Prefer appropriate scope |
| 1235 | * |
| 1236 | * ret |
| 1237 | * ^ |
| 1238 | * -1 | d 15 |
| 1239 | * ---+--+-+---> scope |
| 1240 | * | |
| 1241 | * | d is scope of the destination. |
| 1242 | * B-d | \ |
| 1243 | * | \ <- smaller scope is better if |
| 1244 | * B-15 | \ if scope is enough for destinaion. |
| 1245 | * | ret = B - scope (-1 <= scope >= d <= 15). |
| 1246 | * d-C-1 | / |
| 1247 | * |/ <- greater is better |
| 1248 | * -C / if scope is not enough for destination. |
| 1249 | * /| ret = scope - C (-1 <= d < scope <= 15). |
| 1250 | * |
| 1251 | * d - C - 1 < B -15 (for all -1 <= d <= 15). |
| 1252 | * C > d + 14 - B >= 15 + 14 - B = 29 - B. |
| 1253 | * Assume B = 0 and we get C > 29. |
| 1254 | */ |
| 1255 | ret = __ipv6_addr_src_scope(score->addr_type); |
| 1256 | if (ret >= dst->scope) |
| 1257 | ret = -ret; |
| 1258 | else |
| 1259 | ret -= 128; /* 30 is enough */ |
| 1260 | score->scopedist = ret; |
| 1261 | break; |
| 1262 | case IPV6_SADDR_RULE_PREFERRED: |
| 1263 | /* Rule 3: Avoid deprecated and optimistic addresses */ |
| 1264 | ret = ipv6_saddr_preferred(score->addr_type) || |
| 1265 | !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); |
| 1266 | break; |
| 1267 | #ifdef CONFIG_IPV6_MIP6 |
| 1268 | case IPV6_SADDR_RULE_HOA: |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1269 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1270 | /* Rule 4: Prefer home address */ |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1271 | int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); |
| 1272 | ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1273 | break; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1274 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1275 | #endif |
| 1276 | case IPV6_SADDR_RULE_OIF: |
| 1277 | /* Rule 5: Prefer outgoing interface */ |
| 1278 | ret = (!dst->ifindex || |
| 1279 | dst->ifindex == score->ifa->idev->dev->ifindex); |
| 1280 | break; |
| 1281 | case IPV6_SADDR_RULE_LABEL: |
| 1282 | /* Rule 6: Prefer matching label */ |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1283 | ret = ipv6_addr_label(net, |
| 1284 | &score->ifa->addr, score->addr_type, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1285 | score->ifa->idev->dev->ifindex) == dst->label; |
| 1286 | break; |
| 1287 | #ifdef CONFIG_IPV6_PRIVACY |
| 1288 | case IPV6_SADDR_RULE_PRIVACY: |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1289 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1290 | /* Rule 7: Prefer public address |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1291 | * Note: prefer temporary address if use_tempaddr >= 2 |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1292 | */ |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1293 | int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? |
| 1294 | !!(dst->prefs & IPV6_PREFER_SRC_TMP) : |
| 1295 | score->ifa->idev->cnf.use_tempaddr >= 2; |
| 1296 | ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1297 | break; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1298 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1299 | #endif |
| 1300 | case IPV6_SADDR_RULE_ORCHID: |
| 1301 | /* Rule 8-: Prefer ORCHID vs ORCHID or |
| 1302 | * non-ORCHID vs non-ORCHID |
| 1303 | */ |
| 1304 | ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ |
| 1305 | ipv6_addr_orchid(dst->addr)); |
| 1306 | break; |
| 1307 | case IPV6_SADDR_RULE_PREFIX: |
| 1308 | /* Rule 8: Use longest matching prefix */ |
YOSHIFUJI Hideaki / 吉藤英明 | 91b4b04 | 2012-09-10 18:41:07 +0000 | [diff] [blame] | 1309 | ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); |
| 1310 | if (ret > score->ifa->prefix_len) |
| 1311 | ret = score->ifa->prefix_len; |
| 1312 | score->matchlen = ret; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1313 | break; |
| 1314 | default: |
| 1315 | ret = 0; |
| 1316 | } |
| 1317 | |
| 1318 | if (ret) |
| 1319 | __set_bit(i, score->scorebits); |
| 1320 | score->rule = i; |
| 1321 | out: |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
Patrick McHardy | b3f644f | 2012-08-26 19:14:14 +0200 | [diff] [blame] | 1325 | int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1326 | const struct in6_addr *daddr, unsigned int prefs, |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1327 | struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1329 | struct ipv6_saddr_score scores[2], |
| 1330 | *score = &scores[0], *hiscore = &scores[1]; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1331 | struct ipv6_saddr_dst dst; |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1332 | struct net_device *dev; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1333 | int dst_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1335 | dst_type = __ipv6_addr_type(daddr); |
| 1336 | dst.addr = daddr; |
| 1337 | dst.ifindex = dst_dev ? dst_dev->ifindex : 0; |
| 1338 | dst.scope = __ipv6_addr_src_scope(dst_type); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1339 | dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1340 | dst.prefs = prefs; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1341 | |
| 1342 | hiscore->rule = -1; |
| 1343 | hiscore->ifa = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1345 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1346 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1347 | for_each_netdev_rcu(net, dev) { |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1348 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1349 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1350 | /* Candidate Source Address (section 4) |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1351 | * - multicast and link-local destination address, |
| 1352 | * the set of candidate source address MUST only |
| 1353 | * include addresses assigned to interfaces |
| 1354 | * belonging to the same link as the outgoing |
| 1355 | * interface. |
| 1356 | * (- For site-local destination addresses, the |
| 1357 | * set of candidate source addresses MUST only |
| 1358 | * include addresses assigned to interfaces |
| 1359 | * belonging to the same site as the outgoing |
| 1360 | * interface.) |
| 1361 | */ |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1362 | if (((dst_type & IPV6_ADDR_MULTICAST) || |
| 1363 | dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && |
| 1364 | dst.ifindex && dev->ifindex != dst.ifindex) |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1365 | continue; |
| 1366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | idev = __in6_dev_get(dev); |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1368 | if (!idev) |
| 1369 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1371 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1372 | list_for_each_entry(score->ifa, &idev->addr_list, if_list) { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1373 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1375 | /* |
YOSHIFUJI Hideaki | 6b3ae80 | 2005-12-21 22:58:01 +0900 | [diff] [blame] | 1376 | * - Tentative Address (RFC2462 section 5.4) |
| 1377 | * - A tentative address is not considered |
| 1378 | * "assigned to an interface" in the traditional |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1379 | * sense, unless it is also flagged as optimistic. |
YOSHIFUJI Hideaki | 6b3ae80 | 2005-12-21 22:58:01 +0900 | [diff] [blame] | 1380 | * - Candidate Source Address (section 4) |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1381 | * - In any case, anycast addresses, multicast |
| 1382 | * addresses, and the unspecified address MUST |
| 1383 | * NOT be included in a candidate set. |
| 1384 | */ |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1385 | if ((score->ifa->flags & IFA_F_TENTATIVE) && |
| 1386 | (!(score->ifa->flags & IFA_F_OPTIMISTIC))) |
YOSHIFUJI Hideaki | 6b3ae80 | 2005-12-21 22:58:01 +0900 | [diff] [blame] | 1387 | continue; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1388 | |
| 1389 | score->addr_type = __ipv6_addr_type(&score->ifa->addr); |
| 1390 | |
| 1391 | if (unlikely(score->addr_type == IPV6_ADDR_ANY || |
| 1392 | score->addr_type & IPV6_ADDR_MULTICAST)) { |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1393 | LIMIT_NETDEBUG(KERN_DEBUG |
Joe Perches | 3b6d821c | 2007-11-19 23:47:25 -0800 | [diff] [blame] | 1394 | "ADDRCONF: unspecified / multicast address " |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1395 | "assigned as unicast address on %s", |
| 1396 | dev->name); |
| 1397 | continue; |
| 1398 | } |
| 1399 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1400 | score->rule = -1; |
| 1401 | bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1402 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1403 | for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { |
| 1404 | int minihiscore, miniscore; |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1405 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1406 | minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); |
| 1407 | miniscore = ipv6_get_saddr_eval(net, score, &dst, i); |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1408 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1409 | if (minihiscore > miniscore) { |
| 1410 | if (i == IPV6_SADDR_RULE_SCOPE && |
| 1411 | score->scopedist > 0) { |
| 1412 | /* |
| 1413 | * special case: |
| 1414 | * each remaining entry |
| 1415 | * has too small (not enough) |
| 1416 | * scope, because ifa entries |
| 1417 | * are sorted by their scope |
| 1418 | * values. |
| 1419 | */ |
| 1420 | goto try_nextdev; |
| 1421 | } |
| 1422 | break; |
| 1423 | } else if (minihiscore < miniscore) { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1424 | if (hiscore->ifa) |
| 1425 | in6_ifa_put(hiscore->ifa); |
| 1426 | |
| 1427 | in6_ifa_hold(score->ifa); |
| 1428 | |
Ilpo Järvinen | a0bffff | 2009-03-21 13:36:17 -0700 | [diff] [blame] | 1429 | swap(hiscore, score); |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1430 | |
| 1431 | /* restore our iterator */ |
| 1432 | score->ifa = hiscore->ifa; |
| 1433 | |
| 1434 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1437 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1438 | try_nextdev: |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1439 | read_unlock_bh(&idev->lock); |
| 1440 | } |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1441 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1443 | if (!hiscore->ifa) |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1444 | return -EADDRNOTAVAIL; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1445 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1446 | *saddr = hiscore->ifa->addr; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1447 | in6_ifa_put(hiscore->ifa); |
YOSHIFUJI Hideaki | 072047e | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1448 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
YOSHIFUJI Hideaki | 5e5f3f0 | 2008-03-03 21:44:34 +0900 | [diff] [blame] | 1450 | EXPORT_SYMBOL(ipv6_dev_get_saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1452 | int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, |
| 1453 | unsigned char banned_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | { |
| 1455 | struct inet6_dev *idev; |
| 1456 | int err = -EADDRNOTAVAIL; |
| 1457 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1458 | rcu_read_lock(); |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 1459 | idev = __in6_dev_get(dev); |
| 1460 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | struct inet6_ifaddr *ifp; |
| 1462 | |
| 1463 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1464 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
| 1465 | if (ifp->scope == IFA_LINK && |
| 1466 | !(ifp->flags & banned_flags)) { |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1467 | *addr = ifp->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | err = 0; |
| 1469 | break; |
| 1470 | } |
| 1471 | } |
| 1472 | read_unlock_bh(&idev->lock); |
| 1473 | } |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1474 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | return err; |
| 1476 | } |
| 1477 | |
| 1478 | static int ipv6_count_addresses(struct inet6_dev *idev) |
| 1479 | { |
| 1480 | int cnt = 0; |
| 1481 | struct inet6_ifaddr *ifp; |
| 1482 | |
| 1483 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1484 | list_for_each_entry(ifp, &idev->addr_list, if_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | cnt++; |
| 1486 | read_unlock_bh(&idev->lock); |
| 1487 | return cnt; |
| 1488 | } |
| 1489 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1490 | int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 1491 | struct net_device *dev, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1493 | struct inet6_ifaddr *ifp; |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 1494 | unsigned int hash = inet6_addr_hash(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1496 | rcu_read_lock_bh(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1497 | hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1498 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 1499 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | if (ipv6_addr_equal(&ifp->addr, addr) && |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1501 | !(ifp->flags&IFA_F_TENTATIVE) && |
| 1502 | (dev == NULL || ifp->idev->dev == dev || |
| 1503 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { |
| 1504 | rcu_read_unlock_bh(); |
| 1505 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | } |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1508 | |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1509 | rcu_read_unlock_bh(); |
| 1510 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1512 | EXPORT_SYMBOL(ipv6_chk_addr); |
| 1513 | |
David S. Miller | 3e81c6d | 2010-03-20 16:18:00 -0700 | [diff] [blame] | 1514 | static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, |
| 1515 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | { |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 1517 | unsigned int hash = inet6_addr_hash(addr); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 1518 | struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1520 | hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1521 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | 06bfe65 | 2008-01-10 22:43:42 -0800 | [diff] [blame] | 1522 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
| 1524 | if (dev == NULL || ifp->idev->dev == dev) |
David S. Miller | 3e81c6d | 2010-03-20 16:18:00 -0700 | [diff] [blame] | 1525 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | } |
| 1527 | } |
David S. Miller | 3e81c6d | 2010-03-20 16:18:00 -0700 | [diff] [blame] | 1528 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1531 | int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1532 | { |
| 1533 | struct inet6_dev *idev; |
| 1534 | struct inet6_ifaddr *ifa; |
| 1535 | int onlink; |
| 1536 | |
| 1537 | onlink = 0; |
| 1538 | rcu_read_lock(); |
| 1539 | idev = __in6_dev_get(dev); |
| 1540 | if (idev) { |
| 1541 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1542 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1543 | onlink = ipv6_prefix_equal(addr, &ifa->addr, |
| 1544 | ifa->prefix_len); |
| 1545 | if (onlink) |
| 1546 | break; |
| 1547 | } |
| 1548 | read_unlock_bh(&idev->lock); |
| 1549 | } |
| 1550 | rcu_read_unlock(); |
| 1551 | return onlink; |
| 1552 | } |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1553 | EXPORT_SYMBOL(ipv6_chk_prefix); |
| 1554 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1555 | struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 1556 | struct net_device *dev, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | { |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 1558 | struct inet6_ifaddr *ifp, *result = NULL; |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 1559 | unsigned int hash = inet6_addr_hash(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1561 | rcu_read_lock_bh(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1562 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1563 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 1564 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
| 1566 | if (dev == NULL || ifp->idev->dev == dev || |
| 1567 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 1568 | result = ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | in6_ifa_hold(ifp); |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
| 1573 | } |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1574 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 1576 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | /* Gets referenced address, destroys ifaddr */ |
| 1580 | |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 1581 | static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (ifp->flags&IFA_F_PERMANENT) { |
| 1584 | spin_lock_bh(&ifp->lock); |
| 1585 | addrconf_del_timer(ifp); |
| 1586 | ifp->flags |= IFA_F_TENTATIVE; |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 1587 | if (dad_failed) |
| 1588 | ifp->flags |= IFA_F_DADFAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | spin_unlock_bh(&ifp->lock); |
Herbert Xu | e2577a0 | 2010-03-13 12:23:29 -0800 | [diff] [blame] | 1590 | if (dad_failed) |
| 1591 | ipv6_ifa_notify(0, ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | in6_ifa_put(ifp); |
| 1593 | #ifdef CONFIG_IPV6_PRIVACY |
| 1594 | } else if (ifp->flags&IFA_F_TEMPORARY) { |
| 1595 | struct inet6_ifaddr *ifpub; |
| 1596 | spin_lock_bh(&ifp->lock); |
| 1597 | ifpub = ifp->ifpub; |
| 1598 | if (ifpub) { |
| 1599 | in6_ifa_hold(ifpub); |
| 1600 | spin_unlock_bh(&ifp->lock); |
| 1601 | ipv6_create_tempaddr(ifpub, ifp); |
| 1602 | in6_ifa_put(ifpub); |
| 1603 | } else { |
| 1604 | spin_unlock_bh(&ifp->lock); |
| 1605 | } |
| 1606 | ipv6_del_addr(ifp); |
| 1607 | #endif |
| 1608 | } else |
| 1609 | ipv6_del_addr(ifp); |
| 1610 | } |
| 1611 | |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 1612 | static int addrconf_dad_end(struct inet6_ifaddr *ifp) |
| 1613 | { |
| 1614 | int err = -ENOENT; |
| 1615 | |
| 1616 | spin_lock(&ifp->state_lock); |
| 1617 | if (ifp->state == INET6_IFADDR_STATE_DAD) { |
| 1618 | ifp->state = INET6_IFADDR_STATE_POSTDAD; |
| 1619 | err = 0; |
| 1620 | } |
| 1621 | spin_unlock(&ifp->state_lock); |
| 1622 | |
| 1623 | return err; |
| 1624 | } |
| 1625 | |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 1626 | void addrconf_dad_failure(struct inet6_ifaddr *ifp) |
| 1627 | { |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 1628 | struct inet6_dev *idev = ifp->idev; |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1629 | |
Ursula Braun | 853dc2e | 2010-10-24 23:06:43 +0000 | [diff] [blame] | 1630 | if (addrconf_dad_end(ifp)) { |
| 1631 | in6_ifa_put(ifp); |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 1632 | return; |
Ursula Braun | 853dc2e | 2010-10-24 23:06:43 +0000 | [diff] [blame] | 1633 | } |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 1634 | |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1635 | net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", |
| 1636 | ifp->idev->dev->name, &ifp->addr); |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1637 | |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 1638 | if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { |
| 1639 | struct in6_addr addr; |
| 1640 | |
| 1641 | addr.s6_addr32[0] = htonl(0xfe800000); |
| 1642 | addr.s6_addr32[1] = 0; |
| 1643 | |
| 1644 | if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && |
| 1645 | ipv6_addr_equal(&ifp->addr, &addr)) { |
| 1646 | /* DAD failed for link-local based on MAC address */ |
| 1647 | idev->cnf.disable_ipv6 = 1; |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1648 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1649 | pr_info("%s: IPv6 being disabled!\n", |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1650 | ifp->idev->dev->name); |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 1654 | addrconf_dad_stop(ifp, 1); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 1655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | /* Join to solicited addr multicast group. */ |
| 1658 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1659 | void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | { |
| 1661 | struct in6_addr maddr; |
| 1662 | |
| 1663 | if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) |
| 1664 | return; |
| 1665 | |
| 1666 | addrconf_addr_solict_mult(addr, &maddr); |
| 1667 | ipv6_dev_mc_inc(dev, &maddr); |
| 1668 | } |
| 1669 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1670 | void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | { |
| 1672 | struct in6_addr maddr; |
| 1673 | |
| 1674 | if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) |
| 1675 | return; |
| 1676 | |
| 1677 | addrconf_addr_solict_mult(addr, &maddr); |
| 1678 | __ipv6_dev_mc_dec(idev, &maddr); |
| 1679 | } |
| 1680 | |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 1681 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | { |
| 1683 | struct in6_addr addr; |
Bjørn Mork | 2bda8a0 | 2011-07-05 23:04:13 +0000 | [diff] [blame] | 1684 | if (ifp->prefix_len == 127) /* RFC 6164 */ |
| 1685 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
| 1687 | if (ipv6_addr_any(&addr)) |
| 1688 | return; |
| 1689 | ipv6_dev_ac_inc(ifp->idev->dev, &addr); |
| 1690 | } |
| 1691 | |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 1692 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | { |
| 1694 | struct in6_addr addr; |
YOSHIFUJI Hideaki | 32019e6 | 2011-07-24 11:44:34 +0000 | [diff] [blame] | 1695 | if (ifp->prefix_len == 127) /* RFC 6164 */ |
| 1696 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
| 1698 | if (ipv6_addr_any(&addr)) |
| 1699 | return; |
| 1700 | __ipv6_dev_ac_dec(ifp->idev, &addr); |
| 1701 | } |
| 1702 | |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1703 | static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) |
| 1704 | { |
| 1705 | if (dev->addr_len != ETH_ALEN) |
| 1706 | return -1; |
| 1707 | memcpy(eui, dev->dev_addr, 3); |
| 1708 | memcpy(eui + 5, dev->dev_addr + 3, 3); |
| 1709 | |
| 1710 | /* |
| 1711 | * The zSeries OSA network cards can be shared among various |
| 1712 | * OS instances, but the OSA cards have only one MAC address. |
| 1713 | * This leads to duplicate address conflicts in conjunction |
| 1714 | * with IPv6 if more than one instance uses the same card. |
| 1715 | * |
| 1716 | * The driver for these cards can deliver a unique 16-bit |
| 1717 | * identifier for each instance sharing the same card. It is |
| 1718 | * placed instead of 0xFFFE in the interface identifier. The |
| 1719 | * "u" bit of the interface identifier is not inverted in this |
| 1720 | * case. Hence the resulting interface identifier has local |
| 1721 | * scope according to RFC2373. |
| 1722 | */ |
| 1723 | if (dev->dev_id) { |
| 1724 | eui[3] = (dev->dev_id >> 8) & 0xFF; |
| 1725 | eui[4] = dev->dev_id & 0xFF; |
| 1726 | } else { |
| 1727 | eui[3] = 0xFF; |
| 1728 | eui[4] = 0xFE; |
| 1729 | eui[0] ^= 2; |
| 1730 | } |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 1734 | static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) |
| 1735 | { |
| 1736 | if (dev->addr_len != IEEE802154_ADDR_LEN) |
| 1737 | return -1; |
| 1738 | memcpy(eui, dev->dev_addr, 8); |
YOSHIFUJI Hideaki / 吉藤英明 | 5e98a36 | 2013-01-28 10:44:29 +0000 | [diff] [blame] | 1739 | eui[0] ^= 2; |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 1740 | return 0; |
| 1741 | } |
| 1742 | |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 1743 | static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) |
| 1744 | { |
| 1745 | union fwnet_hwaddr *ha; |
| 1746 | |
| 1747 | if (dev->addr_len != FWNET_ALEN) |
| 1748 | return -1; |
| 1749 | |
| 1750 | ha = (union fwnet_hwaddr *)dev->dev_addr; |
| 1751 | |
| 1752 | memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); |
| 1753 | eui[0] ^= 2; |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1757 | static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) |
| 1758 | { |
| 1759 | /* XXX: inherit EUI-64 from other interface -- yoshfuji */ |
| 1760 | if (dev->addr_len != ARCNET_ALEN) |
| 1761 | return -1; |
| 1762 | memset(eui, 0, 7); |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 1763 | eui[7] = *(u8 *)dev->dev_addr; |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) |
| 1768 | { |
| 1769 | if (dev->addr_len != INFINIBAND_ALEN) |
| 1770 | return -1; |
| 1771 | memcpy(eui, dev->dev_addr + 12, 8); |
| 1772 | eui[0] |= 2; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
stephen hemminger | c61393e | 2010-10-04 20:17:53 +0000 | [diff] [blame] | 1776 | static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 1777 | { |
Sascha Hlusiak | 9af2851 | 2009-05-19 12:56:51 +0000 | [diff] [blame] | 1778 | if (addr == 0) |
| 1779 | return -1; |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 1780 | eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || |
| 1781 | ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || |
| 1782 | ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || |
| 1783 | ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || |
| 1784 | ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || |
| 1785 | ipv4_is_lbcast(addr)) ? 0x00 : 0x02; |
| 1786 | eui[1] = 0; |
| 1787 | eui[2] = 0x5E; |
| 1788 | eui[3] = 0xFE; |
| 1789 | memcpy(eui + 4, &addr, 4); |
| 1790 | return 0; |
| 1791 | } |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 1792 | |
| 1793 | static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) |
| 1794 | { |
| 1795 | if (dev->priv_flags & IFF_ISATAP) |
| 1796 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); |
| 1797 | return -1; |
| 1798 | } |
| 1799 | |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 1800 | static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) |
| 1801 | { |
| 1802 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); |
| 1803 | } |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) |
| 1806 | { |
| 1807 | switch (dev->type) { |
| 1808 | case ARPHRD_ETHER: |
| 1809 | case ARPHRD_FDDI: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1810 | return addrconf_ifid_eui48(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | case ARPHRD_ARCNET: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1812 | return addrconf_ifid_arcnet(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | case ARPHRD_INFINIBAND: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 1814 | return addrconf_ifid_infiniband(eui, dev); |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 1815 | case ARPHRD_SIT: |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 1816 | return addrconf_ifid_sit(eui, dev); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 1817 | case ARPHRD_IPGRE: |
| 1818 | return addrconf_ifid_gre(eui, dev); |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 1819 | case ARPHRD_IEEE802154: |
| 1820 | return addrconf_ifid_eui64(eui, dev); |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 1821 | case ARPHRD_IEEE1394: |
| 1822 | return addrconf_ifid_ieee1394(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | } |
| 1824 | return -1; |
| 1825 | } |
| 1826 | |
| 1827 | static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) |
| 1828 | { |
| 1829 | int err = -1; |
| 1830 | struct inet6_ifaddr *ifp; |
| 1831 | |
| 1832 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1833 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { |
| 1835 | memcpy(eui, ifp->addr.s6_addr+8, 8); |
| 1836 | err = 0; |
| 1837 | break; |
| 1838 | } |
| 1839 | } |
| 1840 | read_unlock_bh(&idev->lock); |
| 1841 | return err; |
| 1842 | } |
| 1843 | |
| 1844 | #ifdef CONFIG_IPV6_PRIVACY |
| 1845 | /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 1846 | static void __ipv6_regen_rndid(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | regen: |
YOSHIFUJI Hideaki | 955189e | 2006-03-20 16:54:09 -0800 | [diff] [blame] | 1849 | get_random_bytes(idev->rndid, sizeof(idev->rndid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | idev->rndid[0] &= ~0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
| 1852 | /* |
| 1853 | * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: |
| 1854 | * check if generated address is not inappropriate |
| 1855 | * |
| 1856 | * - Reserved subnet anycast (RFC 2526) |
| 1857 | * 11111101 11....11 1xxxxxxx |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 1858 | * - ISATAP (RFC4214) 6.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | * 00-00-5E-FE-xx-xx-xx-xx |
| 1860 | * - value 0 |
| 1861 | * - XXX: already assigned to an address on the device |
| 1862 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1863 | if (idev->rndid[0] == 0xfd && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && |
| 1865 | (idev->rndid[7]&0x80)) |
| 1866 | goto regen; |
| 1867 | if ((idev->rndid[0]|idev->rndid[1]) == 0) { |
| 1868 | if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) |
| 1869 | goto regen; |
| 1870 | if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) |
| 1871 | goto regen; |
| 1872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static void ipv6_regen_rndid(unsigned long data) |
| 1876 | { |
| 1877 | struct inet6_dev *idev = (struct inet6_dev *) data; |
| 1878 | unsigned long expires; |
| 1879 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1880 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | write_lock_bh(&idev->lock); |
| 1882 | |
| 1883 | if (idev->dead) |
| 1884 | goto out; |
| 1885 | |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 1886 | __ipv6_regen_rndid(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | expires = jiffies + |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1889 | idev->cnf.temp_prefered_lft * HZ - |
Ben Hutchings | 784e271 | 2010-06-26 11:42:55 +0000 | [diff] [blame] | 1890 | idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - |
| 1891 | idev->cnf.max_desync_factor * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | if (time_before(expires, jiffies)) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1893 | pr_warn("%s: too short regeneration interval; timer disabled for %s\n", |
| 1894 | __func__, idev->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | goto out; |
| 1896 | } |
| 1897 | |
| 1898 | if (!mod_timer(&idev->regen_timer, expires)) |
| 1899 | in6_dev_hold(idev); |
| 1900 | |
| 1901 | out: |
| 1902 | write_unlock_bh(&idev->lock); |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1903 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | in6_dev_put(idev); |
| 1905 | } |
| 1906 | |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 1907 | static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 1908 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) |
Sorin Dumitru | eb7e057 | 2012-08-30 02:01:45 +0000 | [diff] [blame] | 1910 | __ipv6_regen_rndid(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | } |
| 1912 | #endif |
| 1913 | |
| 1914 | /* |
| 1915 | * Add prefix route. |
| 1916 | */ |
| 1917 | |
| 1918 | static void |
| 1919 | addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, |
Jamal Hadi Salim | e431b8c | 2005-06-18 22:55:31 -0700 | [diff] [blame] | 1920 | unsigned long expires, u32 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1922 | struct fib6_config cfg = { |
| 1923 | .fc_table = RT6_TABLE_PREFIX, |
| 1924 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
| 1925 | .fc_ifindex = dev->ifindex, |
| 1926 | .fc_expires = expires, |
| 1927 | .fc_dst_len = plen, |
| 1928 | .fc_flags = RTF_UP | flags, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1929 | .fc_nlinfo.nl_net = dev_net(dev), |
Stephen Hemminger | f410a1f | 2008-08-23 05:16:46 -0700 | [diff] [blame] | 1930 | .fc_protocol = RTPROT_KERNEL, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1931 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1933 | cfg.fc_dst = *pfx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
| 1935 | /* Prevent useless cloning on PtP SIT. |
| 1936 | This thing is done here expecting that the whole |
| 1937 | class of non-broadcast devices need not cloning. |
| 1938 | */ |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 1939 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1940 | if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) |
| 1941 | cfg.fc_flags |= RTF_NONEXTHOP; |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 1942 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1944 | ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 1947 | |
| 1948 | static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, |
| 1949 | int plen, |
| 1950 | const struct net_device *dev, |
| 1951 | u32 flags, u32 noflags) |
| 1952 | { |
| 1953 | struct fib6_node *fn; |
| 1954 | struct rt6_info *rt = NULL; |
| 1955 | struct fib6_table *table; |
| 1956 | |
| 1957 | table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); |
| 1958 | if (table == NULL) |
| 1959 | return NULL; |
| 1960 | |
Li RongQing | 5744dd9 | 2012-09-11 21:59:01 +0000 | [diff] [blame] | 1961 | read_lock_bh(&table->tb6_lock); |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 1962 | fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); |
| 1963 | if (!fn) |
| 1964 | goto out; |
| 1965 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 1966 | if (rt->dst.dev->ifindex != dev->ifindex) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 1967 | continue; |
| 1968 | if ((rt->rt6i_flags & flags) != flags) |
| 1969 | continue; |
Romain Kuntz | 85da53b | 2013-01-09 15:02:26 +0100 | [diff] [blame] | 1970 | if ((rt->rt6i_flags & noflags) != 0) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 1971 | continue; |
| 1972 | dst_hold(&rt->dst); |
| 1973 | break; |
| 1974 | } |
| 1975 | out: |
Li RongQing | 5744dd9 | 2012-09-11 21:59:01 +0000 | [diff] [blame] | 1976 | read_unlock_bh(&table->tb6_lock); |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 1977 | return rt; |
| 1978 | } |
| 1979 | |
| 1980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* Create "default" multicast route to the interface */ |
| 1982 | |
| 1983 | static void addrconf_add_mroute(struct net_device *dev) |
| 1984 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1985 | struct fib6_config cfg = { |
| 1986 | .fc_table = RT6_TABLE_LOCAL, |
| 1987 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
| 1988 | .fc_ifindex = dev->ifindex, |
| 1989 | .fc_dst_len = 8, |
| 1990 | .fc_flags = RTF_UP, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1991 | .fc_nlinfo.nl_net = dev_net(dev), |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1992 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1994 | ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); |
| 1995 | |
| 1996 | ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 1999 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | static void sit_route_add(struct net_device *dev) |
| 2001 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2002 | struct fib6_config cfg = { |
| 2003 | .fc_table = RT6_TABLE_MAIN, |
| 2004 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
| 2005 | .fc_ifindex = dev->ifindex, |
| 2006 | .fc_dst_len = 96, |
| 2007 | .fc_flags = RTF_UP | RTF_NONEXTHOP, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2008 | .fc_nlinfo.nl_net = dev_net(dev), |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2009 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
| 2011 | /* prefix length - 96 bits "::d.d.d.d" */ |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2012 | ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2014 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | static struct inet6_dev *addrconf_add_dev(struct net_device *dev) |
| 2017 | { |
| 2018 | struct inet6_dev *idev; |
| 2019 | |
| 2020 | ASSERT_RTNL(); |
| 2021 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 2022 | idev = ipv6_find_idev(dev); |
| 2023 | if (!idev) |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2024 | return ERR_PTR(-ENOBUFS); |
| 2025 | |
| 2026 | if (idev->cnf.disable_ipv6) |
| 2027 | return ERR_PTR(-EACCES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
| 2029 | /* Add default multicast route */ |
Li Wei | 4af04ab | 2011-12-06 21:23:45 +0000 | [diff] [blame] | 2030 | if (!(dev->flags & IFF_LOOPBACK)) |
| 2031 | addrconf_add_mroute(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | return idev; |
| 2034 | } |
| 2035 | |
Neil Horman | e6bff99 | 2012-01-04 10:49:15 +0000 | [diff] [blame] | 2036 | void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | { |
| 2038 | struct prefix_info *pinfo; |
| 2039 | __u32 valid_lft; |
| 2040 | __u32 prefered_lft; |
| 2041 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | struct inet6_dev *in6_dev; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 2043 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
| 2045 | pinfo = (struct prefix_info *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | if (len < sizeof(struct prefix_info)) { |
| 2048 | ADBG(("addrconf: prefix option too short\n")); |
| 2049 | return; |
| 2050 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | /* |
| 2053 | * Validation checks ([ADDRCONF], page 19) |
| 2054 | */ |
| 2055 | |
| 2056 | addr_type = ipv6_addr_type(&pinfo->prefix); |
| 2057 | |
| 2058 | if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) |
| 2059 | return; |
| 2060 | |
| 2061 | valid_lft = ntohl(pinfo->valid); |
| 2062 | prefered_lft = ntohl(pinfo->prefered); |
| 2063 | |
| 2064 | if (prefered_lft > valid_lft) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2065 | net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | return; |
| 2067 | } |
| 2068 | |
| 2069 | in6_dev = in6_dev_get(dev); |
| 2070 | |
| 2071 | if (in6_dev == NULL) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2072 | net_dbg_ratelimited("addrconf: device %s not configured\n", |
| 2073 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | return; |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * Two things going on here: |
| 2079 | * 1) Add routes for on-link prefixes |
| 2080 | * 2) Configure prefixes with the auto flag set |
| 2081 | */ |
| 2082 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2083 | if (pinfo->onlink) { |
| 2084 | struct rt6_info *rt; |
| 2085 | unsigned long rt_expires; |
| 2086 | |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2087 | /* Avoid arithmetic overflow. Really, we could |
| 2088 | * save rt_expires in seconds, likely valid_lft, |
| 2089 | * but it would require division in fib gc, that it |
| 2090 | * not good. |
| 2091 | */ |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2092 | if (HZ > USER_HZ) |
| 2093 | rt_expires = addrconf_timeout_fixup(valid_lft, HZ); |
| 2094 | else |
| 2095 | rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); |
YOSHIFUJI Hideaki | 3dd4bc6 | 2005-12-19 14:02:45 -0800 | [diff] [blame] | 2096 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2097 | if (addrconf_finite_timeout(rt_expires)) |
| 2098 | rt_expires *= HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2100 | rt = addrconf_get_prefix_route(&pinfo->prefix, |
| 2101 | pinfo->prefix_len, |
| 2102 | dev, |
| 2103 | RTF_ADDRCONF | RTF_PREFIX_RT, |
| 2104 | RTF_GATEWAY | RTF_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2106 | if (rt) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2107 | /* Autoconf prefix route */ |
| 2108 | if (valid_lft == 0) { |
| 2109 | ip6_del_rt(rt); |
| 2110 | rt = NULL; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2111 | } else if (addrconf_finite_timeout(rt_expires)) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2112 | /* not infinity */ |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2113 | rt6_set_expires(rt, jiffies + rt_expires); |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2114 | } else { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2115 | rt6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | } else if (valid_lft) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2118 | clock_t expires = 0; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2119 | int flags = RTF_ADDRCONF | RTF_PREFIX_RT; |
| 2120 | if (addrconf_finite_timeout(rt_expires)) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2121 | /* not infinity */ |
| 2122 | flags |= RTF_EXPIRES; |
| 2123 | expires = jiffies_to_clock_t(rt_expires); |
| 2124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2126 | dev, expires, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 2128 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /* Try to figure out our local address for this prefix */ |
| 2132 | |
| 2133 | if (pinfo->autoconf && in6_dev->cnf.autoconf) { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2134 | struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | struct in6_addr addr; |
| 2136 | int create = 0, update_lft = 0; |
| 2137 | |
| 2138 | if (pinfo->prefix_len == 64) { |
| 2139 | memcpy(&addr, &pinfo->prefix, 8); |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 2140 | |
| 2141 | if (!ipv6_addr_any(&in6_dev->token)) { |
| 2142 | read_lock_bh(&in6_dev->lock); |
| 2143 | memcpy(addr.s6_addr + 8, |
| 2144 | in6_dev->token.s6_addr + 8, 8); |
| 2145 | read_unlock_bh(&in6_dev->lock); |
| 2146 | } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && |
| 2147 | ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | in6_dev_put(in6_dev); |
| 2149 | return; |
| 2150 | } |
| 2151 | goto ok; |
| 2152 | } |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2153 | net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", |
| 2154 | pinfo->prefix_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | in6_dev_put(in6_dev); |
| 2156 | return; |
| 2157 | |
| 2158 | ok: |
| 2159 | |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 2160 | ifp = ipv6_get_ifaddr(net, &addr, dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
| 2162 | if (ifp == NULL && valid_lft) { |
| 2163 | int max_addresses = in6_dev->cnf.max_addresses; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2164 | u32 addr_flags = 0; |
| 2165 | |
| 2166 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 2167 | if (in6_dev->cnf.optimistic_dad && |
Neil Horman | e6bff99 | 2012-01-04 10:49:15 +0000 | [diff] [blame] | 2168 | !net->ipv6.devconf_all->forwarding && sllao) |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2169 | addr_flags = IFA_F_OPTIMISTIC; |
| 2170 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
| 2172 | /* Do not allow to create too much of autoconfigured |
| 2173 | * addresses; this would be too easy way to crash kernel. |
| 2174 | */ |
| 2175 | if (!max_addresses || |
| 2176 | ipv6_count_addresses(in6_dev) < max_addresses) |
| 2177 | ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2178 | addr_type&IPV6_ADDR_SCOPE_MASK, |
| 2179 | addr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | |
YOSHIFUJI Hideaki / 吉藤英明 | 3f0d2ba | 2013-01-22 06:32:54 +0000 | [diff] [blame] | 2181 | if (IS_ERR_OR_NULL(ifp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | in6_dev_put(in6_dev); |
| 2183 | return; |
| 2184 | } |
| 2185 | |
| 2186 | update_lft = create = 1; |
| 2187 | ifp->cstamp = jiffies; |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 2188 | addrconf_dad_start(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | if (ifp) { |
| 2192 | int flags; |
| 2193 | unsigned long now; |
| 2194 | #ifdef CONFIG_IPV6_PRIVACY |
| 2195 | struct inet6_ifaddr *ift; |
| 2196 | #endif |
| 2197 | u32 stored_lft; |
| 2198 | |
| 2199 | /* update lifetime (RFC2462 5.5.3 e) */ |
| 2200 | spin_lock(&ifp->lock); |
| 2201 | now = jiffies; |
| 2202 | if (ifp->valid_lft > (now - ifp->tstamp) / HZ) |
| 2203 | stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; |
| 2204 | else |
| 2205 | stored_lft = 0; |
| 2206 | if (!update_lft && stored_lft) { |
| 2207 | if (valid_lft > MIN_VALID_LIFETIME || |
| 2208 | valid_lft > stored_lft) |
| 2209 | update_lft = 1; |
| 2210 | else if (stored_lft <= MIN_VALID_LIFETIME) { |
| 2211 | /* valid_lft <= stored_lft is always true */ |
Brian Haley | a1ed052 | 2009-07-02 07:10:52 +0000 | [diff] [blame] | 2212 | /* |
| 2213 | * RFC 4862 Section 5.5.3e: |
| 2214 | * "Note that the preferred lifetime of |
| 2215 | * the corresponding address is always |
| 2216 | * reset to the Preferred Lifetime in |
| 2217 | * the received Prefix Information |
| 2218 | * option, regardless of whether the |
| 2219 | * valid lifetime is also reset or |
| 2220 | * ignored." |
| 2221 | * |
| 2222 | * So if the preferred lifetime in |
| 2223 | * this advertisement is different |
| 2224 | * than what we have stored, but the |
| 2225 | * valid lifetime is invalid, just |
| 2226 | * reset prefered_lft. |
| 2227 | * |
| 2228 | * We must set the valid lifetime |
| 2229 | * to the stored lifetime since we'll |
| 2230 | * be updating the timestamp below, |
| 2231 | * else we'll set it back to the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2232 | * minimum. |
Brian Haley | a1ed052 | 2009-07-02 07:10:52 +0000 | [diff] [blame] | 2233 | */ |
| 2234 | if (prefered_lft != ifp->prefered_lft) { |
| 2235 | valid_lft = stored_lft; |
| 2236 | update_lft = 1; |
| 2237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } else { |
| 2239 | valid_lft = MIN_VALID_LIFETIME; |
| 2240 | if (valid_lft < prefered_lft) |
| 2241 | prefered_lft = valid_lft; |
| 2242 | update_lft = 1; |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | if (update_lft) { |
| 2247 | ifp->valid_lft = valid_lft; |
| 2248 | ifp->prefered_lft = prefered_lft; |
| 2249 | ifp->tstamp = now; |
| 2250 | flags = ifp->flags; |
| 2251 | ifp->flags &= ~IFA_F_DEPRECATED; |
| 2252 | spin_unlock(&ifp->lock); |
| 2253 | |
| 2254 | if (!(flags&IFA_F_TENTATIVE)) |
| 2255 | ipv6_ifa_notify(0, ifp); |
| 2256 | } else |
| 2257 | spin_unlock(&ifp->lock); |
| 2258 | |
| 2259 | #ifdef CONFIG_IPV6_PRIVACY |
| 2260 | read_lock_bh(&in6_dev->lock); |
| 2261 | /* update all temporary addresses in the list */ |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 2262 | list_for_each_entry(ift, &in6_dev->tempaddr_list, |
| 2263 | tmp_list) { |
| 2264 | int age, max_valid, max_prefered; |
| 2265 | |
Benoit Boissinot | c6fbfac | 2008-04-02 00:00:58 -0700 | [diff] [blame] | 2266 | if (ifp != ift->ifpub) |
| 2267 | continue; |
| 2268 | |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 2269 | /* |
| 2270 | * RFC 4941 section 3.3: |
| 2271 | * If a received option will extend the lifetime |
| 2272 | * of a public address, the lifetimes of |
| 2273 | * temporary addresses should be extended, |
| 2274 | * subject to the overall constraint that no |
| 2275 | * temporary addresses should ever remain |
| 2276 | * "valid" or "preferred" for a time longer than |
| 2277 | * (TEMP_VALID_LIFETIME) or |
| 2278 | * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), |
| 2279 | * respectively. |
| 2280 | */ |
| 2281 | age = (now - ift->cstamp) / HZ; |
| 2282 | max_valid = in6_dev->cnf.temp_valid_lft - age; |
| 2283 | if (max_valid < 0) |
| 2284 | max_valid = 0; |
| 2285 | |
| 2286 | max_prefered = in6_dev->cnf.temp_prefered_lft - |
| 2287 | in6_dev->cnf.max_desync_factor - |
| 2288 | age; |
| 2289 | if (max_prefered < 0) |
| 2290 | max_prefered = 0; |
| 2291 | |
| 2292 | if (valid_lft > max_valid) |
| 2293 | valid_lft = max_valid; |
| 2294 | |
| 2295 | if (prefered_lft > max_prefered) |
| 2296 | prefered_lft = max_prefered; |
| 2297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | spin_lock(&ift->lock); |
| 2299 | flags = ift->flags; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 2300 | ift->valid_lft = valid_lft; |
| 2301 | ift->prefered_lft = prefered_lft; |
| 2302 | ift->tstamp = now; |
| 2303 | if (prefered_lft > 0) |
| 2304 | ift->flags &= ~IFA_F_DEPRECATED; |
| 2305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | spin_unlock(&ift->lock); |
| 2307 | if (!(flags&IFA_F_TENTATIVE)) |
| 2308 | ipv6_ifa_notify(0, ift); |
| 2309 | } |
| 2310 | |
Glenn Wurster | aed6550 | 2010-09-27 07:04:30 +0000 | [diff] [blame] | 2311 | if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | /* |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 2313 | * When a new public address is created as |
| 2314 | * described in [ADDRCONF], also create a new |
| 2315 | * temporary address. Also create a temporary |
| 2316 | * address if it's enabled but no temporary |
| 2317 | * address currently exists. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2319 | read_unlock_bh(&in6_dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | ipv6_create_tempaddr(ifp, NULL); |
| 2321 | } else { |
| 2322 | read_unlock_bh(&in6_dev->lock); |
| 2323 | } |
| 2324 | #endif |
| 2325 | in6_ifa_put(ifp); |
| 2326 | addrconf_verify(0); |
| 2327 | } |
| 2328 | } |
| 2329 | inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); |
| 2330 | in6_dev_put(in6_dev); |
| 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * Set destination address. |
| 2335 | * Special case for SIT interfaces where we create a new "virtual" |
| 2336 | * device. |
| 2337 | */ |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2338 | int addrconf_set_dstaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | { |
| 2340 | struct in6_ifreq ireq; |
| 2341 | struct net_device *dev; |
| 2342 | int err = -EINVAL; |
| 2343 | |
| 2344 | rtnl_lock(); |
| 2345 | |
| 2346 | err = -EFAULT; |
| 2347 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 2348 | goto err_exit; |
| 2349 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2350 | dev = __dev_get_by_index(net, ireq.ifr6_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | |
| 2352 | err = -ENODEV; |
| 2353 | if (dev == NULL) |
| 2354 | goto err_exit; |
| 2355 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2356 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | if (dev->type == ARPHRD_SIT) { |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 2358 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | struct ifreq ifr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | struct ip_tunnel_parm p; |
| 2361 | |
| 2362 | err = -EADDRNOTAVAIL; |
| 2363 | if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) |
| 2364 | goto err_exit; |
| 2365 | |
| 2366 | memset(&p, 0, sizeof(p)); |
| 2367 | p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; |
| 2368 | p.iph.saddr = 0; |
| 2369 | p.iph.version = 4; |
| 2370 | p.iph.ihl = 5; |
| 2371 | p.iph.protocol = IPPROTO_IPV6; |
| 2372 | p.iph.ttl = 64; |
Stephen Hemminger | d20b310 | 2008-01-21 00:48:43 -0800 | [diff] [blame] | 2373 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 2375 | if (ops->ndo_do_ioctl) { |
| 2376 | mm_segment_t oldfs = get_fs(); |
| 2377 | |
| 2378 | set_fs(KERNEL_DS); |
| 2379 | err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); |
| 2380 | set_fs(oldfs); |
| 2381 | } else |
| 2382 | err = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | |
| 2384 | if (err == 0) { |
| 2385 | err = -ENOBUFS; |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2386 | dev = __dev_get_by_name(net, p.name); |
| 2387 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | goto err_exit; |
| 2389 | err = dev_open(dev); |
| 2390 | } |
| 2391 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2392 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
| 2394 | err_exit: |
| 2395 | rtnl_unlock(); |
| 2396 | return err; |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | * Manual configuration of address on an interface |
| 2401 | */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2402 | static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx, |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2403 | unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2404 | __u32 valid_lft) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | { |
| 2406 | struct inet6_ifaddr *ifp; |
| 2407 | struct inet6_dev *idev; |
| 2408 | struct net_device *dev; |
| 2409 | int scope; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2410 | u32 flags; |
| 2411 | clock_t expires; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2412 | unsigned long timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | |
| 2414 | ASSERT_RTNL(); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2415 | |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2416 | if (plen > 128) |
| 2417 | return -EINVAL; |
| 2418 | |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2419 | /* check the lifetime */ |
| 2420 | if (!valid_lft || prefered_lft > valid_lft) |
| 2421 | return -EINVAL; |
| 2422 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2423 | dev = __dev_get_by_index(net, ifindex); |
| 2424 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | return -ENODEV; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2426 | |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2427 | idev = addrconf_add_dev(dev); |
| 2428 | if (IS_ERR(idev)) |
| 2429 | return PTR_ERR(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | |
| 2431 | scope = ipv6_addr_scope(pfx); |
| 2432 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2433 | timeout = addrconf_timeout_fixup(valid_lft, HZ); |
| 2434 | if (addrconf_finite_timeout(timeout)) { |
| 2435 | expires = jiffies_to_clock_t(timeout * HZ); |
| 2436 | valid_lft = timeout; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2437 | flags = RTF_EXPIRES; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2438 | } else { |
| 2439 | expires = 0; |
| 2440 | flags = 0; |
| 2441 | ifa_flags |= IFA_F_PERMANENT; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2442 | } |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2443 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2444 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); |
| 2445 | if (addrconf_finite_timeout(timeout)) { |
| 2446 | if (timeout == 0) |
| 2447 | ifa_flags |= IFA_F_DEPRECATED; |
| 2448 | prefered_lft = timeout; |
| 2449 | } |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2450 | |
| 2451 | ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); |
| 2452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | if (!IS_ERR(ifp)) { |
Herbert Xu | 06aebfb | 2006-08-09 16:52:04 -0700 | [diff] [blame] | 2454 | spin_lock_bh(&ifp->lock); |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2455 | ifp->valid_lft = valid_lft; |
| 2456 | ifp->prefered_lft = prefered_lft; |
| 2457 | ifp->tstamp = jiffies; |
Herbert Xu | 06aebfb | 2006-08-09 16:52:04 -0700 | [diff] [blame] | 2458 | spin_unlock_bh(&ifp->lock); |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2459 | |
YOSHIFUJI Hideaki | 46d4804 | 2007-02-07 20:36:26 +0900 | [diff] [blame] | 2460 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2461 | expires, flags); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2462 | /* |
| 2463 | * Note that section 3.1 of RFC 4429 indicates |
| 2464 | * that the Optimistic flag should not be set for |
| 2465 | * manually configured addresses |
| 2466 | */ |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 2467 | addrconf_dad_start(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | in6_ifa_put(ifp); |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2469 | addrconf_verify(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | return 0; |
| 2471 | } |
| 2472 | |
| 2473 | return PTR_ERR(ifp); |
| 2474 | } |
| 2475 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2476 | static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx, |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2477 | unsigned int plen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | { |
| 2479 | struct inet6_ifaddr *ifp; |
| 2480 | struct inet6_dev *idev; |
| 2481 | struct net_device *dev; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2482 | |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2483 | if (plen > 128) |
| 2484 | return -EINVAL; |
| 2485 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2486 | dev = __dev_get_by_index(net, ifindex); |
| 2487 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | return -ENODEV; |
| 2489 | |
| 2490 | if ((idev = __in6_dev_get(dev)) == NULL) |
| 2491 | return -ENXIO; |
| 2492 | |
| 2493 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 2494 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | if (ifp->prefix_len == plen && |
| 2496 | ipv6_addr_equal(pfx, &ifp->addr)) { |
| 2497 | in6_ifa_hold(ifp); |
| 2498 | read_unlock_bh(&idev->lock); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | ipv6_del_addr(ifp); |
| 2501 | |
| 2502 | /* If the last address is deleted administratively, |
| 2503 | disable IPv6 on this interface. |
| 2504 | */ |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 2505 | if (list_empty(&idev->addr_list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | addrconf_ifdown(idev->dev, 1); |
| 2507 | return 0; |
| 2508 | } |
| 2509 | } |
| 2510 | read_unlock_bh(&idev->lock); |
| 2511 | return -EADDRNOTAVAIL; |
| 2512 | } |
| 2513 | |
| 2514 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2515 | int addrconf_add_ifaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | { |
| 2517 | struct in6_ifreq ireq; |
| 2518 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2519 | |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 2520 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | return -EPERM; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 2524 | return -EFAULT; |
| 2525 | |
| 2526 | rtnl_lock(); |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2527 | err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, |
| 2528 | ireq.ifr6_prefixlen, IFA_F_PERMANENT, |
| 2529 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | rtnl_unlock(); |
| 2531 | return err; |
| 2532 | } |
| 2533 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2534 | int addrconf_del_ifaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | { |
| 2536 | struct in6_ifreq ireq; |
| 2537 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2538 | |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 2539 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | return -EPERM; |
| 2541 | |
| 2542 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 2543 | return -EFAULT; |
| 2544 | |
| 2545 | rtnl_lock(); |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2546 | err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, |
| 2547 | ireq.ifr6_prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | rtnl_unlock(); |
| 2549 | return err; |
| 2550 | } |
| 2551 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 2552 | static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, |
| 2553 | int plen, int scope) |
| 2554 | { |
| 2555 | struct inet6_ifaddr *ifp; |
| 2556 | |
| 2557 | ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); |
| 2558 | if (!IS_ERR(ifp)) { |
| 2559 | spin_lock_bh(&ifp->lock); |
| 2560 | ifp->flags &= ~IFA_F_TENTATIVE; |
| 2561 | spin_unlock_bh(&ifp->lock); |
| 2562 | ipv6_ifa_notify(RTM_NEWADDR, ifp); |
| 2563 | in6_ifa_put(ifp); |
| 2564 | } |
| 2565 | } |
| 2566 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2567 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | static void sit_add_v4_addrs(struct inet6_dev *idev) |
| 2569 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | struct in6_addr addr; |
| 2571 | struct net_device *dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2572 | struct net *net = dev_net(idev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | int scope; |
| 2574 | |
| 2575 | ASSERT_RTNL(); |
| 2576 | |
| 2577 | memset(&addr, 0, sizeof(struct in6_addr)); |
| 2578 | memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); |
| 2579 | |
| 2580 | if (idev->dev->flags&IFF_POINTOPOINT) { |
| 2581 | addr.s6_addr32[0] = htonl(0xfe800000); |
| 2582 | scope = IFA_LINK; |
| 2583 | } else { |
| 2584 | scope = IPV6_ADDR_COMPATv4; |
| 2585 | } |
| 2586 | |
| 2587 | if (addr.s6_addr32[3]) { |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 2588 | add_addr(idev, &addr, 128, scope); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | return; |
| 2590 | } |
| 2591 | |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 2592 | for_each_netdev(net, dev) { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2593 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | if (in_dev && (dev->flags & IFF_UP)) { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2595 | struct in_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | |
| 2597 | int flag = scope; |
| 2598 | |
| 2599 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 2600 | int plen; |
| 2601 | |
| 2602 | addr.s6_addr32[3] = ifa->ifa_local; |
| 2603 | |
| 2604 | if (ifa->ifa_scope == RT_SCOPE_LINK) |
| 2605 | continue; |
| 2606 | if (ifa->ifa_scope >= RT_SCOPE_HOST) { |
| 2607 | if (idev->dev->flags&IFF_POINTOPOINT) |
| 2608 | continue; |
| 2609 | flag |= IFA_HOST; |
| 2610 | } |
| 2611 | if (idev->dev->flags&IFF_POINTOPOINT) |
| 2612 | plen = 64; |
| 2613 | else |
| 2614 | plen = 96; |
| 2615 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 2616 | add_addr(idev, &addr, plen, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | } |
| 2618 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2621 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
| 2623 | static void init_loopback(struct net_device *dev) |
| 2624 | { |
| 2625 | struct inet6_dev *idev; |
Balakumaran Kannan | 25fb6ca4 | 2013-04-02 16:15:05 +0530 | [diff] [blame] | 2626 | struct net_device *sp_dev; |
| 2627 | struct inet6_ifaddr *sp_ifa; |
| 2628 | struct rt6_info *sp_rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
| 2630 | /* ::1 */ |
| 2631 | |
| 2632 | ASSERT_RTNL(); |
| 2633 | |
| 2634 | if ((idev = ipv6_find_idev(dev)) == NULL) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 2635 | pr_debug("%s: add_dev failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | return; |
| 2637 | } |
| 2638 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 2639 | add_addr(idev, &in6addr_loopback, 128, IFA_HOST); |
Balakumaran Kannan | 25fb6ca4 | 2013-04-02 16:15:05 +0530 | [diff] [blame] | 2640 | |
| 2641 | /* Add routes to other interface's IPv6 addresses */ |
| 2642 | for_each_netdev(dev_net(dev), sp_dev) { |
| 2643 | if (!strcmp(sp_dev->name, dev->name)) |
| 2644 | continue; |
| 2645 | |
| 2646 | idev = __in6_dev_get(sp_dev); |
| 2647 | if (!idev) |
| 2648 | continue; |
| 2649 | |
| 2650 | read_lock_bh(&idev->lock); |
| 2651 | list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { |
| 2652 | |
| 2653 | if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) |
| 2654 | continue; |
| 2655 | |
| 2656 | sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0); |
| 2657 | |
| 2658 | /* Failure cases are ignored */ |
| 2659 | if (!IS_ERR(sp_rt)) |
| 2660 | ip6_ins_rt(sp_rt); |
| 2661 | } |
| 2662 | read_unlock_bh(&idev->lock); |
| 2663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2666 | static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2668 | struct inet6_ifaddr *ifp; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2669 | u32 addr_flags = IFA_F_PERMANENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2671 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 2672 | if (idev->cnf.optimistic_dad && |
David Miller | 702beb8 | 2008-07-20 18:17:02 -0700 | [diff] [blame] | 2673 | !dev_net(idev->dev)->ipv6.devconf_all->forwarding) |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2674 | addr_flags |= IFA_F_OPTIMISTIC; |
| 2675 | #endif |
| 2676 | |
| 2677 | |
| 2678 | ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | if (!IS_ERR(ifp)) { |
YOSHIFUJI Hideaki | 46d4804 | 2007-02-07 20:36:26 +0900 | [diff] [blame] | 2680 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 2681 | addrconf_dad_start(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | in6_ifa_put(ifp); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | static void addrconf_dev_config(struct net_device *dev) |
| 2687 | { |
| 2688 | struct in6_addr addr; |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2689 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
| 2691 | ASSERT_RTNL(); |
| 2692 | |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 2693 | if ((dev->type != ARPHRD_ETHER) && |
| 2694 | (dev->type != ARPHRD_FDDI) && |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 2695 | (dev->type != ARPHRD_ARCNET) && |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 2696 | (dev->type != ARPHRD_INFINIBAND) && |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 2697 | (dev->type != ARPHRD_IEEE802154) && |
| 2698 | (dev->type != ARPHRD_IEEE1394)) { |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 2699 | /* Alas, we support only Ethernet autoconfiguration. */ |
| 2700 | return; |
| 2701 | } |
| 2702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | idev = addrconf_add_dev(dev); |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2704 | if (IS_ERR(idev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | return; |
| 2706 | |
| 2707 | memset(&addr, 0, sizeof(struct in6_addr)); |
| 2708 | addr.s6_addr32[0] = htonl(0xFE800000); |
| 2709 | |
| 2710 | if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) |
| 2711 | addrconf_add_linklocal(idev, &addr); |
| 2712 | } |
| 2713 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2714 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | static void addrconf_sit_config(struct net_device *dev) |
| 2716 | { |
| 2717 | struct inet6_dev *idev; |
| 2718 | |
| 2719 | ASSERT_RTNL(); |
| 2720 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2721 | /* |
| 2722 | * Configure the tunnel with one of our IPv4 |
| 2723 | * addresses... we should configure all of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | * our v4 addrs in the tunnel |
| 2725 | */ |
| 2726 | |
| 2727 | if ((idev = ipv6_find_idev(dev)) == NULL) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 2728 | pr_debug("%s: add_dev failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | return; |
| 2730 | } |
| 2731 | |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 2732 | if (dev->priv_flags & IFF_ISATAP) { |
| 2733 | struct in6_addr addr; |
| 2734 | |
| 2735 | ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); |
| 2736 | addrconf_prefix_route(&addr, 64, dev, 0, 0); |
| 2737 | if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) |
| 2738 | addrconf_add_linklocal(idev, &addr); |
| 2739 | return; |
| 2740 | } |
| 2741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | sit_add_v4_addrs(idev); |
| 2743 | |
Nicolas Dichtel | 62b54dd | 2012-10-01 23:19:14 +0000 | [diff] [blame] | 2744 | if (dev->flags&IFF_POINTOPOINT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | addrconf_add_mroute(dev); |
Nicolas Dichtel | 62b54dd | 2012-10-01 23:19:14 +0000 | [diff] [blame] | 2746 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | sit_route_add(dev); |
| 2748 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2749 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2751 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2752 | static void addrconf_gre_config(struct net_device *dev) |
| 2753 | { |
| 2754 | struct inet6_dev *idev; |
| 2755 | struct in6_addr addr; |
| 2756 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2757 | pr_info("%s(%s)\n", __func__, dev->name); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2758 | |
| 2759 | ASSERT_RTNL(); |
| 2760 | |
| 2761 | if ((idev = ipv6_find_idev(dev)) == NULL) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 2762 | pr_debug("%s: add_dev failed\n", __func__); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2763 | return; |
| 2764 | } |
| 2765 | |
| 2766 | ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); |
| 2767 | addrconf_prefix_route(&addr, 64, dev, 0, 0); |
| 2768 | |
| 2769 | if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) |
| 2770 | addrconf_add_linklocal(idev, &addr); |
| 2771 | } |
| 2772 | #endif |
| 2773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | static inline int |
| 2775 | ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) |
| 2776 | { |
| 2777 | struct in6_addr lladdr; |
| 2778 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2779 | if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | addrconf_add_linklocal(idev, &lladdr); |
| 2781 | return 0; |
| 2782 | } |
| 2783 | return -1; |
| 2784 | } |
| 2785 | |
| 2786 | static void ip6_tnl_add_linklocal(struct inet6_dev *idev) |
| 2787 | { |
| 2788 | struct net_device *link_dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2789 | struct net *net = dev_net(idev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
| 2791 | /* first try to inherit the link-local address from the link device */ |
| 2792 | if (idev->dev->iflink && |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 2793 | (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | if (!ipv6_inherit_linklocal(idev, link_dev)) |
| 2795 | return; |
| 2796 | } |
| 2797 | /* then try to inherit it from any device */ |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 2798 | for_each_netdev(net, link_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | if (!ipv6_inherit_linklocal(idev, link_dev)) |
| 2800 | return; |
| 2801 | } |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 2802 | pr_debug("init ip6-ip6: add_linklocal failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * Autoconfigure tunnel with a link-local address so routing protocols, |
| 2807 | * DHCPv6, MLD etc. can be run over the virtual link |
| 2808 | */ |
| 2809 | |
| 2810 | static void addrconf_ip6_tnl_config(struct net_device *dev) |
| 2811 | { |
| 2812 | struct inet6_dev *idev; |
| 2813 | |
| 2814 | ASSERT_RTNL(); |
| 2815 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 2816 | idev = addrconf_add_dev(dev); |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2817 | if (IS_ERR(idev)) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 2818 | pr_debug("init ip6-ip6: add_dev failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | return; |
| 2820 | } |
| 2821 | ip6_tnl_add_linklocal(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2824 | static int addrconf_notify(struct notifier_block *this, unsigned long event, |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2825 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | { |
| 2827 | struct net_device *dev = (struct net_device *) data; |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 2828 | struct inet6_dev *idev = __in6_dev_get(dev); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 2829 | int run_pending = 0; |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 2830 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 2832 | switch (event) { |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 2833 | case NETDEV_REGISTER: |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 2834 | if (!idev && dev->mtu >= IPV6_MIN_MTU) { |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 2835 | idev = ipv6_add_dev(dev); |
| 2836 | if (!idev) |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 2837 | return notifier_from_errno(-ENOMEM); |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 2838 | } |
| 2839 | break; |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | case NETDEV_UP: |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2842 | case NETDEV_CHANGE: |
Jay Vosburgh | c2edacf | 2007-07-09 10:42:47 -0700 | [diff] [blame] | 2843 | if (dev->flags & IFF_SLAVE) |
| 2844 | break; |
| 2845 | |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2846 | if (event == NETDEV_UP) { |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 2847 | if (!addrconf_qdisc_ok(dev)) { |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2848 | /* device is not ready yet. */ |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2849 | pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2850 | dev->name); |
| 2851 | break; |
| 2852 | } |
Kristian Slavov | 9908104 | 2006-02-08 16:10:53 -0800 | [diff] [blame] | 2853 | |
Evgeniy Polyakov | d31c7b8 | 2007-11-30 23:36:08 +1100 | [diff] [blame] | 2854 | if (!idev && dev->mtu >= IPV6_MIN_MTU) |
| 2855 | idev = ipv6_add_dev(dev); |
| 2856 | |
Benjamin Thery | e3ec6cf | 2008-11-05 01:43:57 -0800 | [diff] [blame] | 2857 | if (idev) { |
Kristian Slavov | 9908104 | 2006-02-08 16:10:53 -0800 | [diff] [blame] | 2858 | idev->if_flags |= IF_READY; |
Benjamin Thery | e3ec6cf | 2008-11-05 01:43:57 -0800 | [diff] [blame] | 2859 | run_pending = 1; |
| 2860 | } |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2861 | } else { |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 2862 | if (!addrconf_qdisc_ok(dev)) { |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2863 | /* device is still not ready. */ |
| 2864 | break; |
| 2865 | } |
| 2866 | |
| 2867 | if (idev) { |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2868 | if (idev->if_flags & IF_READY) |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2869 | /* device is already configured. */ |
| 2870 | break; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2871 | idev->if_flags |= IF_READY; |
| 2872 | } |
| 2873 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2874 | pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", |
| 2875 | dev->name); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2876 | |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 2877 | run_pending = 1; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2878 | } |
| 2879 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 2880 | switch (dev->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2881 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | case ARPHRD_SIT: |
| 2883 | addrconf_sit_config(dev); |
| 2884 | break; |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2885 | #endif |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2886 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2887 | case ARPHRD_IPGRE: |
| 2888 | addrconf_gre_config(dev); |
| 2889 | break; |
| 2890 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | case ARPHRD_TUNNEL6: |
| 2892 | addrconf_ip6_tnl_config(dev); |
| 2893 | break; |
| 2894 | case ARPHRD_LOOPBACK: |
| 2895 | init_loopback(dev); |
| 2896 | break; |
| 2897 | |
| 2898 | default: |
| 2899 | addrconf_dev_config(dev); |
| 2900 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 2901 | } |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | if (idev) { |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 2904 | if (run_pending) |
| 2905 | addrconf_dad_run(idev); |
| 2906 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2907 | /* |
| 2908 | * If the MTU changed during the interface down, |
| 2909 | * when the interface up, the changed MTU must be |
| 2910 | * reflected in the idev as well as routers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | */ |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2912 | if (idev->cnf.mtu6 != dev->mtu && |
| 2913 | dev->mtu >= IPV6_MIN_MTU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | rt6_mtu_change(dev, dev->mtu); |
| 2915 | idev->cnf.mtu6 = dev->mtu; |
| 2916 | } |
| 2917 | idev->tstamp = jiffies; |
| 2918 | inet6_ifinfo_notify(RTM_NEWLINK, idev); |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2919 | |
| 2920 | /* |
| 2921 | * If the changed mtu during down is lower than |
| 2922 | * IPV6_MIN_MTU stop IPv6 on this interface. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | */ |
| 2924 | if (dev->mtu < IPV6_MIN_MTU) |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2925 | addrconf_ifdown(dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | break; |
| 2928 | |
| 2929 | case NETDEV_CHANGEMTU: |
Evgeniy Polyakov | d31c7b8 | 2007-11-30 23:36:08 +1100 | [diff] [blame] | 2930 | if (idev && dev->mtu >= IPV6_MIN_MTU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | rt6_mtu_change(dev, dev->mtu); |
| 2932 | idev->cnf.mtu6 = dev->mtu; |
| 2933 | break; |
| 2934 | } |
| 2935 | |
Evgeniy Polyakov | d31c7b8 | 2007-11-30 23:36:08 +1100 | [diff] [blame] | 2936 | if (!idev && dev->mtu >= IPV6_MIN_MTU) { |
| 2937 | idev = ipv6_add_dev(dev); |
| 2938 | if (idev) |
| 2939 | break; |
| 2940 | } |
| 2941 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2942 | /* |
| 2943 | * MTU falled under IPV6_MIN_MTU. |
| 2944 | * Stop IPv6 on this interface. |
| 2945 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | |
| 2947 | case NETDEV_DOWN: |
| 2948 | case NETDEV_UNREGISTER: |
| 2949 | /* |
| 2950 | * Remove all addresses from this interface. |
| 2951 | */ |
| 2952 | addrconf_ifdown(dev, event != NETDEV_DOWN); |
| 2953 | break; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | case NETDEV_CHANGENAME: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | if (idev) { |
Stephen Hemminger | 5632c51 | 2007-04-28 21:16:39 -0700 | [diff] [blame] | 2957 | snmp6_unregister_dev(idev); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 2958 | addrconf_sysctl_unregister(idev); |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 2959 | addrconf_sysctl_register(idev); |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 2960 | err = snmp6_register_dev(idev); |
| 2961 | if (err) |
| 2962 | return notifier_from_errno(err); |
Stephen Hemminger | 5632c51 | 2007-04-28 21:16:39 -0700 | [diff] [blame] | 2963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | break; |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 2965 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 2966 | case NETDEV_PRE_TYPE_CHANGE: |
| 2967 | case NETDEV_POST_TYPE_CHANGE: |
| 2968 | addrconf_type_change(dev, event); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 2969 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 2970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | |
| 2972 | return NOTIFY_OK; |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * addrconf module should be notified of a device going up |
| 2977 | */ |
| 2978 | static struct notifier_block ipv6_dev_notf = { |
| 2979 | .notifier_call = addrconf_notify, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | }; |
| 2981 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 2982 | static void addrconf_type_change(struct net_device *dev, unsigned long event) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 2983 | { |
| 2984 | struct inet6_dev *idev; |
| 2985 | ASSERT_RTNL(); |
| 2986 | |
| 2987 | idev = __in6_dev_get(dev); |
| 2988 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 2989 | if (event == NETDEV_POST_TYPE_CHANGE) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 2990 | ipv6_mc_remap(idev); |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 2991 | else if (event == NETDEV_PRE_TYPE_CHANGE) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 2992 | ipv6_mc_unmap(idev); |
| 2993 | } |
| 2994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | static int addrconf_ifdown(struct net_device *dev, int how) |
| 2996 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2997 | struct net *net = dev_net(dev); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 2998 | struct inet6_dev *idev; |
| 2999 | struct inet6_ifaddr *ifa; |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3000 | int state, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
| 3002 | ASSERT_RTNL(); |
| 3003 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3004 | rt6_ifdown(net, dev); |
| 3005 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
| 3007 | idev = __in6_dev_get(dev); |
| 3008 | if (idev == NULL) |
| 3009 | return -ENODEV; |
| 3010 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3011 | /* |
| 3012 | * Step 1: remove reference to ipv6 device from parent device. |
| 3013 | * Do not dev_put! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3015 | if (how) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | idev->dead = 1; |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 3017 | |
| 3018 | /* protected by rtnl_lock */ |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 3019 | RCU_INIT_POINTER(dev->ip6_ptr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | |
| 3021 | /* Step 1.5: remove snmp6 entry */ |
| 3022 | snmp6_unregister_dev(idev); |
| 3023 | |
| 3024 | } |
| 3025 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3026 | /* Step 2: clear hash table */ |
| 3027 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { |
| 3028 | struct hlist_head *h = &inet6_addr_lst[i]; |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3029 | |
| 3030 | spin_lock_bh(&addrconf_hash_lock); |
| 3031 | restart: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3032 | hlist_for_each_entry_rcu(ifa, h, addr_lst) { |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3033 | if (ifa->idev == idev) { |
| 3034 | hlist_del_init_rcu(&ifa->addr_lst); |
| 3035 | addrconf_del_timer(ifa); |
| 3036 | goto restart; |
| 3037 | } |
| 3038 | } |
| 3039 | spin_unlock_bh(&addrconf_hash_lock); |
| 3040 | } |
| 3041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | write_lock_bh(&idev->lock); |
| 3043 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3044 | /* Step 2: clear flags for stateless addrconf */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3045 | if (!how) |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3046 | idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | #ifdef CONFIG_IPV6_PRIVACY |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3049 | if (how && del_timer(&idev->regen_timer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | in6_dev_put(idev); |
| 3051 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3052 | /* Step 3: clear tempaddr list */ |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 3053 | while (!list_empty(&idev->tempaddr_list)) { |
| 3054 | ifa = list_first_entry(&idev->tempaddr_list, |
| 3055 | struct inet6_ifaddr, tmp_list); |
| 3056 | list_del(&ifa->tmp_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | write_unlock_bh(&idev->lock); |
| 3058 | spin_lock_bh(&ifa->lock); |
| 3059 | |
| 3060 | if (ifa->ifpub) { |
| 3061 | in6_ifa_put(ifa->ifpub); |
| 3062 | ifa->ifpub = NULL; |
| 3063 | } |
| 3064 | spin_unlock_bh(&ifa->lock); |
| 3065 | in6_ifa_put(ifa); |
| 3066 | write_lock_bh(&idev->lock); |
| 3067 | } |
| 3068 | #endif |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3069 | |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3070 | while (!list_empty(&idev->addr_list)) { |
| 3071 | ifa = list_first_entry(&idev->addr_list, |
| 3072 | struct inet6_ifaddr, if_list); |
stephen hemminger | 84e8b80 | 2010-03-02 13:32:46 +0000 | [diff] [blame] | 3073 | addrconf_del_timer(ifa); |
| 3074 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3075 | list_del(&ifa->if_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3077 | write_unlock_bh(&idev->lock); |
stephen hemminger | 84e8b80 | 2010-03-02 13:32:46 +0000 | [diff] [blame] | 3078 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3079 | spin_lock_bh(&ifa->state_lock); |
| 3080 | state = ifa->state; |
| 3081 | ifa->state = INET6_IFADDR_STATE_DEAD; |
| 3082 | spin_unlock_bh(&ifa->state_lock); |
stephen hemminger | 84e8b80 | 2010-03-02 13:32:46 +0000 | [diff] [blame] | 3083 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3084 | if (state != INET6_IFADDR_STATE_DEAD) { |
| 3085 | __ipv6_ifa_notify(RTM_DELADDR, ifa); |
| 3086 | atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); |
stephen hemminger | 27bdb2a | 2010-04-12 05:41:32 +0000 | [diff] [blame] | 3087 | } |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3088 | in6_ifa_put(ifa); |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3089 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3090 | write_lock_bh(&idev->lock); |
| 3091 | } |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | write_unlock_bh(&idev->lock); |
| 3094 | |
| 3095 | /* Step 5: Discard multicast list */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3096 | if (how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | ipv6_mc_destroy_dev(idev); |
| 3098 | else |
| 3099 | ipv6_mc_down(idev); |
| 3100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | idev->tstamp = jiffies; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3102 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3103 | /* Last: Shot the device (if unregistered) */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3104 | if (how) { |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 3105 | addrconf_sysctl_unregister(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | neigh_parms_release(&nd_tbl, idev->nd_parms); |
| 3107 | neigh_ifdown(&nd_tbl, dev); |
| 3108 | in6_dev_put(idev); |
| 3109 | } |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
| 3113 | static void addrconf_rs_timer(unsigned long data) |
| 3114 | { |
| 3115 | struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3116 | struct inet6_dev *idev = ifp->idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3118 | read_lock(&idev->lock); |
| 3119 | if (idev->dead || !(idev->if_flags & IF_READY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | goto out; |
| 3121 | |
Shmulik Ladkani | 9ba2add | 2012-12-02 01:44:53 +0000 | [diff] [blame] | 3122 | if (!ipv6_accept_ra(idev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | goto out; |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3124 | |
| 3125 | /* Announcement received after solicitation was sent */ |
| 3126 | if (idev->if_flags & IF_RA_RCVD) |
| 3127 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
| 3129 | spin_lock(&ifp->lock); |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3130 | if (ifp->probes++ < idev->cnf.rtr_solicits) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | /* The wait after the last probe can be shorter */ |
| 3132 | addrconf_mod_timer(ifp, AC_RS, |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3133 | (ifp->probes == idev->cnf.rtr_solicits) ? |
| 3134 | idev->cnf.rtr_solicit_delay : |
| 3135 | idev->cnf.rtr_solicit_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | spin_unlock(&ifp->lock); |
| 3137 | |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3138 | ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | } else { |
| 3140 | spin_unlock(&ifp->lock); |
| 3141 | /* |
| 3142 | * Note: we do not support deprecated "all on-link" |
| 3143 | * assumption any longer. |
| 3144 | */ |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 3145 | pr_debug("%s: no IPv6 routers present\n", idev->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | out: |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3149 | read_unlock(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | in6_ifa_put(ifp); |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * Duplicate Address Detection |
| 3155 | */ |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3156 | static void addrconf_dad_kick(struct inet6_ifaddr *ifp) |
| 3157 | { |
| 3158 | unsigned long rand_num; |
| 3159 | struct inet6_dev *idev = ifp->idev; |
| 3160 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3161 | if (ifp->flags & IFA_F_OPTIMISTIC) |
| 3162 | rand_num = 0; |
| 3163 | else |
| 3164 | rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); |
| 3165 | |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3166 | ifp->probes = idev->cnf.dad_transmits; |
| 3167 | addrconf_mod_timer(ifp, AC_DAD, rand_num); |
| 3168 | } |
| 3169 | |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 3170 | static void addrconf_dad_start(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | { |
| 3172 | struct inet6_dev *idev = ifp->idev; |
| 3173 | struct net_device *dev = idev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
| 3175 | addrconf_join_solict(dev, &ifp->addr); |
| 3176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | net_srandom(ifp->addr.s6_addr32[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | |
| 3179 | read_lock_bh(&idev->lock); |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 3180 | spin_lock(&ifp->lock); |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 3181 | if (ifp->state == INET6_IFADDR_STATE_DEAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | |
| 3184 | if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 3185 | idev->cnf.accept_dad < 1 || |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3186 | !(ifp->flags&IFA_F_TENTATIVE) || |
| 3187 | ifp->flags & IFA_F_NODAD) { |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 3188 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3189 | spin_unlock(&ifp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | read_unlock_bh(&idev->lock); |
| 3191 | |
| 3192 | addrconf_dad_completed(ifp); |
| 3193 | return; |
| 3194 | } |
| 3195 | |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3196 | if (!(idev->if_flags & IF_READY)) { |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3197 | spin_unlock(&ifp->lock); |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3198 | read_unlock_bh(&idev->lock); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3199 | /* |
Masatake YAMATO | 590a988 | 2009-06-09 10:41:12 +0900 | [diff] [blame] | 3200 | * If the device is not ready: |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3201 | * - keep it tentative if it is a permanent address. |
| 3202 | * - otherwise, kill it. |
| 3203 | */ |
| 3204 | in6_ifa_hold(ifp); |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 3205 | addrconf_dad_stop(ifp, 0); |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3206 | return; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3207 | } |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3208 | |
| 3209 | /* |
| 3210 | * Optimistic nodes can start receiving |
| 3211 | * Frames right away |
| 3212 | */ |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3213 | if (ifp->flags & IFA_F_OPTIMISTIC) |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3214 | ip6_ins_rt(ifp->rt); |
| 3215 | |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3216 | addrconf_dad_kick(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | out: |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 3218 | spin_unlock(&ifp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | read_unlock_bh(&idev->lock); |
| 3220 | } |
| 3221 | |
| 3222 | static void addrconf_dad_timer(unsigned long data) |
| 3223 | { |
| 3224 | struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; |
| 3225 | struct inet6_dev *idev = ifp->idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | struct in6_addr mcaddr; |
| 3227 | |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 3228 | if (!ifp->probes && addrconf_dad_end(ifp)) |
| 3229 | goto out; |
| 3230 | |
stephen hemminger | 122e451 | 2010-03-02 13:32:44 +0000 | [diff] [blame] | 3231 | read_lock(&idev->lock); |
| 3232 | if (idev->dead || !(idev->if_flags & IF_READY)) { |
| 3233 | read_unlock(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | goto out; |
| 3235 | } |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3236 | |
| 3237 | spin_lock(&ifp->lock); |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 3238 | if (ifp->state == INET6_IFADDR_STATE_DEAD) { |
| 3239 | spin_unlock(&ifp->lock); |
| 3240 | read_unlock(&idev->lock); |
| 3241 | goto out; |
| 3242 | } |
| 3243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | if (ifp->probes == 0) { |
| 3245 | /* |
| 3246 | * DAD was successful |
| 3247 | */ |
| 3248 | |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 3249 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3250 | spin_unlock(&ifp->lock); |
stephen hemminger | 122e451 | 2010-03-02 13:32:44 +0000 | [diff] [blame] | 3251 | read_unlock(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | |
| 3253 | addrconf_dad_completed(ifp); |
| 3254 | |
| 3255 | goto out; |
| 3256 | } |
| 3257 | |
| 3258 | ifp->probes--; |
| 3259 | addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3260 | spin_unlock(&ifp->lock); |
stephen hemminger | 122e451 | 2010-03-02 13:32:44 +0000 | [diff] [blame] | 3261 | read_unlock(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | |
| 3263 | /* send a neighbour solicitation for our addr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | addrconf_addr_solict_mult(&ifp->addr, &mcaddr); |
YOSHIFUJI Hideaki | d7aabf2 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 3265 | ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | out: |
| 3267 | in6_ifa_put(ifp); |
| 3268 | } |
| 3269 | |
| 3270 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp) |
| 3271 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3272 | struct net_device *dev = ifp->idev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
| 3274 | /* |
| 3275 | * Configure the address for reception. Now it is valid. |
| 3276 | */ |
| 3277 | |
| 3278 | ipv6_ifa_notify(RTM_NEWADDR, ifp); |
| 3279 | |
Tore Anderson | 026359b | 2011-08-28 23:47:33 +0000 | [diff] [blame] | 3280 | /* If added prefix is link local and we are prepared to process |
| 3281 | router advertisements, start sending router solicitations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | */ |
| 3283 | |
Shmulik Ladkani | aeaf6e9 | 2012-11-30 10:25:59 +0000 | [diff] [blame] | 3284 | if (ipv6_accept_ra(ifp->idev) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | ifp->idev->cnf.rtr_solicits > 0 && |
| 3286 | (dev->flags&IFF_LOOPBACK) == 0 && |
| 3287 | (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | /* |
| 3289 | * If a host as already performed a random delay |
| 3290 | * [...] as part of DAD [...] there is no need |
| 3291 | * to delay again before sending the first RS |
| 3292 | */ |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 3293 | ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
| 3295 | spin_lock_bh(&ifp->lock); |
| 3296 | ifp->probes = 1; |
| 3297 | ifp->idev->if_flags |= IF_RS_SENT; |
| 3298 | addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); |
| 3299 | spin_unlock_bh(&ifp->lock); |
| 3300 | } |
| 3301 | } |
| 3302 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3303 | static void addrconf_dad_run(struct inet6_dev *idev) |
| 3304 | { |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3305 | struct inet6_ifaddr *ifp; |
| 3306 | |
| 3307 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3308 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3309 | spin_lock(&ifp->lock); |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 3310 | if (ifp->flags & IFA_F_TENTATIVE && |
| 3311 | ifp->state == INET6_IFADDR_STATE_DAD) |
| 3312 | addrconf_dad_kick(ifp); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3313 | spin_unlock(&ifp->lock); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3314 | } |
| 3315 | read_unlock_bh(&idev->lock); |
| 3316 | } |
| 3317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | #ifdef CONFIG_PROC_FS |
| 3319 | struct if6_iter_state { |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3320 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | int bucket; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3322 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | }; |
| 3324 | |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3325 | static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | { |
| 3327 | struct inet6_ifaddr *ifa = NULL; |
| 3328 | struct if6_iter_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3329 | struct net *net = seq_file_net(seq); |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3330 | int p = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3332 | /* initial bucket if pos is 0 */ |
| 3333 | if (pos == 0) { |
| 3334 | state->bucket = 0; |
| 3335 | state->offset = 0; |
| 3336 | } |
| 3337 | |
| 3338 | for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3339 | hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3340 | addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3341 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 3342 | continue; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3343 | /* sync with offset */ |
| 3344 | if (p < state->offset) { |
| 3345 | p++; |
| 3346 | continue; |
| 3347 | } |
| 3348 | state->offset++; |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3349 | return ifa; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* prepare for next bucket */ |
| 3353 | state->offset = 0; |
| 3354 | p = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | } |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3356 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3359 | static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, |
| 3360 | struct inet6_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | { |
| 3362 | struct if6_iter_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3363 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3365 | hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3366 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 3367 | continue; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3368 | state->offset++; |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3369 | return ifa; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3370 | } |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3371 | |
| 3372 | while (++state->bucket < IN6_ADDR_HSIZE) { |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3373 | state->offset = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3374 | hlist_for_each_entry_rcu_bh(ifa, |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3375 | &inet6_addr_lst[state->bucket], addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3376 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 3377 | continue; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3378 | state->offset++; |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 3379 | return ifa; |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3383 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | static void *if6_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 4f70ecc | 2010-05-03 10:50:14 +0000 | [diff] [blame] | 3387 | __acquires(rcu_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | { |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3389 | rcu_read_lock_bh(); |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 3390 | return if6_get_first(seq, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 3394 | { |
| 3395 | struct inet6_ifaddr *ifa; |
| 3396 | |
| 3397 | ifa = if6_get_next(seq, v); |
| 3398 | ++*pos; |
| 3399 | return ifa; |
| 3400 | } |
| 3401 | |
| 3402 | static void if6_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 4f70ecc | 2010-05-03 10:50:14 +0000 | [diff] [blame] | 3403 | __releases(rcu_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | { |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3405 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | static int if6_seq_show(struct seq_file *seq, void *v) |
| 3409 | { |
| 3410 | struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 3411 | seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 3412 | &ifp->addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | ifp->idev->dev->ifindex, |
| 3414 | ifp->prefix_len, |
| 3415 | ifp->scope, |
| 3416 | ifp->flags, |
| 3417 | ifp->idev->dev->name); |
| 3418 | return 0; |
| 3419 | } |
| 3420 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 3421 | static const struct seq_operations if6_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | .start = if6_seq_start, |
| 3423 | .next = if6_seq_next, |
| 3424 | .show = if6_seq_show, |
| 3425 | .stop = if6_seq_stop, |
| 3426 | }; |
| 3427 | |
| 3428 | static int if6_seq_open(struct inode *inode, struct file *file) |
| 3429 | { |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3430 | return seq_open_net(inode, file, &if6_seq_ops, |
| 3431 | sizeof(struct if6_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 3434 | static const struct file_operations if6_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | .owner = THIS_MODULE, |
| 3436 | .open = if6_seq_open, |
| 3437 | .read = seq_read, |
| 3438 | .llseek = seq_lseek, |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3439 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | }; |
| 3441 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3442 | static int __net_init if6_proc_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | { |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 3444 | if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | return -ENOMEM; |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3449 | static void __net_exit if6_proc_net_exit(struct net *net) |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3450 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 3451 | remove_proc_entry("if_inet6", net->proc_net); |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | static struct pernet_operations if6_proc_net_ops = { |
| 3455 | .init = if6_proc_net_init, |
| 3456 | .exit = if6_proc_net_exit, |
| 3457 | }; |
| 3458 | |
| 3459 | int __init if6_proc_init(void) |
| 3460 | { |
| 3461 | return register_pernet_subsys(&if6_proc_net_ops); |
| 3462 | } |
| 3463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | void if6_proc_exit(void) |
| 3465 | { |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 3466 | unregister_pernet_subsys(&if6_proc_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | } |
| 3468 | #endif /* CONFIG_PROC_FS */ |
| 3469 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3470 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3471 | /* Check if address is a home address configured on any interface. */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3472 | int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3473 | { |
| 3474 | int ret = 0; |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3475 | struct inet6_ifaddr *ifp = NULL; |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 3476 | unsigned int hash = inet6_addr_hash(addr); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 3477 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3478 | rcu_read_lock_bh(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3479 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 3480 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | 389f661 | 2008-01-10 22:44:40 -0800 | [diff] [blame] | 3481 | continue; |
YOSHIFUJI Hideaki | caad295 | 2008-04-10 15:42:07 +0900 | [diff] [blame] | 3482 | if (ipv6_addr_equal(&ifp->addr, addr) && |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3483 | (ifp->flags & IFA_F_HOMEADDRESS)) { |
| 3484 | ret = 1; |
| 3485 | break; |
| 3486 | } |
| 3487 | } |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3488 | rcu_read_unlock_bh(); |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3489 | return ret; |
| 3490 | } |
| 3491 | #endif |
| 3492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | /* |
| 3494 | * Periodic address status verification |
| 3495 | */ |
| 3496 | |
| 3497 | static void addrconf_verify(unsigned long foo) |
| 3498 | { |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 3499 | unsigned long now, next, next_sec, next_sched; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | int i; |
| 3502 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3503 | rcu_read_lock_bh(); |
| 3504 | spin_lock(&addrconf_verify_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | now = jiffies; |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 3506 | next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | |
| 3508 | del_timer(&addr_chk_timer); |
| 3509 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3510 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | restart: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3512 | hlist_for_each_entry_rcu_bh(ifp, |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3513 | &inet6_addr_lst[i], addr_lst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | unsigned long age; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | |
| 3516 | if (ifp->flags & IFA_F_PERMANENT) |
| 3517 | continue; |
| 3518 | |
| 3519 | spin_lock(&ifp->lock); |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 3520 | /* We try to batch several events at once. */ |
| 3521 | age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | |
YOSHIFUJI Hideaki | 8f27ebb | 2006-07-28 18:12:11 +0900 | [diff] [blame] | 3523 | if (ifp->valid_lft != INFINITY_LIFE_TIME && |
| 3524 | age >= ifp->valid_lft) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | spin_unlock(&ifp->lock); |
| 3526 | in6_ifa_hold(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | ipv6_del_addr(ifp); |
| 3528 | goto restart; |
YOSHIFUJI Hideaki | 8f27ebb | 2006-07-28 18:12:11 +0900 | [diff] [blame] | 3529 | } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { |
| 3530 | spin_unlock(&ifp->lock); |
| 3531 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | } else if (age >= ifp->prefered_lft) { |
Brian Haley | a1ed052 | 2009-07-02 07:10:52 +0000 | [diff] [blame] | 3533 | /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | int deprecate = 0; |
| 3535 | |
| 3536 | if (!(ifp->flags&IFA_F_DEPRECATED)) { |
| 3537 | deprecate = 1; |
| 3538 | ifp->flags |= IFA_F_DEPRECATED; |
| 3539 | } |
| 3540 | |
| 3541 | if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) |
| 3542 | next = ifp->tstamp + ifp->valid_lft * HZ; |
| 3543 | |
| 3544 | spin_unlock(&ifp->lock); |
| 3545 | |
| 3546 | if (deprecate) { |
| 3547 | in6_ifa_hold(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
| 3549 | ipv6_ifa_notify(0, ifp); |
| 3550 | in6_ifa_put(ifp); |
| 3551 | goto restart; |
| 3552 | } |
| 3553 | #ifdef CONFIG_IPV6_PRIVACY |
| 3554 | } else if ((ifp->flags&IFA_F_TEMPORARY) && |
| 3555 | !(ifp->flags&IFA_F_TENTATIVE)) { |
stephen hemminger | 88949cf | 2010-03-17 20:31:17 +0000 | [diff] [blame] | 3556 | unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * |
| 3557 | ifp->idev->cnf.dad_transmits * |
| 3558 | ifp->idev->nd_parms->retrans_time / HZ; |
| 3559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | if (age >= ifp->prefered_lft - regen_advance) { |
| 3561 | struct inet6_ifaddr *ifpub = ifp->ifpub; |
| 3562 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) |
| 3563 | next = ifp->tstamp + ifp->prefered_lft * HZ; |
| 3564 | if (!ifp->regen_count && ifpub) { |
| 3565 | ifp->regen_count++; |
| 3566 | in6_ifa_hold(ifp); |
| 3567 | in6_ifa_hold(ifpub); |
| 3568 | spin_unlock(&ifp->lock); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3569 | |
Hiroyuki YAMAMORI | 291d809 | 2005-12-23 11:24:05 -0800 | [diff] [blame] | 3570 | spin_lock(&ifpub->lock); |
| 3571 | ifpub->regen_count = 0; |
| 3572 | spin_unlock(&ifpub->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | ipv6_create_tempaddr(ifpub, ifp); |
| 3574 | in6_ifa_put(ifpub); |
| 3575 | in6_ifa_put(ifp); |
| 3576 | goto restart; |
| 3577 | } |
| 3578 | } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) |
| 3579 | next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; |
| 3580 | spin_unlock(&ifp->lock); |
| 3581 | #endif |
| 3582 | } else { |
| 3583 | /* ifp->prefered_lft <= ifp->valid_lft */ |
| 3584 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) |
| 3585 | next = ifp->tstamp + ifp->prefered_lft * HZ; |
| 3586 | spin_unlock(&ifp->lock); |
| 3587 | } |
| 3588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | } |
| 3590 | |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 3591 | next_sec = round_jiffies_up(next); |
| 3592 | next_sched = next; |
| 3593 | |
| 3594 | /* If rounded timeout is accurate enough, accept it. */ |
| 3595 | if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) |
| 3596 | next_sched = next_sec; |
| 3597 | |
| 3598 | /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ |
| 3599 | if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) |
| 3600 | next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; |
| 3601 | |
| 3602 | ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", |
| 3603 | now, next, next_sec, next_sched)); |
| 3604 | |
| 3605 | addr_chk_timer.expires = next_sched; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | add_timer(&addr_chk_timer); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 3607 | spin_unlock(&addrconf_verify_lock); |
| 3608 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3611 | static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) |
| 3612 | { |
| 3613 | struct in6_addr *pfx = NULL; |
| 3614 | |
| 3615 | if (addr) |
| 3616 | pfx = nla_data(addr); |
| 3617 | |
| 3618 | if (local) { |
| 3619 | if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) |
| 3620 | pfx = NULL; |
| 3621 | else |
| 3622 | pfx = nla_data(local); |
| 3623 | } |
| 3624 | |
| 3625 | return pfx; |
| 3626 | } |
| 3627 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 3628 | static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3629 | [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, |
| 3630 | [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, |
| 3631 | [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, |
| 3632 | }; |
| 3633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | static int |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3635 | inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3637 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 3638 | struct ifaddrmsg *ifm; |
| 3639 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | struct in6_addr *pfx; |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 3641 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 3643 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
| 3644 | if (err < 0) |
| 3645 | return err; |
| 3646 | |
| 3647 | ifm = nlmsg_data(nlh); |
| 3648 | pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | if (pfx == NULL) |
| 3650 | return -EINVAL; |
| 3651 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3652 | return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3655 | static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, |
| 3656 | u32 prefered_lft, u32 valid_lft) |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3657 | { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 3658 | u32 flags; |
| 3659 | clock_t expires; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 3660 | unsigned long timeout; |
YOSHIFUJI Hideaki | 46d4804 | 2007-02-07 20:36:26 +0900 | [diff] [blame] | 3661 | |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3662 | if (!valid_lft || (prefered_lft > valid_lft)) |
| 3663 | return -EINVAL; |
| 3664 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 3665 | timeout = addrconf_timeout_fixup(valid_lft, HZ); |
| 3666 | if (addrconf_finite_timeout(timeout)) { |
| 3667 | expires = jiffies_to_clock_t(timeout * HZ); |
| 3668 | valid_lft = timeout; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 3669 | flags = RTF_EXPIRES; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 3670 | } else { |
| 3671 | expires = 0; |
| 3672 | flags = 0; |
| 3673 | ifa_flags |= IFA_F_PERMANENT; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 3674 | } |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3675 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 3676 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); |
| 3677 | if (addrconf_finite_timeout(timeout)) { |
| 3678 | if (timeout == 0) |
| 3679 | ifa_flags |= IFA_F_DEPRECATED; |
| 3680 | prefered_lft = timeout; |
| 3681 | } |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3682 | |
| 3683 | spin_lock_bh(&ifp->lock); |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3684 | ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3685 | ifp->tstamp = jiffies; |
| 3686 | ifp->valid_lft = valid_lft; |
| 3687 | ifp->prefered_lft = prefered_lft; |
| 3688 | |
| 3689 | spin_unlock_bh(&ifp->lock); |
| 3690 | if (!(ifp->flags&IFA_F_TENTATIVE)) |
| 3691 | ipv6_ifa_notify(0, ifp); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3692 | |
YOSHIFUJI Hideaki | 46d4804 | 2007-02-07 20:36:26 +0900 | [diff] [blame] | 3693 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 3694 | expires, flags); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3695 | addrconf_verify(0); |
| 3696 | |
| 3697 | return 0; |
| 3698 | } |
| 3699 | |
| 3700 | static int |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3701 | inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3703 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3704 | struct ifaddrmsg *ifm; |
| 3705 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | struct in6_addr *pfx; |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 3707 | struct inet6_ifaddr *ifa; |
| 3708 | struct net_device *dev; |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3709 | u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; |
| 3710 | u8 ifa_flags; |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3711 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3713 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
| 3714 | if (err < 0) |
| 3715 | return err; |
| 3716 | |
| 3717 | ifm = nlmsg_data(nlh); |
| 3718 | pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | if (pfx == NULL) |
| 3720 | return -EINVAL; |
| 3721 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3722 | if (tb[IFA_CACHEINFO]) { |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 3723 | struct ifa_cacheinfo *ci; |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3724 | |
| 3725 | ci = nla_data(tb[IFA_CACHEINFO]); |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 3726 | valid_lft = ci->ifa_valid; |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 3727 | preferred_lft = ci->ifa_prefered; |
| 3728 | } else { |
| 3729 | preferred_lft = INFINITY_LIFE_TIME; |
| 3730 | valid_lft = INFINITY_LIFE_TIME; |
Noriaki TAKAMIYA | 0778769 | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 3731 | } |
| 3732 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3733 | dev = __dev_get_by_index(net, ifm->ifa_index); |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 3734 | if (dev == NULL) |
| 3735 | return -ENODEV; |
| 3736 | |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3737 | /* We ignore other flags so far. */ |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 3738 | ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3739 | |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 3740 | ifa = ipv6_get_ifaddr(net, pfx, dev, 1); |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 3741 | if (ifa == NULL) { |
| 3742 | /* |
| 3743 | * It would be best to check for !NLM_F_CREATE here but |
| 3744 | * userspace alreay relies on not having to provide this. |
| 3745 | */ |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3746 | return inet6_addr_add(net, ifm->ifa_index, pfx, |
| 3747 | ifm->ifa_prefixlen, ifa_flags, |
| 3748 | preferred_lft, valid_lft); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 3749 | } |
| 3750 | |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 3751 | if (nlh->nlmsg_flags & NLM_F_EXCL || |
| 3752 | !(nlh->nlmsg_flags & NLM_F_REPLACE)) |
| 3753 | err = -EEXIST; |
| 3754 | else |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3755 | err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 3756 | |
| 3757 | in6_ifa_put(ifa); |
| 3758 | |
| 3759 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3762 | static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, |
| 3763 | u8 scope, int ifindex) |
| 3764 | { |
| 3765 | struct ifaddrmsg *ifm; |
| 3766 | |
| 3767 | ifm = nlmsg_data(nlh); |
| 3768 | ifm->ifa_family = AF_INET6; |
| 3769 | ifm->ifa_prefixlen = prefixlen; |
| 3770 | ifm->ifa_flags = flags; |
| 3771 | ifm->ifa_scope = scope; |
| 3772 | ifm->ifa_index = ifindex; |
| 3773 | } |
| 3774 | |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3775 | static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, |
| 3776 | unsigned long tstamp, u32 preferred, u32 valid) |
| 3777 | { |
| 3778 | struct ifa_cacheinfo ci; |
| 3779 | |
Thomas Graf | 18a31e1 | 2010-11-17 04:12:02 +0000 | [diff] [blame] | 3780 | ci.cstamp = cstamp_delta(cstamp); |
| 3781 | ci.tstamp = cstamp_delta(tstamp); |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3782 | ci.ifa_prefered = preferred; |
| 3783 | ci.ifa_valid = valid; |
| 3784 | |
| 3785 | return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); |
| 3786 | } |
| 3787 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3788 | static inline int rt_scope(int ifa_scope) |
| 3789 | { |
| 3790 | if (ifa_scope & IFA_HOST) |
| 3791 | return RT_SCOPE_HOST; |
| 3792 | else if (ifa_scope & IFA_LINK) |
| 3793 | return RT_SCOPE_LINK; |
| 3794 | else if (ifa_scope & IFA_SITE) |
| 3795 | return RT_SCOPE_SITE; |
| 3796 | else |
| 3797 | return RT_SCOPE_UNIVERSE; |
| 3798 | } |
| 3799 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3800 | static inline int inet6_ifaddr_msgsize(void) |
| 3801 | { |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 3802 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
| 3803 | + nla_total_size(16) /* IFA_ADDRESS */ |
| 3804 | + nla_total_size(sizeof(struct ifa_cacheinfo)); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3805 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 3806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3808 | u32 portid, u32 seq, int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | struct nlmsghdr *nlh; |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3811 | u32 preferred, valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3813 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3814 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3815 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3816 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3817 | put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), |
| 3818 | ifa->idev->dev->ifindex); |
| 3819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | if (!(ifa->flags&IFA_F_PERMANENT)) { |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3821 | preferred = ifa->prefered_lft; |
| 3822 | valid = ifa->valid_lft; |
| 3823 | if (preferred != INFINITY_LIFE_TIME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | long tval = (jiffies - ifa->tstamp)/HZ; |
Jens Rosenboom | a1faa69 | 2009-06-25 04:55:50 +0000 | [diff] [blame] | 3825 | if (preferred > tval) |
| 3826 | preferred -= tval; |
| 3827 | else |
| 3828 | preferred = 0; |
Ben Hutchings | f56619f | 2010-06-26 11:37:47 +0000 | [diff] [blame] | 3829 | if (valid != INFINITY_LIFE_TIME) { |
| 3830 | if (valid > tval) |
| 3831 | valid -= tval; |
| 3832 | else |
| 3833 | valid = 0; |
| 3834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } else { |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3837 | preferred = INFINITY_LIFE_TIME; |
| 3838 | valid = INFINITY_LIFE_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3840 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3841 | if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3842 | put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { |
| 3843 | nlmsg_cancel(skb, nlh); |
| 3844 | return -EMSGSIZE; |
| 3845 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3846 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3847 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3851 | u32 portid, u32 seq, int event, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | struct nlmsghdr *nlh; |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3854 | u8 scope = RT_SCOPE_UNIVERSE; |
| 3855 | int ifindex = ifmca->idev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3857 | if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) |
| 3858 | scope = RT_SCOPE_SITE; |
| 3859 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3860 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3861 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3862 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3863 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3864 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3865 | if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || |
| 3866 | put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3867 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
| 3868 | nlmsg_cancel(skb, nlh); |
| 3869 | return -EMSGSIZE; |
| 3870 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3871 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3872 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3876 | u32 portid, u32 seq, int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | struct nlmsghdr *nlh; |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3879 | u8 scope = RT_SCOPE_UNIVERSE; |
| 3880 | int ifindex = ifaca->aca_idev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3882 | if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) |
| 3883 | scope = RT_SCOPE_SITE; |
| 3884 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3885 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3886 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3887 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3888 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 3889 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3890 | if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || |
| 3891 | put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3892 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
| 3893 | nlmsg_cancel(skb, nlh); |
| 3894 | return -EMSGSIZE; |
| 3895 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 3896 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 3897 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | } |
| 3899 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3900 | enum addr_type_t { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | UNICAST_ADDR, |
| 3902 | MULTICAST_ADDR, |
| 3903 | ANYCAST_ADDR, |
| 3904 | }; |
| 3905 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3906 | /* called with rcu_read_lock() */ |
| 3907 | static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, |
| 3908 | struct netlink_callback *cb, enum addr_type_t type, |
| 3909 | int s_ip_idx, int *p_ip_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | struct ifmcaddr6 *ifmca; |
| 3912 | struct ifacaddr6 *ifaca; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3913 | int err = 1; |
| 3914 | int ip_idx = *p_ip_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3916 | read_lock_bh(&idev->lock); |
| 3917 | switch (type) { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3918 | case UNICAST_ADDR: { |
| 3919 | struct inet6_ifaddr *ifa; |
| 3920 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3921 | /* unicast address incl. temp addr */ |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3922 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
| 3923 | if (++ip_idx < s_ip_idx) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3924 | continue; |
| 3925 | err = inet6_fill_ifaddr(skb, ifa, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3926 | NETLINK_CB(cb->skb).portid, |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3927 | cb->nlh->nlmsg_seq, |
| 3928 | RTM_NEWADDR, |
| 3929 | NLM_F_MULTI); |
| 3930 | if (err <= 0) |
| 3931 | break; |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 3932 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3934 | break; |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3935 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3936 | case MULTICAST_ADDR: |
| 3937 | /* multicast address */ |
| 3938 | for (ifmca = idev->mc_list; ifmca; |
| 3939 | ifmca = ifmca->next, ip_idx++) { |
| 3940 | if (ip_idx < s_ip_idx) |
| 3941 | continue; |
| 3942 | err = inet6_fill_ifmcaddr(skb, ifmca, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3943 | NETLINK_CB(cb->skb).portid, |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3944 | cb->nlh->nlmsg_seq, |
| 3945 | RTM_GETMULTICAST, |
| 3946 | NLM_F_MULTI); |
| 3947 | if (err <= 0) |
| 3948 | break; |
| 3949 | } |
| 3950 | break; |
| 3951 | case ANYCAST_ADDR: |
| 3952 | /* anycast address */ |
| 3953 | for (ifaca = idev->ac_list; ifaca; |
| 3954 | ifaca = ifaca->aca_next, ip_idx++) { |
| 3955 | if (ip_idx < s_ip_idx) |
| 3956 | continue; |
| 3957 | err = inet6_fill_ifacaddr(skb, ifaca, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3958 | NETLINK_CB(cb->skb).portid, |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3959 | cb->nlh->nlmsg_seq, |
| 3960 | RTM_GETANYCAST, |
| 3961 | NLM_F_MULTI); |
| 3962 | if (err <= 0) |
| 3963 | break; |
| 3964 | } |
| 3965 | break; |
| 3966 | default: |
| 3967 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3969 | read_unlock_bh(&idev->lock); |
| 3970 | *p_ip_idx = ip_idx; |
| 3971 | return err; |
| 3972 | } |
| 3973 | |
| 3974 | static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, |
| 3975 | enum addr_type_t type) |
| 3976 | { |
| 3977 | struct net *net = sock_net(skb->sk); |
| 3978 | int h, s_h; |
| 3979 | int idx, ip_idx; |
| 3980 | int s_idx, s_ip_idx; |
| 3981 | struct net_device *dev; |
| 3982 | struct inet6_dev *idev; |
| 3983 | struct hlist_head *head; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3984 | |
| 3985 | s_h = cb->args[0]; |
| 3986 | s_idx = idx = cb->args[1]; |
| 3987 | s_ip_idx = ip_idx = cb->args[2]; |
| 3988 | |
| 3989 | rcu_read_lock(); |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 3990 | cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3991 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 3992 | idx = 0; |
| 3993 | head = &net->dev_index_head[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3994 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3995 | if (idx < s_idx) |
| 3996 | goto cont; |
Patrick McHardy | 4b97efd | 2010-03-26 20:27:49 -0700 | [diff] [blame] | 3997 | if (h > s_h || idx > s_idx) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 3998 | s_ip_idx = 0; |
| 3999 | ip_idx = 0; |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4000 | idev = __in6_dev_get(dev); |
| 4001 | if (!idev) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 4002 | goto cont; |
| 4003 | |
| 4004 | if (in6_dump_addrs(idev, skb, cb, type, |
| 4005 | s_ip_idx, &ip_idx) <= 0) |
| 4006 | goto done; |
| 4007 | cont: |
| 4008 | idx++; |
| 4009 | } |
| 4010 | } |
| 4011 | done: |
| 4012 | rcu_read_unlock(); |
| 4013 | cb->args[0] = h; |
| 4014 | cb->args[1] = idx; |
| 4015 | cb->args[2] = ip_idx; |
| 4016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | return skb->len; |
| 4018 | } |
| 4019 | |
| 4020 | static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 4021 | { |
| 4022 | enum addr_type_t type = UNICAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 4023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | return inet6_dump_addr(skb, cb, type); |
| 4025 | } |
| 4026 | |
| 4027 | static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 4028 | { |
| 4029 | enum addr_type_t type = MULTICAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 4030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | return inet6_dump_addr(skb, cb, type); |
| 4032 | } |
| 4033 | |
| 4034 | |
| 4035 | static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 4036 | { |
| 4037 | enum addr_type_t type = ANYCAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 4038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | return inet6_dump_addr(skb, cb, type); |
| 4040 | } |
| 4041 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 4042 | static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4043 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4044 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4045 | struct ifaddrmsg *ifm; |
| 4046 | struct nlattr *tb[IFA_MAX+1]; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4047 | struct in6_addr *addr = NULL; |
| 4048 | struct net_device *dev = NULL; |
| 4049 | struct inet6_ifaddr *ifa; |
| 4050 | struct sk_buff *skb; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4051 | int err; |
| 4052 | |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4053 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
| 4054 | if (err < 0) |
| 4055 | goto errout; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4056 | |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4057 | addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); |
| 4058 | if (addr == NULL) { |
| 4059 | err = -EINVAL; |
| 4060 | goto errout; |
| 4061 | } |
| 4062 | |
| 4063 | ifm = nlmsg_data(nlh); |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4064 | if (ifm->ifa_index) |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 4065 | dev = __dev_get_by_index(net, ifm->ifa_index); |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4066 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4067 | ifa = ipv6_get_ifaddr(net, addr, dev, 1); |
| 4068 | if (!ifa) { |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4069 | err = -EADDRNOTAVAIL; |
| 4070 | goto errout; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4071 | } |
| 4072 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4073 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); |
| 4074 | if (!skb) { |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4075 | err = -ENOBUFS; |
| 4076 | goto errout_ifa; |
| 4077 | } |
| 4078 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4079 | err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4080 | nlh->nlmsg_seq, RTM_NEWADDR, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4081 | if (err < 0) { |
| 4082 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ |
| 4083 | WARN_ON(err == -EMSGSIZE); |
| 4084 | kfree_skb(skb); |
| 4085 | goto errout_ifa; |
| 4086 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4087 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4088 | errout_ifa: |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4089 | in6_ifa_put(ifa); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 4090 | errout: |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4091 | return err; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 4092 | } |
| 4093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) |
| 4095 | { |
| 4096 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4097 | struct net *net = dev_net(ifa->idev->dev); |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 4098 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4100 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 4101 | if (skb == NULL) |
| 4102 | goto errout; |
| 4103 | |
| 4104 | err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4105 | if (err < 0) { |
| 4106 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ |
| 4107 | WARN_ON(err == -EMSGSIZE); |
| 4108 | kfree_skb(skb); |
| 4109 | goto errout; |
| 4110 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 4111 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); |
| 4112 | return; |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 4113 | errout: |
| 4114 | if (err < 0) |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 4115 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | } |
| 4117 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 4118 | static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | __s32 *array, int bytes) |
| 4120 | { |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 4121 | BUG_ON(bytes < (DEVCONF_MAX * 4)); |
| 4122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | memset(array, 0, bytes); |
| 4124 | array[DEVCONF_FORWARDING] = cnf->forwarding; |
| 4125 | array[DEVCONF_HOPLIMIT] = cnf->hop_limit; |
| 4126 | array[DEVCONF_MTU6] = cnf->mtu6; |
| 4127 | array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; |
| 4128 | array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; |
| 4129 | array[DEVCONF_AUTOCONF] = cnf->autoconf; |
| 4130 | array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; |
| 4131 | array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; |
Thomas Graf | 93908d1 | 2010-11-17 01:44:24 +0000 | [diff] [blame] | 4132 | array[DEVCONF_RTR_SOLICIT_INTERVAL] = |
| 4133 | jiffies_to_msecs(cnf->rtr_solicit_interval); |
| 4134 | array[DEVCONF_RTR_SOLICIT_DELAY] = |
| 4135 | jiffies_to_msecs(cnf->rtr_solicit_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; |
| 4137 | #ifdef CONFIG_IPV6_PRIVACY |
| 4138 | array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; |
| 4139 | array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; |
| 4140 | array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; |
| 4141 | array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; |
| 4142 | array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; |
| 4143 | #endif |
| 4144 | array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 4145 | array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 4146 | array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 4147 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 4148 | array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; |
Thomas Graf | 93908d1 | 2010-11-17 01:44:24 +0000 | [diff] [blame] | 4149 | array[DEVCONF_RTR_PROBE_INTERVAL] = |
| 4150 | jiffies_to_msecs(cnf->rtr_probe_interval); |
Neil Horman | fa03ef3 | 2007-01-30 14:30:10 -0800 | [diff] [blame] | 4151 | #ifdef CONFIG_IPV6_ROUTE_INFO |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 4152 | array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; |
| 4153 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 4154 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 4155 | array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 4156 | array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 4157 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 4158 | array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; |
| 4159 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4160 | #ifdef CONFIG_IPV6_MROUTE |
| 4161 | array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; |
| 4162 | #endif |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 4163 | array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 4164 | array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; |
Cosmin Ratiu | c3faca0 | 2009-10-09 03:11:14 +0000 | [diff] [blame] | 4165 | array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; |
Hannes Frederic Sowa | 5cb0443 | 2012-11-06 16:46:20 +0000 | [diff] [blame] | 4166 | array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | } |
| 4168 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4169 | static inline size_t inet6_ifla6_size(void) |
| 4170 | { |
| 4171 | return nla_total_size(4) /* IFLA_INET6_FLAGS */ |
| 4172 | + nla_total_size(sizeof(struct ifla_cacheinfo)) |
| 4173 | + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ |
| 4174 | + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 4175 | + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ |
| 4176 | + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4179 | static inline size_t inet6_if_nlmsg_size(void) |
| 4180 | { |
| 4181 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 4182 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 4183 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 4184 | + nla_total_size(4) /* IFLA_MTU */ |
| 4185 | + nla_total_size(4) /* IFLA_LINK */ |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4186 | + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4187 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 4188 | |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 4189 | static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 4190 | int items, int bytes) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4191 | { |
| 4192 | int i; |
| 4193 | int pad = bytes - sizeof(u64) * items; |
| 4194 | BUG_ON(pad < 0); |
| 4195 | |
| 4196 | /* Use put_unaligned() because stats may not be aligned for u64. */ |
| 4197 | put_unaligned(items, &stats[0]); |
| 4198 | for (i = 1; i < items; i++) |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 4199 | put_unaligned(atomic_long_read(&mib[i]), &stats[i]); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4200 | |
| 4201 | memset(&stats[items], 0, pad); |
| 4202 | } |
| 4203 | |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 4204 | static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, |
| 4205 | int items, int bytes, size_t syncpoff) |
| 4206 | { |
| 4207 | int i; |
| 4208 | int pad = bytes - sizeof(u64) * items; |
| 4209 | BUG_ON(pad < 0); |
| 4210 | |
| 4211 | /* Use put_unaligned() because stats may not be aligned for u64. */ |
| 4212 | put_unaligned(items, &stats[0]); |
| 4213 | for (i = 1; i < items; i++) |
| 4214 | put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); |
| 4215 | |
| 4216 | memset(&stats[items], 0, pad); |
| 4217 | } |
| 4218 | |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4219 | static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, |
| 4220 | int bytes) |
| 4221 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4222 | switch (attrtype) { |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4223 | case IFLA_INET6_STATS: |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 4224 | __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, |
| 4225 | IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4226 | break; |
| 4227 | case IFLA_INET6_ICMP6STATS: |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 4228 | __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 4229 | break; |
| 4230 | } |
| 4231 | } |
| 4232 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4233 | static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | { |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 4235 | struct nlattr *nla; |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 4236 | struct ifla_cacheinfo ci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4238 | if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) |
| 4239 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | ci.max_reasm_len = IPV6_MAXPLEN; |
David S. Miller | 2491242 | 2010-11-19 13:13:47 -0800 | [diff] [blame] | 4241 | ci.tstamp = cstamp_delta(idev->tstamp); |
| 4242 | ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); |
| 4243 | ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time); |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4244 | if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) |
| 4245 | goto nla_put_failure; |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 4246 | nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); |
| 4247 | if (nla == NULL) |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 4248 | goto nla_put_failure; |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 4249 | ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 4251 | /* XXX - MC not implemented */ |
| 4252 | |
| 4253 | nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); |
| 4254 | if (nla == NULL) |
| 4255 | goto nla_put_failure; |
| 4256 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); |
| 4257 | |
| 4258 | nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); |
| 4259 | if (nla == NULL) |
| 4260 | goto nla_put_failure; |
| 4261 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 4263 | nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); |
| 4264 | if (nla == NULL) |
| 4265 | goto nla_put_failure; |
| 4266 | read_lock_bh(&idev->lock); |
| 4267 | memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); |
| 4268 | read_unlock_bh(&idev->lock); |
| 4269 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4270 | return 0; |
| 4271 | |
| 4272 | nla_put_failure: |
| 4273 | return -EMSGSIZE; |
| 4274 | } |
| 4275 | |
| 4276 | static size_t inet6_get_link_af_size(const struct net_device *dev) |
| 4277 | { |
| 4278 | if (!__in6_dev_get(dev)) |
| 4279 | return 0; |
| 4280 | |
| 4281 | return inet6_ifla6_size(); |
| 4282 | } |
| 4283 | |
| 4284 | static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) |
| 4285 | { |
| 4286 | struct inet6_dev *idev = __in6_dev_get(dev); |
| 4287 | |
| 4288 | if (!idev) |
| 4289 | return -ENODATA; |
| 4290 | |
| 4291 | if (inet6_fill_ifla6_attrs(skb, idev) < 0) |
| 4292 | return -EMSGSIZE; |
| 4293 | |
| 4294 | return 0; |
| 4295 | } |
| 4296 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 4297 | static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) |
| 4298 | { |
| 4299 | struct in6_addr ll_addr; |
| 4300 | struct inet6_ifaddr *ifp; |
| 4301 | struct net_device *dev = idev->dev; |
| 4302 | |
| 4303 | if (token == NULL) |
| 4304 | return -EINVAL; |
| 4305 | if (ipv6_addr_any(token)) |
| 4306 | return -EINVAL; |
| 4307 | if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) |
| 4308 | return -EINVAL; |
| 4309 | if (idev->dead || !(idev->if_flags & IF_READY)) |
| 4310 | return -EINVAL; |
| 4311 | if (!ipv6_accept_ra(idev)) |
| 4312 | return -EINVAL; |
| 4313 | if (idev->cnf.rtr_solicits <= 0) |
| 4314 | return -EINVAL; |
| 4315 | |
| 4316 | write_lock_bh(&idev->lock); |
| 4317 | |
| 4318 | BUILD_BUG_ON(sizeof(token->s6_addr) != 16); |
| 4319 | memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); |
| 4320 | |
| 4321 | write_unlock_bh(&idev->lock); |
| 4322 | |
| 4323 | ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | IFA_F_OPTIMISTIC); |
| 4324 | ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); |
| 4325 | |
| 4326 | write_lock_bh(&idev->lock); |
| 4327 | idev->if_flags |= IF_RS_SENT; |
| 4328 | |
| 4329 | /* Well, that's kinda nasty ... */ |
| 4330 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
| 4331 | spin_lock(&ifp->lock); |
| 4332 | if (ipv6_addr_src_scope(&ifp->addr) == |
| 4333 | IPV6_ADDR_SCOPE_GLOBAL) { |
| 4334 | ifp->valid_lft = 0; |
| 4335 | ifp->prefered_lft = 0; |
| 4336 | } |
| 4337 | spin_unlock(&ifp->lock); |
| 4338 | } |
| 4339 | |
| 4340 | write_unlock_bh(&idev->lock); |
| 4341 | return 0; |
| 4342 | } |
| 4343 | |
| 4344 | static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) |
| 4345 | { |
| 4346 | int err = -EINVAL; |
| 4347 | struct inet6_dev *idev = __in6_dev_get(dev); |
| 4348 | struct nlattr *tb[IFLA_INET6_MAX + 1]; |
| 4349 | |
| 4350 | if (!idev) |
| 4351 | return -EAFNOSUPPORT; |
| 4352 | |
| 4353 | if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) |
| 4354 | BUG(); |
| 4355 | |
| 4356 | if (tb[IFLA_INET6_TOKEN]) |
| 4357 | err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); |
| 4358 | |
| 4359 | return err; |
| 4360 | } |
| 4361 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4362 | static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4363 | u32 portid, u32 seq, int event, unsigned int flags) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4364 | { |
| 4365 | struct net_device *dev = idev->dev; |
| 4366 | struct ifinfomsg *hdr; |
| 4367 | struct nlmsghdr *nlh; |
| 4368 | void *protoinfo; |
| 4369 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4370 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4371 | if (nlh == NULL) |
| 4372 | return -EMSGSIZE; |
| 4373 | |
| 4374 | hdr = nlmsg_data(nlh); |
| 4375 | hdr->ifi_family = AF_INET6; |
| 4376 | hdr->__ifi_pad = 0; |
| 4377 | hdr->ifi_type = dev->type; |
| 4378 | hdr->ifi_index = dev->ifindex; |
| 4379 | hdr->ifi_flags = dev_get_flags(dev); |
| 4380 | hdr->ifi_change = 0; |
| 4381 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4382 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 4383 | (dev->addr_len && |
| 4384 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
| 4385 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 4386 | (dev->ifindex != dev->iflink && |
| 4387 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) |
| 4388 | goto nla_put_failure; |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 4389 | protoinfo = nla_nest_start(skb, IFLA_PROTINFO); |
| 4390 | if (protoinfo == NULL) |
| 4391 | goto nla_put_failure; |
| 4392 | |
| 4393 | if (inet6_fill_ifla6_attrs(skb, idev) < 0) |
| 4394 | goto nla_put_failure; |
| 4395 | |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 4396 | nla_nest_end(skb, protoinfo); |
| 4397 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 4399 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4400 | nlmsg_cancel(skb, nlh); |
| 4401 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
| 4405 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4406 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4407 | int h, s_h; |
David S. Miller | 434a8a5 | 2009-11-11 18:53:00 -0800 | [diff] [blame] | 4408 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | struct net_device *dev; |
| 4410 | struct inet6_dev *idev; |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4411 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4413 | s_h = cb->args[0]; |
| 4414 | s_idx = cb->args[1]; |
| 4415 | |
| 4416 | rcu_read_lock(); |
| 4417 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 4418 | idx = 0; |
| 4419 | head = &net->dev_index_head[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4420 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4421 | if (idx < s_idx) |
| 4422 | goto cont; |
| 4423 | idev = __in6_dev_get(dev); |
| 4424 | if (!idev) |
| 4425 | goto cont; |
| 4426 | if (inet6_fill_ifinfo(skb, idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4427 | NETLINK_CB(cb->skb).portid, |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4428 | cb->nlh->nlmsg_seq, |
| 4429 | RTM_NEWLINK, NLM_F_MULTI) <= 0) |
| 4430 | goto out; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 4431 | cont: |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4432 | idx++; |
| 4433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | } |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 4435 | out: |
| 4436 | rcu_read_unlock(); |
| 4437 | cb->args[1] = idx; |
| 4438 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | |
| 4440 | return skb->len; |
| 4441 | } |
| 4442 | |
| 4443 | void inet6_ifinfo_notify(int event, struct inet6_dev *idev) |
| 4444 | { |
| 4445 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4446 | struct net *net = dev_net(idev->dev); |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 4447 | int err = -ENOBUFS; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4448 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4449 | skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 4450 | if (skb == NULL) |
| 4451 | goto errout; |
| 4452 | |
| 4453 | err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4454 | if (err < 0) { |
| 4455 | /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ |
| 4456 | WARN_ON(err == -EMSGSIZE); |
| 4457 | kfree_skb(skb); |
| 4458 | goto errout; |
| 4459 | } |
Nicolas Dichtel | 5f75a10 | 2010-12-07 23:38:31 +0000 | [diff] [blame] | 4460 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 4461 | return; |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 4462 | errout: |
| 4463 | if (err < 0) |
Nicolas Dichtel | 5f75a10 | 2010-12-07 23:38:31 +0000 | [diff] [blame] | 4464 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | } |
| 4466 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4467 | static inline size_t inet6_prefix_nlmsg_size(void) |
| 4468 | { |
| 4469 | return NLMSG_ALIGN(sizeof(struct prefixmsg)) |
| 4470 | + nla_total_size(sizeof(struct in6_addr)) |
| 4471 | + nla_total_size(sizeof(struct prefix_cacheinfo)); |
| 4472 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 4473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4475 | struct prefix_info *pinfo, u32 portid, u32 seq, |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4476 | int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | { |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4478 | struct prefixmsg *pmsg; |
| 4479 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | struct prefix_cacheinfo ci; |
| 4481 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4482 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4483 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4484 | return -EMSGSIZE; |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4485 | |
| 4486 | pmsg = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | pmsg->prefix_family = AF_INET6; |
Patrick McHardy | 8a47077 | 2005-06-28 12:56:45 -0700 | [diff] [blame] | 4488 | pmsg->prefix_pad1 = 0; |
| 4489 | pmsg->prefix_pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | pmsg->prefix_ifindex = idev->dev->ifindex; |
| 4491 | pmsg->prefix_len = pinfo->prefix_len; |
| 4492 | pmsg->prefix_type = pinfo->type; |
Patrick McHardy | 8a47077 | 2005-06-28 12:56:45 -0700 | [diff] [blame] | 4493 | pmsg->prefix_pad3 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | pmsg->prefix_flags = 0; |
| 4495 | if (pinfo->onlink) |
| 4496 | pmsg->prefix_flags |= IF_PREFIX_ONLINK; |
| 4497 | if (pinfo->autoconf) |
| 4498 | pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; |
| 4499 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4500 | if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) |
| 4501 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | ci.preferred_time = ntohl(pinfo->prefered); |
| 4503 | ci.valid_time = ntohl(pinfo->valid); |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4504 | if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) |
| 4505 | goto nla_put_failure; |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4506 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 4508 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4509 | nlmsg_cancel(skb, nlh); |
| 4510 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | } |
| 4512 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4513 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | struct prefix_info *pinfo) |
| 4515 | { |
| 4516 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4517 | struct net *net = dev_net(idev->dev); |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 4518 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4520 | skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 4521 | if (skb == NULL) |
| 4522 | goto errout; |
| 4523 | |
| 4524 | err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4525 | if (err < 0) { |
| 4526 | /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ |
| 4527 | WARN_ON(err == -EMSGSIZE); |
| 4528 | kfree_skb(skb); |
| 4529 | goto errout; |
| 4530 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 4531 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); |
| 4532 | return; |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 4533 | errout: |
| 4534 | if (err < 0) |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 4535 | rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) |
| 4539 | { |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 4540 | struct net *net = dev_net(ifp->idev->dev); |
| 4541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); |
| 4543 | |
| 4544 | switch (event) { |
| 4545 | case RTM_NEWADDR: |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 4546 | /* |
| 4547 | * If the address was optimistic |
| 4548 | * we inserted the route at the start of |
| 4549 | * our DAD process, so we don't need |
| 4550 | * to do it again |
| 4551 | */ |
| 4552 | if (!(ifp->rt->rt6i_node)) |
| 4553 | ip6_ins_rt(ifp->rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | if (ifp->idev->cnf.forwarding) |
| 4555 | addrconf_join_anycast(ifp); |
| 4556 | break; |
| 4557 | case RTM_DELADDR: |
| 4558 | if (ifp->idev->cnf.forwarding) |
| 4559 | addrconf_leave_anycast(ifp); |
| 4560 | addrconf_leave_solict(ifp->idev, &ifp->addr); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4561 | dst_hold(&ifp->rt->dst); |
stephen hemminger | 93fa159 | 2010-04-12 05:41:31 +0000 | [diff] [blame] | 4562 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 4563 | if (ip6_del_rt(ifp->rt)) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4564 | dst_free(&ifp->rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | break; |
| 4566 | } |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 4567 | atomic_inc(&net->ipv6.dev_addr_genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) |
| 4571 | { |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 4572 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | if (likely(ifp->idev->dead == 0)) |
| 4574 | __ipv6_ifa_notify(event, ifp); |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 4575 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | #ifdef CONFIG_SYSCTL |
| 4579 | |
| 4580 | static |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 4581 | int addrconf_sysctl_forward(ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 4583 | { |
| 4584 | int *valp = ctl->data; |
| 4585 | int val = *valp; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 4586 | loff_t pos = *ppos; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4587 | ctl_table lctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | int ret; |
| 4589 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4590 | /* |
| 4591 | * ctl->data points to idev->cnf.forwarding, we should |
| 4592 | * not modify it until we get the rtnl lock. |
| 4593 | */ |
| 4594 | lctl = *ctl; |
| 4595 | lctl.data = &val; |
| 4596 | |
| 4597 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 4599 | if (write) |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 4600 | ret = addrconf_fixup_forwarding(ctl, valp, val); |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 4601 | if (ret) |
| 4602 | *ppos = pos; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4603 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4606 | static void dev_disable_change(struct inet6_dev *idev) |
| 4607 | { |
| 4608 | if (!idev || !idev->dev) |
| 4609 | return; |
| 4610 | |
| 4611 | if (idev->cnf.disable_ipv6) |
| 4612 | addrconf_notify(NULL, NETDEV_DOWN, idev->dev); |
| 4613 | else |
| 4614 | addrconf_notify(NULL, NETDEV_UP, idev->dev); |
| 4615 | } |
| 4616 | |
| 4617 | static void addrconf_disable_change(struct net *net, __s32 newf) |
| 4618 | { |
| 4619 | struct net_device *dev; |
| 4620 | struct inet6_dev *idev; |
| 4621 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 4622 | rcu_read_lock(); |
| 4623 | for_each_netdev_rcu(net, dev) { |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4624 | idev = __in6_dev_get(dev); |
| 4625 | if (idev) { |
| 4626 | int changed = (!idev->cnf.disable_ipv6) ^ (!newf); |
| 4627 | idev->cnf.disable_ipv6 = newf; |
| 4628 | if (changed) |
| 4629 | dev_disable_change(idev); |
| 4630 | } |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4631 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 4632 | rcu_read_unlock(); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4635 | static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4636 | { |
| 4637 | struct net *net; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4638 | int old; |
| 4639 | |
| 4640 | if (!rtnl_trylock()) |
| 4641 | return restart_syscall(); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4642 | |
| 4643 | net = (struct net *)table->extra2; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4644 | old = *p; |
| 4645 | *p = newf; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4646 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4647 | if (p == &net->ipv6.devconf_dflt->disable_ipv6) { |
| 4648 | rtnl_unlock(); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4649 | return 0; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 4650 | } |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4651 | |
| 4652 | if (p == &net->ipv6.devconf_all->disable_ipv6) { |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4653 | net->ipv6.devconf_dflt->disable_ipv6 = newf; |
| 4654 | addrconf_disable_change(net, newf); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4655 | } else if ((!newf) ^ (!old)) |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4656 | dev_disable_change((struct inet6_dev *)table->extra1); |
| 4657 | |
| 4658 | rtnl_unlock(); |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
| 4662 | static |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 4663 | int addrconf_sysctl_disable(ctl_table *ctl, int write, |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4664 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 4665 | { |
| 4666 | int *valp = ctl->data; |
| 4667 | int val = *valp; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 4668 | loff_t pos = *ppos; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4669 | ctl_table lctl; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4670 | int ret; |
| 4671 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 4672 | /* |
| 4673 | * ctl->data points to idev->cnf.disable_ipv6, we should |
| 4674 | * not modify it until we get the rtnl lock. |
| 4675 | */ |
| 4676 | lctl = *ctl; |
| 4677 | lctl.data = &val; |
| 4678 | |
| 4679 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4680 | |
| 4681 | if (write) |
| 4682 | ret = addrconf_disable_ipv6(ctl, valp, val); |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 4683 | if (ret) |
| 4684 | *ppos = pos; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 4685 | return ret; |
| 4686 | } |
| 4687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | static struct addrconf_sysctl_table |
| 4689 | { |
| 4690 | struct ctl_table_header *sysctl_header; |
YOSHIFUJI Hideaki | f6a07b2 | 2008-03-25 00:25:11 +0900 | [diff] [blame] | 4691 | ctl_table addrconf_vars[DEVCONF_MAX+1]; |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 4692 | } addrconf_sysctl __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | .sysctl_header = NULL, |
| 4694 | .addrconf_vars = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4695 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4696 | .procname = "forwarding", |
| 4697 | .data = &ipv6_devconf.forwarding, |
| 4698 | .maxlen = sizeof(int), |
| 4699 | .mode = 0644, |
| 4700 | .proc_handler = addrconf_sysctl_forward, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | }, |
| 4702 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4703 | .procname = "hop_limit", |
| 4704 | .data = &ipv6_devconf.hop_limit, |
| 4705 | .maxlen = sizeof(int), |
| 4706 | .mode = 0644, |
| 4707 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | }, |
| 4709 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4710 | .procname = "mtu", |
| 4711 | .data = &ipv6_devconf.mtu6, |
| 4712 | .maxlen = sizeof(int), |
| 4713 | .mode = 0644, |
| 4714 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | }, |
| 4716 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4717 | .procname = "accept_ra", |
| 4718 | .data = &ipv6_devconf.accept_ra, |
| 4719 | .maxlen = sizeof(int), |
| 4720 | .mode = 0644, |
| 4721 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | }, |
| 4723 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4724 | .procname = "accept_redirects", |
| 4725 | .data = &ipv6_devconf.accept_redirects, |
| 4726 | .maxlen = sizeof(int), |
| 4727 | .mode = 0644, |
| 4728 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | }, |
| 4730 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4731 | .procname = "autoconf", |
| 4732 | .data = &ipv6_devconf.autoconf, |
| 4733 | .maxlen = sizeof(int), |
| 4734 | .mode = 0644, |
| 4735 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | }, |
| 4737 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4738 | .procname = "dad_transmits", |
| 4739 | .data = &ipv6_devconf.dad_transmits, |
| 4740 | .maxlen = sizeof(int), |
| 4741 | .mode = 0644, |
| 4742 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | }, |
| 4744 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4745 | .procname = "router_solicitations", |
| 4746 | .data = &ipv6_devconf.rtr_solicits, |
| 4747 | .maxlen = sizeof(int), |
| 4748 | .mode = 0644, |
| 4749 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | }, |
| 4751 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4752 | .procname = "router_solicitation_interval", |
| 4753 | .data = &ipv6_devconf.rtr_solicit_interval, |
| 4754 | .maxlen = sizeof(int), |
| 4755 | .mode = 0644, |
| 4756 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | }, |
| 4758 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4759 | .procname = "router_solicitation_delay", |
| 4760 | .data = &ipv6_devconf.rtr_solicit_delay, |
| 4761 | .maxlen = sizeof(int), |
| 4762 | .mode = 0644, |
| 4763 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | }, |
| 4765 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4766 | .procname = "force_mld_version", |
| 4767 | .data = &ipv6_devconf.force_mld_version, |
| 4768 | .maxlen = sizeof(int), |
| 4769 | .mode = 0644, |
| 4770 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | }, |
| 4772 | #ifdef CONFIG_IPV6_PRIVACY |
| 4773 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4774 | .procname = "use_tempaddr", |
| 4775 | .data = &ipv6_devconf.use_tempaddr, |
| 4776 | .maxlen = sizeof(int), |
| 4777 | .mode = 0644, |
| 4778 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | }, |
| 4780 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4781 | .procname = "temp_valid_lft", |
| 4782 | .data = &ipv6_devconf.temp_valid_lft, |
| 4783 | .maxlen = sizeof(int), |
| 4784 | .mode = 0644, |
| 4785 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | }, |
| 4787 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4788 | .procname = "temp_prefered_lft", |
| 4789 | .data = &ipv6_devconf.temp_prefered_lft, |
| 4790 | .maxlen = sizeof(int), |
| 4791 | .mode = 0644, |
| 4792 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | }, |
| 4794 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4795 | .procname = "regen_max_retry", |
| 4796 | .data = &ipv6_devconf.regen_max_retry, |
| 4797 | .maxlen = sizeof(int), |
| 4798 | .mode = 0644, |
| 4799 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | }, |
| 4801 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4802 | .procname = "max_desync_factor", |
| 4803 | .data = &ipv6_devconf.max_desync_factor, |
| 4804 | .maxlen = sizeof(int), |
| 4805 | .mode = 0644, |
| 4806 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | }, |
| 4808 | #endif |
| 4809 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4810 | .procname = "max_addresses", |
| 4811 | .data = &ipv6_devconf.max_addresses, |
| 4812 | .maxlen = sizeof(int), |
| 4813 | .mode = 0644, |
| 4814 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | }, |
| 4816 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4817 | .procname = "accept_ra_defrtr", |
| 4818 | .data = &ipv6_devconf.accept_ra_defrtr, |
| 4819 | .maxlen = sizeof(int), |
| 4820 | .mode = 0644, |
| 4821 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 4822 | }, |
| 4823 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4824 | .procname = "accept_ra_pinfo", |
| 4825 | .data = &ipv6_devconf.accept_ra_pinfo, |
| 4826 | .maxlen = sizeof(int), |
| 4827 | .mode = 0644, |
| 4828 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 4829 | }, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 4830 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 4831 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4832 | .procname = "accept_ra_rtr_pref", |
| 4833 | .data = &ipv6_devconf.accept_ra_rtr_pref, |
| 4834 | .maxlen = sizeof(int), |
| 4835 | .mode = 0644, |
| 4836 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 4837 | }, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 4838 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4839 | .procname = "router_probe_interval", |
| 4840 | .data = &ipv6_devconf.rtr_probe_interval, |
| 4841 | .maxlen = sizeof(int), |
| 4842 | .mode = 0644, |
| 4843 | .proc_handler = proc_dointvec_jiffies, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 4844 | }, |
Neil Horman | fa03ef3 | 2007-01-30 14:30:10 -0800 | [diff] [blame] | 4845 | #ifdef CONFIG_IPV6_ROUTE_INFO |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 4846 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4847 | .procname = "accept_ra_rt_info_max_plen", |
| 4848 | .data = &ipv6_devconf.accept_ra_rt_info_max_plen, |
| 4849 | .maxlen = sizeof(int), |
| 4850 | .mode = 0644, |
| 4851 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 4852 | }, |
| 4853 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 4854 | #endif |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 4855 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4856 | .procname = "proxy_ndp", |
| 4857 | .data = &ipv6_devconf.proxy_ndp, |
| 4858 | .maxlen = sizeof(int), |
| 4859 | .mode = 0644, |
| 4860 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 4861 | }, |
| 4862 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4863 | .procname = "accept_source_route", |
| 4864 | .data = &ipv6_devconf.accept_source_route, |
| 4865 | .maxlen = sizeof(int), |
| 4866 | .mode = 0644, |
| 4867 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 4868 | }, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 4869 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 4870 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4871 | .procname = "optimistic_dad", |
| 4872 | .data = &ipv6_devconf.optimistic_dad, |
| 4873 | .maxlen = sizeof(int), |
| 4874 | .mode = 0644, |
| 4875 | .proc_handler = proc_dointvec, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 4876 | |
| 4877 | }, |
| 4878 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4879 | #ifdef CONFIG_IPV6_MROUTE |
| 4880 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4881 | .procname = "mc_forwarding", |
| 4882 | .data = &ipv6_devconf.mc_forwarding, |
| 4883 | .maxlen = sizeof(int), |
| 4884 | .mode = 0444, |
| 4885 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4886 | }, |
| 4887 | #endif |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 4888 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4889 | .procname = "disable_ipv6", |
| 4890 | .data = &ipv6_devconf.disable_ipv6, |
| 4891 | .maxlen = sizeof(int), |
| 4892 | .mode = 0644, |
| 4893 | .proc_handler = addrconf_sysctl_disable, |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 4894 | }, |
| 4895 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4896 | .procname = "accept_dad", |
| 4897 | .data = &ipv6_devconf.accept_dad, |
| 4898 | .maxlen = sizeof(int), |
| 4899 | .mode = 0644, |
| 4900 | .proc_handler = proc_dointvec, |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 4901 | }, |
| 4902 | { |
Octavian Purdila | f7734fd | 2009-10-02 11:39:15 +0000 | [diff] [blame] | 4903 | .procname = "force_tllao", |
| 4904 | .data = &ipv6_devconf.force_tllao, |
| 4905 | .maxlen = sizeof(int), |
| 4906 | .mode = 0644, |
| 4907 | .proc_handler = proc_dointvec |
| 4908 | }, |
| 4909 | { |
Hannes Frederic Sowa | 5cb0443 | 2012-11-06 16:46:20 +0000 | [diff] [blame] | 4910 | .procname = "ndisc_notify", |
| 4911 | .data = &ipv6_devconf.ndisc_notify, |
| 4912 | .maxlen = sizeof(int), |
| 4913 | .mode = 0644, |
| 4914 | .proc_handler = proc_dointvec |
| 4915 | }, |
| 4916 | { |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4917 | /* sentinel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | } |
| 4919 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | }; |
| 4921 | |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 4922 | static int __addrconf_sysctl_register(struct net *net, char *dev_name, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4923 | struct inet6_dev *idev, struct ipv6_devconf *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | { |
| 4925 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | struct addrconf_sysctl_table *t; |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 4927 | char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; |
Pavel Emelyanov | 1dab622 | 2007-12-02 00:59:38 +1100 | [diff] [blame] | 4928 | |
Arnaldo Carvalho de Melo | af879cc | 2006-11-17 12:14:37 -0200 | [diff] [blame] | 4929 | t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | if (t == NULL) |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 4931 | goto out; |
| 4932 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 4933 | for (i = 0; t->addrconf_vars[i].data; i++) { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4934 | t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 4936 | t->addrconf_vars[i].extra2 = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 4939 | snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 4941 | t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | if (t->sysctl_header == NULL) |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 4943 | goto free; |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 4944 | |
| 4945 | p->sysctl = t; |
Pavel Emelyanov | 9589731 | 2008-01-10 17:41:45 -0800 | [diff] [blame] | 4946 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 4948 | free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | kfree(t); |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 4950 | out: |
Pavel Emelyanov | 9589731 | 2008-01-10 17:41:45 -0800 | [diff] [blame] | 4951 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 4954 | static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) |
| 4955 | { |
| 4956 | struct addrconf_sysctl_table *t; |
| 4957 | |
| 4958 | if (p->sysctl == NULL) |
| 4959 | return; |
| 4960 | |
| 4961 | t = p->sysctl; |
| 4962 | p->sysctl = NULL; |
Lucian Adrian Grijincu | ff53881 | 2011-05-01 01:44:01 +0000 | [diff] [blame] | 4963 | unregister_net_sysctl_table(t->sysctl_header); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 4964 | kfree(t); |
| 4965 | } |
| 4966 | |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 4967 | static void addrconf_sysctl_register(struct inet6_dev *idev) |
| 4968 | { |
Eric W. Biederman | 54716e3 | 2010-02-14 03:27:03 +0000 | [diff] [blame] | 4969 | neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4970 | &ndisc_ifinfo_sysctl_change); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4971 | __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4972 | idev, &idev->cnf); |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 4973 | } |
| 4974 | |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 4975 | static void addrconf_sysctl_unregister(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | { |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 4977 | __addrconf_sysctl_unregister(&idev->cnf); |
| 4978 | neigh_sysctl_unregister(idev->nd_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | |
| 4982 | #endif |
| 4983 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4984 | static int __net_init addrconf_init_net(struct net *net) |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 4985 | { |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 4986 | int err = -ENOMEM; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 4987 | struct ipv6_devconf *all, *dflt; |
| 4988 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 4989 | all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); |
| 4990 | if (all == NULL) |
| 4991 | goto err_alloc_all; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 4992 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 4993 | dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); |
| 4994 | if (dflt == NULL) |
| 4995 | goto err_alloc_dflt; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 4996 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 4997 | /* these will be inherited by all namespaces */ |
| 4998 | dflt->autoconf = ipv6_defaults.autoconf; |
| 4999 | dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5000 | |
| 5001 | net->ipv6.devconf_all = all; |
| 5002 | net->ipv6.devconf_dflt = dflt; |
| 5003 | |
| 5004 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 5005 | err = __addrconf_sysctl_register(net, "all", NULL, all); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5006 | if (err < 0) |
| 5007 | goto err_reg_all; |
| 5008 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 5009 | err = __addrconf_sysctl_register(net, "default", NULL, dflt); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5010 | if (err < 0) |
| 5011 | goto err_reg_dflt; |
| 5012 | #endif |
| 5013 | return 0; |
| 5014 | |
| 5015 | #ifdef CONFIG_SYSCTL |
| 5016 | err_reg_dflt: |
| 5017 | __addrconf_sysctl_unregister(all); |
| 5018 | err_reg_all: |
| 5019 | kfree(dflt); |
| 5020 | #endif |
| 5021 | err_alloc_dflt: |
| 5022 | kfree(all); |
| 5023 | err_alloc_all: |
| 5024 | return err; |
| 5025 | } |
| 5026 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5027 | static void __net_exit addrconf_exit_net(struct net *net) |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5028 | { |
| 5029 | #ifdef CONFIG_SYSCTL |
| 5030 | __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); |
| 5031 | __addrconf_sysctl_unregister(net->ipv6.devconf_all); |
| 5032 | #endif |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 5033 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5034 | kfree(net->ipv6.devconf_dflt); |
| 5035 | kfree(net->ipv6.devconf_all); |
| 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | static struct pernet_operations addrconf_ops = { |
| 5040 | .init = addrconf_init_net, |
| 5041 | .exit = addrconf_exit_net, |
| 5042 | }; |
| 5043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | /* |
| 5045 | * Device notifier |
| 5046 | */ |
| 5047 | |
| 5048 | int register_inet6addr_notifier(struct notifier_block *nb) |
| 5049 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5050 | return atomic_notifier_chain_register(&inet6addr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 5052 | EXPORT_SYMBOL(register_inet6addr_notifier); |
| 5053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | int unregister_inet6addr_notifier(struct notifier_block *nb) |
| 5055 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5056 | return atomic_notifier_chain_unregister(&inet6addr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 5058 | EXPORT_SYMBOL(unregister_inet6addr_notifier); |
| 5059 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5060 | static struct rtnl_af_ops inet6_ops = { |
| 5061 | .family = AF_INET6, |
| 5062 | .fill_link_af = inet6_fill_link_af, |
| 5063 | .get_link_af_size = inet6_get_link_af_size, |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame^] | 5064 | .set_link_af = inet6_set_link_af, |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5065 | }; |
| 5066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | /* |
| 5068 | * Init / cleanup code |
| 5069 | */ |
| 5070 | |
| 5071 | int __init addrconf_init(void) |
| 5072 | { |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 5073 | int i, err; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 5074 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5075 | err = ipv6_addr_label_init(); |
| 5076 | if (err < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 5077 | pr_crit("%s: cannot initialize default policy table: %d\n", |
| 5078 | __func__, err); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 5079 | goto out; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 5080 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 5082 | err = register_pernet_subsys(&addrconf_ops); |
| 5083 | if (err < 0) |
| 5084 | goto out_addrlabel; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | /* The addrconf netdev notifier requires that loopback_dev |
| 5087 | * has it's ipv6 private information allocated and setup |
| 5088 | * before it can bring up and give link-local addresses |
| 5089 | * to other devices which are up. |
| 5090 | * |
| 5091 | * Unfortunately, loopback_dev is not necessarily the first |
| 5092 | * entry in the global dev_base list of net devices. In fact, |
| 5093 | * it is likely to be the very last entry on that list. |
| 5094 | * So this causes the notifier registry below to try and |
| 5095 | * give link-local addresses to all devices besides loopback_dev |
| 5096 | * first, then loopback_dev, which cases all the non-loopback_dev |
| 5097 | * devices to fail to get a link-local address. |
| 5098 | * |
| 5099 | * So, as a temporary fix, allocate the ipv6 structure for |
| 5100 | * loopback_dev first by hand. |
| 5101 | * Longer term, all of the dependencies ipv6 has upon the loopback |
| 5102 | * device and it being up should be removed. |
| 5103 | */ |
| 5104 | rtnl_lock(); |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 5105 | if (!ipv6_add_dev(init_net.loopback_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | err = -ENOMEM; |
| 5107 | rtnl_unlock(); |
| 5108 | if (err) |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5109 | goto errlo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 5111 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
| 5112 | INIT_HLIST_HEAD(&inet6_addr_lst[i]); |
| 5113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | register_netdevice_notifier(&ipv6_dev_notf); |
| 5115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | addrconf_verify(0); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5117 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5118 | err = rtnl_af_register(&inet6_ops); |
| 5119 | if (err < 0) |
| 5120 | goto errout_af; |
| 5121 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 5122 | err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, |
| 5123 | NULL); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5124 | if (err < 0) |
| 5125 | goto errout; |
| 5126 | |
| 5127 | /* Only the first call to __rtnl_register can fail */ |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 5128 | __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); |
| 5129 | __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); |
| 5130 | __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, |
| 5131 | inet6_dump_ifaddr, NULL); |
| 5132 | __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, |
| 5133 | inet6_dump_ifmcaddr, NULL); |
| 5134 | __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, |
| 5135 | inet6_dump_ifacaddr, NULL); |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 5136 | __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 5137 | inet6_netconf_dump_devconf, NULL); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5138 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 5139 | ipv6_addr_label_rtnl_register(); |
| 5140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | return 0; |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5142 | errout: |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5143 | rtnl_af_unregister(&inet6_ops); |
| 5144 | errout_af: |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5145 | unregister_netdevice_notifier(&ipv6_dev_notf); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5146 | errlo: |
| 5147 | unregister_pernet_subsys(&addrconf_ops); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 5148 | out_addrlabel: |
| 5149 | ipv6_addr_label_cleanup(); |
| 5150 | out: |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 5151 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
Daniel Lezcano | 09f7709 | 2007-12-13 05:34:58 -0800 | [diff] [blame] | 5154 | void addrconf_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | { |
Daniel Lezcano | 176c39a | 2009-03-03 01:06:45 -0800 | [diff] [blame] | 5156 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | int i; |
| 5158 | |
| 5159 | unregister_netdevice_notifier(&ipv6_dev_notf); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 5160 | unregister_pernet_subsys(&addrconf_ops); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 5161 | ipv6_addr_label_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | |
| 5163 | rtnl_lock(); |
| 5164 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5165 | __rtnl_af_unregister(&inet6_ops); |
| 5166 | |
Daniel Lezcano | 176c39a | 2009-03-03 01:06:45 -0800 | [diff] [blame] | 5167 | /* clean dev list */ |
| 5168 | for_each_netdev(&init_net, dev) { |
| 5169 | if (__in6_dev_get(dev) == NULL) |
| 5170 | continue; |
| 5171 | addrconf_ifdown(dev, 1); |
| 5172 | } |
| 5173 | addrconf_ifdown(init_net.loopback_dev, 2); |
| 5174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | * Check hash table. |
| 5177 | */ |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 5178 | spin_lock_bh(&addrconf_hash_lock); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 5179 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
| 5180 | WARN_ON(!hlist_empty(&inet6_addr_lst[i])); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 5181 | spin_unlock_bh(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | |
| 5183 | del_timer(&addr_chk_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | } |