Sync our ieee80211*.9 manual pages with the recent upgrade.
[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.14 2006/06/13 08:12:04 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 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/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 mbuf **mp, int *rlen)
143 {
144         struct uio auio;
145         struct thread *td = curthread; /* XXX */
146         int error,flags,len;
147         
148         auio.uio_resid = len = 1000000;
149         auio.uio_td = td;
150         flags = MSG_DONTWAIT;
151
152 /*      error = so_pru_soreceive(so, NULL, &auio, NULL, NULL, &flags); */
153         error = so_pru_soreceive(so, NULL, &auio, mp, NULL, &flags);
154         *rlen = len - auio.uio_resid;
155 /*      if (!error) {
156             *rlen=iov.iov_len;
157         } else
158             *rlen=0;*/
159 #ifdef NCP_SOCKET_DEBUG
160         if (error)
161                 printf("ncp_recv: err=%d\n", error);
162 #endif
163         return (error);
164 }
165
166 int
167 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
168 {
169         struct thread *td = curthread; /* XXX */
170         struct sockaddr *to = 0;
171         struct ncp_conn *conn = rqp->conn;
172         struct mbuf *m;
173         int error, flags=0;
174         int sendwait;
175
176         for(;;) {
177                 m = m_copym(top, 0, M_COPYALL, MB_WAIT);
178 /*              NCPDDEBUG(m);*/
179                 error = so_pru_sosend(so, to, NULL, m, NULL, flags, td);
180                 if (error == 0 || error == EINTR || error == ENETDOWN)
181                         break;
182                 if (rqp->rexmit == 0) break;
183                 rqp->rexmit--;
184                 tsleep(&sendwait, 0, "ncprsn", conn->li.timeout * hz);
185                 error = ncp_chkintr(conn, td);
186                 if (error == EINTR) break;
187         }
188         if (error) {
189                 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
190         }
191         return error;
192 }
193
194 int
195 ncp_poll(struct socket *so, int events)
196 {
197     return (so_pru_sopoll(so, events, NULL));
198 }
199
200 int
201 ncp_sock_rselect(struct socket *so, struct thread *td,
202         struct timeval *tv, int events)
203 {
204         struct timeval atv,rtv,ttv;
205         struct proc *p = td->td_proc;
206         int timo,error=0;
207
208         KKASSERT(p);
209
210         if (tv) {
211                 atv=*tv;
212                 if (itimerfix(&atv)) {
213                         error = EINVAL;
214                         goto done;
215                 }
216                 getmicrouptime(&rtv);
217                 timevaladd(&atv, &rtv);
218         }
219         timo = 0;
220 retry:
221         p->p_flag |= P_SELECT;
222         error = ncp_poll(so, events);
223         if (error) {
224                 error = 0;
225                 goto done;
226         }
227         if (tv) {
228                 getmicrouptime(&rtv);
229                 if (timevalcmp(&rtv, &atv, >=))
230                         goto done;
231                 ttv=atv;
232                 timevalsub(&ttv, &rtv);
233                 timo = tvtohz_high(&ttv);
234         }
235         crit_enter();
236         if ((p->p_flag & P_SELECT) == 0) {
237                 crit_exit();
238                 goto retry;
239         }
240         p->p_flag &= ~P_SELECT;
241         error = tsleep((caddr_t)&selwait, 0, "ncpslt", timo);
242         crit_exit();
243 done:
244         p->p_flag &= ~P_SELECT;
245         if (error == ERESTART) {
246 /*              printf("Signal: %x", CURSIG(p));*/
247                 error = 0;
248         }
249         return (error);
250 }
251
252 #ifdef IPX
253 /* 
254  * Connect to specified server via IPX
255  */
256 int
257 ncp_sock_connect_ipx(struct ncp_conn *conn) {
258         struct sockaddr_ipx sipx;
259         struct ipxpcb *npcb;
260         struct thread *td = conn->td;
261         int addrlen, error, count;
262
263         sipx.sipx_port = htons(0);
264
265         for (count = 0;;count++) {
266                 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
267                         error = EADDRINUSE;
268                         goto bad;
269                 }
270                 conn->ncp_so = conn->wdg_so = NULL;
271                 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td));
272                 if (conn->li.opt & NCP_OPT_WDOG)
273                         checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM,0,td));
274                 addrlen = sizeof(sipx);
275                 sipx.sipx_family = AF_IPX;
276                 ipx_setnullnet(sipx.sipx_addr);
277                 ipx_setnullhost(sipx.sipx_addr);
278                 sipx.sipx_len = addrlen;
279                 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
280                 if (error == 0) {
281                         if ((conn->li.opt & NCP_OPT_WDOG) == 0)
282                                 break;
283                         sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
284                         sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
285                         ipx_setnullnet(sipx.sipx_addr);
286                         ipx_setnullhost(sipx.sipx_addr);
287                         error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
288                 }
289                 if (!error) break;
290                 if (error != EADDRINUSE) goto bad;
291                 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
292                 soclose(conn->ncp_so, FNONBLOCK);
293                 if (conn->wdg_so)
294                         soclose(conn->wdg_so, FNONBLOCK);
295         }
296         npcb = sotoipxpcb(conn->ncp_so);
297         npcb->ipxp_dpt = IPXPROTO_NCP;
298         /* IPXrouted must be running, i.e. route must be presented */
299         conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
300         checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
301         if (conn->wdg_so) {
302                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
303                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
304         }
305         if (!error) {
306                 conn->flags |= NCPFL_SOCONN;
307         }
308 #ifdef NCPBURST
309         if (ncp_burst_enabled) {
310                 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
311                 bzero(&sipx, sizeof(sipx));
312                 sipx.sipx_len = sizeof(sipx);
313                 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
314                 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
315         }
316 #endif
317         if (!error) {
318                 conn->flags |= NCPFL_SOCONN;
319                 ncp_sock_checksum(conn, 0);
320         }
321         return error;
322 bad:
323         ncp_sock_disconnect(conn);
324         return (error);
325 }
326
327 int
328 ncp_sock_checksum(struct ncp_conn *conn, int enable) {
329
330 #ifdef SO_IPX_CHECKSUM
331         if (enable) {
332                 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
333         } else {
334                 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
335         }
336 #endif
337         return 0;
338 }
339 #endif
340
341 #ifdef INET
342 /* 
343  * Connect to specified server via IP
344  */
345 int
346 ncp_sock_connect_in(struct ncp_conn *conn) {
347         struct sockaddr_in sin;
348         struct thread *td = conn->td;
349         int addrlen=sizeof(sin), error;
350
351         conn->flags = 0;
352         bzero(&sin,addrlen);
353         conn->ncp_so = conn->wdg_so = NULL;
354         checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td));
355         sin.sin_family = AF_INET;
356         sin.sin_len = addrlen;
357         checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
358         checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
359         if  (!error)
360                 conn->flags |= NCPFL_SOCONN;
361         return error;
362 bad:
363         ncp_sock_disconnect(conn);
364         return (error);
365 }
366 #endif
367
368
369 /*
370  * Connection expected to be locked
371  */
372 int
373 ncp_sock_disconnect(struct ncp_conn *conn) {
374         struct socket *so;
375         conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
376         if (conn->ncp_so) {
377                 so = conn->ncp_so;
378                 conn->ncp_so = (struct socket *)0;
379                 soshutdown(so, 2);
380                 soclose(so, FNONBLOCK);
381         }
382         if (conn->wdg_so) {
383                 so = conn->wdg_so;
384                 conn->wdg_so = (struct socket *)0;
385                 soshutdown(so, 2);
386                 soclose(so, FNONBLOCK);
387         }
388 #ifdef NCPBURST
389         if (conn->bc_so) {
390                 so = conn->bc_so;
391                 conn->bc_so = (struct socket *)NULL;
392                 soshutdown(so, 2);
393                 soclose(so, FNONBLOCK);
394         }
395 #endif
396         return 0;
397 }
398
399 #ifdef IPX
400 static void
401 ncp_watchdog(struct ncp_conn *conn) {
402         char *buf;
403         struct mbuf *m;
404         int error, len, flags;
405         struct socket *so;
406         struct sockaddr *sa;
407         struct uio auio;
408
409         sa = NULL;
410         while (conn->wdg_so) { /* not a loop */
411                 so = conn->wdg_so;
412                 auio.uio_resid = len = 1000000;
413                 auio.uio_td = curthread;
414                 flags = MSG_DONTWAIT;
415                 error = so_pru_soreceive(so, (struct sockaddr**)&sa, &auio, &m,
416                     NULL, &flags);
417                 if (error) break;
418                 len -= auio.uio_resid;
419                 NCPSDEBUG("got watch dog %d\n",len);
420                 if (len != 2) break;
421                 buf = mtod(m, char*);
422                 if (buf[1] != '?') break;
423                 buf[1] = 'Y';
424                 error = so_pru_sosend(so, sa, NULL, m, NULL, 0, curthread);
425                 NCPSDEBUG("send watch dog %d\n",error);
426                 break;
427         }
428         if (sa) FREE(sa, M_SONAME);
429         return;
430 }
431 #endif /* IPX */
432
433 void
434 ncp_check_conn(struct ncp_conn *conn) {
435         if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
436                 return;
437         crit_enter();
438         ncp_check_rq(conn);
439         crit_exit();
440 #ifdef IPX
441         ncp_watchdog(conn);
442 #endif
443 }