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