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