2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/malloc.h>
38 #include <sys/unistd.h>
40 #include <netsmb/smb.h>
41 #include <netsmb/smb_conn.h>
42 #include <netsmb/smb_rq.h>
43 #include <netsmb/smb_tran.h>
44 #include <netsmb/smb_trantcp.h>
47 #define SMBIOD_SLEEP_TIMO 2
48 #define SMBIOD_PING_TIMO 60 /* seconds */
50 #define SMB_IOD_EVLOCKPTR(iod) (&((iod)->iod_evlock))
51 #define SMB_IOD_EVLOCK(iod) smb_sl_lock(&((iod)->iod_evlock))
52 #define SMB_IOD_EVUNLOCK(iod) smb_sl_unlock(&((iod)->iod_evlock))
54 #define SMB_IOD_RQLOCKPTR(iod) (&((iod)->iod_rqlock))
55 #define SMB_IOD_RQLOCK(iod) smb_sl_lock(&((iod)->iod_rqlock))
56 #define SMB_IOD_RQUNLOCK(iod) smb_sl_unlock(&((iod)->iod_rqlock))
58 #define smb_iod_wakeup(iod) wakeup(&(iod)->iod_flags)
61 static MALLOC_DEFINE(M_SMBIOD, "SMBIOD", "SMB network io daemon");
63 static int smb_iod_next;
65 static int smb_iod_sendall(struct smbiod *iod);
66 static int smb_iod_disconnect(struct smbiod *iod);
67 static void smb_iod_thread(void *);
70 smb_iod_rqprocessed(struct smb_rq *rqp, int error)
73 rqp->sr_lerror = error;
75 rqp->sr_state = SMBRQ_NOTIFIED;
76 wakeup(&rqp->sr_state);
81 smb_iod_invrq(struct smbiod *iod)
86 * Invalidate all outstanding requests for this connection
89 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
90 rqp->sr_flags |= SMBR_RESTART;
91 smb_iod_rqprocessed(rqp, ENOTCONN);
93 SMB_IOD_RQUNLOCK(iod);
97 smb_iod_closetran(struct smbiod *iod)
99 struct smb_vc *vcp = iod->iod_vc;
100 struct thread *td = iod->iod_td;
102 if (vcp->vc_tdata == NULL)
104 SMB_TRAN_DISCONNECT(vcp, td);
105 SMB_TRAN_DONE(vcp, td);
106 vcp->vc_tdata = NULL;
110 smb_iod_dead(struct smbiod *iod)
112 iod->iod_state = SMBIOD_ST_DEAD;
113 smb_iod_closetran(iod);
118 smb_iod_connect(struct smbiod *iod)
120 struct smb_vc *vcp = iod->iod_vc;
121 struct thread *td = iod->iod_td;
124 SMBIODEBUG("%d\n", iod->iod_state);
125 switch(iod->iod_state) {
126 case SMBIOD_ST_VCACTIVE:
127 SMBERROR("called for already opened connection\n");
130 return ENOTCONN; /* XXX: last error code ? */
137 error = (int)SMB_TRAN_CREATE(vcp, td);
140 SMBIODEBUG("tcreate\n");
142 error = (int)SMB_TRAN_BIND(vcp, vcp->vc_laddr, td);
146 SMBIODEBUG("tbind\n");
147 error = (int)SMB_TRAN_CONNECT(vcp, vcp->vc_paddr, td);
150 SMB_TRAN_SETPARAM(vcp, SMBTP_SELECTID, &iod->iod_flags);
151 iod->iod_state = SMBIOD_ST_TRANACTIVE;
152 SMBIODEBUG("tconnect\n");
153 /* vcp->vc_mid = 0;*/
154 error = (int)smb_smb_negotiate(vcp, &iod->iod_scred);
157 SMBIODEBUG("snegotiate\n");
158 error = (int)smb_smb_ssnsetup(vcp, &iod->iod_scred);
161 iod->iod_state = SMBIOD_ST_VCACTIVE;
162 SMBIODEBUG("completed\n");
172 smb_iod_disconnect(struct smbiod *iod)
174 struct smb_vc *vcp = iod->iod_vc;
177 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
178 smb_smb_ssnclose(vcp, &iod->iod_scred);
179 iod->iod_state = SMBIOD_ST_TRANACTIVE;
181 vcp->vc_smbuid = SMB_UID_UNKNOWN;
182 smb_iod_closetran(iod);
183 iod->iod_state = SMBIOD_ST_NOTCONN;
188 smb_iod_treeconnect(struct smbiod *iod, struct smb_share *ssp)
192 if (iod->iod_state != SMBIOD_ST_VCACTIVE) {
193 if (iod->iod_state != SMBIOD_ST_DEAD)
195 iod->iod_state = SMBIOD_ST_RECONNECT;
196 error = smb_iod_connect(iod);
200 SMBIODEBUG("tree reconnect\n");
202 ssp->ss_flags |= SMBS_RECONNECTING;
204 error = smb_smb_treeconnect(ssp, &iod->iod_scred);
206 ssp->ss_flags &= ~SMBS_RECONNECTING;
208 wakeup(&ssp->ss_vcgenid);
213 smb_iod_sendrq(struct smbiod *iod, struct smb_rq *rqp)
215 struct thread *td = iod->iod_td;
216 struct smb_vc *vcp = iod->iod_vc;
217 struct smb_share *ssp = rqp->sr_share;
221 SMBIODEBUG("iod_state = %d\n", iod->iod_state);
222 switch (iod->iod_state) {
223 case SMBIOD_ST_NOTCONN:
224 smb_iod_rqprocessed(rqp, ENOTCONN);
227 iod->iod_state = SMBIOD_ST_RECONNECT;
229 case SMBIOD_ST_RECONNECT:
234 if (rqp->sr_sendcnt == 0) {
235 #ifdef movedtoanotherplace
236 if (vcp->vc_maxmux != 0 && iod->iod_muxcnt >= vcp->vc_maxmux)
239 le16enc(rqp->sr_rqtid, ssp ? ssp->ss_tid : SMB_TID_UNKNOWN);
240 le16enc(rqp->sr_rquid, vcp ? vcp->vc_smbuid : 0);
241 mb_fixhdr(&rqp->sr_rq);
242 if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
245 if (rqp->sr_sendcnt++ > 5) {
246 rqp->sr_flags |= SMBR_RESTART;
247 smb_iod_rqprocessed(rqp, rqp->sr_lerror);
249 * If all attempts to send a request failed, then
250 * something is seriously hosed.
254 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x\n", rqp->sr_mid, 0, 0, 0);
255 m_dumpm(rqp->sr_rq.mb_top);
256 m = m_copym(rqp->sr_rq.mb_top, 0, M_COPYALL, M_WAITOK);
257 error = rqp->sr_lerror = SMB_TRAN_SEND(vcp, m, td);
259 getnanotime(&rqp->sr_timesent);
260 iod->iod_lastrqsent = rqp->sr_timesent;
261 rqp->sr_flags |= SMBR_SENT;
262 rqp->sr_state = SMBRQ_SENT;
266 * Check for fatal errors
268 if (SMB_TRAN_FATAL(vcp, error)) {
270 * No further attempts should be made
274 if (smb_rq_intr(rqp))
275 smb_iod_rqprocessed(rqp, EINTR);
280 * Process incoming packets
283 smb_iod_recvall(struct smbiod *iod)
285 struct smb_vc *vcp = iod->iod_vc;
286 struct thread *td = iod->iod_td;
293 switch (iod->iod_state) {
294 case SMBIOD_ST_NOTCONN:
296 case SMBIOD_ST_RECONNECT:
303 error = SMB_TRAN_RECV(vcp, &m, td);
304 if (error == EWOULDBLOCK)
306 if (SMB_TRAN_FATAL(vcp, error)) {
313 SMBERROR("tran return NULL without error\n");
317 m = m_pullup(m, SMB_HDRLEN);
319 continue; /* wait for a good packet */
321 * Now we got an entire and possibly invalid SMB packet.
322 * Be careful while parsing it.
325 hp = mtod(m, u_char*);
326 if (bcmp(hp, SMB_SIGNATURE, SMB_SIGLEN) != 0) {
330 mid = SMB_HDRMID(hp);
331 SMBSDEBUG("mid %04x\n", (u_int)mid);
333 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
334 if (rqp->sr_mid != mid)
337 if (rqp->sr_rp.md_top == NULL) {
338 md_initm(&rqp->sr_rp, m);
340 if (rqp->sr_flags & SMBR_MULTIPACKET) {
341 md_append_record(&rqp->sr_rp, m);
344 SMBERROR("duplicate response %d (ignored)\n", mid);
349 smb_iod_rqprocessed(rqp, 0);
352 SMB_IOD_RQUNLOCK(iod);
354 SMBERROR("drop resp with mid %d\n", (u_int)mid);
355 /* smb_printrqlist(vcp);*/
360 * check for interrupts
363 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
364 if (smb_td_intr(rqp->sr_cred->scr_td)) {
365 smb_iod_rqprocessed(rqp, EINTR);
368 SMB_IOD_RQUNLOCK(iod);
373 smb_iod_request(struct smbiod *iod, int event, void *ident)
375 struct smbiod_event *evp;
379 evp = smb_zmalloc(sizeof(*evp), M_SMBIOD, M_WAITOK);
380 evp->ev_type = event;
381 evp->ev_ident = ident;
383 STAILQ_INSERT_TAIL(&iod->iod_evlist, evp, ev_link);
384 if ((event & SMBIOD_EV_SYNC) == 0) {
385 SMB_IOD_EVUNLOCK(iod);
390 msleep(evp, SMB_IOD_EVLOCKPTR(iod), PWAIT | PDROP, "90evw", 0);
391 error = evp->ev_error;
397 * Place request in the queue.
398 * Request from smbiod have a high priority.
401 smb_iod_addrq(struct smb_rq *rqp)
403 struct smb_vc *vcp = rqp->sr_vc;
404 struct smbiod *iod = vcp->vc_iod;
408 if (rqp->sr_cred->scr_td != NULL &&
409 rqp->sr_cred->scr_td->td_proc == iod->iod_p) {
410 rqp->sr_flags |= SMBR_INTERNAL;
412 TAILQ_INSERT_HEAD(&iod->iod_rqlist, rqp, sr_link);
413 SMB_IOD_RQUNLOCK(iod);
415 if (smb_iod_sendrq(iod, rqp) != 0) {
420 * we don't need to lock state field here
422 if (rqp->sr_state != SMBRQ_NOTSENT)
424 tsleep(&iod->iod_flags, PWAIT, "90sndw", hz);
427 smb_iod_removerq(rqp);
428 return rqp->sr_lerror;
431 switch (iod->iod_state) {
432 case SMBIOD_ST_NOTCONN:
435 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
445 if (vcp->vc_maxmux == 0) {
446 SMBERROR("maxmux == 0\n");
449 if (iod->iod_muxcnt < vcp->vc_maxmux)
452 msleep(&iod->iod_muxwant, SMB_IOD_RQLOCKPTR(iod),
456 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
457 SMB_IOD_RQUNLOCK(iod);
463 smb_iod_removerq(struct smb_rq *rqp)
465 struct smb_vc *vcp = rqp->sr_vc;
466 struct smbiod *iod = vcp->vc_iod;
469 if (rqp->sr_flags & SMBR_INTERNAL) {
471 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
472 SMB_IOD_RQUNLOCK(iod);
476 while (rqp->sr_flags & SMBR_XLOCK) {
477 rqp->sr_flags |= SMBR_XLOCKWANT;
478 msleep(rqp, SMB_IOD_RQLOCKPTR(iod), PWAIT, "90xrm", 0);
480 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
482 if (iod->iod_muxwant) {
484 wakeup(&iod->iod_muxwant);
486 SMB_IOD_RQUNLOCK(iod);
491 smb_iod_waitrq(struct smb_rq *rqp)
493 struct smbiod *iod = rqp->sr_vc->vc_iod;
497 if (rqp->sr_flags & SMBR_INTERNAL) {
499 smb_iod_sendall(iod);
500 smb_iod_recvall(iod);
501 if (rqp->sr_rpgen != rqp->sr_rplast)
503 tsleep(&iod->iod_flags, PWAIT, "90irq", hz);
505 smb_iod_removerq(rqp);
506 return rqp->sr_lerror;
510 if (rqp->sr_rpgen == rqp->sr_rplast)
511 msleep(&rqp->sr_state, SMBRQ_SLOCKPTR(rqp), PWAIT, "90wrq", 0);
514 error = rqp->sr_lerror;
515 if (rqp->sr_flags & SMBR_MULTIPACKET) {
517 * If request should stay in the list, then reinsert it
518 * at the end of queue so other waiters have chance to concur
521 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
522 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
523 SMB_IOD_RQUNLOCK(iod);
525 smb_iod_removerq(rqp);
531 smb_iod_sendall(struct smbiod *iod)
533 struct smb_vc *vcp = iod->iod_vc;
535 struct timespec ts, tstimeout;
540 * Loop through the list of requests and send them if possible
543 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
544 switch (rqp->sr_state) {
546 rqp->sr_flags |= SMBR_XLOCK;
547 SMB_IOD_RQUNLOCK(iod);
548 herror = smb_iod_sendrq(iod, rqp);
550 rqp->sr_flags &= ~SMBR_XLOCK;
551 if (rqp->sr_flags & SMBR_XLOCKWANT) {
552 rqp->sr_flags &= ~SMBR_XLOCKWANT;
557 SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
558 timespecadd(&tstimeout, &tstimeout);
560 timespecsub(&ts, &tstimeout);
561 if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
562 smb_iod_rqprocessed(rqp, ETIMEDOUT);
571 SMB_IOD_RQUNLOCK(iod);
572 if (herror == ENOTCONN)
578 * "main" function for smbiod daemon
581 smb_iod_main(struct smbiod *iod)
583 /* struct smb_vc *vcp = iod->iod_vc;*/
584 struct smbiod_event *evp;
585 /* struct timespec tsnow;*/
592 * Check all interesting events
596 evp = STAILQ_FIRST(&iod->iod_evlist);
598 SMB_IOD_EVUNLOCK(iod);
601 STAILQ_REMOVE_HEAD(&iod->iod_evlist, ev_link);
602 evp->ev_type |= SMBIOD_EV_PROCESSING;
603 SMB_IOD_EVUNLOCK(iod);
604 switch (evp->ev_type & SMBIOD_EV_MASK) {
605 case SMBIOD_EV_CONNECT:
606 iod->iod_state = SMBIOD_ST_RECONNECT;
607 evp->ev_error = smb_iod_connect(iod);
609 case SMBIOD_EV_DISCONNECT:
610 evp->ev_error = smb_iod_disconnect(iod);
612 case SMBIOD_EV_TREECONNECT:
613 evp->ev_error = smb_iod_treeconnect(iod, evp->ev_ident);
615 case SMBIOD_EV_SHUTDOWN:
616 iod->iod_flags |= SMBIOD_SHUTDOWN;
618 case SMBIOD_EV_NEWRQ:
621 if (evp->ev_type & SMBIOD_EV_SYNC) {
624 SMB_IOD_EVUNLOCK(iod);
629 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
631 timespecsub(&tsnow, &iod->iod_pingtimo);
632 if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
633 smb_smb_echo(vcp, &iod->iod_scred);
637 smb_iod_sendall(iod);
638 smb_iod_recvall(iod);
643 smb_iod_thread(void *arg)
645 struct smbiod *iod = arg;
650 * Here we assume that the thread structure will be the same
651 * for an entire kthread (kproc, to be more precise) life.
653 iod->iod_td = curthread;
654 smb_makescred(&iod->iod_scred, iod->iod_td, NULL);
655 while ((iod->iod_flags & SMBIOD_SHUTDOWN) == 0) {
657 SMBIODEBUG("going to sleep for %d ticks\n", iod->iod_sleeptimo);
658 if (iod->iod_flags & SMBIOD_SHUTDOWN)
660 tsleep(&iod->iod_flags, PWAIT, "90idle", iod->iod_sleeptimo);
663 /* We can now safely destroy the mutexes and free the iod structure. */
664 smb_sl_destroy(&iod->iod_rqlock);
665 smb_sl_destroy(&iod->iod_evlock);
672 smb_iod_create(struct smb_vc *vcp)
677 iod = smb_zmalloc(sizeof(*iod), M_SMBIOD, M_WAITOK);
678 iod->iod_id = smb_iod_next++;
679 iod->iod_state = SMBIOD_ST_NOTCONN;
681 iod->iod_sleeptimo = hz * SMBIOD_SLEEP_TIMO;
682 iod->iod_pingtimo.tv_sec = SMBIOD_PING_TIMO;
683 getnanotime(&iod->iod_lastrqsent);
685 smb_sl_init(&iod->iod_rqlock, "90rql");
686 TAILQ_INIT(&iod->iod_rqlist);
687 smb_sl_init(&iod->iod_evlock, "90evl");
688 STAILQ_INIT(&iod->iod_evlist);
689 error = kproc_create(smb_iod_thread, iod, &iod->iod_p,
690 RFNOWAIT, 0, "smbiod%d", iod->iod_id);
692 SMBERROR("can't start smbiod: %d", error);
694 smb_sl_destroy(&iod->iod_rqlock);
695 smb_sl_destroy(&iod->iod_evlock);
703 smb_iod_destroy(struct smbiod *iod)
705 smb_iod_request(iod, SMBIOD_EV_SHUTDOWN | SMBIOD_EV_SYNC, NULL);