xref: /illumos-gate/usr/src/uts/common/fs/smbclnt/netsmb/smb_subr.h (revision e5d0cebc3bbd01b8ae62cebd964dde7bb8157b02)
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  * $Id: smb_subr.h,v 1.13 2004/09/14 22:59:08 lindak Exp $
33  */
34 
35 /*
36  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
38  * Copyright 2024 RackTop Systems, Inc.
39  */
40 
41 #ifndef _NETSMB_SMB_SUBR_H_
42 #define	_NETSMB_SMB_SUBR_H_
43 
44 #include <sys/cmn_err.h>
45 #include <sys/lock.h>
46 #include <sys/note.h>
47 #include <netsmb/mchain.h>
48 #include <netsmb/smb_conn.h>
49 
50 /*
51  * Possible lock commands
52  */
53 #define	SMB_LOCK_EXCL		0
54 #define	SMB_LOCK_SHARED		1
55 #define	SMB_LOCK_RELEASE	2
56 
57 struct msgb;	/* avoiding sys/stream.h here */
58 
59 /* Helper function for SMBERROR */
60 /*PRINTFLIKE3*/
61 extern void smb_errmsg(int, const char *, const char *, ...)
62 	__KPRINTFLIKE(3);
63 void m_dumpm(struct msgb *);
64 
65 /*
66  * Let's use C99 standard variadic macros!
67  * Also the C99 __func__ (function name) feature.
68  */
69 #define	SMBERROR(...) \
70 	smb_errmsg(CE_NOTE, __func__, __VA_ARGS__)
71 #define	SMBPANIC(...) \
72 	smb_errmsg(CE_PANIC, __func__, __VA_ARGS__)
73 #define	SMBSDEBUG(...) \
74 	smb_errmsg(CE_CONT, __func__, __VA_ARGS__)
75 #define	SMBIODEBUG(...) \
76 	smb_errmsg(CE_CONT, __func__, __VA_ARGS__)
77 #define	NBDEBUG(...) \
78 	smb_errmsg(CE_CONT, __func__, __VA_ARGS__)
79 
80 #if defined(DEBUG) || defined(lint)
81 
82 #define	DEBUG_ENTER(str) debug_enter(str)
83 
84 #else /* DEBUG or lint */
85 
86 #define	DEBUG_ENTER(str) ((void)0)
87 
88 #endif /* DEBUG or lint */
89 
90 typedef uint16_t	smb_unichar;
91 typedef	smb_unichar	*smb_uniptr;
92 
93 extern smb_unichar smb_unieol;
94 
95 struct mbchain;
96 struct smb_rq;
97 struct smb_vc;
98 
99 /*
100  * These are the attributes we can get from the server via
101  * SMB commands such as TRANS2_QUERY_FILE_INFORMATION
102  * with info level SMB_QFILEINFO_ALL_INFO, and directory
103  * FindFirst/FindNext info. levels FIND_DIRECTORY_INFO
104  * and FIND_BOTH_DIRECTORY_INFO, etc.
105  *
106  * Values in this struct are always native endian,
107  * and times are converted converted to Unix form.
108  * Note: zero in any of the times means "unknown".
109  */
110 typedef struct smbfattr {
111 	timespec_t	fa_createtime;	/* Note, != ctime */
112 	timespec_t	fa_atime;	/* these 3 are like unix */
113 	timespec_t	fa_mtime;
114 	timespec_t	fa_ctime;
115 	u_offset_t	fa_size;	/* EOF position */
116 	u_offset_t	fa_allocsz;	/* Allocated size. */
117 	uint32_t	fa_attr;	/* Ext. file (DOS) attr */
118 } smbfattr_t;
119 
120 /*
121  * Tunable timeout values.  See: smb_smb.c
122  */
123 extern int smb_timo_notice;
124 extern int smb_timo_default;
125 extern int smb_timo_open;
126 extern int smb_timo_read;
127 extern int smb_timo_write;
128 extern int smb_timo_append;
129 extern dev_t nsmb_dev_tcp;
130 extern dev_t nsmb_dev_tcp6;
131 
132 /*
133  * Tunable timeout values.  See: smb2_smb.c
134  */
135 extern int smb2_timo_notice;
136 extern int smb2_timo_default;
137 extern int smb2_timo_open;
138 extern int smb2_timo_read;
139 extern int smb2_timo_write;
140 extern int smb2_timo_append;
141 
142 void smb_credinit(struct smb_cred *scred, cred_t *cr);
143 void smb_credrele(struct smb_cred *scred);
144 
145 int  smb_maperror(int eclass, int eno);
146 int  smb_maperr32(uint32_t eno);
147 uint_t smb_doserr2status(int, int);
148 int smb_get_dstring(struct mdchain *mdc, struct smb_vc *vcp,
149 	char *outbuf, size_t *outlen, int inlen);
150 int  smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp,
151     const char *src, int len, int caseopt, int *lenp);
152 int  smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp,
153     const char *src, int caseopt);
154 int  smb_put_string(struct smb_rq *rqp, const char *src);
155 int  smb_put_asunistring(struct smb_rq *rqp, const char *src);
156 
157 int smb_smb_ntcreate(struct smb_share *ssp, struct mbchain *name_mb,
158 	uint32_t crflag, uint32_t req_acc, uint32_t efa, uint32_t sh_acc,
159 	uint32_t disp, uint32_t createopt,  uint32_t impersonate,
160 	struct smb_cred *scrp, smb_fh_t *fhp,
161 	uint32_t *cr_act_p, struct smbfattr *fap);
162 
163 int smb_smb_close(struct smb_share *ssp, smb_fh_t *fhp,
164 	struct smb_cred *scrp);
165 
166 int smb_rwuio(smb_fh_t *fhp, uio_rw_t rw,
167 	uio_t *uiop, smb_cred_t *scred, int timo);
168 
169 int smb_cmp_sockaddr(struct sockaddr *, struct sockaddr *);
170 struct sockaddr *smb_dup_sockaddr(struct sockaddr *sa);
171 void smb_free_sockaddr(struct sockaddr *sa);
172 
173 int smb_sign_init(struct smb_vc *);
174 void smb_rq_sign(struct smb_rq *);
175 int smb_rq_verify(struct smb_rq *);
176 int smb_calcv2mackey(struct smb_vc *, const uchar_t *,
177 	const uchar_t *, size_t);
178 int smb_calcmackey(struct smb_vc *, const uchar_t *,
179 	const uchar_t *, size_t);
180 
181 int smb2_sign_init(struct smb_vc *);
182 void smb2_rq_sign(struct smb_rq *);
183 int smb2_rq_verify(struct smb_rq *);
184 
185 void nsmb_crypt_init_mech(struct smb_vc *);
186 void nsmb_crypt_free_mech(struct smb_vc *);
187 void nsmb_crypt_init_keys(struct smb_vc *);
188 
189 int smb3_msg_encrypt(struct smb_vc *vcp, mblk_t **mpp);
190 int smb3_msg_decrypt(struct smb_vc *vcp, mblk_t **mpp);
191 
192 /*
193  * SMB protocol level functions
194  */
195 int  smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred);
196 int  smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred);
197 int  smb_smb_logoff(struct smb_vc *vcp, struct smb_cred *scred);
198 int  smb_smb_echo(smb_vc_t *vcp, smb_cred_t *scred, int timo);
199 int  smb_smb_treeconnect(smb_share_t *ssp, smb_cred_t *scred);
200 int  smb_smb_treedisconnect(smb_share_t *ssp, smb_cred_t *scred);
201 
202 int smb1_smb_ntcreate(struct smb_share *ssp, struct mbchain *name_mb,
203 	uint32_t crflag, uint32_t req_acc, uint32_t efa, uint32_t sh_acc,
204 	uint32_t disp, uint32_t createopt,  uint32_t impersonate,
205 	struct smb_cred *scrp, uint16_t *fidp,
206 	uint32_t *cr_act_p, struct smbfattr *fap);
207 
208 int smb1_smb_close(struct smb_share *ssp, uint16_t fid,
209 	struct timespec *mtime, struct smb_cred *scrp);
210 
211 int smb_smb_open_prjob(struct smb_share *ssp, char *title,
212 	uint16_t setuplen, uint16_t mode,
213 	struct smb_cred *scrp, uint16_t *fidp);
214 
215 int  smb_smb_close_prjob(struct smb_share *ssp, uint16_t fid,
216 	struct smb_cred *scrp);
217 
218 int smb_smb_readx(smb_fh_t *fhp, uint32_t *lenp,
219 	uio_t *uiop, smb_cred_t *scred, int timo);
220 int smb_smb_writex(smb_fh_t *fhp, uint32_t *lenp,
221 	uio_t *uiop, smb_cred_t *scred, int timo);
222 
223 /*
224  * SMB2 protocol level functions
225  */
226 int  smb2_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred);
227 int  smb2_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred);
228 int  smb2_smb_logoff(struct smb_vc *vcp, struct smb_cred *scred);
229 int  smb2_smb_echo(smb_vc_t *vcp, smb_cred_t *scred, int timo);
230 int  smb2_smb_treeconnect(smb_share_t *ssp, smb_cred_t *scred);
231 int  smb2_smb_treedisconnect(smb_share_t *ssp, smb_cred_t *scred);
232 
233 int
234 smb2_smb_ntcreate(struct smb_share *ssp, struct mbchain *name_mb,
235 	struct mbchain *cctx_in, struct mdchain *cctx_out,
236 	uint32_t crflag, uint32_t req_acc, uint32_t efa, uint32_t sh_acc,
237 	uint32_t disp, uint32_t createopt,  uint32_t impersonate,
238 	struct smb_cred *scrp, smb2fid_t *fidp,
239 	uint32_t *cr_act_p, struct smbfattr *fap);
240 
241 int  smb2_smb_close(struct smb_share *ssp, smb2fid_t *fid,
242 	struct smb_cred *scrp);
243 
244 int smb2_smb_ioctl(struct smb_share *ssp, smb2fid_t *fid,
245 	struct mbchain *data_in, struct mdchain *data_out,
246 	uint32_t *data_out_sz,	/* max / returned */
247 	uint32_t ctl_code, struct smb_cred *scrp);
248 
249 int smb2_smb_read(smb_fh_t *fhp, uint32_t *lenp,
250 	uio_t *uiop, smb_cred_t *scred, int timo);
251 int smb2_smb_write(smb_fh_t *fhp, uint32_t *lenp,
252 	uio_t *uiop, smb_cred_t *scred, int timo);
253 
254 /*
255  * time conversions
256  */
257 
258 void  smb_time_local2server(struct timespec *tsp, int tzoff, long *seconds);
259 void  smb_time_server2local(ulong_t seconds, int tzoff, struct timespec *tsp);
260 void  smb_time_NT2local(uint64_t nsec, struct timespec *tsp);
261 void  smb_time_local2NT(struct timespec *tsp, uint64_t *nsec);
262 
263 #endif /* !_NETSMB_SMB_SUBR_H_ */
264