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