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
19 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
21 * Cyrus Durgin: updated for kmod
22 * Matthias Andree: in devinet_ioctl, compare label and
23 * address (4.4BSD alias style support),
24 * fall back to comparing just the label
29 #include <linux/uaccess.h>
30 #include <linux/bitops.h>
31 #include <linux/capability.h>
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/sched/signal.h>
36 #include <linux/string.h>
38 #include <linux/socket.h>
39 #include <linux/sockios.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/if_addr.h>
44 #include <linux/if_ether.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/init.h>
50 #include <linux/notifier.h>
51 #include <linux/inetdevice.h>
52 #include <linux/igmp.h>
53 #include <linux/slab.h>
54 #include <linux/hash.h>
56 #include <linux/sysctl.h>
58 #include <linux/kmod.h>
59 #include <linux/netconf.h>
63 #include <net/route.h>
64 #include <net/ip_fib.h>
65 #include <net/rtnetlink.h>
66 #include <net/net_namespace.h>
67 #include <net/addrconf.h>
69 static struct ipv4_devconf ipv4_devconf = {
71 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
72 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
73 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
74 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
75 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
76 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
80 static struct ipv4_devconf ipv4_devconf_dflt = {
82 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
83 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
84 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
85 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
86 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
87 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
88 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
92 #define IPV4_DEVCONF_DFLT(net, attr) \
93 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
95 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
96 [IFA_LOCAL] = { .type = NLA_U32 },
97 [IFA_ADDRESS] = { .type = NLA_U32 },
98 [IFA_BROADCAST] = { .type = NLA_U32 },
99 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
100 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
101 [IFA_FLAGS] = { .type = NLA_U32 },
102 [IFA_RT_PRIORITY] = { .type = NLA_U32 },
103 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
106 struct inet_fill_args {
115 #define IN4_ADDR_HSIZE_SHIFT 8
116 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
118 static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
120 static u32 inet_addr_hash(const struct net *net, __be32 addr)
122 u32 val = (__force u32) addr ^ net_hash_mix(net);
124 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
127 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
129 u32 hash = inet_addr_hash(net, ifa->ifa_local);
132 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
135 static void inet_hash_remove(struct in_ifaddr *ifa)
138 hlist_del_init_rcu(&ifa->hash);
142 * __ip_dev_find - find the first device with a given source address.
143 * @net: the net namespace
144 * @addr: the source address
145 * @devref: if true, take a reference on the found device
147 * If a caller uses devref=false, it should be protected by RCU, or RTNL
149 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
151 struct net_device *result = NULL;
152 struct in_ifaddr *ifa;
155 ifa = inet_lookup_ifaddr_rcu(net, addr);
157 struct flowi4 fl4 = { .daddr = addr };
158 struct fib_result res = { 0 };
159 struct fib_table *local;
161 /* Fallback to FIB local table so that communication
162 * over loopback subnets work.
164 local = fib_get_table(net, RT_TABLE_LOCAL);
166 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
167 res.type == RTN_LOCAL)
168 result = FIB_RES_DEV(res);
170 result = ifa->ifa_dev->dev;
172 if (result && devref)
177 EXPORT_SYMBOL(__ip_dev_find);
179 /* called under RCU lock */
180 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
182 u32 hash = inet_addr_hash(net, addr);
183 struct in_ifaddr *ifa;
185 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
186 if (ifa->ifa_local == addr &&
187 net_eq(dev_net(ifa->ifa_dev->dev), net))
193 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
195 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
196 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
197 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
200 static int devinet_sysctl_register(struct in_device *idev);
201 static void devinet_sysctl_unregister(struct in_device *idev);
203 static int devinet_sysctl_register(struct in_device *idev)
207 static void devinet_sysctl_unregister(struct in_device *idev)
212 /* Locks all the inet devices. */
214 static struct in_ifaddr *inet_alloc_ifa(void)
216 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
219 static void inet_rcu_free_ifa(struct rcu_head *head)
221 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
223 in_dev_put(ifa->ifa_dev);
227 static void inet_free_ifa(struct in_ifaddr *ifa)
229 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
232 void in_dev_finish_destroy(struct in_device *idev)
234 struct net_device *dev = idev->dev;
236 WARN_ON(idev->ifa_list);
237 WARN_ON(idev->mc_list);
238 kfree(rcu_dereference_protected(idev->mc_hash, 1));
239 #ifdef NET_REFCNT_DEBUG
240 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
244 pr_err("Freeing alive in_device %p\n", idev);
248 EXPORT_SYMBOL(in_dev_finish_destroy);
250 static struct in_device *inetdev_init(struct net_device *dev)
252 struct in_device *in_dev;
257 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
260 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
261 sizeof(in_dev->cnf));
262 in_dev->cnf.sysctl = NULL;
264 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
265 if (!in_dev->arp_parms)
267 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
268 dev_disable_lro(dev);
269 /* Reference in_dev->dev */
271 /* Account for reference dev->ip_ptr (below) */
272 refcount_set(&in_dev->refcnt, 1);
274 err = devinet_sysctl_register(in_dev);
281 ip_mc_init_dev(in_dev);
282 if (dev->flags & IFF_UP)
285 /* we can receive as soon as ip_ptr is set -- do this last */
286 rcu_assign_pointer(dev->ip_ptr, in_dev);
288 return in_dev ?: ERR_PTR(err);
295 static void in_dev_rcu_put(struct rcu_head *head)
297 struct in_device *idev = container_of(head, struct in_device, rcu_head);
301 static void inetdev_destroy(struct in_device *in_dev)
303 struct in_ifaddr *ifa;
304 struct net_device *dev;
312 ip_mc_destroy_dev(in_dev);
314 while ((ifa = in_dev->ifa_list) != NULL) {
315 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
319 RCU_INIT_POINTER(dev->ip_ptr, NULL);
321 devinet_sysctl_unregister(in_dev);
322 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
325 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
328 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
331 for_primary_ifa(in_dev) {
332 if (inet_ifa_match(a, ifa)) {
333 if (!b || inet_ifa_match(b, ifa)) {
338 } endfor_ifa(in_dev);
343 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
344 int destroy, struct nlmsghdr *nlh, u32 portid)
346 struct in_ifaddr *promote = NULL;
347 struct in_ifaddr *ifa, *ifa1 = *ifap;
348 struct in_ifaddr *last_prim = in_dev->ifa_list;
349 struct in_ifaddr *prev_prom = NULL;
350 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
357 /* 1. Deleting primary ifaddr forces deletion all secondaries
358 * unless alias promotion is set
361 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
362 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
364 while ((ifa = *ifap1) != NULL) {
365 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
366 ifa1->ifa_scope <= ifa->ifa_scope)
369 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
370 ifa1->ifa_mask != ifa->ifa_mask ||
371 !inet_ifa_match(ifa1->ifa_address, ifa)) {
372 ifap1 = &ifa->ifa_next;
378 inet_hash_remove(ifa);
379 *ifap1 = ifa->ifa_next;
381 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
382 blocking_notifier_call_chain(&inetaddr_chain,
392 /* On promotion all secondaries from subnet are changing
393 * the primary IP, we must remove all their routes silently
394 * and later to add them back with new prefsrc. Do this
395 * while all addresses are on the device list.
397 for (ifa = promote; ifa; ifa = ifa->ifa_next) {
398 if (ifa1->ifa_mask == ifa->ifa_mask &&
399 inet_ifa_match(ifa1->ifa_address, ifa))
400 fib_del_ifaddr(ifa, ifa1);
406 *ifap = ifa1->ifa_next;
407 inet_hash_remove(ifa1);
409 /* 3. Announce address deletion */
411 /* Send message first, then call notifier.
412 At first sight, FIB update triggered by notifier
413 will refer to already deleted ifaddr, that could confuse
414 netlink listeners. It is not true: look, gated sees
415 that route deleted and if it still thinks that ifaddr
416 is valid, it will try to restore deleted routes... Grr.
417 So that, this order is correct.
419 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
420 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
423 struct in_ifaddr *next_sec = promote->ifa_next;
426 prev_prom->ifa_next = promote->ifa_next;
427 promote->ifa_next = last_prim->ifa_next;
428 last_prim->ifa_next = promote;
431 promote->ifa_flags &= ~IFA_F_SECONDARY;
432 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
433 blocking_notifier_call_chain(&inetaddr_chain,
435 for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
436 if (ifa1->ifa_mask != ifa->ifa_mask ||
437 !inet_ifa_match(ifa1->ifa_address, ifa))
447 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
450 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
453 static void check_lifetime(struct work_struct *work);
455 static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
457 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
458 u32 portid, struct netlink_ext_ack *extack)
460 struct in_device *in_dev = ifa->ifa_dev;
461 struct in_ifaddr *ifa1, **ifap, **last_primary;
462 struct in_validator_info ivi;
467 if (!ifa->ifa_local) {
472 ifa->ifa_flags &= ~IFA_F_SECONDARY;
473 last_primary = &in_dev->ifa_list;
475 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
476 ifap = &ifa1->ifa_next) {
477 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
478 ifa->ifa_scope <= ifa1->ifa_scope)
479 last_primary = &ifa1->ifa_next;
480 if (ifa1->ifa_mask == ifa->ifa_mask &&
481 inet_ifa_match(ifa1->ifa_address, ifa)) {
482 if (ifa1->ifa_local == ifa->ifa_local) {
486 if (ifa1->ifa_scope != ifa->ifa_scope) {
490 ifa->ifa_flags |= IFA_F_SECONDARY;
494 /* Allow any devices that wish to register ifaddr validtors to weigh
495 * in now, before changes are committed. The rntl lock is serializing
496 * access here, so the state should not change between a validator call
497 * and a final notify on commit. This isn't invoked on promotion under
498 * the assumption that validators are checking the address itself, and
501 ivi.ivi_addr = ifa->ifa_address;
502 ivi.ivi_dev = ifa->ifa_dev;
504 ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
506 ret = notifier_to_errno(ret);
512 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
513 prandom_seed((__force u32) ifa->ifa_local);
517 ifa->ifa_next = *ifap;
520 inet_hash_insert(dev_net(in_dev->dev), ifa);
522 cancel_delayed_work(&check_lifetime_work);
523 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
525 /* Send message first, then call notifier.
526 Notifier will trigger FIB update, so that
527 listeners of netlink will know about new ifaddr */
528 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
529 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
534 static int inet_insert_ifa(struct in_ifaddr *ifa)
536 return __inet_insert_ifa(ifa, NULL, 0, NULL);
539 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
541 struct in_device *in_dev = __in_dev_get_rtnl(dev);
549 ipv4_devconf_setall(in_dev);
550 neigh_parms_data_state_setall(in_dev->arp_parms);
551 if (ifa->ifa_dev != in_dev) {
552 WARN_ON(ifa->ifa_dev);
554 ifa->ifa_dev = in_dev;
556 if (ipv4_is_loopback(ifa->ifa_local))
557 ifa->ifa_scope = RT_SCOPE_HOST;
558 return inet_insert_ifa(ifa);
561 /* Caller must hold RCU or RTNL :
562 * We dont take a reference on found in_device
564 struct in_device *inetdev_by_index(struct net *net, int ifindex)
566 struct net_device *dev;
567 struct in_device *in_dev = NULL;
570 dev = dev_get_by_index_rcu(net, ifindex);
572 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
576 EXPORT_SYMBOL(inetdev_by_index);
578 /* Called only from RTNL semaphored context. No locks. */
580 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
585 for_primary_ifa(in_dev) {
586 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
588 } endfor_ifa(in_dev);
592 static int ip_mc_config(struct sock *sk, bool join, const struct in_ifaddr *ifa)
594 struct ip_mreqn mreq = {
595 .imr_multiaddr.s_addr = ifa->ifa_address,
596 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
604 ret = ip_mc_join_group(sk, &mreq);
606 ret = ip_mc_leave_group(sk, &mreq);
612 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
613 struct netlink_ext_ack *extack)
615 struct net *net = sock_net(skb->sk);
616 struct nlattr *tb[IFA_MAX+1];
617 struct in_device *in_dev;
618 struct ifaddrmsg *ifm;
619 struct in_ifaddr *ifa, **ifap;
624 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
625 ifa_ipv4_policy, extack);
629 ifm = nlmsg_data(nlh);
630 in_dev = inetdev_by_index(net, ifm->ifa_index);
636 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
637 ifap = &ifa->ifa_next) {
639 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
642 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
645 if (tb[IFA_ADDRESS] &&
646 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
647 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
650 if (ipv4_is_multicast(ifa->ifa_address))
651 ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa);
652 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
656 err = -EADDRNOTAVAIL;
661 #define INFINITY_LIFE_TIME 0xFFFFFFFF
663 static void check_lifetime(struct work_struct *work)
665 unsigned long now, next, next_sec, next_sched;
666 struct in_ifaddr *ifa;
667 struct hlist_node *n;
671 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
673 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
674 bool change_needed = false;
677 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
680 if (ifa->ifa_flags & IFA_F_PERMANENT)
683 /* We try to batch several events at once. */
684 age = (now - ifa->ifa_tstamp +
685 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
687 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
688 age >= ifa->ifa_valid_lft) {
689 change_needed = true;
690 } else if (ifa->ifa_preferred_lft ==
691 INFINITY_LIFE_TIME) {
693 } else if (age >= ifa->ifa_preferred_lft) {
694 if (time_before(ifa->ifa_tstamp +
695 ifa->ifa_valid_lft * HZ, next))
696 next = ifa->ifa_tstamp +
697 ifa->ifa_valid_lft * HZ;
699 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
700 change_needed = true;
701 } else if (time_before(ifa->ifa_tstamp +
702 ifa->ifa_preferred_lft * HZ,
704 next = ifa->ifa_tstamp +
705 ifa->ifa_preferred_lft * HZ;
712 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
715 if (ifa->ifa_flags & IFA_F_PERMANENT)
718 /* We try to batch several events at once. */
719 age = (now - ifa->ifa_tstamp +
720 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
722 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
723 age >= ifa->ifa_valid_lft) {
724 struct in_ifaddr **ifap;
726 for (ifap = &ifa->ifa_dev->ifa_list;
727 *ifap != NULL; ifap = &(*ifap)->ifa_next) {
729 inet_del_ifa(ifa->ifa_dev,
734 } else if (ifa->ifa_preferred_lft !=
735 INFINITY_LIFE_TIME &&
736 age >= ifa->ifa_preferred_lft &&
737 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
738 ifa->ifa_flags |= IFA_F_DEPRECATED;
739 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
745 next_sec = round_jiffies_up(next);
748 /* If rounded timeout is accurate enough, accept it. */
749 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
750 next_sched = next_sec;
753 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
754 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
755 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
757 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
761 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
764 unsigned long timeout;
766 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
768 timeout = addrconf_timeout_fixup(valid_lft, HZ);
769 if (addrconf_finite_timeout(timeout))
770 ifa->ifa_valid_lft = timeout;
772 ifa->ifa_flags |= IFA_F_PERMANENT;
774 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
775 if (addrconf_finite_timeout(timeout)) {
777 ifa->ifa_flags |= IFA_F_DEPRECATED;
778 ifa->ifa_preferred_lft = timeout;
780 ifa->ifa_tstamp = jiffies;
781 if (!ifa->ifa_cstamp)
782 ifa->ifa_cstamp = ifa->ifa_tstamp;
785 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
786 __u32 *pvalid_lft, __u32 *pprefered_lft,
787 struct netlink_ext_ack *extack)
789 struct nlattr *tb[IFA_MAX+1];
790 struct in_ifaddr *ifa;
791 struct ifaddrmsg *ifm;
792 struct net_device *dev;
793 struct in_device *in_dev;
796 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
797 ifa_ipv4_policy, extack);
801 ifm = nlmsg_data(nlh);
803 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
806 dev = __dev_get_by_index(net, ifm->ifa_index);
811 in_dev = __in_dev_get_rtnl(dev);
816 ifa = inet_alloc_ifa();
819 * A potential indev allocation can be left alive, it stays
820 * assigned to its device and is destroy with it.
824 ipv4_devconf_setall(in_dev);
825 neigh_parms_data_state_setall(in_dev->arp_parms);
828 if (!tb[IFA_ADDRESS])
829 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
831 INIT_HLIST_NODE(&ifa->hash);
832 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
833 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
834 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
836 ifa->ifa_scope = ifm->ifa_scope;
837 ifa->ifa_dev = in_dev;
839 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
840 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
842 if (tb[IFA_BROADCAST])
843 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
846 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
848 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
850 if (tb[IFA_RT_PRIORITY])
851 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
853 if (tb[IFA_CACHEINFO]) {
854 struct ifa_cacheinfo *ci;
856 ci = nla_data(tb[IFA_CACHEINFO]);
857 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
861 *pvalid_lft = ci->ifa_valid;
862 *pprefered_lft = ci->ifa_prefered;
873 static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
875 struct in_device *in_dev = ifa->ifa_dev;
876 struct in_ifaddr *ifa1, **ifap;
881 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
882 ifap = &ifa1->ifa_next) {
883 if (ifa1->ifa_mask == ifa->ifa_mask &&
884 inet_ifa_match(ifa1->ifa_address, ifa) &&
885 ifa1->ifa_local == ifa->ifa_local)
891 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
892 struct netlink_ext_ack *extack)
894 struct net *net = sock_net(skb->sk);
895 struct in_ifaddr *ifa;
896 struct in_ifaddr *ifa_existing;
897 __u32 valid_lft = INFINITY_LIFE_TIME;
898 __u32 prefered_lft = INFINITY_LIFE_TIME;
902 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
906 ifa_existing = find_matching_ifa(ifa);
908 /* It would be best to check for !NLM_F_CREATE here but
909 * userspace already relies on not having to provide this.
911 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
912 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
913 int ret = ip_mc_config(net->ipv4.mc_autojoin_sk,
921 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
924 u32 new_metric = ifa->ifa_rt_priority;
928 if (nlh->nlmsg_flags & NLM_F_EXCL ||
929 !(nlh->nlmsg_flags & NLM_F_REPLACE))
933 if (ifa->ifa_rt_priority != new_metric) {
934 fib_modify_prefix_metric(ifa, new_metric);
935 ifa->ifa_rt_priority = new_metric;
938 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
939 cancel_delayed_work(&check_lifetime_work);
940 queue_delayed_work(system_power_efficient_wq,
941 &check_lifetime_work, 0);
942 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
948 * Determine a default network mask, based on the IP address.
951 static int inet_abc_len(__be32 addr)
953 int rc = -1; /* Something else, probably a multicast. */
955 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
958 __u32 haddr = ntohl(addr);
959 if (IN_CLASSA(haddr))
961 else if (IN_CLASSB(haddr))
963 else if (IN_CLASSC(haddr))
965 else if (IN_CLASSE(haddr))
973 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
975 struct sockaddr_in sin_orig;
976 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
977 struct in_device *in_dev;
978 struct in_ifaddr **ifap = NULL;
979 struct in_ifaddr *ifa = NULL;
980 struct net_device *dev;
983 int tryaddrmatch = 0;
985 ifr->ifr_name[IFNAMSIZ - 1] = 0;
987 /* save original address for comparison */
988 memcpy(&sin_orig, sin, sizeof(*sin));
990 colon = strchr(ifr->ifr_name, ':');
994 dev_load(net, ifr->ifr_name);
997 case SIOCGIFADDR: /* Get interface address */
998 case SIOCGIFBRDADDR: /* Get the broadcast address */
999 case SIOCGIFDSTADDR: /* Get the destination address */
1000 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1001 /* Note that these ioctls will not sleep,
1002 so that we do not impose a lock.
1003 One day we will be forced to put shlock here (I mean SMP)
1005 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1006 memset(sin, 0, sizeof(*sin));
1007 sin->sin_family = AF_INET;
1012 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1015 case SIOCSIFADDR: /* Set interface address (and family) */
1016 case SIOCSIFBRDADDR: /* Set the broadcast address */
1017 case SIOCSIFDSTADDR: /* Set the destination address */
1018 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1020 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1023 if (sin->sin_family != AF_INET)
1034 dev = __dev_get_by_name(net, ifr->ifr_name);
1041 in_dev = __in_dev_get_rtnl(dev);
1044 /* Matthias Andree */
1045 /* compare label and address (4.4BSD style) */
1046 /* note: we only do this for a limited set of ioctls
1047 and only if the original address family was AF_INET.
1048 This is checked above. */
1049 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1050 ifap = &ifa->ifa_next) {
1051 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
1052 sin_orig.sin_addr.s_addr ==
1058 /* we didn't get a match, maybe the application is
1059 4.3BSD-style and passed in junk so we fall back to
1060 comparing just the label */
1062 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
1063 ifap = &ifa->ifa_next)
1064 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
1069 ret = -EADDRNOTAVAIL;
1070 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1074 case SIOCGIFADDR: /* Get interface address */
1076 sin->sin_addr.s_addr = ifa->ifa_local;
1079 case SIOCGIFBRDADDR: /* Get the broadcast address */
1081 sin->sin_addr.s_addr = ifa->ifa_broadcast;
1084 case SIOCGIFDSTADDR: /* Get the destination address */
1086 sin->sin_addr.s_addr = ifa->ifa_address;
1089 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1091 sin->sin_addr.s_addr = ifa->ifa_mask;
1096 ret = -EADDRNOTAVAIL;
1100 if (!(ifr->ifr_flags & IFF_UP))
1101 inet_del_ifa(in_dev, ifap, 1);
1104 ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
1107 case SIOCSIFADDR: /* Set interface address (and family) */
1109 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1114 ifa = inet_alloc_ifa();
1117 INIT_HLIST_NODE(&ifa->hash);
1119 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
1121 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1124 if (ifa->ifa_local == sin->sin_addr.s_addr)
1126 inet_del_ifa(in_dev, ifap, 0);
1127 ifa->ifa_broadcast = 0;
1131 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1133 if (!(dev->flags & IFF_POINTOPOINT)) {
1134 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1135 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1136 if ((dev->flags & IFF_BROADCAST) &&
1137 ifa->ifa_prefixlen < 31)
1138 ifa->ifa_broadcast = ifa->ifa_address |
1141 ifa->ifa_prefixlen = 32;
1142 ifa->ifa_mask = inet_make_mask(32);
1144 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
1145 ret = inet_set_ifa(dev, ifa);
1148 case SIOCSIFBRDADDR: /* Set the broadcast address */
1150 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1151 inet_del_ifa(in_dev, ifap, 0);
1152 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1153 inet_insert_ifa(ifa);
1157 case SIOCSIFDSTADDR: /* Set the destination address */
1159 if (ifa->ifa_address == sin->sin_addr.s_addr)
1162 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1165 inet_del_ifa(in_dev, ifap, 0);
1166 ifa->ifa_address = sin->sin_addr.s_addr;
1167 inet_insert_ifa(ifa);
1170 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1173 * The mask we set must be legal.
1176 if (bad_mask(sin->sin_addr.s_addr, 0))
1179 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1180 __be32 old_mask = ifa->ifa_mask;
1181 inet_del_ifa(in_dev, ifap, 0);
1182 ifa->ifa_mask = sin->sin_addr.s_addr;
1183 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1185 /* See if current broadcast address matches
1186 * with current netmask, then recalculate
1187 * the broadcast address. Otherwise it's a
1188 * funny address, so don't touch it since
1189 * the user seems to know what (s)he's doing...
1191 if ((dev->flags & IFF_BROADCAST) &&
1192 (ifa->ifa_prefixlen < 31) &&
1193 (ifa->ifa_broadcast ==
1194 (ifa->ifa_local|~old_mask))) {
1195 ifa->ifa_broadcast = (ifa->ifa_local |
1196 ~sin->sin_addr.s_addr);
1198 inet_insert_ifa(ifa);
1208 static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
1210 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1211 struct in_ifaddr *ifa;
1215 if (WARN_ON(size > sizeof(struct ifreq)))
1221 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1228 memset(&ifr, 0, sizeof(struct ifreq));
1229 strcpy(ifr.ifr_name, ifa->ifa_label);
1231 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1232 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1235 if (copy_to_user(buf + done, &ifr, size)) {
1246 static __be32 in_dev_select_addr(const struct in_device *in_dev,
1249 for_primary_ifa(in_dev) {
1250 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1251 ifa->ifa_scope <= scope)
1252 return ifa->ifa_local;
1253 } endfor_ifa(in_dev);
1258 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1261 struct in_device *in_dev;
1262 struct net *net = dev_net(dev);
1266 in_dev = __in_dev_get_rcu(dev);
1270 for_primary_ifa(in_dev) {
1271 if (ifa->ifa_scope > scope)
1273 if (!dst || inet_ifa_match(dst, ifa)) {
1274 addr = ifa->ifa_local;
1278 addr = ifa->ifa_local;
1279 } endfor_ifa(in_dev);
1284 master_idx = l3mdev_master_ifindex_rcu(dev);
1286 /* For VRFs, the VRF device takes the place of the loopback device,
1287 * with addresses on it being preferred. Note in such cases the
1288 * loopback device will be among the devices that fail the master_idx
1289 * equality check in the loop below.
1292 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1293 (in_dev = __in_dev_get_rcu(dev))) {
1294 addr = in_dev_select_addr(in_dev, scope);
1299 /* Not loopback addresses on loopback should be preferred
1300 in this case. It is important that lo is the first interface
1303 for_each_netdev_rcu(net, dev) {
1304 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1307 in_dev = __in_dev_get_rcu(dev);
1311 addr = in_dev_select_addr(in_dev, scope);
1319 EXPORT_SYMBOL(inet_select_addr);
1321 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1322 __be32 local, int scope)
1329 (local == ifa->ifa_local || !local) &&
1330 ifa->ifa_scope <= scope) {
1331 addr = ifa->ifa_local;
1336 same = (!local || inet_ifa_match(local, ifa)) &&
1337 (!dst || inet_ifa_match(dst, ifa));
1341 /* Is the selected addr into dst subnet? */
1342 if (inet_ifa_match(addr, ifa))
1344 /* No, then can we use new local src? */
1345 if (ifa->ifa_scope <= scope) {
1346 addr = ifa->ifa_local;
1349 /* search for large dst subnet for addr */
1353 } endfor_ifa(in_dev);
1355 return same ? addr : 0;
1359 * Confirm that local IP address exists using wildcards:
1360 * - net: netns to check, cannot be NULL
1361 * - in_dev: only on this interface, NULL=any interface
1362 * - dst: only in the same subnet as dst, 0=any dst
1363 * - local: address, 0=autoselect the local address
1364 * - scope: maximum allowed scope value for the local address
1366 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1367 __be32 dst, __be32 local, int scope)
1370 struct net_device *dev;
1373 return confirm_addr_indev(in_dev, dst, local, scope);
1376 for_each_netdev_rcu(net, dev) {
1377 in_dev = __in_dev_get_rcu(dev);
1379 addr = confirm_addr_indev(in_dev, dst, local, scope);
1388 EXPORT_SYMBOL(inet_confirm_addr);
1394 int register_inetaddr_notifier(struct notifier_block *nb)
1396 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1398 EXPORT_SYMBOL(register_inetaddr_notifier);
1400 int unregister_inetaddr_notifier(struct notifier_block *nb)
1402 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1404 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1406 int register_inetaddr_validator_notifier(struct notifier_block *nb)
1408 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1410 EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1412 int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1414 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1417 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1419 /* Rename ifa_labels for a device name change. Make some effort to preserve
1420 * existing alias numbering and to create unique labels if possible.
1422 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1424 struct in_ifaddr *ifa;
1427 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1428 char old[IFNAMSIZ], *dot;
1430 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1431 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1434 dot = strchr(old, ':');
1436 sprintf(old, ":%d", named);
1439 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1440 strcat(ifa->ifa_label, dot);
1442 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1444 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1448 static bool inetdev_valid_mtu(unsigned int mtu)
1450 return mtu >= IPV4_MIN_MTU;
1453 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1454 struct in_device *in_dev)
1457 struct in_ifaddr *ifa;
1459 for (ifa = in_dev->ifa_list; ifa;
1460 ifa = ifa->ifa_next) {
1461 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1462 ifa->ifa_local, dev,
1463 ifa->ifa_local, NULL,
1464 dev->dev_addr, NULL);
1468 /* Called only under RTNL semaphore */
1470 static int inetdev_event(struct notifier_block *this, unsigned long event,
1473 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1474 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1479 if (event == NETDEV_REGISTER) {
1480 in_dev = inetdev_init(dev);
1482 return notifier_from_errno(PTR_ERR(in_dev));
1483 if (dev->flags & IFF_LOOPBACK) {
1484 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1485 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1487 } else if (event == NETDEV_CHANGEMTU) {
1488 /* Re-enabling IP */
1489 if (inetdev_valid_mtu(dev->mtu))
1490 in_dev = inetdev_init(dev);
1496 case NETDEV_REGISTER:
1497 pr_debug("%s: bug\n", __func__);
1498 RCU_INIT_POINTER(dev->ip_ptr, NULL);
1501 if (!inetdev_valid_mtu(dev->mtu))
1503 if (dev->flags & IFF_LOOPBACK) {
1504 struct in_ifaddr *ifa = inet_alloc_ifa();
1507 INIT_HLIST_NODE(&ifa->hash);
1509 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1510 ifa->ifa_prefixlen = 8;
1511 ifa->ifa_mask = inet_make_mask(8);
1512 in_dev_hold(in_dev);
1513 ifa->ifa_dev = in_dev;
1514 ifa->ifa_scope = RT_SCOPE_HOST;
1515 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1516 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1517 INFINITY_LIFE_TIME);
1518 ipv4_devconf_setall(in_dev);
1519 neigh_parms_data_state_setall(in_dev->arp_parms);
1520 inet_insert_ifa(ifa);
1525 case NETDEV_CHANGEADDR:
1526 if (!IN_DEV_ARP_NOTIFY(in_dev))
1529 case NETDEV_NOTIFY_PEERS:
1530 /* Send gratuitous ARP to notify of link change */
1531 inetdev_send_gratuitous_arp(dev, in_dev);
1536 case NETDEV_PRE_TYPE_CHANGE:
1537 ip_mc_unmap(in_dev);
1539 case NETDEV_POST_TYPE_CHANGE:
1540 ip_mc_remap(in_dev);
1542 case NETDEV_CHANGEMTU:
1543 if (inetdev_valid_mtu(dev->mtu))
1545 /* disable IP when MTU is not enough */
1547 case NETDEV_UNREGISTER:
1548 inetdev_destroy(in_dev);
1550 case NETDEV_CHANGENAME:
1551 /* Do not notify about label change, this event is
1552 * not interesting to applications using netlink.
1554 inetdev_changename(dev, in_dev);
1556 devinet_sysctl_unregister(in_dev);
1557 devinet_sysctl_register(in_dev);
1564 static struct notifier_block ip_netdev_notifier = {
1565 .notifier_call = inetdev_event,
1568 static size_t inet_nlmsg_size(void)
1570 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1571 + nla_total_size(4) /* IFA_ADDRESS */
1572 + nla_total_size(4) /* IFA_LOCAL */
1573 + nla_total_size(4) /* IFA_BROADCAST */
1574 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
1575 + nla_total_size(4) /* IFA_FLAGS */
1576 + nla_total_size(4) /* IFA_RT_PRIORITY */
1577 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
1580 static inline u32 cstamp_delta(unsigned long cstamp)
1582 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1585 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1586 unsigned long tstamp, u32 preferred, u32 valid)
1588 struct ifa_cacheinfo ci;
1590 ci.cstamp = cstamp_delta(cstamp);
1591 ci.tstamp = cstamp_delta(tstamp);
1592 ci.ifa_prefered = preferred;
1593 ci.ifa_valid = valid;
1595 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1598 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1599 struct inet_fill_args *args)
1601 struct ifaddrmsg *ifm;
1602 struct nlmsghdr *nlh;
1603 u32 preferred, valid;
1605 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1610 ifm = nlmsg_data(nlh);
1611 ifm->ifa_family = AF_INET;
1612 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1613 ifm->ifa_flags = ifa->ifa_flags;
1614 ifm->ifa_scope = ifa->ifa_scope;
1615 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1617 if (args->netnsid >= 0 &&
1618 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
1619 goto nla_put_failure;
1621 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1622 preferred = ifa->ifa_preferred_lft;
1623 valid = ifa->ifa_valid_lft;
1624 if (preferred != INFINITY_LIFE_TIME) {
1625 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1627 if (preferred > tval)
1631 if (valid != INFINITY_LIFE_TIME) {
1639 preferred = INFINITY_LIFE_TIME;
1640 valid = INFINITY_LIFE_TIME;
1642 if ((ifa->ifa_address &&
1643 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1645 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
1646 (ifa->ifa_broadcast &&
1647 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1648 (ifa->ifa_label[0] &&
1649 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1650 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
1651 (ifa->ifa_rt_priority &&
1652 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
1653 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1655 goto nla_put_failure;
1657 nlmsg_end(skb, nlh);
1661 nlmsg_cancel(skb, nlh);
1665 static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1666 struct inet_fill_args *fillargs,
1667 struct net **tgt_net, struct sock *sk,
1668 struct netlink_callback *cb)
1670 struct netlink_ext_ack *extack = cb->extack;
1671 struct nlattr *tb[IFA_MAX+1];
1672 struct ifaddrmsg *ifm;
1675 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1676 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1680 ifm = nlmsg_data(nlh);
1681 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1682 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1686 fillargs->ifindex = ifm->ifa_index;
1687 if (fillargs->ifindex) {
1688 cb->answer_flags |= NLM_F_DUMP_FILTERED;
1689 fillargs->flags |= NLM_F_DUMP_FILTERED;
1692 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1693 ifa_ipv4_policy, extack);
1697 for (i = 0; i <= IFA_MAX; ++i) {
1701 if (i == IFA_TARGET_NETNSID) {
1704 fillargs->netnsid = nla_get_s32(tb[i]);
1706 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1708 fillargs->netnsid = -1;
1709 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1710 return PTR_ERR(net);
1714 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1722 static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1723 struct netlink_callback *cb, int s_ip_idx,
1724 struct inet_fill_args *fillargs)
1726 struct in_ifaddr *ifa;
1730 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next, ip_idx++) {
1731 if (ip_idx < s_ip_idx)
1734 err = inet_fill_ifaddr(skb, ifa, fillargs);
1738 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1743 cb->args[2] = ip_idx;
1748 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1750 const struct nlmsghdr *nlh = cb->nlh;
1751 struct inet_fill_args fillargs = {
1752 .portid = NETLINK_CB(cb->skb).portid,
1753 .seq = nlh->nlmsg_seq,
1754 .event = RTM_NEWADDR,
1755 .flags = NLM_F_MULTI,
1758 struct net *net = sock_net(skb->sk);
1759 struct net *tgt_net = net;
1763 struct net_device *dev;
1764 struct in_device *in_dev;
1765 struct hlist_head *head;
1769 s_idx = idx = cb->args[1];
1770 s_ip_idx = cb->args[2];
1772 if (cb->strict_check) {
1773 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
1779 if (fillargs.ifindex) {
1780 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
1786 in_dev = __in_dev_get_rtnl(dev);
1788 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1795 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1797 head = &tgt_net->dev_index_head[h];
1799 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1800 tgt_net->dev_base_seq;
1801 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1804 if (h > s_h || idx > s_idx)
1806 in_dev = __in_dev_get_rcu(dev);
1810 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1826 if (fillargs.netnsid >= 0)
1829 return skb->len ? : err;
1832 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1835 struct inet_fill_args fillargs = {
1837 .seq = nlh ? nlh->nlmsg_seq : 0,
1842 struct sk_buff *skb;
1846 net = dev_net(ifa->ifa_dev->dev);
1847 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1851 err = inet_fill_ifaddr(skb, ifa, &fillargs);
1853 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1854 WARN_ON(err == -EMSGSIZE);
1858 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1862 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1865 static size_t inet_get_link_af_size(const struct net_device *dev,
1866 u32 ext_filter_mask)
1868 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1873 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1876 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1877 u32 ext_filter_mask)
1879 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1886 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1890 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1891 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1896 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1897 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1900 static int inet_validate_link_af(const struct net_device *dev,
1901 const struct nlattr *nla)
1903 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1906 if (dev && !__in_dev_get_rcu(dev))
1907 return -EAFNOSUPPORT;
1909 err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
1910 inet_af_policy, NULL);
1914 if (tb[IFLA_INET_CONF]) {
1915 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1916 int cfgid = nla_type(a);
1921 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1929 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1931 struct in_device *in_dev = __in_dev_get_rcu(dev);
1932 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1936 return -EAFNOSUPPORT;
1938 if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
1941 if (tb[IFLA_INET_CONF]) {
1942 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1943 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1949 static int inet_netconf_msgsize_devconf(int type)
1951 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1952 + nla_total_size(4); /* NETCONFA_IFINDEX */
1955 if (type == NETCONFA_ALL)
1958 if (all || type == NETCONFA_FORWARDING)
1959 size += nla_total_size(4);
1960 if (all || type == NETCONFA_RP_FILTER)
1961 size += nla_total_size(4);
1962 if (all || type == NETCONFA_MC_FORWARDING)
1963 size += nla_total_size(4);
1964 if (all || type == NETCONFA_BC_FORWARDING)
1965 size += nla_total_size(4);
1966 if (all || type == NETCONFA_PROXY_NEIGH)
1967 size += nla_total_size(4);
1968 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
1969 size += nla_total_size(4);
1974 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1975 struct ipv4_devconf *devconf, u32 portid,
1976 u32 seq, int event, unsigned int flags,
1979 struct nlmsghdr *nlh;
1980 struct netconfmsg *ncm;
1983 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1988 if (type == NETCONFA_ALL)
1991 ncm = nlmsg_data(nlh);
1992 ncm->ncm_family = AF_INET;
1994 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1995 goto nla_put_failure;
2000 if ((all || type == NETCONFA_FORWARDING) &&
2001 nla_put_s32(skb, NETCONFA_FORWARDING,
2002 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
2003 goto nla_put_failure;
2004 if ((all || type == NETCONFA_RP_FILTER) &&
2005 nla_put_s32(skb, NETCONFA_RP_FILTER,
2006 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
2007 goto nla_put_failure;
2008 if ((all || type == NETCONFA_MC_FORWARDING) &&
2009 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
2010 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
2011 goto nla_put_failure;
2012 if ((all || type == NETCONFA_BC_FORWARDING) &&
2013 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
2014 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
2015 goto nla_put_failure;
2016 if ((all || type == NETCONFA_PROXY_NEIGH) &&
2017 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
2018 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
2019 goto nla_put_failure;
2020 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
2021 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2022 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
2023 goto nla_put_failure;
2026 nlmsg_end(skb, nlh);
2030 nlmsg_cancel(skb, nlh);
2034 void inet_netconf_notify_devconf(struct net *net, int event, int type,
2035 int ifindex, struct ipv4_devconf *devconf)
2037 struct sk_buff *skb;
2040 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
2044 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
2047 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2048 WARN_ON(err == -EMSGSIZE);
2052 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
2056 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2059 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2060 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
2061 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
2062 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
2063 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
2064 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
2067 static int inet_netconf_valid_get_req(struct sk_buff *skb,
2068 const struct nlmsghdr *nlh,
2070 struct netlink_ext_ack *extack)
2074 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
2075 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request");
2079 if (!netlink_strict_get_check(skb))
2080 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
2082 devconf_ipv4_policy, extack);
2084 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
2086 devconf_ipv4_policy, extack);
2090 for (i = 0; i <= NETCONFA_MAX; i++) {
2095 case NETCONFA_IFINDEX:
2098 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request");
2106 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
2107 struct nlmsghdr *nlh,
2108 struct netlink_ext_ack *extack)
2110 struct net *net = sock_net(in_skb->sk);
2111 struct nlattr *tb[NETCONFA_MAX+1];
2112 struct sk_buff *skb;
2113 struct ipv4_devconf *devconf;
2114 struct in_device *in_dev;
2115 struct net_device *dev;
2119 err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack);
2124 if (!tb[NETCONFA_IFINDEX])
2127 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2129 case NETCONFA_IFINDEX_ALL:
2130 devconf = net->ipv4.devconf_all;
2132 case NETCONFA_IFINDEX_DEFAULT:
2133 devconf = net->ipv4.devconf_dflt;
2136 dev = __dev_get_by_index(net, ifindex);
2139 in_dev = __in_dev_get_rtnl(dev);
2142 devconf = &in_dev->cnf;
2147 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
2151 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2152 NETLINK_CB(in_skb).portid,
2153 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
2156 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2157 WARN_ON(err == -EMSGSIZE);
2161 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2166 static int inet_netconf_dump_devconf(struct sk_buff *skb,
2167 struct netlink_callback *cb)
2169 const struct nlmsghdr *nlh = cb->nlh;
2170 struct net *net = sock_net(skb->sk);
2173 struct net_device *dev;
2174 struct in_device *in_dev;
2175 struct hlist_head *head;
2177 if (cb->strict_check) {
2178 struct netlink_ext_ack *extack = cb->extack;
2179 struct netconfmsg *ncm;
2181 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2182 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2186 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2187 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2193 s_idx = idx = cb->args[1];
2195 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2197 head = &net->dev_index_head[h];
2199 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2201 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2204 in_dev = __in_dev_get_rcu(dev);
2208 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2210 NETLINK_CB(cb->skb).portid,
2214 NETCONFA_ALL) < 0) {
2218 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2224 if (h == NETDEV_HASHENTRIES) {
2225 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2226 net->ipv4.devconf_all,
2227 NETLINK_CB(cb->skb).portid,
2229 RTM_NEWNETCONF, NLM_F_MULTI,
2235 if (h == NETDEV_HASHENTRIES + 1) {
2236 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2237 net->ipv4.devconf_dflt,
2238 NETLINK_CB(cb->skb).portid,
2240 RTM_NEWNETCONF, NLM_F_MULTI,
2253 #ifdef CONFIG_SYSCTL
2255 static void devinet_copy_dflt_conf(struct net *net, int i)
2257 struct net_device *dev;
2260 for_each_netdev_rcu(net, dev) {
2261 struct in_device *in_dev;
2263 in_dev = __in_dev_get_rcu(dev);
2264 if (in_dev && !test_bit(i, in_dev->cnf.state))
2265 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
2270 /* called with RTNL locked */
2271 static void inet_forward_change(struct net *net)
2273 struct net_device *dev;
2274 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
2276 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
2277 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
2278 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2279 NETCONFA_FORWARDING,
2280 NETCONFA_IFINDEX_ALL,
2281 net->ipv4.devconf_all);
2282 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2283 NETCONFA_FORWARDING,
2284 NETCONFA_IFINDEX_DEFAULT,
2285 net->ipv4.devconf_dflt);
2287 for_each_netdev(net, dev) {
2288 struct in_device *in_dev;
2291 dev_disable_lro(dev);
2293 in_dev = __in_dev_get_rtnl(dev);
2295 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
2296 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2297 NETCONFA_FORWARDING,
2298 dev->ifindex, &in_dev->cnf);
2303 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2305 if (cnf == net->ipv4.devconf_dflt)
2306 return NETCONFA_IFINDEX_DEFAULT;
2307 else if (cnf == net->ipv4.devconf_all)
2308 return NETCONFA_IFINDEX_ALL;
2310 struct in_device *idev
2311 = container_of(cnf, struct in_device, cnf);
2312 return idev->dev->ifindex;
2316 static int devinet_conf_proc(struct ctl_table *ctl, int write,
2317 void __user *buffer,
2318 size_t *lenp, loff_t *ppos)
2320 int old_value = *(int *)ctl->data;
2321 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2322 int new_value = *(int *)ctl->data;
2325 struct ipv4_devconf *cnf = ctl->extra1;
2326 struct net *net = ctl->extra2;
2327 int i = (int *)ctl->data - cnf->data;
2330 set_bit(i, cnf->state);
2332 if (cnf == net->ipv4.devconf_dflt)
2333 devinet_copy_dflt_conf(net, i);
2334 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2335 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
2336 if ((new_value == 0) && (old_value != 0))
2337 rt_cache_flush(net);
2339 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2340 new_value != old_value)
2341 rt_cache_flush(net);
2343 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2344 new_value != old_value) {
2345 ifindex = devinet_conf_ifindex(net, cnf);
2346 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2350 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2351 new_value != old_value) {
2352 ifindex = devinet_conf_ifindex(net, cnf);
2353 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2354 NETCONFA_PROXY_NEIGH,
2357 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2358 new_value != old_value) {
2359 ifindex = devinet_conf_ifindex(net, cnf);
2360 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2361 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2369 static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
2370 void __user *buffer,
2371 size_t *lenp, loff_t *ppos)
2373 int *valp = ctl->data;
2376 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2378 if (write && *valp != val) {
2379 struct net *net = ctl->extra2;
2381 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2382 if (!rtnl_trylock()) {
2383 /* Restore the original values before restarting */
2386 return restart_syscall();
2388 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2389 inet_forward_change(net);
2391 struct ipv4_devconf *cnf = ctl->extra1;
2392 struct in_device *idev =
2393 container_of(cnf, struct in_device, cnf);
2395 dev_disable_lro(idev->dev);
2396 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2397 NETCONFA_FORWARDING,
2402 rt_cache_flush(net);
2404 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2405 NETCONFA_FORWARDING,
2406 NETCONFA_IFINDEX_DEFAULT,
2407 net->ipv4.devconf_dflt);
2413 static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
2414 void __user *buffer,
2415 size_t *lenp, loff_t *ppos)
2417 int *valp = ctl->data;
2419 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2420 struct net *net = ctl->extra2;
2422 if (write && *valp != val)
2423 rt_cache_flush(net);
2428 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2431 .data = ipv4_devconf.data + \
2432 IPV4_DEVCONF_ ## attr - 1, \
2433 .maxlen = sizeof(int), \
2435 .proc_handler = proc, \
2436 .extra1 = &ipv4_devconf, \
2439 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2440 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2442 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2443 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2445 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2446 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2448 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2449 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2451 static struct devinet_sysctl_table {
2452 struct ctl_table_header *sysctl_header;
2453 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2454 } devinet_sysctl = {
2456 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2457 devinet_sysctl_forward),
2458 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2459 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
2461 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2462 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2463 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2464 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2465 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2466 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2467 "accept_source_route"),
2468 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2469 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2470 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2471 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2472 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2473 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2474 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2475 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2476 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2477 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2478 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2479 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2480 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2481 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2482 "force_igmp_version"),
2483 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2484 "igmpv2_unsolicited_report_interval"),
2485 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2486 "igmpv3_unsolicited_report_interval"),
2487 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2488 "ignore_routes_with_linkdown"),
2489 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2490 "drop_gratuitous_arp"),
2492 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2493 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
2494 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2495 "promote_secondaries"),
2496 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2498 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2499 "drop_unicast_in_l2_multicast"),
2503 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2504 int ifindex, struct ipv4_devconf *p)
2507 struct devinet_sysctl_table *t;
2508 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2510 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
2514 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2515 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2516 t->devinet_vars[i].extra1 = p;
2517 t->devinet_vars[i].extra2 = net;
2520 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2522 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2523 if (!t->sysctl_header)
2528 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2538 static void __devinet_sysctl_unregister(struct net *net,
2539 struct ipv4_devconf *cnf, int ifindex)
2541 struct devinet_sysctl_table *t = cnf->sysctl;
2545 unregister_net_sysctl_table(t->sysctl_header);
2549 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
2552 static int devinet_sysctl_register(struct in_device *idev)
2556 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2559 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2562 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2563 idev->dev->ifindex, &idev->cnf);
2565 neigh_sysctl_unregister(idev->arp_parms);
2569 static void devinet_sysctl_unregister(struct in_device *idev)
2571 struct net *net = dev_net(idev->dev);
2573 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
2574 neigh_sysctl_unregister(idev->arp_parms);
2577 static struct ctl_table ctl_forward_entry[] = {
2579 .procname = "ip_forward",
2580 .data = &ipv4_devconf.data[
2581 IPV4_DEVCONF_FORWARDING - 1],
2582 .maxlen = sizeof(int),
2584 .proc_handler = devinet_sysctl_forward,
2585 .extra1 = &ipv4_devconf,
2586 .extra2 = &init_net,
2592 static __net_init int devinet_init_net(struct net *net)
2595 struct ipv4_devconf *all, *dflt;
2596 #ifdef CONFIG_SYSCTL
2597 struct ctl_table *tbl;
2598 struct ctl_table_header *forw_hdr;
2602 all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
2606 dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2608 goto err_alloc_dflt;
2610 #ifdef CONFIG_SYSCTL
2611 tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
2615 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2616 tbl[0].extra1 = all;
2617 tbl[0].extra2 = net;
2620 if ((!IS_ENABLED(CONFIG_SYSCTL) ||
2621 sysctl_devconf_inherit_init_net != 2) &&
2622 !net_eq(net, &init_net)) {
2623 memcpy(all, init_net.ipv4.devconf_all, sizeof(ipv4_devconf));
2624 memcpy(dflt, init_net.ipv4.devconf_dflt, sizeof(ipv4_devconf_dflt));
2627 #ifdef CONFIG_SYSCTL
2628 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
2632 err = __devinet_sysctl_register(net, "default",
2633 NETCONFA_IFINDEX_DEFAULT, dflt);
2638 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2641 net->ipv4.forw_hdr = forw_hdr;
2644 net->ipv4.devconf_all = all;
2645 net->ipv4.devconf_dflt = dflt;
2648 #ifdef CONFIG_SYSCTL
2650 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
2652 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
2664 static __net_exit void devinet_exit_net(struct net *net)
2666 #ifdef CONFIG_SYSCTL
2667 struct ctl_table *tbl;
2669 tbl = net->ipv4.forw_hdr->ctl_table_arg;
2670 unregister_net_sysctl_table(net->ipv4.forw_hdr);
2671 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2672 NETCONFA_IFINDEX_DEFAULT);
2673 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2674 NETCONFA_IFINDEX_ALL);
2677 kfree(net->ipv4.devconf_dflt);
2678 kfree(net->ipv4.devconf_all);
2681 static __net_initdata struct pernet_operations devinet_ops = {
2682 .init = devinet_init_net,
2683 .exit = devinet_exit_net,
2686 static struct rtnl_af_ops inet_af_ops __read_mostly = {
2688 .fill_link_af = inet_fill_link_af,
2689 .get_link_af_size = inet_get_link_af_size,
2690 .validate_link_af = inet_validate_link_af,
2691 .set_link_af = inet_set_link_af,
2694 void __init devinet_init(void)
2698 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2699 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2701 register_pernet_subsys(&devinet_ops);
2703 register_gifconf(PF_INET, inet_gifconf);
2704 register_netdevice_notifier(&ip_netdev_notifier);
2706 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
2708 rtnl_af_register(&inet_af_ops);
2710 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2711 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2712 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
2713 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
2714 inet_netconf_dump_devconf, 0);