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