smb_trantcp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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/condvar.h>
  32. #include <sys/kernel.h>
  33. #include <sys/lock.h>
  34. #include <sys/malloc.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/poll.h>
  37. #include <sys/proc.h>
  38. #include <sys/protosw.h>
  39. #include <sys/signalvar.h>
  40. #include <sys/socket.h>
  41. #include <sys/socketvar.h>
  42. #include <sys/sx.h>
  43. #include <sys/sysctl.h>
  44. #include <sys/systm.h>
  45. #include <sys/uio.h>
  46. #include <net/if.h>
  47. #include <net/route.h>
  48. #include <net/vnet.h>
  49. #include <netinet/in.h>
  50. #include <netinet/tcp.h>
  51. #include <sys/mchain.h>
  52. #include <netsmb/netbios.h>
  53. #include <netsmb/smb.h>
  54. #include <netsmb/smb_conn.h>
  55. #include <netsmb/smb_tran.h>
  56. #include <netsmb/smb_trantcp.h>
  57. #include <netsmb/smb_subr.h>
  58. #define M_NBDATA M_PCB
  59. static int smb_tcpsndbuf = NB_SNDQ - 1;
  60. static int smb_tcprcvbuf = NB_RCVQ - 1;
  61. SYSCTL_DECL(_net_smb);
  62. SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, "");
  63. SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, "");
  64. #define nb_sosend(so,m,flags,td) sosend(so, NULL, 0, m, 0, flags, td)
  65. static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
  66. u_int8_t *rpcodep, struct thread *td);
  67. static int smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td);
  68. static int
  69. nb_setsockopt_int(struct socket *so, int level, int name, int val)
  70. {
  71. struct sockopt sopt;
  72. int error;
  73. bzero(&sopt, sizeof(sopt));
  74. sopt.sopt_level = level;
  75. sopt.sopt_name = name;
  76. sopt.sopt_val = &val;
  77. sopt.sopt_valsize = sizeof(val);
  78. CURVNET_SET(so->so_vnet);
  79. error = sosetopt(so, &sopt);
  80. CURVNET_RESTORE();
  81. return error;
  82. }
  83. static int
  84. nb_intr(struct nbpcb *nbp, struct proc *p)
  85. {
  86. return 0;
  87. }
  88. static int
  89. nb_upcall(struct socket *so, void *arg, int waitflag)
  90. {
  91. struct nbpcb *nbp = arg;
  92. if (arg == NULL || nbp->nbp_selectid == NULL)
  93. return (SU_OK);
  94. wakeup(nbp->nbp_selectid);
  95. return (SU_OK);
  96. }
  97. static int
  98. nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
  99. {
  100. u_int32_t *p = mtod(m, u_int32_t *);
  101. *p = htonl((len & 0x1FFFF) | (type << 24));
  102. return 0;
  103. }
  104. static int
  105. nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
  106. {
  107. int error;
  108. u_char seglen, *cp;
  109. cp = snb->snb_name;
  110. if (*cp == 0)
  111. return EINVAL;
  112. NBDEBUG("[%s]\n", cp);
  113. for (;;) {
  114. seglen = (*cp) + 1;
  115. error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
  116. if (error)
  117. return error;
  118. if (seglen == 1)
  119. break;
  120. cp += seglen;
  121. }
  122. return 0;
  123. }
  124. static int
  125. nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct thread *td)
  126. {
  127. struct socket *so;
  128. int error, s;
  129. error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP,
  130. td->td_ucred, td);
  131. if (error)
  132. return error;
  133. nbp->nbp_tso = so;
  134. SOCKBUF_LOCK(&so->so_rcv);
  135. soupcall_set(so, SO_RCV, nb_upcall, nbp);
  136. SOCKBUF_UNLOCK(&so->so_rcv);
  137. so->so_rcv.sb_timeo = (5 * SBT_1S);
  138. so->so_snd.sb_timeo = (5 * SBT_1S);
  139. error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf);
  140. if (error)
  141. goto bad;
  142. nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
  143. nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
  144. SOCKBUF_LOCK(&so->so_rcv);
  145. so->so_rcv.sb_flags &= ~SB_NOINTR;
  146. SOCKBUF_UNLOCK(&so->so_rcv);
  147. SOCKBUF_LOCK(&so->so_snd);
  148. so->so_snd.sb_flags &= ~SB_NOINTR;
  149. SOCKBUF_UNLOCK(&so->so_snd);
  150. error = soconnect(so, (struct sockaddr*)to, td);
  151. if (error)
  152. goto bad;
  153. s = splnet();
  154. while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  155. tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
  156. if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
  157. (error = nb_intr(nbp, td->td_proc)) != 0) {
  158. so->so_state &= ~SS_ISCONNECTING;
  159. splx(s);
  160. goto bad;
  161. }
  162. }
  163. if (so->so_error) {
  164. error = so->so_error;
  165. so->so_error = 0;
  166. splx(s);
  167. goto bad;
  168. }
  169. splx(s);
  170. return 0;
  171. bad:
  172. smb_nbst_disconnect(nbp->nbp_vc, td);
  173. return error;
  174. }
  175. static int
  176. nbssn_rq_request(struct nbpcb *nbp, struct thread *td)
  177. {
  178. struct mbchain *mbp;
  179. struct mdchain *mdp;
  180. struct mbuf *m0;
  181. struct timeval tv;
  182. struct sockaddr_in sin;
  183. u_short port;
  184. u_int8_t rpcode;
  185. int error, rplen;
  186. mbp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
  187. mdp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
  188. error = mb_init(mbp);
  189. if (error) {
  190. free(mbp, M_NBDATA);
  191. free(mdp, M_NBDATA);
  192. return error;
  193. }
  194. mb_put_uint32le(mbp, 0);
  195. nb_put_name(mbp, nbp->nbp_paddr);
  196. nb_put_name(mbp, nbp->nbp_laddr);
  197. nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
  198. error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td);
  199. if (!error) {
  200. nbp->nbp_state = NBST_RQSENT;
  201. }
  202. mb_detach(mbp);
  203. mb_done(mbp);
  204. free(mbp, M_NBDATA);
  205. if (error) {
  206. free(mdp, M_NBDATA);
  207. return error;
  208. }
  209. TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo);
  210. error = selsocket(nbp->nbp_tso, POLLIN, &tv, td);
  211. if (error == EWOULDBLOCK) { /* Timeout */
  212. NBDEBUG("initial request timeout\n");
  213. free(mdp, M_NBDATA);
  214. return ETIMEDOUT;
  215. }
  216. if (error) { /* restart or interrupt */
  217. free(mdp, M_NBDATA);
  218. return error;
  219. }
  220. error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td);
  221. if (error) {
  222. NBDEBUG("recv() error %d\n", error);
  223. free(mdp, M_NBDATA);
  224. return error;
  225. }
  226. /*
  227. * Process NETBIOS reply
  228. */
  229. if (m0)
  230. md_initm(mdp, m0);
  231. error = 0;
  232. do {
  233. if (rpcode == NB_SSN_POSRESP) {
  234. nbp->nbp_state = NBST_SESSION;
  235. nbp->nbp_flags |= NBF_CONNECTED;
  236. break;
  237. }
  238. if (rpcode != NB_SSN_RTGRESP) {
  239. error = ECONNABORTED;
  240. break;
  241. }
  242. if (rplen != 6) {
  243. error = ECONNABORTED;
  244. break;
  245. }
  246. md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
  247. md_get_uint16(mdp, &port);
  248. sin.sin_port = port;
  249. nbp->nbp_state = NBST_RETARGET;
  250. smb_nbst_disconnect(nbp->nbp_vc, td);
  251. error = nb_connect_in(nbp, &sin, td);
  252. if (!error)
  253. error = nbssn_rq_request(nbp, td);
  254. if (error) {
  255. smb_nbst_disconnect(nbp->nbp_vc, td);
  256. break;
  257. }
  258. } while(0);
  259. if (m0)
  260. md_done(mdp);
  261. free(mdp, M_NBDATA);
  262. return error;
  263. }
  264. static int
  265. nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
  266. u_int8_t *rpcodep, int flags, struct thread *td)
  267. {
  268. struct socket *so = nbp->nbp_tso;
  269. struct uio auio;
  270. struct iovec aio;
  271. u_int32_t len;
  272. int error;
  273. aio.iov_base = (caddr_t)&len;
  274. aio.iov_len = sizeof(len);
  275. auio.uio_iov = &aio;
  276. auio.uio_iovcnt = 1;
  277. auio.uio_segflg = UIO_SYSSPACE;
  278. auio.uio_rw = UIO_READ;
  279. auio.uio_offset = 0;
  280. auio.uio_resid = sizeof(len);
  281. auio.uio_td = td;
  282. CURVNET_SET(so->so_vnet);
  283. error = soreceive(so, (struct sockaddr **)NULL, &auio,
  284. (struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
  285. CURVNET_RESTORE();
  286. if (error)
  287. return error;
  288. if (auio.uio_resid > 0) {
  289. SMBSDEBUG("short reply\n");
  290. return EPIPE;
  291. }
  292. len = ntohl(len);
  293. *rpcodep = (len >> 24) & 0xFF;
  294. len &= 0x1ffff;
  295. if (len > SMB_MAXPKTLEN) {
  296. SMBERROR("packet too long (%d)\n", len);
  297. return EFBIG;
  298. }
  299. *lenp = len;
  300. return 0;
  301. }
  302. static int
  303. nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
  304. u_int8_t *rpcodep, struct thread *td)
  305. {
  306. struct socket *so = nbp->nbp_tso;
  307. struct uio auio;
  308. struct mbuf *m, *tm, *im;
  309. u_int8_t rpcode;
  310. int len, resid;
  311. int error, rcvflg;
  312. if (so == NULL)
  313. return ENOTCONN;
  314. if (mpp)
  315. *mpp = NULL;
  316. m = NULL;
  317. for(;;) {
  318. /*
  319. * Poll for a response header.
  320. * If we don't have one waiting, return.
  321. */
  322. len = 0;
  323. rpcode = 0;
  324. error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, td);
  325. if ((so->so_state & (SS_ISDISCONNECTING | SS_ISDISCONNECTED)) ||
  326. (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
  327. nbp->nbp_state = NBST_CLOSED;
  328. NBDEBUG("session closed by peer\n");
  329. return ECONNRESET;
  330. }
  331. if (error)
  332. return error;
  333. if (len == 0 && nbp->nbp_state != NBST_SESSION)
  334. break;
  335. /* no data, try again */
  336. if (rpcode == NB_SSN_KEEPALIVE)
  337. continue;
  338. /*
  339. * Loop, blocking, for data following the response header.
  340. *
  341. * Note that we can't simply block here with MSG_WAITALL for the
  342. * entire response size, as it may be larger than the TCP
  343. * slow-start window that the sender employs. This will result
  344. * in the sender stalling until the delayed ACK is sent, then
  345. * resuming slow-start, resulting in very poor performance.
  346. *
  347. * Instead, we never request more than NB_SORECEIVE_CHUNK
  348. * bytes at a time, resulting in an ack being pushed by
  349. * the TCP code at the completion of each call.
  350. */
  351. resid = len;
  352. while (resid > 0) {
  353. tm = NULL;
  354. rcvflg = MSG_WAITALL;
  355. bzero(&auio, sizeof(auio));
  356. auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
  357. auio.uio_td = td;
  358. resid -= auio.uio_resid;
  359. /*
  360. * Spin until we have collected everything in
  361. * this chunk.
  362. */
  363. do {
  364. rcvflg = MSG_WAITALL;
  365. CURVNET_SET(so->so_vnet);
  366. error = soreceive(so, (struct sockaddr **)NULL,
  367. &auio, &tm, (struct mbuf **)NULL, &rcvflg);
  368. CURVNET_RESTORE();
  369. } while (error == EWOULDBLOCK || error == EINTR ||
  370. error == ERESTART);
  371. if (error)
  372. goto out;
  373. /* short return guarantees unhappiness */
  374. if (auio.uio_resid > 0) {
  375. SMBERROR("packet is shorter than expected\n");
  376. error = EPIPE;
  377. goto out;
  378. }
  379. /* append received chunk to previous chunk(s) */
  380. if (m == NULL) {
  381. m = tm;
  382. } else {
  383. /*
  384. * Just glue the new chain on the end.
  385. * Consumer will pullup as required.
  386. */
  387. for (im = m; im->m_next != NULL; im = im->m_next)
  388. ;
  389. im->m_next = tm;
  390. }
  391. }
  392. /* got a session/message packet? */
  393. if (nbp->nbp_state == NBST_SESSION &&
  394. rpcode == NB_SSN_MESSAGE)
  395. break;
  396. /* drop packet and try for another */
  397. NBDEBUG("non-session packet %x\n", rpcode);
  398. if (m) {
  399. m_freem(m);
  400. m = NULL;
  401. }
  402. }
  403. out:
  404. if (error) {
  405. if (m)
  406. m_freem(m);
  407. return error;
  408. }
  409. if (mpp)
  410. *mpp = m;
  411. else
  412. m_freem(m);
  413. *lenp = len;
  414. *rpcodep = rpcode;
  415. return 0;
  416. }
  417. /*
  418. * SMB transport interface
  419. */
  420. static int
  421. smb_nbst_create(struct smb_vc *vcp, struct thread *td)
  422. {
  423. struct nbpcb *nbp;
  424. nbp = malloc(sizeof *nbp, M_NBDATA, M_WAITOK);
  425. bzero(nbp, sizeof *nbp);
  426. nbp->nbp_timo.tv_sec = 15; /* XXX: sysctl ? */
  427. nbp->nbp_state = NBST_CLOSED;
  428. nbp->nbp_vc = vcp;
  429. nbp->nbp_sndbuf = smb_tcpsndbuf;
  430. nbp->nbp_rcvbuf = smb_tcprcvbuf;
  431. vcp->vc_tdata = nbp;
  432. return 0;
  433. }
  434. static int
  435. smb_nbst_done(struct smb_vc *vcp, struct thread *td)
  436. {
  437. struct nbpcb *nbp = vcp->vc_tdata;
  438. if (nbp == NULL)
  439. return ENOTCONN;
  440. smb_nbst_disconnect(vcp, td);
  441. if (nbp->nbp_laddr)
  442. free(nbp->nbp_laddr, M_SONAME);
  443. if (nbp->nbp_paddr)
  444. free(nbp->nbp_paddr, M_SONAME);
  445. free(nbp, M_NBDATA);
  446. return 0;
  447. }
  448. static int
  449. smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
  450. {
  451. struct nbpcb *nbp = vcp->vc_tdata;
  452. struct sockaddr_nb *snb;
  453. int error, slen;
  454. NBDEBUG("\n");
  455. error = EINVAL;
  456. do {
  457. if (nbp->nbp_flags & NBF_LOCADDR)
  458. break;
  459. /*
  460. * It is possible to create NETBIOS name in the kernel,
  461. * but nothing prevents us to do it in the user space.
  462. */
  463. if (sap == NULL)
  464. break;
  465. slen = sap->sa_len;
  466. if (slen < NB_MINSALEN)
  467. break;
  468. snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
  469. if (snb == NULL) {
  470. error = ENOMEM;
  471. break;
  472. }
  473. nbp->nbp_laddr = snb;
  474. nbp->nbp_flags |= NBF_LOCADDR;
  475. error = 0;
  476. } while(0);
  477. return error;
  478. }
  479. static int
  480. smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
  481. {
  482. struct nbpcb *nbp = vcp->vc_tdata;
  483. struct sockaddr_in sin;
  484. struct sockaddr_nb *snb;
  485. struct timespec ts1, ts2;
  486. int error, slen;
  487. NBDEBUG("\n");
  488. if (nbp->nbp_tso != NULL)
  489. return EISCONN;
  490. if (nbp->nbp_laddr == NULL)
  491. return EINVAL;
  492. slen = sap->sa_len;
  493. if (slen < NB_MINSALEN)
  494. return EINVAL;
  495. if (nbp->nbp_paddr) {
  496. free(nbp->nbp_paddr, M_SONAME);
  497. nbp->nbp_paddr = NULL;
  498. }
  499. snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
  500. if (snb == NULL)
  501. return ENOMEM;
  502. nbp->nbp_paddr = snb;
  503. sin = snb->snb_addrin;
  504. getnanotime(&ts1);
  505. error = nb_connect_in(nbp, &sin, td);
  506. if (error)
  507. return error;
  508. getnanotime(&ts2);
  509. timespecsub(&ts2, &ts1, &ts2);
  510. if (ts2.tv_sec == 0) {
  511. ts2.tv_sec = 1;
  512. ts2.tv_nsec = 0;
  513. }
  514. timespecadd(&ts2, &ts2, &nbp->nbp_timo);
  515. timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
  516. timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo); /* * 4 */
  517. error = nbssn_rq_request(nbp, td);
  518. if (error)
  519. smb_nbst_disconnect(vcp, td);
  520. return error;
  521. }
  522. static int
  523. smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td)
  524. {
  525. struct nbpcb *nbp = vcp->vc_tdata;
  526. struct socket *so;
  527. if (nbp == NULL || nbp->nbp_tso == NULL)
  528. return ENOTCONN;
  529. if ((so = nbp->nbp_tso) != NULL) {
  530. nbp->nbp_flags &= ~NBF_CONNECTED;
  531. nbp->nbp_tso = (struct socket *)NULL;
  532. soshutdown(so, 2);
  533. soclose(so);
  534. }
  535. if (nbp->nbp_state != NBST_RETARGET) {
  536. nbp->nbp_state = NBST_CLOSED;
  537. }
  538. return 0;
  539. }
  540. static int
  541. smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct thread *td)
  542. {
  543. struct nbpcb *nbp = vcp->vc_tdata;
  544. int error;
  545. if (nbp->nbp_state != NBST_SESSION) {
  546. error = ENOTCONN;
  547. goto abort;
  548. }
  549. M_PREPEND(m0, 4, M_WAITOK);
  550. nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
  551. error = nb_sosend(nbp->nbp_tso, m0, 0, td);
  552. return error;
  553. abort:
  554. if (m0)
  555. m_freem(m0);
  556. return error;
  557. }
  558. static int
  559. smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td)
  560. {
  561. struct nbpcb *nbp = vcp->vc_tdata;
  562. u_int8_t rpcode;
  563. int error, rplen;
  564. nbp->nbp_flags |= NBF_RECVLOCK;
  565. error = nbssn_recv(nbp, mpp, &rplen, &rpcode, td);
  566. nbp->nbp_flags &= ~NBF_RECVLOCK;
  567. return error;
  568. }
  569. static void
  570. smb_nbst_timo(struct smb_vc *vcp)
  571. {
  572. return;
  573. }
  574. static void
  575. smb_nbst_intr(struct smb_vc *vcp)
  576. {
  577. struct nbpcb *nbp = vcp->vc_tdata;
  578. if (nbp == NULL || nbp->nbp_tso == NULL)
  579. return;
  580. sorwakeup(nbp->nbp_tso);
  581. sowwakeup(nbp->nbp_tso);
  582. }
  583. static int
  584. smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
  585. {
  586. struct nbpcb *nbp = vcp->vc_tdata;
  587. switch (param) {
  588. case SMBTP_SNDSZ:
  589. *(int*)data = nbp->nbp_sndbuf;
  590. break;
  591. case SMBTP_RCVSZ:
  592. *(int*)data = nbp->nbp_rcvbuf;
  593. break;
  594. case SMBTP_TIMEOUT:
  595. *(struct timespec*)data = nbp->nbp_timo;
  596. break;
  597. default:
  598. return EINVAL;
  599. }
  600. return 0;
  601. }
  602. static int
  603. smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
  604. {
  605. struct nbpcb *nbp = vcp->vc_tdata;
  606. switch (param) {
  607. case SMBTP_SELECTID:
  608. nbp->nbp_selectid = data;
  609. break;
  610. default:
  611. return EINVAL;
  612. }
  613. return 0;
  614. }
  615. /*
  616. * Check for fatal errors
  617. */
  618. static int
  619. smb_nbst_fatal(struct smb_vc *vcp, int error)
  620. {
  621. switch (error) {
  622. case ENOTCONN:
  623. case ENETRESET:
  624. case ECONNABORTED:
  625. return 1;
  626. }
  627. return 0;
  628. }
  629. struct smb_tran_desc smb_tran_nbtcp_desc = {
  630. SMBT_NBTCP,
  631. smb_nbst_create, smb_nbst_done,
  632. smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
  633. smb_nbst_send, smb_nbst_recv,
  634. smb_nbst_timo, smb_nbst_intr,
  635. smb_nbst_getparam, smb_nbst_setparam,
  636. smb_nbst_fatal
  637. };