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 #define IPV6ONLY_FLAGS \
70 (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \
71 IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \
72 IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY)
74 static struct ipv4_devconf ipv4_devconf = {
76 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
77 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
78 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
79 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
80 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
81 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
85 static struct ipv4_devconf ipv4_devconf_dflt = {
87 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
88 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
89 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
90 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
91 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
92 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
93 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
97 #define IPV4_DEVCONF_DFLT(net, attr) \
98 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
100 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
101 [IFA_LOCAL] = { .type = NLA_U32 },
102 [IFA_ADDRESS] = { .type = NLA_U32 },
103 [IFA_BROADCAST] = { .type = NLA_U32 },
104 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
105 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
106 [IFA_FLAGS] = { .type = NLA_U32 },
107 [IFA_RT_PRIORITY] = { .type = NLA_U32 },
110 #define IN4_ADDR_HSIZE_SHIFT 8
111 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
113 static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
115 static u32 inet_addr_hash(const struct net *net, __be32 addr)
117 u32 val = (__force u32) addr ^ net_hash_mix(net);
119 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
122 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
124 u32 hash = inet_addr_hash(net, ifa->ifa_local);
127 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
130 static void inet_hash_remove(struct in_ifaddr *ifa)
133 hlist_del_init_rcu(&ifa->hash);
137 * __ip_dev_find - find the first device with a given source address.
138 * @net: the net namespace
139 * @addr: the source address
140 * @devref: if true, take a reference on the found device
142 * If a caller uses devref=false, it should be protected by RCU, or RTNL
144 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
146 struct net_device *result = NULL;
147 struct in_ifaddr *ifa;
150 ifa = inet_lookup_ifaddr_rcu(net, addr);
152 struct flowi4 fl4 = { .daddr = addr };
153 struct fib_result res = { 0 };
154 struct fib_table *local;
156 /* Fallback to FIB local table so that communication
157 * over loopback subnets work.
159 local = fib_get_table(net, RT_TABLE_LOCAL);
161 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
162 res.type == RTN_LOCAL)
163 result = FIB_RES_DEV(res);
165 result = ifa->ifa_dev->dev;
167 if (result && devref)
172 EXPORT_SYMBOL(__ip_dev_find);
174 /* called under RCU lock */
175 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
177 u32 hash = inet_addr_hash(net, addr);
178 struct in_ifaddr *ifa;
180 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
181 if (ifa->ifa_local == addr &&
182 net_eq(dev_net(ifa->ifa_dev->dev), net))
188 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
190 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
191 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
192 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
195 static int devinet_sysctl_register(struct in_device *idev);
196 static void devinet_sysctl_unregister(struct in_device *idev);
198 static int devinet_sysctl_register(struct in_device *idev)
202 static void devinet_sysctl_unregister(struct in_device *idev)
207 /* Locks all the inet devices. */
209 static struct in_ifaddr *inet_alloc_ifa(void)
211 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
214 static void inet_rcu_free_ifa(struct rcu_head *head)
216 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
218 in_dev_put(ifa->ifa_dev);
222 static void inet_free_ifa(struct in_ifaddr *ifa)
224 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
227 void in_dev_finish_destroy(struct in_device *idev)
229 struct net_device *dev = idev->dev;
231 WARN_ON(idev->ifa_list);
232 WARN_ON(idev->mc_list);
233 kfree(rcu_dereference_protected(idev->mc_hash, 1));
234 #ifdef NET_REFCNT_DEBUG
235 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
239 pr_err("Freeing alive in_device %p\n", idev);
243 EXPORT_SYMBOL(in_dev_finish_destroy);
245 static struct in_device *inetdev_init(struct net_device *dev)
247 struct in_device *in_dev;
252 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
255 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
256 sizeof(in_dev->cnf));
257 in_dev->cnf.sysctl = NULL;
259 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
260 if (!in_dev->arp_parms)
262 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
263 dev_disable_lro(dev);
264 /* Reference in_dev->dev */
266 /* Account for reference dev->ip_ptr (below) */
267 refcount_set(&in_dev->refcnt, 1);
269 err = devinet_sysctl_register(in_dev);
272 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
277 ip_mc_init_dev(in_dev);
278 if (dev->flags & IFF_UP)
281 /* we can receive as soon as ip_ptr is set -- do this last */
282 rcu_assign_pointer(dev->ip_ptr, in_dev);
284 return in_dev ?: ERR_PTR(err);
291 static void in_dev_rcu_put(struct rcu_head *head)
293 struct in_device *idev = container_of(head, struct in_device, rcu_head);
297 static void inetdev_destroy(struct in_device *in_dev)
299 struct in_ifaddr *ifa;
300 struct net_device *dev;
308 ip_mc_destroy_dev(in_dev);
310 while ((ifa = in_dev->ifa_list) != NULL) {
311 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
315 RCU_INIT_POINTER(dev->ip_ptr, NULL);
317 devinet_sysctl_unregister(in_dev);
318 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
321 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
324 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
327 for_primary_ifa(in_dev) {
328 if (inet_ifa_match(a, ifa)) {
329 if (!b || inet_ifa_match(b, ifa)) {
334 } endfor_ifa(in_dev);
339 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
340 int destroy, struct nlmsghdr *nlh, u32 portid)
342 struct in_ifaddr *promote = NULL;
343 struct in_ifaddr *ifa, *ifa1 = *ifap;
344 struct in_ifaddr *last_prim = in_dev->ifa_list;
345 struct in_ifaddr *prev_prom = NULL;
346 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
353 /* 1. Deleting primary ifaddr forces deletion all secondaries
354 * unless alias promotion is set
357 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
358 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
360 while ((ifa = *ifap1) != NULL) {
361 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
362 ifa1->ifa_scope <= ifa->ifa_scope)
365 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
366 ifa1->ifa_mask != ifa->ifa_mask ||
367 !inet_ifa_match(ifa1->ifa_address, ifa)) {
368 ifap1 = &ifa->ifa_next;
374 inet_hash_remove(ifa);
375 *ifap1 = ifa->ifa_next;
377 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
378 blocking_notifier_call_chain(&inetaddr_chain,
388 /* On promotion all secondaries from subnet are changing
389 * the primary IP, we must remove all their routes silently
390 * and later to add them back with new prefsrc. Do this
391 * while all addresses are on the device list.
393 for (ifa = promote; ifa; ifa = ifa->ifa_next) {
394 if (ifa1->ifa_mask == ifa->ifa_mask &&
395 inet_ifa_match(ifa1->ifa_address, ifa))
396 fib_del_ifaddr(ifa, ifa1);
402 *ifap = ifa1->ifa_next;
403 inet_hash_remove(ifa1);
405 /* 3. Announce address deletion */
407 /* Send message first, then call notifier.
408 At first sight, FIB update triggered by notifier
409 will refer to already deleted ifaddr, that could confuse
410 netlink listeners. It is not true: look, gated sees
411 that route deleted and if it still thinks that ifaddr
412 is valid, it will try to restore deleted routes... Grr.
413 So that, this order is correct.
415 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
416 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
419 struct in_ifaddr *next_sec = promote->ifa_next;
422 prev_prom->ifa_next = promote->ifa_next;
423 promote->ifa_next = last_prim->ifa_next;
424 last_prim->ifa_next = promote;
427 promote->ifa_flags &= ~IFA_F_SECONDARY;
428 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
429 blocking_notifier_call_chain(&inetaddr_chain,
431 for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
432 if (ifa1->ifa_mask != ifa->ifa_mask ||
433 !inet_ifa_match(ifa1->ifa_address, ifa))
443 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
446 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
449 static void check_lifetime(struct work_struct *work);
451 static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
453 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
454 u32 portid, struct netlink_ext_ack *extack)
456 struct in_device *in_dev = ifa->ifa_dev;
457 struct in_ifaddr *ifa1, **ifap, **last_primary;
458 struct in_validator_info ivi;
463 if (!ifa->ifa_local) {
468 ifa->ifa_flags &= ~IFA_F_SECONDARY;
469 last_primary = &in_dev->ifa_list;
471 /* Don't set IPv6 only flags to IPv4 addresses */
472 ifa->ifa_flags &= ~IPV6ONLY_FLAGS;
474 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
475 ifap = &ifa1->ifa_next) {
476 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
477 ifa->ifa_scope <= ifa1->ifa_scope)
478 last_primary = &ifa1->ifa_next;
479 if (ifa1->ifa_mask == ifa->ifa_mask &&
480 inet_ifa_match(ifa1->ifa_address, ifa)) {
481 if (ifa1->ifa_local == ifa->ifa_local) {
485 if (ifa1->ifa_scope != ifa->ifa_scope) {
489 ifa->ifa_flags |= IFA_F_SECONDARY;
493 /* Allow any devices that wish to register ifaddr validtors to weigh
494 * in now, before changes are committed. The rntl lock is serializing
495 * access here, so the state should not change between a validator call
496 * and a final notify on commit. This isn't invoked on promotion under
497 * the assumption that validators are checking the address itself, and
500 ivi.ivi_addr = ifa->ifa_address;
501 ivi.ivi_dev = ifa->ifa_dev;
503 ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
505 ret = notifier_to_errno(ret);
511 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
512 prandom_seed((__force u32) ifa->ifa_local);
516 ifa->ifa_next = *ifap;
519 inet_hash_insert(dev_net(in_dev->dev), ifa);
521 cancel_delayed_work(&check_lifetime_work);
522 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
524 /* Send message first, then call notifier.
525 Notifier will trigger FIB update, so that
526 listeners of netlink will know about new ifaddr */
527 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
528 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
533 static int inet_insert_ifa(struct in_ifaddr *ifa)
535 return __inet_insert_ifa(ifa, NULL, 0, NULL);
538 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
540 struct in_device *in_dev = __in_dev_get_rtnl(dev);
548 ipv4_devconf_setall(in_dev);
549 neigh_parms_data_state_setall(in_dev->arp_parms);
550 if (ifa->ifa_dev != in_dev) {
551 WARN_ON(ifa->ifa_dev);
553 ifa->ifa_dev = in_dev;
555 if (ipv4_is_loopback(ifa->ifa_local))
556 ifa->ifa_scope = RT_SCOPE_HOST;
557 return inet_insert_ifa(ifa);
560 /* Caller must hold RCU or RTNL :
561 * We dont take a reference on found in_device
563 struct in_device *inetdev_by_index(struct net *net, int ifindex)
565 struct net_device *dev;
566 struct in_device *in_dev = NULL;
569 dev = dev_get_by_index_rcu(net, ifindex);
571 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
575 EXPORT_SYMBOL(inetdev_by_index);
577 /* Called only from RTNL semaphored context. No locks. */
579 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
584 for_primary_ifa(in_dev) {
585 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
587 } endfor_ifa(in_dev);
591 static int ip_mc_autojoin_config(struct net *net, bool join,
592 const struct in_ifaddr *ifa)
594 #if defined(CONFIG_IP_MULTICAST)
595 struct ip_mreqn mreq = {
596 .imr_multiaddr.s_addr = ifa->ifa_address,
597 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
599 struct sock *sk = net->ipv4.mc_autojoin_sk;
606 ret = ip_mc_join_group(sk, &mreq);
608 ret = ip_mc_leave_group(sk, &mreq);
617 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
618 struct netlink_ext_ack *extack)
620 struct net *net = sock_net(skb->sk);
621 struct nlattr *tb[IFA_MAX+1];
622 struct in_device *in_dev;
623 struct ifaddrmsg *ifm;
624 struct in_ifaddr *ifa, **ifap;
629 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
634 ifm = nlmsg_data(nlh);
635 in_dev = inetdev_by_index(net, ifm->ifa_index);
641 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
642 ifap = &ifa->ifa_next) {
644 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
647 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
650 if (tb[IFA_ADDRESS] &&
651 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
652 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
655 if (ipv4_is_multicast(ifa->ifa_address))
656 ip_mc_autojoin_config(net, false, ifa);
657 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
661 err = -EADDRNOTAVAIL;
666 #define INFINITY_LIFE_TIME 0xFFFFFFFF
668 static void check_lifetime(struct work_struct *work)
670 unsigned long now, next, next_sec, next_sched;
671 struct in_ifaddr *ifa;
672 struct hlist_node *n;
676 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
678 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
679 bool change_needed = false;
682 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
685 if (ifa->ifa_flags & IFA_F_PERMANENT)
688 /* We try to batch several events at once. */
689 age = (now - ifa->ifa_tstamp +
690 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
692 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
693 age >= ifa->ifa_valid_lft) {
694 change_needed = true;
695 } else if (ifa->ifa_preferred_lft ==
696 INFINITY_LIFE_TIME) {
698 } else if (age >= ifa->ifa_preferred_lft) {
699 if (time_before(ifa->ifa_tstamp +
700 ifa->ifa_valid_lft * HZ, next))
701 next = ifa->ifa_tstamp +
702 ifa->ifa_valid_lft * HZ;
704 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
705 change_needed = true;
706 } else if (time_before(ifa->ifa_tstamp +
707 ifa->ifa_preferred_lft * HZ,
709 next = ifa->ifa_tstamp +
710 ifa->ifa_preferred_lft * HZ;
717 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
720 if (ifa->ifa_flags & IFA_F_PERMANENT)
723 /* We try to batch several events at once. */
724 age = (now - ifa->ifa_tstamp +
725 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
727 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
728 age >= ifa->ifa_valid_lft) {
729 struct in_ifaddr **ifap;
731 for (ifap = &ifa->ifa_dev->ifa_list;
732 *ifap != NULL; ifap = &(*ifap)->ifa_next) {
734 inet_del_ifa(ifa->ifa_dev,
739 } else if (ifa->ifa_preferred_lft !=
740 INFINITY_LIFE_TIME &&
741 age >= ifa->ifa_preferred_lft &&
742 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
743 ifa->ifa_flags |= IFA_F_DEPRECATED;
744 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
750 next_sec = round_jiffies_up(next);
753 /* If rounded timeout is accurate enough, accept it. */
754 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
755 next_sched = next_sec;
758 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
759 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
760 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
762 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
766 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
769 unsigned long timeout;
771 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
773 timeout = addrconf_timeout_fixup(valid_lft, HZ);
774 if (addrconf_finite_timeout(timeout))
775 ifa->ifa_valid_lft = timeout;
777 ifa->ifa_flags |= IFA_F_PERMANENT;
779 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
780 if (addrconf_finite_timeout(timeout)) {
782 ifa->ifa_flags |= IFA_F_DEPRECATED;
783 ifa->ifa_preferred_lft = timeout;
785 ifa->ifa_tstamp = jiffies;
786 if (!ifa->ifa_cstamp)
787 ifa->ifa_cstamp = ifa->ifa_tstamp;
790 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
791 __u32 *pvalid_lft, __u32 *pprefered_lft)
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(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
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, **ifap;
885 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
886 ifap = &ifa1->ifa_next) {
887 if (ifa1->ifa_mask == ifa->ifa_mask &&
888 inet_ifa_match(ifa1->ifa_address, ifa) &&
889 ifa1->ifa_local == ifa->ifa_local)
895 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
896 struct netlink_ext_ack *extack)
898 struct net *net = sock_net(skb->sk);
899 struct in_ifaddr *ifa;
900 struct in_ifaddr *ifa_existing;
901 __u32 valid_lft = INFINITY_LIFE_TIME;
902 __u32 prefered_lft = INFINITY_LIFE_TIME;
906 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft);
910 ifa_existing = find_matching_ifa(ifa);
912 /* It would be best to check for !NLM_F_CREATE here but
913 * userspace already relies on not having to provide this.
915 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
916 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
917 int ret = ip_mc_autojoin_config(net, true, ifa);
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))
961 __u32 haddr = ntohl(addr);
963 if (IN_CLASSA(haddr))
965 else if (IN_CLASSB(haddr))
967 else if (IN_CLASSC(haddr))
975 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
977 struct sockaddr_in sin_orig;
978 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
979 struct in_device *in_dev;
980 struct in_ifaddr **ifap = NULL;
981 struct in_ifaddr *ifa = NULL;
982 struct net_device *dev;
985 int tryaddrmatch = 0;
987 ifr->ifr_name[IFNAMSIZ - 1] = 0;
989 /* save original address for comparison */
990 memcpy(&sin_orig, sin, sizeof(*sin));
992 colon = strchr(ifr->ifr_name, ':');
996 dev_load(net, ifr->ifr_name);
999 case SIOCGIFADDR: /* Get interface address */
1000 case SIOCGIFBRDADDR: /* Get the broadcast address */
1001 case SIOCGIFDSTADDR: /* Get the destination address */
1002 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1003 /* Note that these ioctls will not sleep,
1004 so that we do not impose a lock.
1005 One day we will be forced to put shlock here (I mean SMP)
1007 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1008 memset(sin, 0, sizeof(*sin));
1009 sin->sin_family = AF_INET;
1014 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1017 case SIOCSIFADDR: /* Set interface address (and family) */
1018 case SIOCSIFBRDADDR: /* Set the broadcast address */
1019 case SIOCSIFDSTADDR: /* Set the destination address */
1020 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1022 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1025 if (sin->sin_family != AF_INET)
1036 dev = __dev_get_by_name(net, ifr->ifr_name);
1043 in_dev = __in_dev_get_rtnl(dev);
1046 /* Matthias Andree */
1047 /* compare label and address (4.4BSD style) */
1048 /* note: we only do this for a limited set of ioctls
1049 and only if the original address family was AF_INET.
1050 This is checked above. */
1051 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1052 ifap = &ifa->ifa_next) {
1053 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
1054 sin_orig.sin_addr.s_addr ==
1060 /* we didn't get a match, maybe the application is
1061 4.3BSD-style and passed in junk so we fall back to
1062 comparing just the label */
1064 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1065 ifap = &ifa->ifa_next)
1066 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
1071 ret = -EADDRNOTAVAIL;
1072 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1076 case SIOCGIFADDR: /* Get interface address */
1078 sin->sin_addr.s_addr = ifa->ifa_local;
1081 case SIOCGIFBRDADDR: /* Get the broadcast address */
1083 sin->sin_addr.s_addr = ifa->ifa_broadcast;
1086 case SIOCGIFDSTADDR: /* Get the destination address */
1088 sin->sin_addr.s_addr = ifa->ifa_address;
1091 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1093 sin->sin_addr.s_addr = ifa->ifa_mask;
1098 ret = -EADDRNOTAVAIL;
1102 if (!(ifr->ifr_flags & IFF_UP))
1103 inet_del_ifa(in_dev, ifap, 1);
1106 ret = dev_change_flags(dev, ifr->ifr_flags);
1109 case SIOCSIFADDR: /* Set interface address (and family) */
1111 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1116 ifa = inet_alloc_ifa();
1119 INIT_HLIST_NODE(&ifa->hash);
1121 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
1123 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1126 if (ifa->ifa_local == sin->sin_addr.s_addr)
1128 inet_del_ifa(in_dev, ifap, 0);
1129 ifa->ifa_broadcast = 0;
1133 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1135 if (!(dev->flags & IFF_POINTOPOINT)) {
1136 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1137 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1138 if ((dev->flags & IFF_BROADCAST) &&
1139 ifa->ifa_prefixlen < 31)
1140 ifa->ifa_broadcast = ifa->ifa_address |
1143 ifa->ifa_prefixlen = 32;
1144 ifa->ifa_mask = inet_make_mask(32);
1146 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
1147 ret = inet_set_ifa(dev, ifa);
1150 case SIOCSIFBRDADDR: /* Set the broadcast address */
1152 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1153 inet_del_ifa(in_dev, ifap, 0);
1154 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1155 inet_insert_ifa(ifa);
1159 case SIOCSIFDSTADDR: /* Set the destination address */
1161 if (ifa->ifa_address == sin->sin_addr.s_addr)
1164 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1167 inet_del_ifa(in_dev, ifap, 0);
1168 ifa->ifa_address = sin->sin_addr.s_addr;
1169 inet_insert_ifa(ifa);
1172 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1175 * The mask we set must be legal.
1178 if (bad_mask(sin->sin_addr.s_addr, 0))
1181 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1182 __be32 old_mask = ifa->ifa_mask;
1183 inet_del_ifa(in_dev, ifap, 0);
1184 ifa->ifa_mask = sin->sin_addr.s_addr;
1185 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1187 /* See if current broadcast address matches
1188 * with current netmask, then recalculate
1189 * the broadcast address. Otherwise it's a
1190 * funny address, so don't touch it since
1191 * the user seems to know what (s)he's doing...
1193 if ((dev->flags & IFF_BROADCAST) &&
1194 (ifa->ifa_prefixlen < 31) &&
1195 (ifa->ifa_broadcast ==
1196 (ifa->ifa_local|~old_mask))) {
1197 ifa->ifa_broadcast = (ifa->ifa_local |
1198 ~sin->sin_addr.s_addr);
1200 inet_insert_ifa(ifa);
1210 static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
1212 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1213 struct in_ifaddr *ifa;
1217 if (WARN_ON(size > sizeof(struct ifreq)))
1223 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1230 memset(&ifr, 0, sizeof(struct ifreq));
1231 strcpy(ifr.ifr_name, ifa->ifa_label);
1233 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1234 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1237 if (copy_to_user(buf + done, &ifr, size)) {
1248 static __be32 in_dev_select_addr(const struct in_device *in_dev,
1251 for_primary_ifa(in_dev) {
1252 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1253 ifa->ifa_scope <= scope)
1254 return ifa->ifa_local;
1255 } endfor_ifa(in_dev);
1260 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1263 struct in_device *in_dev;
1264 struct net *net = dev_net(dev);
1268 in_dev = __in_dev_get_rcu(dev);
1272 for_primary_ifa(in_dev) {
1273 if (ifa->ifa_scope > scope)
1275 if (!dst || inet_ifa_match(dst, ifa)) {
1276 addr = ifa->ifa_local;
1280 addr = ifa->ifa_local;
1281 } endfor_ifa(in_dev);
1286 master_idx = l3mdev_master_ifindex_rcu(dev);
1288 /* For VRFs, the VRF device takes the place of the loopback device,
1289 * with addresses on it being preferred. Note in such cases the
1290 * loopback device will be among the devices that fail the master_idx
1291 * equality check in the loop below.
1294 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1295 (in_dev = __in_dev_get_rcu(dev))) {
1296 addr = in_dev_select_addr(in_dev, scope);
1301 /* Not loopback addresses on loopback should be preferred
1302 in this case. It is important that lo is the first interface
1305 for_each_netdev_rcu(net, dev) {
1306 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1309 in_dev = __in_dev_get_rcu(dev);
1313 addr = in_dev_select_addr(in_dev, scope);
1321 EXPORT_SYMBOL(inet_select_addr);
1323 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1324 __be32 local, int scope)
1331 (local == ifa->ifa_local || !local) &&
1332 ifa->ifa_scope <= scope) {
1333 addr = ifa->ifa_local;
1338 same = (!local || inet_ifa_match(local, ifa)) &&
1339 (!dst || inet_ifa_match(dst, ifa));
1343 /* Is the selected addr into dst subnet? */
1344 if (inet_ifa_match(addr, ifa))
1346 /* No, then can we use new local src? */
1347 if (ifa->ifa_scope <= scope) {
1348 addr = ifa->ifa_local;
1351 /* search for large dst subnet for addr */
1355 } endfor_ifa(in_dev);
1357 return same ? addr : 0;
1361 * Confirm that local IP address exists using wildcards:
1362 * - net: netns to check, cannot be NULL
1363 * - in_dev: only on this interface, NULL=any interface
1364 * - dst: only in the same subnet as dst, 0=any dst
1365 * - local: address, 0=autoselect the local address
1366 * - scope: maximum allowed scope value for the local address
1368 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1369 __be32 dst, __be32 local, int scope)
1372 struct net_device *dev;
1375 return confirm_addr_indev(in_dev, dst, local, scope);
1378 for_each_netdev_rcu(net, dev) {
1379 in_dev = __in_dev_get_rcu(dev);
1381 addr = confirm_addr_indev(in_dev, dst, local, scope);
1390 EXPORT_SYMBOL(inet_confirm_addr);
1396 int register_inetaddr_notifier(struct notifier_block *nb)
1398 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1400 EXPORT_SYMBOL(register_inetaddr_notifier);
1402 int unregister_inetaddr_notifier(struct notifier_block *nb)
1404 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1406 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1408 int register_inetaddr_validator_notifier(struct notifier_block *nb)
1410 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1412 EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1414 int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1416 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1419 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1421 /* Rename ifa_labels for a device name change. Make some effort to preserve
1422 * existing alias numbering and to create unique labels if possible.
1424 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1426 struct in_ifaddr *ifa;
1429 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1430 char old[IFNAMSIZ], *dot;
1432 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1433 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1436 dot = strchr(old, ':');
1438 sprintf(old, ":%d", named);
1441 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1442 strcat(ifa->ifa_label, dot);
1444 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1446 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1450 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1451 struct in_device *in_dev)
1454 struct in_ifaddr *ifa;
1456 for (ifa = in_dev->ifa_list; ifa;
1457 ifa = ifa->ifa_next) {
1458 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1459 ifa->ifa_local, dev,
1460 ifa->ifa_local, NULL,
1461 dev->dev_addr, NULL);
1465 /* Called only under RTNL semaphore */
1467 static int inetdev_event(struct notifier_block *this, unsigned long event,
1470 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1471 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1476 if (event == NETDEV_REGISTER) {
1477 in_dev = inetdev_init(dev);
1479 return notifier_from_errno(PTR_ERR(in_dev));
1480 if (dev->flags & IFF_LOOPBACK) {
1481 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1482 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1484 } else if (event == NETDEV_CHANGEMTU) {
1485 /* Re-enabling IP */
1486 if (inetdev_valid_mtu(dev->mtu))
1487 in_dev = inetdev_init(dev);
1493 case NETDEV_REGISTER:
1494 pr_debug("%s: bug\n", __func__);
1495 RCU_INIT_POINTER(dev->ip_ptr, NULL);
1498 if (!inetdev_valid_mtu(dev->mtu))
1500 if (dev->flags & IFF_LOOPBACK) {
1501 struct in_ifaddr *ifa = inet_alloc_ifa();
1504 INIT_HLIST_NODE(&ifa->hash);
1506 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1507 ifa->ifa_prefixlen = 8;
1508 ifa->ifa_mask = inet_make_mask(8);
1509 in_dev_hold(in_dev);
1510 ifa->ifa_dev = in_dev;
1511 ifa->ifa_scope = RT_SCOPE_HOST;
1512 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1513 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1514 INFINITY_LIFE_TIME);
1515 ipv4_devconf_setall(in_dev);
1516 neigh_parms_data_state_setall(in_dev->arp_parms);
1517 inet_insert_ifa(ifa);
1522 case NETDEV_CHANGEADDR:
1523 if (!IN_DEV_ARP_NOTIFY(in_dev))
1526 case NETDEV_NOTIFY_PEERS:
1527 /* Send gratuitous ARP to notify of link change */
1528 inetdev_send_gratuitous_arp(dev, in_dev);
1533 case NETDEV_PRE_TYPE_CHANGE:
1534 ip_mc_unmap(in_dev);
1536 case NETDEV_POST_TYPE_CHANGE:
1537 ip_mc_remap(in_dev);
1539 case NETDEV_CHANGEMTU:
1540 if (inetdev_valid_mtu(dev->mtu))
1542 /* disable IP when MTU is not enough */
1544 case NETDEV_UNREGISTER:
1545 inetdev_destroy(in_dev);
1547 case NETDEV_CHANGENAME:
1548 /* Do not notify about label change, this event is
1549 * not interesting to applications using netlink.
1551 inetdev_changename(dev, in_dev);
1553 devinet_sysctl_unregister(in_dev);
1554 devinet_sysctl_register(in_dev);
1561 static struct notifier_block ip_netdev_notifier = {
1562 .notifier_call = inetdev_event,
1565 static size_t inet_nlmsg_size(void)
1567 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1568 + nla_total_size(4) /* IFA_ADDRESS */
1569 + nla_total_size(4) /* IFA_LOCAL */
1570 + nla_total_size(4) /* IFA_BROADCAST */
1571 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
1572 + nla_total_size(4) /* IFA_FLAGS */
1573 + nla_total_size(4) /* IFA_RT_PRIORITY */
1574 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
1577 static inline u32 cstamp_delta(unsigned long cstamp)
1579 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1582 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1583 unsigned long tstamp, u32 preferred, u32 valid)
1585 struct ifa_cacheinfo ci;
1587 ci.cstamp = cstamp_delta(cstamp);
1588 ci.tstamp = cstamp_delta(tstamp);
1589 ci.ifa_prefered = preferred;
1590 ci.ifa_valid = valid;
1592 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1595 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1596 u32 portid, u32 seq, int event, unsigned int flags)
1598 struct ifaddrmsg *ifm;
1599 struct nlmsghdr *nlh;
1600 u32 preferred, valid;
1602 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
1606 ifm = nlmsg_data(nlh);
1607 ifm->ifa_family = AF_INET;
1608 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1609 ifm->ifa_flags = ifa->ifa_flags;
1610 ifm->ifa_scope = ifa->ifa_scope;
1611 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1613 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1614 preferred = ifa->ifa_preferred_lft;
1615 valid = ifa->ifa_valid_lft;
1616 if (preferred != INFINITY_LIFE_TIME) {
1617 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1619 if (preferred > tval)
1623 if (valid != INFINITY_LIFE_TIME) {
1631 preferred = INFINITY_LIFE_TIME;
1632 valid = INFINITY_LIFE_TIME;
1634 if ((ifa->ifa_address &&
1635 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1637 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
1638 (ifa->ifa_broadcast &&
1639 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1640 (ifa->ifa_label[0] &&
1641 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1642 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
1643 (ifa->ifa_rt_priority &&
1644 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
1645 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1647 goto nla_put_failure;
1649 nlmsg_end(skb, nlh);
1653 nlmsg_cancel(skb, nlh);
1657 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1659 struct net *net = sock_net(skb->sk);
1662 int ip_idx, s_ip_idx;
1663 struct net_device *dev;
1664 struct in_device *in_dev;
1665 struct in_ifaddr *ifa;
1666 struct hlist_head *head;
1669 s_idx = idx = cb->args[1];
1670 s_ip_idx = ip_idx = cb->args[2];
1672 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1674 head = &net->dev_index_head[h];
1676 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
1678 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1681 if (h > s_h || idx > s_idx)
1683 in_dev = __in_dev_get_rcu(dev);
1687 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1688 ifa = ifa->ifa_next, ip_idx++) {
1689 if (ip_idx < s_ip_idx)
1691 if (inet_fill_ifaddr(skb, ifa,
1692 NETLINK_CB(cb->skb).portid,
1694 RTM_NEWADDR, NLM_F_MULTI) < 0) {
1698 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1709 cb->args[2] = ip_idx;
1714 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1717 struct sk_buff *skb;
1718 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1722 net = dev_net(ifa->ifa_dev->dev);
1723 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1727 err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0);
1729 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1730 WARN_ON(err == -EMSGSIZE);
1734 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1738 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1741 static size_t inet_get_link_af_size(const struct net_device *dev,
1742 u32 ext_filter_mask)
1744 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1749 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1752 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1753 u32 ext_filter_mask)
1755 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1762 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1766 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1767 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1772 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1773 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1776 static int inet_validate_link_af(const struct net_device *dev,
1777 const struct nlattr *nla)
1779 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1782 if (dev && !__in_dev_get_rcu(dev))
1783 return -EAFNOSUPPORT;
1785 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy, NULL);
1789 if (tb[IFLA_INET_CONF]) {
1790 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1791 int cfgid = nla_type(a);
1796 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1804 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1806 struct in_device *in_dev = __in_dev_get_rcu(dev);
1807 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1811 return -EAFNOSUPPORT;
1813 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
1816 if (tb[IFLA_INET_CONF]) {
1817 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1818 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1824 static int inet_netconf_msgsize_devconf(int type)
1826 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1827 + nla_total_size(4); /* NETCONFA_IFINDEX */
1830 if (type == NETCONFA_ALL)
1833 if (all || type == NETCONFA_FORWARDING)
1834 size += nla_total_size(4);
1835 if (all || type == NETCONFA_RP_FILTER)
1836 size += nla_total_size(4);
1837 if (all || type == NETCONFA_MC_FORWARDING)
1838 size += nla_total_size(4);
1839 if (all || type == NETCONFA_BC_FORWARDING)
1840 size += nla_total_size(4);
1841 if (all || type == NETCONFA_PROXY_NEIGH)
1842 size += nla_total_size(4);
1843 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
1844 size += nla_total_size(4);
1849 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1850 struct ipv4_devconf *devconf, u32 portid,
1851 u32 seq, int event, unsigned int flags,
1854 struct nlmsghdr *nlh;
1855 struct netconfmsg *ncm;
1858 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1863 if (type == NETCONFA_ALL)
1866 ncm = nlmsg_data(nlh);
1867 ncm->ncm_family = AF_INET;
1869 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1870 goto nla_put_failure;
1875 if ((all || type == NETCONFA_FORWARDING) &&
1876 nla_put_s32(skb, NETCONFA_FORWARDING,
1877 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
1878 goto nla_put_failure;
1879 if ((all || type == NETCONFA_RP_FILTER) &&
1880 nla_put_s32(skb, NETCONFA_RP_FILTER,
1881 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
1882 goto nla_put_failure;
1883 if ((all || type == NETCONFA_MC_FORWARDING) &&
1884 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
1885 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
1886 goto nla_put_failure;
1887 if ((all || type == NETCONFA_BC_FORWARDING) &&
1888 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
1889 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
1890 goto nla_put_failure;
1891 if ((all || type == NETCONFA_PROXY_NEIGH) &&
1892 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
1893 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
1894 goto nla_put_failure;
1895 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
1896 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
1897 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
1898 goto nla_put_failure;
1901 nlmsg_end(skb, nlh);
1905 nlmsg_cancel(skb, nlh);
1909 void inet_netconf_notify_devconf(struct net *net, int event, int type,
1910 int ifindex, struct ipv4_devconf *devconf)
1912 struct sk_buff *skb;
1915 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
1919 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
1922 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1923 WARN_ON(err == -EMSGSIZE);
1927 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
1931 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
1934 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
1935 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1936 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
1937 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
1938 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
1939 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
1942 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
1943 struct nlmsghdr *nlh,
1944 struct netlink_ext_ack *extack)
1946 struct net *net = sock_net(in_skb->sk);
1947 struct nlattr *tb[NETCONFA_MAX+1];
1948 struct netconfmsg *ncm;
1949 struct sk_buff *skb;
1950 struct ipv4_devconf *devconf;
1951 struct in_device *in_dev;
1952 struct net_device *dev;
1956 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1957 devconf_ipv4_policy, extack);
1962 if (!tb[NETCONFA_IFINDEX])
1965 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1967 case NETCONFA_IFINDEX_ALL:
1968 devconf = net->ipv4.devconf_all;
1970 case NETCONFA_IFINDEX_DEFAULT:
1971 devconf = net->ipv4.devconf_dflt;
1974 dev = __dev_get_by_index(net, ifindex);
1977 in_dev = __in_dev_get_rtnl(dev);
1980 devconf = &in_dev->cnf;
1985 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1989 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
1990 NETLINK_CB(in_skb).portid,
1991 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1994 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1995 WARN_ON(err == -EMSGSIZE);
1999 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2004 static int inet_netconf_dump_devconf(struct sk_buff *skb,
2005 struct netlink_callback *cb)
2007 struct net *net = sock_net(skb->sk);
2010 struct net_device *dev;
2011 struct in_device *in_dev;
2012 struct hlist_head *head;
2015 s_idx = idx = cb->args[1];
2017 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2019 head = &net->dev_index_head[h];
2021 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2023 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2026 in_dev = __in_dev_get_rcu(dev);
2030 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2032 NETLINK_CB(cb->skb).portid,
2036 NETCONFA_ALL) < 0) {
2040 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2046 if (h == NETDEV_HASHENTRIES) {
2047 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2048 net->ipv4.devconf_all,
2049 NETLINK_CB(cb->skb).portid,
2051 RTM_NEWNETCONF, NLM_F_MULTI,
2057 if (h == NETDEV_HASHENTRIES + 1) {
2058 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2059 net->ipv4.devconf_dflt,
2060 NETLINK_CB(cb->skb).portid,
2062 RTM_NEWNETCONF, NLM_F_MULTI,
2075 #ifdef CONFIG_SYSCTL
2077 static void devinet_copy_dflt_conf(struct net *net, int i)
2079 struct net_device *dev;
2082 for_each_netdev_rcu(net, dev) {
2083 struct in_device *in_dev;
2085 in_dev = __in_dev_get_rcu(dev);
2086 if (in_dev && !test_bit(i, in_dev->cnf.state))
2087 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
2092 /* called with RTNL locked */
2093 static void inet_forward_change(struct net *net)
2095 struct net_device *dev;
2096 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
2098 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
2099 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
2100 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2101 NETCONFA_FORWARDING,
2102 NETCONFA_IFINDEX_ALL,
2103 net->ipv4.devconf_all);
2104 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2105 NETCONFA_FORWARDING,
2106 NETCONFA_IFINDEX_DEFAULT,
2107 net->ipv4.devconf_dflt);
2109 for_each_netdev(net, dev) {
2110 struct in_device *in_dev;
2113 dev_disable_lro(dev);
2115 in_dev = __in_dev_get_rtnl(dev);
2117 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
2118 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2119 NETCONFA_FORWARDING,
2120 dev->ifindex, &in_dev->cnf);
2125 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2127 if (cnf == net->ipv4.devconf_dflt)
2128 return NETCONFA_IFINDEX_DEFAULT;
2129 else if (cnf == net->ipv4.devconf_all)
2130 return NETCONFA_IFINDEX_ALL;
2132 struct in_device *idev
2133 = container_of(cnf, struct in_device, cnf);
2134 return idev->dev->ifindex;
2138 static int devinet_conf_proc(struct ctl_table *ctl, int write,
2139 void __user *buffer,
2140 size_t *lenp, loff_t *ppos)
2142 int old_value = *(int *)ctl->data;
2143 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2144 int new_value = *(int *)ctl->data;
2147 struct ipv4_devconf *cnf = ctl->extra1;
2148 struct net *net = ctl->extra2;
2149 int i = (int *)ctl->data - cnf->data;
2152 set_bit(i, cnf->state);
2154 if (cnf == net->ipv4.devconf_dflt)
2155 devinet_copy_dflt_conf(net, i);
2156 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2157 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
2158 if ((new_value == 0) && (old_value != 0))
2159 rt_cache_flush(net);
2161 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2162 new_value != old_value)
2163 rt_cache_flush(net);
2165 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2166 new_value != old_value) {
2167 ifindex = devinet_conf_ifindex(net, cnf);
2168 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2172 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2173 new_value != old_value) {
2174 ifindex = devinet_conf_ifindex(net, cnf);
2175 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2176 NETCONFA_PROXY_NEIGH,
2179 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2180 new_value != old_value) {
2181 ifindex = devinet_conf_ifindex(net, cnf);
2182 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2183 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2191 static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
2192 void __user *buffer,
2193 size_t *lenp, loff_t *ppos)
2195 int *valp = ctl->data;
2198 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2200 if (write && *valp != val) {
2201 struct net *net = ctl->extra2;
2203 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2204 if (!rtnl_trylock()) {
2205 /* Restore the original values before restarting */
2208 return restart_syscall();
2210 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2211 inet_forward_change(net);
2213 struct ipv4_devconf *cnf = ctl->extra1;
2214 struct in_device *idev =
2215 container_of(cnf, struct in_device, cnf);
2217 dev_disable_lro(idev->dev);
2218 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2219 NETCONFA_FORWARDING,
2224 rt_cache_flush(net);
2226 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2227 NETCONFA_FORWARDING,
2228 NETCONFA_IFINDEX_DEFAULT,
2229 net->ipv4.devconf_dflt);
2235 static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
2236 void __user *buffer,
2237 size_t *lenp, loff_t *ppos)
2239 int *valp = ctl->data;
2241 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2242 struct net *net = ctl->extra2;
2244 if (write && *valp != val)
2245 rt_cache_flush(net);
2250 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2253 .data = ipv4_devconf.data + \
2254 IPV4_DEVCONF_ ## attr - 1, \
2255 .maxlen = sizeof(int), \
2257 .proc_handler = proc, \
2258 .extra1 = &ipv4_devconf, \
2261 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2262 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2264 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2265 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2267 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2268 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2270 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2271 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2273 static struct devinet_sysctl_table {
2274 struct ctl_table_header *sysctl_header;
2275 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2276 } devinet_sysctl = {
2278 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2279 devinet_sysctl_forward),
2280 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2281 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
2283 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2284 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2285 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2286 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2287 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2288 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2289 "accept_source_route"),
2290 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2291 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2292 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2293 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2294 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2295 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2296 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2297 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2298 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2299 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2300 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2301 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2302 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2303 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2304 "force_igmp_version"),
2305 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2306 "igmpv2_unsolicited_report_interval"),
2307 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2308 "igmpv3_unsolicited_report_interval"),
2309 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2310 "ignore_routes_with_linkdown"),
2311 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2312 "drop_gratuitous_arp"),
2314 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2315 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
2316 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2317 "promote_secondaries"),
2318 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2320 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2321 "drop_unicast_in_l2_multicast"),
2325 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2326 int ifindex, struct ipv4_devconf *p)
2329 struct devinet_sysctl_table *t;
2330 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2332 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
2336 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2337 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2338 t->devinet_vars[i].extra1 = p;
2339 t->devinet_vars[i].extra2 = net;
2342 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2344 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2345 if (!t->sysctl_header)
2350 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2360 static void __devinet_sysctl_unregister(struct net *net,
2361 struct ipv4_devconf *cnf, int ifindex)
2363 struct devinet_sysctl_table *t = cnf->sysctl;
2367 unregister_net_sysctl_table(t->sysctl_header);
2371 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
2374 static int devinet_sysctl_register(struct in_device *idev)
2378 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2381 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2384 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2385 idev->dev->ifindex, &idev->cnf);
2387 neigh_sysctl_unregister(idev->arp_parms);
2391 static void devinet_sysctl_unregister(struct in_device *idev)
2393 struct net *net = dev_net(idev->dev);
2395 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
2396 neigh_sysctl_unregister(idev->arp_parms);
2399 static struct ctl_table ctl_forward_entry[] = {
2401 .procname = "ip_forward",
2402 .data = &ipv4_devconf.data[
2403 IPV4_DEVCONF_FORWARDING - 1],
2404 .maxlen = sizeof(int),
2406 .proc_handler = devinet_sysctl_forward,
2407 .extra1 = &ipv4_devconf,
2408 .extra2 = &init_net,
2414 static __net_init int devinet_init_net(struct net *net)
2417 struct ipv4_devconf *all, *dflt;
2418 #ifdef CONFIG_SYSCTL
2419 struct ctl_table *tbl = ctl_forward_entry;
2420 struct ctl_table_header *forw_hdr;
2424 all = &ipv4_devconf;
2425 dflt = &ipv4_devconf_dflt;
2427 if (!net_eq(net, &init_net)) {
2428 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
2432 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2434 goto err_alloc_dflt;
2436 #ifdef CONFIG_SYSCTL
2437 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
2441 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2442 tbl[0].extra1 = all;
2443 tbl[0].extra2 = net;
2447 #ifdef CONFIG_SYSCTL
2448 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
2452 err = __devinet_sysctl_register(net, "default",
2453 NETCONFA_IFINDEX_DEFAULT, dflt);
2458 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2461 net->ipv4.forw_hdr = forw_hdr;
2464 net->ipv4.devconf_all = all;
2465 net->ipv4.devconf_dflt = dflt;
2468 #ifdef CONFIG_SYSCTL
2470 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
2472 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
2474 if (tbl != ctl_forward_entry)
2478 if (dflt != &ipv4_devconf_dflt)
2481 if (all != &ipv4_devconf)
2487 static __net_exit void devinet_exit_net(struct net *net)
2489 #ifdef CONFIG_SYSCTL
2490 struct ctl_table *tbl;
2492 tbl = net->ipv4.forw_hdr->ctl_table_arg;
2493 unregister_net_sysctl_table(net->ipv4.forw_hdr);
2494 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2495 NETCONFA_IFINDEX_DEFAULT);
2496 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2497 NETCONFA_IFINDEX_ALL);
2500 kfree(net->ipv4.devconf_dflt);
2501 kfree(net->ipv4.devconf_all);
2504 static __net_initdata struct pernet_operations devinet_ops = {
2505 .init = devinet_init_net,
2506 .exit = devinet_exit_net,
2509 static struct rtnl_af_ops inet_af_ops __read_mostly = {
2511 .fill_link_af = inet_fill_link_af,
2512 .get_link_af_size = inet_get_link_af_size,
2513 .validate_link_af = inet_validate_link_af,
2514 .set_link_af = inet_set_link_af,
2517 void __init devinet_init(void)
2521 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2522 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2524 register_pernet_subsys(&devinet_ops);
2526 register_gifconf(PF_INET, inet_gifconf);
2527 register_netdevice_notifier(&ip_netdev_notifier);
2529 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
2531 rtnl_af_register(&inet_af_ops);
2533 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2534 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2535 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
2536 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
2537 inet_netconf_dump_devconf, 0);