1c398230bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov 5681a5bbeSBoris Popov * All rights reserved. 6681a5bbeSBoris Popov * 7681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without 8681a5bbeSBoris Popov * modification, are permitted provided that the following conditions 9681a5bbeSBoris Popov * are met: 10681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright 11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer. 12681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright 13681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the 14681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution. 15681a5bbeSBoris Popov * 16681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26681a5bbeSBoris Popov * SUCH DAMAGE. 27681a5bbeSBoris Popov */ 28ab0de15bSDavid E. O'Brien 29ab0de15bSDavid E. O'Brien #include <sys/cdefs.h> 30ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 31ab0de15bSDavid E. O'Brien 32681a5bbeSBoris Popov #include <sys/param.h> 33960ed29cSSeigo Tanimura #include <sys/condvar.h> 34681a5bbeSBoris Popov #include <sys/kernel.h> 35960ed29cSSeigo Tanimura #include <sys/lock.h> 36681a5bbeSBoris Popov #include <sys/malloc.h> 37681a5bbeSBoris Popov #include <sys/mbuf.h> 38960ed29cSSeigo Tanimura #include <sys/poll.h> 39681a5bbeSBoris Popov #include <sys/proc.h> 40681a5bbeSBoris Popov #include <sys/protosw.h> 41960ed29cSSeigo Tanimura #include <sys/signalvar.h> 42681a5bbeSBoris Popov #include <sys/socket.h> 43681a5bbeSBoris Popov #include <sys/socketvar.h> 44960ed29cSSeigo Tanimura #include <sys/sx.h> 45681a5bbeSBoris Popov #include <sys/sysctl.h> 46960ed29cSSeigo Tanimura #include <sys/systm.h> 47960ed29cSSeigo Tanimura #include <sys/uio.h> 48681a5bbeSBoris Popov 49681a5bbeSBoris Popov #include <net/if.h> 50681a5bbeSBoris Popov #include <net/route.h> 5190e19ee8SDavide Italiano #include <net/vnet.h> 52681a5bbeSBoris Popov 53681a5bbeSBoris Popov #include <netinet/in.h> 54681a5bbeSBoris Popov #include <netinet/tcp.h> 55681a5bbeSBoris Popov 56681a5bbeSBoris Popov #include <sys/mchain.h> 57681a5bbeSBoris Popov 58681a5bbeSBoris Popov #include <netsmb/netbios.h> 59681a5bbeSBoris Popov 60681a5bbeSBoris Popov #include <netsmb/smb.h> 61681a5bbeSBoris Popov #include <netsmb/smb_conn.h> 62681a5bbeSBoris Popov #include <netsmb/smb_tran.h> 63681a5bbeSBoris Popov #include <netsmb/smb_trantcp.h> 64681a5bbeSBoris Popov #include <netsmb/smb_subr.h> 65681a5bbeSBoris Popov 66681a5bbeSBoris Popov #define M_NBDATA M_PCB 67681a5bbeSBoris Popov 683c304004SBoris Popov static int smb_tcpsndbuf = NB_SNDQ - 1; 693c304004SBoris Popov static int smb_tcprcvbuf = NB_RCVQ - 1; 70681a5bbeSBoris Popov 71681a5bbeSBoris Popov SYSCTL_DECL(_net_smb); 72681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, ""); 73681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, ""); 74681a5bbeSBoris Popov 75b0668f71SRobert Watson #define nb_sosend(so,m,flags,td) sosend(so, NULL, 0, m, 0, flags, td) 76681a5bbeSBoris Popov 77681a5bbeSBoris Popov static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp, 78fce6fbfaSBoris Popov u_int8_t *rpcodep, struct thread *td); 79fce6fbfaSBoris Popov static int smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td); 80681a5bbeSBoris Popov 81681a5bbeSBoris Popov static int 82681a5bbeSBoris Popov nb_setsockopt_int(struct socket *so, int level, int name, int val) 83681a5bbeSBoris Popov { 84681a5bbeSBoris Popov struct sockopt sopt; 8590e19ee8SDavide Italiano int error; 86681a5bbeSBoris Popov 87681a5bbeSBoris Popov bzero(&sopt, sizeof(sopt)); 88681a5bbeSBoris Popov sopt.sopt_level = level; 89681a5bbeSBoris Popov sopt.sopt_name = name; 90681a5bbeSBoris Popov sopt.sopt_val = &val; 91681a5bbeSBoris Popov sopt.sopt_valsize = sizeof(val); 9290e19ee8SDavide Italiano CURVNET_SET(so->so_vnet); 9390e19ee8SDavide Italiano error = sosetopt(so, &sopt); 9490e19ee8SDavide Italiano CURVNET_RESTORE(); 9590e19ee8SDavide Italiano return error; 96681a5bbeSBoris Popov } 97681a5bbeSBoris Popov 98681a5bbeSBoris Popov static int 99681a5bbeSBoris Popov nb_intr(struct nbpcb *nbp, struct proc *p) 100681a5bbeSBoris Popov { 101681a5bbeSBoris Popov return 0; 102681a5bbeSBoris Popov } 103681a5bbeSBoris Popov 10474fb0ba7SJohn Baldwin static int 105681a5bbeSBoris Popov nb_upcall(struct socket *so, void *arg, int waitflag) 106681a5bbeSBoris Popov { 107681a5bbeSBoris Popov struct nbpcb *nbp = arg; 108681a5bbeSBoris Popov 109681a5bbeSBoris Popov if (arg == NULL || nbp->nbp_selectid == NULL) 11074fb0ba7SJohn Baldwin return (SU_OK); 111681a5bbeSBoris Popov wakeup(nbp->nbp_selectid); 11274fb0ba7SJohn Baldwin return (SU_OK); 113681a5bbeSBoris Popov } 114681a5bbeSBoris Popov 115681a5bbeSBoris Popov static int 116681a5bbeSBoris Popov nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len) 117681a5bbeSBoris Popov { 118681a5bbeSBoris Popov u_int32_t *p = mtod(m, u_int32_t *); 119681a5bbeSBoris Popov 120681a5bbeSBoris Popov *p = htonl((len & 0x1FFFF) | (type << 24)); 121681a5bbeSBoris Popov return 0; 122681a5bbeSBoris Popov } 123681a5bbeSBoris Popov 124681a5bbeSBoris Popov static int 125681a5bbeSBoris Popov nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb) 126681a5bbeSBoris Popov { 127681a5bbeSBoris Popov int error; 128681a5bbeSBoris Popov u_char seglen, *cp; 129681a5bbeSBoris Popov 130681a5bbeSBoris Popov cp = snb->snb_name; 131681a5bbeSBoris Popov if (*cp == 0) 132681a5bbeSBoris Popov return EINVAL; 133681a5bbeSBoris Popov NBDEBUG("[%s]\n", cp); 134681a5bbeSBoris Popov for (;;) { 135681a5bbeSBoris Popov seglen = (*cp) + 1; 136681a5bbeSBoris Popov error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM); 137681a5bbeSBoris Popov if (error) 138681a5bbeSBoris Popov return error; 139681a5bbeSBoris Popov if (seglen == 1) 140681a5bbeSBoris Popov break; 141681a5bbeSBoris Popov cp += seglen; 142681a5bbeSBoris Popov } 143681a5bbeSBoris Popov return 0; 144681a5bbeSBoris Popov } 145681a5bbeSBoris Popov 146681a5bbeSBoris Popov static int 147fce6fbfaSBoris Popov nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct thread *td) 148681a5bbeSBoris Popov { 149681a5bbeSBoris Popov struct socket *so; 150681a5bbeSBoris Popov int error, s; 151681a5bbeSBoris Popov 1529c4d63daSRobert Watson error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP, 153a854ed98SJohn Baldwin td->td_ucred, td); 154681a5bbeSBoris Popov if (error) 155681a5bbeSBoris Popov return error; 156681a5bbeSBoris Popov nbp->nbp_tso = so; 1579535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 15874fb0ba7SJohn Baldwin soupcall_set(so, SO_RCV, nb_upcall, nbp); 1599535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 16009c7b5a4SGleb Smirnoff so->so_rcv.sb_timeo = (5 * SBT_1S); 16109c7b5a4SGleb Smirnoff so->so_snd.sb_timeo = (5 * SBT_1S); 162681a5bbeSBoris Popov error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf); 163681a5bbeSBoris Popov if (error) 164681a5bbeSBoris Popov goto bad; 165681a5bbeSBoris Popov nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1); 166681a5bbeSBoris Popov nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1); 1679535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 168681a5bbeSBoris Popov so->so_rcv.sb_flags &= ~SB_NOINTR; 1699535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1709535efc0SRobert Watson SOCKBUF_LOCK(&so->so_snd); 171681a5bbeSBoris Popov so->so_snd.sb_flags &= ~SB_NOINTR; 1729535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 173fce6fbfaSBoris Popov error = soconnect(so, (struct sockaddr*)to, td); 174681a5bbeSBoris Popov if (error) 175681a5bbeSBoris Popov goto bad; 176681a5bbeSBoris Popov s = splnet(); 177681a5bbeSBoris Popov while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 1784cc20ab1SSeigo Tanimura tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz); 179681a5bbeSBoris Popov if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 && 180fce6fbfaSBoris Popov (error = nb_intr(nbp, td->td_proc)) != 0) { 181681a5bbeSBoris Popov so->so_state &= ~SS_ISCONNECTING; 182681a5bbeSBoris Popov splx(s); 183681a5bbeSBoris Popov goto bad; 184681a5bbeSBoris Popov } 185681a5bbeSBoris Popov } 186681a5bbeSBoris Popov if (so->so_error) { 187681a5bbeSBoris Popov error = so->so_error; 188681a5bbeSBoris Popov so->so_error = 0; 189681a5bbeSBoris Popov splx(s); 190681a5bbeSBoris Popov goto bad; 191681a5bbeSBoris Popov } 192681a5bbeSBoris Popov splx(s); 193681a5bbeSBoris Popov return 0; 194681a5bbeSBoris Popov bad: 195fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td); 196681a5bbeSBoris Popov return error; 197681a5bbeSBoris Popov } 198681a5bbeSBoris Popov 199681a5bbeSBoris Popov static int 200fce6fbfaSBoris Popov nbssn_rq_request(struct nbpcb *nbp, struct thread *td) 201681a5bbeSBoris Popov { 202afe09751SDavide Italiano struct mbchain *mbp; 203afe09751SDavide Italiano struct mdchain *mdp; 204681a5bbeSBoris Popov struct mbuf *m0; 205681a5bbeSBoris Popov struct timeval tv; 206681a5bbeSBoris Popov struct sockaddr_in sin; 207681a5bbeSBoris Popov u_short port; 208681a5bbeSBoris Popov u_int8_t rpcode; 209681a5bbeSBoris Popov int error, rplen; 210681a5bbeSBoris Popov 211afe09751SDavide Italiano mbp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK); 212afe09751SDavide Italiano mdp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK); 213681a5bbeSBoris Popov error = mb_init(mbp); 214afe09751SDavide Italiano if (error) { 215afe09751SDavide Italiano free(mbp, M_NBDATA); 216afe09751SDavide Italiano free(mdp, M_NBDATA); 217681a5bbeSBoris Popov return error; 218afe09751SDavide Italiano } 219681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 220681a5bbeSBoris Popov nb_put_name(mbp, nbp->nbp_paddr); 221681a5bbeSBoris Popov nb_put_name(mbp, nbp->nbp_laddr); 222681a5bbeSBoris Popov nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4); 223fce6fbfaSBoris Popov error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td); 224681a5bbeSBoris Popov if (!error) { 225681a5bbeSBoris Popov nbp->nbp_state = NBST_RQSENT; 226681a5bbeSBoris Popov } 227681a5bbeSBoris Popov mb_detach(mbp); 228681a5bbeSBoris Popov mb_done(mbp); 229afe09751SDavide Italiano free(mbp, M_NBDATA); 230afe09751SDavide Italiano if (error) { 231afe09751SDavide Italiano free(mdp, M_NBDATA); 232681a5bbeSBoris Popov return error; 233afe09751SDavide Italiano } 234681a5bbeSBoris Popov TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo); 235ace8398dSJeff Roberson error = selsocket(nbp->nbp_tso, POLLIN, &tv, td); 236681a5bbeSBoris Popov if (error == EWOULDBLOCK) { /* Timeout */ 237681a5bbeSBoris Popov NBDEBUG("initial request timeout\n"); 238afe09751SDavide Italiano free(mdp, M_NBDATA); 239681a5bbeSBoris Popov return ETIMEDOUT; 240681a5bbeSBoris Popov } 241afe09751SDavide Italiano if (error) { /* restart or interrupt */ 242afe09751SDavide Italiano free(mdp, M_NBDATA); 243681a5bbeSBoris Popov return error; 244afe09751SDavide Italiano } 245fce6fbfaSBoris Popov error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td); 246681a5bbeSBoris Popov if (error) { 247681a5bbeSBoris Popov NBDEBUG("recv() error %d\n", error); 248afe09751SDavide Italiano free(mdp, M_NBDATA); 249681a5bbeSBoris Popov return error; 250681a5bbeSBoris Popov } 251681a5bbeSBoris Popov /* 252681a5bbeSBoris Popov * Process NETBIOS reply 253681a5bbeSBoris Popov */ 254681a5bbeSBoris Popov if (m0) 255681a5bbeSBoris Popov md_initm(mdp, m0); 256681a5bbeSBoris Popov error = 0; 257681a5bbeSBoris Popov do { 258681a5bbeSBoris Popov if (rpcode == NB_SSN_POSRESP) { 259681a5bbeSBoris Popov nbp->nbp_state = NBST_SESSION; 260681a5bbeSBoris Popov nbp->nbp_flags |= NBF_CONNECTED; 261681a5bbeSBoris Popov break; 262681a5bbeSBoris Popov } 263681a5bbeSBoris Popov if (rpcode != NB_SSN_RTGRESP) { 264681a5bbeSBoris Popov error = ECONNABORTED; 265681a5bbeSBoris Popov break; 266681a5bbeSBoris Popov } 267681a5bbeSBoris Popov if (rplen != 6) { 268681a5bbeSBoris Popov error = ECONNABORTED; 269681a5bbeSBoris Popov break; 270681a5bbeSBoris Popov } 271681a5bbeSBoris Popov md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM); 272681a5bbeSBoris Popov md_get_uint16(mdp, &port); 273681a5bbeSBoris Popov sin.sin_port = port; 274681a5bbeSBoris Popov nbp->nbp_state = NBST_RETARGET; 275fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td); 276fce6fbfaSBoris Popov error = nb_connect_in(nbp, &sin, td); 277681a5bbeSBoris Popov if (!error) 278fce6fbfaSBoris Popov error = nbssn_rq_request(nbp, td); 279681a5bbeSBoris Popov if (error) { 280fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td); 281681a5bbeSBoris Popov break; 282681a5bbeSBoris Popov } 283681a5bbeSBoris Popov } while(0); 284681a5bbeSBoris Popov if (m0) 285681a5bbeSBoris Popov md_done(mdp); 286afe09751SDavide Italiano free(mdp, M_NBDATA); 287681a5bbeSBoris Popov return error; 288681a5bbeSBoris Popov } 289681a5bbeSBoris Popov 290681a5bbeSBoris Popov static int 291681a5bbeSBoris Popov nbssn_recvhdr(struct nbpcb *nbp, int *lenp, 292fce6fbfaSBoris Popov u_int8_t *rpcodep, int flags, struct thread *td) 293681a5bbeSBoris Popov { 294681a5bbeSBoris Popov struct socket *so = nbp->nbp_tso; 295681a5bbeSBoris Popov struct uio auio; 296681a5bbeSBoris Popov struct iovec aio; 297681a5bbeSBoris Popov u_int32_t len; 298681a5bbeSBoris Popov int error; 299681a5bbeSBoris Popov 300681a5bbeSBoris Popov aio.iov_base = (caddr_t)&len; 301681a5bbeSBoris Popov aio.iov_len = sizeof(len); 302681a5bbeSBoris Popov auio.uio_iov = &aio; 303681a5bbeSBoris Popov auio.uio_iovcnt = 1; 304681a5bbeSBoris Popov auio.uio_segflg = UIO_SYSSPACE; 305681a5bbeSBoris Popov auio.uio_rw = UIO_READ; 306681a5bbeSBoris Popov auio.uio_offset = 0; 307681a5bbeSBoris Popov auio.uio_resid = sizeof(len); 308fce6fbfaSBoris Popov auio.uio_td = td; 30990e19ee8SDavide Italiano CURVNET_SET(so->so_vnet); 310b0668f71SRobert Watson error = soreceive(so, (struct sockaddr **)NULL, &auio, 311681a5bbeSBoris Popov (struct mbuf **)NULL, (struct mbuf **)NULL, &flags); 31290e19ee8SDavide Italiano CURVNET_RESTORE(); 313681a5bbeSBoris Popov if (error) 314681a5bbeSBoris Popov return error; 315681a5bbeSBoris Popov if (auio.uio_resid > 0) { 316681a5bbeSBoris Popov SMBSDEBUG("short reply\n"); 317681a5bbeSBoris Popov return EPIPE; 318681a5bbeSBoris Popov } 319681a5bbeSBoris Popov len = ntohl(len); 320681a5bbeSBoris Popov *rpcodep = (len >> 24) & 0xFF; 321681a5bbeSBoris Popov len &= 0x1ffff; 322681a5bbeSBoris Popov if (len > SMB_MAXPKTLEN) { 323681a5bbeSBoris Popov SMBERROR("packet too long (%d)\n", len); 324681a5bbeSBoris Popov return EFBIG; 325681a5bbeSBoris Popov } 326681a5bbeSBoris Popov *lenp = len; 327681a5bbeSBoris Popov return 0; 328681a5bbeSBoris Popov } 329681a5bbeSBoris Popov 330681a5bbeSBoris Popov static int 331681a5bbeSBoris Popov nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp, 332fce6fbfaSBoris Popov u_int8_t *rpcodep, struct thread *td) 333681a5bbeSBoris Popov { 334681a5bbeSBoris Popov struct socket *so = nbp->nbp_tso; 335681a5bbeSBoris Popov struct uio auio; 3363c304004SBoris Popov struct mbuf *m, *tm, *im; 337681a5bbeSBoris Popov u_int8_t rpcode; 3383c304004SBoris Popov int len, resid; 339681a5bbeSBoris Popov int error, rcvflg; 340681a5bbeSBoris Popov 341681a5bbeSBoris Popov if (so == NULL) 342681a5bbeSBoris Popov return ENOTCONN; 343681a5bbeSBoris Popov 344681a5bbeSBoris Popov if (mpp) 345681a5bbeSBoris Popov *mpp = NULL; 346681a5bbeSBoris Popov m = NULL; 3473c304004SBoris Popov for(;;) { 3483c304004SBoris Popov /* 3493c304004SBoris Popov * Poll for a response header. 3503c304004SBoris Popov * If we don't have one waiting, return. 3513c304004SBoris Popov */ 3522d494bc6SMatt Jacob len = 0; 3532d494bc6SMatt Jacob rpcode = 0; 354fce6fbfaSBoris Popov error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, td); 355c0b99ffaSRobert Watson if ((so->so_state & (SS_ISDISCONNECTING | SS_ISDISCONNECTED)) || 356c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_CANTRCVMORE)) { 357681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED; 358681a5bbeSBoris Popov NBDEBUG("session closed by peer\n"); 359681a5bbeSBoris Popov return ECONNRESET; 360681a5bbeSBoris Popov } 361681a5bbeSBoris Popov if (error) 362681a5bbeSBoris Popov return error; 363681a5bbeSBoris Popov if (len == 0 && nbp->nbp_state != NBST_SESSION) 364681a5bbeSBoris Popov break; 3653c304004SBoris Popov /* no data, try again */ 366681a5bbeSBoris Popov if (rpcode == NB_SSN_KEEPALIVE) 367681a5bbeSBoris Popov continue; 3683c304004SBoris Popov 3693c304004SBoris Popov /* 3703c304004SBoris Popov * Loop, blocking, for data following the response header. 3713c304004SBoris Popov * 3723c304004SBoris Popov * Note that we can't simply block here with MSG_WAITALL for the 3733c304004SBoris Popov * entire response size, as it may be larger than the TCP 3743c304004SBoris Popov * slow-start window that the sender employs. This will result 3753c304004SBoris Popov * in the sender stalling until the delayed ACK is sent, then 3763c304004SBoris Popov * resuming slow-start, resulting in very poor performance. 3773c304004SBoris Popov * 3783c304004SBoris Popov * Instead, we never request more than NB_SORECEIVE_CHUNK 3793c304004SBoris Popov * bytes at a time, resulting in an ack being pushed by 3803c304004SBoris Popov * the TCP code at the completion of each call. 3813c304004SBoris Popov */ 3823c304004SBoris Popov resid = len; 3833c304004SBoris Popov while (resid > 0) { 3843c304004SBoris Popov tm = NULL; 3853c304004SBoris Popov rcvflg = MSG_WAITALL; 386681a5bbeSBoris Popov bzero(&auio, sizeof(auio)); 3873c304004SBoris Popov auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK); 388fce6fbfaSBoris Popov auio.uio_td = td; 3893c304004SBoris Popov resid -= auio.uio_resid; 3903c304004SBoris Popov /* 3913c304004SBoris Popov * Spin until we have collected everything in 3923c304004SBoris Popov * this chunk. 3933c304004SBoris Popov */ 394681a5bbeSBoris Popov do { 395681a5bbeSBoris Popov rcvflg = MSG_WAITALL; 39690e19ee8SDavide Italiano CURVNET_SET(so->so_vnet); 397b0668f71SRobert Watson error = soreceive(so, (struct sockaddr **)NULL, 398bd32b702STim J. Robbins &auio, &tm, (struct mbuf **)NULL, &rcvflg); 39990e19ee8SDavide Italiano CURVNET_RESTORE(); 400681a5bbeSBoris Popov } while (error == EWOULDBLOCK || error == EINTR || 401681a5bbeSBoris Popov error == ERESTART); 402681a5bbeSBoris Popov if (error) 4033c304004SBoris Popov goto out; 4043c304004SBoris Popov /* short return guarantees unhappiness */ 405681a5bbeSBoris Popov if (auio.uio_resid > 0) { 406681a5bbeSBoris Popov SMBERROR("packet is shorter than expected\n"); 407681a5bbeSBoris Popov error = EPIPE; 4083c304004SBoris Popov goto out; 409681a5bbeSBoris Popov } 4103c304004SBoris Popov /* append received chunk to previous chunk(s) */ 4113c304004SBoris Popov if (m == NULL) { 4123c304004SBoris Popov m = tm; 4133c304004SBoris Popov } else { 4143c304004SBoris Popov /* 4153c304004SBoris Popov * Just glue the new chain on the end. 4163c304004SBoris Popov * Consumer will pullup as required. 4173c304004SBoris Popov */ 4183c304004SBoris Popov for (im = m; im->m_next != NULL; im = im->m_next) 4193c304004SBoris Popov ; 4203c304004SBoris Popov im->m_next = tm; 4213c304004SBoris Popov } 4223c304004SBoris Popov } 4233c304004SBoris Popov /* got a session/message packet? */ 424681a5bbeSBoris Popov if (nbp->nbp_state == NBST_SESSION && 425681a5bbeSBoris Popov rpcode == NB_SSN_MESSAGE) 426681a5bbeSBoris Popov break; 4273c304004SBoris Popov /* drop packet and try for another */ 428681a5bbeSBoris Popov NBDEBUG("non-session packet %x\n", rpcode); 4293c304004SBoris Popov if (m) { 430681a5bbeSBoris Popov m_freem(m); 4313c304004SBoris Popov m = NULL; 432681a5bbeSBoris Popov } 4333c304004SBoris Popov } 4343c304004SBoris Popov 4353c304004SBoris Popov out: 436681a5bbeSBoris Popov if (error) { 437681a5bbeSBoris Popov if (m) 438681a5bbeSBoris Popov m_freem(m); 439681a5bbeSBoris Popov return error; 440681a5bbeSBoris Popov } 441681a5bbeSBoris Popov if (mpp) 442681a5bbeSBoris Popov *mpp = m; 443681a5bbeSBoris Popov else 444681a5bbeSBoris Popov m_freem(m); 445681a5bbeSBoris Popov *lenp = len; 446681a5bbeSBoris Popov *rpcodep = rpcode; 447681a5bbeSBoris Popov return 0; 448681a5bbeSBoris Popov } 449681a5bbeSBoris Popov 450681a5bbeSBoris Popov /* 451681a5bbeSBoris Popov * SMB transport interface 452681a5bbeSBoris Popov */ 453681a5bbeSBoris Popov static int 454fce6fbfaSBoris Popov smb_nbst_create(struct smb_vc *vcp, struct thread *td) 455681a5bbeSBoris Popov { 456681a5bbeSBoris Popov struct nbpcb *nbp; 457681a5bbeSBoris Popov 4581ede983cSDag-Erling Smørgrav nbp = malloc(sizeof *nbp, M_NBDATA, M_WAITOK); 459681a5bbeSBoris Popov bzero(nbp, sizeof *nbp); 460681a5bbeSBoris Popov nbp->nbp_timo.tv_sec = 15; /* XXX: sysctl ? */ 461681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED; 462681a5bbeSBoris Popov nbp->nbp_vc = vcp; 463681a5bbeSBoris Popov nbp->nbp_sndbuf = smb_tcpsndbuf; 464681a5bbeSBoris Popov nbp->nbp_rcvbuf = smb_tcprcvbuf; 465681a5bbeSBoris Popov vcp->vc_tdata = nbp; 466681a5bbeSBoris Popov return 0; 467681a5bbeSBoris Popov } 468681a5bbeSBoris Popov 469681a5bbeSBoris Popov static int 470fce6fbfaSBoris Popov smb_nbst_done(struct smb_vc *vcp, struct thread *td) 471681a5bbeSBoris Popov { 472681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 473681a5bbeSBoris Popov 474681a5bbeSBoris Popov if (nbp == NULL) 475681a5bbeSBoris Popov return ENOTCONN; 476fce6fbfaSBoris Popov smb_nbst_disconnect(vcp, td); 477681a5bbeSBoris Popov if (nbp->nbp_laddr) 478681a5bbeSBoris Popov free(nbp->nbp_laddr, M_SONAME); 479681a5bbeSBoris Popov if (nbp->nbp_paddr) 480681a5bbeSBoris Popov free(nbp->nbp_paddr, M_SONAME); 481681a5bbeSBoris Popov free(nbp, M_NBDATA); 482681a5bbeSBoris Popov return 0; 483681a5bbeSBoris Popov } 484681a5bbeSBoris Popov 485681a5bbeSBoris Popov static int 486fce6fbfaSBoris Popov smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td) 487681a5bbeSBoris Popov { 488681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 489681a5bbeSBoris Popov struct sockaddr_nb *snb; 490681a5bbeSBoris Popov int error, slen; 491681a5bbeSBoris Popov 492681a5bbeSBoris Popov NBDEBUG("\n"); 493681a5bbeSBoris Popov error = EINVAL; 494681a5bbeSBoris Popov do { 495681a5bbeSBoris Popov if (nbp->nbp_flags & NBF_LOCADDR) 496681a5bbeSBoris Popov break; 497681a5bbeSBoris Popov /* 498681a5bbeSBoris Popov * It is possible to create NETBIOS name in the kernel, 499681a5bbeSBoris Popov * but nothing prevents us to do it in the user space. 500681a5bbeSBoris Popov */ 501681a5bbeSBoris Popov if (sap == NULL) 502681a5bbeSBoris Popov break; 503681a5bbeSBoris Popov slen = sap->sa_len; 504681a5bbeSBoris Popov if (slen < NB_MINSALEN) 505681a5bbeSBoris Popov break; 506746e5bf0SRobert Watson snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK); 507681a5bbeSBoris Popov if (snb == NULL) { 508681a5bbeSBoris Popov error = ENOMEM; 509681a5bbeSBoris Popov break; 510681a5bbeSBoris Popov } 511681a5bbeSBoris Popov nbp->nbp_laddr = snb; 512681a5bbeSBoris Popov nbp->nbp_flags |= NBF_LOCADDR; 513681a5bbeSBoris Popov error = 0; 514681a5bbeSBoris Popov } while(0); 515681a5bbeSBoris Popov return error; 516681a5bbeSBoris Popov } 517681a5bbeSBoris Popov 518681a5bbeSBoris Popov static int 519fce6fbfaSBoris Popov smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td) 520681a5bbeSBoris Popov { 521681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 522681a5bbeSBoris Popov struct sockaddr_in sin; 523681a5bbeSBoris Popov struct sockaddr_nb *snb; 524681a5bbeSBoris Popov struct timespec ts1, ts2; 525681a5bbeSBoris Popov int error, slen; 526681a5bbeSBoris Popov 527681a5bbeSBoris Popov NBDEBUG("\n"); 528681a5bbeSBoris Popov if (nbp->nbp_tso != NULL) 529681a5bbeSBoris Popov return EISCONN; 530681a5bbeSBoris Popov if (nbp->nbp_laddr == NULL) 531681a5bbeSBoris Popov return EINVAL; 532681a5bbeSBoris Popov slen = sap->sa_len; 533681a5bbeSBoris Popov if (slen < NB_MINSALEN) 534681a5bbeSBoris Popov return EINVAL; 535681a5bbeSBoris Popov if (nbp->nbp_paddr) { 536681a5bbeSBoris Popov free(nbp->nbp_paddr, M_SONAME); 537681a5bbeSBoris Popov nbp->nbp_paddr = NULL; 538681a5bbeSBoris Popov } 539746e5bf0SRobert Watson snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK); 540681a5bbeSBoris Popov if (snb == NULL) 541681a5bbeSBoris Popov return ENOMEM; 542681a5bbeSBoris Popov nbp->nbp_paddr = snb; 543681a5bbeSBoris Popov sin = snb->snb_addrin; 544681a5bbeSBoris Popov getnanotime(&ts1); 545fce6fbfaSBoris Popov error = nb_connect_in(nbp, &sin, td); 546681a5bbeSBoris Popov if (error) 547681a5bbeSBoris Popov return error; 548681a5bbeSBoris Popov getnanotime(&ts2); 549*6040822cSAlan Somers timespecsub(&ts2, &ts1, &ts2); 55008bd45d3SChristian Brueffer if (ts2.tv_sec == 0) { 551681a5bbeSBoris Popov ts2.tv_sec = 1; 55208bd45d3SChristian Brueffer ts2.tv_nsec = 0; 55308bd45d3SChristian Brueffer } 554*6040822cSAlan Somers timespecadd(&ts2, &ts2, &nbp->nbp_timo); 555*6040822cSAlan Somers timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo); 556*6040822cSAlan Somers timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo); /* * 4 */ 557fce6fbfaSBoris Popov error = nbssn_rq_request(nbp, td); 558681a5bbeSBoris Popov if (error) 559fce6fbfaSBoris Popov smb_nbst_disconnect(vcp, td); 560681a5bbeSBoris Popov return error; 561681a5bbeSBoris Popov } 562681a5bbeSBoris Popov 563681a5bbeSBoris Popov static int 564fce6fbfaSBoris Popov smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td) 565681a5bbeSBoris Popov { 566681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 567681a5bbeSBoris Popov struct socket *so; 568681a5bbeSBoris Popov 569681a5bbeSBoris Popov if (nbp == NULL || nbp->nbp_tso == NULL) 570681a5bbeSBoris Popov return ENOTCONN; 571681a5bbeSBoris Popov if ((so = nbp->nbp_tso) != NULL) { 572681a5bbeSBoris Popov nbp->nbp_flags &= ~NBF_CONNECTED; 573681a5bbeSBoris Popov nbp->nbp_tso = (struct socket *)NULL; 574681a5bbeSBoris Popov soshutdown(so, 2); 575681a5bbeSBoris Popov soclose(so); 576681a5bbeSBoris Popov } 577681a5bbeSBoris Popov if (nbp->nbp_state != NBST_RETARGET) { 578681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED; 579681a5bbeSBoris Popov } 580681a5bbeSBoris Popov return 0; 581681a5bbeSBoris Popov } 582681a5bbeSBoris Popov 583681a5bbeSBoris Popov static int 584fce6fbfaSBoris Popov smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct thread *td) 585681a5bbeSBoris Popov { 586681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 587681a5bbeSBoris Popov int error; 588681a5bbeSBoris Popov 589681a5bbeSBoris Popov if (nbp->nbp_state != NBST_SESSION) { 590681a5bbeSBoris Popov error = ENOTCONN; 591681a5bbeSBoris Popov goto abort; 592681a5bbeSBoris Popov } 593eb1b1807SGleb Smirnoff M_PREPEND(m0, 4, M_WAITOK); 594681a5bbeSBoris Popov nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4); 595fce6fbfaSBoris Popov error = nb_sosend(nbp->nbp_tso, m0, 0, td); 596681a5bbeSBoris Popov return error; 597681a5bbeSBoris Popov abort: 598681a5bbeSBoris Popov if (m0) 599681a5bbeSBoris Popov m_freem(m0); 600681a5bbeSBoris Popov return error; 601681a5bbeSBoris Popov } 602681a5bbeSBoris Popov 603681a5bbeSBoris Popov 604681a5bbeSBoris Popov static int 605fce6fbfaSBoris Popov smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td) 606681a5bbeSBoris Popov { 607681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 608681a5bbeSBoris Popov u_int8_t rpcode; 609681a5bbeSBoris Popov int error, rplen; 610681a5bbeSBoris Popov 611681a5bbeSBoris Popov nbp->nbp_flags |= NBF_RECVLOCK; 612fce6fbfaSBoris Popov error = nbssn_recv(nbp, mpp, &rplen, &rpcode, td); 613681a5bbeSBoris Popov nbp->nbp_flags &= ~NBF_RECVLOCK; 614681a5bbeSBoris Popov return error; 615681a5bbeSBoris Popov } 616681a5bbeSBoris Popov 617681a5bbeSBoris Popov static void 618681a5bbeSBoris Popov smb_nbst_timo(struct smb_vc *vcp) 619681a5bbeSBoris Popov { 620681a5bbeSBoris Popov return; 621681a5bbeSBoris Popov } 622681a5bbeSBoris Popov 623681a5bbeSBoris Popov static void 624681a5bbeSBoris Popov smb_nbst_intr(struct smb_vc *vcp) 625681a5bbeSBoris Popov { 626681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 627681a5bbeSBoris Popov 628681a5bbeSBoris Popov if (nbp == NULL || nbp->nbp_tso == NULL) 629681a5bbeSBoris Popov return; 630681a5bbeSBoris Popov sorwakeup(nbp->nbp_tso); 631681a5bbeSBoris Popov sowwakeup(nbp->nbp_tso); 632681a5bbeSBoris Popov } 633681a5bbeSBoris Popov 634681a5bbeSBoris Popov static int 635681a5bbeSBoris Popov smb_nbst_getparam(struct smb_vc *vcp, int param, void *data) 636681a5bbeSBoris Popov { 637681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 638681a5bbeSBoris Popov 639681a5bbeSBoris Popov switch (param) { 640681a5bbeSBoris Popov case SMBTP_SNDSZ: 641681a5bbeSBoris Popov *(int*)data = nbp->nbp_sndbuf; 642681a5bbeSBoris Popov break; 643681a5bbeSBoris Popov case SMBTP_RCVSZ: 644681a5bbeSBoris Popov *(int*)data = nbp->nbp_rcvbuf; 645681a5bbeSBoris Popov break; 646681a5bbeSBoris Popov case SMBTP_TIMEOUT: 647681a5bbeSBoris Popov *(struct timespec*)data = nbp->nbp_timo; 648681a5bbeSBoris Popov break; 649681a5bbeSBoris Popov default: 650681a5bbeSBoris Popov return EINVAL; 651681a5bbeSBoris Popov } 652681a5bbeSBoris Popov return 0; 653681a5bbeSBoris Popov } 654681a5bbeSBoris Popov 655681a5bbeSBoris Popov static int 656681a5bbeSBoris Popov smb_nbst_setparam(struct smb_vc *vcp, int param, void *data) 657681a5bbeSBoris Popov { 658681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata; 659681a5bbeSBoris Popov 660681a5bbeSBoris Popov switch (param) { 661681a5bbeSBoris Popov case SMBTP_SELECTID: 662681a5bbeSBoris Popov nbp->nbp_selectid = data; 663681a5bbeSBoris Popov break; 664681a5bbeSBoris Popov default: 665681a5bbeSBoris Popov return EINVAL; 666681a5bbeSBoris Popov } 667681a5bbeSBoris Popov return 0; 668681a5bbeSBoris Popov } 669681a5bbeSBoris Popov 670681a5bbeSBoris Popov /* 671681a5bbeSBoris Popov * Check for fatal errors 672681a5bbeSBoris Popov */ 673681a5bbeSBoris Popov static int 674681a5bbeSBoris Popov smb_nbst_fatal(struct smb_vc *vcp, int error) 675681a5bbeSBoris Popov { 676681a5bbeSBoris Popov switch (error) { 677681a5bbeSBoris Popov case ENOTCONN: 678681a5bbeSBoris Popov case ENETRESET: 679681a5bbeSBoris Popov case ECONNABORTED: 680681a5bbeSBoris Popov return 1; 681681a5bbeSBoris Popov } 682681a5bbeSBoris Popov return 0; 683681a5bbeSBoris Popov } 684681a5bbeSBoris Popov 685681a5bbeSBoris Popov 686681a5bbeSBoris Popov struct smb_tran_desc smb_tran_nbtcp_desc = { 687681a5bbeSBoris Popov SMBT_NBTCP, 688681a5bbeSBoris Popov smb_nbst_create, smb_nbst_done, 689681a5bbeSBoris Popov smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect, 690681a5bbeSBoris Popov smb_nbst_send, smb_nbst_recv, 691681a5bbeSBoris Popov smb_nbst_timo, smb_nbst_intr, 692681a5bbeSBoris Popov smb_nbst_getparam, smb_nbst_setparam, 693681a5bbeSBoris Popov smb_nbst_fatal 694681a5bbeSBoris Popov }; 695681a5bbeSBoris Popov 696