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