11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NET3 IP device support routines. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $ 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Derived from the IP parts of dev.c 1.0.19 1202c30a84SJesper Juhl * Authors: Ross Biro 131da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 141da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Additional Authors: 171da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 181da177e4SLinus Torvalds * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Changes: 211da177e4SLinus Torvalds * Alexey Kuznetsov: pa_* fields are replaced with ifaddr 221da177e4SLinus Torvalds * lists. 231da177e4SLinus Torvalds * Cyrus Durgin: updated for kmod 241da177e4SLinus Torvalds * Matthias Andree: in devinet_ioctl, compare label and 251da177e4SLinus Torvalds * address (4.4BSD alias style support), 261da177e4SLinus Torvalds * fall back to comparing just the label 271da177e4SLinus Torvalds * if no match found. 281da177e4SLinus Torvalds */ 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <asm/uaccess.h> 321da177e4SLinus Torvalds #include <asm/system.h> 331da177e4SLinus Torvalds #include <linux/bitops.h> 344fc268d2SRandy Dunlap #include <linux/capability.h> 351da177e4SLinus Torvalds #include <linux/module.h> 361da177e4SLinus Torvalds #include <linux/types.h> 371da177e4SLinus Torvalds #include <linux/kernel.h> 381da177e4SLinus Torvalds #include <linux/string.h> 391da177e4SLinus Torvalds #include <linux/mm.h> 401da177e4SLinus Torvalds #include <linux/socket.h> 411da177e4SLinus Torvalds #include <linux/sockios.h> 421da177e4SLinus Torvalds #include <linux/in.h> 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/interrupt.h> 451823730fSThomas Graf #include <linux/if_addr.h> 461da177e4SLinus Torvalds #include <linux/if_ether.h> 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #include <linux/etherdevice.h> 501da177e4SLinus Torvalds #include <linux/skbuff.h> 511da177e4SLinus Torvalds #include <linux/init.h> 521da177e4SLinus Torvalds #include <linux/notifier.h> 531da177e4SLinus Torvalds #include <linux/inetdevice.h> 541da177e4SLinus Torvalds #include <linux/igmp.h> 551da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 561da177e4SLinus Torvalds #include <linux/sysctl.h> 571da177e4SLinus Torvalds #endif 581da177e4SLinus Torvalds #include <linux/kmod.h> 591da177e4SLinus Torvalds 6014c85021SArnaldo Carvalho de Melo #include <net/arp.h> 611da177e4SLinus Torvalds #include <net/ip.h> 621da177e4SLinus Torvalds #include <net/route.h> 631da177e4SLinus Torvalds #include <net/ip_fib.h> 6463f3444fSThomas Graf #include <net/rtnetlink.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds struct ipv4_devconf ipv4_devconf = { 6742f811b8SHerbert Xu .data = { 6842f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, 6942f811b8SHerbert Xu [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, 7042f811b8SHerbert Xu [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, 7142f811b8SHerbert Xu [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, 7242f811b8SHerbert Xu }, 731da177e4SLinus Torvalds }; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds static struct ipv4_devconf ipv4_devconf_dflt = { 7642f811b8SHerbert Xu .data = { 7742f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, 7842f811b8SHerbert Xu [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, 7942f811b8SHerbert Xu [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, 8042f811b8SHerbert Xu [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, 8142f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1, 8242f811b8SHerbert Xu }, 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 8542f811b8SHerbert Xu #define IPV4_DEVCONF_DFLT(attr) IPV4_DEVCONF(ipv4_devconf_dflt, attr) 8642f811b8SHerbert Xu 87ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { 885c753978SThomas Graf [IFA_LOCAL] = { .type = NLA_U32 }, 895c753978SThomas Graf [IFA_ADDRESS] = { .type = NLA_U32 }, 905c753978SThomas Graf [IFA_BROADCAST] = { .type = NLA_U32 }, 915c753978SThomas Graf [IFA_ANYCAST] = { .type = NLA_U32 }, 925176f91eSThomas Graf [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, 935c753978SThomas Graf }; 945c753978SThomas Graf 95d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); 961da177e4SLinus Torvalds 97e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); 981da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 991da177e4SLinus Torvalds int destroy); 1001da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1011da177e4SLinus Torvalds static void devinet_sysctl_register(struct in_device *in_dev, 1021da177e4SLinus Torvalds struct ipv4_devconf *p); 1031da177e4SLinus Torvalds static void devinet_sysctl_unregister(struct ipv4_devconf *p); 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* Locks all the inet devices. */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds static struct in_ifaddr *inet_alloc_ifa(void) 1091da177e4SLinus Torvalds { 1100da974f4SPanagiotis Issaris struct in_ifaddr *ifa = kzalloc(sizeof(*ifa), GFP_KERNEL); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds if (ifa) { 1131da177e4SLinus Torvalds INIT_RCU_HEAD(&ifa->rcu_head); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds return ifa; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static void inet_rcu_free_ifa(struct rcu_head *head) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); 1221da177e4SLinus Torvalds if (ifa->ifa_dev) 1231da177e4SLinus Torvalds in_dev_put(ifa->ifa_dev); 1241da177e4SLinus Torvalds kfree(ifa); 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline void inet_free_ifa(struct in_ifaddr *ifa) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds call_rcu(&ifa->rcu_head, inet_rcu_free_ifa); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds void in_dev_finish_destroy(struct in_device *idev) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct net_device *dev = idev->dev; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds BUG_TRAP(!idev->ifa_list); 1371da177e4SLinus Torvalds BUG_TRAP(!idev->mc_list); 1381da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 1391da177e4SLinus Torvalds printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n", 1401da177e4SLinus Torvalds idev, dev ? dev->name : "NIL"); 1411da177e4SLinus Torvalds #endif 1421da177e4SLinus Torvalds dev_put(dev); 1431da177e4SLinus Torvalds if (!idev->dead) 1441da177e4SLinus Torvalds printk("Freeing alive in_device %p\n", idev); 1451da177e4SLinus Torvalds else { 1461da177e4SLinus Torvalds kfree(idev); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 15071e27da9SHerbert Xu static struct in_device *inetdev_init(struct net_device *dev) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct in_device *in_dev; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds ASSERT_RTNL(); 1551da177e4SLinus Torvalds 1560da974f4SPanagiotis Issaris in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); 1571da177e4SLinus Torvalds if (!in_dev) 1581da177e4SLinus Torvalds goto out; 1591da177e4SLinus Torvalds INIT_RCU_HEAD(&in_dev->rcu_head); 1601da177e4SLinus Torvalds memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf)); 1611da177e4SLinus Torvalds in_dev->cnf.sysctl = NULL; 1621da177e4SLinus Torvalds in_dev->dev = dev; 1631da177e4SLinus Torvalds if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL) 1641da177e4SLinus Torvalds goto out_kfree; 1651da177e4SLinus Torvalds /* Reference in_dev->dev */ 1661da177e4SLinus Torvalds dev_hold(dev); 1671da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1681da177e4SLinus Torvalds neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4, 1691da177e4SLinus Torvalds NET_IPV4_NEIGH, "ipv4", NULL, NULL); 1701da177e4SLinus Torvalds #endif 1711da177e4SLinus Torvalds 17230c4cf57SDavid L Stevens /* Account for reference dev->ip_ptr (below) */ 1731da177e4SLinus Torvalds in_dev_hold(in_dev); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1761da177e4SLinus Torvalds devinet_sysctl_register(in_dev, &in_dev->cnf); 1771da177e4SLinus Torvalds #endif 1781da177e4SLinus Torvalds ip_mc_init_dev(in_dev); 1791da177e4SLinus Torvalds if (dev->flags & IFF_UP) 1801da177e4SLinus Torvalds ip_mc_up(in_dev); 181483479ecSJarek Poplawski 18230c4cf57SDavid L Stevens /* we can receive as soon as ip_ptr is set -- do this last */ 18330c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip_ptr, in_dev); 184483479ecSJarek Poplawski out: 1851da177e4SLinus Torvalds return in_dev; 1861da177e4SLinus Torvalds out_kfree: 1871da177e4SLinus Torvalds kfree(in_dev); 1881da177e4SLinus Torvalds in_dev = NULL; 1891da177e4SLinus Torvalds goto out; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static void in_dev_rcu_put(struct rcu_head *head) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct in_device *idev = container_of(head, struct in_device, rcu_head); 1951da177e4SLinus Torvalds in_dev_put(idev); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds static void inetdev_destroy(struct in_device *in_dev) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct in_ifaddr *ifa; 2011da177e4SLinus Torvalds struct net_device *dev; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds ASSERT_RTNL(); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds dev = in_dev->dev; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds in_dev->dead = 1; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds ip_mc_destroy_dev(in_dev); 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds while ((ifa = in_dev->ifa_list) != NULL) { 2121da177e4SLinus Torvalds inet_del_ifa(in_dev, &in_dev->ifa_list, 0); 2131da177e4SLinus Torvalds inet_free_ifa(ifa); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 2171da177e4SLinus Torvalds devinet_sysctl_unregister(&in_dev->cnf); 2181da177e4SLinus Torvalds #endif 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds dev->ip_ptr = NULL; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 2231da177e4SLinus Torvalds neigh_sysctl_unregister(in_dev->arp_parms); 2241da177e4SLinus Torvalds #endif 2251da177e4SLinus Torvalds neigh_parms_release(&arp_tbl, in_dev->arp_parms); 2261da177e4SLinus Torvalds arp_ifdown(dev); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds call_rcu(&in_dev->rcu_head, in_dev_rcu_put); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231ff428d72SAl Viro int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds rcu_read_lock(); 2341da177e4SLinus Torvalds for_primary_ifa(in_dev) { 2351da177e4SLinus Torvalds if (inet_ifa_match(a, ifa)) { 2361da177e4SLinus Torvalds if (!b || inet_ifa_match(b, ifa)) { 2371da177e4SLinus Torvalds rcu_read_unlock(); 2381da177e4SLinus Torvalds return 1; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds } endfor_ifa(in_dev); 2421da177e4SLinus Torvalds rcu_read_unlock(); 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 246d6062cbbSThomas Graf static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 247d6062cbbSThomas Graf int destroy, struct nlmsghdr *nlh, u32 pid) 2481da177e4SLinus Torvalds { 2498f937c60SHarald Welte struct in_ifaddr *promote = NULL; 2500ff60a45SJamal Hadi Salim struct in_ifaddr *ifa, *ifa1 = *ifap; 2510ff60a45SJamal Hadi Salim struct in_ifaddr *last_prim = in_dev->ifa_list; 2520ff60a45SJamal Hadi Salim struct in_ifaddr *prev_prom = NULL; 2530ff60a45SJamal Hadi Salim int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds ASSERT_RTNL(); 2561da177e4SLinus Torvalds 2578f937c60SHarald Welte /* 1. Deleting primary ifaddr forces deletion all secondaries 2588f937c60SHarald Welte * unless alias promotion is set 2598f937c60SHarald Welte **/ 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { 2621da177e4SLinus Torvalds struct in_ifaddr **ifap1 = &ifa1->ifa_next; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds while ((ifa = *ifap1) != NULL) { 2650ff60a45SJamal Hadi Salim if (!(ifa->ifa_flags & IFA_F_SECONDARY) && 2660ff60a45SJamal Hadi Salim ifa1->ifa_scope <= ifa->ifa_scope) 2670ff60a45SJamal Hadi Salim last_prim = ifa; 2680ff60a45SJamal Hadi Salim 2691da177e4SLinus Torvalds if (!(ifa->ifa_flags & IFA_F_SECONDARY) || 2701da177e4SLinus Torvalds ifa1->ifa_mask != ifa->ifa_mask || 2711da177e4SLinus Torvalds !inet_ifa_match(ifa1->ifa_address, ifa)) { 2721da177e4SLinus Torvalds ifap1 = &ifa->ifa_next; 2730ff60a45SJamal Hadi Salim prev_prom = ifa; 2741da177e4SLinus Torvalds continue; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2770ff60a45SJamal Hadi Salim if (!do_promote) { 2781da177e4SLinus Torvalds *ifap1 = ifa->ifa_next; 2791da177e4SLinus Torvalds 280d6062cbbSThomas Graf rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid); 281e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, 282e041c683SAlan Stern NETDEV_DOWN, ifa); 2831da177e4SLinus Torvalds inet_free_ifa(ifa); 2848f937c60SHarald Welte } else { 2858f937c60SHarald Welte promote = ifa; 2868f937c60SHarald Welte break; 2878f937c60SHarald Welte } 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2. Unlink it */ 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds *ifap = ifa1->ifa_next; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* 3. Announce address deletion */ 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Send message first, then call notifier. 2981da177e4SLinus Torvalds At first sight, FIB update triggered by notifier 2991da177e4SLinus Torvalds will refer to already deleted ifaddr, that could confuse 3001da177e4SLinus Torvalds netlink listeners. It is not true: look, gated sees 3011da177e4SLinus Torvalds that route deleted and if it still thinks that ifaddr 3021da177e4SLinus Torvalds is valid, it will try to restore deleted routes... Grr. 3031da177e4SLinus Torvalds So that, this order is correct. 3041da177e4SLinus Torvalds */ 305d6062cbbSThomas Graf rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid); 306e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); 3070ff60a45SJamal Hadi Salim 3080ff60a45SJamal Hadi Salim if (promote) { 3090ff60a45SJamal Hadi Salim 3100ff60a45SJamal Hadi Salim if (prev_prom) { 3110ff60a45SJamal Hadi Salim prev_prom->ifa_next = promote->ifa_next; 3120ff60a45SJamal Hadi Salim promote->ifa_next = last_prim->ifa_next; 3130ff60a45SJamal Hadi Salim last_prim->ifa_next = promote; 3140ff60a45SJamal Hadi Salim } 3150ff60a45SJamal Hadi Salim 3160ff60a45SJamal Hadi Salim promote->ifa_flags &= ~IFA_F_SECONDARY; 317d6062cbbSThomas Graf rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid); 318e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, 319e041c683SAlan Stern NETDEV_UP, promote); 3200ff60a45SJamal Hadi Salim for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) { 3210ff60a45SJamal Hadi Salim if (ifa1->ifa_mask != ifa->ifa_mask || 3220ff60a45SJamal Hadi Salim !inet_ifa_match(ifa1->ifa_address, ifa)) 3230ff60a45SJamal Hadi Salim continue; 3240ff60a45SJamal Hadi Salim fib_add_ifaddr(ifa); 3250ff60a45SJamal Hadi Salim } 3260ff60a45SJamal Hadi Salim 3270ff60a45SJamal Hadi Salim } 3286363097cSHerbert Xu if (destroy) 3291da177e4SLinus Torvalds inet_free_ifa(ifa1); 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332d6062cbbSThomas Graf static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 333d6062cbbSThomas Graf int destroy) 334d6062cbbSThomas Graf { 335d6062cbbSThomas Graf __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); 336d6062cbbSThomas Graf } 337d6062cbbSThomas Graf 338d6062cbbSThomas Graf static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, 339d6062cbbSThomas Graf u32 pid) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds struct in_device *in_dev = ifa->ifa_dev; 3421da177e4SLinus Torvalds struct in_ifaddr *ifa1, **ifap, **last_primary; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds ASSERT_RTNL(); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds if (!ifa->ifa_local) { 3471da177e4SLinus Torvalds inet_free_ifa(ifa); 3481da177e4SLinus Torvalds return 0; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds ifa->ifa_flags &= ~IFA_F_SECONDARY; 3521da177e4SLinus Torvalds last_primary = &in_dev->ifa_list; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; 3551da177e4SLinus Torvalds ifap = &ifa1->ifa_next) { 3561da177e4SLinus Torvalds if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && 3571da177e4SLinus Torvalds ifa->ifa_scope <= ifa1->ifa_scope) 3581da177e4SLinus Torvalds last_primary = &ifa1->ifa_next; 3591da177e4SLinus Torvalds if (ifa1->ifa_mask == ifa->ifa_mask && 3601da177e4SLinus Torvalds inet_ifa_match(ifa1->ifa_address, ifa)) { 3611da177e4SLinus Torvalds if (ifa1->ifa_local == ifa->ifa_local) { 3621da177e4SLinus Torvalds inet_free_ifa(ifa); 3631da177e4SLinus Torvalds return -EEXIST; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds if (ifa1->ifa_scope != ifa->ifa_scope) { 3661da177e4SLinus Torvalds inet_free_ifa(ifa); 3671da177e4SLinus Torvalds return -EINVAL; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds ifa->ifa_flags |= IFA_F_SECONDARY; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (!(ifa->ifa_flags & IFA_F_SECONDARY)) { 3741da177e4SLinus Torvalds net_srandom(ifa->ifa_local); 3751da177e4SLinus Torvalds ifap = last_primary; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds ifa->ifa_next = *ifap; 3791da177e4SLinus Torvalds *ifap = ifa; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* Send message first, then call notifier. 3821da177e4SLinus Torvalds Notifier will trigger FIB update, so that 3831da177e4SLinus Torvalds listeners of netlink will know about new ifaddr */ 384d6062cbbSThomas Graf rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid); 385e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds return 0; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 390d6062cbbSThomas Graf static int inet_insert_ifa(struct in_ifaddr *ifa) 391d6062cbbSThomas Graf { 392d6062cbbSThomas Graf return __inet_insert_ifa(ifa, NULL, 0); 393d6062cbbSThomas Graf } 394d6062cbbSThomas Graf 3951da177e4SLinus Torvalds static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) 3961da177e4SLinus Torvalds { 397e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds ASSERT_RTNL(); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds if (!in_dev) { 4021da177e4SLinus Torvalds inet_free_ifa(ifa); 4031da177e4SLinus Torvalds return -ENOBUFS; 4041da177e4SLinus Torvalds } 40571e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 4061da177e4SLinus Torvalds if (ifa->ifa_dev != in_dev) { 4071da177e4SLinus Torvalds BUG_TRAP(!ifa->ifa_dev); 4081da177e4SLinus Torvalds in_dev_hold(in_dev); 4091da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds if (LOOPBACK(ifa->ifa_local)) 4121da177e4SLinus Torvalds ifa->ifa_scope = RT_SCOPE_HOST; 4131da177e4SLinus Torvalds return inet_insert_ifa(ifa); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds struct in_device *inetdev_by_index(int ifindex) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds struct net_device *dev; 4191da177e4SLinus Torvalds struct in_device *in_dev = NULL; 4201da177e4SLinus Torvalds read_lock(&dev_base_lock); 421881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifindex); 4221da177e4SLinus Torvalds if (dev) 4231da177e4SLinus Torvalds in_dev = in_dev_get(dev); 4241da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4251da177e4SLinus Torvalds return in_dev; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* Called only from RTNL semaphored context. No locks. */ 4291da177e4SLinus Torvalds 43060cad5daSAl Viro struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, 43160cad5daSAl Viro __be32 mask) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds ASSERT_RTNL(); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds for_primary_ifa(in_dev) { 4361da177e4SLinus Torvalds if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) 4371da177e4SLinus Torvalds return ifa; 4381da177e4SLinus Torvalds } endfor_ifa(in_dev); 4391da177e4SLinus Torvalds return NULL; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 4431da177e4SLinus Torvalds { 444b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 445dfdd5fd4SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4461da177e4SLinus Torvalds struct in_device *in_dev; 447dfdd5fd4SThomas Graf struct ifaddrmsg *ifm; 4481da177e4SLinus Torvalds struct in_ifaddr *ifa, **ifap; 449dfdd5fd4SThomas Graf int err = -EINVAL; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds ASSERT_RTNL(); 4521da177e4SLinus Torvalds 453b854272bSDenis V. Lunev if (net != &init_net) 454b854272bSDenis V. Lunev return -EINVAL; 455b854272bSDenis V. Lunev 456dfdd5fd4SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); 457dfdd5fd4SThomas Graf if (err < 0) 458dfdd5fd4SThomas Graf goto errout; 459dfdd5fd4SThomas Graf 460dfdd5fd4SThomas Graf ifm = nlmsg_data(nlh); 461dfdd5fd4SThomas Graf in_dev = inetdev_by_index(ifm->ifa_index); 462dfdd5fd4SThomas Graf if (in_dev == NULL) { 463dfdd5fd4SThomas Graf err = -ENODEV; 464dfdd5fd4SThomas Graf goto errout; 465dfdd5fd4SThomas Graf } 466dfdd5fd4SThomas Graf 4671da177e4SLinus Torvalds __in_dev_put(in_dev); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 4701da177e4SLinus Torvalds ifap = &ifa->ifa_next) { 471dfdd5fd4SThomas Graf if (tb[IFA_LOCAL] && 472a7a628c4SAl Viro ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) 4731da177e4SLinus Torvalds continue; 474dfdd5fd4SThomas Graf 475dfdd5fd4SThomas Graf if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) 476dfdd5fd4SThomas Graf continue; 477dfdd5fd4SThomas Graf 478dfdd5fd4SThomas Graf if (tb[IFA_ADDRESS] && 479dfdd5fd4SThomas Graf (ifm->ifa_prefixlen != ifa->ifa_prefixlen || 480a7a628c4SAl Viro !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) 481dfdd5fd4SThomas Graf continue; 482dfdd5fd4SThomas Graf 483d6062cbbSThomas Graf __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid); 4841da177e4SLinus Torvalds return 0; 4851da177e4SLinus Torvalds } 486dfdd5fd4SThomas Graf 487dfdd5fd4SThomas Graf err = -EADDRNOTAVAIL; 488dfdd5fd4SThomas Graf errout: 489dfdd5fd4SThomas Graf return err; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4925c753978SThomas Graf static struct in_ifaddr *rtm_to_ifaddr(struct nlmsghdr *nlh) 4931da177e4SLinus Torvalds { 4945c753978SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4955c753978SThomas Graf struct in_ifaddr *ifa; 4965c753978SThomas Graf struct ifaddrmsg *ifm; 4971da177e4SLinus Torvalds struct net_device *dev; 4981da177e4SLinus Torvalds struct in_device *in_dev; 4995c753978SThomas Graf int err = -EINVAL; 5001da177e4SLinus Torvalds 5015c753978SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); 5025c753978SThomas Graf if (err < 0) 5035c753978SThomas Graf goto errout; 5041da177e4SLinus Torvalds 5055c753978SThomas Graf ifm = nlmsg_data(nlh); 506c4e38f41SEvgeniy Polyakov if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) { 507c4e38f41SEvgeniy Polyakov err = -EINVAL; 5085c753978SThomas Graf goto errout; 509c4e38f41SEvgeniy Polyakov } 5101da177e4SLinus Torvalds 511881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifm->ifa_index); 5125c753978SThomas Graf if (dev == NULL) { 5135c753978SThomas Graf err = -ENODEV; 5145c753978SThomas Graf goto errout; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5175c753978SThomas Graf in_dev = __in_dev_get_rtnl(dev); 5185c753978SThomas Graf if (in_dev == NULL) { 5195c753978SThomas Graf err = -ENOBUFS; 5205c753978SThomas Graf goto errout; 5215c753978SThomas Graf } 52271e27da9SHerbert Xu 5235c753978SThomas Graf ifa = inet_alloc_ifa(); 5245c753978SThomas Graf if (ifa == NULL) { 5255c753978SThomas Graf /* 5265c753978SThomas Graf * A potential indev allocation can be left alive, it stays 5275c753978SThomas Graf * assigned to its device and is destroy with it. 5285c753978SThomas Graf */ 5295c753978SThomas Graf err = -ENOBUFS; 5305c753978SThomas Graf goto errout; 5315c753978SThomas Graf } 5325c753978SThomas Graf 533a4e65d36SPavel Emelyanov ipv4_devconf_setall(in_dev); 5345c753978SThomas Graf in_dev_hold(in_dev); 5355c753978SThomas Graf 5365c753978SThomas Graf if (tb[IFA_ADDRESS] == NULL) 5375c753978SThomas Graf tb[IFA_ADDRESS] = tb[IFA_LOCAL]; 5385c753978SThomas Graf 5391da177e4SLinus Torvalds ifa->ifa_prefixlen = ifm->ifa_prefixlen; 5401da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); 5411da177e4SLinus Torvalds ifa->ifa_flags = ifm->ifa_flags; 5421da177e4SLinus Torvalds ifa->ifa_scope = ifm->ifa_scope; 5431da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 5445c753978SThomas Graf 545a7a628c4SAl Viro ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); 546a7a628c4SAl Viro ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); 5475c753978SThomas Graf 5485c753978SThomas Graf if (tb[IFA_BROADCAST]) 549a7a628c4SAl Viro ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); 5505c753978SThomas Graf 5515c753978SThomas Graf if (tb[IFA_ANYCAST]) 552a7a628c4SAl Viro ifa->ifa_anycast = nla_get_be32(tb[IFA_ANYCAST]); 5535c753978SThomas Graf 5545c753978SThomas Graf if (tb[IFA_LABEL]) 5555c753978SThomas Graf nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); 5561da177e4SLinus Torvalds else 5571da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 5581da177e4SLinus Torvalds 5595c753978SThomas Graf return ifa; 5605c753978SThomas Graf 5615c753978SThomas Graf errout: 5625c753978SThomas Graf return ERR_PTR(err); 5635c753978SThomas Graf } 5645c753978SThomas Graf 5655c753978SThomas Graf static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 5665c753978SThomas Graf { 567b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 5685c753978SThomas Graf struct in_ifaddr *ifa; 5695c753978SThomas Graf 5705c753978SThomas Graf ASSERT_RTNL(); 5715c753978SThomas Graf 572b854272bSDenis V. Lunev if (net != &init_net) 573b854272bSDenis V. Lunev return -EINVAL; 574b854272bSDenis V. Lunev 5755c753978SThomas Graf ifa = rtm_to_ifaddr(nlh); 5765c753978SThomas Graf if (IS_ERR(ifa)) 5775c753978SThomas Graf return PTR_ERR(ifa); 5785c753978SThomas Graf 579d6062cbbSThomas Graf return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * Determine a default network mask, based on the IP address. 5841da177e4SLinus Torvalds */ 5851da177e4SLinus Torvalds 586714e85beSAl Viro static __inline__ int inet_abc_len(__be32 addr) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds int rc = -1; /* Something else, probably a multicast. */ 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (ZERONET(addr)) 5911da177e4SLinus Torvalds rc = 0; 5921da177e4SLinus Torvalds else { 593714e85beSAl Viro __u32 haddr = ntohl(addr); 5941da177e4SLinus Torvalds 595714e85beSAl Viro if (IN_CLASSA(haddr)) 5961da177e4SLinus Torvalds rc = 8; 597714e85beSAl Viro else if (IN_CLASSB(haddr)) 5981da177e4SLinus Torvalds rc = 16; 599714e85beSAl Viro else if (IN_CLASSC(haddr)) 6001da177e4SLinus Torvalds rc = 24; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds return rc; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds int devinet_ioctl(unsigned int cmd, void __user *arg) 6081da177e4SLinus Torvalds { 6091da177e4SLinus Torvalds struct ifreq ifr; 6101da177e4SLinus Torvalds struct sockaddr_in sin_orig; 6111da177e4SLinus Torvalds struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; 6121da177e4SLinus Torvalds struct in_device *in_dev; 6131da177e4SLinus Torvalds struct in_ifaddr **ifap = NULL; 6141da177e4SLinus Torvalds struct in_ifaddr *ifa = NULL; 6151da177e4SLinus Torvalds struct net_device *dev; 6161da177e4SLinus Torvalds char *colon; 6171da177e4SLinus Torvalds int ret = -EFAULT; 6181da177e4SLinus Torvalds int tryaddrmatch = 0; 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* 6211da177e4SLinus Torvalds * Fetch the caller's info block into kernel space 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) 6251da177e4SLinus Torvalds goto out; 6261da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ - 1] = 0; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /* save original address for comparison */ 6291da177e4SLinus Torvalds memcpy(&sin_orig, sin, sizeof(*sin)); 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds colon = strchr(ifr.ifr_name, ':'); 6321da177e4SLinus Torvalds if (colon) 6331da177e4SLinus Torvalds *colon = 0; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds #ifdef CONFIG_KMOD 636881d966bSEric W. Biederman dev_load(&init_net, ifr.ifr_name); 6371da177e4SLinus Torvalds #endif 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds switch (cmd) { 6401da177e4SLinus Torvalds case SIOCGIFADDR: /* Get interface address */ 6411da177e4SLinus Torvalds case SIOCGIFBRDADDR: /* Get the broadcast address */ 6421da177e4SLinus Torvalds case SIOCGIFDSTADDR: /* Get the destination address */ 6431da177e4SLinus Torvalds case SIOCGIFNETMASK: /* Get the netmask for the interface */ 6441da177e4SLinus Torvalds /* Note that these ioctls will not sleep, 6451da177e4SLinus Torvalds so that we do not impose a lock. 6461da177e4SLinus Torvalds One day we will be forced to put shlock here (I mean SMP) 6471da177e4SLinus Torvalds */ 6481da177e4SLinus Torvalds tryaddrmatch = (sin_orig.sin_family == AF_INET); 6491da177e4SLinus Torvalds memset(sin, 0, sizeof(*sin)); 6501da177e4SLinus Torvalds sin->sin_family = AF_INET; 6511da177e4SLinus Torvalds break; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds case SIOCSIFFLAGS: 6541da177e4SLinus Torvalds ret = -EACCES; 6551da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6561da177e4SLinus Torvalds goto out; 6571da177e4SLinus Torvalds break; 6581da177e4SLinus Torvalds case SIOCSIFADDR: /* Set interface address (and family) */ 6591da177e4SLinus Torvalds case SIOCSIFBRDADDR: /* Set the broadcast address */ 6601da177e4SLinus Torvalds case SIOCSIFDSTADDR: /* Set the destination address */ 6611da177e4SLinus Torvalds case SIOCSIFNETMASK: /* Set the netmask for the interface */ 6621da177e4SLinus Torvalds ret = -EACCES; 6631da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6641da177e4SLinus Torvalds goto out; 6651da177e4SLinus Torvalds ret = -EINVAL; 6661da177e4SLinus Torvalds if (sin->sin_family != AF_INET) 6671da177e4SLinus Torvalds goto out; 6681da177e4SLinus Torvalds break; 6691da177e4SLinus Torvalds default: 6701da177e4SLinus Torvalds ret = -EINVAL; 6711da177e4SLinus Torvalds goto out; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds rtnl_lock(); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds ret = -ENODEV; 677881d966bSEric W. Biederman if ((dev = __dev_get_by_name(&init_net, ifr.ifr_name)) == NULL) 6781da177e4SLinus Torvalds goto done; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds if (colon) 6811da177e4SLinus Torvalds *colon = ':'; 6821da177e4SLinus Torvalds 683e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { 6841da177e4SLinus Torvalds if (tryaddrmatch) { 6851da177e4SLinus Torvalds /* Matthias Andree */ 6861da177e4SLinus Torvalds /* compare label and address (4.4BSD style) */ 6871da177e4SLinus Torvalds /* note: we only do this for a limited set of ioctls 6881da177e4SLinus Torvalds and only if the original address family was AF_INET. 6891da177e4SLinus Torvalds This is checked above. */ 6901da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 6911da177e4SLinus Torvalds ifap = &ifa->ifa_next) { 6921da177e4SLinus Torvalds if (!strcmp(ifr.ifr_name, ifa->ifa_label) && 6931da177e4SLinus Torvalds sin_orig.sin_addr.s_addr == 6941da177e4SLinus Torvalds ifa->ifa_address) { 6951da177e4SLinus Torvalds break; /* found */ 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds /* we didn't get a match, maybe the application is 7001da177e4SLinus Torvalds 4.3BSD-style and passed in junk so we fall back to 7011da177e4SLinus Torvalds comparing just the label */ 7021da177e4SLinus Torvalds if (!ifa) { 7031da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 7041da177e4SLinus Torvalds ifap = &ifa->ifa_next) 7051da177e4SLinus Torvalds if (!strcmp(ifr.ifr_name, ifa->ifa_label)) 7061da177e4SLinus Torvalds break; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 7111da177e4SLinus Torvalds if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) 7121da177e4SLinus Torvalds goto done; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds switch (cmd) { 7151da177e4SLinus Torvalds case SIOCGIFADDR: /* Get interface address */ 7161da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_local; 7171da177e4SLinus Torvalds goto rarok; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds case SIOCGIFBRDADDR: /* Get the broadcast address */ 7201da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_broadcast; 7211da177e4SLinus Torvalds goto rarok; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds case SIOCGIFDSTADDR: /* Get the destination address */ 7241da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_address; 7251da177e4SLinus Torvalds goto rarok; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds case SIOCGIFNETMASK: /* Get the netmask for the interface */ 7281da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_mask; 7291da177e4SLinus Torvalds goto rarok; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds case SIOCSIFFLAGS: 7321da177e4SLinus Torvalds if (colon) { 7331da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 7341da177e4SLinus Torvalds if (!ifa) 7351da177e4SLinus Torvalds break; 7361da177e4SLinus Torvalds ret = 0; 7371da177e4SLinus Torvalds if (!(ifr.ifr_flags & IFF_UP)) 7381da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 1); 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds ret = dev_change_flags(dev, ifr.ifr_flags); 7421da177e4SLinus Torvalds break; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds case SIOCSIFADDR: /* Set interface address (and family) */ 7451da177e4SLinus Torvalds ret = -EINVAL; 7461da177e4SLinus Torvalds if (inet_abc_len(sin->sin_addr.s_addr) < 0) 7471da177e4SLinus Torvalds break; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds if (!ifa) { 7501da177e4SLinus Torvalds ret = -ENOBUFS; 7511da177e4SLinus Torvalds if ((ifa = inet_alloc_ifa()) == NULL) 7521da177e4SLinus Torvalds break; 7531da177e4SLinus Torvalds if (colon) 7541da177e4SLinus Torvalds memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ); 7551da177e4SLinus Torvalds else 7561da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 7571da177e4SLinus Torvalds } else { 7581da177e4SLinus Torvalds ret = 0; 7591da177e4SLinus Torvalds if (ifa->ifa_local == sin->sin_addr.s_addr) 7601da177e4SLinus Torvalds break; 7611da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 7621da177e4SLinus Torvalds ifa->ifa_broadcast = 0; 7631da177e4SLinus Torvalds ifa->ifa_anycast = 0; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds if (!(dev->flags & IFF_POINTOPOINT)) { 7691da177e4SLinus Torvalds ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); 7701da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); 7711da177e4SLinus Torvalds if ((dev->flags & IFF_BROADCAST) && 7721da177e4SLinus Torvalds ifa->ifa_prefixlen < 31) 7731da177e4SLinus Torvalds ifa->ifa_broadcast = ifa->ifa_address | 7741da177e4SLinus Torvalds ~ifa->ifa_mask; 7751da177e4SLinus Torvalds } else { 7761da177e4SLinus Torvalds ifa->ifa_prefixlen = 32; 7771da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(32); 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds ret = inet_set_ifa(dev, ifa); 7801da177e4SLinus Torvalds break; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds case SIOCSIFBRDADDR: /* Set the broadcast address */ 7831da177e4SLinus Torvalds ret = 0; 7841da177e4SLinus Torvalds if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { 7851da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 7861da177e4SLinus Torvalds ifa->ifa_broadcast = sin->sin_addr.s_addr; 7871da177e4SLinus Torvalds inet_insert_ifa(ifa); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds break; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds case SIOCSIFDSTADDR: /* Set the destination address */ 7921da177e4SLinus Torvalds ret = 0; 7931da177e4SLinus Torvalds if (ifa->ifa_address == sin->sin_addr.s_addr) 7941da177e4SLinus Torvalds break; 7951da177e4SLinus Torvalds ret = -EINVAL; 7961da177e4SLinus Torvalds if (inet_abc_len(sin->sin_addr.s_addr) < 0) 7971da177e4SLinus Torvalds break; 7981da177e4SLinus Torvalds ret = 0; 7991da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 8001da177e4SLinus Torvalds ifa->ifa_address = sin->sin_addr.s_addr; 8011da177e4SLinus Torvalds inet_insert_ifa(ifa); 8021da177e4SLinus Torvalds break; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds case SIOCSIFNETMASK: /* Set the netmask for the interface */ 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* 8071da177e4SLinus Torvalds * The mask we set must be legal. 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds ret = -EINVAL; 8101da177e4SLinus Torvalds if (bad_mask(sin->sin_addr.s_addr, 0)) 8111da177e4SLinus Torvalds break; 8121da177e4SLinus Torvalds ret = 0; 8131da177e4SLinus Torvalds if (ifa->ifa_mask != sin->sin_addr.s_addr) { 814a144ea4bSAl Viro __be32 old_mask = ifa->ifa_mask; 8151da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 8161da177e4SLinus Torvalds ifa->ifa_mask = sin->sin_addr.s_addr; 8171da177e4SLinus Torvalds ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds /* See if current broadcast address matches 8201da177e4SLinus Torvalds * with current netmask, then recalculate 8211da177e4SLinus Torvalds * the broadcast address. Otherwise it's a 8221da177e4SLinus Torvalds * funny address, so don't touch it since 8231da177e4SLinus Torvalds * the user seems to know what (s)he's doing... 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds if ((dev->flags & IFF_BROADCAST) && 8261da177e4SLinus Torvalds (ifa->ifa_prefixlen < 31) && 8271da177e4SLinus Torvalds (ifa->ifa_broadcast == 828dcab5e1eSDavid Engel (ifa->ifa_local|~old_mask))) { 8291da177e4SLinus Torvalds ifa->ifa_broadcast = (ifa->ifa_local | 8301da177e4SLinus Torvalds ~sin->sin_addr.s_addr); 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds inet_insert_ifa(ifa); 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds break; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds done: 8371da177e4SLinus Torvalds rtnl_unlock(); 8381da177e4SLinus Torvalds out: 8391da177e4SLinus Torvalds return ret; 8401da177e4SLinus Torvalds rarok: 8411da177e4SLinus Torvalds rtnl_unlock(); 8421da177e4SLinus Torvalds ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0; 8431da177e4SLinus Torvalds goto out; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds static int inet_gifconf(struct net_device *dev, char __user *buf, int len) 8471da177e4SLinus Torvalds { 848e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 8491da177e4SLinus Torvalds struct in_ifaddr *ifa; 8501da177e4SLinus Torvalds struct ifreq ifr; 8511da177e4SLinus Torvalds int done = 0; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds if (!in_dev || (ifa = in_dev->ifa_list) == NULL) 8541da177e4SLinus Torvalds goto out; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds for (; ifa; ifa = ifa->ifa_next) { 8571da177e4SLinus Torvalds if (!buf) { 8581da177e4SLinus Torvalds done += sizeof(ifr); 8591da177e4SLinus Torvalds continue; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds if (len < (int) sizeof(ifr)) 8621da177e4SLinus Torvalds break; 8631da177e4SLinus Torvalds memset(&ifr, 0, sizeof(struct ifreq)); 8641da177e4SLinus Torvalds if (ifa->ifa_label) 8651da177e4SLinus Torvalds strcpy(ifr.ifr_name, ifa->ifa_label); 8661da177e4SLinus Torvalds else 8671da177e4SLinus Torvalds strcpy(ifr.ifr_name, dev->name); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; 8701da177e4SLinus Torvalds (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = 8711da177e4SLinus Torvalds ifa->ifa_local; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) { 8741da177e4SLinus Torvalds done = -EFAULT; 8751da177e4SLinus Torvalds break; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds buf += sizeof(struct ifreq); 8781da177e4SLinus Torvalds len -= sizeof(struct ifreq); 8791da177e4SLinus Torvalds done += sizeof(struct ifreq); 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds out: 8821da177e4SLinus Torvalds return done; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 885a61ced5dSAl Viro __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) 8861da177e4SLinus Torvalds { 887a61ced5dSAl Viro __be32 addr = 0; 8881da177e4SLinus Torvalds struct in_device *in_dev; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds rcu_read_lock(); 891e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 8921da177e4SLinus Torvalds if (!in_dev) 8931da177e4SLinus Torvalds goto no_in_dev; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds for_primary_ifa(in_dev) { 8961da177e4SLinus Torvalds if (ifa->ifa_scope > scope) 8971da177e4SLinus Torvalds continue; 8981da177e4SLinus Torvalds if (!dst || inet_ifa_match(dst, ifa)) { 8991da177e4SLinus Torvalds addr = ifa->ifa_local; 9001da177e4SLinus Torvalds break; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds if (!addr) 9031da177e4SLinus Torvalds addr = ifa->ifa_local; 9041da177e4SLinus Torvalds } endfor_ifa(in_dev); 9051da177e4SLinus Torvalds no_in_dev: 9061da177e4SLinus Torvalds rcu_read_unlock(); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds if (addr) 9091da177e4SLinus Torvalds goto out; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds /* Not loopback addresses on loopback should be preferred 9121da177e4SLinus Torvalds in this case. It is importnat that lo is the first interface 9131da177e4SLinus Torvalds in dev_base list. 9141da177e4SLinus Torvalds */ 9151da177e4SLinus Torvalds read_lock(&dev_base_lock); 9161da177e4SLinus Torvalds rcu_read_lock(); 917881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 918e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) 9191da177e4SLinus Torvalds continue; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds for_primary_ifa(in_dev) { 9221da177e4SLinus Torvalds if (ifa->ifa_scope != RT_SCOPE_LINK && 9231da177e4SLinus Torvalds ifa->ifa_scope <= scope) { 9241da177e4SLinus Torvalds addr = ifa->ifa_local; 9251da177e4SLinus Torvalds goto out_unlock_both; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds } endfor_ifa(in_dev); 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds out_unlock_both: 9301da177e4SLinus Torvalds read_unlock(&dev_base_lock); 9311da177e4SLinus Torvalds rcu_read_unlock(); 9321da177e4SLinus Torvalds out: 9331da177e4SLinus Torvalds return addr; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 93660cad5daSAl Viro static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, 93760cad5daSAl Viro __be32 local, int scope) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds int same = 0; 940a144ea4bSAl Viro __be32 addr = 0; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds for_ifa(in_dev) { 9431da177e4SLinus Torvalds if (!addr && 9441da177e4SLinus Torvalds (local == ifa->ifa_local || !local) && 9451da177e4SLinus Torvalds ifa->ifa_scope <= scope) { 9461da177e4SLinus Torvalds addr = ifa->ifa_local; 9471da177e4SLinus Torvalds if (same) 9481da177e4SLinus Torvalds break; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds if (!same) { 9511da177e4SLinus Torvalds same = (!local || inet_ifa_match(local, ifa)) && 9521da177e4SLinus Torvalds (!dst || inet_ifa_match(dst, ifa)); 9531da177e4SLinus Torvalds if (same && addr) { 9541da177e4SLinus Torvalds if (local || !dst) 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds /* Is the selected addr into dst subnet? */ 9571da177e4SLinus Torvalds if (inet_ifa_match(addr, ifa)) 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds /* No, then can we use new local src? */ 9601da177e4SLinus Torvalds if (ifa->ifa_scope <= scope) { 9611da177e4SLinus Torvalds addr = ifa->ifa_local; 9621da177e4SLinus Torvalds break; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds /* search for large dst subnet for addr */ 9651da177e4SLinus Torvalds same = 0; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds } endfor_ifa(in_dev); 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds return same? addr : 0; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* 9741da177e4SLinus Torvalds * Confirm that local IP address exists using wildcards: 9751da177e4SLinus Torvalds * - dev: only on this interface, 0=any interface 9761da177e4SLinus Torvalds * - dst: only in the same subnet as dst, 0=any dst 9771da177e4SLinus Torvalds * - local: address, 0=autoselect the local address 9781da177e4SLinus Torvalds * - scope: maximum allowed scope value for the local address 9791da177e4SLinus Torvalds */ 98060cad5daSAl Viro __be32 inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope) 9811da177e4SLinus Torvalds { 98260cad5daSAl Viro __be32 addr = 0; 9831da177e4SLinus Torvalds struct in_device *in_dev; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds if (dev) { 9861da177e4SLinus Torvalds rcu_read_lock(); 987e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev))) 9881da177e4SLinus Torvalds addr = confirm_addr_indev(in_dev, dst, local, scope); 9891da177e4SLinus Torvalds rcu_read_unlock(); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds return addr; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds read_lock(&dev_base_lock); 9951da177e4SLinus Torvalds rcu_read_lock(); 996881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 997e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev))) { 9981da177e4SLinus Torvalds addr = confirm_addr_indev(in_dev, dst, local, scope); 9991da177e4SLinus Torvalds if (addr) 10001da177e4SLinus Torvalds break; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds rcu_read_unlock(); 10041da177e4SLinus Torvalds read_unlock(&dev_base_lock); 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds return addr; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * Device notifier 10111da177e4SLinus Torvalds */ 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds int register_inetaddr_notifier(struct notifier_block *nb) 10141da177e4SLinus Torvalds { 1015e041c683SAlan Stern return blocking_notifier_chain_register(&inetaddr_chain, nb); 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds int unregister_inetaddr_notifier(struct notifier_block *nb) 10191da177e4SLinus Torvalds { 1020e041c683SAlan Stern return blocking_notifier_chain_unregister(&inetaddr_chain, nb); 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds /* Rename ifa_labels for a device name change. Make some effort to preserve existing 10241da177e4SLinus Torvalds * alias numbering and to create unique labels if possible. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) 10271da177e4SLinus Torvalds { 10281da177e4SLinus Torvalds struct in_ifaddr *ifa; 10291da177e4SLinus Torvalds int named = 0; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 10321da177e4SLinus Torvalds char old[IFNAMSIZ], *dot; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds memcpy(old, ifa->ifa_label, IFNAMSIZ); 10351da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 10361da177e4SLinus Torvalds if (named++ == 0) 10371da177e4SLinus Torvalds continue; 103844344b2aSMark McLoughlin dot = strchr(old, ':'); 10391da177e4SLinus Torvalds if (dot == NULL) { 10401da177e4SLinus Torvalds sprintf(old, ":%d", named); 10411da177e4SLinus Torvalds dot = old; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) { 10441da177e4SLinus Torvalds strcat(ifa->ifa_label, dot); 10451da177e4SLinus Torvalds } else { 10461da177e4SLinus Torvalds strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* Called only under RTNL semaphore */ 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds static int inetdev_event(struct notifier_block *this, unsigned long event, 10541da177e4SLinus Torvalds void *ptr) 10551da177e4SLinus Torvalds { 10561da177e4SLinus Torvalds struct net_device *dev = ptr; 1057e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 10581da177e4SLinus Torvalds 1059e9dc8653SEric W. Biederman if (dev->nd_net != &init_net) 1060e9dc8653SEric W. Biederman return NOTIFY_DONE; 1061e9dc8653SEric W. Biederman 10621da177e4SLinus Torvalds ASSERT_RTNL(); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds if (!in_dev) { 10658030f544SHerbert Xu if (event == NETDEV_REGISTER) { 10661da177e4SLinus Torvalds in_dev = inetdev_init(dev); 10678d76527eSHerbert Xu if (!in_dev) 1068b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 10690cc217e1SEric W. Biederman if (dev->flags & IFF_LOOPBACK) { 107042f811b8SHerbert Xu IN_DEV_CONF_SET(in_dev, NOXFRM, 1); 107142f811b8SHerbert Xu IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); 10721da177e4SLinus Torvalds } 10738030f544SHerbert Xu } 10741da177e4SLinus Torvalds goto out; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds switch (event) { 10781da177e4SLinus Torvalds case NETDEV_REGISTER: 10791da177e4SLinus Torvalds printk(KERN_DEBUG "inetdev_event: bug\n"); 10801da177e4SLinus Torvalds dev->ip_ptr = NULL; 10811da177e4SLinus Torvalds break; 10821da177e4SLinus Torvalds case NETDEV_UP: 10831da177e4SLinus Torvalds if (dev->mtu < 68) 10841da177e4SLinus Torvalds break; 10850cc217e1SEric W. Biederman if (dev->flags & IFF_LOOPBACK) { 10861da177e4SLinus Torvalds struct in_ifaddr *ifa; 10871da177e4SLinus Torvalds if ((ifa = inet_alloc_ifa()) != NULL) { 10881da177e4SLinus Torvalds ifa->ifa_local = 10891da177e4SLinus Torvalds ifa->ifa_address = htonl(INADDR_LOOPBACK); 10901da177e4SLinus Torvalds ifa->ifa_prefixlen = 8; 10911da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(8); 10921da177e4SLinus Torvalds in_dev_hold(in_dev); 10931da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 10941da177e4SLinus Torvalds ifa->ifa_scope = RT_SCOPE_HOST; 10951da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 10961da177e4SLinus Torvalds inet_insert_ifa(ifa); 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds ip_mc_up(in_dev); 11001da177e4SLinus Torvalds break; 11011da177e4SLinus Torvalds case NETDEV_DOWN: 11021da177e4SLinus Torvalds ip_mc_down(in_dev); 11031da177e4SLinus Torvalds break; 11041da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 11051da177e4SLinus Torvalds if (dev->mtu >= 68) 11061da177e4SLinus Torvalds break; 11071da177e4SLinus Torvalds /* MTU falled under 68, disable IP */ 11081da177e4SLinus Torvalds case NETDEV_UNREGISTER: 11091da177e4SLinus Torvalds inetdev_destroy(in_dev); 11101da177e4SLinus Torvalds break; 11111da177e4SLinus Torvalds case NETDEV_CHANGENAME: 11121da177e4SLinus Torvalds /* Do not notify about label change, this event is 11131da177e4SLinus Torvalds * not interesting to applications using netlink. 11141da177e4SLinus Torvalds */ 11151da177e4SLinus Torvalds inetdev_changename(dev, in_dev); 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 11181da177e4SLinus Torvalds devinet_sysctl_unregister(&in_dev->cnf); 11191da177e4SLinus Torvalds neigh_sysctl_unregister(in_dev->arp_parms); 11201da177e4SLinus Torvalds neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4, 11211da177e4SLinus Torvalds NET_IPV4_NEIGH, "ipv4", NULL, NULL); 11221da177e4SLinus Torvalds devinet_sysctl_register(in_dev, &in_dev->cnf); 11231da177e4SLinus Torvalds #endif 11241da177e4SLinus Torvalds break; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds out: 11271da177e4SLinus Torvalds return NOTIFY_DONE; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds static struct notifier_block ip_netdev_notifier = { 11311da177e4SLinus Torvalds .notifier_call =inetdev_event, 11321da177e4SLinus Torvalds }; 11331da177e4SLinus Torvalds 1134339bf98fSThomas Graf static inline size_t inet_nlmsg_size(void) 1135339bf98fSThomas Graf { 1136339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 1137339bf98fSThomas Graf + nla_total_size(4) /* IFA_ADDRESS */ 1138339bf98fSThomas Graf + nla_total_size(4) /* IFA_LOCAL */ 1139339bf98fSThomas Graf + nla_total_size(4) /* IFA_BROADCAST */ 1140339bf98fSThomas Graf + nla_total_size(4) /* IFA_ANYCAST */ 1141339bf98fSThomas Graf + nla_total_size(IFNAMSIZ); /* IFA_LABEL */ 1142339bf98fSThomas Graf } 1143339bf98fSThomas Graf 11441da177e4SLinus Torvalds static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, 1145b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 11461da177e4SLinus Torvalds { 11471da177e4SLinus Torvalds struct ifaddrmsg *ifm; 11481da177e4SLinus Torvalds struct nlmsghdr *nlh; 11491da177e4SLinus Torvalds 115047f68512SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags); 115147f68512SThomas Graf if (nlh == NULL) 115226932566SPatrick McHardy return -EMSGSIZE; 115347f68512SThomas Graf 115447f68512SThomas Graf ifm = nlmsg_data(nlh); 11551da177e4SLinus Torvalds ifm->ifa_family = AF_INET; 11561da177e4SLinus Torvalds ifm->ifa_prefixlen = ifa->ifa_prefixlen; 11571da177e4SLinus Torvalds ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT; 11581da177e4SLinus Torvalds ifm->ifa_scope = ifa->ifa_scope; 11591da177e4SLinus Torvalds ifm->ifa_index = ifa->ifa_dev->dev->ifindex; 11601da177e4SLinus Torvalds 116147f68512SThomas Graf if (ifa->ifa_address) 1162a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address); 116347f68512SThomas Graf 116447f68512SThomas Graf if (ifa->ifa_local) 1165a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local); 116647f68512SThomas Graf 116747f68512SThomas Graf if (ifa->ifa_broadcast) 1168a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast); 116947f68512SThomas Graf 117047f68512SThomas Graf if (ifa->ifa_anycast) 1171a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_ANYCAST, ifa->ifa_anycast); 117247f68512SThomas Graf 117347f68512SThomas Graf if (ifa->ifa_label[0]) 117447f68512SThomas Graf NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label); 117547f68512SThomas Graf 117647f68512SThomas Graf return nlmsg_end(skb, nlh); 117747f68512SThomas Graf 117847f68512SThomas Graf nla_put_failure: 117926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 118026932566SPatrick McHardy return -EMSGSIZE; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 11841da177e4SLinus Torvalds { 1185b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 11861da177e4SLinus Torvalds int idx, ip_idx; 11871da177e4SLinus Torvalds struct net_device *dev; 11881da177e4SLinus Torvalds struct in_device *in_dev; 11891da177e4SLinus Torvalds struct in_ifaddr *ifa; 11901da177e4SLinus Torvalds int s_ip_idx, s_idx = cb->args[0]; 11911da177e4SLinus Torvalds 1192b854272bSDenis V. Lunev if (net != &init_net) 1193b854272bSDenis V. Lunev return 0; 1194b854272bSDenis V. Lunev 11951da177e4SLinus Torvalds s_ip_idx = ip_idx = cb->args[1]; 11967562f876SPavel Emelianov idx = 0; 1197881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 11981da177e4SLinus Torvalds if (idx < s_idx) 11997562f876SPavel Emelianov goto cont; 12001da177e4SLinus Torvalds if (idx > s_idx) 12011da177e4SLinus Torvalds s_ip_idx = 0; 12026313c1e0SPatrick McHardy if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) 12037562f876SPavel Emelianov goto cont; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds for (ifa = in_dev->ifa_list, ip_idx = 0; ifa; 12061da177e4SLinus Torvalds ifa = ifa->ifa_next, ip_idx++) { 12071da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 1208596e4150SStephen Hemminger continue; 12091da177e4SLinus Torvalds if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid, 12101da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 12116313c1e0SPatrick McHardy RTM_NEWADDR, NLM_F_MULTI) <= 0) 12121da177e4SLinus Torvalds goto done; 12131da177e4SLinus Torvalds } 12147562f876SPavel Emelianov cont: 12157562f876SPavel Emelianov idx++; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds done: 12191da177e4SLinus Torvalds cb->args[0] = idx; 12201da177e4SLinus Torvalds cb->args[1] = ip_idx; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds return skb->len; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 1225d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr* ifa, struct nlmsghdr *nlh, 1226d6062cbbSThomas Graf u32 pid) 12271da177e4SLinus Torvalds { 122847f68512SThomas Graf struct sk_buff *skb; 1229d6062cbbSThomas Graf u32 seq = nlh ? nlh->nlmsg_seq : 0; 1230d6062cbbSThomas Graf int err = -ENOBUFS; 12311da177e4SLinus Torvalds 1232339bf98fSThomas Graf skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); 123347f68512SThomas Graf if (skb == NULL) 1234d6062cbbSThomas Graf goto errout; 1235d6062cbbSThomas Graf 1236d6062cbbSThomas Graf err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0); 123726932566SPatrick McHardy if (err < 0) { 123826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ 123926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 124026932566SPatrick McHardy kfree_skb(skb); 124126932566SPatrick McHardy goto errout; 124226932566SPatrick McHardy } 124397c53cacSDenis V. Lunev err = rtnl_notify(skb, &init_net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); 1244d6062cbbSThomas Graf errout: 1245d6062cbbSThomas Graf if (err < 0) 124697c53cacSDenis V. Lunev rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_IFADDR, err); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 12501da177e4SLinus Torvalds 125131be3085SHerbert Xu static void devinet_copy_dflt_conf(int i) 125231be3085SHerbert Xu { 125331be3085SHerbert Xu struct net_device *dev; 125431be3085SHerbert Xu 125531be3085SHerbert Xu read_lock(&dev_base_lock); 1256881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 125731be3085SHerbert Xu struct in_device *in_dev; 125831be3085SHerbert Xu rcu_read_lock(); 125931be3085SHerbert Xu in_dev = __in_dev_get_rcu(dev); 126031be3085SHerbert Xu if (in_dev && !test_bit(i, in_dev->cnf.state)) 126131be3085SHerbert Xu in_dev->cnf.data[i] = ipv4_devconf_dflt.data[i]; 126231be3085SHerbert Xu rcu_read_unlock(); 126331be3085SHerbert Xu } 126431be3085SHerbert Xu read_unlock(&dev_base_lock); 126531be3085SHerbert Xu } 126631be3085SHerbert Xu 126731be3085SHerbert Xu static int devinet_conf_proc(ctl_table *ctl, int write, 126831be3085SHerbert Xu struct file* filp, void __user *buffer, 126931be3085SHerbert Xu size_t *lenp, loff_t *ppos) 127031be3085SHerbert Xu { 127131be3085SHerbert Xu int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 127231be3085SHerbert Xu 127331be3085SHerbert Xu if (write) { 127431be3085SHerbert Xu struct ipv4_devconf *cnf = ctl->extra1; 127531be3085SHerbert Xu int i = (int *)ctl->data - cnf->data; 127631be3085SHerbert Xu 127731be3085SHerbert Xu set_bit(i, cnf->state); 127831be3085SHerbert Xu 127931be3085SHerbert Xu if (cnf == &ipv4_devconf_dflt) 128031be3085SHerbert Xu devinet_copy_dflt_conf(i); 128131be3085SHerbert Xu } 128231be3085SHerbert Xu 128331be3085SHerbert Xu return ret; 128431be3085SHerbert Xu } 128531be3085SHerbert Xu 128631be3085SHerbert Xu static int devinet_conf_sysctl(ctl_table *table, int __user *name, int nlen, 128731be3085SHerbert Xu void __user *oldval, size_t __user *oldlenp, 128831be3085SHerbert Xu void __user *newval, size_t newlen) 128931be3085SHerbert Xu { 129031be3085SHerbert Xu struct ipv4_devconf *cnf; 129131be3085SHerbert Xu int *valp = table->data; 129231be3085SHerbert Xu int new; 129331be3085SHerbert Xu int i; 129431be3085SHerbert Xu 129531be3085SHerbert Xu if (!newval || !newlen) 129631be3085SHerbert Xu return 0; 129731be3085SHerbert Xu 129831be3085SHerbert Xu if (newlen != sizeof(int)) 129931be3085SHerbert Xu return -EINVAL; 130031be3085SHerbert Xu 130131be3085SHerbert Xu if (get_user(new, (int __user *)newval)) 130231be3085SHerbert Xu return -EFAULT; 130331be3085SHerbert Xu 130431be3085SHerbert Xu if (new == *valp) 130531be3085SHerbert Xu return 0; 130631be3085SHerbert Xu 130731be3085SHerbert Xu if (oldval && oldlenp) { 130831be3085SHerbert Xu size_t len; 130931be3085SHerbert Xu 131031be3085SHerbert Xu if (get_user(len, oldlenp)) 131131be3085SHerbert Xu return -EFAULT; 131231be3085SHerbert Xu 131331be3085SHerbert Xu if (len) { 131431be3085SHerbert Xu if (len > table->maxlen) 131531be3085SHerbert Xu len = table->maxlen; 131631be3085SHerbert Xu if (copy_to_user(oldval, valp, len)) 131731be3085SHerbert Xu return -EFAULT; 131831be3085SHerbert Xu if (put_user(len, oldlenp)) 131931be3085SHerbert Xu return -EFAULT; 132031be3085SHerbert Xu } 132131be3085SHerbert Xu } 132231be3085SHerbert Xu 132331be3085SHerbert Xu *valp = new; 132431be3085SHerbert Xu 132531be3085SHerbert Xu cnf = table->extra1; 132631be3085SHerbert Xu i = (int *)table->data - cnf->data; 132731be3085SHerbert Xu 132831be3085SHerbert Xu set_bit(i, cnf->state); 132931be3085SHerbert Xu 133031be3085SHerbert Xu if (cnf == &ipv4_devconf_dflt) 133131be3085SHerbert Xu devinet_copy_dflt_conf(i); 133231be3085SHerbert Xu 133331be3085SHerbert Xu return 1; 133431be3085SHerbert Xu } 133531be3085SHerbert Xu 13361da177e4SLinus Torvalds void inet_forward_change(void) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds struct net_device *dev; 133942f811b8SHerbert Xu int on = IPV4_DEVCONF_ALL(FORWARDING); 13401da177e4SLinus Torvalds 134142f811b8SHerbert Xu IPV4_DEVCONF_ALL(ACCEPT_REDIRECTS) = !on; 134242f811b8SHerbert Xu IPV4_DEVCONF_DFLT(FORWARDING) = on; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds read_lock(&dev_base_lock); 1345881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 13461da177e4SLinus Torvalds struct in_device *in_dev; 13471da177e4SLinus Torvalds rcu_read_lock(); 1348e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 13491da177e4SLinus Torvalds if (in_dev) 135042f811b8SHerbert Xu IN_DEV_CONF_SET(in_dev, FORWARDING, on); 13511da177e4SLinus Torvalds rcu_read_unlock(); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds read_unlock(&dev_base_lock); 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds rt_cache_flush(0); 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds static int devinet_sysctl_forward(ctl_table *ctl, int write, 13591da177e4SLinus Torvalds struct file* filp, void __user *buffer, 13601da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 13611da177e4SLinus Torvalds { 13621da177e4SLinus Torvalds int *valp = ctl->data; 13631da177e4SLinus Torvalds int val = *valp; 13641da177e4SLinus Torvalds int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds if (write && *valp != val) { 136742f811b8SHerbert Xu if (valp == &IPV4_DEVCONF_ALL(FORWARDING)) 13681da177e4SLinus Torvalds inet_forward_change(); 136942f811b8SHerbert Xu else if (valp != &IPV4_DEVCONF_DFLT(FORWARDING)) 13701da177e4SLinus Torvalds rt_cache_flush(0); 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds return ret; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds int ipv4_doint_and_flush(ctl_table *ctl, int write, 13771da177e4SLinus Torvalds struct file* filp, void __user *buffer, 13781da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 13791da177e4SLinus Torvalds { 13801da177e4SLinus Torvalds int *valp = ctl->data; 13811da177e4SLinus Torvalds int val = *valp; 13821da177e4SLinus Torvalds int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds if (write && *valp != val) 13851da177e4SLinus Torvalds rt_cache_flush(0); 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds return ret; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen, 13911da177e4SLinus Torvalds void __user *oldval, size_t __user *oldlenp, 13921f29bcd7SAlexey Dobriyan void __user *newval, size_t newlen) 13931da177e4SLinus Torvalds { 139431be3085SHerbert Xu int ret = devinet_conf_sysctl(table, name, nlen, oldval, oldlenp, 139531be3085SHerbert Xu newval, newlen); 13961da177e4SLinus Torvalds 139731be3085SHerbert Xu if (ret == 1) 13981da177e4SLinus Torvalds rt_cache_flush(0); 139931be3085SHerbert Xu 140031be3085SHerbert Xu return ret; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds 140442f811b8SHerbert Xu #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \ 140542f811b8SHerbert Xu { \ 140642f811b8SHerbert Xu .ctl_name = NET_IPV4_CONF_ ## attr, \ 140742f811b8SHerbert Xu .procname = name, \ 140842f811b8SHerbert Xu .data = ipv4_devconf.data + \ 140942f811b8SHerbert Xu NET_IPV4_CONF_ ## attr - 1, \ 141042f811b8SHerbert Xu .maxlen = sizeof(int), \ 141142f811b8SHerbert Xu .mode = mval, \ 141242f811b8SHerbert Xu .proc_handler = proc, \ 141342f811b8SHerbert Xu .strategy = sysctl, \ 141431be3085SHerbert Xu .extra1 = &ipv4_devconf, \ 141542f811b8SHerbert Xu } 141642f811b8SHerbert Xu 141742f811b8SHerbert Xu #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ 141831be3085SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \ 141931be3085SHerbert Xu devinet_conf_sysctl) 142042f811b8SHerbert Xu 142142f811b8SHerbert Xu #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ 142231be3085SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \ 142331be3085SHerbert Xu devinet_conf_sysctl) 142442f811b8SHerbert Xu 142542f811b8SHerbert Xu #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \ 142642f811b8SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl) 142742f811b8SHerbert Xu 142842f811b8SHerbert Xu #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ 142942f811b8SHerbert Xu DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \ 143042f811b8SHerbert Xu ipv4_doint_and_flush_strategy) 143142f811b8SHerbert Xu 14321da177e4SLinus Torvalds static struct devinet_sysctl_table { 14331da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 14341da177e4SLinus Torvalds ctl_table devinet_vars[__NET_IPV4_CONF_MAX]; 14351da177e4SLinus Torvalds ctl_table devinet_dev[2]; 14361da177e4SLinus Torvalds ctl_table devinet_conf_dir[2]; 14371da177e4SLinus Torvalds ctl_table devinet_proto_dir[2]; 14381da177e4SLinus Torvalds ctl_table devinet_root_dir[2]; 14391da177e4SLinus Torvalds } devinet_sysctl = { 14401da177e4SLinus Torvalds .devinet_vars = { 144142f811b8SHerbert Xu DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", 144231be3085SHerbert Xu devinet_sysctl_forward, 144331be3085SHerbert Xu devinet_conf_sysctl), 144442f811b8SHerbert Xu DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), 144542f811b8SHerbert Xu 144642f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), 144742f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), 144842f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), 144942f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), 145042f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), 145142f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, 145242f811b8SHerbert Xu "accept_source_route"), 145342f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), 145442f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), 145542f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), 145642f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), 145742f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), 145842f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), 145942f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), 146042f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), 146142f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), 146242f811b8SHerbert Xu 146342f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), 146442f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), 146542f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION, 146642f811b8SHerbert Xu "force_igmp_version"), 146742f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, 146842f811b8SHerbert Xu "promote_secondaries"), 14691da177e4SLinus Torvalds }, 14701da177e4SLinus Torvalds .devinet_dev = { 14711da177e4SLinus Torvalds { 14721da177e4SLinus Torvalds .ctl_name = NET_PROTO_CONF_ALL, 14731da177e4SLinus Torvalds .procname = "all", 14741da177e4SLinus Torvalds .mode = 0555, 14751da177e4SLinus Torvalds .child = devinet_sysctl.devinet_vars, 14761da177e4SLinus Torvalds }, 14771da177e4SLinus Torvalds }, 14781da177e4SLinus Torvalds .devinet_conf_dir = { 14791da177e4SLinus Torvalds { 14801da177e4SLinus Torvalds .ctl_name = NET_IPV4_CONF, 14811da177e4SLinus Torvalds .procname = "conf", 14821da177e4SLinus Torvalds .mode = 0555, 14831da177e4SLinus Torvalds .child = devinet_sysctl.devinet_dev, 14841da177e4SLinus Torvalds }, 14851da177e4SLinus Torvalds }, 14861da177e4SLinus Torvalds .devinet_proto_dir = { 14871da177e4SLinus Torvalds { 14881da177e4SLinus Torvalds .ctl_name = NET_IPV4, 14891da177e4SLinus Torvalds .procname = "ipv4", 14901da177e4SLinus Torvalds .mode = 0555, 14911da177e4SLinus Torvalds .child = devinet_sysctl.devinet_conf_dir, 14921da177e4SLinus Torvalds }, 14931da177e4SLinus Torvalds }, 14941da177e4SLinus Torvalds .devinet_root_dir = { 14951da177e4SLinus Torvalds { 14961da177e4SLinus Torvalds .ctl_name = CTL_NET, 14971da177e4SLinus Torvalds .procname = "net", 14981da177e4SLinus Torvalds .mode = 0555, 14991da177e4SLinus Torvalds .child = devinet_sysctl.devinet_proto_dir, 15001da177e4SLinus Torvalds }, 15011da177e4SLinus Torvalds }, 15021da177e4SLinus Torvalds }; 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds static void devinet_sysctl_register(struct in_device *in_dev, 15051da177e4SLinus Torvalds struct ipv4_devconf *p) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds int i; 15081da177e4SLinus Torvalds struct net_device *dev = in_dev ? in_dev->dev : NULL; 1509*9fa89642SPavel Emelyanov struct devinet_sysctl_table *t; 15101da177e4SLinus Torvalds char *dev_name = NULL; 15111da177e4SLinus Torvalds 1512*9fa89642SPavel Emelyanov t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL); 15131da177e4SLinus Torvalds if (!t) 1514*9fa89642SPavel Emelyanov goto out; 1515*9fa89642SPavel Emelyanov 15161da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) { 15171da177e4SLinus Torvalds t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; 151831be3085SHerbert Xu t->devinet_vars[i].extra1 = p; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds if (dev) { 15221da177e4SLinus Torvalds dev_name = dev->name; 15231da177e4SLinus Torvalds t->devinet_dev[0].ctl_name = dev->ifindex; 15241da177e4SLinus Torvalds } else { 15251da177e4SLinus Torvalds dev_name = "default"; 15261da177e4SLinus Torvalds t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* 15301da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 15311da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 15321da177e4SLinus Torvalds * (see SIOCSIFNAME). 15331da177e4SLinus Torvalds */ 1534543537bdSPaulo Marques dev_name = kstrdup(dev_name, GFP_KERNEL); 15351da177e4SLinus Torvalds if (!dev_name) 15361da177e4SLinus Torvalds goto free; 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds t->devinet_dev[0].procname = dev_name; 15391da177e4SLinus Torvalds t->devinet_dev[0].child = t->devinet_vars; 15401da177e4SLinus Torvalds t->devinet_conf_dir[0].child = t->devinet_dev; 15411da177e4SLinus Torvalds t->devinet_proto_dir[0].child = t->devinet_conf_dir; 15421da177e4SLinus Torvalds t->devinet_root_dir[0].child = t->devinet_proto_dir; 15431da177e4SLinus Torvalds 15440b4d4147SEric W. Biederman t->sysctl_header = register_sysctl_table(t->devinet_root_dir); 15451da177e4SLinus Torvalds if (!t->sysctl_header) 15461da177e4SLinus Torvalds goto free_procname; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds p->sysctl = t; 15491da177e4SLinus Torvalds return; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds free_procname: 15521da177e4SLinus Torvalds kfree(dev_name); 15531da177e4SLinus Torvalds free: 15541da177e4SLinus Torvalds kfree(t); 1555*9fa89642SPavel Emelyanov out: 15561da177e4SLinus Torvalds return; 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds static void devinet_sysctl_unregister(struct ipv4_devconf *p) 15601da177e4SLinus Torvalds { 15611da177e4SLinus Torvalds if (p->sysctl) { 15621da177e4SLinus Torvalds struct devinet_sysctl_table *t = p->sysctl; 15631da177e4SLinus Torvalds p->sysctl = NULL; 15641da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 15651da177e4SLinus Torvalds kfree(t->devinet_dev[0].procname); 15661da177e4SLinus Torvalds kfree(t); 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds #endif 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds void __init devinet_init(void) 15721da177e4SLinus Torvalds { 15731da177e4SLinus Torvalds register_gifconf(PF_INET, inet_gifconf); 15741da177e4SLinus Torvalds register_netdevice_notifier(&ip_netdev_notifier); 157563f3444fSThomas Graf 157663f3444fSThomas Graf rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL); 157763f3444fSThomas Graf rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL); 157863f3444fSThomas Graf rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr); 15791da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 15801da177e4SLinus Torvalds devinet_sysctl.sysctl_header = 15810b4d4147SEric W. Biederman register_sysctl_table(devinet_sysctl.devinet_root_dir); 15821da177e4SLinus Torvalds devinet_sysctl_register(NULL, &ipv4_devconf_dflt); 15831da177e4SLinus Torvalds #endif 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds EXPORT_SYMBOL(in_dev_finish_destroy); 15871da177e4SLinus Torvalds EXPORT_SYMBOL(inet_select_addr); 15881da177e4SLinus Torvalds EXPORT_SYMBOL(inetdev_by_index); 15891da177e4SLinus Torvalds EXPORT_SYMBOL(register_inetaddr_notifier); 15901da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_inetaddr_notifier); 1591