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

Cache object: 2dda7105cd2a0a4dc381f1e0760b4abd


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