^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Anycast support for IPv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Linux INET6 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * David L Stevens (dlstevens@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * based heavily on net/ipv6/mcast.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <net/snmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/if_inet6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/ndisc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define IN6_ADDR_HSIZE_SHIFT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* anycast address hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static DEFINE_SPINLOCK(acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static u32 inet6_acaddr_hash(struct net *net, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * socket join an anycast group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct ipv6_pinfo *np = inet6_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ipv6_ac_socklist *pac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ishost = !net->ipv6.devconf_all->forwarding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (ipv6_addr_is_multicast(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) dev = __dev_get_by_index(net, ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!pac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pac->acl_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pac->acl_addr = *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (ifindex == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else if (ishost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* router, no matching interface: just pick one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) dev = __dev_get_by_flags(net, IFF_UP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) IFF_UP | IFF_LOOPBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!idev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) err = -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* reset ishost, now that we have a specific device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ishost = !idev->cnf.forwarding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pac->acl_ifindex = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* XXX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * For hosts, allow link-local or matching prefix anycasts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * This obviates the need for propagating anycast routes while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * still allowing some non-router anycast participation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!ipv6_chk_prefix(addr, dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (ishost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) err = -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) err = __ipv6_dev_ac_inc(idev, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pac->acl_next = np->ipv6_ac_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) np->ipv6_ac_list = pac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) pac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (pac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) sock_kfree_s(sk, pac, sizeof(*pac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * socket leave an anycast group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct ipv6_pinfo *np = inet6_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct ipv6_ac_socklist *pac, *prev_pac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) prev_pac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ipv6_addr_equal(&pac->acl_addr, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) prev_pac = pac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!pac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (prev_pac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) prev_pac->acl_next = pac->acl_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) np->ipv6_ac_list = pac->acl_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) dev = __dev_get_by_index(net, pac->acl_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ipv6_dev_ac_dec(dev, &pac->acl_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) sock_kfree_s(sk, pac, sizeof(*pac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) void __ipv6_sock_ac_close(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct ipv6_pinfo *np = inet6_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct ipv6_ac_socklist *pac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int prev_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pac = np->ipv6_ac_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) np->ipv6_ac_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) prev_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) while (pac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct ipv6_ac_socklist *next = pac->acl_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (pac->acl_ifindex != prev_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dev = __dev_get_by_index(net, pac->acl_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) prev_index = pac->acl_ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ipv6_dev_ac_dec(dev, &pac->acl_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) sock_kfree_s(sk, pac, sizeof(*pac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) pac = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) void ipv6_sock_ac_close(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct ipv6_pinfo *np = inet6_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!np->ipv6_ac_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) __ipv6_sock_ac_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) spin_lock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) spin_unlock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) spin_lock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) hlist_del_init_rcu(&aca->aca_addr_lst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) spin_unlock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static void aca_get(struct ifacaddr6 *aca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) refcount_inc(&aca->aca_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void aca_free_rcu(struct rcu_head *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) fib6_info_release(aca->aca_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) kfree(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void aca_put(struct ifacaddr6 *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (refcount_dec_and_test(&ac->aca_refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) call_rcu(&ac->rcu, aca_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct ifacaddr6 *aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (!aca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) aca->aca_addr = *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) fib6_info_hold(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) aca->aca_rt = f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) INIT_HLIST_NODE(&aca->aca_addr_lst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) aca->aca_users = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* aca_tstamp should be updated upon changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) aca->aca_cstamp = aca->aca_tstamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) refcount_set(&aca->aca_refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * device anycast group inc (add if not found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct ifacaddr6 *aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct fib6_info *f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) write_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (idev->dead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) for (aca = idev->ac_list; aca; aca = aca->aca_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (ipv6_addr_equal(&aca->aca_addr, addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) aca->aca_users++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) net = dev_net(idev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (IS_ERR(f6i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err = PTR_ERR(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) aca = aca_alloc(f6i, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!aca) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) fib6_info_release(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) aca->aca_next = idev->ac_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) idev->ac_list = aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* Hold this for addrconf_join_solict() below before we unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * it is already exposed via idev->ac_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) aca_get(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ipv6_add_acaddr_hash(net, aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ip6_ins_rt(net, f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) addrconf_join_solict(idev->dev, &aca->aca_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) aca_put(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * device anycast group decrement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct ifacaddr6 *aca, *prev_aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) write_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) prev_aca = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) for (aca = idev->ac_list; aca; aca = aca->aca_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (ipv6_addr_equal(&aca->aca_addr, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) prev_aca = aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!aca) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (--aca->aca_users > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (prev_aca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) prev_aca->aca_next = aca->aca_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) idev->ac_list = aca->aca_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ipv6_del_acaddr_hash(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) addrconf_leave_solict(idev, &aca->aca_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) aca_put(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* called with rtnl_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct inet6_dev *idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return __ipv6_dev_ac_dec(idev, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) void ipv6_ac_destroy_dev(struct inet6_dev *idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct ifacaddr6 *aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) write_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) while ((aca = idev->ac_list) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) idev->ac_list = aca->aca_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ipv6_del_acaddr_hash(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) addrconf_leave_solict(idev, &aca->aca_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) aca_put(aca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) write_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) write_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * check if the interface has this anycast address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * called with rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct ifacaddr6 *aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (idev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) read_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) for (aca = idev->ac_list; aca; aca = aca->aca_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (ipv6_addr_equal(&aca->aca_addr, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) read_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return aca != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * check if given interface (or any, if dev==0) has this anycast address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct net_device *nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct ifacaddr6 *aca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) found = ipv6_chk_acast_dev(dev, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) unsigned int hash = inet6_acaddr_hash(net, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) aca_addr_lst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) nh_dev = fib6_info_nh_dev(aca->aca_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (!nh_dev || !net_eq(dev_net(nh_dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (ipv6_addr_equal(&aca->aca_addr, addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /* check if this anycast address is link-local on given interface or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * is global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return ipv6_chk_acast_addr(net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) dev : NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct ac6_iter_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct seq_net_private p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct ifacaddr6 *im = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct ac6_iter_state *state = ac6_seq_private(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct net *net = seq_file_net(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) state->idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) for_each_netdev_rcu(net, state->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) idev = __in6_dev_get(state->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) read_lock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) im = idev->ac_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (im) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) state->idev = idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) read_unlock_bh(&idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return im;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct ac6_iter_state *state = ac6_seq_private(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) im = im->aca_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) while (!im) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (likely(state->idev != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) read_unlock_bh(&state->idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) state->dev = next_net_device_rcu(state->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (!state->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) state->idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) state->idev = __in6_dev_get(state->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!state->idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) read_lock_bh(&state->idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) im = state->idev->ac_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return im;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct ifacaddr6 *im = ac6_get_first(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (im)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) while (pos && (im = ac6_get_next(seq, im)) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) --pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return pos ? NULL : im;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) __acquires(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return ac6_get_idx(seq, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct ifacaddr6 *im = ac6_get_next(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return im;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static void ac6_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) __releases(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct ac6_iter_state *state = ac6_seq_private(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (likely(state->idev != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) read_unlock_bh(&state->idev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) state->idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static int ac6_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct ifacaddr6 *im = (struct ifacaddr6 *)v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct ac6_iter_state *state = ac6_seq_private(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) state->dev->ifindex, state->dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) &im->aca_addr, im->aca_users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static const struct seq_operations ac6_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) .start = ac6_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) .next = ac6_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) .stop = ac6_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) .show = ac6_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) int __net_init ac6_proc_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) sizeof(struct ac6_iter_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) void ac6_proc_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) remove_proc_entry("anycast6", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /* Init / cleanup code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int __init ipv6_anycast_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) for (i = 0; i < IN6_ADDR_HSIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) void ipv6_anycast_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) spin_lock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) for (i = 0; i < IN6_ADDR_HSIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) spin_unlock(&acaddr_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }