anycast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. ASSERT_RTNL();
  56. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  57. return -EPERM;
  58. if (ipv6_addr_is_multicast(addr))
  59. return -EINVAL;
  60. if (ifindex)
  61. dev = __dev_get_by_index(net, ifindex);
  62. if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
  63. return -EINVAL;
  64. pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
  65. if (!pac)
  66. return -ENOMEM;
  67. pac->acl_next = NULL;
  68. pac->acl_addr = *addr;
  69. if (ifindex == 0) {
  70. struct rt6_info *rt;
  71. rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
  72. if (rt) {
  73. dev = rt->dst.dev;
  74. ip6_rt_put(rt);
  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(net, IFF_UP,
  81. IFF_UP | IFF_LOOPBACK);
  82. }
  83. }
  84. if (!dev) {
  85. err = -ENODEV;
  86. goto error;
  87. }
  88. idev = __in6_dev_get(dev);
  89. if (!idev) {
  90. if (ifindex)
  91. err = -ENODEV;
  92. else
  93. err = -EADDRNOTAVAIL;
  94. goto error;
  95. }
  96. /* reset ishost, now that we have a specific device */
  97. ishost = !idev->cnf.forwarding;
  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 error;
  109. }
  110. err = __ipv6_dev_ac_inc(idev, addr);
  111. if (!err) {
  112. pac->acl_next = np->ipv6_ac_list;
  113. np->ipv6_ac_list = pac;
  114. pac = NULL;
  115. }
  116. error:
  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. ASSERT_RTNL();
  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. return -ENOENT;
  140. if (prev_pac)
  141. prev_pac->acl_next = pac->acl_next;
  142. else
  143. np->ipv6_ac_list = pac->acl_next;
  144. dev = __dev_get_by_index(net, pac->acl_ifindex);
  145. if (dev)
  146. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  147. sock_kfree_s(sk, pac, sizeof(*pac));
  148. return 0;
  149. }
  150. void ipv6_sock_ac_close(struct sock *sk)
  151. {
  152. struct ipv6_pinfo *np = inet6_sk(sk);
  153. struct net_device *dev = NULL;
  154. struct ipv6_ac_socklist *pac;
  155. struct net *net = sock_net(sk);
  156. int prev_index;
  157. if (!np->ipv6_ac_list)
  158. return;
  159. rtnl_lock();
  160. pac = np->ipv6_ac_list;
  161. np->ipv6_ac_list = NULL;
  162. prev_index = 0;
  163. while (pac) {
  164. struct ipv6_ac_socklist *next = pac->acl_next;
  165. if (pac->acl_ifindex != prev_index) {
  166. dev = __dev_get_by_index(net, pac->acl_ifindex);
  167. prev_index = pac->acl_ifindex;
  168. }
  169. if (dev)
  170. ipv6_dev_ac_dec(dev, &pac->acl_addr);
  171. sock_kfree_s(sk, pac, sizeof(*pac));
  172. pac = next;
  173. }
  174. rtnl_unlock();
  175. }
  176. static void aca_get(struct ifacaddr6 *aca)
  177. {
  178. refcount_inc(&aca->aca_refcnt);
  179. }
  180. static void aca_put(struct ifacaddr6 *ac)
  181. {
  182. if (refcount_dec_and_test(&ac->aca_refcnt)) {
  183. fib6_info_release(ac->aca_rt);
  184. kfree(ac);
  185. }
  186. }
  187. static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
  188. const struct in6_addr *addr)
  189. {
  190. struct ifacaddr6 *aca;
  191. aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
  192. if (!aca)
  193. return NULL;
  194. aca->aca_addr = *addr;
  195. fib6_info_hold(f6i);
  196. aca->aca_rt = f6i;
  197. aca->aca_users = 1;
  198. /* aca_tstamp should be updated upon changes */
  199. aca->aca_cstamp = aca->aca_tstamp = jiffies;
  200. refcount_set(&aca->aca_refcnt, 1);
  201. return aca;
  202. }
  203. /*
  204. * device anycast group inc (add if not found)
  205. */
  206. int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
  207. {
  208. struct ifacaddr6 *aca;
  209. struct fib6_info *f6i;
  210. struct net *net;
  211. int err;
  212. ASSERT_RTNL();
  213. write_lock_bh(&idev->lock);
  214. if (idev->dead) {
  215. err = -ENODEV;
  216. goto out;
  217. }
  218. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  219. if (ipv6_addr_equal(&aca->aca_addr, addr)) {
  220. aca->aca_users++;
  221. err = 0;
  222. goto out;
  223. }
  224. }
  225. net = dev_net(idev->dev);
  226. f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
  227. if (IS_ERR(f6i)) {
  228. err = PTR_ERR(f6i);
  229. goto out;
  230. }
  231. aca = aca_alloc(f6i, addr);
  232. if (!aca) {
  233. fib6_info_release(f6i);
  234. err = -ENOMEM;
  235. goto out;
  236. }
  237. aca->aca_next = idev->ac_list;
  238. idev->ac_list = aca;
  239. /* Hold this for addrconf_join_solict() below before we unlock,
  240. * it is already exposed via idev->ac_list.
  241. */
  242. aca_get(aca);
  243. write_unlock_bh(&idev->lock);
  244. ip6_ins_rt(net, f6i);
  245. addrconf_join_solict(idev->dev, &aca->aca_addr);
  246. aca_put(aca);
  247. return 0;
  248. out:
  249. write_unlock_bh(&idev->lock);
  250. return err;
  251. }
  252. /*
  253. * device anycast group decrement
  254. */
  255. int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
  256. {
  257. struct ifacaddr6 *aca, *prev_aca;
  258. ASSERT_RTNL();
  259. write_lock_bh(&idev->lock);
  260. prev_aca = NULL;
  261. for (aca = idev->ac_list; aca; aca = aca->aca_next) {
  262. if (ipv6_addr_equal(&aca->aca_addr, addr))
  263. break;
  264. prev_aca = aca;
  265. }
  266. if (!aca) {
  267. write_unlock_bh(&idev->lock);
  268. return -ENOENT;
  269. }
  270. if (--aca->aca_users > 0) {
  271. write_unlock_bh(&idev->lock);
  272. return 0;
  273. }
  274. if (prev_aca)
  275. prev_aca->aca_next = aca->aca_next;
  276. else
  277. idev->ac_list = aca->aca_next;
  278. write_unlock_bh(&idev->lock);
  279. addrconf_leave_solict(idev, &aca->aca_addr);
  280. ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
  281. aca_put(aca);
  282. return 0;
  283. }
  284. /* called with rtnl_lock() */
  285. static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
  286. {
  287. struct inet6_dev *idev = __in6_dev_get(dev);
  288. if (!idev)
  289. return -ENODEV;
  290. return __ipv6_dev_ac_dec(idev, addr);
  291. }
  292. void ipv6_ac_destroy_dev(struct inet6_dev *idev)
  293. {
  294. struct ifacaddr6 *aca;
  295. write_lock_bh(&idev->lock);
  296. while ((aca = idev->ac_list) != NULL) {
  297. idev->ac_list = aca->aca_next;
  298. write_unlock_bh(&idev->lock);
  299. addrconf_leave_solict(idev, &aca->aca_addr);
  300. ip6_del_rt(dev_net(idev->dev), 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 bool 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 false;
  324. }
  325. /*
  326. * check if given interface (or any, if dev==0) has this anycast address
  327. */
  328. bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
  329. const struct in6_addr *addr)
  330. {
  331. bool found = false;
  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 = true;
  339. break;
  340. }
  341. rcu_read_unlock();
  342. return found;
  343. }
  344. /* check if this anycast address is link-local on given interface or
  345. * is global
  346. */
  347. bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
  348. const struct in6_addr *addr)
  349. {
  350. return ipv6_chk_acast_addr(net,
  351. (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
  352. dev : NULL),
  353. addr);
  354. }
  355. #ifdef CONFIG_PROC_FS
  356. struct ac6_iter_state {
  357. struct seq_net_private p;
  358. struct net_device *dev;
  359. struct inet6_dev *idev;
  360. };
  361. #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
  362. static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
  363. {
  364. struct ifacaddr6 *im = NULL;
  365. struct ac6_iter_state *state = ac6_seq_private(seq);
  366. struct net *net = seq_file_net(seq);
  367. state->idev = NULL;
  368. for_each_netdev_rcu(net, state->dev) {
  369. struct inet6_dev *idev;
  370. idev = __in6_dev_get(state->dev);
  371. if (!idev)
  372. continue;
  373. read_lock_bh(&idev->lock);
  374. im = idev->ac_list;
  375. if (im) {
  376. state->idev = idev;
  377. break;
  378. }
  379. read_unlock_bh(&idev->lock);
  380. }
  381. return im;
  382. }
  383. static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
  384. {
  385. struct ac6_iter_state *state = ac6_seq_private(seq);
  386. im = im->aca_next;
  387. while (!im) {
  388. if (likely(state->idev != NULL))
  389. read_unlock_bh(&state->idev->lock);
  390. state->dev = next_net_device_rcu(state->dev);
  391. if (!state->dev) {
  392. state->idev = NULL;
  393. break;
  394. }
  395. state->idev = __in6_dev_get(state->dev);
  396. if (!state->idev)
  397. continue;
  398. read_lock_bh(&state->idev->lock);
  399. im = state->idev->ac_list;
  400. }
  401. return im;
  402. }
  403. static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
  404. {
  405. struct ifacaddr6 *im = ac6_get_first(seq);
  406. if (im)
  407. while (pos && (im = ac6_get_next(seq, im)) != NULL)
  408. --pos;
  409. return pos ? NULL : im;
  410. }
  411. static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
  412. __acquires(RCU)
  413. {
  414. rcu_read_lock();
  415. return ac6_get_idx(seq, *pos);
  416. }
  417. static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  418. {
  419. struct ifacaddr6 *im = ac6_get_next(seq, v);
  420. ++*pos;
  421. return im;
  422. }
  423. static void ac6_seq_stop(struct seq_file *seq, void *v)
  424. __releases(RCU)
  425. {
  426. struct ac6_iter_state *state = ac6_seq_private(seq);
  427. if (likely(state->idev != NULL)) {
  428. read_unlock_bh(&state->idev->lock);
  429. state->idev = NULL;
  430. }
  431. rcu_read_unlock();
  432. }
  433. static int ac6_seq_show(struct seq_file *seq, void *v)
  434. {
  435. struct ifacaddr6 *im = (struct ifacaddr6 *)v;
  436. struct ac6_iter_state *state = ac6_seq_private(seq);
  437. seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
  438. state->dev->ifindex, state->dev->name,
  439. &im->aca_addr, im->aca_users);
  440. return 0;
  441. }
  442. static const struct seq_operations ac6_seq_ops = {
  443. .start = ac6_seq_start,
  444. .next = ac6_seq_next,
  445. .stop = ac6_seq_stop,
  446. .show = ac6_seq_show,
  447. };
  448. int __net_init ac6_proc_init(struct net *net)
  449. {
  450. if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
  451. sizeof(struct ac6_iter_state)))
  452. return -ENOMEM;
  453. return 0;
  454. }
  455. void ac6_proc_exit(struct net *net)
  456. {
  457. remove_proc_entry("anycast6", net->proc_net);
  458. }
  459. #endif