xref: /freebsd-src/sys/netsmb/smb_trantcp.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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);
1649535efc0SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
165681a5bbeSBoris Popov 	so->so_rcv.sb_flags &= ~SB_NOINTR;
1669535efc0SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
1679535efc0SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
168681a5bbeSBoris Popov 	so->so_snd.sb_flags &= ~SB_NOINTR;
1699535efc0SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
170fce6fbfaSBoris Popov 	error = soconnect(so, (struct sockaddr*)to, td);
171681a5bbeSBoris Popov 	if (error)
172681a5bbeSBoris Popov 		goto bad;
173681a5bbeSBoris Popov 	s = splnet();
174681a5bbeSBoris Popov 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
1754cc20ab1SSeigo Tanimura 		tsleep(&so->so_timeo, PSOCK, "nbcon", 2 * hz);
176681a5bbeSBoris Popov 		if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0 &&
177fce6fbfaSBoris Popov 			(error = nb_intr(nbp, td->td_proc)) != 0) {
178681a5bbeSBoris Popov 			so->so_state &= ~SS_ISCONNECTING;
179681a5bbeSBoris Popov 			splx(s);
180681a5bbeSBoris Popov 			goto bad;
181681a5bbeSBoris Popov 		}
182681a5bbeSBoris Popov 	}
183681a5bbeSBoris Popov 	if (so->so_error) {
184681a5bbeSBoris Popov 		error = so->so_error;
185681a5bbeSBoris Popov 		so->so_error = 0;
186681a5bbeSBoris Popov 		splx(s);
187681a5bbeSBoris Popov 		goto bad;
188681a5bbeSBoris Popov 	}
189681a5bbeSBoris Popov 	splx(s);
190681a5bbeSBoris Popov 	return 0;
191681a5bbeSBoris Popov bad:
192fce6fbfaSBoris Popov 	smb_nbst_disconnect(nbp->nbp_vc, td);
193681a5bbeSBoris Popov 	return error;
194681a5bbeSBoris Popov }
195681a5bbeSBoris Popov 
196681a5bbeSBoris Popov static int
nbssn_rq_request(struct nbpcb * nbp,struct thread * td)197fce6fbfaSBoris Popov nbssn_rq_request(struct nbpcb *nbp, struct thread *td)
198681a5bbeSBoris Popov {
199afe09751SDavide Italiano 	struct mbchain *mbp;
200afe09751SDavide Italiano 	struct mdchain *mdp;
201681a5bbeSBoris Popov 	struct mbuf *m0;
202681a5bbeSBoris Popov 	struct timeval tv;
203681a5bbeSBoris Popov 	struct sockaddr_in sin;
204681a5bbeSBoris Popov 	u_short port;
205681a5bbeSBoris Popov 	u_int8_t rpcode;
206681a5bbeSBoris Popov 	int error, rplen;
207681a5bbeSBoris Popov 
208afe09751SDavide Italiano 	mbp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
209afe09751SDavide Italiano 	mdp = malloc(sizeof(struct mbchain), M_NBDATA, M_WAITOK);
210681a5bbeSBoris Popov 	error = mb_init(mbp);
211afe09751SDavide Italiano 	if (error) {
212afe09751SDavide Italiano 		free(mbp, M_NBDATA);
213afe09751SDavide Italiano 		free(mdp, M_NBDATA);
214681a5bbeSBoris Popov 		return error;
215afe09751SDavide Italiano 	}
216681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);
217681a5bbeSBoris Popov 	nb_put_name(mbp, nbp->nbp_paddr);
218681a5bbeSBoris Popov 	nb_put_name(mbp, nbp->nbp_laddr);
219681a5bbeSBoris Popov 	nb_sethdr(mbp->mb_top, NB_SSN_REQUEST, mb_fixhdr(mbp) - 4);
220fce6fbfaSBoris Popov 	error = nb_sosend(nbp->nbp_tso, mbp->mb_top, 0, td);
221681a5bbeSBoris Popov 	if (!error) {
222681a5bbeSBoris Popov 		nbp->nbp_state = NBST_RQSENT;
223681a5bbeSBoris Popov 	}
224681a5bbeSBoris Popov 	mb_detach(mbp);
225681a5bbeSBoris Popov 	mb_done(mbp);
226afe09751SDavide Italiano 	free(mbp, M_NBDATA);
227afe09751SDavide Italiano 	if (error) {
228afe09751SDavide Italiano 		free(mdp, M_NBDATA);
229681a5bbeSBoris Popov 		return error;
230afe09751SDavide Italiano 	}
231681a5bbeSBoris Popov 	TIMESPEC_TO_TIMEVAL(&tv, &nbp->nbp_timo);
232ace8398dSJeff Roberson 	error = selsocket(nbp->nbp_tso, POLLIN, &tv, td);
233681a5bbeSBoris Popov 	if (error == EWOULDBLOCK) {	/* Timeout */
234681a5bbeSBoris Popov 		NBDEBUG("initial request timeout\n");
235afe09751SDavide Italiano 		free(mdp, M_NBDATA);
236681a5bbeSBoris Popov 		return ETIMEDOUT;
237681a5bbeSBoris Popov 	}
238afe09751SDavide Italiano 	if (error) {			/* restart or interrupt */
239afe09751SDavide Italiano 		free(mdp, M_NBDATA);
240681a5bbeSBoris Popov 		return error;
241afe09751SDavide Italiano 	}
242fce6fbfaSBoris Popov 	error = nbssn_recv(nbp, &m0, &rplen, &rpcode, td);
243681a5bbeSBoris Popov 	if (error) {
244681a5bbeSBoris Popov 		NBDEBUG("recv() error %d\n", error);
245afe09751SDavide Italiano 		free(mdp, M_NBDATA);
246681a5bbeSBoris Popov 		return error;
247681a5bbeSBoris Popov 	}
248681a5bbeSBoris Popov 	/*
249681a5bbeSBoris Popov 	 * Process NETBIOS reply
250681a5bbeSBoris Popov 	 */
251681a5bbeSBoris Popov 	if (m0)
252681a5bbeSBoris Popov 		md_initm(mdp, m0);
253681a5bbeSBoris Popov 	error = 0;
254681a5bbeSBoris Popov 	do {
255681a5bbeSBoris Popov 		if (rpcode == NB_SSN_POSRESP) {
256681a5bbeSBoris Popov 			nbp->nbp_state = NBST_SESSION;
257681a5bbeSBoris Popov 			nbp->nbp_flags |= NBF_CONNECTED;
258681a5bbeSBoris Popov 			break;
259681a5bbeSBoris Popov 		}
260681a5bbeSBoris Popov 		if (rpcode != NB_SSN_RTGRESP) {
261681a5bbeSBoris Popov 			error = ECONNABORTED;
262681a5bbeSBoris Popov 			break;
263681a5bbeSBoris Popov 		}
264681a5bbeSBoris Popov 		if (rplen != 6) {
265681a5bbeSBoris Popov 			error = ECONNABORTED;
266681a5bbeSBoris Popov 			break;
267681a5bbeSBoris Popov 		}
268681a5bbeSBoris Popov 		md_get_mem(mdp, (caddr_t)&sin.sin_addr, 4, MB_MSYSTEM);
269681a5bbeSBoris Popov 		md_get_uint16(mdp, &port);
270681a5bbeSBoris Popov 		sin.sin_port = port;
271681a5bbeSBoris Popov 		nbp->nbp_state = NBST_RETARGET;
272fce6fbfaSBoris Popov 		smb_nbst_disconnect(nbp->nbp_vc, td);
273fce6fbfaSBoris Popov 		error = nb_connect_in(nbp, &sin, td);
274681a5bbeSBoris Popov 		if (!error)
275fce6fbfaSBoris Popov 			error = nbssn_rq_request(nbp, td);
276681a5bbeSBoris Popov 		if (error) {
277fce6fbfaSBoris Popov 			smb_nbst_disconnect(nbp->nbp_vc, td);
278681a5bbeSBoris Popov 			break;
279681a5bbeSBoris Popov 		}
280681a5bbeSBoris Popov 	} while(0);
281681a5bbeSBoris Popov 	if (m0)
282681a5bbeSBoris Popov 		md_done(mdp);
283afe09751SDavide Italiano 	free(mdp, M_NBDATA);
284681a5bbeSBoris Popov 	return error;
285681a5bbeSBoris Popov }
286681a5bbeSBoris Popov 
287681a5bbeSBoris Popov static int
nbssn_recvhdr(struct nbpcb * nbp,int * lenp,u_int8_t * rpcodep,int flags,struct thread * td)288681a5bbeSBoris Popov nbssn_recvhdr(struct nbpcb *nbp, int *lenp,
289fce6fbfaSBoris Popov 	u_int8_t *rpcodep, int flags, struct thread *td)
290681a5bbeSBoris Popov {
291681a5bbeSBoris Popov 	struct socket *so = nbp->nbp_tso;
292681a5bbeSBoris Popov 	struct uio auio;
293681a5bbeSBoris Popov 	struct iovec aio;
294681a5bbeSBoris Popov 	u_int32_t len;
295681a5bbeSBoris Popov 	int error;
296681a5bbeSBoris Popov 
297681a5bbeSBoris Popov 	aio.iov_base = (caddr_t)&len;
298681a5bbeSBoris Popov 	aio.iov_len = sizeof(len);
299681a5bbeSBoris Popov 	auio.uio_iov = &aio;
300681a5bbeSBoris Popov 	auio.uio_iovcnt = 1;
301681a5bbeSBoris Popov 	auio.uio_segflg = UIO_SYSSPACE;
302681a5bbeSBoris Popov 	auio.uio_rw = UIO_READ;
303681a5bbeSBoris Popov 	auio.uio_offset = 0;
304681a5bbeSBoris Popov 	auio.uio_resid = sizeof(len);
305fce6fbfaSBoris Popov 	auio.uio_td = td;
30690e19ee8SDavide Italiano 	CURVNET_SET(so->so_vnet);
307b0668f71SRobert Watson 	error = soreceive(so, (struct sockaddr **)NULL, &auio,
308681a5bbeSBoris Popov 	    (struct mbuf **)NULL, (struct mbuf **)NULL, &flags);
30990e19ee8SDavide Italiano 	CURVNET_RESTORE();
310681a5bbeSBoris Popov 	if (error)
311681a5bbeSBoris Popov 		return error;
312681a5bbeSBoris Popov 	if (auio.uio_resid > 0) {
313681a5bbeSBoris Popov 		SMBSDEBUG("short reply\n");
314681a5bbeSBoris Popov 		return EPIPE;
315681a5bbeSBoris Popov 	}
316681a5bbeSBoris Popov 	len = ntohl(len);
317681a5bbeSBoris Popov 	*rpcodep = (len >> 24) & 0xFF;
318681a5bbeSBoris Popov 	len &= 0x1ffff;
319681a5bbeSBoris Popov 	if (len > SMB_MAXPKTLEN) {
320681a5bbeSBoris Popov 		SMBERROR("packet too long (%d)\n", len);
321681a5bbeSBoris Popov 		return EFBIG;
322681a5bbeSBoris Popov 	}
323681a5bbeSBoris Popov 	*lenp = len;
324681a5bbeSBoris Popov 	return 0;
325681a5bbeSBoris Popov }
326681a5bbeSBoris Popov 
327681a5bbeSBoris Popov static int
nbssn_recv(struct nbpcb * nbp,struct mbuf ** mpp,int * lenp,u_int8_t * rpcodep,struct thread * td)328681a5bbeSBoris Popov nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
329fce6fbfaSBoris Popov 	u_int8_t *rpcodep, struct thread *td)
330681a5bbeSBoris Popov {
331681a5bbeSBoris Popov 	struct socket *so = nbp->nbp_tso;
332681a5bbeSBoris Popov 	struct uio auio;
3333c304004SBoris Popov 	struct mbuf *m, *tm, *im;
334681a5bbeSBoris Popov 	u_int8_t rpcode;
3353c304004SBoris Popov 	int len, resid;
336681a5bbeSBoris Popov 	int error, rcvflg;
337681a5bbeSBoris Popov 
338681a5bbeSBoris Popov 	if (so == NULL)
339681a5bbeSBoris Popov 		return ENOTCONN;
340681a5bbeSBoris Popov 
341681a5bbeSBoris Popov 	if (mpp)
342681a5bbeSBoris Popov 		*mpp = NULL;
343681a5bbeSBoris Popov 	m = NULL;
3443c304004SBoris Popov 	for(;;) {
3453c304004SBoris Popov 		/*
3463c304004SBoris Popov 		 * Poll for a response header.
3473c304004SBoris Popov 		 * If we don't have one waiting, return.
3483c304004SBoris Popov 		 */
3492d494bc6SMatt Jacob 		len = 0;
3502d494bc6SMatt Jacob 		rpcode = 0;
351fce6fbfaSBoris Popov 		error = nbssn_recvhdr(nbp, &len, &rpcode, MSG_DONTWAIT, td);
352c0b99ffaSRobert Watson 		if ((so->so_state & (SS_ISDISCONNECTING | SS_ISDISCONNECTED)) ||
353c0b99ffaSRobert Watson 		    (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
354681a5bbeSBoris Popov 			nbp->nbp_state = NBST_CLOSED;
355681a5bbeSBoris Popov 			NBDEBUG("session closed by peer\n");
356681a5bbeSBoris Popov 			return ECONNRESET;
357681a5bbeSBoris Popov 		}
358681a5bbeSBoris Popov 		if (error)
359681a5bbeSBoris Popov 			return error;
360681a5bbeSBoris Popov 		if (len == 0 && nbp->nbp_state != NBST_SESSION)
361681a5bbeSBoris Popov 			break;
3623c304004SBoris Popov 		/* no data, try again */
363681a5bbeSBoris Popov 		if (rpcode == NB_SSN_KEEPALIVE)
364681a5bbeSBoris Popov 			continue;
3653c304004SBoris Popov 
3663c304004SBoris Popov 		/*
3673c304004SBoris Popov 		 * Loop, blocking, for data following the response header.
3683c304004SBoris Popov 		 *
3693c304004SBoris Popov 		 * Note that we can't simply block here with MSG_WAITALL for the
3703c304004SBoris Popov 		 * entire response size, as it may be larger than the TCP
3713c304004SBoris Popov 		 * slow-start window that the sender employs.  This will result
3723c304004SBoris Popov 		 * in the sender stalling until the delayed ACK is sent, then
3733c304004SBoris Popov 		 * resuming slow-start, resulting in very poor performance.
3743c304004SBoris Popov 		 *
3753c304004SBoris Popov 		 * Instead, we never request more than NB_SORECEIVE_CHUNK
3763c304004SBoris Popov 		 * bytes at a time, resulting in an ack being pushed by
3773c304004SBoris Popov 		 * the TCP code at the completion of each call.
3783c304004SBoris Popov 		 */
3793c304004SBoris Popov 		resid = len;
3803c304004SBoris Popov 		while (resid > 0) {
3813c304004SBoris Popov 			tm = NULL;
3823c304004SBoris Popov 			rcvflg = MSG_WAITALL;
383681a5bbeSBoris Popov 			bzero(&auio, sizeof(auio));
3843c304004SBoris Popov 			auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
385fce6fbfaSBoris Popov 			auio.uio_td = td;
3863c304004SBoris Popov 			resid -= auio.uio_resid;
3873c304004SBoris Popov 			/*
3883c304004SBoris Popov 			 * Spin until we have collected everything in
3893c304004SBoris Popov 			 * this chunk.
3903c304004SBoris Popov 			 */
391681a5bbeSBoris Popov 			do {
392681a5bbeSBoris Popov 				rcvflg = MSG_WAITALL;
39390e19ee8SDavide Italiano 				CURVNET_SET(so->so_vnet);
394b0668f71SRobert Watson 				error = soreceive(so, (struct sockaddr **)NULL,
395bd32b702STim J. Robbins 				    &auio, &tm, (struct mbuf **)NULL, &rcvflg);
39690e19ee8SDavide Italiano 				CURVNET_RESTORE();
397681a5bbeSBoris Popov 			} while (error == EWOULDBLOCK || error == EINTR ||
398681a5bbeSBoris Popov 				 error == ERESTART);
399681a5bbeSBoris Popov 			if (error)
4003c304004SBoris Popov 				goto out;
4013c304004SBoris Popov 			/* short return guarantees unhappiness */
402681a5bbeSBoris Popov 			if (auio.uio_resid > 0) {
403681a5bbeSBoris Popov 				SMBERROR("packet is shorter than expected\n");
404681a5bbeSBoris Popov 				error = EPIPE;
4053c304004SBoris Popov 				goto out;
406681a5bbeSBoris Popov 			}
4073c304004SBoris Popov 			/* append received chunk to previous chunk(s) */
4083c304004SBoris Popov 			if (m == NULL) {
4093c304004SBoris Popov 				m = tm;
4103c304004SBoris Popov 			} else {
4113c304004SBoris Popov 				/*
4123c304004SBoris Popov 				 * Just glue the new chain on the end.
4133c304004SBoris Popov 				 * Consumer will pullup as required.
4143c304004SBoris Popov 				 */
4153c304004SBoris Popov 				for (im = m; im->m_next != NULL; im = im->m_next)
4163c304004SBoris Popov 					;
4173c304004SBoris Popov 				im->m_next = tm;
4183c304004SBoris Popov 			}
4193c304004SBoris Popov 		}
4203c304004SBoris Popov 		/* got a session/message packet? */
421681a5bbeSBoris Popov 		if (nbp->nbp_state == NBST_SESSION &&
422681a5bbeSBoris Popov 		    rpcode == NB_SSN_MESSAGE)
423681a5bbeSBoris Popov 			break;
4243c304004SBoris Popov 		/* drop packet and try for another */
425681a5bbeSBoris Popov 		NBDEBUG("non-session packet %x\n", rpcode);
4263c304004SBoris Popov 		if (m) {
427681a5bbeSBoris Popov 			m_freem(m);
4283c304004SBoris Popov 			m = NULL;
429681a5bbeSBoris Popov 		}
4303c304004SBoris Popov 	}
4313c304004SBoris Popov 
4323c304004SBoris Popov out:
433681a5bbeSBoris Popov 	if (error) {
434681a5bbeSBoris Popov 		if (m)
435681a5bbeSBoris Popov 			m_freem(m);
436681a5bbeSBoris Popov 		return error;
437681a5bbeSBoris Popov 	}
438681a5bbeSBoris Popov 	if (mpp)
439681a5bbeSBoris Popov 		*mpp = m;
440681a5bbeSBoris Popov 	else
441681a5bbeSBoris Popov 		m_freem(m);
442681a5bbeSBoris Popov 	*lenp = len;
443681a5bbeSBoris Popov 	*rpcodep = rpcode;
444681a5bbeSBoris Popov 	return 0;
445681a5bbeSBoris Popov }
446681a5bbeSBoris Popov 
447681a5bbeSBoris Popov /*
448681a5bbeSBoris Popov  * SMB transport interface
449681a5bbeSBoris Popov  */
450681a5bbeSBoris Popov static int
smb_nbst_create(struct smb_vc * vcp,struct thread * td)451fce6fbfaSBoris Popov smb_nbst_create(struct smb_vc *vcp, struct thread *td)
452681a5bbeSBoris Popov {
453681a5bbeSBoris Popov 	struct nbpcb *nbp;
454681a5bbeSBoris Popov 
4551ede983cSDag-Erling Smørgrav 	nbp = malloc(sizeof *nbp, M_NBDATA, M_WAITOK);
456681a5bbeSBoris Popov 	bzero(nbp, sizeof *nbp);
457681a5bbeSBoris Popov 	nbp->nbp_timo.tv_sec = 15;	/* XXX: sysctl ? */
458681a5bbeSBoris Popov 	nbp->nbp_state = NBST_CLOSED;
459681a5bbeSBoris Popov 	nbp->nbp_vc = vcp;
460681a5bbeSBoris Popov 	nbp->nbp_sndbuf = smb_tcpsndbuf;
461681a5bbeSBoris Popov 	nbp->nbp_rcvbuf = smb_tcprcvbuf;
462681a5bbeSBoris Popov 	vcp->vc_tdata = nbp;
463681a5bbeSBoris Popov 	return 0;
464681a5bbeSBoris Popov }
465681a5bbeSBoris Popov 
466681a5bbeSBoris Popov static int
smb_nbst_done(struct smb_vc * vcp,struct thread * td)467fce6fbfaSBoris Popov smb_nbst_done(struct smb_vc *vcp, struct thread *td)
468681a5bbeSBoris Popov {
469681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
470681a5bbeSBoris Popov 
471681a5bbeSBoris Popov 	if (nbp == NULL)
472681a5bbeSBoris Popov 		return ENOTCONN;
473fce6fbfaSBoris Popov 	smb_nbst_disconnect(vcp, td);
474681a5bbeSBoris Popov 	if (nbp->nbp_laddr)
475681a5bbeSBoris Popov 		free(nbp->nbp_laddr, M_SONAME);
476681a5bbeSBoris Popov 	if (nbp->nbp_paddr)
477681a5bbeSBoris Popov 		free(nbp->nbp_paddr, M_SONAME);
478681a5bbeSBoris Popov 	free(nbp, M_NBDATA);
479681a5bbeSBoris Popov 	return 0;
480681a5bbeSBoris Popov }
481681a5bbeSBoris Popov 
482681a5bbeSBoris Popov static int
smb_nbst_bind(struct smb_vc * vcp,struct sockaddr * sap,struct thread * td)483fce6fbfaSBoris Popov smb_nbst_bind(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
484681a5bbeSBoris Popov {
485681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
486681a5bbeSBoris Popov 	struct sockaddr_nb *snb;
487681a5bbeSBoris Popov 	int error, slen;
488681a5bbeSBoris Popov 
489681a5bbeSBoris Popov 	NBDEBUG("\n");
490681a5bbeSBoris Popov 	error = EINVAL;
491681a5bbeSBoris Popov 	do {
492681a5bbeSBoris Popov 		if (nbp->nbp_flags & NBF_LOCADDR)
493681a5bbeSBoris Popov 			break;
494681a5bbeSBoris Popov 		/*
495681a5bbeSBoris Popov 		 * It is possible to create NETBIOS name in the kernel,
496681a5bbeSBoris Popov 		 * but nothing prevents us to do it in the user space.
497681a5bbeSBoris Popov 		 */
498681a5bbeSBoris Popov 		if (sap == NULL)
499681a5bbeSBoris Popov 			break;
500681a5bbeSBoris Popov 		slen = sap->sa_len;
501681a5bbeSBoris Popov 		if (slen < NB_MINSALEN)
502681a5bbeSBoris Popov 			break;
503746e5bf0SRobert Watson 		snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
504681a5bbeSBoris Popov 		if (snb == NULL) {
505681a5bbeSBoris Popov 			error = ENOMEM;
506681a5bbeSBoris Popov 			break;
507681a5bbeSBoris Popov 		}
508681a5bbeSBoris Popov 		nbp->nbp_laddr = snb;
509681a5bbeSBoris Popov 		nbp->nbp_flags |= NBF_LOCADDR;
510681a5bbeSBoris Popov 		error = 0;
511681a5bbeSBoris Popov 	} while(0);
512681a5bbeSBoris Popov 	return error;
513681a5bbeSBoris Popov }
514681a5bbeSBoris Popov 
515681a5bbeSBoris Popov static int
smb_nbst_connect(struct smb_vc * vcp,struct sockaddr * sap,struct thread * td)516fce6fbfaSBoris Popov smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
517681a5bbeSBoris Popov {
518681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
519681a5bbeSBoris Popov 	struct sockaddr_in sin;
520681a5bbeSBoris Popov 	struct sockaddr_nb *snb;
521681a5bbeSBoris Popov 	struct timespec ts1, ts2;
522681a5bbeSBoris Popov 	int error, slen;
523681a5bbeSBoris Popov 
524681a5bbeSBoris Popov 	NBDEBUG("\n");
525681a5bbeSBoris Popov 	if (nbp->nbp_tso != NULL)
526681a5bbeSBoris Popov 		return EISCONN;
527681a5bbeSBoris Popov 	if (nbp->nbp_laddr == NULL)
528681a5bbeSBoris Popov 		return EINVAL;
529681a5bbeSBoris Popov 	slen = sap->sa_len;
530681a5bbeSBoris Popov 	if (slen < NB_MINSALEN)
531681a5bbeSBoris Popov 		return EINVAL;
532681a5bbeSBoris Popov 	if (nbp->nbp_paddr) {
533681a5bbeSBoris Popov 		free(nbp->nbp_paddr, M_SONAME);
534681a5bbeSBoris Popov 		nbp->nbp_paddr = NULL;
535681a5bbeSBoris Popov 	}
536746e5bf0SRobert Watson 	snb = (struct sockaddr_nb*)sodupsockaddr(sap, M_WAITOK);
537681a5bbeSBoris Popov 	if (snb == NULL)
538681a5bbeSBoris Popov 		return ENOMEM;
539681a5bbeSBoris Popov 	nbp->nbp_paddr = snb;
540681a5bbeSBoris Popov 	sin = snb->snb_addrin;
541681a5bbeSBoris Popov 	getnanotime(&ts1);
542fce6fbfaSBoris Popov 	error = nb_connect_in(nbp, &sin, td);
543681a5bbeSBoris Popov 	if (error)
544681a5bbeSBoris Popov 		return error;
545681a5bbeSBoris Popov 	getnanotime(&ts2);
5466040822cSAlan Somers 	timespecsub(&ts2, &ts1, &ts2);
54708bd45d3SChristian Brueffer 	if (ts2.tv_sec == 0) {
548681a5bbeSBoris Popov 		ts2.tv_sec = 1;
54908bd45d3SChristian Brueffer 		ts2.tv_nsec = 0;
55008bd45d3SChristian Brueffer 	}
5516040822cSAlan Somers 	timespecadd(&ts2, &ts2, &nbp->nbp_timo);
5526040822cSAlan Somers 	timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
5536040822cSAlan Somers 	timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);	/*  * 4 */
554fce6fbfaSBoris Popov 	error = nbssn_rq_request(nbp, td);
555681a5bbeSBoris Popov 	if (error)
556fce6fbfaSBoris Popov 		smb_nbst_disconnect(vcp, td);
557681a5bbeSBoris Popov 	return error;
558681a5bbeSBoris Popov }
559681a5bbeSBoris Popov 
560681a5bbeSBoris Popov static int
smb_nbst_disconnect(struct smb_vc * vcp,struct thread * td)561fce6fbfaSBoris Popov smb_nbst_disconnect(struct smb_vc *vcp, struct thread *td)
562681a5bbeSBoris Popov {
563681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
564681a5bbeSBoris Popov 	struct socket *so;
565681a5bbeSBoris Popov 
566681a5bbeSBoris Popov 	if (nbp == NULL || nbp->nbp_tso == NULL)
567681a5bbeSBoris Popov 		return ENOTCONN;
568681a5bbeSBoris Popov 	if ((so = nbp->nbp_tso) != NULL) {
569681a5bbeSBoris Popov 		nbp->nbp_flags &= ~NBF_CONNECTED;
570681a5bbeSBoris Popov 		nbp->nbp_tso = (struct socket *)NULL;
571681a5bbeSBoris Popov 		soshutdown(so, 2);
572681a5bbeSBoris Popov 		soclose(so);
573681a5bbeSBoris Popov 	}
574681a5bbeSBoris Popov 	if (nbp->nbp_state != NBST_RETARGET) {
575681a5bbeSBoris Popov 		nbp->nbp_state = NBST_CLOSED;
576681a5bbeSBoris Popov 	}
577681a5bbeSBoris Popov 	return 0;
578681a5bbeSBoris Popov }
579681a5bbeSBoris Popov 
580681a5bbeSBoris Popov static int
smb_nbst_send(struct smb_vc * vcp,struct mbuf * m0,struct thread * td)581fce6fbfaSBoris Popov smb_nbst_send(struct smb_vc *vcp, struct mbuf *m0, struct thread *td)
582681a5bbeSBoris Popov {
583681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
584681a5bbeSBoris Popov 	int error;
585681a5bbeSBoris Popov 
586681a5bbeSBoris Popov 	if (nbp->nbp_state != NBST_SESSION) {
587681a5bbeSBoris Popov 		error = ENOTCONN;
588681a5bbeSBoris Popov 		goto abort;
589681a5bbeSBoris Popov 	}
590eb1b1807SGleb Smirnoff 	M_PREPEND(m0, 4, M_WAITOK);
591681a5bbeSBoris Popov 	nb_sethdr(m0, NB_SSN_MESSAGE, m_fixhdr(m0) - 4);
592fce6fbfaSBoris Popov 	error = nb_sosend(nbp->nbp_tso, m0, 0, td);
593681a5bbeSBoris Popov 	return error;
594681a5bbeSBoris Popov abort:
595681a5bbeSBoris Popov 	if (m0)
596681a5bbeSBoris Popov 		m_freem(m0);
597681a5bbeSBoris Popov 	return error;
598681a5bbeSBoris Popov }
599681a5bbeSBoris Popov 
600681a5bbeSBoris Popov static int
smb_nbst_recv(struct smb_vc * vcp,struct mbuf ** mpp,struct thread * td)601fce6fbfaSBoris Popov smb_nbst_recv(struct smb_vc *vcp, struct mbuf **mpp, struct thread *td)
602681a5bbeSBoris Popov {
603681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
604681a5bbeSBoris Popov 	u_int8_t rpcode;
605681a5bbeSBoris Popov 	int error, rplen;
606681a5bbeSBoris Popov 
607681a5bbeSBoris Popov 	nbp->nbp_flags |= NBF_RECVLOCK;
608fce6fbfaSBoris Popov 	error = nbssn_recv(nbp, mpp, &rplen, &rpcode, td);
609681a5bbeSBoris Popov 	nbp->nbp_flags &= ~NBF_RECVLOCK;
610681a5bbeSBoris Popov 	return error;
611681a5bbeSBoris Popov }
612681a5bbeSBoris Popov 
613681a5bbeSBoris Popov static void
smb_nbst_timo(struct smb_vc * vcp)614681a5bbeSBoris Popov smb_nbst_timo(struct smb_vc *vcp)
615681a5bbeSBoris Popov {
616681a5bbeSBoris Popov 	return;
617681a5bbeSBoris Popov }
618681a5bbeSBoris Popov 
619681a5bbeSBoris Popov static void
smb_nbst_intr(struct smb_vc * vcp)620681a5bbeSBoris Popov smb_nbst_intr(struct smb_vc *vcp)
621681a5bbeSBoris Popov {
622681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
623681a5bbeSBoris Popov 
624681a5bbeSBoris Popov 	if (nbp == NULL || nbp->nbp_tso == NULL)
625681a5bbeSBoris Popov 		return;
626681a5bbeSBoris Popov 	sorwakeup(nbp->nbp_tso);
627681a5bbeSBoris Popov 	sowwakeup(nbp->nbp_tso);
628681a5bbeSBoris Popov }
629681a5bbeSBoris Popov 
630681a5bbeSBoris Popov static int
smb_nbst_getparam(struct smb_vc * vcp,int param,void * data)631681a5bbeSBoris Popov smb_nbst_getparam(struct smb_vc *vcp, int param, void *data)
632681a5bbeSBoris Popov {
633681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
634681a5bbeSBoris Popov 
635681a5bbeSBoris Popov 	switch (param) {
636681a5bbeSBoris Popov 	    case SMBTP_SNDSZ:
637681a5bbeSBoris Popov 		*(int*)data = nbp->nbp_sndbuf;
638681a5bbeSBoris Popov 		break;
639681a5bbeSBoris Popov 	    case SMBTP_RCVSZ:
640681a5bbeSBoris Popov 		*(int*)data = nbp->nbp_rcvbuf;
641681a5bbeSBoris Popov 		break;
642681a5bbeSBoris Popov 	    case SMBTP_TIMEOUT:
643681a5bbeSBoris Popov 		*(struct timespec*)data = nbp->nbp_timo;
644681a5bbeSBoris Popov 		break;
645681a5bbeSBoris Popov 	    default:
646681a5bbeSBoris Popov 		return EINVAL;
647681a5bbeSBoris Popov 	}
648681a5bbeSBoris Popov 	return 0;
649681a5bbeSBoris Popov }
650681a5bbeSBoris Popov 
651681a5bbeSBoris Popov static int
smb_nbst_setparam(struct smb_vc * vcp,int param,void * data)652681a5bbeSBoris Popov smb_nbst_setparam(struct smb_vc *vcp, int param, void *data)
653681a5bbeSBoris Popov {
654681a5bbeSBoris Popov 	struct nbpcb *nbp = vcp->vc_tdata;
655681a5bbeSBoris Popov 
656681a5bbeSBoris Popov 	switch (param) {
657681a5bbeSBoris Popov 	    case SMBTP_SELECTID:
658681a5bbeSBoris Popov 		nbp->nbp_selectid = data;
659681a5bbeSBoris Popov 		break;
660681a5bbeSBoris Popov 	    default:
661681a5bbeSBoris Popov 		return EINVAL;
662681a5bbeSBoris Popov 	}
663681a5bbeSBoris Popov 	return 0;
664681a5bbeSBoris Popov }
665681a5bbeSBoris Popov 
666681a5bbeSBoris Popov /*
667681a5bbeSBoris Popov  * Check for fatal errors
668681a5bbeSBoris Popov  */
669681a5bbeSBoris Popov static int
smb_nbst_fatal(struct smb_vc * vcp,int error)670681a5bbeSBoris Popov smb_nbst_fatal(struct smb_vc *vcp, int error)
671681a5bbeSBoris Popov {
672681a5bbeSBoris Popov 	switch (error) {
673681a5bbeSBoris Popov 	    case ENOTCONN:
674681a5bbeSBoris Popov 	    case ENETRESET:
675681a5bbeSBoris Popov 	    case ECONNABORTED:
676681a5bbeSBoris Popov 		return 1;
677681a5bbeSBoris Popov 	}
678681a5bbeSBoris Popov 	return 0;
679681a5bbeSBoris Popov }
680681a5bbeSBoris Popov 
681681a5bbeSBoris Popov struct smb_tran_desc smb_tran_nbtcp_desc = {
682681a5bbeSBoris Popov 	SMBT_NBTCP,
683681a5bbeSBoris Popov 	smb_nbst_create, smb_nbst_done,
684681a5bbeSBoris Popov 	smb_nbst_bind, smb_nbst_connect, smb_nbst_disconnect,
685681a5bbeSBoris Popov 	smb_nbst_send, smb_nbst_recv,
686681a5bbeSBoris Popov 	smb_nbst_timo, smb_nbst_intr,
687681a5bbeSBoris Popov 	smb_nbst_getparam, smb_nbst_setparam,
688681a5bbeSBoris Popov 	smb_nbst_fatal
689681a5bbeSBoris Popov };
690