2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/malloc.h>
40 #include <sys/unistd.h>
42 #include <netsmb/smb.h>
43 #include <netsmb/smb_conn.h>
44 #include <netsmb/smb_rq.h>
45 #include <netsmb/smb_tran.h>
46 #include <netsmb/smb_trantcp.h>
49 #define SMBIOD_SLEEP_TIMO 2
50 #define SMBIOD_PING_TIMO 60 /* seconds */
52 #define SMB_IOD_EVLOCKPTR(iod) (&((iod)->iod_evlock))
53 #define SMB_IOD_EVLOCK(iod) smb_sl_lock(&((iod)->iod_evlock))
54 #define SMB_IOD_EVUNLOCK(iod) smb_sl_unlock(&((iod)->iod_evlock))
56 #define SMB_IOD_RQLOCKPTR(iod) (&((iod)->iod_rqlock))
57 #define SMB_IOD_RQLOCK(iod) smb_sl_lock(&((iod)->iod_rqlock))
58 #define SMB_IOD_RQUNLOCK(iod) smb_sl_unlock(&((iod)->iod_rqlock))
60 #define smb_iod_wakeup(iod) wakeup(&(iod)->iod_flags)
63 static MALLOC_DEFINE(M_SMBIOD, "SMBIOD", "SMB network io daemon");
65 static int smb_iod_next;
67 static int smb_iod_sendall(struct smbiod *iod);
68 static int smb_iod_disconnect(struct smbiod *iod);
69 static void smb_iod_thread(void *);
72 smb_iod_rqprocessed(struct smb_rq *rqp, int error)
75 rqp->sr_lerror = error;
77 rqp->sr_state = SMBRQ_NOTIFIED;
78 wakeup(&rqp->sr_state);
83 smb_iod_invrq(struct smbiod *iod)
88 * Invalidate all outstanding requests for this connection
91 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
92 rqp->sr_flags |= SMBR_RESTART;
93 smb_iod_rqprocessed(rqp, ENOTCONN);
95 SMB_IOD_RQUNLOCK(iod);
99 smb_iod_closetran(struct smbiod *iod)
101 struct smb_vc *vcp = iod->iod_vc;
102 struct thread *td = iod->iod_td;
104 if (vcp->vc_tdata == NULL)
106 SMB_TRAN_DISCONNECT(vcp, td);
107 SMB_TRAN_DONE(vcp, td);
108 vcp->vc_tdata = NULL;
112 smb_iod_dead(struct smbiod *iod)
114 iod->iod_state = SMBIOD_ST_DEAD;
115 smb_iod_closetran(iod);
120 smb_iod_connect(struct smbiod *iod)
122 struct smb_vc *vcp = iod->iod_vc;
123 struct thread *td = iod->iod_td;
126 SMBIODEBUG("%d\n", iod->iod_state);
127 switch(iod->iod_state) {
128 case SMBIOD_ST_VCACTIVE:
129 SMBERROR("called for already opened connection\n");
132 return ENOTCONN; /* XXX: last error code ? */
139 error = (int)SMB_TRAN_CREATE(vcp, td);
142 SMBIODEBUG("tcreate\n");
144 error = (int)SMB_TRAN_BIND(vcp, vcp->vc_laddr, td);
148 SMBIODEBUG("tbind\n");
149 error = (int)SMB_TRAN_CONNECT(vcp, vcp->vc_paddr, td);
152 SMB_TRAN_SETPARAM(vcp, SMBTP_SELECTID, &iod->iod_flags);
153 iod->iod_state = SMBIOD_ST_TRANACTIVE;
154 SMBIODEBUG("tconnect\n");
155 /* vcp->vc_mid = 0;*/
156 error = (int)smb_smb_negotiate(vcp, &iod->iod_scred);
159 SMBIODEBUG("snegotiate\n");
160 error = (int)smb_smb_ssnsetup(vcp, &iod->iod_scred);
163 iod->iod_state = SMBIOD_ST_VCACTIVE;
164 SMBIODEBUG("completed\n");
174 smb_iod_disconnect(struct smbiod *iod)
176 struct smb_vc *vcp = iod->iod_vc;
179 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
180 smb_smb_ssnclose(vcp, &iod->iod_scred);
181 iod->iod_state = SMBIOD_ST_TRANACTIVE;
183 vcp->vc_smbuid = SMB_UID_UNKNOWN;
184 smb_iod_closetran(iod);
185 iod->iod_state = SMBIOD_ST_NOTCONN;
190 smb_iod_treeconnect(struct smbiod *iod, struct smb_share *ssp)
194 if (iod->iod_state != SMBIOD_ST_VCACTIVE) {
195 if (iod->iod_state != SMBIOD_ST_DEAD)
197 iod->iod_state = SMBIOD_ST_RECONNECT;
198 error = smb_iod_connect(iod);
202 SMBIODEBUG("tree reconnect\n");
204 ssp->ss_flags |= SMBS_RECONNECTING;
206 error = smb_smb_treeconnect(ssp, &iod->iod_scred);
208 ssp->ss_flags &= ~SMBS_RECONNECTING;
210 wakeup(&ssp->ss_vcgenid);
215 smb_iod_sendrq(struct smbiod *iod, struct smb_rq *rqp)
217 struct thread *td = iod->iod_td;
218 struct smb_vc *vcp = iod->iod_vc;
219 struct smb_share *ssp = rqp->sr_share;
223 SMBIODEBUG("iod_state = %d\n", iod->iod_state);
224 switch (iod->iod_state) {
225 case SMBIOD_ST_NOTCONN:
226 smb_iod_rqprocessed(rqp, ENOTCONN);
229 iod->iod_state = SMBIOD_ST_RECONNECT;
231 case SMBIOD_ST_RECONNECT:
236 if (rqp->sr_sendcnt == 0) {
237 #ifdef movedtoanotherplace
238 if (vcp->vc_maxmux != 0 && iod->iod_muxcnt >= vcp->vc_maxmux)
241 le16enc(rqp->sr_rqtid, ssp ? ssp->ss_tid : SMB_TID_UNKNOWN);
242 le16enc(rqp->sr_rquid, vcp ? vcp->vc_smbuid : 0);
243 mb_fixhdr(&rqp->sr_rq);
244 if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
247 if (rqp->sr_sendcnt++ > 5) {
248 rqp->sr_flags |= SMBR_RESTART;
249 smb_iod_rqprocessed(rqp, rqp->sr_lerror);
251 * If all attempts to send a request failed, then
252 * something is seriously hosed.
256 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x\n", rqp->sr_mid, 0, 0, 0);
257 m_dumpm(rqp->sr_rq.mb_top);
258 m = m_copym(rqp->sr_rq.mb_top, 0, M_COPYALL, M_WAITOK);
259 error = rqp->sr_lerror = SMB_TRAN_SEND(vcp, m, td);
261 getnanotime(&rqp->sr_timesent);
262 iod->iod_lastrqsent = rqp->sr_timesent;
263 rqp->sr_flags |= SMBR_SENT;
264 rqp->sr_state = SMBRQ_SENT;
268 * Check for fatal errors
270 if (SMB_TRAN_FATAL(vcp, error)) {
272 * No further attempts should be made
276 if (smb_rq_intr(rqp))
277 smb_iod_rqprocessed(rqp, EINTR);
282 * Process incoming packets
285 smb_iod_recvall(struct smbiod *iod)
287 struct smb_vc *vcp = iod->iod_vc;
288 struct thread *td = iod->iod_td;
295 switch (iod->iod_state) {
296 case SMBIOD_ST_NOTCONN:
298 case SMBIOD_ST_RECONNECT:
305 error = SMB_TRAN_RECV(vcp, &m, td);
306 if (error == EWOULDBLOCK)
308 if (SMB_TRAN_FATAL(vcp, error)) {
315 SMBERROR("tran return NULL without error\n");
319 m = m_pullup(m, SMB_HDRLEN);
321 continue; /* wait for a good packet */
323 * Now we got an entire and possibly invalid SMB packet.
324 * Be careful while parsing it.
327 hp = mtod(m, u_char*);
328 if (bcmp(hp, SMB_SIGNATURE, SMB_SIGLEN) != 0) {
332 mid = SMB_HDRMID(hp);
333 SMBSDEBUG("mid %04x\n", (u_int)mid);
335 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
336 if (rqp->sr_mid != mid)
339 if (rqp->sr_rp.md_top == NULL) {
340 md_initm(&rqp->sr_rp, m);
342 if (rqp->sr_flags & SMBR_MULTIPACKET) {
343 md_append_record(&rqp->sr_rp, m);
346 SMBERROR("duplicate response %d (ignored)\n", mid);
351 smb_iod_rqprocessed(rqp, 0);
354 SMB_IOD_RQUNLOCK(iod);
356 SMBERROR("drop resp with mid %d\n", (u_int)mid);
357 /* smb_printrqlist(vcp);*/
362 * check for interrupts
365 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
366 if (smb_td_intr(rqp->sr_cred->scr_td)) {
367 smb_iod_rqprocessed(rqp, EINTR);
370 SMB_IOD_RQUNLOCK(iod);
375 smb_iod_request(struct smbiod *iod, int event, void *ident)
377 struct smbiod_event *evp;
381 evp = smb_zmalloc(sizeof(*evp), M_SMBIOD, M_WAITOK);
382 evp->ev_type = event;
383 evp->ev_ident = ident;
385 STAILQ_INSERT_TAIL(&iod->iod_evlist, evp, ev_link);
386 if ((event & SMBIOD_EV_SYNC) == 0) {
387 SMB_IOD_EVUNLOCK(iod);
392 msleep(evp, SMB_IOD_EVLOCKPTR(iod), PWAIT | PDROP, "90evw", 0);
393 error = evp->ev_error;
399 * Place request in the queue.
400 * Request from smbiod have a high priority.
403 smb_iod_addrq(struct smb_rq *rqp)
405 struct smb_vc *vcp = rqp->sr_vc;
406 struct smbiod *iod = vcp->vc_iod;
410 if (rqp->sr_cred->scr_td != NULL &&
411 rqp->sr_cred->scr_td->td_proc == iod->iod_p) {
412 rqp->sr_flags |= SMBR_INTERNAL;
414 TAILQ_INSERT_HEAD(&iod->iod_rqlist, rqp, sr_link);
415 SMB_IOD_RQUNLOCK(iod);
417 if (smb_iod_sendrq(iod, rqp) != 0) {
422 * we don't need to lock state field here
424 if (rqp->sr_state != SMBRQ_NOTSENT)
426 tsleep(&iod->iod_flags, PWAIT, "90sndw", hz);
429 smb_iod_removerq(rqp);
430 return rqp->sr_lerror;
433 switch (iod->iod_state) {
434 case SMBIOD_ST_NOTCONN:
437 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
447 if (vcp->vc_maxmux == 0) {
448 SMBERROR("maxmux == 0\n");
451 if (iod->iod_muxcnt < vcp->vc_maxmux)
454 msleep(&iod->iod_muxwant, SMB_IOD_RQLOCKPTR(iod),
458 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
459 SMB_IOD_RQUNLOCK(iod);
465 smb_iod_removerq(struct smb_rq *rqp)
467 struct smb_vc *vcp = rqp->sr_vc;
468 struct smbiod *iod = vcp->vc_iod;
471 if (rqp->sr_flags & SMBR_INTERNAL) {
473 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
474 SMB_IOD_RQUNLOCK(iod);
478 while (rqp->sr_flags & SMBR_XLOCK) {
479 rqp->sr_flags |= SMBR_XLOCKWANT;
480 msleep(rqp, SMB_IOD_RQLOCKPTR(iod), PWAIT, "90xrm", 0);
482 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
484 if (iod->iod_muxwant) {
486 wakeup(&iod->iod_muxwant);
488 SMB_IOD_RQUNLOCK(iod);
493 smb_iod_waitrq(struct smb_rq *rqp)
495 struct smbiod *iod = rqp->sr_vc->vc_iod;
499 if (rqp->sr_flags & SMBR_INTERNAL) {
501 smb_iod_sendall(iod);
502 smb_iod_recvall(iod);
503 if (rqp->sr_rpgen != rqp->sr_rplast)
505 tsleep(&iod->iod_flags, PWAIT, "90irq", hz);
507 smb_iod_removerq(rqp);
508 return rqp->sr_lerror;
512 if (rqp->sr_rpgen == rqp->sr_rplast)
513 msleep(&rqp->sr_state, SMBRQ_SLOCKPTR(rqp), PWAIT, "90wrq", 0);
516 error = rqp->sr_lerror;
517 if (rqp->sr_flags & SMBR_MULTIPACKET) {
519 * If request should stay in the list, then reinsert it
520 * at the end of queue so other waiters have chance to concur
523 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
524 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
525 SMB_IOD_RQUNLOCK(iod);
527 smb_iod_removerq(rqp);
533 smb_iod_sendall(struct smbiod *iod)
535 struct smb_vc *vcp = iod->iod_vc;
537 struct timespec ts, tstimeout;
542 * Loop through the list of requests and send them if possible
545 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
546 switch (rqp->sr_state) {
548 rqp->sr_flags |= SMBR_XLOCK;
549 SMB_IOD_RQUNLOCK(iod);
550 herror = smb_iod_sendrq(iod, rqp);
552 rqp->sr_flags &= ~SMBR_XLOCK;
553 if (rqp->sr_flags & SMBR_XLOCKWANT) {
554 rqp->sr_flags &= ~SMBR_XLOCKWANT;
559 SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
560 timespecadd(&tstimeout, &tstimeout, &tstimeout);
562 timespecsub(&ts, &tstimeout, &ts);
563 if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
564 smb_iod_rqprocessed(rqp, ETIMEDOUT);
573 SMB_IOD_RQUNLOCK(iod);
574 if (herror == ENOTCONN)
580 * "main" function for smbiod daemon
583 smb_iod_main(struct smbiod *iod)
585 /* struct smb_vc *vcp = iod->iod_vc;*/
586 struct smbiod_event *evp;
587 /* struct timespec tsnow;*/
594 * Check all interesting events
598 evp = STAILQ_FIRST(&iod->iod_evlist);
600 SMB_IOD_EVUNLOCK(iod);
603 STAILQ_REMOVE_HEAD(&iod->iod_evlist, ev_link);
604 evp->ev_type |= SMBIOD_EV_PROCESSING;
605 SMB_IOD_EVUNLOCK(iod);
606 switch (evp->ev_type & SMBIOD_EV_MASK) {
607 case SMBIOD_EV_CONNECT:
608 iod->iod_state = SMBIOD_ST_RECONNECT;
609 evp->ev_error = smb_iod_connect(iod);
611 case SMBIOD_EV_DISCONNECT:
612 evp->ev_error = smb_iod_disconnect(iod);
614 case SMBIOD_EV_TREECONNECT:
615 evp->ev_error = smb_iod_treeconnect(iod, evp->ev_ident);
617 case SMBIOD_EV_SHUTDOWN:
618 iod->iod_flags |= SMBIOD_SHUTDOWN;
620 case SMBIOD_EV_NEWRQ:
623 if (evp->ev_type & SMBIOD_EV_SYNC) {
626 SMB_IOD_EVUNLOCK(iod);
631 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
633 timespecsub(&tsnow, &iod->iod_pingtimo, &tsnow);
634 if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
635 smb_smb_echo(vcp, &iod->iod_scred);
639 smb_iod_sendall(iod);
640 smb_iod_recvall(iod);
645 smb_iod_thread(void *arg)
647 struct smbiod *iod = arg;
652 * Here we assume that the thread structure will be the same
653 * for an entire kthread (kproc, to be more precise) life.
655 iod->iod_td = curthread;
656 smb_makescred(&iod->iod_scred, iod->iod_td, NULL);
657 while ((iod->iod_flags & SMBIOD_SHUTDOWN) == 0) {
659 SMBIODEBUG("going to sleep for %d ticks\n", iod->iod_sleeptimo);
660 if (iod->iod_flags & SMBIOD_SHUTDOWN)
662 tsleep(&iod->iod_flags, PWAIT, "90idle", iod->iod_sleeptimo);
665 /* We can now safely destroy the mutexes and free the iod structure. */
666 smb_sl_destroy(&iod->iod_rqlock);
667 smb_sl_destroy(&iod->iod_evlock);
674 smb_iod_create(struct smb_vc *vcp)
679 iod = smb_zmalloc(sizeof(*iod), M_SMBIOD, M_WAITOK);
680 iod->iod_id = smb_iod_next++;
681 iod->iod_state = SMBIOD_ST_NOTCONN;
683 iod->iod_sleeptimo = hz * SMBIOD_SLEEP_TIMO;
684 iod->iod_pingtimo.tv_sec = SMBIOD_PING_TIMO;
685 getnanotime(&iod->iod_lastrqsent);
687 smb_sl_init(&iod->iod_rqlock, "90rql");
688 TAILQ_INIT(&iod->iod_rqlist);
689 smb_sl_init(&iod->iod_evlock, "90evl");
690 STAILQ_INIT(&iod->iod_evlist);
691 error = kproc_create(smb_iod_thread, iod, &iod->iod_p,
692 RFNOWAIT, 0, "smbiod%d", iod->iod_id);
694 SMBERROR("can't start smbiod: %d", error);
696 smb_sl_destroy(&iod->iod_rqlock);
697 smb_sl_destroy(&iod->iod_evlock);
705 smb_iod_destroy(struct smbiod *iod)
707 smb_iod_request(iod, SMBIOD_EV_SHUTDOWN | SMBIOD_EV_SYNC, NULL);