Print this page
NEX-14666 Need to provide SMB 2.1 Client
NEX-17187 panic in smbfs_acl_store
NEX-17231 smbfs create xattr files finds wrong file
NEX-17224 smbfs lookup EINVAL should be ENOENT
NEX-17260 SMB1 client fails to list directory after NEX-14666
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
and: (cleanup)
NEX-15578 SMB2 durable handle redesign
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
NEX-5665 SMB2 oplock leases
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-15578 SMB2 durable handle redesign
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
NEX-5665 SMB2 oplock leases
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-5844 want SMB2 ioctl FSCTL_SRV_COPYCHUNK
NEX-6124 smb_fsop_read/write should allow file != sr->fid_ofile
NEX-6125 smbtorture invalid response with smb2.ioctl
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
NEX-4538 SMB1 create file should support extended_response format (2)
NEX-6116 Failures in smbtorture raw.open
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Kevin Crowe <kevin.crowe@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Include this commit if upstreaming/backporting any of:
NEX-4540 SMB server declines EA support incorrectly
NEX-4239 smbtorture create failures re. allocation size
(illumos) 6398 SMB should support path names longer than 1024
NEX-4538 SMB1 create file should support extended_response format
Reviewed by: Matt Barden <Matt.Barden@nexenta.com>
Reviewed by: Bayard Bell <bayard.bell@nexenta.com>
NEX-2522 svcadm disable network/smb/server may hang
Reviewed by: Matt Barden <Matt.Barden@nexenta.com>
NEX-3662 Backport illumos 1501: taskq_create_proc ... TQ_DYNAMIC puts tasks in p0 (take 2)
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Dan McDonald <danmcd@omniti.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Marcel Telka <marcel.telka@nexenta.com>
NEX-3576 RPC error when displaying open files via Windows MMC
Reviewed by: Kevin Crowe <kevin.crowe@nexenta.com>
NEX-2188 Browsing top level share produces RPC error 1728
SMB-119 Text file contains garbage when re-opened
SMB-120 Share enumeration fails with Windows 8
SMB-11 SMB2 message parse & dispatch
SMB-12 SMB2 Negotiate Protocol
SMB-13 SMB2 Session Setup
SMB-14 SMB2 Logoff
SMB-15 SMB2 Tree Connect
SMB-16 SMB2 Tree Disconnect
SMB-17 SMB2 Create
SMB-18 SMB2 Close
SMB-19 SMB2 Flush
SMB-20 SMB2 Read
SMB-21 SMB2 Write
SMB-22 SMB2 Lock/Unlock
SMB-23 SMB2 Ioctl
SMB-24 SMB2 Cancel
SMB-25 SMB2 Echo
SMB-26 SMB2 Query Dir
SMB-27 SMB2 Change Notify
SMB-28 SMB2 Query Info
SMB-29 SMB2 Set Info
SMB-30 SMB2 Oplocks
SMB-53 SMB2 Create Context options
(SMB2 code review cleanup 1, 2, 3)
SMB-39 Use AF_UNIX pipes for RPC
SMB-50 User-mode SMB server
 Includes work by these authors:
 Thomas Keiser <thomas.keiser@nexenta.com>
 Albert Lee <trisk@nexenta.com>
