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