anycast.c 12 KB

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