The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netsmb/smb_trantcp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2000-2001 Boris Popov
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *    This product includes software developed by Boris Popov.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/5.3/sys/netsmb/smb_trantcp.c 130653 2004-06-17 22:48:11Z rwatson $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/condvar.h>
   38 #include <sys/kernel.h>
   39 #include <sys/lock.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mbuf.h>
   42 #include <sys/poll.h>
   43 #include <sys/proc.h>
   44 #include <sys/protosw.h>
   45 #include <sys/signalvar.h>
   46 #include <sys/socket.h>
   47 #include <sys/socketvar.h>
   48 #include <sys/sx.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/systm.h>
   51 #include <sys/uio.h>
   52 
   53 #include <net/if.h>
   54 #include <net/route.h>
   55 
   56 #include <netinet/in.h>
   57 #include <netinet/tcp.h>
   58 
   59 #include <sys/mchain.h>
   60 
   61 #include <netsmb/netbios.h>
   62 
   63 #include <netsmb/smb.h>
   64 #include <netsmb/smb_conn.h>
   65 #include <netsmb/smb_tran.h>
   66 #include <netsmb/smb_trantcp.h>
   67 #include <netsmb/smb_subr.h>
   68 
   69 #define M_NBDATA        M_PCB
   70 
   71 static int smb_tcpsndbuf = NB_SNDQ - 1;
   72 static int smb_tcprcvbuf = NB_RCVQ - 1;
   73 
   74 SYSCTL_DECL(_net_smb);
   75 SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, "");
   76 SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, "");
   77 
   78 #define nb_sosend(so,m,flags,td) (so)->so_proto->pr_usrreqs->pru_sosend( \
   79                                     so, NULL, 0, m, 0, flags, td)
   80 
   81 static int  nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
   82         u_int8_t *rpcodep, struct thread *td);
   83 static int  smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td);
   84 
   85 static int
   86 nb_setsockopt_int(struct socket *so, int level, int name, int val)
   87 {
   88         struct sockopt sopt;
   89 
   90         bzero(&sopt, sizeof(sopt));
   91         sopt.sopt_level = level;
   92         sopt.sopt_name = name;
   93         sopt.sopt_val = &val;
   94         sopt.sopt_valsize = sizeof(val);
   95         return sosetopt(so, &sopt);
   96 }
   97 
   98 static __inline int
   99 nb_poll(struct nbpcb *nbp, int events, struct thread *td)
  100 {
  101         return nbp->nbp_tso->so_proto->pr_usrreqs->pru_sopoll(nbp->nbp_tso,
  102             events, NULL, td);
  103 }
  104 
  105 static int
  106 nbssn_rselect(struct nbpcb *nbp, struct timeval *tv, int events,
  107         struct thread *td)
  108 {
  109         struct timeval atv, rtv, ttv;
  110         int ncoll, timo, error;
  111 
  112         if (tv) {
  113                 atv = *tv;
  114                 if (itimerfix(&atv)) {
  115                         error = EINVAL;
  116                         goto done_noproclock;
  117                 }
  118                 getmicrouptime(&rtv);
  119                 timevaladd(&atv, &rtv);
  120         }
  121         timo = 0;
  122         mtx_lock(&sellock);
  123 retry:
  124 
  125         ncoll = nselcoll;
  126         mtx_lock_spin(&sched_lock);
  127         td->td_flags |= TDF_SELECT;
  128         mtx_unlock_spin(&sched_lock);
  129         mtx_unlock(&sellock);
  130 
  131         /* XXX: Should be done when the thread is initialized. */
  132         TAILQ_INIT(&td->td_selq);
  133         error = nb_poll(nbp, events, td);
  134         mtx_lock(&sellock);
  135         if (error) {
  136                 error = 0;
  137                 goto done;
  138         }
  139         if (tv) {
  140                 getmicrouptime(&rtv);
  141                 if (timevalcmp(&rtv, &atv, >=))
  142                         goto done;
  143                 ttv = atv;
  144                 timevalsub(&ttv, &rtv);
  145                 timo = tvtohz(&ttv);
  146         }
  147         /*
  148          * An event of our interest may occur during locking a process.
  149          * In order to avoid missing the event that occurred during locking
  150          * the process, test P_SELECT and rescan file descriptors if
  151          * necessary.
  152          */
  153         mtx_lock_spin(&sched_lock);
  154         if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
  155                 mtx_unlock_spin(&sched_lock);
  156                 goto retry;
  157         }
  158         mtx_unlock_spin(&sched_lock);
  159 
  160         if (timo > 0)
  161                 error = cv_timedwait(&selwait, &sellock, timo);
  162         else {
  163                 cv_wait(&selwait, &sellock);
  164                 error = 0;
  165         }
  166 
  167 done:
  168         clear_selinfo_list(td);
  169         
  170         mtx_lock_spin(&sched_lock);
  171         td->td_flags &= ~TDF_SELECT;
  172         mtx_unlock_spin(&sched_lock);
  173         mtx_unlock(&sellock);
  174 
  175 done_noproclock:
  176         if (error == ERESTART)
  177                 return 0;
  178         return error;
  179 }
  180 
  181 static int
  182 nb_intr(struct nbpcb *nbp, struct proc *p)
  183 {
  184         return 0;
  185 }
  186 
  187 static void
  188 nb_upcall(struct socket *so, void *arg, int waitflag)
  189 {
  190         struct nbpcb *nbp = arg;
  191 
  192         if (arg == NULL || nbp->nbp_selectid == NULL)
  193                 return;
  194         wakeup(nbp->nbp_selectid);
  195 }
  196 
  197 static int
  198 nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
  199 {
  200         u_int32_t *p = mtod(m, u_int32_t *);
  201 
  202         *p = htonl((len & 0x1FFFF) | (type << 24));
  203         return 0;
  204 }
  205 
  206 static int
  207 nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
  208 {
  209         int error;
  210         u_char seglen, *cp;
  211 
  212         cp = snb->snb_name;
  213         if (*cp == 0)
  214                 return EINVAL;
  215         NBDEBUG("[%s]\n", cp);
  216         for (;;) {
  217                 seglen = (*cp) + 1;
  218                 error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
  219                 if (error)
  220                         return error;
  221                 if (seglen == 1)
  222                         break;
  223                 cp += seglen;
  224         }
  225         return 0;
  226 }
  227 
  228 static int
  229 nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct thread *td)
  230 {
  231         struct socket *so;
  232         int error, s;
  233 
  234         error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP,
  235             td->td_ucred, td);
  236         if (error)
  237                 return error;
  238         nbp->nbp_tso = so;
  239         so->so_upcallarg = (caddr_t)nbp;
  240         so->so_upcall = nb_upcall;
  241         SOCKBUF_LOCK(&so->so_rcv);
  242         so->so_rcv.sb_flags |= SB_UPCALL;
  243         SOCKBUF_UNLOCK(&so->so_rcv);
  244         so->so_rcv.sb_timeo = (5 * hz);
  245         so->so_snd.sb_timeo = (5 * hz);
  246         error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf);
  247         if (error)
  248                 goto bad;
  249         nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
  250         nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
  251         SOCKBUF_LOCK(&so->so_rcv);
  252         so->so_rcv.sb_flags &= ~SB_NOINTR;
  253         SOCKBUF_UNLOCK(&so->so_rcv);
  254         SOCKBUF_LOCK(&so->so_snd);
  255         so->so_snd.sb_flags &= ~SB_NOINTR;
  256         SOCKBUF_UNLOCK(&so->so_snd);
  257         error = soconnect(so, (struct sockaddr*)to, td);
  258         if (error)
  259                 goto bad;
  260         s = splnet();
  261         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  262                 tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
  263                 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
  264                         (error = nb_intr(nbp, td->td_proc)) != 0) {
  265                         so->so_state &= ~SS_ISCONNECTING;
  266                         splx(s);
  267                         goto bad;
  268                 }
  269         }
  270         if (so->so_error) {
  271                 error = so->so_error;
  272                 so->so_error = 0;
  273                 splx(s);
  274                 goto bad;
  275         }
  276         splx(s);
  277         return 0;
  278 bad:
  279         smb_nbst_disconnect(nbp->nbp_vc, td);
  280         return error;
  281 }
  282 
  283 static int
  284 nbssn_rq_request(struct nbpcb *nbp, struct thread *td)
  285 {
  286         struct mbchain mb, *mbp = &mb;
  287         struct mdchain md, *mdp = &md;
  288         struct mbuf *m0;
  289         struct timeval tv;
  290         struct sockaddr_in sin;
  291         u_short port;
  292         u_int8_t rpcode;
  293         int error, rplen;
  294 
  295         error = mb_init(mbp);
  296         if (error)
  297                 return error;
  298         mb_put_uint32le(mbp, 0);
  299         nb_put_name(mbp, nbp->nbp_paddr);
  300         nb_put_name(mbp, nbp->nbp_laddr);
  301         nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
  302         error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td);
  303         if (!error) {
  304                 nbp->nbp_state = NBST_RQSENT;
  305         }
  306         mb_detach(mbp);
  307         mb_done(mbp);
  308         if (error)
  309                 return error;
  310         TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo);
  311         error = nbssn_rselect(nbp, &tv, POLLIN, td);
  312         if (error == EWOULDBLOCK) {     /* Timeout */
  313                 NBDEBUG("initial request timeout\n");
  314                 return ETIMEDOUT;
  315         }
  316         if (error)                      /* restart or interrupt */
  317                 return error;
  318         error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td);
  319         if (error) {
  320                 NBDEBUG("recv() error %d\n", error);
  321                 return error;
  322         }
  323         /*
  324          * Process NETBIOS reply
  325          */
  326         if (m0)
  327                 md_initm(mdp, m0);
  328         error = 0;
  329         do {
  330                 if (rpcode == NB_SSN_POSRESP) {
  331                         nbp->nbp_state = NBST_SESSION;
  332                         nbp->nbp_flags |= NBF_CONNECTED;
  333                         break;
  334                 }
  335                 if (rpcode != NB_SSN_RTGRESP) {
  336                         error = ECONNABORTED;
  337                         break;
  338                 }
  339                 if (rplen != 6) {
  340                         error = ECONNABORTED;
  341                         break;
  342                 }
  343                 md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
  344                 md_get_uint16(mdp, &port);
  345                 sin.sin_port = port;
  346                 nbp->nbp_state = NBST_RETARGET;
  347                 smb_nbst_disconnect(nbp->nbp_vc, td);
  348                 error = nb_connect_in(nbp, &sin, td);
  349                 if (!error)
  350                         error = nbssn_rq_request(nbp, td);
  351                 if (error) {
  352                         smb_nbst_disconnect(nbp->nbp_vc, td);
  353                         break;
  354                 }
  355         } while(0);
  356         if (m0)
  357                 md_done(mdp);
  358         return error;
  359 }
  360 
  361 static int
  362 nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
  363         u_int8_t *rpcodep, int flags, struct thread *td)
  364 {
  365         struct socket *so = nbp->nbp_tso;
  366         struct uio auio;
  367         struct iovec aio;
  368         u_int32_t len;
  369         int error;
  370 
  371         aio.iov_base = (caddr_t)&len;
  372         aio.iov_len = sizeof(len);
  373         auio.uio_iov = &aio;
  374         auio.uio_iovcnt = 1;
  375         auio.uio_segflg = UIO_SYSSPACE;
  376         auio.uio_rw = UIO_READ;
  377         auio.uio_offset = 0;
  378         auio.uio_resid = sizeof(len);
  379         auio.uio_td = td;
  380         error = so->so_proto->pr_usrreqs->pru_soreceive
  381             (so, (struct sockaddr **)NULL, &auio,
  382             (struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
  383         if (error)
  384                 return error;
  385         if (auio.uio_resid > 0) {
  386                 SMBSDEBUG("short reply\n");
  387                 return EPIPE;
  388         }
  389         len = ntohl(len);
  390         *rpcodep = (len >> 24) & 0xFF;
  391         len &= 0x1ffff;
  392         if (len > SMB_MAXPKTLEN) {
  393                 SMBERROR("packet too long (%d)\n", len);
  394                 return EFBIG;
  395         }
  396         *lenp = len;
  397         return 0;
  398 }
  399 
  400 static int
  401 nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
  402         u_int8_t *rpcodep, struct thread *td)
  403 {
  404         struct socket *so = nbp->nbp_tso;
  405         struct uio auio;
  406         struct mbuf *m, *tm, *im;
  407         u_int8_t rpcode;
  408         int len, resid;
  409         int error, rcvflg;
  410 
  411         if (so == NULL)
  412                 return ENOTCONN;
  413 
  414         if (mpp)
  415                 *mpp = NULL;
  416         m = NULL;
  417         for(;;) {
  418                 /*
  419                  * Poll for a response header.
  420                  * If we don't have one waiting, return.
  421                  */
  422                 error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, td);
  423                 if ((so->so_state & (SS_ISDISCONNECTING | SS_ISDISCONNECTED)) ||
  424                     (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
  425                         nbp->nbp_state = NBST_CLOSED;
  426                         NBDEBUG("session closed by peer\n");
  427                         return ECONNRESET;
  428                 }
  429                 if (error)
  430                         return error;
  431                 if (len == 0 && nbp->nbp_state != NBST_SESSION)
  432                         break;
  433                 /* no data, try again */
  434                 if (rpcode == NB_SSN_KEEPALIVE)
  435                         continue;
  436 
  437                 /*
  438                  * Loop, blocking, for data following the response header.
  439                  *
  440                  * Note that we can't simply block here with MSG_WAITALL for the
  441                  * entire response size, as it may be larger than the TCP
  442                  * slow-start window that the sender employs.  This will result
  443                  * in the sender stalling until the delayed ACK is sent, then
  444                  * resuming slow-start, resulting in very poor performance.
  445                  *
  446                  * Instead, we never request more than NB_SORECEIVE_CHUNK
  447                  * bytes at a time, resulting in an ack being pushed by
  448                  * the TCP code at the completion of each call.
  449                  */
  450                 resid = len;
  451                 while (resid > 0) {
  452                         tm = NULL;
  453                         rcvflg = MSG_WAITALL;
  454                         bzero(&auio, sizeof(auio));
  455                         auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
  456                         auio.uio_td = td;
  457                         resid -= auio.uio_resid;
  458                         /*
  459                          * Spin until we have collected everything in
  460                          * this chunk.
  461                          */
  462                         do {
  463                                 rcvflg = MSG_WAITALL;
  464                                 error = so->so_proto->pr_usrreqs->pru_soreceive
  465                                     (so, (struct sockaddr **)NULL,
  466                                     &auio, &tm, (struct mbuf **)NULL, &rcvflg);
  467                         } while (error == EWOULDBLOCK || error == EINTR ||
  468                                  error == ERESTART);
  469                         if (error)
  470                                 goto out;
  471                         /* short return guarantees unhappiness */
  472                         if (auio.uio_resid > 0) {
  473                                 SMBERROR("packet is shorter than expected\n");
  474                                 error = EPIPE;
  475                                 goto out;
  476                         }
  477                         /* append received chunk to previous chunk(s) */
  478                         if (m == NULL) {
  479                                 m = tm;
  480                         } else {
  481                                 /*
  482                                  * Just glue the new chain on the end.
  483                                  * Consumer will pullup as required.
  484                                  */
  485                                 for (im = m; im->m_next != NULL; im = im->m_next)
  486                                         ;
  487                                 im->m_next = tm;
  488                         }
  489                 }
  490                 /* got a session/message packet? */
  491                 if (nbp->nbp_state == NBST_SESSION &&
  492                     rpcode == NB_SSN_MESSAGE)
  493                         break;
  494                 /* drop packet and try for another */
  495                 NBDEBUG("non-session packet %x\n", rpcode);
  496                 if (m) {
  497                         m_freem(m);
  498                         m = NULL;
  499                 }
  500         }
  501 
  502 out:
  503         if (error) {
  504                 if (m)
  505                         m_freem(m);
  506                 return error;
  507         }
  508         if (mpp)
  509                 *mpp = m;
  510         else
  511                 m_freem(m);
  512         *lenp = len;
  513         *rpcodep = rpcode;
  514         return 0;
  515 }
  516 
  517 /*
  518  * SMB transport interface
  519  */
  520 static int
  521 smb_nbst_create(struct smb_vc *vcp, struct thread *td)
  522 {
  523         struct nbpcb *nbp;
  524 
  525         MALLOC(nbp, struct nbpcb *, sizeof *nbp, M_NBDATA, M_WAITOK);
  526         bzero(nbp, sizeof *nbp);
  527         nbp->nbp_timo.tv_sec = 15;      /* XXX: sysctl ? */
  528         nbp->nbp_state = NBST_CLOSED;
  529         nbp->nbp_vc = vcp;
  530         nbp->nbp_sndbuf = smb_tcpsndbuf;
  531         nbp->nbp_rcvbuf = smb_tcprcvbuf;
  532         vcp->vc_tdata = nbp;
  533         return 0;
  534 }
  535 
  536 static int
  537 smb_nbst_done(struct smb_vc *vcp, struct thread *td)
  538 {
  539         struct nbpcb *nbp = vcp->vc_tdata;
  540 
  541         if (nbp == NULL)
  542                 return ENOTCONN;
  543         smb_nbst_disconnect(vcp, td);
  544         if (nbp->nbp_laddr)
  545                 free(nbp->nbp_laddr, M_SONAME);
  546         if (nbp->nbp_paddr)
  547                 free(nbp->nbp_paddr, M_SONAME);
  548         free(nbp, M_NBDATA);
  549         return 0;
  550 }
  551 
  552 static int
  553 smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
  554 {
  555         struct nbpcb *nbp = vcp->vc_tdata;
  556         struct sockaddr_nb *snb;
  557         int error, slen;
  558 
  559         NBDEBUG("\n");
  560         error = EINVAL;
  561         do {
  562                 if (nbp->nbp_flags & NBF_LOCADDR)
  563                         break;
  564                 /*
  565                  * It is possible to create NETBIOS name in the kernel,
  566                  * but nothing prevents us to do it in the user space.
  567                  */
  568                 if (sap == NULL)
  569                         break;
  570                 slen = sap->sa_len;
  571                 if (slen < NB_MINSALEN)
  572                         break;
  573                 snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
  574                 if (snb == NULL) {
  575                         error = ENOMEM;
  576                         break;
  577                 }
  578                 nbp->nbp_laddr = snb;
  579                 nbp->nbp_flags |= NBF_LOCADDR;
  580                 error = 0;
  581         } while(0);
  582         return error;
  583 }
  584 
  585 static int
  586 smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
  587 {
  588         struct nbpcb *nbp = vcp->vc_tdata;
  589         struct sockaddr_in sin;
  590         struct sockaddr_nb *snb;
  591         struct timespec ts1, ts2;
  592         int error, slen;
  593 
  594         NBDEBUG("\n");
  595         if (nbp->nbp_tso != NULL)
  596                 return EISCONN;
  597         if (nbp->nbp_laddr == NULL)
  598                 return EINVAL;
  599         slen = sap->sa_len;
  600         if (slen < NB_MINSALEN)
  601                 return EINVAL;
  602         if (nbp->nbp_paddr) {
  603                 free(nbp->nbp_paddr, M_SONAME);
  604                 nbp->nbp_paddr = NULL;
  605         }
  606         snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
  607         if (snb == NULL)
  608                 return ENOMEM;
  609         nbp->nbp_paddr = snb;
  610         sin = snb->snb_addrin;
  611         getnanotime(&ts1);
  612         error = nb_connect_in(nbp, &sin, td);
  613         if (error)
  614                 return error;
  615         getnanotime(&ts2);
  616         timespecsub(&ts2, &ts1);
  617         if (ts2.tv_sec == 0 && ts2.tv_sec == 0)
  618                 ts2.tv_sec = 1;
  619         nbp->nbp_timo = ts2;
  620         timespecadd(&nbp->nbp_timo, &ts2);
  621         timespecadd(&nbp->nbp_timo, &ts2);
  622         timespecadd(&nbp->nbp_timo, &ts2);      /*  * 4 */
  623         error = nbssn_rq_request(nbp, td);
  624         if (error)
  625                 smb_nbst_disconnect(vcp, td);
  626         return error;
  627 }
  628 
  629 static int
  630 smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td)
  631 {
  632         struct nbpcb *nbp = vcp->vc_tdata;
  633         struct socket *so;
  634 
  635         if (nbp == NULL || nbp->nbp_tso == NULL)
  636                 return ENOTCONN;
  637         if ((so = nbp->nbp_tso) != NULL) {
  638                 nbp->nbp_flags &= ~NBF_CONNECTED;
  639                 nbp->nbp_tso = (struct socket *)NULL;
  640                 soshutdown(so, 2);
  641                 soclose(so);
  642         }
  643         if (nbp->nbp_state != NBST_RETARGET) {
  644                 nbp->nbp_state = NBST_CLOSED;
  645         }
  646         return 0;
  647 }
  648 
  649 static int
  650 smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct thread *td)
  651 {
  652         struct nbpcb *nbp = vcp->vc_tdata;
  653         int error;
  654 
  655         if (nbp->nbp_state != NBST_SESSION) {
  656                 error = ENOTCONN;
  657                 goto abort;
  658         }
  659         M_PREPEND(m0, 4, M_TRYWAIT);
  660         if (m0 == NULL)
  661                 return ENOBUFS;
  662         nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
  663         error = nb_sosend(nbp->nbp_tso, m0, 0, td);
  664         return error;
  665 abort:
  666         if (m0)
  667                 m_freem(m0);
  668         return error;
  669 }
  670 
  671 
  672 static int
  673 smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td)
  674 {
  675         struct nbpcb *nbp = vcp->vc_tdata;
  676         u_int8_t rpcode;
  677         int error, rplen;
  678 
  679         nbp->nbp_flags |= NBF_RECVLOCK;
  680         error = nbssn_recv(nbp, mpp, &rplen, &rpcode, td);
  681         nbp->nbp_flags &= ~NBF_RECVLOCK;
  682         return error;
  683 }
  684 
  685 static void
  686 smb_nbst_timo(struct smb_vc *vcp)
  687 {
  688         return;
  689 }
  690 
  691 static void
  692 smb_nbst_intr(struct smb_vc *vcp)
  693 {
  694         struct nbpcb *nbp = vcp->vc_tdata;
  695 
  696         if (nbp == NULL || nbp->nbp_tso == NULL)
  697                 return;
  698         sorwakeup(nbp->nbp_tso);
  699         sowwakeup(nbp->nbp_tso);
  700 }
  701 
  702 static int
  703 smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
  704 {
  705         struct nbpcb *nbp = vcp->vc_tdata;
  706 
  707         switch (param) {
  708             case SMBTP_SNDSZ:
  709                 *(int*)data = nbp->nbp_sndbuf;
  710                 break;
  711             case SMBTP_RCVSZ:
  712                 *(int*)data = nbp->nbp_rcvbuf;
  713                 break;
  714             case SMBTP_TIMEOUT:
  715                 *(struct timespec*)data = nbp->nbp_timo;
  716                 break;
  717             default:
  718                 return EINVAL;
  719         }
  720         return 0;
  721 }
  722 
  723 static int
  724 smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
  725 {
  726         struct nbpcb *nbp = vcp->vc_tdata;
  727 
  728         switch (param) {
  729             case SMBTP_SELECTID:
  730                 nbp->nbp_selectid = data;
  731                 break;
  732             default:
  733                 return EINVAL;
  734         }
  735         return 0;
  736 }
  737 
  738 /*
  739  * Check for fatal errors
  740  */
  741 static int
  742 smb_nbst_fatal(struct smb_vc *vcp, int error)
  743 {
  744         switch (error) {
  745             case ENOTCONN:
  746             case ENETRESET:
  747             case ECONNABORTED:
  748                 return 1;
  749         }
  750         return 0;
  751 }
  752 
  753 
  754 struct smb_tran_desc smb_tran_nbtcp_desc = {
  755         SMBT_NBTCP,
  756         smb_nbst_create, smb_nbst_done,
  757         smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
  758         smb_nbst_send, smb_nbst_recv,
  759         smb_nbst_timo, smb_nbst_intr,
  760         smb_nbst_getparam, smb_nbst_setparam,
  761         smb_nbst_fatal
  762 };
  763 

Cache object: 0442142dcaee08fc23c9408fb1f39a53


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.