]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/netncp/ncp_sock.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / netncp / ncp_sock.c
1 /*-
2  * Copyright (c) 1999, 2001 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  * Low level socket routines
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/errno.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
48 #include <sys/kernel.h>
49 #include <sys/uio.h>
50 #include <sys/syslog.h>
51 #include <sys/mbuf.h>
52 #include <sys/condvar.h>
53 #include <net/route.h>
54
55 #include <netipx/ipx.h>
56 #include <netipx/ipx_pcb.h>
57
58 #include <netncp/ncp.h>
59 #include <netncp/ncp_conn.h>
60 #include <netncp/ncp_sock.h>
61 #include <netncp/ncp_subr.h>
62 #include <netncp/ncp_rq.h>
63
64 #define ipx_setnullnet(x) ((x).x_net.s_net[0]=0); ((x).x_net.s_net[1]=0);
65 #define ipx_setnullhost(x) ((x).x_host.s_host[0] = 0); \
66         ((x).x_host.s_host[1] = 0); ((x).x_host.s_host[2] = 0);
67
68 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
69 static int ncp_soconnect(struct socket *so, struct sockaddr *target,
70                          struct thread *td);
71
72
73 /* This will need only if native IP used, or (unlikely) NCP will be
74  * implemented on the socket level
75  */
76 static int
77 ncp_soconnect(struct socket *so, struct sockaddr *target, struct thread *td)
78 {
79         int error, s;
80
81         error = soconnect(so, (struct sockaddr*)target, td);
82         if (error)
83                 return error;
84         /*
85          * Wait for the connection to complete. Cribbed from the
86          * connect system call but with the wait timing out so
87          * that interruptible mounts don't hang here for a long time.
88          */
89         error = EIO;
90         s = splnet();
91         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
92                 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
93                 if ((so->so_state & SS_ISCONNECTING) &&
94                     so->so_error == 0 /*&& rep &&*/) {
95                         so->so_state &= ~SS_ISCONNECTING;
96                         splx(s);
97                         goto bad;
98                 }
99         }
100         if (so->so_error) {
101                 error = so->so_error;
102                 so->so_error = 0;
103                 splx(s);
104                 goto bad;
105         }
106                 splx(s);
107         error=0;
108 bad:
109         return error;
110 }
111 #ifdef notyet
112 static int
113 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
114         struct sockaddr *sa;
115         int len=0, error;
116
117         sa = 0;
118         error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
119         if (error==0) {
120                 if (sa) {
121                         len = min(len, sa->sa_len);
122                         bcopy(sa, (caddr_t)asa, (u_int)len);
123                 }
124                 *alen=len;
125         }
126         if (sa)
127                 free(sa, M_SONAME);
128         return (error);
129 }
130 #endif
131 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
132 {
133         struct uio auio;
134         struct thread *td = curthread; /* XXX */
135         int error,flags,len;
136
137         auio.uio_resid = len = 1000000;
138         auio.uio_td = td;
139         flags = MSG_DONTWAIT;
140
141 /*      error = soreceive(so, 0, &auio, (struct mbuf **)0, (struct mbuf **)0,
142             &flags);*/
143         error = soreceive(so, 0, &auio, mp, (struct mbuf **)0, &flags);
144         *rlen = len - auio.uio_resid;
145 /*      if (!error) {
146             *rlen=iov.iov_len;
147         } else
148             *rlen=0;*/
149 #ifdef NCP_SOCKET_DEBUG
150         if (error)
151                 printf("ncp_recv: err=%d\n", error);
152 #endif
153         return (error);
154 }
155
156 int
157 ncp_sock_send(struct socket *so, struct mbuf *top, struct ncp_rq *rqp)
158 {
159         struct thread *td = curthread; /* XXX */
160         struct sockaddr *to = 0;
161         struct ncp_conn *conn = rqp->nr_conn;
162         struct mbuf *m;
163         int error, flags=0;
164
165         for (;;) {
166                 m = m_copym(top, 0, M_COPYALL, M_WAIT);
167 /*              NCPDDEBUG(m);*/
168                 error = sosend(so, to, 0, m, 0, flags, td);
169                 if (error == 0 || error == EINTR || error == ENETDOWN)
170                         break;
171                 if (rqp->rexmit == 0) break;
172                 rqp->rexmit--;
173                 pause("ncprsn", conn->li.timeout * hz);
174                 error = ncp_chkintr(conn, td);
175                 if (error == EINTR) break;
176         }
177         if (error) {
178                 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
179         }
180         return error;
181 }
182
183 /*
184  * Connect to specified server via IPX
185  */
186 static int
187 ncp_sock_connect_ipx(struct ncp_conn *conn)
188 {
189         struct sockaddr_ipx sipx;
190         struct ipxpcb *npcb;
191         struct thread *td = conn->td;
192         int addrlen, error, count;
193
194         sipx.sipx_port = htons(0);
195
196         for (count = 0;;count++) {
197                 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
198                         error = EADDRINUSE;
199                         goto bad;
200                 }
201                 conn->ncp_so = conn->wdg_so = NULL;
202                 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
203                 if (conn->li.opt & NCP_OPT_WDOG)
204                         checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
205                 addrlen = sizeof(sipx);
206                 sipx.sipx_family = AF_IPX;
207                 ipx_setnullnet(sipx.sipx_addr);
208                 ipx_setnullhost(sipx.sipx_addr);
209                 sipx.sipx_len = addrlen;
210                 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
211                 if (error == 0) {
212                         if ((conn->li.opt & NCP_OPT_WDOG) == 0)
213                                 break;
214                         sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
215                         sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
216                         ipx_setnullnet(sipx.sipx_addr);
217                         ipx_setnullhost(sipx.sipx_addr);
218                         error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
219                 }
220                 if (!error) break;
221                 if (error != EADDRINUSE) goto bad;
222                 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
223                 soclose(conn->ncp_so);
224                 if (conn->wdg_so)
225                         soclose(conn->wdg_so);
226         }
227         npcb = sotoipxpcb(conn->ncp_so);
228         npcb->ipxp_dpt = IPXPROTO_NCP;
229         /* IPXrouted must be running, i.e. route must be presented */
230         conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
231         checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
232         if (conn->wdg_so) {
233                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
234                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
235         }
236         if (!error) {
237                 conn->flags |= NCPFL_SOCONN;
238         }
239 #ifdef NCPBURST
240         if (ncp_burst_enabled) {
241                 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
242                 bzero(&sipx, sizeof(sipx));
243                 sipx.sipx_len = sizeof(sipx);
244                 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
245                 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
246         }
247 #endif
248         if (!error) {
249                 conn->flags |= NCPFL_SOCONN;
250                 ncp_sock_checksum(conn, 0);
251         }
252         return error;
253 bad:
254         ncp_sock_disconnect(conn);
255         return (error);
256 }
257
258 int
259 ncp_sock_checksum(struct ncp_conn *conn, int enable)
260 {
261
262         if (enable) {
263                 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
264         } else {
265                 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
266         }
267         return 0;
268 }
269
270 /*
271  * Connect to specified server via IP
272  */
273 static int
274 ncp_sock_connect_in(struct ncp_conn *conn)
275 {
276         struct sockaddr_in sin;
277         struct thread *td = conn->td;
278         int addrlen = sizeof(sin), error;
279
280         conn->flags = 0;
281         bzero(&sin,addrlen);
282         conn->ncp_so = conn->wdg_so = NULL;
283         checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
284         sin.sin_family = AF_INET;
285         sin.sin_len = addrlen;
286         checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
287         checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
288         if  (!error)
289                 conn->flags |= NCPFL_SOCONN;
290         return error;
291 bad:
292         ncp_sock_disconnect(conn);
293         return (error);
294 }
295
296 int
297 ncp_sock_connect(struct ncp_conn *ncp)
298 {
299         int error;
300
301         switch (ncp->li.saddr.sa_family) {
302             case AF_IPX:
303                 error = ncp_sock_connect_ipx(ncp);
304                 break;
305             case AF_INET:
306                 error = ncp_sock_connect_in(ncp);
307                 break;
308             default:
309                 return EPROTONOSUPPORT;
310         }
311         return error;
312 }
313
314 /*
315  * Connection expected to be locked
316  */
317 int
318 ncp_sock_disconnect(struct ncp_conn *conn) {
319         register struct socket *so;
320         conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
321         if (conn->ncp_so) {
322                 so = conn->ncp_so;
323                 conn->ncp_so = (struct socket *)0;
324                 soshutdown(so, 2);
325                 soclose(so);
326         }
327         if (conn->wdg_so) {
328                 so = conn->wdg_so;
329                 conn->wdg_so = (struct socket *)0;
330                 soshutdown(so, 2);
331                 soclose(so);
332         }
333 #ifdef NCPBURST
334         if (conn->bc_so) {
335                 so = conn->bc_so;
336                 conn->bc_so = (struct socket *)NULL;
337                 soshutdown(so, 2);
338                 soclose(so);
339         }
340 #endif
341         return 0;
342 }
343
344 static void
345 ncp_watchdog(struct ncp_conn *conn) {
346         char *buf;
347         struct mbuf *m;
348         int error, len, flags;
349         struct socket *so;
350         struct sockaddr *sa;
351         struct uio auio;
352
353         sa = NULL;
354         while (conn->wdg_so) { /* not a loop */
355                 so = conn->wdg_so;
356                 auio.uio_resid = len = 1000000;
357                 auio.uio_td = curthread;
358                 flags = MSG_DONTWAIT;
359                 error = soreceive(so, (struct sockaddr**)&sa, &auio, &m,
360                     (struct mbuf**)0, &flags);
361                 if (error) break;
362                 len -= auio.uio_resid;
363                 NCPSDEBUG("got watch dog %d\n",len);
364                 if (len != 2) break;
365                 buf = mtod(m, char*);
366                 if (buf[1] != '?') break;
367                 buf[1] = 'Y';
368                 error = sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
369                 NCPSDEBUG("send watch dog %d\n",error);
370                 break;
371         }
372         if (sa) free(sa, M_SONAME);
373         return;
374 }
375
376 void
377 ncp_check_conn(struct ncp_conn *conn) {
378         int s;
379
380         if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
381                 return;
382         s = splnet();
383         ncp_check_rq(conn);
384         splx(s);
385         if (conn->li.saddr.sa_family == AF_IPX)
386                 ncp_watchdog(conn);
387 }