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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Low level socket routines
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/errno.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/protosw.h>
42 #include <sys/kernel.h>
44 #include <sys/syslog.h>
46 #include <sys/condvar.h>
47 #include <net/route.h>
49 #include <netipx/ipx.h>
50 #include <netipx/ipx_pcb.h>
52 #include <netncp/ncp.h>
53 #include <netncp/ncp_conn.h>
54 #include <netncp/ncp_sock.h>
55 #include <netncp/ncp_subr.h>
56 #include <netncp/ncp_rq.h>
58 #define ipx_setnullnet(x) ((x).x_net.s_net[0]=0); ((x).x_net.s_net[1]=0);
59 #define ipx_setnullhost(x) ((x).x_host.s_host[0] = 0); \
60 ((x).x_host.s_host[1] = 0); ((x).x_host.s_host[2] = 0);
62 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
63 static int ncp_soconnect(struct socket *so, struct sockaddr *target,
67 /* This will need only if native IP used, or (unlikely) NCP will be
68 * implemented on the socket level
71 ncp_soconnect(struct socket *so, struct sockaddr *target, struct thread *td)
75 error = soconnect(so, (struct sockaddr*)target, td);
79 * Wait for the connection to complete. Cribbed from the
80 * connect system call but with the wait timing out so
81 * that interruptible mounts don't hang here for a long time.
85 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
86 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
87 if ((so->so_state & SS_ISCONNECTING) &&
88 so->so_error == 0 /*&& rep &&*/) {
89 so->so_state &= ~SS_ISCONNECTING;
107 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
112 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
115 len = min(len, sa->sa_len);
116 bcopy(sa, (caddr_t)asa, (u_int)len);
125 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
128 struct thread *td = curthread; /* XXX */
131 auio.uio_resid = len = 1000000;
133 flags = MSG_DONTWAIT;
135 /* error = soreceive(so, 0, &auio, (struct mbuf **)0, (struct mbuf **)0,
137 error = soreceive(so, 0, &auio, mp, (struct mbuf **)0, &flags);
138 *rlen = len - auio.uio_resid;
143 #ifdef NCP_SOCKET_DEBUG
145 printf("ncp_recv: err=%d\n", error);
151 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
153 struct thread *td = curthread; /* XXX */
154 struct sockaddr *to = 0;
155 struct ncp_conn *conn = rqp->nr_conn;
160 m = m_copym(top, 0, M_COPYALL, M_WAIT);
162 error = sosend(so, to, 0, m, 0, flags, td);
163 if (error == 0 || error == EINTR || error == ENETDOWN)
165 if (rqp->rexmit == 0) break;
167 pause("ncprsn", conn->li.timeout * hz);
168 error = ncp_chkintr(conn, td);
169 if (error == EINTR) break;
172 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
178 * Connect to specified server via IPX
181 ncp_sock_connect_ipx(struct ncp_conn *conn)
183 struct sockaddr_ipx sipx;
185 struct thread *td = conn->td;
186 int addrlen, error, count;
188 sipx.sipx_port = htons(0);
190 for (count = 0;;count++) {
191 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
195 conn->ncp_so = conn->wdg_so = NULL;
196 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
197 if (conn->li.opt & NCP_OPT_WDOG)
198 checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
199 addrlen = sizeof(sipx);
200 sipx.sipx_family = AF_IPX;
201 ipx_setnullnet(sipx.sipx_addr);
202 ipx_setnullhost(sipx.sipx_addr);
203 sipx.sipx_len = addrlen;
204 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
206 if ((conn->li.opt & NCP_OPT_WDOG) == 0)
208 sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
209 sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
210 ipx_setnullnet(sipx.sipx_addr);
211 ipx_setnullhost(sipx.sipx_addr);
212 error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
215 if (error != EADDRINUSE) goto bad;
216 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
217 soclose(conn->ncp_so);
219 soclose(conn->wdg_so);
221 npcb = sotoipxpcb(conn->ncp_so);
222 npcb->ipxp_dpt = IPXPROTO_NCP;
223 /* IPXrouted must be running, i.e. route must be presented */
224 conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
225 checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
227 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
228 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
231 conn->flags |= NCPFL_SOCONN;
234 if (ncp_burst_enabled) {
235 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
236 bzero(&sipx, sizeof(sipx));
237 sipx.sipx_len = sizeof(sipx);
238 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
239 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
243 conn->flags |= NCPFL_SOCONN;
244 ncp_sock_checksum(conn, 0);
248 ncp_sock_disconnect(conn);
253 ncp_sock_checksum(struct ncp_conn *conn, int enable)
257 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
259 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
265 * Connect to specified server via IP
268 ncp_sock_connect_in(struct ncp_conn *conn)
270 struct sockaddr_in sin;
271 struct thread *td = conn->td;
272 int addrlen = sizeof(sin), error;
276 conn->ncp_so = conn->wdg_so = NULL;
277 checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
278 sin.sin_family = AF_INET;
279 sin.sin_len = addrlen;
280 checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
281 checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
283 conn->flags |= NCPFL_SOCONN;
286 ncp_sock_disconnect(conn);
291 ncp_sock_connect(struct ncp_conn *ncp)
295 switch (ncp->li.saddr.sa_family) {
297 error = ncp_sock_connect_ipx(ncp);
300 error = ncp_sock_connect_in(ncp);
303 return EPROTONOSUPPORT;
309 * Connection expected to be locked
312 ncp_sock_disconnect(struct ncp_conn *conn) {
313 register struct socket *so;
314 conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
317 conn->ncp_so = (struct socket *)0;
323 conn->wdg_so = (struct socket *)0;
330 conn->bc_so = (struct socket *)NULL;
339 ncp_watchdog(struct ncp_conn *conn) {
342 int error, len, flags;
348 while (conn->wdg_so) { /* not a loop */
350 auio.uio_resid = len = 1000000;
351 auio.uio_td = curthread;
352 flags = MSG_DONTWAIT;
353 error = soreceive(so, (struct sockaddr**)&sa, &auio, &m,
354 (struct mbuf**)0, &flags);
356 len -= auio.uio_resid;
357 NCPSDEBUG("got watch dog %d\n",len);
359 buf = mtod(m, char*);
360 if (buf[1] != '?') break;
362 error = sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
363 NCPSDEBUG("send watch dog %d\n",error);
366 if (sa) free(sa, M_SONAME);
371 ncp_check_conn(struct ncp_conn *conn) {
374 if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
379 if (conn->li.saddr.sa_family == AF_IPX)