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