anycast.c 12 KB

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