1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000-2001 Boris Popov
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
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 * smb_share flags
82 */
83#define SMBS_PERMANENT		0x0001
84#define SMBS_RECONNECTING	0x0002
85#define SMBS_CONNECTED		0x0004
86
87/*
88 * share types
89 */
90#define	SMB_ST_DISK		0x0	/* A: */
91#define	SMB_ST_PRINTER		0x1	/* LPT: */
92#define	SMB_ST_PIPE		0x2	/* IPC */
93#define	SMB_ST_COMM		0x3	/* COMM */
94#define	SMB_ST_ANY		0x4
95#define	SMB_ST_MAX		0x4
96#define SMB_ST_NONE		0xff	/* not a part of protocol */
97
98/*
99 * Negotiated protocol parameters
100 */
101struct smb_sopt {
102	int		sv_proto;
103	int16_t		sv_tz;		/* offset in min relative to UTC */
104	u_int32_t	sv_maxtx;	/* maximum transmit buf size */
105	u_char		sv_sm;		/* security mode */
106	u_int16_t	sv_maxmux;	/* max number of outstanding rq's */
107	u_int16_t 	sv_maxvcs;	/* max number of VCs */
108	u_int16_t	sv_rawmode;
109	u_int32_t	sv_maxraw;	/* maximum raw-buffer size */
110	u_int32_t	sv_skey;	/* session key */
111	u_int32_t	sv_caps;	/* capabilities SMB_CAP_ */
112};
113
114/*
115 * network IO daemon states
116 */
117enum smbiod_state {
118	SMBIOD_ST_NOTCONN,	/* no connect request was made */
119	SMBIOD_ST_RECONNECT,	/* a [re]connect attempt is in progress */
120	SMBIOD_ST_TRANACTIVE,	/* transport level is up */
121	SMBIOD_ST_VCACTIVE,	/* session established */
122	SMBIOD_ST_DEAD		/* connection broken, transport is down */
123};
124
125/*
126 * Info structures
127 */
128#define	SMB_INFO_NONE		0
129#define	SMB_INFO_VC		2
130#define	SMB_INFO_SHARE		3
131
132struct smb_vc_info {
133	int		itype;
134	int		usecount;
135	uid_t		uid;		/* user id of connection */
136	gid_t		gid;		/* group of connection */
137	mode_t		mode;		/* access mode */
138	int		flags;
139	enum smbiod_state iodstate;
140	struct smb_sopt	sopt;
141	char		srvname[SMB_MAXSRVNAMELEN + 1];
142	char		vcname[128];
143};
144
145struct smb_share_info {
146	int		itype;
147	int		usecount;
148	u_short		tid;		/* TID */
149	int		type;		/* share type */
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	char		sname[128];
155};
156
157#ifdef _KERNEL
158
159#include <netsmb/smb_subr.h>
160#include <sys/param.h>
161#include <sys/lock.h>
162#include <sys/condvar.h>
163#include <sys/sx.h>
164
165#define CONNADDREQ(a1,a2)	((a1)->sa_len == (a2)->sa_len && \
166				 bcmp(a1, a2, (a1)->sa_len) == 0)
167
168struct smb_vc;
169struct smb_share;
170struct smb_cred;
171struct smb_rq;
172struct mbdata;
173struct smbioc_oshare;
174struct smbioc_ossn;
175struct uio;
176
177TAILQ_HEAD(smb_rqhead, smb_rq);
178
179#define SMB_DEFRQTIMO	5
180
181#define SMB_DIALECT(vcp)	((vcp)->vc_sopt.sv_proto)
182
183struct smb_tran_desc;
184
185/*
186 * Connection object
187 */
188struct smb_connobj;
189
190typedef void smb_co_gone_t (struct smb_connobj *cp, struct smb_cred *scred);
191typedef void smb_co_free_t (struct smb_connobj *cp);
192
193struct smb_connobj {
194	struct cv		co_lock;
195	struct thread		*co_locker;
196	struct sx		co_interlock;
197	int			co_lockcnt;
198	int			co_level;	/* SMBL_ */
199	int			co_flags;
200	int			co_usecount;
201	struct smb_connobj *	co_parent;
202	SLIST_HEAD(,smb_connobj)co_children;
203	SLIST_ENTRY(smb_connobj)co_next;
204	smb_co_gone_t *		co_gone;
205	smb_co_free_t *		co_free;
206};
207
208#define	SMBCO_FOREACH(var, cp)	SLIST_FOREACH((var), &(cp)->co_children, co_next)
209
210/*
211 * Virtual Circuit (session) to a server.
212 * This is the most (over)complicated part of SMB protocol.
213 * For the user security level (usl), each session with different remote
214 * user name has its own VC.
215 * It is unclear however, should share security level (ssl) allow additional
216 * VCs, because user name is not used and can be the same. On other hand,
217 * multiple VCs allows us to create separate sessions to server on a per
218 * user basis.
219 */
220
221/*
222 * This lock protects vc_flags
223 */
224#define	SMBC_ST_LOCK(vcp)	smb_sl_lock(&(vcp)->vc_stlock)
225#define	SMBC_ST_UNLOCK(vcp)	smb_sl_unlock(&(vcp)->vc_stlock)
226
227struct smb_vc {
228	struct smb_connobj obj;
229	char *		vc_srvname;
230	struct sockaddr*vc_paddr;	/* server addr */
231	struct sockaddr*vc_laddr;	/* local addr, if any */
232	char *		vc_username;
233	char *		vc_pass;	/* password for usl case */
234	char *		vc_domain;	/* workgroup/primary domain */
235
236	u_int		vc_timo;	/* default request timeout */
237	int		vc_maxvcs;	/* maximum number of VC per connection */
238
239	void *		vc_tolower;	/* local charset */
240	void *		vc_toupper;	/* local charset */
241	void *		vc_toserver;	/* local charset to server one */
242	void *		vc_tolocal;	/* server charset to local one */
243	void *		vc_cp_toserver;	/* local charset to server one (using CodePage) */
244	void *		vc_cp_tolocal;	/* server charset to local one (using CodePage) */
245	void *		vc_ucs_toserver; /* local charset to server one (using UCS-2) */
246	void *		vc_ucs_tolocal;	/* server charset to local one (using UCS-2) */
247	int		vc_number;	/* number of this VC from the client side */
248	int		vc_genid;
249	uid_t		vc_uid;		/* user id of connection */
250	gid_t		vc_grp;		/* group of connection */
251	mode_t		vc_mode;	/* access mode */
252	u_short		vc_smbuid;	/* unique vc id assigned by server */
253
254	u_char		vc_hflags;	/* or'ed with flags in the smb header */
255	u_short		vc_hflags2;	/* or'ed with flags in the smb header */
256	void *		vc_tdata;	/* transport control block */
257	struct smb_tran_desc *vc_tdesc;
258	int		vc_chlen;	/* actual challenge length */
259	u_char 		vc_ch[SMB_MAXCHALLENGELEN];
260	u_short		vc_mid;		/* multiplex id */
261	struct smb_sopt	vc_sopt;	/* server options */
262	int		vc_txmax;	/* max tx/rx packet size */
263	int		vc_rxmax;	/* max readx data size */
264	int		vc_wxmax;	/* max writex data size */
265	struct smbiod *	vc_iod;
266	struct smb_slock vc_stlock;
267	u_int32_t	vc_seqno;	/* my next sequence number */
268	u_int8_t	*vc_mackey;	/* MAC key */
269	int		vc_mackeylen;	/* length of MAC key */
270};
271
272#define vc_maxmux	vc_sopt.sv_maxmux
273#define	vc_flags	obj.co_flags
274
275#define SMB_UNICODE_STRINGS(vcp)	((vcp)->vc_hflags2 & SMB_FLAGS2_UNICODE)
276
277#define	SMB_UNICODE_NAME	"UCS-2LE"
278
279/*
280 * smb_share structure describes connection to the given SMB share (tree).
281 * Connection to share is always built on top of the VC.
282 */
283
284/*
285 * This lock protects ss_flags
286 */
287#define	SMBS_ST_LOCK(ssp)	smb_sl_lock(&(ssp)->ss_stlock)
288#define	SMBS_ST_LOCKPTR(ssp)	(&(ssp)->ss_stlock)
289#define	SMBS_ST_UNLOCK(ssp)	smb_sl_unlock(&(ssp)->ss_stlock)
290
291struct smb_share {
292	struct smb_connobj obj;
293	char *		ss_name;
294	u_short		ss_tid;		/* TID */
295	int		ss_type;	/* share type */
296	uid_t		ss_uid;		/* user id of connection */
297	gid_t		ss_grp;		/* group of connection */
298	mode_t		ss_mode;	/* access mode */
299	int		ss_vcgenid;
300	char *		ss_pass;	/* password to a share, can be null */
301	struct smb_slock ss_stlock;
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
312struct 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
334struct 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 */
351int  smb_sm_init(void);
352int  smb_sm_done(void);
353int  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 */
360void smb_co_ref(struct smb_connobj *cp);
361void smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred);
362int  smb_co_get(struct smb_connobj *cp, struct smb_cred *scred);
363void smb_co_put(struct smb_connobj *cp, struct smb_cred *scred);
364int  smb_co_lock(struct smb_connobj *cp);
365void smb_co_unlock(struct smb_connobj *cp);
366
367/*
368 * session level functions
369 */
370int  smb_vc_create(struct smb_vcspec *vcspec,
371	struct smb_cred *scred, struct smb_vc **vcpp);
372int  smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred);
373int  smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode);
374int  smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred);
375void smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred);
376void smb_vc_ref(struct smb_vc *vcp);
377void smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred);
378int  smb_vc_lock(struct smb_vc *vcp);
379void smb_vc_unlock(struct smb_vc *vcp);
380int  smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *shspec,
381	struct smb_cred *scred, struct smb_share **sspp);
382const char * smb_vc_getpass(struct smb_vc *vcp);
383u_short smb_vc_nextmid(struct smb_vc *vcp);
384
385/*
386 * share level functions
387 */
388int  smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
389	struct smb_cred *scred, struct smb_share **sspp);
390int  smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode);
391void smb_share_ref(struct smb_share *ssp);
392void smb_share_rele(struct smb_share *ssp, struct smb_cred *scred);
393int  smb_share_get(struct smb_share *ssp, struct smb_cred *scred);
394void smb_share_put(struct smb_share *ssp, struct smb_cred *scred);
395int  smb_share_lock(struct smb_share *ssp);
396void smb_share_unlock(struct smb_share *ssp);
397void smb_share_invalidate(struct smb_share *ssp);
398int  smb_share_valid(struct smb_share *ssp);
399const char * smb_share_getpass(struct smb_share *ssp);
400
401/*
402 * SMB protocol level functions
403 */
404int  smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred);
405int  smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred);
406int  smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred);
407int  smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred);
408int  smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred);
409int  smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
410	struct smb_cred *scred);
411int  smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
412	struct smb_cred *scred);
413int  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
428struct 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
437struct 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 thread *		iod_td;
449	struct smb_cred		iod_scred;
450	struct smb_slock	iod_evlock;	/* iod_evlist */
451	STAILQ_HEAD(,smbiod_event) iod_evlist;
452	struct timespec 	iod_lastrqsent;
453	struct timespec 	iod_pingtimo;
454};
455
456int  smb_iod_init(void);
457int  smb_iod_done(void);
458int  smb_iod_create(struct smb_vc *vcp);
459int  smb_iod_destroy(struct smbiod *iod);
460int  smb_iod_request(struct smbiod *iod, int event, void *ident);
461int  smb_iod_addrq(struct smb_rq *rqp);
462int  smb_iod_waitrq(struct smb_rq *rqp);
463int  smb_iod_removerq(struct smb_rq *rqp);
464
465#endif /* _KERNEL */
466