xref: /freebsd/sys/netsmb/smb_conn.h (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Two levels of connection hierarchy
31  */
32 #define	SMBL_SM		0
33 #define SMBL_VC		1
34 #define SMBL_SHARE	2
35 #define SMBL_NUM	3
36 #define SMBL_NONE	(-1)
37 
38 #define	SMB_CS_NONE	0x0000
39 #define	SMB_CS_UPPER	0x0001		/* convert passed string to upper case */
40 #define	SMB_CS_LOWER	0x0002		/* convert passed string to lower case */
41 
42 /*
43  * Common object flags
44  */
45 #define SMBO_GONE		0x1000000
46 
47 /*
48  * access modes
49  */
50 #define	SMBM_READ		0400	/* read conn attrs.(like list shares) */
51 #define	SMBM_WRITE		0200	/* modify conn attrs */
52 #define	SMBM_EXEC		0100	/* can send SMB requests */
53 #define	SMBM_READGRP		0040
54 #define	SMBM_WRITEGRP		0020
55 #define	SMBM_EXECGRP		0010
56 #define	SMBM_READOTH		0004
57 #define	SMBM_WRITEOTH		0002
58 #define	SMBM_EXECOTH		0001
59 #define	SMBM_MASK		0777
60 #define	SMBM_EXACT		010000	/* check for specified mode exactly */
61 #define	SMBM_ALL		(SMBM_READ | SMBM_WRITE | SMBM_EXEC)
62 #define	SMBM_DEFAULT		(SMBM_READ | SMBM_WRITE | SMBM_EXEC)
63 #define	SMBM_ANY_OWNER		((uid_t)-1)
64 #define	SMBM_ANY_GROUP		((gid_t)-1)
65 
66 /*
67  * VC flags
68  */
69 #define SMBV_PERMANENT		0x0002
70 #define SMBV_LONGNAMES		0x0004	/* connection is configured to use long names */
71 #define	SMBV_ENCRYPT		0x0008	/* server asked for encrypted password */
72 #define	SMBV_WIN95		0x0010	/* used to apply bugfixes for this OS */
73 #define	SMBV_PRIVATE		0x0020	/* connection can be used only by creator */
74 #define	SMBV_RECONNECTING	0x0040	/* conn is in the process of reconnection */
75 #define SMBV_SINGLESHARE	0x0080	/* only one share connecting should be allowed */
76 #define SMBV_CREATE		0x0100	/* lookup for create operation */
77 /*#define SMBV_FAILED		0x0200*/	/* last reconnect attempt has failed */
78 #define SMBV_UNICODE		0x0400	/* connection is configured to use Unicode */
79 
80 
81 /*
82  * smb_share flags
83  */
84 #define SMBS_PERMANENT		0x0001
85 #define SMBS_RECONNECTING	0x0002
86 #define SMBS_CONNECTED		0x0004
87 
88 /*
89  * share types
90  */
91 #define	SMB_ST_DISK		0x0	/* A: */
92 #define	SMB_ST_PRINTER		0x1	/* LPT: */
93 #define	SMB_ST_PIPE		0x2	/* IPC */
94 #define	SMB_ST_COMM		0x3	/* COMM */
95 #define	SMB_ST_ANY		0x4
96 #define	SMB_ST_MAX		0x4
97 #define SMB_ST_NONE		0xff	/* not a part of protocol */
98 
99 /*
100  * Negotiated protocol parameters
101  */
102 struct smb_sopt {
103 	int		sv_proto;
104 	int16_t		sv_tz;		/* offset in min relative to UTC */
105 	u_int32_t	sv_maxtx;	/* maximum transmit buf size */
106 	u_char		sv_sm;		/* security mode */
107 	u_int16_t	sv_maxmux;	/* max number of outstanding rq's */
108 	u_int16_t 	sv_maxvcs;	/* max number of VCs */
109 	u_int16_t	sv_rawmode;
110 	u_int32_t	sv_maxraw;	/* maximum raw-buffer size */
111 	u_int32_t	sv_skey;	/* session key */
112 	u_int32_t	sv_caps;	/* capabilities SMB_CAP_ */
113 };
114 
115 /*
116  * network IO daemon states
117  */
118 enum smbiod_state {
119 	SMBIOD_ST_NOTCONN,	/* no connect request was made */
120 	SMBIOD_ST_RECONNECT,	/* a [re]connect attempt is in progress */
121 	SMBIOD_ST_TRANACTIVE,	/* transport level is up */
122 	SMBIOD_ST_VCACTIVE,	/* session established */
123 	SMBIOD_ST_DEAD		/* connection broken, transport is down */
124 };
125 
126 
127 /*
128  * Info structures
129  */
130 #define	SMB_INFO_NONE		0
131 #define	SMB_INFO_VC		2
132 #define	SMB_INFO_SHARE		3
133 
134 struct smb_vc_info {
135 	int		itype;
136 	int		usecount;
137 	uid_t		uid;		/* user id of connection */
138 	gid_t		gid;		/* group of connection */
139 	mode_t		mode;		/* access mode */
140 	int		flags;
141 	enum smbiod_state iodstate;
142 	struct smb_sopt	sopt;
143 	char		srvname[SMB_MAXSRVNAMELEN + 1];
144 	char		vcname[128];
145 };
146 
147 struct smb_share_info {
148 	int		itype;
149 	int		usecount;
150 	u_short		tid;		/* TID */
151 	int		type;		/* share type */
152 	uid_t		uid;		/* user id of connection */
153 	gid_t		gid;		/* group of connection */
154 	mode_t		mode;		/* access mode */
155 	int		flags;
156 	char		sname[128];
157 };
158 
159 #ifdef _KERNEL
160 
161 #include <sys/lock.h>
162 #include <sys/lockmgr.h>
163 #include <netsmb/smb_subr.h>
164 
165 #define CONNADDREQ(a1,a2)	((a1)->sa_len == (a2)->sa_len && \
166 				 bcmp(a1, a2, (a1)->sa_len) == 0)
167 
168 struct smb_vc;
169 struct smb_share;
170 struct smb_cred;
171 struct smb_rq;
172 struct mbdata;
173 struct smbioc_oshare;
174 struct smbioc_ossn;
175 struct uio;
176 
177 TAILQ_HEAD(smb_rqhead, smb_rq);
178 
179 #define SMB_DEFRQTIMO	5
180 
181 #define SMB_DIALECT(vcp)	((vcp)->vc_sopt.sv_proto)
182 
183 struct smb_tran_desc;
184 
185 /*
186  * Connection object
187  */
188 struct smb_connobj;
189 
190 typedef void smb_co_gone_t (struct smb_connobj *cp, struct smb_cred *scred);
191 typedef void smb_co_free_t (struct smb_connobj *cp);
192 
193 #define	SMB_CO_LOCK(cp)		smb_sl_lock(&(cp)->co_interlock)
194 #define	SMB_CO_UNLOCK(cp)	smb_sl_unlock(&(cp)->co_interlock)
195 
196 struct smb_connobj {
197 	int			co_level;	/* SMBL_ */
198 	int			co_flags;
199 	struct lock		co_lock;
200 	struct smb_slock	co_interlock;
201 	int			co_usecount;
202 	struct smb_connobj *	co_parent;
203 	SLIST_HEAD(,smb_connobj)co_children;
204 	SLIST_ENTRY(smb_connobj)co_next;
205 	smb_co_gone_t *		co_gone;
206 	smb_co_free_t *		co_free;
207 };
208 
209 #define	SMBCO_FOREACH(var, cp)	SLIST_FOREACH((var), &(cp)->co_children, co_next)
210 
211 /*
212  * Virtual Circuit (session) to a server.
213  * This is the most (over)complicated part of SMB protocol.
214  * For the user security level (usl), each session with different remote
215  * user name has its own VC.
216  * It is unclear however, should share security level (ssl) allow additional
217  * VCs, because user name is not used and can be the same. On other hand,
218  * multiple VCs allows us to create separate sessions to server on a per
219  * user basis.
220  */
221 
222 /*
223  * This lock protects vc_flags
224  */
225 #define	SMBC_ST_LOCK(vcp)	smb_sl_lock(&(vcp)->vc_stlock)
226 #define	SMBC_ST_UNLOCK(vcp)	smb_sl_unlock(&(vcp)->vc_stlock)
227 
228 
229 struct smb_vc {
230 	struct smb_connobj obj;
231 	char *		vc_srvname;
232 	struct sockaddr*vc_paddr;	/* server addr */
233 	struct sockaddr*vc_laddr;	/* local addr, if any */
234 	char *		vc_username;
235 	char *		vc_pass;	/* password for usl case */
236 	char *		vc_domain;	/* workgroup/primary domain */
237 
238 	u_int		vc_timo;	/* default request timeout */
239 	int		vc_maxvcs;	/* maximum number of VC per connection */
240 
241 	void *		vc_tolower;	/* local charset */
242 	void *		vc_toupper;	/* local charset */
243 	void *		vc_toserver;	/* local charset to server one */
244 	void *		vc_tolocal;	/* server charset to local one */
245 	int		vc_number;	/* number of this VC from the client side */
246 	int		vc_genid;
247 	uid_t		vc_uid;		/* user id of connection */
248 	gid_t		vc_grp;		/* group of connection */
249 	mode_t		vc_mode;	/* access mode */
250 	u_short		vc_smbuid;	/* unique vc id assigned by server */
251 
252 	u_char		vc_hflags;	/* or'ed with flags in the smb header */
253 	u_short		vc_hflags2;	/* or'ed with flags in the smb header */
254 	void *		vc_tdata;	/* transport control block */
255 	struct smb_tran_desc *vc_tdesc;
256 	int		vc_chlen;	/* actual challenge length */
257 	u_char 		vc_ch[SMB_MAXCHALLENGELEN];
258 	u_short		vc_mid;		/* multiplex id */
259 	struct smb_sopt	vc_sopt;	/* server options */
260 	int		vc_txmax;	/* max tx/rx packet size */
261 	int		vc_rxmax;	/* max readx data size */
262 	int		vc_wxmax;	/* max writex data size */
263 	struct smbiod *	vc_iod;
264 	struct smb_slock vc_stlock;
265 	u_int32_t	vc_seqno;	/* my next sequence number */
266 	u_int8_t	*vc_mackey;	/* MAC key */
267 	int		vc_mackeylen;	/* length of MAC key */
268 };
269 
270 #define vc_maxmux	vc_sopt.sv_maxmux
271 #define	vc_flags	obj.co_flags
272 
273 #define SMB_UNICODE_STRINGS(vcp)	((vcp)->vc_hflags2 & SMB_FLAGS2_UNICODE)
274 
275 /*
276  * smb_share structure describes connection to the given SMB share (tree).
277  * Connection to share is always built on top of the VC.
278  */
279 
280 /*
281  * This lock protects ss_flags
282  */
283 #define	SMBS_ST_LOCK(ssp)	smb_sl_lock(&(ssp)->ss_stlock)
284 #define	SMBS_ST_LOCKPTR(ssp)	(&(ssp)->ss_stlock)
285 #define	SMBS_ST_UNLOCK(ssp)	smb_sl_unlock(&(ssp)->ss_stlock)
286 
287 struct smb_share {
288 	struct smb_connobj obj;
289 	char *		ss_name;
290 	u_short		ss_tid;		/* TID */
291 	int		ss_type;	/* share type */
292 	uid_t		ss_uid;		/* user id of connection */
293 	gid_t		ss_grp;		/* group of connection */
294 	mode_t		ss_mode;	/* access mode */
295 	int		ss_vcgenid;
296 	char *		ss_pass;	/* password to a share, can be null */
297 	struct smb_slock ss_stlock;
298 };
299 
300 #define	ss_flags	obj.co_flags
301 
302 #define CPTOVC(cp)	((struct smb_vc*)(cp))
303 #define VCTOCP(vcp)	(&(vcp)->obj)
304 #define CPTOSS(cp)	((struct smb_share*)(cp))
305 #define	SSTOVC(ssp)	CPTOVC(((ssp)->obj.co_parent))
306 #define SSTOCP(ssp)	(&(ssp)->obj)
307 
308 struct smb_vcspec {
309 	char *		srvname;
310 	struct sockaddr*sap;
311 	struct sockaddr*lap;
312 	int		flags;
313 	char *		username;
314 	char *		pass;
315 	char *		domain;
316 	mode_t		mode;
317 	mode_t		rights;
318 	uid_t		owner;
319 	gid_t		group;
320 	char *		localcs;
321 	char *		servercs;
322 	struct smb_sharespec *shspec;
323 	struct smb_share *ssp;		/* returned */
324 	/*
325 	 * The rest is an internal data
326 	 */
327 	struct smb_cred *scred;
328 };
329 
330 struct smb_sharespec {
331 	char *		name;
332 	char *		pass;
333 	mode_t		mode;
334 	mode_t		rights;
335 	uid_t		owner;
336 	gid_t		group;
337 	int		stype;
338 	/*
339 	 * The rest is an internal data
340 	 */
341 	struct smb_cred *scred;
342 };
343 
344 /*
345  * Session level functions
346  */
347 int  smb_sm_init(void);
348 int  smb_sm_done(void);
349 int  smb_sm_lookup(struct smb_vcspec *vcspec,
350 	struct smb_sharespec *shspec, struct smb_cred *scred,
351 	struct smb_vc **vcpp);
352 
353 /*
354  * Connection object
355  */
356 void smb_co_ref(struct smb_connobj *cp);
357 void smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred);
358 int  smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred);
359 void smb_co_put(struct smb_connobj *cp, struct smb_cred *scred);
360 int  smb_co_lock(struct smb_connobj *cp, int flags);
361 void smb_co_unlock(struct smb_connobj *cp, int flags);
362 
363 /*
364  * session level functions
365  */
366 int  smb_vc_create(struct smb_vcspec *vcspec,
367 	struct smb_cred *scred, struct smb_vc **vcpp);
368 int  smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred);
369 int  smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode);
370 int  smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred);
371 void smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred);
372 void smb_vc_ref(struct smb_vc *vcp);
373 void smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred);
374 int  smb_vc_lock(struct smb_vc *vcp, int flags);
375 void smb_vc_unlock(struct smb_vc *vcp, int flags);
376 int  smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *shspec,
377 	struct smb_cred *scred, struct smb_share **sspp);
378 const char * smb_vc_getpass(struct smb_vc *vcp);
379 u_short smb_vc_nextmid(struct smb_vc *vcp);
380 
381 /*
382  * share level functions
383  */
384 int  smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
385 	struct smb_cred *scred, struct smb_share **sspp);
386 int  smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode);
387 void smb_share_ref(struct smb_share *ssp);
388 void smb_share_rele(struct smb_share *ssp, struct smb_cred *scred);
389 int  smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred);
390 void smb_share_put(struct smb_share *ssp, struct smb_cred *scred);
391 int  smb_share_lock(struct smb_share *ssp, int flags);
392 void smb_share_unlock(struct smb_share *ssp, int flags);
393 void smb_share_invalidate(struct smb_share *ssp);
394 int  smb_share_valid(struct smb_share *ssp);
395 const char * smb_share_getpass(struct smb_share *ssp);
396 
397 /*
398  * SMB protocol level functions
399  */
400 int  smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred);
401 int  smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred);
402 int  smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred);
403 int  smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred);
404 int  smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred);
405 int  smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
406 	struct smb_cred *scred);
407 int  smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
408 	struct smb_cred *scred);
409 int  smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred);
410 
411 /*
412  * smbiod thread
413  */
414 
415 #define	SMBIOD_EV_NEWRQ		0x0001
416 #define	SMBIOD_EV_SHUTDOWN	0x0002
417 #define	SMBIOD_EV_CONNECT	0x0003
418 #define	SMBIOD_EV_DISCONNECT	0x0004
419 #define	SMBIOD_EV_TREECONNECT	0x0005
420 #define	SMBIOD_EV_MASK		0x00ff
421 #define	SMBIOD_EV_SYNC		0x0100
422 #define	SMBIOD_EV_PROCESSING	0x0200
423 
424 struct smbiod_event {
425 	int	ev_type;
426 	int	ev_error;
427 	void *	ev_ident;
428 	STAILQ_ENTRY(smbiod_event)	ev_link;
429 };
430 
431 #define	SMBIOD_SHUTDOWN		0x0001
432 
433 struct smbiod {
434 	int			iod_id;
435 	int			iod_flags;
436 	enum smbiod_state	iod_state;
437 	int			iod_muxcnt;	/* number of active outstanding requests */
438 	int			iod_sleeptimo;
439 	struct smb_vc *		iod_vc;
440 	struct smb_slock	iod_rqlock;	/* iod_rqlist, iod_muxwant */
441 	struct smb_rqhead	iod_rqlist;	/* list of outstanding requests */
442 	int			iod_muxwant;
443 	struct proc *		iod_p;
444 	struct thread *		iod_td;
445 	struct smb_cred		iod_scred;
446 	struct smb_slock	iod_evlock;	/* iod_evlist */
447 	STAILQ_HEAD(,smbiod_event) iod_evlist;
448 	struct timespec 	iod_lastrqsent;
449 	struct timespec 	iod_pingtimo;
450 };
451 
452 int  smb_iod_init(void);
453 int  smb_iod_done(void);
454 int  smb_iod_create(struct smb_vc *vcp);
455 int  smb_iod_destroy(struct smbiod *iod);
456 int  smb_iod_request(struct smbiod *iod, int event, void *ident);
457 int  smb_iod_addrq(struct smb_rq *rqp);
458 int  smb_iod_waitrq(struct smb_rq *rqp);
459 int  smb_iod_removerq(struct smb_rq *rqp);
460 
461 #endif /* _KERNEL */
462