blob: a2d48173828adbc2710654c9e06a1575367ba04f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Juhl02c30a82005-05-05 16:16:16 -070012 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
32#include <asm/system.h>
33#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080034#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/types.h>
37#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/string.h>
39#include <linux/mm.h>
40#include <linux/socket.h>
41#include <linux/sockios.h>
42#include <linux/in.h>
43#include <linux/errno.h>
44#include <linux/interrupt.h>
Thomas Graf18237302006-08-04 23:04:54 -070045#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/if_ether.h>
47#include <linux/inet.h>
48#include <linux/netdevice.h>
49#include <linux/etherdevice.h>
50#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/init.h>
52#include <linux/notifier.h>
53#include <linux/inetdevice.h>
54#include <linux/igmp.h>
55#ifdef CONFIG_SYSCTL
56#include <linux/sysctl.h>
57#endif
58#include <linux/kmod.h>
59
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020060#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <net/ip.h>
62#include <net/route.h>
63#include <net/ip_fib.h>
Thomas Graf63f34442007-03-22 11:55:17 -070064#include <net/rtnetlink.h>
Pavel Emelyanov752d14d2007-12-16 13:31:47 -080065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67struct ipv4_devconf ipv4_devconf = {
Herbert Xu42f811b2007-06-04 23:34:44 -070068 .data = {
69 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
70 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
71 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
72 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
73 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76static struct ipv4_devconf ipv4_devconf_dflt = {
Herbert Xu42f811b2007-06-04 23:34:44 -070077 .data = {
78 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
79 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
80 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
81 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
82 [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
83 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Herbert Xu42f811b2007-06-04 23:34:44 -070086#define IPV4_DEVCONF_DFLT(attr) IPV4_DEVCONF(ipv4_devconf_dflt, attr)
87
Patrick McHardyef7c79e2007-06-05 12:38:30 -070088static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
Thomas Graf5c753972006-08-04 23:03:53 -070089 [IFA_LOCAL] = { .type = NLA_U32 },
90 [IFA_ADDRESS] = { .type = NLA_U32 },
91 [IFA_BROADCAST] = { .type = NLA_U32 },
92 [IFA_ANYCAST] = { .type = NLA_U32 },
Thomas Graf5176f912006-08-26 20:13:18 -070093 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Thomas Graf5c753972006-08-04 23:03:53 -070094};
95
Thomas Grafd6062cb2006-08-15 00:33:59 -070096static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alan Sterne041c682006-03-27 01:16:30 -080098static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
100 int destroy);
101#ifdef CONFIG_SYSCTL
Pavel Emelyanov66f27a52007-12-02 00:55:54 +1100102static void devinet_sysctl_register(struct in_device *idev);
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800103static void devinet_sysctl_unregister(struct in_device *idev);
104#else
105static inline void devinet_sysctl_register(struct in_device *idev)
106{
107}
108static inline void devinet_sysctl_unregister(struct in_device *idev)
109{
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
113/* Locks all the inet devices. */
114
115static struct in_ifaddr *inet_alloc_ifa(void)
116{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700117 struct in_ifaddr *ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (ifa) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 INIT_RCU_HEAD(&ifa->rcu_head);
121 }
122
123 return ifa;
124}
125
126static void inet_rcu_free_ifa(struct rcu_head *head)
127{
128 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
129 if (ifa->ifa_dev)
130 in_dev_put(ifa->ifa_dev);
131 kfree(ifa);
132}
133
134static inline void inet_free_ifa(struct in_ifaddr *ifa)
135{
136 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
137}
138
139void in_dev_finish_destroy(struct in_device *idev)
140{
141 struct net_device *dev = idev->dev;
142
143 BUG_TRAP(!idev->ifa_list);
144 BUG_TRAP(!idev->mc_list);
145#ifdef NET_REFCNT_DEBUG
146 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
147 idev, dev ? dev->name : "NIL");
148#endif
149 dev_put(dev);
150 if (!idev->dead)
151 printk("Freeing alive in_device %p\n", idev);
152 else {
153 kfree(idev);
154 }
155}
156
Herbert Xu71e27da2007-06-04 23:36:06 -0700157static struct in_device *inetdev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct in_device *in_dev;
160
161 ASSERT_RTNL();
162
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700163 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!in_dev)
165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 INIT_RCU_HEAD(&in_dev->rcu_head);
167 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
168 in_dev->cnf.sysctl = NULL;
169 in_dev->dev = dev;
170 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
171 goto out_kfree;
172 /* Reference in_dev->dev */
173 dev_hold(dev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800174 /* Account for reference dev->ip_ptr (below) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 in_dev_hold(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Pavel Emelyanov66f27a52007-12-02 00:55:54 +1100177 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ip_mc_init_dev(in_dev);
179 if (dev->flags & IFF_UP)
180 ip_mc_up(in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800181
David L Stevens30c4cf52007-01-04 12:31:14 -0800182 /* we can receive as soon as ip_ptr is set -- do this last */
183 rcu_assign_pointer(dev->ip_ptr, in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800184out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return in_dev;
186out_kfree:
187 kfree(in_dev);
188 in_dev = NULL;
189 goto out;
190}
191
192static void in_dev_rcu_put(struct rcu_head *head)
193{
194 struct in_device *idev = container_of(head, struct in_device, rcu_head);
195 in_dev_put(idev);
196}
197
198static void inetdev_destroy(struct in_device *in_dev)
199{
200 struct in_ifaddr *ifa;
201 struct net_device *dev;
202
203 ASSERT_RTNL();
204
205 dev = in_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 in_dev->dead = 1;
208
209 ip_mc_destroy_dev(in_dev);
210
211 while ((ifa = in_dev->ifa_list) != NULL) {
212 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
213 inet_free_ifa(ifa);
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dev->ip_ptr = NULL;
217
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800218 devinet_sysctl_unregister(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
220 arp_ifdown(dev);
221
222 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
223}
224
Al Viroff428d72006-09-26 22:13:35 -0700225int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 rcu_read_lock();
228 for_primary_ifa(in_dev) {
229 if (inet_ifa_match(a, ifa)) {
230 if (!b || inet_ifa_match(b, ifa)) {
231 rcu_read_unlock();
232 return 1;
233 }
234 }
235 } endfor_ifa(in_dev);
236 rcu_read_unlock();
237 return 0;
238}
239
Thomas Grafd6062cb2006-08-15 00:33:59 -0700240static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
241 int destroy, struct nlmsghdr *nlh, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Harald Welte8f937c62005-05-29 20:23:46 -0700243 struct in_ifaddr *promote = NULL;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800244 struct in_ifaddr *ifa, *ifa1 = *ifap;
245 struct in_ifaddr *last_prim = in_dev->ifa_list;
246 struct in_ifaddr *prev_prom = NULL;
247 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 ASSERT_RTNL();
250
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900251 /* 1. Deleting primary ifaddr forces deletion all secondaries
Harald Welte8f937c62005-05-29 20:23:46 -0700252 * unless alias promotion is set
253 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
257
258 while ((ifa = *ifap1) != NULL) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900259 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800260 ifa1->ifa_scope <= ifa->ifa_scope)
261 last_prim = ifa;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
264 ifa1->ifa_mask != ifa->ifa_mask ||
265 !inet_ifa_match(ifa1->ifa_address, ifa)) {
266 ifap1 = &ifa->ifa_next;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800267 prev_prom = ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 continue;
269 }
270
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800271 if (!do_promote) {
Harald Welte8f937c62005-05-29 20:23:46 -0700272 *ifap1 = ifa->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Thomas Grafd6062cb2006-08-15 00:33:59 -0700274 rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800275 blocking_notifier_call_chain(&inetaddr_chain,
276 NETDEV_DOWN, ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700277 inet_free_ifa(ifa);
278 } else {
279 promote = ifa;
280 break;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284
285 /* 2. Unlink it */
286
287 *ifap = ifa1->ifa_next;
288
289 /* 3. Announce address deletion */
290
291 /* Send message first, then call notifier.
292 At first sight, FIB update triggered by notifier
293 will refer to already deleted ifaddr, that could confuse
294 netlink listeners. It is not true: look, gated sees
295 that route deleted and if it still thinks that ifaddr
296 is valid, it will try to restore deleted routes... Grr.
297 So that, this order is correct.
298 */
Thomas Grafd6062cb2006-08-15 00:33:59 -0700299 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800300 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800301
302 if (promote) {
303
304 if (prev_prom) {
305 prev_prom->ifa_next = promote->ifa_next;
306 promote->ifa_next = last_prim->ifa_next;
307 last_prim->ifa_next = promote;
308 }
309
310 promote->ifa_flags &= ~IFA_F_SECONDARY;
Thomas Grafd6062cb2006-08-15 00:33:59 -0700311 rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800312 blocking_notifier_call_chain(&inetaddr_chain,
313 NETDEV_UP, promote);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800314 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
315 if (ifa1->ifa_mask != ifa->ifa_mask ||
316 !inet_ifa_match(ifa1->ifa_address, ifa))
317 continue;
318 fib_add_ifaddr(ifa);
319 }
320
321 }
Herbert Xu63630972007-06-07 18:35:38 -0700322 if (destroy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 inet_free_ifa(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Thomas Grafd6062cb2006-08-15 00:33:59 -0700326static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
327 int destroy)
328{
329 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
330}
331
332static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
333 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct in_device *in_dev = ifa->ifa_dev;
336 struct in_ifaddr *ifa1, **ifap, **last_primary;
337
338 ASSERT_RTNL();
339
340 if (!ifa->ifa_local) {
341 inet_free_ifa(ifa);
342 return 0;
343 }
344
345 ifa->ifa_flags &= ~IFA_F_SECONDARY;
346 last_primary = &in_dev->ifa_list;
347
348 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
349 ifap = &ifa1->ifa_next) {
350 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
351 ifa->ifa_scope <= ifa1->ifa_scope)
352 last_primary = &ifa1->ifa_next;
353 if (ifa1->ifa_mask == ifa->ifa_mask &&
354 inet_ifa_match(ifa1->ifa_address, ifa)) {
355 if (ifa1->ifa_local == ifa->ifa_local) {
356 inet_free_ifa(ifa);
357 return -EEXIST;
358 }
359 if (ifa1->ifa_scope != ifa->ifa_scope) {
360 inet_free_ifa(ifa);
361 return -EINVAL;
362 }
363 ifa->ifa_flags |= IFA_F_SECONDARY;
364 }
365 }
366
367 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
368 net_srandom(ifa->ifa_local);
369 ifap = last_primary;
370 }
371
372 ifa->ifa_next = *ifap;
373 *ifap = ifa;
374
375 /* Send message first, then call notifier.
376 Notifier will trigger FIB update, so that
377 listeners of netlink will know about new ifaddr */
Thomas Grafd6062cb2006-08-15 00:33:59 -0700378 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800379 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return 0;
382}
383
Thomas Grafd6062cb2006-08-15 00:33:59 -0700384static int inet_insert_ifa(struct in_ifaddr *ifa)
385{
386 return __inet_insert_ifa(ifa, NULL, 0);
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
390{
Herbert Xue5ed6392005-10-03 14:35:55 -0700391 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 ASSERT_RTNL();
394
395 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700396 inet_free_ifa(ifa);
397 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Herbert Xu71e27da2007-06-04 23:36:06 -0700399 ipv4_devconf_setall(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (ifa->ifa_dev != in_dev) {
401 BUG_TRAP(!ifa->ifa_dev);
402 in_dev_hold(in_dev);
403 ifa->ifa_dev = in_dev;
404 }
405 if (LOOPBACK(ifa->ifa_local))
406 ifa->ifa_scope = RT_SCOPE_HOST;
407 return inet_insert_ifa(ifa);
408}
409
410struct in_device *inetdev_by_index(int ifindex)
411{
412 struct net_device *dev;
413 struct in_device *in_dev = NULL;
414 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700415 dev = __dev_get_by_index(&init_net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (dev)
417 in_dev = in_dev_get(dev);
418 read_unlock(&dev_base_lock);
419 return in_dev;
420}
421
422/* Called only from RTNL semaphored context. No locks. */
423
Al Viro60cad5d2006-09-26 22:17:09 -0700424struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
425 __be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 ASSERT_RTNL();
428
429 for_primary_ifa(in_dev) {
430 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
431 return ifa;
432 } endfor_ifa(in_dev);
433 return NULL;
434}
435
436static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
437{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100438 struct net *net = skb->sk->sk_net;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700439 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct in_device *in_dev;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700441 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct in_ifaddr *ifa, **ifap;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700443 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 ASSERT_RTNL();
446
Denis V. Lunevb8542722007-12-01 00:21:31 +1100447 if (net != &init_net)
448 return -EINVAL;
449
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700450 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
451 if (err < 0)
452 goto errout;
453
454 ifm = nlmsg_data(nlh);
455 in_dev = inetdev_by_index(ifm->ifa_index);
456 if (in_dev == NULL) {
457 err = -ENODEV;
458 goto errout;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 __in_dev_put(in_dev);
462
463 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
464 ifap = &ifa->ifa_next) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700465 if (tb[IFA_LOCAL] &&
Al Viroa7a628c2006-09-26 22:16:43 -0700466 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 continue;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700468
469 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
470 continue;
471
472 if (tb[IFA_ADDRESS] &&
473 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
Al Viroa7a628c2006-09-26 22:16:43 -0700474 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700475 continue;
476
Thomas Grafd6062cb2006-08-15 00:33:59 -0700477 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479 }
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700480
481 err = -EADDRNOTAVAIL;
482errout:
483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Thomas Graf5c753972006-08-04 23:03:53 -0700486static struct in_ifaddr *rtm_to_ifaddr(struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Thomas Graf5c753972006-08-04 23:03:53 -0700488 struct nlattr *tb[IFA_MAX+1];
489 struct in_ifaddr *ifa;
490 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct net_device *dev;
492 struct in_device *in_dev;
Thomas Graf5c753972006-08-04 23:03:53 -0700493 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Thomas Graf5c753972006-08-04 23:03:53 -0700495 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
496 if (err < 0)
497 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Thomas Graf5c753972006-08-04 23:03:53 -0700499 ifm = nlmsg_data(nlh);
Evgeniy Polyakovc4e38f42007-03-09 13:43:24 -0800500 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) {
501 err = -EINVAL;
Thomas Graf5c753972006-08-04 23:03:53 -0700502 goto errout;
Evgeniy Polyakovc4e38f42007-03-09 13:43:24 -0800503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Eric W. Biederman881d9662007-09-17 11:56:21 -0700505 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Thomas Graf5c753972006-08-04 23:03:53 -0700506 if (dev == NULL) {
507 err = -ENODEV;
508 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Thomas Graf5c753972006-08-04 23:03:53 -0700511 in_dev = __in_dev_get_rtnl(dev);
512 if (in_dev == NULL) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700513 err = -ENOBUFS;
514 goto errout;
Thomas Graf5c753972006-08-04 23:03:53 -0700515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Thomas Graf5c753972006-08-04 23:03:53 -0700517 ifa = inet_alloc_ifa();
518 if (ifa == NULL) {
519 /*
520 * A potential indev allocation can be left alive, it stays
521 * assigned to its device and is destroy with it.
522 */
523 err = -ENOBUFS;
524 goto errout;
525 }
526
Pavel Emelyanova4e65d32007-12-07 23:55:43 -0800527 ipv4_devconf_setall(in_dev);
Thomas Graf5c753972006-08-04 23:03:53 -0700528 in_dev_hold(in_dev);
529
530 if (tb[IFA_ADDRESS] == NULL)
531 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
534 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ifa->ifa_flags = ifm->ifa_flags;
536 ifa->ifa_scope = ifm->ifa_scope;
Thomas Graf5c753972006-08-04 23:03:53 -0700537 ifa->ifa_dev = in_dev;
538
Al Viroa7a628c2006-09-26 22:16:43 -0700539 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
540 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
Thomas Graf5c753972006-08-04 23:03:53 -0700541
542 if (tb[IFA_BROADCAST])
Al Viroa7a628c2006-09-26 22:16:43 -0700543 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700544
545 if (tb[IFA_ANYCAST])
Al Viroa7a628c2006-09-26 22:16:43 -0700546 ifa->ifa_anycast = nla_get_be32(tb[IFA_ANYCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700547
548 if (tb[IFA_LABEL])
549 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 else
551 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
552
Thomas Graf5c753972006-08-04 23:03:53 -0700553 return ifa;
554
555errout:
556 return ERR_PTR(err);
557}
558
559static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
560{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100561 struct net *net = skb->sk->sk_net;
Thomas Graf5c753972006-08-04 23:03:53 -0700562 struct in_ifaddr *ifa;
563
564 ASSERT_RTNL();
565
Denis V. Lunevb8542722007-12-01 00:21:31 +1100566 if (net != &init_net)
567 return -EINVAL;
568
Thomas Graf5c753972006-08-04 23:03:53 -0700569 ifa = rtm_to_ifaddr(nlh);
570 if (IS_ERR(ifa))
571 return PTR_ERR(ifa);
572
Thomas Grafd6062cb2006-08-15 00:33:59 -0700573 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
577 * Determine a default network mask, based on the IP address.
578 */
579
Al Viro714e85b2006-11-14 20:51:49 -0800580static __inline__ int inet_abc_len(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int rc = -1; /* Something else, probably a multicast. */
583
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900584 if (ZERONET(addr))
585 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 else {
Al Viro714e85b2006-11-14 20:51:49 -0800587 __u32 haddr = ntohl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Al Viro714e85b2006-11-14 20:51:49 -0800589 if (IN_CLASSA(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 rc = 8;
Al Viro714e85b2006-11-14 20:51:49 -0800591 else if (IN_CLASSB(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 rc = 16;
Al Viro714e85b2006-11-14 20:51:49 -0800593 else if (IN_CLASSC(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rc = 24;
595 }
596
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900597 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600
601int 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
Eric W. Biederman881d9662007-09-17 11:56:21 -0700630 dev_load(&init_net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#endif
632
Stephen Hemminger132adf52007-03-08 20:44:43 -0800633 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 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;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700671 if ((dev = __dev_get_by_name(&init_net, ifr.ifr_name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto done;
673
674 if (colon)
675 *colon = ':';
676
Herbert Xue5ed6392005-10-03 14:35:55 -0700677 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 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
Stephen Hemminger132adf52007-03-08 20:44:43 -0800708 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 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) {
Al Viroa144ea42006-09-28 18:00:55 -0700808 __be32 old_mask = ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 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 Engeldcab5e12005-10-21 22:09:16 -0500822 (ifa->ifa_local|~old_mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ifa->ifa_broadcast = (ifa->ifa_local |
824 ~sin->sin_addr.s_addr);
825 }
826 inet_insert_ifa(ifa);
827 }
828 break;
829 }
830done:
831 rtnl_unlock();
832out:
833 return ret;
834rarok:
835 rtnl_unlock();
836 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
837 goto out;
838}
839
840static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
841{
Herbert Xue5ed6392005-10-03 14:35:55 -0700842 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 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 }
875out:
876 return done;
877}
878
Al Viroa61ced52006-09-26 21:27:54 -0700879__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Al Viroa61ced52006-09-26 21:27:54 -0700881 __be32 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct in_device *in_dev;
883
884 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700885 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 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);
899no_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();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700911 for_each_netdev(&init_net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -0700912 if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 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 }
923out_unlock_both:
924 read_unlock(&dev_base_lock);
925 rcu_read_unlock();
926out:
927 return addr;
928}
929
Al Viro60cad5d2006-09-26 22:17:09 -0700930static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
931 __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int same = 0;
Al Viroa144ea42006-09-28 18:00:55 -0700934 __be32 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
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 */
Al Viro60cad5d2006-09-26 22:17:09 -0700974__be32 inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Al Viro60cad5d2006-09-26 22:17:09 -0700976 __be32 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct in_device *in_dev;
978
979 if (dev) {
980 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700981 if ((in_dev = __in_dev_get_rcu(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 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();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700990 for_each_netdev(&init_net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -0700991 if ((in_dev = __in_dev_get_rcu(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 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
1007int register_inetaddr_notifier(struct notifier_block *nb)
1008{
Alan Sterne041c682006-03-27 01:16:30 -08001009 return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012int unregister_inetaddr_notifier(struct notifier_block *nb)
1013{
Alan Sterne041c682006-03-27 01:16:30 -08001014 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
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*/
1020static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct in_ifaddr *ifa;
1023 int named = 0;
1024
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001025 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1026 char old[IFNAMSIZ], *dot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 memcpy(old, ifa->ifa_label, IFNAMSIZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001029 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (named++ == 0)
1031 continue;
Mark McLoughlin44344b22008-01-04 00:56:25 -08001032 dot = strchr(old, ':');
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001033 if (dot == NULL) {
1034 sprintf(old, ":%d", named);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dot = old;
1036 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001037 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045/* Called only under RTNL semaphore */
1046
1047static int inetdev_event(struct notifier_block *this, unsigned long event,
1048 void *ptr)
1049{
1050 struct net_device *dev = ptr;
Herbert Xue5ed6392005-10-03 14:35:55 -07001051 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001053 if (dev->nd_net != &init_net)
1054 return NOTIFY_DONE;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ASSERT_RTNL();
1057
1058 if (!in_dev) {
Herbert Xu8030f542007-02-22 01:53:47 +09001059 if (event == NETDEV_REGISTER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 in_dev = inetdev_init(dev);
Herbert Xub217d612007-07-30 17:04:52 -07001061 if (!in_dev)
1062 return notifier_from_errno(-ENOMEM);
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001063 if (dev->flags & IFF_LOOPBACK) {
Herbert Xu42f811b2007-06-04 23:34:44 -07001064 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1065 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
Herbert Xu8030f542007-02-22 01:53:47 +09001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 goto out;
1069 }
1070
1071 switch (event) {
1072 case NETDEV_REGISTER:
1073 printk(KERN_DEBUG "inetdev_event: bug\n");
1074 dev->ip_ptr = NULL;
1075 break;
1076 case NETDEV_UP:
1077 if (dev->mtu < 68)
1078 break;
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001079 if (dev->flags & IFF_LOOPBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct in_ifaddr *ifa;
1081 if ((ifa = inet_alloc_ifa()) != NULL) {
1082 ifa->ifa_local =
1083 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1084 ifa->ifa_prefixlen = 8;
1085 ifa->ifa_mask = inet_make_mask(8);
1086 in_dev_hold(in_dev);
1087 ifa->ifa_dev = in_dev;
1088 ifa->ifa_scope = RT_SCOPE_HOST;
1089 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1090 inet_insert_ifa(ifa);
1091 }
1092 }
1093 ip_mc_up(in_dev);
1094 break;
1095 case NETDEV_DOWN:
1096 ip_mc_down(in_dev);
1097 break;
1098 case NETDEV_CHANGEMTU:
1099 if (dev->mtu >= 68)
1100 break;
1101 /* MTU falled under 68, disable IP */
1102 case NETDEV_UNREGISTER:
1103 inetdev_destroy(in_dev);
1104 break;
1105 case NETDEV_CHANGENAME:
1106 /* Do not notify about label change, this event is
1107 * not interesting to applications using netlink.
1108 */
1109 inetdev_changename(dev, in_dev);
1110
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001111 devinet_sysctl_unregister(in_dev);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001112 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 }
1115out:
1116 return NOTIFY_DONE;
1117}
1118
1119static struct notifier_block ip_netdev_notifier = {
1120 .notifier_call =inetdev_event,
1121};
1122
Thomas Graf339bf982006-11-10 14:10:15 -08001123static inline size_t inet_nlmsg_size(void)
1124{
1125 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1126 + nla_total_size(4) /* IFA_ADDRESS */
1127 + nla_total_size(4) /* IFA_LOCAL */
1128 + nla_total_size(4) /* IFA_BROADCAST */
1129 + nla_total_size(4) /* IFA_ANYCAST */
1130 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001134 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct ifaddrmsg *ifm;
1137 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Thomas Graf47f68512006-08-04 23:04:36 -07001139 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1140 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001141 return -EMSGSIZE;
Thomas Graf47f68512006-08-04 23:04:36 -07001142
1143 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 ifm->ifa_family = AF_INET;
1145 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1146 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1147 ifm->ifa_scope = ifa->ifa_scope;
1148 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Thomas Graf47f68512006-08-04 23:04:36 -07001150 if (ifa->ifa_address)
Al Viroa7a628c2006-09-26 22:16:43 -07001151 NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
Thomas Graf47f68512006-08-04 23:04:36 -07001152
1153 if (ifa->ifa_local)
Al Viroa7a628c2006-09-26 22:16:43 -07001154 NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
Thomas Graf47f68512006-08-04 23:04:36 -07001155
1156 if (ifa->ifa_broadcast)
Al Viroa7a628c2006-09-26 22:16:43 -07001157 NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
Thomas Graf47f68512006-08-04 23:04:36 -07001158
1159 if (ifa->ifa_anycast)
Al Viroa7a628c2006-09-26 22:16:43 -07001160 NLA_PUT_BE32(skb, IFA_ANYCAST, ifa->ifa_anycast);
Thomas Graf47f68512006-08-04 23:04:36 -07001161
1162 if (ifa->ifa_label[0])
1163 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1164
1165 return nlmsg_end(skb, nlh);
1166
1167nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001168 nlmsg_cancel(skb, nlh);
1169 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1173{
Denis V. Lunevb8542722007-12-01 00:21:31 +11001174 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int idx, ip_idx;
1176 struct net_device *dev;
1177 struct in_device *in_dev;
1178 struct in_ifaddr *ifa;
1179 int s_ip_idx, s_idx = cb->args[0];
1180
Denis V. Lunevb8542722007-12-01 00:21:31 +11001181 if (net != &init_net)
1182 return 0;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 s_ip_idx = ip_idx = cb->args[1];
Pavel Emelianov7562f872007-05-03 15:13:45 -07001185 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001186 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001188 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (idx > s_idx)
1190 s_ip_idx = 0;
Patrick McHardy6313c1e2007-04-16 17:00:53 -07001191 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001192 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1195 ifa = ifa->ifa_next, ip_idx++) {
1196 if (ip_idx < s_ip_idx)
Stephen Hemminger596e4152007-09-11 10:41:04 +02001197 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1199 cb->nlh->nlmsg_seq,
Patrick McHardy6313c1e2007-04-16 17:00:53 -07001200 RTM_NEWADDR, NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001203cont:
1204 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 cb->args[0] = idx;
1209 cb->args[1] = ip_idx;
1210
1211 return skb->len;
1212}
1213
Thomas Grafd6062cb2006-08-15 00:33:59 -07001214static void rtmsg_ifa(int event, struct in_ifaddr* ifa, struct nlmsghdr *nlh,
1215 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Thomas Graf47f68512006-08-04 23:04:36 -07001217 struct sk_buff *skb;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001218 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1219 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Thomas Graf339bf982006-11-10 14:10:15 -08001221 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
Thomas Graf47f68512006-08-04 23:04:36 -07001222 if (skb == NULL)
Thomas Grafd6062cb2006-08-15 00:33:59 -07001223 goto errout;
1224
1225 err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001226 if (err < 0) {
1227 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1228 WARN_ON(err == -EMSGSIZE);
1229 kfree_skb(skb);
1230 goto errout;
1231 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001232 err = rtnl_notify(skb, &init_net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
Thomas Grafd6062cb2006-08-15 00:33:59 -07001233errout:
1234 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001235 rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#ifdef CONFIG_SYSCTL
1239
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001240static void devinet_copy_dflt_conf(struct net *net, int i)
Herbert Xu31be3082007-06-04 23:35:37 -07001241{
1242 struct net_device *dev;
1243
1244 read_lock(&dev_base_lock);
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001245 for_each_netdev(net, dev) {
Herbert Xu31be3082007-06-04 23:35:37 -07001246 struct in_device *in_dev;
1247 rcu_read_lock();
1248 in_dev = __in_dev_get_rcu(dev);
1249 if (in_dev && !test_bit(i, in_dev->cnf.state))
1250 in_dev->cnf.data[i] = ipv4_devconf_dflt.data[i];
1251 rcu_read_unlock();
1252 }
1253 read_unlock(&dev_base_lock);
1254}
1255
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001256static void inet_forward_change(struct net *net)
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001257{
1258 struct net_device *dev;
1259 int on = IPV4_DEVCONF_ALL(FORWARDING);
1260
1261 IPV4_DEVCONF_ALL(ACCEPT_REDIRECTS) = !on;
1262 IPV4_DEVCONF_DFLT(FORWARDING) = on;
1263
1264 read_lock(&dev_base_lock);
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001265 for_each_netdev(net, dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001266 struct in_device *in_dev;
1267 rcu_read_lock();
1268 in_dev = __in_dev_get_rcu(dev);
1269 if (in_dev)
1270 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1271 rcu_read_unlock();
1272 }
1273 read_unlock(&dev_base_lock);
1274
1275 rt_cache_flush(0);
1276}
1277
Herbert Xu31be3082007-06-04 23:35:37 -07001278static int devinet_conf_proc(ctl_table *ctl, int write,
1279 struct file* filp, void __user *buffer,
1280 size_t *lenp, loff_t *ppos)
1281{
1282 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1283
1284 if (write) {
1285 struct ipv4_devconf *cnf = ctl->extra1;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001286 struct net *net = ctl->extra2;
Herbert Xu31be3082007-06-04 23:35:37 -07001287 int i = (int *)ctl->data - cnf->data;
1288
1289 set_bit(i, cnf->state);
1290
1291 if (cnf == &ipv4_devconf_dflt)
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001292 devinet_copy_dflt_conf(net, i);
Herbert Xu31be3082007-06-04 23:35:37 -07001293 }
1294
1295 return ret;
1296}
1297
1298static int devinet_conf_sysctl(ctl_table *table, int __user *name, int nlen,
1299 void __user *oldval, size_t __user *oldlenp,
1300 void __user *newval, size_t newlen)
1301{
1302 struct ipv4_devconf *cnf;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001303 struct net *net;
Herbert Xu31be3082007-06-04 23:35:37 -07001304 int *valp = table->data;
1305 int new;
1306 int i;
1307
1308 if (!newval || !newlen)
1309 return 0;
1310
1311 if (newlen != sizeof(int))
1312 return -EINVAL;
1313
1314 if (get_user(new, (int __user *)newval))
1315 return -EFAULT;
1316
1317 if (new == *valp)
1318 return 0;
1319
1320 if (oldval && oldlenp) {
1321 size_t len;
1322
1323 if (get_user(len, oldlenp))
1324 return -EFAULT;
1325
1326 if (len) {
1327 if (len > table->maxlen)
1328 len = table->maxlen;
1329 if (copy_to_user(oldval, valp, len))
1330 return -EFAULT;
1331 if (put_user(len, oldlenp))
1332 return -EFAULT;
1333 }
1334 }
1335
1336 *valp = new;
1337
1338 cnf = table->extra1;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001339 net = table->extra2;
Herbert Xu31be3082007-06-04 23:35:37 -07001340 i = (int *)table->data - cnf->data;
1341
1342 set_bit(i, cnf->state);
1343
1344 if (cnf == &ipv4_devconf_dflt)
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001345 devinet_copy_dflt_conf(net, i);
Herbert Xu31be3082007-06-04 23:35:37 -07001346
1347 return 1;
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static int devinet_sysctl_forward(ctl_table *ctl, int write,
1351 struct file* filp, void __user *buffer,
1352 size_t *lenp, loff_t *ppos)
1353{
1354 int *valp = ctl->data;
1355 int val = *valp;
1356 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1357
1358 if (write && *valp != val) {
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001359 struct net *net = ctl->extra2;
1360
Herbert Xu42f811b2007-06-04 23:34:44 -07001361 if (valp == &IPV4_DEVCONF_ALL(FORWARDING))
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001362 inet_forward_change(net);
Herbert Xu42f811b2007-06-04 23:34:44 -07001363 else if (valp != &IPV4_DEVCONF_DFLT(FORWARDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 rt_cache_flush(0);
1365 }
1366
1367 return ret;
1368}
1369
1370int ipv4_doint_and_flush(ctl_table *ctl, int write,
1371 struct file* filp, void __user *buffer,
1372 size_t *lenp, loff_t *ppos)
1373{
1374 int *valp = ctl->data;
1375 int val = *valp;
1376 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1377
1378 if (write && *valp != val)
1379 rt_cache_flush(0);
1380
1381 return ret;
1382}
1383
1384int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1385 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08001386 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Herbert Xu31be3082007-06-04 23:35:37 -07001388 int ret = devinet_conf_sysctl(table, name, nlen, oldval, oldlenp,
1389 newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Herbert Xu31be3082007-06-04 23:35:37 -07001391 if (ret == 1)
1392 rt_cache_flush(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Herbert Xu31be3082007-06-04 23:35:37 -07001394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397
Herbert Xu42f811b2007-06-04 23:34:44 -07001398#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \
1399 { \
1400 .ctl_name = NET_IPV4_CONF_ ## attr, \
1401 .procname = name, \
1402 .data = ipv4_devconf.data + \
1403 NET_IPV4_CONF_ ## attr - 1, \
1404 .maxlen = sizeof(int), \
1405 .mode = mval, \
1406 .proc_handler = proc, \
1407 .strategy = sysctl, \
Herbert Xu31be3082007-06-04 23:35:37 -07001408 .extra1 = &ipv4_devconf, \
Herbert Xu42f811b2007-06-04 23:34:44 -07001409 }
1410
1411#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
Herbert Xu31be3082007-06-04 23:35:37 -07001412 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \
1413 devinet_conf_sysctl)
Herbert Xu42f811b2007-06-04 23:34:44 -07001414
1415#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
Herbert Xu31be3082007-06-04 23:35:37 -07001416 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \
1417 devinet_conf_sysctl)
Herbert Xu42f811b2007-06-04 23:34:44 -07001418
1419#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \
1420 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl)
1421
1422#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1423 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \
1424 ipv4_doint_and_flush_strategy)
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static struct devinet_sysctl_table {
1427 struct ctl_table_header *sysctl_header;
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001428 struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1429 char *dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430} devinet_sysctl = {
1431 .devinet_vars = {
Herbert Xu42f811b2007-06-04 23:34:44 -07001432 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
Herbert Xu31be3082007-06-04 23:35:37 -07001433 devinet_sysctl_forward,
1434 devinet_conf_sysctl),
Herbert Xu42f811b2007-06-04 23:34:44 -07001435 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1436
1437 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1438 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1439 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1440 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1441 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1442 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1443 "accept_source_route"),
1444 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1445 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1446 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1447 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1448 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1449 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1450 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1451 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1452 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1453
1454 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1455 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1456 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1457 "force_igmp_version"),
1458 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1459 "promote_secondaries"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461};
1462
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001463static int __devinet_sysctl_register(struct net *net, char *dev_name,
1464 int ctl_name, struct ipv4_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 int i;
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001467 struct devinet_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001469#define DEVINET_CTL_PATH_DEV 3
1470
1471 struct ctl_path devinet_ctl_path[] = {
1472 { .procname = "net", .ctl_name = CTL_NET, },
1473 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1474 { .procname = "conf", .ctl_name = NET_IPV4_CONF, },
1475 { /* to be set */ },
1476 { },
1477 };
1478
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001479 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (!t)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001481 goto out;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1484 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Herbert Xu31be3082007-06-04 23:35:37 -07001485 t->devinet_vars[i].extra1 = p;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001486 t->devinet_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001489 /*
1490 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * by sysctl and we wouldn't want anyone to change it under our feet
1492 * (see SIOCSIFNAME).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001493 */
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001494 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1495 if (!t->dev_name)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001496 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001498 devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
1499 devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001501 t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001502 t->devinet_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (!t->sysctl_header)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001504 goto free_procname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 p->sysctl = t;
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001509free_procname:
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001510 kfree(t->dev_name);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001511free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 kfree(t);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001513out:
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001514 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001517static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
1518{
1519 struct devinet_sysctl_table *t = cnf->sysctl;
1520
1521 if (t == NULL)
1522 return;
1523
1524 cnf->sysctl = NULL;
1525 unregister_sysctl_table(t->sysctl_header);
1526 kfree(t->dev_name);
1527 kfree(t);
1528}
1529
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001530static void devinet_sysctl_register(struct in_device *idev)
1531{
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001532 neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4,
1533 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001534 __devinet_sysctl_register(idev->dev->nd_net, idev->dev->name,
1535 idev->dev->ifindex, &idev->cnf);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001536}
1537
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001538static void devinet_sysctl_unregister(struct in_device *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001540 __devinet_sysctl_unregister(&idev->cnf);
1541 neigh_sysctl_unregister(idev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543#endif
1544
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001545static struct ctl_table ctl_forward_entry[] = {
1546 {
1547 .ctl_name = NET_IPV4_FORWARD,
1548 .procname = "ip_forward",
1549 .data = &ipv4_devconf.data[
1550 NET_IPV4_CONF_FORWARDING - 1],
1551 .maxlen = sizeof(int),
1552 .mode = 0644,
1553 .proc_handler = devinet_sysctl_forward,
1554 .strategy = devinet_conf_sysctl,
1555 .extra1 = &ipv4_devconf,
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001556 .extra2 = &init_net,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001557 },
1558 { },
1559};
1560
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001561static __net_initdata struct ctl_path net_ipv4_path[] = {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001562 { .procname = "net", .ctl_name = CTL_NET, },
1563 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1564 { },
1565};
1566
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001567static __net_init int devinet_init_net(struct net *net)
1568{
1569 int err;
1570 struct ctl_table *tbl;
1571 struct ipv4_devconf *all, *dflt;
1572 struct ctl_table_header *forw_hdr;
1573
1574 err = -ENOMEM;
1575 all = &ipv4_devconf;
1576 dflt = &ipv4_devconf_dflt;
1577 tbl = ctl_forward_entry;
1578
1579 if (net != &init_net) {
1580 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1581 if (all == NULL)
1582 goto err_alloc_all;
1583
1584 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1585 if (dflt == NULL)
1586 goto err_alloc_dflt;
1587
1588 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1589 if (tbl == NULL)
1590 goto err_alloc_ctl;
1591
1592 tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1];
1593 tbl[0].extra1 = all;
1594 tbl[0].extra2 = net;
1595 }
1596
1597#ifdef CONFIG_SYSCTL
1598 err = __devinet_sysctl_register(net, "all",
1599 NET_PROTO_CONF_ALL, all);
1600 if (err < 0)
1601 goto err_reg_all;
1602
1603 err = __devinet_sysctl_register(net, "default",
1604 NET_PROTO_CONF_DEFAULT, dflt);
1605 if (err < 0)
1606 goto err_reg_dflt;
1607
1608 err = -ENOMEM;
1609 forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
1610 if (forw_hdr == NULL)
1611 goto err_reg_ctl;
1612#endif
1613
1614 net->ipv4.forw_hdr = forw_hdr;
1615 net->ipv4.devconf_all = all;
1616 net->ipv4.devconf_dflt = dflt;
1617 return 0;
1618
1619#ifdef CONFIG_SYSCTL
1620err_reg_ctl:
1621 __devinet_sysctl_unregister(dflt);
1622err_reg_dflt:
1623 __devinet_sysctl_unregister(all);
1624err_reg_all:
1625 if (tbl != ctl_forward_entry)
1626 kfree(tbl);
1627#endif
1628err_alloc_ctl:
1629 if (dflt != &ipv4_devconf_dflt)
1630 kfree(dflt);
1631err_alloc_dflt:
1632 if (all != &ipv4_devconf)
1633 kfree(all);
1634err_alloc_all:
1635 return err;
1636}
1637
1638static __net_exit void devinet_exit_net(struct net *net)
1639{
1640 struct ctl_table *tbl;
1641
1642 tbl = net->ipv4.forw_hdr->ctl_table_arg;
1643#ifdef CONFIG_SYSCTL
1644 unregister_net_sysctl_table(net->ipv4.forw_hdr);
1645 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1646 __devinet_sysctl_unregister(net->ipv4.devconf_all);
1647#endif
1648 kfree(tbl);
1649 kfree(net->ipv4.devconf_dflt);
1650 kfree(net->ipv4.devconf_all);
1651}
1652
1653static __net_initdata struct pernet_operations devinet_ops = {
1654 .init = devinet_init_net,
1655 .exit = devinet_exit_net,
1656};
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658void __init devinet_init(void)
1659{
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001660 register_pernet_subsys(&devinet_ops);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 register_gifconf(PF_INET, inet_gifconf);
1663 register_netdevice_notifier(&ip_netdev_notifier);
Thomas Graf63f34442007-03-22 11:55:17 -07001664
1665 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
1666 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
1667 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670EXPORT_SYMBOL(in_dev_finish_destroy);
1671EXPORT_SYMBOL(inet_select_addr);
1672EXPORT_SYMBOL(inetdev_by_index);
1673EXPORT_SYMBOL(register_inetaddr_notifier);
1674EXPORT_SYMBOL(unregister_inetaddr_notifier);