ce822018e20fbb424d11b405b0e36154da8cebf8
[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  *
34  * Low level socket routines
35  */
36
37 #include "opt_inet.h"
38 #include "opt_ipx.h"
39
40 #if !defined(INET) && !defined(IPX)
41 #error "NCP requires either INET of IPX protocol family"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/errno.h>
46 #include <sys/malloc.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/socketvar2.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                         soclrstate(so, 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 = NULL;
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                 kfree(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 = NULL;
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 #ifdef IPX
186 /* 
187  * Connect to specified server via IPX
188  */
189 int
190 ncp_sock_connect_ipx(struct ncp_conn *conn) {
191         struct sockaddr_ipx sipx;
192         struct ipxpcb *npcb;
193         struct thread *td = conn->td;
194         int addrlen, error, count;
195
196         sipx.sipx_port = htons(0);
197
198         for (count = 0;;count++) {
199                 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
200                         error = EADDRINUSE;
201                         goto bad;
202                 }
203                 conn->ncp_so = conn->wdg_so = NULL;
204                 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td));
205                 if (conn->li.opt & NCP_OPT_WDOG)
206                         checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM,0,td));
207                 addrlen = sizeof(sipx);
208                 sipx.sipx_family = AF_IPX;
209                 ipx_setnullnet(sipx.sipx_addr);
210                 ipx_setnullhost(sipx.sipx_addr);
211                 sipx.sipx_len = addrlen;
212                 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
213                 if (error == 0) {
214                         if ((conn->li.opt & NCP_OPT_WDOG) == 0)
215                                 break;
216                         sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
217                         sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
218                         ipx_setnullnet(sipx.sipx_addr);
219                         ipx_setnullhost(sipx.sipx_addr);
220                         error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
221                 }
222                 if (!error) break;
223                 if (error != EADDRINUSE) goto bad;
224                 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
225                 soclose(conn->ncp_so, FNONBLOCK);
226                 if (conn->wdg_so)
227                         soclose(conn->wdg_so, FNONBLOCK);
228         }
229         npcb = sotoipxpcb(conn->ncp_so);
230         npcb->ipxp_dpt = IPXPROTO_NCP;
231         /* IPXrouted must be running, i.e. route must be presented */
232         conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
233         checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
234         if (conn->wdg_so) {
235                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
236                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
237         }
238         if (!error) {
239                 conn->flags |= NCPFL_SOCONN;
240         }
241 #ifdef NCPBURST
242         if (ncp_burst_enabled) {
243                 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
244                 bzero(&sipx, sizeof(sipx));
245                 sipx.sipx_len = sizeof(sipx);
246                 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
247                 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
248         }
249 #endif
250         if (!error) {
251                 conn->flags |= NCPFL_SOCONN;
252                 ncp_sock_checksum(conn, 0);
253         }
254         return error;
255 bad:
256         ncp_sock_disconnect(conn);
257         return (error);
258 }
259
260 int
261 ncp_sock_checksum(struct ncp_conn *conn, int enable) {
262
263 #ifdef SO_IPX_CHECKSUM
264         if (enable) {
265                 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
266         } else {
267                 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
268         }
269 #endif
270         return 0;
271 }
272 #endif
273
274 #ifdef INET
275 /* 
276  * Connect to specified server via IP
277  */
278 int
279 ncp_sock_connect_in(struct ncp_conn *conn) {
280         struct sockaddr_in sin;
281         struct thread *td = conn->td;
282         int addrlen=sizeof(sin), error;
283
284         conn->flags = 0;
285         bzero(&sin,addrlen);
286         conn->ncp_so = conn->wdg_so = NULL;
287         checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td));
288         sin.sin_family = AF_INET;
289         sin.sin_len = addrlen;
290         checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
291         checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
292         if  (!error)
293                 conn->flags |= NCPFL_SOCONN;
294         return error;
295 bad:
296         ncp_sock_disconnect(conn);
297         return (error);
298 }
299 #endif
300
301
302 /*
303  * Connection expected to be locked
304  */
305 int
306 ncp_sock_disconnect(struct ncp_conn *conn) {
307         struct socket *so;
308         conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
309         if (conn->ncp_so) {
310                 so = conn->ncp_so;
311                 conn->ncp_so = NULL;
312                 soshutdown(so, SHUT_RDWR);
313                 soclose(so, FNONBLOCK);
314         }
315         if (conn->wdg_so) {
316                 so = conn->wdg_so;
317                 conn->wdg_so = NULL;
318                 soshutdown(so, SHUT_RDWR);
319                 soclose(so, FNONBLOCK);
320         }
321 #ifdef NCPBURST
322         if (conn->bc_so) {
323                 so = conn->bc_so;
324                 conn->bc_so = NULL;
325                 soshutdown(so, SHUT_RDWR);
326                 soclose(so, FNONBLOCK);
327         }
328 #endif
329         return 0;
330 }
331
332 #ifdef IPX
333 static void
334 ncp_watchdog(struct ncp_conn *conn) {
335         char *buf;
336         int error, len, flags;
337         struct socket *so;
338         struct sockaddr *sa;
339         struct sockbuf sio;
340
341         sa = NULL;
342         while (conn->wdg_so) { /* not a loop */
343                 so = conn->wdg_so;
344                 sbinit(&sio, 1000000);
345                 flags = MSG_DONTWAIT;
346                 error = so_pru_soreceive(so, (struct sockaddr**)&sa,
347                                          NULL, &sio, NULL, &flags);
348                 if (error)
349                         break;
350                 len = sio.sb_cc;
351                 NCPSDEBUG("got watch dog %d\n",len);
352                 if (len != 2) {
353                         m_freem(sio.sb_mb);
354                         break;
355                 }
356                 buf = mtod(sio.sb_mb, char *);
357                 if (buf[1] != '?') {
358                         m_freem(sio.sb_mb);
359                         break;
360                 }
361                 buf[1] = 'Y';
362                 error = so_pru_sosend(so, sa, NULL, sio.sb_mb, NULL, 0, curthread);
363                 NCPSDEBUG("send watch dog %d\n",error);
364                 break;
365         }
366         if (sa)
367                 kfree(sa, M_SONAME);
368         return;
369 }
370 #endif /* IPX */
371
372 void
373 ncp_check_conn(struct ncp_conn *conn) {
374         if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
375                 return;
376         crit_enter();
377         ncp_check_rq(conn);
378         crit_exit();
379 #ifdef IPX
380         ncp_watchdog(conn);
381 #endif
382 }