2003-02-25 12:12:11 +03:00
|
|
|
/* $NetBSD: smb_trantcp.c,v 1.8 2003/02/25 09:12:11 jdolecek Exp $ */
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Copyright (c) 2000-2001 Boris Popov
|
2000-12-07 06:48:09 +03:00
|
|
|
* 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
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 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.
|
2002-01-04 05:39:37 +03:00
|
|
|
*
|
|
|
|
* FreeBSD: src/sys/netsmb/smb_trantcp.c,v 1.4 2001/12/02 08:47:29 bp Exp
|
2000-12-07 06:48:09 +03:00
|
|
|
*/
|
2003-02-25 12:12:11 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__KERNEL_RCSID(0, "$NetBSD: smb_trantcp.c,v 1.8 2003/02/25 09:12:11 jdolecek Exp $");
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/mchain.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
#include <netsmb/netbios.h>
|
|
|
|
|
|
|
|
#include <netsmb/smb.h>
|
|
|
|
#include <netsmb/smb_conn.h>
|
|
|
|
#include <netsmb/smb_tran.h>
|
|
|
|
#include <netsmb/smb_trantcp.h>
|
|
|
|
#include <netsmb/smb_subr.h>
|
|
|
|
|
|
|
|
#define M_NBDATA M_PCB
|
|
|
|
|
2003-02-21 23:12:05 +03:00
|
|
|
static int nb_tcpsndbuf = NB_SNDQ;
|
|
|
|
static int nb_tcprcvbuf = NB_RCVQ;
|
|
|
|
static const struct timeval nb_timo = { 15, 0 }; /* XXX sysctl? */
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
2000-12-07 06:48:09 +03:00
|
|
|
SYSCTL_DECL(_net_smb);
|
2003-02-21 23:12:05 +03:00
|
|
|
SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &nb_tcpsndbuf, 0, "");
|
|
|
|
SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &nb_tcprcvbuf, 0, "");
|
2000-12-07 06:48:09 +03:00
|
|
|
#endif
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
|
|
|
#define nb_sosend(so,m,flags,p) (so)->so_proto->pr_usrreqs->pru_sosend( \
|
2000-12-07 06:48:09 +03:00
|
|
|
so, NULL, 0, m, 0, flags, p)
|
|
|
|
#else
|
2002-01-04 05:39:37 +03:00
|
|
|
#define nb_sosend(so,m,flags,p) (*(so)->so_send)(so, NULL, (struct uio *)0, \
|
|
|
|
m, (struct mbuf *)0, flags)
|
2000-12-07 06:48:09 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
|
|
|
|
u_int8_t *rpcodep, struct proc *p);
|
|
|
|
static int smb_nbst_disconnect(struct smb_vc *vcp, struct proc *p);
|
|
|
|
|
|
|
|
static int
|
|
|
|
nb_setsockopt_int(struct socket *so, int level, int name, int val)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
return sosetopt(so, level, name, NULL); /* XXX */
|
|
|
|
#else
|
2000-12-07 06:48:09 +03:00
|
|
|
struct sockopt sopt;
|
|
|
|
|
|
|
|
bzero(&sopt, sizeof(sopt));
|
|
|
|
sopt.sopt_level = level;
|
|
|
|
sopt.sopt_name = name;
|
|
|
|
sopt.sopt_val = &val;
|
|
|
|
sopt.sopt_valsize = sizeof(val);
|
|
|
|
return sosetopt(so, &sopt);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
nb_poll(struct nbpcb *nbp, int events, struct proc *p)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
|
|
|
return nbp->nbp_tso->so_proto->pr_usrreqs->pru_sopoll(nbp->nbp_tso,
|
|
|
|
events, NULL, p);
|
2000-12-07 06:48:09 +03:00
|
|
|
#else
|
2003-02-21 23:12:05 +03:00
|
|
|
/* XXX this is exactly equal to soo_poll() */
|
|
|
|
struct socket *so = nbp->nbp_tso;
|
|
|
|
int revents = 0;
|
|
|
|
int s = splsoftnet();
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2003-02-21 23:12:05 +03:00
|
|
|
if (events & (POLLIN | POLLRDNORM))
|
|
|
|
if (soreadable(so))
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
|
|
|
|
if (events & (POLLOUT | POLLWRNORM))
|
|
|
|
if (sowritable(so))
|
|
|
|
revents |= events & (POLLOUT | POLLWRNORM);
|
|
|
|
|
|
|
|
if (events & (POLLPRI | POLLRDBAND))
|
|
|
|
if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
|
|
|
|
revents |= events & (POLLPRI | POLLRDBAND);
|
|
|
|
|
|
|
|
if (revents == 0) {
|
|
|
|
if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
|
|
|
|
selrecord(p, &so->so_rcv.sb_sel);
|
|
|
|
so->so_rcv.sb_flags |= SB_SEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & (POLLOUT | POLLWRNORM)) {
|
|
|
|
selrecord(p, &so->so_snd.sb_sel);
|
|
|
|
so->so_snd.sb_flags |= SB_SEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return (revents);
|
2002-01-04 05:39:37 +03:00
|
|
|
#endif
|
2003-02-21 23:12:05 +03:00
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
static int
|
2003-02-21 23:12:05 +03:00
|
|
|
nbssn_rselect(struct nbpcb *nbp, const struct timeval *tv, int events,
|
2002-01-04 05:39:37 +03:00
|
|
|
struct proc *p)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2003-02-21 23:12:05 +03:00
|
|
|
struct timeval atv;
|
|
|
|
extern int nselcoll;
|
|
|
|
int ncoll;
|
2002-01-04 05:39:37 +03:00
|
|
|
int timo, error;
|
2003-02-21 23:12:05 +03:00
|
|
|
int s;
|
2003-02-16 02:26:57 +03:00
|
|
|
struct lwp *l = curlwp;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
if (tv) {
|
|
|
|
atv = *tv;
|
2003-02-22 00:12:24 +03:00
|
|
|
if (itimerfix(&atv))
|
|
|
|
return (EINVAL);
|
2000-12-07 06:48:09 +03:00
|
|
|
s = splclock();
|
|
|
|
timeradd(&atv, &time, &atv);
|
|
|
|
splx(s);
|
2003-02-21 23:12:05 +03:00
|
|
|
timo = hzto(&atv);
|
|
|
|
if (timo <= 0)
|
|
|
|
return (EWOULDBLOCK);
|
2002-01-04 05:39:37 +03:00
|
|
|
} else
|
2000-12-07 06:48:09 +03:00
|
|
|
timo = 0;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2003-02-21 23:12:05 +03:00
|
|
|
retry:
|
|
|
|
ncoll = nselcoll;
|
2003-02-16 02:26:57 +03:00
|
|
|
l->l_flag |= L_SELECT;
|
2000-12-07 06:48:09 +03:00
|
|
|
error = nb_poll(nbp, events, p);
|
|
|
|
if (error) {
|
|
|
|
error = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
if (tv) {
|
2003-02-21 23:12:05 +03:00
|
|
|
/*
|
|
|
|
* We have to recalculate the timeout on every retry.
|
|
|
|
*/
|
|
|
|
timo = hzto(&atv);
|
|
|
|
if (timo <= 0) {
|
|
|
|
error = EWOULDBLOCK;
|
2002-01-04 05:39:37 +03:00
|
|
|
goto done;
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2003-02-21 23:12:05 +03:00
|
|
|
|
|
|
|
s = splsched();
|
|
|
|
if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
|
|
|
|
splx(s);
|
|
|
|
goto retry;
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
2003-02-21 23:12:05 +03:00
|
|
|
l->l_flag &= ~L_SELECT;
|
|
|
|
error = tsleep((caddr_t)&selwait, PSOCK, "nbssn_rselect", timo);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
goto retry;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
done:
|
2003-02-16 02:26:57 +03:00
|
|
|
l->l_flag &= ~L_SELECT;
|
2003-02-22 00:12:24 +03:00
|
|
|
/* select is not restarted after signals... */
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error == ERESTART)
|
2003-02-22 00:12:24 +03:00
|
|
|
error = 0;
|
|
|
|
return (error);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nb_intr(struct nbpcb *nbp, struct proc *p)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static void
|
|
|
|
nb_upcall(struct socket *so, caddr_t arg, int waitflag)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = (void *)arg;
|
|
|
|
|
|
|
|
if (arg == NULL || nbp->nbp_selectid == NULL)
|
|
|
|
return;
|
|
|
|
wakeup(nbp->nbp_selectid);
|
|
|
|
}
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
static int
|
|
|
|
nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
|
|
|
|
{
|
|
|
|
u_int32_t *p = mtod(m, u_int32_t *);
|
|
|
|
|
|
|
|
*p = htonl((len & 0x1FFFF) | (type << 24));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-01-04 05:39:37 +03:00
|
|
|
nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
u_char seglen, *cp;
|
|
|
|
|
|
|
|
cp = snb->snb_name;
|
|
|
|
if (*cp == 0)
|
|
|
|
return EINVAL;
|
|
|
|
NBDEBUG("[%s]\n", cp);
|
|
|
|
for (;;) {
|
|
|
|
seglen = (*cp) + 1;
|
|
|
|
error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
if (seglen == 1)
|
|
|
|
break;
|
|
|
|
cp += seglen;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct proc *p)
|
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
int error, s;
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifdef __NetBSD__
|
2000-12-07 06:48:09 +03:00
|
|
|
struct mbuf *m;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
nbp->nbp_tso = so;
|
2002-01-04 05:39:37 +03:00
|
|
|
so->so_upcallarg = (caddr_t)nbp;
|
|
|
|
so->so_upcall = nb_upcall;
|
|
|
|
so->so_rcv.sb_flags |= SB_UPCALL;
|
2003-02-21 23:12:05 +03:00
|
|
|
so->so_rcv.sb_timeo = NB_SNDTIMEO;
|
|
|
|
so->so_snd.sb_timeo = NB_RCVTIMEO;
|
|
|
|
error = soreserve(so, nb_tcpsndbuf, nb_tcprcvbuf);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
|
|
|
|
nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
|
|
|
|
so->so_rcv.sb_flags &= ~SB_NOINTR;
|
|
|
|
so->so_snd.sb_flags &= ~SB_NOINTR;
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
2000-12-07 06:48:09 +03:00
|
|
|
error = soconnect(so, (struct sockaddr*)to, p);
|
|
|
|
#else
|
|
|
|
MGET(m, M_WAIT, MT_SONAME);
|
2002-01-04 05:39:37 +03:00
|
|
|
*mtod(m, struct sockaddr *) = *(struct sockaddr *)to;
|
2000-12-07 06:48:09 +03:00
|
|
|
m->m_len = sizeof(struct sockaddr);
|
|
|
|
error = soconnect(so, m);
|
|
|
|
m_free(m);
|
|
|
|
#endif
|
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
s = splnet();
|
|
|
|
while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
|
|
|
|
tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
|
|
|
|
if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
|
|
|
|
(error = nb_intr(nbp, p)) != 0) {
|
|
|
|
so->so_state &= ~SS_ISCONNECTING;
|
|
|
|
splx(s);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (so->so_error) {
|
|
|
|
error = so->so_error;
|
|
|
|
so->so_error = 0;
|
|
|
|
splx(s);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return 0;
|
|
|
|
bad:
|
|
|
|
smb_nbst_disconnect(nbp->nbp_vc, p);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nbssn_rq_request(struct nbpcb *nbp, struct proc *p)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct mbchain mb, *mbp = &mb;
|
|
|
|
struct mdchain md, *mdp = &md;
|
2000-12-07 06:48:09 +03:00
|
|
|
struct mbuf *m0;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
u_short port;
|
|
|
|
u_int8_t rpcode;
|
|
|
|
int error, rplen;
|
|
|
|
|
|
|
|
error = mb_init(mbp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint32le(mbp, 0);
|
2003-02-21 23:12:05 +03:00
|
|
|
(void) nb_put_name(mbp, nbp->nbp_paddr);
|
|
|
|
(void) nb_put_name(mbp, nbp->nbp_laddr);
|
2000-12-07 06:48:09 +03:00
|
|
|
nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
|
|
|
|
error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, p);
|
|
|
|
if (!error) {
|
|
|
|
nbp->nbp_state = NBST_RQSENT;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_detach(mbp);
|
2000-12-07 06:48:09 +03:00
|
|
|
mb_done(mbp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2003-02-21 23:12:05 +03:00
|
|
|
error = nbssn_rselect(nbp, &nb_timo, POLLIN, p);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error == EWOULDBLOCK) { /* Timeout */
|
|
|
|
NBDEBUG("initial request timeout\n");
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
if (error) /* restart or interrupt */
|
|
|
|
return error;
|
|
|
|
error = nbssn_recv(nbp, &m0, &rplen, &rpcode, p);
|
|
|
|
if (error) {
|
|
|
|
NBDEBUG("recv() error %d\n", error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Process NETBIOS reply
|
|
|
|
*/
|
|
|
|
if (m0)
|
2002-01-04 05:39:37 +03:00
|
|
|
md_initm(mdp, m0);
|
2000-12-07 06:48:09 +03:00
|
|
|
error = 0;
|
|
|
|
do {
|
|
|
|
if (rpcode == NB_SSN_POSRESP) {
|
|
|
|
nbp->nbp_state = NBST_SESSION;
|
|
|
|
nbp->nbp_flags |= NBF_CONNECTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rpcode != NB_SSN_RTGRESP) {
|
|
|
|
error = ECONNABORTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rplen != 6) {
|
|
|
|
error = ECONNABORTED;
|
|
|
|
break;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
|
|
|
|
md_get_uint16(mdp, &port);
|
2000-12-07 06:48:09 +03:00
|
|
|
sin.sin_port = port;
|
|
|
|
nbp->nbp_state = NBST_RETARGET;
|
|
|
|
smb_nbst_disconnect(nbp->nbp_vc, p);
|
|
|
|
error = nb_connect_in(nbp, &sin, p);
|
|
|
|
if (!error)
|
|
|
|
error = nbssn_rq_request(nbp, p);
|
|
|
|
if (error) {
|
|
|
|
smb_nbst_disconnect(nbp->nbp_vc, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(0);
|
2002-01-04 05:39:37 +03:00
|
|
|
if (m0)
|
|
|
|
md_done(mdp);
|
2000-12-07 06:48:09 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
|
|
|
|
u_int8_t *rpcodep, int flags, struct proc *p)
|
|
|
|
{
|
|
|
|
struct socket *so = nbp->nbp_tso;
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aio;
|
|
|
|
u_int32_t len;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
aio.iov_base = (caddr_t)&len;
|
|
|
|
aio.iov_len = sizeof(len);
|
|
|
|
auio.uio_iov = &aio;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_offset = 0;
|
|
|
|
auio.uio_resid = sizeof(len);
|
|
|
|
auio.uio_procp = p;
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
2000-12-07 06:48:09 +03:00
|
|
|
error = so->so_proto->pr_usrreqs->pru_soreceive
|
|
|
|
(so, (struct sockaddr **)NULL, &auio,
|
|
|
|
(struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
|
|
|
|
#else
|
|
|
|
error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
|
2002-01-04 05:39:37 +03:00
|
|
|
(struct mbuf **)NULL,
|
|
|
|
(struct mbuf **)NULL, &flags);
|
2000-12-07 06:48:09 +03:00
|
|
|
#endif
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
if (auio.uio_resid > 0) {
|
|
|
|
SMBSDEBUG("short reply\n");
|
|
|
|
return EPIPE;
|
|
|
|
}
|
|
|
|
len = ntohl(len);
|
|
|
|
*rpcodep = (len >> 24) & 0xFF;
|
|
|
|
len &= 0x1ffff;
|
|
|
|
if (len > SMB_MAXPKTLEN) {
|
|
|
|
SMBERROR("packet too long (%d)\n", len);
|
|
|
|
return EFBIG;
|
|
|
|
}
|
|
|
|
*lenp = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
|
|
|
|
u_int8_t *rpcodep, struct proc *p)
|
|
|
|
{
|
|
|
|
struct socket *so = nbp->nbp_tso;
|
|
|
|
struct uio auio;
|
|
|
|
struct mbuf *m;
|
|
|
|
u_int8_t rpcode;
|
|
|
|
int len;
|
|
|
|
int error, rcvflg;
|
|
|
|
|
|
|
|
if (so == NULL)
|
|
|
|
return ENOTCONN;
|
|
|
|
|
|
|
|
if (mpp)
|
|
|
|
*mpp = NULL;
|
|
|
|
for(;;) {
|
|
|
|
m = NULL;
|
2002-01-04 05:39:37 +03:00
|
|
|
error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, p);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (so->so_state &
|
|
|
|
(SS_ISDISCONNECTING | SS_ISDISCONNECTED | SS_CANTRCVMORE)) {
|
|
|
|
nbp->nbp_state = NBST_CLOSED;
|
|
|
|
NBDEBUG("session closed by peer\n");
|
|
|
|
return ECONNRESET;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
if (len == 0 && nbp->nbp_state != NBST_SESSION)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
if (rpcode == NB_SSN_KEEPALIVE)
|
|
|
|
continue;
|
2000-12-07 06:48:09 +03:00
|
|
|
bzero(&auio, sizeof(auio));
|
|
|
|
auio.uio_resid = len;
|
|
|
|
auio.uio_procp = p;
|
|
|
|
do {
|
|
|
|
rcvflg = MSG_WAITALL;
|
2002-01-04 05:39:37 +03:00
|
|
|
#ifndef __NetBSD__
|
2000-12-07 06:48:09 +03:00
|
|
|
error = so->so_proto->pr_usrreqs->pru_soreceive
|
|
|
|
(so, (struct sockaddr **)NULL,
|
|
|
|
&auio, &m, (struct mbuf **)NULL, &rcvflg);
|
|
|
|
#else
|
|
|
|
error = (*so->so_receive)(so, (struct mbuf **)0,
|
2002-01-04 05:39:37 +03:00
|
|
|
&auio, &m,
|
|
|
|
(struct mbuf **)NULL,
|
|
|
|
&rcvflg);
|
2000-12-07 06:48:09 +03:00
|
|
|
#endif
|
|
|
|
} while (error == EWOULDBLOCK || error == EINTR ||
|
|
|
|
error == ERESTART);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
if (auio.uio_resid > 0) {
|
|
|
|
SMBERROR("packet is shorter than expected\n");
|
|
|
|
error = EPIPE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nbp->nbp_state == NBST_SESSION &&
|
|
|
|
rpcode == NB_SSN_MESSAGE)
|
|
|
|
break;
|
|
|
|
NBDEBUG("non-session packet %x\n", rpcode);
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (mpp)
|
|
|
|
*mpp = m;
|
|
|
|
else
|
|
|
|
m_freem(m);
|
|
|
|
*lenp = len;
|
|
|
|
*rpcodep = rpcode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SMB transport interface
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_nbst_create(struct smb_vc *vcp, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp;
|
|
|
|
|
|
|
|
MALLOC(nbp, struct nbpcb *, sizeof *nbp, M_NBDATA, M_WAITOK);
|
2003-02-21 23:12:05 +03:00
|
|
|
memset(nbp, 0, sizeof *nbp);
|
2000-12-07 06:48:09 +03:00
|
|
|
nbp->nbp_state = NBST_CLOSED;
|
|
|
|
nbp->nbp_vc = vcp;
|
|
|
|
vcp->vc_tdata = nbp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_done(struct smb_vc *vcp, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
|
|
|
|
if (nbp == NULL)
|
|
|
|
return ENOTCONN;
|
|
|
|
smb_nbst_disconnect(vcp, p);
|
|
|
|
if (nbp->nbp_laddr)
|
|
|
|
free(nbp->nbp_laddr, M_SONAME);
|
|
|
|
if (nbp->nbp_paddr)
|
|
|
|
free(nbp->nbp_paddr, M_SONAME);
|
|
|
|
free(nbp, M_NBDATA);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
struct sockaddr_nb *snb;
|
|
|
|
int error, slen;
|
|
|
|
|
|
|
|
NBDEBUG("\n");
|
|
|
|
error = EINVAL;
|
|
|
|
do {
|
|
|
|
if (nbp->nbp_flags & NBF_LOCADDR)
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* It is possible to create NETBIOS name in the kernel,
|
|
|
|
* but nothing prevents us to do it in the user space.
|
|
|
|
*/
|
|
|
|
if (sap == NULL)
|
|
|
|
break;
|
|
|
|
slen = sap->sa_len;
|
|
|
|
if (slen < NB_MINSALEN)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
|
|
|
|
if (snb == NULL) {
|
|
|
|
error = ENOMEM;
|
2000-12-07 06:48:09 +03:00
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
nbp->nbp_laddr = snb;
|
|
|
|
nbp->nbp_flags |= NBF_LOCADDR;
|
2002-01-04 05:39:37 +03:00
|
|
|
error = 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
} while(0);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_nb *snb;
|
2002-01-04 05:39:37 +03:00
|
|
|
int error, slen;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
NBDEBUG("\n");
|
|
|
|
if (nbp->nbp_tso != NULL)
|
|
|
|
return EISCONN;
|
|
|
|
if (nbp->nbp_laddr == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
slen = sap->sa_len;
|
|
|
|
if (slen < NB_MINSALEN)
|
|
|
|
return EINVAL;
|
|
|
|
if (nbp->nbp_paddr) {
|
|
|
|
free(nbp->nbp_paddr, M_SONAME);
|
|
|
|
nbp->nbp_paddr = NULL;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
|
|
|
|
if (snb == NULL)
|
|
|
|
return ENOMEM;
|
2000-12-07 06:48:09 +03:00
|
|
|
nbp->nbp_paddr = snb;
|
|
|
|
sin = snb->snb_addrin;
|
|
|
|
error = nb_connect_in(nbp, &sin, p);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = nbssn_rq_request(nbp, p);
|
|
|
|
if (error)
|
|
|
|
smb_nbst_disconnect(vcp, p);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_disconnect(struct smb_vc *vcp, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
struct socket *so;
|
|
|
|
|
|
|
|
if (nbp == NULL || nbp->nbp_tso == NULL)
|
|
|
|
return ENOTCONN;
|
|
|
|
if ((so = nbp->nbp_tso) != NULL) {
|
|
|
|
nbp->nbp_flags &= ~NBF_CONNECTED;
|
|
|
|
nbp->nbp_tso = (struct socket *)NULL;
|
|
|
|
soshutdown(so, 2);
|
|
|
|
soclose(so);
|
|
|
|
}
|
|
|
|
if (nbp->nbp_state != NBST_RETARGET) {
|
|
|
|
nbp->nbp_state = NBST_CLOSED;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (nbp->nbp_state != NBST_SESSION) {
|
|
|
|
error = ENOTCONN;
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
M_PREPEND(m0, 4, M_WAITOK);
|
|
|
|
if (m0 == NULL)
|
|
|
|
return ENOBUFS;
|
|
|
|
nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
|
|
|
|
error = nb_sosend(nbp->nbp_tso, m0, 0, p);
|
|
|
|
return error;
|
|
|
|
abort:
|
|
|
|
if (m0)
|
|
|
|
m_freem(m0);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct proc *p)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
u_int8_t rpcode;
|
|
|
|
int error, rplen;
|
|
|
|
|
|
|
|
nbp->nbp_flags |= NBF_RECVLOCK;
|
|
|
|
error = nbssn_recv(nbp, mpp, &rplen, &rpcode, p);
|
|
|
|
nbp->nbp_flags &= ~NBF_RECVLOCK;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
smb_nbst_timo(struct smb_vc *vcp)
|
|
|
|
{
|
2003-02-21 23:12:05 +03:00
|
|
|
|
|
|
|
/* Nothing */
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
smb_nbst_intr(struct smb_vc *vcp)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
|
|
|
|
if (nbp == NULL || nbp->nbp_tso == NULL)
|
|
|
|
return;
|
|
|
|
sorwakeup(nbp->nbp_tso);
|
|
|
|
sowwakeup(nbp->nbp_tso);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
switch (param) {
|
2003-02-21 23:12:05 +03:00
|
|
|
case SMBTP_SNDSZ:
|
|
|
|
*(int*)data = nb_tcpsndbuf;
|
2002-01-04 05:39:37 +03:00
|
|
|
break;
|
2003-02-21 23:12:05 +03:00
|
|
|
case SMBTP_RCVSZ:
|
|
|
|
*(int*)data = nb_tcprcvbuf;
|
2002-01-04 05:39:37 +03:00
|
|
|
break;
|
2003-02-21 23:12:05 +03:00
|
|
|
case SMBTP_TIMEOUT:
|
|
|
|
*(struct timeval*)data = nb_timo;
|
2002-01-04 05:39:37 +03:00
|
|
|
break;
|
2003-02-21 23:12:05 +03:00
|
|
|
default:
|
2002-01-04 05:39:37 +03:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static int
|
|
|
|
smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
|
|
|
|
{
|
|
|
|
struct nbpcb *nbp = vcp->vc_tdata;
|
|
|
|
|
|
|
|
switch (param) {
|
|
|
|
case SMBTP_SELECTID:
|
|
|
|
nbp->nbp_selectid = data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for fatal errors
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_nbst_fatal(struct smb_vc *vcp, int error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case ENOTCONN:
|
|
|
|
case ENETRESET:
|
|
|
|
case ECONNABORTED:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
struct smb_tran_desc smb_tran_nbtcp_desc = {
|
|
|
|
SMBT_NBTCP,
|
|
|
|
smb_nbst_create, smb_nbst_done,
|
|
|
|
smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
|
|
|
|
smb_nbst_send, smb_nbst_recv,
|
|
|
|
smb_nbst_timo, smb_nbst_intr,
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_nbst_getparam, smb_nbst_setparam,
|
|
|
|
smb_nbst_fatal
|
2000-12-07 06:48:09 +03:00
|
|
|
};
|
|
|
|
|