anycast.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 <net/sock.h>
  32. #include <net/snmp.h>
  33. #include <net/ipv6.h>
  34. #include <net/protocol.h>
  35. #include <net/if_inet6.h>
  36. #include <net/ndisc.h>
  37. #include <net/addrconf.h>
  38. #include <net/ip6_route.h>
  39. #include <net/checksum.h>
  40. static int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr);
  41. /* Big ac list lock for all the sockets */
  42. static DEFINE_RWLOCK(ipv6_sk_ac_lock);
  43. static int
  44. ip6_onlink(struct in6_addr *addr, struct net_device *dev)
  45. {
  46. struct inet6_dev *idev;
  47. struct inet6_ifaddr *ifa;
  48. int onlink;
  49. onlink = 0;
  50. rcu_read_lock();
  51. idev = __in6_dev_get(dev);
  52. if (idev) {
  53. read_lock_bh(&idev->lock);
  54. for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
  55. onlink = ipv6_prefix_equal(addr, &ifa->addr,
  56. ifa->prefix_len);
  57. if (onlink)
  58. break;
  59. }
  60. read_unlock_bh(&idev->lock);
  61. }
  62. rcu_read_unlock();
  63. return onlink;
  64. }
  65. /*
  66. * socket join an anycast group
  67. */
  68. int ipv6_sock_ac_join(struct sock *sk, int ifindex, struct in6_addr *addr)
  69. {
  70. struct ipv6_pinfo *np = inet6_sk(sk);
  71. struct net_device *dev = NULL;
  72. struct inet6_dev *idev;
  73. struct ipv6_ac_socklist *pac;
  74. int ishost = !ipv6_devconf.forwarding;
  75. int err = 0;
  76. if (!capable(CAP_NET_ADMIN))
  77. return -EPERM;
  78. if (ipv6_addr_is_multicast(addr))
  79. return -EINVAL;
  80. if (ipv6_chk_addr(addr, NULL, 0))
  81. return -EINVAL;
  82. pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
  83. if (pac == NULL)
  84. return -ENOMEM;
  85. pac->acl_next = NULL;
  86. ipv6_addr_copy(&pac->acl_addr, addr);
  87. if (ifindex == 0) {
  88. struct rt6_info *rt;
  89. rt = rt6_lookup(addr, NULL, 0, 0);
  90. if (rt) {
  91. dev = rt->rt6i_dev;
  92. dev_hold(dev);
  93. dst_release(&rt->u.dst);
  94. } else if (ishost) {
  95. err = -EADDRNOTAVAIL;
  96. goto out_free_pac;
  97. } else {
  98. /* router, no matching interface: just pick one */
  99. dev = dev_get_by_flags(IFF_UP, IFF_UP|IFF_LOOPBACK);
  100. }
  101. } else
  102. dev = dev_get_by_index(ifindex);
  103. if (dev == NULL) {
  104. err = -ENODEV;
  105. goto out_free_pac;
  106. }
  107. idev = in6_dev_get(dev);
  108. if (!idev) {
  109. if (ifindex)
  110. err = -ENODEV;
  111. else
  112. err = -EADDRNOTAVAIL;
  113. goto out_dev_put;
  114. }
  115. /* reset ishost, now that we have a specific device */
  116. ishost = !idev->cnf.forwarding;
  117. in6_dev_put(idev);
  118. pac->acl_ifindex = dev->ifindex;
  119. /* XXX
  120. * For hosts, allow link-local or matching prefix anycasts.
  121. * This obviates the need for propagating anycast routes while
  122. * still allowing some non-router anycast participation.
  123. */
  124. if (!ip6_onlink(addr, dev)) {
  125. if (ishost)
  126. err = -EADDRNOTAVAIL;
  127. if (err)
  128. goto out_dev_put;
  129. }
  130. err = ipv6_dev_ac_inc(dev, addr);
  131. if (err)
  132. goto out_dev_put;
  133. write_lock_bh(&ipv6_sk_ac_lock);
  134. pac->acl_next = np->ipv6_ac_list;
  135. np->ipv6_ac_list = pac;
  136. write_unlock_bh(&ipv6_sk_ac_lock);
  137. dev_put(dev);
  138. return 0;
  139. out_dev_put:
  140. dev_put(dev);
  141. out_free_pac:
  142. sock_kfree_s(sk, pac, sizeof(*pac));
  143. return err;
  144. }
  145. /*
  146. * socket leave an anycast group
  147. */
  148. int ipv6_sock_ac_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
  149. {
  150. struct ipv6_pinfo *np = inet6_sk(sk);
  151. struct net_device *dev;
  152. struct ipv6_ac_socklist *pac, *prev_pac;
  153. write_lock_bh(&ipv6_sk_ac_lock);
  154. prev_pac = NULL;
  155. for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
  156. if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
  157. ipv6_addr_equal(&pac->acl_addr, addr))
  158. break;
  159. prev_pac = pac;
  160. }
  161. if (!pac) {
  162. write_unlock_bh(&ipv6_sk_ac_lock);
  163. return -ENOENT;
  164. }
  165. if (prev_pac)
  166. prev_pac->acl_next = pac->acl_next;
  167. else
  168. np->ipv6_ac_list = pac->acl_next;
  169. write_unlock_bh(&ipv6_sk_ac_lock);
  170. dev = dev_get_by_index(pac->acl_ifindex);
  171. if (dev) {
  172. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  173. dev_put(dev);
  174. }
  175. sock_kfree_s(sk, pac, sizeof(*pac));
  176. return 0;
  177. }
  178. void ipv6_sock_ac_close(struct sock *sk)
  179. {
  180. struct ipv6_pinfo *np = inet6_sk(sk);
  181. struct net_device *dev = NULL;
  182. struct ipv6_ac_socklist *pac;
  183. int prev_index;
  184. write_lock_bh(&ipv6_sk_ac_lock);
  185. pac = np->ipv6_ac_list;
  186. np->ipv6_ac_list = NULL;
  187. write_unlock_bh(&ipv6_sk_ac_lock);
  188. prev_index = 0;
  189. while (pac) {
  190. struct ipv6_ac_socklist *next = pac->acl_next;
  191. if (pac->acl_ifindex != prev_index) {
  192. if (dev)
  193. dev_put(dev);
  194. dev = dev_get_by_index(pac->acl_ifindex);
  195. prev_index = pac->acl_ifindex;
  196. }
  197. if (dev)
  198. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  199. sock_kfree_s(sk, pac, sizeof(*pac));
  200. pac = next;
  201. }
  202. if (dev)
  203. dev_put(dev);
  204. }
  205. #if 0
  206. /* The function is not used, which is funny. Apparently, author
  207. * supposed to use it to filter out datagrams inside udp/raw but forgot.
  208. *
  209. * It is OK, anycasts are not special comparing to delivery to unicasts.
  210. */
  211. int inet6_ac_check(struct sock *sk, struct in6_addr *addr, int ifindex)
  212. {
  213. struct ipv6_ac_socklist *pac;
  214. struct ipv6_pinfo *np = inet6_sk(sk);
  215. int found;
  216. found = 0;
  217. read_lock(&ipv6_sk_ac_lock);
  218. for (pac=np->ipv6_ac_list; pac; pac=pac->acl_next) {
  219. if (ifindex && pac->acl_ifindex != ifindex)
  220. continue;
  221. found = ipv6_addr_equal(&pac->acl_addr, addr);
  222. if (found)
  223. break;
  224. }
  225. read_unlock(&ipv6_sk_ac_lock);
  226. return found;
  227. }
  228. #endif
  229. static void aca_put(struct ifacaddr6 *ac)
  230. {
  231. if (atomic_dec_and_test(&ac->aca_refcnt)) {
  232. in6_dev_put(ac->aca_idev);
  233. dst_release(&ac->aca_rt->u.dst);
  234. kfree(ac);
  235. }
  236. }
  237. /*
  238. * device anycast group inc (add if not found)
  239. */
  240. int ipv6_dev_ac_inc(struct net_device *dev, struct in6_addr *addr)
  241. {
  242. struct ifacaddr6 *aca;
  243. struct inet6_dev *idev;
  244. struct rt6_info *rt;
  245. int err;
  246. idev = in6_dev_get(dev);
  247. if (idev == NULL)
  248. return -EINVAL;
  249. write_lock_bh(&idev->lock);
  250. if (idev->dead) {
  251. err = -ENODEV;
  252. goto out;
  253. }
  254. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  255. if (ipv6_addr_equal(&aca->aca_addr, addr)) {
  256. aca->aca_users++;
  257. err = 0;
  258. goto out;
  259. }
  260. }
  261. /*
  262. * not found: create a new one.
  263. */
  264. aca = kzalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
  265. if (aca == NULL) {
  266. err = -ENOMEM;
  267. goto out;
  268. }
  269. rt = addrconf_dst_alloc(idev, addr, 1);
  270. if (IS_ERR(rt)) {
  271. kfree(aca);
  272. err = PTR_ERR(rt);
  273. goto out;
  274. }
  275. ipv6_addr_copy(&aca->aca_addr, addr);
  276. aca->aca_idev = idev;
  277. aca->aca_rt = rt;
  278. aca->aca_users = 1;
  279. /* aca_tstamp should be updated upon changes */
  280. aca->aca_cstamp = aca->aca_tstamp = jiffies;
  281. atomic_set(&aca->aca_refcnt, 2);
  282. spin_lock_init(&aca->aca_lock);
  283. aca->aca_next = idev->ac_list;
  284. idev->ac_list = aca;
  285. write_unlock_bh(&idev->lock);
  286. dst_hold(&rt->u.dst);
  287. if (ip6_ins_rt(rt))
  288. dst_release(&rt->u.dst);
  289. addrconf_join_solict(dev, &aca->aca_addr);
  290. aca_put(aca);
  291. return 0;
  292. out:
  293. write_unlock_bh(&idev->lock);
  294. in6_dev_put(idev);
  295. return err;
  296. }
  297. /*
  298. * device anycast group decrement
  299. */
  300. int __ipv6_dev_ac_dec(struct inet6_dev *idev, struct in6_addr *addr)
  301. {
  302. struct ifacaddr6 *aca, *prev_aca;
  303. write_lock_bh(&idev->lock);
  304. prev_aca = NULL;
  305. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  306. if (ipv6_addr_equal(&aca->aca_addr, addr))
  307. break;
  308. prev_aca = aca;
  309. }
  310. if (!aca) {
  311. write_unlock_bh(&idev->lock);
  312. return -ENOENT;
  313. }
  314. if (--aca->aca_users > 0) {
  315. write_unlock_bh(&idev->lock);
  316. return 0;
  317. }
  318. if (prev_aca)
  319. prev_aca->aca_next = aca->aca_next;
  320. else
  321. idev->ac_list = aca->aca_next;
  322. write_unlock_bh(&idev->lock);
  323. addrconf_leave_solict(idev, &aca->aca_addr);
  324. dst_hold(&aca->aca_rt->u.dst);
  325. if (ip6_del_rt(aca->aca_rt))
  326. dst_free(&aca->aca_rt->u.dst);
  327. else
  328. dst_release(&aca->aca_rt->u.dst);
  329. aca_put(aca);
  330. return 0;
  331. }
  332. static int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
  333. {
  334. int ret;
  335. struct inet6_dev *idev = in6_dev_get(dev);
  336. if (idev == NULL)
  337. return -ENODEV;
  338. ret = __ipv6_dev_ac_dec(idev, addr);
  339. in6_dev_put(idev);
  340. return ret;
  341. }
  342. /*
  343. * check if the interface has this anycast address
  344. */
  345. static int ipv6_chk_acast_dev(struct net_device *dev, 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. in6_dev_put(idev);
  357. return aca != 0;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * check if given interface (or any, if dev==0) has this anycast address
  363. */
  364. int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
  365. {
  366. int found = 0;
  367. if (dev)
  368. return ipv6_chk_acast_dev(dev, addr);
  369. read_lock(&dev_base_lock);
  370. for_each_netdev(dev)
  371. if (ipv6_chk_acast_dev(dev, addr)) {
  372. found = 1;
  373. break;
  374. }
  375. read_unlock(&dev_base_lock);
  376. return found;
  377. }
  378. #ifdef CONFIG_PROC_FS
  379. struct ac6_iter_state {
  380. struct net_device *dev;
  381. struct inet6_dev *idev;
  382. };
  383. #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
  384. static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
  385. {
  386. struct ifacaddr6 *im = NULL;
  387. struct ac6_iter_state *state = ac6_seq_private(seq);
  388. state->idev = NULL;
  389. for_each_netdev(state->dev) {
  390. struct inet6_dev *idev;
  391. idev = in6_dev_get(state->dev);
  392. if (!idev)
  393. continue;
  394. read_lock_bh(&idev->lock);
  395. im = idev->ac_list;
  396. if (im) {
  397. state->idev = idev;
  398. break;
  399. }
  400. read_unlock_bh(&idev->lock);
  401. in6_dev_put(idev);
  402. }
  403. return im;
  404. }
  405. static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
  406. {
  407. struct ac6_iter_state *state = ac6_seq_private(seq);
  408. im = im->aca_next;
  409. while (!im) {
  410. if (likely(state->idev != NULL)) {
  411. read_unlock_bh(&state->idev->lock);
  412. in6_dev_put(state->idev);
  413. }
  414. state->dev = next_net_device(state->dev);
  415. if (!state->dev) {
  416. state->idev = NULL;
  417. break;
  418. }
  419. state->idev = in6_dev_get(state->dev);
  420. if (!state->idev)
  421. continue;
  422. read_lock_bh(&state->idev->lock);
  423. im = state->idev->ac_list;
  424. }
  425. return im;
  426. }
  427. static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
  428. {
  429. struct ifacaddr6 *im = ac6_get_first(seq);
  430. if (im)
  431. while (pos && (im = ac6_get_next(seq, im)) != NULL)
  432. --pos;
  433. return pos ? NULL : im;
  434. }
  435. static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
  436. {
  437. read_lock(&dev_base_lock);
  438. return ac6_get_idx(seq, *pos);
  439. }
  440. static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  441. {
  442. struct ifacaddr6 *im;
  443. im = ac6_get_next(seq, v);
  444. ++*pos;
  445. return im;
  446. }
  447. static void ac6_seq_stop(struct seq_file *seq, void *v)
  448. {
  449. struct ac6_iter_state *state = ac6_seq_private(seq);
  450. if (likely(state->idev != NULL)) {
  451. read_unlock_bh(&state->idev->lock);
  452. in6_dev_put(state->idev);
  453. }
  454. read_unlock(&dev_base_lock);
  455. }
  456. static int ac6_seq_show(struct seq_file *seq, void *v)
  457. {
  458. struct ifacaddr6 *im = (struct ifacaddr6 *)v;
  459. struct ac6_iter_state *state = ac6_seq_private(seq);
  460. seq_printf(seq,
  461. "%-4d %-15s " NIP6_SEQFMT " %5d\n",
  462. state->dev->ifindex, state->dev->name,
  463. NIP6(im->aca_addr),
  464. im->aca_users);
  465. return 0;
  466. }
  467. static const struct seq_operations ac6_seq_ops = {
  468. .start = ac6_seq_start,
  469. .next = ac6_seq_next,
  470. .stop = ac6_seq_stop,
  471. .show = ac6_seq_show,
  472. };
  473. static int ac6_seq_open(struct inode *inode, struct file *file)
  474. {
  475. struct seq_file *seq;
  476. int rc = -ENOMEM;
  477. struct ac6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  478. if (!s)
  479. goto out;
  480. rc = seq_open(file, &ac6_seq_ops);
  481. if (rc)
  482. goto out_kfree;
  483. seq = file->private_data;
  484. seq->private = s;
  485. out:
  486. return rc;
  487. out_kfree:
  488. kfree(s);
  489. goto out;
  490. }
  491. static const struct file_operations ac6_seq_fops = {
  492. .owner = THIS_MODULE,
  493. .open = ac6_seq_open,
  494. .read = seq_read,
  495. .llseek = seq_lseek,
  496. .release = seq_release_private,
  497. };
  498. int __init ac6_proc_init(void)
  499. {
  500. if (!proc_net_fops_create("anycast6", S_IRUGO, &ac6_seq_fops))
  501. return -ENOMEM;
  502. return 0;
  503. }
  504. void ac6_proc_exit(void)
  505. {
  506. proc_net_remove("anycast6");
  507. }
  508. #endif