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

Cache object: 1b9ae2d4a0cd4ab9467c8ee75c90ab62


[ 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.