FreeBSD/Linux Kernel Cross Reference
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
166 for (;;) {
167 m = m_copym(top, 0, M_COPYALL, M_TRYWAIT);
168 /* NCPDDEBUG(m);*/
169 error = sosend(so, to, 0, m, 0, flags, td);
170 if (error == 0 || error == EINTR || error == ENETDOWN)
171 break;
172 if (rqp->rexmit == 0) break;
173 rqp->rexmit--;
174 pause("ncprsn", conn->li.timeout * hz);
175 error = ncp_chkintr(conn, td);
176 if (error == EINTR) break;
177 }
178 if (error) {
179 log(LOG_INFO, "ncp_send: error %d for server %s", error, conn->li.server);
180 }
181 return error;
182 }
183
184 int
185 ncp_poll(struct socket *so, int events)
186 {
187 struct thread *td = curthread;
188 int revents;
189
190 /* Fake up enough state to look like we are in poll(2). */
191 mtx_lock(&sellock);
192 thread_lock(td);
193 td->td_flags |= TDF_SELECT;
194 thread_unlock(td);
195 mtx_unlock(&sellock);
196 TAILQ_INIT(&td->td_selq);
197
198 revents = sopoll(so, events, NULL, td);
199
200 /* Tear down the fake poll(2) state. */
201 mtx_lock(&sellock);
202 clear_selinfo_list(td);
203 thread_lock(td);
204 td->td_flags &= ~TDF_SELECT;
205 thread_unlock(td);
206 mtx_unlock(&sellock);
207
208 return (revents);
209 }
210
211 int
212 ncp_sock_rselect(struct socket *so, struct thread *td, struct timeval *tv,
213 int events)
214 {
215 struct timeval atv, rtv, ttv;
216 int ncoll, timo, error, revents;
217
218 if (tv) {
219 atv = *tv;
220 if (itimerfix(&atv)) {
221 error = EINVAL;
222 goto done_noproclock;
223 }
224 getmicrouptime(&rtv);
225 timevaladd(&atv, &rtv);
226 }
227 timo = 0;
228 mtx_lock(&sellock);
229
230 retry:
231 ncoll = nselcoll;
232 thread_lock(td);
233 td->td_flags |= TDF_SELECT;
234 thread_unlock(td);
235 mtx_unlock(&sellock);
236
237 TAILQ_INIT(&td->td_selq);
238 revents = sopoll(so, events, NULL, td);
239 mtx_lock(&sellock);
240 if (revents) {
241 error = 0;
242 goto done;
243 }
244 if (tv) {
245 getmicrouptime(&rtv);
246 if (timevalcmp(&rtv, &atv, >=)) {
247 error = EWOULDBLOCK;
248 goto done;
249 }
250 ttv = atv;
251 timevalsub(&ttv, &rtv);
252 timo = tvtohz(&ttv);
253 }
254 /*
255 * An event of our interest may occur during locking a thread.
256 * In order to avoid missing the event that occurred during locking
257 * the process, test TDF_SELECT and rescan file descriptors if
258 * necessary.
259 */
260 thread_lock(td);
261 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
262 thread_unlock(td);
263 goto retry;
264 }
265 thread_unlock(td);
266
267 if (timo > 0)
268 error = cv_timedwait(&selwait, &sellock, timo);
269 else {
270 cv_wait(&selwait, &sellock);
271 error = 0;
272 }
273
274 done:
275 clear_selinfo_list(td);
276
277 thread_lock(td);
278 td->td_flags &= ~TDF_SELECT;
279 thread_unlock(td);
280 mtx_unlock(&sellock);
281
282 done_noproclock:
283 if (error == ERESTART)
284 error = 0;
285 return (error);
286 }
287
288 /*
289 * Connect to specified server via IPX
290 */
291 static int
292 ncp_sock_connect_ipx(struct ncp_conn *conn)
293 {
294 struct sockaddr_ipx sipx;
295 struct ipxpcb *npcb;
296 struct thread *td = conn->td;
297 int addrlen, error, count;
298
299 sipx.sipx_port = htons(0);
300
301 for (count = 0;;count++) {
302 if (count > (IPXPORT_WELLKNOWN-IPXPORT_RESERVED)*2) {
303 error = EADDRINUSE;
304 goto bad;
305 }
306 conn->ncp_so = conn->wdg_so = NULL;
307 checkbad(socreate(AF_IPX, &conn->ncp_so, SOCK_DGRAM, 0, td->td_ucred, td));
308 if (conn->li.opt & NCP_OPT_WDOG)
309 checkbad(socreate(AF_IPX, &conn->wdg_so, SOCK_DGRAM, 0, td->td_ucred, td));
310 addrlen = sizeof(sipx);
311 sipx.sipx_family = AF_IPX;
312 ipx_setnullnet(sipx.sipx_addr);
313 ipx_setnullhost(sipx.sipx_addr);
314 sipx.sipx_len = addrlen;
315 error = sobind(conn->ncp_so, (struct sockaddr *)&sipx, td);
316 if (error == 0) {
317 if ((conn->li.opt & NCP_OPT_WDOG) == 0)
318 break;
319 sipx.sipx_addr = sotoipxpcb(conn->ncp_so)->ipxp_laddr;
320 sipx.sipx_port = htons(ntohs(sipx.sipx_port) + 1);
321 ipx_setnullnet(sipx.sipx_addr);
322 ipx_setnullhost(sipx.sipx_addr);
323 error = sobind(conn->wdg_so, (struct sockaddr *)&sipx, td);
324 }
325 if (!error) break;
326 if (error != EADDRINUSE) goto bad;
327 sipx.sipx_port = htons((ntohs(sipx.sipx_port)+4) & 0xfff8);
328 soclose(conn->ncp_so);
329 if (conn->wdg_so)
330 soclose(conn->wdg_so);
331 }
332 npcb = sotoipxpcb(conn->ncp_so);
333 npcb->ipxp_dpt = IPXPROTO_NCP;
334 /* IPXrouted must be running, i.e. route must be presented */
335 conn->li.ipxaddr.sipx_len = sizeof(struct sockaddr_ipx);
336 checkbad(ncp_soconnect(conn->ncp_so, &conn->li.saddr, td));
337 if (conn->wdg_so) {
338 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_net = npcb->ipxp_laddr.x_net;
339 sotoipxpcb(conn->wdg_so)->ipxp_laddr.x_host= npcb->ipxp_laddr.x_host;
340 }
341 if (!error) {
342 conn->flags |= NCPFL_SOCONN;
343 }
344 #ifdef NCPBURST
345 if (ncp_burst_enabled) {
346 checkbad(socreate(AF_IPX, &conn->bc_so, SOCK_DGRAM, 0, td));
347 bzero(&sipx, sizeof(sipx));
348 sipx.sipx_len = sizeof(sipx);
349 checkbad(sobind(conn->bc_so, (struct sockaddr *)&sipx, td));
350 checkbad(ncp_soconnect(conn->bc_so, &conn->li.saddr, td));
351 }
352 #endif
353 if (!error) {
354 conn->flags |= NCPFL_SOCONN;
355 ncp_sock_checksum(conn, 0);
356 }
357 return error;
358 bad:
359 ncp_sock_disconnect(conn);
360 return (error);
361 }
362
363 int
364 ncp_sock_checksum(struct ncp_conn *conn, int enable)
365 {
366
367 if (enable) {
368 sotoipxpcb(conn->ncp_so)->ipxp_flags |= IPXP_CHECKSUM;
369 } else {
370 sotoipxpcb(conn->ncp_so)->ipxp_flags &= ~IPXP_CHECKSUM;
371 }
372 return 0;
373 }
374
375 /*
376 * Connect to specified server via IP
377 */
378 static int
379 ncp_sock_connect_in(struct ncp_conn *conn)
380 {
381 struct sockaddr_in sin;
382 struct thread *td = conn->td;
383 int addrlen = sizeof(sin), error;
384
385 conn->flags = 0;
386 bzero(&sin,addrlen);
387 conn->ncp_so = conn->wdg_so = NULL;
388 checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td));
389 sin.sin_family = AF_INET;
390 sin.sin_len = addrlen;
391 checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
392 checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
393 if (!error)
394 conn->flags |= NCPFL_SOCONN;
395 return error;
396 bad:
397 ncp_sock_disconnect(conn);
398 return (error);
399 }
400
401 int
402 ncp_sock_connect(struct ncp_conn *ncp)
403 {
404 int error;
405
406 switch (ncp->li.saddr.sa_family) {
407 case AF_IPX:
408 error = ncp_sock_connect_ipx(ncp);
409 break;
410 case AF_INET:
411 error = ncp_sock_connect_in(ncp);
412 break;
413 default:
414 return EPROTONOSUPPORT;
415 }
416 return error;
417 }
418
419 /*
420 * Connection expected to be locked
421 */
422 int
423 ncp_sock_disconnect(struct ncp_conn *conn) {
424 register struct socket *so;
425 conn->flags &= ~(NCPFL_SOCONN | NCPFL_ATTACHED | NCPFL_LOGGED);
426 if (conn->ncp_so) {
427 so = conn->ncp_so;
428 conn->ncp_so = (struct socket *)0;
429 soshutdown(so, 2);
430 soclose(so);
431 }
432 if (conn->wdg_so) {
433 so = conn->wdg_so;
434 conn->wdg_so = (struct socket *)0;
435 soshutdown(so, 2);
436 soclose(so);
437 }
438 #ifdef NCPBURST
439 if (conn->bc_so) {
440 so = conn->bc_so;
441 conn->bc_so = (struct socket *)NULL;
442 soshutdown(so, 2);
443 soclose(so);
444 }
445 #endif
446 return 0;
447 }
448
449 static void
450 ncp_watchdog(struct ncp_conn *conn) {
451 char *buf;
452 struct mbuf *m;
453 int error, len, flags;
454 struct socket *so;
455 struct sockaddr *sa;
456 struct uio auio;
457
458 sa = NULL;
459 while (conn->wdg_so) { /* not a loop */
460 so = conn->wdg_so;
461 auio.uio_resid = len = 1000000;
462 auio.uio_td = curthread;
463 flags = MSG_DONTWAIT;
464 error = soreceive(so, (struct sockaddr**)&sa, &auio, &m,
465 (struct mbuf**)0, &flags);
466 if (error) break;
467 len -= auio.uio_resid;
468 NCPSDEBUG("got watch dog %d\n",len);
469 if (len != 2) break;
470 buf = mtod(m, char*);
471 if (buf[1] != '?') break;
472 buf[1] = 'Y';
473 error = sosend(so, (struct sockaddr*)sa, 0, m, 0, 0, curthread);
474 NCPSDEBUG("send watch dog %d\n",error);
475 break;
476 }
477 if (sa) FREE(sa, M_SONAME);
478 return;
479 }
480
481 void
482 ncp_check_conn(struct ncp_conn *conn) {
483 int s;
484
485 if (conn == NULL || !(conn->flags & NCPFL_ATTACHED))
486 return;
487 s = splnet();
488 ncp_check_rq(conn);
489 splx(s);
490 if (conn->li.saddr.sa_family == AF_IPX)
491 ncp_watchdog(conn);
492 }
Cache object: 912360a8ccc7987899501a0bc2767e0d
|