]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/devinet.c
ixgbe: Make queue pairs on single MSI-X interrupts
[net-next-2.6.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
LT
28
29#include <asm/uaccess.h>
30#include <asm/system.h>
31#include <linux/bitops.h>
4fc268d2 32#include <linux/capability.h>
1da177e4
LT
33#include <linux/module.h>
34#include <linux/types.h>
35#include <linux/kernel.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>
53#ifdef CONFIG_SYSCTL
54#include <linux/sysctl.h>
55#endif
56#include <linux/kmod.h>
57
14c85021 58#include <net/arp.h>
1da177e4
LT
59#include <net/ip.h>
60#include <net/route.h>
61#include <net/ip_fib.h>
63f3444f 62#include <net/rtnetlink.h>
752d14dc 63#include <net/net_namespace.h>
1da177e4 64
0027ba84 65static struct ipv4_devconf ipv4_devconf = {
42f811b8
HX
66 .data = {
67 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
68 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
69 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
70 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
71 },
1da177e4
LT
72};
73
74static struct ipv4_devconf ipv4_devconf_dflt = {
42f811b8
HX
75 .data = {
76 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
77 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
78 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
79 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
80 [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
81 },
1da177e4
LT
82};
83
9355bbd6
PE
84#define IPV4_DEVCONF_DFLT(net, attr) \
85 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
42f811b8 86
ef7c79ed 87static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
5c753978
TG
88 [IFA_LOCAL] = { .type = NLA_U32 },
89 [IFA_ADDRESS] = { .type = NLA_U32 },
90 [IFA_BROADCAST] = { .type = NLA_U32 },
5176f91e 91 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
5c753978
TG
92};
93
d6062cbb 94static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
1da177e4 95
e041c683 96static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
1da177e4
LT
97static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
98 int destroy);
99#ifdef CONFIG_SYSCTL
66f27a52 100static void devinet_sysctl_register(struct in_device *idev);
51602b2a
PE
101static void devinet_sysctl_unregister(struct in_device *idev);
102#else
103static inline void devinet_sysctl_register(struct in_device *idev)
104{
105}
106static inline void devinet_sysctl_unregister(struct in_device *idev)
107{
108}
1da177e4
LT
109#endif
110
111/* Locks all the inet devices. */
112
113static struct in_ifaddr *inet_alloc_ifa(void)
114{
93adcc80 115 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
1da177e4
LT
116}
117
118static void inet_rcu_free_ifa(struct rcu_head *head)
119{
120 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
121 if (ifa->ifa_dev)
122 in_dev_put(ifa->ifa_dev);
123 kfree(ifa);
124}
125
126static inline void inet_free_ifa(struct in_ifaddr *ifa)
127{
128 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
129}
130
131void in_dev_finish_destroy(struct in_device *idev)
132{
133 struct net_device *dev = idev->dev;
134
547b792c
IJ
135 WARN_ON(idev->ifa_list);
136 WARN_ON(idev->mc_list);
1da177e4
LT
137#ifdef NET_REFCNT_DEBUG
138 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
139 idev, dev ? dev->name : "NIL");
140#endif
141 dev_put(dev);
142 if (!idev->dead)
9f9354b9
ED
143 pr_err("Freeing alive in_device %p\n", idev);
144 else
1da177e4 145 kfree(idev);
1da177e4 146}
9f9354b9 147EXPORT_SYMBOL(in_dev_finish_destroy);
1da177e4 148
71e27da9 149static struct in_device *inetdev_init(struct net_device *dev)
1da177e4
LT
150{
151 struct in_device *in_dev;
152
153 ASSERT_RTNL();
154
0da974f4 155 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
1da177e4
LT
156 if (!in_dev)
157 goto out;
c346dca1 158 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
9355bbd6 159 sizeof(in_dev->cnf));
1da177e4
LT
160 in_dev->cnf.sysctl = NULL;
161 in_dev->dev = dev;
9f9354b9
ED
162 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
163 if (!in_dev->arp_parms)
1da177e4 164 goto out_kfree;
0187bdfb
BH
165 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
166 dev_disable_lro(dev);
1da177e4
LT
167 /* Reference in_dev->dev */
168 dev_hold(dev);
30c4cf57 169 /* Account for reference dev->ip_ptr (below) */
1da177e4 170 in_dev_hold(in_dev);
1da177e4 171
66f27a52 172 devinet_sysctl_register(in_dev);
1da177e4
LT
173 ip_mc_init_dev(in_dev);
174 if (dev->flags & IFF_UP)
175 ip_mc_up(in_dev);
483479ec 176
30c4cf57
DS
177 /* we can receive as soon as ip_ptr is set -- do this last */
178 rcu_assign_pointer(dev->ip_ptr, in_dev);
483479ec 179out:
1da177e4
LT
180 return in_dev;
181out_kfree:
182 kfree(in_dev);
183 in_dev = NULL;
184 goto out;
185}
186
187static void in_dev_rcu_put(struct rcu_head *head)
188{
189 struct in_device *idev = container_of(head, struct in_device, rcu_head);
190 in_dev_put(idev);
191}
192
193static void inetdev_destroy(struct in_device *in_dev)
194{
195 struct in_ifaddr *ifa;
196 struct net_device *dev;
197
198 ASSERT_RTNL();
199
200 dev = in_dev->dev;
1da177e4
LT
201
202 in_dev->dead = 1;
203
204 ip_mc_destroy_dev(in_dev);
205
206 while ((ifa = in_dev->ifa_list) != NULL) {
207 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
208 inet_free_ifa(ifa);
209 }
210
1da177e4
LT
211 dev->ip_ptr = NULL;
212
51602b2a 213 devinet_sysctl_unregister(in_dev);
1da177e4
LT
214 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
215 arp_ifdown(dev);
216
217 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
218}
219
ff428d72 220int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
1da177e4
LT
221{
222 rcu_read_lock();
223 for_primary_ifa(in_dev) {
224 if (inet_ifa_match(a, ifa)) {
225 if (!b || inet_ifa_match(b, ifa)) {
226 rcu_read_unlock();
227 return 1;
228 }
229 }
230 } endfor_ifa(in_dev);
231 rcu_read_unlock();
232 return 0;
233}
234
d6062cbb
TG
235static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
236 int destroy, struct nlmsghdr *nlh, u32 pid)
1da177e4 237{
8f937c60 238 struct in_ifaddr *promote = NULL;
0ff60a45
JHS
239 struct in_ifaddr *ifa, *ifa1 = *ifap;
240 struct in_ifaddr *last_prim = in_dev->ifa_list;
241 struct in_ifaddr *prev_prom = NULL;
242 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
1da177e4
LT
243
244 ASSERT_RTNL();
245
e905a9ed 246 /* 1. Deleting primary ifaddr forces deletion all secondaries
8f937c60
HW
247 * unless alias promotion is set
248 **/
1da177e4
LT
249
250 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
1da177e4
LT
251 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
252
253 while ((ifa = *ifap1) != NULL) {
e905a9ed 254 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
0ff60a45
JHS
255 ifa1->ifa_scope <= ifa->ifa_scope)
256 last_prim = ifa;
257
1da177e4
LT
258 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
259 ifa1->ifa_mask != ifa->ifa_mask ||
260 !inet_ifa_match(ifa1->ifa_address, ifa)) {
261 ifap1 = &ifa->ifa_next;
0ff60a45 262 prev_prom = ifa;
1da177e4
LT
263 continue;
264 }
265
0ff60a45 266 if (!do_promote) {
8f937c60 267 *ifap1 = ifa->ifa_next;
1da177e4 268
d6062cbb 269 rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
e041c683
AS
270 blocking_notifier_call_chain(&inetaddr_chain,
271 NETDEV_DOWN, ifa);
8f937c60
HW
272 inet_free_ifa(ifa);
273 } else {
274 promote = ifa;
275 break;
276 }
1da177e4
LT
277 }
278 }
279
280 /* 2. Unlink it */
281
282 *ifap = ifa1->ifa_next;
283
284 /* 3. Announce address deletion */
285
286 /* Send message first, then call notifier.
287 At first sight, FIB update triggered by notifier
288 will refer to already deleted ifaddr, that could confuse
289 netlink listeners. It is not true: look, gated sees
290 that route deleted and if it still thinks that ifaddr
291 is valid, it will try to restore deleted routes... Grr.
292 So that, this order is correct.
293 */
d6062cbb 294 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
e041c683 295 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
1da177e4 296
0ff60a45
JHS
297 if (promote) {
298
299 if (prev_prom) {
300 prev_prom->ifa_next = promote->ifa_next;
301 promote->ifa_next = last_prim->ifa_next;
302 last_prim->ifa_next = promote;
303 }
8f937c60 304
8f937c60 305 promote->ifa_flags &= ~IFA_F_SECONDARY;
d6062cbb 306 rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
e041c683
AS
307 blocking_notifier_call_chain(&inetaddr_chain,
308 NETDEV_UP, promote);
0ff60a45
JHS
309 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
310 if (ifa1->ifa_mask != ifa->ifa_mask ||
311 !inet_ifa_match(ifa1->ifa_address, ifa))
312 continue;
313 fib_add_ifaddr(ifa);
314 }
315
316 }
6363097c 317 if (destroy)
0ff60a45 318 inet_free_ifa(ifa1);
1da177e4
LT
319}
320
d6062cbb
TG
321static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
322 int destroy)
323{
324 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
325}
326
327static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
328 u32 pid)
1da177e4
LT
329{
330 struct in_device *in_dev = ifa->ifa_dev;
331 struct in_ifaddr *ifa1, **ifap, **last_primary;
332
333 ASSERT_RTNL();
334
335 if (!ifa->ifa_local) {
336 inet_free_ifa(ifa);
337 return 0;
338 }
339
340 ifa->ifa_flags &= ~IFA_F_SECONDARY;
341 last_primary = &in_dev->ifa_list;
342
343 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
344 ifap = &ifa1->ifa_next) {
345 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
346 ifa->ifa_scope <= ifa1->ifa_scope)
347 last_primary = &ifa1->ifa_next;
348 if (ifa1->ifa_mask == ifa->ifa_mask &&
349 inet_ifa_match(ifa1->ifa_address, ifa)) {
350 if (ifa1->ifa_local == ifa->ifa_local) {
351 inet_free_ifa(ifa);
352 return -EEXIST;
353 }
354 if (ifa1->ifa_scope != ifa->ifa_scope) {
355 inet_free_ifa(ifa);
356 return -EINVAL;
357 }
358 ifa->ifa_flags |= IFA_F_SECONDARY;
359 }
360 }
361
362 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
363 net_srandom(ifa->ifa_local);
364 ifap = last_primary;
365 }
366
367 ifa->ifa_next = *ifap;
368 *ifap = ifa;
369
370 /* Send message first, then call notifier.
371 Notifier will trigger FIB update, so that
372 listeners of netlink will know about new ifaddr */
d6062cbb 373 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
e041c683 374 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
1da177e4
LT
375
376 return 0;
377}
378
d6062cbb
TG
379static int inet_insert_ifa(struct in_ifaddr *ifa)
380{
381 return __inet_insert_ifa(ifa, NULL, 0);
382}
383
1da177e4
LT
384static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
385{
e5ed6399 386 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1da177e4
LT
387
388 ASSERT_RTNL();
389
390 if (!in_dev) {
71e27da9
HX
391 inet_free_ifa(ifa);
392 return -ENOBUFS;
1da177e4 393 }
71e27da9 394 ipv4_devconf_setall(in_dev);
1da177e4 395 if (ifa->ifa_dev != in_dev) {
547b792c 396 WARN_ON(ifa->ifa_dev);
1da177e4
LT
397 in_dev_hold(in_dev);
398 ifa->ifa_dev = in_dev;
399 }
f97c1e0c 400 if (ipv4_is_loopback(ifa->ifa_local))
1da177e4
LT
401 ifa->ifa_scope = RT_SCOPE_HOST;
402 return inet_insert_ifa(ifa);
403}
404
7fee0ca2 405struct in_device *inetdev_by_index(struct net *net, int ifindex)
1da177e4
LT
406{
407 struct net_device *dev;
408 struct in_device *in_dev = NULL;
c148fc2e
ED
409
410 rcu_read_lock();
411 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
412 if (dev)
413 in_dev = in_dev_get(dev);
c148fc2e 414 rcu_read_unlock();
1da177e4
LT
415 return in_dev;
416}
9f9354b9 417EXPORT_SYMBOL(inetdev_by_index);
1da177e4
LT
418
419/* Called only from RTNL semaphored context. No locks. */
420
60cad5da
AV
421struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
422 __be32 mask)
1da177e4
LT
423{
424 ASSERT_RTNL();
425
426 for_primary_ifa(in_dev) {
427 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
428 return ifa;
429 } endfor_ifa(in_dev);
430 return NULL;
431}
432
433static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
434{
3b1e0a65 435 struct net *net = sock_net(skb->sk);
dfdd5fd4 436 struct nlattr *tb[IFA_MAX+1];
1da177e4 437 struct in_device *in_dev;
dfdd5fd4 438 struct ifaddrmsg *ifm;
1da177e4 439 struct in_ifaddr *ifa, **ifap;
dfdd5fd4 440 int err = -EINVAL;
1da177e4
LT
441
442 ASSERT_RTNL();
443
dfdd5fd4
TG
444 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
445 if (err < 0)
446 goto errout;
447
448 ifm = nlmsg_data(nlh);
7fee0ca2 449 in_dev = inetdev_by_index(net, ifm->ifa_index);
dfdd5fd4
TG
450 if (in_dev == NULL) {
451 err = -ENODEV;
452 goto errout;
453 }
454
1da177e4
LT
455 __in_dev_put(in_dev);
456
457 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
458 ifap = &ifa->ifa_next) {
dfdd5fd4 459 if (tb[IFA_LOCAL] &&
a7a628c4 460 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
dfdd5fd4
TG
461 continue;
462
463 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
1da177e4 464 continue;
dfdd5fd4
TG
465
466 if (tb[IFA_ADDRESS] &&
467 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
a7a628c4 468 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
dfdd5fd4
TG
469 continue;
470
d6062cbb 471 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
1da177e4
LT
472 return 0;
473 }
dfdd5fd4
TG
474
475 err = -EADDRNOTAVAIL;
476errout:
477 return err;
1da177e4
LT
478}
479
4b8aa9ab 480static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
1da177e4 481{
5c753978
TG
482 struct nlattr *tb[IFA_MAX+1];
483 struct in_ifaddr *ifa;
484 struct ifaddrmsg *ifm;
1da177e4
LT
485 struct net_device *dev;
486 struct in_device *in_dev;
7b218574 487 int err;
1da177e4 488
5c753978
TG
489 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
490 if (err < 0)
491 goto errout;
1da177e4 492
5c753978 493 ifm = nlmsg_data(nlh);
7b218574
DL
494 err = -EINVAL;
495 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
5c753978 496 goto errout;
1da177e4 497
4b8aa9ab 498 dev = __dev_get_by_index(net, ifm->ifa_index);
7b218574
DL
499 err = -ENODEV;
500 if (dev == NULL)
5c753978 501 goto errout;
1da177e4 502
5c753978 503 in_dev = __in_dev_get_rtnl(dev);
7b218574
DL
504 err = -ENOBUFS;
505 if (in_dev == NULL)
71e27da9 506 goto errout;
1da177e4 507
5c753978 508 ifa = inet_alloc_ifa();
7b218574 509 if (ifa == NULL)
5c753978
TG
510 /*
511 * A potential indev allocation can be left alive, it stays
512 * assigned to its device and is destroy with it.
513 */
5c753978 514 goto errout;
5c753978 515
a4e65d36 516 ipv4_devconf_setall(in_dev);
5c753978
TG
517 in_dev_hold(in_dev);
518
519 if (tb[IFA_ADDRESS] == NULL)
520 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1da177e4 521
1da177e4
LT
522 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
523 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
1da177e4
LT
524 ifa->ifa_flags = ifm->ifa_flags;
525 ifa->ifa_scope = ifm->ifa_scope;
5c753978
TG
526 ifa->ifa_dev = in_dev;
527
a7a628c4
AV
528 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
529 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
5c753978
TG
530
531 if (tb[IFA_BROADCAST])
a7a628c4 532 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
5c753978 533
5c753978
TG
534 if (tb[IFA_LABEL])
535 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
1da177e4
LT
536 else
537 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
538
5c753978
TG
539 return ifa;
540
541errout:
542 return ERR_PTR(err);
543}
544
545static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
546{
3b1e0a65 547 struct net *net = sock_net(skb->sk);
5c753978
TG
548 struct in_ifaddr *ifa;
549
550 ASSERT_RTNL();
551
4b8aa9ab 552 ifa = rtm_to_ifaddr(net, nlh);
5c753978
TG
553 if (IS_ERR(ifa))
554 return PTR_ERR(ifa);
555
d6062cbb 556 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
1da177e4
LT
557}
558
559/*
560 * Determine a default network mask, based on the IP address.
561 */
562
9f9354b9 563static inline int inet_abc_len(__be32 addr)
1da177e4
LT
564{
565 int rc = -1; /* Something else, probably a multicast. */
566
f97c1e0c 567 if (ipv4_is_zeronet(addr))
e905a9ed 568 rc = 0;
1da177e4 569 else {
714e85be 570 __u32 haddr = ntohl(addr);
1da177e4 571
714e85be 572 if (IN_CLASSA(haddr))
1da177e4 573 rc = 8;
714e85be 574 else if (IN_CLASSB(haddr))
1da177e4 575 rc = 16;
714e85be 576 else if (IN_CLASSC(haddr))
1da177e4
LT
577 rc = 24;
578 }
579
e905a9ed 580 return rc;
1da177e4
LT
581}
582
583
e5b13cb1 584int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1da177e4
LT
585{
586 struct ifreq ifr;
587 struct sockaddr_in sin_orig;
588 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
589 struct in_device *in_dev;
590 struct in_ifaddr **ifap = NULL;
591 struct in_ifaddr *ifa = NULL;
592 struct net_device *dev;
593 char *colon;
594 int ret = -EFAULT;
595 int tryaddrmatch = 0;
596
597 /*
598 * Fetch the caller's info block into kernel space
599 */
600
601 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
602 goto out;
603 ifr.ifr_name[IFNAMSIZ - 1] = 0;
604
605 /* save original address for comparison */
606 memcpy(&sin_orig, sin, sizeof(*sin));
607
608 colon = strchr(ifr.ifr_name, ':');
609 if (colon)
610 *colon = 0;
611
e5b13cb1 612 dev_load(net, ifr.ifr_name);
1da177e4 613
132adf54 614 switch (cmd) {
1da177e4
LT
615 case SIOCGIFADDR: /* Get interface address */
616 case SIOCGIFBRDADDR: /* Get the broadcast address */
617 case SIOCGIFDSTADDR: /* Get the destination address */
618 case SIOCGIFNETMASK: /* Get the netmask for the interface */
619 /* Note that these ioctls will not sleep,
620 so that we do not impose a lock.
621 One day we will be forced to put shlock here (I mean SMP)
622 */
623 tryaddrmatch = (sin_orig.sin_family == AF_INET);
624 memset(sin, 0, sizeof(*sin));
625 sin->sin_family = AF_INET;
626 break;
627
628 case SIOCSIFFLAGS:
629 ret = -EACCES;
630 if (!capable(CAP_NET_ADMIN))
631 goto out;
632 break;
633 case SIOCSIFADDR: /* Set interface address (and family) */
634 case SIOCSIFBRDADDR: /* Set the broadcast address */
635 case SIOCSIFDSTADDR: /* Set the destination address */
636 case SIOCSIFNETMASK: /* Set the netmask for the interface */
637 ret = -EACCES;
638 if (!capable(CAP_NET_ADMIN))
639 goto out;
640 ret = -EINVAL;
641 if (sin->sin_family != AF_INET)
642 goto out;
643 break;
644 default:
645 ret = -EINVAL;
646 goto out;
647 }
648
649 rtnl_lock();
650
651 ret = -ENODEV;
9f9354b9
ED
652 dev = __dev_get_by_name(net, ifr.ifr_name);
653 if (!dev)
1da177e4
LT
654 goto done;
655
656 if (colon)
657 *colon = ':';
658
9f9354b9
ED
659 in_dev = __in_dev_get_rtnl(dev);
660 if (in_dev) {
1da177e4
LT
661 if (tryaddrmatch) {
662 /* Matthias Andree */
663 /* compare label and address (4.4BSD style) */
664 /* note: we only do this for a limited set of ioctls
665 and only if the original address family was AF_INET.
666 This is checked above. */
667 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
668 ifap = &ifa->ifa_next) {
669 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
670 sin_orig.sin_addr.s_addr ==
671 ifa->ifa_address) {
672 break; /* found */
673 }
674 }
675 }
676 /* we didn't get a match, maybe the application is
677 4.3BSD-style and passed in junk so we fall back to
678 comparing just the label */
679 if (!ifa) {
680 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
681 ifap = &ifa->ifa_next)
682 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
683 break;
684 }
685 }
686
687 ret = -EADDRNOTAVAIL;
688 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
689 goto done;
690
132adf54 691 switch (cmd) {
1da177e4
LT
692 case SIOCGIFADDR: /* Get interface address */
693 sin->sin_addr.s_addr = ifa->ifa_local;
694 goto rarok;
695
696 case SIOCGIFBRDADDR: /* Get the broadcast address */
697 sin->sin_addr.s_addr = ifa->ifa_broadcast;
698 goto rarok;
699
700 case SIOCGIFDSTADDR: /* Get the destination address */
701 sin->sin_addr.s_addr = ifa->ifa_address;
702 goto rarok;
703
704 case SIOCGIFNETMASK: /* Get the netmask for the interface */
705 sin->sin_addr.s_addr = ifa->ifa_mask;
706 goto rarok;
707
708 case SIOCSIFFLAGS:
709 if (colon) {
710 ret = -EADDRNOTAVAIL;
711 if (!ifa)
712 break;
713 ret = 0;
714 if (!(ifr.ifr_flags & IFF_UP))
715 inet_del_ifa(in_dev, ifap, 1);
716 break;
717 }
718 ret = dev_change_flags(dev, ifr.ifr_flags);
719 break;
720
721 case SIOCSIFADDR: /* Set interface address (and family) */
722 ret = -EINVAL;
723 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
724 break;
725
726 if (!ifa) {
727 ret = -ENOBUFS;
9f9354b9
ED
728 ifa = inet_alloc_ifa();
729 if (!ifa)
1da177e4
LT
730 break;
731 if (colon)
732 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
733 else
734 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
735 } else {
736 ret = 0;
737 if (ifa->ifa_local == sin->sin_addr.s_addr)
738 break;
739 inet_del_ifa(in_dev, ifap, 0);
740 ifa->ifa_broadcast = 0;
148f9729 741 ifa->ifa_scope = 0;
1da177e4
LT
742 }
743
744 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
745
746 if (!(dev->flags & IFF_POINTOPOINT)) {
747 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
748 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
749 if ((dev->flags & IFF_BROADCAST) &&
750 ifa->ifa_prefixlen < 31)
751 ifa->ifa_broadcast = ifa->ifa_address |
752 ~ifa->ifa_mask;
753 } else {
754 ifa->ifa_prefixlen = 32;
755 ifa->ifa_mask = inet_make_mask(32);
756 }
757 ret = inet_set_ifa(dev, ifa);
758 break;
759
760 case SIOCSIFBRDADDR: /* Set the broadcast address */
761 ret = 0;
762 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
763 inet_del_ifa(in_dev, ifap, 0);
764 ifa->ifa_broadcast = sin->sin_addr.s_addr;
765 inet_insert_ifa(ifa);
766 }
767 break;
768
769 case SIOCSIFDSTADDR: /* Set the destination address */
770 ret = 0;
771 if (ifa->ifa_address == sin->sin_addr.s_addr)
772 break;
773 ret = -EINVAL;
774 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
775 break;
776 ret = 0;
777 inet_del_ifa(in_dev, ifap, 0);
778 ifa->ifa_address = sin->sin_addr.s_addr;
779 inet_insert_ifa(ifa);
780 break;
781
782 case SIOCSIFNETMASK: /* Set the netmask for the interface */
783
784 /*
785 * The mask we set must be legal.
786 */
787 ret = -EINVAL;
788 if (bad_mask(sin->sin_addr.s_addr, 0))
789 break;
790 ret = 0;
791 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
a144ea4b 792 __be32 old_mask = ifa->ifa_mask;
1da177e4
LT
793 inet_del_ifa(in_dev, ifap, 0);
794 ifa->ifa_mask = sin->sin_addr.s_addr;
795 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
796
797 /* See if current broadcast address matches
798 * with current netmask, then recalculate
799 * the broadcast address. Otherwise it's a
800 * funny address, so don't touch it since
801 * the user seems to know what (s)he's doing...
802 */
803 if ((dev->flags & IFF_BROADCAST) &&
804 (ifa->ifa_prefixlen < 31) &&
805 (ifa->ifa_broadcast ==
dcab5e1e 806 (ifa->ifa_local|~old_mask))) {
1da177e4
LT
807 ifa->ifa_broadcast = (ifa->ifa_local |
808 ~sin->sin_addr.s_addr);
809 }
810 inet_insert_ifa(ifa);
811 }
812 break;
813 }
814done:
815 rtnl_unlock();
816out:
817 return ret;
818rarok:
819 rtnl_unlock();
820 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
821 goto out;
822}
823
824static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
825{
e5ed6399 826 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1da177e4
LT
827 struct in_ifaddr *ifa;
828 struct ifreq ifr;
829 int done = 0;
830
9f9354b9 831 if (!in_dev)
1da177e4
LT
832 goto out;
833
9f9354b9 834 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1da177e4
LT
835 if (!buf) {
836 done += sizeof(ifr);
837 continue;
838 }
839 if (len < (int) sizeof(ifr))
840 break;
841 memset(&ifr, 0, sizeof(struct ifreq));
842 if (ifa->ifa_label)
843 strcpy(ifr.ifr_name, ifa->ifa_label);
844 else
845 strcpy(ifr.ifr_name, dev->name);
846
847 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
848 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
849 ifa->ifa_local;
850
851 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
852 done = -EFAULT;
853 break;
854 }
855 buf += sizeof(struct ifreq);
856 len -= sizeof(struct ifreq);
857 done += sizeof(struct ifreq);
858 }
859out:
860 return done;
861}
862
a61ced5d 863__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1da177e4 864{
a61ced5d 865 __be32 addr = 0;
1da177e4 866 struct in_device *in_dev;
c346dca1 867 struct net *net = dev_net(dev);
1da177e4
LT
868
869 rcu_read_lock();
e5ed6399 870 in_dev = __in_dev_get_rcu(dev);
1da177e4
LT
871 if (!in_dev)
872 goto no_in_dev;
873
874 for_primary_ifa(in_dev) {
875 if (ifa->ifa_scope > scope)
876 continue;
877 if (!dst || inet_ifa_match(dst, ifa)) {
878 addr = ifa->ifa_local;
879 break;
880 }
881 if (!addr)
882 addr = ifa->ifa_local;
883 } endfor_ifa(in_dev);
1da177e4
LT
884
885 if (addr)
c6d14c84 886 goto out_unlock;
9f9354b9 887no_in_dev:
1da177e4
LT
888
889 /* Not loopback addresses on loopback should be preferred
890 in this case. It is importnat that lo is the first interface
891 in dev_base list.
892 */
c6d14c84 893 for_each_netdev_rcu(net, dev) {
9f9354b9
ED
894 in_dev = __in_dev_get_rcu(dev);
895 if (!in_dev)
1da177e4
LT
896 continue;
897
898 for_primary_ifa(in_dev) {
899 if (ifa->ifa_scope != RT_SCOPE_LINK &&
900 ifa->ifa_scope <= scope) {
901 addr = ifa->ifa_local;
c6d14c84 902 goto out_unlock;
1da177e4
LT
903 }
904 } endfor_ifa(in_dev);
905 }
c6d14c84 906out_unlock:
1da177e4 907 rcu_read_unlock();
1da177e4
LT
908 return addr;
909}
9f9354b9 910EXPORT_SYMBOL(inet_select_addr);
1da177e4 911
60cad5da
AV
912static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
913 __be32 local, int scope)
1da177e4
LT
914{
915 int same = 0;
a144ea4b 916 __be32 addr = 0;
1da177e4
LT
917
918 for_ifa(in_dev) {
919 if (!addr &&
920 (local == ifa->ifa_local || !local) &&
921 ifa->ifa_scope <= scope) {
922 addr = ifa->ifa_local;
923 if (same)
924 break;
925 }
926 if (!same) {
927 same = (!local || inet_ifa_match(local, ifa)) &&
928 (!dst || inet_ifa_match(dst, ifa));
929 if (same && addr) {
930 if (local || !dst)
931 break;
932 /* Is the selected addr into dst subnet? */
933 if (inet_ifa_match(addr, ifa))
934 break;
935 /* No, then can we use new local src? */
936 if (ifa->ifa_scope <= scope) {
937 addr = ifa->ifa_local;
938 break;
939 }
940 /* search for large dst subnet for addr */
941 same = 0;
942 }
943 }
944 } endfor_ifa(in_dev);
945
9f9354b9 946 return same ? addr : 0;
1da177e4
LT
947}
948
949/*
950 * Confirm that local IP address exists using wildcards:
9bd85e32 951 * - in_dev: only on this interface, 0=any interface
1da177e4
LT
952 * - dst: only in the same subnet as dst, 0=any dst
953 * - local: address, 0=autoselect the local address
954 * - scope: maximum allowed scope value for the local address
955 */
9bd85e32
DL
956__be32 inet_confirm_addr(struct in_device *in_dev,
957 __be32 dst, __be32 local, int scope)
1da177e4 958{
60cad5da 959 __be32 addr = 0;
9bd85e32 960 struct net_device *dev;
39a6d063 961 struct net *net;
1da177e4 962
39a6d063 963 if (scope != RT_SCOPE_LINK)
9bd85e32 964 return confirm_addr_indev(in_dev, dst, local, scope);
1da177e4 965
c346dca1 966 net = dev_net(in_dev->dev);
1da177e4 967 rcu_read_lock();
c6d14c84 968 for_each_netdev_rcu(net, dev) {
9f9354b9
ED
969 in_dev = __in_dev_get_rcu(dev);
970 if (in_dev) {
1da177e4
LT
971 addr = confirm_addr_indev(in_dev, dst, local, scope);
972 if (addr)
973 break;
974 }
975 }
976 rcu_read_unlock();
1da177e4
LT
977
978 return addr;
979}
980
981/*
982 * Device notifier
983 */
984
985int register_inetaddr_notifier(struct notifier_block *nb)
986{
e041c683 987 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1da177e4 988}
9f9354b9 989EXPORT_SYMBOL(register_inetaddr_notifier);
1da177e4
LT
990
991int unregister_inetaddr_notifier(struct notifier_block *nb)
992{
e041c683 993 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1da177e4 994}
9f9354b9 995EXPORT_SYMBOL(unregister_inetaddr_notifier);
1da177e4 996
9f9354b9
ED
997/* Rename ifa_labels for a device name change. Make some effort to preserve
998 * existing alias numbering and to create unique labels if possible.
1da177e4
LT
999*/
1000static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
e905a9ed 1001{
1da177e4
LT
1002 struct in_ifaddr *ifa;
1003 int named = 0;
1004
e905a9ed
YH
1005 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1006 char old[IFNAMSIZ], *dot;
1da177e4
LT
1007
1008 memcpy(old, ifa->ifa_label, IFNAMSIZ);
e905a9ed 1009 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1da177e4 1010 if (named++ == 0)
573bf470 1011 goto skip;
44344b2a 1012 dot = strchr(old, ':');
e905a9ed
YH
1013 if (dot == NULL) {
1014 sprintf(old, ":%d", named);
1da177e4
LT
1015 dot = old;
1016 }
9f9354b9 1017 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
e905a9ed 1018 strcat(ifa->ifa_label, dot);
9f9354b9 1019 else
e905a9ed 1020 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
573bf470
TG
1021skip:
1022 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
e905a9ed
YH
1023 }
1024}
1da177e4 1025
06770843
BL
1026static inline bool inetdev_valid_mtu(unsigned mtu)
1027{
1028 return mtu >= 68;
1029}
1030
1da177e4
LT
1031/* Called only under RTNL semaphore */
1032
1033static int inetdev_event(struct notifier_block *this, unsigned long event,
1034 void *ptr)
1035{
1036 struct net_device *dev = ptr;
e5ed6399 1037 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1da177e4
LT
1038
1039 ASSERT_RTNL();
1040
1041 if (!in_dev) {
8030f544 1042 if (event == NETDEV_REGISTER) {
1da177e4 1043 in_dev = inetdev_init(dev);
b217d616
HX
1044 if (!in_dev)
1045 return notifier_from_errno(-ENOMEM);
0cc217e1 1046 if (dev->flags & IFF_LOOPBACK) {
42f811b8
HX
1047 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1048 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
8030f544 1049 }
06770843
BL
1050 } else if (event == NETDEV_CHANGEMTU) {
1051 /* Re-enabling IP */
1052 if (inetdev_valid_mtu(dev->mtu))
1053 in_dev = inetdev_init(dev);
1da177e4
LT
1054 }
1055 goto out;
1056 }
1057
1058 switch (event) {
1059 case NETDEV_REGISTER:
1060 printk(KERN_DEBUG "inetdev_event: bug\n");
1061 dev->ip_ptr = NULL;
1062 break;
1063 case NETDEV_UP:
06770843 1064 if (!inetdev_valid_mtu(dev->mtu))
1da177e4 1065 break;
0cc217e1 1066 if (dev->flags & IFF_LOOPBACK) {
9f9354b9
ED
1067 struct in_ifaddr *ifa = inet_alloc_ifa();
1068
1069 if (ifa) {
1da177e4
LT
1070 ifa->ifa_local =
1071 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1072 ifa->ifa_prefixlen = 8;
1073 ifa->ifa_mask = inet_make_mask(8);
1074 in_dev_hold(in_dev);
1075 ifa->ifa_dev = in_dev;
1076 ifa->ifa_scope = RT_SCOPE_HOST;
1077 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1078 inet_insert_ifa(ifa);
1079 }
1080 }
1081 ip_mc_up(in_dev);
eefef1cf
SH
1082 /* fall through */
1083 case NETDEV_CHANGEADDR:
a21090cf
SH
1084 /* Send gratuitous ARP to notify of link change */
1085 if (IN_DEV_ARP_NOTIFY(in_dev)) {
1086 struct in_ifaddr *ifa = in_dev->ifa_list;
1087
1088 if (ifa)
1089 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1090 ifa->ifa_address, dev,
1091 ifa->ifa_address, NULL,
1092 dev->dev_addr, NULL);
1093 }
1da177e4
LT
1094 break;
1095 case NETDEV_DOWN:
1096 ip_mc_down(in_dev);
1097 break;
75c78500
MS
1098 case NETDEV_BONDING_OLDTYPE:
1099 ip_mc_unmap(in_dev);
1100 break;
1101 case NETDEV_BONDING_NEWTYPE:
1102 ip_mc_remap(in_dev);
1103 break;
1da177e4 1104 case NETDEV_CHANGEMTU:
06770843 1105 if (inetdev_valid_mtu(dev->mtu))
1da177e4 1106 break;
06770843 1107 /* disable IP when MTU is not enough */
1da177e4
LT
1108 case NETDEV_UNREGISTER:
1109 inetdev_destroy(in_dev);
1110 break;
1111 case NETDEV_CHANGENAME:
1112 /* Do not notify about label change, this event is
1113 * not interesting to applications using netlink.
1114 */
1115 inetdev_changename(dev, in_dev);
1116
51602b2a 1117 devinet_sysctl_unregister(in_dev);
66f27a52 1118 devinet_sysctl_register(in_dev);
1da177e4
LT
1119 break;
1120 }
1121out:
1122 return NOTIFY_DONE;
1123}
1124
1125static struct notifier_block ip_netdev_notifier = {
539afedf 1126 .notifier_call = inetdev_event,
1da177e4
LT
1127};
1128
339bf98f
TG
1129static inline size_t inet_nlmsg_size(void)
1130{
1131 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1132 + nla_total_size(4) /* IFA_ADDRESS */
1133 + nla_total_size(4) /* IFA_LOCAL */
1134 + nla_total_size(4) /* IFA_BROADCAST */
339bf98f
TG
1135 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1136}
1137
1da177e4 1138static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
b6544c0b 1139 u32 pid, u32 seq, int event, unsigned int flags)
1da177e4
LT
1140{
1141 struct ifaddrmsg *ifm;
1142 struct nlmsghdr *nlh;
1da177e4 1143
47f68512
TG
1144 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1145 if (nlh == NULL)
26932566 1146 return -EMSGSIZE;
47f68512
TG
1147
1148 ifm = nlmsg_data(nlh);
1da177e4
LT
1149 ifm->ifa_family = AF_INET;
1150 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1151 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1152 ifm->ifa_scope = ifa->ifa_scope;
1153 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
47f68512 1154
1da177e4 1155 if (ifa->ifa_address)
a7a628c4 1156 NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
47f68512 1157
1da177e4 1158 if (ifa->ifa_local)
a7a628c4 1159 NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
47f68512 1160
1da177e4 1161 if (ifa->ifa_broadcast)
a7a628c4 1162 NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
47f68512 1163
1da177e4 1164 if (ifa->ifa_label[0])
47f68512 1165 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1da177e4 1166
47f68512
TG
1167 return nlmsg_end(skb, nlh);
1168
1169nla_put_failure:
26932566
PM
1170 nlmsg_cancel(skb, nlh);
1171 return -EMSGSIZE;
1da177e4
LT
1172}
1173
1174static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1175{
3b1e0a65 1176 struct net *net = sock_net(skb->sk);
1da177e4
LT
1177 int idx, ip_idx;
1178 struct net_device *dev;
1179 struct in_device *in_dev;
1180 struct in_ifaddr *ifa;
1181 int s_ip_idx, s_idx = cb->args[0];
1182
1183 s_ip_idx = ip_idx = cb->args[1];
7562f876 1184 idx = 0;
4b8aa9ab 1185 for_each_netdev(net, dev) {
1da177e4 1186 if (idx < s_idx)
7562f876 1187 goto cont;
1da177e4
LT
1188 if (idx > s_idx)
1189 s_ip_idx = 0;
9f9354b9
ED
1190 in_dev = __in_dev_get_rtnl(dev);
1191 if (!in_dev)
7562f876 1192 goto cont;
1da177e4
LT
1193
1194 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1195 ifa = ifa->ifa_next, ip_idx++) {
1196 if (ip_idx < s_ip_idx)
596e4150 1197 continue;
1da177e4
LT
1198 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1199 cb->nlh->nlmsg_seq,
6313c1e0 1200 RTM_NEWADDR, NLM_F_MULTI) <= 0)
1da177e4 1201 goto done;
1da177e4 1202 }
7562f876
PE
1203cont:
1204 idx++;
1da177e4
LT
1205 }
1206
1207done:
1da177e4
LT
1208 cb->args[0] = idx;
1209 cb->args[1] = ip_idx;
1210
1211 return skb->len;
1212}
1213
539afedf 1214static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
d6062cbb 1215 u32 pid)
1da177e4 1216{
47f68512 1217 struct sk_buff *skb;
d6062cbb
TG
1218 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1219 int err = -ENOBUFS;
4b8aa9ab 1220 struct net *net;
1da177e4 1221
c346dca1 1222 net = dev_net(ifa->ifa_dev->dev);
339bf98f 1223 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
47f68512 1224 if (skb == NULL)
d6062cbb
TG
1225 goto errout;
1226
1227 err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
26932566
PM
1228 if (err < 0) {
1229 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1230 WARN_ON(err == -EMSGSIZE);
1231 kfree_skb(skb);
1232 goto errout;
1233 }
1ce85fe4
PNA
1234 rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1235 return;
d6062cbb
TG
1236errout:
1237 if (err < 0)
4b8aa9ab 1238 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1da177e4
LT
1239}
1240
1da177e4
LT
1241#ifdef CONFIG_SYSCTL
1242
c0ce9fb3 1243static void devinet_copy_dflt_conf(struct net *net, int i)
31be3085
HX
1244{
1245 struct net_device *dev;
1246
c6d14c84
ED
1247 rcu_read_lock();
1248 for_each_netdev_rcu(net, dev) {
31be3085 1249 struct in_device *in_dev;
c6d14c84 1250
31be3085
HX
1251 in_dev = __in_dev_get_rcu(dev);
1252 if (in_dev && !test_bit(i, in_dev->cnf.state))
9355bbd6 1253 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
31be3085 1254 }
c6d14c84 1255 rcu_read_unlock();
31be3085
HX
1256}
1257
c6d14c84 1258/* called with RTNL locked */
c0ce9fb3 1259static void inet_forward_change(struct net *net)
68dd299b
PE
1260{
1261 struct net_device *dev;
586f1211 1262 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
68dd299b 1263
586f1211 1264 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
9355bbd6 1265 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
68dd299b 1266
c0ce9fb3 1267 for_each_netdev(net, dev) {
68dd299b 1268 struct in_device *in_dev;
0187bdfb
BH
1269 if (on)
1270 dev_disable_lro(dev);
68dd299b
PE
1271 rcu_read_lock();
1272 in_dev = __in_dev_get_rcu(dev);
1273 if (in_dev)
1274 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1275 rcu_read_unlock();
1276 }
68dd299b
PE
1277}
1278
31be3085 1279static int devinet_conf_proc(ctl_table *ctl, int write,
8d65af78 1280 void __user *buffer,
31be3085
HX
1281 size_t *lenp, loff_t *ppos)
1282{
8d65af78 1283 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
31be3085
HX
1284
1285 if (write) {
1286 struct ipv4_devconf *cnf = ctl->extra1;
c0ce9fb3 1287 struct net *net = ctl->extra2;
31be3085
HX
1288 int i = (int *)ctl->data - cnf->data;
1289
1290 set_bit(i, cnf->state);
1291
9355bbd6 1292 if (cnf == net->ipv4.devconf_dflt)
c0ce9fb3 1293 devinet_copy_dflt_conf(net, i);
31be3085
HX
1294 }
1295
1296 return ret;
1297}
1298
f221e726 1299static int devinet_conf_sysctl(ctl_table *table,
31be3085
HX
1300 void __user *oldval, size_t __user *oldlenp,
1301 void __user *newval, size_t newlen)
1302{
1303 struct ipv4_devconf *cnf;
c0ce9fb3 1304 struct net *net;
31be3085
HX
1305 int *valp = table->data;
1306 int new;
1307 int i;
1308
1309 if (!newval || !newlen)
1310 return 0;
1311
1312 if (newlen != sizeof(int))
1313 return -EINVAL;
1314
1315 if (get_user(new, (int __user *)newval))
1316 return -EFAULT;
1317
1318 if (new == *valp)
1319 return 0;
1320
1321 if (oldval && oldlenp) {
1322 size_t len;
1323
1324 if (get_user(len, oldlenp))
1325 return -EFAULT;
1326
1327 if (len) {
1328 if (len > table->maxlen)
1329 len = table->maxlen;
1330 if (copy_to_user(oldval, valp, len))
1331 return -EFAULT;
1332 if (put_user(len, oldlenp))
1333 return -EFAULT;
1334 }
1335 }
1336
1337 *valp = new;
1338
1339 cnf = table->extra1;
c0ce9fb3 1340 net = table->extra2;
31be3085
HX
1341 i = (int *)table->data - cnf->data;
1342
1343 set_bit(i, cnf->state);
1344
9355bbd6 1345 if (cnf == net->ipv4.devconf_dflt)
c0ce9fb3 1346 devinet_copy_dflt_conf(net, i);
31be3085
HX
1347
1348 return 1;
1349}
1350
1da177e4 1351static int devinet_sysctl_forward(ctl_table *ctl, int write,
8d65af78 1352 void __user *buffer,
1da177e4
LT
1353 size_t *lenp, loff_t *ppos)
1354{
1355 int *valp = ctl->data;
1356 int val = *valp;
8d65af78 1357 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1da177e4
LT
1358
1359 if (write && *valp != val) {
c0ce9fb3
PE
1360 struct net *net = ctl->extra2;
1361
0187bdfb 1362 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
9b8adb5e
EB
1363 if (!rtnl_trylock())
1364 return restart_syscall();
0187bdfb
BH
1365 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1366 inet_forward_change(net);
1367 } else if (*valp) {
1368 struct ipv4_devconf *cnf = ctl->extra1;
1369 struct in_device *idev =
1370 container_of(cnf, struct in_device, cnf);
1371 dev_disable_lro(idev->dev);
1372 }
1373 rtnl_unlock();
76e6ebfb 1374 rt_cache_flush(net, 0);
0187bdfb 1375 }
1da177e4
LT
1376 }
1377
1378 return ret;
1379}
1380
1381int ipv4_doint_and_flush(ctl_table *ctl, int write,
8d65af78 1382 void __user *buffer,
1da177e4
LT
1383 size_t *lenp, loff_t *ppos)
1384{
1385 int *valp = ctl->data;
1386 int val = *valp;
8d65af78 1387 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
76e6ebfb 1388 struct net *net = ctl->extra2;
1da177e4
LT
1389
1390 if (write && *valp != val)
76e6ebfb 1391 rt_cache_flush(net, 0);
1da177e4
LT
1392
1393 return ret;
1394}
1395
f221e726 1396int ipv4_doint_and_flush_strategy(ctl_table *table,
1da177e4 1397 void __user *oldval, size_t __user *oldlenp,
1f29bcd7 1398 void __user *newval, size_t newlen)
1da177e4 1399{
f221e726 1400 int ret = devinet_conf_sysctl(table, oldval, oldlenp, newval, newlen);
76e6ebfb 1401 struct net *net = table->extra2;
1da177e4 1402
31be3085 1403 if (ret == 1)
76e6ebfb 1404 rt_cache_flush(net, 0);
1da177e4 1405
31be3085 1406 return ret;
1da177e4
LT
1407}
1408
1409
42f811b8
HX
1410#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \
1411 { \
1412 .ctl_name = NET_IPV4_CONF_ ## attr, \
1413 .procname = name, \
1414 .data = ipv4_devconf.data + \
1415 NET_IPV4_CONF_ ## attr - 1, \
1416 .maxlen = sizeof(int), \
1417 .mode = mval, \
1418 .proc_handler = proc, \
1419 .strategy = sysctl, \
31be3085 1420 .extra1 = &ipv4_devconf, \
42f811b8
HX
1421 }
1422
1423#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
31be3085
HX
1424 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \
1425 devinet_conf_sysctl)
42f811b8
HX
1426
1427#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
31be3085
HX
1428 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \
1429 devinet_conf_sysctl)
42f811b8
HX
1430
1431#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \
1432 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl)
1433
1434#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1435 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \
1436 ipv4_doint_and_flush_strategy)
1437
1da177e4
LT
1438static struct devinet_sysctl_table {
1439 struct ctl_table_header *sysctl_header;
bfada697
PE
1440 struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1441 char *dev_name;
1da177e4
LT
1442} devinet_sysctl = {
1443 .devinet_vars = {
42f811b8 1444 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
31be3085
HX
1445 devinet_sysctl_forward,
1446 devinet_conf_sysctl),
42f811b8
HX
1447 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1448
1449 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1450 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1451 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1452 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1453 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1454 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1455 "accept_source_route"),
1456 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1457 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1458 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1459 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1460 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1461 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1462 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1463 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1464 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
eefef1cf 1465 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
42f811b8
HX
1466
1467 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1468 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1469 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1470 "force_igmp_version"),
1471 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1472 "promote_secondaries"),
1da177e4 1473 },
1da177e4
LT
1474};
1475
ea40b324
PE
1476static int __devinet_sysctl_register(struct net *net, char *dev_name,
1477 int ctl_name, struct ipv4_devconf *p)
1da177e4
LT
1478{
1479 int i;
9fa89642 1480 struct devinet_sysctl_table *t;
1da177e4 1481
bfada697
PE
1482#define DEVINET_CTL_PATH_DEV 3
1483
1484 struct ctl_path devinet_ctl_path[] = {
1485 { .procname = "net", .ctl_name = CTL_NET, },
1486 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1487 { .procname = "conf", .ctl_name = NET_IPV4_CONF, },
1488 { /* to be set */ },
1489 { },
1490 };
1491
9fa89642 1492 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
1da177e4 1493 if (!t)
9fa89642
PE
1494 goto out;
1495
1da177e4
LT
1496 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1497 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
31be3085 1498 t->devinet_vars[i].extra1 = p;
c0ce9fb3 1499 t->devinet_vars[i].extra2 = net;
1da177e4
LT
1500 }
1501
e905a9ed
YH
1502 /*
1503 * Make a copy of dev_name, because '.procname' is regarded as const
1da177e4
LT
1504 * by sysctl and we wouldn't want anyone to change it under our feet
1505 * (see SIOCSIFNAME).
e905a9ed 1506 */
bfada697
PE
1507 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1508 if (!t->dev_name)
9fa89642 1509 goto free;
1da177e4 1510
bfada697
PE
1511 devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
1512 devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name;
1da177e4 1513
752d14dc 1514 t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
bfada697 1515 t->devinet_vars);
1da177e4 1516 if (!t->sysctl_header)
9fa89642 1517 goto free_procname;
1da177e4
LT
1518
1519 p->sysctl = t;
ea40b324 1520 return 0;
1da177e4 1521
9fa89642 1522free_procname:
bfada697 1523 kfree(t->dev_name);
9fa89642 1524free:
1da177e4 1525 kfree(t);
9fa89642 1526out:
ea40b324 1527 return -ENOBUFS;
1da177e4
LT
1528}
1529
51602b2a
PE
1530static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
1531{
1532 struct devinet_sysctl_table *t = cnf->sysctl;
1533
1534 if (t == NULL)
1535 return;
1536
1537 cnf->sysctl = NULL;
1538 unregister_sysctl_table(t->sysctl_header);
1539 kfree(t->dev_name);
1540 kfree(t);
1541}
1542
66f27a52
PE
1543static void devinet_sysctl_register(struct in_device *idev)
1544{
51602b2a
PE
1545 neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4,
1546 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
c346dca1 1547 __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
c0ce9fb3 1548 idev->dev->ifindex, &idev->cnf);
66f27a52
PE
1549}
1550
51602b2a 1551static void devinet_sysctl_unregister(struct in_device *idev)
1da177e4 1552{
51602b2a
PE
1553 __devinet_sysctl_unregister(&idev->cnf);
1554 neigh_sysctl_unregister(idev->arp_parms);
1da177e4 1555}
1da177e4 1556
68dd299b
PE
1557static struct ctl_table ctl_forward_entry[] = {
1558 {
1559 .ctl_name = NET_IPV4_FORWARD,
1560 .procname = "ip_forward",
1561 .data = &ipv4_devconf.data[
1562 NET_IPV4_CONF_FORWARDING - 1],
1563 .maxlen = sizeof(int),
1564 .mode = 0644,
1565 .proc_handler = devinet_sysctl_forward,
1566 .strategy = devinet_conf_sysctl,
1567 .extra1 = &ipv4_devconf,
c0ce9fb3 1568 .extra2 = &init_net,
68dd299b
PE
1569 },
1570 { },
1571};
1572
752d14dc 1573static __net_initdata struct ctl_path net_ipv4_path[] = {
68dd299b
PE
1574 { .procname = "net", .ctl_name = CTL_NET, },
1575 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1576 { },
1577};
2a75de0c 1578#endif
68dd299b 1579
752d14dc
PE
1580static __net_init int devinet_init_net(struct net *net)
1581{
1582 int err;
752d14dc 1583 struct ipv4_devconf *all, *dflt;
2a75de0c
ED
1584#ifdef CONFIG_SYSCTL
1585 struct ctl_table *tbl = ctl_forward_entry;
752d14dc 1586 struct ctl_table_header *forw_hdr;
2a75de0c 1587#endif
752d14dc
PE
1588
1589 err = -ENOMEM;
1590 all = &ipv4_devconf;
1591 dflt = &ipv4_devconf_dflt;
752d14dc
PE
1592
1593 if (net != &init_net) {
1594 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1595 if (all == NULL)
1596 goto err_alloc_all;
1597
1598 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1599 if (dflt == NULL)
1600 goto err_alloc_dflt;
1601
2a75de0c 1602#ifdef CONFIG_SYSCTL
752d14dc
PE
1603 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1604 if (tbl == NULL)
1605 goto err_alloc_ctl;
1606
1607 tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1];
1608 tbl[0].extra1 = all;
1609 tbl[0].extra2 = net;
2a75de0c 1610#endif
752d14dc
PE
1611 }
1612
1613#ifdef CONFIG_SYSCTL
1614 err = __devinet_sysctl_register(net, "all",
1615 NET_PROTO_CONF_ALL, all);
1616 if (err < 0)
1617 goto err_reg_all;
1618
1619 err = __devinet_sysctl_register(net, "default",
1620 NET_PROTO_CONF_DEFAULT, dflt);
1621 if (err < 0)
1622 goto err_reg_dflt;
1623
1624 err = -ENOMEM;
1625 forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
1626 if (forw_hdr == NULL)
1627 goto err_reg_ctl;
2a75de0c 1628 net->ipv4.forw_hdr = forw_hdr;
752d14dc
PE
1629#endif
1630
752d14dc
PE
1631 net->ipv4.devconf_all = all;
1632 net->ipv4.devconf_dflt = dflt;
1633 return 0;
1634
1635#ifdef CONFIG_SYSCTL
1636err_reg_ctl:
1637 __devinet_sysctl_unregister(dflt);
1638err_reg_dflt:
1639 __devinet_sysctl_unregister(all);
1640err_reg_all:
1641 if (tbl != ctl_forward_entry)
1642 kfree(tbl);
752d14dc 1643err_alloc_ctl:
2a75de0c 1644#endif
752d14dc
PE
1645 if (dflt != &ipv4_devconf_dflt)
1646 kfree(dflt);
1647err_alloc_dflt:
1648 if (all != &ipv4_devconf)
1649 kfree(all);
1650err_alloc_all:
1651 return err;
1652}
1653
1654static __net_exit void devinet_exit_net(struct net *net)
1655{
2a75de0c 1656#ifdef CONFIG_SYSCTL
752d14dc
PE
1657 struct ctl_table *tbl;
1658
1659 tbl = net->ipv4.forw_hdr->ctl_table_arg;
752d14dc
PE
1660 unregister_net_sysctl_table(net->ipv4.forw_hdr);
1661 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1662 __devinet_sysctl_unregister(net->ipv4.devconf_all);
752d14dc 1663 kfree(tbl);
2a75de0c 1664#endif
752d14dc
PE
1665 kfree(net->ipv4.devconf_dflt);
1666 kfree(net->ipv4.devconf_all);
1667}
1668
1669static __net_initdata struct pernet_operations devinet_ops = {
1670 .init = devinet_init_net,
1671 .exit = devinet_exit_net,
1672};
1673
1da177e4
LT
1674void __init devinet_init(void)
1675{
752d14dc
PE
1676 register_pernet_subsys(&devinet_ops);
1677
1da177e4
LT
1678 register_gifconf(PF_INET, inet_gifconf);
1679 register_netdevice_notifier(&ip_netdev_notifier);
63f3444f
TG
1680
1681 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
1682 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
1683 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
1da177e4
LT
1684}
1685