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