smb_dev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2000-2001 Boris Popov
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/kernel.h>
  32. #include <sys/capsicum.h>
  33. #include <sys/module.h>
  34. #include <sys/systm.h>
  35. #include <sys/conf.h>
  36. #include <sys/fcntl.h>
  37. #include <sys/ioccom.h>
  38. #include <sys/lock.h>
  39. #include <sys/malloc.h>
  40. #include <sys/file.h> /* Must come after sys/malloc.h */
  41. #include <sys/filedesc.h>
  42. #include <sys/mbuf.h>
  43. #include <sys/poll.h>
  44. #include <sys/proc.h>
  45. #include <sys/select.h>
  46. #include <sys/socket.h>
  47. #include <sys/socketvar.h>
  48. #include <sys/sysctl.h>
  49. #include <sys/uio.h>
  50. #include <sys/vnode.h>
  51. #include <net/if.h>
  52. #include <netsmb/smb.h>
  53. #include <netsmb/smb_conn.h>
  54. #include <netsmb/smb_subr.h>
  55. #include <netsmb/smb_dev.h>
  56. static struct cdev *nsmb_dev;
  57. static d_open_t nsmb_dev_open;
  58. static d_ioctl_t nsmb_dev_ioctl;
  59. MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
  60. MODULE_VERSION(netsmb, NSMB_VERSION);
  61. static int smb_version = NSMB_VERSION;
  62. struct sx smb_lock;
  63. SYSCTL_DECL(_net_smb);
  64. SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
  65. static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
  66. static struct cdevsw nsmb_cdevsw = {
  67. .d_version = D_VERSION,
  68. .d_open = nsmb_dev_open,
  69. .d_ioctl = nsmb_dev_ioctl,
  70. .d_name = NSMB_NAME
  71. };
  72. static int
  73. nsmb_dev_init(void)
  74. {
  75. nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
  76. 0600, "nsmb");
  77. if (nsmb_dev == NULL)
  78. return (ENOMEM);
  79. return (0);
  80. }
  81. static void
  82. nsmb_dev_destroy(void)
  83. {
  84. MPASS(nsmb_dev != NULL);
  85. destroy_dev(nsmb_dev);
  86. nsmb_dev = NULL;
  87. }
  88. static struct smb_dev *
  89. smbdev_alloc(struct cdev *dev)
  90. {
  91. struct smb_dev *sdp;
  92. sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
  93. sdp->dev = dev;
  94. sdp->sd_level = -1;
  95. sdp->sd_flags |= NSMBFL_OPEN;
  96. sdp->refcount = 1;
  97. return (sdp);
  98. }
  99. void
  100. sdp_dtor(void *arg)
  101. {
  102. struct smb_dev *dev;
  103. dev = (struct smb_dev *)arg;
  104. SMB_LOCK();
  105. sdp_trydestroy(dev);
  106. SMB_UNLOCK();
  107. }
  108. static int
  109. nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  110. {
  111. struct smb_dev *sdp;
  112. int error;
  113. sdp = smbdev_alloc(dev);
  114. error = devfs_set_cdevpriv(sdp, sdp_dtor);
  115. if (error) {
  116. free(sdp, M_NSMBDEV);
  117. return (error);
  118. }
  119. return (0);
  120. }
  121. void
  122. sdp_trydestroy(struct smb_dev *sdp)
  123. {
  124. struct smb_vc *vcp;
  125. struct smb_share *ssp;
  126. struct smb_cred *scred;
  127. SMB_LOCKASSERT();
  128. if (!sdp)
  129. panic("No smb_dev upon device close");
  130. MPASS(sdp->refcount > 0);
  131. sdp->refcount--;
  132. if (sdp->refcount)
  133. return;
  134. scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
  135. smb_makescred(scred, curthread, NULL);
  136. ssp = sdp->sd_share;
  137. if (ssp != NULL) {
  138. smb_share_lock(ssp);
  139. smb_share_rele(ssp, scred);
  140. }
  141. vcp = sdp->sd_vc;
  142. if (vcp != NULL) {
  143. smb_vc_lock(vcp);
  144. smb_vc_rele(vcp, scred);
  145. }
  146. free(scred, M_NSMBDEV);
  147. free(sdp, M_NSMBDEV);
  148. return;
  149. }
  150. static int
  151. nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  152. {
  153. struct smb_dev *sdp;
  154. struct smb_vc *vcp;
  155. struct smb_share *ssp;
  156. struct smb_cred *scred;
  157. int error = 0;
  158. error = devfs_get_cdevpriv((void **)&sdp);
  159. if (error)
  160. return (error);
  161. scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
  162. SMB_LOCK();
  163. smb_makescred(scred, td, NULL);
  164. switch (cmd) {
  165. case SMBIOC_OPENSESSION:
  166. if (sdp->sd_vc) {
  167. error = EISCONN;
  168. goto out;
  169. }
  170. error = smb_usr_opensession((struct smbioc_ossn*)data,
  171. scred, &vcp);
  172. if (error)
  173. break;
  174. sdp->sd_vc = vcp;
  175. smb_vc_unlock(vcp);
  176. sdp->sd_level = SMBL_VC;
  177. break;
  178. case SMBIOC_OPENSHARE:
  179. if (sdp->sd_share) {
  180. error = EISCONN;
  181. goto out;
  182. }
  183. if (sdp->sd_vc == NULL) {
  184. error = ENOTCONN;
  185. goto out;
  186. }
  187. error = smb_usr_openshare(sdp->sd_vc,
  188. (struct smbioc_oshare*)data, scred, &ssp);
  189. if (error)
  190. break;
  191. sdp->sd_share = ssp;
  192. smb_share_unlock(ssp);
  193. sdp->sd_level = SMBL_SHARE;
  194. break;
  195. case SMBIOC_REQUEST:
  196. if (sdp->sd_share == NULL) {
  197. error = ENOTCONN;
  198. goto out;
  199. }
  200. error = smb_usr_simplerequest(sdp->sd_share,
  201. (struct smbioc_rq*)data, scred);
  202. break;
  203. case SMBIOC_T2RQ:
  204. if (sdp->sd_share == NULL) {
  205. error = ENOTCONN;
  206. goto out;
  207. }
  208. error = smb_usr_t2request(sdp->sd_share,
  209. (struct smbioc_t2rq*)data, scred);
  210. break;
  211. case SMBIOC_SETFLAGS: {
  212. struct smbioc_flags *fl = (struct smbioc_flags*)data;
  213. int on;
  214. if (fl->ioc_level == SMBL_VC) {
  215. if (fl->ioc_mask & SMBV_PERMANENT) {
  216. on = fl->ioc_flags & SMBV_PERMANENT;
  217. if ((vcp = sdp->sd_vc) == NULL) {
  218. error = ENOTCONN;
  219. goto out;
  220. }
  221. error = smb_vc_get(vcp, scred);
  222. if (error)
  223. break;
  224. if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
  225. vcp->obj.co_flags |= SMBV_PERMANENT;
  226. smb_vc_ref(vcp);
  227. } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
  228. vcp->obj.co_flags &= ~SMBV_PERMANENT;
  229. smb_vc_rele(vcp, scred);
  230. }
  231. smb_vc_put(vcp, scred);
  232. } else
  233. error = EINVAL;
  234. } else if (fl->ioc_level == SMBL_SHARE) {
  235. if (fl->ioc_mask & SMBS_PERMANENT) {
  236. on = fl->ioc_flags & SMBS_PERMANENT;
  237. if ((ssp = sdp->sd_share) == NULL) {
  238. error = ENOTCONN;
  239. goto out;
  240. }
  241. error = smb_share_get(ssp, scred);
  242. if (error)
  243. break;
  244. if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
  245. ssp->obj.co_flags |= SMBS_PERMANENT;
  246. smb_share_ref(ssp);
  247. } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
  248. ssp->obj.co_flags &= ~SMBS_PERMANENT;
  249. smb_share_rele(ssp, scred);
  250. }
  251. smb_share_put(ssp, scred);
  252. } else
  253. error = EINVAL;
  254. break;
  255. } else
  256. error = EINVAL;
  257. break;
  258. }
  259. case SMBIOC_LOOKUP:
  260. if (sdp->sd_vc || sdp->sd_share) {
  261. error = EISCONN;
  262. goto out;
  263. }
  264. vcp = NULL;
  265. ssp = NULL;
  266. error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
  267. if (error)
  268. break;
  269. if (vcp) {
  270. sdp->sd_vc = vcp;
  271. smb_vc_unlock(vcp);
  272. sdp->sd_level = SMBL_VC;
  273. }
  274. if (ssp) {
  275. sdp->sd_share = ssp;
  276. smb_share_unlock(ssp);
  277. sdp->sd_level = SMBL_SHARE;
  278. }
  279. break;
  280. case SMBIOC_READ: case SMBIOC_WRITE: {
  281. struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
  282. struct uio auio;
  283. struct iovec iov;
  284. if ((ssp = sdp->sd_share) == NULL) {
  285. error = ENOTCONN;
  286. goto out;
  287. }
  288. iov.iov_base = rwrq->ioc_base;
  289. iov.iov_len = rwrq->ioc_cnt;
  290. auio.uio_iov = &iov;
  291. auio.uio_iovcnt = 1;
  292. auio.uio_offset = rwrq->ioc_offset;
  293. auio.uio_resid = rwrq->ioc_cnt;
  294. auio.uio_segflg = UIO_USERSPACE;
  295. auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
  296. auio.uio_td = td;
  297. if (cmd == SMBIOC_READ)
  298. error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
  299. else
  300. error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
  301. rwrq->ioc_cnt -= auio.uio_resid;
  302. break;
  303. }
  304. default:
  305. error = ENODEV;
  306. }
  307. out:
  308. free(scred, M_NSMBDEV);
  309. SMB_UNLOCK();
  310. return error;
  311. }
  312. static int
  313. nsmb_dev_load(module_t mod, int cmd, void *arg)
  314. {
  315. int error = 0;
  316. switch (cmd) {
  317. case MOD_LOAD:
  318. error = smb_sm_init();
  319. if (error)
  320. break;
  321. error = smb_iod_init();
  322. if (error) {
  323. smb_sm_done();
  324. break;
  325. }
  326. error = nsmb_dev_init();
  327. if (error)
  328. break;
  329. sx_init(&smb_lock, "samba device lock");
  330. break;
  331. case MOD_UNLOAD:
  332. smb_iod_done();
  333. error = smb_sm_done();
  334. if (error)
  335. break;
  336. nsmb_dev_destroy();
  337. sx_destroy(&smb_lock);
  338. break;
  339. default:
  340. error = EINVAL;
  341. break;
  342. }
  343. return error;
  344. }
  345. DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
  346. int
  347. smb_dev2share(int fd, int mode, struct smb_cred *scred,
  348. struct smb_share **sspp, struct smb_dev **ssdp)
  349. {
  350. struct file *fp, *fptmp;
  351. struct smb_dev *sdp;
  352. struct smb_share *ssp;
  353. struct thread *td;
  354. int error;
  355. td = curthread;
  356. error = fget(td, fd, &cap_read_rights, &fp);
  357. if (error)
  358. return (error);
  359. fptmp = td->td_fpop;
  360. td->td_fpop = fp;
  361. error = devfs_get_cdevpriv((void **)&sdp);
  362. td->td_fpop = fptmp;
  363. fdrop(fp, td);
  364. if (error || sdp == NULL)
  365. return (error);
  366. SMB_LOCK();
  367. *ssdp = sdp;
  368. ssp = sdp->sd_share;
  369. if (ssp == NULL) {
  370. SMB_UNLOCK();
  371. return (ENOTCONN);
  372. }
  373. error = smb_share_get(ssp, scred);
  374. if (error == 0) {
  375. sdp->refcount++;
  376. *sspp = ssp;
  377. }
  378. SMB_UNLOCK();
  379. return error;
  380. }