protocol.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. 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. /*
  101. b - int8_t
  102. w - int16_t
  103. d - int32_t
  104. q - int64_t
  105. s - string
  106. S - stat
  107. Q - qid
  108. D - data blob (int32_t size followed by void *, results are not freed)
  109. T - array of strings (int16_t count, followed by strings)
  110. R - array of qids (int16_t count, followed by qids)
  111. A - stat for 9p2000.L (p9_stat_dotl)
  112. ? - if optional = 1, continue parsing
  113. */
  114. static int
  115. p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
  116. va_list ap)
  117. {
  118. const char *ptr;
  119. int errcode = 0;
  120. for (ptr = fmt; *ptr; ptr++) {
  121. switch (*ptr) {
  122. case 'b':{
  123. int8_t *val = va_arg(ap, int8_t *);
  124. if (pdu_read(pdu, val, sizeof(*val))) {
  125. errcode = -EFAULT;
  126. break;
  127. }
  128. }
  129. break;
  130. case 'w':{
  131. int16_t *val = va_arg(ap, int16_t *);
  132. __le16 le_val;
  133. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  134. errcode = -EFAULT;
  135. break;
  136. }
  137. *val = le16_to_cpu(le_val);
  138. }
  139. break;
  140. case 'd':{
  141. int32_t *val = va_arg(ap, int32_t *);
  142. __le32 le_val;
  143. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  144. errcode = -EFAULT;
  145. break;
  146. }
  147. *val = le32_to_cpu(le_val);
  148. }
  149. break;
  150. case 'q':{
  151. int64_t *val = va_arg(ap, int64_t *);
  152. __le64 le_val;
  153. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  154. errcode = -EFAULT;
  155. break;
  156. }
  157. *val = le64_to_cpu(le_val);
  158. }
  159. break;
  160. case 's':{
  161. char **sptr = va_arg(ap, char **);
  162. uint16_t len;
  163. errcode = p9pdu_readf(pdu, proto_version,
  164. "w", &len);
  165. if (errcode)
  166. break;
  167. *sptr = kmalloc(len + 1, GFP_NOFS);
  168. if (*sptr == NULL) {
  169. errcode = -EFAULT;
  170. break;
  171. }
  172. if (pdu_read(pdu, *sptr, len)) {
  173. errcode = -EFAULT;
  174. kfree(*sptr);
  175. *sptr = NULL;
  176. } else
  177. (*sptr)[len] = 0;
  178. }
  179. break;
  180. case 'Q':{
  181. struct p9_qid *qid =
  182. va_arg(ap, struct p9_qid *);
  183. errcode = p9pdu_readf(pdu, proto_version, "bdq",
  184. &qid->type, &qid->version,
  185. &qid->path);
  186. }
  187. break;
  188. case 'S':{
  189. struct p9_wstat *stbuf =
  190. va_arg(ap, struct p9_wstat *);
  191. memset(stbuf, 0, sizeof(struct p9_wstat));
  192. stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
  193. -1;
  194. errcode =
  195. p9pdu_readf(pdu, proto_version,
  196. "wwdQdddqssss?sddd",
  197. &stbuf->size, &stbuf->type,
  198. &stbuf->dev, &stbuf->qid,
  199. &stbuf->mode, &stbuf->atime,
  200. &stbuf->mtime, &stbuf->length,
  201. &stbuf->name, &stbuf->uid,
  202. &stbuf->gid, &stbuf->muid,
  203. &stbuf->extension,
  204. &stbuf->n_uid, &stbuf->n_gid,
  205. &stbuf->n_muid);
  206. if (errcode)
  207. p9stat_free(stbuf);
  208. }
  209. break;
  210. case 'D':{
  211. uint32_t *count = va_arg(ap, uint32_t *);
  212. void **data = va_arg(ap, void **);
  213. errcode =
  214. p9pdu_readf(pdu, proto_version, "d", count);
  215. if (!errcode) {
  216. *count =
  217. min_t(uint32_t, *count,
  218. pdu->size - pdu->offset);
  219. *data = &pdu->sdata[pdu->offset];
  220. }
  221. }
  222. break;
  223. case 'T':{
  224. uint16_t *nwname = va_arg(ap, uint16_t *);
  225. char ***wnames = va_arg(ap, char ***);
  226. errcode = p9pdu_readf(pdu, proto_version,
  227. "w", nwname);
  228. if (!errcode) {
  229. *wnames =
  230. kmalloc(sizeof(char *) * *nwname,
  231. GFP_NOFS);
  232. if (!*wnames)
  233. errcode = -ENOMEM;
  234. }
  235. if (!errcode) {
  236. int i;
  237. for (i = 0; i < *nwname; i++) {
  238. errcode =
  239. p9pdu_readf(pdu,
  240. proto_version,
  241. "s",
  242. &(*wnames)[i]);
  243. if (errcode)
  244. break;
  245. }
  246. }
  247. if (errcode) {
  248. if (*wnames) {
  249. int i;
  250. for (i = 0; i < *nwname; i++)
  251. kfree((*wnames)[i]);
  252. }
  253. kfree(*wnames);
  254. *wnames = NULL;
  255. }
  256. }
  257. break;
  258. case 'R':{
  259. int16_t *nwqid = va_arg(ap, int16_t *);
  260. struct p9_qid **wqids =
  261. va_arg(ap, struct p9_qid **);
  262. *wqids = NULL;
  263. errcode =
  264. p9pdu_readf(pdu, proto_version, "w", nwqid);
  265. if (!errcode) {
  266. *wqids =
  267. kmalloc(*nwqid *
  268. sizeof(struct p9_qid),
  269. GFP_NOFS);
  270. if (*wqids == NULL)
  271. errcode = -ENOMEM;
  272. }
  273. if (!errcode) {
  274. int i;
  275. for (i = 0; i < *nwqid; i++) {
  276. errcode =
  277. p9pdu_readf(pdu,
  278. proto_version,
  279. "Q",
  280. &(*wqids)[i]);
  281. if (errcode)
  282. break;
  283. }
  284. }
  285. if (errcode) {
  286. kfree(*wqids);
  287. *wqids = NULL;
  288. }
  289. }
  290. break;
  291. case 'A': {
  292. struct p9_stat_dotl *stbuf =
  293. va_arg(ap, struct p9_stat_dotl *);
  294. memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  295. errcode =
  296. p9pdu_readf(pdu, proto_version,
  297. "qQdddqqqqqqqqqqqqqqq",
  298. &stbuf->st_result_mask,
  299. &stbuf->qid,
  300. &stbuf->st_mode,
  301. &stbuf->st_uid, &stbuf->st_gid,
  302. &stbuf->st_nlink,
  303. &stbuf->st_rdev, &stbuf->st_size,
  304. &stbuf->st_blksize, &stbuf->st_blocks,
  305. &stbuf->st_atime_sec,
  306. &stbuf->st_atime_nsec,
  307. &stbuf->st_mtime_sec,
  308. &stbuf->st_mtime_nsec,
  309. &stbuf->st_ctime_sec,
  310. &stbuf->st_ctime_nsec,
  311. &stbuf->st_btime_sec,
  312. &stbuf->st_btime_nsec,
  313. &stbuf->st_gen,
  314. &stbuf->st_data_version);
  315. }
  316. break;
  317. case '?':
  318. if ((proto_version != p9_proto_2000u) &&
  319. (proto_version != p9_proto_2000L))
  320. return 0;
  321. break;
  322. default:
  323. BUG();
  324. break;
  325. }
  326. if (errcode)
  327. break;
  328. }
  329. return errcode;
  330. }
  331. int
  332. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  333. va_list ap)
  334. {
  335. const char *ptr;
  336. int errcode = 0;
  337. for (ptr = fmt; *ptr; ptr++) {
  338. switch (*ptr) {
  339. case 'b':{
  340. int8_t val = va_arg(ap, int);
  341. if (pdu_write(pdu, &val, sizeof(val)))
  342. errcode = -EFAULT;
  343. }
  344. break;
  345. case 'w':{
  346. __le16 val = cpu_to_le16(va_arg(ap, int));
  347. if (pdu_write(pdu, &val, sizeof(val)))
  348. errcode = -EFAULT;
  349. }
  350. break;
  351. case 'd':{
  352. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  353. if (pdu_write(pdu, &val, sizeof(val)))
  354. errcode = -EFAULT;
  355. }
  356. break;
  357. case 'q':{
  358. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  359. if (pdu_write(pdu, &val, sizeof(val)))
  360. errcode = -EFAULT;
  361. }
  362. break;
  363. case 's':{
  364. const char *sptr = va_arg(ap, const char *);
  365. uint16_t len = 0;
  366. if (sptr)
  367. len = min_t(uint16_t, strlen(sptr),
  368. USHRT_MAX);
  369. errcode = p9pdu_writef(pdu, proto_version,
  370. "w", len);
  371. if (!errcode && pdu_write(pdu, sptr, len))
  372. errcode = -EFAULT;
  373. }
  374. break;
  375. case 'Q':{
  376. const struct p9_qid *qid =
  377. va_arg(ap, const struct p9_qid *);
  378. errcode =
  379. p9pdu_writef(pdu, proto_version, "bdq",
  380. qid->type, qid->version,
  381. qid->path);
  382. } break;
  383. case 'S':{
  384. const struct p9_wstat *stbuf =
  385. va_arg(ap, const struct p9_wstat *);
  386. errcode =
  387. p9pdu_writef(pdu, proto_version,
  388. "wwdQdddqssss?sddd",
  389. stbuf->size, stbuf->type,
  390. stbuf->dev, &stbuf->qid,
  391. stbuf->mode, stbuf->atime,
  392. stbuf->mtime, stbuf->length,
  393. stbuf->name, stbuf->uid,
  394. stbuf->gid, stbuf->muid,
  395. stbuf->extension, stbuf->n_uid,
  396. stbuf->n_gid, stbuf->n_muid);
  397. } break;
  398. case 'D':{
  399. uint32_t count = va_arg(ap, uint32_t);
  400. const void *data = va_arg(ap, const void *);
  401. errcode = p9pdu_writef(pdu, proto_version, "d",
  402. count);
  403. if (!errcode && pdu_write(pdu, data, count))
  404. errcode = -EFAULT;
  405. }
  406. break;
  407. case 'U':{
  408. int32_t count = va_arg(ap, int32_t);
  409. const char __user *udata =
  410. va_arg(ap, const void __user *);
  411. errcode = p9pdu_writef(pdu, proto_version, "d",
  412. count);
  413. if (!errcode && pdu_write_u(pdu, udata, count))
  414. errcode = -EFAULT;
  415. }
  416. break;
  417. case 'T':{
  418. uint16_t nwname = va_arg(ap, int);
  419. const char **wnames = va_arg(ap, const char **);
  420. errcode = p9pdu_writef(pdu, proto_version, "w",
  421. nwname);
  422. if (!errcode) {
  423. int i;
  424. for (i = 0; i < nwname; i++) {
  425. errcode =
  426. p9pdu_writef(pdu,
  427. proto_version,
  428. "s",
  429. wnames[i]);
  430. if (errcode)
  431. break;
  432. }
  433. }
  434. }
  435. break;
  436. case 'R':{
  437. int16_t nwqid = va_arg(ap, int);
  438. struct p9_qid *wqids =
  439. va_arg(ap, struct p9_qid *);
  440. errcode = p9pdu_writef(pdu, proto_version, "w",
  441. nwqid);
  442. if (!errcode) {
  443. int i;
  444. for (i = 0; i < nwqid; i++) {
  445. errcode =
  446. p9pdu_writef(pdu,
  447. proto_version,
  448. "Q",
  449. &wqids[i]);
  450. if (errcode)
  451. break;
  452. }
  453. }
  454. }
  455. break;
  456. case 'I':{
  457. struct p9_iattr_dotl *p9attr = va_arg(ap,
  458. struct p9_iattr_dotl *);
  459. errcode = p9pdu_writef(pdu, proto_version,
  460. "ddddqqqqq",
  461. p9attr->valid,
  462. p9attr->mode,
  463. p9attr->uid,
  464. p9attr->gid,
  465. p9attr->size,
  466. p9attr->atime_sec,
  467. p9attr->atime_nsec,
  468. p9attr->mtime_sec,
  469. p9attr->mtime_nsec);
  470. }
  471. break;
  472. case '?':
  473. if ((proto_version != p9_proto_2000u) &&
  474. (proto_version != p9_proto_2000L))
  475. return 0;
  476. break;
  477. default:
  478. BUG();
  479. break;
  480. }
  481. if (errcode)
  482. break;
  483. }
  484. return errcode;
  485. }
  486. int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  487. {
  488. va_list ap;
  489. int ret;
  490. va_start(ap, fmt);
  491. ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
  492. va_end(ap);
  493. return ret;
  494. }
  495. static int
  496. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  497. {
  498. va_list ap;
  499. int ret;
  500. va_start(ap, fmt);
  501. ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
  502. va_end(ap);
  503. return ret;
  504. }
  505. int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
  506. {
  507. struct p9_fcall fake_pdu;
  508. int ret;
  509. fake_pdu.size = len;
  510. fake_pdu.capacity = len;
  511. fake_pdu.sdata = buf;
  512. fake_pdu.offset = 0;
  513. ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
  514. if (ret) {
  515. P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  516. P9_DUMP_PKT(0, &fake_pdu);
  517. }
  518. return ret;
  519. }
  520. EXPORT_SYMBOL(p9stat_read);
  521. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  522. {
  523. pdu->id = type;
  524. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  525. }
  526. int p9pdu_finalize(struct p9_fcall *pdu)
  527. {
  528. int size = pdu->size;
  529. int err;
  530. pdu->size = 0;
  531. err = p9pdu_writef(pdu, 0, "d", size);
  532. pdu->size = size;
  533. P9_DUMP_PKT(0, pdu);
  534. P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
  535. pdu->id, pdu->tag);
  536. return err;
  537. }
  538. void p9pdu_reset(struct p9_fcall *pdu)
  539. {
  540. pdu->offset = 0;
  541. pdu->size = 0;
  542. }
  543. int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
  544. int proto_version)
  545. {
  546. struct p9_fcall fake_pdu;
  547. int ret;
  548. char *nameptr;
  549. fake_pdu.size = len;
  550. fake_pdu.capacity = len;
  551. fake_pdu.sdata = buf;
  552. fake_pdu.offset = 0;
  553. ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
  554. &dirent->d_off, &dirent->d_type, &nameptr);
  555. if (ret) {
  556. P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
  557. P9_DUMP_PKT(1, &fake_pdu);
  558. goto out;
  559. }
  560. strcpy(dirent->d_name, nameptr);
  561. kfree(nameptr);
  562. out:
  563. return fake_pdu.offset;
  564. }
  565. EXPORT_SYMBOL(p9dirent_read);