anycast.c 11 KB

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