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