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/sched/signal.h>
47 #include <linux/socket.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
50 #include <linux/inet.h>
51 #include <linux/in6.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_addr.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_arcnet.h>
56 #include <linux/if_infiniband.h>
57 #include <linux/route.h>
58 #include <linux/inetdevice.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
62 #include <linux/sysctl.h>
64 #include <linux/capability.h>
65 #include <linux/delay.h>
66 #include <linux/notifier.h>
67 #include <linux/string.h>
68 #include <linux/hash.h>
70 #include <net/net_namespace.h>
74 #include <net/6lowpan.h>
75 #include <net/firewire.h>
77 #include <net/protocol.h>
78 #include <net/ndisc.h>
79 #include <net/ip6_route.h>
80 #include <net/addrconf.h>
83 #include <net/netlink.h>
84 #include <net/pkt_sched.h>
85 #include <net/l3mdev.h>
86 #include <linux/if_tunnel.h>
87 #include <linux/rtnetlink.h>
88 #include <linux/netconf.h>
89 #include <linux/random.h>
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define IPV6_MAX_STRLEN \
100 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
102 static inline u32
cstamp_delta(unsigned long cstamp
)
104 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
107 static inline s32
rfc3315_s14_backoff_init(s32 irt
)
109 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
110 u64 tmp
= (900000 + prandom_u32() % 200001) * (u64
)irt
;
111 do_div(tmp
, 1000000);
115 static inline s32
rfc3315_s14_backoff_update(s32 rt
, s32 mrt
)
117 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
118 u64 tmp
= (1900000 + prandom_u32() % 200001) * (u64
)rt
;
119 do_div(tmp
, 1000000);
120 if ((s32
)tmp
> mrt
) {
121 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
122 tmp
= (900000 + prandom_u32() % 200001) * (u64
)mrt
;
123 do_div(tmp
, 1000000);
129 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
130 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
132 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
137 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
142 static void ipv6_regen_rndid(struct inet6_dev
*idev
);
143 static void ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
145 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
146 static int ipv6_count_addresses(const struct inet6_dev
*idev
);
147 static int ipv6_generate_stable_address(struct in6_addr
*addr
,
149 const struct inet6_dev
*idev
);
151 #define IN6_ADDR_HSIZE_SHIFT 8
152 #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
154 * Configured unicast address hash table
156 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
157 static DEFINE_SPINLOCK(addrconf_hash_lock
);
159 static void addrconf_verify(void);
160 static void addrconf_verify_rtnl(void);
161 static void addrconf_verify_work(struct work_struct
*);
163 static struct workqueue_struct
*addrconf_wq
;
164 static DECLARE_DELAYED_WORK(addr_chk_work
, addrconf_verify_work
);
166 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
167 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
169 static void addrconf_type_change(struct net_device
*dev
,
170 unsigned long event
);
171 static int addrconf_ifdown(struct net_device
*dev
, int how
);
173 static struct fib6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
175 const struct net_device
*dev
,
176 u32 flags
, u32 noflags
);
178 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
179 static void addrconf_dad_work(struct work_struct
*w
);
180 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
,
182 static void addrconf_dad_run(struct inet6_dev
*idev
, bool restart
);
183 static void addrconf_rs_timer(struct timer_list
*t
);
184 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
185 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
187 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
188 struct prefix_info
*pinfo
);
190 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
192 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
193 .mtu6
= IPV6_MIN_MTU
,
195 .accept_redirects
= 1,
197 .force_mld_version
= 0,
198 .mldv1_unsolicited_report_interval
= 10 * HZ
,
199 .mldv2_unsolicited_report_interval
= HZ
,
201 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
202 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
203 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
204 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
206 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
207 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
208 .regen_max_retry
= REGEN_MAX_RETRY
,
209 .max_desync_factor
= MAX_DESYNC_FACTOR
,
210 .max_addresses
= IPV6_MAX_ADDRESSES
,
211 .accept_ra_defrtr
= 1,
212 .accept_ra_from_local
= 0,
213 .accept_ra_min_hop_limit
= 1,
214 .accept_ra_pinfo
= 1,
215 #ifdef CONFIG_IPV6_ROUTER_PREF
216 .accept_ra_rtr_pref
= 1,
217 .rtr_probe_interval
= 60 * HZ
,
218 #ifdef CONFIG_IPV6_ROUTE_INFO
219 .accept_ra_rt_info_min_plen
= 0,
220 .accept_ra_rt_info_max_plen
= 0,
224 .accept_source_route
= 0, /* we do not accept RH0 by default. */
227 .suppress_frag_ndisc
= 1,
230 .initialized
= false,
232 .use_oif_addrs_only
= 0,
233 .ignore_routes_with_linkdown
= 0,
234 .keep_addr_on_down
= 0,
236 #ifdef CONFIG_IPV6_SEG6_HMAC
237 .seg6_require_hmac
= 0,
240 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
244 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
246 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
247 .mtu6
= IPV6_MIN_MTU
,
249 .accept_redirects
= 1,
251 .force_mld_version
= 0,
252 .mldv1_unsolicited_report_interval
= 10 * HZ
,
253 .mldv2_unsolicited_report_interval
= HZ
,
255 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
256 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
257 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
258 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
260 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
261 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
262 .regen_max_retry
= REGEN_MAX_RETRY
,
263 .max_desync_factor
= MAX_DESYNC_FACTOR
,
264 .max_addresses
= IPV6_MAX_ADDRESSES
,
265 .accept_ra_defrtr
= 1,
266 .accept_ra_from_local
= 0,
267 .accept_ra_min_hop_limit
= 1,
268 .accept_ra_pinfo
= 1,
269 #ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref
= 1,
271 .rtr_probe_interval
= 60 * HZ
,
272 #ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_min_plen
= 0,
274 .accept_ra_rt_info_max_plen
= 0,
278 .accept_source_route
= 0, /* we do not accept RH0 by default. */
281 .suppress_frag_ndisc
= 1,
284 .initialized
= false,
286 .use_oif_addrs_only
= 0,
287 .ignore_routes_with_linkdown
= 0,
288 .keep_addr_on_down
= 0,
290 #ifdef CONFIG_IPV6_SEG6_HMAC
291 .seg6_require_hmac
= 0,
294 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
298 /* Check if link is ready: is it up and is a valid qdisc available */
299 static inline bool addrconf_link_ready(const struct net_device
*dev
)
301 return netif_oper_up(dev
) && !qdisc_tx_is_noop(dev
);
304 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
306 if (del_timer(&idev
->rs_timer
))
310 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
312 if (cancel_delayed_work(&ifp
->dad_work
))
316 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
319 if (!timer_pending(&idev
->rs_timer
))
321 mod_timer(&idev
->rs_timer
, jiffies
+ when
);
324 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
328 if (mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
))
332 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
336 idev
->stats
.ipv6
= alloc_percpu(struct ipstats_mib
);
337 if (!idev
->stats
.ipv6
)
340 for_each_possible_cpu(i
) {
341 struct ipstats_mib
*addrconf_stats
;
342 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
343 u64_stats_init(&addrconf_stats
->syncp
);
347 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
349 if (!idev
->stats
.icmpv6dev
)
351 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
353 if (!idev
->stats
.icmpv6msgdev
)
359 kfree(idev
->stats
.icmpv6dev
);
361 free_percpu(idev
->stats
.ipv6
);
366 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
368 struct inet6_dev
*ndev
;
373 if (dev
->mtu
< IPV6_MIN_MTU
)
374 return ERR_PTR(-EINVAL
);
376 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
380 rwlock_init(&ndev
->lock
);
382 INIT_LIST_HEAD(&ndev
->addr_list
);
383 timer_setup(&ndev
->rs_timer
, addrconf_rs_timer
, 0);
384 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
386 if (ndev
->cnf
.stable_secret
.initialized
)
387 ndev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
389 ndev
->cnf
.mtu6
= dev
->mtu
;
390 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
391 if (!ndev
->nd_parms
) {
395 if (ndev
->cnf
.forwarding
)
396 dev_disable_lro(dev
);
397 /* We refer to the device */
400 if (snmp6_alloc_dev(ndev
) < 0) {
401 netdev_dbg(dev
, "%s: cannot allocate memory for statistics\n",
403 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
409 if (snmp6_register_dev(ndev
) < 0) {
410 netdev_dbg(dev
, "%s: cannot create /proc/net/dev_snmp6/%s\n",
411 __func__
, dev
->name
);
415 /* One reference from device. */
416 refcount_set(&ndev
->refcnt
, 1);
418 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
419 ndev
->cnf
.accept_dad
= -1;
421 #if IS_ENABLED(CONFIG_IPV6_SIT)
422 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
423 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
424 ndev
->cnf
.rtr_solicits
= 0;
428 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
429 ndev
->desync_factor
= U32_MAX
;
430 if ((dev
->flags
&IFF_LOOPBACK
) ||
431 dev
->type
== ARPHRD_TUNNEL
||
432 dev
->type
== ARPHRD_TUNNEL6
||
433 dev
->type
== ARPHRD_SIT
||
434 dev
->type
== ARPHRD_NONE
) {
435 ndev
->cnf
.use_tempaddr
= -1;
437 ipv6_regen_rndid(ndev
);
439 ndev
->token
= in6addr_any
;
441 if (netif_running(dev
) && addrconf_link_ready(dev
))
442 ndev
->if_flags
|= IF_READY
;
444 ipv6_mc_init_dev(ndev
);
445 ndev
->tstamp
= jiffies
;
446 err
= addrconf_sysctl_register(ndev
);
448 ipv6_mc_destroy_dev(ndev
);
449 snmp6_unregister_dev(ndev
);
452 /* protected by rtnl_lock */
453 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
455 /* Join interface-local all-node multicast group */
456 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
458 /* Join all-node multicast group */
459 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
461 /* Join all-router multicast group if forwarding is set */
462 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
463 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
468 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
470 in6_dev_finish_destroy(ndev
);
474 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
476 struct inet6_dev
*idev
;
480 idev
= __in6_dev_get(dev
);
482 idev
= ipv6_add_dev(dev
);
487 if (dev
->flags
&IFF_UP
)
492 static int inet6_netconf_msgsize_devconf(int type
)
494 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
495 + nla_total_size(4); /* NETCONFA_IFINDEX */
498 if (type
== NETCONFA_ALL
)
501 if (all
|| type
== NETCONFA_FORWARDING
)
502 size
+= nla_total_size(4);
503 #ifdef CONFIG_IPV6_MROUTE
504 if (all
|| type
== NETCONFA_MC_FORWARDING
)
505 size
+= nla_total_size(4);
507 if (all
|| type
== NETCONFA_PROXY_NEIGH
)
508 size
+= nla_total_size(4);
510 if (all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
)
511 size
+= nla_total_size(4);
516 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
517 struct ipv6_devconf
*devconf
, u32 portid
,
518 u32 seq
, int event
, unsigned int flags
,
521 struct nlmsghdr
*nlh
;
522 struct netconfmsg
*ncm
;
525 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
530 if (type
== NETCONFA_ALL
)
533 ncm
= nlmsg_data(nlh
);
534 ncm
->ncm_family
= AF_INET6
;
536 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
537 goto nla_put_failure
;
542 if ((all
|| type
== NETCONFA_FORWARDING
) &&
543 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
544 goto nla_put_failure
;
545 #ifdef CONFIG_IPV6_MROUTE
546 if ((all
|| type
== NETCONFA_MC_FORWARDING
) &&
547 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
548 devconf
->mc_forwarding
) < 0)
549 goto nla_put_failure
;
551 if ((all
|| type
== NETCONFA_PROXY_NEIGH
) &&
552 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
553 goto nla_put_failure
;
555 if ((all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
) &&
556 nla_put_s32(skb
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
557 devconf
->ignore_routes_with_linkdown
) < 0)
558 goto nla_put_failure
;
565 nlmsg_cancel(skb
, nlh
);
569 void inet6_netconf_notify_devconf(struct net
*net
, int event
, int type
,
570 int ifindex
, struct ipv6_devconf
*devconf
)
575 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_KERNEL
);
579 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
582 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
583 WARN_ON(err
== -EMSGSIZE
);
587 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_KERNEL
);
590 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
593 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
594 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
595 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
596 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
597 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
] = { .len
= sizeof(int) },
600 static int inet6_netconf_valid_get_req(struct sk_buff
*skb
,
601 const struct nlmsghdr
*nlh
,
603 struct netlink_ext_ack
*extack
)
607 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(struct netconfmsg
))) {
608 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for netconf get request");
612 if (!netlink_strict_get_check(skb
))
613 return nlmsg_parse(nlh
, sizeof(struct netconfmsg
), tb
,
614 NETCONFA_MAX
, devconf_ipv6_policy
, extack
);
616 err
= nlmsg_parse_strict(nlh
, sizeof(struct netconfmsg
), tb
,
617 NETCONFA_MAX
, devconf_ipv6_policy
, extack
);
621 for (i
= 0; i
<= NETCONFA_MAX
; i
++) {
626 case NETCONFA_IFINDEX
:
629 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in netconf get request");
637 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
638 struct nlmsghdr
*nlh
,
639 struct netlink_ext_ack
*extack
)
641 struct net
*net
= sock_net(in_skb
->sk
);
642 struct nlattr
*tb
[NETCONFA_MAX
+1];
643 struct inet6_dev
*in6_dev
= NULL
;
644 struct net_device
*dev
= NULL
;
646 struct ipv6_devconf
*devconf
;
650 err
= inet6_netconf_valid_get_req(in_skb
, nlh
, tb
, extack
);
654 if (!tb
[NETCONFA_IFINDEX
])
658 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
660 case NETCONFA_IFINDEX_ALL
:
661 devconf
= net
->ipv6
.devconf_all
;
663 case NETCONFA_IFINDEX_DEFAULT
:
664 devconf
= net
->ipv6
.devconf_dflt
;
667 dev
= dev_get_by_index(net
, ifindex
);
670 in6_dev
= in6_dev_get(dev
);
673 devconf
= &in6_dev
->cnf
;
678 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_KERNEL
);
682 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
683 NETLINK_CB(in_skb
).portid
,
684 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
687 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
688 WARN_ON(err
== -EMSGSIZE
);
692 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
695 in6_dev_put(in6_dev
);
701 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
702 struct netlink_callback
*cb
)
704 const struct nlmsghdr
*nlh
= cb
->nlh
;
705 struct net
*net
= sock_net(skb
->sk
);
708 struct net_device
*dev
;
709 struct inet6_dev
*idev
;
710 struct hlist_head
*head
;
712 if (cb
->strict_check
) {
713 struct netlink_ext_ack
*extack
= cb
->extack
;
714 struct netconfmsg
*ncm
;
716 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ncm
))) {
717 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for netconf dump request");
721 if (nlmsg_attrlen(nlh
, sizeof(*ncm
))) {
722 NL_SET_ERR_MSG_MOD(extack
, "Invalid data after header in netconf dump request");
728 s_idx
= idx
= cb
->args
[1];
730 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
732 head
= &net
->dev_index_head
[h
];
734 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
736 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
739 idev
= __in6_dev_get(dev
);
743 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
745 NETLINK_CB(cb
->skb
).portid
,
753 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
759 if (h
== NETDEV_HASHENTRIES
) {
760 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
761 net
->ipv6
.devconf_all
,
762 NETLINK_CB(cb
->skb
).portid
,
764 RTM_NEWNETCONF
, NLM_F_MULTI
,
770 if (h
== NETDEV_HASHENTRIES
+ 1) {
771 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
772 net
->ipv6
.devconf_dflt
,
773 NETLINK_CB(cb
->skb
).portid
,
775 RTM_NEWNETCONF
, NLM_F_MULTI
,
789 static void dev_forward_change(struct inet6_dev
*idev
)
791 struct net_device
*dev
;
792 struct inet6_ifaddr
*ifa
;
797 if (idev
->cnf
.forwarding
)
798 dev_disable_lro(dev
);
799 if (dev
->flags
& IFF_MULTICAST
) {
800 if (idev
->cnf
.forwarding
) {
801 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
802 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
803 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
805 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
806 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
807 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
811 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
812 if (ifa
->flags
&IFA_F_TENTATIVE
)
814 if (idev
->cnf
.forwarding
)
815 addrconf_join_anycast(ifa
);
817 addrconf_leave_anycast(ifa
);
819 inet6_netconf_notify_devconf(dev_net(dev
), RTM_NEWNETCONF
,
821 dev
->ifindex
, &idev
->cnf
);
825 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
827 struct net_device
*dev
;
828 struct inet6_dev
*idev
;
830 for_each_netdev(net
, dev
) {
831 idev
= __in6_dev_get(dev
);
833 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
834 idev
->cnf
.forwarding
= newf
;
836 dev_forward_change(idev
);
841 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
847 return restart_syscall();
849 net
= (struct net
*)table
->extra2
;
853 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
854 if ((!newf
) ^ (!old
))
855 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
857 NETCONFA_IFINDEX_DEFAULT
,
858 net
->ipv6
.devconf_dflt
);
863 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
864 int old_dflt
= net
->ipv6
.devconf_dflt
->forwarding
;
866 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
867 if ((!newf
) ^ (!old_dflt
))
868 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
870 NETCONFA_IFINDEX_DEFAULT
,
871 net
->ipv6
.devconf_dflt
);
873 addrconf_forward_change(net
, newf
);
874 if ((!newf
) ^ (!old
))
875 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
877 NETCONFA_IFINDEX_ALL
,
878 net
->ipv6
.devconf_all
);
879 } else if ((!newf
) ^ (!old
))
880 dev_forward_change((struct inet6_dev
*)table
->extra1
);
884 rt6_purge_dflt_routers(net
);
888 static void addrconf_linkdown_change(struct net
*net
, __s32 newf
)
890 struct net_device
*dev
;
891 struct inet6_dev
*idev
;
893 for_each_netdev(net
, dev
) {
894 idev
= __in6_dev_get(dev
);
896 int changed
= (!idev
->cnf
.ignore_routes_with_linkdown
) ^ (!newf
);
898 idev
->cnf
.ignore_routes_with_linkdown
= newf
;
900 inet6_netconf_notify_devconf(dev_net(dev
),
902 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
909 static int addrconf_fixup_linkdown(struct ctl_table
*table
, int *p
, int newf
)
915 return restart_syscall();
917 net
= (struct net
*)table
->extra2
;
921 if (p
== &net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
) {
922 if ((!newf
) ^ (!old
))
923 inet6_netconf_notify_devconf(net
,
925 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
926 NETCONFA_IFINDEX_DEFAULT
,
927 net
->ipv6
.devconf_dflt
);
932 if (p
== &net
->ipv6
.devconf_all
->ignore_routes_with_linkdown
) {
933 net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
= newf
;
934 addrconf_linkdown_change(net
, newf
);
935 if ((!newf
) ^ (!old
))
936 inet6_netconf_notify_devconf(net
,
938 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
939 NETCONFA_IFINDEX_ALL
,
940 net
->ipv6
.devconf_all
);
949 /* Nobody refers to this ifaddr, destroy it */
950 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
952 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
954 #ifdef NET_REFCNT_DEBUG
955 pr_debug("%s\n", __func__
);
958 in6_dev_put(ifp
->idev
);
960 if (cancel_delayed_work(&ifp
->dad_work
))
961 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
964 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
965 pr_warn("Freeing alive inet6 address %p\n", ifp
);
973 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
976 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
979 * Each device address list is sorted in order of scope -
980 * global before linklocal.
982 list_for_each(p
, &idev
->addr_list
) {
983 struct inet6_ifaddr
*ifa
984 = list_entry(p
, struct inet6_ifaddr
, if_list
);
985 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
989 list_add_tail_rcu(&ifp
->if_list
, p
);
992 static u32
inet6_addr_hash(const struct net
*net
, const struct in6_addr
*addr
)
994 u32 val
= ipv6_addr_hash(addr
) ^ net_hash_mix(net
);
996 return hash_32(val
, IN6_ADDR_HSIZE_SHIFT
);
999 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1000 struct net_device
*dev
, unsigned int hash
)
1002 struct inet6_ifaddr
*ifp
;
1004 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1005 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1007 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1008 if (!dev
|| ifp
->idev
->dev
== dev
)
1015 static int ipv6_add_addr_hash(struct net_device
*dev
, struct inet6_ifaddr
*ifa
)
1017 unsigned int hash
= inet6_addr_hash(dev_net(dev
), &ifa
->addr
);
1020 spin_lock(&addrconf_hash_lock
);
1022 /* Ignore adding duplicate addresses on an interface */
1023 if (ipv6_chk_same_addr(dev_net(dev
), &ifa
->addr
, dev
, hash
)) {
1024 netdev_dbg(dev
, "ipv6_add_addr: already assigned\n");
1027 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
1030 spin_unlock(&addrconf_hash_lock
);
1035 /* On success it returns ifp with increased reference count */
1037 static struct inet6_ifaddr
*
1038 ipv6_add_addr(struct inet6_dev
*idev
, struct ifa6_config
*cfg
,
1039 bool can_block
, struct netlink_ext_ack
*extack
)
1041 gfp_t gfp_flags
= can_block
? GFP_KERNEL
: GFP_ATOMIC
;
1042 int addr_type
= ipv6_addr_type(cfg
->pfx
);
1043 struct net
*net
= dev_net(idev
->dev
);
1044 struct inet6_ifaddr
*ifa
= NULL
;
1045 struct fib6_info
*f6i
= NULL
;
1048 if (addr_type
== IPV6_ADDR_ANY
||
1049 addr_type
& IPV6_ADDR_MULTICAST
||
1050 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
1051 !netif_is_l3_master(idev
->dev
) &&
1052 addr_type
& IPV6_ADDR_LOOPBACK
))
1053 return ERR_PTR(-EADDRNOTAVAIL
);
1056 err
= -ENODEV
; /*XXX*/
1060 if (idev
->cnf
.disable_ipv6
) {
1065 /* validator notifier needs to be blocking;
1066 * do not call in atomic context
1069 struct in6_validator_info i6vi
= {
1070 .i6vi_addr
= *cfg
->pfx
,
1075 err
= inet6addr_validator_notifier_call_chain(NETDEV_UP
, &i6vi
);
1076 err
= notifier_to_errno(err
);
1081 ifa
= kzalloc(sizeof(*ifa
), gfp_flags
);
1087 f6i
= addrconf_f6i_alloc(net
, idev
, cfg
->pfx
, false, gfp_flags
);
1094 if (net
->ipv6
.devconf_all
->disable_policy
||
1095 idev
->cnf
.disable_policy
)
1096 f6i
->dst_nopolicy
= true;
1098 neigh_parms_data_state_setall(idev
->nd_parms
);
1100 ifa
->addr
= *cfg
->pfx
;
1102 ifa
->peer_addr
= *cfg
->peer_pfx
;
1104 spin_lock_init(&ifa
->lock
);
1105 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
1106 INIT_HLIST_NODE(&ifa
->addr_lst
);
1107 ifa
->scope
= cfg
->scope
;
1108 ifa
->prefix_len
= cfg
->plen
;
1109 ifa
->rt_priority
= cfg
->rt_priority
;
1110 ifa
->flags
= cfg
->ifa_flags
;
1111 /* No need to add the TENTATIVE flag for addresses with NODAD */
1112 if (!(cfg
->ifa_flags
& IFA_F_NODAD
))
1113 ifa
->flags
|= IFA_F_TENTATIVE
;
1114 ifa
->valid_lft
= cfg
->valid_lft
;
1115 ifa
->prefered_lft
= cfg
->preferred_lft
;
1116 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
1117 ifa
->tokenized
= false;
1125 refcount_set(&ifa
->refcnt
, 1);
1129 err
= ipv6_add_addr_hash(idev
->dev
, ifa
);
1131 rcu_read_unlock_bh();
1135 write_lock(&idev
->lock
);
1137 /* Add to inet6_dev unicast addr list. */
1138 ipv6_link_dev_addr(idev
, ifa
);
1140 if (ifa
->flags
&IFA_F_TEMPORARY
) {
1141 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
1146 write_unlock(&idev
->lock
);
1148 rcu_read_unlock_bh();
1150 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
1152 if (unlikely(err
< 0)) {
1153 fib6_info_release(f6i
);
1157 in6_dev_put(ifa
->idev
);
1166 enum cleanup_prefix_rt_t
{
1167 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
1168 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
1169 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
1173 * Check, whether the prefix for ifp would still need a prefix route
1174 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1177 * 1) we don't purge prefix if address was not permanent.
1178 * prefix is managed by its own lifetime.
1179 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1180 * 3) if there are no addresses, delete prefix.
1181 * 4) if there are still other permanent address(es),
1182 * corresponding prefix is still permanent.
1183 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1184 * don't purge the prefix, assume user space is managing it.
1185 * 6) otherwise, update prefix lifetime to the
1186 * longest valid lifetime among the corresponding
1187 * addresses on the device.
1188 * Note: subsequent RA will update lifetime.
1190 static enum cleanup_prefix_rt_t
1191 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
1193 struct inet6_ifaddr
*ifa
;
1194 struct inet6_dev
*idev
= ifp
->idev
;
1195 unsigned long lifetime
;
1196 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
1200 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1203 if (ifa
->prefix_len
!= ifp
->prefix_len
||
1204 !ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
1207 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
1208 return CLEANUP_PREFIX_RT_NOP
;
1210 action
= CLEANUP_PREFIX_RT_EXPIRE
;
1212 spin_lock(&ifa
->lock
);
1214 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
1216 * Note: Because this address is
1217 * not permanent, lifetime <
1218 * LONG_MAX / HZ here.
1220 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
1221 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
1222 spin_unlock(&ifa
->lock
);
1229 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
, bool del_rt
)
1231 struct fib6_info
*f6i
;
1233 f6i
= addrconf_get_prefix_route(&ifp
->addr
,
1236 0, RTF_GATEWAY
| RTF_DEFAULT
);
1239 ip6_del_rt(dev_net(ifp
->idev
->dev
), f6i
);
1241 if (!(f6i
->fib6_flags
& RTF_EXPIRES
))
1242 fib6_set_expires(f6i
, expires
);
1243 fib6_info_release(f6i
);
1249 /* This function wants to get referenced ifp and releases it before return */
1251 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1254 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1255 unsigned long expires
;
1259 spin_lock_bh(&ifp
->lock
);
1261 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1262 spin_unlock_bh(&ifp
->lock
);
1264 if (state
== INET6_IFADDR_STATE_DEAD
)
1267 spin_lock_bh(&addrconf_hash_lock
);
1268 hlist_del_init_rcu(&ifp
->addr_lst
);
1269 spin_unlock_bh(&addrconf_hash_lock
);
1271 write_lock_bh(&ifp
->idev
->lock
);
1273 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1274 list_del(&ifp
->tmp_list
);
1276 in6_ifa_put(ifp
->ifpub
);
1282 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1283 action
= check_cleanup_prefix_route(ifp
, &expires
);
1285 list_del_rcu(&ifp
->if_list
);
1288 write_unlock_bh(&ifp
->idev
->lock
);
1290 addrconf_del_dad_work(ifp
);
1292 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1294 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1296 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1297 cleanup_prefix_route(ifp
, expires
,
1298 action
== CLEANUP_PREFIX_RT_DEL
);
1301 /* clean up prefsrc entries */
1302 rt6_remove_prefsrc(ifp
);
1307 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
,
1308 struct inet6_ifaddr
*ift
,
1311 struct inet6_dev
*idev
= ifp
->idev
;
1312 struct in6_addr addr
, *tmpaddr
;
1313 unsigned long tmp_tstamp
, age
;
1314 unsigned long regen_advance
;
1315 struct ifa6_config cfg
;
1317 unsigned long now
= jiffies
;
1318 long max_desync_factor
;
1319 s32 cnf_temp_preferred_lft
;
1321 write_lock_bh(&idev
->lock
);
1323 spin_lock_bh(&ift
->lock
);
1324 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1325 spin_unlock_bh(&ift
->lock
);
1332 if (idev
->cnf
.use_tempaddr
<= 0) {
1333 write_unlock_bh(&idev
->lock
);
1334 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1339 spin_lock_bh(&ifp
->lock
);
1340 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1341 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1342 spin_unlock_bh(&ifp
->lock
);
1343 write_unlock_bh(&idev
->lock
);
1344 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1351 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1352 ipv6_try_regen_rndid(idev
, tmpaddr
);
1353 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1354 age
= (now
- ifp
->tstamp
) / HZ
;
1356 regen_advance
= idev
->cnf
.regen_max_retry
*
1357 idev
->cnf
.dad_transmits
*
1358 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) / HZ
;
1360 /* recalculate max_desync_factor each time and update
1361 * idev->desync_factor if it's larger
1363 cnf_temp_preferred_lft
= READ_ONCE(idev
->cnf
.temp_prefered_lft
);
1364 max_desync_factor
= min_t(__u32
,
1365 idev
->cnf
.max_desync_factor
,
1366 cnf_temp_preferred_lft
- regen_advance
);
1368 if (unlikely(idev
->desync_factor
> max_desync_factor
)) {
1369 if (max_desync_factor
> 0) {
1370 get_random_bytes(&idev
->desync_factor
,
1371 sizeof(idev
->desync_factor
));
1372 idev
->desync_factor
%= max_desync_factor
;
1374 idev
->desync_factor
= 0;
1378 memset(&cfg
, 0, sizeof(cfg
));
1379 cfg
.valid_lft
= min_t(__u32
, ifp
->valid_lft
,
1380 idev
->cnf
.temp_valid_lft
+ age
);
1381 cfg
.preferred_lft
= cnf_temp_preferred_lft
+ age
- idev
->desync_factor
;
1382 cfg
.preferred_lft
= min_t(__u32
, ifp
->prefered_lft
, cfg
.preferred_lft
);
1384 cfg
.plen
= ifp
->prefix_len
;
1385 tmp_tstamp
= ifp
->tstamp
;
1386 spin_unlock_bh(&ifp
->lock
);
1388 write_unlock_bh(&idev
->lock
);
1390 /* A temporary address is created only if this calculated Preferred
1391 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1392 * an implementation must not create a temporary address with a zero
1393 * Preferred Lifetime.
1394 * Use age calculation as in addrconf_verify to avoid unnecessary
1395 * temporary addresses being generated.
1397 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1398 if (cfg
.preferred_lft
<= regen_advance
+ age
) {
1405 cfg
.ifa_flags
= IFA_F_TEMPORARY
;
1406 /* set in addrconf_prefix_rcv() */
1407 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1408 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
1411 cfg
.scope
= ipv6_addr_scope(cfg
.pfx
);
1413 ift
= ipv6_add_addr(idev
, &cfg
, block
, NULL
);
1417 pr_info("%s: retry temporary address regeneration\n", __func__
);
1419 write_lock_bh(&idev
->lock
);
1423 spin_lock_bh(&ift
->lock
);
1426 ift
->tstamp
= tmp_tstamp
;
1427 spin_unlock_bh(&ift
->lock
);
1429 addrconf_dad_start(ift
);
1437 * Choose an appropriate source address (RFC3484)
1440 IPV6_SADDR_RULE_INIT
= 0,
1441 IPV6_SADDR_RULE_LOCAL
,
1442 IPV6_SADDR_RULE_SCOPE
,
1443 IPV6_SADDR_RULE_PREFERRED
,
1444 #ifdef CONFIG_IPV6_MIP6
1445 IPV6_SADDR_RULE_HOA
,
1447 IPV6_SADDR_RULE_OIF
,
1448 IPV6_SADDR_RULE_LABEL
,
1449 IPV6_SADDR_RULE_PRIVACY
,
1450 IPV6_SADDR_RULE_ORCHID
,
1451 IPV6_SADDR_RULE_PREFIX
,
1452 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1453 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1458 struct ipv6_saddr_score
{
1461 struct inet6_ifaddr
*ifa
;
1462 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1467 struct ipv6_saddr_dst
{
1468 const struct in6_addr
*addr
;
1475 static inline int ipv6_saddr_preferred(int type
)
1477 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1482 static bool ipv6_use_optimistic_addr(struct net
*net
,
1483 struct inet6_dev
*idev
)
1485 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1488 if (!net
->ipv6
.devconf_all
->optimistic_dad
&& !idev
->cnf
.optimistic_dad
)
1490 if (!net
->ipv6
.devconf_all
->use_optimistic
&& !idev
->cnf
.use_optimistic
)
1499 static bool ipv6_allow_optimistic_dad(struct net
*net
,
1500 struct inet6_dev
*idev
)
1502 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1505 if (!net
->ipv6
.devconf_all
->optimistic_dad
&& !idev
->cnf
.optimistic_dad
)
1514 static int ipv6_get_saddr_eval(struct net
*net
,
1515 struct ipv6_saddr_score
*score
,
1516 struct ipv6_saddr_dst
*dst
,
1521 if (i
<= score
->rule
) {
1523 case IPV6_SADDR_RULE_SCOPE
:
1524 ret
= score
->scopedist
;
1526 case IPV6_SADDR_RULE_PREFIX
:
1527 ret
= score
->matchlen
;
1530 ret
= !!test_bit(i
, score
->scorebits
);
1536 case IPV6_SADDR_RULE_INIT
:
1537 /* Rule 0: remember if hiscore is not ready yet */
1540 case IPV6_SADDR_RULE_LOCAL
:
1541 /* Rule 1: Prefer same address */
1542 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1544 case IPV6_SADDR_RULE_SCOPE
:
1545 /* Rule 2: Prefer appropriate scope
1550 * ---+--+-+---> scope
1552 * | d is scope of the destination.
1554 * | \ <- smaller scope is better if
1555 * B-15 | \ if scope is enough for destination.
1556 * | ret = B - scope (-1 <= scope >= d <= 15).
1558 * |/ <- greater is better
1559 * -C / if scope is not enough for destination.
1560 * /| ret = scope - C (-1 <= d < scope <= 15).
1562 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1563 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1564 * Assume B = 0 and we get C > 29.
1566 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1567 if (ret
>= dst
->scope
)
1570 ret
-= 128; /* 30 is enough */
1571 score
->scopedist
= ret
;
1573 case IPV6_SADDR_RULE_PREFERRED
:
1575 /* Rule 3: Avoid deprecated and optimistic addresses */
1576 u8 avoid
= IFA_F_DEPRECATED
;
1578 if (!ipv6_use_optimistic_addr(net
, score
->ifa
->idev
))
1579 avoid
|= IFA_F_OPTIMISTIC
;
1580 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1581 !(score
->ifa
->flags
& avoid
);
1584 #ifdef CONFIG_IPV6_MIP6
1585 case IPV6_SADDR_RULE_HOA
:
1587 /* Rule 4: Prefer home address */
1588 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1589 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1593 case IPV6_SADDR_RULE_OIF
:
1594 /* Rule 5: Prefer outgoing interface */
1595 ret
= (!dst
->ifindex
||
1596 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1598 case IPV6_SADDR_RULE_LABEL
:
1599 /* Rule 6: Prefer matching label */
1600 ret
= ipv6_addr_label(net
,
1601 &score
->ifa
->addr
, score
->addr_type
,
1602 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1604 case IPV6_SADDR_RULE_PRIVACY
:
1606 /* Rule 7: Prefer public address
1607 * Note: prefer temporary address if use_tempaddr >= 2
1609 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1610 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1611 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1612 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1615 case IPV6_SADDR_RULE_ORCHID
:
1616 /* Rule 8-: Prefer ORCHID vs ORCHID or
1617 * non-ORCHID vs non-ORCHID
1619 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1620 ipv6_addr_orchid(dst
->addr
));
1622 case IPV6_SADDR_RULE_PREFIX
:
1623 /* Rule 8: Use longest matching prefix */
1624 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1625 if (ret
> score
->ifa
->prefix_len
)
1626 ret
= score
->ifa
->prefix_len
;
1627 score
->matchlen
= ret
;
1629 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1630 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1631 /* Optimistic addresses still have lower precedence than other
1632 * preferred addresses.
1634 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1642 __set_bit(i
, score
->scorebits
);
1648 static int __ipv6_dev_get_saddr(struct net
*net
,
1649 struct ipv6_saddr_dst
*dst
,
1650 struct inet6_dev
*idev
,
1651 struct ipv6_saddr_score
*scores
,
1654 struct ipv6_saddr_score
*score
= &scores
[1 - hiscore_idx
], *hiscore
= &scores
[hiscore_idx
];
1656 list_for_each_entry_rcu(score
->ifa
, &idev
->addr_list
, if_list
) {
1660 * - Tentative Address (RFC2462 section 5.4)
1661 * - A tentative address is not considered
1662 * "assigned to an interface" in the traditional
1663 * sense, unless it is also flagged as optimistic.
1664 * - Candidate Source Address (section 4)
1665 * - In any case, anycast addresses, multicast
1666 * addresses, and the unspecified address MUST
1667 * NOT be included in a candidate set.
1669 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1670 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1673 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1675 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1676 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1677 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1683 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1685 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1686 int minihiscore
, miniscore
;
1688 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, dst
, i
);
1689 miniscore
= ipv6_get_saddr_eval(net
, score
, dst
, i
);
1691 if (minihiscore
> miniscore
) {
1692 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1693 score
->scopedist
> 0) {
1696 * each remaining entry
1697 * has too small (not enough)
1698 * scope, because ifa entries
1699 * are sorted by their scope
1705 } else if (minihiscore
< miniscore
) {
1706 swap(hiscore
, score
);
1707 hiscore_idx
= 1 - hiscore_idx
;
1709 /* restore our iterator */
1710 score
->ifa
= hiscore
->ifa
;
1720 static int ipv6_get_saddr_master(struct net
*net
,
1721 const struct net_device
*dst_dev
,
1722 const struct net_device
*master
,
1723 struct ipv6_saddr_dst
*dst
,
1724 struct ipv6_saddr_score
*scores
,
1727 struct inet6_dev
*idev
;
1729 idev
= __in6_dev_get(dst_dev
);
1731 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1732 scores
, hiscore_idx
);
1734 idev
= __in6_dev_get(master
);
1736 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1737 scores
, hiscore_idx
);
1742 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1743 const struct in6_addr
*daddr
, unsigned int prefs
,
1744 struct in6_addr
*saddr
)
1746 struct ipv6_saddr_score scores
[2], *hiscore
;
1747 struct ipv6_saddr_dst dst
;
1748 struct inet6_dev
*idev
;
1749 struct net_device
*dev
;
1751 bool use_oif_addr
= false;
1752 int hiscore_idx
= 0;
1755 dst_type
= __ipv6_addr_type(daddr
);
1757 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1758 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1759 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1762 scores
[hiscore_idx
].rule
= -1;
1763 scores
[hiscore_idx
].ifa
= NULL
;
1767 /* Candidate Source Address (section 4)
1768 * - multicast and link-local destination address,
1769 * the set of candidate source address MUST only
1770 * include addresses assigned to interfaces
1771 * belonging to the same link as the outgoing
1773 * (- For site-local destination addresses, the
1774 * set of candidate source addresses MUST only
1775 * include addresses assigned to interfaces
1776 * belonging to the same site as the outgoing
1778 * - "It is RECOMMENDED that the candidate source addresses
1779 * be the set of unicast addresses assigned to the
1780 * interface that will be used to send to the destination
1781 * (the 'outgoing' interface)." (RFC 6724)
1784 idev
= __in6_dev_get(dst_dev
);
1785 if ((dst_type
& IPV6_ADDR_MULTICAST
) ||
1786 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
||
1787 (idev
&& idev
->cnf
.use_oif_addrs_only
)) {
1788 use_oif_addr
= true;
1794 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1796 const struct net_device
*master
;
1799 /* if dst_dev exists and is enslaved to an L3 device, then
1800 * prefer addresses from dst_dev and then the master over
1801 * any other enslaved devices in the L3 domain.
1803 master
= l3mdev_master_dev_rcu(dst_dev
);
1805 master_idx
= master
->ifindex
;
1807 hiscore_idx
= ipv6_get_saddr_master(net
, dst_dev
,
1809 scores
, hiscore_idx
);
1811 if (scores
[hiscore_idx
].ifa
)
1815 for_each_netdev_rcu(net
, dev
) {
1816 /* only consider addresses on devices in the
1819 if (l3mdev_master_ifindex_rcu(dev
) != master_idx
)
1821 idev
= __in6_dev_get(dev
);
1824 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1829 hiscore
= &scores
[hiscore_idx
];
1831 ret
= -EADDRNOTAVAIL
;
1833 *saddr
= hiscore
->ifa
->addr
;
1838 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1840 int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1843 struct inet6_ifaddr
*ifp
;
1844 int err
= -EADDRNOTAVAIL
;
1846 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1847 if (ifp
->scope
> IFA_LINK
)
1849 if (ifp
->scope
== IFA_LINK
&&
1850 !(ifp
->flags
& banned_flags
)) {
1859 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1862 struct inet6_dev
*idev
;
1863 int err
= -EADDRNOTAVAIL
;
1866 idev
= __in6_dev_get(dev
);
1868 read_lock_bh(&idev
->lock
);
1869 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1870 read_unlock_bh(&idev
->lock
);
1876 static int ipv6_count_addresses(const struct inet6_dev
*idev
)
1878 const struct inet6_ifaddr
*ifp
;
1882 list_for_each_entry_rcu(ifp
, &idev
->addr_list
, if_list
)
1888 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1889 const struct net_device
*dev
, int strict
)
1891 return ipv6_chk_addr_and_flags(net
, addr
, dev
, !dev
,
1892 strict
, IFA_F_TENTATIVE
);
1894 EXPORT_SYMBOL(ipv6_chk_addr
);
1896 /* device argument is used to find the L3 domain of interest. If
1897 * skip_dev_check is set, then the ifp device is not checked against
1898 * the passed in dev argument. So the 2 cases for addresses checks are:
1899 * 1. does the address exist in the L3 domain that dev is part of
1900 * (skip_dev_check = true), or
1902 * 2. does the address exist on the specific device
1903 * (skip_dev_check = false)
1905 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1906 const struct net_device
*dev
, bool skip_dev_check
,
1907 int strict
, u32 banned_flags
)
1909 unsigned int hash
= inet6_addr_hash(net
, addr
);
1910 const struct net_device
*l3mdev
;
1911 struct inet6_ifaddr
*ifp
;
1916 l3mdev
= l3mdev_master_dev_rcu(dev
);
1920 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1921 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1924 if (l3mdev_master_dev_rcu(ifp
->idev
->dev
) != l3mdev
)
1927 /* Decouple optimistic from tentative for evaluation here.
1928 * Ban optimistic addresses explicitly, when required.
1930 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1931 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1933 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1934 !(ifp_flags
&banned_flags
) &&
1935 (!dev
|| ifp
->idev
->dev
== dev
||
1936 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1945 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1948 /* Compares an address/prefix_len with addresses on device @dev.
1949 * If one is found it returns true.
1951 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1952 const unsigned int prefix_len
, struct net_device
*dev
)
1954 const struct inet6_ifaddr
*ifa
;
1955 const struct inet6_dev
*idev
;
1959 idev
= __in6_dev_get(dev
);
1961 list_for_each_entry_rcu(ifa
, &idev
->addr_list
, if_list
) {
1962 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
1971 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
1973 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1975 const struct inet6_ifaddr
*ifa
;
1976 const struct inet6_dev
*idev
;
1981 idev
= __in6_dev_get(dev
);
1983 list_for_each_entry_rcu(ifa
, &idev
->addr_list
, if_list
) {
1984 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1993 EXPORT_SYMBOL(ipv6_chk_prefix
);
1995 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1996 struct net_device
*dev
, int strict
)
1998 unsigned int hash
= inet6_addr_hash(net
, addr
);
1999 struct inet6_ifaddr
*ifp
, *result
= NULL
;
2002 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
2003 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
2005 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
2006 if (!dev
|| ifp
->idev
->dev
== dev
||
2007 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
2019 /* Gets referenced address, destroys ifaddr */
2021 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
2024 ifp
->flags
|= IFA_F_DADFAILED
;
2026 if (ifp
->flags
&IFA_F_TEMPORARY
) {
2027 struct inet6_ifaddr
*ifpub
;
2028 spin_lock_bh(&ifp
->lock
);
2031 in6_ifa_hold(ifpub
);
2032 spin_unlock_bh(&ifp
->lock
);
2033 ipv6_create_tempaddr(ifpub
, ifp
, true);
2036 spin_unlock_bh(&ifp
->lock
);
2039 } else if (ifp
->flags
&IFA_F_PERMANENT
|| !dad_failed
) {
2040 spin_lock_bh(&ifp
->lock
);
2041 addrconf_del_dad_work(ifp
);
2042 ifp
->flags
|= IFA_F_TENTATIVE
;
2044 ifp
->flags
&= ~IFA_F_OPTIMISTIC
;
2045 spin_unlock_bh(&ifp
->lock
);
2047 ipv6_ifa_notify(0, ifp
);
2054 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
2058 spin_lock_bh(&ifp
->lock
);
2059 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
2060 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
2063 spin_unlock_bh(&ifp
->lock
);
2068 void addrconf_dad_failure(struct sk_buff
*skb
, struct inet6_ifaddr
*ifp
)
2070 struct inet6_dev
*idev
= ifp
->idev
;
2071 struct net
*net
= dev_net(ifp
->idev
->dev
);
2073 if (addrconf_dad_end(ifp
)) {
2078 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2079 ifp
->idev
->dev
->name
, &ifp
->addr
, eth_hdr(skb
)->h_source
);
2081 spin_lock_bh(&ifp
->lock
);
2083 if (ifp
->flags
& IFA_F_STABLE_PRIVACY
) {
2084 struct in6_addr new_addr
;
2085 struct inet6_ifaddr
*ifp2
;
2086 int retries
= ifp
->stable_privacy_retry
+ 1;
2087 struct ifa6_config cfg
= {
2089 .plen
= ifp
->prefix_len
,
2090 .ifa_flags
= ifp
->flags
,
2091 .valid_lft
= ifp
->valid_lft
,
2092 .preferred_lft
= ifp
->prefered_lft
,
2093 .scope
= ifp
->scope
,
2096 if (retries
> net
->ipv6
.sysctl
.idgen_retries
) {
2097 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2098 ifp
->idev
->dev
->name
);
2102 new_addr
= ifp
->addr
;
2103 if (ipv6_generate_stable_address(&new_addr
, retries
,
2107 spin_unlock_bh(&ifp
->lock
);
2109 if (idev
->cnf
.max_addresses
&&
2110 ipv6_count_addresses(idev
) >=
2111 idev
->cnf
.max_addresses
)
2114 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2115 ifp
->idev
->dev
->name
);
2117 ifp2
= ipv6_add_addr(idev
, &cfg
, false, NULL
);
2121 spin_lock_bh(&ifp2
->lock
);
2122 ifp2
->stable_privacy_retry
= retries
;
2123 ifp2
->state
= INET6_IFADDR_STATE_PREDAD
;
2124 spin_unlock_bh(&ifp2
->lock
);
2126 addrconf_mod_dad_work(ifp2
, net
->ipv6
.sysctl
.idgen_delay
);
2129 spin_lock_bh(&ifp
->lock
);
2133 /* transition from _POSTDAD to _ERRDAD */
2134 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
2135 spin_unlock_bh(&ifp
->lock
);
2137 addrconf_mod_dad_work(ifp
, 0);
2141 /* Join to solicited addr multicast group.
2142 * caller must hold RTNL */
2143 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
2145 struct in6_addr maddr
;
2147 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2150 addrconf_addr_solict_mult(addr
, &maddr
);
2151 ipv6_dev_mc_inc(dev
, &maddr
);
2154 /* caller must hold RTNL */
2155 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2157 struct in6_addr maddr
;
2159 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2162 addrconf_addr_solict_mult(addr
, &maddr
);
2163 __ipv6_dev_mc_dec(idev
, &maddr
);
2166 /* caller must hold RTNL */
2167 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
2169 struct in6_addr addr
;
2171 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2173 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2174 if (ipv6_addr_any(&addr
))
2176 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
2179 /* caller must hold RTNL */
2180 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
2182 struct in6_addr addr
;
2184 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2186 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2187 if (ipv6_addr_any(&addr
))
2189 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
2192 static int addrconf_ifid_6lowpan(u8
*eui
, struct net_device
*dev
)
2194 switch (dev
->addr_len
) {
2196 memcpy(eui
, dev
->dev_addr
, 3);
2199 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
2201 case EUI64_ADDR_LEN
:
2202 memcpy(eui
, dev
->dev_addr
, EUI64_ADDR_LEN
);
2212 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
2214 union fwnet_hwaddr
*ha
;
2216 if (dev
->addr_len
!= FWNET_ALEN
)
2219 ha
= (union fwnet_hwaddr
*)dev
->dev_addr
;
2221 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
2226 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
2228 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2229 if (dev
->addr_len
!= ARCNET_ALEN
)
2232 eui
[7] = *(u8
*)dev
->dev_addr
;
2236 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
2238 if (dev
->addr_len
!= INFINIBAND_ALEN
)
2240 memcpy(eui
, dev
->dev_addr
+ 12, 8);
2245 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
2249 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
2250 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
2251 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
2252 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
2253 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
2254 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
2258 memcpy(eui
+ 4, &addr
, 4);
2262 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
2264 if (dev
->priv_flags
& IFF_ISATAP
)
2265 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2269 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
2271 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2274 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
2276 memcpy(eui
, dev
->perm_addr
, 3);
2277 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
2284 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
2286 switch (dev
->type
) {
2289 return addrconf_ifid_eui48(eui
, dev
);
2291 return addrconf_ifid_arcnet(eui
, dev
);
2292 case ARPHRD_INFINIBAND
:
2293 return addrconf_ifid_infiniband(eui
, dev
);
2295 return addrconf_ifid_sit(eui
, dev
);
2298 return addrconf_ifid_gre(eui
, dev
);
2299 case ARPHRD_6LOWPAN
:
2300 return addrconf_ifid_6lowpan(eui
, dev
);
2301 case ARPHRD_IEEE1394
:
2302 return addrconf_ifid_ieee1394(eui
, dev
);
2303 case ARPHRD_TUNNEL6
:
2306 return addrconf_ifid_ip6tnl(eui
, dev
);
2311 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
2314 struct inet6_ifaddr
*ifp
;
2316 read_lock_bh(&idev
->lock
);
2317 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
2318 if (ifp
->scope
> IFA_LINK
)
2320 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2321 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
2326 read_unlock_bh(&idev
->lock
);
2330 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2331 static void ipv6_regen_rndid(struct inet6_dev
*idev
)
2334 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
2335 idev
->rndid
[0] &= ~0x02;
2338 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2339 * check if generated address is not inappropriate
2341 * - Reserved subnet anycast (RFC 2526)
2342 * 11111101 11....11 1xxxxxxx
2343 * - ISATAP (RFC4214) 6.1
2344 * 00-00-5E-FE-xx-xx-xx-xx
2346 * - XXX: already assigned to an address on the device
2348 if (idev
->rndid
[0] == 0xfd &&
2349 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
2350 (idev
->rndid
[7]&0x80))
2352 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
2353 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
2355 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
2360 static void ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
2362 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
2363 ipv6_regen_rndid(idev
);
2371 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, u32 metric
,
2372 struct net_device
*dev
, unsigned long expires
,
2373 u32 flags
, gfp_t gfp_flags
)
2375 struct fib6_config cfg
= {
2376 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
,
2377 .fc_metric
= metric
? : IP6_RT_PRIO_ADDRCONF
,
2378 .fc_ifindex
= dev
->ifindex
,
2379 .fc_expires
= expires
,
2381 .fc_flags
= RTF_UP
| flags
,
2382 .fc_nlinfo
.nl_net
= dev_net(dev
),
2383 .fc_protocol
= RTPROT_KERNEL
,
2384 .fc_type
= RTN_UNICAST
,
2389 /* Prevent useless cloning on PtP SIT.
2390 This thing is done here expecting that the whole
2391 class of non-broadcast devices need not cloning.
2393 #if IS_ENABLED(CONFIG_IPV6_SIT)
2394 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2395 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2398 ip6_route_add(&cfg
, gfp_flags
, NULL
);
2402 static struct fib6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2404 const struct net_device
*dev
,
2405 u32 flags
, u32 noflags
)
2407 struct fib6_node
*fn
;
2408 struct fib6_info
*rt
= NULL
;
2409 struct fib6_table
*table
;
2410 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
;
2412 table
= fib6_get_table(dev_net(dev
), tb_id
);
2417 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0, true);
2421 for_each_fib6_node_rt_rcu(fn
) {
2422 if (rt
->fib6_nh
.nh_dev
->ifindex
!= dev
->ifindex
)
2424 if ((rt
->fib6_flags
& flags
) != flags
)
2426 if ((rt
->fib6_flags
& noflags
) != 0)
2428 if (!fib6_info_hold_safe(rt
))
2438 /* Create "default" multicast route to the interface */
2440 static void addrconf_add_mroute(struct net_device
*dev
)
2442 struct fib6_config cfg
= {
2443 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_LOCAL
,
2444 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2445 .fc_ifindex
= dev
->ifindex
,
2448 .fc_type
= RTN_UNICAST
,
2449 .fc_nlinfo
.nl_net
= dev_net(dev
),
2452 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2454 ip6_route_add(&cfg
, GFP_KERNEL
, NULL
);
2457 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2459 struct inet6_dev
*idev
;
2463 idev
= ipv6_find_idev(dev
);
2465 return ERR_PTR(-ENOBUFS
);
2467 if (idev
->cnf
.disable_ipv6
)
2468 return ERR_PTR(-EACCES
);
2470 /* Add default multicast route */
2471 if (!(dev
->flags
& IFF_LOOPBACK
) && !netif_is_l3_master(dev
))
2472 addrconf_add_mroute(dev
);
2477 static void manage_tempaddrs(struct inet6_dev
*idev
,
2478 struct inet6_ifaddr
*ifp
,
2479 __u32 valid_lft
, __u32 prefered_lft
,
2480 bool create
, unsigned long now
)
2483 struct inet6_ifaddr
*ift
;
2485 read_lock_bh(&idev
->lock
);
2486 /* update all temporary addresses in the list */
2487 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2488 int age
, max_valid
, max_prefered
;
2490 if (ifp
!= ift
->ifpub
)
2493 /* RFC 4941 section 3.3:
2494 * If a received option will extend the lifetime of a public
2495 * address, the lifetimes of temporary addresses should
2496 * be extended, subject to the overall constraint that no
2497 * temporary addresses should ever remain "valid" or "preferred"
2498 * for a time longer than (TEMP_VALID_LIFETIME) or
2499 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2501 age
= (now
- ift
->cstamp
) / HZ
;
2502 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2506 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2507 idev
->desync_factor
- age
;
2508 if (max_prefered
< 0)
2511 if (valid_lft
> max_valid
)
2512 valid_lft
= max_valid
;
2514 if (prefered_lft
> max_prefered
)
2515 prefered_lft
= max_prefered
;
2517 spin_lock(&ift
->lock
);
2519 ift
->valid_lft
= valid_lft
;
2520 ift
->prefered_lft
= prefered_lft
;
2522 if (prefered_lft
> 0)
2523 ift
->flags
&= ~IFA_F_DEPRECATED
;
2525 spin_unlock(&ift
->lock
);
2526 if (!(flags
&IFA_F_TENTATIVE
))
2527 ipv6_ifa_notify(0, ift
);
2530 if ((create
|| list_empty(&idev
->tempaddr_list
)) &&
2531 idev
->cnf
.use_tempaddr
> 0) {
2532 /* When a new public address is created as described
2533 * in [ADDRCONF], also create a new temporary address.
2534 * Also create a temporary address if it's enabled but
2535 * no temporary address currently exists.
2537 read_unlock_bh(&idev
->lock
);
2538 ipv6_create_tempaddr(ifp
, NULL
, false);
2540 read_unlock_bh(&idev
->lock
);
2544 static bool is_addr_mode_generate_stable(struct inet6_dev
*idev
)
2546 return idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
||
2547 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_RANDOM
;
2550 int addrconf_prefix_rcv_add_addr(struct net
*net
, struct net_device
*dev
,
2551 const struct prefix_info
*pinfo
,
2552 struct inet6_dev
*in6_dev
,
2553 const struct in6_addr
*addr
, int addr_type
,
2554 u32 addr_flags
, bool sllao
, bool tokenized
,
2555 __u32 valid_lft
, u32 prefered_lft
)
2557 struct inet6_ifaddr
*ifp
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
2558 int create
= 0, update_lft
= 0;
2560 if (!ifp
&& valid_lft
) {
2561 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2562 struct ifa6_config cfg
= {
2564 .plen
= pinfo
->prefix_len
,
2565 .ifa_flags
= addr_flags
,
2566 .valid_lft
= valid_lft
,
2567 .preferred_lft
= prefered_lft
,
2568 .scope
= addr_type
& IPV6_ADDR_SCOPE_MASK
,
2571 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2572 if ((net
->ipv6
.devconf_all
->optimistic_dad
||
2573 in6_dev
->cnf
.optimistic_dad
) &&
2574 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2575 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
2578 /* Do not allow to create too much of autoconfigured
2579 * addresses; this would be too easy way to crash kernel.
2581 if (!max_addresses
||
2582 ipv6_count_addresses(in6_dev
) < max_addresses
)
2583 ifp
= ipv6_add_addr(in6_dev
, &cfg
, false, NULL
);
2585 if (IS_ERR_OR_NULL(ifp
))
2589 spin_lock_bh(&ifp
->lock
);
2590 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2591 ifp
->cstamp
= jiffies
;
2592 ifp
->tokenized
= tokenized
;
2593 spin_unlock_bh(&ifp
->lock
);
2594 addrconf_dad_start(ifp
);
2602 /* update lifetime (RFC2462 5.5.3 e) */
2603 spin_lock_bh(&ifp
->lock
);
2605 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2606 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2609 if (!create
&& stored_lft
) {
2610 const u32 minimum_lft
= min_t(u32
,
2611 stored_lft
, MIN_VALID_LIFETIME
);
2612 valid_lft
= max(valid_lft
, minimum_lft
);
2614 /* RFC4862 Section 5.5.3e:
2615 * "Note that the preferred lifetime of the
2616 * corresponding address is always reset to
2617 * the Preferred Lifetime in the received
2618 * Prefix Information option, regardless of
2619 * whether the valid lifetime is also reset or
2622 * So we should always update prefered_lft here.
2628 ifp
->valid_lft
= valid_lft
;
2629 ifp
->prefered_lft
= prefered_lft
;
2632 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2633 spin_unlock_bh(&ifp
->lock
);
2635 if (!(flags
&IFA_F_TENTATIVE
))
2636 ipv6_ifa_notify(0, ifp
);
2638 spin_unlock_bh(&ifp
->lock
);
2640 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2649 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr
);
2651 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2653 struct prefix_info
*pinfo
;
2658 struct inet6_dev
*in6_dev
;
2659 struct net
*net
= dev_net(dev
);
2661 pinfo
= (struct prefix_info
*) opt
;
2663 if (len
< sizeof(struct prefix_info
)) {
2664 netdev_dbg(dev
, "addrconf: prefix option too short\n");
2669 * Validation checks ([ADDRCONF], page 19)
2672 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2674 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2677 valid_lft
= ntohl(pinfo
->valid
);
2678 prefered_lft
= ntohl(pinfo
->prefered
);
2680 if (prefered_lft
> valid_lft
) {
2681 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2685 in6_dev
= in6_dev_get(dev
);
2688 net_dbg_ratelimited("addrconf: device %s not configured\n",
2694 * Two things going on here:
2695 * 1) Add routes for on-link prefixes
2696 * 2) Configure prefixes with the auto flag set
2699 if (pinfo
->onlink
) {
2700 struct fib6_info
*rt
;
2701 unsigned long rt_expires
;
2703 /* Avoid arithmetic overflow. Really, we could
2704 * save rt_expires in seconds, likely valid_lft,
2705 * but it would require division in fib gc, that it
2709 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2711 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2713 if (addrconf_finite_timeout(rt_expires
))
2716 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2719 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2720 RTF_GATEWAY
| RTF_DEFAULT
);
2723 /* Autoconf prefix route */
2724 if (valid_lft
== 0) {
2725 ip6_del_rt(net
, rt
);
2727 } else if (addrconf_finite_timeout(rt_expires
)) {
2729 fib6_set_expires(rt
, jiffies
+ rt_expires
);
2731 fib6_clean_expires(rt
);
2733 } else if (valid_lft
) {
2734 clock_t expires
= 0;
2735 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2736 if (addrconf_finite_timeout(rt_expires
)) {
2738 flags
|= RTF_EXPIRES
;
2739 expires
= jiffies_to_clock_t(rt_expires
);
2741 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2742 0, dev
, expires
, flags
,
2745 fib6_info_release(rt
);
2748 /* Try to figure out our local address for this prefix */
2750 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2751 struct in6_addr addr
;
2752 bool tokenized
= false, dev_addr_generated
= false;
2754 if (pinfo
->prefix_len
== 64) {
2755 memcpy(&addr
, &pinfo
->prefix
, 8);
2757 if (!ipv6_addr_any(&in6_dev
->token
)) {
2758 read_lock_bh(&in6_dev
->lock
);
2759 memcpy(addr
.s6_addr
+ 8,
2760 in6_dev
->token
.s6_addr
+ 8, 8);
2761 read_unlock_bh(&in6_dev
->lock
);
2763 } else if (is_addr_mode_generate_stable(in6_dev
) &&
2764 !ipv6_generate_stable_address(&addr
, 0,
2766 addr_flags
|= IFA_F_STABLE_PRIVACY
;
2768 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2769 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2772 dev_addr_generated
= true;
2776 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2781 err
= addrconf_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
2784 tokenized
, valid_lft
,
2789 /* Ignore error case here because previous prefix add addr was
2790 * successful which will be notified.
2792 ndisc_ops_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
, &addr
,
2793 addr_type
, addr_flags
, sllao
,
2794 tokenized
, valid_lft
,
2796 dev_addr_generated
);
2798 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2800 in6_dev_put(in6_dev
);
2804 * Set destination address.
2805 * Special case for SIT interfaces where we create a new "virtual"
2808 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2810 struct in6_ifreq ireq
;
2811 struct net_device
*dev
;
2817 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2820 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2826 #if IS_ENABLED(CONFIG_IPV6_SIT)
2827 if (dev
->type
== ARPHRD_SIT
) {
2828 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2830 struct ip_tunnel_parm p
;
2832 err
= -EADDRNOTAVAIL
;
2833 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2836 memset(&p
, 0, sizeof(p
));
2837 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2841 p
.iph
.protocol
= IPPROTO_IPV6
;
2843 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2845 if (ops
->ndo_do_ioctl
) {
2846 mm_segment_t oldfs
= get_fs();
2849 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2856 dev
= __dev_get_by_name(net
, p
.name
);
2859 err
= dev_open(dev
, NULL
);
2869 static int ipv6_mc_config(struct sock
*sk
, bool join
,
2870 const struct in6_addr
*addr
, int ifindex
)
2878 ret
= ipv6_sock_mc_join(sk
, ifindex
, addr
);
2880 ret
= ipv6_sock_mc_drop(sk
, ifindex
, addr
);
2887 * Manual configuration of address on an interface
2889 static int inet6_addr_add(struct net
*net
, int ifindex
,
2890 struct ifa6_config
*cfg
,
2891 struct netlink_ext_ack
*extack
)
2893 struct inet6_ifaddr
*ifp
;
2894 struct inet6_dev
*idev
;
2895 struct net_device
*dev
;
2896 unsigned long timeout
;
2902 if (cfg
->plen
> 128)
2905 /* check the lifetime */
2906 if (!cfg
->valid_lft
|| cfg
->preferred_lft
> cfg
->valid_lft
)
2909 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
&& cfg
->plen
!= 64)
2912 dev
= __dev_get_by_index(net
, ifindex
);
2916 idev
= addrconf_add_dev(dev
);
2918 return PTR_ERR(idev
);
2920 if (cfg
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
2921 int ret
= ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2922 true, cfg
->pfx
, ifindex
);
2928 cfg
->scope
= ipv6_addr_scope(cfg
->pfx
);
2930 timeout
= addrconf_timeout_fixup(cfg
->valid_lft
, HZ
);
2931 if (addrconf_finite_timeout(timeout
)) {
2932 expires
= jiffies_to_clock_t(timeout
* HZ
);
2933 cfg
->valid_lft
= timeout
;
2934 flags
= RTF_EXPIRES
;
2938 cfg
->ifa_flags
|= IFA_F_PERMANENT
;
2941 timeout
= addrconf_timeout_fixup(cfg
->preferred_lft
, HZ
);
2942 if (addrconf_finite_timeout(timeout
)) {
2944 cfg
->ifa_flags
|= IFA_F_DEPRECATED
;
2945 cfg
->preferred_lft
= timeout
;
2948 ifp
= ipv6_add_addr(idev
, cfg
, true, extack
);
2950 if (!(cfg
->ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
2951 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
2952 ifp
->rt_priority
, dev
, expires
,
2956 /* Send a netlink notification if DAD is enabled and
2957 * optimistic flag is not set
2959 if (!(ifp
->flags
& (IFA_F_OPTIMISTIC
| IFA_F_NODAD
)))
2960 ipv6_ifa_notify(0, ifp
);
2962 * Note that section 3.1 of RFC 4429 indicates
2963 * that the Optimistic flag should not be set for
2964 * manually configured addresses
2966 addrconf_dad_start(ifp
);
2967 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
)
2968 manage_tempaddrs(idev
, ifp
, cfg
->valid_lft
,
2969 cfg
->preferred_lft
, true, jiffies
);
2971 addrconf_verify_rtnl();
2973 } else if (cfg
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
2974 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
, false,
2978 return PTR_ERR(ifp
);
2981 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
2982 const struct in6_addr
*pfx
, unsigned int plen
)
2984 struct inet6_ifaddr
*ifp
;
2985 struct inet6_dev
*idev
;
2986 struct net_device
*dev
;
2991 dev
= __dev_get_by_index(net
, ifindex
);
2995 idev
= __in6_dev_get(dev
);
2999 read_lock_bh(&idev
->lock
);
3000 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3001 if (ifp
->prefix_len
== plen
&&
3002 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
3004 read_unlock_bh(&idev
->lock
);
3006 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
3007 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
3008 manage_tempaddrs(idev
, ifp
, 0, 0, false,
3011 addrconf_verify_rtnl();
3012 if (ipv6_addr_is_multicast(pfx
)) {
3013 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
3014 false, pfx
, dev
->ifindex
);
3019 read_unlock_bh(&idev
->lock
);
3020 return -EADDRNOTAVAIL
;
3024 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
3026 struct ifa6_config cfg
= {
3027 .ifa_flags
= IFA_F_PERMANENT
,
3028 .preferred_lft
= INFINITY_LIFE_TIME
,
3029 .valid_lft
= INFINITY_LIFE_TIME
,
3031 struct in6_ifreq ireq
;
3034 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
3037 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
3040 cfg
.pfx
= &ireq
.ifr6_addr
;
3041 cfg
.plen
= ireq
.ifr6_prefixlen
;
3044 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &cfg
, NULL
);
3049 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
3051 struct in6_ifreq ireq
;
3054 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
3057 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
3061 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
3062 ireq
.ifr6_prefixlen
);
3067 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
3068 int plen
, int scope
)
3070 struct inet6_ifaddr
*ifp
;
3071 struct ifa6_config cfg
= {
3074 .ifa_flags
= IFA_F_PERMANENT
,
3075 .valid_lft
= INFINITY_LIFE_TIME
,
3076 .preferred_lft
= INFINITY_LIFE_TIME
,
3080 ifp
= ipv6_add_addr(idev
, &cfg
, true, NULL
);
3082 spin_lock_bh(&ifp
->lock
);
3083 ifp
->flags
&= ~IFA_F_TENTATIVE
;
3084 spin_unlock_bh(&ifp
->lock
);
3085 rt_genid_bump_ipv6(dev_net(idev
->dev
));
3086 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3091 #if IS_ENABLED(CONFIG_IPV6_SIT)
3092 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
3094 struct in6_addr addr
;
3095 struct net_device
*dev
;
3096 struct net
*net
= dev_net(idev
->dev
);
3102 memset(&addr
, 0, sizeof(struct in6_addr
));
3103 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
3105 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
3106 addr
.s6_addr32
[0] = htonl(0xfe800000);
3110 scope
= IPV6_ADDR_COMPATv4
;
3112 pflags
|= RTF_NONEXTHOP
;
3115 if (addr
.s6_addr32
[3]) {
3116 add_addr(idev
, &addr
, plen
, scope
);
3117 addrconf_prefix_route(&addr
, plen
, 0, idev
->dev
, 0, pflags
,
3122 for_each_netdev(net
, dev
) {
3123 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
3124 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
3125 struct in_ifaddr
*ifa
;
3129 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
3131 addr
.s6_addr32
[3] = ifa
->ifa_local
;
3133 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
3135 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
3136 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
3141 add_addr(idev
, &addr
, plen
, flag
);
3142 addrconf_prefix_route(&addr
, plen
, 0, idev
->dev
,
3143 0, pflags
, GFP_KERNEL
);
3150 static void init_loopback(struct net_device
*dev
)
3152 struct inet6_dev
*idev
;
3158 idev
= ipv6_find_idev(dev
);
3160 pr_debug("%s: add_dev failed\n", __func__
);
3164 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
3167 void addrconf_add_linklocal(struct inet6_dev
*idev
,
3168 const struct in6_addr
*addr
, u32 flags
)
3170 struct ifa6_config cfg
= {
3173 .ifa_flags
= flags
| IFA_F_PERMANENT
,
3174 .valid_lft
= INFINITY_LIFE_TIME
,
3175 .preferred_lft
= INFINITY_LIFE_TIME
,
3178 struct inet6_ifaddr
*ifp
;
3180 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3181 if ((dev_net(idev
->dev
)->ipv6
.devconf_all
->optimistic_dad
||
3182 idev
->cnf
.optimistic_dad
) &&
3183 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
3184 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
3187 ifp
= ipv6_add_addr(idev
, &cfg
, true, NULL
);
3189 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, 0, idev
->dev
,
3191 addrconf_dad_start(ifp
);
3195 EXPORT_SYMBOL_GPL(addrconf_add_linklocal
);
3197 static bool ipv6_reserved_interfaceid(struct in6_addr address
)
3199 if ((address
.s6_addr32
[2] | address
.s6_addr32
[3]) == 0)
3202 if (address
.s6_addr32
[2] == htonl(0x02005eff) &&
3203 ((address
.s6_addr32
[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3206 if (address
.s6_addr32
[2] == htonl(0xfdffffff) &&
3207 ((address
.s6_addr32
[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3213 static int ipv6_generate_stable_address(struct in6_addr
*address
,
3215 const struct inet6_dev
*idev
)
3217 static DEFINE_SPINLOCK(lock
);
3218 static __u32 digest
[SHA_DIGEST_WORDS
];
3219 static __u32 workspace
[SHA_WORKSPACE_WORDS
];
3222 char __data
[SHA_MESSAGE_BYTES
];
3224 struct in6_addr secret
;
3226 unsigned char hwaddr
[MAX_ADDR_LEN
];
3231 struct in6_addr secret
;
3232 struct in6_addr temp
;
3233 struct net
*net
= dev_net(idev
->dev
);
3235 BUILD_BUG_ON(sizeof(data
.__data
) != sizeof(data
));
3237 if (idev
->cnf
.stable_secret
.initialized
)
3238 secret
= idev
->cnf
.stable_secret
.secret
;
3239 else if (net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
3240 secret
= net
->ipv6
.devconf_dflt
->stable_secret
.secret
;
3245 spin_lock_bh(&lock
);
3248 memset(&data
, 0, sizeof(data
));
3249 memset(workspace
, 0, sizeof(workspace
));
3250 memcpy(data
.hwaddr
, idev
->dev
->perm_addr
, idev
->dev
->addr_len
);
3251 data
.prefix
[0] = address
->s6_addr32
[0];
3252 data
.prefix
[1] = address
->s6_addr32
[1];
3253 data
.secret
= secret
;
3254 data
.dad_count
= dad_count
;
3256 sha_transform(digest
, data
.__data
, workspace
);
3259 temp
.s6_addr32
[2] = (__force __be32
)digest
[0];
3260 temp
.s6_addr32
[3] = (__force __be32
)digest
[1];
3262 spin_unlock_bh(&lock
);
3264 if (ipv6_reserved_interfaceid(temp
)) {
3266 if (dad_count
> dev_net(idev
->dev
)->ipv6
.sysctl
.idgen_retries
)
3275 static void ipv6_gen_mode_random_init(struct inet6_dev
*idev
)
3277 struct ipv6_stable_secret
*s
= &idev
->cnf
.stable_secret
;
3281 s
= &idev
->cnf
.stable_secret
;
3282 get_random_bytes(&s
->secret
, sizeof(s
->secret
));
3283 s
->initialized
= true;
3286 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
3288 struct in6_addr addr
;
3290 /* no link local addresses on L3 master devices */
3291 if (netif_is_l3_master(idev
->dev
))
3294 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
3296 switch (idev
->cnf
.addr_gen_mode
) {
3297 case IN6_ADDR_GEN_MODE_RANDOM
:
3298 ipv6_gen_mode_random_init(idev
);
3300 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
3301 if (!ipv6_generate_stable_address(&addr
, 0, idev
))
3302 addrconf_add_linklocal(idev
, &addr
,
3303 IFA_F_STABLE_PRIVACY
);
3304 else if (prefix_route
)
3305 addrconf_prefix_route(&addr
, 64, 0, idev
->dev
,
3308 case IN6_ADDR_GEN_MODE_EUI64
:
3309 /* addrconf_add_linklocal also adds a prefix_route and we
3310 * only need to care about prefix routes if ipv6_generate_eui64
3311 * couldn't generate one.
3313 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
3314 addrconf_add_linklocal(idev
, &addr
, 0);
3315 else if (prefix_route
)
3316 addrconf_prefix_route(&addr
, 64, 0, idev
->dev
,
3319 case IN6_ADDR_GEN_MODE_NONE
:
3321 /* will not add any link local address */
3326 static void addrconf_dev_config(struct net_device
*dev
)
3328 struct inet6_dev
*idev
;
3332 if ((dev
->type
!= ARPHRD_ETHER
) &&
3333 (dev
->type
!= ARPHRD_FDDI
) &&
3334 (dev
->type
!= ARPHRD_ARCNET
) &&
3335 (dev
->type
!= ARPHRD_INFINIBAND
) &&
3336 (dev
->type
!= ARPHRD_IEEE1394
) &&
3337 (dev
->type
!= ARPHRD_TUNNEL6
) &&
3338 (dev
->type
!= ARPHRD_6LOWPAN
) &&
3339 (dev
->type
!= ARPHRD_IP6GRE
) &&
3340 (dev
->type
!= ARPHRD_IPGRE
) &&
3341 (dev
->type
!= ARPHRD_TUNNEL
) &&
3342 (dev
->type
!= ARPHRD_NONE
) &&
3343 (dev
->type
!= ARPHRD_RAWIP
)) {
3344 /* Alas, we support only Ethernet autoconfiguration. */
3348 idev
= addrconf_add_dev(dev
);
3352 /* this device type has no EUI support */
3353 if (dev
->type
== ARPHRD_NONE
&&
3354 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
)
3355 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_RANDOM
;
3357 addrconf_addr_gen(idev
, false);
3360 #if IS_ENABLED(CONFIG_IPV6_SIT)
3361 static void addrconf_sit_config(struct net_device
*dev
)
3363 struct inet6_dev
*idev
;
3368 * Configure the tunnel with one of our IPv4
3369 * addresses... we should configure all of
3370 * our v4 addrs in the tunnel
3373 idev
= ipv6_find_idev(dev
);
3375 pr_debug("%s: add_dev failed\n", __func__
);
3379 if (dev
->priv_flags
& IFF_ISATAP
) {
3380 addrconf_addr_gen(idev
, false);
3384 sit_add_v4_addrs(idev
);
3386 if (dev
->flags
&IFF_POINTOPOINT
)
3387 addrconf_add_mroute(dev
);
3391 #if IS_ENABLED(CONFIG_NET_IPGRE)
3392 static void addrconf_gre_config(struct net_device
*dev
)
3394 struct inet6_dev
*idev
;
3398 idev
= ipv6_find_idev(dev
);
3400 pr_debug("%s: add_dev failed\n", __func__
);
3404 addrconf_addr_gen(idev
, true);
3405 if (dev
->flags
& IFF_POINTOPOINT
)
3406 addrconf_add_mroute(dev
);
3410 static int fixup_permanent_addr(struct net
*net
,
3411 struct inet6_dev
*idev
,
3412 struct inet6_ifaddr
*ifp
)
3414 /* !fib6_node means the host route was removed from the
3415 * FIB, for example, if 'lo' device is taken down. In that
3416 * case regenerate the host route.
3418 if (!ifp
->rt
|| !ifp
->rt
->fib6_node
) {
3419 struct fib6_info
*f6i
, *prev
;
3421 f6i
= addrconf_f6i_alloc(net
, idev
, &ifp
->addr
, false,
3424 return PTR_ERR(f6i
);
3426 /* ifp->rt can be accessed outside of rtnl */
3427 spin_lock(&ifp
->lock
);
3430 spin_unlock(&ifp
->lock
);
3432 fib6_info_release(prev
);
3435 if (!(ifp
->flags
& IFA_F_NOPREFIXROUTE
)) {
3436 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
3437 ifp
->rt_priority
, idev
->dev
, 0, 0,
3441 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
)
3442 addrconf_dad_start(ifp
);
3447 static void addrconf_permanent_addr(struct net
*net
, struct net_device
*dev
)
3449 struct inet6_ifaddr
*ifp
, *tmp
;
3450 struct inet6_dev
*idev
;
3452 idev
= __in6_dev_get(dev
);
3456 write_lock_bh(&idev
->lock
);
3458 list_for_each_entry_safe(ifp
, tmp
, &idev
->addr_list
, if_list
) {
3459 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3460 fixup_permanent_addr(net
, idev
, ifp
) < 0) {
3461 write_unlock_bh(&idev
->lock
);
3464 write_lock_bh(&idev
->lock
);
3466 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3467 idev
->dev
->name
, &ifp
->addr
);
3471 write_unlock_bh(&idev
->lock
);
3474 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
3477 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3478 struct netdev_notifier_change_info
*change_info
;
3479 struct netdev_notifier_changeupper_info
*info
;
3480 struct inet6_dev
*idev
= __in6_dev_get(dev
);
3481 struct net
*net
= dev_net(dev
);
3482 int run_pending
= 0;
3486 case NETDEV_REGISTER
:
3487 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3488 idev
= ipv6_add_dev(dev
);
3490 return notifier_from_errno(PTR_ERR(idev
));
3494 case NETDEV_CHANGEMTU
:
3495 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3496 if (dev
->mtu
< IPV6_MIN_MTU
) {
3497 addrconf_ifdown(dev
, dev
!= net
->loopback_dev
);
3502 rt6_mtu_change(dev
, dev
->mtu
);
3503 idev
->cnf
.mtu6
= dev
->mtu
;
3507 /* allocate new idev */
3508 idev
= ipv6_add_dev(dev
);
3512 /* device is still not ready */
3513 if (!(idev
->if_flags
& IF_READY
))
3522 if (dev
->flags
& IFF_SLAVE
)
3525 if (idev
&& idev
->cnf
.disable_ipv6
)
3528 if (event
== NETDEV_UP
) {
3529 /* restore routes for permanent addresses */
3530 addrconf_permanent_addr(net
, dev
);
3532 if (!addrconf_link_ready(dev
)) {
3533 /* device is not ready yet. */
3534 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3539 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
3540 idev
= ipv6_add_dev(dev
);
3542 if (!IS_ERR_OR_NULL(idev
)) {
3543 idev
->if_flags
|= IF_READY
;
3546 } else if (event
== NETDEV_CHANGE
) {
3547 if (!addrconf_link_ready(dev
)) {
3548 /* device is still not ready. */
3549 rt6_sync_down_dev(dev
, event
);
3553 if (!IS_ERR_OR_NULL(idev
)) {
3554 if (idev
->if_flags
& IF_READY
) {
3555 /* device is already configured -
3556 * but resend MLD reports, we might
3557 * have roamed and need to update
3558 * multicast snooping switches
3562 if (change_info
->flags_changed
& IFF_NOARP
)
3563 addrconf_dad_run(idev
, true);
3564 rt6_sync_up(dev
, RTNH_F_LINKDOWN
);
3567 idev
->if_flags
|= IF_READY
;
3570 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3576 switch (dev
->type
) {
3577 #if IS_ENABLED(CONFIG_IPV6_SIT)
3579 addrconf_sit_config(dev
);
3582 #if IS_ENABLED(CONFIG_NET_IPGRE)
3584 addrconf_gre_config(dev
);
3587 case ARPHRD_LOOPBACK
:
3592 addrconf_dev_config(dev
);
3596 if (!IS_ERR_OR_NULL(idev
)) {
3598 addrconf_dad_run(idev
, false);
3600 /* Device has an address by now */
3601 rt6_sync_up(dev
, RTNH_F_DEAD
);
3604 * If the MTU changed during the interface down,
3605 * when the interface up, the changed MTU must be
3606 * reflected in the idev as well as routers.
3608 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
3609 dev
->mtu
>= IPV6_MIN_MTU
) {
3610 rt6_mtu_change(dev
, dev
->mtu
);
3611 idev
->cnf
.mtu6
= dev
->mtu
;
3613 idev
->tstamp
= jiffies
;
3614 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
3617 * If the changed mtu during down is lower than
3618 * IPV6_MIN_MTU stop IPv6 on this interface.
3620 if (dev
->mtu
< IPV6_MIN_MTU
)
3621 addrconf_ifdown(dev
, dev
!= net
->loopback_dev
);
3626 case NETDEV_UNREGISTER
:
3628 * Remove all addresses from this interface.
3630 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3633 case NETDEV_CHANGENAME
:
3635 snmp6_unregister_dev(idev
);
3636 addrconf_sysctl_unregister(idev
);
3637 err
= addrconf_sysctl_register(idev
);
3639 return notifier_from_errno(err
);
3640 err
= snmp6_register_dev(idev
);
3642 addrconf_sysctl_unregister(idev
);
3643 return notifier_from_errno(err
);
3648 case NETDEV_PRE_TYPE_CHANGE
:
3649 case NETDEV_POST_TYPE_CHANGE
:
3651 addrconf_type_change(dev
, event
);
3654 case NETDEV_CHANGEUPPER
:
3657 /* flush all routes if dev is linked to or unlinked from
3658 * an L3 master device (e.g., VRF)
3660 if (info
->upper_dev
&& netif_is_l3_master(info
->upper_dev
))
3661 addrconf_ifdown(dev
, 0);
3668 * addrconf module should be notified of a device going up
3670 static struct notifier_block ipv6_dev_notf
= {
3671 .notifier_call
= addrconf_notify
,
3672 .priority
= ADDRCONF_NOTIFY_PRIORITY
,
3675 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3677 struct inet6_dev
*idev
;
3680 idev
= __in6_dev_get(dev
);
3682 if (event
== NETDEV_POST_TYPE_CHANGE
)
3683 ipv6_mc_remap(idev
);
3684 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3685 ipv6_mc_unmap(idev
);
3688 static bool addr_is_local(const struct in6_addr
*addr
)
3690 return ipv6_addr_type(addr
) &
3691 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
);
3694 static int addrconf_ifdown(struct net_device
*dev
, int how
)
3696 unsigned long event
= how
? NETDEV_UNREGISTER
: NETDEV_DOWN
;
3697 struct net
*net
= dev_net(dev
);
3698 struct inet6_dev
*idev
;
3699 struct inet6_ifaddr
*ifa
, *tmp
;
3700 bool keep_addr
= false;
3705 rt6_disable_ip(dev
, event
);
3707 idev
= __in6_dev_get(dev
);
3712 * Step 1: remove reference to ipv6 device from parent device.
3718 /* protected by rtnl_lock */
3719 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3721 /* Step 1.5: remove snmp6 entry */
3722 snmp6_unregister_dev(idev
);
3726 /* combine the user config with event to determine if permanent
3727 * addresses are to be removed from address hash table
3729 if (!how
&& !idev
->cnf
.disable_ipv6
) {
3730 /* aggregate the system setting and interface setting */
3731 int _keep_addr
= net
->ipv6
.devconf_all
->keep_addr_on_down
;
3734 _keep_addr
= idev
->cnf
.keep_addr_on_down
;
3736 keep_addr
= (_keep_addr
> 0);
3739 /* Step 2: clear hash table */
3740 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3741 struct hlist_head
*h
= &inet6_addr_lst
[i
];
3743 spin_lock_bh(&addrconf_hash_lock
);
3745 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3746 if (ifa
->idev
== idev
) {
3747 addrconf_del_dad_work(ifa
);
3748 /* combined flag + permanent flag decide if
3749 * address is retained on a down event
3752 !(ifa
->flags
& IFA_F_PERMANENT
) ||
3753 addr_is_local(&ifa
->addr
)) {
3754 hlist_del_init_rcu(&ifa
->addr_lst
);
3759 spin_unlock_bh(&addrconf_hash_lock
);
3762 write_lock_bh(&idev
->lock
);
3764 addrconf_del_rs_timer(idev
);
3766 /* Step 2: clear flags for stateless addrconf */
3768 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3770 /* Step 3: clear tempaddr list */
3771 while (!list_empty(&idev
->tempaddr_list
)) {
3772 ifa
= list_first_entry(&idev
->tempaddr_list
,
3773 struct inet6_ifaddr
, tmp_list
);
3774 list_del(&ifa
->tmp_list
);
3775 write_unlock_bh(&idev
->lock
);
3776 spin_lock_bh(&ifa
->lock
);
3779 in6_ifa_put(ifa
->ifpub
);
3782 spin_unlock_bh(&ifa
->lock
);
3784 write_lock_bh(&idev
->lock
);
3787 list_for_each_entry_safe(ifa
, tmp
, &idev
->addr_list
, if_list
) {
3788 struct fib6_info
*rt
= NULL
;
3791 addrconf_del_dad_work(ifa
);
3793 keep
= keep_addr
&& (ifa
->flags
& IFA_F_PERMANENT
) &&
3794 !addr_is_local(&ifa
->addr
);
3796 write_unlock_bh(&idev
->lock
);
3797 spin_lock_bh(&ifa
->lock
);
3800 /* set state to skip the notifier below */
3801 state
= INET6_IFADDR_STATE_DEAD
;
3802 ifa
->state
= INET6_IFADDR_STATE_PREDAD
;
3803 if (!(ifa
->flags
& IFA_F_NODAD
))
3804 ifa
->flags
|= IFA_F_TENTATIVE
;
3810 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3813 spin_unlock_bh(&ifa
->lock
);
3816 ip6_del_rt(net
, rt
);
3818 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3819 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3820 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3822 if (idev
->cnf
.forwarding
)
3823 addrconf_leave_anycast(ifa
);
3824 addrconf_leave_solict(ifa
->idev
, &ifa
->addr
);
3827 write_lock_bh(&idev
->lock
);
3829 list_del_rcu(&ifa
->if_list
);
3834 write_unlock_bh(&idev
->lock
);
3836 /* Step 5: Discard anycast and multicast list */
3838 ipv6_ac_destroy_dev(idev
);
3839 ipv6_mc_destroy_dev(idev
);
3844 idev
->tstamp
= jiffies
;
3846 /* Last: Shot the device (if unregistered) */
3848 addrconf_sysctl_unregister(idev
);
3849 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3850 neigh_ifdown(&nd_tbl
, dev
);
3856 static void addrconf_rs_timer(struct timer_list
*t
)
3858 struct inet6_dev
*idev
= from_timer(idev
, t
, rs_timer
);
3859 struct net_device
*dev
= idev
->dev
;
3860 struct in6_addr lladdr
;
3862 write_lock(&idev
->lock
);
3863 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3866 if (!ipv6_accept_ra(idev
))
3869 /* Announcement received after solicitation was sent */
3870 if (idev
->if_flags
& IF_RA_RCVD
)
3873 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
|| idev
->cnf
.rtr_solicits
< 0) {
3874 write_unlock(&idev
->lock
);
3875 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3876 ndisc_send_rs(dev
, &lladdr
,
3877 &in6addr_linklocal_allrouters
);
3881 write_lock(&idev
->lock
);
3882 idev
->rs_interval
= rfc3315_s14_backoff_update(
3883 idev
->rs_interval
, idev
->cnf
.rtr_solicit_max_interval
);
3884 /* The wait after the last probe can be shorter */
3885 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3886 idev
->cnf
.rtr_solicits
) ?
3887 idev
->cnf
.rtr_solicit_delay
:
3891 * Note: we do not support deprecated "all on-link"
3892 * assumption any longer.
3894 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3898 write_unlock(&idev
->lock
);
3904 * Duplicate Address Detection
3906 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3908 unsigned long rand_num
;
3909 struct inet6_dev
*idev
= ifp
->idev
;
3912 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3915 rand_num
= prandom_u32() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3918 if (idev
->cnf
.enhanced_dad
||
3919 dev_net(idev
->dev
)->ipv6
.devconf_all
->enhanced_dad
) {
3921 get_random_bytes(&nonce
, 6);
3924 ifp
->dad_nonce
= nonce
;
3925 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
3926 addrconf_mod_dad_work(ifp
, rand_num
);
3929 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
3931 struct inet6_dev
*idev
= ifp
->idev
;
3932 struct net_device
*dev
= idev
->dev
;
3933 bool bump_id
, notify
= false;
3936 addrconf_join_solict(dev
, &ifp
->addr
);
3938 prandom_seed((__force u32
) ifp
->addr
.s6_addr32
[3]);
3940 read_lock_bh(&idev
->lock
);
3941 spin_lock(&ifp
->lock
);
3942 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3946 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3947 (net
->ipv6
.devconf_all
->accept_dad
< 1 &&
3948 idev
->cnf
.accept_dad
< 1) ||
3949 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3950 ifp
->flags
& IFA_F_NODAD
) {
3951 bool send_na
= false;
3953 if (ifp
->flags
& IFA_F_TENTATIVE
&&
3954 !(ifp
->flags
& IFA_F_OPTIMISTIC
))
3956 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
3957 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3958 spin_unlock(&ifp
->lock
);
3959 read_unlock_bh(&idev
->lock
);
3961 addrconf_dad_completed(ifp
, bump_id
, send_na
);
3965 if (!(idev
->if_flags
& IF_READY
)) {
3966 spin_unlock(&ifp
->lock
);
3967 read_unlock_bh(&idev
->lock
);
3969 * If the device is not ready:
3970 * - keep it tentative if it is a permanent address.
3971 * - otherwise, kill it.
3974 addrconf_dad_stop(ifp
, 0);
3979 * Optimistic nodes can start receiving
3982 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
3983 ip6_ins_rt(net
, ifp
->rt
);
3984 if (ipv6_use_optimistic_addr(net
, idev
)) {
3985 /* Because optimistic nodes can use this address,
3986 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3992 addrconf_dad_kick(ifp
);
3994 spin_unlock(&ifp
->lock
);
3995 read_unlock_bh(&idev
->lock
);
3997 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
4000 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
4002 bool begin_dad
= false;
4004 spin_lock_bh(&ifp
->lock
);
4005 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
4006 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
4009 spin_unlock_bh(&ifp
->lock
);
4012 addrconf_mod_dad_work(ifp
, 0);
4015 static void addrconf_dad_work(struct work_struct
*w
)
4017 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
4018 struct inet6_ifaddr
,
4020 struct inet6_dev
*idev
= ifp
->idev
;
4021 bool bump_id
, disable_ipv6
= false;
4022 struct in6_addr mcaddr
;
4028 } action
= DAD_PROCESS
;
4032 spin_lock_bh(&ifp
->lock
);
4033 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
4035 ifp
->state
= INET6_IFADDR_STATE_DAD
;
4036 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
4038 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
4040 if ((dev_net(idev
->dev
)->ipv6
.devconf_all
->accept_dad
> 1 ||
4041 idev
->cnf
.accept_dad
> 1) &&
4042 !idev
->cnf
.disable_ipv6
&&
4043 !(ifp
->flags
& IFA_F_STABLE_PRIVACY
)) {
4044 struct in6_addr addr
;
4046 addr
.s6_addr32
[0] = htonl(0xfe800000);
4047 addr
.s6_addr32
[1] = 0;
4049 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
4050 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
4051 /* DAD failed for link-local based on MAC */
4052 idev
->cnf
.disable_ipv6
= 1;
4054 pr_info("%s: IPv6 being disabled!\n",
4055 ifp
->idev
->dev
->name
);
4056 disable_ipv6
= true;
4060 spin_unlock_bh(&ifp
->lock
);
4062 if (action
== DAD_BEGIN
) {
4063 addrconf_dad_begin(ifp
);
4065 } else if (action
== DAD_ABORT
) {
4067 addrconf_dad_stop(ifp
, 1);
4069 addrconf_ifdown(idev
->dev
, 0);
4073 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
4076 write_lock_bh(&idev
->lock
);
4077 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
4078 write_unlock_bh(&idev
->lock
);
4082 spin_lock(&ifp
->lock
);
4083 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
4084 spin_unlock(&ifp
->lock
);
4085 write_unlock_bh(&idev
->lock
);
4089 if (ifp
->dad_probes
== 0) {
4090 bool send_na
= false;
4093 * DAD was successful
4096 if (ifp
->flags
& IFA_F_TENTATIVE
&&
4097 !(ifp
->flags
& IFA_F_OPTIMISTIC
))
4099 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
4100 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
4101 spin_unlock(&ifp
->lock
);
4102 write_unlock_bh(&idev
->lock
);
4104 addrconf_dad_completed(ifp
, bump_id
, send_na
);
4110 addrconf_mod_dad_work(ifp
,
4111 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
));
4112 spin_unlock(&ifp
->lock
);
4113 write_unlock_bh(&idev
->lock
);
4115 /* send a neighbour solicitation for our addr */
4116 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
4117 ndisc_send_ns(ifp
->idev
->dev
, &ifp
->addr
, &mcaddr
, &in6addr_any
,
4124 /* ifp->idev must be at least read locked */
4125 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
4127 struct inet6_ifaddr
*ifpiter
;
4128 struct inet6_dev
*idev
= ifp
->idev
;
4130 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
4131 if (ifpiter
->scope
> IFA_LINK
)
4133 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
4134 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
4135 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
4142 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
,
4145 struct net_device
*dev
= ifp
->idev
->dev
;
4146 struct in6_addr lladdr
;
4147 bool send_rs
, send_mld
;
4149 addrconf_del_dad_work(ifp
);
4152 * Configure the address for reception. Now it is valid.
4155 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
4157 /* If added prefix is link local and we are prepared to process
4158 router advertisements, start sending router solicitations.
4161 read_lock_bh(&ifp
->idev
->lock
);
4162 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
4163 send_rs
= send_mld
&&
4164 ipv6_accept_ra(ifp
->idev
) &&
4165 ifp
->idev
->cnf
.rtr_solicits
!= 0 &&
4166 (dev
->flags
&IFF_LOOPBACK
) == 0;
4167 read_unlock_bh(&ifp
->idev
->lock
);
4169 /* While dad is in progress mld report's source address is in6_addrany.
4170 * Resend with proper ll now.
4173 ipv6_mc_dad_complete(ifp
->idev
);
4175 /* send unsolicited NA if enabled */
4177 (ifp
->idev
->cnf
.ndisc_notify
||
4178 dev_net(dev
)->ipv6
.devconf_all
->ndisc_notify
)) {
4179 ndisc_send_na(dev
, &in6addr_linklocal_allnodes
, &ifp
->addr
,
4180 /*router=*/ !!ifp
->idev
->cnf
.forwarding
,
4181 /*solicited=*/ false, /*override=*/ true,
4187 * If a host as already performed a random delay
4188 * [...] as part of DAD [...] there is no need
4189 * to delay again before sending the first RS
4191 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
4193 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
4195 write_lock_bh(&ifp
->idev
->lock
);
4196 spin_lock(&ifp
->lock
);
4197 ifp
->idev
->rs_interval
= rfc3315_s14_backoff_init(
4198 ifp
->idev
->cnf
.rtr_solicit_interval
);
4199 ifp
->idev
->rs_probes
= 1;
4200 ifp
->idev
->if_flags
|= IF_RS_SENT
;
4201 addrconf_mod_rs_timer(ifp
->idev
, ifp
->idev
->rs_interval
);
4202 spin_unlock(&ifp
->lock
);
4203 write_unlock_bh(&ifp
->idev
->lock
);
4207 rt_genid_bump_ipv6(dev_net(dev
));
4209 /* Make sure that a new temporary address will be created
4210 * before this temporary address becomes deprecated.
4212 if (ifp
->flags
& IFA_F_TEMPORARY
)
4213 addrconf_verify_rtnl();
4216 static void addrconf_dad_run(struct inet6_dev
*idev
, bool restart
)
4218 struct inet6_ifaddr
*ifp
;
4220 read_lock_bh(&idev
->lock
);
4221 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4222 spin_lock(&ifp
->lock
);
4223 if ((ifp
->flags
& IFA_F_TENTATIVE
&&
4224 ifp
->state
== INET6_IFADDR_STATE_DAD
) || restart
) {
4226 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
4227 addrconf_dad_kick(ifp
);
4229 spin_unlock(&ifp
->lock
);
4231 read_unlock_bh(&idev
->lock
);
4234 #ifdef CONFIG_PROC_FS
4235 struct if6_iter_state
{
4236 struct seq_net_private p
;
4241 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
4243 struct if6_iter_state
*state
= seq
->private;
4244 struct net
*net
= seq_file_net(seq
);
4245 struct inet6_ifaddr
*ifa
= NULL
;
4248 /* initial bucket if pos is 0 */
4254 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
4255 hlist_for_each_entry_rcu(ifa
, &inet6_addr_lst
[state
->bucket
],
4257 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4259 /* sync with offset */
4260 if (p
< state
->offset
) {
4267 /* prepare for next bucket */
4274 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
4275 struct inet6_ifaddr
*ifa
)
4277 struct if6_iter_state
*state
= seq
->private;
4278 struct net
*net
= seq_file_net(seq
);
4280 hlist_for_each_entry_continue_rcu(ifa
, addr_lst
) {
4281 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4288 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
4289 hlist_for_each_entry_rcu(ifa
,
4290 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
4291 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4300 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4304 return if6_get_first(seq
, *pos
);
4307 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4309 struct inet6_ifaddr
*ifa
;
4311 ifa
= if6_get_next(seq
, v
);
4316 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
4322 static int if6_seq_show(struct seq_file
*seq
, void *v
)
4324 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
4325 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
4327 ifp
->idev
->dev
->ifindex
,
4331 ifp
->idev
->dev
->name
);
4335 static const struct seq_operations if6_seq_ops
= {
4336 .start
= if6_seq_start
,
4337 .next
= if6_seq_next
,
4338 .show
= if6_seq_show
,
4339 .stop
= if6_seq_stop
,
4342 static int __net_init
if6_proc_net_init(struct net
*net
)
4344 if (!proc_create_net("if_inet6", 0444, net
->proc_net
, &if6_seq_ops
,
4345 sizeof(struct if6_iter_state
)))
4350 static void __net_exit
if6_proc_net_exit(struct net
*net
)
4352 remove_proc_entry("if_inet6", net
->proc_net
);
4355 static struct pernet_operations if6_proc_net_ops
= {
4356 .init
= if6_proc_net_init
,
4357 .exit
= if6_proc_net_exit
,
4360 int __init
if6_proc_init(void)
4362 return register_pernet_subsys(&if6_proc_net_ops
);
4365 void if6_proc_exit(void)
4367 unregister_pernet_subsys(&if6_proc_net_ops
);
4369 #endif /* CONFIG_PROC_FS */
4371 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4372 /* Check if address is a home address configured on any interface. */
4373 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
4375 unsigned int hash
= inet6_addr_hash(net
, addr
);
4376 struct inet6_ifaddr
*ifp
= NULL
;
4380 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
4381 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
4383 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
4384 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
4395 * Periodic address status verification
4398 static void addrconf_verify_rtnl(void)
4400 unsigned long now
, next
, next_sec
, next_sched
;
4401 struct inet6_ifaddr
*ifp
;
4408 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
4410 cancel_delayed_work(&addr_chk_work
);
4412 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
4414 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[i
], addr_lst
) {
4417 /* When setting preferred_lft to a value not zero or
4418 * infinity, while valid_lft is infinity
4419 * IFA_F_PERMANENT has a non-infinity life time.
4421 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
4422 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
4425 spin_lock(&ifp
->lock
);
4426 /* We try to batch several events at once. */
4427 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
4429 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
4430 age
>= ifp
->valid_lft
) {
4431 spin_unlock(&ifp
->lock
);
4435 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
4436 spin_unlock(&ifp
->lock
);
4438 } else if (age
>= ifp
->prefered_lft
) {
4439 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4442 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
4444 ifp
->flags
|= IFA_F_DEPRECATED
;
4447 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
4448 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
4449 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
4451 spin_unlock(&ifp
->lock
);
4456 ipv6_ifa_notify(0, ifp
);
4460 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
4461 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
4462 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
4463 ifp
->idev
->cnf
.dad_transmits
*
4464 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
) / HZ
;
4466 if (age
>= ifp
->prefered_lft
- regen_advance
) {
4467 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
4468 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4469 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4470 if (!ifp
->regen_count
&& ifpub
) {
4473 in6_ifa_hold(ifpub
);
4474 spin_unlock(&ifp
->lock
);
4476 spin_lock(&ifpub
->lock
);
4477 ifpub
->regen_count
= 0;
4478 spin_unlock(&ifpub
->lock
);
4479 rcu_read_unlock_bh();
4480 ipv6_create_tempaddr(ifpub
, ifp
, true);
4486 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
4487 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
4488 spin_unlock(&ifp
->lock
);
4490 /* ifp->prefered_lft <= ifp->valid_lft */
4491 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4492 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4493 spin_unlock(&ifp
->lock
);
4498 next_sec
= round_jiffies_up(next
);
4501 /* If rounded timeout is accurate enough, accept it. */
4502 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
4503 next_sched
= next_sec
;
4505 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4506 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
4507 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
4509 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4510 now
, next
, next_sec
, next_sched
);
4511 mod_delayed_work(addrconf_wq
, &addr_chk_work
, next_sched
- now
);
4512 rcu_read_unlock_bh();
4515 static void addrconf_verify_work(struct work_struct
*w
)
4518 addrconf_verify_rtnl();
4522 static void addrconf_verify(void)
4524 mod_delayed_work(addrconf_wq
, &addr_chk_work
, 0);
4527 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
4528 struct in6_addr
**peer_pfx
)
4530 struct in6_addr
*pfx
= NULL
;
4535 pfx
= nla_data(addr
);
4538 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
4540 pfx
= nla_data(local
);
4546 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
4547 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
4548 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
4549 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
4550 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
4551 [IFA_RT_PRIORITY
] = { .len
= sizeof(u32
) },
4552 [IFA_TARGET_NETNSID
] = { .type
= NLA_S32
},
4556 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4557 struct netlink_ext_ack
*extack
)
4559 struct net
*net
= sock_net(skb
->sk
);
4560 struct ifaddrmsg
*ifm
;
4561 struct nlattr
*tb
[IFA_MAX
+1];
4562 struct in6_addr
*pfx
, *peer_pfx
;
4566 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
,
4571 ifm
= nlmsg_data(nlh
);
4572 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4576 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4578 /* We ignore other flags so far. */
4579 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
4581 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
4582 ifm
->ifa_prefixlen
);
4585 static int modify_prefix_route(struct inet6_ifaddr
*ifp
,
4586 unsigned long expires
, u32 flags
)
4588 struct fib6_info
*f6i
;
4591 f6i
= addrconf_get_prefix_route(&ifp
->addr
,
4594 0, RTF_GATEWAY
| RTF_DEFAULT
);
4598 prio
= ifp
->rt_priority
? : IP6_RT_PRIO_ADDRCONF
;
4599 if (f6i
->fib6_metric
!= prio
) {
4600 /* delete old one */
4601 ip6_del_rt(dev_net(ifp
->idev
->dev
), f6i
);
4604 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
4605 ifp
->rt_priority
, ifp
->idev
->dev
,
4606 expires
, flags
, GFP_KERNEL
);
4609 fib6_clean_expires(f6i
);
4611 fib6_set_expires(f6i
, expires
);
4613 fib6_info_release(f6i
);
4619 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, struct ifa6_config
*cfg
)
4623 unsigned long timeout
;
4624 bool was_managetempaddr
;
4625 bool had_prefixroute
;
4629 if (!cfg
->valid_lft
|| cfg
->preferred_lft
> cfg
->valid_lft
)
4632 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
&&
4633 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
4636 if (!(ifp
->flags
& IFA_F_TENTATIVE
) || ifp
->flags
& IFA_F_DADFAILED
)
4637 cfg
->ifa_flags
&= ~IFA_F_OPTIMISTIC
;
4639 timeout
= addrconf_timeout_fixup(cfg
->valid_lft
, HZ
);
4640 if (addrconf_finite_timeout(timeout
)) {
4641 expires
= jiffies_to_clock_t(timeout
* HZ
);
4642 cfg
->valid_lft
= timeout
;
4643 flags
= RTF_EXPIRES
;
4647 cfg
->ifa_flags
|= IFA_F_PERMANENT
;
4650 timeout
= addrconf_timeout_fixup(cfg
->preferred_lft
, HZ
);
4651 if (addrconf_finite_timeout(timeout
)) {
4653 cfg
->ifa_flags
|= IFA_F_DEPRECATED
;
4654 cfg
->preferred_lft
= timeout
;
4657 spin_lock_bh(&ifp
->lock
);
4658 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
4659 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
4660 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
4661 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
4662 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4663 IFA_F_NOPREFIXROUTE
);
4664 ifp
->flags
|= cfg
->ifa_flags
;
4665 ifp
->tstamp
= jiffies
;
4666 ifp
->valid_lft
= cfg
->valid_lft
;
4667 ifp
->prefered_lft
= cfg
->preferred_lft
;
4669 if (cfg
->rt_priority
&& cfg
->rt_priority
!= ifp
->rt_priority
)
4670 ifp
->rt_priority
= cfg
->rt_priority
;
4672 spin_unlock_bh(&ifp
->lock
);
4673 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
4674 ipv6_ifa_notify(0, ifp
);
4676 if (!(cfg
->ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
4679 if (had_prefixroute
)
4680 rc
= modify_prefix_route(ifp
, expires
, flags
);
4682 /* prefix route could have been deleted; if so restore it */
4683 if (rc
== -ENOENT
) {
4684 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
4685 ifp
->rt_priority
, ifp
->idev
->dev
,
4686 expires
, flags
, GFP_KERNEL
);
4688 } else if (had_prefixroute
) {
4689 enum cleanup_prefix_rt_t action
;
4690 unsigned long rt_expires
;
4692 write_lock_bh(&ifp
->idev
->lock
);
4693 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
4694 write_unlock_bh(&ifp
->idev
->lock
);
4696 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
4697 cleanup_prefix_route(ifp
, rt_expires
,
4698 action
== CLEANUP_PREFIX_RT_DEL
);
4702 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
4703 if (was_managetempaddr
&&
4704 !(ifp
->flags
& IFA_F_MANAGETEMPADDR
)) {
4706 cfg
->preferred_lft
= 0;
4708 manage_tempaddrs(ifp
->idev
, ifp
, cfg
->valid_lft
,
4709 cfg
->preferred_lft
, !was_managetempaddr
,
4713 addrconf_verify_rtnl();
4719 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4720 struct netlink_ext_ack
*extack
)
4722 struct net
*net
= sock_net(skb
->sk
);
4723 struct ifaddrmsg
*ifm
;
4724 struct nlattr
*tb
[IFA_MAX
+1];
4725 struct in6_addr
*peer_pfx
;
4726 struct inet6_ifaddr
*ifa
;
4727 struct net_device
*dev
;
4728 struct inet6_dev
*idev
;
4729 struct ifa6_config cfg
;
4732 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
,
4737 memset(&cfg
, 0, sizeof(cfg
));
4739 ifm
= nlmsg_data(nlh
);
4740 cfg
.pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4744 cfg
.peer_pfx
= peer_pfx
;
4745 cfg
.plen
= ifm
->ifa_prefixlen
;
4746 if (tb
[IFA_RT_PRIORITY
])
4747 cfg
.rt_priority
= nla_get_u32(tb
[IFA_RT_PRIORITY
]);
4749 cfg
.valid_lft
= INFINITY_LIFE_TIME
;
4750 cfg
.preferred_lft
= INFINITY_LIFE_TIME
;
4752 if (tb
[IFA_CACHEINFO
]) {
4753 struct ifa_cacheinfo
*ci
;
4755 ci
= nla_data(tb
[IFA_CACHEINFO
]);
4756 cfg
.valid_lft
= ci
->ifa_valid
;
4757 cfg
.preferred_lft
= ci
->ifa_prefered
;
4760 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4765 cfg
.ifa_flags
= nla_get_u32(tb
[IFA_FLAGS
]);
4767 cfg
.ifa_flags
= ifm
->ifa_flags
;
4769 /* We ignore other flags so far. */
4770 cfg
.ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
|
4771 IFA_F_MANAGETEMPADDR
| IFA_F_NOPREFIXROUTE
|
4772 IFA_F_MCAUTOJOIN
| IFA_F_OPTIMISTIC
;
4774 idev
= ipv6_find_idev(dev
);
4778 if (!ipv6_allow_optimistic_dad(net
, idev
))
4779 cfg
.ifa_flags
&= ~IFA_F_OPTIMISTIC
;
4781 if (cfg
.ifa_flags
& IFA_F_NODAD
&&
4782 cfg
.ifa_flags
& IFA_F_OPTIMISTIC
) {
4783 NL_SET_ERR_MSG(extack
, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4787 ifa
= ipv6_get_ifaddr(net
, cfg
.pfx
, dev
, 1);
4790 * It would be best to check for !NLM_F_CREATE here but
4791 * userspace already relies on not having to provide this.
4793 return inet6_addr_add(net
, ifm
->ifa_index
, &cfg
, extack
);
4796 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
4797 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
4800 err
= inet6_addr_modify(ifa
, &cfg
);
4807 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
4808 u8 scope
, int ifindex
)
4810 struct ifaddrmsg
*ifm
;
4812 ifm
= nlmsg_data(nlh
);
4813 ifm
->ifa_family
= AF_INET6
;
4814 ifm
->ifa_prefixlen
= prefixlen
;
4815 ifm
->ifa_flags
= flags
;
4816 ifm
->ifa_scope
= scope
;
4817 ifm
->ifa_index
= ifindex
;
4820 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
4821 unsigned long tstamp
, u32 preferred
, u32 valid
)
4823 struct ifa_cacheinfo ci
;
4825 ci
.cstamp
= cstamp_delta(cstamp
);
4826 ci
.tstamp
= cstamp_delta(tstamp
);
4827 ci
.ifa_prefered
= preferred
;
4828 ci
.ifa_valid
= valid
;
4830 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
4833 static inline int rt_scope(int ifa_scope
)
4835 if (ifa_scope
& IFA_HOST
)
4836 return RT_SCOPE_HOST
;
4837 else if (ifa_scope
& IFA_LINK
)
4838 return RT_SCOPE_LINK
;
4839 else if (ifa_scope
& IFA_SITE
)
4840 return RT_SCOPE_SITE
;
4842 return RT_SCOPE_UNIVERSE
;
4845 static inline int inet6_ifaddr_msgsize(void)
4847 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
4848 + nla_total_size(16) /* IFA_LOCAL */
4849 + nla_total_size(16) /* IFA_ADDRESS */
4850 + nla_total_size(sizeof(struct ifa_cacheinfo
))
4851 + nla_total_size(4) /* IFA_FLAGS */
4852 + nla_total_size(4) /* IFA_RT_PRIORITY */;
4861 struct inet6_fill_args
{
4868 enum addr_type_t type
;
4871 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
4872 struct inet6_fill_args
*args
)
4874 struct nlmsghdr
*nlh
;
4875 u32 preferred
, valid
;
4877 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
4878 sizeof(struct ifaddrmsg
), args
->flags
);
4882 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
4883 ifa
->idev
->dev
->ifindex
);
4885 if (args
->netnsid
>= 0 &&
4886 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
))
4889 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
4890 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
4891 preferred
= ifa
->prefered_lft
;
4892 valid
= ifa
->valid_lft
;
4893 if (preferred
!= INFINITY_LIFE_TIME
) {
4894 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
4895 if (preferred
> tval
)
4899 if (valid
!= INFINITY_LIFE_TIME
) {
4907 preferred
= INFINITY_LIFE_TIME
;
4908 valid
= INFINITY_LIFE_TIME
;
4911 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
4912 if (nla_put_in6_addr(skb
, IFA_LOCAL
, &ifa
->addr
) < 0 ||
4913 nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->peer_addr
) < 0)
4916 if (nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->addr
) < 0)
4919 if (ifa
->rt_priority
&&
4920 nla_put_u32(skb
, IFA_RT_PRIORITY
, ifa
->rt_priority
))
4923 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
4926 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
4929 nlmsg_end(skb
, nlh
);
4933 nlmsg_cancel(skb
, nlh
);
4937 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
4938 struct inet6_fill_args
*args
)
4940 struct nlmsghdr
*nlh
;
4941 u8 scope
= RT_SCOPE_UNIVERSE
;
4942 int ifindex
= ifmca
->idev
->dev
->ifindex
;
4944 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
4945 scope
= RT_SCOPE_SITE
;
4947 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
4948 sizeof(struct ifaddrmsg
), args
->flags
);
4952 if (args
->netnsid
>= 0 &&
4953 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
))
4956 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4957 if (nla_put_in6_addr(skb
, IFA_MULTICAST
, &ifmca
->mca_addr
) < 0 ||
4958 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
4959 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4960 nlmsg_cancel(skb
, nlh
);
4964 nlmsg_end(skb
, nlh
);
4968 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
4969 struct inet6_fill_args
*args
)
4971 struct net_device
*dev
= fib6_info_nh_dev(ifaca
->aca_rt
);
4972 int ifindex
= dev
? dev
->ifindex
: 1;
4973 struct nlmsghdr
*nlh
;
4974 u8 scope
= RT_SCOPE_UNIVERSE
;
4976 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
4977 scope
= RT_SCOPE_SITE
;
4979 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
4980 sizeof(struct ifaddrmsg
), args
->flags
);
4984 if (args
->netnsid
>= 0 &&
4985 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
))
4988 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4989 if (nla_put_in6_addr(skb
, IFA_ANYCAST
, &ifaca
->aca_addr
) < 0 ||
4990 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
4991 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4992 nlmsg_cancel(skb
, nlh
);
4996 nlmsg_end(skb
, nlh
);
5000 /* called with rcu_read_lock() */
5001 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
5002 struct netlink_callback
*cb
, int s_ip_idx
,
5003 struct inet6_fill_args
*fillargs
)
5005 struct ifmcaddr6
*ifmca
;
5006 struct ifacaddr6
*ifaca
;
5010 read_lock_bh(&idev
->lock
);
5011 switch (fillargs
->type
) {
5012 case UNICAST_ADDR
: {
5013 struct inet6_ifaddr
*ifa
;
5014 fillargs
->event
= RTM_NEWADDR
;
5016 /* unicast address incl. temp addr */
5017 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
5018 if (ip_idx
< s_ip_idx
)
5020 err
= inet6_fill_ifaddr(skb
, ifa
, fillargs
);
5023 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5029 case MULTICAST_ADDR
:
5030 fillargs
->event
= RTM_GETMULTICAST
;
5032 /* multicast address */
5033 for (ifmca
= idev
->mc_list
; ifmca
;
5034 ifmca
= ifmca
->next
, ip_idx
++) {
5035 if (ip_idx
< s_ip_idx
)
5037 err
= inet6_fill_ifmcaddr(skb
, ifmca
, fillargs
);
5043 fillargs
->event
= RTM_GETANYCAST
;
5044 /* anycast address */
5045 for (ifaca
= idev
->ac_list
; ifaca
;
5046 ifaca
= ifaca
->aca_next
, ip_idx
++) {
5047 if (ip_idx
< s_ip_idx
)
5049 err
= inet6_fill_ifacaddr(skb
, ifaca
, fillargs
);
5057 read_unlock_bh(&idev
->lock
);
5058 cb
->args
[2] = ip_idx
;
5062 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr
*nlh
,
5063 struct inet6_fill_args
*fillargs
,
5064 struct net
**tgt_net
, struct sock
*sk
,
5065 struct netlink_callback
*cb
)
5067 struct netlink_ext_ack
*extack
= cb
->extack
;
5068 struct nlattr
*tb
[IFA_MAX
+1];
5069 struct ifaddrmsg
*ifm
;
5072 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5073 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for address dump request");
5077 ifm
= nlmsg_data(nlh
);
5078 if (ifm
->ifa_prefixlen
|| ifm
->ifa_flags
|| ifm
->ifa_scope
) {
5079 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for address dump request");
5083 fillargs
->ifindex
= ifm
->ifa_index
;
5084 if (fillargs
->ifindex
) {
5085 cb
->answer_flags
|= NLM_F_DUMP_FILTERED
;
5086 fillargs
->flags
|= NLM_F_DUMP_FILTERED
;
5089 err
= nlmsg_parse_strict(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5090 ifa_ipv6_policy
, extack
);
5094 for (i
= 0; i
<= IFA_MAX
; ++i
) {
5098 if (i
== IFA_TARGET_NETNSID
) {
5101 fillargs
->netnsid
= nla_get_s32(tb
[i
]);
5102 net
= rtnl_get_net_ns_capable(sk
, fillargs
->netnsid
);
5104 fillargs
->netnsid
= -1;
5105 NL_SET_ERR_MSG_MOD(extack
, "Invalid target network namespace id");
5106 return PTR_ERR(net
);
5110 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in dump request");
5118 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
5119 enum addr_type_t type
)
5121 const struct nlmsghdr
*nlh
= cb
->nlh
;
5122 struct inet6_fill_args fillargs
= {
5123 .portid
= NETLINK_CB(cb
->skb
).portid
,
5124 .seq
= cb
->nlh
->nlmsg_seq
,
5125 .flags
= NLM_F_MULTI
,
5129 struct net
*net
= sock_net(skb
->sk
);
5130 struct net
*tgt_net
= net
;
5131 int idx
, s_idx
, s_ip_idx
;
5133 struct net_device
*dev
;
5134 struct inet6_dev
*idev
;
5135 struct hlist_head
*head
;
5139 s_idx
= idx
= cb
->args
[1];
5140 s_ip_idx
= cb
->args
[2];
5142 if (cb
->strict_check
) {
5143 err
= inet6_valid_dump_ifaddr_req(nlh
, &fillargs
, &tgt_net
,
5149 if (fillargs
.ifindex
) {
5150 dev
= __dev_get_by_index(tgt_net
, fillargs
.ifindex
);
5155 idev
= __in6_dev_get(dev
);
5157 err
= in6_dump_addrs(idev
, skb
, cb
, s_ip_idx
,
5167 cb
->seq
= atomic_read(&tgt_net
->ipv6
.dev_addr_genid
) ^ tgt_net
->dev_base_seq
;
5168 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5170 head
= &tgt_net
->dev_index_head
[h
];
5171 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
5174 if (h
> s_h
|| idx
> s_idx
)
5176 idev
= __in6_dev_get(dev
);
5180 if (in6_dump_addrs(idev
, skb
, cb
, s_ip_idx
,
5192 if (fillargs
.netnsid
>= 0)
5195 return skb
->len
? : err
;
5198 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5200 enum addr_type_t type
= UNICAST_ADDR
;
5202 return inet6_dump_addr(skb
, cb
, type
);
5205 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5207 enum addr_type_t type
= MULTICAST_ADDR
;
5209 return inet6_dump_addr(skb
, cb
, type
);
5213 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5215 enum addr_type_t type
= ANYCAST_ADDR
;
5217 return inet6_dump_addr(skb
, cb
, type
);
5220 static int inet6_rtm_valid_getaddr_req(struct sk_buff
*skb
,
5221 const struct nlmsghdr
*nlh
,
5223 struct netlink_ext_ack
*extack
)
5225 struct ifaddrmsg
*ifm
;
5228 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5229 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for get address request");
5233 ifm
= nlmsg_data(nlh
);
5234 if (ifm
->ifa_prefixlen
|| ifm
->ifa_flags
|| ifm
->ifa_scope
) {
5235 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for get address request");
5239 if (!netlink_strict_get_check(skb
))
5240 return nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5241 ifa_ipv6_policy
, extack
);
5243 err
= nlmsg_parse_strict(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5244 ifa_ipv6_policy
, extack
);
5248 for (i
= 0; i
<= IFA_MAX
; i
++) {
5253 case IFA_TARGET_NETNSID
:
5258 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in get address request");
5266 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
5267 struct netlink_ext_ack
*extack
)
5269 struct net
*net
= sock_net(in_skb
->sk
);
5270 struct inet6_fill_args fillargs
= {
5271 .portid
= NETLINK_CB(in_skb
).portid
,
5272 .seq
= nlh
->nlmsg_seq
,
5273 .event
= RTM_NEWADDR
,
5277 struct net
*tgt_net
= net
;
5278 struct ifaddrmsg
*ifm
;
5279 struct nlattr
*tb
[IFA_MAX
+1];
5280 struct in6_addr
*addr
= NULL
, *peer
;
5281 struct net_device
*dev
= NULL
;
5282 struct inet6_ifaddr
*ifa
;
5283 struct sk_buff
*skb
;
5286 err
= inet6_rtm_valid_getaddr_req(in_skb
, nlh
, tb
, extack
);
5290 if (tb
[IFA_TARGET_NETNSID
]) {
5291 fillargs
.netnsid
= nla_get_s32(tb
[IFA_TARGET_NETNSID
]);
5293 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(in_skb
).sk
,
5295 if (IS_ERR(tgt_net
))
5296 return PTR_ERR(tgt_net
);
5299 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
5303 ifm
= nlmsg_data(nlh
);
5305 dev
= dev_get_by_index(tgt_net
, ifm
->ifa_index
);
5307 ifa
= ipv6_get_ifaddr(tgt_net
, addr
, dev
, 1);
5309 err
= -EADDRNOTAVAIL
;
5313 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
5319 err
= inet6_fill_ifaddr(skb
, ifa
, &fillargs
);
5321 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5322 WARN_ON(err
== -EMSGSIZE
);
5326 err
= rtnl_unicast(skb
, tgt_net
, NETLINK_CB(in_skb
).portid
);
5332 if (fillargs
.netnsid
>= 0)
5338 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
5340 struct sk_buff
*skb
;
5341 struct net
*net
= dev_net(ifa
->idev
->dev
);
5342 struct inet6_fill_args fillargs
= {
5351 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
5355 err
= inet6_fill_ifaddr(skb
, ifa
, &fillargs
);
5357 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5358 WARN_ON(err
== -EMSGSIZE
);
5362 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
5366 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
5369 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
5370 __s32
*array
, int bytes
)
5372 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
5374 memset(array
, 0, bytes
);
5375 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
5376 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
5377 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
5378 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
5379 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
5380 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
5381 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
5382 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
5383 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
5384 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
5385 array
[DEVCONF_RTR_SOLICIT_MAX_INTERVAL
] =
5386 jiffies_to_msecs(cnf
->rtr_solicit_max_interval
);
5387 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
5388 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
5389 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
5390 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
5391 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
5392 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
5393 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
5394 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
5395 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
5396 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
5397 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
5398 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
5399 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
5400 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
5401 array
[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT
] = cnf
->accept_ra_min_hop_limit
;
5402 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
5403 #ifdef CONFIG_IPV6_ROUTER_PREF
5404 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
5405 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
5406 jiffies_to_msecs(cnf
->rtr_probe_interval
);
5407 #ifdef CONFIG_IPV6_ROUTE_INFO
5408 array
[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN
] = cnf
->accept_ra_rt_info_min_plen
;
5409 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
5412 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
5413 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
5414 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5415 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
5416 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
5418 #ifdef CONFIG_IPV6_MROUTE
5419 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
5421 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
5422 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
5423 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
5424 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
5425 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
5426 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
5427 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
5428 array
[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN
] = cnf
->ignore_routes_with_linkdown
;
5429 /* we omit DEVCONF_STABLE_SECRET for now */
5430 array
[DEVCONF_USE_OIF_ADDRS_ONLY
] = cnf
->use_oif_addrs_only
;
5431 array
[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST
] = cnf
->drop_unicast_in_l2_multicast
;
5432 array
[DEVCONF_DROP_UNSOLICITED_NA
] = cnf
->drop_unsolicited_na
;
5433 array
[DEVCONF_KEEP_ADDR_ON_DOWN
] = cnf
->keep_addr_on_down
;
5434 array
[DEVCONF_SEG6_ENABLED
] = cnf
->seg6_enabled
;
5435 #ifdef CONFIG_IPV6_SEG6_HMAC
5436 array
[DEVCONF_SEG6_REQUIRE_HMAC
] = cnf
->seg6_require_hmac
;
5438 array
[DEVCONF_ENHANCED_DAD
] = cnf
->enhanced_dad
;
5439 array
[DEVCONF_ADDR_GEN_MODE
] = cnf
->addr_gen_mode
;
5440 array
[DEVCONF_DISABLE_POLICY
] = cnf
->disable_policy
;
5441 array
[DEVCONF_NDISC_TCLASS
] = cnf
->ndisc_tclass
;
5444 static inline size_t inet6_ifla6_size(void)
5446 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5447 + nla_total_size(sizeof(struct ifla_cacheinfo
))
5448 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
5449 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
5450 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
5451 + nla_total_size(sizeof(struct in6_addr
)) /* IFLA_INET6_TOKEN */
5452 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5456 static inline size_t inet6_if_nlmsg_size(void)
5458 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
5459 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
5460 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
5461 + nla_total_size(4) /* IFLA_MTU */
5462 + nla_total_size(4) /* IFLA_LINK */
5463 + nla_total_size(1) /* IFLA_OPERSTATE */
5464 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5467 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
5471 int pad
= bytes
- sizeof(u64
) * ICMP6_MIB_MAX
;
5474 /* Use put_unaligned() because stats may not be aligned for u64. */
5475 put_unaligned(ICMP6_MIB_MAX
, &stats
[0]);
5476 for (i
= 1; i
< ICMP6_MIB_MAX
; i
++)
5477 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
5479 memset(&stats
[ICMP6_MIB_MAX
], 0, pad
);
5482 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
5483 int bytes
, size_t syncpoff
)
5486 u64 buff
[IPSTATS_MIB_MAX
];
5487 int pad
= bytes
- sizeof(u64
) * IPSTATS_MIB_MAX
;
5491 memset(buff
, 0, sizeof(buff
));
5492 buff
[0] = IPSTATS_MIB_MAX
;
5494 for_each_possible_cpu(c
) {
5495 for (i
= 1; i
< IPSTATS_MIB_MAX
; i
++)
5496 buff
[i
] += snmp_get_cpu_field64(mib
, c
, i
, syncpoff
);
5499 memcpy(stats
, buff
, IPSTATS_MIB_MAX
* sizeof(u64
));
5500 memset(&stats
[IPSTATS_MIB_MAX
], 0, pad
);
5503 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
5507 case IFLA_INET6_STATS
:
5508 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
, bytes
,
5509 offsetof(struct ipstats_mib
, syncp
));
5511 case IFLA_INET6_ICMP6STATS
:
5512 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, bytes
);
5517 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5518 u32 ext_filter_mask
)
5521 struct ifla_cacheinfo ci
;
5523 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
5524 goto nla_put_failure
;
5525 ci
.max_reasm_len
= IPV6_MAXPLEN
;
5526 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
5527 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
5528 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
5529 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
5530 goto nla_put_failure
;
5531 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
5533 goto nla_put_failure
;
5534 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
5536 /* XXX - MC not implemented */
5538 if (ext_filter_mask
& RTEXT_FILTER_SKIP_STATS
)
5541 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
5543 goto nla_put_failure
;
5544 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
5546 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
5548 goto nla_put_failure
;
5549 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
5551 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
5553 goto nla_put_failure
;
5555 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->cnf
.addr_gen_mode
))
5556 goto nla_put_failure
;
5558 read_lock_bh(&idev
->lock
);
5559 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
5560 read_unlock_bh(&idev
->lock
);
5568 static size_t inet6_get_link_af_size(const struct net_device
*dev
,
5569 u32 ext_filter_mask
)
5571 if (!__in6_dev_get(dev
))
5574 return inet6_ifla6_size();
5577 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
,
5578 u32 ext_filter_mask
)
5580 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5585 if (inet6_fill_ifla6_attrs(skb
, idev
, ext_filter_mask
) < 0)
5591 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
)
5593 struct inet6_ifaddr
*ifp
;
5594 struct net_device
*dev
= idev
->dev
;
5595 bool clear_token
, update_rs
= false;
5596 struct in6_addr ll_addr
;
5602 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
))
5604 if (!ipv6_accept_ra(idev
))
5606 if (idev
->cnf
.rtr_solicits
== 0)
5609 write_lock_bh(&idev
->lock
);
5611 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
5612 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
5614 write_unlock_bh(&idev
->lock
);
5616 clear_token
= ipv6_addr_any(token
);
5620 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
5621 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
5622 IFA_F_OPTIMISTIC
)) {
5623 /* If we're not ready, then normal ifup will take care
5624 * of this. Otherwise, we need to request our rs here.
5626 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
5631 write_lock_bh(&idev
->lock
);
5634 idev
->if_flags
|= IF_RS_SENT
;
5635 idev
->rs_interval
= rfc3315_s14_backoff_init(
5636 idev
->cnf
.rtr_solicit_interval
);
5637 idev
->rs_probes
= 1;
5638 addrconf_mod_rs_timer(idev
, idev
->rs_interval
);
5641 /* Well, that's kinda nasty ... */
5642 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
5643 spin_lock(&ifp
->lock
);
5644 if (ifp
->tokenized
) {
5646 ifp
->prefered_lft
= 0;
5648 spin_unlock(&ifp
->lock
);
5651 write_unlock_bh(&idev
->lock
);
5652 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
5653 addrconf_verify_rtnl();
5657 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
5658 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
5659 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
5662 static int inet6_validate_link_af(const struct net_device
*dev
,
5663 const struct nlattr
*nla
)
5665 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5667 if (dev
&& !__in6_dev_get(dev
))
5668 return -EAFNOSUPPORT
;
5670 return nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, inet6_af_policy
,
5674 static int check_addr_gen_mode(int mode
)
5676 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
5677 mode
!= IN6_ADDR_GEN_MODE_NONE
&&
5678 mode
!= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5679 mode
!= IN6_ADDR_GEN_MODE_RANDOM
)
5684 static int check_stable_privacy(struct inet6_dev
*idev
, struct net
*net
,
5687 if (mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5688 !idev
->cnf
.stable_secret
.initialized
&&
5689 !net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
5694 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
5697 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5698 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5701 return -EAFNOSUPPORT
;
5703 if (nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, NULL
, NULL
) < 0)
5706 if (tb
[IFLA_INET6_TOKEN
]) {
5707 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]));
5712 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
5713 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
5715 if (check_addr_gen_mode(mode
) < 0 ||
5716 check_stable_privacy(idev
, dev_net(dev
), mode
) < 0)
5719 idev
->cnf
.addr_gen_mode
= mode
;
5726 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5727 u32 portid
, u32 seq
, int event
, unsigned int flags
)
5729 struct net_device
*dev
= idev
->dev
;
5730 struct ifinfomsg
*hdr
;
5731 struct nlmsghdr
*nlh
;
5734 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
5738 hdr
= nlmsg_data(nlh
);
5739 hdr
->ifi_family
= AF_INET6
;
5741 hdr
->ifi_type
= dev
->type
;
5742 hdr
->ifi_index
= dev
->ifindex
;
5743 hdr
->ifi_flags
= dev_get_flags(dev
);
5744 hdr
->ifi_change
= 0;
5746 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
5748 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
5749 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
5750 (dev
->ifindex
!= dev_get_iflink(dev
) &&
5751 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
5752 nla_put_u8(skb
, IFLA_OPERSTATE
,
5753 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
))
5754 goto nla_put_failure
;
5755 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
5757 goto nla_put_failure
;
5759 if (inet6_fill_ifla6_attrs(skb
, idev
, 0) < 0)
5760 goto nla_put_failure
;
5762 nla_nest_end(skb
, protoinfo
);
5763 nlmsg_end(skb
, nlh
);
5767 nlmsg_cancel(skb
, nlh
);
5771 static int inet6_valid_dump_ifinfo(const struct nlmsghdr
*nlh
,
5772 struct netlink_ext_ack
*extack
)
5774 struct ifinfomsg
*ifm
;
5776 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5777 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for link dump request");
5781 if (nlmsg_attrlen(nlh
, sizeof(*ifm
))) {
5782 NL_SET_ERR_MSG_MOD(extack
, "Invalid data after header");
5786 ifm
= nlmsg_data(nlh
);
5787 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
5788 ifm
->ifi_change
|| ifm
->ifi_index
) {
5789 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for dump request");
5796 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5798 struct net
*net
= sock_net(skb
->sk
);
5801 struct net_device
*dev
;
5802 struct inet6_dev
*idev
;
5803 struct hlist_head
*head
;
5805 /* only requests using strict checking can pass data to
5806 * influence the dump
5808 if (cb
->strict_check
) {
5809 int err
= inet6_valid_dump_ifinfo(cb
->nlh
, cb
->extack
);
5816 s_idx
= cb
->args
[1];
5819 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5821 head
= &net
->dev_index_head
[h
];
5822 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
5825 idev
= __in6_dev_get(dev
);
5828 if (inet6_fill_ifinfo(skb
, idev
,
5829 NETLINK_CB(cb
->skb
).portid
,
5831 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
5845 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
5847 struct sk_buff
*skb
;
5848 struct net
*net
= dev_net(idev
->dev
);
5851 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
5855 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
5857 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5858 WARN_ON(err
== -EMSGSIZE
);
5862 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
5866 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
5869 static inline size_t inet6_prefix_nlmsg_size(void)
5871 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
5872 + nla_total_size(sizeof(struct in6_addr
))
5873 + nla_total_size(sizeof(struct prefix_cacheinfo
));
5876 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5877 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
5878 int event
, unsigned int flags
)
5880 struct prefixmsg
*pmsg
;
5881 struct nlmsghdr
*nlh
;
5882 struct prefix_cacheinfo ci
;
5884 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
5888 pmsg
= nlmsg_data(nlh
);
5889 pmsg
->prefix_family
= AF_INET6
;
5890 pmsg
->prefix_pad1
= 0;
5891 pmsg
->prefix_pad2
= 0;
5892 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
5893 pmsg
->prefix_len
= pinfo
->prefix_len
;
5894 pmsg
->prefix_type
= pinfo
->type
;
5895 pmsg
->prefix_pad3
= 0;
5896 pmsg
->prefix_flags
= 0;
5898 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
5899 if (pinfo
->autoconf
)
5900 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
5902 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
5903 goto nla_put_failure
;
5904 ci
.preferred_time
= ntohl(pinfo
->prefered
);
5905 ci
.valid_time
= ntohl(pinfo
->valid
);
5906 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
5907 goto nla_put_failure
;
5908 nlmsg_end(skb
, nlh
);
5912 nlmsg_cancel(skb
, nlh
);
5916 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
5917 struct prefix_info
*pinfo
)
5919 struct sk_buff
*skb
;
5920 struct net
*net
= dev_net(idev
->dev
);
5923 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
5927 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
5929 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5930 WARN_ON(err
== -EMSGSIZE
);
5934 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
5938 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
5941 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5943 struct net
*net
= dev_net(ifp
->idev
->dev
);
5948 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
5953 * If the address was optimistic
5954 * we inserted the route at the start of
5955 * our DAD process, so we don't need
5958 if (!rcu_access_pointer(ifp
->rt
->fib6_node
))
5959 ip6_ins_rt(net
, ifp
->rt
);
5960 if (ifp
->idev
->cnf
.forwarding
)
5961 addrconf_join_anycast(ifp
);
5962 if (!ipv6_addr_any(&ifp
->peer_addr
))
5963 addrconf_prefix_route(&ifp
->peer_addr
, 128, 0,
5964 ifp
->idev
->dev
, 0, 0,
5968 if (ifp
->idev
->cnf
.forwarding
)
5969 addrconf_leave_anycast(ifp
);
5970 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
5971 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
5972 struct fib6_info
*rt
;
5974 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
5975 ifp
->idev
->dev
, 0, 0);
5977 ip6_del_rt(net
, rt
);
5980 ip6_del_rt(net
, ifp
->rt
);
5983 rt_genid_bump_ipv6(net
);
5986 atomic_inc(&net
->ipv6
.dev_addr_genid
);
5989 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5992 if (likely(ifp
->idev
->dead
== 0))
5993 __ipv6_ifa_notify(event
, ifp
);
5994 rcu_read_unlock_bh();
5997 #ifdef CONFIG_SYSCTL
6000 int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
6001 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6003 int *valp
= ctl
->data
;
6006 struct ctl_table lctl
;
6010 * ctl->data points to idev->cnf.forwarding, we should
6011 * not modify it until we get the rtnl lock.
6016 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6019 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
6026 int addrconf_sysctl_mtu(struct ctl_table
*ctl
, int write
,
6027 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6029 struct inet6_dev
*idev
= ctl
->extra1
;
6030 int min_mtu
= IPV6_MIN_MTU
;
6031 struct ctl_table lctl
;
6034 lctl
.extra1
= &min_mtu
;
6035 lctl
.extra2
= idev
? &idev
->dev
->mtu
: NULL
;
6037 return proc_dointvec_minmax(&lctl
, write
, buffer
, lenp
, ppos
);
6040 static void dev_disable_change(struct inet6_dev
*idev
)
6042 struct netdev_notifier_info info
;
6044 if (!idev
|| !idev
->dev
)
6047 netdev_notifier_info_init(&info
, idev
->dev
);
6048 if (idev
->cnf
.disable_ipv6
)
6049 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
6051 addrconf_notify(NULL
, NETDEV_UP
, &info
);
6054 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
6056 struct net_device
*dev
;
6057 struct inet6_dev
*idev
;
6059 for_each_netdev(net
, dev
) {
6060 idev
= __in6_dev_get(dev
);
6062 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
6063 idev
->cnf
.disable_ipv6
= newf
;
6065 dev_disable_change(idev
);
6070 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
6075 if (!rtnl_trylock())
6076 return restart_syscall();
6078 net
= (struct net
*)table
->extra2
;
6082 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
6087 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
6088 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
6089 addrconf_disable_change(net
, newf
);
6090 } else if ((!newf
) ^ (!old
))
6091 dev_disable_change((struct inet6_dev
*)table
->extra1
);
6098 int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
6099 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6101 int *valp
= ctl
->data
;
6104 struct ctl_table lctl
;
6108 * ctl->data points to idev->cnf.disable_ipv6, we should
6109 * not modify it until we get the rtnl lock.
6114 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6117 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
6124 int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
6125 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6127 int *valp
= ctl
->data
;
6132 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
6135 if (write
&& old
!= new) {
6136 struct net
*net
= ctl
->extra2
;
6138 if (!rtnl_trylock())
6139 return restart_syscall();
6141 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
6142 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6143 NETCONFA_PROXY_NEIGH
,
6144 NETCONFA_IFINDEX_DEFAULT
,
6145 net
->ipv6
.devconf_dflt
);
6146 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
6147 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6148 NETCONFA_PROXY_NEIGH
,
6149 NETCONFA_IFINDEX_ALL
,
6150 net
->ipv6
.devconf_all
);
6152 struct inet6_dev
*idev
= ctl
->extra1
;
6154 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6155 NETCONFA_PROXY_NEIGH
,
6165 static int addrconf_sysctl_addr_gen_mode(struct ctl_table
*ctl
, int write
,
6166 void __user
*buffer
, size_t *lenp
,
6171 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
6172 struct net
*net
= (struct net
*)ctl
->extra2
;
6173 struct ctl_table tmp
= {
6175 .maxlen
= sizeof(new_val
),
6179 if (!rtnl_trylock())
6180 return restart_syscall();
6182 new_val
= *((u32
*)ctl
->data
);
6184 ret
= proc_douintvec(&tmp
, write
, buffer
, lenp
, ppos
);
6189 if (check_addr_gen_mode(new_val
) < 0) {
6195 if (check_stable_privacy(idev
, net
, new_val
) < 0) {
6200 if (idev
->cnf
.addr_gen_mode
!= new_val
) {
6201 idev
->cnf
.addr_gen_mode
= new_val
;
6202 addrconf_dev_config(idev
->dev
);
6204 } else if (&net
->ipv6
.devconf_all
->addr_gen_mode
== ctl
->data
) {
6205 struct net_device
*dev
;
6207 net
->ipv6
.devconf_dflt
->addr_gen_mode
= new_val
;
6208 for_each_netdev(net
, dev
) {
6209 idev
= __in6_dev_get(dev
);
6211 idev
->cnf
.addr_gen_mode
!= new_val
) {
6212 idev
->cnf
.addr_gen_mode
= new_val
;
6213 addrconf_dev_config(idev
->dev
);
6218 *((u32
*)ctl
->data
) = new_val
;
6227 static int addrconf_sysctl_stable_secret(struct ctl_table
*ctl
, int write
,
6228 void __user
*buffer
, size_t *lenp
,
6232 struct in6_addr addr
;
6233 char str
[IPV6_MAX_STRLEN
];
6234 struct ctl_table lctl
= *ctl
;
6235 struct net
*net
= ctl
->extra2
;
6236 struct ipv6_stable_secret
*secret
= ctl
->data
;
6238 if (&net
->ipv6
.devconf_all
->stable_secret
== ctl
->data
)
6241 lctl
.maxlen
= IPV6_MAX_STRLEN
;
6244 if (!rtnl_trylock())
6245 return restart_syscall();
6247 if (!write
&& !secret
->initialized
) {
6252 err
= snprintf(str
, sizeof(str
), "%pI6", &secret
->secret
);
6253 if (err
>= sizeof(str
)) {
6258 err
= proc_dostring(&lctl
, write
, buffer
, lenp
, ppos
);
6262 if (in6_pton(str
, -1, addr
.in6_u
.u6_addr8
, -1, NULL
) != 1) {
6267 secret
->initialized
= true;
6268 secret
->secret
= addr
;
6270 if (&net
->ipv6
.devconf_dflt
->stable_secret
== ctl
->data
) {
6271 struct net_device
*dev
;
6273 for_each_netdev(net
, dev
) {
6274 struct inet6_dev
*idev
= __in6_dev_get(dev
);
6277 idev
->cnf
.addr_gen_mode
=
6278 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
6282 struct inet6_dev
*idev
= ctl
->extra1
;
6284 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
6294 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table
*ctl
,
6296 void __user
*buffer
,
6300 int *valp
= ctl
->data
;
6303 struct ctl_table lctl
;
6306 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6307 * we should not modify it until we get the rtnl lock.
6312 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6315 ret
= addrconf_fixup_linkdown(ctl
, valp
, val
);
6322 void addrconf_set_nopolicy(struct rt6_info
*rt
, int action
)
6326 rt
->dst
.flags
|= DST_NOPOLICY
;
6328 rt
->dst
.flags
&= ~DST_NOPOLICY
;
6333 void addrconf_disable_policy_idev(struct inet6_dev
*idev
, int val
)
6335 struct inet6_ifaddr
*ifa
;
6337 read_lock_bh(&idev
->lock
);
6338 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
6339 spin_lock(&ifa
->lock
);
6341 struct fib6_info
*rt
= ifa
->rt
;
6345 ifa
->rt
->dst_nopolicy
= val
? true : false;
6346 if (rt
->rt6i_pcpu
) {
6347 for_each_possible_cpu(cpu
) {
6348 struct rt6_info
**rtp
;
6350 rtp
= per_cpu_ptr(rt
->rt6i_pcpu
, cpu
);
6351 addrconf_set_nopolicy(*rtp
, val
);
6356 spin_unlock(&ifa
->lock
);
6358 read_unlock_bh(&idev
->lock
);
6362 int addrconf_disable_policy(struct ctl_table
*ctl
, int *valp
, int val
)
6364 struct inet6_dev
*idev
;
6367 if (!rtnl_trylock())
6368 return restart_syscall();
6372 net
= (struct net
*)ctl
->extra2
;
6373 if (valp
== &net
->ipv6
.devconf_dflt
->disable_policy
) {
6378 if (valp
== &net
->ipv6
.devconf_all
->disable_policy
) {
6379 struct net_device
*dev
;
6381 for_each_netdev(net
, dev
) {
6382 idev
= __in6_dev_get(dev
);
6384 addrconf_disable_policy_idev(idev
, val
);
6387 idev
= (struct inet6_dev
*)ctl
->extra1
;
6388 addrconf_disable_policy_idev(idev
, val
);
6396 int addrconf_sysctl_disable_policy(struct ctl_table
*ctl
, int write
,
6397 void __user
*buffer
, size_t *lenp
,
6400 int *valp
= ctl
->data
;
6403 struct ctl_table lctl
;
6408 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6410 if (write
&& (*valp
!= val
))
6411 ret
= addrconf_disable_policy(ctl
, valp
, val
);
6419 static int minus_one
= -1;
6420 static const int zero
= 0;
6421 static const int one
= 1;
6422 static const int two_five_five
= 255;
6424 static const struct ctl_table addrconf_sysctl
[] = {
6426 .procname
= "forwarding",
6427 .data
= &ipv6_devconf
.forwarding
,
6428 .maxlen
= sizeof(int),
6430 .proc_handler
= addrconf_sysctl_forward
,
6433 .procname
= "hop_limit",
6434 .data
= &ipv6_devconf
.hop_limit
,
6435 .maxlen
= sizeof(int),
6437 .proc_handler
= proc_dointvec_minmax
,
6438 .extra1
= (void *)&one
,
6439 .extra2
= (void *)&two_five_five
,
6443 .data
= &ipv6_devconf
.mtu6
,
6444 .maxlen
= sizeof(int),
6446 .proc_handler
= addrconf_sysctl_mtu
,
6449 .procname
= "accept_ra",
6450 .data
= &ipv6_devconf
.accept_ra
,
6451 .maxlen
= sizeof(int),
6453 .proc_handler
= proc_dointvec
,
6456 .procname
= "accept_redirects",
6457 .data
= &ipv6_devconf
.accept_redirects
,
6458 .maxlen
= sizeof(int),
6460 .proc_handler
= proc_dointvec
,
6463 .procname
= "autoconf",
6464 .data
= &ipv6_devconf
.autoconf
,
6465 .maxlen
= sizeof(int),
6467 .proc_handler
= proc_dointvec
,
6470 .procname
= "dad_transmits",
6471 .data
= &ipv6_devconf
.dad_transmits
,
6472 .maxlen
= sizeof(int),
6474 .proc_handler
= proc_dointvec
,
6477 .procname
= "router_solicitations",
6478 .data
= &ipv6_devconf
.rtr_solicits
,
6479 .maxlen
= sizeof(int),
6481 .proc_handler
= proc_dointvec_minmax
,
6482 .extra1
= &minus_one
,
6485 .procname
= "router_solicitation_interval",
6486 .data
= &ipv6_devconf
.rtr_solicit_interval
,
6487 .maxlen
= sizeof(int),
6489 .proc_handler
= proc_dointvec_jiffies
,
6492 .procname
= "router_solicitation_max_interval",
6493 .data
= &ipv6_devconf
.rtr_solicit_max_interval
,
6494 .maxlen
= sizeof(int),
6496 .proc_handler
= proc_dointvec_jiffies
,
6499 .procname
= "router_solicitation_delay",
6500 .data
= &ipv6_devconf
.rtr_solicit_delay
,
6501 .maxlen
= sizeof(int),
6503 .proc_handler
= proc_dointvec_jiffies
,
6506 .procname
= "force_mld_version",
6507 .data
= &ipv6_devconf
.force_mld_version
,
6508 .maxlen
= sizeof(int),
6510 .proc_handler
= proc_dointvec
,
6513 .procname
= "mldv1_unsolicited_report_interval",
6515 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
6516 .maxlen
= sizeof(int),
6518 .proc_handler
= proc_dointvec_ms_jiffies
,
6521 .procname
= "mldv2_unsolicited_report_interval",
6523 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
6524 .maxlen
= sizeof(int),
6526 .proc_handler
= proc_dointvec_ms_jiffies
,
6529 .procname
= "use_tempaddr",
6530 .data
= &ipv6_devconf
.use_tempaddr
,
6531 .maxlen
= sizeof(int),
6533 .proc_handler
= proc_dointvec
,
6536 .procname
= "temp_valid_lft",
6537 .data
= &ipv6_devconf
.temp_valid_lft
,
6538 .maxlen
= sizeof(int),
6540 .proc_handler
= proc_dointvec
,
6543 .procname
= "temp_prefered_lft",
6544 .data
= &ipv6_devconf
.temp_prefered_lft
,
6545 .maxlen
= sizeof(int),
6547 .proc_handler
= proc_dointvec
,
6550 .procname
= "regen_max_retry",
6551 .data
= &ipv6_devconf
.regen_max_retry
,
6552 .maxlen
= sizeof(int),
6554 .proc_handler
= proc_dointvec
,
6557 .procname
= "max_desync_factor",
6558 .data
= &ipv6_devconf
.max_desync_factor
,
6559 .maxlen
= sizeof(int),
6561 .proc_handler
= proc_dointvec
,
6564 .procname
= "max_addresses",
6565 .data
= &ipv6_devconf
.max_addresses
,
6566 .maxlen
= sizeof(int),
6568 .proc_handler
= proc_dointvec
,
6571 .procname
= "accept_ra_defrtr",
6572 .data
= &ipv6_devconf
.accept_ra_defrtr
,
6573 .maxlen
= sizeof(int),
6575 .proc_handler
= proc_dointvec
,
6578 .procname
= "accept_ra_min_hop_limit",
6579 .data
= &ipv6_devconf
.accept_ra_min_hop_limit
,
6580 .maxlen
= sizeof(int),
6582 .proc_handler
= proc_dointvec
,
6585 .procname
= "accept_ra_pinfo",
6586 .data
= &ipv6_devconf
.accept_ra_pinfo
,
6587 .maxlen
= sizeof(int),
6589 .proc_handler
= proc_dointvec
,
6591 #ifdef CONFIG_IPV6_ROUTER_PREF
6593 .procname
= "accept_ra_rtr_pref",
6594 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
6595 .maxlen
= sizeof(int),
6597 .proc_handler
= proc_dointvec
,
6600 .procname
= "router_probe_interval",
6601 .data
= &ipv6_devconf
.rtr_probe_interval
,
6602 .maxlen
= sizeof(int),
6604 .proc_handler
= proc_dointvec_jiffies
,
6606 #ifdef CONFIG_IPV6_ROUTE_INFO
6608 .procname
= "accept_ra_rt_info_min_plen",
6609 .data
= &ipv6_devconf
.accept_ra_rt_info_min_plen
,
6610 .maxlen
= sizeof(int),
6612 .proc_handler
= proc_dointvec
,
6615 .procname
= "accept_ra_rt_info_max_plen",
6616 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
6617 .maxlen
= sizeof(int),
6619 .proc_handler
= proc_dointvec
,
6624 .procname
= "proxy_ndp",
6625 .data
= &ipv6_devconf
.proxy_ndp
,
6626 .maxlen
= sizeof(int),
6628 .proc_handler
= addrconf_sysctl_proxy_ndp
,
6631 .procname
= "accept_source_route",
6632 .data
= &ipv6_devconf
.accept_source_route
,
6633 .maxlen
= sizeof(int),
6635 .proc_handler
= proc_dointvec
,
6637 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6639 .procname
= "optimistic_dad",
6640 .data
= &ipv6_devconf
.optimistic_dad
,
6641 .maxlen
= sizeof(int),
6643 .proc_handler
= proc_dointvec
,
6646 .procname
= "use_optimistic",
6647 .data
= &ipv6_devconf
.use_optimistic
,
6648 .maxlen
= sizeof(int),
6650 .proc_handler
= proc_dointvec
,
6653 #ifdef CONFIG_IPV6_MROUTE
6655 .procname
= "mc_forwarding",
6656 .data
= &ipv6_devconf
.mc_forwarding
,
6657 .maxlen
= sizeof(int),
6659 .proc_handler
= proc_dointvec
,
6663 .procname
= "disable_ipv6",
6664 .data
= &ipv6_devconf
.disable_ipv6
,
6665 .maxlen
= sizeof(int),
6667 .proc_handler
= addrconf_sysctl_disable
,
6670 .procname
= "accept_dad",
6671 .data
= &ipv6_devconf
.accept_dad
,
6672 .maxlen
= sizeof(int),
6674 .proc_handler
= proc_dointvec
,
6677 .procname
= "force_tllao",
6678 .data
= &ipv6_devconf
.force_tllao
,
6679 .maxlen
= sizeof(int),
6681 .proc_handler
= proc_dointvec
6684 .procname
= "ndisc_notify",
6685 .data
= &ipv6_devconf
.ndisc_notify
,
6686 .maxlen
= sizeof(int),
6688 .proc_handler
= proc_dointvec
6691 .procname
= "suppress_frag_ndisc",
6692 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
6693 .maxlen
= sizeof(int),
6695 .proc_handler
= proc_dointvec
6698 .procname
= "accept_ra_from_local",
6699 .data
= &ipv6_devconf
.accept_ra_from_local
,
6700 .maxlen
= sizeof(int),
6702 .proc_handler
= proc_dointvec
,
6705 .procname
= "accept_ra_mtu",
6706 .data
= &ipv6_devconf
.accept_ra_mtu
,
6707 .maxlen
= sizeof(int),
6709 .proc_handler
= proc_dointvec
,
6712 .procname
= "stable_secret",
6713 .data
= &ipv6_devconf
.stable_secret
,
6714 .maxlen
= IPV6_MAX_STRLEN
,
6716 .proc_handler
= addrconf_sysctl_stable_secret
,
6719 .procname
= "use_oif_addrs_only",
6720 .data
= &ipv6_devconf
.use_oif_addrs_only
,
6721 .maxlen
= sizeof(int),
6723 .proc_handler
= proc_dointvec
,
6726 .procname
= "ignore_routes_with_linkdown",
6727 .data
= &ipv6_devconf
.ignore_routes_with_linkdown
,
6728 .maxlen
= sizeof(int),
6730 .proc_handler
= addrconf_sysctl_ignore_routes_with_linkdown
,
6733 .procname
= "drop_unicast_in_l2_multicast",
6734 .data
= &ipv6_devconf
.drop_unicast_in_l2_multicast
,
6735 .maxlen
= sizeof(int),
6737 .proc_handler
= proc_dointvec
,
6740 .procname
= "drop_unsolicited_na",
6741 .data
= &ipv6_devconf
.drop_unsolicited_na
,
6742 .maxlen
= sizeof(int),
6744 .proc_handler
= proc_dointvec
,
6747 .procname
= "keep_addr_on_down",
6748 .data
= &ipv6_devconf
.keep_addr_on_down
,
6749 .maxlen
= sizeof(int),
6751 .proc_handler
= proc_dointvec
,
6755 .procname
= "seg6_enabled",
6756 .data
= &ipv6_devconf
.seg6_enabled
,
6757 .maxlen
= sizeof(int),
6759 .proc_handler
= proc_dointvec
,
6761 #ifdef CONFIG_IPV6_SEG6_HMAC
6763 .procname
= "seg6_require_hmac",
6764 .data
= &ipv6_devconf
.seg6_require_hmac
,
6765 .maxlen
= sizeof(int),
6767 .proc_handler
= proc_dointvec
,
6771 .procname
= "enhanced_dad",
6772 .data
= &ipv6_devconf
.enhanced_dad
,
6773 .maxlen
= sizeof(int),
6775 .proc_handler
= proc_dointvec
,
6778 .procname
= "addr_gen_mode",
6779 .data
= &ipv6_devconf
.addr_gen_mode
,
6780 .maxlen
= sizeof(int),
6782 .proc_handler
= addrconf_sysctl_addr_gen_mode
,
6785 .procname
= "disable_policy",
6786 .data
= &ipv6_devconf
.disable_policy
,
6787 .maxlen
= sizeof(int),
6789 .proc_handler
= addrconf_sysctl_disable_policy
,
6792 .procname
= "ndisc_tclass",
6793 .data
= &ipv6_devconf
.ndisc_tclass
,
6794 .maxlen
= sizeof(int),
6796 .proc_handler
= proc_dointvec_minmax
,
6797 .extra1
= (void *)&zero
,
6798 .extra2
= (void *)&two_five_five
,
6805 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
6806 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
6809 struct ctl_table
*table
;
6810 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
6812 table
= kmemdup(addrconf_sysctl
, sizeof(addrconf_sysctl
), GFP_KERNEL
);
6816 for (i
= 0; table
[i
].data
; i
++) {
6817 table
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
6818 /* If one of these is already set, then it is not safe to
6819 * overwrite either of them: this makes proc_dointvec_minmax
6822 if (!table
[i
].extra1
&& !table
[i
].extra2
) {
6823 table
[i
].extra1
= idev
; /* embedded; no ref */
6824 table
[i
].extra2
= net
;
6828 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
6830 p
->sysctl_header
= register_net_sysctl(net
, path
, table
);
6831 if (!p
->sysctl_header
)
6834 if (!strcmp(dev_name
, "all"))
6835 ifindex
= NETCONFA_IFINDEX_ALL
;
6836 else if (!strcmp(dev_name
, "default"))
6837 ifindex
= NETCONFA_IFINDEX_DEFAULT
;
6839 ifindex
= idev
->dev
->ifindex
;
6840 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
, NETCONFA_ALL
,
6850 static void __addrconf_sysctl_unregister(struct net
*net
,
6851 struct ipv6_devconf
*p
, int ifindex
)
6853 struct ctl_table
*table
;
6855 if (!p
->sysctl_header
)
6858 table
= p
->sysctl_header
->ctl_table_arg
;
6859 unregister_net_sysctl_table(p
->sysctl_header
);
6860 p
->sysctl_header
= NULL
;
6863 inet6_netconf_notify_devconf(net
, RTM_DELNETCONF
, 0, ifindex
, NULL
);
6866 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
6870 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
6873 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
6874 &ndisc_ifinfo_sysctl_change
);
6877 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
6880 neigh_sysctl_unregister(idev
->nd_parms
);
6885 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
6887 __addrconf_sysctl_unregister(dev_net(idev
->dev
), &idev
->cnf
,
6888 idev
->dev
->ifindex
);
6889 neigh_sysctl_unregister(idev
->nd_parms
);
6895 static int __net_init
addrconf_init_net(struct net
*net
)
6898 struct ipv6_devconf
*all
, *dflt
;
6900 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
6904 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
6906 goto err_alloc_dflt
;
6908 if (IS_ENABLED(CONFIG_SYSCTL
) &&
6909 sysctl_devconf_inherit_init_net
== 1 && !net_eq(net
, &init_net
)) {
6910 memcpy(all
, init_net
.ipv6
.devconf_all
, sizeof(ipv6_devconf
));
6911 memcpy(dflt
, init_net
.ipv6
.devconf_dflt
, sizeof(ipv6_devconf_dflt
));
6914 /* these will be inherited by all namespaces */
6915 dflt
->autoconf
= ipv6_defaults
.autoconf
;
6916 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
6918 dflt
->stable_secret
.initialized
= false;
6919 all
->stable_secret
.initialized
= false;
6921 net
->ipv6
.devconf_all
= all
;
6922 net
->ipv6
.devconf_dflt
= dflt
;
6924 #ifdef CONFIG_SYSCTL
6925 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
6929 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
6935 #ifdef CONFIG_SYSCTL
6937 __addrconf_sysctl_unregister(net
, all
, NETCONFA_IFINDEX_ALL
);
6947 static void __net_exit
addrconf_exit_net(struct net
*net
)
6949 #ifdef CONFIG_SYSCTL
6950 __addrconf_sysctl_unregister(net
, net
->ipv6
.devconf_dflt
,
6951 NETCONFA_IFINDEX_DEFAULT
);
6952 __addrconf_sysctl_unregister(net
, net
->ipv6
.devconf_all
,
6953 NETCONFA_IFINDEX_ALL
);
6955 kfree(net
->ipv6
.devconf_dflt
);
6956 kfree(net
->ipv6
.devconf_all
);
6959 static struct pernet_operations addrconf_ops
= {
6960 .init
= addrconf_init_net
,
6961 .exit
= addrconf_exit_net
,
6964 static struct rtnl_af_ops inet6_ops __read_mostly
= {
6966 .fill_link_af
= inet6_fill_link_af
,
6967 .get_link_af_size
= inet6_get_link_af_size
,
6968 .validate_link_af
= inet6_validate_link_af
,
6969 .set_link_af
= inet6_set_link_af
,
6973 * Init / cleanup code
6976 int __init
addrconf_init(void)
6978 struct inet6_dev
*idev
;
6981 err
= ipv6_addr_label_init();
6983 pr_crit("%s: cannot initialize default policy table: %d\n",
6988 err
= register_pernet_subsys(&addrconf_ops
);
6992 addrconf_wq
= create_workqueue("ipv6_addrconf");
6998 /* The addrconf netdev notifier requires that loopback_dev
6999 * has it's ipv6 private information allocated and setup
7000 * before it can bring up and give link-local addresses
7001 * to other devices which are up.
7003 * Unfortunately, loopback_dev is not necessarily the first
7004 * entry in the global dev_base list of net devices. In fact,
7005 * it is likely to be the very last entry on that list.
7006 * So this causes the notifier registry below to try and
7007 * give link-local addresses to all devices besides loopback_dev
7008 * first, then loopback_dev, which cases all the non-loopback_dev
7009 * devices to fail to get a link-local address.
7011 * So, as a temporary fix, allocate the ipv6 structure for
7012 * loopback_dev first by hand.
7013 * Longer term, all of the dependencies ipv6 has upon the loopback
7014 * device and it being up should be removed.
7017 idev
= ipv6_add_dev(init_net
.loopback_dev
);
7020 err
= PTR_ERR(idev
);
7024 ip6_route_init_special_entries();
7026 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
7027 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
7029 register_netdevice_notifier(&ipv6_dev_notf
);
7033 rtnl_af_register(&inet6_ops
);
7035 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETLINK
,
7036 NULL
, inet6_dump_ifinfo
, 0);
7040 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_NEWADDR
,
7041 inet6_rtm_newaddr
, NULL
, 0);
7044 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_DELADDR
,
7045 inet6_rtm_deladdr
, NULL
, 0);
7048 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETADDR
,
7049 inet6_rtm_getaddr
, inet6_dump_ifaddr
,
7050 RTNL_FLAG_DOIT_UNLOCKED
);
7053 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETMULTICAST
,
7054 NULL
, inet6_dump_ifmcaddr
, 0);
7057 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETANYCAST
,
7058 NULL
, inet6_dump_ifacaddr
, 0);
7061 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETNETCONF
,
7062 inet6_netconf_get_devconf
,
7063 inet6_netconf_dump_devconf
,
7064 RTNL_FLAG_DOIT_UNLOCKED
);
7067 err
= ipv6_addr_label_rtnl_register();
7073 rtnl_unregister_all(PF_INET6
);
7074 rtnl_af_unregister(&inet6_ops
);
7075 unregister_netdevice_notifier(&ipv6_dev_notf
);
7077 destroy_workqueue(addrconf_wq
);
7079 unregister_pernet_subsys(&addrconf_ops
);
7081 ipv6_addr_label_cleanup();
7086 void addrconf_cleanup(void)
7088 struct net_device
*dev
;
7091 unregister_netdevice_notifier(&ipv6_dev_notf
);
7092 unregister_pernet_subsys(&addrconf_ops
);
7093 ipv6_addr_label_cleanup();
7095 rtnl_af_unregister(&inet6_ops
);
7099 /* clean dev list */
7100 for_each_netdev(&init_net
, dev
) {
7101 if (__in6_dev_get(dev
) == NULL
)
7103 addrconf_ifdown(dev
, 1);
7105 addrconf_ifdown(init_net
.loopback_dev
, 2);
7110 spin_lock_bh(&addrconf_hash_lock
);
7111 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
7112 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
7113 spin_unlock_bh(&addrconf_hash_lock
);
7114 cancel_delayed_work(&addr_chk_work
);
7117 destroy_workqueue(addrconf_wq
);