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/sched/signal.h>
36 #include <linux/string.h>
38 #include <linux/socket.h>
39 #include <linux/sockios.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/if_addr.h>
44 #include <linux/if_ether.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/init.h>
50 #include <linux/notifier.h>
51 #include <linux/inetdevice.h>
52 #include <linux/igmp.h>
53 #include <linux/slab.h>
54 #include <linux/hash.h>
56 #include <linux/sysctl.h>
58 #include <linux/kmod.h>
59 #include <linux/netconf.h>
63 #include <net/route.h>
64 #include <net/ip_fib.h>
65 #include <net/rtnetlink.h>
66 #include <net/net_namespace.h>
67 #include <net/addrconf.h>
69 static struct ipv4_devconf ipv4_devconf
= {
71 [IPV4_DEVCONF_ACCEPT_REDIRECTS
- 1] = 1,
72 [IPV4_DEVCONF_SEND_REDIRECTS
- 1] = 1,
73 [IPV4_DEVCONF_SECURE_REDIRECTS
- 1] = 1,
74 [IPV4_DEVCONF_SHARED_MEDIA
- 1] = 1,
75 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL
- 1] = 10000 /*ms*/,
76 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL
- 1] = 1000 /*ms*/,
80 static struct ipv4_devconf ipv4_devconf_dflt
= {
82 [IPV4_DEVCONF_ACCEPT_REDIRECTS
- 1] = 1,
83 [IPV4_DEVCONF_SEND_REDIRECTS
- 1] = 1,
84 [IPV4_DEVCONF_SECURE_REDIRECTS
- 1] = 1,
85 [IPV4_DEVCONF_SHARED_MEDIA
- 1] = 1,
86 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE
- 1] = 1,
87 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL
- 1] = 10000 /*ms*/,
88 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL
- 1] = 1000 /*ms*/,
92 #define IPV4_DEVCONF_DFLT(net, attr) \
93 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
95 static const struct nla_policy ifa_ipv4_policy
[IFA_MAX
+1] = {
96 [IFA_LOCAL
] = { .type
= NLA_U32
},
97 [IFA_ADDRESS
] = { .type
= NLA_U32
},
98 [IFA_BROADCAST
] = { .type
= NLA_U32
},
99 [IFA_LABEL
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
- 1 },
100 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
101 [IFA_FLAGS
] = { .type
= NLA_U32
},
102 [IFA_RT_PRIORITY
] = { .type
= NLA_U32
},
103 [IFA_TARGET_NETNSID
] = { .type
= NLA_S32
},
106 struct inet_fill_args
{
115 #define IN4_ADDR_HSIZE_SHIFT 8
116 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
118 static struct hlist_head inet_addr_lst
[IN4_ADDR_HSIZE
];
120 static u32
inet_addr_hash(const struct net
*net
, __be32 addr
)
122 u32 val
= (__force u32
) addr
^ net_hash_mix(net
);
124 return hash_32(val
, IN4_ADDR_HSIZE_SHIFT
);
127 static void inet_hash_insert(struct net
*net
, struct in_ifaddr
*ifa
)
129 u32 hash
= inet_addr_hash(net
, ifa
->ifa_local
);
132 hlist_add_head_rcu(&ifa
->hash
, &inet_addr_lst
[hash
]);
135 static void inet_hash_remove(struct in_ifaddr
*ifa
)
138 hlist_del_init_rcu(&ifa
->hash
);
142 * __ip_dev_find - find the first device with a given source address.
143 * @net: the net namespace
144 * @addr: the source address
145 * @devref: if true, take a reference on the found device
147 * If a caller uses devref=false, it should be protected by RCU, or RTNL
149 struct net_device
*__ip_dev_find(struct net
*net
, __be32 addr
, bool devref
)
151 struct net_device
*result
= NULL
;
152 struct in_ifaddr
*ifa
;
155 ifa
= inet_lookup_ifaddr_rcu(net
, addr
);
157 struct flowi4 fl4
= { .daddr
= addr
};
158 struct fib_result res
= { 0 };
159 struct fib_table
*local
;
161 /* Fallback to FIB local table so that communication
162 * over loopback subnets work.
164 local
= fib_get_table(net
, RT_TABLE_LOCAL
);
166 !fib_table_lookup(local
, &fl4
, &res
, FIB_LOOKUP_NOREF
) &&
167 res
.type
== RTN_LOCAL
)
168 result
= FIB_RES_DEV(res
);
170 result
= ifa
->ifa_dev
->dev
;
172 if (result
&& devref
)
177 EXPORT_SYMBOL(__ip_dev_find
);
179 /* called under RCU lock */
180 struct in_ifaddr
*inet_lookup_ifaddr_rcu(struct net
*net
, __be32 addr
)
182 u32 hash
= inet_addr_hash(net
, addr
);
183 struct in_ifaddr
*ifa
;
185 hlist_for_each_entry_rcu(ifa
, &inet_addr_lst
[hash
], hash
)
186 if (ifa
->ifa_local
== addr
&&
187 net_eq(dev_net(ifa
->ifa_dev
->dev
), net
))
193 static void rtmsg_ifa(int event
, struct in_ifaddr
*, struct nlmsghdr
*, u32
);
195 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain
);
196 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain
);
197 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
200 static int devinet_sysctl_register(struct in_device
*idev
);
201 static void devinet_sysctl_unregister(struct in_device
*idev
);
203 static int devinet_sysctl_register(struct in_device
*idev
)
207 static void devinet_sysctl_unregister(struct in_device
*idev
)
212 /* Locks all the inet devices. */
214 static struct in_ifaddr
*inet_alloc_ifa(void)
216 return kzalloc(sizeof(struct in_ifaddr
), GFP_KERNEL
);
219 static void inet_rcu_free_ifa(struct rcu_head
*head
)
221 struct in_ifaddr
*ifa
= container_of(head
, struct in_ifaddr
, rcu_head
);
223 in_dev_put(ifa
->ifa_dev
);
227 static void inet_free_ifa(struct in_ifaddr
*ifa
)
229 call_rcu(&ifa
->rcu_head
, inet_rcu_free_ifa
);
232 void in_dev_finish_destroy(struct in_device
*idev
)
234 struct net_device
*dev
= idev
->dev
;
236 WARN_ON(idev
->ifa_list
);
237 WARN_ON(idev
->mc_list
);
238 kfree(rcu_dereference_protected(idev
->mc_hash
, 1));
239 #ifdef NET_REFCNT_DEBUG
240 pr_debug("%s: %p=%s\n", __func__
, idev
, dev
? dev
->name
: "NIL");
244 pr_err("Freeing alive in_device %p\n", idev
);
248 EXPORT_SYMBOL(in_dev_finish_destroy
);
250 static struct in_device
*inetdev_init(struct net_device
*dev
)
252 struct in_device
*in_dev
;
257 in_dev
= kzalloc(sizeof(*in_dev
), GFP_KERNEL
);
260 memcpy(&in_dev
->cnf
, dev_net(dev
)->ipv4
.devconf_dflt
,
261 sizeof(in_dev
->cnf
));
262 in_dev
->cnf
.sysctl
= NULL
;
264 in_dev
->arp_parms
= neigh_parms_alloc(dev
, &arp_tbl
);
265 if (!in_dev
->arp_parms
)
267 if (IPV4_DEVCONF(in_dev
->cnf
, FORWARDING
))
268 dev_disable_lro(dev
);
269 /* Reference in_dev->dev */
271 /* Account for reference dev->ip_ptr (below) */
272 refcount_set(&in_dev
->refcnt
, 1);
274 err
= devinet_sysctl_register(in_dev
);
281 ip_mc_init_dev(in_dev
);
282 if (dev
->flags
& IFF_UP
)
285 /* we can receive as soon as ip_ptr is set -- do this last */
286 rcu_assign_pointer(dev
->ip_ptr
, in_dev
);
288 return in_dev
?: ERR_PTR(err
);
295 static void in_dev_rcu_put(struct rcu_head
*head
)
297 struct in_device
*idev
= container_of(head
, struct in_device
, rcu_head
);
301 static void inetdev_destroy(struct in_device
*in_dev
)
303 struct in_ifaddr
*ifa
;
304 struct net_device
*dev
;
312 ip_mc_destroy_dev(in_dev
);
314 while ((ifa
= in_dev
->ifa_list
) != NULL
) {
315 inet_del_ifa(in_dev
, &in_dev
->ifa_list
, 0);
319 RCU_INIT_POINTER(dev
->ip_ptr
, NULL
);
321 devinet_sysctl_unregister(in_dev
);
322 neigh_parms_release(&arp_tbl
, in_dev
->arp_parms
);
325 call_rcu(&in_dev
->rcu_head
, in_dev_rcu_put
);
328 int inet_addr_onlink(struct in_device
*in_dev
, __be32 a
, __be32 b
)
330 const struct in_ifaddr
*ifa
;
333 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
334 if (inet_ifa_match(a
, ifa
)) {
335 if (!b
|| inet_ifa_match(b
, ifa
)) {
345 static void __inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
346 int destroy
, struct nlmsghdr
*nlh
, u32 portid
)
348 struct in_ifaddr
*promote
= NULL
;
349 struct in_ifaddr
*ifa
, *ifa1
= *ifap
;
350 struct in_ifaddr
*last_prim
= in_dev
->ifa_list
;
351 struct in_ifaddr
*prev_prom
= NULL
;
352 int do_promote
= IN_DEV_PROMOTE_SECONDARIES(in_dev
);
359 /* 1. Deleting primary ifaddr forces deletion all secondaries
360 * unless alias promotion is set
363 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
)) {
364 struct in_ifaddr
**ifap1
= &ifa1
->ifa_next
;
366 while ((ifa
= *ifap1
) != NULL
) {
367 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
) &&
368 ifa1
->ifa_scope
<= ifa
->ifa_scope
)
371 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
) ||
372 ifa1
->ifa_mask
!= ifa
->ifa_mask
||
373 !inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
374 ifap1
= &ifa
->ifa_next
;
380 inet_hash_remove(ifa
);
381 *ifap1
= ifa
->ifa_next
;
383 rtmsg_ifa(RTM_DELADDR
, ifa
, nlh
, portid
);
384 blocking_notifier_call_chain(&inetaddr_chain
,
394 /* On promotion all secondaries from subnet are changing
395 * the primary IP, we must remove all their routes silently
396 * and later to add them back with new prefsrc. Do this
397 * while all addresses are on the device list.
399 for (ifa
= promote
; ifa
; ifa
= ifa
->ifa_next
) {
400 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
401 inet_ifa_match(ifa1
->ifa_address
, ifa
))
402 fib_del_ifaddr(ifa
, ifa1
);
408 *ifap
= ifa1
->ifa_next
;
409 inet_hash_remove(ifa1
);
411 /* 3. Announce address deletion */
413 /* Send message first, then call notifier.
414 At first sight, FIB update triggered by notifier
415 will refer to already deleted ifaddr, that could confuse
416 netlink listeners. It is not true: look, gated sees
417 that route deleted and if it still thinks that ifaddr
418 is valid, it will try to restore deleted routes... Grr.
419 So that, this order is correct.
421 rtmsg_ifa(RTM_DELADDR
, ifa1
, nlh
, portid
);
422 blocking_notifier_call_chain(&inetaddr_chain
, NETDEV_DOWN
, ifa1
);
425 struct in_ifaddr
*next_sec
= promote
->ifa_next
;
428 prev_prom
->ifa_next
= promote
->ifa_next
;
429 promote
->ifa_next
= last_prim
->ifa_next
;
430 last_prim
->ifa_next
= promote
;
433 promote
->ifa_flags
&= ~IFA_F_SECONDARY
;
434 rtmsg_ifa(RTM_NEWADDR
, promote
, nlh
, portid
);
435 blocking_notifier_call_chain(&inetaddr_chain
,
437 for (ifa
= next_sec
; ifa
; ifa
= ifa
->ifa_next
) {
438 if (ifa1
->ifa_mask
!= ifa
->ifa_mask
||
439 !inet_ifa_match(ifa1
->ifa_address
, ifa
))
449 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
452 __inet_del_ifa(in_dev
, ifap
, destroy
, NULL
, 0);
455 static void check_lifetime(struct work_struct
*work
);
457 static DECLARE_DELAYED_WORK(check_lifetime_work
, check_lifetime
);
459 static int __inet_insert_ifa(struct in_ifaddr
*ifa
, struct nlmsghdr
*nlh
,
460 u32 portid
, struct netlink_ext_ack
*extack
)
462 struct in_device
*in_dev
= ifa
->ifa_dev
;
463 struct in_ifaddr
*ifa1
, **ifap
, **last_primary
;
464 struct in_validator_info ivi
;
469 if (!ifa
->ifa_local
) {
474 ifa
->ifa_flags
&= ~IFA_F_SECONDARY
;
475 last_primary
= &in_dev
->ifa_list
;
477 for (ifap
= &in_dev
->ifa_list
; (ifa1
= *ifap
) != NULL
;
478 ifap
= &ifa1
->ifa_next
) {
479 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
) &&
480 ifa
->ifa_scope
<= ifa1
->ifa_scope
)
481 last_primary
= &ifa1
->ifa_next
;
482 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
483 inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
484 if (ifa1
->ifa_local
== ifa
->ifa_local
) {
488 if (ifa1
->ifa_scope
!= ifa
->ifa_scope
) {
492 ifa
->ifa_flags
|= IFA_F_SECONDARY
;
496 /* Allow any devices that wish to register ifaddr validtors to weigh
497 * in now, before changes are committed. The rntl lock is serializing
498 * access here, so the state should not change between a validator call
499 * and a final notify on commit. This isn't invoked on promotion under
500 * the assumption that validators are checking the address itself, and
503 ivi
.ivi_addr
= ifa
->ifa_address
;
504 ivi
.ivi_dev
= ifa
->ifa_dev
;
506 ret
= blocking_notifier_call_chain(&inetaddr_validator_chain
,
508 ret
= notifier_to_errno(ret
);
514 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
)) {
515 prandom_seed((__force u32
) ifa
->ifa_local
);
519 ifa
->ifa_next
= *ifap
;
522 inet_hash_insert(dev_net(in_dev
->dev
), ifa
);
524 cancel_delayed_work(&check_lifetime_work
);
525 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
, 0);
527 /* Send message first, then call notifier.
528 Notifier will trigger FIB update, so that
529 listeners of netlink will know about new ifaddr */
530 rtmsg_ifa(RTM_NEWADDR
, ifa
, nlh
, portid
);
531 blocking_notifier_call_chain(&inetaddr_chain
, NETDEV_UP
, ifa
);
536 static int inet_insert_ifa(struct in_ifaddr
*ifa
)
538 return __inet_insert_ifa(ifa
, NULL
, 0, NULL
);
541 static int inet_set_ifa(struct net_device
*dev
, struct in_ifaddr
*ifa
)
543 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
551 ipv4_devconf_setall(in_dev
);
552 neigh_parms_data_state_setall(in_dev
->arp_parms
);
553 if (ifa
->ifa_dev
!= in_dev
) {
554 WARN_ON(ifa
->ifa_dev
);
556 ifa
->ifa_dev
= in_dev
;
558 if (ipv4_is_loopback(ifa
->ifa_local
))
559 ifa
->ifa_scope
= RT_SCOPE_HOST
;
560 return inet_insert_ifa(ifa
);
563 /* Caller must hold RCU or RTNL :
564 * We dont take a reference on found in_device
566 struct in_device
*inetdev_by_index(struct net
*net
, int ifindex
)
568 struct net_device
*dev
;
569 struct in_device
*in_dev
= NULL
;
572 dev
= dev_get_by_index_rcu(net
, ifindex
);
574 in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
578 EXPORT_SYMBOL(inetdev_by_index
);
580 /* Called only from RTNL semaphored context. No locks. */
582 struct in_ifaddr
*inet_ifa_byprefix(struct in_device
*in_dev
, __be32 prefix
,
585 struct in_ifaddr
*ifa
;
589 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
590 if (ifa
->ifa_mask
== mask
&& inet_ifa_match(prefix
, ifa
))
596 static int ip_mc_config(struct sock
*sk
, bool join
, const struct in_ifaddr
*ifa
)
598 struct ip_mreqn mreq
= {
599 .imr_multiaddr
.s_addr
= ifa
->ifa_address
,
600 .imr_ifindex
= ifa
->ifa_dev
->dev
->ifindex
,
608 ret
= ip_mc_join_group(sk
, &mreq
);
610 ret
= ip_mc_leave_group(sk
, &mreq
);
616 static int inet_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
617 struct netlink_ext_ack
*extack
)
619 struct net
*net
= sock_net(skb
->sk
);
620 struct nlattr
*tb
[IFA_MAX
+1];
621 struct in_device
*in_dev
;
622 struct ifaddrmsg
*ifm
;
623 struct in_ifaddr
*ifa
, **ifap
;
628 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
629 ifa_ipv4_policy
, extack
);
633 ifm
= nlmsg_data(nlh
);
634 in_dev
= inetdev_by_index(net
, ifm
->ifa_index
);
640 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
641 ifap
= &ifa
->ifa_next
) {
643 ifa
->ifa_local
!= nla_get_in_addr(tb
[IFA_LOCAL
]))
646 if (tb
[IFA_LABEL
] && nla_strcmp(tb
[IFA_LABEL
], ifa
->ifa_label
))
649 if (tb
[IFA_ADDRESS
] &&
650 (ifm
->ifa_prefixlen
!= ifa
->ifa_prefixlen
||
651 !inet_ifa_match(nla_get_in_addr(tb
[IFA_ADDRESS
]), ifa
)))
654 if (ipv4_is_multicast(ifa
->ifa_address
))
655 ip_mc_config(net
->ipv4
.mc_autojoin_sk
, false, ifa
);
656 __inet_del_ifa(in_dev
, ifap
, 1, nlh
, NETLINK_CB(skb
).portid
);
660 err
= -EADDRNOTAVAIL
;
665 #define INFINITY_LIFE_TIME 0xFFFFFFFF
667 static void check_lifetime(struct work_struct
*work
)
669 unsigned long now
, next
, next_sec
, next_sched
;
670 struct in_ifaddr
*ifa
;
671 struct hlist_node
*n
;
675 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
677 for (i
= 0; i
< IN4_ADDR_HSIZE
; i
++) {
678 bool change_needed
= false;
681 hlist_for_each_entry_rcu(ifa
, &inet_addr_lst
[i
], hash
) {
684 if (ifa
->ifa_flags
& IFA_F_PERMANENT
)
687 /* We try to batch several events at once. */
688 age
= (now
- ifa
->ifa_tstamp
+
689 ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
691 if (ifa
->ifa_valid_lft
!= INFINITY_LIFE_TIME
&&
692 age
>= ifa
->ifa_valid_lft
) {
693 change_needed
= true;
694 } else if (ifa
->ifa_preferred_lft
==
695 INFINITY_LIFE_TIME
) {
697 } else if (age
>= ifa
->ifa_preferred_lft
) {
698 if (time_before(ifa
->ifa_tstamp
+
699 ifa
->ifa_valid_lft
* HZ
, next
))
700 next
= ifa
->ifa_tstamp
+
701 ifa
->ifa_valid_lft
* HZ
;
703 if (!(ifa
->ifa_flags
& IFA_F_DEPRECATED
))
704 change_needed
= true;
705 } else if (time_before(ifa
->ifa_tstamp
+
706 ifa
->ifa_preferred_lft
* HZ
,
708 next
= ifa
->ifa_tstamp
+
709 ifa
->ifa_preferred_lft
* HZ
;
716 hlist_for_each_entry_safe(ifa
, n
, &inet_addr_lst
[i
], hash
) {
719 if (ifa
->ifa_flags
& IFA_F_PERMANENT
)
722 /* We try to batch several events at once. */
723 age
= (now
- ifa
->ifa_tstamp
+
724 ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
726 if (ifa
->ifa_valid_lft
!= INFINITY_LIFE_TIME
&&
727 age
>= ifa
->ifa_valid_lft
) {
728 struct in_ifaddr
**ifap
;
730 for (ifap
= &ifa
->ifa_dev
->ifa_list
;
731 *ifap
!= NULL
; ifap
= &(*ifap
)->ifa_next
) {
733 inet_del_ifa(ifa
->ifa_dev
,
738 } else if (ifa
->ifa_preferred_lft
!=
739 INFINITY_LIFE_TIME
&&
740 age
>= ifa
->ifa_preferred_lft
&&
741 !(ifa
->ifa_flags
& IFA_F_DEPRECATED
)) {
742 ifa
->ifa_flags
|= IFA_F_DEPRECATED
;
743 rtmsg_ifa(RTM_NEWADDR
, ifa
, NULL
, 0);
749 next_sec
= round_jiffies_up(next
);
752 /* If rounded timeout is accurate enough, accept it. */
753 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
754 next_sched
= next_sec
;
757 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
758 if (time_before(next_sched
, now
+ ADDRCONF_TIMER_FUZZ_MAX
))
759 next_sched
= now
+ ADDRCONF_TIMER_FUZZ_MAX
;
761 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
,
765 static void set_ifa_lifetime(struct in_ifaddr
*ifa
, __u32 valid_lft
,
768 unsigned long timeout
;
770 ifa
->ifa_flags
&= ~(IFA_F_PERMANENT
| IFA_F_DEPRECATED
);
772 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
773 if (addrconf_finite_timeout(timeout
))
774 ifa
->ifa_valid_lft
= timeout
;
776 ifa
->ifa_flags
|= IFA_F_PERMANENT
;
778 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
779 if (addrconf_finite_timeout(timeout
)) {
781 ifa
->ifa_flags
|= IFA_F_DEPRECATED
;
782 ifa
->ifa_preferred_lft
= timeout
;
784 ifa
->ifa_tstamp
= jiffies
;
785 if (!ifa
->ifa_cstamp
)
786 ifa
->ifa_cstamp
= ifa
->ifa_tstamp
;
789 static struct in_ifaddr
*rtm_to_ifaddr(struct net
*net
, struct nlmsghdr
*nlh
,
790 __u32
*pvalid_lft
, __u32
*pprefered_lft
,
791 struct netlink_ext_ack
*extack
)
793 struct nlattr
*tb
[IFA_MAX
+1];
794 struct in_ifaddr
*ifa
;
795 struct ifaddrmsg
*ifm
;
796 struct net_device
*dev
;
797 struct in_device
*in_dev
;
800 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
801 ifa_ipv4_policy
, extack
);
805 ifm
= nlmsg_data(nlh
);
807 if (ifm
->ifa_prefixlen
> 32 || !tb
[IFA_LOCAL
])
810 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
815 in_dev
= __in_dev_get_rtnl(dev
);
820 ifa
= inet_alloc_ifa();
823 * A potential indev allocation can be left alive, it stays
824 * assigned to its device and is destroy with it.
828 ipv4_devconf_setall(in_dev
);
829 neigh_parms_data_state_setall(in_dev
->arp_parms
);
832 if (!tb
[IFA_ADDRESS
])
833 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
835 INIT_HLIST_NODE(&ifa
->hash
);
836 ifa
->ifa_prefixlen
= ifm
->ifa_prefixlen
;
837 ifa
->ifa_mask
= inet_make_mask(ifm
->ifa_prefixlen
);
838 ifa
->ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) :
840 ifa
->ifa_scope
= ifm
->ifa_scope
;
841 ifa
->ifa_dev
= in_dev
;
843 ifa
->ifa_local
= nla_get_in_addr(tb
[IFA_LOCAL
]);
844 ifa
->ifa_address
= nla_get_in_addr(tb
[IFA_ADDRESS
]);
846 if (tb
[IFA_BROADCAST
])
847 ifa
->ifa_broadcast
= nla_get_in_addr(tb
[IFA_BROADCAST
]);
850 nla_strlcpy(ifa
->ifa_label
, tb
[IFA_LABEL
], IFNAMSIZ
);
852 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
854 if (tb
[IFA_RT_PRIORITY
])
855 ifa
->ifa_rt_priority
= nla_get_u32(tb
[IFA_RT_PRIORITY
]);
857 if (tb
[IFA_CACHEINFO
]) {
858 struct ifa_cacheinfo
*ci
;
860 ci
= nla_data(tb
[IFA_CACHEINFO
]);
861 if (!ci
->ifa_valid
|| ci
->ifa_prefered
> ci
->ifa_valid
) {
865 *pvalid_lft
= ci
->ifa_valid
;
866 *pprefered_lft
= ci
->ifa_prefered
;
877 static struct in_ifaddr
*find_matching_ifa(struct in_ifaddr
*ifa
)
879 struct in_device
*in_dev
= ifa
->ifa_dev
;
880 struct in_ifaddr
*ifa1
;
885 in_dev_for_each_ifa_rtnl(ifa1
, in_dev
) {
886 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
887 inet_ifa_match(ifa1
->ifa_address
, ifa
) &&
888 ifa1
->ifa_local
== ifa
->ifa_local
)
894 static int inet_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
895 struct netlink_ext_ack
*extack
)
897 struct net
*net
= sock_net(skb
->sk
);
898 struct in_ifaddr
*ifa
;
899 struct in_ifaddr
*ifa_existing
;
900 __u32 valid_lft
= INFINITY_LIFE_TIME
;
901 __u32 prefered_lft
= INFINITY_LIFE_TIME
;
905 ifa
= rtm_to_ifaddr(net
, nlh
, &valid_lft
, &prefered_lft
, extack
);
909 ifa_existing
= find_matching_ifa(ifa
);
911 /* It would be best to check for !NLM_F_CREATE here but
912 * userspace already relies on not having to provide this.
914 set_ifa_lifetime(ifa
, valid_lft
, prefered_lft
);
915 if (ifa
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
916 int ret
= ip_mc_config(net
->ipv4
.mc_autojoin_sk
,
924 return __inet_insert_ifa(ifa
, nlh
, NETLINK_CB(skb
).portid
,
927 u32 new_metric
= ifa
->ifa_rt_priority
;
931 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
932 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
936 if (ifa
->ifa_rt_priority
!= new_metric
) {
937 fib_modify_prefix_metric(ifa
, new_metric
);
938 ifa
->ifa_rt_priority
= new_metric
;
941 set_ifa_lifetime(ifa
, valid_lft
, prefered_lft
);
942 cancel_delayed_work(&check_lifetime_work
);
943 queue_delayed_work(system_power_efficient_wq
,
944 &check_lifetime_work
, 0);
945 rtmsg_ifa(RTM_NEWADDR
, ifa
, nlh
, NETLINK_CB(skb
).portid
);
951 * Determine a default network mask, based on the IP address.
954 static int inet_abc_len(__be32 addr
)
956 int rc
= -1; /* Something else, probably a multicast. */
958 if (ipv4_is_zeronet(addr
) || ipv4_is_lbcast(addr
))
961 __u32 haddr
= ntohl(addr
);
962 if (IN_CLASSA(haddr
))
964 else if (IN_CLASSB(haddr
))
966 else if (IN_CLASSC(haddr
))
968 else if (IN_CLASSE(haddr
))
976 int devinet_ioctl(struct net
*net
, unsigned int cmd
, struct ifreq
*ifr
)
978 struct sockaddr_in sin_orig
;
979 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
980 struct in_device
*in_dev
;
981 struct in_ifaddr
**ifap
= NULL
;
982 struct in_ifaddr
*ifa
= NULL
;
983 struct net_device
*dev
;
986 int tryaddrmatch
= 0;
988 ifr
->ifr_name
[IFNAMSIZ
- 1] = 0;
990 /* save original address for comparison */
991 memcpy(&sin_orig
, sin
, sizeof(*sin
));
993 colon
= strchr(ifr
->ifr_name
, ':');
997 dev_load(net
, ifr
->ifr_name
);
1000 case SIOCGIFADDR
: /* Get interface address */
1001 case SIOCGIFBRDADDR
: /* Get the broadcast address */
1002 case SIOCGIFDSTADDR
: /* Get the destination address */
1003 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
1004 /* Note that these ioctls will not sleep,
1005 so that we do not impose a lock.
1006 One day we will be forced to put shlock here (I mean SMP)
1008 tryaddrmatch
= (sin_orig
.sin_family
== AF_INET
);
1009 memset(sin
, 0, sizeof(*sin
));
1010 sin
->sin_family
= AF_INET
;
1015 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1018 case SIOCSIFADDR
: /* Set interface address (and family) */
1019 case SIOCSIFBRDADDR
: /* Set the broadcast address */
1020 case SIOCSIFDSTADDR
: /* Set the destination address */
1021 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
1023 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1026 if (sin
->sin_family
!= AF_INET
)
1037 dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1044 in_dev
= __in_dev_get_rtnl(dev
);
1047 /* Matthias Andree */
1048 /* compare label and address (4.4BSD style) */
1049 /* note: we only do this for a limited set of ioctls
1050 and only if the original address family was AF_INET.
1051 This is checked above. */
1052 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
1053 ifap
= &ifa
->ifa_next
) {
1054 if (!strcmp(ifr
->ifr_name
, ifa
->ifa_label
) &&
1055 sin_orig
.sin_addr
.s_addr
==
1061 /* we didn't get a match, maybe the application is
1062 4.3BSD-style and passed in junk so we fall back to
1063 comparing just the label */
1065 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
1066 ifap
= &ifa
->ifa_next
)
1067 if (!strcmp(ifr
->ifr_name
, ifa
->ifa_label
))
1072 ret
= -EADDRNOTAVAIL
;
1073 if (!ifa
&& cmd
!= SIOCSIFADDR
&& cmd
!= SIOCSIFFLAGS
)
1077 case SIOCGIFADDR
: /* Get interface address */
1079 sin
->sin_addr
.s_addr
= ifa
->ifa_local
;
1082 case SIOCGIFBRDADDR
: /* Get the broadcast address */
1084 sin
->sin_addr
.s_addr
= ifa
->ifa_broadcast
;
1087 case SIOCGIFDSTADDR
: /* Get the destination address */
1089 sin
->sin_addr
.s_addr
= ifa
->ifa_address
;
1092 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
1094 sin
->sin_addr
.s_addr
= ifa
->ifa_mask
;
1099 ret
= -EADDRNOTAVAIL
;
1103 if (!(ifr
->ifr_flags
& IFF_UP
))
1104 inet_del_ifa(in_dev
, ifap
, 1);
1107 ret
= dev_change_flags(dev
, ifr
->ifr_flags
, NULL
);
1110 case SIOCSIFADDR
: /* Set interface address (and family) */
1112 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
1117 ifa
= inet_alloc_ifa();
1120 INIT_HLIST_NODE(&ifa
->hash
);
1122 memcpy(ifa
->ifa_label
, ifr
->ifr_name
, IFNAMSIZ
);
1124 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1127 if (ifa
->ifa_local
== sin
->sin_addr
.s_addr
)
1129 inet_del_ifa(in_dev
, ifap
, 0);
1130 ifa
->ifa_broadcast
= 0;
1134 ifa
->ifa_address
= ifa
->ifa_local
= sin
->sin_addr
.s_addr
;
1136 if (!(dev
->flags
& IFF_POINTOPOINT
)) {
1137 ifa
->ifa_prefixlen
= inet_abc_len(ifa
->ifa_address
);
1138 ifa
->ifa_mask
= inet_make_mask(ifa
->ifa_prefixlen
);
1139 if ((dev
->flags
& IFF_BROADCAST
) &&
1140 ifa
->ifa_prefixlen
< 31)
1141 ifa
->ifa_broadcast
= ifa
->ifa_address
|
1144 ifa
->ifa_prefixlen
= 32;
1145 ifa
->ifa_mask
= inet_make_mask(32);
1147 set_ifa_lifetime(ifa
, INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
1148 ret
= inet_set_ifa(dev
, ifa
);
1151 case SIOCSIFBRDADDR
: /* Set the broadcast address */
1153 if (ifa
->ifa_broadcast
!= sin
->sin_addr
.s_addr
) {
1154 inet_del_ifa(in_dev
, ifap
, 0);
1155 ifa
->ifa_broadcast
= sin
->sin_addr
.s_addr
;
1156 inet_insert_ifa(ifa
);
1160 case SIOCSIFDSTADDR
: /* Set the destination address */
1162 if (ifa
->ifa_address
== sin
->sin_addr
.s_addr
)
1165 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
1168 inet_del_ifa(in_dev
, ifap
, 0);
1169 ifa
->ifa_address
= sin
->sin_addr
.s_addr
;
1170 inet_insert_ifa(ifa
);
1173 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
1176 * The mask we set must be legal.
1179 if (bad_mask(sin
->sin_addr
.s_addr
, 0))
1182 if (ifa
->ifa_mask
!= sin
->sin_addr
.s_addr
) {
1183 __be32 old_mask
= ifa
->ifa_mask
;
1184 inet_del_ifa(in_dev
, ifap
, 0);
1185 ifa
->ifa_mask
= sin
->sin_addr
.s_addr
;
1186 ifa
->ifa_prefixlen
= inet_mask_len(ifa
->ifa_mask
);
1188 /* See if current broadcast address matches
1189 * with current netmask, then recalculate
1190 * the broadcast address. Otherwise it's a
1191 * funny address, so don't touch it since
1192 * the user seems to know what (s)he's doing...
1194 if ((dev
->flags
& IFF_BROADCAST
) &&
1195 (ifa
->ifa_prefixlen
< 31) &&
1196 (ifa
->ifa_broadcast
==
1197 (ifa
->ifa_local
|~old_mask
))) {
1198 ifa
->ifa_broadcast
= (ifa
->ifa_local
|
1199 ~sin
->sin_addr
.s_addr
);
1201 inet_insert_ifa(ifa
);
1211 static int inet_gifconf(struct net_device
*dev
, char __user
*buf
, int len
, int size
)
1213 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
1214 const struct in_ifaddr
*ifa
;
1218 if (WARN_ON(size
> sizeof(struct ifreq
)))
1224 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
1231 memset(&ifr
, 0, sizeof(struct ifreq
));
1232 strcpy(ifr
.ifr_name
, ifa
->ifa_label
);
1234 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_family
= AF_INET
;
1235 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_addr
.s_addr
=
1238 if (copy_to_user(buf
+ done
, &ifr
, size
)) {
1249 static __be32
in_dev_select_addr(const struct in_device
*in_dev
,
1252 const struct in_ifaddr
*ifa
;
1254 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
1255 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
1257 if (ifa
->ifa_scope
!= RT_SCOPE_LINK
&&
1258 ifa
->ifa_scope
<= scope
)
1259 return ifa
->ifa_local
;
1265 __be32
inet_select_addr(const struct net_device
*dev
, __be32 dst
, int scope
)
1267 const struct in_ifaddr
*ifa
;
1269 struct in_device
*in_dev
;
1270 struct net
*net
= dev_net(dev
);
1274 in_dev
= __in_dev_get_rcu(dev
);
1278 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
1279 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
1281 if (ifa
->ifa_scope
> scope
)
1283 if (!dst
|| inet_ifa_match(dst
, ifa
)) {
1284 addr
= ifa
->ifa_local
;
1288 addr
= ifa
->ifa_local
;
1294 master_idx
= l3mdev_master_ifindex_rcu(dev
);
1296 /* For VRFs, the VRF device takes the place of the loopback device,
1297 * with addresses on it being preferred. Note in such cases the
1298 * loopback device will be among the devices that fail the master_idx
1299 * equality check in the loop below.
1302 (dev
= dev_get_by_index_rcu(net
, master_idx
)) &&
1303 (in_dev
= __in_dev_get_rcu(dev
))) {
1304 addr
= in_dev_select_addr(in_dev
, scope
);
1309 /* Not loopback addresses on loopback should be preferred
1310 in this case. It is important that lo is the first interface
1313 for_each_netdev_rcu(net
, dev
) {
1314 if (l3mdev_master_ifindex_rcu(dev
) != master_idx
)
1317 in_dev
= __in_dev_get_rcu(dev
);
1321 addr
= in_dev_select_addr(in_dev
, scope
);
1329 EXPORT_SYMBOL(inet_select_addr
);
1331 static __be32
confirm_addr_indev(struct in_device
*in_dev
, __be32 dst
,
1332 __be32 local
, int scope
)
1334 const struct in_ifaddr
*ifa
;
1338 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
1340 (local
== ifa
->ifa_local
|| !local
) &&
1341 ifa
->ifa_scope
<= scope
) {
1342 addr
= ifa
->ifa_local
;
1347 same
= (!local
|| inet_ifa_match(local
, ifa
)) &&
1348 (!dst
|| inet_ifa_match(dst
, ifa
));
1352 /* Is the selected addr into dst subnet? */
1353 if (inet_ifa_match(addr
, ifa
))
1355 /* No, then can we use new local src? */
1356 if (ifa
->ifa_scope
<= scope
) {
1357 addr
= ifa
->ifa_local
;
1360 /* search for large dst subnet for addr */
1366 return same
? addr
: 0;
1370 * Confirm that local IP address exists using wildcards:
1371 * - net: netns to check, cannot be NULL
1372 * - in_dev: only on this interface, NULL=any interface
1373 * - dst: only in the same subnet as dst, 0=any dst
1374 * - local: address, 0=autoselect the local address
1375 * - scope: maximum allowed scope value for the local address
1377 __be32
inet_confirm_addr(struct net
*net
, struct in_device
*in_dev
,
1378 __be32 dst
, __be32 local
, int scope
)
1381 struct net_device
*dev
;
1384 return confirm_addr_indev(in_dev
, dst
, local
, scope
);
1387 for_each_netdev_rcu(net
, dev
) {
1388 in_dev
= __in_dev_get_rcu(dev
);
1390 addr
= confirm_addr_indev(in_dev
, dst
, local
, scope
);
1399 EXPORT_SYMBOL(inet_confirm_addr
);
1405 int register_inetaddr_notifier(struct notifier_block
*nb
)
1407 return blocking_notifier_chain_register(&inetaddr_chain
, nb
);
1409 EXPORT_SYMBOL(register_inetaddr_notifier
);
1411 int unregister_inetaddr_notifier(struct notifier_block
*nb
)
1413 return blocking_notifier_chain_unregister(&inetaddr_chain
, nb
);
1415 EXPORT_SYMBOL(unregister_inetaddr_notifier
);
1417 int register_inetaddr_validator_notifier(struct notifier_block
*nb
)
1419 return blocking_notifier_chain_register(&inetaddr_validator_chain
, nb
);
1421 EXPORT_SYMBOL(register_inetaddr_validator_notifier
);
1423 int unregister_inetaddr_validator_notifier(struct notifier_block
*nb
)
1425 return blocking_notifier_chain_unregister(&inetaddr_validator_chain
,
1428 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier
);
1430 /* Rename ifa_labels for a device name change. Make some effort to preserve
1431 * existing alias numbering and to create unique labels if possible.
1433 static void inetdev_changename(struct net_device
*dev
, struct in_device
*in_dev
)
1435 struct in_ifaddr
*ifa
;
1438 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
1439 char old
[IFNAMSIZ
], *dot
;
1441 memcpy(old
, ifa
->ifa_label
, IFNAMSIZ
);
1442 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1445 dot
= strchr(old
, ':');
1447 sprintf(old
, ":%d", named
);
1450 if (strlen(dot
) + strlen(dev
->name
) < IFNAMSIZ
)
1451 strcat(ifa
->ifa_label
, dot
);
1453 strcpy(ifa
->ifa_label
+ (IFNAMSIZ
- strlen(dot
) - 1), dot
);
1455 rtmsg_ifa(RTM_NEWADDR
, ifa
, NULL
, 0);
1459 static bool inetdev_valid_mtu(unsigned int mtu
)
1461 return mtu
>= IPV4_MIN_MTU
;
1464 static void inetdev_send_gratuitous_arp(struct net_device
*dev
,
1465 struct in_device
*in_dev
)
1468 const struct in_ifaddr
*ifa
;
1470 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
1471 arp_send(ARPOP_REQUEST
, ETH_P_ARP
,
1472 ifa
->ifa_local
, dev
,
1473 ifa
->ifa_local
, NULL
,
1474 dev
->dev_addr
, NULL
);
1478 /* Called only under RTNL semaphore */
1480 static int inetdev_event(struct notifier_block
*this, unsigned long event
,
1483 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1484 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
1489 if (event
== NETDEV_REGISTER
) {
1490 in_dev
= inetdev_init(dev
);
1492 return notifier_from_errno(PTR_ERR(in_dev
));
1493 if (dev
->flags
& IFF_LOOPBACK
) {
1494 IN_DEV_CONF_SET(in_dev
, NOXFRM
, 1);
1495 IN_DEV_CONF_SET(in_dev
, NOPOLICY
, 1);
1497 } else if (event
== NETDEV_CHANGEMTU
) {
1498 /* Re-enabling IP */
1499 if (inetdev_valid_mtu(dev
->mtu
))
1500 in_dev
= inetdev_init(dev
);
1506 case NETDEV_REGISTER
:
1507 pr_debug("%s: bug\n", __func__
);
1508 RCU_INIT_POINTER(dev
->ip_ptr
, NULL
);
1511 if (!inetdev_valid_mtu(dev
->mtu
))
1513 if (dev
->flags
& IFF_LOOPBACK
) {
1514 struct in_ifaddr
*ifa
= inet_alloc_ifa();
1517 INIT_HLIST_NODE(&ifa
->hash
);
1519 ifa
->ifa_address
= htonl(INADDR_LOOPBACK
);
1520 ifa
->ifa_prefixlen
= 8;
1521 ifa
->ifa_mask
= inet_make_mask(8);
1522 in_dev_hold(in_dev
);
1523 ifa
->ifa_dev
= in_dev
;
1524 ifa
->ifa_scope
= RT_SCOPE_HOST
;
1525 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
1526 set_ifa_lifetime(ifa
, INFINITY_LIFE_TIME
,
1527 INFINITY_LIFE_TIME
);
1528 ipv4_devconf_setall(in_dev
);
1529 neigh_parms_data_state_setall(in_dev
->arp_parms
);
1530 inet_insert_ifa(ifa
);
1535 case NETDEV_CHANGEADDR
:
1536 if (!IN_DEV_ARP_NOTIFY(in_dev
))
1539 case NETDEV_NOTIFY_PEERS
:
1540 /* Send gratuitous ARP to notify of link change */
1541 inetdev_send_gratuitous_arp(dev
, in_dev
);
1546 case NETDEV_PRE_TYPE_CHANGE
:
1547 ip_mc_unmap(in_dev
);
1549 case NETDEV_POST_TYPE_CHANGE
:
1550 ip_mc_remap(in_dev
);
1552 case NETDEV_CHANGEMTU
:
1553 if (inetdev_valid_mtu(dev
->mtu
))
1555 /* disable IP when MTU is not enough */
1557 case NETDEV_UNREGISTER
:
1558 inetdev_destroy(in_dev
);
1560 case NETDEV_CHANGENAME
:
1561 /* Do not notify about label change, this event is
1562 * not interesting to applications using netlink.
1564 inetdev_changename(dev
, in_dev
);
1566 devinet_sysctl_unregister(in_dev
);
1567 devinet_sysctl_register(in_dev
);
1574 static struct notifier_block ip_netdev_notifier
= {
1575 .notifier_call
= inetdev_event
,
1578 static size_t inet_nlmsg_size(void)
1580 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
1581 + nla_total_size(4) /* IFA_ADDRESS */
1582 + nla_total_size(4) /* IFA_LOCAL */
1583 + nla_total_size(4) /* IFA_BROADCAST */
1584 + nla_total_size(IFNAMSIZ
) /* IFA_LABEL */
1585 + nla_total_size(4) /* IFA_FLAGS */
1586 + nla_total_size(4) /* IFA_RT_PRIORITY */
1587 + nla_total_size(sizeof(struct ifa_cacheinfo
)); /* IFA_CACHEINFO */
1590 static inline u32
cstamp_delta(unsigned long cstamp
)
1592 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
1595 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
1596 unsigned long tstamp
, u32 preferred
, u32 valid
)
1598 struct ifa_cacheinfo ci
;
1600 ci
.cstamp
= cstamp_delta(cstamp
);
1601 ci
.tstamp
= cstamp_delta(tstamp
);
1602 ci
.ifa_prefered
= preferred
;
1603 ci
.ifa_valid
= valid
;
1605 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
1608 static int inet_fill_ifaddr(struct sk_buff
*skb
, struct in_ifaddr
*ifa
,
1609 struct inet_fill_args
*args
)
1611 struct ifaddrmsg
*ifm
;
1612 struct nlmsghdr
*nlh
;
1613 u32 preferred
, valid
;
1615 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
, sizeof(*ifm
),
1620 ifm
= nlmsg_data(nlh
);
1621 ifm
->ifa_family
= AF_INET
;
1622 ifm
->ifa_prefixlen
= ifa
->ifa_prefixlen
;
1623 ifm
->ifa_flags
= ifa
->ifa_flags
;
1624 ifm
->ifa_scope
= ifa
->ifa_scope
;
1625 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
1627 if (args
->netnsid
>= 0 &&
1628 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
))
1629 goto nla_put_failure
;
1631 if (!(ifm
->ifa_flags
& IFA_F_PERMANENT
)) {
1632 preferred
= ifa
->ifa_preferred_lft
;
1633 valid
= ifa
->ifa_valid_lft
;
1634 if (preferred
!= INFINITY_LIFE_TIME
) {
1635 long tval
= (jiffies
- ifa
->ifa_tstamp
) / HZ
;
1637 if (preferred
> tval
)
1641 if (valid
!= INFINITY_LIFE_TIME
) {
1649 preferred
= INFINITY_LIFE_TIME
;
1650 valid
= INFINITY_LIFE_TIME
;
1652 if ((ifa
->ifa_address
&&
1653 nla_put_in_addr(skb
, IFA_ADDRESS
, ifa
->ifa_address
)) ||
1655 nla_put_in_addr(skb
, IFA_LOCAL
, ifa
->ifa_local
)) ||
1656 (ifa
->ifa_broadcast
&&
1657 nla_put_in_addr(skb
, IFA_BROADCAST
, ifa
->ifa_broadcast
)) ||
1658 (ifa
->ifa_label
[0] &&
1659 nla_put_string(skb
, IFA_LABEL
, ifa
->ifa_label
)) ||
1660 nla_put_u32(skb
, IFA_FLAGS
, ifa
->ifa_flags
) ||
1661 (ifa
->ifa_rt_priority
&&
1662 nla_put_u32(skb
, IFA_RT_PRIORITY
, ifa
->ifa_rt_priority
)) ||
1663 put_cacheinfo(skb
, ifa
->ifa_cstamp
, ifa
->ifa_tstamp
,
1665 goto nla_put_failure
;
1667 nlmsg_end(skb
, nlh
);
1671 nlmsg_cancel(skb
, nlh
);
1675 static int inet_valid_dump_ifaddr_req(const struct nlmsghdr
*nlh
,
1676 struct inet_fill_args
*fillargs
,
1677 struct net
**tgt_net
, struct sock
*sk
,
1678 struct netlink_callback
*cb
)
1680 struct netlink_ext_ack
*extack
= cb
->extack
;
1681 struct nlattr
*tb
[IFA_MAX
+1];
1682 struct ifaddrmsg
*ifm
;
1685 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
1686 NL_SET_ERR_MSG(extack
, "ipv4: Invalid header for address dump request");
1690 ifm
= nlmsg_data(nlh
);
1691 if (ifm
->ifa_prefixlen
|| ifm
->ifa_flags
|| ifm
->ifa_scope
) {
1692 NL_SET_ERR_MSG(extack
, "ipv4: Invalid values in header for address dump request");
1696 fillargs
->ifindex
= ifm
->ifa_index
;
1697 if (fillargs
->ifindex
) {
1698 cb
->answer_flags
|= NLM_F_DUMP_FILTERED
;
1699 fillargs
->flags
|= NLM_F_DUMP_FILTERED
;
1702 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
1703 ifa_ipv4_policy
, extack
);
1707 for (i
= 0; i
<= IFA_MAX
; ++i
) {
1711 if (i
== IFA_TARGET_NETNSID
) {
1714 fillargs
->netnsid
= nla_get_s32(tb
[i
]);
1716 net
= rtnl_get_net_ns_capable(sk
, fillargs
->netnsid
);
1718 fillargs
->netnsid
= -1;
1719 NL_SET_ERR_MSG(extack
, "ipv4: Invalid target network namespace id");
1720 return PTR_ERR(net
);
1724 NL_SET_ERR_MSG(extack
, "ipv4: Unsupported attribute in dump request");
1732 static int in_dev_dump_addr(struct in_device
*in_dev
, struct sk_buff
*skb
,
1733 struct netlink_callback
*cb
, int s_ip_idx
,
1734 struct inet_fill_args
*fillargs
)
1736 struct in_ifaddr
*ifa
;
1740 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
1741 if (ip_idx
< s_ip_idx
) {
1745 err
= inet_fill_ifaddr(skb
, ifa
, fillargs
);
1749 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1755 cb
->args
[2] = ip_idx
;
1760 static int inet_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1762 const struct nlmsghdr
*nlh
= cb
->nlh
;
1763 struct inet_fill_args fillargs
= {
1764 .portid
= NETLINK_CB(cb
->skb
).portid
,
1765 .seq
= nlh
->nlmsg_seq
,
1766 .event
= RTM_NEWADDR
,
1767 .flags
= NLM_F_MULTI
,
1770 struct net
*net
= sock_net(skb
->sk
);
1771 struct net
*tgt_net
= net
;
1775 struct net_device
*dev
;
1776 struct in_device
*in_dev
;
1777 struct hlist_head
*head
;
1781 s_idx
= idx
= cb
->args
[1];
1782 s_ip_idx
= cb
->args
[2];
1784 if (cb
->strict_check
) {
1785 err
= inet_valid_dump_ifaddr_req(nlh
, &fillargs
, &tgt_net
,
1791 if (fillargs
.ifindex
) {
1792 dev
= __dev_get_by_index(tgt_net
, fillargs
.ifindex
);
1798 in_dev
= __in_dev_get_rtnl(dev
);
1800 err
= in_dev_dump_addr(in_dev
, skb
, cb
, s_ip_idx
,
1807 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1809 head
= &tgt_net
->dev_index_head
[h
];
1811 cb
->seq
= atomic_read(&tgt_net
->ipv4
.dev_addr_genid
) ^
1812 tgt_net
->dev_base_seq
;
1813 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
1816 if (h
> s_h
|| idx
> s_idx
)
1818 in_dev
= __in_dev_get_rcu(dev
);
1822 err
= in_dev_dump_addr(in_dev
, skb
, cb
, s_ip_idx
,
1838 if (fillargs
.netnsid
>= 0)
1841 return skb
->len
? : err
;
1844 static void rtmsg_ifa(int event
, struct in_ifaddr
*ifa
, struct nlmsghdr
*nlh
,
1847 struct inet_fill_args fillargs
= {
1849 .seq
= nlh
? nlh
->nlmsg_seq
: 0,
1854 struct sk_buff
*skb
;
1858 net
= dev_net(ifa
->ifa_dev
->dev
);
1859 skb
= nlmsg_new(inet_nlmsg_size(), GFP_KERNEL
);
1863 err
= inet_fill_ifaddr(skb
, ifa
, &fillargs
);
1865 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1866 WARN_ON(err
== -EMSGSIZE
);
1870 rtnl_notify(skb
, net
, portid
, RTNLGRP_IPV4_IFADDR
, nlh
, GFP_KERNEL
);
1874 rtnl_set_sk_err(net
, RTNLGRP_IPV4_IFADDR
, err
);
1877 static size_t inet_get_link_af_size(const struct net_device
*dev
,
1878 u32 ext_filter_mask
)
1880 struct in_device
*in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
1885 return nla_total_size(IPV4_DEVCONF_MAX
* 4); /* IFLA_INET_CONF */
1888 static int inet_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
,
1889 u32 ext_filter_mask
)
1891 struct in_device
*in_dev
= rcu_dereference_rtnl(dev
->ip_ptr
);
1898 nla
= nla_reserve(skb
, IFLA_INET_CONF
, IPV4_DEVCONF_MAX
* 4);
1902 for (i
= 0; i
< IPV4_DEVCONF_MAX
; i
++)
1903 ((u32
*) nla_data(nla
))[i
] = in_dev
->cnf
.data
[i
];
1908 static const struct nla_policy inet_af_policy
[IFLA_INET_MAX
+1] = {
1909 [IFLA_INET_CONF
] = { .type
= NLA_NESTED
},
1912 static int inet_validate_link_af(const struct net_device
*dev
,
1913 const struct nlattr
*nla
)
1915 struct nlattr
*a
, *tb
[IFLA_INET_MAX
+1];
1918 if (dev
&& !__in_dev_get_rcu(dev
))
1919 return -EAFNOSUPPORT
;
1921 err
= nla_parse_nested_deprecated(tb
, IFLA_INET_MAX
, nla
,
1922 inet_af_policy
, NULL
);
1926 if (tb
[IFLA_INET_CONF
]) {
1927 nla_for_each_nested(a
, tb
[IFLA_INET_CONF
], rem
) {
1928 int cfgid
= nla_type(a
);
1933 if (cfgid
<= 0 || cfgid
> IPV4_DEVCONF_MAX
)
1941 static int inet_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
1943 struct in_device
*in_dev
= __in_dev_get_rcu(dev
);
1944 struct nlattr
*a
, *tb
[IFLA_INET_MAX
+1];
1948 return -EAFNOSUPPORT
;
1950 if (nla_parse_nested_deprecated(tb
, IFLA_INET_MAX
, nla
, NULL
, NULL
) < 0)
1953 if (tb
[IFLA_INET_CONF
]) {
1954 nla_for_each_nested(a
, tb
[IFLA_INET_CONF
], rem
)
1955 ipv4_devconf_set(in_dev
, nla_type(a
), nla_get_u32(a
));
1961 static int inet_netconf_msgsize_devconf(int type
)
1963 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
1964 + nla_total_size(4); /* NETCONFA_IFINDEX */
1967 if (type
== NETCONFA_ALL
)
1970 if (all
|| type
== NETCONFA_FORWARDING
)
1971 size
+= nla_total_size(4);
1972 if (all
|| type
== NETCONFA_RP_FILTER
)
1973 size
+= nla_total_size(4);
1974 if (all
|| type
== NETCONFA_MC_FORWARDING
)
1975 size
+= nla_total_size(4);
1976 if (all
|| type
== NETCONFA_BC_FORWARDING
)
1977 size
+= nla_total_size(4);
1978 if (all
|| type
== NETCONFA_PROXY_NEIGH
)
1979 size
+= nla_total_size(4);
1980 if (all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
)
1981 size
+= nla_total_size(4);
1986 static int inet_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
1987 struct ipv4_devconf
*devconf
, u32 portid
,
1988 u32 seq
, int event
, unsigned int flags
,
1991 struct nlmsghdr
*nlh
;
1992 struct netconfmsg
*ncm
;
1995 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
2000 if (type
== NETCONFA_ALL
)
2003 ncm
= nlmsg_data(nlh
);
2004 ncm
->ncm_family
= AF_INET
;
2006 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
2007 goto nla_put_failure
;
2012 if ((all
|| type
== NETCONFA_FORWARDING
) &&
2013 nla_put_s32(skb
, NETCONFA_FORWARDING
,
2014 IPV4_DEVCONF(*devconf
, FORWARDING
)) < 0)
2015 goto nla_put_failure
;
2016 if ((all
|| type
== NETCONFA_RP_FILTER
) &&
2017 nla_put_s32(skb
, NETCONFA_RP_FILTER
,
2018 IPV4_DEVCONF(*devconf
, RP_FILTER
)) < 0)
2019 goto nla_put_failure
;
2020 if ((all
|| type
== NETCONFA_MC_FORWARDING
) &&
2021 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
2022 IPV4_DEVCONF(*devconf
, MC_FORWARDING
)) < 0)
2023 goto nla_put_failure
;
2024 if ((all
|| type
== NETCONFA_BC_FORWARDING
) &&
2025 nla_put_s32(skb
, NETCONFA_BC_FORWARDING
,
2026 IPV4_DEVCONF(*devconf
, BC_FORWARDING
)) < 0)
2027 goto nla_put_failure
;
2028 if ((all
|| type
== NETCONFA_PROXY_NEIGH
) &&
2029 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
,
2030 IPV4_DEVCONF(*devconf
, PROXY_ARP
)) < 0)
2031 goto nla_put_failure
;
2032 if ((all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
) &&
2033 nla_put_s32(skb
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
2034 IPV4_DEVCONF(*devconf
, IGNORE_ROUTES_WITH_LINKDOWN
)) < 0)
2035 goto nla_put_failure
;
2038 nlmsg_end(skb
, nlh
);
2042 nlmsg_cancel(skb
, nlh
);
2046 void inet_netconf_notify_devconf(struct net
*net
, int event
, int type
,
2047 int ifindex
, struct ipv4_devconf
*devconf
)
2049 struct sk_buff
*skb
;
2052 skb
= nlmsg_new(inet_netconf_msgsize_devconf(type
), GFP_KERNEL
);
2056 err
= inet_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
2059 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2060 WARN_ON(err
== -EMSGSIZE
);
2064 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_NETCONF
, NULL
, GFP_KERNEL
);
2068 rtnl_set_sk_err(net
, RTNLGRP_IPV4_NETCONF
, err
);
2071 static const struct nla_policy devconf_ipv4_policy
[NETCONFA_MAX
+1] = {
2072 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
2073 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
2074 [NETCONFA_RP_FILTER
] = { .len
= sizeof(int) },
2075 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
2076 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
] = { .len
= sizeof(int) },
2079 static int inet_netconf_valid_get_req(struct sk_buff
*skb
,
2080 const struct nlmsghdr
*nlh
,
2082 struct netlink_ext_ack
*extack
)
2086 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(struct netconfmsg
))) {
2087 NL_SET_ERR_MSG(extack
, "ipv4: Invalid header for netconf get request");
2091 if (!netlink_strict_get_check(skb
))
2092 return nlmsg_parse_deprecated(nlh
, sizeof(struct netconfmsg
),
2094 devconf_ipv4_policy
, extack
);
2096 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct netconfmsg
),
2098 devconf_ipv4_policy
, extack
);
2102 for (i
= 0; i
<= NETCONFA_MAX
; i
++) {
2107 case NETCONFA_IFINDEX
:
2110 NL_SET_ERR_MSG(extack
, "ipv4: Unsupported attribute in netconf get request");
2118 static int inet_netconf_get_devconf(struct sk_buff
*in_skb
,
2119 struct nlmsghdr
*nlh
,
2120 struct netlink_ext_ack
*extack
)
2122 struct net
*net
= sock_net(in_skb
->sk
);
2123 struct nlattr
*tb
[NETCONFA_MAX
+1];
2124 struct sk_buff
*skb
;
2125 struct ipv4_devconf
*devconf
;
2126 struct in_device
*in_dev
;
2127 struct net_device
*dev
;
2131 err
= inet_netconf_valid_get_req(in_skb
, nlh
, tb
, extack
);
2136 if (!tb
[NETCONFA_IFINDEX
])
2139 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
2141 case NETCONFA_IFINDEX_ALL
:
2142 devconf
= net
->ipv4
.devconf_all
;
2144 case NETCONFA_IFINDEX_DEFAULT
:
2145 devconf
= net
->ipv4
.devconf_dflt
;
2148 dev
= __dev_get_by_index(net
, ifindex
);
2151 in_dev
= __in_dev_get_rtnl(dev
);
2154 devconf
= &in_dev
->cnf
;
2159 skb
= nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_KERNEL
);
2163 err
= inet_netconf_fill_devconf(skb
, ifindex
, devconf
,
2164 NETLINK_CB(in_skb
).portid
,
2165 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
2168 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2169 WARN_ON(err
== -EMSGSIZE
);
2173 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
2178 static int inet_netconf_dump_devconf(struct sk_buff
*skb
,
2179 struct netlink_callback
*cb
)
2181 const struct nlmsghdr
*nlh
= cb
->nlh
;
2182 struct net
*net
= sock_net(skb
->sk
);
2185 struct net_device
*dev
;
2186 struct in_device
*in_dev
;
2187 struct hlist_head
*head
;
2189 if (cb
->strict_check
) {
2190 struct netlink_ext_ack
*extack
= cb
->extack
;
2191 struct netconfmsg
*ncm
;
2193 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ncm
))) {
2194 NL_SET_ERR_MSG(extack
, "ipv4: Invalid header for netconf dump request");
2198 if (nlmsg_attrlen(nlh
, sizeof(*ncm
))) {
2199 NL_SET_ERR_MSG(extack
, "ipv4: Invalid data after header in netconf dump request");
2205 s_idx
= idx
= cb
->args
[1];
2207 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
2209 head
= &net
->dev_index_head
[h
];
2211 cb
->seq
= atomic_read(&net
->ipv4
.dev_addr_genid
) ^
2213 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
2216 in_dev
= __in_dev_get_rcu(dev
);
2220 if (inet_netconf_fill_devconf(skb
, dev
->ifindex
,
2222 NETLINK_CB(cb
->skb
).portid
,
2226 NETCONFA_ALL
) < 0) {
2230 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2236 if (h
== NETDEV_HASHENTRIES
) {
2237 if (inet_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
2238 net
->ipv4
.devconf_all
,
2239 NETLINK_CB(cb
->skb
).portid
,
2241 RTM_NEWNETCONF
, NLM_F_MULTI
,
2247 if (h
== NETDEV_HASHENTRIES
+ 1) {
2248 if (inet_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
2249 net
->ipv4
.devconf_dflt
,
2250 NETLINK_CB(cb
->skb
).portid
,
2252 RTM_NEWNETCONF
, NLM_F_MULTI
,
2265 #ifdef CONFIG_SYSCTL
2267 static void devinet_copy_dflt_conf(struct net
*net
, int i
)
2269 struct net_device
*dev
;
2272 for_each_netdev_rcu(net
, dev
) {
2273 struct in_device
*in_dev
;
2275 in_dev
= __in_dev_get_rcu(dev
);
2276 if (in_dev
&& !test_bit(i
, in_dev
->cnf
.state
))
2277 in_dev
->cnf
.data
[i
] = net
->ipv4
.devconf_dflt
->data
[i
];
2282 /* called with RTNL locked */
2283 static void inet_forward_change(struct net
*net
)
2285 struct net_device
*dev
;
2286 int on
= IPV4_DEVCONF_ALL(net
, FORWARDING
);
2288 IPV4_DEVCONF_ALL(net
, ACCEPT_REDIRECTS
) = !on
;
2289 IPV4_DEVCONF_DFLT(net
, FORWARDING
) = on
;
2290 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2291 NETCONFA_FORWARDING
,
2292 NETCONFA_IFINDEX_ALL
,
2293 net
->ipv4
.devconf_all
);
2294 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2295 NETCONFA_FORWARDING
,
2296 NETCONFA_IFINDEX_DEFAULT
,
2297 net
->ipv4
.devconf_dflt
);
2299 for_each_netdev(net
, dev
) {
2300 struct in_device
*in_dev
;
2303 dev_disable_lro(dev
);
2305 in_dev
= __in_dev_get_rtnl(dev
);
2307 IN_DEV_CONF_SET(in_dev
, FORWARDING
, on
);
2308 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2309 NETCONFA_FORWARDING
,
2310 dev
->ifindex
, &in_dev
->cnf
);
2315 static int devinet_conf_ifindex(struct net
*net
, struct ipv4_devconf
*cnf
)
2317 if (cnf
== net
->ipv4
.devconf_dflt
)
2318 return NETCONFA_IFINDEX_DEFAULT
;
2319 else if (cnf
== net
->ipv4
.devconf_all
)
2320 return NETCONFA_IFINDEX_ALL
;
2322 struct in_device
*idev
2323 = container_of(cnf
, struct in_device
, cnf
);
2324 return idev
->dev
->ifindex
;
2328 static int devinet_conf_proc(struct ctl_table
*ctl
, int write
,
2329 void __user
*buffer
,
2330 size_t *lenp
, loff_t
*ppos
)
2332 int old_value
= *(int *)ctl
->data
;
2333 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2334 int new_value
= *(int *)ctl
->data
;
2337 struct ipv4_devconf
*cnf
= ctl
->extra1
;
2338 struct net
*net
= ctl
->extra2
;
2339 int i
= (int *)ctl
->data
- cnf
->data
;
2342 set_bit(i
, cnf
->state
);
2344 if (cnf
== net
->ipv4
.devconf_dflt
)
2345 devinet_copy_dflt_conf(net
, i
);
2346 if (i
== IPV4_DEVCONF_ACCEPT_LOCAL
- 1 ||
2347 i
== IPV4_DEVCONF_ROUTE_LOCALNET
- 1)
2348 if ((new_value
== 0) && (old_value
!= 0))
2349 rt_cache_flush(net
);
2351 if (i
== IPV4_DEVCONF_BC_FORWARDING
- 1 &&
2352 new_value
!= old_value
)
2353 rt_cache_flush(net
);
2355 if (i
== IPV4_DEVCONF_RP_FILTER
- 1 &&
2356 new_value
!= old_value
) {
2357 ifindex
= devinet_conf_ifindex(net
, cnf
);
2358 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2362 if (i
== IPV4_DEVCONF_PROXY_ARP
- 1 &&
2363 new_value
!= old_value
) {
2364 ifindex
= devinet_conf_ifindex(net
, cnf
);
2365 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2366 NETCONFA_PROXY_NEIGH
,
2369 if (i
== IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN
- 1 &&
2370 new_value
!= old_value
) {
2371 ifindex
= devinet_conf_ifindex(net
, cnf
);
2372 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2373 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
2381 static int devinet_sysctl_forward(struct ctl_table
*ctl
, int write
,
2382 void __user
*buffer
,
2383 size_t *lenp
, loff_t
*ppos
)
2385 int *valp
= ctl
->data
;
2388 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2390 if (write
&& *valp
!= val
) {
2391 struct net
*net
= ctl
->extra2
;
2393 if (valp
!= &IPV4_DEVCONF_DFLT(net
, FORWARDING
)) {
2394 if (!rtnl_trylock()) {
2395 /* Restore the original values before restarting */
2398 return restart_syscall();
2400 if (valp
== &IPV4_DEVCONF_ALL(net
, FORWARDING
)) {
2401 inet_forward_change(net
);
2403 struct ipv4_devconf
*cnf
= ctl
->extra1
;
2404 struct in_device
*idev
=
2405 container_of(cnf
, struct in_device
, cnf
);
2407 dev_disable_lro(idev
->dev
);
2408 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2409 NETCONFA_FORWARDING
,
2414 rt_cache_flush(net
);
2416 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
2417 NETCONFA_FORWARDING
,
2418 NETCONFA_IFINDEX_DEFAULT
,
2419 net
->ipv4
.devconf_dflt
);
2425 static int ipv4_doint_and_flush(struct ctl_table
*ctl
, int write
,
2426 void __user
*buffer
,
2427 size_t *lenp
, loff_t
*ppos
)
2429 int *valp
= ctl
->data
;
2431 int ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2432 struct net
*net
= ctl
->extra2
;
2434 if (write
&& *valp
!= val
)
2435 rt_cache_flush(net
);
2440 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2443 .data = ipv4_devconf.data + \
2444 IPV4_DEVCONF_ ## attr - 1, \
2445 .maxlen = sizeof(int), \
2447 .proc_handler = proc, \
2448 .extra1 = &ipv4_devconf, \
2451 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2452 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2454 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2455 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2457 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2458 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2460 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2461 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2463 static struct devinet_sysctl_table
{
2464 struct ctl_table_header
*sysctl_header
;
2465 struct ctl_table devinet_vars
[__IPV4_DEVCONF_MAX
];
2466 } devinet_sysctl
= {
2468 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING
, "forwarding",
2469 devinet_sysctl_forward
),
2470 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING
, "mc_forwarding"),
2471 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING
, "bc_forwarding"),
2473 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS
, "accept_redirects"),
2474 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS
, "secure_redirects"),
2475 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA
, "shared_media"),
2476 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER
, "rp_filter"),
2477 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS
, "send_redirects"),
2478 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE
,
2479 "accept_source_route"),
2480 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL
, "accept_local"),
2481 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK
, "src_valid_mark"),
2482 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP
, "proxy_arp"),
2483 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID
, "medium_id"),
2484 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY
, "bootp_relay"),
2485 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS
, "log_martians"),
2486 DEVINET_SYSCTL_RW_ENTRY(TAG
, "tag"),
2487 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER
, "arp_filter"),
2488 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE
, "arp_announce"),
2489 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE
, "arp_ignore"),
2490 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT
, "arp_accept"),
2491 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY
, "arp_notify"),
2492 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN
, "proxy_arp_pvlan"),
2493 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION
,
2494 "force_igmp_version"),
2495 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL
,
2496 "igmpv2_unsolicited_report_interval"),
2497 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL
,
2498 "igmpv3_unsolicited_report_interval"),
2499 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN
,
2500 "ignore_routes_with_linkdown"),
2501 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP
,
2502 "drop_gratuitous_arp"),
2504 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM
, "disable_xfrm"),
2505 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY
, "disable_policy"),
2506 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES
,
2507 "promote_secondaries"),
2508 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET
,
2510 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST
,
2511 "drop_unicast_in_l2_multicast"),
2515 static int __devinet_sysctl_register(struct net
*net
, char *dev_name
,
2516 int ifindex
, struct ipv4_devconf
*p
)
2519 struct devinet_sysctl_table
*t
;
2520 char path
[sizeof("net/ipv4/conf/") + IFNAMSIZ
];
2522 t
= kmemdup(&devinet_sysctl
, sizeof(*t
), GFP_KERNEL
);
2526 for (i
= 0; i
< ARRAY_SIZE(t
->devinet_vars
) - 1; i
++) {
2527 t
->devinet_vars
[i
].data
+= (char *)p
- (char *)&ipv4_devconf
;
2528 t
->devinet_vars
[i
].extra1
= p
;
2529 t
->devinet_vars
[i
].extra2
= net
;
2532 snprintf(path
, sizeof(path
), "net/ipv4/conf/%s", dev_name
);
2534 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->devinet_vars
);
2535 if (!t
->sysctl_header
)
2540 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
, NETCONFA_ALL
,
2550 static void __devinet_sysctl_unregister(struct net
*net
,
2551 struct ipv4_devconf
*cnf
, int ifindex
)
2553 struct devinet_sysctl_table
*t
= cnf
->sysctl
;
2557 unregister_net_sysctl_table(t
->sysctl_header
);
2561 inet_netconf_notify_devconf(net
, RTM_DELNETCONF
, 0, ifindex
, NULL
);
2564 static int devinet_sysctl_register(struct in_device
*idev
)
2568 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
2571 err
= neigh_sysctl_register(idev
->dev
, idev
->arp_parms
, NULL
);
2574 err
= __devinet_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
2575 idev
->dev
->ifindex
, &idev
->cnf
);
2577 neigh_sysctl_unregister(idev
->arp_parms
);
2581 static void devinet_sysctl_unregister(struct in_device
*idev
)
2583 struct net
*net
= dev_net(idev
->dev
);
2585 __devinet_sysctl_unregister(net
, &idev
->cnf
, idev
->dev
->ifindex
);
2586 neigh_sysctl_unregister(idev
->arp_parms
);
2589 static struct ctl_table ctl_forward_entry
[] = {
2591 .procname
= "ip_forward",
2592 .data
= &ipv4_devconf
.data
[
2593 IPV4_DEVCONF_FORWARDING
- 1],
2594 .maxlen
= sizeof(int),
2596 .proc_handler
= devinet_sysctl_forward
,
2597 .extra1
= &ipv4_devconf
,
2598 .extra2
= &init_net
,
2604 static __net_init
int devinet_init_net(struct net
*net
)
2607 struct ipv4_devconf
*all
, *dflt
;
2608 #ifdef CONFIG_SYSCTL
2609 struct ctl_table
*tbl
;
2610 struct ctl_table_header
*forw_hdr
;
2614 all
= kmemdup(&ipv4_devconf
, sizeof(ipv4_devconf
), GFP_KERNEL
);
2618 dflt
= kmemdup(&ipv4_devconf_dflt
, sizeof(ipv4_devconf_dflt
), GFP_KERNEL
);
2620 goto err_alloc_dflt
;
2622 #ifdef CONFIG_SYSCTL
2623 tbl
= kmemdup(ctl_forward_entry
, sizeof(ctl_forward_entry
), GFP_KERNEL
);
2627 tbl
[0].data
= &all
->data
[IPV4_DEVCONF_FORWARDING
- 1];
2628 tbl
[0].extra1
= all
;
2629 tbl
[0].extra2
= net
;
2632 if ((!IS_ENABLED(CONFIG_SYSCTL
) ||
2633 sysctl_devconf_inherit_init_net
!= 2) &&
2634 !net_eq(net
, &init_net
)) {
2635 memcpy(all
, init_net
.ipv4
.devconf_all
, sizeof(ipv4_devconf
));
2636 memcpy(dflt
, init_net
.ipv4
.devconf_dflt
, sizeof(ipv4_devconf_dflt
));
2639 #ifdef CONFIG_SYSCTL
2640 err
= __devinet_sysctl_register(net
, "all", NETCONFA_IFINDEX_ALL
, all
);
2644 err
= __devinet_sysctl_register(net
, "default",
2645 NETCONFA_IFINDEX_DEFAULT
, dflt
);
2650 forw_hdr
= register_net_sysctl(net
, "net/ipv4", tbl
);
2653 net
->ipv4
.forw_hdr
= forw_hdr
;
2656 net
->ipv4
.devconf_all
= all
;
2657 net
->ipv4
.devconf_dflt
= dflt
;
2660 #ifdef CONFIG_SYSCTL
2662 __devinet_sysctl_unregister(net
, dflt
, NETCONFA_IFINDEX_DEFAULT
);
2664 __devinet_sysctl_unregister(net
, all
, NETCONFA_IFINDEX_ALL
);
2676 static __net_exit
void devinet_exit_net(struct net
*net
)
2678 #ifdef CONFIG_SYSCTL
2679 struct ctl_table
*tbl
;
2681 tbl
= net
->ipv4
.forw_hdr
->ctl_table_arg
;
2682 unregister_net_sysctl_table(net
->ipv4
.forw_hdr
);
2683 __devinet_sysctl_unregister(net
, net
->ipv4
.devconf_dflt
,
2684 NETCONFA_IFINDEX_DEFAULT
);
2685 __devinet_sysctl_unregister(net
, net
->ipv4
.devconf_all
,
2686 NETCONFA_IFINDEX_ALL
);
2689 kfree(net
->ipv4
.devconf_dflt
);
2690 kfree(net
->ipv4
.devconf_all
);
2693 static __net_initdata
struct pernet_operations devinet_ops
= {
2694 .init
= devinet_init_net
,
2695 .exit
= devinet_exit_net
,
2698 static struct rtnl_af_ops inet_af_ops __read_mostly
= {
2700 .fill_link_af
= inet_fill_link_af
,
2701 .get_link_af_size
= inet_get_link_af_size
,
2702 .validate_link_af
= inet_validate_link_af
,
2703 .set_link_af
= inet_set_link_af
,
2706 void __init
devinet_init(void)
2710 for (i
= 0; i
< IN4_ADDR_HSIZE
; i
++)
2711 INIT_HLIST_HEAD(&inet_addr_lst
[i
]);
2713 register_pernet_subsys(&devinet_ops
);
2715 register_gifconf(PF_INET
, inet_gifconf
);
2716 register_netdevice_notifier(&ip_netdev_notifier
);
2718 queue_delayed_work(system_power_efficient_wq
, &check_lifetime_work
, 0);
2720 rtnl_af_register(&inet_af_ops
);
2722 rtnl_register(PF_INET
, RTM_NEWADDR
, inet_rtm_newaddr
, NULL
, 0);
2723 rtnl_register(PF_INET
, RTM_DELADDR
, inet_rtm_deladdr
, NULL
, 0);
2724 rtnl_register(PF_INET
, RTM_GETADDR
, NULL
, inet_dump_ifaddr
, 0);
2725 rtnl_register(PF_INET
, RTM_GETNETCONF
, inet_netconf_get_devconf
,
2726 inet_netconf_dump_devconf
, 0);