anycast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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, const 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, const 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. pac->acl_addr = *addr;
  68. rcu_read_lock();
  69. if (ifindex == 0) {
  70. struct rt6_info *rt;
  71. rt = rt6_lookup(net, addr, NULL, 0, 0);
  72. if (rt) {
  73. dev = rt->dst.dev;
  74. dst_release(&rt->dst);
  75. } else if (ishost) {
  76. err = -EADDRNOTAVAIL;
  77. goto error;
  78. } else {
  79. /* router, no matching interface: just pick one */
  80. dev = dev_get_by_flags_rcu(net, IFF_UP,
  81. IFF_UP | IFF_LOOPBACK);
  82. }
  83. } else
  84. dev = dev_get_by_index_rcu(net, ifindex);
  85. if (dev == NULL) {
  86. err = -ENODEV;
  87. goto error;
  88. }
  89. idev = __in6_dev_get(dev);
  90. if (!idev) {
  91. if (ifindex)
  92. err = -ENODEV;
  93. else
  94. err = -EADDRNOTAVAIL;
  95. goto error;
  96. }
  97. /* reset ishost, now that we have a specific device */
  98. ishost = !idev->cnf.forwarding;
  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 error;
  110. }
  111. err = ipv6_dev_ac_inc(dev, addr);
  112. if (!err) {
  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. pac = NULL;
  118. }
  119. error:
  120. rcu_read_unlock();
  121. if (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, const 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. rcu_read_lock();
  152. dev = dev_get_by_index_rcu(net, pac->acl_ifindex);
  153. if (dev)
  154. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  155. rcu_read_unlock();
  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. rcu_read_lock();
  172. while (pac) {
  173. struct ipv6_ac_socklist *next = pac->acl_next;
  174. if (pac->acl_ifindex != prev_index) {
  175. dev = dev_get_by_index_rcu(net, pac->acl_ifindex);
  176. prev_index = pac->acl_ifindex;
  177. }
  178. if (dev)
  179. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  180. sock_kfree_s(sk, pac, sizeof(*pac));
  181. pac = next;
  182. }
  183. rcu_read_unlock();
  184. }
  185. static void aca_put(struct ifacaddr6 *ac)
  186. {
  187. if (atomic_dec_and_test(&ac->aca_refcnt)) {
  188. in6_dev_put(ac->aca_idev);
  189. dst_release(&ac->aca_rt->dst);
  190. kfree(ac);
  191. }
  192. }
  193. /*
  194. * device anycast group inc (add if not found)
  195. */
  196. int ipv6_dev_ac_inc(struct net_device *dev, const struct in6_addr *addr)
  197. {
  198. struct ifacaddr6 *aca;
  199. struct inet6_dev *idev;
  200. struct rt6_info *rt;
  201. int err;
  202. idev = in6_dev_get(dev);
  203. if (idev == NULL)
  204. return -EINVAL;
  205. write_lock_bh(&idev->lock);
  206. if (idev->dead) {
  207. err = -ENODEV;
  208. goto out;
  209. }
  210. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  211. if (ipv6_addr_equal(&aca->aca_addr, addr)) {
  212. aca->aca_users++;
  213. err = 0;
  214. goto out;
  215. }
  216. }
  217. /*
  218. * not found: create a new one.
  219. */
  220. aca = kzalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
  221. if (aca == NULL) {
  222. err = -ENOMEM;
  223. goto out;
  224. }
  225. rt = addrconf_dst_alloc(idev, addr, true);
  226. if (IS_ERR(rt)) {
  227. kfree(aca);
  228. err = PTR_ERR(rt);
  229. goto out;
  230. }
  231. aca->aca_addr = *addr;
  232. aca->aca_idev = idev;
  233. aca->aca_rt = rt;
  234. aca->aca_users = 1;
  235. /* aca_tstamp should be updated upon changes */
  236. aca->aca_cstamp = aca->aca_tstamp = jiffies;
  237. atomic_set(&aca->aca_refcnt, 2);
  238. spin_lock_init(&aca->aca_lock);
  239. aca->aca_next = idev->ac_list;
  240. idev->ac_list = aca;
  241. write_unlock_bh(&idev->lock);
  242. ip6_ins_rt(rt);
  243. addrconf_join_solict(dev, &aca->aca_addr);
  244. aca_put(aca);
  245. return 0;
  246. out:
  247. write_unlock_bh(&idev->lock);
  248. in6_dev_put(idev);
  249. return err;
  250. }
  251. /*
  252. * device anycast group decrement
  253. */
  254. int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
  255. {
  256. struct ifacaddr6 *aca, *prev_aca;
  257. write_lock_bh(&idev->lock);
  258. prev_aca = NULL;
  259. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  260. if (ipv6_addr_equal(&aca->aca_addr, addr))
  261. break;
  262. prev_aca = aca;
  263. }
  264. if (!aca) {
  265. write_unlock_bh(&idev->lock);
  266. return -ENOENT;
  267. }
  268. if (--aca->aca_users > 0) {
  269. write_unlock_bh(&idev->lock);
  270. return 0;
  271. }
  272. if (prev_aca)
  273. prev_aca->aca_next = aca->aca_next;
  274. else
  275. idev->ac_list = aca->aca_next;
  276. write_unlock_bh(&idev->lock);
  277. addrconf_leave_solict(idev, &aca->aca_addr);
  278. dst_hold(&aca->aca_rt->dst);
  279. ip6_del_rt(aca->aca_rt);
  280. aca_put(aca);
  281. return 0;
  282. }
  283. /* called with rcu_read_lock() */
  284. static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
  285. {
  286. struct inet6_dev *idev = __in6_dev_get(dev);
  287. if (idev == NULL)
  288. return -ENODEV;
  289. return __ipv6_dev_ac_dec(idev, addr);
  290. }
  291. void ipv6_ac_destroy_dev(struct inet6_dev *idev)
  292. {
  293. struct ifacaddr6 *aca;
  294. write_lock_bh(&idev->lock);
  295. while ((aca = idev->ac_list) != NULL) {
  296. idev->ac_list = aca->aca_next;
  297. write_unlock_bh(&idev->lock);
  298. addrconf_leave_solict(idev, &aca->aca_addr);
  299. dst_hold(&aca->aca_rt->dst);
  300. ip6_del_rt(aca->aca_rt);
  301. aca_put(aca);
  302. write_lock_bh(&idev->lock);
  303. }
  304. write_unlock_bh(&idev->lock);
  305. }
  306. /*
  307. * check if the interface has this anycast address
  308. * called with rcu_read_lock()
  309. */
  310. static int ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
  311. {
  312. struct inet6_dev *idev;
  313. struct ifacaddr6 *aca;
  314. idev = __in6_dev_get(dev);
  315. if (idev) {
  316. read_lock_bh(&idev->lock);
  317. for (aca = idev->ac_list; aca; aca = aca->aca_next)
  318. if (ipv6_addr_equal(&aca->aca_addr, addr))
  319. break;
  320. read_unlock_bh(&idev->lock);
  321. return aca != NULL;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * check if given interface (or any, if dev==0) has this anycast address
  327. */
  328. int ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
  329. const struct in6_addr *addr)
  330. {
  331. int found = 0;
  332. rcu_read_lock();
  333. if (dev)
  334. found = ipv6_chk_acast_dev(dev, addr);
  335. else
  336. for_each_netdev_rcu(net, dev)
  337. if (ipv6_chk_acast_dev(dev, addr)) {
  338. found = 1;
  339. break;
  340. }
  341. rcu_read_unlock();
  342. return found;
  343. }
  344. #ifdef CONFIG_PROC_FS
  345. struct ac6_iter_state {
  346. struct seq_net_private p;
  347. struct net_device *dev;
  348. struct inet6_dev *idev;
  349. };
  350. #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
  351. static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
  352. {
  353. struct ifacaddr6 *im = NULL;
  354. struct ac6_iter_state *state = ac6_seq_private(seq);
  355. struct net *net = seq_file_net(seq);
  356. state->idev = NULL;
  357. for_each_netdev_rcu(net, state->dev) {
  358. struct inet6_dev *idev;
  359. idev = __in6_dev_get(state->dev);
  360. if (!idev)
  361. continue;
  362. read_lock_bh(&idev->lock);
  363. im = idev->ac_list;
  364. if (im) {
  365. state->idev = idev;
  366. break;
  367. }
  368. read_unlock_bh(&idev->lock);
  369. }
  370. return im;
  371. }
  372. static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
  373. {
  374. struct ac6_iter_state *state = ac6_seq_private(seq);
  375. im = im->aca_next;
  376. while (!im) {
  377. if (likely(state->idev != NULL))
  378. read_unlock_bh(&state->idev->lock);
  379. state->dev = next_net_device_rcu(state->dev);
  380. if (!state->dev) {
  381. state->idev = NULL;
  382. break;
  383. }
  384. state->idev = __in6_dev_get(state->dev);
  385. if (!state->idev)
  386. continue;
  387. read_lock_bh(&state->idev->lock);
  388. im = state->idev->ac_list;
  389. }
  390. return im;
  391. }
  392. static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
  393. {
  394. struct ifacaddr6 *im = ac6_get_first(seq);
  395. if (im)
  396. while (pos && (im = ac6_get_next(seq, im)) != NULL)
  397. --pos;
  398. return pos ? NULL : im;
  399. }
  400. static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
  401. __acquires(RCU)
  402. {
  403. rcu_read_lock();
  404. return ac6_get_idx(seq, *pos);
  405. }
  406. static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  407. {
  408. struct ifacaddr6 *im = ac6_get_next(seq, v);
  409. ++*pos;
  410. return im;
  411. }
  412. static void ac6_seq_stop(struct seq_file *seq, void *v)
  413. __releases(RCU)
  414. {
  415. struct ac6_iter_state *state = ac6_seq_private(seq);
  416. if (likely(state->idev != NULL)) {
  417. read_unlock_bh(&state->idev->lock);
  418. state->idev = NULL;
  419. }
  420. rcu_read_unlock();
  421. }
  422. static int ac6_seq_show(struct seq_file *seq, void *v)
  423. {
  424. struct ifacaddr6 *im = (struct ifacaddr6 *)v;
  425. struct ac6_iter_state *state = ac6_seq_private(seq);
  426. seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
  427. state->dev->ifindex, state->dev->name,
  428. &im->aca_addr, im->aca_users);
  429. return 0;
  430. }
  431. static const struct seq_operations ac6_seq_ops = {
  432. .start = ac6_seq_start,
  433. .next = ac6_seq_next,
  434. .stop = ac6_seq_stop,
  435. .show = ac6_seq_show,
  436. };
  437. static int ac6_seq_open(struct inode *inode, struct file *file)
  438. {
  439. return seq_open_net(inode, file, &ac6_seq_ops,
  440. sizeof(struct ac6_iter_state));
  441. }
  442. static const struct file_operations ac6_seq_fops = {
  443. .owner = THIS_MODULE,
  444. .open = ac6_seq_open,
  445. .read = seq_read,
  446. .llseek = seq_lseek,
  447. .release = seq_release_net,
  448. };
  449. int __net_init ac6_proc_init(struct net *net)
  450. {
  451. if (!proc_net_fops_create(net, "anycast6", S_IRUGO, &ac6_seq_fops))
  452. return -ENOMEM;
  453. return 0;
  454. }
  455. void ac6_proc_exit(struct net *net)
  456. {
  457. proc_net_remove(net, "anycast6");
  458. }
  459. #endif