2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
61 #include <linux/sysctl.h>
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
73 #include <net/af_ieee802154.h>
74 #include <net/firewire.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <linux/if_tunnel.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/netconf.h>
87 #include <linux/random.h>
88 #include <linux/uaccess.h>
89 #include <asm/unaligned.h>
91 #include <linux/proc_fs.h>
92 #include <linux/seq_file.h>
93 #include <linux/export.h>
95 /* Set to 3 to get tracing... */
99 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
101 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
104 #define INFINITY_LIFE_TIME 0xFFFFFFFF
106 #define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109 static inline u32
cstamp_delta(unsigned long cstamp
)
111 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
115 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
116 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
118 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
123 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
128 static void __ipv6_regen_rndid(struct inet6_dev
*idev
);
129 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
130 static void ipv6_regen_rndid(unsigned long data
);
132 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
133 static int ipv6_count_addresses(struct inet6_dev
*idev
);
134 static int ipv6_generate_stable_address(struct in6_addr
*addr
,
136 const struct inet6_dev
*idev
);
139 * Configured unicast address hash table
141 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
142 static DEFINE_SPINLOCK(addrconf_hash_lock
);
144 static void addrconf_verify(void);
145 static void addrconf_verify_rtnl(void);
146 static void addrconf_verify_work(struct work_struct
*);
148 static struct workqueue_struct
*addrconf_wq
;
149 static DECLARE_DELAYED_WORK(addr_chk_work
, addrconf_verify_work
);
151 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
152 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
154 static void addrconf_type_change(struct net_device
*dev
,
155 unsigned long event
);
156 static int addrconf_ifdown(struct net_device
*dev
, int how
);
158 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
160 const struct net_device
*dev
,
161 u32 flags
, u32 noflags
);
163 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
164 static void addrconf_dad_work(struct work_struct
*w
);
165 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
166 static void addrconf_dad_run(struct inet6_dev
*idev
);
167 static void addrconf_rs_timer(unsigned long data
);
168 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
169 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
171 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
172 struct prefix_info
*pinfo
);
173 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
174 struct net_device
*dev
);
176 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
178 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
179 .mtu6
= IPV6_MIN_MTU
,
181 .accept_redirects
= 1,
183 .force_mld_version
= 0,
184 .mldv1_unsolicited_report_interval
= 10 * HZ
,
185 .mldv2_unsolicited_report_interval
= HZ
,
187 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
188 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
189 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
191 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
192 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
193 .regen_max_retry
= REGEN_MAX_RETRY
,
194 .max_desync_factor
= MAX_DESYNC_FACTOR
,
195 .max_addresses
= IPV6_MAX_ADDRESSES
,
196 .accept_ra_defrtr
= 1,
197 .accept_ra_from_local
= 0,
198 .accept_ra_pinfo
= 1,
199 #ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref
= 1,
201 .rtr_probe_interval
= 60 * HZ
,
202 #ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen
= 0,
207 .accept_source_route
= 0, /* we do not accept RH0 by default. */
210 .suppress_frag_ndisc
= 1,
213 .initialized
= false,
217 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
219 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
220 .mtu6
= IPV6_MIN_MTU
,
222 .accept_redirects
= 1,
224 .force_mld_version
= 0,
225 .mldv1_unsolicited_report_interval
= 10 * HZ
,
226 .mldv2_unsolicited_report_interval
= HZ
,
228 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
229 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
230 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
232 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
233 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
234 .regen_max_retry
= REGEN_MAX_RETRY
,
235 .max_desync_factor
= MAX_DESYNC_FACTOR
,
236 .max_addresses
= IPV6_MAX_ADDRESSES
,
237 .accept_ra_defrtr
= 1,
238 .accept_ra_from_local
= 0,
239 .accept_ra_pinfo
= 1,
240 #ifdef CONFIG_IPV6_ROUTER_PREF
241 .accept_ra_rtr_pref
= 1,
242 .rtr_probe_interval
= 60 * HZ
,
243 #ifdef CONFIG_IPV6_ROUTE_INFO
244 .accept_ra_rt_info_max_plen
= 0,
248 .accept_source_route
= 0, /* we do not accept RH0 by default. */
251 .suppress_frag_ndisc
= 1,
254 .initialized
= false,
258 /* Check if a valid qdisc is available */
259 static inline bool addrconf_qdisc_ok(const struct net_device
*dev
)
261 return !qdisc_tx_is_noop(dev
);
264 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
266 if (del_timer(&idev
->rs_timer
))
270 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
272 if (cancel_delayed_work(&ifp
->dad_work
))
276 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
279 if (!timer_pending(&idev
->rs_timer
))
281 mod_timer(&idev
->rs_timer
, jiffies
+ when
);
284 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
287 if (!delayed_work_pending(&ifp
->dad_work
))
289 mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
);
292 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
296 idev
->stats
.ipv6
= alloc_percpu(struct ipstats_mib
);
297 if (!idev
->stats
.ipv6
)
300 for_each_possible_cpu(i
) {
301 struct ipstats_mib
*addrconf_stats
;
302 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
303 u64_stats_init(&addrconf_stats
->syncp
);
307 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
309 if (!idev
->stats
.icmpv6dev
)
311 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
313 if (!idev
->stats
.icmpv6msgdev
)
319 kfree(idev
->stats
.icmpv6dev
);
321 free_percpu(idev
->stats
.ipv6
);
326 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
328 struct inet6_dev
*ndev
;
333 if (dev
->mtu
< IPV6_MIN_MTU
)
334 return ERR_PTR(-EINVAL
);
336 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
340 rwlock_init(&ndev
->lock
);
342 INIT_LIST_HEAD(&ndev
->addr_list
);
343 setup_timer(&ndev
->rs_timer
, addrconf_rs_timer
,
344 (unsigned long)ndev
);
345 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
347 if (ndev
->cnf
.stable_secret
.initialized
)
348 ndev
->addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
350 ndev
->addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
;
352 ndev
->cnf
.mtu6
= dev
->mtu
;
353 ndev
->cnf
.sysctl
= NULL
;
354 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
355 if (!ndev
->nd_parms
) {
359 if (ndev
->cnf
.forwarding
)
360 dev_disable_lro(dev
);
361 /* We refer to the device */
364 if (snmp6_alloc_dev(ndev
) < 0) {
366 "%s: cannot allocate memory for statistics; dev=%s.\n",
367 __func__
, dev
->name
);
368 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
374 if (snmp6_register_dev(ndev
) < 0) {
376 "%s: cannot create /proc/net/dev_snmp6/%s\n",
377 __func__
, dev
->name
);
381 /* One reference from device. We must do this before
382 * we invoke __ipv6_regen_rndid().
386 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
387 ndev
->cnf
.accept_dad
= -1;
389 #if IS_ENABLED(CONFIG_IPV6_SIT)
390 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
391 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
392 ndev
->cnf
.rtr_solicits
= 0;
396 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
397 setup_timer(&ndev
->regen_timer
, ipv6_regen_rndid
, (unsigned long)ndev
);
398 if ((dev
->flags
&IFF_LOOPBACK
) ||
399 dev
->type
== ARPHRD_TUNNEL
||
400 dev
->type
== ARPHRD_TUNNEL6
||
401 dev
->type
== ARPHRD_SIT
||
402 dev
->type
== ARPHRD_NONE
) {
403 ndev
->cnf
.use_tempaddr
= -1;
406 ipv6_regen_rndid((unsigned long) ndev
);
409 ndev
->token
= in6addr_any
;
411 if (netif_running(dev
) && addrconf_qdisc_ok(dev
))
412 ndev
->if_flags
|= IF_READY
;
414 ipv6_mc_init_dev(ndev
);
415 ndev
->tstamp
= jiffies
;
416 err
= addrconf_sysctl_register(ndev
);
418 ipv6_mc_destroy_dev(ndev
);
419 del_timer(&ndev
->regen_timer
);
420 snmp6_unregister_dev(ndev
);
423 /* protected by rtnl_lock */
424 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
426 /* Join interface-local all-node multicast group */
427 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
429 /* Join all-node multicast group */
430 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
432 /* Join all-router multicast group if forwarding is set */
433 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
434 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
439 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
441 in6_dev_finish_destroy(ndev
);
445 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
447 struct inet6_dev
*idev
;
451 idev
= __in6_dev_get(dev
);
453 idev
= ipv6_add_dev(dev
);
458 if (dev
->flags
&IFF_UP
)
463 static int inet6_netconf_msgsize_devconf(int type
)
465 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
466 + nla_total_size(4); /* NETCONFA_IFINDEX */
468 /* type -1 is used for ALL */
469 if (type
== -1 || type
== NETCONFA_FORWARDING
)
470 size
+= nla_total_size(4);
471 #ifdef CONFIG_IPV6_MROUTE
472 if (type
== -1 || type
== NETCONFA_MC_FORWARDING
)
473 size
+= nla_total_size(4);
475 if (type
== -1 || type
== NETCONFA_PROXY_NEIGH
)
476 size
+= nla_total_size(4);
481 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
482 struct ipv6_devconf
*devconf
, u32 portid
,
483 u32 seq
, int event
, unsigned int flags
,
486 struct nlmsghdr
*nlh
;
487 struct netconfmsg
*ncm
;
489 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
494 ncm
= nlmsg_data(nlh
);
495 ncm
->ncm_family
= AF_INET6
;
497 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
498 goto nla_put_failure
;
500 /* type -1 is used for ALL */
501 if ((type
== -1 || type
== NETCONFA_FORWARDING
) &&
502 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
503 goto nla_put_failure
;
504 #ifdef CONFIG_IPV6_MROUTE
505 if ((type
== -1 || type
== NETCONFA_MC_FORWARDING
) &&
506 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
507 devconf
->mc_forwarding
) < 0)
508 goto nla_put_failure
;
510 if ((type
== -1 || type
== NETCONFA_PROXY_NEIGH
) &&
511 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
512 goto nla_put_failure
;
518 nlmsg_cancel(skb
, nlh
);
522 void inet6_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
523 struct ipv6_devconf
*devconf
)
528 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_ATOMIC
);
532 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
533 RTM_NEWNETCONF
, 0, type
);
535 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
536 WARN_ON(err
== -EMSGSIZE
);
540 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_ATOMIC
);
543 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
546 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
547 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
548 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
549 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
552 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
553 struct nlmsghdr
*nlh
)
555 struct net
*net
= sock_net(in_skb
->sk
);
556 struct nlattr
*tb
[NETCONFA_MAX
+1];
557 struct netconfmsg
*ncm
;
559 struct ipv6_devconf
*devconf
;
560 struct inet6_dev
*in6_dev
;
561 struct net_device
*dev
;
565 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
566 devconf_ipv6_policy
);
571 if (!tb
[NETCONFA_IFINDEX
])
574 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
576 case NETCONFA_IFINDEX_ALL
:
577 devconf
= net
->ipv6
.devconf_all
;
579 case NETCONFA_IFINDEX_DEFAULT
:
580 devconf
= net
->ipv6
.devconf_dflt
;
583 dev
= __dev_get_by_index(net
, ifindex
);
586 in6_dev
= __in6_dev_get(dev
);
589 devconf
= &in6_dev
->cnf
;
594 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC
);
598 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
599 NETLINK_CB(in_skb
).portid
,
600 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
603 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
604 WARN_ON(err
== -EMSGSIZE
);
608 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
613 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
614 struct netlink_callback
*cb
)
616 struct net
*net
= sock_net(skb
->sk
);
619 struct net_device
*dev
;
620 struct inet6_dev
*idev
;
621 struct hlist_head
*head
;
624 s_idx
= idx
= cb
->args
[1];
626 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
628 head
= &net
->dev_index_head
[h
];
630 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
632 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
635 idev
= __in6_dev_get(dev
);
639 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
641 NETLINK_CB(cb
->skb
).portid
,
649 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
655 if (h
== NETDEV_HASHENTRIES
) {
656 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
657 net
->ipv6
.devconf_all
,
658 NETLINK_CB(cb
->skb
).portid
,
660 RTM_NEWNETCONF
, NLM_F_MULTI
,
666 if (h
== NETDEV_HASHENTRIES
+ 1) {
667 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
668 net
->ipv6
.devconf_dflt
,
669 NETLINK_CB(cb
->skb
).portid
,
671 RTM_NEWNETCONF
, NLM_F_MULTI
,
685 static void dev_forward_change(struct inet6_dev
*idev
)
687 struct net_device
*dev
;
688 struct inet6_ifaddr
*ifa
;
693 if (idev
->cnf
.forwarding
)
694 dev_disable_lro(dev
);
695 if (dev
->flags
& IFF_MULTICAST
) {
696 if (idev
->cnf
.forwarding
) {
697 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
698 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
699 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
701 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
702 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
703 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
707 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
708 if (ifa
->flags
&IFA_F_TENTATIVE
)
710 if (idev
->cnf
.forwarding
)
711 addrconf_join_anycast(ifa
);
713 addrconf_leave_anycast(ifa
);
715 inet6_netconf_notify_devconf(dev_net(dev
), NETCONFA_FORWARDING
,
716 dev
->ifindex
, &idev
->cnf
);
720 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
722 struct net_device
*dev
;
723 struct inet6_dev
*idev
;
725 for_each_netdev(net
, dev
) {
726 idev
= __in6_dev_get(dev
);
728 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
729 idev
->cnf
.forwarding
= newf
;
731 dev_forward_change(idev
);
736 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
742 return restart_syscall();
744 net
= (struct net
*)table
->extra2
;
748 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
749 if ((!newf
) ^ (!old
))
750 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
751 NETCONFA_IFINDEX_DEFAULT
,
752 net
->ipv6
.devconf_dflt
);
757 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
758 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
759 addrconf_forward_change(net
, newf
);
760 if ((!newf
) ^ (!old
))
761 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
762 NETCONFA_IFINDEX_ALL
,
763 net
->ipv6
.devconf_all
);
764 } else if ((!newf
) ^ (!old
))
765 dev_forward_change((struct inet6_dev
*)table
->extra1
);
769 rt6_purge_dflt_routers(net
);
774 /* Nobody refers to this ifaddr, destroy it */
775 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
777 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
779 #ifdef NET_REFCNT_DEBUG
780 pr_debug("%s\n", __func__
);
783 in6_dev_put(ifp
->idev
);
785 if (cancel_delayed_work(&ifp
->dad_work
))
786 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
789 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
790 pr_warn("Freeing alive inet6 address %p\n", ifp
);
799 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
802 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
805 * Each device address list is sorted in order of scope -
806 * global before linklocal.
808 list_for_each(p
, &idev
->addr_list
) {
809 struct inet6_ifaddr
*ifa
810 = list_entry(p
, struct inet6_ifaddr
, if_list
);
811 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
815 list_add_tail(&ifp
->if_list
, p
);
818 static u32
inet6_addr_hash(const struct in6_addr
*addr
)
820 return hash_32(ipv6_addr_hash(addr
), IN6_ADDR_HSIZE_SHIFT
);
823 /* On success it returns ifp with increased reference count */
825 static struct inet6_ifaddr
*
826 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
827 const struct in6_addr
*peer_addr
, int pfxlen
,
828 int scope
, u32 flags
, u32 valid_lft
, u32 prefered_lft
)
830 struct inet6_ifaddr
*ifa
= NULL
;
834 int addr_type
= ipv6_addr_type(addr
);
836 if (addr_type
== IPV6_ADDR_ANY
||
837 addr_type
& IPV6_ADDR_MULTICAST
||
838 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
839 addr_type
& IPV6_ADDR_LOOPBACK
))
840 return ERR_PTR(-EADDRNOTAVAIL
);
844 err
= -ENODEV
; /*XXX*/
848 if (idev
->cnf
.disable_ipv6
) {
853 spin_lock(&addrconf_hash_lock
);
855 /* Ignore adding duplicate addresses on an interface */
856 if (ipv6_chk_same_addr(dev_net(idev
->dev
), addr
, idev
->dev
)) {
857 ADBG("ipv6_add_addr: already assigned\n");
862 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
865 ADBG("ipv6_add_addr: malloc failed\n");
870 rt
= addrconf_dst_alloc(idev
, addr
, false);
876 neigh_parms_data_state_setall(idev
->nd_parms
);
880 ifa
->peer_addr
= *peer_addr
;
882 spin_lock_init(&ifa
->lock
);
883 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
884 INIT_HLIST_NODE(&ifa
->addr_lst
);
886 ifa
->prefix_len
= pfxlen
;
887 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
888 ifa
->valid_lft
= valid_lft
;
889 ifa
->prefered_lft
= prefered_lft
;
890 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
891 ifa
->tokenized
= false;
900 /* Add to big hash table */
901 hash
= inet6_addr_hash(addr
);
903 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
904 spin_unlock(&addrconf_hash_lock
);
906 write_lock(&idev
->lock
);
907 /* Add to inet6_dev unicast addr list. */
908 ipv6_link_dev_addr(idev
, ifa
);
910 if (ifa
->flags
&IFA_F_TEMPORARY
) {
911 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
916 write_unlock(&idev
->lock
);
918 rcu_read_unlock_bh();
920 if (likely(err
== 0))
921 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
929 spin_unlock(&addrconf_hash_lock
);
933 enum cleanup_prefix_rt_t
{
934 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
935 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
936 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
940 * Check, whether the prefix for ifp would still need a prefix route
941 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
944 * 1) we don't purge prefix if address was not permanent.
945 * prefix is managed by its own lifetime.
946 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
947 * 3) if there are no addresses, delete prefix.
948 * 4) if there are still other permanent address(es),
949 * corresponding prefix is still permanent.
950 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
951 * don't purge the prefix, assume user space is managing it.
952 * 6) otherwise, update prefix lifetime to the
953 * longest valid lifetime among the corresponding
954 * addresses on the device.
955 * Note: subsequent RA will update lifetime.
957 static enum cleanup_prefix_rt_t
958 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
960 struct inet6_ifaddr
*ifa
;
961 struct inet6_dev
*idev
= ifp
->idev
;
962 unsigned long lifetime
;
963 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
967 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
970 if (!ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
973 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
974 return CLEANUP_PREFIX_RT_NOP
;
976 action
= CLEANUP_PREFIX_RT_EXPIRE
;
978 spin_lock(&ifa
->lock
);
980 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
982 * Note: Because this address is
983 * not permanent, lifetime <
984 * LONG_MAX / HZ here.
986 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
987 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
988 spin_unlock(&ifa
->lock
);
995 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
, bool del_rt
)
999 rt
= addrconf_get_prefix_route(&ifp
->addr
,
1002 0, RTF_GATEWAY
| RTF_DEFAULT
);
1007 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
1008 rt6_set_expires(rt
, expires
);
1015 /* This function wants to get referenced ifp and releases it before return */
1017 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1020 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1021 unsigned long expires
;
1025 spin_lock_bh(&ifp
->lock
);
1027 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1028 spin_unlock_bh(&ifp
->lock
);
1030 if (state
== INET6_IFADDR_STATE_DEAD
)
1033 spin_lock_bh(&addrconf_hash_lock
);
1034 hlist_del_init_rcu(&ifp
->addr_lst
);
1035 spin_unlock_bh(&addrconf_hash_lock
);
1037 write_lock_bh(&ifp
->idev
->lock
);
1039 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1040 list_del(&ifp
->tmp_list
);
1042 in6_ifa_put(ifp
->ifpub
);
1048 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1049 action
= check_cleanup_prefix_route(ifp
, &expires
);
1051 list_del_init(&ifp
->if_list
);
1054 write_unlock_bh(&ifp
->idev
->lock
);
1056 addrconf_del_dad_work(ifp
);
1058 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1060 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1062 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1063 cleanup_prefix_route(ifp
, expires
,
1064 action
== CLEANUP_PREFIX_RT_DEL
);
1067 /* clean up prefsrc entries */
1068 rt6_remove_prefsrc(ifp
);
1073 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
1075 struct inet6_dev
*idev
= ifp
->idev
;
1076 struct in6_addr addr
, *tmpaddr
;
1077 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_tstamp
, age
;
1078 unsigned long regen_advance
;
1082 unsigned long now
= jiffies
;
1084 write_lock_bh(&idev
->lock
);
1086 spin_lock_bh(&ift
->lock
);
1087 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1088 spin_unlock_bh(&ift
->lock
);
1095 if (idev
->cnf
.use_tempaddr
<= 0) {
1096 write_unlock_bh(&idev
->lock
);
1097 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1102 spin_lock_bh(&ifp
->lock
);
1103 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1104 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1105 spin_unlock_bh(&ifp
->lock
);
1106 write_unlock_bh(&idev
->lock
);
1107 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1114 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1115 __ipv6_try_regen_rndid(idev
, tmpaddr
);
1116 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1117 age
= (now
- ifp
->tstamp
) / HZ
;
1118 tmp_valid_lft
= min_t(__u32
,
1120 idev
->cnf
.temp_valid_lft
+ age
);
1121 tmp_prefered_lft
= min_t(__u32
,
1123 idev
->cnf
.temp_prefered_lft
+ age
-
1124 idev
->cnf
.max_desync_factor
);
1125 tmp_plen
= ifp
->prefix_len
;
1126 tmp_tstamp
= ifp
->tstamp
;
1127 spin_unlock_bh(&ifp
->lock
);
1129 regen_advance
= idev
->cnf
.regen_max_retry
*
1130 idev
->cnf
.dad_transmits
*
1131 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) / HZ
;
1132 write_unlock_bh(&idev
->lock
);
1134 /* A temporary address is created only if this calculated Preferred
1135 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1136 * an implementation must not create a temporary address with a zero
1137 * Preferred Lifetime.
1138 * Use age calculation as in addrconf_verify to avoid unnecessary
1139 * temporary addresses being generated.
1141 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1142 if (tmp_prefered_lft
<= regen_advance
+ age
) {
1149 addr_flags
= IFA_F_TEMPORARY
;
1150 /* set in addrconf_prefix_rcv() */
1151 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1152 addr_flags
|= IFA_F_OPTIMISTIC
;
1154 ift
= ipv6_add_addr(idev
, &addr
, NULL
, tmp_plen
,
1155 ipv6_addr_scope(&addr
), addr_flags
,
1156 tmp_valid_lft
, tmp_prefered_lft
);
1160 pr_info("%s: retry temporary address regeneration\n", __func__
);
1162 write_lock_bh(&idev
->lock
);
1166 spin_lock_bh(&ift
->lock
);
1169 ift
->tstamp
= tmp_tstamp
;
1170 spin_unlock_bh(&ift
->lock
);
1172 addrconf_dad_start(ift
);
1180 * Choose an appropriate source address (RFC3484)
1183 IPV6_SADDR_RULE_INIT
= 0,
1184 IPV6_SADDR_RULE_LOCAL
,
1185 IPV6_SADDR_RULE_SCOPE
,
1186 IPV6_SADDR_RULE_PREFERRED
,
1187 #ifdef CONFIG_IPV6_MIP6
1188 IPV6_SADDR_RULE_HOA
,
1190 IPV6_SADDR_RULE_OIF
,
1191 IPV6_SADDR_RULE_LABEL
,
1192 IPV6_SADDR_RULE_PRIVACY
,
1193 IPV6_SADDR_RULE_ORCHID
,
1194 IPV6_SADDR_RULE_PREFIX
,
1195 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1196 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1201 struct ipv6_saddr_score
{
1204 struct inet6_ifaddr
*ifa
;
1205 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1210 struct ipv6_saddr_dst
{
1211 const struct in6_addr
*addr
;
1218 static inline int ipv6_saddr_preferred(int type
)
1220 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1225 static inline bool ipv6_use_optimistic_addr(struct inet6_dev
*idev
)
1227 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1228 return idev
&& idev
->cnf
.optimistic_dad
&& idev
->cnf
.use_optimistic
;
1234 static int ipv6_get_saddr_eval(struct net
*net
,
1235 struct ipv6_saddr_score
*score
,
1236 struct ipv6_saddr_dst
*dst
,
1241 if (i
<= score
->rule
) {
1243 case IPV6_SADDR_RULE_SCOPE
:
1244 ret
= score
->scopedist
;
1246 case IPV6_SADDR_RULE_PREFIX
:
1247 ret
= score
->matchlen
;
1250 ret
= !!test_bit(i
, score
->scorebits
);
1256 case IPV6_SADDR_RULE_INIT
:
1257 /* Rule 0: remember if hiscore is not ready yet */
1260 case IPV6_SADDR_RULE_LOCAL
:
1261 /* Rule 1: Prefer same address */
1262 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1264 case IPV6_SADDR_RULE_SCOPE
:
1265 /* Rule 2: Prefer appropriate scope
1270 * ---+--+-+---> scope
1272 * | d is scope of the destination.
1274 * | \ <- smaller scope is better if
1275 * B-15 | \ if scope is enough for destination.
1276 * | ret = B - scope (-1 <= scope >= d <= 15).
1278 * |/ <- greater is better
1279 * -C / if scope is not enough for destination.
1280 * /| ret = scope - C (-1 <= d < scope <= 15).
1282 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1283 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1284 * Assume B = 0 and we get C > 29.
1286 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1287 if (ret
>= dst
->scope
)
1290 ret
-= 128; /* 30 is enough */
1291 score
->scopedist
= ret
;
1293 case IPV6_SADDR_RULE_PREFERRED
:
1295 /* Rule 3: Avoid deprecated and optimistic addresses */
1296 u8 avoid
= IFA_F_DEPRECATED
;
1298 if (!ipv6_use_optimistic_addr(score
->ifa
->idev
))
1299 avoid
|= IFA_F_OPTIMISTIC
;
1300 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1301 !(score
->ifa
->flags
& avoid
);
1304 #ifdef CONFIG_IPV6_MIP6
1305 case IPV6_SADDR_RULE_HOA
:
1307 /* Rule 4: Prefer home address */
1308 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1309 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1313 case IPV6_SADDR_RULE_OIF
:
1314 /* Rule 5: Prefer outgoing interface */
1315 ret
= (!dst
->ifindex
||
1316 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1318 case IPV6_SADDR_RULE_LABEL
:
1319 /* Rule 6: Prefer matching label */
1320 ret
= ipv6_addr_label(net
,
1321 &score
->ifa
->addr
, score
->addr_type
,
1322 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1324 case IPV6_SADDR_RULE_PRIVACY
:
1326 /* Rule 7: Prefer public address
1327 * Note: prefer temporary address if use_tempaddr >= 2
1329 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1330 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1331 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1332 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1335 case IPV6_SADDR_RULE_ORCHID
:
1336 /* Rule 8-: Prefer ORCHID vs ORCHID or
1337 * non-ORCHID vs non-ORCHID
1339 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1340 ipv6_addr_orchid(dst
->addr
));
1342 case IPV6_SADDR_RULE_PREFIX
:
1343 /* Rule 8: Use longest matching prefix */
1344 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1345 if (ret
> score
->ifa
->prefix_len
)
1346 ret
= score
->ifa
->prefix_len
;
1347 score
->matchlen
= ret
;
1349 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1350 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1351 /* Optimistic addresses still have lower precedence than other
1352 * preferred addresses.
1354 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1362 __set_bit(i
, score
->scorebits
);
1368 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1369 const struct in6_addr
*daddr
, unsigned int prefs
,
1370 struct in6_addr
*saddr
)
1372 struct ipv6_saddr_score scores
[2],
1373 *score
= &scores
[0], *hiscore
= &scores
[1];
1374 struct ipv6_saddr_dst dst
;
1375 struct net_device
*dev
;
1378 dst_type
= __ipv6_addr_type(daddr
);
1380 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1381 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1382 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1386 hiscore
->ifa
= NULL
;
1390 for_each_netdev_rcu(net
, dev
) {
1391 struct inet6_dev
*idev
;
1393 /* Candidate Source Address (section 4)
1394 * - multicast and link-local destination address,
1395 * the set of candidate source address MUST only
1396 * include addresses assigned to interfaces
1397 * belonging to the same link as the outgoing
1399 * (- For site-local destination addresses, the
1400 * set of candidate source addresses MUST only
1401 * include addresses assigned to interfaces
1402 * belonging to the same site as the outgoing
1405 if (((dst_type
& IPV6_ADDR_MULTICAST
) ||
1406 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
1407 dst
.ifindex
&& dev
->ifindex
!= dst
.ifindex
)
1410 idev
= __in6_dev_get(dev
);
1414 read_lock_bh(&idev
->lock
);
1415 list_for_each_entry(score
->ifa
, &idev
->addr_list
, if_list
) {
1419 * - Tentative Address (RFC2462 section 5.4)
1420 * - A tentative address is not considered
1421 * "assigned to an interface" in the traditional
1422 * sense, unless it is also flagged as optimistic.
1423 * - Candidate Source Address (section 4)
1424 * - In any case, anycast addresses, multicast
1425 * addresses, and the unspecified address MUST
1426 * NOT be included in a candidate set.
1428 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1429 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1432 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1434 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1435 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1436 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1442 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1444 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1445 int minihiscore
, miniscore
;
1447 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, &dst
, i
);
1448 miniscore
= ipv6_get_saddr_eval(net
, score
, &dst
, i
);
1450 if (minihiscore
> miniscore
) {
1451 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1452 score
->scopedist
> 0) {
1455 * each remaining entry
1456 * has too small (not enough)
1457 * scope, because ifa entries
1458 * are sorted by their scope
1464 } else if (minihiscore
< miniscore
) {
1466 in6_ifa_put(hiscore
->ifa
);
1468 in6_ifa_hold(score
->ifa
);
1470 swap(hiscore
, score
);
1472 /* restore our iterator */
1473 score
->ifa
= hiscore
->ifa
;
1480 read_unlock_bh(&idev
->lock
);
1485 return -EADDRNOTAVAIL
;
1487 *saddr
= hiscore
->ifa
->addr
;
1488 in6_ifa_put(hiscore
->ifa
);
1491 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1493 int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1496 struct inet6_ifaddr
*ifp
;
1497 int err
= -EADDRNOTAVAIL
;
1499 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1500 if (ifp
->scope
> IFA_LINK
)
1502 if (ifp
->scope
== IFA_LINK
&&
1503 !(ifp
->flags
& banned_flags
)) {
1512 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1515 struct inet6_dev
*idev
;
1516 int err
= -EADDRNOTAVAIL
;
1519 idev
= __in6_dev_get(dev
);
1521 read_lock_bh(&idev
->lock
);
1522 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1523 read_unlock_bh(&idev
->lock
);
1529 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1532 struct inet6_ifaddr
*ifp
;
1534 read_lock_bh(&idev
->lock
);
1535 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
)
1537 read_unlock_bh(&idev
->lock
);
1541 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1542 const struct net_device
*dev
, int strict
)
1544 return ipv6_chk_addr_and_flags(net
, addr
, dev
, strict
, IFA_F_TENTATIVE
);
1546 EXPORT_SYMBOL(ipv6_chk_addr
);
1548 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1549 const struct net_device
*dev
, int strict
,
1552 struct inet6_ifaddr
*ifp
;
1553 unsigned int hash
= inet6_addr_hash(addr
);
1557 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1558 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1560 /* Decouple optimistic from tentative for evaluation here.
1561 * Ban optimistic addresses explicitly, when required.
1563 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1564 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1566 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1567 !(ifp_flags
&banned_flags
) &&
1568 (!dev
|| ifp
->idev
->dev
== dev
||
1569 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1570 rcu_read_unlock_bh();
1575 rcu_read_unlock_bh();
1578 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1580 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1581 struct net_device
*dev
)
1583 unsigned int hash
= inet6_addr_hash(addr
);
1584 struct inet6_ifaddr
*ifp
;
1586 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1587 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1589 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1590 if (!dev
|| ifp
->idev
->dev
== dev
)
1597 /* Compares an address/prefix_len with addresses on device @dev.
1598 * If one is found it returns true.
1600 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1601 const unsigned int prefix_len
, struct net_device
*dev
)
1603 struct inet6_dev
*idev
;
1604 struct inet6_ifaddr
*ifa
;
1608 idev
= __in6_dev_get(dev
);
1610 read_lock_bh(&idev
->lock
);
1611 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1612 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
1616 read_unlock_bh(&idev
->lock
);
1622 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
1624 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1626 struct inet6_dev
*idev
;
1627 struct inet6_ifaddr
*ifa
;
1632 idev
= __in6_dev_get(dev
);
1634 read_lock_bh(&idev
->lock
);
1635 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1636 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1641 read_unlock_bh(&idev
->lock
);
1646 EXPORT_SYMBOL(ipv6_chk_prefix
);
1648 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1649 struct net_device
*dev
, int strict
)
1651 struct inet6_ifaddr
*ifp
, *result
= NULL
;
1652 unsigned int hash
= inet6_addr_hash(addr
);
1655 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1656 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1658 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1659 if (!dev
|| ifp
->idev
->dev
== dev
||
1660 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1667 rcu_read_unlock_bh();
1672 /* Gets referenced address, destroys ifaddr */
1674 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
1676 if (ifp
->flags
&IFA_F_PERMANENT
) {
1677 spin_lock_bh(&ifp
->lock
);
1678 addrconf_del_dad_work(ifp
);
1679 ifp
->flags
|= IFA_F_TENTATIVE
;
1681 ifp
->flags
|= IFA_F_DADFAILED
;
1682 spin_unlock_bh(&ifp
->lock
);
1684 ipv6_ifa_notify(0, ifp
);
1686 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1687 struct inet6_ifaddr
*ifpub
;
1688 spin_lock_bh(&ifp
->lock
);
1691 in6_ifa_hold(ifpub
);
1692 spin_unlock_bh(&ifp
->lock
);
1693 ipv6_create_tempaddr(ifpub
, ifp
);
1696 spin_unlock_bh(&ifp
->lock
);
1704 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
1708 spin_lock_bh(&ifp
->lock
);
1709 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
1710 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
1713 spin_unlock_bh(&ifp
->lock
);
1718 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1720 struct in6_addr addr
;
1721 struct inet6_dev
*idev
= ifp
->idev
;
1722 struct net
*net
= dev_net(ifp
->idev
->dev
);
1724 if (addrconf_dad_end(ifp
)) {
1729 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1730 ifp
->idev
->dev
->name
, &ifp
->addr
);
1732 spin_lock_bh(&ifp
->lock
);
1734 if (ifp
->flags
& IFA_F_STABLE_PRIVACY
) {
1735 int scope
= ifp
->scope
;
1736 u32 flags
= ifp
->flags
;
1737 struct in6_addr new_addr
;
1738 struct inet6_ifaddr
*ifp2
;
1739 u32 valid_lft
, preferred_lft
;
1740 int pfxlen
= ifp
->prefix_len
;
1741 int retries
= ifp
->stable_privacy_retry
+ 1;
1743 if (retries
> net
->ipv6
.sysctl
.idgen_retries
) {
1744 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1745 ifp
->idev
->dev
->name
);
1749 new_addr
= ifp
->addr
;
1750 if (ipv6_generate_stable_address(&new_addr
, retries
,
1754 valid_lft
= ifp
->valid_lft
;
1755 preferred_lft
= ifp
->prefered_lft
;
1757 spin_unlock_bh(&ifp
->lock
);
1759 if (idev
->cnf
.max_addresses
&&
1760 ipv6_count_addresses(idev
) >=
1761 idev
->cnf
.max_addresses
)
1764 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1765 ifp
->idev
->dev
->name
);
1767 ifp2
= ipv6_add_addr(idev
, &new_addr
, NULL
, pfxlen
,
1768 scope
, flags
, valid_lft
,
1773 spin_lock_bh(&ifp2
->lock
);
1774 ifp2
->stable_privacy_retry
= retries
;
1775 ifp2
->state
= INET6_IFADDR_STATE_PREDAD
;
1776 spin_unlock_bh(&ifp2
->lock
);
1778 addrconf_mod_dad_work(ifp2
, net
->ipv6
.sysctl
.idgen_delay
);
1781 spin_lock_bh(&ifp
->lock
);
1782 } else if (idev
->cnf
.accept_dad
> 1 && !idev
->cnf
.disable_ipv6
) {
1783 addr
.s6_addr32
[0] = htonl(0xfe800000);
1784 addr
.s6_addr32
[1] = 0;
1786 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
1787 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
1788 /* DAD failed for link-local based on MAC address */
1789 idev
->cnf
.disable_ipv6
= 1;
1791 pr_info("%s: IPv6 being disabled!\n",
1792 ifp
->idev
->dev
->name
);
1797 /* transition from _POSTDAD to _ERRDAD */
1798 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
1799 spin_unlock_bh(&ifp
->lock
);
1801 addrconf_mod_dad_work(ifp
, 0);
1804 /* Join to solicited addr multicast group.
1805 * caller must hold RTNL */
1806 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
1808 struct in6_addr maddr
;
1810 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1813 addrconf_addr_solict_mult(addr
, &maddr
);
1814 ipv6_dev_mc_inc(dev
, &maddr
);
1817 /* caller must hold RTNL */
1818 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
1820 struct in6_addr maddr
;
1822 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1825 addrconf_addr_solict_mult(addr
, &maddr
);
1826 __ipv6_dev_mc_dec(idev
, &maddr
);
1829 /* caller must hold RTNL */
1830 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1832 struct in6_addr addr
;
1834 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1836 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1837 if (ipv6_addr_any(&addr
))
1839 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
1842 /* caller must hold RTNL */
1843 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1845 struct in6_addr addr
;
1847 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
1849 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1850 if (ipv6_addr_any(&addr
))
1852 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1855 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1857 if (dev
->addr_len
!= ETH_ALEN
)
1859 memcpy(eui
, dev
->dev_addr
, 3);
1860 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1863 * The zSeries OSA network cards can be shared among various
1864 * OS instances, but the OSA cards have only one MAC address.
1865 * This leads to duplicate address conflicts in conjunction
1866 * with IPv6 if more than one instance uses the same card.
1868 * The driver for these cards can deliver a unique 16-bit
1869 * identifier for each instance sharing the same card. It is
1870 * placed instead of 0xFFFE in the interface identifier. The
1871 * "u" bit of the interface identifier is not inverted in this
1872 * case. Hence the resulting interface identifier has local
1873 * scope according to RFC2373.
1876 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1877 eui
[4] = dev
->dev_id
& 0xFF;
1886 static int addrconf_ifid_eui64(u8
*eui
, struct net_device
*dev
)
1888 if (dev
->addr_len
!= IEEE802154_ADDR_LEN
)
1890 memcpy(eui
, dev
->dev_addr
, 8);
1895 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
1897 union fwnet_hwaddr
*ha
;
1899 if (dev
->addr_len
!= FWNET_ALEN
)
1902 ha
= (union fwnet_hwaddr
*)dev
->dev_addr
;
1904 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
1909 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1911 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1912 if (dev
->addr_len
!= ARCNET_ALEN
)
1915 eui
[7] = *(u8
*)dev
->dev_addr
;
1919 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1921 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1923 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1928 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
1932 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
1933 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
1934 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
1935 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
1936 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
1937 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
1941 memcpy(eui
+ 4, &addr
, 4);
1945 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
1947 if (dev
->priv_flags
& IFF_ISATAP
)
1948 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1952 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
1954 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1957 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
1959 memcpy(eui
, dev
->perm_addr
, 3);
1960 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
1967 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1969 switch (dev
->type
) {
1972 return addrconf_ifid_eui48(eui
, dev
);
1974 return addrconf_ifid_arcnet(eui
, dev
);
1975 case ARPHRD_INFINIBAND
:
1976 return addrconf_ifid_infiniband(eui
, dev
);
1978 return addrconf_ifid_sit(eui
, dev
);
1980 return addrconf_ifid_gre(eui
, dev
);
1981 case ARPHRD_6LOWPAN
:
1982 case ARPHRD_IEEE802154
:
1983 return addrconf_ifid_eui64(eui
, dev
);
1984 case ARPHRD_IEEE1394
:
1985 return addrconf_ifid_ieee1394(eui
, dev
);
1986 case ARPHRD_TUNNEL6
:
1987 return addrconf_ifid_ip6tnl(eui
, dev
);
1992 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1995 struct inet6_ifaddr
*ifp
;
1997 read_lock_bh(&idev
->lock
);
1998 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1999 if (ifp
->scope
> IFA_LINK
)
2001 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2002 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
2007 read_unlock_bh(&idev
->lock
);
2011 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2012 static void __ipv6_regen_rndid(struct inet6_dev
*idev
)
2015 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
2016 idev
->rndid
[0] &= ~0x02;
2019 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2020 * check if generated address is not inappropriate
2022 * - Reserved subnet anycast (RFC 2526)
2023 * 11111101 11....11 1xxxxxxx
2024 * - ISATAP (RFC4214) 6.1
2025 * 00-00-5E-FE-xx-xx-xx-xx
2027 * - XXX: already assigned to an address on the device
2029 if (idev
->rndid
[0] == 0xfd &&
2030 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
2031 (idev
->rndid
[7]&0x80))
2033 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
2034 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
2036 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
2041 static void ipv6_regen_rndid(unsigned long data
)
2043 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
2044 unsigned long expires
;
2047 write_lock_bh(&idev
->lock
);
2052 __ipv6_regen_rndid(idev
);
2055 idev
->cnf
.temp_prefered_lft
* HZ
-
2056 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
*
2057 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) -
2058 idev
->cnf
.max_desync_factor
* HZ
;
2059 if (time_before(expires
, jiffies
)) {
2060 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2061 __func__
, idev
->dev
->name
);
2065 if (!mod_timer(&idev
->regen_timer
, expires
))
2069 write_unlock_bh(&idev
->lock
);
2070 rcu_read_unlock_bh();
2074 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
2076 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
2077 __ipv6_regen_rndid(idev
);
2085 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
2086 unsigned long expires
, u32 flags
)
2088 struct fib6_config cfg
= {
2089 .fc_table
= RT6_TABLE_PREFIX
,
2090 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2091 .fc_ifindex
= dev
->ifindex
,
2092 .fc_expires
= expires
,
2094 .fc_flags
= RTF_UP
| flags
,
2095 .fc_nlinfo
.nl_net
= dev_net(dev
),
2096 .fc_protocol
= RTPROT_KERNEL
,
2101 /* Prevent useless cloning on PtP SIT.
2102 This thing is done here expecting that the whole
2103 class of non-broadcast devices need not cloning.
2105 #if IS_ENABLED(CONFIG_IPV6_SIT)
2106 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2107 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2110 ip6_route_add(&cfg
);
2114 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2116 const struct net_device
*dev
,
2117 u32 flags
, u32 noflags
)
2119 struct fib6_node
*fn
;
2120 struct rt6_info
*rt
= NULL
;
2121 struct fib6_table
*table
;
2123 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_PREFIX
);
2127 read_lock_bh(&table
->tb6_lock
);
2128 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0);
2131 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
2132 if (rt
->dst
.dev
->ifindex
!= dev
->ifindex
)
2134 if ((rt
->rt6i_flags
& flags
) != flags
)
2136 if ((rt
->rt6i_flags
& noflags
) != 0)
2142 read_unlock_bh(&table
->tb6_lock
);
2147 /* Create "default" multicast route to the interface */
2149 static void addrconf_add_mroute(struct net_device
*dev
)
2151 struct fib6_config cfg
= {
2152 .fc_table
= RT6_TABLE_LOCAL
,
2153 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2154 .fc_ifindex
= dev
->ifindex
,
2157 .fc_nlinfo
.nl_net
= dev_net(dev
),
2160 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2162 ip6_route_add(&cfg
);
2165 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2167 struct inet6_dev
*idev
;
2171 idev
= ipv6_find_idev(dev
);
2173 return ERR_PTR(-ENOBUFS
);
2175 if (idev
->cnf
.disable_ipv6
)
2176 return ERR_PTR(-EACCES
);
2178 /* Add default multicast route */
2179 if (!(dev
->flags
& IFF_LOOPBACK
))
2180 addrconf_add_mroute(dev
);
2185 static void manage_tempaddrs(struct inet6_dev
*idev
,
2186 struct inet6_ifaddr
*ifp
,
2187 __u32 valid_lft
, __u32 prefered_lft
,
2188 bool create
, unsigned long now
)
2191 struct inet6_ifaddr
*ift
;
2193 read_lock_bh(&idev
->lock
);
2194 /* update all temporary addresses in the list */
2195 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2196 int age
, max_valid
, max_prefered
;
2198 if (ifp
!= ift
->ifpub
)
2201 /* RFC 4941 section 3.3:
2202 * If a received option will extend the lifetime of a public
2203 * address, the lifetimes of temporary addresses should
2204 * be extended, subject to the overall constraint that no
2205 * temporary addresses should ever remain "valid" or "preferred"
2206 * for a time longer than (TEMP_VALID_LIFETIME) or
2207 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2209 age
= (now
- ift
->cstamp
) / HZ
;
2210 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2214 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2215 idev
->cnf
.max_desync_factor
- age
;
2216 if (max_prefered
< 0)
2219 if (valid_lft
> max_valid
)
2220 valid_lft
= max_valid
;
2222 if (prefered_lft
> max_prefered
)
2223 prefered_lft
= max_prefered
;
2225 spin_lock(&ift
->lock
);
2227 ift
->valid_lft
= valid_lft
;
2228 ift
->prefered_lft
= prefered_lft
;
2230 if (prefered_lft
> 0)
2231 ift
->flags
&= ~IFA_F_DEPRECATED
;
2233 spin_unlock(&ift
->lock
);
2234 if (!(flags
&IFA_F_TENTATIVE
))
2235 ipv6_ifa_notify(0, ift
);
2238 if ((create
|| list_empty(&idev
->tempaddr_list
)) &&
2239 idev
->cnf
.use_tempaddr
> 0) {
2240 /* When a new public address is created as described
2241 * in [ADDRCONF], also create a new temporary address.
2242 * Also create a temporary address if it's enabled but
2243 * no temporary address currently exists.
2245 read_unlock_bh(&idev
->lock
);
2246 ipv6_create_tempaddr(ifp
, NULL
);
2248 read_unlock_bh(&idev
->lock
);
2252 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2254 struct prefix_info
*pinfo
;
2259 struct inet6_dev
*in6_dev
;
2260 struct net
*net
= dev_net(dev
);
2262 pinfo
= (struct prefix_info
*) opt
;
2264 if (len
< sizeof(struct prefix_info
)) {
2265 ADBG("addrconf: prefix option too short\n");
2270 * Validation checks ([ADDRCONF], page 19)
2273 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2275 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2278 valid_lft
= ntohl(pinfo
->valid
);
2279 prefered_lft
= ntohl(pinfo
->prefered
);
2281 if (prefered_lft
> valid_lft
) {
2282 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2286 in6_dev
= in6_dev_get(dev
);
2289 net_dbg_ratelimited("addrconf: device %s not configured\n",
2295 * Two things going on here:
2296 * 1) Add routes for on-link prefixes
2297 * 2) Configure prefixes with the auto flag set
2300 if (pinfo
->onlink
) {
2301 struct rt6_info
*rt
;
2302 unsigned long rt_expires
;
2304 /* Avoid arithmetic overflow. Really, we could
2305 * save rt_expires in seconds, likely valid_lft,
2306 * but it would require division in fib gc, that it
2310 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2312 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2314 if (addrconf_finite_timeout(rt_expires
))
2317 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2320 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2321 RTF_GATEWAY
| RTF_DEFAULT
);
2324 /* Autoconf prefix route */
2325 if (valid_lft
== 0) {
2328 } else if (addrconf_finite_timeout(rt_expires
)) {
2330 rt6_set_expires(rt
, jiffies
+ rt_expires
);
2332 rt6_clean_expires(rt
);
2334 } else if (valid_lft
) {
2335 clock_t expires
= 0;
2336 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2337 if (addrconf_finite_timeout(rt_expires
)) {
2339 flags
|= RTF_EXPIRES
;
2340 expires
= jiffies_to_clock_t(rt_expires
);
2342 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2343 dev
, expires
, flags
);
2348 /* Try to figure out our local address for this prefix */
2350 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2351 struct inet6_ifaddr
*ifp
;
2352 struct in6_addr addr
;
2353 int create
= 0, update_lft
= 0;
2354 bool tokenized
= false;
2356 if (pinfo
->prefix_len
== 64) {
2357 memcpy(&addr
, &pinfo
->prefix
, 8);
2359 if (!ipv6_addr_any(&in6_dev
->token
)) {
2360 read_lock_bh(&in6_dev
->lock
);
2361 memcpy(addr
.s6_addr
+ 8,
2362 in6_dev
->token
.s6_addr
+ 8, 8);
2363 read_unlock_bh(&in6_dev
->lock
);
2365 } else if (in6_dev
->addr_gen_mode
==
2366 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
2367 !ipv6_generate_stable_address(&addr
, 0,
2369 addr_flags
|= IFA_F_STABLE_PRIVACY
;
2371 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2372 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2373 in6_dev_put(in6_dev
);
2378 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2380 in6_dev_put(in6_dev
);
2385 ifp
= ipv6_get_ifaddr(net
, &addr
, dev
, 1);
2387 if (!ifp
&& valid_lft
) {
2388 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2390 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2391 if (in6_dev
->cnf
.optimistic_dad
&&
2392 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2393 addr_flags
|= IFA_F_OPTIMISTIC
;
2396 /* Do not allow to create too much of autoconfigured
2397 * addresses; this would be too easy way to crash kernel.
2399 if (!max_addresses
||
2400 ipv6_count_addresses(in6_dev
) < max_addresses
)
2401 ifp
= ipv6_add_addr(in6_dev
, &addr
, NULL
,
2403 addr_type
&IPV6_ADDR_SCOPE_MASK
,
2404 addr_flags
, valid_lft
,
2407 if (IS_ERR_OR_NULL(ifp
)) {
2408 in6_dev_put(in6_dev
);
2414 spin_lock_bh(&ifp
->lock
);
2415 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2416 ifp
->cstamp
= jiffies
;
2417 ifp
->tokenized
= tokenized
;
2418 spin_unlock_bh(&ifp
->lock
);
2419 addrconf_dad_start(ifp
);
2427 /* update lifetime (RFC2462 5.5.3 e) */
2428 spin_lock_bh(&ifp
->lock
);
2430 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2431 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2434 if (!update_lft
&& !create
&& stored_lft
) {
2435 const u32 minimum_lft
= min_t(u32
,
2436 stored_lft
, MIN_VALID_LIFETIME
);
2437 valid_lft
= max(valid_lft
, minimum_lft
);
2439 /* RFC4862 Section 5.5.3e:
2440 * "Note that the preferred lifetime of the
2441 * corresponding address is always reset to
2442 * the Preferred Lifetime in the received
2443 * Prefix Information option, regardless of
2444 * whether the valid lifetime is also reset or
2447 * So we should always update prefered_lft here.
2453 ifp
->valid_lft
= valid_lft
;
2454 ifp
->prefered_lft
= prefered_lft
;
2457 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2458 spin_unlock_bh(&ifp
->lock
);
2460 if (!(flags
&IFA_F_TENTATIVE
))
2461 ipv6_ifa_notify(0, ifp
);
2463 spin_unlock_bh(&ifp
->lock
);
2465 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2472 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2473 in6_dev_put(in6_dev
);
2477 * Set destination address.
2478 * Special case for SIT interfaces where we create a new "virtual"
2481 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2483 struct in6_ifreq ireq
;
2484 struct net_device
*dev
;
2490 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2493 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2499 #if IS_ENABLED(CONFIG_IPV6_SIT)
2500 if (dev
->type
== ARPHRD_SIT
) {
2501 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2503 struct ip_tunnel_parm p
;
2505 err
= -EADDRNOTAVAIL
;
2506 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2509 memset(&p
, 0, sizeof(p
));
2510 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2514 p
.iph
.protocol
= IPPROTO_IPV6
;
2516 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2518 if (ops
->ndo_do_ioctl
) {
2519 mm_segment_t oldfs
= get_fs();
2522 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2529 dev
= __dev_get_by_name(net
, p
.name
);
2532 err
= dev_open(dev
);
2542 static int ipv6_mc_config(struct sock
*sk
, bool join
,
2543 const struct in6_addr
*addr
, int ifindex
)
2551 ret
= ipv6_sock_mc_join(sk
, ifindex
, addr
);
2553 ret
= ipv6_sock_mc_drop(sk
, ifindex
, addr
);
2560 * Manual configuration of address on an interface
2562 static int inet6_addr_add(struct net
*net
, int ifindex
,
2563 const struct in6_addr
*pfx
,
2564 const struct in6_addr
*peer_pfx
,
2565 unsigned int plen
, __u32 ifa_flags
,
2566 __u32 prefered_lft
, __u32 valid_lft
)
2568 struct inet6_ifaddr
*ifp
;
2569 struct inet6_dev
*idev
;
2570 struct net_device
*dev
;
2571 unsigned long timeout
;
2581 /* check the lifetime */
2582 if (!valid_lft
|| prefered_lft
> valid_lft
)
2585 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&& plen
!= 64)
2588 dev
= __dev_get_by_index(net
, ifindex
);
2592 idev
= addrconf_add_dev(dev
);
2594 return PTR_ERR(idev
);
2596 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2597 int ret
= ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2598 true, pfx
, ifindex
);
2604 scope
= ipv6_addr_scope(pfx
);
2606 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
2607 if (addrconf_finite_timeout(timeout
)) {
2608 expires
= jiffies_to_clock_t(timeout
* HZ
);
2609 valid_lft
= timeout
;
2610 flags
= RTF_EXPIRES
;
2614 ifa_flags
|= IFA_F_PERMANENT
;
2617 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
2618 if (addrconf_finite_timeout(timeout
)) {
2620 ifa_flags
|= IFA_F_DEPRECATED
;
2621 prefered_lft
= timeout
;
2624 ifp
= ipv6_add_addr(idev
, pfx
, peer_pfx
, plen
, scope
, ifa_flags
,
2625 valid_lft
, prefered_lft
);
2628 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
2629 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
2634 * Note that section 3.1 of RFC 4429 indicates
2635 * that the Optimistic flag should not be set for
2636 * manually configured addresses
2638 addrconf_dad_start(ifp
);
2639 if (ifa_flags
& IFA_F_MANAGETEMPADDR
)
2640 manage_tempaddrs(idev
, ifp
, valid_lft
, prefered_lft
,
2643 addrconf_verify_rtnl();
2645 } else if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2646 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2647 false, pfx
, ifindex
);
2650 return PTR_ERR(ifp
);
2653 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
2654 const struct in6_addr
*pfx
, unsigned int plen
)
2656 struct inet6_ifaddr
*ifp
;
2657 struct inet6_dev
*idev
;
2658 struct net_device
*dev
;
2663 dev
= __dev_get_by_index(net
, ifindex
);
2667 idev
= __in6_dev_get(dev
);
2671 read_lock_bh(&idev
->lock
);
2672 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
2673 if (ifp
->prefix_len
== plen
&&
2674 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
2676 read_unlock_bh(&idev
->lock
);
2678 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
2679 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
2680 manage_tempaddrs(idev
, ifp
, 0, 0, false,
2683 addrconf_verify_rtnl();
2684 if (ipv6_addr_is_multicast(pfx
)) {
2685 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2686 false, pfx
, dev
->ifindex
);
2691 read_unlock_bh(&idev
->lock
);
2692 return -EADDRNOTAVAIL
;
2696 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
2698 struct in6_ifreq ireq
;
2701 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2704 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2708 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, NULL
,
2709 ireq
.ifr6_prefixlen
, IFA_F_PERMANENT
,
2710 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2715 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
2717 struct in6_ifreq ireq
;
2720 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2723 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2727 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
2728 ireq
.ifr6_prefixlen
);
2733 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
2734 int plen
, int scope
)
2736 struct inet6_ifaddr
*ifp
;
2738 ifp
= ipv6_add_addr(idev
, addr
, NULL
, plen
,
2739 scope
, IFA_F_PERMANENT
,
2740 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2742 spin_lock_bh(&ifp
->lock
);
2743 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2744 spin_unlock_bh(&ifp
->lock
);
2745 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2750 #if IS_ENABLED(CONFIG_IPV6_SIT)
2751 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2753 struct in6_addr addr
;
2754 struct net_device
*dev
;
2755 struct net
*net
= dev_net(idev
->dev
);
2761 memset(&addr
, 0, sizeof(struct in6_addr
));
2762 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2764 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2765 addr
.s6_addr32
[0] = htonl(0xfe800000);
2769 scope
= IPV6_ADDR_COMPATv4
;
2771 pflags
|= RTF_NONEXTHOP
;
2774 if (addr
.s6_addr32
[3]) {
2775 add_addr(idev
, &addr
, plen
, scope
);
2776 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0, pflags
);
2780 for_each_netdev(net
, dev
) {
2781 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
2782 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2783 struct in_ifaddr
*ifa
;
2787 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2789 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2791 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2793 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2794 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2799 add_addr(idev
, &addr
, plen
, flag
);
2800 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0,
2808 static void init_loopback(struct net_device
*dev
)
2810 struct inet6_dev
*idev
;
2811 struct net_device
*sp_dev
;
2812 struct inet6_ifaddr
*sp_ifa
;
2813 struct rt6_info
*sp_rt
;
2819 idev
= ipv6_find_idev(dev
);
2821 pr_debug("%s: add_dev failed\n", __func__
);
2825 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
2827 /* Add routes to other interface's IPv6 addresses */
2828 for_each_netdev(dev_net(dev
), sp_dev
) {
2829 if (!strcmp(sp_dev
->name
, dev
->name
))
2832 idev
= __in6_dev_get(sp_dev
);
2836 read_lock_bh(&idev
->lock
);
2837 list_for_each_entry(sp_ifa
, &idev
->addr_list
, if_list
) {
2839 if (sp_ifa
->flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))
2843 /* This dst has been added to garbage list when
2844 * lo device down, release this obsolete dst and
2845 * reallocate a new router for ifa.
2847 if (sp_ifa
->rt
->dst
.obsolete
> 0) {
2848 ip6_rt_put(sp_ifa
->rt
);
2855 sp_rt
= addrconf_dst_alloc(idev
, &sp_ifa
->addr
, false);
2857 /* Failure cases are ignored */
2858 if (!IS_ERR(sp_rt
)) {
2863 read_unlock_bh(&idev
->lock
);
2867 static void addrconf_add_linklocal(struct inet6_dev
*idev
,
2868 const struct in6_addr
*addr
, u32 flags
)
2870 struct inet6_ifaddr
*ifp
;
2871 u32 addr_flags
= flags
| IFA_F_PERMANENT
;
2873 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2874 if (idev
->cnf
.optimistic_dad
&&
2875 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
2876 addr_flags
|= IFA_F_OPTIMISTIC
;
2879 ifp
= ipv6_add_addr(idev
, addr
, NULL
, 64, IFA_LINK
, addr_flags
,
2880 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2882 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
2883 addrconf_dad_start(ifp
);
2888 static bool ipv6_reserved_interfaceid(struct in6_addr address
)
2890 if ((address
.s6_addr32
[2] | address
.s6_addr32
[3]) == 0)
2893 if (address
.s6_addr32
[2] == htonl(0x02005eff) &&
2894 ((address
.s6_addr32
[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2897 if (address
.s6_addr32
[2] == htonl(0xfdffffff) &&
2898 ((address
.s6_addr32
[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2904 static int ipv6_generate_stable_address(struct in6_addr
*address
,
2906 const struct inet6_dev
*idev
)
2908 static DEFINE_SPINLOCK(lock
);
2909 static __u32 digest
[SHA_DIGEST_WORDS
];
2910 static __u32 workspace
[SHA_WORKSPACE_WORDS
];
2913 char __data
[SHA_MESSAGE_BYTES
];
2915 struct in6_addr secret
;
2917 unsigned char hwaddr
[MAX_ADDR_LEN
];
2922 struct in6_addr secret
;
2923 struct in6_addr temp
;
2924 struct net
*net
= dev_net(idev
->dev
);
2926 BUILD_BUG_ON(sizeof(data
.__data
) != sizeof(data
));
2928 if (idev
->cnf
.stable_secret
.initialized
)
2929 secret
= idev
->cnf
.stable_secret
.secret
;
2930 else if (net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
2931 secret
= net
->ipv6
.devconf_dflt
->stable_secret
.secret
;
2936 spin_lock_bh(&lock
);
2939 memset(&data
, 0, sizeof(data
));
2940 memset(workspace
, 0, sizeof(workspace
));
2941 memcpy(data
.hwaddr
, idev
->dev
->perm_addr
, idev
->dev
->addr_len
);
2942 data
.prefix
[0] = address
->s6_addr32
[0];
2943 data
.prefix
[1] = address
->s6_addr32
[1];
2944 data
.secret
= secret
;
2945 data
.dad_count
= dad_count
;
2947 sha_transform(digest
, data
.__data
, workspace
);
2950 temp
.s6_addr32
[2] = (__force __be32
)digest
[0];
2951 temp
.s6_addr32
[3] = (__force __be32
)digest
[1];
2953 spin_unlock_bh(&lock
);
2955 if (ipv6_reserved_interfaceid(temp
)) {
2957 if (dad_count
> dev_net(idev
->dev
)->ipv6
.sysctl
.idgen_retries
)
2966 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
2968 struct in6_addr addr
;
2970 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
2972 if (idev
->addr_gen_mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
) {
2973 if (!ipv6_generate_stable_address(&addr
, 0, idev
))
2974 addrconf_add_linklocal(idev
, &addr
,
2975 IFA_F_STABLE_PRIVACY
);
2976 else if (prefix_route
)
2977 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
2978 } else if (idev
->addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
) {
2979 /* addrconf_add_linklocal also adds a prefix_route and we
2980 * only need to care about prefix routes if ipv6_generate_eui64
2981 * couldn't generate one.
2983 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
2984 addrconf_add_linklocal(idev
, &addr
, 0);
2985 else if (prefix_route
)
2986 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
2990 static void addrconf_dev_config(struct net_device
*dev
)
2992 struct inet6_dev
*idev
;
2996 if ((dev
->type
!= ARPHRD_ETHER
) &&
2997 (dev
->type
!= ARPHRD_FDDI
) &&
2998 (dev
->type
!= ARPHRD_ARCNET
) &&
2999 (dev
->type
!= ARPHRD_INFINIBAND
) &&
3000 (dev
->type
!= ARPHRD_IEEE802154
) &&
3001 (dev
->type
!= ARPHRD_IEEE1394
) &&
3002 (dev
->type
!= ARPHRD_TUNNEL6
) &&
3003 (dev
->type
!= ARPHRD_6LOWPAN
)) {
3004 /* Alas, we support only Ethernet autoconfiguration. */
3008 idev
= addrconf_add_dev(dev
);
3012 addrconf_addr_gen(idev
, false);
3015 #if IS_ENABLED(CONFIG_IPV6_SIT)
3016 static void addrconf_sit_config(struct net_device
*dev
)
3018 struct inet6_dev
*idev
;
3023 * Configure the tunnel with one of our IPv4
3024 * addresses... we should configure all of
3025 * our v4 addrs in the tunnel
3028 idev
= ipv6_find_idev(dev
);
3030 pr_debug("%s: add_dev failed\n", __func__
);
3034 if (dev
->priv_flags
& IFF_ISATAP
) {
3035 addrconf_addr_gen(idev
, false);
3039 sit_add_v4_addrs(idev
);
3041 if (dev
->flags
&IFF_POINTOPOINT
)
3042 addrconf_add_mroute(dev
);
3046 #if IS_ENABLED(CONFIG_NET_IPGRE)
3047 static void addrconf_gre_config(struct net_device
*dev
)
3049 struct inet6_dev
*idev
;
3053 idev
= ipv6_find_idev(dev
);
3055 pr_debug("%s: add_dev failed\n", __func__
);
3059 addrconf_addr_gen(idev
, true);
3063 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
3066 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3067 struct inet6_dev
*idev
= __in6_dev_get(dev
);
3068 int run_pending
= 0;
3072 case NETDEV_REGISTER
:
3073 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3074 idev
= ipv6_add_dev(dev
);
3076 return notifier_from_errno(PTR_ERR(idev
));
3082 if (dev
->flags
& IFF_SLAVE
)
3085 if (idev
&& idev
->cnf
.disable_ipv6
)
3088 if (event
== NETDEV_UP
) {
3089 if (!addrconf_qdisc_ok(dev
)) {
3090 /* device is not ready yet. */
3091 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3096 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
3097 idev
= ipv6_add_dev(dev
);
3099 if (!IS_ERR_OR_NULL(idev
)) {
3100 idev
->if_flags
|= IF_READY
;
3104 if (!addrconf_qdisc_ok(dev
)) {
3105 /* device is still not ready. */
3110 if (idev
->if_flags
& IF_READY
)
3111 /* device is already configured. */
3113 idev
->if_flags
|= IF_READY
;
3116 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3122 switch (dev
->type
) {
3123 #if IS_ENABLED(CONFIG_IPV6_SIT)
3125 addrconf_sit_config(dev
);
3128 #if IS_ENABLED(CONFIG_NET_IPGRE)
3130 addrconf_gre_config(dev
);
3133 case ARPHRD_LOOPBACK
:
3138 addrconf_dev_config(dev
);
3142 if (!IS_ERR_OR_NULL(idev
)) {
3144 addrconf_dad_run(idev
);
3147 * If the MTU changed during the interface down,
3148 * when the interface up, the changed MTU must be
3149 * reflected in the idev as well as routers.
3151 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
3152 dev
->mtu
>= IPV6_MIN_MTU
) {
3153 rt6_mtu_change(dev
, dev
->mtu
);
3154 idev
->cnf
.mtu6
= dev
->mtu
;
3156 idev
->tstamp
= jiffies
;
3157 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
3160 * If the changed mtu during down is lower than
3161 * IPV6_MIN_MTU stop IPv6 on this interface.
3163 if (dev
->mtu
< IPV6_MIN_MTU
)
3164 addrconf_ifdown(dev
, 1);
3168 case NETDEV_CHANGEMTU
:
3169 if (idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3170 rt6_mtu_change(dev
, dev
->mtu
);
3171 idev
->cnf
.mtu6
= dev
->mtu
;
3175 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3176 idev
= ipv6_add_dev(dev
);
3182 * if MTU under IPV6_MIN_MTU.
3183 * Stop IPv6 on this interface.
3187 case NETDEV_UNREGISTER
:
3189 * Remove all addresses from this interface.
3191 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3194 case NETDEV_CHANGENAME
:
3196 snmp6_unregister_dev(idev
);
3197 addrconf_sysctl_unregister(idev
);
3198 err
= addrconf_sysctl_register(idev
);
3200 return notifier_from_errno(err
);
3201 err
= snmp6_register_dev(idev
);
3203 addrconf_sysctl_unregister(idev
);
3204 return notifier_from_errno(err
);
3209 case NETDEV_PRE_TYPE_CHANGE
:
3210 case NETDEV_POST_TYPE_CHANGE
:
3211 addrconf_type_change(dev
, event
);
3219 * addrconf module should be notified of a device going up
3221 static struct notifier_block ipv6_dev_notf
= {
3222 .notifier_call
= addrconf_notify
,
3225 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3227 struct inet6_dev
*idev
;
3230 idev
= __in6_dev_get(dev
);
3232 if (event
== NETDEV_POST_TYPE_CHANGE
)
3233 ipv6_mc_remap(idev
);
3234 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3235 ipv6_mc_unmap(idev
);
3238 static int addrconf_ifdown(struct net_device
*dev
, int how
)
3240 struct net
*net
= dev_net(dev
);
3241 struct inet6_dev
*idev
;
3242 struct inet6_ifaddr
*ifa
;
3247 rt6_ifdown(net
, dev
);
3248 neigh_ifdown(&nd_tbl
, dev
);
3250 idev
= __in6_dev_get(dev
);
3255 * Step 1: remove reference to ipv6 device from parent device.
3261 /* protected by rtnl_lock */
3262 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3264 /* Step 1.5: remove snmp6 entry */
3265 snmp6_unregister_dev(idev
);
3269 /* Step 2: clear hash table */
3270 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3271 struct hlist_head
*h
= &inet6_addr_lst
[i
];
3273 spin_lock_bh(&addrconf_hash_lock
);
3275 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3276 if (ifa
->idev
== idev
) {
3277 hlist_del_init_rcu(&ifa
->addr_lst
);
3278 addrconf_del_dad_work(ifa
);
3282 spin_unlock_bh(&addrconf_hash_lock
);
3285 write_lock_bh(&idev
->lock
);
3287 addrconf_del_rs_timer(idev
);
3289 /* Step 2: clear flags for stateless addrconf */
3291 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3293 if (how
&& del_timer(&idev
->regen_timer
))
3296 /* Step 3: clear tempaddr list */
3297 while (!list_empty(&idev
->tempaddr_list
)) {
3298 ifa
= list_first_entry(&idev
->tempaddr_list
,
3299 struct inet6_ifaddr
, tmp_list
);
3300 list_del(&ifa
->tmp_list
);
3301 write_unlock_bh(&idev
->lock
);
3302 spin_lock_bh(&ifa
->lock
);
3305 in6_ifa_put(ifa
->ifpub
);
3308 spin_unlock_bh(&ifa
->lock
);
3310 write_lock_bh(&idev
->lock
);
3313 while (!list_empty(&idev
->addr_list
)) {
3314 ifa
= list_first_entry(&idev
->addr_list
,
3315 struct inet6_ifaddr
, if_list
);
3316 addrconf_del_dad_work(ifa
);
3318 list_del(&ifa
->if_list
);
3320 write_unlock_bh(&idev
->lock
);
3322 spin_lock_bh(&ifa
->lock
);
3324 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3325 spin_unlock_bh(&ifa
->lock
);
3327 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3328 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3329 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3333 write_lock_bh(&idev
->lock
);
3336 write_unlock_bh(&idev
->lock
);
3338 /* Step 5: Discard anycast and multicast list */
3340 ipv6_ac_destroy_dev(idev
);
3341 ipv6_mc_destroy_dev(idev
);
3346 idev
->tstamp
= jiffies
;
3348 /* Last: Shot the device (if unregistered) */
3350 addrconf_sysctl_unregister(idev
);
3351 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3352 neigh_ifdown(&nd_tbl
, dev
);
3358 static void addrconf_rs_timer(unsigned long data
)
3360 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
3361 struct net_device
*dev
= idev
->dev
;
3362 struct in6_addr lladdr
;
3364 write_lock(&idev
->lock
);
3365 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3368 if (!ipv6_accept_ra(idev
))
3371 /* Announcement received after solicitation was sent */
3372 if (idev
->if_flags
& IF_RA_RCVD
)
3375 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
) {
3376 write_unlock(&idev
->lock
);
3377 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3378 ndisc_send_rs(dev
, &lladdr
,
3379 &in6addr_linklocal_allrouters
);
3383 write_lock(&idev
->lock
);
3384 /* The wait after the last probe can be shorter */
3385 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3386 idev
->cnf
.rtr_solicits
) ?
3387 idev
->cnf
.rtr_solicit_delay
:
3388 idev
->cnf
.rtr_solicit_interval
);
3391 * Note: we do not support deprecated "all on-link"
3392 * assumption any longer.
3394 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3398 write_unlock(&idev
->lock
);
3404 * Duplicate Address Detection
3406 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3408 unsigned long rand_num
;
3409 struct inet6_dev
*idev
= ifp
->idev
;
3411 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3414 rand_num
= prandom_u32() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3416 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
3417 addrconf_mod_dad_work(ifp
, rand_num
);
3420 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
3422 struct inet6_dev
*idev
= ifp
->idev
;
3423 struct net_device
*dev
= idev
->dev
;
3425 addrconf_join_solict(dev
, &ifp
->addr
);
3427 prandom_seed((__force u32
) ifp
->addr
.s6_addr32
[3]);
3429 read_lock_bh(&idev
->lock
);
3430 spin_lock(&ifp
->lock
);
3431 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3434 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3435 idev
->cnf
.accept_dad
< 1 ||
3436 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3437 ifp
->flags
& IFA_F_NODAD
) {
3438 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3439 spin_unlock(&ifp
->lock
);
3440 read_unlock_bh(&idev
->lock
);
3442 addrconf_dad_completed(ifp
);
3446 if (!(idev
->if_flags
& IF_READY
)) {
3447 spin_unlock(&ifp
->lock
);
3448 read_unlock_bh(&idev
->lock
);
3450 * If the device is not ready:
3451 * - keep it tentative if it is a permanent address.
3452 * - otherwise, kill it.
3455 addrconf_dad_stop(ifp
, 0);
3460 * Optimistic nodes can start receiving
3463 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
3464 ip6_ins_rt(ifp
->rt
);
3465 if (ipv6_use_optimistic_addr(idev
)) {
3466 /* Because optimistic nodes can use this address,
3467 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3469 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3473 addrconf_dad_kick(ifp
);
3475 spin_unlock(&ifp
->lock
);
3476 read_unlock_bh(&idev
->lock
);
3479 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
3481 bool begin_dad
= false;
3483 spin_lock_bh(&ifp
->lock
);
3484 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
3485 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
3488 spin_unlock_bh(&ifp
->lock
);
3491 addrconf_mod_dad_work(ifp
, 0);
3494 static void addrconf_dad_work(struct work_struct
*w
)
3496 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
3497 struct inet6_ifaddr
,
3499 struct inet6_dev
*idev
= ifp
->idev
;
3500 struct in6_addr mcaddr
;
3506 } action
= DAD_PROCESS
;
3510 spin_lock_bh(&ifp
->lock
);
3511 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
3513 ifp
->state
= INET6_IFADDR_STATE_DAD
;
3514 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
3516 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
3518 spin_unlock_bh(&ifp
->lock
);
3520 if (action
== DAD_BEGIN
) {
3521 addrconf_dad_begin(ifp
);
3523 } else if (action
== DAD_ABORT
) {
3524 addrconf_dad_stop(ifp
, 1);
3528 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
3531 write_lock_bh(&idev
->lock
);
3532 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
3533 write_unlock_bh(&idev
->lock
);
3537 spin_lock(&ifp
->lock
);
3538 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
3539 spin_unlock(&ifp
->lock
);
3540 write_unlock_bh(&idev
->lock
);
3544 if (ifp
->dad_probes
== 0) {
3546 * DAD was successful
3549 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3550 spin_unlock(&ifp
->lock
);
3551 write_unlock_bh(&idev
->lock
);
3553 addrconf_dad_completed(ifp
);
3559 addrconf_mod_dad_work(ifp
,
3560 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
));
3561 spin_unlock(&ifp
->lock
);
3562 write_unlock_bh(&idev
->lock
);
3564 /* send a neighbour solicitation for our addr */
3565 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
3566 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &in6addr_any
);
3572 /* ifp->idev must be at least read locked */
3573 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
3575 struct inet6_ifaddr
*ifpiter
;
3576 struct inet6_dev
*idev
= ifp
->idev
;
3578 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
3579 if (ifpiter
->scope
> IFA_LINK
)
3581 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
3582 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
3583 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
3590 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
3592 struct net_device
*dev
= ifp
->idev
->dev
;
3593 struct in6_addr lladdr
;
3594 bool send_rs
, send_mld
;
3596 addrconf_del_dad_work(ifp
);
3599 * Configure the address for reception. Now it is valid.
3602 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3604 /* If added prefix is link local and we are prepared to process
3605 router advertisements, start sending router solicitations.
3608 read_lock_bh(&ifp
->idev
->lock
);
3609 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
3610 send_rs
= send_mld
&&
3611 ipv6_accept_ra(ifp
->idev
) &&
3612 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
3613 (dev
->flags
&IFF_LOOPBACK
) == 0;
3614 read_unlock_bh(&ifp
->idev
->lock
);
3616 /* While dad is in progress mld report's source address is in6_addrany.
3617 * Resend with proper ll now.
3620 ipv6_mc_dad_complete(ifp
->idev
);
3624 * If a host as already performed a random delay
3625 * [...] as part of DAD [...] there is no need
3626 * to delay again before sending the first RS
3628 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3630 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
3632 write_lock_bh(&ifp
->idev
->lock
);
3633 spin_lock(&ifp
->lock
);
3634 ifp
->idev
->rs_probes
= 1;
3635 ifp
->idev
->if_flags
|= IF_RS_SENT
;
3636 addrconf_mod_rs_timer(ifp
->idev
,
3637 ifp
->idev
->cnf
.rtr_solicit_interval
);
3638 spin_unlock(&ifp
->lock
);
3639 write_unlock_bh(&ifp
->idev
->lock
);
3643 static void addrconf_dad_run(struct inet6_dev
*idev
)
3645 struct inet6_ifaddr
*ifp
;
3647 read_lock_bh(&idev
->lock
);
3648 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3649 spin_lock(&ifp
->lock
);
3650 if (ifp
->flags
& IFA_F_TENTATIVE
&&
3651 ifp
->state
== INET6_IFADDR_STATE_DAD
)
3652 addrconf_dad_kick(ifp
);
3653 spin_unlock(&ifp
->lock
);
3655 read_unlock_bh(&idev
->lock
);
3658 #ifdef CONFIG_PROC_FS
3659 struct if6_iter_state
{
3660 struct seq_net_private p
;
3665 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
3667 struct inet6_ifaddr
*ifa
= NULL
;
3668 struct if6_iter_state
*state
= seq
->private;
3669 struct net
*net
= seq_file_net(seq
);
3672 /* initial bucket if pos is 0 */
3678 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
3679 hlist_for_each_entry_rcu_bh(ifa
, &inet6_addr_lst
[state
->bucket
],
3681 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3683 /* sync with offset */
3684 if (p
< state
->offset
) {
3692 /* prepare for next bucket */
3699 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
3700 struct inet6_ifaddr
*ifa
)
3702 struct if6_iter_state
*state
= seq
->private;
3703 struct net
*net
= seq_file_net(seq
);
3705 hlist_for_each_entry_continue_rcu_bh(ifa
, addr_lst
) {
3706 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3712 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
3714 hlist_for_each_entry_rcu_bh(ifa
,
3715 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
3716 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3726 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3730 return if6_get_first(seq
, *pos
);
3733 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3735 struct inet6_ifaddr
*ifa
;
3737 ifa
= if6_get_next(seq
, v
);
3742 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
3745 rcu_read_unlock_bh();
3748 static int if6_seq_show(struct seq_file
*seq
, void *v
)
3750 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
3751 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
3753 ifp
->idev
->dev
->ifindex
,
3757 ifp
->idev
->dev
->name
);
3761 static const struct seq_operations if6_seq_ops
= {
3762 .start
= if6_seq_start
,
3763 .next
= if6_seq_next
,
3764 .show
= if6_seq_show
,
3765 .stop
= if6_seq_stop
,
3768 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
3770 return seq_open_net(inode
, file
, &if6_seq_ops
,
3771 sizeof(struct if6_iter_state
));
3774 static const struct file_operations if6_fops
= {
3775 .owner
= THIS_MODULE
,
3776 .open
= if6_seq_open
,
3778 .llseek
= seq_lseek
,
3779 .release
= seq_release_net
,
3782 static int __net_init
if6_proc_net_init(struct net
*net
)
3784 if (!proc_create("if_inet6", S_IRUGO
, net
->proc_net
, &if6_fops
))
3789 static void __net_exit
if6_proc_net_exit(struct net
*net
)
3791 remove_proc_entry("if_inet6", net
->proc_net
);
3794 static struct pernet_operations if6_proc_net_ops
= {
3795 .init
= if6_proc_net_init
,
3796 .exit
= if6_proc_net_exit
,
3799 int __init
if6_proc_init(void)
3801 return register_pernet_subsys(&if6_proc_net_ops
);
3804 void if6_proc_exit(void)
3806 unregister_pernet_subsys(&if6_proc_net_ops
);
3808 #endif /* CONFIG_PROC_FS */
3810 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3811 /* Check if address is a home address configured on any interface. */
3812 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
3815 struct inet6_ifaddr
*ifp
= NULL
;
3816 unsigned int hash
= inet6_addr_hash(addr
);
3819 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
3820 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
3822 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
3823 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
3828 rcu_read_unlock_bh();
3834 * Periodic address status verification
3837 static void addrconf_verify_rtnl(void)
3839 unsigned long now
, next
, next_sec
, next_sched
;
3840 struct inet6_ifaddr
*ifp
;
3847 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
3849 cancel_delayed_work(&addr_chk_work
);
3851 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3853 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[i
], addr_lst
) {
3856 /* When setting preferred_lft to a value not zero or
3857 * infinity, while valid_lft is infinity
3858 * IFA_F_PERMANENT has a non-infinity life time.
3860 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3861 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
3864 spin_lock(&ifp
->lock
);
3865 /* We try to batch several events at once. */
3866 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
3868 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
3869 age
>= ifp
->valid_lft
) {
3870 spin_unlock(&ifp
->lock
);
3874 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
3875 spin_unlock(&ifp
->lock
);
3877 } else if (age
>= ifp
->prefered_lft
) {
3878 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3881 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
3883 ifp
->flags
|= IFA_F_DEPRECATED
;
3886 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
3887 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
3888 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
3890 spin_unlock(&ifp
->lock
);
3895 ipv6_ifa_notify(0, ifp
);
3899 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
3900 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
3901 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
3902 ifp
->idev
->cnf
.dad_transmits
*
3903 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
) / HZ
;
3905 if (age
>= ifp
->prefered_lft
- regen_advance
) {
3906 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
3907 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3908 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3909 if (!ifp
->regen_count
&& ifpub
) {
3912 in6_ifa_hold(ifpub
);
3913 spin_unlock(&ifp
->lock
);
3915 spin_lock(&ifpub
->lock
);
3916 ifpub
->regen_count
= 0;
3917 spin_unlock(&ifpub
->lock
);
3918 ipv6_create_tempaddr(ifpub
, ifp
);
3923 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
3924 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
3925 spin_unlock(&ifp
->lock
);
3927 /* ifp->prefered_lft <= ifp->valid_lft */
3928 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3929 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3930 spin_unlock(&ifp
->lock
);
3935 next_sec
= round_jiffies_up(next
);
3938 /* If rounded timeout is accurate enough, accept it. */
3939 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
3940 next_sched
= next_sec
;
3942 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3943 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
3944 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
3946 ADBG(KERN_DEBUG
"now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3947 now
, next
, next_sec
, next_sched
);
3948 mod_delayed_work(addrconf_wq
, &addr_chk_work
, next_sched
- now
);
3949 rcu_read_unlock_bh();
3952 static void addrconf_verify_work(struct work_struct
*w
)
3955 addrconf_verify_rtnl();
3959 static void addrconf_verify(void)
3961 mod_delayed_work(addrconf_wq
, &addr_chk_work
, 0);
3964 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
3965 struct in6_addr
**peer_pfx
)
3967 struct in6_addr
*pfx
= NULL
;
3972 pfx
= nla_data(addr
);
3975 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
3977 pfx
= nla_data(local
);
3983 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
3984 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
3985 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
3986 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
3987 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
3991 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3993 struct net
*net
= sock_net(skb
->sk
);
3994 struct ifaddrmsg
*ifm
;
3995 struct nlattr
*tb
[IFA_MAX
+1];
3996 struct in6_addr
*pfx
, *peer_pfx
;
4000 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4004 ifm
= nlmsg_data(nlh
);
4005 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4009 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4011 /* We ignore other flags so far. */
4012 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
4014 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
4015 ifm
->ifa_prefixlen
);
4018 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u32 ifa_flags
,
4019 u32 prefered_lft
, u32 valid_lft
)
4023 unsigned long timeout
;
4024 bool was_managetempaddr
;
4025 bool had_prefixroute
;
4029 if (!valid_lft
|| (prefered_lft
> valid_lft
))
4032 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&&
4033 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
4036 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
4037 if (addrconf_finite_timeout(timeout
)) {
4038 expires
= jiffies_to_clock_t(timeout
* HZ
);
4039 valid_lft
= timeout
;
4040 flags
= RTF_EXPIRES
;
4044 ifa_flags
|= IFA_F_PERMANENT
;
4047 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
4048 if (addrconf_finite_timeout(timeout
)) {
4050 ifa_flags
|= IFA_F_DEPRECATED
;
4051 prefered_lft
= timeout
;
4054 spin_lock_bh(&ifp
->lock
);
4055 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
4056 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
4057 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
4058 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
4059 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4060 IFA_F_NOPREFIXROUTE
);
4061 ifp
->flags
|= ifa_flags
;
4062 ifp
->tstamp
= jiffies
;
4063 ifp
->valid_lft
= valid_lft
;
4064 ifp
->prefered_lft
= prefered_lft
;
4066 spin_unlock_bh(&ifp
->lock
);
4067 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
4068 ipv6_ifa_notify(0, ifp
);
4070 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
4071 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
4073 } else if (had_prefixroute
) {
4074 enum cleanup_prefix_rt_t action
;
4075 unsigned long rt_expires
;
4077 write_lock_bh(&ifp
->idev
->lock
);
4078 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
4079 write_unlock_bh(&ifp
->idev
->lock
);
4081 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
4082 cleanup_prefix_route(ifp
, rt_expires
,
4083 action
== CLEANUP_PREFIX_RT_DEL
);
4087 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
4088 if (was_managetempaddr
&& !(ifp
->flags
& IFA_F_MANAGETEMPADDR
))
4089 valid_lft
= prefered_lft
= 0;
4090 manage_tempaddrs(ifp
->idev
, ifp
, valid_lft
, prefered_lft
,
4091 !was_managetempaddr
, jiffies
);
4094 addrconf_verify_rtnl();
4100 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
4102 struct net
*net
= sock_net(skb
->sk
);
4103 struct ifaddrmsg
*ifm
;
4104 struct nlattr
*tb
[IFA_MAX
+1];
4105 struct in6_addr
*pfx
, *peer_pfx
;
4106 struct inet6_ifaddr
*ifa
;
4107 struct net_device
*dev
;
4108 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
4112 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4116 ifm
= nlmsg_data(nlh
);
4117 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4121 if (tb
[IFA_CACHEINFO
]) {
4122 struct ifa_cacheinfo
*ci
;
4124 ci
= nla_data(tb
[IFA_CACHEINFO
]);
4125 valid_lft
= ci
->ifa_valid
;
4126 preferred_lft
= ci
->ifa_prefered
;
4128 preferred_lft
= INFINITY_LIFE_TIME
;
4129 valid_lft
= INFINITY_LIFE_TIME
;
4132 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4136 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4138 /* We ignore other flags so far. */
4139 ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4140 IFA_F_NOPREFIXROUTE
| IFA_F_MCAUTOJOIN
;
4142 ifa
= ipv6_get_ifaddr(net
, pfx
, dev
, 1);
4145 * It would be best to check for !NLM_F_CREATE here but
4146 * userspace already relies on not having to provide this.
4148 return inet6_addr_add(net
, ifm
->ifa_index
, pfx
, peer_pfx
,
4149 ifm
->ifa_prefixlen
, ifa_flags
,
4150 preferred_lft
, valid_lft
);
4153 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
4154 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
4157 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
4164 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
4165 u8 scope
, int ifindex
)
4167 struct ifaddrmsg
*ifm
;
4169 ifm
= nlmsg_data(nlh
);
4170 ifm
->ifa_family
= AF_INET6
;
4171 ifm
->ifa_prefixlen
= prefixlen
;
4172 ifm
->ifa_flags
= flags
;
4173 ifm
->ifa_scope
= scope
;
4174 ifm
->ifa_index
= ifindex
;
4177 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
4178 unsigned long tstamp
, u32 preferred
, u32 valid
)
4180 struct ifa_cacheinfo ci
;
4182 ci
.cstamp
= cstamp_delta(cstamp
);
4183 ci
.tstamp
= cstamp_delta(tstamp
);
4184 ci
.ifa_prefered
= preferred
;
4185 ci
.ifa_valid
= valid
;
4187 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
4190 static inline int rt_scope(int ifa_scope
)
4192 if (ifa_scope
& IFA_HOST
)
4193 return RT_SCOPE_HOST
;
4194 else if (ifa_scope
& IFA_LINK
)
4195 return RT_SCOPE_LINK
;
4196 else if (ifa_scope
& IFA_SITE
)
4197 return RT_SCOPE_SITE
;
4199 return RT_SCOPE_UNIVERSE
;
4202 static inline int inet6_ifaddr_msgsize(void)
4204 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
4205 + nla_total_size(16) /* IFA_LOCAL */
4206 + nla_total_size(16) /* IFA_ADDRESS */
4207 + nla_total_size(sizeof(struct ifa_cacheinfo
))
4208 + nla_total_size(4) /* IFA_FLAGS */;
4211 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
4212 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4214 struct nlmsghdr
*nlh
;
4215 u32 preferred
, valid
;
4217 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4221 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
4222 ifa
->idev
->dev
->ifindex
);
4224 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
4225 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
4226 preferred
= ifa
->prefered_lft
;
4227 valid
= ifa
->valid_lft
;
4228 if (preferred
!= INFINITY_LIFE_TIME
) {
4229 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
4230 if (preferred
> tval
)
4234 if (valid
!= INFINITY_LIFE_TIME
) {
4242 preferred
= INFINITY_LIFE_TIME
;
4243 valid
= INFINITY_LIFE_TIME
;
4246 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
4247 if (nla_put_in6_addr(skb
, IFA_LOCAL
, &ifa
->addr
) < 0 ||
4248 nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->peer_addr
) < 0)
4251 if (nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->addr
) < 0)
4254 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
4257 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
4260 nlmsg_end(skb
, nlh
);
4264 nlmsg_cancel(skb
, nlh
);
4268 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
4269 u32 portid
, u32 seq
, int event
, u16 flags
)
4271 struct nlmsghdr
*nlh
;
4272 u8 scope
= RT_SCOPE_UNIVERSE
;
4273 int ifindex
= ifmca
->idev
->dev
->ifindex
;
4275 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
4276 scope
= RT_SCOPE_SITE
;
4278 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4282 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4283 if (nla_put_in6_addr(skb
, IFA_MULTICAST
, &ifmca
->mca_addr
) < 0 ||
4284 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
4285 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4286 nlmsg_cancel(skb
, nlh
);
4290 nlmsg_end(skb
, nlh
);
4294 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
4295 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4297 struct nlmsghdr
*nlh
;
4298 u8 scope
= RT_SCOPE_UNIVERSE
;
4299 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
4301 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
4302 scope
= RT_SCOPE_SITE
;
4304 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4308 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4309 if (nla_put_in6_addr(skb
, IFA_ANYCAST
, &ifaca
->aca_addr
) < 0 ||
4310 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
4311 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4312 nlmsg_cancel(skb
, nlh
);
4316 nlmsg_end(skb
, nlh
);
4326 /* called with rcu_read_lock() */
4327 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
4328 struct netlink_callback
*cb
, enum addr_type_t type
,
4329 int s_ip_idx
, int *p_ip_idx
)
4331 struct ifmcaddr6
*ifmca
;
4332 struct ifacaddr6
*ifaca
;
4334 int ip_idx
= *p_ip_idx
;
4336 read_lock_bh(&idev
->lock
);
4338 case UNICAST_ADDR
: {
4339 struct inet6_ifaddr
*ifa
;
4341 /* unicast address incl. temp addr */
4342 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
4343 if (++ip_idx
< s_ip_idx
)
4345 err
= inet6_fill_ifaddr(skb
, ifa
,
4346 NETLINK_CB(cb
->skb
).portid
,
4352 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4356 case MULTICAST_ADDR
:
4357 /* multicast address */
4358 for (ifmca
= idev
->mc_list
; ifmca
;
4359 ifmca
= ifmca
->next
, ip_idx
++) {
4360 if (ip_idx
< s_ip_idx
)
4362 err
= inet6_fill_ifmcaddr(skb
, ifmca
,
4363 NETLINK_CB(cb
->skb
).portid
,
4372 /* anycast address */
4373 for (ifaca
= idev
->ac_list
; ifaca
;
4374 ifaca
= ifaca
->aca_next
, ip_idx
++) {
4375 if (ip_idx
< s_ip_idx
)
4377 err
= inet6_fill_ifacaddr(skb
, ifaca
,
4378 NETLINK_CB(cb
->skb
).portid
,
4389 read_unlock_bh(&idev
->lock
);
4394 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
4395 enum addr_type_t type
)
4397 struct net
*net
= sock_net(skb
->sk
);
4400 int s_idx
, s_ip_idx
;
4401 struct net_device
*dev
;
4402 struct inet6_dev
*idev
;
4403 struct hlist_head
*head
;
4406 s_idx
= idx
= cb
->args
[1];
4407 s_ip_idx
= ip_idx
= cb
->args
[2];
4410 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^ net
->dev_base_seq
;
4411 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4413 head
= &net
->dev_index_head
[h
];
4414 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4417 if (h
> s_h
|| idx
> s_idx
)
4420 idev
= __in6_dev_get(dev
);
4424 if (in6_dump_addrs(idev
, skb
, cb
, type
,
4425 s_ip_idx
, &ip_idx
) < 0)
4435 cb
->args
[2] = ip_idx
;
4440 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4442 enum addr_type_t type
= UNICAST_ADDR
;
4444 return inet6_dump_addr(skb
, cb
, type
);
4447 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4449 enum addr_type_t type
= MULTICAST_ADDR
;
4451 return inet6_dump_addr(skb
, cb
, type
);
4455 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4457 enum addr_type_t type
= ANYCAST_ADDR
;
4459 return inet6_dump_addr(skb
, cb
, type
);
4462 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
)
4464 struct net
*net
= sock_net(in_skb
->sk
);
4465 struct ifaddrmsg
*ifm
;
4466 struct nlattr
*tb
[IFA_MAX
+1];
4467 struct in6_addr
*addr
= NULL
, *peer
;
4468 struct net_device
*dev
= NULL
;
4469 struct inet6_ifaddr
*ifa
;
4470 struct sk_buff
*skb
;
4473 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4477 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
4483 ifm
= nlmsg_data(nlh
);
4485 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4487 ifa
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
4489 err
= -EADDRNOTAVAIL
;
4493 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
4499 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).portid
,
4500 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
4502 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4503 WARN_ON(err
== -EMSGSIZE
);
4507 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4514 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
4516 struct sk_buff
*skb
;
4517 struct net
*net
= dev_net(ifa
->idev
->dev
);
4520 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
4524 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
4526 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4527 WARN_ON(err
== -EMSGSIZE
);
4531 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
4535 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
4538 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
4539 __s32
*array
, int bytes
)
4541 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
4543 memset(array
, 0, bytes
);
4544 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
4545 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
4546 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
4547 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
4548 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
4549 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
4550 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
4551 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
4552 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
4553 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
4554 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
4555 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
4556 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
4557 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
4558 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
4559 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
4560 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
4561 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
4562 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
4563 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
4564 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
4565 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
4566 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
4567 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
4568 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
4569 #ifdef CONFIG_IPV6_ROUTER_PREF
4570 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
4571 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
4572 jiffies_to_msecs(cnf
->rtr_probe_interval
);
4573 #ifdef CONFIG_IPV6_ROUTE_INFO
4574 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
4577 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
4578 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
4579 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4580 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
4581 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
4583 #ifdef CONFIG_IPV6_MROUTE
4584 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
4586 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
4587 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
4588 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
4589 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
4590 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
4591 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
4592 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
4593 /* we omit DEVCONF_STABLE_SECRET for now */
4596 static inline size_t inet6_ifla6_size(void)
4598 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4599 + nla_total_size(sizeof(struct ifla_cacheinfo
))
4600 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
4601 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
4602 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
4603 + nla_total_size(sizeof(struct in6_addr
)); /* IFLA_INET6_TOKEN */
4606 static inline size_t inet6_if_nlmsg_size(void)
4608 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4609 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4610 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4611 + nla_total_size(4) /* IFLA_MTU */
4612 + nla_total_size(4) /* IFLA_LINK */
4613 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4616 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
4617 int items
, int bytes
)
4620 int pad
= bytes
- sizeof(u64
) * items
;
4623 /* Use put_unaligned() because stats may not be aligned for u64. */
4624 put_unaligned(items
, &stats
[0]);
4625 for (i
= 1; i
< items
; i
++)
4626 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
4628 memset(&stats
[items
], 0, pad
);
4631 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
4632 int items
, int bytes
, size_t syncpoff
)
4635 int pad
= bytes
- sizeof(u64
) * items
;
4638 /* Use put_unaligned() because stats may not be aligned for u64. */
4639 put_unaligned(items
, &stats
[0]);
4640 for (i
= 1; i
< items
; i
++)
4641 put_unaligned(snmp_fold_field64(mib
, i
, syncpoff
), &stats
[i
]);
4643 memset(&stats
[items
], 0, pad
);
4646 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
4650 case IFLA_INET6_STATS
:
4651 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
,
4652 IPSTATS_MIB_MAX
, bytes
, offsetof(struct ipstats_mib
, syncp
));
4654 case IFLA_INET6_ICMP6STATS
:
4655 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, ICMP6_MIB_MAX
, bytes
);
4660 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
)
4663 struct ifla_cacheinfo ci
;
4665 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
4666 goto nla_put_failure
;
4667 ci
.max_reasm_len
= IPV6_MAXPLEN
;
4668 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
4669 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
4670 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
4671 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
4672 goto nla_put_failure
;
4673 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
4675 goto nla_put_failure
;
4676 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
4678 /* XXX - MC not implemented */
4680 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
4682 goto nla_put_failure
;
4683 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
4685 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
4687 goto nla_put_failure
;
4688 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
4690 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
4692 goto nla_put_failure
;
4694 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->addr_gen_mode
))
4695 goto nla_put_failure
;
4697 read_lock_bh(&idev
->lock
);
4698 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
4699 read_unlock_bh(&idev
->lock
);
4707 static size_t inet6_get_link_af_size(const struct net_device
*dev
)
4709 if (!__in6_dev_get(dev
))
4712 return inet6_ifla6_size();
4715 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
)
4717 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4722 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4728 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
)
4730 struct inet6_ifaddr
*ifp
;
4731 struct net_device
*dev
= idev
->dev
;
4732 bool update_rs
= false;
4733 struct in6_addr ll_addr
;
4739 if (ipv6_addr_any(token
))
4741 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
))
4743 if (!ipv6_accept_ra(idev
))
4745 if (idev
->cnf
.rtr_solicits
<= 0)
4748 write_lock_bh(&idev
->lock
);
4750 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
4751 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
4753 write_unlock_bh(&idev
->lock
);
4755 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
4756 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
4757 IFA_F_OPTIMISTIC
)) {
4759 /* If we're not ready, then normal ifup will take care
4760 * of this. Otherwise, we need to request our rs here.
4762 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
4766 write_lock_bh(&idev
->lock
);
4769 idev
->if_flags
|= IF_RS_SENT
;
4770 idev
->rs_probes
= 1;
4771 addrconf_mod_rs_timer(idev
, idev
->cnf
.rtr_solicit_interval
);
4774 /* Well, that's kinda nasty ... */
4775 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4776 spin_lock(&ifp
->lock
);
4777 if (ifp
->tokenized
) {
4779 ifp
->prefered_lft
= 0;
4781 spin_unlock(&ifp
->lock
);
4784 write_unlock_bh(&idev
->lock
);
4785 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
4786 addrconf_verify_rtnl();
4790 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
4791 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
4792 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
4795 static int inet6_validate_link_af(const struct net_device
*dev
,
4796 const struct nlattr
*nla
)
4798 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4800 if (dev
&& !__in6_dev_get(dev
))
4801 return -EAFNOSUPPORT
;
4803 return nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, inet6_af_policy
);
4806 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
4809 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4810 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
4813 return -EAFNOSUPPORT
;
4815 if (nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, NULL
) < 0)
4818 if (tb
[IFLA_INET6_TOKEN
]) {
4819 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]));
4824 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
4825 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
4827 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
4828 mode
!= IN6_ADDR_GEN_MODE_NONE
&&
4829 mode
!= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
)
4832 if (mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
4833 !idev
->cnf
.stable_secret
.initialized
&&
4834 !dev_net(dev
)->ipv6
.devconf_dflt
->stable_secret
.initialized
)
4837 idev
->addr_gen_mode
= mode
;
4844 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4845 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4847 struct net_device
*dev
= idev
->dev
;
4848 struct ifinfomsg
*hdr
;
4849 struct nlmsghdr
*nlh
;
4852 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
4856 hdr
= nlmsg_data(nlh
);
4857 hdr
->ifi_family
= AF_INET6
;
4859 hdr
->ifi_type
= dev
->type
;
4860 hdr
->ifi_index
= dev
->ifindex
;
4861 hdr
->ifi_flags
= dev_get_flags(dev
);
4862 hdr
->ifi_change
= 0;
4864 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4866 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4867 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4868 (dev
->ifindex
!= dev_get_iflink(dev
) &&
4869 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
4870 goto nla_put_failure
;
4871 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4873 goto nla_put_failure
;
4875 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4876 goto nla_put_failure
;
4878 nla_nest_end(skb
, protoinfo
);
4879 nlmsg_end(skb
, nlh
);
4883 nlmsg_cancel(skb
, nlh
);
4887 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4889 struct net
*net
= sock_net(skb
->sk
);
4892 struct net_device
*dev
;
4893 struct inet6_dev
*idev
;
4894 struct hlist_head
*head
;
4897 s_idx
= cb
->args
[1];
4900 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4902 head
= &net
->dev_index_head
[h
];
4903 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4906 idev
= __in6_dev_get(dev
);
4909 if (inet6_fill_ifinfo(skb
, idev
,
4910 NETLINK_CB(cb
->skb
).portid
,
4912 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
4926 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
4928 struct sk_buff
*skb
;
4929 struct net
*net
= dev_net(idev
->dev
);
4932 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
4936 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
4938 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4939 WARN_ON(err
== -EMSGSIZE
);
4943 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
4947 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
4950 static inline size_t inet6_prefix_nlmsg_size(void)
4952 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
4953 + nla_total_size(sizeof(struct in6_addr
))
4954 + nla_total_size(sizeof(struct prefix_cacheinfo
));
4957 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4958 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
4959 int event
, unsigned int flags
)
4961 struct prefixmsg
*pmsg
;
4962 struct nlmsghdr
*nlh
;
4963 struct prefix_cacheinfo ci
;
4965 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
4969 pmsg
= nlmsg_data(nlh
);
4970 pmsg
->prefix_family
= AF_INET6
;
4971 pmsg
->prefix_pad1
= 0;
4972 pmsg
->prefix_pad2
= 0;
4973 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
4974 pmsg
->prefix_len
= pinfo
->prefix_len
;
4975 pmsg
->prefix_type
= pinfo
->type
;
4976 pmsg
->prefix_pad3
= 0;
4977 pmsg
->prefix_flags
= 0;
4979 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
4980 if (pinfo
->autoconf
)
4981 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
4983 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
4984 goto nla_put_failure
;
4985 ci
.preferred_time
= ntohl(pinfo
->prefered
);
4986 ci
.valid_time
= ntohl(pinfo
->valid
);
4987 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
4988 goto nla_put_failure
;
4989 nlmsg_end(skb
, nlh
);
4993 nlmsg_cancel(skb
, nlh
);
4997 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
4998 struct prefix_info
*pinfo
)
5000 struct sk_buff
*skb
;
5001 struct net
*net
= dev_net(idev
->dev
);
5004 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
5008 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
5010 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5011 WARN_ON(err
== -EMSGSIZE
);
5015 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
5019 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
5022 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5024 struct net
*net
= dev_net(ifp
->idev
->dev
);
5029 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
5034 * If the address was optimistic
5035 * we inserted the route at the start of
5036 * our DAD process, so we don't need
5039 if (!(ifp
->rt
->rt6i_node
))
5040 ip6_ins_rt(ifp
->rt
);
5041 if (ifp
->idev
->cnf
.forwarding
)
5042 addrconf_join_anycast(ifp
);
5043 if (!ipv6_addr_any(&ifp
->peer_addr
))
5044 addrconf_prefix_route(&ifp
->peer_addr
, 128,
5045 ifp
->idev
->dev
, 0, 0);
5048 if (ifp
->idev
->cnf
.forwarding
)
5049 addrconf_leave_anycast(ifp
);
5050 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
5051 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
5052 struct rt6_info
*rt
;
5054 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
5055 ifp
->idev
->dev
, 0, 0);
5056 if (rt
&& ip6_del_rt(rt
))
5059 dst_hold(&ifp
->rt
->dst
);
5061 if (ip6_del_rt(ifp
->rt
))
5062 dst_free(&ifp
->rt
->dst
);
5064 rt_genid_bump_ipv6(net
);
5067 atomic_inc(&net
->ipv6
.dev_addr_genid
);
5070 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5073 if (likely(ifp
->idev
->dead
== 0))
5074 __ipv6_ifa_notify(event
, ifp
);
5075 rcu_read_unlock_bh();
5078 #ifdef CONFIG_SYSCTL
5081 int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
5082 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5084 int *valp
= ctl
->data
;
5087 struct ctl_table lctl
;
5091 * ctl->data points to idev->cnf.forwarding, we should
5092 * not modify it until we get the rtnl lock.
5097 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5100 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
5107 int addrconf_sysctl_mtu(struct ctl_table
*ctl
, int write
,
5108 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5110 struct inet6_dev
*idev
= ctl
->extra1
;
5111 int min_mtu
= IPV6_MIN_MTU
;
5112 struct ctl_table lctl
;
5115 lctl
.extra1
= &min_mtu
;
5116 lctl
.extra2
= idev
? &idev
->dev
->mtu
: NULL
;
5118 return proc_dointvec_minmax(&lctl
, write
, buffer
, lenp
, ppos
);
5121 static void dev_disable_change(struct inet6_dev
*idev
)
5123 struct netdev_notifier_info info
;
5125 if (!idev
|| !idev
->dev
)
5128 netdev_notifier_info_init(&info
, idev
->dev
);
5129 if (idev
->cnf
.disable_ipv6
)
5130 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
5132 addrconf_notify(NULL
, NETDEV_UP
, &info
);
5135 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
5137 struct net_device
*dev
;
5138 struct inet6_dev
*idev
;
5141 for_each_netdev_rcu(net
, dev
) {
5142 idev
= __in6_dev_get(dev
);
5144 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
5145 idev
->cnf
.disable_ipv6
= newf
;
5147 dev_disable_change(idev
);
5153 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
5158 if (!rtnl_trylock())
5159 return restart_syscall();
5161 net
= (struct net
*)table
->extra2
;
5165 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
5170 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
5171 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
5172 addrconf_disable_change(net
, newf
);
5173 } else if ((!newf
) ^ (!old
))
5174 dev_disable_change((struct inet6_dev
*)table
->extra1
);
5181 int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
5182 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5184 int *valp
= ctl
->data
;
5187 struct ctl_table lctl
;
5191 * ctl->data points to idev->cnf.disable_ipv6, we should
5192 * not modify it until we get the rtnl lock.
5197 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5200 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
5207 int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
5208 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5210 int *valp
= ctl
->data
;
5215 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
5218 if (write
&& old
!= new) {
5219 struct net
*net
= ctl
->extra2
;
5221 if (!rtnl_trylock())
5222 return restart_syscall();
5224 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
5225 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5226 NETCONFA_IFINDEX_DEFAULT
,
5227 net
->ipv6
.devconf_dflt
);
5228 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
5229 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5230 NETCONFA_IFINDEX_ALL
,
5231 net
->ipv6
.devconf_all
);
5233 struct inet6_dev
*idev
= ctl
->extra1
;
5235 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5245 static int addrconf_sysctl_stable_secret(struct ctl_table
*ctl
, int write
,
5246 void __user
*buffer
, size_t *lenp
,
5250 struct in6_addr addr
;
5251 char str
[IPV6_MAX_STRLEN
];
5252 struct ctl_table lctl
= *ctl
;
5253 struct net
*net
= ctl
->extra2
;
5254 struct ipv6_stable_secret
*secret
= ctl
->data
;
5256 if (&net
->ipv6
.devconf_all
->stable_secret
== ctl
->data
)
5259 lctl
.maxlen
= IPV6_MAX_STRLEN
;
5262 if (!rtnl_trylock())
5263 return restart_syscall();
5265 if (!write
&& !secret
->initialized
) {
5271 err
= snprintf(str
, sizeof(str
), "%pI6",
5273 if (err
>= sizeof(str
)) {
5279 err
= proc_dostring(&lctl
, write
, buffer
, lenp
, ppos
);
5283 if (in6_pton(str
, -1, addr
.in6_u
.u6_addr8
, -1, NULL
) != 1) {
5288 secret
->initialized
= true;
5289 secret
->secret
= addr
;
5291 if (&net
->ipv6
.devconf_dflt
->stable_secret
== ctl
->data
) {
5292 struct net_device
*dev
;
5294 for_each_netdev(net
, dev
) {
5295 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5298 idev
->addr_gen_mode
=
5299 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
5303 struct inet6_dev
*idev
= ctl
->extra1
;
5305 idev
->addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
5314 static struct addrconf_sysctl_table
5316 struct ctl_table_header
*sysctl_header
;
5317 struct ctl_table addrconf_vars
[DEVCONF_MAX
+1];
5318 } addrconf_sysctl __read_mostly
= {
5319 .sysctl_header
= NULL
,
5322 .procname
= "forwarding",
5323 .data
= &ipv6_devconf
.forwarding
,
5324 .maxlen
= sizeof(int),
5326 .proc_handler
= addrconf_sysctl_forward
,
5329 .procname
= "hop_limit",
5330 .data
= &ipv6_devconf
.hop_limit
,
5331 .maxlen
= sizeof(int),
5333 .proc_handler
= proc_dointvec
,
5337 .data
= &ipv6_devconf
.mtu6
,
5338 .maxlen
= sizeof(int),
5340 .proc_handler
= addrconf_sysctl_mtu
,
5343 .procname
= "accept_ra",
5344 .data
= &ipv6_devconf
.accept_ra
,
5345 .maxlen
= sizeof(int),
5347 .proc_handler
= proc_dointvec
,
5350 .procname
= "accept_redirects",
5351 .data
= &ipv6_devconf
.accept_redirects
,
5352 .maxlen
= sizeof(int),
5354 .proc_handler
= proc_dointvec
,
5357 .procname
= "autoconf",
5358 .data
= &ipv6_devconf
.autoconf
,
5359 .maxlen
= sizeof(int),
5361 .proc_handler
= proc_dointvec
,
5364 .procname
= "dad_transmits",
5365 .data
= &ipv6_devconf
.dad_transmits
,
5366 .maxlen
= sizeof(int),
5368 .proc_handler
= proc_dointvec
,
5371 .procname
= "router_solicitations",
5372 .data
= &ipv6_devconf
.rtr_solicits
,
5373 .maxlen
= sizeof(int),
5375 .proc_handler
= proc_dointvec
,
5378 .procname
= "router_solicitation_interval",
5379 .data
= &ipv6_devconf
.rtr_solicit_interval
,
5380 .maxlen
= sizeof(int),
5382 .proc_handler
= proc_dointvec_jiffies
,
5385 .procname
= "router_solicitation_delay",
5386 .data
= &ipv6_devconf
.rtr_solicit_delay
,
5387 .maxlen
= sizeof(int),
5389 .proc_handler
= proc_dointvec_jiffies
,
5392 .procname
= "force_mld_version",
5393 .data
= &ipv6_devconf
.force_mld_version
,
5394 .maxlen
= sizeof(int),
5396 .proc_handler
= proc_dointvec
,
5399 .procname
= "mldv1_unsolicited_report_interval",
5401 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
5402 .maxlen
= sizeof(int),
5404 .proc_handler
= proc_dointvec_ms_jiffies
,
5407 .procname
= "mldv2_unsolicited_report_interval",
5409 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
5410 .maxlen
= sizeof(int),
5412 .proc_handler
= proc_dointvec_ms_jiffies
,
5415 .procname
= "use_tempaddr",
5416 .data
= &ipv6_devconf
.use_tempaddr
,
5417 .maxlen
= sizeof(int),
5419 .proc_handler
= proc_dointvec
,
5422 .procname
= "temp_valid_lft",
5423 .data
= &ipv6_devconf
.temp_valid_lft
,
5424 .maxlen
= sizeof(int),
5426 .proc_handler
= proc_dointvec
,
5429 .procname
= "temp_prefered_lft",
5430 .data
= &ipv6_devconf
.temp_prefered_lft
,
5431 .maxlen
= sizeof(int),
5433 .proc_handler
= proc_dointvec
,
5436 .procname
= "regen_max_retry",
5437 .data
= &ipv6_devconf
.regen_max_retry
,
5438 .maxlen
= sizeof(int),
5440 .proc_handler
= proc_dointvec
,
5443 .procname
= "max_desync_factor",
5444 .data
= &ipv6_devconf
.max_desync_factor
,
5445 .maxlen
= sizeof(int),
5447 .proc_handler
= proc_dointvec
,
5450 .procname
= "max_addresses",
5451 .data
= &ipv6_devconf
.max_addresses
,
5452 .maxlen
= sizeof(int),
5454 .proc_handler
= proc_dointvec
,
5457 .procname
= "accept_ra_defrtr",
5458 .data
= &ipv6_devconf
.accept_ra_defrtr
,
5459 .maxlen
= sizeof(int),
5461 .proc_handler
= proc_dointvec
,
5464 .procname
= "accept_ra_pinfo",
5465 .data
= &ipv6_devconf
.accept_ra_pinfo
,
5466 .maxlen
= sizeof(int),
5468 .proc_handler
= proc_dointvec
,
5470 #ifdef CONFIG_IPV6_ROUTER_PREF
5472 .procname
= "accept_ra_rtr_pref",
5473 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
5474 .maxlen
= sizeof(int),
5476 .proc_handler
= proc_dointvec
,
5479 .procname
= "router_probe_interval",
5480 .data
= &ipv6_devconf
.rtr_probe_interval
,
5481 .maxlen
= sizeof(int),
5483 .proc_handler
= proc_dointvec_jiffies
,
5485 #ifdef CONFIG_IPV6_ROUTE_INFO
5487 .procname
= "accept_ra_rt_info_max_plen",
5488 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
5489 .maxlen
= sizeof(int),
5491 .proc_handler
= proc_dointvec
,
5496 .procname
= "proxy_ndp",
5497 .data
= &ipv6_devconf
.proxy_ndp
,
5498 .maxlen
= sizeof(int),
5500 .proc_handler
= addrconf_sysctl_proxy_ndp
,
5503 .procname
= "accept_source_route",
5504 .data
= &ipv6_devconf
.accept_source_route
,
5505 .maxlen
= sizeof(int),
5507 .proc_handler
= proc_dointvec
,
5509 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5511 .procname
= "optimistic_dad",
5512 .data
= &ipv6_devconf
.optimistic_dad
,
5513 .maxlen
= sizeof(int),
5515 .proc_handler
= proc_dointvec
,
5519 .procname
= "use_optimistic",
5520 .data
= &ipv6_devconf
.use_optimistic
,
5521 .maxlen
= sizeof(int),
5523 .proc_handler
= proc_dointvec
,
5527 #ifdef CONFIG_IPV6_MROUTE
5529 .procname
= "mc_forwarding",
5530 .data
= &ipv6_devconf
.mc_forwarding
,
5531 .maxlen
= sizeof(int),
5533 .proc_handler
= proc_dointvec
,
5537 .procname
= "disable_ipv6",
5538 .data
= &ipv6_devconf
.disable_ipv6
,
5539 .maxlen
= sizeof(int),
5541 .proc_handler
= addrconf_sysctl_disable
,
5544 .procname
= "accept_dad",
5545 .data
= &ipv6_devconf
.accept_dad
,
5546 .maxlen
= sizeof(int),
5548 .proc_handler
= proc_dointvec
,
5551 .procname
= "force_tllao",
5552 .data
= &ipv6_devconf
.force_tllao
,
5553 .maxlen
= sizeof(int),
5555 .proc_handler
= proc_dointvec
5558 .procname
= "ndisc_notify",
5559 .data
= &ipv6_devconf
.ndisc_notify
,
5560 .maxlen
= sizeof(int),
5562 .proc_handler
= proc_dointvec
5565 .procname
= "suppress_frag_ndisc",
5566 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
5567 .maxlen
= sizeof(int),
5569 .proc_handler
= proc_dointvec
5572 .procname
= "accept_ra_from_local",
5573 .data
= &ipv6_devconf
.accept_ra_from_local
,
5574 .maxlen
= sizeof(int),
5576 .proc_handler
= proc_dointvec
,
5579 .procname
= "accept_ra_mtu",
5580 .data
= &ipv6_devconf
.accept_ra_mtu
,
5581 .maxlen
= sizeof(int),
5583 .proc_handler
= proc_dointvec
,
5586 .procname
= "stable_secret",
5587 .data
= &ipv6_devconf
.stable_secret
,
5588 .maxlen
= IPV6_MAX_STRLEN
,
5590 .proc_handler
= addrconf_sysctl_stable_secret
,
5598 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
5599 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
5602 struct addrconf_sysctl_table
*t
;
5603 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
5605 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
5609 for (i
= 0; t
->addrconf_vars
[i
].data
; i
++) {
5610 t
->addrconf_vars
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
5611 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
5612 t
->addrconf_vars
[i
].extra2
= net
;
5615 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
5617 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->addrconf_vars
);
5618 if (!t
->sysctl_header
)
5630 static void __addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
5632 struct addrconf_sysctl_table
*t
;
5639 unregister_net_sysctl_table(t
->sysctl_header
);
5643 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
5647 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
5650 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
5651 &ndisc_ifinfo_sysctl_change
);
5654 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
5657 neigh_sysctl_unregister(idev
->nd_parms
);
5662 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
5664 __addrconf_sysctl_unregister(&idev
->cnf
);
5665 neigh_sysctl_unregister(idev
->nd_parms
);
5671 static int __net_init
addrconf_init_net(struct net
*net
)
5674 struct ipv6_devconf
*all
, *dflt
;
5676 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
5680 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
5682 goto err_alloc_dflt
;
5684 /* these will be inherited by all namespaces */
5685 dflt
->autoconf
= ipv6_defaults
.autoconf
;
5686 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
5688 dflt
->stable_secret
.initialized
= false;
5689 all
->stable_secret
.initialized
= false;
5691 net
->ipv6
.devconf_all
= all
;
5692 net
->ipv6
.devconf_dflt
= dflt
;
5694 #ifdef CONFIG_SYSCTL
5695 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
5699 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
5705 #ifdef CONFIG_SYSCTL
5707 __addrconf_sysctl_unregister(all
);
5717 static void __net_exit
addrconf_exit_net(struct net
*net
)
5719 #ifdef CONFIG_SYSCTL
5720 __addrconf_sysctl_unregister(net
->ipv6
.devconf_dflt
);
5721 __addrconf_sysctl_unregister(net
->ipv6
.devconf_all
);
5723 kfree(net
->ipv6
.devconf_dflt
);
5724 kfree(net
->ipv6
.devconf_all
);
5727 static struct pernet_operations addrconf_ops
= {
5728 .init
= addrconf_init_net
,
5729 .exit
= addrconf_exit_net
,
5732 static struct rtnl_af_ops inet6_ops __read_mostly
= {
5734 .fill_link_af
= inet6_fill_link_af
,
5735 .get_link_af_size
= inet6_get_link_af_size
,
5736 .validate_link_af
= inet6_validate_link_af
,
5737 .set_link_af
= inet6_set_link_af
,
5741 * Init / cleanup code
5744 int __init
addrconf_init(void)
5746 struct inet6_dev
*idev
;
5749 err
= ipv6_addr_label_init();
5751 pr_crit("%s: cannot initialize default policy table: %d\n",
5756 err
= register_pernet_subsys(&addrconf_ops
);
5760 addrconf_wq
= create_workqueue("ipv6_addrconf");
5766 /* The addrconf netdev notifier requires that loopback_dev
5767 * has it's ipv6 private information allocated and setup
5768 * before it can bring up and give link-local addresses
5769 * to other devices which are up.
5771 * Unfortunately, loopback_dev is not necessarily the first
5772 * entry in the global dev_base list of net devices. In fact,
5773 * it is likely to be the very last entry on that list.
5774 * So this causes the notifier registry below to try and
5775 * give link-local addresses to all devices besides loopback_dev
5776 * first, then loopback_dev, which cases all the non-loopback_dev
5777 * devices to fail to get a link-local address.
5779 * So, as a temporary fix, allocate the ipv6 structure for
5780 * loopback_dev first by hand.
5781 * Longer term, all of the dependencies ipv6 has upon the loopback
5782 * device and it being up should be removed.
5785 idev
= ipv6_add_dev(init_net
.loopback_dev
);
5788 err
= PTR_ERR(idev
);
5792 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5793 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
5795 register_netdevice_notifier(&ipv6_dev_notf
);
5799 rtnl_af_register(&inet6_ops
);
5801 err
= __rtnl_register(PF_INET6
, RTM_GETLINK
, NULL
, inet6_dump_ifinfo
,
5806 /* Only the first call to __rtnl_register can fail */
5807 __rtnl_register(PF_INET6
, RTM_NEWADDR
, inet6_rtm_newaddr
, NULL
, NULL
);
5808 __rtnl_register(PF_INET6
, RTM_DELADDR
, inet6_rtm_deladdr
, NULL
, NULL
);
5809 __rtnl_register(PF_INET6
, RTM_GETADDR
, inet6_rtm_getaddr
,
5810 inet6_dump_ifaddr
, NULL
);
5811 __rtnl_register(PF_INET6
, RTM_GETMULTICAST
, NULL
,
5812 inet6_dump_ifmcaddr
, NULL
);
5813 __rtnl_register(PF_INET6
, RTM_GETANYCAST
, NULL
,
5814 inet6_dump_ifacaddr
, NULL
);
5815 __rtnl_register(PF_INET6
, RTM_GETNETCONF
, inet6_netconf_get_devconf
,
5816 inet6_netconf_dump_devconf
, NULL
);
5818 ipv6_addr_label_rtnl_register();
5822 rtnl_af_unregister(&inet6_ops
);
5823 unregister_netdevice_notifier(&ipv6_dev_notf
);
5825 destroy_workqueue(addrconf_wq
);
5827 unregister_pernet_subsys(&addrconf_ops
);
5829 ipv6_addr_label_cleanup();
5834 void addrconf_cleanup(void)
5836 struct net_device
*dev
;
5839 unregister_netdevice_notifier(&ipv6_dev_notf
);
5840 unregister_pernet_subsys(&addrconf_ops
);
5841 ipv6_addr_label_cleanup();
5845 __rtnl_af_unregister(&inet6_ops
);
5847 /* clean dev list */
5848 for_each_netdev(&init_net
, dev
) {
5849 if (__in6_dev_get(dev
) == NULL
)
5851 addrconf_ifdown(dev
, 1);
5853 addrconf_ifdown(init_net
.loopback_dev
, 2);
5858 spin_lock_bh(&addrconf_hash_lock
);
5859 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5860 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
5861 spin_unlock_bh(&addrconf_hash_lock
);
5862 cancel_delayed_work(&addr_chk_work
);
5865 destroy_workqueue(addrconf_wq
);