anycast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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(&ac->rcu, aca_free_rcu);
  216. }
  217. }
  218. static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
  219. const struct in6_addr *addr)
  220. {
  221. struct ifacaddr6 *aca;
  222. aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
  223. if (!aca)
  224. return NULL;
  225. aca->aca_addr = *addr;
  226. fib6_info_hold(f6i);
  227. aca->aca_rt = f6i;
  228. INIT_HLIST_NODE(&aca->aca_addr_lst);
  229. aca->aca_users = 1;
  230. /* aca_tstamp should be updated upon changes */
  231. aca->aca_cstamp = aca->aca_tstamp = jiffies;
  232. refcount_set(&aca->aca_refcnt, 1);
  233. return aca;
  234. }
  235. /*
  236. * device anycast group inc (add if not found)
  237. */
  238. int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
  239. {
  240. struct ifacaddr6 *aca;
  241. struct fib6_info *f6i;
  242. struct net *net;
  243. int err;
  244. ASSERT_RTNL();
  245. write_lock_bh(&idev->lock);
  246. if (idev->dead) {
  247. err = -ENODEV;
  248. goto out;
  249. }
  250. for (aca = idev->ac_list; aca; aca = 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);
  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. aca->aca_next = idev->ac_list;
  270. idev->ac_list = aca;
  271. /* Hold this for addrconf_join_solict() below before we unlock,
  272. * it is already exposed via idev->ac_list.
  273. */
  274. aca_get(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 = idev->ac_list; aca; aca = aca->aca_next) {
  295. if (ipv6_addr_equal(&aca->aca_addr, addr))
  296. break;
  297. prev_aca = aca;
  298. }
  299. if (!aca) {
  300. write_unlock_bh(&idev->lock);
  301. return -ENOENT;
  302. }
  303. if (--aca->aca_users > 0) {
  304. write_unlock_bh(&idev->lock);
  305. return 0;
  306. }
  307. if (prev_aca)
  308. prev_aca->aca_next = aca->aca_next;
  309. else
  310. idev->ac_list = aca->aca_next;
  311. write_unlock_bh(&idev->lock);
  312. ipv6_del_acaddr_hash(aca);
  313. addrconf_leave_solict(idev, &aca->aca_addr);
  314. ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
  315. aca_put(aca);
  316. return 0;
  317. }
  318. /* called with rtnl_lock() */
  319. static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
  320. {
  321. struct inet6_dev *idev = __in6_dev_get(dev);
  322. if (!idev)
  323. return -ENODEV;
  324. return __ipv6_dev_ac_dec(idev, addr);
  325. }
  326. void ipv6_ac_destroy_dev(struct inet6_dev *idev)
  327. {
  328. struct ifacaddr6 *aca;
  329. write_lock_bh(&idev->lock);
  330. while ((aca = idev->ac_list) != NULL) {
  331. idev->ac_list = aca->aca_next;
  332. write_unlock_bh(&idev->lock);
  333. ipv6_del_acaddr_hash(aca);
  334. addrconf_leave_solict(idev, &aca->aca_addr);
  335. ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
  336. aca_put(aca);
  337. write_lock_bh(&idev->lock);
  338. }
  339. write_unlock_bh(&idev->lock);
  340. }
  341. /*
  342. * check if the interface has this anycast address
  343. * called with rcu_read_lock()
  344. */
  345. static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
  346. {
  347. struct inet6_dev *idev;
  348. struct ifacaddr6 *aca;
  349. idev = __in6_dev_get(dev);
  350. if (idev) {
  351. read_lock_bh(&idev->lock);
  352. for (aca = idev->ac_list; aca; aca = aca->aca_next)
  353. if (ipv6_addr_equal(&aca->aca_addr, addr))
  354. break;
  355. read_unlock_bh(&idev->lock);
  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. struct inet6_dev *idev;
  404. };
  405. #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
  406. static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
  407. {
  408. struct ifacaddr6 *im = NULL;
  409. struct ac6_iter_state *state = ac6_seq_private(seq);
  410. struct net *net = seq_file_net(seq);
  411. state->idev = NULL;
  412. for_each_netdev_rcu(net, state->dev) {
  413. struct inet6_dev *idev;
  414. idev = __in6_dev_get(state->dev);
  415. if (!idev)
  416. continue;
  417. read_lock_bh(&idev->lock);
  418. im = idev->ac_list;
  419. if (im) {
  420. state->idev = idev;
  421. break;
  422. }
  423. read_unlock_bh(&idev->lock);
  424. }
  425. return im;
  426. }
  427. static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
  428. {
  429. struct ac6_iter_state *state = ac6_seq_private(seq);
  430. im = im->aca_next;
  431. while (!im) {
  432. if (likely(state->idev != NULL))
  433. read_unlock_bh(&state->idev->lock);
  434. state->dev = next_net_device_rcu(state->dev);
  435. if (!state->dev) {
  436. state->idev = NULL;
  437. break;
  438. }
  439. state->idev = __in6_dev_get(state->dev);
  440. if (!state->idev)
  441. continue;
  442. read_lock_bh(&state->idev->lock);
  443. im = state->idev->ac_list;
  444. }
  445. return im;
  446. }
  447. static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
  448. {
  449. struct ifacaddr6 *im = ac6_get_first(seq);
  450. if (im)
  451. while (pos && (im = ac6_get_next(seq, im)) != NULL)
  452. --pos;
  453. return pos ? NULL : im;
  454. }
  455. static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
  456. __acquires(RCU)
  457. {
  458. rcu_read_lock();
  459. return ac6_get_idx(seq, *pos);
  460. }
  461. static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  462. {
  463. struct ifacaddr6 *im = ac6_get_next(seq, v);
  464. ++*pos;
  465. return im;
  466. }
  467. static void ac6_seq_stop(struct seq_file *seq, void *v)
  468. __releases(RCU)
  469. {
  470. struct ac6_iter_state *state = ac6_seq_private(seq);
  471. if (likely(state->idev != NULL)) {
  472. read_unlock_bh(&state->idev->lock);
  473. state->idev = NULL;
  474. }
  475. rcu_read_unlock();
  476. }
  477. static int ac6_seq_show(struct seq_file *seq, void *v)
  478. {
  479. struct ifacaddr6 *im = (struct ifacaddr6 *)v;
  480. struct ac6_iter_state *state = ac6_seq_private(seq);
  481. seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
  482. state->dev->ifindex, state->dev->name,
  483. &im->aca_addr, im->aca_users);
  484. return 0;
  485. }
  486. static const struct seq_operations ac6_seq_ops = {
  487. .start = ac6_seq_start,
  488. .next = ac6_seq_next,
  489. .stop = ac6_seq_stop,
  490. .show = ac6_seq_show,
  491. };
  492. int __net_init ac6_proc_init(struct net *net)
  493. {
  494. if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
  495. sizeof(struct ac6_iter_state)))
  496. return -ENOMEM;
  497. return 0;
  498. }
  499. void ac6_proc_exit(struct net *net)
  500. {
  501. remove_proc_entry("anycast6", net->proc_net);
  502. }
  503. #endif
  504. /* Init / cleanup code
  505. */
  506. int __init ipv6_anycast_init(void)
  507. {
  508. int i;
  509. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  510. INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
  511. return 0;
  512. }
  513. void ipv6_anycast_cleanup(void)
  514. {
  515. int i;
  516. spin_lock(&acaddr_hash_lock);
  517. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  518. WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
  519. spin_unlock(&acaddr_hash_lock);
  520. }