protocol.c 14 KB

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