addr_list.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Server address list management
  2. *
  3. * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/ctype.h>
  13. #include <linux/dns_resolver.h>
  14. #include <linux/inet.h>
  15. #include <keys/rxrpc-type.h>
  16. #include "internal.h"
  17. #include "afs_fs.h"
  18. //#define AFS_MAX_ADDRESSES
  19. // ((unsigned int)((PAGE_SIZE - sizeof(struct afs_addr_list)) /
  20. // sizeof(struct sockaddr_rxrpc)))
  21. #define AFS_MAX_ADDRESSES ((unsigned int)(sizeof(unsigned long) * 8))
  22. /*
  23. * Release an address list.
  24. */
  25. void afs_put_addrlist(struct afs_addr_list *alist)
  26. {
  27. if (alist && refcount_dec_and_test(&alist->usage))
  28. call_rcu(&alist->rcu, (rcu_callback_t)kfree);
  29. }
  30. /*
  31. * Allocate an address list.
  32. */
  33. struct afs_addr_list *afs_alloc_addrlist(unsigned int nr,
  34. unsigned short service,
  35. unsigned short port)
  36. {
  37. struct afs_addr_list *alist;
  38. unsigned int i;
  39. _enter("%u,%u,%u", nr, service, port);
  40. alist = kzalloc(struct_size(alist, addrs, nr), GFP_KERNEL);
  41. if (!alist)
  42. return NULL;
  43. refcount_set(&alist->usage, 1);
  44. for (i = 0; i < nr; i++) {
  45. struct sockaddr_rxrpc *srx = &alist->addrs[i];
  46. srx->srx_family = AF_RXRPC;
  47. srx->srx_service = service;
  48. srx->transport_type = SOCK_DGRAM;
  49. srx->transport_len = sizeof(srx->transport.sin6);
  50. srx->transport.sin6.sin6_family = AF_INET6;
  51. srx->transport.sin6.sin6_port = htons(port);
  52. }
  53. return alist;
  54. }
  55. /*
  56. * Parse a text string consisting of delimited addresses.
  57. */
  58. struct afs_addr_list *afs_parse_text_addrs(const char *text, size_t len,
  59. char delim,
  60. unsigned short service,
  61. unsigned short port)
  62. {
  63. struct afs_addr_list *alist;
  64. const char *p, *end = text + len;
  65. unsigned int nr = 0;
  66. _enter("%*.*s,%c", (int)len, (int)len, text, delim);
  67. if (!len)
  68. return ERR_PTR(-EDESTADDRREQ);
  69. if (delim == ':' && (memchr(text, ',', len) || !memchr(text, '.', len)))
  70. delim = ',';
  71. /* Count the addresses */
  72. p = text;
  73. do {
  74. if (!*p)
  75. return ERR_PTR(-EINVAL);
  76. if (*p == delim)
  77. continue;
  78. nr++;
  79. if (*p == '[') {
  80. p++;
  81. if (p == end)
  82. return ERR_PTR(-EINVAL);
  83. p = memchr(p, ']', end - p);
  84. if (!p)
  85. return ERR_PTR(-EINVAL);
  86. p++;
  87. if (p >= end)
  88. break;
  89. }
  90. p = memchr(p, delim, end - p);
  91. if (!p)
  92. break;
  93. p++;
  94. } while (p < end);
  95. _debug("%u/%u addresses", nr, AFS_MAX_ADDRESSES);
  96. if (nr > AFS_MAX_ADDRESSES)
  97. nr = AFS_MAX_ADDRESSES;
  98. alist = afs_alloc_addrlist(nr, service, port);
  99. if (!alist)
  100. return ERR_PTR(-ENOMEM);
  101. /* Extract the addresses */
  102. p = text;
  103. do {
  104. struct sockaddr_rxrpc *srx = &alist->addrs[alist->nr_addrs];
  105. const char *q, *stop;
  106. if (*p == delim) {
  107. p++;
  108. continue;
  109. }
  110. if (*p == '[') {
  111. p++;
  112. q = memchr(p, ']', end - p);
  113. } else {
  114. for (q = p; q < end; q++)
  115. if (*q == '+' || *q == delim)
  116. break;
  117. }
  118. if (in4_pton(p, q - p,
  119. (u8 *)&srx->transport.sin6.sin6_addr.s6_addr32[3],
  120. -1, &stop)) {
  121. srx->transport.sin6.sin6_addr.s6_addr32[0] = 0;
  122. srx->transport.sin6.sin6_addr.s6_addr32[1] = 0;
  123. srx->transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
  124. } else if (in6_pton(p, q - p,
  125. srx->transport.sin6.sin6_addr.s6_addr,
  126. -1, &stop)) {
  127. /* Nothing to do */
  128. } else {
  129. goto bad_address;
  130. }
  131. if (stop != q)
  132. goto bad_address;
  133. p = q;
  134. if (q < end && *q == ']')
  135. p++;
  136. if (p < end) {
  137. if (*p == '+') {
  138. /* Port number specification "+1234" */
  139. unsigned int xport = 0;
  140. p++;
  141. if (p >= end || !isdigit(*p))
  142. goto bad_address;
  143. do {
  144. xport *= 10;
  145. xport += *p - '0';
  146. if (xport > 65535)
  147. goto bad_address;
  148. p++;
  149. } while (p < end && isdigit(*p));
  150. srx->transport.sin6.sin6_port = htons(xport);
  151. } else if (*p == delim) {
  152. p++;
  153. } else {
  154. goto bad_address;
  155. }
  156. }
  157. alist->nr_addrs++;
  158. } while (p < end && alist->nr_addrs < AFS_MAX_ADDRESSES);
  159. _leave(" = [nr %u]", alist->nr_addrs);
  160. return alist;
  161. bad_address:
  162. kfree(alist);
  163. return ERR_PTR(-EINVAL);
  164. }
  165. /*
  166. * Compare old and new address lists to see if there's been any change.
  167. * - How to do this in better than O(Nlog(N)) time?
  168. * - We don't really want to sort the address list, but would rather take the
  169. * list as we got it so as not to undo record rotation by the DNS server.
  170. */
  171. #if 0
  172. static int afs_cmp_addr_list(const struct afs_addr_list *a1,
  173. const struct afs_addr_list *a2)
  174. {
  175. }
  176. #endif
  177. /*
  178. * Perform a DNS query for VL servers and build a up an address list.
  179. */
  180. struct afs_addr_list *afs_dns_query(struct afs_cell *cell, time64_t *_expiry)
  181. {
  182. struct afs_addr_list *alist;
  183. char *vllist = NULL;
  184. int ret;
  185. _enter("%s", cell->name);
  186. ret = dns_query("afsdb", cell->name, cell->name_len,
  187. "", &vllist, _expiry);
  188. if (ret < 0)
  189. return ERR_PTR(ret);
  190. alist = afs_parse_text_addrs(vllist, strlen(vllist), ',',
  191. VL_SERVICE, AFS_VL_PORT);
  192. if (IS_ERR(alist)) {
  193. kfree(vllist);
  194. if (alist != ERR_PTR(-ENOMEM))
  195. pr_err("Failed to parse DNS data\n");
  196. return alist;
  197. }
  198. kfree(vllist);
  199. return alist;
  200. }
  201. /*
  202. * Merge an IPv4 entry into a fileserver address list.
  203. */
  204. void afs_merge_fs_addr4(struct afs_addr_list *alist, __be32 xdr, u16 port)
  205. {
  206. struct sockaddr_in6 *a;
  207. __be16 xport = htons(port);
  208. int i;
  209. for (i = 0; i < alist->nr_ipv4; i++) {
  210. a = &alist->addrs[i].transport.sin6;
  211. if (xdr == a->sin6_addr.s6_addr32[3] &&
  212. xport == a->sin6_port)
  213. return;
  214. if (xdr == a->sin6_addr.s6_addr32[3] &&
  215. (u16 __force)xport < (u16 __force)a->sin6_port)
  216. break;
  217. if ((u32 __force)xdr < (u32 __force)a->sin6_addr.s6_addr32[3])
  218. break;
  219. }
  220. if (i < alist->nr_addrs)
  221. memmove(alist->addrs + i + 1,
  222. alist->addrs + i,
  223. sizeof(alist->addrs[0]) * (alist->nr_addrs - i));
  224. a = &alist->addrs[i].transport.sin6;
  225. a->sin6_port = xport;
  226. a->sin6_addr.s6_addr32[0] = 0;
  227. a->sin6_addr.s6_addr32[1] = 0;
  228. a->sin6_addr.s6_addr32[2] = htonl(0xffff);
  229. a->sin6_addr.s6_addr32[3] = xdr;
  230. alist->nr_ipv4++;
  231. alist->nr_addrs++;
  232. }
  233. /*
  234. * Merge an IPv6 entry into a fileserver address list.
  235. */
  236. void afs_merge_fs_addr6(struct afs_addr_list *alist, __be32 *xdr, u16 port)
  237. {
  238. struct sockaddr_in6 *a;
  239. __be16 xport = htons(port);
  240. int i, diff;
  241. for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) {
  242. a = &alist->addrs[i].transport.sin6;
  243. diff = memcmp(xdr, &a->sin6_addr, 16);
  244. if (diff == 0 &&
  245. xport == a->sin6_port)
  246. return;
  247. if (diff == 0 &&
  248. (u16 __force)xport < (u16 __force)a->sin6_port)
  249. break;
  250. if (diff < 0)
  251. break;
  252. }
  253. if (i < alist->nr_addrs)
  254. memmove(alist->addrs + i + 1,
  255. alist->addrs + i,
  256. sizeof(alist->addrs[0]) * (alist->nr_addrs - i));
  257. a = &alist->addrs[i].transport.sin6;
  258. a->sin6_port = xport;
  259. a->sin6_addr.s6_addr32[0] = xdr[0];
  260. a->sin6_addr.s6_addr32[1] = xdr[1];
  261. a->sin6_addr.s6_addr32[2] = xdr[2];
  262. a->sin6_addr.s6_addr32[3] = xdr[3];
  263. alist->nr_addrs++;
  264. }
  265. /*
  266. * Get an address to try.
  267. */
  268. bool afs_iterate_addresses(struct afs_addr_cursor *ac)
  269. {
  270. _enter("%hu+%hd", ac->start, (short)ac->index);
  271. if (!ac->alist)
  272. return false;
  273. if (ac->begun) {
  274. ac->index++;
  275. if (ac->index == ac->alist->nr_addrs)
  276. ac->index = 0;
  277. if (ac->index == ac->start) {
  278. ac->error = -EDESTADDRREQ;
  279. return false;
  280. }
  281. }
  282. ac->begun = true;
  283. ac->responded = false;
  284. ac->addr = &ac->alist->addrs[ac->index];
  285. return true;
  286. }
  287. /*
  288. * Release an address list cursor.
  289. */
  290. int afs_end_cursor(struct afs_addr_cursor *ac)
  291. {
  292. struct afs_addr_list *alist;
  293. alist = ac->alist;
  294. if (alist) {
  295. if (ac->responded && ac->index != ac->start)
  296. WRITE_ONCE(alist->index, ac->index);
  297. afs_put_addrlist(alist);
  298. }
  299. ac->addr = NULL;
  300. ac->alist = NULL;
  301. ac->begun = false;
  302. return ac->error;
  303. }
  304. /*
  305. * Set the address cursor for iterating over VL servers.
  306. */
  307. int afs_set_vl_cursor(struct afs_addr_cursor *ac, struct afs_cell *cell)
  308. {
  309. struct afs_addr_list *alist;
  310. int ret;
  311. if (!rcu_access_pointer(cell->vl_addrs)) {
  312. ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET,
  313. TASK_INTERRUPTIBLE);
  314. if (ret < 0)
  315. return ret;
  316. if (!rcu_access_pointer(cell->vl_addrs) &&
  317. ktime_get_real_seconds() < cell->dns_expiry)
  318. return cell->error;
  319. }
  320. read_lock(&cell->vl_addrs_lock);
  321. alist = rcu_dereference_protected(cell->vl_addrs,
  322. lockdep_is_held(&cell->vl_addrs_lock));
  323. if (alist->nr_addrs > 0)
  324. afs_get_addrlist(alist);
  325. else
  326. alist = NULL;
  327. read_unlock(&cell->vl_addrs_lock);
  328. if (!alist)
  329. return -EDESTADDRREQ;
  330. ac->alist = alist;
  331. ac->addr = NULL;
  332. ac->start = READ_ONCE(alist->index);
  333. ac->index = ac->start;
  334. ac->error = 0;
  335. ac->begun = false;
  336. return 0;
  337. }