2 * Copyright (c) 1999, 2001 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 * Low level socket routines
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
48 #include <sys/kernel.h>
50 #include <sys/syslog.h>
52 #include <sys/condvar.h>
53 #include <net/route.h>
55 #include <netipx/ipx.h>
56 #include <netipx/ipx_pcb.h>
58 #include <netncp/ncp.h>
59 #include <netncp/ncp_conn.h>
60 #include <netncp/ncp_sock.h>
61 #include <netncp/ncp_subr.h>
62 #include <netncp/ncp_rq.h>
64 #define ipx_setnullnet(x) ((x).x_net.s_net[0]=0); ((x).x_net.s_net[1]=0);
65 #define ipx_setnullhost(x) ((x).x_host.s_host[0] = 0); \
66 ((x).x_host.s_host[1] = 0); ((x).x_host.s_host[2] = 0);
68 /*int ncp_poll(struct socket *so, int events);*/
69 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
70 static int ncp_soconnect(struct socket *so, struct sockaddr *target,
74 /* This will need only if native IP used, or (unlikely) NCP will be
75 * implemented on the socket level
78 ncp_soconnect(struct socket *so, struct sockaddr *target, struct thread *td)
82 error = soconnect(so, (struct sockaddr*)target, td);
86 * Wait for the connection to complete. Cribbed from the
87 * connect system call but with the wait timing out so
88 * that interruptible mounts don't hang here for a long time.
92 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
93 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
94 if ((so->so_state & SS_ISCONNECTING) &&
95 so->so_error == 0 /*&& rep &&*/) {
96 so->so_state &= ~SS_ISCONNECTING;
102 error = so->so_error;
114 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
119 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
122 len = min(len, sa->sa_len);
123 bcopy(sa, (caddr_t)asa, (u_int)len);
132 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
135 struct thread *td = curthread; /* XXX */
138 auio.uio_resid = len = 1000000;
140 flags = MSG_DONTWAIT;
142 /* error = soreceive(so, 0, &auio, (struct mbuf **)0, (struct mbuf **)0,
144 error = soreceive(so, 0, &auio, mp, (struct mbuf **)0, &flags);
145 *rlen = len - auio.uio_resid;
150 #ifdef NCP_SOCKET_DEBUG
152 printf("ncp_recv: err=%d\n", error);
158 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
160 struct thread *td = curthread; /* XXX */
161 struct sockaddr *to = 0;
162 struct ncp_conn *conn = rqp->nr_conn;
168 m = m_copym(top, 0, M_COPYALL, M_TRYWAIT);
170 error = sosend(so, to, 0, m, 0, flags, td);
171 if (error == 0 || error == EINTR || error == ENETDOWN)
173 if (rqp->rexmit == 0) break;
175 tsleep(&sendwait, PWAIT, "ncprsn", conn->li.timeout * hz);
176 error = ncp_chkintr(conn, td);
177 if (error == EINTR) break;
180 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
186 ncp_poll(struct socket *so, int events)
188 struct thread *td = curthread;
191 /* Fake up enough state to look like we are in poll(2). */
193 mtx_lock_spin(&sched_lock);
194 td->td_flags |= TDF_SELECT;
195 mtx_unlock_spin(&sched_lock);
196 mtx_unlock(&sellock);
197 TAILQ_INIT(&td->td_selq);
199 revents = sopoll(so, events, NULL, td);
201 /* Tear down the fake poll(2) state. */
203 clear_selinfo_list(td);
204 mtx_lock_spin(&sched_lock);
205 td->td_flags &= ~TDF_SELECT;
206 mtx_unlock_spin(&sched_lock);
207 mtx_unlock(&sellock);
213 ncp_sock_rselect(struct socket *so, struct thread *td, struct timeval *tv,
216 struct timeval atv, rtv, ttv;
217 int ncoll, timo, error, revents;
221 if (itimerfix(&atv)) {
223 goto done_noproclock;
225 getmicrouptime(&rtv);
226 timevaladd(&atv, &rtv);
233 mtx_lock_spin(&sched_lock);
234 td->td_flags |= TDF_SELECT;
235 mtx_unlock_spin(&sched_lock);
236 mtx_unlock(&sellock);
238 TAILQ_INIT(&td->td_selq);
239 revents = sopoll(so, events, NULL, td);
246 getmicrouptime(&rtv);
247 if (timevalcmp(&rtv, &atv, >=)) {
252 timevalsub(&ttv, &rtv);
256 * An event of our interest may occur during locking a thread.
257 * In order to avoid missing the event that occurred during locking
258 * the process, test TDF_SELECT and rescan file descriptors if
261 mtx_lock_spin(&sched_lock);
262 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
263 mtx_unlock_spin(&sched_lock);
266 mtx_unlock_spin(&sched_lock);
269 error = cv_timedwait(&selwait, &sellock, timo);
271 cv_wait(&selwait, &sellock);
276 clear_selinfo_list(td);
278 mtx_lock_spin(&sched_lock);
279 td->td_flags &= ~TDF_SELECT;
280 mtx_unlock_spin(&sched_lock);
281 mtx_unlock(&sellock);
284 if (error == ERESTART)
290 * Connect to specified server via IPX
293 ncp_sock_connect_ipx(struct ncp_conn *conn)
295 struct sockaddr_ipx sipx;
297 struct thread *td = conn->td;
298 int addrlen, error, count;
300 sipx.sipx_port = htons(0);
302 for (count = 0;;count++) {
303 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
307 conn->ncp_so = conn->wdg_so = NULL;
308 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
309 if (conn->li.opt & NCP_OPT_WDOG)
310 checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
311 addrlen = sizeof(sipx);
312 sipx.sipx_family = AF_IPX;
313 ipx_setnullnet(sipx.sipx_addr);
314 ipx_setnullhost(sipx.sipx_addr);
315 sipx.sipx_len = addrlen;
316 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
318 if ((conn->li.opt & NCP_OPT_WDOG) == 0)
320 sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
321 sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
322 ipx_setnullnet(sipx.sipx_addr);
323 ipx_setnullhost(sipx.sipx_addr);
324 error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
327 if (error != EADDRINUSE) goto bad;
328 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
329 soclose(conn->ncp_so);
331 soclose(conn->wdg_so);
333 npcb = sotoipxpcb(conn->ncp_so);
334 npcb->ipxp_dpt = IPXPROTO_NCP;
335 /* IPXrouted must be running, i.e. route must be presented */
336 conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
337 checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
339 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
340 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
343 conn->flags |= NCPFL_SOCONN;
346 if (ncp_burst_enabled) {
347 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
348 bzero(&sipx, sizeof(sipx));
349 sipx.sipx_len = sizeof(sipx);
350 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
351 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
355 conn->flags |= NCPFL_SOCONN;
356 ncp_sock_checksum(conn, 0);
360 ncp_sock_disconnect(conn);
365 ncp_sock_checksum(struct ncp_conn *conn, int enable)
369 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
371 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
377 * Connect to specified server via IP
380 ncp_sock_connect_in(struct ncp_conn *conn)
382 struct sockaddr_in sin;
383 struct thread *td = conn->td;
384 int addrlen = sizeof(sin), error;
388 conn->ncp_so = conn->wdg_so = NULL;
389 checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
390 sin.sin_family = AF_INET;
391 sin.sin_len = addrlen;
392 checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
393 checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
395 conn->flags |= NCPFL_SOCONN;
398 ncp_sock_disconnect(conn);
403 ncp_sock_connect(struct ncp_conn *ncp)
407 switch (ncp->li.saddr.sa_family) {
409 error = ncp_sock_connect_ipx(ncp);
412 error = ncp_sock_connect_in(ncp);
415 return EPROTONOSUPPORT;
421 * Connection expected to be locked
424 ncp_sock_disconnect(struct ncp_conn *conn) {
425 register struct socket *so;
426 conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
429 conn->ncp_so = (struct socket *)0;
435 conn->wdg_so = (struct socket *)0;
442 conn->bc_so = (struct socket *)NULL;
451 ncp_watchdog(struct ncp_conn *conn) {
454 int error, len, flags;
460 while (conn->wdg_so) { /* not a loop */
462 auio.uio_resid = len = 1000000;
463 auio.uio_td = curthread;
464 flags = MSG_DONTWAIT;
465 error = soreceive(so, (struct sockaddr**)&sa, &auio, &m,
466 (struct mbuf**)0, &flags);
468 len -= auio.uio_resid;
469 NCPSDEBUG("got watch dog %d\n",len);
471 buf = mtod(m, char*);
472 if (buf[1] != '?') break;
474 error = sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
475 NCPSDEBUG("send watch dog %d\n",error);
478 if (sa) FREE(sa, M_SONAME);
483 ncp_check_conn(struct ncp_conn *conn) {
486 if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
491 if (conn->li.saddr.sa_family == AF_IPX)