2 * Copyright (c) 1999, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/sys/netncp/ncp_sock.c,v 1.2 1999/10/12 10:36:59 bp Exp $
33 * $DragonFly: src/sys/netproto/ncp/ncp_sock.c,v 1.2 2003/06/17 04:28:53 dillon Exp $
35 * Low level socket routines
41 #if !defined(INET) && !defined(IPX)
42 #error "NCP requeires either INET of IPX protocol family"
45 #include <sys/param.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
55 #include <sys/syslog.h>
57 #include <net/route.h>
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_pcb.h>
64 #include <netncp/ncp.h>
65 #include <netncp/ncp_conn.h>
66 #include <netncp/ncp_sock.h>
67 #include <netncp/ncp_subr.h>
68 #include <netncp/ncp_rq.h>
71 #define ipx_setnullnet(x) ((x).x_net.s_net[0]=0); ((x).x_net.s_net[1]=0);
72 #define ipx_setnullhost(x) ((x).x_host.s_host[0] = 0); \
73 ((x).x_host.s_host[1] = 0); ((x).x_host.s_host[2] = 0);
76 /*int ncp_poll(struct socket *so, int events);*/
77 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
78 static int ncp_soconnect(struct socket *so,struct sockaddr *target, struct proc *p);
81 /* This will need only if native IP used, or (unlikely) NCP will be
82 * implemented on the socket level
85 ncp_soconnect(struct socket *so,struct sockaddr *target, struct proc *p) {
88 error = soconnect(so, (struct sockaddr*)target, p);
92 * Wait for the connection to complete. Cribbed from the
93 * connect system call but with the wait timing out so
94 * that interruptible mounts don't hang here for a long time.
98 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
99 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
100 if ((so->so_state & SS_ISCONNECTING) &&
101 so->so_error == 0 /*&& rep &&*/) {
102 so->so_state &= ~SS_ISCONNECTING;
108 error = so->so_error;
120 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
125 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
128 len = min(len, sa->sa_len);
129 bcopy(sa, (caddr_t)asa, (u_int)len);
138 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
141 struct proc *p=curproc; /* XXX */
144 auio.uio_resid = len = 1000000;
146 flags = MSG_DONTWAIT;
148 /* error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
149 (struct mbuf **)0, (struct mbuf **)0, &flags);*/
150 error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
151 mp, (struct mbuf **)0, &flags);
152 *rlen = len - auio.uio_resid;
157 #ifdef NCP_SOCKET_DEBUG
159 printf("ncp_recv: err=%d\n", error);
165 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
167 struct proc *p = curproc; /* XXX */
168 struct sockaddr *to = 0;
169 struct ncp_conn *conn = rqp->conn;
175 m = m_copym(top, 0, M_COPYALL, M_WAIT);
177 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, 0, m, 0, flags, p);
178 if (error == 0 || error == EINTR || error == ENETDOWN)
180 if (rqp->rexmit == 0) break;
182 tsleep(&sendwait, PWAIT, "ncprsn", conn->li.timeout * hz);
183 error = ncp_chkintr(conn, p);
184 if (error == EINTR) break;
187 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
193 ncp_poll(struct socket *so, int events){
194 struct proc *p = curproc;
195 struct ucred *cred=NULL;
196 return so->so_proto->pr_usrreqs->pru_sopoll(so, events, cred, p);
200 ncp_sock_rselect(struct socket *so,struct proc *p, struct timeval *tv, int events)
202 struct timeval atv,rtv,ttv;
207 if (itimerfix(&atv)) {
211 getmicrouptime(&rtv);
212 timevaladd(&atv, &rtv);
216 p->p_flag |= P_SELECT;
217 error = ncp_poll(so, events);
223 getmicrouptime(&rtv);
224 if (timevalcmp(&rtv, &atv, >=))
227 timevalsub(&ttv, &rtv);
231 if ((p->p_flag & P_SELECT) == 0) {
235 p->p_flag &= ~P_SELECT;
236 error = tsleep((caddr_t)&selwait, PSOCK, "ncpslt", timo);
239 p->p_flag &= ~P_SELECT;
240 if (error == ERESTART) {
241 /* printf("Signal: %x", CURSIG(p));*/
249 * Connect to specified server via IPX
252 ncp_sock_connect_ipx(struct ncp_conn *conn) {
253 struct sockaddr_ipx sipx;
255 struct proc *p = conn->procp;
256 int addrlen, error, count;
258 sipx.sipx_port = htons(0);
260 for (count = 0;;count++) {
261 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
265 conn->ncp_so = conn->wdg_so = NULL;
266 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, p));
267 if (conn->li.opt & NCP_OPT_WDOG)
268 checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM,0,p));
269 addrlen = sizeof(sipx);
270 sipx.sipx_family = AF_IPX;
271 ipx_setnullnet(sipx.sipx_addr);
272 ipx_setnullhost(sipx.sipx_addr);
273 sipx.sipx_len = addrlen;
274 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, p);
276 if ((conn->li.opt & NCP_OPT_WDOG) == 0)
278 sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
279 sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
280 ipx_setnullnet(sipx.sipx_addr);
281 ipx_setnullhost(sipx.sipx_addr);
282 error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, p);
285 if (error != EADDRINUSE) goto bad;
286 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
287 soclose(conn->ncp_so);
289 soclose(conn->wdg_so);
291 npcb = sotoipxpcb(conn->ncp_so);
292 npcb->ipxp_dpt = IPXPROTO_NCP;
293 /* IPXrouted must be running, i.e. route must be presented */
294 conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
295 checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, p));
297 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
298 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
301 conn->flags |= NCPFL_SOCONN;
304 if (ncp_burst_enabled) {
305 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, p));
306 bzero(&sipx, sizeof(sipx));
307 sipx.sipx_len = sizeof(sipx);
308 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, p));
309 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, p));
313 conn->flags |= NCPFL_SOCONN;
314 ncp_sock_checksum(conn, 0);
318 ncp_sock_disconnect(conn);
323 ncp_sock_checksum(struct ncp_conn *conn, int enable) {
325 #ifdef SO_IPX_CHECKSUM
327 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
329 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
338 * Connect to specified server via IP
341 ncp_sock_connect_in(struct ncp_conn *conn) {
342 struct sockaddr_in sin;
343 struct proc *p=conn->procp;
344 int addrlen=sizeof(sin), error;
348 conn->ncp_so = conn->wdg_so = NULL;
349 checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, p));
350 sin.sin_family = AF_INET;
351 sin.sin_len = addrlen;
352 checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, p));
353 checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, p));
355 conn->flags |= NCPFL_SOCONN;
358 ncp_sock_disconnect(conn);
365 * Connection expected to be locked
368 ncp_sock_disconnect(struct ncp_conn *conn) {
369 register struct socket *so;
370 conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
373 conn->ncp_so = (struct socket *)0;
379 conn->wdg_so = (struct socket *)0;
386 conn->bc_so = (struct socket *)NULL;
396 ncp_watchdog(struct ncp_conn *conn) {
399 int error, len, flags;
405 while (conn->wdg_so) { /* not a loop */
407 auio.uio_resid = len = 1000000;
408 auio.uio_procp = curproc;
409 flags = MSG_DONTWAIT;
410 error = so->so_proto->pr_usrreqs->pru_soreceive(so,
411 (struct sockaddr**)&sa, &auio, &m, (struct mbuf**)0, &flags);
413 len -= auio.uio_resid;
414 NCPSDEBUG("got watch dog %d\n",len);
416 buf = mtod(m, char*);
417 if (buf[1] != '?') break;
419 error = so->so_proto->pr_usrreqs->pru_sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curproc);
420 NCPSDEBUG("send watch dog %d\n",error);
423 if (sa) FREE(sa, M_SONAME);
429 ncp_check_conn(struct ncp_conn *conn) {
432 if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))