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>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
43 #include <netsmb/smb.h>
44 #include <netsmb/smb_conn.h>
45 #include <netsmb/smb_rq.h>
46 #include <netsmb/smb_subr.h>
47 #include <netsmb/smb_tran.h>
49 static MALLOC_DEFINE(M_SMBRQ, "SMBRQ", "SMB request");
51 MODULE_DEPEND(netsmb, libmchain, 1, 1, 1);
53 static int smb_rq_reply(struct smb_rq *rqp);
54 static int smb_rq_enqueue(struct smb_rq *rqp);
55 static int smb_rq_getenv(struct smb_connobj *layer,
56 struct smb_vc **vcpp, struct smb_share **sspp);
57 static int smb_rq_new(struct smb_rq *rqp, u_char cmd);
58 static int smb_t2_reply(struct smb_t2rq *t2p);
61 smb_rq_alloc(struct smb_connobj *layer, u_char cmd, struct smb_cred *scred,
67 rqp = malloc(sizeof(*rqp), M_SMBRQ, M_WAITOK);
70 error = smb_rq_init(rqp, layer, cmd, scred);
71 rqp->sr_flags |= SMBR_ALLOCED;
80 static char tzero[12];
83 smb_rq_init(struct smb_rq *rqp, struct smb_connobj *layer, u_char cmd,
84 struct smb_cred *scred)
88 bzero(rqp, sizeof(*rqp));
89 smb_sl_init(&rqp->sr_slock, "srslock");
90 error = smb_rq_getenv(layer, &rqp->sr_vc, &rqp->sr_share);
93 error = smb_vc_access(rqp->sr_vc, scred, SMBM_EXEC);
97 error = smb_share_access(rqp->sr_share, scred, SMBM_EXEC);
101 rqp->sr_cred = scred;
102 rqp->sr_mid = smb_vc_nextmid(rqp->sr_vc);
103 return smb_rq_new(rqp, cmd);
107 smb_rq_new(struct smb_rq *rqp, u_char cmd)
109 struct smb_vc *vcp = rqp->sr_vc;
110 struct mbchain *mbp = &rqp->sr_rq;
116 md_done(&rqp->sr_rp);
117 error = mb_init(mbp);
120 mb_put_mem(mbp, SMB_SIGNATURE, SMB_SIGLEN, MB_MSYSTEM);
121 mb_put_uint8(mbp, cmd);
122 mb_put_uint32le(mbp, 0); /* DosError */
123 mb_put_uint8(mbp, vcp->vc_hflags);
124 flags2 = vcp->vc_hflags2;
125 if (cmd == SMB_COM_TRANSACTION || cmd == SMB_COM_TRANSACTION_SECONDARY)
126 flags2 &= ~SMB_FLAGS2_UNICODE;
127 if (cmd == SMB_COM_NEGOTIATE)
128 flags2 &= ~SMB_FLAGS2_SECURITY_SIGNATURE;
129 mb_put_uint16le(mbp, flags2);
130 if ((flags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) {
131 mb_put_mem(mbp, tzero, 12, MB_MSYSTEM);
132 rqp->sr_rqsig = NULL;
134 mb_put_uint16le(mbp, 0 /*scred->sc_p->p_pid >> 16*/);
135 rqp->sr_rqsig = (u_int8_t *)mb_reserve(mbp, 8);
136 mb_put_uint16le(mbp, 0);
138 rqp->sr_rqtid = mb_reserve(mbp, sizeof(u_int16_t));
139 mb_put_uint16le(mbp, 1 /*scred->sc_p->p_pid & 0xffff*/);
140 rqp->sr_rquid = mb_reserve(mbp, sizeof(u_int16_t));
141 mb_put_uint16le(mbp, rqp->sr_mid);
146 smb_rq_done(struct smb_rq *rqp)
148 mb_done(&rqp->sr_rq);
149 md_done(&rqp->sr_rp);
150 smb_sl_destroy(&rqp->sr_slock);
151 if (rqp->sr_flags & SMBR_ALLOCED)
156 * Simple request-reply exchange
159 smb_rq_simple(struct smb_rq *rqp)
161 struct smb_vc *vcp = rqp->sr_vc;
162 int error = EINVAL, i;
164 for (i = 0; i < SMB_MAXRCN; i++) {
165 rqp->sr_flags &= ~SMBR_RESTART;
166 rqp->sr_timo = vcp->vc_timo;
167 rqp->sr_state = SMBRQ_NOTSENT;
168 error = smb_rq_enqueue(rqp);
171 error = smb_rq_reply(rqp);
174 if ((rqp->sr_flags & (SMBR_RESTART | SMBR_NORESTART)) != SMBR_RESTART)
181 smb_rq_enqueue(struct smb_rq *rqp)
183 struct smb_share *ssp = rqp->sr_share;
186 if (ssp == NULL || rqp->sr_cred == &rqp->sr_vc->vc_iod->iod_scred) {
187 return smb_iod_addrq(rqp);
191 if (ssp->ss_flags & SMBS_RECONNECTING) {
192 msleep(&ssp->ss_vcgenid, SMBS_ST_LOCKPTR(ssp),
193 PWAIT | PDROP, "90trcn", hz);
194 if (smb_td_intr(rqp->sr_cred->scr_td))
198 if (smb_share_valid(ssp) || (ssp->ss_flags & SMBS_CONNECTED) == 0) {
202 error = smb_iod_request(rqp->sr_vc->vc_iod,
203 SMBIOD_EV_TREECONNECT | SMBIOD_EV_SYNC, ssp);
207 error = smb_iod_addrq(rqp);
215 smb_rq_wstart(struct smb_rq *rqp)
217 rqp->sr_wcount = mb_reserve(&rqp->sr_rq, sizeof(u_int8_t));
218 rqp->sr_rq.mb_count = 0;
222 smb_rq_wend(struct smb_rq *rqp)
224 if (rqp->sr_wcount == NULL) {
225 SMBERROR("no wcount\n"); /* actually panic */
228 if (rqp->sr_rq.mb_count & 1)
229 SMBERROR("odd word count\n");
230 *rqp->sr_wcount = rqp->sr_rq.mb_count / 2;
234 smb_rq_bstart(struct smb_rq *rqp)
236 rqp->sr_bcount = mb_reserve(&rqp->sr_rq, sizeof(u_short));
237 rqp->sr_rq.mb_count = 0;
241 smb_rq_bend(struct smb_rq *rqp)
245 if (rqp->sr_bcount == NULL) {
246 SMBERROR("no bcount\n"); /* actually panic */
249 bcnt = rqp->sr_rq.mb_count;
251 SMBERROR("byte count too large (%d)\n", bcnt);
252 le16enc(rqp->sr_bcount, bcnt);
256 smb_rq_intr(struct smb_rq *rqp)
258 if (rqp->sr_flags & SMBR_INTR)
260 return smb_td_intr(rqp->sr_cred->scr_td);
264 smb_rq_getrequest(struct smb_rq *rqp, struct mbchain **mbpp)
271 smb_rq_getreply(struct smb_rq *rqp, struct mdchain **mbpp)
278 smb_rq_getenv(struct smb_connobj *layer,
279 struct smb_vc **vcpp, struct smb_share **sspp)
281 struct smb_vc *vcp = NULL;
282 struct smb_share *ssp = NULL;
283 struct smb_connobj *cp;
286 switch (layer->co_level) {
289 if (layer->co_parent == NULL) {
290 SMBERROR("zombie VC %s\n", vcp->vc_srvname);
297 cp = layer->co_parent;
299 SMBERROR("zombie share %s\n", ssp->ss_name);
303 error = smb_rq_getenv(cp, &vcp, NULL);
308 SMBERROR("invalid layer %d passed\n", layer->co_level);
319 * Wait for reply on the request
322 smb_rq_reply(struct smb_rq *rqp)
324 struct mdchain *mdp = &rqp->sr_rp;
327 int error, rperror = 0;
329 error = smb_iod_waitrq(rqp);
332 error = md_get_uint32(mdp, &tdw);
335 error = md_get_uint8(mdp, &tb);
336 if (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_ERR_STATUS) {
337 error = md_get_uint32le(mdp, &rqp->sr_error);
339 error = md_get_uint8(mdp, &rqp->sr_errclass);
340 error = md_get_uint8(mdp, &tb);
341 error = md_get_uint16le(mdp, &rqp->sr_serror);
343 rperror = smb_maperror(rqp->sr_errclass, rqp->sr_serror);
345 error = md_get_uint8(mdp, &rqp->sr_rpflags);
346 error = md_get_uint16le(mdp, &rqp->sr_rpflags2);
348 error = md_get_uint32(mdp, &tdw);
349 error = md_get_uint32(mdp, &tdw);
350 error = md_get_uint32(mdp, &tdw);
352 error = md_get_uint16le(mdp, &rqp->sr_rptid);
353 error = md_get_uint16le(mdp, &rqp->sr_rppid);
354 error = md_get_uint16le(mdp, &rqp->sr_rpuid);
355 error = md_get_uint16le(mdp, &rqp->sr_rpmid);
358 (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE))
359 error = smb_rq_verify(rqp);
361 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x, E: %d:%d\n",
362 rqp->sr_rpmid, rqp->sr_rppid, rqp->sr_rpuid, rqp->sr_rptid,
363 rqp->sr_errclass, rqp->sr_serror);
364 return error ? error : rperror;
368 #define ALIGN4(a) (((a) + 3) & ~3)
371 * TRANS2 request implementation
374 smb_t2_alloc(struct smb_connobj *layer, u_short setup, struct smb_cred *scred,
375 struct smb_t2rq **t2pp)
377 struct smb_t2rq *t2p;
380 t2p = malloc(sizeof(*t2p), M_SMBRQ, M_WAITOK);
383 error = smb_t2_init(t2p, layer, setup, scred);
384 t2p->t2_flags |= SMBT2_ALLOCED;
394 smb_t2_init(struct smb_t2rq *t2p, struct smb_connobj *source, u_short setup,
395 struct smb_cred *scred)
399 bzero(t2p, sizeof(*t2p));
400 t2p->t2_source = source;
401 t2p->t2_setupcount = 1;
402 t2p->t2_setupdata = t2p->t2_setup;
403 t2p->t2_setup[0] = setup;
404 t2p->t2_fid = 0xffff;
405 t2p->t2_cred = scred;
406 error = smb_rq_getenv(source, &t2p->t2_vc, NULL);
413 smb_t2_done(struct smb_t2rq *t2p)
415 mb_done(&t2p->t2_tparam);
416 mb_done(&t2p->t2_tdata);
417 md_done(&t2p->t2_rparam);
418 md_done(&t2p->t2_rdata);
419 if (t2p->t2_flags & SMBT2_ALLOCED)
424 smb_t2_placedata(struct mbuf *mtop, u_int16_t offset, u_int16_t count,
430 m0 = m_split(mtop, offset, M_WAITOK);
431 len = m_length(m0, &m);
432 m->m_len -= len - count;
433 if (mdp->md_top == NULL) {
436 m_cat(mdp->md_top, m0);
441 smb_t2_reply(struct smb_t2rq *t2p)
444 struct smb_rq *rqp = t2p->t2_rq;
445 int error, totpgot, totdgot;
446 u_int16_t totpcount, totdcount, pcount, poff, doff, pdisp, ddisp;
447 u_int16_t tmp, bc, dcount;
450 error = smb_rq_reply(rqp);
453 if ((t2p->t2_flags & SMBT2_ALLSENT) == 0) {
455 * this is an interim response, ignore it.
458 md_next_record(&rqp->sr_rp);
463 * Now we have to get all subsequent responses. The CIFS specification
464 * says that they can be disordered which is weird.
467 totpgot = totdgot = 0;
468 totpcount = totdcount = 0xffff;
471 m_dumpm(mdp->md_top);
472 if ((error = md_get_uint8(mdp, &wc)) != 0)
478 if ((error = md_get_uint16le(mdp, &tmp)) != 0)
482 md_get_uint16le(mdp, &tmp);
485 if ((error = md_get_uint16le(mdp, &tmp)) != 0 || /* reserved */
486 (error = md_get_uint16le(mdp, &pcount)) != 0 ||
487 (error = md_get_uint16le(mdp, &poff)) != 0 ||
488 (error = md_get_uint16le(mdp, &pdisp)) != 0)
490 if (pcount != 0 && pdisp != totpgot) {
491 SMBERROR("Can't handle disordered parameters %d:%d\n",
496 if ((error = md_get_uint16le(mdp, &dcount)) != 0 ||
497 (error = md_get_uint16le(mdp, &doff)) != 0 ||
498 (error = md_get_uint16le(mdp, &ddisp)) != 0)
500 if (dcount != 0 && ddisp != totdgot) {
501 SMBERROR("Can't handle disordered data\n");
505 md_get_uint8(mdp, &wc);
506 md_get_uint8(mdp, NULL);
509 md_get_uint16(mdp, NULL);
510 if ((error = md_get_uint16le(mdp, &bc)) != 0)
512 /* tmp = SMB_HDRLEN + 1 + 10 * 2 + 2 * wc + 2;*/
514 error = smb_t2_placedata(mdp->md_top, doff, dcount,
520 error = smb_t2_placedata(mdp->md_top, poff, pcount,
527 if (totpgot >= totpcount && totdgot >= totdcount) {
529 t2p->t2_flags |= SMBT2_ALLRECV;
533 * We're done with this reply, look for the next one.
536 md_next_record(&rqp->sr_rp);
538 error = smb_rq_reply(rqp);
546 * Perform a full round of TRANS2 request
549 smb_t2_request_int(struct smb_t2rq *t2p)
551 struct smb_vc *vcp = t2p->t2_vc;
552 struct smb_cred *scred = t2p->t2_cred;
554 struct mdchain *mdp, mbparam, mbdata;
557 int totpcount, leftpcount, totdcount, leftdcount, len, txmax, i;
558 int error, doff, poff, txdcount, txpcount, nmlen;
560 m = t2p->t2_tparam.mb_top;
562 md_initm(&mbparam, m); /* do not free it! */
563 totpcount = m_fixhdr(m);
564 if (totpcount > 0xffff) /* maxvalue for u_short */
568 m = t2p->t2_tdata.mb_top;
570 md_initm(&mbdata, m); /* do not free it! */
571 totdcount = m_fixhdr(m);
572 if (totdcount > 0xffff)
576 leftdcount = totdcount;
577 leftpcount = totpcount;
578 txmax = vcp->vc_txmax;
579 error = smb_rq_alloc(t2p->t2_source, t2p->t_name ?
580 SMB_COM_TRANSACTION : SMB_COM_TRANSACTION2, scred, &rqp);
583 rqp->sr_flags |= SMBR_MULTIPACKET;
588 mb_put_uint16le(mbp, totpcount);
589 mb_put_uint16le(mbp, totdcount);
590 mb_put_uint16le(mbp, t2p->t2_maxpcount);
591 mb_put_uint16le(mbp, t2p->t2_maxdcount);
592 mb_put_uint8(mbp, t2p->t2_maxscount);
593 mb_put_uint8(mbp, 0); /* reserved */
594 mb_put_uint16le(mbp, 0); /* flags */
595 mb_put_uint32le(mbp, 0); /* Timeout */
596 mb_put_uint16le(mbp, 0); /* reserved 2 */
597 len = mb_fixhdr(mbp);
599 * now we have known packet size as
600 * ALIGN4(len + 5 * 2 + setupcount * 2 + 2 + strlen(name) + 1),
601 * and need to decide which parts should go into the first request
603 nmlen = t2p->t_name ? strlen(t2p->t_name) : 0;
604 len = ALIGN4(len + 5 * 2 + t2p->t2_setupcount * 2 + 2 + nmlen + 1);
605 if (len + leftpcount > txmax) {
606 txpcount = min(leftpcount, txmax - len);
611 txpcount = leftpcount;
612 poff = txpcount ? len : 0;
613 len = ALIGN4(len + txpcount);
614 txdcount = min(leftdcount, txmax - len);
615 doff = txdcount ? len : 0;
617 leftpcount -= txpcount;
618 leftdcount -= txdcount;
619 mb_put_uint16le(mbp, txpcount);
620 mb_put_uint16le(mbp, poff);
621 mb_put_uint16le(mbp, txdcount);
622 mb_put_uint16le(mbp, doff);
623 mb_put_uint8(mbp, t2p->t2_setupcount);
624 mb_put_uint8(mbp, 0);
625 for (i = 0; i < t2p->t2_setupcount; i++)
626 mb_put_uint16le(mbp, t2p->t2_setupdata[i]);
631 mb_put_mem(mbp, t2p->t_name, nmlen, MB_MSYSTEM);
632 mb_put_uint8(mbp, 0); /* terminating zero */
633 len = mb_fixhdr(mbp);
635 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
636 error = md_get_mbuf(&mbparam, txpcount, &m);
637 SMBSDEBUG("%d:%d:%d\n", error, txpcount, txmax);
642 len = mb_fixhdr(mbp);
644 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
645 error = md_get_mbuf(&mbdata, txdcount, &m);
650 smb_rq_bend(rqp); /* incredible, but thats it... */
651 error = smb_rq_enqueue(rqp);
654 if (leftpcount == 0 && leftdcount == 0)
655 t2p->t2_flags |= SMBT2_ALLSENT;
656 error = smb_t2_reply(t2p);
659 while (leftpcount || leftdcount) {
660 t2p->t2_flags |= SMBT2_SECONDARY;
661 error = smb_rq_new(rqp, t2p->t_name ?
662 SMB_COM_TRANSACTION_SECONDARY : SMB_COM_TRANSACTION2_SECONDARY);
667 mb_put_uint16le(mbp, totpcount);
668 mb_put_uint16le(mbp, totdcount);
669 len = mb_fixhdr(mbp);
671 * now we have known packet size as
672 * ALIGN4(len + 7 * 2 + 2) for T2 request, and -2 for T one,
673 * and need to decide which parts should go into request
675 len = ALIGN4(len + 6 * 2 + 2);
676 if (t2p->t_name == NULL)
678 if (len + leftpcount > txmax) {
679 txpcount = min(leftpcount, txmax - len);
684 txpcount = leftpcount;
685 poff = txpcount ? len : 0;
686 len = ALIGN4(len + txpcount);
687 txdcount = min(leftdcount, txmax - len);
688 doff = txdcount ? len : 0;
690 mb_put_uint16le(mbp, txpcount);
691 mb_put_uint16le(mbp, poff);
692 mb_put_uint16le(mbp, totpcount - leftpcount);
693 mb_put_uint16le(mbp, txdcount);
694 mb_put_uint16le(mbp, doff);
695 mb_put_uint16le(mbp, totdcount - leftdcount);
696 leftpcount -= txpcount;
697 leftdcount -= txdcount;
698 if (t2p->t_name == NULL)
699 mb_put_uint16le(mbp, t2p->t2_fid);
702 mb_put_uint8(mbp, 0); /* name */
703 len = mb_fixhdr(mbp);
705 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
706 error = md_get_mbuf(&mbparam, txpcount, &m);
711 len = mb_fixhdr(mbp);
713 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
714 error = md_get_mbuf(&mbdata, txdcount, &m);
720 rqp->sr_state = SMBRQ_NOTSENT;
721 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_NEWRQ, NULL);
724 } /* while left params or data */
725 t2p->t2_flags |= SMBT2_ALLSENT;
726 mdp = &t2p->t2_rdata;
728 m_fixhdr(mdp->md_top);
729 md_initm(mdp, mdp->md_top);
731 mdp = &t2p->t2_rparam;
733 m_fixhdr(mdp->md_top);
734 md_initm(mdp, mdp->md_top);
737 smb_iod_removerq(rqp);
741 if (rqp->sr_flags & SMBR_RESTART)
742 t2p->t2_flags |= SMBT2_RESTART;
743 md_done(&t2p->t2_rparam);
744 md_done(&t2p->t2_rdata);
750 smb_t2_request(struct smb_t2rq *t2p)
752 int error = EINVAL, i;
754 for (i = 0; i < SMB_MAXRCN; i++) {
755 t2p->t2_flags &= ~SMBR_RESTART;
756 error = smb_t2_request_int(t2p);
759 if ((t2p->t2_flags & (SMBT2_RESTART | SMBT2_NORESTART)) != SMBT2_RESTART)