smb_trantcp.c revision 6d304f342b0429b33e034c9e0b53b4a739265cf3
/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smb_trantcp.c,v 1.39 2005/03/02 01:27:44 lindak Exp $
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/autoconf.h>
#include <sys/sysmacros.h>
#ifdef APPLE
#include <sys/smb_apple.h>
#else
#include <netsmb/smb_osdep.h>
#endif
#include <netsmb/smb_conn.h>
#include <netsmb/smb_subr.h>
#include <netsmb/smb_tran.h>
#include <netsmb/smb_trantcp.h>
/*
* SMB messages are up to 64K.
* Let's leave room for two.
*/
static int smb_tcpsndbuf = 0x20000;
static int smb_tcprcvbuf = 0x20000;
static dev_t smb_tcp_dev;
/*
* Internal set sockopt for int-sized options.
* Is there a common Solaris function for this?
* Code from uts/common/rpc/clnt_cots.c
*/
static int
{
int fmode;
struct T_optmgmt_req *tor;
struct T_optmgmt_ack *toa;
int *valp;
mlen = (sizeof (struct T_optmgmt_req) +
sizeof (struct opthdr) + sizeof (int));
return (error);
/*LINTED*/
/*LINTED*/
/* LINTED */
return (error);
/*
* Wait for T_OPTMGMT_ACK
*/
fmode = 0; /* need to block */
return (error);
/*LINTED*/
return (error);
}
static void
{
int error;
NBDEBUG("no tiptr!\n");
return;
}
/*
* Failures here are not fatal -
* just log a complaint.
*
* We don't need these two:
* SO_RCVTIMEO, SO_SNDTIMEO
*/
nbp->nbp_sndbuf);
if (error)
NBDEBUG("can't set SO_SNDBUF");
nbp->nbp_rcvbuf);
if (error)
NBDEBUG("can't set SO_RCVBUF");
if (error)
NBDEBUG("can't set SO_KEEPALIVE");
if (error)
NBDEBUG("can't set TCP_NODELAY");
/* Set the connect timeout (in milliseconds). */
if (error)
NBDEBUG("can't set connect timeout");
}
/*
* Get mblks into *mpp until the data length is at least mlen.
* Note that *mpp may already contain a fragment.
*
* If we ever have to wait more than 15 sec. to read a message,
* return ETIME. (Caller will declare the VD dead.)
*/
static int
{
union T_primitives *pptr;
int error = 0;
/*
* Get the first message (fragment) if
* we don't already have a left-over.
*/
/*
* I think we still want this to return ETIME
* if nothing arrives for SMB_NBTIMO (15) sec.
* so we can report "server not responding".
* We _could_ just block here now that our
* IOD is just a reader.
*/
#if 1
/* Wait with timeout... */
events = 0;
if (error)
break;
/* file mode for recv is: */
#else
fmode = 0; /* normal (blocking) */
#endif
/* Get some more... */
continue;
if (error)
break;
/*
* Normally get M_DATA messages here,
* but have to check for other types.
*/
case M_DATA:
break;
case M_PROTO:
case M_PCPROTO:
/*LINTED*/
case T_DATA_IND:
/* remove 1st mblk, keep the rest. */
break;
case T_DISCON_IND:
/* Peer disconnected. */
NBDEBUG("T_DISCON_IND: reason=%d",
goto discon;
case T_ORDREL_IND:
/* Peer disconnecting. */
NBDEBUG("T_ORDREL_IND");
goto discon;
case T_OK_ACK:
case T_DISCON_REQ:
goto discon;
default:
goto discon;
}
default:
goto discon;
}
break; /* M_PROTO, M_PCPROTO */
default:
NBDEBUG("unexpected msg type=%d",
/*FALLTHROUGH*/
/*
* The connection is no longer usable.
* Drop this message and disconnect.
*
* Note: nb_disconnect only does t_snddis
* on the first call, but does important
* cleanup and state change on any call.
*/
return (ENOTCONN);
}
/*
* If we have a data message, append it to
* the previous chunk(s) and update dlen
*/
if (!tm)
continue;
} else {
/* Append */
;
}
}
return (error);
}
/*
* Send a T_DISCON_REQ (disconnect)
*/
static int
{
struct T_discon_req *dreq;
mlen = sizeof (struct T_discon_req);
return (error);
/*LINTED*/
return (error);
fmode = 0; /* need to block */
return (error);
}
#ifdef APPLE
static int
{
return (0);
}
#endif
/*
* Stuff the NetBIOS header into space already prepended.
*/
static int
{
uint32_t *p;
len &= 0x1FFFF;
/*LINTED*/
return (0);
}
/*
* Note: Moved name encoding into here.
*/
static int
{
int i, len;
/*
* Do the NetBIOS "first-level encoding" here.
* (RFC1002 explains this wierdness...)
* See similar code in smbfs library:
*
* Here is what we marshall:
* uint8_t NAME_LENGTH (always 32)
* uint8_t ENCODED_NAME[32]
* uint8_t SCOPE_LENGTH
* XXX Scope should follow here, then another null,
* if and when we support NetBIOS scopes.
*/
if (!p)
return (ENOSR);
/* NAME_LENGTH */
*p++ = (2 * NB_NAMELEN);
/* ENCODED_NAME */
for (i = 0; i < NB_NAMELEN; i++) {
}
/* SCOPE_LENGTH */
*p++ = 0;
return (0);
}
static int
{
if (!smb_tcp_dev) {
}
/*
* This magic arranges for our network endpoint
* to have the right "label" for operation in a
* "trusted extensions" environment.
*/
if (is_system_labeled()) {
} else {
}
if (err)
return (err);
/* Note: I_PUSH "timod" is done by t_kopen */
/* Save the TPI handle we use everywhere. */
/*
* Internal ktli calls need the "fmode" flags
* from the t_kopen call. XXX: Not sure if the
* flags have the right bits set, or if we
* XXX: Look into this...
*/
return (0);
}
/*ARGSUSED*/
static int
{
int error;
return (EBADF);
return (EISCONN);
/*
* Setup (snd)call address (connect to).
* Just pass NULL for the (rcv)call.
*/
/* call.opt - none */
/* call.udata -- XXX: Should put NB session req here! */
/* Send the connect, wait... */
if (error) {
} else {
}
return (error);
}
static int
{
struct sockaddr_in sin;
if (error)
return (error);
/*
* Put a zero for the 4-byte NetBIOS header,
* then let nb_sethdr() overwrite it.
*/
mb_put_uint32le(mbp, 0);
if (error)
return (error);
NBDEBUG("initial request timeout\n");
return (ETIMEDOUT);
}
if (error) {
return (error);
}
/*
* Process NETBIOS reply
*/
if (m0)
error = 0;
if (rpcode == NB_SSN_POSRESP) {
goto out;
}
if (rpcode != NB_SSN_RTGRESP) {
goto out;
}
if (rplen != 6) {
goto out;
}
if (!error)
if (error) {
}
out:
if (m0)
return (error);
}
/*
* Wait for up to 15 sec. for the next packet.
* Often return ETIME and do nothing else.
* When a packet header is available, check
* the header and get the length, but don't
* consume it. No side effects here except
* for the pullupmsg call.
*/
static int
{
int error;
/*
* Get the first message (fragment) if
* we don't already have a left-over.
*/
if (error)
return (error);
return (ENOSR);
/*
* Check the NetBIOS header.
* (NOT consumed here)
*/
/*LINTED*/
return (EPIPE);
}
switch (*rpcodep) {
case NB_SSN_MESSAGE:
case NB_SSN_REQUEST:
case NB_SSN_POSRESP:
case NB_SSN_NEGRESP:
case NB_SSN_RTGRESP:
case NB_SSN_KEEPALIVE:
break;
default:
return (EPIPE);
}
len &= 0x1ffff;
if (len > SMB_MAXPKTLEN) {
return (EFBIG);
}
return (0);
}
/*
* Receive a NetBIOS message. This may block to wait for the entire
* message to arrive. The caller knows there is (or should be) a
* message to be read. When we receive and drop a keepalive or
* zero-length message, return EAGAIN so the caller knows that
* something was received. This avoids false triggering of the
* "server not responding" state machine.
*/
/*ARGSUSED*/
static int
{
int error;
/* We should be the only reader. */
return (ENOTCONN);
return (EBADF);
if (mpp) {
if (*mpp) {
NBDEBUG("*mpp not 0 - leak?");
}
}
/*
* Get the NetBIOS header (not consumed yet)
*/
if (error) {
return (error);
}
NBDEBUG("Have pkt, type=0x%x len=0x%x\n",
/*
* Block here waiting for the whole packet to arrive.
* If we get a timeout, return without side effects.
* The data length we wait for here includes both the
* NetBIOS header and the payload.
*/
if (error) {
return (error);
}
/*
* We now have an entire NetBIOS message.
* Trim off the NetBIOS header and consume it.
* Note: _peekhdr has done pullupmsg for us,
* so we know it's safe to advance b_rptr.
*/
/*
* There may be more data after the message
* we're about to return, in which case we
* split it and leave the remainder.
*/
/*
* No session is established.
* Return whatever packet we got.
*/
goto out;
}
/*
* A session is established; the only packets
* we should see are session message and
* keep-alive packets. Drop anything else.
*/
switch (rpcode) {
case NB_SSN_KEEPALIVE:
/*
* It's a keepalive. Discard any data in it
* (there's not supposed to be any, but that
* doesn't mean some server won't send some)
*/
if (len)
break;
case NB_SSN_MESSAGE:
/*
* Session message. Does it have any data?
*/
if (len == 0) {
/*
* No data - treat as keepalive (drop).
*/
break;
}
/*
* Yes, has data. Return it.
*/
error = 0;
break;
default:
/*
* Drop anything else.
*/
break;
}
out:
if (error) {
if (m0)
return (error);
}
if (mpp)
else
return (0);
}
/*
* SMB transport interface
*/
static int
{
int error;
/*
* We don't keep reference counts or otherwise
* prevent nbp->nbp_tiptr from going away, so
* do the TLI open here and keep it until the
* last ref calls smb_nbst_done.
* This does t_kopen (open endpoint)
*/
if (error) {
return (error);
}
return (0);
}
/*ARGSUSED*/
static int
{
return (ENOTCONN);
/*
* Don't really need to disconnect here,
* because the close following will do it.
* But it's harmless.
*/
return (0);
}
/*ARGSUSED*/
static int
{
struct sockaddr_nb *snb;
int error = 0;
return (EBADF);
/*
* Allow repeated bind calls on one endpoint.
* This happens with reconnect.
*/
/*
* Null name is an "anonymous" (NULL) bind request.
* (Let the transport pick a local name.)
* This transport does not support NULL bind,
* because we require a local NetBIOS name.
*/
return (EINVAL);
/*LINTED*/
return (ENOMEM);
/*
* Do local TCP bind with NULL (any address),
* but just once (for multiple connect attempts)
* or extra bind calls would cause errors.
*/
if (error) {
NBDEBUG("t_kbind failed");
} else {
}
}
return (error);
}
static int
{
struct sockaddr_in sin;
struct sockaddr_nb *snb;
int error;
return (EBADF);
return (EINVAL);
/*
* Note: nbssn_rq_request() will call nbssn_recv(),
* so set the RECVLOCK flag here. Otherwise we'll
* hit an ASSERT for this flag in nbssn_recv().
*/
NBDEBUG("attempt to reenter session layer!\n");
return (EWOULDBLOCK);
}
/*LINTED*/
goto out;
}
/*
* Setup the remote IP address.
* Try plain TCP first (port 445).
*/
switch (error) {
case 0:
break;
case ECONNREFUSED:
/* Try again w/ NetBIOS (port 139) */
goto again;
}
/* FALLTHROUGH */
default:
goto out;
}
/*
* If we connected via NetBIOS (port 139),
* need to do a session request.
*/
if (error)
} else
out:
return (error);
}
/*ARGSUSED*/
static int
{
return (ENOTCONN);
return (nb_disconnect(nbp));
}
static int
{
int save_flags;
return (EBADF);
}
if (save_flags & NBF_CONNECTED)
}
return (0);
}
/*
* Always consume the message.
* (On error too!)
*/
/*ARGSUSED*/
static int
{
int error;
goto errout;
}
/*
* Get the message length, which
* does NOT include the NetBIOS header
*/
/*
* Normally, mb_init() will have left space
* for us to prepend the NetBIOS header in
* the data block of the first mblk.
* However, we have to check in case other
* code did not leave this space, or if the
* message is from dupmsg (db_ref > 1)
*
* If don't find room in the first data block,
* we have to allocb a new message and link it
* on the front of the chain. We try not to
* do this becuase it's less efficient. Also,
* some network drivers will apparently send
* each mblk in the chain as separate frames.
* (That's arguably a driver bug.)
*/
/* We can use the first dblk. */
m->b_rptr -= 4;
} else {
/* Link a new mblk on the head. */
/* M_PREPEND */
if (!m0)
goto errout;
m = m0;
}
return (error);
if (m)
m_freem(m);
return (error);
}
static int
{
NBDEBUG("attempt to reenter session layer!\n");
return (EWOULDBLOCK);
}
return (error);
}
/*
* Wait for up to "ticks" clock ticks for input on vcp.
* Returns zero if input is available, otherwise ETIME
* indicating time expired, or other error codes.
*/
/*ARGSUSED*/
static int
{
int error;
int events = 0;
return (error);
}
static int
{
switch (param) {
case SMBTP_SNDSZ:
break;
case SMBTP_RCVSZ:
break;
case SMBTP_TIMEOUT:
break;
#ifdef SMBTP_SELECTID
case SMBTP_SELECTID:
break;
#endif
#ifdef SMBTP_UPCALL
case SMBTP_UPCALL:
break;
#endif
default:
return (EINVAL);
}
return (0);
}
/*ARGSUSED*/
static int
{
return (EINVAL);
}
/*
* Check for fatal errors
*/
/*ARGSUSED*/
static int
{
switch (error) {
case ENOTCONN:
case ENETRESET:
case ECONNABORTED:
case EPIPE:
return (1);
}
return (0);
}
struct smb_tran_desc smb_tran_nbtcp_desc = {
};