1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/endian.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/proc.h> 39 #include <sys/lock.h> 40 #include <sys/sysctl.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/mbuf.h> 44 45 #include <netsmb/smb.h> 46 #include <netsmb/smb_conn.h> 47 #include <netsmb/smb_rq.h> 48 #include <netsmb/smb_subr.h> 49 #include <netsmb/smb_tran.h> 50 51 static MALLOC_DEFINE(M_SMBRQ, "SMBRQ", "SMB request"); 52 53 MODULE_DEPEND(netsmb, libmchain, 1, 1, 1); 54 55 static int smb_rq_reply(struct smb_rq *rqp); 56 static int smb_rq_enqueue(struct smb_rq *rqp); 57 static int smb_rq_getenv(struct smb_connobj *layer, 58 struct smb_vc **vcpp, struct smb_share **sspp); 59 static int smb_rq_new(struct smb_rq *rqp, u_char cmd); 60 static int smb_t2_reply(struct smb_t2rq *t2p); 61 62 int 63 smb_rq_alloc(struct smb_connobj *layer, u_char cmd, struct smb_cred *scred, 64 struct smb_rq **rqpp) 65 { 66 struct smb_rq *rqp; 67 int error; 68 69 rqp = malloc(sizeof(*rqp), M_SMBRQ, M_WAITOK); 70 if (rqp == NULL) 71 return ENOMEM; 72 error = smb_rq_init(rqp, layer, cmd, scred); 73 rqp->sr_flags |= SMBR_ALLOCED; 74 if (error) { 75 smb_rq_done(rqp); 76 return error; 77 } 78 *rqpp = rqp; 79 return 0; 80 } 81 82 static char tzero[12]; 83 84 int 85 smb_rq_init(struct smb_rq *rqp, struct smb_connobj *layer, u_char cmd, 86 struct smb_cred *scred) 87 { 88 int error; 89 90 bzero(rqp, sizeof(*rqp)); 91 smb_sl_init(&rqp->sr_slock, "srslock"); 92 error = smb_rq_getenv(layer, &rqp->sr_vc, &rqp->sr_share); 93 if (error) 94 return error; 95 error = smb_vc_access(rqp->sr_vc, scred, SMBM_EXEC); 96 if (error) 97 return error; 98 if (rqp->sr_share) { 99 error = smb_share_access(rqp->sr_share, scred, SMBM_EXEC); 100 if (error) 101 return error; 102 } 103 rqp->sr_cred = scred; 104 rqp->sr_mid = smb_vc_nextmid(rqp->sr_vc); 105 return smb_rq_new(rqp, cmd); 106 } 107 108 static int 109 smb_rq_new(struct smb_rq *rqp, u_char cmd) 110 { 111 struct smb_vc *vcp = rqp->sr_vc; 112 struct mbchain *mbp = &rqp->sr_rq; 113 int error; 114 u_int16_t flags2; 115 116 rqp->sr_sendcnt = 0; 117 mb_done(mbp); 118 md_done(&rqp->sr_rp); 119 error = mb_init(mbp); 120 if (error) 121 return error; 122 mb_put_mem(mbp, SMB_SIGNATURE, SMB_SIGLEN, MB_MSYSTEM); 123 mb_put_uint8(mbp, cmd); 124 mb_put_uint32le(mbp, 0); /* DosError */ 125 mb_put_uint8(mbp, vcp->vc_hflags); 126 flags2 = vcp->vc_hflags2; 127 if (cmd == SMB_COM_TRANSACTION || cmd == SMB_COM_TRANSACTION_SECONDARY) 128 flags2 &= ~SMB_FLAGS2_UNICODE; 129 if (cmd == SMB_COM_NEGOTIATE) 130 flags2 &= ~SMB_FLAGS2_SECURITY_SIGNATURE; 131 mb_put_uint16le(mbp, flags2); 132 if ((flags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) { 133 mb_put_mem(mbp, tzero, 12, MB_MSYSTEM); 134 rqp->sr_rqsig = NULL; 135 } else { 136 mb_put_uint16le(mbp, 0 /*scred->sc_p->p_pid >> 16*/); 137 rqp->sr_rqsig = (u_int8_t *)mb_reserve(mbp, 8); 138 mb_put_uint16le(mbp, 0); 139 } 140 rqp->sr_rqtid = mb_reserve(mbp, sizeof(u_int16_t)); 141 mb_put_uint16le(mbp, 1 /*scred->sc_p->p_pid & 0xffff*/); 142 rqp->sr_rquid = mb_reserve(mbp, sizeof(u_int16_t)); 143 mb_put_uint16le(mbp, rqp->sr_mid); 144 return 0; 145 } 146 147 void 148 smb_rq_done(struct smb_rq *rqp) 149 { 150 mb_done(&rqp->sr_rq); 151 md_done(&rqp->sr_rp); 152 smb_sl_destroy(&rqp->sr_slock); 153 if (rqp->sr_flags & SMBR_ALLOCED) 154 free(rqp, M_SMBRQ); 155 } 156 157 /* 158 * Simple request-reply exchange 159 */ 160 int 161 smb_rq_simple(struct smb_rq *rqp) 162 { 163 struct smb_vc *vcp = rqp->sr_vc; 164 int error = EINVAL, i; 165 166 for (i = 0; i < SMB_MAXRCN; i++) { 167 rqp->sr_flags &= ~SMBR_RESTART; 168 rqp->sr_timo = vcp->vc_timo; 169 rqp->sr_state = SMBRQ_NOTSENT; 170 error = smb_rq_enqueue(rqp); 171 if (error) 172 return error; 173 error = smb_rq_reply(rqp); 174 if (error == 0) 175 break; 176 if ((rqp->sr_flags & (SMBR_RESTART | SMBR_NORESTART)) != SMBR_RESTART) 177 break; 178 } 179 return error; 180 } 181 182 static int 183 smb_rq_enqueue(struct smb_rq *rqp) 184 { 185 struct smb_share *ssp = rqp->sr_share; 186 int error; 187 188 if (ssp == NULL || rqp->sr_cred == &rqp->sr_vc->vc_iod->iod_scred) { 189 return smb_iod_addrq(rqp); 190 } 191 for (;;) { 192 SMBS_ST_LOCK(ssp); 193 if (ssp->ss_flags & SMBS_RECONNECTING) { 194 msleep(&ssp->ss_vcgenid, SMBS_ST_LOCKPTR(ssp), 195 PWAIT | PDROP, "90trcn", hz); 196 if (smb_td_intr(rqp->sr_cred->scr_td)) 197 return EINTR; 198 continue; 199 } 200 if (smb_share_valid(ssp) || (ssp->ss_flags & SMBS_CONNECTED) == 0) { 201 SMBS_ST_UNLOCK(ssp); 202 } else { 203 SMBS_ST_UNLOCK(ssp); 204 error = smb_iod_request(rqp->sr_vc->vc_iod, 205 SMBIOD_EV_TREECONNECT | SMBIOD_EV_SYNC, ssp); 206 if (error) 207 return error; 208 } 209 error = smb_iod_addrq(rqp); 210 if (error != EXDEV) 211 break; 212 } 213 return error; 214 } 215 216 void 217 smb_rq_wstart(struct smb_rq *rqp) 218 { 219 rqp->sr_wcount = mb_reserve(&rqp->sr_rq, sizeof(u_int8_t)); 220 rqp->sr_rq.mb_count = 0; 221 } 222 223 void 224 smb_rq_wend(struct smb_rq *rqp) 225 { 226 if (rqp->sr_wcount == NULL) { 227 SMBERROR("no wcount\n"); /* actually panic */ 228 return; 229 } 230 if (rqp->sr_rq.mb_count & 1) 231 SMBERROR("odd word count\n"); 232 *rqp->sr_wcount = rqp->sr_rq.mb_count / 2; 233 } 234 235 void 236 smb_rq_bstart(struct smb_rq *rqp) 237 { 238 rqp->sr_bcount = mb_reserve(&rqp->sr_rq, sizeof(u_short)); 239 rqp->sr_rq.mb_count = 0; 240 } 241 242 void 243 smb_rq_bend(struct smb_rq *rqp) 244 { 245 int bcnt; 246 247 if (rqp->sr_bcount == NULL) { 248 SMBERROR("no bcount\n"); /* actually panic */ 249 return; 250 } 251 bcnt = rqp->sr_rq.mb_count; 252 if (bcnt > 0xffff) 253 SMBERROR("byte count too large (%d)\n", bcnt); 254 le16enc(rqp->sr_bcount, bcnt); 255 } 256 257 int 258 smb_rq_intr(struct smb_rq *rqp) 259 { 260 if (rqp->sr_flags & SMBR_INTR) 261 return EINTR; 262 return smb_td_intr(rqp->sr_cred->scr_td); 263 } 264 265 int 266 smb_rq_getrequest(struct smb_rq *rqp, struct mbchain **mbpp) 267 { 268 *mbpp = &rqp->sr_rq; 269 return 0; 270 } 271 272 int 273 smb_rq_getreply(struct smb_rq *rqp, struct mdchain **mbpp) 274 { 275 *mbpp = &rqp->sr_rp; 276 return 0; 277 } 278 279 static int 280 smb_rq_getenv(struct smb_connobj *layer, 281 struct smb_vc **vcpp, struct smb_share **sspp) 282 { 283 struct smb_vc *vcp = NULL; 284 struct smb_share *ssp = NULL; 285 struct smb_connobj *cp; 286 int error = 0; 287 288 switch (layer->co_level) { 289 case SMBL_VC: 290 vcp = CPTOVC(layer); 291 if (layer->co_parent == NULL) { 292 SMBERROR("zombie VC %s\n", vcp->vc_srvname); 293 error = EINVAL; 294 break; 295 } 296 break; 297 case SMBL_SHARE: 298 ssp = CPTOSS(layer); 299 cp = layer->co_parent; 300 if (cp == NULL) { 301 SMBERROR("zombie share %s\n", ssp->ss_name); 302 error = EINVAL; 303 break; 304 } 305 error = smb_rq_getenv(cp, &vcp, NULL); 306 if (error) 307 break; 308 break; 309 default: 310 SMBERROR("invalid layer %d passed\n", layer->co_level); 311 error = EINVAL; 312 } 313 if (vcpp) 314 *vcpp = vcp; 315 if (sspp) 316 *sspp = ssp; 317 return error; 318 } 319 320 /* 321 * Wait for reply on the request 322 */ 323 static int 324 smb_rq_reply(struct smb_rq *rqp) 325 { 326 struct mdchain *mdp = &rqp->sr_rp; 327 u_int32_t tdw; 328 u_int8_t tb; 329 int error, rperror = 0; 330 331 error = smb_iod_waitrq(rqp); 332 if (error) 333 return error; 334 error = md_get_uint32(mdp, &tdw); 335 if (error) 336 return error; 337 error = md_get_uint8(mdp, &tb); 338 if (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_ERR_STATUS) { 339 error = md_get_uint32le(mdp, &rqp->sr_error); 340 } else { 341 error = md_get_uint8(mdp, &rqp->sr_errclass); 342 error = md_get_uint8(mdp, &tb); 343 error = md_get_uint16le(mdp, &rqp->sr_serror); 344 if (!error) 345 rperror = smb_maperror(rqp->sr_errclass, rqp->sr_serror); 346 } 347 error = md_get_uint8(mdp, &rqp->sr_rpflags); 348 error = md_get_uint16le(mdp, &rqp->sr_rpflags2); 349 350 error = md_get_uint32(mdp, &tdw); 351 error = md_get_uint32(mdp, &tdw); 352 error = md_get_uint32(mdp, &tdw); 353 354 error = md_get_uint16le(mdp, &rqp->sr_rptid); 355 error = md_get_uint16le(mdp, &rqp->sr_rppid); 356 error = md_get_uint16le(mdp, &rqp->sr_rpuid); 357 error = md_get_uint16le(mdp, &rqp->sr_rpmid); 358 359 if (error == 0 && 360 (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)) 361 error = smb_rq_verify(rqp); 362 363 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x, E: %d:%d\n", 364 rqp->sr_rpmid, rqp->sr_rppid, rqp->sr_rpuid, rqp->sr_rptid, 365 rqp->sr_errclass, rqp->sr_serror); 366 return error ? error : rperror; 367 } 368 369 #define ALIGN4(a) (((a) + 3) & ~3) 370 371 /* 372 * TRANS2 request implementation 373 */ 374 int 375 smb_t2_alloc(struct smb_connobj *layer, u_short setup, struct smb_cred *scred, 376 struct smb_t2rq **t2pp) 377 { 378 struct smb_t2rq *t2p; 379 int error; 380 381 t2p = malloc(sizeof(*t2p), M_SMBRQ, M_WAITOK); 382 if (t2p == NULL) 383 return ENOMEM; 384 error = smb_t2_init(t2p, layer, setup, scred); 385 t2p->t2_flags |= SMBT2_ALLOCED; 386 if (error) { 387 smb_t2_done(t2p); 388 return error; 389 } 390 *t2pp = t2p; 391 return 0; 392 } 393 394 int 395 smb_t2_init(struct smb_t2rq *t2p, struct smb_connobj *source, u_short setup, 396 struct smb_cred *scred) 397 { 398 int error; 399 400 bzero(t2p, sizeof(*t2p)); 401 t2p->t2_source = source; 402 t2p->t2_setupcount = 1; 403 t2p->t2_setupdata = t2p->t2_setup; 404 t2p->t2_setup[0] = setup; 405 t2p->t2_fid = 0xffff; 406 t2p->t2_cred = scred; 407 error = smb_rq_getenv(source, &t2p->t2_vc, NULL); 408 if (error) 409 return error; 410 return 0; 411 } 412 413 void 414 smb_t2_done(struct smb_t2rq *t2p) 415 { 416 mb_done(&t2p->t2_tparam); 417 mb_done(&t2p->t2_tdata); 418 md_done(&t2p->t2_rparam); 419 md_done(&t2p->t2_rdata); 420 if (t2p->t2_flags & SMBT2_ALLOCED) 421 free(t2p, M_SMBRQ); 422 } 423 424 static int 425 smb_t2_placedata(struct mbuf *mtop, u_int16_t offset, u_int16_t count, 426 struct mdchain *mdp) 427 { 428 struct mbuf *m0; 429 int len; 430 431 len = m_length(mtop, NULL); 432 if (offset + count > len) 433 return (EPROTO); 434 435 m0 = m_split(mtop, offset, M_WAITOK); 436 if (len != offset + count) { 437 len -= offset + count; 438 m_adj(m0, -len); 439 } 440 if (mdp->md_top == NULL) { 441 md_initm(mdp, m0); 442 } else 443 m_cat(mdp->md_top, m0); 444 return 0; 445 } 446 447 static int 448 smb_t2_reply(struct smb_t2rq *t2p) 449 { 450 struct mdchain *mdp; 451 struct smb_rq *rqp = t2p->t2_rq; 452 int error, totpgot, totdgot; 453 u_int16_t totpcount, totdcount, pcount, poff, doff, pdisp, ddisp; 454 u_int16_t tmp, bc, dcount; 455 u_int8_t wc; 456 457 error = smb_rq_reply(rqp); 458 if (error) 459 return error; 460 if ((t2p->t2_flags & SMBT2_ALLSENT) == 0) { 461 /* 462 * this is an interim response, ignore it. 463 */ 464 SMBRQ_SLOCK(rqp); 465 md_next_record(&rqp->sr_rp); 466 SMBRQ_SUNLOCK(rqp); 467 return 0; 468 } 469 /* 470 * Now we have to get all subsequent responses. The CIFS specification 471 * says that they can be disordered which is weird. 472 * TODO: timo 473 */ 474 totpgot = totdgot = 0; 475 totpcount = totdcount = 0xffff; 476 mdp = &rqp->sr_rp; 477 for (;;) { 478 m_dumpm(mdp->md_top); 479 if ((error = md_get_uint8(mdp, &wc)) != 0) 480 break; 481 if (wc < 10) { 482 error = ENOENT; 483 break; 484 } 485 if ((error = md_get_uint16le(mdp, &tmp)) != 0) 486 break; 487 if (totpcount > tmp) 488 totpcount = tmp; 489 md_get_uint16le(mdp, &tmp); 490 if (totdcount > tmp) 491 totdcount = tmp; 492 if ((error = md_get_uint16le(mdp, &tmp)) != 0 || /* reserved */ 493 (error = md_get_uint16le(mdp, &pcount)) != 0 || 494 (error = md_get_uint16le(mdp, &poff)) != 0 || 495 (error = md_get_uint16le(mdp, &pdisp)) != 0) 496 break; 497 if (pcount != 0 && pdisp != totpgot) { 498 SMBERROR("Can't handle disordered parameters %d:%d\n", 499 pdisp, totpgot); 500 error = EINVAL; 501 break; 502 } 503 if ((error = md_get_uint16le(mdp, &dcount)) != 0 || 504 (error = md_get_uint16le(mdp, &doff)) != 0 || 505 (error = md_get_uint16le(mdp, &ddisp)) != 0) 506 break; 507 if (dcount != 0 && ddisp != totdgot) { 508 SMBERROR("Can't handle disordered data\n"); 509 error = EINVAL; 510 break; 511 } 512 md_get_uint8(mdp, &wc); 513 md_get_uint8(mdp, NULL); 514 tmp = wc; 515 while (tmp--) 516 md_get_uint16(mdp, NULL); 517 if ((error = md_get_uint16le(mdp, &bc)) != 0) 518 break; 519 /* tmp = SMB_HDRLEN + 1 + 10 * 2 + 2 * wc + 2;*/ 520 if (dcount) { 521 error = smb_t2_placedata(mdp->md_top, doff, dcount, 522 &t2p->t2_rdata); 523 if (error) 524 break; 525 } 526 if (pcount) { 527 error = smb_t2_placedata(mdp->md_top, poff, pcount, 528 &t2p->t2_rparam); 529 if (error) 530 break; 531 } 532 totpgot += pcount; 533 totdgot += dcount; 534 if (totpgot >= totpcount && totdgot >= totdcount) { 535 error = 0; 536 t2p->t2_flags |= SMBT2_ALLRECV; 537 break; 538 } 539 /* 540 * We're done with this reply, look for the next one. 541 */ 542 SMBRQ_SLOCK(rqp); 543 md_next_record(&rqp->sr_rp); 544 SMBRQ_SUNLOCK(rqp); 545 error = smb_rq_reply(rqp); 546 if (error) 547 break; 548 } 549 return error; 550 } 551 552 /* 553 * Perform a full round of TRANS2 request 554 */ 555 static int 556 smb_t2_request_int(struct smb_t2rq *t2p) 557 { 558 struct smb_vc *vcp = t2p->t2_vc; 559 struct smb_cred *scred = t2p->t2_cred; 560 struct mbchain *mbp; 561 struct mdchain *mdp, mbparam, mbdata; 562 struct mbuf *m; 563 struct smb_rq *rqp; 564 int totpcount, leftpcount, totdcount, leftdcount, len, txmax, i; 565 int error, doff, poff, txdcount, txpcount, nmlen; 566 567 m = t2p->t2_tparam.mb_top; 568 if (m) { 569 md_initm(&mbparam, m); /* do not free it! */ 570 totpcount = m_fixhdr(m); 571 if (totpcount > 0xffff) /* maxvalue for u_short */ 572 return EINVAL; 573 } else 574 totpcount = 0; 575 m = t2p->t2_tdata.mb_top; 576 if (m) { 577 md_initm(&mbdata, m); /* do not free it! */ 578 totdcount = m_fixhdr(m); 579 if (totdcount > 0xffff) 580 return EINVAL; 581 } else 582 totdcount = 0; 583 leftdcount = totdcount; 584 leftpcount = totpcount; 585 txmax = vcp->vc_txmax; 586 error = smb_rq_alloc(t2p->t2_source, t2p->t_name ? 587 SMB_COM_TRANSACTION : SMB_COM_TRANSACTION2, scred, &rqp); 588 if (error) 589 return error; 590 rqp->sr_flags |= SMBR_MULTIPACKET; 591 t2p->t2_rq = rqp; 592 rqp->sr_t2 = t2p; 593 mbp = &rqp->sr_rq; 594 smb_rq_wstart(rqp); 595 mb_put_uint16le(mbp, totpcount); 596 mb_put_uint16le(mbp, totdcount); 597 mb_put_uint16le(mbp, t2p->t2_maxpcount); 598 mb_put_uint16le(mbp, t2p->t2_maxdcount); 599 mb_put_uint8(mbp, t2p->t2_maxscount); 600 mb_put_uint8(mbp, 0); /* reserved */ 601 mb_put_uint16le(mbp, 0); /* flags */ 602 mb_put_uint32le(mbp, 0); /* Timeout */ 603 mb_put_uint16le(mbp, 0); /* reserved 2 */ 604 len = mb_fixhdr(mbp); 605 /* 606 * now we have known packet size as 607 * ALIGN4(len + 5 * 2 + setupcount * 2 + 2 + strlen(name) + 1), 608 * and need to decide which parts should go into the first request 609 */ 610 nmlen = t2p->t_name ? strlen(t2p->t_name) : 0; 611 len = ALIGN4(len + 5 * 2 + t2p->t2_setupcount * 2 + 2 + nmlen + 1); 612 if (len + leftpcount > txmax) { 613 txpcount = min(leftpcount, txmax - len); 614 poff = len; 615 txdcount = 0; 616 doff = 0; 617 } else { 618 txpcount = leftpcount; 619 poff = txpcount ? len : 0; 620 len = ALIGN4(len + txpcount); 621 txdcount = min(leftdcount, txmax - len); 622 doff = txdcount ? len : 0; 623 } 624 leftpcount -= txpcount; 625 leftdcount -= txdcount; 626 mb_put_uint16le(mbp, txpcount); 627 mb_put_uint16le(mbp, poff); 628 mb_put_uint16le(mbp, txdcount); 629 mb_put_uint16le(mbp, doff); 630 mb_put_uint8(mbp, t2p->t2_setupcount); 631 mb_put_uint8(mbp, 0); 632 for (i = 0; i < t2p->t2_setupcount; i++) 633 mb_put_uint16le(mbp, t2p->t2_setupdata[i]); 634 smb_rq_wend(rqp); 635 smb_rq_bstart(rqp); 636 /* TDUNICODE */ 637 if (t2p->t_name) 638 mb_put_mem(mbp, t2p->t_name, nmlen, MB_MSYSTEM); 639 mb_put_uint8(mbp, 0); /* terminating zero */ 640 len = mb_fixhdr(mbp); 641 if (txpcount) { 642 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 643 error = md_get_mbuf(&mbparam, txpcount, &m); 644 SMBSDEBUG("%d:%d:%d\n", error, txpcount, txmax); 645 if (error) 646 goto freerq; 647 mb_put_mbuf(mbp, m); 648 } 649 len = mb_fixhdr(mbp); 650 if (txdcount) { 651 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 652 error = md_get_mbuf(&mbdata, txdcount, &m); 653 if (error) 654 goto freerq; 655 mb_put_mbuf(mbp, m); 656 } 657 smb_rq_bend(rqp); /* incredible, but thats it... */ 658 error = smb_rq_enqueue(rqp); 659 if (error) 660 goto freerq; 661 if (leftpcount == 0 && leftdcount == 0) 662 t2p->t2_flags |= SMBT2_ALLSENT; 663 error = smb_t2_reply(t2p); 664 if (error) 665 goto bad; 666 while (leftpcount || leftdcount) { 667 t2p->t2_flags |= SMBT2_SECONDARY; 668 error = smb_rq_new(rqp, t2p->t_name ? 669 SMB_COM_TRANSACTION_SECONDARY : SMB_COM_TRANSACTION2_SECONDARY); 670 if (error) 671 goto bad; 672 mbp = &rqp->sr_rq; 673 smb_rq_wstart(rqp); 674 mb_put_uint16le(mbp, totpcount); 675 mb_put_uint16le(mbp, totdcount); 676 len = mb_fixhdr(mbp); 677 /* 678 * now we have known packet size as 679 * ALIGN4(len + 7 * 2 + 2) for T2 request, and -2 for T one, 680 * and need to decide which parts should go into request 681 */ 682 len = ALIGN4(len + 6 * 2 + 2); 683 if (t2p->t_name == NULL) 684 len += 2; 685 if (len + leftpcount > txmax) { 686 txpcount = min(leftpcount, txmax - len); 687 poff = len; 688 txdcount = 0; 689 doff = 0; 690 } else { 691 txpcount = leftpcount; 692 poff = txpcount ? len : 0; 693 len = ALIGN4(len + txpcount); 694 txdcount = min(leftdcount, txmax - len); 695 doff = txdcount ? len : 0; 696 } 697 mb_put_uint16le(mbp, txpcount); 698 mb_put_uint16le(mbp, poff); 699 mb_put_uint16le(mbp, totpcount - leftpcount); 700 mb_put_uint16le(mbp, txdcount); 701 mb_put_uint16le(mbp, doff); 702 mb_put_uint16le(mbp, totdcount - leftdcount); 703 leftpcount -= txpcount; 704 leftdcount -= txdcount; 705 if (t2p->t_name == NULL) 706 mb_put_uint16le(mbp, t2p->t2_fid); 707 smb_rq_wend(rqp); 708 smb_rq_bstart(rqp); 709 mb_put_uint8(mbp, 0); /* name */ 710 len = mb_fixhdr(mbp); 711 if (txpcount) { 712 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 713 error = md_get_mbuf(&mbparam, txpcount, &m); 714 if (error) 715 goto bad; 716 mb_put_mbuf(mbp, m); 717 } 718 len = mb_fixhdr(mbp); 719 if (txdcount) { 720 mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 721 error = md_get_mbuf(&mbdata, txdcount, &m); 722 if (error) 723 goto bad; 724 mb_put_mbuf(mbp, m); 725 } 726 smb_rq_bend(rqp); 727 rqp->sr_state = SMBRQ_NOTSENT; 728 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_NEWRQ, NULL); 729 if (error) 730 goto bad; 731 } /* while left params or data */ 732 t2p->t2_flags |= SMBT2_ALLSENT; 733 mdp = &t2p->t2_rdata; 734 if (mdp->md_top) { 735 m_fixhdr(mdp->md_top); 736 md_initm(mdp, mdp->md_top); 737 } 738 mdp = &t2p->t2_rparam; 739 if (mdp->md_top) { 740 m_fixhdr(mdp->md_top); 741 md_initm(mdp, mdp->md_top); 742 } 743 bad: 744 smb_iod_removerq(rqp); 745 freerq: 746 if (error) { 747 if (rqp->sr_flags & SMBR_RESTART) 748 t2p->t2_flags |= SMBT2_RESTART; 749 md_done(&t2p->t2_rparam); 750 md_done(&t2p->t2_rdata); 751 } 752 smb_rq_done(rqp); 753 return error; 754 } 755 756 int 757 smb_t2_request(struct smb_t2rq *t2p) 758 { 759 int error = EINVAL, i; 760 761 for (i = 0; i < SMB_MAXRCN; i++) { 762 t2p->t2_flags &= ~SMBR_RESTART; 763 error = smb_t2_request_int(t2p); 764 if (error == 0) 765 break; 766 if ((t2p->t2_flags & (SMBT2_RESTART | SMBT2_NORESTART)) != SMBT2_RESTART) 767 break; 768 } 769 return error; 770 } 771