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