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 inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1718 const struct nlmsghdr *nlh = cb->nlh;
1719 struct inet_fill_args fillargs = {
1720 .portid = NETLINK_CB(cb->skb).portid,
1721 .seq = nlh->nlmsg_seq,
1722 .event = RTM_NEWADDR,
1723 .flags = NLM_F_MULTI,
1726 struct net *net = sock_net(skb->sk);
1727 struct net *tgt_net = net;
1730 int ip_idx, s_ip_idx;
1731 struct net_device *dev;
1732 struct in_device *in_dev;
1733 struct in_ifaddr *ifa;
1734 struct hlist_head *head;
1737 s_idx = idx = cb->args[1];
1738 s_ip_idx = ip_idx = cb->args[2];
1740 if (cb->strict_check) {
1743 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
1744 skb->sk, cb->extack);
1749 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1751 head = &tgt_net->dev_index_head[h];
1753 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1754 tgt_net->dev_base_seq;
1755 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1758 if (h > s_h || idx > s_idx)
1760 in_dev = __in_dev_get_rcu(dev);
1764 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1765 ifa = ifa->ifa_next, ip_idx++) {
1766 if (ip_idx < s_ip_idx)
1768 if (inet_fill_ifaddr(skb, ifa, &fillargs) < 0) {
1772 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1783 cb->args[2] = ip_idx;
1784 if (fillargs.netnsid >= 0)
1790 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1793 struct inet_fill_args fillargs = {
1795 .seq = nlh ? nlh->nlmsg_seq : 0,
1800 struct sk_buff *skb;
1804 net = dev_net(ifa->ifa_dev->dev);
1805 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1809 err = inet_fill_ifaddr(skb, ifa, &fillargs);
1811 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1812 WARN_ON(err == -EMSGSIZE);
1816 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1820 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1823 static size_t inet_get_link_af_size(const struct net_device *dev,
1824 u32 ext_filter_mask)
1826 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1831 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1834 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1835 u32 ext_filter_mask)
1837 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1844 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1848 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1849 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1854 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1855 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1858 static int inet_validate_link_af(const struct net_device *dev,
1859 const struct nlattr *nla)
1861 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1864 if (dev && !__in_dev_get_rcu(dev))
1865 return -EAFNOSUPPORT;
1867 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy, NULL);
1871 if (tb[IFLA_INET_CONF]) {
1872 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1873 int cfgid = nla_type(a);
1878 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1886 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1888 struct in_device *in_dev = __in_dev_get_rcu(dev);
1889 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1893 return -EAFNOSUPPORT;
1895 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
1898 if (tb[IFLA_INET_CONF]) {
1899 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1900 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1906 static int inet_netconf_msgsize_devconf(int type)
1908 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1909 + nla_total_size(4); /* NETCONFA_IFINDEX */
1912 if (type == NETCONFA_ALL)
1915 if (all || type == NETCONFA_FORWARDING)
1916 size += nla_total_size(4);
1917 if (all || type == NETCONFA_RP_FILTER)
1918 size += nla_total_size(4);
1919 if (all || type == NETCONFA_MC_FORWARDING)
1920 size += nla_total_size(4);
1921 if (all || type == NETCONFA_BC_FORWARDING)
1922 size += nla_total_size(4);
1923 if (all || type == NETCONFA_PROXY_NEIGH)
1924 size += nla_total_size(4);
1925 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
1926 size += nla_total_size(4);
1931 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1932 struct ipv4_devconf *devconf, u32 portid,
1933 u32 seq, int event, unsigned int flags,
1936 struct nlmsghdr *nlh;
1937 struct netconfmsg *ncm;
1940 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1945 if (type == NETCONFA_ALL)
1948 ncm = nlmsg_data(nlh);
1949 ncm->ncm_family = AF_INET;
1951 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1952 goto nla_put_failure;
1957 if ((all || type == NETCONFA_FORWARDING) &&
1958 nla_put_s32(skb, NETCONFA_FORWARDING,
1959 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
1960 goto nla_put_failure;
1961 if ((all || type == NETCONFA_RP_FILTER) &&
1962 nla_put_s32(skb, NETCONFA_RP_FILTER,
1963 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
1964 goto nla_put_failure;
1965 if ((all || type == NETCONFA_MC_FORWARDING) &&
1966 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
1967 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
1968 goto nla_put_failure;
1969 if ((all || type == NETCONFA_BC_FORWARDING) &&
1970 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
1971 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
1972 goto nla_put_failure;
1973 if ((all || type == NETCONFA_PROXY_NEIGH) &&
1974 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
1975 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
1976 goto nla_put_failure;
1977 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
1978 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
1979 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
1980 goto nla_put_failure;
1983 nlmsg_end(skb, nlh);
1987 nlmsg_cancel(skb, nlh);
1991 void inet_netconf_notify_devconf(struct net *net, int event, int type,
1992 int ifindex, struct ipv4_devconf *devconf)
1994 struct sk_buff *skb;
1997 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
2001 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
2004 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2005 WARN_ON(err == -EMSGSIZE);
2009 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
2013 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2016 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2017 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
2018 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
2019 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
2020 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
2021 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
2024 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
2025 struct nlmsghdr *nlh,
2026 struct netlink_ext_ack *extack)
2028 struct net *net = sock_net(in_skb->sk);
2029 struct nlattr *tb[NETCONFA_MAX+1];
2030 struct netconfmsg *ncm;
2031 struct sk_buff *skb;
2032 struct ipv4_devconf *devconf;
2033 struct in_device *in_dev;
2034 struct net_device *dev;
2038 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
2039 devconf_ipv4_policy, extack);
2044 if (!tb[NETCONFA_IFINDEX])
2047 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2049 case NETCONFA_IFINDEX_ALL:
2050 devconf = net->ipv4.devconf_all;
2052 case NETCONFA_IFINDEX_DEFAULT:
2053 devconf = net->ipv4.devconf_dflt;
2056 dev = __dev_get_by_index(net, ifindex);
2059 in_dev = __in_dev_get_rtnl(dev);
2062 devconf = &in_dev->cnf;
2067 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
2071 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2072 NETLINK_CB(in_skb).portid,
2073 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
2076 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2077 WARN_ON(err == -EMSGSIZE);
2081 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2086 static int inet_netconf_dump_devconf(struct sk_buff *skb,
2087 struct netlink_callback *cb)
2089 const struct nlmsghdr *nlh = cb->nlh;
2090 struct net *net = sock_net(skb->sk);
2093 struct net_device *dev;
2094 struct in_device *in_dev;
2095 struct hlist_head *head;
2097 if (cb->strict_check) {
2098 struct netlink_ext_ack *extack = cb->extack;
2099 struct netconfmsg *ncm;
2101 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2102 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2106 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2107 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2113 s_idx = idx = cb->args[1];
2115 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2117 head = &net->dev_index_head[h];
2119 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2121 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2124 in_dev = __in_dev_get_rcu(dev);
2128 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2130 NETLINK_CB(cb->skb).portid,
2134 NETCONFA_ALL) < 0) {
2138 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2144 if (h == NETDEV_HASHENTRIES) {
2145 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2146 net->ipv4.devconf_all,
2147 NETLINK_CB(cb->skb).portid,
2149 RTM_NEWNETCONF, NLM_F_MULTI,
2155 if (h == NETDEV_HASHENTRIES + 1) {
2156 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2157 net->ipv4.devconf_dflt,
2158 NETLINK_CB(cb->skb).portid,
2160 RTM_NEWNETCONF, NLM_F_MULTI,
2173 #ifdef CONFIG_SYSCTL
2175 static void devinet_copy_dflt_conf(struct net *net, int i)
2177 struct net_device *dev;
2180 for_each_netdev_rcu(net, dev) {
2181 struct in_device *in_dev;
2183 in_dev = __in_dev_get_rcu(dev);
2184 if (in_dev && !test_bit(i, in_dev->cnf.state))
2185 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
2190 /* called with RTNL locked */
2191 static void inet_forward_change(struct net *net)
2193 struct net_device *dev;
2194 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
2196 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
2197 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
2198 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2199 NETCONFA_FORWARDING,
2200 NETCONFA_IFINDEX_ALL,
2201 net->ipv4.devconf_all);
2202 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2203 NETCONFA_FORWARDING,
2204 NETCONFA_IFINDEX_DEFAULT,
2205 net->ipv4.devconf_dflt);
2207 for_each_netdev(net, dev) {
2208 struct in_device *in_dev;
2211 dev_disable_lro(dev);
2213 in_dev = __in_dev_get_rtnl(dev);
2215 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
2216 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2217 NETCONFA_FORWARDING,
2218 dev->ifindex, &in_dev->cnf);
2223 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2225 if (cnf == net->ipv4.devconf_dflt)
2226 return NETCONFA_IFINDEX_DEFAULT;
2227 else if (cnf == net->ipv4.devconf_all)
2228 return NETCONFA_IFINDEX_ALL;
2230 struct in_device *idev
2231 = container_of(cnf, struct in_device, cnf);
2232 return idev->dev->ifindex;
2236 static int devinet_conf_proc(struct ctl_table *ctl, int write,
2237 void __user *buffer,
2238 size_t *lenp, loff_t *ppos)
2240 int old_value = *(int *)ctl->data;
2241 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2242 int new_value = *(int *)ctl->data;
2245 struct ipv4_devconf *cnf = ctl->extra1;
2246 struct net *net = ctl->extra2;
2247 int i = (int *)ctl->data - cnf->data;
2250 set_bit(i, cnf->state);
2252 if (cnf == net->ipv4.devconf_dflt)
2253 devinet_copy_dflt_conf(net, i);
2254 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2255 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
2256 if ((new_value == 0) && (old_value != 0))
2257 rt_cache_flush(net);
2259 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2260 new_value != old_value)
2261 rt_cache_flush(net);
2263 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2264 new_value != old_value) {
2265 ifindex = devinet_conf_ifindex(net, cnf);
2266 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2270 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2271 new_value != old_value) {
2272 ifindex = devinet_conf_ifindex(net, cnf);
2273 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2274 NETCONFA_PROXY_NEIGH,
2277 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2278 new_value != old_value) {
2279 ifindex = devinet_conf_ifindex(net, cnf);
2280 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2281 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2289 static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
2290 void __user *buffer,
2291 size_t *lenp, loff_t *ppos)
2293 int *valp = ctl->data;
2296 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2298 if (write && *valp != val) {
2299 struct net *net = ctl->extra2;
2301 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2302 if (!rtnl_trylock()) {
2303 /* Restore the original values before restarting */
2306 return restart_syscall();
2308 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2309 inet_forward_change(net);
2311 struct ipv4_devconf *cnf = ctl->extra1;
2312 struct in_device *idev =
2313 container_of(cnf, struct in_device, cnf);
2315 dev_disable_lro(idev->dev);
2316 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2317 NETCONFA_FORWARDING,
2322 rt_cache_flush(net);
2324 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2325 NETCONFA_FORWARDING,
2326 NETCONFA_IFINDEX_DEFAULT,
2327 net->ipv4.devconf_dflt);
2333 static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
2334 void __user *buffer,
2335 size_t *lenp, loff_t *ppos)
2337 int *valp = ctl->data;
2339 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2340 struct net *net = ctl->extra2;
2342 if (write && *valp != val)
2343 rt_cache_flush(net);
2348 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2351 .data = ipv4_devconf.data + \
2352 IPV4_DEVCONF_ ## attr - 1, \
2353 .maxlen = sizeof(int), \
2355 .proc_handler = proc, \
2356 .extra1 = &ipv4_devconf, \
2359 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2360 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2362 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2363 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2365 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2366 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2368 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2369 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2371 static struct devinet_sysctl_table {
2372 struct ctl_table_header *sysctl_header;
2373 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2374 } devinet_sysctl = {
2376 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2377 devinet_sysctl_forward),
2378 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2379 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
2381 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2382 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2383 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2384 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2385 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2386 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2387 "accept_source_route"),
2388 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2389 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2390 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2391 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2392 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2393 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2394 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2395 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2396 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2397 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2398 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2399 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2400 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2401 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2402 "force_igmp_version"),
2403 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2404 "igmpv2_unsolicited_report_interval"),
2405 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2406 "igmpv3_unsolicited_report_interval"),
2407 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2408 "ignore_routes_with_linkdown"),
2409 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2410 "drop_gratuitous_arp"),
2412 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2413 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
2414 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2415 "promote_secondaries"),
2416 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2418 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2419 "drop_unicast_in_l2_multicast"),
2423 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2424 int ifindex, struct ipv4_devconf *p)
2427 struct devinet_sysctl_table *t;
2428 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2430 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
2434 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2435 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2436 t->devinet_vars[i].extra1 = p;
2437 t->devinet_vars[i].extra2 = net;
2440 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2442 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2443 if (!t->sysctl_header)
2448 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2458 static void __devinet_sysctl_unregister(struct net *net,
2459 struct ipv4_devconf *cnf, int ifindex)
2461 struct devinet_sysctl_table *t = cnf->sysctl;
2465 unregister_net_sysctl_table(t->sysctl_header);
2469 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
2472 static int devinet_sysctl_register(struct in_device *idev)
2476 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2479 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2482 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2483 idev->dev->ifindex, &idev->cnf);
2485 neigh_sysctl_unregister(idev->arp_parms);
2489 static void devinet_sysctl_unregister(struct in_device *idev)
2491 struct net *net = dev_net(idev->dev);
2493 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
2494 neigh_sysctl_unregister(idev->arp_parms);
2497 static struct ctl_table ctl_forward_entry[] = {
2499 .procname = "ip_forward",
2500 .data = &ipv4_devconf.data[
2501 IPV4_DEVCONF_FORWARDING - 1],
2502 .maxlen = sizeof(int),
2504 .proc_handler = devinet_sysctl_forward,
2505 .extra1 = &ipv4_devconf,
2506 .extra2 = &init_net,
2512 static __net_init int devinet_init_net(struct net *net)
2515 struct ipv4_devconf *all, *dflt;
2516 #ifdef CONFIG_SYSCTL
2517 struct ctl_table *tbl = ctl_forward_entry;
2518 struct ctl_table_header *forw_hdr;
2522 all = &ipv4_devconf;
2523 dflt = &ipv4_devconf_dflt;
2525 if (!net_eq(net, &init_net)) {
2526 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
2530 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2532 goto err_alloc_dflt;
2534 #ifdef CONFIG_SYSCTL
2535 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
2539 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2540 tbl[0].extra1 = all;
2541 tbl[0].extra2 = net;
2545 #ifdef CONFIG_SYSCTL
2546 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
2550 err = __devinet_sysctl_register(net, "default",
2551 NETCONFA_IFINDEX_DEFAULT, dflt);
2556 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2559 net->ipv4.forw_hdr = forw_hdr;
2562 net->ipv4.devconf_all = all;
2563 net->ipv4.devconf_dflt = dflt;
2566 #ifdef CONFIG_SYSCTL
2568 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
2570 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
2572 if (tbl != ctl_forward_entry)
2576 if (dflt != &ipv4_devconf_dflt)
2579 if (all != &ipv4_devconf)
2585 static __net_exit void devinet_exit_net(struct net *net)
2587 #ifdef CONFIG_SYSCTL
2588 struct ctl_table *tbl;
2590 tbl = net->ipv4.forw_hdr->ctl_table_arg;
2591 unregister_net_sysctl_table(net->ipv4.forw_hdr);
2592 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2593 NETCONFA_IFINDEX_DEFAULT);
2594 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2595 NETCONFA_IFINDEX_ALL);
2598 kfree(net->ipv4.devconf_dflt);
2599 kfree(net->ipv4.devconf_all);
2602 static __net_initdata struct pernet_operations devinet_ops = {
2603 .init = devinet_init_net,
2604 .exit = devinet_exit_net,
2607 static struct rtnl_af_ops inet_af_ops __read_mostly = {
2609 .fill_link_af = inet_fill_link_af,
2610 .get_link_af_size = inet_get_link_af_size,
2611 .validate_link_af = inet_validate_link_af,
2612 .set_link_af = inet_set_link_af,
2615 void __init devinet_init(void)
2619 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2620 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2622 register_pernet_subsys(&devinet_ops);
2624 register_gifconf(PF_INET, inet_gifconf);
2625 register_netdevice_notifier(&ip_netdev_notifier);
2627 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
2629 rtnl_af_register(&inet_af_ops);
2631 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2632 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2633 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
2634 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
2635 inet_netconf_dump_devconf, 0);