xref: /openbmc/linux/net/9p/protocol.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  * net/9p/protocol.c
3  *
4  * 9P Protocol Support Code
5  *
6  *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7  *
8  *  Base on code from Anthony Liguori <aliguori@us.ibm.com>
9  *  Copyright (C) 2008 by IBM, Corp.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2
13  *  as published by the Free Software Foundation.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to:
22  *  Free Software Foundation
23  *  51 Franklin Street, Fifth Floor
24  *  Boston, MA  02111-1301  USA
25  *
26  */
27 
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/uaccess.h>
32 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/stddef.h>
35 #include <linux/types.h>
36 #include <net/9p/9p.h>
37 #include <net/9p/client.h>
38 #include "protocol.h"
39 
40 static int
41 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
42 
43 #ifdef CONFIG_NET_9P_DEBUG
44 void
45 p9pdu_dump(int way, struct p9_fcall *pdu)
46 {
47 	int i, n;
48 	u8 *data = pdu->sdata;
49 	int datalen = pdu->size;
50 	char buf[255];
51 	int buflen = 255;
52 
53 	i = n = 0;
54 	if (datalen > (buflen-16))
55 		datalen = buflen-16;
56 	while (i < datalen) {
57 		n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
58 		if (i%4 == 3)
59 			n += scnprintf(buf + n, buflen - n, " ");
60 		if (i%32 == 31)
61 			n += scnprintf(buf + n, buflen - n, "\n");
62 
63 		i++;
64 	}
65 	n += scnprintf(buf + n, buflen - n, "\n");
66 
67 	if (way)
68 		P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
69 	else
70 		P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
71 }
72 #else
73 void
74 p9pdu_dump(int way, struct p9_fcall *pdu)
75 {
76 }
77 #endif
78 EXPORT_SYMBOL(p9pdu_dump);
79 
80 void p9stat_free(struct p9_wstat *stbuf)
81 {
82 	kfree(stbuf->name);
83 	kfree(stbuf->uid);
84 	kfree(stbuf->gid);
85 	kfree(stbuf->muid);
86 	kfree(stbuf->extension);
87 }
88 EXPORT_SYMBOL(p9stat_free);
89 
90 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
91 {
92 	size_t len = min(pdu->size - pdu->offset, size);
93 	memcpy(data, &pdu->sdata[pdu->offset], len);
94 	pdu->offset += len;
95 	return size - len;
96 }
97 
98 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
99 {
100 	size_t len = min(pdu->capacity - pdu->size, size);
101 	memcpy(&pdu->sdata[pdu->size], data, len);
102 	pdu->size += len;
103 	return size - len;
104 }
105 
106 static size_t
107 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
108 {
109 	size_t len = min(pdu->capacity - pdu->size, size);
110 	if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
111 		len = 0;
112 
113 	pdu->size += len;
114 	return size - len;
115 }
116 
117 /*
118 	b - int8_t
119 	w - int16_t
120 	d - int32_t
121 	q - int64_t
122 	s - string
123 	S - stat
124 	Q - qid
125 	D - data blob (int32_t size followed by void *, results are not freed)
126 	T - array of strings (int16_t count, followed by strings)
127 	R - array of qids (int16_t count, followed by qids)
128 	A - stat for 9p2000.L (p9_stat_dotl)
129 	? - if optional = 1, continue parsing
130 */
131 
132 static int
133 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
134 	va_list ap)
135 {
136 	const char *ptr;
137 	int errcode = 0;
138 
139 	for (ptr = fmt; *ptr; ptr++) {
140 		switch (*ptr) {
141 		case 'b':{
142 				int8_t *val = va_arg(ap, int8_t *);
143 				if (pdu_read(pdu, val, sizeof(*val))) {
144 					errcode = -EFAULT;
145 					break;
146 				}
147 			}
148 			break;
149 		case 'w':{
150 				int16_t *val = va_arg(ap, int16_t *);
151 				__le16 le_val;
152 				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
153 					errcode = -EFAULT;
154 					break;
155 				}
156 				*val = le16_to_cpu(le_val);
157 			}
158 			break;
159 		case 'd':{
160 				int32_t *val = va_arg(ap, int32_t *);
161 				__le32 le_val;
162 				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
163 					errcode = -EFAULT;
164 					break;
165 				}
166 				*val = le32_to_cpu(le_val);
167 			}
168 			break;
169 		case 'q':{
170 				int64_t *val = va_arg(ap, int64_t *);
171 				__le64 le_val;
172 				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
173 					errcode = -EFAULT;
174 					break;
175 				}
176 				*val = le64_to_cpu(le_val);
177 			}
178 			break;
179 		case 's':{
180 				char **sptr = va_arg(ap, char **);
181 				int16_t len;
182 				int size;
183 
184 				errcode = p9pdu_readf(pdu, proto_version,
185 								"w", &len);
186 				if (errcode)
187 					break;
188 
189 				size = max_t(int16_t, len, 0);
190 
191 				*sptr = kmalloc(size + 1, GFP_KERNEL);
192 				if (*sptr == NULL) {
193 					errcode = -EFAULT;
194 					break;
195 				}
196 				if (pdu_read(pdu, *sptr, size)) {
197 					errcode = -EFAULT;
198 					kfree(*sptr);
199 					*sptr = NULL;
200 				} else
201 					(*sptr)[size] = 0;
202 			}
203 			break;
204 		case 'Q':{
205 				struct p9_qid *qid =
206 				    va_arg(ap, struct p9_qid *);
207 
208 				errcode = p9pdu_readf(pdu, proto_version, "bdq",
209 						      &qid->type, &qid->version,
210 						      &qid->path);
211 			}
212 			break;
213 		case 'S':{
214 				struct p9_wstat *stbuf =
215 				    va_arg(ap, struct p9_wstat *);
216 
217 				memset(stbuf, 0, sizeof(struct p9_wstat));
218 				stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
219 									-1;
220 				errcode =
221 				    p9pdu_readf(pdu, proto_version,
222 						"wwdQdddqssss?sddd",
223 						&stbuf->size, &stbuf->type,
224 						&stbuf->dev, &stbuf->qid,
225 						&stbuf->mode, &stbuf->atime,
226 						&stbuf->mtime, &stbuf->length,
227 						&stbuf->name, &stbuf->uid,
228 						&stbuf->gid, &stbuf->muid,
229 						&stbuf->extension,
230 						&stbuf->n_uid, &stbuf->n_gid,
231 						&stbuf->n_muid);
232 				if (errcode)
233 					p9stat_free(stbuf);
234 			}
235 			break;
236 		case 'D':{
237 				int32_t *count = va_arg(ap, int32_t *);
238 				void **data = va_arg(ap, void **);
239 
240 				errcode =
241 				    p9pdu_readf(pdu, proto_version, "d", count);
242 				if (!errcode) {
243 					*count =
244 					    min_t(int32_t, *count,
245 						  pdu->size - pdu->offset);
246 					*data = &pdu->sdata[pdu->offset];
247 				}
248 			}
249 			break;
250 		case 'T':{
251 				int16_t *nwname = va_arg(ap, int16_t *);
252 				char ***wnames = va_arg(ap, char ***);
253 
254 				errcode = p9pdu_readf(pdu, proto_version,
255 								"w", nwname);
256 				if (!errcode) {
257 					*wnames =
258 					    kmalloc(sizeof(char *) * *nwname,
259 						    GFP_KERNEL);
260 					if (!*wnames)
261 						errcode = -ENOMEM;
262 				}
263 
264 				if (!errcode) {
265 					int i;
266 
267 					for (i = 0; i < *nwname; i++) {
268 						errcode =
269 						    p9pdu_readf(pdu,
270 								proto_version,
271 								"s",
272 								&(*wnames)[i]);
273 						if (errcode)
274 							break;
275 					}
276 				}
277 
278 				if (errcode) {
279 					if (*wnames) {
280 						int i;
281 
282 						for (i = 0; i < *nwname; i++)
283 							kfree((*wnames)[i]);
284 					}
285 					kfree(*wnames);
286 					*wnames = NULL;
287 				}
288 			}
289 			break;
290 		case 'R':{
291 				int16_t *nwqid = va_arg(ap, int16_t *);
292 				struct p9_qid **wqids =
293 				    va_arg(ap, struct p9_qid **);
294 
295 				*wqids = NULL;
296 
297 				errcode =
298 				    p9pdu_readf(pdu, proto_version, "w", nwqid);
299 				if (!errcode) {
300 					*wqids =
301 					    kmalloc(*nwqid *
302 						    sizeof(struct p9_qid),
303 						    GFP_KERNEL);
304 					if (*wqids == NULL)
305 						errcode = -ENOMEM;
306 				}
307 
308 				if (!errcode) {
309 					int i;
310 
311 					for (i = 0; i < *nwqid; i++) {
312 						errcode =
313 						    p9pdu_readf(pdu,
314 								proto_version,
315 								"Q",
316 								&(*wqids)[i]);
317 						if (errcode)
318 							break;
319 					}
320 				}
321 
322 				if (errcode) {
323 					kfree(*wqids);
324 					*wqids = NULL;
325 				}
326 			}
327 			break;
328 		case 'A': {
329 				struct p9_stat_dotl *stbuf =
330 				    va_arg(ap, struct p9_stat_dotl *);
331 
332 				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
333 				errcode =
334 				    p9pdu_readf(pdu, proto_version,
335 					"qQdddqqqqqqqqqqqqqqq",
336 					&stbuf->st_result_mask,
337 					&stbuf->qid,
338 					&stbuf->st_mode,
339 					&stbuf->st_uid, &stbuf->st_gid,
340 					&stbuf->st_nlink,
341 					&stbuf->st_rdev, &stbuf->st_size,
342 					&stbuf->st_blksize, &stbuf->st_blocks,
343 					&stbuf->st_atime_sec,
344 					&stbuf->st_atime_nsec,
345 					&stbuf->st_mtime_sec,
346 					&stbuf->st_mtime_nsec,
347 					&stbuf->st_ctime_sec,
348 					&stbuf->st_ctime_nsec,
349 					&stbuf->st_btime_sec,
350 					&stbuf->st_btime_nsec,
351 					&stbuf->st_gen,
352 					&stbuf->st_data_version);
353 			}
354 			break;
355 		case '?':
356 			if ((proto_version != p9_proto_2000u) &&
357 				(proto_version != p9_proto_2000L))
358 				return 0;
359 			break;
360 		default:
361 			BUG();
362 			break;
363 		}
364 
365 		if (errcode)
366 			break;
367 	}
368 
369 	return errcode;
370 }
371 
372 int
373 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
374 	va_list ap)
375 {
376 	const char *ptr;
377 	int errcode = 0;
378 
379 	for (ptr = fmt; *ptr; ptr++) {
380 		switch (*ptr) {
381 		case 'b':{
382 				int8_t val = va_arg(ap, int);
383 				if (pdu_write(pdu, &val, sizeof(val)))
384 					errcode = -EFAULT;
385 			}
386 			break;
387 		case 'w':{
388 				__le16 val = cpu_to_le16(va_arg(ap, int));
389 				if (pdu_write(pdu, &val, sizeof(val)))
390 					errcode = -EFAULT;
391 			}
392 			break;
393 		case 'd':{
394 				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
395 				if (pdu_write(pdu, &val, sizeof(val)))
396 					errcode = -EFAULT;
397 			}
398 			break;
399 		case 'q':{
400 				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
401 				if (pdu_write(pdu, &val, sizeof(val)))
402 					errcode = -EFAULT;
403 			}
404 			break;
405 		case 's':{
406 				const char *sptr = va_arg(ap, const char *);
407 				int16_t len = 0;
408 				if (sptr)
409 					len = min_t(int16_t, strlen(sptr), USHRT_MAX);
410 
411 				errcode = p9pdu_writef(pdu, proto_version,
412 								"w", len);
413 				if (!errcode && pdu_write(pdu, sptr, len))
414 					errcode = -EFAULT;
415 			}
416 			break;
417 		case 'Q':{
418 				const struct p9_qid *qid =
419 				    va_arg(ap, const struct p9_qid *);
420 				errcode =
421 				    p9pdu_writef(pdu, proto_version, "bdq",
422 						 qid->type, qid->version,
423 						 qid->path);
424 			} break;
425 		case 'S':{
426 				const struct p9_wstat *stbuf =
427 				    va_arg(ap, const struct p9_wstat *);
428 				errcode =
429 				    p9pdu_writef(pdu, proto_version,
430 						 "wwdQdddqssss?sddd",
431 						 stbuf->size, stbuf->type,
432 						 stbuf->dev, &stbuf->qid,
433 						 stbuf->mode, stbuf->atime,
434 						 stbuf->mtime, stbuf->length,
435 						 stbuf->name, stbuf->uid,
436 						 stbuf->gid, stbuf->muid,
437 						 stbuf->extension, stbuf->n_uid,
438 						 stbuf->n_gid, stbuf->n_muid);
439 			} break;
440 		case 'D':{
441 				int32_t count = va_arg(ap, int32_t);
442 				const void *data = va_arg(ap, const void *);
443 
444 				errcode = p9pdu_writef(pdu, proto_version, "d",
445 									count);
446 				if (!errcode && pdu_write(pdu, data, count))
447 					errcode = -EFAULT;
448 			}
449 			break;
450 		case 'U':{
451 				int32_t count = va_arg(ap, int32_t);
452 				const char __user *udata =
453 						va_arg(ap, const void __user *);
454 				errcode = p9pdu_writef(pdu, proto_version, "d",
455 									count);
456 				if (!errcode && pdu_write_u(pdu, udata, count))
457 					errcode = -EFAULT;
458 			}
459 			break;
460 		case 'T':{
461 				int16_t nwname = va_arg(ap, int);
462 				const char **wnames = va_arg(ap, const char **);
463 
464 				errcode = p9pdu_writef(pdu, proto_version, "w",
465 									nwname);
466 				if (!errcode) {
467 					int i;
468 
469 					for (i = 0; i < nwname; i++) {
470 						errcode =
471 						    p9pdu_writef(pdu,
472 								proto_version,
473 								 "s",
474 								 wnames[i]);
475 						if (errcode)
476 							break;
477 					}
478 				}
479 			}
480 			break;
481 		case 'R':{
482 				int16_t nwqid = va_arg(ap, int);
483 				struct p9_qid *wqids =
484 				    va_arg(ap, struct p9_qid *);
485 
486 				errcode = p9pdu_writef(pdu, proto_version, "w",
487 									nwqid);
488 				if (!errcode) {
489 					int i;
490 
491 					for (i = 0; i < nwqid; i++) {
492 						errcode =
493 						    p9pdu_writef(pdu,
494 								proto_version,
495 								 "Q",
496 								 &wqids[i]);
497 						if (errcode)
498 							break;
499 					}
500 				}
501 			}
502 			break;
503 		case 'I':{
504 				struct p9_iattr_dotl *p9attr = va_arg(ap,
505 							struct p9_iattr_dotl *);
506 
507 				errcode = p9pdu_writef(pdu, proto_version,
508 							"ddddqqqqq",
509 							p9attr->valid,
510 							p9attr->mode,
511 							p9attr->uid,
512 							p9attr->gid,
513 							p9attr->size,
514 							p9attr->atime_sec,
515 							p9attr->atime_nsec,
516 							p9attr->mtime_sec,
517 							p9attr->mtime_nsec);
518 			}
519 			break;
520 		case '?':
521 			if ((proto_version != p9_proto_2000u) &&
522 				(proto_version != p9_proto_2000L))
523 				return 0;
524 			break;
525 		default:
526 			BUG();
527 			break;
528 		}
529 
530 		if (errcode)
531 			break;
532 	}
533 
534 	return errcode;
535 }
536 
537 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
538 {
539 	va_list ap;
540 	int ret;
541 
542 	va_start(ap, fmt);
543 	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
544 	va_end(ap);
545 
546 	return ret;
547 }
548 
549 static int
550 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
551 {
552 	va_list ap;
553 	int ret;
554 
555 	va_start(ap, fmt);
556 	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
557 	va_end(ap);
558 
559 	return ret;
560 }
561 
562 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
563 {
564 	struct p9_fcall fake_pdu;
565 	int ret;
566 
567 	fake_pdu.size = len;
568 	fake_pdu.capacity = len;
569 	fake_pdu.sdata = buf;
570 	fake_pdu.offset = 0;
571 
572 	ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
573 	if (ret) {
574 		P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
575 		p9pdu_dump(1, &fake_pdu);
576 	}
577 
578 	return ret;
579 }
580 EXPORT_SYMBOL(p9stat_read);
581 
582 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
583 {
584 	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
585 }
586 
587 int p9pdu_finalize(struct p9_fcall *pdu)
588 {
589 	int size = pdu->size;
590 	int err;
591 
592 	pdu->size = 0;
593 	err = p9pdu_writef(pdu, 0, "d", size);
594 	pdu->size = size;
595 
596 #ifdef CONFIG_NET_9P_DEBUG
597 	if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
598 		p9pdu_dump(0, pdu);
599 #endif
600 
601 	P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
602 							pdu->id, pdu->tag);
603 
604 	return err;
605 }
606 
607 void p9pdu_reset(struct p9_fcall *pdu)
608 {
609 	pdu->offset = 0;
610 	pdu->size = 0;
611 }
612 
613 int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
614 						int proto_version)
615 {
616 	struct p9_fcall fake_pdu;
617 	int ret;
618 	char *nameptr;
619 
620 	fake_pdu.size = len;
621 	fake_pdu.capacity = len;
622 	fake_pdu.sdata = buf;
623 	fake_pdu.offset = 0;
624 
625 	ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
626 			&dirent->d_off, &dirent->d_type, &nameptr);
627 	if (ret) {
628 		P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
629 		p9pdu_dump(1, &fake_pdu);
630 		goto out;
631 	}
632 
633 	strcpy(dirent->d_name, nameptr);
634 
635 out:
636 	return fake_pdu.offset;
637 }
638 EXPORT_SYMBOL(p9dirent_read);
639