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