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/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
60 #include <linux/sysctl.h>
62 #include <linux/capability.h>
63 #include <linux/delay.h>
64 #include <linux/notifier.h>
65 #include <linux/string.h>
66 #include <linux/hash.h>
68 #include <net/net_namespace.h>
72 #include <net/af_ieee802154.h>
73 #include <net/firewire.h>
75 #include <net/protocol.h>
76 #include <net/ndisc.h>
77 #include <net/ip6_route.h>
78 #include <net/addrconf.h>
81 #include <net/netlink.h>
82 #include <net/pkt_sched.h>
83 #include <linux/if_tunnel.h>
84 #include <linux/rtnetlink.h>
85 #include <linux/netconf.h>
86 #include <linux/random.h>
87 #include <linux/uaccess.h>
88 #include <asm/unaligned.h>
90 #include <linux/proc_fs.h>
91 #include <linux/seq_file.h>
92 #include <linux/export.h>
94 /* Set to 3 to get tracing... */
98 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
100 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
103 #define INFINITY_LIFE_TIME 0xFFFFFFFF
105 static inline u32
cstamp_delta(unsigned long cstamp
)
107 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
111 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
112 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
114 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
119 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
124 static void __ipv6_regen_rndid(struct inet6_dev
*idev
);
125 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
126 static void ipv6_regen_rndid(unsigned long data
);
128 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
129 static int ipv6_count_addresses(struct inet6_dev
*idev
);
132 * Configured unicast address hash table
134 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
135 static DEFINE_SPINLOCK(addrconf_hash_lock
);
137 static void addrconf_verify(void);
138 static void addrconf_verify_rtnl(void);
139 static void addrconf_verify_work(struct work_struct
*);
141 static struct workqueue_struct
*addrconf_wq
;
142 static DECLARE_DELAYED_WORK(addr_chk_work
, addrconf_verify_work
);
144 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
145 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
147 static void addrconf_type_change(struct net_device
*dev
,
148 unsigned long event
);
149 static int addrconf_ifdown(struct net_device
*dev
, int how
);
151 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
153 const struct net_device
*dev
,
154 u32 flags
, u32 noflags
);
156 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
157 static void addrconf_dad_work(struct work_struct
*w
);
158 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
159 static void addrconf_dad_run(struct inet6_dev
*idev
);
160 static void addrconf_rs_timer(unsigned long data
);
161 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
162 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
164 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
165 struct prefix_info
*pinfo
);
166 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
167 struct net_device
*dev
);
169 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
171 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
172 .mtu6
= IPV6_MIN_MTU
,
174 .accept_redirects
= 1,
176 .force_mld_version
= 0,
177 .mldv1_unsolicited_report_interval
= 10 * HZ
,
178 .mldv2_unsolicited_report_interval
= HZ
,
180 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
181 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
182 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
184 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
185 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
186 .regen_max_retry
= REGEN_MAX_RETRY
,
187 .max_desync_factor
= MAX_DESYNC_FACTOR
,
188 .max_addresses
= IPV6_MAX_ADDRESSES
,
189 .accept_ra_defrtr
= 1,
190 .accept_ra_from_local
= 0,
191 .accept_ra_pinfo
= 1,
192 #ifdef CONFIG_IPV6_ROUTER_PREF
193 .accept_ra_rtr_pref
= 1,
194 .rtr_probe_interval
= 60 * HZ
,
195 #ifdef CONFIG_IPV6_ROUTE_INFO
196 .accept_ra_rt_info_max_plen
= 0,
200 .accept_source_route
= 0, /* we do not accept RH0 by default. */
203 .suppress_frag_ndisc
= 1,
207 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
209 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
210 .mtu6
= IPV6_MIN_MTU
,
212 .accept_redirects
= 1,
214 .force_mld_version
= 0,
215 .mldv1_unsolicited_report_interval
= 10 * HZ
,
216 .mldv2_unsolicited_report_interval
= HZ
,
218 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
219 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
220 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
222 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
223 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
224 .regen_max_retry
= REGEN_MAX_RETRY
,
225 .max_desync_factor
= MAX_DESYNC_FACTOR
,
226 .max_addresses
= IPV6_MAX_ADDRESSES
,
227 .accept_ra_defrtr
= 1,
228 .accept_ra_from_local
= 0,
229 .accept_ra_pinfo
= 1,
230 #ifdef CONFIG_IPV6_ROUTER_PREF
231 .accept_ra_rtr_pref
= 1,
232 .rtr_probe_interval
= 60 * HZ
,
233 #ifdef CONFIG_IPV6_ROUTE_INFO
234 .accept_ra_rt_info_max_plen
= 0,
238 .accept_source_route
= 0, /* we do not accept RH0 by default. */
241 .suppress_frag_ndisc
= 1,
245 /* Check if a valid qdisc is available */
246 static inline bool addrconf_qdisc_ok(const struct net_device
*dev
)
248 return !qdisc_tx_is_noop(dev
);
251 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
253 if (del_timer(&idev
->rs_timer
))
257 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
259 if (cancel_delayed_work(&ifp
->dad_work
))
263 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
266 if (!timer_pending(&idev
->rs_timer
))
268 mod_timer(&idev
->rs_timer
, jiffies
+ when
);
271 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
274 if (!delayed_work_pending(&ifp
->dad_work
))
276 mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
);
279 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
283 idev
->stats
.ipv6
= alloc_percpu(struct ipstats_mib
);
284 if (!idev
->stats
.ipv6
)
287 for_each_possible_cpu(i
) {
288 struct ipstats_mib
*addrconf_stats
;
289 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
290 u64_stats_init(&addrconf_stats
->syncp
);
294 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
296 if (!idev
->stats
.icmpv6dev
)
298 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
300 if (!idev
->stats
.icmpv6msgdev
)
306 kfree(idev
->stats
.icmpv6dev
);
308 free_percpu(idev
->stats
.ipv6
);
313 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
315 struct inet6_dev
*ndev
;
320 if (dev
->mtu
< IPV6_MIN_MTU
)
321 return ERR_PTR(-EINVAL
);
323 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
327 rwlock_init(&ndev
->lock
);
329 INIT_LIST_HEAD(&ndev
->addr_list
);
330 setup_timer(&ndev
->rs_timer
, addrconf_rs_timer
,
331 (unsigned long)ndev
);
332 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
333 ndev
->cnf
.mtu6
= dev
->mtu
;
334 ndev
->cnf
.sysctl
= NULL
;
335 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
336 if (ndev
->nd_parms
== NULL
) {
340 if (ndev
->cnf
.forwarding
)
341 dev_disable_lro(dev
);
342 /* We refer to the device */
345 if (snmp6_alloc_dev(ndev
) < 0) {
347 "%s: cannot allocate memory for statistics; dev=%s.\n",
348 __func__
, dev
->name
);
349 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
355 if (snmp6_register_dev(ndev
) < 0) {
357 "%s: cannot create /proc/net/dev_snmp6/%s\n",
358 __func__
, dev
->name
);
362 /* One reference from device. We must do this before
363 * we invoke __ipv6_regen_rndid().
367 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
368 ndev
->cnf
.accept_dad
= -1;
370 #if IS_ENABLED(CONFIG_IPV6_SIT)
371 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
372 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
373 ndev
->cnf
.rtr_solicits
= 0;
377 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
378 setup_timer(&ndev
->regen_timer
, ipv6_regen_rndid
, (unsigned long)ndev
);
379 if ((dev
->flags
&IFF_LOOPBACK
) ||
380 dev
->type
== ARPHRD_TUNNEL
||
381 dev
->type
== ARPHRD_TUNNEL6
||
382 dev
->type
== ARPHRD_SIT
||
383 dev
->type
== ARPHRD_NONE
) {
384 ndev
->cnf
.use_tempaddr
= -1;
387 ipv6_regen_rndid((unsigned long) ndev
);
390 ndev
->token
= in6addr_any
;
392 if (netif_running(dev
) && addrconf_qdisc_ok(dev
))
393 ndev
->if_flags
|= IF_READY
;
395 ipv6_mc_init_dev(ndev
);
396 ndev
->tstamp
= jiffies
;
397 err
= addrconf_sysctl_register(ndev
);
399 ipv6_mc_destroy_dev(ndev
);
400 del_timer(&ndev
->regen_timer
);
403 /* protected by rtnl_lock */
404 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
406 /* Join interface-local all-node multicast group */
407 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
409 /* Join all-node multicast group */
410 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
412 /* Join all-router multicast group if forwarding is set */
413 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
414 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
419 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
421 in6_dev_finish_destroy(ndev
);
425 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
427 struct inet6_dev
*idev
;
431 idev
= __in6_dev_get(dev
);
433 idev
= ipv6_add_dev(dev
);
438 if (dev
->flags
&IFF_UP
)
443 static int inet6_netconf_msgsize_devconf(int type
)
445 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
446 + nla_total_size(4); /* NETCONFA_IFINDEX */
448 /* type -1 is used for ALL */
449 if (type
== -1 || type
== NETCONFA_FORWARDING
)
450 size
+= nla_total_size(4);
451 #ifdef CONFIG_IPV6_MROUTE
452 if (type
== -1 || type
== NETCONFA_MC_FORWARDING
)
453 size
+= nla_total_size(4);
455 if (type
== -1 || type
== NETCONFA_PROXY_NEIGH
)
456 size
+= nla_total_size(4);
461 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
462 struct ipv6_devconf
*devconf
, u32 portid
,
463 u32 seq
, int event
, unsigned int flags
,
466 struct nlmsghdr
*nlh
;
467 struct netconfmsg
*ncm
;
469 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
474 ncm
= nlmsg_data(nlh
);
475 ncm
->ncm_family
= AF_INET6
;
477 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
478 goto nla_put_failure
;
480 /* type -1 is used for ALL */
481 if ((type
== -1 || type
== NETCONFA_FORWARDING
) &&
482 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
483 goto nla_put_failure
;
484 #ifdef CONFIG_IPV6_MROUTE
485 if ((type
== -1 || type
== NETCONFA_MC_FORWARDING
) &&
486 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
487 devconf
->mc_forwarding
) < 0)
488 goto nla_put_failure
;
490 if ((type
== -1 || type
== NETCONFA_PROXY_NEIGH
) &&
491 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
492 goto nla_put_failure
;
498 nlmsg_cancel(skb
, nlh
);
502 void inet6_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
503 struct ipv6_devconf
*devconf
)
508 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_ATOMIC
);
512 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
513 RTM_NEWNETCONF
, 0, type
);
515 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
516 WARN_ON(err
== -EMSGSIZE
);
520 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_ATOMIC
);
523 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
526 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
527 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
528 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
529 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
532 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
533 struct nlmsghdr
*nlh
)
535 struct net
*net
= sock_net(in_skb
->sk
);
536 struct nlattr
*tb
[NETCONFA_MAX
+1];
537 struct netconfmsg
*ncm
;
539 struct ipv6_devconf
*devconf
;
540 struct inet6_dev
*in6_dev
;
541 struct net_device
*dev
;
545 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
546 devconf_ipv6_policy
);
551 if (!tb
[NETCONFA_IFINDEX
])
554 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
556 case NETCONFA_IFINDEX_ALL
:
557 devconf
= net
->ipv6
.devconf_all
;
559 case NETCONFA_IFINDEX_DEFAULT
:
560 devconf
= net
->ipv6
.devconf_dflt
;
563 dev
= __dev_get_by_index(net
, ifindex
);
566 in6_dev
= __in6_dev_get(dev
);
569 devconf
= &in6_dev
->cnf
;
574 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC
);
578 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
579 NETLINK_CB(in_skb
).portid
,
580 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
583 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
584 WARN_ON(err
== -EMSGSIZE
);
588 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
593 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
594 struct netlink_callback
*cb
)
596 struct net
*net
= sock_net(skb
->sk
);
599 struct net_device
*dev
;
600 struct inet6_dev
*idev
;
601 struct hlist_head
*head
;
604 s_idx
= idx
= cb
->args
[1];
606 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
608 head
= &net
->dev_index_head
[h
];
610 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
612 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
615 idev
= __in6_dev_get(dev
);
619 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
621 NETLINK_CB(cb
->skb
).portid
,
629 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
635 if (h
== NETDEV_HASHENTRIES
) {
636 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
637 net
->ipv6
.devconf_all
,
638 NETLINK_CB(cb
->skb
).portid
,
640 RTM_NEWNETCONF
, NLM_F_MULTI
,
646 if (h
== NETDEV_HASHENTRIES
+ 1) {
647 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
648 net
->ipv6
.devconf_dflt
,
649 NETLINK_CB(cb
->skb
).portid
,
651 RTM_NEWNETCONF
, NLM_F_MULTI
,
665 static void dev_forward_change(struct inet6_dev
*idev
)
667 struct net_device
*dev
;
668 struct inet6_ifaddr
*ifa
;
673 if (idev
->cnf
.forwarding
)
674 dev_disable_lro(dev
);
675 if (dev
->flags
& IFF_MULTICAST
) {
676 if (idev
->cnf
.forwarding
) {
677 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
678 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
679 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
681 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
682 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
683 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
687 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
688 if (ifa
->flags
&IFA_F_TENTATIVE
)
690 if (idev
->cnf
.forwarding
)
691 addrconf_join_anycast(ifa
);
693 addrconf_leave_anycast(ifa
);
695 inet6_netconf_notify_devconf(dev_net(dev
), NETCONFA_FORWARDING
,
696 dev
->ifindex
, &idev
->cnf
);
700 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
702 struct net_device
*dev
;
703 struct inet6_dev
*idev
;
705 for_each_netdev(net
, dev
) {
706 idev
= __in6_dev_get(dev
);
708 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
709 idev
->cnf
.forwarding
= newf
;
711 dev_forward_change(idev
);
716 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
722 return restart_syscall();
724 net
= (struct net
*)table
->extra2
;
728 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
729 if ((!newf
) ^ (!old
))
730 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
731 NETCONFA_IFINDEX_DEFAULT
,
732 net
->ipv6
.devconf_dflt
);
737 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
738 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
739 addrconf_forward_change(net
, newf
);
740 if ((!newf
) ^ (!old
))
741 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
742 NETCONFA_IFINDEX_ALL
,
743 net
->ipv6
.devconf_all
);
744 } else if ((!newf
) ^ (!old
))
745 dev_forward_change((struct inet6_dev
*)table
->extra1
);
749 rt6_purge_dflt_routers(net
);
754 /* Nobody refers to this ifaddr, destroy it */
755 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
757 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
759 #ifdef NET_REFCNT_DEBUG
760 pr_debug("%s\n", __func__
);
763 in6_dev_put(ifp
->idev
);
765 if (cancel_delayed_work(&ifp
->dad_work
))
766 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
769 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
770 pr_warn("Freeing alive inet6 address %p\n", ifp
);
779 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
782 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
785 * Each device address list is sorted in order of scope -
786 * global before linklocal.
788 list_for_each(p
, &idev
->addr_list
) {
789 struct inet6_ifaddr
*ifa
790 = list_entry(p
, struct inet6_ifaddr
, if_list
);
791 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
795 list_add_tail(&ifp
->if_list
, p
);
798 static u32
inet6_addr_hash(const struct in6_addr
*addr
)
800 return hash_32(ipv6_addr_hash(addr
), IN6_ADDR_HSIZE_SHIFT
);
803 /* On success it returns ifp with increased reference count */
805 static struct inet6_ifaddr
*
806 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
807 const struct in6_addr
*peer_addr
, int pfxlen
,
808 int scope
, u32 flags
, u32 valid_lft
, u32 prefered_lft
)
810 struct inet6_ifaddr
*ifa
= NULL
;
814 int addr_type
= ipv6_addr_type(addr
);
816 if (addr_type
== IPV6_ADDR_ANY
||
817 addr_type
& IPV6_ADDR_MULTICAST
||
818 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
819 addr_type
& IPV6_ADDR_LOOPBACK
))
820 return ERR_PTR(-EADDRNOTAVAIL
);
824 err
= -ENODEV
; /*XXX*/
828 if (idev
->cnf
.disable_ipv6
) {
833 spin_lock(&addrconf_hash_lock
);
835 /* Ignore adding duplicate addresses on an interface */
836 if (ipv6_chk_same_addr(dev_net(idev
->dev
), addr
, idev
->dev
)) {
837 ADBG("ipv6_add_addr: already assigned\n");
842 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
845 ADBG("ipv6_add_addr: malloc failed\n");
850 rt
= addrconf_dst_alloc(idev
, addr
, false);
856 neigh_parms_data_state_setall(idev
->nd_parms
);
860 ifa
->peer_addr
= *peer_addr
;
862 spin_lock_init(&ifa
->lock
);
863 spin_lock_init(&ifa
->state_lock
);
864 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
865 INIT_HLIST_NODE(&ifa
->addr_lst
);
867 ifa
->prefix_len
= pfxlen
;
868 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
869 ifa
->valid_lft
= valid_lft
;
870 ifa
->prefered_lft
= prefered_lft
;
871 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
872 ifa
->tokenized
= false;
881 /* Add to big hash table */
882 hash
= inet6_addr_hash(addr
);
884 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
885 spin_unlock(&addrconf_hash_lock
);
887 write_lock(&idev
->lock
);
888 /* Add to inet6_dev unicast addr list. */
889 ipv6_link_dev_addr(idev
, ifa
);
891 if (ifa
->flags
&IFA_F_TEMPORARY
) {
892 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
897 write_unlock(&idev
->lock
);
899 rcu_read_unlock_bh();
901 if (likely(err
== 0))
902 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
910 spin_unlock(&addrconf_hash_lock
);
914 enum cleanup_prefix_rt_t
{
915 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
916 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
917 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
921 * Check, whether the prefix for ifp would still need a prefix route
922 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
925 * 1) we don't purge prefix if address was not permanent.
926 * prefix is managed by its own lifetime.
927 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
928 * 3) if there are no addresses, delete prefix.
929 * 4) if there are still other permanent address(es),
930 * corresponding prefix is still permanent.
931 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
932 * don't purge the prefix, assume user space is managing it.
933 * 6) otherwise, update prefix lifetime to the
934 * longest valid lifetime among the corresponding
935 * addresses on the device.
936 * Note: subsequent RA will update lifetime.
938 static enum cleanup_prefix_rt_t
939 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
941 struct inet6_ifaddr
*ifa
;
942 struct inet6_dev
*idev
= ifp
->idev
;
943 unsigned long lifetime
;
944 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
948 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
951 if (!ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
954 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
955 return CLEANUP_PREFIX_RT_NOP
;
957 action
= CLEANUP_PREFIX_RT_EXPIRE
;
959 spin_lock(&ifa
->lock
);
961 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
963 * Note: Because this address is
964 * not permanent, lifetime <
965 * LONG_MAX / HZ here.
967 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
968 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
969 spin_unlock(&ifa
->lock
);
976 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
, bool del_rt
)
980 rt
= addrconf_get_prefix_route(&ifp
->addr
,
983 0, RTF_GATEWAY
| RTF_DEFAULT
);
988 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
989 rt6_set_expires(rt
, expires
);
996 /* This function wants to get referenced ifp and releases it before return */
998 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1001 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1002 unsigned long expires
;
1006 spin_lock_bh(&ifp
->state_lock
);
1008 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1009 spin_unlock_bh(&ifp
->state_lock
);
1011 if (state
== INET6_IFADDR_STATE_DEAD
)
1014 spin_lock_bh(&addrconf_hash_lock
);
1015 hlist_del_init_rcu(&ifp
->addr_lst
);
1016 spin_unlock_bh(&addrconf_hash_lock
);
1018 write_lock_bh(&ifp
->idev
->lock
);
1020 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1021 list_del(&ifp
->tmp_list
);
1023 in6_ifa_put(ifp
->ifpub
);
1029 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1030 action
= check_cleanup_prefix_route(ifp
, &expires
);
1032 list_del_init(&ifp
->if_list
);
1035 write_unlock_bh(&ifp
->idev
->lock
);
1037 addrconf_del_dad_work(ifp
);
1039 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1041 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1043 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1044 cleanup_prefix_route(ifp
, expires
,
1045 action
== CLEANUP_PREFIX_RT_DEL
);
1048 /* clean up prefsrc entries */
1049 rt6_remove_prefsrc(ifp
);
1054 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
1056 struct inet6_dev
*idev
= ifp
->idev
;
1057 struct in6_addr addr
, *tmpaddr
;
1058 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_tstamp
, age
;
1059 unsigned long regen_advance
;
1063 unsigned long now
= jiffies
;
1065 write_lock_bh(&idev
->lock
);
1067 spin_lock_bh(&ift
->lock
);
1068 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1069 spin_unlock_bh(&ift
->lock
);
1076 if (idev
->cnf
.use_tempaddr
<= 0) {
1077 write_unlock_bh(&idev
->lock
);
1078 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1083 spin_lock_bh(&ifp
->lock
);
1084 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1085 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1086 spin_unlock_bh(&ifp
->lock
);
1087 write_unlock_bh(&idev
->lock
);
1088 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1095 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1096 __ipv6_try_regen_rndid(idev
, tmpaddr
);
1097 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1098 age
= (now
- ifp
->tstamp
) / HZ
;
1099 tmp_valid_lft
= min_t(__u32
,
1101 idev
->cnf
.temp_valid_lft
+ age
);
1102 tmp_prefered_lft
= min_t(__u32
,
1104 idev
->cnf
.temp_prefered_lft
+ age
-
1105 idev
->cnf
.max_desync_factor
);
1106 tmp_plen
= ifp
->prefix_len
;
1107 tmp_tstamp
= ifp
->tstamp
;
1108 spin_unlock_bh(&ifp
->lock
);
1110 regen_advance
= idev
->cnf
.regen_max_retry
*
1111 idev
->cnf
.dad_transmits
*
1112 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) / HZ
;
1113 write_unlock_bh(&idev
->lock
);
1115 /* A temporary address is created only if this calculated Preferred
1116 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1117 * an implementation must not create a temporary address with a zero
1118 * Preferred Lifetime.
1119 * Use age calculation as in addrconf_verify to avoid unnecessary
1120 * temporary addresses being generated.
1122 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1123 if (tmp_prefered_lft
<= regen_advance
+ age
) {
1130 addr_flags
= IFA_F_TEMPORARY
;
1131 /* set in addrconf_prefix_rcv() */
1132 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1133 addr_flags
|= IFA_F_OPTIMISTIC
;
1135 ift
= ipv6_add_addr(idev
, &addr
, NULL
, tmp_plen
,
1136 ipv6_addr_scope(&addr
), addr_flags
,
1137 tmp_valid_lft
, tmp_prefered_lft
);
1141 pr_info("%s: retry temporary address regeneration\n", __func__
);
1143 write_lock_bh(&idev
->lock
);
1147 spin_lock_bh(&ift
->lock
);
1150 ift
->tstamp
= tmp_tstamp
;
1151 spin_unlock_bh(&ift
->lock
);
1153 addrconf_dad_start(ift
);
1161 * Choose an appropriate source address (RFC3484)
1164 IPV6_SADDR_RULE_INIT
= 0,
1165 IPV6_SADDR_RULE_LOCAL
,
1166 IPV6_SADDR_RULE_SCOPE
,
1167 IPV6_SADDR_RULE_PREFERRED
,
1168 #ifdef CONFIG_IPV6_MIP6
1169 IPV6_SADDR_RULE_HOA
,
1171 IPV6_SADDR_RULE_OIF
,
1172 IPV6_SADDR_RULE_LABEL
,
1173 IPV6_SADDR_RULE_PRIVACY
,
1174 IPV6_SADDR_RULE_ORCHID
,
1175 IPV6_SADDR_RULE_PREFIX
,
1176 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1177 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1182 struct ipv6_saddr_score
{
1185 struct inet6_ifaddr
*ifa
;
1186 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1191 struct ipv6_saddr_dst
{
1192 const struct in6_addr
*addr
;
1199 static inline int ipv6_saddr_preferred(int type
)
1201 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1206 static inline bool ipv6_use_optimistic_addr(struct inet6_dev
*idev
)
1208 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1209 return idev
&& idev
->cnf
.optimistic_dad
&& idev
->cnf
.use_optimistic
;
1215 static int ipv6_get_saddr_eval(struct net
*net
,
1216 struct ipv6_saddr_score
*score
,
1217 struct ipv6_saddr_dst
*dst
,
1222 if (i
<= score
->rule
) {
1224 case IPV6_SADDR_RULE_SCOPE
:
1225 ret
= score
->scopedist
;
1227 case IPV6_SADDR_RULE_PREFIX
:
1228 ret
= score
->matchlen
;
1231 ret
= !!test_bit(i
, score
->scorebits
);
1237 case IPV6_SADDR_RULE_INIT
:
1238 /* Rule 0: remember if hiscore is not ready yet */
1241 case IPV6_SADDR_RULE_LOCAL
:
1242 /* Rule 1: Prefer same address */
1243 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1245 case IPV6_SADDR_RULE_SCOPE
:
1246 /* Rule 2: Prefer appropriate scope
1251 * ---+--+-+---> scope
1253 * | d is scope of the destination.
1255 * | \ <- smaller scope is better if
1256 * B-15 | \ if scope is enough for destination.
1257 * | ret = B - scope (-1 <= scope >= d <= 15).
1259 * |/ <- greater is better
1260 * -C / if scope is not enough for destination.
1261 * /| ret = scope - C (-1 <= d < scope <= 15).
1263 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1264 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1265 * Assume B = 0 and we get C > 29.
1267 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1268 if (ret
>= dst
->scope
)
1271 ret
-= 128; /* 30 is enough */
1272 score
->scopedist
= ret
;
1274 case IPV6_SADDR_RULE_PREFERRED
:
1276 /* Rule 3: Avoid deprecated and optimistic addresses */
1277 u8 avoid
= IFA_F_DEPRECATED
;
1279 if (!ipv6_use_optimistic_addr(score
->ifa
->idev
))
1280 avoid
|= IFA_F_OPTIMISTIC
;
1281 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1282 !(score
->ifa
->flags
& avoid
);
1285 #ifdef CONFIG_IPV6_MIP6
1286 case IPV6_SADDR_RULE_HOA
:
1288 /* Rule 4: Prefer home address */
1289 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1290 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1294 case IPV6_SADDR_RULE_OIF
:
1295 /* Rule 5: Prefer outgoing interface */
1296 ret
= (!dst
->ifindex
||
1297 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1299 case IPV6_SADDR_RULE_LABEL
:
1300 /* Rule 6: Prefer matching label */
1301 ret
= ipv6_addr_label(net
,
1302 &score
->ifa
->addr
, score
->addr_type
,
1303 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1305 case IPV6_SADDR_RULE_PRIVACY
:
1307 /* Rule 7: Prefer public address
1308 * Note: prefer temporary address if use_tempaddr >= 2
1310 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1311 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1312 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1313 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1316 case IPV6_SADDR_RULE_ORCHID
:
1317 /* Rule 8-: Prefer ORCHID vs ORCHID or
1318 * non-ORCHID vs non-ORCHID
1320 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1321 ipv6_addr_orchid(dst
->addr
));
1323 case IPV6_SADDR_RULE_PREFIX
:
1324 /* Rule 8: Use longest matching prefix */
1325 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1326 if (ret
> score
->ifa
->prefix_len
)
1327 ret
= score
->ifa
->prefix_len
;
1328 score
->matchlen
= ret
;
1330 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1331 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1332 /* Optimistic addresses still have lower precedence than other
1333 * preferred addresses.
1335 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1343 __set_bit(i
, score
->scorebits
);
1349 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1350 const struct in6_addr
*daddr
, unsigned int prefs
,
1351 struct in6_addr
*saddr
)
1353 struct ipv6_saddr_score scores
[2],
1354 *score
= &scores
[0], *hiscore
= &scores
[1];
1355 struct ipv6_saddr_dst dst
;
1356 struct net_device
*dev
;
1359 dst_type
= __ipv6_addr_type(daddr
);
1361 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1362 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1363 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1367 hiscore
->ifa
= NULL
;
1371 for_each_netdev_rcu(net
, dev
) {
1372 struct inet6_dev
*idev
;
1374 /* Candidate Source Address (section 4)
1375 * - multicast and link-local destination address,
1376 * the set of candidate source address MUST only
1377 * include addresses assigned to interfaces
1378 * belonging to the same link as the outgoing
1380 * (- For site-local destination addresses, the
1381 * set of candidate source addresses MUST only
1382 * include addresses assigned to interfaces
1383 * belonging to the same site as the outgoing
1386 if (((dst_type
& IPV6_ADDR_MULTICAST
) ||
1387 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
1388 dst
.ifindex
&& dev
->ifindex
!= dst
.ifindex
)
1391 idev
= __in6_dev_get(dev
);
1395 read_lock_bh(&idev
->lock
);
1396 list_for_each_entry(score
->ifa
, &idev
->addr_list
, if_list
) {
1400 * - Tentative Address (RFC2462 section 5.4)
1401 * - A tentative address is not considered
1402 * "assigned to an interface" in the traditional
1403 * sense, unless it is also flagged as optimistic.
1404 * - Candidate Source Address (section 4)
1405 * - In any case, anycast addresses, multicast
1406 * addresses, and the unspecified address MUST
1407 * NOT be included in a candidate set.
1409 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1410 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1413 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1415 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1416 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1417 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1423 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1425 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1426 int minihiscore
, miniscore
;
1428 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, &dst
, i
);
1429 miniscore
= ipv6_get_saddr_eval(net
, score
, &dst
, i
);
1431 if (minihiscore
> miniscore
) {
1432 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1433 score
->scopedist
> 0) {
1436 * each remaining entry
1437 * has too small (not enough)
1438 * scope, because ifa entries
1439 * are sorted by their scope
1445 } else if (minihiscore
< miniscore
) {
1447 in6_ifa_put(hiscore
->ifa
);
1449 in6_ifa_hold(score
->ifa
);
1451 swap(hiscore
, score
);
1453 /* restore our iterator */
1454 score
->ifa
= hiscore
->ifa
;
1461 read_unlock_bh(&idev
->lock
);
1466 return -EADDRNOTAVAIL
;
1468 *saddr
= hiscore
->ifa
->addr
;
1469 in6_ifa_put(hiscore
->ifa
);
1472 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1474 int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1477 struct inet6_ifaddr
*ifp
;
1478 int err
= -EADDRNOTAVAIL
;
1480 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1481 if (ifp
->scope
> IFA_LINK
)
1483 if (ifp
->scope
== IFA_LINK
&&
1484 !(ifp
->flags
& banned_flags
)) {
1493 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1496 struct inet6_dev
*idev
;
1497 int err
= -EADDRNOTAVAIL
;
1500 idev
= __in6_dev_get(dev
);
1502 read_lock_bh(&idev
->lock
);
1503 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1504 read_unlock_bh(&idev
->lock
);
1510 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1513 struct inet6_ifaddr
*ifp
;
1515 read_lock_bh(&idev
->lock
);
1516 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
)
1518 read_unlock_bh(&idev
->lock
);
1522 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1523 const struct net_device
*dev
, int strict
)
1525 return ipv6_chk_addr_and_flags(net
, addr
, dev
, strict
, IFA_F_TENTATIVE
);
1527 EXPORT_SYMBOL(ipv6_chk_addr
);
1529 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1530 const struct net_device
*dev
, int strict
,
1533 struct inet6_ifaddr
*ifp
;
1534 unsigned int hash
= inet6_addr_hash(addr
);
1538 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1539 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1541 /* Decouple optimistic from tentative for evaluation here.
1542 * Ban optimistic addresses explicitly, when required.
1544 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1545 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1547 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1548 !(ifp_flags
&banned_flags
) &&
1549 (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1550 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1551 rcu_read_unlock_bh();
1556 rcu_read_unlock_bh();
1559 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1561 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1562 struct net_device
*dev
)
1564 unsigned int hash
= inet6_addr_hash(addr
);
1565 struct inet6_ifaddr
*ifp
;
1567 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1568 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1570 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1571 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1578 /* Compares an address/prefix_len with addresses on device @dev.
1579 * If one is found it returns true.
1581 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1582 const unsigned int prefix_len
, struct net_device
*dev
)
1584 struct inet6_dev
*idev
;
1585 struct inet6_ifaddr
*ifa
;
1589 idev
= __in6_dev_get(dev
);
1591 read_lock_bh(&idev
->lock
);
1592 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1593 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
1597 read_unlock_bh(&idev
->lock
);
1603 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
1605 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1607 struct inet6_dev
*idev
;
1608 struct inet6_ifaddr
*ifa
;
1613 idev
= __in6_dev_get(dev
);
1615 read_lock_bh(&idev
->lock
);
1616 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1617 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1622 read_unlock_bh(&idev
->lock
);
1627 EXPORT_SYMBOL(ipv6_chk_prefix
);
1629 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1630 struct net_device
*dev
, int strict
)
1632 struct inet6_ifaddr
*ifp
, *result
= NULL
;
1633 unsigned int hash
= inet6_addr_hash(addr
);
1636 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1637 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1639 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1640 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1641 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1648 rcu_read_unlock_bh();
1653 /* Gets referenced address, destroys ifaddr */
1655 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
1657 if (ifp
->flags
&IFA_F_PERMANENT
) {
1658 spin_lock_bh(&ifp
->lock
);
1659 addrconf_del_dad_work(ifp
);
1660 ifp
->flags
|= IFA_F_TENTATIVE
;
1662 ifp
->flags
|= IFA_F_DADFAILED
;
1663 spin_unlock_bh(&ifp
->lock
);
1665 ipv6_ifa_notify(0, ifp
);
1667 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1668 struct inet6_ifaddr
*ifpub
;
1669 spin_lock_bh(&ifp
->lock
);
1672 in6_ifa_hold(ifpub
);
1673 spin_unlock_bh(&ifp
->lock
);
1674 ipv6_create_tempaddr(ifpub
, ifp
);
1677 spin_unlock_bh(&ifp
->lock
);
1685 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
1689 spin_lock_bh(&ifp
->state_lock
);
1690 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
1691 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
1694 spin_unlock_bh(&ifp
->state_lock
);
1699 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1701 struct inet6_dev
*idev
= ifp
->idev
;
1703 if (addrconf_dad_end(ifp
)) {
1708 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1709 ifp
->idev
->dev
->name
, &ifp
->addr
);
1711 if (idev
->cnf
.accept_dad
> 1 && !idev
->cnf
.disable_ipv6
) {
1712 struct in6_addr addr
;
1714 addr
.s6_addr32
[0] = htonl(0xfe800000);
1715 addr
.s6_addr32
[1] = 0;
1717 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
1718 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
1719 /* DAD failed for link-local based on MAC address */
1720 idev
->cnf
.disable_ipv6
= 1;
1722 pr_info("%s: IPv6 being disabled!\n",
1723 ifp
->idev
->dev
->name
);
1727 spin_lock_bh(&ifp
->state_lock
);
1728 /* transition from _POSTDAD to _ERRDAD */
1729 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
1730 spin_unlock_bh(&ifp
->state_lock
);
1732 addrconf_mod_dad_work(ifp
, 0);
1735 /* Join to solicited addr multicast group.
1736 * caller must hold RTNL */
1737 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
1739 struct in6_addr maddr
;
1741 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1744 addrconf_addr_solict_mult(addr
, &maddr
);
1745 ipv6_dev_mc_inc(dev
, &maddr
);
1748 /* caller must hold RTNL */
1749 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
1751 struct in6_addr maddr
;
1753 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1756 addrconf_addr_solict_mult(addr
, &maddr
);
1757 __ipv6_dev_mc_dec(idev
, &maddr
);
1760 /* caller must hold RTNL */
1761 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1763 struct in6_addr addr
;
1765 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1767 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1768 if (ipv6_addr_any(&addr
))
1770 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
1773 /* caller must hold RTNL */
1774 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1776 struct in6_addr addr
;
1778 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1780 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1781 if (ipv6_addr_any(&addr
))
1783 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1786 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1788 if (dev
->addr_len
!= ETH_ALEN
)
1790 memcpy(eui
, dev
->dev_addr
, 3);
1791 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1794 * The zSeries OSA network cards can be shared among various
1795 * OS instances, but the OSA cards have only one MAC address.
1796 * This leads to duplicate address conflicts in conjunction
1797 * with IPv6 if more than one instance uses the same card.
1799 * The driver for these cards can deliver a unique 16-bit
1800 * identifier for each instance sharing the same card. It is
1801 * placed instead of 0xFFFE in the interface identifier. The
1802 * "u" bit of the interface identifier is not inverted in this
1803 * case. Hence the resulting interface identifier has local
1804 * scope according to RFC2373.
1807 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1808 eui
[4] = dev
->dev_id
& 0xFF;
1817 static int addrconf_ifid_eui64(u8
*eui
, struct net_device
*dev
)
1819 if (dev
->addr_len
!= IEEE802154_ADDR_LEN
)
1821 memcpy(eui
, dev
->dev_addr
, 8);
1826 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
1828 union fwnet_hwaddr
*ha
;
1830 if (dev
->addr_len
!= FWNET_ALEN
)
1833 ha
= (union fwnet_hwaddr
*)dev
->dev_addr
;
1835 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
1840 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1842 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1843 if (dev
->addr_len
!= ARCNET_ALEN
)
1846 eui
[7] = *(u8
*)dev
->dev_addr
;
1850 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1852 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1854 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1859 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
1863 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
1864 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
1865 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
1866 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
1867 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
1868 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
1872 memcpy(eui
+ 4, &addr
, 4);
1876 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
1878 if (dev
->priv_flags
& IFF_ISATAP
)
1879 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1883 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
1885 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1888 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
1890 memcpy(eui
, dev
->perm_addr
, 3);
1891 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
1898 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1900 switch (dev
->type
) {
1903 return addrconf_ifid_eui48(eui
, dev
);
1905 return addrconf_ifid_arcnet(eui
, dev
);
1906 case ARPHRD_INFINIBAND
:
1907 return addrconf_ifid_infiniband(eui
, dev
);
1909 return addrconf_ifid_sit(eui
, dev
);
1911 return addrconf_ifid_gre(eui
, dev
);
1912 case ARPHRD_6LOWPAN
:
1913 case ARPHRD_IEEE802154
:
1914 return addrconf_ifid_eui64(eui
, dev
);
1915 case ARPHRD_IEEE1394
:
1916 return addrconf_ifid_ieee1394(eui
, dev
);
1917 case ARPHRD_TUNNEL6
:
1918 return addrconf_ifid_ip6tnl(eui
, dev
);
1923 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1926 struct inet6_ifaddr
*ifp
;
1928 read_lock_bh(&idev
->lock
);
1929 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1930 if (ifp
->scope
> IFA_LINK
)
1932 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1933 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1938 read_unlock_bh(&idev
->lock
);
1942 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1943 static void __ipv6_regen_rndid(struct inet6_dev
*idev
)
1946 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1947 idev
->rndid
[0] &= ~0x02;
1950 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1951 * check if generated address is not inappropriate
1953 * - Reserved subnet anycast (RFC 2526)
1954 * 11111101 11....11 1xxxxxxx
1955 * - ISATAP (RFC4214) 6.1
1956 * 00-00-5E-FE-xx-xx-xx-xx
1958 * - XXX: already assigned to an address on the device
1960 if (idev
->rndid
[0] == 0xfd &&
1961 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1962 (idev
->rndid
[7]&0x80))
1964 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1965 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1967 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1972 static void ipv6_regen_rndid(unsigned long data
)
1974 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1975 unsigned long expires
;
1978 write_lock_bh(&idev
->lock
);
1983 __ipv6_regen_rndid(idev
);
1986 idev
->cnf
.temp_prefered_lft
* HZ
-
1987 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
*
1988 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) -
1989 idev
->cnf
.max_desync_factor
* HZ
;
1990 if (time_before(expires
, jiffies
)) {
1991 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1992 __func__
, idev
->dev
->name
);
1996 if (!mod_timer(&idev
->regen_timer
, expires
))
2000 write_unlock_bh(&idev
->lock
);
2001 rcu_read_unlock_bh();
2005 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
2007 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
2008 __ipv6_regen_rndid(idev
);
2016 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
2017 unsigned long expires
, u32 flags
)
2019 struct fib6_config cfg
= {
2020 .fc_table
= RT6_TABLE_PREFIX
,
2021 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2022 .fc_ifindex
= dev
->ifindex
,
2023 .fc_expires
= expires
,
2025 .fc_flags
= RTF_UP
| flags
,
2026 .fc_nlinfo
.nl_net
= dev_net(dev
),
2027 .fc_protocol
= RTPROT_KERNEL
,
2032 /* Prevent useless cloning on PtP SIT.
2033 This thing is done here expecting that the whole
2034 class of non-broadcast devices need not cloning.
2036 #if IS_ENABLED(CONFIG_IPV6_SIT)
2037 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2038 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2041 ip6_route_add(&cfg
);
2045 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2047 const struct net_device
*dev
,
2048 u32 flags
, u32 noflags
)
2050 struct fib6_node
*fn
;
2051 struct rt6_info
*rt
= NULL
;
2052 struct fib6_table
*table
;
2054 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_PREFIX
);
2058 read_lock_bh(&table
->tb6_lock
);
2059 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0);
2062 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
2063 if (rt
->dst
.dev
->ifindex
!= dev
->ifindex
)
2065 if ((rt
->rt6i_flags
& flags
) != flags
)
2067 if ((rt
->rt6i_flags
& noflags
) != 0)
2073 read_unlock_bh(&table
->tb6_lock
);
2078 /* Create "default" multicast route to the interface */
2080 static void addrconf_add_mroute(struct net_device
*dev
)
2082 struct fib6_config cfg
= {
2083 .fc_table
= RT6_TABLE_LOCAL
,
2084 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2085 .fc_ifindex
= dev
->ifindex
,
2088 .fc_nlinfo
.nl_net
= dev_net(dev
),
2091 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2093 ip6_route_add(&cfg
);
2096 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2098 struct inet6_dev
*idev
;
2102 idev
= ipv6_find_idev(dev
);
2104 return ERR_PTR(-ENOBUFS
);
2106 if (idev
->cnf
.disable_ipv6
)
2107 return ERR_PTR(-EACCES
);
2109 /* Add default multicast route */
2110 if (!(dev
->flags
& IFF_LOOPBACK
))
2111 addrconf_add_mroute(dev
);
2116 static void manage_tempaddrs(struct inet6_dev
*idev
,
2117 struct inet6_ifaddr
*ifp
,
2118 __u32 valid_lft
, __u32 prefered_lft
,
2119 bool create
, unsigned long now
)
2122 struct inet6_ifaddr
*ift
;
2124 read_lock_bh(&idev
->lock
);
2125 /* update all temporary addresses in the list */
2126 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2127 int age
, max_valid
, max_prefered
;
2129 if (ifp
!= ift
->ifpub
)
2132 /* RFC 4941 section 3.3:
2133 * If a received option will extend the lifetime of a public
2134 * address, the lifetimes of temporary addresses should
2135 * be extended, subject to the overall constraint that no
2136 * temporary addresses should ever remain "valid" or "preferred"
2137 * for a time longer than (TEMP_VALID_LIFETIME) or
2138 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2140 age
= (now
- ift
->cstamp
) / HZ
;
2141 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2145 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2146 idev
->cnf
.max_desync_factor
- age
;
2147 if (max_prefered
< 0)
2150 if (valid_lft
> max_valid
)
2151 valid_lft
= max_valid
;
2153 if (prefered_lft
> max_prefered
)
2154 prefered_lft
= max_prefered
;
2156 spin_lock(&ift
->lock
);
2158 ift
->valid_lft
= valid_lft
;
2159 ift
->prefered_lft
= prefered_lft
;
2161 if (prefered_lft
> 0)
2162 ift
->flags
&= ~IFA_F_DEPRECATED
;
2164 spin_unlock(&ift
->lock
);
2165 if (!(flags
&IFA_F_TENTATIVE
))
2166 ipv6_ifa_notify(0, ift
);
2169 if ((create
|| list_empty(&idev
->tempaddr_list
)) &&
2170 idev
->cnf
.use_tempaddr
> 0) {
2171 /* When a new public address is created as described
2172 * in [ADDRCONF], also create a new temporary address.
2173 * Also create a temporary address if it's enabled but
2174 * no temporary address currently exists.
2176 read_unlock_bh(&idev
->lock
);
2177 ipv6_create_tempaddr(ifp
, NULL
);
2179 read_unlock_bh(&idev
->lock
);
2183 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2185 struct prefix_info
*pinfo
;
2189 struct inet6_dev
*in6_dev
;
2190 struct net
*net
= dev_net(dev
);
2192 pinfo
= (struct prefix_info
*) opt
;
2194 if (len
< sizeof(struct prefix_info
)) {
2195 ADBG("addrconf: prefix option too short\n");
2200 * Validation checks ([ADDRCONF], page 19)
2203 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2205 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2208 valid_lft
= ntohl(pinfo
->valid
);
2209 prefered_lft
= ntohl(pinfo
->prefered
);
2211 if (prefered_lft
> valid_lft
) {
2212 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2216 in6_dev
= in6_dev_get(dev
);
2218 if (in6_dev
== NULL
) {
2219 net_dbg_ratelimited("addrconf: device %s not configured\n",
2225 * Two things going on here:
2226 * 1) Add routes for on-link prefixes
2227 * 2) Configure prefixes with the auto flag set
2230 if (pinfo
->onlink
) {
2231 struct rt6_info
*rt
;
2232 unsigned long rt_expires
;
2234 /* Avoid arithmetic overflow. Really, we could
2235 * save rt_expires in seconds, likely valid_lft,
2236 * but it would require division in fib gc, that it
2240 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2242 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2244 if (addrconf_finite_timeout(rt_expires
))
2247 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2250 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2251 RTF_GATEWAY
| RTF_DEFAULT
);
2254 /* Autoconf prefix route */
2255 if (valid_lft
== 0) {
2258 } else if (addrconf_finite_timeout(rt_expires
)) {
2260 rt6_set_expires(rt
, jiffies
+ rt_expires
);
2262 rt6_clean_expires(rt
);
2264 } else if (valid_lft
) {
2265 clock_t expires
= 0;
2266 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2267 if (addrconf_finite_timeout(rt_expires
)) {
2269 flags
|= RTF_EXPIRES
;
2270 expires
= jiffies_to_clock_t(rt_expires
);
2272 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2273 dev
, expires
, flags
);
2278 /* Try to figure out our local address for this prefix */
2280 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2281 struct inet6_ifaddr
*ifp
;
2282 struct in6_addr addr
;
2283 int create
= 0, update_lft
= 0;
2284 bool tokenized
= false;
2286 if (pinfo
->prefix_len
== 64) {
2287 memcpy(&addr
, &pinfo
->prefix
, 8);
2289 if (!ipv6_addr_any(&in6_dev
->token
)) {
2290 read_lock_bh(&in6_dev
->lock
);
2291 memcpy(addr
.s6_addr
+ 8,
2292 in6_dev
->token
.s6_addr
+ 8, 8);
2293 read_unlock_bh(&in6_dev
->lock
);
2295 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2296 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2297 in6_dev_put(in6_dev
);
2302 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2304 in6_dev_put(in6_dev
);
2309 ifp
= ipv6_get_ifaddr(net
, &addr
, dev
, 1);
2311 if (ifp
== NULL
&& valid_lft
) {
2312 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2315 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2316 if (in6_dev
->cnf
.optimistic_dad
&&
2317 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2318 addr_flags
= IFA_F_OPTIMISTIC
;
2321 /* Do not allow to create too much of autoconfigured
2322 * addresses; this would be too easy way to crash kernel.
2324 if (!max_addresses
||
2325 ipv6_count_addresses(in6_dev
) < max_addresses
)
2326 ifp
= ipv6_add_addr(in6_dev
, &addr
, NULL
,
2328 addr_type
&IPV6_ADDR_SCOPE_MASK
,
2329 addr_flags
, valid_lft
,
2332 if (IS_ERR_OR_NULL(ifp
)) {
2333 in6_dev_put(in6_dev
);
2339 spin_lock_bh(&ifp
->lock
);
2340 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2341 ifp
->cstamp
= jiffies
;
2342 ifp
->tokenized
= tokenized
;
2343 spin_unlock_bh(&ifp
->lock
);
2344 addrconf_dad_start(ifp
);
2352 /* update lifetime (RFC2462 5.5.3 e) */
2353 spin_lock(&ifp
->lock
);
2355 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2356 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2359 if (!update_lft
&& !create
&& stored_lft
) {
2360 const u32 minimum_lft
= min_t(u32
,
2361 stored_lft
, MIN_VALID_LIFETIME
);
2362 valid_lft
= max(valid_lft
, minimum_lft
);
2364 /* RFC4862 Section 5.5.3e:
2365 * "Note that the preferred lifetime of the
2366 * corresponding address is always reset to
2367 * the Preferred Lifetime in the received
2368 * Prefix Information option, regardless of
2369 * whether the valid lifetime is also reset or
2372 * So we should always update prefered_lft here.
2378 ifp
->valid_lft
= valid_lft
;
2379 ifp
->prefered_lft
= prefered_lft
;
2382 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2383 spin_unlock(&ifp
->lock
);
2385 if (!(flags
&IFA_F_TENTATIVE
))
2386 ipv6_ifa_notify(0, ifp
);
2388 spin_unlock(&ifp
->lock
);
2390 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2397 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2398 in6_dev_put(in6_dev
);
2402 * Set destination address.
2403 * Special case for SIT interfaces where we create a new "virtual"
2406 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2408 struct in6_ifreq ireq
;
2409 struct net_device
*dev
;
2415 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2418 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2424 #if IS_ENABLED(CONFIG_IPV6_SIT)
2425 if (dev
->type
== ARPHRD_SIT
) {
2426 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2428 struct ip_tunnel_parm p
;
2430 err
= -EADDRNOTAVAIL
;
2431 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2434 memset(&p
, 0, sizeof(p
));
2435 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2439 p
.iph
.protocol
= IPPROTO_IPV6
;
2441 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2443 if (ops
->ndo_do_ioctl
) {
2444 mm_segment_t oldfs
= get_fs();
2447 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2454 dev
= __dev_get_by_name(net
, p
.name
);
2457 err
= dev_open(dev
);
2468 * Manual configuration of address on an interface
2470 static int inet6_addr_add(struct net
*net
, int ifindex
,
2471 const struct in6_addr
*pfx
,
2472 const struct in6_addr
*peer_pfx
,
2473 unsigned int plen
, __u32 ifa_flags
,
2474 __u32 prefered_lft
, __u32 valid_lft
)
2476 struct inet6_ifaddr
*ifp
;
2477 struct inet6_dev
*idev
;
2478 struct net_device
*dev
;
2482 unsigned long timeout
;
2489 /* check the lifetime */
2490 if (!valid_lft
|| prefered_lft
> valid_lft
)
2493 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&& plen
!= 64)
2496 dev
= __dev_get_by_index(net
, ifindex
);
2500 idev
= addrconf_add_dev(dev
);
2502 return PTR_ERR(idev
);
2504 scope
= ipv6_addr_scope(pfx
);
2506 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
2507 if (addrconf_finite_timeout(timeout
)) {
2508 expires
= jiffies_to_clock_t(timeout
* HZ
);
2509 valid_lft
= timeout
;
2510 flags
= RTF_EXPIRES
;
2514 ifa_flags
|= IFA_F_PERMANENT
;
2517 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
2518 if (addrconf_finite_timeout(timeout
)) {
2520 ifa_flags
|= IFA_F_DEPRECATED
;
2521 prefered_lft
= timeout
;
2524 ifp
= ipv6_add_addr(idev
, pfx
, peer_pfx
, plen
, scope
, ifa_flags
,
2525 valid_lft
, prefered_lft
);
2528 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
2529 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
2534 * Note that section 3.1 of RFC 4429 indicates
2535 * that the Optimistic flag should not be set for
2536 * manually configured addresses
2538 addrconf_dad_start(ifp
);
2539 if (ifa_flags
& IFA_F_MANAGETEMPADDR
)
2540 manage_tempaddrs(idev
, ifp
, valid_lft
, prefered_lft
,
2543 addrconf_verify_rtnl();
2547 return PTR_ERR(ifp
);
2550 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
2551 const struct in6_addr
*pfx
, unsigned int plen
)
2553 struct inet6_ifaddr
*ifp
;
2554 struct inet6_dev
*idev
;
2555 struct net_device
*dev
;
2560 dev
= __dev_get_by_index(net
, ifindex
);
2564 idev
= __in6_dev_get(dev
);
2568 read_lock_bh(&idev
->lock
);
2569 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
2570 if (ifp
->prefix_len
== plen
&&
2571 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
2573 read_unlock_bh(&idev
->lock
);
2575 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
2576 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
2577 manage_tempaddrs(idev
, ifp
, 0, 0, false,
2580 addrconf_verify_rtnl();
2584 read_unlock_bh(&idev
->lock
);
2585 return -EADDRNOTAVAIL
;
2589 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
2591 struct in6_ifreq ireq
;
2594 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2597 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2601 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, NULL
,
2602 ireq
.ifr6_prefixlen
, IFA_F_PERMANENT
,
2603 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2608 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
2610 struct in6_ifreq ireq
;
2613 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2616 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2620 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
2621 ireq
.ifr6_prefixlen
);
2626 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
2627 int plen
, int scope
)
2629 struct inet6_ifaddr
*ifp
;
2631 ifp
= ipv6_add_addr(idev
, addr
, NULL
, plen
,
2632 scope
, IFA_F_PERMANENT
,
2633 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2635 spin_lock_bh(&ifp
->lock
);
2636 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2637 spin_unlock_bh(&ifp
->lock
);
2638 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2643 #if IS_ENABLED(CONFIG_IPV6_SIT)
2644 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2646 struct in6_addr addr
;
2647 struct net_device
*dev
;
2648 struct net
*net
= dev_net(idev
->dev
);
2654 memset(&addr
, 0, sizeof(struct in6_addr
));
2655 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2657 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2658 addr
.s6_addr32
[0] = htonl(0xfe800000);
2662 scope
= IPV6_ADDR_COMPATv4
;
2664 pflags
|= RTF_NONEXTHOP
;
2667 if (addr
.s6_addr32
[3]) {
2668 add_addr(idev
, &addr
, plen
, scope
);
2669 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0, pflags
);
2673 for_each_netdev(net
, dev
) {
2674 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
2675 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2676 struct in_ifaddr
*ifa
;
2680 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2682 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2684 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2686 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2687 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2692 add_addr(idev
, &addr
, plen
, flag
);
2693 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0,
2701 static void init_loopback(struct net_device
*dev
)
2703 struct inet6_dev
*idev
;
2704 struct net_device
*sp_dev
;
2705 struct inet6_ifaddr
*sp_ifa
;
2706 struct rt6_info
*sp_rt
;
2712 idev
= ipv6_find_idev(dev
);
2714 pr_debug("%s: add_dev failed\n", __func__
);
2718 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
2720 /* Add routes to other interface's IPv6 addresses */
2721 for_each_netdev(dev_net(dev
), sp_dev
) {
2722 if (!strcmp(sp_dev
->name
, dev
->name
))
2725 idev
= __in6_dev_get(sp_dev
);
2729 read_lock_bh(&idev
->lock
);
2730 list_for_each_entry(sp_ifa
, &idev
->addr_list
, if_list
) {
2732 if (sp_ifa
->flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))
2736 /* This dst has been added to garbage list when
2737 * lo device down, release this obsolete dst and
2738 * reallocate a new router for ifa.
2740 if (sp_ifa
->rt
->dst
.obsolete
> 0) {
2741 ip6_rt_put(sp_ifa
->rt
);
2748 sp_rt
= addrconf_dst_alloc(idev
, &sp_ifa
->addr
, false);
2750 /* Failure cases are ignored */
2751 if (!IS_ERR(sp_rt
)) {
2756 read_unlock_bh(&idev
->lock
);
2760 static void addrconf_add_linklocal(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2762 struct inet6_ifaddr
*ifp
;
2763 u32 addr_flags
= IFA_F_PERMANENT
;
2765 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2766 if (idev
->cnf
.optimistic_dad
&&
2767 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
2768 addr_flags
|= IFA_F_OPTIMISTIC
;
2772 ifp
= ipv6_add_addr(idev
, addr
, NULL
, 64, IFA_LINK
, addr_flags
,
2773 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2775 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
2776 addrconf_dad_start(ifp
);
2781 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
2783 if (idev
->addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
) {
2784 struct in6_addr addr
;
2786 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
2787 /* addrconf_add_linklocal also adds a prefix_route and we
2788 * only need to care about prefix routes if ipv6_generate_eui64
2789 * couldn't generate one.
2791 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
2792 addrconf_add_linklocal(idev
, &addr
);
2793 else if (prefix_route
)
2794 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
2798 static void addrconf_dev_config(struct net_device
*dev
)
2800 struct inet6_dev
*idev
;
2804 if ((dev
->type
!= ARPHRD_ETHER
) &&
2805 (dev
->type
!= ARPHRD_FDDI
) &&
2806 (dev
->type
!= ARPHRD_ARCNET
) &&
2807 (dev
->type
!= ARPHRD_INFINIBAND
) &&
2808 (dev
->type
!= ARPHRD_IEEE802154
) &&
2809 (dev
->type
!= ARPHRD_IEEE1394
) &&
2810 (dev
->type
!= ARPHRD_TUNNEL6
) &&
2811 (dev
->type
!= ARPHRD_6LOWPAN
)) {
2812 /* Alas, we support only Ethernet autoconfiguration. */
2816 idev
= addrconf_add_dev(dev
);
2820 addrconf_addr_gen(idev
, false);
2823 #if IS_ENABLED(CONFIG_IPV6_SIT)
2824 static void addrconf_sit_config(struct net_device
*dev
)
2826 struct inet6_dev
*idev
;
2831 * Configure the tunnel with one of our IPv4
2832 * addresses... we should configure all of
2833 * our v4 addrs in the tunnel
2836 idev
= ipv6_find_idev(dev
);
2838 pr_debug("%s: add_dev failed\n", __func__
);
2842 if (dev
->priv_flags
& IFF_ISATAP
) {
2843 addrconf_addr_gen(idev
, false);
2847 sit_add_v4_addrs(idev
);
2849 if (dev
->flags
&IFF_POINTOPOINT
)
2850 addrconf_add_mroute(dev
);
2854 #if IS_ENABLED(CONFIG_NET_IPGRE)
2855 static void addrconf_gre_config(struct net_device
*dev
)
2857 struct inet6_dev
*idev
;
2861 idev
= ipv6_find_idev(dev
);
2863 pr_debug("%s: add_dev failed\n", __func__
);
2867 addrconf_addr_gen(idev
, true);
2871 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2874 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2875 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2876 int run_pending
= 0;
2880 case NETDEV_REGISTER
:
2881 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2882 idev
= ipv6_add_dev(dev
);
2884 return notifier_from_errno(PTR_ERR(idev
));
2890 if (dev
->flags
& IFF_SLAVE
)
2893 if (idev
&& idev
->cnf
.disable_ipv6
)
2896 if (event
== NETDEV_UP
) {
2897 if (!addrconf_qdisc_ok(dev
)) {
2898 /* device is not ready yet. */
2899 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2904 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
2905 idev
= ipv6_add_dev(dev
);
2907 if (!IS_ERR_OR_NULL(idev
)) {
2908 idev
->if_flags
|= IF_READY
;
2912 if (!addrconf_qdisc_ok(dev
)) {
2913 /* device is still not ready. */
2918 if (idev
->if_flags
& IF_READY
)
2919 /* device is already configured. */
2921 idev
->if_flags
|= IF_READY
;
2924 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2930 switch (dev
->type
) {
2931 #if IS_ENABLED(CONFIG_IPV6_SIT)
2933 addrconf_sit_config(dev
);
2936 #if IS_ENABLED(CONFIG_NET_IPGRE)
2938 addrconf_gre_config(dev
);
2941 case ARPHRD_LOOPBACK
:
2946 addrconf_dev_config(dev
);
2950 if (!IS_ERR_OR_NULL(idev
)) {
2952 addrconf_dad_run(idev
);
2955 * If the MTU changed during the interface down,
2956 * when the interface up, the changed MTU must be
2957 * reflected in the idev as well as routers.
2959 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
2960 dev
->mtu
>= IPV6_MIN_MTU
) {
2961 rt6_mtu_change(dev
, dev
->mtu
);
2962 idev
->cnf
.mtu6
= dev
->mtu
;
2964 idev
->tstamp
= jiffies
;
2965 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2968 * If the changed mtu during down is lower than
2969 * IPV6_MIN_MTU stop IPv6 on this interface.
2971 if (dev
->mtu
< IPV6_MIN_MTU
)
2972 addrconf_ifdown(dev
, 1);
2976 case NETDEV_CHANGEMTU
:
2977 if (idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2978 rt6_mtu_change(dev
, dev
->mtu
);
2979 idev
->cnf
.mtu6
= dev
->mtu
;
2983 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2984 idev
= ipv6_add_dev(dev
);
2990 * if MTU under IPV6_MIN_MTU.
2991 * Stop IPv6 on this interface.
2995 case NETDEV_UNREGISTER
:
2997 * Remove all addresses from this interface.
2999 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3002 case NETDEV_CHANGENAME
:
3004 snmp6_unregister_dev(idev
);
3005 addrconf_sysctl_unregister(idev
);
3006 err
= addrconf_sysctl_register(idev
);
3008 return notifier_from_errno(err
);
3009 err
= snmp6_register_dev(idev
);
3011 addrconf_sysctl_unregister(idev
);
3012 return notifier_from_errno(err
);
3017 case NETDEV_PRE_TYPE_CHANGE
:
3018 case NETDEV_POST_TYPE_CHANGE
:
3019 addrconf_type_change(dev
, event
);
3027 * addrconf module should be notified of a device going up
3029 static struct notifier_block ipv6_dev_notf
= {
3030 .notifier_call
= addrconf_notify
,
3033 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3035 struct inet6_dev
*idev
;
3038 idev
= __in6_dev_get(dev
);
3040 if (event
== NETDEV_POST_TYPE_CHANGE
)
3041 ipv6_mc_remap(idev
);
3042 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3043 ipv6_mc_unmap(idev
);
3046 static int addrconf_ifdown(struct net_device
*dev
, int how
)
3048 struct net
*net
= dev_net(dev
);
3049 struct inet6_dev
*idev
;
3050 struct inet6_ifaddr
*ifa
;
3055 rt6_ifdown(net
, dev
);
3056 neigh_ifdown(&nd_tbl
, dev
);
3058 idev
= __in6_dev_get(dev
);
3063 * Step 1: remove reference to ipv6 device from parent device.
3069 /* protected by rtnl_lock */
3070 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3072 /* Step 1.5: remove snmp6 entry */
3073 snmp6_unregister_dev(idev
);
3077 /* Step 2: clear hash table */
3078 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3079 struct hlist_head
*h
= &inet6_addr_lst
[i
];
3081 spin_lock_bh(&addrconf_hash_lock
);
3083 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3084 if (ifa
->idev
== idev
) {
3085 hlist_del_init_rcu(&ifa
->addr_lst
);
3086 addrconf_del_dad_work(ifa
);
3090 spin_unlock_bh(&addrconf_hash_lock
);
3093 write_lock_bh(&idev
->lock
);
3095 addrconf_del_rs_timer(idev
);
3097 /* Step 2: clear flags for stateless addrconf */
3099 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3101 if (how
&& del_timer(&idev
->regen_timer
))
3104 /* Step 3: clear tempaddr list */
3105 while (!list_empty(&idev
->tempaddr_list
)) {
3106 ifa
= list_first_entry(&idev
->tempaddr_list
,
3107 struct inet6_ifaddr
, tmp_list
);
3108 list_del(&ifa
->tmp_list
);
3109 write_unlock_bh(&idev
->lock
);
3110 spin_lock_bh(&ifa
->lock
);
3113 in6_ifa_put(ifa
->ifpub
);
3116 spin_unlock_bh(&ifa
->lock
);
3118 write_lock_bh(&idev
->lock
);
3121 while (!list_empty(&idev
->addr_list
)) {
3122 ifa
= list_first_entry(&idev
->addr_list
,
3123 struct inet6_ifaddr
, if_list
);
3124 addrconf_del_dad_work(ifa
);
3126 list_del(&ifa
->if_list
);
3128 write_unlock_bh(&idev
->lock
);
3130 spin_lock_bh(&ifa
->state_lock
);
3132 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3133 spin_unlock_bh(&ifa
->state_lock
);
3135 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3136 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3137 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3141 write_lock_bh(&idev
->lock
);
3144 write_unlock_bh(&idev
->lock
);
3146 /* Step 5: Discard anycast and multicast list */
3148 ipv6_ac_destroy_dev(idev
);
3149 ipv6_mc_destroy_dev(idev
);
3154 idev
->tstamp
= jiffies
;
3156 /* Last: Shot the device (if unregistered) */
3158 addrconf_sysctl_unregister(idev
);
3159 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3160 neigh_ifdown(&nd_tbl
, dev
);
3166 static void addrconf_rs_timer(unsigned long data
)
3168 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
3169 struct net_device
*dev
= idev
->dev
;
3170 struct in6_addr lladdr
;
3172 write_lock(&idev
->lock
);
3173 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3176 if (!ipv6_accept_ra(idev
))
3179 /* Announcement received after solicitation was sent */
3180 if (idev
->if_flags
& IF_RA_RCVD
)
3183 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
) {
3184 write_unlock(&idev
->lock
);
3185 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3186 ndisc_send_rs(dev
, &lladdr
,
3187 &in6addr_linklocal_allrouters
);
3191 write_lock(&idev
->lock
);
3192 /* The wait after the last probe can be shorter */
3193 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3194 idev
->cnf
.rtr_solicits
) ?
3195 idev
->cnf
.rtr_solicit_delay
:
3196 idev
->cnf
.rtr_solicit_interval
);
3199 * Note: we do not support deprecated "all on-link"
3200 * assumption any longer.
3202 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3206 write_unlock(&idev
->lock
);
3212 * Duplicate Address Detection
3214 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3216 unsigned long rand_num
;
3217 struct inet6_dev
*idev
= ifp
->idev
;
3219 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3222 rand_num
= prandom_u32() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3224 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
3225 addrconf_mod_dad_work(ifp
, rand_num
);
3228 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
3230 struct inet6_dev
*idev
= ifp
->idev
;
3231 struct net_device
*dev
= idev
->dev
;
3233 addrconf_join_solict(dev
, &ifp
->addr
);
3235 prandom_seed((__force u32
) ifp
->addr
.s6_addr32
[3]);
3237 read_lock_bh(&idev
->lock
);
3238 spin_lock(&ifp
->lock
);
3239 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3242 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3243 idev
->cnf
.accept_dad
< 1 ||
3244 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3245 ifp
->flags
& IFA_F_NODAD
) {
3246 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3247 spin_unlock(&ifp
->lock
);
3248 read_unlock_bh(&idev
->lock
);
3250 addrconf_dad_completed(ifp
);
3254 if (!(idev
->if_flags
& IF_READY
)) {
3255 spin_unlock(&ifp
->lock
);
3256 read_unlock_bh(&idev
->lock
);
3258 * If the device is not ready:
3259 * - keep it tentative if it is a permanent address.
3260 * - otherwise, kill it.
3263 addrconf_dad_stop(ifp
, 0);
3268 * Optimistic nodes can start receiving
3271 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
3272 ip6_ins_rt(ifp
->rt
);
3273 if (ipv6_use_optimistic_addr(idev
)) {
3274 /* Because optimistic nodes can use this address,
3275 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3277 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3281 addrconf_dad_kick(ifp
);
3283 spin_unlock(&ifp
->lock
);
3284 read_unlock_bh(&idev
->lock
);
3287 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
3289 bool begin_dad
= false;
3291 spin_lock_bh(&ifp
->state_lock
);
3292 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
3293 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
3296 spin_unlock_bh(&ifp
->state_lock
);
3299 addrconf_mod_dad_work(ifp
, 0);
3302 static void addrconf_dad_work(struct work_struct
*w
)
3304 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
3305 struct inet6_ifaddr
,
3307 struct inet6_dev
*idev
= ifp
->idev
;
3308 struct in6_addr mcaddr
;
3314 } action
= DAD_PROCESS
;
3318 spin_lock_bh(&ifp
->state_lock
);
3319 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
3321 ifp
->state
= INET6_IFADDR_STATE_DAD
;
3322 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
3324 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
3326 spin_unlock_bh(&ifp
->state_lock
);
3328 if (action
== DAD_BEGIN
) {
3329 addrconf_dad_begin(ifp
);
3331 } else if (action
== DAD_ABORT
) {
3332 addrconf_dad_stop(ifp
, 1);
3336 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
3339 write_lock_bh(&idev
->lock
);
3340 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
3341 write_unlock_bh(&idev
->lock
);
3345 spin_lock(&ifp
->lock
);
3346 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
3347 spin_unlock(&ifp
->lock
);
3348 write_unlock_bh(&idev
->lock
);
3352 if (ifp
->dad_probes
== 0) {
3354 * DAD was successful
3357 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3358 spin_unlock(&ifp
->lock
);
3359 write_unlock_bh(&idev
->lock
);
3361 addrconf_dad_completed(ifp
);
3367 addrconf_mod_dad_work(ifp
,
3368 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
));
3369 spin_unlock(&ifp
->lock
);
3370 write_unlock_bh(&idev
->lock
);
3372 /* send a neighbour solicitation for our addr */
3373 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
3374 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &in6addr_any
);
3380 /* ifp->idev must be at least read locked */
3381 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
3383 struct inet6_ifaddr
*ifpiter
;
3384 struct inet6_dev
*idev
= ifp
->idev
;
3386 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
3387 if (ifpiter
->scope
> IFA_LINK
)
3389 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
3390 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
3391 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
3398 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
3400 struct net_device
*dev
= ifp
->idev
->dev
;
3401 struct in6_addr lladdr
;
3402 bool send_rs
, send_mld
;
3404 addrconf_del_dad_work(ifp
);
3407 * Configure the address for reception. Now it is valid.
3410 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3412 /* If added prefix is link local and we are prepared to process
3413 router advertisements, start sending router solicitations.
3416 read_lock_bh(&ifp
->idev
->lock
);
3417 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
3418 send_rs
= send_mld
&&
3419 ipv6_accept_ra(ifp
->idev
) &&
3420 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
3421 (dev
->flags
&IFF_LOOPBACK
) == 0;
3422 read_unlock_bh(&ifp
->idev
->lock
);
3424 /* While dad is in progress mld report's source address is in6_addrany.
3425 * Resend with proper ll now.
3428 ipv6_mc_dad_complete(ifp
->idev
);
3432 * If a host as already performed a random delay
3433 * [...] as part of DAD [...] there is no need
3434 * to delay again before sending the first RS
3436 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3438 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
3440 write_lock_bh(&ifp
->idev
->lock
);
3441 spin_lock(&ifp
->lock
);
3442 ifp
->idev
->rs_probes
= 1;
3443 ifp
->idev
->if_flags
|= IF_RS_SENT
;
3444 addrconf_mod_rs_timer(ifp
->idev
,
3445 ifp
->idev
->cnf
.rtr_solicit_interval
);
3446 spin_unlock(&ifp
->lock
);
3447 write_unlock_bh(&ifp
->idev
->lock
);
3451 static void addrconf_dad_run(struct inet6_dev
*idev
)
3453 struct inet6_ifaddr
*ifp
;
3455 read_lock_bh(&idev
->lock
);
3456 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3457 spin_lock(&ifp
->lock
);
3458 if (ifp
->flags
& IFA_F_TENTATIVE
&&
3459 ifp
->state
== INET6_IFADDR_STATE_DAD
)
3460 addrconf_dad_kick(ifp
);
3461 spin_unlock(&ifp
->lock
);
3463 read_unlock_bh(&idev
->lock
);
3466 #ifdef CONFIG_PROC_FS
3467 struct if6_iter_state
{
3468 struct seq_net_private p
;
3473 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
3475 struct inet6_ifaddr
*ifa
= NULL
;
3476 struct if6_iter_state
*state
= seq
->private;
3477 struct net
*net
= seq_file_net(seq
);
3480 /* initial bucket if pos is 0 */
3486 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
3487 hlist_for_each_entry_rcu_bh(ifa
, &inet6_addr_lst
[state
->bucket
],
3489 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3491 /* sync with offset */
3492 if (p
< state
->offset
) {
3500 /* prepare for next bucket */
3507 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
3508 struct inet6_ifaddr
*ifa
)
3510 struct if6_iter_state
*state
= seq
->private;
3511 struct net
*net
= seq_file_net(seq
);
3513 hlist_for_each_entry_continue_rcu_bh(ifa
, addr_lst
) {
3514 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3520 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
3522 hlist_for_each_entry_rcu_bh(ifa
,
3523 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
3524 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3534 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3538 return if6_get_first(seq
, *pos
);
3541 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3543 struct inet6_ifaddr
*ifa
;
3545 ifa
= if6_get_next(seq
, v
);
3550 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
3553 rcu_read_unlock_bh();
3556 static int if6_seq_show(struct seq_file
*seq
, void *v
)
3558 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
3559 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
3561 ifp
->idev
->dev
->ifindex
,
3565 ifp
->idev
->dev
->name
);
3569 static const struct seq_operations if6_seq_ops
= {
3570 .start
= if6_seq_start
,
3571 .next
= if6_seq_next
,
3572 .show
= if6_seq_show
,
3573 .stop
= if6_seq_stop
,
3576 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
3578 return seq_open_net(inode
, file
, &if6_seq_ops
,
3579 sizeof(struct if6_iter_state
));
3582 static const struct file_operations if6_fops
= {
3583 .owner
= THIS_MODULE
,
3584 .open
= if6_seq_open
,
3586 .llseek
= seq_lseek
,
3587 .release
= seq_release_net
,
3590 static int __net_init
if6_proc_net_init(struct net
*net
)
3592 if (!proc_create("if_inet6", S_IRUGO
, net
->proc_net
, &if6_fops
))
3597 static void __net_exit
if6_proc_net_exit(struct net
*net
)
3599 remove_proc_entry("if_inet6", net
->proc_net
);
3602 static struct pernet_operations if6_proc_net_ops
= {
3603 .init
= if6_proc_net_init
,
3604 .exit
= if6_proc_net_exit
,
3607 int __init
if6_proc_init(void)
3609 return register_pernet_subsys(&if6_proc_net_ops
);
3612 void if6_proc_exit(void)
3614 unregister_pernet_subsys(&if6_proc_net_ops
);
3616 #endif /* CONFIG_PROC_FS */
3618 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3619 /* Check if address is a home address configured on any interface. */
3620 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
3623 struct inet6_ifaddr
*ifp
= NULL
;
3624 unsigned int hash
= inet6_addr_hash(addr
);
3627 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
3628 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
3630 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
3631 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
3636 rcu_read_unlock_bh();
3642 * Periodic address status verification
3645 static void addrconf_verify_rtnl(void)
3647 unsigned long now
, next
, next_sec
, next_sched
;
3648 struct inet6_ifaddr
*ifp
;
3655 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
3657 cancel_delayed_work(&addr_chk_work
);
3659 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3661 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[i
], addr_lst
) {
3664 /* When setting preferred_lft to a value not zero or
3665 * infinity, while valid_lft is infinity
3666 * IFA_F_PERMANENT has a non-infinity life time.
3668 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3669 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
3672 spin_lock(&ifp
->lock
);
3673 /* We try to batch several events at once. */
3674 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
3676 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
3677 age
>= ifp
->valid_lft
) {
3678 spin_unlock(&ifp
->lock
);
3682 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
3683 spin_unlock(&ifp
->lock
);
3685 } else if (age
>= ifp
->prefered_lft
) {
3686 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3689 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
3691 ifp
->flags
|= IFA_F_DEPRECATED
;
3694 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
3695 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
3696 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
3698 spin_unlock(&ifp
->lock
);
3703 ipv6_ifa_notify(0, ifp
);
3707 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
3708 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
3709 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
3710 ifp
->idev
->cnf
.dad_transmits
*
3711 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
) / HZ
;
3713 if (age
>= ifp
->prefered_lft
- regen_advance
) {
3714 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
3715 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3716 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3717 if (!ifp
->regen_count
&& ifpub
) {
3720 in6_ifa_hold(ifpub
);
3721 spin_unlock(&ifp
->lock
);
3723 spin_lock(&ifpub
->lock
);
3724 ifpub
->regen_count
= 0;
3725 spin_unlock(&ifpub
->lock
);
3726 ipv6_create_tempaddr(ifpub
, ifp
);
3731 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
3732 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
3733 spin_unlock(&ifp
->lock
);
3735 /* ifp->prefered_lft <= ifp->valid_lft */
3736 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3737 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3738 spin_unlock(&ifp
->lock
);
3743 next_sec
= round_jiffies_up(next
);
3746 /* If rounded timeout is accurate enough, accept it. */
3747 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
3748 next_sched
= next_sec
;
3750 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3751 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
3752 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
3754 ADBG(KERN_DEBUG
"now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3755 now
, next
, next_sec
, next_sched
);
3756 mod_delayed_work(addrconf_wq
, &addr_chk_work
, next_sched
- now
);
3757 rcu_read_unlock_bh();
3760 static void addrconf_verify_work(struct work_struct
*w
)
3763 addrconf_verify_rtnl();
3767 static void addrconf_verify(void)
3769 mod_delayed_work(addrconf_wq
, &addr_chk_work
, 0);
3772 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
3773 struct in6_addr
**peer_pfx
)
3775 struct in6_addr
*pfx
= NULL
;
3780 pfx
= nla_data(addr
);
3783 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
3785 pfx
= nla_data(local
);
3791 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
3792 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
3793 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
3794 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
3795 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
3799 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3801 struct net
*net
= sock_net(skb
->sk
);
3802 struct ifaddrmsg
*ifm
;
3803 struct nlattr
*tb
[IFA_MAX
+1];
3804 struct in6_addr
*pfx
, *peer_pfx
;
3808 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3812 ifm
= nlmsg_data(nlh
);
3813 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
3817 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
3819 /* We ignore other flags so far. */
3820 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
3822 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
3823 ifm
->ifa_prefixlen
);
3826 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u32 ifa_flags
,
3827 u32 prefered_lft
, u32 valid_lft
)
3831 unsigned long timeout
;
3832 bool was_managetempaddr
;
3833 bool had_prefixroute
;
3837 if (!valid_lft
|| (prefered_lft
> valid_lft
))
3840 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&&
3841 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
3844 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
3845 if (addrconf_finite_timeout(timeout
)) {
3846 expires
= jiffies_to_clock_t(timeout
* HZ
);
3847 valid_lft
= timeout
;
3848 flags
= RTF_EXPIRES
;
3852 ifa_flags
|= IFA_F_PERMANENT
;
3855 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
3856 if (addrconf_finite_timeout(timeout
)) {
3858 ifa_flags
|= IFA_F_DEPRECATED
;
3859 prefered_lft
= timeout
;
3862 spin_lock_bh(&ifp
->lock
);
3863 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
3864 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
3865 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
3866 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
3867 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
3868 IFA_F_NOPREFIXROUTE
);
3869 ifp
->flags
|= ifa_flags
;
3870 ifp
->tstamp
= jiffies
;
3871 ifp
->valid_lft
= valid_lft
;
3872 ifp
->prefered_lft
= prefered_lft
;
3874 spin_unlock_bh(&ifp
->lock
);
3875 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
3876 ipv6_ifa_notify(0, ifp
);
3878 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
3879 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
3881 } else if (had_prefixroute
) {
3882 enum cleanup_prefix_rt_t action
;
3883 unsigned long rt_expires
;
3885 write_lock_bh(&ifp
->idev
->lock
);
3886 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
3887 write_unlock_bh(&ifp
->idev
->lock
);
3889 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
3890 cleanup_prefix_route(ifp
, rt_expires
,
3891 action
== CLEANUP_PREFIX_RT_DEL
);
3895 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
3896 if (was_managetempaddr
&& !(ifp
->flags
& IFA_F_MANAGETEMPADDR
))
3897 valid_lft
= prefered_lft
= 0;
3898 manage_tempaddrs(ifp
->idev
, ifp
, valid_lft
, prefered_lft
,
3899 !was_managetempaddr
, jiffies
);
3902 addrconf_verify_rtnl();
3908 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3910 struct net
*net
= sock_net(skb
->sk
);
3911 struct ifaddrmsg
*ifm
;
3912 struct nlattr
*tb
[IFA_MAX
+1];
3913 struct in6_addr
*pfx
, *peer_pfx
;
3914 struct inet6_ifaddr
*ifa
;
3915 struct net_device
*dev
;
3916 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
3920 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3924 ifm
= nlmsg_data(nlh
);
3925 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
3929 if (tb
[IFA_CACHEINFO
]) {
3930 struct ifa_cacheinfo
*ci
;
3932 ci
= nla_data(tb
[IFA_CACHEINFO
]);
3933 valid_lft
= ci
->ifa_valid
;
3934 preferred_lft
= ci
->ifa_prefered
;
3936 preferred_lft
= INFINITY_LIFE_TIME
;
3937 valid_lft
= INFINITY_LIFE_TIME
;
3940 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
3944 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
3946 /* We ignore other flags so far. */
3947 ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
3948 IFA_F_NOPREFIXROUTE
;
3950 ifa
= ipv6_get_ifaddr(net
, pfx
, dev
, 1);
3953 * It would be best to check for !NLM_F_CREATE here but
3954 * userspace already relies on not having to provide this.
3956 return inet6_addr_add(net
, ifm
->ifa_index
, pfx
, peer_pfx
,
3957 ifm
->ifa_prefixlen
, ifa_flags
,
3958 preferred_lft
, valid_lft
);
3961 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
3962 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
3965 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
3972 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
3973 u8 scope
, int ifindex
)
3975 struct ifaddrmsg
*ifm
;
3977 ifm
= nlmsg_data(nlh
);
3978 ifm
->ifa_family
= AF_INET6
;
3979 ifm
->ifa_prefixlen
= prefixlen
;
3980 ifm
->ifa_flags
= flags
;
3981 ifm
->ifa_scope
= scope
;
3982 ifm
->ifa_index
= ifindex
;
3985 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
3986 unsigned long tstamp
, u32 preferred
, u32 valid
)
3988 struct ifa_cacheinfo ci
;
3990 ci
.cstamp
= cstamp_delta(cstamp
);
3991 ci
.tstamp
= cstamp_delta(tstamp
);
3992 ci
.ifa_prefered
= preferred
;
3993 ci
.ifa_valid
= valid
;
3995 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3998 static inline int rt_scope(int ifa_scope
)
4000 if (ifa_scope
& IFA_HOST
)
4001 return RT_SCOPE_HOST
;
4002 else if (ifa_scope
& IFA_LINK
)
4003 return RT_SCOPE_LINK
;
4004 else if (ifa_scope
& IFA_SITE
)
4005 return RT_SCOPE_SITE
;
4007 return RT_SCOPE_UNIVERSE
;
4010 static inline int inet6_ifaddr_msgsize(void)
4012 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
4013 + nla_total_size(16) /* IFA_LOCAL */
4014 + nla_total_size(16) /* IFA_ADDRESS */
4015 + nla_total_size(sizeof(struct ifa_cacheinfo
))
4016 + nla_total_size(4) /* IFA_FLAGS */;
4019 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
4020 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4022 struct nlmsghdr
*nlh
;
4023 u32 preferred
, valid
;
4025 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4029 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
4030 ifa
->idev
->dev
->ifindex
);
4032 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
4033 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
4034 preferred
= ifa
->prefered_lft
;
4035 valid
= ifa
->valid_lft
;
4036 if (preferred
!= INFINITY_LIFE_TIME
) {
4037 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
4038 if (preferred
> tval
)
4042 if (valid
!= INFINITY_LIFE_TIME
) {
4050 preferred
= INFINITY_LIFE_TIME
;
4051 valid
= INFINITY_LIFE_TIME
;
4054 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
4055 if (nla_put(skb
, IFA_LOCAL
, 16, &ifa
->addr
) < 0 ||
4056 nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->peer_addr
) < 0)
4059 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0)
4062 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
4065 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
4068 nlmsg_end(skb
, nlh
);
4072 nlmsg_cancel(skb
, nlh
);
4076 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
4077 u32 portid
, u32 seq
, int event
, u16 flags
)
4079 struct nlmsghdr
*nlh
;
4080 u8 scope
= RT_SCOPE_UNIVERSE
;
4081 int ifindex
= ifmca
->idev
->dev
->ifindex
;
4083 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
4084 scope
= RT_SCOPE_SITE
;
4086 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4090 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4091 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
4092 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
4093 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4094 nlmsg_cancel(skb
, nlh
);
4098 nlmsg_end(skb
, nlh
);
4102 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
4103 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4105 struct nlmsghdr
*nlh
;
4106 u8 scope
= RT_SCOPE_UNIVERSE
;
4107 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
4109 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
4110 scope
= RT_SCOPE_SITE
;
4112 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4116 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4117 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
4118 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
4119 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4120 nlmsg_cancel(skb
, nlh
);
4124 nlmsg_end(skb
, nlh
);
4134 /* called with rcu_read_lock() */
4135 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
4136 struct netlink_callback
*cb
, enum addr_type_t type
,
4137 int s_ip_idx
, int *p_ip_idx
)
4139 struct ifmcaddr6
*ifmca
;
4140 struct ifacaddr6
*ifaca
;
4142 int ip_idx
= *p_ip_idx
;
4144 read_lock_bh(&idev
->lock
);
4146 case UNICAST_ADDR
: {
4147 struct inet6_ifaddr
*ifa
;
4149 /* unicast address incl. temp addr */
4150 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
4151 if (++ip_idx
< s_ip_idx
)
4153 err
= inet6_fill_ifaddr(skb
, ifa
,
4154 NETLINK_CB(cb
->skb
).portid
,
4160 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4164 case MULTICAST_ADDR
:
4165 /* multicast address */
4166 for (ifmca
= idev
->mc_list
; ifmca
;
4167 ifmca
= ifmca
->next
, ip_idx
++) {
4168 if (ip_idx
< s_ip_idx
)
4170 err
= inet6_fill_ifmcaddr(skb
, ifmca
,
4171 NETLINK_CB(cb
->skb
).portid
,
4180 /* anycast address */
4181 for (ifaca
= idev
->ac_list
; ifaca
;
4182 ifaca
= ifaca
->aca_next
, ip_idx
++) {
4183 if (ip_idx
< s_ip_idx
)
4185 err
= inet6_fill_ifacaddr(skb
, ifaca
,
4186 NETLINK_CB(cb
->skb
).portid
,
4197 read_unlock_bh(&idev
->lock
);
4202 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
4203 enum addr_type_t type
)
4205 struct net
*net
= sock_net(skb
->sk
);
4208 int s_idx
, s_ip_idx
;
4209 struct net_device
*dev
;
4210 struct inet6_dev
*idev
;
4211 struct hlist_head
*head
;
4214 s_idx
= idx
= cb
->args
[1];
4215 s_ip_idx
= ip_idx
= cb
->args
[2];
4218 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^ net
->dev_base_seq
;
4219 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4221 head
= &net
->dev_index_head
[h
];
4222 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4225 if (h
> s_h
|| idx
> s_idx
)
4228 idev
= __in6_dev_get(dev
);
4232 if (in6_dump_addrs(idev
, skb
, cb
, type
,
4233 s_ip_idx
, &ip_idx
) < 0)
4243 cb
->args
[2] = ip_idx
;
4248 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4250 enum addr_type_t type
= UNICAST_ADDR
;
4252 return inet6_dump_addr(skb
, cb
, type
);
4255 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4257 enum addr_type_t type
= MULTICAST_ADDR
;
4259 return inet6_dump_addr(skb
, cb
, type
);
4263 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4265 enum addr_type_t type
= ANYCAST_ADDR
;
4267 return inet6_dump_addr(skb
, cb
, type
);
4270 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
)
4272 struct net
*net
= sock_net(in_skb
->sk
);
4273 struct ifaddrmsg
*ifm
;
4274 struct nlattr
*tb
[IFA_MAX
+1];
4275 struct in6_addr
*addr
= NULL
, *peer
;
4276 struct net_device
*dev
= NULL
;
4277 struct inet6_ifaddr
*ifa
;
4278 struct sk_buff
*skb
;
4281 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4285 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
4291 ifm
= nlmsg_data(nlh
);
4293 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4295 ifa
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
4297 err
= -EADDRNOTAVAIL
;
4301 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
4307 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).portid
,
4308 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
4310 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4311 WARN_ON(err
== -EMSGSIZE
);
4315 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4322 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
4324 struct sk_buff
*skb
;
4325 struct net
*net
= dev_net(ifa
->idev
->dev
);
4328 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
4332 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
4334 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4335 WARN_ON(err
== -EMSGSIZE
);
4339 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
4343 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
4346 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
4347 __s32
*array
, int bytes
)
4349 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
4351 memset(array
, 0, bytes
);
4352 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
4353 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
4354 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
4355 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
4356 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
4357 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
4358 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
4359 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
4360 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
4361 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
4362 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
4363 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
4364 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
4365 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
4366 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
4367 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
4368 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
4369 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
4370 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
4371 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
4372 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
4373 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
4374 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
4375 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
4376 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
4377 #ifdef CONFIG_IPV6_ROUTER_PREF
4378 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
4379 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
4380 jiffies_to_msecs(cnf
->rtr_probe_interval
);
4381 #ifdef CONFIG_IPV6_ROUTE_INFO
4382 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
4385 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
4386 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
4387 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4388 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
4389 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
4391 #ifdef CONFIG_IPV6_MROUTE
4392 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
4394 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
4395 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
4396 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
4397 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
4398 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
4399 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
4400 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
4403 static inline size_t inet6_ifla6_size(void)
4405 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4406 + nla_total_size(sizeof(struct ifla_cacheinfo
))
4407 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
4408 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
4409 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
4410 + nla_total_size(sizeof(struct in6_addr
)); /* IFLA_INET6_TOKEN */
4413 static inline size_t inet6_if_nlmsg_size(void)
4415 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4416 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4417 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4418 + nla_total_size(4) /* IFLA_MTU */
4419 + nla_total_size(4) /* IFLA_LINK */
4420 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4423 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
4424 int items
, int bytes
)
4427 int pad
= bytes
- sizeof(u64
) * items
;
4430 /* Use put_unaligned() because stats may not be aligned for u64. */
4431 put_unaligned(items
, &stats
[0]);
4432 for (i
= 1; i
< items
; i
++)
4433 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
4435 memset(&stats
[items
], 0, pad
);
4438 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
4439 int items
, int bytes
, size_t syncpoff
)
4442 int pad
= bytes
- sizeof(u64
) * items
;
4445 /* Use put_unaligned() because stats may not be aligned for u64. */
4446 put_unaligned(items
, &stats
[0]);
4447 for (i
= 1; i
< items
; i
++)
4448 put_unaligned(snmp_fold_field64(mib
, i
, syncpoff
), &stats
[i
]);
4450 memset(&stats
[items
], 0, pad
);
4453 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
4457 case IFLA_INET6_STATS
:
4458 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
,
4459 IPSTATS_MIB_MAX
, bytes
, offsetof(struct ipstats_mib
, syncp
));
4461 case IFLA_INET6_ICMP6STATS
:
4462 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, ICMP6_MIB_MAX
, bytes
);
4467 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
)
4470 struct ifla_cacheinfo ci
;
4472 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
4473 goto nla_put_failure
;
4474 ci
.max_reasm_len
= IPV6_MAXPLEN
;
4475 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
4476 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
4477 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
4478 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
4479 goto nla_put_failure
;
4480 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
4482 goto nla_put_failure
;
4483 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
4485 /* XXX - MC not implemented */
4487 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
4489 goto nla_put_failure
;
4490 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
4492 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
4494 goto nla_put_failure
;
4495 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
4497 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
4499 goto nla_put_failure
;
4501 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->addr_gen_mode
))
4502 goto nla_put_failure
;
4504 read_lock_bh(&idev
->lock
);
4505 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
4506 read_unlock_bh(&idev
->lock
);
4514 static size_t inet6_get_link_af_size(const struct net_device
*dev
)
4516 if (!__in6_dev_get(dev
))
4519 return inet6_ifla6_size();
4522 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
)
4524 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4529 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4535 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
)
4537 struct inet6_ifaddr
*ifp
;
4538 struct net_device
*dev
= idev
->dev
;
4539 bool update_rs
= false;
4540 struct in6_addr ll_addr
;
4546 if (ipv6_addr_any(token
))
4548 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
))
4550 if (!ipv6_accept_ra(idev
))
4552 if (idev
->cnf
.rtr_solicits
<= 0)
4555 write_lock_bh(&idev
->lock
);
4557 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
4558 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
4560 write_unlock_bh(&idev
->lock
);
4562 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
4563 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
4564 IFA_F_OPTIMISTIC
)) {
4566 /* If we're not ready, then normal ifup will take care
4567 * of this. Otherwise, we need to request our rs here.
4569 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
4573 write_lock_bh(&idev
->lock
);
4576 idev
->if_flags
|= IF_RS_SENT
;
4577 idev
->rs_probes
= 1;
4578 addrconf_mod_rs_timer(idev
, idev
->cnf
.rtr_solicit_interval
);
4581 /* Well, that's kinda nasty ... */
4582 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4583 spin_lock(&ifp
->lock
);
4584 if (ifp
->tokenized
) {
4586 ifp
->prefered_lft
= 0;
4588 spin_unlock(&ifp
->lock
);
4591 write_unlock_bh(&idev
->lock
);
4592 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
4593 addrconf_verify_rtnl();
4597 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
4598 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
4599 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
4602 static int inet6_validate_link_af(const struct net_device
*dev
,
4603 const struct nlattr
*nla
)
4605 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4607 if (dev
&& !__in6_dev_get(dev
))
4608 return -EAFNOSUPPORT
;
4610 return nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, inet6_af_policy
);
4613 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
4616 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4617 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4620 return -EAFNOSUPPORT
;
4622 if (nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, NULL
) < 0)
4625 if (tb
[IFLA_INET6_TOKEN
]) {
4626 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]));
4631 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
4632 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
4634 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
4635 mode
!= IN6_ADDR_GEN_MODE_NONE
)
4637 idev
->addr_gen_mode
= mode
;
4644 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4645 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4647 struct net_device
*dev
= idev
->dev
;
4648 struct ifinfomsg
*hdr
;
4649 struct nlmsghdr
*nlh
;
4652 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
4656 hdr
= nlmsg_data(nlh
);
4657 hdr
->ifi_family
= AF_INET6
;
4659 hdr
->ifi_type
= dev
->type
;
4660 hdr
->ifi_index
= dev
->ifindex
;
4661 hdr
->ifi_flags
= dev_get_flags(dev
);
4662 hdr
->ifi_change
= 0;
4664 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4666 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4667 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4668 (dev
->ifindex
!= dev
->iflink
&&
4669 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)))
4670 goto nla_put_failure
;
4671 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4672 if (protoinfo
== NULL
)
4673 goto nla_put_failure
;
4675 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4676 goto nla_put_failure
;
4678 nla_nest_end(skb
, protoinfo
);
4679 nlmsg_end(skb
, nlh
);
4683 nlmsg_cancel(skb
, nlh
);
4687 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4689 struct net
*net
= sock_net(skb
->sk
);
4692 struct net_device
*dev
;
4693 struct inet6_dev
*idev
;
4694 struct hlist_head
*head
;
4697 s_idx
= cb
->args
[1];
4700 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4702 head
= &net
->dev_index_head
[h
];
4703 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4706 idev
= __in6_dev_get(dev
);
4709 if (inet6_fill_ifinfo(skb
, idev
,
4710 NETLINK_CB(cb
->skb
).portid
,
4712 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
4726 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
4728 struct sk_buff
*skb
;
4729 struct net
*net
= dev_net(idev
->dev
);
4732 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
4736 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
4738 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4739 WARN_ON(err
== -EMSGSIZE
);
4743 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
4747 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
4750 static inline size_t inet6_prefix_nlmsg_size(void)
4752 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
4753 + nla_total_size(sizeof(struct in6_addr
))
4754 + nla_total_size(sizeof(struct prefix_cacheinfo
));
4757 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4758 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
4759 int event
, unsigned int flags
)
4761 struct prefixmsg
*pmsg
;
4762 struct nlmsghdr
*nlh
;
4763 struct prefix_cacheinfo ci
;
4765 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
4769 pmsg
= nlmsg_data(nlh
);
4770 pmsg
->prefix_family
= AF_INET6
;
4771 pmsg
->prefix_pad1
= 0;
4772 pmsg
->prefix_pad2
= 0;
4773 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
4774 pmsg
->prefix_len
= pinfo
->prefix_len
;
4775 pmsg
->prefix_type
= pinfo
->type
;
4776 pmsg
->prefix_pad3
= 0;
4777 pmsg
->prefix_flags
= 0;
4779 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
4780 if (pinfo
->autoconf
)
4781 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
4783 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
4784 goto nla_put_failure
;
4785 ci
.preferred_time
= ntohl(pinfo
->prefered
);
4786 ci
.valid_time
= ntohl(pinfo
->valid
);
4787 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
4788 goto nla_put_failure
;
4789 nlmsg_end(skb
, nlh
);
4793 nlmsg_cancel(skb
, nlh
);
4797 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
4798 struct prefix_info
*pinfo
)
4800 struct sk_buff
*skb
;
4801 struct net
*net
= dev_net(idev
->dev
);
4804 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
4808 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
4810 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4811 WARN_ON(err
== -EMSGSIZE
);
4815 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
4819 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
4822 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4824 struct net
*net
= dev_net(ifp
->idev
->dev
);
4829 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
4834 * If the address was optimistic
4835 * we inserted the route at the start of
4836 * our DAD process, so we don't need
4839 if (!(ifp
->rt
->rt6i_node
))
4840 ip6_ins_rt(ifp
->rt
);
4841 if (ifp
->idev
->cnf
.forwarding
)
4842 addrconf_join_anycast(ifp
);
4843 if (!ipv6_addr_any(&ifp
->peer_addr
))
4844 addrconf_prefix_route(&ifp
->peer_addr
, 128,
4845 ifp
->idev
->dev
, 0, 0);
4848 if (ifp
->idev
->cnf
.forwarding
)
4849 addrconf_leave_anycast(ifp
);
4850 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
4851 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
4852 struct rt6_info
*rt
;
4854 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
4855 ifp
->idev
->dev
, 0, 0);
4856 if (rt
&& ip6_del_rt(rt
))
4859 dst_hold(&ifp
->rt
->dst
);
4861 if (ip6_del_rt(ifp
->rt
))
4862 dst_free(&ifp
->rt
->dst
);
4864 rt_genid_bump_ipv6(net
);
4867 atomic_inc(&net
->ipv6
.dev_addr_genid
);
4870 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4873 if (likely(ifp
->idev
->dead
== 0))
4874 __ipv6_ifa_notify(event
, ifp
);
4875 rcu_read_unlock_bh();
4878 #ifdef CONFIG_SYSCTL
4881 int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
4882 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4884 int *valp
= ctl
->data
;
4887 struct ctl_table lctl
;
4891 * ctl->data points to idev->cnf.forwarding, we should
4892 * not modify it until we get the rtnl lock.
4897 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
4900 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
4906 static void dev_disable_change(struct inet6_dev
*idev
)
4908 struct netdev_notifier_info info
;
4910 if (!idev
|| !idev
->dev
)
4913 netdev_notifier_info_init(&info
, idev
->dev
);
4914 if (idev
->cnf
.disable_ipv6
)
4915 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
4917 addrconf_notify(NULL
, NETDEV_UP
, &info
);
4920 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
4922 struct net_device
*dev
;
4923 struct inet6_dev
*idev
;
4926 for_each_netdev_rcu(net
, dev
) {
4927 idev
= __in6_dev_get(dev
);
4929 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
4930 idev
->cnf
.disable_ipv6
= newf
;
4932 dev_disable_change(idev
);
4938 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
4943 if (!rtnl_trylock())
4944 return restart_syscall();
4946 net
= (struct net
*)table
->extra2
;
4950 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
4955 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
4956 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
4957 addrconf_disable_change(net
, newf
);
4958 } else if ((!newf
) ^ (!old
))
4959 dev_disable_change((struct inet6_dev
*)table
->extra1
);
4966 int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
4967 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4969 int *valp
= ctl
->data
;
4972 struct ctl_table lctl
;
4976 * ctl->data points to idev->cnf.disable_ipv6, we should
4977 * not modify it until we get the rtnl lock.
4982 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
4985 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
4992 int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
4993 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4995 int *valp
= ctl
->data
;
5000 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
5003 if (write
&& old
!= new) {
5004 struct net
*net
= ctl
->extra2
;
5006 if (!rtnl_trylock())
5007 return restart_syscall();
5009 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
5010 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5011 NETCONFA_IFINDEX_DEFAULT
,
5012 net
->ipv6
.devconf_dflt
);
5013 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
5014 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5015 NETCONFA_IFINDEX_ALL
,
5016 net
->ipv6
.devconf_all
);
5018 struct inet6_dev
*idev
= ctl
->extra1
;
5020 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5031 static struct addrconf_sysctl_table
5033 struct ctl_table_header
*sysctl_header
;
5034 struct ctl_table addrconf_vars
[DEVCONF_MAX
+1];
5035 } addrconf_sysctl __read_mostly
= {
5036 .sysctl_header
= NULL
,
5039 .procname
= "forwarding",
5040 .data
= &ipv6_devconf
.forwarding
,
5041 .maxlen
= sizeof(int),
5043 .proc_handler
= addrconf_sysctl_forward
,
5046 .procname
= "hop_limit",
5047 .data
= &ipv6_devconf
.hop_limit
,
5048 .maxlen
= sizeof(int),
5050 .proc_handler
= proc_dointvec
,
5054 .data
= &ipv6_devconf
.mtu6
,
5055 .maxlen
= sizeof(int),
5057 .proc_handler
= proc_dointvec
,
5060 .procname
= "accept_ra",
5061 .data
= &ipv6_devconf
.accept_ra
,
5062 .maxlen
= sizeof(int),
5064 .proc_handler
= proc_dointvec
,
5067 .procname
= "accept_redirects",
5068 .data
= &ipv6_devconf
.accept_redirects
,
5069 .maxlen
= sizeof(int),
5071 .proc_handler
= proc_dointvec
,
5074 .procname
= "autoconf",
5075 .data
= &ipv6_devconf
.autoconf
,
5076 .maxlen
= sizeof(int),
5078 .proc_handler
= proc_dointvec
,
5081 .procname
= "dad_transmits",
5082 .data
= &ipv6_devconf
.dad_transmits
,
5083 .maxlen
= sizeof(int),
5085 .proc_handler
= proc_dointvec
,
5088 .procname
= "router_solicitations",
5089 .data
= &ipv6_devconf
.rtr_solicits
,
5090 .maxlen
= sizeof(int),
5092 .proc_handler
= proc_dointvec
,
5095 .procname
= "router_solicitation_interval",
5096 .data
= &ipv6_devconf
.rtr_solicit_interval
,
5097 .maxlen
= sizeof(int),
5099 .proc_handler
= proc_dointvec_jiffies
,
5102 .procname
= "router_solicitation_delay",
5103 .data
= &ipv6_devconf
.rtr_solicit_delay
,
5104 .maxlen
= sizeof(int),
5106 .proc_handler
= proc_dointvec_jiffies
,
5109 .procname
= "force_mld_version",
5110 .data
= &ipv6_devconf
.force_mld_version
,
5111 .maxlen
= sizeof(int),
5113 .proc_handler
= proc_dointvec
,
5116 .procname
= "mldv1_unsolicited_report_interval",
5118 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
5119 .maxlen
= sizeof(int),
5121 .proc_handler
= proc_dointvec_ms_jiffies
,
5124 .procname
= "mldv2_unsolicited_report_interval",
5126 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
5127 .maxlen
= sizeof(int),
5129 .proc_handler
= proc_dointvec_ms_jiffies
,
5132 .procname
= "use_tempaddr",
5133 .data
= &ipv6_devconf
.use_tempaddr
,
5134 .maxlen
= sizeof(int),
5136 .proc_handler
= proc_dointvec
,
5139 .procname
= "temp_valid_lft",
5140 .data
= &ipv6_devconf
.temp_valid_lft
,
5141 .maxlen
= sizeof(int),
5143 .proc_handler
= proc_dointvec
,
5146 .procname
= "temp_prefered_lft",
5147 .data
= &ipv6_devconf
.temp_prefered_lft
,
5148 .maxlen
= sizeof(int),
5150 .proc_handler
= proc_dointvec
,
5153 .procname
= "regen_max_retry",
5154 .data
= &ipv6_devconf
.regen_max_retry
,
5155 .maxlen
= sizeof(int),
5157 .proc_handler
= proc_dointvec
,
5160 .procname
= "max_desync_factor",
5161 .data
= &ipv6_devconf
.max_desync_factor
,
5162 .maxlen
= sizeof(int),
5164 .proc_handler
= proc_dointvec
,
5167 .procname
= "max_addresses",
5168 .data
= &ipv6_devconf
.max_addresses
,
5169 .maxlen
= sizeof(int),
5171 .proc_handler
= proc_dointvec
,
5174 .procname
= "accept_ra_defrtr",
5175 .data
= &ipv6_devconf
.accept_ra_defrtr
,
5176 .maxlen
= sizeof(int),
5178 .proc_handler
= proc_dointvec
,
5181 .procname
= "accept_ra_pinfo",
5182 .data
= &ipv6_devconf
.accept_ra_pinfo
,
5183 .maxlen
= sizeof(int),
5185 .proc_handler
= proc_dointvec
,
5187 #ifdef CONFIG_IPV6_ROUTER_PREF
5189 .procname
= "accept_ra_rtr_pref",
5190 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
5191 .maxlen
= sizeof(int),
5193 .proc_handler
= proc_dointvec
,
5196 .procname
= "router_probe_interval",
5197 .data
= &ipv6_devconf
.rtr_probe_interval
,
5198 .maxlen
= sizeof(int),
5200 .proc_handler
= proc_dointvec_jiffies
,
5202 #ifdef CONFIG_IPV6_ROUTE_INFO
5204 .procname
= "accept_ra_rt_info_max_plen",
5205 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
5206 .maxlen
= sizeof(int),
5208 .proc_handler
= proc_dointvec
,
5213 .procname
= "proxy_ndp",
5214 .data
= &ipv6_devconf
.proxy_ndp
,
5215 .maxlen
= sizeof(int),
5217 .proc_handler
= addrconf_sysctl_proxy_ndp
,
5220 .procname
= "accept_source_route",
5221 .data
= &ipv6_devconf
.accept_source_route
,
5222 .maxlen
= sizeof(int),
5224 .proc_handler
= proc_dointvec
,
5226 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5228 .procname
= "optimistic_dad",
5229 .data
= &ipv6_devconf
.optimistic_dad
,
5230 .maxlen
= sizeof(int),
5232 .proc_handler
= proc_dointvec
,
5236 .procname
= "use_optimistic",
5237 .data
= &ipv6_devconf
.use_optimistic
,
5238 .maxlen
= sizeof(int),
5240 .proc_handler
= proc_dointvec
,
5244 #ifdef CONFIG_IPV6_MROUTE
5246 .procname
= "mc_forwarding",
5247 .data
= &ipv6_devconf
.mc_forwarding
,
5248 .maxlen
= sizeof(int),
5250 .proc_handler
= proc_dointvec
,
5254 .procname
= "disable_ipv6",
5255 .data
= &ipv6_devconf
.disable_ipv6
,
5256 .maxlen
= sizeof(int),
5258 .proc_handler
= addrconf_sysctl_disable
,
5261 .procname
= "accept_dad",
5262 .data
= &ipv6_devconf
.accept_dad
,
5263 .maxlen
= sizeof(int),
5265 .proc_handler
= proc_dointvec
,
5268 .procname
= "force_tllao",
5269 .data
= &ipv6_devconf
.force_tllao
,
5270 .maxlen
= sizeof(int),
5272 .proc_handler
= proc_dointvec
5275 .procname
= "ndisc_notify",
5276 .data
= &ipv6_devconf
.ndisc_notify
,
5277 .maxlen
= sizeof(int),
5279 .proc_handler
= proc_dointvec
5282 .procname
= "suppress_frag_ndisc",
5283 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
5284 .maxlen
= sizeof(int),
5286 .proc_handler
= proc_dointvec
5289 .procname
= "accept_ra_from_local",
5290 .data
= &ipv6_devconf
.accept_ra_from_local
,
5291 .maxlen
= sizeof(int),
5293 .proc_handler
= proc_dointvec
,
5296 .procname
= "accept_ra_mtu",
5297 .data
= &ipv6_devconf
.accept_ra_mtu
,
5298 .maxlen
= sizeof(int),
5300 .proc_handler
= proc_dointvec
,
5308 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
5309 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
5312 struct addrconf_sysctl_table
*t
;
5313 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
5315 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
5319 for (i
= 0; t
->addrconf_vars
[i
].data
; i
++) {
5320 t
->addrconf_vars
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
5321 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
5322 t
->addrconf_vars
[i
].extra2
= net
;
5325 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
5327 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->addrconf_vars
);
5328 if (t
->sysctl_header
== NULL
)
5340 static void __addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
5342 struct addrconf_sysctl_table
*t
;
5344 if (p
->sysctl
== NULL
)
5349 unregister_net_sysctl_table(t
->sysctl_header
);
5353 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
5357 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
5360 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
5361 &ndisc_ifinfo_sysctl_change
);
5364 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
5367 neigh_sysctl_unregister(idev
->nd_parms
);
5372 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
5374 __addrconf_sysctl_unregister(&idev
->cnf
);
5375 neigh_sysctl_unregister(idev
->nd_parms
);
5381 static int __net_init
addrconf_init_net(struct net
*net
)
5384 struct ipv6_devconf
*all
, *dflt
;
5386 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
5390 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
5392 goto err_alloc_dflt
;
5394 /* these will be inherited by all namespaces */
5395 dflt
->autoconf
= ipv6_defaults
.autoconf
;
5396 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
5398 net
->ipv6
.devconf_all
= all
;
5399 net
->ipv6
.devconf_dflt
= dflt
;
5401 #ifdef CONFIG_SYSCTL
5402 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
5406 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
5412 #ifdef CONFIG_SYSCTL
5414 __addrconf_sysctl_unregister(all
);
5424 static void __net_exit
addrconf_exit_net(struct net
*net
)
5426 #ifdef CONFIG_SYSCTL
5427 __addrconf_sysctl_unregister(net
->ipv6
.devconf_dflt
);
5428 __addrconf_sysctl_unregister(net
->ipv6
.devconf_all
);
5430 kfree(net
->ipv6
.devconf_dflt
);
5431 kfree(net
->ipv6
.devconf_all
);
5434 static struct pernet_operations addrconf_ops
= {
5435 .init
= addrconf_init_net
,
5436 .exit
= addrconf_exit_net
,
5439 static struct rtnl_af_ops inet6_ops __read_mostly
= {
5441 .fill_link_af
= inet6_fill_link_af
,
5442 .get_link_af_size
= inet6_get_link_af_size
,
5443 .validate_link_af
= inet6_validate_link_af
,
5444 .set_link_af
= inet6_set_link_af
,
5448 * Init / cleanup code
5451 int __init
addrconf_init(void)
5453 struct inet6_dev
*idev
;
5456 err
= ipv6_addr_label_init();
5458 pr_crit("%s: cannot initialize default policy table: %d\n",
5463 err
= register_pernet_subsys(&addrconf_ops
);
5467 addrconf_wq
= create_workqueue("ipv6_addrconf");
5473 /* The addrconf netdev notifier requires that loopback_dev
5474 * has it's ipv6 private information allocated and setup
5475 * before it can bring up and give link-local addresses
5476 * to other devices which are up.
5478 * Unfortunately, loopback_dev is not necessarily the first
5479 * entry in the global dev_base list of net devices. In fact,
5480 * it is likely to be the very last entry on that list.
5481 * So this causes the notifier registry below to try and
5482 * give link-local addresses to all devices besides loopback_dev
5483 * first, then loopback_dev, which cases all the non-loopback_dev
5484 * devices to fail to get a link-local address.
5486 * So, as a temporary fix, allocate the ipv6 structure for
5487 * loopback_dev first by hand.
5488 * Longer term, all of the dependencies ipv6 has upon the loopback
5489 * device and it being up should be removed.
5492 idev
= ipv6_add_dev(init_net
.loopback_dev
);
5495 err
= PTR_ERR(idev
);
5499 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5500 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
5502 register_netdevice_notifier(&ipv6_dev_notf
);
5506 rtnl_af_register(&inet6_ops
);
5508 err
= __rtnl_register(PF_INET6
, RTM_GETLINK
, NULL
, inet6_dump_ifinfo
,
5513 /* Only the first call to __rtnl_register can fail */
5514 __rtnl_register(PF_INET6
, RTM_NEWADDR
, inet6_rtm_newaddr
, NULL
, NULL
);
5515 __rtnl_register(PF_INET6
, RTM_DELADDR
, inet6_rtm_deladdr
, NULL
, NULL
);
5516 __rtnl_register(PF_INET6
, RTM_GETADDR
, inet6_rtm_getaddr
,
5517 inet6_dump_ifaddr
, NULL
);
5518 __rtnl_register(PF_INET6
, RTM_GETMULTICAST
, NULL
,
5519 inet6_dump_ifmcaddr
, NULL
);
5520 __rtnl_register(PF_INET6
, RTM_GETANYCAST
, NULL
,
5521 inet6_dump_ifacaddr
, NULL
);
5522 __rtnl_register(PF_INET6
, RTM_GETNETCONF
, inet6_netconf_get_devconf
,
5523 inet6_netconf_dump_devconf
, NULL
);
5525 ipv6_addr_label_rtnl_register();
5529 rtnl_af_unregister(&inet6_ops
);
5530 unregister_netdevice_notifier(&ipv6_dev_notf
);
5532 destroy_workqueue(addrconf_wq
);
5534 unregister_pernet_subsys(&addrconf_ops
);
5536 ipv6_addr_label_cleanup();
5541 void addrconf_cleanup(void)
5543 struct net_device
*dev
;
5546 unregister_netdevice_notifier(&ipv6_dev_notf
);
5547 unregister_pernet_subsys(&addrconf_ops
);
5548 ipv6_addr_label_cleanup();
5552 __rtnl_af_unregister(&inet6_ops
);
5554 /* clean dev list */
5555 for_each_netdev(&init_net
, dev
) {
5556 if (__in6_dev_get(dev
) == NULL
)
5558 addrconf_ifdown(dev
, 1);
5560 addrconf_ifdown(init_net
.loopback_dev
, 2);
5565 spin_lock_bh(&addrconf_hash_lock
);
5566 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5567 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
5568 spin_unlock_bh(&addrconf_hash_lock
);
5569 cancel_delayed_work(&addr_chk_work
);
5572 destroy_workqueue(addrconf_wq
);