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