1c398230bSWarner Losh /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov * All rights reserved.
6681a5bbeSBoris Popov *
7681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov * are met:
10681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov *
16681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov * SUCH DAMAGE.
27681a5bbeSBoris Popov */
28ab0de15bSDavid E. O'Brien
29681a5bbeSBoris Popov #include <sys/param.h>
30960ed29cSSeigo Tanimura #include <sys/condvar.h>
31681a5bbeSBoris Popov #include <sys/kernel.h>
32960ed29cSSeigo Tanimura #include <sys/lock.h>
33681a5bbeSBoris Popov #include <sys/malloc.h>
34681a5bbeSBoris Popov #include <sys/mbuf.h>
35960ed29cSSeigo Tanimura #include <sys/poll.h>
36681a5bbeSBoris Popov #include <sys/proc.h>
37681a5bbeSBoris Popov #include <sys/protosw.h>
38960ed29cSSeigo Tanimura #include <sys/signalvar.h>
39681a5bbeSBoris Popov #include <sys/socket.h>
40681a5bbeSBoris Popov #include <sys/socketvar.h>
41960ed29cSSeigo Tanimura #include <sys/sx.h>
42681a5bbeSBoris Popov #include <sys/sysctl.h>
43960ed29cSSeigo Tanimura #include <sys/systm.h>
44960ed29cSSeigo Tanimura #include <sys/uio.h>
45681a5bbeSBoris Popov
46681a5bbeSBoris Popov #include <net/if.h>
47681a5bbeSBoris Popov #include <net/route.h>
4890e19ee8SDavide Italiano #include <net/vnet.h>
49681a5bbeSBoris Popov
50681a5bbeSBoris Popov #include <netinet/in.h>
51681a5bbeSBoris Popov #include <netinet/tcp.h>
52681a5bbeSBoris Popov
53681a5bbeSBoris Popov #include <sys/mchain.h>
54681a5bbeSBoris Popov
55681a5bbeSBoris Popov #include <netsmb/netbios.h>
56681a5bbeSBoris Popov
57681a5bbeSBoris Popov #include <netsmb/smb.h>
58681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
59681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
60681a5bbeSBoris Popov #include <netsmb/smb_trantcp.h>
61681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
62681a5bbeSBoris Popov
63681a5bbeSBoris Popov #define M_NBDATA M_PCB
64681a5bbeSBoris Popov
653c304004SBoris Popov static int smb_tcpsndbuf = NB_SNDQ - 1;
663c304004SBoris Popov static int smb_tcprcvbuf = NB_RCVQ - 1;
67681a5bbeSBoris Popov
68681a5bbeSBoris Popov SYSCTL_DECL(_net_smb);
69681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, tcpsndbuf, CTLFLAG_RW, &smb_tcpsndbuf, 0, "");
70681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, tcprcvbuf, CTLFLAG_RW, &smb_tcprcvbuf, 0, "");
71681a5bbeSBoris Popov
72b0668f71SRobert Watson #define nb_sosend(so,m,flags,td) sosend(so, NULL, 0, m, 0, flags, td)
73681a5bbeSBoris Popov
74681a5bbeSBoris Popov static int nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
75fce6fbfaSBoris Popov u_int8_t *rpcodep, struct thread *td);
76fce6fbfaSBoris Popov static int smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td);
77681a5bbeSBoris Popov
78681a5bbeSBoris Popov static int
nb_setsockopt_int(struct socket * so,int level,int name,int val)79681a5bbeSBoris Popov nb_setsockopt_int(struct socket *so, int level, int name, int val)
80681a5bbeSBoris Popov {
81681a5bbeSBoris Popov struct sockopt sopt;
8290e19ee8SDavide Italiano int error;
83681a5bbeSBoris Popov
84681a5bbeSBoris Popov bzero(&sopt, sizeof(sopt));
85681a5bbeSBoris Popov sopt.sopt_level = level;
86681a5bbeSBoris Popov sopt.sopt_name = name;
87681a5bbeSBoris Popov sopt.sopt_val = &val;
88681a5bbeSBoris Popov sopt.sopt_valsize = sizeof(val);
8990e19ee8SDavide Italiano CURVNET_SET(so->so_vnet);
9090e19ee8SDavide Italiano error = sosetopt(so, &sopt);
9190e19ee8SDavide Italiano CURVNET_RESTORE();
9290e19ee8SDavide Italiano return error;
93681a5bbeSBoris Popov }
94681a5bbeSBoris Popov
95681a5bbeSBoris Popov static int
nb_intr(struct nbpcb * nbp,struct proc * p)96681a5bbeSBoris Popov nb_intr(struct nbpcb *nbp, struct proc *p)
97681a5bbeSBoris Popov {
98681a5bbeSBoris Popov return 0;
99681a5bbeSBoris Popov }
100681a5bbeSBoris Popov
10174fb0ba7SJohn Baldwin static int
nb_upcall(struct socket * so,void * arg,int waitflag)102681a5bbeSBoris Popov nb_upcall(struct socket *so, void *arg, int waitflag)
103681a5bbeSBoris Popov {
104681a5bbeSBoris Popov struct nbpcb *nbp = arg;
105681a5bbeSBoris Popov
106681a5bbeSBoris Popov if (arg == NULL || nbp->nbp_selectid == NULL)
10774fb0ba7SJohn Baldwin return (SU_OK);
108681a5bbeSBoris Popov wakeup(nbp->nbp_selectid);
10974fb0ba7SJohn Baldwin return (SU_OK);
110681a5bbeSBoris Popov }
111681a5bbeSBoris Popov
112681a5bbeSBoris Popov static int
nb_sethdr(struct mbuf * m,u_int8_t type,u_int32_t len)113681a5bbeSBoris Popov nb_sethdr(struct mbuf *m, u_int8_t type, u_int32_t len)
114681a5bbeSBoris Popov {
115681a5bbeSBoris Popov u_int32_t *p = mtod(m, u_int32_t *);
116681a5bbeSBoris Popov
117681a5bbeSBoris Popov *p = htonl((len & 0x1FFFF) | (type << 24));
118681a5bbeSBoris Popov return 0;
119681a5bbeSBoris Popov }
120681a5bbeSBoris Popov
121681a5bbeSBoris Popov static int
nb_put_name(struct mbchain * mbp,struct sockaddr_nb * snb)122681a5bbeSBoris Popov nb_put_name(struct mbchain *mbp, struct sockaddr_nb *snb)
123681a5bbeSBoris Popov {
124681a5bbeSBoris Popov int error;
125681a5bbeSBoris Popov u_char seglen, *cp;
126681a5bbeSBoris Popov
127681a5bbeSBoris Popov cp = snb->snb_name;
128681a5bbeSBoris Popov if (*cp == 0)
129681a5bbeSBoris Popov return EINVAL;
130681a5bbeSBoris Popov NBDEBUG("[%s]\n", cp);
131681a5bbeSBoris Popov for (;;) {
132681a5bbeSBoris Popov seglen = (*cp) + 1;
133681a5bbeSBoris Popov error = mb_put_mem(mbp, cp, seglen, MB_MSYSTEM);
134681a5bbeSBoris Popov if (error)
135681a5bbeSBoris Popov return error;
136681a5bbeSBoris Popov if (seglen == 1)
137681a5bbeSBoris Popov break;
138681a5bbeSBoris Popov cp += seglen;
139681a5bbeSBoris Popov }
140681a5bbeSBoris Popov return 0;
141681a5bbeSBoris Popov }
142681a5bbeSBoris Popov
143681a5bbeSBoris Popov static int
nb_connect_in(struct nbpcb * nbp,struct sockaddr_in * to,struct thread * td)144fce6fbfaSBoris Popov nb_connect_in(struct nbpcb *nbp, struct sockaddr_in *to, struct thread *td)
145681a5bbeSBoris Popov {
146681a5bbeSBoris Popov struct socket *so;
147681a5bbeSBoris Popov int error, s;
148681a5bbeSBoris Popov
1499c4d63daSRobert Watson error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP,
150a854ed98SJohn Baldwin td->td_ucred, td);
151681a5bbeSBoris Popov if (error)
152681a5bbeSBoris Popov return error;
153681a5bbeSBoris Popov nbp->nbp_tso = so;
1549535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv);
15574fb0ba7SJohn Baldwin soupcall_set(so, SO_RCV, nb_upcall, nbp);
1569535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv);
15709c7b5a4SGleb Smirnoff so->so_rcv.sb_timeo = (5 * SBT_1S);
15809c7b5a4SGleb Smirnoff so->so_snd.sb_timeo = (5 * SBT_1S);
159681a5bbeSBoris Popov error = soreserve(so, nbp->nbp_sndbuf, nbp->nbp_rcvbuf);
160681a5bbeSBoris Popov if (error)
161681a5bbeSBoris Popov goto bad;
162681a5bbeSBoris Popov nb_setsockopt_int(so, SOL_SOCKET, SO_KEEPALIVE, 1);
163681a5bbeSBoris Popov nb_setsockopt_int(so, IPPROTO_TCP, TCP_NODELAY, 1);
164fce6fbfaSBoris Popov error = soconnect(so, (struct sockaddr*)to, td);
165681a5bbeSBoris Popov if (error)
166681a5bbeSBoris Popov goto bad;
167681a5bbeSBoris Popov s = splnet();
168681a5bbeSBoris Popov while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
1694cc20ab1SSeigo Tanimura tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
170681a5bbeSBoris Popov if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
171fce6fbfaSBoris Popov (error = nb_intr(nbp, td->td_proc)) != 0) {
172681a5bbeSBoris Popov so->so_state &= ~SS_ISCONNECTING;
173681a5bbeSBoris Popov splx(s);
174681a5bbeSBoris Popov goto bad;
175681a5bbeSBoris Popov }
176681a5bbeSBoris Popov }
177681a5bbeSBoris Popov if (so->so_error) {
178681a5bbeSBoris Popov error = so->so_error;
179681a5bbeSBoris Popov so->so_error = 0;
180681a5bbeSBoris Popov splx(s);
181681a5bbeSBoris Popov goto bad;
182681a5bbeSBoris Popov }
183681a5bbeSBoris Popov splx(s);
184681a5bbeSBoris Popov return 0;
185681a5bbeSBoris Popov bad:
186fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td);
187681a5bbeSBoris Popov return error;
188681a5bbeSBoris Popov }
189681a5bbeSBoris Popov
190681a5bbeSBoris Popov static int
nbssn_rq_request(struct nbpcb * nbp,struct thread * td)191fce6fbfaSBoris Popov nbssn_rq_request(struct nbpcb *nbp, struct thread *td)
192681a5bbeSBoris Popov {
193afe09751SDavide Italiano struct mbchain *mbp;
194afe09751SDavide Italiano struct mdchain *mdp;
195681a5bbeSBoris Popov struct mbuf *m0;
196681a5bbeSBoris Popov struct timeval tv;
197681a5bbeSBoris Popov struct sockaddr_in sin;
198681a5bbeSBoris Popov u_short port;
199681a5bbeSBoris Popov u_int8_t rpcode;
200681a5bbeSBoris Popov int error, rplen;
201681a5bbeSBoris Popov
202afe09751SDavide Italiano mbp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
203afe09751SDavide Italiano mdp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
204681a5bbeSBoris Popov error = mb_init(mbp);
205afe09751SDavide Italiano if (error) {
206afe09751SDavide Italiano free(mbp, M_NBDATA);
207afe09751SDavide Italiano free(mdp, M_NBDATA);
208681a5bbeSBoris Popov return error;
209afe09751SDavide Italiano }
210681a5bbeSBoris Popov mb_put_uint32le(mbp, 0);
211681a5bbeSBoris Popov nb_put_name(mbp, nbp->nbp_paddr);
212681a5bbeSBoris Popov nb_put_name(mbp, nbp->nbp_laddr);
213681a5bbeSBoris Popov nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
214fce6fbfaSBoris Popov error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td);
215681a5bbeSBoris Popov if (!error) {
216681a5bbeSBoris Popov nbp->nbp_state = NBST_RQSENT;
217681a5bbeSBoris Popov }
218681a5bbeSBoris Popov mb_detach(mbp);
219681a5bbeSBoris Popov mb_done(mbp);
220afe09751SDavide Italiano free(mbp, M_NBDATA);
221afe09751SDavide Italiano if (error) {
222afe09751SDavide Italiano free(mdp, M_NBDATA);
223681a5bbeSBoris Popov return error;
224afe09751SDavide Italiano }
225681a5bbeSBoris Popov TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo);
226ace8398dSJeff Roberson error = selsocket(nbp->nbp_tso, POLLIN, &tv, td);
227681a5bbeSBoris Popov if (error == EWOULDBLOCK) { /* Timeout */
228681a5bbeSBoris Popov NBDEBUG("initial request timeout\n");
229afe09751SDavide Italiano free(mdp, M_NBDATA);
230681a5bbeSBoris Popov return ETIMEDOUT;
231681a5bbeSBoris Popov }
232afe09751SDavide Italiano if (error) { /* restart or interrupt */
233afe09751SDavide Italiano free(mdp, M_NBDATA);
234681a5bbeSBoris Popov return error;
235afe09751SDavide Italiano }
236fce6fbfaSBoris Popov error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td);
237681a5bbeSBoris Popov if (error) {
238681a5bbeSBoris Popov NBDEBUG("recv() error %d\n", error);
239afe09751SDavide Italiano free(mdp, M_NBDATA);
240681a5bbeSBoris Popov return error;
241681a5bbeSBoris Popov }
242681a5bbeSBoris Popov /*
243681a5bbeSBoris Popov * Process NETBIOS reply
244681a5bbeSBoris Popov */
245681a5bbeSBoris Popov if (m0)
246681a5bbeSBoris Popov md_initm(mdp, m0);
247681a5bbeSBoris Popov error = 0;
248681a5bbeSBoris Popov do {
249681a5bbeSBoris Popov if (rpcode == NB_SSN_POSRESP) {
250681a5bbeSBoris Popov nbp->nbp_state = NBST_SESSION;
251681a5bbeSBoris Popov nbp->nbp_flags |= NBF_CONNECTED;
252681a5bbeSBoris Popov break;
253681a5bbeSBoris Popov }
254681a5bbeSBoris Popov if (rpcode != NB_SSN_RTGRESP) {
255681a5bbeSBoris Popov error = ECONNABORTED;
256681a5bbeSBoris Popov break;
257681a5bbeSBoris Popov }
258681a5bbeSBoris Popov if (rplen != 6) {
259681a5bbeSBoris Popov error = ECONNABORTED;
260681a5bbeSBoris Popov break;
261681a5bbeSBoris Popov }
262681a5bbeSBoris Popov md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
263681a5bbeSBoris Popov md_get_uint16(mdp, &port);
264681a5bbeSBoris Popov sin.sin_port = port;
265681a5bbeSBoris Popov nbp->nbp_state = NBST_RETARGET;
266fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td);
267fce6fbfaSBoris Popov error = nb_connect_in(nbp, &sin, td);
268681a5bbeSBoris Popov if (!error)
269fce6fbfaSBoris Popov error = nbssn_rq_request(nbp, td);
270681a5bbeSBoris Popov if (error) {
271fce6fbfaSBoris Popov smb_nbst_disconnect(nbp->nbp_vc, td);
272681a5bbeSBoris Popov break;
273681a5bbeSBoris Popov }
274681a5bbeSBoris Popov } while(0);
275681a5bbeSBoris Popov if (m0)
276681a5bbeSBoris Popov md_done(mdp);
277afe09751SDavide Italiano free(mdp, M_NBDATA);
278681a5bbeSBoris Popov return error;
279681a5bbeSBoris Popov }
280681a5bbeSBoris Popov
281681a5bbeSBoris Popov static int
nbssn_recvhdr(struct nbpcb * nbp,int * lenp,u_int8_t * rpcodep,int flags,struct thread * td)282681a5bbeSBoris Popov nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
283fce6fbfaSBoris Popov u_int8_t *rpcodep, int flags, struct thread *td)
284681a5bbeSBoris Popov {
285681a5bbeSBoris Popov struct socket *so = nbp->nbp_tso;
286681a5bbeSBoris Popov struct uio auio;
287681a5bbeSBoris Popov struct iovec aio;
288681a5bbeSBoris Popov u_int32_t len;
289681a5bbeSBoris Popov int error;
290681a5bbeSBoris Popov
291681a5bbeSBoris Popov aio.iov_base = (caddr_t)&len;
292681a5bbeSBoris Popov aio.iov_len = sizeof(len);
293681a5bbeSBoris Popov auio.uio_iov = &aio;
294681a5bbeSBoris Popov auio.uio_iovcnt = 1;
295681a5bbeSBoris Popov auio.uio_segflg = UIO_SYSSPACE;
296681a5bbeSBoris Popov auio.uio_rw = UIO_READ;
297681a5bbeSBoris Popov auio.uio_offset = 0;
298681a5bbeSBoris Popov auio.uio_resid = sizeof(len);
299fce6fbfaSBoris Popov auio.uio_td = td;
30090e19ee8SDavide Italiano CURVNET_SET(so->so_vnet);
301b0668f71SRobert Watson error = soreceive(so, (struct sockaddr **)NULL, &auio,
302681a5bbeSBoris Popov (struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
30390e19ee8SDavide Italiano CURVNET_RESTORE();
304681a5bbeSBoris Popov if (error)
305681a5bbeSBoris Popov return error;
306681a5bbeSBoris Popov if (auio.uio_resid > 0) {
307681a5bbeSBoris Popov SMBSDEBUG("short reply\n");
308681a5bbeSBoris Popov return EPIPE;
309681a5bbeSBoris Popov }
310681a5bbeSBoris Popov len = ntohl(len);
311681a5bbeSBoris Popov *rpcodep = (len >> 24) & 0xFF;
312681a5bbeSBoris Popov len &= 0x1ffff;
313681a5bbeSBoris Popov if (len > SMB_MAXPKTLEN) {
314681a5bbeSBoris Popov SMBERROR("packet too long (%d)\n", len);
315681a5bbeSBoris Popov return EFBIG;
316681a5bbeSBoris Popov }
317681a5bbeSBoris Popov *lenp = len;
318681a5bbeSBoris Popov return 0;
319681a5bbeSBoris Popov }
320681a5bbeSBoris Popov
321681a5bbeSBoris Popov static int
nbssn_recv(struct nbpcb * nbp,struct mbuf ** mpp,int * lenp,u_int8_t * rpcodep,struct thread * td)322681a5bbeSBoris Popov nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
323fce6fbfaSBoris Popov u_int8_t *rpcodep, struct thread *td)
324681a5bbeSBoris Popov {
325681a5bbeSBoris Popov struct socket *so = nbp->nbp_tso;
326681a5bbeSBoris Popov struct uio auio;
3273c304004SBoris Popov struct mbuf *m, *tm, *im;
328681a5bbeSBoris Popov u_int8_t rpcode;
3293c304004SBoris Popov int len, resid;
330681a5bbeSBoris Popov int error, rcvflg;
331681a5bbeSBoris Popov
332681a5bbeSBoris Popov if (so == NULL)
333681a5bbeSBoris Popov return ENOTCONN;
334681a5bbeSBoris Popov
335681a5bbeSBoris Popov if (mpp)
336681a5bbeSBoris Popov *mpp = NULL;
337681a5bbeSBoris Popov m = NULL;
3383c304004SBoris Popov for(;;) {
3393c304004SBoris Popov /*
3403c304004SBoris Popov * Poll for a response header.
3413c304004SBoris Popov * If we don't have one waiting, return.
3423c304004SBoris Popov */
3432d494bc6SMatt Jacob len = 0;
3442d494bc6SMatt Jacob rpcode = 0;
345fce6fbfaSBoris Popov error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, td);
346c0b99ffaSRobert Watson if ((so->so_state & (SS_ISDISCONNECTING | SS_ISDISCONNECTED)) ||
347c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
348681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED;
349681a5bbeSBoris Popov NBDEBUG("session closed by peer\n");
350681a5bbeSBoris Popov return ECONNRESET;
351681a5bbeSBoris Popov }
352681a5bbeSBoris Popov if (error)
353681a5bbeSBoris Popov return error;
354681a5bbeSBoris Popov if (len == 0 && nbp->nbp_state != NBST_SESSION)
355681a5bbeSBoris Popov break;
3563c304004SBoris Popov /* no data, try again */
357681a5bbeSBoris Popov if (rpcode == NB_SSN_KEEPALIVE)
358681a5bbeSBoris Popov continue;
3593c304004SBoris Popov
3603c304004SBoris Popov /*
3613c304004SBoris Popov * Loop, blocking, for data following the response header.
3623c304004SBoris Popov *
3633c304004SBoris Popov * Note that we can't simply block here with MSG_WAITALL for the
3643c304004SBoris Popov * entire response size, as it may be larger than the TCP
3653c304004SBoris Popov * slow-start window that the sender employs. This will result
3663c304004SBoris Popov * in the sender stalling until the delayed ACK is sent, then
3673c304004SBoris Popov * resuming slow-start, resulting in very poor performance.
3683c304004SBoris Popov *
3693c304004SBoris Popov * Instead, we never request more than NB_SORECEIVE_CHUNK
3703c304004SBoris Popov * bytes at a time, resulting in an ack being pushed by
3713c304004SBoris Popov * the TCP code at the completion of each call.
3723c304004SBoris Popov */
3733c304004SBoris Popov resid = len;
3743c304004SBoris Popov while (resid > 0) {
3753c304004SBoris Popov tm = NULL;
3763c304004SBoris Popov rcvflg = MSG_WAITALL;
377681a5bbeSBoris Popov bzero(&auio, sizeof(auio));
3783c304004SBoris Popov auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
379fce6fbfaSBoris Popov auio.uio_td = td;
3803c304004SBoris Popov resid -= auio.uio_resid;
3813c304004SBoris Popov /*
3823c304004SBoris Popov * Spin until we have collected everything in
3833c304004SBoris Popov * this chunk.
3843c304004SBoris Popov */
385681a5bbeSBoris Popov do {
386681a5bbeSBoris Popov rcvflg = MSG_WAITALL;
38790e19ee8SDavide Italiano CURVNET_SET(so->so_vnet);
388b0668f71SRobert Watson error = soreceive(so, (struct sockaddr **)NULL,
389bd32b702STim J. Robbins &auio, &tm, (struct mbuf **)NULL, &rcvflg);
39090e19ee8SDavide Italiano CURVNET_RESTORE();
391681a5bbeSBoris Popov } while (error == EWOULDBLOCK || error == EINTR ||
392681a5bbeSBoris Popov error == ERESTART);
393681a5bbeSBoris Popov if (error)
3943c304004SBoris Popov goto out;
3953c304004SBoris Popov /* short return guarantees unhappiness */
396681a5bbeSBoris Popov if (auio.uio_resid > 0) {
397681a5bbeSBoris Popov SMBERROR("packet is shorter than expected\n");
398681a5bbeSBoris Popov error = EPIPE;
3993c304004SBoris Popov goto out;
400681a5bbeSBoris Popov }
4013c304004SBoris Popov /* append received chunk to previous chunk(s) */
4023c304004SBoris Popov if (m == NULL) {
4033c304004SBoris Popov m = tm;
4043c304004SBoris Popov } else {
4053c304004SBoris Popov /*
4063c304004SBoris Popov * Just glue the new chain on the end.
4073c304004SBoris Popov * Consumer will pullup as required.
4083c304004SBoris Popov */
4093c304004SBoris Popov for (im = m; im->m_next != NULL; im = im->m_next)
4103c304004SBoris Popov ;
4113c304004SBoris Popov im->m_next = tm;
4123c304004SBoris Popov }
4133c304004SBoris Popov }
4143c304004SBoris Popov /* got a session/message packet? */
415681a5bbeSBoris Popov if (nbp->nbp_state == NBST_SESSION &&
416681a5bbeSBoris Popov rpcode == NB_SSN_MESSAGE)
417681a5bbeSBoris Popov break;
4183c304004SBoris Popov /* drop packet and try for another */
419681a5bbeSBoris Popov NBDEBUG("non-session packet %x\n", rpcode);
4203c304004SBoris Popov if (m) {
421681a5bbeSBoris Popov m_freem(m);
4223c304004SBoris Popov m = NULL;
423681a5bbeSBoris Popov }
4243c304004SBoris Popov }
4253c304004SBoris Popov
4263c304004SBoris Popov out:
427681a5bbeSBoris Popov if (error) {
428681a5bbeSBoris Popov if (m)
429681a5bbeSBoris Popov m_freem(m);
430681a5bbeSBoris Popov return error;
431681a5bbeSBoris Popov }
432681a5bbeSBoris Popov if (mpp)
433681a5bbeSBoris Popov *mpp = m;
434681a5bbeSBoris Popov else
435681a5bbeSBoris Popov m_freem(m);
436681a5bbeSBoris Popov *lenp = len;
437681a5bbeSBoris Popov *rpcodep = rpcode;
438681a5bbeSBoris Popov return 0;
439681a5bbeSBoris Popov }
440681a5bbeSBoris Popov
441681a5bbeSBoris Popov /*
442681a5bbeSBoris Popov * SMB transport interface
443681a5bbeSBoris Popov */
444681a5bbeSBoris Popov static int
smb_nbst_create(struct smb_vc * vcp,struct thread * td)445fce6fbfaSBoris Popov smb_nbst_create(struct smb_vc *vcp, struct thread *td)
446681a5bbeSBoris Popov {
447681a5bbeSBoris Popov struct nbpcb *nbp;
448681a5bbeSBoris Popov
4491ede983cSDag-Erling Smørgrav nbp = malloc(sizeof *nbp, M_NBDATA, M_WAITOK);
450681a5bbeSBoris Popov bzero(nbp, sizeof *nbp);
451681a5bbeSBoris Popov nbp->nbp_timo.tv_sec = 15; /* XXX: sysctl ? */
452681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED;
453681a5bbeSBoris Popov nbp->nbp_vc = vcp;
454681a5bbeSBoris Popov nbp->nbp_sndbuf = smb_tcpsndbuf;
455681a5bbeSBoris Popov nbp->nbp_rcvbuf = smb_tcprcvbuf;
456681a5bbeSBoris Popov vcp->vc_tdata = nbp;
457681a5bbeSBoris Popov return 0;
458681a5bbeSBoris Popov }
459681a5bbeSBoris Popov
460681a5bbeSBoris Popov static int
smb_nbst_done(struct smb_vc * vcp,struct thread * td)461fce6fbfaSBoris Popov smb_nbst_done(struct smb_vc *vcp, struct thread *td)
462681a5bbeSBoris Popov {
463681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
464681a5bbeSBoris Popov
465681a5bbeSBoris Popov if (nbp == NULL)
466681a5bbeSBoris Popov return ENOTCONN;
467fce6fbfaSBoris Popov smb_nbst_disconnect(vcp, td);
468681a5bbeSBoris Popov if (nbp->nbp_laddr)
469681a5bbeSBoris Popov free(nbp->nbp_laddr, M_SONAME);
470681a5bbeSBoris Popov if (nbp->nbp_paddr)
471681a5bbeSBoris Popov free(nbp->nbp_paddr, M_SONAME);
472681a5bbeSBoris Popov free(nbp, M_NBDATA);
473681a5bbeSBoris Popov return 0;
474681a5bbeSBoris Popov }
475681a5bbeSBoris Popov
476681a5bbeSBoris Popov static int
smb_nbst_bind(struct smb_vc * vcp,struct sockaddr * sap,struct thread * td)477fce6fbfaSBoris Popov smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
478681a5bbeSBoris Popov {
479681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
480681a5bbeSBoris Popov struct sockaddr_nb *snb;
481681a5bbeSBoris Popov int error, slen;
482681a5bbeSBoris Popov
483681a5bbeSBoris Popov NBDEBUG("\n");
484681a5bbeSBoris Popov error = EINVAL;
485681a5bbeSBoris Popov do {
486681a5bbeSBoris Popov if (nbp->nbp_flags & NBF_LOCADDR)
487681a5bbeSBoris Popov break;
488681a5bbeSBoris Popov /*
489681a5bbeSBoris Popov * It is possible to create NETBIOS name in the kernel,
490681a5bbeSBoris Popov * but nothing prevents us to do it in the user space.
491681a5bbeSBoris Popov */
492681a5bbeSBoris Popov if (sap == NULL)
493681a5bbeSBoris Popov break;
494681a5bbeSBoris Popov slen = sap->sa_len;
495681a5bbeSBoris Popov if (slen < NB_MINSALEN)
496681a5bbeSBoris Popov break;
497746e5bf0SRobert Watson snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
498681a5bbeSBoris Popov if (snb == NULL) {
499681a5bbeSBoris Popov error = ENOMEM;
500681a5bbeSBoris Popov break;
501681a5bbeSBoris Popov }
502681a5bbeSBoris Popov nbp->nbp_laddr = snb;
503681a5bbeSBoris Popov nbp->nbp_flags |= NBF_LOCADDR;
504681a5bbeSBoris Popov error = 0;
505681a5bbeSBoris Popov } while(0);
506681a5bbeSBoris Popov return error;
507681a5bbeSBoris Popov }
508681a5bbeSBoris Popov
509681a5bbeSBoris Popov static int
smb_nbst_connect(struct smb_vc * vcp,struct sockaddr * sap,struct thread * td)510fce6fbfaSBoris Popov smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
511681a5bbeSBoris Popov {
512681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
513681a5bbeSBoris Popov struct sockaddr_in sin;
514681a5bbeSBoris Popov struct sockaddr_nb *snb;
515681a5bbeSBoris Popov struct timespec ts1, ts2;
516681a5bbeSBoris Popov int error, slen;
517681a5bbeSBoris Popov
518681a5bbeSBoris Popov NBDEBUG("\n");
519681a5bbeSBoris Popov if (nbp->nbp_tso != NULL)
520681a5bbeSBoris Popov return EISCONN;
521681a5bbeSBoris Popov if (nbp->nbp_laddr == NULL)
522681a5bbeSBoris Popov return EINVAL;
523681a5bbeSBoris Popov slen = sap->sa_len;
524681a5bbeSBoris Popov if (slen < NB_MINSALEN)
525681a5bbeSBoris Popov return EINVAL;
526681a5bbeSBoris Popov if (nbp->nbp_paddr) {
527681a5bbeSBoris Popov free(nbp->nbp_paddr, M_SONAME);
528681a5bbeSBoris Popov nbp->nbp_paddr = NULL;
529681a5bbeSBoris Popov }
530746e5bf0SRobert Watson snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
531681a5bbeSBoris Popov if (snb == NULL)
532681a5bbeSBoris Popov return ENOMEM;
533681a5bbeSBoris Popov nbp->nbp_paddr = snb;
534681a5bbeSBoris Popov sin = snb->snb_addrin;
535681a5bbeSBoris Popov getnanotime(&ts1);
536fce6fbfaSBoris Popov error = nb_connect_in(nbp, &sin, td);
537681a5bbeSBoris Popov if (error)
538681a5bbeSBoris Popov return error;
539681a5bbeSBoris Popov getnanotime(&ts2);
5406040822cSAlan Somers timespecsub(&ts2, &ts1, &ts2);
54108bd45d3SChristian Brueffer if (ts2.tv_sec == 0) {
542681a5bbeSBoris Popov ts2.tv_sec = 1;
54308bd45d3SChristian Brueffer ts2.tv_nsec = 0;
54408bd45d3SChristian Brueffer }
5456040822cSAlan Somers timespecadd(&ts2, &ts2, &nbp->nbp_timo);
5466040822cSAlan Somers timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
5476040822cSAlan Somers timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo); /* * 4 */
548fce6fbfaSBoris Popov error = nbssn_rq_request(nbp, td);
549681a5bbeSBoris Popov if (error)
550fce6fbfaSBoris Popov smb_nbst_disconnect(vcp, td);
551681a5bbeSBoris Popov return error;
552681a5bbeSBoris Popov }
553681a5bbeSBoris Popov
554681a5bbeSBoris Popov static int
smb_nbst_disconnect(struct smb_vc * vcp,struct thread * td)555fce6fbfaSBoris Popov smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td)
556681a5bbeSBoris Popov {
557681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
558681a5bbeSBoris Popov struct socket *so;
559681a5bbeSBoris Popov
560681a5bbeSBoris Popov if (nbp == NULL || nbp->nbp_tso == NULL)
561681a5bbeSBoris Popov return ENOTCONN;
562681a5bbeSBoris Popov if ((so = nbp->nbp_tso) != NULL) {
563681a5bbeSBoris Popov nbp->nbp_flags &= ~NBF_CONNECTED;
564681a5bbeSBoris Popov nbp->nbp_tso = (struct socket *)NULL;
565681a5bbeSBoris Popov soshutdown(so, 2);
566681a5bbeSBoris Popov soclose(so);
567681a5bbeSBoris Popov }
568681a5bbeSBoris Popov if (nbp->nbp_state != NBST_RETARGET) {
569681a5bbeSBoris Popov nbp->nbp_state = NBST_CLOSED;
570681a5bbeSBoris Popov }
571681a5bbeSBoris Popov return 0;
572681a5bbeSBoris Popov }
573681a5bbeSBoris Popov
574681a5bbeSBoris Popov static int
smb_nbst_send(struct smb_vc * vcp,struct mbuf * m0,struct thread * td)575fce6fbfaSBoris Popov smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct thread *td)
576681a5bbeSBoris Popov {
577681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
578681a5bbeSBoris Popov int error;
579681a5bbeSBoris Popov
580681a5bbeSBoris Popov if (nbp->nbp_state != NBST_SESSION) {
581681a5bbeSBoris Popov error = ENOTCONN;
582681a5bbeSBoris Popov goto abort;
583681a5bbeSBoris Popov }
584eb1b1807SGleb Smirnoff M_PREPEND(m0, 4, M_WAITOK);
585681a5bbeSBoris Popov nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
586fce6fbfaSBoris Popov error = nb_sosend(nbp->nbp_tso, m0, 0, td);
587681a5bbeSBoris Popov return error;
588681a5bbeSBoris Popov abort:
589681a5bbeSBoris Popov if (m0)
590681a5bbeSBoris Popov m_freem(m0);
591681a5bbeSBoris Popov return error;
592681a5bbeSBoris Popov }
593681a5bbeSBoris Popov
594681a5bbeSBoris Popov static int
smb_nbst_recv(struct smb_vc * vcp,struct mbuf ** mpp,struct thread * td)595fce6fbfaSBoris Popov smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td)
596681a5bbeSBoris Popov {
597681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
598681a5bbeSBoris Popov u_int8_t rpcode;
599681a5bbeSBoris Popov int error, rplen;
600681a5bbeSBoris Popov
601681a5bbeSBoris Popov nbp->nbp_flags |= NBF_RECVLOCK;
602fce6fbfaSBoris Popov error = nbssn_recv(nbp, mpp, &rplen, &rpcode, td);
603681a5bbeSBoris Popov nbp->nbp_flags &= ~NBF_RECVLOCK;
604681a5bbeSBoris Popov return error;
605681a5bbeSBoris Popov }
606681a5bbeSBoris Popov
607681a5bbeSBoris Popov static void
smb_nbst_timo(struct smb_vc * vcp)608681a5bbeSBoris Popov smb_nbst_timo(struct smb_vc *vcp)
609681a5bbeSBoris Popov {
610681a5bbeSBoris Popov return;
611681a5bbeSBoris Popov }
612681a5bbeSBoris Popov
613681a5bbeSBoris Popov static void
smb_nbst_intr(struct smb_vc * vcp)614681a5bbeSBoris Popov smb_nbst_intr(struct smb_vc *vcp)
615681a5bbeSBoris Popov {
616681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
617681a5bbeSBoris Popov
618681a5bbeSBoris Popov if (nbp == NULL || nbp->nbp_tso == NULL)
619681a5bbeSBoris Popov return;
620681a5bbeSBoris Popov sorwakeup(nbp->nbp_tso);
621681a5bbeSBoris Popov sowwakeup(nbp->nbp_tso);
622681a5bbeSBoris Popov }
623681a5bbeSBoris Popov
624681a5bbeSBoris Popov static int
smb_nbst_getparam(struct smb_vc * vcp,int param,void * data)625681a5bbeSBoris Popov smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
626681a5bbeSBoris Popov {
627681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
628681a5bbeSBoris Popov
629681a5bbeSBoris Popov switch (param) {
630681a5bbeSBoris Popov case SMBTP_SNDSZ:
631681a5bbeSBoris Popov *(int*)data = nbp->nbp_sndbuf;
632681a5bbeSBoris Popov break;
633681a5bbeSBoris Popov case SMBTP_RCVSZ:
634681a5bbeSBoris Popov *(int*)data = nbp->nbp_rcvbuf;
635681a5bbeSBoris Popov break;
636681a5bbeSBoris Popov case SMBTP_TIMEOUT:
637681a5bbeSBoris Popov *(struct timespec*)data = nbp->nbp_timo;
638681a5bbeSBoris Popov break;
639681a5bbeSBoris Popov default:
640681a5bbeSBoris Popov return EINVAL;
641681a5bbeSBoris Popov }
642681a5bbeSBoris Popov return 0;
643681a5bbeSBoris Popov }
644681a5bbeSBoris Popov
645681a5bbeSBoris Popov static int
smb_nbst_setparam(struct smb_vc * vcp,int param,void * data)646681a5bbeSBoris Popov smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
647681a5bbeSBoris Popov {
648681a5bbeSBoris Popov struct nbpcb *nbp = vcp->vc_tdata;
649681a5bbeSBoris Popov
650681a5bbeSBoris Popov switch (param) {
651681a5bbeSBoris Popov case SMBTP_SELECTID:
652681a5bbeSBoris Popov nbp->nbp_selectid = data;
653681a5bbeSBoris Popov break;
654681a5bbeSBoris Popov default:
655681a5bbeSBoris Popov return EINVAL;
656681a5bbeSBoris Popov }
657681a5bbeSBoris Popov return 0;
658681a5bbeSBoris Popov }
659681a5bbeSBoris Popov
660681a5bbeSBoris Popov /*
661681a5bbeSBoris Popov * Check for fatal errors
662681a5bbeSBoris Popov */
663681a5bbeSBoris Popov static int
smb_nbst_fatal(struct smb_vc * vcp,int error)664681a5bbeSBoris Popov smb_nbst_fatal(struct smb_vc *vcp, int error)
665681a5bbeSBoris Popov {
666681a5bbeSBoris Popov switch (error) {
667681a5bbeSBoris Popov case ENOTCONN:
668681a5bbeSBoris Popov case ENETRESET:
669681a5bbeSBoris Popov case ECONNABORTED:
670681a5bbeSBoris Popov return 1;
671681a5bbeSBoris Popov }
672681a5bbeSBoris Popov return 0;
673681a5bbeSBoris Popov }
674681a5bbeSBoris Popov
675681a5bbeSBoris Popov struct smb_tran_desc smb_tran_nbtcp_desc = {
676681a5bbeSBoris Popov SMBT_NBTCP,
677681a5bbeSBoris Popov smb_nbst_create, smb_nbst_done,
678681a5bbeSBoris Popov smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
679681a5bbeSBoris Popov smb_nbst_send, smb_nbst_recv,
680681a5bbeSBoris Popov smb_nbst_timo, smb_nbst_intr,
681681a5bbeSBoris Popov smb_nbst_getparam, smb_nbst_setparam,
682681a5bbeSBoris Popov smb_nbst_fatal
683681a5bbeSBoris Popov };
684