]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 Address [auto]configuration | |
3 | * Linux INET6 implementation | |
4 | * | |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
7 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
8 | * | |
1da177e4 LT |
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. | |
b1cacb68 YH |
36 | * YOSHIFUJI Hideaki @USAGI : improved source address |
37 | * selection; consider scope, | |
38 | * status etc. | |
1da177e4 LT |
39 | */ |
40 | ||
f3213831 JP |
41 | #define pr_fmt(fmt) "IPv6: " fmt |
42 | ||
1da177e4 LT |
43 | #include <linux/errno.h> |
44 | #include <linux/types.h> | |
a0bffffc | 45 | #include <linux/kernel.h> |
1da177e4 LT |
46 | #include <linux/socket.h> |
47 | #include <linux/sockios.h> | |
1da177e4 | 48 | #include <linux/net.h> |
3d1bec99 | 49 | #include <linux/inet.h> |
1da177e4 LT |
50 | #include <linux/in6.h> |
51 | #include <linux/netdevice.h> | |
1823730f | 52 | #include <linux/if_addr.h> |
1da177e4 LT |
53 | #include <linux/if_arp.h> |
54 | #include <linux/if_arcnet.h> | |
55 | #include <linux/if_infiniband.h> | |
56 | #include <linux/route.h> | |
57 | #include <linux/inetdevice.h> | |
58 | #include <linux/init.h> | |
5a0e3ad6 | 59 | #include <linux/slab.h> |
1da177e4 LT |
60 | #ifdef CONFIG_SYSCTL |
61 | #include <linux/sysctl.h> | |
62 | #endif | |
4fc268d2 | 63 | #include <linux/capability.h> |
1da177e4 LT |
64 | #include <linux/delay.h> |
65 | #include <linux/notifier.h> | |
543537bd | 66 | #include <linux/string.h> |
ddbe5032 | 67 | #include <linux/hash.h> |
1da177e4 | 68 | |
457c4cbc | 69 | #include <net/net_namespace.h> |
1da177e4 LT |
70 | #include <net/sock.h> |
71 | #include <net/snmp.h> | |
72 | ||
5241c2d7 | 73 | #include <net/6lowpan.h> |
cb6bf355 | 74 | #include <net/firewire.h> |
1da177e4 LT |
75 | #include <net/ipv6.h> |
76 | #include <net/protocol.h> | |
77 | #include <net/ndisc.h> | |
78 | #include <net/ip6_route.h> | |
79 | #include <net/addrconf.h> | |
80 | #include <net/tcp.h> | |
81 | #include <net/ip.h> | |
5d620266 | 82 | #include <net/netlink.h> |
f24e3d65 | 83 | #include <net/pkt_sched.h> |
ca254490 | 84 | #include <net/l3mdev.h> |
1da177e4 LT |
85 | #include <linux/if_tunnel.h> |
86 | #include <linux/rtnetlink.h> | |
f3a1bfb1 | 87 | #include <linux/netconf.h> |
1da177e4 | 88 | #include <linux/random.h> |
e21e8467 | 89 | #include <linux/uaccess.h> |
7f7d9a6b | 90 | #include <asm/unaligned.h> |
1da177e4 LT |
91 | |
92 | #include <linux/proc_fs.h> | |
93 | #include <linux/seq_file.h> | |
bc3b2d7f | 94 | #include <linux/export.h> |
1da177e4 LT |
95 | |
96 | /* Set to 3 to get tracing... */ | |
97 | #define ACONF_DEBUG 2 | |
98 | ||
99 | #if ACONF_DEBUG >= 3 | |
ba3542e1 | 100 | #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) |
1da177e4 | 101 | #else |
ba3542e1 | 102 | #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) |
1da177e4 LT |
103 | #endif |
104 | ||
105 | #define INFINITY_LIFE_TIME 0xFFFFFFFF | |
18a31e1e | 106 | |
3d1bec99 HFS |
107 | #define IPV6_MAX_STRLEN \ |
108 | sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") | |
109 | ||
18a31e1e TG |
110 | static inline u32 cstamp_delta(unsigned long cstamp) |
111 | { | |
112 | return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; | |
113 | } | |
1da177e4 LT |
114 | |
115 | #ifdef CONFIG_SYSCTL | |
a317a2f1 | 116 | static int addrconf_sysctl_register(struct inet6_dev *idev); |
408c4768 PE |
117 | static void addrconf_sysctl_unregister(struct inet6_dev *idev); |
118 | #else | |
a317a2f1 | 119 | static inline int addrconf_sysctl_register(struct inet6_dev *idev) |
408c4768 | 120 | { |
a317a2f1 | 121 | return 0; |
408c4768 PE |
122 | } |
123 | ||
124 | static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) | |
125 | { | |
126 | } | |
1da177e4 LT |
127 | #endif |
128 | ||
eb7e0575 SD |
129 | static void __ipv6_regen_rndid(struct inet6_dev *idev); |
130 | static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); | |
1da177e4 | 131 | static void ipv6_regen_rndid(unsigned long data); |
1da177e4 | 132 | |
1b34be74 | 133 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); |
1da177e4 | 134 | static int ipv6_count_addresses(struct inet6_dev *idev); |
622c81d5 HFS |
135 | static int ipv6_generate_stable_address(struct in6_addr *addr, |
136 | u8 dad_count, | |
137 | const struct inet6_dev *idev); | |
1da177e4 LT |
138 | |
139 | /* | |
140 | * Configured unicast address hash table | |
141 | */ | |
c2e21293 | 142 | static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; |
5c578aed | 143 | static DEFINE_SPINLOCK(addrconf_hash_lock); |
1da177e4 | 144 | |
c15b1cca HFS |
145 | static void addrconf_verify(void); |
146 | static void addrconf_verify_rtnl(void); | |
147 | static void addrconf_verify_work(struct work_struct *); | |
1da177e4 | 148 | |
c15b1cca HFS |
149 | static struct workqueue_struct *addrconf_wq; |
150 | static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); | |
1da177e4 LT |
151 | |
152 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp); | |
153 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); | |
154 | ||
93d9b7d7 JP |
155 | static void addrconf_type_change(struct net_device *dev, |
156 | unsigned long event); | |
1da177e4 LT |
157 | static int addrconf_ifdown(struct net_device *dev, int how); |
158 | ||
21caa662 RK |
159 | static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, |
160 | int plen, | |
161 | const struct net_device *dev, | |
162 | u32 flags, u32 noflags); | |
163 | ||
cf22f9a2 | 164 | static void addrconf_dad_start(struct inet6_ifaddr *ifp); |
c15b1cca | 165 | static void addrconf_dad_work(struct work_struct *w); |
1da177e4 | 166 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp); |
c5e33bdd | 167 | static void addrconf_dad_run(struct inet6_dev *idev); |
1da177e4 LT |
168 | static void addrconf_rs_timer(unsigned long data); |
169 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); | |
170 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); | |
171 | ||
1ab1457c | 172 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
1da177e4 | 173 | struct prefix_info *pinfo); |
3e81c6da DM |
174 | static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, |
175 | struct net_device *dev); | |
1da177e4 | 176 | |
888c848e | 177 | static struct ipv6_devconf ipv6_devconf __read_mostly = { |
1da177e4 LT |
178 | .forwarding = 0, |
179 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, | |
180 | .mtu6 = IPV6_MIN_MTU, | |
181 | .accept_ra = 1, | |
182 | .accept_redirects = 1, | |
183 | .autoconf = 1, | |
184 | .force_mld_version = 0, | |
fc4eba58 HFS |
185 | .mldv1_unsolicited_report_interval = 10 * HZ, |
186 | .mldv2_unsolicited_report_interval = HZ, | |
1da177e4 LT |
187 | .dad_transmits = 1, |
188 | .rtr_solicits = MAX_RTR_SOLICITATIONS, | |
189 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, | |
190 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, | |
67ba4152 | 191 | .use_tempaddr = 0, |
1da177e4 LT |
192 | .temp_valid_lft = TEMP_VALID_LIFETIME, |
193 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, | |
194 | .regen_max_retry = REGEN_MAX_RETRY, | |
195 | .max_desync_factor = MAX_DESYNC_FACTOR, | |
1da177e4 | 196 | .max_addresses = IPV6_MAX_ADDRESSES, |
65f5c7c1 | 197 | .accept_ra_defrtr = 1, |
d9333196 | 198 | .accept_ra_from_local = 0, |
8013d1d7 | 199 | .accept_ra_min_hop_limit= 1, |
c4fd30eb | 200 | .accept_ra_pinfo = 1, |
930d6ff2 YH |
201 | #ifdef CONFIG_IPV6_ROUTER_PREF |
202 | .accept_ra_rtr_pref = 1, | |
52e16356 | 203 | .rtr_probe_interval = 60 * HZ, |
09c884d4 YH |
204 | #ifdef CONFIG_IPV6_ROUTE_INFO |
205 | .accept_ra_rt_info_max_plen = 0, | |
206 | #endif | |
930d6ff2 | 207 | #endif |
fbea49e1 | 208 | .proxy_ndp = 0, |
0bcbc926 | 209 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
778d80be | 210 | .disable_ipv6 = 0, |
1b34be74 | 211 | .accept_dad = 1, |
b800c3b9 | 212 | .suppress_frag_ndisc = 1, |
c2943f14 | 213 | .accept_ra_mtu = 1, |
3d1bec99 HFS |
214 | .stable_secret = { |
215 | .initialized = false, | |
3985e8a3 EK |
216 | }, |
217 | .use_oif_addrs_only = 0, | |
35103d11 | 218 | .ignore_routes_with_linkdown = 0, |
f1705ec1 | 219 | .keep_addr_on_down = 0, |
1da177e4 LT |
220 | }; |
221 | ||
ab32ea5d | 222 | static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { |
1da177e4 LT |
223 | .forwarding = 0, |
224 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, | |
225 | .mtu6 = IPV6_MIN_MTU, | |
226 | .accept_ra = 1, | |
227 | .accept_redirects = 1, | |
228 | .autoconf = 1, | |
fc4eba58 HFS |
229 | .force_mld_version = 0, |
230 | .mldv1_unsolicited_report_interval = 10 * HZ, | |
231 | .mldv2_unsolicited_report_interval = HZ, | |
1da177e4 LT |
232 | .dad_transmits = 1, |
233 | .rtr_solicits = MAX_RTR_SOLICITATIONS, | |
234 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, | |
235 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, | |
1da177e4 LT |
236 | .use_tempaddr = 0, |
237 | .temp_valid_lft = TEMP_VALID_LIFETIME, | |
238 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, | |
239 | .regen_max_retry = REGEN_MAX_RETRY, | |
240 | .max_desync_factor = MAX_DESYNC_FACTOR, | |
1da177e4 | 241 | .max_addresses = IPV6_MAX_ADDRESSES, |
65f5c7c1 | 242 | .accept_ra_defrtr = 1, |
d9333196 | 243 | .accept_ra_from_local = 0, |
8013d1d7 | 244 | .accept_ra_min_hop_limit= 1, |
c4fd30eb | 245 | .accept_ra_pinfo = 1, |
930d6ff2 YH |
246 | #ifdef CONFIG_IPV6_ROUTER_PREF |
247 | .accept_ra_rtr_pref = 1, | |
52e16356 | 248 | .rtr_probe_interval = 60 * HZ, |
09c884d4 YH |
249 | #ifdef CONFIG_IPV6_ROUTE_INFO |
250 | .accept_ra_rt_info_max_plen = 0, | |
251 | #endif | |
930d6ff2 | 252 | #endif |
fbea49e1 | 253 | .proxy_ndp = 0, |
0bcbc926 | 254 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
778d80be | 255 | .disable_ipv6 = 0, |
1b34be74 | 256 | .accept_dad = 1, |
b800c3b9 | 257 | .suppress_frag_ndisc = 1, |
c2943f14 | 258 | .accept_ra_mtu = 1, |
3d1bec99 HFS |
259 | .stable_secret = { |
260 | .initialized = false, | |
261 | }, | |
3985e8a3 | 262 | .use_oif_addrs_only = 0, |
35103d11 | 263 | .ignore_routes_with_linkdown = 0, |
f1705ec1 | 264 | .keep_addr_on_down = 0, |
1da177e4 LT |
265 | }; |
266 | ||
f24e3d65 | 267 | /* Check if a valid qdisc is available */ |
05297949 | 268 | static inline bool addrconf_qdisc_ok(const struct net_device *dev) |
f24e3d65 | 269 | { |
05297949 | 270 | return !qdisc_tx_is_noop(dev); |
f24e3d65 MC |
271 | } |
272 | ||
b7b1bfce | 273 | static void addrconf_del_rs_timer(struct inet6_dev *idev) |
1da177e4 | 274 | { |
b7b1bfce HFS |
275 | if (del_timer(&idev->rs_timer)) |
276 | __in6_dev_put(idev); | |
277 | } | |
278 | ||
c15b1cca | 279 | static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) |
b7b1bfce | 280 | { |
c15b1cca | 281 | if (cancel_delayed_work(&ifp->dad_work)) |
1da177e4 LT |
282 | __in6_ifa_put(ifp); |
283 | } | |
284 | ||
b7b1bfce HFS |
285 | static void addrconf_mod_rs_timer(struct inet6_dev *idev, |
286 | unsigned long when) | |
287 | { | |
288 | if (!timer_pending(&idev->rs_timer)) | |
289 | in6_dev_hold(idev); | |
290 | mod_timer(&idev->rs_timer, jiffies + when); | |
291 | } | |
1da177e4 | 292 | |
c15b1cca HFS |
293 | static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, |
294 | unsigned long delay) | |
1da177e4 | 295 | { |
c15b1cca | 296 | if (!delayed_work_pending(&ifp->dad_work)) |
1da177e4 | 297 | in6_ifa_hold(ifp); |
c15b1cca | 298 | mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); |
1da177e4 LT |
299 | } |
300 | ||
7f7d9a6b HX |
301 | static int snmp6_alloc_dev(struct inet6_dev *idev) |
302 | { | |
827da44c JS |
303 | int i; |
304 | ||
698365fa WC |
305 | idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); |
306 | if (!idev->stats.ipv6) | |
7f7d9a6b | 307 | goto err_ip; |
827da44c JS |
308 | |
309 | for_each_possible_cpu(i) { | |
310 | struct ipstats_mib *addrconf_stats; | |
698365fa | 311 | addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); |
827da44c | 312 | u64_stats_init(&addrconf_stats->syncp); |
827da44c JS |
313 | } |
314 | ||
315 | ||
be281e55 ED |
316 | idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), |
317 | GFP_KERNEL); | |
318 | if (!idev->stats.icmpv6dev) | |
7f7d9a6b | 319 | goto err_icmp; |
be281e55 ED |
320 | idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), |
321 | GFP_KERNEL); | |
322 | if (!idev->stats.icmpv6msgdev) | |
14878f75 | 323 | goto err_icmpmsg; |
7f7d9a6b HX |
324 | |
325 | return 0; | |
326 | ||
14878f75 | 327 | err_icmpmsg: |
be281e55 | 328 | kfree(idev->stats.icmpv6dev); |
7f7d9a6b | 329 | err_icmp: |
698365fa | 330 | free_percpu(idev->stats.ipv6); |
7f7d9a6b | 331 | err_ip: |
aaf70ec7 | 332 | return -ENOMEM; |
7f7d9a6b HX |
333 | } |
334 | ||
8e5e8f30 | 335 | static struct inet6_dev *ipv6_add_dev(struct net_device *dev) |
1da177e4 LT |
336 | { |
337 | struct inet6_dev *ndev; | |
a317a2f1 | 338 | int err = -ENOMEM; |
1da177e4 LT |
339 | |
340 | ASSERT_RTNL(); | |
341 | ||
342 | if (dev->mtu < IPV6_MIN_MTU) | |
a317a2f1 | 343 | return ERR_PTR(-EINVAL); |
1da177e4 | 344 | |
1ab1457c | 345 | ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); |
63159f29 | 346 | if (!ndev) |
a317a2f1 | 347 | return ERR_PTR(err); |
322f74a4 IO |
348 | |
349 | rwlock_init(&ndev->lock); | |
350 | ndev->dev = dev; | |
502a2ffd | 351 | INIT_LIST_HEAD(&ndev->addr_list); |
b7b1bfce HFS |
352 | setup_timer(&ndev->rs_timer, addrconf_rs_timer, |
353 | (unsigned long)ndev); | |
c346dca1 | 354 | memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); |
9b29c696 HFS |
355 | |
356 | if (ndev->cnf.stable_secret.initialized) | |
357 | ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
358 | else | |
359 | ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64; | |
360 | ||
322f74a4 | 361 | ndev->cnf.mtu6 = dev->mtu; |
322f74a4 | 362 | ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); |
63159f29 | 363 | if (!ndev->nd_parms) { |
322f74a4 | 364 | kfree(ndev); |
a317a2f1 | 365 | return ERR_PTR(err); |
322f74a4 | 366 | } |
0187bdfb BH |
367 | if (ndev->cnf.forwarding) |
368 | dev_disable_lro(dev); | |
322f74a4 IO |
369 | /* We refer to the device */ |
370 | dev_hold(dev); | |
1da177e4 | 371 | |
322f74a4 | 372 | if (snmp6_alloc_dev(ndev) < 0) { |
ba3542e1 | 373 | ADBG(KERN_WARNING |
f3213831 | 374 | "%s: cannot allocate memory for statistics; dev=%s.\n", |
ba3542e1 | 375 | __func__, dev->name); |
322f74a4 | 376 | neigh_parms_release(&nd_tbl, ndev->nd_parms); |
8603e33d RL |
377 | dev_put(dev); |
378 | kfree(ndev); | |
a317a2f1 | 379 | return ERR_PTR(err); |
322f74a4 | 380 | } |
1da177e4 | 381 | |
322f74a4 | 382 | if (snmp6_register_dev(ndev) < 0) { |
ba3542e1 | 383 | ADBG(KERN_WARNING |
f3213831 | 384 | "%s: cannot create /proc/net/dev_snmp6/%s\n", |
ba3542e1 | 385 | __func__, dev->name); |
a317a2f1 | 386 | goto err_release; |
322f74a4 IO |
387 | } |
388 | ||
389 | /* One reference from device. We must do this before | |
390 | * we invoke __ipv6_regen_rndid(). | |
391 | */ | |
392 | in6_dev_hold(ndev); | |
1da177e4 | 393 | |
1b34be74 YH |
394 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
395 | ndev->cnf.accept_dad = -1; | |
396 | ||
07a93626 | 397 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
b077d7ab | 398 | if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { |
f3213831 | 399 | pr_info("%s: Disabled Multicast RS\n", dev->name); |
b077d7ab YH |
400 | ndev->cnf.rtr_solicits = 0; |
401 | } | |
402 | #endif | |
403 | ||
372e6c8f | 404 | INIT_LIST_HEAD(&ndev->tempaddr_list); |
b24b8a24 | 405 | setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); |
322f74a4 IO |
406 | if ((dev->flags&IFF_LOOPBACK) || |
407 | dev->type == ARPHRD_TUNNEL || | |
9625ed72 | 408 | dev->type == ARPHRD_TUNNEL6 || |
0be669bb | 409 | dev->type == ARPHRD_SIT || |
0be669bb | 410 | dev->type == ARPHRD_NONE) { |
322f74a4 IO |
411 | ndev->cnf.use_tempaddr = -1; |
412 | } else { | |
413 | in6_dev_hold(ndev); | |
414 | ipv6_regen_rndid((unsigned long) ndev); | |
415 | } | |
5d9efa7e | 416 | |
914faa14 | 417 | ndev->token = in6addr_any; |
1da177e4 | 418 | |
f24e3d65 | 419 | if (netif_running(dev) && addrconf_qdisc_ok(dev)) |
53aadcc9 HX |
420 | ndev->if_flags |= IF_READY; |
421 | ||
322f74a4 IO |
422 | ipv6_mc_init_dev(ndev); |
423 | ndev->tstamp = jiffies; | |
a317a2f1 WC |
424 | err = addrconf_sysctl_register(ndev); |
425 | if (err) { | |
426 | ipv6_mc_destroy_dev(ndev); | |
427 | del_timer(&ndev->regen_timer); | |
2a189f9e | 428 | snmp6_unregister_dev(ndev); |
a317a2f1 WC |
429 | goto err_release; |
430 | } | |
30c4cf57 | 431 | /* protected by rtnl_lock */ |
cf778b00 | 432 | rcu_assign_pointer(dev->ip6_ptr, ndev); |
d88ae4cc | 433 | |
2c5e8933 HFS |
434 | /* Join interface-local all-node multicast group */ |
435 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); | |
436 | ||
d88ae4cc | 437 | /* Join all-node multicast group */ |
f3ee4010 | 438 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); |
d88ae4cc | 439 | |
d6ddef9e | 440 | /* Join all-router multicast group if forwarding is set */ |
8b2aaede | 441 | if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) |
d6ddef9e LW |
442 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
443 | ||
1da177e4 | 444 | return ndev; |
a317a2f1 WC |
445 | |
446 | err_release: | |
447 | neigh_parms_release(&nd_tbl, ndev->nd_parms); | |
448 | ndev->dead = 1; | |
449 | in6_dev_finish_destroy(ndev); | |
450 | return ERR_PTR(err); | |
1da177e4 LT |
451 | } |
452 | ||
8e5e8f30 | 453 | static struct inet6_dev *ipv6_find_idev(struct net_device *dev) |
1da177e4 LT |
454 | { |
455 | struct inet6_dev *idev; | |
456 | ||
457 | ASSERT_RTNL(); | |
458 | ||
e21e8467 SH |
459 | idev = __in6_dev_get(dev); |
460 | if (!idev) { | |
461 | idev = ipv6_add_dev(dev); | |
a317a2f1 | 462 | if (IS_ERR(idev)) |
1da177e4 LT |
463 | return NULL; |
464 | } | |
c5e33bdd | 465 | |
1da177e4 LT |
466 | if (dev->flags&IFF_UP) |
467 | ipv6_mc_up(idev); | |
468 | return idev; | |
469 | } | |
470 | ||
f3a1bfb1 ND |
471 | static int inet6_netconf_msgsize_devconf(int type) |
472 | { | |
473 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) | |
474 | + nla_total_size(4); /* NETCONFA_IFINDEX */ | |
136ba622 | 475 | bool all = false; |
f3a1bfb1 | 476 | |
136ba622 ZS |
477 | if (type == NETCONFA_ALL) |
478 | all = true; | |
479 | ||
480 | if (all || type == NETCONFA_FORWARDING) | |
f3a1bfb1 | 481 | size += nla_total_size(4); |
b1afce95 | 482 | #ifdef CONFIG_IPV6_MROUTE |
136ba622 | 483 | if (all || type == NETCONFA_MC_FORWARDING) |
d67b8c61 | 484 | size += nla_total_size(4); |
b1afce95 | 485 | #endif |
136ba622 | 486 | if (all || type == NETCONFA_PROXY_NEIGH) |
c92d5491 | 487 | size += nla_total_size(4); |
f3a1bfb1 | 488 | |
136ba622 | 489 | if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) |
35103d11 AG |
490 | size += nla_total_size(4); |
491 | ||
f3a1bfb1 ND |
492 | return size; |
493 | } | |
494 | ||
495 | static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, | |
496 | struct ipv6_devconf *devconf, u32 portid, | |
497 | u32 seq, int event, unsigned int flags, | |
498 | int type) | |
499 | { | |
500 | struct nlmsghdr *nlh; | |
501 | struct netconfmsg *ncm; | |
136ba622 | 502 | bool all = false; |
f3a1bfb1 ND |
503 | |
504 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), | |
505 | flags); | |
63159f29 | 506 | if (!nlh) |
f3a1bfb1 ND |
507 | return -EMSGSIZE; |
508 | ||
136ba622 ZS |
509 | if (type == NETCONFA_ALL) |
510 | all = true; | |
511 | ||
f3a1bfb1 ND |
512 | ncm = nlmsg_data(nlh); |
513 | ncm->ncm_family = AF_INET6; | |
514 | ||
515 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) | |
516 | goto nla_put_failure; | |
517 | ||
136ba622 | 518 | if ((all || type == NETCONFA_FORWARDING) && |
f3a1bfb1 ND |
519 | nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) |
520 | goto nla_put_failure; | |
b1afce95 | 521 | #ifdef CONFIG_IPV6_MROUTE |
136ba622 | 522 | if ((all || type == NETCONFA_MC_FORWARDING) && |
d67b8c61 ND |
523 | nla_put_s32(skb, NETCONFA_MC_FORWARDING, |
524 | devconf->mc_forwarding) < 0) | |
525 | goto nla_put_failure; | |
b1afce95 | 526 | #endif |
136ba622 | 527 | if ((all || type == NETCONFA_PROXY_NEIGH) && |
c92d5491 | 528 | nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) |
529 | goto nla_put_failure; | |
530 | ||
136ba622 | 531 | if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && |
35103d11 AG |
532 | nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
533 | devconf->ignore_routes_with_linkdown) < 0) | |
534 | goto nla_put_failure; | |
535 | ||
053c095a JB |
536 | nlmsg_end(skb, nlh); |
537 | return 0; | |
f3a1bfb1 ND |
538 | |
539 | nla_put_failure: | |
540 | nlmsg_cancel(skb, nlh); | |
541 | return -EMSGSIZE; | |
542 | } | |
543 | ||
d67b8c61 ND |
544 | void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, |
545 | struct ipv6_devconf *devconf) | |
f3a1bfb1 ND |
546 | { |
547 | struct sk_buff *skb; | |
548 | int err = -ENOBUFS; | |
549 | ||
550 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); | |
63159f29 | 551 | if (!skb) |
f3a1bfb1 ND |
552 | goto errout; |
553 | ||
554 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, | |
555 | RTM_NEWNETCONF, 0, type); | |
556 | if (err < 0) { | |
557 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ | |
558 | WARN_ON(err == -EMSGSIZE); | |
559 | kfree_skb(skb); | |
560 | goto errout; | |
561 | } | |
562 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); | |
563 | return; | |
564 | errout: | |
bd779028 | 565 | rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); |
f3a1bfb1 ND |
566 | } |
567 | ||
76f8f6cb ND |
568 | static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { |
569 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, | |
570 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, | |
c92d5491 | 571 | [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, |
35103d11 | 572 | [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, |
76f8f6cb ND |
573 | }; |
574 | ||
575 | static int inet6_netconf_get_devconf(struct sk_buff *in_skb, | |
661d2967 | 576 | struct nlmsghdr *nlh) |
76f8f6cb ND |
577 | { |
578 | struct net *net = sock_net(in_skb->sk); | |
579 | struct nlattr *tb[NETCONFA_MAX+1]; | |
580 | struct netconfmsg *ncm; | |
581 | struct sk_buff *skb; | |
582 | struct ipv6_devconf *devconf; | |
583 | struct inet6_dev *in6_dev; | |
584 | struct net_device *dev; | |
585 | int ifindex; | |
586 | int err; | |
587 | ||
588 | err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, | |
589 | devconf_ipv6_policy); | |
590 | if (err < 0) | |
591 | goto errout; | |
592 | ||
a97eb33f | 593 | err = -EINVAL; |
76f8f6cb ND |
594 | if (!tb[NETCONFA_IFINDEX]) |
595 | goto errout; | |
596 | ||
597 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); | |
598 | switch (ifindex) { | |
599 | case NETCONFA_IFINDEX_ALL: | |
600 | devconf = net->ipv6.devconf_all; | |
601 | break; | |
602 | case NETCONFA_IFINDEX_DEFAULT: | |
603 | devconf = net->ipv6.devconf_dflt; | |
604 | break; | |
605 | default: | |
606 | dev = __dev_get_by_index(net, ifindex); | |
63159f29 | 607 | if (!dev) |
76f8f6cb ND |
608 | goto errout; |
609 | in6_dev = __in6_dev_get(dev); | |
63159f29 | 610 | if (!in6_dev) |
76f8f6cb ND |
611 | goto errout; |
612 | devconf = &in6_dev->cnf; | |
613 | break; | |
614 | } | |
615 | ||
616 | err = -ENOBUFS; | |
136ba622 | 617 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC); |
63159f29 | 618 | if (!skb) |
76f8f6cb ND |
619 | goto errout; |
620 | ||
621 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, | |
622 | NETLINK_CB(in_skb).portid, | |
623 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, | |
136ba622 | 624 | NETCONFA_ALL); |
76f8f6cb ND |
625 | if (err < 0) { |
626 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ | |
627 | WARN_ON(err == -EMSGSIZE); | |
628 | kfree_skb(skb); | |
629 | goto errout; | |
630 | } | |
631 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); | |
632 | errout: | |
633 | return err; | |
634 | } | |
635 | ||
7a674200 ND |
636 | static int inet6_netconf_dump_devconf(struct sk_buff *skb, |
637 | struct netlink_callback *cb) | |
638 | { | |
639 | struct net *net = sock_net(skb->sk); | |
640 | int h, s_h; | |
641 | int idx, s_idx; | |
642 | struct net_device *dev; | |
643 | struct inet6_dev *idev; | |
644 | struct hlist_head *head; | |
645 | ||
646 | s_h = cb->args[0]; | |
647 | s_idx = idx = cb->args[1]; | |
648 | ||
649 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
650 | idx = 0; | |
651 | head = &net->dev_index_head[h]; | |
652 | rcu_read_lock(); | |
63998ac2 ND |
653 | cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ |
654 | net->dev_base_seq; | |
7a674200 ND |
655 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
656 | if (idx < s_idx) | |
657 | goto cont; | |
658 | idev = __in6_dev_get(dev); | |
659 | if (!idev) | |
660 | goto cont; | |
661 | ||
662 | if (inet6_netconf_fill_devconf(skb, dev->ifindex, | |
663 | &idev->cnf, | |
664 | NETLINK_CB(cb->skb).portid, | |
665 | cb->nlh->nlmsg_seq, | |
666 | RTM_NEWNETCONF, | |
667 | NLM_F_MULTI, | |
136ba622 | 668 | NETCONFA_ALL) < 0) { |
7a674200 ND |
669 | rcu_read_unlock(); |
670 | goto done; | |
671 | } | |
63998ac2 | 672 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
7a674200 ND |
673 | cont: |
674 | idx++; | |
675 | } | |
676 | rcu_read_unlock(); | |
677 | } | |
678 | if (h == NETDEV_HASHENTRIES) { | |
679 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, | |
680 | net->ipv6.devconf_all, | |
681 | NETLINK_CB(cb->skb).portid, | |
682 | cb->nlh->nlmsg_seq, | |
683 | RTM_NEWNETCONF, NLM_F_MULTI, | |
136ba622 | 684 | NETCONFA_ALL) < 0) |
7a674200 ND |
685 | goto done; |
686 | else | |
687 | h++; | |
688 | } | |
689 | if (h == NETDEV_HASHENTRIES + 1) { | |
690 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, | |
691 | net->ipv6.devconf_dflt, | |
692 | NETLINK_CB(cb->skb).portid, | |
693 | cb->nlh->nlmsg_seq, | |
694 | RTM_NEWNETCONF, NLM_F_MULTI, | |
136ba622 | 695 | NETCONFA_ALL) < 0) |
7a674200 ND |
696 | goto done; |
697 | else | |
698 | h++; | |
699 | } | |
700 | done: | |
701 | cb->args[0] = h; | |
702 | cb->args[1] = idx; | |
703 | ||
704 | return skb->len; | |
705 | } | |
706 | ||
1da177e4 LT |
707 | #ifdef CONFIG_SYSCTL |
708 | static void dev_forward_change(struct inet6_dev *idev) | |
709 | { | |
710 | struct net_device *dev; | |
711 | struct inet6_ifaddr *ifa; | |
1da177e4 LT |
712 | |
713 | if (!idev) | |
714 | return; | |
715 | dev = idev->dev; | |
0187bdfb BH |
716 | if (idev->cnf.forwarding) |
717 | dev_disable_lro(dev); | |
1a940835 | 718 | if (dev->flags & IFF_MULTICAST) { |
2c5e8933 | 719 | if (idev->cnf.forwarding) { |
f3ee4010 | 720 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
2c5e8933 HFS |
721 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); |
722 | ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); | |
723 | } else { | |
f3ee4010 | 724 | ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); |
2c5e8933 HFS |
725 | ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); |
726 | ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); | |
727 | } | |
1da177e4 | 728 | } |
502a2ffd | 729 | |
730 | list_for_each_entry(ifa, &idev->addr_list, if_list) { | |
2c12a74c MW |
731 | if (ifa->flags&IFA_F_TENTATIVE) |
732 | continue; | |
1da177e4 LT |
733 | if (idev->cnf.forwarding) |
734 | addrconf_join_anycast(ifa); | |
735 | else | |
736 | addrconf_leave_anycast(ifa); | |
737 | } | |
f3a1bfb1 ND |
738 | inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, |
739 | dev->ifindex, &idev->cnf); | |
1da177e4 LT |
740 | } |
741 | ||
742 | ||
e186932b | 743 | static void addrconf_forward_change(struct net *net, __s32 newf) |
1da177e4 LT |
744 | { |
745 | struct net_device *dev; | |
746 | struct inet6_dev *idev; | |
747 | ||
4acd4945 | 748 | for_each_netdev(net, dev) { |
1da177e4 LT |
749 | idev = __in6_dev_get(dev); |
750 | if (idev) { | |
e186932b PE |
751 | int changed = (!idev->cnf.forwarding) ^ (!newf); |
752 | idev->cnf.forwarding = newf; | |
1da177e4 LT |
753 | if (changed) |
754 | dev_forward_change(idev); | |
755 | } | |
1da177e4 | 756 | } |
1da177e4 | 757 | } |
c8fecf22 | 758 | |
013d97e9 | 759 | static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) |
c8fecf22 | 760 | { |
bff16c2f | 761 | struct net *net; |
013d97e9 FR |
762 | int old; |
763 | ||
764 | if (!rtnl_trylock()) | |
765 | return restart_syscall(); | |
bff16c2f PE |
766 | |
767 | net = (struct net *)table->extra2; | |
013d97e9 FR |
768 | old = *p; |
769 | *p = newf; | |
b325fddb | 770 | |
013d97e9 | 771 | if (p == &net->ipv6.devconf_dflt->forwarding) { |
f3a1bfb1 ND |
772 | if ((!newf) ^ (!old)) |
773 | inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, | |
774 | NETCONFA_IFINDEX_DEFAULT, | |
775 | net->ipv6.devconf_dflt); | |
013d97e9 FR |
776 | rtnl_unlock(); |
777 | return 0; | |
88af182e | 778 | } |
c8fecf22 | 779 | |
e186932b | 780 | if (p == &net->ipv6.devconf_all->forwarding) { |
e186932b PE |
781 | net->ipv6.devconf_dflt->forwarding = newf; |
782 | addrconf_forward_change(net, newf); | |
f3a1bfb1 ND |
783 | if ((!newf) ^ (!old)) |
784 | inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, | |
785 | NETCONFA_IFINDEX_ALL, | |
786 | net->ipv6.devconf_all); | |
013d97e9 | 787 | } else if ((!newf) ^ (!old)) |
c8fecf22 | 788 | dev_forward_change((struct inet6_dev *)table->extra1); |
0187bdfb | 789 | rtnl_unlock(); |
c8fecf22 | 790 | |
013d97e9 | 791 | if (newf) |
7b4da532 | 792 | rt6_purge_dflt_routers(net); |
b325fddb | 793 | return 1; |
c8fecf22 | 794 | } |
35103d11 AG |
795 | |
796 | static void addrconf_linkdown_change(struct net *net, __s32 newf) | |
797 | { | |
798 | struct net_device *dev; | |
799 | struct inet6_dev *idev; | |
800 | ||
801 | for_each_netdev(net, dev) { | |
802 | idev = __in6_dev_get(dev); | |
803 | if (idev) { | |
804 | int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); | |
805 | ||
806 | idev->cnf.ignore_routes_with_linkdown = newf; | |
807 | if (changed) | |
808 | inet6_netconf_notify_devconf(dev_net(dev), | |
809 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, | |
810 | dev->ifindex, | |
811 | &idev->cnf); | |
812 | } | |
813 | } | |
814 | } | |
815 | ||
816 | static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) | |
817 | { | |
818 | struct net *net; | |
819 | int old; | |
820 | ||
821 | if (!rtnl_trylock()) | |
822 | return restart_syscall(); | |
823 | ||
824 | net = (struct net *)table->extra2; | |
825 | old = *p; | |
826 | *p = newf; | |
827 | ||
828 | if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { | |
829 | if ((!newf) ^ (!old)) | |
830 | inet6_netconf_notify_devconf(net, | |
831 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, | |
832 | NETCONFA_IFINDEX_DEFAULT, | |
833 | net->ipv6.devconf_dflt); | |
834 | rtnl_unlock(); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { | |
839 | net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; | |
840 | addrconf_linkdown_change(net, newf); | |
841 | if ((!newf) ^ (!old)) | |
842 | inet6_netconf_notify_devconf(net, | |
843 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, | |
844 | NETCONFA_IFINDEX_ALL, | |
845 | net->ipv6.devconf_all); | |
846 | } | |
847 | rtnl_unlock(); | |
848 | ||
849 | return 1; | |
850 | } | |
851 | ||
1da177e4 LT |
852 | #endif |
853 | ||
5c578aed | 854 | /* Nobody refers to this ifaddr, destroy it */ |
1da177e4 LT |
855 | void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) |
856 | { | |
c2e21293 | 857 | WARN_ON(!hlist_unhashed(&ifp->addr_lst)); |
547b792c | 858 | |
1da177e4 | 859 | #ifdef NET_REFCNT_DEBUG |
91df42be | 860 | pr_debug("%s\n", __func__); |
1da177e4 LT |
861 | #endif |
862 | ||
863 | in6_dev_put(ifp->idev); | |
864 | ||
c15b1cca HFS |
865 | if (cancel_delayed_work(&ifp->dad_work)) |
866 | pr_notice("delayed DAD work was pending while freeing ifa=%p\n", | |
867 | ifp); | |
1da177e4 | 868 | |
e9d3e084 | 869 | if (ifp->state != INET6_IFADDR_STATE_DEAD) { |
f3213831 | 870 | pr_warn("Freeing alive inet6 address %p\n", ifp); |
1da177e4 LT |
871 | return; |
872 | } | |
94e187c0 | 873 | ip6_rt_put(ifp->rt); |
1da177e4 | 874 | |
e5785985 | 875 | kfree_rcu(ifp, rcu); |
1da177e4 LT |
876 | } |
877 | ||
e55ffac6 BH |
878 | static void |
879 | ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) | |
880 | { | |
502a2ffd | 881 | struct list_head *p; |
8a6ce0c0 | 882 | int ifp_scope = ipv6_addr_src_scope(&ifp->addr); |
e55ffac6 BH |
883 | |
884 | /* | |
885 | * Each device address list is sorted in order of scope - | |
886 | * global before linklocal. | |
887 | */ | |
502a2ffd | 888 | list_for_each(p, &idev->addr_list) { |
889 | struct inet6_ifaddr *ifa | |
890 | = list_entry(p, struct inet6_ifaddr, if_list); | |
8a6ce0c0 | 891 | if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) |
e55ffac6 BH |
892 | break; |
893 | } | |
894 | ||
b54c9b98 | 895 | list_add_tail(&ifp->if_list, p); |
e55ffac6 BH |
896 | } |
897 | ||
ddbe5032 | 898 | static u32 inet6_addr_hash(const struct in6_addr *addr) |
3eb84f49 | 899 | { |
ddbe5032 | 900 | return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); |
3eb84f49 YH |
901 | } |
902 | ||
1da177e4 LT |
903 | /* On success it returns ifp with increased reference count */ |
904 | ||
905 | static struct inet6_ifaddr * | |
3f8f5298 JP |
906 | ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, |
907 | const struct in6_addr *peer_addr, int pfxlen, | |
8a226b2c | 908 | int scope, u32 flags, u32 valid_lft, u32 prefered_lft) |
1da177e4 LT |
909 | { |
910 | struct inet6_ifaddr *ifa = NULL; | |
911 | struct rt6_info *rt; | |
3a88a81d | 912 | unsigned int hash; |
1da177e4 | 913 | int err = 0; |
d68b8270 YH |
914 | int addr_type = ipv6_addr_type(addr); |
915 | ||
916 | if (addr_type == IPV6_ADDR_ANY || | |
917 | addr_type & IPV6_ADDR_MULTICAST || | |
918 | (!(idev->dev->flags & IFF_LOOPBACK) && | |
919 | addr_type & IPV6_ADDR_LOOPBACK)) | |
920 | return ERR_PTR(-EADDRNOTAVAIL); | |
1da177e4 | 921 | |
8814c4b5 | 922 | rcu_read_lock_bh(); |
1da177e4 LT |
923 | if (idev->dead) { |
924 | err = -ENODEV; /*XXX*/ | |
925 | goto out2; | |
926 | } | |
927 | ||
56d417b1 | 928 | if (idev->cnf.disable_ipv6) { |
9bdd8d40 BH |
929 | err = -EACCES; |
930 | goto out2; | |
931 | } | |
932 | ||
5c578aed | 933 | spin_lock(&addrconf_hash_lock); |
1da177e4 LT |
934 | |
935 | /* Ignore adding duplicate addresses on an interface */ | |
c346dca1 | 936 | if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { |
ba3542e1 | 937 | ADBG("ipv6_add_addr: already assigned\n"); |
1da177e4 LT |
938 | err = -EEXIST; |
939 | goto out; | |
940 | } | |
941 | ||
322f74a4 | 942 | ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); |
1da177e4 | 943 | |
63159f29 | 944 | if (!ifa) { |
ba3542e1 | 945 | ADBG("ipv6_add_addr: malloc failed\n"); |
1da177e4 LT |
946 | err = -ENOBUFS; |
947 | goto out; | |
948 | } | |
949 | ||
8f031519 | 950 | rt = addrconf_dst_alloc(idev, addr, false); |
1da177e4 LT |
951 | if (IS_ERR(rt)) { |
952 | err = PTR_ERR(rt); | |
953 | goto out; | |
954 | } | |
955 | ||
bba24896 JP |
956 | neigh_parms_data_state_setall(idev->nd_parms); |
957 | ||
4e3fd7a0 | 958 | ifa->addr = *addr; |
3f8f5298 JP |
959 | if (peer_addr) |
960 | ifa->peer_addr = *peer_addr; | |
1da177e4 LT |
961 | |
962 | spin_lock_init(&ifa->lock); | |
c15b1cca | 963 | INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); |
c2e21293 | 964 | INIT_HLIST_NODE(&ifa->addr_lst); |
1da177e4 LT |
965 | ifa->scope = scope; |
966 | ifa->prefix_len = pfxlen; | |
967 | ifa->flags = flags | IFA_F_TENTATIVE; | |
8a226b2c JB |
968 | ifa->valid_lft = valid_lft; |
969 | ifa->prefered_lft = prefered_lft; | |
1da177e4 | 970 | ifa->cstamp = ifa->tstamp = jiffies; |
617fe29d | 971 | ifa->tokenized = false; |
1da177e4 | 972 | |
57f5f544 KF |
973 | ifa->rt = rt; |
974 | ||
1da177e4 LT |
975 | ifa->idev = idev; |
976 | in6_dev_hold(idev); | |
977 | /* For caller */ | |
978 | in6_ifa_hold(ifa); | |
979 | ||
980 | /* Add to big hash table */ | |
ddbe5032 | 981 | hash = inet6_addr_hash(addr); |
1da177e4 | 982 | |
5c578aed | 983 | hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); |
5c578aed | 984 | spin_unlock(&addrconf_hash_lock); |
1da177e4 LT |
985 | |
986 | write_lock(&idev->lock); | |
987 | /* Add to inet6_dev unicast addr list. */ | |
e55ffac6 | 988 | ipv6_link_dev_addr(idev, ifa); |
1da177e4 | 989 | |
1da177e4 | 990 | if (ifa->flags&IFA_F_TEMPORARY) { |
372e6c8f | 991 | list_add(&ifa->tmp_list, &idev->tempaddr_list); |
1da177e4 LT |
992 | in6_ifa_hold(ifa); |
993 | } | |
1da177e4 | 994 | |
1da177e4 LT |
995 | in6_ifa_hold(ifa); |
996 | write_unlock(&idev->lock); | |
997 | out2: | |
8814c4b5 | 998 | rcu_read_unlock_bh(); |
1da177e4 | 999 | |
fd92833a | 1000 | if (likely(err == 0)) |
f88c91dd | 1001 | inet6addr_notifier_call_chain(NETDEV_UP, ifa); |
1da177e4 LT |
1002 | else { |
1003 | kfree(ifa); | |
1004 | ifa = ERR_PTR(err); | |
1005 | } | |
1006 | ||
1007 | return ifa; | |
1008 | out: | |
5c578aed | 1009 | spin_unlock(&addrconf_hash_lock); |
1da177e4 LT |
1010 | goto out2; |
1011 | } | |
1012 | ||
5b84efec TH |
1013 | enum cleanup_prefix_rt_t { |
1014 | CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ | |
1015 | CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ | |
1016 | CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ | |
1017 | }; | |
1018 | ||
1019 | /* | |
1020 | * Check, whether the prefix for ifp would still need a prefix route | |
1021 | * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* | |
1022 | * constants. | |
1023 | * | |
1024 | * 1) we don't purge prefix if address was not permanent. | |
1025 | * prefix is managed by its own lifetime. | |
1026 | * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. | |
1027 | * 3) if there are no addresses, delete prefix. | |
1028 | * 4) if there are still other permanent address(es), | |
1029 | * corresponding prefix is still permanent. | |
1030 | * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, | |
1031 | * don't purge the prefix, assume user space is managing it. | |
1032 | * 6) otherwise, update prefix lifetime to the | |
1033 | * longest valid lifetime among the corresponding | |
1034 | * addresses on the device. | |
1035 | * Note: subsequent RA will update lifetime. | |
1036 | **/ | |
1037 | static enum cleanup_prefix_rt_t | |
1038 | check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) | |
1039 | { | |
1040 | struct inet6_ifaddr *ifa; | |
1041 | struct inet6_dev *idev = ifp->idev; | |
1042 | unsigned long lifetime; | |
1043 | enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; | |
1044 | ||
1045 | *expires = jiffies; | |
1046 | ||
1047 | list_for_each_entry(ifa, &idev->addr_list, if_list) { | |
1048 | if (ifa == ifp) | |
1049 | continue; | |
1050 | if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, | |
1051 | ifp->prefix_len)) | |
1052 | continue; | |
1053 | if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) | |
1054 | return CLEANUP_PREFIX_RT_NOP; | |
1055 | ||
1056 | action = CLEANUP_PREFIX_RT_EXPIRE; | |
1057 | ||
1058 | spin_lock(&ifa->lock); | |
1059 | ||
1060 | lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); | |
1061 | /* | |
1062 | * Note: Because this address is | |
1063 | * not permanent, lifetime < | |
1064 | * LONG_MAX / HZ here. | |
1065 | */ | |
1066 | if (time_before(*expires, ifa->tstamp + lifetime * HZ)) | |
1067 | *expires = ifa->tstamp + lifetime * HZ; | |
1068 | spin_unlock(&ifa->lock); | |
1069 | } | |
1070 | ||
1071 | return action; | |
1072 | } | |
1073 | ||
1074 | static void | |
1075 | cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) | |
1076 | { | |
1077 | struct rt6_info *rt; | |
1078 | ||
1079 | rt = addrconf_get_prefix_route(&ifp->addr, | |
1080 | ifp->prefix_len, | |
1081 | ifp->idev->dev, | |
1082 | 0, RTF_GATEWAY | RTF_DEFAULT); | |
1083 | if (rt) { | |
1084 | if (del_rt) | |
1085 | ip6_del_rt(rt); | |
1086 | else { | |
1087 | if (!(rt->rt6i_flags & RTF_EXPIRES)) | |
1088 | rt6_set_expires(rt, expires); | |
1089 | ip6_rt_put(rt); | |
1090 | } | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | ||
1da177e4 LT |
1095 | /* This function wants to get referenced ifp and releases it before return */ |
1096 | ||
1097 | static void ipv6_del_addr(struct inet6_ifaddr *ifp) | |
1098 | { | |
4c5ff6a6 | 1099 | int state; |
5b84efec TH |
1100 | enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; |
1101 | unsigned long expires; | |
1da177e4 | 1102 | |
c15b1cca HFS |
1103 | ASSERT_RTNL(); |
1104 | ||
8e8e676d | 1105 | spin_lock_bh(&ifp->lock); |
4c5ff6a6 | 1106 | state = ifp->state; |
e9d3e084 | 1107 | ifp->state = INET6_IFADDR_STATE_DEAD; |
8e8e676d | 1108 | spin_unlock_bh(&ifp->lock); |
4c5ff6a6 HX |
1109 | |
1110 | if (state == INET6_IFADDR_STATE_DEAD) | |
1111 | goto out; | |
1da177e4 | 1112 | |
5c578aed | 1113 | spin_lock_bh(&addrconf_hash_lock); |
1114 | hlist_del_init_rcu(&ifp->addr_lst); | |
5c578aed | 1115 | spin_unlock_bh(&addrconf_hash_lock); |
1da177e4 | 1116 | |
5b84efec | 1117 | write_lock_bh(&ifp->idev->lock); |
5d9efa7e | 1118 | |
1da177e4 | 1119 | if (ifp->flags&IFA_F_TEMPORARY) { |
372e6c8f | 1120 | list_del(&ifp->tmp_list); |
1121 | if (ifp->ifpub) { | |
1122 | in6_ifa_put(ifp->ifpub); | |
1123 | ifp->ifpub = NULL; | |
1da177e4 | 1124 | } |
372e6c8f | 1125 | __in6_ifa_put(ifp); |
1da177e4 | 1126 | } |
1da177e4 | 1127 | |
5b84efec TH |
1128 | if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) |
1129 | action = check_cleanup_prefix_route(ifp, &expires); | |
502a2ffd | 1130 | |
5b84efec TH |
1131 | list_del_init(&ifp->if_list); |
1132 | __in6_ifa_put(ifp); | |
1133 | ||
1134 | write_unlock_bh(&ifp->idev->lock); | |
1da177e4 | 1135 | |
c15b1cca | 1136 | addrconf_del_dad_work(ifp); |
b2238566 | 1137 | |
1da177e4 LT |
1138 | ipv6_ifa_notify(RTM_DELADDR, ifp); |
1139 | ||
f88c91dd | 1140 | inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); |
1da177e4 | 1141 | |
5b84efec TH |
1142 | if (action != CLEANUP_PREFIX_RT_NOP) { |
1143 | cleanup_prefix_route(ifp, expires, | |
1144 | action == CLEANUP_PREFIX_RT_DEL); | |
1da177e4 LT |
1145 | } |
1146 | ||
c3968a85 DW |
1147 | /* clean up prefsrc entries */ |
1148 | rt6_remove_prefsrc(ifp); | |
4c5ff6a6 | 1149 | out: |
1da177e4 LT |
1150 | in6_ifa_put(ifp); |
1151 | } | |
1152 | ||
1da177e4 LT |
1153 | static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) |
1154 | { | |
1155 | struct inet6_dev *idev = ifp->idev; | |
1156 | struct in6_addr addr, *tmpaddr; | |
76f793e3 | 1157 | unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; |
eac55bf9 | 1158 | unsigned long regen_advance; |
1da177e4 LT |
1159 | int tmp_plen; |
1160 | int ret = 0; | |
95c385b4 | 1161 | u32 addr_flags; |
76f793e3 | 1162 | unsigned long now = jiffies; |
1da177e4 | 1163 | |
53bd6749 | 1164 | write_lock_bh(&idev->lock); |
1da177e4 LT |
1165 | if (ift) { |
1166 | spin_lock_bh(&ift->lock); | |
1167 | memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); | |
1168 | spin_unlock_bh(&ift->lock); | |
1169 | tmpaddr = &addr; | |
1170 | } else { | |
1171 | tmpaddr = NULL; | |
1172 | } | |
1173 | retry: | |
1174 | in6_dev_hold(idev); | |
1175 | if (idev->cnf.use_tempaddr <= 0) { | |
53bd6749 | 1176 | write_unlock_bh(&idev->lock); |
f3213831 | 1177 | pr_info("%s: use_tempaddr is disabled\n", __func__); |
1da177e4 LT |
1178 | in6_dev_put(idev); |
1179 | ret = -1; | |
1180 | goto out; | |
1181 | } | |
1182 | spin_lock_bh(&ifp->lock); | |
1183 | if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { | |
1184 | idev->cnf.use_tempaddr = -1; /*XXX*/ | |
1185 | spin_unlock_bh(&ifp->lock); | |
53bd6749 | 1186 | write_unlock_bh(&idev->lock); |
f3213831 JP |
1187 | pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", |
1188 | __func__); | |
1da177e4 LT |
1189 | in6_dev_put(idev); |
1190 | ret = -1; | |
1191 | goto out; | |
1192 | } | |
1193 | in6_ifa_hold(ifp); | |
1194 | memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); | |
eb7e0575 | 1195 | __ipv6_try_regen_rndid(idev, tmpaddr); |
1da177e4 | 1196 | memcpy(&addr.s6_addr[8], idev->rndid, 8); |
76f793e3 | 1197 | age = (now - ifp->tstamp) / HZ; |
1da177e4 LT |
1198 | tmp_valid_lft = min_t(__u32, |
1199 | ifp->valid_lft, | |
7a876b0e | 1200 | idev->cnf.temp_valid_lft + age); |
1ab1457c YH |
1201 | tmp_prefered_lft = min_t(__u32, |
1202 | ifp->prefered_lft, | |
7a876b0e | 1203 | idev->cnf.temp_prefered_lft + age - |
784e2710 | 1204 | idev->cnf.max_desync_factor); |
1da177e4 | 1205 | tmp_plen = ifp->prefix_len; |
1da177e4 LT |
1206 | tmp_tstamp = ifp->tstamp; |
1207 | spin_unlock_bh(&ifp->lock); | |
1208 | ||
eac55bf9 | 1209 | regen_advance = idev->cnf.regen_max_retry * |
67ba4152 IM |
1210 | idev->cnf.dad_transmits * |
1211 | NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; | |
53bd6749 | 1212 | write_unlock_bh(&idev->lock); |
95c385b4 | 1213 | |
eac55bf9 BB |
1214 | /* A temporary address is created only if this calculated Preferred |
1215 | * Lifetime is greater than REGEN_ADVANCE time units. In particular, | |
1216 | * an implementation must not create a temporary address with a zero | |
1217 | * Preferred Lifetime. | |
ecab6701 HK |
1218 | * Use age calculation as in addrconf_verify to avoid unnecessary |
1219 | * temporary addresses being generated. | |
eac55bf9 | 1220 | */ |
ecab6701 HK |
1221 | age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
1222 | if (tmp_prefered_lft <= regen_advance + age) { | |
eac55bf9 BB |
1223 | in6_ifa_put(ifp); |
1224 | in6_dev_put(idev); | |
1225 | ret = -1; | |
1226 | goto out; | |
1227 | } | |
1228 | ||
95c385b4 NH |
1229 | addr_flags = IFA_F_TEMPORARY; |
1230 | /* set in addrconf_prefix_rcv() */ | |
1231 | if (ifp->flags & IFA_F_OPTIMISTIC) | |
1232 | addr_flags |= IFA_F_OPTIMISTIC; | |
1233 | ||
4b08a8f1 HFS |
1234 | ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, |
1235 | ipv6_addr_scope(&addr), addr_flags, | |
1236 | tmp_valid_lft, tmp_prefered_lft); | |
1237 | if (IS_ERR(ift)) { | |
1da177e4 LT |
1238 | in6_ifa_put(ifp); |
1239 | in6_dev_put(idev); | |
f3213831 | 1240 | pr_info("%s: retry temporary address regeneration\n", __func__); |
1da177e4 | 1241 | tmpaddr = &addr; |
53bd6749 | 1242 | write_lock_bh(&idev->lock); |
1da177e4 LT |
1243 | goto retry; |
1244 | } | |
1245 | ||
1246 | spin_lock_bh(&ift->lock); | |
1247 | ift->ifpub = ifp; | |
76f793e3 | 1248 | ift->cstamp = now; |
1da177e4 LT |
1249 | ift->tstamp = tmp_tstamp; |
1250 | spin_unlock_bh(&ift->lock); | |
1251 | ||
cf22f9a2 | 1252 | addrconf_dad_start(ift); |
1da177e4 LT |
1253 | in6_ifa_put(ift); |
1254 | in6_dev_put(idev); | |
1255 | out: | |
1256 | return ret; | |
1257 | } | |
1da177e4 LT |
1258 | |
1259 | /* | |
072047e4 | 1260 | * Choose an appropriate source address (RFC3484) |
1da177e4 | 1261 | */ |
a9b05723 YH |
1262 | enum { |
1263 | IPV6_SADDR_RULE_INIT = 0, | |
1264 | IPV6_SADDR_RULE_LOCAL, | |
1265 | IPV6_SADDR_RULE_SCOPE, | |
1266 | IPV6_SADDR_RULE_PREFERRED, | |
1267 | #ifdef CONFIG_IPV6_MIP6 | |
1268 | IPV6_SADDR_RULE_HOA, | |
1269 | #endif | |
1270 | IPV6_SADDR_RULE_OIF, | |
1271 | IPV6_SADDR_RULE_LABEL, | |
a9b05723 | 1272 | IPV6_SADDR_RULE_PRIVACY, |
a9b05723 YH |
1273 | IPV6_SADDR_RULE_ORCHID, |
1274 | IPV6_SADDR_RULE_PREFIX, | |
7fd2561e EK |
1275 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
1276 | IPV6_SADDR_RULE_NOT_OPTIMISTIC, | |
1277 | #endif | |
a9b05723 YH |
1278 | IPV6_SADDR_RULE_MAX |
1279 | }; | |
1280 | ||
072047e4 | 1281 | struct ipv6_saddr_score { |
a9b05723 YH |
1282 | int rule; |
1283 | int addr_type; | |
1284 | struct inet6_ifaddr *ifa; | |
1285 | DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); | |
1286 | int scopedist; | |
1287 | int matchlen; | |
072047e4 YH |
1288 | }; |
1289 | ||
a9b05723 | 1290 | struct ipv6_saddr_dst { |
9acd9f3a | 1291 | const struct in6_addr *addr; |
a9b05723 YH |
1292 | int ifindex; |
1293 | int scope; | |
1294 | int label; | |
7cbca67c | 1295 | unsigned int prefs; |
a9b05723 | 1296 | }; |
072047e4 | 1297 | |
b6f99a21 | 1298 | static inline int ipv6_saddr_preferred(int type) |
1da177e4 | 1299 | { |
45bb0060 | 1300 | if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) |
072047e4 YH |
1301 | return 1; |
1302 | return 0; | |
1da177e4 LT |
1303 | } |
1304 | ||
7fd2561e EK |
1305 | static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) |
1306 | { | |
1307 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD | |
1308 | return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; | |
1309 | #else | |
1310 | return false; | |
1311 | #endif | |
1312 | } | |
1313 | ||
3de23255 BT |
1314 | static int ipv6_get_saddr_eval(struct net *net, |
1315 | struct ipv6_saddr_score *score, | |
a9b05723 YH |
1316 | struct ipv6_saddr_dst *dst, |
1317 | int i) | |
1318 | { | |
1319 | int ret; | |
1320 | ||
1321 | if (i <= score->rule) { | |
1322 | switch (i) { | |
1323 | case IPV6_SADDR_RULE_SCOPE: | |
1324 | ret = score->scopedist; | |
1325 | break; | |
1326 | case IPV6_SADDR_RULE_PREFIX: | |
1327 | ret = score->matchlen; | |
1328 | break; | |
1329 | default: | |
1330 | ret = !!test_bit(i, score->scorebits); | |
1331 | } | |
1332 | goto out; | |
1333 | } | |
1334 | ||
1335 | switch (i) { | |
1336 | case IPV6_SADDR_RULE_INIT: | |
1337 | /* Rule 0: remember if hiscore is not ready yet */ | |
1338 | ret = !!score->ifa; | |
1339 | break; | |
1340 | case IPV6_SADDR_RULE_LOCAL: | |
1341 | /* Rule 1: Prefer same address */ | |
1342 | ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); | |
1343 | break; | |
1344 | case IPV6_SADDR_RULE_SCOPE: | |
1345 | /* Rule 2: Prefer appropriate scope | |
1346 | * | |
1347 | * ret | |
1348 | * ^ | |
1349 | * -1 | d 15 | |
1350 | * ---+--+-+---> scope | |
1351 | * | | |
1352 | * | d is scope of the destination. | |
1353 | * B-d | \ | |
1354 | * | \ <- smaller scope is better if | |
db9c7c39 | 1355 | * B-15 | \ if scope is enough for destination. |
a9b05723 YH |
1356 | * | ret = B - scope (-1 <= scope >= d <= 15). |
1357 | * d-C-1 | / | |
1358 | * |/ <- greater is better | |
1359 | * -C / if scope is not enough for destination. | |
1360 | * /| ret = scope - C (-1 <= d < scope <= 15). | |
1361 | * | |
1362 | * d - C - 1 < B -15 (for all -1 <= d <= 15). | |
1363 | * C > d + 14 - B >= 15 + 14 - B = 29 - B. | |
1364 | * Assume B = 0 and we get C > 29. | |
1365 | */ | |
1366 | ret = __ipv6_addr_src_scope(score->addr_type); | |
1367 | if (ret >= dst->scope) | |
1368 | ret = -ret; | |
1369 | else | |
1370 | ret -= 128; /* 30 is enough */ | |
1371 | score->scopedist = ret; | |
1372 | break; | |
1373 | case IPV6_SADDR_RULE_PREFERRED: | |
7fd2561e | 1374 | { |
a9b05723 | 1375 | /* Rule 3: Avoid deprecated and optimistic addresses */ |
7fd2561e EK |
1376 | u8 avoid = IFA_F_DEPRECATED; |
1377 | ||
1378 | if (!ipv6_use_optimistic_addr(score->ifa->idev)) | |
1379 | avoid |= IFA_F_OPTIMISTIC; | |
a9b05723 | 1380 | ret = ipv6_saddr_preferred(score->addr_type) || |
7fd2561e | 1381 | !(score->ifa->flags & avoid); |
a9b05723 | 1382 | break; |
7fd2561e | 1383 | } |
a9b05723 YH |
1384 | #ifdef CONFIG_IPV6_MIP6 |
1385 | case IPV6_SADDR_RULE_HOA: | |
7cbca67c | 1386 | { |
a9b05723 | 1387 | /* Rule 4: Prefer home address */ |
7cbca67c YH |
1388 | int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); |
1389 | ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; | |
a9b05723 | 1390 | break; |
7cbca67c | 1391 | } |
a9b05723 YH |
1392 | #endif |
1393 | case IPV6_SADDR_RULE_OIF: | |
1394 | /* Rule 5: Prefer outgoing interface */ | |
1395 | ret = (!dst->ifindex || | |
1396 | dst->ifindex == score->ifa->idev->dev->ifindex); | |
1397 | break; | |
1398 | case IPV6_SADDR_RULE_LABEL: | |
1399 | /* Rule 6: Prefer matching label */ | |
3de23255 BT |
1400 | ret = ipv6_addr_label(net, |
1401 | &score->ifa->addr, score->addr_type, | |
a9b05723 YH |
1402 | score->ifa->idev->dev->ifindex) == dst->label; |
1403 | break; | |
a9b05723 | 1404 | case IPV6_SADDR_RULE_PRIVACY: |
7cbca67c | 1405 | { |
a9b05723 | 1406 | /* Rule 7: Prefer public address |
25985edc | 1407 | * Note: prefer temporary address if use_tempaddr >= 2 |
a9b05723 | 1408 | */ |
7cbca67c YH |
1409 | int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? |
1410 | !!(dst->prefs & IPV6_PREFER_SRC_TMP) : | |
1411 | score->ifa->idev->cnf.use_tempaddr >= 2; | |
1412 | ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; | |
a9b05723 | 1413 | break; |
7cbca67c | 1414 | } |
a9b05723 YH |
1415 | case IPV6_SADDR_RULE_ORCHID: |
1416 | /* Rule 8-: Prefer ORCHID vs ORCHID or | |
1417 | * non-ORCHID vs non-ORCHID | |
1418 | */ | |
1419 | ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ | |
1420 | ipv6_addr_orchid(dst->addr)); | |
1421 | break; | |
1422 | case IPV6_SADDR_RULE_PREFIX: | |
1423 | /* Rule 8: Use longest matching prefix */ | |
91b4b04f YH |
1424 | ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); |
1425 | if (ret > score->ifa->prefix_len) | |
1426 | ret = score->ifa->prefix_len; | |
1427 | score->matchlen = ret; | |
a9b05723 | 1428 | break; |
7fd2561e EK |
1429 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
1430 | case IPV6_SADDR_RULE_NOT_OPTIMISTIC: | |
1431 | /* Optimistic addresses still have lower precedence than other | |
1432 | * preferred addresses. | |
1433 | */ | |
1434 | ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); | |
1435 | break; | |
1436 | #endif | |
a9b05723 YH |
1437 | default: |
1438 | ret = 0; | |
1439 | } | |
1440 | ||
1441 | if (ret) | |
1442 | __set_bit(i, score->scorebits); | |
1443 | score->rule = i; | |
1444 | out: | |
1445 | return ret; | |
1446 | } | |
1447 | ||
c0b8da1e YH |
1448 | static int __ipv6_dev_get_saddr(struct net *net, |
1449 | struct ipv6_saddr_dst *dst, | |
c0b8da1e YH |
1450 | struct inet6_dev *idev, |
1451 | struct ipv6_saddr_score *scores, | |
1452 | int hiscore_idx) | |
9131f3de | 1453 | { |
c0b8da1e | 1454 | struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; |
9131f3de YH |
1455 | |
1456 | read_lock_bh(&idev->lock); | |
1457 | list_for_each_entry(score->ifa, &idev->addr_list, if_list) { | |
1458 | int i; | |
1459 | ||
1460 | /* | |
1461 | * - Tentative Address (RFC2462 section 5.4) | |
1462 | * - A tentative address is not considered | |
1463 | * "assigned to an interface" in the traditional | |
1464 | * sense, unless it is also flagged as optimistic. | |
1465 | * - Candidate Source Address (section 4) | |
1466 | * - In any case, anycast addresses, multicast | |
1467 | * addresses, and the unspecified address MUST | |
1468 | * NOT be included in a candidate set. | |
1469 | */ | |
1470 | if ((score->ifa->flags & IFA_F_TENTATIVE) && | |
1471 | (!(score->ifa->flags & IFA_F_OPTIMISTIC))) | |
1472 | continue; | |
1473 | ||
1474 | score->addr_type = __ipv6_addr_type(&score->ifa->addr); | |
1475 | ||
1476 | if (unlikely(score->addr_type == IPV6_ADDR_ANY || | |
1477 | score->addr_type & IPV6_ADDR_MULTICAST)) { | |
1478 | net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", | |
1479 | idev->dev->name); | |
1480 | continue; | |
1481 | } | |
1482 | ||
1483 | score->rule = -1; | |
1484 | bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); | |
1485 | ||
1486 | for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { | |
1487 | int minihiscore, miniscore; | |
1488 | ||
1489 | minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); | |
1490 | miniscore = ipv6_get_saddr_eval(net, score, dst, i); | |
1491 | ||
1492 | if (minihiscore > miniscore) { | |
1493 | if (i == IPV6_SADDR_RULE_SCOPE && | |
1494 | score->scopedist > 0) { | |
1495 | /* | |
1496 | * special case: | |
1497 | * each remaining entry | |
1498 | * has too small (not enough) | |
1499 | * scope, because ifa entries | |
1500 | * are sorted by their scope | |
1501 | * values. | |
1502 | */ | |
1503 | goto out; | |
1504 | } | |
1505 | break; | |
1506 | } else if (minihiscore < miniscore) { | |
1507 | if (hiscore->ifa) | |
1508 | in6_ifa_put(hiscore->ifa); | |
1509 | ||
1510 | in6_ifa_hold(score->ifa); | |
1511 | ||
1512 | swap(hiscore, score); | |
c0b8da1e | 1513 | hiscore_idx = 1 - hiscore_idx; |
9131f3de YH |
1514 | |
1515 | /* restore our iterator */ | |
1516 | score->ifa = hiscore->ifa; | |
1517 | ||
1518 | break; | |
1519 | } | |
1520 | } | |
1521 | } | |
1522 | out: | |
1523 | read_unlock_bh(&idev->lock); | |
c0b8da1e | 1524 | return hiscore_idx; |
9131f3de YH |
1525 | } |
1526 | ||
b3f644fc | 1527 | int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, |
9acd9f3a | 1528 | const struct in6_addr *daddr, unsigned int prefs, |
7cbca67c | 1529 | struct in6_addr *saddr) |
1da177e4 | 1530 | { |
c0b8da1e | 1531 | struct ipv6_saddr_score scores[2], *hiscore; |
a9b05723 | 1532 | struct ipv6_saddr_dst dst; |
9131f3de | 1533 | struct inet6_dev *idev; |
072047e4 | 1534 | struct net_device *dev; |
a9b05723 | 1535 | int dst_type; |
9131f3de | 1536 | bool use_oif_addr = false; |
c0b8da1e | 1537 | int hiscore_idx = 0; |
1da177e4 | 1538 | |
a9b05723 YH |
1539 | dst_type = __ipv6_addr_type(daddr); |
1540 | dst.addr = daddr; | |
1541 | dst.ifindex = dst_dev ? dst_dev->ifindex : 0; | |
1542 | dst.scope = __ipv6_addr_src_scope(dst_type); | |
3de23255 | 1543 | dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); |
7cbca67c | 1544 | dst.prefs = prefs; |
a9b05723 | 1545 | |
c0b8da1e YH |
1546 | scores[hiscore_idx].rule = -1; |
1547 | scores[hiscore_idx].ifa = NULL; | |
1da177e4 | 1548 | |
8814c4b5 | 1549 | rcu_read_lock(); |
1da177e4 | 1550 | |
9131f3de YH |
1551 | /* Candidate Source Address (section 4) |
1552 | * - multicast and link-local destination address, | |
1553 | * the set of candidate source address MUST only | |
1554 | * include addresses assigned to interfaces | |
1555 | * belonging to the same link as the outgoing | |
1556 | * interface. | |
1557 | * (- For site-local destination addresses, the | |
1558 | * set of candidate source addresses MUST only | |
1559 | * include addresses assigned to interfaces | |
1560 | * belonging to the same site as the outgoing | |
1561 | * interface.) | |
3985e8a3 EK |
1562 | * - "It is RECOMMENDED that the candidate source addresses |
1563 | * be the set of unicast addresses assigned to the | |
1564 | * interface that will be used to send to the destination | |
1565 | * (the 'outgoing' interface)." (RFC 6724) | |
9131f3de YH |
1566 | */ |
1567 | if (dst_dev) { | |
3985e8a3 | 1568 | idev = __in6_dev_get(dst_dev); |
9131f3de | 1569 | if ((dst_type & IPV6_ADDR_MULTICAST) || |
3985e8a3 EK |
1570 | dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || |
1571 | (idev && idev->cnf.use_oif_addrs_only)) { | |
9131f3de YH |
1572 | use_oif_addr = true; |
1573 | } | |
1574 | } | |
a9b05723 | 1575 | |
9131f3de | 1576 | if (use_oif_addr) { |
c0b8da1e | 1577 | if (idev) |
c15df306 | 1578 | hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); |
9131f3de YH |
1579 | } else { |
1580 | for_each_netdev_rcu(net, dev) { | |
1581 | idev = __in6_dev_get(dev); | |
1582 | if (!idev) | |
072047e4 | 1583 | continue; |
c15df306 | 1584 | hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); |
1da177e4 LT |
1585 | } |
1586 | } | |
8814c4b5 | 1587 | rcu_read_unlock(); |
1da177e4 | 1588 | |
c0b8da1e | 1589 | hiscore = &scores[hiscore_idx]; |
a9b05723 | 1590 | if (!hiscore->ifa) |
072047e4 | 1591 | return -EADDRNOTAVAIL; |
1ab1457c | 1592 | |
4e3fd7a0 | 1593 | *saddr = hiscore->ifa->addr; |
a9b05723 | 1594 | in6_ifa_put(hiscore->ifa); |
072047e4 | 1595 | return 0; |
1da177e4 | 1596 | } |
5e5f3f0f | 1597 | EXPORT_SYMBOL(ipv6_dev_get_saddr); |
1da177e4 | 1598 | |
8965779d | 1599 | int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, |
479840ff | 1600 | u32 banned_flags) |
b7b1bfce HFS |
1601 | { |
1602 | struct inet6_ifaddr *ifp; | |
1603 | int err = -EADDRNOTAVAIL; | |
1604 | ||
602582ca HFS |
1605 | list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { |
1606 | if (ifp->scope > IFA_LINK) | |
1607 | break; | |
b7b1bfce HFS |
1608 | if (ifp->scope == IFA_LINK && |
1609 | !(ifp->flags & banned_flags)) { | |
1610 | *addr = ifp->addr; | |
1611 | err = 0; | |
1612 | break; | |
1613 | } | |
1614 | } | |
1615 | return err; | |
1616 | } | |
1617 | ||
95c385b4 | 1618 | int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, |
479840ff | 1619 | u32 banned_flags) |
1da177e4 LT |
1620 | { |
1621 | struct inet6_dev *idev; | |
1622 | int err = -EADDRNOTAVAIL; | |
1623 | ||
8814c4b5 | 1624 | rcu_read_lock(); |
e21e8467 SH |
1625 | idev = __in6_dev_get(dev); |
1626 | if (idev) { | |
1da177e4 | 1627 | read_lock_bh(&idev->lock); |
b7b1bfce | 1628 | err = __ipv6_get_lladdr(idev, addr, banned_flags); |
1da177e4 LT |
1629 | read_unlock_bh(&idev->lock); |
1630 | } | |
8814c4b5 | 1631 | rcu_read_unlock(); |
1da177e4 LT |
1632 | return err; |
1633 | } | |
1634 | ||
1635 | static int ipv6_count_addresses(struct inet6_dev *idev) | |
1636 | { | |
1637 | int cnt = 0; | |
1638 | struct inet6_ifaddr *ifp; | |
1639 | ||
1640 | read_lock_bh(&idev->lock); | |
502a2ffd | 1641 | list_for_each_entry(ifp, &idev->addr_list, if_list) |
1da177e4 LT |
1642 | cnt++; |
1643 | read_unlock_bh(&idev->lock); | |
1644 | return cnt; | |
1645 | } | |
1646 | ||
b71d1d42 | 1647 | int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, |
2a7851bf | 1648 | const struct net_device *dev, int strict) |
c58da4c6 EK |
1649 | { |
1650 | return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); | |
1651 | } | |
1652 | EXPORT_SYMBOL(ipv6_chk_addr); | |
1653 | ||
1654 | int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, | |
1655 | const struct net_device *dev, int strict, | |
1656 | u32 banned_flags) | |
1da177e4 | 1657 | { |
eedf042a | 1658 | struct inet6_ifaddr *ifp; |
ddbe5032 | 1659 | unsigned int hash = inet6_addr_hash(addr); |
c58da4c6 | 1660 | u32 ifp_flags; |
1da177e4 | 1661 | |
5c578aed | 1662 | rcu_read_lock_bh(); |
b67bfe0d | 1663 | hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { |
878628fb | 1664 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
bfeade08 | 1665 | continue; |
c58da4c6 EK |
1666 | /* Decouple optimistic from tentative for evaluation here. |
1667 | * Ban optimistic addresses explicitly, when required. | |
1668 | */ | |
1669 | ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) | |
1670 | ? (ifp->flags&~IFA_F_TENTATIVE) | |
1671 | : ifp->flags; | |
1da177e4 | 1672 | if (ipv6_addr_equal(&ifp->addr, addr) && |
c58da4c6 | 1673 | !(ifp_flags&banned_flags) && |
63159f29 | 1674 | (!dev || ifp->idev->dev == dev || |
eedf042a SH |
1675 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { |
1676 | rcu_read_unlock_bh(); | |
1677 | return 1; | |
1da177e4 LT |
1678 | } |
1679 | } | |
5c578aed | 1680 | |
eedf042a SH |
1681 | rcu_read_unlock_bh(); |
1682 | return 0; | |
1da177e4 | 1683 | } |
c58da4c6 | 1684 | EXPORT_SYMBOL(ipv6_chk_addr_and_flags); |
7159039a | 1685 | |
3e81c6da DM |
1686 | static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, |
1687 | struct net_device *dev) | |
1da177e4 | 1688 | { |
ddbe5032 | 1689 | unsigned int hash = inet6_addr_hash(addr); |
c2e21293 | 1690 | struct inet6_ifaddr *ifp; |
1da177e4 | 1691 | |
b67bfe0d | 1692 | hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { |
878628fb | 1693 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
06bfe655 | 1694 | continue; |
1da177e4 | 1695 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
63159f29 | 1696 | if (!dev || ifp->idev->dev == dev) |
3e81c6da | 1697 | return true; |
1da177e4 LT |
1698 | } |
1699 | } | |
3e81c6da | 1700 | return false; |
1da177e4 LT |
1701 | } |
1702 | ||
7df37ff3 CB |
1703 | /* Compares an address/prefix_len with addresses on device @dev. |
1704 | * If one is found it returns true. | |
1705 | */ | |
1706 | bool ipv6_chk_custom_prefix(const struct in6_addr *addr, | |
1707 | const unsigned int prefix_len, struct net_device *dev) | |
1708 | { | |
1709 | struct inet6_dev *idev; | |
1710 | struct inet6_ifaddr *ifa; | |
1711 | bool ret = false; | |
1712 | ||
1713 | rcu_read_lock(); | |
1714 | idev = __in6_dev_get(dev); | |
1715 | if (idev) { | |
1716 | read_lock_bh(&idev->lock); | |
1717 | list_for_each_entry(ifa, &idev->addr_list, if_list) { | |
1718 | ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); | |
1719 | if (ret) | |
1720 | break; | |
1721 | } | |
1722 | read_unlock_bh(&idev->lock); | |
1723 | } | |
1724 | rcu_read_unlock(); | |
1725 | ||
1726 | return ret; | |
1727 | } | |
1728 | EXPORT_SYMBOL(ipv6_chk_custom_prefix); | |
1729 | ||
b71d1d42 | 1730 | int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) |
52eeeb84 YH |
1731 | { |
1732 | struct inet6_dev *idev; | |
1733 | struct inet6_ifaddr *ifa; | |
1734 | int onlink; | |
1735 | ||
1736 | onlink = 0; | |
1737 | rcu_read_lock(); | |
1738 | idev = __in6_dev_get(dev); | |
1739 | if (idev) { | |
1740 | read_lock_bh(&idev->lock); | |
502a2ffd | 1741 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
52eeeb84 YH |
1742 | onlink = ipv6_prefix_equal(addr, &ifa->addr, |
1743 | ifa->prefix_len); | |
1744 | if (onlink) | |
1745 | break; | |
1746 | } | |
1747 | read_unlock_bh(&idev->lock); | |
1748 | } | |
1749 | rcu_read_unlock(); | |
1750 | return onlink; | |
1751 | } | |
52eeeb84 YH |
1752 | EXPORT_SYMBOL(ipv6_chk_prefix); |
1753 | ||
9acd9f3a | 1754 | struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, |
1cab3da6 | 1755 | struct net_device *dev, int strict) |
1da177e4 | 1756 | { |
b79d1d54 | 1757 | struct inet6_ifaddr *ifp, *result = NULL; |
ddbe5032 | 1758 | unsigned int hash = inet6_addr_hash(addr); |
1da177e4 | 1759 | |
5c578aed | 1760 | rcu_read_lock_bh(); |
b67bfe0d | 1761 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { |
878628fb | 1762 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
1cab3da6 | 1763 | continue; |
1da177e4 | 1764 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
63159f29 | 1765 | if (!dev || ifp->idev->dev == dev || |
1da177e4 | 1766 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { |
b79d1d54 | 1767 | result = ifp; |
1da177e4 LT |
1768 | in6_ifa_hold(ifp); |
1769 | break; | |
1770 | } | |
1771 | } | |
1772 | } | |
5c578aed | 1773 | rcu_read_unlock_bh(); |
1da177e4 | 1774 | |
b79d1d54 | 1775 | return result; |
1da177e4 LT |
1776 | } |
1777 | ||
1da177e4 LT |
1778 | /* Gets referenced address, destroys ifaddr */ |
1779 | ||
cc411d0b | 1780 | static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) |
1da177e4 | 1781 | { |
3d171f39 LR |
1782 | if (dad_failed) |
1783 | ifp->flags |= IFA_F_DADFAILED; | |
1784 | ||
1da177e4 LT |
1785 | if (ifp->flags&IFA_F_PERMANENT) { |
1786 | spin_lock_bh(&ifp->lock); | |
c15b1cca | 1787 | addrconf_del_dad_work(ifp); |
1da177e4 LT |
1788 | ifp->flags |= IFA_F_TENTATIVE; |
1789 | spin_unlock_bh(&ifp->lock); | |
e2577a06 HX |
1790 | if (dad_failed) |
1791 | ipv6_ifa_notify(0, ifp); | |
1da177e4 | 1792 | in6_ifa_put(ifp); |
1da177e4 LT |
1793 | } else if (ifp->flags&IFA_F_TEMPORARY) { |
1794 | struct inet6_ifaddr *ifpub; | |
1795 | spin_lock_bh(&ifp->lock); | |
1796 | ifpub = ifp->ifpub; | |
1797 | if (ifpub) { | |
1798 | in6_ifa_hold(ifpub); | |
1799 | spin_unlock_bh(&ifp->lock); | |
1800 | ipv6_create_tempaddr(ifpub, ifp); | |
1801 | in6_ifa_put(ifpub); | |
1802 | } else { | |
1803 | spin_unlock_bh(&ifp->lock); | |
1804 | } | |
1805 | ipv6_del_addr(ifp); | |
c15b1cca | 1806 | } else { |
1da177e4 | 1807 | ipv6_del_addr(ifp); |
c15b1cca | 1808 | } |
1da177e4 LT |
1809 | } |
1810 | ||
f2344a13 HX |
1811 | static int addrconf_dad_end(struct inet6_ifaddr *ifp) |
1812 | { | |
1813 | int err = -ENOENT; | |
1814 | ||
8e8e676d | 1815 | spin_lock_bh(&ifp->lock); |
f2344a13 HX |
1816 | if (ifp->state == INET6_IFADDR_STATE_DAD) { |
1817 | ifp->state = INET6_IFADDR_STATE_POSTDAD; | |
1818 | err = 0; | |
1819 | } | |
8e8e676d | 1820 | spin_unlock_bh(&ifp->lock); |
f2344a13 HX |
1821 | |
1822 | return err; | |
1823 | } | |
1824 | ||
3c21edbd YH |
1825 | void addrconf_dad_failure(struct inet6_ifaddr *ifp) |
1826 | { | |
5f40ef77 | 1827 | struct in6_addr addr; |
1b34be74 | 1828 | struct inet6_dev *idev = ifp->idev; |
1855b7c3 | 1829 | struct net *net = dev_net(ifp->idev->dev); |
9bdd8d40 | 1830 | |
853dc2e0 UB |
1831 | if (addrconf_dad_end(ifp)) { |
1832 | in6_ifa_put(ifp); | |
f2344a13 | 1833 | return; |
853dc2e0 | 1834 | } |
f2344a13 | 1835 | |
e87cc472 JP |
1836 | net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", |
1837 | ifp->idev->dev->name, &ifp->addr); | |
9bdd8d40 | 1838 | |
5f40ef77 HFS |
1839 | spin_lock_bh(&ifp->lock); |
1840 | ||
1841 | if (ifp->flags & IFA_F_STABLE_PRIVACY) { | |
1842 | int scope = ifp->scope; | |
1843 | u32 flags = ifp->flags; | |
1844 | struct in6_addr new_addr; | |
1845 | struct inet6_ifaddr *ifp2; | |
1846 | u32 valid_lft, preferred_lft; | |
1847 | int pfxlen = ifp->prefix_len; | |
5f40ef77 HFS |
1848 | int retries = ifp->stable_privacy_retry + 1; |
1849 | ||
1855b7c3 | 1850 | if (retries > net->ipv6.sysctl.idgen_retries) { |
5f40ef77 HFS |
1851 | net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", |
1852 | ifp->idev->dev->name); | |
1853 | goto errdad; | |
1854 | } | |
1855 | ||
1856 | new_addr = ifp->addr; | |
1857 | if (ipv6_generate_stable_address(&new_addr, retries, | |
1858 | idev)) | |
1859 | goto errdad; | |
1860 | ||
1861 | valid_lft = ifp->valid_lft; | |
1862 | preferred_lft = ifp->prefered_lft; | |
1863 | ||
1864 | spin_unlock_bh(&ifp->lock); | |
1b34be74 | 1865 | |
5f40ef77 HFS |
1866 | if (idev->cnf.max_addresses && |
1867 | ipv6_count_addresses(idev) >= | |
1868 | idev->cnf.max_addresses) | |
1869 | goto lock_errdad; | |
1870 | ||
1871 | net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", | |
1872 | ifp->idev->dev->name); | |
1873 | ||
1874 | ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, | |
1875 | scope, flags, valid_lft, | |
1876 | preferred_lft); | |
1877 | if (IS_ERR(ifp2)) | |
1878 | goto lock_errdad; | |
1879 | ||
1880 | spin_lock_bh(&ifp2->lock); | |
1881 | ifp2->stable_privacy_retry = retries; | |
1882 | ifp2->state = INET6_IFADDR_STATE_PREDAD; | |
1883 | spin_unlock_bh(&ifp2->lock); | |
1884 | ||
1855b7c3 | 1885 | addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); |
5f40ef77 HFS |
1886 | in6_ifa_put(ifp2); |
1887 | lock_errdad: | |
1888 | spin_lock_bh(&ifp->lock); | |
1889 | } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { | |
1b34be74 YH |
1890 | addr.s6_addr32[0] = htonl(0xfe800000); |
1891 | addr.s6_addr32[1] = 0; | |
1892 | ||
1893 | if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && | |
1894 | ipv6_addr_equal(&ifp->addr, &addr)) { | |
1895 | /* DAD failed for link-local based on MAC address */ | |
1896 | idev->cnf.disable_ipv6 = 1; | |
9bdd8d40 | 1897 | |
f3213831 | 1898 | pr_info("%s: IPv6 being disabled!\n", |
9bdd8d40 | 1899 | ifp->idev->dev->name); |
1b34be74 YH |
1900 | } |
1901 | } | |
1902 | ||
5f40ef77 | 1903 | errdad: |
c15b1cca HFS |
1904 | /* transition from _POSTDAD to _ERRDAD */ |
1905 | ifp->state = INET6_IFADDR_STATE_ERRDAD; | |
8e8e676d | 1906 | spin_unlock_bh(&ifp->lock); |
c15b1cca HFS |
1907 | |
1908 | addrconf_mod_dad_work(ifp, 0); | |
3c21edbd | 1909 | } |
1da177e4 | 1910 | |
a9ed4a29 SD |
1911 | /* Join to solicited addr multicast group. |
1912 | * caller must hold RTNL */ | |
b71d1d42 | 1913 | void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) |
1da177e4 LT |
1914 | { |
1915 | struct in6_addr maddr; | |
1916 | ||
1917 | if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) | |
1918 | return; | |
1919 | ||
1920 | addrconf_addr_solict_mult(addr, &maddr); | |
1921 | ipv6_dev_mc_inc(dev, &maddr); | |
1922 | } | |
1923 | ||
a9ed4a29 | 1924 | /* caller must hold RTNL */ |
b71d1d42 | 1925 | void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) |
1da177e4 LT |
1926 | { |
1927 | struct in6_addr maddr; | |
1928 | ||
1929 | if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) | |
1930 | return; | |
1931 | ||
1932 | addrconf_addr_solict_mult(addr, &maddr); | |
1933 | __ipv6_dev_mc_dec(idev, &maddr); | |
1934 | } | |
1935 | ||
a9ed4a29 | 1936 | /* caller must hold RTNL */ |
20380731 | 1937 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp) |
1da177e4 LT |
1938 | { |
1939 | struct in6_addr addr; | |
c15b1cca | 1940 | |
88ad3149 | 1941 | if (ifp->prefix_len >= 127) /* RFC 6164 */ |
2bda8a0c | 1942 | return; |
1da177e4 LT |
1943 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
1944 | if (ipv6_addr_any(&addr)) | |
1945 | return; | |
013b4d90 | 1946 | __ipv6_dev_ac_inc(ifp->idev, &addr); |
1da177e4 LT |
1947 | } |
1948 | ||
a9ed4a29 | 1949 | /* caller must hold RTNL */ |
20380731 | 1950 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) |
1da177e4 LT |
1951 | { |
1952 | struct in6_addr addr; | |
c15b1cca | 1953 | |
88ad3149 | 1954 | if (ifp->prefix_len >= 127) /* RFC 6164 */ |
32019e65 | 1955 | return; |
1da177e4 LT |
1956 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
1957 | if (ipv6_addr_any(&addr)) | |
1958 | return; | |
1959 | __ipv6_dev_ac_dec(ifp->idev, &addr); | |
1960 | } | |
1961 | ||
06a4c1c5 | 1962 | static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) |
1963 | { | |
5241c2d7 | 1964 | if (dev->addr_len != EUI64_ADDR_LEN) |
06a4c1c5 | 1965 | return -1; |
5241c2d7 | 1966 | memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); |
5e98a36e | 1967 | eui[0] ^= 2; |
06a4c1c5 | 1968 | return 0; |
1969 | } | |
1970 | ||
cb6bf355 YH |
1971 | static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) |
1972 | { | |
1973 | union fwnet_hwaddr *ha; | |
1974 | ||
1975 | if (dev->addr_len != FWNET_ALEN) | |
1976 | return -1; | |
1977 | ||
1978 | ha = (union fwnet_hwaddr *)dev->dev_addr; | |
1979 | ||
1980 | memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); | |
1981 | eui[0] ^= 2; | |
1982 | return 0; | |
1983 | } | |
1984 | ||
073a8e0e YH |
1985 | static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) |
1986 | { | |
1987 | /* XXX: inherit EUI-64 from other interface -- yoshfuji */ | |
1988 | if (dev->addr_len != ARCNET_ALEN) | |
1989 | return -1; | |
1990 | memset(eui, 0, 7); | |
8e5e8f30 | 1991 | eui[7] = *(u8 *)dev->dev_addr; |
073a8e0e YH |
1992 | return 0; |
1993 | } | |
1994 | ||
1995 | static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) | |
1996 | { | |
1997 | if (dev->addr_len != INFINIBAND_ALEN) | |
1998 | return -1; | |
1999 | memcpy(eui, dev->dev_addr + 12, 8); | |
2000 | eui[0] |= 2; | |
2001 | return 0; | |
2002 | } | |
2003 | ||
c61393ea | 2004 | static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) |
dfd982ba | 2005 | { |
9af28511 SH |
2006 | if (addr == 0) |
2007 | return -1; | |
dfd982ba YH |
2008 | eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || |
2009 | ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || | |
2010 | ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || | |
2011 | ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || | |
2012 | ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || | |
2013 | ipv4_is_lbcast(addr)) ? 0x00 : 0x02; | |
2014 | eui[1] = 0; | |
2015 | eui[2] = 0x5E; | |
2016 | eui[3] = 0xFE; | |
2017 | memcpy(eui + 4, &addr, 4); | |
2018 | return 0; | |
2019 | } | |
dfd982ba YH |
2020 | |
2021 | static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) | |
2022 | { | |
2023 | if (dev->priv_flags & IFF_ISATAP) | |
2024 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); | |
2025 | return -1; | |
2026 | } | |
2027 | ||
aee80b54 | 2028 | static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) |
2029 | { | |
2030 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); | |
2031 | } | |
2032 | ||
e837735e ND |
2033 | static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) |
2034 | { | |
2035 | memcpy(eui, dev->perm_addr, 3); | |
2036 | memcpy(eui + 5, dev->perm_addr + 3, 3); | |
2037 | eui[3] = 0xFF; | |
2038 | eui[4] = 0xFE; | |
2039 | eui[0] ^= 2; | |
2040 | return 0; | |
2041 | } | |
2042 | ||
1da177e4 LT |
2043 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) |
2044 | { | |
2045 | switch (dev->type) { | |
2046 | case ARPHRD_ETHER: | |
2047 | case ARPHRD_FDDI: | |
073a8e0e | 2048 | return addrconf_ifid_eui48(eui, dev); |
1da177e4 | 2049 | case ARPHRD_ARCNET: |
073a8e0e | 2050 | return addrconf_ifid_arcnet(eui, dev); |
1da177e4 | 2051 | case ARPHRD_INFINIBAND: |
073a8e0e | 2052 | return addrconf_ifid_infiniband(eui, dev); |
c7dc89c0 | 2053 | case ARPHRD_SIT: |
dfd982ba | 2054 | return addrconf_ifid_sit(eui, dev); |
aee80b54 | 2055 | case ARPHRD_IPGRE: |
2056 | return addrconf_ifid_gre(eui, dev); | |
e74bccb8 | 2057 | case ARPHRD_6LOWPAN: |
06a4c1c5 | 2058 | return addrconf_ifid_eui64(eui, dev); |
cb6bf355 YH |
2059 | case ARPHRD_IEEE1394: |
2060 | return addrconf_ifid_ieee1394(eui, dev); | |
e837735e ND |
2061 | case ARPHRD_TUNNEL6: |
2062 | return addrconf_ifid_ip6tnl(eui, dev); | |
1da177e4 LT |
2063 | } |
2064 | return -1; | |
2065 | } | |
2066 | ||
2067 | static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) | |
2068 | { | |
2069 | int err = -1; | |
2070 | struct inet6_ifaddr *ifp; | |
2071 | ||
2072 | read_lock_bh(&idev->lock); | |
602582ca HFS |
2073 | list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { |
2074 | if (ifp->scope > IFA_LINK) | |
2075 | break; | |
1da177e4 LT |
2076 | if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { |
2077 | memcpy(eui, ifp->addr.s6_addr+8, 8); | |
2078 | err = 0; | |
2079 | break; | |
2080 | } | |
2081 | } | |
2082 | read_unlock_bh(&idev->lock); | |
2083 | return err; | |
2084 | } | |
2085 | ||
1da177e4 | 2086 | /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ |
eb7e0575 | 2087 | static void __ipv6_regen_rndid(struct inet6_dev *idev) |
1da177e4 | 2088 | { |
1da177e4 | 2089 | regen: |
955189ef | 2090 | get_random_bytes(idev->rndid, sizeof(idev->rndid)); |
1da177e4 | 2091 | idev->rndid[0] &= ~0x02; |
1da177e4 LT |
2092 | |
2093 | /* | |
2094 | * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: | |
2095 | * check if generated address is not inappropriate | |
2096 | * | |
2097 | * - Reserved subnet anycast (RFC 2526) | |
2098 | * 11111101 11....11 1xxxxxxx | |
c7dc89c0 | 2099 | * - ISATAP (RFC4214) 6.1 |
1da177e4 LT |
2100 | * 00-00-5E-FE-xx-xx-xx-xx |
2101 | * - value 0 | |
2102 | * - XXX: already assigned to an address on the device | |
2103 | */ | |
1ab1457c | 2104 | if (idev->rndid[0] == 0xfd && |
1da177e4 LT |
2105 | (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && |
2106 | (idev->rndid[7]&0x80)) | |
2107 | goto regen; | |
2108 | if ((idev->rndid[0]|idev->rndid[1]) == 0) { | |
2109 | if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) | |
2110 | goto regen; | |
2111 | if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) | |
2112 | goto regen; | |
2113 | } | |
1da177e4 LT |
2114 | } |
2115 | ||
2116 | static void ipv6_regen_rndid(unsigned long data) | |
2117 | { | |
2118 | struct inet6_dev *idev = (struct inet6_dev *) data; | |
2119 | unsigned long expires; | |
2120 | ||
8814c4b5 | 2121 | rcu_read_lock_bh(); |
1da177e4 LT |
2122 | write_lock_bh(&idev->lock); |
2123 | ||
2124 | if (idev->dead) | |
2125 | goto out; | |
2126 | ||
eb7e0575 | 2127 | __ipv6_regen_rndid(idev); |
1ab1457c | 2128 | |
1da177e4 | 2129 | expires = jiffies + |
1ab1457c | 2130 | idev->cnf.temp_prefered_lft * HZ - |
1f9248e5 JP |
2131 | idev->cnf.regen_max_retry * idev->cnf.dad_transmits * |
2132 | NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - | |
784e2710 | 2133 | idev->cnf.max_desync_factor * HZ; |
1da177e4 | 2134 | if (time_before(expires, jiffies)) { |
f3213831 JP |
2135 | pr_warn("%s: too short regeneration interval; timer disabled for %s\n", |
2136 | __func__, idev->dev->name); | |
1da177e4 LT |
2137 | goto out; |
2138 | } | |
2139 | ||
2140 | if (!mod_timer(&idev->regen_timer, expires)) | |
2141 | in6_dev_hold(idev); | |
2142 | ||
2143 | out: | |
2144 | write_unlock_bh(&idev->lock); | |
8814c4b5 | 2145 | rcu_read_unlock_bh(); |
1da177e4 LT |
2146 | in6_dev_put(idev); |
2147 | } | |
2148 | ||
eb7e0575 | 2149 | static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) |
8e5e8f30 | 2150 | { |
1da177e4 | 2151 | if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) |
eb7e0575 | 2152 | __ipv6_regen_rndid(idev); |
1da177e4 | 2153 | } |
1da177e4 LT |
2154 | |
2155 | /* | |
2156 | * Add prefix route. | |
2157 | */ | |
2158 | ||
2159 | static void | |
2160 | addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, | |
e431b8c0 | 2161 | unsigned long expires, u32 flags) |
1da177e4 | 2162 | { |
86872cb5 | 2163 | struct fib6_config cfg = { |
ca254490 | 2164 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, |
86872cb5 TG |
2165 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
2166 | .fc_ifindex = dev->ifindex, | |
2167 | .fc_expires = expires, | |
2168 | .fc_dst_len = plen, | |
2169 | .fc_flags = RTF_UP | flags, | |
c346dca1 | 2170 | .fc_nlinfo.nl_net = dev_net(dev), |
f410a1fb | 2171 | .fc_protocol = RTPROT_KERNEL, |
86872cb5 | 2172 | }; |
1da177e4 | 2173 | |
4e3fd7a0 | 2174 | cfg.fc_dst = *pfx; |
1da177e4 LT |
2175 | |
2176 | /* Prevent useless cloning on PtP SIT. | |
2177 | This thing is done here expecting that the whole | |
2178 | class of non-broadcast devices need not cloning. | |
2179 | */ | |
07a93626 | 2180 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
86872cb5 TG |
2181 | if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) |
2182 | cfg.fc_flags |= RTF_NONEXTHOP; | |
0be669bb | 2183 | #endif |
1da177e4 | 2184 | |
86872cb5 | 2185 | ip6_route_add(&cfg); |
1da177e4 LT |
2186 | } |
2187 | ||
14ef37b6 AH |
2188 | |
2189 | static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, | |
2190 | int plen, | |
2191 | const struct net_device *dev, | |
2192 | u32 flags, u32 noflags) | |
2193 | { | |
2194 | struct fib6_node *fn; | |
2195 | struct rt6_info *rt = NULL; | |
2196 | struct fib6_table *table; | |
ca254490 | 2197 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; |
14ef37b6 | 2198 | |
ca254490 | 2199 | table = fib6_get_table(dev_net(dev), tb_id); |
63159f29 | 2200 | if (!table) |
14ef37b6 AH |
2201 | return NULL; |
2202 | ||
5744dd9b | 2203 | read_lock_bh(&table->tb6_lock); |
14ef37b6 AH |
2204 | fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); |
2205 | if (!fn) | |
2206 | goto out; | |
1f56a01f MKL |
2207 | |
2208 | noflags |= RTF_CACHE; | |
14ef37b6 | 2209 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
d1918542 | 2210 | if (rt->dst.dev->ifindex != dev->ifindex) |
14ef37b6 AH |
2211 | continue; |
2212 | if ((rt->rt6i_flags & flags) != flags) | |
2213 | continue; | |
85da53bf | 2214 | if ((rt->rt6i_flags & noflags) != 0) |
14ef37b6 AH |
2215 | continue; |
2216 | dst_hold(&rt->dst); | |
2217 | break; | |
2218 | } | |
2219 | out: | |
5744dd9b | 2220 | read_unlock_bh(&table->tb6_lock); |
14ef37b6 AH |
2221 | return rt; |
2222 | } | |
2223 | ||
2224 | ||
1da177e4 LT |
2225 | /* Create "default" multicast route to the interface */ |
2226 | ||
2227 | static void addrconf_add_mroute(struct net_device *dev) | |
2228 | { | |
86872cb5 | 2229 | struct fib6_config cfg = { |
ca254490 | 2230 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, |
86872cb5 TG |
2231 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
2232 | .fc_ifindex = dev->ifindex, | |
2233 | .fc_dst_len = 8, | |
2234 | .fc_flags = RTF_UP, | |
c346dca1 | 2235 | .fc_nlinfo.nl_net = dev_net(dev), |
86872cb5 TG |
2236 | }; |
2237 | ||
2238 | ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); | |
2239 | ||
2240 | ip6_route_add(&cfg); | |
1da177e4 LT |
2241 | } |
2242 | ||
1da177e4 LT |
2243 | static struct inet6_dev *addrconf_add_dev(struct net_device *dev) |
2244 | { | |
2245 | struct inet6_dev *idev; | |
2246 | ||
2247 | ASSERT_RTNL(); | |
2248 | ||
e21e8467 SH |
2249 | idev = ipv6_find_idev(dev); |
2250 | if (!idev) | |
64e724f6 BH |
2251 | return ERR_PTR(-ENOBUFS); |
2252 | ||
2253 | if (idev->cnf.disable_ipv6) | |
2254 | return ERR_PTR(-EACCES); | |
1da177e4 LT |
2255 | |
2256 | /* Add default multicast route */ | |
4af04aba LW |
2257 | if (!(dev->flags & IFF_LOOPBACK)) |
2258 | addrconf_add_mroute(dev); | |
1da177e4 | 2259 | |
1da177e4 LT |
2260 | return idev; |
2261 | } | |
2262 | ||
53bd6749 JP |
2263 | static void manage_tempaddrs(struct inet6_dev *idev, |
2264 | struct inet6_ifaddr *ifp, | |
2265 | __u32 valid_lft, __u32 prefered_lft, | |
2266 | bool create, unsigned long now) | |
2267 | { | |
2268 | u32 flags; | |
2269 | struct inet6_ifaddr *ift; | |
2270 | ||
2271 | read_lock_bh(&idev->lock); | |
2272 | /* update all temporary addresses in the list */ | |
2273 | list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { | |
2274 | int age, max_valid, max_prefered; | |
2275 | ||
2276 | if (ifp != ift->ifpub) | |
2277 | continue; | |
2278 | ||
2279 | /* RFC 4941 section 3.3: | |
2280 | * If a received option will extend the lifetime of a public | |
2281 | * address, the lifetimes of temporary addresses should | |
2282 | * be extended, subject to the overall constraint that no | |
2283 | * temporary addresses should ever remain "valid" or "preferred" | |
2284 | * for a time longer than (TEMP_VALID_LIFETIME) or | |
2285 | * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. | |
2286 | */ | |
2287 | age = (now - ift->cstamp) / HZ; | |
2288 | max_valid = idev->cnf.temp_valid_lft - age; | |
2289 | if (max_valid < 0) | |
2290 | max_valid = 0; | |
2291 | ||
2292 | max_prefered = idev->cnf.temp_prefered_lft - | |
2293 | idev->cnf.max_desync_factor - age; | |
2294 | if (max_prefered < 0) | |
2295 | max_prefered = 0; | |
2296 | ||
2297 | if (valid_lft > max_valid) | |
2298 | valid_lft = max_valid; | |
2299 | ||
2300 | if (prefered_lft > max_prefered) | |
2301 | prefered_lft = max_prefered; | |
2302 | ||
2303 | spin_lock(&ift->lock); | |
2304 | flags = ift->flags; | |
2305 | ift->valid_lft = valid_lft; | |
2306 | ift->prefered_lft = prefered_lft; | |
2307 | ift->tstamp = now; | |
2308 | if (prefered_lft > 0) | |
2309 | ift->flags &= ~IFA_F_DEPRECATED; | |
2310 | ||
2311 | spin_unlock(&ift->lock); | |
2312 | if (!(flags&IFA_F_TENTATIVE)) | |
2313 | ipv6_ifa_notify(0, ift); | |
2314 | } | |
2315 | ||
2316 | if ((create || list_empty(&idev->tempaddr_list)) && | |
2317 | idev->cnf.use_tempaddr > 0) { | |
2318 | /* When a new public address is created as described | |
2319 | * in [ADDRCONF], also create a new temporary address. | |
2320 | * Also create a temporary address if it's enabled but | |
2321 | * no temporary address currently exists. | |
2322 | */ | |
2323 | read_unlock_bh(&idev->lock); | |
2324 | ipv6_create_tempaddr(ifp, NULL); | |
2325 | } else { | |
2326 | read_unlock_bh(&idev->lock); | |
2327 | } | |
2328 | } | |
2329 | ||
cc9da6cc BM |
2330 | static bool is_addr_mode_generate_stable(struct inet6_dev *idev) |
2331 | { | |
2332 | return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || | |
2333 | idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; | |
2334 | } | |
2335 | ||
e6bff995 | 2336 | void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) |
1da177e4 LT |
2337 | { |
2338 | struct prefix_info *pinfo; | |
2339 | __u32 valid_lft; | |
2340 | __u32 prefered_lft; | |
2341 | int addr_type; | |
64236f3f | 2342 | u32 addr_flags = 0; |
1da177e4 | 2343 | struct inet6_dev *in6_dev; |
56d417b1 | 2344 | struct net *net = dev_net(dev); |
1da177e4 LT |
2345 | |
2346 | pinfo = (struct prefix_info *) opt; | |
1ab1457c | 2347 | |
1da177e4 | 2348 | if (len < sizeof(struct prefix_info)) { |
ba3542e1 | 2349 | ADBG("addrconf: prefix option too short\n"); |
1da177e4 LT |
2350 | return; |
2351 | } | |
1ab1457c | 2352 | |
1da177e4 LT |
2353 | /* |
2354 | * Validation checks ([ADDRCONF], page 19) | |
2355 | */ | |
2356 | ||
2357 | addr_type = ipv6_addr_type(&pinfo->prefix); | |
2358 | ||
2359 | if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) | |
2360 | return; | |
2361 | ||
2362 | valid_lft = ntohl(pinfo->valid); | |
2363 | prefered_lft = ntohl(pinfo->prefered); | |
2364 | ||
2365 | if (prefered_lft > valid_lft) { | |
e87cc472 | 2366 | net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); |
1da177e4 LT |
2367 | return; |
2368 | } | |
2369 | ||
2370 | in6_dev = in6_dev_get(dev); | |
2371 | ||
63159f29 | 2372 | if (!in6_dev) { |
e87cc472 JP |
2373 | net_dbg_ratelimited("addrconf: device %s not configured\n", |
2374 | dev->name); | |
1da177e4 LT |
2375 | return; |
2376 | } | |
2377 | ||
2378 | /* | |
2379 | * Two things going on here: | |
2380 | * 1) Add routes for on-link prefixes | |
2381 | * 2) Configure prefixes with the auto flag set | |
2382 | */ | |
2383 | ||
4bed72e4 YH |
2384 | if (pinfo->onlink) { |
2385 | struct rt6_info *rt; | |
2386 | unsigned long rt_expires; | |
2387 | ||
6f704992 YH |
2388 | /* Avoid arithmetic overflow. Really, we could |
2389 | * save rt_expires in seconds, likely valid_lft, | |
2390 | * but it would require division in fib gc, that it | |
2391 | * not good. | |
2392 | */ | |
4bed72e4 YH |
2393 | if (HZ > USER_HZ) |
2394 | rt_expires = addrconf_timeout_fixup(valid_lft, HZ); | |
2395 | else | |
2396 | rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); | |
3dd4bc68 | 2397 | |
4bed72e4 YH |
2398 | if (addrconf_finite_timeout(rt_expires)) |
2399 | rt_expires *= HZ; | |
1da177e4 | 2400 | |
14ef37b6 AH |
2401 | rt = addrconf_get_prefix_route(&pinfo->prefix, |
2402 | pinfo->prefix_len, | |
2403 | dev, | |
2404 | RTF_ADDRCONF | RTF_PREFIX_RT, | |
2405 | RTF_GATEWAY | RTF_DEFAULT); | |
1da177e4 | 2406 | |
14ef37b6 | 2407 | if (rt) { |
6f704992 YH |
2408 | /* Autoconf prefix route */ |
2409 | if (valid_lft == 0) { | |
2410 | ip6_del_rt(rt); | |
2411 | rt = NULL; | |
4bed72e4 | 2412 | } else if (addrconf_finite_timeout(rt_expires)) { |
6f704992 | 2413 | /* not infinity */ |
1716a961 | 2414 | rt6_set_expires(rt, jiffies + rt_expires); |
6f704992 | 2415 | } else { |
1716a961 | 2416 | rt6_clean_expires(rt); |
1da177e4 LT |
2417 | } |
2418 | } else if (valid_lft) { | |
6f704992 | 2419 | clock_t expires = 0; |
4bed72e4 YH |
2420 | int flags = RTF_ADDRCONF | RTF_PREFIX_RT; |
2421 | if (addrconf_finite_timeout(rt_expires)) { | |
6f704992 YH |
2422 | /* not infinity */ |
2423 | flags |= RTF_EXPIRES; | |
2424 | expires = jiffies_to_clock_t(rt_expires); | |
2425 | } | |
1da177e4 | 2426 | addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, |
6f704992 | 2427 | dev, expires, flags); |
1da177e4 | 2428 | } |
94e187c0 | 2429 | ip6_rt_put(rt); |
1da177e4 LT |
2430 | } |
2431 | ||
2432 | /* Try to figure out our local address for this prefix */ | |
2433 | ||
2434 | if (pinfo->autoconf && in6_dev->cnf.autoconf) { | |
8e5e8f30 | 2435 | struct inet6_ifaddr *ifp; |
1da177e4 LT |
2436 | struct in6_addr addr; |
2437 | int create = 0, update_lft = 0; | |
617fe29d | 2438 | bool tokenized = false; |
1da177e4 LT |
2439 | |
2440 | if (pinfo->prefix_len == 64) { | |
2441 | memcpy(&addr, &pinfo->prefix, 8); | |
f53adae4 DB |
2442 | |
2443 | if (!ipv6_addr_any(&in6_dev->token)) { | |
2444 | read_lock_bh(&in6_dev->lock); | |
2445 | memcpy(addr.s6_addr + 8, | |
2446 | in6_dev->token.s6_addr + 8, 8); | |
2447 | read_unlock_bh(&in6_dev->lock); | |
617fe29d | 2448 | tokenized = true; |
cc9da6cc | 2449 | } else if (is_addr_mode_generate_stable(in6_dev) && |
622c81d5 HFS |
2450 | !ipv6_generate_stable_address(&addr, 0, |
2451 | in6_dev)) { | |
64236f3f | 2452 | addr_flags |= IFA_F_STABLE_PRIVACY; |
622c81d5 | 2453 | goto ok; |
f53adae4 DB |
2454 | } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && |
2455 | ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { | |
1da177e4 LT |
2456 | in6_dev_put(in6_dev); |
2457 | return; | |
2458 | } | |
2459 | goto ok; | |
2460 | } | |
e87cc472 JP |
2461 | net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", |
2462 | pinfo->prefix_len); | |
1da177e4 LT |
2463 | in6_dev_put(in6_dev); |
2464 | return; | |
2465 | ||
2466 | ok: | |
2467 | ||
53b7997f | 2468 | ifp = ipv6_get_ifaddr(net, &addr, dev, 1); |
1da177e4 | 2469 | |
63159f29 | 2470 | if (!ifp && valid_lft) { |
1da177e4 | 2471 | int max_addresses = in6_dev->cnf.max_addresses; |
95c385b4 NH |
2472 | |
2473 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD | |
2474 | if (in6_dev->cnf.optimistic_dad && | |
e6bff995 | 2475 | !net->ipv6.devconf_all->forwarding && sllao) |
9a1ec461 | 2476 | addr_flags |= IFA_F_OPTIMISTIC; |
95c385b4 | 2477 | #endif |
1da177e4 LT |
2478 | |
2479 | /* Do not allow to create too much of autoconfigured | |
2480 | * addresses; this would be too easy way to crash kernel. | |
2481 | */ | |
2482 | if (!max_addresses || | |
2483 | ipv6_count_addresses(in6_dev) < max_addresses) | |
3f8f5298 JP |
2484 | ifp = ipv6_add_addr(in6_dev, &addr, NULL, |
2485 | pinfo->prefix_len, | |
95c385b4 | 2486 | addr_type&IPV6_ADDR_SCOPE_MASK, |
8a226b2c JB |
2487 | addr_flags, valid_lft, |
2488 | prefered_lft); | |
1da177e4 | 2489 | |
3f0d2ba0 | 2490 | if (IS_ERR_OR_NULL(ifp)) { |
1da177e4 LT |
2491 | in6_dev_put(in6_dev); |
2492 | return; | |
2493 | } | |
2494 | ||
8a226b2c JB |
2495 | update_lft = 0; |
2496 | create = 1; | |
c15b1cca HFS |
2497 | spin_lock_bh(&ifp->lock); |
2498 | ifp->flags |= IFA_F_MANAGETEMPADDR; | |
1da177e4 | 2499 | ifp->cstamp = jiffies; |
617fe29d | 2500 | ifp->tokenized = tokenized; |
c15b1cca | 2501 | spin_unlock_bh(&ifp->lock); |
cf22f9a2 | 2502 | addrconf_dad_start(ifp); |
1da177e4 LT |
2503 | } |
2504 | ||
2505 | if (ifp) { | |
479840ff | 2506 | u32 flags; |
1da177e4 | 2507 | unsigned long now; |
1da177e4 LT |
2508 | u32 stored_lft; |
2509 | ||
2510 | /* update lifetime (RFC2462 5.5.3 e) */ | |
8e8e676d | 2511 | spin_lock_bh(&ifp->lock); |
1da177e4 LT |
2512 | now = jiffies; |
2513 | if (ifp->valid_lft > (now - ifp->tstamp) / HZ) | |
2514 | stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; | |
2515 | else | |
2516 | stored_lft = 0; | |
8a226b2c | 2517 | if (!update_lft && !create && stored_lft) { |
9451a304 FF |
2518 | const u32 minimum_lft = min_t(u32, |
2519 | stored_lft, MIN_VALID_LIFETIME); | |
c9d55d5b PM |
2520 | valid_lft = max(valid_lft, minimum_lft); |
2521 | ||
2522 | /* RFC4862 Section 5.5.3e: | |
2523 | * "Note that the preferred lifetime of the | |
2524 | * corresponding address is always reset to | |
2525 | * the Preferred Lifetime in the received | |
2526 | * Prefix Information option, regardless of | |
2527 | * whether the valid lifetime is also reset or | |
2528 | * ignored." | |
2529 | * | |
2530 | * So we should always update prefered_lft here. | |
2531 | */ | |
2532 | update_lft = 1; | |
1da177e4 LT |
2533 | } |
2534 | ||
2535 | if (update_lft) { | |
2536 | ifp->valid_lft = valid_lft; | |
2537 | ifp->prefered_lft = prefered_lft; | |
2538 | ifp->tstamp = now; | |
2539 | flags = ifp->flags; | |
2540 | ifp->flags &= ~IFA_F_DEPRECATED; | |
8e8e676d | 2541 | spin_unlock_bh(&ifp->lock); |
1da177e4 LT |
2542 | |
2543 | if (!(flags&IFA_F_TENTATIVE)) | |
2544 | ipv6_ifa_notify(0, ifp); | |
2545 | } else | |
8e8e676d | 2546 | spin_unlock_bh(&ifp->lock); |
1da177e4 | 2547 | |
53bd6749 JP |
2548 | manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, |
2549 | create, now); | |
5d9efa7e | 2550 | |
1da177e4 | 2551 | in6_ifa_put(ifp); |
c15b1cca | 2552 | addrconf_verify(); |
1da177e4 LT |
2553 | } |
2554 | } | |
2555 | inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); | |
2556 | in6_dev_put(in6_dev); | |
2557 | } | |
2558 | ||
2559 | /* | |
2560 | * Set destination address. | |
2561 | * Special case for SIT interfaces where we create a new "virtual" | |
2562 | * device. | |
2563 | */ | |
af284937 | 2564 | int addrconf_set_dstaddr(struct net *net, void __user *arg) |
1da177e4 LT |
2565 | { |
2566 | struct in6_ifreq ireq; | |
2567 | struct net_device *dev; | |
2568 | int err = -EINVAL; | |
2569 | ||
2570 | rtnl_lock(); | |
2571 | ||
2572 | err = -EFAULT; | |
2573 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) | |
2574 | goto err_exit; | |
2575 | ||
af284937 | 2576 | dev = __dev_get_by_index(net, ireq.ifr6_ifindex); |
1da177e4 LT |
2577 | |
2578 | err = -ENODEV; | |
63159f29 | 2579 | if (!dev) |
1da177e4 LT |
2580 | goto err_exit; |
2581 | ||
07a93626 | 2582 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
1da177e4 | 2583 | if (dev->type == ARPHRD_SIT) { |
5bc3eb7e | 2584 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 2585 | struct ifreq ifr; |
1da177e4 LT |
2586 | struct ip_tunnel_parm p; |
2587 | ||
2588 | err = -EADDRNOTAVAIL; | |
2589 | if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) | |
2590 | goto err_exit; | |
2591 | ||
2592 | memset(&p, 0, sizeof(p)); | |
2593 | p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; | |
2594 | p.iph.saddr = 0; | |
2595 | p.iph.version = 4; | |
2596 | p.iph.ihl = 5; | |
2597 | p.iph.protocol = IPPROTO_IPV6; | |
2598 | p.iph.ttl = 64; | |
d20b3109 | 2599 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; |
1da177e4 | 2600 | |
5bc3eb7e SH |
2601 | if (ops->ndo_do_ioctl) { |
2602 | mm_segment_t oldfs = get_fs(); | |
2603 | ||
2604 | set_fs(KERNEL_DS); | |
2605 | err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); | |
2606 | set_fs(oldfs); | |
2607 | } else | |
2608 | err = -EOPNOTSUPP; | |
1da177e4 LT |
2609 | |
2610 | if (err == 0) { | |
2611 | err = -ENOBUFS; | |
af284937 DL |
2612 | dev = __dev_get_by_name(net, p.name); |
2613 | if (!dev) | |
1da177e4 LT |
2614 | goto err_exit; |
2615 | err = dev_open(dev); | |
2616 | } | |
2617 | } | |
0be669bb | 2618 | #endif |
1da177e4 LT |
2619 | |
2620 | err_exit: | |
2621 | rtnl_unlock(); | |
2622 | return err; | |
2623 | } | |
2624 | ||
93a714d6 MC |
2625 | static int ipv6_mc_config(struct sock *sk, bool join, |
2626 | const struct in6_addr *addr, int ifindex) | |
2627 | { | |
2628 | int ret; | |
2629 | ||
2630 | ASSERT_RTNL(); | |
2631 | ||
2632 | lock_sock(sk); | |
2633 | if (join) | |
54ff9ef3 | 2634 | ret = ipv6_sock_mc_join(sk, ifindex, addr); |
93a714d6 | 2635 | else |
54ff9ef3 | 2636 | ret = ipv6_sock_mc_drop(sk, ifindex, addr); |
93a714d6 MC |
2637 | release_sock(sk); |
2638 | ||
2639 | return ret; | |
2640 | } | |
2641 | ||
1da177e4 LT |
2642 | /* |
2643 | * Manual configuration of address on an interface | |
2644 | */ | |
479840ff JP |
2645 | static int inet6_addr_add(struct net *net, int ifindex, |
2646 | const struct in6_addr *pfx, | |
caeaba79 | 2647 | const struct in6_addr *peer_pfx, |
479840ff JP |
2648 | unsigned int plen, __u32 ifa_flags, |
2649 | __u32 prefered_lft, __u32 valid_lft) | |
1da177e4 LT |
2650 | { |
2651 | struct inet6_ifaddr *ifp; | |
2652 | struct inet6_dev *idev; | |
2653 | struct net_device *dev; | |
93a714d6 MC |
2654 | unsigned long timeout; |
2655 | clock_t expires; | |
1da177e4 | 2656 | int scope; |
6f704992 | 2657 | u32 flags; |
1da177e4 LT |
2658 | |
2659 | ASSERT_RTNL(); | |
1ab1457c | 2660 | |
24ef0da7 TG |
2661 | if (plen > 128) |
2662 | return -EINVAL; | |
2663 | ||
0778769d NT |
2664 | /* check the lifetime */ |
2665 | if (!valid_lft || prefered_lft > valid_lft) | |
2666 | return -EINVAL; | |
2667 | ||
53bd6749 JP |
2668 | if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) |
2669 | return -EINVAL; | |
2670 | ||
af284937 DL |
2671 | dev = __dev_get_by_index(net, ifindex); |
2672 | if (!dev) | |
1da177e4 | 2673 | return -ENODEV; |
1ab1457c | 2674 | |
64e724f6 BH |
2675 | idev = addrconf_add_dev(dev); |
2676 | if (IS_ERR(idev)) | |
2677 | return PTR_ERR(idev); | |
1da177e4 | 2678 | |
93a714d6 MC |
2679 | if (ifa_flags & IFA_F_MCAUTOJOIN) { |
2680 | int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, | |
2681 | true, pfx, ifindex); | |
2682 | ||
2683 | if (ret < 0) | |
2684 | return ret; | |
2685 | } | |
2686 | ||
1da177e4 LT |
2687 | scope = ipv6_addr_scope(pfx); |
2688 | ||
4bed72e4 YH |
2689 | timeout = addrconf_timeout_fixup(valid_lft, HZ); |
2690 | if (addrconf_finite_timeout(timeout)) { | |
2691 | expires = jiffies_to_clock_t(timeout * HZ); | |
2692 | valid_lft = timeout; | |
6f704992 | 2693 | flags = RTF_EXPIRES; |
4bed72e4 YH |
2694 | } else { |
2695 | expires = 0; | |
2696 | flags = 0; | |
2697 | ifa_flags |= IFA_F_PERMANENT; | |
6f704992 | 2698 | } |
0778769d | 2699 | |
4bed72e4 YH |
2700 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); |
2701 | if (addrconf_finite_timeout(timeout)) { | |
2702 | if (timeout == 0) | |
2703 | ifa_flags |= IFA_F_DEPRECATED; | |
2704 | prefered_lft = timeout; | |
2705 | } | |
0778769d | 2706 | |
8a226b2c JB |
2707 | ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, |
2708 | valid_lft, prefered_lft); | |
0778769d | 2709 | |
1da177e4 | 2710 | if (!IS_ERR(ifp)) { |
761aac73 TH |
2711 | if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { |
2712 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, | |
2713 | expires, flags); | |
2714 | } | |
2715 | ||
95c385b4 NH |
2716 | /* |
2717 | * Note that section 3.1 of RFC 4429 indicates | |
2718 | * that the Optimistic flag should not be set for | |
2719 | * manually configured addresses | |
2720 | */ | |
cf22f9a2 | 2721 | addrconf_dad_start(ifp); |
53bd6749 JP |
2722 | if (ifa_flags & IFA_F_MANAGETEMPADDR) |
2723 | manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, | |
2724 | true, jiffies); | |
1da177e4 | 2725 | in6_ifa_put(ifp); |
c15b1cca | 2726 | addrconf_verify_rtnl(); |
1da177e4 | 2727 | return 0; |
93a714d6 MC |
2728 | } else if (ifa_flags & IFA_F_MCAUTOJOIN) { |
2729 | ipv6_mc_config(net->ipv6.mc_autojoin_sk, | |
2730 | false, pfx, ifindex); | |
1da177e4 LT |
2731 | } |
2732 | ||
2733 | return PTR_ERR(ifp); | |
2734 | } | |
2735 | ||
6046d5b4 HK |
2736 | static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, |
2737 | const struct in6_addr *pfx, unsigned int plen) | |
1da177e4 LT |
2738 | { |
2739 | struct inet6_ifaddr *ifp; | |
2740 | struct inet6_dev *idev; | |
2741 | struct net_device *dev; | |
1ab1457c | 2742 | |
24ef0da7 TG |
2743 | if (plen > 128) |
2744 | return -EINVAL; | |
2745 | ||
af284937 DL |
2746 | dev = __dev_get_by_index(net, ifindex); |
2747 | if (!dev) | |
1da177e4 LT |
2748 | return -ENODEV; |
2749 | ||
e5d08d71 | 2750 | idev = __in6_dev_get(dev); |
63159f29 | 2751 | if (!idev) |
1da177e4 LT |
2752 | return -ENXIO; |
2753 | ||
2754 | read_lock_bh(&idev->lock); | |
502a2ffd | 2755 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
1da177e4 LT |
2756 | if (ifp->prefix_len == plen && |
2757 | ipv6_addr_equal(pfx, &ifp->addr)) { | |
2758 | in6_ifa_hold(ifp); | |
2759 | read_unlock_bh(&idev->lock); | |
1ab1457c | 2760 | |
6046d5b4 HK |
2761 | if (!(ifp->flags & IFA_F_TEMPORARY) && |
2762 | (ifa_flags & IFA_F_MANAGETEMPADDR)) | |
2763 | manage_tempaddrs(idev, ifp, 0, 0, false, | |
2764 | jiffies); | |
1da177e4 | 2765 | ipv6_del_addr(ifp); |
6046d5b4 | 2766 | addrconf_verify_rtnl(); |
93a714d6 MC |
2767 | if (ipv6_addr_is_multicast(pfx)) { |
2768 | ipv6_mc_config(net->ipv6.mc_autojoin_sk, | |
2769 | false, pfx, dev->ifindex); | |
2770 | } | |
1da177e4 LT |
2771 | return 0; |
2772 | } | |
2773 | } | |
2774 | read_unlock_bh(&idev->lock); | |
2775 | return -EADDRNOTAVAIL; | |
2776 | } | |
2777 | ||
2778 | ||
af284937 | 2779 | int addrconf_add_ifaddr(struct net *net, void __user *arg) |
1da177e4 LT |
2780 | { |
2781 | struct in6_ifreq ireq; | |
2782 | int err; | |
1ab1457c | 2783 | |
af31f412 | 2784 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 2785 | return -EPERM; |
1ab1457c | 2786 | |
1da177e4 LT |
2787 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
2788 | return -EFAULT; | |
2789 | ||
2790 | rtnl_lock(); | |
caeaba79 | 2791 | err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, |
af284937 DL |
2792 | ireq.ifr6_prefixlen, IFA_F_PERMANENT, |
2793 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); | |
1da177e4 LT |
2794 | rtnl_unlock(); |
2795 | return err; | |
2796 | } | |
2797 | ||
af284937 | 2798 | int addrconf_del_ifaddr(struct net *net, void __user *arg) |
1da177e4 LT |
2799 | { |
2800 | struct in6_ifreq ireq; | |
2801 | int err; | |
1ab1457c | 2802 | |
af31f412 | 2803 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
2804 | return -EPERM; |
2805 | ||
2806 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) | |
2807 | return -EFAULT; | |
2808 | ||
2809 | rtnl_lock(); | |
6046d5b4 | 2810 | err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, |
af284937 | 2811 | ireq.ifr6_prefixlen); |
1da177e4 LT |
2812 | rtnl_unlock(); |
2813 | return err; | |
2814 | } | |
2815 | ||
b5f348e5 IJ |
2816 | static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, |
2817 | int plen, int scope) | |
2818 | { | |
2819 | struct inet6_ifaddr *ifp; | |
2820 | ||
8a226b2c | 2821 | ifp = ipv6_add_addr(idev, addr, NULL, plen, |
07edd741 HFS |
2822 | scope, IFA_F_PERMANENT, |
2823 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); | |
b5f348e5 IJ |
2824 | if (!IS_ERR(ifp)) { |
2825 | spin_lock_bh(&ifp->lock); | |
2826 | ifp->flags &= ~IFA_F_TENTATIVE; | |
2827 | spin_unlock_bh(&ifp->lock); | |
2828 | ipv6_ifa_notify(RTM_NEWADDR, ifp); | |
2829 | in6_ifa_put(ifp); | |
2830 | } | |
2831 | } | |
2832 | ||
07a93626 | 2833 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
1da177e4 LT |
2834 | static void sit_add_v4_addrs(struct inet6_dev *idev) |
2835 | { | |
1da177e4 LT |
2836 | struct in6_addr addr; |
2837 | struct net_device *dev; | |
c346dca1 | 2838 | struct net *net = dev_net(idev->dev); |
929c9cf3 | 2839 | int scope, plen; |
f0e2acfa | 2840 | u32 pflags = 0; |
1da177e4 LT |
2841 | |
2842 | ASSERT_RTNL(); | |
2843 | ||
2844 | memset(&addr, 0, sizeof(struct in6_addr)); | |
2845 | memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); | |
2846 | ||
2847 | if (idev->dev->flags&IFF_POINTOPOINT) { | |
2848 | addr.s6_addr32[0] = htonl(0xfe800000); | |
2849 | scope = IFA_LINK; | |
929c9cf3 | 2850 | plen = 64; |
1da177e4 LT |
2851 | } else { |
2852 | scope = IPV6_ADDR_COMPATv4; | |
929c9cf3 | 2853 | plen = 96; |
f0e2acfa | 2854 | pflags |= RTF_NONEXTHOP; |
1da177e4 LT |
2855 | } |
2856 | ||
2857 | if (addr.s6_addr32[3]) { | |
929c9cf3 | 2858 | add_addr(idev, &addr, plen, scope); |
f0e2acfa | 2859 | addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); |
1da177e4 LT |
2860 | return; |
2861 | } | |
2862 | ||
6fda7350 | 2863 | for_each_netdev(net, dev) { |
8e5e8f30 | 2864 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
1da177e4 | 2865 | if (in_dev && (dev->flags & IFF_UP)) { |
8e5e8f30 | 2866 | struct in_ifaddr *ifa; |
1da177e4 LT |
2867 | |
2868 | int flag = scope; | |
2869 | ||
2870 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { | |
1da177e4 LT |
2871 | |
2872 | addr.s6_addr32[3] = ifa->ifa_local; | |
2873 | ||
2874 | if (ifa->ifa_scope == RT_SCOPE_LINK) | |
2875 | continue; | |
2876 | if (ifa->ifa_scope >= RT_SCOPE_HOST) { | |
2877 | if (idev->dev->flags&IFF_POINTOPOINT) | |
2878 | continue; | |
2879 | flag |= IFA_HOST; | |
2880 | } | |
1da177e4 | 2881 | |
b5f348e5 | 2882 | add_addr(idev, &addr, plen, flag); |
f0e2acfa ND |
2883 | addrconf_prefix_route(&addr, plen, idev->dev, 0, |
2884 | pflags); | |
1da177e4 LT |
2885 | } |
2886 | } | |
1ab1457c | 2887 | } |
1da177e4 | 2888 | } |
0be669bb | 2889 | #endif |
1da177e4 LT |
2890 | |
2891 | static void init_loopback(struct net_device *dev) | |
2892 | { | |
2893 | struct inet6_dev *idev; | |
25fb6ca4 BK |
2894 | struct net_device *sp_dev; |
2895 | struct inet6_ifaddr *sp_ifa; | |
2896 | struct rt6_info *sp_rt; | |
1da177e4 LT |
2897 | |
2898 | /* ::1 */ | |
2899 | ||
2900 | ASSERT_RTNL(); | |
2901 | ||
e5d08d71 | 2902 | idev = ipv6_find_idev(dev); |
63159f29 | 2903 | if (!idev) { |
91df42be | 2904 | pr_debug("%s: add_dev failed\n", __func__); |
1da177e4 LT |
2905 | return; |
2906 | } | |
2907 | ||
b5f348e5 | 2908 | add_addr(idev, &in6addr_loopback, 128, IFA_HOST); |
25fb6ca4 BK |
2909 | |
2910 | /* Add routes to other interface's IPv6 addresses */ | |
2911 | for_each_netdev(dev_net(dev), sp_dev) { | |
2912 | if (!strcmp(sp_dev->name, dev->name)) | |
2913 | continue; | |
2914 | ||
2915 | idev = __in6_dev_get(sp_dev); | |
2916 | if (!idev) | |
2917 | continue; | |
2918 | ||
2919 | read_lock_bh(&idev->lock); | |
2920 | list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { | |
2921 | ||
2922 | if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) | |
2923 | continue; | |
2924 | ||
33d99113 G |
2925 | if (sp_ifa->rt) { |
2926 | /* This dst has been added to garbage list when | |
2927 | * lo device down, release this obsolete dst and | |
2928 | * reallocate a new router for ifa. | |
2929 | */ | |
2930 | if (sp_ifa->rt->dst.obsolete > 0) { | |
2931 | ip6_rt_put(sp_ifa->rt); | |
2932 | sp_ifa->rt = NULL; | |
2933 | } else { | |
2934 | continue; | |
2935 | } | |
2936 | } | |
a881ae1f | 2937 | |
57ec0afe | 2938 | sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); |
25fb6ca4 BK |
2939 | |
2940 | /* Failure cases are ignored */ | |
534c8779 G |
2941 | if (!IS_ERR(sp_rt)) { |
2942 | sp_ifa->rt = sp_rt; | |
25fb6ca4 | 2943 | ip6_ins_rt(sp_rt); |
534c8779 | 2944 | } |
25fb6ca4 BK |
2945 | } |
2946 | read_unlock_bh(&idev->lock); | |
2947 | } | |
1da177e4 LT |
2948 | } |
2949 | ||
64236f3f HFS |
2950 | static void addrconf_add_linklocal(struct inet6_dev *idev, |
2951 | const struct in6_addr *addr, u32 flags) | |
1da177e4 | 2952 | { |
8e5e8f30 | 2953 | struct inet6_ifaddr *ifp; |
64236f3f | 2954 | u32 addr_flags = flags | IFA_F_PERMANENT; |
95c385b4 NH |
2955 | |
2956 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD | |
2957 | if (idev->cnf.optimistic_dad && | |
702beb87 | 2958 | !dev_net(idev->dev)->ipv6.devconf_all->forwarding) |
95c385b4 NH |
2959 | addr_flags |= IFA_F_OPTIMISTIC; |
2960 | #endif | |
1da177e4 | 2961 | |
07edd741 HFS |
2962 | ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, |
2963 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); | |
1da177e4 | 2964 | if (!IS_ERR(ifp)) { |
46d48046 | 2965 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); |
cf22f9a2 | 2966 | addrconf_dad_start(ifp); |
1da177e4 LT |
2967 | in6_ifa_put(ifp); |
2968 | } | |
2969 | } | |
2970 | ||
622c81d5 HFS |
2971 | static bool ipv6_reserved_interfaceid(struct in6_addr address) |
2972 | { | |
2973 | if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) | |
2974 | return true; | |
2975 | ||
2976 | if (address.s6_addr32[2] == htonl(0x02005eff) && | |
2977 | ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) | |
2978 | return true; | |
2979 | ||
2980 | if (address.s6_addr32[2] == htonl(0xfdffffff) && | |
2981 | ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) | |
2982 | return true; | |
2983 | ||
2984 | return false; | |
2985 | } | |
2986 | ||
2987 | static int ipv6_generate_stable_address(struct in6_addr *address, | |
2988 | u8 dad_count, | |
2989 | const struct inet6_dev *idev) | |
2990 | { | |
622c81d5 HFS |
2991 | static DEFINE_SPINLOCK(lock); |
2992 | static __u32 digest[SHA_DIGEST_WORDS]; | |
2993 | static __u32 workspace[SHA_WORKSPACE_WORDS]; | |
2994 | ||
2995 | static union { | |
2996 | char __data[SHA_MESSAGE_BYTES]; | |
2997 | struct { | |
2998 | struct in6_addr secret; | |
ff40217e | 2999 | __be32 prefix[2]; |
622c81d5 HFS |
3000 | unsigned char hwaddr[MAX_ADDR_LEN]; |
3001 | u8 dad_count; | |
3002 | } __packed; | |
3003 | } data; | |
3004 | ||
3005 | struct in6_addr secret; | |
3006 | struct in6_addr temp; | |
3007 | struct net *net = dev_net(idev->dev); | |
3008 | ||
3009 | BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); | |
3010 | ||
3011 | if (idev->cnf.stable_secret.initialized) | |
3012 | secret = idev->cnf.stable_secret.secret; | |
3013 | else if (net->ipv6.devconf_dflt->stable_secret.initialized) | |
3014 | secret = net->ipv6.devconf_dflt->stable_secret.secret; | |
3015 | else | |
3016 | return -1; | |
3017 | ||
3018 | retry: | |
3019 | spin_lock_bh(&lock); | |
3020 | ||
3021 | sha_init(digest); | |
3022 | memset(&data, 0, sizeof(data)); | |
3023 | memset(workspace, 0, sizeof(workspace)); | |
3024 | memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); | |
ff40217e HFS |
3025 | data.prefix[0] = address->s6_addr32[0]; |
3026 | data.prefix[1] = address->s6_addr32[1]; | |
622c81d5 HFS |
3027 | data.secret = secret; |
3028 | data.dad_count = dad_count; | |
3029 | ||
3030 | sha_transform(digest, data.__data, workspace); | |
3031 | ||
3032 | temp = *address; | |
ff40217e HFS |
3033 | temp.s6_addr32[2] = (__force __be32)digest[0]; |
3034 | temp.s6_addr32[3] = (__force __be32)digest[1]; | |
622c81d5 HFS |
3035 | |
3036 | spin_unlock_bh(&lock); | |
3037 | ||
3038 | if (ipv6_reserved_interfaceid(temp)) { | |
3039 | dad_count++; | |
1855b7c3 | 3040 | if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) |
622c81d5 HFS |
3041 | return -1; |
3042 | goto retry; | |
3043 | } | |
3044 | ||
3045 | *address = temp; | |
3046 | return 0; | |
3047 | } | |
3048 | ||
cc9da6cc BM |
3049 | static void ipv6_gen_mode_random_init(struct inet6_dev *idev) |
3050 | { | |
3051 | struct ipv6_stable_secret *s = &idev->cnf.stable_secret; | |
3052 | ||
3053 | if (s->initialized) | |
3054 | return; | |
3055 | s = &idev->cnf.stable_secret; | |
3056 | get_random_bytes(&s->secret, sizeof(s->secret)); | |
3057 | s->initialized = true; | |
3058 | } | |
3059 | ||
bc91b0f0 JP |
3060 | static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) |
3061 | { | |
622c81d5 HFS |
3062 | struct in6_addr addr; |
3063 | ||
ca254490 DA |
3064 | /* no link local addresses on L3 master devices */ |
3065 | if (netif_is_l3_master(idev->dev)) | |
3066 | return; | |
3067 | ||
622c81d5 | 3068 | ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); |
bc91b0f0 | 3069 | |
cc9da6cc BM |
3070 | switch (idev->addr_gen_mode) { |
3071 | case IN6_ADDR_GEN_MODE_RANDOM: | |
3072 | ipv6_gen_mode_random_init(idev); | |
3073 | /* fallthrough */ | |
3074 | case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: | |
622c81d5 | 3075 | if (!ipv6_generate_stable_address(&addr, 0, idev)) |
64236f3f HFS |
3076 | addrconf_add_linklocal(idev, &addr, |
3077 | IFA_F_STABLE_PRIVACY); | |
622c81d5 HFS |
3078 | else if (prefix_route) |
3079 | addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); | |
cc9da6cc BM |
3080 | break; |
3081 | case IN6_ADDR_GEN_MODE_EUI64: | |
bc91b0f0 JP |
3082 | /* addrconf_add_linklocal also adds a prefix_route and we |
3083 | * only need to care about prefix routes if ipv6_generate_eui64 | |
3084 | * couldn't generate one. | |
3085 | */ | |
3086 | if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) | |
64236f3f | 3087 | addrconf_add_linklocal(idev, &addr, 0); |
bc91b0f0 JP |
3088 | else if (prefix_route) |
3089 | addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); | |
cc9da6cc BM |
3090 | break; |
3091 | case IN6_ADDR_GEN_MODE_NONE: | |
3092 | default: | |
3093 | /* will not add any link local address */ | |
3094 | break; | |
bc91b0f0 JP |
3095 | } |
3096 | } | |
3097 | ||
1da177e4 LT |
3098 | static void addrconf_dev_config(struct net_device *dev) |
3099 | { | |
8e5e8f30 | 3100 | struct inet6_dev *idev; |
1da177e4 LT |
3101 | |
3102 | ASSERT_RTNL(); | |
3103 | ||
74235a25 HX |
3104 | if ((dev->type != ARPHRD_ETHER) && |
3105 | (dev->type != ARPHRD_FDDI) && | |
74235a25 | 3106 | (dev->type != ARPHRD_ARCNET) && |
06a4c1c5 | 3107 | (dev->type != ARPHRD_INFINIBAND) && |
e837735e | 3108 | (dev->type != ARPHRD_IEEE1394) && |
e74bccb8 | 3109 | (dev->type != ARPHRD_TUNNEL6) && |
cc9da6cc BM |
3110 | (dev->type != ARPHRD_6LOWPAN) && |
3111 | (dev->type != ARPHRD_NONE)) { | |
74235a25 HX |
3112 | /* Alas, we support only Ethernet autoconfiguration. */ |
3113 | return; | |
3114 | } | |
3115 | ||
1da177e4 | 3116 | idev = addrconf_add_dev(dev); |
64e724f6 | 3117 | if (IS_ERR(idev)) |
1da177e4 LT |
3118 | return; |
3119 | ||
cc9da6cc BM |
3120 | /* this device type has no EUI support */ |
3121 | if (dev->type == ARPHRD_NONE && | |
3122 | idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) | |
3123 | idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; | |
3124 | ||
bc91b0f0 | 3125 | addrconf_addr_gen(idev, false); |
1da177e4 LT |
3126 | } |
3127 | ||
07a93626 | 3128 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
1da177e4 LT |
3129 | static void addrconf_sit_config(struct net_device *dev) |
3130 | { | |
3131 | struct inet6_dev *idev; | |
3132 | ||
3133 | ASSERT_RTNL(); | |
3134 | ||
1ab1457c YH |
3135 | /* |
3136 | * Configure the tunnel with one of our IPv4 | |
3137 | * addresses... we should configure all of | |
1da177e4 LT |
3138 | * our v4 addrs in the tunnel |
3139 | */ | |
3140 | ||
e5d08d71 | 3141 | idev = ipv6_find_idev(dev); |
63159f29 | 3142 | if (!idev) { |
91df42be | 3143 | pr_debug("%s: add_dev failed\n", __func__); |
1da177e4 LT |
3144 | return; |
3145 | } | |
3146 | ||
c7dc89c0 | 3147 | if (dev->priv_flags & IFF_ISATAP) { |
bc91b0f0 | 3148 | addrconf_addr_gen(idev, false); |
c7dc89c0 FT |
3149 | return; |
3150 | } | |
3151 | ||
1da177e4 LT |
3152 | sit_add_v4_addrs(idev); |
3153 | ||
62b54dd9 | 3154 | if (dev->flags&IFF_POINTOPOINT) |
1da177e4 | 3155 | addrconf_add_mroute(dev); |
1da177e4 | 3156 | } |
0be669bb | 3157 | #endif |
1da177e4 | 3158 | |
07a93626 | 3159 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
aee80b54 | 3160 | static void addrconf_gre_config(struct net_device *dev) |
3161 | { | |
3162 | struct inet6_dev *idev; | |
aee80b54 | 3163 | |
aee80b54 | 3164 | ASSERT_RTNL(); |
3165 | ||
e5d08d71 | 3166 | idev = ipv6_find_idev(dev); |
63159f29 | 3167 | if (!idev) { |
91df42be | 3168 | pr_debug("%s: add_dev failed\n", __func__); |
aee80b54 | 3169 | return; |
3170 | } | |
3171 | ||
bc91b0f0 | 3172 | addrconf_addr_gen(idev, true); |
d9e4ce65 HFS |
3173 | if (dev->flags & IFF_POINTOPOINT) |
3174 | addrconf_add_mroute(dev); | |
aee80b54 | 3175 | } |
3176 | #endif | |
3177 | ||
f1705ec1 DA |
3178 | static int fixup_permanent_addr(struct inet6_dev *idev, |
3179 | struct inet6_ifaddr *ifp) | |
3180 | { | |
3181 | if (!ifp->rt) { | |
3182 | struct rt6_info *rt; | |
3183 | ||
3184 | rt = addrconf_dst_alloc(idev, &ifp->addr, false); | |
3185 | if (unlikely(IS_ERR(rt))) | |
3186 | return PTR_ERR(rt); | |
3187 | ||
3188 | ifp->rt = rt; | |
3189 | } | |
3190 | ||
3191 | if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { | |
3192 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, | |
3193 | idev->dev, 0, 0); | |
3194 | } | |
3195 | ||
3196 | addrconf_dad_start(ifp); | |
3197 | ||
3198 | return 0; | |
3199 | } | |
3200 | ||
3201 | static void addrconf_permanent_addr(struct net_device *dev) | |
3202 | { | |
3203 | struct inet6_ifaddr *ifp, *tmp; | |
3204 | struct inet6_dev *idev; | |
3205 | ||
3206 | idev = __in6_dev_get(dev); | |
3207 | if (!idev) | |
3208 | return; | |
3209 | ||
3210 | write_lock_bh(&idev->lock); | |
3211 | ||
3212 | list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { | |
3213 | if ((ifp->flags & IFA_F_PERMANENT) && | |
3214 | fixup_permanent_addr(idev, ifp) < 0) { | |
3215 | write_unlock_bh(&idev->lock); | |
3216 | ipv6_del_addr(ifp); | |
3217 | write_lock_bh(&idev->lock); | |
3218 | ||
3219 | net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", | |
3220 | idev->dev->name, &ifp->addr); | |
3221 | } | |
3222 | } | |
3223 | ||
3224 | write_unlock_bh(&idev->lock); | |
3225 | } | |
3226 | ||
1ab1457c | 3227 | static int addrconf_notify(struct notifier_block *this, unsigned long event, |
351638e7 | 3228 | void *ptr) |
1da177e4 | 3229 | { |
351638e7 | 3230 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
4f7f34ea | 3231 | struct netdev_notifier_changeupper_info *info; |
748e2d93 | 3232 | struct inet6_dev *idev = __in6_dev_get(dev); |
c5e33bdd | 3233 | int run_pending = 0; |
b217d616 | 3234 | int err; |
1da177e4 | 3235 | |
e21e8467 | 3236 | switch (event) { |
45ba9dd2 | 3237 | case NETDEV_REGISTER: |
74235a25 | 3238 | if (!idev && dev->mtu >= IPV6_MIN_MTU) { |
45ba9dd2 | 3239 | idev = ipv6_add_dev(dev); |
a317a2f1 WC |
3240 | if (IS_ERR(idev)) |
3241 | return notifier_from_errno(PTR_ERR(idev)); | |
45ba9dd2 YH |
3242 | } |
3243 | break; | |
bcdd553f | 3244 | |
b7b0b1d2 AD |
3245 | case NETDEV_CHANGEMTU: |
3246 | /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ | |
3247 | if (dev->mtu < IPV6_MIN_MTU) { | |
3248 | addrconf_ifdown(dev, 1); | |
3249 | break; | |
3250 | } | |
3251 | ||
3252 | if (idev) { | |
3253 | rt6_mtu_change(dev, dev->mtu); | |
3254 | idev->cnf.mtu6 = dev->mtu; | |
3255 | break; | |
3256 | } | |
3257 | ||
3258 | /* allocate new idev */ | |
3259 | idev = ipv6_add_dev(dev); | |
3260 | if (IS_ERR(idev)) | |
3261 | break; | |
3262 | ||
3263 | /* device is still not ready */ | |
3264 | if (!(idev->if_flags & IF_READY)) | |
3265 | break; | |
3266 | ||
3267 | run_pending = 1; | |
3268 | ||
3269 | /* fall through */ | |
3270 | ||
1da177e4 | 3271 | case NETDEV_UP: |
3c21edbd | 3272 | case NETDEV_CHANGE: |
c2edacf8 JV |
3273 | if (dev->flags & IFF_SLAVE) |
3274 | break; | |
3275 | ||
3ce62a84 WC |
3276 | if (idev && idev->cnf.disable_ipv6) |
3277 | break; | |
3278 | ||
3c21edbd | 3279 | if (event == NETDEV_UP) { |
38bd10c4 DA |
3280 | /* restore routes for permanent addresses */ |
3281 | addrconf_permanent_addr(dev); | |
3282 | ||
f24e3d65 | 3283 | if (!addrconf_qdisc_ok(dev)) { |
3c21edbd | 3284 | /* device is not ready yet. */ |
f3213831 | 3285 | pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", |
3c21edbd YH |
3286 | dev->name); |
3287 | break; | |
3288 | } | |
99081049 | 3289 | |
d31c7b8f EP |
3290 | if (!idev && dev->mtu >= IPV6_MIN_MTU) |
3291 | idev = ipv6_add_dev(dev); | |
3292 | ||
a317a2f1 | 3293 | if (!IS_ERR_OR_NULL(idev)) { |
99081049 | 3294 | idev->if_flags |= IF_READY; |
e3ec6cfc BT |
3295 | run_pending = 1; |
3296 | } | |
b7b0b1d2 | 3297 | } else if (event == NETDEV_CHANGE) { |
f24e3d65 | 3298 | if (!addrconf_qdisc_ok(dev)) { |
3c21edbd YH |
3299 | /* device is still not ready. */ |
3300 | break; | |
3301 | } | |
3302 | ||
3303 | if (idev) { | |
bcdd553f | 3304 | if (idev->if_flags & IF_READY) |
3c21edbd YH |
3305 | /* device is already configured. */ |
3306 | break; | |
3c21edbd YH |
3307 | idev->if_flags |= IF_READY; |
3308 | } | |
3309 | ||
f3213831 JP |
3310 | pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", |
3311 | dev->name); | |
3c21edbd | 3312 | |
c5e33bdd | 3313 | run_pending = 1; |
3c21edbd YH |
3314 | } |
3315 | ||
e21e8467 | 3316 | switch (dev->type) { |
07a93626 | 3317 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
1da177e4 LT |
3318 | case ARPHRD_SIT: |
3319 | addrconf_sit_config(dev); | |
3320 | break; | |
aee80b54 | 3321 | #endif |
07a93626 | 3322 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
aee80b54 | 3323 | case ARPHRD_IPGRE: |
3324 | addrconf_gre_config(dev); | |
3325 | break; | |
0be669bb | 3326 | #endif |
1da177e4 LT |
3327 | case ARPHRD_LOOPBACK: |
3328 | init_loopback(dev); | |
3329 | break; | |
3330 | ||
3331 | default: | |
3332 | addrconf_dev_config(dev); | |
3333 | break; | |
3ff50b79 | 3334 | } |
bcdd553f | 3335 | |
a317a2f1 | 3336 | if (!IS_ERR_OR_NULL(idev)) { |
c5e33bdd YH |
3337 | if (run_pending) |
3338 | addrconf_dad_run(idev); | |
3339 | ||
bcdd553f SH |
3340 | /* |
3341 | * If the MTU changed during the interface down, | |
3342 | * when the interface up, the changed MTU must be | |
3343 | * reflected in the idev as well as routers. | |
1da177e4 | 3344 | */ |
bcdd553f SH |
3345 | if (idev->cnf.mtu6 != dev->mtu && |
3346 | dev->mtu >= IPV6_MIN_MTU) { | |
1da177e4 LT |
3347 | rt6_mtu_change(dev, dev->mtu); |
3348 | idev->cnf.mtu6 = dev->mtu; | |
3349 | } | |
3350 | idev->tstamp = jiffies; | |
3351 | inet6_ifinfo_notify(RTM_NEWLINK, idev); | |
bcdd553f SH |
3352 | |
3353 | /* | |
3354 | * If the changed mtu during down is lower than | |
3355 | * IPV6_MIN_MTU stop IPv6 on this interface. | |
1da177e4 LT |
3356 | */ |
3357 | if (dev->mtu < IPV6_MIN_MTU) | |
bcdd553f | 3358 | addrconf_ifdown(dev, 1); |
1da177e4 LT |
3359 | } |
3360 | break; | |
3361 | ||
1da177e4 LT |
3362 | case NETDEV_DOWN: |
3363 | case NETDEV_UNREGISTER: | |
3364 | /* | |
3365 | * Remove all addresses from this interface. | |
3366 | */ | |
3367 | addrconf_ifdown(dev, event != NETDEV_DOWN); | |
3368 | break; | |
3c21edbd | 3369 | |
1da177e4 | 3370 | case NETDEV_CHANGENAME: |
1da177e4 | 3371 | if (idev) { |
5632c515 | 3372 | snmp6_unregister_dev(idev); |
408c4768 | 3373 | addrconf_sysctl_unregister(idev); |
a317a2f1 | 3374 | err = addrconf_sysctl_register(idev); |
b217d616 HX |
3375 | if (err) |
3376 | return notifier_from_errno(err); | |
a317a2f1 WC |
3377 | err = snmp6_register_dev(idev); |
3378 | if (err) { | |
3379 | addrconf_sysctl_unregister(idev); | |
3380 | return notifier_from_errno(err); | |
3381 | } | |
5632c515 | 3382 | } |
1da177e4 | 3383 | break; |
bcdd553f | 3384 | |
93d9b7d7 JP |
3385 | case NETDEV_PRE_TYPE_CHANGE: |
3386 | case NETDEV_POST_TYPE_CHANGE: | |
3ef0952c AL |
3387 | if (idev) |
3388 | addrconf_type_change(dev, event); | |
75c78500 | 3389 | break; |
4f7f34ea DA |
3390 | |
3391 | case NETDEV_CHANGEUPPER: | |
3392 | info = ptr; | |
3393 | ||
3394 | /* flush all routes if dev is linked to or unlinked from | |
3395 | * an L3 master device (e.g., VRF) | |
3396 | */ | |
3397 | if (info->upper_dev && netif_is_l3_master(info->upper_dev)) | |
3398 | addrconf_ifdown(dev, 0); | |
3ff50b79 | 3399 | } |
1da177e4 LT |
3400 | |
3401 | return NOTIFY_OK; | |
3402 | } | |
3403 | ||
3404 | /* | |
3405 | * addrconf module should be notified of a device going up | |
3406 | */ | |
3407 | static struct notifier_block ipv6_dev_notf = { | |
3408 | .notifier_call = addrconf_notify, | |
1da177e4 LT |
3409 | }; |
3410 | ||
93d9b7d7 | 3411 | static void addrconf_type_change(struct net_device *dev, unsigned long event) |
75c78500 MS |
3412 | { |
3413 | struct inet6_dev *idev; | |
3414 | ASSERT_RTNL(); | |
3415 | ||
3416 | idev = __in6_dev_get(dev); | |
3417 | ||
93d9b7d7 | 3418 | if (event == NETDEV_POST_TYPE_CHANGE) |
75c78500 | 3419 | ipv6_mc_remap(idev); |
93d9b7d7 | 3420 | else if (event == NETDEV_PRE_TYPE_CHANGE) |
75c78500 MS |
3421 | ipv6_mc_unmap(idev); |
3422 | } | |
3423 | ||
70af921d DA |
3424 | static bool addr_is_local(const struct in6_addr *addr) |
3425 | { | |
3426 | return ipv6_addr_type(addr) & | |
3427 | (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); | |
3428 | } | |
3429 | ||
1da177e4 LT |
3430 | static int addrconf_ifdown(struct net_device *dev, int how) |
3431 | { | |
c346dca1 | 3432 | struct net *net = dev_net(dev); |
502a2ffd | 3433 | struct inet6_dev *idev; |
f1705ec1 DA |
3434 | struct inet6_ifaddr *ifa, *tmp; |
3435 | struct list_head del_list; | |
3436 | int _keep_addr; | |
3437 | bool keep_addr; | |
73a8bd74 | 3438 | int state, i; |
1da177e4 LT |
3439 | |
3440 | ASSERT_RTNL(); | |
3441 | ||
73a8bd74 DM |
3442 | rt6_ifdown(net, dev); |
3443 | neigh_ifdown(&nd_tbl, dev); | |
1da177e4 LT |
3444 | |
3445 | idev = __in6_dev_get(dev); | |
63159f29 | 3446 | if (!idev) |
1da177e4 LT |
3447 | return -ENODEV; |
3448 | ||
bcdd553f SH |
3449 | /* |
3450 | * Step 1: remove reference to ipv6 device from parent device. | |
3451 | * Do not dev_put! | |
1da177e4 | 3452 | */ |
439e2385 | 3453 | if (how) { |
1da177e4 | 3454 | idev->dead = 1; |
8814c4b5 YH |
3455 | |
3456 | /* protected by rtnl_lock */ | |
a9b3cd7f | 3457 | RCU_INIT_POINTER(dev->ip6_ptr, NULL); |
1da177e4 LT |
3458 | |
3459 | /* Step 1.5: remove snmp6 entry */ | |
3460 | snmp6_unregister_dev(idev); | |
3461 | ||
3462 | } | |
3463 | ||
f1705ec1 DA |
3464 | /* aggregate the system setting and interface setting */ |
3465 | _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; | |
3466 | if (!_keep_addr) | |
3467 | _keep_addr = idev->cnf.keep_addr_on_down; | |
3468 | ||
3469 | /* combine the user config with event to determine if permanent | |
3470 | * addresses are to be removed from address hash table | |
3471 | */ | |
3472 | keep_addr = !(how || _keep_addr <= 0); | |
3473 | ||
73a8bd74 DM |
3474 | /* Step 2: clear hash table */ |
3475 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { | |
3476 | struct hlist_head *h = &inet6_addr_lst[i]; | |
73a8bd74 DM |
3477 | |
3478 | spin_lock_bh(&addrconf_hash_lock); | |
67ba4152 | 3479 | restart: |
b67bfe0d | 3480 | hlist_for_each_entry_rcu(ifa, h, addr_lst) { |
73a8bd74 | 3481 | if (ifa->idev == idev) { |
c15b1cca | 3482 | addrconf_del_dad_work(ifa); |
f1705ec1 DA |
3483 | /* combined flag + permanent flag decide if |
3484 | * address is retained on a down event | |
3485 | */ | |
3486 | if (!keep_addr || | |
70af921d DA |
3487 | !(ifa->flags & IFA_F_PERMANENT) || |
3488 | addr_is_local(&ifa->addr)) { | |
f1705ec1 DA |
3489 | hlist_del_init_rcu(&ifa->addr_lst); |
3490 | goto restart; | |
3491 | } | |
73a8bd74 DM |
3492 | } |
3493 | } | |
3494 | spin_unlock_bh(&addrconf_hash_lock); | |
3495 | } | |
3496 | ||
1da177e4 LT |
3497 | write_lock_bh(&idev->lock); |
3498 | ||
b7b1bfce HFS |
3499 | addrconf_del_rs_timer(idev); |
3500 | ||
bcdd553f | 3501 | /* Step 2: clear flags for stateless addrconf */ |
439e2385 | 3502 | if (!how) |
3c21edbd | 3503 | idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); |
1da177e4 | 3504 | |
439e2385 | 3505 | if (how && del_timer(&idev->regen_timer)) |
1da177e4 LT |
3506 | in6_dev_put(idev); |
3507 | ||
bcdd553f | 3508 | /* Step 3: clear tempaddr list */ |
372e6c8f | 3509 | while (!list_empty(&idev->tempaddr_list)) { |
3510 | ifa = list_first_entry(&idev->tempaddr_list, | |
3511 | struct inet6_ifaddr, tmp_list); | |
3512 | list_del(&ifa->tmp_list); | |
1da177e4 LT |
3513 | write_unlock_bh(&idev->lock); |
3514 | spin_lock_bh(&ifa->lock); | |
3515 | ||
3516 | if (ifa->ifpub) { | |
3517 | in6_ifa_put(ifa->ifpub); | |
3518 | ifa->ifpub = NULL; | |
3519 | } | |
3520 | spin_unlock_bh(&ifa->lock); | |
3521 | in6_ifa_put(ifa); | |
3522 | write_lock_bh(&idev->lock); | |
3523 | } | |
8f37ada5 | 3524 | |
f1705ec1 DA |
3525 | /* re-combine the user config with event to determine if permanent |
3526 | * addresses are to be removed from the interface list | |
3527 | */ | |
3528 | keep_addr = (!how && _keep_addr > 0); | |
84e8b803 | 3529 | |
f1705ec1 DA |
3530 | INIT_LIST_HEAD(&del_list); |
3531 | list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { | |
38bd10c4 DA |
3532 | struct rt6_info *rt = NULL; |
3533 | ||
f1705ec1 | 3534 | addrconf_del_dad_work(ifa); |
84e8b803 | 3535 | |
73a8bd74 | 3536 | write_unlock_bh(&idev->lock); |
8e8e676d | 3537 | spin_lock_bh(&ifa->lock); |
f1705ec1 | 3538 | |
70af921d DA |
3539 | if (keep_addr && (ifa->flags & IFA_F_PERMANENT) && |
3540 | !addr_is_local(&ifa->addr)) { | |
f1705ec1 DA |
3541 | /* set state to skip the notifier below */ |
3542 | state = INET6_IFADDR_STATE_DEAD; | |
3543 | ifa->state = 0; | |
3544 | if (!(ifa->flags & IFA_F_NODAD)) | |
3545 | ifa->flags |= IFA_F_TENTATIVE; | |
38bd10c4 DA |
3546 | |
3547 | rt = ifa->rt; | |
3548 | ifa->rt = NULL; | |
f1705ec1 DA |
3549 | } else { |
3550 | state = ifa->state; | |
3551 | ifa->state = INET6_IFADDR_STATE_DEAD; | |
3552 | ||
3553 | list_del(&ifa->if_list); | |
3554 | list_add(&ifa->if_list, &del_list); | |
3555 | } | |
3556 | ||
8e8e676d | 3557 | spin_unlock_bh(&ifa->lock); |
9d82ca98 | 3558 | |
38bd10c4 DA |
3559 | if (rt) |
3560 | ip6_del_rt(rt); | |
3561 | ||
73a8bd74 DM |
3562 | if (state != INET6_IFADDR_STATE_DEAD) { |
3563 | __ipv6_ifa_notify(RTM_DELADDR, ifa); | |
f88c91dd | 3564 | inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); |
27bdb2ab | 3565 | } |
8f37ada5 | 3566 | |
73a8bd74 DM |
3567 | write_lock_bh(&idev->lock); |
3568 | } | |
8f37ada5 | 3569 | |
1da177e4 LT |
3570 | write_unlock_bh(&idev->lock); |
3571 | ||
f1705ec1 DA |
3572 | /* now clean up addresses to be removed */ |
3573 | while (!list_empty(&del_list)) { | |
3574 | ifa = list_first_entry(&del_list, | |
3575 | struct inet6_ifaddr, if_list); | |
3576 | list_del(&ifa->if_list); | |
3577 | ||
3578 | in6_ifa_put(ifa); | |
3579 | } | |
3580 | ||
381f4dca SD |
3581 | /* Step 5: Discard anycast and multicast list */ |
3582 | if (how) { | |
3583 | ipv6_ac_destroy_dev(idev); | |
1da177e4 | 3584 | ipv6_mc_destroy_dev(idev); |
381f4dca | 3585 | } else { |
1da177e4 | 3586 | ipv6_mc_down(idev); |
381f4dca | 3587 | } |
1da177e4 | 3588 | |
1da177e4 | 3589 | idev->tstamp = jiffies; |
1ab1457c | 3590 | |
bcdd553f | 3591 | /* Last: Shot the device (if unregistered) */ |
439e2385 | 3592 | if (how) { |
408c4768 | 3593 | addrconf_sysctl_unregister(idev); |
1da177e4 LT |
3594 | neigh_parms_release(&nd_tbl, idev->nd_parms); |
3595 | neigh_ifdown(&nd_tbl, dev); | |
3596 | in6_dev_put(idev); | |
3597 | } | |
3598 | return 0; | |
3599 | } | |
3600 | ||
3601 | static void addrconf_rs_timer(unsigned long data) | |
3602 | { | |
b7b1bfce | 3603 | struct inet6_dev *idev = (struct inet6_dev *)data; |
caf92bc4 | 3604 | struct net_device *dev = idev->dev; |
b7b1bfce | 3605 | struct in6_addr lladdr; |
1da177e4 | 3606 | |
b7b1bfce | 3607 | write_lock(&idev->lock); |
5b2a1953 | 3608 | if (idev->dead || !(idev->if_flags & IF_READY)) |
1da177e4 LT |
3609 | goto out; |
3610 | ||
9ba2add3 | 3611 | if (!ipv6_accept_ra(idev)) |
5b2a1953 | 3612 | goto out; |
3613 | ||
3614 | /* Announcement received after solicitation was sent */ | |
3615 | if (idev->if_flags & IF_RA_RCVD) | |
1da177e4 | 3616 | goto out; |
1da177e4 | 3617 | |
b7b1bfce | 3618 | if (idev->rs_probes++ < idev->cnf.rtr_solicits) { |
caf92bc4 CW |
3619 | write_unlock(&idev->lock); |
3620 | if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) | |
3621 | ndisc_send_rs(dev, &lladdr, | |
b7b1bfce HFS |
3622 | &in6addr_linklocal_allrouters); |
3623 | else | |
caf92bc4 | 3624 | goto put; |
1da177e4 | 3625 | |
caf92bc4 | 3626 | write_lock(&idev->lock); |
b7b1bfce HFS |
3627 | /* The wait after the last probe can be shorter */ |
3628 | addrconf_mod_rs_timer(idev, (idev->rs_probes == | |
3629 | idev->cnf.rtr_solicits) ? | |
3630 | idev->cnf.rtr_solicit_delay : | |
3631 | idev->cnf.rtr_solicit_interval); | |
1da177e4 | 3632 | } else { |
1da177e4 LT |
3633 | /* |
3634 | * Note: we do not support deprecated "all on-link" | |
3635 | * assumption any longer. | |
3636 | */ | |
91df42be | 3637 | pr_debug("%s: no IPv6 routers present\n", idev->dev->name); |
1da177e4 LT |
3638 | } |
3639 | ||
3640 | out: | |
b7b1bfce | 3641 | write_unlock(&idev->lock); |
caf92bc4 | 3642 | put: |
b7b1bfce | 3643 | in6_dev_put(idev); |
1da177e4 LT |
3644 | } |
3645 | ||
3646 | /* | |
3647 | * Duplicate Address Detection | |
3648 | */ | |
3c21edbd YH |
3649 | static void addrconf_dad_kick(struct inet6_ifaddr *ifp) |
3650 | { | |
3651 | unsigned long rand_num; | |
3652 | struct inet6_dev *idev = ifp->idev; | |
3653 | ||
95c385b4 NH |
3654 | if (ifp->flags & IFA_F_OPTIMISTIC) |
3655 | rand_num = 0; | |
3656 | else | |
63862b5b | 3657 | rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); |
95c385b4 | 3658 | |
b7b1bfce | 3659 | ifp->dad_probes = idev->cnf.dad_transmits; |
c15b1cca | 3660 | addrconf_mod_dad_work(ifp, rand_num); |
3c21edbd YH |
3661 | } |
3662 | ||
c15b1cca | 3663 | static void addrconf_dad_begin(struct inet6_ifaddr *ifp) |
1da177e4 LT |
3664 | { |
3665 | struct inet6_dev *idev = ifp->idev; | |
3666 | struct net_device *dev = idev->dev; | |
16186a82 | 3667 | bool notify = false; |
1da177e4 LT |
3668 | |
3669 | addrconf_join_solict(dev, &ifp->addr); | |
3670 | ||
63862b5b | 3671 | prandom_seed((__force u32) ifp->addr.s6_addr32[3]); |
1da177e4 LT |
3672 | |
3673 | read_lock_bh(&idev->lock); | |
622ccdf1 | 3674 | spin_lock(&ifp->lock); |
e9d3e084 | 3675 | if (ifp->state == INET6_IFADDR_STATE_DEAD) |
1da177e4 | 3676 | goto out; |
1da177e4 LT |
3677 | |
3678 | if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || | |
1b34be74 | 3679 | idev->cnf.accept_dad < 1 || |
55ebaef1 NT |
3680 | !(ifp->flags&IFA_F_TENTATIVE) || |
3681 | ifp->flags & IFA_F_NODAD) { | |
cc411d0b | 3682 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
21809faf | 3683 | spin_unlock(&ifp->lock); |
1da177e4 LT |
3684 | read_unlock_bh(&idev->lock); |
3685 | ||
3686 | addrconf_dad_completed(ifp); | |
3687 | return; | |
3688 | } | |
3689 | ||
6732bade | 3690 | if (!(idev->if_flags & IF_READY)) { |
21809faf | 3691 | spin_unlock(&ifp->lock); |
6732bade | 3692 | read_unlock_bh(&idev->lock); |
3c21edbd | 3693 | /* |
590a9887 | 3694 | * If the device is not ready: |
3c21edbd YH |
3695 | * - keep it tentative if it is a permanent address. |
3696 | * - otherwise, kill it. | |
3697 | */ | |
3698 | in6_ifa_hold(ifp); | |
cc411d0b | 3699 | addrconf_dad_stop(ifp, 0); |
6732bade | 3700 | return; |
3c21edbd | 3701 | } |
95c385b4 NH |
3702 | |
3703 | /* | |
3704 | * Optimistic nodes can start receiving | |
3705 | * Frames right away | |
3706 | */ | |
7fd2561e | 3707 | if (ifp->flags & IFA_F_OPTIMISTIC) { |
95c385b4 | 3708 | ip6_ins_rt(ifp->rt); |
7fd2561e EK |
3709 | if (ipv6_use_optimistic_addr(idev)) { |
3710 | /* Because optimistic nodes can use this address, | |
3711 | * notify listeners. If DAD fails, RTM_DELADDR is sent. | |
3712 | */ | |
16186a82 | 3713 | notify = true; |
7fd2561e EK |
3714 | } |
3715 | } | |
95c385b4 | 3716 | |
6732bade | 3717 | addrconf_dad_kick(ifp); |
1da177e4 | 3718 | out: |
622ccdf1 | 3719 | spin_unlock(&ifp->lock); |
1da177e4 | 3720 | read_unlock_bh(&idev->lock); |
16186a82 SAK |
3721 | if (notify) |
3722 | ipv6_ifa_notify(RTM_NEWADDR, ifp); | |
1da177e4 LT |
3723 | } |
3724 | ||
c15b1cca | 3725 | static void addrconf_dad_start(struct inet6_ifaddr *ifp) |
1da177e4 | 3726 | { |
c15b1cca HFS |
3727 | bool begin_dad = false; |
3728 | ||
8e8e676d | 3729 | spin_lock_bh(&ifp->lock); |
c15b1cca HFS |
3730 | if (ifp->state != INET6_IFADDR_STATE_DEAD) { |
3731 | ifp->state = INET6_IFADDR_STATE_PREDAD; | |
3732 | begin_dad = true; | |
3733 | } | |
8e8e676d | 3734 | spin_unlock_bh(&ifp->lock); |
c15b1cca HFS |
3735 | |
3736 | if (begin_dad) | |
3737 | addrconf_mod_dad_work(ifp, 0); | |
3738 | } | |
3739 | ||
3740 | static void addrconf_dad_work(struct work_struct *w) | |
3741 | { | |
3742 | struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), | |
3743 | struct inet6_ifaddr, | |
3744 | dad_work); | |
1da177e4 | 3745 | struct inet6_dev *idev = ifp->idev; |
1da177e4 LT |
3746 | struct in6_addr mcaddr; |
3747 | ||
c15b1cca HFS |
3748 | enum { |
3749 | DAD_PROCESS, | |
3750 | DAD_BEGIN, | |
3751 | DAD_ABORT, | |
3752 | } action = DAD_PROCESS; | |
3753 | ||
3754 | rtnl_lock(); | |
3755 | ||
8e8e676d | 3756 | spin_lock_bh(&ifp->lock); |
c15b1cca HFS |
3757 | if (ifp->state == INET6_IFADDR_STATE_PREDAD) { |
3758 | action = DAD_BEGIN; | |
3759 | ifp->state = INET6_IFADDR_STATE_DAD; | |
3760 | } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { | |
3761 | action = DAD_ABORT; | |
3762 | ifp->state = INET6_IFADDR_STATE_POSTDAD; | |
3763 | } | |
8e8e676d | 3764 | spin_unlock_bh(&ifp->lock); |
c15b1cca HFS |
3765 | |
3766 | if (action == DAD_BEGIN) { | |
3767 | addrconf_dad_begin(ifp); | |
3768 | goto out; | |
3769 | } else if (action == DAD_ABORT) { | |
3770 | addrconf_dad_stop(ifp, 1); | |
3771 | goto out; | |
3772 | } | |
3773 | ||
b7b1bfce | 3774 | if (!ifp->dad_probes && addrconf_dad_end(ifp)) |
f2344a13 HX |
3775 | goto out; |
3776 | ||
c15b1cca | 3777 | write_lock_bh(&idev->lock); |
122e4519 | 3778 | if (idev->dead || !(idev->if_flags & IF_READY)) { |
c15b1cca | 3779 | write_unlock_bh(&idev->lock); |
1da177e4 LT |
3780 | goto out; |
3781 | } | |
21809faf | 3782 | |
3783 | spin_lock(&ifp->lock); | |
622ccdf1 HX |
3784 | if (ifp->state == INET6_IFADDR_STATE_DEAD) { |
3785 | spin_unlock(&ifp->lock); | |
c15b1cca | 3786 | write_unlock_bh(&idev->lock); |
622ccdf1 HX |
3787 | goto out; |
3788 | } | |
3789 | ||
b7b1bfce | 3790 | if (ifp->dad_probes == 0) { |
1da177e4 LT |
3791 | /* |
3792 | * DAD was successful | |
3793 | */ | |
3794 | ||
cc411d0b | 3795 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
21809faf | 3796 | spin_unlock(&ifp->lock); |
c15b1cca | 3797 | write_unlock_bh(&idev->lock); |
1da177e4 LT |
3798 | |
3799 | addrconf_dad_completed(ifp); | |
3800 | ||
3801 | goto out; | |
3802 | } | |
3803 | ||
b7b1bfce | 3804 | ifp->dad_probes--; |
c15b1cca HFS |
3805 | addrconf_mod_dad_work(ifp, |
3806 | NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); | |
21809faf | 3807 | spin_unlock(&ifp->lock); |
c15b1cca | 3808 | write_unlock_bh(&idev->lock); |
1da177e4 LT |
3809 | |
3810 | /* send a neighbour solicitation for our addr */ | |
1da177e4 | 3811 | addrconf_addr_solict_mult(&ifp->addr, &mcaddr); |
304d888b | 3812 | ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any); |
1da177e4 LT |
3813 | out: |
3814 | in6_ifa_put(ifp); | |
c15b1cca | 3815 | rtnl_unlock(); |
1da177e4 LT |
3816 | } |
3817 | ||
11ffff75 HFS |
3818 | /* ifp->idev must be at least read locked */ |
3819 | static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) | |
3820 | { | |
3821 | struct inet6_ifaddr *ifpiter; | |
3822 | struct inet6_dev *idev = ifp->idev; | |
3823 | ||
602582ca HFS |
3824 | list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { |
3825 | if (ifpiter->scope > IFA_LINK) | |
3826 | break; | |
11ffff75 HFS |
3827 | if (ifp != ifpiter && ifpiter->scope == IFA_LINK && |
3828 | (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| | |
3829 | IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == | |
3830 | IFA_F_PERMANENT) | |
3831 | return false; | |
3832 | } | |
3833 | return true; | |
3834 | } | |
3835 | ||
1da177e4 LT |
3836 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp) |
3837 | { | |
e21e8467 | 3838 | struct net_device *dev = ifp->idev->dev; |
b7b1bfce | 3839 | struct in6_addr lladdr; |
b173ee48 | 3840 | bool send_rs, send_mld; |
b7b1bfce | 3841 | |
c15b1cca | 3842 | addrconf_del_dad_work(ifp); |
1da177e4 LT |
3843 | |
3844 | /* | |
3845 | * Configure the address for reception. Now it is valid. | |
3846 | */ | |
3847 | ||
3848 | ipv6_ifa_notify(RTM_NEWADDR, ifp); | |
3849 | ||
026359bc TA |
3850 | /* If added prefix is link local and we are prepared to process |
3851 | router advertisements, start sending router solicitations. | |
1da177e4 LT |
3852 | */ |
3853 | ||
1ec047eb | 3854 | read_lock_bh(&ifp->idev->lock); |
11ffff75 | 3855 | send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); |
b173ee48 HFS |
3856 | send_rs = send_mld && |
3857 | ipv6_accept_ra(ifp->idev) && | |
1ec047eb | 3858 | ifp->idev->cnf.rtr_solicits > 0 && |
b173ee48 | 3859 | (dev->flags&IFF_LOOPBACK) == 0; |
1ec047eb HFS |
3860 | read_unlock_bh(&ifp->idev->lock); |
3861 | ||
b173ee48 HFS |
3862 | /* While dad is in progress mld report's source address is in6_addrany. |
3863 | * Resend with proper ll now. | |
3864 | */ | |
3865 | if (send_mld) | |
3866 | ipv6_mc_dad_complete(ifp->idev); | |
3867 | ||
1ec047eb | 3868 | if (send_rs) { |
1da177e4 LT |
3869 | /* |
3870 | * If a host as already performed a random delay | |
3871 | * [...] as part of DAD [...] there is no need | |
3872 | * to delay again before sending the first RS | |
3873 | */ | |
1ec047eb | 3874 | if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) |
b7b1bfce | 3875 | return; |
1ec047eb | 3876 | ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); |
1da177e4 | 3877 | |
b7b1bfce HFS |
3878 | write_lock_bh(&ifp->idev->lock); |
3879 | spin_lock(&ifp->lock); | |
3880 | ifp->idev->rs_probes = 1; | |
1da177e4 | 3881 | ifp->idev->if_flags |= IF_RS_SENT; |
b7b1bfce HFS |
3882 | addrconf_mod_rs_timer(ifp->idev, |
3883 | ifp->idev->cnf.rtr_solicit_interval); | |
3884 | spin_unlock(&ifp->lock); | |
3885 | write_unlock_bh(&ifp->idev->lock); | |
1da177e4 LT |
3886 | } |
3887 | } | |
3888 | ||
e21e8467 SH |
3889 | static void addrconf_dad_run(struct inet6_dev *idev) |
3890 | { | |
c5e33bdd YH |
3891 | struct inet6_ifaddr *ifp; |
3892 | ||
3893 | read_lock_bh(&idev->lock); | |
502a2ffd | 3894 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
21809faf | 3895 | spin_lock(&ifp->lock); |
f2344a13 HX |
3896 | if (ifp->flags & IFA_F_TENTATIVE && |
3897 | ifp->state == INET6_IFADDR_STATE_DAD) | |
3898 | addrconf_dad_kick(ifp); | |
21809faf | 3899 | spin_unlock(&ifp->lock); |
c5e33bdd YH |
3900 | } |
3901 | read_unlock_bh(&idev->lock); | |
3902 | } | |
3903 | ||
1da177e4 LT |
3904 | #ifdef CONFIG_PROC_FS |
3905 | struct if6_iter_state { | |
3c40090a | 3906 | struct seq_net_private p; |
1da177e4 | 3907 | int bucket; |
1d578303 | 3908 | int offset; |
1da177e4 LT |
3909 | }; |
3910 | ||
1d578303 | 3911 | static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) |
1da177e4 LT |
3912 | { |
3913 | struct inet6_ifaddr *ifa = NULL; | |
3914 | struct if6_iter_state *state = seq->private; | |
1218854a | 3915 | struct net *net = seq_file_net(seq); |
1d578303 | 3916 | int p = 0; |
1da177e4 | 3917 | |
1d578303 MM |
3918 | /* initial bucket if pos is 0 */ |
3919 | if (pos == 0) { | |
3920 | state->bucket = 0; | |
3921 | state->offset = 0; | |
3922 | } | |
3923 | ||
3924 | for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { | |
b67bfe0d | 3925 | hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], |
1d578303 | 3926 | addr_lst) { |
9f0d3c27 ED |
3927 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
3928 | continue; | |
1d578303 MM |
3929 | /* sync with offset */ |
3930 | if (p < state->offset) { | |
3931 | p++; | |
3932 | continue; | |
3933 | } | |
3934 | state->offset++; | |
9f0d3c27 | 3935 | return ifa; |
1d578303 MM |
3936 | } |
3937 | ||
3938 | /* prepare for next bucket */ | |
3939 | state->offset = 0; | |
3940 | p = 0; | |
1da177e4 | 3941 | } |
c2e21293 | 3942 | return NULL; |
1da177e4 LT |
3943 | } |
3944 | ||
c2e21293 | 3945 | static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, |
3946 | struct inet6_ifaddr *ifa) | |
1da177e4 LT |
3947 | { |
3948 | struct if6_iter_state *state = seq->private; | |
1218854a | 3949 | struct net *net = seq_file_net(seq); |
1da177e4 | 3950 | |
b67bfe0d | 3951 | hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { |
9f0d3c27 ED |
3952 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
3953 | continue; | |
1d578303 | 3954 | state->offset++; |
9f0d3c27 | 3955 | return ifa; |
1d578303 | 3956 | } |
3c40090a | 3957 | |
c2e21293 | 3958 | while (++state->bucket < IN6_ADDR_HSIZE) { |
1d578303 | 3959 | state->offset = 0; |
b67bfe0d | 3960 | hlist_for_each_entry_rcu_bh(ifa, |
c2e21293 | 3961 | &inet6_addr_lst[state->bucket], addr_lst) { |
9f0d3c27 ED |
3962 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
3963 | continue; | |
1d578303 | 3964 | state->offset++; |
9f0d3c27 | 3965 | return ifa; |
c2e21293 | 3966 | } |
1da177e4 | 3967 | } |
3c40090a | 3968 | |
c2e21293 | 3969 | return NULL; |
1da177e4 LT |
3970 | } |
3971 | ||
1da177e4 | 3972 | static void *if6_seq_start(struct seq_file *seq, loff_t *pos) |
4f70ecca | 3973 | __acquires(rcu_bh) |
1da177e4 | 3974 | { |
5c578aed | 3975 | rcu_read_lock_bh(); |
1d578303 | 3976 | return if6_get_first(seq, *pos); |
1da177e4 LT |
3977 | } |
3978 | ||
3979 | static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
3980 | { | |
3981 | struct inet6_ifaddr *ifa; | |
3982 | ||
3983 | ifa = if6_get_next(seq, v); | |
3984 | ++*pos; | |
3985 | return ifa; | |
3986 | } | |
3987 | ||
3988 | static void if6_seq_stop(struct seq_file *seq, void *v) | |
4f70ecca | 3989 | __releases(rcu_bh) |
1da177e4 | 3990 | { |
5c578aed | 3991 | rcu_read_unlock_bh(); |
1da177e4 LT |
3992 | } |
3993 | ||
3994 | static int if6_seq_show(struct seq_file *seq, void *v) | |
3995 | { | |
3996 | struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; | |
971a351c | 3997 | seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", |
b071195d | 3998 | &ifp->addr, |
1da177e4 LT |
3999 | ifp->idev->dev->ifindex, |
4000 | ifp->prefix_len, | |
4001 | ifp->scope, | |
971a351c | 4002 | (u8) ifp->flags, |
1da177e4 LT |
4003 | ifp->idev->dev->name); |
4004 | return 0; | |
4005 | } | |
4006 | ||
56b3d975 | 4007 | static const struct seq_operations if6_seq_ops = { |
1da177e4 LT |
4008 | .start = if6_seq_start, |
4009 | .next = if6_seq_next, | |
4010 | .show = if6_seq_show, | |
4011 | .stop = if6_seq_stop, | |
4012 | }; | |
4013 | ||
4014 | static int if6_seq_open(struct inode *inode, struct file *file) | |
4015 | { | |
3c40090a DL |
4016 | return seq_open_net(inode, file, &if6_seq_ops, |
4017 | sizeof(struct if6_iter_state)); | |
1da177e4 LT |
4018 | } |
4019 | ||
9a32144e | 4020 | static const struct file_operations if6_fops = { |
1da177e4 LT |
4021 | .owner = THIS_MODULE, |
4022 | .open = if6_seq_open, | |
4023 | .read = seq_read, | |
4024 | .llseek = seq_lseek, | |
3c40090a | 4025 | .release = seq_release_net, |
1da177e4 LT |
4026 | }; |
4027 | ||
2c8c1e72 | 4028 | static int __net_init if6_proc_net_init(struct net *net) |
1da177e4 | 4029 | { |
d4beaa66 | 4030 | if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) |
1da177e4 LT |
4031 | return -ENOMEM; |
4032 | return 0; | |
4033 | } | |
4034 | ||
2c8c1e72 | 4035 | static void __net_exit if6_proc_net_exit(struct net *net) |
3c40090a | 4036 | { |
ece31ffd | 4037 | remove_proc_entry("if_inet6", net->proc_net); |
3c40090a DL |
4038 | } |
4039 | ||
4040 | static struct pernet_operations if6_proc_net_ops = { | |
67ba4152 IM |
4041 | .init = if6_proc_net_init, |
4042 | .exit = if6_proc_net_exit, | |
3c40090a DL |
4043 | }; |
4044 | ||
4045 | int __init if6_proc_init(void) | |
4046 | { | |
4047 | return register_pernet_subsys(&if6_proc_net_ops); | |
4048 | } | |
4049 | ||
1da177e4 LT |
4050 | void if6_proc_exit(void) |
4051 | { | |
3c40090a | 4052 | unregister_pernet_subsys(&if6_proc_net_ops); |
1da177e4 LT |
4053 | } |
4054 | #endif /* CONFIG_PROC_FS */ | |
4055 | ||
07a93626 | 4056 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
3b9f9a1c | 4057 | /* Check if address is a home address configured on any interface. */ |
b71d1d42 | 4058 | int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) |
3b9f9a1c NT |
4059 | { |
4060 | int ret = 0; | |
c2e21293 | 4061 | struct inet6_ifaddr *ifp = NULL; |
ddbe5032 | 4062 | unsigned int hash = inet6_addr_hash(addr); |
c2e21293 | 4063 | |
5c578aed | 4064 | rcu_read_lock_bh(); |
b67bfe0d | 4065 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { |
878628fb | 4066 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
389f6612 | 4067 | continue; |
caad295f | 4068 | if (ipv6_addr_equal(&ifp->addr, addr) && |
3b9f9a1c NT |
4069 | (ifp->flags & IFA_F_HOMEADDRESS)) { |
4070 | ret = 1; | |
4071 | break; | |
4072 | } | |
4073 | } | |
5c578aed | 4074 | rcu_read_unlock_bh(); |
3b9f9a1c NT |
4075 | return ret; |
4076 | } | |
4077 | #endif | |
4078 | ||
1da177e4 LT |
4079 | /* |
4080 | * Periodic address status verification | |
4081 | */ | |
4082 | ||
c15b1cca | 4083 | static void addrconf_verify_rtnl(void) |
1da177e4 | 4084 | { |
b2db7564 | 4085 | unsigned long now, next, next_sec, next_sched; |
1da177e4 | 4086 | struct inet6_ifaddr *ifp; |
1da177e4 LT |
4087 | int i; |
4088 | ||
c15b1cca HFS |
4089 | ASSERT_RTNL(); |
4090 | ||
5c578aed | 4091 | rcu_read_lock_bh(); |
1da177e4 | 4092 | now = jiffies; |
b2db7564 | 4093 | next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); |
1da177e4 | 4094 | |
c15b1cca | 4095 | cancel_delayed_work(&addr_chk_work); |
1da177e4 | 4096 | |
bcdd553f | 4097 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { |
1da177e4 | 4098 | restart: |
c15b1cca | 4099 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { |
1da177e4 | 4100 | unsigned long age; |
1da177e4 | 4101 | |
fad8da3e YA |
4102 | /* When setting preferred_lft to a value not zero or |
4103 | * infinity, while valid_lft is infinity | |
4104 | * IFA_F_PERMANENT has a non-infinity life time. | |
4105 | */ | |
4106 | if ((ifp->flags & IFA_F_PERMANENT) && | |
4107 | (ifp->prefered_lft == INFINITY_LIFE_TIME)) | |
1da177e4 LT |
4108 | continue; |
4109 | ||
4110 | spin_lock(&ifp->lock); | |
b2db7564 YH |
4111 | /* We try to batch several events at once. */ |
4112 | age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; | |
1da177e4 | 4113 | |
8f27ebb9 YH |
4114 | if (ifp->valid_lft != INFINITY_LIFE_TIME && |
4115 | age >= ifp->valid_lft) { | |
1da177e4 LT |
4116 | spin_unlock(&ifp->lock); |
4117 | in6_ifa_hold(ifp); | |
1da177e4 LT |
4118 | ipv6_del_addr(ifp); |
4119 | goto restart; | |
8f27ebb9 YH |
4120 | } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { |
4121 | spin_unlock(&ifp->lock); | |
4122 | continue; | |
1da177e4 | 4123 | } else if (age >= ifp->prefered_lft) { |
a1ed0526 | 4124 | /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ |
1da177e4 LT |
4125 | int deprecate = 0; |
4126 | ||
4127 | if (!(ifp->flags&IFA_F_DEPRECATED)) { | |
4128 | deprecate = 1; | |
4129 | ifp->flags |= IFA_F_DEPRECATED; | |
4130 | } | |
4131 | ||
fad8da3e YA |
4132 | if ((ifp->valid_lft != INFINITY_LIFE_TIME) && |
4133 | (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) | |
1da177e4 LT |
4134 | next = ifp->tstamp + ifp->valid_lft * HZ; |
4135 | ||
4136 | spin_unlock(&ifp->lock); | |
4137 | ||
4138 | if (deprecate) { | |
4139 | in6_ifa_hold(ifp); | |
1da177e4 LT |
4140 | |
4141 | ipv6_ifa_notify(0, ifp); | |
4142 | in6_ifa_put(ifp); | |
4143 | goto restart; | |
4144 | } | |
1da177e4 LT |
4145 | } else if ((ifp->flags&IFA_F_TEMPORARY) && |
4146 | !(ifp->flags&IFA_F_TENTATIVE)) { | |
88949cf4 | 4147 | unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * |
4148 | ifp->idev->cnf.dad_transmits * | |
1f9248e5 | 4149 | NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; |
88949cf4 | 4150 | |
1da177e4 LT |
4151 | if (age >= ifp->prefered_lft - regen_advance) { |
4152 | struct inet6_ifaddr *ifpub = ifp->ifpub; | |
4153 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) | |
4154 | next = ifp->tstamp + ifp->prefered_lft * HZ; | |
4155 | if (!ifp->regen_count && ifpub) { | |
4156 | ifp->regen_count++; | |
4157 | in6_ifa_hold(ifp); | |
4158 | in6_ifa_hold(ifpub); | |
4159 | spin_unlock(&ifp->lock); | |
5c578aed | 4160 | |
291d809b HY |
4161 | spin_lock(&ifpub->lock); |
4162 | ifpub->regen_count = 0; | |
4163 | spin_unlock(&ifpub->lock); | |
1da177e4 LT |
4164 | ipv6_create_tempaddr(ifpub, ifp); |
4165 | in6_ifa_put(ifpub); | |
4166 | in6_ifa_put(ifp); | |
4167 | goto restart; | |
4168 | } | |
4169 | } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) | |
4170 | next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; | |
4171 | spin_unlock(&ifp->lock); | |
1da177e4 LT |
4172 | } else { |
4173 | /* ifp->prefered_lft <= ifp->valid_lft */ | |
4174 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) | |
4175 | next = ifp->tstamp + ifp->prefered_lft * HZ; | |
4176 | spin_unlock(&ifp->lock); | |
4177 | } | |
4178 | } | |
1da177e4 LT |
4179 | } |
4180 | ||
b2db7564 YH |
4181 | next_sec = round_jiffies_up(next); |
4182 | next_sched = next; | |
4183 | ||
4184 | /* If rounded timeout is accurate enough, accept it. */ | |
4185 | if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) | |
4186 | next_sched = next_sec; | |
4187 | ||
4188 | /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ | |
4189 | if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) | |
4190 | next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; | |
4191 | ||
ba3542e1 | 4192 | ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", |
4193 | now, next, next_sec, next_sched); | |
c15b1cca | 4194 | mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); |
5c578aed | 4195 | rcu_read_unlock_bh(); |
1da177e4 LT |
4196 | } |
4197 | ||
c15b1cca HFS |
4198 | static void addrconf_verify_work(struct work_struct *w) |
4199 | { | |
4200 | rtnl_lock(); | |
4201 | addrconf_verify_rtnl(); | |
4202 | rtnl_unlock(); | |
4203 | } | |
4204 | ||
4205 | static void addrconf_verify(void) | |
4206 | { | |
4207 | mod_delayed_work(addrconf_wq, &addr_chk_work, 0); | |
4208 | } | |
4209 | ||
caeaba79 ND |
4210 | static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, |
4211 | struct in6_addr **peer_pfx) | |
461d8837 TG |
4212 | { |
4213 | struct in6_addr *pfx = NULL; | |
4214 | ||
caeaba79 ND |
4215 | *peer_pfx = NULL; |
4216 | ||
461d8837 TG |
4217 | if (addr) |
4218 | pfx = nla_data(addr); | |
4219 | ||
4220 | if (local) { | |
4221 | if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) | |
caeaba79 ND |
4222 | *peer_pfx = pfx; |
4223 | pfx = nla_data(local); | |
461d8837 TG |
4224 | } |
4225 | ||
4226 | return pfx; | |
4227 | } | |
4228 | ||
ef7c79ed | 4229 | static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { |
461d8837 TG |
4230 | [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, |
4231 | [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, | |
4232 | [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, | |
479840ff | 4233 | [IFA_FLAGS] = { .len = sizeof(u32) }, |
461d8837 TG |
4234 | }; |
4235 | ||
1da177e4 | 4236 | static int |
661d2967 | 4237 | inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 4238 | { |
3b1e0a65 | 4239 | struct net *net = sock_net(skb->sk); |
b933f716 TG |
4240 | struct ifaddrmsg *ifm; |
4241 | struct nlattr *tb[IFA_MAX+1]; | |
caeaba79 | 4242 | struct in6_addr *pfx, *peer_pfx; |
6046d5b4 | 4243 | u32 ifa_flags; |
b933f716 | 4244 | int err; |
1da177e4 | 4245 | |
b933f716 TG |
4246 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
4247 | if (err < 0) | |
4248 | return err; | |
4249 | ||
4250 | ifm = nlmsg_data(nlh); | |
caeaba79 | 4251 | pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); |
63159f29 | 4252 | if (!pfx) |
1da177e4 LT |
4253 | return -EINVAL; |
4254 | ||
6046d5b4 HK |
4255 | ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; |
4256 | ||
4257 | /* We ignore other flags so far. */ | |
4258 | ifa_flags &= IFA_F_MANAGETEMPADDR; | |
4259 | ||
4260 | return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, | |
4261 | ifm->ifa_prefixlen); | |
1da177e4 LT |
4262 | } |
4263 | ||
479840ff | 4264 | static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, |
55ebaef1 | 4265 | u32 prefered_lft, u32 valid_lft) |
081bba5b | 4266 | { |
6f704992 YH |
4267 | u32 flags; |
4268 | clock_t expires; | |
4bed72e4 | 4269 | unsigned long timeout; |
53bd6749 | 4270 | bool was_managetempaddr; |
5b84efec | 4271 | bool had_prefixroute; |
46d48046 | 4272 | |
c15b1cca HFS |
4273 | ASSERT_RTNL(); |
4274 | ||
081bba5b NT |
4275 | if (!valid_lft || (prefered_lft > valid_lft)) |
4276 | return -EINVAL; | |
4277 | ||
53bd6749 JP |
4278 | if (ifa_flags & IFA_F_MANAGETEMPADDR && |
4279 | (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) | |
4280 | return -EINVAL; | |
4281 | ||
4bed72e4 YH |
4282 | timeout = addrconf_timeout_fixup(valid_lft, HZ); |
4283 | if (addrconf_finite_timeout(timeout)) { | |
4284 | expires = jiffies_to_clock_t(timeout * HZ); | |
4285 | valid_lft = timeout; | |
6f704992 | 4286 | flags = RTF_EXPIRES; |
4bed72e4 YH |
4287 | } else { |
4288 | expires = 0; | |
4289 | flags = 0; | |
4290 | ifa_flags |= IFA_F_PERMANENT; | |
6f704992 | 4291 | } |
081bba5b | 4292 | |
4bed72e4 YH |
4293 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); |
4294 | if (addrconf_finite_timeout(timeout)) { | |
4295 | if (timeout == 0) | |
4296 | ifa_flags |= IFA_F_DEPRECATED; | |
4297 | prefered_lft = timeout; | |
4298 | } | |
081bba5b NT |
4299 | |
4300 | spin_lock_bh(&ifp->lock); | |
53bd6749 | 4301 | was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; |
5b84efec TH |
4302 | had_prefixroute = ifp->flags & IFA_F_PERMANENT && |
4303 | !(ifp->flags & IFA_F_NOPREFIXROUTE); | |
53bd6749 | 4304 | ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | |
761aac73 TH |
4305 | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | |
4306 | IFA_F_NOPREFIXROUTE); | |
53bd6749 | 4307 | ifp->flags |= ifa_flags; |
081bba5b NT |
4308 | ifp->tstamp = jiffies; |
4309 | ifp->valid_lft = valid_lft; | |
4310 | ifp->prefered_lft = prefered_lft; | |
4311 | ||
4312 | spin_unlock_bh(&ifp->lock); | |
4313 | if (!(ifp->flags&IFA_F_TENTATIVE)) | |
4314 | ipv6_ifa_notify(0, ifp); | |
081bba5b | 4315 | |
761aac73 TH |
4316 | if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { |
4317 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, | |
4318 | expires, flags); | |
5b84efec TH |
4319 | } else if (had_prefixroute) { |
4320 | enum cleanup_prefix_rt_t action; | |
4321 | unsigned long rt_expires; | |
4322 | ||
4323 | write_lock_bh(&ifp->idev->lock); | |
4324 | action = check_cleanup_prefix_route(ifp, &rt_expires); | |
4325 | write_unlock_bh(&ifp->idev->lock); | |
4326 | ||
4327 | if (action != CLEANUP_PREFIX_RT_NOP) { | |
4328 | cleanup_prefix_route(ifp, rt_expires, | |
4329 | action == CLEANUP_PREFIX_RT_DEL); | |
4330 | } | |
761aac73 | 4331 | } |
53bd6749 JP |
4332 | |
4333 | if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { | |
4334 | if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) | |
4335 | valid_lft = prefered_lft = 0; | |
4336 | manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, | |
4337 | !was_managetempaddr, jiffies); | |
4338 | } | |
4339 | ||
c15b1cca | 4340 | addrconf_verify_rtnl(); |
081bba5b NT |
4341 | |
4342 | return 0; | |
4343 | } | |
4344 | ||
1da177e4 | 4345 | static int |
661d2967 | 4346 | inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 4347 | { |
3b1e0a65 | 4348 | struct net *net = sock_net(skb->sk); |
461d8837 TG |
4349 | struct ifaddrmsg *ifm; |
4350 | struct nlattr *tb[IFA_MAX+1]; | |
caeaba79 | 4351 | struct in6_addr *pfx, *peer_pfx; |
7198f8ce TG |
4352 | struct inet6_ifaddr *ifa; |
4353 | struct net_device *dev; | |
55ebaef1 | 4354 | u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; |
479840ff | 4355 | u32 ifa_flags; |
461d8837 | 4356 | int err; |
1da177e4 | 4357 | |
461d8837 TG |
4358 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
4359 | if (err < 0) | |
4360 | return err; | |
4361 | ||
4362 | ifm = nlmsg_data(nlh); | |
caeaba79 | 4363 | pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); |
63159f29 | 4364 | if (!pfx) |
1da177e4 LT |
4365 | return -EINVAL; |
4366 | ||
461d8837 | 4367 | if (tb[IFA_CACHEINFO]) { |
0778769d | 4368 | struct ifa_cacheinfo *ci; |
461d8837 TG |
4369 | |
4370 | ci = nla_data(tb[IFA_CACHEINFO]); | |
0778769d | 4371 | valid_lft = ci->ifa_valid; |
461d8837 TG |
4372 | preferred_lft = ci->ifa_prefered; |
4373 | } else { | |
4374 | preferred_lft = INFINITY_LIFE_TIME; | |
4375 | valid_lft = INFINITY_LIFE_TIME; | |
0778769d NT |
4376 | } |
4377 | ||
af284937 | 4378 | dev = __dev_get_by_index(net, ifm->ifa_index); |
63159f29 | 4379 | if (!dev) |
7198f8ce TG |
4380 | return -ENODEV; |
4381 | ||
479840ff JP |
4382 | ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; |
4383 | ||
55ebaef1 | 4384 | /* We ignore other flags so far. */ |
761aac73 | 4385 | ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | |
93a714d6 | 4386 | IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; |
55ebaef1 | 4387 | |
1cab3da6 | 4388 | ifa = ipv6_get_ifaddr(net, pfx, dev, 1); |
63159f29 | 4389 | if (!ifa) { |
7198f8ce TG |
4390 | /* |
4391 | * It would be best to check for !NLM_F_CREATE here but | |
db9c7c39 | 4392 | * userspace already relies on not having to provide this. |
7198f8ce | 4393 | */ |
caeaba79 | 4394 | return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, |
af284937 DL |
4395 | ifm->ifa_prefixlen, ifa_flags, |
4396 | preferred_lft, valid_lft); | |
081bba5b NT |
4397 | } |
4398 | ||
7198f8ce TG |
4399 | if (nlh->nlmsg_flags & NLM_F_EXCL || |
4400 | !(nlh->nlmsg_flags & NLM_F_REPLACE)) | |
4401 | err = -EEXIST; | |
4402 | else | |
55ebaef1 | 4403 | err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); |
7198f8ce TG |
4404 | |
4405 | in6_ifa_put(ifa); | |
4406 | ||
4407 | return err; | |
1da177e4 LT |
4408 | } |
4409 | ||
479840ff | 4410 | static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, |
101bb229 TG |
4411 | u8 scope, int ifindex) |
4412 | { | |
4413 | struct ifaddrmsg *ifm; | |
4414 | ||
4415 | ifm = nlmsg_data(nlh); | |
4416 | ifm->ifa_family = AF_INET6; | |
4417 | ifm->ifa_prefixlen = prefixlen; | |
4418 | ifm->ifa_flags = flags; | |
4419 | ifm->ifa_scope = scope; | |
4420 | ifm->ifa_index = ifindex; | |
4421 | } | |
4422 | ||
85486af0 TG |
4423 | static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, |
4424 | unsigned long tstamp, u32 preferred, u32 valid) | |
4425 | { | |
4426 | struct ifa_cacheinfo ci; | |
4427 | ||
18a31e1e TG |
4428 | ci.cstamp = cstamp_delta(cstamp); |
4429 | ci.tstamp = cstamp_delta(tstamp); | |
85486af0 TG |
4430 | ci.ifa_prefered = preferred; |
4431 | ci.ifa_valid = valid; | |
4432 | ||
4433 | return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); | |
4434 | } | |
4435 | ||
101bb229 TG |
4436 | static inline int rt_scope(int ifa_scope) |
4437 | { | |
4438 | if (ifa_scope & IFA_HOST) | |
4439 | return RT_SCOPE_HOST; | |
4440 | else if (ifa_scope & IFA_LINK) | |
4441 | return RT_SCOPE_LINK; | |
4442 | else if (ifa_scope & IFA_SITE) | |
4443 | return RT_SCOPE_SITE; | |
4444 | else | |
4445 | return RT_SCOPE_UNIVERSE; | |
4446 | } | |
4447 | ||
0ab6803b TG |
4448 | static inline int inet6_ifaddr_msgsize(void) |
4449 | { | |
339bf98f | 4450 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
caeaba79 | 4451 | + nla_total_size(16) /* IFA_LOCAL */ |
339bf98f | 4452 | + nla_total_size(16) /* IFA_ADDRESS */ |
479840ff JP |
4453 | + nla_total_size(sizeof(struct ifa_cacheinfo)) |
4454 | + nla_total_size(4) /* IFA_FLAGS */; | |
0ab6803b | 4455 | } |
c5396a31 | 4456 | |
1da177e4 | 4457 | static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, |
15e47304 | 4458 | u32 portid, u32 seq, int event, unsigned int flags) |
1da177e4 | 4459 | { |
1da177e4 | 4460 | struct nlmsghdr *nlh; |
85486af0 | 4461 | u32 preferred, valid; |
1da177e4 | 4462 | |
15e47304 | 4463 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
63159f29 | 4464 | if (!nlh) |
26932566 | 4465 | return -EMSGSIZE; |
0ab6803b | 4466 | |
101bb229 TG |
4467 | put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), |
4468 | ifa->idev->dev->ifindex); | |
4469 | ||
fad8da3e YA |
4470 | if (!((ifa->flags&IFA_F_PERMANENT) && |
4471 | (ifa->prefered_lft == INFINITY_LIFE_TIME))) { | |
85486af0 TG |
4472 | preferred = ifa->prefered_lft; |
4473 | valid = ifa->valid_lft; | |
4474 | if (preferred != INFINITY_LIFE_TIME) { | |
1da177e4 | 4475 | long tval = (jiffies - ifa->tstamp)/HZ; |
a1faa698 JR |
4476 | if (preferred > tval) |
4477 | preferred -= tval; | |
4478 | else | |
4479 | preferred = 0; | |
f56619fc BH |
4480 | if (valid != INFINITY_LIFE_TIME) { |
4481 | if (valid > tval) | |
4482 | valid -= tval; | |
4483 | else | |
4484 | valid = 0; | |
4485 | } | |
1da177e4 LT |
4486 | } |
4487 | } else { | |
85486af0 TG |
4488 | preferred = INFINITY_LIFE_TIME; |
4489 | valid = INFINITY_LIFE_TIME; | |
4490 | } | |
4491 | ||
7996c799 | 4492 | if (!ipv6_addr_any(&ifa->peer_addr)) { |
930345ea JB |
4493 | if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || |
4494 | nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) | |
caeaba79 ND |
4495 | goto error; |
4496 | } else | |
930345ea | 4497 | if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) |
caeaba79 ND |
4498 | goto error; |
4499 | ||
4500 | if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) | |
4501 | goto error; | |
1da177e4 | 4502 | |
479840ff JP |
4503 | if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) |
4504 | goto error; | |
4505 | ||
053c095a JB |
4506 | nlmsg_end(skb, nlh); |
4507 | return 0; | |
caeaba79 ND |
4508 | |
4509 | error: | |
4510 | nlmsg_cancel(skb, nlh); | |
4511 | return -EMSGSIZE; | |
1da177e4 LT |
4512 | } |
4513 | ||
4514 | static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, | |
15e47304 | 4515 | u32 portid, u32 seq, int event, u16 flags) |
1da177e4 | 4516 | { |
1da177e4 | 4517 | struct nlmsghdr *nlh; |
101bb229 TG |
4518 | u8 scope = RT_SCOPE_UNIVERSE; |
4519 | int ifindex = ifmca->idev->dev->ifindex; | |
4520 | ||
4521 | if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) | |
4522 | scope = RT_SCOPE_SITE; | |
1da177e4 | 4523 | |
15e47304 | 4524 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
63159f29 | 4525 | if (!nlh) |
26932566 | 4526 | return -EMSGSIZE; |
85486af0 | 4527 | |
0ab6803b | 4528 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
930345ea | 4529 | if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || |
0ab6803b | 4530 | put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, |
26932566 PM |
4531 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
4532 | nlmsg_cancel(skb, nlh); | |
4533 | return -EMSGSIZE; | |
4534 | } | |
85486af0 | 4535 | |
053c095a JB |
4536 | nlmsg_end(skb, nlh); |
4537 | return 0; | |
1da177e4 LT |
4538 | } |
4539 | ||
4540 | static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, | |
15e47304 | 4541 | u32 portid, u32 seq, int event, unsigned int flags) |
1da177e4 | 4542 | { |
1da177e4 | 4543 | struct nlmsghdr *nlh; |
101bb229 TG |
4544 | u8 scope = RT_SCOPE_UNIVERSE; |
4545 | int ifindex = ifaca->aca_idev->dev->ifindex; | |
4546 | ||
4547 | if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) | |
4548 | scope = RT_SCOPE_SITE; | |
1da177e4 | 4549 | |
15e47304 | 4550 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); |
63159f29 | 4551 | if (!nlh) |
26932566 | 4552 | return -EMSGSIZE; |
85486af0 | 4553 | |
0ab6803b | 4554 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
930345ea | 4555 | if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || |
0ab6803b | 4556 | put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, |
26932566 PM |
4557 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
4558 | nlmsg_cancel(skb, nlh); | |
4559 | return -EMSGSIZE; | |
4560 | } | |
1da177e4 | 4561 | |
053c095a JB |
4562 | nlmsg_end(skb, nlh); |
4563 | return 0; | |
1da177e4 LT |
4564 | } |
4565 | ||
e21e8467 | 4566 | enum addr_type_t { |
1da177e4 LT |
4567 | UNICAST_ADDR, |
4568 | MULTICAST_ADDR, | |
4569 | ANYCAST_ADDR, | |
4570 | }; | |
4571 | ||
234b27c3 ED |
4572 | /* called with rcu_read_lock() */ |
4573 | static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, | |
4574 | struct netlink_callback *cb, enum addr_type_t type, | |
4575 | int s_ip_idx, int *p_ip_idx) | |
4576 | { | |
234b27c3 ED |
4577 | struct ifmcaddr6 *ifmca; |
4578 | struct ifacaddr6 *ifaca; | |
4579 | int err = 1; | |
4580 | int ip_idx = *p_ip_idx; | |
4581 | ||
4582 | read_lock_bh(&idev->lock); | |
4583 | switch (type) { | |
502a2ffd | 4584 | case UNICAST_ADDR: { |
4585 | struct inet6_ifaddr *ifa; | |
4586 | ||
234b27c3 | 4587 | /* unicast address incl. temp addr */ |
502a2ffd | 4588 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
4589 | if (++ip_idx < s_ip_idx) | |
234b27c3 ED |
4590 | continue; |
4591 | err = inet6_fill_ifaddr(skb, ifa, | |
15e47304 | 4592 | NETLINK_CB(cb->skb).portid, |
234b27c3 ED |
4593 | cb->nlh->nlmsg_seq, |
4594 | RTM_NEWADDR, | |
4595 | NLM_F_MULTI); | |
053c095a | 4596 | if (err < 0) |
234b27c3 | 4597 | break; |
63998ac2 | 4598 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
234b27c3 ED |
4599 | } |
4600 | break; | |
502a2ffd | 4601 | } |
234b27c3 ED |
4602 | case MULTICAST_ADDR: |
4603 | /* multicast address */ | |
4604 | for (ifmca = idev->mc_list; ifmca; | |
4605 | ifmca = ifmca->next, ip_idx++) { | |
4606 | if (ip_idx < s_ip_idx) | |
4607 | continue; | |
4608 | err = inet6_fill_ifmcaddr(skb, ifmca, | |
15e47304 | 4609 | NETLINK_CB(cb->skb).portid, |
234b27c3 ED |
4610 | cb->nlh->nlmsg_seq, |
4611 | RTM_GETMULTICAST, | |
4612 | NLM_F_MULTI); | |
053c095a | 4613 | if (err < 0) |
234b27c3 ED |
4614 | break; |
4615 | } | |
4616 | break; | |
4617 | case ANYCAST_ADDR: | |
4618 | /* anycast address */ | |
4619 | for (ifaca = idev->ac_list; ifaca; | |
4620 | ifaca = ifaca->aca_next, ip_idx++) { | |
4621 | if (ip_idx < s_ip_idx) | |
4622 | continue; | |
4623 | err = inet6_fill_ifacaddr(skb, ifaca, | |
15e47304 | 4624 | NETLINK_CB(cb->skb).portid, |
234b27c3 ED |
4625 | cb->nlh->nlmsg_seq, |
4626 | RTM_GETANYCAST, | |
4627 | NLM_F_MULTI); | |
053c095a | 4628 | if (err < 0) |
234b27c3 ED |
4629 | break; |
4630 | } | |
4631 | break; | |
4632 | default: | |
4633 | break; | |
4634 | } | |
4635 | read_unlock_bh(&idev->lock); | |
4636 | *p_ip_idx = ip_idx; | |
4637 | return err; | |
4638 | } | |
4639 | ||
1da177e4 LT |
4640 | static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, |
4641 | enum addr_type_t type) | |
4642 | { | |
234b27c3 ED |
4643 | struct net *net = sock_net(skb->sk); |
4644 | int h, s_h; | |
1da177e4 LT |
4645 | int idx, ip_idx; |
4646 | int s_idx, s_ip_idx; | |
1da177e4 | 4647 | struct net_device *dev; |
234b27c3 ED |
4648 | struct inet6_dev *idev; |
4649 | struct hlist_head *head; | |
1ab1457c | 4650 | |
234b27c3 ED |
4651 | s_h = cb->args[0]; |
4652 | s_idx = idx = cb->args[1]; | |
4653 | s_ip_idx = ip_idx = cb->args[2]; | |
5d5619b4 | 4654 | |
234b27c3 | 4655 | rcu_read_lock(); |
63998ac2 | 4656 | cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; |
234b27c3 ED |
4657 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
4658 | idx = 0; | |
4659 | head = &net->dev_index_head[h]; | |
b67bfe0d | 4660 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
234b27c3 ED |
4661 | if (idx < s_idx) |
4662 | goto cont; | |
4b97efdf | 4663 | if (h > s_h || idx > s_idx) |
234b27c3 ED |
4664 | s_ip_idx = 0; |
4665 | ip_idx = 0; | |
e21e8467 SH |
4666 | idev = __in6_dev_get(dev); |
4667 | if (!idev) | |
234b27c3 ED |
4668 | goto cont; |
4669 | ||
4670 | if (in6_dump_addrs(idev, skb, cb, type, | |
7b46a644 | 4671 | s_ip_idx, &ip_idx) < 0) |
234b27c3 | 4672 | goto done; |
7562f876 | 4673 | cont: |
234b27c3 ED |
4674 | idx++; |
4675 | } | |
1da177e4 | 4676 | } |
234b27c3 ED |
4677 | done: |
4678 | rcu_read_unlock(); | |
4679 | cb->args[0] = h; | |
4680 | cb->args[1] = idx; | |
4681 | cb->args[2] = ip_idx; | |
4682 | ||
1da177e4 LT |
4683 | return skb->len; |
4684 | } | |
4685 | ||
4686 | static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) | |
4687 | { | |
4688 | enum addr_type_t type = UNICAST_ADDR; | |
b854272b | 4689 | |
1da177e4 LT |
4690 | return inet6_dump_addr(skb, cb, type); |
4691 | } | |
4692 | ||
4693 | static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) | |
4694 | { | |
4695 | enum addr_type_t type = MULTICAST_ADDR; | |
b854272b | 4696 | |
1da177e4 LT |
4697 | return inet6_dump_addr(skb, cb, type); |
4698 | } | |
4699 | ||
4700 | ||
4701 | static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) | |
4702 | { | |
4703 | enum addr_type_t type = ANYCAST_ADDR; | |
b854272b | 4704 | |
1da177e4 LT |
4705 | return inet6_dump_addr(skb, cb, type); |
4706 | } | |
4707 | ||
661d2967 | 4708 | static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) |
6c223828 | 4709 | { |
3b1e0a65 | 4710 | struct net *net = sock_net(in_skb->sk); |
1b29fc2c TG |
4711 | struct ifaddrmsg *ifm; |
4712 | struct nlattr *tb[IFA_MAX+1]; | |
caeaba79 | 4713 | struct in6_addr *addr = NULL, *peer; |
6c223828 NT |
4714 | struct net_device *dev = NULL; |
4715 | struct inet6_ifaddr *ifa; | |
4716 | struct sk_buff *skb; | |
6c223828 NT |
4717 | int err; |
4718 | ||
1b29fc2c TG |
4719 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); |
4720 | if (err < 0) | |
4721 | goto errout; | |
4722 | ||
caeaba79 | 4723 | addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); |
63159f29 | 4724 | if (!addr) { |
1b29fc2c TG |
4725 | err = -EINVAL; |
4726 | goto errout; | |
6c223828 | 4727 | } |
6c223828 | 4728 | |
1b29fc2c | 4729 | ifm = nlmsg_data(nlh); |
6c223828 | 4730 | if (ifm->ifa_index) |
6fda7350 | 4731 | dev = __dev_get_by_index(net, ifm->ifa_index); |
6c223828 | 4732 | |
e21e8467 SH |
4733 | ifa = ipv6_get_ifaddr(net, addr, dev, 1); |
4734 | if (!ifa) { | |
1b29fc2c TG |
4735 | err = -EADDRNOTAVAIL; |
4736 | goto errout; | |
4737 | } | |
6c223828 | 4738 | |
e21e8467 SH |
4739 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); |
4740 | if (!skb) { | |
6c223828 | 4741 | err = -ENOBUFS; |
1b29fc2c | 4742 | goto errout_ifa; |
6c223828 NT |
4743 | } |
4744 | ||
15e47304 | 4745 | err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, |
6c223828 | 4746 | nlh->nlmsg_seq, RTM_NEWADDR, 0); |
26932566 PM |
4747 | if (err < 0) { |
4748 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ | |
4749 | WARN_ON(err == -EMSGSIZE); | |
4750 | kfree_skb(skb); | |
4751 | goto errout_ifa; | |
4752 | } | |
15e47304 | 4753 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
1b29fc2c | 4754 | errout_ifa: |
6c223828 | 4755 | in6_ifa_put(ifa); |
1b29fc2c | 4756 | errout: |
6c223828 | 4757 | return err; |
6c223828 NT |
4758 | } |
4759 | ||
1da177e4 LT |
4760 | static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) |
4761 | { | |
4762 | struct sk_buff *skb; | |
c346dca1 | 4763 | struct net *net = dev_net(ifa->idev->dev); |
5d620266 | 4764 | int err = -ENOBUFS; |
1da177e4 | 4765 | |
0ab6803b | 4766 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); |
63159f29 | 4767 | if (!skb) |
5d620266 TG |
4768 | goto errout; |
4769 | ||
4770 | err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); | |
26932566 PM |
4771 | if (err < 0) { |
4772 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ | |
4773 | WARN_ON(err == -EMSGSIZE); | |
4774 | kfree_skb(skb); | |
4775 | goto errout; | |
4776 | } | |
1ce85fe4 PNA |
4777 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); |
4778 | return; | |
5d620266 TG |
4779 | errout: |
4780 | if (err < 0) | |
6fda7350 | 4781 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); |
1da177e4 LT |
4782 | } |
4783 | ||
b6f99a21 | 4784 | static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, |
1da177e4 LT |
4785 | __s32 *array, int bytes) |
4786 | { | |
04561c1f TG |
4787 | BUG_ON(bytes < (DEVCONF_MAX * 4)); |
4788 | ||
1da177e4 LT |
4789 | memset(array, 0, bytes); |
4790 | array[DEVCONF_FORWARDING] = cnf->forwarding; | |
4791 | array[DEVCONF_HOPLIMIT] = cnf->hop_limit; | |
4792 | array[DEVCONF_MTU6] = cnf->mtu6; | |
4793 | array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; | |
4794 | array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; | |
4795 | array[DEVCONF_AUTOCONF] = cnf->autoconf; | |
4796 | array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; | |
4797 | array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; | |
93908d19 TG |
4798 | array[DEVCONF_RTR_SOLICIT_INTERVAL] = |
4799 | jiffies_to_msecs(cnf->rtr_solicit_interval); | |
4800 | array[DEVCONF_RTR_SOLICIT_DELAY] = | |
4801 | jiffies_to_msecs(cnf->rtr_solicit_delay); | |
1da177e4 | 4802 | array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; |
fc4eba58 HFS |
4803 | array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = |
4804 | jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); | |
4805 | array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = | |
4806 | jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); | |
1da177e4 LT |
4807 | array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; |
4808 | array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; | |
4809 | array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; | |
4810 | array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; | |
4811 | array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; | |
1da177e4 | 4812 | array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; |
65f5c7c1 | 4813 | array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; |
8013d1d7 | 4814 | array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; |
c4fd30eb | 4815 | array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; |
930d6ff2 YH |
4816 | #ifdef CONFIG_IPV6_ROUTER_PREF |
4817 | array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; | |
93908d19 TG |
4818 | array[DEVCONF_RTR_PROBE_INTERVAL] = |
4819 | jiffies_to_msecs(cnf->rtr_probe_interval); | |
fa03ef38 | 4820 | #ifdef CONFIG_IPV6_ROUTE_INFO |
09c884d4 YH |
4821 | array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; |
4822 | #endif | |
930d6ff2 | 4823 | #endif |
fbea49e1 | 4824 | array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; |
0bcbc926 | 4825 | array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; |
95c385b4 NH |
4826 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
4827 | array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; | |
7fd2561e | 4828 | array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; |
95c385b4 | 4829 | #endif |
7bc570c8 YH |
4830 | #ifdef CONFIG_IPV6_MROUTE |
4831 | array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; | |
4832 | #endif | |
778d80be | 4833 | array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; |
1b34be74 | 4834 | array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; |
c3faca05 | 4835 | array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; |
5cb04436 | 4836 | array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; |
b800c3b9 | 4837 | array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; |
d9333196 | 4838 | array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; |
c2943f14 | 4839 | array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; |
35103d11 | 4840 | array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; |
3d1bec99 | 4841 | /* we omit DEVCONF_STABLE_SECRET for now */ |
3985e8a3 | 4842 | array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; |
abbc3043 | 4843 | array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; |
7a02bf89 | 4844 | array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; |
f1705ec1 | 4845 | array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; |
1da177e4 LT |
4846 | } |
4847 | ||
b382b191 TG |
4848 | static inline size_t inet6_ifla6_size(void) |
4849 | { | |
4850 | return nla_total_size(4) /* IFLA_INET6_FLAGS */ | |
4851 | + nla_total_size(sizeof(struct ifla_cacheinfo)) | |
4852 | + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ | |
4853 | + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ | |
f53adae4 DB |
4854 | + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ |
4855 | + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ | |
b382b191 TG |
4856 | } |
4857 | ||
339bf98f TG |
4858 | static inline size_t inet6_if_nlmsg_size(void) |
4859 | { | |
4860 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
4861 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
4862 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
4863 | + nla_total_size(4) /* IFLA_MTU */ | |
4864 | + nla_total_size(4) /* IFLA_LINK */ | |
0344338b | 4865 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
b382b191 | 4866 | + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ |
339bf98f | 4867 | } |
c5396a31 | 4868 | |
be281e55 | 4869 | static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, |
7d720c3e | 4870 | int items, int bytes) |
7f7d9a6b HX |
4871 | { |
4872 | int i; | |
4873 | int pad = bytes - sizeof(u64) * items; | |
4874 | BUG_ON(pad < 0); | |
4875 | ||
4876 | /* Use put_unaligned() because stats may not be aligned for u64. */ | |
4877 | put_unaligned(items, &stats[0]); | |
4878 | for (i = 1; i < items; i++) | |
be281e55 | 4879 | put_unaligned(atomic_long_read(&mib[i]), &stats[i]); |
7f7d9a6b HX |
4880 | |
4881 | memset(&stats[items], 0, pad); | |
4882 | } | |
4883 | ||
698365fa | 4884 | static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, |
a3a77372 | 4885 | int bytes, size_t syncpoff) |
4ce3c183 | 4886 | { |
a3a77372 R |
4887 | int i, c; |
4888 | u64 buff[IPSTATS_MIB_MAX]; | |
4889 | int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; | |
4890 | ||
4ce3c183 ED |
4891 | BUG_ON(pad < 0); |
4892 | ||
a3a77372 R |
4893 | memset(buff, 0, sizeof(buff)); |
4894 | buff[0] = IPSTATS_MIB_MAX; | |
4ce3c183 | 4895 | |
a3a77372 R |
4896 | for_each_possible_cpu(c) { |
4897 | for (i = 1; i < IPSTATS_MIB_MAX; i++) | |
4898 | buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); | |
4899 | } | |
4900 | ||
4901 | memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); | |
4902 | memset(&stats[IPSTATS_MIB_MAX], 0, pad); | |
4ce3c183 ED |
4903 | } |
4904 | ||
7f7d9a6b HX |
4905 | static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, |
4906 | int bytes) | |
4907 | { | |
e21e8467 | 4908 | switch (attrtype) { |
7f7d9a6b | 4909 | case IFLA_INET6_STATS: |
a3a77372 R |
4910 | __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, |
4911 | offsetof(struct ipstats_mib, syncp)); | |
7f7d9a6b HX |
4912 | break; |
4913 | case IFLA_INET6_ICMP6STATS: | |
be281e55 | 4914 | __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); |
7f7d9a6b HX |
4915 | break; |
4916 | } | |
4917 | } | |
4918 | ||
d5566fd7 SV |
4919 | static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, |
4920 | u32 ext_filter_mask) | |
b382b191 TG |
4921 | { |
4922 | struct nlattr *nla; | |
4923 | struct ifla_cacheinfo ci; | |
4924 | ||
c78679e8 DM |
4925 | if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) |
4926 | goto nla_put_failure; | |
b382b191 | 4927 | ci.max_reasm_len = IPV6_MAXPLEN; |
24912420 DM |
4928 | ci.tstamp = cstamp_delta(idev->tstamp); |
4929 | ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); | |
1f9248e5 | 4930 | ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); |
c78679e8 DM |
4931 | if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) |
4932 | goto nla_put_failure; | |
b382b191 | 4933 | nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); |
63159f29 | 4934 | if (!nla) |
b382b191 TG |
4935 | goto nla_put_failure; |
4936 | ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); | |
4937 | ||
4938 | /* XXX - MC not implemented */ | |
4939 | ||
d5566fd7 SV |
4940 | if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) |
4941 | return 0; | |
4942 | ||
b382b191 | 4943 | nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); |
63159f29 | 4944 | if (!nla) |
b382b191 TG |
4945 | goto nla_put_failure; |
4946 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); | |
4947 | ||
4948 | nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); | |
63159f29 | 4949 | if (!nla) |
b382b191 TG |
4950 | goto nla_put_failure; |
4951 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); | |
4952 | ||
f53adae4 | 4953 | nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); |
63159f29 | 4954 | if (!nla) |
f53adae4 | 4955 | goto nla_put_failure; |
bc91b0f0 JP |
4956 | |
4957 | if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) | |
4958 | goto nla_put_failure; | |
4959 | ||
f53adae4 DB |
4960 | read_lock_bh(&idev->lock); |
4961 | memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); | |
4962 | read_unlock_bh(&idev->lock); | |
4963 | ||
b382b191 TG |
4964 | return 0; |
4965 | ||
4966 | nla_put_failure: | |
4967 | return -EMSGSIZE; | |
4968 | } | |
4969 | ||
b1974ed0 AR |
4970 | static size_t inet6_get_link_af_size(const struct net_device *dev, |
4971 | u32 ext_filter_mask) | |
b382b191 TG |
4972 | { |
4973 | if (!__in6_dev_get(dev)) | |
4974 | return 0; | |
4975 | ||
4976 | return inet6_ifla6_size(); | |
4977 | } | |
4978 | ||
d5566fd7 SV |
4979 | static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, |
4980 | u32 ext_filter_mask) | |
b382b191 TG |
4981 | { |
4982 | struct inet6_dev *idev = __in6_dev_get(dev); | |
4983 | ||
4984 | if (!idev) | |
4985 | return -ENODATA; | |
4986 | ||
d5566fd7 | 4987 | if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) |
b382b191 TG |
4988 | return -EMSGSIZE; |
4989 | ||
4990 | return 0; | |
4991 | } | |
4992 | ||
f53adae4 DB |
4993 | static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) |
4994 | { | |
f53adae4 DB |
4995 | struct inet6_ifaddr *ifp; |
4996 | struct net_device *dev = idev->dev; | |
47e27d5e | 4997 | bool clear_token, update_rs = false; |
dc848292 | 4998 | struct in6_addr ll_addr; |
f53adae4 | 4999 | |
c15b1cca HFS |
5000 | ASSERT_RTNL(); |
5001 | ||
63159f29 | 5002 | if (!token) |
f53adae4 | 5003 | return -EINVAL; |
f53adae4 DB |
5004 | if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) |
5005 | return -EINVAL; | |
f53adae4 DB |
5006 | if (!ipv6_accept_ra(idev)) |
5007 | return -EINVAL; | |
5008 | if (idev->cnf.rtr_solicits <= 0) | |
5009 | return -EINVAL; | |
5010 | ||
5011 | write_lock_bh(&idev->lock); | |
5012 | ||
5013 | BUILD_BUG_ON(sizeof(token->s6_addr) != 16); | |
5014 | memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); | |
5015 | ||
5016 | write_unlock_bh(&idev->lock); | |
5017 | ||
47e27d5e DB |
5018 | clear_token = ipv6_addr_any(token); |
5019 | if (clear_token) | |
5020 | goto update_lft; | |
5021 | ||
dc848292 HFS |
5022 | if (!idev->dead && (idev->if_flags & IF_READY) && |
5023 | !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | | |
5024 | IFA_F_OPTIMISTIC)) { | |
fc403832 DB |
5025 | /* If we're not ready, then normal ifup will take care |
5026 | * of this. Otherwise, we need to request our rs here. | |
5027 | */ | |
5028 | ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); | |
5029 | update_rs = true; | |
5030 | } | |
f53adae4 | 5031 | |
47e27d5e | 5032 | update_lft: |
f53adae4 | 5033 | write_lock_bh(&idev->lock); |
fc403832 | 5034 | |
77ecaace | 5035 | if (update_rs) { |
fc403832 | 5036 | idev->if_flags |= IF_RS_SENT; |
77ecaace HFS |
5037 | idev->rs_probes = 1; |
5038 | addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); | |
5039 | } | |
f53adae4 DB |
5040 | |
5041 | /* Well, that's kinda nasty ... */ | |
5042 | list_for_each_entry(ifp, &idev->addr_list, if_list) { | |
5043 | spin_lock(&ifp->lock); | |
617fe29d | 5044 | if (ifp->tokenized) { |
f53adae4 DB |
5045 | ifp->valid_lft = 0; |
5046 | ifp->prefered_lft = 0; | |
5047 | } | |
5048 | spin_unlock(&ifp->lock); | |
5049 | } | |
5050 | ||
5051 | write_unlock_bh(&idev->lock); | |
b2ed64a9 | 5052 | inet6_ifinfo_notify(RTM_NEWLINK, idev); |
c15b1cca | 5053 | addrconf_verify_rtnl(); |
f53adae4 DB |
5054 | return 0; |
5055 | } | |
5056 | ||
11b1f828 DB |
5057 | static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { |
5058 | [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, | |
5059 | [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, | |
5060 | }; | |
5061 | ||
5062 | static int inet6_validate_link_af(const struct net_device *dev, | |
5063 | const struct nlattr *nla) | |
5064 | { | |
5065 | struct nlattr *tb[IFLA_INET6_MAX + 1]; | |
5066 | ||
5067 | if (dev && !__in6_dev_get(dev)) | |
5068 | return -EAFNOSUPPORT; | |
5069 | ||
5070 | return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); | |
5071 | } | |
5072 | ||
f53adae4 DB |
5073 | static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) |
5074 | { | |
5075 | int err = -EINVAL; | |
5076 | struct inet6_dev *idev = __in6_dev_get(dev); | |
5077 | struct nlattr *tb[IFLA_INET6_MAX + 1]; | |
5078 | ||
5079 | if (!idev) | |
5080 | return -EAFNOSUPPORT; | |
5081 | ||
5082 | if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) | |
5083 | BUG(); | |
5084 | ||
bc91b0f0 | 5085 | if (tb[IFLA_INET6_TOKEN]) { |
f53adae4 | 5086 | err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); |
bc91b0f0 JP |
5087 | if (err) |
5088 | return err; | |
5089 | } | |
5090 | ||
5091 | if (tb[IFLA_INET6_ADDR_GEN_MODE]) { | |
5092 | u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); | |
5093 | ||
5094 | if (mode != IN6_ADDR_GEN_MODE_EUI64 && | |
622c81d5 | 5095 | mode != IN6_ADDR_GEN_MODE_NONE && |
cc9da6cc BM |
5096 | mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && |
5097 | mode != IN6_ADDR_GEN_MODE_RANDOM) | |
622c81d5 HFS |
5098 | return -EINVAL; |
5099 | ||
5100 | if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && | |
5101 | !idev->cnf.stable_secret.initialized && | |
5102 | !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) | |
bc91b0f0 | 5103 | return -EINVAL; |
622c81d5 | 5104 | |
bc91b0f0 JP |
5105 | idev->addr_gen_mode = mode; |
5106 | err = 0; | |
5107 | } | |
f53adae4 DB |
5108 | |
5109 | return err; | |
5110 | } | |
5111 | ||
1ab1457c | 5112 | static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, |
15e47304 | 5113 | u32 portid, u32 seq, int event, unsigned int flags) |
1da177e4 | 5114 | { |
04561c1f | 5115 | struct net_device *dev = idev->dev; |
04561c1f TG |
5116 | struct ifinfomsg *hdr; |
5117 | struct nlmsghdr *nlh; | |
5118 | void *protoinfo; | |
04561c1f | 5119 | |
15e47304 | 5120 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); |
63159f29 | 5121 | if (!nlh) |
26932566 | 5122 | return -EMSGSIZE; |
04561c1f TG |
5123 | |
5124 | hdr = nlmsg_data(nlh); | |
5125 | hdr->ifi_family = AF_INET6; | |
5126 | hdr->__ifi_pad = 0; | |
5127 | hdr->ifi_type = dev->type; | |
5128 | hdr->ifi_index = dev->ifindex; | |
5129 | hdr->ifi_flags = dev_get_flags(dev); | |
5130 | hdr->ifi_change = 0; | |
5131 | ||
c78679e8 DM |
5132 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
5133 | (dev->addr_len && | |
5134 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
5135 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
a54acb3a | 5136 | (dev->ifindex != dev_get_iflink(dev) && |
0344338b AG |
5137 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || |
5138 | nla_put_u8(skb, IFLA_OPERSTATE, | |
5139 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) | |
c78679e8 | 5140 | goto nla_put_failure; |
04561c1f | 5141 | protoinfo = nla_nest_start(skb, IFLA_PROTINFO); |
63159f29 | 5142 | if (!protoinfo) |
04561c1f | 5143 | goto nla_put_failure; |
1da177e4 | 5144 | |
d5566fd7 | 5145 | if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) |
bf99f1bd | 5146 | goto nla_put_failure; |
1da177e4 | 5147 | |
04561c1f | 5148 | nla_nest_end(skb, protoinfo); |
053c095a JB |
5149 | nlmsg_end(skb, nlh); |
5150 | return 0; | |
1da177e4 | 5151 | |
04561c1f | 5152 | nla_put_failure: |
26932566 PM |
5153 | nlmsg_cancel(skb, nlh); |
5154 | return -EMSGSIZE; | |
1da177e4 LT |
5155 | } |
5156 | ||
5157 | static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) | |
5158 | { | |
3b1e0a65 | 5159 | struct net *net = sock_net(skb->sk); |
84d2697d | 5160 | int h, s_h; |
434a8a58 | 5161 | int idx = 0, s_idx; |
1da177e4 LT |
5162 | struct net_device *dev; |
5163 | struct inet6_dev *idev; | |
84d2697d | 5164 | struct hlist_head *head; |
1da177e4 | 5165 | |
84d2697d ED |
5166 | s_h = cb->args[0]; |
5167 | s_idx = cb->args[1]; | |
5168 | ||
5169 | rcu_read_lock(); | |
5170 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
5171 | idx = 0; | |
5172 | head = &net->dev_index_head[h]; | |
b67bfe0d | 5173 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
84d2697d ED |
5174 | if (idx < s_idx) |
5175 | goto cont; | |
5176 | idev = __in6_dev_get(dev); | |
5177 | if (!idev) | |
5178 | goto cont; | |
5179 | if (inet6_fill_ifinfo(skb, idev, | |
15e47304 | 5180 | NETLINK_CB(cb->skb).portid, |
84d2697d | 5181 | cb->nlh->nlmsg_seq, |
053c095a | 5182 | RTM_NEWLINK, NLM_F_MULTI) < 0) |
84d2697d | 5183 | goto out; |
7562f876 | 5184 | cont: |
84d2697d ED |
5185 | idx++; |
5186 | } | |
1da177e4 | 5187 | } |
84d2697d ED |
5188 | out: |
5189 | rcu_read_unlock(); | |
5190 | cb->args[1] = idx; | |
5191 | cb->args[0] = h; | |
1da177e4 LT |
5192 | |
5193 | return skb->len; | |
5194 | } | |
5195 | ||
5196 | void inet6_ifinfo_notify(int event, struct inet6_dev *idev) | |
5197 | { | |
5198 | struct sk_buff *skb; | |
c346dca1 | 5199 | struct net *net = dev_net(idev->dev); |
8d7a76c9 | 5200 | int err = -ENOBUFS; |
1ab1457c | 5201 | |
339bf98f | 5202 | skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); |
63159f29 | 5203 | if (!skb) |
8d7a76c9 TG |
5204 | goto errout; |
5205 | ||
5206 | err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); | |
26932566 PM |
5207 | if (err < 0) { |
5208 | /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ | |
5209 | WARN_ON(err == -EMSGSIZE); | |
5210 | kfree_skb(skb); | |
5211 | goto errout; | |
5212 | } | |
5f75a104 | 5213 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); |
1ce85fe4 | 5214 | return; |
8d7a76c9 TG |
5215 | errout: |
5216 | if (err < 0) | |
5f75a104 | 5217 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); |
1da177e4 LT |
5218 | } |
5219 | ||
339bf98f TG |
5220 | static inline size_t inet6_prefix_nlmsg_size(void) |
5221 | { | |
5222 | return NLMSG_ALIGN(sizeof(struct prefixmsg)) | |
5223 | + nla_total_size(sizeof(struct in6_addr)) | |
5224 | + nla_total_size(sizeof(struct prefix_cacheinfo)); | |
5225 | } | |
c5396a31 | 5226 | |
1da177e4 | 5227 | static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, |
15e47304 | 5228 | struct prefix_info *pinfo, u32 portid, u32 seq, |
6051e2f4 | 5229 | int event, unsigned int flags) |
1da177e4 | 5230 | { |
6051e2f4 TG |
5231 | struct prefixmsg *pmsg; |
5232 | struct nlmsghdr *nlh; | |
1da177e4 LT |
5233 | struct prefix_cacheinfo ci; |
5234 | ||
15e47304 | 5235 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); |
63159f29 | 5236 | if (!nlh) |
26932566 | 5237 | return -EMSGSIZE; |
6051e2f4 TG |
5238 | |
5239 | pmsg = nlmsg_data(nlh); | |
1da177e4 | 5240 | pmsg->prefix_family = AF_INET6; |
8a47077a PM |
5241 | pmsg->prefix_pad1 = 0; |
5242 | pmsg->prefix_pad2 = 0; | |
1da177e4 LT |
5243 | pmsg->prefix_ifindex = idev->dev->ifindex; |
5244 | pmsg->prefix_len = pinfo->prefix_len; | |
5245 | pmsg->prefix_type = pinfo->type; | |
8a47077a | 5246 | pmsg->prefix_pad3 = 0; |
1da177e4 LT |
5247 | pmsg->prefix_flags = 0; |
5248 | if (pinfo->onlink) | |
5249 | pmsg->prefix_flags |= IF_PREFIX_ONLINK; | |
5250 | if (pinfo->autoconf) | |
5251 | pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; | |
5252 | ||
c78679e8 DM |
5253 | if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) |
5254 | goto nla_put_failure; | |
1da177e4 LT |
5255 | ci.preferred_time = ntohl(pinfo->prefered); |
5256 | ci.valid_time = ntohl(pinfo->valid); | |
c78679e8 DM |
5257 | if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) |
5258 | goto nla_put_failure; | |
053c095a JB |
5259 | nlmsg_end(skb, nlh); |
5260 | return 0; | |
1da177e4 | 5261 | |
6051e2f4 | 5262 | nla_put_failure: |
26932566 PM |
5263 | nlmsg_cancel(skb, nlh); |
5264 | return -EMSGSIZE; | |
1da177e4 LT |
5265 | } |
5266 | ||
1ab1457c | 5267 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
1da177e4 LT |
5268 | struct prefix_info *pinfo) |
5269 | { | |
5270 | struct sk_buff *skb; | |
c346dca1 | 5271 | struct net *net = dev_net(idev->dev); |
8c384bfa | 5272 | int err = -ENOBUFS; |
1da177e4 | 5273 | |
339bf98f | 5274 | skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); |
63159f29 | 5275 | if (!skb) |
8c384bfa TG |
5276 | goto errout; |
5277 | ||
5278 | err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); | |
26932566 PM |
5279 | if (err < 0) { |
5280 | /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ | |
5281 | WARN_ON(err == -EMSGSIZE); | |
5282 | kfree_skb(skb); | |
5283 | goto errout; | |
5284 | } | |
1ce85fe4 PNA |
5285 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); |
5286 | return; | |
8c384bfa TG |
5287 | errout: |
5288 | if (err < 0) | |
6fda7350 | 5289 | rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); |
1da177e4 LT |
5290 | } |
5291 | ||
1da177e4 LT |
5292 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) |
5293 | { | |
63998ac2 ND |
5294 | struct net *net = dev_net(ifp->idev->dev); |
5295 | ||
c15b1cca HFS |
5296 | if (event) |
5297 | ASSERT_RTNL(); | |
5298 | ||
1da177e4 LT |
5299 | inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); |
5300 | ||
5301 | switch (event) { | |
5302 | case RTM_NEWADDR: | |
95c385b4 NH |
5303 | /* |
5304 | * If the address was optimistic | |
5305 | * we inserted the route at the start of | |
5306 | * our DAD process, so we don't need | |
5307 | * to do it again | |
5308 | */ | |
5309 | if (!(ifp->rt->rt6i_node)) | |
5310 | ip6_ins_rt(ifp->rt); | |
1da177e4 LT |
5311 | if (ifp->idev->cnf.forwarding) |
5312 | addrconf_join_anycast(ifp); | |
7996c799 | 5313 | if (!ipv6_addr_any(&ifp->peer_addr)) |
caeaba79 ND |
5314 | addrconf_prefix_route(&ifp->peer_addr, 128, |
5315 | ifp->idev->dev, 0, 0); | |
1da177e4 LT |
5316 | break; |
5317 | case RTM_DELADDR: | |
5318 | if (ifp->idev->cnf.forwarding) | |
5319 | addrconf_leave_anycast(ifp); | |
5320 | addrconf_leave_solict(ifp->idev, &ifp->addr); | |
7996c799 | 5321 | if (!ipv6_addr_any(&ifp->peer_addr)) { |
caeaba79 | 5322 | struct rt6_info *rt; |
caeaba79 | 5323 | |
e7478dfc ND |
5324 | rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, |
5325 | ifp->idev->dev, 0, 0); | |
8e3d5be7 MKL |
5326 | if (rt) |
5327 | ip6_del_rt(rt); | |
caeaba79 | 5328 | } |
38bd10c4 DA |
5329 | if (ifp->rt) { |
5330 | dst_hold(&ifp->rt->dst); | |
5331 | ip6_del_rt(ifp->rt); | |
5332 | } | |
705f1c86 | 5333 | rt_genid_bump_ipv6(net); |
1da177e4 LT |
5334 | break; |
5335 | } | |
63998ac2 | 5336 | atomic_inc(&net->ipv6.dev_addr_genid); |
1da177e4 LT |
5337 | } |
5338 | ||
5339 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) | |
5340 | { | |
8814c4b5 | 5341 | rcu_read_lock_bh(); |
1da177e4 LT |
5342 | if (likely(ifp->idev->dead == 0)) |
5343 | __ipv6_ifa_notify(event, ifp); | |
8814c4b5 | 5344 | rcu_read_unlock_bh(); |
1da177e4 LT |
5345 | } |
5346 | ||
5347 | #ifdef CONFIG_SYSCTL | |
5348 | ||
5349 | static | |
fe2c6338 | 5350 | int addrconf_sysctl_forward(struct ctl_table *ctl, int write, |
1da177e4 LT |
5351 | void __user *buffer, size_t *lenp, loff_t *ppos) |
5352 | { | |
5353 | int *valp = ctl->data; | |
5354 | int val = *valp; | |
88af182e | 5355 | loff_t pos = *ppos; |
fe2c6338 | 5356 | struct ctl_table lctl; |
1da177e4 LT |
5357 | int ret; |
5358 | ||
013d97e9 FR |
5359 | /* |
5360 | * ctl->data points to idev->cnf.forwarding, we should | |
5361 | * not modify it until we get the rtnl lock. | |
5362 | */ | |
5363 | lctl = *ctl; | |
5364 | lctl.data = &val; | |
5365 | ||
5366 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); | |
1da177e4 | 5367 | |
c8fecf22 | 5368 | if (write) |
b325fddb | 5369 | ret = addrconf_fixup_forwarding(ctl, valp, val); |
88af182e EB |
5370 | if (ret) |
5371 | *ppos = pos; | |
1ab1457c | 5372 | return ret; |
1da177e4 LT |
5373 | } |
5374 | ||
d6df198d PS |
5375 | static |
5376 | int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write, | |
5377 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
5378 | { | |
5379 | struct ctl_table lctl; | |
5380 | int min_hl = 1, max_hl = 255; | |
5381 | ||
5382 | lctl = *ctl; | |
5383 | lctl.extra1 = &min_hl; | |
5384 | lctl.extra2 = &max_hl; | |
5385 | ||
5386 | return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); | |
5387 | } | |
5388 | ||
77751427 ML |
5389 | static |
5390 | int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, | |
5391 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
5392 | { | |
5393 | struct inet6_dev *idev = ctl->extra1; | |
5394 | int min_mtu = IPV6_MIN_MTU; | |
5395 | struct ctl_table lctl; | |
5396 | ||
5397 | lctl = *ctl; | |
5398 | lctl.extra1 = &min_mtu; | |
5399 | lctl.extra2 = idev ? &idev->dev->mtu : NULL; | |
5400 | ||
5401 | return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); | |
5402 | } | |
5403 | ||
56d417b1 BH |
5404 | static void dev_disable_change(struct inet6_dev *idev) |
5405 | { | |
75538c2b CW |
5406 | struct netdev_notifier_info info; |
5407 | ||
56d417b1 BH |
5408 | if (!idev || !idev->dev) |
5409 | return; | |
5410 | ||
75538c2b | 5411 | netdev_notifier_info_init(&info, idev->dev); |
56d417b1 | 5412 | if (idev->cnf.disable_ipv6) |
75538c2b | 5413 | addrconf_notify(NULL, NETDEV_DOWN, &info); |
56d417b1 | 5414 | else |
75538c2b | 5415 | addrconf_notify(NULL, NETDEV_UP, &info); |
56d417b1 BH |
5416 | } |
5417 | ||
5418 | static void addrconf_disable_change(struct net *net, __s32 newf) | |
5419 | { | |
5420 | struct net_device *dev; | |
5421 | struct inet6_dev *idev; | |
5422 | ||
c6d14c84 ED |
5423 | rcu_read_lock(); |
5424 | for_each_netdev_rcu(net, dev) { | |
56d417b1 BH |
5425 | idev = __in6_dev_get(dev); |
5426 | if (idev) { | |
5427 | int changed = (!idev->cnf.disable_ipv6) ^ (!newf); | |
5428 | idev->cnf.disable_ipv6 = newf; | |
5429 | if (changed) | |
5430 | dev_disable_change(idev); | |
5431 | } | |
56d417b1 | 5432 | } |
c6d14c84 | 5433 | rcu_read_unlock(); |
56d417b1 BH |
5434 | } |
5435 | ||
013d97e9 | 5436 | static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) |
56d417b1 BH |
5437 | { |
5438 | struct net *net; | |
013d97e9 FR |
5439 | int old; |
5440 | ||
5441 | if (!rtnl_trylock()) | |
5442 | return restart_syscall(); | |
56d417b1 BH |
5443 | |
5444 | net = (struct net *)table->extra2; | |
013d97e9 FR |
5445 | old = *p; |
5446 | *p = newf; | |
56d417b1 | 5447 | |
013d97e9 FR |
5448 | if (p == &net->ipv6.devconf_dflt->disable_ipv6) { |
5449 | rtnl_unlock(); | |
56d417b1 | 5450 | return 0; |
88af182e | 5451 | } |
56d417b1 BH |
5452 | |
5453 | if (p == &net->ipv6.devconf_all->disable_ipv6) { | |
56d417b1 BH |
5454 | net->ipv6.devconf_dflt->disable_ipv6 = newf; |
5455 | addrconf_disable_change(net, newf); | |
013d97e9 | 5456 | } else if ((!newf) ^ (!old)) |
56d417b1 BH |
5457 | dev_disable_change((struct inet6_dev *)table->extra1); |
5458 | ||
5459 | rtnl_unlock(); | |
5460 | return 0; | |
5461 | } | |
5462 | ||
5463 | static | |
fe2c6338 | 5464 | int addrconf_sysctl_disable(struct ctl_table *ctl, int write, |
56d417b1 BH |
5465 | void __user *buffer, size_t *lenp, loff_t *ppos) |
5466 | { | |
5467 | int *valp = ctl->data; | |
5468 | int val = *valp; | |
88af182e | 5469 | loff_t pos = *ppos; |
fe2c6338 | 5470 | struct ctl_table lctl; |
56d417b1 BH |
5471 | int ret; |
5472 | ||
013d97e9 FR |
5473 | /* |
5474 | * ctl->data points to idev->cnf.disable_ipv6, we should | |
5475 | * not modify it until we get the rtnl lock. | |
5476 | */ | |
5477 | lctl = *ctl; | |
5478 | lctl.data = &val; | |
5479 | ||
5480 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); | |
56d417b1 BH |
5481 | |
5482 | if (write) | |
5483 | ret = addrconf_disable_ipv6(ctl, valp, val); | |
88af182e EB |
5484 | if (ret) |
5485 | *ppos = pos; | |
56d417b1 BH |
5486 | return ret; |
5487 | } | |
5488 | ||
c92d5491 | 5489 | static |
5490 | int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, | |
5491 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
5492 | { | |
5493 | int *valp = ctl->data; | |
5494 | int ret; | |
5495 | int old, new; | |
5496 | ||
5497 | old = *valp; | |
5498 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); | |
5499 | new = *valp; | |
5500 | ||
5501 | if (write && old != new) { | |
5502 | struct net *net = ctl->extra2; | |
5503 | ||
5504 | if (!rtnl_trylock()) | |
5505 | return restart_syscall(); | |
5506 | ||
5507 | if (valp == &net->ipv6.devconf_dflt->proxy_ndp) | |
5508 | inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, | |
5509 | NETCONFA_IFINDEX_DEFAULT, | |
5510 | net->ipv6.devconf_dflt); | |
5511 | else if (valp == &net->ipv6.devconf_all->proxy_ndp) | |
5512 | inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, | |
5513 | NETCONFA_IFINDEX_ALL, | |
5514 | net->ipv6.devconf_all); | |
5515 | else { | |
5516 | struct inet6_dev *idev = ctl->extra1; | |
5517 | ||
5518 | inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, | |
5519 | idev->dev->ifindex, | |
5520 | &idev->cnf); | |
5521 | } | |
5522 | rtnl_unlock(); | |
5523 | } | |
5524 | ||
5525 | return ret; | |
5526 | } | |
5527 | ||
3d1bec99 HFS |
5528 | static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, |
5529 | void __user *buffer, size_t *lenp, | |
5530 | loff_t *ppos) | |
5531 | { | |
5532 | int err; | |
5533 | struct in6_addr addr; | |
5534 | char str[IPV6_MAX_STRLEN]; | |
5535 | struct ctl_table lctl = *ctl; | |
622c81d5 | 5536 | struct net *net = ctl->extra2; |
3d1bec99 HFS |
5537 | struct ipv6_stable_secret *secret = ctl->data; |
5538 | ||
622c81d5 HFS |
5539 | if (&net->ipv6.devconf_all->stable_secret == ctl->data) |
5540 | return -EIO; | |
5541 | ||
3d1bec99 HFS |
5542 | lctl.maxlen = IPV6_MAX_STRLEN; |
5543 | lctl.data = str; | |
5544 | ||
5545 | if (!rtnl_trylock()) | |
5546 | return restart_syscall(); | |
5547 | ||
5548 | if (!write && !secret->initialized) { | |
5549 | err = -EIO; | |
5550 | goto out; | |
5551 | } | |
5552 | ||
5449a5ca WC |
5553 | err = snprintf(str, sizeof(str), "%pI6", &secret->secret); |
5554 | if (err >= sizeof(str)) { | |
5555 | err = -EIO; | |
5556 | goto out; | |
3d1bec99 HFS |
5557 | } |
5558 | ||
5559 | err = proc_dostring(&lctl, write, buffer, lenp, ppos); | |
5560 | if (err || !write) | |
5561 | goto out; | |
5562 | ||
5563 | if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { | |
5564 | err = -EIO; | |
5565 | goto out; | |
5566 | } | |
5567 | ||
5568 | secret->initialized = true; | |
5569 | secret->secret = addr; | |
5570 | ||
622c81d5 HFS |
5571 | if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { |
5572 | struct net_device *dev; | |
5573 | ||
5574 | for_each_netdev(net, dev) { | |
5575 | struct inet6_dev *idev = __in6_dev_get(dev); | |
5576 | ||
5577 | if (idev) { | |
5578 | idev->addr_gen_mode = | |
5579 | IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
5580 | } | |
5581 | } | |
5582 | } else { | |
5583 | struct inet6_dev *idev = ctl->extra1; | |
5584 | ||
5585 | idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
5586 | } | |
5587 | ||
3d1bec99 HFS |
5588 | out: |
5589 | rtnl_unlock(); | |
5590 | ||
5591 | return err; | |
5592 | } | |
c92d5491 | 5593 | |
35103d11 AG |
5594 | static |
5595 | int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, | |
5596 | int write, | |
5597 | void __user *buffer, | |
5598 | size_t *lenp, | |
5599 | loff_t *ppos) | |
5600 | { | |
5601 | int *valp = ctl->data; | |
5602 | int val = *valp; | |
5603 | loff_t pos = *ppos; | |
5604 | struct ctl_table lctl; | |
5605 | int ret; | |
5606 | ||
5607 | /* ctl->data points to idev->cnf.ignore_routes_when_linkdown | |
5608 | * we should not modify it until we get the rtnl lock. | |
5609 | */ | |
5610 | lctl = *ctl; | |
5611 | lctl.data = &val; | |
5612 | ||
5613 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); | |
5614 | ||
5615 | if (write) | |
5616 | ret = addrconf_fixup_linkdown(ctl, valp, val); | |
5617 | if (ret) | |
5618 | *ppos = pos; | |
5619 | return ret; | |
5620 | } | |
5621 | ||
607ea7cd | 5622 | static const struct ctl_table addrconf_sysctl[] = { |
5df1f77f KK |
5623 | { |
5624 | .procname = "forwarding", | |
5625 | .data = &ipv6_devconf.forwarding, | |
5626 | .maxlen = sizeof(int), | |
5627 | .mode = 0644, | |
5628 | .proc_handler = addrconf_sysctl_forward, | |
5629 | }, | |
5630 | { | |
5631 | .procname = "hop_limit", | |
5632 | .data = &ipv6_devconf.hop_limit, | |
5633 | .maxlen = sizeof(int), | |
5634 | .mode = 0644, | |
5635 | .proc_handler = addrconf_sysctl_hop_limit, | |
5636 | }, | |
5637 | { | |
5638 | .procname = "mtu", | |
5639 | .data = &ipv6_devconf.mtu6, | |
5640 | .maxlen = sizeof(int), | |
5641 | .mode = 0644, | |
5642 | .proc_handler = addrconf_sysctl_mtu, | |
5643 | }, | |
5644 | { | |
5645 | .procname = "accept_ra", | |
5646 | .data = &ipv6_devconf.accept_ra, | |
5647 | .maxlen = sizeof(int), | |
5648 | .mode = 0644, | |
5649 | .proc_handler = proc_dointvec, | |
5650 | }, | |
5651 | { | |
5652 | .procname = "accept_redirects", | |
5653 | .data = &ipv6_devconf.accept_redirects, | |
5654 | .maxlen = sizeof(int), | |
5655 | .mode = 0644, | |
5656 | .proc_handler = proc_dointvec, | |
5657 | }, | |
5658 | { | |
5659 | .procname = "autoconf", | |
5660 | .data = &ipv6_devconf.autoconf, | |
5661 | .maxlen = sizeof(int), | |
5662 | .mode = 0644, | |
5663 | .proc_handler = proc_dointvec, | |
5664 | }, | |
5665 | { | |
5666 | .procname = "dad_transmits", | |
5667 | .data = &ipv6_devconf.dad_transmits, | |
5668 | .maxlen = sizeof(int), | |
5669 | .mode = 0644, | |
5670 | .proc_handler = proc_dointvec, | |
5671 | }, | |
5672 | { | |
5673 | .procname = "router_solicitations", | |
5674 | .data = &ipv6_devconf.rtr_solicits, | |
5675 | .maxlen = sizeof(int), | |
5676 | .mode = 0644, | |
5677 | .proc_handler = proc_dointvec, | |
5678 | }, | |
5679 | { | |
5680 | .procname = "router_solicitation_interval", | |
5681 | .data = &ipv6_devconf.rtr_solicit_interval, | |
5682 | .maxlen = sizeof(int), | |
5683 | .mode = 0644, | |
5684 | .proc_handler = proc_dointvec_jiffies, | |
5685 | }, | |
5686 | { | |
5687 | .procname = "router_solicitation_delay", | |
5688 | .data = &ipv6_devconf.rtr_solicit_delay, | |
5689 | .maxlen = sizeof(int), | |
5690 | .mode = 0644, | |
5691 | .proc_handler = proc_dointvec_jiffies, | |
5692 | }, | |
5693 | { | |
5694 | .procname = "force_mld_version", | |
5695 | .data = &ipv6_devconf.force_mld_version, | |
5696 | .maxlen = sizeof(int), | |
5697 | .mode = 0644, | |
5698 | .proc_handler = proc_dointvec, | |
5699 | }, | |
5700 | { | |
5701 | .procname = "mldv1_unsolicited_report_interval", | |
5702 | .data = | |
5703 | &ipv6_devconf.mldv1_unsolicited_report_interval, | |
5704 | .maxlen = sizeof(int), | |
5705 | .mode = 0644, | |
5706 | .proc_handler = proc_dointvec_ms_jiffies, | |
5707 | }, | |
5708 | { | |
5709 | .procname = "mldv2_unsolicited_report_interval", | |
5710 | .data = | |
5711 | &ipv6_devconf.mldv2_unsolicited_report_interval, | |
5712 | .maxlen = sizeof(int), | |
5713 | .mode = 0644, | |
5714 | .proc_handler = proc_dointvec_ms_jiffies, | |
5715 | }, | |
5716 | { | |
5717 | .procname = "use_tempaddr", | |
5718 | .data = &ipv6_devconf.use_tempaddr, | |
5719 | .maxlen = sizeof(int), | |
5720 | .mode = 0644, | |
5721 | .proc_handler = proc_dointvec, | |
5722 | }, | |
5723 | { | |
5724 | .procname = "temp_valid_lft", | |
5725 | .data = &ipv6_devconf.temp_valid_lft, | |
5726 | .maxlen = sizeof(int), | |
5727 | .mode = 0644, | |
5728 | .proc_handler = proc_dointvec, | |
5729 | }, | |
5730 | { | |
5731 | .procname = "temp_prefered_lft", | |
5732 | .data = &ipv6_devconf.temp_prefered_lft, | |
5733 | .maxlen = sizeof(int), | |
5734 | .mode = 0644, | |
5735 | .proc_handler = proc_dointvec, | |
5736 | }, | |
5737 | { | |
5738 | .procname = "regen_max_retry", | |
5739 | .data = &ipv6_devconf.regen_max_retry, | |
5740 | .maxlen = sizeof(int), | |
5741 | .mode = 0644, | |
5742 | .proc_handler = proc_dointvec, | |
5743 | }, | |
5744 | { | |
5745 | .procname = "max_desync_factor", | |
5746 | .data = &ipv6_devconf.max_desync_factor, | |
5747 | .maxlen = sizeof(int), | |
5748 | .mode = 0644, | |
5749 | .proc_handler = proc_dointvec, | |
5750 | }, | |
5751 | { | |
5752 | .procname = "max_addresses", | |
5753 | .data = &ipv6_devconf.max_addresses, | |
5754 | .maxlen = sizeof(int), | |
5755 | .mode = 0644, | |
5756 | .proc_handler = proc_dointvec, | |
5757 | }, | |
5758 | { | |
5759 | .procname = "accept_ra_defrtr", | |
5760 | .data = &ipv6_devconf.accept_ra_defrtr, | |
5761 | .maxlen = sizeof(int), | |
5762 | .mode = 0644, | |
5763 | .proc_handler = proc_dointvec, | |
5764 | }, | |
5765 | { | |
5766 | .procname = "accept_ra_min_hop_limit", | |
5767 | .data = &ipv6_devconf.accept_ra_min_hop_limit, | |
5768 | .maxlen = sizeof(int), | |
5769 | .mode = 0644, | |
5770 | .proc_handler = proc_dointvec, | |
5771 | }, | |
5772 | { | |
5773 | .procname = "accept_ra_pinfo", | |
5774 | .data = &ipv6_devconf.accept_ra_pinfo, | |
5775 | .maxlen = sizeof(int), | |
5776 | .mode = 0644, | |
5777 | .proc_handler = proc_dointvec, | |
5778 | }, | |
930d6ff2 | 5779 | #ifdef CONFIG_IPV6_ROUTER_PREF |
5df1f77f KK |
5780 | { |
5781 | .procname = "accept_ra_rtr_pref", | |
5782 | .data = &ipv6_devconf.accept_ra_rtr_pref, | |
5783 | .maxlen = sizeof(int), | |
5784 | .mode = 0644, | |
5785 | .proc_handler = proc_dointvec, | |
5786 | }, | |
5787 | { | |
5788 | .procname = "router_probe_interval", | |
5789 | .data = &ipv6_devconf.rtr_probe_interval, | |
5790 | .maxlen = sizeof(int), | |
5791 | .mode = 0644, | |
5792 | .proc_handler = proc_dointvec_jiffies, | |
5793 | }, | |
fa03ef38 | 5794 | #ifdef CONFIG_IPV6_ROUTE_INFO |
5df1f77f KK |
5795 | { |
5796 | .procname = "accept_ra_rt_info_max_plen", | |
5797 | .data = &ipv6_devconf.accept_ra_rt_info_max_plen, | |
5798 | .maxlen = sizeof(int), | |
5799 | .mode = 0644, | |
5800 | .proc_handler = proc_dointvec, | |
5801 | }, | |
09c884d4 | 5802 | #endif |
930d6ff2 | 5803 | #endif |
5df1f77f KK |
5804 | { |
5805 | .procname = "proxy_ndp", | |
5806 | .data = &ipv6_devconf.proxy_ndp, | |
5807 | .maxlen = sizeof(int), | |
5808 | .mode = 0644, | |
5809 | .proc_handler = addrconf_sysctl_proxy_ndp, | |
5810 | }, | |
5811 | { | |
5812 | .procname = "accept_source_route", | |
5813 | .data = &ipv6_devconf.accept_source_route, | |
5814 | .maxlen = sizeof(int), | |
5815 | .mode = 0644, | |
5816 | .proc_handler = proc_dointvec, | |
5817 | }, | |
95c385b4 | 5818 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
5df1f77f KK |
5819 | { |
5820 | .procname = "optimistic_dad", | |
5821 | .data = &ipv6_devconf.optimistic_dad, | |
5822 | .maxlen = sizeof(int), | |
5823 | .mode = 0644, | |
5824 | .proc_handler = proc_dointvec, | |
5825 | }, | |
5826 | { | |
5827 | .procname = "use_optimistic", | |
5828 | .data = &ipv6_devconf.use_optimistic, | |
5829 | .maxlen = sizeof(int), | |
5830 | .mode = 0644, | |
5831 | .proc_handler = proc_dointvec, | |
5832 | }, | |
7bc570c8 YH |
5833 | #endif |
5834 | #ifdef CONFIG_IPV6_MROUTE | |
5df1f77f KK |
5835 | { |
5836 | .procname = "mc_forwarding", | |
5837 | .data = &ipv6_devconf.mc_forwarding, | |
5838 | .maxlen = sizeof(int), | |
5839 | .mode = 0444, | |
5840 | .proc_handler = proc_dointvec, | |
5841 | }, | |
95c385b4 | 5842 | #endif |
5df1f77f KK |
5843 | { |
5844 | .procname = "disable_ipv6", | |
5845 | .data = &ipv6_devconf.disable_ipv6, | |
5846 | .maxlen = sizeof(int), | |
5847 | .mode = 0644, | |
5848 | .proc_handler = addrconf_sysctl_disable, | |
5849 | }, | |
5850 | { | |
5851 | .procname = "accept_dad", | |
5852 | .data = &ipv6_devconf.accept_dad, | |
5853 | .maxlen = sizeof(int), | |
5854 | .mode = 0644, | |
5855 | .proc_handler = proc_dointvec, | |
5856 | }, | |
5857 | { | |
5858 | .procname = "force_tllao", | |
5859 | .data = &ipv6_devconf.force_tllao, | |
5860 | .maxlen = sizeof(int), | |
5861 | .mode = 0644, | |
5862 | .proc_handler = proc_dointvec | |
5863 | }, | |
5864 | { | |
5865 | .procname = "ndisc_notify", | |
5866 | .data = &ipv6_devconf.ndisc_notify, | |
5867 | .maxlen = sizeof(int), | |
5868 | .mode = 0644, | |
5869 | .proc_handler = proc_dointvec | |
5870 | }, | |
5871 | { | |
5872 | .procname = "suppress_frag_ndisc", | |
5873 | .data = &ipv6_devconf.suppress_frag_ndisc, | |
5874 | .maxlen = sizeof(int), | |
5875 | .mode = 0644, | |
5876 | .proc_handler = proc_dointvec | |
5877 | }, | |
5878 | { | |
5879 | .procname = "accept_ra_from_local", | |
5880 | .data = &ipv6_devconf.accept_ra_from_local, | |
5881 | .maxlen = sizeof(int), | |
5882 | .mode = 0644, | |
5883 | .proc_handler = proc_dointvec, | |
5884 | }, | |
5885 | { | |
5886 | .procname = "accept_ra_mtu", | |
5887 | .data = &ipv6_devconf.accept_ra_mtu, | |
5888 | .maxlen = sizeof(int), | |
5889 | .mode = 0644, | |
5890 | .proc_handler = proc_dointvec, | |
5891 | }, | |
5892 | { | |
5893 | .procname = "stable_secret", | |
5894 | .data = &ipv6_devconf.stable_secret, | |
5895 | .maxlen = IPV6_MAX_STRLEN, | |
5896 | .mode = 0600, | |
5897 | .proc_handler = addrconf_sysctl_stable_secret, | |
5898 | }, | |
5899 | { | |
5900 | .procname = "use_oif_addrs_only", | |
5901 | .data = &ipv6_devconf.use_oif_addrs_only, | |
5902 | .maxlen = sizeof(int), | |
5903 | .mode = 0644, | |
5904 | .proc_handler = proc_dointvec, | |
5905 | }, | |
5906 | { | |
5907 | .procname = "ignore_routes_with_linkdown", | |
5908 | .data = &ipv6_devconf.ignore_routes_with_linkdown, | |
5909 | .maxlen = sizeof(int), | |
5910 | .mode = 0644, | |
5911 | .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, | |
5912 | }, | |
5913 | { | |
5914 | .procname = "drop_unicast_in_l2_multicast", | |
5915 | .data = &ipv6_devconf.drop_unicast_in_l2_multicast, | |
5916 | .maxlen = sizeof(int), | |
5917 | .mode = 0644, | |
5918 | .proc_handler = proc_dointvec, | |
5919 | }, | |
5920 | { | |
5921 | .procname = "drop_unsolicited_na", | |
5922 | .data = &ipv6_devconf.drop_unsolicited_na, | |
5923 | .maxlen = sizeof(int), | |
5924 | .mode = 0644, | |
5925 | .proc_handler = proc_dointvec, | |
5926 | }, | |
5927 | { | |
5928 | .procname = "keep_addr_on_down", | |
5929 | .data = &ipv6_devconf.keep_addr_on_down, | |
5930 | .maxlen = sizeof(int), | |
5931 | .mode = 0644, | |
5932 | .proc_handler = proc_dointvec, | |
5933 | ||
5934 | }, | |
5935 | { | |
5936 | /* sentinel */ | |
5937 | } | |
1da177e4 LT |
5938 | }; |
5939 | ||
bff16c2f | 5940 | static int __addrconf_sysctl_register(struct net *net, char *dev_name, |
f8572d8f | 5941 | struct inet6_dev *idev, struct ipv6_devconf *p) |
1da177e4 LT |
5942 | { |
5943 | int i; | |
607ea7cd | 5944 | struct ctl_table *table; |
6105e293 | 5945 | char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; |
1dab6222 | 5946 | |
607ea7cd KK |
5947 | table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); |
5948 | if (!table) | |
f68635e6 PE |
5949 | goto out; |
5950 | ||
607ea7cd KK |
5951 | for (i = 0; table[i].data; i++) { |
5952 | table[i].data += (char *)p - (char *)&ipv6_devconf; | |
5953 | table[i].extra1 = idev; /* embedded; no ref */ | |
5954 | table[i].extra2 = net; | |
1da177e4 | 5955 | } |
1da177e4 | 5956 | |
6105e293 | 5957 | snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); |
1da177e4 | 5958 | |
607ea7cd KK |
5959 | p->sysctl_header = register_net_sysctl(net, path, table); |
5960 | if (!p->sysctl_header) | |
6105e293 | 5961 | goto free; |
f68635e6 | 5962 | |
95897312 | 5963 | return 0; |
1da177e4 | 5964 | |
f68635e6 | 5965 | free: |
607ea7cd | 5966 | kfree(table); |
f68635e6 | 5967 | out: |
95897312 | 5968 | return -ENOBUFS; |
1da177e4 LT |
5969 | } |
5970 | ||
408c4768 PE |
5971 | static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) |
5972 | { | |
607ea7cd | 5973 | struct ctl_table *table; |
408c4768 | 5974 | |
607ea7cd | 5975 | if (!p->sysctl_header) |
408c4768 PE |
5976 | return; |
5977 | ||
607ea7cd KK |
5978 | table = p->sysctl_header->ctl_table_arg; |
5979 | unregister_net_sysctl_table(p->sysctl_header); | |
5980 | p->sysctl_header = NULL; | |
5981 | kfree(table); | |
408c4768 PE |
5982 | } |
5983 | ||
a317a2f1 | 5984 | static int addrconf_sysctl_register(struct inet6_dev *idev) |
f52295a9 | 5985 | { |
a317a2f1 WC |
5986 | int err; |
5987 | ||
5988 | if (!sysctl_dev_name_is_allowed(idev->dev->name)) | |
5989 | return -EINVAL; | |
5990 | ||
5991 | err = neigh_sysctl_register(idev->dev, idev->nd_parms, | |
5992 | &ndisc_ifinfo_sysctl_change); | |
5993 | if (err) | |
5994 | return err; | |
5995 | err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, | |
5996 | idev, &idev->cnf); | |
5997 | if (err) | |
5998 | neigh_sysctl_unregister(idev->nd_parms); | |
5999 | ||
6000 | return err; | |
f52295a9 PE |
6001 | } |
6002 | ||
408c4768 | 6003 | static void addrconf_sysctl_unregister(struct inet6_dev *idev) |
1da177e4 | 6004 | { |
408c4768 PE |
6005 | __addrconf_sysctl_unregister(&idev->cnf); |
6006 | neigh_sysctl_unregister(idev->nd_parms); | |
1da177e4 LT |
6007 | } |
6008 | ||
6009 | ||
6010 | #endif | |
6011 | ||
2c8c1e72 | 6012 | static int __net_init addrconf_init_net(struct net *net) |
e0da5a48 | 6013 | { |
a79ca223 | 6014 | int err = -ENOMEM; |
e0da5a48 PE |
6015 | struct ipv6_devconf *all, *dflt; |
6016 | ||
a79ca223 | 6017 | all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); |
63159f29 | 6018 | if (!all) |
a79ca223 | 6019 | goto err_alloc_all; |
e0da5a48 | 6020 | |
a79ca223 | 6021 | dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); |
63159f29 | 6022 | if (!dflt) |
a79ca223 | 6023 | goto err_alloc_dflt; |
e0da5a48 | 6024 | |
a79ca223 HZ |
6025 | /* these will be inherited by all namespaces */ |
6026 | dflt->autoconf = ipv6_defaults.autoconf; | |
6027 | dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; | |
e0da5a48 | 6028 | |
3d1bec99 HFS |
6029 | dflt->stable_secret.initialized = false; |
6030 | all->stable_secret.initialized = false; | |
6031 | ||
e0da5a48 PE |
6032 | net->ipv6.devconf_all = all; |
6033 | net->ipv6.devconf_dflt = dflt; | |
6034 | ||
6035 | #ifdef CONFIG_SYSCTL | |
f8572d8f | 6036 | err = __addrconf_sysctl_register(net, "all", NULL, all); |
e0da5a48 PE |
6037 | if (err < 0) |
6038 | goto err_reg_all; | |
6039 | ||
f8572d8f | 6040 | err = __addrconf_sysctl_register(net, "default", NULL, dflt); |
e0da5a48 PE |
6041 | if (err < 0) |
6042 | goto err_reg_dflt; | |
6043 | #endif | |
6044 | return 0; | |
6045 | ||
6046 | #ifdef CONFIG_SYSCTL | |
6047 | err_reg_dflt: | |
6048 | __addrconf_sysctl_unregister(all); | |
6049 | err_reg_all: | |
6050 | kfree(dflt); | |
6051 | #endif | |
6052 | err_alloc_dflt: | |
6053 | kfree(all); | |
6054 | err_alloc_all: | |
6055 | return err; | |
6056 | } | |
6057 | ||
2c8c1e72 | 6058 | static void __net_exit addrconf_exit_net(struct net *net) |
e0da5a48 PE |
6059 | { |
6060 | #ifdef CONFIG_SYSCTL | |
6061 | __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); | |
6062 | __addrconf_sysctl_unregister(net->ipv6.devconf_all); | |
6063 | #endif | |
73cf0e92 | 6064 | kfree(net->ipv6.devconf_dflt); |
6065 | kfree(net->ipv6.devconf_all); | |
e0da5a48 PE |
6066 | } |
6067 | ||
6068 | static struct pernet_operations addrconf_ops = { | |
6069 | .init = addrconf_init_net, | |
6070 | .exit = addrconf_exit_net, | |
6071 | }; | |
6072 | ||
207895fd | 6073 | static struct rtnl_af_ops inet6_ops __read_mostly = { |
b382b191 TG |
6074 | .family = AF_INET6, |
6075 | .fill_link_af = inet6_fill_link_af, | |
6076 | .get_link_af_size = inet6_get_link_af_size, | |
11b1f828 | 6077 | .validate_link_af = inet6_validate_link_af, |
f53adae4 | 6078 | .set_link_af = inet6_set_link_af, |
b382b191 TG |
6079 | }; |
6080 | ||
1da177e4 LT |
6081 | /* |
6082 | * Init / cleanup code | |
6083 | */ | |
6084 | ||
6085 | int __init addrconf_init(void) | |
6086 | { | |
a317a2f1 | 6087 | struct inet6_dev *idev; |
c2e21293 | 6088 | int i, err; |
2a8cc6c8 | 6089 | |
e21e8467 SH |
6090 | err = ipv6_addr_label_init(); |
6091 | if (err < 0) { | |
f3213831 JP |
6092 | pr_crit("%s: cannot initialize default policy table: %d\n", |
6093 | __func__, err); | |
2cc6d2bf | 6094 | goto out; |
2a8cc6c8 | 6095 | } |
1da177e4 | 6096 | |
2cc6d2bf NH |
6097 | err = register_pernet_subsys(&addrconf_ops); |
6098 | if (err < 0) | |
6099 | goto out_addrlabel; | |
e0da5a48 | 6100 | |
c15b1cca HFS |
6101 | addrconf_wq = create_workqueue("ipv6_addrconf"); |
6102 | if (!addrconf_wq) { | |
6103 | err = -ENOMEM; | |
6104 | goto out_nowq; | |
6105 | } | |
6106 | ||
1da177e4 LT |
6107 | /* The addrconf netdev notifier requires that loopback_dev |
6108 | * has it's ipv6 private information allocated and setup | |
6109 | * before it can bring up and give link-local addresses | |
6110 | * to other devices which are up. | |
6111 | * | |
6112 | * Unfortunately, loopback_dev is not necessarily the first | |
6113 | * entry in the global dev_base list of net devices. In fact, | |
6114 | * it is likely to be the very last entry on that list. | |
6115 | * So this causes the notifier registry below to try and | |
6116 | * give link-local addresses to all devices besides loopback_dev | |
6117 | * first, then loopback_dev, which cases all the non-loopback_dev | |
6118 | * devices to fail to get a link-local address. | |
6119 | * | |
6120 | * So, as a temporary fix, allocate the ipv6 structure for | |
6121 | * loopback_dev first by hand. | |
6122 | * Longer term, all of the dependencies ipv6 has upon the loopback | |
6123 | * device and it being up should be removed. | |
6124 | */ | |
6125 | rtnl_lock(); | |
a317a2f1 | 6126 | idev = ipv6_add_dev(init_net.loopback_dev); |
1da177e4 | 6127 | rtnl_unlock(); |
a317a2f1 WC |
6128 | if (IS_ERR(idev)) { |
6129 | err = PTR_ERR(idev); | |
e0da5a48 | 6130 | goto errlo; |
a317a2f1 | 6131 | } |
1da177e4 | 6132 | |
c2e21293 | 6133 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
6134 | INIT_HLIST_HEAD(&inet6_addr_lst[i]); | |
6135 | ||
1da177e4 LT |
6136 | register_netdevice_notifier(&ipv6_dev_notf); |
6137 | ||
c15b1cca | 6138 | addrconf_verify(); |
c127ea2c | 6139 | |
3678a9d8 | 6140 | rtnl_af_register(&inet6_ops); |
b382b191 | 6141 | |
c7ac8679 GR |
6142 | err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, |
6143 | NULL); | |
c127ea2c TG |
6144 | if (err < 0) |
6145 | goto errout; | |
6146 | ||
6147 | /* Only the first call to __rtnl_register can fail */ | |
c7ac8679 GR |
6148 | __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); |
6149 | __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); | |
6150 | __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, | |
6151 | inet6_dump_ifaddr, NULL); | |
6152 | __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, | |
6153 | inet6_dump_ifmcaddr, NULL); | |
6154 | __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, | |
6155 | inet6_dump_ifacaddr, NULL); | |
76f8f6cb | 6156 | __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, |
7a674200 | 6157 | inet6_netconf_dump_devconf, NULL); |
c127ea2c | 6158 | |
2a8cc6c8 YH |
6159 | ipv6_addr_label_rtnl_register(); |
6160 | ||
1da177e4 | 6161 | return 0; |
c127ea2c | 6162 | errout: |
b382b191 | 6163 | rtnl_af_unregister(&inet6_ops); |
c127ea2c | 6164 | unregister_netdevice_notifier(&ipv6_dev_notf); |
e0da5a48 | 6165 | errlo: |
c15b1cca HFS |
6166 | destroy_workqueue(addrconf_wq); |
6167 | out_nowq: | |
e0da5a48 | 6168 | unregister_pernet_subsys(&addrconf_ops); |
2cc6d2bf NH |
6169 | out_addrlabel: |
6170 | ipv6_addr_label_cleanup(); | |
6171 | out: | |
c127ea2c | 6172 | return err; |
1da177e4 LT |
6173 | } |
6174 | ||
09f7709f | 6175 | void addrconf_cleanup(void) |
1da177e4 | 6176 | { |
176c39af | 6177 | struct net_device *dev; |
1da177e4 LT |
6178 | int i; |
6179 | ||
6180 | unregister_netdevice_notifier(&ipv6_dev_notf); | |
e0da5a48 | 6181 | unregister_pernet_subsys(&addrconf_ops); |
2cc6d2bf | 6182 | ipv6_addr_label_cleanup(); |
1da177e4 LT |
6183 | |
6184 | rtnl_lock(); | |
6185 | ||
b382b191 TG |
6186 | __rtnl_af_unregister(&inet6_ops); |
6187 | ||
176c39af DL |
6188 | /* clean dev list */ |
6189 | for_each_netdev(&init_net, dev) { | |
6190 | if (__in6_dev_get(dev) == NULL) | |
6191 | continue; | |
6192 | addrconf_ifdown(dev, 1); | |
6193 | } | |
6194 | addrconf_ifdown(init_net.loopback_dev, 2); | |
6195 | ||
1da177e4 LT |
6196 | /* |
6197 | * Check hash table. | |
6198 | */ | |
5c578aed | 6199 | spin_lock_bh(&addrconf_hash_lock); |
c2e21293 | 6200 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
6201 | WARN_ON(!hlist_empty(&inet6_addr_lst[i])); | |
5c578aed | 6202 | spin_unlock_bh(&addrconf_hash_lock); |
c15b1cca | 6203 | cancel_delayed_work(&addr_chk_work); |
1da177e4 | 6204 | rtnl_unlock(); |
c15b1cca HFS |
6205 | |
6206 | destroy_workqueue(addrconf_wq); | |
1da177e4 | 6207 | } |