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

Cache object: c4308f367283c457bf892ca39c44b249


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