anycast.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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(&init_net, 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. ip6_ins_rt(rt);
  288. addrconf_join_solict(dev, &aca->aca_addr);
  289. aca_put(aca);
  290. return 0;
  291. out:
  292. write_unlock_bh(&idev->lock);
  293. in6_dev_put(idev);
  294. return err;
  295. }
  296. /*
  297. * device anycast group decrement
  298. */
  299. int __ipv6_dev_ac_dec(struct inet6_dev *idev, struct in6_addr *addr)
  300. {
  301. struct ifacaddr6 *aca, *prev_aca;
  302. write_lock_bh(&idev->lock);
  303. prev_aca = NULL;
  304. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  305. if (ipv6_addr_equal(&aca->aca_addr, addr))
  306. break;
  307. prev_aca = aca;
  308. }
  309. if (!aca) {
  310. write_unlock_bh(&idev->lock);
  311. return -ENOENT;
  312. }
  313. if (--aca->aca_users > 0) {
  314. write_unlock_bh(&idev->lock);
  315. return 0;
  316. }
  317. if (prev_aca)
  318. prev_aca->aca_next = aca->aca_next;
  319. else
  320. idev->ac_list = aca->aca_next;
  321. write_unlock_bh(&idev->lock);
  322. addrconf_leave_solict(idev, &aca->aca_addr);
  323. dst_hold(&aca->aca_rt->u.dst);
  324. ip6_del_rt(aca->aca_rt);
  325. aca_put(aca);
  326. return 0;
  327. }
  328. static int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
  329. {
  330. int ret;
  331. struct inet6_dev *idev = in6_dev_get(dev);
  332. if (idev == NULL)
  333. return -ENODEV;
  334. ret = __ipv6_dev_ac_dec(idev, addr);
  335. in6_dev_put(idev);
  336. return ret;
  337. }
  338. /*
  339. * check if the interface has this anycast address
  340. */
  341. static int ipv6_chk_acast_dev(struct net_device *dev, struct in6_addr *addr)
  342. {
  343. struct inet6_dev *idev;
  344. struct ifacaddr6 *aca;
  345. idev = in6_dev_get(dev);
  346. if (idev) {
  347. read_lock_bh(&idev->lock);
  348. for (aca = idev->ac_list; aca; aca = aca->aca_next)
  349. if (ipv6_addr_equal(&aca->aca_addr, addr))
  350. break;
  351. read_unlock_bh(&idev->lock);
  352. in6_dev_put(idev);
  353. return aca != NULL;
  354. }
  355. return 0;
  356. }
  357. /*
  358. * check if given interface (or any, if dev==0) has this anycast address
  359. */
  360. int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
  361. {
  362. int found = 0;
  363. if (dev)
  364. return ipv6_chk_acast_dev(dev, addr);
  365. read_lock(&dev_base_lock);
  366. for_each_netdev(&init_net, dev)
  367. if (ipv6_chk_acast_dev(dev, addr)) {
  368. found = 1;
  369. break;
  370. }
  371. read_unlock(&dev_base_lock);
  372. return found;
  373. }
  374. #ifdef CONFIG_PROC_FS
  375. struct ac6_iter_state {
  376. struct net_device *dev;
  377. struct inet6_dev *idev;
  378. };
  379. #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
  380. static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
  381. {
  382. struct ifacaddr6 *im = NULL;
  383. struct ac6_iter_state *state = ac6_seq_private(seq);
  384. state->idev = NULL;
  385. for_each_netdev(&init_net, state->dev) {
  386. struct inet6_dev *idev;
  387. idev = in6_dev_get(state->dev);
  388. if (!idev)
  389. continue;
  390. read_lock_bh(&idev->lock);
  391. im = idev->ac_list;
  392. if (im) {
  393. state->idev = idev;
  394. break;
  395. }
  396. read_unlock_bh(&idev->lock);
  397. in6_dev_put(idev);
  398. }
  399. return im;
  400. }
  401. static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
  402. {
  403. struct ac6_iter_state *state = ac6_seq_private(seq);
  404. im = im->aca_next;
  405. while (!im) {
  406. if (likely(state->idev != NULL)) {
  407. read_unlock_bh(&state->idev->lock);
  408. in6_dev_put(state->idev);
  409. }
  410. state->dev = next_net_device(state->dev);
  411. if (!state->dev) {
  412. state->idev = NULL;
  413. break;
  414. }
  415. state->idev = in6_dev_get(state->dev);
  416. if (!state->idev)
  417. continue;
  418. read_lock_bh(&state->idev->lock);
  419. im = state->idev->ac_list;
  420. }
  421. return im;
  422. }
  423. static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
  424. {
  425. struct ifacaddr6 *im = ac6_get_first(seq);
  426. if (im)
  427. while (pos && (im = ac6_get_next(seq, im)) != NULL)
  428. --pos;
  429. return pos ? NULL : im;
  430. }
  431. static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
  432. __acquires(dev_base_lock)
  433. {
  434. read_lock(&dev_base_lock);
  435. return ac6_get_idx(seq, *pos);
  436. }
  437. static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  438. {
  439. struct ifacaddr6 *im;
  440. im = ac6_get_next(seq, v);
  441. ++*pos;
  442. return im;
  443. }
  444. static void ac6_seq_stop(struct seq_file *seq, void *v)
  445. __releases(dev_base_lock)
  446. {
  447. struct ac6_iter_state *state = ac6_seq_private(seq);
  448. if (likely(state->idev != NULL)) {
  449. read_unlock_bh(&state->idev->lock);
  450. in6_dev_put(state->idev);
  451. }
  452. read_unlock(&dev_base_lock);
  453. }
  454. static int ac6_seq_show(struct seq_file *seq, void *v)
  455. {
  456. struct ifacaddr6 *im = (struct ifacaddr6 *)v;
  457. struct ac6_iter_state *state = ac6_seq_private(seq);
  458. seq_printf(seq,
  459. "%-4d %-15s " NIP6_SEQFMT " %5d\n",
  460. state->dev->ifindex, state->dev->name,
  461. NIP6(im->aca_addr),
  462. im->aca_users);
  463. return 0;
  464. }
  465. static const struct seq_operations ac6_seq_ops = {
  466. .start = ac6_seq_start,
  467. .next = ac6_seq_next,
  468. .stop = ac6_seq_stop,
  469. .show = ac6_seq_show,
  470. };
  471. static int ac6_seq_open(struct inode *inode, struct file *file)
  472. {
  473. return seq_open_private(file, &ac6_seq_ops,
  474. sizeof(struct ac6_iter_state));
  475. }
  476. static const struct file_operations ac6_seq_fops = {
  477. .owner = THIS_MODULE,
  478. .open = ac6_seq_open,
  479. .read = seq_read,
  480. .llseek = seq_lseek,
  481. .release = seq_release_private,
  482. };
  483. int __init ac6_proc_init(void)
  484. {
  485. if (!proc_net_fops_create(&init_net, "anycast6", S_IRUGO, &ac6_seq_fops))
  486. return -ENOMEM;
  487. return 0;
  488. }
  489. void ac6_proc_exit(void)
  490. {
  491. proc_net_remove(&init_net, "anycast6");
  492. }
  493. #endif