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

Cache object: dfd0ee270b9514f34ef931d975448a55


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