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> 65752d14dcSPavel Emelyanov #include <net/net_namespace.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds struct ipv4_devconf ipv4_devconf = { 6842f811b8SHerbert Xu .data = { 6942f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, 7042f811b8SHerbert Xu [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, 7142f811b8SHerbert Xu [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, 7242f811b8SHerbert Xu [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, 7342f811b8SHerbert Xu }, 741da177e4SLinus Torvalds }; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds static struct ipv4_devconf ipv4_devconf_dflt = { 7742f811b8SHerbert Xu .data = { 7842f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, 7942f811b8SHerbert Xu [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, 8042f811b8SHerbert Xu [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, 8142f811b8SHerbert Xu [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, 8242f811b8SHerbert Xu [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1, 8342f811b8SHerbert Xu }, 841da177e4SLinus Torvalds }; 851da177e4SLinus Torvalds 869355bbd6SPavel Emelyanov #define IPV4_DEVCONF_DFLT(net, attr) \ 879355bbd6SPavel Emelyanov IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) 8842f811b8SHerbert Xu 89ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { 905c753978SThomas Graf [IFA_LOCAL] = { .type = NLA_U32 }, 915c753978SThomas Graf [IFA_ADDRESS] = { .type = NLA_U32 }, 925c753978SThomas Graf [IFA_BROADCAST] = { .type = NLA_U32 }, 935c753978SThomas Graf [IFA_ANYCAST] = { .type = NLA_U32 }, 945176f91eSThomas Graf [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, 955c753978SThomas Graf }; 965c753978SThomas Graf 97d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); 981da177e4SLinus Torvalds 99e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); 1001da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 1011da177e4SLinus Torvalds int destroy); 1021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 10366f27a52SPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev); 10451602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev); 10551602b2aSPavel Emelyanov #else 10651602b2aSPavel Emelyanov static inline void devinet_sysctl_register(struct in_device *idev) 10751602b2aSPavel Emelyanov { 10851602b2aSPavel Emelyanov } 10951602b2aSPavel Emelyanov static inline void devinet_sysctl_unregister(struct in_device *idev) 11051602b2aSPavel Emelyanov { 11151602b2aSPavel Emelyanov } 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* Locks all the inet devices. */ 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds static struct in_ifaddr *inet_alloc_ifa(void) 1171da177e4SLinus Torvalds { 1180da974f4SPanagiotis Issaris struct in_ifaddr *ifa = kzalloc(sizeof(*ifa), GFP_KERNEL); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (ifa) { 1211da177e4SLinus Torvalds INIT_RCU_HEAD(&ifa->rcu_head); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds return ifa; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static void inet_rcu_free_ifa(struct rcu_head *head) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); 1301da177e4SLinus Torvalds if (ifa->ifa_dev) 1311da177e4SLinus Torvalds in_dev_put(ifa->ifa_dev); 1321da177e4SLinus Torvalds kfree(ifa); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static inline void inet_free_ifa(struct in_ifaddr *ifa) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds call_rcu(&ifa->rcu_head, inet_rcu_free_ifa); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds void in_dev_finish_destroy(struct in_device *idev) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct net_device *dev = idev->dev; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds BUG_TRAP(!idev->ifa_list); 1451da177e4SLinus Torvalds BUG_TRAP(!idev->mc_list); 1461da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 1471da177e4SLinus Torvalds printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n", 1481da177e4SLinus Torvalds idev, dev ? dev->name : "NIL"); 1491da177e4SLinus Torvalds #endif 1501da177e4SLinus Torvalds dev_put(dev); 1511da177e4SLinus Torvalds if (!idev->dead) 1521da177e4SLinus Torvalds printk("Freeing alive in_device %p\n", idev); 1531da177e4SLinus Torvalds else { 1541da177e4SLinus Torvalds kfree(idev); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 15871e27da9SHerbert Xu static struct in_device *inetdev_init(struct net_device *dev) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds struct in_device *in_dev; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds ASSERT_RTNL(); 1631da177e4SLinus Torvalds 1640da974f4SPanagiotis Issaris in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); 1651da177e4SLinus Torvalds if (!in_dev) 1661da177e4SLinus Torvalds goto out; 1671da177e4SLinus Torvalds INIT_RCU_HEAD(&in_dev->rcu_head); 1689355bbd6SPavel Emelyanov memcpy(&in_dev->cnf, dev->nd_net->ipv4.devconf_dflt, 1699355bbd6SPavel Emelyanov sizeof(in_dev->cnf)); 1701da177e4SLinus Torvalds in_dev->cnf.sysctl = NULL; 1711da177e4SLinus Torvalds in_dev->dev = dev; 1721da177e4SLinus Torvalds if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL) 1731da177e4SLinus Torvalds goto out_kfree; 1741da177e4SLinus Torvalds /* Reference in_dev->dev */ 1751da177e4SLinus Torvalds dev_hold(dev); 17630c4cf57SDavid L Stevens /* Account for reference dev->ip_ptr (below) */ 1771da177e4SLinus Torvalds in_dev_hold(in_dev); 1781da177e4SLinus Torvalds 17966f27a52SPavel Emelyanov devinet_sysctl_register(in_dev); 1801da177e4SLinus Torvalds ip_mc_init_dev(in_dev); 1811da177e4SLinus Torvalds if (dev->flags & IFF_UP) 1821da177e4SLinus Torvalds ip_mc_up(in_dev); 183483479ecSJarek Poplawski 18430c4cf57SDavid L Stevens /* we can receive as soon as ip_ptr is set -- do this last */ 18530c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip_ptr, in_dev); 186483479ecSJarek Poplawski out: 1871da177e4SLinus Torvalds return in_dev; 1881da177e4SLinus Torvalds out_kfree: 1891da177e4SLinus Torvalds kfree(in_dev); 1901da177e4SLinus Torvalds in_dev = NULL; 1911da177e4SLinus Torvalds goto out; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds static void in_dev_rcu_put(struct rcu_head *head) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds struct in_device *idev = container_of(head, struct in_device, rcu_head); 1971da177e4SLinus Torvalds in_dev_put(idev); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds static void inetdev_destroy(struct in_device *in_dev) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds struct in_ifaddr *ifa; 2031da177e4SLinus Torvalds struct net_device *dev; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds ASSERT_RTNL(); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds dev = in_dev->dev; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds in_dev->dead = 1; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds ip_mc_destroy_dev(in_dev); 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds while ((ifa = in_dev->ifa_list) != NULL) { 2141da177e4SLinus Torvalds inet_del_ifa(in_dev, &in_dev->ifa_list, 0); 2151da177e4SLinus Torvalds inet_free_ifa(ifa); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds dev->ip_ptr = NULL; 2191da177e4SLinus Torvalds 22051602b2aSPavel Emelyanov devinet_sysctl_unregister(in_dev); 2211da177e4SLinus Torvalds neigh_parms_release(&arp_tbl, in_dev->arp_parms); 2221da177e4SLinus Torvalds arp_ifdown(dev); 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds call_rcu(&in_dev->rcu_head, in_dev_rcu_put); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 227ff428d72SAl Viro int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds rcu_read_lock(); 2301da177e4SLinus Torvalds for_primary_ifa(in_dev) { 2311da177e4SLinus Torvalds if (inet_ifa_match(a, ifa)) { 2321da177e4SLinus Torvalds if (!b || inet_ifa_match(b, ifa)) { 2331da177e4SLinus Torvalds rcu_read_unlock(); 2341da177e4SLinus Torvalds return 1; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds } endfor_ifa(in_dev); 2381da177e4SLinus Torvalds rcu_read_unlock(); 2391da177e4SLinus Torvalds return 0; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 242d6062cbbSThomas Graf static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 243d6062cbbSThomas Graf int destroy, struct nlmsghdr *nlh, u32 pid) 2441da177e4SLinus Torvalds { 2458f937c60SHarald Welte struct in_ifaddr *promote = NULL; 2460ff60a45SJamal Hadi Salim struct in_ifaddr *ifa, *ifa1 = *ifap; 2470ff60a45SJamal Hadi Salim struct in_ifaddr *last_prim = in_dev->ifa_list; 2480ff60a45SJamal Hadi Salim struct in_ifaddr *prev_prom = NULL; 2490ff60a45SJamal Hadi Salim int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds ASSERT_RTNL(); 2521da177e4SLinus Torvalds 2538f937c60SHarald Welte /* 1. Deleting primary ifaddr forces deletion all secondaries 2548f937c60SHarald Welte * unless alias promotion is set 2558f937c60SHarald Welte **/ 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { 2581da177e4SLinus Torvalds struct in_ifaddr **ifap1 = &ifa1->ifa_next; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds while ((ifa = *ifap1) != NULL) { 2610ff60a45SJamal Hadi Salim if (!(ifa->ifa_flags & IFA_F_SECONDARY) && 2620ff60a45SJamal Hadi Salim ifa1->ifa_scope <= ifa->ifa_scope) 2630ff60a45SJamal Hadi Salim last_prim = ifa; 2640ff60a45SJamal Hadi Salim 2651da177e4SLinus Torvalds if (!(ifa->ifa_flags & IFA_F_SECONDARY) || 2661da177e4SLinus Torvalds ifa1->ifa_mask != ifa->ifa_mask || 2671da177e4SLinus Torvalds !inet_ifa_match(ifa1->ifa_address, ifa)) { 2681da177e4SLinus Torvalds ifap1 = &ifa->ifa_next; 2690ff60a45SJamal Hadi Salim prev_prom = ifa; 2701da177e4SLinus Torvalds continue; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2730ff60a45SJamal Hadi Salim if (!do_promote) { 2741da177e4SLinus Torvalds *ifap1 = ifa->ifa_next; 2751da177e4SLinus Torvalds 276d6062cbbSThomas Graf rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid); 277e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, 278e041c683SAlan Stern NETDEV_DOWN, ifa); 2791da177e4SLinus Torvalds inet_free_ifa(ifa); 2808f937c60SHarald Welte } else { 2818f937c60SHarald Welte promote = ifa; 2828f937c60SHarald Welte break; 2838f937c60SHarald Welte } 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* 2. Unlink it */ 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds *ifap = ifa1->ifa_next; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 3. Announce address deletion */ 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* Send message first, then call notifier. 2941da177e4SLinus Torvalds At first sight, FIB update triggered by notifier 2951da177e4SLinus Torvalds will refer to already deleted ifaddr, that could confuse 2961da177e4SLinus Torvalds netlink listeners. It is not true: look, gated sees 2971da177e4SLinus Torvalds that route deleted and if it still thinks that ifaddr 2981da177e4SLinus Torvalds is valid, it will try to restore deleted routes... Grr. 2991da177e4SLinus Torvalds So that, this order is correct. 3001da177e4SLinus Torvalds */ 301d6062cbbSThomas Graf rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid); 302e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); 3030ff60a45SJamal Hadi Salim 3040ff60a45SJamal Hadi Salim if (promote) { 3050ff60a45SJamal Hadi Salim 3060ff60a45SJamal Hadi Salim if (prev_prom) { 3070ff60a45SJamal Hadi Salim prev_prom->ifa_next = promote->ifa_next; 3080ff60a45SJamal Hadi Salim promote->ifa_next = last_prim->ifa_next; 3090ff60a45SJamal Hadi Salim last_prim->ifa_next = promote; 3100ff60a45SJamal Hadi Salim } 3110ff60a45SJamal Hadi Salim 3120ff60a45SJamal Hadi Salim promote->ifa_flags &= ~IFA_F_SECONDARY; 313d6062cbbSThomas Graf rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid); 314e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, 315e041c683SAlan Stern NETDEV_UP, promote); 3160ff60a45SJamal Hadi Salim for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) { 3170ff60a45SJamal Hadi Salim if (ifa1->ifa_mask != ifa->ifa_mask || 3180ff60a45SJamal Hadi Salim !inet_ifa_match(ifa1->ifa_address, ifa)) 3190ff60a45SJamal Hadi Salim continue; 3200ff60a45SJamal Hadi Salim fib_add_ifaddr(ifa); 3210ff60a45SJamal Hadi Salim } 3220ff60a45SJamal Hadi Salim 3230ff60a45SJamal Hadi Salim } 3246363097cSHerbert Xu if (destroy) 3251da177e4SLinus Torvalds inet_free_ifa(ifa1); 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 328d6062cbbSThomas Graf static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, 329d6062cbbSThomas Graf int destroy) 330d6062cbbSThomas Graf { 331d6062cbbSThomas Graf __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); 332d6062cbbSThomas Graf } 333d6062cbbSThomas Graf 334d6062cbbSThomas Graf static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, 335d6062cbbSThomas Graf u32 pid) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct in_device *in_dev = ifa->ifa_dev; 3381da177e4SLinus Torvalds struct in_ifaddr *ifa1, **ifap, **last_primary; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds ASSERT_RTNL(); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (!ifa->ifa_local) { 3431da177e4SLinus Torvalds inet_free_ifa(ifa); 3441da177e4SLinus Torvalds return 0; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds ifa->ifa_flags &= ~IFA_F_SECONDARY; 3481da177e4SLinus Torvalds last_primary = &in_dev->ifa_list; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; 3511da177e4SLinus Torvalds ifap = &ifa1->ifa_next) { 3521da177e4SLinus Torvalds if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && 3531da177e4SLinus Torvalds ifa->ifa_scope <= ifa1->ifa_scope) 3541da177e4SLinus Torvalds last_primary = &ifa1->ifa_next; 3551da177e4SLinus Torvalds if (ifa1->ifa_mask == ifa->ifa_mask && 3561da177e4SLinus Torvalds inet_ifa_match(ifa1->ifa_address, ifa)) { 3571da177e4SLinus Torvalds if (ifa1->ifa_local == ifa->ifa_local) { 3581da177e4SLinus Torvalds inet_free_ifa(ifa); 3591da177e4SLinus Torvalds return -EEXIST; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds if (ifa1->ifa_scope != ifa->ifa_scope) { 3621da177e4SLinus Torvalds inet_free_ifa(ifa); 3631da177e4SLinus Torvalds return -EINVAL; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds ifa->ifa_flags |= IFA_F_SECONDARY; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (!(ifa->ifa_flags & IFA_F_SECONDARY)) { 3701da177e4SLinus Torvalds net_srandom(ifa->ifa_local); 3711da177e4SLinus Torvalds ifap = last_primary; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds ifa->ifa_next = *ifap; 3751da177e4SLinus Torvalds *ifap = ifa; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* Send message first, then call notifier. 3781da177e4SLinus Torvalds Notifier will trigger FIB update, so that 3791da177e4SLinus Torvalds listeners of netlink will know about new ifaddr */ 380d6062cbbSThomas Graf rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid); 381e041c683SAlan Stern blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds return 0; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 386d6062cbbSThomas Graf static int inet_insert_ifa(struct in_ifaddr *ifa) 387d6062cbbSThomas Graf { 388d6062cbbSThomas Graf return __inet_insert_ifa(ifa, NULL, 0); 389d6062cbbSThomas Graf } 390d6062cbbSThomas Graf 3911da177e4SLinus Torvalds static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) 3921da177e4SLinus Torvalds { 393e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds ASSERT_RTNL(); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds if (!in_dev) { 3981da177e4SLinus Torvalds inet_free_ifa(ifa); 3991da177e4SLinus Torvalds return -ENOBUFS; 4001da177e4SLinus Torvalds } 40171e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 4021da177e4SLinus Torvalds if (ifa->ifa_dev != in_dev) { 4031da177e4SLinus Torvalds BUG_TRAP(!ifa->ifa_dev); 4041da177e4SLinus Torvalds in_dev_hold(in_dev); 4051da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 4061da177e4SLinus Torvalds } 407*f97c1e0cSJoe Perches if (ipv4_is_loopback(ifa->ifa_local)) 4081da177e4SLinus Torvalds ifa->ifa_scope = RT_SCOPE_HOST; 4091da177e4SLinus Torvalds return inet_insert_ifa(ifa); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds struct in_device *inetdev_by_index(int ifindex) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds struct net_device *dev; 4151da177e4SLinus Torvalds struct in_device *in_dev = NULL; 4161da177e4SLinus Torvalds read_lock(&dev_base_lock); 417881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifindex); 4181da177e4SLinus Torvalds if (dev) 4191da177e4SLinus Torvalds in_dev = in_dev_get(dev); 4201da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4211da177e4SLinus Torvalds return in_dev; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* Called only from RTNL semaphored context. No locks. */ 4251da177e4SLinus Torvalds 42660cad5daSAl Viro struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, 42760cad5daSAl Viro __be32 mask) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds ASSERT_RTNL(); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds for_primary_ifa(in_dev) { 4321da177e4SLinus Torvalds if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) 4331da177e4SLinus Torvalds return ifa; 4341da177e4SLinus Torvalds } endfor_ifa(in_dev); 4351da177e4SLinus Torvalds return NULL; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 4391da177e4SLinus Torvalds { 440b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 441dfdd5fd4SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4421da177e4SLinus Torvalds struct in_device *in_dev; 443dfdd5fd4SThomas Graf struct ifaddrmsg *ifm; 4441da177e4SLinus Torvalds struct in_ifaddr *ifa, **ifap; 445dfdd5fd4SThomas Graf int err = -EINVAL; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds ASSERT_RTNL(); 4481da177e4SLinus Torvalds 449b854272bSDenis V. Lunev if (net != &init_net) 450b854272bSDenis V. Lunev return -EINVAL; 451b854272bSDenis V. Lunev 452dfdd5fd4SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); 453dfdd5fd4SThomas Graf if (err < 0) 454dfdd5fd4SThomas Graf goto errout; 455dfdd5fd4SThomas Graf 456dfdd5fd4SThomas Graf ifm = nlmsg_data(nlh); 457dfdd5fd4SThomas Graf in_dev = inetdev_by_index(ifm->ifa_index); 458dfdd5fd4SThomas Graf if (in_dev == NULL) { 459dfdd5fd4SThomas Graf err = -ENODEV; 460dfdd5fd4SThomas Graf goto errout; 461dfdd5fd4SThomas Graf } 462dfdd5fd4SThomas Graf 4631da177e4SLinus Torvalds __in_dev_put(in_dev); 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 4661da177e4SLinus Torvalds ifap = &ifa->ifa_next) { 467dfdd5fd4SThomas Graf if (tb[IFA_LOCAL] && 468a7a628c4SAl Viro ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) 4691da177e4SLinus Torvalds continue; 470dfdd5fd4SThomas Graf 471dfdd5fd4SThomas Graf if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) 472dfdd5fd4SThomas Graf continue; 473dfdd5fd4SThomas Graf 474dfdd5fd4SThomas Graf if (tb[IFA_ADDRESS] && 475dfdd5fd4SThomas Graf (ifm->ifa_prefixlen != ifa->ifa_prefixlen || 476a7a628c4SAl Viro !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) 477dfdd5fd4SThomas Graf continue; 478dfdd5fd4SThomas Graf 479d6062cbbSThomas Graf __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid); 4801da177e4SLinus Torvalds return 0; 4811da177e4SLinus Torvalds } 482dfdd5fd4SThomas Graf 483dfdd5fd4SThomas Graf err = -EADDRNOTAVAIL; 484dfdd5fd4SThomas Graf errout: 485dfdd5fd4SThomas Graf return err; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4885c753978SThomas Graf static struct in_ifaddr *rtm_to_ifaddr(struct nlmsghdr *nlh) 4891da177e4SLinus Torvalds { 4905c753978SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4915c753978SThomas Graf struct in_ifaddr *ifa; 4925c753978SThomas Graf struct ifaddrmsg *ifm; 4931da177e4SLinus Torvalds struct net_device *dev; 4941da177e4SLinus Torvalds struct in_device *in_dev; 4955c753978SThomas Graf int err = -EINVAL; 4961da177e4SLinus Torvalds 4975c753978SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); 4985c753978SThomas Graf if (err < 0) 4995c753978SThomas Graf goto errout; 5001da177e4SLinus Torvalds 5015c753978SThomas Graf ifm = nlmsg_data(nlh); 502c4e38f41SEvgeniy Polyakov if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) { 503c4e38f41SEvgeniy Polyakov err = -EINVAL; 5045c753978SThomas Graf goto errout; 505c4e38f41SEvgeniy Polyakov } 5061da177e4SLinus Torvalds 507881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifm->ifa_index); 5085c753978SThomas Graf if (dev == NULL) { 5095c753978SThomas Graf err = -ENODEV; 5105c753978SThomas Graf goto errout; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5135c753978SThomas Graf in_dev = __in_dev_get_rtnl(dev); 5145c753978SThomas Graf if (in_dev == NULL) { 5155c753978SThomas Graf err = -ENOBUFS; 5165c753978SThomas Graf goto errout; 5175c753978SThomas Graf } 51871e27da9SHerbert Xu 5195c753978SThomas Graf ifa = inet_alloc_ifa(); 5205c753978SThomas Graf if (ifa == NULL) { 5215c753978SThomas Graf /* 5225c753978SThomas Graf * A potential indev allocation can be left alive, it stays 5235c753978SThomas Graf * assigned to its device and is destroy with it. 5245c753978SThomas Graf */ 5255c753978SThomas Graf err = -ENOBUFS; 5265c753978SThomas Graf goto errout; 5275c753978SThomas Graf } 5285c753978SThomas Graf 529a4e65d36SPavel Emelyanov ipv4_devconf_setall(in_dev); 5305c753978SThomas Graf in_dev_hold(in_dev); 5315c753978SThomas Graf 5325c753978SThomas Graf if (tb[IFA_ADDRESS] == NULL) 5335c753978SThomas Graf tb[IFA_ADDRESS] = tb[IFA_LOCAL]; 5345c753978SThomas Graf 5351da177e4SLinus Torvalds ifa->ifa_prefixlen = ifm->ifa_prefixlen; 5361da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); 5371da177e4SLinus Torvalds ifa->ifa_flags = ifm->ifa_flags; 5381da177e4SLinus Torvalds ifa->ifa_scope = ifm->ifa_scope; 5391da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 5405c753978SThomas Graf 541a7a628c4SAl Viro ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); 542a7a628c4SAl Viro ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); 5435c753978SThomas Graf 5445c753978SThomas Graf if (tb[IFA_BROADCAST]) 545a7a628c4SAl Viro ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); 5465c753978SThomas Graf 5475c753978SThomas Graf if (tb[IFA_ANYCAST]) 548a7a628c4SAl Viro ifa->ifa_anycast = nla_get_be32(tb[IFA_ANYCAST]); 5495c753978SThomas Graf 5505c753978SThomas Graf if (tb[IFA_LABEL]) 5515c753978SThomas Graf nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); 5521da177e4SLinus Torvalds else 5531da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 5541da177e4SLinus Torvalds 5555c753978SThomas Graf return ifa; 5565c753978SThomas Graf 5575c753978SThomas Graf errout: 5585c753978SThomas Graf return ERR_PTR(err); 5595c753978SThomas Graf } 5605c753978SThomas Graf 5615c753978SThomas Graf static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 5625c753978SThomas Graf { 563b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 5645c753978SThomas Graf struct in_ifaddr *ifa; 5655c753978SThomas Graf 5665c753978SThomas Graf ASSERT_RTNL(); 5675c753978SThomas Graf 568b854272bSDenis V. Lunev if (net != &init_net) 569b854272bSDenis V. Lunev return -EINVAL; 570b854272bSDenis V. Lunev 5715c753978SThomas Graf ifa = rtm_to_ifaddr(nlh); 5725c753978SThomas Graf if (IS_ERR(ifa)) 5735c753978SThomas Graf return PTR_ERR(ifa); 5745c753978SThomas Graf 575d6062cbbSThomas Graf return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* 5791da177e4SLinus Torvalds * Determine a default network mask, based on the IP address. 5801da177e4SLinus Torvalds */ 5811da177e4SLinus Torvalds 582714e85beSAl Viro static __inline__ int inet_abc_len(__be32 addr) 5831da177e4SLinus Torvalds { 5841da177e4SLinus Torvalds int rc = -1; /* Something else, probably a multicast. */ 5851da177e4SLinus Torvalds 586*f97c1e0cSJoe Perches if (ipv4_is_zeronet(addr)) 5871da177e4SLinus Torvalds rc = 0; 5881da177e4SLinus Torvalds else { 589714e85beSAl Viro __u32 haddr = ntohl(addr); 5901da177e4SLinus Torvalds 591714e85beSAl Viro if (IN_CLASSA(haddr)) 5921da177e4SLinus Torvalds rc = 8; 593714e85beSAl Viro else if (IN_CLASSB(haddr)) 5941da177e4SLinus Torvalds rc = 16; 595714e85beSAl Viro else if (IN_CLASSC(haddr)) 5961da177e4SLinus Torvalds rc = 24; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds return rc; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds int devinet_ioctl(unsigned int cmd, void __user *arg) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds struct ifreq ifr; 6061da177e4SLinus Torvalds struct sockaddr_in sin_orig; 6071da177e4SLinus Torvalds struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; 6081da177e4SLinus Torvalds struct in_device *in_dev; 6091da177e4SLinus Torvalds struct in_ifaddr **ifap = NULL; 6101da177e4SLinus Torvalds struct in_ifaddr *ifa = NULL; 6111da177e4SLinus Torvalds struct net_device *dev; 6121da177e4SLinus Torvalds char *colon; 6131da177e4SLinus Torvalds int ret = -EFAULT; 6141da177e4SLinus Torvalds int tryaddrmatch = 0; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds /* 6171da177e4SLinus Torvalds * Fetch the caller's info block into kernel space 6181da177e4SLinus Torvalds */ 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) 6211da177e4SLinus Torvalds goto out; 6221da177e4SLinus Torvalds ifr.ifr_name[IFNAMSIZ - 1] = 0; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* save original address for comparison */ 6251da177e4SLinus Torvalds memcpy(&sin_orig, sin, sizeof(*sin)); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds colon = strchr(ifr.ifr_name, ':'); 6281da177e4SLinus Torvalds if (colon) 6291da177e4SLinus Torvalds *colon = 0; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds #ifdef CONFIG_KMOD 632881d966bSEric W. Biederman dev_load(&init_net, ifr.ifr_name); 6331da177e4SLinus Torvalds #endif 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds switch (cmd) { 6361da177e4SLinus Torvalds case SIOCGIFADDR: /* Get interface address */ 6371da177e4SLinus Torvalds case SIOCGIFBRDADDR: /* Get the broadcast address */ 6381da177e4SLinus Torvalds case SIOCGIFDSTADDR: /* Get the destination address */ 6391da177e4SLinus Torvalds case SIOCGIFNETMASK: /* Get the netmask for the interface */ 6401da177e4SLinus Torvalds /* Note that these ioctls will not sleep, 6411da177e4SLinus Torvalds so that we do not impose a lock. 6421da177e4SLinus Torvalds One day we will be forced to put shlock here (I mean SMP) 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds tryaddrmatch = (sin_orig.sin_family == AF_INET); 6451da177e4SLinus Torvalds memset(sin, 0, sizeof(*sin)); 6461da177e4SLinus Torvalds sin->sin_family = AF_INET; 6471da177e4SLinus Torvalds break; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds case SIOCSIFFLAGS: 6501da177e4SLinus Torvalds ret = -EACCES; 6511da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6521da177e4SLinus Torvalds goto out; 6531da177e4SLinus Torvalds break; 6541da177e4SLinus Torvalds case SIOCSIFADDR: /* Set interface address (and family) */ 6551da177e4SLinus Torvalds case SIOCSIFBRDADDR: /* Set the broadcast address */ 6561da177e4SLinus Torvalds case SIOCSIFDSTADDR: /* Set the destination address */ 6571da177e4SLinus Torvalds case SIOCSIFNETMASK: /* Set the netmask for the interface */ 6581da177e4SLinus Torvalds ret = -EACCES; 6591da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6601da177e4SLinus Torvalds goto out; 6611da177e4SLinus Torvalds ret = -EINVAL; 6621da177e4SLinus Torvalds if (sin->sin_family != AF_INET) 6631da177e4SLinus Torvalds goto out; 6641da177e4SLinus Torvalds break; 6651da177e4SLinus Torvalds default: 6661da177e4SLinus Torvalds ret = -EINVAL; 6671da177e4SLinus Torvalds goto out; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds rtnl_lock(); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds ret = -ENODEV; 673881d966bSEric W. Biederman if ((dev = __dev_get_by_name(&init_net, ifr.ifr_name)) == NULL) 6741da177e4SLinus Torvalds goto done; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds if (colon) 6771da177e4SLinus Torvalds *colon = ':'; 6781da177e4SLinus Torvalds 679e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { 6801da177e4SLinus Torvalds if (tryaddrmatch) { 6811da177e4SLinus Torvalds /* Matthias Andree */ 6821da177e4SLinus Torvalds /* compare label and address (4.4BSD style) */ 6831da177e4SLinus Torvalds /* note: we only do this for a limited set of ioctls 6841da177e4SLinus Torvalds and only if the original address family was AF_INET. 6851da177e4SLinus Torvalds This is checked above. */ 6861da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 6871da177e4SLinus Torvalds ifap = &ifa->ifa_next) { 6881da177e4SLinus Torvalds if (!strcmp(ifr.ifr_name, ifa->ifa_label) && 6891da177e4SLinus Torvalds sin_orig.sin_addr.s_addr == 6901da177e4SLinus Torvalds ifa->ifa_address) { 6911da177e4SLinus Torvalds break; /* found */ 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds /* we didn't get a match, maybe the application is 6961da177e4SLinus Torvalds 4.3BSD-style and passed in junk so we fall back to 6971da177e4SLinus Torvalds comparing just the label */ 6981da177e4SLinus Torvalds if (!ifa) { 6991da177e4SLinus Torvalds for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 7001da177e4SLinus Torvalds ifap = &ifa->ifa_next) 7011da177e4SLinus Torvalds if (!strcmp(ifr.ifr_name, ifa->ifa_label)) 7021da177e4SLinus Torvalds break; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 7071da177e4SLinus Torvalds if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) 7081da177e4SLinus Torvalds goto done; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds switch (cmd) { 7111da177e4SLinus Torvalds case SIOCGIFADDR: /* Get interface address */ 7121da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_local; 7131da177e4SLinus Torvalds goto rarok; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds case SIOCGIFBRDADDR: /* Get the broadcast address */ 7161da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_broadcast; 7171da177e4SLinus Torvalds goto rarok; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds case SIOCGIFDSTADDR: /* Get the destination address */ 7201da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_address; 7211da177e4SLinus Torvalds goto rarok; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds case SIOCGIFNETMASK: /* Get the netmask for the interface */ 7241da177e4SLinus Torvalds sin->sin_addr.s_addr = ifa->ifa_mask; 7251da177e4SLinus Torvalds goto rarok; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds case SIOCSIFFLAGS: 7281da177e4SLinus Torvalds if (colon) { 7291da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 7301da177e4SLinus Torvalds if (!ifa) 7311da177e4SLinus Torvalds break; 7321da177e4SLinus Torvalds ret = 0; 7331da177e4SLinus Torvalds if (!(ifr.ifr_flags & IFF_UP)) 7341da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 1); 7351da177e4SLinus Torvalds break; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds ret = dev_change_flags(dev, ifr.ifr_flags); 7381da177e4SLinus Torvalds break; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds case SIOCSIFADDR: /* Set interface address (and family) */ 7411da177e4SLinus Torvalds ret = -EINVAL; 7421da177e4SLinus Torvalds if (inet_abc_len(sin->sin_addr.s_addr) < 0) 7431da177e4SLinus Torvalds break; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds if (!ifa) { 7461da177e4SLinus Torvalds ret = -ENOBUFS; 7471da177e4SLinus Torvalds if ((ifa = inet_alloc_ifa()) == NULL) 7481da177e4SLinus Torvalds break; 7491da177e4SLinus Torvalds if (colon) 7501da177e4SLinus Torvalds memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ); 7511da177e4SLinus Torvalds else 7521da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 7531da177e4SLinus Torvalds } else { 7541da177e4SLinus Torvalds ret = 0; 7551da177e4SLinus Torvalds if (ifa->ifa_local == sin->sin_addr.s_addr) 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 7581da177e4SLinus Torvalds ifa->ifa_broadcast = 0; 7591da177e4SLinus Torvalds ifa->ifa_anycast = 0; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds if (!(dev->flags & IFF_POINTOPOINT)) { 7651da177e4SLinus Torvalds ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); 7661da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); 7671da177e4SLinus Torvalds if ((dev->flags & IFF_BROADCAST) && 7681da177e4SLinus Torvalds ifa->ifa_prefixlen < 31) 7691da177e4SLinus Torvalds ifa->ifa_broadcast = ifa->ifa_address | 7701da177e4SLinus Torvalds ~ifa->ifa_mask; 7711da177e4SLinus Torvalds } else { 7721da177e4SLinus Torvalds ifa->ifa_prefixlen = 32; 7731da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(32); 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds ret = inet_set_ifa(dev, ifa); 7761da177e4SLinus Torvalds break; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds case SIOCSIFBRDADDR: /* Set the broadcast address */ 7791da177e4SLinus Torvalds ret = 0; 7801da177e4SLinus Torvalds if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { 7811da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 7821da177e4SLinus Torvalds ifa->ifa_broadcast = sin->sin_addr.s_addr; 7831da177e4SLinus Torvalds inet_insert_ifa(ifa); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds case SIOCSIFDSTADDR: /* Set the destination address */ 7881da177e4SLinus Torvalds ret = 0; 7891da177e4SLinus Torvalds if (ifa->ifa_address == sin->sin_addr.s_addr) 7901da177e4SLinus Torvalds break; 7911da177e4SLinus Torvalds ret = -EINVAL; 7921da177e4SLinus Torvalds if (inet_abc_len(sin->sin_addr.s_addr) < 0) 7931da177e4SLinus Torvalds break; 7941da177e4SLinus Torvalds ret = 0; 7951da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 7961da177e4SLinus Torvalds ifa->ifa_address = sin->sin_addr.s_addr; 7971da177e4SLinus Torvalds inet_insert_ifa(ifa); 7981da177e4SLinus Torvalds break; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds case SIOCSIFNETMASK: /* Set the netmask for the interface */ 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* 8031da177e4SLinus Torvalds * The mask we set must be legal. 8041da177e4SLinus Torvalds */ 8051da177e4SLinus Torvalds ret = -EINVAL; 8061da177e4SLinus Torvalds if (bad_mask(sin->sin_addr.s_addr, 0)) 8071da177e4SLinus Torvalds break; 8081da177e4SLinus Torvalds ret = 0; 8091da177e4SLinus Torvalds if (ifa->ifa_mask != sin->sin_addr.s_addr) { 810a144ea4bSAl Viro __be32 old_mask = ifa->ifa_mask; 8111da177e4SLinus Torvalds inet_del_ifa(in_dev, ifap, 0); 8121da177e4SLinus Torvalds ifa->ifa_mask = sin->sin_addr.s_addr; 8131da177e4SLinus Torvalds ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds /* See if current broadcast address matches 8161da177e4SLinus Torvalds * with current netmask, then recalculate 8171da177e4SLinus Torvalds * the broadcast address. Otherwise it's a 8181da177e4SLinus Torvalds * funny address, so don't touch it since 8191da177e4SLinus Torvalds * the user seems to know what (s)he's doing... 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds if ((dev->flags & IFF_BROADCAST) && 8221da177e4SLinus Torvalds (ifa->ifa_prefixlen < 31) && 8231da177e4SLinus Torvalds (ifa->ifa_broadcast == 824dcab5e1eSDavid Engel (ifa->ifa_local|~old_mask))) { 8251da177e4SLinus Torvalds ifa->ifa_broadcast = (ifa->ifa_local | 8261da177e4SLinus Torvalds ~sin->sin_addr.s_addr); 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds inet_insert_ifa(ifa); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds break; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds done: 8331da177e4SLinus Torvalds rtnl_unlock(); 8341da177e4SLinus Torvalds out: 8351da177e4SLinus Torvalds return ret; 8361da177e4SLinus Torvalds rarok: 8371da177e4SLinus Torvalds rtnl_unlock(); 8381da177e4SLinus Torvalds ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0; 8391da177e4SLinus Torvalds goto out; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds static int inet_gifconf(struct net_device *dev, char __user *buf, int len) 8431da177e4SLinus Torvalds { 844e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 8451da177e4SLinus Torvalds struct in_ifaddr *ifa; 8461da177e4SLinus Torvalds struct ifreq ifr; 8471da177e4SLinus Torvalds int done = 0; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds if (!in_dev || (ifa = in_dev->ifa_list) == NULL) 8501da177e4SLinus Torvalds goto out; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds for (; ifa; ifa = ifa->ifa_next) { 8531da177e4SLinus Torvalds if (!buf) { 8541da177e4SLinus Torvalds done += sizeof(ifr); 8551da177e4SLinus Torvalds continue; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds if (len < (int) sizeof(ifr)) 8581da177e4SLinus Torvalds break; 8591da177e4SLinus Torvalds memset(&ifr, 0, sizeof(struct ifreq)); 8601da177e4SLinus Torvalds if (ifa->ifa_label) 8611da177e4SLinus Torvalds strcpy(ifr.ifr_name, ifa->ifa_label); 8621da177e4SLinus Torvalds else 8631da177e4SLinus Torvalds strcpy(ifr.ifr_name, dev->name); 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; 8661da177e4SLinus Torvalds (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = 8671da177e4SLinus Torvalds ifa->ifa_local; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) { 8701da177e4SLinus Torvalds done = -EFAULT; 8711da177e4SLinus Torvalds break; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds buf += sizeof(struct ifreq); 8741da177e4SLinus Torvalds len -= sizeof(struct ifreq); 8751da177e4SLinus Torvalds done += sizeof(struct ifreq); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds out: 8781da177e4SLinus Torvalds return done; 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 881a61ced5dSAl Viro __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) 8821da177e4SLinus Torvalds { 883a61ced5dSAl Viro __be32 addr = 0; 8841da177e4SLinus Torvalds struct in_device *in_dev; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds rcu_read_lock(); 887e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 8881da177e4SLinus Torvalds if (!in_dev) 8891da177e4SLinus Torvalds goto no_in_dev; 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds for_primary_ifa(in_dev) { 8921da177e4SLinus Torvalds if (ifa->ifa_scope > scope) 8931da177e4SLinus Torvalds continue; 8941da177e4SLinus Torvalds if (!dst || inet_ifa_match(dst, ifa)) { 8951da177e4SLinus Torvalds addr = ifa->ifa_local; 8961da177e4SLinus Torvalds break; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds if (!addr) 8991da177e4SLinus Torvalds addr = ifa->ifa_local; 9001da177e4SLinus Torvalds } endfor_ifa(in_dev); 9011da177e4SLinus Torvalds no_in_dev: 9021da177e4SLinus Torvalds rcu_read_unlock(); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (addr) 9051da177e4SLinus Torvalds goto out; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds /* Not loopback addresses on loopback should be preferred 9081da177e4SLinus Torvalds in this case. It is importnat that lo is the first interface 9091da177e4SLinus Torvalds in dev_base list. 9101da177e4SLinus Torvalds */ 9111da177e4SLinus Torvalds read_lock(&dev_base_lock); 9121da177e4SLinus Torvalds rcu_read_lock(); 913881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 914e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) 9151da177e4SLinus Torvalds continue; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds for_primary_ifa(in_dev) { 9181da177e4SLinus Torvalds if (ifa->ifa_scope != RT_SCOPE_LINK && 9191da177e4SLinus Torvalds ifa->ifa_scope <= scope) { 9201da177e4SLinus Torvalds addr = ifa->ifa_local; 9211da177e4SLinus Torvalds goto out_unlock_both; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds } endfor_ifa(in_dev); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds out_unlock_both: 9261da177e4SLinus Torvalds read_unlock(&dev_base_lock); 9271da177e4SLinus Torvalds rcu_read_unlock(); 9281da177e4SLinus Torvalds out: 9291da177e4SLinus Torvalds return addr; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 93260cad5daSAl Viro static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, 93360cad5daSAl Viro __be32 local, int scope) 9341da177e4SLinus Torvalds { 9351da177e4SLinus Torvalds int same = 0; 936a144ea4bSAl Viro __be32 addr = 0; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds for_ifa(in_dev) { 9391da177e4SLinus Torvalds if (!addr && 9401da177e4SLinus Torvalds (local == ifa->ifa_local || !local) && 9411da177e4SLinus Torvalds ifa->ifa_scope <= scope) { 9421da177e4SLinus Torvalds addr = ifa->ifa_local; 9431da177e4SLinus Torvalds if (same) 9441da177e4SLinus Torvalds break; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds if (!same) { 9471da177e4SLinus Torvalds same = (!local || inet_ifa_match(local, ifa)) && 9481da177e4SLinus Torvalds (!dst || inet_ifa_match(dst, ifa)); 9491da177e4SLinus Torvalds if (same && addr) { 9501da177e4SLinus Torvalds if (local || !dst) 9511da177e4SLinus Torvalds break; 9521da177e4SLinus Torvalds /* Is the selected addr into dst subnet? */ 9531da177e4SLinus Torvalds if (inet_ifa_match(addr, ifa)) 9541da177e4SLinus Torvalds break; 9551da177e4SLinus Torvalds /* No, then can we use new local src? */ 9561da177e4SLinus Torvalds if (ifa->ifa_scope <= scope) { 9571da177e4SLinus Torvalds addr = ifa->ifa_local; 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds /* search for large dst subnet for addr */ 9611da177e4SLinus Torvalds same = 0; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds } endfor_ifa(in_dev); 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds return same? addr : 0; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * Confirm that local IP address exists using wildcards: 9711da177e4SLinus Torvalds * - dev: only on this interface, 0=any interface 9721da177e4SLinus Torvalds * - dst: only in the same subnet as dst, 0=any dst 9731da177e4SLinus Torvalds * - local: address, 0=autoselect the local address 9741da177e4SLinus Torvalds * - scope: maximum allowed scope value for the local address 9751da177e4SLinus Torvalds */ 97660cad5daSAl Viro __be32 inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope) 9771da177e4SLinus Torvalds { 97860cad5daSAl Viro __be32 addr = 0; 9791da177e4SLinus Torvalds struct in_device *in_dev; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds if (dev) { 9821da177e4SLinus Torvalds rcu_read_lock(); 983e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev))) 9841da177e4SLinus Torvalds addr = confirm_addr_indev(in_dev, dst, local, scope); 9851da177e4SLinus Torvalds rcu_read_unlock(); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds return addr; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds read_lock(&dev_base_lock); 9911da177e4SLinus Torvalds rcu_read_lock(); 992881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 993e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev))) { 9941da177e4SLinus Torvalds addr = confirm_addr_indev(in_dev, dst, local, scope); 9951da177e4SLinus Torvalds if (addr) 9961da177e4SLinus Torvalds break; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds rcu_read_unlock(); 10001da177e4SLinus Torvalds read_unlock(&dev_base_lock); 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds return addr; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds /* 10061da177e4SLinus Torvalds * Device notifier 10071da177e4SLinus Torvalds */ 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds int register_inetaddr_notifier(struct notifier_block *nb) 10101da177e4SLinus Torvalds { 1011e041c683SAlan Stern return blocking_notifier_chain_register(&inetaddr_chain, nb); 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds int unregister_inetaddr_notifier(struct notifier_block *nb) 10151da177e4SLinus Torvalds { 1016e041c683SAlan Stern return blocking_notifier_chain_unregister(&inetaddr_chain, nb); 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* Rename ifa_labels for a device name change. Make some effort to preserve existing 10201da177e4SLinus Torvalds * alias numbering and to create unique labels if possible. 10211da177e4SLinus Torvalds */ 10221da177e4SLinus Torvalds static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) 10231da177e4SLinus Torvalds { 10241da177e4SLinus Torvalds struct in_ifaddr *ifa; 10251da177e4SLinus Torvalds int named = 0; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 10281da177e4SLinus Torvalds char old[IFNAMSIZ], *dot; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds memcpy(old, ifa->ifa_label, IFNAMSIZ); 10311da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 10321da177e4SLinus Torvalds if (named++ == 0) 10331da177e4SLinus Torvalds continue; 103444344b2aSMark McLoughlin dot = strchr(old, ':'); 10351da177e4SLinus Torvalds if (dot == NULL) { 10361da177e4SLinus Torvalds sprintf(old, ":%d", named); 10371da177e4SLinus Torvalds dot = old; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) { 10401da177e4SLinus Torvalds strcat(ifa->ifa_label, dot); 10411da177e4SLinus Torvalds } else { 10421da177e4SLinus Torvalds strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* Called only under RTNL semaphore */ 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds static int inetdev_event(struct notifier_block *this, unsigned long event, 10501da177e4SLinus Torvalds void *ptr) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds struct net_device *dev = ptr; 1053e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 10541da177e4SLinus Torvalds 1055e9dc8653SEric W. Biederman if (dev->nd_net != &init_net) 1056e9dc8653SEric W. Biederman return NOTIFY_DONE; 1057e9dc8653SEric W. Biederman 10581da177e4SLinus Torvalds ASSERT_RTNL(); 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds if (!in_dev) { 10618030f544SHerbert Xu if (event == NETDEV_REGISTER) { 10621da177e4SLinus Torvalds in_dev = inetdev_init(dev); 10638d76527eSHerbert Xu if (!in_dev) 1064b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 10650cc217e1SEric W. Biederman if (dev->flags & IFF_LOOPBACK) { 106642f811b8SHerbert Xu IN_DEV_CONF_SET(in_dev, NOXFRM, 1); 106742f811b8SHerbert Xu IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); 10681da177e4SLinus Torvalds } 10698030f544SHerbert Xu } 10701da177e4SLinus Torvalds goto out; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds switch (event) { 10741da177e4SLinus Torvalds case NETDEV_REGISTER: 10751da177e4SLinus Torvalds printk(KERN_DEBUG "inetdev_event: bug\n"); 10761da177e4SLinus Torvalds dev->ip_ptr = NULL; 10771da177e4SLinus Torvalds break; 10781da177e4SLinus Torvalds case NETDEV_UP: 10791da177e4SLinus Torvalds if (dev->mtu < 68) 10801da177e4SLinus Torvalds break; 10810cc217e1SEric W. Biederman if (dev->flags & IFF_LOOPBACK) { 10821da177e4SLinus Torvalds struct in_ifaddr *ifa; 10831da177e4SLinus Torvalds if ((ifa = inet_alloc_ifa()) != NULL) { 10841da177e4SLinus Torvalds ifa->ifa_local = 10851da177e4SLinus Torvalds ifa->ifa_address = htonl(INADDR_LOOPBACK); 10861da177e4SLinus Torvalds ifa->ifa_prefixlen = 8; 10871da177e4SLinus Torvalds ifa->ifa_mask = inet_make_mask(8); 10881da177e4SLinus Torvalds in_dev_hold(in_dev); 10891da177e4SLinus Torvalds ifa->ifa_dev = in_dev; 10901da177e4SLinus Torvalds ifa->ifa_scope = RT_SCOPE_HOST; 10911da177e4SLinus Torvalds memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 10921da177e4SLinus Torvalds inet_insert_ifa(ifa); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds ip_mc_up(in_dev); 10961da177e4SLinus Torvalds break; 10971da177e4SLinus Torvalds case NETDEV_DOWN: 10981da177e4SLinus Torvalds ip_mc_down(in_dev); 10991da177e4SLinus Torvalds break; 11001da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 11011da177e4SLinus Torvalds if (dev->mtu >= 68) 11021da177e4SLinus Torvalds break; 11031da177e4SLinus Torvalds /* MTU falled under 68, disable IP */ 11041da177e4SLinus Torvalds case NETDEV_UNREGISTER: 11051da177e4SLinus Torvalds inetdev_destroy(in_dev); 11061da177e4SLinus Torvalds break; 11071da177e4SLinus Torvalds case NETDEV_CHANGENAME: 11081da177e4SLinus Torvalds /* Do not notify about label change, this event is 11091da177e4SLinus Torvalds * not interesting to applications using netlink. 11101da177e4SLinus Torvalds */ 11111da177e4SLinus Torvalds inetdev_changename(dev, in_dev); 11121da177e4SLinus Torvalds 111351602b2aSPavel Emelyanov devinet_sysctl_unregister(in_dev); 111466f27a52SPavel Emelyanov devinet_sysctl_register(in_dev); 11151da177e4SLinus Torvalds break; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds out: 11181da177e4SLinus Torvalds return NOTIFY_DONE; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds static struct notifier_block ip_netdev_notifier = { 11221da177e4SLinus Torvalds .notifier_call =inetdev_event, 11231da177e4SLinus Torvalds }; 11241da177e4SLinus Torvalds 1125339bf98fSThomas Graf static inline size_t inet_nlmsg_size(void) 1126339bf98fSThomas Graf { 1127339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 1128339bf98fSThomas Graf + nla_total_size(4) /* IFA_ADDRESS */ 1129339bf98fSThomas Graf + nla_total_size(4) /* IFA_LOCAL */ 1130339bf98fSThomas Graf + nla_total_size(4) /* IFA_BROADCAST */ 1131339bf98fSThomas Graf + nla_total_size(4) /* IFA_ANYCAST */ 1132339bf98fSThomas Graf + nla_total_size(IFNAMSIZ); /* IFA_LABEL */ 1133339bf98fSThomas Graf } 1134339bf98fSThomas Graf 11351da177e4SLinus Torvalds static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, 1136b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds struct ifaddrmsg *ifm; 11391da177e4SLinus Torvalds struct nlmsghdr *nlh; 11401da177e4SLinus Torvalds 114147f68512SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags); 114247f68512SThomas Graf if (nlh == NULL) 114326932566SPatrick McHardy return -EMSGSIZE; 114447f68512SThomas Graf 114547f68512SThomas Graf ifm = nlmsg_data(nlh); 11461da177e4SLinus Torvalds ifm->ifa_family = AF_INET; 11471da177e4SLinus Torvalds ifm->ifa_prefixlen = ifa->ifa_prefixlen; 11481da177e4SLinus Torvalds ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT; 11491da177e4SLinus Torvalds ifm->ifa_scope = ifa->ifa_scope; 11501da177e4SLinus Torvalds ifm->ifa_index = ifa->ifa_dev->dev->ifindex; 11511da177e4SLinus Torvalds 115247f68512SThomas Graf if (ifa->ifa_address) 1153a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address); 115447f68512SThomas Graf 115547f68512SThomas Graf if (ifa->ifa_local) 1156a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local); 115747f68512SThomas Graf 115847f68512SThomas Graf if (ifa->ifa_broadcast) 1159a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast); 116047f68512SThomas Graf 116147f68512SThomas Graf if (ifa->ifa_anycast) 1162a7a628c4SAl Viro NLA_PUT_BE32(skb, IFA_ANYCAST, ifa->ifa_anycast); 116347f68512SThomas Graf 116447f68512SThomas Graf if (ifa->ifa_label[0]) 116547f68512SThomas Graf NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label); 116647f68512SThomas Graf 116747f68512SThomas Graf return nlmsg_end(skb, nlh); 116847f68512SThomas Graf 116947f68512SThomas Graf nla_put_failure: 117026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 117126932566SPatrick McHardy return -EMSGSIZE; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 11751da177e4SLinus Torvalds { 1176b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 11771da177e4SLinus Torvalds int idx, ip_idx; 11781da177e4SLinus Torvalds struct net_device *dev; 11791da177e4SLinus Torvalds struct in_device *in_dev; 11801da177e4SLinus Torvalds struct in_ifaddr *ifa; 11811da177e4SLinus Torvalds int s_ip_idx, s_idx = cb->args[0]; 11821da177e4SLinus Torvalds 1183b854272bSDenis V. Lunev if (net != &init_net) 1184b854272bSDenis V. Lunev return 0; 1185b854272bSDenis V. Lunev 11861da177e4SLinus Torvalds s_ip_idx = ip_idx = cb->args[1]; 11877562f876SPavel Emelianov idx = 0; 1188881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 11891da177e4SLinus Torvalds if (idx < s_idx) 11907562f876SPavel Emelianov goto cont; 11911da177e4SLinus Torvalds if (idx > s_idx) 11921da177e4SLinus Torvalds s_ip_idx = 0; 11936313c1e0SPatrick McHardy if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) 11947562f876SPavel Emelianov goto cont; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds for (ifa = in_dev->ifa_list, ip_idx = 0; ifa; 11971da177e4SLinus Torvalds ifa = ifa->ifa_next, ip_idx++) { 11981da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 1199596e4150SStephen Hemminger continue; 12001da177e4SLinus Torvalds if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid, 12011da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 12026313c1e0SPatrick McHardy RTM_NEWADDR, NLM_F_MULTI) <= 0) 12031da177e4SLinus Torvalds goto done; 12041da177e4SLinus Torvalds } 12057562f876SPavel Emelianov cont: 12067562f876SPavel Emelianov idx++; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds done: 12101da177e4SLinus Torvalds cb->args[0] = idx; 12111da177e4SLinus Torvalds cb->args[1] = ip_idx; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds return skb->len; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 1216d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr* ifa, struct nlmsghdr *nlh, 1217d6062cbbSThomas Graf u32 pid) 12181da177e4SLinus Torvalds { 121947f68512SThomas Graf struct sk_buff *skb; 1220d6062cbbSThomas Graf u32 seq = nlh ? nlh->nlmsg_seq : 0; 1221d6062cbbSThomas Graf int err = -ENOBUFS; 12221da177e4SLinus Torvalds 1223339bf98fSThomas Graf skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); 122447f68512SThomas Graf if (skb == NULL) 1225d6062cbbSThomas Graf goto errout; 1226d6062cbbSThomas Graf 1227d6062cbbSThomas Graf err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0); 122826932566SPatrick McHardy if (err < 0) { 122926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ 123026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 123126932566SPatrick McHardy kfree_skb(skb); 123226932566SPatrick McHardy goto errout; 123326932566SPatrick McHardy } 123497c53cacSDenis V. Lunev err = rtnl_notify(skb, &init_net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); 1235d6062cbbSThomas Graf errout: 1236d6062cbbSThomas Graf if (err < 0) 123797c53cacSDenis V. Lunev rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_IFADDR, err); 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 12411da177e4SLinus Torvalds 1242c0ce9fb3SPavel Emelyanov static void devinet_copy_dflt_conf(struct net *net, int i) 124331be3085SHerbert Xu { 124431be3085SHerbert Xu struct net_device *dev; 124531be3085SHerbert Xu 124631be3085SHerbert Xu read_lock(&dev_base_lock); 1247c0ce9fb3SPavel Emelyanov for_each_netdev(net, dev) { 124831be3085SHerbert Xu struct in_device *in_dev; 124931be3085SHerbert Xu rcu_read_lock(); 125031be3085SHerbert Xu in_dev = __in_dev_get_rcu(dev); 125131be3085SHerbert Xu if (in_dev && !test_bit(i, in_dev->cnf.state)) 12529355bbd6SPavel Emelyanov in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; 125331be3085SHerbert Xu rcu_read_unlock(); 125431be3085SHerbert Xu } 125531be3085SHerbert Xu read_unlock(&dev_base_lock); 125631be3085SHerbert Xu } 125731be3085SHerbert Xu 1258c0ce9fb3SPavel Emelyanov static void inet_forward_change(struct net *net) 125968dd299bSPavel Emelyanov { 126068dd299bSPavel Emelyanov struct net_device *dev; 1261586f1211SPavel Emelyanov int on = IPV4_DEVCONF_ALL(net, FORWARDING); 126268dd299bSPavel Emelyanov 1263586f1211SPavel Emelyanov IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; 12649355bbd6SPavel Emelyanov IPV4_DEVCONF_DFLT(net, FORWARDING) = on; 126568dd299bSPavel Emelyanov 126668dd299bSPavel Emelyanov read_lock(&dev_base_lock); 1267c0ce9fb3SPavel Emelyanov for_each_netdev(net, dev) { 126868dd299bSPavel Emelyanov struct in_device *in_dev; 126968dd299bSPavel Emelyanov rcu_read_lock(); 127068dd299bSPavel Emelyanov in_dev = __in_dev_get_rcu(dev); 127168dd299bSPavel Emelyanov if (in_dev) 127268dd299bSPavel Emelyanov IN_DEV_CONF_SET(in_dev, FORWARDING, on); 127368dd299bSPavel Emelyanov rcu_read_unlock(); 127468dd299bSPavel Emelyanov } 127568dd299bSPavel Emelyanov read_unlock(&dev_base_lock); 127668dd299bSPavel Emelyanov 127768dd299bSPavel Emelyanov rt_cache_flush(0); 127868dd299bSPavel Emelyanov } 127968dd299bSPavel Emelyanov 128031be3085SHerbert Xu static int devinet_conf_proc(ctl_table *ctl, int write, 128131be3085SHerbert Xu struct file* filp, void __user *buffer, 128231be3085SHerbert Xu size_t *lenp, loff_t *ppos) 128331be3085SHerbert Xu { 128431be3085SHerbert Xu int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 128531be3085SHerbert Xu 128631be3085SHerbert Xu if (write) { 128731be3085SHerbert Xu struct ipv4_devconf *cnf = ctl->extra1; 1288c0ce9fb3SPavel Emelyanov struct net *net = ctl->extra2; 128931be3085SHerbert Xu int i = (int *)ctl->data - cnf->data; 129031be3085SHerbert Xu 129131be3085SHerbert Xu set_bit(i, cnf->state); 129231be3085SHerbert Xu 12939355bbd6SPavel Emelyanov if (cnf == net->ipv4.devconf_dflt) 1294c0ce9fb3SPavel Emelyanov devinet_copy_dflt_conf(net, i); 129531be3085SHerbert Xu } 129631be3085SHerbert Xu 129731be3085SHerbert Xu return ret; 129831be3085SHerbert Xu } 129931be3085SHerbert Xu 130031be3085SHerbert Xu static int devinet_conf_sysctl(ctl_table *table, int __user *name, int nlen, 130131be3085SHerbert Xu void __user *oldval, size_t __user *oldlenp, 130231be3085SHerbert Xu void __user *newval, size_t newlen) 130331be3085SHerbert Xu { 130431be3085SHerbert Xu struct ipv4_devconf *cnf; 1305c0ce9fb3SPavel Emelyanov struct net *net; 130631be3085SHerbert Xu int *valp = table->data; 130731be3085SHerbert Xu int new; 130831be3085SHerbert Xu int i; 130931be3085SHerbert Xu 131031be3085SHerbert Xu if (!newval || !newlen) 131131be3085SHerbert Xu return 0; 131231be3085SHerbert Xu 131331be3085SHerbert Xu if (newlen != sizeof(int)) 131431be3085SHerbert Xu return -EINVAL; 131531be3085SHerbert Xu 131631be3085SHerbert Xu if (get_user(new, (int __user *)newval)) 131731be3085SHerbert Xu return -EFAULT; 131831be3085SHerbert Xu 131931be3085SHerbert Xu if (new == *valp) 132031be3085SHerbert Xu return 0; 132131be3085SHerbert Xu 132231be3085SHerbert Xu if (oldval && oldlenp) { 132331be3085SHerbert Xu size_t len; 132431be3085SHerbert Xu 132531be3085SHerbert Xu if (get_user(len, oldlenp)) 132631be3085SHerbert Xu return -EFAULT; 132731be3085SHerbert Xu 132831be3085SHerbert Xu if (len) { 132931be3085SHerbert Xu if (len > table->maxlen) 133031be3085SHerbert Xu len = table->maxlen; 133131be3085SHerbert Xu if (copy_to_user(oldval, valp, len)) 133231be3085SHerbert Xu return -EFAULT; 133331be3085SHerbert Xu if (put_user(len, oldlenp)) 133431be3085SHerbert Xu return -EFAULT; 133531be3085SHerbert Xu } 133631be3085SHerbert Xu } 133731be3085SHerbert Xu 133831be3085SHerbert Xu *valp = new; 133931be3085SHerbert Xu 134031be3085SHerbert Xu cnf = table->extra1; 1341c0ce9fb3SPavel Emelyanov net = table->extra2; 134231be3085SHerbert Xu i = (int *)table->data - cnf->data; 134331be3085SHerbert Xu 134431be3085SHerbert Xu set_bit(i, cnf->state); 134531be3085SHerbert Xu 13469355bbd6SPavel Emelyanov if (cnf == net->ipv4.devconf_dflt) 1347c0ce9fb3SPavel Emelyanov devinet_copy_dflt_conf(net, i); 134831be3085SHerbert Xu 134931be3085SHerbert Xu return 1; 135031be3085SHerbert Xu } 135131be3085SHerbert Xu 13521da177e4SLinus Torvalds static int devinet_sysctl_forward(ctl_table *ctl, int write, 13531da177e4SLinus Torvalds struct file* filp, void __user *buffer, 13541da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds int *valp = ctl->data; 13571da177e4SLinus Torvalds int val = *valp; 13581da177e4SLinus Torvalds int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds if (write && *valp != val) { 1361c0ce9fb3SPavel Emelyanov struct net *net = ctl->extra2; 1362c0ce9fb3SPavel Emelyanov 1363586f1211SPavel Emelyanov if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) 1364c0ce9fb3SPavel Emelyanov inet_forward_change(net); 13659355bbd6SPavel Emelyanov else if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) 13661da177e4SLinus Torvalds rt_cache_flush(0); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds return ret; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds int ipv4_doint_and_flush(ctl_table *ctl, int write, 13731da177e4SLinus Torvalds struct file* filp, void __user *buffer, 13741da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 13751da177e4SLinus Torvalds { 13761da177e4SLinus Torvalds int *valp = ctl->data; 13771da177e4SLinus Torvalds int val = *valp; 13781da177e4SLinus Torvalds int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds if (write && *valp != val) 13811da177e4SLinus Torvalds rt_cache_flush(0); 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds return ret; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen, 13871da177e4SLinus Torvalds void __user *oldval, size_t __user *oldlenp, 13881f29bcd7SAlexey Dobriyan void __user *newval, size_t newlen) 13891da177e4SLinus Torvalds { 139031be3085SHerbert Xu int ret = devinet_conf_sysctl(table, name, nlen, oldval, oldlenp, 139131be3085SHerbert Xu newval, newlen); 13921da177e4SLinus Torvalds 139331be3085SHerbert Xu if (ret == 1) 13941da177e4SLinus Torvalds rt_cache_flush(0); 139531be3085SHerbert Xu 139631be3085SHerbert Xu return ret; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds 140042f811b8SHerbert Xu #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \ 140142f811b8SHerbert Xu { \ 140242f811b8SHerbert Xu .ctl_name = NET_IPV4_CONF_ ## attr, \ 140342f811b8SHerbert Xu .procname = name, \ 140442f811b8SHerbert Xu .data = ipv4_devconf.data + \ 140542f811b8SHerbert Xu NET_IPV4_CONF_ ## attr - 1, \ 140642f811b8SHerbert Xu .maxlen = sizeof(int), \ 140742f811b8SHerbert Xu .mode = mval, \ 140842f811b8SHerbert Xu .proc_handler = proc, \ 140942f811b8SHerbert Xu .strategy = sysctl, \ 141031be3085SHerbert Xu .extra1 = &ipv4_devconf, \ 141142f811b8SHerbert Xu } 141242f811b8SHerbert Xu 141342f811b8SHerbert Xu #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ 141431be3085SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \ 141531be3085SHerbert Xu devinet_conf_sysctl) 141642f811b8SHerbert Xu 141742f811b8SHerbert Xu #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ 141831be3085SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \ 141931be3085SHerbert Xu devinet_conf_sysctl) 142042f811b8SHerbert Xu 142142f811b8SHerbert Xu #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \ 142242f811b8SHerbert Xu DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl) 142342f811b8SHerbert Xu 142442f811b8SHerbert Xu #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ 142542f811b8SHerbert Xu DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \ 142642f811b8SHerbert Xu ipv4_doint_and_flush_strategy) 142742f811b8SHerbert Xu 14281da177e4SLinus Torvalds static struct devinet_sysctl_table { 14291da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 1430bfada697SPavel Emelyanov struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX]; 1431bfada697SPavel Emelyanov char *dev_name; 14321da177e4SLinus Torvalds } devinet_sysctl = { 14331da177e4SLinus Torvalds .devinet_vars = { 143442f811b8SHerbert Xu DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", 143531be3085SHerbert Xu devinet_sysctl_forward, 143631be3085SHerbert Xu devinet_conf_sysctl), 143742f811b8SHerbert Xu DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), 143842f811b8SHerbert Xu 143942f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), 144042f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), 144142f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), 144242f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), 144342f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), 144442f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, 144542f811b8SHerbert Xu "accept_source_route"), 144642f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), 144742f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), 144842f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), 144942f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), 145042f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), 145142f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), 145242f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), 145342f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), 145442f811b8SHerbert Xu DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), 145542f811b8SHerbert Xu 145642f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), 145742f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), 145842f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION, 145942f811b8SHerbert Xu "force_igmp_version"), 146042f811b8SHerbert Xu DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, 146142f811b8SHerbert Xu "promote_secondaries"), 14621da177e4SLinus Torvalds }, 14631da177e4SLinus Torvalds }; 14641da177e4SLinus Torvalds 1465ea40b324SPavel Emelyanov static int __devinet_sysctl_register(struct net *net, char *dev_name, 1466ea40b324SPavel Emelyanov int ctl_name, struct ipv4_devconf *p) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds int i; 14699fa89642SPavel Emelyanov struct devinet_sysctl_table *t; 14701da177e4SLinus Torvalds 1471bfada697SPavel Emelyanov #define DEVINET_CTL_PATH_DEV 3 1472bfada697SPavel Emelyanov 1473bfada697SPavel Emelyanov struct ctl_path devinet_ctl_path[] = { 1474bfada697SPavel Emelyanov { .procname = "net", .ctl_name = CTL_NET, }, 1475bfada697SPavel Emelyanov { .procname = "ipv4", .ctl_name = NET_IPV4, }, 1476bfada697SPavel Emelyanov { .procname = "conf", .ctl_name = NET_IPV4_CONF, }, 1477bfada697SPavel Emelyanov { /* to be set */ }, 1478bfada697SPavel Emelyanov { }, 1479bfada697SPavel Emelyanov }; 1480bfada697SPavel Emelyanov 14819fa89642SPavel Emelyanov t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL); 14821da177e4SLinus Torvalds if (!t) 14839fa89642SPavel Emelyanov goto out; 14849fa89642SPavel Emelyanov 14851da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) { 14861da177e4SLinus Torvalds t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; 148731be3085SHerbert Xu t->devinet_vars[i].extra1 = p; 1488c0ce9fb3SPavel Emelyanov t->devinet_vars[i].extra2 = net; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds /* 14921da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 14931da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 14941da177e4SLinus Torvalds * (see SIOCSIFNAME). 14951da177e4SLinus Torvalds */ 1496bfada697SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 1497bfada697SPavel Emelyanov if (!t->dev_name) 14981da177e4SLinus Torvalds goto free; 14991da177e4SLinus Torvalds 1500bfada697SPavel Emelyanov devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name; 1501bfada697SPavel Emelyanov devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name; 15021da177e4SLinus Torvalds 1503752d14dcSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path, 1504bfada697SPavel Emelyanov t->devinet_vars); 15051da177e4SLinus Torvalds if (!t->sysctl_header) 15061da177e4SLinus Torvalds goto free_procname; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds p->sysctl = t; 1509ea40b324SPavel Emelyanov return 0; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds free_procname: 1512bfada697SPavel Emelyanov kfree(t->dev_name); 15131da177e4SLinus Torvalds free: 15141da177e4SLinus Torvalds kfree(t); 15159fa89642SPavel Emelyanov out: 1516ea40b324SPavel Emelyanov return -ENOBUFS; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 151951602b2aSPavel Emelyanov static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf) 152066f27a52SPavel Emelyanov { 152151602b2aSPavel Emelyanov struct devinet_sysctl_table *t = cnf->sysctl; 152266f27a52SPavel Emelyanov 152351602b2aSPavel Emelyanov if (t == NULL) 152451602b2aSPavel Emelyanov return; 152551602b2aSPavel Emelyanov 152651602b2aSPavel Emelyanov cnf->sysctl = NULL; 15271da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 1528bfada697SPavel Emelyanov kfree(t->dev_name); 15291da177e4SLinus Torvalds kfree(t); 15301da177e4SLinus Torvalds } 153151602b2aSPavel Emelyanov 153251602b2aSPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev) 153351602b2aSPavel Emelyanov { 153451602b2aSPavel Emelyanov neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4, 153551602b2aSPavel Emelyanov NET_IPV4_NEIGH, "ipv4", NULL, NULL); 1536c0ce9fb3SPavel Emelyanov __devinet_sysctl_register(idev->dev->nd_net, idev->dev->name, 1537c0ce9fb3SPavel Emelyanov idev->dev->ifindex, &idev->cnf); 153851602b2aSPavel Emelyanov } 153951602b2aSPavel Emelyanov 154051602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev) 154151602b2aSPavel Emelyanov { 154251602b2aSPavel Emelyanov __devinet_sysctl_unregister(&idev->cnf); 154351602b2aSPavel Emelyanov neigh_sysctl_unregister(idev->arp_parms); 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds #endif 15461da177e4SLinus Torvalds 154768dd299bSPavel Emelyanov static struct ctl_table ctl_forward_entry[] = { 154868dd299bSPavel Emelyanov { 154968dd299bSPavel Emelyanov .ctl_name = NET_IPV4_FORWARD, 155068dd299bSPavel Emelyanov .procname = "ip_forward", 155168dd299bSPavel Emelyanov .data = &ipv4_devconf.data[ 155268dd299bSPavel Emelyanov NET_IPV4_CONF_FORWARDING - 1], 155368dd299bSPavel Emelyanov .maxlen = sizeof(int), 155468dd299bSPavel Emelyanov .mode = 0644, 155568dd299bSPavel Emelyanov .proc_handler = devinet_sysctl_forward, 155668dd299bSPavel Emelyanov .strategy = devinet_conf_sysctl, 155768dd299bSPavel Emelyanov .extra1 = &ipv4_devconf, 1558c0ce9fb3SPavel Emelyanov .extra2 = &init_net, 155968dd299bSPavel Emelyanov }, 156068dd299bSPavel Emelyanov { }, 156168dd299bSPavel Emelyanov }; 156268dd299bSPavel Emelyanov 1563752d14dcSPavel Emelyanov static __net_initdata struct ctl_path net_ipv4_path[] = { 156468dd299bSPavel Emelyanov { .procname = "net", .ctl_name = CTL_NET, }, 156568dd299bSPavel Emelyanov { .procname = "ipv4", .ctl_name = NET_IPV4, }, 156668dd299bSPavel Emelyanov { }, 156768dd299bSPavel Emelyanov }; 156868dd299bSPavel Emelyanov 1569752d14dcSPavel Emelyanov static __net_init int devinet_init_net(struct net *net) 1570752d14dcSPavel Emelyanov { 1571752d14dcSPavel Emelyanov int err; 1572752d14dcSPavel Emelyanov struct ctl_table *tbl; 1573752d14dcSPavel Emelyanov struct ipv4_devconf *all, *dflt; 1574752d14dcSPavel Emelyanov struct ctl_table_header *forw_hdr; 1575752d14dcSPavel Emelyanov 1576752d14dcSPavel Emelyanov err = -ENOMEM; 1577752d14dcSPavel Emelyanov all = &ipv4_devconf; 1578752d14dcSPavel Emelyanov dflt = &ipv4_devconf_dflt; 1579752d14dcSPavel Emelyanov tbl = ctl_forward_entry; 1580752d14dcSPavel Emelyanov 1581752d14dcSPavel Emelyanov if (net != &init_net) { 1582752d14dcSPavel Emelyanov all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL); 1583752d14dcSPavel Emelyanov if (all == NULL) 1584752d14dcSPavel Emelyanov goto err_alloc_all; 1585752d14dcSPavel Emelyanov 1586752d14dcSPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); 1587752d14dcSPavel Emelyanov if (dflt == NULL) 1588752d14dcSPavel Emelyanov goto err_alloc_dflt; 1589752d14dcSPavel Emelyanov 1590752d14dcSPavel Emelyanov tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL); 1591752d14dcSPavel Emelyanov if (tbl == NULL) 1592752d14dcSPavel Emelyanov goto err_alloc_ctl; 1593752d14dcSPavel Emelyanov 1594752d14dcSPavel Emelyanov tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1]; 1595752d14dcSPavel Emelyanov tbl[0].extra1 = all; 1596752d14dcSPavel Emelyanov tbl[0].extra2 = net; 1597752d14dcSPavel Emelyanov } 1598752d14dcSPavel Emelyanov 1599752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL 1600752d14dcSPavel Emelyanov err = __devinet_sysctl_register(net, "all", 1601752d14dcSPavel Emelyanov NET_PROTO_CONF_ALL, all); 1602752d14dcSPavel Emelyanov if (err < 0) 1603752d14dcSPavel Emelyanov goto err_reg_all; 1604752d14dcSPavel Emelyanov 1605752d14dcSPavel Emelyanov err = __devinet_sysctl_register(net, "default", 1606752d14dcSPavel Emelyanov NET_PROTO_CONF_DEFAULT, dflt); 1607752d14dcSPavel Emelyanov if (err < 0) 1608752d14dcSPavel Emelyanov goto err_reg_dflt; 1609752d14dcSPavel Emelyanov 1610752d14dcSPavel Emelyanov err = -ENOMEM; 1611752d14dcSPavel Emelyanov forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl); 1612752d14dcSPavel Emelyanov if (forw_hdr == NULL) 1613752d14dcSPavel Emelyanov goto err_reg_ctl; 1614752d14dcSPavel Emelyanov #endif 1615752d14dcSPavel Emelyanov 1616752d14dcSPavel Emelyanov net->ipv4.forw_hdr = forw_hdr; 1617752d14dcSPavel Emelyanov net->ipv4.devconf_all = all; 1618752d14dcSPavel Emelyanov net->ipv4.devconf_dflt = dflt; 1619752d14dcSPavel Emelyanov return 0; 1620752d14dcSPavel Emelyanov 1621752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL 1622752d14dcSPavel Emelyanov err_reg_ctl: 1623752d14dcSPavel Emelyanov __devinet_sysctl_unregister(dflt); 1624752d14dcSPavel Emelyanov err_reg_dflt: 1625752d14dcSPavel Emelyanov __devinet_sysctl_unregister(all); 1626752d14dcSPavel Emelyanov err_reg_all: 1627752d14dcSPavel Emelyanov if (tbl != ctl_forward_entry) 1628752d14dcSPavel Emelyanov kfree(tbl); 1629752d14dcSPavel Emelyanov #endif 1630752d14dcSPavel Emelyanov err_alloc_ctl: 1631752d14dcSPavel Emelyanov if (dflt != &ipv4_devconf_dflt) 1632752d14dcSPavel Emelyanov kfree(dflt); 1633752d14dcSPavel Emelyanov err_alloc_dflt: 1634752d14dcSPavel Emelyanov if (all != &ipv4_devconf) 1635752d14dcSPavel Emelyanov kfree(all); 1636752d14dcSPavel Emelyanov err_alloc_all: 1637752d14dcSPavel Emelyanov return err; 1638752d14dcSPavel Emelyanov } 1639752d14dcSPavel Emelyanov 1640752d14dcSPavel Emelyanov static __net_exit void devinet_exit_net(struct net *net) 1641752d14dcSPavel Emelyanov { 1642752d14dcSPavel Emelyanov struct ctl_table *tbl; 1643752d14dcSPavel Emelyanov 1644752d14dcSPavel Emelyanov tbl = net->ipv4.forw_hdr->ctl_table_arg; 1645752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL 1646752d14dcSPavel Emelyanov unregister_net_sysctl_table(net->ipv4.forw_hdr); 1647752d14dcSPavel Emelyanov __devinet_sysctl_unregister(net->ipv4.devconf_dflt); 1648752d14dcSPavel Emelyanov __devinet_sysctl_unregister(net->ipv4.devconf_all); 1649752d14dcSPavel Emelyanov #endif 1650752d14dcSPavel Emelyanov kfree(tbl); 1651752d14dcSPavel Emelyanov kfree(net->ipv4.devconf_dflt); 1652752d14dcSPavel Emelyanov kfree(net->ipv4.devconf_all); 1653752d14dcSPavel Emelyanov } 1654752d14dcSPavel Emelyanov 1655752d14dcSPavel Emelyanov static __net_initdata struct pernet_operations devinet_ops = { 1656752d14dcSPavel Emelyanov .init = devinet_init_net, 1657752d14dcSPavel Emelyanov .exit = devinet_exit_net, 1658752d14dcSPavel Emelyanov }; 1659752d14dcSPavel Emelyanov 16601da177e4SLinus Torvalds void __init devinet_init(void) 16611da177e4SLinus Torvalds { 1662752d14dcSPavel Emelyanov register_pernet_subsys(&devinet_ops); 1663752d14dcSPavel Emelyanov 16641da177e4SLinus Torvalds register_gifconf(PF_INET, inet_gifconf); 16651da177e4SLinus Torvalds register_netdevice_notifier(&ip_netdev_notifier); 166663f3444fSThomas Graf 166763f3444fSThomas Graf rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL); 166863f3444fSThomas Graf rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL); 166963f3444fSThomas Graf rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds EXPORT_SYMBOL(in_dev_finish_destroy); 16731da177e4SLinus Torvalds EXPORT_SYMBOL(inet_select_addr); 16741da177e4SLinus Torvalds EXPORT_SYMBOL(inetdev_by_index); 16751da177e4SLinus Torvalds EXPORT_SYMBOL(register_inetaddr_notifier); 16761da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_inetaddr_notifier); 1677