]>
Commit | Line | Data |
---|---|---|
8b2a464c DH |
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 | ||
12 | #include <linux/slab.h> | |
13 | #include <linux/ctype.h> | |
14 | #include <linux/dns_resolver.h> | |
15 | #include <linux/inet.h> | |
16 | #include <keys/rxrpc-type.h> | |
17 | #include "internal.h" | |
18 | #include "afs_fs.h" | |
19 | ||
8b2a464c DH |
20 | /* |
21 | * Release an address list. | |
22 | */ | |
23 | void afs_put_addrlist(struct afs_addr_list *alist) | |
24 | { | |
25 | if (alist && refcount_dec_and_test(&alist->usage)) | |
26 | call_rcu(&alist->rcu, (rcu_callback_t)kfree); | |
27 | } | |
28 | ||
29 | /* | |
30 | * Allocate an address list. | |
31 | */ | |
32 | struct afs_addr_list *afs_alloc_addrlist(unsigned int nr, | |
33 | unsigned short service, | |
34 | unsigned short port) | |
35 | { | |
36 | struct afs_addr_list *alist; | |
37 | unsigned int i; | |
38 | ||
39 | _enter("%u,%u,%u", nr, service, port); | |
40 | ||
68eb64c3 DH |
41 | if (nr > AFS_MAX_ADDRESSES) |
42 | nr = AFS_MAX_ADDRESSES; | |
43 | ||
acafe7e3 | 44 | alist = kzalloc(struct_size(alist, addrs, nr), GFP_KERNEL); |
8b2a464c DH |
45 | if (!alist) |
46 | return NULL; | |
47 | ||
48 | refcount_set(&alist->usage, 1); | |
68eb64c3 | 49 | alist->max_addrs = nr; |
8b2a464c DH |
50 | |
51 | for (i = 0; i < nr; i++) { | |
52 | struct sockaddr_rxrpc *srx = &alist->addrs[i]; | |
53 | srx->srx_family = AF_RXRPC; | |
54 | srx->srx_service = service; | |
55 | srx->transport_type = SOCK_DGRAM; | |
56 | srx->transport_len = sizeof(srx->transport.sin6); | |
57 | srx->transport.sin6.sin6_family = AF_INET6; | |
58 | srx->transport.sin6.sin6_port = htons(port); | |
59 | } | |
60 | ||
61 | return alist; | |
62 | } | |
63 | ||
64 | /* | |
65 | * Parse a text string consisting of delimited addresses. | |
66 | */ | |
0a5143f2 DH |
67 | struct afs_vlserver_list *afs_parse_text_addrs(struct afs_net *net, |
68 | const char *text, size_t len, | |
69 | char delim, | |
70 | unsigned short service, | |
71 | unsigned short port) | |
8b2a464c | 72 | { |
0a5143f2 | 73 | struct afs_vlserver_list *vllist; |
8b2a464c DH |
74 | struct afs_addr_list *alist; |
75 | const char *p, *end = text + len; | |
0a5143f2 | 76 | const char *problem; |
8b2a464c | 77 | unsigned int nr = 0; |
0a5143f2 | 78 | int ret = -ENOMEM; |
8b2a464c DH |
79 | |
80 | _enter("%*.*s,%c", (int)len, (int)len, text, delim); | |
81 | ||
0a5143f2 DH |
82 | if (!len) { |
83 | _leave(" = -EDESTADDRREQ [empty]"); | |
8b2a464c | 84 | return ERR_PTR(-EDESTADDRREQ); |
0a5143f2 | 85 | } |
8b2a464c DH |
86 | |
87 | if (delim == ':' && (memchr(text, ',', len) || !memchr(text, '.', len))) | |
88 | delim = ','; | |
89 | ||
90 | /* Count the addresses */ | |
91 | p = text; | |
92 | do { | |
0a5143f2 DH |
93 | if (!*p) { |
94 | problem = "nul"; | |
95 | goto inval; | |
96 | } | |
8b2a464c DH |
97 | if (*p == delim) |
98 | continue; | |
99 | nr++; | |
100 | if (*p == '[') { | |
101 | p++; | |
0a5143f2 DH |
102 | if (p == end) { |
103 | problem = "brace1"; | |
104 | goto inval; | |
105 | } | |
8b2a464c | 106 | p = memchr(p, ']', end - p); |
0a5143f2 DH |
107 | if (!p) { |
108 | problem = "brace2"; | |
109 | goto inval; | |
110 | } | |
8b2a464c DH |
111 | p++; |
112 | if (p >= end) | |
113 | break; | |
114 | } | |
115 | ||
116 | p = memchr(p, delim, end - p); | |
117 | if (!p) | |
118 | break; | |
119 | p++; | |
120 | } while (p < end); | |
121 | ||
122 | _debug("%u/%u addresses", nr, AFS_MAX_ADDRESSES); | |
8b2a464c | 123 | |
0a5143f2 DH |
124 | vllist = afs_alloc_vlserver_list(1); |
125 | if (!vllist) | |
8b2a464c DH |
126 | return ERR_PTR(-ENOMEM); |
127 | ||
0a5143f2 DH |
128 | vllist->nr_servers = 1; |
129 | vllist->servers[0].server = afs_alloc_vlserver("<dummy>", 7, AFS_VL_PORT); | |
130 | if (!vllist->servers[0].server) | |
131 | goto error_vl; | |
132 | ||
133 | alist = afs_alloc_addrlist(nr, service, AFS_VL_PORT); | |
134 | if (!alist) | |
135 | goto error; | |
136 | ||
8b2a464c DH |
137 | /* Extract the addresses */ |
138 | p = text; | |
139 | do { | |
01fd79e6 | 140 | const char *q, *stop; |
4c19bbdc DH |
141 | unsigned int xport = port; |
142 | __be32 x[4]; | |
143 | int family; | |
8b2a464c DH |
144 | |
145 | if (*p == delim) { | |
146 | p++; | |
147 | continue; | |
148 | } | |
149 | ||
150 | if (*p == '[') { | |
151 | p++; | |
01fd79e6 DH |
152 | q = memchr(p, ']', end - p); |
153 | } else { | |
154 | for (q = p; q < end; q++) | |
155 | if (*q == '+' || *q == delim) | |
156 | break; | |
8b2a464c DH |
157 | } |
158 | ||
0a5143f2 | 159 | if (in4_pton(p, q - p, (u8 *)&x[0], -1, &stop)) { |
4c19bbdc | 160 | family = AF_INET; |
0a5143f2 | 161 | } else if (in6_pton(p, q - p, (u8 *)x, -1, &stop)) { |
4c19bbdc | 162 | family = AF_INET6; |
0a5143f2 DH |
163 | } else { |
164 | problem = "family"; | |
8b2a464c | 165 | goto bad_address; |
0a5143f2 | 166 | } |
8b2a464c | 167 | |
0a5143f2 DH |
168 | p = q; |
169 | if (stop != p) { | |
170 | problem = "nostop"; | |
01fd79e6 | 171 | goto bad_address; |
0a5143f2 | 172 | } |
01fd79e6 | 173 | |
01fd79e6 | 174 | if (q < end && *q == ']') |
8b2a464c | 175 | p++; |
8b2a464c DH |
176 | |
177 | if (p < end) { | |
178 | if (*p == '+') { | |
179 | /* Port number specification "+1234" */ | |
4c19bbdc | 180 | xport = 0; |
8b2a464c | 181 | p++; |
0a5143f2 DH |
182 | if (p >= end || !isdigit(*p)) { |
183 | problem = "port"; | |
8b2a464c | 184 | goto bad_address; |
0a5143f2 | 185 | } |
8b2a464c DH |
186 | do { |
187 | xport *= 10; | |
188 | xport += *p - '0'; | |
0a5143f2 DH |
189 | if (xport > 65535) { |
190 | problem = "pval"; | |
8b2a464c | 191 | goto bad_address; |
0a5143f2 | 192 | } |
8b2a464c DH |
193 | p++; |
194 | } while (p < end && isdigit(*p)); | |
8b2a464c DH |
195 | } else if (*p == delim) { |
196 | p++; | |
197 | } else { | |
0a5143f2 | 198 | problem = "weird"; |
8b2a464c DH |
199 | goto bad_address; |
200 | } | |
201 | } | |
202 | ||
4c19bbdc DH |
203 | if (family == AF_INET) |
204 | afs_merge_fs_addr4(alist, x[0], xport); | |
205 | else | |
206 | afs_merge_fs_addr6(alist, x, xport); | |
207 | ||
208 | } while (p < end); | |
8b2a464c | 209 | |
0a5143f2 | 210 | rcu_assign_pointer(vllist->servers[0].server->addresses, alist); |
8b2a464c | 211 | _leave(" = [nr %u]", alist->nr_addrs); |
0a5143f2 | 212 | return vllist; |
8b2a464c | 213 | |
0a5143f2 DH |
214 | inval: |
215 | _leave(" = -EINVAL [%s %zu %*.*s]", | |
216 | problem, p - text, (int)len, (int)len, text); | |
8b2a464c | 217 | return ERR_PTR(-EINVAL); |
0a5143f2 DH |
218 | bad_address: |
219 | _leave(" = -EINVAL [%s %zu %*.*s]", | |
220 | problem, p - text, (int)len, (int)len, text); | |
221 | ret = -EINVAL; | |
222 | error: | |
223 | afs_put_addrlist(alist); | |
224 | error_vl: | |
225 | afs_put_vlserverlist(net, vllist); | |
226 | return ERR_PTR(ret); | |
8b2a464c DH |
227 | } |
228 | ||
229 | /* | |
230 | * Compare old and new address lists to see if there's been any change. | |
231 | * - How to do this in better than O(Nlog(N)) time? | |
232 | * - We don't really want to sort the address list, but would rather take the | |
233 | * list as we got it so as not to undo record rotation by the DNS server. | |
234 | */ | |
235 | #if 0 | |
236 | static int afs_cmp_addr_list(const struct afs_addr_list *a1, | |
237 | const struct afs_addr_list *a2) | |
238 | { | |
239 | } | |
240 | #endif | |
241 | ||
242 | /* | |
243 | * Perform a DNS query for VL servers and build a up an address list. | |
244 | */ | |
0a5143f2 | 245 | struct afs_vlserver_list *afs_dns_query(struct afs_cell *cell, time64_t *_expiry) |
8b2a464c | 246 | { |
0a5143f2 DH |
247 | struct afs_vlserver_list *vllist; |
248 | char *result = NULL; | |
8b2a464c DH |
249 | int ret; |
250 | ||
251 | _enter("%s", cell->name); | |
252 | ||
0a5143f2 DH |
253 | ret = dns_query("afsdb", cell->name, cell->name_len, "srv=1", |
254 | &result, _expiry); | |
255 | if (ret < 0) { | |
256 | _leave(" = %d [dns]", ret); | |
8b2a464c | 257 | return ERR_PTR(ret); |
8b2a464c DH |
258 | } |
259 | ||
0a5143f2 DH |
260 | if (*_expiry == 0) |
261 | *_expiry = ktime_get_real_seconds() + 60; | |
262 | ||
263 | if (ret > 1 && result[0] == 0) | |
264 | vllist = afs_extract_vlserver_list(cell, result, ret); | |
265 | else | |
266 | vllist = afs_parse_text_addrs(cell->net, result, ret, ',', | |
267 | VL_SERVICE, AFS_VL_PORT); | |
268 | kfree(result); | |
269 | if (IS_ERR(vllist) && vllist != ERR_PTR(-ENOMEM)) | |
270 | pr_err("Failed to parse DNS data %ld\n", PTR_ERR(vllist)); | |
271 | ||
272 | return vllist; | |
8b2a464c DH |
273 | } |
274 | ||
d2ddc776 DH |
275 | /* |
276 | * Merge an IPv4 entry into a fileserver address list. | |
277 | */ | |
bf99a53c | 278 | void afs_merge_fs_addr4(struct afs_addr_list *alist, __be32 xdr, u16 port) |
d2ddc776 | 279 | { |
46894a13 | 280 | struct sockaddr_rxrpc *srx; |
66be646b | 281 | u32 addr = ntohl(xdr); |
d2ddc776 DH |
282 | int i; |
283 | ||
68eb64c3 DH |
284 | if (alist->nr_addrs >= alist->max_addrs) |
285 | return; | |
286 | ||
d2ddc776 | 287 | for (i = 0; i < alist->nr_ipv4; i++) { |
46894a13 DH |
288 | struct sockaddr_in *a = &alist->addrs[i].transport.sin; |
289 | u32 a_addr = ntohl(a->sin_addr.s_addr); | |
290 | u16 a_port = ntohs(a->sin_port); | |
66be646b DH |
291 | |
292 | if (addr == a_addr && port == a_port) | |
d2ddc776 | 293 | return; |
66be646b | 294 | if (addr == a_addr && port < a_port) |
bf99a53c | 295 | break; |
66be646b | 296 | if (addr < a_addr) |
d2ddc776 DH |
297 | break; |
298 | } | |
299 | ||
300 | if (i < alist->nr_addrs) | |
301 | memmove(alist->addrs + i + 1, | |
302 | alist->addrs + i, | |
303 | sizeof(alist->addrs[0]) * (alist->nr_addrs - i)); | |
304 | ||
46894a13 | 305 | srx = &alist->addrs[i]; |
3bf0fb6f DH |
306 | srx->srx_family = AF_RXRPC; |
307 | srx->transport_type = SOCK_DGRAM; | |
46894a13 DH |
308 | srx->transport_len = sizeof(srx->transport.sin); |
309 | srx->transport.sin.sin_family = AF_INET; | |
310 | srx->transport.sin.sin_port = htons(port); | |
311 | srx->transport.sin.sin_addr.s_addr = xdr; | |
d2ddc776 DH |
312 | alist->nr_ipv4++; |
313 | alist->nr_addrs++; | |
314 | } | |
315 | ||
bf99a53c DH |
316 | /* |
317 | * Merge an IPv6 entry into a fileserver address list. | |
318 | */ | |
319 | void afs_merge_fs_addr6(struct afs_addr_list *alist, __be32 *xdr, u16 port) | |
320 | { | |
46894a13 | 321 | struct sockaddr_rxrpc *srx; |
bf99a53c DH |
322 | int i, diff; |
323 | ||
68eb64c3 DH |
324 | if (alist->nr_addrs >= alist->max_addrs) |
325 | return; | |
326 | ||
bf99a53c | 327 | for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) { |
66be646b DH |
328 | struct sockaddr_in6 *a = &alist->addrs[i].transport.sin6; |
329 | u16 a_port = ntohs(a->sin6_port); | |
330 | ||
bf99a53c | 331 | diff = memcmp(xdr, &a->sin6_addr, 16); |
66be646b | 332 | if (diff == 0 && port == a_port) |
bf99a53c | 333 | return; |
66be646b | 334 | if (diff == 0 && port < a_port) |
bf99a53c DH |
335 | break; |
336 | if (diff < 0) | |
337 | break; | |
338 | } | |
339 | ||
340 | if (i < alist->nr_addrs) | |
341 | memmove(alist->addrs + i + 1, | |
342 | alist->addrs + i, | |
343 | sizeof(alist->addrs[0]) * (alist->nr_addrs - i)); | |
344 | ||
46894a13 | 345 | srx = &alist->addrs[i]; |
3bf0fb6f DH |
346 | srx->srx_family = AF_RXRPC; |
347 | srx->transport_type = SOCK_DGRAM; | |
46894a13 DH |
348 | srx->transport_len = sizeof(srx->transport.sin6); |
349 | srx->transport.sin6.sin6_family = AF_INET6; | |
350 | srx->transport.sin6.sin6_port = htons(port); | |
351 | memcpy(&srx->transport.sin6.sin6_addr, xdr, 16); | |
bf99a53c DH |
352 | alist->nr_addrs++; |
353 | } | |
354 | ||
8b2a464c DH |
355 | /* |
356 | * Get an address to try. | |
357 | */ | |
358 | bool afs_iterate_addresses(struct afs_addr_cursor *ac) | |
359 | { | |
3bf0fb6f DH |
360 | unsigned long set, failed; |
361 | int index; | |
8b2a464c DH |
362 | |
363 | if (!ac->alist) | |
364 | return false; | |
365 | ||
3bf0fb6f DH |
366 | set = ac->alist->responded; |
367 | failed = ac->alist->failed; | |
368 | _enter("%lx-%lx-%lx,%d", set, failed, ac->tried, ac->index); | |
369 | ||
744bcd71 DH |
370 | ac->nr_iterations++; |
371 | ||
3bf0fb6f | 372 | set &= ~(failed | ac->tried); |
8b2a464c | 373 | |
3bf0fb6f DH |
374 | if (!set) |
375 | return false; | |
376 | ||
377 | index = READ_ONCE(ac->alist->preferred); | |
378 | if (test_bit(index, &set)) | |
379 | goto selected; | |
380 | ||
381 | index = __ffs(set); | |
8b2a464c | 382 | |
3bf0fb6f DH |
383 | selected: |
384 | ac->index = index; | |
385 | set_bit(index, &ac->tried); | |
8b2a464c | 386 | ac->responded = false; |
8b2a464c DH |
387 | return true; |
388 | } | |
389 | ||
390 | /* | |
391 | * Release an address list cursor. | |
392 | */ | |
393 | int afs_end_cursor(struct afs_addr_cursor *ac) | |
394 | { | |
fe4d774c DH |
395 | struct afs_addr_list *alist; |
396 | ||
397 | alist = ac->alist; | |
398 | if (alist) { | |
3bf0fb6f DH |
399 | if (ac->responded && |
400 | ac->index != alist->preferred && | |
401 | test_bit(ac->alist->preferred, &ac->tried)) | |
402 | WRITE_ONCE(alist->preferred, ac->index); | |
fe4d774c | 403 | afs_put_addrlist(alist); |
3bf0fb6f | 404 | ac->alist = NULL; |
fe4d774c | 405 | } |
8b2a464c | 406 | |
8b2a464c DH |
407 | return ac->error; |
408 | } |