anycast.c 12 KB

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