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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
44 #include <sys/unistd.h>
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_conn.h>
48 #include <netsmb/smb_rq.h>
49 #include <netsmb/smb_tran.h>
50 #include <netsmb/smb_trantcp.h>
53 #define SMBIOD_SLEEP_TIMO 2
54 #define SMBIOD_PING_TIMO 60 /* seconds */
56 #define SMB_IOD_EVLOCKPTR(iod) (&((iod)->iod_evlock))
57 #define SMB_IOD_EVLOCK(iod) smb_sl_lock(&((iod)->iod_evlock))
58 #define SMB_IOD_EVUNLOCK(iod) smb_sl_unlock(&((iod)->iod_evlock))
60 #define SMB_IOD_RQLOCKPTR(iod) (&((iod)->iod_rqlock))
61 #define SMB_IOD_RQLOCK(iod) smb_sl_lock(&((iod)->iod_rqlock))
62 #define SMB_IOD_RQUNLOCK(iod) smb_sl_unlock(&((iod)->iod_rqlock))
64 #define smb_iod_wakeup(iod) wakeup(&(iod)->iod_flags)
67 static MALLOC_DEFINE(M_SMBIOD, "SMBIOD", "SMB network io daemon");
69 static int smb_iod_next;
71 static int smb_iod_sendall(struct smbiod *iod);
72 static int smb_iod_disconnect(struct smbiod *iod);
73 static void smb_iod_thread(void *);
76 smb_iod_rqprocessed(struct smb_rq *rqp, int error)
79 rqp->sr_lerror = error;
81 rqp->sr_state = SMBRQ_NOTIFIED;
82 wakeup(&rqp->sr_state);
87 smb_iod_invrq(struct smbiod *iod)
92 * Invalidate all outstanding requests for this connection
95 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
96 if (rqp->sr_flags & SMBR_INTERNAL)
98 rqp->sr_flags |= SMBR_RESTART;
99 smb_iod_rqprocessed(rqp, ENOTCONN);
101 SMB_IOD_RQUNLOCK(iod);
105 smb_iod_closetran(struct smbiod *iod)
107 struct smb_vc *vcp = iod->iod_vc;
108 struct thread *td = iod->iod_td;
110 if (vcp->vc_tdata == NULL)
112 SMB_TRAN_DISCONNECT(vcp, td);
113 SMB_TRAN_DONE(vcp, td);
114 vcp->vc_tdata = NULL;
118 smb_iod_dead(struct smbiod *iod)
120 iod->iod_state = SMBIOD_ST_DEAD;
121 smb_iod_closetran(iod);
126 smb_iod_connect(struct smbiod *iod)
128 struct smb_vc *vcp = iod->iod_vc;
129 struct thread *td = iod->iod_td;
132 SMBIODEBUG("%d\n", iod->iod_state);
133 switch(iod->iod_state) {
134 case SMBIOD_ST_VCACTIVE:
135 SMBERROR("called for already opened connection\n");
138 return ENOTCONN; /* XXX: last error code ? */
145 error = (int)SMB_TRAN_CREATE(vcp, td);
148 SMBIODEBUG("tcreate\n");
150 error = (int)SMB_TRAN_BIND(vcp, vcp->vc_laddr, td);
154 SMBIODEBUG("tbind\n");
155 error = (int)SMB_TRAN_CONNECT(vcp, vcp->vc_paddr, td);
158 SMB_TRAN_SETPARAM(vcp, SMBTP_SELECTID, &iod->iod_flags);
159 iod->iod_state = SMBIOD_ST_TRANACTIVE;
160 SMBIODEBUG("tconnect\n");
161 /* vcp->vc_mid = 0;*/
162 error = (int)smb_smb_negotiate(vcp, &iod->iod_scred);
165 SMBIODEBUG("snegotiate\n");
166 error = (int)smb_smb_ssnsetup(vcp, &iod->iod_scred);
169 iod->iod_state = SMBIOD_ST_VCACTIVE;
170 SMBIODEBUG("completed\n");
180 smb_iod_disconnect(struct smbiod *iod)
182 struct smb_vc *vcp = iod->iod_vc;
185 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
186 smb_smb_ssnclose(vcp, &iod->iod_scred);
187 iod->iod_state = SMBIOD_ST_TRANACTIVE;
189 vcp->vc_smbuid = SMB_UID_UNKNOWN;
190 smb_iod_closetran(iod);
191 iod->iod_state = SMBIOD_ST_NOTCONN;
196 smb_iod_treeconnect(struct smbiod *iod, struct smb_share *ssp)
200 if (iod->iod_state != SMBIOD_ST_VCACTIVE) {
201 if (iod->iod_state != SMBIOD_ST_DEAD)
203 iod->iod_state = SMBIOD_ST_RECONNECT;
204 error = smb_iod_connect(iod);
208 SMBIODEBUG("tree reconnect\n");
210 ssp->ss_flags |= SMBS_RECONNECTING;
212 error = smb_smb_treeconnect(ssp, &iod->iod_scred);
214 ssp->ss_flags &= ~SMBS_RECONNECTING;
216 wakeup(&ssp->ss_vcgenid);
221 smb_iod_sendrq(struct smbiod *iod, struct smb_rq *rqp)
223 struct thread *td = iod->iod_td;
224 struct smb_vc *vcp = iod->iod_vc;
225 struct smb_share *ssp = rqp->sr_share;
229 SMBIODEBUG("iod_state = %d\n", iod->iod_state);
230 switch (iod->iod_state) {
231 case SMBIOD_ST_NOTCONN:
232 smb_iod_rqprocessed(rqp, ENOTCONN);
235 iod->iod_state = SMBIOD_ST_RECONNECT;
237 case SMBIOD_ST_RECONNECT:
242 if (rqp->sr_sendcnt == 0) {
243 #ifdef movedtoanotherplace
244 if (vcp->vc_maxmux != 0 && iod->iod_muxcnt >= vcp->vc_maxmux)
247 le16enc(rqp->sr_rqtid, ssp ? ssp->ss_tid : SMB_TID_UNKNOWN);
248 le16enc(rqp->sr_rquid, vcp ? vcp->vc_smbuid : 0);
249 mb_fixhdr(&rqp->sr_rq);
250 if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
253 if (rqp->sr_sendcnt++ > 5) {
254 rqp->sr_flags |= SMBR_RESTART;
255 smb_iod_rqprocessed(rqp, rqp->sr_lerror);
257 * If all attempts to send a request failed, then
258 * something is seriously hosed.
262 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x\n", rqp->sr_mid, 0, 0, 0);
263 m_dumpm(rqp->sr_rq.mb_top);
264 m = m_copym(rqp->sr_rq.mb_top, 0, M_COPYALL, M_WAIT);
265 error = rqp->sr_lerror = SMB_TRAN_SEND(vcp, m, td);
267 getnanotime(&rqp->sr_timesent);
268 iod->iod_lastrqsent = rqp->sr_timesent;
269 rqp->sr_flags |= SMBR_SENT;
270 rqp->sr_state = SMBRQ_SENT;
274 * Check for fatal errors
276 if (SMB_TRAN_FATAL(vcp, error)) {
278 * No further attempts should be made
282 if (smb_rq_intr(rqp))
283 smb_iod_rqprocessed(rqp, EINTR);
288 * Process incoming packets
291 smb_iod_recvall(struct smbiod *iod)
293 struct smb_vc *vcp = iod->iod_vc;
294 struct thread *td = iod->iod_td;
301 switch (iod->iod_state) {
302 case SMBIOD_ST_NOTCONN:
304 case SMBIOD_ST_RECONNECT:
311 error = SMB_TRAN_RECV(vcp, &m, td);
312 if (error == EWOULDBLOCK)
314 if (SMB_TRAN_FATAL(vcp, error)) {
321 SMBERROR("tran return NULL without error\n");
325 m = m_pullup(m, SMB_HDRLEN);
327 continue; /* wait for a good packet */
329 * Now we got an entire and possibly invalid SMB packet.
330 * Be careful while parsing it.
333 hp = mtod(m, u_char*);
334 if (bcmp(hp, SMB_SIGNATURE, SMB_SIGLEN) != 0) {
338 mid = SMB_HDRMID(hp);
339 SMBSDEBUG("mid %04x\n", (u_int)mid);
341 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
342 if (rqp->sr_mid != mid)
345 if (rqp->sr_rp.md_top == NULL) {
346 md_initm(&rqp->sr_rp, m);
348 if (rqp->sr_flags & SMBR_MULTIPACKET) {
349 md_append_record(&rqp->sr_rp, m);
352 SMBERROR("duplicate response %d (ignored)\n", mid);
357 smb_iod_rqprocessed(rqp, 0);
360 SMB_IOD_RQUNLOCK(iod);
362 SMBERROR("drop resp with mid %d\n", (u_int)mid);
363 /* smb_printrqlist(vcp);*/
368 * check for interrupts
371 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
372 if (smb_td_intr(rqp->sr_cred->scr_td)) {
373 smb_iod_rqprocessed(rqp, EINTR);
376 SMB_IOD_RQUNLOCK(iod);
381 smb_iod_request(struct smbiod *iod, int event, void *ident)
383 struct smbiod_event *evp;
387 evp = smb_zmalloc(sizeof(*evp), M_SMBIOD, M_WAITOK);
388 evp->ev_type = event;
389 evp->ev_ident = ident;
391 STAILQ_INSERT_TAIL(&iod->iod_evlist, evp, ev_link);
392 if ((event & SMBIOD_EV_SYNC) == 0) {
393 SMB_IOD_EVUNLOCK(iod);
398 msleep(evp, SMB_IOD_EVLOCKPTR(iod), PWAIT | PDROP, "90evw", 0);
399 error = evp->ev_error;
405 * Place request in the queue.
406 * Request from smbiod have a high priority.
409 smb_iod_addrq(struct smb_rq *rqp)
411 struct smb_vc *vcp = rqp->sr_vc;
412 struct smbiod *iod = vcp->vc_iod;
416 if (rqp->sr_cred->scr_td != NULL &&
417 rqp->sr_cred->scr_td->td_proc == iod->iod_p) {
418 rqp->sr_flags |= SMBR_INTERNAL;
420 TAILQ_INSERT_HEAD(&iod->iod_rqlist, rqp, sr_link);
421 SMB_IOD_RQUNLOCK(iod);
423 if (smb_iod_sendrq(iod, rqp) != 0) {
428 * we don't need to lock state field here
430 if (rqp->sr_state != SMBRQ_NOTSENT)
432 tsleep(&iod->iod_flags, PWAIT, "90sndw", hz);
435 smb_iod_removerq(rqp);
436 return rqp->sr_lerror;
439 switch (iod->iod_state) {
440 case SMBIOD_ST_NOTCONN:
443 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
453 if (vcp->vc_maxmux == 0) {
454 SMBERROR("maxmux == 0\n");
457 if (iod->iod_muxcnt < vcp->vc_maxmux)
460 msleep(&iod->iod_muxwant, SMB_IOD_RQLOCKPTR(iod),
464 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
465 SMB_IOD_RQUNLOCK(iod);
471 smb_iod_removerq(struct smb_rq *rqp)
473 struct smb_vc *vcp = rqp->sr_vc;
474 struct smbiod *iod = vcp->vc_iod;
477 if (rqp->sr_flags & SMBR_INTERNAL) {
479 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
480 SMB_IOD_RQUNLOCK(iod);
484 while (rqp->sr_flags & SMBR_XLOCK) {
485 rqp->sr_flags |= SMBR_XLOCKWANT;
486 msleep(rqp, SMB_IOD_RQLOCKPTR(iod), PWAIT, "90xrm", 0);
488 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
490 if (iod->iod_muxwant) {
492 wakeup(&iod->iod_muxwant);
494 SMB_IOD_RQUNLOCK(iod);
499 smb_iod_waitrq(struct smb_rq *rqp)
501 struct smbiod *iod = rqp->sr_vc->vc_iod;
505 if (rqp->sr_flags & SMBR_INTERNAL) {
507 smb_iod_sendall(iod);
508 smb_iod_recvall(iod);
509 if (rqp->sr_rpgen != rqp->sr_rplast)
511 tsleep(&iod->iod_flags, PWAIT, "90irq", hz);
513 smb_iod_removerq(rqp);
514 return rqp->sr_lerror;
518 if (rqp->sr_rpgen == rqp->sr_rplast)
519 msleep(&rqp->sr_state, SMBRQ_SLOCKPTR(rqp), PWAIT, "90wrq", 0);
522 error = rqp->sr_lerror;
523 if (rqp->sr_flags & SMBR_MULTIPACKET) {
525 * If request should stay in the list, then reinsert it
526 * at the end of queue so other waiters have chance to concur
529 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
530 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
531 SMB_IOD_RQUNLOCK(iod);
533 smb_iod_removerq(rqp);
539 smb_iod_sendall(struct smbiod *iod)
541 struct smb_vc *vcp = iod->iod_vc;
543 struct timespec ts, tstimeout;
548 * Loop through the list of requests and send them if possible
551 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
552 switch (rqp->sr_state) {
554 rqp->sr_flags |= SMBR_XLOCK;
555 SMB_IOD_RQUNLOCK(iod);
556 herror = smb_iod_sendrq(iod, rqp);
558 rqp->sr_flags &= ~SMBR_XLOCK;
559 if (rqp->sr_flags & SMBR_XLOCKWANT) {
560 rqp->sr_flags &= ~SMBR_XLOCKWANT;
565 SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
566 timespecadd(&tstimeout, &tstimeout);
568 timespecsub(&ts, &tstimeout);
569 if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
570 smb_iod_rqprocessed(rqp, ETIMEDOUT);
579 SMB_IOD_RQUNLOCK(iod);
580 if (herror == ENOTCONN)
586 * "main" function for smbiod daemon
589 smb_iod_main(struct smbiod *iod)
591 /* struct smb_vc *vcp = iod->iod_vc;*/
592 struct smbiod_event *evp;
593 /* struct timespec tsnow;*/
600 * Check all interesting events
604 evp = STAILQ_FIRST(&iod->iod_evlist);
606 SMB_IOD_EVUNLOCK(iod);
609 STAILQ_REMOVE_HEAD(&iod->iod_evlist, ev_link);
610 evp->ev_type |= SMBIOD_EV_PROCESSING;
611 SMB_IOD_EVUNLOCK(iod);
612 switch (evp->ev_type & SMBIOD_EV_MASK) {
613 case SMBIOD_EV_CONNECT:
614 iod->iod_state = SMBIOD_ST_RECONNECT;
615 evp->ev_error = smb_iod_connect(iod);
617 case SMBIOD_EV_DISCONNECT:
618 evp->ev_error = smb_iod_disconnect(iod);
620 case SMBIOD_EV_TREECONNECT:
621 evp->ev_error = smb_iod_treeconnect(iod, evp->ev_ident);
623 case SMBIOD_EV_SHUTDOWN:
624 iod->iod_flags |= SMBIOD_SHUTDOWN;
626 case SMBIOD_EV_NEWRQ:
629 if (evp->ev_type & SMBIOD_EV_SYNC) {
632 SMB_IOD_EVUNLOCK(iod);
637 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
639 timespecsub(&tsnow, &iod->iod_pingtimo);
640 if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
641 smb_smb_echo(vcp, &iod->iod_scred);
645 smb_iod_sendall(iod);
646 smb_iod_recvall(iod);
651 smb_iod_thread(void *arg)
653 struct smbiod *iod = arg;
658 * Here we assume that the thread structure will be the same
659 * for an entire kthread (kproc, to be more precise) life.
661 iod->iod_td = curthread;
662 smb_makescred(&iod->iod_scred, iod->iod_td, NULL);
663 while ((iod->iod_flags & SMBIOD_SHUTDOWN) == 0) {
665 SMBIODEBUG("going to sleep for %d ticks\n", iod->iod_sleeptimo);
666 if (iod->iod_flags & SMBIOD_SHUTDOWN)
668 tsleep(&iod->iod_flags, PWAIT, "90idle", iod->iod_sleeptimo);
675 smb_iod_create(struct smb_vc *vcp)
680 iod = smb_zmalloc(sizeof(*iod), M_SMBIOD, M_WAITOK);
681 iod->iod_id = smb_iod_next++;
682 iod->iod_state = SMBIOD_ST_NOTCONN;
684 iod->iod_sleeptimo = hz * SMBIOD_SLEEP_TIMO;
685 iod->iod_pingtimo.tv_sec = SMBIOD_PING_TIMO;
686 getnanotime(&iod->iod_lastrqsent);
688 smb_sl_init(&iod->iod_rqlock, "90rql");
689 TAILQ_INIT(&iod->iod_rqlist);
690 smb_sl_init(&iod->iod_evlock, "90evl");
691 STAILQ_INIT(&iod->iod_evlist);
692 error = kproc_create(smb_iod_thread, iod, &iod->iod_p,
693 RFNOWAIT, 0, "smbiod%d", iod->iod_id);
695 SMBERROR("can't start smbiod: %d", error);
703 smb_iod_destroy(struct smbiod *iod)
705 smb_iod_request(iod, SMBIOD_EV_SHUTDOWN | SMBIOD_EV_SYNC, NULL);
706 smb_sl_destroy(&iod->iod_rqlock);
707 smb_sl_destroy(&iod->iod_evlock);