]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netncp/ncp_sock.c
This commit was generated by cvs2svn to compensate for changes in r163820,
[FreeBSD/FreeBSD.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 /*int ncp_poll(struct socket *so, int events);*/
69 /*static int ncp_getsockname(struct socket *so, caddr_t asa, int *alen);*/
70 static int ncp_soconnect(struct socket *so, struct sockaddr *target,
71                          struct thread *td);
72
73
74 /* This will need only if native IP used, or (unlikely) NCP will be
75  * implemented on the socket level
76  */
77 static int
78 ncp_soconnect(struct socket *so, struct sockaddr *target, struct thread *td)
79 {
80         int error, s;
81
82         error = soconnect(so, (struct sockaddr*)target, td);
83         if (error)
84                 return error;
85         /*
86          * Wait for the connection to complete. Cribbed from the
87          * connect system call but with the wait timing out so
88          * that interruptible mounts don't hang here for a long time.
89          */
90         error = EIO;
91         s = splnet();
92         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
93                 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "ncpcon", 2 * hz);
94                 if ((so->so_state & SS_ISCONNECTING) &&
95                     so->so_error == 0 /*&& rep &&*/) {
96                         so->so_state &= ~SS_ISCONNECTING;
97                         splx(s);
98                         goto bad;
99                 }
100         }
101         if (so->so_error) {
102                 error = so->so_error;
103                 so->so_error = 0;
104                 splx(s);
105                 goto bad;
106         }
107                 splx(s);
108         error=0;
109 bad:
110         return error;
111 }
112 #ifdef notyet
113 static int
114 ncp_getsockname(struct socket *so, caddr_t asa, int *alen) {
115         struct sockaddr *sa;
116         int len=0, error;
117
118         sa = 0;
119         error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
120         if (error==0) {
121                 if (sa) {
122                         len = min(len, sa->sa_len);
123                         bcopy(sa, (caddr_t)asa, (u_int)len);
124                 }
125                 *alen=len;
126         }
127         if (sa)
128                 FREE(sa, M_SONAME);
129         return (error);
130 }
131 #endif
132 int ncp_sock_recv(struct socket *so, struct mbuf **mp, int *rlen)
133 {
134         struct uio auio;
135         struct thread *td = curthread; /* XXX */
136         int error,flags,len;
137
138         auio.uio_resid = len = 1000000;
139         auio.uio_td = td;
140         flags = MSG_DONTWAIT;
141
142 /*      error = soreceive(so, 0, &auio, (struct mbuf **)0, (struct mbuf **)0,
143             &flags);*/
144         error = soreceive(so, 0, &auio, mp, (struct mbuf **)0, &flags);
145         *rlen = len - auio.uio_resid;
146 /*      if (!error) {
147             *rlen=iov.iov_len;
148         } else
149             *rlen=0;*/
150 #ifdef NCP_SOCKET_DEBUG
151         if (error)
152                 printf("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->nr_conn;
163         struct mbuf *m;
164         int error, flags=0;
165         int sendwait;
166
167         for (;;) {
168                 m = m_copym(top, 0, M_COPYALL, M_TRYWAIT);
169 /*              NCPDDEBUG(m);*/
170                 error = sosend(so, to, 0, m, 0, flags, td);
171                 if (error == 0 || error == EINTR || error == ENETDOWN)
172                         break;
173                 if (rqp->rexmit == 0) break;
174                 rqp->rexmit--;
175                 tsleep(&sendwait, PWAIT, "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         struct thread *td = curthread;
189         int revents;
190
191         /* Fake up enough state to look like we are in poll(2). */
192         mtx_lock(&sellock);
193         mtx_lock_spin(&sched_lock);
194         td->td_flags |= TDF_SELECT;
195         mtx_unlock_spin(&sched_lock);
196         mtx_unlock(&sellock);
197         TAILQ_INIT(&td->td_selq);
198
199         revents = sopoll(so, events, NULL, td);
200
201         /* Tear down the fake poll(2) state. */
202         mtx_lock(&sellock);
203         clear_selinfo_list(td);
204         mtx_lock_spin(&sched_lock);
205         td->td_flags &= ~TDF_SELECT;
206         mtx_unlock_spin(&sched_lock);
207         mtx_unlock(&sellock);
208
209         return (revents);
210 }
211
212 int
213 ncp_sock_rselect(struct socket *so, struct thread *td, struct timeval *tv,
214                  int events)
215 {
216         struct timeval atv, rtv, ttv;
217         int ncoll, timo, error, revents;
218
219         if (tv) {
220                 atv = *tv;
221                 if (itimerfix(&atv)) {
222                         error = EINVAL;
223                         goto done_noproclock;
224                 }
225                 getmicrouptime(&rtv);
226                 timevaladd(&atv, &rtv);
227         }
228         timo = 0;
229         mtx_lock(&sellock);
230
231 retry:
232         ncoll = nselcoll;
233         mtx_lock_spin(&sched_lock);
234         td->td_flags |= TDF_SELECT;
235         mtx_unlock_spin(&sched_lock);
236         mtx_unlock(&sellock);
237
238         TAILQ_INIT(&td->td_selq);
239         revents = sopoll(so, events, NULL, td);
240         mtx_lock(&sellock);
241         if (revents) {
242                 error = 0;
243                 goto done;
244         }
245         if (tv) {
246                 getmicrouptime(&rtv);
247                 if (timevalcmp(&rtv, &atv, >=)) {
248                         error = EWOULDBLOCK;
249                         goto done;
250                 }
251                 ttv = atv;
252                 timevalsub(&ttv, &rtv);
253                 timo = tvtohz(&ttv);
254         }
255         /*
256          * An event of our interest may occur during locking a thread.
257          * In order to avoid missing the event that occurred during locking
258          * the process, test TDF_SELECT and rescan file descriptors if
259          * necessary.
260          */
261         mtx_lock_spin(&sched_lock);
262         if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
263                 mtx_unlock_spin(&sched_lock);
264                 goto retry;
265         }
266         mtx_unlock_spin(&sched_lock);
267
268         if (timo > 0)
269                 error = cv_timedwait(&selwait, &sellock, timo);
270         else {
271                 cv_wait(&selwait, &sellock);
272                 error = 0;
273         }
274
275 done:
276         clear_selinfo_list(td);
277
278         mtx_lock_spin(&sched_lock);
279         td->td_flags &= ~TDF_SELECT;
280         mtx_unlock_spin(&sched_lock);
281         mtx_unlock(&sellock);
282
283 done_noproclock:
284         if (error == ERESTART)
285                 error = 0;
286         return (error);
287 }
288
289 /*
290  * Connect to specified server via IPX
291  */
292 static int
293 ncp_sock_connect_ipx(struct ncp_conn *conn)
294 {
295         struct sockaddr_ipx sipx;
296         struct ipxpcb *npcb;
297         struct thread *td = conn->td;
298         int addrlen, error, count;
299
300         sipx.sipx_port = htons(0);
301
302         for (count = 0;;count++) {
303                 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
304                         error = EADDRINUSE;
305                         goto bad;
306                 }
307                 conn->ncp_so = conn->wdg_so = NULL;
308                 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
309                 if (conn->li.opt & NCP_OPT_WDOG)
310                         checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
311                 addrlen = sizeof(sipx);
312                 sipx.sipx_family = AF_IPX;
313                 ipx_setnullnet(sipx.sipx_addr);
314                 ipx_setnullhost(sipx.sipx_addr);
315                 sipx.sipx_len = addrlen;
316                 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
317                 if (error == 0) {
318                         if ((conn->li.opt & NCP_OPT_WDOG) == 0)
319                                 break;
320                         sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
321                         sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
322                         ipx_setnullnet(sipx.sipx_addr);
323                         ipx_setnullhost(sipx.sipx_addr);
324                         error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
325                 }
326                 if (!error) break;
327                 if (error != EADDRINUSE) goto bad;
328                 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
329                 soclose(conn->ncp_so);
330                 if (conn->wdg_so)
331                         soclose(conn->wdg_so);
332         }
333         npcb = sotoipxpcb(conn->ncp_so);
334         npcb->ipxp_dpt = IPXPROTO_NCP;
335         /* IPXrouted must be running, i.e. route must be presented */
336         conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
337         checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
338         if (conn->wdg_so) {
339                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
340                 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
341         }
342         if (!error) {
343                 conn->flags |= NCPFL_SOCONN;
344         }
345 #ifdef NCPBURST
346         if (ncp_burst_enabled) {
347                 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
348                 bzero(&sipx, sizeof(sipx));
349                 sipx.sipx_len = sizeof(sipx);
350                 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
351                 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
352         }
353 #endif
354         if (!error) {
355                 conn->flags |= NCPFL_SOCONN;
356                 ncp_sock_checksum(conn, 0);
357         }
358         return error;
359 bad:
360         ncp_sock_disconnect(conn);
361         return (error);
362 }
363
364 int
365 ncp_sock_checksum(struct ncp_conn *conn, int enable)
366 {
367
368         if (enable) {
369                 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
370         } else {
371                 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
372         }
373         return 0;
374 }
375
376 /*
377  * Connect to specified server via IP
378  */
379 static int
380 ncp_sock_connect_in(struct ncp_conn *conn)
381 {
382         struct sockaddr_in sin;
383         struct thread *td = conn->td;
384         int addrlen = sizeof(sin), error;
385
386         conn->flags = 0;
387         bzero(&sin,addrlen);
388         conn->ncp_so = conn->wdg_so = NULL;
389         checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
390         sin.sin_family = AF_INET;
391         sin.sin_len = addrlen;
392         checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
393         checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
394         if  (!error)
395                 conn->flags |= NCPFL_SOCONN;
396         return error;
397 bad:
398         ncp_sock_disconnect(conn);
399         return (error);
400 }
401
402 int
403 ncp_sock_connect(struct ncp_conn *ncp)
404 {
405         int error;
406
407         switch (ncp->li.saddr.sa_family) {
408             case AF_IPX:
409                 error = ncp_sock_connect_ipx(ncp);
410                 break;
411             case AF_INET:
412                 error = ncp_sock_connect_in(ncp);
413                 break;
414             default:
415                 return EPROTONOSUPPORT;
416         }
417         return error;
418 }
419
420 /*
421  * Connection expected to be locked
422  */
423 int
424 ncp_sock_disconnect(struct ncp_conn *conn) {
425         register struct socket *so;
426         conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
427         if (conn->ncp_so) {
428                 so = conn->ncp_so;
429                 conn->ncp_so = (struct socket *)0;
430                 soshutdown(so, 2);
431                 soclose(so);
432         }
433         if (conn->wdg_so) {
434                 so = conn->wdg_so;
435                 conn->wdg_so = (struct socket *)0;
436                 soshutdown(so, 2);
437                 soclose(so);
438         }
439 #ifdef NCPBURST
440         if (conn->bc_so) {
441                 so = conn->bc_so;
442                 conn->bc_so = (struct socket *)NULL;
443                 soshutdown(so, 2);
444                 soclose(so);
445         }
446 #endif
447         return 0;
448 }
449
450 static void
451 ncp_watchdog(struct ncp_conn *conn) {
452         char *buf;
453         struct mbuf *m;
454         int error, len, flags;
455         struct socket *so;
456         struct sockaddr *sa;
457         struct uio auio;
458
459         sa = NULL;
460         while (conn->wdg_so) { /* not a loop */
461                 so = conn->wdg_so;
462                 auio.uio_resid = len = 1000000;
463                 auio.uio_td = curthread;
464                 flags = MSG_DONTWAIT;
465                 error = soreceive(so, (struct sockaddr**)&sa, &auio, &m,
466                     (struct mbuf**)0, &flags);
467                 if (error) break;
468                 len -= auio.uio_resid;
469                 NCPSDEBUG("got watch dog %d\n",len);
470                 if (len != 2) break;
471                 buf = mtod(m, char*);
472                 if (buf[1] != '?') break;
473                 buf[1] = 'Y';
474                 error = sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
475                 NCPSDEBUG("send watch dog %d\n",error);
476                 break;
477         }
478         if (sa) FREE(sa, M_SONAME);
479         return;
480 }
481
482 void
483 ncp_check_conn(struct ncp_conn *conn) {
484         int s;
485
486         if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
487                 return;
488         s = splnet();
489         ncp_check_rq(conn);
490         splx(s);
491         if (conn->li.saddr.sa_family == AF_IPX)
492                 ncp_watchdog(conn);
493 }