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