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