2 * NET3 IP device support routines.
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.
9 * Derived from the IP parts of dev.c 1.0.19
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
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
29 #include <linux/uaccess.h>
30 #include <linux/bitops.h>
31 #include <linux/capability.h>
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/string.h>
37 #include <linux/socket.h>
38 #include <linux/sockios.h>
40 #include <linux/errno.h>
41 #include <linux/interrupt.h>
42 #include <linux/if_addr.h>
43 #include <linux/if_ether.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/init.h>
49 #include <linux/notifier.h>
50 #include <linux/inetdevice.h>
51 #include <linux/igmp.h>
52 #include <linux/slab.h>
53 #include <linux/hash.h>
55 #include <linux/sysctl.h>
57 #include <linux/kmod.h>
58 #include <linux/netconf.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64 #include <net/rtnetlink.h>
65 #include <net/net_namespace.h>
66 #include <net/addrconf.h>
68 static struct ipv4_devconf ipv4_devconf
= {
70 [IPV4_DEVCONF_ACCEPT_REDIRECTS
- 1] = 1,
71 [IPV4_DEVCONF_SEND_REDIRECTS
- 1] = 1,
72 [IPV4_DEVCONF_SECURE_REDIRECTS
- 1] = 1,
73 [IPV4_DEVCONF_SHARED_MEDIA
- 1] = 1,
74 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL
- 1] = 10000 /*ms*/,
75 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL
- 1] = 1000 /*ms*/,
79 static struct ipv4_devconf ipv4_devconf_dflt
= {
81 [IPV4_DEVCONF_ACCEPT_REDIRECTS
- 1] = 1,
82 [IPV4_DEVCONF_SEND_REDIRECTS
- 1] = 1,
83 [IPV4_DEVCONF_SECURE_REDIRECTS
- 1] = 1,
84 [IPV4_DEVCONF_SHARED_MEDIA
- 1] = 1,
85 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE
- 1] = 1,
86 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL
- 1] = 10000 /*ms*/,
87 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL
- 1] = 1000 /*ms*/,
91 #define IPV4_DEVCONF_DFLT(net, attr) \
92 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
94 static const struct nla_policy ifa_ipv4_policy
[IFA_MAX
+1] = {
95 [IFA_LOCAL
] = { .type
= NLA_U32
},
96 [IFA_ADDRESS
] = { .type
= NLA_U32
},
97 [IFA_BROADCAST
] = { .type
= NLA_U32
},
98 [IFA_LABEL
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
- 1 },
99 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
100 [IFA_FLAGS
] = { .type
= NLA_U32
},
103 #define IN4_ADDR_HSIZE_SHIFT 8
104 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
106 static struct hlist_head inet_addr_lst
[IN4_ADDR_HSIZE
];
108 static u32
inet_addr_hash(const struct net
*net
, __be32 addr
)
110 u32 val
= (__force u32
) addr
^ net_hash_mix(net
);
112 return hash_32(val
, IN4_ADDR_HSIZE_SHIFT
);
115 static void inet_hash_insert(struct net
*net
, struct in_ifaddr
*ifa
)
117 u32 hash
= inet_addr_hash(net
, ifa
->ifa_local
);
120 hlist_add_head_rcu(&ifa
->hash
, &inet_addr_lst
[hash
]);
123 static void inet_hash_remove(struct in_ifaddr
*ifa
)
126 hlist_del_init_rcu(&ifa
->hash
);
130 * __ip_dev_find - find the first device with a given source address.
131 * @net: the net namespace
132 * @addr: the source address
133 * @devref: if true, take a reference on the found device
135 * If a caller uses devref=false, it should be protected by RCU, or RTNL
137 struct net_device
*__ip_dev_find(struct net
*net
, __be32 addr
, bool devref
)
139 u32 hash
= inet_addr_hash(net
, addr
);
140 struct net_device
*result
= NULL
;
141 struct in_ifaddr
*ifa
;
144 hlist_for_each_entry_rcu(ifa
, &inet_addr_lst
[hash
], hash
) {
145 if (ifa
->ifa_local
== addr
) {
146 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
148 if (!net_eq(dev_net(dev
), net
))
155 struct flowi4 fl4
= { .daddr
= addr
};
156 struct fib_result res
= { 0 };
157 struct fib_table
*local
;
159 /* Fallback to FIB local table so that communication
160 * over loopback subnets work.
162 local
= fib_get_table(net
, RT_TABLE_LOCAL
);
164 !fib_table_lookup(local
, &fl4
, &res
, FIB_LOOKUP_NOREF
) &&
165 res
.type
== RTN_LOCAL
)
166 result
= FIB_RES_DEV(res
);
168 if (result
&& devref
)
173 EXPORT_SYMBOL(__ip_dev_find
);
175 static void rtmsg_ifa(int event
, struct in_ifaddr
*, struct nlmsghdr
*, u32
);
177 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain
);
178 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
181 static int devinet_sysctl_register(struct in_device
*idev
);
182 static void devinet_sysctl_unregister(struct in_device
*idev
);
184 static int devinet_sysctl_register(struct in_device
*idev
)
188 static void devinet_sysctl_unregister(struct in_device
*idev
)
193 /* Locks all the inet devices. */
195 static struct in_ifaddr
*inet_alloc_ifa(void)
197 return kzalloc(sizeof(struct in_ifaddr
), GFP_KERNEL
);
200 static void inet_rcu_free_ifa(struct rcu_head
*head
)
202 struct in_ifaddr
*ifa
= container_of(head
, struct in_ifaddr
, rcu_head
);
204 in_dev_put(ifa
->ifa_dev
);
208 static void inet_free_ifa(struct in_ifaddr
*ifa
)
210 call_rcu(&ifa
->rcu_head
, inet_rcu_free_ifa
);
213 void in_dev_finish_destroy(struct in_device
*idev
)
215 struct net_device
*dev
= idev
->dev
;
217 WARN_ON(idev
->ifa_list
);
218 WARN_ON(idev
->mc_list
);
219 kfree(rcu_dereference_protected(idev
->mc_hash
, 1));
220 #ifdef NET_REFCNT_DEBUG
221 pr_debug("%s: %p=%s\n", __func__
, idev
, dev
? dev
->name
: "NIL");
225 pr_err("Freeing alive in_device %p\n", idev
);
229 EXPORT_SYMBOL(in_dev_finish_destroy
);
231 static struct in_device
*inetdev_init(struct net_device
*dev
)
233 struct in_device
*in_dev
;
238 in_dev
= kzalloc(sizeof(*in_dev
), GFP_KERNEL
);
241 memcpy(&in_dev
->cnf
, dev_net(dev
)->ipv4
.devconf_dflt
,
242 sizeof(in_dev
->cnf
));
243 in_dev
->cnf
.sysctl
= NULL
;
245 in_dev
->arp_parms
= neigh_parms_alloc(dev
, &arp_tbl
);
246 if (!in_dev
->arp_parms
)
248 if (IPV4_DEVCONF(in_dev
->cnf
, FORWARDING
))
249 dev_disable_lro(dev
);
250 /* Reference in_dev->dev */
252 /* Account for reference dev->ip_ptr (below) */
255 err
= devinet_sysctl_register(in_dev
);
262 ip_mc_init_dev(in_dev
);
263 if (dev
->flags
& IFF_UP
)
266 /* we can receive as soon as ip_ptr is set -- do this last */
267 rcu_assign_pointer(dev
->ip_ptr
, in_dev
);
269 return in_dev
?: ERR_PTR(err
);
276 static void in_dev_rcu_put(struct rcu_head
*head
)
278 struct in_device
*idev
= container_of(head
, struct in_device
, rcu_head
);
282 static void inetdev_destroy(struct in_device
*in_dev
)
284 struct in_ifaddr
*ifa
;
285 struct net_device
*dev
;
293 ip_mc_destroy_dev(in_dev
);
295 while ((ifa
= in_dev
->ifa_list
) != NULL
) {
296 inet_del_ifa(in_dev
, &in_dev
->ifa_list
, 0);
300 RCU_INIT_POINTER(dev
->ip_ptr
, NULL
);
302 devinet_sysctl_unregister(in_dev
);
303 neigh_parms_release(&arp_tbl
, in_dev
->arp_parms
);
306 call_rcu(&in_dev
->rcu_head
, in_dev_rcu_put
);
309 int inet_addr_onlink(struct in_device
*in_dev
, __be32 a
, __be32 b
)
312 for_primary_ifa(in_dev
) {
313 if (inet_ifa_match(a
, ifa
)) {
314 if (!b
|| inet_ifa_match(b
, ifa
)) {
319 } endfor_ifa(in_dev
);
324 static void __inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
325 int destroy
, struct nlmsghdr
*nlh
, u32 portid
)
327 struct in_ifaddr
*promote
= NULL
;
328 struct in_ifaddr
*ifa
, *ifa1
= *ifap
;
329 struct in_ifaddr
*last_prim
= in_dev
->ifa_list
;
330 struct in_ifaddr
*prev_prom
= NULL
;
331 int do_promote
= IN_DEV_PROMOTE_SECONDARIES(in_dev
);
338 /* 1. Deleting primary ifaddr forces deletion all secondaries
339 * unless alias promotion is set
342 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
)) {
343 struct in_ifaddr
**ifap1
= &ifa1
->ifa_next
;
345 while ((ifa
= *ifap1
) != NULL
) {
346 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
) &&
347 ifa1
->ifa_scope
<= ifa
->ifa_scope
)
350 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
) ||
351 ifa1
->ifa_mask
!= ifa
->ifa_mask
||
352 !inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
353 ifap1
= &ifa
->ifa_next
;
359 inet_hash_remove(ifa
);
360 *ifap1
= ifa
->ifa_next
;
362 rtmsg_ifa(RTM_DELADDR
, ifa
, nlh
, portid
);
363 blocking_notifier_call_chain(&inetaddr_chain
,
373 /* On promotion all secondaries from subnet are changing
374 * the primary IP, we must remove all their routes silently
375 * and later to add them back with new prefsrc. Do this
376 * while all addresses are on the device list.
378 for (ifa
= promote
; ifa
; ifa
= ifa
->ifa_next
) {
379 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
380 inet_ifa_match(ifa1
->ifa_address
, ifa
))
381 fib_del_ifaddr(ifa
, ifa1
);
387 *ifap
= ifa1
->ifa_next
;
388 inet_hash_remove(ifa1
);
390 /* 3. Announce address deletion */
392 /* Send message first, then call notifier.
393 At first sight, FIB update triggered by notifier
394 will refer to already deleted ifaddr, that could confuse
395 netlink listeners. It is not true: look, gated sees
396 that route deleted and if it still thinks that ifaddr
397 is valid, it will try to restore deleted routes... Grr.
398 So that, this order is correct.
400 rtmsg_ifa(RTM_DELADDR
, ifa1
, nlh
, portid
);
401 blocking_notifier_call_chain(&inetaddr_chain
, NETDEV_DOWN
, ifa1
);
404 struct in_ifaddr
*next_sec
= promote
->ifa_next
;
407 prev_prom
->ifa_next
= promote
->ifa_next
;
408 promote
->ifa_next
= last_prim
->ifa_next
;
409 last_prim
->ifa_next
= promote
;
412 promote
->ifa_flags
&= ~IFA_F_SECONDARY
;
413 rtmsg_ifa(RTM_NEWADDR
, promote
, nlh
, portid
);
414 blocking_notifier_call_chain(&inetaddr_chain
,
416 for (ifa
= next_sec
; ifa
; ifa
= ifa
->ifa_next
) {
417 if (ifa1
->ifa_mask
!= ifa
->ifa_mask
||
418 !inet_ifa_match(ifa1
->ifa_address
, ifa
))
428 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
431 __inet_del_ifa(in_dev
, ifap
, destroy
, NULL
, 0);
434 static void check_lifetime(struct work_struct
*work
);
436 static DECLARE_DELAYED_WORK(check_lifetime_work
, check_lifetime
);
438 static int __inet_insert_ifa(struct in_ifaddr
*ifa
, struct nlmsghdr
*nlh
,
441 struct in_device
*in_dev
= ifa
->ifa_dev
;
442 struct in_ifaddr
*ifa1
, **ifap
, **last_primary
;
446 if (!ifa
->ifa_local
) {
451 ifa
->ifa_flags
&= ~IFA_F_SECONDARY
;
452 last_primary
= &in_dev
->ifa_list
;
454 for (ifap
= &in_dev
->ifa_list
; (ifa1
= *ifap
) != NULL
;
455 ifap
= &ifa1
->ifa_next
) {
456 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
) &&
457 ifa
->ifa_scope
<= ifa1
->ifa_scope
)
458 last_primary
= &ifa1
->ifa_next
;
459 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
460 inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
461 if (ifa1
->ifa_local
== ifa
->ifa_local
) {
465 if (ifa1
->ifa_scope
!= ifa
->ifa_scope
) {
469 ifa
->ifa_flags
|= IFA_F_SECONDARY
;
473 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
)) {
474 prandom_seed((__force u32
) ifa
->ifa_local
);
478 ifa
->ifa_next
= *ifap
;
481 inet_hash_insert(dev_net(in_dev
->dev
), ifa
);
483 cancel_delayed_work(&check_lifetime_work
);
484 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
, 0);
486 /* Send message first, then call notifier.
487 Notifier will trigger FIB update, so that
488 listeners of netlink will know about new ifaddr */
489 rtmsg_ifa(RTM_NEWADDR
, ifa
, nlh
, portid
);
490 blocking_notifier_call_chain(&inetaddr_chain
, NETDEV_UP
, ifa
);
495 static int inet_insert_ifa(struct in_ifaddr
*ifa
)
497 return __inet_insert_ifa(ifa
, NULL
, 0);
500 static int inet_set_ifa(struct net_device
*dev
, struct in_ifaddr
*ifa
)
502 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
510 ipv4_devconf_setall(in_dev
);
511 neigh_parms_data_state_setall(in_dev
->arp_parms
);
512 if (ifa
->ifa_dev
!= in_dev
) {
513 WARN_ON(ifa
->ifa_dev
);
515 ifa
->ifa_dev
= in_dev
;
517 if (ipv4_is_loopback(ifa
->ifa_local
))
518 ifa
->ifa_scope
= RT_SCOPE_HOST
;
519 return inet_insert_ifa(ifa
);
522 /* Caller must hold RCU or RTNL :
523 * We dont take a reference on found in_device
525 struct in_device
*inetdev_by_index(struct net
*net
, int ifindex
)
527 struct net_device
*dev
;
528 struct in_device
*in_dev
= NULL
;
531 dev
= dev_get_by_index_rcu(net
, ifindex
);
533 in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
537 EXPORT_SYMBOL(inetdev_by_index
);
539 /* Called only from RTNL semaphored context. No locks. */
541 struct in_ifaddr
*inet_ifa_byprefix(struct in_device
*in_dev
, __be32 prefix
,
546 for_primary_ifa(in_dev
) {
547 if (ifa
->ifa_mask
== mask
&& inet_ifa_match(prefix
, ifa
))
549 } endfor_ifa(in_dev
);
553 static int ip_mc_config(struct sock
*sk
, bool join
, const struct in_ifaddr
*ifa
)
555 struct ip_mreqn mreq
= {
556 .imr_multiaddr
.s_addr
= ifa
->ifa_address
,
557 .imr_ifindex
= ifa
->ifa_dev
->dev
->ifindex
,
565 ret
= ip_mc_join_group(sk
, &mreq
);
567 ret
= ip_mc_leave_group(sk
, &mreq
);
573 static int inet_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
575 struct net
*net
= sock_net(skb
->sk
);
576 struct nlattr
*tb
[IFA_MAX
+1];
577 struct in_device
*in_dev
;
578 struct ifaddrmsg
*ifm
;
579 struct in_ifaddr
*ifa
, **ifap
;
584 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv4_policy
);
588 ifm
= nlmsg_data(nlh
);
589 in_dev
= inetdev_by_index(net
, ifm
->ifa_index
);
595 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
596 ifap
= &ifa
->ifa_next
) {
598 ifa
->ifa_local
!= nla_get_in_addr(tb
[IFA_LOCAL
]))
601 if (tb
[IFA_LABEL
] && nla_strcmp(tb
[IFA_LABEL
], ifa
->ifa_label
))
604 if (tb
[IFA_ADDRESS
] &&
605 (ifm
->ifa_prefixlen
!= ifa
->ifa_prefixlen
||
606 !inet_ifa_match(nla_get_in_addr(tb
[IFA_ADDRESS
]), ifa
)))
609 if (ipv4_is_multicast(ifa
->ifa_address
))
610 ip_mc_config(net
->ipv4
.mc_autojoin_sk
, false, ifa
);
611 __inet_del_ifa(in_dev
, ifap
, 1, nlh
, NETLINK_CB(skb
).portid
);
615 err
= -EADDRNOTAVAIL
;
620 #define INFINITY_LIFE_TIME 0xFFFFFFFF
622 static void check_lifetime(struct work_struct
*work
)
624 unsigned long now
, next
, next_sec
, next_sched
;
625 struct in_ifaddr
*ifa
;
626 struct hlist_node
*n
;
630 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
632 for (i
= 0; i
< IN4_ADDR_HSIZE
; i
++) {
633 bool change_needed
= false;
636 hlist_for_each_entry_rcu(ifa
, &inet_addr_lst
[i
], hash
) {
639 if (ifa
->ifa_flags
& IFA_F_PERMANENT
)
642 /* We try to batch several events at once. */
643 age
= (now
- ifa
->ifa_tstamp
+
644 ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
646 if (ifa
->ifa_valid_lft
!= INFINITY_LIFE_TIME
&&
647 age
>= ifa
->ifa_valid_lft
) {
648 change_needed
= true;
649 } else if (ifa
->ifa_preferred_lft
==
650 INFINITY_LIFE_TIME
) {
652 } else if (age
>= ifa
->ifa_preferred_lft
) {
653 if (time_before(ifa
->ifa_tstamp
+
654 ifa
->ifa_valid_lft
* HZ
, next
))
655 next
= ifa
->ifa_tstamp
+
656 ifa
->ifa_valid_lft
* HZ
;
658 if (!(ifa
->ifa_flags
& IFA_F_DEPRECATED
))
659 change_needed
= true;
660 } else if (time_before(ifa
->ifa_tstamp
+
661 ifa
->ifa_preferred_lft
* HZ
,
663 next
= ifa
->ifa_tstamp
+
664 ifa
->ifa_preferred_lft
* HZ
;
671 hlist_for_each_entry_safe(ifa
, n
, &inet_addr_lst
[i
], hash
) {
674 if (ifa
->ifa_flags
& IFA_F_PERMANENT
)
677 /* We try to batch several events at once. */
678 age
= (now
- ifa
->ifa_tstamp
+
679 ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
681 if (ifa
->ifa_valid_lft
!= INFINITY_LIFE_TIME
&&
682 age
>= ifa
->ifa_valid_lft
) {
683 struct in_ifaddr
**ifap
;
685 for (ifap
= &ifa
->ifa_dev
->ifa_list
;
686 *ifap
!= NULL
; ifap
= &(*ifap
)->ifa_next
) {
688 inet_del_ifa(ifa
->ifa_dev
,
693 } else if (ifa
->ifa_preferred_lft
!=
694 INFINITY_LIFE_TIME
&&
695 age
>= ifa
->ifa_preferred_lft
&&
696 !(ifa
->ifa_flags
& IFA_F_DEPRECATED
)) {
697 ifa
->ifa_flags
|= IFA_F_DEPRECATED
;
698 rtmsg_ifa(RTM_NEWADDR
, ifa
, NULL
, 0);
704 next_sec
= round_jiffies_up(next
);
707 /* If rounded timeout is accurate enough, accept it. */
708 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
709 next_sched
= next_sec
;
712 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
713 if (time_before(next_sched
, now
+ ADDRCONF_TIMER_FUZZ_MAX
))
714 next_sched
= now
+ ADDRCONF_TIMER_FUZZ_MAX
;
716 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
,
720 static void set_ifa_lifetime(struct in_ifaddr
*ifa
, __u32 valid_lft
,
723 unsigned long timeout
;
725 ifa
->ifa_flags
&= ~(IFA_F_PERMANENT
| IFA_F_DEPRECATED
);
727 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
728 if (addrconf_finite_timeout(timeout
))
729 ifa
->ifa_valid_lft
= timeout
;
731 ifa
->ifa_flags
|= IFA_F_PERMANENT
;
733 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
734 if (addrconf_finite_timeout(timeout
)) {
736 ifa
->ifa_flags
|= IFA_F_DEPRECATED
;
737 ifa
->ifa_preferred_lft
= timeout
;
739 ifa
->ifa_tstamp
= jiffies
;
740 if (!ifa
->ifa_cstamp
)
741 ifa
->ifa_cstamp
= ifa
->ifa_tstamp
;
744 static struct in_ifaddr
*rtm_to_ifaddr(struct net
*net
, struct nlmsghdr
*nlh
,
745 __u32
*pvalid_lft
, __u32
*pprefered_lft
)
747 struct nlattr
*tb
[IFA_MAX
+1];
748 struct in_ifaddr
*ifa
;
749 struct ifaddrmsg
*ifm
;
750 struct net_device
*dev
;
751 struct in_device
*in_dev
;
754 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv4_policy
);
758 ifm
= nlmsg_data(nlh
);
760 if (ifm
->ifa_prefixlen
> 32 || !tb
[IFA_LOCAL
])
763 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
768 in_dev
= __in_dev_get_rtnl(dev
);
773 ifa
= inet_alloc_ifa();
776 * A potential indev allocation can be left alive, it stays
777 * assigned to its device and is destroy with it.
781 ipv4_devconf_setall(in_dev
);
782 neigh_parms_data_state_setall(in_dev
->arp_parms
);
785 if (!tb
[IFA_ADDRESS
])
786 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
788 INIT_HLIST_NODE(&ifa
->hash
);
789 ifa
->ifa_prefixlen
= ifm
->ifa_prefixlen
;
790 ifa
->ifa_mask
= inet_make_mask(ifm
->ifa_prefixlen
);
791 ifa
->ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) :
793 ifa
->ifa_scope
= ifm
->ifa_scope
;
794 ifa
->ifa_dev
= in_dev
;
796 ifa
->ifa_local
= nla_get_in_addr(tb
[IFA_LOCAL
]);
797 ifa
->ifa_address
= nla_get_in_addr(tb
[IFA_ADDRESS
]);
799 if (tb
[IFA_BROADCAST
])
800 ifa
->ifa_broadcast
= nla_get_in_addr(tb
[IFA_BROADCAST
]);
803 nla_strlcpy(ifa
->ifa_label
, tb
[IFA_LABEL
], IFNAMSIZ
);
805 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
807 if (tb
[IFA_CACHEINFO
]) {
808 struct ifa_cacheinfo
*ci
;
810 ci
= nla_data(tb
[IFA_CACHEINFO
]);
811 if (!ci
->ifa_valid
|| ci
->ifa_prefered
> ci
->ifa_valid
) {
815 *pvalid_lft
= ci
->ifa_valid
;
816 *pprefered_lft
= ci
->ifa_prefered
;
827 static struct in_ifaddr
*find_matching_ifa(struct in_ifaddr
*ifa
)
829 struct in_device
*in_dev
= ifa
->ifa_dev
;
830 struct in_ifaddr
*ifa1
, **ifap
;
835 for (ifap
= &in_dev
->ifa_list
; (ifa1
= *ifap
) != NULL
;
836 ifap
= &ifa1
->ifa_next
) {
837 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
838 inet_ifa_match(ifa1
->ifa_address
, ifa
) &&
839 ifa1
->ifa_local
== ifa
->ifa_local
)
845 static int inet_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
847 struct net
*net
= sock_net(skb
->sk
);
848 struct in_ifaddr
*ifa
;
849 struct in_ifaddr
*ifa_existing
;
850 __u32 valid_lft
= INFINITY_LIFE_TIME
;
851 __u32 prefered_lft
= INFINITY_LIFE_TIME
;
855 ifa
= rtm_to_ifaddr(net
, nlh
, &valid_lft
, &prefered_lft
);
859 ifa_existing
= find_matching_ifa(ifa
);
861 /* It would be best to check for !NLM_F_CREATE here but
862 * userspace already relies on not having to provide this.
864 set_ifa_lifetime(ifa
, valid_lft
, prefered_lft
);
865 if (ifa
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
866 int ret
= ip_mc_config(net
->ipv4
.mc_autojoin_sk
,
874 return __inet_insert_ifa(ifa
, nlh
, NETLINK_CB(skb
).portid
);
878 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
879 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
882 set_ifa_lifetime(ifa
, valid_lft
, prefered_lft
);
883 cancel_delayed_work(&check_lifetime_work
);
884 queue_delayed_work(system_power_efficient_wq
,
885 &check_lifetime_work
, 0);
886 rtmsg_ifa(RTM_NEWADDR
, ifa
, nlh
, NETLINK_CB(skb
).portid
);
892 * Determine a default network mask, based on the IP address.
895 static int inet_abc_len(__be32 addr
)
897 int rc
= -1; /* Something else, probably a multicast. */
899 if (ipv4_is_zeronet(addr
))
902 __u32 haddr
= ntohl(addr
);
904 if (IN_CLASSA(haddr
))
906 else if (IN_CLASSB(haddr
))
908 else if (IN_CLASSC(haddr
))
916 int devinet_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
919 struct sockaddr_in sin_orig
;
920 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
921 struct in_device
*in_dev
;
922 struct in_ifaddr
**ifap
= NULL
;
923 struct in_ifaddr
*ifa
= NULL
;
924 struct net_device
*dev
;
927 int tryaddrmatch
= 0;
930 * Fetch the caller's info block into kernel space
933 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
935 ifr
.ifr_name
[IFNAMSIZ
- 1] = 0;
937 /* save original address for comparison */
938 memcpy(&sin_orig
, sin
, sizeof(*sin
));
940 colon
= strchr(ifr
.ifr_name
, ':');
944 dev_load(net
, ifr
.ifr_name
);
947 case SIOCGIFADDR
: /* Get interface address */
948 case SIOCGIFBRDADDR
: /* Get the broadcast address */
949 case SIOCGIFDSTADDR
: /* Get the destination address */
950 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
951 /* Note that these ioctls will not sleep,
952 so that we do not impose a lock.
953 One day we will be forced to put shlock here (I mean SMP)
955 tryaddrmatch
= (sin_orig
.sin_family
== AF_INET
);
956 memset(sin
, 0, sizeof(*sin
));
957 sin
->sin_family
= AF_INET
;
962 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
965 case SIOCSIFADDR
: /* Set interface address (and family) */
966 case SIOCSIFBRDADDR
: /* Set the broadcast address */
967 case SIOCSIFDSTADDR
: /* Set the destination address */
968 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
970 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
973 if (sin
->sin_family
!= AF_INET
)
984 dev
= __dev_get_by_name(net
, ifr
.ifr_name
);
991 in_dev
= __in_dev_get_rtnl(dev
);
994 /* Matthias Andree */
995 /* compare label and address (4.4BSD style) */
996 /* note: we only do this for a limited set of ioctls
997 and only if the original address family was AF_INET.
998 This is checked above. */
999 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
1000 ifap
= &ifa
->ifa_next
) {
1001 if (!strcmp(ifr
.ifr_name
, ifa
->ifa_label
) &&
1002 sin_orig
.sin_addr
.s_addr
==
1008 /* we didn't get a match, maybe the application is
1009 4.3BSD-style and passed in junk so we fall back to
1010 comparing just the label */
1012 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
1013 ifap
= &ifa
->ifa_next
)
1014 if (!strcmp(ifr
.ifr_name
, ifa
->ifa_label
))
1019 ret
= -EADDRNOTAVAIL
;
1020 if (!ifa
&& cmd
!= SIOCSIFADDR
&& cmd
!= SIOCSIFFLAGS
)
1024 case SIOCGIFADDR
: /* Get interface address */
1025 sin
->sin_addr
.s_addr
= ifa
->ifa_local
;
1028 case SIOCGIFBRDADDR
: /* Get the broadcast address */
1029 sin
->sin_addr
.s_addr
= ifa
->ifa_broadcast
;
1032 case SIOCGIFDSTADDR
: /* Get the destination address */
1033 sin
->sin_addr
.s_addr
= ifa
->ifa_address
;
1036 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
1037 sin
->sin_addr
.s_addr
= ifa
->ifa_mask
;
1042 ret
= -EADDRNOTAVAIL
;
1046 if (!(ifr
.ifr_flags
& IFF_UP
))
1047 inet_del_ifa(in_dev
, ifap
, 1);
1050 ret
= dev_change_flags(dev
, ifr
.ifr_flags
);
1053 case SIOCSIFADDR
: /* Set interface address (and family) */
1055 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
1060 ifa
= inet_alloc_ifa();
1063 INIT_HLIST_NODE(&ifa
->hash
);
1065 memcpy(ifa
->ifa_label
, ifr
.ifr_name
, IFNAMSIZ
);
1067 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1070 if (ifa
->ifa_local
== sin
->sin_addr
.s_addr
)
1072 inet_del_ifa(in_dev
, ifap
, 0);
1073 ifa
->ifa_broadcast
= 0;
1077 ifa
->ifa_address
= ifa
->ifa_local
= sin
->sin_addr
.s_addr
;
1079 if (!(dev
->flags
& IFF_POINTOPOINT
)) {
1080 ifa
->ifa_prefixlen
= inet_abc_len(ifa
->ifa_address
);
1081 ifa
->ifa_mask
= inet_make_mask(ifa
->ifa_prefixlen
);
1082 if ((dev
->flags
& IFF_BROADCAST
) &&
1083 ifa
->ifa_prefixlen
< 31)
1084 ifa
->ifa_broadcast
= ifa
->ifa_address
|
1087 ifa
->ifa_prefixlen
= 32;
1088 ifa
->ifa_mask
= inet_make_mask(32);
1090 set_ifa_lifetime(ifa
, INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
1091 ret
= inet_set_ifa(dev
, ifa
);
1094 case SIOCSIFBRDADDR
: /* Set the broadcast address */
1096 if (ifa
->ifa_broadcast
!= sin
->sin_addr
.s_addr
) {
1097 inet_del_ifa(in_dev
, ifap
, 0);
1098 ifa
->ifa_broadcast
= sin
->sin_addr
.s_addr
;
1099 inet_insert_ifa(ifa
);
1103 case SIOCSIFDSTADDR
: /* Set the destination address */
1105 if (ifa
->ifa_address
== sin
->sin_addr
.s_addr
)
1108 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
1111 inet_del_ifa(in_dev
, ifap
, 0);
1112 ifa
->ifa_address
= sin
->sin_addr
.s_addr
;
1113 inet_insert_ifa(ifa
);
1116 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
1119 * The mask we set must be legal.
1122 if (bad_mask(sin
->sin_addr
.s_addr
, 0))
1125 if (ifa
->ifa_mask
!= sin
->sin_addr
.s_addr
) {
1126 __be32 old_mask
= ifa
->ifa_mask
;
1127 inet_del_ifa(in_dev
, ifap
, 0);
1128 ifa
->ifa_mask
= sin
->sin_addr
.s_addr
;
1129 ifa
->ifa_prefixlen
= inet_mask_len(ifa
->ifa_mask
);
1131 /* See if current broadcast address matches
1132 * with current netmask, then recalculate
1133 * the broadcast address. Otherwise it's a
1134 * funny address, so don't touch it since
1135 * the user seems to know what (s)he's doing...
1137 if ((dev
->flags
& IFF_BROADCAST
) &&
1138 (ifa
->ifa_prefixlen
< 31) &&
1139 (ifa
->ifa_broadcast
==
1140 (ifa
->ifa_local
|~old_mask
))) {
1141 ifa
->ifa_broadcast
= (ifa
->ifa_local
|
1142 ~sin
->sin_addr
.s_addr
);
1144 inet_insert_ifa(ifa
);
1154 ret
= copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)) ? -EFAULT
: 0;
1158 static int inet_gifconf(struct net_device
*dev
, char __user
*buf
, int len
)
1160 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
1161 struct in_ifaddr
*ifa
;
1168 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1170 done
+= sizeof(ifr
);
1173 if (len
< (int) sizeof(ifr
))
1175 memset(&ifr
, 0, sizeof(struct ifreq
));
1176 strcpy(ifr
.ifr_name
, ifa
->ifa_label
);
1178 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_family
= AF_INET
;
1179 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_addr
.s_addr
=
1182 if (copy_to_user(buf
, &ifr
, sizeof(struct ifreq
))) {
1186 buf
+= sizeof(struct ifreq
);
1187 len
-= sizeof(struct ifreq
);
1188 done
+= sizeof(struct ifreq
);
1194 __be32
inet_select_addr(const struct net_device
*dev
, __be32 dst
, int scope
)
1197 struct in_device
*in_dev
;
1198 struct net
*net
= dev_net(dev
);
1202 in_dev
= __in_dev_get_rcu(dev
);
1206 for_primary_ifa(in_dev
) {
1207 if (ifa
->ifa_scope
> scope
)
1209 if (!dst
|| inet_ifa_match(dst
, ifa
)) {
1210 addr
= ifa
->ifa_local
;
1214 addr
= ifa
->ifa_local
;
1215 } endfor_ifa(in_dev
);
1220 master_idx
= l3mdev_master_ifindex_rcu(dev
);
1222 /* For VRFs, the VRF device takes the place of the loopback device,
1223 * with addresses on it being preferred. Note in such cases the
1224 * loopback device will be among the devices that fail the master_idx
1225 * equality check in the loop below.
1228 (dev
= dev_get_by_index_rcu(net
, master_idx
)) &&
1229 (in_dev
= __in_dev_get_rcu(dev
))) {
1230 for_primary_ifa(in_dev
) {
1231 if (ifa
->ifa_scope
!= RT_SCOPE_LINK
&&
1232 ifa
->ifa_scope
<= scope
) {
1233 addr
= ifa
->ifa_local
;
1236 } endfor_ifa(in_dev
);
1239 /* Not loopback addresses on loopback should be preferred
1240 in this case. It is important that lo is the first interface
1243 for_each_netdev_rcu(net
, dev
) {
1244 if (l3mdev_master_ifindex_rcu(dev
) != master_idx
)
1247 in_dev
= __in_dev_get_rcu(dev
);
1251 for_primary_ifa(in_dev
) {
1252 if (ifa
->ifa_scope
!= RT_SCOPE_LINK
&&
1253 ifa
->ifa_scope
<= scope
) {
1254 addr
= ifa
->ifa_local
;
1257 } endfor_ifa(in_dev
);
1263 EXPORT_SYMBOL(inet_select_addr
);
1265 static __be32
confirm_addr_indev(struct in_device
*in_dev
, __be32 dst
,
1266 __be32 local
, int scope
)
1273 (local
== ifa
->ifa_local
|| !local
) &&
1274 ifa
->ifa_scope
<= scope
) {
1275 addr
= ifa
->ifa_local
;
1280 same
= (!local
|| inet_ifa_match(local
, ifa
)) &&
1281 (!dst
|| inet_ifa_match(dst
, ifa
));
1285 /* Is the selected addr into dst subnet? */
1286 if (inet_ifa_match(addr
, ifa
))
1288 /* No, then can we use new local src? */
1289 if (ifa
->ifa_scope
<= scope
) {
1290 addr
= ifa
->ifa_local
;
1293 /* search for large dst subnet for addr */
1297 } endfor_ifa(in_dev
);
1299 return same
? addr
: 0;
1303 * Confirm that local IP address exists using wildcards:
1304 * - net: netns to check, cannot be NULL
1305 * - in_dev: only on this interface, NULL=any interface
1306 * - dst: only in the same subnet as dst, 0=any dst
1307 * - local: address, 0=autoselect the local address
1308 * - scope: maximum allowed scope value for the local address
1310 __be32
inet_confirm_addr(struct net
*net
, struct in_device
*in_dev
,
1311 __be32 dst
, __be32 local
, int scope
)
1314 struct net_device
*dev
;
1317 return confirm_addr_indev(in_dev
, dst
, local
, scope
);
1320 for_each_netdev_rcu(net
, dev
) {
1321 in_dev
= __in_dev_get_rcu(dev
);
1323 addr
= confirm_addr_indev(in_dev
, dst
, local
, scope
);
1332 EXPORT_SYMBOL(inet_confirm_addr
);
1338 int register_inetaddr_notifier(struct notifier_block
*nb
)
1340 return blocking_notifier_chain_register(&inetaddr_chain
, nb
);
1342 EXPORT_SYMBOL(register_inetaddr_notifier
);
1344 int unregister_inetaddr_notifier(struct notifier_block
*nb
)
1346 return blocking_notifier_chain_unregister(&inetaddr_chain
, nb
);
1348 EXPORT_SYMBOL(unregister_inetaddr_notifier
);
1350 /* Rename ifa_labels for a device name change. Make some effort to preserve
1351 * existing alias numbering and to create unique labels if possible.
1353 static void inetdev_changename(struct net_device
*dev
, struct in_device
*in_dev
)
1355 struct in_ifaddr
*ifa
;
1358 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1359 char old
[IFNAMSIZ
], *dot
;
1361 memcpy(old
, ifa
->ifa_label
, IFNAMSIZ
);
1362 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1365 dot
= strchr(old
, ':');
1367 sprintf(old
, ":%d", named
);
1370 if (strlen(dot
) + strlen(dev
->name
) < IFNAMSIZ
)
1371 strcat(ifa
->ifa_label
, dot
);
1373 strcpy(ifa
->ifa_label
+ (IFNAMSIZ
- strlen(dot
) - 1), dot
);
1375 rtmsg_ifa(RTM_NEWADDR
, ifa
, NULL
, 0);
1379 static bool inetdev_valid_mtu(unsigned int mtu
)
1384 static void inetdev_send_gratuitous_arp(struct net_device
*dev
,
1385 struct in_device
*in_dev
)
1388 struct in_ifaddr
*ifa
;
1390 for (ifa
= in_dev
->ifa_list
; ifa
;
1391 ifa
= ifa
->ifa_next
) {
1392 arp_send(ARPOP_REQUEST
, ETH_P_ARP
,
1393 ifa
->ifa_local
, dev
,
1394 ifa
->ifa_local
, NULL
,
1395 dev
->dev_addr
, NULL
);
1399 /* Called only under RTNL semaphore */
1401 static int inetdev_event(struct notifier_block
*this, unsigned long event
,
1404 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1405 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
1410 if (event
== NETDEV_REGISTER
) {
1411 in_dev
= inetdev_init(dev
);
1413 return notifier_from_errno(PTR_ERR(in_dev
));
1414 if (dev
->flags
& IFF_LOOPBACK
) {
1415 IN_DEV_CONF_SET(in_dev
, NOXFRM
, 1);
1416 IN_DEV_CONF_SET(in_dev
, NOPOLICY
, 1);
1418 } else if (event
== NETDEV_CHANGEMTU
) {
1419 /* Re-enabling IP */
1420 if (inetdev_valid_mtu(dev
->mtu
))
1421 in_dev
= inetdev_init(dev
);
1427 case NETDEV_REGISTER
:
1428 pr_debug("%s: bug\n", __func__
);
1429 RCU_INIT_POINTER(dev
->ip_ptr
, NULL
);
1432 if (!inetdev_valid_mtu(dev
->mtu
))
1434 if (dev
->flags
& IFF_LOOPBACK
) {
1435 struct in_ifaddr
*ifa
= inet_alloc_ifa();
1438 INIT_HLIST_NODE(&ifa
->hash
);
1440 ifa
->ifa_address
= htonl(INADDR_LOOPBACK
);
1441 ifa
->ifa_prefixlen
= 8;
1442 ifa
->ifa_mask
= inet_make_mask(8);
1443 in_dev_hold(in_dev
);
1444 ifa
->ifa_dev
= in_dev
;
1445 ifa
->ifa_scope
= RT_SCOPE_HOST
;
1446 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1447 set_ifa_lifetime(ifa
, INFINITY_LIFE_TIME
,
1448 INFINITY_LIFE_TIME
);
1449 ipv4_devconf_setall(in_dev
);
1450 neigh_parms_data_state_setall(in_dev
->arp_parms
);
1451 inet_insert_ifa(ifa
);
1456 case NETDEV_CHANGEADDR
:
1457 if (!IN_DEV_ARP_NOTIFY(in_dev
))
1460 case NETDEV_NOTIFY_PEERS
:
1461 /* Send gratuitous ARP to notify of link change */
1462 inetdev_send_gratuitous_arp(dev
, in_dev
);
1467 case NETDEV_PRE_TYPE_CHANGE
:
1468 ip_mc_unmap(in_dev
);
1470 case NETDEV_POST_TYPE_CHANGE
:
1471 ip_mc_remap(in_dev
);
1473 case NETDEV_CHANGEMTU
:
1474 if (inetdev_valid_mtu(dev
->mtu
))
1476 /* disable IP when MTU is not enough */
1477 case NETDEV_UNREGISTER
:
1478 inetdev_destroy(in_dev
);
1480 case NETDEV_CHANGENAME
:
1481 /* Do not notify about label change, this event is
1482 * not interesting to applications using netlink.
1484 inetdev_changename(dev
, in_dev
);
1486 devinet_sysctl_unregister(in_dev
);
1487 devinet_sysctl_register(in_dev
);
1494 static struct notifier_block ip_netdev_notifier
= {
1495 .notifier_call
= inetdev_event
,
1498 static size_t inet_nlmsg_size(void)
1500 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
1501 + nla_total_size(4) /* IFA_ADDRESS */
1502 + nla_total_size(4) /* IFA_LOCAL */
1503 + nla_total_size(4) /* IFA_BROADCAST */
1504 + nla_total_size(IFNAMSIZ
) /* IFA_LABEL */
1505 + nla_total_size(4) /* IFA_FLAGS */
1506 + nla_total_size(sizeof(struct ifa_cacheinfo
)); /* IFA_CACHEINFO */
1509 static inline u32
cstamp_delta(unsigned long cstamp
)
1511 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
1514 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
1515 unsigned long tstamp
, u32 preferred
, u32 valid
)
1517 struct ifa_cacheinfo ci
;
1519 ci
.cstamp
= cstamp_delta(cstamp
);
1520 ci
.tstamp
= cstamp_delta(tstamp
);
1521 ci
.ifa_prefered
= preferred
;
1522 ci
.ifa_valid
= valid
;
1524 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
1527 static int inet_fill_ifaddr(struct sk_buff
*skb
, struct in_ifaddr
*ifa
,
1528 u32 portid
, u32 seq
, int event
, unsigned int flags
)
1530 struct ifaddrmsg
*ifm
;
1531 struct nlmsghdr
*nlh
;
1532 u32 preferred
, valid
;
1534 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*ifm
), flags
);
1538 ifm
= nlmsg_data(nlh
);
1539 ifm
->ifa_family
= AF_INET
;
1540 ifm
->ifa_prefixlen
= ifa
->ifa_prefixlen
;
1541 ifm
->ifa_flags
= ifa
->ifa_flags
;
1542 ifm
->ifa_scope
= ifa
->ifa_scope
;
1543 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
1545 if (!(ifm
->ifa_flags
& IFA_F_PERMANENT
)) {
1546 preferred
= ifa
->ifa_preferred_lft
;
1547 valid
= ifa
->ifa_valid_lft
;
1548 if (preferred
!= INFINITY_LIFE_TIME
) {
1549 long tval
= (jiffies
- ifa
->ifa_tstamp
) / HZ
;
1551 if (preferred
> tval
)
1555 if (valid
!= INFINITY_LIFE_TIME
) {
1563 preferred
= INFINITY_LIFE_TIME
;
1564 valid
= INFINITY_LIFE_TIME
;
1566 if ((ifa
->ifa_address
&&
1567 nla_put_in_addr(skb
, IFA_ADDRESS
, ifa
->ifa_address
)) ||
1569 nla_put_in_addr(skb
, IFA_LOCAL
, ifa
->ifa_local
)) ||
1570 (ifa
->ifa_broadcast
&&
1571 nla_put_in_addr(skb
, IFA_BROADCAST
, ifa
->ifa_broadcast
)) ||
1572 (ifa
->ifa_label
[0] &&
1573 nla_put_string(skb
, IFA_LABEL
, ifa
->ifa_label
)) ||
1574 nla_put_u32(skb
, IFA_FLAGS
, ifa
->ifa_flags
) ||
1575 put_cacheinfo(skb
, ifa
->ifa_cstamp
, ifa
->ifa_tstamp
,
1577 goto nla_put_failure
;
1579 nlmsg_end(skb
, nlh
);
1583 nlmsg_cancel(skb
, nlh
);
1587 static int inet_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1589 struct net
*net
= sock_net(skb
->sk
);
1592 int ip_idx
, s_ip_idx
;
1593 struct net_device
*dev
;
1594 struct in_device
*in_dev
;
1595 struct in_ifaddr
*ifa
;
1596 struct hlist_head
*head
;
1599 s_idx
= idx
= cb
->args
[1];
1600 s_ip_idx
= ip_idx
= cb
->args
[2];
1602 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1604 head
= &net
->dev_index_head
[h
];
1606 cb
->seq
= atomic_read(&net
->ipv4
.dev_addr_genid
) ^
1608 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
1611 if (h
> s_h
|| idx
> s_idx
)
1613 in_dev
= __in_dev_get_rcu(dev
);
1617 for (ifa
= in_dev
->ifa_list
, ip_idx
= 0; ifa
;
1618 ifa
= ifa
->ifa_next
, ip_idx
++) {
1619 if (ip_idx
< s_ip_idx
)
1621 if (inet_fill_ifaddr(skb
, ifa
,
1622 NETLINK_CB(cb
->skb
).portid
,
1624 RTM_NEWADDR
, NLM_F_MULTI
) < 0) {
1628 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1639 cb
->args
[2] = ip_idx
;
1644 static void rtmsg_ifa(int event
, struct in_ifaddr
*ifa
, struct nlmsghdr
*nlh
,
1647 struct sk_buff
*skb
;
1648 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
1652 net
= dev_net(ifa
->ifa_dev
->dev
);
1653 skb
= nlmsg_new(inet_nlmsg_size(), GFP_KERNEL
);
1657 err
= inet_fill_ifaddr(skb
, ifa
, portid
, seq
, event
, 0);
1659 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1660 WARN_ON(err
== -EMSGSIZE
);
1664 rtnl_notify(skb
, net
, portid
, RTNLGRP_IPV4_IFADDR
, nlh
, GFP_KERNEL
);
1668 rtnl_set_sk_err(net
, RTNLGRP_IPV4_IFADDR
, err
);
1671 static size_t inet_get_link_af_size(const struct net_device
*dev
,
1672 u32 ext_filter_mask
)
1674 struct in_device
*in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
1679 return nla_total_size(IPV4_DEVCONF_MAX
* 4); /* IFLA_INET_CONF */
1682 static int inet_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
,
1683 u32 ext_filter_mask
)
1685 struct in_device
*in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
1692 nla
= nla_reserve(skb
, IFLA_INET_CONF
, IPV4_DEVCONF_MAX
* 4);
1696 for (i
= 0; i
< IPV4_DEVCONF_MAX
; i
++)
1697 ((u32
*) nla_data(nla
))[i
] = in_dev
->cnf
.data
[i
];
1702 static const struct nla_policy inet_af_policy
[IFLA_INET_MAX
+1] = {
1703 [IFLA_INET_CONF
] = { .type
= NLA_NESTED
},
1706 static int inet_validate_link_af(const struct net_device
*dev
,
1707 const struct nlattr
*nla
)
1709 struct nlattr
*a
, *tb
[IFLA_INET_MAX
+1];
1712 if (dev
&& !__in_dev_get_rtnl(dev
))
1713 return -EAFNOSUPPORT
;
1715 err
= nla_parse_nested(tb
, IFLA_INET_MAX
, nla
, inet_af_policy
);
1719 if (tb
[IFLA_INET_CONF
]) {
1720 nla_for_each_nested(a
, tb
[IFLA_INET_CONF
], rem
) {
1721 int cfgid
= nla_type(a
);
1726 if (cfgid
<= 0 || cfgid
> IPV4_DEVCONF_MAX
)
1734 static int inet_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
1736 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
1737 struct nlattr
*a
, *tb
[IFLA_INET_MAX
+1];
1741 return -EAFNOSUPPORT
;
1743 if (nla_parse_nested(tb
, IFLA_INET_MAX
, nla
, NULL
) < 0)
1746 if (tb
[IFLA_INET_CONF
]) {
1747 nla_for_each_nested(a
, tb
[IFLA_INET_CONF
], rem
)
1748 ipv4_devconf_set(in_dev
, nla_type(a
), nla_get_u32(a
));
1754 static int inet_netconf_msgsize_devconf(int type
)
1756 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
1757 + nla_total_size(4); /* NETCONFA_IFINDEX */
1760 if (type
== NETCONFA_ALL
)
1763 if (all
|| type
== NETCONFA_FORWARDING
)
1764 size
+= nla_total_size(4);
1765 if (all
|| type
== NETCONFA_RP_FILTER
)
1766 size
+= nla_total_size(4);
1767 if (all
|| type
== NETCONFA_MC_FORWARDING
)
1768 size
+= nla_total_size(4);
1769 if (all
|| type
== NETCONFA_PROXY_NEIGH
)
1770 size
+= nla_total_size(4);
1771 if (all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
)
1772 size
+= nla_total_size(4);
1777 static int inet_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
1778 struct ipv4_devconf
*devconf
, u32 portid
,
1779 u32 seq
, int event
, unsigned int flags
,
1782 struct nlmsghdr
*nlh
;
1783 struct netconfmsg
*ncm
;
1786 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
1791 if (type
== NETCONFA_ALL
)
1794 ncm
= nlmsg_data(nlh
);
1795 ncm
->ncm_family
= AF_INET
;
1797 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
1798 goto nla_put_failure
;
1800 if ((all
|| type
== NETCONFA_FORWARDING
) &&
1801 nla_put_s32(skb
, NETCONFA_FORWARDING
,
1802 IPV4_DEVCONF(*devconf
, FORWARDING
)) < 0)
1803 goto nla_put_failure
;
1804 if ((all
|| type
== NETCONFA_RP_FILTER
) &&
1805 nla_put_s32(skb
, NETCONFA_RP_FILTER
,
1806 IPV4_DEVCONF(*devconf
, RP_FILTER
)) < 0)
1807 goto nla_put_failure
;
1808 if ((all
|| type
== NETCONFA_MC_FORWARDING
) &&
1809 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
1810 IPV4_DEVCONF(*devconf
, MC_FORWARDING
)) < 0)
1811 goto nla_put_failure
;
1812 if ((all
|| type
== NETCONFA_PROXY_NEIGH
) &&
1813 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
,
1814 IPV4_DEVCONF(*devconf
, PROXY_ARP
)) < 0)
1815 goto nla_put_failure
;
1816 if ((all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
) &&
1817 nla_put_s32(skb
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
1818 IPV4_DEVCONF(*devconf
, IGNORE_ROUTES_WITH_LINKDOWN
)) < 0)
1819 goto nla_put_failure
;
1821 nlmsg_end(skb
, nlh
);
1825 nlmsg_cancel(skb
, nlh
);
1829 void inet_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
1830 struct ipv4_devconf
*devconf
)
1832 struct sk_buff
*skb
;
1835 skb
= nlmsg_new(inet_netconf_msgsize_devconf(type
), GFP_KERNEL
);
1839 err
= inet_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
1840 RTM_NEWNETCONF
, 0, type
);
1842 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1843 WARN_ON(err
== -EMSGSIZE
);
1847 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_NETCONF
, NULL
, GFP_KERNEL
);
1851 rtnl_set_sk_err(net
, RTNLGRP_IPV4_NETCONF
, err
);
1854 static const struct nla_policy devconf_ipv4_policy
[NETCONFA_MAX
+1] = {
1855 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
1856 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
1857 [NETCONFA_RP_FILTER
] = { .len
= sizeof(int) },
1858 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
1859 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
] = { .len
= sizeof(int) },
1862 static int inet_netconf_get_devconf(struct sk_buff
*in_skb
,
1863 struct nlmsghdr
*nlh
)
1865 struct net
*net
= sock_net(in_skb
->sk
);
1866 struct nlattr
*tb
[NETCONFA_MAX
+1];
1867 struct netconfmsg
*ncm
;
1868 struct sk_buff
*skb
;
1869 struct ipv4_devconf
*devconf
;
1870 struct in_device
*in_dev
;
1871 struct net_device
*dev
;
1875 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
1876 devconf_ipv4_policy
);
1881 if (!tb
[NETCONFA_IFINDEX
])
1884 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
1886 case NETCONFA_IFINDEX_ALL
:
1887 devconf
= net
->ipv4
.devconf_all
;
1889 case NETCONFA_IFINDEX_DEFAULT
:
1890 devconf
= net
->ipv4
.devconf_dflt
;
1893 dev
= __dev_get_by_index(net
, ifindex
);
1896 in_dev
= __in_dev_get_rtnl(dev
);
1899 devconf
= &in_dev
->cnf
;
1904 skb
= nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_KERNEL
);
1908 err
= inet_netconf_fill_devconf(skb
, ifindex
, devconf
,
1909 NETLINK_CB(in_skb
).portid
,
1910 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
1913 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1914 WARN_ON(err
== -EMSGSIZE
);
1918 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
1923 static int inet_netconf_dump_devconf(struct sk_buff
*skb
,
1924 struct netlink_callback
*cb
)
1926 struct net
*net
= sock_net(skb
->sk
);
1929 struct net_device
*dev
;
1930 struct in_device
*in_dev
;
1931 struct hlist_head
*head
;
1934 s_idx
= idx
= cb
->args
[1];
1936 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1938 head
= &net
->dev_index_head
[h
];
1940 cb
->seq
= atomic_read(&net
->ipv4
.dev_addr_genid
) ^
1942 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
1945 in_dev
= __in_dev_get_rcu(dev
);
1949 if (inet_netconf_fill_devconf(skb
, dev
->ifindex
,
1951 NETLINK_CB(cb
->skb
).portid
,
1955 NETCONFA_ALL
) < 0) {
1959 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1965 if (h
== NETDEV_HASHENTRIES
) {
1966 if (inet_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
1967 net
->ipv4
.devconf_all
,
1968 NETLINK_CB(cb
->skb
).portid
,
1970 RTM_NEWNETCONF
, NLM_F_MULTI
,
1976 if (h
== NETDEV_HASHENTRIES
+ 1) {
1977 if (inet_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
1978 net
->ipv4
.devconf_dflt
,
1979 NETLINK_CB(cb
->skb
).portid
,
1981 RTM_NEWNETCONF
, NLM_F_MULTI
,
1994 #ifdef CONFIG_SYSCTL
1996 static void devinet_copy_dflt_conf(struct net
*net
, int i
)
1998 struct net_device
*dev
;
2001 for_each_netdev_rcu(net
, dev
) {
2002 struct in_device
*in_dev
;
2004 in_dev
= __in_dev_get_rcu(dev
);
2005 if (in_dev
&& !test_bit(i
, in_dev
->cnf
.state
))
2006 in_dev
->cnf
.data
[i
] = net
->ipv4
.devconf_dflt
->data
[i
];
2011 /* called with RTNL locked */
2012 static void inet_forward_change(struct net
*net
)
2014 struct net_device
*dev
;
2015 int on
= IPV4_DEVCONF_ALL(net
, FORWARDING
);
2017 IPV4_DEVCONF_ALL(net
, ACCEPT_REDIRECTS
) = !on
;
2018 IPV4_DEVCONF_DFLT(net
, FORWARDING
) = on
;
2019 inet_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
2020 NETCONFA_IFINDEX_ALL
,
2021 net
->ipv4
.devconf_all
);
2022 inet_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
2023 NETCONFA_IFINDEX_DEFAULT
,
2024 net
->ipv4
.devconf_dflt
);
2026 for_each_netdev(net
, dev
) {
2027 struct in_device
*in_dev
;
2030 dev_disable_lro(dev
);
2032 in_dev
= __in_dev_get_rtnl(dev
);
2034 IN_DEV_CONF_SET(in_dev
, FORWARDING
, on
);
2035 inet_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
2036 dev
->ifindex
, &in_dev
->cnf
);
2041 static int devinet_conf_ifindex(struct net
*net
, struct ipv4_devconf
*cnf
)
2043 if (cnf
== net
->ipv4
.devconf_dflt
)
2044 return NETCONFA_IFINDEX_DEFAULT
;
2045 else if (cnf
== net
->ipv4
.devconf_all
)
2046 return NETCONFA_IFINDEX_ALL
;
2048 struct in_device
*idev
2049 = container_of(cnf
, struct in_device
, cnf
);
2050 return idev
->dev
->ifindex
;
2054 static int devinet_conf_proc(struct ctl_table
*ctl
, int write
,
2055 void __user
*buffer
,
2056 size_t *lenp
, loff_t
*ppos
)
2058 int old_value
= *(int *)ctl
->data
;
2059 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2060 int new_value
= *(int *)ctl
->data
;
2063 struct ipv4_devconf
*cnf
= ctl
->extra1
;
2064 struct net
*net
= ctl
->extra2
;
2065 int i
= (int *)ctl
->data
- cnf
->data
;
2068 set_bit(i
, cnf
->state
);
2070 if (cnf
== net
->ipv4
.devconf_dflt
)
2071 devinet_copy_dflt_conf(net
, i
);
2072 if (i
== IPV4_DEVCONF_ACCEPT_LOCAL
- 1 ||
2073 i
== IPV4_DEVCONF_ROUTE_LOCALNET
- 1)
2074 if ((new_value
== 0) && (old_value
!= 0))
2075 rt_cache_flush(net
);
2077 if (i
== IPV4_DEVCONF_RP_FILTER
- 1 &&
2078 new_value
!= old_value
) {
2079 ifindex
= devinet_conf_ifindex(net
, cnf
);
2080 inet_netconf_notify_devconf(net
, NETCONFA_RP_FILTER
,
2083 if (i
== IPV4_DEVCONF_PROXY_ARP
- 1 &&
2084 new_value
!= old_value
) {
2085 ifindex
= devinet_conf_ifindex(net
, cnf
);
2086 inet_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
2089 if (i
== IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN
- 1 &&
2090 new_value
!= old_value
) {
2091 ifindex
= devinet_conf_ifindex(net
, cnf
);
2092 inet_netconf_notify_devconf(net
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
2100 static int devinet_sysctl_forward(struct ctl_table
*ctl
, int write
,
2101 void __user
*buffer
,
2102 size_t *lenp
, loff_t
*ppos
)
2104 int *valp
= ctl
->data
;
2107 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2109 if (write
&& *valp
!= val
) {
2110 struct net
*net
= ctl
->extra2
;
2112 if (valp
!= &IPV4_DEVCONF_DFLT(net
, FORWARDING
)) {
2113 if (!rtnl_trylock()) {
2114 /* Restore the original values before restarting */
2117 return restart_syscall();
2119 if (valp
== &IPV4_DEVCONF_ALL(net
, FORWARDING
)) {
2120 inet_forward_change(net
);
2122 struct ipv4_devconf
*cnf
= ctl
->extra1
;
2123 struct in_device
*idev
=
2124 container_of(cnf
, struct in_device
, cnf
);
2126 dev_disable_lro(idev
->dev
);
2127 inet_netconf_notify_devconf(net
,
2128 NETCONFA_FORWARDING
,
2133 rt_cache_flush(net
);
2135 inet_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
2136 NETCONFA_IFINDEX_DEFAULT
,
2137 net
->ipv4
.devconf_dflt
);
2143 static int ipv4_doint_and_flush(struct ctl_table
*ctl
, int write
,
2144 void __user
*buffer
,
2145 size_t *lenp
, loff_t
*ppos
)
2147 int *valp
= ctl
->data
;
2149 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2150 struct net
*net
= ctl
->extra2
;
2152 if (write
&& *valp
!= val
)
2153 rt_cache_flush(net
);
2158 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2161 .data = ipv4_devconf.data + \
2162 IPV4_DEVCONF_ ## attr - 1, \
2163 .maxlen = sizeof(int), \
2165 .proc_handler = proc, \
2166 .extra1 = &ipv4_devconf, \
2169 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2170 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2172 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2173 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2175 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2176 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2178 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2179 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2181 static struct devinet_sysctl_table
{
2182 struct ctl_table_header
*sysctl_header
;
2183 struct ctl_table devinet_vars
[__IPV4_DEVCONF_MAX
];
2184 } devinet_sysctl
= {
2186 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING
, "forwarding",
2187 devinet_sysctl_forward
),
2188 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING
, "mc_forwarding"),
2190 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS
, "accept_redirects"),
2191 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS
, "secure_redirects"),
2192 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA
, "shared_media"),
2193 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER
, "rp_filter"),
2194 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS
, "send_redirects"),
2195 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE
,
2196 "accept_source_route"),
2197 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL
, "accept_local"),
2198 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK
, "src_valid_mark"),
2199 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP
, "proxy_arp"),
2200 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID
, "medium_id"),
2201 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY
, "bootp_relay"),
2202 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS
, "log_martians"),
2203 DEVINET_SYSCTL_RW_ENTRY(TAG
, "tag"),
2204 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER
, "arp_filter"),
2205 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE
, "arp_announce"),
2206 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE
, "arp_ignore"),
2207 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT
, "arp_accept"),
2208 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY
, "arp_notify"),
2209 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN
, "proxy_arp_pvlan"),
2210 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION
,
2211 "force_igmp_version"),
2212 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL
,
2213 "igmpv2_unsolicited_report_interval"),
2214 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL
,
2215 "igmpv3_unsolicited_report_interval"),
2216 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN
,
2217 "ignore_routes_with_linkdown"),
2218 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP
,
2219 "drop_gratuitous_arp"),
2221 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM
, "disable_xfrm"),
2222 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY
, "disable_policy"),
2223 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES
,
2224 "promote_secondaries"),
2225 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET
,
2227 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST
,
2228 "drop_unicast_in_l2_multicast"),
2232 static int __devinet_sysctl_register(struct net
*net
, char *dev_name
,
2233 int ifindex
, struct ipv4_devconf
*p
)
2236 struct devinet_sysctl_table
*t
;
2237 char path
[sizeof("net/ipv4/conf/") + IFNAMSIZ
];
2239 t
= kmemdup(&devinet_sysctl
, sizeof(*t
), GFP_KERNEL
);
2243 for (i
= 0; i
< ARRAY_SIZE(t
->devinet_vars
) - 1; i
++) {
2244 t
->devinet_vars
[i
].data
+= (char *)p
- (char *)&ipv4_devconf
;
2245 t
->devinet_vars
[i
].extra1
= p
;
2246 t
->devinet_vars
[i
].extra2
= net
;
2249 snprintf(path
, sizeof(path
), "net/ipv4/conf/%s", dev_name
);
2251 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->devinet_vars
);
2252 if (!t
->sysctl_header
)
2257 inet_netconf_notify_devconf(net
, NETCONFA_ALL
, ifindex
, p
);
2266 static void __devinet_sysctl_unregister(struct ipv4_devconf
*cnf
)
2268 struct devinet_sysctl_table
*t
= cnf
->sysctl
;
2274 unregister_net_sysctl_table(t
->sysctl_header
);
2278 static int devinet_sysctl_register(struct in_device
*idev
)
2282 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
2285 err
= neigh_sysctl_register(idev
->dev
, idev
->arp_parms
, NULL
);
2288 err
= __devinet_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
2289 idev
->dev
->ifindex
, &idev
->cnf
);
2291 neigh_sysctl_unregister(idev
->arp_parms
);
2295 static void devinet_sysctl_unregister(struct in_device
*idev
)
2297 __devinet_sysctl_unregister(&idev
->cnf
);
2298 neigh_sysctl_unregister(idev
->arp_parms
);
2301 static struct ctl_table ctl_forward_entry
[] = {
2303 .procname
= "ip_forward",
2304 .data
= &ipv4_devconf
.data
[
2305 IPV4_DEVCONF_FORWARDING
- 1],
2306 .maxlen
= sizeof(int),
2308 .proc_handler
= devinet_sysctl_forward
,
2309 .extra1
= &ipv4_devconf
,
2310 .extra2
= &init_net
,
2316 static __net_init
int devinet_init_net(struct net
*net
)
2319 struct ipv4_devconf
*all
, *dflt
;
2320 #ifdef CONFIG_SYSCTL
2321 struct ctl_table
*tbl
= ctl_forward_entry
;
2322 struct ctl_table_header
*forw_hdr
;
2326 all
= &ipv4_devconf
;
2327 dflt
= &ipv4_devconf_dflt
;
2329 if (!net_eq(net
, &init_net
)) {
2330 all
= kmemdup(all
, sizeof(ipv4_devconf
), GFP_KERNEL
);
2334 dflt
= kmemdup(dflt
, sizeof(ipv4_devconf_dflt
), GFP_KERNEL
);
2336 goto err_alloc_dflt
;
2338 #ifdef CONFIG_SYSCTL
2339 tbl
= kmemdup(tbl
, sizeof(ctl_forward_entry
), GFP_KERNEL
);
2343 tbl
[0].data
= &all
->data
[IPV4_DEVCONF_FORWARDING
- 1];
2344 tbl
[0].extra1
= all
;
2345 tbl
[0].extra2
= net
;
2349 #ifdef CONFIG_SYSCTL
2350 err
= __devinet_sysctl_register(net
, "all", NETCONFA_IFINDEX_ALL
, all
);
2354 err
= __devinet_sysctl_register(net
, "default",
2355 NETCONFA_IFINDEX_DEFAULT
, dflt
);
2360 forw_hdr
= register_net_sysctl(net
, "net/ipv4", tbl
);
2363 net
->ipv4
.forw_hdr
= forw_hdr
;
2366 net
->ipv4
.devconf_all
= all
;
2367 net
->ipv4
.devconf_dflt
= dflt
;
2370 #ifdef CONFIG_SYSCTL
2372 __devinet_sysctl_unregister(dflt
);
2374 __devinet_sysctl_unregister(all
);
2376 if (tbl
!= ctl_forward_entry
)
2380 if (dflt
!= &ipv4_devconf_dflt
)
2383 if (all
!= &ipv4_devconf
)
2389 static __net_exit
void devinet_exit_net(struct net
*net
)
2391 #ifdef CONFIG_SYSCTL
2392 struct ctl_table
*tbl
;
2394 tbl
= net
->ipv4
.forw_hdr
->ctl_table_arg
;
2395 unregister_net_sysctl_table(net
->ipv4
.forw_hdr
);
2396 __devinet_sysctl_unregister(net
->ipv4
.devconf_dflt
);
2397 __devinet_sysctl_unregister(net
->ipv4
.devconf_all
);
2400 kfree(net
->ipv4
.devconf_dflt
);
2401 kfree(net
->ipv4
.devconf_all
);
2404 static __net_initdata
struct pernet_operations devinet_ops
= {
2405 .init
= devinet_init_net
,
2406 .exit
= devinet_exit_net
,
2409 static struct rtnl_af_ops inet_af_ops __read_mostly
= {
2411 .fill_link_af
= inet_fill_link_af
,
2412 .get_link_af_size
= inet_get_link_af_size
,
2413 .validate_link_af
= inet_validate_link_af
,
2414 .set_link_af
= inet_set_link_af
,
2417 void __init
devinet_init(void)
2421 for (i
= 0; i
< IN4_ADDR_HSIZE
; i
++)
2422 INIT_HLIST_HEAD(&inet_addr_lst
[i
]);
2424 register_pernet_subsys(&devinet_ops
);
2426 register_gifconf(PF_INET
, inet_gifconf
);
2427 register_netdevice_notifier(&ip_netdev_notifier
);
2429 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
, 0);
2431 rtnl_af_register(&inet_af_ops
);
2433 rtnl_register(PF_INET
, RTM_NEWADDR
, inet_rtm_newaddr
, NULL
, NULL
);
2434 rtnl_register(PF_INET
, RTM_DELADDR
, inet_rtm_deladdr
, NULL
, NULL
);
2435 rtnl_register(PF_INET
, RTM_GETADDR
, NULL
, inet_dump_ifaddr
, NULL
);
2436 rtnl_register(PF_INET
, RTM_GETNETCONF
, inet_netconf_get_devconf
,
2437 inet_netconf_dump_devconf
, NULL
);