2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
61 #include <linux/sysctl.h>
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
73 #include <net/af_ieee802154.h>
74 #include <net/firewire.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <linux/if_tunnel.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/netconf.h>
87 #include <linux/random.h>
88 #include <linux/uaccess.h>
89 #include <asm/unaligned.h>
91 #include <linux/proc_fs.h>
92 #include <linux/seq_file.h>
93 #include <linux/export.h>
95 /* Set to 3 to get tracing... */
99 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
101 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
104 #define INFINITY_LIFE_TIME 0xFFFFFFFF
106 #define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109 static inline u32
cstamp_delta(unsigned long cstamp
)
111 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
115 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
116 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
118 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
123 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
128 static void __ipv6_regen_rndid(struct inet6_dev
*idev
);
129 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
130 static void ipv6_regen_rndid(unsigned long data
);
132 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
133 static int ipv6_count_addresses(struct inet6_dev
*idev
);
136 * Configured unicast address hash table
138 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
139 static DEFINE_SPINLOCK(addrconf_hash_lock
);
141 static void addrconf_verify(void);
142 static void addrconf_verify_rtnl(void);
143 static void addrconf_verify_work(struct work_struct
*);
145 static struct workqueue_struct
*addrconf_wq
;
146 static DECLARE_DELAYED_WORK(addr_chk_work
, addrconf_verify_work
);
148 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
149 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
151 static void addrconf_type_change(struct net_device
*dev
,
152 unsigned long event
);
153 static int addrconf_ifdown(struct net_device
*dev
, int how
);
155 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
157 const struct net_device
*dev
,
158 u32 flags
, u32 noflags
);
160 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
161 static void addrconf_dad_work(struct work_struct
*w
);
162 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
163 static void addrconf_dad_run(struct inet6_dev
*idev
);
164 static void addrconf_rs_timer(unsigned long data
);
165 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
166 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
168 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
169 struct prefix_info
*pinfo
);
170 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
171 struct net_device
*dev
);
173 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
175 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
176 .mtu6
= IPV6_MIN_MTU
,
178 .accept_redirects
= 1,
180 .force_mld_version
= 0,
181 .mldv1_unsolicited_report_interval
= 10 * HZ
,
182 .mldv2_unsolicited_report_interval
= HZ
,
184 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
185 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
186 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
188 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
189 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
190 .regen_max_retry
= REGEN_MAX_RETRY
,
191 .max_desync_factor
= MAX_DESYNC_FACTOR
,
192 .max_addresses
= IPV6_MAX_ADDRESSES
,
193 .accept_ra_defrtr
= 1,
194 .accept_ra_from_local
= 0,
195 .accept_ra_pinfo
= 1,
196 #ifdef CONFIG_IPV6_ROUTER_PREF
197 .accept_ra_rtr_pref
= 1,
198 .rtr_probe_interval
= 60 * HZ
,
199 #ifdef CONFIG_IPV6_ROUTE_INFO
200 .accept_ra_rt_info_max_plen
= 0,
204 .accept_source_route
= 0, /* we do not accept RH0 by default. */
207 .suppress_frag_ndisc
= 1,
210 .initialized
= false,
214 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
216 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
217 .mtu6
= IPV6_MIN_MTU
,
219 .accept_redirects
= 1,
221 .force_mld_version
= 0,
222 .mldv1_unsolicited_report_interval
= 10 * HZ
,
223 .mldv2_unsolicited_report_interval
= HZ
,
225 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
226 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
227 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
229 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
230 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
231 .regen_max_retry
= REGEN_MAX_RETRY
,
232 .max_desync_factor
= MAX_DESYNC_FACTOR
,
233 .max_addresses
= IPV6_MAX_ADDRESSES
,
234 .accept_ra_defrtr
= 1,
235 .accept_ra_from_local
= 0,
236 .accept_ra_pinfo
= 1,
237 #ifdef CONFIG_IPV6_ROUTER_PREF
238 .accept_ra_rtr_pref
= 1,
239 .rtr_probe_interval
= 60 * HZ
,
240 #ifdef CONFIG_IPV6_ROUTE_INFO
241 .accept_ra_rt_info_max_plen
= 0,
245 .accept_source_route
= 0, /* we do not accept RH0 by default. */
248 .suppress_frag_ndisc
= 1,
251 .initialized
= false,
255 /* Check if a valid qdisc is available */
256 static inline bool addrconf_qdisc_ok(const struct net_device
*dev
)
258 return !qdisc_tx_is_noop(dev
);
261 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
263 if (del_timer(&idev
->rs_timer
))
267 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
269 if (cancel_delayed_work(&ifp
->dad_work
))
273 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
276 if (!timer_pending(&idev
->rs_timer
))
278 mod_timer(&idev
->rs_timer
, jiffies
+ when
);
281 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
284 if (!delayed_work_pending(&ifp
->dad_work
))
286 mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
);
289 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
293 idev
->stats
.ipv6
= alloc_percpu(struct ipstats_mib
);
294 if (!idev
->stats
.ipv6
)
297 for_each_possible_cpu(i
) {
298 struct ipstats_mib
*addrconf_stats
;
299 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
300 u64_stats_init(&addrconf_stats
->syncp
);
304 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
306 if (!idev
->stats
.icmpv6dev
)
308 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
310 if (!idev
->stats
.icmpv6msgdev
)
316 kfree(idev
->stats
.icmpv6dev
);
318 free_percpu(idev
->stats
.ipv6
);
323 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
325 struct inet6_dev
*ndev
;
330 if (dev
->mtu
< IPV6_MIN_MTU
)
331 return ERR_PTR(-EINVAL
);
333 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
337 rwlock_init(&ndev
->lock
);
339 INIT_LIST_HEAD(&ndev
->addr_list
);
340 setup_timer(&ndev
->rs_timer
, addrconf_rs_timer
,
341 (unsigned long)ndev
);
342 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
343 ndev
->cnf
.mtu6
= dev
->mtu
;
344 ndev
->cnf
.sysctl
= NULL
;
345 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
346 if (ndev
->nd_parms
== NULL
) {
350 if (ndev
->cnf
.forwarding
)
351 dev_disable_lro(dev
);
352 /* We refer to the device */
355 if (snmp6_alloc_dev(ndev
) < 0) {
357 "%s: cannot allocate memory for statistics; dev=%s.\n",
358 __func__
, dev
->name
);
359 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
365 if (snmp6_register_dev(ndev
) < 0) {
367 "%s: cannot create /proc/net/dev_snmp6/%s\n",
368 __func__
, dev
->name
);
372 /* One reference from device. We must do this before
373 * we invoke __ipv6_regen_rndid().
377 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
378 ndev
->cnf
.accept_dad
= -1;
380 #if IS_ENABLED(CONFIG_IPV6_SIT)
381 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
382 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
383 ndev
->cnf
.rtr_solicits
= 0;
387 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
388 setup_timer(&ndev
->regen_timer
, ipv6_regen_rndid
, (unsigned long)ndev
);
389 if ((dev
->flags
&IFF_LOOPBACK
) ||
390 dev
->type
== ARPHRD_TUNNEL
||
391 dev
->type
== ARPHRD_TUNNEL6
||
392 dev
->type
== ARPHRD_SIT
||
393 dev
->type
== ARPHRD_NONE
) {
394 ndev
->cnf
.use_tempaddr
= -1;
397 ipv6_regen_rndid((unsigned long) ndev
);
400 ndev
->token
= in6addr_any
;
402 if (netif_running(dev
) && addrconf_qdisc_ok(dev
))
403 ndev
->if_flags
|= IF_READY
;
405 ipv6_mc_init_dev(ndev
);
406 ndev
->tstamp
= jiffies
;
407 err
= addrconf_sysctl_register(ndev
);
409 ipv6_mc_destroy_dev(ndev
);
410 del_timer(&ndev
->regen_timer
);
413 /* protected by rtnl_lock */
414 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
416 /* Join interface-local all-node multicast group */
417 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
419 /* Join all-node multicast group */
420 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
422 /* Join all-router multicast group if forwarding is set */
423 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
424 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
429 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
431 in6_dev_finish_destroy(ndev
);
435 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
437 struct inet6_dev
*idev
;
441 idev
= __in6_dev_get(dev
);
443 idev
= ipv6_add_dev(dev
);
448 if (dev
->flags
&IFF_UP
)
453 static int inet6_netconf_msgsize_devconf(int type
)
455 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
456 + nla_total_size(4); /* NETCONFA_IFINDEX */
458 /* type -1 is used for ALL */
459 if (type
== -1 || type
== NETCONFA_FORWARDING
)
460 size
+= nla_total_size(4);
461 #ifdef CONFIG_IPV6_MROUTE
462 if (type
== -1 || type
== NETCONFA_MC_FORWARDING
)
463 size
+= nla_total_size(4);
465 if (type
== -1 || type
== NETCONFA_PROXY_NEIGH
)
466 size
+= nla_total_size(4);
471 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
472 struct ipv6_devconf
*devconf
, u32 portid
,
473 u32 seq
, int event
, unsigned int flags
,
476 struct nlmsghdr
*nlh
;
477 struct netconfmsg
*ncm
;
479 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
484 ncm
= nlmsg_data(nlh
);
485 ncm
->ncm_family
= AF_INET6
;
487 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
488 goto nla_put_failure
;
490 /* type -1 is used for ALL */
491 if ((type
== -1 || type
== NETCONFA_FORWARDING
) &&
492 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
493 goto nla_put_failure
;
494 #ifdef CONFIG_IPV6_MROUTE
495 if ((type
== -1 || type
== NETCONFA_MC_FORWARDING
) &&
496 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
497 devconf
->mc_forwarding
) < 0)
498 goto nla_put_failure
;
500 if ((type
== -1 || type
== NETCONFA_PROXY_NEIGH
) &&
501 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
502 goto nla_put_failure
;
508 nlmsg_cancel(skb
, nlh
);
512 void inet6_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
513 struct ipv6_devconf
*devconf
)
518 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_ATOMIC
);
522 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
523 RTM_NEWNETCONF
, 0, type
);
525 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
526 WARN_ON(err
== -EMSGSIZE
);
530 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_ATOMIC
);
533 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
536 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
537 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
538 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
539 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
542 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
543 struct nlmsghdr
*nlh
)
545 struct net
*net
= sock_net(in_skb
->sk
);
546 struct nlattr
*tb
[NETCONFA_MAX
+1];
547 struct netconfmsg
*ncm
;
549 struct ipv6_devconf
*devconf
;
550 struct inet6_dev
*in6_dev
;
551 struct net_device
*dev
;
555 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
556 devconf_ipv6_policy
);
561 if (!tb
[NETCONFA_IFINDEX
])
564 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
566 case NETCONFA_IFINDEX_ALL
:
567 devconf
= net
->ipv6
.devconf_all
;
569 case NETCONFA_IFINDEX_DEFAULT
:
570 devconf
= net
->ipv6
.devconf_dflt
;
573 dev
= __dev_get_by_index(net
, ifindex
);
576 in6_dev
= __in6_dev_get(dev
);
579 devconf
= &in6_dev
->cnf
;
584 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC
);
588 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
589 NETLINK_CB(in_skb
).portid
,
590 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
593 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
594 WARN_ON(err
== -EMSGSIZE
);
598 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
603 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
604 struct netlink_callback
*cb
)
606 struct net
*net
= sock_net(skb
->sk
);
609 struct net_device
*dev
;
610 struct inet6_dev
*idev
;
611 struct hlist_head
*head
;
614 s_idx
= idx
= cb
->args
[1];
616 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
618 head
= &net
->dev_index_head
[h
];
620 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
622 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
625 idev
= __in6_dev_get(dev
);
629 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
631 NETLINK_CB(cb
->skb
).portid
,
639 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
645 if (h
== NETDEV_HASHENTRIES
) {
646 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
647 net
->ipv6
.devconf_all
,
648 NETLINK_CB(cb
->skb
).portid
,
650 RTM_NEWNETCONF
, NLM_F_MULTI
,
656 if (h
== NETDEV_HASHENTRIES
+ 1) {
657 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
658 net
->ipv6
.devconf_dflt
,
659 NETLINK_CB(cb
->skb
).portid
,
661 RTM_NEWNETCONF
, NLM_F_MULTI
,
675 static void dev_forward_change(struct inet6_dev
*idev
)
677 struct net_device
*dev
;
678 struct inet6_ifaddr
*ifa
;
683 if (idev
->cnf
.forwarding
)
684 dev_disable_lro(dev
);
685 if (dev
->flags
& IFF_MULTICAST
) {
686 if (idev
->cnf
.forwarding
) {
687 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
688 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
689 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
691 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
692 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
693 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
697 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
698 if (ifa
->flags
&IFA_F_TENTATIVE
)
700 if (idev
->cnf
.forwarding
)
701 addrconf_join_anycast(ifa
);
703 addrconf_leave_anycast(ifa
);
705 inet6_netconf_notify_devconf(dev_net(dev
), NETCONFA_FORWARDING
,
706 dev
->ifindex
, &idev
->cnf
);
710 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
712 struct net_device
*dev
;
713 struct inet6_dev
*idev
;
715 for_each_netdev(net
, dev
) {
716 idev
= __in6_dev_get(dev
);
718 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
719 idev
->cnf
.forwarding
= newf
;
721 dev_forward_change(idev
);
726 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
732 return restart_syscall();
734 net
= (struct net
*)table
->extra2
;
738 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
739 if ((!newf
) ^ (!old
))
740 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
741 NETCONFA_IFINDEX_DEFAULT
,
742 net
->ipv6
.devconf_dflt
);
747 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
748 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
749 addrconf_forward_change(net
, newf
);
750 if ((!newf
) ^ (!old
))
751 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
752 NETCONFA_IFINDEX_ALL
,
753 net
->ipv6
.devconf_all
);
754 } else if ((!newf
) ^ (!old
))
755 dev_forward_change((struct inet6_dev
*)table
->extra1
);
759 rt6_purge_dflt_routers(net
);
764 /* Nobody refers to this ifaddr, destroy it */
765 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
767 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
769 #ifdef NET_REFCNT_DEBUG
770 pr_debug("%s\n", __func__
);
773 in6_dev_put(ifp
->idev
);
775 if (cancel_delayed_work(&ifp
->dad_work
))
776 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
779 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
780 pr_warn("Freeing alive inet6 address %p\n", ifp
);
789 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
792 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
795 * Each device address list is sorted in order of scope -
796 * global before linklocal.
798 list_for_each(p
, &idev
->addr_list
) {
799 struct inet6_ifaddr
*ifa
800 = list_entry(p
, struct inet6_ifaddr
, if_list
);
801 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
805 list_add_tail(&ifp
->if_list
, p
);
808 static u32
inet6_addr_hash(const struct in6_addr
*addr
)
810 return hash_32(ipv6_addr_hash(addr
), IN6_ADDR_HSIZE_SHIFT
);
813 /* On success it returns ifp with increased reference count */
815 static struct inet6_ifaddr
*
816 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
817 const struct in6_addr
*peer_addr
, int pfxlen
,
818 int scope
, u32 flags
, u32 valid_lft
, u32 prefered_lft
)
820 struct inet6_ifaddr
*ifa
= NULL
;
824 int addr_type
= ipv6_addr_type(addr
);
826 if (addr_type
== IPV6_ADDR_ANY
||
827 addr_type
& IPV6_ADDR_MULTICAST
||
828 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
829 addr_type
& IPV6_ADDR_LOOPBACK
))
830 return ERR_PTR(-EADDRNOTAVAIL
);
834 err
= -ENODEV
; /*XXX*/
838 if (idev
->cnf
.disable_ipv6
) {
843 spin_lock(&addrconf_hash_lock
);
845 /* Ignore adding duplicate addresses on an interface */
846 if (ipv6_chk_same_addr(dev_net(idev
->dev
), addr
, idev
->dev
)) {
847 ADBG("ipv6_add_addr: already assigned\n");
852 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
855 ADBG("ipv6_add_addr: malloc failed\n");
860 rt
= addrconf_dst_alloc(idev
, addr
, false);
866 neigh_parms_data_state_setall(idev
->nd_parms
);
870 ifa
->peer_addr
= *peer_addr
;
872 spin_lock_init(&ifa
->lock
);
873 spin_lock_init(&ifa
->state_lock
);
874 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
875 INIT_HLIST_NODE(&ifa
->addr_lst
);
877 ifa
->prefix_len
= pfxlen
;
878 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
879 ifa
->valid_lft
= valid_lft
;
880 ifa
->prefered_lft
= prefered_lft
;
881 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
882 ifa
->tokenized
= false;
891 /* Add to big hash table */
892 hash
= inet6_addr_hash(addr
);
894 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
895 spin_unlock(&addrconf_hash_lock
);
897 write_lock(&idev
->lock
);
898 /* Add to inet6_dev unicast addr list. */
899 ipv6_link_dev_addr(idev
, ifa
);
901 if (ifa
->flags
&IFA_F_TEMPORARY
) {
902 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
907 write_unlock(&idev
->lock
);
909 rcu_read_unlock_bh();
911 if (likely(err
== 0))
912 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
920 spin_unlock(&addrconf_hash_lock
);
924 enum cleanup_prefix_rt_t
{
925 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
926 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
927 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
931 * Check, whether the prefix for ifp would still need a prefix route
932 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
935 * 1) we don't purge prefix if address was not permanent.
936 * prefix is managed by its own lifetime.
937 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
938 * 3) if there are no addresses, delete prefix.
939 * 4) if there are still other permanent address(es),
940 * corresponding prefix is still permanent.
941 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
942 * don't purge the prefix, assume user space is managing it.
943 * 6) otherwise, update prefix lifetime to the
944 * longest valid lifetime among the corresponding
945 * addresses on the device.
946 * Note: subsequent RA will update lifetime.
948 static enum cleanup_prefix_rt_t
949 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
951 struct inet6_ifaddr
*ifa
;
952 struct inet6_dev
*idev
= ifp
->idev
;
953 unsigned long lifetime
;
954 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
958 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
961 if (!ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
964 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
965 return CLEANUP_PREFIX_RT_NOP
;
967 action
= CLEANUP_PREFIX_RT_EXPIRE
;
969 spin_lock(&ifa
->lock
);
971 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
973 * Note: Because this address is
974 * not permanent, lifetime <
975 * LONG_MAX / HZ here.
977 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
978 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
979 spin_unlock(&ifa
->lock
);
986 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
, bool del_rt
)
990 rt
= addrconf_get_prefix_route(&ifp
->addr
,
993 0, RTF_GATEWAY
| RTF_DEFAULT
);
998 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
999 rt6_set_expires(rt
, expires
);
1006 /* This function wants to get referenced ifp and releases it before return */
1008 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1011 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1012 unsigned long expires
;
1016 spin_lock_bh(&ifp
->state_lock
);
1018 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1019 spin_unlock_bh(&ifp
->state_lock
);
1021 if (state
== INET6_IFADDR_STATE_DEAD
)
1024 spin_lock_bh(&addrconf_hash_lock
);
1025 hlist_del_init_rcu(&ifp
->addr_lst
);
1026 spin_unlock_bh(&addrconf_hash_lock
);
1028 write_lock_bh(&ifp
->idev
->lock
);
1030 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1031 list_del(&ifp
->tmp_list
);
1033 in6_ifa_put(ifp
->ifpub
);
1039 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1040 action
= check_cleanup_prefix_route(ifp
, &expires
);
1042 list_del_init(&ifp
->if_list
);
1045 write_unlock_bh(&ifp
->idev
->lock
);
1047 addrconf_del_dad_work(ifp
);
1049 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1051 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1053 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1054 cleanup_prefix_route(ifp
, expires
,
1055 action
== CLEANUP_PREFIX_RT_DEL
);
1058 /* clean up prefsrc entries */
1059 rt6_remove_prefsrc(ifp
);
1064 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
1066 struct inet6_dev
*idev
= ifp
->idev
;
1067 struct in6_addr addr
, *tmpaddr
;
1068 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_tstamp
, age
;
1069 unsigned long regen_advance
;
1073 unsigned long now
= jiffies
;
1075 write_lock_bh(&idev
->lock
);
1077 spin_lock_bh(&ift
->lock
);
1078 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1079 spin_unlock_bh(&ift
->lock
);
1086 if (idev
->cnf
.use_tempaddr
<= 0) {
1087 write_unlock_bh(&idev
->lock
);
1088 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1093 spin_lock_bh(&ifp
->lock
);
1094 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1095 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1096 spin_unlock_bh(&ifp
->lock
);
1097 write_unlock_bh(&idev
->lock
);
1098 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1105 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1106 __ipv6_try_regen_rndid(idev
, tmpaddr
);
1107 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1108 age
= (now
- ifp
->tstamp
) / HZ
;
1109 tmp_valid_lft
= min_t(__u32
,
1111 idev
->cnf
.temp_valid_lft
+ age
);
1112 tmp_prefered_lft
= min_t(__u32
,
1114 idev
->cnf
.temp_prefered_lft
+ age
-
1115 idev
->cnf
.max_desync_factor
);
1116 tmp_plen
= ifp
->prefix_len
;
1117 tmp_tstamp
= ifp
->tstamp
;
1118 spin_unlock_bh(&ifp
->lock
);
1120 regen_advance
= idev
->cnf
.regen_max_retry
*
1121 idev
->cnf
.dad_transmits
*
1122 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) / HZ
;
1123 write_unlock_bh(&idev
->lock
);
1125 /* A temporary address is created only if this calculated Preferred
1126 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1127 * an implementation must not create a temporary address with a zero
1128 * Preferred Lifetime.
1129 * Use age calculation as in addrconf_verify to avoid unnecessary
1130 * temporary addresses being generated.
1132 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1133 if (tmp_prefered_lft
<= regen_advance
+ age
) {
1140 addr_flags
= IFA_F_TEMPORARY
;
1141 /* set in addrconf_prefix_rcv() */
1142 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1143 addr_flags
|= IFA_F_OPTIMISTIC
;
1145 ift
= ipv6_add_addr(idev
, &addr
, NULL
, tmp_plen
,
1146 ipv6_addr_scope(&addr
), addr_flags
,
1147 tmp_valid_lft
, tmp_prefered_lft
);
1151 pr_info("%s: retry temporary address regeneration\n", __func__
);
1153 write_lock_bh(&idev
->lock
);
1157 spin_lock_bh(&ift
->lock
);
1160 ift
->tstamp
= tmp_tstamp
;
1161 spin_unlock_bh(&ift
->lock
);
1163 addrconf_dad_start(ift
);
1171 * Choose an appropriate source address (RFC3484)
1174 IPV6_SADDR_RULE_INIT
= 0,
1175 IPV6_SADDR_RULE_LOCAL
,
1176 IPV6_SADDR_RULE_SCOPE
,
1177 IPV6_SADDR_RULE_PREFERRED
,
1178 #ifdef CONFIG_IPV6_MIP6
1179 IPV6_SADDR_RULE_HOA
,
1181 IPV6_SADDR_RULE_OIF
,
1182 IPV6_SADDR_RULE_LABEL
,
1183 IPV6_SADDR_RULE_PRIVACY
,
1184 IPV6_SADDR_RULE_ORCHID
,
1185 IPV6_SADDR_RULE_PREFIX
,
1186 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1187 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1192 struct ipv6_saddr_score
{
1195 struct inet6_ifaddr
*ifa
;
1196 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1201 struct ipv6_saddr_dst
{
1202 const struct in6_addr
*addr
;
1209 static inline int ipv6_saddr_preferred(int type
)
1211 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1216 static inline bool ipv6_use_optimistic_addr(struct inet6_dev
*idev
)
1218 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1219 return idev
&& idev
->cnf
.optimistic_dad
&& idev
->cnf
.use_optimistic
;
1225 static int ipv6_get_saddr_eval(struct net
*net
,
1226 struct ipv6_saddr_score
*score
,
1227 struct ipv6_saddr_dst
*dst
,
1232 if (i
<= score
->rule
) {
1234 case IPV6_SADDR_RULE_SCOPE
:
1235 ret
= score
->scopedist
;
1237 case IPV6_SADDR_RULE_PREFIX
:
1238 ret
= score
->matchlen
;
1241 ret
= !!test_bit(i
, score
->scorebits
);
1247 case IPV6_SADDR_RULE_INIT
:
1248 /* Rule 0: remember if hiscore is not ready yet */
1251 case IPV6_SADDR_RULE_LOCAL
:
1252 /* Rule 1: Prefer same address */
1253 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1255 case IPV6_SADDR_RULE_SCOPE
:
1256 /* Rule 2: Prefer appropriate scope
1261 * ---+--+-+---> scope
1263 * | d is scope of the destination.
1265 * | \ <- smaller scope is better if
1266 * B-15 | \ if scope is enough for destination.
1267 * | ret = B - scope (-1 <= scope >= d <= 15).
1269 * |/ <- greater is better
1270 * -C / if scope is not enough for destination.
1271 * /| ret = scope - C (-1 <= d < scope <= 15).
1273 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1274 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1275 * Assume B = 0 and we get C > 29.
1277 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1278 if (ret
>= dst
->scope
)
1281 ret
-= 128; /* 30 is enough */
1282 score
->scopedist
= ret
;
1284 case IPV6_SADDR_RULE_PREFERRED
:
1286 /* Rule 3: Avoid deprecated and optimistic addresses */
1287 u8 avoid
= IFA_F_DEPRECATED
;
1289 if (!ipv6_use_optimistic_addr(score
->ifa
->idev
))
1290 avoid
|= IFA_F_OPTIMISTIC
;
1291 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1292 !(score
->ifa
->flags
& avoid
);
1295 #ifdef CONFIG_IPV6_MIP6
1296 case IPV6_SADDR_RULE_HOA
:
1298 /* Rule 4: Prefer home address */
1299 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1300 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1304 case IPV6_SADDR_RULE_OIF
:
1305 /* Rule 5: Prefer outgoing interface */
1306 ret
= (!dst
->ifindex
||
1307 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1309 case IPV6_SADDR_RULE_LABEL
:
1310 /* Rule 6: Prefer matching label */
1311 ret
= ipv6_addr_label(net
,
1312 &score
->ifa
->addr
, score
->addr_type
,
1313 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1315 case IPV6_SADDR_RULE_PRIVACY
:
1317 /* Rule 7: Prefer public address
1318 * Note: prefer temporary address if use_tempaddr >= 2
1320 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1321 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1322 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1323 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1326 case IPV6_SADDR_RULE_ORCHID
:
1327 /* Rule 8-: Prefer ORCHID vs ORCHID or
1328 * non-ORCHID vs non-ORCHID
1330 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1331 ipv6_addr_orchid(dst
->addr
));
1333 case IPV6_SADDR_RULE_PREFIX
:
1334 /* Rule 8: Use longest matching prefix */
1335 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1336 if (ret
> score
->ifa
->prefix_len
)
1337 ret
= score
->ifa
->prefix_len
;
1338 score
->matchlen
= ret
;
1340 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1341 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1342 /* Optimistic addresses still have lower precedence than other
1343 * preferred addresses.
1345 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1353 __set_bit(i
, score
->scorebits
);
1359 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1360 const struct in6_addr
*daddr
, unsigned int prefs
,
1361 struct in6_addr
*saddr
)
1363 struct ipv6_saddr_score scores
[2],
1364 *score
= &scores
[0], *hiscore
= &scores
[1];
1365 struct ipv6_saddr_dst dst
;
1366 struct net_device
*dev
;
1369 dst_type
= __ipv6_addr_type(daddr
);
1371 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1372 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1373 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1377 hiscore
->ifa
= NULL
;
1381 for_each_netdev_rcu(net
, dev
) {
1382 struct inet6_dev
*idev
;
1384 /* Candidate Source Address (section 4)
1385 * - multicast and link-local destination address,
1386 * the set of candidate source address MUST only
1387 * include addresses assigned to interfaces
1388 * belonging to the same link as the outgoing
1390 * (- For site-local destination addresses, the
1391 * set of candidate source addresses MUST only
1392 * include addresses assigned to interfaces
1393 * belonging to the same site as the outgoing
1396 if (((dst_type
& IPV6_ADDR_MULTICAST
) ||
1397 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
1398 dst
.ifindex
&& dev
->ifindex
!= dst
.ifindex
)
1401 idev
= __in6_dev_get(dev
);
1405 read_lock_bh(&idev
->lock
);
1406 list_for_each_entry(score
->ifa
, &idev
->addr_list
, if_list
) {
1410 * - Tentative Address (RFC2462 section 5.4)
1411 * - A tentative address is not considered
1412 * "assigned to an interface" in the traditional
1413 * sense, unless it is also flagged as optimistic.
1414 * - Candidate Source Address (section 4)
1415 * - In any case, anycast addresses, multicast
1416 * addresses, and the unspecified address MUST
1417 * NOT be included in a candidate set.
1419 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1420 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1423 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1425 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1426 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1427 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1433 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1435 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1436 int minihiscore
, miniscore
;
1438 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, &dst
, i
);
1439 miniscore
= ipv6_get_saddr_eval(net
, score
, &dst
, i
);
1441 if (minihiscore
> miniscore
) {
1442 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1443 score
->scopedist
> 0) {
1446 * each remaining entry
1447 * has too small (not enough)
1448 * scope, because ifa entries
1449 * are sorted by their scope
1455 } else if (minihiscore
< miniscore
) {
1457 in6_ifa_put(hiscore
->ifa
);
1459 in6_ifa_hold(score
->ifa
);
1461 swap(hiscore
, score
);
1463 /* restore our iterator */
1464 score
->ifa
= hiscore
->ifa
;
1471 read_unlock_bh(&idev
->lock
);
1476 return -EADDRNOTAVAIL
;
1478 *saddr
= hiscore
->ifa
->addr
;
1479 in6_ifa_put(hiscore
->ifa
);
1482 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1484 int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1487 struct inet6_ifaddr
*ifp
;
1488 int err
= -EADDRNOTAVAIL
;
1490 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1491 if (ifp
->scope
> IFA_LINK
)
1493 if (ifp
->scope
== IFA_LINK
&&
1494 !(ifp
->flags
& banned_flags
)) {
1503 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1506 struct inet6_dev
*idev
;
1507 int err
= -EADDRNOTAVAIL
;
1510 idev
= __in6_dev_get(dev
);
1512 read_lock_bh(&idev
->lock
);
1513 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1514 read_unlock_bh(&idev
->lock
);
1520 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1523 struct inet6_ifaddr
*ifp
;
1525 read_lock_bh(&idev
->lock
);
1526 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
)
1528 read_unlock_bh(&idev
->lock
);
1532 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1533 const struct net_device
*dev
, int strict
)
1535 return ipv6_chk_addr_and_flags(net
, addr
, dev
, strict
, IFA_F_TENTATIVE
);
1537 EXPORT_SYMBOL(ipv6_chk_addr
);
1539 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1540 const struct net_device
*dev
, int strict
,
1543 struct inet6_ifaddr
*ifp
;
1544 unsigned int hash
= inet6_addr_hash(addr
);
1548 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1549 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1551 /* Decouple optimistic from tentative for evaluation here.
1552 * Ban optimistic addresses explicitly, when required.
1554 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1555 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1557 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1558 !(ifp_flags
&banned_flags
) &&
1559 (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1560 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1561 rcu_read_unlock_bh();
1566 rcu_read_unlock_bh();
1569 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1571 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1572 struct net_device
*dev
)
1574 unsigned int hash
= inet6_addr_hash(addr
);
1575 struct inet6_ifaddr
*ifp
;
1577 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1578 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1580 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1581 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1588 /* Compares an address/prefix_len with addresses on device @dev.
1589 * If one is found it returns true.
1591 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1592 const unsigned int prefix_len
, struct net_device
*dev
)
1594 struct inet6_dev
*idev
;
1595 struct inet6_ifaddr
*ifa
;
1599 idev
= __in6_dev_get(dev
);
1601 read_lock_bh(&idev
->lock
);
1602 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1603 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
1607 read_unlock_bh(&idev
->lock
);
1613 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
1615 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1617 struct inet6_dev
*idev
;
1618 struct inet6_ifaddr
*ifa
;
1623 idev
= __in6_dev_get(dev
);
1625 read_lock_bh(&idev
->lock
);
1626 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1627 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1632 read_unlock_bh(&idev
->lock
);
1637 EXPORT_SYMBOL(ipv6_chk_prefix
);
1639 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1640 struct net_device
*dev
, int strict
)
1642 struct inet6_ifaddr
*ifp
, *result
= NULL
;
1643 unsigned int hash
= inet6_addr_hash(addr
);
1646 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1647 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1649 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1650 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1651 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1658 rcu_read_unlock_bh();
1663 /* Gets referenced address, destroys ifaddr */
1665 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
1667 if (ifp
->flags
&IFA_F_PERMANENT
) {
1668 spin_lock_bh(&ifp
->lock
);
1669 addrconf_del_dad_work(ifp
);
1670 ifp
->flags
|= IFA_F_TENTATIVE
;
1672 ifp
->flags
|= IFA_F_DADFAILED
;
1673 spin_unlock_bh(&ifp
->lock
);
1675 ipv6_ifa_notify(0, ifp
);
1677 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1678 struct inet6_ifaddr
*ifpub
;
1679 spin_lock_bh(&ifp
->lock
);
1682 in6_ifa_hold(ifpub
);
1683 spin_unlock_bh(&ifp
->lock
);
1684 ipv6_create_tempaddr(ifpub
, ifp
);
1687 spin_unlock_bh(&ifp
->lock
);
1695 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
1699 spin_lock_bh(&ifp
->state_lock
);
1700 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
1701 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
1704 spin_unlock_bh(&ifp
->state_lock
);
1709 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1711 struct inet6_dev
*idev
= ifp
->idev
;
1713 if (addrconf_dad_end(ifp
)) {
1718 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1719 ifp
->idev
->dev
->name
, &ifp
->addr
);
1721 if (idev
->cnf
.accept_dad
> 1 && !idev
->cnf
.disable_ipv6
) {
1722 struct in6_addr addr
;
1724 addr
.s6_addr32
[0] = htonl(0xfe800000);
1725 addr
.s6_addr32
[1] = 0;
1727 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
1728 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
1729 /* DAD failed for link-local based on MAC address */
1730 idev
->cnf
.disable_ipv6
= 1;
1732 pr_info("%s: IPv6 being disabled!\n",
1733 ifp
->idev
->dev
->name
);
1737 spin_lock_bh(&ifp
->state_lock
);
1738 /* transition from _POSTDAD to _ERRDAD */
1739 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
1740 spin_unlock_bh(&ifp
->state_lock
);
1742 addrconf_mod_dad_work(ifp
, 0);
1745 /* Join to solicited addr multicast group.
1746 * caller must hold RTNL */
1747 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
1749 struct in6_addr maddr
;
1751 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1754 addrconf_addr_solict_mult(addr
, &maddr
);
1755 ipv6_dev_mc_inc(dev
, &maddr
);
1758 /* caller must hold RTNL */
1759 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
1761 struct in6_addr maddr
;
1763 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1766 addrconf_addr_solict_mult(addr
, &maddr
);
1767 __ipv6_dev_mc_dec(idev
, &maddr
);
1770 /* caller must hold RTNL */
1771 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1773 struct in6_addr addr
;
1775 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1777 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1778 if (ipv6_addr_any(&addr
))
1780 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
1783 /* caller must hold RTNL */
1784 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1786 struct in6_addr addr
;
1788 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1790 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1791 if (ipv6_addr_any(&addr
))
1793 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1796 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1798 if (dev
->addr_len
!= ETH_ALEN
)
1800 memcpy(eui
, dev
->dev_addr
, 3);
1801 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1804 * The zSeries OSA network cards can be shared among various
1805 * OS instances, but the OSA cards have only one MAC address.
1806 * This leads to duplicate address conflicts in conjunction
1807 * with IPv6 if more than one instance uses the same card.
1809 * The driver for these cards can deliver a unique 16-bit
1810 * identifier for each instance sharing the same card. It is
1811 * placed instead of 0xFFFE in the interface identifier. The
1812 * "u" bit of the interface identifier is not inverted in this
1813 * case. Hence the resulting interface identifier has local
1814 * scope according to RFC2373.
1817 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1818 eui
[4] = dev
->dev_id
& 0xFF;
1827 static int addrconf_ifid_eui64(u8
*eui
, struct net_device
*dev
)
1829 if (dev
->addr_len
!= IEEE802154_ADDR_LEN
)
1831 memcpy(eui
, dev
->dev_addr
, 8);
1836 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
1838 union fwnet_hwaddr
*ha
;
1840 if (dev
->addr_len
!= FWNET_ALEN
)
1843 ha
= (union fwnet_hwaddr
*)dev
->dev_addr
;
1845 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
1850 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1852 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1853 if (dev
->addr_len
!= ARCNET_ALEN
)
1856 eui
[7] = *(u8
*)dev
->dev_addr
;
1860 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1862 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1864 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1869 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
1873 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
1874 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
1875 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
1876 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
1877 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
1878 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
1882 memcpy(eui
+ 4, &addr
, 4);
1886 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
1888 if (dev
->priv_flags
& IFF_ISATAP
)
1889 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1893 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
1895 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1898 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
1900 memcpy(eui
, dev
->perm_addr
, 3);
1901 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
1908 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1910 switch (dev
->type
) {
1913 return addrconf_ifid_eui48(eui
, dev
);
1915 return addrconf_ifid_arcnet(eui
, dev
);
1916 case ARPHRD_INFINIBAND
:
1917 return addrconf_ifid_infiniband(eui
, dev
);
1919 return addrconf_ifid_sit(eui
, dev
);
1921 return addrconf_ifid_gre(eui
, dev
);
1922 case ARPHRD_6LOWPAN
:
1923 case ARPHRD_IEEE802154
:
1924 return addrconf_ifid_eui64(eui
, dev
);
1925 case ARPHRD_IEEE1394
:
1926 return addrconf_ifid_ieee1394(eui
, dev
);
1927 case ARPHRD_TUNNEL6
:
1928 return addrconf_ifid_ip6tnl(eui
, dev
);
1933 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1936 struct inet6_ifaddr
*ifp
;
1938 read_lock_bh(&idev
->lock
);
1939 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1940 if (ifp
->scope
> IFA_LINK
)
1942 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1943 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1948 read_unlock_bh(&idev
->lock
);
1952 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1953 static void __ipv6_regen_rndid(struct inet6_dev
*idev
)
1956 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1957 idev
->rndid
[0] &= ~0x02;
1960 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1961 * check if generated address is not inappropriate
1963 * - Reserved subnet anycast (RFC 2526)
1964 * 11111101 11....11 1xxxxxxx
1965 * - ISATAP (RFC4214) 6.1
1966 * 00-00-5E-FE-xx-xx-xx-xx
1968 * - XXX: already assigned to an address on the device
1970 if (idev
->rndid
[0] == 0xfd &&
1971 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1972 (idev
->rndid
[7]&0x80))
1974 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1975 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1977 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1982 static void ipv6_regen_rndid(unsigned long data
)
1984 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1985 unsigned long expires
;
1988 write_lock_bh(&idev
->lock
);
1993 __ipv6_regen_rndid(idev
);
1996 idev
->cnf
.temp_prefered_lft
* HZ
-
1997 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
*
1998 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) -
1999 idev
->cnf
.max_desync_factor
* HZ
;
2000 if (time_before(expires
, jiffies
)) {
2001 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2002 __func__
, idev
->dev
->name
);
2006 if (!mod_timer(&idev
->regen_timer
, expires
))
2010 write_unlock_bh(&idev
->lock
);
2011 rcu_read_unlock_bh();
2015 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
2017 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
2018 __ipv6_regen_rndid(idev
);
2026 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
2027 unsigned long expires
, u32 flags
)
2029 struct fib6_config cfg
= {
2030 .fc_table
= RT6_TABLE_PREFIX
,
2031 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2032 .fc_ifindex
= dev
->ifindex
,
2033 .fc_expires
= expires
,
2035 .fc_flags
= RTF_UP
| flags
,
2036 .fc_nlinfo
.nl_net
= dev_net(dev
),
2037 .fc_protocol
= RTPROT_KERNEL
,
2042 /* Prevent useless cloning on PtP SIT.
2043 This thing is done here expecting that the whole
2044 class of non-broadcast devices need not cloning.
2046 #if IS_ENABLED(CONFIG_IPV6_SIT)
2047 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2048 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2051 ip6_route_add(&cfg
);
2055 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2057 const struct net_device
*dev
,
2058 u32 flags
, u32 noflags
)
2060 struct fib6_node
*fn
;
2061 struct rt6_info
*rt
= NULL
;
2062 struct fib6_table
*table
;
2064 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_PREFIX
);
2068 read_lock_bh(&table
->tb6_lock
);
2069 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0);
2072 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
2073 if (rt
->dst
.dev
->ifindex
!= dev
->ifindex
)
2075 if ((rt
->rt6i_flags
& flags
) != flags
)
2077 if ((rt
->rt6i_flags
& noflags
) != 0)
2083 read_unlock_bh(&table
->tb6_lock
);
2088 /* Create "default" multicast route to the interface */
2090 static void addrconf_add_mroute(struct net_device
*dev
)
2092 struct fib6_config cfg
= {
2093 .fc_table
= RT6_TABLE_LOCAL
,
2094 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2095 .fc_ifindex
= dev
->ifindex
,
2098 .fc_nlinfo
.nl_net
= dev_net(dev
),
2101 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2103 ip6_route_add(&cfg
);
2106 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2108 struct inet6_dev
*idev
;
2112 idev
= ipv6_find_idev(dev
);
2114 return ERR_PTR(-ENOBUFS
);
2116 if (idev
->cnf
.disable_ipv6
)
2117 return ERR_PTR(-EACCES
);
2119 /* Add default multicast route */
2120 if (!(dev
->flags
& IFF_LOOPBACK
))
2121 addrconf_add_mroute(dev
);
2126 static void manage_tempaddrs(struct inet6_dev
*idev
,
2127 struct inet6_ifaddr
*ifp
,
2128 __u32 valid_lft
, __u32 prefered_lft
,
2129 bool create
, unsigned long now
)
2132 struct inet6_ifaddr
*ift
;
2134 read_lock_bh(&idev
->lock
);
2135 /* update all temporary addresses in the list */
2136 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2137 int age
, max_valid
, max_prefered
;
2139 if (ifp
!= ift
->ifpub
)
2142 /* RFC 4941 section 3.3:
2143 * If a received option will extend the lifetime of a public
2144 * address, the lifetimes of temporary addresses should
2145 * be extended, subject to the overall constraint that no
2146 * temporary addresses should ever remain "valid" or "preferred"
2147 * for a time longer than (TEMP_VALID_LIFETIME) or
2148 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2150 age
= (now
- ift
->cstamp
) / HZ
;
2151 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2155 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2156 idev
->cnf
.max_desync_factor
- age
;
2157 if (max_prefered
< 0)
2160 if (valid_lft
> max_valid
)
2161 valid_lft
= max_valid
;
2163 if (prefered_lft
> max_prefered
)
2164 prefered_lft
= max_prefered
;
2166 spin_lock(&ift
->lock
);
2168 ift
->valid_lft
= valid_lft
;
2169 ift
->prefered_lft
= prefered_lft
;
2171 if (prefered_lft
> 0)
2172 ift
->flags
&= ~IFA_F_DEPRECATED
;
2174 spin_unlock(&ift
->lock
);
2175 if (!(flags
&IFA_F_TENTATIVE
))
2176 ipv6_ifa_notify(0, ift
);
2179 if ((create
|| list_empty(&idev
->tempaddr_list
)) &&
2180 idev
->cnf
.use_tempaddr
> 0) {
2181 /* When a new public address is created as described
2182 * in [ADDRCONF], also create a new temporary address.
2183 * Also create a temporary address if it's enabled but
2184 * no temporary address currently exists.
2186 read_unlock_bh(&idev
->lock
);
2187 ipv6_create_tempaddr(ifp
, NULL
);
2189 read_unlock_bh(&idev
->lock
);
2193 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2195 struct prefix_info
*pinfo
;
2199 struct inet6_dev
*in6_dev
;
2200 struct net
*net
= dev_net(dev
);
2202 pinfo
= (struct prefix_info
*) opt
;
2204 if (len
< sizeof(struct prefix_info
)) {
2205 ADBG("addrconf: prefix option too short\n");
2210 * Validation checks ([ADDRCONF], page 19)
2213 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2215 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2218 valid_lft
= ntohl(pinfo
->valid
);
2219 prefered_lft
= ntohl(pinfo
->prefered
);
2221 if (prefered_lft
> valid_lft
) {
2222 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2226 in6_dev
= in6_dev_get(dev
);
2228 if (in6_dev
== NULL
) {
2229 net_dbg_ratelimited("addrconf: device %s not configured\n",
2235 * Two things going on here:
2236 * 1) Add routes for on-link prefixes
2237 * 2) Configure prefixes with the auto flag set
2240 if (pinfo
->onlink
) {
2241 struct rt6_info
*rt
;
2242 unsigned long rt_expires
;
2244 /* Avoid arithmetic overflow. Really, we could
2245 * save rt_expires in seconds, likely valid_lft,
2246 * but it would require division in fib gc, that it
2250 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2252 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2254 if (addrconf_finite_timeout(rt_expires
))
2257 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2260 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2261 RTF_GATEWAY
| RTF_DEFAULT
);
2264 /* Autoconf prefix route */
2265 if (valid_lft
== 0) {
2268 } else if (addrconf_finite_timeout(rt_expires
)) {
2270 rt6_set_expires(rt
, jiffies
+ rt_expires
);
2272 rt6_clean_expires(rt
);
2274 } else if (valid_lft
) {
2275 clock_t expires
= 0;
2276 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2277 if (addrconf_finite_timeout(rt_expires
)) {
2279 flags
|= RTF_EXPIRES
;
2280 expires
= jiffies_to_clock_t(rt_expires
);
2282 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2283 dev
, expires
, flags
);
2288 /* Try to figure out our local address for this prefix */
2290 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2291 struct inet6_ifaddr
*ifp
;
2292 struct in6_addr addr
;
2293 int create
= 0, update_lft
= 0;
2294 bool tokenized
= false;
2296 if (pinfo
->prefix_len
== 64) {
2297 memcpy(&addr
, &pinfo
->prefix
, 8);
2299 if (!ipv6_addr_any(&in6_dev
->token
)) {
2300 read_lock_bh(&in6_dev
->lock
);
2301 memcpy(addr
.s6_addr
+ 8,
2302 in6_dev
->token
.s6_addr
+ 8, 8);
2303 read_unlock_bh(&in6_dev
->lock
);
2305 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2306 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2307 in6_dev_put(in6_dev
);
2312 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2314 in6_dev_put(in6_dev
);
2319 ifp
= ipv6_get_ifaddr(net
, &addr
, dev
, 1);
2321 if (ifp
== NULL
&& valid_lft
) {
2322 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2325 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2326 if (in6_dev
->cnf
.optimistic_dad
&&
2327 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2328 addr_flags
= IFA_F_OPTIMISTIC
;
2331 /* Do not allow to create too much of autoconfigured
2332 * addresses; this would be too easy way to crash kernel.
2334 if (!max_addresses
||
2335 ipv6_count_addresses(in6_dev
) < max_addresses
)
2336 ifp
= ipv6_add_addr(in6_dev
, &addr
, NULL
,
2338 addr_type
&IPV6_ADDR_SCOPE_MASK
,
2339 addr_flags
, valid_lft
,
2342 if (IS_ERR_OR_NULL(ifp
)) {
2343 in6_dev_put(in6_dev
);
2349 spin_lock_bh(&ifp
->lock
);
2350 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2351 ifp
->cstamp
= jiffies
;
2352 ifp
->tokenized
= tokenized
;
2353 spin_unlock_bh(&ifp
->lock
);
2354 addrconf_dad_start(ifp
);
2362 /* update lifetime (RFC2462 5.5.3 e) */
2363 spin_lock(&ifp
->lock
);
2365 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2366 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2369 if (!update_lft
&& !create
&& stored_lft
) {
2370 const u32 minimum_lft
= min_t(u32
,
2371 stored_lft
, MIN_VALID_LIFETIME
);
2372 valid_lft
= max(valid_lft
, minimum_lft
);
2374 /* RFC4862 Section 5.5.3e:
2375 * "Note that the preferred lifetime of the
2376 * corresponding address is always reset to
2377 * the Preferred Lifetime in the received
2378 * Prefix Information option, regardless of
2379 * whether the valid lifetime is also reset or
2382 * So we should always update prefered_lft here.
2388 ifp
->valid_lft
= valid_lft
;
2389 ifp
->prefered_lft
= prefered_lft
;
2392 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2393 spin_unlock(&ifp
->lock
);
2395 if (!(flags
&IFA_F_TENTATIVE
))
2396 ipv6_ifa_notify(0, ifp
);
2398 spin_unlock(&ifp
->lock
);
2400 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2407 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2408 in6_dev_put(in6_dev
);
2412 * Set destination address.
2413 * Special case for SIT interfaces where we create a new "virtual"
2416 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2418 struct in6_ifreq ireq
;
2419 struct net_device
*dev
;
2425 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2428 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2434 #if IS_ENABLED(CONFIG_IPV6_SIT)
2435 if (dev
->type
== ARPHRD_SIT
) {
2436 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2438 struct ip_tunnel_parm p
;
2440 err
= -EADDRNOTAVAIL
;
2441 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2444 memset(&p
, 0, sizeof(p
));
2445 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2449 p
.iph
.protocol
= IPPROTO_IPV6
;
2451 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2453 if (ops
->ndo_do_ioctl
) {
2454 mm_segment_t oldfs
= get_fs();
2457 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2464 dev
= __dev_get_by_name(net
, p
.name
);
2467 err
= dev_open(dev
);
2477 static int ipv6_mc_config(struct sock
*sk
, bool join
,
2478 const struct in6_addr
*addr
, int ifindex
)
2486 ret
= ipv6_sock_mc_join(sk
, ifindex
, addr
);
2488 ret
= ipv6_sock_mc_drop(sk
, ifindex
, addr
);
2495 * Manual configuration of address on an interface
2497 static int inet6_addr_add(struct net
*net
, int ifindex
,
2498 const struct in6_addr
*pfx
,
2499 const struct in6_addr
*peer_pfx
,
2500 unsigned int plen
, __u32 ifa_flags
,
2501 __u32 prefered_lft
, __u32 valid_lft
)
2503 struct inet6_ifaddr
*ifp
;
2504 struct inet6_dev
*idev
;
2505 struct net_device
*dev
;
2506 unsigned long timeout
;
2516 /* check the lifetime */
2517 if (!valid_lft
|| prefered_lft
> valid_lft
)
2520 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&& plen
!= 64)
2523 dev
= __dev_get_by_index(net
, ifindex
);
2527 idev
= addrconf_add_dev(dev
);
2529 return PTR_ERR(idev
);
2531 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2532 int ret
= ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2533 true, pfx
, ifindex
);
2539 scope
= ipv6_addr_scope(pfx
);
2541 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
2542 if (addrconf_finite_timeout(timeout
)) {
2543 expires
= jiffies_to_clock_t(timeout
* HZ
);
2544 valid_lft
= timeout
;
2545 flags
= RTF_EXPIRES
;
2549 ifa_flags
|= IFA_F_PERMANENT
;
2552 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
2553 if (addrconf_finite_timeout(timeout
)) {
2555 ifa_flags
|= IFA_F_DEPRECATED
;
2556 prefered_lft
= timeout
;
2559 ifp
= ipv6_add_addr(idev
, pfx
, peer_pfx
, plen
, scope
, ifa_flags
,
2560 valid_lft
, prefered_lft
);
2563 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
2564 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
2569 * Note that section 3.1 of RFC 4429 indicates
2570 * that the Optimistic flag should not be set for
2571 * manually configured addresses
2573 addrconf_dad_start(ifp
);
2574 if (ifa_flags
& IFA_F_MANAGETEMPADDR
)
2575 manage_tempaddrs(idev
, ifp
, valid_lft
, prefered_lft
,
2578 addrconf_verify_rtnl();
2580 } else if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2581 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2582 false, pfx
, ifindex
);
2585 return PTR_ERR(ifp
);
2588 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
2589 const struct in6_addr
*pfx
, unsigned int plen
)
2591 struct inet6_ifaddr
*ifp
;
2592 struct inet6_dev
*idev
;
2593 struct net_device
*dev
;
2598 dev
= __dev_get_by_index(net
, ifindex
);
2602 idev
= __in6_dev_get(dev
);
2606 read_lock_bh(&idev
->lock
);
2607 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
2608 if (ifp
->prefix_len
== plen
&&
2609 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
2611 read_unlock_bh(&idev
->lock
);
2613 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
2614 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
2615 manage_tempaddrs(idev
, ifp
, 0, 0, false,
2618 addrconf_verify_rtnl();
2619 if (ipv6_addr_is_multicast(pfx
)) {
2620 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2621 false, pfx
, dev
->ifindex
);
2626 read_unlock_bh(&idev
->lock
);
2627 return -EADDRNOTAVAIL
;
2631 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
2633 struct in6_ifreq ireq
;
2636 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2639 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2643 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, NULL
,
2644 ireq
.ifr6_prefixlen
, IFA_F_PERMANENT
,
2645 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2650 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
2652 struct in6_ifreq ireq
;
2655 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2658 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2662 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
2663 ireq
.ifr6_prefixlen
);
2668 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
2669 int plen
, int scope
)
2671 struct inet6_ifaddr
*ifp
;
2673 ifp
= ipv6_add_addr(idev
, addr
, NULL
, plen
,
2674 scope
, IFA_F_PERMANENT
,
2675 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2677 spin_lock_bh(&ifp
->lock
);
2678 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2679 spin_unlock_bh(&ifp
->lock
);
2680 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2685 #if IS_ENABLED(CONFIG_IPV6_SIT)
2686 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2688 struct in6_addr addr
;
2689 struct net_device
*dev
;
2690 struct net
*net
= dev_net(idev
->dev
);
2696 memset(&addr
, 0, sizeof(struct in6_addr
));
2697 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2699 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2700 addr
.s6_addr32
[0] = htonl(0xfe800000);
2704 scope
= IPV6_ADDR_COMPATv4
;
2706 pflags
|= RTF_NONEXTHOP
;
2709 if (addr
.s6_addr32
[3]) {
2710 add_addr(idev
, &addr
, plen
, scope
);
2711 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0, pflags
);
2715 for_each_netdev(net
, dev
) {
2716 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
2717 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2718 struct in_ifaddr
*ifa
;
2722 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2724 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2726 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2728 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2729 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2734 add_addr(idev
, &addr
, plen
, flag
);
2735 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0,
2743 static void init_loopback(struct net_device
*dev
)
2745 struct inet6_dev
*idev
;
2746 struct net_device
*sp_dev
;
2747 struct inet6_ifaddr
*sp_ifa
;
2748 struct rt6_info
*sp_rt
;
2754 idev
= ipv6_find_idev(dev
);
2756 pr_debug("%s: add_dev failed\n", __func__
);
2760 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
2762 /* Add routes to other interface's IPv6 addresses */
2763 for_each_netdev(dev_net(dev
), sp_dev
) {
2764 if (!strcmp(sp_dev
->name
, dev
->name
))
2767 idev
= __in6_dev_get(sp_dev
);
2771 read_lock_bh(&idev
->lock
);
2772 list_for_each_entry(sp_ifa
, &idev
->addr_list
, if_list
) {
2774 if (sp_ifa
->flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))
2778 /* This dst has been added to garbage list when
2779 * lo device down, release this obsolete dst and
2780 * reallocate a new router for ifa.
2782 if (sp_ifa
->rt
->dst
.obsolete
> 0) {
2783 ip6_rt_put(sp_ifa
->rt
);
2790 sp_rt
= addrconf_dst_alloc(idev
, &sp_ifa
->addr
, false);
2792 /* Failure cases are ignored */
2793 if (!IS_ERR(sp_rt
)) {
2798 read_unlock_bh(&idev
->lock
);
2802 static void addrconf_add_linklocal(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2804 struct inet6_ifaddr
*ifp
;
2805 u32 addr_flags
= IFA_F_PERMANENT
;
2807 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2808 if (idev
->cnf
.optimistic_dad
&&
2809 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
2810 addr_flags
|= IFA_F_OPTIMISTIC
;
2814 ifp
= ipv6_add_addr(idev
, addr
, NULL
, 64, IFA_LINK
, addr_flags
,
2815 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2817 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
2818 addrconf_dad_start(ifp
);
2823 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
2825 if (idev
->addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
) {
2826 struct in6_addr addr
;
2828 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
2829 /* addrconf_add_linklocal also adds a prefix_route and we
2830 * only need to care about prefix routes if ipv6_generate_eui64
2831 * couldn't generate one.
2833 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
2834 addrconf_add_linklocal(idev
, &addr
);
2835 else if (prefix_route
)
2836 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
2840 static void addrconf_dev_config(struct net_device
*dev
)
2842 struct inet6_dev
*idev
;
2846 if ((dev
->type
!= ARPHRD_ETHER
) &&
2847 (dev
->type
!= ARPHRD_FDDI
) &&
2848 (dev
->type
!= ARPHRD_ARCNET
) &&
2849 (dev
->type
!= ARPHRD_INFINIBAND
) &&
2850 (dev
->type
!= ARPHRD_IEEE802154
) &&
2851 (dev
->type
!= ARPHRD_IEEE1394
) &&
2852 (dev
->type
!= ARPHRD_TUNNEL6
) &&
2853 (dev
->type
!= ARPHRD_6LOWPAN
)) {
2854 /* Alas, we support only Ethernet autoconfiguration. */
2858 idev
= addrconf_add_dev(dev
);
2862 addrconf_addr_gen(idev
, false);
2865 #if IS_ENABLED(CONFIG_IPV6_SIT)
2866 static void addrconf_sit_config(struct net_device
*dev
)
2868 struct inet6_dev
*idev
;
2873 * Configure the tunnel with one of our IPv4
2874 * addresses... we should configure all of
2875 * our v4 addrs in the tunnel
2878 idev
= ipv6_find_idev(dev
);
2880 pr_debug("%s: add_dev failed\n", __func__
);
2884 if (dev
->priv_flags
& IFF_ISATAP
) {
2885 addrconf_addr_gen(idev
, false);
2889 sit_add_v4_addrs(idev
);
2891 if (dev
->flags
&IFF_POINTOPOINT
)
2892 addrconf_add_mroute(dev
);
2896 #if IS_ENABLED(CONFIG_NET_IPGRE)
2897 static void addrconf_gre_config(struct net_device
*dev
)
2899 struct inet6_dev
*idev
;
2903 idev
= ipv6_find_idev(dev
);
2905 pr_debug("%s: add_dev failed\n", __func__
);
2909 addrconf_addr_gen(idev
, true);
2913 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2916 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2917 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2918 int run_pending
= 0;
2922 case NETDEV_REGISTER
:
2923 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2924 idev
= ipv6_add_dev(dev
);
2926 return notifier_from_errno(PTR_ERR(idev
));
2932 if (dev
->flags
& IFF_SLAVE
)
2935 if (idev
&& idev
->cnf
.disable_ipv6
)
2938 if (event
== NETDEV_UP
) {
2939 if (!addrconf_qdisc_ok(dev
)) {
2940 /* device is not ready yet. */
2941 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2946 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
2947 idev
= ipv6_add_dev(dev
);
2949 if (!IS_ERR_OR_NULL(idev
)) {
2950 idev
->if_flags
|= IF_READY
;
2954 if (!addrconf_qdisc_ok(dev
)) {
2955 /* device is still not ready. */
2960 if (idev
->if_flags
& IF_READY
)
2961 /* device is already configured. */
2963 idev
->if_flags
|= IF_READY
;
2966 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2972 switch (dev
->type
) {
2973 #if IS_ENABLED(CONFIG_IPV6_SIT)
2975 addrconf_sit_config(dev
);
2978 #if IS_ENABLED(CONFIG_NET_IPGRE)
2980 addrconf_gre_config(dev
);
2983 case ARPHRD_LOOPBACK
:
2988 addrconf_dev_config(dev
);
2992 if (!IS_ERR_OR_NULL(idev
)) {
2994 addrconf_dad_run(idev
);
2997 * If the MTU changed during the interface down,
2998 * when the interface up, the changed MTU must be
2999 * reflected in the idev as well as routers.
3001 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
3002 dev
->mtu
>= IPV6_MIN_MTU
) {
3003 rt6_mtu_change(dev
, dev
->mtu
);
3004 idev
->cnf
.mtu6
= dev
->mtu
;
3006 idev
->tstamp
= jiffies
;
3007 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
3010 * If the changed mtu during down is lower than
3011 * IPV6_MIN_MTU stop IPv6 on this interface.
3013 if (dev
->mtu
< IPV6_MIN_MTU
)
3014 addrconf_ifdown(dev
, 1);
3018 case NETDEV_CHANGEMTU
:
3019 if (idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3020 rt6_mtu_change(dev
, dev
->mtu
);
3021 idev
->cnf
.mtu6
= dev
->mtu
;
3025 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3026 idev
= ipv6_add_dev(dev
);
3032 * if MTU under IPV6_MIN_MTU.
3033 * Stop IPv6 on this interface.
3037 case NETDEV_UNREGISTER
:
3039 * Remove all addresses from this interface.
3041 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3044 case NETDEV_CHANGENAME
:
3046 snmp6_unregister_dev(idev
);
3047 addrconf_sysctl_unregister(idev
);
3048 err
= addrconf_sysctl_register(idev
);
3050 return notifier_from_errno(err
);
3051 err
= snmp6_register_dev(idev
);
3053 addrconf_sysctl_unregister(idev
);
3054 return notifier_from_errno(err
);
3059 case NETDEV_PRE_TYPE_CHANGE
:
3060 case NETDEV_POST_TYPE_CHANGE
:
3061 addrconf_type_change(dev
, event
);
3069 * addrconf module should be notified of a device going up
3071 static struct notifier_block ipv6_dev_notf
= {
3072 .notifier_call
= addrconf_notify
,
3075 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3077 struct inet6_dev
*idev
;
3080 idev
= __in6_dev_get(dev
);
3082 if (event
== NETDEV_POST_TYPE_CHANGE
)
3083 ipv6_mc_remap(idev
);
3084 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3085 ipv6_mc_unmap(idev
);
3088 static int addrconf_ifdown(struct net_device
*dev
, int how
)
3090 struct net
*net
= dev_net(dev
);
3091 struct inet6_dev
*idev
;
3092 struct inet6_ifaddr
*ifa
;
3097 rt6_ifdown(net
, dev
);
3098 neigh_ifdown(&nd_tbl
, dev
);
3100 idev
= __in6_dev_get(dev
);
3105 * Step 1: remove reference to ipv6 device from parent device.
3111 /* protected by rtnl_lock */
3112 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3114 /* Step 1.5: remove snmp6 entry */
3115 snmp6_unregister_dev(idev
);
3119 /* Step 2: clear hash table */
3120 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3121 struct hlist_head
*h
= &inet6_addr_lst
[i
];
3123 spin_lock_bh(&addrconf_hash_lock
);
3125 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3126 if (ifa
->idev
== idev
) {
3127 hlist_del_init_rcu(&ifa
->addr_lst
);
3128 addrconf_del_dad_work(ifa
);
3132 spin_unlock_bh(&addrconf_hash_lock
);
3135 write_lock_bh(&idev
->lock
);
3137 addrconf_del_rs_timer(idev
);
3139 /* Step 2: clear flags for stateless addrconf */
3141 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3143 if (how
&& del_timer(&idev
->regen_timer
))
3146 /* Step 3: clear tempaddr list */
3147 while (!list_empty(&idev
->tempaddr_list
)) {
3148 ifa
= list_first_entry(&idev
->tempaddr_list
,
3149 struct inet6_ifaddr
, tmp_list
);
3150 list_del(&ifa
->tmp_list
);
3151 write_unlock_bh(&idev
->lock
);
3152 spin_lock_bh(&ifa
->lock
);
3155 in6_ifa_put(ifa
->ifpub
);
3158 spin_unlock_bh(&ifa
->lock
);
3160 write_lock_bh(&idev
->lock
);
3163 while (!list_empty(&idev
->addr_list
)) {
3164 ifa
= list_first_entry(&idev
->addr_list
,
3165 struct inet6_ifaddr
, if_list
);
3166 addrconf_del_dad_work(ifa
);
3168 list_del(&ifa
->if_list
);
3170 write_unlock_bh(&idev
->lock
);
3172 spin_lock_bh(&ifa
->state_lock
);
3174 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3175 spin_unlock_bh(&ifa
->state_lock
);
3177 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3178 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3179 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3183 write_lock_bh(&idev
->lock
);
3186 write_unlock_bh(&idev
->lock
);
3188 /* Step 5: Discard anycast and multicast list */
3190 ipv6_ac_destroy_dev(idev
);
3191 ipv6_mc_destroy_dev(idev
);
3196 idev
->tstamp
= jiffies
;
3198 /* Last: Shot the device (if unregistered) */
3200 addrconf_sysctl_unregister(idev
);
3201 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3202 neigh_ifdown(&nd_tbl
, dev
);
3208 static void addrconf_rs_timer(unsigned long data
)
3210 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
3211 struct net_device
*dev
= idev
->dev
;
3212 struct in6_addr lladdr
;
3214 write_lock(&idev
->lock
);
3215 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3218 if (!ipv6_accept_ra(idev
))
3221 /* Announcement received after solicitation was sent */
3222 if (idev
->if_flags
& IF_RA_RCVD
)
3225 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
) {
3226 write_unlock(&idev
->lock
);
3227 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3228 ndisc_send_rs(dev
, &lladdr
,
3229 &in6addr_linklocal_allrouters
);
3233 write_lock(&idev
->lock
);
3234 /* The wait after the last probe can be shorter */
3235 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3236 idev
->cnf
.rtr_solicits
) ?
3237 idev
->cnf
.rtr_solicit_delay
:
3238 idev
->cnf
.rtr_solicit_interval
);
3241 * Note: we do not support deprecated "all on-link"
3242 * assumption any longer.
3244 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3248 write_unlock(&idev
->lock
);
3254 * Duplicate Address Detection
3256 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3258 unsigned long rand_num
;
3259 struct inet6_dev
*idev
= ifp
->idev
;
3261 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3264 rand_num
= prandom_u32() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3266 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
3267 addrconf_mod_dad_work(ifp
, rand_num
);
3270 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
3272 struct inet6_dev
*idev
= ifp
->idev
;
3273 struct net_device
*dev
= idev
->dev
;
3275 addrconf_join_solict(dev
, &ifp
->addr
);
3277 prandom_seed((__force u32
) ifp
->addr
.s6_addr32
[3]);
3279 read_lock_bh(&idev
->lock
);
3280 spin_lock(&ifp
->lock
);
3281 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3284 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3285 idev
->cnf
.accept_dad
< 1 ||
3286 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3287 ifp
->flags
& IFA_F_NODAD
) {
3288 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3289 spin_unlock(&ifp
->lock
);
3290 read_unlock_bh(&idev
->lock
);
3292 addrconf_dad_completed(ifp
);
3296 if (!(idev
->if_flags
& IF_READY
)) {
3297 spin_unlock(&ifp
->lock
);
3298 read_unlock_bh(&idev
->lock
);
3300 * If the device is not ready:
3301 * - keep it tentative if it is a permanent address.
3302 * - otherwise, kill it.
3305 addrconf_dad_stop(ifp
, 0);
3310 * Optimistic nodes can start receiving
3313 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
3314 ip6_ins_rt(ifp
->rt
);
3315 if (ipv6_use_optimistic_addr(idev
)) {
3316 /* Because optimistic nodes can use this address,
3317 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3319 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3323 addrconf_dad_kick(ifp
);
3325 spin_unlock(&ifp
->lock
);
3326 read_unlock_bh(&idev
->lock
);
3329 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
3331 bool begin_dad
= false;
3333 spin_lock_bh(&ifp
->state_lock
);
3334 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
3335 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
3338 spin_unlock_bh(&ifp
->state_lock
);
3341 addrconf_mod_dad_work(ifp
, 0);
3344 static void addrconf_dad_work(struct work_struct
*w
)
3346 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
3347 struct inet6_ifaddr
,
3349 struct inet6_dev
*idev
= ifp
->idev
;
3350 struct in6_addr mcaddr
;
3356 } action
= DAD_PROCESS
;
3360 spin_lock_bh(&ifp
->state_lock
);
3361 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
3363 ifp
->state
= INET6_IFADDR_STATE_DAD
;
3364 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
3366 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
3368 spin_unlock_bh(&ifp
->state_lock
);
3370 if (action
== DAD_BEGIN
) {
3371 addrconf_dad_begin(ifp
);
3373 } else if (action
== DAD_ABORT
) {
3374 addrconf_dad_stop(ifp
, 1);
3378 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
3381 write_lock_bh(&idev
->lock
);
3382 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
3383 write_unlock_bh(&idev
->lock
);
3387 spin_lock(&ifp
->lock
);
3388 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
3389 spin_unlock(&ifp
->lock
);
3390 write_unlock_bh(&idev
->lock
);
3394 if (ifp
->dad_probes
== 0) {
3396 * DAD was successful
3399 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3400 spin_unlock(&ifp
->lock
);
3401 write_unlock_bh(&idev
->lock
);
3403 addrconf_dad_completed(ifp
);
3409 addrconf_mod_dad_work(ifp
,
3410 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
));
3411 spin_unlock(&ifp
->lock
);
3412 write_unlock_bh(&idev
->lock
);
3414 /* send a neighbour solicitation for our addr */
3415 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
3416 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &in6addr_any
);
3422 /* ifp->idev must be at least read locked */
3423 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
3425 struct inet6_ifaddr
*ifpiter
;
3426 struct inet6_dev
*idev
= ifp
->idev
;
3428 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
3429 if (ifpiter
->scope
> IFA_LINK
)
3431 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
3432 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
3433 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
3440 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
3442 struct net_device
*dev
= ifp
->idev
->dev
;
3443 struct in6_addr lladdr
;
3444 bool send_rs
, send_mld
;
3446 addrconf_del_dad_work(ifp
);
3449 * Configure the address for reception. Now it is valid.
3452 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3454 /* If added prefix is link local and we are prepared to process
3455 router advertisements, start sending router solicitations.
3458 read_lock_bh(&ifp
->idev
->lock
);
3459 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
3460 send_rs
= send_mld
&&
3461 ipv6_accept_ra(ifp
->idev
) &&
3462 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
3463 (dev
->flags
&IFF_LOOPBACK
) == 0;
3464 read_unlock_bh(&ifp
->idev
->lock
);
3466 /* While dad is in progress mld report's source address is in6_addrany.
3467 * Resend with proper ll now.
3470 ipv6_mc_dad_complete(ifp
->idev
);
3474 * If a host as already performed a random delay
3475 * [...] as part of DAD [...] there is no need
3476 * to delay again before sending the first RS
3478 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3480 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
3482 write_lock_bh(&ifp
->idev
->lock
);
3483 spin_lock(&ifp
->lock
);
3484 ifp
->idev
->rs_probes
= 1;
3485 ifp
->idev
->if_flags
|= IF_RS_SENT
;
3486 addrconf_mod_rs_timer(ifp
->idev
,
3487 ifp
->idev
->cnf
.rtr_solicit_interval
);
3488 spin_unlock(&ifp
->lock
);
3489 write_unlock_bh(&ifp
->idev
->lock
);
3493 static void addrconf_dad_run(struct inet6_dev
*idev
)
3495 struct inet6_ifaddr
*ifp
;
3497 read_lock_bh(&idev
->lock
);
3498 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3499 spin_lock(&ifp
->lock
);
3500 if (ifp
->flags
& IFA_F_TENTATIVE
&&
3501 ifp
->state
== INET6_IFADDR_STATE_DAD
)
3502 addrconf_dad_kick(ifp
);
3503 spin_unlock(&ifp
->lock
);
3505 read_unlock_bh(&idev
->lock
);
3508 #ifdef CONFIG_PROC_FS
3509 struct if6_iter_state
{
3510 struct seq_net_private p
;
3515 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
3517 struct inet6_ifaddr
*ifa
= NULL
;
3518 struct if6_iter_state
*state
= seq
->private;
3519 struct net
*net
= seq_file_net(seq
);
3522 /* initial bucket if pos is 0 */
3528 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
3529 hlist_for_each_entry_rcu_bh(ifa
, &inet6_addr_lst
[state
->bucket
],
3531 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3533 /* sync with offset */
3534 if (p
< state
->offset
) {
3542 /* prepare for next bucket */
3549 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
3550 struct inet6_ifaddr
*ifa
)
3552 struct if6_iter_state
*state
= seq
->private;
3553 struct net
*net
= seq_file_net(seq
);
3555 hlist_for_each_entry_continue_rcu_bh(ifa
, addr_lst
) {
3556 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3562 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
3564 hlist_for_each_entry_rcu_bh(ifa
,
3565 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
3566 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3576 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3580 return if6_get_first(seq
, *pos
);
3583 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3585 struct inet6_ifaddr
*ifa
;
3587 ifa
= if6_get_next(seq
, v
);
3592 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
3595 rcu_read_unlock_bh();
3598 static int if6_seq_show(struct seq_file
*seq
, void *v
)
3600 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
3601 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
3603 ifp
->idev
->dev
->ifindex
,
3607 ifp
->idev
->dev
->name
);
3611 static const struct seq_operations if6_seq_ops
= {
3612 .start
= if6_seq_start
,
3613 .next
= if6_seq_next
,
3614 .show
= if6_seq_show
,
3615 .stop
= if6_seq_stop
,
3618 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
3620 return seq_open_net(inode
, file
, &if6_seq_ops
,
3621 sizeof(struct if6_iter_state
));
3624 static const struct file_operations if6_fops
= {
3625 .owner
= THIS_MODULE
,
3626 .open
= if6_seq_open
,
3628 .llseek
= seq_lseek
,
3629 .release
= seq_release_net
,
3632 static int __net_init
if6_proc_net_init(struct net
*net
)
3634 if (!proc_create("if_inet6", S_IRUGO
, net
->proc_net
, &if6_fops
))
3639 static void __net_exit
if6_proc_net_exit(struct net
*net
)
3641 remove_proc_entry("if_inet6", net
->proc_net
);
3644 static struct pernet_operations if6_proc_net_ops
= {
3645 .init
= if6_proc_net_init
,
3646 .exit
= if6_proc_net_exit
,
3649 int __init
if6_proc_init(void)
3651 return register_pernet_subsys(&if6_proc_net_ops
);
3654 void if6_proc_exit(void)
3656 unregister_pernet_subsys(&if6_proc_net_ops
);
3658 #endif /* CONFIG_PROC_FS */
3660 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3661 /* Check if address is a home address configured on any interface. */
3662 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
3665 struct inet6_ifaddr
*ifp
= NULL
;
3666 unsigned int hash
= inet6_addr_hash(addr
);
3669 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
3670 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
3672 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
3673 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
3678 rcu_read_unlock_bh();
3684 * Periodic address status verification
3687 static void addrconf_verify_rtnl(void)
3689 unsigned long now
, next
, next_sec
, next_sched
;
3690 struct inet6_ifaddr
*ifp
;
3697 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
3699 cancel_delayed_work(&addr_chk_work
);
3701 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3703 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[i
], addr_lst
) {
3706 /* When setting preferred_lft to a value not zero or
3707 * infinity, while valid_lft is infinity
3708 * IFA_F_PERMANENT has a non-infinity life time.
3710 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3711 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
3714 spin_lock(&ifp
->lock
);
3715 /* We try to batch several events at once. */
3716 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
3718 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
3719 age
>= ifp
->valid_lft
) {
3720 spin_unlock(&ifp
->lock
);
3724 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
3725 spin_unlock(&ifp
->lock
);
3727 } else if (age
>= ifp
->prefered_lft
) {
3728 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3731 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
3733 ifp
->flags
|= IFA_F_DEPRECATED
;
3736 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
3737 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
3738 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
3740 spin_unlock(&ifp
->lock
);
3745 ipv6_ifa_notify(0, ifp
);
3749 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
3750 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
3751 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
3752 ifp
->idev
->cnf
.dad_transmits
*
3753 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
) / HZ
;
3755 if (age
>= ifp
->prefered_lft
- regen_advance
) {
3756 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
3757 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3758 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3759 if (!ifp
->regen_count
&& ifpub
) {
3762 in6_ifa_hold(ifpub
);
3763 spin_unlock(&ifp
->lock
);
3765 spin_lock(&ifpub
->lock
);
3766 ifpub
->regen_count
= 0;
3767 spin_unlock(&ifpub
->lock
);
3768 ipv6_create_tempaddr(ifpub
, ifp
);
3773 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
3774 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
3775 spin_unlock(&ifp
->lock
);
3777 /* ifp->prefered_lft <= ifp->valid_lft */
3778 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3779 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3780 spin_unlock(&ifp
->lock
);
3785 next_sec
= round_jiffies_up(next
);
3788 /* If rounded timeout is accurate enough, accept it. */
3789 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
3790 next_sched
= next_sec
;
3792 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3793 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
3794 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
3796 ADBG(KERN_DEBUG
"now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3797 now
, next
, next_sec
, next_sched
);
3798 mod_delayed_work(addrconf_wq
, &addr_chk_work
, next_sched
- now
);
3799 rcu_read_unlock_bh();
3802 static void addrconf_verify_work(struct work_struct
*w
)
3805 addrconf_verify_rtnl();
3809 static void addrconf_verify(void)
3811 mod_delayed_work(addrconf_wq
, &addr_chk_work
, 0);
3814 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
3815 struct in6_addr
**peer_pfx
)
3817 struct in6_addr
*pfx
= NULL
;
3822 pfx
= nla_data(addr
);
3825 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
3827 pfx
= nla_data(local
);
3833 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
3834 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
3835 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
3836 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
3837 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
3841 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3843 struct net
*net
= sock_net(skb
->sk
);
3844 struct ifaddrmsg
*ifm
;
3845 struct nlattr
*tb
[IFA_MAX
+1];
3846 struct in6_addr
*pfx
, *peer_pfx
;
3850 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3854 ifm
= nlmsg_data(nlh
);
3855 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
3859 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
3861 /* We ignore other flags so far. */
3862 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
3864 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
3865 ifm
->ifa_prefixlen
);
3868 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u32 ifa_flags
,
3869 u32 prefered_lft
, u32 valid_lft
)
3873 unsigned long timeout
;
3874 bool was_managetempaddr
;
3875 bool had_prefixroute
;
3879 if (!valid_lft
|| (prefered_lft
> valid_lft
))
3882 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&&
3883 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
3886 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
3887 if (addrconf_finite_timeout(timeout
)) {
3888 expires
= jiffies_to_clock_t(timeout
* HZ
);
3889 valid_lft
= timeout
;
3890 flags
= RTF_EXPIRES
;
3894 ifa_flags
|= IFA_F_PERMANENT
;
3897 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
3898 if (addrconf_finite_timeout(timeout
)) {
3900 ifa_flags
|= IFA_F_DEPRECATED
;
3901 prefered_lft
= timeout
;
3904 spin_lock_bh(&ifp
->lock
);
3905 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
3906 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
3907 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
3908 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
3909 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
3910 IFA_F_NOPREFIXROUTE
);
3911 ifp
->flags
|= ifa_flags
;
3912 ifp
->tstamp
= jiffies
;
3913 ifp
->valid_lft
= valid_lft
;
3914 ifp
->prefered_lft
= prefered_lft
;
3916 spin_unlock_bh(&ifp
->lock
);
3917 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
3918 ipv6_ifa_notify(0, ifp
);
3920 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
3921 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
3923 } else if (had_prefixroute
) {
3924 enum cleanup_prefix_rt_t action
;
3925 unsigned long rt_expires
;
3927 write_lock_bh(&ifp
->idev
->lock
);
3928 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
3929 write_unlock_bh(&ifp
->idev
->lock
);
3931 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
3932 cleanup_prefix_route(ifp
, rt_expires
,
3933 action
== CLEANUP_PREFIX_RT_DEL
);
3937 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
3938 if (was_managetempaddr
&& !(ifp
->flags
& IFA_F_MANAGETEMPADDR
))
3939 valid_lft
= prefered_lft
= 0;
3940 manage_tempaddrs(ifp
->idev
, ifp
, valid_lft
, prefered_lft
,
3941 !was_managetempaddr
, jiffies
);
3944 addrconf_verify_rtnl();
3950 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3952 struct net
*net
= sock_net(skb
->sk
);
3953 struct ifaddrmsg
*ifm
;
3954 struct nlattr
*tb
[IFA_MAX
+1];
3955 struct in6_addr
*pfx
, *peer_pfx
;
3956 struct inet6_ifaddr
*ifa
;
3957 struct net_device
*dev
;
3958 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
3962 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3966 ifm
= nlmsg_data(nlh
);
3967 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
3971 if (tb
[IFA_CACHEINFO
]) {
3972 struct ifa_cacheinfo
*ci
;
3974 ci
= nla_data(tb
[IFA_CACHEINFO
]);
3975 valid_lft
= ci
->ifa_valid
;
3976 preferred_lft
= ci
->ifa_prefered
;
3978 preferred_lft
= INFINITY_LIFE_TIME
;
3979 valid_lft
= INFINITY_LIFE_TIME
;
3982 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
3986 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
3988 /* We ignore other flags so far. */
3989 ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
3990 IFA_F_NOPREFIXROUTE
| IFA_F_MCAUTOJOIN
;
3992 ifa
= ipv6_get_ifaddr(net
, pfx
, dev
, 1);
3995 * It would be best to check for !NLM_F_CREATE here but
3996 * userspace already relies on not having to provide this.
3998 return inet6_addr_add(net
, ifm
->ifa_index
, pfx
, peer_pfx
,
3999 ifm
->ifa_prefixlen
, ifa_flags
,
4000 preferred_lft
, valid_lft
);
4003 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
4004 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
4007 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
4014 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
4015 u8 scope
, int ifindex
)
4017 struct ifaddrmsg
*ifm
;
4019 ifm
= nlmsg_data(nlh
);
4020 ifm
->ifa_family
= AF_INET6
;
4021 ifm
->ifa_prefixlen
= prefixlen
;
4022 ifm
->ifa_flags
= flags
;
4023 ifm
->ifa_scope
= scope
;
4024 ifm
->ifa_index
= ifindex
;
4027 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
4028 unsigned long tstamp
, u32 preferred
, u32 valid
)
4030 struct ifa_cacheinfo ci
;
4032 ci
.cstamp
= cstamp_delta(cstamp
);
4033 ci
.tstamp
= cstamp_delta(tstamp
);
4034 ci
.ifa_prefered
= preferred
;
4035 ci
.ifa_valid
= valid
;
4037 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
4040 static inline int rt_scope(int ifa_scope
)
4042 if (ifa_scope
& IFA_HOST
)
4043 return RT_SCOPE_HOST
;
4044 else if (ifa_scope
& IFA_LINK
)
4045 return RT_SCOPE_LINK
;
4046 else if (ifa_scope
& IFA_SITE
)
4047 return RT_SCOPE_SITE
;
4049 return RT_SCOPE_UNIVERSE
;
4052 static inline int inet6_ifaddr_msgsize(void)
4054 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
4055 + nla_total_size(16) /* IFA_LOCAL */
4056 + nla_total_size(16) /* IFA_ADDRESS */
4057 + nla_total_size(sizeof(struct ifa_cacheinfo
))
4058 + nla_total_size(4) /* IFA_FLAGS */;
4061 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
4062 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4064 struct nlmsghdr
*nlh
;
4065 u32 preferred
, valid
;
4067 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4071 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
4072 ifa
->idev
->dev
->ifindex
);
4074 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
4075 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
4076 preferred
= ifa
->prefered_lft
;
4077 valid
= ifa
->valid_lft
;
4078 if (preferred
!= INFINITY_LIFE_TIME
) {
4079 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
4080 if (preferred
> tval
)
4084 if (valid
!= INFINITY_LIFE_TIME
) {
4092 preferred
= INFINITY_LIFE_TIME
;
4093 valid
= INFINITY_LIFE_TIME
;
4096 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
4097 if (nla_put(skb
, IFA_LOCAL
, 16, &ifa
->addr
) < 0 ||
4098 nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->peer_addr
) < 0)
4101 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0)
4104 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
4107 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
4110 nlmsg_end(skb
, nlh
);
4114 nlmsg_cancel(skb
, nlh
);
4118 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
4119 u32 portid
, u32 seq
, int event
, u16 flags
)
4121 struct nlmsghdr
*nlh
;
4122 u8 scope
= RT_SCOPE_UNIVERSE
;
4123 int ifindex
= ifmca
->idev
->dev
->ifindex
;
4125 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
4126 scope
= RT_SCOPE_SITE
;
4128 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4132 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4133 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
4134 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
4135 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4136 nlmsg_cancel(skb
, nlh
);
4140 nlmsg_end(skb
, nlh
);
4144 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
4145 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4147 struct nlmsghdr
*nlh
;
4148 u8 scope
= RT_SCOPE_UNIVERSE
;
4149 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
4151 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
4152 scope
= RT_SCOPE_SITE
;
4154 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4158 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4159 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
4160 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
4161 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4162 nlmsg_cancel(skb
, nlh
);
4166 nlmsg_end(skb
, nlh
);
4176 /* called with rcu_read_lock() */
4177 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
4178 struct netlink_callback
*cb
, enum addr_type_t type
,
4179 int s_ip_idx
, int *p_ip_idx
)
4181 struct ifmcaddr6
*ifmca
;
4182 struct ifacaddr6
*ifaca
;
4184 int ip_idx
= *p_ip_idx
;
4186 read_lock_bh(&idev
->lock
);
4188 case UNICAST_ADDR
: {
4189 struct inet6_ifaddr
*ifa
;
4191 /* unicast address incl. temp addr */
4192 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
4193 if (++ip_idx
< s_ip_idx
)
4195 err
= inet6_fill_ifaddr(skb
, ifa
,
4196 NETLINK_CB(cb
->skb
).portid
,
4202 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4206 case MULTICAST_ADDR
:
4207 /* multicast address */
4208 for (ifmca
= idev
->mc_list
; ifmca
;
4209 ifmca
= ifmca
->next
, ip_idx
++) {
4210 if (ip_idx
< s_ip_idx
)
4212 err
= inet6_fill_ifmcaddr(skb
, ifmca
,
4213 NETLINK_CB(cb
->skb
).portid
,
4222 /* anycast address */
4223 for (ifaca
= idev
->ac_list
; ifaca
;
4224 ifaca
= ifaca
->aca_next
, ip_idx
++) {
4225 if (ip_idx
< s_ip_idx
)
4227 err
= inet6_fill_ifacaddr(skb
, ifaca
,
4228 NETLINK_CB(cb
->skb
).portid
,
4239 read_unlock_bh(&idev
->lock
);
4244 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
4245 enum addr_type_t type
)
4247 struct net
*net
= sock_net(skb
->sk
);
4250 int s_idx
, s_ip_idx
;
4251 struct net_device
*dev
;
4252 struct inet6_dev
*idev
;
4253 struct hlist_head
*head
;
4256 s_idx
= idx
= cb
->args
[1];
4257 s_ip_idx
= ip_idx
= cb
->args
[2];
4260 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^ net
->dev_base_seq
;
4261 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4263 head
= &net
->dev_index_head
[h
];
4264 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4267 if (h
> s_h
|| idx
> s_idx
)
4270 idev
= __in6_dev_get(dev
);
4274 if (in6_dump_addrs(idev
, skb
, cb
, type
,
4275 s_ip_idx
, &ip_idx
) < 0)
4285 cb
->args
[2] = ip_idx
;
4290 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4292 enum addr_type_t type
= UNICAST_ADDR
;
4294 return inet6_dump_addr(skb
, cb
, type
);
4297 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4299 enum addr_type_t type
= MULTICAST_ADDR
;
4301 return inet6_dump_addr(skb
, cb
, type
);
4305 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4307 enum addr_type_t type
= ANYCAST_ADDR
;
4309 return inet6_dump_addr(skb
, cb
, type
);
4312 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
)
4314 struct net
*net
= sock_net(in_skb
->sk
);
4315 struct ifaddrmsg
*ifm
;
4316 struct nlattr
*tb
[IFA_MAX
+1];
4317 struct in6_addr
*addr
= NULL
, *peer
;
4318 struct net_device
*dev
= NULL
;
4319 struct inet6_ifaddr
*ifa
;
4320 struct sk_buff
*skb
;
4323 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4327 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
4333 ifm
= nlmsg_data(nlh
);
4335 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4337 ifa
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
4339 err
= -EADDRNOTAVAIL
;
4343 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
4349 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).portid
,
4350 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
4352 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4353 WARN_ON(err
== -EMSGSIZE
);
4357 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4364 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
4366 struct sk_buff
*skb
;
4367 struct net
*net
= dev_net(ifa
->idev
->dev
);
4370 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
4374 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
4376 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4377 WARN_ON(err
== -EMSGSIZE
);
4381 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
4385 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
4388 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
4389 __s32
*array
, int bytes
)
4391 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
4393 memset(array
, 0, bytes
);
4394 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
4395 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
4396 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
4397 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
4398 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
4399 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
4400 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
4401 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
4402 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
4403 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
4404 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
4405 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
4406 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
4407 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
4408 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
4409 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
4410 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
4411 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
4412 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
4413 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
4414 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
4415 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
4416 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
4417 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
4418 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
4419 #ifdef CONFIG_IPV6_ROUTER_PREF
4420 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
4421 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
4422 jiffies_to_msecs(cnf
->rtr_probe_interval
);
4423 #ifdef CONFIG_IPV6_ROUTE_INFO
4424 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
4427 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
4428 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
4429 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4430 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
4431 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
4433 #ifdef CONFIG_IPV6_MROUTE
4434 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
4436 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
4437 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
4438 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
4439 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
4440 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
4441 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
4442 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
4443 /* we omit DEVCONF_STABLE_SECRET for now */
4446 static inline size_t inet6_ifla6_size(void)
4448 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4449 + nla_total_size(sizeof(struct ifla_cacheinfo
))
4450 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
4451 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
4452 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
4453 + nla_total_size(sizeof(struct in6_addr
)); /* IFLA_INET6_TOKEN */
4456 static inline size_t inet6_if_nlmsg_size(void)
4458 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4459 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4460 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4461 + nla_total_size(4) /* IFLA_MTU */
4462 + nla_total_size(4) /* IFLA_LINK */
4463 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4466 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
4467 int items
, int bytes
)
4470 int pad
= bytes
- sizeof(u64
) * items
;
4473 /* Use put_unaligned() because stats may not be aligned for u64. */
4474 put_unaligned(items
, &stats
[0]);
4475 for (i
= 1; i
< items
; i
++)
4476 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
4478 memset(&stats
[items
], 0, pad
);
4481 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
4482 int items
, int bytes
, size_t syncpoff
)
4485 int pad
= bytes
- sizeof(u64
) * items
;
4488 /* Use put_unaligned() because stats may not be aligned for u64. */
4489 put_unaligned(items
, &stats
[0]);
4490 for (i
= 1; i
< items
; i
++)
4491 put_unaligned(snmp_fold_field64(mib
, i
, syncpoff
), &stats
[i
]);
4493 memset(&stats
[items
], 0, pad
);
4496 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
4500 case IFLA_INET6_STATS
:
4501 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
,
4502 IPSTATS_MIB_MAX
, bytes
, offsetof(struct ipstats_mib
, syncp
));
4504 case IFLA_INET6_ICMP6STATS
:
4505 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, ICMP6_MIB_MAX
, bytes
);
4510 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
)
4513 struct ifla_cacheinfo ci
;
4515 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
4516 goto nla_put_failure
;
4517 ci
.max_reasm_len
= IPV6_MAXPLEN
;
4518 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
4519 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
4520 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
4521 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
4522 goto nla_put_failure
;
4523 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
4525 goto nla_put_failure
;
4526 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
4528 /* XXX - MC not implemented */
4530 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
4532 goto nla_put_failure
;
4533 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
4535 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
4537 goto nla_put_failure
;
4538 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
4540 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
4542 goto nla_put_failure
;
4544 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->addr_gen_mode
))
4545 goto nla_put_failure
;
4547 read_lock_bh(&idev
->lock
);
4548 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
4549 read_unlock_bh(&idev
->lock
);
4557 static size_t inet6_get_link_af_size(const struct net_device
*dev
)
4559 if (!__in6_dev_get(dev
))
4562 return inet6_ifla6_size();
4565 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
)
4567 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4572 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4578 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
)
4580 struct inet6_ifaddr
*ifp
;
4581 struct net_device
*dev
= idev
->dev
;
4582 bool update_rs
= false;
4583 struct in6_addr ll_addr
;
4589 if (ipv6_addr_any(token
))
4591 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
))
4593 if (!ipv6_accept_ra(idev
))
4595 if (idev
->cnf
.rtr_solicits
<= 0)
4598 write_lock_bh(&idev
->lock
);
4600 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
4601 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
4603 write_unlock_bh(&idev
->lock
);
4605 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
4606 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
4607 IFA_F_OPTIMISTIC
)) {
4609 /* If we're not ready, then normal ifup will take care
4610 * of this. Otherwise, we need to request our rs here.
4612 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
4616 write_lock_bh(&idev
->lock
);
4619 idev
->if_flags
|= IF_RS_SENT
;
4620 idev
->rs_probes
= 1;
4621 addrconf_mod_rs_timer(idev
, idev
->cnf
.rtr_solicit_interval
);
4624 /* Well, that's kinda nasty ... */
4625 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4626 spin_lock(&ifp
->lock
);
4627 if (ifp
->tokenized
) {
4629 ifp
->prefered_lft
= 0;
4631 spin_unlock(&ifp
->lock
);
4634 write_unlock_bh(&idev
->lock
);
4635 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
4636 addrconf_verify_rtnl();
4640 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
4641 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
4642 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
4645 static int inet6_validate_link_af(const struct net_device
*dev
,
4646 const struct nlattr
*nla
)
4648 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4650 if (dev
&& !__in6_dev_get(dev
))
4651 return -EAFNOSUPPORT
;
4653 return nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, inet6_af_policy
);
4656 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
4659 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4660 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4663 return -EAFNOSUPPORT
;
4665 if (nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, NULL
) < 0)
4668 if (tb
[IFLA_INET6_TOKEN
]) {
4669 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]));
4674 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
4675 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
4677 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
4678 mode
!= IN6_ADDR_GEN_MODE_NONE
)
4680 idev
->addr_gen_mode
= mode
;
4687 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4688 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4690 struct net_device
*dev
= idev
->dev
;
4691 struct ifinfomsg
*hdr
;
4692 struct nlmsghdr
*nlh
;
4695 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
4699 hdr
= nlmsg_data(nlh
);
4700 hdr
->ifi_family
= AF_INET6
;
4702 hdr
->ifi_type
= dev
->type
;
4703 hdr
->ifi_index
= dev
->ifindex
;
4704 hdr
->ifi_flags
= dev_get_flags(dev
);
4705 hdr
->ifi_change
= 0;
4707 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4709 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4710 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4711 (dev
->ifindex
!= dev
->iflink
&&
4712 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)))
4713 goto nla_put_failure
;
4714 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4715 if (protoinfo
== NULL
)
4716 goto nla_put_failure
;
4718 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4719 goto nla_put_failure
;
4721 nla_nest_end(skb
, protoinfo
);
4722 nlmsg_end(skb
, nlh
);
4726 nlmsg_cancel(skb
, nlh
);
4730 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4732 struct net
*net
= sock_net(skb
->sk
);
4735 struct net_device
*dev
;
4736 struct inet6_dev
*idev
;
4737 struct hlist_head
*head
;
4740 s_idx
= cb
->args
[1];
4743 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4745 head
= &net
->dev_index_head
[h
];
4746 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4749 idev
= __in6_dev_get(dev
);
4752 if (inet6_fill_ifinfo(skb
, idev
,
4753 NETLINK_CB(cb
->skb
).portid
,
4755 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
4769 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
4771 struct sk_buff
*skb
;
4772 struct net
*net
= dev_net(idev
->dev
);
4775 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
4779 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
4781 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4782 WARN_ON(err
== -EMSGSIZE
);
4786 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
4790 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
4793 static inline size_t inet6_prefix_nlmsg_size(void)
4795 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
4796 + nla_total_size(sizeof(struct in6_addr
))
4797 + nla_total_size(sizeof(struct prefix_cacheinfo
));
4800 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4801 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
4802 int event
, unsigned int flags
)
4804 struct prefixmsg
*pmsg
;
4805 struct nlmsghdr
*nlh
;
4806 struct prefix_cacheinfo ci
;
4808 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
4812 pmsg
= nlmsg_data(nlh
);
4813 pmsg
->prefix_family
= AF_INET6
;
4814 pmsg
->prefix_pad1
= 0;
4815 pmsg
->prefix_pad2
= 0;
4816 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
4817 pmsg
->prefix_len
= pinfo
->prefix_len
;
4818 pmsg
->prefix_type
= pinfo
->type
;
4819 pmsg
->prefix_pad3
= 0;
4820 pmsg
->prefix_flags
= 0;
4822 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
4823 if (pinfo
->autoconf
)
4824 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
4826 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
4827 goto nla_put_failure
;
4828 ci
.preferred_time
= ntohl(pinfo
->prefered
);
4829 ci
.valid_time
= ntohl(pinfo
->valid
);
4830 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
4831 goto nla_put_failure
;
4832 nlmsg_end(skb
, nlh
);
4836 nlmsg_cancel(skb
, nlh
);
4840 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
4841 struct prefix_info
*pinfo
)
4843 struct sk_buff
*skb
;
4844 struct net
*net
= dev_net(idev
->dev
);
4847 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
4851 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
4853 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4854 WARN_ON(err
== -EMSGSIZE
);
4858 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
4862 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
4865 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4867 struct net
*net
= dev_net(ifp
->idev
->dev
);
4872 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
4877 * If the address was optimistic
4878 * we inserted the route at the start of
4879 * our DAD process, so we don't need
4882 if (!(ifp
->rt
->rt6i_node
))
4883 ip6_ins_rt(ifp
->rt
);
4884 if (ifp
->idev
->cnf
.forwarding
)
4885 addrconf_join_anycast(ifp
);
4886 if (!ipv6_addr_any(&ifp
->peer_addr
))
4887 addrconf_prefix_route(&ifp
->peer_addr
, 128,
4888 ifp
->idev
->dev
, 0, 0);
4891 if (ifp
->idev
->cnf
.forwarding
)
4892 addrconf_leave_anycast(ifp
);
4893 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
4894 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
4895 struct rt6_info
*rt
;
4897 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
4898 ifp
->idev
->dev
, 0, 0);
4899 if (rt
&& ip6_del_rt(rt
))
4902 dst_hold(&ifp
->rt
->dst
);
4904 if (ip6_del_rt(ifp
->rt
))
4905 dst_free(&ifp
->rt
->dst
);
4907 rt_genid_bump_ipv6(net
);
4910 atomic_inc(&net
->ipv6
.dev_addr_genid
);
4913 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4916 if (likely(ifp
->idev
->dead
== 0))
4917 __ipv6_ifa_notify(event
, ifp
);
4918 rcu_read_unlock_bh();
4921 #ifdef CONFIG_SYSCTL
4924 int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
4925 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4927 int *valp
= ctl
->data
;
4930 struct ctl_table lctl
;
4934 * ctl->data points to idev->cnf.forwarding, we should
4935 * not modify it until we get the rtnl lock.
4940 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
4943 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
4950 int addrconf_sysctl_mtu(struct ctl_table
*ctl
, int write
,
4951 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4953 struct inet6_dev
*idev
= ctl
->extra1
;
4954 int min_mtu
= IPV6_MIN_MTU
;
4955 struct ctl_table lctl
;
4958 lctl
.extra1
= &min_mtu
;
4959 lctl
.extra2
= idev
? &idev
->dev
->mtu
: NULL
;
4961 return proc_dointvec_minmax(&lctl
, write
, buffer
, lenp
, ppos
);
4964 static void dev_disable_change(struct inet6_dev
*idev
)
4966 struct netdev_notifier_info info
;
4968 if (!idev
|| !idev
->dev
)
4971 netdev_notifier_info_init(&info
, idev
->dev
);
4972 if (idev
->cnf
.disable_ipv6
)
4973 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
4975 addrconf_notify(NULL
, NETDEV_UP
, &info
);
4978 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
4980 struct net_device
*dev
;
4981 struct inet6_dev
*idev
;
4984 for_each_netdev_rcu(net
, dev
) {
4985 idev
= __in6_dev_get(dev
);
4987 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
4988 idev
->cnf
.disable_ipv6
= newf
;
4990 dev_disable_change(idev
);
4996 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
5001 if (!rtnl_trylock())
5002 return restart_syscall();
5004 net
= (struct net
*)table
->extra2
;
5008 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
5013 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
5014 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
5015 addrconf_disable_change(net
, newf
);
5016 } else if ((!newf
) ^ (!old
))
5017 dev_disable_change((struct inet6_dev
*)table
->extra1
);
5024 int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
5025 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5027 int *valp
= ctl
->data
;
5030 struct ctl_table lctl
;
5034 * ctl->data points to idev->cnf.disable_ipv6, we should
5035 * not modify it until we get the rtnl lock.
5040 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5043 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
5050 int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
5051 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5053 int *valp
= ctl
->data
;
5058 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
5061 if (write
&& old
!= new) {
5062 struct net
*net
= ctl
->extra2
;
5064 if (!rtnl_trylock())
5065 return restart_syscall();
5067 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
5068 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5069 NETCONFA_IFINDEX_DEFAULT
,
5070 net
->ipv6
.devconf_dflt
);
5071 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
5072 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5073 NETCONFA_IFINDEX_ALL
,
5074 net
->ipv6
.devconf_all
);
5076 struct inet6_dev
*idev
= ctl
->extra1
;
5078 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5088 static int addrconf_sysctl_stable_secret(struct ctl_table
*ctl
, int write
,
5089 void __user
*buffer
, size_t *lenp
,
5093 struct in6_addr addr
;
5094 char str
[IPV6_MAX_STRLEN
];
5095 struct ctl_table lctl
= *ctl
;
5096 struct ipv6_stable_secret
*secret
= ctl
->data
;
5098 lctl
.maxlen
= IPV6_MAX_STRLEN
;
5101 if (!rtnl_trylock())
5102 return restart_syscall();
5104 if (!write
&& !secret
->initialized
) {
5110 err
= snprintf(str
, sizeof(str
), "%pI6",
5112 if (err
>= sizeof(str
)) {
5118 err
= proc_dostring(&lctl
, write
, buffer
, lenp
, ppos
);
5122 if (in6_pton(str
, -1, addr
.in6_u
.u6_addr8
, -1, NULL
) != 1) {
5127 secret
->initialized
= true;
5128 secret
->secret
= addr
;
5136 static struct addrconf_sysctl_table
5138 struct ctl_table_header
*sysctl_header
;
5139 struct ctl_table addrconf_vars
[DEVCONF_MAX
+1];
5140 } addrconf_sysctl __read_mostly
= {
5141 .sysctl_header
= NULL
,
5144 .procname
= "forwarding",
5145 .data
= &ipv6_devconf
.forwarding
,
5146 .maxlen
= sizeof(int),
5148 .proc_handler
= addrconf_sysctl_forward
,
5151 .procname
= "hop_limit",
5152 .data
= &ipv6_devconf
.hop_limit
,
5153 .maxlen
= sizeof(int),
5155 .proc_handler
= proc_dointvec
,
5159 .data
= &ipv6_devconf
.mtu6
,
5160 .maxlen
= sizeof(int),
5162 .proc_handler
= addrconf_sysctl_mtu
,
5165 .procname
= "accept_ra",
5166 .data
= &ipv6_devconf
.accept_ra
,
5167 .maxlen
= sizeof(int),
5169 .proc_handler
= proc_dointvec
,
5172 .procname
= "accept_redirects",
5173 .data
= &ipv6_devconf
.accept_redirects
,
5174 .maxlen
= sizeof(int),
5176 .proc_handler
= proc_dointvec
,
5179 .procname
= "autoconf",
5180 .data
= &ipv6_devconf
.autoconf
,
5181 .maxlen
= sizeof(int),
5183 .proc_handler
= proc_dointvec
,
5186 .procname
= "dad_transmits",
5187 .data
= &ipv6_devconf
.dad_transmits
,
5188 .maxlen
= sizeof(int),
5190 .proc_handler
= proc_dointvec
,
5193 .procname
= "router_solicitations",
5194 .data
= &ipv6_devconf
.rtr_solicits
,
5195 .maxlen
= sizeof(int),
5197 .proc_handler
= proc_dointvec
,
5200 .procname
= "router_solicitation_interval",
5201 .data
= &ipv6_devconf
.rtr_solicit_interval
,
5202 .maxlen
= sizeof(int),
5204 .proc_handler
= proc_dointvec_jiffies
,
5207 .procname
= "router_solicitation_delay",
5208 .data
= &ipv6_devconf
.rtr_solicit_delay
,
5209 .maxlen
= sizeof(int),
5211 .proc_handler
= proc_dointvec_jiffies
,
5214 .procname
= "force_mld_version",
5215 .data
= &ipv6_devconf
.force_mld_version
,
5216 .maxlen
= sizeof(int),
5218 .proc_handler
= proc_dointvec
,
5221 .procname
= "mldv1_unsolicited_report_interval",
5223 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
5224 .maxlen
= sizeof(int),
5226 .proc_handler
= proc_dointvec_ms_jiffies
,
5229 .procname
= "mldv2_unsolicited_report_interval",
5231 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
5232 .maxlen
= sizeof(int),
5234 .proc_handler
= proc_dointvec_ms_jiffies
,
5237 .procname
= "use_tempaddr",
5238 .data
= &ipv6_devconf
.use_tempaddr
,
5239 .maxlen
= sizeof(int),
5241 .proc_handler
= proc_dointvec
,
5244 .procname
= "temp_valid_lft",
5245 .data
= &ipv6_devconf
.temp_valid_lft
,
5246 .maxlen
= sizeof(int),
5248 .proc_handler
= proc_dointvec
,
5251 .procname
= "temp_prefered_lft",
5252 .data
= &ipv6_devconf
.temp_prefered_lft
,
5253 .maxlen
= sizeof(int),
5255 .proc_handler
= proc_dointvec
,
5258 .procname
= "regen_max_retry",
5259 .data
= &ipv6_devconf
.regen_max_retry
,
5260 .maxlen
= sizeof(int),
5262 .proc_handler
= proc_dointvec
,
5265 .procname
= "max_desync_factor",
5266 .data
= &ipv6_devconf
.max_desync_factor
,
5267 .maxlen
= sizeof(int),
5269 .proc_handler
= proc_dointvec
,
5272 .procname
= "max_addresses",
5273 .data
= &ipv6_devconf
.max_addresses
,
5274 .maxlen
= sizeof(int),
5276 .proc_handler
= proc_dointvec
,
5279 .procname
= "accept_ra_defrtr",
5280 .data
= &ipv6_devconf
.accept_ra_defrtr
,
5281 .maxlen
= sizeof(int),
5283 .proc_handler
= proc_dointvec
,
5286 .procname
= "accept_ra_pinfo",
5287 .data
= &ipv6_devconf
.accept_ra_pinfo
,
5288 .maxlen
= sizeof(int),
5290 .proc_handler
= proc_dointvec
,
5292 #ifdef CONFIG_IPV6_ROUTER_PREF
5294 .procname
= "accept_ra_rtr_pref",
5295 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
5296 .maxlen
= sizeof(int),
5298 .proc_handler
= proc_dointvec
,
5301 .procname
= "router_probe_interval",
5302 .data
= &ipv6_devconf
.rtr_probe_interval
,
5303 .maxlen
= sizeof(int),
5305 .proc_handler
= proc_dointvec_jiffies
,
5307 #ifdef CONFIG_IPV6_ROUTE_INFO
5309 .procname
= "accept_ra_rt_info_max_plen",
5310 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
5311 .maxlen
= sizeof(int),
5313 .proc_handler
= proc_dointvec
,
5318 .procname
= "proxy_ndp",
5319 .data
= &ipv6_devconf
.proxy_ndp
,
5320 .maxlen
= sizeof(int),
5322 .proc_handler
= addrconf_sysctl_proxy_ndp
,
5325 .procname
= "accept_source_route",
5326 .data
= &ipv6_devconf
.accept_source_route
,
5327 .maxlen
= sizeof(int),
5329 .proc_handler
= proc_dointvec
,
5331 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5333 .procname
= "optimistic_dad",
5334 .data
= &ipv6_devconf
.optimistic_dad
,
5335 .maxlen
= sizeof(int),
5337 .proc_handler
= proc_dointvec
,
5341 .procname
= "use_optimistic",
5342 .data
= &ipv6_devconf
.use_optimistic
,
5343 .maxlen
= sizeof(int),
5345 .proc_handler
= proc_dointvec
,
5349 #ifdef CONFIG_IPV6_MROUTE
5351 .procname
= "mc_forwarding",
5352 .data
= &ipv6_devconf
.mc_forwarding
,
5353 .maxlen
= sizeof(int),
5355 .proc_handler
= proc_dointvec
,
5359 .procname
= "disable_ipv6",
5360 .data
= &ipv6_devconf
.disable_ipv6
,
5361 .maxlen
= sizeof(int),
5363 .proc_handler
= addrconf_sysctl_disable
,
5366 .procname
= "accept_dad",
5367 .data
= &ipv6_devconf
.accept_dad
,
5368 .maxlen
= sizeof(int),
5370 .proc_handler
= proc_dointvec
,
5373 .procname
= "force_tllao",
5374 .data
= &ipv6_devconf
.force_tllao
,
5375 .maxlen
= sizeof(int),
5377 .proc_handler
= proc_dointvec
5380 .procname
= "ndisc_notify",
5381 .data
= &ipv6_devconf
.ndisc_notify
,
5382 .maxlen
= sizeof(int),
5384 .proc_handler
= proc_dointvec
5387 .procname
= "suppress_frag_ndisc",
5388 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
5389 .maxlen
= sizeof(int),
5391 .proc_handler
= proc_dointvec
5394 .procname
= "accept_ra_from_local",
5395 .data
= &ipv6_devconf
.accept_ra_from_local
,
5396 .maxlen
= sizeof(int),
5398 .proc_handler
= proc_dointvec
,
5401 .procname
= "accept_ra_mtu",
5402 .data
= &ipv6_devconf
.accept_ra_mtu
,
5403 .maxlen
= sizeof(int),
5405 .proc_handler
= proc_dointvec
,
5408 .procname
= "stable_secret",
5409 .data
= &ipv6_devconf
.stable_secret
,
5410 .maxlen
= IPV6_MAX_STRLEN
,
5412 .proc_handler
= addrconf_sysctl_stable_secret
,
5420 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
5421 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
5424 struct addrconf_sysctl_table
*t
;
5425 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
5427 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
5431 for (i
= 0; t
->addrconf_vars
[i
].data
; i
++) {
5432 t
->addrconf_vars
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
5433 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
5434 t
->addrconf_vars
[i
].extra2
= net
;
5437 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
5439 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->addrconf_vars
);
5440 if (t
->sysctl_header
== NULL
)
5452 static void __addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
5454 struct addrconf_sysctl_table
*t
;
5456 if (p
->sysctl
== NULL
)
5461 unregister_net_sysctl_table(t
->sysctl_header
);
5465 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
5469 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
5472 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
5473 &ndisc_ifinfo_sysctl_change
);
5476 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
5479 neigh_sysctl_unregister(idev
->nd_parms
);
5484 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
5486 __addrconf_sysctl_unregister(&idev
->cnf
);
5487 neigh_sysctl_unregister(idev
->nd_parms
);
5493 static int __net_init
addrconf_init_net(struct net
*net
)
5496 struct ipv6_devconf
*all
, *dflt
;
5498 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
5502 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
5504 goto err_alloc_dflt
;
5506 /* these will be inherited by all namespaces */
5507 dflt
->autoconf
= ipv6_defaults
.autoconf
;
5508 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
5510 dflt
->stable_secret
.initialized
= false;
5511 all
->stable_secret
.initialized
= false;
5513 net
->ipv6
.devconf_all
= all
;
5514 net
->ipv6
.devconf_dflt
= dflt
;
5516 #ifdef CONFIG_SYSCTL
5517 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
5521 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
5527 #ifdef CONFIG_SYSCTL
5529 __addrconf_sysctl_unregister(all
);
5539 static void __net_exit
addrconf_exit_net(struct net
*net
)
5541 #ifdef CONFIG_SYSCTL
5542 __addrconf_sysctl_unregister(net
->ipv6
.devconf_dflt
);
5543 __addrconf_sysctl_unregister(net
->ipv6
.devconf_all
);
5545 kfree(net
->ipv6
.devconf_dflt
);
5546 kfree(net
->ipv6
.devconf_all
);
5549 static struct pernet_operations addrconf_ops
= {
5550 .init
= addrconf_init_net
,
5551 .exit
= addrconf_exit_net
,
5554 static struct rtnl_af_ops inet6_ops __read_mostly
= {
5556 .fill_link_af
= inet6_fill_link_af
,
5557 .get_link_af_size
= inet6_get_link_af_size
,
5558 .validate_link_af
= inet6_validate_link_af
,
5559 .set_link_af
= inet6_set_link_af
,
5563 * Init / cleanup code
5566 int __init
addrconf_init(void)
5568 struct inet6_dev
*idev
;
5571 err
= ipv6_addr_label_init();
5573 pr_crit("%s: cannot initialize default policy table: %d\n",
5578 err
= register_pernet_subsys(&addrconf_ops
);
5582 addrconf_wq
= create_workqueue("ipv6_addrconf");
5588 /* The addrconf netdev notifier requires that loopback_dev
5589 * has it's ipv6 private information allocated and setup
5590 * before it can bring up and give link-local addresses
5591 * to other devices which are up.
5593 * Unfortunately, loopback_dev is not necessarily the first
5594 * entry in the global dev_base list of net devices. In fact,
5595 * it is likely to be the very last entry on that list.
5596 * So this causes the notifier registry below to try and
5597 * give link-local addresses to all devices besides loopback_dev
5598 * first, then loopback_dev, which cases all the non-loopback_dev
5599 * devices to fail to get a link-local address.
5601 * So, as a temporary fix, allocate the ipv6 structure for
5602 * loopback_dev first by hand.
5603 * Longer term, all of the dependencies ipv6 has upon the loopback
5604 * device and it being up should be removed.
5607 idev
= ipv6_add_dev(init_net
.loopback_dev
);
5610 err
= PTR_ERR(idev
);
5614 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5615 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
5617 register_netdevice_notifier(&ipv6_dev_notf
);
5621 rtnl_af_register(&inet6_ops
);
5623 err
= __rtnl_register(PF_INET6
, RTM_GETLINK
, NULL
, inet6_dump_ifinfo
,
5628 /* Only the first call to __rtnl_register can fail */
5629 __rtnl_register(PF_INET6
, RTM_NEWADDR
, inet6_rtm_newaddr
, NULL
, NULL
);
5630 __rtnl_register(PF_INET6
, RTM_DELADDR
, inet6_rtm_deladdr
, NULL
, NULL
);
5631 __rtnl_register(PF_INET6
, RTM_GETADDR
, inet6_rtm_getaddr
,
5632 inet6_dump_ifaddr
, NULL
);
5633 __rtnl_register(PF_INET6
, RTM_GETMULTICAST
, NULL
,
5634 inet6_dump_ifmcaddr
, NULL
);
5635 __rtnl_register(PF_INET6
, RTM_GETANYCAST
, NULL
,
5636 inet6_dump_ifacaddr
, NULL
);
5637 __rtnl_register(PF_INET6
, RTM_GETNETCONF
, inet6_netconf_get_devconf
,
5638 inet6_netconf_dump_devconf
, NULL
);
5640 ipv6_addr_label_rtnl_register();
5644 rtnl_af_unregister(&inet6_ops
);
5645 unregister_netdevice_notifier(&ipv6_dev_notf
);
5647 destroy_workqueue(addrconf_wq
);
5649 unregister_pernet_subsys(&addrconf_ops
);
5651 ipv6_addr_label_cleanup();
5656 void addrconf_cleanup(void)
5658 struct net_device
*dev
;
5661 unregister_netdevice_notifier(&ipv6_dev_notf
);
5662 unregister_pernet_subsys(&addrconf_ops
);
5663 ipv6_addr_label_cleanup();
5667 __rtnl_af_unregister(&inet6_ops
);
5669 /* clean dev list */
5670 for_each_netdev(&init_net
, dev
) {
5671 if (__in6_dev_get(dev
) == NULL
)
5673 addrconf_ifdown(dev
, 1);
5675 addrconf_ifdown(init_net
.loopback_dev
, 2);
5680 spin_lock_bh(&addrconf_hash_lock
);
5681 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5682 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
5683 spin_unlock_bh(&addrconf_hash_lock
);
5684 cancel_delayed_work(&addr_chk_work
);
5687 destroy_workqueue(addrconf_wq
);