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

Cache object: e0763271dcf00c428e7cbcd63c40ea5b


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