blob: de1ae0bead3baf9192bde373287924c3b0d500fd [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells8b2a4642017-11-02 15:27:50 +00002/* Server address list management
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells8b2a4642017-11-02 15:27:50 +00006 */
7
8#include <linux/slab.h>
9#include <linux/ctype.h>
10#include <linux/dns_resolver.h>
11#include <linux/inet.h>
12#include <keys/rxrpc-type.h>
13#include "internal.h"
14#include "afs_fs.h"
15
David Howells8b2a4642017-11-02 15:27:50 +000016/*
17 * Release an address list.
18 */
19void afs_put_addrlist(struct afs_addr_list *alist)
20{
21 if (alist && refcount_dec_and_test(&alist->usage))
Jann Hornddd2b852020-03-12 21:36:53 +000022 kfree_rcu(alist, rcu);
David Howells8b2a4642017-11-02 15:27:50 +000023}
24
25/*
26 * Allocate an address list.
27 */
28struct afs_addr_list *afs_alloc_addrlist(unsigned int nr,
29 unsigned short service,
30 unsigned short port)
31{
32 struct afs_addr_list *alist;
33 unsigned int i;
34
35 _enter("%u,%u,%u", nr, service, port);
36
David Howells68eb64c2018-10-04 09:32:27 +010037 if (nr > AFS_MAX_ADDRESSES)
38 nr = AFS_MAX_ADDRESSES;
39
Kees Cookacafe7e2018-05-08 13:45:50 -070040 alist = kzalloc(struct_size(alist, addrs, nr), GFP_KERNEL);
David Howells8b2a4642017-11-02 15:27:50 +000041 if (!alist)
42 return NULL;
43
44 refcount_set(&alist->usage, 1);
David Howells68eb64c2018-10-04 09:32:27 +010045 alist->max_addrs = nr;
David Howells8b2a4642017-11-02 15:27:50 +000046
47 for (i = 0; i < nr; i++) {
48 struct sockaddr_rxrpc *srx = &alist->addrs[i];
49 srx->srx_family = AF_RXRPC;
50 srx->srx_service = service;
51 srx->transport_type = SOCK_DGRAM;
52 srx->transport_len = sizeof(srx->transport.sin6);
53 srx->transport.sin6.sin6_family = AF_INET6;
54 srx->transport.sin6.sin6_port = htons(port);
55 }
56
57 return alist;
58}
59
60/*
61 * Parse a text string consisting of delimited addresses.
62 */
David Howells0a5143f2018-10-20 00:57:57 +010063struct afs_vlserver_list *afs_parse_text_addrs(struct afs_net *net,
64 const char *text, size_t len,
65 char delim,
66 unsigned short service,
67 unsigned short port)
David Howells8b2a4642017-11-02 15:27:50 +000068{
David Howells0a5143f2018-10-20 00:57:57 +010069 struct afs_vlserver_list *vllist;
David Howells8b2a4642017-11-02 15:27:50 +000070 struct afs_addr_list *alist;
71 const char *p, *end = text + len;
David Howells0a5143f2018-10-20 00:57:57 +010072 const char *problem;
David Howells8b2a4642017-11-02 15:27:50 +000073 unsigned int nr = 0;
David Howells0a5143f2018-10-20 00:57:57 +010074 int ret = -ENOMEM;
David Howells8b2a4642017-11-02 15:27:50 +000075
76 _enter("%*.*s,%c", (int)len, (int)len, text, delim);
77
David Howells0a5143f2018-10-20 00:57:57 +010078 if (!len) {
79 _leave(" = -EDESTADDRREQ [empty]");
David Howells8b2a4642017-11-02 15:27:50 +000080 return ERR_PTR(-EDESTADDRREQ);
David Howells0a5143f2018-10-20 00:57:57 +010081 }
David Howells8b2a4642017-11-02 15:27:50 +000082
83 if (delim == ':' && (memchr(text, ',', len) || !memchr(text, '.', len)))
84 delim = ',';
85
86 /* Count the addresses */
87 p = text;
88 do {
David Howells0a5143f2018-10-20 00:57:57 +010089 if (!*p) {
90 problem = "nul";
91 goto inval;
92 }
David Howells8b2a4642017-11-02 15:27:50 +000093 if (*p == delim)
94 continue;
95 nr++;
96 if (*p == '[') {
97 p++;
David Howells0a5143f2018-10-20 00:57:57 +010098 if (p == end) {
99 problem = "brace1";
100 goto inval;
101 }
David Howells8b2a4642017-11-02 15:27:50 +0000102 p = memchr(p, ']', end - p);
David Howells0a5143f2018-10-20 00:57:57 +0100103 if (!p) {
104 problem = "brace2";
105 goto inval;
106 }
David Howells8b2a4642017-11-02 15:27:50 +0000107 p++;
108 if (p >= end)
109 break;
110 }
111
112 p = memchr(p, delim, end - p);
113 if (!p)
114 break;
115 p++;
116 } while (p < end);
117
118 _debug("%u/%u addresses", nr, AFS_MAX_ADDRESSES);
David Howells8b2a4642017-11-02 15:27:50 +0000119
David Howells0a5143f2018-10-20 00:57:57 +0100120 vllist = afs_alloc_vlserver_list(1);
121 if (!vllist)
David Howells8b2a4642017-11-02 15:27:50 +0000122 return ERR_PTR(-ENOMEM);
123
David Howells0a5143f2018-10-20 00:57:57 +0100124 vllist->nr_servers = 1;
125 vllist->servers[0].server = afs_alloc_vlserver("<dummy>", 7, AFS_VL_PORT);
126 if (!vllist->servers[0].server)
127 goto error_vl;
128
129 alist = afs_alloc_addrlist(nr, service, AFS_VL_PORT);
130 if (!alist)
131 goto error;
132
David Howells8b2a4642017-11-02 15:27:50 +0000133 /* Extract the addresses */
134 p = text;
135 do {
David Howells01fd79e2018-05-09 22:03:18 +0100136 const char *q, *stop;
David Howells4c19bbd2018-10-04 09:32:27 +0100137 unsigned int xport = port;
138 __be32 x[4];
139 int family;
David Howells8b2a4642017-11-02 15:27:50 +0000140
141 if (*p == delim) {
142 p++;
143 continue;
144 }
145
146 if (*p == '[') {
147 p++;
David Howells01fd79e2018-05-09 22:03:18 +0100148 q = memchr(p, ']', end - p);
149 } else {
150 for (q = p; q < end; q++)
151 if (*q == '+' || *q == delim)
152 break;
David Howells8b2a4642017-11-02 15:27:50 +0000153 }
154
David Howells0a5143f2018-10-20 00:57:57 +0100155 if (in4_pton(p, q - p, (u8 *)&x[0], -1, &stop)) {
David Howells4c19bbd2018-10-04 09:32:27 +0100156 family = AF_INET;
David Howells0a5143f2018-10-20 00:57:57 +0100157 } else if (in6_pton(p, q - p, (u8 *)x, -1, &stop)) {
David Howells4c19bbd2018-10-04 09:32:27 +0100158 family = AF_INET6;
David Howells0a5143f2018-10-20 00:57:57 +0100159 } else {
160 problem = "family";
David Howells8b2a4642017-11-02 15:27:50 +0000161 goto bad_address;
David Howells0a5143f2018-10-20 00:57:57 +0100162 }
David Howells01fd79e2018-05-09 22:03:18 +0100163
164 p = q;
David Howells0a5143f2018-10-20 00:57:57 +0100165 if (stop != p) {
166 problem = "nostop";
167 goto bad_address;
168 }
169
David Howells01fd79e2018-05-09 22:03:18 +0100170 if (q < end && *q == ']')
David Howells8b2a4642017-11-02 15:27:50 +0000171 p++;
David Howells8b2a4642017-11-02 15:27:50 +0000172
173 if (p < end) {
174 if (*p == '+') {
175 /* Port number specification "+1234" */
David Howells4c19bbd2018-10-04 09:32:27 +0100176 xport = 0;
David Howells8b2a4642017-11-02 15:27:50 +0000177 p++;
David Howells0a5143f2018-10-20 00:57:57 +0100178 if (p >= end || !isdigit(*p)) {
179 problem = "port";
David Howells8b2a4642017-11-02 15:27:50 +0000180 goto bad_address;
David Howells0a5143f2018-10-20 00:57:57 +0100181 }
David Howells8b2a4642017-11-02 15:27:50 +0000182 do {
183 xport *= 10;
184 xport += *p - '0';
David Howells0a5143f2018-10-20 00:57:57 +0100185 if (xport > 65535) {
186 problem = "pval";
David Howells8b2a4642017-11-02 15:27:50 +0000187 goto bad_address;
David Howells0a5143f2018-10-20 00:57:57 +0100188 }
David Howells8b2a4642017-11-02 15:27:50 +0000189 p++;
190 } while (p < end && isdigit(*p));
David Howells8b2a4642017-11-02 15:27:50 +0000191 } else if (*p == delim) {
192 p++;
193 } else {
David Howells0a5143f2018-10-20 00:57:57 +0100194 problem = "weird";
David Howells8b2a4642017-11-02 15:27:50 +0000195 goto bad_address;
196 }
197 }
198
David Howells4c19bbd2018-10-04 09:32:27 +0100199 if (family == AF_INET)
200 afs_merge_fs_addr4(alist, x[0], xport);
201 else
202 afs_merge_fs_addr6(alist, x, xport);
203
204 } while (p < end);
David Howells8b2a4642017-11-02 15:27:50 +0000205
David Howells0a5143f2018-10-20 00:57:57 +0100206 rcu_assign_pointer(vllist->servers[0].server->addresses, alist);
David Howells8b2a4642017-11-02 15:27:50 +0000207 _leave(" = [nr %u]", alist->nr_addrs);
David Howells0a5143f2018-10-20 00:57:57 +0100208 return vllist;
David Howells8b2a4642017-11-02 15:27:50 +0000209
David Howells0a5143f2018-10-20 00:57:57 +0100210inval:
211 _leave(" = -EINVAL [%s %zu %*.*s]",
212 problem, p - text, (int)len, (int)len, text);
David Howells8b2a4642017-11-02 15:27:50 +0000213 return ERR_PTR(-EINVAL);
David Howells0a5143f2018-10-20 00:57:57 +0100214bad_address:
215 _leave(" = -EINVAL [%s %zu %*.*s]",
216 problem, p - text, (int)len, (int)len, text);
217 ret = -EINVAL;
218error:
219 afs_put_addrlist(alist);
220error_vl:
221 afs_put_vlserverlist(net, vllist);
222 return ERR_PTR(ret);
David Howells8b2a4642017-11-02 15:27:50 +0000223}
224
225/*
226 * Compare old and new address lists to see if there's been any change.
227 * - How to do this in better than O(Nlog(N)) time?
228 * - We don't really want to sort the address list, but would rather take the
229 * list as we got it so as not to undo record rotation by the DNS server.
230 */
231#if 0
232static int afs_cmp_addr_list(const struct afs_addr_list *a1,
233 const struct afs_addr_list *a2)
234{
235}
236#endif
237
238/*
239 * Perform a DNS query for VL servers and build a up an address list.
240 */
David Howells0a5143f2018-10-20 00:57:57 +0100241struct afs_vlserver_list *afs_dns_query(struct afs_cell *cell, time64_t *_expiry)
David Howells8b2a4642017-11-02 15:27:50 +0000242{
David Howells0a5143f2018-10-20 00:57:57 +0100243 struct afs_vlserver_list *vllist;
244 char *result = NULL;
David Howells8b2a4642017-11-02 15:27:50 +0000245 int ret;
246
247 _enter("%s", cell->name);
248
David Howellsa58946c2019-06-26 21:02:33 +0100249 ret = dns_query(cell->net->net, "afsdb", cell->name, cell->name_len,
250 "srv=1", &result, _expiry, true);
David Howells0a5143f2018-10-20 00:57:57 +0100251 if (ret < 0) {
252 _leave(" = %d [dns]", ret);
David Howells8b2a4642017-11-02 15:27:50 +0000253 return ERR_PTR(ret);
David Howells8b2a4642017-11-02 15:27:50 +0000254 }
255
David Howells0a5143f2018-10-20 00:57:57 +0100256 if (*_expiry == 0)
257 *_expiry = ktime_get_real_seconds() + 60;
258
259 if (ret > 1 && result[0] == 0)
260 vllist = afs_extract_vlserver_list(cell, result, ret);
261 else
262 vllist = afs_parse_text_addrs(cell->net, result, ret, ',',
263 VL_SERVICE, AFS_VL_PORT);
264 kfree(result);
265 if (IS_ERR(vllist) && vllist != ERR_PTR(-ENOMEM))
266 pr_err("Failed to parse DNS data %ld\n", PTR_ERR(vllist));
267
268 return vllist;
David Howells8b2a4642017-11-02 15:27:50 +0000269}
270
271/*
David Howellsd2ddc772017-11-02 15:27:50 +0000272 * Merge an IPv4 entry into a fileserver address list.
273 */
David Howellsbf99a532017-11-02 15:27:51 +0000274void afs_merge_fs_addr4(struct afs_addr_list *alist, __be32 xdr, u16 port)
David Howellsd2ddc772017-11-02 15:27:50 +0000275{
David Howells46894a12018-10-04 09:32:28 +0100276 struct sockaddr_rxrpc *srx;
David Howells66be6462018-10-04 09:32:28 +0100277 u32 addr = ntohl(xdr);
David Howellsd2ddc772017-11-02 15:27:50 +0000278 int i;
279
David Howells68eb64c2018-10-04 09:32:27 +0100280 if (alist->nr_addrs >= alist->max_addrs)
281 return;
282
David Howellsd2ddc772017-11-02 15:27:50 +0000283 for (i = 0; i < alist->nr_ipv4; i++) {
David Howells46894a12018-10-04 09:32:28 +0100284 struct sockaddr_in *a = &alist->addrs[i].transport.sin;
285 u32 a_addr = ntohl(a->sin_addr.s_addr);
286 u16 a_port = ntohs(a->sin_port);
David Howells66be6462018-10-04 09:32:28 +0100287
288 if (addr == a_addr && port == a_port)
David Howellsd2ddc772017-11-02 15:27:50 +0000289 return;
David Howells66be6462018-10-04 09:32:28 +0100290 if (addr == a_addr && port < a_port)
David Howellsbf99a532017-11-02 15:27:51 +0000291 break;
David Howells66be6462018-10-04 09:32:28 +0100292 if (addr < a_addr)
David Howellsd2ddc772017-11-02 15:27:50 +0000293 break;
294 }
295
296 if (i < alist->nr_addrs)
297 memmove(alist->addrs + i + 1,
298 alist->addrs + i,
299 sizeof(alist->addrs[0]) * (alist->nr_addrs - i));
300
David Howells46894a12018-10-04 09:32:28 +0100301 srx = &alist->addrs[i];
David Howells3bf0fb62018-10-20 00:57:59 +0100302 srx->srx_family = AF_RXRPC;
303 srx->transport_type = SOCK_DGRAM;
David Howells46894a12018-10-04 09:32:28 +0100304 srx->transport_len = sizeof(srx->transport.sin);
305 srx->transport.sin.sin_family = AF_INET;
306 srx->transport.sin.sin_port = htons(port);
307 srx->transport.sin.sin_addr.s_addr = xdr;
David Howellsd2ddc772017-11-02 15:27:50 +0000308 alist->nr_ipv4++;
309 alist->nr_addrs++;
310}
311
312/*
David Howellsbf99a532017-11-02 15:27:51 +0000313 * Merge an IPv6 entry into a fileserver address list.
314 */
315void afs_merge_fs_addr6(struct afs_addr_list *alist, __be32 *xdr, u16 port)
316{
David Howells46894a12018-10-04 09:32:28 +0100317 struct sockaddr_rxrpc *srx;
David Howellsbf99a532017-11-02 15:27:51 +0000318 int i, diff;
319
David Howells68eb64c2018-10-04 09:32:27 +0100320 if (alist->nr_addrs >= alist->max_addrs)
321 return;
322
David Howellsbf99a532017-11-02 15:27:51 +0000323 for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) {
David Howells66be6462018-10-04 09:32:28 +0100324 struct sockaddr_in6 *a = &alist->addrs[i].transport.sin6;
325 u16 a_port = ntohs(a->sin6_port);
326
David Howellsbf99a532017-11-02 15:27:51 +0000327 diff = memcmp(xdr, &a->sin6_addr, 16);
David Howells66be6462018-10-04 09:32:28 +0100328 if (diff == 0 && port == a_port)
David Howellsbf99a532017-11-02 15:27:51 +0000329 return;
David Howells66be6462018-10-04 09:32:28 +0100330 if (diff == 0 && port < a_port)
David Howellsbf99a532017-11-02 15:27:51 +0000331 break;
332 if (diff < 0)
333 break;
334 }
335
336 if (i < alist->nr_addrs)
337 memmove(alist->addrs + i + 1,
338 alist->addrs + i,
339 sizeof(alist->addrs[0]) * (alist->nr_addrs - i));
340
David Howells46894a12018-10-04 09:32:28 +0100341 srx = &alist->addrs[i];
David Howells3bf0fb62018-10-20 00:57:59 +0100342 srx->srx_family = AF_RXRPC;
343 srx->transport_type = SOCK_DGRAM;
David Howells46894a12018-10-04 09:32:28 +0100344 srx->transport_len = sizeof(srx->transport.sin6);
345 srx->transport.sin6.sin6_family = AF_INET6;
346 srx->transport.sin6.sin6_port = htons(port);
347 memcpy(&srx->transport.sin6.sin6_addr, xdr, 16);
David Howellsbf99a532017-11-02 15:27:51 +0000348 alist->nr_addrs++;
349}
350
351/*
David Howells8b2a4642017-11-02 15:27:50 +0000352 * Get an address to try.
353 */
354bool afs_iterate_addresses(struct afs_addr_cursor *ac)
355{
David Howells3bf0fb62018-10-20 00:57:59 +0100356 unsigned long set, failed;
357 int index;
David Howells8b2a4642017-11-02 15:27:50 +0000358
359 if (!ac->alist)
360 return false;
361
David Howells3bf0fb62018-10-20 00:57:59 +0100362 set = ac->alist->responded;
363 failed = ac->alist->failed;
364 _enter("%lx-%lx-%lx,%d", set, failed, ac->tried, ac->index);
365
David Howells744bcd72018-10-20 00:57:58 +0100366 ac->nr_iterations++;
367
David Howells3bf0fb62018-10-20 00:57:59 +0100368 set &= ~(failed | ac->tried);
David Howells8b2a4642017-11-02 15:27:50 +0000369
David Howells3bf0fb62018-10-20 00:57:59 +0100370 if (!set)
371 return false;
David Howells8b2a4642017-11-02 15:27:50 +0000372
David Howells3bf0fb62018-10-20 00:57:59 +0100373 index = READ_ONCE(ac->alist->preferred);
374 if (test_bit(index, &set))
375 goto selected;
376
377 index = __ffs(set);
378
379selected:
380 ac->index = index;
381 set_bit(index, &ac->tried);
David Howells8b2a4642017-11-02 15:27:50 +0000382 ac->responded = false;
David Howells8b2a4642017-11-02 15:27:50 +0000383 return true;
384}
385
386/*
387 * Release an address list cursor.
388 */
389int afs_end_cursor(struct afs_addr_cursor *ac)
390{
David Howellsfe4d7742018-02-06 06:26:30 +0000391 struct afs_addr_list *alist;
David Howells8b2a4642017-11-02 15:27:50 +0000392
David Howellsfe4d7742018-02-06 06:26:30 +0000393 alist = ac->alist;
394 if (alist) {
David Howells3bf0fb62018-10-20 00:57:59 +0100395 if (ac->responded &&
396 ac->index != alist->preferred &&
397 test_bit(ac->alist->preferred, &ac->tried))
398 WRITE_ONCE(alist->preferred, ac->index);
David Howellsfe4d7742018-02-06 06:26:30 +0000399 afs_put_addrlist(alist);
David Howells3bf0fb62018-10-20 00:57:59 +0100400 ac->alist = NULL;
David Howellsfe4d7742018-02-06 06:26:30 +0000401 }
402
David Howells8b2a4642017-11-02 15:27:50 +0000403 return ac->error;
404}