1 /*
2 * NET3 IP device support routines.
3 *
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
10 * Authors: Ross Biro
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
28
29 #include <asm/uaccess.h>
30 #include <linux/bitops.h>
31 #include <linux/capability.h>
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
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>
42 #include <linux/if_addr.h>
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>
48 #include <linux/init.h>
49 #include <linux/notifier.h>
50 #include <linux/inetdevice.h>
51 #include <linux/igmp.h>
52 #include <linux/slab.h>
53 #include <linux/hash.h>
54 #ifdef CONFIG_SYSCTL
55 #include <linux/sysctl.h>
56 #endif
57 #include <linux/kmod.h>
58 #include <linux/netconf.h>
59
60 #include <net/arp.h>
61 #include <net/ip.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64 #include <net/rtnetlink.h>
65 #include <net/net_namespace.h>
66 #include <net/addrconf.h>
67
68 #include "fib_lookup.h"
69
70 static struct ipv4_devconf ipv4_devconf = {
71 .data = {
72 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
73 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
74 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
75 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
76 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
77 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
78 },
79 };
80
81 static struct ipv4_devconf ipv4_devconf_dflt = {
82 .data = {
83 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
84 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
85 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
86 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
87 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
88 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
89 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
90 },
91 };
92
93 #define IPV4_DEVCONF_DFLT(net, attr) \
94 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
95
96 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
97 [IFA_LOCAL] = { .type = NLA_U32 },
98 [IFA_ADDRESS] = { .type = NLA_U32 },
99 [IFA_BROADCAST] = { .type = NLA_U32 },
100 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
101 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
102 [IFA_FLAGS] = { .type = NLA_U32 },
103 };
104
105 #define IN4_ADDR_HSIZE_SHIFT 8
106 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
107
108 static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
109
inet_addr_hash(struct net * net,__be32 addr)110 static u32 inet_addr_hash(struct net *net, __be32 addr)
111 {
112 u32 val = (__force u32) addr ^ net_hash_mix(net);
113
114 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
115 }
116
inet_hash_insert(struct net * net,struct in_ifaddr * ifa)117 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
118 {
119 u32 hash = inet_addr_hash(net, ifa->ifa_local);
120
121 ASSERT_RTNL();
122 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
123 }
124
inet_hash_remove(struct in_ifaddr * ifa)125 static void inet_hash_remove(struct in_ifaddr *ifa)
126 {
127 ASSERT_RTNL();
128 hlist_del_init_rcu(&ifa->hash);
129 }
130
131 /**
132 * __ip_dev_find - find the first device with a given source address.
133 * @net: the net namespace
134 * @addr: the source address
135 * @devref: if true, take a reference on the found device
136 *
137 * If a caller uses devref=false, it should be protected by RCU, or RTNL
138 */
__ip_dev_find(struct net * net,__be32 addr,bool devref)139 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
140 {
141 u32 hash = inet_addr_hash(net, addr);
142 struct net_device *result = NULL;
143 struct in_ifaddr *ifa;
144
145 rcu_read_lock();
146 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash) {
147 if (ifa->ifa_local == addr) {
148 struct net_device *dev = ifa->ifa_dev->dev;
149
150 if (!net_eq(dev_net(dev), net))
151 continue;
152 result = dev;
153 break;
154 }
155 }
156 if (!result) {
157 struct flowi4 fl4 = { .daddr = addr };
158 struct fib_result res = { 0 };
159 struct fib_table *local;
160
161 /* Fallback to FIB local table so that communication
162 * over loopback subnets work.
163 */
164 local = fib_get_table(net, RT_TABLE_LOCAL);
165 if (local &&
166 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
167 res.type == RTN_LOCAL)
168 result = FIB_RES_DEV(res);
169 }
170 if (result && devref)
171 dev_hold(result);
172 rcu_read_unlock();
173 return result;
174 }
175 EXPORT_SYMBOL(__ip_dev_find);
176
177 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
178
179 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
180 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
181 int destroy);
182 #ifdef CONFIG_SYSCTL
183 static int devinet_sysctl_register(struct in_device *idev);
184 static void devinet_sysctl_unregister(struct in_device *idev);
185 #else
devinet_sysctl_register(struct in_device * idev)186 static int devinet_sysctl_register(struct in_device *idev)
187 {
188 return 0;
189 }
devinet_sysctl_unregister(struct in_device * idev)190 static void devinet_sysctl_unregister(struct in_device *idev)
191 {
192 }
193 #endif
194
195 /* Locks all the inet devices. */
196
inet_alloc_ifa(void)197 static struct in_ifaddr *inet_alloc_ifa(void)
198 {
199 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
200 }
201
inet_rcu_free_ifa(struct rcu_head * head)202 static void inet_rcu_free_ifa(struct rcu_head *head)
203 {
204 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
205 if (ifa->ifa_dev)
206 in_dev_put(ifa->ifa_dev);
207 kfree(ifa);
208 }
209
inet_free_ifa(struct in_ifaddr * ifa)210 static void inet_free_ifa(struct in_ifaddr *ifa)
211 {
212 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
213 }
214
in_dev_finish_destroy(struct in_device * idev)215 void in_dev_finish_destroy(struct in_device *idev)
216 {
217 struct net_device *dev = idev->dev;
218
219 WARN_ON(idev->ifa_list);
220 WARN_ON(idev->mc_list);
221 kfree(rcu_dereference_protected(idev->mc_hash, 1));
222 #ifdef NET_REFCNT_DEBUG
223 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
224 #endif
225 dev_put(dev);
226 if (!idev->dead)
227 pr_err("Freeing alive in_device %p\n", idev);
228 else
229 kfree(idev);
230 }
231 EXPORT_SYMBOL(in_dev_finish_destroy);
232
inetdev_init(struct net_device * dev)233 static struct in_device *inetdev_init(struct net_device *dev)
234 {
235 struct in_device *in_dev;
236 int err = -ENOMEM;
237
238 ASSERT_RTNL();
239
240 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
241 if (!in_dev)
242 goto out;
243 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
244 sizeof(in_dev->cnf));
245 in_dev->cnf.sysctl = NULL;
246 in_dev->dev = dev;
247 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
248 if (!in_dev->arp_parms)
249 goto out_kfree;
250 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
251 dev_disable_lro(dev);
252 /* Reference in_dev->dev */
253 dev_hold(dev);
254 /* Account for reference dev->ip_ptr (below) */
255 in_dev_hold(in_dev);
256
257 err = devinet_sysctl_register(in_dev);
258 if (err) {
259 in_dev->dead = 1;
260 in_dev_put(in_dev);
261 in_dev = NULL;
262 goto out;
263 }
264 ip_mc_init_dev(in_dev);
265 if (dev->flags & IFF_UP)
266 ip_mc_up(in_dev);
267
268 /* we can receive as soon as ip_ptr is set -- do this last */
269 rcu_assign_pointer(dev->ip_ptr, in_dev);
270 out:
271 return in_dev ?: ERR_PTR(err);
272 out_kfree:
273 kfree(in_dev);
274 in_dev = NULL;
275 goto out;
276 }
277
in_dev_rcu_put(struct rcu_head * head)278 static void in_dev_rcu_put(struct rcu_head *head)
279 {
280 struct in_device *idev = container_of(head, struct in_device, rcu_head);
281 in_dev_put(idev);
282 }
283
inetdev_destroy(struct in_device * in_dev)284 static void inetdev_destroy(struct in_device *in_dev)
285 {
286 struct in_ifaddr *ifa;
287 struct net_device *dev;
288
289 ASSERT_RTNL();
290
291 dev = in_dev->dev;
292
293 in_dev->dead = 1;
294
295 ip_mc_destroy_dev(in_dev);
296
297 while ((ifa = in_dev->ifa_list) != NULL) {
298 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
299 inet_free_ifa(ifa);
300 }
301
302 RCU_INIT_POINTER(dev->ip_ptr, NULL);
303
304 devinet_sysctl_unregister(in_dev);
305 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
306 arp_ifdown(dev);
307
308 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
309 }
310
inet_addr_onlink(struct in_device * in_dev,__be32 a,__be32 b)311 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
312 {
313 rcu_read_lock();
314 for_primary_ifa(in_dev) {
315 if (inet_ifa_match(a, ifa)) {
316 if (!b || inet_ifa_match(b, ifa)) {
317 rcu_read_unlock();
318 return 1;
319 }
320 }
321 } endfor_ifa(in_dev);
322 rcu_read_unlock();
323 return 0;
324 }
325
__inet_del_ifa(struct in_device * in_dev,struct in_ifaddr ** ifap,int destroy,struct nlmsghdr * nlh,u32 portid)326 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
327 int destroy, struct nlmsghdr *nlh, u32 portid)
328 {
329 struct in_ifaddr *promote = NULL;
330 struct in_ifaddr *ifa, *ifa1 = *ifap;
331 struct in_ifaddr *last_prim = in_dev->ifa_list;
332 struct in_ifaddr *prev_prom = NULL;
333 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
334
335 ASSERT_RTNL();
336
337 if (in_dev->dead)
338 goto no_promotions;
339
340 /* 1. Deleting primary ifaddr forces deletion all secondaries
341 * unless alias promotion is set
342 **/
343
344 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
345 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
346
347 while ((ifa = *ifap1) != NULL) {
348 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
349 ifa1->ifa_scope <= ifa->ifa_scope)
350 last_prim = ifa;
351
352 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
353 ifa1->ifa_mask != ifa->ifa_mask ||
354 !inet_ifa_match(ifa1->ifa_address, ifa)) {
355 ifap1 = &ifa->ifa_next;
356 prev_prom = ifa;
357 continue;
358 }
359
360 if (!do_promote) {
361 inet_hash_remove(ifa);
362 *ifap1 = ifa->ifa_next;
363
364 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
365 blocking_notifier_call_chain(&inetaddr_chain,
366 NETDEV_DOWN, ifa);
367 inet_free_ifa(ifa);
368 } else {
369 promote = ifa;
370 break;
371 }
372 }
373 }
374
375 /* On promotion all secondaries from subnet are changing
376 * the primary IP, we must remove all their routes silently
377 * and later to add them back with new prefsrc. Do this
378 * while all addresses are on the device list.
379 */
380 for (ifa = promote; ifa; ifa = ifa->ifa_next) {
381 if (ifa1->ifa_mask == ifa->ifa_mask &&
382 inet_ifa_match(ifa1->ifa_address, ifa))
383 fib_del_ifaddr(ifa, ifa1);
384 }
385
386 no_promotions:
387 /* 2. Unlink it */
388
389 *ifap = ifa1->ifa_next;
390 inet_hash_remove(ifa1);
391
392 /* 3. Announce address deletion */
393
394 /* Send message first, then call notifier.
395 At first sight, FIB update triggered by notifier
396 will refer to already deleted ifaddr, that could confuse
397 netlink listeners. It is not true: look, gated sees
398 that route deleted and if it still thinks that ifaddr
399 is valid, it will try to restore deleted routes... Grr.
400 So that, this order is correct.
401 */
402 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
403 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
404
405 if (promote) {
406 struct in_ifaddr *next_sec = promote->ifa_next;
407
408 if (prev_prom) {
409 prev_prom->ifa_next = promote->ifa_next;
410 promote->ifa_next = last_prim->ifa_next;
411 last_prim->ifa_next = promote;
412 }
413
414 promote->ifa_flags &= ~IFA_F_SECONDARY;
415 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
416 blocking_notifier_call_chain(&inetaddr_chain,
417 NETDEV_UP, promote);
418 for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
419 if (ifa1->ifa_mask != ifa->ifa_mask ||
420 !inet_ifa_match(ifa1->ifa_address, ifa))
421 continue;
422 fib_add_ifaddr(ifa);
423 }
424
425 }
426 if (destroy)
427 inet_free_ifa(ifa1);
428 }
429
inet_del_ifa(struct in_device * in_dev,struct in_ifaddr ** ifap,int destroy)430 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
431 int destroy)
432 {
433 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
434 }
435
436 static void check_lifetime(struct work_struct *work);
437
438 static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
439
__inet_insert_ifa(struct in_ifaddr * ifa,struct nlmsghdr * nlh,u32 portid)440 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
441 u32 portid)
442 {
443 struct in_device *in_dev = ifa->ifa_dev;
444 struct in_ifaddr *ifa1, **ifap, **last_primary;
445
446 ASSERT_RTNL();
447
448 if (!ifa->ifa_local) {
449 inet_free_ifa(ifa);
450 return 0;
451 }
452
453 ifa->ifa_flags &= ~IFA_F_SECONDARY;
454 last_primary = &in_dev->ifa_list;
455
456 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
457 ifap = &ifa1->ifa_next) {
458 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
459 ifa->ifa_scope <= ifa1->ifa_scope)
460 last_primary = &ifa1->ifa_next;
461 if (ifa1->ifa_mask == ifa->ifa_mask &&
462 inet_ifa_match(ifa1->ifa_address, ifa)) {
463 if (ifa1->ifa_local == ifa->ifa_local) {
464 inet_free_ifa(ifa);
465 return -EEXIST;
466 }
467 if (ifa1->ifa_scope != ifa->ifa_scope) {
468 inet_free_ifa(ifa);
469 return -EINVAL;
470 }
471 ifa->ifa_flags |= IFA_F_SECONDARY;
472 }
473 }
474
475 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
476 prandom_seed((__force u32) ifa->ifa_local);
477 ifap = last_primary;
478 }
479
480 ifa->ifa_next = *ifap;
481 *ifap = ifa;
482
483 inet_hash_insert(dev_net(in_dev->dev), ifa);
484
485 cancel_delayed_work(&check_lifetime_work);
486 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
487
488 /* Send message first, then call notifier.
489 Notifier will trigger FIB update, so that
490 listeners of netlink will know about new ifaddr */
491 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
492 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
493
494 return 0;
495 }
496
inet_insert_ifa(struct in_ifaddr * ifa)497 static int inet_insert_ifa(struct in_ifaddr *ifa)
498 {
499 return __inet_insert_ifa(ifa, NULL, 0);
500 }
501
inet_set_ifa(struct net_device * dev,struct in_ifaddr * ifa)502 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
503 {
504 struct in_device *in_dev = __in_dev_get_rtnl(dev);
505
506 ASSERT_RTNL();
507
508 if (!in_dev) {
509 inet_free_ifa(ifa);
510 return -ENOBUFS;
511 }
512 ipv4_devconf_setall(in_dev);
513 neigh_parms_data_state_setall(in_dev->arp_parms);
514 if (ifa->ifa_dev != in_dev) {
515 WARN_ON(ifa->ifa_dev);
516 in_dev_hold(in_dev);
517 ifa->ifa_dev = in_dev;
518 }
519 if (ipv4_is_loopback(ifa->ifa_local))
520 ifa->ifa_scope = RT_SCOPE_HOST;
521 return inet_insert_ifa(ifa);
522 }
523
524 /* Caller must hold RCU or RTNL :
525 * We dont take a reference on found in_device
526 */
inetdev_by_index(struct net * net,int ifindex)527 struct in_device *inetdev_by_index(struct net *net, int ifindex)
528 {
529 struct net_device *dev;
530 struct in_device *in_dev = NULL;
531
532 rcu_read_lock();
533 dev = dev_get_by_index_rcu(net, ifindex);
534 if (dev)
535 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
536 rcu_read_unlock();
537 return in_dev;
538 }
539 EXPORT_SYMBOL(inetdev_by_index);
540
541 /* Called only from RTNL semaphored context. No locks. */
542
inet_ifa_byprefix(struct in_device * in_dev,__be32 prefix,__be32 mask)543 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
544 __be32 mask)
545 {
546 ASSERT_RTNL();
547
548 for_primary_ifa(in_dev) {
549 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
550 return ifa;
551 } endfor_ifa(in_dev);
552 return NULL;
553 }
554
inet_rtm_deladdr(struct sk_buff * skb,struct nlmsghdr * nlh)555 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
556 {
557 struct net *net = sock_net(skb->sk);
558 struct nlattr *tb[IFA_MAX+1];
559 struct in_device *in_dev;
560 struct ifaddrmsg *ifm;
561 struct in_ifaddr *ifa, **ifap;
562 int err = -EINVAL;
563
564 ASSERT_RTNL();
565
566 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
567 if (err < 0)
568 goto errout;
569
570 ifm = nlmsg_data(nlh);
571 in_dev = inetdev_by_index(net, ifm->ifa_index);
572 if (in_dev == NULL) {
573 err = -ENODEV;
574 goto errout;
575 }
576
577 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
578 ifap = &ifa->ifa_next) {
579 if (tb[IFA_LOCAL] &&
580 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
581 continue;
582
583 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
584 continue;
585
586 if (tb[IFA_ADDRESS] &&
587 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
588 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
589 continue;
590
591 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
592 return 0;
593 }
594
595 err = -EADDRNOTAVAIL;
596 errout:
597 return err;
598 }
599
600 #define INFINITY_LIFE_TIME 0xFFFFFFFF
601
check_lifetime(struct work_struct * work)602 static void check_lifetime(struct work_struct *work)
603 {
604 unsigned long now, next, next_sec, next_sched;
605 struct in_ifaddr *ifa;
606 struct hlist_node *n;
607 int i;
608
609 now = jiffies;
610 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
611
612 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
613 bool change_needed = false;
614
615 rcu_read_lock();
616 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
617 unsigned long age;
618
619 if (ifa->ifa_flags & IFA_F_PERMANENT)
620 continue;
621
622 /* We try to batch several events at once. */
623 age = (now - ifa->ifa_tstamp +
624 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
625
626 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
627 age >= ifa->ifa_valid_lft) {
628 change_needed = true;
629 } else if (ifa->ifa_preferred_lft ==
630 INFINITY_LIFE_TIME) {
631 continue;
632 } else if (age >= ifa->ifa_preferred_lft) {
633 if (time_before(ifa->ifa_tstamp +
634 ifa->ifa_valid_lft * HZ, next))
635 next = ifa->ifa_tstamp +
636 ifa->ifa_valid_lft * HZ;
637
638 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
639 change_needed = true;
640 } else if (time_before(ifa->ifa_tstamp +
641 ifa->ifa_preferred_lft * HZ,
642 next)) {
643 next = ifa->ifa_tstamp +
644 ifa->ifa_preferred_lft * HZ;
645 }
646 }
647 rcu_read_unlock();
648 if (!change_needed)
649 continue;
650 rtnl_lock();
651 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
652 unsigned long age;
653
654 if (ifa->ifa_flags & IFA_F_PERMANENT)
655 continue;
656
657 /* We try to batch several events at once. */
658 age = (now - ifa->ifa_tstamp +
659 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
660
661 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
662 age >= ifa->ifa_valid_lft) {
663 struct in_ifaddr **ifap;
664
665 for (ifap = &ifa->ifa_dev->ifa_list;
666 *ifap != NULL; ifap = &(*ifap)->ifa_next) {
667 if (*ifap == ifa) {
668 inet_del_ifa(ifa->ifa_dev,
669 ifap, 1);
670 break;
671 }
672 }
673 } else if (ifa->ifa_preferred_lft !=
674 INFINITY_LIFE_TIME &&
675 age >= ifa->ifa_preferred_lft &&
676 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
677 ifa->ifa_flags |= IFA_F_DEPRECATED;
678 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
679 }
680 }
681 rtnl_unlock();
682 }
683
684 next_sec = round_jiffies_up(next);
685 next_sched = next;
686
687 /* If rounded timeout is accurate enough, accept it. */
688 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
689 next_sched = next_sec;
690
691 now = jiffies;
692 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
693 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
694 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
695
696 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
697 next_sched - now);
698 }
699
set_ifa_lifetime(struct in_ifaddr * ifa,__u32 valid_lft,__u32 prefered_lft)700 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
701 __u32 prefered_lft)
702 {
703 unsigned long timeout;
704
705 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
706
707 timeout = addrconf_timeout_fixup(valid_lft, HZ);
708 if (addrconf_finite_timeout(timeout))
709 ifa->ifa_valid_lft = timeout;
710 else
711 ifa->ifa_flags |= IFA_F_PERMANENT;
712
713 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
714 if (addrconf_finite_timeout(timeout)) {
715 if (timeout == 0)
716 ifa->ifa_flags |= IFA_F_DEPRECATED;
717 ifa->ifa_preferred_lft = timeout;
718 }
719 ifa->ifa_tstamp = jiffies;
720 if (!ifa->ifa_cstamp)
721 ifa->ifa_cstamp = ifa->ifa_tstamp;
722 }
723
rtm_to_ifaddr(struct net * net,struct nlmsghdr * nlh,__u32 * pvalid_lft,__u32 * pprefered_lft)724 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
725 __u32 *pvalid_lft, __u32 *pprefered_lft)
726 {
727 struct nlattr *tb[IFA_MAX+1];
728 struct in_ifaddr *ifa;
729 struct ifaddrmsg *ifm;
730 struct net_device *dev;
731 struct in_device *in_dev;
732 int err;
733
734 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
735 if (err < 0)
736 goto errout;
737
738 ifm = nlmsg_data(nlh);
739 err = -EINVAL;
740 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
741 goto errout;
742
743 dev = __dev_get_by_index(net, ifm->ifa_index);
744 err = -ENODEV;
745 if (dev == NULL)
746 goto errout;
747
748 in_dev = __in_dev_get_rtnl(dev);
749 err = -ENOBUFS;
750 if (in_dev == NULL)
751 goto errout;
752
753 ifa = inet_alloc_ifa();
754 if (ifa == NULL)
755 /*
756 * A potential indev allocation can be left alive, it stays
757 * assigned to its device and is destroy with it.
758 */
759 goto errout;
760
761 ipv4_devconf_setall(in_dev);
762 neigh_parms_data_state_setall(in_dev->arp_parms);
763 in_dev_hold(in_dev);
764
765 if (tb[IFA_ADDRESS] == NULL)
766 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
767
768 INIT_HLIST_NODE(&ifa->hash);
769 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
770 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
771 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
772 ifm->ifa_flags;
773 ifa->ifa_scope = ifm->ifa_scope;
774 ifa->ifa_dev = in_dev;
775
776 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
777 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
778
779 if (tb[IFA_BROADCAST])
780 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
781
782 if (tb[IFA_LABEL])
783 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
784 else
785 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
786
787 if (tb[IFA_CACHEINFO]) {
788 struct ifa_cacheinfo *ci;
789
790 ci = nla_data(tb[IFA_CACHEINFO]);
791 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
792 err = -EINVAL;
793 goto errout_free;
794 }
795 *pvalid_lft = ci->ifa_valid;
796 *pprefered_lft = ci->ifa_prefered;
797 }
798
799 return ifa;
800
801 errout_free:
802 inet_free_ifa(ifa);
803 errout:
804 return ERR_PTR(err);
805 }
806
find_matching_ifa(struct in_ifaddr * ifa)807 static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
808 {
809 struct in_device *in_dev = ifa->ifa_dev;
810 struct in_ifaddr *ifa1, **ifap;
811
812 if (!ifa->ifa_local)
813 return NULL;
814
815 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
816 ifap = &ifa1->ifa_next) {
817 if (ifa1->ifa_mask == ifa->ifa_mask &&
818 inet_ifa_match(ifa1->ifa_address, ifa) &&
819 ifa1->ifa_local == ifa->ifa_local)
820 return ifa1;
821 }
822 return NULL;
823 }
824
inet_rtm_newaddr(struct sk_buff * skb,struct nlmsghdr * nlh)825 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
826 {
827 struct net *net = sock_net(skb->sk);
828 struct in_ifaddr *ifa;
829 struct in_ifaddr *ifa_existing;
830 __u32 valid_lft = INFINITY_LIFE_TIME;
831 __u32 prefered_lft = INFINITY_LIFE_TIME;
832
833 ASSERT_RTNL();
834
835 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft);
836 if (IS_ERR(ifa))
837 return PTR_ERR(ifa);
838
839 ifa_existing = find_matching_ifa(ifa);
840 if (!ifa_existing) {
841 /* It would be best to check for !NLM_F_CREATE here but
842 * userspace already relies on not having to provide this.
843 */
844 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
845 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid);
846 } else {
847 inet_free_ifa(ifa);
848
849 if (nlh->nlmsg_flags & NLM_F_EXCL ||
850 !(nlh->nlmsg_flags & NLM_F_REPLACE))
851 return -EEXIST;
852 ifa = ifa_existing;
853 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
854 cancel_delayed_work(&check_lifetime_work);
855 queue_delayed_work(system_power_efficient_wq,
856 &check_lifetime_work, 0);
857 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
858 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
859 }
860 return 0;
861 }
862
863 /*
864 * Determine a default network mask, based on the IP address.
865 */
866
inet_abc_len(__be32 addr)867 static int inet_abc_len(__be32 addr)
868 {
869 int rc = -1; /* Something else, probably a multicast. */
870
871 if (ipv4_is_zeronet(addr))
872 rc = 0;
873 else {
874 __u32 haddr = ntohl(addr);
875
876 if (IN_CLASSA(haddr))
877 rc = 8;
878 else if (IN_CLASSB(haddr))
879 rc = 16;
880 else if (IN_CLASSC(haddr))
881 rc = 24;
882 }
883
884 return rc;
885 }
886
887
devinet_ioctl(struct net * net,unsigned int cmd,void __user * arg)888 int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
889 {
890 struct ifreq ifr;
891 struct sockaddr_in sin_orig;
892 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
893 struct in_device *in_dev;
894 struct in_ifaddr **ifap = NULL;
895 struct in_ifaddr *ifa = NULL;
896 struct net_device *dev;
897 char *colon;
898 int ret = -EFAULT;
899 int tryaddrmatch = 0;
900
901 /*
902 * Fetch the caller's info block into kernel space
903 */
904
905 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
906 goto out;
907 ifr.ifr_name[IFNAMSIZ - 1] = 0;
908
909 /* save original address for comparison */
910 memcpy(&sin_orig, sin, sizeof(*sin));
911
912 colon = strchr(ifr.ifr_name, ':');
913 if (colon)
914 *colon = 0;
915
916 dev_load(net, ifr.ifr_name);
917
918 switch (cmd) {
919 case SIOCGIFADDR: /* Get interface address */
920 case SIOCGIFBRDADDR: /* Get the broadcast address */
921 case SIOCGIFDSTADDR: /* Get the destination address */
922 case SIOCGIFNETMASK: /* Get the netmask for the interface */
923 /* Note that these ioctls will not sleep,
924 so that we do not impose a lock.
925 One day we will be forced to put shlock here (I mean SMP)
926 */
927 tryaddrmatch = (sin_orig.sin_family == AF_INET);
928 memset(sin, 0, sizeof(*sin));
929 sin->sin_family = AF_INET;
930 break;
931
932 case SIOCSIFFLAGS:
933 ret = -EPERM;
934 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
935 goto out;
936 break;
937 case SIOCSIFADDR: /* Set interface address (and family) */
938 case SIOCSIFBRDADDR: /* Set the broadcast address */
939 case SIOCSIFDSTADDR: /* Set the destination address */
940 case SIOCSIFNETMASK: /* Set the netmask for the interface */
941 ret = -EPERM;
942 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
943 goto out;
944 ret = -EINVAL;
945 if (sin->sin_family != AF_INET)
946 goto out;
947 break;
948 default:
949 ret = -EINVAL;
950 goto out;
951 }
952
953 rtnl_lock();
954
955 ret = -ENODEV;
956 dev = __dev_get_by_name(net, ifr.ifr_name);
957 if (!dev)
958 goto done;
959
960 if (colon)
961 *colon = ':';
962
963 in_dev = __in_dev_get_rtnl(dev);
964 if (in_dev) {
965 if (tryaddrmatch) {
966 /* Matthias Andree */
967 /* compare label and address (4.4BSD style) */
968 /* note: we only do this for a limited set of ioctls
969 and only if the original address family was AF_INET.
970 This is checked above. */
971 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
972 ifap = &ifa->ifa_next) {
973 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
974 sin_orig.sin_addr.s_addr ==
975 ifa->ifa_local) {
976 break; /* found */
977 }
978 }
979 }
980 /* we didn't get a match, maybe the application is
981 4.3BSD-style and passed in junk so we fall back to
982 comparing just the label */
983 if (!ifa) {
984 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
985 ifap = &ifa->ifa_next)
986 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
987 break;
988 }
989 }
990
991 ret = -EADDRNOTAVAIL;
992 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
993 goto done;
994
995 switch (cmd) {
996 case SIOCGIFADDR: /* Get interface address */
997 sin->sin_addr.s_addr = ifa->ifa_local;
998 goto rarok;
999
1000 case SIOCGIFBRDADDR: /* Get the broadcast address */
1001 sin->sin_addr.s_addr = ifa->ifa_broadcast;
1002 goto rarok;
1003
1004 case SIOCGIFDSTADDR: /* Get the destination address */
1005 sin->sin_addr.s_addr = ifa->ifa_address;
1006 goto rarok;
1007
1008 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1009 sin->sin_addr.s_addr = ifa->ifa_mask;
1010 goto rarok;
1011
1012 case SIOCSIFFLAGS:
1013 if (colon) {
1014 ret = -EADDRNOTAVAIL;
1015 if (!ifa)
1016 break;
1017 ret = 0;
1018 if (!(ifr.ifr_flags & IFF_UP))
1019 inet_del_ifa(in_dev, ifap, 1);
1020 break;
1021 }
1022 ret = dev_change_flags(dev, ifr.ifr_flags);
1023 break;
1024
1025 case SIOCSIFADDR: /* Set interface address (and family) */
1026 ret = -EINVAL;
1027 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1028 break;
1029
1030 if (!ifa) {
1031 ret = -ENOBUFS;
1032 ifa = inet_alloc_ifa();
1033 if (!ifa)
1034 break;
1035 INIT_HLIST_NODE(&ifa->hash);
1036 if (colon)
1037 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
1038 else
1039 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1040 } else {
1041 ret = 0;
1042 if (ifa->ifa_local == sin->sin_addr.s_addr)
1043 break;
1044 inet_del_ifa(in_dev, ifap, 0);
1045 ifa->ifa_broadcast = 0;
1046 ifa->ifa_scope = 0;
1047 }
1048
1049 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1050
1051 if (!(dev->flags & IFF_POINTOPOINT)) {
1052 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1053 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1054 if ((dev->flags & IFF_BROADCAST) &&
1055 ifa->ifa_prefixlen < 31)
1056 ifa->ifa_broadcast = ifa->ifa_address |
1057 ~ifa->ifa_mask;
1058 } else {
1059 ifa->ifa_prefixlen = 32;
1060 ifa->ifa_mask = inet_make_mask(32);
1061 }
1062 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
1063 ret = inet_set_ifa(dev, ifa);
1064 break;
1065
1066 case SIOCSIFBRDADDR: /* Set the broadcast address */
1067 ret = 0;
1068 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1069 inet_del_ifa(in_dev, ifap, 0);
1070 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1071 inet_insert_ifa(ifa);
1072 }
1073 break;
1074
1075 case SIOCSIFDSTADDR: /* Set the destination address */
1076 ret = 0;
1077 if (ifa->ifa_address == sin->sin_addr.s_addr)
1078 break;
1079 ret = -EINVAL;
1080 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1081 break;
1082 ret = 0;
1083 inet_del_ifa(in_dev, ifap, 0);
1084 ifa->ifa_address = sin->sin_addr.s_addr;
1085 inet_insert_ifa(ifa);
1086 break;
1087
1088 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1089
1090 /*
1091 * The mask we set must be legal.
1092 */
1093 ret = -EINVAL;
1094 if (bad_mask(sin->sin_addr.s_addr, 0))
1095 break;
1096 ret = 0;
1097 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1098 __be32 old_mask = ifa->ifa_mask;
1099 inet_del_ifa(in_dev, ifap, 0);
1100 ifa->ifa_mask = sin->sin_addr.s_addr;
1101 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1102
1103 /* See if current broadcast address matches
1104 * with current netmask, then recalculate
1105 * the broadcast address. Otherwise it's a
1106 * funny address, so don't touch it since
1107 * the user seems to know what (s)he's doing...
1108 */
1109 if ((dev->flags & IFF_BROADCAST) &&
1110 (ifa->ifa_prefixlen < 31) &&
1111 (ifa->ifa_broadcast ==
1112 (ifa->ifa_local|~old_mask))) {
1113 ifa->ifa_broadcast = (ifa->ifa_local |
1114 ~sin->sin_addr.s_addr);
1115 }
1116 inet_insert_ifa(ifa);
1117 }
1118 break;
1119 }
1120 done:
1121 rtnl_unlock();
1122 out:
1123 return ret;
1124 rarok:
1125 rtnl_unlock();
1126 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
1127 goto out;
1128 }
1129
inet_gifconf(struct net_device * dev,char __user * buf,int len)1130 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
1131 {
1132 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1133 struct in_ifaddr *ifa;
1134 struct ifreq ifr;
1135 int done = 0;
1136
1137 if (!in_dev)
1138 goto out;
1139
1140 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1141 if (!buf) {
1142 done += sizeof(ifr);
1143 continue;
1144 }
1145 if (len < (int) sizeof(ifr))
1146 break;
1147 memset(&ifr, 0, sizeof(struct ifreq));
1148 strcpy(ifr.ifr_name, ifa->ifa_label);
1149
1150 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1151 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1152 ifa->ifa_local;
1153
1154 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
1155 done = -EFAULT;
1156 break;
1157 }
1158 buf += sizeof(struct ifreq);
1159 len -= sizeof(struct ifreq);
1160 done += sizeof(struct ifreq);
1161 }
1162 out:
1163 return done;
1164 }
1165
inet_select_addr(const struct net_device * dev,__be32 dst,int scope)1166 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1167 {
1168 __be32 addr = 0;
1169 struct in_device *in_dev;
1170 struct net *net = dev_net(dev);
1171
1172 rcu_read_lock();
1173 in_dev = __in_dev_get_rcu(dev);
1174 if (!in_dev)
1175 goto no_in_dev;
1176
1177 for_primary_ifa(in_dev) {
1178 if (ifa->ifa_scope > scope)
1179 continue;
1180 if (!dst || inet_ifa_match(dst, ifa)) {
1181 addr = ifa->ifa_local;
1182 break;
1183 }
1184 if (!addr)
1185 addr = ifa->ifa_local;
1186 } endfor_ifa(in_dev);
1187
1188 if (addr)
1189 goto out_unlock;
1190 no_in_dev:
1191
1192 /* Not loopback addresses on loopback should be preferred
1193 in this case. It is importnat that lo is the first interface
1194 in dev_base list.
1195 */
1196 for_each_netdev_rcu(net, dev) {
1197 in_dev = __in_dev_get_rcu(dev);
1198 if (!in_dev)
1199 continue;
1200
1201 for_primary_ifa(in_dev) {
1202 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1203 ifa->ifa_scope <= scope) {
1204 addr = ifa->ifa_local;
1205 goto out_unlock;
1206 }
1207 } endfor_ifa(in_dev);
1208 }
1209 out_unlock:
1210 rcu_read_unlock();
1211 return addr;
1212 }
1213 EXPORT_SYMBOL(inet_select_addr);
1214
confirm_addr_indev(struct in_device * in_dev,__be32 dst,__be32 local,int scope)1215 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1216 __be32 local, int scope)
1217 {
1218 int same = 0;
1219 __be32 addr = 0;
1220
1221 for_ifa(in_dev) {
1222 if (!addr &&
1223 (local == ifa->ifa_local || !local) &&
1224 ifa->ifa_scope <= scope) {
1225 addr = ifa->ifa_local;
1226 if (same)
1227 break;
1228 }
1229 if (!same) {
1230 same = (!local || inet_ifa_match(local, ifa)) &&
1231 (!dst || inet_ifa_match(dst, ifa));
1232 if (same && addr) {
1233 if (local || !dst)
1234 break;
1235 /* Is the selected addr into dst subnet? */
1236 if (inet_ifa_match(addr, ifa))
1237 break;
1238 /* No, then can we use new local src? */
1239 if (ifa->ifa_scope <= scope) {
1240 addr = ifa->ifa_local;
1241 break;
1242 }
1243 /* search for large dst subnet for addr */
1244 same = 0;
1245 }
1246 }
1247 } endfor_ifa(in_dev);
1248
1249 return same ? addr : 0;
1250 }
1251
1252 /*
1253 * Confirm that local IP address exists using wildcards:
1254 * - net: netns to check, cannot be NULL
1255 * - in_dev: only on this interface, NULL=any interface
1256 * - dst: only in the same subnet as dst, 0=any dst
1257 * - local: address, 0=autoselect the local address
1258 * - scope: maximum allowed scope value for the local address
1259 */
inet_confirm_addr(struct net * net,struct in_device * in_dev,__be32 dst,__be32 local,int scope)1260 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1261 __be32 dst, __be32 local, int scope)
1262 {
1263 __be32 addr = 0;
1264 struct net_device *dev;
1265
1266 if (in_dev != NULL)
1267 return confirm_addr_indev(in_dev, dst, local, scope);
1268
1269 rcu_read_lock();
1270 for_each_netdev_rcu(net, dev) {
1271 in_dev = __in_dev_get_rcu(dev);
1272 if (in_dev) {
1273 addr = confirm_addr_indev(in_dev, dst, local, scope);
1274 if (addr)
1275 break;
1276 }
1277 }
1278 rcu_read_unlock();
1279
1280 return addr;
1281 }
1282 EXPORT_SYMBOL(inet_confirm_addr);
1283
1284 /*
1285 * Device notifier
1286 */
1287
register_inetaddr_notifier(struct notifier_block * nb)1288 int register_inetaddr_notifier(struct notifier_block *nb)
1289 {
1290 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1291 }
1292 EXPORT_SYMBOL(register_inetaddr_notifier);
1293
unregister_inetaddr_notifier(struct notifier_block * nb)1294 int unregister_inetaddr_notifier(struct notifier_block *nb)
1295 {
1296 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1297 }
1298 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1299
1300 /* Rename ifa_labels for a device name change. Make some effort to preserve
1301 * existing alias numbering and to create unique labels if possible.
1302 */
inetdev_changename(struct net_device * dev,struct in_device * in_dev)1303 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1304 {
1305 struct in_ifaddr *ifa;
1306 int named = 0;
1307
1308 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1309 char old[IFNAMSIZ], *dot;
1310
1311 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1312 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1313 if (named++ == 0)
1314 goto skip;
1315 dot = strchr(old, ':');
1316 if (dot == NULL) {
1317 sprintf(old, ":%d", named);
1318 dot = old;
1319 }
1320 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1321 strcat(ifa->ifa_label, dot);
1322 else
1323 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1324 skip:
1325 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1326 }
1327 }
1328
inetdev_valid_mtu(unsigned int mtu)1329 static bool inetdev_valid_mtu(unsigned int mtu)
1330 {
1331 return mtu >= IPV4_MIN_MTU;
1332 }
1333
inetdev_send_gratuitous_arp(struct net_device * dev,struct in_device * in_dev)1334 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1335 struct in_device *in_dev)
1336
1337 {
1338 struct in_ifaddr *ifa;
1339
1340 for (ifa = in_dev->ifa_list; ifa;
1341 ifa = ifa->ifa_next) {
1342 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1343 ifa->ifa_local, dev,
1344 ifa->ifa_local, NULL,
1345 dev->dev_addr, NULL);
1346 }
1347 }
1348
1349 /* Called only under RTNL semaphore */
1350
inetdev_event(struct notifier_block * this,unsigned long event,void * ptr)1351 static int inetdev_event(struct notifier_block *this, unsigned long event,
1352 void *ptr)
1353 {
1354 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1355 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1356
1357 ASSERT_RTNL();
1358
1359 if (!in_dev) {
1360 if (event == NETDEV_REGISTER) {
1361 in_dev = inetdev_init(dev);
1362 if (IS_ERR(in_dev))
1363 return notifier_from_errno(PTR_ERR(in_dev));
1364 if (dev->flags & IFF_LOOPBACK) {
1365 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1366 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1367 }
1368 } else if (event == NETDEV_CHANGEMTU) {
1369 /* Re-enabling IP */
1370 if (inetdev_valid_mtu(dev->mtu))
1371 in_dev = inetdev_init(dev);
1372 }
1373 goto out;
1374 }
1375
1376 switch (event) {
1377 case NETDEV_REGISTER:
1378 pr_debug("%s: bug\n", __func__);
1379 RCU_INIT_POINTER(dev->ip_ptr, NULL);
1380 break;
1381 case NETDEV_UP:
1382 if (!inetdev_valid_mtu(dev->mtu))
1383 break;
1384 if (dev->flags & IFF_LOOPBACK) {
1385 struct in_ifaddr *ifa = inet_alloc_ifa();
1386
1387 if (ifa) {
1388 INIT_HLIST_NODE(&ifa->hash);
1389 ifa->ifa_local =
1390 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1391 ifa->ifa_prefixlen = 8;
1392 ifa->ifa_mask = inet_make_mask(8);
1393 in_dev_hold(in_dev);
1394 ifa->ifa_dev = in_dev;
1395 ifa->ifa_scope = RT_SCOPE_HOST;
1396 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1397 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1398 INFINITY_LIFE_TIME);
1399 ipv4_devconf_setall(in_dev);
1400 neigh_parms_data_state_setall(in_dev->arp_parms);
1401 inet_insert_ifa(ifa);
1402 }
1403 }
1404 ip_mc_up(in_dev);
1405 /* fall through */
1406 case NETDEV_CHANGEADDR:
1407 if (!IN_DEV_ARP_NOTIFY(in_dev))
1408 break;
1409 /* fall through */
1410 case NETDEV_NOTIFY_PEERS:
1411 /* Send gratuitous ARP to notify of link change */
1412 inetdev_send_gratuitous_arp(dev, in_dev);
1413 break;
1414 case NETDEV_DOWN:
1415 ip_mc_down(in_dev);
1416 break;
1417 case NETDEV_PRE_TYPE_CHANGE:
1418 ip_mc_unmap(in_dev);
1419 break;
1420 case NETDEV_POST_TYPE_CHANGE:
1421 ip_mc_remap(in_dev);
1422 break;
1423 case NETDEV_CHANGEMTU:
1424 if (inetdev_valid_mtu(dev->mtu))
1425 break;
1426 /* disable IP when MTU is not enough */
1427 case NETDEV_UNREGISTER:
1428 inetdev_destroy(in_dev);
1429 break;
1430 case NETDEV_CHANGENAME:
1431 /* Do not notify about label change, this event is
1432 * not interesting to applications using netlink.
1433 */
1434 inetdev_changename(dev, in_dev);
1435
1436 devinet_sysctl_unregister(in_dev);
1437 devinet_sysctl_register(in_dev);
1438 break;
1439 }
1440 out:
1441 return NOTIFY_DONE;
1442 }
1443
1444 static struct notifier_block ip_netdev_notifier = {
1445 .notifier_call = inetdev_event,
1446 };
1447
inet_nlmsg_size(void)1448 static size_t inet_nlmsg_size(void)
1449 {
1450 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1451 + nla_total_size(4) /* IFA_ADDRESS */
1452 + nla_total_size(4) /* IFA_LOCAL */
1453 + nla_total_size(4) /* IFA_BROADCAST */
1454 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
1455 + nla_total_size(4) /* IFA_FLAGS */
1456 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
1457 }
1458
cstamp_delta(unsigned long cstamp)1459 static inline u32 cstamp_delta(unsigned long cstamp)
1460 {
1461 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1462 }
1463
put_cacheinfo(struct sk_buff * skb,unsigned long cstamp,unsigned long tstamp,u32 preferred,u32 valid)1464 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1465 unsigned long tstamp, u32 preferred, u32 valid)
1466 {
1467 struct ifa_cacheinfo ci;
1468
1469 ci.cstamp = cstamp_delta(cstamp);
1470 ci.tstamp = cstamp_delta(tstamp);
1471 ci.ifa_prefered = preferred;
1472 ci.ifa_valid = valid;
1473
1474 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1475 }
1476
inet_fill_ifaddr(struct sk_buff * skb,struct in_ifaddr * ifa,u32 portid,u32 seq,int event,unsigned int flags)1477 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1478 u32 portid, u32 seq, int event, unsigned int flags)
1479 {
1480 struct ifaddrmsg *ifm;
1481 struct nlmsghdr *nlh;
1482 u32 preferred, valid;
1483
1484 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
1485 if (nlh == NULL)
1486 return -EMSGSIZE;
1487
1488 ifm = nlmsg_data(nlh);
1489 ifm->ifa_family = AF_INET;
1490 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1491 ifm->ifa_flags = ifa->ifa_flags;
1492 ifm->ifa_scope = ifa->ifa_scope;
1493 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1494
1495 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1496 preferred = ifa->ifa_preferred_lft;
1497 valid = ifa->ifa_valid_lft;
1498 if (preferred != INFINITY_LIFE_TIME) {
1499 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1500
1501 if (preferred > tval)
1502 preferred -= tval;
1503 else
1504 preferred = 0;
1505 if (valid != INFINITY_LIFE_TIME) {
1506 if (valid > tval)
1507 valid -= tval;
1508 else
1509 valid = 0;
1510 }
1511 }
1512 } else {
1513 preferred = INFINITY_LIFE_TIME;
1514 valid = INFINITY_LIFE_TIME;
1515 }
1516 if ((ifa->ifa_address &&
1517 nla_put_be32(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1518 (ifa->ifa_local &&
1519 nla_put_be32(skb, IFA_LOCAL, ifa->ifa_local)) ||
1520 (ifa->ifa_broadcast &&
1521 nla_put_be32(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1522 (ifa->ifa_label[0] &&
1523 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1524 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
1525 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1526 preferred, valid))
1527 goto nla_put_failure;
1528
1529 return nlmsg_end(skb, nlh);
1530
1531 nla_put_failure:
1532 nlmsg_cancel(skb, nlh);
1533 return -EMSGSIZE;
1534 }
1535
inet_dump_ifaddr(struct sk_buff * skb,struct netlink_callback * cb)1536 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1537 {
1538 struct net *net = sock_net(skb->sk);
1539 int h, s_h;
1540 int idx, s_idx;
1541 int ip_idx, s_ip_idx;
1542 struct net_device *dev;
1543 struct in_device *in_dev;
1544 struct in_ifaddr *ifa;
1545 struct hlist_head *head;
1546
1547 s_h = cb->args[0];
1548 s_idx = idx = cb->args[1];
1549 s_ip_idx = ip_idx = cb->args[2];
1550
1551 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1552 idx = 0;
1553 head = &net->dev_index_head[h];
1554 rcu_read_lock();
1555 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
1556 net->dev_base_seq;
1557 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1558 if (idx < s_idx)
1559 goto cont;
1560 if (h > s_h || idx > s_idx)
1561 s_ip_idx = 0;
1562 in_dev = __in_dev_get_rcu(dev);
1563 if (!in_dev)
1564 goto cont;
1565
1566 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1567 ifa = ifa->ifa_next, ip_idx++) {
1568 if (ip_idx < s_ip_idx)
1569 continue;
1570 if (inet_fill_ifaddr(skb, ifa,
1571 NETLINK_CB(cb->skb).portid,
1572 cb->nlh->nlmsg_seq,
1573 RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1574 rcu_read_unlock();
1575 goto done;
1576 }
1577 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1578 }
1579 cont:
1580 idx++;
1581 }
1582 rcu_read_unlock();
1583 }
1584
1585 done:
1586 cb->args[0] = h;
1587 cb->args[1] = idx;
1588 cb->args[2] = ip_idx;
1589
1590 return skb->len;
1591 }
1592
rtmsg_ifa(int event,struct in_ifaddr * ifa,struct nlmsghdr * nlh,u32 portid)1593 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1594 u32 portid)
1595 {
1596 struct sk_buff *skb;
1597 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1598 int err = -ENOBUFS;
1599 struct net *net;
1600
1601 net = dev_net(ifa->ifa_dev->dev);
1602 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1603 if (skb == NULL)
1604 goto errout;
1605
1606 err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0);
1607 if (err < 0) {
1608 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1609 WARN_ON(err == -EMSGSIZE);
1610 kfree_skb(skb);
1611 goto errout;
1612 }
1613 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1614 return;
1615 errout:
1616 if (err < 0)
1617 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1618 }
1619
inet_get_link_af_size(const struct net_device * dev)1620 static size_t inet_get_link_af_size(const struct net_device *dev)
1621 {
1622 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1623
1624 if (!in_dev)
1625 return 0;
1626
1627 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1628 }
1629
inet_fill_link_af(struct sk_buff * skb,const struct net_device * dev)1630 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
1631 {
1632 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
1633 struct nlattr *nla;
1634 int i;
1635
1636 if (!in_dev)
1637 return -ENODATA;
1638
1639 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1640 if (nla == NULL)
1641 return -EMSGSIZE;
1642
1643 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1644 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1645
1646 return 0;
1647 }
1648
1649 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1650 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1651 };
1652
inet_validate_link_af(const struct net_device * dev,const struct nlattr * nla)1653 static int inet_validate_link_af(const struct net_device *dev,
1654 const struct nlattr *nla)
1655 {
1656 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1657 int err, rem;
1658
1659 if (dev && !__in_dev_get_rtnl(dev))
1660 return -EAFNOSUPPORT;
1661
1662 err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
1663 if (err < 0)
1664 return err;
1665
1666 if (tb[IFLA_INET_CONF]) {
1667 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1668 int cfgid = nla_type(a);
1669
1670 if (nla_len(a) < 4)
1671 return -EINVAL;
1672
1673 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1674 return -EINVAL;
1675 }
1676 }
1677
1678 return 0;
1679 }
1680
inet_set_link_af(struct net_device * dev,const struct nlattr * nla)1681 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1682 {
1683 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1684 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1685 int rem;
1686
1687 if (!in_dev)
1688 return -EAFNOSUPPORT;
1689
1690 if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
1691 BUG();
1692
1693 if (tb[IFLA_INET_CONF]) {
1694 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1695 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1696 }
1697
1698 return 0;
1699 }
1700
inet_netconf_msgsize_devconf(int type)1701 static int inet_netconf_msgsize_devconf(int type)
1702 {
1703 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1704 + nla_total_size(4); /* NETCONFA_IFINDEX */
1705
1706 /* type -1 is used for ALL */
1707 if (type == -1 || type == NETCONFA_FORWARDING)
1708 size += nla_total_size(4);
1709 if (type == -1 || type == NETCONFA_RP_FILTER)
1710 size += nla_total_size(4);
1711 if (type == -1 || type == NETCONFA_MC_FORWARDING)
1712 size += nla_total_size(4);
1713 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
1714 size += nla_total_size(4);
1715
1716 return size;
1717 }
1718
inet_netconf_fill_devconf(struct sk_buff * skb,int ifindex,struct ipv4_devconf * devconf,u32 portid,u32 seq,int event,unsigned int flags,int type)1719 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1720 struct ipv4_devconf *devconf, u32 portid,
1721 u32 seq, int event, unsigned int flags,
1722 int type)
1723 {
1724 struct nlmsghdr *nlh;
1725 struct netconfmsg *ncm;
1726
1727 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1728 flags);
1729 if (nlh == NULL)
1730 return -EMSGSIZE;
1731
1732 ncm = nlmsg_data(nlh);
1733 ncm->ncm_family = AF_INET;
1734
1735 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
1736 goto nla_put_failure;
1737
1738 /* type -1 is used for ALL */
1739 if ((type == -1 || type == NETCONFA_FORWARDING) &&
1740 nla_put_s32(skb, NETCONFA_FORWARDING,
1741 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
1742 goto nla_put_failure;
1743 if ((type == -1 || type == NETCONFA_RP_FILTER) &&
1744 nla_put_s32(skb, NETCONFA_RP_FILTER,
1745 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
1746 goto nla_put_failure;
1747 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
1748 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
1749 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
1750 goto nla_put_failure;
1751 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
1752 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
1753 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
1754 goto nla_put_failure;
1755
1756 return nlmsg_end(skb, nlh);
1757
1758 nla_put_failure:
1759 nlmsg_cancel(skb, nlh);
1760 return -EMSGSIZE;
1761 }
1762
inet_netconf_notify_devconf(struct net * net,int type,int ifindex,struct ipv4_devconf * devconf)1763 void inet_netconf_notify_devconf(struct net *net, int type, int ifindex,
1764 struct ipv4_devconf *devconf)
1765 {
1766 struct sk_buff *skb;
1767 int err = -ENOBUFS;
1768
1769 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_ATOMIC);
1770 if (skb == NULL)
1771 goto errout;
1772
1773 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
1774 RTM_NEWNETCONF, 0, type);
1775 if (err < 0) {
1776 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1777 WARN_ON(err == -EMSGSIZE);
1778 kfree_skb(skb);
1779 goto errout;
1780 }
1781 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_ATOMIC);
1782 return;
1783 errout:
1784 if (err < 0)
1785 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
1786 }
1787
1788 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
1789 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1790 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
1791 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
1792 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
1793 };
1794
inet_netconf_get_devconf(struct sk_buff * in_skb,struct nlmsghdr * nlh)1795 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
1796 struct nlmsghdr *nlh)
1797 {
1798 struct net *net = sock_net(in_skb->sk);
1799 struct nlattr *tb[NETCONFA_MAX+1];
1800 struct netconfmsg *ncm;
1801 struct sk_buff *skb;
1802 struct ipv4_devconf *devconf;
1803 struct in_device *in_dev;
1804 struct net_device *dev;
1805 int ifindex;
1806 int err;
1807
1808 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1809 devconf_ipv4_policy);
1810 if (err < 0)
1811 goto errout;
1812
1813 err = EINVAL;
1814 if (!tb[NETCONFA_IFINDEX])
1815 goto errout;
1816
1817 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1818 switch (ifindex) {
1819 case NETCONFA_IFINDEX_ALL:
1820 devconf = net->ipv4.devconf_all;
1821 break;
1822 case NETCONFA_IFINDEX_DEFAULT:
1823 devconf = net->ipv4.devconf_dflt;
1824 break;
1825 default:
1826 dev = __dev_get_by_index(net, ifindex);
1827 if (dev == NULL)
1828 goto errout;
1829 in_dev = __in_dev_get_rtnl(dev);
1830 if (in_dev == NULL)
1831 goto errout;
1832 devconf = &in_dev->cnf;
1833 break;
1834 }
1835
1836 err = -ENOBUFS;
1837 skb = nlmsg_new(inet_netconf_msgsize_devconf(-1), GFP_ATOMIC);
1838 if (skb == NULL)
1839 goto errout;
1840
1841 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
1842 NETLINK_CB(in_skb).portid,
1843 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1844 -1);
1845 if (err < 0) {
1846 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
1847 WARN_ON(err == -EMSGSIZE);
1848 kfree_skb(skb);
1849 goto errout;
1850 }
1851 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1852 errout:
1853 return err;
1854 }
1855
inet_netconf_dump_devconf(struct sk_buff * skb,struct netlink_callback * cb)1856 static int inet_netconf_dump_devconf(struct sk_buff *skb,
1857 struct netlink_callback *cb)
1858 {
1859 struct net *net = sock_net(skb->sk);
1860 int h, s_h;
1861 int idx, s_idx;
1862 struct net_device *dev;
1863 struct in_device *in_dev;
1864 struct hlist_head *head;
1865
1866 s_h = cb->args[0];
1867 s_idx = idx = cb->args[1];
1868
1869 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1870 idx = 0;
1871 head = &net->dev_index_head[h];
1872 rcu_read_lock();
1873 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
1874 net->dev_base_seq;
1875 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1876 if (idx < s_idx)
1877 goto cont;
1878 in_dev = __in_dev_get_rcu(dev);
1879 if (!in_dev)
1880 goto cont;
1881
1882 if (inet_netconf_fill_devconf(skb, dev->ifindex,
1883 &in_dev->cnf,
1884 NETLINK_CB(cb->skb).portid,
1885 cb->nlh->nlmsg_seq,
1886 RTM_NEWNETCONF,
1887 NLM_F_MULTI,
1888 -1) <= 0) {
1889 rcu_read_unlock();
1890 goto done;
1891 }
1892 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1893 cont:
1894 idx++;
1895 }
1896 rcu_read_unlock();
1897 }
1898 if (h == NETDEV_HASHENTRIES) {
1899 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
1900 net->ipv4.devconf_all,
1901 NETLINK_CB(cb->skb).portid,
1902 cb->nlh->nlmsg_seq,
1903 RTM_NEWNETCONF, NLM_F_MULTI,
1904 -1) <= 0)
1905 goto done;
1906 else
1907 h++;
1908 }
1909 if (h == NETDEV_HASHENTRIES + 1) {
1910 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
1911 net->ipv4.devconf_dflt,
1912 NETLINK_CB(cb->skb).portid,
1913 cb->nlh->nlmsg_seq,
1914 RTM_NEWNETCONF, NLM_F_MULTI,
1915 -1) <= 0)
1916 goto done;
1917 else
1918 h++;
1919 }
1920 done:
1921 cb->args[0] = h;
1922 cb->args[1] = idx;
1923
1924 return skb->len;
1925 }
1926
1927 #ifdef CONFIG_SYSCTL
1928
devinet_copy_dflt_conf(struct net * net,int i)1929 static void devinet_copy_dflt_conf(struct net *net, int i)
1930 {
1931 struct net_device *dev;
1932
1933 rcu_read_lock();
1934 for_each_netdev_rcu(net, dev) {
1935 struct in_device *in_dev;
1936
1937 in_dev = __in_dev_get_rcu(dev);
1938 if (in_dev && !test_bit(i, in_dev->cnf.state))
1939 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
1940 }
1941 rcu_read_unlock();
1942 }
1943
1944 /* called with RTNL locked */
inet_forward_change(struct net * net)1945 static void inet_forward_change(struct net *net)
1946 {
1947 struct net_device *dev;
1948 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
1949
1950 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
1951 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
1952 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1953 NETCONFA_IFINDEX_ALL,
1954 net->ipv4.devconf_all);
1955 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1956 NETCONFA_IFINDEX_DEFAULT,
1957 net->ipv4.devconf_dflt);
1958
1959 for_each_netdev(net, dev) {
1960 struct in_device *in_dev;
1961 if (on)
1962 dev_disable_lro(dev);
1963 rcu_read_lock();
1964 in_dev = __in_dev_get_rcu(dev);
1965 if (in_dev) {
1966 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1967 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
1968 dev->ifindex, &in_dev->cnf);
1969 }
1970 rcu_read_unlock();
1971 }
1972 }
1973
devinet_conf_ifindex(struct net * net,struct ipv4_devconf * cnf)1974 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
1975 {
1976 if (cnf == net->ipv4.devconf_dflt)
1977 return NETCONFA_IFINDEX_DEFAULT;
1978 else if (cnf == net->ipv4.devconf_all)
1979 return NETCONFA_IFINDEX_ALL;
1980 else {
1981 struct in_device *idev
1982 = container_of(cnf, struct in_device, cnf);
1983 return idev->dev->ifindex;
1984 }
1985 }
1986
devinet_conf_proc(struct ctl_table * ctl,int write,void __user * buffer,size_t * lenp,loff_t * ppos)1987 static int devinet_conf_proc(struct ctl_table *ctl, int write,
1988 void __user *buffer,
1989 size_t *lenp, loff_t *ppos)
1990 {
1991 int old_value = *(int *)ctl->data;
1992 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1993 int new_value = *(int *)ctl->data;
1994
1995 if (write) {
1996 struct ipv4_devconf *cnf = ctl->extra1;
1997 struct net *net = ctl->extra2;
1998 int i = (int *)ctl->data - cnf->data;
1999 int ifindex;
2000
2001 set_bit(i, cnf->state);
2002
2003 if (cnf == net->ipv4.devconf_dflt)
2004 devinet_copy_dflt_conf(net, i);
2005 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2006 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
2007 if ((new_value == 0) && (old_value != 0))
2008 rt_cache_flush(net);
2009
2010 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2011 new_value != old_value) {
2012 ifindex = devinet_conf_ifindex(net, cnf);
2013 inet_netconf_notify_devconf(net, NETCONFA_RP_FILTER,
2014 ifindex, cnf);
2015 }
2016 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2017 new_value != old_value) {
2018 ifindex = devinet_conf_ifindex(net, cnf);
2019 inet_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
2020 ifindex, cnf);
2021 }
2022 }
2023
2024 return ret;
2025 }
2026
devinet_sysctl_forward(struct ctl_table * ctl,int write,void __user * buffer,size_t * lenp,loff_t * ppos)2027 static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
2028 void __user *buffer,
2029 size_t *lenp, loff_t *ppos)
2030 {
2031 int *valp = ctl->data;
2032 int val = *valp;
2033 loff_t pos = *ppos;
2034 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2035
2036 if (write && *valp != val) {
2037 struct net *net = ctl->extra2;
2038
2039 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2040 if (!rtnl_trylock()) {
2041 /* Restore the original values before restarting */
2042 *valp = val;
2043 *ppos = pos;
2044 return restart_syscall();
2045 }
2046 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2047 inet_forward_change(net);
2048 } else {
2049 struct ipv4_devconf *cnf = ctl->extra1;
2050 struct in_device *idev =
2051 container_of(cnf, struct in_device, cnf);
2052 if (*valp)
2053 dev_disable_lro(idev->dev);
2054 inet_netconf_notify_devconf(net,
2055 NETCONFA_FORWARDING,
2056 idev->dev->ifindex,
2057 cnf);
2058 }
2059 rtnl_unlock();
2060 rt_cache_flush(net);
2061 } else
2062 inet_netconf_notify_devconf(net, NETCONFA_FORWARDING,
2063 NETCONFA_IFINDEX_DEFAULT,
2064 net->ipv4.devconf_dflt);
2065 }
2066
2067 return ret;
2068 }
2069
ipv4_doint_and_flush(struct ctl_table * ctl,int write,void __user * buffer,size_t * lenp,loff_t * ppos)2070 static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
2071 void __user *buffer,
2072 size_t *lenp, loff_t *ppos)
2073 {
2074 int *valp = ctl->data;
2075 int val = *valp;
2076 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2077 struct net *net = ctl->extra2;
2078
2079 if (write && *valp != val)
2080 rt_cache_flush(net);
2081
2082 return ret;
2083 }
2084
2085 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2086 { \
2087 .procname = name, \
2088 .data = ipv4_devconf.data + \
2089 IPV4_DEVCONF_ ## attr - 1, \
2090 .maxlen = sizeof(int), \
2091 .mode = mval, \
2092 .proc_handler = proc, \
2093 .extra1 = &ipv4_devconf, \
2094 }
2095
2096 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2097 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2098
2099 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2100 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2101
2102 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2103 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2104
2105 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
2106 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
2107
2108 static struct devinet_sysctl_table {
2109 struct ctl_table_header *sysctl_header;
2110 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2111 } devinet_sysctl = {
2112 .devinet_vars = {
2113 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2114 devinet_sysctl_forward),
2115 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2116
2117 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2118 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2119 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2120 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2121 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2122 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2123 "accept_source_route"),
2124 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2125 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2126 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2127 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2128 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2129 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2130 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2131 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2132 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2133 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2134 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2135 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2136 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2137 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2138 "force_igmp_version"),
2139 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2140 "igmpv2_unsolicited_report_interval"),
2141 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2142 "igmpv3_unsolicited_report_interval"),
2143
2144 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2145 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
2146 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2147 "promote_secondaries"),
2148 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2149 "route_localnet"),
2150 },
2151 };
2152
__devinet_sysctl_register(struct net * net,char * dev_name,struct ipv4_devconf * p)2153 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2154 struct ipv4_devconf *p)
2155 {
2156 int i;
2157 struct devinet_sysctl_table *t;
2158 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2159
2160 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
2161 if (!t)
2162 goto out;
2163
2164 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2165 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2166 t->devinet_vars[i].extra1 = p;
2167 t->devinet_vars[i].extra2 = net;
2168 }
2169
2170 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2171
2172 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2173 if (!t->sysctl_header)
2174 goto free;
2175
2176 p->sysctl = t;
2177 return 0;
2178
2179 free:
2180 kfree(t);
2181 out:
2182 return -ENOBUFS;
2183 }
2184
__devinet_sysctl_unregister(struct ipv4_devconf * cnf)2185 static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
2186 {
2187 struct devinet_sysctl_table *t = cnf->sysctl;
2188
2189 if (t == NULL)
2190 return;
2191
2192 cnf->sysctl = NULL;
2193 unregister_net_sysctl_table(t->sysctl_header);
2194 kfree(t);
2195 }
2196
devinet_sysctl_register(struct in_device * idev)2197 static int devinet_sysctl_register(struct in_device *idev)
2198 {
2199 int err;
2200
2201 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2202 return -EINVAL;
2203
2204 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2205 if (err)
2206 return err;
2207 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2208 &idev->cnf);
2209 if (err)
2210 neigh_sysctl_unregister(idev->arp_parms);
2211 return err;
2212 }
2213
devinet_sysctl_unregister(struct in_device * idev)2214 static void devinet_sysctl_unregister(struct in_device *idev)
2215 {
2216 __devinet_sysctl_unregister(&idev->cnf);
2217 neigh_sysctl_unregister(idev->arp_parms);
2218 }
2219
2220 static struct ctl_table ctl_forward_entry[] = {
2221 {
2222 .procname = "ip_forward",
2223 .data = &ipv4_devconf.data[
2224 IPV4_DEVCONF_FORWARDING - 1],
2225 .maxlen = sizeof(int),
2226 .mode = 0644,
2227 .proc_handler = devinet_sysctl_forward,
2228 .extra1 = &ipv4_devconf,
2229 .extra2 = &init_net,
2230 },
2231 { },
2232 };
2233 #endif
2234
devinet_init_net(struct net * net)2235 static __net_init int devinet_init_net(struct net *net)
2236 {
2237 int err;
2238 struct ipv4_devconf *all, *dflt;
2239 #ifdef CONFIG_SYSCTL
2240 struct ctl_table *tbl = ctl_forward_entry;
2241 struct ctl_table_header *forw_hdr;
2242 #endif
2243
2244 err = -ENOMEM;
2245 all = &ipv4_devconf;
2246 dflt = &ipv4_devconf_dflt;
2247
2248 if (!net_eq(net, &init_net)) {
2249 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
2250 if (all == NULL)
2251 goto err_alloc_all;
2252
2253 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2254 if (dflt == NULL)
2255 goto err_alloc_dflt;
2256
2257 #ifdef CONFIG_SYSCTL
2258 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
2259 if (tbl == NULL)
2260 goto err_alloc_ctl;
2261
2262 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2263 tbl[0].extra1 = all;
2264 tbl[0].extra2 = net;
2265 #endif
2266 }
2267
2268 #ifdef CONFIG_SYSCTL
2269 err = __devinet_sysctl_register(net, "all", all);
2270 if (err < 0)
2271 goto err_reg_all;
2272
2273 err = __devinet_sysctl_register(net, "default", dflt);
2274 if (err < 0)
2275 goto err_reg_dflt;
2276
2277 err = -ENOMEM;
2278 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2279 if (forw_hdr == NULL)
2280 goto err_reg_ctl;
2281 net->ipv4.forw_hdr = forw_hdr;
2282 #endif
2283
2284 net->ipv4.devconf_all = all;
2285 net->ipv4.devconf_dflt = dflt;
2286 return 0;
2287
2288 #ifdef CONFIG_SYSCTL
2289 err_reg_ctl:
2290 __devinet_sysctl_unregister(dflt);
2291 err_reg_dflt:
2292 __devinet_sysctl_unregister(all);
2293 err_reg_all:
2294 if (tbl != ctl_forward_entry)
2295 kfree(tbl);
2296 err_alloc_ctl:
2297 #endif
2298 if (dflt != &ipv4_devconf_dflt)
2299 kfree(dflt);
2300 err_alloc_dflt:
2301 if (all != &ipv4_devconf)
2302 kfree(all);
2303 err_alloc_all:
2304 return err;
2305 }
2306
devinet_exit_net(struct net * net)2307 static __net_exit void devinet_exit_net(struct net *net)
2308 {
2309 #ifdef CONFIG_SYSCTL
2310 struct ctl_table *tbl;
2311
2312 tbl = net->ipv4.forw_hdr->ctl_table_arg;
2313 unregister_net_sysctl_table(net->ipv4.forw_hdr);
2314 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
2315 __devinet_sysctl_unregister(net->ipv4.devconf_all);
2316 kfree(tbl);
2317 #endif
2318 kfree(net->ipv4.devconf_dflt);
2319 kfree(net->ipv4.devconf_all);
2320 }
2321
2322 static __net_initdata struct pernet_operations devinet_ops = {
2323 .init = devinet_init_net,
2324 .exit = devinet_exit_net,
2325 };
2326
2327 static struct rtnl_af_ops inet_af_ops = {
2328 .family = AF_INET,
2329 .fill_link_af = inet_fill_link_af,
2330 .get_link_af_size = inet_get_link_af_size,
2331 .validate_link_af = inet_validate_link_af,
2332 .set_link_af = inet_set_link_af,
2333 };
2334
devinet_init(void)2335 void __init devinet_init(void)
2336 {
2337 int i;
2338
2339 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2340 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2341
2342 register_pernet_subsys(&devinet_ops);
2343
2344 register_gifconf(PF_INET, inet_gifconf);
2345 register_netdevice_notifier(&ip_netdev_notifier);
2346
2347 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
2348
2349 rtnl_af_register(&inet_af_ops);
2350
2351 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, NULL);
2352 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, NULL);
2353 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, NULL);
2354 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
2355 inet_netconf_dump_devconf, NULL);
2356 }
2357
2358