anycast.c 13 KB

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