anycast.c 12 KB

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