1 /*
2  * xenstore_dev.c
3  *
4  * Driver giving user-space access to the kernel's connection to the
5  * XenStore service.
6  *
7  * Copyright (c) 2005, Christian Limpach
8  * Copyright (c) 2005, Rusty Russell, IBM Corporation
9  *
10  * This file may be distributed separately from the Linux kernel, or
11  * incorporated into other software packages, subject to the following license:
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this source file (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use, copy, modify,
16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  */
31 
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
38 #include <sys/errno.h>
39 #include <sys/uio.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/conf.h>
46 #include <sys/module.h>
47 #include <sys/selinfo.h>
48 #include <sys/sysctl.h>
49 #include <sys/poll.h>
50 
51 #include <xen/xen-os.h>
52 
53 #include <xen/hypervisor.h>
54 #include <xen/xenstore/xenstorevar.h>
55 #include <xen/xenstore/xenstore_internal.h>
56 
57 static unsigned int max_pending_watches = 1000;
58 
59 struct xs_dev_transaction {
60 	LIST_ENTRY(xs_dev_transaction) list;
61 	struct xs_transaction handle;
62 };
63 
64 struct xs_dev_watch {
65 	LIST_ENTRY(xs_dev_watch) list;
66 	struct xs_watch watch;
67 	char *token;
68 	struct xs_dev_data *user;
69 };
70 
71 struct xs_dev_data {
72 	/* In-progress transaction. */
73 	LIST_HEAD(, xs_dev_transaction) transactions;
74 
75 	/* Active watches. */
76 	LIST_HEAD(, xs_dev_watch) watches;
77 
78 	/* Partial request. */
79 	unsigned int len;
80 	union {
81 		struct xsd_sockmsg msg;
82 		char buffer[PAGE_SIZE];
83 	} u;
84 
85 	/* Response queue. */
86 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
87 	char read_buffer[PAGE_SIZE];
88 	unsigned int read_cons, read_prod;
89 
90 	/* Serializes writes to the read buffer. */
91 	struct mtx lock;
92 
93 	/* Polling structure (for reads only ATM). */
94 	struct selinfo ev_rsel;
95 };
96 
97 static void
xs_queue_reply(struct xs_dev_data * u,const char * data,unsigned int len)98 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
99 {
100 	unsigned int i;
101 
102 	for (i = 0; i < len; i++, u->read_prod++)
103 		u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
104 
105 	KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
106 	    ("xenstore reply too big"));
107 
108 	wakeup(u);
109 	selwakeup(&u->ev_rsel);
110 }
111 
112 static const char *
xs_dev_error_to_string(int error)113 xs_dev_error_to_string(int error)
114 {
115 	unsigned int i;
116 
117 	for (i = 0; i < nitems(xsd_errors); i++)
118 		if (xsd_errors[i].errnum == error)
119 			return (xsd_errors[i].errstring);
120 
121 	return (NULL);
122 }
123 
124 static void
xs_dev_return_error(struct xs_dev_data * u,int error,int req_id,int tx_id)125 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
126 {
127 	struct xsd_sockmsg msg;
128 	const char *payload;
129 
130 	msg.type = XS_ERROR;
131 	msg.req_id = req_id;
132 	msg.tx_id = tx_id;
133 	payload = NULL;
134 
135 
136 	payload = xs_dev_error_to_string(error);
137 	if (payload == NULL)
138 		payload = xs_dev_error_to_string(EINVAL);
139 	KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
140 
141 	msg.len = strlen(payload) + 1;
142 
143 	mtx_lock(&u->lock);
144 	xs_queue_reply(u, (char *)&msg, sizeof(msg));
145 	xs_queue_reply(u, payload, msg.len);
146 	mtx_unlock(&u->lock);
147 }
148 
149 static int
xs_dev_watch_message_parse_string(const char ** p,const char * end,const char ** string_r)150 xs_dev_watch_message_parse_string(const char **p, const char *end,
151     const char **string_r)
152 {
153 	const char *nul;
154 
155 	nul = memchr(*p, 0, end - *p);
156 	if (!nul)
157 		return (EINVAL);
158 
159 	*string_r = *p;
160 	*p = nul+1;
161 
162 	return (0);
163 }
164 
165 static int
xs_dev_watch_message_parse(const struct xsd_sockmsg * msg,const char ** path_r,const char ** token_r)166 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
167     const char **token_r)
168 {
169 	const char *p, *end;
170 	int error;
171 
172 	p = (const char *)msg + sizeof(*msg);
173 	end = p + msg->len;
174 	KASSERT(p <= end, ("payload overflow"));
175 
176 	error = xs_dev_watch_message_parse_string(&p, end, path_r);
177 	if (error)
178 		return (error);
179 	error = xs_dev_watch_message_parse_string(&p, end, token_r);
180 	if (error)
181 		return (error);
182 
183 	return (0);
184 }
185 
186 static struct xs_dev_watch *
xs_dev_find_watch(struct xs_dev_data * u,const char * token)187 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
188 {
189 	struct xs_dev_watch *watch;
190 
191 	LIST_FOREACH(watch, &u->watches, list)
192 		if (strcmp(watch->token, token) == 0)
193 			return (watch);
194 
195 	return (NULL);
196 }
197 
198 static void
xs_dev_watch_cb(struct xs_watch * watch,const char ** vec,unsigned int len)199 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
200 {
201 	struct xs_dev_watch *dwatch;
202 	struct xsd_sockmsg msg;
203 	char *payload;
204 
205 	dwatch = (struct xs_dev_watch *)watch->callback_data;
206 	msg.type = XS_WATCH_EVENT;
207 	msg.req_id = msg.tx_id = 0;
208 	msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
209 
210 	payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
211 	strcpy(payload, vec[XS_WATCH_PATH]);
212 	strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
213 	mtx_lock(&dwatch->user->lock);
214 	xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
215 	xs_queue_reply(dwatch->user, payload, msg.len);
216 	mtx_unlock(&dwatch->user->lock);
217 	free(payload, M_XENSTORE);
218 }
219 
220 static struct xs_dev_transaction *
xs_dev_find_transaction(struct xs_dev_data * u,uint32_t tx_id)221 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
222 {
223 	struct xs_dev_transaction *trans;
224 
225 	LIST_FOREACH(trans, &u->transactions, list)
226 		if (trans->handle.id == tx_id)
227 			return (trans);
228 
229 	return (NULL);
230 }
231 
232 static int
xs_dev_read(struct cdev * dev,struct uio * uio,int ioflag)233 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
234 {
235 	int error;
236 	struct xs_dev_data *u;
237 
238 	error = devfs_get_cdevpriv((void **)&u);
239 	if (error != 0)
240 		return (error);
241 
242 	while (u->read_prod == u->read_cons) {
243 		error = tsleep(u, PCATCH, "xsdread", hz/10);
244 		if (error && error != EWOULDBLOCK)
245 			return (error);
246 	}
247 
248 	while (uio->uio_resid > 0) {
249 		if (u->read_cons == u->read_prod)
250 			break;
251 		error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
252 		    1, uio);
253 		if (error)
254 			return (error);
255 		u->read_cons++;
256 	}
257 	return (0);
258 }
259 
260 static int
xs_dev_write(struct cdev * dev,struct uio * uio,int ioflag)261 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
262 {
263 	int error;
264 	const char *wpath, *wtoken;
265 	struct xs_dev_data *u;
266 	struct xs_dev_transaction *trans;
267 	struct xs_dev_watch *watch;
268 	void *reply;
269 	static const char *ok = "OK";
270 	int len = uio->uio_resid;
271 
272 	error = devfs_get_cdevpriv((void **)&u);
273 	if (error != 0)
274 		return (error);
275 
276 	if ((len + u->len) > sizeof(u->u.buffer))
277 		return (EINVAL);
278 
279 	error = uiomove(u->u.buffer + u->len, len, uio);
280 	if (error)
281 		return (error);
282 
283 	u->len += len;
284 	if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
285 		return (0);
286 
287 	switch (u->u.msg.type) {
288 	case XS_TRANSACTION_START:
289 	case XS_TRANSACTION_END:
290 	case XS_DIRECTORY:
291 	case XS_READ:
292 	case XS_GET_PERMS:
293 	case XS_RELEASE:
294 	case XS_GET_DOMAIN_PATH:
295 	case XS_WRITE:
296 	case XS_MKDIR:
297 	case XS_RM:
298 	case XS_SET_PERMS:
299 		/* Check that this transaction id is not hijacked. */
300 		if (u->u.msg.tx_id != 0 &&
301 		    xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
302 			error = EINVAL;
303 			break;
304 		}
305 		error = xs_dev_request_and_reply(&u->u.msg, &reply);
306 		if (!error) {
307 			if (u->u.msg.type == XS_TRANSACTION_START) {
308 				trans = malloc(sizeof(*trans), M_XENSTORE,
309 				    M_WAITOK);
310 				trans->handle.id = strtoul(reply, NULL, 0);
311 				LIST_INSERT_HEAD(&u->transactions, trans, list);
312 			} else if (u->u.msg.type == XS_TRANSACTION_END) {
313 				trans = xs_dev_find_transaction(u,
314 				    u->u.msg.tx_id);
315 				KASSERT(trans != NULL,
316 				    ("Unable to find transaction"));
317 				LIST_REMOVE(trans, list);
318 				free(trans, M_XENSTORE);
319 			}
320 			mtx_lock(&u->lock);
321 			xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
322 			xs_queue_reply(u, (char *)reply, u->u.msg.len);
323 			mtx_unlock(&u->lock);
324 			free(reply, M_XENSTORE);
325 		}
326 		break;
327 	case XS_WATCH:
328 		u->u.msg.tx_id = 0;
329 		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
330 		if (error)
331 			break;
332 		if (xs_dev_find_watch(u, wtoken) != NULL) {
333 			error = EINVAL;
334 			break;
335 		}
336 
337 		watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
338 		watch->watch.node = strdup(wpath, M_XENSTORE);
339 		watch->watch.callback = xs_dev_watch_cb;
340 		watch->watch.callback_data = (uintptr_t)watch;
341 		watch->watch.max_pending = max_pending_watches;
342 		watch->token = strdup(wtoken, M_XENSTORE);
343 		watch->user = u;
344 
345 		error = xs_register_watch(&watch->watch);
346 		if (error != 0) {
347 			free(watch->token, M_XENSTORE);
348 			free(watch->watch.node, M_XENSTORE);
349 			free(watch, M_XENSTORE);
350 			break;
351 		}
352 
353 		LIST_INSERT_HEAD(&u->watches, watch, list);
354 		u->u.msg.len = sizeof(ok);
355 		mtx_lock(&u->lock);
356 		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
357 		xs_queue_reply(u, ok, sizeof(ok));
358 		mtx_unlock(&u->lock);
359 		break;
360 	case XS_UNWATCH:
361 		u->u.msg.tx_id = 0;
362 		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
363 		if (error)
364 			break;
365 		watch = xs_dev_find_watch(u, wtoken);
366 		if (watch == NULL) {
367 			error = EINVAL;
368 			break;
369 		}
370 
371 		LIST_REMOVE(watch, list);
372 		xs_unregister_watch(&watch->watch);
373 		free(watch->watch.node, M_XENSTORE);
374 		free(watch->token, M_XENSTORE);
375 		free(watch, M_XENSTORE);
376 		u->u.msg.len = sizeof(ok);
377 		mtx_lock(&u->lock);
378 		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
379 		xs_queue_reply(u, ok, sizeof(ok));
380 		mtx_unlock(&u->lock);
381 		break;
382 	default:
383 		error = EINVAL;
384 		break;
385 	}
386 
387 	if (error != 0)
388 		xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
389 
390 	/* Reset the write buffer. */
391 	u->len = 0;
392 
393 	return (0);
394 }
395 
396 static int
xs_dev_poll(struct cdev * dev,int events,struct thread * td)397 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
398 {
399 	struct xs_dev_data *u;
400 	int error, mask;
401 
402 	error = devfs_get_cdevpriv((void **)&u);
403 	if (error != 0)
404 		return (POLLERR);
405 
406 	/* we can always write */
407 	mask = events & (POLLOUT | POLLWRNORM);
408 
409 	if (events & (POLLIN | POLLRDNORM)) {
410 		if (u->read_cons != u->read_prod) {
411 			mask |= events & (POLLIN | POLLRDNORM);
412 		} else {
413 			/* Record that someone is waiting */
414 			selrecord(td, &u->ev_rsel);
415 		}
416 	}
417 
418 	return (mask);
419 }
420 
421 static void
xs_dev_dtor(void * arg)422 xs_dev_dtor(void *arg)
423 {
424 	struct xs_dev_data *u = arg;
425 	struct xs_dev_transaction *trans, *tmpt;
426 	struct xs_dev_watch *watch, *tmpw;
427 
428 	seldrain(&u->ev_rsel);
429 
430 	LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
431 		xs_transaction_end(trans->handle, 1);
432 		LIST_REMOVE(trans, list);
433 		free(trans, M_XENSTORE);
434 	}
435 
436 	LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
437 		LIST_REMOVE(watch, list);
438 		xs_unregister_watch(&watch->watch);
439 		free(watch->watch.node, M_XENSTORE);
440 		free(watch->token, M_XENSTORE);
441 		free(watch, M_XENSTORE);
442 	}
443 	mtx_destroy(&u->lock);
444 
445 	free(u, M_XENSTORE);
446 }
447 
448 static int
xs_dev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)449 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
450 {
451 	struct xs_dev_data *u;
452 	int error;
453 
454 	u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
455 	mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
456 	LIST_INIT(&u->transactions);
457 	LIST_INIT(&u->watches);
458 	error = devfs_set_cdevpriv(u, xs_dev_dtor);
459 	if (error != 0)
460 		free(u, M_XENSTORE);
461 
462 	return (error);
463 }
464 
465 static struct cdevsw xs_dev_cdevsw = {
466 	.d_version = D_VERSION,
467 	.d_read = xs_dev_read,
468 	.d_write = xs_dev_write,
469 	.d_open = xs_dev_open,
470 	.d_poll = xs_dev_poll,
471 	.d_name = "xs_dev",
472 };
473 
474 /*------------------ Private Device Attachment Functions  --------------------*/
475 /**
476  * \brief Identify instances of this device type in the system.
477  *
478  * \param driver  The driver performing this identify action.
479  * \param parent  The NewBus parent device for any devices this method adds.
480  */
481 static void
xs_dev_identify(driver_t * driver __unused,device_t parent)482 xs_dev_identify(driver_t *driver __unused, device_t parent)
483 {
484 	/*
485 	 * A single device instance for our driver is always present
486 	 * in a system operating under Xen.
487 	 */
488 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
489 }
490 
491 /**
492  * \brief Probe for the existence of the Xenstore device
493  *
494  * \param dev  NewBus device_t for this instance.
495  *
496  * \return  Always returns 0 indicating success.
497  */
498 static int
xs_dev_probe(device_t dev)499 xs_dev_probe(device_t dev)
500 {
501 
502 	device_set_desc(dev, "Xenstore user-space device");
503 	return (0);
504 }
505 
506 /**
507  * \brief Attach the Xenstore device.
508  *
509  * \param dev  NewBus device_t for this instance.
510  *
511  * \return  On success, 0. Otherwise an errno value indicating the
512  *          type of failure.
513  */
514 static int
xs_dev_attach(device_t dev)515 xs_dev_attach(device_t dev)
516 {
517 	struct cdev *xs_cdev;
518 	struct sysctl_ctx_list *sysctl_ctx;
519 	struct sysctl_oid *sysctl_tree;
520 
521 	sysctl_ctx = device_get_sysctl_ctx(dev);
522 	sysctl_tree = device_get_sysctl_tree(dev);
523 	if (sysctl_ctx == NULL || sysctl_tree == NULL)
524 	    return (EINVAL);
525 
526 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
527 	    "max_pending_watch_events", CTLFLAG_RW, &max_pending_watches, 0,
528 	    "maximum amount of pending watch events to be delivered");
529 
530 	xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
531 	    UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
532 	if (xs_cdev == NULL)
533 		return (EINVAL);
534 
535 	return (0);
536 }
537 
538 /*-------------------- Private Device Attachment Data  -----------------------*/
539 static device_method_t xs_dev_methods[] = {
540 	/* Device interface */
541 	DEVMETHOD(device_identify,	xs_dev_identify),
542 	DEVMETHOD(device_probe,         xs_dev_probe),
543 	DEVMETHOD(device_attach,        xs_dev_attach),
544 
545 	DEVMETHOD_END
546 };
547 
548 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
549 devclass_t xs_dev_devclass;
550 
551 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,
552     NULL, NULL);
553