Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / netproto / smb / smb_trantcp.c
1 /*
2  * Copyright (c) 2000-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  * $FreeBSD: src/sys/netsmb/smb_trantcp.c,v 1.3.2.1 2001/05/22 08:32:34 bp Exp $
33  * $DragonFly: src/sys/netproto/smb/smb_trantcp.c,v 1.2 2003/06/17 04:28:54 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/proc.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/poll.h>
45 #include <sys/uio.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53
54 #include <sys/mchain.h>
55
56 #include <netsmb/netbios.h>
57
58 #include <netsmb/smb.h>
59 #include <netsmb/smb_conn.h>
60 #include <netsmb/smb_tran.h>
61 #include <netsmb/smb_trantcp.h>
62 #include <netsmb/smb_subr.h>
63
64 #define M_NBDATA        M_PCB
65
66 static int smb_tcpsndbuf = 10 * 1024;
67 static int smb_tcprcvbuf = 10 * 1024;
68
69 SYSCTL_DECL(_net_smb);
70 SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, "");
71 SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, "");
72
73 #define nb_sosend(so,m,flags,p) (so)->so_proto->pr_usrreqs->pru_sosend( \
74                                     so, NULL, 0, m, 0, flags, p)
75
76 static int  nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
77         u_int8_t *rpcodep, struct proc *p);
78 static int  smb_nbst_disconnect(struct smb_vc *vcp, struct proc *p);
79
80 static int
81 nb_setsockopt_int(struct socket *so, int level, int name, int val)
82 {
83         struct sockopt sopt;
84
85         bzero(&sopt, sizeof(sopt));
86         sopt.sopt_level = level;
87         sopt.sopt_name = name;
88         sopt.sopt_val = &val;
89         sopt.sopt_valsize = sizeof(val);
90         return sosetopt(so, &sopt);
91 }
92
93 static __inline int
94 nb_poll(struct nbpcb *nbp, int events, struct proc *p)
95 {
96         return nbp->nbp_tso->so_proto->pr_usrreqs->pru_sopoll(nbp->nbp_tso,
97             events, NULL, p);
98 }
99
100 static int
101 nbssn_rselect(struct nbpcb *nbp, struct timeval *tv, int events, struct proc *p)
102 {
103         struct timeval atv, rtv, ttv;
104         int s, timo, error;
105
106         if (tv) {
107                 atv = *tv;
108                 if (itimerfix(&atv)) {
109                         error = EINVAL;
110                         goto done;
111                 }
112                 getmicrouptime(&rtv);
113                 timevaladd(&atv, &rtv);
114         }
115         timo = 0;
116 retry:
117         p->p_flag |= P_SELECT;
118         error = nb_poll(nbp, events, p);
119         if (error) {
120                 error = 0;
121                 goto done;
122         }
123         if (tv) {
124                 getmicrouptime(&rtv);
125                 if (timevalcmp(&rtv, &atv, >=))
126                         goto done;
127                 ttv = atv;
128                 timevalsub(&ttv, &rtv);
129                 timo = tvtohz(&ttv);
130         }
131         s = splhigh();
132         if ((p->p_flag & P_SELECT) == 0) {
133                 splx(s);
134                 goto retry;
135         }
136         p->p_flag &= ~P_SELECT;
137         error = tsleep((caddr_t)&selwait, PSOCK, "nbsel", timo);
138         splx(s);
139 done:
140         p->p_flag &= ~P_SELECT;
141         if (error == ERESTART)
142                 return 0;
143         return error;
144 }
145
146 static int
147 nb_intr(struct nbpcb *nbp, struct proc *p)
148 {
149         return 0;
150 }
151
152 static void
153 nb_upcall(struct socket *so, void *arg, int waitflag)
154 {
155         struct nbpcb *nbp = arg;
156
157         if (arg == NULL || nbp->nbp_selectid == NULL)
158                 return;
159         wakeup(nbp->nbp_selectid);
160 }
161
162 static int
163 nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
164 {
165         u_int32_t *p = mtod(m, u_int32_t *);
166
167         *p = htonl((len & 0x1FFFF) | (type << 24));
168         return 0;
169 }
170
171 static int
172 nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
173 {
174         int error;
175         u_char seglen, *cp;
176
177         cp = snb->snb_name;
178         if (*cp == 0)
179                 return EINVAL;
180         NBDEBUG("[%s]\n", cp);
181         for (;;) {
182                 seglen = (*cp) + 1;
183                 error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
184                 if (error)
185                         return error;
186                 if (seglen == 1)
187                         break;
188                 cp += seglen;
189         }
190         return 0;
191 }
192
193 static int
194 nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct proc *p)
195 {
196         struct socket *so;
197         int error, s;
198
199         error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP, p);
200         if (error)
201                 return error;
202         nbp->nbp_tso = so;
203         so->so_upcallarg = (caddr_t)nbp;
204         so->so_upcall = nb_upcall;
205         so->so_rcv.sb_flags |= SB_UPCALL;
206         so->so_rcv.sb_timeo = (5 * hz);
207         so->so_snd.sb_timeo = (5 * hz);
208         error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf);
209         if (error)
210                 goto bad;
211         nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
212         nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
213         so->so_rcv.sb_flags &= ~SB_NOINTR;
214         so->so_snd.sb_flags &= ~SB_NOINTR;
215         error = soconnect(so, (struct sockaddr*)to, p);
216         if (error)
217                 goto bad;
218         s = splnet();
219         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
220                 tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
221                 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
222                         (error = nb_intr(nbp, p)) != 0) {
223                         so->so_state &= ~SS_ISCONNECTING;
224                         splx(s);
225                         goto bad;
226                 }
227         }
228         if (so->so_error) {
229                 error = so->so_error;
230                 so->so_error = 0;
231                 splx(s);
232                 goto bad;
233         }
234         splx(s);
235         return 0;
236 bad:
237         smb_nbst_disconnect(nbp->nbp_vc, p);
238         return error;
239 }
240
241 static int
242 nbssn_rq_request(struct nbpcb *nbp, struct proc *p)
243 {
244         struct mbchain mb, *mbp = &mb;
245         struct mdchain md, *mdp = &md;
246         struct mbuf *m0;
247         struct timeval tv;
248         struct sockaddr_in sin;
249         u_short port;
250         u_int8_t rpcode;
251         int error, rplen;
252
253         error = mb_init(mbp);
254         if (error)
255                 return error;
256         mb_put_uint32le(mbp, 0);
257         nb_put_name(mbp, nbp->nbp_paddr);
258         nb_put_name(mbp, nbp->nbp_laddr);
259         nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
260         error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, p);
261         if (!error) {
262                 nbp->nbp_state = NBST_RQSENT;
263         }
264         mb_detach(mbp);
265         mb_done(mbp);
266         if (error)
267                 return error;
268         TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo);
269         error = nbssn_rselect(nbp, &tv, POLLIN, p);
270         if (error == EWOULDBLOCK) {     /* Timeout */
271                 NBDEBUG("initial request timeout\n");
272                 return ETIMEDOUT;
273         }
274         if (error)                      /* restart or interrupt */
275                 return error;
276         error = nbssn_recv(nbp, &m0, &rplen, &rpcode, p);
277         if (error) {
278                 NBDEBUG("recv() error %d\n", error);
279                 return error;
280         }
281         /*
282          * Process NETBIOS reply
283          */
284         if (m0)
285                 md_initm(mdp, m0);
286         error = 0;
287         do {
288                 if (rpcode == NB_SSN_POSRESP) {
289                         nbp->nbp_state = NBST_SESSION;
290                         nbp->nbp_flags |= NBF_CONNECTED;
291                         break;
292                 }
293                 if (rpcode != NB_SSN_RTGRESP) {
294                         error = ECONNABORTED;
295                         break;
296                 }
297                 if (rplen != 6) {
298                         error = ECONNABORTED;
299                         break;
300                 }
301                 md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
302                 md_get_uint16(mdp, &port);
303                 sin.sin_port = port;
304                 nbp->nbp_state = NBST_RETARGET;
305                 smb_nbst_disconnect(nbp->nbp_vc, p);
306                 error = nb_connect_in(nbp, &sin, p);
307                 if (!error)
308                         error = nbssn_rq_request(nbp, p);
309                 if (error) {
310                         smb_nbst_disconnect(nbp->nbp_vc, p);
311                         break;
312                 }
313         } while(0);
314         if (m0)
315                 md_done(mdp);
316         return error;
317 }
318
319 static int
320 nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
321         u_int8_t *rpcodep, int flags, struct proc *p)
322 {
323         struct socket *so = nbp->nbp_tso;
324         struct uio auio;
325         struct iovec aio;
326         u_int32_t len;
327         int error;
328
329         aio.iov_base = (caddr_t)&len;
330         aio.iov_len = sizeof(len);
331         auio.uio_iov = &aio;
332         auio.uio_iovcnt = 1;
333         auio.uio_segflg = UIO_SYSSPACE;
334         auio.uio_rw = UIO_READ;
335         auio.uio_offset = 0;
336         auio.uio_resid = sizeof(len);
337         auio.uio_procp = p;
338         error = so->so_proto->pr_usrreqs->pru_soreceive
339             (so, (struct sockaddr **)NULL, &auio,
340             (struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
341         if (error)
342                 return error;
343         if (auio.uio_resid > 0) {
344                 SMBSDEBUG("short reply\n");
345                 return EPIPE;
346         }
347         len = ntohl(len);
348         *rpcodep = (len >> 24) & 0xFF;
349         len &= 0x1ffff;
350         if (len > SMB_MAXPKTLEN) {
351                 SMBERROR("packet too long (%d)\n", len);
352                 return EFBIG;
353         }
354         *lenp = len;
355         return 0;
356 }
357
358 static int
359 nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
360         u_int8_t *rpcodep, struct proc *p)
361 {
362         struct socket *so = nbp->nbp_tso;
363         struct uio auio;
364         struct mbuf *m;
365         u_int8_t rpcode;
366         int len;
367         int error, rcvflg;
368
369         if (so == NULL)
370                 return ENOTCONN;
371
372         if (mpp)
373                 *mpp = NULL;
374         for(;;) {
375                 m = NULL;
376                 error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, p);
377                 if (so->so_state &
378                     (SS_ISDISCONNECTING | SS_ISDISCONNECTED | SS_CANTRCVMORE)) {
379                         nbp->nbp_state = NBST_CLOSED;
380                         NBDEBUG("session closed by peer\n");
381                         return ECONNRESET;
382                 }
383                 if (error)
384                         return error;
385                 if (len == 0 && nbp->nbp_state != NBST_SESSION)
386                         break;
387                 if (rpcode == NB_SSN_KEEPALIVE)
388                         continue;
389                 bzero(&auio, sizeof(auio));
390                 auio.uio_resid = len;
391                 auio.uio_procp = p;
392                 do {
393                         rcvflg = MSG_WAITALL;
394                         error = so->so_proto->pr_usrreqs->pru_soreceive
395                             (so, (struct sockaddr **)NULL,
396                             &auio, &m, (struct mbuf **)NULL, &rcvflg);
397                 } while (error == EWOULDBLOCK || error == EINTR ||
398                                  error == ERESTART);
399                 if (error)
400                         break;
401                 if (auio.uio_resid > 0) {
402                         SMBERROR("packet is shorter than expected\n");
403                         error = EPIPE;
404                         break;
405                 }
406                 if (nbp->nbp_state == NBST_SESSION &&
407                     rpcode == NB_SSN_MESSAGE)
408                         break;
409                 NBDEBUG("non-session packet %x\n", rpcode);
410                 if (m)
411                         m_freem(m);
412         }
413         if (error) {
414                 if (m)
415                         m_freem(m);
416                 return error;
417         }
418         if (mpp)
419                 *mpp = m;
420         else
421                 m_freem(m);
422         *lenp = len;
423         *rpcodep = rpcode;
424         return 0;
425 }
426
427 /*
428  * SMB transport interface
429  */
430 static int
431 smb_nbst_create(struct smb_vc *vcp, struct proc *p)
432 {
433         struct nbpcb *nbp;
434
435         MALLOC(nbp, struct nbpcb *, sizeof *nbp, M_NBDATA, M_WAITOK);
436         bzero(nbp, sizeof *nbp);
437         nbp->nbp_timo.tv_sec = 15;      /* XXX: sysctl ? */
438         nbp->nbp_state = NBST_CLOSED;
439         nbp->nbp_vc = vcp;
440         nbp->nbp_sndbuf = smb_tcpsndbuf;
441         nbp->nbp_rcvbuf = smb_tcprcvbuf;
442         vcp->vc_tdata = nbp;
443         return 0;
444 }
445
446 static int
447 smb_nbst_done(struct smb_vc *vcp, struct proc *p)
448 {
449         struct nbpcb *nbp = vcp->vc_tdata;
450
451         if (nbp == NULL)
452                 return ENOTCONN;
453         smb_nbst_disconnect(vcp, p);
454         if (nbp->nbp_laddr)
455                 free(nbp->nbp_laddr, M_SONAME);
456         if (nbp->nbp_paddr)
457                 free(nbp->nbp_paddr, M_SONAME);
458         free(nbp, M_NBDATA);
459         return 0;
460 }
461
462 static int
463 smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct proc *p)
464 {
465         struct nbpcb *nbp = vcp->vc_tdata;
466         struct sockaddr_nb *snb;
467         int error, slen;
468
469         NBDEBUG("\n");
470         error = EINVAL;
471         do {
472                 if (nbp->nbp_flags & NBF_LOCADDR)
473                         break;
474                 /*
475                  * It is possible to create NETBIOS name in the kernel,
476                  * but nothing prevents us to do it in the user space.
477                  */
478                 if (sap == NULL)
479                         break;
480                 slen = sap->sa_len;
481                 if (slen < NB_MINSALEN)
482                         break;
483                 snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
484                 if (snb == NULL) {
485                         error = ENOMEM;
486                         break;
487                 }
488                 nbp->nbp_laddr = snb;
489                 nbp->nbp_flags |= NBF_LOCADDR;
490                 error = 0;
491         } while(0);
492         return error;
493 }
494
495 static int
496 smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct proc *p)
497 {
498         struct nbpcb *nbp = vcp->vc_tdata;
499         struct sockaddr_in sin;
500         struct sockaddr_nb *snb;
501         struct timespec ts1, ts2;
502         int error, slen;
503
504         NBDEBUG("\n");
505         if (nbp->nbp_tso != NULL)
506                 return EISCONN;
507         if (nbp->nbp_laddr == NULL)
508                 return EINVAL;
509         slen = sap->sa_len;
510         if (slen < NB_MINSALEN)
511                 return EINVAL;
512         if (nbp->nbp_paddr) {
513                 free(nbp->nbp_paddr, M_SONAME);
514                 nbp->nbp_paddr = NULL;
515         }
516         snb = (struct sockaddr_nb*)dup_sockaddr(sap, 1);
517         if (snb == NULL)
518                 return ENOMEM;
519         nbp->nbp_paddr = snb;
520         sin = snb->snb_addrin;
521         getnanotime(&ts1);
522         error = nb_connect_in(nbp, &sin, p);
523         if (error)
524                 return error;
525         getnanotime(&ts2);
526         timespecsub(&ts2, &ts1);
527         if (ts2.tv_sec == 0 && ts2.tv_sec == 0)
528                 ts2.tv_sec = 1;
529         nbp->nbp_timo = ts2;
530         timespecadd(&nbp->nbp_timo, &ts2);
531         timespecadd(&nbp->nbp_timo, &ts2);
532         timespecadd(&nbp->nbp_timo, &ts2);      /*  * 4 */
533         error = nbssn_rq_request(nbp, p);
534         if (error)
535                 smb_nbst_disconnect(vcp, p);
536         return error;
537 }
538
539 static int
540 smb_nbst_disconnect(struct smb_vc *vcp, struct proc *p)
541 {
542         struct nbpcb *nbp = vcp->vc_tdata;
543         struct socket *so;
544
545         if (nbp == NULL || nbp->nbp_tso == NULL)
546                 return ENOTCONN;
547         if ((so = nbp->nbp_tso) != NULL) {
548                 nbp->nbp_flags &= ~NBF_CONNECTED;
549                 nbp->nbp_tso = (struct socket *)NULL;
550                 soshutdown(so, 2);
551                 soclose(so);
552         }
553         if (nbp->nbp_state != NBST_RETARGET) {
554                 nbp->nbp_state = NBST_CLOSED;
555         }
556         return 0;
557 }
558
559 static int
560 smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct proc *p)
561 {
562         struct nbpcb *nbp = vcp->vc_tdata;
563         int error;
564
565         if (nbp->nbp_state != NBST_SESSION) {
566                 error = ENOTCONN;
567                 goto abort;
568         }
569         M_PREPEND(m0, 4, M_WAITOK);
570         if (m0 == NULL)
571                 return ENOBUFS;
572         nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
573         error = nb_sosend(nbp->nbp_tso, m0, 0, p);
574         return error;
575 abort:
576         if (m0)
577                 m_freem(m0);
578         return error;
579 }
580
581
582 static int
583 smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct proc *p)
584 {
585         struct nbpcb *nbp = vcp->vc_tdata;
586         u_int8_t rpcode;
587         int error, rplen;
588
589         nbp->nbp_flags |= NBF_RECVLOCK;
590         error = nbssn_recv(nbp, mpp, &rplen, &rpcode, p);
591         nbp->nbp_flags &= ~NBF_RECVLOCK;
592         return error;
593 }
594
595 static void
596 smb_nbst_timo(struct smb_vc *vcp)
597 {
598         return;
599 }
600
601 static void
602 smb_nbst_intr(struct smb_vc *vcp)
603 {
604         struct nbpcb *nbp = vcp->vc_tdata;
605
606         if (nbp == NULL || nbp->nbp_tso == NULL)
607                 return;
608         sorwakeup(nbp->nbp_tso);
609         sowwakeup(nbp->nbp_tso);
610 }
611
612 static int
613 smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
614 {
615         struct nbpcb *nbp = vcp->vc_tdata;
616
617         switch (param) {
618             case SMBTP_SNDSZ:
619                 *(int*)data = nbp->nbp_sndbuf;
620                 break;
621             case SMBTP_RCVSZ:
622                 *(int*)data = nbp->nbp_rcvbuf;
623                 break;
624             case SMBTP_TIMEOUT:
625                 *(struct timespec*)data = nbp->nbp_timo;
626                 break;
627             default:
628                 return EINVAL;
629         }
630         return 0;
631 }
632
633 static int
634 smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
635 {
636         struct nbpcb *nbp = vcp->vc_tdata;
637
638         switch (param) {
639             case SMBTP_SELECTID:
640                 nbp->nbp_selectid = data;
641                 break;
642             default:
643                 return EINVAL;
644         }
645         return 0;
646 }
647
648 /*
649  * Check for fatal errors
650  */
651 static int
652 smb_nbst_fatal(struct smb_vc *vcp, int error)
653 {
654         switch (error) {
655             case ENOTCONN:
656             case ENETRESET:
657             case ECONNABORTED:
658                 return 1;
659         }
660         return 0;
661 }
662
663
664 struct smb_tran_desc smb_tran_nbtcp_desc = {
665         SMBT_NBTCP,
666         smb_nbst_create, smb_nbst_done,
667         smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
668         smb_nbst_send, smb_nbst_recv,
669         smb_nbst_timo, smb_nbst_intr,
670         smb_nbst_getparam, smb_nbst_setparam,
671         smb_nbst_fatal
672 };
673