#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/resourcevar.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/socketvar2.h>
#include <sys/socketops.h>
#include <sys/poll.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <sys/sysctl.h>
#include <sys/thread2.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/mchain.h>
#include "netbios.h"
#include "smb.h"
#include "smb_conn.h"
#include "smb_tran.h"
#include "smb_trantcp.h"
#include "smb_subr.h"
#define M_NBDATA M_PCB
static int smb_tcpsndbuf = 10 * 1024;
static int smb_tcprcvbuf = 10 * 1024;
SYSCTL_DECL(_net_smb);
SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, "");
SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, "");
#define nb_sosend(so,m,flags,p) \
so_pru_sosend(so, NULL, NULL, m, NULL, flags, td)
static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
u_int8_t *rpcodep, struct thread *td);
static int smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td);
static int
nb_setsockopt_int(struct socket *so, int level, int name, int val)
{
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);
}
static int
nb_intr(struct nbpcb *nbp, struct thread *td)
{
return 0;
}
static void
nb_upcall(struct socket *so, void *arg, int waitflag)
{
struct nbpcb *nbp = arg;
if (arg == NULL || nbp->nbp_selectid == NULL)
return;
wakeup(nbp->nbp_selectid);
}
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
nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
{
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 thread *td)
{
struct socket *so;
int error;
error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP, td);
if (error)
return error;
nbp->nbp_tso = so;
so->so_upcallarg = (caddr_t)nbp;
so->so_upcall = nb_upcall;
atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
so->so_rcv.ssb_timeo = (5 * hz);
so->so_snd.ssb_timeo = (5 * hz);
error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf,
&td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
if (error)
goto bad;
nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
atomic_clear_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
atomic_clear_int(&so->so_snd.ssb_flags, SSB_NOINTR);
error = soconnect(so, (struct sockaddr*)to, td, TRUE);
atomic_set_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
atomic_set_int(&so->so_snd.ssb_flags, SSB_NOINTR);
if (error)
goto bad;
crit_enter();
while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
tsleep(&so->so_timeo, 0, "nbcon", 2 * hz);
if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
(error = nb_intr(nbp, td)) != 0) {
soclrstate(so, SS_ISCONNECTING);
crit_exit();
goto bad;
}
}
if (so->so_error) {
error = so->so_error;
so->so_error = 0;
crit_exit();
goto bad;
}
crit_exit();
return 0;
bad:
smb_nbst_disconnect(nbp->nbp_vc, td);
return error;
}
static int
nbssn_rq_request(struct nbpcb *nbp, struct thread *td)
{
struct mbchain mb, *mbp = &mb;
struct mdchain md, *mdp = &md;
struct mbuf *m0;
struct sockaddr_in sin;
u_short port;
u_int8_t rpcode;
int error, rplen, res;
error = mb_init(mbp);
if (error)
return error;
mb_put_uint32le(mbp, 0);
nb_put_name(mbp, nbp->nbp_paddr);
nb_put_name(mbp, nbp->nbp_laddr);
nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td);
if (!error) {
nbp->nbp_state = NBST_RQSENT;
}
mb_detach(mbp);
mb_done(mbp);
if (error)
return error;
error = socket_wait(nbp->nbp_tso, &nbp->nbp_timo, &res);
if (error == EWOULDBLOCK) {
NBDEBUG("initial request timeout\n");
return ETIMEDOUT;
}
if (error)
return error;
error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td);
if (error) {
NBDEBUG("recv() error %d\n", error);
return error;
}
if (m0)
md_initm(mdp, m0);
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;
}
md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
md_get_uint16(mdp, &port);
sin.sin_port = port;
nbp->nbp_state = NBST_RETARGET;
smb_nbst_disconnect(nbp->nbp_vc, td);
error = nb_connect_in(nbp, &sin, td);
if (!error)
error = nbssn_rq_request(nbp, td);
if (error) {
smb_nbst_disconnect(nbp->nbp_vc, td);
break;
}
} while(0);
if (m0)
md_done(mdp);
return error;
}
static int
nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
u_int8_t *rpcodep, int flags, struct thread *td)
{
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_td = td;
error = so_pru_soreceive(so, NULL, &auio, NULL, NULL, &flags);
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 thread *td)
{
struct socket *so = nbp->nbp_tso;
struct sockbuf sio;
int error, rcvflg;
int savelen = 0;
u_int8_t rpcode = 0;
if (so == NULL)
return ENOTCONN;
sbinit(&sio, 0);
if (mpp)
*mpp = NULL;
for(;;) {
error = nbssn_recvhdr(nbp, &savelen, &rpcode, MSG_DONTWAIT, td);
if (so->so_state &
(SS_ISDISCONNECTING | SS_ISDISCONNECTED | SS_CANTRCVMORE)) {
nbp->nbp_state = NBST_CLOSED;
NBDEBUG("session closed by peer\n");
error = ECONNRESET;
break;
}
if (error)
break;
if (savelen == 0 && nbp->nbp_state != NBST_SESSION)
break;
if (rpcode == NB_SSN_KEEPALIVE)
continue;
do {
sbinit(&sio, savelen);
rcvflg = MSG_WAITALL;
error = so_pru_soreceive(so, NULL, NULL, &sio,
NULL, &rcvflg);
} while (error == EWOULDBLOCK || error == EINTR ||
error == ERESTART);
if (error)
break;
if (sio.sb_cc != savelen) {
SMBERROR("packet is shorter than expected\n");
error = EPIPE;
m_freem(sio.sb_mb);
break;
}
if (nbp->nbp_state == NBST_SESSION && rpcode == NB_SSN_MESSAGE)
break;
NBDEBUG("non-session packet %x\n", rpcode);
m_freem(sio.sb_mb);
sio.sb_mb = NULL;
sio.sb_cc = 0;
}
if (error == 0) {
if (mpp)
*mpp = sio.sb_mb;
else
m_freem(sio.sb_mb);
*lenp = sio.sb_cc;
*rpcodep = rpcode;
}
return (error);
}
static int
smb_nbst_create(struct smb_vc *vcp, struct thread *td)
{
struct nbpcb *nbp;
nbp = kmalloc(sizeof *nbp, M_NBDATA, M_WAITOK | M_ZERO);
nbp->nbp_timo.tv_sec = 15;
nbp->nbp_state = NBST_CLOSED;
nbp->nbp_vc = vcp;
nbp->nbp_sndbuf = smb_tcpsndbuf;
nbp->nbp_rcvbuf = smb_tcprcvbuf;
vcp->vc_tdata = nbp;
return 0;
}
static int
smb_nbst_done(struct smb_vc *vcp, struct thread *td)
{
struct nbpcb *nbp = vcp->vc_tdata;
if (nbp == NULL)
return ENOTCONN;
smb_nbst_disconnect(vcp, td);
if (nbp->nbp_laddr)
kfree(nbp->nbp_laddr, M_SONAME);
if (nbp->nbp_paddr)
kfree(nbp->nbp_paddr, M_SONAME);
kfree(nbp, M_NBDATA);
return 0;
}
static int
smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
{
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;
if (sap == NULL)
break;
slen = sap->sa_len;
if (slen < NB_MINSALEN)
break;
snb = (struct sockaddr_nb*)dup_sockaddr(sap);
if (snb == NULL) {
error = ENOMEM;
break;
}
nbp->nbp_laddr = snb;
nbp->nbp_flags |= NBF_LOCADDR;
error = 0;
} while(0);
return error;
}
static int
smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
{
struct nbpcb *nbp = vcp->vc_tdata;
struct sockaddr_in sin;
struct sockaddr_nb *snb;
struct timespec ts1, ts2;
int error, slen;
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) {
kfree(nbp->nbp_paddr, M_SONAME);
nbp->nbp_paddr = NULL;
}
snb = (struct sockaddr_nb*)dup_sockaddr(sap);
if (snb == NULL)
return ENOMEM;
nbp->nbp_paddr = snb;
sin = snb->snb_addrin;
getnanotime(&ts1);
error = nb_connect_in(nbp, &sin, td);
if (error)
return error;
getnanotime(&ts2);
timespecsub(&ts2, &ts1, &ts2);
if (ts2.tv_sec == 0) {
ts2.tv_sec = 1;
ts2.tv_nsec = 0;
}
timespecadd(&ts2, &ts2, &nbp->nbp_timo);
timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
error = nbssn_rq_request(nbp, td);
if (error)
smb_nbst_disconnect(vcp, td);
return error;
}
static int
smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td)
{
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 = NULL;
soshutdown(so, SHUT_RDWR);
soclose(so, FNONBLOCK);
}
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 thread *td)
{
struct nbpcb *nbp = vcp->vc_tdata;
int error;
if (nbp->nbp_state != NBST_SESSION) {
error = ENOTCONN;
goto abort;
}
M_PREPEND(m0, 4, M_WAITOK);
nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
error = nb_sosend(nbp->nbp_tso, m0, 0, td);
return error;
abort:
if (m0)
m_freem(m0);
return error;
}
static int
smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td)
{
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, td);
nbp->nbp_flags &= ~NBF_RECVLOCK;
return error;
}
static void
smb_nbst_timo(struct smb_vc *vcp)
{
return;
}
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
smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
{
struct nbpcb *nbp = vcp->vc_tdata;
switch (param) {
case SMBTP_SNDSZ:
*(int*)data = nbp->nbp_sndbuf;
break;
case SMBTP_RCVSZ:
*(int*)data = nbp->nbp_rcvbuf;
break;
case SMBTP_TIMEOUT:
*(struct timespec*)data = nbp->nbp_timo;
break;
default:
return EINVAL;
}
return 0;
}
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;
}
static int
smb_nbst_fatal(struct smb_vc *vcp, int error)
{
switch (error) {
case ENOTCONN:
case ENETRESET:
case ECONNABORTED:
return 1;
}
return 0;
}
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,
smb_nbst_getparam, smb_nbst_setparam,
smb_nbst_fatal
};