Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netsmb / smb_trantcp.c
blob179dd48d47781e54da99d30faecbd7b704395028
1 /* $NetBSD: smb_trantcp.c,v 1.42 2009/03/29 19:21:20 christos Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2000-2001 Boris Popov
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Boris Popov.
44 * 4. Neither the name of the author nor the names of any co-contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
60 * FreeBSD: src/sys/netsmb/smb_trantcp.c,v 1.17 2003/02/19 05:47:38 imp Exp
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: smb_trantcp.c,v 1.42 2009/03/29 19:21:20 christos Exp $");
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/proc.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/poll.h>
76 #include <sys/uio.h>
77 #include <sys/select.h>
79 #include <net/if.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/tcp.h>
85 #include <netsmb/mchain.h>
87 #include <netsmb/netbios.h>
89 #include <netsmb/smb.h>
90 #include <netsmb/smb_conn.h>
91 #include <netsmb/smb_tran.h>
92 #include <netsmb/smb_trantcp.h>
93 #include <netsmb/smb_subr.h>
95 #define M_NBDATA M_PCB
97 static int nb_tcpsndbuf = NB_SNDQ;
98 static int nb_tcprcvbuf = NB_RCVQ;
99 static const struct timespec nb_timo = { 15, 0 }; /* XXX sysctl? */
101 #define nb_sosend(so,m,flags,l) (*(so)->so_send)(so, NULL, (struct uio *)0, \
102 m, (struct mbuf *)0, flags, l)
104 static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
105 u_int8_t *rpcodep, struct lwp *l);
106 static int smb_nbst_disconnect(struct smb_vc *vcp, struct lwp *l);
108 static int
109 nb_setsockopt_int(struct socket *so, int level, int name, int val)
112 return so_setsockopt(NULL, so, level, name, &val, sizeof(val)); /* XXX */
115 static int
116 nbssn_rselect(struct nbpcb *nbp, const struct timespec *ts, int events,
117 struct lwp *l)
120 return pollsock(nbp->nbp_tso, ts, events);
123 static int
124 nb_intr(struct nbpcb *nbp, struct lwp *l)
126 return 0;
129 static void
130 nb_upcall(struct socket *so, void *arg, int events, int waitflag)
132 struct nbpcb *nbp = (void *)arg;
134 if (arg == NULL || nbp->nbp_selectid == NULL)
135 return;
136 wakeup(nbp->nbp_selectid);
139 static int
140 nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
142 u_int32_t *p = mtod(m, u_int32_t *);
144 *p = htonl((len & 0x1FFFF) | (type << 24));
145 return 0;
148 static int
149 nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
151 int error;
152 u_char seglen, *cp;
154 cp = snb->snb_name;
155 if (*cp == 0)
156 return EINVAL;
157 NBDEBUG(("[%s]\n", cp));
158 for (;;) {
159 seglen = (*cp) + 1;
160 error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
161 if (error)
162 return error;
163 if (seglen == 1)
164 break;
165 cp += seglen;
167 return 0;
170 static int
171 nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct lwp *l)
173 struct socket *so;
174 int error;
175 struct mbuf *m;
177 error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP, l, NULL);
178 if (error)
179 return error;
180 solock(so);
181 nbp->nbp_tso = so;
182 so->so_upcallarg = (void *)nbp;
183 so->so_upcall = nb_upcall;
184 so->so_rcv.sb_flags |= SB_UPCALL;
185 so->so_rcv.sb_flags &= ~SB_NOINTR;
186 so->so_snd.sb_flags &= ~SB_NOINTR;
187 so->so_rcv.sb_timeo = NB_SNDTIMEO;
188 so->so_snd.sb_timeo = NB_RCVTIMEO;
189 error = soreserve(so, nb_tcpsndbuf, nb_tcprcvbuf);
190 sounlock(so);
191 if (error)
192 goto bad;
193 nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
194 nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
195 m = m_get(M_WAIT, MT_SONAME);
196 *mtod(m, struct sockaddr *) = *(struct sockaddr *)to;
197 m->m_len = sizeof(struct sockaddr);
198 solock(so);
199 error = soconnect(so, m, l);
200 m_free(m);
201 if (error) {
202 sounlock(so);
203 goto bad;
205 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
206 sowait(so, false, 2 * hz);
207 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
208 (error = nb_intr(nbp, l)) != 0) {
209 so->so_state &= ~SS_ISCONNECTING;
210 sounlock(so);
211 goto bad;
214 if (so->so_error) {
215 error = so->so_error;
216 so->so_error = 0;
217 sounlock(so);
218 goto bad;
220 sounlock(so);
221 return 0;
222 bad:
223 smb_nbst_disconnect(nbp->nbp_vc, l);
224 return error;
227 static int
228 nbssn_rq_request(struct nbpcb *nbp, struct lwp *l)
230 struct mbchain mb, *mbp = &mb;
231 struct mdchain md, *mdp = &md;
232 struct mbuf *m0;
233 struct sockaddr_in sin;
234 u_short port;
235 u_int8_t rpcode;
236 int error, rplen;
238 error = mb_init(mbp);
239 if (error)
240 return error;
241 mb_put_uint32le(mbp, 0);
242 (void) nb_put_name(mbp, nbp->nbp_paddr);
243 (void) nb_put_name(mbp, nbp->nbp_laddr);
244 nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
245 error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, l);
246 if (!error) {
247 nbp->nbp_state = NBST_RQSENT;
249 mb_detach(mbp);
250 mb_done(mbp);
251 if (error)
252 return error;
253 error = nbssn_rselect(nbp, &nb_timo, POLLIN, l);
254 if (error == EWOULDBLOCK) { /* Timeout */
255 NBDEBUG(("initial request timeout\n"));
256 return ETIMEDOUT;
258 if (error) /* restart or interrupt */
259 return error;
260 error = nbssn_recv(nbp, &m0, &rplen, &rpcode, l);
261 if (error) {
262 NBDEBUG(("recv() error %d\n", error));
263 return error;
266 * Process NETBIOS reply
268 if (m0)
269 md_initm(mdp, m0);
270 error = 0;
271 do {
272 if (rpcode == NB_SSN_POSRESP) {
273 nbp->nbp_state = NBST_SESSION;
274 nbp->nbp_flags |= NBF_CONNECTED;
275 break;
277 if (rpcode != NB_SSN_RTGRESP) {
278 error = ECONNABORTED;
279 break;
281 if (rplen != 6) {
282 error = ECONNABORTED;
283 break;
285 md_get_mem(mdp, (void *)&sin.sin_addr, 4, MB_MSYSTEM);
286 md_get_uint16(mdp, &port);
287 sin.sin_port = port;
288 nbp->nbp_state = NBST_RETARGET;
289 smb_nbst_disconnect(nbp->nbp_vc, l);
290 error = nb_connect_in(nbp, &sin, l);
291 if (!error)
292 error = nbssn_rq_request(nbp, l);
293 if (error) {
294 smb_nbst_disconnect(nbp->nbp_vc, l);
295 break;
297 } while(0);
298 if (m0)
299 md_done(mdp);
300 return error;
303 static int
304 nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
305 u_int8_t *rpcodep, int flags, struct lwp *l)
307 struct socket *so = nbp->nbp_tso;
308 struct uio auio;
309 struct iovec aio;
310 u_int32_t len;
311 int error;
313 aio.iov_base = (void *)&len;
314 aio.iov_len = sizeof(len);
315 auio.uio_iov = &aio;
316 auio.uio_iovcnt = 1;
317 auio.uio_rw = UIO_READ;
318 auio.uio_offset = 0;
319 auio.uio_resid = sizeof(len);
320 UIO_SETUP_SYSSPACE(&auio);
321 error = (*so->so_receive)(so, NULL, &auio, NULL, NULL, &flags);
322 if (error)
323 return error;
324 if (auio.uio_resid > 0) {
325 SMBSDEBUG(("short reply\n"));
326 return EPIPE;
328 len = ntohl(len);
329 *rpcodep = (len >> 24) & 0xFF;
330 len &= 0x1ffff;
331 if (len > SMB_MAXPKTLEN) {
332 SMBERROR(("packet too long (%d)\n", len));
333 return EFBIG;
335 *lenp = len;
336 return 0;
339 static int
340 nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
341 u_int8_t *rpcodep, struct lwp *l)
343 struct socket *so = nbp->nbp_tso;
344 struct uio auio;
345 struct mbuf *m, *tm, *im;
346 u_int8_t rpcode;
347 int len, resid;
348 int error, rcvflg;
350 len = 0; /* XXX gcc */
351 rpcode = 0; /* XXX gcc */
353 if (so == NULL)
354 return ENOTCONN;
356 if (mpp)
357 *mpp = NULL;
358 m = NULL;
359 for(;;) {
361 * Poll for a response header.
362 * If we don't have one waiting, return.
364 error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, l);
365 if (so->so_state &
366 (SS_ISDISCONNECTING | SS_ISDISCONNECTED | SS_CANTRCVMORE)) {
367 nbp->nbp_state = NBST_CLOSED;
368 NBDEBUG(("session closed by peer\n"));
369 return ECONNRESET;
371 if (error)
372 return error;
373 if (len == 0 && nbp->nbp_state != NBST_SESSION)
374 break;
375 /* no data, try again */
376 if (rpcode == NB_SSN_KEEPALIVE)
377 continue;
380 * Loop, blocking, for data following the response header.
382 * Note that we can't simply block here with MSG_WAITALL for the
383 * entire response size, as it may be larger than the TCP
384 * slow-start window that the sender employs. This will result
385 * in the sender stalling until the delayed ACK is sent, then
386 * resuming slow-start, resulting in very poor performance.
388 * Instead, we never request more than NB_SORECEIVE_CHUNK
389 * bytes at a time, resulting in an ack being pushed by
390 * the TCP code at the completion of each call.
392 resid = len;
393 while (resid > 0) {
394 tm = NULL;
395 rcvflg = MSG_WAITALL;
396 memset(&auio, 0, sizeof(auio));
397 auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
398 /* not need to setup uio_vmspace */
399 resid -= auio.uio_resid;
401 * Spin until we have collected everything in
402 * this chunk.
404 do {
405 rcvflg = MSG_WAITALL;
406 error = (*so->so_receive)(so, NULL, &auio, &tm,
407 NULL, &rcvflg);
408 } while (error == EWOULDBLOCK || error == EINTR ||
409 error == ERESTART);
410 if (error)
411 goto out;
412 /* short return guarantees unhappiness */
413 if (auio.uio_resid > 0) {
414 SMBERROR(("packet is shorter than expected\n"));
415 error = EPIPE;
416 goto out;
418 /* append received chunk to previous chunk(s) */
419 if (m == NULL) {
420 m = tm;
421 } else {
423 * Just glue the new chain on the end.
424 * Consumer will pullup as required.
426 for (im = m; im->m_next != NULL; im = im->m_next)
428 im->m_next = tm;
431 /* got a session/message packet? */
432 if (nbp->nbp_state == NBST_SESSION &&
433 rpcode == NB_SSN_MESSAGE)
434 break;
435 /* drop packet and try for another */
436 NBDEBUG(("non-session packet %x\n", rpcode));
437 if (m) {
438 m_freem(m);
439 m = NULL;
443 out:
444 if (error) {
445 if (m)
446 m_freem(m);
447 return error;
449 if (mpp)
450 *mpp = m;
451 else
452 m_freem(m);
453 *lenp = len;
454 *rpcodep = rpcode;
455 return 0;
459 * SMB transport interface
461 static int
462 smb_nbst_create(struct smb_vc *vcp, struct lwp *l)
464 struct nbpcb *nbp;
466 nbp = malloc(sizeof *nbp, M_NBDATA, M_WAITOK|M_ZERO);
467 nbp->nbp_state = NBST_CLOSED;
468 nbp->nbp_vc = vcp;
469 vcp->vc_tdata = nbp;
470 return 0;
473 static int
474 smb_nbst_done(struct smb_vc *vcp, struct lwp *l)
476 struct nbpcb *nbp = vcp->vc_tdata;
478 if (nbp == NULL)
479 return ENOTCONN;
480 smb_nbst_disconnect(vcp, l);
481 if (nbp->nbp_laddr)
482 free(nbp->nbp_laddr, M_SONAME);
483 if (nbp->nbp_paddr)
484 free(nbp->nbp_paddr, M_SONAME);
485 free(nbp, M_NBDATA);
486 return 0;
489 static int
490 smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct lwp *l)
492 struct nbpcb *nbp = vcp->vc_tdata;
493 struct sockaddr_nb *snb;
494 int error, slen;
496 NBDEBUG(("\n"));
497 error = EINVAL;
498 do {
499 if (nbp->nbp_flags & NBF_LOCADDR)
500 break;
502 * It is possible to create NETBIOS name in the kernel,
503 * but nothing prevents us to do it in the user space.
505 if (sap == NULL)
506 break;
507 slen = sap->sa_len;
508 if (slen < NB_MINSALEN)
509 break;
510 snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
511 if (snb == NULL) {
512 error = ENOMEM;
513 break;
515 nbp->nbp_laddr = snb;
516 nbp->nbp_flags |= NBF_LOCADDR;
517 error = 0;
518 } while(0);
519 return error;
522 static int
523 smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct lwp *l)
525 struct nbpcb *nbp = vcp->vc_tdata;
526 struct sockaddr_in sin;
527 struct sockaddr_nb *snb;
528 int error, slen;
530 NBDEBUG(("\n"));
531 if (nbp->nbp_tso != NULL)
532 return EISCONN;
533 if (nbp->nbp_laddr == NULL)
534 return EINVAL;
535 slen = sap->sa_len;
536 if (slen < NB_MINSALEN)
537 return EINVAL;
538 if (nbp->nbp_paddr) {
539 free(nbp->nbp_paddr, M_SONAME);
540 nbp->nbp_paddr = NULL;
542 snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
543 if (snb == NULL)
544 return ENOMEM;
545 nbp->nbp_paddr = snb;
546 sin = snb->snb_addrin;
547 error = nb_connect_in(nbp, &sin, l);
548 if (error)
549 return error;
550 error = nbssn_rq_request(nbp, l);
551 if (error)
552 smb_nbst_disconnect(vcp, l);
553 return error;
556 static int
557 smb_nbst_disconnect(struct smb_vc *vcp, struct lwp *l)
559 struct nbpcb *nbp = vcp->vc_tdata;
560 struct socket *so;
562 if (nbp == NULL || nbp->nbp_tso == NULL)
563 return ENOTCONN;
564 if ((so = nbp->nbp_tso) != NULL) {
565 nbp->nbp_flags &= ~NBF_CONNECTED;
566 nbp->nbp_tso = (struct socket *)NULL;
567 solock(so);
568 soshutdown(so, 2);
569 sounlock(so);
570 soclose(so);
572 if (nbp->nbp_state != NBST_RETARGET) {
573 nbp->nbp_state = NBST_CLOSED;
575 return 0;
578 static int
579 smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct lwp *l)
581 struct nbpcb *nbp = vcp->vc_tdata;
582 int error;
584 if (nbp->nbp_state != NBST_SESSION) {
585 error = ENOTCONN;
586 goto abort;
588 M_PREPEND(m0, 4, M_WAITOK);
589 if (m0 == NULL)
590 return ENOBUFS;
591 nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
592 error = nb_sosend(nbp->nbp_tso, m0, 0, l);
593 return error;
594 abort:
595 if (m0)
596 m_freem(m0);
597 return error;
601 static int
602 smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct lwp *l)
604 struct nbpcb *nbp = vcp->vc_tdata;
605 u_int8_t rpcode;
606 int error, rplen;
608 nbp->nbp_flags |= NBF_RECVLOCK;
609 error = nbssn_recv(nbp, mpp, &rplen, &rpcode, l);
610 nbp->nbp_flags &= ~NBF_RECVLOCK;
611 return error;
614 static void
615 smb_nbst_timo(struct smb_vc *vcp)
618 /* Nothing */
621 static void
622 smb_nbst_intr(struct smb_vc *vcp)
624 struct nbpcb *nbp = vcp->vc_tdata;
625 struct socket *so;
627 if (nbp == NULL || (so = nbp->nbp_tso) == NULL)
628 return;
630 solock(so);
631 sorwakeup(so);
632 sowwakeup(so);
633 sounlock(so);
636 static int
637 smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
639 struct timeval *tvp;
640 switch (param) {
641 case SMBTP_SNDSZ:
642 *(int*)data = nb_tcpsndbuf;
643 break;
644 case SMBTP_RCVSZ:
645 *(int*)data = nb_tcprcvbuf;
646 break;
647 case SMBTP_TIMEOUT:
648 tvp = (struct timeval *)data;
649 tvp->tv_sec = nb_timo.tv_sec;
650 tvp->tv_usec = nb_timo.tv_nsec / 1000;
651 break;
652 default:
653 return EINVAL;
655 return 0;
658 static int
659 smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
661 struct nbpcb *nbp = vcp->vc_tdata;
663 switch (param) {
664 case SMBTP_SELECTID:
665 nbp->nbp_selectid = data;
666 break;
667 default:
668 return EINVAL;
670 return 0;
674 * Check for fatal errors
676 static int
677 smb_nbst_fatal(struct smb_vc *vcp, int error)
679 switch (error) {
680 case ENOTCONN:
681 case ENETRESET:
682 case ECONNABORTED:
683 return 1;
685 return 0;
689 struct smb_tran_desc smb_tran_nbtcp_desc = {
690 SMBT_NBTCP,
691 smb_nbst_create, smb_nbst_done,
692 smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
693 smb_nbst_send, smb_nbst_recv,
694 smb_nbst_timo, smb_nbst_intr,
695 smb_nbst_getparam, smb_nbst_setparam,
696 smb_nbst_fatal,
697 { NULL, NULL },