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 {
114 #define IN4_ADDR_HSIZE_SHIFT 8
115 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
117 static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
119 static u32 inet_addr_hash(const struct net *net, __be32 addr)
121 u32 val = (__force u32) addr ^ net_hash_mix(net);
123 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
126 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
128 u32 hash = inet_addr_hash(net, ifa->ifa_local);
131 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
134 static void inet_hash_remove(struct in_ifaddr *ifa)
137 hlist_del_init_rcu(&ifa->hash);
141 * __ip_dev_find - find the first device with a given source address.
142 * @net: the net namespace
143 * @addr: the source address
144 * @devref: if true, take a reference on the found device
146 * If a caller uses devref=false, it should be protected by RCU, or RTNL
148 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
150 struct net_device *result = NULL;
151 struct in_ifaddr *ifa;
154 ifa = inet_lookup_ifaddr_rcu(net, addr);
156 struct flowi4 fl4 = { .daddr = addr };
157 struct fib_result res = { 0 };
158 struct fib_table *local;
160 /* Fallback to FIB local table so that communication
161 * over loopback subnets work.
163 local = fib_get_table(net, RT_TABLE_LOCAL);
165 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
166 res.type == RTN_LOCAL)
167 result = FIB_RES_DEV(res);
169 result = ifa->ifa_dev->dev;
171 if (result && devref)
176 EXPORT_SYMBOL(__ip_dev_find);
178 /* called under RCU lock */
179 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
181 u32 hash = inet_addr_hash(net, addr);
182 struct in_ifaddr *ifa;
184 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
185 if (ifa->ifa_local == addr &&
186 net_eq(dev_net(ifa->ifa_dev->dev), net))
192 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
194 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
195 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
196 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
199 static int devinet_sysctl_register(struct in_device *idev);
200 static void devinet_sysctl_unregister(struct in_device *idev);
202 static int devinet_sysctl_register(struct in_device *idev)
206 static void devinet_sysctl_unregister(struct in_device *idev)
211 /* Locks all the inet devices. */
213 static struct in_ifaddr *inet_alloc_ifa(void)
215 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
218 static void inet_rcu_free_ifa(struct rcu_head *head)
220 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
222 in_dev_put(ifa->ifa_dev);
226 static void inet_free_ifa(struct in_ifaddr *ifa)
228 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
231 void in_dev_finish_destroy(struct in_device *idev)
233 struct net_device *dev = idev->dev;
235 WARN_ON(idev->ifa_list);
236 WARN_ON(idev->mc_list);
237 kfree(rcu_dereference_protected(idev->mc_hash, 1));
238 #ifdef NET_REFCNT_DEBUG
239 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
243 pr_err("Freeing alive in_device %p\n", idev);
247 EXPORT_SYMBOL(in_dev_finish_destroy);
249 static struct in_device *inetdev_init(struct net_device *dev)
251 struct in_device *in_dev;
256 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
259 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
260 sizeof(in_dev->cnf));
261 in_dev->cnf.sysctl = NULL;
263 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
264 if (!in_dev->arp_parms)
266 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
267 dev_disable_lro(dev);
268 /* Reference in_dev->dev */
270 /* Account for reference dev->ip_ptr (below) */
271 refcount_set(&in_dev->refcnt, 1);
273 err = devinet_sysctl_register(in_dev);
280 ip_mc_init_dev(in_dev);
281 if (dev->flags & IFF_UP)
284 /* we can receive as soon as ip_ptr is set -- do this last */
285 rcu_assign_pointer(dev->ip_ptr, in_dev);
287 return in_dev ?: ERR_PTR(err);
294 static void in_dev_rcu_put(struct rcu_head *head)
296 struct in_device *idev = container_of(head, struct in_device, rcu_head);
300 static void inetdev_destroy(struct in_device *in_dev)
302 struct in_ifaddr *ifa;
303 struct net_device *dev;
311 ip_mc_destroy_dev(in_dev);
313 while ((ifa = in_dev->ifa_list) != NULL) {
314 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
318 RCU_INIT_POINTER(dev->ip_ptr, NULL);
320 devinet_sysctl_unregister(in_dev);
321 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
324 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
327 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
330 for_primary_ifa(in_dev) {
331 if (inet_ifa_match(a, ifa)) {
332 if (!b || inet_ifa_match(b, ifa)) {
337 } endfor_ifa(in_dev);
342 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
343 int destroy, struct nlmsghdr *nlh, u32 portid)
345 struct in_ifaddr *promote = NULL;
346 struct in_ifaddr *ifa, *ifa1 = *ifap;
347 struct in_ifaddr *last_prim = in_dev->ifa_list;
348 struct in_ifaddr *prev_prom = NULL;
349 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
356 /* 1. Deleting primary ifaddr forces deletion all secondaries
357 * unless alias promotion is set
360 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
361 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
363 while ((ifa = *ifap1) != NULL) {
364 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
365 ifa1->ifa_scope <= ifa->ifa_scope)
368 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
369 ifa1->ifa_mask != ifa->ifa_mask ||
370 !inet_ifa_match(ifa1->ifa_address, ifa)) {
371 ifap1 = &ifa->ifa_next;
377 inet_hash_remove(ifa);
378 *ifap1 = ifa->ifa_next;
380 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
381 blocking_notifier_call_chain(&inetaddr_chain,
391 /* On promotion all secondaries from subnet are changing
392 * the primary IP, we must remove all their routes silently
393 * and later to add them back with new prefsrc. Do this
394 * while all addresses are on the device list.
396 for (ifa = promote; ifa; ifa = ifa->ifa_next) {
397 if (ifa1->ifa_mask == ifa->ifa_mask &&
398 inet_ifa_match(ifa1->ifa_address, ifa))
399 fib_del_ifaddr(ifa, ifa1);
405 *ifap = ifa1->ifa_next;
406 inet_hash_remove(ifa1);
408 /* 3. Announce address deletion */
410 /* Send message first, then call notifier.
411 At first sight, FIB update triggered by notifier
412 will refer to already deleted ifaddr, that could confuse
413 netlink listeners. It is not true: look, gated sees
414 that route deleted and if it still thinks that ifaddr
415 is valid, it will try to restore deleted routes... Grr.
416 So that, this order is correct.
418 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
419 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
422 struct in_ifaddr *next_sec = promote->ifa_next;
425 prev_prom->ifa_next = promote->ifa_next;
426 promote->ifa_next = last_prim->ifa_next;
427 last_prim->ifa_next = promote;
430 promote->ifa_flags &= ~IFA_F_SECONDARY;
431 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
432 blocking_notifier_call_chain(&inetaddr_chain,
434 for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
435 if (ifa1->ifa_mask != ifa->ifa_mask ||
436 !inet_ifa_match(ifa1->ifa_address, ifa))
446 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
449 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
452 static void check_lifetime(struct work_struct *work);
454 static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
456 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
457 u32 portid, struct netlink_ext_ack *extack)
459 struct in_device *in_dev = ifa->ifa_dev;
460 struct in_ifaddr *ifa1, **ifap, **last_primary;
461 struct in_validator_info ivi;
466 if (!ifa->ifa_local) {
471 ifa->ifa_flags &= ~IFA_F_SECONDARY;
472 last_primary = &in_dev->ifa_list;
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_config(struct sock *sk, bool join, const struct in_ifaddr *ifa)
593 struct ip_mreqn mreq = {
594 .imr_multiaddr.s_addr = ifa->ifa_address,
595 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
603 ret = ip_mc_join_group(sk, &mreq);
605 ret = ip_mc_leave_group(sk, &mreq);
611 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
612 struct netlink_ext_ack *extack)
614 struct net *net = sock_net(skb->sk);
615 struct nlattr *tb[IFA_MAX+1];
616 struct in_device *in_dev;
617 struct ifaddrmsg *ifm;
618 struct in_ifaddr *ifa, **ifap;
623 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
628 ifm = nlmsg_data(nlh);
629 in_dev = inetdev_by_index(net, ifm->ifa_index);
635 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
636 ifap = &ifa->ifa_next) {
638 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
641 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
644 if (tb[IFA_ADDRESS] &&
645 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
646 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
649 if (ipv4_is_multicast(ifa->ifa_address))
650 ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa);
651 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
655 err = -EADDRNOTAVAIL;
660 #define INFINITY_LIFE_TIME 0xFFFFFFFF
662 static void check_lifetime(struct work_struct *work)
664 unsigned long now, next, next_sec, next_sched;
665 struct in_ifaddr *ifa;
666 struct hlist_node *n;
670 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
672 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
673 bool change_needed = false;
676 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
679 if (ifa->ifa_flags & IFA_F_PERMANENT)
682 /* We try to batch several events at once. */
683 age = (now - ifa->ifa_tstamp +
684 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
686 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
687 age >= ifa->ifa_valid_lft) {
688 change_needed = true;
689 } else if (ifa->ifa_preferred_lft ==
690 INFINITY_LIFE_TIME) {
692 } else if (age >= ifa->ifa_preferred_lft) {
693 if (time_before(ifa->ifa_tstamp +
694 ifa->ifa_valid_lft * HZ, next))
695 next = ifa->ifa_tstamp +
696 ifa->ifa_valid_lft * HZ;
698 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
699 change_needed = true;
700 } else if (time_before(ifa->ifa_tstamp +
701 ifa->ifa_preferred_lft * HZ,
703 next = ifa->ifa_tstamp +
704 ifa->ifa_preferred_lft * HZ;
711 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
714 if (ifa->ifa_flags & IFA_F_PERMANENT)
717 /* We try to batch several events at once. */
718 age = (now - ifa->ifa_tstamp +
719 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
721 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
722 age >= ifa->ifa_valid_lft) {
723 struct in_ifaddr **ifap;
725 for (ifap = &ifa->ifa_dev->ifa_list;
726 *ifap != NULL; ifap = &(*ifap)->ifa_next) {
728 inet_del_ifa(ifa->ifa_dev,
733 } else if (ifa->ifa_preferred_lft !=
734 INFINITY_LIFE_TIME &&
735 age >= ifa->ifa_preferred_lft &&
736 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
737 ifa->ifa_flags |= IFA_F_DEPRECATED;
738 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
744 next_sec = round_jiffies_up(next);
747 /* If rounded timeout is accurate enough, accept it. */
748 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
749 next_sched = next_sec;
752 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
753 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
754 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
756 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
760 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
763 unsigned long timeout;
765 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
767 timeout = addrconf_timeout_fixup(valid_lft, HZ);
768 if (addrconf_finite_timeout(timeout))
769 ifa->ifa_valid_lft = timeout;
771 ifa->ifa_flags |= IFA_F_PERMANENT;
773 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
774 if (addrconf_finite_timeout(timeout)) {
776 ifa->ifa_flags |= IFA_F_DEPRECATED;
777 ifa->ifa_preferred_lft = timeout;
779 ifa->ifa_tstamp = jiffies;
780 if (!ifa->ifa_cstamp)
781 ifa->ifa_cstamp = ifa->ifa_tstamp;
784 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
785 __u32 *pvalid_lft, __u32 *pprefered_lft,
786 struct netlink_ext_ack *extack)
788 struct nlattr *tb[IFA_MAX+1];
789 struct in_ifaddr *ifa;
790 struct ifaddrmsg *ifm;
791 struct net_device *dev;
792 struct in_device *in_dev;
795 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
800 ifm = nlmsg_data(nlh);
802 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
805 dev = __dev_get_by_index(net, ifm->ifa_index);
810 in_dev = __in_dev_get_rtnl(dev);
815 ifa = inet_alloc_ifa();
818 * A potential indev allocation can be left alive, it stays
819 * assigned to its device and is destroy with it.
823 ipv4_devconf_setall(in_dev);
824 neigh_parms_data_state_setall(in_dev->arp_parms);
827 if (!tb[IFA_ADDRESS])
828 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
830 INIT_HLIST_NODE(&ifa->hash);
831 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
832 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
833 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
835 ifa->ifa_scope = ifm->ifa_scope;
836 ifa->ifa_dev = in_dev;
838 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
839 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
841 if (tb[IFA_BROADCAST])
842 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
845 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
847 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
849 if (tb[IFA_RT_PRIORITY])
850 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
852 if (tb[IFA_CACHEINFO]) {
853 struct ifa_cacheinfo *ci;
855 ci = nla_data(tb[IFA_CACHEINFO]);
856 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
860 *pvalid_lft = ci->ifa_valid;
861 *pprefered_lft = ci->ifa_prefered;
872 static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
874 struct in_device *in_dev = ifa->ifa_dev;
875 struct in_ifaddr *ifa1, **ifap;
880 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
881 ifap = &ifa1->ifa_next) {
882 if (ifa1->ifa_mask == ifa->ifa_mask &&
883 inet_ifa_match(ifa1->ifa_address, ifa) &&
884 ifa1->ifa_local == ifa->ifa_local)
890 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
891 struct netlink_ext_ack *extack)
893 struct net *net = sock_net(skb->sk);
894 struct in_ifaddr *ifa;
895 struct in_ifaddr *ifa_existing;
896 __u32 valid_lft = INFINITY_LIFE_TIME;
897 __u32 prefered_lft = INFINITY_LIFE_TIME;
901 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
905 ifa_existing = find_matching_ifa(ifa);
907 /* It would be best to check for !NLM_F_CREATE here but
908 * userspace already relies on not having to provide this.
910 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
911 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
912 int ret = ip_mc_config(net->ipv4.mc_autojoin_sk,
920 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
923 u32 new_metric = ifa->ifa_rt_priority;
927 if (nlh->nlmsg_flags & NLM_F_EXCL ||
928 !(nlh->nlmsg_flags & NLM_F_REPLACE))
932 if (ifa->ifa_rt_priority != new_metric) {
933 fib_modify_prefix_metric(ifa, new_metric);
934 ifa->ifa_rt_priority = new_metric;
937 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
938 cancel_delayed_work(&check_lifetime_work);
939 queue_delayed_work(system_power_efficient_wq,
940 &check_lifetime_work, 0);
941 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
947 * Determine a default network mask, based on the IP address.
950 static int inet_abc_len(__be32 addr)
952 int rc = -1; /* Something else, probably a multicast. */
954 if (ipv4_is_zeronet(addr))
957 __u32 haddr = ntohl(addr);
959 if (IN_CLASSA(haddr))
961 else if (IN_CLASSB(haddr))
963 else if (IN_CLASSC(haddr))
971 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
973 struct sockaddr_in sin_orig;
974 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
975 struct in_device *in_dev;
976 struct in_ifaddr **ifap = NULL;
977 struct in_ifaddr *ifa = NULL;
978 struct net_device *dev;
981 int tryaddrmatch = 0;
983 ifr->ifr_name[IFNAMSIZ - 1] = 0;
985 /* save original address for comparison */
986 memcpy(&sin_orig, sin, sizeof(*sin));
988 colon = strchr(ifr->ifr_name, ':');
992 dev_load(net, ifr->ifr_name);
995 case SIOCGIFADDR: /* Get interface address */
996 case SIOCGIFBRDADDR: /* Get the broadcast address */
997 case SIOCGIFDSTADDR: /* Get the destination address */
998 case SIOCGIFNETMASK: /* Get the netmask for the interface */
999 /* Note that these ioctls will not sleep,
1000 so that we do not impose a lock.
1001 One day we will be forced to put shlock here (I mean SMP)
1003 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1004 memset(sin, 0, sizeof(*sin));
1005 sin->sin_family = AF_INET;
1010 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1013 case SIOCSIFADDR: /* Set interface address (and family) */
1014 case SIOCSIFBRDADDR: /* Set the broadcast address */
1015 case SIOCSIFDSTADDR: /* Set the destination address */
1016 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1018 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1021 if (sin->sin_family != AF_INET)
1032 dev = __dev_get_by_name(net, ifr->ifr_name);
1039 in_dev = __in_dev_get_rtnl(dev);
1042 /* Matthias Andree */
1043 /* compare label and address (4.4BSD style) */
1044 /* note: we only do this for a limited set of ioctls
1045 and only if the original address family was AF_INET.
1046 This is checked above. */
1047 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1048 ifap = &ifa->ifa_next) {
1049 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
1050 sin_orig.sin_addr.s_addr ==
1056 /* we didn't get a match, maybe the application is
1057 4.3BSD-style and passed in junk so we fall back to
1058 comparing just the label */
1060 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1061 ifap = &ifa->ifa_next)
1062 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
1067 ret = -EADDRNOTAVAIL;
1068 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1072 case SIOCGIFADDR: /* Get interface address */
1074 sin->sin_addr.s_addr = ifa->ifa_local;
1077 case SIOCGIFBRDADDR: /* Get the broadcast address */
1079 sin->sin_addr.s_addr = ifa->ifa_broadcast;
1082 case SIOCGIFDSTADDR: /* Get the destination address */
1084 sin->sin_addr.s_addr = ifa->ifa_address;
1087 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1089 sin->sin_addr.s_addr = ifa->ifa_mask;
1094 ret = -EADDRNOTAVAIL;
1098 if (!(ifr->ifr_flags & IFF_UP))
1099 inet_del_ifa(in_dev, ifap, 1);
1102 ret = dev_change_flags(dev, ifr->ifr_flags);
1105 case SIOCSIFADDR: /* Set interface address (and family) */
1107 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1112 ifa = inet_alloc_ifa();
1115 INIT_HLIST_NODE(&ifa->hash);
1117 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
1119 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1122 if (ifa->ifa_local == sin->sin_addr.s_addr)
1124 inet_del_ifa(in_dev, ifap, 0);
1125 ifa->ifa_broadcast = 0;
1129 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1131 if (!(dev->flags & IFF_POINTOPOINT)) {
1132 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1133 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1134 if ((dev->flags & IFF_BROADCAST) &&
1135 ifa->ifa_prefixlen < 31)
1136 ifa->ifa_broadcast = ifa->ifa_address |
1139 ifa->ifa_prefixlen = 32;
1140 ifa->ifa_mask = inet_make_mask(32);
1142 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
1143 ret = inet_set_ifa(dev, ifa);
1146 case SIOCSIFBRDADDR: /* Set the broadcast address */
1148 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1149 inet_del_ifa(in_dev, ifap, 0);
1150 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1151 inet_insert_ifa(ifa);
1155 case SIOCSIFDSTADDR: /* Set the destination address */
1157 if (ifa->ifa_address == sin->sin_addr.s_addr)
1160 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1163 inet_del_ifa(in_dev, ifap, 0);
1164 ifa->ifa_address = sin->sin_addr.s_addr;
1165 inet_insert_ifa(ifa);
1168 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1171 * The mask we set must be legal.
1174 if (bad_mask(sin->sin_addr.s_addr, 0))
1177 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1178 __be32 old_mask = ifa->ifa_mask;
1179 inet_del_ifa(in_dev, ifap, 0);
1180 ifa->ifa_mask = sin->sin_addr.s_addr;
1181 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1183 /* See if current broadcast address matches
1184 * with current netmask, then recalculate
1185 * the broadcast address. Otherwise it's a
1186 * funny address, so don't touch it since
1187 * the user seems to know what (s)he's doing...
1189 if ((dev->flags & IFF_BROADCAST) &&
1190 (ifa->ifa_prefixlen < 31) &&
1191 (ifa->ifa_broadcast ==
1192 (ifa->ifa_local|~old_mask))) {
1193 ifa->ifa_broadcast = (ifa->ifa_local |
1194 ~sin->sin_addr.s_addr);
1196 inet_insert_ifa(ifa);
1206 static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
1208 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1209 struct in_ifaddr *ifa;
1213 if (WARN_ON(size > sizeof(struct ifreq)))
1219 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1226 memset(&ifr, 0, sizeof(struct ifreq));
1227 strcpy(ifr.ifr_name, ifa->ifa_label);
1229 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1230 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1233 if (copy_to_user(buf + done, &ifr, size)) {
1244 static __be32 in_dev_select_addr(const struct in_device *in_dev,
1247 for_primary_ifa(in_dev) {
1248 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1249 ifa->ifa_scope <= scope)
1250 return ifa->ifa_local;
1251 } endfor_ifa(in_dev);
1256 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1259 struct in_device *in_dev;
1260 struct net *net = dev_net(dev);
1264 in_dev = __in_dev_get_rcu(dev);
1268 for_primary_ifa(in_dev) {
1269 if (ifa->ifa_scope > scope)
1271 if (!dst || inet_ifa_match(dst, ifa)) {
1272 addr = ifa->ifa_local;
1276 addr = ifa->ifa_local;
1277 } endfor_ifa(in_dev);
1282 master_idx = l3mdev_master_ifindex_rcu(dev);
1284 /* For VRFs, the VRF device takes the place of the loopback device,
1285 * with addresses on it being preferred. Note in such cases the
1286 * loopback device will be among the devices that fail the master_idx
1287 * equality check in the loop below.
1290 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1291 (in_dev = __in_dev_get_rcu(dev))) {
1292 addr = in_dev_select_addr(in_dev, scope);
1297 /* Not loopback addresses on loopback should be preferred
1298 in this case. It is important that lo is the first interface
1301 for_each_netdev_rcu(net, dev) {
1302 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1305 in_dev = __in_dev_get_rcu(dev);
1309 addr = in_dev_select_addr(in_dev, scope);
1317 EXPORT_SYMBOL(inet_select_addr);
1319 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1320 __be32 local, int scope)
1327 (local == ifa->ifa_local || !local) &&
1328 ifa->ifa_scope <= scope) {
1329 addr = ifa->ifa_local;
1334 same = (!local || inet_ifa_match(local, ifa)) &&
1335 (!dst || inet_ifa_match(dst, ifa));
1339 /* Is the selected addr into dst subnet? */
1340 if (inet_ifa_match(addr, ifa))
1342 /* No, then can we use new local src? */
1343 if (ifa->ifa_scope <= scope) {
1344 addr = ifa->ifa_local;
1347 /* search for large dst subnet for addr */
1351 } endfor_ifa(in_dev);
1353 return same ? addr : 0;
1357 * Confirm that local IP address exists using wildcards:
1358 * - net: netns to check, cannot be NULL
1359 * - in_dev: only on this interface, NULL=any interface
1360 * - dst: only in the same subnet as dst, 0=any dst
1361 * - local: address, 0=autoselect the local address
1362 * - scope: maximum allowed scope value for the local address
1364 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1365 __be32 dst, __be32 local, int scope)
1368 struct net_device *dev;
1371 return confirm_addr_indev(in_dev, dst, local, scope);
1374 for_each_netdev_rcu(net, dev) {
1375 in_dev = __in_dev_get_rcu(dev);
1377 addr = confirm_addr_indev(in_dev, dst, local, scope);
1386 EXPORT_SYMBOL(inet_confirm_addr);
1392 int register_inetaddr_notifier(struct notifier_block *nb)
1394 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1396 EXPORT_SYMBOL(register_inetaddr_notifier);
1398 int unregister_inetaddr_notifier(struct notifier_block *nb)
1400 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1402 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1404 int register_inetaddr_validator_notifier(struct notifier_block *nb)
1406 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1408 EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1410 int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1412 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1415 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1417 /* Rename ifa_labels for a device name change. Make some effort to preserve
1418 * existing alias numbering and to create unique labels if possible.
1420 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1422 struct in_ifaddr *ifa;
1425 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1426 char old[IFNAMSIZ], *dot;
1428 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1429 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1432 dot = strchr(old, ':');
1434 sprintf(old, ":%d", named);
1437 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1438 strcat(ifa->ifa_label, dot);
1440 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1442 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1446 static bool inetdev_valid_mtu(unsigned int mtu)
1448 return mtu >= IPV4_MIN_MTU;
1451 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1452 struct in_device *in_dev)
1455 struct in_ifaddr *ifa;
1457 for (ifa = in_dev->ifa_list; ifa;
1458 ifa = ifa->ifa_next) {
1459 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1460 ifa->ifa_local, dev,
1461 ifa->ifa_local, NULL,
1462 dev->dev_addr, NULL);
1466 /* Called only under RTNL semaphore */
1468 static int inetdev_event(struct notifier_block *this, unsigned long event,
1471 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1472 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1477 if (event == NETDEV_REGISTER) {
1478 in_dev = inetdev_init(dev);
1480 return notifier_from_errno(PTR_ERR(in_dev));
1481 if (dev->flags & IFF_LOOPBACK) {
1482 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1483 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1485 } else if (event == NETDEV_CHANGEMTU) {
1486 /* Re-enabling IP */
1487 if (inetdev_valid_mtu(dev->mtu))
1488 in_dev = inetdev_init(dev);
1494 case NETDEV_REGISTER:
1495 pr_debug("%s: bug\n", __func__);
1496 RCU_INIT_POINTER(dev->ip_ptr, NULL);
1499 if (!inetdev_valid_mtu(dev->mtu))
1501 if (dev->flags & IFF_LOOPBACK) {
1502 struct in_ifaddr *ifa = inet_alloc_ifa();
1505 INIT_HLIST_NODE(&ifa->hash);
1507 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1508 ifa->ifa_prefixlen = 8;
1509 ifa->ifa_mask = inet_make_mask(8);
1510 in_dev_hold(in_dev);
1511 ifa->ifa_dev = in_dev;
1512 ifa->ifa_scope = RT_SCOPE_HOST;
1513 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1514 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1515 INFINITY_LIFE_TIME);
1516 ipv4_devconf_setall(in_dev);
1517 neigh_parms_data_state_setall(in_dev->arp_parms);
1518 inet_insert_ifa(ifa);
1523 case NETDEV_CHANGEADDR:
1524 if (!IN_DEV_ARP_NOTIFY(in_dev))
1527 case NETDEV_NOTIFY_PEERS:
1528 /* Send gratuitous ARP to notify of link change */
1529 inetdev_send_gratuitous_arp(dev, in_dev);
1534 case NETDEV_PRE_TYPE_CHANGE:
1535 ip_mc_unmap(in_dev);
1537 case NETDEV_POST_TYPE_CHANGE:
1538 ip_mc_remap(in_dev);
1540 case NETDEV_CHANGEMTU:
1541 if (inetdev_valid_mtu(dev->mtu))
1543 /* disable IP when MTU is not enough */
1545 case NETDEV_UNREGISTER:
1546 inetdev_destroy(in_dev);
1548 case NETDEV_CHANGENAME:
1549 /* Do not notify about label change, this event is
1550 * not interesting to applications using netlink.
1552 inetdev_changename(dev, in_dev);
1554 devinet_sysctl_unregister(in_dev);
1555 devinet_sysctl_register(in_dev);
1562 static struct notifier_block ip_netdev_notifier = {
1563 .notifier_call = inetdev_event,
1566 static size_t inet_nlmsg_size(void)
1568 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1569 + nla_total_size(4) /* IFA_ADDRESS */
1570 + nla_total_size(4) /* IFA_LOCAL */
1571 + nla_total_size(4) /* IFA_BROADCAST */
1572 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
1573 + nla_total_size(4) /* IFA_FLAGS */
1574 + nla_total_size(4) /* IFA_RT_PRIORITY */
1575 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
1578 static inline u32 cstamp_delta(unsigned long cstamp)
1580 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1583 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1584 unsigned long tstamp, u32 preferred, u32 valid)
1586 struct ifa_cacheinfo ci;
1588 ci.cstamp = cstamp_delta(cstamp);
1589 ci.tstamp = cstamp_delta(tstamp);
1590 ci.ifa_prefered = preferred;
1591 ci.ifa_valid = valid;
1593 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1596 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1597 struct inet_fill_args *args)
1599 struct ifaddrmsg *ifm;
1600 struct nlmsghdr *nlh;
1601 u32 preferred, valid;
1603 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1608 ifm = nlmsg_data(nlh);
1609 ifm->ifa_family = AF_INET;
1610 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1611 ifm->ifa_flags = ifa->ifa_flags;
1612 ifm->ifa_scope = ifa->ifa_scope;
1613 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1615 if (args->netnsid >= 0 &&
1616 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
1617 goto nla_put_failure;
1619 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1620 preferred = ifa->ifa_preferred_lft;
1621 valid = ifa->ifa_valid_lft;
1622 if (preferred != INFINITY_LIFE_TIME) {
1623 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1625 if (preferred > tval)
1629 if (valid != INFINITY_LIFE_TIME) {
1637 preferred = INFINITY_LIFE_TIME;
1638 valid = INFINITY_LIFE_TIME;
1640 if ((ifa->ifa_address &&
1641 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1643 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
1644 (ifa->ifa_broadcast &&
1645 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1646 (ifa->ifa_label[0] &&
1647 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1648 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
1649 (ifa->ifa_rt_priority &&
1650 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
1651 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1653 goto nla_put_failure;
1655 nlmsg_end(skb, nlh);
1659 nlmsg_cancel(skb, nlh);
1663 static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1664 struct inet_fill_args *fillargs,
1665 struct net **tgt_net, struct sock *sk,
1666 struct netlink_ext_ack *extack)
1668 struct nlattr *tb[IFA_MAX+1];
1669 struct ifaddrmsg *ifm;
1672 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1673 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1677 ifm = nlmsg_data(nlh);
1678 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1679 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1682 if (ifm->ifa_index) {
1683 NL_SET_ERR_MSG(extack, "ipv4: Filter by device index not supported for address dump");
1687 err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1688 ifa_ipv4_policy, extack);
1692 for (i = 0; i <= IFA_MAX; ++i) {
1696 if (i == IFA_TARGET_NETNSID) {
1699 fillargs->netnsid = nla_get_s32(tb[i]);
1701 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1703 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1704 return PTR_ERR(net);
1708 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1716 static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1717 struct netlink_callback *cb, int s_ip_idx,
1718 struct inet_fill_args *fillargs)
1720 struct in_ifaddr *ifa;
1724 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next, ip_idx++) {
1725 if (ip_idx < s_ip_idx)
1728 err = inet_fill_ifaddr(skb, ifa, fillargs);
1732 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1737 cb->args[2] = ip_idx;
1742 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1744 const struct nlmsghdr *nlh = cb->nlh;
1745 struct inet_fill_args fillargs = {
1746 .portid = NETLINK_CB(cb->skb).portid,
1747 .seq = nlh->nlmsg_seq,
1748 .event = RTM_NEWADDR,
1749 .flags = NLM_F_MULTI,
1752 struct net *net = sock_net(skb->sk);
1753 struct net *tgt_net = net;
1757 struct net_device *dev;
1758 struct in_device *in_dev;
1759 struct hlist_head *head;
1763 s_idx = idx = cb->args[1];
1764 s_ip_idx = cb->args[2];
1766 if (cb->strict_check) {
1767 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
1768 skb->sk, cb->extack);
1773 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1775 head = &tgt_net->dev_index_head[h];
1777 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1778 tgt_net->dev_base_seq;
1779 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1782 if (h > s_h || idx > s_idx)
1784 in_dev = __in_dev_get_rcu(dev);
1788 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1803 if (fillargs.netnsid >= 0)
1809 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1812 struct inet_fill_args fillargs = {
1814 .seq = nlh ? nlh->nlmsg_seq : 0,
1819 struct sk_buff *skb;
1823 net = dev_net(ifa->ifa_dev->dev);
1824 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1828 err = inet_fill_ifaddr(skb, ifa, &fillargs);
1830 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1831 WARN_ON(err == -EMSGSIZE);
1835 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1839 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1842 static size_t inet_get_link_af_size(const struct net_device *dev,
1843 u32 ext_filter_mask)
1845 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1850 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1853 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1854 u32 ext_filter_mask)
1856 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1863 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1867 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1868 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1873 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1874 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1877 static int inet_validate_link_af(const struct net_device *dev,
1878 const struct nlattr *nla)
1880 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1883 if (dev && !__in_dev_get_rcu(dev))
1884 return -EAFNOSUPPORT;
1886 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy, NULL);
1890 if (tb[IFLA_INET_CONF]) {
1891 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1892 int cfgid = nla_type(a);
1897 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1905 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1907 struct in_device *in_dev = __in_dev_get_rcu(dev);
1908 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1912 return -EAFNOSUPPORT;
1914 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
1917 if (tb[IFLA_INET_CONF]) {
1918 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1919 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1925 static int inet_netconf_msgsize_devconf(int type)
1927 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1928 + nla_total_size(4); /* NETCONFA_IFINDEX */
1931 if (type == NETCONFA_ALL)
1934 if (all || type == NETCONFA_FORWARDING)
1935 size += nla_total_size(4);
1936 if (all || type == NETCONFA_RP_FILTER)
1937 size += nla_total_size(4);
1938 if (all || type == NETCONFA_MC_FORWARDING)
1939 size += nla_total_size(4);
1940 if (all || type == NETCONFA_BC_FORWARDING)
1941 size += nla_total_size(4);
1942 if (all || type == NETCONFA_PROXY_NEIGH)
1943 size += nla_total_size(4);
1944 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
1945 size += nla_total_size(4);
1950 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1951 struct ipv4_devconf *devconf, u32 portid,
1952 u32 seq, int event, unsigned int flags,
1955 struct nlmsghdr *nlh;
1956 struct netconfmsg *ncm;
1959 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1964 if (type == NETCONFA_ALL)
1967 ncm = nlmsg_data(nlh);
1968 ncm->ncm_family = AF_INET;
1970 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1971 goto nla_put_failure;
1976 if ((all || type == NETCONFA_FORWARDING) &&
1977 nla_put_s32(skb, NETCONFA_FORWARDING,
1978 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
1979 goto nla_put_failure;
1980 if ((all || type == NETCONFA_RP_FILTER) &&
1981 nla_put_s32(skb, NETCONFA_RP_FILTER,
1982 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
1983 goto nla_put_failure;
1984 if ((all || type == NETCONFA_MC_FORWARDING) &&
1985 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
1986 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
1987 goto nla_put_failure;
1988 if ((all || type == NETCONFA_BC_FORWARDING) &&
1989 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
1990 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
1991 goto nla_put_failure;
1992 if ((all || type == NETCONFA_PROXY_NEIGH) &&
1993 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
1994 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
1995 goto nla_put_failure;
1996 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
1997 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
1998 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
1999 goto nla_put_failure;
2002 nlmsg_end(skb, nlh);
2006 nlmsg_cancel(skb, nlh);
2010 void inet_netconf_notify_devconf(struct net *net, int event, int type,
2011 int ifindex, struct ipv4_devconf *devconf)
2013 struct sk_buff *skb;
2016 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
2020 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
2023 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2024 WARN_ON(err == -EMSGSIZE);
2028 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
2032 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2035 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2036 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
2037 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
2038 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
2039 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
2040 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
2043 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
2044 struct nlmsghdr *nlh,
2045 struct netlink_ext_ack *extack)
2047 struct net *net = sock_net(in_skb->sk);
2048 struct nlattr *tb[NETCONFA_MAX+1];
2049 struct netconfmsg *ncm;
2050 struct sk_buff *skb;
2051 struct ipv4_devconf *devconf;
2052 struct in_device *in_dev;
2053 struct net_device *dev;
2057 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
2058 devconf_ipv4_policy, extack);
2063 if (!tb[NETCONFA_IFINDEX])
2066 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2068 case NETCONFA_IFINDEX_ALL:
2069 devconf = net->ipv4.devconf_all;
2071 case NETCONFA_IFINDEX_DEFAULT:
2072 devconf = net->ipv4.devconf_dflt;
2075 dev = __dev_get_by_index(net, ifindex);
2078 in_dev = __in_dev_get_rtnl(dev);
2081 devconf = &in_dev->cnf;
2086 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
2090 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2091 NETLINK_CB(in_skb).portid,
2092 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
2095 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2096 WARN_ON(err == -EMSGSIZE);
2100 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2105 static int inet_netconf_dump_devconf(struct sk_buff *skb,
2106 struct netlink_callback *cb)
2108 const struct nlmsghdr *nlh = cb->nlh;
2109 struct net *net = sock_net(skb->sk);
2112 struct net_device *dev;
2113 struct in_device *in_dev;
2114 struct hlist_head *head;
2116 if (cb->strict_check) {
2117 struct netlink_ext_ack *extack = cb->extack;
2118 struct netconfmsg *ncm;
2120 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2121 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2125 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2126 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2132 s_idx = idx = cb->args[1];
2134 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2136 head = &net->dev_index_head[h];
2138 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2140 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2143 in_dev = __in_dev_get_rcu(dev);
2147 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2149 NETLINK_CB(cb->skb).portid,
2153 NETCONFA_ALL) < 0) {
2157 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2163 if (h == NETDEV_HASHENTRIES) {
2164 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2165 net->ipv4.devconf_all,
2166 NETLINK_CB(cb->skb).portid,
2168 RTM_NEWNETCONF, NLM_F_MULTI,
2174 if (h == NETDEV_HASHENTRIES + 1) {
2175 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2176 net->ipv4.devconf_dflt,
2177 NETLINK_CB(cb->skb).portid,
2179 RTM_NEWNETCONF, NLM_F_MULTI,
2192 #ifdef CONFIG_SYSCTL
2194 static void devinet_copy_dflt_conf(struct net *net, int i)
2196 struct net_device *dev;
2199 for_each_netdev_rcu(net, dev) {
2200 struct in_device *in_dev;
2202 in_dev = __in_dev_get_rcu(dev);
2203 if (in_dev && !test_bit(i, in_dev->cnf.state))
2204 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
2209 /* called with RTNL locked */
2210 static void inet_forward_change(struct net *net)
2212 struct net_device *dev;
2213 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
2215 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
2216 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
2217 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2218 NETCONFA_FORWARDING,
2219 NETCONFA_IFINDEX_ALL,
2220 net->ipv4.devconf_all);
2221 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2222 NETCONFA_FORWARDING,
2223 NETCONFA_IFINDEX_DEFAULT,
2224 net->ipv4.devconf_dflt);
2226 for_each_netdev(net, dev) {
2227 struct in_device *in_dev;
2230 dev_disable_lro(dev);
2232 in_dev = __in_dev_get_rtnl(dev);
2234 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
2235 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2236 NETCONFA_FORWARDING,
2237 dev->ifindex, &in_dev->cnf);
2242 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2244 if (cnf == net->ipv4.devconf_dflt)
2245 return NETCONFA_IFINDEX_DEFAULT;
2246 else if (cnf == net->ipv4.devconf_all)
2247 return NETCONFA_IFINDEX_ALL;
2249 struct in_device *idev
2250 = container_of(cnf, struct in_device, cnf);
2251 return idev->dev->ifindex;
2255 static int devinet_conf_proc(struct ctl_table *ctl, int write,
2256 void __user *buffer,
2257 size_t *lenp, loff_t *ppos)
2259 int old_value = *(int *)ctl->data;
2260 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2261 int new_value = *(int *)ctl->data;
2264 struct ipv4_devconf *cnf = ctl->extra1;
2265 struct net *net = ctl->extra2;
2266 int i = (int *)ctl->data - cnf->data;
2269 set_bit(i, cnf->state);
2271 if (cnf == net->ipv4.devconf_dflt)
2272 devinet_copy_dflt_conf(net, i);
2273 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2274 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
2275 if ((new_value == 0) && (old_value != 0))
2276 rt_cache_flush(net);
2278 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2279 new_value != old_value)
2280 rt_cache_flush(net);
2282 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2283 new_value != old_value) {
2284 ifindex = devinet_conf_ifindex(net, cnf);
2285 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2289 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2290 new_value != old_value) {
2291 ifindex = devinet_conf_ifindex(net, cnf);
2292 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2293 NETCONFA_PROXY_NEIGH,
2296 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2297 new_value != old_value) {
2298 ifindex = devinet_conf_ifindex(net, cnf);
2299 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2300 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2308 static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
2309 void __user *buffer,
2310 size_t *lenp, loff_t *ppos)
2312 int *valp = ctl->data;
2315 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2317 if (write && *valp != val) {
2318 struct net *net = ctl->extra2;
2320 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2321 if (!rtnl_trylock()) {
2322 /* Restore the original values before restarting */
2325 return restart_syscall();
2327 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2328 inet_forward_change(net);
2330 struct ipv4_devconf *cnf = ctl->extra1;
2331 struct in_device *idev =
2332 container_of(cnf, struct in_device, cnf);
2334 dev_disable_lro(idev->dev);
2335 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2336 NETCONFA_FORWARDING,
2341 rt_cache_flush(net);
2343 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2344 NETCONFA_FORWARDING,
2345 NETCONFA_IFINDEX_DEFAULT,
2346 net->ipv4.devconf_dflt);
2352 static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
2353 void __user *buffer,
2354 size_t *lenp, loff_t *ppos)
2356 int *valp = ctl->data;
2358 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2359 struct net *net = ctl->extra2;
2361 if (write && *valp != val)
2362 rt_cache_flush(net);
2367 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2370 .data = ipv4_devconf.data + \
2371 IPV4_DEVCONF_ ## attr - 1, \
2372 .maxlen = sizeof(int), \
2374 .proc_handler = proc, \
2375 .extra1 = &ipv4_devconf, \
2378 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2379 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2381 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2382 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2384 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2385 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2387 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2388 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2390 static struct devinet_sysctl_table {
2391 struct ctl_table_header *sysctl_header;
2392 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2393 } devinet_sysctl = {
2395 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2396 devinet_sysctl_forward),
2397 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2398 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
2400 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2401 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2402 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2403 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2404 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2405 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2406 "accept_source_route"),
2407 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2408 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2409 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2410 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2411 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2412 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2413 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2414 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2415 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2416 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2417 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2418 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2419 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2420 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2421 "force_igmp_version"),
2422 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2423 "igmpv2_unsolicited_report_interval"),
2424 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2425 "igmpv3_unsolicited_report_interval"),
2426 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2427 "ignore_routes_with_linkdown"),
2428 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2429 "drop_gratuitous_arp"),
2431 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2432 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
2433 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2434 "promote_secondaries"),
2435 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2437 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2438 "drop_unicast_in_l2_multicast"),
2442 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2443 int ifindex, struct ipv4_devconf *p)
2446 struct devinet_sysctl_table *t;
2447 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2449 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
2453 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2454 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2455 t->devinet_vars[i].extra1 = p;
2456 t->devinet_vars[i].extra2 = net;
2459 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2461 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2462 if (!t->sysctl_header)
2467 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2477 static void __devinet_sysctl_unregister(struct net *net,
2478 struct ipv4_devconf *cnf, int ifindex)
2480 struct devinet_sysctl_table *t = cnf->sysctl;
2484 unregister_net_sysctl_table(t->sysctl_header);
2488 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
2491 static int devinet_sysctl_register(struct in_device *idev)
2495 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2498 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2501 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2502 idev->dev->ifindex, &idev->cnf);
2504 neigh_sysctl_unregister(idev->arp_parms);
2508 static void devinet_sysctl_unregister(struct in_device *idev)
2510 struct net *net = dev_net(idev->dev);
2512 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
2513 neigh_sysctl_unregister(idev->arp_parms);
2516 static struct ctl_table ctl_forward_entry[] = {
2518 .procname = "ip_forward",
2519 .data = &ipv4_devconf.data[
2520 IPV4_DEVCONF_FORWARDING - 1],
2521 .maxlen = sizeof(int),
2523 .proc_handler = devinet_sysctl_forward,
2524 .extra1 = &ipv4_devconf,
2525 .extra2 = &init_net,
2531 static __net_init int devinet_init_net(struct net *net)
2534 struct ipv4_devconf *all, *dflt;
2535 #ifdef CONFIG_SYSCTL
2536 struct ctl_table *tbl = ctl_forward_entry;
2537 struct ctl_table_header *forw_hdr;
2541 all = &ipv4_devconf;
2542 dflt = &ipv4_devconf_dflt;
2544 if (!net_eq(net, &init_net)) {
2545 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
2549 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2551 goto err_alloc_dflt;
2553 #ifdef CONFIG_SYSCTL
2554 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
2558 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2559 tbl[0].extra1 = all;
2560 tbl[0].extra2 = net;
2564 #ifdef CONFIG_SYSCTL
2565 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
2569 err = __devinet_sysctl_register(net, "default",
2570 NETCONFA_IFINDEX_DEFAULT, dflt);
2575 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2578 net->ipv4.forw_hdr = forw_hdr;
2581 net->ipv4.devconf_all = all;
2582 net->ipv4.devconf_dflt = dflt;
2585 #ifdef CONFIG_SYSCTL
2587 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
2589 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
2591 if (tbl != ctl_forward_entry)
2595 if (dflt != &ipv4_devconf_dflt)
2598 if (all != &ipv4_devconf)
2604 static __net_exit void devinet_exit_net(struct net *net)
2606 #ifdef CONFIG_SYSCTL
2607 struct ctl_table *tbl;
2609 tbl = net->ipv4.forw_hdr->ctl_table_arg;
2610 unregister_net_sysctl_table(net->ipv4.forw_hdr);
2611 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2612 NETCONFA_IFINDEX_DEFAULT);
2613 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2614 NETCONFA_IFINDEX_ALL);
2617 kfree(net->ipv4.devconf_dflt);
2618 kfree(net->ipv4.devconf_all);
2621 static __net_initdata struct pernet_operations devinet_ops = {
2622 .init = devinet_init_net,
2623 .exit = devinet_exit_net,
2626 static struct rtnl_af_ops inet_af_ops __read_mostly = {
2628 .fill_link_af = inet_fill_link_af,
2629 .get_link_af_size = inet_get_link_af_size,
2630 .validate_link_af = inet_validate_link_af,
2631 .set_link_af = inet_set_link_af,
2634 void __init devinet_init(void)
2638 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2639 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2641 register_pernet_subsys(&devinet_ops);
2643 register_gifconf(PF_INET, inet_gifconf);
2644 register_netdevice_notifier(&ip_netdev_notifier);
2646 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
2648 rtnl_af_register(&inet_af_ops);
2650 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2651 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2652 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
2653 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
2654 inet_netconf_dump_devconf, 0);