2 * Anycast support for IPv6
3 * Linux INET6 implementation
6 * David L Stevens (dlstevens@us.ibm.com)
8 * based heavily on net/ipv6/mcast.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/capability.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/random.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/in6.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/route.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
34 #include <net/net_namespace.h>
39 #include <net/protocol.h>
40 #include <net/if_inet6.h>
41 #include <net/ndisc.h>
42 #include <net/addrconf.h>
43 #include <net/ip6_route.h>
45 #include <net/checksum.h>
47 #define IN6_ADDR_HSIZE_SHIFT 8
48 #define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT)
49 /* anycast address hash table
51 static struct hlist_head inet6_acaddr_lst
[IN6_ADDR_HSIZE
];
52 static DEFINE_SPINLOCK(acaddr_hash_lock
);
54 static int ipv6_dev_ac_dec(struct net_device
*dev
, const struct in6_addr
*addr
);
56 static u32
inet6_acaddr_hash(struct net
*net
, const struct in6_addr
*addr
)
58 u32 val
= ipv6_addr_hash(addr
) ^ net_hash_mix(net
);
60 return hash_32(val
, IN6_ADDR_HSIZE_SHIFT
);
64 * socket join an anycast group
67 int ipv6_sock_ac_join(struct sock
*sk
, int ifindex
, const struct in6_addr
*addr
)
69 struct ipv6_pinfo
*np
= inet6_sk(sk
);
70 struct net_device
*dev
= NULL
;
71 struct inet6_dev
*idev
;
72 struct ipv6_ac_socklist
*pac
;
73 struct net
*net
= sock_net(sk
);
74 int ishost
= !net
->ipv6
.devconf_all
->forwarding
;
79 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
81 if (ipv6_addr_is_multicast(addr
))
85 dev
= __dev_get_by_index(net
, ifindex
);
87 if (ipv6_chk_addr_and_flags(net
, addr
, dev
, true, 0, IFA_F_TENTATIVE
))
90 pac
= sock_kmalloc(sk
, sizeof(struct ipv6_ac_socklist
), GFP_KERNEL
);
94 pac
->acl_addr
= *addr
;
99 rt
= rt6_lookup(net
, addr
, NULL
, 0, NULL
, 0);
104 err
= -EADDRNOTAVAIL
;
107 /* router, no matching interface: just pick one */
108 dev
= __dev_get_by_flags(net
, IFF_UP
,
109 IFF_UP
| IFF_LOOPBACK
);
118 idev
= __in6_dev_get(dev
);
123 err
= -EADDRNOTAVAIL
;
126 /* reset ishost, now that we have a specific device */
127 ishost
= !idev
->cnf
.forwarding
;
129 pac
->acl_ifindex
= dev
->ifindex
;
132 * For hosts, allow link-local or matching prefix anycasts.
133 * This obviates the need for propagating anycast routes while
134 * still allowing some non-router anycast participation.
136 if (!ipv6_chk_prefix(addr
, dev
)) {
138 err
= -EADDRNOTAVAIL
;
143 err
= __ipv6_dev_ac_inc(idev
, addr
);
145 pac
->acl_next
= np
->ipv6_ac_list
;
146 np
->ipv6_ac_list
= pac
;
152 sock_kfree_s(sk
, pac
, sizeof(*pac
));
157 * socket leave an anycast group
159 int ipv6_sock_ac_drop(struct sock
*sk
, int ifindex
, const struct in6_addr
*addr
)
161 struct ipv6_pinfo
*np
= inet6_sk(sk
);
162 struct net_device
*dev
;
163 struct ipv6_ac_socklist
*pac
, *prev_pac
;
164 struct net
*net
= sock_net(sk
);
169 for (pac
= np
->ipv6_ac_list
; pac
; pac
= pac
->acl_next
) {
170 if ((ifindex
== 0 || pac
->acl_ifindex
== ifindex
) &&
171 ipv6_addr_equal(&pac
->acl_addr
, addr
))
178 prev_pac
->acl_next
= pac
->acl_next
;
180 np
->ipv6_ac_list
= pac
->acl_next
;
182 dev
= __dev_get_by_index(net
, pac
->acl_ifindex
);
184 ipv6_dev_ac_dec(dev
, &pac
->acl_addr
);
186 sock_kfree_s(sk
, pac
, sizeof(*pac
));
190 void ipv6_sock_ac_close(struct sock
*sk
)
192 struct ipv6_pinfo
*np
= inet6_sk(sk
);
193 struct net_device
*dev
= NULL
;
194 struct ipv6_ac_socklist
*pac
;
195 struct net
*net
= sock_net(sk
);
198 if (!np
->ipv6_ac_list
)
202 pac
= np
->ipv6_ac_list
;
203 np
->ipv6_ac_list
= NULL
;
207 struct ipv6_ac_socklist
*next
= pac
->acl_next
;
209 if (pac
->acl_ifindex
!= prev_index
) {
210 dev
= __dev_get_by_index(net
, pac
->acl_ifindex
);
211 prev_index
= pac
->acl_ifindex
;
214 ipv6_dev_ac_dec(dev
, &pac
->acl_addr
);
215 sock_kfree_s(sk
, pac
, sizeof(*pac
));
221 static void ipv6_add_acaddr_hash(struct net
*net
, struct ifacaddr6
*aca
)
223 unsigned int hash
= inet6_acaddr_hash(net
, &aca
->aca_addr
);
225 spin_lock(&acaddr_hash_lock
);
226 hlist_add_head_rcu(&aca
->aca_addr_lst
, &inet6_acaddr_lst
[hash
]);
227 spin_unlock(&acaddr_hash_lock
);
230 static void ipv6_del_acaddr_hash(struct ifacaddr6
*aca
)
232 spin_lock(&acaddr_hash_lock
);
233 hlist_del_init_rcu(&aca
->aca_addr_lst
);
234 spin_unlock(&acaddr_hash_lock
);
237 static void aca_get(struct ifacaddr6
*aca
)
239 refcount_inc(&aca
->aca_refcnt
);
242 static void aca_free_rcu(struct rcu_head
*h
)
244 struct ifacaddr6
*aca
= container_of(h
, struct ifacaddr6
, rcu
);
246 fib6_info_release(aca
->aca_rt
);
250 static void aca_put(struct ifacaddr6
*ac
)
252 if (refcount_dec_and_test(&ac
->aca_refcnt
)) {
253 call_rcu(&ac
->rcu
, aca_free_rcu
);
257 static struct ifacaddr6
*aca_alloc(struct fib6_info
*f6i
,
258 const struct in6_addr
*addr
)
260 struct ifacaddr6
*aca
;
262 aca
= kzalloc(sizeof(*aca
), GFP_ATOMIC
);
266 aca
->aca_addr
= *addr
;
269 INIT_HLIST_NODE(&aca
->aca_addr_lst
);
271 /* aca_tstamp should be updated upon changes */
272 aca
->aca_cstamp
= aca
->aca_tstamp
= jiffies
;
273 refcount_set(&aca
->aca_refcnt
, 1);
279 * device anycast group inc (add if not found)
281 int __ipv6_dev_ac_inc(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
283 struct ifacaddr6
*aca
;
284 struct fib6_info
*f6i
;
290 write_lock_bh(&idev
->lock
);
296 for (aca
= idev
->ac_list
; aca
; aca
= aca
->aca_next
) {
297 if (ipv6_addr_equal(&aca
->aca_addr
, addr
)) {
304 net
= dev_net(idev
->dev
);
305 f6i
= addrconf_f6i_alloc(net
, idev
, addr
, true, GFP_ATOMIC
);
310 aca
= aca_alloc(f6i
, addr
);
312 fib6_info_release(f6i
);
317 aca
->aca_next
= idev
->ac_list
;
320 /* Hold this for addrconf_join_solict() below before we unlock,
321 * it is already exposed via idev->ac_list.
324 write_unlock_bh(&idev
->lock
);
326 ipv6_add_acaddr_hash(net
, aca
);
328 ip6_ins_rt(net
, f6i
);
330 addrconf_join_solict(idev
->dev
, &aca
->aca_addr
);
335 write_unlock_bh(&idev
->lock
);
340 * device anycast group decrement
342 int __ipv6_dev_ac_dec(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
344 struct ifacaddr6
*aca
, *prev_aca
;
348 write_lock_bh(&idev
->lock
);
350 for (aca
= idev
->ac_list
; aca
; aca
= aca
->aca_next
) {
351 if (ipv6_addr_equal(&aca
->aca_addr
, addr
))
356 write_unlock_bh(&idev
->lock
);
359 if (--aca
->aca_users
> 0) {
360 write_unlock_bh(&idev
->lock
);
364 prev_aca
->aca_next
= aca
->aca_next
;
366 idev
->ac_list
= aca
->aca_next
;
367 write_unlock_bh(&idev
->lock
);
368 ipv6_del_acaddr_hash(aca
);
369 addrconf_leave_solict(idev
, &aca
->aca_addr
);
371 ip6_del_rt(dev_net(idev
->dev
), aca
->aca_rt
);
377 /* called with rtnl_lock() */
378 static int ipv6_dev_ac_dec(struct net_device
*dev
, const struct in6_addr
*addr
)
380 struct inet6_dev
*idev
= __in6_dev_get(dev
);
384 return __ipv6_dev_ac_dec(idev
, addr
);
387 void ipv6_ac_destroy_dev(struct inet6_dev
*idev
)
389 struct ifacaddr6
*aca
;
391 write_lock_bh(&idev
->lock
);
392 while ((aca
= idev
->ac_list
) != NULL
) {
393 idev
->ac_list
= aca
->aca_next
;
394 write_unlock_bh(&idev
->lock
);
396 ipv6_del_acaddr_hash(aca
);
398 addrconf_leave_solict(idev
, &aca
->aca_addr
);
400 ip6_del_rt(dev_net(idev
->dev
), aca
->aca_rt
);
404 write_lock_bh(&idev
->lock
);
406 write_unlock_bh(&idev
->lock
);
410 * check if the interface has this anycast address
411 * called with rcu_read_lock()
413 static bool ipv6_chk_acast_dev(struct net_device
*dev
, const struct in6_addr
*addr
)
415 struct inet6_dev
*idev
;
416 struct ifacaddr6
*aca
;
418 idev
= __in6_dev_get(dev
);
420 read_lock_bh(&idev
->lock
);
421 for (aca
= idev
->ac_list
; aca
; aca
= aca
->aca_next
)
422 if (ipv6_addr_equal(&aca
->aca_addr
, addr
))
424 read_unlock_bh(&idev
->lock
);
431 * check if given interface (or any, if dev==0) has this anycast address
433 bool ipv6_chk_acast_addr(struct net
*net
, struct net_device
*dev
,
434 const struct in6_addr
*addr
)
436 struct net_device
*nh_dev
;
437 struct ifacaddr6
*aca
;
442 found
= ipv6_chk_acast_dev(dev
, addr
);
444 unsigned int hash
= inet6_acaddr_hash(net
, addr
);
446 hlist_for_each_entry_rcu(aca
, &inet6_acaddr_lst
[hash
],
448 nh_dev
= fib6_info_nh_dev(aca
->aca_rt
);
449 if (!nh_dev
|| !net_eq(dev_net(nh_dev
), net
))
451 if (ipv6_addr_equal(&aca
->aca_addr
, addr
)) {
461 /* check if this anycast address is link-local on given interface or
464 bool ipv6_chk_acast_addr_src(struct net
*net
, struct net_device
*dev
,
465 const struct in6_addr
*addr
)
467 return ipv6_chk_acast_addr(net
,
468 (ipv6_addr_type(addr
) & IPV6_ADDR_LINKLOCAL
?
473 #ifdef CONFIG_PROC_FS
474 struct ac6_iter_state
{
475 struct seq_net_private p
;
476 struct net_device
*dev
;
477 struct inet6_dev
*idev
;
480 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
482 static inline struct ifacaddr6
*ac6_get_first(struct seq_file
*seq
)
484 struct ifacaddr6
*im
= NULL
;
485 struct ac6_iter_state
*state
= ac6_seq_private(seq
);
486 struct net
*net
= seq_file_net(seq
);
489 for_each_netdev_rcu(net
, state
->dev
) {
490 struct inet6_dev
*idev
;
491 idev
= __in6_dev_get(state
->dev
);
494 read_lock_bh(&idev
->lock
);
500 read_unlock_bh(&idev
->lock
);
505 static struct ifacaddr6
*ac6_get_next(struct seq_file
*seq
, struct ifacaddr6
*im
)
507 struct ac6_iter_state
*state
= ac6_seq_private(seq
);
511 if (likely(state
->idev
!= NULL
))
512 read_unlock_bh(&state
->idev
->lock
);
514 state
->dev
= next_net_device_rcu(state
->dev
);
519 state
->idev
= __in6_dev_get(state
->dev
);
522 read_lock_bh(&state
->idev
->lock
);
523 im
= state
->idev
->ac_list
;
528 static struct ifacaddr6
*ac6_get_idx(struct seq_file
*seq
, loff_t pos
)
530 struct ifacaddr6
*im
= ac6_get_first(seq
);
532 while (pos
&& (im
= ac6_get_next(seq
, im
)) != NULL
)
534 return pos
? NULL
: im
;
537 static void *ac6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
541 return ac6_get_idx(seq
, *pos
);
544 static void *ac6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
546 struct ifacaddr6
*im
= ac6_get_next(seq
, v
);
552 static void ac6_seq_stop(struct seq_file
*seq
, void *v
)
555 struct ac6_iter_state
*state
= ac6_seq_private(seq
);
557 if (likely(state
->idev
!= NULL
)) {
558 read_unlock_bh(&state
->idev
->lock
);
564 static int ac6_seq_show(struct seq_file
*seq
, void *v
)
566 struct ifacaddr6
*im
= (struct ifacaddr6
*)v
;
567 struct ac6_iter_state
*state
= ac6_seq_private(seq
);
569 seq_printf(seq
, "%-4d %-15s %pi6 %5d\n",
570 state
->dev
->ifindex
, state
->dev
->name
,
571 &im
->aca_addr
, im
->aca_users
);
575 static const struct seq_operations ac6_seq_ops
= {
576 .start
= ac6_seq_start
,
577 .next
= ac6_seq_next
,
578 .stop
= ac6_seq_stop
,
579 .show
= ac6_seq_show
,
582 int __net_init
ac6_proc_init(struct net
*net
)
584 if (!proc_create_net("anycast6", 0444, net
->proc_net
, &ac6_seq_ops
,
585 sizeof(struct ac6_iter_state
)))
591 void ac6_proc_exit(struct net
*net
)
593 remove_proc_entry("anycast6", net
->proc_net
);
597 /* Init / cleanup code
599 int __init
ipv6_anycast_init(void)
603 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
604 INIT_HLIST_HEAD(&inet6_acaddr_lst
[i
]);
608 void ipv6_anycast_cleanup(void)
612 spin_lock(&acaddr_hash_lock
);
613 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
614 WARN_ON(!hlist_empty(&inet6_acaddr_lst
[i
]));
615 spin_unlock(&acaddr_hash_lock
);