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 <asm/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/string.h>
37 #include <linux/socket.h>
38 #include <linux/sockios.h>
40 #include <linux/errno.h>
41 #include <linux/interrupt.h>
42 #include <linux/if_addr.h>
43 #include <linux/if_ether.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/init.h>
49 #include <linux/notifier.h>
50 #include <linux/inetdevice.h>
51 #include <linux/igmp.h>
52 #include <linux/slab.h>
53 #include <linux/hash.h>
55 #include <linux/sysctl.h>
57 #include <linux/kmod.h>
58 #include <linux/netconf.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64 #include <net/rtnetlink.h>
65 #include <net/net_namespace.h>
67 #include "fib_lookup.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,
78 static struct ipv4_devconf ipv4_devconf_dflt = {
80 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
81 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
82 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
83 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
84 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
88 #define IPV4_DEVCONF_DFLT(net, attr) \
89 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
91 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
92 [IFA_LOCAL] = { .type = NLA_U32 },
93 [IFA_ADDRESS] = { .type = NLA_U32 },
94 [IFA_BROADCAST] = { .type = NLA_U32 },
95 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
98 #define IN4_ADDR_HSIZE_SHIFT 8
99 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
101 static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
102 static DEFINE_SPINLOCK(inet_addr_hash_lock);
104 static u32 inet_addr_hash(struct net *net, __be32 addr)
106 u32 val = (__force u32) addr ^ net_hash_mix(net);
108 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
111 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
113 u32 hash = inet_addr_hash(net, ifa->ifa_local);
115 spin_lock(&inet_addr_hash_lock);
116 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
117 spin_unlock(&inet_addr_hash_lock);
120 static void inet_hash_remove(struct in_ifaddr *ifa)
122 spin_lock(&inet_addr_hash_lock);
123 hlist_del_init_rcu(&ifa->hash);
124 spin_unlock(&inet_addr_hash_lock);
128 * __ip_dev_find - find the first device with a given source address.
129 * @net: the net namespace
130 * @addr: the source address
131 * @devref: if true, take a reference on the found device
133 * If a caller uses devref=false, it should be protected by RCU, or RTNL
135 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
137 u32 hash = inet_addr_hash(net, addr);
138 struct net_device *result = NULL;
139 struct in_ifaddr *ifa;
140 struct hlist_node *node;
143 hlist_for_each_entry_rcu(ifa, node, &inet_addr_lst[hash], hash) {
144 if (ifa->ifa_local == addr) {
145 struct net_device *dev = ifa->ifa_dev->dev;
147 if (!net_eq(dev_net(dev), net))
154 struct flowi4 fl4 = { .daddr = addr };
155 struct fib_result res = { 0 };
156 struct fib_table *local;
158 /* Fallback to FIB local table so that communication
159 * over loopback subnets work.
161 local = fib_get_table(net, RT_TABLE_LOCAL);
163 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
164 res.type == RTN_LOCAL)
165 result = FIB_RES_DEV(res);
167 if (result && devref)
172 EXPORT_SYMBOL(__ip_dev_find);
174 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
176 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
177 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
180 static void devinet_sysctl_register(struct in_device *idev);
181 static void devinet_sysctl_unregister(struct in_device *idev);
183 static void devinet_sysctl_register(struct in_device *idev)
186 static void devinet_sysctl_unregister(struct in_device *idev)
191 /* Locks all the inet devices. */
193 static struct in_ifaddr *inet_alloc_ifa(void)
195 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
198 static void inet_rcu_free_ifa(struct rcu_head *head)
200 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
202 in_dev_put(ifa->ifa_dev);
206 static void inet_free_ifa(struct in_ifaddr *ifa)
208 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
211 void in_dev_finish_destroy(struct in_device *idev)
213 struct net_device *dev = idev->dev;
215 WARN_ON(idev->ifa_list);
216 WARN_ON(idev->mc_list);
217 #ifdef NET_REFCNT_DEBUG
218 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
222 pr_err("Freeing alive in_device %p\n", idev);
226 EXPORT_SYMBOL(in_dev_finish_destroy);
228 static struct in_device *inetdev_init(struct net_device *dev)
230 struct in_device *in_dev;
234 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
237 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
238 sizeof(in_dev->cnf));
239 in_dev->cnf.sysctl = NULL;
241 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
242 if (!in_dev->arp_parms)
244 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
245 dev_disable_lro(dev);
246 /* Reference in_dev->dev */
248 /* Account for reference dev->ip_ptr (below) */
251 devinet_sysctl_register(in_dev);
252 ip_mc_init_dev(in_dev);
253 if (dev->flags & IFF_UP)
256 /* we can receive as soon as ip_ptr is set -- do this last */
257 rcu_assign_pointer(dev->ip_ptr, in_dev);
266 static void in_dev_rcu_put(struct rcu_head *head)
268 struct in_device *idev = container_of(head, struct in_device, rcu_head);
272 static void inetdev_destroy(struct in_device *in_dev)
274 struct in_ifaddr *ifa;
275 struct net_device *dev;
283 ip_mc_destroy_dev(in_dev);
285 while ((ifa = in_dev->ifa_list) != NULL) {
286 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
290 RCU_INIT_POINTER(dev->ip_ptr, NULL);
292 devinet_sysctl_unregister(in_dev);
293 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
296 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
299 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
302 for_primary_ifa(in_dev) {
303 if (inet_ifa_match(a, ifa)) {
304 if (!b || inet_ifa_match(b, ifa)) {
309 } endfor_ifa(in_dev);
314 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
315 int destroy, struct nlmsghdr *nlh, u32 portid)
317 struct in_ifaddr *promote = NULL;
318 struct in_ifaddr *ifa, *ifa1 = *ifap;
319 struct in_ifaddr *last_prim = in_dev->ifa_list;
320 struct in_ifaddr *prev_prom = NULL;
321 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
325 /* 1. Deleting primary ifaddr forces deletion all secondaries
326 * unless alias promotion is set
329 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
330 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
332 while ((ifa = *ifap1) != NULL) {
333 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
334 ifa1->ifa_scope <= ifa->ifa_scope)
337 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
338 ifa1->ifa_mask != ifa->ifa_mask ||
339 !inet_ifa_match(ifa1->ifa_address, ifa)) {
340 ifap1 = &ifa->ifa_next;
346 inet_hash_remove(ifa);
347 *ifap1 = ifa->ifa_next;
349 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
350 blocking_notifier_call_chain(&inetaddr_chain,
360 /* On promotion all secondaries from subnet are changing
361 * the primary IP, we must remove all their routes silently
362 * and later to add them back with new prefsrc. Do this
363 * while all addresses are on the device list.
365 for (ifa = promote; ifa; ifa = ifa->ifa_next) {
366 if (ifa1->ifa_mask == ifa->ifa_mask &&
367 inet_ifa_match(ifa1->ifa_address, ifa))
368 fib_del_ifaddr(ifa, ifa1);
373 *ifap = ifa1->ifa_next;
374 inet_hash_remove(ifa1);
376 /* 3. Announce address deletion */
378 /* Send message first, then call notifier.
379 At first sight, FIB update triggered by notifier
380 will refer to already deleted ifaddr, that could confuse
381 netlink listeners. It is not true: look, gated sees
382 that route deleted and if it still thinks that ifaddr
383 is valid, it will try to restore deleted routes... Grr.
384 So that, this order is correct.
386 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
387 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
390 struct in_ifaddr *next_sec = promote->ifa_next;
393 prev_prom->ifa_next = promote->ifa_next;
394 promote->ifa_next = last_prim->ifa_next;
395 last_prim->ifa_next = promote;
398 promote->ifa_flags &= ~IFA_F_SECONDARY;
399 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
400 blocking_notifier_call_chain(&inetaddr_chain,
402 for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
403 if (ifa1->ifa_mask != ifa->ifa_mask ||
404 !inet_ifa_match(ifa1->ifa_address, ifa))
414 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
417 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
420 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
423 struct in_device *in_dev = ifa->ifa_dev;
424 struct in_ifaddr *ifa1, **ifap, **last_primary;
428 if (!ifa->ifa_local) {
433 ifa->ifa_flags &= ~IFA_F_SECONDARY;
434 last_primary = &in_dev->ifa_list;
436 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
437 ifap = &ifa1->ifa_next) {
438 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
439 ifa->ifa_scope <= ifa1->ifa_scope)
440 last_primary = &ifa1->ifa_next;
441 if (ifa1->ifa_mask == ifa->ifa_mask &&
442 inet_ifa_match(ifa1->ifa_address, ifa)) {
443 if (ifa1->ifa_local == ifa->ifa_local) {
447 if (ifa1->ifa_scope != ifa->ifa_scope) {
451 ifa->ifa_flags |= IFA_F_SECONDARY;
455 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
456 net_srandom(ifa->ifa_local);
460 ifa->ifa_next = *ifap;
463 inet_hash_insert(dev_net(in_dev->dev), ifa);
465 /* Send message first, then call notifier.
466 Notifier will trigger FIB update, so that
467 listeners of netlink will know about new ifaddr */
468 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
469 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
474 static int inet_insert_ifa(struct in_ifaddr *ifa)
476 return __inet_insert_ifa(ifa, NULL, 0);
479 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
481 struct in_device *in_dev = __in_dev_get_rtnl(dev);
489 ipv4_devconf_setall(in_dev);
490 if (ifa->ifa_dev != in_dev) {
491 WARN_ON(ifa->ifa_dev);
493 ifa->ifa_dev = in_dev;
495 if (ipv4_is_loopback(ifa->ifa_local))
496 ifa->ifa_scope = RT_SCOPE_HOST;
497 return inet_insert_ifa(ifa);
500 /* Caller must hold RCU or RTNL :
501 * We dont take a reference on found in_device
503 struct in_device *inetdev_by_index(struct net *net, int ifindex)
505 struct net_device *dev;
506 struct in_device *in_dev = NULL;
509 dev = dev_get_by_index_rcu(net, ifindex);
511 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
515 EXPORT_SYMBOL(inetdev_by_index);
517 /* Called only from RTNL semaphored context. No locks. */
519 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
524 for_primary_ifa(in_dev) {
525 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
527 } endfor_ifa(in_dev);
531 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
533 struct net *net = sock_net(skb->sk);
534 struct nlattr *tb[IFA_MAX+1];
535 struct in_device *in_dev;
536 struct ifaddrmsg *ifm;
537 struct in_ifaddr *ifa, **ifap;
542 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
546 ifm = nlmsg_data(nlh);
547 in_dev = inetdev_by_index(net, ifm->ifa_index);
548 if (in_dev == NULL) {
553 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
554 ifap = &ifa->ifa_next) {
556 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
559 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
562 if (tb[IFA_ADDRESS] &&
563 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
564 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
567 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
571 err = -EADDRNOTAVAIL;
576 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
578 struct nlattr *tb[IFA_MAX+1];
579 struct in_ifaddr *ifa;
580 struct ifaddrmsg *ifm;
581 struct net_device *dev;
582 struct in_device *in_dev;
585 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
589 ifm = nlmsg_data(nlh);
591 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
594 dev = __dev_get_by_index(net, ifm->ifa_index);
599 in_dev = __in_dev_get_rtnl(dev);
604 ifa = inet_alloc_ifa();
607 * A potential indev allocation can be left alive, it stays
608 * assigned to its device and is destroy with it.
612 ipv4_devconf_setall(in_dev);
615 if (tb[IFA_ADDRESS] == NULL)
616 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
618 INIT_HLIST_NODE(&ifa->hash);
619 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
620 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
621 ifa->ifa_flags = ifm->ifa_flags;
622 ifa->ifa_scope = ifm->ifa_scope;
623 ifa->ifa_dev = in_dev;
625 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
626 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
628 if (tb[IFA_BROADCAST])
629 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
632 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
634 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
642 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
644 struct net *net = sock_net(skb->sk);
645 struct in_ifaddr *ifa;
649 ifa = rtm_to_ifaddr(net, nlh);
653 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid);
657 * Determine a default network mask, based on the IP address.
660 static int inet_abc_len(__be32 addr)
662 int rc = -1; /* Something else, probably a multicast. */
664 if (ipv4_is_zeronet(addr))
667 __u32 haddr = ntohl(addr);
669 if (IN_CLASSA(haddr))
671 else if (IN_CLASSB(haddr))
673 else if (IN_CLASSC(haddr))
681 int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
684 struct sockaddr_in sin_orig;
685 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
686 struct in_device *in_dev;
687 struct in_ifaddr **ifap = NULL;
688 struct in_ifaddr *ifa = NULL;
689 struct net_device *dev;
692 int tryaddrmatch = 0;
695 * Fetch the caller's info block into kernel space
698 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
700 ifr.ifr_name[IFNAMSIZ - 1] = 0;
702 /* save original address for comparison */
703 memcpy(&sin_orig, sin, sizeof(*sin));
705 colon = strchr(ifr.ifr_name, ':');
709 dev_load(net, ifr.ifr_name);
712 case SIOCGIFADDR: /* Get interface address */
713 case SIOCGIFBRDADDR: /* Get the broadcast address */
714 case SIOCGIFDSTADDR: /* Get the destination address */
715 case SIOCGIFNETMASK: /* Get the netmask for the interface */
716 /* Note that these ioctls will not sleep,
717 so that we do not impose a lock.
718 One day we will be forced to put shlock here (I mean SMP)
720 tryaddrmatch = (sin_orig.sin_family == AF_INET);
721 memset(sin, 0, sizeof(*sin));
722 sin->sin_family = AF_INET;
727 if (!capable(CAP_NET_ADMIN))
730 case SIOCSIFADDR: /* Set interface address (and family) */
731 case SIOCSIFBRDADDR: /* Set the broadcast address */
732 case SIOCSIFDSTADDR: /* Set the destination address */
733 case SIOCSIFNETMASK: /* Set the netmask for the interface */
735 if (!capable(CAP_NET_ADMIN))
738 if (sin->sin_family != AF_INET)
749 dev = __dev_get_by_name(net, ifr.ifr_name);
756 in_dev = __in_dev_get_rtnl(dev);
759 /* Matthias Andree */
760 /* compare label and address (4.4BSD style) */
761 /* note: we only do this for a limited set of ioctls
762 and only if the original address family was AF_INET.
763 This is checked above. */
764 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
765 ifap = &ifa->ifa_next) {
766 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
767 sin_orig.sin_addr.s_addr ==
773 /* we didn't get a match, maybe the application is
774 4.3BSD-style and passed in junk so we fall back to
775 comparing just the label */
777 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
778 ifap = &ifa->ifa_next)
779 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
784 ret = -EADDRNOTAVAIL;
785 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
789 case SIOCGIFADDR: /* Get interface address */
790 sin->sin_addr.s_addr = ifa->ifa_local;
793 case SIOCGIFBRDADDR: /* Get the broadcast address */
794 sin->sin_addr.s_addr = ifa->ifa_broadcast;
797 case SIOCGIFDSTADDR: /* Get the destination address */
798 sin->sin_addr.s_addr = ifa->ifa_address;
801 case SIOCGIFNETMASK: /* Get the netmask for the interface */
802 sin->sin_addr.s_addr = ifa->ifa_mask;
807 ret = -EADDRNOTAVAIL;
811 if (!(ifr.ifr_flags & IFF_UP))
812 inet_del_ifa(in_dev, ifap, 1);
815 ret = dev_change_flags(dev, ifr.ifr_flags);
818 case SIOCSIFADDR: /* Set interface address (and family) */
820 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
825 ifa = inet_alloc_ifa();
826 INIT_HLIST_NODE(&ifa->hash);
830 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
832 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
835 if (ifa->ifa_local == sin->sin_addr.s_addr)
837 inet_del_ifa(in_dev, ifap, 0);
838 ifa->ifa_broadcast = 0;
842 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
844 if (!(dev->flags & IFF_POINTOPOINT)) {
845 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
846 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
847 if ((dev->flags & IFF_BROADCAST) &&
848 ifa->ifa_prefixlen < 31)
849 ifa->ifa_broadcast = ifa->ifa_address |
852 ifa->ifa_prefixlen = 32;
853 ifa->ifa_mask = inet_make_mask(32);
855 ret = inet_set_ifa(dev, ifa);
858 case SIOCSIFBRDADDR: /* Set the broadcast address */
860 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
861 inet_del_ifa(in_dev, ifap, 0);
862 ifa->ifa_broadcast = sin->sin_addr.s_addr;
863 inet_insert_ifa(ifa);
867 case SIOCSIFDSTADDR: /* Set the destination address */
869 if (ifa->ifa_address == sin->sin_addr.s_addr)
872 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
875 inet_del_ifa(in_dev, ifap, 0);
876 ifa->ifa_address = sin->sin_addr.s_addr;
877 inet_insert_ifa(ifa);
880 case SIOCSIFNETMASK: /* Set the netmask for the interface */
883 * The mask we set must be legal.
886 if (bad_mask(sin->sin_addr.s_addr, 0))
889 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
890 __be32 old_mask = ifa->ifa_mask;
891 inet_del_ifa(in_dev, ifap, 0);
892 ifa->ifa_mask = sin->sin_addr.s_addr;
893 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
895 /* See if current broadcast address matches
896 * with current netmask, then recalculate
897 * the broadcast address. Otherwise it's a
898 * funny address, so don't touch it since
899 * the user seems to know what (s)he's doing...
901 if ((dev->flags & IFF_BROADCAST) &&
902 (ifa->ifa_prefixlen < 31) &&
903 (ifa->ifa_broadcast ==
904 (ifa->ifa_local|~old_mask))) {
905 ifa->ifa_broadcast = (ifa->ifa_local |
906 ~sin->sin_addr.s_addr);
908 inet_insert_ifa(ifa);
918 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
922 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
924 struct in_device *in_dev = __in_dev_get_rtnl(dev);
925 struct in_ifaddr *ifa;
932 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
937 if (len < (int) sizeof(ifr))
939 memset(&ifr, 0, sizeof(struct ifreq));
941 strcpy(ifr.ifr_name, ifa->ifa_label);
943 strcpy(ifr.ifr_name, dev->name);
945 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
946 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
949 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
953 buf += sizeof(struct ifreq);
954 len -= sizeof(struct ifreq);
955 done += sizeof(struct ifreq);
961 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
964 struct in_device *in_dev;
965 struct net *net = dev_net(dev);
968 in_dev = __in_dev_get_rcu(dev);
972 for_primary_ifa(in_dev) {
973 if (ifa->ifa_scope > scope)
975 if (!dst || inet_ifa_match(dst, ifa)) {
976 addr = ifa->ifa_local;
980 addr = ifa->ifa_local;
981 } endfor_ifa(in_dev);
987 /* Not loopback addresses on loopback should be preferred
988 in this case. It is importnat that lo is the first interface
991 for_each_netdev_rcu(net, dev) {
992 in_dev = __in_dev_get_rcu(dev);
996 for_primary_ifa(in_dev) {
997 if (ifa->ifa_scope != RT_SCOPE_LINK &&
998 ifa->ifa_scope <= scope) {
999 addr = ifa->ifa_local;
1002 } endfor_ifa(in_dev);
1008 EXPORT_SYMBOL(inet_select_addr);
1010 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1011 __be32 local, int scope)
1018 (local == ifa->ifa_local || !local) &&
1019 ifa->ifa_scope <= scope) {
1020 addr = ifa->ifa_local;
1025 same = (!local || inet_ifa_match(local, ifa)) &&
1026 (!dst || inet_ifa_match(dst, ifa));
1030 /* Is the selected addr into dst subnet? */
1031 if (inet_ifa_match(addr, ifa))
1033 /* No, then can we use new local src? */
1034 if (ifa->ifa_scope <= scope) {
1035 addr = ifa->ifa_local;
1038 /* search for large dst subnet for addr */
1042 } endfor_ifa(in_dev);
1044 return same ? addr : 0;
1048 * Confirm that local IP address exists using wildcards:
1049 * - in_dev: only on this interface, 0=any interface
1050 * - dst: only in the same subnet as dst, 0=any dst
1051 * - local: address, 0=autoselect the local address
1052 * - scope: maximum allowed scope value for the local address
1054 __be32 inet_confirm_addr(struct in_device *in_dev,
1055 __be32 dst, __be32 local, int scope)
1058 struct net_device *dev;
1061 if (scope != RT_SCOPE_LINK)
1062 return confirm_addr_indev(in_dev, dst, local, scope);
1064 net = dev_net(in_dev->dev);
1066 for_each_netdev_rcu(net, dev) {
1067 in_dev = __in_dev_get_rcu(dev);
1069 addr = confirm_addr_indev(in_dev, dst, local, scope);
1078 EXPORT_SYMBOL(inet_confirm_addr);
1084 int register_inetaddr_notifier(struct notifier_block *nb)
1086 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1088 EXPORT_SYMBOL(register_inetaddr_notifier);
1090 int unregister_inetaddr_notifier(struct notifier_block *nb)
1092 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1094 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1096 /* Rename ifa_labels for a device name change. Make some effort to preserve
1097 * existing alias numbering and to create unique labels if possible.
1099 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1101 struct in_ifaddr *ifa;
1104 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1105 char old[IFNAMSIZ], *dot;
1107 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1108 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1111 dot = strchr(old, ':');
1113 sprintf(old, ":%d", named);
1116 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1117 strcat(ifa->ifa_label, dot);
1119 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1121 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1125 static bool inetdev_valid_mtu(unsigned int mtu)
1130 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1131 struct in_device *in_dev)
1134 struct in_ifaddr *ifa;
1136 for (ifa = in_dev->ifa_list; ifa;
1137 ifa = ifa->ifa_next) {
1138 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1139 ifa->ifa_local, dev,
1140 ifa->ifa_local, NULL,
1141 dev->dev_addr, NULL);
1145 /* Called only under RTNL semaphore */
1147 static int inetdev_event(struct notifier_block *this, unsigned long event,
1150 struct net_device *dev = ptr;
1151 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1156 if (event == NETDEV_REGISTER) {
1157 in_dev = inetdev_init(dev);
1159 return notifier_from_errno(-ENOMEM);
1160 if (dev->flags & IFF_LOOPBACK) {
1161 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1162 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1164 } else if (event == NETDEV_CHANGEMTU) {
1165 /* Re-enabling IP */
1166 if (inetdev_valid_mtu(dev->mtu))
1167 in_dev = inetdev_init(dev);
1173 case NETDEV_REGISTER:
1174 pr_debug("%s: bug\n", __func__);
1175 RCU_INIT_POINTER(dev->ip_ptr, NULL);
1178 if (!inetdev_valid_mtu(dev->mtu))
1180 if (dev->flags & IFF_LOOPBACK) {
1181 struct in_ifaddr *ifa = inet_alloc_ifa();
1184 INIT_HLIST_NODE(&ifa->hash);
1186 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1187 ifa->ifa_prefixlen = 8;
1188 ifa->ifa_mask = inet_make_mask(8);
1189 in_dev_hold(in_dev);
1190 ifa->ifa_dev = in_dev;
1191 ifa->ifa_scope = RT_SCOPE_HOST;
1192 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1193 inet_insert_ifa(ifa);
1198 case NETDEV_CHANGEADDR:
1199 if (!IN_DEV_ARP_NOTIFY(in_dev))
1202 case NETDEV_NOTIFY_PEERS:
1203 /* Send gratuitous ARP to notify of link change */
1204 inetdev_send_gratuitous_arp(dev, in_dev);
1209 case NETDEV_PRE_TYPE_CHANGE:
1210 ip_mc_unmap(in_dev);
1212 case NETDEV_POST_TYPE_CHANGE:
1213 ip_mc_remap(in_dev);
1215 case NETDEV_CHANGEMTU:
1216 if (inetdev_valid_mtu(dev->mtu))
1218 /* disable IP when MTU is not enough */
1219 case NETDEV_UNREGISTER:
1220 inetdev_destroy(in_dev);
1222 case NETDEV_CHANGENAME:
1223 /* Do not notify about label change, this event is
1224 * not interesting to applications using netlink.
1226 inetdev_changename(dev, in_dev);
1228 devinet_sysctl_unregister(in_dev);
1229 devinet_sysctl_register(in_dev);
1236 static struct notifier_block ip_netdev_notifier = {
1237 .notifier_call = inetdev_event,
1240 static size_t inet_nlmsg_size(void)
1242 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1243 + nla_total_size(4) /* IFA_ADDRESS */
1244 + nla_total_size(4) /* IFA_LOCAL */
1245 + nla_total_size(4) /* IFA_BROADCAST */
1246 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1249 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1250 u32 portid, u32 seq, int event, unsigned int flags)
1252 struct ifaddrmsg *ifm;
1253 struct nlmsghdr *nlh;
1255 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
1259 ifm = nlmsg_data(nlh);
1260 ifm->ifa_family = AF_INET;
1261 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1262 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1263 ifm->ifa_scope = ifa->ifa_scope;
1264 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1266 if ((ifa->ifa_address &&
1267 nla_put_be32(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1269 nla_put_be32(skb, IFA_LOCAL, ifa->ifa_local)) ||
1270 (ifa->ifa_broadcast &&
1271 nla_put_be32(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1272 (ifa->ifa_label[0] &&
1273 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)))
1274 goto nla_put_failure;
1276 return nlmsg_end(skb, nlh);
1279 nlmsg_cancel(skb, nlh);
1283 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1285 struct net *net = sock_net(skb->sk);
1288 int ip_idx, s_ip_idx;
1289 struct net_device *dev;
1290 struct in_device *in_dev;
1291 struct in_ifaddr *ifa;
1292 struct hlist_head *head;
1293 struct hlist_node *node;
1296 s_idx = idx = cb->args[1];
1297 s_ip_idx = ip_idx = cb->args[2];
1299 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1301 head = &net->dev_index_head[h];
1303 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1306 if (h > s_h || idx > s_idx)
1308 in_dev = __in_dev_get_rcu(dev);
1312 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1313 ifa = ifa->ifa_next, ip_idx++) {
1314 if (ip_idx < s_ip_idx)
1316 if (inet_fill_ifaddr(skb, ifa,
1317 NETLINK_CB(cb->skb).portid,
1319 RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1333 cb->args[2] = ip_idx;
1338 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1341 struct sk_buff *skb;
1342 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1346 net = dev_net(ifa->ifa_dev->dev);
1347 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1351 err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0);
1353 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1354 WARN_ON(err == -EMSGSIZE);
1358 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1362 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1365 static size_t inet_get_link_af_size(const struct net_device *dev)
1367 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1372 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1375 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
1377 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1384 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1388 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1389 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1394 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1395 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1398 static int inet_validate_link_af(const struct net_device *dev,
1399 const struct nlattr *nla)
1401 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1404 if (dev && !__in_dev_get_rtnl(dev))
1405 return -EAFNOSUPPORT;
1407 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
1411 if (tb[IFLA_INET_CONF]) {
1412 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1413 int cfgid = nla_type(a);
1418 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1426 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1428 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1429 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1433 return -EAFNOSUPPORT;
1435 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
1438 if (tb[IFLA_INET_CONF]) {
1439 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1440 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1446 static int inet_netconf_msgsize_devconf(int type)
1448 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1449 + nla_total_size(4); /* NETCONFA_IFINDEX */
1451 if (type == NETCONFA_FORWARDING)
1452 size += nla_total_size(4);
1457 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1458 struct ipv4_devconf *devconf, u32 portid,
1459 u32 seq, int event, unsigned int flags,
1462 struct nlmsghdr *nlh;
1463 struct netconfmsg *ncm;
1465 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1470 ncm = nlmsg_data(nlh);
1471 ncm->ncm_family = AF_INET;
1473 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1474 goto nla_put_failure;
1476 if (type == NETCONFA_FORWARDING &&
1477 nla_put_s32(skb, NETCONFA_FORWARDING,
1478 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
1479 goto nla_put_failure;
1481 return nlmsg_end(skb, nlh);
1484 nlmsg_cancel(skb, nlh);
1488 static void inet_netconf_notify_devconf(struct net *net, int type, int ifindex,
1489 struct ipv4_devconf *devconf)
1491 struct sk_buff *skb;
1494 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_ATOMIC);
1498 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
1499 RTM_NEWNETCONF, 0, type);
1501 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1502 WARN_ON(err == -EMSGSIZE);
1506 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_ATOMIC);
1510 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
1513 #ifdef CONFIG_SYSCTL
1515 static void devinet_copy_dflt_conf(struct net *net, int i)
1517 struct net_device *dev;
1520 for_each_netdev_rcu(net, dev) {
1521 struct in_device *in_dev;
1523 in_dev = __in_dev_get_rcu(dev);
1524 if (in_dev && !test_bit(i, in_dev->cnf.state))
1525 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
1530 /* called with RTNL locked */
1531 static void inet_forward_change(struct net *net)
1533 struct net_device *dev;
1534 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
1536 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
1537 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
1538 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1539 NETCONFA_IFINDEX_ALL,
1540 net->ipv4.devconf_all);
1541 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1542 NETCONFA_IFINDEX_DEFAULT,
1543 net->ipv4.devconf_dflt);
1545 for_each_netdev(net, dev) {
1546 struct in_device *in_dev;
1548 dev_disable_lro(dev);
1550 in_dev = __in_dev_get_rcu(dev);
1552 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1553 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1554 dev->ifindex, &in_dev->cnf);
1560 static int devinet_conf_proc(ctl_table *ctl, int write,
1561 void __user *buffer,
1562 size_t *lenp, loff_t *ppos)
1564 int old_value = *(int *)ctl->data;
1565 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1566 int new_value = *(int *)ctl->data;
1569 struct ipv4_devconf *cnf = ctl->extra1;
1570 struct net *net = ctl->extra2;
1571 int i = (int *)ctl->data - cnf->data;
1573 set_bit(i, cnf->state);
1575 if (cnf == net->ipv4.devconf_dflt)
1576 devinet_copy_dflt_conf(net, i);
1577 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
1578 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
1579 if ((new_value == 0) && (old_value != 0))
1580 rt_cache_flush(net);
1586 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1587 void __user *buffer,
1588 size_t *lenp, loff_t *ppos)
1590 int *valp = ctl->data;
1593 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1595 if (write && *valp != val) {
1596 struct net *net = ctl->extra2;
1598 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
1599 if (!rtnl_trylock()) {
1600 /* Restore the original values before restarting */
1603 return restart_syscall();
1605 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1606 inet_forward_change(net);
1608 struct ipv4_devconf *cnf = ctl->extra1;
1609 struct in_device *idev =
1610 container_of(cnf, struct in_device, cnf);
1612 dev_disable_lro(idev->dev);
1613 inet_netconf_notify_devconf(net,
1614 NETCONFA_FORWARDING,
1619 rt_cache_flush(net);
1621 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1622 NETCONFA_IFINDEX_DEFAULT,
1623 net->ipv4.devconf_dflt);
1629 static int ipv4_doint_and_flush(ctl_table *ctl, int write,
1630 void __user *buffer,
1631 size_t *lenp, loff_t *ppos)
1633 int *valp = ctl->data;
1635 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1636 struct net *net = ctl->extra2;
1638 if (write && *valp != val)
1639 rt_cache_flush(net);
1644 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
1647 .data = ipv4_devconf.data + \
1648 IPV4_DEVCONF_ ## attr - 1, \
1649 .maxlen = sizeof(int), \
1651 .proc_handler = proc, \
1652 .extra1 = &ipv4_devconf, \
1655 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
1656 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
1658 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
1659 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
1661 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
1662 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
1664 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1665 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
1667 static struct devinet_sysctl_table {
1668 struct ctl_table_header *sysctl_header;
1669 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
1670 } devinet_sysctl = {
1672 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
1673 devinet_sysctl_forward),
1674 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1676 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1677 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1678 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1679 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1680 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1681 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1682 "accept_source_route"),
1683 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
1684 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
1685 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1686 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1687 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1688 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1689 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1690 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1691 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1692 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1693 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1694 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
1695 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
1697 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1698 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1699 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1700 "force_igmp_version"),
1701 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1702 "promote_secondaries"),
1703 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
1708 static int __devinet_sysctl_register(struct net *net, char *dev_name,
1709 struct ipv4_devconf *p)
1712 struct devinet_sysctl_table *t;
1713 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
1715 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
1719 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1720 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1721 t->devinet_vars[i].extra1 = p;
1722 t->devinet_vars[i].extra2 = net;
1725 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
1727 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
1728 if (!t->sysctl_header)
1740 static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
1742 struct devinet_sysctl_table *t = cnf->sysctl;
1748 unregister_net_sysctl_table(t->sysctl_header);
1752 static void devinet_sysctl_register(struct in_device *idev)
1754 neigh_sysctl_register(idev->dev, idev->arp_parms, "ipv4", NULL);
1755 __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
1759 static void devinet_sysctl_unregister(struct in_device *idev)
1761 __devinet_sysctl_unregister(&idev->cnf);
1762 neigh_sysctl_unregister(idev->arp_parms);
1765 static struct ctl_table ctl_forward_entry[] = {
1767 .procname = "ip_forward",
1768 .data = &ipv4_devconf.data[
1769 IPV4_DEVCONF_FORWARDING - 1],
1770 .maxlen = sizeof(int),
1772 .proc_handler = devinet_sysctl_forward,
1773 .extra1 = &ipv4_devconf,
1774 .extra2 = &init_net,
1780 static __net_init int devinet_init_net(struct net *net)
1783 struct ipv4_devconf *all, *dflt;
1784 #ifdef CONFIG_SYSCTL
1785 struct ctl_table *tbl = ctl_forward_entry;
1786 struct ctl_table_header *forw_hdr;
1790 all = &ipv4_devconf;
1791 dflt = &ipv4_devconf_dflt;
1793 if (!net_eq(net, &init_net)) {
1794 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1798 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1800 goto err_alloc_dflt;
1802 #ifdef CONFIG_SYSCTL
1803 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1807 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
1808 tbl[0].extra1 = all;
1809 tbl[0].extra2 = net;
1813 #ifdef CONFIG_SYSCTL
1814 err = __devinet_sysctl_register(net, "all", all);
1818 err = __devinet_sysctl_register(net, "default", dflt);
1823 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
1824 if (forw_hdr == NULL)
1826 net->ipv4.forw_hdr = forw_hdr;
1829 net->ipv4.devconf_all = all;
1830 net->ipv4.devconf_dflt = dflt;
1833 #ifdef CONFIG_SYSCTL
1835 __devinet_sysctl_unregister(dflt);
1837 __devinet_sysctl_unregister(all);
1839 if (tbl != ctl_forward_entry)
1843 if (dflt != &ipv4_devconf_dflt)
1846 if (all != &ipv4_devconf)
1852 static __net_exit void devinet_exit_net(struct net *net)
1854 #ifdef CONFIG_SYSCTL
1855 struct ctl_table *tbl;
1857 tbl = net->ipv4.forw_hdr->ctl_table_arg;
1858 unregister_net_sysctl_table(net->ipv4.forw_hdr);
1859 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1860 __devinet_sysctl_unregister(net->ipv4.devconf_all);
1863 kfree(net->ipv4.devconf_dflt);
1864 kfree(net->ipv4.devconf_all);
1867 static __net_initdata struct pernet_operations devinet_ops = {
1868 .init = devinet_init_net,
1869 .exit = devinet_exit_net,
1872 static struct rtnl_af_ops inet_af_ops = {
1874 .fill_link_af = inet_fill_link_af,
1875 .get_link_af_size = inet_get_link_af_size,
1876 .validate_link_af = inet_validate_link_af,
1877 .set_link_af = inet_set_link_af,
1880 void __init devinet_init(void)
1884 for (i = 0; i < IN4_ADDR_HSIZE; i++)
1885 INIT_HLIST_HEAD(&inet_addr_lst[i]);
1887 register_pernet_subsys(&devinet_ops);
1889 register_gifconf(PF_INET, inet_gifconf);
1890 register_netdevice_notifier(&ip_netdev_notifier);
1892 rtnl_af_register(&inet_af_ops);
1894 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, NULL);
1895 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, NULL);
1896 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, NULL);