anycast.c 12 KB

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