xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_session.c (revision bbf6f00c25b6a2bed23c35eac6d62998ecdb338c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/atomic.h>
27 #include <sys/strsubr.h>
28 #include <sys/synch.h>
29 #include <sys/types.h>
30 #include <sys/socketvar.h>
31 #include <sys/sdt.h>
32 #include <smbsrv/netbios.h>
33 #include <smbsrv/smb_kproto.h>
34 #include <smbsrv/string.h>
35 #include <inet/tcp.h>
36 
37 static volatile uint64_t smb_kids;
38 
39 uint32_t smb_keep_alive = SSN_KEEP_ALIVE_TIMEOUT;
40 
41 static void smb_session_cancel(smb_session_t *);
42 static int smb_session_message(smb_session_t *);
43 static int smb_session_xprt_puthdr(smb_session_t *, smb_xprt_t *,
44     uint8_t *, size_t);
45 static smb_user_t *smb_session_lookup_user(smb_session_t *, char *, char *);
46 static void smb_request_init_command_mbuf(smb_request_t *sr);
47 void dump_smb_inaddr(smb_inaddr_t *ipaddr);
48 
49 void
50 smb_session_timers(smb_session_list_t *se)
51 {
52 	smb_session_t	*session;
53 
54 	rw_enter(&se->se_lock, RW_READER);
55 	session = list_head(&se->se_act.lst);
56 	while (session) {
57 		/*
58 		 * Walk through the table and decrement each keep_alive
59 		 * timer that has not timed out yet. (keepalive > 0)
60 		 */
61 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
62 		if (session->keep_alive &&
63 		    (session->keep_alive != (uint32_t)-1))
64 			session->keep_alive--;
65 		session = list_next(&se->se_act.lst, session);
66 	}
67 	rw_exit(&se->se_lock);
68 }
69 
70 void
71 smb_session_correct_keep_alive_values(
72     smb_session_list_t	*se,
73     uint32_t		new_keep_alive)
74 {
75 	smb_session_t		*sn;
76 
77 	if (new_keep_alive == smb_keep_alive)
78 		return;
79 	/*
80 	 * keep alive == 0 means do not drop connection if it's idle
81 	 */
82 	smb_keep_alive = (new_keep_alive) ? new_keep_alive : -1;
83 
84 	/*
85 	 * Walk through the table and set each session to the new keep_alive
86 	 * value if they have not already timed out.  Block clock interrupts.
87 	 */
88 	rw_enter(&se->se_lock, RW_READER);
89 	sn = list_head(&se->se_rdy.lst);
90 	while (sn) {
91 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
92 		sn->keep_alive = new_keep_alive;
93 		sn = list_next(&se->se_rdy.lst, sn);
94 	}
95 	sn = list_head(&se->se_act.lst);
96 	while (sn) {
97 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
98 		if (sn->keep_alive)
99 			sn->keep_alive = new_keep_alive;
100 		sn = list_next(&se->se_act.lst, sn);
101 	}
102 	rw_exit(&se->se_lock);
103 }
104 
105 /*
106  * smb_reconnection_check
107  *
108  * This function is called when a client indicates its current connection
109  * should be the only one it has with the server, as indicated by VC=0 in
110  * a SessionSetupX request. We go through the session list and destroy any
111  * stale connections for that client.
112  *
113  * Clients don't associate IP addresses and servers. So a client may make
114  * independent connections (i.e. with VC=0) to a server with multiple
115  * IP addresses. So, when checking for a reconnection, we need to include
116  * the local IP address, to which the client is connecting, when checking
117  * for stale sessions.
118  *
119  * Also check the server's NetBIOS name to support simultaneous access by
120  * multiple clients behind a NAT server.  This will only work for SMB over
121  * NetBIOS on TCP port 139, it will not work SMB over TCP port 445 because
122  * there is no NetBIOS name.  See also Knowledge Base article Q301673.
123  */
124 void
125 smb_session_reconnection_check(smb_session_list_t *se, smb_session_t *sess)
126 {
127 	smb_session_t	*sn;
128 
129 	rw_enter(&se->se_lock, RW_READER);
130 	sn = list_head(&se->se_act.lst);
131 	while (sn) {
132 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
133 		if ((sn != sess) &&
134 		    smb_inet_equal(&sn->ipaddr, &sess->ipaddr) &&
135 		    smb_inet_equal(&sn->local_ipaddr, &sess->local_ipaddr) &&
136 		    (strcasecmp(sn->workstation, sess->workstation) == 0) &&
137 		    (sn->opentime <= sess->opentime) &&
138 		    (sn->s_kid < sess->s_kid)) {
139 			tsignal(sn->s_thread, SIGUSR1);
140 		}
141 		sn = list_next(&se->se_act.lst, sn);
142 	}
143 	rw_exit(&se->se_lock);
144 }
145 
146 /*
147  * Send a session message - supports SMB-over-NBT and SMB-over-TCP.
148  *
149  * The mbuf chain is copied into a contiguous buffer so that the whole
150  * message is submitted to smb_sosend as a single request.  This should
151  * help Ethereal/Wireshark delineate the packets correctly even though
152  * TCP_NODELAY has been set on the socket.
153  *
154  * If an mbuf chain is provided, it will be freed and set to NULL here.
155  */
156 int
157 smb_session_send(smb_session_t *session, uint8_t type, mbuf_chain_t *mbc)
158 {
159 	smb_txreq_t	*txr;
160 	smb_xprt_t	hdr;
161 	int		rc;
162 
163 	switch (session->s_state) {
164 	case SMB_SESSION_STATE_DISCONNECTED:
165 	case SMB_SESSION_STATE_TERMINATED:
166 		if ((mbc != NULL) && (mbc->chain != NULL)) {
167 			m_freem(mbc->chain);
168 			mbc->chain = NULL;
169 			mbc->flags = 0;
170 		}
171 		return (ENOTCONN);
172 	default:
173 		break;
174 	}
175 
176 	txr = smb_net_txr_alloc();
177 
178 	if ((mbc != NULL) && (mbc->chain != NULL)) {
179 		rc = mbc_moveout(mbc, (caddr_t)&txr->tr_buf[NETBIOS_HDR_SZ],
180 		    sizeof (txr->tr_buf) - NETBIOS_HDR_SZ, &txr->tr_len);
181 		if (rc != 0) {
182 			smb_net_txr_free(txr);
183 			return (rc);
184 		}
185 	}
186 
187 	hdr.xh_type = type;
188 	hdr.xh_length = (uint32_t)txr->tr_len;
189 
190 	rc = smb_session_xprt_puthdr(session, &hdr, txr->tr_buf,
191 	    NETBIOS_HDR_SZ);
192 
193 	if (rc != 0) {
194 		smb_net_txr_free(txr);
195 		return (rc);
196 	}
197 	txr->tr_len += NETBIOS_HDR_SZ;
198 	return (smb_net_txr_send(session->sock, &session->s_txlst, txr));
199 }
200 
201 /*
202  * Read, process and respond to a NetBIOS session request.
203  *
204  * A NetBIOS session must be established for SMB-over-NetBIOS.  Validate
205  * the calling and called name format and save the client NetBIOS name,
206  * which is used when a NetBIOS session is established to check for and
207  * cleanup leftover state from a previous session.
208  *
209  * Session requests are not valid for SMB-over-TCP, which is unfortunate
210  * because without the client name leftover state cannot be cleaned up
211  * if the client is behind a NAT server.
212  */
213 static int
214 smb_session_request(struct smb_session *session)
215 {
216 	int			rc;
217 	char			*calling_name;
218 	char			*called_name;
219 	char 			client_name[NETBIOS_NAME_SZ];
220 	struct mbuf_chain 	mbc;
221 	char 			*names = NULL;
222 	smb_wchar_t		*wbuf = NULL;
223 	smb_xprt_t		hdr;
224 	char *p;
225 	int rc1, rc2;
226 
227 	session->keep_alive = smb_keep_alive;
228 
229 	if ((rc = smb_session_xprt_gethdr(session, &hdr)) != 0)
230 		return (rc);
231 
232 	DTRACE_PROBE2(receive__session__req__xprthdr, struct session *, session,
233 	    smb_xprt_t *, &hdr);
234 
235 	if ((hdr.xh_type != SESSION_REQUEST) ||
236 	    (hdr.xh_length != NETBIOS_SESSION_REQUEST_DATA_LENGTH)) {
237 		DTRACE_PROBE1(receive__session__req__failed,
238 		    struct session *, session);
239 		return (EINVAL);
240 	}
241 
242 	names = kmem_alloc(hdr.xh_length, KM_SLEEP);
243 
244 	if ((rc = smb_sorecv(session->sock, names, hdr.xh_length)) != 0) {
245 		kmem_free(names, hdr.xh_length);
246 		DTRACE_PROBE1(receive__session__req__failed,
247 		    struct session *, session);
248 		return (rc);
249 	}
250 
251 	DTRACE_PROBE3(receive__session__req__data, struct session *, session,
252 	    char *, names, uint32_t, hdr.xh_length);
253 
254 	called_name = &names[0];
255 	calling_name = &names[NETBIOS_ENCODED_NAME_SZ + 2];
256 
257 	rc1 = netbios_name_isvalid(called_name, 0);
258 	rc2 = netbios_name_isvalid(calling_name, client_name);
259 
260 	if (rc1 == 0 || rc2 == 0) {
261 
262 		DTRACE_PROBE3(receive__invalid__session__req,
263 		    struct session *, session, char *, names,
264 		    uint32_t, hdr.xh_length);
265 
266 		kmem_free(names, hdr.xh_length);
267 		MBC_INIT(&mbc, MAX_DATAGRAM_LENGTH);
268 		(void) smb_mbc_encodef(&mbc, "b",
269 		    DATAGRAM_INVALID_SOURCE_NAME_FORMAT);
270 		(void) smb_session_send(session, NEGATIVE_SESSION_RESPONSE,
271 		    &mbc);
272 		return (EINVAL);
273 	}
274 
275 	DTRACE_PROBE3(receive__session__req__calling__decoded,
276 	    struct session *, session,
277 	    char *, calling_name, char *, client_name);
278 
279 	/*
280 	 * The client NetBIOS name is in oem codepage format.
281 	 * We need to convert it to unicode and store it in
282 	 * multi-byte format.  We also need to strip off any
283 	 * spaces added as part of the NetBIOS name encoding.
284 	 */
285 	wbuf = kmem_alloc((SMB_PI_MAX_HOST * sizeof (smb_wchar_t)), KM_SLEEP);
286 	(void) oemtoucs(wbuf, client_name, SMB_PI_MAX_HOST, OEM_CPG_850);
287 	(void) smb_wcstombs(session->workstation, wbuf, SMB_PI_MAX_HOST);
288 	kmem_free(wbuf, (SMB_PI_MAX_HOST * sizeof (smb_wchar_t)));
289 
290 	if ((p = strchr(session->workstation, ' ')) != 0)
291 		*p = '\0';
292 
293 	kmem_free(names, hdr.xh_length);
294 	return (smb_session_send(session, POSITIVE_SESSION_RESPONSE, NULL));
295 }
296 
297 /*
298  * Read 4-byte header from the session socket and build an in-memory
299  * session transport header.  See smb_xprt_t definition for header
300  * format information.
301  *
302  * Direct hosted NetBIOS-less SMB (SMB-over-TCP) uses port 445.  The
303  * first byte of the four-byte header must be 0 and the next three
304  * bytes contain the length of the remaining data.
305  */
306 int
307 smb_session_xprt_gethdr(smb_session_t *session, smb_xprt_t *ret_hdr)
308 {
309 	int		rc;
310 	unsigned char	buf[NETBIOS_HDR_SZ];
311 
312 	if ((rc = smb_sorecv(session->sock, buf, NETBIOS_HDR_SZ)) != 0)
313 		return (rc);
314 
315 	switch (session->s_local_port) {
316 	case IPPORT_NETBIOS_SSN:
317 		ret_hdr->xh_type = buf[0];
318 		ret_hdr->xh_length = (((uint32_t)buf[1] & 1) << 16) |
319 		    ((uint32_t)buf[2] << 8) |
320 		    ((uint32_t)buf[3]);
321 		break;
322 
323 	case IPPORT_SMB:
324 		ret_hdr->xh_type = buf[0];
325 
326 		if (ret_hdr->xh_type != 0) {
327 			cmn_err(CE_WARN, "invalid type (%u)", ret_hdr->xh_type);
328 			dump_smb_inaddr(&session->ipaddr);
329 			return (EPROTO);
330 		}
331 
332 		ret_hdr->xh_length = ((uint32_t)buf[1] << 16) |
333 		    ((uint32_t)buf[2] << 8) |
334 		    ((uint32_t)buf[3]);
335 		break;
336 
337 	default:
338 		cmn_err(CE_WARN, "invalid port %u", session->s_local_port);
339 		dump_smb_inaddr(&session->ipaddr);
340 		return (EPROTO);
341 	}
342 
343 	return (0);
344 }
345 
346 /*
347  * Encode a transport session packet header into a 4-byte buffer.
348  * See smb_xprt_t definition for header format information.
349  */
350 static int
351 smb_session_xprt_puthdr(smb_session_t *session, smb_xprt_t *hdr,
352     uint8_t *buf, size_t buflen)
353 {
354 	if (session == NULL || hdr == NULL ||
355 	    buf == NULL || buflen < NETBIOS_HDR_SZ) {
356 		return (-1);
357 	}
358 
359 	switch (session->s_local_port) {
360 	case IPPORT_NETBIOS_SSN:
361 		buf[0] = hdr->xh_type;
362 		buf[1] = ((hdr->xh_length >> 16) & 1);
363 		buf[2] = (hdr->xh_length >> 8) & 0xff;
364 		buf[3] = hdr->xh_length & 0xff;
365 		break;
366 
367 	case IPPORT_SMB:
368 		buf[0] = hdr->xh_type;
369 		buf[1] = (hdr->xh_length >> 16) & 0xff;
370 		buf[2] = (hdr->xh_length >> 8) & 0xff;
371 		buf[3] = hdr->xh_length & 0xff;
372 		break;
373 
374 	default:
375 		cmn_err(CE_WARN, "invalid port %u", session->s_local_port);
376 		dump_smb_inaddr(&session->ipaddr);
377 		return (-1);
378 	}
379 
380 	return (0);
381 }
382 
383 static void
384 smb_request_init_command_mbuf(smb_request_t *sr)
385 {
386 	MGET(sr->command.chain, 0, MT_DATA);
387 
388 	/*
389 	 * Setup mbuf, mimic MCLGET but use the complete packet buffer.
390 	 */
391 	sr->command.chain->m_ext.ext_buf = sr->sr_request_buf;
392 	sr->command.chain->m_data = sr->command.chain->m_ext.ext_buf;
393 	sr->command.chain->m_len = sr->sr_req_length;
394 	sr->command.chain->m_flags |= M_EXT;
395 	sr->command.chain->m_ext.ext_size = sr->sr_req_length;
396 	sr->command.chain->m_ext.ext_ref = &mclrefnoop;
397 
398 	/*
399 	 * Initialize the rest of the mbuf_chain fields
400 	 */
401 	sr->command.flags = 0;
402 	sr->command.shadow_of = 0;
403 	sr->command.max_bytes = sr->sr_req_length;
404 	sr->command.chain_offset = 0;
405 }
406 
407 /*
408  * smb_request_cancel
409  *
410  * Handle a cancel for a request properly depending on the current request
411  * state.
412  */
413 void
414 smb_request_cancel(smb_request_t *sr)
415 {
416 	mutex_enter(&sr->sr_mutex);
417 	switch (sr->sr_state) {
418 
419 	case SMB_REQ_STATE_SUBMITTED:
420 	case SMB_REQ_STATE_ACTIVE:
421 	case SMB_REQ_STATE_CLEANED_UP:
422 		sr->sr_state = SMB_REQ_STATE_CANCELED;
423 		break;
424 
425 	case SMB_REQ_STATE_WAITING_LOCK:
426 		/*
427 		 * This request is waiting on a lock.  Wakeup everything
428 		 * waiting on the lock so that the relevant thread regains
429 		 * control and notices that is has been canceled.  The
430 		 * other lock request threads waiting on this lock will go
431 		 * back to sleep when they discover they are still blocked.
432 		 */
433 		sr->sr_state = SMB_REQ_STATE_CANCELED;
434 
435 		ASSERT(sr->sr_awaiting != NULL);
436 		mutex_enter(&sr->sr_awaiting->l_mutex);
437 		cv_broadcast(&sr->sr_awaiting->l_cv);
438 		mutex_exit(&sr->sr_awaiting->l_mutex);
439 		break;
440 
441 	case SMB_REQ_STATE_WAITING_EVENT:
442 	case SMB_REQ_STATE_EVENT_OCCURRED:
443 		/*
444 		 * Cancellations for these states are handled by the
445 		 * notify-change code
446 		 */
447 		break;
448 
449 	case SMB_REQ_STATE_COMPLETED:
450 	case SMB_REQ_STATE_CANCELED:
451 		/*
452 		 * No action required for these states since the request
453 		 * is completing.
454 		 */
455 		break;
456 	/*
457 	 * Cases included:
458 	 *	SMB_REQ_STATE_FREE:
459 	 *	SMB_REQ_STATE_INITIALIZING:
460 	 */
461 	default:
462 		SMB_PANIC();
463 	}
464 	mutex_exit(&sr->sr_mutex);
465 }
466 
467 /*
468  * This is the entry point for processing SMB messages over NetBIOS or
469  * SMB-over-TCP.
470  *
471  * NetBIOS connections require a session request to establish a session
472  * on which to send session messages.
473  *
474  * Session requests are not valid on SMB-over-TCP.  We don't need to do
475  * anything here as session requests will be treated as an error when
476  * handling session messages.
477  */
478 int
479 smb_session_daemon(smb_session_list_t *se)
480 {
481 	int		rc = 0;
482 	smb_session_t	*session;
483 
484 	session = smb_session_list_activate_head(se);
485 	if (session == NULL)
486 		return (EINVAL);
487 
488 	if (session->s_local_port == IPPORT_NETBIOS_SSN) {
489 		rc = smb_session_request(session);
490 		if (rc) {
491 			smb_rwx_rwenter(&session->s_lock, RW_WRITER);
492 			session->s_state = SMB_SESSION_STATE_DISCONNECTED;
493 			smb_rwx_rwexit(&session->s_lock);
494 			smb_session_list_terminate(se, session);
495 			return (rc);
496 		}
497 	}
498 
499 	smb_rwx_rwenter(&session->s_lock, RW_WRITER);
500 	session->s_state = SMB_SESSION_STATE_ESTABLISHED;
501 	smb_rwx_rwexit(&session->s_lock);
502 
503 	rc = smb_session_message(session);
504 
505 	smb_rwx_rwenter(&session->s_lock, RW_WRITER);
506 	session->s_state = SMB_SESSION_STATE_DISCONNECTED;
507 	smb_rwx_rwexit(&session->s_lock);
508 
509 	smb_soshutdown(session->sock);
510 
511 	DTRACE_PROBE2(session__drop, struct session *, session, int, rc);
512 
513 	smb_session_cancel(session);
514 
515 	/*
516 	 * At this point everything related to the session should have been
517 	 * cleaned up and we expect that nothing will attempt to use the
518 	 * socket.
519 	 */
520 	smb_session_list_terminate(se, session);
521 
522 	return (rc);
523 }
524 
525 /*
526  * Read and process SMB requests.
527  *
528  * Returns:
529  *	0	Success
530  *	1	Unable to read transport header
531  *	2	Invalid transport header type
532  *	3	Invalid SMB length (too small)
533  *	4	Unable to read SMB header
534  *	5	Invalid SMB header (bad magic number)
535  *	6	Unable to read SMB data
536  *	2x	Write raw failed
537  */
538 static int
539 smb_session_message(smb_session_t *session)
540 {
541 	smb_request_t	*sr = NULL;
542 	smb_xprt_t	hdr;
543 	uint8_t		*req_buf;
544 	uint32_t	resid;
545 	int		rc;
546 
547 	for (;;) {
548 
549 		rc = smb_session_xprt_gethdr(session, &hdr);
550 		if (rc)
551 			return (rc);
552 
553 		DTRACE_PROBE2(session__receive__xprthdr, session_t *, session,
554 		    smb_xprt_t *, &hdr);
555 
556 		if (hdr.xh_type != SESSION_MESSAGE) {
557 			/*
558 			 * Anything other than SESSION_MESSAGE or
559 			 * SESSION_KEEP_ALIVE is an error.  A SESSION_REQUEST
560 			 * may indicate a new session request but we need to
561 			 * close this session and we can treat it as an error
562 			 * here.
563 			 */
564 			if (hdr.xh_type == SESSION_KEEP_ALIVE) {
565 				session->keep_alive = smb_keep_alive;
566 				continue;
567 			}
568 			return (EPROTO);
569 		}
570 
571 		if (hdr.xh_length < SMB_HEADER_LEN)
572 			return (EPROTO);
573 
574 		session->keep_alive = smb_keep_alive;
575 
576 		/*
577 		 * Allocate a request context, read the SMB header and validate
578 		 * it. The sr includes a buffer large enough to hold the SMB
579 		 * request payload.  If the header looks valid, read any
580 		 * remaining data.
581 		 */
582 		sr = smb_request_alloc(session, hdr.xh_length);
583 
584 		req_buf = (uint8_t *)sr->sr_request_buf;
585 		resid = hdr.xh_length;
586 
587 		rc = smb_sorecv(session->sock, req_buf, SMB_HEADER_LEN);
588 		if (rc) {
589 			smb_request_free(sr);
590 			return (rc);
591 		}
592 
593 		if (SMB_PROTOCOL_MAGIC_INVALID(sr)) {
594 			smb_request_free(sr);
595 			return (EPROTO);
596 		}
597 
598 		if (resid > SMB_HEADER_LEN) {
599 			req_buf += SMB_HEADER_LEN;
600 			resid -= SMB_HEADER_LEN;
601 
602 			rc = smb_sorecv(session->sock, req_buf, resid);
603 			if (rc) {
604 				smb_request_free(sr);
605 				return (rc);
606 			}
607 		}
608 
609 		/*
610 		 * Initialize command MBC to represent the received data.
611 		 */
612 		smb_request_init_command_mbuf(sr);
613 
614 		DTRACE_PROBE1(session__receive__smb, smb_request_t *, sr);
615 
616 		/*
617 		 * If this is a raw write, hand off the request.  The handler
618 		 * will retrieve the remaining raw data and process the request.
619 		 */
620 		if (SMB_IS_WRITERAW(sr)) {
621 			rc = smb_handle_write_raw(session, sr);
622 			/* XXX smb_request_free(sr); ??? */
623 			return (rc);
624 		}
625 
626 		sr->sr_state = SMB_REQ_STATE_SUBMITTED;
627 		(void) taskq_dispatch(session->s_server->sv_thread_pool,
628 		    smb_session_worker, sr, TQ_SLEEP);
629 	}
630 }
631 
632 /*
633  * Port will be IPPORT_NETBIOS_SSN or IPPORT_SMB.
634  */
635 smb_session_t *
636 smb_session_create(ksocket_t new_so, uint16_t port, smb_server_t *sv,
637     int family)
638 {
639 	struct sockaddr_in	sin;
640 	socklen_t		slen;
641 	struct sockaddr_in6	sin6;
642 	smb_session_t		*session;
643 
644 	session = kmem_cache_alloc(sv->si_cache_session, KM_SLEEP);
645 	bzero(session, sizeof (smb_session_t));
646 
647 	if (smb_idpool_constructor(&session->s_uid_pool)) {
648 		kmem_cache_free(sv->si_cache_session, session);
649 		return (NULL);
650 	}
651 
652 	session->s_kid = SMB_NEW_KID();
653 	session->s_state = SMB_SESSION_STATE_INITIALIZED;
654 	session->native_os = NATIVE_OS_UNKNOWN;
655 	session->opentime = lbolt64;
656 	session->keep_alive = smb_keep_alive;
657 	session->activity_timestamp = lbolt64;
658 
659 	smb_slist_constructor(&session->s_req_list, sizeof (smb_request_t),
660 	    offsetof(smb_request_t, sr_session_lnd));
661 
662 	smb_llist_constructor(&session->s_user_list, sizeof (smb_user_t),
663 	    offsetof(smb_user_t, u_lnd));
664 
665 	smb_llist_constructor(&session->s_xa_list, sizeof (smb_xa_t),
666 	    offsetof(smb_xa_t, xa_lnd));
667 
668 	list_create(&session->s_oplock_brkreqs, sizeof (mbuf_chain_t),
669 	    offsetof(mbuf_chain_t, mbc_lnd));
670 
671 	smb_net_txl_constructor(&session->s_txlst);
672 
673 	smb_rwx_init(&session->s_lock);
674 
675 	if (new_so) {
676 		if (family == AF_INET) {
677 			slen = sizeof (sin);
678 			(void) ksocket_getsockname(new_so,
679 			    (struct sockaddr *)&sin, &slen, CRED());
680 			bcopy(&sin.sin_addr,
681 			    &session->local_ipaddr.au_addr.au_ipv4,
682 			    sizeof (in_addr_t));
683 			slen = sizeof (sin);
684 			(void) ksocket_getpeername(new_so,
685 			    (struct sockaddr *)&sin, &slen, CRED());
686 			bcopy(&sin.sin_addr,
687 			    &session->ipaddr.au_addr.au_ipv4,
688 			    sizeof (in_addr_t));
689 		} else {
690 			slen = sizeof (sin6);
691 			(void) ksocket_getsockname(new_so,
692 			    (struct sockaddr *)&sin6, &slen, CRED());
693 			bcopy(&sin6.sin6_addr,
694 			    &session->local_ipaddr.au_addr.au_ipv6,
695 			    sizeof (in6_addr_t));
696 			slen = sizeof (sin6);
697 			(void) ksocket_getpeername(new_so,
698 			    (struct sockaddr *)&sin6, &slen, CRED());
699 			bcopy(&sin6.sin6_addr,
700 			    &session->ipaddr.au_addr.au_ipv6,
701 			    sizeof (in6_addr_t));
702 		}
703 		session->ipaddr.a_family = family;
704 		session->local_ipaddr.a_family = family;
705 		session->s_local_port = port;
706 		session->sock = new_so;
707 	}
708 
709 	session->s_server = sv;
710 	smb_server_get_cfg(sv, &session->s_cfg);
711 	session->s_cache_request = sv->si_cache_request;
712 	session->s_cache = sv->si_cache_session;
713 	session->s_magic = SMB_SESSION_MAGIC;
714 	return (session);
715 }
716 
717 void
718 smb_session_delete(smb_session_t *session)
719 {
720 	mbuf_chain_t	*mbc;
721 
722 	ASSERT(session->s_magic == SMB_SESSION_MAGIC);
723 
724 	session->s_magic = 0;
725 
726 	smb_rwx_destroy(&session->s_lock);
727 	smb_net_txl_destructor(&session->s_txlst);
728 
729 	while ((mbc = list_head(&session->s_oplock_brkreqs)) != NULL) {
730 		SMB_MBC_VALID(mbc);
731 		list_remove(&session->s_oplock_brkreqs, mbc);
732 		smb_mbc_free(mbc);
733 	}
734 	list_destroy(&session->s_oplock_brkreqs);
735 
736 	smb_slist_destructor(&session->s_req_list);
737 	smb_llist_destructor(&session->s_user_list);
738 	smb_llist_destructor(&session->s_xa_list);
739 
740 	ASSERT(session->s_tree_cnt == 0);
741 	ASSERT(session->s_file_cnt == 0);
742 	ASSERT(session->s_dir_cnt == 0);
743 
744 	smb_idpool_destructor(&session->s_uid_pool);
745 	kmem_cache_free(session->s_cache, session);
746 }
747 
748 static void
749 smb_session_cancel(smb_session_t *session)
750 {
751 	smb_xa_t	*xa, *nextxa;
752 
753 	/* All the request currently being treated must be canceled. */
754 	smb_session_cancel_requests(session, NULL, NULL);
755 
756 	/*
757 	 * We wait for the completion of all the requests associated with
758 	 * this session.
759 	 */
760 	smb_slist_wait_for_empty(&session->s_req_list);
761 
762 	/*
763 	 * At this point the reference count of the users, trees, files,
764 	 * directories should be zero. It should be possible to destroy them
765 	 * without any problem.
766 	 */
767 	xa = smb_llist_head(&session->s_xa_list);
768 	while (xa) {
769 		nextxa = smb_llist_next(&session->s_xa_list, xa);
770 		smb_xa_close(xa);
771 		xa = nextxa;
772 	}
773 	smb_user_logoff_all(session);
774 }
775 
776 /*
777  * Cancel requests.  If a non-null tree is specified, only requests specific
778  * to that tree will be cancelled.  If a non-null sr is specified, that sr
779  * will be not be cancelled - this would typically be the caller's sr.
780  */
781 void
782 smb_session_cancel_requests(
783     smb_session_t	*session,
784     smb_tree_t		*tree,
785     smb_request_t	*exclude_sr)
786 {
787 	smb_request_t	*sr;
788 
789 	smb_process_session_notify_change_queue(session, tree);
790 
791 	smb_slist_enter(&session->s_req_list);
792 	sr = smb_slist_head(&session->s_req_list);
793 
794 	while (sr) {
795 		ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
796 		if ((sr != exclude_sr) &&
797 		    (tree == NULL || sr->tid_tree == tree))
798 			smb_request_cancel(sr);
799 
800 		sr = smb_slist_next(&session->s_req_list, sr);
801 	}
802 
803 	smb_slist_exit(&session->s_req_list);
804 }
805 
806 void
807 smb_session_worker(void	*arg)
808 {
809 	smb_request_t	*sr;
810 
811 	sr = (smb_request_t *)arg;
812 
813 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
814 
815 	sr->sr_worker = curthread;
816 	mutex_enter(&sr->sr_mutex);
817 	switch (sr->sr_state) {
818 	case SMB_REQ_STATE_SUBMITTED:
819 		mutex_exit(&sr->sr_mutex);
820 		if (smb_dispatch_request(sr)) {
821 			mutex_enter(&sr->sr_mutex);
822 			sr->sr_state = SMB_REQ_STATE_COMPLETED;
823 			mutex_exit(&sr->sr_mutex);
824 			smb_request_free(sr);
825 		}
826 		break;
827 
828 	default:
829 		ASSERT(sr->sr_state == SMB_REQ_STATE_CANCELED);
830 		sr->sr_state = SMB_REQ_STATE_COMPLETED;
831 		mutex_exit(&sr->sr_mutex);
832 		smb_request_free(sr);
833 		break;
834 	}
835 }
836 
837 /*
838  * smb_session_disconnect_share
839  *
840  * Disconnects the specified share. This function should be called after the
841  * share passed in has been made unavailable by the "share manager".
842  */
843 void
844 smb_session_disconnect_share(smb_session_list_t *se, char *sharename)
845 {
846 	smb_session_t	*session;
847 
848 	rw_enter(&se->se_lock, RW_READER);
849 	session = list_head(&se->se_act.lst);
850 	while (session) {
851 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
852 		smb_rwx_rwenter(&session->s_lock, RW_READER);
853 		switch (session->s_state) {
854 		case SMB_SESSION_STATE_NEGOTIATED:
855 		case SMB_SESSION_STATE_OPLOCK_BREAKING:
856 		case SMB_SESSION_STATE_WRITE_RAW_ACTIVE: {
857 			smb_user_t	*user;
858 			smb_user_t	*next;
859 
860 			user = smb_user_lookup_by_state(session, NULL);
861 			while (user) {
862 				smb_user_disconnect_share(user, sharename);
863 				next = smb_user_lookup_by_state(session, user);
864 				smb_user_release(user);
865 				user = next;
866 			}
867 			break;
868 
869 		}
870 		default:
871 			break;
872 		}
873 		smb_rwx_rwexit(&session->s_lock);
874 		session = list_next(&se->se_act.lst, session);
875 	}
876 	rw_exit(&se->se_lock);
877 }
878 
879 void
880 smb_session_list_constructor(smb_session_list_t *se)
881 {
882 	bzero(se, sizeof (*se));
883 	rw_init(&se->se_lock, NULL, RW_DEFAULT, NULL);
884 	list_create(&se->se_rdy.lst, sizeof (smb_session_t),
885 	    offsetof(smb_session_t, s_lnd));
886 	list_create(&se->se_act.lst, sizeof (smb_session_t),
887 	    offsetof(smb_session_t, s_lnd));
888 }
889 
890 void
891 smb_session_list_destructor(smb_session_list_t *se)
892 {
893 	list_destroy(&se->se_rdy.lst);
894 	list_destroy(&se->se_act.lst);
895 	rw_destroy(&se->se_lock);
896 }
897 
898 void
899 smb_session_list_append(smb_session_list_t *se, smb_session_t *session)
900 {
901 	ASSERT(session->s_magic == SMB_SESSION_MAGIC);
902 	ASSERT(session->s_state == SMB_SESSION_STATE_INITIALIZED);
903 
904 	rw_enter(&se->se_lock, RW_WRITER);
905 	list_insert_tail(&se->se_rdy.lst, session);
906 	se->se_rdy.count++;
907 	se->se_wrop++;
908 	rw_exit(&se->se_lock);
909 }
910 
911 void
912 smb_session_list_delete_tail(smb_session_list_t *se)
913 {
914 	smb_session_t	*session;
915 
916 	rw_enter(&se->se_lock, RW_WRITER);
917 	session = list_tail(&se->se_rdy.lst);
918 	if (session) {
919 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
920 		ASSERT(session->s_state == SMB_SESSION_STATE_INITIALIZED);
921 		list_remove(&se->se_rdy.lst, session);
922 		ASSERT(se->se_rdy.count);
923 		se->se_rdy.count--;
924 		rw_exit(&se->se_lock);
925 		smb_session_delete(session);
926 		return;
927 	}
928 	rw_exit(&se->se_lock);
929 }
930 
931 smb_session_t *
932 smb_session_list_activate_head(smb_session_list_t *se)
933 {
934 	smb_session_t	*session;
935 
936 	rw_enter(&se->se_lock, RW_WRITER);
937 	session = list_head(&se->se_rdy.lst);
938 	if (session) {
939 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
940 		smb_rwx_rwenter(&session->s_lock, RW_WRITER);
941 		ASSERT(session->s_state == SMB_SESSION_STATE_INITIALIZED);
942 		session->s_thread = curthread;
943 		session->s_ktdid = session->s_thread->t_did;
944 		smb_rwx_rwexit(&session->s_lock);
945 		list_remove(&se->se_rdy.lst, session);
946 		se->se_rdy.count--;
947 		list_insert_tail(&se->se_act.lst, session);
948 		se->se_act.count++;
949 		se->se_wrop++;
950 	}
951 	rw_exit(&se->se_lock);
952 	return (session);
953 }
954 
955 void
956 smb_session_list_terminate(smb_session_list_t *se, smb_session_t *session)
957 {
958 	ASSERT(session->s_magic == SMB_SESSION_MAGIC);
959 
960 	rw_enter(&se->se_lock, RW_WRITER);
961 
962 	smb_rwx_rwenter(&session->s_lock, RW_WRITER);
963 	ASSERT(session->s_state == SMB_SESSION_STATE_DISCONNECTED);
964 	session->s_state = SMB_SESSION_STATE_TERMINATED;
965 	smb_sodestroy(session->sock);
966 	session->sock = NULL;
967 	smb_rwx_rwexit(&session->s_lock);
968 
969 	list_remove(&se->se_act.lst, session);
970 	se->se_act.count--;
971 	se->se_wrop++;
972 
973 	ASSERT(session->s_thread == curthread);
974 
975 	rw_exit(&se->se_lock);
976 
977 	smb_session_delete(session);
978 }
979 
980 /*
981  * smb_session_list_signal
982  *
983  * This function signals all the session threads. The intent is to terminate
984  * them. The sessions still in the SMB_SESSION_STATE_INITIALIZED are delete
985  * immediately.
986  *
987  * This function must only be called by the threads listening and accepting
988  * connections. They must pass in their respective session list.
989  */
990 void
991 smb_session_list_signal(smb_session_list_t *se)
992 {
993 	smb_session_t	*session;
994 
995 	rw_enter(&se->se_lock, RW_WRITER);
996 	while (session = list_head(&se->se_rdy.lst)) {
997 
998 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
999 
1000 		smb_rwx_rwenter(&session->s_lock, RW_WRITER);
1001 		ASSERT(session->s_state == SMB_SESSION_STATE_INITIALIZED);
1002 		session->s_state = SMB_SESSION_STATE_TERMINATED;
1003 		smb_sodestroy(session->sock);
1004 		session->sock = NULL;
1005 		smb_rwx_rwexit(&session->s_lock);
1006 
1007 		list_remove(&se->se_rdy.lst, session);
1008 		se->se_rdy.count--;
1009 		se->se_wrop++;
1010 
1011 		rw_exit(&se->se_lock);
1012 		smb_session_delete(session);
1013 		rw_enter(&se->se_lock, RW_WRITER);
1014 	}
1015 	rw_downgrade(&se->se_lock);
1016 
1017 	session = list_head(&se->se_act.lst);
1018 	while (session) {
1019 
1020 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
1021 		tsignal(session->s_thread, SIGUSR1);
1022 		session = list_next(&se->se_act.lst, session);
1023 	}
1024 	rw_exit(&se->se_lock);
1025 }
1026 
1027 /*
1028  * smb_session_lookup_user
1029  */
1030 static smb_user_t *
1031 smb_session_lookup_user(smb_session_t *session, char *domain, char *name)
1032 {
1033 	smb_user_t	*user;
1034 	smb_llist_t	*ulist;
1035 
1036 	ulist = &session->s_user_list;
1037 	smb_llist_enter(ulist, RW_READER);
1038 	user = smb_llist_head(ulist);
1039 	while (user) {
1040 		ASSERT(user->u_magic == SMB_USER_MAGIC);
1041 		if (!smb_strcasecmp(user->u_name, name, 0) &&
1042 		    !smb_strcasecmp(user->u_domain, domain, 0)) {
1043 			if (smb_user_hold(user))
1044 				break;
1045 		}
1046 		user = smb_llist_next(ulist, user);
1047 	}
1048 	smb_llist_exit(ulist);
1049 
1050 	return (user);
1051 }
1052 
1053 /*
1054  * If a user attempts to log in subsequently from the specified session,
1055  * duplicates the existing SMB user instance such that all SMB user
1056  * instances that corresponds to the same user on the given session
1057  * reference the same user's cred.
1058  *
1059  * Returns NULL if the given user hasn't yet logged in from this
1060  * specified session.  Otherwise, returns a user instance that corresponds
1061  * to this subsequent login.
1062  */
1063 smb_user_t *
1064 smb_session_dup_user(smb_session_t *session, char *domain, char *account_name)
1065 {
1066 	smb_user_t *orig_user = NULL;
1067 	smb_user_t *user = NULL;
1068 
1069 	orig_user = smb_session_lookup_user(session, domain,
1070 	    account_name);
1071 
1072 	if (orig_user) {
1073 		user = smb_user_dup(orig_user);
1074 		smb_user_release(orig_user);
1075 	}
1076 
1077 	return (user);
1078 }
1079 
1080 /*
1081  * Copy the session workstation/client name to buf.  If the workstation
1082  * is an empty string (which it will be on TCP connections), use the
1083  * client IP address.
1084  */
1085 void
1086 smb_session_getclient(smb_session_t *sn, char *buf, size_t buflen)
1087 {
1088 	char		ipbuf[INET6_ADDRSTRLEN];
1089 	smb_inaddr_t	*ipaddr;
1090 
1091 	ASSERT(sn);
1092 	ASSERT(buf);
1093 	ASSERT(buflen);
1094 
1095 	*buf = '\0';
1096 
1097 	if (sn->workstation[0] != '\0') {
1098 		(void) strlcpy(buf, sn->workstation, buflen);
1099 		return;
1100 	}
1101 
1102 	ipaddr = &sn->ipaddr;
1103 	if (smb_inet_ntop(ipaddr, ipbuf, SMB_IPSTRLEN(ipaddr->a_family)))
1104 		(void) strlcpy(buf, ipbuf, buflen);
1105 }
1106 
1107 /*
1108  * Check whether or not the specified client name is the client of this
1109  * session.  The name may be in UNC format (\\CLIENT).
1110  *
1111  * A workstation/client name is setup on NBT connections as part of the
1112  * NetBIOS session request but that isn't available on TCP connections.
1113  * If the session doesn't have a client name we typically return the
1114  * client IP address as the workstation name on MSRPC requests.  So we
1115  * check for the IP address here in addition to the workstation name.
1116  */
1117 boolean_t
1118 smb_session_isclient(smb_session_t *sn, const char *client)
1119 {
1120 	char		buf[INET6_ADDRSTRLEN];
1121 	smb_inaddr_t	*ipaddr;
1122 
1123 	client += strspn(client, "\\");
1124 
1125 	if (smb_strcasecmp(client, sn->workstation, 0) == 0)
1126 		return (B_TRUE);
1127 
1128 	ipaddr = &sn->ipaddr;
1129 	if (smb_inet_ntop(ipaddr, buf, SMB_IPSTRLEN(ipaddr->a_family)) == NULL)
1130 		return (B_FALSE);
1131 
1132 	if (smb_strcasecmp(client, buf, 0) == 0)
1133 		return (B_TRUE);
1134 
1135 	return (B_FALSE);
1136 }
1137 
1138 /*
1139  * smb_request_alloc
1140  *
1141  * Allocate an smb_request_t structure from the kmem_cache.  Partially
1142  * initialize the found/new request.
1143  *
1144  * Returns pointer to a request
1145  */
1146 smb_request_t *
1147 smb_request_alloc(smb_session_t *session, int req_length)
1148 {
1149 	smb_request_t	*sr;
1150 
1151 	ASSERT(session->s_magic == SMB_SESSION_MAGIC);
1152 
1153 	sr = kmem_cache_alloc(session->s_cache_request, KM_SLEEP);
1154 
1155 	/*
1156 	 * Future:  Use constructor to pre-initialize some fields.  For now
1157 	 * there are so many fields that it is easiest just to zero the
1158 	 * whole thing and start over.
1159 	 */
1160 	bzero(sr, sizeof (smb_request_t));
1161 
1162 	mutex_init(&sr->sr_mutex, NULL, MUTEX_DEFAULT, NULL);
1163 	smb_srm_init(sr);
1164 	sr->session = session;
1165 	sr->sr_server = session->s_server;
1166 	sr->sr_gmtoff = session->s_server->si_gmtoff;
1167 	sr->sr_cache = session->s_server->si_cache_request;
1168 	sr->sr_cfg = &session->s_cfg;
1169 	sr->command.max_bytes = req_length;
1170 	sr->reply.max_bytes = smb_maxbufsize;
1171 	sr->sr_req_length = req_length;
1172 	if (req_length)
1173 		sr->sr_request_buf = kmem_alloc(req_length, KM_SLEEP);
1174 	sr->sr_magic = SMB_REQ_MAGIC;
1175 	sr->sr_state = SMB_REQ_STATE_INITIALIZING;
1176 	smb_slist_insert_tail(&session->s_req_list, sr);
1177 	return (sr);
1178 }
1179 
1180 /*
1181  * smb_request_free
1182  *
1183  * release the memories which have been allocated for a smb request.
1184  */
1185 void
1186 smb_request_free(smb_request_t *sr)
1187 {
1188 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1189 	ASSERT(sr->session);
1190 	ASSERT(sr->r_xa == NULL);
1191 
1192 	if (sr->fid_ofile != NULL)
1193 		smb_ofile_release(sr->fid_ofile);
1194 
1195 	if (sr->tid_tree != NULL)
1196 		smb_tree_release(sr->tid_tree);
1197 
1198 	if (sr->uid_user != NULL)
1199 		smb_user_release(sr->uid_user);
1200 
1201 	smb_slist_remove(&sr->session->s_req_list, sr);
1202 
1203 	sr->session = NULL;
1204 
1205 	smb_srm_fini(sr);
1206 
1207 	if (sr->sr_request_buf)
1208 		kmem_free(sr->sr_request_buf, sr->sr_req_length);
1209 	if (sr->command.chain)
1210 		m_freem(sr->command.chain);
1211 	if (sr->reply.chain)
1212 		m_freem(sr->reply.chain);
1213 	if (sr->raw_data.chain)
1214 		m_freem(sr->raw_data.chain);
1215 
1216 	sr->sr_magic = 0;
1217 	mutex_destroy(&sr->sr_mutex);
1218 	kmem_cache_free(sr->sr_cache, sr);
1219 }
1220 
1221 void
1222 dump_smb_inaddr(smb_inaddr_t *ipaddr)
1223 {
1224 	char ipstr[INET6_ADDRSTRLEN];
1225 
1226 	if (smb_inet_ntop(ipaddr, ipstr, SMB_IPSTRLEN(ipaddr->a_family)))
1227 		cmn_err(CE_WARN, "error ipstr=%s", ipstr);
1228 	else
1229 		cmn_err(CE_WARN, "error converting ip address");
1230 }
1231 
1232 boolean_t
1233 smb_session_oplocks_enable(smb_session_t *session)
1234 {
1235 	SMB_SESSION_VALID(session);
1236 	if (session->s_cfg.skc_oplock_enable == 0)
1237 		return (B_FALSE);
1238 	else
1239 		return (B_TRUE);
1240 }
1241 
1242 /*
1243  * smb_session_breaking_oplock
1244  *
1245  * This MUST be a cross-session call, i.e. the caller must be in a different
1246  * context than the one passed. The mutex of the SMB node requiring an oplock
1247  * break MUST be dropped before calling this function. This last requirement is
1248  * due to a potential deadlock that can occur when trying to enter the lock of
1249  * the session passed in.
1250  */
1251 void
1252 smb_session_oplock_break(smb_session_t *session, smb_ofile_t *of)
1253 {
1254 	mbuf_chain_t	*mbc;
1255 
1256 	SMB_SESSION_VALID(session);
1257 
1258 	mbc = smb_mbc_alloc(MLEN);
1259 
1260 	(void) smb_mbc_encodef(mbc, "Mb19.wwwwbb3.ww10.",
1261 	    SMB_COM_LOCKING_ANDX,
1262 	    SMB_TREE_GET_TID(SMB_OFILE_GET_TREE(of)),
1263 	    0xFFFF, 0, 0xFFFF, 8, 0xFF,
1264 	    SMB_OFILE_GET_FID(of),
1265 	    LOCKING_ANDX_OPLOCK_RELEASE);
1266 
1267 	smb_rwx_rwenter(&session->s_lock, RW_WRITER);
1268 	switch (session->s_state) {
1269 	case SMB_SESSION_STATE_NEGOTIATED:
1270 	case SMB_SESSION_STATE_OPLOCK_BREAKING:
1271 		session->s_state = SMB_SESSION_STATE_OPLOCK_BREAKING;
1272 		(void) smb_session_send(session, 0, mbc);
1273 		smb_mbc_free(mbc);
1274 		break;
1275 
1276 	case SMB_SESSION_STATE_READ_RAW_ACTIVE:
1277 		list_insert_tail(&session->s_oplock_brkreqs, mbc);
1278 		break;
1279 
1280 	case SMB_SESSION_STATE_DISCONNECTED:
1281 	case SMB_SESSION_STATE_TERMINATED:
1282 		smb_mbc_free(mbc);
1283 		break;
1284 
1285 	default:
1286 		SMB_PANIC();
1287 	}
1288 	smb_rwx_rwexit(&session->s_lock);
1289 }
1290