smb_conn.h 14 KB

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