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 
40 #ifndef _SMB_CONN_H
41 #define	_SMB_CONN_H
42 
43 #include <sys/dditypes.h>
44 #include <sys/t_lock.h>
45 #include <sys/queue.h> /* for SLIST below */
46 #include <sys/uio.h>
47 #include <netsmb/smb_dev.h>
48 
49 /*
50  * Credentials of user/process for processing in the connection procedures
51  */
52 typedef struct smb_cred {
53 	struct cred *scr_cred;
54 } smb_cred_t;
55 
56 /*
57  * Common object flags
58  */
59 #define	SMBO_GONE		0x1000000
60 
61 /*
62  * Bits in vc_flags (a.k.a. vc_co.co_flags)
63  * Many of these were duplicates of SMBVOPT_ flags
64  * and we now keep those too instead of merging
65  * them into vc_flags.
66  */
67 
68 #define	SMBV_WIN95		0x0010	/* used to apply bugfixes for this OS */
69 #define	SMBV_NT4		0x0020	/* used when NT4 issues invalid resp */
70 #define	SMBV_UNICODE		0x0040	/* conn configured to use Unicode */
71 
72 /*
73  * Note: the common "obj" level uses this GONE flag by
74  * the name SMBO_GONE.  Keep this alias as a reminder.
75  */
76 #define	SMBV_GONE		SMBO_GONE
77 
78 /*
79  * bits in smb_share ss_flags (a.k.a. ss_co.co_flags)
80  */
81 #define	SMBS_RECONNECTING	0x0002
82 #define	SMBS_CONNECTED		0x0004
83 #define	SMBS_TCON_WAIT		0x0008
84 #define	SMBS_1980		0x0010
85 /*
86  * ^ This partition can't handle dates before 1980. It's probably a FAT
87  * partition but could be some other ancient FS type
88  */
89 #define	SMBS_RESUMEKEYS		0x0020	/* must use resume keys */
90 #define	SMBS_LONGNAMES		0x0040	/* share can use long names */
91 /*
92  * Note: the common "obj" level uses this GONE flag by
93  * the name SMBO_GONE.  Keep this alias as a reminder.
94  */
95 #define	SMBS_GONE		SMBO_GONE
96 
97 struct smb_rq;
98 /* This declares struct smb_rqhead */
99 TAILQ_HEAD(smb_rqhead, smb_rq);
100 
101 #define	SMB_NBTIMO	15
102 #define	SMB_DEFRQTIMO	30	/* 30 for oplock revoke/writeback */
103 #define	SMBWRTTIMO	60
104 #define	SMBSSNSETUPTIMO	60
105 #define	SMBNOREPLYWAIT (0)
106 
107 #define	SMB_DIALECT(vcp)	((vcp)->vc_sopt.sv_proto)
108 
109 /*
110  * Connection object
111  */
112 
113 #define	SMB_CO_LOCK(cp)		mutex_enter(&(cp)->co_lock)
114 #define	SMB_CO_UNLOCK(cp)	mutex_exit(&(cp)->co_lock)
115 
116 /*
117  * Common part of smb_vc, smb_share
118  * Locking: co_lock protects most
119  * fields in this struct, except
120  * as noted below:
121  */
122 struct smb_connobj {
123 	kmutex_t		co_lock;
124 	int			co_level;	/* SMBL_ */
125 	int			co_flags;
126 	int			co_usecount;
127 
128 	/* Note: must lock co_parent before child. */
129 	struct smb_connobj	*co_parent;
130 
131 	/* this.co_lock protects the co_children list */
132 	SLIST_HEAD(, smb_connobj) co_children;
133 
134 	/*
135 	 * Linkage in parent's list of children.
136 	 * Must hold parent.co_lock to traverse.
137 	 */
138 	SLIST_ENTRY(smb_connobj) co_next;
139 
140 	/* These two are set only at creation. */
141 	void (*co_gone)(struct smb_connobj *);
142 	void (*co_free)(struct smb_connobj *);
143 };
144 typedef struct smb_connobj smb_connobj_t;
145 
146 /*
147  * "Level" in the connection object hierarchy
148  */
149 #define	SMBL_SM		0
150 #define	SMBL_VC		1
151 #define	SMBL_SHARE	2
152 
153 /*
154  * Virtual Circuit to a server (really connection + session).
155  * Yes, calling this a "Virtual Circuit" is confusining,
156  * because it has nothing to do with the SMB notion of a
157  * "Virtual Circuit".
158  */
159 typedef struct smb_vc {
160 	struct smb_connobj	vc_co;	/* keep first! See CPTOVC */
161 	enum smbiod_state	vc_state;
162 	kcondvar_t		vc_statechg;
163 
164 	zoneid_t		vc_zoneid;
165 	uid_t			vc_owner;	/* Unix owner */
166 	int			vc_genid;	/* "generation" ID */
167 
168 	int			vc_mackeylen;	/* length of MAC key */
169 	uint8_t			*vc_mackey;	/* MAC key */
170 
171 	ksema_t			vc_sendlock;
172 	struct smb_tran_desc	*vc_tdesc;	/* transport ops. vector */
173 	void			*vc_tdata;	/* transport control block */
174 
175 	kcondvar_t		iod_idle; 	/* IOD thread idle CV */
176 	krwlock_t		iod_rqlock;	/* iod_rqlist */
177 	struct smb_rqhead	iod_rqlist;	/* list of outstanding reqs */
178 	struct _kthread 	*iod_thr;	/* the IOD (reader) thread */
179 	int			iod_flags;	/* see SMBIOD_* below */
180 	int			iod_newrq;	/* send needed (iod_rqlock) */
181 	int			iod_muxfull;	/* maxmux limit reached */
182 
183 	/* This is copied in/out when IOD enters/returns */
184 	smbioc_ssn_work_t	vc_work;
185 
186 	/* session identity, etc. */
187 	smbioc_ossn_t		vc_ssn;
188 } smb_vc_t;
189 
190 #define	vc_lock		vc_co.co_lock
191 #define	vc_flags	vc_co.co_flags
192 
193 /* defines for members in vc_ssn */
194 #define	vc_owner	vc_ssn.ssn_owner
195 #define	vc_srvname	vc_ssn.ssn_srvname
196 #define	vc_srvaddr	vc_ssn.ssn_id.id_srvaddr
197 #define	vc_domain	vc_ssn.ssn_id.id_domain
198 #define	vc_username	vc_ssn.ssn_id.id_user
199 #define	vc_vopt 	vc_ssn.ssn_vopt
200 
201 /* defines for members in vc_work */
202 #define	vc_sopt		vc_work.wk_sopt
203 #define	vc_maxmux	vc_work.wk_sopt.sv_maxmux
204 #define	vc_tran_fd	vc_work.wk_iods.is_tran_fd
205 #define	vc_hflags	vc_work.wk_iods.is_hflags
206 #define	vc_hflags2	vc_work.wk_iods.is_hflags2
207 #define	vc_smbuid	vc_work.wk_iods.is_smbuid
208 #define	vc_next_mid	vc_work.wk_iods.is_next_mid
209 #define	vc_txmax	vc_work.wk_iods.is_txmax
210 #define	vc_rwmax	vc_work.wk_iods.is_rwmax
211 #define	vc_rxmax	vc_work.wk_iods.is_rxmax
212 #define	vc_wxmax	vc_work.wk_iods.is_wxmax
213 #define	vc_ssn_key	vc_work.wk_iods.is_ssn_key
214 #define	vc_next_seq	vc_work.wk_iods.is_next_seq
215 #define	vc_u_mackey	vc_work.wk_iods.is_u_mackey
216 #define	vc_u_maclen	vc_work.wk_iods.is_u_maclen
217 
218 #define	SMB_VC_LOCK(vcp)	mutex_enter(&(vcp)->vc_lock)
219 #define	SMB_VC_UNLOCK(vcp)	mutex_exit(&(vcp)->vc_lock)
220 
221 #define	SMB_UNICODE_STRINGS(vcp)	((vcp)->vc_hflags2 & SMB_FLAGS2_UNICODE)
222 
223 /* Bits in iod_flags */
224 #define	SMBIOD_RUNNING		0x0001
225 #define	SMBIOD_SHUTDOWN		0x0002
226 
227 /*
228  * smb_share structure describes connection to the given SMB share (tree).
229  * Connection to share is always built on top of the VC.
230  */
231 
232 typedef struct smb_share {
233 	struct smb_connobj ss_co;	/* keep first! See CPTOSS */
234 	kcondvar_t	ss_conn_done;	/* wait for reconnect */
235 	int		ss_conn_waiters;
236 	int		ss_vcgenid;	/* check VC generation ID */
237 	uint16_t	ss_tid;		/* TID */
238 	uint16_t	ss_options;	/* option support bits */
239 	smbioc_oshare_t ss_ioc;
240 } smb_share_t;
241 
242 #define	ss_lock		ss_co.co_lock
243 #define	ss_flags	ss_co.co_flags
244 
245 #define	ss_name		ss_ioc.sh_name
246 #define	ss_pwlen	ss_ioc.sh_pwlen
247 #define	ss_pass		ss_ioc.sh_pass
248 #define	ss_type_req	ss_ioc.sh_type_req
249 #define	ss_type_ret	ss_ioc.sh_type_ret
250 
251 #define	SMB_SS_LOCK(ssp)	mutex_enter(&(ssp)->ss_lock)
252 #define	SMB_SS_UNLOCK(ssp)	mutex_exit(&(ssp)->ss_lock)
253 
254 #define	CPTOVC(cp)	((struct smb_vc *)((void *)(cp)))
255 #define	VCTOCP(vcp)	(&(vcp)->vc_co)
256 
257 #define	CPTOSS(cp)	((struct smb_share *)((void *)(cp)))
258 #define	SSTOVC(ssp)	CPTOVC(((ssp)->ss_co.co_parent))
259 #define	SSTOCP(ssp)	(&(ssp)->ss_co)
260 
261 /*
262  * Call-back operations vector, so the netsmb module
263  * can notify smbfs about events affecting mounts.
264  * Installed in netsmb after smbfs loads.
265  */
266 typedef struct smb_fscb {
267 	/* Called when the VC has disconnected. */
268 	void (*fscb_disconn)(smb_share_t *);
269 	/* Called when the VC has reconnected. */
270 	void (*fscb_connect)(smb_share_t *);
271 	/* Called when the server becomes unresponsive. */
272 	void (*fscb_down)(smb_share_t *);
273 	/* Called when the server is responding again. */
274 	void (*fscb_up)(smb_share_t *);
275 } smb_fscb_t;
276 /* Install the above vector, or pass NULL to clear it. */
277 int smb_fscb_set(smb_fscb_t *);
278 
279 /*
280  * The driver per open instance object.
281  * Mostly used in: smb_dev.c, smb_usr.c
282  */
283 typedef struct smb_dev {
284 	int		sd_opened;	/* Opened or not */
285 	int		sd_level;	/* Future use */
286 	struct smb_vc	*sd_vc;		/* Reference to VC */
287 	struct smb_share *sd_share;	/* Reference to share if any */
288 	int		sd_vcgenid;	/* Generation of share or VC */
289 	int		sd_poll;	/* Future use */
290 	int		sd_seq;		/* Kind of minor number/instance no */
291 	int		sd_flags;	/* State of connection */
292 #define	NSMBFL_OPEN		0x0001
293 #define	NSMBFL_IOD		0x0002
294 	zoneid_t	zoneid;		/* Zone id */
295 	dev_info_t	*smb_dip;	/* ptr to dev_info node */
296 	void		*sd_devfs;	/* Dont know how to use this. but */
297 	struct cred	*smb_cred;	/* per dev credentails. Future use */
298 } smb_dev_t;
299 
300 extern const uint32_t nsmb_version;
301 
302 /*
303  * smb_dev.c
304  */
305 int  smb_dev2share(int fd, struct smb_share **sspp);
306 
307 
308 /*
309  * smb_usr.c
310  */
311 int smb_usr_get_flags2(smb_dev_t *sdp, intptr_t arg, int flags);
312 int smb_usr_get_ssnkey(smb_dev_t *sdp, intptr_t arg, int flags);
313 
314 int smb_usr_simplerq(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
315 int smb_usr_t2request(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
316 int smb_usr_rw(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr);
317 
318 int smb_usr_get_ssn(smb_dev_t *, int, intptr_t, int, cred_t *);
319 int smb_usr_drop_ssn(smb_dev_t *sdp, int cmd);
320 
321 int smb_usr_get_tree(smb_dev_t *, int, intptr_t, int, cred_t *);
322 int smb_usr_drop_tree(smb_dev_t *sdp, int cmd);
323 
324 int smb_usr_iod_work(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr);
325 int smb_usr_iod_ioctl(smb_dev_t *sdp, int cmd, intptr_t arg, int flags);
326 
327 
328 /*
329  * IOD functions
330  */
331 int  smb_iod_create(smb_vc_t *vcp);
332 int  smb_iod_destroy(smb_vc_t *vcp);
333 int  smb_iod_connect(smb_vc_t *vcp);
334 int  smb_iod_disconnect(smb_vc_t *vcp);
335 int  smb_iod_addrq(struct smb_rq *rqp);
336 int  smb_iod_multirq(struct smb_rq *rqp);
337 int  smb_iod_waitrq(struct smb_rq *rqp);
338 int  smb_iod_removerq(struct smb_rq *rqp);
339 void smb_iod_shutdown_share(smb_share_t *ssp);
340 
341 void smb_iod_sendall(smb_vc_t *);
342 int smb_iod_recvall(smb_vc_t *);
343 
344 int smb_iod_vc_work(smb_vc_t *, cred_t *);
345 int smb_iod_vc_idle(smb_vc_t *);
346 int smb_iod_vc_rcfail(smb_vc_t *);
347 int smb_iod_reconnect(smb_vc_t *);
348 
349 /*
350  * Session level functions
351  */
352 int  smb_sm_init(void);
353 int  smb_sm_idle(void);
354 void smb_sm_done(void);
355 
356 /*
357  * VC level functions
358  */
359 void smb_vc_hold(smb_vc_t *vcp);
360 void smb_vc_rele(smb_vc_t *vcp);
361 void smb_vc_kill(smb_vc_t *vcp);
362 
363 int smb_vc_findcreate(smbioc_ossn_t *, smb_cred_t *, smb_vc_t **);
364 int smb_vc_create(smbioc_ossn_t *ossn, smb_cred_t *scred, smb_vc_t **vcpp);
365 
366 const char *smb_vc_getpass(smb_vc_t *vcp);
367 uint16_t smb_vc_nextmid(smb_vc_t *vcp);
368 void *smb_vc_getipaddr(smb_vc_t *vcp, int *ipvers);
369 
370 typedef void (*walk_share_func_t)(smb_share_t *);
371 void smb_vc_walkshares(struct smb_vc *,	walk_share_func_t);
372 
373 /*
374  * share level functions
375  */
376 
377 int smb_share_findcreate(smbioc_tcon_t *, smb_vc_t *,
378 	smb_share_t **, smb_cred_t *);
379 
380 void smb_share_hold(smb_share_t *ssp);
381 void smb_share_rele(smb_share_t *ssp);
382 void smb_share_kill(smb_share_t *ssp);
383 
384 void smb_share_invalidate(smb_share_t *ssp);
385 int  smb_share_tcon(smb_share_t *, smb_cred_t *);
386 
387 /*
388  * SMB protocol level functions
389  */
390 int  smb_smb_echo(smb_vc_t *vcp, smb_cred_t *scred, int timo);
391 int  smb_smb_treeconnect(smb_share_t *ssp, smb_cred_t *scred);
392 int  smb_smb_treedisconnect(smb_share_t *ssp, smb_cred_t *scred);
393 
394 int smb_rwuio(smb_share_t *ssp, uint16_t fid, uio_rw_t rw,
395 	uio_t *uiop, smb_cred_t *scred, int timo);
396 
397 #endif /* _SMB_CONN_H */
398