xref: /illumos-gate/usr/src/uts/common/fs/smbclnt/netsmb/smb_conn.h (revision 48e11a6ea0245c522078ddb86a73f16c8c28b949)
1 /*
2  * Copyright (c) 2000-2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: smb_conn.h,v 1.32.42.1 2005/05/27 02:35:29 lindak Exp $
33  */
34 
35 /*
36  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  *
39  * Portions Copyright (C) 2001 - 2013 Apple Inc. All rights reserved.
40  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
41  * Copyright 2024 RackTop Systems, Inc.
42  */
43 
44 #ifndef _SMB_CONN_H
45 #define	_SMB_CONN_H
46 
47 #include <sys/dditypes.h>
48 #include <sys/t_lock.h>
49 #include <sys/queue.h> /* for SLIST below */
50 #include <sys/uio.h>
51 #include <netsmb/smb_dev.h>
52 #include <netsmb/nsmb_kcrypt.h>
53 
54 /*
55  * Credentials of user/process for processing in the connection procedures
56  */
57 typedef struct smb_cred {
58 	struct cred *scr_cred;
59 } smb_cred_t;
60 
61 /*
62  * Common object flags
63  */
64 #define	SMBO_GONE		0x1000000
65 
66 /*
67  * Bits in vc_flags (a.k.a. vc_co.co_flags)
68  * Note: SMBO_GONE is also in vc_flags
69  */
70 #define	SMBV_UNICODE		0x0040	/* conn configured to use Unicode */
71 #define	SMBV_EXT_SEC		0x0080	/* conn to use extended security */
72 #define	SMBV_SIGNING		0x0100	/* negotiated signing */
73 #define	SMBV_SMB2		0x0200	/* VC using SMB 2 or 3 */
74 #define	SMBV_HAS_FILEIDS	0x0400	/* Use File IDs for hash and inums */
75 #define	SMBV_NO_WRITE_THRU	0x0800	/* Can't use ... */
76 
77 /*
78  * Note: the common "obj" level uses this GONE flag by
79  * the name SMBO_GONE.  Keep this alias as a reminder.
80  */
81 #define	SMBV_GONE		SMBO_GONE
82 
83 /*
84  * bits in smb_share ss_flags (a.k.a. ss_co.co_flags)
85  */
86 #define	SMBS_RECONNECTING	0x0002
87 #define	SMBS_CONNECTED		0x0004
88 #define	SMBS_TCON_WAIT		0x0008
89 #define	SMBS_FST_FAT		0x0010	/* share FS Type is FAT */
90 /*
91  * Note: the common "obj" level uses this GONE flag by
92  * the name SMBO_GONE.  Keep this alias as a reminder.
93  */
94 #define	SMBS_GONE		SMBO_GONE
95 
96 /*
97  * bits in smb_fh fh_flags (a.k.a. ss_co.co_flags)
98  */
99 #define	SMBFH_VALID		0x0002	/* FID is valid */
100 /*
101  * Note: the common "obj" level uses this GONE flag by
102  * the name SMBO_GONE.  Keep this alias as a reminder.
103  */
104 #define	SMBFH_GONE		SMBO_GONE
105 
106 struct smb_rq;
107 /* This declares struct smb_rqhead */
108 TAILQ_HEAD(smb_rqhead, smb_rq);
109 
110 #define	SMB_NBTIMO	15
111 #define	SMB_DEFRQTIMO	30	/* 30 for oplock revoke/writeback */
112 #define	SMBWRTTIMO	60
113 #define	SMBSSNSETUPTIMO	60
114 #define	SMBNOREPLYWAIT (0)
115 
116 #define	SMB_DIALECT(vcp)	((vcp)->vc_sopt.sv_proto)
117 
118 /*
119  * Connection object
120  */
121 
122 #define	SMB_CO_LOCK(cp)		mutex_enter(&(cp)->co_lock)
123 #define	SMB_CO_UNLOCK(cp)	mutex_exit(&(cp)->co_lock)
124 
125 /*
126  * Common part of smb_vc, smb_share
127  * Locking: co_lock protects most
128  * fields in this struct, except
129  * as noted below:
130  */
131 struct smb_connobj {
132 	kmutex_t		co_lock;
133 	int			co_level;	/* SMBL_ */
134 	int			co_flags;
135 	int			co_usecount;
136 
137 	/* Note: must lock co_parent before child. */
138 	struct smb_connobj	*co_parent;
139 
140 	/* this.co_lock protects the co_children list */
141 	SLIST_HEAD(, smb_connobj) co_children;
142 
143 	/*
144 	 * Linkage in parent's list of children.
145 	 * Must hold parent.co_lock to traverse.
146 	 */
147 	SLIST_ENTRY(smb_connobj) co_next;
148 
149 	/* These two are set only at creation. */
150 	void (*co_gone)(struct smb_connobj *);
151 	void (*co_free)(struct smb_connobj *);
152 };
153 typedef struct smb_connobj smb_connobj_t;
154 
155 /*
156  * "Level" in the connection object hierarchy
157  */
158 enum smbco_level {
159 	SMBL_SM = 0,
160 	SMBL_VC = 1,
161 	SMBL_SHARE = 2,
162 	SMBL_FH = 3
163 };
164 
165 /*
166  * SMB1 Negotiated protocol parameters
167  * Note:  All set to zero at start of nsmb_iod_negotiate
168  */
169 struct smb_sopt {
170 	uint16_t	sv_proto;	/* protocol dialect */
171 	uchar_t		sv_sm;		/* security mode */
172 	int16_t		sv_tz;		/* offset in min relative to UTC */
173 	uint16_t	sv_maxmux;	/* max number of outstanding rq's */
174 	uint16_t	sv_maxvcs;	/* max number of VCs */
175 	uint16_t	sv_rawmode;
176 	uint32_t	sv_maxtx;	/* maximum transmit buf size */
177 	uint32_t	sv_maxraw;	/* maximum raw-buffer size */
178 	uint32_t	sv_skey;	/* session key */
179 	uint32_t	sv_caps;	/* capabilites SMB_CAP_ */
180 
181 	/* SMB2+ fields */
182 	uint32_t	sv2_capabilities;	/* capabilities */
183 	uint32_t	sv2_maxtransact;	/* max transact size */
184 	uint32_t	sv2_maxread;	/* max read size */
185 	uint32_t	sv2_maxwrite;	/* max write size */
186 	uint16_t	sv2_security_mode;	/* security mode */
187 	uint16_t	sv2_sessflags;	/* final session setup reply flags */
188 	uint8_t		sv2_guid[16];	/* GUID */
189 };
190 typedef struct smb_sopt smb_sopt_t;
191 
192 /*
193  * SMB1 I/O Deamon state
194  */
195 struct smb_iods {
196 	uint8_t		is_hflags;	/* SMB header flags */
197 	uint16_t	is_hflags2;	/* SMB header flags2 */
198 	uint16_t	is_smbuid;	/* SMB header UID */
199 	uint16_t	is_next_mid;	/* SMB header MID */
200 	uint32_t	is_txmax;	/* max tx/rx packet size */
201 	uint32_t	is_rwmax;	/* max read/write data size */
202 	uint32_t	is_rxmax;	/* max readx data size */
203 	uint32_t	is_wxmax;	/* max writex data size */
204 	/* Signing state */
205 	uint32_t	is_next_seq;	/* my next sequence number */
206 
207 };
208 typedef struct smb_iods smb_iods_t;
209 
210 /*
211  * Virtual Circuit to a server (really connection + session).
212  * Yes, calling this a "Virtual Circuit" is confusining,
213  * because it has nothing to do with the SMB notion of a
214  * "Virtual Circuit".
215  */
216 typedef struct smb_vc {
217 	struct smb_connobj	vc_co;	/* keep first! See CPTOVC */
218 	enum smbiod_state	vc_state;
219 	kcondvar_t		vc_statechg;
220 
221 	zoneid_t		vc_zoneid;
222 	uid_t			vc_owner;	/* Unix owner */
223 	int			vc_genid;	/* "generation" ID */
224 
225 	int			vc_mackeylen;	/* MAC key length */
226 	int			vc_ssnkeylen;	/* session key length */
227 	uint8_t			*vc_mackey;	/* MAC key buffer */
228 	uint8_t			*vc_ssnkey;	/* session key buffer */
229 	smb_crypto_mech_t	vc_signmech;
230 	struct smb_mac_ops	*vc_sign_ops;
231 
232 	struct smb_tran_desc	*vc_tdesc;	/* transport ops. vector */
233 	void			*vc_tdata;	/* transport control block */
234 
235 	/* SMB2+ fields */
236 	uint64_t	vc2_oldest_message_id;
237 	uint64_t	vc2_next_message_id;
238 	uint64_t	vc2_limit_message_id;
239 	uint64_t	vc2_session_id;		/* session id */
240 	uint64_t	vc2_prev_session_id;	/* for reconnect */
241 	uint32_t	vc2_lease_key;		/* lease key gen */
242 
243 	/* SMB3+ fields */
244 	smb_crypto_mech_t *vc3_crypt_mech;
245 
246 	uint8_t		vc3_encrypt_key[SMB3_KEYLEN];
247 	uint32_t	vc3_encrypt_key_len;
248 
249 	uint8_t		vc3_decrypt_key[SMB3_KEYLEN];
250 	uint32_t	vc3_decrypt_key_len;
251 
252 	/* SMB 3 Nonce used for encryption */
253 	uint64_t	vc3_nonce_high;
254 	uint64_t	vc3_nonce_low;
255 
256 	kcondvar_t		iod_idle;	/* IOD thread idle CV */
257 	krwlock_t		iod_rqlock;	/* iod_rqlist */
258 	struct smb_rqhead	iod_rqlist;	/* list of active reqs */
259 	struct _kthread		*iod_thr;	/* the IOD (reader) thread */
260 	int			iod_flags;	/* see SMBIOD_* below */
261 	uint_t			iod_muxcnt;	/* num. active requests */
262 	uint_t			iod_muxwant;	/* waiting to be active */
263 	kcondvar_t		iod_muxwait;
264 	boolean_t		iod_noresp;	/* Logged "not responding" */
265 
266 	smb_iods_t		vc_iods;
267 	smb_sopt_t		vc_sopt;
268 
269 	/* This is copied in/out when IOD enters/returns */
270 	smbioc_ssn_work_t	vc_work;
271 
272 	/* session identity, etc. */
273 	smbioc_ossn_t		vc_ssn;
274 } smb_vc_t;
275 
276 #define	vc_lock		vc_co.co_lock
277 #define	vc_flags	vc_co.co_flags
278 
279 /* defines for members in vc_ssn */
280 #define	vc_owner	vc_ssn.ssn_owner
281 #define	vc_vopt		vc_ssn.ssn_vopt
282 #define	vc_minver	vc_ssn.ssn_minver
283 #define	vc_maxver	vc_ssn.ssn_maxver
284 #define	vc_srvname	vc_ssn.ssn_srvname
285 #define	vc_srvaddr	vc_ssn.ssn_id.id_srvaddr
286 #define	vc_domain	vc_ssn.ssn_id.id_domain
287 #define	vc_username	vc_ssn.ssn_id.id_user
288 
289 /* defines for members in vc_work */
290 #define	vc_cl_guid	vc_work.wk_cl_guid
291 
292 /* defines for members in vc_sopt ? */
293 #define	vc_maxmux	vc_sopt.sv_maxmux
294 
295 /* defines for members in vc_iods */
296 #define	vc_hflags	vc_iods.is_hflags
297 #define	vc_hflags2	vc_iods.is_hflags2
298 #define	vc_smbuid	vc_iods.is_smbuid
299 #define	vc_next_mid	vc_iods.is_next_mid
300 #define	vc_txmax	vc_iods.is_txmax
301 #define	vc_rwmax	vc_iods.is_rwmax
302 #define	vc_rxmax	vc_iods.is_rxmax
303 #define	vc_wxmax	vc_iods.is_wxmax
304 #define	vc_next_seq	vc_iods.is_next_seq
305 
306 #define	SMB_VC_LOCK(vcp)	mutex_enter(&(vcp)->vc_lock)
307 #define	SMB_VC_UNLOCK(vcp)	mutex_exit(&(vcp)->vc_lock)
308 
309 #define	CPTOVC(cp)	((struct smb_vc *)((void *)(cp)))
310 #define	VCTOCP(vcp)	(&(vcp)->vc_co)
311 
312 #define	SMB_UNICODE_STRINGS(vcp) \
313 	(((vcp)->vc_flags & SMBV_SMB2) != 0 ||	\
314 	((vcp)->vc_hflags2 & SMB_FLAGS2_UNICODE) != 0)
315 
316 /* Bits in iod_flags */
317 #define	SMBIOD_RUNNING		0x0001
318 #define	SMBIOD_SHUTDOWN		0x0002
319 
320 /*
321  * smb_share structure describes connection to the given SMB share (tree).
322  * Connection to share is always built on top of the VC.
323  */
324 
325 typedef struct smb_share {
326 	struct smb_connobj ss_co;	/* keep first! See CPTOSS */
327 	kcondvar_t	ss_conn_done;	/* wait for reconnect */
328 	int		ss_conn_waiters;
329 	int		ss_vcgenid;	/* check VC generation ID */
330 	uint16_t	ss_tid;		/* TID */
331 	uint16_t	ss_options;	/* option support bits */
332 	uint32_t	ss2_tree_id;
333 	uint32_t	ss2_share_flags;
334 	uint32_t	ss2_share_caps;
335 	smbioc_oshare_t ss_ioc;
336 } smb_share_t;
337 
338 #define	ss_lock		ss_co.co_lock
339 #define	ss_flags	ss_co.co_flags
340 
341 #define	ss_use		ss_ioc.sh_use
342 #define	ss_type		ss_ioc.sh_type
343 #define	ss_name		ss_ioc.sh_name
344 #define	ss_pass		ss_ioc.sh_pass
345 
346 #define	SMB_SS_LOCK(ssp)	mutex_enter(&(ssp)->ss_lock)
347 #define	SMB_SS_UNLOCK(ssp)	mutex_exit(&(ssp)->ss_lock)
348 
349 #define	CPTOSS(cp)	((struct smb_share *)((void *)(cp)))
350 #define	SSTOCP(ssp)	(&(ssp)->ss_co)
351 #define	SSTOVC(ssp)	CPTOVC(((ssp)->ss_co.co_parent))
352 
353 typedef struct smb2fid {
354 	uint64_t fid_persistent;
355 	uint64_t fid_volatile;
356 } smb2fid_t;
357 
358 /*
359  * smb_fh struct describes an open file handle under some share.
360  */
361 typedef struct smb_fh {
362 	struct smb_connobj fh_co;	/* keep first! See CPTOSS */
363 	int	fh_vcgenid;		/* check VC generation ID */
364 	uint32_t fh_rights;		/* granted access */
365 	smb2fid_t fh_fid2;
366 	uint16_t fh_fid1;
367 } smb_fh_t;
368 
369 #define	fh_lock		fh_co.co_lock
370 #define	fh_flags	fh_co.co_flags
371 
372 #define	SMB_FH_LOCK(fhp)	mutex_enter(&(fhp)->fh_lock)
373 #define	SMB_FH_UNLOCK(fhp)	mutex_exit(&(fhp)->fh_lock)
374 
375 #define	CPTOFH(cp)	((struct smb_fh *)((void *)(cp)))
376 #define	FHTOCP(fhp)	(&(fhp)->fh_co)
377 #define	FHTOSS(fhp)	CPTOSS(((fhp)->fh_co.co_parent))
378 
379 /*
380  * Call-back operations vector, so the netsmb module
381  * can notify smbfs about events affecting mounts.
382  * Installed in netsmb after smbfs loads.
383  * Note: smbfs only uses the fscb_discon hook.
384  */
385 typedef struct smb_fscb {
386 	/* Called when the VC has disconnected. */
387 	void (*fscb_disconn)(smb_share_t *);
388 	/* Called when the VC has reconnected. */
389 	void (*fscb_connect)(smb_share_t *);
390 } smb_fscb_t;
391 /* Install the above vector, or pass NULL to clear it. */
392 void smb_fscb_set(smb_fscb_t *);
393 
394 /*
395  * The driver per open instance object.
396  * Mostly used in: smb_dev.c, smb_usr.c
397  */
398 typedef struct smb_dev {
399 	kmutex_t	sd_lock;
400 	struct smb_vc	*sd_vc;		/* Reference to VC */
401 	struct smb_share *sd_share;	/* Reference to share if any */
402 	struct smb_fh	*sd_fh;		/* Reference to FH, if any */
403 	int		sd_level;	/* SMBL_VC, ... */
404 	int		sd_vcgenid;	/* Generation of share or VC */
405 	int		sd_poll;	/* Future use */
406 	int		sd_flags;	/* State of connection */
407 #define	NSMBFL_OPEN		0x0001	/* Device minor is open */
408 #define	NSMBFL_IOD		0x0004	/* Open by IOD */
409 #define	NSMBFL_IOCTL		0x0010	/* Serialize ioctl calls */
410 	zoneid_t	zoneid;		/* Zone id */
411 } smb_dev_t;
412 
413 extern const uint32_t nsmb_version;
414 
415 /*
416  * smb_dev.c
417  */
418 int  smb_dev2share(int fd, struct smb_share **sspp);
419 
420 
421 /*
422  * smb_usr.c
423  */
424 int smb_usr_ioctl(smb_dev_t *, int, intptr_t, int, cred_t *);
425 
426 int smb_usr_get_flags2(smb_dev_t *sdp, intptr_t arg, int flags);
427 int smb_usr_get_ssnkey(smb_dev_t *sdp, intptr_t arg, int flags);
428 int smb_usr_dup_dev(smb_dev_t *sdp, intptr_t arg, int flags);
429 
430 int smb_usr_simplerq(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
431 int smb_usr_t2request(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
432 
433 int smb_usr_closefh(smb_dev_t *, cred_t *);
434 int smb_usr_rw(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr);
435 int smb_usr_ntcreate(smb_dev_t *, intptr_t, int, cred_t *);
436 int smb_usr_printjob(smb_dev_t *, intptr_t, int, cred_t *);
437 
438 int smb_usr_get_ssn(smb_dev_t *, int, intptr_t, int, cred_t *);
439 int smb_usr_drop_ssn(smb_dev_t *sdp, int cmd);
440 
441 int smb_usr_get_tree(smb_dev_t *, int, intptr_t, int, cred_t *);
442 int smb_usr_drop_tree(smb_dev_t *sdp, int cmd);
443 
444 int smb_usr_iod_work(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
445 int smb_usr_iod_ioctl(smb_dev_t *sdp, int cmd, intptr_t arg, int flags,
446     cred_t *cr);
447 
448 int smb_pkey_ioctl(int, intptr_t, int, cred_t *);
449 
450 
451 /*
452  * IOD functions
453  */
454 int  smb_iod_create(smb_vc_t *vcp);
455 int  smb_iod_destroy(smb_vc_t *vcp);
456 void smb_iod_disconnect(smb_vc_t *vcp);
457 int  smb2_iod_addrq(struct smb_rq *rqp);
458 int  smb1_iod_addrq(struct smb_rq *rqp);
459 int  smb1_iod_multirq(struct smb_rq *rqp);
460 int  smb_iod_waitrq(struct smb_rq *rqp);
461 int  smb_iod_waitrq_int(struct smb_rq *rqp);
462 void smb_iod_removerq(struct smb_rq *rqp);
463 int  smb_iod_sendrecv(struct smb_rq *, int);
464 void smb_iod_shutdown_share(smb_share_t *ssp);
465 
466 void smb_iod_sendall(smb_vc_t *);
467 int smb_iod_recvall(smb_vc_t *, boolean_t);
468 
469 int nsmb_iod_connect(smb_vc_t *vcp, cred_t *cr);
470 int nsmb_iod_negotiate(smb_vc_t *vcp, cred_t *cr);
471 int nsmb_iod_ssnsetup(smb_vc_t *vcp, cred_t *cr);
472 int smb_iod_vc_work(smb_vc_t *, int, cred_t *);
473 int smb_iod_vc_idle(smb_vc_t *);
474 int smb_iod_vc_rcfail(smb_vc_t *);
475 int smb_iod_reconnect(smb_vc_t *);
476 
477 /*
478  * Session level functions
479  */
480 int  smb_sm_init(void);
481 int  smb_sm_idle(void);
482 void smb_sm_done(void);
483 
484 /*
485  * VC level functions
486  */
487 void smb_vc_hold(smb_vc_t *vcp);
488 void smb_vc_rele(smb_vc_t *vcp);
489 void smb_vc_kill(smb_vc_t *vcp);
490 
491 int smb_vc_findcreate(smbioc_ossn_t *, smb_cred_t *, smb_vc_t **);
492 int smb_vc_create(smbioc_ossn_t *ossn, smb_cred_t *scred, smb_vc_t **vcpp);
493 
494 const char *smb_vc_getpass(smb_vc_t *vcp);
495 uint16_t smb_vc_nextmid(smb_vc_t *vcp);
496 void *smb_vc_getipaddr(smb_vc_t *vcp, int *ipvers);
497 
498 typedef void (*walk_share_func_t)(smb_share_t *);
499 void smb_vc_walkshares(struct smb_vc *,	walk_share_func_t);
500 
501 /*
502  * share level functions
503  */
504 
505 int smb_share_findcreate(smbioc_tcon_t *, smb_vc_t *,
506 	smb_share_t **, smb_cred_t *);
507 
508 void smb_share_hold(smb_share_t *ssp);
509 void smb_share_rele(smb_share_t *ssp);
510 void smb_share_kill(smb_share_t *ssp);
511 
512 void smb_share_invalidate(smb_share_t *ssp);
513 int  smb_share_tcon(smb_share_t *, smb_cred_t *);
514 
515 /*
516  * File handle level functions
517  */
518 int smb_fh_create(smb_share_t *ssp, struct smb_fh **fhpp);
519 void smb_fh_opened(struct smb_fh *fhp);
520 void smb_fh_close(struct smb_fh *fhp);
521 void smb_fh_hold(struct smb_fh *fhp);
522 void smb_fh_rele(struct smb_fh *fhp);
523 
524 #endif /* _SMB_CONN_H */
525