Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NET3 IP device support routines. |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Derived from the IP parts of dev.c 1.0.19 |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 10 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 12 | * Mark Evans, <evansmp@uhura.aston.ac.uk> |
| 13 | * |
| 14 | * Additional Authors: |
| 15 | * Alan Cox, <gw4pts@gw4pts.ampr.org> |
| 16 | * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 17 | * |
| 18 | * Changes: |
| 19 | * Alexey Kuznetsov: pa_* fields are replaced with ifaddr |
| 20 | * lists. |
| 21 | * Cyrus Durgin: updated for kmod |
| 22 | * Matthias Andree: in devinet_ioctl, compare label and |
| 23 | * address (4.4BSD alias style support), |
| 24 | * fall back to comparing just the label |
| 25 | * if no match found. |
| 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/system.h> |
| 31 | #include <linux/bitops.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/module.h> |
| 34 | #include <linux/types.h> |
| 35 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/string.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/socket.h> |
| 39 | #include <linux/sockios.h> |
| 40 | #include <linux/in.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/interrupt.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 43 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/if_ether.h> |
| 45 | #include <linux/inet.h> |
| 46 | #include <linux/netdevice.h> |
| 47 | #include <linux/etherdevice.h> |
| 48 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/init.h> |
| 50 | #include <linux/notifier.h> |
| 51 | #include <linux/inetdevice.h> |
| 52 | #include <linux/igmp.h> |
| 53 | #ifdef CONFIG_SYSCTL |
| 54 | #include <linux/sysctl.h> |
| 55 | #endif |
| 56 | #include <linux/kmod.h> |
| 57 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 58 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <net/ip.h> |
| 60 | #include <net/route.h> |
| 61 | #include <net/ip_fib.h> |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 62 | #include <net/rtnetlink.h> |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 63 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Adrian Bunk | 0027ba8 | 2008-01-31 17:17:31 -0800 | [diff] [blame] | 65 | static struct ipv4_devconf ipv4_devconf = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 66 | .data = { |
| 67 | [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, |
| 68 | [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, |
| 69 | [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, |
| 70 | [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, |
| 71 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static struct ipv4_devconf ipv4_devconf_dflt = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 75 | .data = { |
| 76 | [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1, |
| 77 | [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1, |
| 78 | [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1, |
| 79 | [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1, |
| 80 | [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1, |
| 81 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 84 | #define IPV4_DEVCONF_DFLT(net, attr) \ |
| 85 | IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 86 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 87 | static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 88 | [IFA_LOCAL] = { .type = NLA_U32 }, |
| 89 | [IFA_ADDRESS] = { .type = NLA_U32 }, |
| 90 | [IFA_BROADCAST] = { .type = NLA_U32 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 91 | [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 94 | static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 96 | static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
| 98 | int destroy); |
| 99 | #ifdef CONFIG_SYSCTL |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 100 | static void devinet_sysctl_register(struct in_device *idev); |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 101 | static void devinet_sysctl_unregister(struct in_device *idev); |
| 102 | #else |
| 103 | static inline void devinet_sysctl_register(struct in_device *idev) |
| 104 | { |
| 105 | } |
| 106 | static inline void devinet_sysctl_unregister(struct in_device *idev) |
| 107 | { |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | /* Locks all the inet devices. */ |
| 112 | |
| 113 | static struct in_ifaddr *inet_alloc_ifa(void) |
| 114 | { |
Alexey Dobriyan | 93adcc8 | 2008-10-28 13:25:09 -0700 | [diff] [blame] | 115 | return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void inet_rcu_free_ifa(struct rcu_head *head) |
| 119 | { |
| 120 | struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); |
| 121 | if (ifa->ifa_dev) |
| 122 | in_dev_put(ifa->ifa_dev); |
| 123 | kfree(ifa); |
| 124 | } |
| 125 | |
| 126 | static inline void inet_free_ifa(struct in_ifaddr *ifa) |
| 127 | { |
| 128 | call_rcu(&ifa->rcu_head, inet_rcu_free_ifa); |
| 129 | } |
| 130 | |
| 131 | void in_dev_finish_destroy(struct in_device *idev) |
| 132 | { |
| 133 | struct net_device *dev = idev->dev; |
| 134 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 135 | WARN_ON(idev->ifa_list); |
| 136 | WARN_ON(idev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #ifdef NET_REFCNT_DEBUG |
| 138 | printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n", |
| 139 | idev, dev ? dev->name : "NIL"); |
| 140 | #endif |
| 141 | dev_put(dev); |
| 142 | if (!idev->dead) |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 143 | pr_err("Freeing alive in_device %p\n", idev); |
| 144 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | kfree(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 147 | EXPORT_SYMBOL(in_dev_finish_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 149 | static struct in_device *inetdev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct in_device *in_dev; |
| 152 | |
| 153 | ASSERT_RTNL(); |
| 154 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 155 | in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (!in_dev) |
| 157 | goto out; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 158 | memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt, |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 159 | sizeof(in_dev->cnf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | in_dev->cnf.sysctl = NULL; |
| 161 | in_dev->dev = dev; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 162 | in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl); |
| 163 | if (!in_dev->arp_parms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | goto out_kfree; |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 165 | if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) |
| 166 | dev_disable_lro(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* Reference in_dev->dev */ |
| 168 | dev_hold(dev); |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 169 | /* Account for reference dev->ip_ptr (below) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | in_dev_hold(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 172 | devinet_sysctl_register(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | ip_mc_init_dev(in_dev); |
| 174 | if (dev->flags & IFF_UP) |
| 175 | ip_mc_up(in_dev); |
Jarek Poplawski | 483479e | 2007-01-09 14:38:31 -0800 | [diff] [blame] | 176 | |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 177 | /* we can receive as soon as ip_ptr is set -- do this last */ |
| 178 | rcu_assign_pointer(dev->ip_ptr, in_dev); |
Jarek Poplawski | 483479e | 2007-01-09 14:38:31 -0800 | [diff] [blame] | 179 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return in_dev; |
| 181 | out_kfree: |
| 182 | kfree(in_dev); |
| 183 | in_dev = NULL; |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | static void in_dev_rcu_put(struct rcu_head *head) |
| 188 | { |
| 189 | struct in_device *idev = container_of(head, struct in_device, rcu_head); |
| 190 | in_dev_put(idev); |
| 191 | } |
| 192 | |
| 193 | static void inetdev_destroy(struct in_device *in_dev) |
| 194 | { |
| 195 | struct in_ifaddr *ifa; |
| 196 | struct net_device *dev; |
| 197 | |
| 198 | ASSERT_RTNL(); |
| 199 | |
| 200 | dev = in_dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | in_dev->dead = 1; |
| 203 | |
| 204 | ip_mc_destroy_dev(in_dev); |
| 205 | |
| 206 | while ((ifa = in_dev->ifa_list) != NULL) { |
| 207 | inet_del_ifa(in_dev, &in_dev->ifa_list, 0); |
| 208 | inet_free_ifa(ifa); |
| 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | dev->ip_ptr = NULL; |
| 212 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 213 | devinet_sysctl_unregister(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | neigh_parms_release(&arp_tbl, in_dev->arp_parms); |
| 215 | arp_ifdown(dev); |
| 216 | |
| 217 | call_rcu(&in_dev->rcu_head, in_dev_rcu_put); |
| 218 | } |
| 219 | |
Al Viro | ff428d7 | 2006-09-26 22:13:35 -0700 | [diff] [blame] | 220 | int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | rcu_read_lock(); |
| 223 | for_primary_ifa(in_dev) { |
| 224 | if (inet_ifa_match(a, ifa)) { |
| 225 | if (!b || inet_ifa_match(b, ifa)) { |
| 226 | rcu_read_unlock(); |
| 227 | return 1; |
| 228 | } |
| 229 | } |
| 230 | } endfor_ifa(in_dev); |
| 231 | rcu_read_unlock(); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 235 | static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
| 236 | int destroy, struct nlmsghdr *nlh, u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 238 | struct in_ifaddr *promote = NULL; |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 239 | struct in_ifaddr *ifa, *ifa1 = *ifap; |
| 240 | struct in_ifaddr *last_prim = in_dev->ifa_list; |
| 241 | struct in_ifaddr *prev_prom = NULL; |
| 242 | int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | ASSERT_RTNL(); |
| 245 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 246 | /* 1. Deleting primary ifaddr forces deletion all secondaries |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 247 | * unless alias promotion is set |
| 248 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | struct in_ifaddr **ifap1 = &ifa1->ifa_next; |
| 252 | |
| 253 | while ((ifa = *ifap1) != NULL) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 254 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) && |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 255 | ifa1->ifa_scope <= ifa->ifa_scope) |
| 256 | last_prim = ifa; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) || |
| 259 | ifa1->ifa_mask != ifa->ifa_mask || |
| 260 | !inet_ifa_match(ifa1->ifa_address, ifa)) { |
| 261 | ifap1 = &ifa->ifa_next; |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 262 | prev_prom = ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 266 | if (!do_promote) { |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 267 | *ifap1 = ifa->ifa_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 269 | rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 270 | blocking_notifier_call_chain(&inetaddr_chain, |
| 271 | NETDEV_DOWN, ifa); |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 272 | inet_free_ifa(ifa); |
| 273 | } else { |
| 274 | promote = ifa; |
| 275 | break; |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | /* 2. Unlink it */ |
| 281 | |
| 282 | *ifap = ifa1->ifa_next; |
| 283 | |
| 284 | /* 3. Announce address deletion */ |
| 285 | |
| 286 | /* Send message first, then call notifier. |
| 287 | At first sight, FIB update triggered by notifier |
| 288 | will refer to already deleted ifaddr, that could confuse |
| 289 | netlink listeners. It is not true: look, gated sees |
| 290 | that route deleted and if it still thinks that ifaddr |
| 291 | is valid, it will try to restore deleted routes... Grr. |
| 292 | So that, this order is correct. |
| 293 | */ |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 294 | rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 295 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 296 | |
| 297 | if (promote) { |
| 298 | |
| 299 | if (prev_prom) { |
| 300 | prev_prom->ifa_next = promote->ifa_next; |
| 301 | promote->ifa_next = last_prim->ifa_next; |
| 302 | last_prim->ifa_next = promote; |
| 303 | } |
| 304 | |
| 305 | promote->ifa_flags &= ~IFA_F_SECONDARY; |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 306 | rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 307 | blocking_notifier_call_chain(&inetaddr_chain, |
| 308 | NETDEV_UP, promote); |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 309 | for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) { |
| 310 | if (ifa1->ifa_mask != ifa->ifa_mask || |
| 311 | !inet_ifa_match(ifa1->ifa_address, ifa)) |
| 312 | continue; |
| 313 | fib_add_ifaddr(ifa); |
| 314 | } |
| 315 | |
| 316 | } |
Herbert Xu | 6363097 | 2007-06-07 18:35:38 -0700 | [diff] [blame] | 317 | if (destroy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | inet_free_ifa(ifa1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 321 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
| 322 | int destroy) |
| 323 | { |
| 324 | __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); |
| 325 | } |
| 326 | |
| 327 | static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
| 328 | u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | struct in_device *in_dev = ifa->ifa_dev; |
| 331 | struct in_ifaddr *ifa1, **ifap, **last_primary; |
| 332 | |
| 333 | ASSERT_RTNL(); |
| 334 | |
| 335 | if (!ifa->ifa_local) { |
| 336 | inet_free_ifa(ifa); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 341 | last_primary = &in_dev->ifa_list; |
| 342 | |
| 343 | for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; |
| 344 | ifap = &ifa1->ifa_next) { |
| 345 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && |
| 346 | ifa->ifa_scope <= ifa1->ifa_scope) |
| 347 | last_primary = &ifa1->ifa_next; |
| 348 | if (ifa1->ifa_mask == ifa->ifa_mask && |
| 349 | inet_ifa_match(ifa1->ifa_address, ifa)) { |
| 350 | if (ifa1->ifa_local == ifa->ifa_local) { |
| 351 | inet_free_ifa(ifa); |
| 352 | return -EEXIST; |
| 353 | } |
| 354 | if (ifa1->ifa_scope != ifa->ifa_scope) { |
| 355 | inet_free_ifa(ifa); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | ifa->ifa_flags |= IFA_F_SECONDARY; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) { |
| 363 | net_srandom(ifa->ifa_local); |
| 364 | ifap = last_primary; |
| 365 | } |
| 366 | |
| 367 | ifa->ifa_next = *ifap; |
| 368 | *ifap = ifa; |
| 369 | |
| 370 | /* Send message first, then call notifier. |
| 371 | Notifier will trigger FIB update, so that |
| 372 | listeners of netlink will know about new ifaddr */ |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 373 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 374 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 379 | static int inet_insert_ifa(struct in_ifaddr *ifa) |
| 380 | { |
| 381 | return __inet_insert_ifa(ifa, NULL, 0); |
| 382 | } |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) |
| 385 | { |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 386 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | ASSERT_RTNL(); |
| 389 | |
| 390 | if (!in_dev) { |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 391 | inet_free_ifa(ifa); |
| 392 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 394 | ipv4_devconf_setall(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (ifa->ifa_dev != in_dev) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 396 | WARN_ON(ifa->ifa_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | in_dev_hold(in_dev); |
| 398 | ifa->ifa_dev = in_dev; |
| 399 | } |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 400 | if (ipv4_is_loopback(ifa->ifa_local)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | ifa->ifa_scope = RT_SCOPE_HOST; |
| 402 | return inet_insert_ifa(ifa); |
| 403 | } |
| 404 | |
Denis V. Lunev | 7fee0ca | 2008-01-21 17:32:38 -0800 | [diff] [blame] | 405 | struct in_device *inetdev_by_index(struct net *net, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | struct net_device *dev; |
| 408 | struct in_device *in_dev = NULL; |
Eric Dumazet | c148fc2 | 2009-11-01 19:23:04 +0000 | [diff] [blame] | 409 | |
| 410 | rcu_read_lock(); |
| 411 | dev = dev_get_by_index_rcu(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (dev) |
| 413 | in_dev = in_dev_get(dev); |
Eric Dumazet | c148fc2 | 2009-11-01 19:23:04 +0000 | [diff] [blame] | 414 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return in_dev; |
| 416 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 417 | EXPORT_SYMBOL(inetdev_by_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | /* Called only from RTNL semaphored context. No locks. */ |
| 420 | |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 421 | struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, |
| 422 | __be32 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | ASSERT_RTNL(); |
| 425 | |
| 426 | for_primary_ifa(in_dev) { |
| 427 | if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) |
| 428 | return ifa; |
| 429 | } endfor_ifa(in_dev); |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 434 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 435 | struct net *net = sock_net(skb->sk); |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 436 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | struct in_device *in_dev; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 438 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | struct in_ifaddr *ifa, **ifap; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 440 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | ASSERT_RTNL(); |
| 443 | |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 444 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); |
| 445 | if (err < 0) |
| 446 | goto errout; |
| 447 | |
| 448 | ifm = nlmsg_data(nlh); |
Denis V. Lunev | 7fee0ca | 2008-01-21 17:32:38 -0800 | [diff] [blame] | 449 | in_dev = inetdev_by_index(net, ifm->ifa_index); |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 450 | if (in_dev == NULL) { |
| 451 | err = -ENODEV; |
| 452 | goto errout; |
| 453 | } |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | __in_dev_put(in_dev); |
| 456 | |
| 457 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 458 | ifap = &ifa->ifa_next) { |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 459 | if (tb[IFA_LOCAL] && |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 460 | ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | continue; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 462 | |
| 463 | if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) |
| 464 | continue; |
| 465 | |
| 466 | if (tb[IFA_ADDRESS] && |
| 467 | (ifm->ifa_prefixlen != ifa->ifa_prefixlen || |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 468 | !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 469 | continue; |
| 470 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 471 | __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | return 0; |
| 473 | } |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 474 | |
| 475 | err = -EADDRNOTAVAIL; |
| 476 | errout: |
| 477 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 480 | static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 482 | struct nlattr *tb[IFA_MAX+1]; |
| 483 | struct in_ifaddr *ifa; |
| 484 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | struct net_device *dev; |
| 486 | struct in_device *in_dev; |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 487 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 489 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); |
| 490 | if (err < 0) |
| 491 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 493 | ifm = nlmsg_data(nlh); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 494 | err = -EINVAL; |
| 495 | if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 496 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 498 | dev = __dev_get_by_index(net, ifm->ifa_index); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 499 | err = -ENODEV; |
| 500 | if (dev == NULL) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 501 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 503 | in_dev = __in_dev_get_rtnl(dev); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 504 | err = -ENOBUFS; |
| 505 | if (in_dev == NULL) |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 506 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 508 | ifa = inet_alloc_ifa(); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 509 | if (ifa == NULL) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 510 | /* |
| 511 | * A potential indev allocation can be left alive, it stays |
| 512 | * assigned to its device and is destroy with it. |
| 513 | */ |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 514 | goto errout; |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 515 | |
Pavel Emelyanov | a4e65d3 | 2007-12-07 23:55:43 -0800 | [diff] [blame] | 516 | ipv4_devconf_setall(in_dev); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 517 | in_dev_hold(in_dev); |
| 518 | |
| 519 | if (tb[IFA_ADDRESS] == NULL) |
| 520 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | ifa->ifa_prefixlen = ifm->ifa_prefixlen; |
| 523 | ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | ifa->ifa_flags = ifm->ifa_flags; |
| 525 | ifa->ifa_scope = ifm->ifa_scope; |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 526 | ifa->ifa_dev = in_dev; |
| 527 | |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 528 | ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); |
| 529 | ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 530 | |
| 531 | if (tb[IFA_BROADCAST]) |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 532 | ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 533 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 534 | if (tb[IFA_LABEL]) |
| 535 | nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | else |
| 537 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 538 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 539 | return ifa; |
| 540 | |
| 541 | errout: |
| 542 | return ERR_PTR(err); |
| 543 | } |
| 544 | |
| 545 | static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 546 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 547 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 548 | struct in_ifaddr *ifa; |
| 549 | |
| 550 | ASSERT_RTNL(); |
| 551 | |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 552 | ifa = rtm_to_ifaddr(net, nlh); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 553 | if (IS_ERR(ifa)) |
| 554 | return PTR_ERR(ifa); |
| 555 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 556 | return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Determine a default network mask, based on the IP address. |
| 561 | */ |
| 562 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 563 | static inline int inet_abc_len(__be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | int rc = -1; /* Something else, probably a multicast. */ |
| 566 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 567 | if (ipv4_is_zeronet(addr)) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 568 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | else { |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 570 | __u32 haddr = ntohl(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 572 | if (IN_CLASSA(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | rc = 8; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 574 | else if (IN_CLASSB(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | rc = 16; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 576 | else if (IN_CLASSC(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | rc = 24; |
| 578 | } |
| 579 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 580 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
Denis V. Lunev | e5b13cb | 2008-02-28 20:51:43 -0800 | [diff] [blame] | 584 | int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
| 586 | struct ifreq ifr; |
| 587 | struct sockaddr_in sin_orig; |
| 588 | struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; |
| 589 | struct in_device *in_dev; |
| 590 | struct in_ifaddr **ifap = NULL; |
| 591 | struct in_ifaddr *ifa = NULL; |
| 592 | struct net_device *dev; |
| 593 | char *colon; |
| 594 | int ret = -EFAULT; |
| 595 | int tryaddrmatch = 0; |
| 596 | |
| 597 | /* |
| 598 | * Fetch the caller's info block into kernel space |
| 599 | */ |
| 600 | |
| 601 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) |
| 602 | goto out; |
| 603 | ifr.ifr_name[IFNAMSIZ - 1] = 0; |
| 604 | |
| 605 | /* save original address for comparison */ |
| 606 | memcpy(&sin_orig, sin, sizeof(*sin)); |
| 607 | |
| 608 | colon = strchr(ifr.ifr_name, ':'); |
| 609 | if (colon) |
| 610 | *colon = 0; |
| 611 | |
Denis V. Lunev | e5b13cb | 2008-02-28 20:51:43 -0800 | [diff] [blame] | 612 | dev_load(net, ifr.ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 614 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | case SIOCGIFADDR: /* Get interface address */ |
| 616 | case SIOCGIFBRDADDR: /* Get the broadcast address */ |
| 617 | case SIOCGIFDSTADDR: /* Get the destination address */ |
| 618 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ |
| 619 | /* Note that these ioctls will not sleep, |
| 620 | so that we do not impose a lock. |
| 621 | One day we will be forced to put shlock here (I mean SMP) |
| 622 | */ |
| 623 | tryaddrmatch = (sin_orig.sin_family == AF_INET); |
| 624 | memset(sin, 0, sizeof(*sin)); |
| 625 | sin->sin_family = AF_INET; |
| 626 | break; |
| 627 | |
| 628 | case SIOCSIFFLAGS: |
| 629 | ret = -EACCES; |
| 630 | if (!capable(CAP_NET_ADMIN)) |
| 631 | goto out; |
| 632 | break; |
| 633 | case SIOCSIFADDR: /* Set interface address (and family) */ |
| 634 | case SIOCSIFBRDADDR: /* Set the broadcast address */ |
| 635 | case SIOCSIFDSTADDR: /* Set the destination address */ |
| 636 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ |
| 637 | ret = -EACCES; |
| 638 | if (!capable(CAP_NET_ADMIN)) |
| 639 | goto out; |
| 640 | ret = -EINVAL; |
| 641 | if (sin->sin_family != AF_INET) |
| 642 | goto out; |
| 643 | break; |
| 644 | default: |
| 645 | ret = -EINVAL; |
| 646 | goto out; |
| 647 | } |
| 648 | |
| 649 | rtnl_lock(); |
| 650 | |
| 651 | ret = -ENODEV; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 652 | dev = __dev_get_by_name(net, ifr.ifr_name); |
| 653 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | goto done; |
| 655 | |
| 656 | if (colon) |
| 657 | *colon = ':'; |
| 658 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 659 | in_dev = __in_dev_get_rtnl(dev); |
| 660 | if (in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if (tryaddrmatch) { |
| 662 | /* Matthias Andree */ |
| 663 | /* compare label and address (4.4BSD style) */ |
| 664 | /* note: we only do this for a limited set of ioctls |
| 665 | and only if the original address family was AF_INET. |
| 666 | This is checked above. */ |
| 667 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 668 | ifap = &ifa->ifa_next) { |
| 669 | if (!strcmp(ifr.ifr_name, ifa->ifa_label) && |
| 670 | sin_orig.sin_addr.s_addr == |
| 671 | ifa->ifa_address) { |
| 672 | break; /* found */ |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | /* we didn't get a match, maybe the application is |
| 677 | 4.3BSD-style and passed in junk so we fall back to |
| 678 | comparing just the label */ |
| 679 | if (!ifa) { |
| 680 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 681 | ifap = &ifa->ifa_next) |
| 682 | if (!strcmp(ifr.ifr_name, ifa->ifa_label)) |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | ret = -EADDRNOTAVAIL; |
| 688 | if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) |
| 689 | goto done; |
| 690 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 691 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | case SIOCGIFADDR: /* Get interface address */ |
| 693 | sin->sin_addr.s_addr = ifa->ifa_local; |
| 694 | goto rarok; |
| 695 | |
| 696 | case SIOCGIFBRDADDR: /* Get the broadcast address */ |
| 697 | sin->sin_addr.s_addr = ifa->ifa_broadcast; |
| 698 | goto rarok; |
| 699 | |
| 700 | case SIOCGIFDSTADDR: /* Get the destination address */ |
| 701 | sin->sin_addr.s_addr = ifa->ifa_address; |
| 702 | goto rarok; |
| 703 | |
| 704 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ |
| 705 | sin->sin_addr.s_addr = ifa->ifa_mask; |
| 706 | goto rarok; |
| 707 | |
| 708 | case SIOCSIFFLAGS: |
| 709 | if (colon) { |
| 710 | ret = -EADDRNOTAVAIL; |
| 711 | if (!ifa) |
| 712 | break; |
| 713 | ret = 0; |
| 714 | if (!(ifr.ifr_flags & IFF_UP)) |
| 715 | inet_del_ifa(in_dev, ifap, 1); |
| 716 | break; |
| 717 | } |
| 718 | ret = dev_change_flags(dev, ifr.ifr_flags); |
| 719 | break; |
| 720 | |
| 721 | case SIOCSIFADDR: /* Set interface address (and family) */ |
| 722 | ret = -EINVAL; |
| 723 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) |
| 724 | break; |
| 725 | |
| 726 | if (!ifa) { |
| 727 | ret = -ENOBUFS; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 728 | ifa = inet_alloc_ifa(); |
| 729 | if (!ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | break; |
| 731 | if (colon) |
| 732 | memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ); |
| 733 | else |
| 734 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 735 | } else { |
| 736 | ret = 0; |
| 737 | if (ifa->ifa_local == sin->sin_addr.s_addr) |
| 738 | break; |
| 739 | inet_del_ifa(in_dev, ifap, 0); |
| 740 | ifa->ifa_broadcast = 0; |
Bjorn Mork | 148f972 | 2008-02-26 18:17:53 -0800 | [diff] [blame] | 741 | ifa->ifa_scope = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; |
| 745 | |
| 746 | if (!(dev->flags & IFF_POINTOPOINT)) { |
| 747 | ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); |
| 748 | ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); |
| 749 | if ((dev->flags & IFF_BROADCAST) && |
| 750 | ifa->ifa_prefixlen < 31) |
| 751 | ifa->ifa_broadcast = ifa->ifa_address | |
| 752 | ~ifa->ifa_mask; |
| 753 | } else { |
| 754 | ifa->ifa_prefixlen = 32; |
| 755 | ifa->ifa_mask = inet_make_mask(32); |
| 756 | } |
| 757 | ret = inet_set_ifa(dev, ifa); |
| 758 | break; |
| 759 | |
| 760 | case SIOCSIFBRDADDR: /* Set the broadcast address */ |
| 761 | ret = 0; |
| 762 | if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { |
| 763 | inet_del_ifa(in_dev, ifap, 0); |
| 764 | ifa->ifa_broadcast = sin->sin_addr.s_addr; |
| 765 | inet_insert_ifa(ifa); |
| 766 | } |
| 767 | break; |
| 768 | |
| 769 | case SIOCSIFDSTADDR: /* Set the destination address */ |
| 770 | ret = 0; |
| 771 | if (ifa->ifa_address == sin->sin_addr.s_addr) |
| 772 | break; |
| 773 | ret = -EINVAL; |
| 774 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) |
| 775 | break; |
| 776 | ret = 0; |
| 777 | inet_del_ifa(in_dev, ifap, 0); |
| 778 | ifa->ifa_address = sin->sin_addr.s_addr; |
| 779 | inet_insert_ifa(ifa); |
| 780 | break; |
| 781 | |
| 782 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ |
| 783 | |
| 784 | /* |
| 785 | * The mask we set must be legal. |
| 786 | */ |
| 787 | ret = -EINVAL; |
| 788 | if (bad_mask(sin->sin_addr.s_addr, 0)) |
| 789 | break; |
| 790 | ret = 0; |
| 791 | if (ifa->ifa_mask != sin->sin_addr.s_addr) { |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 792 | __be32 old_mask = ifa->ifa_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | inet_del_ifa(in_dev, ifap, 0); |
| 794 | ifa->ifa_mask = sin->sin_addr.s_addr; |
| 795 | ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); |
| 796 | |
| 797 | /* See if current broadcast address matches |
| 798 | * with current netmask, then recalculate |
| 799 | * the broadcast address. Otherwise it's a |
| 800 | * funny address, so don't touch it since |
| 801 | * the user seems to know what (s)he's doing... |
| 802 | */ |
| 803 | if ((dev->flags & IFF_BROADCAST) && |
| 804 | (ifa->ifa_prefixlen < 31) && |
| 805 | (ifa->ifa_broadcast == |
David Engel | dcab5e1 | 2005-10-21 22:09:16 -0500 | [diff] [blame] | 806 | (ifa->ifa_local|~old_mask))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | ifa->ifa_broadcast = (ifa->ifa_local | |
| 808 | ~sin->sin_addr.s_addr); |
| 809 | } |
| 810 | inet_insert_ifa(ifa); |
| 811 | } |
| 812 | break; |
| 813 | } |
| 814 | done: |
| 815 | rtnl_unlock(); |
| 816 | out: |
| 817 | return ret; |
| 818 | rarok: |
| 819 | rtnl_unlock(); |
| 820 | ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0; |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | static int inet_gifconf(struct net_device *dev, char __user *buf, int len) |
| 825 | { |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 826 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | struct in_ifaddr *ifa; |
| 828 | struct ifreq ifr; |
| 829 | int done = 0; |
| 830 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 831 | if (!in_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | goto out; |
| 833 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 834 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (!buf) { |
| 836 | done += sizeof(ifr); |
| 837 | continue; |
| 838 | } |
| 839 | if (len < (int) sizeof(ifr)) |
| 840 | break; |
| 841 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 842 | if (ifa->ifa_label) |
| 843 | strcpy(ifr.ifr_name, ifa->ifa_label); |
| 844 | else |
| 845 | strcpy(ifr.ifr_name, dev->name); |
| 846 | |
| 847 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; |
| 848 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = |
| 849 | ifa->ifa_local; |
| 850 | |
| 851 | if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) { |
| 852 | done = -EFAULT; |
| 853 | break; |
| 854 | } |
| 855 | buf += sizeof(struct ifreq); |
| 856 | len -= sizeof(struct ifreq); |
| 857 | done += sizeof(struct ifreq); |
| 858 | } |
| 859 | out: |
| 860 | return done; |
| 861 | } |
| 862 | |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 863 | __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 865 | __be32 addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | struct in_device *in_dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 867 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
| 869 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 870 | in_dev = __in_dev_get_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (!in_dev) |
| 872 | goto no_in_dev; |
| 873 | |
| 874 | for_primary_ifa(in_dev) { |
| 875 | if (ifa->ifa_scope > scope) |
| 876 | continue; |
| 877 | if (!dst || inet_ifa_match(dst, ifa)) { |
| 878 | addr = ifa->ifa_local; |
| 879 | break; |
| 880 | } |
| 881 | if (!addr) |
| 882 | addr = ifa->ifa_local; |
| 883 | } endfor_ifa(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | if (addr) |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 886 | goto out_unlock; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 887 | no_in_dev: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | /* Not loopback addresses on loopback should be preferred |
| 890 | in this case. It is importnat that lo is the first interface |
| 891 | in dev_base list. |
| 892 | */ |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 893 | for_each_netdev_rcu(net, dev) { |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 894 | in_dev = __in_dev_get_rcu(dev); |
| 895 | if (!in_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | continue; |
| 897 | |
| 898 | for_primary_ifa(in_dev) { |
| 899 | if (ifa->ifa_scope != RT_SCOPE_LINK && |
| 900 | ifa->ifa_scope <= scope) { |
| 901 | addr = ifa->ifa_local; |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 902 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | } endfor_ifa(in_dev); |
| 905 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 906 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return addr; |
| 909 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 910 | EXPORT_SYMBOL(inet_select_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 912 | static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, |
| 913 | __be32 local, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
| 915 | int same = 0; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 916 | __be32 addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | for_ifa(in_dev) { |
| 919 | if (!addr && |
| 920 | (local == ifa->ifa_local || !local) && |
| 921 | ifa->ifa_scope <= scope) { |
| 922 | addr = ifa->ifa_local; |
| 923 | if (same) |
| 924 | break; |
| 925 | } |
| 926 | if (!same) { |
| 927 | same = (!local || inet_ifa_match(local, ifa)) && |
| 928 | (!dst || inet_ifa_match(dst, ifa)); |
| 929 | if (same && addr) { |
| 930 | if (local || !dst) |
| 931 | break; |
| 932 | /* Is the selected addr into dst subnet? */ |
| 933 | if (inet_ifa_match(addr, ifa)) |
| 934 | break; |
| 935 | /* No, then can we use new local src? */ |
| 936 | if (ifa->ifa_scope <= scope) { |
| 937 | addr = ifa->ifa_local; |
| 938 | break; |
| 939 | } |
| 940 | /* search for large dst subnet for addr */ |
| 941 | same = 0; |
| 942 | } |
| 943 | } |
| 944 | } endfor_ifa(in_dev); |
| 945 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 946 | return same ? addr : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Confirm that local IP address exists using wildcards: |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 951 | * - in_dev: only on this interface, 0=any interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | * - dst: only in the same subnet as dst, 0=any dst |
| 953 | * - local: address, 0=autoselect the local address |
| 954 | * - scope: maximum allowed scope value for the local address |
| 955 | */ |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 956 | __be32 inet_confirm_addr(struct in_device *in_dev, |
| 957 | __be32 dst, __be32 local, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 959 | __be32 addr = 0; |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 960 | struct net_device *dev; |
Denis V. Lunev | 39a6d06 | 2008-01-14 23:06:19 -0800 | [diff] [blame] | 961 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Denis V. Lunev | 39a6d06 | 2008-01-14 23:06:19 -0800 | [diff] [blame] | 963 | if (scope != RT_SCOPE_LINK) |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 964 | return confirm_addr_indev(in_dev, dst, local, scope); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 966 | net = dev_net(in_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | rcu_read_lock(); |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 968 | for_each_netdev_rcu(net, dev) { |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 969 | in_dev = __in_dev_get_rcu(dev); |
| 970 | if (in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | addr = confirm_addr_indev(in_dev, dst, local, scope); |
| 972 | if (addr) |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | return addr; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Device notifier |
| 983 | */ |
| 984 | |
| 985 | int register_inetaddr_notifier(struct notifier_block *nb) |
| 986 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 987 | return blocking_notifier_chain_register(&inetaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 989 | EXPORT_SYMBOL(register_inetaddr_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | int unregister_inetaddr_notifier(struct notifier_block *nb) |
| 992 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 993 | return blocking_notifier_chain_unregister(&inetaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 995 | EXPORT_SYMBOL(unregister_inetaddr_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 997 | /* Rename ifa_labels for a device name change. Make some effort to preserve |
| 998 | * existing alias numbering and to create unique labels if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | */ |
| 1000 | static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1001 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | struct in_ifaddr *ifa; |
| 1003 | int named = 0; |
| 1004 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1005 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 1006 | char old[IFNAMSIZ], *dot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | memcpy(old, ifa->ifa_label, IFNAMSIZ); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1009 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (named++ == 0) |
Thomas Graf | 573bf47 | 2008-06-10 15:40:04 -0700 | [diff] [blame] | 1011 | goto skip; |
Mark McLoughlin | 44344b2 | 2008-01-04 00:56:25 -0800 | [diff] [blame] | 1012 | dot = strchr(old, ':'); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1013 | if (dot == NULL) { |
| 1014 | sprintf(old, ":%d", named); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dot = old; |
| 1016 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1017 | if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1018 | strcat(ifa->ifa_label, dot); |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1019 | else |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1020 | strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); |
Thomas Graf | 573bf47 | 2008-06-10 15:40:04 -0700 | [diff] [blame] | 1021 | skip: |
| 1022 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1023 | } |
| 1024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1026 | static inline bool inetdev_valid_mtu(unsigned mtu) |
| 1027 | { |
| 1028 | return mtu >= 68; |
| 1029 | } |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | /* Called only under RTNL semaphore */ |
| 1032 | |
| 1033 | static int inetdev_event(struct notifier_block *this, unsigned long event, |
| 1034 | void *ptr) |
| 1035 | { |
| 1036 | struct net_device *dev = ptr; |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 1037 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | ASSERT_RTNL(); |
| 1040 | |
| 1041 | if (!in_dev) { |
Herbert Xu | 8030f54 | 2007-02-22 01:53:47 +0900 | [diff] [blame] | 1042 | if (event == NETDEV_REGISTER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | in_dev = inetdev_init(dev); |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 1044 | if (!in_dev) |
| 1045 | return notifier_from_errno(-ENOMEM); |
Eric W. Biederman | 0cc217e | 2007-09-26 22:10:06 -0700 | [diff] [blame] | 1046 | if (dev->flags & IFF_LOOPBACK) { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1047 | IN_DEV_CONF_SET(in_dev, NOXFRM, 1); |
| 1048 | IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); |
Herbert Xu | 8030f54 | 2007-02-22 01:53:47 +0900 | [diff] [blame] | 1049 | } |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1050 | } else if (event == NETDEV_CHANGEMTU) { |
| 1051 | /* Re-enabling IP */ |
| 1052 | if (inetdev_valid_mtu(dev->mtu)) |
| 1053 | in_dev = inetdev_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | goto out; |
| 1056 | } |
| 1057 | |
| 1058 | switch (event) { |
| 1059 | case NETDEV_REGISTER: |
| 1060 | printk(KERN_DEBUG "inetdev_event: bug\n"); |
| 1061 | dev->ip_ptr = NULL; |
| 1062 | break; |
| 1063 | case NETDEV_UP: |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1064 | if (!inetdev_valid_mtu(dev->mtu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | break; |
Eric W. Biederman | 0cc217e | 2007-09-26 22:10:06 -0700 | [diff] [blame] | 1066 | if (dev->flags & IFF_LOOPBACK) { |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1067 | struct in_ifaddr *ifa = inet_alloc_ifa(); |
| 1068 | |
| 1069 | if (ifa) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | ifa->ifa_local = |
| 1071 | ifa->ifa_address = htonl(INADDR_LOOPBACK); |
| 1072 | ifa->ifa_prefixlen = 8; |
| 1073 | ifa->ifa_mask = inet_make_mask(8); |
| 1074 | in_dev_hold(in_dev); |
| 1075 | ifa->ifa_dev = in_dev; |
| 1076 | ifa->ifa_scope = RT_SCOPE_HOST; |
| 1077 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 1078 | inet_insert_ifa(ifa); |
| 1079 | } |
| 1080 | } |
| 1081 | ip_mc_up(in_dev); |
Stephen Hemminger | eefef1c | 2009-02-01 01:04:33 -0800 | [diff] [blame] | 1082 | /* fall through */ |
| 1083 | case NETDEV_CHANGEADDR: |
Stephen Hemminger | a21090c | 2009-10-07 03:18:17 -0700 | [diff] [blame] | 1084 | /* Send gratuitous ARP to notify of link change */ |
| 1085 | if (IN_DEV_ARP_NOTIFY(in_dev)) { |
| 1086 | struct in_ifaddr *ifa = in_dev->ifa_list; |
| 1087 | |
| 1088 | if (ifa) |
| 1089 | arp_send(ARPOP_REQUEST, ETH_P_ARP, |
| 1090 | ifa->ifa_address, dev, |
| 1091 | ifa->ifa_address, NULL, |
| 1092 | dev->dev_addr, NULL); |
| 1093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | break; |
| 1095 | case NETDEV_DOWN: |
| 1096 | ip_mc_down(in_dev); |
| 1097 | break; |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1098 | case NETDEV_BONDING_OLDTYPE: |
| 1099 | ip_mc_unmap(in_dev); |
| 1100 | break; |
| 1101 | case NETDEV_BONDING_NEWTYPE: |
| 1102 | ip_mc_remap(in_dev); |
| 1103 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | case NETDEV_CHANGEMTU: |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1105 | if (inetdev_valid_mtu(dev->mtu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | break; |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1107 | /* disable IP when MTU is not enough */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | case NETDEV_UNREGISTER: |
| 1109 | inetdev_destroy(in_dev); |
| 1110 | break; |
| 1111 | case NETDEV_CHANGENAME: |
| 1112 | /* Do not notify about label change, this event is |
| 1113 | * not interesting to applications using netlink. |
| 1114 | */ |
| 1115 | inetdev_changename(dev, in_dev); |
| 1116 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1117 | devinet_sysctl_unregister(in_dev); |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 1118 | devinet_sysctl_register(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | break; |
| 1120 | } |
| 1121 | out: |
| 1122 | return NOTIFY_DONE; |
| 1123 | } |
| 1124 | |
| 1125 | static struct notifier_block ip_netdev_notifier = { |
Jianjun Kong | 539afed | 2008-11-03 02:48:48 -0800 | [diff] [blame] | 1126 | .notifier_call = inetdev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | }; |
| 1128 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1129 | static inline size_t inet_nlmsg_size(void) |
| 1130 | { |
| 1131 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
| 1132 | + nla_total_size(4) /* IFA_ADDRESS */ |
| 1133 | + nla_total_size(4) /* IFA_LOCAL */ |
| 1134 | + nla_total_size(4) /* IFA_BROADCAST */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1135 | + nla_total_size(IFNAMSIZ); /* IFA_LABEL */ |
| 1136 | } |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 1139 | u32 pid, u32 seq, int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
| 1141 | struct ifaddrmsg *ifm; |
| 1142 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1144 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags); |
| 1145 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1146 | return -EMSGSIZE; |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | ifm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | ifm->ifa_family = AF_INET; |
| 1150 | ifm->ifa_prefixlen = ifa->ifa_prefixlen; |
| 1151 | ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT; |
| 1152 | ifm->ifa_scope = ifa->ifa_scope; |
| 1153 | ifm->ifa_index = ifa->ifa_dev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1155 | if (ifa->ifa_address) |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 1156 | NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address); |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | if (ifa->ifa_local) |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 1159 | NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local); |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (ifa->ifa_broadcast) |
Al Viro | a7a628c | 2006-09-26 22:16:43 -0700 | [diff] [blame] | 1162 | NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast); |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1163 | |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1164 | if (ifa->ifa_label[0]) |
| 1165 | NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label); |
| 1166 | |
| 1167 | return nlmsg_end(skb, nlh); |
| 1168 | |
| 1169 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1170 | nlmsg_cancel(skb, nlh); |
| 1171 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 1175 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1176 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1177 | int h, s_h; |
| 1178 | int idx, s_idx; |
| 1179 | int ip_idx, s_ip_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | struct net_device *dev; |
| 1181 | struct in_device *in_dev; |
| 1182 | struct in_ifaddr *ifa; |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1183 | struct hlist_head *head; |
| 1184 | struct hlist_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1186 | s_h = cb->args[0]; |
| 1187 | s_idx = idx = cb->args[1]; |
| 1188 | s_ip_idx = ip_idx = cb->args[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1190 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1191 | idx = 0; |
| 1192 | head = &net->dev_index_head[h]; |
| 1193 | rcu_read_lock(); |
| 1194 | hlist_for_each_entry_rcu(dev, node, head, index_hlist) { |
| 1195 | if (idx < s_idx) |
| 1196 | goto cont; |
| 1197 | if (idx > s_idx) |
| 1198 | s_ip_idx = 0; |
| 1199 | in_dev = __in_dev_get_rcu(dev); |
| 1200 | if (!in_dev) |
| 1201 | goto cont; |
| 1202 | |
| 1203 | for (ifa = in_dev->ifa_list, ip_idx = 0; ifa; |
| 1204 | ifa = ifa->ifa_next, ip_idx++) { |
| 1205 | if (ip_idx < s_ip_idx) |
| 1206 | continue; |
| 1207 | if (inet_fill_ifaddr(skb, ifa, |
| 1208 | NETLINK_CB(cb->skb).pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | cb->nlh->nlmsg_seq, |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1210 | RTM_NEWADDR, NLM_F_MULTI) <= 0) { |
| 1211 | rcu_read_unlock(); |
| 1212 | goto done; |
| 1213 | } |
| 1214 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1215 | cont: |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1216 | idx++; |
| 1217 | } |
| 1218 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | done: |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1222 | cb->args[0] = h; |
| 1223 | cb->args[1] = idx; |
| 1224 | cb->args[2] = ip_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
| 1226 | return skb->len; |
| 1227 | } |
| 1228 | |
Jianjun Kong | 539afed | 2008-11-03 02:48:48 -0800 | [diff] [blame] | 1229 | static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1230 | u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1232 | struct sk_buff *skb; |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1233 | u32 seq = nlh ? nlh->nlmsg_seq : 0; |
| 1234 | int err = -ENOBUFS; |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 1235 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1237 | net = dev_net(ifa->ifa_dev->dev); |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1238 | skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1239 | if (skb == NULL) |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1240 | goto errout; |
| 1241 | |
| 1242 | err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1243 | if (err < 0) { |
| 1244 | /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ |
| 1245 | WARN_ON(err == -EMSGSIZE); |
| 1246 | kfree_skb(skb); |
| 1247 | goto errout; |
| 1248 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 1249 | rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); |
| 1250 | return; |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1251 | errout: |
| 1252 | if (err < 0) |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 1253 | rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | #ifdef CONFIG_SYSCTL |
| 1257 | |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1258 | static void devinet_copy_dflt_conf(struct net *net, int i) |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1259 | { |
| 1260 | struct net_device *dev; |
| 1261 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1262 | rcu_read_lock(); |
| 1263 | for_each_netdev_rcu(net, dev) { |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1264 | struct in_device *in_dev; |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1265 | |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1266 | in_dev = __in_dev_get_rcu(dev); |
| 1267 | if (in_dev && !test_bit(i, in_dev->cnf.state)) |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 1268 | in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1269 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1270 | rcu_read_unlock(); |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1273 | /* called with RTNL locked */ |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1274 | static void inet_forward_change(struct net *net) |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1275 | { |
| 1276 | struct net_device *dev; |
Pavel Emelyanov | 586f121 | 2007-12-16 13:32:48 -0800 | [diff] [blame] | 1277 | int on = IPV4_DEVCONF_ALL(net, FORWARDING); |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1278 | |
Pavel Emelyanov | 586f121 | 2007-12-16 13:32:48 -0800 | [diff] [blame] | 1279 | IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 1280 | IPV4_DEVCONF_DFLT(net, FORWARDING) = on; |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1281 | |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1282 | for_each_netdev(net, dev) { |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1283 | struct in_device *in_dev; |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1284 | if (on) |
| 1285 | dev_disable_lro(dev); |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1286 | rcu_read_lock(); |
| 1287 | in_dev = __in_dev_get_rcu(dev); |
| 1288 | if (in_dev) |
| 1289 | IN_DEV_CONF_SET(in_dev, FORWARDING, on); |
| 1290 | rcu_read_unlock(); |
| 1291 | } |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1294 | static int devinet_conf_proc(ctl_table *ctl, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1295 | void __user *buffer, |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1296 | size_t *lenp, loff_t *ppos) |
| 1297 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1298 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1299 | |
| 1300 | if (write) { |
| 1301 | struct ipv4_devconf *cnf = ctl->extra1; |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1302 | struct net *net = ctl->extra2; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1303 | int i = (int *)ctl->data - cnf->data; |
| 1304 | |
| 1305 | set_bit(i, cnf->state); |
| 1306 | |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 1307 | if (cnf == net->ipv4.devconf_dflt) |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1308 | devinet_copy_dflt_conf(net, i); |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | static int devinet_sysctl_forward(ctl_table *ctl, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1315 | void __user *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | size_t *lenp, loff_t *ppos) |
| 1317 | { |
| 1318 | int *valp = ctl->data; |
| 1319 | int val = *valp; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1320 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | if (write && *valp != val) { |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1323 | struct net *net = ctl->extra2; |
| 1324 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1325 | if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) { |
Eric W. Biederman | 9b8adb5 | 2009-05-13 16:59:21 +0000 | [diff] [blame] | 1326 | if (!rtnl_trylock()) |
| 1327 | return restart_syscall(); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1328 | if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) { |
| 1329 | inet_forward_change(net); |
| 1330 | } else if (*valp) { |
| 1331 | struct ipv4_devconf *cnf = ctl->extra1; |
| 1332 | struct in_device *idev = |
| 1333 | container_of(cnf, struct in_device, cnf); |
| 1334 | dev_disable_lro(idev->dev); |
| 1335 | } |
| 1336 | rtnl_unlock(); |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 1337 | rt_cache_flush(net, 0); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return ret; |
| 1342 | } |
| 1343 | |
| 1344 | int ipv4_doint_and_flush(ctl_table *ctl, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1345 | void __user *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | size_t *lenp, loff_t *ppos) |
| 1347 | { |
| 1348 | int *valp = ctl->data; |
| 1349 | int val = *valp; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1350 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 1351 | struct net *net = ctl->extra2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (write && *valp != val) |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 1354 | rt_cache_flush(net, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1359 | #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1360 | { \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1361 | .procname = name, \ |
| 1362 | .data = ipv4_devconf.data + \ |
| 1363 | NET_IPV4_CONF_ ## attr - 1, \ |
| 1364 | .maxlen = sizeof(int), \ |
| 1365 | .mode = mval, \ |
| 1366 | .proc_handler = proc, \ |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1367 | .extra1 = &ipv4_devconf, \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1371 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1372 | |
| 1373 | #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1374 | DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1375 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1376 | #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \ |
| 1377 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1378 | |
| 1379 | #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1380 | DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | static struct devinet_sysctl_table { |
| 1383 | struct ctl_table_header *sysctl_header; |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1384 | struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX]; |
| 1385 | char *dev_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } devinet_sysctl = { |
| 1387 | .devinet_vars = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1388 | DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1389 | devinet_sysctl_forward), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1390 | DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), |
| 1391 | |
| 1392 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), |
| 1393 | DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), |
| 1394 | DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), |
| 1395 | DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), |
| 1396 | DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), |
| 1397 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, |
| 1398 | "accept_source_route"), |
Patrick McHardy | 8153a10 | 2009-12-03 01:25:58 +0000 | [diff] [blame] | 1399 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1400 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), |
| 1401 | DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), |
| 1402 | DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), |
| 1403 | DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), |
| 1404 | DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), |
| 1405 | DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), |
| 1406 | DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), |
| 1407 | DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), |
| 1408 | DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), |
Stephen Hemminger | eefef1c | 2009-02-01 01:04:33 -0800 | [diff] [blame] | 1409 | DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"), |
Jesper Dangaard Brouer | 6532414 | 2010-01-05 05:50:47 +0000 | [diff] [blame^] | 1410 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1411 | |
| 1412 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), |
| 1413 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), |
| 1414 | DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION, |
| 1415 | "force_igmp_version"), |
| 1416 | DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, |
| 1417 | "promote_secondaries"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | }; |
| 1420 | |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 1421 | static int __devinet_sysctl_register(struct net *net, char *dev_name, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1422 | struct ipv4_devconf *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | { |
| 1424 | int i; |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1425 | struct devinet_sysctl_table *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1427 | #define DEVINET_CTL_PATH_DEV 3 |
| 1428 | |
| 1429 | struct ctl_path devinet_ctl_path[] = { |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1430 | { .procname = "net", }, |
| 1431 | { .procname = "ipv4", }, |
| 1432 | { .procname = "conf", }, |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1433 | { /* to be set */ }, |
| 1434 | { }, |
| 1435 | }; |
| 1436 | |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1437 | t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | if (!t) |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1439 | goto out; |
| 1440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) { |
| 1442 | t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 1443 | t->devinet_vars[i].extra1 = p; |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1444 | t->devinet_vars[i].extra2 = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1447 | /* |
| 1448 | * Make a copy of dev_name, because '.procname' is regarded as const |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | * by sysctl and we wouldn't want anyone to change it under our feet |
| 1450 | * (see SIOCSIFNAME). |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1451 | */ |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1452 | t->dev_name = kstrdup(dev_name, GFP_KERNEL); |
| 1453 | if (!t->dev_name) |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1454 | goto free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1456 | devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1458 | t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path, |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1459 | t->devinet_vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | if (!t->sysctl_header) |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1461 | goto free_procname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | p->sysctl = t; |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 1464 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1466 | free_procname: |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 1467 | kfree(t->dev_name); |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1468 | free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | kfree(t); |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 1470 | out: |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 1471 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1474 | static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf) |
| 1475 | { |
| 1476 | struct devinet_sysctl_table *t = cnf->sysctl; |
| 1477 | |
| 1478 | if (t == NULL) |
| 1479 | return; |
| 1480 | |
| 1481 | cnf->sysctl = NULL; |
| 1482 | unregister_sysctl_table(t->sysctl_header); |
| 1483 | kfree(t->dev_name); |
| 1484 | kfree(t); |
| 1485 | } |
| 1486 | |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 1487 | static void devinet_sysctl_register(struct in_device *idev) |
| 1488 | { |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1489 | neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1490 | NET_IPV4_NEIGH, "ipv4", NULL); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1491 | __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1492 | &idev->cnf); |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 1493 | } |
| 1494 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1495 | static void devinet_sysctl_unregister(struct in_device *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | { |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1497 | __devinet_sysctl_unregister(&idev->cnf); |
| 1498 | neigh_sysctl_unregister(idev->arp_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1501 | static struct ctl_table ctl_forward_entry[] = { |
| 1502 | { |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1503 | .procname = "ip_forward", |
| 1504 | .data = &ipv4_devconf.data[ |
| 1505 | NET_IPV4_CONF_FORWARDING - 1], |
| 1506 | .maxlen = sizeof(int), |
| 1507 | .mode = 0644, |
| 1508 | .proc_handler = devinet_sysctl_forward, |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1509 | .extra1 = &ipv4_devconf, |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 1510 | .extra2 = &init_net, |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1511 | }, |
| 1512 | { }, |
| 1513 | }; |
| 1514 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1515 | static __net_initdata struct ctl_path net_ipv4_path[] = { |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1516 | { .procname = "net", }, |
| 1517 | { .procname = "ipv4", }, |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1518 | { }, |
| 1519 | }; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1520 | #endif |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 1521 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1522 | static __net_init int devinet_init_net(struct net *net) |
| 1523 | { |
| 1524 | int err; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1525 | struct ipv4_devconf *all, *dflt; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1526 | #ifdef CONFIG_SYSCTL |
| 1527 | struct ctl_table *tbl = ctl_forward_entry; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1528 | struct ctl_table_header *forw_hdr; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1529 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1530 | |
| 1531 | err = -ENOMEM; |
| 1532 | all = &ipv4_devconf; |
| 1533 | dflt = &ipv4_devconf_dflt; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1534 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1535 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1536 | all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL); |
| 1537 | if (all == NULL) |
| 1538 | goto err_alloc_all; |
| 1539 | |
| 1540 | dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); |
| 1541 | if (dflt == NULL) |
| 1542 | goto err_alloc_dflt; |
| 1543 | |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1544 | #ifdef CONFIG_SYSCTL |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1545 | tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL); |
| 1546 | if (tbl == NULL) |
| 1547 | goto err_alloc_ctl; |
| 1548 | |
| 1549 | tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1]; |
| 1550 | tbl[0].extra1 = all; |
| 1551 | tbl[0].extra2 = net; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1552 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1556 | err = __devinet_sysctl_register(net, "all", all); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1557 | if (err < 0) |
| 1558 | goto err_reg_all; |
| 1559 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1560 | err = __devinet_sysctl_register(net, "default", dflt); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1561 | if (err < 0) |
| 1562 | goto err_reg_dflt; |
| 1563 | |
| 1564 | err = -ENOMEM; |
| 1565 | forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl); |
| 1566 | if (forw_hdr == NULL) |
| 1567 | goto err_reg_ctl; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1568 | net->ipv4.forw_hdr = forw_hdr; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1569 | #endif |
| 1570 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1571 | net->ipv4.devconf_all = all; |
| 1572 | net->ipv4.devconf_dflt = dflt; |
| 1573 | return 0; |
| 1574 | |
| 1575 | #ifdef CONFIG_SYSCTL |
| 1576 | err_reg_ctl: |
| 1577 | __devinet_sysctl_unregister(dflt); |
| 1578 | err_reg_dflt: |
| 1579 | __devinet_sysctl_unregister(all); |
| 1580 | err_reg_all: |
| 1581 | if (tbl != ctl_forward_entry) |
| 1582 | kfree(tbl); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1583 | err_alloc_ctl: |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1584 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1585 | if (dflt != &ipv4_devconf_dflt) |
| 1586 | kfree(dflt); |
| 1587 | err_alloc_dflt: |
| 1588 | if (all != &ipv4_devconf) |
| 1589 | kfree(all); |
| 1590 | err_alloc_all: |
| 1591 | return err; |
| 1592 | } |
| 1593 | |
| 1594 | static __net_exit void devinet_exit_net(struct net *net) |
| 1595 | { |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1596 | #ifdef CONFIG_SYSCTL |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1597 | struct ctl_table *tbl; |
| 1598 | |
| 1599 | tbl = net->ipv4.forw_hdr->ctl_table_arg; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1600 | unregister_net_sysctl_table(net->ipv4.forw_hdr); |
| 1601 | __devinet_sysctl_unregister(net->ipv4.devconf_dflt); |
| 1602 | __devinet_sysctl_unregister(net->ipv4.devconf_all); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1603 | kfree(tbl); |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 1604 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1605 | kfree(net->ipv4.devconf_dflt); |
| 1606 | kfree(net->ipv4.devconf_all); |
| 1607 | } |
| 1608 | |
| 1609 | static __net_initdata struct pernet_operations devinet_ops = { |
| 1610 | .init = devinet_init_net, |
| 1611 | .exit = devinet_exit_net, |
| 1612 | }; |
| 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | void __init devinet_init(void) |
| 1615 | { |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 1616 | register_pernet_subsys(&devinet_ops); |
| 1617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | register_gifconf(PF_INET, inet_gifconf); |
| 1619 | register_netdevice_notifier(&ip_netdev_notifier); |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 1620 | |
| 1621 | rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL); |
| 1622 | rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL); |
| 1623 | rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |