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