2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
61 #include <linux/sysctl.h>
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
73 #include <net/6lowpan.h>
74 #include <net/firewire.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <net/l3mdev.h>
85 #include <linux/if_tunnel.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/netconf.h>
88 #include <linux/random.h>
89 #include <linux/uaccess.h>
90 #include <asm/unaligned.h>
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/export.h>
96 /* Set to 3 to get tracing... */
100 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
102 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
105 #define INFINITY_LIFE_TIME 0xFFFFFFFF
107 #define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110 static inline u32
cstamp_delta(unsigned long cstamp
)
112 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
115 static inline s32
rfc3315_s14_backoff_init(s32 irt
)
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp
= (900000 + prandom_u32() % 200001) * (u64
)irt
;
119 do_div(tmp
, 1000000);
123 static inline s32
rfc3315_s14_backoff_update(s32 rt
, s32 mrt
)
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp
= (1900000 + prandom_u32() % 200001) * (u64
)rt
;
127 do_div(tmp
, 1000000);
128 if ((s32
)tmp
> mrt
) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp
= (900000 + prandom_u32() % 200001) * (u64
)mrt
;
131 do_div(tmp
, 1000000);
137 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
138 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
140 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
145 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
150 static void ipv6_regen_rndid(struct inet6_dev
*idev
);
151 static void ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
153 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
154 static int ipv6_count_addresses(struct inet6_dev
*idev
);
155 static int ipv6_generate_stable_address(struct in6_addr
*addr
,
157 const struct inet6_dev
*idev
);
160 * Configured unicast address hash table
162 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
163 static DEFINE_SPINLOCK(addrconf_hash_lock
);
165 static void addrconf_verify(void);
166 static void addrconf_verify_rtnl(void);
167 static void addrconf_verify_work(struct work_struct
*);
169 static struct workqueue_struct
*addrconf_wq
;
170 static DECLARE_DELAYED_WORK(addr_chk_work
, addrconf_verify_work
);
172 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
173 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
175 static void addrconf_type_change(struct net_device
*dev
,
176 unsigned long event
);
177 static int addrconf_ifdown(struct net_device
*dev
, int how
);
179 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
181 const struct net_device
*dev
,
182 u32 flags
, u32 noflags
);
184 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
185 static void addrconf_dad_work(struct work_struct
*w
);
186 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
);
187 static void addrconf_dad_run(struct inet6_dev
*idev
);
188 static void addrconf_rs_timer(unsigned long data
);
189 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
190 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
192 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
193 struct prefix_info
*pinfo
);
194 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
195 struct net_device
*dev
);
197 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
199 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
200 .mtu6
= IPV6_MIN_MTU
,
202 .accept_redirects
= 1,
204 .force_mld_version
= 0,
205 .mldv1_unsolicited_report_interval
= 10 * HZ
,
206 .mldv2_unsolicited_report_interval
= HZ
,
208 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
209 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
210 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
211 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
213 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
214 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
215 .regen_max_retry
= REGEN_MAX_RETRY
,
216 .max_desync_factor
= MAX_DESYNC_FACTOR
,
217 .max_addresses
= IPV6_MAX_ADDRESSES
,
218 .accept_ra_defrtr
= 1,
219 .accept_ra_from_local
= 0,
220 .accept_ra_min_hop_limit
= 1,
221 .accept_ra_pinfo
= 1,
222 #ifdef CONFIG_IPV6_ROUTER_PREF
223 .accept_ra_rtr_pref
= 1,
224 .rtr_probe_interval
= 60 * HZ
,
225 #ifdef CONFIG_IPV6_ROUTE_INFO
226 .accept_ra_rt_info_max_plen
= 0,
230 .accept_source_route
= 0, /* we do not accept RH0 by default. */
233 .suppress_frag_ndisc
= 1,
236 .initialized
= false,
238 .use_oif_addrs_only
= 0,
239 .ignore_routes_with_linkdown
= 0,
240 .keep_addr_on_down
= 0,
242 #ifdef CONFIG_IPV6_SEG6_HMAC
243 .seg6_require_hmac
= 0,
246 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
249 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
251 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
252 .mtu6
= IPV6_MIN_MTU
,
254 .accept_redirects
= 1,
256 .force_mld_version
= 0,
257 .mldv1_unsolicited_report_interval
= 10 * HZ
,
258 .mldv2_unsolicited_report_interval
= HZ
,
260 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
261 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
262 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
263 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
265 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
266 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
267 .regen_max_retry
= REGEN_MAX_RETRY
,
268 .max_desync_factor
= MAX_DESYNC_FACTOR
,
269 .max_addresses
= IPV6_MAX_ADDRESSES
,
270 .accept_ra_defrtr
= 1,
271 .accept_ra_from_local
= 0,
272 .accept_ra_min_hop_limit
= 1,
273 .accept_ra_pinfo
= 1,
274 #ifdef CONFIG_IPV6_ROUTER_PREF
275 .accept_ra_rtr_pref
= 1,
276 .rtr_probe_interval
= 60 * HZ
,
277 #ifdef CONFIG_IPV6_ROUTE_INFO
278 .accept_ra_rt_info_max_plen
= 0,
282 .accept_source_route
= 0, /* we do not accept RH0 by default. */
285 .suppress_frag_ndisc
= 1,
288 .initialized
= false,
290 .use_oif_addrs_only
= 0,
291 .ignore_routes_with_linkdown
= 0,
292 .keep_addr_on_down
= 0,
294 #ifdef CONFIG_IPV6_SEG6_HMAC
295 .seg6_require_hmac
= 0,
298 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
301 /* Check if a valid qdisc is available */
302 static inline bool addrconf_qdisc_ok(const struct net_device
*dev
)
304 return !qdisc_tx_is_noop(dev
);
307 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
309 if (del_timer(&idev
->rs_timer
))
313 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
315 if (cancel_delayed_work(&ifp
->dad_work
))
319 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
322 if (!timer_pending(&idev
->rs_timer
))
324 mod_timer(&idev
->rs_timer
, jiffies
+ when
);
327 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
330 if (!delayed_work_pending(&ifp
->dad_work
))
332 mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
);
335 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
339 idev
->stats
.ipv6
= alloc_percpu(struct ipstats_mib
);
340 if (!idev
->stats
.ipv6
)
343 for_each_possible_cpu(i
) {
344 struct ipstats_mib
*addrconf_stats
;
345 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
346 u64_stats_init(&addrconf_stats
->syncp
);
350 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
352 if (!idev
->stats
.icmpv6dev
)
354 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
356 if (!idev
->stats
.icmpv6msgdev
)
362 kfree(idev
->stats
.icmpv6dev
);
364 free_percpu(idev
->stats
.ipv6
);
369 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
371 struct inet6_dev
*ndev
;
376 if (dev
->mtu
< IPV6_MIN_MTU
)
377 return ERR_PTR(-EINVAL
);
379 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
383 rwlock_init(&ndev
->lock
);
385 INIT_LIST_HEAD(&ndev
->addr_list
);
386 setup_timer(&ndev
->rs_timer
, addrconf_rs_timer
,
387 (unsigned long)ndev
);
388 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
390 if (ndev
->cnf
.stable_secret
.initialized
)
391 ndev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
393 ndev
->cnf
.addr_gen_mode
= ipv6_devconf_dflt
.addr_gen_mode
;
395 ndev
->cnf
.mtu6
= dev
->mtu
;
396 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
397 if (!ndev
->nd_parms
) {
401 if (ndev
->cnf
.forwarding
)
402 dev_disable_lro(dev
);
403 /* We refer to the device */
406 if (snmp6_alloc_dev(ndev
) < 0) {
408 "%s: cannot allocate memory for statistics; dev=%s.\n",
409 __func__
, dev
->name
);
410 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
416 if (snmp6_register_dev(ndev
) < 0) {
418 "%s: cannot create /proc/net/dev_snmp6/%s\n",
419 __func__
, dev
->name
);
423 /* One reference from device. */
426 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
427 ndev
->cnf
.accept_dad
= -1;
429 #if IS_ENABLED(CONFIG_IPV6_SIT)
430 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
431 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
432 ndev
->cnf
.rtr_solicits
= 0;
436 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
437 ndev
->desync_factor
= U32_MAX
;
438 if ((dev
->flags
&IFF_LOOPBACK
) ||
439 dev
->type
== ARPHRD_TUNNEL
||
440 dev
->type
== ARPHRD_TUNNEL6
||
441 dev
->type
== ARPHRD_SIT
||
442 dev
->type
== ARPHRD_NONE
) {
443 ndev
->cnf
.use_tempaddr
= -1;
445 ipv6_regen_rndid(ndev
);
447 ndev
->token
= in6addr_any
;
449 if (netif_running(dev
) && addrconf_qdisc_ok(dev
))
450 ndev
->if_flags
|= IF_READY
;
452 ipv6_mc_init_dev(ndev
);
453 ndev
->tstamp
= jiffies
;
454 err
= addrconf_sysctl_register(ndev
);
456 ipv6_mc_destroy_dev(ndev
);
457 snmp6_unregister_dev(ndev
);
460 /* protected by rtnl_lock */
461 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
463 /* Join interface-local all-node multicast group */
464 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
466 /* Join all-node multicast group */
467 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
469 /* Join all-router multicast group if forwarding is set */
470 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
471 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
476 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
478 in6_dev_finish_destroy(ndev
);
482 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
484 struct inet6_dev
*idev
;
488 idev
= __in6_dev_get(dev
);
490 idev
= ipv6_add_dev(dev
);
495 if (dev
->flags
&IFF_UP
)
500 static int inet6_netconf_msgsize_devconf(int type
)
502 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
503 + nla_total_size(4); /* NETCONFA_IFINDEX */
506 if (type
== NETCONFA_ALL
)
509 if (all
|| type
== NETCONFA_FORWARDING
)
510 size
+= nla_total_size(4);
511 #ifdef CONFIG_IPV6_MROUTE
512 if (all
|| type
== NETCONFA_MC_FORWARDING
)
513 size
+= nla_total_size(4);
515 if (all
|| type
== NETCONFA_PROXY_NEIGH
)
516 size
+= nla_total_size(4);
518 if (all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
)
519 size
+= nla_total_size(4);
524 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
525 struct ipv6_devconf
*devconf
, u32 portid
,
526 u32 seq
, int event
, unsigned int flags
,
529 struct nlmsghdr
*nlh
;
530 struct netconfmsg
*ncm
;
533 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
538 if (type
== NETCONFA_ALL
)
541 ncm
= nlmsg_data(nlh
);
542 ncm
->ncm_family
= AF_INET6
;
544 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
545 goto nla_put_failure
;
547 if ((all
|| type
== NETCONFA_FORWARDING
) &&
548 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
549 goto nla_put_failure
;
550 #ifdef CONFIG_IPV6_MROUTE
551 if ((all
|| type
== NETCONFA_MC_FORWARDING
) &&
552 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
553 devconf
->mc_forwarding
) < 0)
554 goto nla_put_failure
;
556 if ((all
|| type
== NETCONFA_PROXY_NEIGH
) &&
557 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
558 goto nla_put_failure
;
560 if ((all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
) &&
561 nla_put_s32(skb
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
562 devconf
->ignore_routes_with_linkdown
) < 0)
563 goto nla_put_failure
;
569 nlmsg_cancel(skb
, nlh
);
573 void inet6_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
574 struct ipv6_devconf
*devconf
)
579 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_KERNEL
);
583 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
584 RTM_NEWNETCONF
, 0, type
);
586 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
587 WARN_ON(err
== -EMSGSIZE
);
591 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_KERNEL
);
594 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
597 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
598 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
599 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
600 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
601 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
] = { .len
= sizeof(int) },
604 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
605 struct nlmsghdr
*nlh
)
607 struct net
*net
= sock_net(in_skb
->sk
);
608 struct nlattr
*tb
[NETCONFA_MAX
+1];
609 struct netconfmsg
*ncm
;
611 struct ipv6_devconf
*devconf
;
612 struct inet6_dev
*in6_dev
;
613 struct net_device
*dev
;
617 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
618 devconf_ipv6_policy
);
623 if (!tb
[NETCONFA_IFINDEX
])
626 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
628 case NETCONFA_IFINDEX_ALL
:
629 devconf
= net
->ipv6
.devconf_all
;
631 case NETCONFA_IFINDEX_DEFAULT
:
632 devconf
= net
->ipv6
.devconf_dflt
;
635 dev
= __dev_get_by_index(net
, ifindex
);
638 in6_dev
= __in6_dev_get(dev
);
641 devconf
= &in6_dev
->cnf
;
646 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_ATOMIC
);
650 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
651 NETLINK_CB(in_skb
).portid
,
652 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
655 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
656 WARN_ON(err
== -EMSGSIZE
);
660 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
665 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
666 struct netlink_callback
*cb
)
668 struct net
*net
= sock_net(skb
->sk
);
671 struct net_device
*dev
;
672 struct inet6_dev
*idev
;
673 struct hlist_head
*head
;
676 s_idx
= idx
= cb
->args
[1];
678 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
680 head
= &net
->dev_index_head
[h
];
682 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
684 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
687 idev
= __in6_dev_get(dev
);
691 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
693 NETLINK_CB(cb
->skb
).portid
,
701 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
707 if (h
== NETDEV_HASHENTRIES
) {
708 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
709 net
->ipv6
.devconf_all
,
710 NETLINK_CB(cb
->skb
).portid
,
712 RTM_NEWNETCONF
, NLM_F_MULTI
,
718 if (h
== NETDEV_HASHENTRIES
+ 1) {
719 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
720 net
->ipv6
.devconf_dflt
,
721 NETLINK_CB(cb
->skb
).portid
,
723 RTM_NEWNETCONF
, NLM_F_MULTI
,
737 static void dev_forward_change(struct inet6_dev
*idev
)
739 struct net_device
*dev
;
740 struct inet6_ifaddr
*ifa
;
745 if (idev
->cnf
.forwarding
)
746 dev_disable_lro(dev
);
747 if (dev
->flags
& IFF_MULTICAST
) {
748 if (idev
->cnf
.forwarding
) {
749 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
750 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
751 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
753 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
754 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
755 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
759 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
760 if (ifa
->flags
&IFA_F_TENTATIVE
)
762 if (idev
->cnf
.forwarding
)
763 addrconf_join_anycast(ifa
);
765 addrconf_leave_anycast(ifa
);
767 inet6_netconf_notify_devconf(dev_net(dev
), NETCONFA_FORWARDING
,
768 dev
->ifindex
, &idev
->cnf
);
772 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
774 struct net_device
*dev
;
775 struct inet6_dev
*idev
;
777 for_each_netdev(net
, dev
) {
778 idev
= __in6_dev_get(dev
);
780 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
781 idev
->cnf
.forwarding
= newf
;
783 dev_forward_change(idev
);
788 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
794 return restart_syscall();
796 net
= (struct net
*)table
->extra2
;
800 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
801 if ((!newf
) ^ (!old
))
802 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
803 NETCONFA_IFINDEX_DEFAULT
,
804 net
->ipv6
.devconf_dflt
);
809 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
810 int old_dflt
= net
->ipv6
.devconf_dflt
->forwarding
;
812 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
813 if ((!newf
) ^ (!old_dflt
))
814 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
815 NETCONFA_IFINDEX_DEFAULT
,
816 net
->ipv6
.devconf_dflt
);
818 addrconf_forward_change(net
, newf
);
819 if ((!newf
) ^ (!old
))
820 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
821 NETCONFA_IFINDEX_ALL
,
822 net
->ipv6
.devconf_all
);
823 } else if ((!newf
) ^ (!old
))
824 dev_forward_change((struct inet6_dev
*)table
->extra1
);
828 rt6_purge_dflt_routers(net
);
832 static void addrconf_linkdown_change(struct net
*net
, __s32 newf
)
834 struct net_device
*dev
;
835 struct inet6_dev
*idev
;
837 for_each_netdev(net
, dev
) {
838 idev
= __in6_dev_get(dev
);
840 int changed
= (!idev
->cnf
.ignore_routes_with_linkdown
) ^ (!newf
);
842 idev
->cnf
.ignore_routes_with_linkdown
= newf
;
844 inet6_netconf_notify_devconf(dev_net(dev
),
845 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
852 static int addrconf_fixup_linkdown(struct ctl_table
*table
, int *p
, int newf
)
858 return restart_syscall();
860 net
= (struct net
*)table
->extra2
;
864 if (p
== &net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
) {
865 if ((!newf
) ^ (!old
))
866 inet6_netconf_notify_devconf(net
,
867 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
868 NETCONFA_IFINDEX_DEFAULT
,
869 net
->ipv6
.devconf_dflt
);
874 if (p
== &net
->ipv6
.devconf_all
->ignore_routes_with_linkdown
) {
875 net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
= newf
;
876 addrconf_linkdown_change(net
, newf
);
877 if ((!newf
) ^ (!old
))
878 inet6_netconf_notify_devconf(net
,
879 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
880 NETCONFA_IFINDEX_ALL
,
881 net
->ipv6
.devconf_all
);
890 /* Nobody refers to this ifaddr, destroy it */
891 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
893 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
895 #ifdef NET_REFCNT_DEBUG
896 pr_debug("%s\n", __func__
);
899 in6_dev_put(ifp
->idev
);
901 if (cancel_delayed_work(&ifp
->dad_work
))
902 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
905 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
906 pr_warn("Freeing alive inet6 address %p\n", ifp
);
915 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
918 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
921 * Each device address list is sorted in order of scope -
922 * global before linklocal.
924 list_for_each(p
, &idev
->addr_list
) {
925 struct inet6_ifaddr
*ifa
926 = list_entry(p
, struct inet6_ifaddr
, if_list
);
927 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
931 list_add_tail(&ifp
->if_list
, p
);
934 static u32
inet6_addr_hash(const struct in6_addr
*addr
)
936 return hash_32(ipv6_addr_hash(addr
), IN6_ADDR_HSIZE_SHIFT
);
939 /* On success it returns ifp with increased reference count */
941 static struct inet6_ifaddr
*
942 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
943 const struct in6_addr
*peer_addr
, int pfxlen
,
944 int scope
, u32 flags
, u32 valid_lft
, u32 prefered_lft
)
946 struct inet6_ifaddr
*ifa
= NULL
;
950 int addr_type
= ipv6_addr_type(addr
);
952 if (addr_type
== IPV6_ADDR_ANY
||
953 addr_type
& IPV6_ADDR_MULTICAST
||
954 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
955 addr_type
& IPV6_ADDR_LOOPBACK
))
956 return ERR_PTR(-EADDRNOTAVAIL
);
960 err
= -ENODEV
; /*XXX*/
964 if (idev
->cnf
.disable_ipv6
) {
969 spin_lock(&addrconf_hash_lock
);
971 /* Ignore adding duplicate addresses on an interface */
972 if (ipv6_chk_same_addr(dev_net(idev
->dev
), addr
, idev
->dev
)) {
973 ADBG("ipv6_add_addr: already assigned\n");
978 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
981 ADBG("ipv6_add_addr: malloc failed\n");
986 rt
= addrconf_dst_alloc(idev
, addr
, false);
992 neigh_parms_data_state_setall(idev
->nd_parms
);
996 ifa
->peer_addr
= *peer_addr
;
998 spin_lock_init(&ifa
->lock
);
999 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
1000 INIT_HLIST_NODE(&ifa
->addr_lst
);
1002 ifa
->prefix_len
= pfxlen
;
1003 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
1004 ifa
->valid_lft
= valid_lft
;
1005 ifa
->prefered_lft
= prefered_lft
;
1006 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
1007 ifa
->tokenized
= false;
1016 /* Add to big hash table */
1017 hash
= inet6_addr_hash(addr
);
1019 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
1020 spin_unlock(&addrconf_hash_lock
);
1022 write_lock(&idev
->lock
);
1023 /* Add to inet6_dev unicast addr list. */
1024 ipv6_link_dev_addr(idev
, ifa
);
1026 if (ifa
->flags
&IFA_F_TEMPORARY
) {
1027 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
1032 write_unlock(&idev
->lock
);
1034 rcu_read_unlock_bh();
1036 if (likely(err
== 0))
1037 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
1045 spin_unlock(&addrconf_hash_lock
);
1049 enum cleanup_prefix_rt_t
{
1050 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
1051 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
1052 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
1056 * Check, whether the prefix for ifp would still need a prefix route
1057 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1060 * 1) we don't purge prefix if address was not permanent.
1061 * prefix is managed by its own lifetime.
1062 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1063 * 3) if there are no addresses, delete prefix.
1064 * 4) if there are still other permanent address(es),
1065 * corresponding prefix is still permanent.
1066 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1067 * don't purge the prefix, assume user space is managing it.
1068 * 6) otherwise, update prefix lifetime to the
1069 * longest valid lifetime among the corresponding
1070 * addresses on the device.
1071 * Note: subsequent RA will update lifetime.
1073 static enum cleanup_prefix_rt_t
1074 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
1076 struct inet6_ifaddr
*ifa
;
1077 struct inet6_dev
*idev
= ifp
->idev
;
1078 unsigned long lifetime
;
1079 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
1083 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1086 if (!ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
1089 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
1090 return CLEANUP_PREFIX_RT_NOP
;
1092 action
= CLEANUP_PREFIX_RT_EXPIRE
;
1094 spin_lock(&ifa
->lock
);
1096 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
1098 * Note: Because this address is
1099 * not permanent, lifetime <
1100 * LONG_MAX / HZ here.
1102 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
1103 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
1104 spin_unlock(&ifa
->lock
);
1111 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
, bool del_rt
)
1113 struct rt6_info
*rt
;
1115 rt
= addrconf_get_prefix_route(&ifp
->addr
,
1118 0, RTF_GATEWAY
| RTF_DEFAULT
);
1123 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
1124 rt6_set_expires(rt
, expires
);
1131 /* This function wants to get referenced ifp and releases it before return */
1133 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1136 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1137 unsigned long expires
;
1141 spin_lock_bh(&ifp
->lock
);
1143 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1144 spin_unlock_bh(&ifp
->lock
);
1146 if (state
== INET6_IFADDR_STATE_DEAD
)
1149 spin_lock_bh(&addrconf_hash_lock
);
1150 hlist_del_init_rcu(&ifp
->addr_lst
);
1151 spin_unlock_bh(&addrconf_hash_lock
);
1153 write_lock_bh(&ifp
->idev
->lock
);
1155 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1156 list_del(&ifp
->tmp_list
);
1158 in6_ifa_put(ifp
->ifpub
);
1164 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1165 action
= check_cleanup_prefix_route(ifp
, &expires
);
1167 list_del_init(&ifp
->if_list
);
1170 write_unlock_bh(&ifp
->idev
->lock
);
1172 addrconf_del_dad_work(ifp
);
1174 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1176 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1178 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1179 cleanup_prefix_route(ifp
, expires
,
1180 action
== CLEANUP_PREFIX_RT_DEL
);
1183 /* clean up prefsrc entries */
1184 rt6_remove_prefsrc(ifp
);
1189 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
1191 struct inet6_dev
*idev
= ifp
->idev
;
1192 struct in6_addr addr
, *tmpaddr
;
1193 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_tstamp
, age
;
1194 unsigned long regen_advance
;
1198 unsigned long now
= jiffies
;
1199 long max_desync_factor
;
1200 s32 cnf_temp_preferred_lft
;
1202 write_lock_bh(&idev
->lock
);
1204 spin_lock_bh(&ift
->lock
);
1205 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1206 spin_unlock_bh(&ift
->lock
);
1213 if (idev
->cnf
.use_tempaddr
<= 0) {
1214 write_unlock_bh(&idev
->lock
);
1215 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1220 spin_lock_bh(&ifp
->lock
);
1221 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1222 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1223 spin_unlock_bh(&ifp
->lock
);
1224 write_unlock_bh(&idev
->lock
);
1225 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1232 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1233 ipv6_try_regen_rndid(idev
, tmpaddr
);
1234 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1235 age
= (now
- ifp
->tstamp
) / HZ
;
1237 regen_advance
= idev
->cnf
.regen_max_retry
*
1238 idev
->cnf
.dad_transmits
*
1239 NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
) / HZ
;
1241 /* recalculate max_desync_factor each time and update
1242 * idev->desync_factor if it's larger
1244 cnf_temp_preferred_lft
= READ_ONCE(idev
->cnf
.temp_prefered_lft
);
1245 max_desync_factor
= min_t(__u32
,
1246 idev
->cnf
.max_desync_factor
,
1247 cnf_temp_preferred_lft
- regen_advance
);
1249 if (unlikely(idev
->desync_factor
> max_desync_factor
)) {
1250 if (max_desync_factor
> 0) {
1251 get_random_bytes(&idev
->desync_factor
,
1252 sizeof(idev
->desync_factor
));
1253 idev
->desync_factor
%= max_desync_factor
;
1255 idev
->desync_factor
= 0;
1259 tmp_valid_lft
= min_t(__u32
,
1261 idev
->cnf
.temp_valid_lft
+ age
);
1262 tmp_prefered_lft
= cnf_temp_preferred_lft
+ age
-
1263 idev
->desync_factor
;
1264 tmp_prefered_lft
= min_t(__u32
, ifp
->prefered_lft
, tmp_prefered_lft
);
1265 tmp_plen
= ifp
->prefix_len
;
1266 tmp_tstamp
= ifp
->tstamp
;
1267 spin_unlock_bh(&ifp
->lock
);
1269 write_unlock_bh(&idev
->lock
);
1271 /* A temporary address is created only if this calculated Preferred
1272 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1273 * an implementation must not create a temporary address with a zero
1274 * Preferred Lifetime.
1275 * Use age calculation as in addrconf_verify to avoid unnecessary
1276 * temporary addresses being generated.
1278 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1279 if (tmp_prefered_lft
<= regen_advance
+ age
) {
1286 addr_flags
= IFA_F_TEMPORARY
;
1287 /* set in addrconf_prefix_rcv() */
1288 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1289 addr_flags
|= IFA_F_OPTIMISTIC
;
1291 ift
= ipv6_add_addr(idev
, &addr
, NULL
, tmp_plen
,
1292 ipv6_addr_scope(&addr
), addr_flags
,
1293 tmp_valid_lft
, tmp_prefered_lft
);
1297 pr_info("%s: retry temporary address regeneration\n", __func__
);
1299 write_lock_bh(&idev
->lock
);
1303 spin_lock_bh(&ift
->lock
);
1306 ift
->tstamp
= tmp_tstamp
;
1307 spin_unlock_bh(&ift
->lock
);
1309 addrconf_dad_start(ift
);
1317 * Choose an appropriate source address (RFC3484)
1320 IPV6_SADDR_RULE_INIT
= 0,
1321 IPV6_SADDR_RULE_LOCAL
,
1322 IPV6_SADDR_RULE_SCOPE
,
1323 IPV6_SADDR_RULE_PREFERRED
,
1324 #ifdef CONFIG_IPV6_MIP6
1325 IPV6_SADDR_RULE_HOA
,
1327 IPV6_SADDR_RULE_OIF
,
1328 IPV6_SADDR_RULE_LABEL
,
1329 IPV6_SADDR_RULE_PRIVACY
,
1330 IPV6_SADDR_RULE_ORCHID
,
1331 IPV6_SADDR_RULE_PREFIX
,
1332 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1333 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1338 struct ipv6_saddr_score
{
1341 struct inet6_ifaddr
*ifa
;
1342 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1347 struct ipv6_saddr_dst
{
1348 const struct in6_addr
*addr
;
1355 static inline int ipv6_saddr_preferred(int type
)
1357 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1362 static inline bool ipv6_use_optimistic_addr(struct inet6_dev
*idev
)
1364 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1365 return idev
&& idev
->cnf
.optimistic_dad
&& idev
->cnf
.use_optimistic
;
1371 static int ipv6_get_saddr_eval(struct net
*net
,
1372 struct ipv6_saddr_score
*score
,
1373 struct ipv6_saddr_dst
*dst
,
1378 if (i
<= score
->rule
) {
1380 case IPV6_SADDR_RULE_SCOPE
:
1381 ret
= score
->scopedist
;
1383 case IPV6_SADDR_RULE_PREFIX
:
1384 ret
= score
->matchlen
;
1387 ret
= !!test_bit(i
, score
->scorebits
);
1393 case IPV6_SADDR_RULE_INIT
:
1394 /* Rule 0: remember if hiscore is not ready yet */
1397 case IPV6_SADDR_RULE_LOCAL
:
1398 /* Rule 1: Prefer same address */
1399 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1401 case IPV6_SADDR_RULE_SCOPE
:
1402 /* Rule 2: Prefer appropriate scope
1407 * ---+--+-+---> scope
1409 * | d is scope of the destination.
1411 * | \ <- smaller scope is better if
1412 * B-15 | \ if scope is enough for destination.
1413 * | ret = B - scope (-1 <= scope >= d <= 15).
1415 * |/ <- greater is better
1416 * -C / if scope is not enough for destination.
1417 * /| ret = scope - C (-1 <= d < scope <= 15).
1419 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1420 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1421 * Assume B = 0 and we get C > 29.
1423 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1424 if (ret
>= dst
->scope
)
1427 ret
-= 128; /* 30 is enough */
1428 score
->scopedist
= ret
;
1430 case IPV6_SADDR_RULE_PREFERRED
:
1432 /* Rule 3: Avoid deprecated and optimistic addresses */
1433 u8 avoid
= IFA_F_DEPRECATED
;
1435 if (!ipv6_use_optimistic_addr(score
->ifa
->idev
))
1436 avoid
|= IFA_F_OPTIMISTIC
;
1437 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1438 !(score
->ifa
->flags
& avoid
);
1441 #ifdef CONFIG_IPV6_MIP6
1442 case IPV6_SADDR_RULE_HOA
:
1444 /* Rule 4: Prefer home address */
1445 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1446 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1450 case IPV6_SADDR_RULE_OIF
:
1451 /* Rule 5: Prefer outgoing interface */
1452 ret
= (!dst
->ifindex
||
1453 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1455 case IPV6_SADDR_RULE_LABEL
:
1456 /* Rule 6: Prefer matching label */
1457 ret
= ipv6_addr_label(net
,
1458 &score
->ifa
->addr
, score
->addr_type
,
1459 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1461 case IPV6_SADDR_RULE_PRIVACY
:
1463 /* Rule 7: Prefer public address
1464 * Note: prefer temporary address if use_tempaddr >= 2
1466 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1467 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1468 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1469 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1472 case IPV6_SADDR_RULE_ORCHID
:
1473 /* Rule 8-: Prefer ORCHID vs ORCHID or
1474 * non-ORCHID vs non-ORCHID
1476 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1477 ipv6_addr_orchid(dst
->addr
));
1479 case IPV6_SADDR_RULE_PREFIX
:
1480 /* Rule 8: Use longest matching prefix */
1481 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1482 if (ret
> score
->ifa
->prefix_len
)
1483 ret
= score
->ifa
->prefix_len
;
1484 score
->matchlen
= ret
;
1486 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1487 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1488 /* Optimistic addresses still have lower precedence than other
1489 * preferred addresses.
1491 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1499 __set_bit(i
, score
->scorebits
);
1505 static int __ipv6_dev_get_saddr(struct net
*net
,
1506 struct ipv6_saddr_dst
*dst
,
1507 struct inet6_dev
*idev
,
1508 struct ipv6_saddr_score
*scores
,
1511 struct ipv6_saddr_score
*score
= &scores
[1 - hiscore_idx
], *hiscore
= &scores
[hiscore_idx
];
1513 read_lock_bh(&idev
->lock
);
1514 list_for_each_entry(score
->ifa
, &idev
->addr_list
, if_list
) {
1518 * - Tentative Address (RFC2462 section 5.4)
1519 * - A tentative address is not considered
1520 * "assigned to an interface" in the traditional
1521 * sense, unless it is also flagged as optimistic.
1522 * - Candidate Source Address (section 4)
1523 * - In any case, anycast addresses, multicast
1524 * addresses, and the unspecified address MUST
1525 * NOT be included in a candidate set.
1527 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1528 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1531 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1533 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1534 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1535 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1541 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1543 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1544 int minihiscore
, miniscore
;
1546 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, dst
, i
);
1547 miniscore
= ipv6_get_saddr_eval(net
, score
, dst
, i
);
1549 if (minihiscore
> miniscore
) {
1550 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1551 score
->scopedist
> 0) {
1554 * each remaining entry
1555 * has too small (not enough)
1556 * scope, because ifa entries
1557 * are sorted by their scope
1563 } else if (minihiscore
< miniscore
) {
1565 in6_ifa_put(hiscore
->ifa
);
1567 in6_ifa_hold(score
->ifa
);
1569 swap(hiscore
, score
);
1570 hiscore_idx
= 1 - hiscore_idx
;
1572 /* restore our iterator */
1573 score
->ifa
= hiscore
->ifa
;
1580 read_unlock_bh(&idev
->lock
);
1584 static int ipv6_get_saddr_master(struct net
*net
,
1585 const struct net_device
*dst_dev
,
1586 const struct net_device
*master
,
1587 struct ipv6_saddr_dst
*dst
,
1588 struct ipv6_saddr_score
*scores
,
1591 struct inet6_dev
*idev
;
1593 idev
= __in6_dev_get(dst_dev
);
1595 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1596 scores
, hiscore_idx
);
1598 idev
= __in6_dev_get(master
);
1600 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1601 scores
, hiscore_idx
);
1606 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1607 const struct in6_addr
*daddr
, unsigned int prefs
,
1608 struct in6_addr
*saddr
)
1610 struct ipv6_saddr_score scores
[2], *hiscore
;
1611 struct ipv6_saddr_dst dst
;
1612 struct inet6_dev
*idev
;
1613 struct net_device
*dev
;
1615 bool use_oif_addr
= false;
1616 int hiscore_idx
= 0;
1618 dst_type
= __ipv6_addr_type(daddr
);
1620 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1621 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1622 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1625 scores
[hiscore_idx
].rule
= -1;
1626 scores
[hiscore_idx
].ifa
= NULL
;
1630 /* Candidate Source Address (section 4)
1631 * - multicast and link-local destination address,
1632 * the set of candidate source address MUST only
1633 * include addresses assigned to interfaces
1634 * belonging to the same link as the outgoing
1636 * (- For site-local destination addresses, the
1637 * set of candidate source addresses MUST only
1638 * include addresses assigned to interfaces
1639 * belonging to the same site as the outgoing
1641 * - "It is RECOMMENDED that the candidate source addresses
1642 * be the set of unicast addresses assigned to the
1643 * interface that will be used to send to the destination
1644 * (the 'outgoing' interface)." (RFC 6724)
1647 idev
= __in6_dev_get(dst_dev
);
1648 if ((dst_type
& IPV6_ADDR_MULTICAST
) ||
1649 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
||
1650 (idev
&& idev
->cnf
.use_oif_addrs_only
)) {
1651 use_oif_addr
= true;
1657 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1659 const struct net_device
*master
;
1662 /* if dst_dev exists and is enslaved to an L3 device, then
1663 * prefer addresses from dst_dev and then the master over
1664 * any other enslaved devices in the L3 domain.
1666 master
= l3mdev_master_dev_rcu(dst_dev
);
1668 master_idx
= master
->ifindex
;
1670 hiscore_idx
= ipv6_get_saddr_master(net
, dst_dev
,
1672 scores
, hiscore_idx
);
1674 if (scores
[hiscore_idx
].ifa
)
1678 for_each_netdev_rcu(net
, dev
) {
1679 /* only consider addresses on devices in the
1682 if (l3mdev_master_ifindex_rcu(dev
) != master_idx
)
1684 idev
= __in6_dev_get(dev
);
1687 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1694 hiscore
= &scores
[hiscore_idx
];
1696 return -EADDRNOTAVAIL
;
1698 *saddr
= hiscore
->ifa
->addr
;
1699 in6_ifa_put(hiscore
->ifa
);
1702 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1704 int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1707 struct inet6_ifaddr
*ifp
;
1708 int err
= -EADDRNOTAVAIL
;
1710 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1711 if (ifp
->scope
> IFA_LINK
)
1713 if (ifp
->scope
== IFA_LINK
&&
1714 !(ifp
->flags
& banned_flags
)) {
1723 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1726 struct inet6_dev
*idev
;
1727 int err
= -EADDRNOTAVAIL
;
1730 idev
= __in6_dev_get(dev
);
1732 read_lock_bh(&idev
->lock
);
1733 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1734 read_unlock_bh(&idev
->lock
);
1740 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1743 struct inet6_ifaddr
*ifp
;
1745 read_lock_bh(&idev
->lock
);
1746 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
)
1748 read_unlock_bh(&idev
->lock
);
1752 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1753 const struct net_device
*dev
, int strict
)
1755 return ipv6_chk_addr_and_flags(net
, addr
, dev
, strict
, IFA_F_TENTATIVE
);
1757 EXPORT_SYMBOL(ipv6_chk_addr
);
1759 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1760 const struct net_device
*dev
, int strict
,
1763 struct inet6_ifaddr
*ifp
;
1764 unsigned int hash
= inet6_addr_hash(addr
);
1768 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1769 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1771 /* Decouple optimistic from tentative for evaluation here.
1772 * Ban optimistic addresses explicitly, when required.
1774 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1775 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1777 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1778 !(ifp_flags
&banned_flags
) &&
1779 (!dev
|| ifp
->idev
->dev
== dev
||
1780 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1781 rcu_read_unlock_bh();
1786 rcu_read_unlock_bh();
1789 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1791 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1792 struct net_device
*dev
)
1794 unsigned int hash
= inet6_addr_hash(addr
);
1795 struct inet6_ifaddr
*ifp
;
1797 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1798 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1800 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1801 if (!dev
|| ifp
->idev
->dev
== dev
)
1808 /* Compares an address/prefix_len with addresses on device @dev.
1809 * If one is found it returns true.
1811 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1812 const unsigned int prefix_len
, struct net_device
*dev
)
1814 struct inet6_dev
*idev
;
1815 struct inet6_ifaddr
*ifa
;
1819 idev
= __in6_dev_get(dev
);
1821 read_lock_bh(&idev
->lock
);
1822 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1823 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
1827 read_unlock_bh(&idev
->lock
);
1833 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
1835 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1837 struct inet6_dev
*idev
;
1838 struct inet6_ifaddr
*ifa
;
1843 idev
= __in6_dev_get(dev
);
1845 read_lock_bh(&idev
->lock
);
1846 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1847 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1852 read_unlock_bh(&idev
->lock
);
1857 EXPORT_SYMBOL(ipv6_chk_prefix
);
1859 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1860 struct net_device
*dev
, int strict
)
1862 struct inet6_ifaddr
*ifp
, *result
= NULL
;
1863 unsigned int hash
= inet6_addr_hash(addr
);
1866 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1867 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1869 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1870 if (!dev
|| ifp
->idev
->dev
== dev
||
1871 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1878 rcu_read_unlock_bh();
1883 /* Gets referenced address, destroys ifaddr */
1885 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
1888 ifp
->flags
|= IFA_F_DADFAILED
;
1890 if (ifp
->flags
&IFA_F_PERMANENT
) {
1891 spin_lock_bh(&ifp
->lock
);
1892 addrconf_del_dad_work(ifp
);
1893 ifp
->flags
|= IFA_F_TENTATIVE
;
1894 spin_unlock_bh(&ifp
->lock
);
1896 ipv6_ifa_notify(0, ifp
);
1898 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1899 struct inet6_ifaddr
*ifpub
;
1900 spin_lock_bh(&ifp
->lock
);
1903 in6_ifa_hold(ifpub
);
1904 spin_unlock_bh(&ifp
->lock
);
1905 ipv6_create_tempaddr(ifpub
, ifp
);
1908 spin_unlock_bh(&ifp
->lock
);
1916 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
1920 spin_lock_bh(&ifp
->lock
);
1921 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
1922 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
1925 spin_unlock_bh(&ifp
->lock
);
1930 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1932 struct inet6_dev
*idev
= ifp
->idev
;
1933 struct net
*net
= dev_net(ifp
->idev
->dev
);
1935 if (addrconf_dad_end(ifp
)) {
1940 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1941 ifp
->idev
->dev
->name
, &ifp
->addr
);
1943 spin_lock_bh(&ifp
->lock
);
1945 if (ifp
->flags
& IFA_F_STABLE_PRIVACY
) {
1946 int scope
= ifp
->scope
;
1947 u32 flags
= ifp
->flags
;
1948 struct in6_addr new_addr
;
1949 struct inet6_ifaddr
*ifp2
;
1950 u32 valid_lft
, preferred_lft
;
1951 int pfxlen
= ifp
->prefix_len
;
1952 int retries
= ifp
->stable_privacy_retry
+ 1;
1954 if (retries
> net
->ipv6
.sysctl
.idgen_retries
) {
1955 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1956 ifp
->idev
->dev
->name
);
1960 new_addr
= ifp
->addr
;
1961 if (ipv6_generate_stable_address(&new_addr
, retries
,
1965 valid_lft
= ifp
->valid_lft
;
1966 preferred_lft
= ifp
->prefered_lft
;
1968 spin_unlock_bh(&ifp
->lock
);
1970 if (idev
->cnf
.max_addresses
&&
1971 ipv6_count_addresses(idev
) >=
1972 idev
->cnf
.max_addresses
)
1975 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1976 ifp
->idev
->dev
->name
);
1978 ifp2
= ipv6_add_addr(idev
, &new_addr
, NULL
, pfxlen
,
1979 scope
, flags
, valid_lft
,
1984 spin_lock_bh(&ifp2
->lock
);
1985 ifp2
->stable_privacy_retry
= retries
;
1986 ifp2
->state
= INET6_IFADDR_STATE_PREDAD
;
1987 spin_unlock_bh(&ifp2
->lock
);
1989 addrconf_mod_dad_work(ifp2
, net
->ipv6
.sysctl
.idgen_delay
);
1992 spin_lock_bh(&ifp
->lock
);
1996 /* transition from _POSTDAD to _ERRDAD */
1997 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
1998 spin_unlock_bh(&ifp
->lock
);
2000 addrconf_mod_dad_work(ifp
, 0);
2004 /* Join to solicited addr multicast group.
2005 * caller must hold RTNL */
2006 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
2008 struct in6_addr maddr
;
2010 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2013 addrconf_addr_solict_mult(addr
, &maddr
);
2014 ipv6_dev_mc_inc(dev
, &maddr
);
2017 /* caller must hold RTNL */
2018 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2020 struct in6_addr maddr
;
2022 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2025 addrconf_addr_solict_mult(addr
, &maddr
);
2026 __ipv6_dev_mc_dec(idev
, &maddr
);
2029 /* caller must hold RTNL */
2030 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
2032 struct in6_addr addr
;
2034 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2036 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2037 if (ipv6_addr_any(&addr
))
2039 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
2042 /* caller must hold RTNL */
2043 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
2045 struct in6_addr addr
;
2047 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2049 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2050 if (ipv6_addr_any(&addr
))
2052 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
2055 static int addrconf_ifid_eui64(u8
*eui
, struct net_device
*dev
)
2057 if (dev
->addr_len
!= EUI64_ADDR_LEN
)
2059 memcpy(eui
, dev
->dev_addr
, EUI64_ADDR_LEN
);
2064 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
2066 union fwnet_hwaddr
*ha
;
2068 if (dev
->addr_len
!= FWNET_ALEN
)
2071 ha
= (union fwnet_hwaddr
*)dev
->dev_addr
;
2073 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
2078 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
2080 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2081 if (dev
->addr_len
!= ARCNET_ALEN
)
2084 eui
[7] = *(u8
*)dev
->dev_addr
;
2088 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
2090 if (dev
->addr_len
!= INFINIBAND_ALEN
)
2092 memcpy(eui
, dev
->dev_addr
+ 12, 8);
2097 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
2101 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
2102 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
2103 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
2104 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
2105 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
2106 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
2110 memcpy(eui
+ 4, &addr
, 4);
2114 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
2116 if (dev
->priv_flags
& IFF_ISATAP
)
2117 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2121 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
2123 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2126 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
2128 memcpy(eui
, dev
->perm_addr
, 3);
2129 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
2136 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
2138 switch (dev
->type
) {
2141 return addrconf_ifid_eui48(eui
, dev
);
2143 return addrconf_ifid_arcnet(eui
, dev
);
2144 case ARPHRD_INFINIBAND
:
2145 return addrconf_ifid_infiniband(eui
, dev
);
2147 return addrconf_ifid_sit(eui
, dev
);
2150 return addrconf_ifid_gre(eui
, dev
);
2151 case ARPHRD_6LOWPAN
:
2152 return addrconf_ifid_eui64(eui
, dev
);
2153 case ARPHRD_IEEE1394
:
2154 return addrconf_ifid_ieee1394(eui
, dev
);
2155 case ARPHRD_TUNNEL6
:
2157 return addrconf_ifid_ip6tnl(eui
, dev
);
2162 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
2165 struct inet6_ifaddr
*ifp
;
2167 read_lock_bh(&idev
->lock
);
2168 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
2169 if (ifp
->scope
> IFA_LINK
)
2171 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2172 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
2177 read_unlock_bh(&idev
->lock
);
2181 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2182 static void ipv6_regen_rndid(struct inet6_dev
*idev
)
2185 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
2186 idev
->rndid
[0] &= ~0x02;
2189 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2190 * check if generated address is not inappropriate
2192 * - Reserved subnet anycast (RFC 2526)
2193 * 11111101 11....11 1xxxxxxx
2194 * - ISATAP (RFC4214) 6.1
2195 * 00-00-5E-FE-xx-xx-xx-xx
2197 * - XXX: already assigned to an address on the device
2199 if (idev
->rndid
[0] == 0xfd &&
2200 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
2201 (idev
->rndid
[7]&0x80))
2203 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
2204 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
2206 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
2211 static void ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
2213 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
2214 ipv6_regen_rndid(idev
);
2222 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
2223 unsigned long expires
, u32 flags
)
2225 struct fib6_config cfg
= {
2226 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
,
2227 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2228 .fc_ifindex
= dev
->ifindex
,
2229 .fc_expires
= expires
,
2231 .fc_flags
= RTF_UP
| flags
,
2232 .fc_nlinfo
.nl_net
= dev_net(dev
),
2233 .fc_protocol
= RTPROT_KERNEL
,
2238 /* Prevent useless cloning on PtP SIT.
2239 This thing is done here expecting that the whole
2240 class of non-broadcast devices need not cloning.
2242 #if IS_ENABLED(CONFIG_IPV6_SIT)
2243 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2244 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2247 ip6_route_add(&cfg
);
2251 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2253 const struct net_device
*dev
,
2254 u32 flags
, u32 noflags
)
2256 struct fib6_node
*fn
;
2257 struct rt6_info
*rt
= NULL
;
2258 struct fib6_table
*table
;
2259 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
;
2261 table
= fib6_get_table(dev_net(dev
), tb_id
);
2265 read_lock_bh(&table
->tb6_lock
);
2266 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0);
2270 noflags
|= RTF_CACHE
;
2271 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
2272 if (rt
->dst
.dev
->ifindex
!= dev
->ifindex
)
2274 if ((rt
->rt6i_flags
& flags
) != flags
)
2276 if ((rt
->rt6i_flags
& noflags
) != 0)
2282 read_unlock_bh(&table
->tb6_lock
);
2287 /* Create "default" multicast route to the interface */
2289 static void addrconf_add_mroute(struct net_device
*dev
)
2291 struct fib6_config cfg
= {
2292 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_LOCAL
,
2293 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2294 .fc_ifindex
= dev
->ifindex
,
2297 .fc_nlinfo
.nl_net
= dev_net(dev
),
2300 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2302 ip6_route_add(&cfg
);
2305 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2307 struct inet6_dev
*idev
;
2311 idev
= ipv6_find_idev(dev
);
2313 return ERR_PTR(-ENOBUFS
);
2315 if (idev
->cnf
.disable_ipv6
)
2316 return ERR_PTR(-EACCES
);
2318 /* Add default multicast route */
2319 if (!(dev
->flags
& IFF_LOOPBACK
) && !netif_is_l3_master(dev
))
2320 addrconf_add_mroute(dev
);
2325 static void manage_tempaddrs(struct inet6_dev
*idev
,
2326 struct inet6_ifaddr
*ifp
,
2327 __u32 valid_lft
, __u32 prefered_lft
,
2328 bool create
, unsigned long now
)
2331 struct inet6_ifaddr
*ift
;
2333 read_lock_bh(&idev
->lock
);
2334 /* update all temporary addresses in the list */
2335 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2336 int age
, max_valid
, max_prefered
;
2338 if (ifp
!= ift
->ifpub
)
2341 /* RFC 4941 section 3.3:
2342 * If a received option will extend the lifetime of a public
2343 * address, the lifetimes of temporary addresses should
2344 * be extended, subject to the overall constraint that no
2345 * temporary addresses should ever remain "valid" or "preferred"
2346 * for a time longer than (TEMP_VALID_LIFETIME) or
2347 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2349 age
= (now
- ift
->cstamp
) / HZ
;
2350 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2354 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2355 idev
->desync_factor
- age
;
2356 if (max_prefered
< 0)
2359 if (valid_lft
> max_valid
)
2360 valid_lft
= max_valid
;
2362 if (prefered_lft
> max_prefered
)
2363 prefered_lft
= max_prefered
;
2365 spin_lock(&ift
->lock
);
2367 ift
->valid_lft
= valid_lft
;
2368 ift
->prefered_lft
= prefered_lft
;
2370 if (prefered_lft
> 0)
2371 ift
->flags
&= ~IFA_F_DEPRECATED
;
2373 spin_unlock(&ift
->lock
);
2374 if (!(flags
&IFA_F_TENTATIVE
))
2375 ipv6_ifa_notify(0, ift
);
2378 if ((create
|| list_empty(&idev
->tempaddr_list
)) &&
2379 idev
->cnf
.use_tempaddr
> 0) {
2380 /* When a new public address is created as described
2381 * in [ADDRCONF], also create a new temporary address.
2382 * Also create a temporary address if it's enabled but
2383 * no temporary address currently exists.
2385 read_unlock_bh(&idev
->lock
);
2386 ipv6_create_tempaddr(ifp
, NULL
);
2388 read_unlock_bh(&idev
->lock
);
2392 static bool is_addr_mode_generate_stable(struct inet6_dev
*idev
)
2394 return idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
||
2395 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_RANDOM
;
2398 int addrconf_prefix_rcv_add_addr(struct net
*net
, struct net_device
*dev
,
2399 const struct prefix_info
*pinfo
,
2400 struct inet6_dev
*in6_dev
,
2401 const struct in6_addr
*addr
, int addr_type
,
2402 u32 addr_flags
, bool sllao
, bool tokenized
,
2403 __u32 valid_lft
, u32 prefered_lft
)
2405 struct inet6_ifaddr
*ifp
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
2406 int create
= 0, update_lft
= 0;
2408 if (!ifp
&& valid_lft
) {
2409 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2411 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2412 if (in6_dev
->cnf
.optimistic_dad
&&
2413 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2414 addr_flags
|= IFA_F_OPTIMISTIC
;
2417 /* Do not allow to create too much of autoconfigured
2418 * addresses; this would be too easy way to crash kernel.
2420 if (!max_addresses
||
2421 ipv6_count_addresses(in6_dev
) < max_addresses
)
2422 ifp
= ipv6_add_addr(in6_dev
, addr
, NULL
,
2424 addr_type
&IPV6_ADDR_SCOPE_MASK
,
2425 addr_flags
, valid_lft
,
2428 if (IS_ERR_OR_NULL(ifp
))
2433 spin_lock_bh(&ifp
->lock
);
2434 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2435 ifp
->cstamp
= jiffies
;
2436 ifp
->tokenized
= tokenized
;
2437 spin_unlock_bh(&ifp
->lock
);
2438 addrconf_dad_start(ifp
);
2446 /* update lifetime (RFC2462 5.5.3 e) */
2447 spin_lock_bh(&ifp
->lock
);
2449 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2450 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2453 if (!update_lft
&& !create
&& stored_lft
) {
2454 const u32 minimum_lft
= min_t(u32
,
2455 stored_lft
, MIN_VALID_LIFETIME
);
2456 valid_lft
= max(valid_lft
, minimum_lft
);
2458 /* RFC4862 Section 5.5.3e:
2459 * "Note that the preferred lifetime of the
2460 * corresponding address is always reset to
2461 * the Preferred Lifetime in the received
2462 * Prefix Information option, regardless of
2463 * whether the valid lifetime is also reset or
2466 * So we should always update prefered_lft here.
2472 ifp
->valid_lft
= valid_lft
;
2473 ifp
->prefered_lft
= prefered_lft
;
2476 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2477 spin_unlock_bh(&ifp
->lock
);
2479 if (!(flags
&IFA_F_TENTATIVE
))
2480 ipv6_ifa_notify(0, ifp
);
2482 spin_unlock_bh(&ifp
->lock
);
2484 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2493 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr
);
2495 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2497 struct prefix_info
*pinfo
;
2502 struct inet6_dev
*in6_dev
;
2503 struct net
*net
= dev_net(dev
);
2505 pinfo
= (struct prefix_info
*) opt
;
2507 if (len
< sizeof(struct prefix_info
)) {
2508 ADBG("addrconf: prefix option too short\n");
2513 * Validation checks ([ADDRCONF], page 19)
2516 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2518 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2521 valid_lft
= ntohl(pinfo
->valid
);
2522 prefered_lft
= ntohl(pinfo
->prefered
);
2524 if (prefered_lft
> valid_lft
) {
2525 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2529 in6_dev
= in6_dev_get(dev
);
2532 net_dbg_ratelimited("addrconf: device %s not configured\n",
2538 * Two things going on here:
2539 * 1) Add routes for on-link prefixes
2540 * 2) Configure prefixes with the auto flag set
2543 if (pinfo
->onlink
) {
2544 struct rt6_info
*rt
;
2545 unsigned long rt_expires
;
2547 /* Avoid arithmetic overflow. Really, we could
2548 * save rt_expires in seconds, likely valid_lft,
2549 * but it would require division in fib gc, that it
2553 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2555 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2557 if (addrconf_finite_timeout(rt_expires
))
2560 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2563 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2564 RTF_GATEWAY
| RTF_DEFAULT
);
2567 /* Autoconf prefix route */
2568 if (valid_lft
== 0) {
2571 } else if (addrconf_finite_timeout(rt_expires
)) {
2573 rt6_set_expires(rt
, jiffies
+ rt_expires
);
2575 rt6_clean_expires(rt
);
2577 } else if (valid_lft
) {
2578 clock_t expires
= 0;
2579 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2580 if (addrconf_finite_timeout(rt_expires
)) {
2582 flags
|= RTF_EXPIRES
;
2583 expires
= jiffies_to_clock_t(rt_expires
);
2585 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2586 dev
, expires
, flags
);
2591 /* Try to figure out our local address for this prefix */
2593 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2594 struct in6_addr addr
;
2595 bool tokenized
= false, dev_addr_generated
= false;
2597 if (pinfo
->prefix_len
== 64) {
2598 memcpy(&addr
, &pinfo
->prefix
, 8);
2600 if (!ipv6_addr_any(&in6_dev
->token
)) {
2601 read_lock_bh(&in6_dev
->lock
);
2602 memcpy(addr
.s6_addr
+ 8,
2603 in6_dev
->token
.s6_addr
+ 8, 8);
2604 read_unlock_bh(&in6_dev
->lock
);
2606 } else if (is_addr_mode_generate_stable(in6_dev
) &&
2607 !ipv6_generate_stable_address(&addr
, 0,
2609 addr_flags
|= IFA_F_STABLE_PRIVACY
;
2611 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2612 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2615 dev_addr_generated
= true;
2619 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2624 err
= addrconf_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
2627 tokenized
, valid_lft
,
2632 /* Ignore error case here because previous prefix add addr was
2633 * successful which will be notified.
2635 ndisc_ops_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
, &addr
,
2636 addr_type
, addr_flags
, sllao
,
2637 tokenized
, valid_lft
,
2639 dev_addr_generated
);
2641 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2643 in6_dev_put(in6_dev
);
2647 * Set destination address.
2648 * Special case for SIT interfaces where we create a new "virtual"
2651 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2653 struct in6_ifreq ireq
;
2654 struct net_device
*dev
;
2660 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2663 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2669 #if IS_ENABLED(CONFIG_IPV6_SIT)
2670 if (dev
->type
== ARPHRD_SIT
) {
2671 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2673 struct ip_tunnel_parm p
;
2675 err
= -EADDRNOTAVAIL
;
2676 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2679 memset(&p
, 0, sizeof(p
));
2680 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2684 p
.iph
.protocol
= IPPROTO_IPV6
;
2686 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2688 if (ops
->ndo_do_ioctl
) {
2689 mm_segment_t oldfs
= get_fs();
2692 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2699 dev
= __dev_get_by_name(net
, p
.name
);
2702 err
= dev_open(dev
);
2712 static int ipv6_mc_config(struct sock
*sk
, bool join
,
2713 const struct in6_addr
*addr
, int ifindex
)
2721 ret
= ipv6_sock_mc_join(sk
, ifindex
, addr
);
2723 ret
= ipv6_sock_mc_drop(sk
, ifindex
, addr
);
2730 * Manual configuration of address on an interface
2732 static int inet6_addr_add(struct net
*net
, int ifindex
,
2733 const struct in6_addr
*pfx
,
2734 const struct in6_addr
*peer_pfx
,
2735 unsigned int plen
, __u32 ifa_flags
,
2736 __u32 prefered_lft
, __u32 valid_lft
)
2738 struct inet6_ifaddr
*ifp
;
2739 struct inet6_dev
*idev
;
2740 struct net_device
*dev
;
2741 unsigned long timeout
;
2751 /* check the lifetime */
2752 if (!valid_lft
|| prefered_lft
> valid_lft
)
2755 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&& plen
!= 64)
2758 dev
= __dev_get_by_index(net
, ifindex
);
2762 idev
= addrconf_add_dev(dev
);
2764 return PTR_ERR(idev
);
2766 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2767 int ret
= ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2768 true, pfx
, ifindex
);
2774 scope
= ipv6_addr_scope(pfx
);
2776 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
2777 if (addrconf_finite_timeout(timeout
)) {
2778 expires
= jiffies_to_clock_t(timeout
* HZ
);
2779 valid_lft
= timeout
;
2780 flags
= RTF_EXPIRES
;
2784 ifa_flags
|= IFA_F_PERMANENT
;
2787 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
2788 if (addrconf_finite_timeout(timeout
)) {
2790 ifa_flags
|= IFA_F_DEPRECATED
;
2791 prefered_lft
= timeout
;
2794 ifp
= ipv6_add_addr(idev
, pfx
, peer_pfx
, plen
, scope
, ifa_flags
,
2795 valid_lft
, prefered_lft
);
2798 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
2799 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
2804 * Note that section 3.1 of RFC 4429 indicates
2805 * that the Optimistic flag should not be set for
2806 * manually configured addresses
2808 addrconf_dad_start(ifp
);
2809 if (ifa_flags
& IFA_F_MANAGETEMPADDR
)
2810 manage_tempaddrs(idev
, ifp
, valid_lft
, prefered_lft
,
2813 addrconf_verify_rtnl();
2815 } else if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
2816 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2817 false, pfx
, ifindex
);
2820 return PTR_ERR(ifp
);
2823 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
2824 const struct in6_addr
*pfx
, unsigned int plen
)
2826 struct inet6_ifaddr
*ifp
;
2827 struct inet6_dev
*idev
;
2828 struct net_device
*dev
;
2833 dev
= __dev_get_by_index(net
, ifindex
);
2837 idev
= __in6_dev_get(dev
);
2841 read_lock_bh(&idev
->lock
);
2842 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
2843 if (ifp
->prefix_len
== plen
&&
2844 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
2846 read_unlock_bh(&idev
->lock
);
2848 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
2849 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
2850 manage_tempaddrs(idev
, ifp
, 0, 0, false,
2853 addrconf_verify_rtnl();
2854 if (ipv6_addr_is_multicast(pfx
)) {
2855 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2856 false, pfx
, dev
->ifindex
);
2861 read_unlock_bh(&idev
->lock
);
2862 return -EADDRNOTAVAIL
;
2866 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
2868 struct in6_ifreq ireq
;
2871 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2874 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2878 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, NULL
,
2879 ireq
.ifr6_prefixlen
, IFA_F_PERMANENT
,
2880 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2885 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
2887 struct in6_ifreq ireq
;
2890 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2893 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2897 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
2898 ireq
.ifr6_prefixlen
);
2903 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
2904 int plen
, int scope
)
2906 struct inet6_ifaddr
*ifp
;
2908 ifp
= ipv6_add_addr(idev
, addr
, NULL
, plen
,
2909 scope
, IFA_F_PERMANENT
,
2910 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2912 spin_lock_bh(&ifp
->lock
);
2913 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2914 spin_unlock_bh(&ifp
->lock
);
2915 rt_genid_bump_ipv6(dev_net(idev
->dev
));
2916 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2921 #if IS_ENABLED(CONFIG_IPV6_SIT)
2922 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2924 struct in6_addr addr
;
2925 struct net_device
*dev
;
2926 struct net
*net
= dev_net(idev
->dev
);
2932 memset(&addr
, 0, sizeof(struct in6_addr
));
2933 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2935 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2936 addr
.s6_addr32
[0] = htonl(0xfe800000);
2940 scope
= IPV6_ADDR_COMPATv4
;
2942 pflags
|= RTF_NONEXTHOP
;
2945 if (addr
.s6_addr32
[3]) {
2946 add_addr(idev
, &addr
, plen
, scope
);
2947 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0, pflags
);
2951 for_each_netdev(net
, dev
) {
2952 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
2953 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2954 struct in_ifaddr
*ifa
;
2958 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2960 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2962 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2964 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2965 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2970 add_addr(idev
, &addr
, plen
, flag
);
2971 addrconf_prefix_route(&addr
, plen
, idev
->dev
, 0,
2979 static void init_loopback(struct net_device
*dev
)
2981 struct inet6_dev
*idev
;
2982 struct net_device
*sp_dev
;
2983 struct inet6_ifaddr
*sp_ifa
;
2984 struct rt6_info
*sp_rt
;
2990 idev
= ipv6_find_idev(dev
);
2992 pr_debug("%s: add_dev failed\n", __func__
);
2996 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
2998 /* Add routes to other interface's IPv6 addresses */
2999 for_each_netdev(dev_net(dev
), sp_dev
) {
3000 if (!strcmp(sp_dev
->name
, dev
->name
))
3003 idev
= __in6_dev_get(sp_dev
);
3007 read_lock_bh(&idev
->lock
);
3008 list_for_each_entry(sp_ifa
, &idev
->addr_list
, if_list
) {
3010 if (sp_ifa
->flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))
3014 /* This dst has been added to garbage list when
3015 * lo device down, release this obsolete dst and
3016 * reallocate a new router for ifa.
3018 if (!atomic_read(&sp_ifa
->rt
->rt6i_ref
)) {
3019 ip6_rt_put(sp_ifa
->rt
);
3026 sp_rt
= addrconf_dst_alloc(idev
, &sp_ifa
->addr
, false);
3028 /* Failure cases are ignored */
3029 if (!IS_ERR(sp_rt
)) {
3034 read_unlock_bh(&idev
->lock
);
3038 void addrconf_add_linklocal(struct inet6_dev
*idev
,
3039 const struct in6_addr
*addr
, u32 flags
)
3041 struct inet6_ifaddr
*ifp
;
3042 u32 addr_flags
= flags
| IFA_F_PERMANENT
;
3044 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3045 if (idev
->cnf
.optimistic_dad
&&
3046 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
3047 addr_flags
|= IFA_F_OPTIMISTIC
;
3050 ifp
= ipv6_add_addr(idev
, addr
, NULL
, 64, IFA_LINK
, addr_flags
,
3051 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
3053 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
3054 addrconf_dad_start(ifp
);
3058 EXPORT_SYMBOL_GPL(addrconf_add_linklocal
);
3060 static bool ipv6_reserved_interfaceid(struct in6_addr address
)
3062 if ((address
.s6_addr32
[2] | address
.s6_addr32
[3]) == 0)
3065 if (address
.s6_addr32
[2] == htonl(0x02005eff) &&
3066 ((address
.s6_addr32
[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3069 if (address
.s6_addr32
[2] == htonl(0xfdffffff) &&
3070 ((address
.s6_addr32
[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3076 static int ipv6_generate_stable_address(struct in6_addr
*address
,
3078 const struct inet6_dev
*idev
)
3080 static DEFINE_SPINLOCK(lock
);
3081 static __u32 digest
[SHA_DIGEST_WORDS
];
3082 static __u32 workspace
[SHA_WORKSPACE_WORDS
];
3085 char __data
[SHA_MESSAGE_BYTES
];
3087 struct in6_addr secret
;
3089 unsigned char hwaddr
[MAX_ADDR_LEN
];
3094 struct in6_addr secret
;
3095 struct in6_addr temp
;
3096 struct net
*net
= dev_net(idev
->dev
);
3098 BUILD_BUG_ON(sizeof(data
.__data
) != sizeof(data
));
3100 if (idev
->cnf
.stable_secret
.initialized
)
3101 secret
= idev
->cnf
.stable_secret
.secret
;
3102 else if (net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
3103 secret
= net
->ipv6
.devconf_dflt
->stable_secret
.secret
;
3108 spin_lock_bh(&lock
);
3111 memset(&data
, 0, sizeof(data
));
3112 memset(workspace
, 0, sizeof(workspace
));
3113 memcpy(data
.hwaddr
, idev
->dev
->perm_addr
, idev
->dev
->addr_len
);
3114 data
.prefix
[0] = address
->s6_addr32
[0];
3115 data
.prefix
[1] = address
->s6_addr32
[1];
3116 data
.secret
= secret
;
3117 data
.dad_count
= dad_count
;
3119 sha_transform(digest
, data
.__data
, workspace
);
3122 temp
.s6_addr32
[2] = (__force __be32
)digest
[0];
3123 temp
.s6_addr32
[3] = (__force __be32
)digest
[1];
3125 spin_unlock_bh(&lock
);
3127 if (ipv6_reserved_interfaceid(temp
)) {
3129 if (dad_count
> dev_net(idev
->dev
)->ipv6
.sysctl
.idgen_retries
)
3138 static void ipv6_gen_mode_random_init(struct inet6_dev
*idev
)
3140 struct ipv6_stable_secret
*s
= &idev
->cnf
.stable_secret
;
3144 s
= &idev
->cnf
.stable_secret
;
3145 get_random_bytes(&s
->secret
, sizeof(s
->secret
));
3146 s
->initialized
= true;
3149 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
3151 struct in6_addr addr
;
3153 /* no link local addresses on L3 master devices */
3154 if (netif_is_l3_master(idev
->dev
))
3157 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
3159 switch (idev
->cnf
.addr_gen_mode
) {
3160 case IN6_ADDR_GEN_MODE_RANDOM
:
3161 ipv6_gen_mode_random_init(idev
);
3163 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
3164 if (!ipv6_generate_stable_address(&addr
, 0, idev
))
3165 addrconf_add_linklocal(idev
, &addr
,
3166 IFA_F_STABLE_PRIVACY
);
3167 else if (prefix_route
)
3168 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
3170 case IN6_ADDR_GEN_MODE_EUI64
:
3171 /* addrconf_add_linklocal also adds a prefix_route and we
3172 * only need to care about prefix routes if ipv6_generate_eui64
3173 * couldn't generate one.
3175 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
3176 addrconf_add_linklocal(idev
, &addr
, 0);
3177 else if (prefix_route
)
3178 addrconf_prefix_route(&addr
, 64, idev
->dev
, 0, 0);
3180 case IN6_ADDR_GEN_MODE_NONE
:
3182 /* will not add any link local address */
3187 static void addrconf_dev_config(struct net_device
*dev
)
3189 struct inet6_dev
*idev
;
3193 if ((dev
->type
!= ARPHRD_ETHER
) &&
3194 (dev
->type
!= ARPHRD_FDDI
) &&
3195 (dev
->type
!= ARPHRD_ARCNET
) &&
3196 (dev
->type
!= ARPHRD_INFINIBAND
) &&
3197 (dev
->type
!= ARPHRD_IEEE1394
) &&
3198 (dev
->type
!= ARPHRD_TUNNEL6
) &&
3199 (dev
->type
!= ARPHRD_6LOWPAN
) &&
3200 (dev
->type
!= ARPHRD_IP6GRE
) &&
3201 (dev
->type
!= ARPHRD_IPGRE
) &&
3202 (dev
->type
!= ARPHRD_TUNNEL
) &&
3203 (dev
->type
!= ARPHRD_NONE
)) {
3204 /* Alas, we support only Ethernet autoconfiguration. */
3208 idev
= addrconf_add_dev(dev
);
3212 /* this device type has no EUI support */
3213 if (dev
->type
== ARPHRD_NONE
&&
3214 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
)
3215 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_RANDOM
;
3217 addrconf_addr_gen(idev
, false);
3220 #if IS_ENABLED(CONFIG_IPV6_SIT)
3221 static void addrconf_sit_config(struct net_device
*dev
)
3223 struct inet6_dev
*idev
;
3228 * Configure the tunnel with one of our IPv4
3229 * addresses... we should configure all of
3230 * our v4 addrs in the tunnel
3233 idev
= ipv6_find_idev(dev
);
3235 pr_debug("%s: add_dev failed\n", __func__
);
3239 if (dev
->priv_flags
& IFF_ISATAP
) {
3240 addrconf_addr_gen(idev
, false);
3244 sit_add_v4_addrs(idev
);
3246 if (dev
->flags
&IFF_POINTOPOINT
)
3247 addrconf_add_mroute(dev
);
3251 #if IS_ENABLED(CONFIG_NET_IPGRE)
3252 static void addrconf_gre_config(struct net_device
*dev
)
3254 struct inet6_dev
*idev
;
3258 idev
= ipv6_find_idev(dev
);
3260 pr_debug("%s: add_dev failed\n", __func__
);
3264 addrconf_addr_gen(idev
, true);
3265 if (dev
->flags
& IFF_POINTOPOINT
)
3266 addrconf_add_mroute(dev
);
3270 static int fixup_permanent_addr(struct inet6_dev
*idev
,
3271 struct inet6_ifaddr
*ifp
)
3274 struct rt6_info
*rt
;
3276 rt
= addrconf_dst_alloc(idev
, &ifp
->addr
, false);
3277 if (unlikely(IS_ERR(rt
)))
3283 if (!(ifp
->flags
& IFA_F_NOPREFIXROUTE
)) {
3284 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
3288 addrconf_dad_start(ifp
);
3293 static void addrconf_permanent_addr(struct net_device
*dev
)
3295 struct inet6_ifaddr
*ifp
, *tmp
;
3296 struct inet6_dev
*idev
;
3298 idev
= __in6_dev_get(dev
);
3302 write_lock_bh(&idev
->lock
);
3304 list_for_each_entry_safe(ifp
, tmp
, &idev
->addr_list
, if_list
) {
3305 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3306 fixup_permanent_addr(idev
, ifp
) < 0) {
3307 write_unlock_bh(&idev
->lock
);
3309 write_lock_bh(&idev
->lock
);
3311 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3312 idev
->dev
->name
, &ifp
->addr
);
3316 write_unlock_bh(&idev
->lock
);
3319 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
3322 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3323 struct netdev_notifier_changeupper_info
*info
;
3324 struct inet6_dev
*idev
= __in6_dev_get(dev
);
3325 int run_pending
= 0;
3329 case NETDEV_REGISTER
:
3330 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3331 idev
= ipv6_add_dev(dev
);
3333 return notifier_from_errno(PTR_ERR(idev
));
3337 case NETDEV_CHANGEMTU
:
3338 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3339 if (dev
->mtu
< IPV6_MIN_MTU
) {
3340 addrconf_ifdown(dev
, 1);
3345 rt6_mtu_change(dev
, dev
->mtu
);
3346 idev
->cnf
.mtu6
= dev
->mtu
;
3350 /* allocate new idev */
3351 idev
= ipv6_add_dev(dev
);
3355 /* device is still not ready */
3356 if (!(idev
->if_flags
& IF_READY
))
3365 if (dev
->flags
& IFF_SLAVE
)
3368 if (idev
&& idev
->cnf
.disable_ipv6
)
3371 if (event
== NETDEV_UP
) {
3372 /* restore routes for permanent addresses */
3373 addrconf_permanent_addr(dev
);
3375 if (!addrconf_qdisc_ok(dev
)) {
3376 /* device is not ready yet. */
3377 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3382 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
3383 idev
= ipv6_add_dev(dev
);
3385 if (!IS_ERR_OR_NULL(idev
)) {
3386 idev
->if_flags
|= IF_READY
;
3389 } else if (event
== NETDEV_CHANGE
) {
3390 if (!addrconf_qdisc_ok(dev
)) {
3391 /* device is still not ready. */
3396 if (idev
->if_flags
& IF_READY
)
3397 /* device is already configured. */
3399 idev
->if_flags
|= IF_READY
;
3402 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3408 switch (dev
->type
) {
3409 #if IS_ENABLED(CONFIG_IPV6_SIT)
3411 addrconf_sit_config(dev
);
3414 #if IS_ENABLED(CONFIG_NET_IPGRE)
3416 addrconf_gre_config(dev
);
3419 case ARPHRD_LOOPBACK
:
3424 addrconf_dev_config(dev
);
3428 if (!IS_ERR_OR_NULL(idev
)) {
3430 addrconf_dad_run(idev
);
3433 * If the MTU changed during the interface down,
3434 * when the interface up, the changed MTU must be
3435 * reflected in the idev as well as routers.
3437 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
3438 dev
->mtu
>= IPV6_MIN_MTU
) {
3439 rt6_mtu_change(dev
, dev
->mtu
);
3440 idev
->cnf
.mtu6
= dev
->mtu
;
3442 idev
->tstamp
= jiffies
;
3443 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
3446 * If the changed mtu during down is lower than
3447 * IPV6_MIN_MTU stop IPv6 on this interface.
3449 if (dev
->mtu
< IPV6_MIN_MTU
)
3450 addrconf_ifdown(dev
, 1);
3455 case NETDEV_UNREGISTER
:
3457 * Remove all addresses from this interface.
3459 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3462 case NETDEV_CHANGENAME
:
3464 snmp6_unregister_dev(idev
);
3465 addrconf_sysctl_unregister(idev
);
3466 err
= addrconf_sysctl_register(idev
);
3468 return notifier_from_errno(err
);
3469 err
= snmp6_register_dev(idev
);
3471 addrconf_sysctl_unregister(idev
);
3472 return notifier_from_errno(err
);
3477 case NETDEV_PRE_TYPE_CHANGE
:
3478 case NETDEV_POST_TYPE_CHANGE
:
3480 addrconf_type_change(dev
, event
);
3483 case NETDEV_CHANGEUPPER
:
3486 /* flush all routes if dev is linked to or unlinked from
3487 * an L3 master device (e.g., VRF)
3489 if (info
->upper_dev
&& netif_is_l3_master(info
->upper_dev
))
3490 addrconf_ifdown(dev
, 0);
3497 * addrconf module should be notified of a device going up
3499 static struct notifier_block ipv6_dev_notf
= {
3500 .notifier_call
= addrconf_notify
,
3503 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3505 struct inet6_dev
*idev
;
3508 idev
= __in6_dev_get(dev
);
3510 if (event
== NETDEV_POST_TYPE_CHANGE
)
3511 ipv6_mc_remap(idev
);
3512 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3513 ipv6_mc_unmap(idev
);
3516 static bool addr_is_local(const struct in6_addr
*addr
)
3518 return ipv6_addr_type(addr
) &
3519 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
);
3522 static int addrconf_ifdown(struct net_device
*dev
, int how
)
3524 struct net
*net
= dev_net(dev
);
3525 struct inet6_dev
*idev
;
3526 struct inet6_ifaddr
*ifa
, *tmp
;
3527 struct list_head del_list
;
3534 rt6_ifdown(net
, dev
);
3535 neigh_ifdown(&nd_tbl
, dev
);
3537 idev
= __in6_dev_get(dev
);
3542 * Step 1: remove reference to ipv6 device from parent device.
3548 /* protected by rtnl_lock */
3549 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3551 /* Step 1.5: remove snmp6 entry */
3552 snmp6_unregister_dev(idev
);
3556 /* aggregate the system setting and interface setting */
3557 _keep_addr
= net
->ipv6
.devconf_all
->keep_addr_on_down
;
3559 _keep_addr
= idev
->cnf
.keep_addr_on_down
;
3561 /* combine the user config with event to determine if permanent
3562 * addresses are to be removed from address hash table
3564 keep_addr
= !(how
|| _keep_addr
<= 0 || idev
->cnf
.disable_ipv6
);
3566 /* Step 2: clear hash table */
3567 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3568 struct hlist_head
*h
= &inet6_addr_lst
[i
];
3570 spin_lock_bh(&addrconf_hash_lock
);
3572 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3573 if (ifa
->idev
== idev
) {
3574 addrconf_del_dad_work(ifa
);
3575 /* combined flag + permanent flag decide if
3576 * address is retained on a down event
3579 !(ifa
->flags
& IFA_F_PERMANENT
) ||
3580 addr_is_local(&ifa
->addr
)) {
3581 hlist_del_init_rcu(&ifa
->addr_lst
);
3586 spin_unlock_bh(&addrconf_hash_lock
);
3589 write_lock_bh(&idev
->lock
);
3591 addrconf_del_rs_timer(idev
);
3593 /* Step 2: clear flags for stateless addrconf */
3595 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3597 /* Step 3: clear tempaddr list */
3598 while (!list_empty(&idev
->tempaddr_list
)) {
3599 ifa
= list_first_entry(&idev
->tempaddr_list
,
3600 struct inet6_ifaddr
, tmp_list
);
3601 list_del(&ifa
->tmp_list
);
3602 write_unlock_bh(&idev
->lock
);
3603 spin_lock_bh(&ifa
->lock
);
3606 in6_ifa_put(ifa
->ifpub
);
3609 spin_unlock_bh(&ifa
->lock
);
3611 write_lock_bh(&idev
->lock
);
3614 /* re-combine the user config with event to determine if permanent
3615 * addresses are to be removed from the interface list
3617 keep_addr
= (!how
&& _keep_addr
> 0 && !idev
->cnf
.disable_ipv6
);
3619 INIT_LIST_HEAD(&del_list
);
3620 list_for_each_entry_safe(ifa
, tmp
, &idev
->addr_list
, if_list
) {
3621 struct rt6_info
*rt
= NULL
;
3623 addrconf_del_dad_work(ifa
);
3625 write_unlock_bh(&idev
->lock
);
3626 spin_lock_bh(&ifa
->lock
);
3628 if (keep_addr
&& (ifa
->flags
& IFA_F_PERMANENT
) &&
3629 !addr_is_local(&ifa
->addr
)) {
3630 /* set state to skip the notifier below */
3631 state
= INET6_IFADDR_STATE_DEAD
;
3633 if (!(ifa
->flags
& IFA_F_NODAD
))
3634 ifa
->flags
|= IFA_F_TENTATIVE
;
3640 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3642 list_move(&ifa
->if_list
, &del_list
);
3645 spin_unlock_bh(&ifa
->lock
);
3650 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3651 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3652 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3654 if (idev
->cnf
.forwarding
)
3655 addrconf_leave_anycast(ifa
);
3656 addrconf_leave_solict(ifa
->idev
, &ifa
->addr
);
3659 write_lock_bh(&idev
->lock
);
3662 write_unlock_bh(&idev
->lock
);
3664 /* now clean up addresses to be removed */
3665 while (!list_empty(&del_list
)) {
3666 ifa
= list_first_entry(&del_list
,
3667 struct inet6_ifaddr
, if_list
);
3668 list_del(&ifa
->if_list
);
3673 /* Step 5: Discard anycast and multicast list */
3675 ipv6_ac_destroy_dev(idev
);
3676 ipv6_mc_destroy_dev(idev
);
3681 idev
->tstamp
= jiffies
;
3683 /* Last: Shot the device (if unregistered) */
3685 addrconf_sysctl_unregister(idev
);
3686 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3687 neigh_ifdown(&nd_tbl
, dev
);
3693 static void addrconf_rs_timer(unsigned long data
)
3695 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
3696 struct net_device
*dev
= idev
->dev
;
3697 struct in6_addr lladdr
;
3699 write_lock(&idev
->lock
);
3700 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3703 if (!ipv6_accept_ra(idev
))
3706 /* Announcement received after solicitation was sent */
3707 if (idev
->if_flags
& IF_RA_RCVD
)
3710 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
|| idev
->cnf
.rtr_solicits
< 0) {
3711 write_unlock(&idev
->lock
);
3712 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3713 ndisc_send_rs(dev
, &lladdr
,
3714 &in6addr_linklocal_allrouters
);
3718 write_lock(&idev
->lock
);
3719 idev
->rs_interval
= rfc3315_s14_backoff_update(
3720 idev
->rs_interval
, idev
->cnf
.rtr_solicit_max_interval
);
3721 /* The wait after the last probe can be shorter */
3722 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3723 idev
->cnf
.rtr_solicits
) ?
3724 idev
->cnf
.rtr_solicit_delay
:
3728 * Note: we do not support deprecated "all on-link"
3729 * assumption any longer.
3731 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3735 write_unlock(&idev
->lock
);
3741 * Duplicate Address Detection
3743 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3745 unsigned long rand_num
;
3746 struct inet6_dev
*idev
= ifp
->idev
;
3749 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3752 rand_num
= prandom_u32() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3755 if (idev
->cnf
.enhanced_dad
||
3756 dev_net(idev
->dev
)->ipv6
.devconf_all
->enhanced_dad
) {
3758 get_random_bytes(&nonce
, 6);
3761 ifp
->dad_nonce
= nonce
;
3762 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
3763 addrconf_mod_dad_work(ifp
, rand_num
);
3766 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
3768 struct inet6_dev
*idev
= ifp
->idev
;
3769 struct net_device
*dev
= idev
->dev
;
3770 bool bump_id
, notify
= false;
3772 addrconf_join_solict(dev
, &ifp
->addr
);
3774 prandom_seed((__force u32
) ifp
->addr
.s6_addr32
[3]);
3776 read_lock_bh(&idev
->lock
);
3777 spin_lock(&ifp
->lock
);
3778 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3781 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3782 idev
->cnf
.accept_dad
< 1 ||
3783 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3784 ifp
->flags
& IFA_F_NODAD
) {
3785 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
3786 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3787 spin_unlock(&ifp
->lock
);
3788 read_unlock_bh(&idev
->lock
);
3790 addrconf_dad_completed(ifp
, bump_id
);
3794 if (!(idev
->if_flags
& IF_READY
)) {
3795 spin_unlock(&ifp
->lock
);
3796 read_unlock_bh(&idev
->lock
);
3798 * If the device is not ready:
3799 * - keep it tentative if it is a permanent address.
3800 * - otherwise, kill it.
3803 addrconf_dad_stop(ifp
, 0);
3808 * Optimistic nodes can start receiving
3811 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
3812 ip6_ins_rt(ifp
->rt
);
3813 if (ipv6_use_optimistic_addr(idev
)) {
3814 /* Because optimistic nodes can use this address,
3815 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3821 addrconf_dad_kick(ifp
);
3823 spin_unlock(&ifp
->lock
);
3824 read_unlock_bh(&idev
->lock
);
3826 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3829 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
3831 bool begin_dad
= false;
3833 spin_lock_bh(&ifp
->lock
);
3834 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
3835 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
3838 spin_unlock_bh(&ifp
->lock
);
3841 addrconf_mod_dad_work(ifp
, 0);
3844 static void addrconf_dad_work(struct work_struct
*w
)
3846 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
3847 struct inet6_ifaddr
,
3849 struct inet6_dev
*idev
= ifp
->idev
;
3850 bool bump_id
, disable_ipv6
= false;
3851 struct in6_addr mcaddr
;
3857 } action
= DAD_PROCESS
;
3861 spin_lock_bh(&ifp
->lock
);
3862 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
3864 ifp
->state
= INET6_IFADDR_STATE_DAD
;
3865 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
3867 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
3869 if (idev
->cnf
.accept_dad
> 1 && !idev
->cnf
.disable_ipv6
&&
3870 !(ifp
->flags
& IFA_F_STABLE_PRIVACY
)) {
3871 struct in6_addr addr
;
3873 addr
.s6_addr32
[0] = htonl(0xfe800000);
3874 addr
.s6_addr32
[1] = 0;
3876 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
3877 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
3878 /* DAD failed for link-local based on MAC */
3879 idev
->cnf
.disable_ipv6
= 1;
3881 pr_info("%s: IPv6 being disabled!\n",
3882 ifp
->idev
->dev
->name
);
3883 disable_ipv6
= true;
3887 spin_unlock_bh(&ifp
->lock
);
3889 if (action
== DAD_BEGIN
) {
3890 addrconf_dad_begin(ifp
);
3892 } else if (action
== DAD_ABORT
) {
3894 addrconf_dad_stop(ifp
, 1);
3896 addrconf_ifdown(idev
->dev
, 0);
3900 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
3903 write_lock_bh(&idev
->lock
);
3904 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
3905 write_unlock_bh(&idev
->lock
);
3909 spin_lock(&ifp
->lock
);
3910 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
3911 spin_unlock(&ifp
->lock
);
3912 write_unlock_bh(&idev
->lock
);
3916 if (ifp
->dad_probes
== 0) {
3918 * DAD was successful
3921 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
3922 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3923 spin_unlock(&ifp
->lock
);
3924 write_unlock_bh(&idev
->lock
);
3926 addrconf_dad_completed(ifp
, bump_id
);
3932 addrconf_mod_dad_work(ifp
,
3933 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
));
3934 spin_unlock(&ifp
->lock
);
3935 write_unlock_bh(&idev
->lock
);
3937 /* send a neighbour solicitation for our addr */
3938 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
3939 ndisc_send_ns(ifp
->idev
->dev
, &ifp
->addr
, &mcaddr
, &in6addr_any
,
3946 /* ifp->idev must be at least read locked */
3947 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
3949 struct inet6_ifaddr
*ifpiter
;
3950 struct inet6_dev
*idev
= ifp
->idev
;
3952 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
3953 if (ifpiter
->scope
> IFA_LINK
)
3955 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
3956 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
3957 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
3964 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
)
3966 struct net_device
*dev
= ifp
->idev
->dev
;
3967 struct in6_addr lladdr
;
3968 bool send_rs
, send_mld
;
3970 addrconf_del_dad_work(ifp
);
3973 * Configure the address for reception. Now it is valid.
3976 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3978 /* If added prefix is link local and we are prepared to process
3979 router advertisements, start sending router solicitations.
3982 read_lock_bh(&ifp
->idev
->lock
);
3983 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
3984 send_rs
= send_mld
&&
3985 ipv6_accept_ra(ifp
->idev
) &&
3986 ifp
->idev
->cnf
.rtr_solicits
!= 0 &&
3987 (dev
->flags
&IFF_LOOPBACK
) == 0;
3988 read_unlock_bh(&ifp
->idev
->lock
);
3990 /* While dad is in progress mld report's source address is in6_addrany.
3991 * Resend with proper ll now.
3994 ipv6_mc_dad_complete(ifp
->idev
);
3998 * If a host as already performed a random delay
3999 * [...] as part of DAD [...] there is no need
4000 * to delay again before sending the first RS
4002 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
4004 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
4006 write_lock_bh(&ifp
->idev
->lock
);
4007 spin_lock(&ifp
->lock
);
4008 ifp
->idev
->rs_interval
= rfc3315_s14_backoff_init(
4009 ifp
->idev
->cnf
.rtr_solicit_interval
);
4010 ifp
->idev
->rs_probes
= 1;
4011 ifp
->idev
->if_flags
|= IF_RS_SENT
;
4012 addrconf_mod_rs_timer(ifp
->idev
, ifp
->idev
->rs_interval
);
4013 spin_unlock(&ifp
->lock
);
4014 write_unlock_bh(&ifp
->idev
->lock
);
4018 rt_genid_bump_ipv6(dev_net(dev
));
4021 static void addrconf_dad_run(struct inet6_dev
*idev
)
4023 struct inet6_ifaddr
*ifp
;
4025 read_lock_bh(&idev
->lock
);
4026 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4027 spin_lock(&ifp
->lock
);
4028 if (ifp
->flags
& IFA_F_TENTATIVE
&&
4029 ifp
->state
== INET6_IFADDR_STATE_DAD
)
4030 addrconf_dad_kick(ifp
);
4031 spin_unlock(&ifp
->lock
);
4033 read_unlock_bh(&idev
->lock
);
4036 #ifdef CONFIG_PROC_FS
4037 struct if6_iter_state
{
4038 struct seq_net_private p
;
4043 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
4045 struct inet6_ifaddr
*ifa
= NULL
;
4046 struct if6_iter_state
*state
= seq
->private;
4047 struct net
*net
= seq_file_net(seq
);
4050 /* initial bucket if pos is 0 */
4056 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
4057 hlist_for_each_entry_rcu_bh(ifa
, &inet6_addr_lst
[state
->bucket
],
4059 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4061 /* sync with offset */
4062 if (p
< state
->offset
) {
4070 /* prepare for next bucket */
4077 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
4078 struct inet6_ifaddr
*ifa
)
4080 struct if6_iter_state
*state
= seq
->private;
4081 struct net
*net
= seq_file_net(seq
);
4083 hlist_for_each_entry_continue_rcu_bh(ifa
, addr_lst
) {
4084 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4090 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
4092 hlist_for_each_entry_rcu_bh(ifa
,
4093 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
4094 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
4104 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4108 return if6_get_first(seq
, *pos
);
4111 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4113 struct inet6_ifaddr
*ifa
;
4115 ifa
= if6_get_next(seq
, v
);
4120 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
4123 rcu_read_unlock_bh();
4126 static int if6_seq_show(struct seq_file
*seq
, void *v
)
4128 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
4129 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
4131 ifp
->idev
->dev
->ifindex
,
4135 ifp
->idev
->dev
->name
);
4139 static const struct seq_operations if6_seq_ops
= {
4140 .start
= if6_seq_start
,
4141 .next
= if6_seq_next
,
4142 .show
= if6_seq_show
,
4143 .stop
= if6_seq_stop
,
4146 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
4148 return seq_open_net(inode
, file
, &if6_seq_ops
,
4149 sizeof(struct if6_iter_state
));
4152 static const struct file_operations if6_fops
= {
4153 .owner
= THIS_MODULE
,
4154 .open
= if6_seq_open
,
4156 .llseek
= seq_lseek
,
4157 .release
= seq_release_net
,
4160 static int __net_init
if6_proc_net_init(struct net
*net
)
4162 if (!proc_create("if_inet6", S_IRUGO
, net
->proc_net
, &if6_fops
))
4167 static void __net_exit
if6_proc_net_exit(struct net
*net
)
4169 remove_proc_entry("if_inet6", net
->proc_net
);
4172 static struct pernet_operations if6_proc_net_ops
= {
4173 .init
= if6_proc_net_init
,
4174 .exit
= if6_proc_net_exit
,
4177 int __init
if6_proc_init(void)
4179 return register_pernet_subsys(&if6_proc_net_ops
);
4182 void if6_proc_exit(void)
4184 unregister_pernet_subsys(&if6_proc_net_ops
);
4186 #endif /* CONFIG_PROC_FS */
4188 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4189 /* Check if address is a home address configured on any interface. */
4190 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
4193 struct inet6_ifaddr
*ifp
= NULL
;
4194 unsigned int hash
= inet6_addr_hash(addr
);
4197 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
4198 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
4200 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
4201 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
4206 rcu_read_unlock_bh();
4212 * Periodic address status verification
4215 static void addrconf_verify_rtnl(void)
4217 unsigned long now
, next
, next_sec
, next_sched
;
4218 struct inet6_ifaddr
*ifp
;
4225 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
4227 cancel_delayed_work(&addr_chk_work
);
4229 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
4231 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[i
], addr_lst
) {
4234 /* When setting preferred_lft to a value not zero or
4235 * infinity, while valid_lft is infinity
4236 * IFA_F_PERMANENT has a non-infinity life time.
4238 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
4239 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
4242 spin_lock(&ifp
->lock
);
4243 /* We try to batch several events at once. */
4244 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
4246 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
4247 age
>= ifp
->valid_lft
) {
4248 spin_unlock(&ifp
->lock
);
4252 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
4253 spin_unlock(&ifp
->lock
);
4255 } else if (age
>= ifp
->prefered_lft
) {
4256 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4259 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
4261 ifp
->flags
|= IFA_F_DEPRECATED
;
4264 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
4265 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
4266 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
4268 spin_unlock(&ifp
->lock
);
4273 ipv6_ifa_notify(0, ifp
);
4277 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
4278 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
4279 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
4280 ifp
->idev
->cnf
.dad_transmits
*
4281 NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
) / HZ
;
4283 if (age
>= ifp
->prefered_lft
- regen_advance
) {
4284 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
4285 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4286 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4287 if (!ifp
->regen_count
&& ifpub
) {
4290 in6_ifa_hold(ifpub
);
4291 spin_unlock(&ifp
->lock
);
4293 spin_lock(&ifpub
->lock
);
4294 ifpub
->regen_count
= 0;
4295 spin_unlock(&ifpub
->lock
);
4296 ipv6_create_tempaddr(ifpub
, ifp
);
4301 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
4302 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
4303 spin_unlock(&ifp
->lock
);
4305 /* ifp->prefered_lft <= ifp->valid_lft */
4306 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4307 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4308 spin_unlock(&ifp
->lock
);
4313 next_sec
= round_jiffies_up(next
);
4316 /* If rounded timeout is accurate enough, accept it. */
4317 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
4318 next_sched
= next_sec
;
4320 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4321 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
4322 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
4324 ADBG(KERN_DEBUG
"now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4325 now
, next
, next_sec
, next_sched
);
4326 mod_delayed_work(addrconf_wq
, &addr_chk_work
, next_sched
- now
);
4327 rcu_read_unlock_bh();
4330 static void addrconf_verify_work(struct work_struct
*w
)
4333 addrconf_verify_rtnl();
4337 static void addrconf_verify(void)
4339 mod_delayed_work(addrconf_wq
, &addr_chk_work
, 0);
4342 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
4343 struct in6_addr
**peer_pfx
)
4345 struct in6_addr
*pfx
= NULL
;
4350 pfx
= nla_data(addr
);
4353 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
4355 pfx
= nla_data(local
);
4361 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
4362 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
4363 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
4364 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
4365 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
4369 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
4371 struct net
*net
= sock_net(skb
->sk
);
4372 struct ifaddrmsg
*ifm
;
4373 struct nlattr
*tb
[IFA_MAX
+1];
4374 struct in6_addr
*pfx
, *peer_pfx
;
4378 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4382 ifm
= nlmsg_data(nlh
);
4383 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4387 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4389 /* We ignore other flags so far. */
4390 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
4392 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
4393 ifm
->ifa_prefixlen
);
4396 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u32 ifa_flags
,
4397 u32 prefered_lft
, u32 valid_lft
)
4401 unsigned long timeout
;
4402 bool was_managetempaddr
;
4403 bool had_prefixroute
;
4407 if (!valid_lft
|| (prefered_lft
> valid_lft
))
4410 if (ifa_flags
& IFA_F_MANAGETEMPADDR
&&
4411 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
4414 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
4415 if (addrconf_finite_timeout(timeout
)) {
4416 expires
= jiffies_to_clock_t(timeout
* HZ
);
4417 valid_lft
= timeout
;
4418 flags
= RTF_EXPIRES
;
4422 ifa_flags
|= IFA_F_PERMANENT
;
4425 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
4426 if (addrconf_finite_timeout(timeout
)) {
4428 ifa_flags
|= IFA_F_DEPRECATED
;
4429 prefered_lft
= timeout
;
4432 spin_lock_bh(&ifp
->lock
);
4433 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
4434 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
4435 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
4436 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
4437 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4438 IFA_F_NOPREFIXROUTE
);
4439 ifp
->flags
|= ifa_flags
;
4440 ifp
->tstamp
= jiffies
;
4441 ifp
->valid_lft
= valid_lft
;
4442 ifp
->prefered_lft
= prefered_lft
;
4444 spin_unlock_bh(&ifp
->lock
);
4445 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
4446 ipv6_ifa_notify(0, ifp
);
4448 if (!(ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
4449 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
4451 } else if (had_prefixroute
) {
4452 enum cleanup_prefix_rt_t action
;
4453 unsigned long rt_expires
;
4455 write_lock_bh(&ifp
->idev
->lock
);
4456 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
4457 write_unlock_bh(&ifp
->idev
->lock
);
4459 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
4460 cleanup_prefix_route(ifp
, rt_expires
,
4461 action
== CLEANUP_PREFIX_RT_DEL
);
4465 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
4466 if (was_managetempaddr
&& !(ifp
->flags
& IFA_F_MANAGETEMPADDR
))
4467 valid_lft
= prefered_lft
= 0;
4468 manage_tempaddrs(ifp
->idev
, ifp
, valid_lft
, prefered_lft
,
4469 !was_managetempaddr
, jiffies
);
4472 addrconf_verify_rtnl();
4478 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
4480 struct net
*net
= sock_net(skb
->sk
);
4481 struct ifaddrmsg
*ifm
;
4482 struct nlattr
*tb
[IFA_MAX
+1];
4483 struct in6_addr
*pfx
, *peer_pfx
;
4484 struct inet6_ifaddr
*ifa
;
4485 struct net_device
*dev
;
4486 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
4490 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4494 ifm
= nlmsg_data(nlh
);
4495 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4499 if (tb
[IFA_CACHEINFO
]) {
4500 struct ifa_cacheinfo
*ci
;
4502 ci
= nla_data(tb
[IFA_CACHEINFO
]);
4503 valid_lft
= ci
->ifa_valid
;
4504 preferred_lft
= ci
->ifa_prefered
;
4506 preferred_lft
= INFINITY_LIFE_TIME
;
4507 valid_lft
= INFINITY_LIFE_TIME
;
4510 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4514 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4516 /* We ignore other flags so far. */
4517 ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4518 IFA_F_NOPREFIXROUTE
| IFA_F_MCAUTOJOIN
;
4520 ifa
= ipv6_get_ifaddr(net
, pfx
, dev
, 1);
4523 * It would be best to check for !NLM_F_CREATE here but
4524 * userspace already relies on not having to provide this.
4526 return inet6_addr_add(net
, ifm
->ifa_index
, pfx
, peer_pfx
,
4527 ifm
->ifa_prefixlen
, ifa_flags
,
4528 preferred_lft
, valid_lft
);
4531 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
4532 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
4535 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
4542 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
4543 u8 scope
, int ifindex
)
4545 struct ifaddrmsg
*ifm
;
4547 ifm
= nlmsg_data(nlh
);
4548 ifm
->ifa_family
= AF_INET6
;
4549 ifm
->ifa_prefixlen
= prefixlen
;
4550 ifm
->ifa_flags
= flags
;
4551 ifm
->ifa_scope
= scope
;
4552 ifm
->ifa_index
= ifindex
;
4555 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
4556 unsigned long tstamp
, u32 preferred
, u32 valid
)
4558 struct ifa_cacheinfo ci
;
4560 ci
.cstamp
= cstamp_delta(cstamp
);
4561 ci
.tstamp
= cstamp_delta(tstamp
);
4562 ci
.ifa_prefered
= preferred
;
4563 ci
.ifa_valid
= valid
;
4565 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
4568 static inline int rt_scope(int ifa_scope
)
4570 if (ifa_scope
& IFA_HOST
)
4571 return RT_SCOPE_HOST
;
4572 else if (ifa_scope
& IFA_LINK
)
4573 return RT_SCOPE_LINK
;
4574 else if (ifa_scope
& IFA_SITE
)
4575 return RT_SCOPE_SITE
;
4577 return RT_SCOPE_UNIVERSE
;
4580 static inline int inet6_ifaddr_msgsize(void)
4582 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
4583 + nla_total_size(16) /* IFA_LOCAL */
4584 + nla_total_size(16) /* IFA_ADDRESS */
4585 + nla_total_size(sizeof(struct ifa_cacheinfo
))
4586 + nla_total_size(4) /* IFA_FLAGS */;
4589 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
4590 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4592 struct nlmsghdr
*nlh
;
4593 u32 preferred
, valid
;
4595 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4599 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
4600 ifa
->idev
->dev
->ifindex
);
4602 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
4603 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
4604 preferred
= ifa
->prefered_lft
;
4605 valid
= ifa
->valid_lft
;
4606 if (preferred
!= INFINITY_LIFE_TIME
) {
4607 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
4608 if (preferred
> tval
)
4612 if (valid
!= INFINITY_LIFE_TIME
) {
4620 preferred
= INFINITY_LIFE_TIME
;
4621 valid
= INFINITY_LIFE_TIME
;
4624 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
4625 if (nla_put_in6_addr(skb
, IFA_LOCAL
, &ifa
->addr
) < 0 ||
4626 nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->peer_addr
) < 0)
4629 if (nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->addr
) < 0)
4632 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
4635 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
4638 nlmsg_end(skb
, nlh
);
4642 nlmsg_cancel(skb
, nlh
);
4646 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
4647 u32 portid
, u32 seq
, int event
, u16 flags
)
4649 struct nlmsghdr
*nlh
;
4650 u8 scope
= RT_SCOPE_UNIVERSE
;
4651 int ifindex
= ifmca
->idev
->dev
->ifindex
;
4653 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
4654 scope
= RT_SCOPE_SITE
;
4656 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4660 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4661 if (nla_put_in6_addr(skb
, IFA_MULTICAST
, &ifmca
->mca_addr
) < 0 ||
4662 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
4663 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4664 nlmsg_cancel(skb
, nlh
);
4668 nlmsg_end(skb
, nlh
);
4672 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
4673 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4675 struct nlmsghdr
*nlh
;
4676 u8 scope
= RT_SCOPE_UNIVERSE
;
4677 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
4679 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
4680 scope
= RT_SCOPE_SITE
;
4682 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
4686 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
4687 if (nla_put_in6_addr(skb
, IFA_ANYCAST
, &ifaca
->aca_addr
) < 0 ||
4688 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
4689 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
4690 nlmsg_cancel(skb
, nlh
);
4694 nlmsg_end(skb
, nlh
);
4704 /* called with rcu_read_lock() */
4705 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
4706 struct netlink_callback
*cb
, enum addr_type_t type
,
4707 int s_ip_idx
, int *p_ip_idx
)
4709 struct ifmcaddr6
*ifmca
;
4710 struct ifacaddr6
*ifaca
;
4712 int ip_idx
= *p_ip_idx
;
4714 read_lock_bh(&idev
->lock
);
4716 case UNICAST_ADDR
: {
4717 struct inet6_ifaddr
*ifa
;
4719 /* unicast address incl. temp addr */
4720 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
4721 if (++ip_idx
< s_ip_idx
)
4723 err
= inet6_fill_ifaddr(skb
, ifa
,
4724 NETLINK_CB(cb
->skb
).portid
,
4730 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4734 case MULTICAST_ADDR
:
4735 /* multicast address */
4736 for (ifmca
= idev
->mc_list
; ifmca
;
4737 ifmca
= ifmca
->next
, ip_idx
++) {
4738 if (ip_idx
< s_ip_idx
)
4740 err
= inet6_fill_ifmcaddr(skb
, ifmca
,
4741 NETLINK_CB(cb
->skb
).portid
,
4750 /* anycast address */
4751 for (ifaca
= idev
->ac_list
; ifaca
;
4752 ifaca
= ifaca
->aca_next
, ip_idx
++) {
4753 if (ip_idx
< s_ip_idx
)
4755 err
= inet6_fill_ifacaddr(skb
, ifaca
,
4756 NETLINK_CB(cb
->skb
).portid
,
4767 read_unlock_bh(&idev
->lock
);
4772 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
4773 enum addr_type_t type
)
4775 struct net
*net
= sock_net(skb
->sk
);
4778 int s_idx
, s_ip_idx
;
4779 struct net_device
*dev
;
4780 struct inet6_dev
*idev
;
4781 struct hlist_head
*head
;
4784 s_idx
= idx
= cb
->args
[1];
4785 s_ip_idx
= ip_idx
= cb
->args
[2];
4788 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^ net
->dev_base_seq
;
4789 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4791 head
= &net
->dev_index_head
[h
];
4792 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4795 if (h
> s_h
|| idx
> s_idx
)
4798 idev
= __in6_dev_get(dev
);
4802 if (in6_dump_addrs(idev
, skb
, cb
, type
,
4803 s_ip_idx
, &ip_idx
) < 0)
4813 cb
->args
[2] = ip_idx
;
4818 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4820 enum addr_type_t type
= UNICAST_ADDR
;
4822 return inet6_dump_addr(skb
, cb
, type
);
4825 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4827 enum addr_type_t type
= MULTICAST_ADDR
;
4829 return inet6_dump_addr(skb
, cb
, type
);
4833 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4835 enum addr_type_t type
= ANYCAST_ADDR
;
4837 return inet6_dump_addr(skb
, cb
, type
);
4840 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
)
4842 struct net
*net
= sock_net(in_skb
->sk
);
4843 struct ifaddrmsg
*ifm
;
4844 struct nlattr
*tb
[IFA_MAX
+1];
4845 struct in6_addr
*addr
= NULL
, *peer
;
4846 struct net_device
*dev
= NULL
;
4847 struct inet6_ifaddr
*ifa
;
4848 struct sk_buff
*skb
;
4851 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4855 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
4861 ifm
= nlmsg_data(nlh
);
4863 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4865 ifa
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
4867 err
= -EADDRNOTAVAIL
;
4871 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
4877 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).portid
,
4878 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
4880 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4881 WARN_ON(err
== -EMSGSIZE
);
4885 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4892 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
4894 struct sk_buff
*skb
;
4895 struct net
*net
= dev_net(ifa
->idev
->dev
);
4898 /* Don't send DELADDR notification for TENTATIVE address,
4899 * since NEWADDR notification is sent only after removing
4902 if (ifa
->flags
& IFA_F_TENTATIVE
&& event
== RTM_DELADDR
)
4905 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
4909 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
4911 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4912 WARN_ON(err
== -EMSGSIZE
);
4916 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
4920 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
4923 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
4924 __s32
*array
, int bytes
)
4926 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
4928 memset(array
, 0, bytes
);
4929 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
4930 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
4931 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
4932 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
4933 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
4934 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
4935 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
4936 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
4937 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
4938 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
4939 array
[DEVCONF_RTR_SOLICIT_MAX_INTERVAL
] =
4940 jiffies_to_msecs(cnf
->rtr_solicit_max_interval
);
4941 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
4942 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
4943 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
4944 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
4945 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
4946 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
4947 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
4948 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
4949 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
4950 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
4951 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
4952 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
4953 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
4954 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
4955 array
[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT
] = cnf
->accept_ra_min_hop_limit
;
4956 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
4957 #ifdef CONFIG_IPV6_ROUTER_PREF
4958 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
4959 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
4960 jiffies_to_msecs(cnf
->rtr_probe_interval
);
4961 #ifdef CONFIG_IPV6_ROUTE_INFO
4962 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
4965 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
4966 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
4967 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4968 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
4969 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
4971 #ifdef CONFIG_IPV6_MROUTE
4972 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
4974 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
4975 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
4976 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
4977 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
4978 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
4979 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
4980 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
4981 array
[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN
] = cnf
->ignore_routes_with_linkdown
;
4982 /* we omit DEVCONF_STABLE_SECRET for now */
4983 array
[DEVCONF_USE_OIF_ADDRS_ONLY
] = cnf
->use_oif_addrs_only
;
4984 array
[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST
] = cnf
->drop_unicast_in_l2_multicast
;
4985 array
[DEVCONF_DROP_UNSOLICITED_NA
] = cnf
->drop_unsolicited_na
;
4986 array
[DEVCONF_KEEP_ADDR_ON_DOWN
] = cnf
->keep_addr_on_down
;
4987 array
[DEVCONF_SEG6_ENABLED
] = cnf
->seg6_enabled
;
4988 #ifdef CONFIG_IPV6_SEG6_HMAC
4989 array
[DEVCONF_SEG6_REQUIRE_HMAC
] = cnf
->seg6_require_hmac
;
4991 array
[DEVCONF_ENHANCED_DAD
] = cnf
->enhanced_dad
;
4992 array
[DEVCONF_ADDR_GEN_MODE
] = cnf
->addr_gen_mode
;
4995 static inline size_t inet6_ifla6_size(void)
4997 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4998 + nla_total_size(sizeof(struct ifla_cacheinfo
))
4999 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
5000 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
5001 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
5002 + nla_total_size(sizeof(struct in6_addr
)); /* IFLA_INET6_TOKEN */
5005 static inline size_t inet6_if_nlmsg_size(void)
5007 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
5008 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
5009 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
5010 + nla_total_size(4) /* IFLA_MTU */
5011 + nla_total_size(4) /* IFLA_LINK */
5012 + nla_total_size(1) /* IFLA_OPERSTATE */
5013 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5016 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
5020 int pad
= bytes
- sizeof(u64
) * ICMP6_MIB_MAX
;
5023 /* Use put_unaligned() because stats may not be aligned for u64. */
5024 put_unaligned(ICMP6_MIB_MAX
, &stats
[0]);
5025 for (i
= 1; i
< ICMP6_MIB_MAX
; i
++)
5026 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
5028 memset(&stats
[ICMP6_MIB_MAX
], 0, pad
);
5031 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
5032 int bytes
, size_t syncpoff
)
5035 u64 buff
[IPSTATS_MIB_MAX
];
5036 int pad
= bytes
- sizeof(u64
) * IPSTATS_MIB_MAX
;
5040 memset(buff
, 0, sizeof(buff
));
5041 buff
[0] = IPSTATS_MIB_MAX
;
5043 for_each_possible_cpu(c
) {
5044 for (i
= 1; i
< IPSTATS_MIB_MAX
; i
++)
5045 buff
[i
] += snmp_get_cpu_field64(mib
, c
, i
, syncpoff
);
5048 memcpy(stats
, buff
, IPSTATS_MIB_MAX
* sizeof(u64
));
5049 memset(&stats
[IPSTATS_MIB_MAX
], 0, pad
);
5052 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
5056 case IFLA_INET6_STATS
:
5057 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
, bytes
,
5058 offsetof(struct ipstats_mib
, syncp
));
5060 case IFLA_INET6_ICMP6STATS
:
5061 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, bytes
);
5066 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5067 u32 ext_filter_mask
)
5070 struct ifla_cacheinfo ci
;
5072 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
5073 goto nla_put_failure
;
5074 ci
.max_reasm_len
= IPV6_MAXPLEN
;
5075 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
5076 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
5077 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
5078 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
5079 goto nla_put_failure
;
5080 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
5082 goto nla_put_failure
;
5083 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
5085 /* XXX - MC not implemented */
5087 if (ext_filter_mask
& RTEXT_FILTER_SKIP_STATS
)
5090 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
5092 goto nla_put_failure
;
5093 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
5095 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
5097 goto nla_put_failure
;
5098 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
5100 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
5102 goto nla_put_failure
;
5104 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->cnf
.addr_gen_mode
))
5105 goto nla_put_failure
;
5107 read_lock_bh(&idev
->lock
);
5108 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
5109 read_unlock_bh(&idev
->lock
);
5117 static size_t inet6_get_link_af_size(const struct net_device
*dev
,
5118 u32 ext_filter_mask
)
5120 if (!__in6_dev_get(dev
))
5123 return inet6_ifla6_size();
5126 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
,
5127 u32 ext_filter_mask
)
5129 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5134 if (inet6_fill_ifla6_attrs(skb
, idev
, ext_filter_mask
) < 0)
5140 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
)
5142 struct inet6_ifaddr
*ifp
;
5143 struct net_device
*dev
= idev
->dev
;
5144 bool clear_token
, update_rs
= false;
5145 struct in6_addr ll_addr
;
5151 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
))
5153 if (!ipv6_accept_ra(idev
))
5155 if (idev
->cnf
.rtr_solicits
== 0)
5158 write_lock_bh(&idev
->lock
);
5160 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
5161 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
5163 write_unlock_bh(&idev
->lock
);
5165 clear_token
= ipv6_addr_any(token
);
5169 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
5170 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
5171 IFA_F_OPTIMISTIC
)) {
5172 /* If we're not ready, then normal ifup will take care
5173 * of this. Otherwise, we need to request our rs here.
5175 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
5180 write_lock_bh(&idev
->lock
);
5183 idev
->if_flags
|= IF_RS_SENT
;
5184 idev
->rs_interval
= rfc3315_s14_backoff_init(
5185 idev
->cnf
.rtr_solicit_interval
);
5186 idev
->rs_probes
= 1;
5187 addrconf_mod_rs_timer(idev
, idev
->rs_interval
);
5190 /* Well, that's kinda nasty ... */
5191 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
5192 spin_lock(&ifp
->lock
);
5193 if (ifp
->tokenized
) {
5195 ifp
->prefered_lft
= 0;
5197 spin_unlock(&ifp
->lock
);
5200 write_unlock_bh(&idev
->lock
);
5201 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
5202 addrconf_verify_rtnl();
5206 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
5207 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
5208 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
5211 static int inet6_validate_link_af(const struct net_device
*dev
,
5212 const struct nlattr
*nla
)
5214 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5216 if (dev
&& !__in6_dev_get(dev
))
5217 return -EAFNOSUPPORT
;
5219 return nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, inet6_af_policy
);
5222 static int check_addr_gen_mode(int mode
)
5224 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
5225 mode
!= IN6_ADDR_GEN_MODE_NONE
&&
5226 mode
!= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5227 mode
!= IN6_ADDR_GEN_MODE_RANDOM
)
5232 static int check_stable_privacy(struct inet6_dev
*idev
, struct net
*net
,
5235 if (mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5236 !idev
->cnf
.stable_secret
.initialized
&&
5237 !net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
5242 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
)
5245 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5246 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5249 return -EAFNOSUPPORT
;
5251 if (nla_parse_nested(tb
, IFLA_INET6_MAX
, nla
, NULL
) < 0)
5254 if (tb
[IFLA_INET6_TOKEN
]) {
5255 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]));
5260 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
5261 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
5263 if (check_addr_gen_mode(mode
) < 0 ||
5264 check_stable_privacy(idev
, dev_net(dev
), mode
) < 0)
5267 idev
->cnf
.addr_gen_mode
= mode
;
5274 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5275 u32 portid
, u32 seq
, int event
, unsigned int flags
)
5277 struct net_device
*dev
= idev
->dev
;
5278 struct ifinfomsg
*hdr
;
5279 struct nlmsghdr
*nlh
;
5282 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
5286 hdr
= nlmsg_data(nlh
);
5287 hdr
->ifi_family
= AF_INET6
;
5289 hdr
->ifi_type
= dev
->type
;
5290 hdr
->ifi_index
= dev
->ifindex
;
5291 hdr
->ifi_flags
= dev_get_flags(dev
);
5292 hdr
->ifi_change
= 0;
5294 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
5296 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
5297 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
5298 (dev
->ifindex
!= dev_get_iflink(dev
) &&
5299 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
5300 nla_put_u8(skb
, IFLA_OPERSTATE
,
5301 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
))
5302 goto nla_put_failure
;
5303 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
5305 goto nla_put_failure
;
5307 if (inet6_fill_ifla6_attrs(skb
, idev
, 0) < 0)
5308 goto nla_put_failure
;
5310 nla_nest_end(skb
, protoinfo
);
5311 nlmsg_end(skb
, nlh
);
5315 nlmsg_cancel(skb
, nlh
);
5319 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5321 struct net
*net
= sock_net(skb
->sk
);
5324 struct net_device
*dev
;
5325 struct inet6_dev
*idev
;
5326 struct hlist_head
*head
;
5329 s_idx
= cb
->args
[1];
5332 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5334 head
= &net
->dev_index_head
[h
];
5335 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
5338 idev
= __in6_dev_get(dev
);
5341 if (inet6_fill_ifinfo(skb
, idev
,
5342 NETLINK_CB(cb
->skb
).portid
,
5344 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
5358 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
5360 struct sk_buff
*skb
;
5361 struct net
*net
= dev_net(idev
->dev
);
5364 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
5368 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
5370 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5371 WARN_ON(err
== -EMSGSIZE
);
5375 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
5379 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
5382 static inline size_t inet6_prefix_nlmsg_size(void)
5384 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
5385 + nla_total_size(sizeof(struct in6_addr
))
5386 + nla_total_size(sizeof(struct prefix_cacheinfo
));
5389 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5390 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
5391 int event
, unsigned int flags
)
5393 struct prefixmsg
*pmsg
;
5394 struct nlmsghdr
*nlh
;
5395 struct prefix_cacheinfo ci
;
5397 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
5401 pmsg
= nlmsg_data(nlh
);
5402 pmsg
->prefix_family
= AF_INET6
;
5403 pmsg
->prefix_pad1
= 0;
5404 pmsg
->prefix_pad2
= 0;
5405 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
5406 pmsg
->prefix_len
= pinfo
->prefix_len
;
5407 pmsg
->prefix_type
= pinfo
->type
;
5408 pmsg
->prefix_pad3
= 0;
5409 pmsg
->prefix_flags
= 0;
5411 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
5412 if (pinfo
->autoconf
)
5413 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
5415 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
5416 goto nla_put_failure
;
5417 ci
.preferred_time
= ntohl(pinfo
->prefered
);
5418 ci
.valid_time
= ntohl(pinfo
->valid
);
5419 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
5420 goto nla_put_failure
;
5421 nlmsg_end(skb
, nlh
);
5425 nlmsg_cancel(skb
, nlh
);
5429 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
5430 struct prefix_info
*pinfo
)
5432 struct sk_buff
*skb
;
5433 struct net
*net
= dev_net(idev
->dev
);
5436 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
5440 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
5442 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5443 WARN_ON(err
== -EMSGSIZE
);
5447 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
5451 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
5454 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5456 struct net
*net
= dev_net(ifp
->idev
->dev
);
5461 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
5466 * If the address was optimistic
5467 * we inserted the route at the start of
5468 * our DAD process, so we don't need
5471 if (!(ifp
->rt
->rt6i_node
))
5472 ip6_ins_rt(ifp
->rt
);
5473 if (ifp
->idev
->cnf
.forwarding
)
5474 addrconf_join_anycast(ifp
);
5475 if (!ipv6_addr_any(&ifp
->peer_addr
))
5476 addrconf_prefix_route(&ifp
->peer_addr
, 128,
5477 ifp
->idev
->dev
, 0, 0);
5480 if (ifp
->idev
->cnf
.forwarding
)
5481 addrconf_leave_anycast(ifp
);
5482 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
5483 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
5484 struct rt6_info
*rt
;
5486 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
5487 ifp
->idev
->dev
, 0, 0);
5492 dst_hold(&ifp
->rt
->dst
);
5493 ip6_del_rt(ifp
->rt
);
5495 rt_genid_bump_ipv6(net
);
5498 atomic_inc(&net
->ipv6
.dev_addr_genid
);
5501 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
5504 if (likely(ifp
->idev
->dead
== 0))
5505 __ipv6_ifa_notify(event
, ifp
);
5506 rcu_read_unlock_bh();
5509 #ifdef CONFIG_SYSCTL
5512 int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
5513 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5515 int *valp
= ctl
->data
;
5518 struct ctl_table lctl
;
5522 * ctl->data points to idev->cnf.forwarding, we should
5523 * not modify it until we get the rtnl lock.
5528 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5531 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
5538 int addrconf_sysctl_mtu(struct ctl_table
*ctl
, int write
,
5539 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5541 struct inet6_dev
*idev
= ctl
->extra1
;
5542 int min_mtu
= IPV6_MIN_MTU
;
5543 struct ctl_table lctl
;
5546 lctl
.extra1
= &min_mtu
;
5547 lctl
.extra2
= idev
? &idev
->dev
->mtu
: NULL
;
5549 return proc_dointvec_minmax(&lctl
, write
, buffer
, lenp
, ppos
);
5552 static void dev_disable_change(struct inet6_dev
*idev
)
5554 struct netdev_notifier_info info
;
5556 if (!idev
|| !idev
->dev
)
5559 netdev_notifier_info_init(&info
, idev
->dev
);
5560 if (idev
->cnf
.disable_ipv6
)
5561 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
5563 addrconf_notify(NULL
, NETDEV_UP
, &info
);
5566 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
5568 struct net_device
*dev
;
5569 struct inet6_dev
*idev
;
5571 for_each_netdev(net
, dev
) {
5572 idev
= __in6_dev_get(dev
);
5574 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
5575 idev
->cnf
.disable_ipv6
= newf
;
5577 dev_disable_change(idev
);
5582 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
5587 if (!rtnl_trylock())
5588 return restart_syscall();
5590 net
= (struct net
*)table
->extra2
;
5594 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
5599 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
5600 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
5601 addrconf_disable_change(net
, newf
);
5602 } else if ((!newf
) ^ (!old
))
5603 dev_disable_change((struct inet6_dev
*)table
->extra1
);
5610 int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
5611 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5613 int *valp
= ctl
->data
;
5616 struct ctl_table lctl
;
5620 * ctl->data points to idev->cnf.disable_ipv6, we should
5621 * not modify it until we get the rtnl lock.
5626 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5629 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
5636 int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
5637 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
5639 int *valp
= ctl
->data
;
5644 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
5647 if (write
&& old
!= new) {
5648 struct net
*net
= ctl
->extra2
;
5650 if (!rtnl_trylock())
5651 return restart_syscall();
5653 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
5654 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5655 NETCONFA_IFINDEX_DEFAULT
,
5656 net
->ipv6
.devconf_dflt
);
5657 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
5658 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5659 NETCONFA_IFINDEX_ALL
,
5660 net
->ipv6
.devconf_all
);
5662 struct inet6_dev
*idev
= ctl
->extra1
;
5664 inet6_netconf_notify_devconf(net
, NETCONFA_PROXY_NEIGH
,
5674 static int addrconf_sysctl_addr_gen_mode(struct ctl_table
*ctl
, int write
,
5675 void __user
*buffer
, size_t *lenp
,
5680 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
5681 struct net
*net
= (struct net
*)ctl
->extra2
;
5683 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
5686 new_val
= *((int *)ctl
->data
);
5688 if (check_addr_gen_mode(new_val
) < 0)
5691 /* request for default */
5692 if (&net
->ipv6
.devconf_dflt
->addr_gen_mode
== ctl
->data
) {
5693 ipv6_devconf_dflt
.addr_gen_mode
= new_val
;
5695 /* request for individual net device */
5700 if (check_stable_privacy(idev
, net
, new_val
) < 0)
5703 if (idev
->cnf
.addr_gen_mode
!= new_val
) {
5704 idev
->cnf
.addr_gen_mode
= new_val
;
5706 addrconf_dev_config(idev
->dev
);
5715 static int addrconf_sysctl_stable_secret(struct ctl_table
*ctl
, int write
,
5716 void __user
*buffer
, size_t *lenp
,
5720 struct in6_addr addr
;
5721 char str
[IPV6_MAX_STRLEN
];
5722 struct ctl_table lctl
= *ctl
;
5723 struct net
*net
= ctl
->extra2
;
5724 struct ipv6_stable_secret
*secret
= ctl
->data
;
5726 if (&net
->ipv6
.devconf_all
->stable_secret
== ctl
->data
)
5729 lctl
.maxlen
= IPV6_MAX_STRLEN
;
5732 if (!rtnl_trylock())
5733 return restart_syscall();
5735 if (!write
&& !secret
->initialized
) {
5740 err
= snprintf(str
, sizeof(str
), "%pI6", &secret
->secret
);
5741 if (err
>= sizeof(str
)) {
5746 err
= proc_dostring(&lctl
, write
, buffer
, lenp
, ppos
);
5750 if (in6_pton(str
, -1, addr
.in6_u
.u6_addr8
, -1, NULL
) != 1) {
5755 secret
->initialized
= true;
5756 secret
->secret
= addr
;
5758 if (&net
->ipv6
.devconf_dflt
->stable_secret
== ctl
->data
) {
5759 struct net_device
*dev
;
5761 for_each_netdev(net
, dev
) {
5762 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5765 idev
->cnf
.addr_gen_mode
=
5766 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
5770 struct inet6_dev
*idev
= ctl
->extra1
;
5772 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
5782 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table
*ctl
,
5784 void __user
*buffer
,
5788 int *valp
= ctl
->data
;
5791 struct ctl_table lctl
;
5794 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5795 * we should not modify it until we get the rtnl lock.
5800 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
5803 ret
= addrconf_fixup_linkdown(ctl
, valp
, val
);
5809 static int minus_one
= -1;
5810 static const int one
= 1;
5811 static const int two_five_five
= 255;
5813 static const struct ctl_table addrconf_sysctl
[] = {
5815 .procname
= "forwarding",
5816 .data
= &ipv6_devconf
.forwarding
,
5817 .maxlen
= sizeof(int),
5819 .proc_handler
= addrconf_sysctl_forward
,
5822 .procname
= "hop_limit",
5823 .data
= &ipv6_devconf
.hop_limit
,
5824 .maxlen
= sizeof(int),
5826 .proc_handler
= proc_dointvec_minmax
,
5827 .extra1
= (void *)&one
,
5828 .extra2
= (void *)&two_five_five
,
5832 .data
= &ipv6_devconf
.mtu6
,
5833 .maxlen
= sizeof(int),
5835 .proc_handler
= addrconf_sysctl_mtu
,
5838 .procname
= "accept_ra",
5839 .data
= &ipv6_devconf
.accept_ra
,
5840 .maxlen
= sizeof(int),
5842 .proc_handler
= proc_dointvec
,
5845 .procname
= "accept_redirects",
5846 .data
= &ipv6_devconf
.accept_redirects
,
5847 .maxlen
= sizeof(int),
5849 .proc_handler
= proc_dointvec
,
5852 .procname
= "autoconf",
5853 .data
= &ipv6_devconf
.autoconf
,
5854 .maxlen
= sizeof(int),
5856 .proc_handler
= proc_dointvec
,
5859 .procname
= "dad_transmits",
5860 .data
= &ipv6_devconf
.dad_transmits
,
5861 .maxlen
= sizeof(int),
5863 .proc_handler
= proc_dointvec
,
5866 .procname
= "router_solicitations",
5867 .data
= &ipv6_devconf
.rtr_solicits
,
5868 .maxlen
= sizeof(int),
5870 .proc_handler
= proc_dointvec_minmax
,
5871 .extra1
= &minus_one
,
5874 .procname
= "router_solicitation_interval",
5875 .data
= &ipv6_devconf
.rtr_solicit_interval
,
5876 .maxlen
= sizeof(int),
5878 .proc_handler
= proc_dointvec_jiffies
,
5881 .procname
= "router_solicitation_max_interval",
5882 .data
= &ipv6_devconf
.rtr_solicit_max_interval
,
5883 .maxlen
= sizeof(int),
5885 .proc_handler
= proc_dointvec_jiffies
,
5888 .procname
= "router_solicitation_delay",
5889 .data
= &ipv6_devconf
.rtr_solicit_delay
,
5890 .maxlen
= sizeof(int),
5892 .proc_handler
= proc_dointvec_jiffies
,
5895 .procname
= "force_mld_version",
5896 .data
= &ipv6_devconf
.force_mld_version
,
5897 .maxlen
= sizeof(int),
5899 .proc_handler
= proc_dointvec
,
5902 .procname
= "mldv1_unsolicited_report_interval",
5904 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
5905 .maxlen
= sizeof(int),
5907 .proc_handler
= proc_dointvec_ms_jiffies
,
5910 .procname
= "mldv2_unsolicited_report_interval",
5912 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
5913 .maxlen
= sizeof(int),
5915 .proc_handler
= proc_dointvec_ms_jiffies
,
5918 .procname
= "use_tempaddr",
5919 .data
= &ipv6_devconf
.use_tempaddr
,
5920 .maxlen
= sizeof(int),
5922 .proc_handler
= proc_dointvec
,
5925 .procname
= "temp_valid_lft",
5926 .data
= &ipv6_devconf
.temp_valid_lft
,
5927 .maxlen
= sizeof(int),
5929 .proc_handler
= proc_dointvec
,
5932 .procname
= "temp_prefered_lft",
5933 .data
= &ipv6_devconf
.temp_prefered_lft
,
5934 .maxlen
= sizeof(int),
5936 .proc_handler
= proc_dointvec
,
5939 .procname
= "regen_max_retry",
5940 .data
= &ipv6_devconf
.regen_max_retry
,
5941 .maxlen
= sizeof(int),
5943 .proc_handler
= proc_dointvec
,
5946 .procname
= "max_desync_factor",
5947 .data
= &ipv6_devconf
.max_desync_factor
,
5948 .maxlen
= sizeof(int),
5950 .proc_handler
= proc_dointvec
,
5953 .procname
= "max_addresses",
5954 .data
= &ipv6_devconf
.max_addresses
,
5955 .maxlen
= sizeof(int),
5957 .proc_handler
= proc_dointvec
,
5960 .procname
= "accept_ra_defrtr",
5961 .data
= &ipv6_devconf
.accept_ra_defrtr
,
5962 .maxlen
= sizeof(int),
5964 .proc_handler
= proc_dointvec
,
5967 .procname
= "accept_ra_min_hop_limit",
5968 .data
= &ipv6_devconf
.accept_ra_min_hop_limit
,
5969 .maxlen
= sizeof(int),
5971 .proc_handler
= proc_dointvec
,
5974 .procname
= "accept_ra_pinfo",
5975 .data
= &ipv6_devconf
.accept_ra_pinfo
,
5976 .maxlen
= sizeof(int),
5978 .proc_handler
= proc_dointvec
,
5980 #ifdef CONFIG_IPV6_ROUTER_PREF
5982 .procname
= "accept_ra_rtr_pref",
5983 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
5984 .maxlen
= sizeof(int),
5986 .proc_handler
= proc_dointvec
,
5989 .procname
= "router_probe_interval",
5990 .data
= &ipv6_devconf
.rtr_probe_interval
,
5991 .maxlen
= sizeof(int),
5993 .proc_handler
= proc_dointvec_jiffies
,
5995 #ifdef CONFIG_IPV6_ROUTE_INFO
5997 .procname
= "accept_ra_rt_info_max_plen",
5998 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
5999 .maxlen
= sizeof(int),
6001 .proc_handler
= proc_dointvec
,
6006 .procname
= "proxy_ndp",
6007 .data
= &ipv6_devconf
.proxy_ndp
,
6008 .maxlen
= sizeof(int),
6010 .proc_handler
= addrconf_sysctl_proxy_ndp
,
6013 .procname
= "accept_source_route",
6014 .data
= &ipv6_devconf
.accept_source_route
,
6015 .maxlen
= sizeof(int),
6017 .proc_handler
= proc_dointvec
,
6019 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6021 .procname
= "optimistic_dad",
6022 .data
= &ipv6_devconf
.optimistic_dad
,
6023 .maxlen
= sizeof(int),
6025 .proc_handler
= proc_dointvec
,
6028 .procname
= "use_optimistic",
6029 .data
= &ipv6_devconf
.use_optimistic
,
6030 .maxlen
= sizeof(int),
6032 .proc_handler
= proc_dointvec
,
6035 #ifdef CONFIG_IPV6_MROUTE
6037 .procname
= "mc_forwarding",
6038 .data
= &ipv6_devconf
.mc_forwarding
,
6039 .maxlen
= sizeof(int),
6041 .proc_handler
= proc_dointvec
,
6045 .procname
= "disable_ipv6",
6046 .data
= &ipv6_devconf
.disable_ipv6
,
6047 .maxlen
= sizeof(int),
6049 .proc_handler
= addrconf_sysctl_disable
,
6052 .procname
= "accept_dad",
6053 .data
= &ipv6_devconf
.accept_dad
,
6054 .maxlen
= sizeof(int),
6056 .proc_handler
= proc_dointvec
,
6059 .procname
= "force_tllao",
6060 .data
= &ipv6_devconf
.force_tllao
,
6061 .maxlen
= sizeof(int),
6063 .proc_handler
= proc_dointvec
6066 .procname
= "ndisc_notify",
6067 .data
= &ipv6_devconf
.ndisc_notify
,
6068 .maxlen
= sizeof(int),
6070 .proc_handler
= proc_dointvec
6073 .procname
= "suppress_frag_ndisc",
6074 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
6075 .maxlen
= sizeof(int),
6077 .proc_handler
= proc_dointvec
6080 .procname
= "accept_ra_from_local",
6081 .data
= &ipv6_devconf
.accept_ra_from_local
,
6082 .maxlen
= sizeof(int),
6084 .proc_handler
= proc_dointvec
,
6087 .procname
= "accept_ra_mtu",
6088 .data
= &ipv6_devconf
.accept_ra_mtu
,
6089 .maxlen
= sizeof(int),
6091 .proc_handler
= proc_dointvec
,
6094 .procname
= "stable_secret",
6095 .data
= &ipv6_devconf
.stable_secret
,
6096 .maxlen
= IPV6_MAX_STRLEN
,
6098 .proc_handler
= addrconf_sysctl_stable_secret
,
6101 .procname
= "use_oif_addrs_only",
6102 .data
= &ipv6_devconf
.use_oif_addrs_only
,
6103 .maxlen
= sizeof(int),
6105 .proc_handler
= proc_dointvec
,
6108 .procname
= "ignore_routes_with_linkdown",
6109 .data
= &ipv6_devconf
.ignore_routes_with_linkdown
,
6110 .maxlen
= sizeof(int),
6112 .proc_handler
= addrconf_sysctl_ignore_routes_with_linkdown
,
6115 .procname
= "drop_unicast_in_l2_multicast",
6116 .data
= &ipv6_devconf
.drop_unicast_in_l2_multicast
,
6117 .maxlen
= sizeof(int),
6119 .proc_handler
= proc_dointvec
,
6122 .procname
= "drop_unsolicited_na",
6123 .data
= &ipv6_devconf
.drop_unsolicited_na
,
6124 .maxlen
= sizeof(int),
6126 .proc_handler
= proc_dointvec
,
6129 .procname
= "keep_addr_on_down",
6130 .data
= &ipv6_devconf
.keep_addr_on_down
,
6131 .maxlen
= sizeof(int),
6133 .proc_handler
= proc_dointvec
,
6137 .procname
= "seg6_enabled",
6138 .data
= &ipv6_devconf
.seg6_enabled
,
6139 .maxlen
= sizeof(int),
6141 .proc_handler
= proc_dointvec
,
6143 #ifdef CONFIG_IPV6_SEG6_HMAC
6145 .procname
= "seg6_require_hmac",
6146 .data
= &ipv6_devconf
.seg6_require_hmac
,
6147 .maxlen
= sizeof(int),
6149 .proc_handler
= proc_dointvec
,
6153 .procname
= "enhanced_dad",
6154 .data
= &ipv6_devconf
.enhanced_dad
,
6155 .maxlen
= sizeof(int),
6157 .proc_handler
= proc_dointvec
,
6160 .procname
= "addr_gen_mode",
6161 .data
= &ipv6_devconf
.addr_gen_mode
,
6162 .maxlen
= sizeof(int),
6164 .proc_handler
= addrconf_sysctl_addr_gen_mode
,
6171 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
6172 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
6175 struct ctl_table
*table
;
6176 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
6178 table
= kmemdup(addrconf_sysctl
, sizeof(addrconf_sysctl
), GFP_KERNEL
);
6182 for (i
= 0; table
[i
].data
; i
++) {
6183 table
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
6184 /* If one of these is already set, then it is not safe to
6185 * overwrite either of them: this makes proc_dointvec_minmax
6188 if (!table
[i
].extra1
&& !table
[i
].extra2
) {
6189 table
[i
].extra1
= idev
; /* embedded; no ref */
6190 table
[i
].extra2
= net
;
6194 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
6196 p
->sysctl_header
= register_net_sysctl(net
, path
, table
);
6197 if (!p
->sysctl_header
)
6200 if (!strcmp(dev_name
, "all"))
6201 ifindex
= NETCONFA_IFINDEX_ALL
;
6202 else if (!strcmp(dev_name
, "default"))
6203 ifindex
= NETCONFA_IFINDEX_DEFAULT
;
6205 ifindex
= idev
->dev
->ifindex
;
6206 inet6_netconf_notify_devconf(net
, NETCONFA_ALL
, ifindex
, p
);
6215 static void __addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
6217 struct ctl_table
*table
;
6219 if (!p
->sysctl_header
)
6222 table
= p
->sysctl_header
->ctl_table_arg
;
6223 unregister_net_sysctl_table(p
->sysctl_header
);
6224 p
->sysctl_header
= NULL
;
6228 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
6232 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
6235 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
6236 &ndisc_ifinfo_sysctl_change
);
6239 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
6242 neigh_sysctl_unregister(idev
->nd_parms
);
6247 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
6249 __addrconf_sysctl_unregister(&idev
->cnf
);
6250 neigh_sysctl_unregister(idev
->nd_parms
);
6256 static int __net_init
addrconf_init_net(struct net
*net
)
6259 struct ipv6_devconf
*all
, *dflt
;
6261 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
6265 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
6267 goto err_alloc_dflt
;
6269 /* these will be inherited by all namespaces */
6270 dflt
->autoconf
= ipv6_defaults
.autoconf
;
6271 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
6273 dflt
->stable_secret
.initialized
= false;
6274 all
->stable_secret
.initialized
= false;
6276 net
->ipv6
.devconf_all
= all
;
6277 net
->ipv6
.devconf_dflt
= dflt
;
6279 #ifdef CONFIG_SYSCTL
6280 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
6284 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
6290 #ifdef CONFIG_SYSCTL
6292 __addrconf_sysctl_unregister(all
);
6302 static void __net_exit
addrconf_exit_net(struct net
*net
)
6304 #ifdef CONFIG_SYSCTL
6305 __addrconf_sysctl_unregister(net
->ipv6
.devconf_dflt
);
6306 __addrconf_sysctl_unregister(net
->ipv6
.devconf_all
);
6308 kfree(net
->ipv6
.devconf_dflt
);
6309 kfree(net
->ipv6
.devconf_all
);
6312 static struct pernet_operations addrconf_ops
= {
6313 .init
= addrconf_init_net
,
6314 .exit
= addrconf_exit_net
,
6317 static struct rtnl_af_ops inet6_ops __read_mostly
= {
6319 .fill_link_af
= inet6_fill_link_af
,
6320 .get_link_af_size
= inet6_get_link_af_size
,
6321 .validate_link_af
= inet6_validate_link_af
,
6322 .set_link_af
= inet6_set_link_af
,
6326 * Init / cleanup code
6329 int __init
addrconf_init(void)
6331 struct inet6_dev
*idev
;
6334 err
= ipv6_addr_label_init();
6336 pr_crit("%s: cannot initialize default policy table: %d\n",
6341 err
= register_pernet_subsys(&addrconf_ops
);
6345 addrconf_wq
= create_workqueue("ipv6_addrconf");
6351 /* The addrconf netdev notifier requires that loopback_dev
6352 * has it's ipv6 private information allocated and setup
6353 * before it can bring up and give link-local addresses
6354 * to other devices which are up.
6356 * Unfortunately, loopback_dev is not necessarily the first
6357 * entry in the global dev_base list of net devices. In fact,
6358 * it is likely to be the very last entry on that list.
6359 * So this causes the notifier registry below to try and
6360 * give link-local addresses to all devices besides loopback_dev
6361 * first, then loopback_dev, which cases all the non-loopback_dev
6362 * devices to fail to get a link-local address.
6364 * So, as a temporary fix, allocate the ipv6 structure for
6365 * loopback_dev first by hand.
6366 * Longer term, all of the dependencies ipv6 has upon the loopback
6367 * device and it being up should be removed.
6370 idev
= ipv6_add_dev(init_net
.loopback_dev
);
6373 err
= PTR_ERR(idev
);
6377 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
6378 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
6380 register_netdevice_notifier(&ipv6_dev_notf
);
6384 rtnl_af_register(&inet6_ops
);
6386 err
= __rtnl_register(PF_INET6
, RTM_GETLINK
, NULL
, inet6_dump_ifinfo
,
6391 /* Only the first call to __rtnl_register can fail */
6392 __rtnl_register(PF_INET6
, RTM_NEWADDR
, inet6_rtm_newaddr
, NULL
, NULL
);
6393 __rtnl_register(PF_INET6
, RTM_DELADDR
, inet6_rtm_deladdr
, NULL
, NULL
);
6394 __rtnl_register(PF_INET6
, RTM_GETADDR
, inet6_rtm_getaddr
,
6395 inet6_dump_ifaddr
, NULL
);
6396 __rtnl_register(PF_INET6
, RTM_GETMULTICAST
, NULL
,
6397 inet6_dump_ifmcaddr
, NULL
);
6398 __rtnl_register(PF_INET6
, RTM_GETANYCAST
, NULL
,
6399 inet6_dump_ifacaddr
, NULL
);
6400 __rtnl_register(PF_INET6
, RTM_GETNETCONF
, inet6_netconf_get_devconf
,
6401 inet6_netconf_dump_devconf
, NULL
);
6403 ipv6_addr_label_rtnl_register();
6407 rtnl_af_unregister(&inet6_ops
);
6408 unregister_netdevice_notifier(&ipv6_dev_notf
);
6410 destroy_workqueue(addrconf_wq
);
6412 unregister_pernet_subsys(&addrconf_ops
);
6414 ipv6_addr_label_cleanup();
6419 void addrconf_cleanup(void)
6421 struct net_device
*dev
;
6424 unregister_netdevice_notifier(&ipv6_dev_notf
);
6425 unregister_pernet_subsys(&addrconf_ops
);
6426 ipv6_addr_label_cleanup();
6430 __rtnl_af_unregister(&inet6_ops
);
6432 /* clean dev list */
6433 for_each_netdev(&init_net
, dev
) {
6434 if (__in6_dev_get(dev
) == NULL
)
6436 addrconf_ifdown(dev
, 1);
6438 addrconf_ifdown(init_net
.loopback_dev
, 2);
6443 spin_lock_bh(&addrconf_hash_lock
);
6444 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
6445 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
6446 spin_unlock_bh(&addrconf_hash_lock
);
6447 cancel_delayed_work(&addr_chk_work
);
6450 destroy_workqueue(addrconf_wq
);