xref: /openbmc/linux/fs/afs/vl_list.c (revision 12cecbf9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS vlserver list management.
3  *
4  * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include "internal.h"
11 
12 struct afs_vlserver *afs_alloc_vlserver(const char *name, size_t name_len,
13 					unsigned short port)
14 {
15 	struct afs_vlserver *vlserver;
16 
17 	vlserver = kzalloc(struct_size(vlserver, name, name_len + 1),
18 			   GFP_KERNEL);
19 	if (vlserver) {
20 		refcount_set(&vlserver->ref, 1);
21 		rwlock_init(&vlserver->lock);
22 		init_waitqueue_head(&vlserver->probe_wq);
23 		spin_lock_init(&vlserver->probe_lock);
24 		vlserver->rtt = UINT_MAX;
25 		vlserver->name_len = name_len;
26 		vlserver->port = port;
27 		memcpy(vlserver->name, name, name_len);
28 	}
29 	return vlserver;
30 }
31 
32 static void afs_vlserver_rcu(struct rcu_head *rcu)
33 {
34 	struct afs_vlserver *vlserver = container_of(rcu, struct afs_vlserver, rcu);
35 
36 	afs_put_addrlist(rcu_access_pointer(vlserver->addresses));
37 	kfree_rcu(vlserver, rcu);
38 }
39 
40 void afs_put_vlserver(struct afs_net *net, struct afs_vlserver *vlserver)
41 {
42 	if (vlserver &&
43 	    refcount_dec_and_test(&vlserver->ref))
44 		call_rcu(&vlserver->rcu, afs_vlserver_rcu);
45 }
46 
47 struct afs_vlserver_list *afs_alloc_vlserver_list(unsigned int nr_servers)
48 {
49 	struct afs_vlserver_list *vllist;
50 
51 	vllist = kzalloc(struct_size(vllist, servers, nr_servers), GFP_KERNEL);
52 	if (vllist) {
53 		refcount_set(&vllist->ref, 1);
54 		rwlock_init(&vllist->lock);
55 	}
56 
57 	return vllist;
58 }
59 
60 void afs_put_vlserverlist(struct afs_net *net, struct afs_vlserver_list *vllist)
61 {
62 	if (vllist) {
63 		if (refcount_dec_and_test(&vllist->ref)) {
64 			int i;
65 
66 			for (i = 0; i < vllist->nr_servers; i++) {
67 				afs_put_vlserver(net, vllist->servers[i].server);
68 			}
69 			kfree_rcu(vllist, rcu);
70 		}
71 	}
72 }
73 
74 static u16 afs_extract_le16(const u8 **_b)
75 {
76 	u16 val;
77 
78 	val  = (u16)*(*_b)++ << 0;
79 	val |= (u16)*(*_b)++ << 8;
80 	return val;
81 }
82 
83 /*
84  * Build a VL server address list from a DNS queried server list.
85  */
86 static struct afs_addr_list *afs_extract_vl_addrs(const u8 **_b, const u8 *end,
87 						  u8 nr_addrs, u16 port)
88 {
89 	struct afs_addr_list *alist;
90 	const u8 *b = *_b;
91 	int ret = -EINVAL;
92 
93 	alist = afs_alloc_addrlist(nr_addrs, VL_SERVICE, port);
94 	if (!alist)
95 		return ERR_PTR(-ENOMEM);
96 	if (nr_addrs == 0)
97 		return alist;
98 
99 	for (; nr_addrs > 0 && end - b >= nr_addrs; nr_addrs--) {
100 		struct dns_server_list_v1_address hdr;
101 		__be32 x[4];
102 
103 		hdr.address_type = *b++;
104 
105 		switch (hdr.address_type) {
106 		case DNS_ADDRESS_IS_IPV4:
107 			if (end - b < 4) {
108 				_leave(" = -EINVAL [short inet]");
109 				goto error;
110 			}
111 			memcpy(x, b, 4);
112 			afs_merge_fs_addr4(alist, x[0], port);
113 			b += 4;
114 			break;
115 
116 		case DNS_ADDRESS_IS_IPV6:
117 			if (end - b < 16) {
118 				_leave(" = -EINVAL [short inet6]");
119 				goto error;
120 			}
121 			memcpy(x, b, 16);
122 			afs_merge_fs_addr6(alist, x, port);
123 			b += 16;
124 			break;
125 
126 		default:
127 			_leave(" = -EADDRNOTAVAIL [unknown af %u]",
128 			       hdr.address_type);
129 			ret = -EADDRNOTAVAIL;
130 			goto error;
131 		}
132 	}
133 
134 	/* Start with IPv6 if available. */
135 	if (alist->nr_ipv4 < alist->nr_addrs)
136 		alist->preferred = alist->nr_ipv4;
137 
138 	*_b = b;
139 	return alist;
140 
141 error:
142 	*_b = b;
143 	afs_put_addrlist(alist);
144 	return ERR_PTR(ret);
145 }
146 
147 /*
148  * Build a VL server list from a DNS queried server list.
149  */
150 struct afs_vlserver_list *afs_extract_vlserver_list(struct afs_cell *cell,
151 						    const void *buffer,
152 						    size_t buffer_size)
153 {
154 	const struct dns_server_list_v1_header *hdr = buffer;
155 	struct dns_server_list_v1_server bs;
156 	struct afs_vlserver_list *vllist, *previous;
157 	struct afs_addr_list *addrs;
158 	struct afs_vlserver *server;
159 	const u8 *b = buffer, *end = buffer + buffer_size;
160 	int ret = -ENOMEM, nr_servers, i, j;
161 
162 	_enter("");
163 
164 	/* Check that it's a server list, v1 */
165 	if (end - b < sizeof(*hdr) ||
166 	    hdr->hdr.content != DNS_PAYLOAD_IS_SERVER_LIST ||
167 	    hdr->hdr.version != 1) {
168 		pr_notice("kAFS: Got DNS record [%u,%u] len %zu\n",
169 			  hdr->hdr.content, hdr->hdr.version, end - b);
170 		ret = -EDESTADDRREQ;
171 		goto dump;
172 	}
173 
174 	nr_servers = hdr->nr_servers;
175 
176 	vllist = afs_alloc_vlserver_list(nr_servers);
177 	if (!vllist)
178 		return ERR_PTR(-ENOMEM);
179 
180 	vllist->source = (hdr->source < NR__dns_record_source) ?
181 		hdr->source : NR__dns_record_source;
182 	vllist->status = (hdr->status < NR__dns_lookup_status) ?
183 		hdr->status : NR__dns_lookup_status;
184 
185 	read_lock(&cell->vl_servers_lock);
186 	previous = afs_get_vlserverlist(
187 		rcu_dereference_protected(cell->vl_servers,
188 					  lockdep_is_held(&cell->vl_servers_lock)));
189 	read_unlock(&cell->vl_servers_lock);
190 
191 	b += sizeof(*hdr);
192 	while (end - b >= sizeof(bs)) {
193 		bs.name_len	= afs_extract_le16(&b);
194 		bs.priority	= afs_extract_le16(&b);
195 		bs.weight	= afs_extract_le16(&b);
196 		bs.port		= afs_extract_le16(&b);
197 		bs.source	= *b++;
198 		bs.status	= *b++;
199 		bs.protocol	= *b++;
200 		bs.nr_addrs	= *b++;
201 
202 		_debug("extract %u %u %u %u %u %u %*.*s",
203 		       bs.name_len, bs.priority, bs.weight,
204 		       bs.port, bs.protocol, bs.nr_addrs,
205 		       bs.name_len, bs.name_len, b);
206 
207 		if (end - b < bs.name_len)
208 			break;
209 
210 		ret = -EPROTONOSUPPORT;
211 		if (bs.protocol == DNS_SERVER_PROTOCOL_UNSPECIFIED) {
212 			bs.protocol = DNS_SERVER_PROTOCOL_UDP;
213 		} else if (bs.protocol != DNS_SERVER_PROTOCOL_UDP) {
214 			_leave(" = [proto %u]", bs.protocol);
215 			goto error;
216 		}
217 
218 		if (bs.port == 0)
219 			bs.port = AFS_VL_PORT;
220 		if (bs.source > NR__dns_record_source)
221 			bs.source = NR__dns_record_source;
222 		if (bs.status > NR__dns_lookup_status)
223 			bs.status = NR__dns_lookup_status;
224 
225 		/* See if we can update an old server record */
226 		server = NULL;
227 		for (i = 0; i < previous->nr_servers; i++) {
228 			struct afs_vlserver *p = previous->servers[i].server;
229 
230 			if (p->name_len == bs.name_len &&
231 			    p->port == bs.port &&
232 			    strncasecmp(b, p->name, bs.name_len) == 0) {
233 				server = afs_get_vlserver(p);
234 				break;
235 			}
236 		}
237 
238 		if (!server) {
239 			ret = -ENOMEM;
240 			server = afs_alloc_vlserver(b, bs.name_len, bs.port);
241 			if (!server)
242 				goto error;
243 		}
244 
245 		b += bs.name_len;
246 
247 		/* Extract the addresses - note that we can't skip this as we
248 		 * have to advance the payload pointer.
249 		 */
250 		addrs = afs_extract_vl_addrs(&b, end, bs.nr_addrs, bs.port);
251 		if (IS_ERR(addrs)) {
252 			ret = PTR_ERR(addrs);
253 			goto error_2;
254 		}
255 
256 		if (vllist->nr_servers >= nr_servers) {
257 			_debug("skip %u >= %u", vllist->nr_servers, nr_servers);
258 			afs_put_addrlist(addrs);
259 			afs_put_vlserver(cell->net, server);
260 			continue;
261 		}
262 
263 		addrs->source = bs.source;
264 		addrs->status = bs.status;
265 
266 		if (addrs->nr_addrs == 0) {
267 			afs_put_addrlist(addrs);
268 			if (!rcu_access_pointer(server->addresses)) {
269 				afs_put_vlserver(cell->net, server);
270 				continue;
271 			}
272 		} else {
273 			struct afs_addr_list *old = addrs;
274 
275 			write_lock(&server->lock);
276 			old = rcu_replace_pointer(server->addresses, old,
277 						  lockdep_is_held(&server->lock));
278 			write_unlock(&server->lock);
279 			afs_put_addrlist(old);
280 		}
281 
282 
283 		/* TODO: Might want to check for duplicates */
284 
285 		/* Insertion-sort by priority and weight */
286 		for (j = 0; j < vllist->nr_servers; j++) {
287 			if (bs.priority < vllist->servers[j].priority)
288 				break; /* Lower preferable */
289 			if (bs.priority == vllist->servers[j].priority &&
290 			    bs.weight > vllist->servers[j].weight)
291 				break; /* Higher preferable */
292 		}
293 
294 		if (j < vllist->nr_servers) {
295 			memmove(vllist->servers + j + 1,
296 				vllist->servers + j,
297 				(vllist->nr_servers - j) * sizeof(struct afs_vlserver_entry));
298 		}
299 
300 		clear_bit(AFS_VLSERVER_FL_PROBED, &server->flags);
301 
302 		vllist->servers[j].priority = bs.priority;
303 		vllist->servers[j].weight = bs.weight;
304 		vllist->servers[j].server = server;
305 		vllist->nr_servers++;
306 	}
307 
308 	if (b != end) {
309 		_debug("parse error %zd", b - end);
310 		goto error;
311 	}
312 
313 	afs_put_vlserverlist(cell->net, previous);
314 	_leave(" = ok [%u]", vllist->nr_servers);
315 	return vllist;
316 
317 error_2:
318 	afs_put_vlserver(cell->net, server);
319 error:
320 	afs_put_vlserverlist(cell->net, vllist);
321 	afs_put_vlserverlist(cell->net, previous);
322 dump:
323 	if (ret != -ENOMEM) {
324 		printk(KERN_DEBUG "DNS: at %zu\n", (const void *)b - buffer);
325 		print_hex_dump_bytes("DNS: ", DUMP_PREFIX_NONE, buffer, buffer_size);
326 	}
327 	return ERR_PTR(ret);
328 }
329