anycast.c 13 KB

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