Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * NET3 IP device support routines. | |
4 | * | |
1da177e4 | 5 | * Derived from the IP parts of dev.c 1.0.19 |
02c30a84 | 6 | * Authors: Ross Biro |
1da177e4 LT |
7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Alan Cox, <gw4pts@gw4pts.ampr.org> | |
12 | * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
13 | * | |
14 | * Changes: | |
15 | * Alexey Kuznetsov: pa_* fields are replaced with ifaddr | |
16 | * lists. | |
17 | * Cyrus Durgin: updated for kmod | |
18 | * Matthias Andree: in devinet_ioctl, compare label and | |
19 | * address (4.4BSD alias style support), | |
20 | * fall back to comparing just the label | |
21 | * if no match found. | |
22 | */ | |
23 | ||
1da177e4 | 24 | |
7c0f6ba6 | 25 | #include <linux/uaccess.h> |
1da177e4 | 26 | #include <linux/bitops.h> |
4fc268d2 | 27 | #include <linux/capability.h> |
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/types.h> | |
30 | #include <linux/kernel.h> | |
174cd4b1 | 31 | #include <linux/sched/signal.h> |
1da177e4 LT |
32 | #include <linux/string.h> |
33 | #include <linux/mm.h> | |
34 | #include <linux/socket.h> | |
35 | #include <linux/sockios.h> | |
36 | #include <linux/in.h> | |
37 | #include <linux/errno.h> | |
38 | #include <linux/interrupt.h> | |
1823730f | 39 | #include <linux/if_addr.h> |
1da177e4 LT |
40 | #include <linux/if_ether.h> |
41 | #include <linux/inet.h> | |
42 | #include <linux/netdevice.h> | |
43 | #include <linux/etherdevice.h> | |
44 | #include <linux/skbuff.h> | |
1da177e4 LT |
45 | #include <linux/init.h> |
46 | #include <linux/notifier.h> | |
47 | #include <linux/inetdevice.h> | |
48 | #include <linux/igmp.h> | |
eb4e17a1 | 49 | #include "igmp_internal.h" |
5a0e3ad6 | 50 | #include <linux/slab.h> |
fd23c3b3 | 51 | #include <linux/hash.h> |
1da177e4 LT |
52 | #ifdef CONFIG_SYSCTL |
53 | #include <linux/sysctl.h> | |
54 | #endif | |
55 | #include <linux/kmod.h> | |
edc9e748 | 56 | #include <linux/netconf.h> |
1da177e4 | 57 | |
14c85021 | 58 | #include <net/arp.h> |
1da177e4 LT |
59 | #include <net/ip.h> |
60 | #include <net/route.h> | |
61 | #include <net/ip_fib.h> | |
63f3444f | 62 | #include <net/rtnetlink.h> |
752d14dc | 63 | #include <net/net_namespace.h> |
5c766d64 | 64 | #include <net/addrconf.h> |
1da177e4 | 65 | |
2e605463 MC |
66 | #define IPV6ONLY_FLAGS \ |
67 | (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \ | |
68 | IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \ | |
69 | IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY) | |
70 | ||
0027ba84 | 71 | static struct ipv4_devconf ipv4_devconf = { |
42f811b8 | 72 | .data = { |
02291680 EB |
73 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
74 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, | |
75 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, | |
76 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, | |
2690048c WM |
77 | [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, |
78 | [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, | |
fcdb44d0 | 79 | [IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1, |
42f811b8 | 80 | }, |
1da177e4 LT |
81 | }; |
82 | ||
83 | static struct ipv4_devconf ipv4_devconf_dflt = { | |
42f811b8 | 84 | .data = { |
02291680 EB |
85 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
86 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, | |
87 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, | |
88 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, | |
89 | [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1, | |
2690048c WM |
90 | [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, |
91 | [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, | |
fcdb44d0 | 92 | [IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1, |
42f811b8 | 93 | }, |
1da177e4 LT |
94 | }; |
95 | ||
9355bbd6 PE |
96 | #define IPV4_DEVCONF_DFLT(net, attr) \ |
97 | IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) | |
42f811b8 | 98 | |
ef7c79ed | 99 | static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { |
5c753978 TG |
100 | [IFA_LOCAL] = { .type = NLA_U32 }, |
101 | [IFA_ADDRESS] = { .type = NLA_U32 }, | |
102 | [IFA_BROADCAST] = { .type = NLA_U32 }, | |
5176f91e | 103 | [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, |
5c766d64 | 104 | [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, |
ad6c8135 | 105 | [IFA_FLAGS] = { .type = NLA_U32 }, |
af4d768a | 106 | [IFA_RT_PRIORITY] = { .type = NLA_U32 }, |
d3807145 | 107 | [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, |
47f0bd50 | 108 | [IFA_PROTO] = { .type = NLA_U8 }, |
5c753978 TG |
109 | }; |
110 | ||
40384999 ED |
111 | #define IN4_ADDR_HSIZE_SHIFT 8 |
112 | #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT) | |
113 | ||
6eada011 | 114 | static u32 inet_addr_hash(const struct net *net, __be32 addr) |
fd23c3b3 | 115 | { |
c972c1c4 | 116 | u32 val = __ipv4_addr_hash(addr, net_hash_mix(net)); |
fd23c3b3 | 117 | |
40384999 | 118 | return hash_32(val, IN4_ADDR_HSIZE_SHIFT); |
fd23c3b3 DM |
119 | } |
120 | ||
121 | static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa) | |
122 | { | |
40384999 | 123 | u32 hash = inet_addr_hash(net, ifa->ifa_local); |
fd23c3b3 | 124 | |
32a4be48 | 125 | ASSERT_RTNL(); |
87173021 | 126 | hlist_add_head_rcu(&ifa->addr_lst, &net->ipv4.inet_addr_lst[hash]); |
fd23c3b3 DM |
127 | } |
128 | ||
129 | static void inet_hash_remove(struct in_ifaddr *ifa) | |
130 | { | |
32a4be48 | 131 | ASSERT_RTNL(); |
87173021 | 132 | hlist_del_init_rcu(&ifa->addr_lst); |
fd23c3b3 DM |
133 | } |
134 | ||
9435eb1c DM |
135 | /** |
136 | * __ip_dev_find - find the first device with a given source address. | |
137 | * @net: the net namespace | |
138 | * @addr: the source address | |
139 | * @devref: if true, take a reference on the found device | |
140 | * | |
141 | * If a caller uses devref=false, it should be protected by RCU, or RTNL | |
142 | */ | |
143 | struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref) | |
144 | { | |
9435eb1c DM |
145 | struct net_device *result = NULL; |
146 | struct in_ifaddr *ifa; | |
9435eb1c DM |
147 | |
148 | rcu_read_lock(); | |
6e617de8 PA |
149 | ifa = inet_lookup_ifaddr_rcu(net, addr); |
150 | if (!ifa) { | |
406b6f97 DM |
151 | struct flowi4 fl4 = { .daddr = addr }; |
152 | struct fib_result res = { 0 }; | |
153 | struct fib_table *local; | |
154 | ||
155 | /* Fallback to FIB local table so that communication | |
156 | * over loopback subnets work. | |
157 | */ | |
158 | local = fib_get_table(net, RT_TABLE_LOCAL); | |
159 | if (local && | |
160 | !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) && | |
161 | res.type == RTN_LOCAL) | |
162 | result = FIB_RES_DEV(res); | |
6e617de8 PA |
163 | } else { |
164 | result = ifa->ifa_dev->dev; | |
406b6f97 | 165 | } |
9435eb1c DM |
166 | if (result && devref) |
167 | dev_hold(result); | |
168 | rcu_read_unlock(); | |
169 | return result; | |
170 | } | |
171 | EXPORT_SYMBOL(__ip_dev_find); | |
172 | ||
6e617de8 PA |
173 | /* called under RCU lock */ |
174 | struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr) | |
175 | { | |
176 | u32 hash = inet_addr_hash(net, addr); | |
177 | struct in_ifaddr *ifa; | |
178 | ||
49e61319 KI |
179 | hlist_for_each_entry_rcu(ifa, &net->ipv4.inet_addr_lst[hash], addr_lst) |
180 | if (ifa->ifa_local == addr) | |
6e617de8 PA |
181 | return ifa; |
182 | ||
183 | return NULL; | |
184 | } | |
185 | ||
d6062cbb | 186 | static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); |
1da177e4 | 187 | |
e041c683 | 188 | static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); |
3ad7d246 | 189 | static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain); |
2638eb8b FW |
190 | static void inet_del_ifa(struct in_device *in_dev, |
191 | struct in_ifaddr __rcu **ifap, | |
1da177e4 LT |
192 | int destroy); |
193 | #ifdef CONFIG_SYSCTL | |
20e61da7 | 194 | static int devinet_sysctl_register(struct in_device *idev); |
51602b2a PE |
195 | static void devinet_sysctl_unregister(struct in_device *idev); |
196 | #else | |
20e61da7 | 197 | static int devinet_sysctl_register(struct in_device *idev) |
51602b2a | 198 | { |
20e61da7 | 199 | return 0; |
51602b2a | 200 | } |
40384999 | 201 | static void devinet_sysctl_unregister(struct in_device *idev) |
51602b2a PE |
202 | { |
203 | } | |
1da177e4 LT |
204 | #endif |
205 | ||
206 | /* Locks all the inet devices. */ | |
207 | ||
6e701eb9 | 208 | static struct in_ifaddr *inet_alloc_ifa(struct in_device *in_dev) |
1da177e4 | 209 | { |
6e701eb9 KI |
210 | struct in_ifaddr *ifa; |
211 | ||
212 | ifa = kzalloc(sizeof(*ifa), GFP_KERNEL_ACCOUNT); | |
213 | if (!ifa) | |
214 | return NULL; | |
215 | ||
216 | in_dev_hold(in_dev); | |
217 | ifa->ifa_dev = in_dev; | |
218 | ||
87173021 | 219 | INIT_HLIST_NODE(&ifa->addr_lst); |
100465a9 | 220 | |
6e701eb9 | 221 | return ifa; |
1da177e4 LT |
222 | } |
223 | ||
224 | static void inet_rcu_free_ifa(struct rcu_head *head) | |
225 | { | |
226 | struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); | |
61f5338d | 227 | |
ecdae516 | 228 | in_dev_put(ifa->ifa_dev); |
1da177e4 LT |
229 | kfree(ifa); |
230 | } | |
231 | ||
40384999 | 232 | static void inet_free_ifa(struct in_ifaddr *ifa) |
1da177e4 | 233 | { |
61f5338d ED |
234 | /* Our reference to ifa->ifa_dev must be freed ASAP |
235 | * to release the reference to the netdev the same way. | |
236 | * in_dev_put() -> in_dev_finish_destroy() -> netdev_put() | |
237 | */ | |
238 | call_rcu_hurry(&ifa->rcu_head, inet_rcu_free_ifa); | |
1da177e4 LT |
239 | } |
240 | ||
9d40c84c ED |
241 | static void in_dev_free_rcu(struct rcu_head *head) |
242 | { | |
243 | struct in_device *idev = container_of(head, struct in_device, rcu_head); | |
244 | ||
245 | kfree(rcu_dereference_protected(idev->mc_hash, 1)); | |
246 | kfree(idev); | |
247 | } | |
248 | ||
1da177e4 LT |
249 | void in_dev_finish_destroy(struct in_device *idev) |
250 | { | |
251 | struct net_device *dev = idev->dev; | |
252 | ||
547b792c IJ |
253 | WARN_ON(idev->ifa_list); |
254 | WARN_ON(idev->mc_list); | |
1da177e4 | 255 | #ifdef NET_REFCNT_DEBUG |
91df42be | 256 | pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL"); |
1da177e4 | 257 | #endif |
d62607c3 | 258 | netdev_put(dev, &idev->dev_tracker); |
1da177e4 | 259 | if (!idev->dead) |
9f9354b9 ED |
260 | pr_err("Freeing alive in_device %p\n", idev); |
261 | else | |
9d40c84c | 262 | call_rcu(&idev->rcu_head, in_dev_free_rcu); |
1da177e4 | 263 | } |
9f9354b9 | 264 | EXPORT_SYMBOL(in_dev_finish_destroy); |
1da177e4 | 265 | |
71e27da9 | 266 | static struct in_device *inetdev_init(struct net_device *dev) |
1da177e4 LT |
267 | { |
268 | struct in_device *in_dev; | |
20e61da7 | 269 | int err = -ENOMEM; |
1da177e4 LT |
270 | |
271 | ASSERT_RTNL(); | |
272 | ||
0da974f4 | 273 | in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); |
1da177e4 LT |
274 | if (!in_dev) |
275 | goto out; | |
c346dca1 | 276 | memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt, |
9355bbd6 | 277 | sizeof(in_dev->cnf)); |
1da177e4 LT |
278 | in_dev->cnf.sysctl = NULL; |
279 | in_dev->dev = dev; | |
9f9354b9 ED |
280 | in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl); |
281 | if (!in_dev->arp_parms) | |
1da177e4 | 282 | goto out_kfree; |
0187bdfb | 283 | if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) |
d2ccd056 | 284 | netif_disable_lro(dev); |
1da177e4 | 285 | /* Reference in_dev->dev */ |
d62607c3 | 286 | netdev_hold(dev, &in_dev->dev_tracker, GFP_KERNEL); |
30c4cf57 | 287 | /* Account for reference dev->ip_ptr (below) */ |
7658b36f | 288 | refcount_set(&in_dev->refcnt, 1); |
1da177e4 | 289 | |
22600596 XL |
290 | if (dev != blackhole_netdev) { |
291 | err = devinet_sysctl_register(in_dev); | |
292 | if (err) { | |
293 | in_dev->dead = 1; | |
294 | neigh_parms_release(&arp_tbl, in_dev->arp_parms); | |
295 | in_dev_put(in_dev); | |
296 | in_dev = NULL; | |
297 | goto out; | |
298 | } | |
299 | ip_mc_init_dev(in_dev); | |
300 | if (dev->flags & IFF_UP) | |
301 | ip_mc_up(in_dev); | |
20e61da7 | 302 | } |
483479ec | 303 | |
30c4cf57 | 304 | /* we can receive as soon as ip_ptr is set -- do this last */ |
cf778b00 | 305 | rcu_assign_pointer(dev->ip_ptr, in_dev); |
483479ec | 306 | out: |
20e61da7 | 307 | return in_dev ?: ERR_PTR(err); |
1da177e4 LT |
308 | out_kfree: |
309 | kfree(in_dev); | |
310 | in_dev = NULL; | |
311 | goto out; | |
312 | } | |
313 | ||
1da177e4 LT |
314 | static void inetdev_destroy(struct in_device *in_dev) |
315 | { | |
1da177e4 | 316 | struct net_device *dev; |
2638eb8b | 317 | struct in_ifaddr *ifa; |
1da177e4 LT |
318 | |
319 | ASSERT_RTNL(); | |
320 | ||
321 | dev = in_dev->dev; | |
1da177e4 LT |
322 | |
323 | in_dev->dead = 1; | |
324 | ||
325 | ip_mc_destroy_dev(in_dev); | |
326 | ||
2638eb8b | 327 | while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) { |
1da177e4 LT |
328 | inet_del_ifa(in_dev, &in_dev->ifa_list, 0); |
329 | inet_free_ifa(ifa); | |
330 | } | |
331 | ||
a9b3cd7f | 332 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
1da177e4 | 333 | |
51602b2a | 334 | devinet_sysctl_unregister(in_dev); |
1da177e4 LT |
335 | neigh_parms_release(&arp_tbl, in_dev->arp_parms); |
336 | arp_ifdown(dev); | |
337 | ||
9d40c84c | 338 | in_dev_put(in_dev); |
1da177e4 LT |
339 | } |
340 | ||
22600596 XL |
341 | static int __init inet_blackhole_dev_init(void) |
342 | { | |
343 | int err = 0; | |
344 | ||
345 | rtnl_lock(); | |
346 | if (!inetdev_init(blackhole_netdev)) | |
347 | err = -ENOMEM; | |
348 | rtnl_unlock(); | |
349 | ||
350 | return err; | |
351 | } | |
352 | late_initcall(inet_blackhole_dev_init); | |
353 | ||
ff428d72 | 354 | int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) |
1da177e4 | 355 | { |
d519e870 FW |
356 | const struct in_ifaddr *ifa; |
357 | ||
1da177e4 | 358 | rcu_read_lock(); |
d519e870 | 359 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
1da177e4 LT |
360 | if (inet_ifa_match(a, ifa)) { |
361 | if (!b || inet_ifa_match(b, ifa)) { | |
362 | rcu_read_unlock(); | |
363 | return 1; | |
364 | } | |
365 | } | |
d519e870 | 366 | } |
1da177e4 LT |
367 | rcu_read_unlock(); |
368 | return 0; | |
369 | } | |
370 | ||
2638eb8b FW |
371 | static void __inet_del_ifa(struct in_device *in_dev, |
372 | struct in_ifaddr __rcu **ifap, | |
373 | int destroy, struct nlmsghdr *nlh, u32 portid) | |
1da177e4 | 374 | { |
8f937c60 | 375 | struct in_ifaddr *promote = NULL; |
2638eb8b | 376 | struct in_ifaddr *ifa, *ifa1; |
ac28b1ec | 377 | struct in_ifaddr __rcu **last_prim; |
0ff60a45 JHS |
378 | struct in_ifaddr *prev_prom = NULL; |
379 | int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); | |
1da177e4 LT |
380 | |
381 | ASSERT_RTNL(); | |
382 | ||
2638eb8b | 383 | ifa1 = rtnl_dereference(*ifap); |
ac28b1ec | 384 | last_prim = ifap; |
fbd40ea0 DM |
385 | if (in_dev->dead) |
386 | goto no_promotions; | |
387 | ||
e905a9ed | 388 | /* 1. Deleting primary ifaddr forces deletion all secondaries |
8f937c60 HW |
389 | * unless alias promotion is set |
390 | **/ | |
1da177e4 LT |
391 | |
392 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { | |
2638eb8b | 393 | struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next; |
1da177e4 | 394 | |
2638eb8b | 395 | while ((ifa = rtnl_dereference(*ifap1)) != NULL) { |
e905a9ed | 396 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) && |
0ff60a45 | 397 | ifa1->ifa_scope <= ifa->ifa_scope) |
ac28b1ec | 398 | last_prim = &ifa->ifa_next; |
0ff60a45 | 399 | |
1da177e4 LT |
400 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) || |
401 | ifa1->ifa_mask != ifa->ifa_mask || | |
402 | !inet_ifa_match(ifa1->ifa_address, ifa)) { | |
403 | ifap1 = &ifa->ifa_next; | |
0ff60a45 | 404 | prev_prom = ifa; |
1da177e4 LT |
405 | continue; |
406 | } | |
407 | ||
0ff60a45 | 408 | if (!do_promote) { |
fd23c3b3 | 409 | inet_hash_remove(ifa); |
8f937c60 | 410 | *ifap1 = ifa->ifa_next; |
1da177e4 | 411 | |
15e47304 | 412 | rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid); |
e041c683 AS |
413 | blocking_notifier_call_chain(&inetaddr_chain, |
414 | NETDEV_DOWN, ifa); | |
8f937c60 HW |
415 | inet_free_ifa(ifa); |
416 | } else { | |
417 | promote = ifa; | |
418 | break; | |
419 | } | |
1da177e4 LT |
420 | } |
421 | } | |
422 | ||
2d230e2b JA |
423 | /* On promotion all secondaries from subnet are changing |
424 | * the primary IP, we must remove all their routes silently | |
425 | * and later to add them back with new prefsrc. Do this | |
426 | * while all addresses are on the device list. | |
427 | */ | |
2638eb8b | 428 | for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) { |
2d230e2b JA |
429 | if (ifa1->ifa_mask == ifa->ifa_mask && |
430 | inet_ifa_match(ifa1->ifa_address, ifa)) | |
431 | fib_del_ifaddr(ifa, ifa1); | |
432 | } | |
433 | ||
fbd40ea0 | 434 | no_promotions: |
1da177e4 LT |
435 | /* 2. Unlink it */ |
436 | ||
437 | *ifap = ifa1->ifa_next; | |
fd23c3b3 | 438 | inet_hash_remove(ifa1); |
1da177e4 LT |
439 | |
440 | /* 3. Announce address deletion */ | |
441 | ||
442 | /* Send message first, then call notifier. | |
443 | At first sight, FIB update triggered by notifier | |
444 | will refer to already deleted ifaddr, that could confuse | |
445 | netlink listeners. It is not true: look, gated sees | |
446 | that route deleted and if it still thinks that ifaddr | |
447 | is valid, it will try to restore deleted routes... Grr. | |
448 | So that, this order is correct. | |
449 | */ | |
15e47304 | 450 | rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid); |
e041c683 | 451 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); |
1da177e4 | 452 | |
0ff60a45 | 453 | if (promote) { |
2638eb8b | 454 | struct in_ifaddr *next_sec; |
0ff60a45 | 455 | |
2638eb8b | 456 | next_sec = rtnl_dereference(promote->ifa_next); |
0ff60a45 | 457 | if (prev_prom) { |
2638eb8b FW |
458 | struct in_ifaddr *last_sec; |
459 | ||
2638eb8b | 460 | rcu_assign_pointer(prev_prom->ifa_next, next_sec); |
6a9e9cea | 461 | |
ac28b1ec | 462 | last_sec = rtnl_dereference(*last_prim); |
2638eb8b | 463 | rcu_assign_pointer(promote->ifa_next, last_sec); |
ac28b1ec | 464 | rcu_assign_pointer(*last_prim, promote); |
0ff60a45 | 465 | } |
8f937c60 | 466 | |
8f937c60 | 467 | promote->ifa_flags &= ~IFA_F_SECONDARY; |
15e47304 | 468 | rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid); |
e041c683 AS |
469 | blocking_notifier_call_chain(&inetaddr_chain, |
470 | NETDEV_UP, promote); | |
2638eb8b FW |
471 | for (ifa = next_sec; ifa; |
472 | ifa = rtnl_dereference(ifa->ifa_next)) { | |
0ff60a45 JHS |
473 | if (ifa1->ifa_mask != ifa->ifa_mask || |
474 | !inet_ifa_match(ifa1->ifa_address, ifa)) | |
475 | continue; | |
476 | fib_add_ifaddr(ifa); | |
477 | } | |
478 | ||
479 | } | |
6363097c | 480 | if (destroy) |
0ff60a45 | 481 | inet_free_ifa(ifa1); |
1da177e4 LT |
482 | } |
483 | ||
2638eb8b FW |
484 | static void inet_del_ifa(struct in_device *in_dev, |
485 | struct in_ifaddr __rcu **ifap, | |
d6062cbb TG |
486 | int destroy) |
487 | { | |
488 | __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); | |
489 | } | |
490 | ||
491 | static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, | |
de95e047 | 492 | u32 portid, struct netlink_ext_ack *extack) |
1da177e4 | 493 | { |
2638eb8b | 494 | struct in_ifaddr __rcu **last_primary, **ifap; |
1da177e4 | 495 | struct in_device *in_dev = ifa->ifa_dev; |
1675f385 | 496 | struct net *net = dev_net(in_dev->dev); |
3ad7d246 | 497 | struct in_validator_info ivi; |
2638eb8b | 498 | struct in_ifaddr *ifa1; |
3ad7d246 | 499 | int ret; |
1da177e4 LT |
500 | |
501 | ASSERT_RTNL(); | |
502 | ||
1da177e4 LT |
503 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
504 | last_primary = &in_dev->ifa_list; | |
505 | ||
2e605463 MC |
506 | /* Don't set IPv6 only flags to IPv4 addresses */ |
507 | ifa->ifa_flags &= ~IPV6ONLY_FLAGS; | |
508 | ||
2638eb8b FW |
509 | ifap = &in_dev->ifa_list; |
510 | ifa1 = rtnl_dereference(*ifap); | |
511 | ||
512 | while (ifa1) { | |
1da177e4 LT |
513 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && |
514 | ifa->ifa_scope <= ifa1->ifa_scope) | |
515 | last_primary = &ifa1->ifa_next; | |
516 | if (ifa1->ifa_mask == ifa->ifa_mask && | |
517 | inet_ifa_match(ifa1->ifa_address, ifa)) { | |
518 | if (ifa1->ifa_local == ifa->ifa_local) { | |
519 | inet_free_ifa(ifa); | |
520 | return -EEXIST; | |
521 | } | |
522 | if (ifa1->ifa_scope != ifa->ifa_scope) { | |
b4672c73 | 523 | NL_SET_ERR_MSG(extack, "ipv4: Invalid scope value"); |
1da177e4 LT |
524 | inet_free_ifa(ifa); |
525 | return -EINVAL; | |
526 | } | |
527 | ifa->ifa_flags |= IFA_F_SECONDARY; | |
528 | } | |
2638eb8b FW |
529 | |
530 | ifap = &ifa1->ifa_next; | |
531 | ifa1 = rtnl_dereference(*ifap); | |
1da177e4 LT |
532 | } |
533 | ||
3ad7d246 KJ |
534 | /* Allow any devices that wish to register ifaddr validtors to weigh |
535 | * in now, before changes are committed. The rntl lock is serializing | |
536 | * access here, so the state should not change between a validator call | |
537 | * and a final notify on commit. This isn't invoked on promotion under | |
538 | * the assumption that validators are checking the address itself, and | |
539 | * not the flags. | |
540 | */ | |
541 | ivi.ivi_addr = ifa->ifa_address; | |
542 | ivi.ivi_dev = ifa->ifa_dev; | |
de95e047 | 543 | ivi.extack = extack; |
3ad7d246 KJ |
544 | ret = blocking_notifier_call_chain(&inetaddr_validator_chain, |
545 | NETDEV_UP, &ivi); | |
546 | ret = notifier_to_errno(ret); | |
547 | if (ret) { | |
548 | inet_free_ifa(ifa); | |
549 | return ret; | |
550 | } | |
551 | ||
d4150779 | 552 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) |
1da177e4 | 553 | ifap = last_primary; |
1da177e4 | 554 | |
2638eb8b FW |
555 | rcu_assign_pointer(ifa->ifa_next, *ifap); |
556 | rcu_assign_pointer(*ifap, ifa); | |
1da177e4 | 557 | |
fd23c3b3 DM |
558 | inet_hash_insert(dev_net(in_dev->dev), ifa); |
559 | ||
1675f385 KI |
560 | cancel_delayed_work(&net->ipv4.addr_chk_work); |
561 | queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, 0); | |
5c766d64 | 562 | |
1da177e4 LT |
563 | /* Send message first, then call notifier. |
564 | Notifier will trigger FIB update, so that | |
565 | listeners of netlink will know about new ifaddr */ | |
15e47304 | 566 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid); |
e041c683 | 567 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); |
1da177e4 LT |
568 | |
569 | return 0; | |
570 | } | |
571 | ||
d6062cbb TG |
572 | static int inet_insert_ifa(struct in_ifaddr *ifa) |
573 | { | |
abd0deff KI |
574 | if (!ifa->ifa_local) { |
575 | inet_free_ifa(ifa); | |
576 | return 0; | |
577 | } | |
578 | ||
de95e047 | 579 | return __inet_insert_ifa(ifa, NULL, 0, NULL); |
d6062cbb TG |
580 | } |
581 | ||
1da177e4 LT |
582 | static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) |
583 | { | |
88d1f877 | 584 | struct in_device *in_dev = __in_dev_get_rtnl_net(dev); |
1da177e4 | 585 | |
71e27da9 | 586 | ipv4_devconf_setall(in_dev); |
1d4c8c29 | 587 | neigh_parms_data_state_setall(in_dev->arp_parms); |
6e701eb9 | 588 | |
f97c1e0c | 589 | if (ipv4_is_loopback(ifa->ifa_local)) |
1da177e4 LT |
590 | ifa->ifa_scope = RT_SCOPE_HOST; |
591 | return inet_insert_ifa(ifa); | |
592 | } | |
593 | ||
8723e1b4 ED |
594 | /* Caller must hold RCU or RTNL : |
595 | * We dont take a reference on found in_device | |
596 | */ | |
7fee0ca2 | 597 | struct in_device *inetdev_by_index(struct net *net, int ifindex) |
1da177e4 LT |
598 | { |
599 | struct net_device *dev; | |
600 | struct in_device *in_dev = NULL; | |
c148fc2e ED |
601 | |
602 | rcu_read_lock(); | |
603 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 | 604 | if (dev) |
8723e1b4 | 605 | in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
c148fc2e | 606 | rcu_read_unlock(); |
1da177e4 LT |
607 | return in_dev; |
608 | } | |
9f9354b9 | 609 | EXPORT_SYMBOL(inetdev_by_index); |
1da177e4 LT |
610 | |
611 | /* Called only from RTNL semaphored context. No locks. */ | |
612 | ||
60cad5da AV |
613 | struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, |
614 | __be32 mask) | |
1da177e4 | 615 | { |
d519e870 FW |
616 | struct in_ifaddr *ifa; |
617 | ||
1da177e4 LT |
618 | ASSERT_RTNL(); |
619 | ||
d519e870 | 620 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
1da177e4 LT |
621 | if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) |
622 | return ifa; | |
d519e870 | 623 | } |
1da177e4 LT |
624 | return NULL; |
625 | } | |
626 | ||
690cc863 TC |
627 | static int ip_mc_autojoin_config(struct net *net, bool join, |
628 | const struct in_ifaddr *ifa) | |
93a714d6 | 629 | { |
690cc863 | 630 | #if defined(CONFIG_IP_MULTICAST) |
93a714d6 MC |
631 | struct ip_mreqn mreq = { |
632 | .imr_multiaddr.s_addr = ifa->ifa_address, | |
633 | .imr_ifindex = ifa->ifa_dev->dev->ifindex, | |
634 | }; | |
690cc863 | 635 | struct sock *sk = net->ipv4.mc_autojoin_sk; |
93a714d6 MC |
636 | int ret; |
637 | ||
4df5066f | 638 | ASSERT_RTNL_NET(net); |
93a714d6 MC |
639 | |
640 | lock_sock(sk); | |
641 | if (join) | |
54ff9ef3 | 642 | ret = ip_mc_join_group(sk, &mreq); |
93a714d6 | 643 | else |
54ff9ef3 | 644 | ret = ip_mc_leave_group(sk, &mreq); |
93a714d6 MC |
645 | release_sock(sk); |
646 | ||
647 | return ret; | |
690cc863 TC |
648 | #else |
649 | return -EOPNOTSUPP; | |
650 | #endif | |
93a714d6 MC |
651 | } |
652 | ||
c21ef3e3 DA |
653 | static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, |
654 | struct netlink_ext_ack *extack) | |
1da177e4 | 655 | { |
3b1e0a65 | 656 | struct net *net = sock_net(skb->sk); |
2638eb8b | 657 | struct in_ifaddr __rcu **ifap; |
dfdd5fd4 | 658 | struct nlattr *tb[IFA_MAX+1]; |
1da177e4 | 659 | struct in_device *in_dev; |
dfdd5fd4 | 660 | struct ifaddrmsg *ifm; |
2638eb8b | 661 | struct in_ifaddr *ifa; |
30e2379e | 662 | int err; |
1da177e4 | 663 | |
8cb08174 JB |
664 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
665 | ifa_ipv4_policy, extack); | |
dfdd5fd4 | 666 | if (err < 0) |
4df5066f | 667 | goto out; |
dfdd5fd4 TG |
668 | |
669 | ifm = nlmsg_data(nlh); | |
4df5066f KI |
670 | |
671 | rtnl_net_lock(net); | |
672 | ||
7fee0ca2 | 673 | in_dev = inetdev_by_index(net, ifm->ifa_index); |
51456b29 | 674 | if (!in_dev) { |
b4672c73 | 675 | NL_SET_ERR_MSG(extack, "ipv4: Device not found"); |
dfdd5fd4 | 676 | err = -ENODEV; |
4df5066f | 677 | goto unlock; |
dfdd5fd4 TG |
678 | } |
679 | ||
4df5066f KI |
680 | for (ifap = &in_dev->ifa_list; |
681 | (ifa = rtnl_net_dereference(net, *ifap)) != NULL; | |
1da177e4 | 682 | ifap = &ifa->ifa_next) { |
dfdd5fd4 | 683 | if (tb[IFA_LOCAL] && |
67b61f6c | 684 | ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL])) |
dfdd5fd4 TG |
685 | continue; |
686 | ||
687 | if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) | |
1da177e4 | 688 | continue; |
dfdd5fd4 TG |
689 | |
690 | if (tb[IFA_ADDRESS] && | |
691 | (ifm->ifa_prefixlen != ifa->ifa_prefixlen || | |
67b61f6c | 692 | !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa))) |
dfdd5fd4 TG |
693 | continue; |
694 | ||
93a714d6 | 695 | if (ipv4_is_multicast(ifa->ifa_address)) |
690cc863 | 696 | ip_mc_autojoin_config(net, false, ifa); |
4df5066f | 697 | |
15e47304 | 698 | __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid); |
4df5066f | 699 | goto unlock; |
1da177e4 | 700 | } |
dfdd5fd4 | 701 | |
b4672c73 | 702 | NL_SET_ERR_MSG(extack, "ipv4: Address not found"); |
dfdd5fd4 | 703 | err = -EADDRNOTAVAIL; |
4df5066f KI |
704 | unlock: |
705 | rtnl_net_unlock(net); | |
706 | out: | |
dfdd5fd4 | 707 | return err; |
1da177e4 LT |
708 | } |
709 | ||
5c766d64 JP |
710 | static void check_lifetime(struct work_struct *work) |
711 | { | |
712 | unsigned long now, next, next_sec, next_sched; | |
713 | struct in_ifaddr *ifa; | |
c988d1e8 | 714 | struct hlist_node *n; |
1675f385 | 715 | struct net *net; |
5c766d64 JP |
716 | int i; |
717 | ||
1675f385 | 718 | net = container_of(to_delayed_work(work), struct net, ipv4.addr_chk_work); |
5c766d64 JP |
719 | now = jiffies; |
720 | next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); | |
721 | ||
5c766d64 | 722 | for (i = 0; i < IN4_ADDR_HSIZE; i++) { |
1675f385 | 723 | struct hlist_head *head = &net->ipv4.inet_addr_lst[i]; |
c988d1e8 JP |
724 | bool change_needed = false; |
725 | ||
726 | rcu_read_lock(); | |
1675f385 | 727 | hlist_for_each_entry_rcu(ifa, head, addr_lst) { |
3cd3e72c | 728 | unsigned long age, tstamp; |
9f6fa3c4 | 729 | u32 preferred_lft; |
a5fcf74d | 730 | u32 valid_lft; |
3ddc2231 | 731 | u32 flags; |
5c766d64 | 732 | |
3ddc2231 ED |
733 | flags = READ_ONCE(ifa->ifa_flags); |
734 | if (flags & IFA_F_PERMANENT) | |
5c766d64 JP |
735 | continue; |
736 | ||
9f6fa3c4 | 737 | preferred_lft = READ_ONCE(ifa->ifa_preferred_lft); |
a5fcf74d | 738 | valid_lft = READ_ONCE(ifa->ifa_valid_lft); |
3cd3e72c | 739 | tstamp = READ_ONCE(ifa->ifa_tstamp); |
5c766d64 | 740 | /* We try to batch several events at once. */ |
3cd3e72c | 741 | age = (now - tstamp + |
5c766d64 JP |
742 | ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
743 | ||
a5fcf74d ED |
744 | if (valid_lft != INFINITY_LIFE_TIME && |
745 | age >= valid_lft) { | |
c988d1e8 | 746 | change_needed = true; |
9f6fa3c4 | 747 | } else if (preferred_lft == |
5c766d64 JP |
748 | INFINITY_LIFE_TIME) { |
749 | continue; | |
9f6fa3c4 | 750 | } else if (age >= preferred_lft) { |
a5fcf74d ED |
751 | if (time_before(tstamp + valid_lft * HZ, next)) |
752 | next = tstamp + valid_lft * HZ; | |
5c766d64 | 753 | |
3ddc2231 | 754 | if (!(flags & IFA_F_DEPRECATED)) |
c988d1e8 | 755 | change_needed = true; |
9f6fa3c4 | 756 | } else if (time_before(tstamp + preferred_lft * HZ, |
5c766d64 | 757 | next)) { |
9f6fa3c4 | 758 | next = tstamp + preferred_lft * HZ; |
5c766d64 JP |
759 | } |
760 | } | |
c988d1e8 JP |
761 | rcu_read_unlock(); |
762 | if (!change_needed) | |
763 | continue; | |
c350c476 KI |
764 | |
765 | rtnl_net_lock(net); | |
1675f385 | 766 | hlist_for_each_entry_safe(ifa, n, head, addr_lst) { |
c988d1e8 JP |
767 | unsigned long age; |
768 | ||
769 | if (ifa->ifa_flags & IFA_F_PERMANENT) | |
770 | continue; | |
771 | ||
772 | /* We try to batch several events at once. */ | |
773 | age = (now - ifa->ifa_tstamp + | |
774 | ADDRCONF_TIMER_FUZZ_MINUS) / HZ; | |
775 | ||
776 | if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && | |
777 | age >= ifa->ifa_valid_lft) { | |
2638eb8b FW |
778 | struct in_ifaddr __rcu **ifap; |
779 | struct in_ifaddr *tmp; | |
780 | ||
781 | ifap = &ifa->ifa_dev->ifa_list; | |
c350c476 | 782 | tmp = rtnl_net_dereference(net, *ifap); |
2638eb8b | 783 | while (tmp) { |
40008e92 | 784 | if (tmp == ifa) { |
c988d1e8 JP |
785 | inet_del_ifa(ifa->ifa_dev, |
786 | ifap, 1); | |
787 | break; | |
788 | } | |
2638eb8b | 789 | ifap = &tmp->ifa_next; |
c350c476 | 790 | tmp = rtnl_net_dereference(net, *ifap); |
c988d1e8 JP |
791 | } |
792 | } else if (ifa->ifa_preferred_lft != | |
793 | INFINITY_LIFE_TIME && | |
794 | age >= ifa->ifa_preferred_lft && | |
795 | !(ifa->ifa_flags & IFA_F_DEPRECATED)) { | |
796 | ifa->ifa_flags |= IFA_F_DEPRECATED; | |
797 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); | |
798 | } | |
799 | } | |
c350c476 | 800 | rtnl_net_unlock(net); |
5c766d64 | 801 | } |
5c766d64 JP |
802 | |
803 | next_sec = round_jiffies_up(next); | |
804 | next_sched = next; | |
805 | ||
806 | /* If rounded timeout is accurate enough, accept it. */ | |
807 | if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) | |
808 | next_sched = next_sec; | |
809 | ||
810 | now = jiffies; | |
811 | /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ | |
812 | if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX)) | |
813 | next_sched = now + ADDRCONF_TIMER_FUZZ_MAX; | |
814 | ||
1675f385 KI |
815 | queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, |
816 | next_sched - now); | |
5c766d64 JP |
817 | } |
818 | ||
819 | static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, | |
820 | __u32 prefered_lft) | |
821 | { | |
822 | unsigned long timeout; | |
3ddc2231 | 823 | u32 flags; |
5c766d64 | 824 | |
3ddc2231 | 825 | flags = ifa->ifa_flags & ~(IFA_F_PERMANENT | IFA_F_DEPRECATED); |
5c766d64 JP |
826 | |
827 | timeout = addrconf_timeout_fixup(valid_lft, HZ); | |
828 | if (addrconf_finite_timeout(timeout)) | |
a5fcf74d | 829 | WRITE_ONCE(ifa->ifa_valid_lft, timeout); |
5c766d64 | 830 | else |
3ddc2231 | 831 | flags |= IFA_F_PERMANENT; |
5c766d64 JP |
832 | |
833 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); | |
834 | if (addrconf_finite_timeout(timeout)) { | |
835 | if (timeout == 0) | |
3ddc2231 | 836 | flags |= IFA_F_DEPRECATED; |
9f6fa3c4 | 837 | WRITE_ONCE(ifa->ifa_preferred_lft, timeout); |
5c766d64 | 838 | } |
3ddc2231 | 839 | WRITE_ONCE(ifa->ifa_flags, flags); |
3cd3e72c | 840 | WRITE_ONCE(ifa->ifa_tstamp, jiffies); |
5c766d64 | 841 | if (!ifa->ifa_cstamp) |
3cd3e72c | 842 | WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp); |
5c766d64 JP |
843 | } |
844 | ||
2d34429d KI |
845 | static int inet_validate_rtm(struct nlmsghdr *nlh, struct nlattr **tb, |
846 | struct netlink_ext_ack *extack, | |
847 | __u32 *valid_lft, __u32 *prefered_lft) | |
1da177e4 | 848 | { |
2d34429d | 849 | struct ifaddrmsg *ifm = nlmsg_data(nlh); |
7b218574 | 850 | int err; |
1da177e4 | 851 | |
8cb08174 JB |
852 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
853 | ifa_ipv4_policy, extack); | |
5c753978 | 854 | if (err < 0) |
2d34429d | 855 | return err; |
b4672c73 HL |
856 | |
857 | if (ifm->ifa_prefixlen > 32) { | |
858 | NL_SET_ERR_MSG(extack, "ipv4: Invalid prefix length"); | |
2d34429d | 859 | return -EINVAL; |
b4672c73 HL |
860 | } |
861 | ||
862 | if (!tb[IFA_LOCAL]) { | |
863 | NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied"); | |
2d34429d KI |
864 | return -EINVAL; |
865 | } | |
866 | ||
867 | if (tb[IFA_CACHEINFO]) { | |
868 | struct ifa_cacheinfo *ci; | |
869 | ||
870 | ci = nla_data(tb[IFA_CACHEINFO]); | |
871 | if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) { | |
872 | NL_SET_ERR_MSG(extack, "ipv4: address lifetime invalid"); | |
873 | return -EINVAL; | |
874 | } | |
875 | ||
876 | *valid_lft = ci->ifa_valid; | |
877 | *prefered_lft = ci->ifa_prefered; | |
b4672c73 | 878 | } |
1da177e4 | 879 | |
2d34429d KI |
880 | return 0; |
881 | } | |
882 | ||
883 | static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh, | |
884 | struct nlattr **tb, | |
885 | struct netlink_ext_ack *extack) | |
886 | { | |
887 | struct ifaddrmsg *ifm = nlmsg_data(nlh); | |
888 | struct in_device *in_dev; | |
889 | struct net_device *dev; | |
890 | struct in_ifaddr *ifa; | |
891 | int err; | |
892 | ||
4b8aa9ab | 893 | dev = __dev_get_by_index(net, ifm->ifa_index); |
7b218574 | 894 | err = -ENODEV; |
b4672c73 HL |
895 | if (!dev) { |
896 | NL_SET_ERR_MSG(extack, "ipv4: Device not found"); | |
5c753978 | 897 | goto errout; |
b4672c73 | 898 | } |
1da177e4 | 899 | |
d4b48320 | 900 | in_dev = __in_dev_get_rtnl_net(dev); |
7b218574 | 901 | err = -ENOBUFS; |
51456b29 | 902 | if (!in_dev) |
71e27da9 | 903 | goto errout; |
1da177e4 | 904 | |
6e701eb9 | 905 | ifa = inet_alloc_ifa(in_dev); |
51456b29 | 906 | if (!ifa) |
5c753978 TG |
907 | /* |
908 | * A potential indev allocation can be left alive, it stays | |
909 | * assigned to its device and is destroy with it. | |
910 | */ | |
5c753978 | 911 | goto errout; |
5c753978 | 912 | |
a4e65d36 | 913 | ipv4_devconf_setall(in_dev); |
1d4c8c29 | 914 | neigh_parms_data_state_setall(in_dev->arp_parms); |
5c753978 | 915 | |
51456b29 | 916 | if (!tb[IFA_ADDRESS]) |
5c753978 | 917 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
1da177e4 | 918 | |
1da177e4 LT |
919 | ifa->ifa_prefixlen = ifm->ifa_prefixlen; |
920 | ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); | |
a885a6b2 | 921 | ifa->ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags); |
1da177e4 | 922 | ifa->ifa_scope = ifm->ifa_scope; |
67b61f6c JB |
923 | ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]); |
924 | ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]); | |
5c753978 TG |
925 | |
926 | if (tb[IFA_BROADCAST]) | |
67b61f6c | 927 | ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]); |
5c753978 | 928 | |
5c753978 | 929 | if (tb[IFA_LABEL]) |
872f6903 | 930 | nla_strscpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); |
1da177e4 LT |
931 | else |
932 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
933 | ||
af4d768a DA |
934 | if (tb[IFA_RT_PRIORITY]) |
935 | ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); | |
936 | ||
47f0bd50 JL |
937 | if (tb[IFA_PROTO]) |
938 | ifa->ifa_proto = nla_get_u8(tb[IFA_PROTO]); | |
939 | ||
5c753978 TG |
940 | return ifa; |
941 | ||
942 | errout: | |
943 | return ERR_PTR(err); | |
944 | } | |
945 | ||
d4b48320 | 946 | static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa) |
5c766d64 JP |
947 | { |
948 | struct in_device *in_dev = ifa->ifa_dev; | |
ef11db33 | 949 | struct in_ifaddr *ifa1; |
5c766d64 | 950 | |
d4b48320 | 951 | in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) { |
5c766d64 JP |
952 | if (ifa1->ifa_mask == ifa->ifa_mask && |
953 | inet_ifa_match(ifa1->ifa_address, ifa) && | |
954 | ifa1->ifa_local == ifa->ifa_local) | |
955 | return ifa1; | |
956 | } | |
abd0deff | 957 | |
5c766d64 JP |
958 | return NULL; |
959 | } | |
960 | ||
c21ef3e3 DA |
961 | static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, |
962 | struct netlink_ext_ack *extack) | |
5c753978 | 963 | { |
2d34429d KI |
964 | __u32 prefered_lft = INFINITY_LIFE_TIME; |
965 | __u32 valid_lft = INFINITY_LIFE_TIME; | |
3b1e0a65 | 966 | struct net *net = sock_net(skb->sk); |
5c766d64 | 967 | struct in_ifaddr *ifa_existing; |
2d34429d KI |
968 | struct nlattr *tb[IFA_MAX + 1]; |
969 | struct in_ifaddr *ifa; | |
970 | int ret; | |
5c753978 | 971 | |
2d34429d KI |
972 | ret = inet_validate_rtm(nlh, tb, extack, &valid_lft, &prefered_lft); |
973 | if (ret < 0) | |
974 | return ret; | |
975 | ||
abd0deff KI |
976 | if (!nla_get_in_addr(tb[IFA_LOCAL])) |
977 | return 0; | |
978 | ||
48725778 KI |
979 | rtnl_net_lock(net); |
980 | ||
2d34429d | 981 | ifa = inet_rtm_to_ifa(net, nlh, tb, extack); |
48725778 KI |
982 | if (IS_ERR(ifa)) { |
983 | ret = PTR_ERR(ifa); | |
984 | goto unlock; | |
985 | } | |
5c753978 | 986 | |
d4b48320 | 987 | ifa_existing = find_matching_ifa(net, ifa); |
5c766d64 JP |
988 | if (!ifa_existing) { |
989 | /* It would be best to check for !NLM_F_CREATE here but | |
614d056c | 990 | * userspace already relies on not having to provide this. |
5c766d64 JP |
991 | */ |
992 | set_ifa_lifetime(ifa, valid_lft, prefered_lft); | |
93a714d6 | 993 | if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) { |
2d34429d | 994 | ret = ip_mc_autojoin_config(net, true, ifa); |
93a714d6 | 995 | if (ret < 0) { |
b4672c73 | 996 | NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed"); |
93a714d6 | 997 | inet_free_ifa(ifa); |
48725778 | 998 | goto unlock; |
93a714d6 MC |
999 | } |
1000 | } | |
48725778 KI |
1001 | |
1002 | ret = __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid, extack); | |
5c766d64 | 1003 | } else { |
af4d768a | 1004 | u32 new_metric = ifa->ifa_rt_priority; |
5c4a9aa8 | 1005 | u8 new_proto = ifa->ifa_proto; |
af4d768a | 1006 | |
5c766d64 JP |
1007 | inet_free_ifa(ifa); |
1008 | ||
1009 | if (nlh->nlmsg_flags & NLM_F_EXCL || | |
b4672c73 HL |
1010 | !(nlh->nlmsg_flags & NLM_F_REPLACE)) { |
1011 | NL_SET_ERR_MSG(extack, "ipv4: Address already assigned"); | |
48725778 KI |
1012 | ret = -EEXIST; |
1013 | goto unlock; | |
b4672c73 | 1014 | } |
34e2ed34 | 1015 | ifa = ifa_existing; |
af4d768a DA |
1016 | |
1017 | if (ifa->ifa_rt_priority != new_metric) { | |
1018 | fib_modify_prefix_metric(ifa, new_metric); | |
1019 | ifa->ifa_rt_priority = new_metric; | |
1020 | } | |
1021 | ||
5c4a9aa8 PM |
1022 | ifa->ifa_proto = new_proto; |
1023 | ||
34e2ed34 | 1024 | set_ifa_lifetime(ifa, valid_lft, prefered_lft); |
1675f385 | 1025 | cancel_delayed_work(&net->ipv4.addr_chk_work); |
906e073f | 1026 | queue_delayed_work(system_power_efficient_wq, |
1675f385 | 1027 | &net->ipv4.addr_chk_work, 0); |
34e2ed34 | 1028 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid); |
5c766d64 | 1029 | } |
48725778 KI |
1030 | |
1031 | unlock: | |
1032 | rtnl_net_unlock(net); | |
1033 | ||
1034 | return ret; | |
1da177e4 LT |
1035 | } |
1036 | ||
1037 | /* | |
1038 | * Determine a default network mask, based on the IP address. | |
1039 | */ | |
1040 | ||
40384999 | 1041 | static int inet_abc_len(__be32 addr) |
1da177e4 LT |
1042 | { |
1043 | int rc = -1; /* Something else, probably a multicast. */ | |
1044 | ||
65cab850 | 1045 | if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) |
e905a9ed | 1046 | rc = 0; |
1da177e4 | 1047 | else { |
714e85be | 1048 | __u32 haddr = ntohl(addr); |
714e85be | 1049 | if (IN_CLASSA(haddr)) |
1da177e4 | 1050 | rc = 8; |
714e85be | 1051 | else if (IN_CLASSB(haddr)) |
1da177e4 | 1052 | rc = 16; |
714e85be | 1053 | else if (IN_CLASSC(haddr)) |
1da177e4 | 1054 | rc = 24; |
65cab850 DT |
1055 | else if (IN_CLASSE(haddr)) |
1056 | rc = 32; | |
1da177e4 LT |
1057 | } |
1058 | ||
e905a9ed | 1059 | return rc; |
1da177e4 LT |
1060 | } |
1061 | ||
1062 | ||
03aef17b | 1063 | int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr) |
1da177e4 | 1064 | { |
1da177e4 | 1065 | struct sockaddr_in sin_orig; |
03aef17b | 1066 | struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr; |
2638eb8b | 1067 | struct in_ifaddr __rcu **ifap = NULL; |
1da177e4 | 1068 | struct in_device *in_dev; |
1da177e4 LT |
1069 | struct in_ifaddr *ifa = NULL; |
1070 | struct net_device *dev; | |
1071 | char *colon; | |
1072 | int ret = -EFAULT; | |
1073 | int tryaddrmatch = 0; | |
1074 | ||
03aef17b | 1075 | ifr->ifr_name[IFNAMSIZ - 1] = 0; |
1da177e4 LT |
1076 | |
1077 | /* save original address for comparison */ | |
1078 | memcpy(&sin_orig, sin, sizeof(*sin)); | |
1079 | ||
03aef17b | 1080 | colon = strchr(ifr->ifr_name, ':'); |
1da177e4 LT |
1081 | if (colon) |
1082 | *colon = 0; | |
1083 | ||
03aef17b | 1084 | dev_load(net, ifr->ifr_name); |
1da177e4 | 1085 | |
132adf54 | 1086 | switch (cmd) { |
1da177e4 LT |
1087 | case SIOCGIFADDR: /* Get interface address */ |
1088 | case SIOCGIFBRDADDR: /* Get the broadcast address */ | |
1089 | case SIOCGIFDSTADDR: /* Get the destination address */ | |
1090 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ | |
1091 | /* Note that these ioctls will not sleep, | |
1092 | so that we do not impose a lock. | |
1093 | One day we will be forced to put shlock here (I mean SMP) | |
1094 | */ | |
1095 | tryaddrmatch = (sin_orig.sin_family == AF_INET); | |
1096 | memset(sin, 0, sizeof(*sin)); | |
1097 | sin->sin_family = AF_INET; | |
1098 | break; | |
1099 | ||
1100 | case SIOCSIFFLAGS: | |
bf5b30b8 | 1101 | ret = -EPERM; |
52e804c6 | 1102 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1103 | goto out; |
1104 | break; | |
1105 | case SIOCSIFADDR: /* Set interface address (and family) */ | |
1106 | case SIOCSIFBRDADDR: /* Set the broadcast address */ | |
1107 | case SIOCSIFDSTADDR: /* Set the destination address */ | |
1108 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ | |
bf5b30b8 | 1109 | ret = -EPERM; |
52e804c6 | 1110 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1111 | goto out; |
1112 | ret = -EINVAL; | |
1113 | if (sin->sin_family != AF_INET) | |
1114 | goto out; | |
1115 | break; | |
1116 | default: | |
1117 | ret = -EINVAL; | |
1118 | goto out; | |
1119 | } | |
1120 | ||
88d1f877 | 1121 | rtnl_net_lock(net); |
1da177e4 LT |
1122 | |
1123 | ret = -ENODEV; | |
03aef17b | 1124 | dev = __dev_get_by_name(net, ifr->ifr_name); |
9f9354b9 | 1125 | if (!dev) |
1da177e4 LT |
1126 | goto done; |
1127 | ||
1128 | if (colon) | |
1129 | *colon = ':'; | |
1130 | ||
88d1f877 | 1131 | in_dev = __in_dev_get_rtnl_net(dev); |
9f9354b9 | 1132 | if (in_dev) { |
1da177e4 LT |
1133 | if (tryaddrmatch) { |
1134 | /* Matthias Andree */ | |
1135 | /* compare label and address (4.4BSD style) */ | |
1136 | /* note: we only do this for a limited set of ioctls | |
1137 | and only if the original address family was AF_INET. | |
1138 | This is checked above. */ | |
2638eb8b FW |
1139 | |
1140 | for (ifap = &in_dev->ifa_list; | |
88d1f877 | 1141 | (ifa = rtnl_net_dereference(net, *ifap)) != NULL; |
1da177e4 | 1142 | ifap = &ifa->ifa_next) { |
03aef17b | 1143 | if (!strcmp(ifr->ifr_name, ifa->ifa_label) && |
1da177e4 | 1144 | sin_orig.sin_addr.s_addr == |
6c91afe1 | 1145 | ifa->ifa_local) { |
1da177e4 LT |
1146 | break; /* found */ |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | /* we didn't get a match, maybe the application is | |
1151 | 4.3BSD-style and passed in junk so we fall back to | |
1152 | comparing just the label */ | |
1153 | if (!ifa) { | |
2638eb8b | 1154 | for (ifap = &in_dev->ifa_list; |
88d1f877 | 1155 | (ifa = rtnl_net_dereference(net, *ifap)) != NULL; |
1da177e4 | 1156 | ifap = &ifa->ifa_next) |
03aef17b | 1157 | if (!strcmp(ifr->ifr_name, ifa->ifa_label)) |
1da177e4 LT |
1158 | break; |
1159 | } | |
1160 | } | |
1161 | ||
1162 | ret = -EADDRNOTAVAIL; | |
1163 | if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) | |
1164 | goto done; | |
1165 | ||
132adf54 | 1166 | switch (cmd) { |
1da177e4 | 1167 | case SIOCGIFADDR: /* Get interface address */ |
30e948a3 | 1168 | ret = 0; |
1da177e4 | 1169 | sin->sin_addr.s_addr = ifa->ifa_local; |
03aef17b | 1170 | break; |
1da177e4 LT |
1171 | |
1172 | case SIOCGIFBRDADDR: /* Get the broadcast address */ | |
30e948a3 | 1173 | ret = 0; |
1da177e4 | 1174 | sin->sin_addr.s_addr = ifa->ifa_broadcast; |
03aef17b | 1175 | break; |
1da177e4 LT |
1176 | |
1177 | case SIOCGIFDSTADDR: /* Get the destination address */ | |
30e948a3 | 1178 | ret = 0; |
1da177e4 | 1179 | sin->sin_addr.s_addr = ifa->ifa_address; |
03aef17b | 1180 | break; |
1da177e4 LT |
1181 | |
1182 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ | |
30e948a3 | 1183 | ret = 0; |
1da177e4 | 1184 | sin->sin_addr.s_addr = ifa->ifa_mask; |
03aef17b | 1185 | break; |
1da177e4 LT |
1186 | |
1187 | case SIOCSIFFLAGS: | |
1188 | if (colon) { | |
1189 | ret = -EADDRNOTAVAIL; | |
1190 | if (!ifa) | |
1191 | break; | |
1192 | ret = 0; | |
03aef17b | 1193 | if (!(ifr->ifr_flags & IFF_UP)) |
1da177e4 LT |
1194 | inet_del_ifa(in_dev, ifap, 1); |
1195 | break; | |
1196 | } | |
88d1f877 KI |
1197 | |
1198 | /* NETDEV_UP/DOWN/CHANGE could touch a peer dev */ | |
1199 | ASSERT_RTNL(); | |
567c5e13 | 1200 | ret = dev_change_flags(dev, ifr->ifr_flags, NULL); |
1da177e4 LT |
1201 | break; |
1202 | ||
1203 | case SIOCSIFADDR: /* Set interface address (and family) */ | |
1204 | ret = -EINVAL; | |
1205 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) | |
1206 | break; | |
1207 | ||
1208 | if (!ifa) { | |
1209 | ret = -ENOBUFS; | |
e3af3d3c KI |
1210 | if (!in_dev) |
1211 | break; | |
6e701eb9 | 1212 | ifa = inet_alloc_ifa(in_dev); |
9f9354b9 | 1213 | if (!ifa) |
1da177e4 | 1214 | break; |
100465a9 | 1215 | |
1da177e4 | 1216 | if (colon) |
03aef17b | 1217 | memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ); |
1da177e4 LT |
1218 | else |
1219 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
1220 | } else { | |
1221 | ret = 0; | |
1222 | if (ifa->ifa_local == sin->sin_addr.s_addr) | |
1223 | break; | |
1224 | inet_del_ifa(in_dev, ifap, 0); | |
1225 | ifa->ifa_broadcast = 0; | |
148f9729 | 1226 | ifa->ifa_scope = 0; |
1da177e4 LT |
1227 | } |
1228 | ||
1229 | ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; | |
1230 | ||
1231 | if (!(dev->flags & IFF_POINTOPOINT)) { | |
1232 | ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); | |
1233 | ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); | |
1234 | if ((dev->flags & IFF_BROADCAST) && | |
1235 | ifa->ifa_prefixlen < 31) | |
1236 | ifa->ifa_broadcast = ifa->ifa_address | | |
1237 | ~ifa->ifa_mask; | |
1238 | } else { | |
1239 | ifa->ifa_prefixlen = 32; | |
1240 | ifa->ifa_mask = inet_make_mask(32); | |
1241 | } | |
5c766d64 | 1242 | set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); |
1da177e4 LT |
1243 | ret = inet_set_ifa(dev, ifa); |
1244 | break; | |
1245 | ||
1246 | case SIOCSIFBRDADDR: /* Set the broadcast address */ | |
1247 | ret = 0; | |
1248 | if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { | |
1249 | inet_del_ifa(in_dev, ifap, 0); | |
1250 | ifa->ifa_broadcast = sin->sin_addr.s_addr; | |
1251 | inet_insert_ifa(ifa); | |
1252 | } | |
1253 | break; | |
1254 | ||
1255 | case SIOCSIFDSTADDR: /* Set the destination address */ | |
1256 | ret = 0; | |
1257 | if (ifa->ifa_address == sin->sin_addr.s_addr) | |
1258 | break; | |
1259 | ret = -EINVAL; | |
1260 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) | |
1261 | break; | |
1262 | ret = 0; | |
1263 | inet_del_ifa(in_dev, ifap, 0); | |
1264 | ifa->ifa_address = sin->sin_addr.s_addr; | |
1265 | inet_insert_ifa(ifa); | |
1266 | break; | |
1267 | ||
1268 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ | |
1269 | ||
1270 | /* | |
1271 | * The mask we set must be legal. | |
1272 | */ | |
1273 | ret = -EINVAL; | |
1274 | if (bad_mask(sin->sin_addr.s_addr, 0)) | |
1275 | break; | |
1276 | ret = 0; | |
1277 | if (ifa->ifa_mask != sin->sin_addr.s_addr) { | |
a144ea4b | 1278 | __be32 old_mask = ifa->ifa_mask; |
1da177e4 LT |
1279 | inet_del_ifa(in_dev, ifap, 0); |
1280 | ifa->ifa_mask = sin->sin_addr.s_addr; | |
1281 | ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); | |
1282 | ||
1283 | /* See if current broadcast address matches | |
1284 | * with current netmask, then recalculate | |
1285 | * the broadcast address. Otherwise it's a | |
1286 | * funny address, so don't touch it since | |
1287 | * the user seems to know what (s)he's doing... | |
1288 | */ | |
1289 | if ((dev->flags & IFF_BROADCAST) && | |
1290 | (ifa->ifa_prefixlen < 31) && | |
1291 | (ifa->ifa_broadcast == | |
dcab5e1e | 1292 | (ifa->ifa_local|~old_mask))) { |
1da177e4 LT |
1293 | ifa->ifa_broadcast = (ifa->ifa_local | |
1294 | ~sin->sin_addr.s_addr); | |
1295 | } | |
1296 | inet_insert_ifa(ifa); | |
1297 | } | |
1298 | break; | |
1299 | } | |
1300 | done: | |
88d1f877 | 1301 | rtnl_net_unlock(net); |
1da177e4 LT |
1302 | out: |
1303 | return ret; | |
1da177e4 LT |
1304 | } |
1305 | ||
b0e99d03 | 1306 | int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size) |
1da177e4 | 1307 | { |
7ed8da17 | 1308 | struct in_device *in_dev = __in_dev_get_rtnl_net(dev); |
ef11db33 | 1309 | const struct in_ifaddr *ifa; |
1da177e4 LT |
1310 | struct ifreq ifr; |
1311 | int done = 0; | |
1312 | ||
36fd633e AV |
1313 | if (WARN_ON(size > sizeof(struct ifreq))) |
1314 | goto out; | |
1315 | ||
9f9354b9 | 1316 | if (!in_dev) |
1da177e4 LT |
1317 | goto out; |
1318 | ||
7ed8da17 | 1319 | in_dev_for_each_ifa_rtnl_net(dev_net(dev), ifa, in_dev) { |
1da177e4 | 1320 | if (!buf) { |
36fd633e | 1321 | done += size; |
1da177e4 LT |
1322 | continue; |
1323 | } | |
36fd633e | 1324 | if (len < size) |
1da177e4 LT |
1325 | break; |
1326 | memset(&ifr, 0, sizeof(struct ifreq)); | |
4299c8a9 | 1327 | strcpy(ifr.ifr_name, ifa->ifa_label); |
1da177e4 LT |
1328 | |
1329 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; | |
1330 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = | |
1331 | ifa->ifa_local; | |
1332 | ||
36fd633e | 1333 | if (copy_to_user(buf + done, &ifr, size)) { |
1da177e4 LT |
1334 | done = -EFAULT; |
1335 | break; | |
1336 | } | |
36fd633e AV |
1337 | len -= size; |
1338 | done += size; | |
1da177e4 LT |
1339 | } |
1340 | out: | |
1341 | return done; | |
1342 | } | |
1343 | ||
8b57fd1e GF |
1344 | static __be32 in_dev_select_addr(const struct in_device *in_dev, |
1345 | int scope) | |
1346 | { | |
d519e870 FW |
1347 | const struct in_ifaddr *ifa; |
1348 | ||
1349 | in_dev_for_each_ifa_rcu(ifa, in_dev) { | |
3ddc2231 | 1350 | if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) |
d519e870 | 1351 | continue; |
8b57fd1e GF |
1352 | if (ifa->ifa_scope != RT_SCOPE_LINK && |
1353 | ifa->ifa_scope <= scope) | |
1354 | return ifa->ifa_local; | |
d519e870 | 1355 | } |
8b57fd1e GF |
1356 | |
1357 | return 0; | |
1358 | } | |
1359 | ||
a61ced5d | 1360 | __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) |
1da177e4 | 1361 | { |
d519e870 | 1362 | const struct in_ifaddr *ifa; |
a61ced5d | 1363 | __be32 addr = 0; |
d8c444d5 | 1364 | unsigned char localnet_scope = RT_SCOPE_HOST; |
1da177e4 | 1365 | struct in_device *in_dev; |
719817cd | 1366 | struct net *net; |
3f2fb9a8 | 1367 | int master_idx; |
1da177e4 LT |
1368 | |
1369 | rcu_read_lock(); | |
719817cd | 1370 | net = dev_net_rcu(dev); |
e5ed6399 | 1371 | in_dev = __in_dev_get_rcu(dev); |
1da177e4 LT |
1372 | if (!in_dev) |
1373 | goto no_in_dev; | |
1374 | ||
d8c444d5 SL |
1375 | if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev))) |
1376 | localnet_scope = RT_SCOPE_LINK; | |
1377 | ||
d519e870 | 1378 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
3ddc2231 | 1379 | if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) |
d519e870 | 1380 | continue; |
d8c444d5 | 1381 | if (min(ifa->ifa_scope, localnet_scope) > scope) |
1da177e4 LT |
1382 | continue; |
1383 | if (!dst || inet_ifa_match(dst, ifa)) { | |
1384 | addr = ifa->ifa_local; | |
1385 | break; | |
1386 | } | |
1387 | if (!addr) | |
1388 | addr = ifa->ifa_local; | |
d519e870 | 1389 | } |
1da177e4 LT |
1390 | |
1391 | if (addr) | |
c6d14c84 | 1392 | goto out_unlock; |
9f9354b9 | 1393 | no_in_dev: |
3f2fb9a8 | 1394 | master_idx = l3mdev_master_ifindex_rcu(dev); |
1da177e4 | 1395 | |
17b693cd DL |
1396 | /* For VRFs, the VRF device takes the place of the loopback device, |
1397 | * with addresses on it being preferred. Note in such cases the | |
1398 | * loopback device will be among the devices that fail the master_idx | |
1399 | * equality check in the loop below. | |
1400 | */ | |
1401 | if (master_idx && | |
1402 | (dev = dev_get_by_index_rcu(net, master_idx)) && | |
1403 | (in_dev = __in_dev_get_rcu(dev))) { | |
8b57fd1e GF |
1404 | addr = in_dev_select_addr(in_dev, scope); |
1405 | if (addr) | |
1406 | goto out_unlock; | |
17b693cd DL |
1407 | } |
1408 | ||
1da177e4 | 1409 | /* Not loopback addresses on loopback should be preferred |
ca9f1fd2 | 1410 | in this case. It is important that lo is the first interface |
1da177e4 LT |
1411 | in dev_base list. |
1412 | */ | |
c6d14c84 | 1413 | for_each_netdev_rcu(net, dev) { |
3f2fb9a8 DA |
1414 | if (l3mdev_master_ifindex_rcu(dev) != master_idx) |
1415 | continue; | |
1416 | ||
9f9354b9 ED |
1417 | in_dev = __in_dev_get_rcu(dev); |
1418 | if (!in_dev) | |
1da177e4 LT |
1419 | continue; |
1420 | ||
8b57fd1e GF |
1421 | addr = in_dev_select_addr(in_dev, scope); |
1422 | if (addr) | |
1423 | goto out_unlock; | |
1da177e4 | 1424 | } |
c6d14c84 | 1425 | out_unlock: |
1da177e4 | 1426 | rcu_read_unlock(); |
1da177e4 LT |
1427 | return addr; |
1428 | } | |
9f9354b9 | 1429 | EXPORT_SYMBOL(inet_select_addr); |
1da177e4 | 1430 | |
60cad5da AV |
1431 | static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, |
1432 | __be32 local, int scope) | |
1da177e4 | 1433 | { |
650638a7 | 1434 | unsigned char localnet_scope = RT_SCOPE_HOST; |
ef11db33 | 1435 | const struct in_ifaddr *ifa; |
a144ea4b | 1436 | __be32 addr = 0; |
ef11db33 | 1437 | int same = 0; |
1da177e4 | 1438 | |
650638a7 SL |
1439 | if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev))) |
1440 | localnet_scope = RT_SCOPE_LINK; | |
1441 | ||
ef11db33 | 1442 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
650638a7 SL |
1443 | unsigned char min_scope = min(ifa->ifa_scope, localnet_scope); |
1444 | ||
1da177e4 LT |
1445 | if (!addr && |
1446 | (local == ifa->ifa_local || !local) && | |
650638a7 | 1447 | min_scope <= scope) { |
1da177e4 LT |
1448 | addr = ifa->ifa_local; |
1449 | if (same) | |
1450 | break; | |
1451 | } | |
1452 | if (!same) { | |
1453 | same = (!local || inet_ifa_match(local, ifa)) && | |
1454 | (!dst || inet_ifa_match(dst, ifa)); | |
1455 | if (same && addr) { | |
1456 | if (local || !dst) | |
1457 | break; | |
1458 | /* Is the selected addr into dst subnet? */ | |
1459 | if (inet_ifa_match(addr, ifa)) | |
1460 | break; | |
1461 | /* No, then can we use new local src? */ | |
650638a7 | 1462 | if (min_scope <= scope) { |
1da177e4 LT |
1463 | addr = ifa->ifa_local; |
1464 | break; | |
1465 | } | |
1466 | /* search for large dst subnet for addr */ | |
1467 | same = 0; | |
1468 | } | |
1469 | } | |
ef11db33 | 1470 | } |
1da177e4 | 1471 | |
9f9354b9 | 1472 | return same ? addr : 0; |
1da177e4 LT |
1473 | } |
1474 | ||
1475 | /* | |
1476 | * Confirm that local IP address exists using wildcards: | |
b601fa19 ND |
1477 | * - net: netns to check, cannot be NULL |
1478 | * - in_dev: only on this interface, NULL=any interface | |
1da177e4 LT |
1479 | * - dst: only in the same subnet as dst, 0=any dst |
1480 | * - local: address, 0=autoselect the local address | |
1481 | * - scope: maximum allowed scope value for the local address | |
1482 | */ | |
b601fa19 | 1483 | __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, |
9bd85e32 | 1484 | __be32 dst, __be32 local, int scope) |
1da177e4 | 1485 | { |
60cad5da | 1486 | __be32 addr = 0; |
9bd85e32 | 1487 | struct net_device *dev; |
1da177e4 | 1488 | |
00db4124 | 1489 | if (in_dev) |
9bd85e32 | 1490 | return confirm_addr_indev(in_dev, dst, local, scope); |
1da177e4 | 1491 | |
1da177e4 | 1492 | rcu_read_lock(); |
c6d14c84 | 1493 | for_each_netdev_rcu(net, dev) { |
9f9354b9 ED |
1494 | in_dev = __in_dev_get_rcu(dev); |
1495 | if (in_dev) { | |
1da177e4 LT |
1496 | addr = confirm_addr_indev(in_dev, dst, local, scope); |
1497 | if (addr) | |
1498 | break; | |
1499 | } | |
1500 | } | |
1501 | rcu_read_unlock(); | |
1da177e4 LT |
1502 | |
1503 | return addr; | |
1504 | } | |
eaddcd76 | 1505 | EXPORT_SYMBOL(inet_confirm_addr); |
1da177e4 LT |
1506 | |
1507 | /* | |
1508 | * Device notifier | |
1509 | */ | |
1510 | ||
1511 | int register_inetaddr_notifier(struct notifier_block *nb) | |
1512 | { | |
e041c683 | 1513 | return blocking_notifier_chain_register(&inetaddr_chain, nb); |
1da177e4 | 1514 | } |
9f9354b9 | 1515 | EXPORT_SYMBOL(register_inetaddr_notifier); |
1da177e4 LT |
1516 | |
1517 | int unregister_inetaddr_notifier(struct notifier_block *nb) | |
1518 | { | |
e041c683 | 1519 | return blocking_notifier_chain_unregister(&inetaddr_chain, nb); |
1da177e4 | 1520 | } |
9f9354b9 | 1521 | EXPORT_SYMBOL(unregister_inetaddr_notifier); |
1da177e4 | 1522 | |
3ad7d246 KJ |
1523 | int register_inetaddr_validator_notifier(struct notifier_block *nb) |
1524 | { | |
1525 | return blocking_notifier_chain_register(&inetaddr_validator_chain, nb); | |
1526 | } | |
1527 | EXPORT_SYMBOL(register_inetaddr_validator_notifier); | |
1528 | ||
1529 | int unregister_inetaddr_validator_notifier(struct notifier_block *nb) | |
1530 | { | |
1531 | return blocking_notifier_chain_unregister(&inetaddr_validator_chain, | |
1532 | nb); | |
1533 | } | |
1534 | EXPORT_SYMBOL(unregister_inetaddr_validator_notifier); | |
1535 | ||
9f9354b9 ED |
1536 | /* Rename ifa_labels for a device name change. Make some effort to preserve |
1537 | * existing alias numbering and to create unique labels if possible. | |
1da177e4 LT |
1538 | */ |
1539 | static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) | |
e905a9ed | 1540 | { |
1da177e4 LT |
1541 | struct in_ifaddr *ifa; |
1542 | int named = 0; | |
1543 | ||
ef11db33 | 1544 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
e905a9ed | 1545 | char old[IFNAMSIZ], *dot; |
1da177e4 LT |
1546 | |
1547 | memcpy(old, ifa->ifa_label, IFNAMSIZ); | |
e905a9ed | 1548 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
1da177e4 | 1549 | if (named++ == 0) |
573bf470 | 1550 | goto skip; |
44344b2a | 1551 | dot = strchr(old, ':'); |
51456b29 | 1552 | if (!dot) { |
e905a9ed | 1553 | sprintf(old, ":%d", named); |
1da177e4 LT |
1554 | dot = old; |
1555 | } | |
9f9354b9 | 1556 | if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) |
e905a9ed | 1557 | strcat(ifa->ifa_label, dot); |
9f9354b9 | 1558 | else |
e905a9ed | 1559 | strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); |
573bf470 TG |
1560 | skip: |
1561 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); | |
e905a9ed YH |
1562 | } |
1563 | } | |
1da177e4 | 1564 | |
d11327ad IC |
1565 | static void inetdev_send_gratuitous_arp(struct net_device *dev, |
1566 | struct in_device *in_dev) | |
1567 | ||
1568 | { | |
ef11db33 | 1569 | const struct in_ifaddr *ifa; |
d11327ad | 1570 | |
ef11db33 | 1571 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
b76d0789 ZK |
1572 | arp_send(ARPOP_REQUEST, ETH_P_ARP, |
1573 | ifa->ifa_local, dev, | |
1574 | ifa->ifa_local, NULL, | |
1575 | dev->dev_addr, NULL); | |
1576 | } | |
d11327ad IC |
1577 | } |
1578 | ||
1da177e4 LT |
1579 | /* Called only under RTNL semaphore */ |
1580 | ||
1581 | static int inetdev_event(struct notifier_block *this, unsigned long event, | |
1582 | void *ptr) | |
1583 | { | |
351638e7 | 1584 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
748e2d93 | 1585 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
0115e8e3 | 1586 | |
1da177e4 LT |
1587 | ASSERT_RTNL(); |
1588 | ||
1589 | if (!in_dev) { | |
8030f544 | 1590 | if (event == NETDEV_REGISTER) { |
1da177e4 | 1591 | in_dev = inetdev_init(dev); |
20e61da7 WC |
1592 | if (IS_ERR(in_dev)) |
1593 | return notifier_from_errno(PTR_ERR(in_dev)); | |
0cc217e1 | 1594 | if (dev->flags & IFF_LOOPBACK) { |
42f811b8 HX |
1595 | IN_DEV_CONF_SET(in_dev, NOXFRM, 1); |
1596 | IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); | |
8030f544 | 1597 | } |
06770843 BL |
1598 | } else if (event == NETDEV_CHANGEMTU) { |
1599 | /* Re-enabling IP */ | |
1600 | if (inetdev_valid_mtu(dev->mtu)) | |
1601 | in_dev = inetdev_init(dev); | |
1da177e4 LT |
1602 | } |
1603 | goto out; | |
1604 | } | |
1605 | ||
1606 | switch (event) { | |
1607 | case NETDEV_REGISTER: | |
91df42be | 1608 | pr_debug("%s: bug\n", __func__); |
a9b3cd7f | 1609 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
1da177e4 LT |
1610 | break; |
1611 | case NETDEV_UP: | |
06770843 | 1612 | if (!inetdev_valid_mtu(dev->mtu)) |
1da177e4 | 1613 | break; |
0cc217e1 | 1614 | if (dev->flags & IFF_LOOPBACK) { |
6e701eb9 | 1615 | struct in_ifaddr *ifa = inet_alloc_ifa(in_dev); |
9f9354b9 ED |
1616 | |
1617 | if (ifa) { | |
1da177e4 LT |
1618 | ifa->ifa_local = |
1619 | ifa->ifa_address = htonl(INADDR_LOOPBACK); | |
1620 | ifa->ifa_prefixlen = 8; | |
1621 | ifa->ifa_mask = inet_make_mask(8); | |
1da177e4 LT |
1622 | ifa->ifa_scope = RT_SCOPE_HOST; |
1623 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
5c766d64 JP |
1624 | set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, |
1625 | INFINITY_LIFE_TIME); | |
dfd1582d JP |
1626 | ipv4_devconf_setall(in_dev); |
1627 | neigh_parms_data_state_setall(in_dev->arp_parms); | |
1da177e4 LT |
1628 | inet_insert_ifa(ifa); |
1629 | } | |
1630 | } | |
1631 | ip_mc_up(in_dev); | |
a8eceea8 | 1632 | fallthrough; |
eefef1cf | 1633 | case NETDEV_CHANGEADDR: |
d11327ad IC |
1634 | if (!IN_DEV_ARP_NOTIFY(in_dev)) |
1635 | break; | |
a8eceea8 | 1636 | fallthrough; |
d11327ad | 1637 | case NETDEV_NOTIFY_PEERS: |
a21090cf | 1638 | /* Send gratuitous ARP to notify of link change */ |
d11327ad | 1639 | inetdev_send_gratuitous_arp(dev, in_dev); |
1da177e4 LT |
1640 | break; |
1641 | case NETDEV_DOWN: | |
1642 | ip_mc_down(in_dev); | |
1643 | break; | |
93d9b7d7 | 1644 | case NETDEV_PRE_TYPE_CHANGE: |
75c78500 MS |
1645 | ip_mc_unmap(in_dev); |
1646 | break; | |
93d9b7d7 | 1647 | case NETDEV_POST_TYPE_CHANGE: |
75c78500 MS |
1648 | ip_mc_remap(in_dev); |
1649 | break; | |
1da177e4 | 1650 | case NETDEV_CHANGEMTU: |
06770843 | 1651 | if (inetdev_valid_mtu(dev->mtu)) |
1da177e4 | 1652 | break; |
06770843 | 1653 | /* disable IP when MTU is not enough */ |
a8eceea8 | 1654 | fallthrough; |
1da177e4 LT |
1655 | case NETDEV_UNREGISTER: |
1656 | inetdev_destroy(in_dev); | |
1657 | break; | |
1658 | case NETDEV_CHANGENAME: | |
1659 | /* Do not notify about label change, this event is | |
1660 | * not interesting to applications using netlink. | |
1661 | */ | |
1662 | inetdev_changename(dev, in_dev); | |
1663 | ||
51602b2a | 1664 | devinet_sysctl_unregister(in_dev); |
66f27a52 | 1665 | devinet_sysctl_register(in_dev); |
1da177e4 LT |
1666 | break; |
1667 | } | |
1668 | out: | |
1669 | return NOTIFY_DONE; | |
1670 | } | |
1671 | ||
1672 | static struct notifier_block ip_netdev_notifier = { | |
539afedf | 1673 | .notifier_call = inetdev_event, |
1da177e4 LT |
1674 | }; |
1675 | ||
40384999 | 1676 | static size_t inet_nlmsg_size(void) |
339bf98f TG |
1677 | { |
1678 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) | |
1679 | + nla_total_size(4) /* IFA_ADDRESS */ | |
1680 | + nla_total_size(4) /* IFA_LOCAL */ | |
1681 | + nla_total_size(4) /* IFA_BROADCAST */ | |
ad6c8135 | 1682 | + nla_total_size(IFNAMSIZ) /* IFA_LABEL */ |
63b5f152 | 1683 | + nla_total_size(4) /* IFA_FLAGS */ |
47f0bd50 | 1684 | + nla_total_size(1) /* IFA_PROTO */ |
af4d768a | 1685 | + nla_total_size(4) /* IFA_RT_PRIORITY */ |
63b5f152 | 1686 | + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */ |
339bf98f TG |
1687 | } |
1688 | ||
5c766d64 JP |
1689 | static inline u32 cstamp_delta(unsigned long cstamp) |
1690 | { | |
1691 | return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; | |
1692 | } | |
1693 | ||
1694 | static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, | |
1695 | unsigned long tstamp, u32 preferred, u32 valid) | |
1696 | { | |
1697 | struct ifa_cacheinfo ci; | |
1698 | ||
1699 | ci.cstamp = cstamp_delta(cstamp); | |
1700 | ci.tstamp = cstamp_delta(tstamp); | |
1701 | ci.ifa_prefered = preferred; | |
1702 | ci.ifa_valid = valid; | |
1703 | ||
1704 | return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); | |
1705 | } | |
1706 | ||
cdb2f80f | 1707 | static int inet_fill_ifaddr(struct sk_buff *skb, const struct in_ifaddr *ifa, |
978a46fa | 1708 | struct inet_fill_args *args) |
1da177e4 LT |
1709 | { |
1710 | struct ifaddrmsg *ifm; | |
1711 | struct nlmsghdr *nlh; | |
3cd3e72c | 1712 | unsigned long tstamp; |
5c766d64 | 1713 | u32 preferred, valid; |
1af7f88a | 1714 | u32 flags; |
1da177e4 | 1715 | |
978a46fa CB |
1716 | nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm), |
1717 | args->flags); | |
51456b29 | 1718 | if (!nlh) |
26932566 | 1719 | return -EMSGSIZE; |
47f68512 TG |
1720 | |
1721 | ifm = nlmsg_data(nlh); | |
1da177e4 LT |
1722 | ifm->ifa_family = AF_INET; |
1723 | ifm->ifa_prefixlen = ifa->ifa_prefixlen; | |
1af7f88a ED |
1724 | |
1725 | flags = READ_ONCE(ifa->ifa_flags); | |
1726 | /* Warning : ifm->ifa_flags is an __u8, it holds only 8 bits. | |
1727 | * The 32bit value is given in IFA_FLAGS attribute. | |
1728 | */ | |
1729 | ifm->ifa_flags = (__u8)flags; | |
1730 | ||
1da177e4 LT |
1731 | ifm->ifa_scope = ifa->ifa_scope; |
1732 | ifm->ifa_index = ifa->ifa_dev->dev->ifindex; | |
47f68512 | 1733 | |
978a46fa CB |
1734 | if (args->netnsid >= 0 && |
1735 | nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) | |
d3807145 CB |
1736 | goto nla_put_failure; |
1737 | ||
3cd3e72c | 1738 | tstamp = READ_ONCE(ifa->ifa_tstamp); |
1af7f88a | 1739 | if (!(flags & IFA_F_PERMANENT)) { |
9f6fa3c4 | 1740 | preferred = READ_ONCE(ifa->ifa_preferred_lft); |
a5fcf74d | 1741 | valid = READ_ONCE(ifa->ifa_valid_lft); |
5c766d64 | 1742 | if (preferred != INFINITY_LIFE_TIME) { |
3cd3e72c | 1743 | long tval = (jiffies - tstamp) / HZ; |
5c766d64 JP |
1744 | |
1745 | if (preferred > tval) | |
1746 | preferred -= tval; | |
1747 | else | |
1748 | preferred = 0; | |
1749 | if (valid != INFINITY_LIFE_TIME) { | |
1750 | if (valid > tval) | |
1751 | valid -= tval; | |
1752 | else | |
1753 | valid = 0; | |
1754 | } | |
1755 | } | |
1756 | } else { | |
1757 | preferred = INFINITY_LIFE_TIME; | |
1758 | valid = INFINITY_LIFE_TIME; | |
1759 | } | |
f3756b79 | 1760 | if ((ifa->ifa_address && |
930345ea | 1761 | nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) || |
f3756b79 | 1762 | (ifa->ifa_local && |
930345ea | 1763 | nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) || |
f3756b79 | 1764 | (ifa->ifa_broadcast && |
930345ea | 1765 | nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) || |
f3756b79 | 1766 | (ifa->ifa_label[0] && |
5c766d64 | 1767 | nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) || |
47f0bd50 JL |
1768 | (ifa->ifa_proto && |
1769 | nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) || | |
1af7f88a | 1770 | nla_put_u32(skb, IFA_FLAGS, flags) || |
af4d768a DA |
1771 | (ifa->ifa_rt_priority && |
1772 | nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) || | |
3cd3e72c | 1773 | put_cacheinfo(skb, READ_ONCE(ifa->ifa_cstamp), tstamp, |
5c766d64 | 1774 | preferred, valid)) |
f3756b79 | 1775 | goto nla_put_failure; |
1da177e4 | 1776 | |
053c095a JB |
1777 | nlmsg_end(skb, nlh); |
1778 | return 0; | |
47f68512 TG |
1779 | |
1780 | nla_put_failure: | |
26932566 PM |
1781 | nlmsg_cancel(skb, nlh); |
1782 | return -EMSGSIZE; | |
1da177e4 LT |
1783 | } |
1784 | ||
c33078e3 DA |
1785 | static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, |
1786 | struct inet_fill_args *fillargs, | |
1787 | struct net **tgt_net, struct sock *sk, | |
5fcd266a | 1788 | struct netlink_callback *cb) |
c33078e3 | 1789 | { |
5fcd266a | 1790 | struct netlink_ext_ack *extack = cb->extack; |
c33078e3 DA |
1791 | struct nlattr *tb[IFA_MAX+1]; |
1792 | struct ifaddrmsg *ifm; | |
1793 | int err, i; | |
1794 | ||
7d82cc22 BL |
1795 | ifm = nlmsg_payload(nlh, sizeof(*ifm)); |
1796 | if (!ifm) { | |
c33078e3 DA |
1797 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request"); |
1798 | return -EINVAL; | |
1799 | } | |
1800 | ||
c33078e3 DA |
1801 | if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { |
1802 | NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request"); | |
1803 | return -EINVAL; | |
1804 | } | |
5fcd266a DA |
1805 | |
1806 | fillargs->ifindex = ifm->ifa_index; | |
1807 | if (fillargs->ifindex) { | |
1808 | cb->answer_flags |= NLM_F_DUMP_FILTERED; | |
1809 | fillargs->flags |= NLM_F_DUMP_FILTERED; | |
c33078e3 DA |
1810 | } |
1811 | ||
8cb08174 JB |
1812 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, |
1813 | ifa_ipv4_policy, extack); | |
c33078e3 DA |
1814 | if (err < 0) |
1815 | return err; | |
1816 | ||
1817 | for (i = 0; i <= IFA_MAX; ++i) { | |
1818 | if (!tb[i]) | |
1819 | continue; | |
1820 | ||
1821 | if (i == IFA_TARGET_NETNSID) { | |
1822 | struct net *net; | |
1823 | ||
1824 | fillargs->netnsid = nla_get_s32(tb[i]); | |
1825 | ||
1826 | net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); | |
1827 | if (IS_ERR(net)) { | |
bf4cc40e | 1828 | fillargs->netnsid = -1; |
c33078e3 DA |
1829 | NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id"); |
1830 | return PTR_ERR(net); | |
1831 | } | |
1832 | *tgt_net = net; | |
1833 | } else { | |
1834 | NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request"); | |
1835 | return -EINVAL; | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | return 0; | |
1840 | } | |
1841 | ||
eb4e17a1 YH |
1842 | static int in_dev_dump_ifmcaddr(struct in_device *in_dev, struct sk_buff *skb, |
1843 | struct netlink_callback *cb, int *s_ip_idx, | |
1844 | struct inet_fill_args *fillargs) | |
1845 | { | |
1846 | struct ip_mc_list *im; | |
1847 | int ip_idx = 0; | |
1848 | int err; | |
1849 | ||
1850 | for (im = rcu_dereference(in_dev->mc_list); | |
1851 | im; | |
1852 | im = rcu_dereference(im->next_rcu)) { | |
1853 | if (ip_idx < *s_ip_idx) { | |
1854 | ip_idx++; | |
1855 | continue; | |
1856 | } | |
1857 | err = inet_fill_ifmcaddr(skb, in_dev->dev, im, fillargs); | |
1858 | if (err < 0) | |
1859 | goto done; | |
1860 | ||
1861 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
1862 | ip_idx++; | |
1863 | } | |
1864 | err = 0; | |
1865 | ip_idx = 0; | |
1866 | done: | |
1867 | *s_ip_idx = ip_idx; | |
1868 | return err; | |
1869 | } | |
1870 | ||
1871 | static int in_dev_dump_ifaddr(struct in_device *in_dev, struct sk_buff *skb, | |
1872 | struct netlink_callback *cb, int *s_ip_idx, | |
1873 | struct inet_fill_args *fillargs) | |
1c98eca4 DA |
1874 | { |
1875 | struct in_ifaddr *ifa; | |
1876 | int ip_idx = 0; | |
1877 | int err; | |
1878 | ||
cdb2f80f ED |
1879 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
1880 | if (ip_idx < *s_ip_idx) { | |
ef11db33 | 1881 | ip_idx++; |
1c98eca4 | 1882 | continue; |
ef11db33 | 1883 | } |
1c98eca4 DA |
1884 | err = inet_fill_ifaddr(skb, ifa, fillargs); |
1885 | if (err < 0) | |
1886 | goto done; | |
1887 | ||
1888 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
ef11db33 | 1889 | ip_idx++; |
1c98eca4 DA |
1890 | } |
1891 | err = 0; | |
cdb2f80f | 1892 | ip_idx = 0; |
1c98eca4 | 1893 | done: |
cdb2f80f | 1894 | *s_ip_idx = ip_idx; |
1c98eca4 DA |
1895 | |
1896 | return err; | |
1897 | } | |
1898 | ||
eb4e17a1 YH |
1899 | static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, |
1900 | struct netlink_callback *cb, int *s_ip_idx, | |
1901 | struct inet_fill_args *fillargs) | |
1902 | { | |
1903 | switch (fillargs->event) { | |
1904 | case RTM_NEWADDR: | |
1905 | return in_dev_dump_ifaddr(in_dev, skb, cb, s_ip_idx, fillargs); | |
1906 | case RTM_GETMULTICAST: | |
1907 | return in_dev_dump_ifmcaddr(in_dev, skb, cb, s_ip_idx, | |
1908 | fillargs); | |
1909 | default: | |
1910 | return -EINVAL; | |
1911 | } | |
1912 | } | |
1913 | ||
081a0e3b ED |
1914 | /* Combine dev_addr_genid and dev_base_seq to detect changes. |
1915 | */ | |
1916 | static u32 inet_base_seq(const struct net *net) | |
1917 | { | |
1918 | u32 res = atomic_read(&net->ipv4.dev_addr_genid) + | |
590e92cd | 1919 | READ_ONCE(net->dev_base_seq); |
081a0e3b ED |
1920 | |
1921 | /* Must not return 0 (see nl_dump_check_consistent()). | |
1922 | * Chose a value far away from 0. | |
1923 | */ | |
1924 | if (!res) | |
1925 | res = 0x80000000; | |
1926 | return res; | |
1927 | } | |
1928 | ||
eb4e17a1 YH |
1929 | static int inet_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, |
1930 | int event) | |
1da177e4 | 1931 | { |
c33078e3 | 1932 | const struct nlmsghdr *nlh = cb->nlh; |
978a46fa CB |
1933 | struct inet_fill_args fillargs = { |
1934 | .portid = NETLINK_CB(cb->skb).portid, | |
c33078e3 | 1935 | .seq = nlh->nlmsg_seq, |
eb4e17a1 | 1936 | .event = event, |
978a46fa CB |
1937 | .flags = NLM_F_MULTI, |
1938 | .netnsid = -1, | |
1939 | }; | |
3b1e0a65 | 1940 | struct net *net = sock_net(skb->sk); |
d3807145 | 1941 | struct net *tgt_net = net; |
cdb2f80f ED |
1942 | struct { |
1943 | unsigned long ifindex; | |
1944 | int ip_idx; | |
1945 | } *ctx = (void *)cb->ctx; | |
1da177e4 | 1946 | struct in_device *in_dev; |
cdb2f80f | 1947 | struct net_device *dev; |
d7e38611 | 1948 | int err = 0; |
1da177e4 | 1949 | |
cdb2f80f | 1950 | rcu_read_lock(); |
c33078e3 | 1951 | if (cb->strict_check) { |
c33078e3 | 1952 | err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, |
5fcd266a | 1953 | skb->sk, cb); |
c33078e3 | 1954 | if (err < 0) |
cdb2f80f | 1955 | goto done; |
5fcd266a DA |
1956 | |
1957 | if (fillargs.ifindex) { | |
cdb2f80f | 1958 | dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex); |
7b05ab85 IS |
1959 | if (!dev) { |
1960 | err = -ENODEV; | |
cdb2f80f | 1961 | goto done; |
7b05ab85 | 1962 | } |
eec4df98 ED |
1963 | in_dev = __in_dev_get_rcu(dev); |
1964 | if (!in_dev) | |
1c98eca4 | 1965 | goto done; |
cdb2f80f ED |
1966 | err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, |
1967 | &fillargs); | |
1968 | goto done; | |
eec4df98 | 1969 | } |
1da177e4 LT |
1970 | } |
1971 | ||
cdb2f80f ED |
1972 | cb->seq = inet_base_seq(tgt_net); |
1973 | ||
b8c8abef | 1974 | for_each_netdev_dump(tgt_net, dev, ctx->ifindex) { |
cdb2f80f ED |
1975 | in_dev = __in_dev_get_rcu(dev); |
1976 | if (!in_dev) | |
1977 | continue; | |
1978 | err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, | |
1979 | &fillargs); | |
1980 | if (err < 0) | |
1981 | goto done; | |
1982 | } | |
1da177e4 | 1983 | done: |
978a46fa | 1984 | if (fillargs.netnsid >= 0) |
d3807145 | 1985 | put_net(tgt_net); |
cdb2f80f ED |
1986 | rcu_read_unlock(); |
1987 | return err; | |
1da177e4 LT |
1988 | } |
1989 | ||
eb4e17a1 YH |
1990 | static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
1991 | { | |
1992 | return inet_dump_addr(skb, cb, RTM_NEWADDR); | |
1993 | } | |
1994 | ||
1995 | static int inet_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) | |
1996 | { | |
1997 | return inet_dump_addr(skb, cb, RTM_GETMULTICAST); | |
1998 | } | |
1999 | ||
539afedf | 2000 | static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
15e47304 | 2001 | u32 portid) |
1da177e4 | 2002 | { |
978a46fa CB |
2003 | struct inet_fill_args fillargs = { |
2004 | .portid = portid, | |
2005 | .seq = nlh ? nlh->nlmsg_seq : 0, | |
2006 | .event = event, | |
2007 | .flags = 0, | |
2008 | .netnsid = -1, | |
2009 | }; | |
47f68512 | 2010 | struct sk_buff *skb; |
d6062cbb | 2011 | int err = -ENOBUFS; |
4b8aa9ab | 2012 | struct net *net; |
1da177e4 | 2013 | |
c346dca1 | 2014 | net = dev_net(ifa->ifa_dev->dev); |
339bf98f | 2015 | skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); |
51456b29 | 2016 | if (!skb) |
d6062cbb TG |
2017 | goto errout; |
2018 | ||
978a46fa | 2019 | err = inet_fill_ifaddr(skb, ifa, &fillargs); |
26932566 PM |
2020 | if (err < 0) { |
2021 | /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ | |
2022 | WARN_ON(err == -EMSGSIZE); | |
2023 | kfree_skb(skb); | |
2024 | goto errout; | |
2025 | } | |
15e47304 | 2026 | rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); |
1ce85fe4 | 2027 | return; |
d6062cbb | 2028 | errout: |
4c180887 | 2029 | rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err); |
1da177e4 LT |
2030 | } |
2031 | ||
b1974ed0 AR |
2032 | static size_t inet_get_link_af_size(const struct net_device *dev, |
2033 | u32 ext_filter_mask) | |
9f0f7272 | 2034 | { |
1fc19aff | 2035 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
9f0f7272 TG |
2036 | |
2037 | if (!in_dev) | |
2038 | return 0; | |
2039 | ||
2040 | return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */ | |
2041 | } | |
2042 | ||
d5566fd7 SV |
2043 | static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev, |
2044 | u32 ext_filter_mask) | |
9f0f7272 | 2045 | { |
1fc19aff | 2046 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
9f0f7272 TG |
2047 | struct nlattr *nla; |
2048 | int i; | |
2049 | ||
2050 | if (!in_dev) | |
2051 | return -ENODATA; | |
2052 | ||
2053 | nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4); | |
51456b29 | 2054 | if (!nla) |
9f0f7272 TG |
2055 | return -EMSGSIZE; |
2056 | ||
2057 | for (i = 0; i < IPV4_DEVCONF_MAX; i++) | |
0598f8f3 | 2058 | ((u32 *) nla_data(nla))[i] = READ_ONCE(in_dev->cnf.data[i]); |
9f0f7272 TG |
2059 | |
2060 | return 0; | |
2061 | } | |
2062 | ||
2063 | static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = { | |
2064 | [IFLA_INET_CONF] = { .type = NLA_NESTED }, | |
2065 | }; | |
2066 | ||
cf7afbfe | 2067 | static int inet_validate_link_af(const struct net_device *dev, |
8679c31e RY |
2068 | const struct nlattr *nla, |
2069 | struct netlink_ext_ack *extack) | |
9f0f7272 | 2070 | { |
9f0f7272 TG |
2071 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
2072 | int err, rem; | |
2073 | ||
a100243d | 2074 | if (dev && !__in_dev_get_rtnl(dev)) |
cf7afbfe | 2075 | return -EAFNOSUPPORT; |
9f0f7272 | 2076 | |
8cb08174 | 2077 | err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, |
8679c31e | 2078 | inet_af_policy, extack); |
9f0f7272 TG |
2079 | if (err < 0) |
2080 | return err; | |
2081 | ||
2082 | if (tb[IFLA_INET_CONF]) { | |
2083 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) { | |
2084 | int cfgid = nla_type(a); | |
2085 | ||
2086 | if (nla_len(a) < 4) | |
2087 | return -EINVAL; | |
2088 | ||
2089 | if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX) | |
2090 | return -EINVAL; | |
2091 | } | |
2092 | } | |
2093 | ||
cf7afbfe TG |
2094 | return 0; |
2095 | } | |
2096 | ||
3583a4e8 SH |
2097 | static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, |
2098 | struct netlink_ext_ack *extack) | |
cf7afbfe | 2099 | { |
a100243d | 2100 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
cf7afbfe TG |
2101 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
2102 | int rem; | |
2103 | ||
2104 | if (!in_dev) | |
2105 | return -EAFNOSUPPORT; | |
2106 | ||
8cb08174 | 2107 | if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0) |
5ac6b198 | 2108 | return -EINVAL; |
cf7afbfe | 2109 | |
9f0f7272 TG |
2110 | if (tb[IFLA_INET_CONF]) { |
2111 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) | |
2112 | ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a)); | |
2113 | } | |
2114 | ||
2115 | return 0; | |
2116 | } | |
2117 | ||
edc9e748 ND |
2118 | static int inet_netconf_msgsize_devconf(int type) |
2119 | { | |
2120 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) | |
2121 | + nla_total_size(4); /* NETCONFA_IFINDEX */ | |
136ba622 | 2122 | bool all = false; |
edc9e748 | 2123 | |
136ba622 ZS |
2124 | if (type == NETCONFA_ALL) |
2125 | all = true; | |
2126 | ||
2127 | if (all || type == NETCONFA_FORWARDING) | |
edc9e748 | 2128 | size += nla_total_size(4); |
136ba622 | 2129 | if (all || type == NETCONFA_RP_FILTER) |
cc535dfb | 2130 | size += nla_total_size(4); |
136ba622 | 2131 | if (all || type == NETCONFA_MC_FORWARDING) |
d67b8c61 | 2132 | size += nla_total_size(4); |
5cbf777c XL |
2133 | if (all || type == NETCONFA_BC_FORWARDING) |
2134 | size += nla_total_size(4); | |
136ba622 | 2135 | if (all || type == NETCONFA_PROXY_NEIGH) |
f085ff1c | 2136 | size += nla_total_size(4); |
136ba622 | 2137 | if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) |
974d7af5 | 2138 | size += nla_total_size(4); |
edc9e748 ND |
2139 | |
2140 | return size; | |
2141 | } | |
2142 | ||
2143 | static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex, | |
0598f8f3 ED |
2144 | const struct ipv4_devconf *devconf, |
2145 | u32 portid, u32 seq, int event, | |
2146 | unsigned int flags, int type) | |
edc9e748 ND |
2147 | { |
2148 | struct nlmsghdr *nlh; | |
2149 | struct netconfmsg *ncm; | |
136ba622 | 2150 | bool all = false; |
edc9e748 ND |
2151 | |
2152 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), | |
2153 | flags); | |
51456b29 | 2154 | if (!nlh) |
edc9e748 ND |
2155 | return -EMSGSIZE; |
2156 | ||
136ba622 ZS |
2157 | if (type == NETCONFA_ALL) |
2158 | all = true; | |
2159 | ||
edc9e748 ND |
2160 | ncm = nlmsg_data(nlh); |
2161 | ncm->ncm_family = AF_INET; | |
2162 | ||
2163 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) | |
2164 | goto nla_put_failure; | |
2165 | ||
b5c9641d DA |
2166 | if (!devconf) |
2167 | goto out; | |
2168 | ||
136ba622 | 2169 | if ((all || type == NETCONFA_FORWARDING) && |
edc9e748 | 2170 | nla_put_s32(skb, NETCONFA_FORWARDING, |
0598f8f3 | 2171 | IPV4_DEVCONF_RO(*devconf, FORWARDING)) < 0) |
edc9e748 | 2172 | goto nla_put_failure; |
136ba622 | 2173 | if ((all || type == NETCONFA_RP_FILTER) && |
cc535dfb | 2174 | nla_put_s32(skb, NETCONFA_RP_FILTER, |
0598f8f3 | 2175 | IPV4_DEVCONF_RO(*devconf, RP_FILTER)) < 0) |
cc535dfb | 2176 | goto nla_put_failure; |
136ba622 | 2177 | if ((all || type == NETCONFA_MC_FORWARDING) && |
d67b8c61 | 2178 | nla_put_s32(skb, NETCONFA_MC_FORWARDING, |
0598f8f3 | 2179 | IPV4_DEVCONF_RO(*devconf, MC_FORWARDING)) < 0) |
d67b8c61 | 2180 | goto nla_put_failure; |
5cbf777c XL |
2181 | if ((all || type == NETCONFA_BC_FORWARDING) && |
2182 | nla_put_s32(skb, NETCONFA_BC_FORWARDING, | |
0598f8f3 | 2183 | IPV4_DEVCONF_RO(*devconf, BC_FORWARDING)) < 0) |
5cbf777c | 2184 | goto nla_put_failure; |
136ba622 | 2185 | if ((all || type == NETCONFA_PROXY_NEIGH) && |
09aea5df | 2186 | nla_put_s32(skb, NETCONFA_PROXY_NEIGH, |
0598f8f3 | 2187 | IPV4_DEVCONF_RO(*devconf, PROXY_ARP)) < 0) |
f085ff1c | 2188 | goto nla_put_failure; |
136ba622 | 2189 | if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && |
974d7af5 | 2190 | nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
0598f8f3 ED |
2191 | IPV4_DEVCONF_RO(*devconf, |
2192 | IGNORE_ROUTES_WITH_LINKDOWN)) < 0) | |
974d7af5 | 2193 | goto nla_put_failure; |
edc9e748 | 2194 | |
b5c9641d | 2195 | out: |
053c095a JB |
2196 | nlmsg_end(skb, nlh); |
2197 | return 0; | |
edc9e748 ND |
2198 | |
2199 | nla_put_failure: | |
2200 | nlmsg_cancel(skb, nlh); | |
2201 | return -EMSGSIZE; | |
2202 | } | |
2203 | ||
3b022865 DA |
2204 | void inet_netconf_notify_devconf(struct net *net, int event, int type, |
2205 | int ifindex, struct ipv4_devconf *devconf) | |
edc9e748 ND |
2206 | { |
2207 | struct sk_buff *skb; | |
2208 | int err = -ENOBUFS; | |
2209 | ||
fa17806c | 2210 | skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL); |
51456b29 | 2211 | if (!skb) |
edc9e748 ND |
2212 | goto errout; |
2213 | ||
2214 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, | |
3b022865 | 2215 | event, 0, type); |
edc9e748 ND |
2216 | if (err < 0) { |
2217 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ | |
2218 | WARN_ON(err == -EMSGSIZE); | |
2219 | kfree_skb(skb); | |
2220 | goto errout; | |
2221 | } | |
fa17806c | 2222 | rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL); |
edc9e748 ND |
2223 | return; |
2224 | errout: | |
4c180887 | 2225 | rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err); |
edc9e748 ND |
2226 | } |
2227 | ||
9e551110 ND |
2228 | static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = { |
2229 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, | |
2230 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, | |
cc535dfb | 2231 | [NETCONFA_RP_FILTER] = { .len = sizeof(int) }, |
09aea5df | 2232 | [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, |
974d7af5 | 2233 | [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, |
9e551110 ND |
2234 | }; |
2235 | ||
eede370d JK |
2236 | static int inet_netconf_valid_get_req(struct sk_buff *skb, |
2237 | const struct nlmsghdr *nlh, | |
2238 | struct nlattr **tb, | |
2239 | struct netlink_ext_ack *extack) | |
2240 | { | |
2241 | int i, err; | |
2242 | ||
2243 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { | |
2244 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request"); | |
2245 | return -EINVAL; | |
2246 | } | |
2247 | ||
2248 | if (!netlink_strict_get_check(skb)) | |
8cb08174 JB |
2249 | return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), |
2250 | tb, NETCONFA_MAX, | |
2251 | devconf_ipv4_policy, extack); | |
eede370d | 2252 | |
8cb08174 JB |
2253 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), |
2254 | tb, NETCONFA_MAX, | |
2255 | devconf_ipv4_policy, extack); | |
eede370d JK |
2256 | if (err) |
2257 | return err; | |
2258 | ||
2259 | for (i = 0; i <= NETCONFA_MAX; i++) { | |
2260 | if (!tb[i]) | |
2261 | continue; | |
2262 | ||
2263 | switch (i) { | |
2264 | case NETCONFA_IFINDEX: | |
2265 | break; | |
2266 | default: | |
2267 | NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request"); | |
2268 | return -EINVAL; | |
2269 | } | |
2270 | } | |
2271 | ||
2272 | return 0; | |
2273 | } | |
2274 | ||
9e551110 | 2275 | static int inet_netconf_get_devconf(struct sk_buff *in_skb, |
c21ef3e3 DA |
2276 | struct nlmsghdr *nlh, |
2277 | struct netlink_ext_ack *extack) | |
9e551110 ND |
2278 | { |
2279 | struct net *net = sock_net(in_skb->sk); | |
bbcf9105 ED |
2280 | struct nlattr *tb[NETCONFA_MAX + 1]; |
2281 | const struct ipv4_devconf *devconf; | |
2282 | struct in_device *in_dev = NULL; | |
2283 | struct net_device *dev = NULL; | |
9e551110 | 2284 | struct sk_buff *skb; |
9e551110 ND |
2285 | int ifindex; |
2286 | int err; | |
2287 | ||
eede370d JK |
2288 | err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack); |
2289 | if (err) | |
bbcf9105 | 2290 | return err; |
9e551110 | 2291 | |
9e551110 | 2292 | if (!tb[NETCONFA_IFINDEX]) |
bbcf9105 | 2293 | return -EINVAL; |
9e551110 ND |
2294 | |
2295 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); | |
2296 | switch (ifindex) { | |
2297 | case NETCONFA_IFINDEX_ALL: | |
2298 | devconf = net->ipv4.devconf_all; | |
2299 | break; | |
2300 | case NETCONFA_IFINDEX_DEFAULT: | |
2301 | devconf = net->ipv4.devconf_dflt; | |
2302 | break; | |
2303 | default: | |
bbcf9105 ED |
2304 | err = -ENODEV; |
2305 | dev = dev_get_by_index(net, ifindex); | |
2306 | if (dev) | |
2307 | in_dev = in_dev_get(dev); | |
51456b29 | 2308 | if (!in_dev) |
9e551110 ND |
2309 | goto errout; |
2310 | devconf = &in_dev->cnf; | |
2311 | break; | |
2312 | } | |
2313 | ||
2314 | err = -ENOBUFS; | |
fa17806c | 2315 | skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); |
51456b29 | 2316 | if (!skb) |
9e551110 ND |
2317 | goto errout; |
2318 | ||
2319 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, | |
2320 | NETLINK_CB(in_skb).portid, | |
2321 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, | |
136ba622 | 2322 | NETCONFA_ALL); |
9e551110 ND |
2323 | if (err < 0) { |
2324 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ | |
2325 | WARN_ON(err == -EMSGSIZE); | |
2326 | kfree_skb(skb); | |
2327 | goto errout; | |
2328 | } | |
2329 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); | |
2330 | errout: | |
bbcf9105 ED |
2331 | if (in_dev) |
2332 | in_dev_put(in_dev); | |
2333 | dev_put(dev); | |
9e551110 ND |
2334 | return err; |
2335 | } | |
2336 | ||
7a674200 ND |
2337 | static int inet_netconf_dump_devconf(struct sk_buff *skb, |
2338 | struct netlink_callback *cb) | |
2339 | { | |
addd383f | 2340 | const struct nlmsghdr *nlh = cb->nlh; |
7a674200 | 2341 | struct net *net = sock_net(skb->sk); |
16748707 ED |
2342 | struct { |
2343 | unsigned long ifindex; | |
2344 | unsigned int all_default; | |
2345 | } *ctx = (void *)cb->ctx; | |
2346 | const struct in_device *in_dev; | |
7a674200 | 2347 | struct net_device *dev; |
16748707 | 2348 | int err = 0; |
7a674200 | 2349 | |
addd383f DA |
2350 | if (cb->strict_check) { |
2351 | struct netlink_ext_ack *extack = cb->extack; | |
2352 | struct netconfmsg *ncm; | |
2353 | ||
2354 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { | |
2355 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request"); | |
2356 | return -EINVAL; | |
2357 | } | |
2358 | ||
2359 | if (nlmsg_attrlen(nlh, sizeof(*ncm))) { | |
2360 | NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request"); | |
2361 | return -EINVAL; | |
2362 | } | |
2363 | } | |
2364 | ||
16748707 ED |
2365 | rcu_read_lock(); |
2366 | for_each_netdev_dump(net, dev, ctx->ifindex) { | |
2367 | in_dev = __in_dev_get_rcu(dev); | |
2368 | if (!in_dev) | |
2369 | continue; | |
2370 | err = inet_netconf_fill_devconf(skb, dev->ifindex, | |
2371 | &in_dev->cnf, | |
2372 | NETLINK_CB(cb->skb).portid, | |
2373 | nlh->nlmsg_seq, | |
2374 | RTM_NEWNETCONF, NLM_F_MULTI, | |
2375 | NETCONFA_ALL); | |
2376 | if (err < 0) | |
2377 | goto done; | |
7a674200 | 2378 | } |
16748707 ED |
2379 | if (ctx->all_default == 0) { |
2380 | err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, | |
2381 | net->ipv4.devconf_all, | |
2382 | NETLINK_CB(cb->skb).portid, | |
2383 | nlh->nlmsg_seq, | |
2384 | RTM_NEWNETCONF, NLM_F_MULTI, | |
2385 | NETCONFA_ALL); | |
2386 | if (err < 0) | |
7a674200 | 2387 | goto done; |
16748707 ED |
2388 | ctx->all_default++; |
2389 | } | |
2390 | if (ctx->all_default == 1) { | |
2391 | err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, | |
2392 | net->ipv4.devconf_dflt, | |
2393 | NETLINK_CB(cb->skb).portid, | |
2394 | nlh->nlmsg_seq, | |
2395 | RTM_NEWNETCONF, NLM_F_MULTI, | |
2396 | NETCONFA_ALL); | |
2397 | if (err < 0) | |
7a674200 | 2398 | goto done; |
16748707 | 2399 | ctx->all_default++; |
7a674200 ND |
2400 | } |
2401 | done: | |
16748707 ED |
2402 | rcu_read_unlock(); |
2403 | return err; | |
7a674200 ND |
2404 | } |
2405 | ||
1da177e4 LT |
2406 | #ifdef CONFIG_SYSCTL |
2407 | ||
c0ce9fb3 | 2408 | static void devinet_copy_dflt_conf(struct net *net, int i) |
31be3085 HX |
2409 | { |
2410 | struct net_device *dev; | |
2411 | ||
c6d14c84 ED |
2412 | rcu_read_lock(); |
2413 | for_each_netdev_rcu(net, dev) { | |
31be3085 | 2414 | struct in_device *in_dev; |
c6d14c84 | 2415 | |
31be3085 HX |
2416 | in_dev = __in_dev_get_rcu(dev); |
2417 | if (in_dev && !test_bit(i, in_dev->cnf.state)) | |
9355bbd6 | 2418 | in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; |
31be3085 | 2419 | } |
c6d14c84 | 2420 | rcu_read_unlock(); |
31be3085 HX |
2421 | } |
2422 | ||
c6d14c84 | 2423 | /* called with RTNL locked */ |
c0ce9fb3 | 2424 | static void inet_forward_change(struct net *net) |
68dd299b PE |
2425 | { |
2426 | struct net_device *dev; | |
586f1211 | 2427 | int on = IPV4_DEVCONF_ALL(net, FORWARDING); |
68dd299b | 2428 | |
586f1211 | 2429 | IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; |
9355bbd6 | 2430 | IPV4_DEVCONF_DFLT(net, FORWARDING) = on; |
3b022865 DA |
2431 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2432 | NETCONFA_FORWARDING, | |
edc9e748 ND |
2433 | NETCONFA_IFINDEX_ALL, |
2434 | net->ipv4.devconf_all); | |
3b022865 DA |
2435 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2436 | NETCONFA_FORWARDING, | |
edc9e748 ND |
2437 | NETCONFA_IFINDEX_DEFAULT, |
2438 | net->ipv4.devconf_dflt); | |
68dd299b | 2439 | |
c0ce9fb3 | 2440 | for_each_netdev(net, dev) { |
68dd299b | 2441 | struct in_device *in_dev; |
fa17806c | 2442 | |
0187bdfb BH |
2443 | if (on) |
2444 | dev_disable_lro(dev); | |
fa17806c | 2445 | |
77453d42 | 2446 | in_dev = __in_dev_get_rtnl_net(dev); |
edc9e748 | 2447 | if (in_dev) { |
68dd299b | 2448 | IN_DEV_CONF_SET(in_dev, FORWARDING, on); |
3b022865 DA |
2449 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2450 | NETCONFA_FORWARDING, | |
edc9e748 ND |
2451 | dev->ifindex, &in_dev->cnf); |
2452 | } | |
68dd299b | 2453 | } |
68dd299b PE |
2454 | } |
2455 | ||
f085ff1c | 2456 | static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf) |
2457 | { | |
2458 | if (cnf == net->ipv4.devconf_dflt) | |
2459 | return NETCONFA_IFINDEX_DEFAULT; | |
2460 | else if (cnf == net->ipv4.devconf_all) | |
2461 | return NETCONFA_IFINDEX_ALL; | |
2462 | else { | |
2463 | struct in_device *idev | |
2464 | = container_of(cnf, struct in_device, cnf); | |
2465 | return idev->dev->ifindex; | |
2466 | } | |
2467 | } | |
2468 | ||
78eb4ea2 | 2469 | static int devinet_conf_proc(const struct ctl_table *ctl, int write, |
32927393 | 2470 | void *buffer, size_t *lenp, loff_t *ppos) |
31be3085 | 2471 | { |
d01ff0a0 | 2472 | int old_value = *(int *)ctl->data; |
8d65af78 | 2473 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
d01ff0a0 | 2474 | int new_value = *(int *)ctl->data; |
31be3085 HX |
2475 | |
2476 | if (write) { | |
2477 | struct ipv4_devconf *cnf = ctl->extra1; | |
c0ce9fb3 | 2478 | struct net *net = ctl->extra2; |
31be3085 | 2479 | int i = (int *)ctl->data - cnf->data; |
f085ff1c | 2480 | int ifindex; |
31be3085 HX |
2481 | |
2482 | set_bit(i, cnf->state); | |
2483 | ||
9355bbd6 | 2484 | if (cnf == net->ipv4.devconf_dflt) |
c0ce9fb3 | 2485 | devinet_copy_dflt_conf(net, i); |
d0daebc3 TG |
2486 | if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 || |
2487 | i == IPV4_DEVCONF_ROUTE_LOCALNET - 1) | |
d01ff0a0 | 2488 | if ((new_value == 0) && (old_value != 0)) |
4ccfe6d4 | 2489 | rt_cache_flush(net); |
f085ff1c | 2490 | |
5cbf777c XL |
2491 | if (i == IPV4_DEVCONF_BC_FORWARDING - 1 && |
2492 | new_value != old_value) | |
2493 | rt_cache_flush(net); | |
2494 | ||
cc535dfb ND |
2495 | if (i == IPV4_DEVCONF_RP_FILTER - 1 && |
2496 | new_value != old_value) { | |
f085ff1c | 2497 | ifindex = devinet_conf_ifindex(net, cnf); |
3b022865 DA |
2498 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2499 | NETCONFA_RP_FILTER, | |
cc535dfb ND |
2500 | ifindex, cnf); |
2501 | } | |
f085ff1c | 2502 | if (i == IPV4_DEVCONF_PROXY_ARP - 1 && |
2503 | new_value != old_value) { | |
2504 | ifindex = devinet_conf_ifindex(net, cnf); | |
3b022865 DA |
2505 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2506 | NETCONFA_PROXY_NEIGH, | |
f085ff1c | 2507 | ifindex, cnf); |
2508 | } | |
974d7af5 AG |
2509 | if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 && |
2510 | new_value != old_value) { | |
2511 | ifindex = devinet_conf_ifindex(net, cnf); | |
3b022865 DA |
2512 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2513 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, | |
974d7af5 AG |
2514 | ifindex, cnf); |
2515 | } | |
31be3085 HX |
2516 | } |
2517 | ||
2518 | return ret; | |
2519 | } | |
2520 | ||
78eb4ea2 | 2521 | static int devinet_sysctl_forward(const struct ctl_table *ctl, int write, |
32927393 | 2522 | void *buffer, size_t *lenp, loff_t *ppos) |
1da177e4 LT |
2523 | { |
2524 | int *valp = ctl->data; | |
2525 | int val = *valp; | |
88af182e | 2526 | loff_t pos = *ppos; |
8292d7f6 YY |
2527 | struct net *net = ctl->extra2; |
2528 | int ret; | |
1da177e4 | 2529 | |
8292d7f6 YY |
2530 | if (write && !ns_capable(net->user_ns, CAP_NET_ADMIN)) |
2531 | return -EPERM; | |
2532 | ||
2533 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); | |
c0ce9fb3 | 2534 | |
8292d7f6 | 2535 | if (write && *valp != val) { |
0187bdfb | 2536 | if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) { |
77453d42 | 2537 | if (!rtnl_net_trylock(net)) { |
88af182e EB |
2538 | /* Restore the original values before restarting */ |
2539 | *valp = val; | |
2540 | *ppos = pos; | |
9b8adb5e | 2541 | return restart_syscall(); |
88af182e | 2542 | } |
0187bdfb BH |
2543 | if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) { |
2544 | inet_forward_change(net); | |
edc9e748 | 2545 | } else { |
0187bdfb BH |
2546 | struct ipv4_devconf *cnf = ctl->extra1; |
2547 | struct in_device *idev = | |
2548 | container_of(cnf, struct in_device, cnf); | |
edc9e748 ND |
2549 | if (*valp) |
2550 | dev_disable_lro(idev->dev); | |
3b022865 | 2551 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
edc9e748 ND |
2552 | NETCONFA_FORWARDING, |
2553 | idev->dev->ifindex, | |
2554 | cnf); | |
0187bdfb | 2555 | } |
77453d42 | 2556 | rtnl_net_unlock(net); |
4ccfe6d4 | 2557 | rt_cache_flush(net); |
edc9e748 | 2558 | } else |
3b022865 DA |
2559 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
2560 | NETCONFA_FORWARDING, | |
edc9e748 ND |
2561 | NETCONFA_IFINDEX_DEFAULT, |
2562 | net->ipv4.devconf_dflt); | |
1da177e4 LT |
2563 | } |
2564 | ||
2565 | return ret; | |
2566 | } | |
2567 | ||
78eb4ea2 | 2568 | static int ipv4_doint_and_flush(const struct ctl_table *ctl, int write, |
32927393 | 2569 | void *buffer, size_t *lenp, loff_t *ppos) |
1da177e4 LT |
2570 | { |
2571 | int *valp = ctl->data; | |
2572 | int val = *valp; | |
8d65af78 | 2573 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
76e6ebfb | 2574 | struct net *net = ctl->extra2; |
1da177e4 LT |
2575 | |
2576 | if (write && *valp != val) | |
4ccfe6d4 | 2577 | rt_cache_flush(net); |
1da177e4 LT |
2578 | |
2579 | return ret; | |
2580 | } | |
2581 | ||
f8572d8f | 2582 | #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \ |
42f811b8 | 2583 | { \ |
42f811b8 HX |
2584 | .procname = name, \ |
2585 | .data = ipv4_devconf.data + \ | |
02291680 | 2586 | IPV4_DEVCONF_ ## attr - 1, \ |
42f811b8 HX |
2587 | .maxlen = sizeof(int), \ |
2588 | .mode = mval, \ | |
2589 | .proc_handler = proc, \ | |
31be3085 | 2590 | .extra1 = &ipv4_devconf, \ |
42f811b8 HX |
2591 | } |
2592 | ||
2593 | #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ | |
f8572d8f | 2594 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc) |
42f811b8 HX |
2595 | |
2596 | #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ | |
f8572d8f | 2597 | DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc) |
42f811b8 | 2598 | |
f8572d8f EB |
2599 | #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \ |
2600 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc) | |
42f811b8 HX |
2601 | |
2602 | #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ | |
f8572d8f | 2603 | DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush) |
42f811b8 | 2604 | |
1da177e4 LT |
2605 | static struct devinet_sysctl_table { |
2606 | struct ctl_table_header *sysctl_header; | |
1c106eb0 | 2607 | struct ctl_table devinet_vars[IPV4_DEVCONF_MAX]; |
1da177e4 LT |
2608 | } devinet_sysctl = { |
2609 | .devinet_vars = { | |
42f811b8 | 2610 | DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", |
f8572d8f | 2611 | devinet_sysctl_forward), |
42f811b8 | 2612 | DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), |
5cbf777c | 2613 | DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"), |
42f811b8 HX |
2614 | |
2615 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), | |
2616 | DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), | |
2617 | DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), | |
2618 | DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), | |
2619 | DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), | |
2620 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, | |
2621 | "accept_source_route"), | |
8153a10c | 2622 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"), |
28f6aeea | 2623 | DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"), |
42f811b8 HX |
2624 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), |
2625 | DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), | |
2626 | DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), | |
2627 | DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), | |
2628 | DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), | |
2629 | DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), | |
2630 | DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), | |
2631 | DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), | |
2632 | DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), | |
eefef1cf | 2633 | DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"), |
fcdb44d0 JP |
2634 | DEVINET_SYSCTL_RW_ENTRY(ARP_EVICT_NOCARRIER, |
2635 | "arp_evict_nocarrier"), | |
65324144 | 2636 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"), |
5c6fe01c WM |
2637 | DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION, |
2638 | "force_igmp_version"), | |
2690048c WM |
2639 | DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL, |
2640 | "igmpv2_unsolicited_report_interval"), | |
2641 | DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL, | |
2642 | "igmpv3_unsolicited_report_interval"), | |
0eeb075f AG |
2643 | DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN, |
2644 | "ignore_routes_with_linkdown"), | |
97daf331 JB |
2645 | DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP, |
2646 | "drop_gratuitous_arp"), | |
42f811b8 HX |
2647 | |
2648 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), | |
2649 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), | |
42f811b8 HX |
2650 | DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, |
2651 | "promote_secondaries"), | |
d0daebc3 TG |
2652 | DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET, |
2653 | "route_localnet"), | |
12b74dfa JB |
2654 | DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST, |
2655 | "drop_unicast_in_l2_multicast"), | |
1da177e4 | 2656 | }, |
1da177e4 LT |
2657 | }; |
2658 | ||
ea40b324 | 2659 | static int __devinet_sysctl_register(struct net *net, char *dev_name, |
29c994e3 | 2660 | int ifindex, struct ipv4_devconf *p) |
1da177e4 LT |
2661 | { |
2662 | int i; | |
9fa89642 | 2663 | struct devinet_sysctl_table *t; |
8607ddb8 | 2664 | char path[sizeof("net/ipv4/conf/") + IFNAMSIZ]; |
bfada697 | 2665 | |
425b9c7f | 2666 | t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL_ACCOUNT); |
1da177e4 | 2667 | if (!t) |
9fa89642 PE |
2668 | goto out; |
2669 | ||
1c106eb0 | 2670 | for (i = 0; i < ARRAY_SIZE(t->devinet_vars); i++) { |
1da177e4 | 2671 | t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; |
31be3085 | 2672 | t->devinet_vars[i].extra1 = p; |
c0ce9fb3 | 2673 | t->devinet_vars[i].extra2 = net; |
1da177e4 LT |
2674 | } |
2675 | ||
8607ddb8 | 2676 | snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name); |
1da177e4 | 2677 | |
8607ddb8 | 2678 | t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars); |
1da177e4 | 2679 | if (!t->sysctl_header) |
8607ddb8 | 2680 | goto free; |
1da177e4 LT |
2681 | |
2682 | p->sysctl = t; | |
29c994e3 | 2683 | |
3b022865 DA |
2684 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, |
2685 | ifindex, p); | |
ea40b324 | 2686 | return 0; |
1da177e4 | 2687 | |
9fa89642 | 2688 | free: |
1da177e4 | 2689 | kfree(t); |
9fa89642 | 2690 | out: |
6def4801 | 2691 | return -ENOMEM; |
1da177e4 LT |
2692 | } |
2693 | ||
b5c9641d DA |
2694 | static void __devinet_sysctl_unregister(struct net *net, |
2695 | struct ipv4_devconf *cnf, int ifindex) | |
51602b2a PE |
2696 | { |
2697 | struct devinet_sysctl_table *t = cnf->sysctl; | |
2698 | ||
b5c9641d DA |
2699 | if (t) { |
2700 | cnf->sysctl = NULL; | |
2701 | unregister_net_sysctl_table(t->sysctl_header); | |
2702 | kfree(t); | |
2703 | } | |
51602b2a | 2704 | |
b5c9641d | 2705 | inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); |
51602b2a PE |
2706 | } |
2707 | ||
20e61da7 | 2708 | static int devinet_sysctl_register(struct in_device *idev) |
66f27a52 | 2709 | { |
20e61da7 WC |
2710 | int err; |
2711 | ||
2712 | if (!sysctl_dev_name_is_allowed(idev->dev->name)) | |
2713 | return -EINVAL; | |
2714 | ||
2715 | err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL); | |
2716 | if (err) | |
2717 | return err; | |
2718 | err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name, | |
29c994e3 | 2719 | idev->dev->ifindex, &idev->cnf); |
20e61da7 WC |
2720 | if (err) |
2721 | neigh_sysctl_unregister(idev->arp_parms); | |
2722 | return err; | |
66f27a52 PE |
2723 | } |
2724 | ||
51602b2a | 2725 | static void devinet_sysctl_unregister(struct in_device *idev) |
1da177e4 | 2726 | { |
b5c9641d DA |
2727 | struct net *net = dev_net(idev->dev); |
2728 | ||
2729 | __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex); | |
51602b2a | 2730 | neigh_sysctl_unregister(idev->arp_parms); |
1da177e4 | 2731 | } |
1da177e4 | 2732 | |
68dd299b PE |
2733 | static struct ctl_table ctl_forward_entry[] = { |
2734 | { | |
68dd299b PE |
2735 | .procname = "ip_forward", |
2736 | .data = &ipv4_devconf.data[ | |
02291680 | 2737 | IPV4_DEVCONF_FORWARDING - 1], |
68dd299b PE |
2738 | .maxlen = sizeof(int), |
2739 | .mode = 0644, | |
2740 | .proc_handler = devinet_sysctl_forward, | |
68dd299b | 2741 | .extra1 = &ipv4_devconf, |
c0ce9fb3 | 2742 | .extra2 = &init_net, |
68dd299b | 2743 | }, |
68dd299b | 2744 | }; |
2a75de0c | 2745 | #endif |
68dd299b | 2746 | |
752d14dc PE |
2747 | static __net_init int devinet_init_net(struct net *net) |
2748 | { | |
2a75de0c | 2749 | #ifdef CONFIG_SYSCTL |
752d14dc | 2750 | struct ctl_table_header *forw_hdr; |
87173021 | 2751 | struct ctl_table *tbl; |
2a75de0c | 2752 | #endif |
87173021 KI |
2753 | struct ipv4_devconf *all, *dflt; |
2754 | int err; | |
2755 | int i; | |
752d14dc PE |
2756 | |
2757 | err = -ENOMEM; | |
87173021 KI |
2758 | net->ipv4.inet_addr_lst = kmalloc_array(IN4_ADDR_HSIZE, |
2759 | sizeof(struct hlist_head), | |
2760 | GFP_KERNEL); | |
2761 | if (!net->ipv4.inet_addr_lst) | |
2762 | goto err_alloc_hash; | |
2763 | ||
856c395c CW |
2764 | all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL); |
2765 | if (!all) | |
2766 | goto err_alloc_all; | |
752d14dc | 2767 | |
856c395c CW |
2768 | dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); |
2769 | if (!dflt) | |
2770 | goto err_alloc_dflt; | |
752d14dc | 2771 | |
2a75de0c | 2772 | #ifdef CONFIG_SYSCTL |
856c395c CW |
2773 | tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL); |
2774 | if (!tbl) | |
2775 | goto err_alloc_ctl; | |
752d14dc | 2776 | |
856c395c CW |
2777 | tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1]; |
2778 | tbl[0].extra1 = all; | |
2779 | tbl[0].extra2 = net; | |
2a75de0c | 2780 | #endif |
856c395c | 2781 | |
9efd6a3c | 2782 | if (!net_eq(net, &init_net)) { |
a5612ca1 KI |
2783 | switch (net_inherit_devconf()) { |
2784 | case 3: | |
9efd6a3c ND |
2785 | /* copy from the current netns */ |
2786 | memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all, | |
2787 | sizeof(ipv4_devconf)); | |
2788 | memcpy(dflt, | |
2789 | current->nsproxy->net_ns->ipv4.devconf_dflt, | |
2790 | sizeof(ipv4_devconf_dflt)); | |
a5612ca1 KI |
2791 | break; |
2792 | case 0: | |
2793 | case 1: | |
2794 | /* copy from init_net */ | |
9efd6a3c ND |
2795 | memcpy(all, init_net.ipv4.devconf_all, |
2796 | sizeof(ipv4_devconf)); | |
2797 | memcpy(dflt, init_net.ipv4.devconf_dflt, | |
2798 | sizeof(ipv4_devconf_dflt)); | |
a5612ca1 KI |
2799 | break; |
2800 | case 2: | |
2801 | /* use compiled values */ | |
2802 | break; | |
9efd6a3c | 2803 | } |
752d14dc PE |
2804 | } |
2805 | ||
2806 | #ifdef CONFIG_SYSCTL | |
29c994e3 | 2807 | err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all); |
752d14dc PE |
2808 | if (err < 0) |
2809 | goto err_reg_all; | |
2810 | ||
29c994e3 ND |
2811 | err = __devinet_sysctl_register(net, "default", |
2812 | NETCONFA_IFINDEX_DEFAULT, dflt); | |
752d14dc PE |
2813 | if (err < 0) |
2814 | goto err_reg_dflt; | |
2815 | ||
2816 | err = -ENOMEM; | |
c899710f JG |
2817 | forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl, |
2818 | ARRAY_SIZE(ctl_forward_entry)); | |
51456b29 | 2819 | if (!forw_hdr) |
752d14dc | 2820 | goto err_reg_ctl; |
2a75de0c | 2821 | net->ipv4.forw_hdr = forw_hdr; |
752d14dc PE |
2822 | #endif |
2823 | ||
87173021 KI |
2824 | for (i = 0; i < IN4_ADDR_HSIZE; i++) |
2825 | INIT_HLIST_HEAD(&net->ipv4.inet_addr_lst[i]); | |
2826 | ||
1675f385 KI |
2827 | INIT_DEFERRABLE_WORK(&net->ipv4.addr_chk_work, check_lifetime); |
2828 | ||
752d14dc PE |
2829 | net->ipv4.devconf_all = all; |
2830 | net->ipv4.devconf_dflt = dflt; | |
2831 | return 0; | |
2832 | ||
2833 | #ifdef CONFIG_SYSCTL | |
2834 | err_reg_ctl: | |
b5c9641d | 2835 | __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT); |
752d14dc | 2836 | err_reg_dflt: |
b5c9641d | 2837 | __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); |
752d14dc | 2838 | err_reg_all: |
856c395c | 2839 | kfree(tbl); |
752d14dc | 2840 | err_alloc_ctl: |
2a75de0c | 2841 | #endif |
856c395c | 2842 | kfree(dflt); |
752d14dc | 2843 | err_alloc_dflt: |
856c395c | 2844 | kfree(all); |
752d14dc | 2845 | err_alloc_all: |
87173021 KI |
2846 | kfree(net->ipv4.inet_addr_lst); |
2847 | err_alloc_hash: | |
752d14dc PE |
2848 | return err; |
2849 | } | |
2850 | ||
2851 | static __net_exit void devinet_exit_net(struct net *net) | |
2852 | { | |
2a75de0c | 2853 | #ifdef CONFIG_SYSCTL |
bfa858f2 | 2854 | const struct ctl_table *tbl; |
1675f385 KI |
2855 | #endif |
2856 | ||
2857 | cancel_delayed_work_sync(&net->ipv4.addr_chk_work); | |
752d14dc | 2858 | |
1675f385 | 2859 | #ifdef CONFIG_SYSCTL |
752d14dc | 2860 | tbl = net->ipv4.forw_hdr->ctl_table_arg; |
752d14dc | 2861 | unregister_net_sysctl_table(net->ipv4.forw_hdr); |
b5c9641d DA |
2862 | __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt, |
2863 | NETCONFA_IFINDEX_DEFAULT); | |
2864 | __devinet_sysctl_unregister(net, net->ipv4.devconf_all, | |
2865 | NETCONFA_IFINDEX_ALL); | |
752d14dc | 2866 | kfree(tbl); |
2a75de0c | 2867 | #endif |
752d14dc PE |
2868 | kfree(net->ipv4.devconf_dflt); |
2869 | kfree(net->ipv4.devconf_all); | |
87173021 | 2870 | kfree(net->ipv4.inet_addr_lst); |
752d14dc PE |
2871 | } |
2872 | ||
2873 | static __net_initdata struct pernet_operations devinet_ops = { | |
2874 | .init = devinet_init_net, | |
2875 | .exit = devinet_exit_net, | |
2876 | }; | |
2877 | ||
207895fd | 2878 | static struct rtnl_af_ops inet_af_ops __read_mostly = { |
9f0f7272 TG |
2879 | .family = AF_INET, |
2880 | .fill_link_af = inet_fill_link_af, | |
2881 | .get_link_af_size = inet_get_link_af_size, | |
cf7afbfe TG |
2882 | .validate_link_af = inet_validate_link_af, |
2883 | .set_link_af = inet_set_link_af, | |
9f0f7272 TG |
2884 | }; |
2885 | ||
465bac91 | 2886 | static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = { |
48725778 KI |
2887 | {.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr, |
2888 | .flags = RTNL_FLAG_DOIT_PERNET}, | |
4df5066f KI |
2889 | {.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr, |
2890 | .flags = RTNL_FLAG_DOIT_PERNET}, | |
465bac91 KI |
2891 | {.protocol = PF_INET, .msgtype = RTM_GETADDR, .dumpit = inet_dump_ifaddr, |
2892 | .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE}, | |
2893 | {.protocol = PF_INET, .msgtype = RTM_GETNETCONF, | |
2894 | .doit = inet_netconf_get_devconf, .dumpit = inet_netconf_dump_devconf, | |
2895 | .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, | |
eb4e17a1 YH |
2896 | {.owner = THIS_MODULE, .protocol = PF_INET, .msgtype = RTM_GETMULTICAST, |
2897 | .dumpit = inet_dump_ifmcaddr, .flags = RTNL_FLAG_DUMP_UNLOCKED}, | |
465bac91 KI |
2898 | }; |
2899 | ||
1da177e4 LT |
2900 | void __init devinet_init(void) |
2901 | { | |
752d14dc | 2902 | register_pernet_subsys(&devinet_ops); |
1da177e4 | 2903 | register_netdevice_notifier(&ip_netdev_notifier); |
63f3444f | 2904 | |
26eebdc4 KI |
2905 | if (rtnl_af_register(&inet_af_ops)) |
2906 | panic("Unable to register inet_af_ops\n"); | |
9f0f7272 | 2907 | |
465bac91 | 2908 | rtnl_register_many(devinet_rtnl_msg_handlers); |
1da177e4 | 2909 | } |