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 = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
143 (struct mbuf **)0, (struct mbuf **)0, &flags);*/
144 error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, &auio,
145 mp, (struct mbuf **)0, &flags);
146 *rlen = len - auio.uio_resid;
151 #ifdef NCP_SOCKET_DEBUG
153 printf("ncp_recv: err=%d\n", error);
159 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
161 struct thread *td = curthread; /* XXX */
162 struct sockaddr *to = 0;
163 struct ncp_conn *conn = rqp->nr_conn;
169 m = m_copym(top, 0, M_COPYALL, M_TRYWAIT);
171 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, 0, m, 0, flags, td);
172 if (error == 0 || error == EINTR || error == ENETDOWN)
174 if (rqp->rexmit == 0) break;
176 tsleep(&sendwait, PWAIT, "ncprsn", conn->li.timeout * hz);
177 error = ncp_chkintr(conn, td);
178 if (error == EINTR) break;
181 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
187 ncp_poll(struct socket *so, int events)
189 struct thread *td = curthread;
190 struct ucred *cred = NULL;
192 return so->so_proto->pr_usrreqs->pru_sopoll(so, events, cred, td);
196 ncp_sock_rselect(struct socket *so, struct thread *td, struct timeval *tv,
199 struct timeval atv, rtv, ttv;
200 int ncoll, timo, error = 0;
204 if (itimerfix(&atv)) {
206 goto done_noproclock;
208 getmicrouptime(&rtv);
209 timevaladd(&atv, &rtv);
216 mtx_lock_spin(&sched_lock);
217 td->td_flags |= TDF_SELECT;
218 mtx_unlock_spin(&sched_lock);
219 mtx_unlock(&sellock);
221 TAILQ_INIT(&td->td_selq);
222 error = ncp_poll(so, events);
229 getmicrouptime(&rtv);
230 if (timevalcmp(&rtv, &atv, >=))
233 timevalsub(&ttv, &rtv);
237 * An event of our interest may occur during locking a thread.
238 * In order to avoid missing the event that occurred during locking
239 * the process, test TDF_SELECT and rescan file descriptors if
242 mtx_lock_spin(&sched_lock);
243 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
244 mtx_unlock_spin(&sched_lock);
247 mtx_unlock_spin(&sched_lock);
250 error = cv_timedwait(&selwait, &sellock, timo);
252 cv_wait(&selwait, &sellock);
257 clear_selinfo_list(td);
259 mtx_lock_spin(&sched_lock);
260 td->td_flags &= ~TDF_SELECT;
261 mtx_unlock_spin(&sched_lock);
262 mtx_unlock(&sellock);
265 if (error == ERESTART)
271 * Connect to specified server via IPX
274 ncp_sock_connect_ipx(struct ncp_conn *conn)
276 struct sockaddr_ipx sipx;
278 struct thread *td = conn->td;
279 int addrlen, error, count;
281 sipx.sipx_port = htons(0);
283 for (count = 0;;count++) {
284 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
288 conn->ncp_so = conn->wdg_so = NULL;
289 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
290 if (conn->li.opt & NCP_OPT_WDOG)
291 checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
292 addrlen = sizeof(sipx);
293 sipx.sipx_family = AF_IPX;
294 ipx_setnullnet(sipx.sipx_addr);
295 ipx_setnullhost(sipx.sipx_addr);
296 sipx.sipx_len = addrlen;
297 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
299 if ((conn->li.opt & NCP_OPT_WDOG) == 0)
301 sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
302 sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
303 ipx_setnullnet(sipx.sipx_addr);
304 ipx_setnullhost(sipx.sipx_addr);
305 error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
308 if (error != EADDRINUSE) goto bad;
309 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
310 soclose(conn->ncp_so);
312 soclose(conn->wdg_so);
314 npcb = sotoipxpcb(conn->ncp_so);
315 npcb->ipxp_dpt = IPXPROTO_NCP;
316 /* IPXrouted must be running, i.e. route must be presented */
317 conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
318 checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
320 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
321 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
324 conn->flags |= NCPFL_SOCONN;
327 if (ncp_burst_enabled) {
328 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
329 bzero(&sipx, sizeof(sipx));
330 sipx.sipx_len = sizeof(sipx);
331 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
332 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
336 conn->flags |= NCPFL_SOCONN;
337 ncp_sock_checksum(conn, 0);
341 ncp_sock_disconnect(conn);
346 ncp_sock_checksum(struct ncp_conn *conn, int enable)
350 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
352 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
358 * Connect to specified server via IP
361 ncp_sock_connect_in(struct ncp_conn *conn)
363 struct sockaddr_in sin;
364 struct thread *td = conn->td;
365 int addrlen = sizeof(sin), error;
369 conn->ncp_so = conn->wdg_so = NULL;
370 checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
371 sin.sin_family = AF_INET;
372 sin.sin_len = addrlen;
373 checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
374 checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
376 conn->flags |= NCPFL_SOCONN;
379 ncp_sock_disconnect(conn);
384 ncp_sock_connect(struct ncp_conn *ncp)
388 switch (ncp->li.saddr.sa_family) {
390 error = ncp_sock_connect_ipx(ncp);
393 error = ncp_sock_connect_in(ncp);
396 return EPROTONOSUPPORT;
402 * Connection expected to be locked
405 ncp_sock_disconnect(struct ncp_conn *conn) {
406 register struct socket *so;
407 conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
410 conn->ncp_so = (struct socket *)0;
416 conn->wdg_so = (struct socket *)0;
423 conn->bc_so = (struct socket *)NULL;
432 ncp_watchdog(struct ncp_conn *conn) {
435 int error, len, flags;
441 while (conn->wdg_so) { /* not a loop */
443 auio.uio_resid = len = 1000000;
444 auio.uio_td = curthread;
445 flags = MSG_DONTWAIT;
446 error = so->so_proto->pr_usrreqs->pru_soreceive(so,
447 (struct sockaddr**)&sa, &auio, &m, (struct mbuf**)0, &flags);
449 len -= auio.uio_resid;
450 NCPSDEBUG("got watch dog %d\n",len);
452 buf = mtod(m, char*);
453 if (buf[1] != '?') break;
455 error = so->so_proto->pr_usrreqs->pru_sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
456 NCPSDEBUG("send watch dog %d\n",error);
459 if (sa) FREE(sa, M_SONAME);
464 ncp_check_conn(struct ncp_conn *conn) {
467 if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
472 if (conn->li.saddr.sa_family == AF_IPX)