Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NET3 IP device support routines. | |
3 | * | |
1da177e4 LT |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Derived from the IP parts of dev.c 1.0.19 | |
02c30a84 | 10 | * Authors: Ross Biro |
1da177e4 LT |
11 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
12 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
13 | * | |
14 | * Additional Authors: | |
15 | * Alan Cox, <gw4pts@gw4pts.ampr.org> | |
16 | * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
17 | * | |
18 | * Changes: | |
19 | * Alexey Kuznetsov: pa_* fields are replaced with ifaddr | |
20 | * lists. | |
21 | * Cyrus Durgin: updated for kmod | |
22 | * Matthias Andree: in devinet_ioctl, compare label and | |
23 | * address (4.4BSD alias style support), | |
24 | * fall back to comparing just the label | |
25 | * if no match found. | |
26 | */ | |
27 | ||
1da177e4 LT |
28 | |
29 | #include <asm/uaccess.h> | |
1da177e4 | 30 | #include <linux/bitops.h> |
4fc268d2 | 31 | #include <linux/capability.h> |
1da177e4 LT |
32 | #include <linux/module.h> |
33 | #include <linux/types.h> | |
34 | #include <linux/kernel.h> | |
1da177e4 LT |
35 | #include <linux/string.h> |
36 | #include <linux/mm.h> | |
37 | #include <linux/socket.h> | |
38 | #include <linux/sockios.h> | |
39 | #include <linux/in.h> | |
40 | #include <linux/errno.h> | |
41 | #include <linux/interrupt.h> | |
1823730f | 42 | #include <linux/if_addr.h> |
1da177e4 LT |
43 | #include <linux/if_ether.h> |
44 | #include <linux/inet.h> | |
45 | #include <linux/netdevice.h> | |
46 | #include <linux/etherdevice.h> | |
47 | #include <linux/skbuff.h> | |
1da177e4 LT |
48 | #include <linux/init.h> |
49 | #include <linux/notifier.h> | |
50 | #include <linux/inetdevice.h> | |
51 | #include <linux/igmp.h> | |
5a0e3ad6 | 52 | #include <linux/slab.h> |
fd23c3b3 | 53 | #include <linux/hash.h> |
1da177e4 LT |
54 | #ifdef CONFIG_SYSCTL |
55 | #include <linux/sysctl.h> | |
56 | #endif | |
57 | #include <linux/kmod.h> | |
edc9e748 | 58 | #include <linux/netconf.h> |
1da177e4 | 59 | |
14c85021 | 60 | #include <net/arp.h> |
1da177e4 LT |
61 | #include <net/ip.h> |
62 | #include <net/route.h> | |
63 | #include <net/ip_fib.h> | |
63f3444f | 64 | #include <net/rtnetlink.h> |
752d14dc | 65 | #include <net/net_namespace.h> |
1da177e4 | 66 | |
406b6f97 DM |
67 | #include "fib_lookup.h" |
68 | ||
0027ba84 | 69 | static struct ipv4_devconf ipv4_devconf = { |
42f811b8 | 70 | .data = { |
02291680 EB |
71 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
72 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, | |
73 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, | |
74 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, | |
42f811b8 | 75 | }, |
1da177e4 LT |
76 | }; |
77 | ||
78 | static struct ipv4_devconf ipv4_devconf_dflt = { | |
42f811b8 | 79 | .data = { |
02291680 EB |
80 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
81 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, | |
82 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, | |
83 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, | |
84 | [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1, | |
42f811b8 | 85 | }, |
1da177e4 LT |
86 | }; |
87 | ||
9355bbd6 PE |
88 | #define IPV4_DEVCONF_DFLT(net, attr) \ |
89 | IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) | |
42f811b8 | 90 | |
ef7c79ed | 91 | static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { |
5c753978 TG |
92 | [IFA_LOCAL] = { .type = NLA_U32 }, |
93 | [IFA_ADDRESS] = { .type = NLA_U32 }, | |
94 | [IFA_BROADCAST] = { .type = NLA_U32 }, | |
5176f91e | 95 | [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, |
5c753978 TG |
96 | }; |
97 | ||
40384999 ED |
98 | #define IN4_ADDR_HSIZE_SHIFT 8 |
99 | #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT) | |
100 | ||
fd23c3b3 DM |
101 | static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE]; |
102 | static DEFINE_SPINLOCK(inet_addr_hash_lock); | |
103 | ||
40384999 | 104 | static u32 inet_addr_hash(struct net *net, __be32 addr) |
fd23c3b3 | 105 | { |
40384999 | 106 | u32 val = (__force u32) addr ^ net_hash_mix(net); |
fd23c3b3 | 107 | |
40384999 | 108 | return hash_32(val, IN4_ADDR_HSIZE_SHIFT); |
fd23c3b3 DM |
109 | } |
110 | ||
111 | static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa) | |
112 | { | |
40384999 | 113 | u32 hash = inet_addr_hash(net, ifa->ifa_local); |
fd23c3b3 DM |
114 | |
115 | spin_lock(&inet_addr_hash_lock); | |
116 | hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]); | |
117 | spin_unlock(&inet_addr_hash_lock); | |
118 | } | |
119 | ||
120 | static void inet_hash_remove(struct in_ifaddr *ifa) | |
121 | { | |
122 | spin_lock(&inet_addr_hash_lock); | |
123 | hlist_del_init_rcu(&ifa->hash); | |
124 | spin_unlock(&inet_addr_hash_lock); | |
125 | } | |
126 | ||
9435eb1c DM |
127 | /** |
128 | * __ip_dev_find - find the first device with a given source address. | |
129 | * @net: the net namespace | |
130 | * @addr: the source address | |
131 | * @devref: if true, take a reference on the found device | |
132 | * | |
133 | * If a caller uses devref=false, it should be protected by RCU, or RTNL | |
134 | */ | |
135 | struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref) | |
136 | { | |
40384999 | 137 | u32 hash = inet_addr_hash(net, addr); |
9435eb1c DM |
138 | struct net_device *result = NULL; |
139 | struct in_ifaddr *ifa; | |
140 | struct hlist_node *node; | |
141 | ||
142 | rcu_read_lock(); | |
143 | hlist_for_each_entry_rcu(ifa, node, &inet_addr_lst[hash], hash) { | |
e066008b | 144 | if (ifa->ifa_local == addr) { |
40384999 ED |
145 | struct net_device *dev = ifa->ifa_dev->dev; |
146 | ||
147 | if (!net_eq(dev_net(dev), net)) | |
148 | continue; | |
9435eb1c DM |
149 | result = dev; |
150 | break; | |
151 | } | |
152 | } | |
406b6f97 DM |
153 | if (!result) { |
154 | struct flowi4 fl4 = { .daddr = addr }; | |
155 | struct fib_result res = { 0 }; | |
156 | struct fib_table *local; | |
157 | ||
158 | /* Fallback to FIB local table so that communication | |
159 | * over loopback subnets work. | |
160 | */ | |
161 | local = fib_get_table(net, RT_TABLE_LOCAL); | |
162 | if (local && | |
163 | !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) && | |
164 | res.type == RTN_LOCAL) | |
165 | result = FIB_RES_DEV(res); | |
166 | } | |
9435eb1c DM |
167 | if (result && devref) |
168 | dev_hold(result); | |
169 | rcu_read_unlock(); | |
170 | return result; | |
171 | } | |
172 | EXPORT_SYMBOL(__ip_dev_find); | |
173 | ||
d6062cbb | 174 | static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); |
1da177e4 | 175 | |
e041c683 | 176 | static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); |
1da177e4 LT |
177 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
178 | int destroy); | |
179 | #ifdef CONFIG_SYSCTL | |
66f27a52 | 180 | static void devinet_sysctl_register(struct in_device *idev); |
51602b2a PE |
181 | static void devinet_sysctl_unregister(struct in_device *idev); |
182 | #else | |
40384999 | 183 | static void devinet_sysctl_register(struct in_device *idev) |
51602b2a PE |
184 | { |
185 | } | |
40384999 | 186 | static void devinet_sysctl_unregister(struct in_device *idev) |
51602b2a PE |
187 | { |
188 | } | |
1da177e4 LT |
189 | #endif |
190 | ||
191 | /* Locks all the inet devices. */ | |
192 | ||
193 | static struct in_ifaddr *inet_alloc_ifa(void) | |
194 | { | |
93adcc80 | 195 | return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL); |
1da177e4 LT |
196 | } |
197 | ||
198 | static void inet_rcu_free_ifa(struct rcu_head *head) | |
199 | { | |
200 | struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); | |
201 | if (ifa->ifa_dev) | |
202 | in_dev_put(ifa->ifa_dev); | |
203 | kfree(ifa); | |
204 | } | |
205 | ||
40384999 | 206 | static void inet_free_ifa(struct in_ifaddr *ifa) |
1da177e4 LT |
207 | { |
208 | call_rcu(&ifa->rcu_head, inet_rcu_free_ifa); | |
209 | } | |
210 | ||
211 | void in_dev_finish_destroy(struct in_device *idev) | |
212 | { | |
213 | struct net_device *dev = idev->dev; | |
214 | ||
547b792c IJ |
215 | WARN_ON(idev->ifa_list); |
216 | WARN_ON(idev->mc_list); | |
1da177e4 | 217 | #ifdef NET_REFCNT_DEBUG |
91df42be | 218 | pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL"); |
1da177e4 LT |
219 | #endif |
220 | dev_put(dev); | |
221 | if (!idev->dead) | |
9f9354b9 ED |
222 | pr_err("Freeing alive in_device %p\n", idev); |
223 | else | |
1da177e4 | 224 | kfree(idev); |
1da177e4 | 225 | } |
9f9354b9 | 226 | EXPORT_SYMBOL(in_dev_finish_destroy); |
1da177e4 | 227 | |
71e27da9 | 228 | static struct in_device *inetdev_init(struct net_device *dev) |
1da177e4 LT |
229 | { |
230 | struct in_device *in_dev; | |
231 | ||
232 | ASSERT_RTNL(); | |
233 | ||
0da974f4 | 234 | in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); |
1da177e4 LT |
235 | if (!in_dev) |
236 | goto out; | |
c346dca1 | 237 | memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt, |
9355bbd6 | 238 | sizeof(in_dev->cnf)); |
1da177e4 LT |
239 | in_dev->cnf.sysctl = NULL; |
240 | in_dev->dev = dev; | |
9f9354b9 ED |
241 | in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl); |
242 | if (!in_dev->arp_parms) | |
1da177e4 | 243 | goto out_kfree; |
0187bdfb BH |
244 | if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) |
245 | dev_disable_lro(dev); | |
1da177e4 LT |
246 | /* Reference in_dev->dev */ |
247 | dev_hold(dev); | |
30c4cf57 | 248 | /* Account for reference dev->ip_ptr (below) */ |
1da177e4 | 249 | in_dev_hold(in_dev); |
1da177e4 | 250 | |
66f27a52 | 251 | devinet_sysctl_register(in_dev); |
1da177e4 LT |
252 | ip_mc_init_dev(in_dev); |
253 | if (dev->flags & IFF_UP) | |
254 | ip_mc_up(in_dev); | |
483479ec | 255 | |
30c4cf57 | 256 | /* we can receive as soon as ip_ptr is set -- do this last */ |
cf778b00 | 257 | rcu_assign_pointer(dev->ip_ptr, in_dev); |
483479ec | 258 | out: |
1da177e4 LT |
259 | return in_dev; |
260 | out_kfree: | |
261 | kfree(in_dev); | |
262 | in_dev = NULL; | |
263 | goto out; | |
264 | } | |
265 | ||
266 | static void in_dev_rcu_put(struct rcu_head *head) | |
267 | { | |
268 | struct in_device *idev = container_of(head, struct in_device, rcu_head); | |
269 | in_dev_put(idev); | |
270 | } | |
271 | ||
272 | static void inetdev_destroy(struct in_device *in_dev) | |
273 | { | |
274 | struct in_ifaddr *ifa; | |
275 | struct net_device *dev; | |
276 | ||
277 | ASSERT_RTNL(); | |
278 | ||
279 | dev = in_dev->dev; | |
1da177e4 LT |
280 | |
281 | in_dev->dead = 1; | |
282 | ||
283 | ip_mc_destroy_dev(in_dev); | |
284 | ||
285 | while ((ifa = in_dev->ifa_list) != NULL) { | |
286 | inet_del_ifa(in_dev, &in_dev->ifa_list, 0); | |
287 | inet_free_ifa(ifa); | |
288 | } | |
289 | ||
a9b3cd7f | 290 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
1da177e4 | 291 | |
51602b2a | 292 | devinet_sysctl_unregister(in_dev); |
1da177e4 LT |
293 | neigh_parms_release(&arp_tbl, in_dev->arp_parms); |
294 | arp_ifdown(dev); | |
295 | ||
296 | call_rcu(&in_dev->rcu_head, in_dev_rcu_put); | |
297 | } | |
298 | ||
ff428d72 | 299 | int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) |
1da177e4 LT |
300 | { |
301 | rcu_read_lock(); | |
302 | for_primary_ifa(in_dev) { | |
303 | if (inet_ifa_match(a, ifa)) { | |
304 | if (!b || inet_ifa_match(b, ifa)) { | |
305 | rcu_read_unlock(); | |
306 | return 1; | |
307 | } | |
308 | } | |
309 | } endfor_ifa(in_dev); | |
310 | rcu_read_unlock(); | |
311 | return 0; | |
312 | } | |
313 | ||
d6062cbb | 314 | static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
15e47304 | 315 | int destroy, struct nlmsghdr *nlh, u32 portid) |
1da177e4 | 316 | { |
8f937c60 | 317 | struct in_ifaddr *promote = NULL; |
0ff60a45 JHS |
318 | struct in_ifaddr *ifa, *ifa1 = *ifap; |
319 | struct in_ifaddr *last_prim = in_dev->ifa_list; | |
320 | struct in_ifaddr *prev_prom = NULL; | |
321 | int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); | |
1da177e4 LT |
322 | |
323 | ASSERT_RTNL(); | |
324 | ||
e905a9ed | 325 | /* 1. Deleting primary ifaddr forces deletion all secondaries |
8f937c60 HW |
326 | * unless alias promotion is set |
327 | **/ | |
1da177e4 LT |
328 | |
329 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { | |
1da177e4 LT |
330 | struct in_ifaddr **ifap1 = &ifa1->ifa_next; |
331 | ||
332 | while ((ifa = *ifap1) != NULL) { | |
e905a9ed | 333 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) && |
0ff60a45 JHS |
334 | ifa1->ifa_scope <= ifa->ifa_scope) |
335 | last_prim = ifa; | |
336 | ||
1da177e4 LT |
337 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) || |
338 | ifa1->ifa_mask != ifa->ifa_mask || | |
339 | !inet_ifa_match(ifa1->ifa_address, ifa)) { | |
340 | ifap1 = &ifa->ifa_next; | |
0ff60a45 | 341 | prev_prom = ifa; |
1da177e4 LT |
342 | continue; |
343 | } | |
344 | ||
0ff60a45 | 345 | if (!do_promote) { |
fd23c3b3 | 346 | inet_hash_remove(ifa); |
8f937c60 | 347 | *ifap1 = ifa->ifa_next; |
1da177e4 | 348 | |
15e47304 | 349 | rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid); |
e041c683 AS |
350 | blocking_notifier_call_chain(&inetaddr_chain, |
351 | NETDEV_DOWN, ifa); | |
8f937c60 HW |
352 | inet_free_ifa(ifa); |
353 | } else { | |
354 | promote = ifa; | |
355 | break; | |
356 | } | |
1da177e4 LT |
357 | } |
358 | } | |
359 | ||
2d230e2b JA |
360 | /* On promotion all secondaries from subnet are changing |
361 | * the primary IP, we must remove all their routes silently | |
362 | * and later to add them back with new prefsrc. Do this | |
363 | * while all addresses are on the device list. | |
364 | */ | |
365 | for (ifa = promote; ifa; ifa = ifa->ifa_next) { | |
366 | if (ifa1->ifa_mask == ifa->ifa_mask && | |
367 | inet_ifa_match(ifa1->ifa_address, ifa)) | |
368 | fib_del_ifaddr(ifa, ifa1); | |
369 | } | |
370 | ||
1da177e4 LT |
371 | /* 2. Unlink it */ |
372 | ||
373 | *ifap = ifa1->ifa_next; | |
fd23c3b3 | 374 | inet_hash_remove(ifa1); |
1da177e4 LT |
375 | |
376 | /* 3. Announce address deletion */ | |
377 | ||
378 | /* Send message first, then call notifier. | |
379 | At first sight, FIB update triggered by notifier | |
380 | will refer to already deleted ifaddr, that could confuse | |
381 | netlink listeners. It is not true: look, gated sees | |
382 | that route deleted and if it still thinks that ifaddr | |
383 | is valid, it will try to restore deleted routes... Grr. | |
384 | So that, this order is correct. | |
385 | */ | |
15e47304 | 386 | rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid); |
e041c683 | 387 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); |
1da177e4 | 388 | |
0ff60a45 | 389 | if (promote) { |
04024b93 | 390 | struct in_ifaddr *next_sec = promote->ifa_next; |
0ff60a45 JHS |
391 | |
392 | if (prev_prom) { | |
393 | prev_prom->ifa_next = promote->ifa_next; | |
394 | promote->ifa_next = last_prim->ifa_next; | |
395 | last_prim->ifa_next = promote; | |
396 | } | |
8f937c60 | 397 | |
8f937c60 | 398 | promote->ifa_flags &= ~IFA_F_SECONDARY; |
15e47304 | 399 | rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid); |
e041c683 AS |
400 | blocking_notifier_call_chain(&inetaddr_chain, |
401 | NETDEV_UP, promote); | |
04024b93 | 402 | for (ifa = next_sec; ifa; ifa = ifa->ifa_next) { |
0ff60a45 JHS |
403 | if (ifa1->ifa_mask != ifa->ifa_mask || |
404 | !inet_ifa_match(ifa1->ifa_address, ifa)) | |
405 | continue; | |
406 | fib_add_ifaddr(ifa); | |
407 | } | |
408 | ||
409 | } | |
6363097c | 410 | if (destroy) |
0ff60a45 | 411 | inet_free_ifa(ifa1); |
1da177e4 LT |
412 | } |
413 | ||
d6062cbb TG |
414 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
415 | int destroy) | |
416 | { | |
417 | __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); | |
418 | } | |
419 | ||
420 | static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, | |
15e47304 | 421 | u32 portid) |
1da177e4 LT |
422 | { |
423 | struct in_device *in_dev = ifa->ifa_dev; | |
424 | struct in_ifaddr *ifa1, **ifap, **last_primary; | |
425 | ||
426 | ASSERT_RTNL(); | |
427 | ||
428 | if (!ifa->ifa_local) { | |
429 | inet_free_ifa(ifa); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | ifa->ifa_flags &= ~IFA_F_SECONDARY; | |
434 | last_primary = &in_dev->ifa_list; | |
435 | ||
436 | for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; | |
437 | ifap = &ifa1->ifa_next) { | |
438 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && | |
439 | ifa->ifa_scope <= ifa1->ifa_scope) | |
440 | last_primary = &ifa1->ifa_next; | |
441 | if (ifa1->ifa_mask == ifa->ifa_mask && | |
442 | inet_ifa_match(ifa1->ifa_address, ifa)) { | |
443 | if (ifa1->ifa_local == ifa->ifa_local) { | |
444 | inet_free_ifa(ifa); | |
445 | return -EEXIST; | |
446 | } | |
447 | if (ifa1->ifa_scope != ifa->ifa_scope) { | |
448 | inet_free_ifa(ifa); | |
449 | return -EINVAL; | |
450 | } | |
451 | ifa->ifa_flags |= IFA_F_SECONDARY; | |
452 | } | |
453 | } | |
454 | ||
455 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) { | |
456 | net_srandom(ifa->ifa_local); | |
457 | ifap = last_primary; | |
458 | } | |
459 | ||
460 | ifa->ifa_next = *ifap; | |
461 | *ifap = ifa; | |
462 | ||
fd23c3b3 DM |
463 | inet_hash_insert(dev_net(in_dev->dev), ifa); |
464 | ||
1da177e4 LT |
465 | /* Send message first, then call notifier. |
466 | Notifier will trigger FIB update, so that | |
467 | listeners of netlink will know about new ifaddr */ | |
15e47304 | 468 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid); |
e041c683 | 469 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); |
1da177e4 LT |
470 | |
471 | return 0; | |
472 | } | |
473 | ||
d6062cbb TG |
474 | static int inet_insert_ifa(struct in_ifaddr *ifa) |
475 | { | |
476 | return __inet_insert_ifa(ifa, NULL, 0); | |
477 | } | |
478 | ||
1da177e4 LT |
479 | static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) |
480 | { | |
e5ed6399 | 481 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
1da177e4 LT |
482 | |
483 | ASSERT_RTNL(); | |
484 | ||
485 | if (!in_dev) { | |
71e27da9 HX |
486 | inet_free_ifa(ifa); |
487 | return -ENOBUFS; | |
1da177e4 | 488 | } |
71e27da9 | 489 | ipv4_devconf_setall(in_dev); |
1da177e4 | 490 | if (ifa->ifa_dev != in_dev) { |
547b792c | 491 | WARN_ON(ifa->ifa_dev); |
1da177e4 LT |
492 | in_dev_hold(in_dev); |
493 | ifa->ifa_dev = in_dev; | |
494 | } | |
f97c1e0c | 495 | if (ipv4_is_loopback(ifa->ifa_local)) |
1da177e4 LT |
496 | ifa->ifa_scope = RT_SCOPE_HOST; |
497 | return inet_insert_ifa(ifa); | |
498 | } | |
499 | ||
8723e1b4 ED |
500 | /* Caller must hold RCU or RTNL : |
501 | * We dont take a reference on found in_device | |
502 | */ | |
7fee0ca2 | 503 | struct in_device *inetdev_by_index(struct net *net, int ifindex) |
1da177e4 LT |
504 | { |
505 | struct net_device *dev; | |
506 | struct in_device *in_dev = NULL; | |
c148fc2e ED |
507 | |
508 | rcu_read_lock(); | |
509 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 | 510 | if (dev) |
8723e1b4 | 511 | in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
c148fc2e | 512 | rcu_read_unlock(); |
1da177e4 LT |
513 | return in_dev; |
514 | } | |
9f9354b9 | 515 | EXPORT_SYMBOL(inetdev_by_index); |
1da177e4 LT |
516 | |
517 | /* Called only from RTNL semaphored context. No locks. */ | |
518 | ||
60cad5da AV |
519 | struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, |
520 | __be32 mask) | |
1da177e4 LT |
521 | { |
522 | ASSERT_RTNL(); | |
523 | ||
524 | for_primary_ifa(in_dev) { | |
525 | if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) | |
526 | return ifa; | |
527 | } endfor_ifa(in_dev); | |
528 | return NULL; | |
529 | } | |
530 | ||
531 | static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
532 | { | |
3b1e0a65 | 533 | struct net *net = sock_net(skb->sk); |
dfdd5fd4 | 534 | struct nlattr *tb[IFA_MAX+1]; |
1da177e4 | 535 | struct in_device *in_dev; |
dfdd5fd4 | 536 | struct ifaddrmsg *ifm; |
1da177e4 | 537 | struct in_ifaddr *ifa, **ifap; |
dfdd5fd4 | 538 | int err = -EINVAL; |
1da177e4 LT |
539 | |
540 | ASSERT_RTNL(); | |
541 | ||
dfdd5fd4 TG |
542 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); |
543 | if (err < 0) | |
544 | goto errout; | |
545 | ||
546 | ifm = nlmsg_data(nlh); | |
7fee0ca2 | 547 | in_dev = inetdev_by_index(net, ifm->ifa_index); |
dfdd5fd4 TG |
548 | if (in_dev == NULL) { |
549 | err = -ENODEV; | |
550 | goto errout; | |
551 | } | |
552 | ||
1da177e4 LT |
553 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
554 | ifap = &ifa->ifa_next) { | |
dfdd5fd4 | 555 | if (tb[IFA_LOCAL] && |
a7a628c4 | 556 | ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) |
dfdd5fd4 TG |
557 | continue; |
558 | ||
559 | if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) | |
1da177e4 | 560 | continue; |
dfdd5fd4 TG |
561 | |
562 | if (tb[IFA_ADDRESS] && | |
563 | (ifm->ifa_prefixlen != ifa->ifa_prefixlen || | |
a7a628c4 | 564 | !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) |
dfdd5fd4 TG |
565 | continue; |
566 | ||
15e47304 | 567 | __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid); |
1da177e4 LT |
568 | return 0; |
569 | } | |
dfdd5fd4 TG |
570 | |
571 | err = -EADDRNOTAVAIL; | |
572 | errout: | |
573 | return err; | |
1da177e4 LT |
574 | } |
575 | ||
4b8aa9ab | 576 | static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh) |
1da177e4 | 577 | { |
5c753978 TG |
578 | struct nlattr *tb[IFA_MAX+1]; |
579 | struct in_ifaddr *ifa; | |
580 | struct ifaddrmsg *ifm; | |
1da177e4 LT |
581 | struct net_device *dev; |
582 | struct in_device *in_dev; | |
7b218574 | 583 | int err; |
1da177e4 | 584 | |
5c753978 TG |
585 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy); |
586 | if (err < 0) | |
587 | goto errout; | |
1da177e4 | 588 | |
5c753978 | 589 | ifm = nlmsg_data(nlh); |
7b218574 DL |
590 | err = -EINVAL; |
591 | if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) | |
5c753978 | 592 | goto errout; |
1da177e4 | 593 | |
4b8aa9ab | 594 | dev = __dev_get_by_index(net, ifm->ifa_index); |
7b218574 DL |
595 | err = -ENODEV; |
596 | if (dev == NULL) | |
5c753978 | 597 | goto errout; |
1da177e4 | 598 | |
5c753978 | 599 | in_dev = __in_dev_get_rtnl(dev); |
7b218574 DL |
600 | err = -ENOBUFS; |
601 | if (in_dev == NULL) | |
71e27da9 | 602 | goto errout; |
1da177e4 | 603 | |
5c753978 | 604 | ifa = inet_alloc_ifa(); |
7b218574 | 605 | if (ifa == NULL) |
5c753978 TG |
606 | /* |
607 | * A potential indev allocation can be left alive, it stays | |
608 | * assigned to its device and is destroy with it. | |
609 | */ | |
5c753978 | 610 | goto errout; |
5c753978 | 611 | |
a4e65d36 | 612 | ipv4_devconf_setall(in_dev); |
5c753978 TG |
613 | in_dev_hold(in_dev); |
614 | ||
615 | if (tb[IFA_ADDRESS] == NULL) | |
616 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1da177e4 | 617 | |
fd23c3b3 | 618 | INIT_HLIST_NODE(&ifa->hash); |
1da177e4 LT |
619 | ifa->ifa_prefixlen = ifm->ifa_prefixlen; |
620 | ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); | |
1da177e4 LT |
621 | ifa->ifa_flags = ifm->ifa_flags; |
622 | ifa->ifa_scope = ifm->ifa_scope; | |
5c753978 TG |
623 | ifa->ifa_dev = in_dev; |
624 | ||
a7a628c4 AV |
625 | ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); |
626 | ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); | |
5c753978 TG |
627 | |
628 | if (tb[IFA_BROADCAST]) | |
a7a628c4 | 629 | ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); |
5c753978 | 630 | |
5c753978 TG |
631 | if (tb[IFA_LABEL]) |
632 | nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); | |
1da177e4 LT |
633 | else |
634 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
635 | ||
5c753978 TG |
636 | return ifa; |
637 | ||
638 | errout: | |
639 | return ERR_PTR(err); | |
640 | } | |
641 | ||
642 | static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
643 | { | |
3b1e0a65 | 644 | struct net *net = sock_net(skb->sk); |
5c753978 TG |
645 | struct in_ifaddr *ifa; |
646 | ||
647 | ASSERT_RTNL(); | |
648 | ||
4b8aa9ab | 649 | ifa = rtm_to_ifaddr(net, nlh); |
5c753978 TG |
650 | if (IS_ERR(ifa)) |
651 | return PTR_ERR(ifa); | |
652 | ||
15e47304 | 653 | return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid); |
1da177e4 LT |
654 | } |
655 | ||
656 | /* | |
657 | * Determine a default network mask, based on the IP address. | |
658 | */ | |
659 | ||
40384999 | 660 | static int inet_abc_len(__be32 addr) |
1da177e4 LT |
661 | { |
662 | int rc = -1; /* Something else, probably a multicast. */ | |
663 | ||
f97c1e0c | 664 | if (ipv4_is_zeronet(addr)) |
e905a9ed | 665 | rc = 0; |
1da177e4 | 666 | else { |
714e85be | 667 | __u32 haddr = ntohl(addr); |
1da177e4 | 668 | |
714e85be | 669 | if (IN_CLASSA(haddr)) |
1da177e4 | 670 | rc = 8; |
714e85be | 671 | else if (IN_CLASSB(haddr)) |
1da177e4 | 672 | rc = 16; |
714e85be | 673 | else if (IN_CLASSC(haddr)) |
1da177e4 LT |
674 | rc = 24; |
675 | } | |
676 | ||
e905a9ed | 677 | return rc; |
1da177e4 LT |
678 | } |
679 | ||
680 | ||
e5b13cb1 | 681 | int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
1da177e4 LT |
682 | { |
683 | struct ifreq ifr; | |
684 | struct sockaddr_in sin_orig; | |
685 | struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; | |
686 | struct in_device *in_dev; | |
687 | struct in_ifaddr **ifap = NULL; | |
688 | struct in_ifaddr *ifa = NULL; | |
689 | struct net_device *dev; | |
690 | char *colon; | |
691 | int ret = -EFAULT; | |
692 | int tryaddrmatch = 0; | |
693 | ||
694 | /* | |
695 | * Fetch the caller's info block into kernel space | |
696 | */ | |
697 | ||
698 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) | |
699 | goto out; | |
700 | ifr.ifr_name[IFNAMSIZ - 1] = 0; | |
701 | ||
702 | /* save original address for comparison */ | |
703 | memcpy(&sin_orig, sin, sizeof(*sin)); | |
704 | ||
705 | colon = strchr(ifr.ifr_name, ':'); | |
706 | if (colon) | |
707 | *colon = 0; | |
708 | ||
e5b13cb1 | 709 | dev_load(net, ifr.ifr_name); |
1da177e4 | 710 | |
132adf54 | 711 | switch (cmd) { |
1da177e4 LT |
712 | case SIOCGIFADDR: /* Get interface address */ |
713 | case SIOCGIFBRDADDR: /* Get the broadcast address */ | |
714 | case SIOCGIFDSTADDR: /* Get the destination address */ | |
715 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ | |
716 | /* Note that these ioctls will not sleep, | |
717 | so that we do not impose a lock. | |
718 | One day we will be forced to put shlock here (I mean SMP) | |
719 | */ | |
720 | tryaddrmatch = (sin_orig.sin_family == AF_INET); | |
721 | memset(sin, 0, sizeof(*sin)); | |
722 | sin->sin_family = AF_INET; | |
723 | break; | |
724 | ||
725 | case SIOCSIFFLAGS: | |
bf5b30b8 | 726 | ret = -EPERM; |
52e804c6 | 727 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
728 | goto out; |
729 | break; | |
730 | case SIOCSIFADDR: /* Set interface address (and family) */ | |
731 | case SIOCSIFBRDADDR: /* Set the broadcast address */ | |
732 | case SIOCSIFDSTADDR: /* Set the destination address */ | |
733 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ | |
bf5b30b8 | 734 | ret = -EPERM; |
52e804c6 | 735 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
736 | goto out; |
737 | ret = -EINVAL; | |
738 | if (sin->sin_family != AF_INET) | |
739 | goto out; | |
740 | break; | |
741 | default: | |
742 | ret = -EINVAL; | |
743 | goto out; | |
744 | } | |
745 | ||
746 | rtnl_lock(); | |
747 | ||
748 | ret = -ENODEV; | |
9f9354b9 ED |
749 | dev = __dev_get_by_name(net, ifr.ifr_name); |
750 | if (!dev) | |
1da177e4 LT |
751 | goto done; |
752 | ||
753 | if (colon) | |
754 | *colon = ':'; | |
755 | ||
9f9354b9 ED |
756 | in_dev = __in_dev_get_rtnl(dev); |
757 | if (in_dev) { | |
1da177e4 LT |
758 | if (tryaddrmatch) { |
759 | /* Matthias Andree */ | |
760 | /* compare label and address (4.4BSD style) */ | |
761 | /* note: we only do this for a limited set of ioctls | |
762 | and only if the original address family was AF_INET. | |
763 | This is checked above. */ | |
764 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; | |
765 | ifap = &ifa->ifa_next) { | |
766 | if (!strcmp(ifr.ifr_name, ifa->ifa_label) && | |
767 | sin_orig.sin_addr.s_addr == | |
6c91afe1 | 768 | ifa->ifa_local) { |
1da177e4 LT |
769 | break; /* found */ |
770 | } | |
771 | } | |
772 | } | |
773 | /* we didn't get a match, maybe the application is | |
774 | 4.3BSD-style and passed in junk so we fall back to | |
775 | comparing just the label */ | |
776 | if (!ifa) { | |
777 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; | |
778 | ifap = &ifa->ifa_next) | |
779 | if (!strcmp(ifr.ifr_name, ifa->ifa_label)) | |
780 | break; | |
781 | } | |
782 | } | |
783 | ||
784 | ret = -EADDRNOTAVAIL; | |
785 | if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) | |
786 | goto done; | |
787 | ||
132adf54 | 788 | switch (cmd) { |
1da177e4 LT |
789 | case SIOCGIFADDR: /* Get interface address */ |
790 | sin->sin_addr.s_addr = ifa->ifa_local; | |
791 | goto rarok; | |
792 | ||
793 | case SIOCGIFBRDADDR: /* Get the broadcast address */ | |
794 | sin->sin_addr.s_addr = ifa->ifa_broadcast; | |
795 | goto rarok; | |
796 | ||
797 | case SIOCGIFDSTADDR: /* Get the destination address */ | |
798 | sin->sin_addr.s_addr = ifa->ifa_address; | |
799 | goto rarok; | |
800 | ||
801 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ | |
802 | sin->sin_addr.s_addr = ifa->ifa_mask; | |
803 | goto rarok; | |
804 | ||
805 | case SIOCSIFFLAGS: | |
806 | if (colon) { | |
807 | ret = -EADDRNOTAVAIL; | |
808 | if (!ifa) | |
809 | break; | |
810 | ret = 0; | |
811 | if (!(ifr.ifr_flags & IFF_UP)) | |
812 | inet_del_ifa(in_dev, ifap, 1); | |
813 | break; | |
814 | } | |
815 | ret = dev_change_flags(dev, ifr.ifr_flags); | |
816 | break; | |
817 | ||
818 | case SIOCSIFADDR: /* Set interface address (and family) */ | |
819 | ret = -EINVAL; | |
820 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) | |
821 | break; | |
822 | ||
823 | if (!ifa) { | |
824 | ret = -ENOBUFS; | |
9f9354b9 | 825 | ifa = inet_alloc_ifa(); |
fd23c3b3 | 826 | INIT_HLIST_NODE(&ifa->hash); |
9f9354b9 | 827 | if (!ifa) |
1da177e4 LT |
828 | break; |
829 | if (colon) | |
830 | memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ); | |
831 | else | |
832 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
833 | } else { | |
834 | ret = 0; | |
835 | if (ifa->ifa_local == sin->sin_addr.s_addr) | |
836 | break; | |
837 | inet_del_ifa(in_dev, ifap, 0); | |
838 | ifa->ifa_broadcast = 0; | |
148f9729 | 839 | ifa->ifa_scope = 0; |
1da177e4 LT |
840 | } |
841 | ||
842 | ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; | |
843 | ||
844 | if (!(dev->flags & IFF_POINTOPOINT)) { | |
845 | ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); | |
846 | ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); | |
847 | if ((dev->flags & IFF_BROADCAST) && | |
848 | ifa->ifa_prefixlen < 31) | |
849 | ifa->ifa_broadcast = ifa->ifa_address | | |
850 | ~ifa->ifa_mask; | |
851 | } else { | |
852 | ifa->ifa_prefixlen = 32; | |
853 | ifa->ifa_mask = inet_make_mask(32); | |
854 | } | |
855 | ret = inet_set_ifa(dev, ifa); | |
856 | break; | |
857 | ||
858 | case SIOCSIFBRDADDR: /* Set the broadcast address */ | |
859 | ret = 0; | |
860 | if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { | |
861 | inet_del_ifa(in_dev, ifap, 0); | |
862 | ifa->ifa_broadcast = sin->sin_addr.s_addr; | |
863 | inet_insert_ifa(ifa); | |
864 | } | |
865 | break; | |
866 | ||
867 | case SIOCSIFDSTADDR: /* Set the destination address */ | |
868 | ret = 0; | |
869 | if (ifa->ifa_address == sin->sin_addr.s_addr) | |
870 | break; | |
871 | ret = -EINVAL; | |
872 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) | |
873 | break; | |
874 | ret = 0; | |
875 | inet_del_ifa(in_dev, ifap, 0); | |
876 | ifa->ifa_address = sin->sin_addr.s_addr; | |
877 | inet_insert_ifa(ifa); | |
878 | break; | |
879 | ||
880 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ | |
881 | ||
882 | /* | |
883 | * The mask we set must be legal. | |
884 | */ | |
885 | ret = -EINVAL; | |
886 | if (bad_mask(sin->sin_addr.s_addr, 0)) | |
887 | break; | |
888 | ret = 0; | |
889 | if (ifa->ifa_mask != sin->sin_addr.s_addr) { | |
a144ea4b | 890 | __be32 old_mask = ifa->ifa_mask; |
1da177e4 LT |
891 | inet_del_ifa(in_dev, ifap, 0); |
892 | ifa->ifa_mask = sin->sin_addr.s_addr; | |
893 | ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); | |
894 | ||
895 | /* See if current broadcast address matches | |
896 | * with current netmask, then recalculate | |
897 | * the broadcast address. Otherwise it's a | |
898 | * funny address, so don't touch it since | |
899 | * the user seems to know what (s)he's doing... | |
900 | */ | |
901 | if ((dev->flags & IFF_BROADCAST) && | |
902 | (ifa->ifa_prefixlen < 31) && | |
903 | (ifa->ifa_broadcast == | |
dcab5e1e | 904 | (ifa->ifa_local|~old_mask))) { |
1da177e4 LT |
905 | ifa->ifa_broadcast = (ifa->ifa_local | |
906 | ~sin->sin_addr.s_addr); | |
907 | } | |
908 | inet_insert_ifa(ifa); | |
909 | } | |
910 | break; | |
911 | } | |
912 | done: | |
913 | rtnl_unlock(); | |
914 | out: | |
915 | return ret; | |
916 | rarok: | |
917 | rtnl_unlock(); | |
918 | ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0; | |
919 | goto out; | |
920 | } | |
921 | ||
922 | static int inet_gifconf(struct net_device *dev, char __user *buf, int len) | |
923 | { | |
e5ed6399 | 924 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
1da177e4 LT |
925 | struct in_ifaddr *ifa; |
926 | struct ifreq ifr; | |
927 | int done = 0; | |
928 | ||
9f9354b9 | 929 | if (!in_dev) |
1da177e4 LT |
930 | goto out; |
931 | ||
9f9354b9 | 932 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
1da177e4 LT |
933 | if (!buf) { |
934 | done += sizeof(ifr); | |
935 | continue; | |
936 | } | |
937 | if (len < (int) sizeof(ifr)) | |
938 | break; | |
939 | memset(&ifr, 0, sizeof(struct ifreq)); | |
940 | if (ifa->ifa_label) | |
941 | strcpy(ifr.ifr_name, ifa->ifa_label); | |
942 | else | |
943 | strcpy(ifr.ifr_name, dev->name); | |
944 | ||
945 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; | |
946 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = | |
947 | ifa->ifa_local; | |
948 | ||
949 | if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) { | |
950 | done = -EFAULT; | |
951 | break; | |
952 | } | |
953 | buf += sizeof(struct ifreq); | |
954 | len -= sizeof(struct ifreq); | |
955 | done += sizeof(struct ifreq); | |
956 | } | |
957 | out: | |
958 | return done; | |
959 | } | |
960 | ||
a61ced5d | 961 | __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) |
1da177e4 | 962 | { |
a61ced5d | 963 | __be32 addr = 0; |
1da177e4 | 964 | struct in_device *in_dev; |
c346dca1 | 965 | struct net *net = dev_net(dev); |
1da177e4 LT |
966 | |
967 | rcu_read_lock(); | |
e5ed6399 | 968 | in_dev = __in_dev_get_rcu(dev); |
1da177e4 LT |
969 | if (!in_dev) |
970 | goto no_in_dev; | |
971 | ||
972 | for_primary_ifa(in_dev) { | |
973 | if (ifa->ifa_scope > scope) | |
974 | continue; | |
975 | if (!dst || inet_ifa_match(dst, ifa)) { | |
976 | addr = ifa->ifa_local; | |
977 | break; | |
978 | } | |
979 | if (!addr) | |
980 | addr = ifa->ifa_local; | |
981 | } endfor_ifa(in_dev); | |
1da177e4 LT |
982 | |
983 | if (addr) | |
c6d14c84 | 984 | goto out_unlock; |
9f9354b9 | 985 | no_in_dev: |
1da177e4 LT |
986 | |
987 | /* Not loopback addresses on loopback should be preferred | |
988 | in this case. It is importnat that lo is the first interface | |
989 | in dev_base list. | |
990 | */ | |
c6d14c84 | 991 | for_each_netdev_rcu(net, dev) { |
9f9354b9 ED |
992 | in_dev = __in_dev_get_rcu(dev); |
993 | if (!in_dev) | |
1da177e4 LT |
994 | continue; |
995 | ||
996 | for_primary_ifa(in_dev) { | |
997 | if (ifa->ifa_scope != RT_SCOPE_LINK && | |
998 | ifa->ifa_scope <= scope) { | |
999 | addr = ifa->ifa_local; | |
c6d14c84 | 1000 | goto out_unlock; |
1da177e4 LT |
1001 | } |
1002 | } endfor_ifa(in_dev); | |
1003 | } | |
c6d14c84 | 1004 | out_unlock: |
1da177e4 | 1005 | rcu_read_unlock(); |
1da177e4 LT |
1006 | return addr; |
1007 | } | |
9f9354b9 | 1008 | EXPORT_SYMBOL(inet_select_addr); |
1da177e4 | 1009 | |
60cad5da AV |
1010 | static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, |
1011 | __be32 local, int scope) | |
1da177e4 LT |
1012 | { |
1013 | int same = 0; | |
a144ea4b | 1014 | __be32 addr = 0; |
1da177e4 LT |
1015 | |
1016 | for_ifa(in_dev) { | |
1017 | if (!addr && | |
1018 | (local == ifa->ifa_local || !local) && | |
1019 | ifa->ifa_scope <= scope) { | |
1020 | addr = ifa->ifa_local; | |
1021 | if (same) | |
1022 | break; | |
1023 | } | |
1024 | if (!same) { | |
1025 | same = (!local || inet_ifa_match(local, ifa)) && | |
1026 | (!dst || inet_ifa_match(dst, ifa)); | |
1027 | if (same && addr) { | |
1028 | if (local || !dst) | |
1029 | break; | |
1030 | /* Is the selected addr into dst subnet? */ | |
1031 | if (inet_ifa_match(addr, ifa)) | |
1032 | break; | |
1033 | /* No, then can we use new local src? */ | |
1034 | if (ifa->ifa_scope <= scope) { | |
1035 | addr = ifa->ifa_local; | |
1036 | break; | |
1037 | } | |
1038 | /* search for large dst subnet for addr */ | |
1039 | same = 0; | |
1040 | } | |
1041 | } | |
1042 | } endfor_ifa(in_dev); | |
1043 | ||
9f9354b9 | 1044 | return same ? addr : 0; |
1da177e4 LT |
1045 | } |
1046 | ||
1047 | /* | |
1048 | * Confirm that local IP address exists using wildcards: | |
9bd85e32 | 1049 | * - in_dev: only on this interface, 0=any interface |
1da177e4 LT |
1050 | * - dst: only in the same subnet as dst, 0=any dst |
1051 | * - local: address, 0=autoselect the local address | |
1052 | * - scope: maximum allowed scope value for the local address | |
1053 | */ | |
9bd85e32 DL |
1054 | __be32 inet_confirm_addr(struct in_device *in_dev, |
1055 | __be32 dst, __be32 local, int scope) | |
1da177e4 | 1056 | { |
60cad5da | 1057 | __be32 addr = 0; |
9bd85e32 | 1058 | struct net_device *dev; |
39a6d063 | 1059 | struct net *net; |
1da177e4 | 1060 | |
39a6d063 | 1061 | if (scope != RT_SCOPE_LINK) |
9bd85e32 | 1062 | return confirm_addr_indev(in_dev, dst, local, scope); |
1da177e4 | 1063 | |
c346dca1 | 1064 | net = dev_net(in_dev->dev); |
1da177e4 | 1065 | rcu_read_lock(); |
c6d14c84 | 1066 | for_each_netdev_rcu(net, dev) { |
9f9354b9 ED |
1067 | in_dev = __in_dev_get_rcu(dev); |
1068 | if (in_dev) { | |
1da177e4 LT |
1069 | addr = confirm_addr_indev(in_dev, dst, local, scope); |
1070 | if (addr) | |
1071 | break; | |
1072 | } | |
1073 | } | |
1074 | rcu_read_unlock(); | |
1da177e4 LT |
1075 | |
1076 | return addr; | |
1077 | } | |
eaddcd76 | 1078 | EXPORT_SYMBOL(inet_confirm_addr); |
1da177e4 LT |
1079 | |
1080 | /* | |
1081 | * Device notifier | |
1082 | */ | |
1083 | ||
1084 | int register_inetaddr_notifier(struct notifier_block *nb) | |
1085 | { | |
e041c683 | 1086 | return blocking_notifier_chain_register(&inetaddr_chain, nb); |
1da177e4 | 1087 | } |
9f9354b9 | 1088 | EXPORT_SYMBOL(register_inetaddr_notifier); |
1da177e4 LT |
1089 | |
1090 | int unregister_inetaddr_notifier(struct notifier_block *nb) | |
1091 | { | |
e041c683 | 1092 | return blocking_notifier_chain_unregister(&inetaddr_chain, nb); |
1da177e4 | 1093 | } |
9f9354b9 | 1094 | EXPORT_SYMBOL(unregister_inetaddr_notifier); |
1da177e4 | 1095 | |
9f9354b9 ED |
1096 | /* Rename ifa_labels for a device name change. Make some effort to preserve |
1097 | * existing alias numbering and to create unique labels if possible. | |
1da177e4 LT |
1098 | */ |
1099 | static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) | |
e905a9ed | 1100 | { |
1da177e4 LT |
1101 | struct in_ifaddr *ifa; |
1102 | int named = 0; | |
1103 | ||
e905a9ed YH |
1104 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
1105 | char old[IFNAMSIZ], *dot; | |
1da177e4 LT |
1106 | |
1107 | memcpy(old, ifa->ifa_label, IFNAMSIZ); | |
e905a9ed | 1108 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
1da177e4 | 1109 | if (named++ == 0) |
573bf470 | 1110 | goto skip; |
44344b2a | 1111 | dot = strchr(old, ':'); |
e905a9ed YH |
1112 | if (dot == NULL) { |
1113 | sprintf(old, ":%d", named); | |
1da177e4 LT |
1114 | dot = old; |
1115 | } | |
9f9354b9 | 1116 | if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) |
e905a9ed | 1117 | strcat(ifa->ifa_label, dot); |
9f9354b9 | 1118 | else |
e905a9ed | 1119 | strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); |
573bf470 TG |
1120 | skip: |
1121 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); | |
e905a9ed YH |
1122 | } |
1123 | } | |
1da177e4 | 1124 | |
40384999 | 1125 | static bool inetdev_valid_mtu(unsigned int mtu) |
06770843 BL |
1126 | { |
1127 | return mtu >= 68; | |
1128 | } | |
1129 | ||
d11327ad IC |
1130 | static void inetdev_send_gratuitous_arp(struct net_device *dev, |
1131 | struct in_device *in_dev) | |
1132 | ||
1133 | { | |
b76d0789 | 1134 | struct in_ifaddr *ifa; |
d11327ad | 1135 | |
b76d0789 ZK |
1136 | for (ifa = in_dev->ifa_list; ifa; |
1137 | ifa = ifa->ifa_next) { | |
1138 | arp_send(ARPOP_REQUEST, ETH_P_ARP, | |
1139 | ifa->ifa_local, dev, | |
1140 | ifa->ifa_local, NULL, | |
1141 | dev->dev_addr, NULL); | |
1142 | } | |
d11327ad IC |
1143 | } |
1144 | ||
1da177e4 LT |
1145 | /* Called only under RTNL semaphore */ |
1146 | ||
1147 | static int inetdev_event(struct notifier_block *this, unsigned long event, | |
1148 | void *ptr) | |
1149 | { | |
1150 | struct net_device *dev = ptr; | |
748e2d93 | 1151 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
0115e8e3 | 1152 | |
1da177e4 LT |
1153 | ASSERT_RTNL(); |
1154 | ||
1155 | if (!in_dev) { | |
8030f544 | 1156 | if (event == NETDEV_REGISTER) { |
1da177e4 | 1157 | in_dev = inetdev_init(dev); |
b217d616 HX |
1158 | if (!in_dev) |
1159 | return notifier_from_errno(-ENOMEM); | |
0cc217e1 | 1160 | if (dev->flags & IFF_LOOPBACK) { |
42f811b8 HX |
1161 | IN_DEV_CONF_SET(in_dev, NOXFRM, 1); |
1162 | IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); | |
8030f544 | 1163 | } |
06770843 BL |
1164 | } else if (event == NETDEV_CHANGEMTU) { |
1165 | /* Re-enabling IP */ | |
1166 | if (inetdev_valid_mtu(dev->mtu)) | |
1167 | in_dev = inetdev_init(dev); | |
1da177e4 LT |
1168 | } |
1169 | goto out; | |
1170 | } | |
1171 | ||
1172 | switch (event) { | |
1173 | case NETDEV_REGISTER: | |
91df42be | 1174 | pr_debug("%s: bug\n", __func__); |
a9b3cd7f | 1175 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
1da177e4 LT |
1176 | break; |
1177 | case NETDEV_UP: | |
06770843 | 1178 | if (!inetdev_valid_mtu(dev->mtu)) |
1da177e4 | 1179 | break; |
0cc217e1 | 1180 | if (dev->flags & IFF_LOOPBACK) { |
9f9354b9 ED |
1181 | struct in_ifaddr *ifa = inet_alloc_ifa(); |
1182 | ||
1183 | if (ifa) { | |
fd23c3b3 | 1184 | INIT_HLIST_NODE(&ifa->hash); |
1da177e4 LT |
1185 | ifa->ifa_local = |
1186 | ifa->ifa_address = htonl(INADDR_LOOPBACK); | |
1187 | ifa->ifa_prefixlen = 8; | |
1188 | ifa->ifa_mask = inet_make_mask(8); | |
1189 | in_dev_hold(in_dev); | |
1190 | ifa->ifa_dev = in_dev; | |
1191 | ifa->ifa_scope = RT_SCOPE_HOST; | |
1192 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); | |
1193 | inet_insert_ifa(ifa); | |
1194 | } | |
1195 | } | |
1196 | ip_mc_up(in_dev); | |
eefef1cf SH |
1197 | /* fall through */ |
1198 | case NETDEV_CHANGEADDR: | |
d11327ad IC |
1199 | if (!IN_DEV_ARP_NOTIFY(in_dev)) |
1200 | break; | |
1201 | /* fall through */ | |
1202 | case NETDEV_NOTIFY_PEERS: | |
a21090cf | 1203 | /* Send gratuitous ARP to notify of link change */ |
d11327ad | 1204 | inetdev_send_gratuitous_arp(dev, in_dev); |
1da177e4 LT |
1205 | break; |
1206 | case NETDEV_DOWN: | |
1207 | ip_mc_down(in_dev); | |
1208 | break; | |
93d9b7d7 | 1209 | case NETDEV_PRE_TYPE_CHANGE: |
75c78500 MS |
1210 | ip_mc_unmap(in_dev); |
1211 | break; | |
93d9b7d7 | 1212 | case NETDEV_POST_TYPE_CHANGE: |
75c78500 MS |
1213 | ip_mc_remap(in_dev); |
1214 | break; | |
1da177e4 | 1215 | case NETDEV_CHANGEMTU: |
06770843 | 1216 | if (inetdev_valid_mtu(dev->mtu)) |
1da177e4 | 1217 | break; |
06770843 | 1218 | /* disable IP when MTU is not enough */ |
1da177e4 LT |
1219 | case NETDEV_UNREGISTER: |
1220 | inetdev_destroy(in_dev); | |
1221 | break; | |
1222 | case NETDEV_CHANGENAME: | |
1223 | /* Do not notify about label change, this event is | |
1224 | * not interesting to applications using netlink. | |
1225 | */ | |
1226 | inetdev_changename(dev, in_dev); | |
1227 | ||
51602b2a | 1228 | devinet_sysctl_unregister(in_dev); |
66f27a52 | 1229 | devinet_sysctl_register(in_dev); |
1da177e4 LT |
1230 | break; |
1231 | } | |
1232 | out: | |
1233 | return NOTIFY_DONE; | |
1234 | } | |
1235 | ||
1236 | static struct notifier_block ip_netdev_notifier = { | |
539afedf | 1237 | .notifier_call = inetdev_event, |
1da177e4 LT |
1238 | }; |
1239 | ||
40384999 | 1240 | static size_t inet_nlmsg_size(void) |
339bf98f TG |
1241 | { |
1242 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) | |
1243 | + nla_total_size(4) /* IFA_ADDRESS */ | |
1244 | + nla_total_size(4) /* IFA_LOCAL */ | |
1245 | + nla_total_size(4) /* IFA_BROADCAST */ | |
339bf98f TG |
1246 | + nla_total_size(IFNAMSIZ); /* IFA_LABEL */ |
1247 | } | |
1248 | ||
1da177e4 | 1249 | static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, |
15e47304 | 1250 | u32 portid, u32 seq, int event, unsigned int flags) |
1da177e4 LT |
1251 | { |
1252 | struct ifaddrmsg *ifm; | |
1253 | struct nlmsghdr *nlh; | |
1da177e4 | 1254 | |
15e47304 | 1255 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags); |
47f68512 | 1256 | if (nlh == NULL) |
26932566 | 1257 | return -EMSGSIZE; |
47f68512 TG |
1258 | |
1259 | ifm = nlmsg_data(nlh); | |
1da177e4 LT |
1260 | ifm->ifa_family = AF_INET; |
1261 | ifm->ifa_prefixlen = ifa->ifa_prefixlen; | |
1262 | ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT; | |
1263 | ifm->ifa_scope = ifa->ifa_scope; | |
1264 | ifm->ifa_index = ifa->ifa_dev->dev->ifindex; | |
47f68512 | 1265 | |
f3756b79 DM |
1266 | if ((ifa->ifa_address && |
1267 | nla_put_be32(skb, IFA_ADDRESS, ifa->ifa_address)) || | |
1268 | (ifa->ifa_local && | |
1269 | nla_put_be32(skb, IFA_LOCAL, ifa->ifa_local)) || | |
1270 | (ifa->ifa_broadcast && | |
1271 | nla_put_be32(skb, IFA_BROADCAST, ifa->ifa_broadcast)) || | |
1272 | (ifa->ifa_label[0] && | |
1273 | nla_put_string(skb, IFA_LABEL, ifa->ifa_label))) | |
1274 | goto nla_put_failure; | |
1da177e4 | 1275 | |
47f68512 TG |
1276 | return nlmsg_end(skb, nlh); |
1277 | ||
1278 | nla_put_failure: | |
26932566 PM |
1279 | nlmsg_cancel(skb, nlh); |
1280 | return -EMSGSIZE; | |
1da177e4 LT |
1281 | } |
1282 | ||
1283 | static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) | |
1284 | { | |
3b1e0a65 | 1285 | struct net *net = sock_net(skb->sk); |
eec4df98 ED |
1286 | int h, s_h; |
1287 | int idx, s_idx; | |
1288 | int ip_idx, s_ip_idx; | |
1da177e4 LT |
1289 | struct net_device *dev; |
1290 | struct in_device *in_dev; | |
1291 | struct in_ifaddr *ifa; | |
eec4df98 ED |
1292 | struct hlist_head *head; |
1293 | struct hlist_node *node; | |
1da177e4 | 1294 | |
eec4df98 ED |
1295 | s_h = cb->args[0]; |
1296 | s_idx = idx = cb->args[1]; | |
1297 | s_ip_idx = ip_idx = cb->args[2]; | |
1298 | ||
1299 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
1300 | idx = 0; | |
1301 | head = &net->dev_index_head[h]; | |
1302 | rcu_read_lock(); | |
1303 | hlist_for_each_entry_rcu(dev, node, head, index_hlist) { | |
1304 | if (idx < s_idx) | |
1305 | goto cont; | |
4b97efdf | 1306 | if (h > s_h || idx > s_idx) |
eec4df98 ED |
1307 | s_ip_idx = 0; |
1308 | in_dev = __in_dev_get_rcu(dev); | |
1309 | if (!in_dev) | |
1310 | goto cont; | |
1da177e4 | 1311 | |
eec4df98 ED |
1312 | for (ifa = in_dev->ifa_list, ip_idx = 0; ifa; |
1313 | ifa = ifa->ifa_next, ip_idx++) { | |
1314 | if (ip_idx < s_ip_idx) | |
1315 | continue; | |
1316 | if (inet_fill_ifaddr(skb, ifa, | |
15e47304 | 1317 | NETLINK_CB(cb->skb).portid, |
1da177e4 | 1318 | cb->nlh->nlmsg_seq, |
eec4df98 ED |
1319 | RTM_NEWADDR, NLM_F_MULTI) <= 0) { |
1320 | rcu_read_unlock(); | |
1321 | goto done; | |
1322 | } | |
1323 | } | |
7562f876 | 1324 | cont: |
eec4df98 ED |
1325 | idx++; |
1326 | } | |
1327 | rcu_read_unlock(); | |
1da177e4 LT |
1328 | } |
1329 | ||
1330 | done: | |
eec4df98 ED |
1331 | cb->args[0] = h; |
1332 | cb->args[1] = idx; | |
1333 | cb->args[2] = ip_idx; | |
1da177e4 LT |
1334 | |
1335 | return skb->len; | |
1336 | } | |
1337 | ||
539afedf | 1338 | static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
15e47304 | 1339 | u32 portid) |
1da177e4 | 1340 | { |
47f68512 | 1341 | struct sk_buff *skb; |
d6062cbb TG |
1342 | u32 seq = nlh ? nlh->nlmsg_seq : 0; |
1343 | int err = -ENOBUFS; | |
4b8aa9ab | 1344 | struct net *net; |
1da177e4 | 1345 | |
c346dca1 | 1346 | net = dev_net(ifa->ifa_dev->dev); |
339bf98f | 1347 | skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); |
47f68512 | 1348 | if (skb == NULL) |
d6062cbb TG |
1349 | goto errout; |
1350 | ||
15e47304 | 1351 | err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0); |
26932566 PM |
1352 | if (err < 0) { |
1353 | /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ | |
1354 | WARN_ON(err == -EMSGSIZE); | |
1355 | kfree_skb(skb); | |
1356 | goto errout; | |
1357 | } | |
15e47304 | 1358 | rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); |
1ce85fe4 | 1359 | return; |
d6062cbb TG |
1360 | errout: |
1361 | if (err < 0) | |
4b8aa9ab | 1362 | rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err); |
1da177e4 LT |
1363 | } |
1364 | ||
9f0f7272 TG |
1365 | static size_t inet_get_link_af_size(const struct net_device *dev) |
1366 | { | |
1fc19aff | 1367 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
9f0f7272 TG |
1368 | |
1369 | if (!in_dev) | |
1370 | return 0; | |
1371 | ||
1372 | return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */ | |
1373 | } | |
1374 | ||
1375 | static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev) | |
1376 | { | |
1fc19aff | 1377 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
9f0f7272 TG |
1378 | struct nlattr *nla; |
1379 | int i; | |
1380 | ||
1381 | if (!in_dev) | |
1382 | return -ENODATA; | |
1383 | ||
1384 | nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4); | |
1385 | if (nla == NULL) | |
1386 | return -EMSGSIZE; | |
1387 | ||
1388 | for (i = 0; i < IPV4_DEVCONF_MAX; i++) | |
1389 | ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i]; | |
1390 | ||
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = { | |
1395 | [IFLA_INET_CONF] = { .type = NLA_NESTED }, | |
1396 | }; | |
1397 | ||
cf7afbfe TG |
1398 | static int inet_validate_link_af(const struct net_device *dev, |
1399 | const struct nlattr *nla) | |
9f0f7272 | 1400 | { |
9f0f7272 TG |
1401 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
1402 | int err, rem; | |
1403 | ||
f7fce74e | 1404 | if (dev && !__in_dev_get_rtnl(dev)) |
cf7afbfe | 1405 | return -EAFNOSUPPORT; |
9f0f7272 TG |
1406 | |
1407 | err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy); | |
1408 | if (err < 0) | |
1409 | return err; | |
1410 | ||
1411 | if (tb[IFLA_INET_CONF]) { | |
1412 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) { | |
1413 | int cfgid = nla_type(a); | |
1414 | ||
1415 | if (nla_len(a) < 4) | |
1416 | return -EINVAL; | |
1417 | ||
1418 | if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX) | |
1419 | return -EINVAL; | |
1420 | } | |
1421 | } | |
1422 | ||
cf7afbfe TG |
1423 | return 0; |
1424 | } | |
1425 | ||
1426 | static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla) | |
1427 | { | |
f7fce74e | 1428 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
cf7afbfe TG |
1429 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
1430 | int rem; | |
1431 | ||
1432 | if (!in_dev) | |
1433 | return -EAFNOSUPPORT; | |
1434 | ||
1435 | if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0) | |
1436 | BUG(); | |
1437 | ||
9f0f7272 TG |
1438 | if (tb[IFLA_INET_CONF]) { |
1439 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) | |
1440 | ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a)); | |
1441 | } | |
1442 | ||
1443 | return 0; | |
1444 | } | |
1445 | ||
edc9e748 ND |
1446 | static int inet_netconf_msgsize_devconf(int type) |
1447 | { | |
1448 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) | |
1449 | + nla_total_size(4); /* NETCONFA_IFINDEX */ | |
1450 | ||
9e551110 ND |
1451 | /* type -1 is used for ALL */ |
1452 | if (type == -1 || type == NETCONFA_FORWARDING) | |
edc9e748 | 1453 | size += nla_total_size(4); |
cc535dfb ND |
1454 | if (type == -1 || type == NETCONFA_RP_FILTER) |
1455 | size += nla_total_size(4); | |
edc9e748 ND |
1456 | |
1457 | return size; | |
1458 | } | |
1459 | ||
1460 | static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex, | |
1461 | struct ipv4_devconf *devconf, u32 portid, | |
1462 | u32 seq, int event, unsigned int flags, | |
1463 | int type) | |
1464 | { | |
1465 | struct nlmsghdr *nlh; | |
1466 | struct netconfmsg *ncm; | |
1467 | ||
1468 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), | |
1469 | flags); | |
1470 | if (nlh == NULL) | |
1471 | return -EMSGSIZE; | |
1472 | ||
1473 | ncm = nlmsg_data(nlh); | |
1474 | ncm->ncm_family = AF_INET; | |
1475 | ||
1476 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) | |
1477 | goto nla_put_failure; | |
1478 | ||
9e551110 ND |
1479 | /* type -1 is used for ALL */ |
1480 | if ((type == -1 || type == NETCONFA_FORWARDING) && | |
edc9e748 ND |
1481 | nla_put_s32(skb, NETCONFA_FORWARDING, |
1482 | IPV4_DEVCONF(*devconf, FORWARDING)) < 0) | |
1483 | goto nla_put_failure; | |
cc535dfb ND |
1484 | if ((type == -1 || type == NETCONFA_RP_FILTER) && |
1485 | nla_put_s32(skb, NETCONFA_RP_FILTER, | |
1486 | IPV4_DEVCONF(*devconf, RP_FILTER)) < 0) | |
1487 | goto nla_put_failure; | |
edc9e748 ND |
1488 | |
1489 | return nlmsg_end(skb, nlh); | |
1490 | ||
1491 | nla_put_failure: | |
1492 | nlmsg_cancel(skb, nlh); | |
1493 | return -EMSGSIZE; | |
1494 | } | |
1495 | ||
1496 | static void inet_netconf_notify_devconf(struct net *net, int type, int ifindex, | |
1497 | struct ipv4_devconf *devconf) | |
1498 | { | |
1499 | struct sk_buff *skb; | |
1500 | int err = -ENOBUFS; | |
1501 | ||
1502 | skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_ATOMIC); | |
1503 | if (skb == NULL) | |
1504 | goto errout; | |
1505 | ||
1506 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, | |
1507 | RTM_NEWNETCONF, 0, type); | |
1508 | if (err < 0) { | |
1509 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ | |
1510 | WARN_ON(err == -EMSGSIZE); | |
1511 | kfree_skb(skb); | |
1512 | goto errout; | |
1513 | } | |
1514 | rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_ATOMIC); | |
1515 | return; | |
1516 | errout: | |
1517 | if (err < 0) | |
1518 | rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err); | |
1519 | } | |
1520 | ||
9e551110 ND |
1521 | static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = { |
1522 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, | |
1523 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, | |
cc535dfb | 1524 | [NETCONFA_RP_FILTER] = { .len = sizeof(int) }, |
9e551110 ND |
1525 | }; |
1526 | ||
1527 | static int inet_netconf_get_devconf(struct sk_buff *in_skb, | |
1528 | struct nlmsghdr *nlh, | |
1529 | void *arg) | |
1530 | { | |
1531 | struct net *net = sock_net(in_skb->sk); | |
1532 | struct nlattr *tb[NETCONFA_MAX+1]; | |
1533 | struct netconfmsg *ncm; | |
1534 | struct sk_buff *skb; | |
1535 | struct ipv4_devconf *devconf; | |
1536 | struct in_device *in_dev; | |
1537 | struct net_device *dev; | |
1538 | int ifindex; | |
1539 | int err; | |
1540 | ||
1541 | err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, | |
1542 | devconf_ipv4_policy); | |
1543 | if (err < 0) | |
1544 | goto errout; | |
1545 | ||
1546 | err = EINVAL; | |
1547 | if (!tb[NETCONFA_IFINDEX]) | |
1548 | goto errout; | |
1549 | ||
1550 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); | |
1551 | switch (ifindex) { | |
1552 | case NETCONFA_IFINDEX_ALL: | |
1553 | devconf = net->ipv4.devconf_all; | |
1554 | break; | |
1555 | case NETCONFA_IFINDEX_DEFAULT: | |
1556 | devconf = net->ipv4.devconf_dflt; | |
1557 | break; | |
1558 | default: | |
1559 | dev = __dev_get_by_index(net, ifindex); | |
1560 | if (dev == NULL) | |
1561 | goto errout; | |
1562 | in_dev = __in_dev_get_rtnl(dev); | |
1563 | if (in_dev == NULL) | |
1564 | goto errout; | |
1565 | devconf = &in_dev->cnf; | |
1566 | break; | |
1567 | } | |
1568 | ||
1569 | err = -ENOBUFS; | |
1570 | skb = nlmsg_new(inet_netconf_msgsize_devconf(-1), GFP_ATOMIC); | |
1571 | if (skb == NULL) | |
1572 | goto errout; | |
1573 | ||
1574 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, | |
1575 | NETLINK_CB(in_skb).portid, | |
1576 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, | |
1577 | -1); | |
1578 | if (err < 0) { | |
1579 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ | |
1580 | WARN_ON(err == -EMSGSIZE); | |
1581 | kfree_skb(skb); | |
1582 | goto errout; | |
1583 | } | |
1584 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); | |
1585 | errout: | |
1586 | return err; | |
1587 | } | |
1588 | ||
1da177e4 LT |
1589 | #ifdef CONFIG_SYSCTL |
1590 | ||
c0ce9fb3 | 1591 | static void devinet_copy_dflt_conf(struct net *net, int i) |
31be3085 HX |
1592 | { |
1593 | struct net_device *dev; | |
1594 | ||
c6d14c84 ED |
1595 | rcu_read_lock(); |
1596 | for_each_netdev_rcu(net, dev) { | |
31be3085 | 1597 | struct in_device *in_dev; |
c6d14c84 | 1598 | |
31be3085 HX |
1599 | in_dev = __in_dev_get_rcu(dev); |
1600 | if (in_dev && !test_bit(i, in_dev->cnf.state)) | |
9355bbd6 | 1601 | in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; |
31be3085 | 1602 | } |
c6d14c84 | 1603 | rcu_read_unlock(); |
31be3085 HX |
1604 | } |
1605 | ||
c6d14c84 | 1606 | /* called with RTNL locked */ |
c0ce9fb3 | 1607 | static void inet_forward_change(struct net *net) |
68dd299b PE |
1608 | { |
1609 | struct net_device *dev; | |
586f1211 | 1610 | int on = IPV4_DEVCONF_ALL(net, FORWARDING); |
68dd299b | 1611 | |
586f1211 | 1612 | IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; |
9355bbd6 | 1613 | IPV4_DEVCONF_DFLT(net, FORWARDING) = on; |
edc9e748 ND |
1614 | inet_netconf_notify_devconf(net, NETCONFA_FORWARDING, |
1615 | NETCONFA_IFINDEX_ALL, | |
1616 | net->ipv4.devconf_all); | |
1617 | inet_netconf_notify_devconf(net, NETCONFA_FORWARDING, | |
1618 | NETCONFA_IFINDEX_DEFAULT, | |
1619 | net->ipv4.devconf_dflt); | |
68dd299b | 1620 | |
c0ce9fb3 | 1621 | for_each_netdev(net, dev) { |
68dd299b | 1622 | struct in_device *in_dev; |
0187bdfb BH |
1623 | if (on) |
1624 | dev_disable_lro(dev); | |
68dd299b PE |
1625 | rcu_read_lock(); |
1626 | in_dev = __in_dev_get_rcu(dev); | |
edc9e748 | 1627 | if (in_dev) { |
68dd299b | 1628 | IN_DEV_CONF_SET(in_dev, FORWARDING, on); |
edc9e748 ND |
1629 | inet_netconf_notify_devconf(net, NETCONFA_FORWARDING, |
1630 | dev->ifindex, &in_dev->cnf); | |
1631 | } | |
68dd299b PE |
1632 | rcu_read_unlock(); |
1633 | } | |
68dd299b PE |
1634 | } |
1635 | ||
31be3085 | 1636 | static int devinet_conf_proc(ctl_table *ctl, int write, |
8d65af78 | 1637 | void __user *buffer, |
31be3085 HX |
1638 | size_t *lenp, loff_t *ppos) |
1639 | { | |
d01ff0a0 | 1640 | int old_value = *(int *)ctl->data; |
8d65af78 | 1641 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
d01ff0a0 | 1642 | int new_value = *(int *)ctl->data; |
31be3085 HX |
1643 | |
1644 | if (write) { | |
1645 | struct ipv4_devconf *cnf = ctl->extra1; | |
c0ce9fb3 | 1646 | struct net *net = ctl->extra2; |
31be3085 HX |
1647 | int i = (int *)ctl->data - cnf->data; |
1648 | ||
1649 | set_bit(i, cnf->state); | |
1650 | ||
9355bbd6 | 1651 | if (cnf == net->ipv4.devconf_dflt) |
c0ce9fb3 | 1652 | devinet_copy_dflt_conf(net, i); |
d0daebc3 TG |
1653 | if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 || |
1654 | i == IPV4_DEVCONF_ROUTE_LOCALNET - 1) | |
d01ff0a0 | 1655 | if ((new_value == 0) && (old_value != 0)) |
4ccfe6d4 | 1656 | rt_cache_flush(net); |
cc535dfb ND |
1657 | if (i == IPV4_DEVCONF_RP_FILTER - 1 && |
1658 | new_value != old_value) { | |
1659 | int ifindex; | |
1660 | ||
1661 | if (cnf == net->ipv4.devconf_dflt) | |
1662 | ifindex = NETCONFA_IFINDEX_DEFAULT; | |
1663 | else if (cnf == net->ipv4.devconf_all) | |
1664 | ifindex = NETCONFA_IFINDEX_ALL; | |
1665 | else { | |
1666 | struct in_device *idev = | |
1667 | container_of(cnf, struct in_device, | |
1668 | cnf); | |
1669 | ifindex = idev->dev->ifindex; | |
1670 | } | |
1671 | inet_netconf_notify_devconf(net, NETCONFA_RP_FILTER, | |
1672 | ifindex, cnf); | |
1673 | } | |
31be3085 HX |
1674 | } |
1675 | ||
1676 | return ret; | |
1677 | } | |
1678 | ||
1da177e4 | 1679 | static int devinet_sysctl_forward(ctl_table *ctl, int write, |
8d65af78 | 1680 | void __user *buffer, |
1da177e4 LT |
1681 | size_t *lenp, loff_t *ppos) |
1682 | { | |
1683 | int *valp = ctl->data; | |
1684 | int val = *valp; | |
88af182e | 1685 | loff_t pos = *ppos; |
8d65af78 | 1686 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
1da177e4 LT |
1687 | |
1688 | if (write && *valp != val) { | |
c0ce9fb3 PE |
1689 | struct net *net = ctl->extra2; |
1690 | ||
0187bdfb | 1691 | if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) { |
88af182e EB |
1692 | if (!rtnl_trylock()) { |
1693 | /* Restore the original values before restarting */ | |
1694 | *valp = val; | |
1695 | *ppos = pos; | |
9b8adb5e | 1696 | return restart_syscall(); |
88af182e | 1697 | } |
0187bdfb BH |
1698 | if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) { |
1699 | inet_forward_change(net); | |
edc9e748 | 1700 | } else { |
0187bdfb BH |
1701 | struct ipv4_devconf *cnf = ctl->extra1; |
1702 | struct in_device *idev = | |
1703 | container_of(cnf, struct in_device, cnf); | |
edc9e748 ND |
1704 | if (*valp) |
1705 | dev_disable_lro(idev->dev); | |
1706 | inet_netconf_notify_devconf(net, | |
1707 | NETCONFA_FORWARDING, | |
1708 | idev->dev->ifindex, | |
1709 | cnf); | |
0187bdfb BH |
1710 | } |
1711 | rtnl_unlock(); | |
4ccfe6d4 | 1712 | rt_cache_flush(net); |
edc9e748 ND |
1713 | } else |
1714 | inet_netconf_notify_devconf(net, NETCONFA_FORWARDING, | |
1715 | NETCONFA_IFINDEX_DEFAULT, | |
1716 | net->ipv4.devconf_dflt); | |
1da177e4 LT |
1717 | } |
1718 | ||
1719 | return ret; | |
1720 | } | |
1721 | ||
323e126f DM |
1722 | static int ipv4_doint_and_flush(ctl_table *ctl, int write, |
1723 | void __user *buffer, | |
1724 | size_t *lenp, loff_t *ppos) | |
1da177e4 LT |
1725 | { |
1726 | int *valp = ctl->data; | |
1727 | int val = *valp; | |
8d65af78 | 1728 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
76e6ebfb | 1729 | struct net *net = ctl->extra2; |
1da177e4 LT |
1730 | |
1731 | if (write && *valp != val) | |
4ccfe6d4 | 1732 | rt_cache_flush(net); |
1da177e4 LT |
1733 | |
1734 | return ret; | |
1735 | } | |
1736 | ||
f8572d8f | 1737 | #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \ |
42f811b8 | 1738 | { \ |
42f811b8 HX |
1739 | .procname = name, \ |
1740 | .data = ipv4_devconf.data + \ | |
02291680 | 1741 | IPV4_DEVCONF_ ## attr - 1, \ |
42f811b8 HX |
1742 | .maxlen = sizeof(int), \ |
1743 | .mode = mval, \ | |
1744 | .proc_handler = proc, \ | |
31be3085 | 1745 | .extra1 = &ipv4_devconf, \ |
42f811b8 HX |
1746 | } |
1747 | ||
1748 | #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ | |
f8572d8f | 1749 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc) |
42f811b8 HX |
1750 | |
1751 | #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ | |
f8572d8f | 1752 | DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc) |
42f811b8 | 1753 | |
f8572d8f EB |
1754 | #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \ |
1755 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc) | |
42f811b8 HX |
1756 | |
1757 | #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ | |
f8572d8f | 1758 | DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush) |
42f811b8 | 1759 | |
1da177e4 LT |
1760 | static struct devinet_sysctl_table { |
1761 | struct ctl_table_header *sysctl_header; | |
02291680 | 1762 | struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX]; |
1da177e4 LT |
1763 | } devinet_sysctl = { |
1764 | .devinet_vars = { | |
42f811b8 | 1765 | DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", |
f8572d8f | 1766 | devinet_sysctl_forward), |
42f811b8 HX |
1767 | DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), |
1768 | ||
1769 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), | |
1770 | DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), | |
1771 | DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), | |
1772 | DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), | |
1773 | DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), | |
1774 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, | |
1775 | "accept_source_route"), | |
8153a10c | 1776 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"), |
28f6aeea | 1777 | DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"), |
42f811b8 HX |
1778 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), |
1779 | DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), | |
1780 | DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), | |
1781 | DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), | |
1782 | DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), | |
1783 | DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), | |
1784 | DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), | |
1785 | DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), | |
1786 | DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), | |
eefef1cf | 1787 | DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"), |
65324144 | 1788 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"), |
42f811b8 HX |
1789 | |
1790 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), | |
1791 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), | |
1792 | DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION, | |
1793 | "force_igmp_version"), | |
1794 | DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, | |
1795 | "promote_secondaries"), | |
d0daebc3 TG |
1796 | DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET, |
1797 | "route_localnet"), | |
1da177e4 | 1798 | }, |
1da177e4 LT |
1799 | }; |
1800 | ||
ea40b324 | 1801 | static int __devinet_sysctl_register(struct net *net, char *dev_name, |
f8572d8f | 1802 | struct ipv4_devconf *p) |
1da177e4 LT |
1803 | { |
1804 | int i; | |
9fa89642 | 1805 | struct devinet_sysctl_table *t; |
8607ddb8 | 1806 | char path[sizeof("net/ipv4/conf/") + IFNAMSIZ]; |
bfada697 | 1807 | |
9fa89642 | 1808 | t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL); |
1da177e4 | 1809 | if (!t) |
9fa89642 PE |
1810 | goto out; |
1811 | ||
1da177e4 LT |
1812 | for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) { |
1813 | t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; | |
31be3085 | 1814 | t->devinet_vars[i].extra1 = p; |
c0ce9fb3 | 1815 | t->devinet_vars[i].extra2 = net; |
1da177e4 LT |
1816 | } |
1817 | ||
8607ddb8 | 1818 | snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name); |
1da177e4 | 1819 | |
8607ddb8 | 1820 | t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars); |
1da177e4 | 1821 | if (!t->sysctl_header) |
8607ddb8 | 1822 | goto free; |
1da177e4 LT |
1823 | |
1824 | p->sysctl = t; | |
ea40b324 | 1825 | return 0; |
1da177e4 | 1826 | |
9fa89642 | 1827 | free: |
1da177e4 | 1828 | kfree(t); |
9fa89642 | 1829 | out: |
ea40b324 | 1830 | return -ENOBUFS; |
1da177e4 LT |
1831 | } |
1832 | ||
51602b2a PE |
1833 | static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf) |
1834 | { | |
1835 | struct devinet_sysctl_table *t = cnf->sysctl; | |
1836 | ||
1837 | if (t == NULL) | |
1838 | return; | |
1839 | ||
1840 | cnf->sysctl = NULL; | |
ff538818 | 1841 | unregister_net_sysctl_table(t->sysctl_header); |
51602b2a PE |
1842 | kfree(t); |
1843 | } | |
1844 | ||
66f27a52 PE |
1845 | static void devinet_sysctl_register(struct in_device *idev) |
1846 | { | |
54716e3b | 1847 | neigh_sysctl_register(idev->dev, idev->arp_parms, "ipv4", NULL); |
c346dca1 | 1848 | __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name, |
f8572d8f | 1849 | &idev->cnf); |
66f27a52 PE |
1850 | } |
1851 | ||
51602b2a | 1852 | static void devinet_sysctl_unregister(struct in_device *idev) |
1da177e4 | 1853 | { |
51602b2a PE |
1854 | __devinet_sysctl_unregister(&idev->cnf); |
1855 | neigh_sysctl_unregister(idev->arp_parms); | |
1da177e4 | 1856 | } |
1da177e4 | 1857 | |
68dd299b PE |
1858 | static struct ctl_table ctl_forward_entry[] = { |
1859 | { | |
68dd299b PE |
1860 | .procname = "ip_forward", |
1861 | .data = &ipv4_devconf.data[ | |
02291680 | 1862 | IPV4_DEVCONF_FORWARDING - 1], |
68dd299b PE |
1863 | .maxlen = sizeof(int), |
1864 | .mode = 0644, | |
1865 | .proc_handler = devinet_sysctl_forward, | |
68dd299b | 1866 | .extra1 = &ipv4_devconf, |
c0ce9fb3 | 1867 | .extra2 = &init_net, |
68dd299b PE |
1868 | }, |
1869 | { }, | |
1870 | }; | |
2a75de0c | 1871 | #endif |
68dd299b | 1872 | |
752d14dc PE |
1873 | static __net_init int devinet_init_net(struct net *net) |
1874 | { | |
1875 | int err; | |
752d14dc | 1876 | struct ipv4_devconf *all, *dflt; |
2a75de0c ED |
1877 | #ifdef CONFIG_SYSCTL |
1878 | struct ctl_table *tbl = ctl_forward_entry; | |
752d14dc | 1879 | struct ctl_table_header *forw_hdr; |
2a75de0c | 1880 | #endif |
752d14dc PE |
1881 | |
1882 | err = -ENOMEM; | |
1883 | all = &ipv4_devconf; | |
1884 | dflt = &ipv4_devconf_dflt; | |
752d14dc | 1885 | |
09ad9bc7 | 1886 | if (!net_eq(net, &init_net)) { |
752d14dc PE |
1887 | all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL); |
1888 | if (all == NULL) | |
1889 | goto err_alloc_all; | |
1890 | ||
1891 | dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); | |
1892 | if (dflt == NULL) | |
1893 | goto err_alloc_dflt; | |
1894 | ||
2a75de0c | 1895 | #ifdef CONFIG_SYSCTL |
752d14dc PE |
1896 | tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL); |
1897 | if (tbl == NULL) | |
1898 | goto err_alloc_ctl; | |
1899 | ||
02291680 | 1900 | tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1]; |
752d14dc PE |
1901 | tbl[0].extra1 = all; |
1902 | tbl[0].extra2 = net; | |
2a75de0c | 1903 | #endif |
752d14dc PE |
1904 | } |
1905 | ||
1906 | #ifdef CONFIG_SYSCTL | |
f8572d8f | 1907 | err = __devinet_sysctl_register(net, "all", all); |
752d14dc PE |
1908 | if (err < 0) |
1909 | goto err_reg_all; | |
1910 | ||
f8572d8f | 1911 | err = __devinet_sysctl_register(net, "default", dflt); |
752d14dc PE |
1912 | if (err < 0) |
1913 | goto err_reg_dflt; | |
1914 | ||
1915 | err = -ENOMEM; | |
8607ddb8 | 1916 | forw_hdr = register_net_sysctl(net, "net/ipv4", tbl); |
752d14dc PE |
1917 | if (forw_hdr == NULL) |
1918 | goto err_reg_ctl; | |
2a75de0c | 1919 | net->ipv4.forw_hdr = forw_hdr; |
752d14dc PE |
1920 | #endif |
1921 | ||
752d14dc PE |
1922 | net->ipv4.devconf_all = all; |
1923 | net->ipv4.devconf_dflt = dflt; | |
1924 | return 0; | |
1925 | ||
1926 | #ifdef CONFIG_SYSCTL | |
1927 | err_reg_ctl: | |
1928 | __devinet_sysctl_unregister(dflt); | |
1929 | err_reg_dflt: | |
1930 | __devinet_sysctl_unregister(all); | |
1931 | err_reg_all: | |
1932 | if (tbl != ctl_forward_entry) | |
1933 | kfree(tbl); | |
752d14dc | 1934 | err_alloc_ctl: |
2a75de0c | 1935 | #endif |
752d14dc PE |
1936 | if (dflt != &ipv4_devconf_dflt) |
1937 | kfree(dflt); | |
1938 | err_alloc_dflt: | |
1939 | if (all != &ipv4_devconf) | |
1940 | kfree(all); | |
1941 | err_alloc_all: | |
1942 | return err; | |
1943 | } | |
1944 | ||
1945 | static __net_exit void devinet_exit_net(struct net *net) | |
1946 | { | |
2a75de0c | 1947 | #ifdef CONFIG_SYSCTL |
752d14dc PE |
1948 | struct ctl_table *tbl; |
1949 | ||
1950 | tbl = net->ipv4.forw_hdr->ctl_table_arg; | |
752d14dc PE |
1951 | unregister_net_sysctl_table(net->ipv4.forw_hdr); |
1952 | __devinet_sysctl_unregister(net->ipv4.devconf_dflt); | |
1953 | __devinet_sysctl_unregister(net->ipv4.devconf_all); | |
752d14dc | 1954 | kfree(tbl); |
2a75de0c | 1955 | #endif |
752d14dc PE |
1956 | kfree(net->ipv4.devconf_dflt); |
1957 | kfree(net->ipv4.devconf_all); | |
1958 | } | |
1959 | ||
1960 | static __net_initdata struct pernet_operations devinet_ops = { | |
1961 | .init = devinet_init_net, | |
1962 | .exit = devinet_exit_net, | |
1963 | }; | |
1964 | ||
9f0f7272 TG |
1965 | static struct rtnl_af_ops inet_af_ops = { |
1966 | .family = AF_INET, | |
1967 | .fill_link_af = inet_fill_link_af, | |
1968 | .get_link_af_size = inet_get_link_af_size, | |
cf7afbfe TG |
1969 | .validate_link_af = inet_validate_link_af, |
1970 | .set_link_af = inet_set_link_af, | |
9f0f7272 TG |
1971 | }; |
1972 | ||
1da177e4 LT |
1973 | void __init devinet_init(void) |
1974 | { | |
fd23c3b3 DM |
1975 | int i; |
1976 | ||
1977 | for (i = 0; i < IN4_ADDR_HSIZE; i++) | |
1978 | INIT_HLIST_HEAD(&inet_addr_lst[i]); | |
1979 | ||
752d14dc PE |
1980 | register_pernet_subsys(&devinet_ops); |
1981 | ||
1da177e4 LT |
1982 | register_gifconf(PF_INET, inet_gifconf); |
1983 | register_netdevice_notifier(&ip_netdev_notifier); | |
63f3444f | 1984 | |
9f0f7272 TG |
1985 | rtnl_af_register(&inet_af_ops); |
1986 | ||
c7ac8679 GR |
1987 | rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, NULL); |
1988 | rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, NULL); | |
1989 | rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, NULL); | |
9e551110 ND |
1990 | rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf, |
1991 | NULL, NULL); | |
1da177e4 LT |
1992 | } |
1993 |