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/netncp/ncp_sock.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) 1999, 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  * $FreeBSD: releng/5.0/sys/netncp/ncp_sock.c 97658 2002-05-31 11:52:35Z tanimura $
   33  *
   34  * Low level socket routines
   35  */
   36 
   37 #include <sys/param.h>
   38 #include <sys/errno.h>
   39 #include <sys/lock.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mutex.h>
   42 #include <sys/systm.h>
   43 #include <sys/proc.h>
   44 #include <sys/socket.h>
   45 #include <sys/socketvar.h>
   46 #include <sys/protosw.h>
   47 #include <sys/kernel.h>
   48 #include <sys/uio.h>
   49 #include <sys/syslog.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/condvar.h>
   52 #include <net/route.h>
   53 
   54 #include <netipx/ipx.h>
   55 #include <netipx/ipx_pcb.h>
   56 
   57 #include <netncp/ncp.h>
   58 #include <netncp/ncp_conn.h>
   59 #include <netncp/ncp_sock.h>
   60 #include <netncp/ncp_subr.h>
   61 #include <netncp/ncp_rq.h>
   62 
   63 #define ipx_setnullnet(x) ((x).x_net.s_net[0]=0); ((x).x_net.s_net[1]=0);
   64 #define ipx_setnullhost(x) ((x).x_host.s_host[0] = 0); \
   65         ((x).x_host.s_host[1] = 0); ((x).x_host.s_host[2] = 0);
   66 
   67 /*int ncp_poll(struct socket *so, int events);*/
   68 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
   69 static int ncp_soconnect(struct socket *so,struct sockaddr *target, struct proc *p);
   70 
   71 
   72 /* This will need only if native IP used, or (unlikely) NCP will be
   73  * implemented on the socket level
   74  */
   75 static int
   76 ncp_soconnect(struct socket *so,struct sockaddr *target, struct proc *p) {
   77         int error,s;
   78 
   79         error = soconnect(so, (struct sockaddr*)target, p);
   80         if (error)
   81                 return error;
   82         /*
   83          * Wait for the connection to complete. Cribbed from the
   84          * connect system call but with the wait timing out so
   85          * that interruptible mounts don't hang here for a long time.
   86          */
   87         error = EIO;
   88         s = splnet();
   89         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
   90                 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
   91                 if ((so->so_state & SS_ISCONNECTING) &&
   92                     so->so_error == 0 /*&& rep &&*/) {
   93                         so->so_state &= ~SS_ISCONNECTING;
   94                         splx(s);
   95                         goto bad;
   96                 }
   97         }
   98         if (so->so_error) {
   99                 error = so->so_error;
  100                 so->so_error = 0;
  101                 splx(s);
  102                 goto bad;
  103         }
  104                 splx(s);
  105         error=0;
  106 bad:
  107         return error;
  108 }
  109 #ifdef notyet
  110 static int
  111 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
  112         struct sockaddr *sa;
  113         int len=0, error;
  114 
  115         sa = 0;
  116         error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
  117         if (error==0) {
  118                 if (sa) {
  119                         len = min(len, sa->sa_len);
  120                         bcopy(sa, (caddr_t)asa, (u_int)len);
  121                 }
  122                 *alen=len;
  123         }
  124         if (sa)
  125                 FREE(sa, M_SONAME);
  126         return (error);
  127 }
  128 #endif
  129 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
  130 {
  131         struct uio auio;
  132         struct proc *p=curproc; /* XXX */
  133         int error,flags,len;
  134         
  135         auio.uio_resid = len = 1000000;
  136         auio.uio_procp = p;
  137         flags = MSG_DONTWAIT;
  138 
  139 /*      error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
  140             (struct mbuf **)0, (struct mbuf **)0, &flags);*/
  141         error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
  142             mp, (struct mbuf **)0, &flags);
  143         *rlen = len - auio.uio_resid;
  144 /*      if (!error) {
  145             *rlen=iov.iov_len;
  146         } else
  147             *rlen=0;*/
  148 #ifdef NCP_SOCKET_DEBUG
  149         if (error)
  150                 printf("ncp_recv: err=%d\n", error);
  151 #endif
  152         return (error);
  153 }
  154 
  155 int
  156 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
  157 {
  158         struct proc *p = curproc; /* XXX */
  159         struct sockaddr *to = 0;
  160         struct ncp_conn *conn = rqp->nr_conn;
  161         struct mbuf *m;
  162         int error, flags=0;
  163         int sendwait;
  164 
  165         for(;;) {
  166                 m = m_copym(top, 0, M_COPYALL, M_TRYWAIT);
  167 /*              NCPDDEBUG(m);*/
  168                 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, 0, m, 0, flags, p);
  169                 if (error == 0 || error == EINTR || error == ENETDOWN)
  170                         break;
  171                 if (rqp->rexmit == 0) break;
  172                 rqp->rexmit--;
  173                 tsleep(&sendwait, PWAIT, "ncprsn", conn->li.timeout * hz);
  174                 error = ncp_chkintr(conn, p);
  175                 if (error == EINTR) break;
  176         }
  177         if (error) {
  178                 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
  179         }
  180         return error;
  181 }
  182 
  183 int
  184 ncp_poll(struct socket *so, int events){
  185     struct proc *p = curproc;
  186     struct ucred *cred=NULL;
  187     return so->so_proto->pr_usrreqs->pru_sopoll(so, events, cred, p);
  188 }
  189 
  190 int
  191 ncp_sock_rselect(struct socket *so,struct proc *p, struct timeval *tv, int events)
  192 {
  193         struct timeval atv,rtv,ttv;
  194         int timo,error=0;
  195 
  196         if (tv) {
  197                 atv=*tv;
  198                 if (itimerfix(&atv)) {
  199                         error = EINVAL;
  200                         goto done_noproclock;
  201                 }
  202                 getmicrouptime(&rtv);
  203                 timevaladd(&atv, &rtv);
  204         }
  205         timo = 0;
  206         PROC_LOCK(p);
  207         p->p_flag |= P_SELECT;
  208         PROC_UNLOCK(p);
  209         error = ncp_poll(so, events);
  210         PROC_LOCK(p);
  211         if (error) {
  212                 error = 0;
  213                 goto done;
  214         }
  215         if (tv) {
  216                 getmicrouptime(&rtv);
  217                 if (timevalcmp(&rtv, &atv, >=)) {
  218                         /*
  219                          * An event of our interest may occur during locking a process.
  220                          * In order to avoid missing the event that occured during locking
  221                          * the process, test P_SELECT and rescan file descriptors if
  222                          * necessary.
  223                          */
  224                         if ((p->p_flag & P_SELECT) == 0) {
  225                                 p->p_flag |= P_SELECT;
  226                                 PROC_UNLOCK(p);
  227                                 error = ncp_poll(so, events);
  228                                 PROC_LOCK(p);
  229                         }
  230                         goto done;
  231                 }
  232                 ttv=atv;
  233                 timevalsub(&ttv, &rtv);
  234                 timo = tvtohz(&ttv);
  235         }
  236         p->p_flag &= ~P_SELECT;
  237         if (timo > 0)
  238                 error = cv_timedwait(&selwait, &p->p_mtx, timo);
  239         else {
  240                 cv_wait(&selwait, &p->p_mtx);
  241                 error = 0;
  242         }
  243 
  244 done:
  245         p->p_flag &= ~P_SELECT;
  246         PROC_UNLOCK(p);
  247 done_noproclock:
  248         if (error == ERESTART) {
  249 /*              printf("Signal: %x", cursig(p));*/
  250                 error = 0;
  251         }
  252         return (error);
  253 }
  254 
  255 /* 
  256  * Connect to specified server via IPX
  257  */
  258 static int
  259 ncp_sock_connect_ipx(struct ncp_conn *conn)
  260 {
  261         struct sockaddr_ipx sipx;
  262         struct ipxpcb *npcb;
  263         struct proc *p = conn->procp;
  264         int addrlen, error, count;
  265 
  266         sipx.sipx_port = htons(0);
  267 
  268         for (count = 0;;count++) {
  269                 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
  270                         error = EADDRINUSE;
  271                         goto bad;
  272                 }
  273                 conn->ncp_so = conn->wdg_so = NULL;
  274                 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, p));
  275                 if (conn->li.opt & NCP_OPT_WDOG)
  276                         checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM,0,p));
  277                 addrlen = sizeof(sipx);
  278                 sipx.sipx_family = AF_IPX;
  279                 ipx_setnullnet(sipx.sipx_addr);
  280                 ipx_setnullhost(sipx.sipx_addr);
  281                 sipx.sipx_len = addrlen;
  282                 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, p);
  283                 if (error == 0) {
  284                         if ((conn->li.opt & NCP_OPT_WDOG) == 0)
  285                                 break;
  286                         sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
  287                         sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
  288                         ipx_setnullnet(sipx.sipx_addr);
  289                         ipx_setnullhost(sipx.sipx_addr);
  290                         error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, p);
  291                 }
  292                 if (!error) break;
  293                 if (error != EADDRINUSE) goto bad;
  294                 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
  295                 soclose(conn->ncp_so);
  296                 if (conn->wdg_so)
  297                         soclose(conn->wdg_so);
  298         }
  299         npcb = sotoipxpcb(conn->ncp_so);
  300         npcb->ipxp_dpt = IPXPROTO_NCP;
  301         /* IPXrouted must be running, i.e. route must be presented */
  302         conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
  303         checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, p));
  304         if (conn->wdg_so) {
  305                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
  306                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
  307         }
  308         if (!error) {
  309                 conn->flags |= NCPFL_SOCONN;
  310         }
  311 #ifdef NCPBURST
  312         if (ncp_burst_enabled) {
  313                 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, p));
  314                 bzero(&sipx, sizeof(sipx));
  315                 sipx.sipx_len = sizeof(sipx);
  316                 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, p));
  317                 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, p));
  318         }
  319 #endif
  320         if (!error) {
  321                 conn->flags |= NCPFL_SOCONN;
  322                 ncp_sock_checksum(conn, 0);
  323         }
  324         return error;
  325 bad:
  326         ncp_sock_disconnect(conn);
  327         return (error);
  328 }
  329 
  330 int
  331 ncp_sock_checksum(struct ncp_conn *conn, int enable)
  332 {
  333 
  334         if (enable) {
  335                 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
  336         } else {
  337                 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
  338         }
  339         return 0;
  340 }
  341 
  342 /* 
  343  * Connect to specified server via IP
  344  */
  345 static int
  346 ncp_sock_connect_in(struct ncp_conn *conn)
  347 {
  348         struct sockaddr_in sin;
  349         struct proc *p=conn->procp;
  350         int addrlen=sizeof(sin), error;
  351 
  352         conn->flags = 0;
  353         bzero(&sin,addrlen);
  354         conn->ncp_so = conn->wdg_so = NULL;
  355         checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, p));
  356         sin.sin_family = AF_INET;
  357         sin.sin_len = addrlen;
  358         checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, p));
  359         checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, p));
  360         if  (!error)
  361                 conn->flags |= NCPFL_SOCONN;
  362         return error;
  363 bad:
  364         ncp_sock_disconnect(conn);
  365         return (error);
  366 }
  367 
  368 int
  369 ncp_sock_connect(struct ncp_conn *ncp)
  370 {
  371         int error;
  372 
  373         switch (ncp->li.saddr.sa_family) {
  374             case AF_IPX:
  375                 error = ncp_sock_connect_ipx(ncp);
  376                 break;
  377             case AF_INET:
  378                 error = ncp_sock_connect_in(ncp);
  379                 break;
  380             default:
  381                 return EPROTONOSUPPORT;
  382         }
  383         return error;
  384 }
  385 
  386 /*
  387  * Connection expected to be locked
  388  */
  389 int
  390 ncp_sock_disconnect(struct ncp_conn *conn) {
  391         register struct socket *so;
  392         conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
  393         if (conn->ncp_so) {
  394                 so = conn->ncp_so;
  395                 conn->ncp_so = (struct socket *)0;
  396                 soshutdown(so, 2);
  397                 soclose(so);
  398         }
  399         if (conn->wdg_so) {
  400                 so = conn->wdg_so;
  401                 conn->wdg_so = (struct socket *)0;
  402                 soshutdown(so, 2);
  403                 soclose(so);
  404         }
  405 #ifdef NCPBURST
  406         if (conn->bc_so) {
  407                 so = conn->bc_so;
  408                 conn->bc_so = (struct socket *)NULL;
  409                 soshutdown(so, 2);
  410                 soclose(so);
  411         }
  412 #endif
  413         return 0;
  414 }
  415 
  416 static void
  417 ncp_watchdog(struct ncp_conn *conn) {
  418         char *buf;
  419         struct mbuf *m;
  420         int error, len, flags;
  421         struct socket *so;
  422         struct sockaddr *sa;
  423         struct uio auio;
  424 
  425         sa = NULL;
  426         while (conn->wdg_so) { /* not a loop */
  427                 so = conn->wdg_so;
  428                 auio.uio_resid = len = 1000000;
  429                 auio.uio_procp = curproc;
  430                 flags = MSG_DONTWAIT;
  431                 error = so->so_proto->pr_usrreqs->pru_soreceive(so, 
  432                     (struct sockaddr**)&sa, &auio, &m, (struct mbuf**)0, &flags);
  433                 if (error) break;
  434                 len -= auio.uio_resid;
  435                 NCPSDEBUG("got watch dog %d\n",len);
  436                 if (len != 2) break;
  437                 buf = mtod(m, char*);
  438                 if (buf[1] != '?') break;
  439                 buf[1] = 'Y';
  440                 error = so->so_proto->pr_usrreqs->pru_sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curproc);
  441                 NCPSDEBUG("send watch dog %d\n",error);
  442                 break;
  443         }
  444         if (sa) FREE(sa, M_SONAME);
  445         return;
  446 }
  447 
  448 void
  449 ncp_check_conn(struct ncp_conn *conn) {
  450         int s;
  451 
  452         if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
  453                 return;
  454         s = splnet();
  455         ncp_check_rq(conn);
  456         splx(s);
  457         if (conn->li.saddr.sa_family == AF_IPX)
  458                 ncp_watchdog(conn);
  459 }

Cache object: 2f623b7fbaea6f1790506b8ddfb531dc


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