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