anycast.c 14 KB

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