protocol.c 14 KB

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