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