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