blob: 2fe50765a6723471736d636c533df9ee92c7f8b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET3 IP device support routines.
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the IP parts of dev.c 1.0.19
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 *
18 * Changes:
19 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
20 * lists.
21 * Cyrus Durgin: updated for kmod
22 * Matthias Andree: in devinet_ioctl, compare label and
23 * address (4.4BSD alias style support),
24 * fall back to comparing just the label
25 * if no match found.
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/uaccess.h>
30#include <asm/system.h>
31#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
34#include <linux/types.h>
35#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/socket.h>
39#include <linux/sockios.h>
40#include <linux/in.h>
41#include <linux/errno.h>
42#include <linux/interrupt.h>
Thomas Graf18237302006-08-04 23:04:54 -070043#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/if_ether.h>
45#include <linux/inet.h>
46#include <linux/netdevice.h>
47#include <linux/etherdevice.h>
48#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/init.h>
50#include <linux/notifier.h>
51#include <linux/inetdevice.h>
52#include <linux/igmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
David S. Millerfd23c3b2011-02-18 12:42:28 -080054#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#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
Adrian Bunk0027ba82008-01-31 17:17:31 -080067static struct ipv4_devconf ipv4_devconf = {
Herbert Xu42f811b2007-06-04 23:34:44 -070068 .data = {
Eric W. Biederman02291682010-02-14 03:25:51 +000069 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
70 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
71 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
72 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
Herbert Xu42f811b2007-06-04 23:34:44 -070073 },
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 = {
Eric W. Biederman02291682010-02-14 03:25:51 +000078 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
79 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
80 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
81 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
82 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
Herbert Xu42f811b2007-06-04 23:34:44 -070083 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -080086#define IPV4_DEVCONF_DFLT(net, attr) \
87 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
Herbert Xu42f811b2007-06-04 23:34:44 -070088
Patrick McHardyef7c79e2007-06-05 12:38:30 -070089static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
Thomas Graf5c753972006-08-04 23:03:53 -070090 [IFA_LOCAL] = { .type = NLA_U32 },
91 [IFA_ADDRESS] = { .type = NLA_U32 },
92 [IFA_BROADCAST] = { .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
David S. Millerfd23c3b2011-02-18 12:42:28 -080096/* inet_addr_hash's shifting is dependent upon this IN4_ADDR_HSIZE
97 * value. So if you change this define, make appropriate changes to
98 * inet_addr_hash as well.
99 */
100#define IN4_ADDR_HSIZE 256
101static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
102static DEFINE_SPINLOCK(inet_addr_hash_lock);
103
104static inline unsigned int inet_addr_hash(struct net *net, __be32 addr)
105{
106 u32 val = (__force u32) addr ^ hash_ptr(net, 8);
107
108 return ((val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24)) &
109 (IN4_ADDR_HSIZE - 1));
110}
111
112static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
113{
114 unsigned int hash = inet_addr_hash(net, ifa->ifa_address);
115
116 spin_lock(&inet_addr_hash_lock);
117 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
118 spin_unlock(&inet_addr_hash_lock);
119}
120
121static void inet_hash_remove(struct in_ifaddr *ifa)
122{
123 spin_lock(&inet_addr_hash_lock);
124 hlist_del_init_rcu(&ifa->hash);
125 spin_unlock(&inet_addr_hash_lock);
126}
127
Thomas Grafd6062cb2006-08-15 00:33:59 -0700128static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Alan Sterne041c682006-03-27 01:16:30 -0800130static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
132 int destroy);
133#ifdef CONFIG_SYSCTL
Pavel Emelyanov66f27a52007-12-02 00:55:54 +1100134static void devinet_sysctl_register(struct in_device *idev);
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800135static void devinet_sysctl_unregister(struct in_device *idev);
136#else
137static inline void devinet_sysctl_register(struct in_device *idev)
138{
139}
140static inline void devinet_sysctl_unregister(struct in_device *idev)
141{
142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#endif
144
145/* Locks all the inet devices. */
146
147static struct in_ifaddr *inet_alloc_ifa(void)
148{
Alexey Dobriyan93adcc82008-10-28 13:25:09 -0700149 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static void inet_rcu_free_ifa(struct rcu_head *head)
153{
154 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
155 if (ifa->ifa_dev)
156 in_dev_put(ifa->ifa_dev);
157 kfree(ifa);
158}
159
160static inline void inet_free_ifa(struct in_ifaddr *ifa)
161{
162 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
163}
164
165void in_dev_finish_destroy(struct in_device *idev)
166{
167 struct net_device *dev = idev->dev;
168
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700169 WARN_ON(idev->ifa_list);
170 WARN_ON(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#ifdef NET_REFCNT_DEBUG
172 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
173 idev, dev ? dev->name : "NIL");
174#endif
175 dev_put(dev);
176 if (!idev->dead)
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800177 pr_err("Freeing alive in_device %p\n", idev);
178 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 kfree(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800181EXPORT_SYMBOL(in_dev_finish_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Herbert Xu71e27da2007-06-04 23:36:06 -0700183static struct in_device *inetdev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct in_device *in_dev;
186
187 ASSERT_RTNL();
188
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700189 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!in_dev)
191 goto out;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900192 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -0800193 sizeof(in_dev->cnf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 in_dev->cnf.sysctl = NULL;
195 in_dev->dev = dev;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800196 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
197 if (!in_dev->arp_parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out_kfree;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700199 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
200 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Reference in_dev->dev */
202 dev_hold(dev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800203 /* Account for reference dev->ip_ptr (below) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 in_dev_hold(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Pavel Emelyanov66f27a52007-12-02 00:55:54 +1100206 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 ip_mc_init_dev(in_dev);
208 if (dev->flags & IFF_UP)
209 ip_mc_up(in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800210
David L Stevens30c4cf52007-01-04 12:31:14 -0800211 /* we can receive as soon as ip_ptr is set -- do this last */
212 rcu_assign_pointer(dev->ip_ptr, in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return in_dev;
215out_kfree:
216 kfree(in_dev);
217 in_dev = NULL;
218 goto out;
219}
220
221static void in_dev_rcu_put(struct rcu_head *head)
222{
223 struct in_device *idev = container_of(head, struct in_device, rcu_head);
224 in_dev_put(idev);
225}
226
227static void inetdev_destroy(struct in_device *in_dev)
228{
229 struct in_ifaddr *ifa;
230 struct net_device *dev;
231
232 ASSERT_RTNL();
233
234 dev = in_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 in_dev->dead = 1;
237
238 ip_mc_destroy_dev(in_dev);
239
240 while ((ifa = in_dev->ifa_list) != NULL) {
241 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
242 inet_free_ifa(ifa);
243 }
244
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000245 rcu_assign_pointer(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800247 devinet_sysctl_unregister(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
249 arp_ifdown(dev);
250
251 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
252}
253
Al Viroff428d72006-09-26 22:13:35 -0700254int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 rcu_read_lock();
257 for_primary_ifa(in_dev) {
258 if (inet_ifa_match(a, ifa)) {
259 if (!b || inet_ifa_match(b, ifa)) {
260 rcu_read_unlock();
261 return 1;
262 }
263 }
264 } endfor_ifa(in_dev);
265 rcu_read_unlock();
266 return 0;
267}
268
Thomas Grafd6062cb2006-08-15 00:33:59 -0700269static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
270 int destroy, struct nlmsghdr *nlh, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Harald Welte8f937c62005-05-29 20:23:46 -0700272 struct in_ifaddr *promote = NULL;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800273 struct in_ifaddr *ifa, *ifa1 = *ifap;
274 struct in_ifaddr *last_prim = in_dev->ifa_list;
275 struct in_ifaddr *prev_prom = NULL;
276 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 ASSERT_RTNL();
279
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900280 /* 1. Deleting primary ifaddr forces deletion all secondaries
Harald Welte8f937c62005-05-29 20:23:46 -0700281 * unless alias promotion is set
282 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
286
287 while ((ifa = *ifap1) != NULL) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900288 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800289 ifa1->ifa_scope <= ifa->ifa_scope)
290 last_prim = ifa;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
293 ifa1->ifa_mask != ifa->ifa_mask ||
294 !inet_ifa_match(ifa1->ifa_address, ifa)) {
295 ifap1 = &ifa->ifa_next;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800296 prev_prom = ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 continue;
298 }
299
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800300 if (!do_promote) {
David S. Millerfd23c3b2011-02-18 12:42:28 -0800301 inet_hash_remove(ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700302 *ifap1 = ifa->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Thomas Grafd6062cb2006-08-15 00:33:59 -0700304 rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800305 blocking_notifier_call_chain(&inetaddr_chain,
306 NETDEV_DOWN, ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700307 inet_free_ifa(ifa);
308 } else {
309 promote = ifa;
310 break;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314
315 /* 2. Unlink it */
316
317 *ifap = ifa1->ifa_next;
David S. Millerfd23c3b2011-02-18 12:42:28 -0800318 inet_hash_remove(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* 3. Announce address deletion */
321
322 /* Send message first, then call notifier.
323 At first sight, FIB update triggered by notifier
324 will refer to already deleted ifaddr, that could confuse
325 netlink listeners. It is not true: look, gated sees
326 that route deleted and if it still thinks that ifaddr
327 is valid, it will try to restore deleted routes... Grr.
328 So that, this order is correct.
329 */
Thomas Grafd6062cb2006-08-15 00:33:59 -0700330 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800331 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800332
333 if (promote) {
334
335 if (prev_prom) {
336 prev_prom->ifa_next = promote->ifa_next;
337 promote->ifa_next = last_prim->ifa_next;
338 last_prim->ifa_next = promote;
339 }
340
341 promote->ifa_flags &= ~IFA_F_SECONDARY;
Thomas Grafd6062cb2006-08-15 00:33:59 -0700342 rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800343 blocking_notifier_call_chain(&inetaddr_chain,
344 NETDEV_UP, promote);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800345 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
346 if (ifa1->ifa_mask != ifa->ifa_mask ||
347 !inet_ifa_match(ifa1->ifa_address, ifa))
348 continue;
349 fib_add_ifaddr(ifa);
350 }
351
352 }
Herbert Xu63630972007-06-07 18:35:38 -0700353 if (destroy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 inet_free_ifa(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Thomas Grafd6062cb2006-08-15 00:33:59 -0700357static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
358 int destroy)
359{
360 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
361}
362
363static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
364 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct in_device *in_dev = ifa->ifa_dev;
367 struct in_ifaddr *ifa1, **ifap, **last_primary;
368
369 ASSERT_RTNL();
370
371 if (!ifa->ifa_local) {
372 inet_free_ifa(ifa);
373 return 0;
374 }
375
376 ifa->ifa_flags &= ~IFA_F_SECONDARY;
377 last_primary = &in_dev->ifa_list;
378
379 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
380 ifap = &ifa1->ifa_next) {
381 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
382 ifa->ifa_scope <= ifa1->ifa_scope)
383 last_primary = &ifa1->ifa_next;
384 if (ifa1->ifa_mask == ifa->ifa_mask &&
385 inet_ifa_match(ifa1->ifa_address, ifa)) {
386 if (ifa1->ifa_local == ifa->ifa_local) {
387 inet_free_ifa(ifa);
388 return -EEXIST;
389 }
390 if (ifa1->ifa_scope != ifa->ifa_scope) {
391 inet_free_ifa(ifa);
392 return -EINVAL;
393 }
394 ifa->ifa_flags |= IFA_F_SECONDARY;
395 }
396 }
397
398 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
399 net_srandom(ifa->ifa_local);
400 ifap = last_primary;
401 }
402
403 ifa->ifa_next = *ifap;
404 *ifap = ifa;
405
David S. Millerfd23c3b2011-02-18 12:42:28 -0800406 inet_hash_insert(dev_net(in_dev->dev), ifa);
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Send message first, then call notifier.
409 Notifier will trigger FIB update, so that
410 listeners of netlink will know about new ifaddr */
Thomas Grafd6062cb2006-08-15 00:33:59 -0700411 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
Alan Sterne041c682006-03-27 01:16:30 -0800412 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return 0;
415}
416
Thomas Grafd6062cb2006-08-15 00:33:59 -0700417static int inet_insert_ifa(struct in_ifaddr *ifa)
418{
419 return __inet_insert_ifa(ifa, NULL, 0);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
423{
Herbert Xue5ed6392005-10-03 14:35:55 -0700424 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 ASSERT_RTNL();
427
428 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700429 inet_free_ifa(ifa);
430 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Herbert Xu71e27da2007-06-04 23:36:06 -0700432 ipv4_devconf_setall(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ifa->ifa_dev != in_dev) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700434 WARN_ON(ifa->ifa_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 in_dev_hold(in_dev);
436 ifa->ifa_dev = in_dev;
437 }
Joe Perchesf97c1e02007-12-16 13:45:43 -0800438 if (ipv4_is_loopback(ifa->ifa_local))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ifa->ifa_scope = RT_SCOPE_HOST;
440 return inet_insert_ifa(ifa);
441}
442
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000443/* Caller must hold RCU or RTNL :
444 * We dont take a reference on found in_device
445 */
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800446struct in_device *inetdev_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct net_device *dev;
449 struct in_device *in_dev = NULL;
Eric Dumazetc148fc22009-11-01 19:23:04 +0000450
451 rcu_read_lock();
452 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000454 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Eric Dumazetc148fc22009-11-01 19:23:04 +0000455 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return in_dev;
457}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800458EXPORT_SYMBOL(inetdev_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/* Called only from RTNL semaphored context. No locks. */
461
Al Viro60cad5d2006-09-26 22:17:09 -0700462struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
463 __be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 ASSERT_RTNL();
466
467 for_primary_ifa(in_dev) {
468 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
469 return ifa;
470 } endfor_ifa(in_dev);
471 return NULL;
472}
473
474static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
475{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900476 struct net *net = sock_net(skb->sk);
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700477 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct in_device *in_dev;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700479 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct in_ifaddr *ifa, **ifap;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700481 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 ASSERT_RTNL();
484
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700485 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
486 if (err < 0)
487 goto errout;
488
489 ifm = nlmsg_data(nlh);
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800490 in_dev = inetdev_by_index(net, ifm->ifa_index);
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700491 if (in_dev == NULL) {
492 err = -ENODEV;
493 goto errout;
494 }
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
497 ifap = &ifa->ifa_next) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700498 if (tb[IFA_LOCAL] &&
Al Viroa7a628c2006-09-26 22:16:43 -0700499 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 continue;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700501
502 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
503 continue;
504
505 if (tb[IFA_ADDRESS] &&
506 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
Al Viroa7a628c2006-09-26 22:16:43 -0700507 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700508 continue;
509
Thomas Grafd6062cb2006-08-15 00:33:59 -0700510 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512 }
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700513
514 err = -EADDRNOTAVAIL;
515errout:
516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800519static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Thomas Graf5c753972006-08-04 23:03:53 -0700521 struct nlattr *tb[IFA_MAX+1];
522 struct in_ifaddr *ifa;
523 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct net_device *dev;
525 struct in_device *in_dev;
Denis V. Lunev7b218572008-01-31 18:47:00 -0800526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Thomas Graf5c753972006-08-04 23:03:53 -0700528 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
529 if (err < 0)
530 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Thomas Graf5c753972006-08-04 23:03:53 -0700532 ifm = nlmsg_data(nlh);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800533 err = -EINVAL;
534 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
Thomas Graf5c753972006-08-04 23:03:53 -0700535 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800537 dev = __dev_get_by_index(net, ifm->ifa_index);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800538 err = -ENODEV;
539 if (dev == NULL)
Thomas Graf5c753972006-08-04 23:03:53 -0700540 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Thomas Graf5c753972006-08-04 23:03:53 -0700542 in_dev = __in_dev_get_rtnl(dev);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800543 err = -ENOBUFS;
544 if (in_dev == NULL)
Herbert Xu71e27da2007-06-04 23:36:06 -0700545 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Thomas Graf5c753972006-08-04 23:03:53 -0700547 ifa = inet_alloc_ifa();
Denis V. Lunev7b218572008-01-31 18:47:00 -0800548 if (ifa == NULL)
Thomas Graf5c753972006-08-04 23:03:53 -0700549 /*
550 * A potential indev allocation can be left alive, it stays
551 * assigned to its device and is destroy with it.
552 */
Thomas Graf5c753972006-08-04 23:03:53 -0700553 goto errout;
Thomas Graf5c753972006-08-04 23:03:53 -0700554
Pavel Emelyanova4e65d32007-12-07 23:55:43 -0800555 ipv4_devconf_setall(in_dev);
Thomas Graf5c753972006-08-04 23:03:53 -0700556 in_dev_hold(in_dev);
557
558 if (tb[IFA_ADDRESS] == NULL)
559 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
560
David S. Millerfd23c3b2011-02-18 12:42:28 -0800561 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
563 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ifa->ifa_flags = ifm->ifa_flags;
565 ifa->ifa_scope = ifm->ifa_scope;
Thomas Graf5c753972006-08-04 23:03:53 -0700566 ifa->ifa_dev = in_dev;
567
Al Viroa7a628c2006-09-26 22:16:43 -0700568 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
569 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
Thomas Graf5c753972006-08-04 23:03:53 -0700570
571 if (tb[IFA_BROADCAST])
Al Viroa7a628c2006-09-26 22:16:43 -0700572 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700573
Thomas Graf5c753972006-08-04 23:03:53 -0700574 if (tb[IFA_LABEL])
575 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 else
577 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
578
Thomas Graf5c753972006-08-04 23:03:53 -0700579 return ifa;
580
581errout:
582 return ERR_PTR(err);
583}
584
585static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
586{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900587 struct net *net = sock_net(skb->sk);
Thomas Graf5c753972006-08-04 23:03:53 -0700588 struct in_ifaddr *ifa;
589
590 ASSERT_RTNL();
591
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800592 ifa = rtm_to_ifaddr(net, nlh);
Thomas Graf5c753972006-08-04 23:03:53 -0700593 if (IS_ERR(ifa))
594 return PTR_ERR(ifa);
595
Thomas Grafd6062cb2006-08-15 00:33:59 -0700596 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * Determine a default network mask, based on the IP address.
601 */
602
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800603static inline int inet_abc_len(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 int rc = -1; /* Something else, probably a multicast. */
606
Joe Perchesf97c1e02007-12-16 13:45:43 -0800607 if (ipv4_is_zeronet(addr))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900608 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else {
Al Viro714e85b2006-11-14 20:51:49 -0800610 __u32 haddr = ntohl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Al Viro714e85b2006-11-14 20:51:49 -0800612 if (IN_CLASSA(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rc = 8;
Al Viro714e85b2006-11-14 20:51:49 -0800614 else if (IN_CLASSB(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 rc = 16;
Al Viro714e85b2006-11-14 20:51:49 -0800616 else if (IN_CLASSC(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 rc = 24;
618 }
619
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900620 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623
Denis V. Luneve5b13cb2008-02-28 20:51:43 -0800624int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct ifreq ifr;
627 struct sockaddr_in sin_orig;
628 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
629 struct in_device *in_dev;
630 struct in_ifaddr **ifap = NULL;
631 struct in_ifaddr *ifa = NULL;
632 struct net_device *dev;
633 char *colon;
634 int ret = -EFAULT;
635 int tryaddrmatch = 0;
636
637 /*
638 * Fetch the caller's info block into kernel space
639 */
640
641 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
642 goto out;
643 ifr.ifr_name[IFNAMSIZ - 1] = 0;
644
645 /* save original address for comparison */
646 memcpy(&sin_orig, sin, sizeof(*sin));
647
648 colon = strchr(ifr.ifr_name, ':');
649 if (colon)
650 *colon = 0;
651
Denis V. Luneve5b13cb2008-02-28 20:51:43 -0800652 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Stephen Hemminger132adf52007-03-08 20:44:43 -0800654 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 case SIOCGIFADDR: /* Get interface address */
656 case SIOCGIFBRDADDR: /* Get the broadcast address */
657 case SIOCGIFDSTADDR: /* Get the destination address */
658 case SIOCGIFNETMASK: /* Get the netmask for the interface */
659 /* Note that these ioctls will not sleep,
660 so that we do not impose a lock.
661 One day we will be forced to put shlock here (I mean SMP)
662 */
663 tryaddrmatch = (sin_orig.sin_family == AF_INET);
664 memset(sin, 0, sizeof(*sin));
665 sin->sin_family = AF_INET;
666 break;
667
668 case SIOCSIFFLAGS:
669 ret = -EACCES;
670 if (!capable(CAP_NET_ADMIN))
671 goto out;
672 break;
673 case SIOCSIFADDR: /* Set interface address (and family) */
674 case SIOCSIFBRDADDR: /* Set the broadcast address */
675 case SIOCSIFDSTADDR: /* Set the destination address */
676 case SIOCSIFNETMASK: /* Set the netmask for the interface */
677 ret = -EACCES;
678 if (!capable(CAP_NET_ADMIN))
679 goto out;
680 ret = -EINVAL;
681 if (sin->sin_family != AF_INET)
682 goto out;
683 break;
684 default:
685 ret = -EINVAL;
686 goto out;
687 }
688
689 rtnl_lock();
690
691 ret = -ENODEV;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800692 dev = __dev_get_by_name(net, ifr.ifr_name);
693 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto done;
695
696 if (colon)
697 *colon = ':';
698
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800699 in_dev = __in_dev_get_rtnl(dev);
700 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (tryaddrmatch) {
702 /* Matthias Andree */
703 /* compare label and address (4.4BSD style) */
704 /* note: we only do this for a limited set of ioctls
705 and only if the original address family was AF_INET.
706 This is checked above. */
707 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
708 ifap = &ifa->ifa_next) {
709 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
710 sin_orig.sin_addr.s_addr ==
711 ifa->ifa_address) {
712 break; /* found */
713 }
714 }
715 }
716 /* we didn't get a match, maybe the application is
717 4.3BSD-style and passed in junk so we fall back to
718 comparing just the label */
719 if (!ifa) {
720 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
721 ifap = &ifa->ifa_next)
722 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
723 break;
724 }
725 }
726
727 ret = -EADDRNOTAVAIL;
728 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
729 goto done;
730
Stephen Hemminger132adf52007-03-08 20:44:43 -0800731 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case SIOCGIFADDR: /* Get interface address */
733 sin->sin_addr.s_addr = ifa->ifa_local;
734 goto rarok;
735
736 case SIOCGIFBRDADDR: /* Get the broadcast address */
737 sin->sin_addr.s_addr = ifa->ifa_broadcast;
738 goto rarok;
739
740 case SIOCGIFDSTADDR: /* Get the destination address */
741 sin->sin_addr.s_addr = ifa->ifa_address;
742 goto rarok;
743
744 case SIOCGIFNETMASK: /* Get the netmask for the interface */
745 sin->sin_addr.s_addr = ifa->ifa_mask;
746 goto rarok;
747
748 case SIOCSIFFLAGS:
749 if (colon) {
750 ret = -EADDRNOTAVAIL;
751 if (!ifa)
752 break;
753 ret = 0;
754 if (!(ifr.ifr_flags & IFF_UP))
755 inet_del_ifa(in_dev, ifap, 1);
756 break;
757 }
758 ret = dev_change_flags(dev, ifr.ifr_flags);
759 break;
760
761 case SIOCSIFADDR: /* Set interface address (and family) */
762 ret = -EINVAL;
763 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
764 break;
765
766 if (!ifa) {
767 ret = -ENOBUFS;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800768 ifa = inet_alloc_ifa();
David S. Millerfd23c3b2011-02-18 12:42:28 -0800769 INIT_HLIST_NODE(&ifa->hash);
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800770 if (!ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 if (colon)
773 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
774 else
775 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
776 } else {
777 ret = 0;
778 if (ifa->ifa_local == sin->sin_addr.s_addr)
779 break;
780 inet_del_ifa(in_dev, ifap, 0);
781 ifa->ifa_broadcast = 0;
Bjorn Mork148f9722008-02-26 18:17:53 -0800782 ifa->ifa_scope = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
786
787 if (!(dev->flags & IFF_POINTOPOINT)) {
788 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
789 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
790 if ((dev->flags & IFF_BROADCAST) &&
791 ifa->ifa_prefixlen < 31)
792 ifa->ifa_broadcast = ifa->ifa_address |
793 ~ifa->ifa_mask;
794 } else {
795 ifa->ifa_prefixlen = 32;
796 ifa->ifa_mask = inet_make_mask(32);
797 }
798 ret = inet_set_ifa(dev, ifa);
799 break;
800
801 case SIOCSIFBRDADDR: /* Set the broadcast address */
802 ret = 0;
803 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
804 inet_del_ifa(in_dev, ifap, 0);
805 ifa->ifa_broadcast = sin->sin_addr.s_addr;
806 inet_insert_ifa(ifa);
807 }
808 break;
809
810 case SIOCSIFDSTADDR: /* Set the destination address */
811 ret = 0;
812 if (ifa->ifa_address == sin->sin_addr.s_addr)
813 break;
814 ret = -EINVAL;
815 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
816 break;
817 ret = 0;
818 inet_del_ifa(in_dev, ifap, 0);
819 ifa->ifa_address = sin->sin_addr.s_addr;
820 inet_insert_ifa(ifa);
821 break;
822
823 case SIOCSIFNETMASK: /* Set the netmask for the interface */
824
825 /*
826 * The mask we set must be legal.
827 */
828 ret = -EINVAL;
829 if (bad_mask(sin->sin_addr.s_addr, 0))
830 break;
831 ret = 0;
832 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
Al Viroa144ea42006-09-28 18:00:55 -0700833 __be32 old_mask = ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 inet_del_ifa(in_dev, ifap, 0);
835 ifa->ifa_mask = sin->sin_addr.s_addr;
836 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
837
838 /* See if current broadcast address matches
839 * with current netmask, then recalculate
840 * the broadcast address. Otherwise it's a
841 * funny address, so don't touch it since
842 * the user seems to know what (s)he's doing...
843 */
844 if ((dev->flags & IFF_BROADCAST) &&
845 (ifa->ifa_prefixlen < 31) &&
846 (ifa->ifa_broadcast ==
David Engeldcab5e12005-10-21 22:09:16 -0500847 (ifa->ifa_local|~old_mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ifa->ifa_broadcast = (ifa->ifa_local |
849 ~sin->sin_addr.s_addr);
850 }
851 inet_insert_ifa(ifa);
852 }
853 break;
854 }
855done:
856 rtnl_unlock();
857out:
858 return ret;
859rarok:
860 rtnl_unlock();
861 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
862 goto out;
863}
864
865static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
866{
Herbert Xue5ed6392005-10-03 14:35:55 -0700867 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct in_ifaddr *ifa;
869 struct ifreq ifr;
870 int done = 0;
871
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800872 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto out;
874
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800875 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!buf) {
877 done += sizeof(ifr);
878 continue;
879 }
880 if (len < (int) sizeof(ifr))
881 break;
882 memset(&ifr, 0, sizeof(struct ifreq));
883 if (ifa->ifa_label)
884 strcpy(ifr.ifr_name, ifa->ifa_label);
885 else
886 strcpy(ifr.ifr_name, dev->name);
887
888 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
889 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
890 ifa->ifa_local;
891
892 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
893 done = -EFAULT;
894 break;
895 }
896 buf += sizeof(struct ifreq);
897 len -= sizeof(struct ifreq);
898 done += sizeof(struct ifreq);
899 }
900out:
901 return done;
902}
903
Al Viroa61ced52006-09-26 21:27:54 -0700904__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Al Viroa61ced52006-09-26 21:27:54 -0700906 __be32 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct in_device *in_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900908 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700911 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (!in_dev)
913 goto no_in_dev;
914
915 for_primary_ifa(in_dev) {
916 if (ifa->ifa_scope > scope)
917 continue;
918 if (!dst || inet_ifa_match(dst, ifa)) {
919 addr = ifa->ifa_local;
920 break;
921 }
922 if (!addr)
923 addr = ifa->ifa_local;
924 } endfor_ifa(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (addr)
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800927 goto out_unlock;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800928no_in_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /* Not loopback addresses on loopback should be preferred
931 in this case. It is importnat that lo is the first interface
932 in dev_base list.
933 */
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800934 for_each_netdev_rcu(net, dev) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800935 in_dev = __in_dev_get_rcu(dev);
936 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
938
939 for_primary_ifa(in_dev) {
940 if (ifa->ifa_scope != RT_SCOPE_LINK &&
941 ifa->ifa_scope <= scope) {
942 addr = ifa->ifa_local;
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800943 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 } endfor_ifa(in_dev);
946 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800947out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return addr;
950}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800951EXPORT_SYMBOL(inet_select_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Al Viro60cad5d2006-09-26 22:17:09 -0700953static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
954 __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 int same = 0;
Al Viroa144ea42006-09-28 18:00:55 -0700957 __be32 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 for_ifa(in_dev) {
960 if (!addr &&
961 (local == ifa->ifa_local || !local) &&
962 ifa->ifa_scope <= scope) {
963 addr = ifa->ifa_local;
964 if (same)
965 break;
966 }
967 if (!same) {
968 same = (!local || inet_ifa_match(local, ifa)) &&
969 (!dst || inet_ifa_match(dst, ifa));
970 if (same && addr) {
971 if (local || !dst)
972 break;
973 /* Is the selected addr into dst subnet? */
974 if (inet_ifa_match(addr, ifa))
975 break;
976 /* No, then can we use new local src? */
977 if (ifa->ifa_scope <= scope) {
978 addr = ifa->ifa_local;
979 break;
980 }
981 /* search for large dst subnet for addr */
982 same = 0;
983 }
984 }
985 } endfor_ifa(in_dev);
986
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800987 return same ? addr : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/*
991 * Confirm that local IP address exists using wildcards:
Denis V. Lunev9bd85e32008-01-14 23:05:55 -0800992 * - in_dev: only on this interface, 0=any interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * - dst: only in the same subnet as dst, 0=any dst
994 * - local: address, 0=autoselect the local address
995 * - scope: maximum allowed scope value for the local address
996 */
Denis V. Lunev9bd85e32008-01-14 23:05:55 -0800997__be32 inet_confirm_addr(struct in_device *in_dev,
998 __be32 dst, __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Al Viro60cad5d2006-09-26 22:17:09 -07001000 __be32 addr = 0;
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001001 struct net_device *dev;
Denis V. Lunev39a6d062008-01-14 23:06:19 -08001002 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Denis V. Lunev39a6d062008-01-14 23:06:19 -08001004 if (scope != RT_SCOPE_LINK)
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001005 return confirm_addr_indev(in_dev, dst, local, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001007 net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 rcu_read_lock();
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001009 for_each_netdev_rcu(net, dev) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001010 in_dev = __in_dev_get_rcu(dev);
1011 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 addr = confirm_addr_indev(in_dev, dst, local, scope);
1013 if (addr)
1014 break;
1015 }
1016 }
1017 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return addr;
1020}
1021
1022/*
1023 * Device notifier
1024 */
1025
1026int register_inetaddr_notifier(struct notifier_block *nb)
1027{
Alan Sterne041c682006-03-27 01:16:30 -08001028 return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001030EXPORT_SYMBOL(register_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032int unregister_inetaddr_notifier(struct notifier_block *nb)
1033{
Alan Sterne041c682006-03-27 01:16:30 -08001034 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001036EXPORT_SYMBOL(unregister_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001038/* Rename ifa_labels for a device name change. Make some effort to preserve
1039 * existing alias numbering and to create unique labels if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040*/
1041static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct in_ifaddr *ifa;
1044 int named = 0;
1045
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001046 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1047 char old[IFNAMSIZ], *dot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 memcpy(old, ifa->ifa_label, IFNAMSIZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001050 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (named++ == 0)
Thomas Graf573bf472008-06-10 15:40:04 -07001052 goto skip;
Mark McLoughlin44344b22008-01-04 00:56:25 -08001053 dot = strchr(old, ':');
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001054 if (dot == NULL) {
1055 sprintf(old, ":%d", named);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 dot = old;
1057 }
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001058 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001059 strcat(ifa->ifa_label, dot);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001060 else
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001061 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
Thomas Graf573bf472008-06-10 15:40:04 -07001062skip:
1063 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001064 }
1065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Breno Leitao06770842008-09-02 17:28:58 -07001067static inline bool inetdev_valid_mtu(unsigned mtu)
1068{
1069 return mtu >= 68;
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/* Called only under RTNL semaphore */
1073
1074static int inetdev_event(struct notifier_block *this, unsigned long event,
1075 void *ptr)
1076{
1077 struct net_device *dev = ptr;
Herbert Xue5ed6392005-10-03 14:35:55 -07001078 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 ASSERT_RTNL();
1081
1082 if (!in_dev) {
Herbert Xu8030f542007-02-22 01:53:47 +09001083 if (event == NETDEV_REGISTER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 in_dev = inetdev_init(dev);
Herbert Xub217d612007-07-30 17:04:52 -07001085 if (!in_dev)
1086 return notifier_from_errno(-ENOMEM);
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001087 if (dev->flags & IFF_LOOPBACK) {
Herbert Xu42f811b2007-06-04 23:34:44 -07001088 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1089 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
Herbert Xu8030f542007-02-22 01:53:47 +09001090 }
Breno Leitao06770842008-09-02 17:28:58 -07001091 } else if (event == NETDEV_CHANGEMTU) {
1092 /* Re-enabling IP */
1093 if (inetdev_valid_mtu(dev->mtu))
1094 in_dev = inetdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 goto out;
1097 }
1098
1099 switch (event) {
1100 case NETDEV_REGISTER:
1101 printk(KERN_DEBUG "inetdev_event: bug\n");
Eric Dumazet95ae6b22010-09-15 04:04:31 +00001102 rcu_assign_pointer(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
1104 case NETDEV_UP:
Breno Leitao06770842008-09-02 17:28:58 -07001105 if (!inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001107 if (dev->flags & IFF_LOOPBACK) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001108 struct in_ifaddr *ifa = inet_alloc_ifa();
1109
1110 if (ifa) {
David S. Millerfd23c3b2011-02-18 12:42:28 -08001111 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ifa->ifa_local =
1113 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1114 ifa->ifa_prefixlen = 8;
1115 ifa->ifa_mask = inet_make_mask(8);
1116 in_dev_hold(in_dev);
1117 ifa->ifa_dev = in_dev;
1118 ifa->ifa_scope = RT_SCOPE_HOST;
1119 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1120 inet_insert_ifa(ifa);
1121 }
1122 }
1123 ip_mc_up(in_dev);
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001124 /* fall through */
Ian Campbell06c46482010-05-26 00:09:42 +00001125 case NETDEV_NOTIFY_PEERS:
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001126 case NETDEV_CHANGEADDR:
Stephen Hemmingera21090c2009-10-07 03:18:17 -07001127 /* Send gratuitous ARP to notify of link change */
1128 if (IN_DEV_ARP_NOTIFY(in_dev)) {
1129 struct in_ifaddr *ifa = in_dev->ifa_list;
1130
1131 if (ifa)
1132 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1133 ifa->ifa_address, dev,
1134 ifa->ifa_address, NULL,
1135 dev->dev_addr, NULL);
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 break;
1138 case NETDEV_DOWN:
1139 ip_mc_down(in_dev);
1140 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001141 case NETDEV_PRE_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001142 ip_mc_unmap(in_dev);
1143 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001144 case NETDEV_POST_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001145 ip_mc_remap(in_dev);
1146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 case NETDEV_CHANGEMTU:
Breno Leitao06770842008-09-02 17:28:58 -07001148 if (inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
Breno Leitao06770842008-09-02 17:28:58 -07001150 /* disable IP when MTU is not enough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 case NETDEV_UNREGISTER:
1152 inetdev_destroy(in_dev);
1153 break;
1154 case NETDEV_CHANGENAME:
1155 /* Do not notify about label change, this event is
1156 * not interesting to applications using netlink.
1157 */
1158 inetdev_changename(dev, in_dev);
1159
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001160 devinet_sysctl_unregister(in_dev);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001161 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
1163 }
1164out:
1165 return NOTIFY_DONE;
1166}
1167
1168static struct notifier_block ip_netdev_notifier = {
Jianjun Kong539afed2008-11-03 02:48:48 -08001169 .notifier_call = inetdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
Thomas Graf339bf982006-11-10 14:10:15 -08001172static inline size_t inet_nlmsg_size(void)
1173{
1174 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1175 + nla_total_size(4) /* IFA_ADDRESS */
1176 + nla_total_size(4) /* IFA_LOCAL */
1177 + nla_total_size(4) /* IFA_BROADCAST */
Thomas Graf339bf982006-11-10 14:10:15 -08001178 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001182 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct ifaddrmsg *ifm;
1185 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Thomas Graf47f68512006-08-04 23:04:36 -07001187 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1188 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001189 return -EMSGSIZE;
Thomas Graf47f68512006-08-04 23:04:36 -07001190
1191 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ifm->ifa_family = AF_INET;
1193 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1194 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1195 ifm->ifa_scope = ifa->ifa_scope;
1196 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Thomas Graf47f68512006-08-04 23:04:36 -07001198 if (ifa->ifa_address)
Al Viroa7a628c2006-09-26 22:16:43 -07001199 NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
Thomas Graf47f68512006-08-04 23:04:36 -07001200
1201 if (ifa->ifa_local)
Al Viroa7a628c2006-09-26 22:16:43 -07001202 NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
Thomas Graf47f68512006-08-04 23:04:36 -07001203
1204 if (ifa->ifa_broadcast)
Al Viroa7a628c2006-09-26 22:16:43 -07001205 NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
Thomas Graf47f68512006-08-04 23:04:36 -07001206
Thomas Graf47f68512006-08-04 23:04:36 -07001207 if (ifa->ifa_label[0])
1208 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1209
1210 return nlmsg_end(skb, nlh);
1211
1212nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001213 nlmsg_cancel(skb, nlh);
1214 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1218{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001219 struct net *net = sock_net(skb->sk);
Eric Dumazeteec4df92009-11-12 07:44:25 +00001220 int h, s_h;
1221 int idx, s_idx;
1222 int ip_idx, s_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct net_device *dev;
1224 struct in_device *in_dev;
1225 struct in_ifaddr *ifa;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001226 struct hlist_head *head;
1227 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Eric Dumazeteec4df92009-11-12 07:44:25 +00001229 s_h = cb->args[0];
1230 s_idx = idx = cb->args[1];
1231 s_ip_idx = ip_idx = cb->args[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Eric Dumazeteec4df92009-11-12 07:44:25 +00001233 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1234 idx = 0;
1235 head = &net->dev_index_head[h];
1236 rcu_read_lock();
1237 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1238 if (idx < s_idx)
1239 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07001240 if (h > s_h || idx > s_idx)
Eric Dumazeteec4df92009-11-12 07:44:25 +00001241 s_ip_idx = 0;
1242 in_dev = __in_dev_get_rcu(dev);
1243 if (!in_dev)
1244 goto cont;
1245
1246 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1247 ifa = ifa->ifa_next, ip_idx++) {
1248 if (ip_idx < s_ip_idx)
1249 continue;
1250 if (inet_fill_ifaddr(skb, ifa,
1251 NETLINK_CB(cb->skb).pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 cb->nlh->nlmsg_seq,
Eric Dumazeteec4df92009-11-12 07:44:25 +00001253 RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1254 rcu_read_unlock();
1255 goto done;
1256 }
1257 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001258cont:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001259 idx++;
1260 }
1261 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264done:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001265 cb->args[0] = h;
1266 cb->args[1] = idx;
1267 cb->args[2] = ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 return skb->len;
1270}
1271
Jianjun Kong539afed2008-11-03 02:48:48 -08001272static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
Thomas Grafd6062cb2006-08-15 00:33:59 -07001273 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Thomas Graf47f68512006-08-04 23:04:36 -07001275 struct sk_buff *skb;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001276 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1277 int err = -ENOBUFS;
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001278 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001280 net = dev_net(ifa->ifa_dev->dev);
Thomas Graf339bf982006-11-10 14:10:15 -08001281 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
Thomas Graf47f68512006-08-04 23:04:36 -07001282 if (skb == NULL)
Thomas Grafd6062cb2006-08-15 00:33:59 -07001283 goto errout;
1284
1285 err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001286 if (err < 0) {
1287 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1288 WARN_ON(err == -EMSGSIZE);
1289 kfree_skb(skb);
1290 goto errout;
1291 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001292 rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1293 return;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001294errout:
1295 if (err < 0)
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001296 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Thomas Graf9f0f7272010-11-16 04:32:48 +00001299static size_t inet_get_link_af_size(const struct net_device *dev)
1300{
Eric Dumazetf7fce742010-12-01 06:03:06 +00001301 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001302
1303 if (!in_dev)
1304 return 0;
1305
1306 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1307}
1308
1309static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
1310{
Eric Dumazetf7fce742010-12-01 06:03:06 +00001311 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001312 struct nlattr *nla;
1313 int i;
1314
1315 if (!in_dev)
1316 return -ENODATA;
1317
1318 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1319 if (nla == NULL)
1320 return -EMSGSIZE;
1321
1322 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1323 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1324
1325 return 0;
1326}
1327
1328static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1329 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1330};
1331
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001332static int inet_validate_link_af(const struct net_device *dev,
1333 const struct nlattr *nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001334{
Thomas Graf9f0f7272010-11-16 04:32:48 +00001335 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1336 int err, rem;
1337
Eric Dumazetf7fce742010-12-01 06:03:06 +00001338 if (dev && !__in_dev_get_rtnl(dev))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001339 return -EAFNOSUPPORT;
Thomas Graf9f0f7272010-11-16 04:32:48 +00001340
1341 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
1342 if (err < 0)
1343 return err;
1344
1345 if (tb[IFLA_INET_CONF]) {
1346 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1347 int cfgid = nla_type(a);
1348
1349 if (nla_len(a) < 4)
1350 return -EINVAL;
1351
1352 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1353 return -EINVAL;
1354 }
1355 }
1356
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001357 return 0;
1358}
1359
1360static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1361{
Eric Dumazetf7fce742010-12-01 06:03:06 +00001362 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001363 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1364 int rem;
1365
1366 if (!in_dev)
1367 return -EAFNOSUPPORT;
1368
1369 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
1370 BUG();
1371
Thomas Graf9f0f7272010-11-16 04:32:48 +00001372 if (tb[IFLA_INET_CONF]) {
1373 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1374 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1375 }
1376
1377 return 0;
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_SYSCTL
1381
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001382static void devinet_copy_dflt_conf(struct net *net, int i)
Herbert Xu31be3082007-06-04 23:35:37 -07001383{
1384 struct net_device *dev;
1385
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001386 rcu_read_lock();
1387 for_each_netdev_rcu(net, dev) {
Herbert Xu31be3082007-06-04 23:35:37 -07001388 struct in_device *in_dev;
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001389
Herbert Xu31be3082007-06-04 23:35:37 -07001390 in_dev = __in_dev_get_rcu(dev);
1391 if (in_dev && !test_bit(i, in_dev->cnf.state))
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08001392 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
Herbert Xu31be3082007-06-04 23:35:37 -07001393 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001394 rcu_read_unlock();
Herbert Xu31be3082007-06-04 23:35:37 -07001395}
1396
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001397/* called with RTNL locked */
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001398static void inet_forward_change(struct net *net)
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001399{
1400 struct net_device *dev;
Pavel Emelyanov586f1212007-12-16 13:32:48 -08001401 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001402
Pavel Emelyanov586f1212007-12-16 13:32:48 -08001403 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08001404 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001405
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001406 for_each_netdev(net, dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001407 struct in_device *in_dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001408 if (on)
1409 dev_disable_lro(dev);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001410 rcu_read_lock();
1411 in_dev = __in_dev_get_rcu(dev);
1412 if (in_dev)
1413 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1414 rcu_read_unlock();
1415 }
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001416}
1417
Herbert Xu31be3082007-06-04 23:35:37 -07001418static int devinet_conf_proc(ctl_table *ctl, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001419 void __user *buffer,
Herbert Xu31be3082007-06-04 23:35:37 -07001420 size_t *lenp, loff_t *ppos)
1421{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001422 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Herbert Xu31be3082007-06-04 23:35:37 -07001423
1424 if (write) {
1425 struct ipv4_devconf *cnf = ctl->extra1;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001426 struct net *net = ctl->extra2;
Herbert Xu31be3082007-06-04 23:35:37 -07001427 int i = (int *)ctl->data - cnf->data;
1428
1429 set_bit(i, cnf->state);
1430
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08001431 if (cnf == net->ipv4.devconf_dflt)
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001432 devinet_copy_dflt_conf(net, i);
Herbert Xu31be3082007-06-04 23:35:37 -07001433 }
1434
1435 return ret;
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static int devinet_sysctl_forward(ctl_table *ctl, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001439 void __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 size_t *lenp, loff_t *ppos)
1441{
1442 int *valp = ctl->data;
1443 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00001444 loff_t pos = *ppos;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001445 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (write && *valp != val) {
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001448 struct net *net = ctl->extra2;
1449
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001450 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
Eric W. Biederman88af1822010-02-19 13:22:59 +00001451 if (!rtnl_trylock()) {
1452 /* Restore the original values before restarting */
1453 *valp = val;
1454 *ppos = pos;
Eric W. Biederman9b8adb52009-05-13 16:59:21 +00001455 return restart_syscall();
Eric W. Biederman88af1822010-02-19 13:22:59 +00001456 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001457 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1458 inet_forward_change(net);
1459 } else if (*valp) {
1460 struct ipv4_devconf *cnf = ctl->extra1;
1461 struct in_device *idev =
1462 container_of(cnf, struct in_device, cnf);
1463 dev_disable_lro(idev->dev);
1464 }
1465 rtnl_unlock();
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001466 rt_cache_flush(net, 0);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 return ret;
1471}
1472
David S. Miller323e1262010-12-12 21:55:08 -08001473static int ipv4_doint_and_flush(ctl_table *ctl, int write,
1474 void __user *buffer,
1475 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 int *valp = ctl->data;
1478 int val = *valp;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001479 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001480 struct net *net = ctl->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (write && *valp != val)
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001483 rt_cache_flush(net, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 return ret;
1486}
1487
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001488#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
Herbert Xu42f811b2007-06-04 23:34:44 -07001489 { \
Herbert Xu42f811b2007-06-04 23:34:44 -07001490 .procname = name, \
1491 .data = ipv4_devconf.data + \
Eric W. Biederman02291682010-02-14 03:25:51 +00001492 IPV4_DEVCONF_ ## attr - 1, \
Herbert Xu42f811b2007-06-04 23:34:44 -07001493 .maxlen = sizeof(int), \
1494 .mode = mval, \
1495 .proc_handler = proc, \
Herbert Xu31be3082007-06-04 23:35:37 -07001496 .extra1 = &ipv4_devconf, \
Herbert Xu42f811b2007-06-04 23:34:44 -07001497 }
1498
1499#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001500 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07001501
1502#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001503 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07001504
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001505#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
1506 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07001507
1508#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001509 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
Herbert Xu42f811b2007-06-04 23:34:44 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511static struct devinet_sysctl_table {
1512 struct ctl_table_header *sysctl_header;
Eric W. Biederman02291682010-02-14 03:25:51 +00001513 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001514 char *dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515} devinet_sysctl = {
1516 .devinet_vars = {
Herbert Xu42f811b2007-06-04 23:34:44 -07001517 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001518 devinet_sysctl_forward),
Herbert Xu42f811b2007-06-04 23:34:44 -07001519 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1520
1521 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1522 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1523 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1524 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1525 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1526 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1527 "accept_source_route"),
Patrick McHardy8153a102009-12-03 01:25:58 +00001528 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
Jamal Hadi Salim28f6aee2009-12-25 17:30:22 -08001529 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
Herbert Xu42f811b2007-06-04 23:34:44 -07001530 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1531 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1532 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1533 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1534 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1535 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1536 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1537 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1538 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001539 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00001540 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
Herbert Xu42f811b2007-06-04 23:34:44 -07001541
1542 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1543 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1544 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1545 "force_igmp_version"),
1546 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1547 "promote_secondaries"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549};
1550
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001551static int __devinet_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001552 struct ipv4_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int i;
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001555 struct devinet_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001557#define DEVINET_CTL_PATH_DEV 3
1558
1559 struct ctl_path devinet_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001560 { .procname = "net", },
1561 { .procname = "ipv4", },
1562 { .procname = "conf", },
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001563 { /* to be set */ },
1564 { },
1565 };
1566
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001567 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!t)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001569 goto out;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1572 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Herbert Xu31be3082007-06-04 23:35:37 -07001573 t->devinet_vars[i].extra1 = p;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001574 t->devinet_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001577 /*
1578 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * by sysctl and we wouldn't want anyone to change it under our feet
1580 * (see SIOCSIFNAME).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001581 */
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001582 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1583 if (!t->dev_name)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001584 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001586 devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001588 t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001589 t->devinet_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!t->sysctl_header)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001591 goto free_procname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 p->sysctl = t;
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001596free_procname:
Pavel Emelyanovbfada692007-12-02 00:57:08 +11001597 kfree(t->dev_name);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001598free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 kfree(t);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11001600out:
Pavel Emelyanovea40b322007-12-16 13:30:07 -08001601 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001604static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
1605{
1606 struct devinet_sysctl_table *t = cnf->sysctl;
1607
1608 if (t == NULL)
1609 return;
1610
1611 cnf->sysctl = NULL;
1612 unregister_sysctl_table(t->sysctl_header);
1613 kfree(t->dev_name);
1614 kfree(t);
1615}
1616
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001617static void devinet_sysctl_register(struct in_device *idev)
1618{
Eric W. Biederman54716e32010-02-14 03:27:03 +00001619 neigh_sysctl_register(idev->dev, idev->arp_parms, "ipv4", NULL);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001620 __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001621 &idev->cnf);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001622}
1623
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001624static void devinet_sysctl_unregister(struct in_device *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001626 __devinet_sysctl_unregister(&idev->cnf);
1627 neigh_sysctl_unregister(idev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001630static struct ctl_table ctl_forward_entry[] = {
1631 {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001632 .procname = "ip_forward",
1633 .data = &ipv4_devconf.data[
Eric W. Biederman02291682010-02-14 03:25:51 +00001634 IPV4_DEVCONF_FORWARDING - 1],
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001635 .maxlen = sizeof(int),
1636 .mode = 0644,
1637 .proc_handler = devinet_sysctl_forward,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001638 .extra1 = &ipv4_devconf,
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08001639 .extra2 = &init_net,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001640 },
1641 { },
1642};
1643
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001644static __net_initdata struct ctl_path net_ipv4_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001645 { .procname = "net", },
1646 { .procname = "ipv4", },
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001647 { },
1648};
Eric Dumazet2a75de02008-01-05 23:08:49 -08001649#endif
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08001650
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001651static __net_init int devinet_init_net(struct net *net)
1652{
1653 int err;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001654 struct ipv4_devconf *all, *dflt;
Eric Dumazet2a75de02008-01-05 23:08:49 -08001655#ifdef CONFIG_SYSCTL
1656 struct ctl_table *tbl = ctl_forward_entry;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001657 struct ctl_table_header *forw_hdr;
Eric Dumazet2a75de02008-01-05 23:08:49 -08001658#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001659
1660 err = -ENOMEM;
1661 all = &ipv4_devconf;
1662 dflt = &ipv4_devconf_dflt;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001663
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001664 if (!net_eq(net, &init_net)) {
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001665 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1666 if (all == NULL)
1667 goto err_alloc_all;
1668
1669 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1670 if (dflt == NULL)
1671 goto err_alloc_dflt;
1672
Eric Dumazet2a75de02008-01-05 23:08:49 -08001673#ifdef CONFIG_SYSCTL
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001674 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1675 if (tbl == NULL)
1676 goto err_alloc_ctl;
1677
Eric W. Biederman02291682010-02-14 03:25:51 +00001678 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001679 tbl[0].extra1 = all;
1680 tbl[0].extra2 = net;
Eric Dumazet2a75de02008-01-05 23:08:49 -08001681#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001682 }
1683
1684#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001685 err = __devinet_sysctl_register(net, "all", all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001686 if (err < 0)
1687 goto err_reg_all;
1688
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001689 err = __devinet_sysctl_register(net, "default", dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001690 if (err < 0)
1691 goto err_reg_dflt;
1692
1693 err = -ENOMEM;
1694 forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
1695 if (forw_hdr == NULL)
1696 goto err_reg_ctl;
Eric Dumazet2a75de02008-01-05 23:08:49 -08001697 net->ipv4.forw_hdr = forw_hdr;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001698#endif
1699
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001700 net->ipv4.devconf_all = all;
1701 net->ipv4.devconf_dflt = dflt;
1702 return 0;
1703
1704#ifdef CONFIG_SYSCTL
1705err_reg_ctl:
1706 __devinet_sysctl_unregister(dflt);
1707err_reg_dflt:
1708 __devinet_sysctl_unregister(all);
1709err_reg_all:
1710 if (tbl != ctl_forward_entry)
1711 kfree(tbl);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001712err_alloc_ctl:
Eric Dumazet2a75de02008-01-05 23:08:49 -08001713#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001714 if (dflt != &ipv4_devconf_dflt)
1715 kfree(dflt);
1716err_alloc_dflt:
1717 if (all != &ipv4_devconf)
1718 kfree(all);
1719err_alloc_all:
1720 return err;
1721}
1722
1723static __net_exit void devinet_exit_net(struct net *net)
1724{
Eric Dumazet2a75de02008-01-05 23:08:49 -08001725#ifdef CONFIG_SYSCTL
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001726 struct ctl_table *tbl;
1727
1728 tbl = net->ipv4.forw_hdr->ctl_table_arg;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001729 unregister_net_sysctl_table(net->ipv4.forw_hdr);
1730 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1731 __devinet_sysctl_unregister(net->ipv4.devconf_all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001732 kfree(tbl);
Eric Dumazet2a75de02008-01-05 23:08:49 -08001733#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001734 kfree(net->ipv4.devconf_dflt);
1735 kfree(net->ipv4.devconf_all);
1736}
1737
1738static __net_initdata struct pernet_operations devinet_ops = {
1739 .init = devinet_init_net,
1740 .exit = devinet_exit_net,
1741};
1742
Thomas Graf9f0f7272010-11-16 04:32:48 +00001743static struct rtnl_af_ops inet_af_ops = {
1744 .family = AF_INET,
1745 .fill_link_af = inet_fill_link_af,
1746 .get_link_af_size = inet_get_link_af_size,
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001747 .validate_link_af = inet_validate_link_af,
1748 .set_link_af = inet_set_link_af,
Thomas Graf9f0f7272010-11-16 04:32:48 +00001749};
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751void __init devinet_init(void)
1752{
David S. Millerfd23c3b2011-02-18 12:42:28 -08001753 int i;
1754
1755 for (i = 0; i < IN4_ADDR_HSIZE; i++)
1756 INIT_HLIST_HEAD(&inet_addr_lst[i]);
1757
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08001758 register_pernet_subsys(&devinet_ops);
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 register_gifconf(PF_INET, inet_gifconf);
1761 register_netdevice_notifier(&ip_netdev_notifier);
Thomas Graf63f34442007-03-22 11:55:17 -07001762
Thomas Graf9f0f7272010-11-16 04:32:48 +00001763 rtnl_af_register(&inet_af_ops);
1764
Thomas Graf63f34442007-03-22 11:55:17 -07001765 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
1766 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
1767 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769