SMB-65 SMB server in non-global zones (data structure changes)
Many things move to the smb_server_t object, and
many functions gain an sv arg (which server).
SMB-65 SMB server in non-global zones (kmem_caches)
common kmem_cache instances across zones
separate GZ-only init from NGZ init
SMB-63 taskq_create_proc ... TQ_DYNAMIC puts tasks in p0
re #11974 CIFS Share - Tree connect fails from Windows 7 Clients
re #6812 rb1753 backport illumos 1604 smbd print_enable doesn't really work


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * This module provides the interface to NDR RPC.
  28  */
  29 
  30 #include <sys/stat.h>
  31 #include <sys/uio.h>
  32 #include <sys/ksynch.h>
  33 #include <sys/stropts.h>
  34 #include <sys/socket.h>
  35 #include <sys/filio.h>
  36 #include <smbsrv/smb_kproto.h>
  37 #include <smbsrv/smb_xdr.h>
  38 #include <smbsrv/winioctl.h>
  39 
  40 static uint32_t smb_opipe_transceive(smb_request_t *, smb_fsctl_t *);
  41 
  42 /*
  43  * Allocate a new opipe and return it, or NULL, in which case
  44  * the caller will report "internal error".
  45  */
  46 static smb_opipe_t *
  47 smb_opipe_alloc(smb_request_t *sr)
  48 {
  49         smb_server_t    *sv = sr->sr_server;
  50         smb_opipe_t     *opipe;
  51         ksocket_t       sock;
  52 
  53         if (ksocket_socket(&sock, AF_UNIX, SOCK_STREAM, 0,
  54             KSOCKET_SLEEP, sr->user_cr) != 0)
  55                 return (NULL);
  56 
  57         opipe = kmem_cache_alloc(smb_cache_opipe, KM_SLEEP);
  58 
  59         bzero(opipe, sizeof (smb_opipe_t));
  60         mutex_init(&opipe->p_mutex, NULL, MUTEX_DEFAULT, NULL);
  61         cv_init(&opipe->p_cv, NULL, CV_DEFAULT, NULL);


  80 
  81         SMB_OPIPE_VALID(opipe);
  82         sv = opipe->p_server;
  83         SMB_SERVER_VALID(sv);
  84 
  85         /*
  86          * This is called in the error path when opening,
  87          * in which case we close the socket here.
  88          */
  89         if (opipe->p_socket != NULL)
  90                 (void) ksocket_close(opipe->p_socket, zone_kcred());
  91 
  92         opipe->p_magic = (uint32_t)~SMB_OPIPE_MAGIC;
  93         cv_destroy(&opipe->p_cv);
  94         mutex_destroy(&opipe->p_mutex);
  95 
  96         kmem_cache_free(smb_cache_opipe, opipe);
  97 }
  98 
  99 /*
























 100  * Helper for open: build pipe name and connect.
 101  */
 102 static int
 103 smb_opipe_connect(smb_request_t *sr, smb_opipe_t *opipe)
 104 {
 105         struct sockaddr_un saddr;
 106         smb_arg_open_t  *op = &sr->sr_open;
 107         const char *name;
 108         int rc;
 109 
 110         name = op->fqi.fq_path.pn_path;
 111         name += strspn(name, "\\");
 112         if (smb_strcasecmp(name, "PIPE", 4) == 0) {
 113                 name += 4;
 114                 name += strspn(name, "\\");
 115         }
 116         (void) strlcpy(opipe->p_name, name, SMB_OPIPE_MAXNAME);
 117         (void) smb_strlwr(opipe->p_name);
 118 
 119         bzero(&saddr, sizeof (saddr));


 150         int rc;
 151 
 152         /*
 153          * Any errors building the XDR message etc.
 154          */
 155         errp->status = NT_STATUS_INTERNAL_ERROR;
 156 
 157         smb_user_netinfo_init(sr->uid_user, &nui);
 158         phdr.ph_magic = SMB_PIPE_HDR_MAGIC;
 159         phdr.ph_uilen = xdr_sizeof(smb_netuserinfo_xdr, &nui);
 160 
 161         buflen = sizeof (phdr) + phdr.ph_uilen;
 162         buf = kmem_alloc(buflen, KM_SLEEP);
 163 
 164         bcopy(&phdr, buf, sizeof (phdr));
 165         xdrmem_create(&xdrs, buf + sizeof (phdr),
 166             buflen - (sizeof (phdr)), XDR_ENCODE);
 167         if (!smb_netuserinfo_xdr(&xdrs, &nui))
 168                 goto out;
 169 
 170         /*
 171          * If we fail sending the netuserinfo or recv'ing the
 172          * status reponse, we have probably run into the limit
 173          * on the number of open pipes.  That's this status:
 174          */
 175         errp->status = NT_STATUS_PIPE_NOT_AVAILABLE;




 176 
 177         rc = ksocket_send(opipe->p_socket, buf, buflen, 0,
 178             &iocnt, sr->user_cr);
 179         if (rc == 0 && iocnt != buflen)
 180                 rc = EIO;
 181         if (rc != 0)
 182                 goto out;



 183 
 184         rc = ksocket_recv(opipe->p_socket, &status, sizeof (status), 0,
 185             &iocnt, sr->user_cr);
 186         if (rc != 0 || iocnt != sizeof (status))
 187                 goto out;












 188 

 189         /*
 190          * Return the status we read from the pipe service,
 191          * normally NT_STATUS_SUCCESS, but could be something
 192          * else like NT_STATUS_ACCESS_DENIED.
 193          */


 194         errp->status = status;













 195 
 196 out:
 197         xdr_destroy(&xdrs);
 198         kmem_free(buf, buflen);
 199         smb_user_netinfo_fini(&nui);
 200 }
 201 
 202 /*
 203  * smb_opipe_open
 204  *
 205  * Open an RPC named pipe. This routine should be called if
 206  * a file open is requested on a share of type STYPE_IPC.
 207  * If we recognize the pipe, we setup a new ofile.
 208  *
 209  * Returns 0 on success, Otherwise an NT status code.
 210  */
 211 int
 212 smb_opipe_open(smb_request_t *sr, uint32_t uniqid)
 213 {
 214         smb_arg_open_t  *op = &sr->sr_open;
 215         smb_ofile_t *ofile;
 216         smb_opipe_t *opipe;
 217         smb_error_t err;
 218 
 219         opipe = smb_opipe_alloc(sr);
 220         if (opipe == NULL)
 221                 return (NT_STATUS_INTERNAL_ERROR);
 222 
 223         if (smb_opipe_connect(sr, opipe) != 0) {
 224                 smb_opipe_dealloc(opipe);
 225                 return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
 226         }
 227 
 228         smb_opipe_send_userinfo(sr, opipe, &err);
 229         if (err.status != 0) {
 230                 smb_opipe_dealloc(opipe);
 231                 return (err.status);
 232         }
 233 
 234         /*
 235          * Note: If smb_ofile_open succeeds, the new ofile is
 236          * in the FID lists can can be used by I/O requests.


 237          */
 238         op->create_options = 0;
 239         op->pipe = opipe;
 240         ofile = smb_ofile_open(sr, NULL, op,
 241             SMB_FTYPE_MESG_PIPE, uniqid, &err);
 242         op->pipe = NULL;
 243         if (ofile == NULL) {
 244                 smb_opipe_dealloc(opipe);
 245                 return (err.status);
 246         }
 247 








 248         /* An "up" pointer, for debug. */
 249         opipe->p_ofile = ofile;
 250 
 251         op->dsize = 0x01000;
 252         op->dattr = FILE_ATTRIBUTE_NORMAL;






 253         op->ftype = SMB_FTYPE_MESG_PIPE;
 254         op->action_taken = SMB_OACT_LOCK | SMB_OACT_OPENED; /* 0x8001 */
 255         op->devstate = SMB_PIPE_READMODE_MESSAGE
 256             | SMB_PIPE_TYPE_MESSAGE
 257             | SMB_PIPE_UNLIMITED_INSTANCES; /* 0x05ff */
 258         op->fileid = ofile->f_fid;
 259 
 260         sr->smb_fid = ofile->f_fid;
 261         sr->fid_ofile = ofile;
 262 
 263         return (NT_STATUS_SUCCESS);
 264 }
 265 
 266 /*
 267  * smb_opipe_close
 268  *
 269  * Called by smb_ofile_close for pipes.
 270  *
 271  * Note: ksocket_close may block while waiting for
 272  * any I/O threads with a hold to get out.
 273  */
 274 void
 275 smb_opipe_close(smb_ofile_t *of)
 276 {
 277         smb_opipe_t *opipe;
 278         ksocket_t sock;


 355         struct nmsghdr msghdr;
 356         smb_ofile_t *ofile;
 357         smb_opipe_t *opipe;
 358         ksocket_t sock;
 359         size_t recvcnt = 0;
 360         int rc;
 361 
 362         ofile = sr->fid_ofile;
 363         ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
 364         opipe = ofile->f_pipe;
 365         SMB_OPIPE_VALID(opipe);
 366 
 367         mutex_enter(&opipe->p_mutex);
 368         sock = opipe->p_socket;
 369         if (sock != NULL)
 370                 ksocket_hold(sock);
 371         mutex_exit(&opipe->p_mutex);
 372         if (sock == NULL)
 373                 return (EBADF);
 374 
 375         bzero(&msghdr, sizeof (msghdr));
 376         msghdr.msg_iov = uio->uio_iov;
 377         msghdr.msg_iovlen = uio->uio_iovcnt;







 378 
 379         /*
 380          * This should block only if there's no data.
 381          * A single call to recvmsg does just that.
 382          * (Intentionaly no recv loop here.)
 383          */



 384         rc = ksocket_recvmsg(sock, &msghdr, 0,
 385             &recvcnt, ofile->f_cr);


















 386         if (rc != 0)
 387                 goto out;
 388 
 389         if (recvcnt == 0) {
 390                 /* Other side closed. */
 391                 rc = EPIPE;
 392                 goto out;
 393         }
 394         uio->uio_resid -= recvcnt;
 395 
 396 out:
 397         ksocket_rele(sock);
 398 
 399         return (rc);
 400 }
 401 
 402 int
 403 smb_opipe_ioctl(smb_request_t *sr, int cmd, void *arg, int *rvalp)
 404 {
 405         smb_ofile_t *ofile;


 423         rc = ksocket_ioctl(sock, cmd, (intptr_t)arg, rvalp, ofile->f_cr);
 424 
 425         ksocket_rele(sock);
 426 
 427         return (rc);
 428 }
 429 
 430 /*
 431  * Get the smb_attr_t for a named pipe.
 432  * Caller has already cleared to zero.
 433  */
 434 int
 435 smb_opipe_getattr(smb_ofile_t *of, smb_attr_t *ap)
 436 {
 437 
 438         if (of->f_pipe == NULL)
 439                 return (EINVAL);
 440 
 441         ap->sa_vattr.va_type = VFIFO;
 442         ap->sa_vattr.va_nlink = 1;

 443         ap->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
 444         ap->sa_allocsz = 0x1000LL;
 445 
 446         return (0);
 447 }
 448 
 449 int
 450 smb_opipe_getname(smb_ofile_t *of, char *buf, size_t buflen)
 451 {
 452         smb_opipe_t *opipe;
 453 
 454         if ((opipe = of->f_pipe) == NULL)
 455                 return (EINVAL);
 456 
 457         (void) snprintf(buf, buflen, "\\%s", opipe->p_name);
 458         return (0);
 459 }
 460 
 461 /*
 462  * Handler for smb2_ioctl

 463  */
 464 /* ARGSUSED */
 465 uint32_t
 466 smb_opipe_fsctl(smb_request_t *sr, smb_fsctl_t *fsctl)
 467 {
 468         uint32_t status;
 469 



 470         switch (fsctl->CtlCode) {
 471         case FSCTL_PIPE_TRANSCEIVE:
 472                 status = smb_opipe_transceive(sr, fsctl);
 473                 break;
 474 
 475         case FSCTL_PIPE_PEEK:
 476         case FSCTL_PIPE_WAIT:
 477                 /* XXX todo */
 478                 status = NT_STATUS_NOT_SUPPORTED;
 479                 break;
 480 
 481         default:
 482                 ASSERT(!"CtlCode");
 483                 status = NT_STATUS_INTERNAL_ERROR;
 484                 break;
 485         }
 486 
 487         return (status);
 488 }
 489 
 490 static uint32_t
 491 smb_opipe_transceive(smb_request_t *sr, smb_fsctl_t *fsctl)
 492 {
 493         smb_vdb_t       vdb;
 494         smb_ofile_t     *ofile;
 495         struct mbuf     *mb;
 496         uint32_t        status;
 497         int             len, rc;
 498 
 499         /*
 500          * Caller checked that this is the IPC$ share,
 501          * and that this call has a valid open handle.
 502          * Just check the type.
 503          */
 504         ofile = sr->fid_ofile;
 505         if (ofile->f_ftype != SMB_FTYPE_MESG_PIPE)
 506                 return (NT_STATUS_INVALID_HANDLE);
 507 
 508         rc = smb_mbc_decodef(fsctl->in_mbc, "#B",
 509             fsctl->InputCount, &vdb);
 510         if (rc != 0) {




   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * This module provides the interface to NDR RPC.
  28  */
  29 
  30 #include <sys/stat.h>
  31 #include <sys/uio.h>
  32 #include <sys/ksynch.h>
  33 #include <sys/stropts.h>
  34 #include <sys/socket.h>
  35 #include <sys/filio.h>
  36 #include <smbsrv/smb_kproto.h>
  37 #include <smbsrv/smb_xdr.h>
  38 #include <smb/winioctl.h>
  39 


  40 /*
  41  * Allocate a new opipe and return it, or NULL, in which case
  42  * the caller will report "internal error".
  43  */
  44 static smb_opipe_t *
  45 smb_opipe_alloc(smb_request_t *sr)
  46 {
  47         smb_server_t    *sv = sr->sr_server;
  48         smb_opipe_t     *opipe;
  49         ksocket_t       sock;
  50 
  51         if (ksocket_socket(&sock, AF_UNIX, SOCK_STREAM, 0,
  52             KSOCKET_SLEEP, sr->user_cr) != 0)
  53                 return (NULL);
  54 
  55         opipe = kmem_cache_alloc(smb_cache_opipe, KM_SLEEP);
  56 
  57         bzero(opipe, sizeof (smb_opipe_t));
  58         mutex_init(&opipe->p_mutex, NULL, MUTEX_DEFAULT, NULL);
  59         cv_init(&opipe->p_cv, NULL, CV_DEFAULT, NULL);


  78 
  79         SMB_OPIPE_VALID(opipe);
  80         sv = opipe->p_server;
  81         SMB_SERVER_VALID(sv);
  82 
  83         /*
  84          * This is called in the error path when opening,
  85          * in which case we close the socket here.
  86          */
  87         if (opipe->p_socket != NULL)
  88                 (void) ksocket_close(opipe->p_socket, zone_kcred());
  89 
  90         opipe->p_magic = (uint32_t)~SMB_OPIPE_MAGIC;
  91         cv_destroy(&opipe->p_cv);
  92         mutex_destroy(&opipe->p_mutex);
  93 
  94         kmem_cache_free(smb_cache_opipe, opipe);
  95 }
  96 
  97 /*
  98  * Unblock a request that might be blocked reading some
  99  * pipe (AF_UNIX socket).  We don't have an easy way to
 100  * interrupt just the thread servicing this request, so
 101  * we shutdown(3socket) the socket, waking all readers.
 102  * That's a bit heavy-handed, making the socket unusable
 103  * after this, so we do this only when disconnecting a
 104  * session (i.e. stopping the SMB service), and not when
 105  * handling an SMB2_cancel or SMB_nt_cancel request.
 106  */
 107 static void
 108 smb_opipe_cancel(smb_request_t *sr)
 109 {
 110         ksocket_t so;
 111 
 112         switch (sr->session->s_state) {
 113         case SMB_SESSION_STATE_DISCONNECTED:
 114         case SMB_SESSION_STATE_TERMINATED:
 115                 if ((so = sr->cancel_arg2) != NULL)
 116                         (void) ksocket_shutdown(so, SHUT_RDWR, sr->user_cr);
 117                 break;
 118         }
 119 }
 120 
 121 /*
 122  * Helper for open: build pipe name and connect.
 123  */
 124 static int
 125 smb_opipe_connect(smb_request_t *sr, smb_opipe_t *opipe)
 126 {
 127         struct sockaddr_un saddr;
 128         smb_arg_open_t  *op = &sr->sr_open;
 129         const char *name;
 130         int rc;
 131 
 132         name = op->fqi.fq_path.pn_path;
 133         name += strspn(name, "\\");
 134         if (smb_strcasecmp(name, "PIPE", 4) == 0) {
 135                 name += 4;
 136                 name += strspn(name, "\\");
 137         }
 138         (void) strlcpy(opipe->p_name, name, SMB_OPIPE_MAXNAME);
 139         (void) smb_strlwr(opipe->p_name);
 140 
 141         bzero(&saddr, sizeof (saddr));


 172         int rc;
 173 
 174         /*
 175          * Any errors building the XDR message etc.
 176          */
 177         errp->status = NT_STATUS_INTERNAL_ERROR;
 178 
 179         smb_user_netinfo_init(sr->uid_user, &nui);
 180         phdr.ph_magic = SMB_PIPE_HDR_MAGIC;
 181         phdr.ph_uilen = xdr_sizeof(smb_netuserinfo_xdr, &nui);
 182 
 183         buflen = sizeof (phdr) + phdr.ph_uilen;
 184         buf = kmem_alloc(buflen, KM_SLEEP);
 185 
 186         bcopy(&phdr, buf, sizeof (phdr));
 187         xdrmem_create(&xdrs, buf + sizeof (phdr),
 188             buflen - (sizeof (phdr)), XDR_ENCODE);
 189         if (!smb_netuserinfo_xdr(&xdrs, &nui))
 190                 goto out;
 191 
 192         mutex_enter(&sr->sr_mutex);
 193         if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
 194                 mutex_exit(&sr->sr_mutex);
 195                 errp->status = NT_STATUS_CANCELLED;
 196                 goto out;
 197         }
 198         sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
 199         sr->cancel_method = smb_opipe_cancel;
 200         sr->cancel_arg2 = opipe->p_socket;
 201         mutex_exit(&sr->sr_mutex);
 202 
 203         rc = ksocket_send(opipe->p_socket, buf, buflen, 0,
 204             &iocnt, sr->user_cr);
 205         if (rc == 0 && iocnt != buflen)
 206                 rc = EIO;
 207         if (rc == 0)
 208                 rc = ksocket_recv(opipe->p_socket, &status, sizeof (status),
 209                     0, &iocnt, sr->user_cr);
 210         if (rc == 0 && iocnt != sizeof (status))
 211                 rc = EIO;
 212 
 213         mutex_enter(&sr->sr_mutex);
 214         sr->cancel_method = NULL;
 215         sr->cancel_arg2 = NULL;
 216         switch (sr->sr_state) {
 217         case SMB_REQ_STATE_WAITING_PIPE:
 218                 sr->sr_state = SMB_REQ_STATE_ACTIVE;
 219                 break;
 220         case SMB_REQ_STATE_CANCEL_PENDING:
 221                 sr->sr_state = SMB_REQ_STATE_CANCELLED;
 222                 rc = EINTR;
 223                 break;
 224         default:
 225                 /* keep rc from above */
 226                 break;
 227         }
 228         mutex_exit(&sr->sr_mutex);
 229 
 230 
 231         /*
 232          * Return the status we read from the pipe service,
 233          * normally NT_STATUS_SUCCESS, but could be something
 234          * else like NT_STATUS_ACCESS_DENIED.
 235          */
 236         switch (rc) {
 237         case 0:
 238                 errp->status = status;
 239                 break;
 240         case EINTR:
 241                 errp->status = NT_STATUS_CANCELLED;
 242                 break;
 243         /*
 244          * If we fail sending the netuserinfo or recv'ing the
 245          * status reponse, we have probably run into the limit
 246          * on the number of open pipes.  That's this status:
 247          */
 248         default:
 249                 errp->status = NT_STATUS_PIPE_NOT_AVAILABLE;
 250                 break;
 251         }
 252 
 253 out:
 254         xdr_destroy(&xdrs);
 255         kmem_free(buf, buflen);
 256         smb_user_netinfo_fini(&nui);
 257 }
 258 
 259 /*
 260  * smb_opipe_open
 261  *
 262  * Open an RPC named pipe. This routine should be called if
 263  * a file open is requested on a share of type STYPE_IPC.
 264  * If we recognize the pipe, we setup a new ofile.
 265  *
 266  * Returns 0 on success, Otherwise an NT status code.
 267  */
 268 int
 269 smb_opipe_open(smb_request_t *sr, smb_ofile_t *ofile)
 270 {
 271         smb_arg_open_t  *op = &sr->sr_open;
 272         smb_attr_t *ap = &op->fqi.fq_fattr;
 273         smb_opipe_t *opipe;
 274         smb_error_t err;
 275 
 276         opipe = smb_opipe_alloc(sr);
 277         if (opipe == NULL)
 278                 return (NT_STATUS_INTERNAL_ERROR);
 279 
 280         if (smb_opipe_connect(sr, opipe) != 0) {
 281                 smb_opipe_dealloc(opipe);
 282                 return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
 283         }
 284 
 285         smb_opipe_send_userinfo(sr, opipe, &err);
 286         if (err.status != 0) {
 287                 smb_opipe_dealloc(opipe);
 288                 return (err.status);
 289         }
 290 
 291         /*
 292          * We might have blocked in smb_opipe_connect long enough so
 293          * a tree disconnect might have happened.  In that case, we
 294          * would be adding an ofile to a tree that's disconnecting,
 295          * which would interfere with tear-down.
 296          */
 297         if (!smb_tree_is_connected(sr->tid_tree)) {





 298                 smb_opipe_dealloc(opipe);
 299                 return (NT_STATUS_NETWORK_NAME_DELETED);
 300         }
 301 
 302         /*
 303          * Note: The new opipe is given to smb_ofile_open
 304          * via op->pipe
 305          */
 306         op->pipe = opipe;
 307         smb_ofile_open(sr, op, ofile);
 308         op->pipe = NULL;
 309 
 310         /* An "up" pointer, for debug. */
 311         opipe->p_ofile = ofile;
 312 
 313         /*
 314          * Caller expects attributes in op->fqi
 315          */
 316         (void) smb_opipe_getattr(ofile, &op->fqi.fq_fattr);
 317 
 318         op->dsize = 0;
 319         op->dattr = ap->sa_dosattr;
 320         op->fileid = ap->sa_vattr.va_nodeid;
 321         op->ftype = SMB_FTYPE_MESG_PIPE;
 322         op->action_taken = SMB_OACT_OPLOCK | SMB_OACT_OPENED;
 323         op->devstate = SMB_PIPE_READMODE_MESSAGE
 324             | SMB_PIPE_TYPE_MESSAGE
 325             | SMB_PIPE_UNLIMITED_INSTANCES; /* 0x05ff */

 326 
 327         sr->smb_fid = ofile->f_fid;
 328         sr->fid_ofile = ofile;
 329 
 330         return (NT_STATUS_SUCCESS);
 331 }
 332 
 333 /*
 334  * smb_opipe_close
 335  *
 336  * Called by smb_ofile_close for pipes.
 337  *
 338  * Note: ksocket_close may block while waiting for
 339  * any I/O threads with a hold to get out.
 340  */
 341 void
 342 smb_opipe_close(smb_ofile_t *of)
 343 {
 344         smb_opipe_t *opipe;
 345         ksocket_t sock;


 422         struct nmsghdr msghdr;
 423         smb_ofile_t *ofile;
 424         smb_opipe_t *opipe;
 425         ksocket_t sock;
 426         size_t recvcnt = 0;
 427         int rc;
 428 
 429         ofile = sr->fid_ofile;
 430         ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
 431         opipe = ofile->f_pipe;
 432         SMB_OPIPE_VALID(opipe);
 433 
 434         mutex_enter(&opipe->p_mutex);
 435         sock = opipe->p_socket;
 436         if (sock != NULL)
 437                 ksocket_hold(sock);
 438         mutex_exit(&opipe->p_mutex);
 439         if (sock == NULL)
 440                 return (EBADF);
 441 
 442         mutex_enter(&sr->sr_mutex);
 443         if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
 444                 mutex_exit(&sr->sr_mutex);
 445                 rc = EINTR;
 446                 goto out;
 447         }
 448         sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
 449         sr->cancel_method = smb_opipe_cancel;
 450         sr->cancel_arg2 = sock;
 451         mutex_exit(&sr->sr_mutex);
 452 
 453         /*
 454          * This should block only if there's no data.
 455          * A single call to recvmsg does just that.
 456          * (Intentionaly no recv loop here.)
 457          */
 458         bzero(&msghdr, sizeof (msghdr));
 459         msghdr.msg_iov = uio->uio_iov;
 460         msghdr.msg_iovlen = uio->uio_iovcnt;
 461         rc = ksocket_recvmsg(sock, &msghdr, 0,
 462             &recvcnt, ofile->f_cr);
 463 
 464         mutex_enter(&sr->sr_mutex);
 465         sr->cancel_method = NULL;
 466         sr->cancel_arg2 = NULL;
 467         switch (sr->sr_state) {
 468         case SMB_REQ_STATE_WAITING_PIPE:
 469                 sr->sr_state = SMB_REQ_STATE_ACTIVE;
 470                 break;
 471         case SMB_REQ_STATE_CANCEL_PENDING:
 472                 sr->sr_state = SMB_REQ_STATE_CANCELLED;
 473                 rc = EINTR;
 474                 break;
 475         default:
 476                 /* keep rc from above */
 477                 break;
 478         }
 479         mutex_exit(&sr->sr_mutex);
 480 
 481         if (rc != 0)
 482                 goto out;
 483 
 484         if (recvcnt == 0) {
 485                 /* Other side closed. */
 486                 rc = EPIPE;
 487                 goto out;
 488         }
 489         uio->uio_resid -= recvcnt;
 490 
 491 out:
 492         ksocket_rele(sock);
 493 
 494         return (rc);
 495 }
 496 
 497 int
 498 smb_opipe_ioctl(smb_request_t *sr, int cmd, void *arg, int *rvalp)
 499 {
 500         smb_ofile_t *ofile;


 518         rc = ksocket_ioctl(sock, cmd, (intptr_t)arg, rvalp, ofile->f_cr);
 519 
 520         ksocket_rele(sock);
 521 
 522         return (rc);
 523 }
 524 
 525 /*
 526  * Get the smb_attr_t for a named pipe.
 527  * Caller has already cleared to zero.
 528  */
 529 int
 530 smb_opipe_getattr(smb_ofile_t *of, smb_attr_t *ap)
 531 {
 532 
 533         if (of->f_pipe == NULL)
 534                 return (EINVAL);
 535 
 536         ap->sa_vattr.va_type = VFIFO;
 537         ap->sa_vattr.va_nlink = 1;
 538         ap->sa_vattr.va_nodeid = (uintptr_t)of->f_pipe;
 539         ap->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
 540         ap->sa_allocsz = SMB_PIPE_MAX_MSGSIZE;
 541 
 542         return (0);
 543 }
 544 
 545 int
 546 smb_opipe_getname(smb_ofile_t *of, char *buf, size_t buflen)
 547 {
 548         smb_opipe_t *opipe;
 549 
 550         if ((opipe = of->f_pipe) == NULL)
 551                 return (EINVAL);
 552 
 553         (void) snprintf(buf, buflen, "\\%s", opipe->p_name);
 554         return (0);
 555 }
 556 
 557 /*
 558  * Handle device type FILE_DEVICE_NAMED_PIPE
 559  * for smb2_ioctl
 560  */
 561 /* ARGSUSED */
 562 uint32_t
 563 smb_opipe_fsctl(smb_request_t *sr, smb_fsctl_t *fsctl)
 564 {
 565         uint32_t status;
 566 
 567         if (!STYPE_ISIPC(sr->tid_tree->t_res_type))
 568                 return (NT_STATUS_INVALID_DEVICE_REQUEST);
 569 
 570         switch (fsctl->CtlCode) {
 571         case FSCTL_PIPE_TRANSCEIVE:
 572                 status = smb_opipe_transceive(sr, fsctl);
 573                 break;
 574 
 575         case FSCTL_PIPE_PEEK:
 576         case FSCTL_PIPE_WAIT:
 577                 /* XXX todo */
 578                 status = NT_STATUS_NOT_SUPPORTED;
 579                 break;
 580 
 581         default:
 582                 ASSERT(!"CtlCode");
 583                 status = NT_STATUS_INTERNAL_ERROR;
 584                 break;
 585         }
 586 
 587         return (status);
 588 }
 589 
 590 uint32_t
 591 smb_opipe_transceive(smb_request_t *sr, smb_fsctl_t *fsctl)
 592 {
 593         smb_vdb_t       vdb;
 594         smb_ofile_t     *ofile;
 595         struct mbuf     *mb;
 596         uint32_t        status;
 597         int             len, rc;
 598 
 599         /*
 600          * Caller checked that this is the IPC$ share,
 601          * and that this call has a valid open handle.
 602          * Just check the type.
 603          */
 604         ofile = sr->fid_ofile;
 605         if (ofile->f_ftype != SMB_FTYPE_MESG_PIPE)
 606                 return (NT_STATUS_INVALID_HANDLE);
 607 
 608         rc = smb_mbc_decodef(fsctl->in_mbc, "#B",
 609             fsctl->InputCount, &vdb);
 610         if (rc != 0) {