1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2017 Nexenta Systems, Inc. All rights reserved. 24 */ 25 26 /* 27 * Structures and type definitions for the SMB module. 28 */ 29 30 #ifndef _SMBSRV_SMB_KTYPES_H 31 #define _SMBSRV_SMB_KTYPES_H 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 #include <sys/note.h> 38 #include <sys/systm.h> 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/synch.h> 42 #include <sys/taskq.h> 43 #include <sys/socket.h> 44 #include <sys/acl.h> 45 #include <sys/sdt.h> 46 #include <sys/stat.h> 47 #include <sys/vnode.h> 48 #include <sys/cred.h> 49 #include <netinet/in.h> 50 #include <sys/ksocket.h> 51 #include <sys/fem.h> 52 #include <smbsrv/smb.h> 53 #include <smbsrv/smb2.h> 54 #include <smbsrv/smbinfo.h> 55 #include <smbsrv/mbuf.h> 56 #include <smbsrv/smb_sid.h> 57 #include <smbsrv/smb_xdr.h> 58 #include <smbsrv/netbios.h> 59 #include <smbsrv/smb_vops.h> 60 #include <smbsrv/smb_kstat.h> 61 62 struct __door_handle; /* <sys/door.h> */ 63 struct edirent; /* <sys/extdirent.h> */ 64 65 struct smb_disp_entry; 66 struct smb_request; 67 struct smb_server; 68 struct smb_event; 69 struct smb_export; 70 71 /* 72 * Accumulated time and queue length statistics. 73 * 74 * Accumulated time statistics are kept as a running sum of "active" time. 75 * Queue length statistics are kept as a running sum of the product of queue 76 * length and elapsed time at that length -- i.e., a Riemann sum for queue 77 * length integrated against time. (You can also think of the active time as a 78 * Riemann sum, for the boolean function (queue_length > 0) integrated against 79 * time, or you can think of it as the Lebesgue measure of the set on which 80 * queue_length > 0.) 81 * 82 * ^ 83 * | _________ 84 * 8 | i4 | 85 * | | | 86 * Queue 6 | | 87 * Length | _________ | | 88 * 4 | i2 |_______| | 89 * | | i3 | 90 * 2_______| | 91 * | i1 | 92 * |_______________________________| 93 * Time-> t1 t2 t3 t4 94 * 95 * At each change of state (entry or exit from the queue), we add the elapsed 96 * time (since the previous state change) to the active time if the queue length 97 * was non-zero during that interval; and we add the product of the elapsed time 98 * times the queue length to the running length*time sum. 99 * 100 * This method is generalizable to measuring residency in any defined system: 101 * instead of queue lengths, think of "outstanding RPC calls to server X". 102 * 103 * A large number of I/O subsystems have at least two basic "lists" of 104 * transactions they manage: one for transactions that have been accepted for 105 * processing but for which processing has yet to begin, and one for 106 * transactions which are actively being processed (but not done). For this 107 * reason, two cumulative time statistics are defined here: wait (pre-service) 108 * time, and run (service) time. 109 * 110 * All times are 64-bit nanoseconds (hrtime_t), as returned by gethrtime(). 111 * 112 * The units of cumulative busy time are accumulated nanoseconds. The units of 113 * cumulative length*time products are elapsed time times queue length. 114 * 115 * Updates to the fields below are performed implicitly by calls to 116 * these functions: 117 * 118 * smb_srqueue_init() 119 * smb_srqueue_destroy() 120 * smb_srqueue_waitq_enter() 121 * smb_srqueue_runq_exit() 122 * smb_srqueue_waitq_to_runq() 123 * smb_srqueue_update() 124 * 125 * These fields should never be updated by any other means. 126 */ 127 typedef struct smb_srqueue { 128 kmutex_t srq_mutex; 129 hrtime_t srq_wlastupdate; 130 hrtime_t srq_wtime; 131 hrtime_t srq_wlentime; 132 hrtime_t srq_rlastupdate; 133 hrtime_t srq_rtime; 134 hrtime_t srq_rlentime; 135 uint32_t srq_wcnt; 136 uint32_t srq_rcnt; 137 } smb_srqueue_t; 138 139 /* 140 * The fields with the prefix 'ly_a' contain the statistics collected since the 141 * server was last started ('a' for 'aggregated'). The fields with the prefix 142 * 'ly_d' contain the statistics collected since the last snapshot ('d' for 143 * 'delta'). 144 */ 145 typedef struct smb_latency { 146 kmutex_t ly_mutex; 147 uint64_t ly_a_nreq; 148 hrtime_t ly_a_sum; 149 hrtime_t ly_a_mean; 150 hrtime_t ly_a_stddev; 151 uint64_t ly_d_nreq; 152 hrtime_t ly_d_sum; 153 hrtime_t ly_d_mean; 154 hrtime_t ly_d_stddev; 155 } smb_latency_t; 156 157 typedef struct smb_disp_stats { 158 volatile uint64_t sdt_txb; 159 volatile uint64_t sdt_rxb; 160 smb_latency_t sdt_lat; 161 } smb_disp_stats_t; 162 163 int smb_noop(void *, size_t, int); 164 165 #define SMB_AUDIT_STACK_DEPTH 16 166 #define SMB_AUDIT_BUF_MAX_REC 16 167 #define SMB_AUDIT_NODE 0x00000001 168 169 /* 170 * Maximum number of records returned in SMBsearch, SMBfind 171 * and SMBfindunique response. Value set to 10 for compatibility 172 * with Windows. 173 */ 174 #define SMB_MAX_SEARCH 10 175 176 #define SMB_SEARCH_ATTRIBUTES \ 177 (FILE_ATTRIBUTE_HIDDEN | \ 178 FILE_ATTRIBUTE_SYSTEM | \ 179 FILE_ATTRIBUTE_DIRECTORY) 180 181 #define SMB_SEARCH_HIDDEN(sattr) ((sattr) & FILE_ATTRIBUTE_HIDDEN) 182 #define SMB_SEARCH_SYSTEM(sattr) ((sattr) & FILE_ATTRIBUTE_SYSTEM) 183 #define SMB_SEARCH_DIRECTORY(sattr) ((sattr) & FILE_ATTRIBUTE_DIRECTORY) 184 #define SMB_SEARCH_ALL(sattr) ((sattr) & SMB_SEARCH_ATTRIBUTES) 185 186 typedef struct { 187 uint32_t anr_refcnt; 188 int anr_depth; 189 pc_t anr_stack[SMB_AUDIT_STACK_DEPTH]; 190 } smb_audit_record_node_t; 191 192 typedef struct { 193 int anb_index; 194 int anb_max_index; 195 smb_audit_record_node_t anb_records[SMB_AUDIT_BUF_MAX_REC]; 196 } smb_audit_buf_node_t; 197 198 /* 199 * Thread State Machine 200 * -------------------- 201 * 202 * T5 T0 203 * smb_thread_destroy() <-------+ +------- smb_thread_init() 204 * | | 205 * | v 206 * +-----------------------------+ 207 * | SMB_THREAD_STATE_EXITED |<---+ 208 * +-----------------------------+ | 209 * | T1 | 210 * v | 211 * +-----------------------------+ | 212 * | SMB_THREAD_STATE_STARTING | | 213 * +-----------------------------+ | 214 * | T2 | T4 215 * v | 216 * +-----------------------------+ | 217 * | SMB_THREAD_STATE_RUNNING | | 218 * +-----------------------------+ | 219 * | T3 | 220 * v | 221 * +-----------------------------+ | 222 * | SMB_THREAD_STATE_EXITING |----+ 223 * +-----------------------------+ 224 * 225 * Transition T0 226 * 227 * This transition is executed in smb_thread_init(). 228 * 229 * Transition T1 230 * 231 * This transition is executed in smb_thread_start(). 232 * 233 * Transition T2 234 * 235 * This transition is executed by the thread itself when it starts running. 236 * 237 * Transition T3 238 * 239 * This transition is executed by the thread itself in 240 * smb_thread_entry_point() just before calling thread_exit(). 241 * 242 * 243 * Transition T4 244 * 245 * This transition is executed in smb_thread_stop(). 246 * 247 * Transition T5 248 * 249 * This transition is executed in smb_thread_destroy(). 250 */ 251 typedef enum smb_thread_state { 252 SMB_THREAD_STATE_STARTING = 0, 253 SMB_THREAD_STATE_RUNNING, 254 SMB_THREAD_STATE_EXITING, 255 SMB_THREAD_STATE_EXITED, 256 SMB_THREAD_STATE_FAILED 257 } smb_thread_state_t; 258 259 struct _smb_thread; 260 261 typedef void (*smb_thread_ep_t)(struct _smb_thread *, void *ep_arg); 262 263 #define SMB_THREAD_MAGIC 0x534D4254 /* SMBT */ 264 265 typedef struct _smb_thread { 266 uint32_t sth_magic; 267 char sth_name[32]; 268 smb_thread_state_t sth_state; 269 kthread_t *sth_th; 270 kt_did_t sth_did; 271 smb_thread_ep_t sth_ep; 272 void *sth_ep_arg; 273 pri_t sth_pri; 274 boolean_t sth_kill; 275 kmutex_t sth_mtx; 276 kcondvar_t sth_cv; 277 } smb_thread_t; 278 279 /* 280 * Pool of IDs 281 * ----------- 282 * 283 * A pool of IDs is a pool of 16 bit numbers. It is implemented as a bitmap. 284 * A bit set to '1' indicates that that particular value has been allocated. 285 * The allocation process is done shifting a bit through the whole bitmap. 286 * The current position of that index bit is kept in the smb_idpool_t 287 * structure and represented by a byte index (0 to buffer size minus 1) and 288 * a bit index (0 to 7). 289 * 290 * The pools start with a size of 8 bytes or 64 IDs. Each time the pool runs 291 * out of IDs its current size is doubled until it reaches its maximum size 292 * (8192 bytes or 65536 IDs). The IDs 0 and 65535 are never given out which 293 * means that a pool can have a maximum number of 65534 IDs available. 294 */ 295 #define SMB_IDPOOL_MAGIC 0x4944504C /* IDPL */ 296 #define SMB_IDPOOL_MIN_SIZE 64 /* Number of IDs to begin with */ 297 #define SMB_IDPOOL_MAX_SIZE 64 * 1024 298 299 typedef struct smb_idpool { 300 uint32_t id_magic; 301 kmutex_t id_mutex; 302 uint8_t *id_pool; 303 uint32_t id_size; 304 uint32_t id_maxsize; 305 uint8_t id_bit; 306 uint8_t id_bit_idx; 307 uint32_t id_idx; 308 uint32_t id_idx_msk; 309 uint32_t id_free_counter; 310 uint32_t id_max_free_counter; 311 } smb_idpool_t; 312 313 /* 314 * Maximum size of a Transport Data Unit when CAP_LARGE_READX and 315 * CAP_LARGE_WRITEX are not set. CAP_LARGE_READX/CAP_LARGE_WRITEX 316 * allow the payload to exceed the negotiated buffer size. 317 * 4 --> NBT/TCP Transport Header. 318 * 32 --> SMB Header 319 * 1 --> Word Count byte 320 * 510 --> Maximum Number of bytes of the Word Table (2 * 255) 321 * 2 --> Byte count of the data 322 * 65535 --> Maximum size of the data 323 * ----- 324 * 66084 325 */ 326 #define SMB_REQ_MAX_SIZE 66560 /* 65KB */ 327 #define SMB_XPRT_MAX_SIZE (SMB_REQ_MAX_SIZE + NETBIOS_HDR_SZ) 328 329 #define SMB_TXREQ_MAGIC 0X54524251 /* 'TREQ' */ 330 typedef struct { 331 list_node_t tr_lnd; 332 uint32_t tr_magic; 333 int tr_len; 334 uint8_t tr_buf[SMB_XPRT_MAX_SIZE]; 335 } smb_txreq_t; 336 337 #define SMB_TXLST_MAGIC 0X544C5354 /* 'TLST' */ 338 typedef struct { 339 uint32_t tl_magic; 340 kmutex_t tl_mutex; 341 kcondvar_t tl_wait_cv; 342 boolean_t tl_active; 343 } smb_txlst_t; 344 345 /* 346 * Maximum buffer size for NT is 37KB. If all clients are Windows 2000, this 347 * can be changed to 64KB. 37KB must be used with a mix of NT/Windows 2000 348 * clients because NT loses directory entries when values greater than 37KB are 349 * used. 350 * 351 * Note: NBT_MAXBUF will be subtracted from the specified max buffer size to 352 * account for the NBT header. 353 */ 354 #define NBT_MAXBUF 8 355 #define SMB_NT_MAXBUF (37 * 1024) 356 357 #define OUTBUFSIZE (65 * 1024) 358 #define SMBHEADERSIZE 32 359 #define SMBND_HASH_MASK (0xFF) 360 #define MAX_IOVEC 512 361 #define MAX_READREF (8 * 1024) 362 363 #define SMB_WORKER_MIN 4 364 #define SMB_WORKER_DEFAULT 64 365 #define SMB_WORKER_MAX 1024 366 367 /* 368 * Destructor object used in the locked-list delete queue. 369 */ 370 #define SMB_DTOR_MAGIC 0x44544F52 /* DTOR */ 371 #define SMB_DTOR_VALID(d) \ 372 ASSERT(((d) != NULL) && ((d)->dt_magic == SMB_DTOR_MAGIC)) 373 374 typedef void (*smb_dtorproc_t)(void *); 375 376 typedef struct smb_dtor { 377 list_node_t dt_lnd; 378 uint32_t dt_magic; 379 void *dt_object; 380 smb_dtorproc_t dt_proc; 381 } smb_dtor_t; 382 383 typedef struct smb_llist { 384 krwlock_t ll_lock; 385 list_t ll_list; 386 uint32_t ll_count; 387 uint64_t ll_wrop; 388 kmutex_t ll_mutex; 389 list_t ll_deleteq; 390 uint32_t ll_deleteq_count; 391 boolean_t ll_flushing; 392 } smb_llist_t; 393 394 typedef struct smb_slist { 395 kmutex_t sl_mutex; 396 kcondvar_t sl_cv; 397 list_t sl_list; 398 uint32_t sl_count; 399 boolean_t sl_waiting; 400 } smb_slist_t; 401 402 /* 403 * smb_avl_t State Machine 404 * -------------------- 405 * 406 * +-----------------------------+ 407 * | SMB_AVL_STATE_START | 408 * +-----------------------------+ 409 * | T0 410 * v 411 * +-----------------------------+ 412 * | SMB_AVL_STATE_READY | 413 * +-----------------------------+ 414 * | T1 415 * v 416 * +-----------------------------+ 417 * | SMB_AVL_STATE_DESTROYING | 418 * +-----------------------------+ 419 * 420 * Transition T0 421 * 422 * This transition is executed in smb_avl_create(). 423 * 424 * Transition T1 425 * 426 * This transition is executed in smb_avl_destroy(). 427 * 428 */ 429 typedef enum { 430 SMB_AVL_STATE_START = 0, 431 SMB_AVL_STATE_READY, 432 SMB_AVL_STATE_DESTROYING 433 } smb_avl_state_t; 434 435 typedef struct smb_avl_nops { 436 int (*avln_cmp) (const void *, const void *); 437 void (*avln_hold)(const void *); 438 boolean_t (*avln_rele)(const void *); 439 void (*avln_destroy)(void *); 440 } smb_avl_nops_t; 441 442 typedef struct smb_avl_cursor { 443 void *avlc_next; 444 uint32_t avlc_sequence; 445 } smb_avl_cursor_t; 446 447 typedef struct smb_avl { 448 krwlock_t avl_lock; 449 avl_tree_t avl_tree; 450 kmutex_t avl_mutex; 451 kcondvar_t avl_cv; 452 smb_avl_state_t avl_state; 453 uint32_t avl_refcnt; 454 uint32_t avl_sequence; 455 const smb_avl_nops_t *avl_nops; 456 } smb_avl_t; 457 458 typedef struct { 459 kcondvar_t rwx_cv; 460 kmutex_t rwx_mutex; 461 krwlock_t rwx_lock; 462 boolean_t rwx_waiting; 463 } smb_rwx_t; 464 465 typedef struct smb_export { 466 kmutex_t e_mutex; 467 boolean_t e_ready; 468 smb_llist_t e_vfs_list; 469 smb_avl_t e_share_avl; 470 smb_slist_t e_unexport_list; 471 smb_thread_t e_unexport_thread; 472 } smb_export_t; 473 474 /* 475 * NOTIFY CHANGE, a.k.a. File Change Notification (FCN) 476 */ 477 478 /* 479 * These FCN filter mask values are not from MS-FSCC, but 480 * must not overlap with any FILE_NOTIFY_VALID_MASK values. 481 */ 482 #define FILE_NOTIFY_CHANGE_EV_SUBDIR 0x00010000 483 #define FILE_NOTIFY_CHANGE_EV_DELETE 0x00020000 484 #define FILE_NOTIFY_CHANGE_EV_CLOSED 0x00040000 485 #define FILE_NOTIFY_CHANGE_EV_OVERFLOW 0x00080000 486 487 /* 488 * Note: These FCN action values are not from MS-FSCC, but must 489 * follow in sequence from FILE_ACTION_MODIFIED_STREAM. 490 * 491 * FILE_ACTION_SUBDIR_CHANGED is used internally for 492 * "watch tree" support, posted to all parents of a 493 * directory that had one of the changes above. 494 * 495 * FILE_ACTION_DELETE_PENDING is used internally to tell 496 * notify change requests when the "delete-on-close" flag 497 * has been set on the directory being watched. 498 * 499 * FILE_ACTION_HANDLE_CLOSED is used to wakeup notify change 500 * requests when the watched directory handle is closed. 501 */ 502 #define FILE_ACTION_SUBDIR_CHANGED 0x00000009 503 #define FILE_ACTION_DELETE_PENDING 0x0000000a 504 #define FILE_ACTION_HANDLE_CLOSED 0x0000000b 505 506 /* 507 * Sub-struct within smb_ofile_t 508 */ 509 typedef struct smb_notify { 510 list_t nc_waiters; /* Waiting SRs */ 511 mbuf_chain_t nc_buffer; 512 uint32_t nc_filter; 513 uint32_t nc_events; 514 int nc_last_off; 515 boolean_t nc_subscribed; 516 } smb_notify_t; 517 518 /* 519 * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly 520 * over TCP, which is also known as direct hosted NetBIOS-less SMB 521 * or SMB-over-TCP. 522 * 523 * NBT messages have a 4-byte header that defines the message type 524 * (8-bits), a 7-bit flags field and a 17-bit length. 525 * 526 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 527 * | TYPE | FLAGS |E| LENGTH | 528 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 529 * 530 * 8-bit type Defined in RFC 1002 531 * 7-bit flags Bits 0-6 reserved (must be 0) 532 * Bit 7: Length extension bit (E) 533 * 17-bit length Includes bit 7 of the flags byte 534 * 535 * 536 * SMB-over-TCP is defined to use a modified version of the NBT header 537 * containing an 8-bit message type and 24-bit message length. 538 * 539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 540 * | TYPE | LENGTH | 541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 542 * 543 * 8-bit type Must be 0 544 * 24-bit length 545 * 546 * The following structure is used to represent a generic, in-memory 547 * SMB transport header; it is not intended to map directly to either 548 * of the over-the-wire formats. 549 */ 550 typedef struct { 551 uint8_t xh_type; 552 uint32_t xh_length; 553 } smb_xprt_t; 554 555 int MBC_LENGTH(struct mbuf_chain *); 556 int MBC_MAXBYTES(struct mbuf_chain *); 557 void MBC_SETUP(struct mbuf_chain *, uint32_t); 558 void MBC_INIT(struct mbuf_chain *, uint32_t); 559 void MBC_FLUSH(struct mbuf_chain *); 560 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *); 561 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *); 562 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN); 563 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC, 564 int OFF, int LEN); 565 566 #define MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes) 567 568 #define OPLOCK_MIN_TIMEOUT (5 * 1000) 569 #define OPLOCK_STD_TIMEOUT (30 * 1000) 570 571 /* 572 * Oplock break flags: 573 * SMB_OPLOCK_BREAK_EXCLUSIVE - only break exclusive oplock 574 * (type SMB_OPLOCK_EXCLUSIVE or SMB_OPLOCK_BATCH) 575 * SMB_OPLOCK_BREAK_BATCH - only break exclusive BATCH oplock 576 * SMB_OPLOCK_BREAK_NOWAIT - do not wait for oplock break ack 577 */ 578 #define SMB_OPLOCK_NO_BREAK 0x00 579 #define SMB_OPLOCK_BREAK_TO_NONE 0x01 580 #define SMB_OPLOCK_BREAK_TO_LEVEL_II 0x02 581 #define SMB_OPLOCK_BREAK_EXCLUSIVE 0x04 582 #define SMB_OPLOCK_BREAK_BATCH 0x08 583 #define SMB_OPLOCK_BREAK_NOWAIT 0x10 584 585 /* 586 * Oplocks levels are defined to match the levels in the SMB 587 * protocol (nt_create_andx / nt_transact_create) and should 588 * not be changed 589 */ 590 #define SMB_OPLOCK_NONE 0 591 #define SMB_OPLOCK_EXCLUSIVE 1 592 #define SMB_OPLOCK_BATCH 2 593 #define SMB_OPLOCK_LEVEL_II 3 594 595 typedef struct smb_oplock { 596 kmutex_t ol_mutex; 597 kcondvar_t ol_cv; 598 kthread_t *ol_xthread; 599 boolean_t ol_fem; /* fem monitor installed? */ 600 uint8_t ol_brk_pending; 601 uint8_t ol_break; 602 uint32_t ol_count; /* number of grants */ 603 list_t ol_grants; /* list of smb_oplock_grant_t */ 604 } smb_oplock_t; 605 606 #define SMB_OPLOCK_GRANT_MAGIC 0x4F4C4B47 /* OLKG */ 607 #define SMB_OPLOCK_GRANT_VALID(p) \ 608 ASSERT((p)->og_magic == SMB_OPLOCK_GRANT_MAGIC) 609 #define SMB_OFILE_OPLOCK_GRANTED(p) \ 610 ((p)->f_oplock_grant.og_magic == SMB_OPLOCK_GRANT_MAGIC) 611 typedef struct smb_oplock_grant { 612 list_node_t og_lnd; 613 uint32_t og_magic; 614 uint8_t og_breaking; 615 uint8_t og_level; 616 struct smb_ofile *og_ofile; 617 } smb_oplock_grant_t; 618 619 #define SMB_OPLOCK_BREAK_MAGIC 0x4F4C4B42 /* OLKB */ 620 #define SMB_OPLOCK_BREAK_VALID(p) \ 621 ASSERT((p)->ob_magic == SMB_OPLOCK_BREAK_MAGIC) 622 typedef struct smb_oplock_break { 623 list_node_t ob_lnd; 624 uint32_t ob_magic; 625 struct smb_node *ob_node; 626 } smb_oplock_break_t; 627 628 629 #define SMB_VFS_MAGIC 0x534D4256 /* 'SMBV' */ 630 631 typedef struct smb_vfs { 632 list_node_t sv_lnd; 633 uint32_t sv_magic; 634 uint32_t sv_refcnt; 635 vfs_t *sv_vfsp; 636 vnode_t *sv_rootvp; 637 } smb_vfs_t; 638 639 #define SMB_NODE_MAGIC 0x4E4F4445 /* 'NODE' */ 640 #define SMB_NODE_VALID(p) ASSERT((p)->n_magic == SMB_NODE_MAGIC) 641 642 typedef enum { 643 SMB_NODE_STATE_AVAILABLE = 0, 644 SMB_NODE_STATE_DESTROYING 645 } smb_node_state_t; 646 647 /* 648 * waiting_event # of clients requesting FCN 649 * n_timestamps cached timestamps 650 * n_allocsz cached file allocation size 651 * n_dnode directory node 652 * n_unode unnamed stream node 653 * delete_on_close_cred credentials for delayed delete 654 */ 655 typedef struct smb_node { 656 list_node_t n_lnd; 657 uint32_t n_magic; 658 krwlock_t n_lock; 659 kmutex_t n_mutex; 660 smb_node_state_t n_state; 661 uint32_t n_refcnt; 662 uint32_t n_hashkey; 663 smb_llist_t *n_hash_bucket; 664 uint32_t n_open_count; 665 uint32_t n_opening_count; 666 smb_llist_t n_ofile_list; 667 /* If entering both, go in order n_lock_list, n_wlock_list */ 668 smb_llist_t n_lock_list; /* active locks */ 669 smb_llist_t n_wlock_list; /* waiting locks */ 670 uint32_t n_pending_dosattr; 671 volatile int flags; 672 u_offset_t n_allocsz; 673 uint32_t n_fcn_count; 674 smb_oplock_t n_oplock; 675 struct smb_node *n_dnode; 676 struct smb_node *n_unode; 677 cred_t *delete_on_close_cred; 678 uint32_t n_delete_on_close_flags; 679 char od_name[MAXNAMELEN]; 680 vnode_t *vp; 681 smb_audit_buf_node_t *n_audit_buf; 682 } smb_node_t; 683 684 #define NODE_FLAGS_REPARSE 0x00001000 685 #define NODE_FLAGS_DFSLINK 0x00002000 686 #define NODE_FLAGS_VFSROOT 0x00004000 687 #define NODE_FLAGS_SYSTEM 0x00008000 688 #define NODE_FLAGS_WRITE_THROUGH 0x00100000 689 #define NODE_XATTR_DIR 0x01000000 690 #define NODE_FLAGS_DELETE_ON_CLOSE 0x40000000 691 #define NODE_FLAGS_EXECUTABLE 0x80000000 692 693 #define SMB_NODE_VFS(node) ((node)->vp->v_vfsp) 694 #define SMB_NODE_FSID(node) ((node)->vp->v_vfsp->vfs_fsid) 695 696 /* Maximum buffer size for encryption key */ 697 #define SMB_ENCRYPT_KEY_MAXLEN 32 698 699 #define SMB_SHARE_MAGIC 0x4B534852 /* KSHR */ 700 701 typedef struct smb_kshare { 702 uint32_t shr_magic; 703 char *shr_name; 704 char *shr_path; 705 char *shr_cmnt; 706 char *shr_container; 707 char *shr_oemname; 708 uint32_t shr_flags; 709 uint32_t shr_type; 710 uint32_t shr_refcnt; 711 uint32_t shr_autocnt; 712 uid_t shr_uid; 713 gid_t shr_gid; 714 char *shr_access_none; 715 char *shr_access_ro; 716 char *shr_access_rw; 717 avl_node_t shr_link; 718 kmutex_t shr_mutex; 719 } smb_kshare_t; 720 721 722 typedef struct smb_arg_negotiate { 723 char *ni_name; 724 int ni_dialect; 725 int ni_index; 726 uint32_t ni_capabilities; 727 uint16_t ni_maxmpxcount; 728 int16_t ni_tzcorrection; 729 uint8_t ni_keylen; 730 uint8_t ni_key[SMB_ENCRYPT_KEY_MAXLEN]; 731 timestruc_t ni_servertime; 732 } smb_arg_negotiate_t; 733 734 typedef enum { 735 SMB_SSNSETUP_PRE_NTLM012 = 1, 736 SMB_SSNSETUP_NTLM012_NOEXT, 737 SMB_SSNSETUP_NTLM012_EXTSEC 738 } smb_ssnsetup_type_t; 739 740 typedef struct smb_arg_sessionsetup { 741 smb_ssnsetup_type_t ssi_type; 742 char *ssi_user; 743 char *ssi_domain; 744 /* LM password hash, f.k.a. case-insensitive p/w */ 745 uint16_t ssi_lmpwlen; 746 uint8_t *ssi_lmpwd; 747 /* NT password hash, f.k.a. case-sensitive p/w */ 748 uint16_t ssi_ntpwlen; 749 uint8_t *ssi_ntpwd; 750 /* Incoming security blob */ 751 uint16_t ssi_iseclen; 752 uint8_t *ssi_isecblob; 753 /* Incoming security blob */ 754 uint16_t ssi_oseclen; 755 uint8_t *ssi_osecblob; 756 /* parameters */ 757 uint16_t ssi_maxbufsize; 758 uint16_t ssi_maxmpxcount; 759 uint32_t ssi_capabilities; 760 int ssi_native_os; 761 int ssi_native_lm; 762 } smb_arg_sessionsetup_t; 763 764 typedef struct tcon { 765 char *name; 766 char *path; 767 char *service; 768 int pwdlen; 769 char *password; 770 uint16_t flags; 771 uint16_t optional_support; 772 smb_kshare_t *si; 773 } smb_arg_tcon_t; 774 775 /* 776 * Based on section 2.6.1.2 (Connection Management) of the June 13, 777 * 1996 CIFS spec, a server may terminate the transport connection 778 * due to inactivity. The client software is expected to be able to 779 * automatically reconnect to the server if this happens. Like much 780 * of the useful background information, this section appears to 781 * have been dropped from later revisions of the document. 782 * 783 * Each session has an activity timestamp that's updated whenever a 784 * request is dispatched. If the session is idle, i.e. receives no 785 * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be 786 * closed. 787 * 788 * Each session has an I/O semaphore to serialize communication with 789 * the client. For example, after receiving a raw-read request, the 790 * server is not allowed to send an oplock break to the client until 791 * after it has sent the raw-read data. 792 */ 793 #define SMB_SESSION_INACTIVITY_TIMEOUT (15 * 60) 794 795 /* SMB1 signing */ 796 struct smb_sign { 797 unsigned int flags; 798 uint32_t seqnum; 799 uint_t mackey_len; 800 uint8_t *mackey; 801 }; 802 803 /* 804 * SMB2 signing 805 */ 806 struct smb_key { 807 uint_t len; 808 uint8_t key[SMB2_SESSION_KEY_LEN]; 809 }; 810 811 #define SMB_SIGNING_ENABLED 1 812 #define SMB_SIGNING_CHECK 2 813 814 /* 815 * Session State Machine 816 * --------------------- 817 * 818 * +-----------------------------+ +------------------------------+ 819 * | SMB_SESSION_STATE_CONNECTED | | SMB_SESSION_STATE_TERMINATED | 820 * +-----------------------------+ +------------------------------+ 821 * T0| ^ 822 * +--------------------+ |T5 823 * v |T4 | 824 * +-------------------------------+ | +--------------------------------+ 825 * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED | 826 * +-------------------------------+ +--------------------------------+ 827 * T1| ^ 828 * +----------+ |T3 829 * v | 830 * +------------------------------+ 831 * | SMB_SESSION_STATE_NEGOTIATED | 832 * +------------------------------+ 833 * 834 * 835 * Transition T0 836 * 837 * 838 * 839 * Transition T1 840 * 841 * 842 * 843 * Transition T2 844 * 845 * 846 * 847 * Transition T3 848 * 849 * 850 * 851 * Transition T4 852 * 853 * 854 * 855 * Transition T5 856 * 857 * 858 * 859 */ 860 #define SMB_SESSION_MAGIC 0x53455353 /* 'SESS' */ 861 #define SMB_SESSION_VALID(p) \ 862 ASSERT(((p) != NULL) && ((p)->s_magic == SMB_SESSION_MAGIC)) 863 864 #define SMB_CHALLENGE_SZ 8 865 866 typedef enum { 867 SMB_SESSION_STATE_INITIALIZED = 0, 868 SMB_SESSION_STATE_DISCONNECTED, 869 SMB_SESSION_STATE_CONNECTED, 870 SMB_SESSION_STATE_ESTABLISHED, 871 SMB_SESSION_STATE_NEGOTIATED, 872 SMB_SESSION_STATE_TERMINATED, 873 SMB_SESSION_STATE_SENTINEL 874 } smb_session_state_t; 875 876 /* Bits in s_flags below */ 877 #define SMB_SSN_AAPL_CCEXT 1 /* Saw "AAPL" create ctx. ext. */ 878 #define SMB_SSN_AAPL_READDIR 2 /* Wants MacOS ext. readdir */ 879 880 typedef struct smb_session { 881 list_node_t s_lnd; 882 uint32_t s_magic; 883 smb_rwx_t s_lock; 884 uint64_t s_kid; 885 smb_session_state_t s_state; 886 uint32_t s_flags; 887 taskqid_t s_receiver_tqid; 888 kthread_t *s_thread; 889 kt_did_t s_ktdid; 890 int (*newrq_func)(struct smb_request *); 891 struct smb_server *s_server; 892 smb_kmod_cfg_t s_cfg; 893 int32_t s_gmtoff; 894 uint32_t keep_alive; 895 uint64_t opentime; 896 uint16_t s_local_port; 897 uint16_t s_remote_port; 898 smb_inaddr_t ipaddr; 899 smb_inaddr_t local_ipaddr; 900 int dialect; 901 int native_os; 902 int native_lm; 903 904 kmutex_t s_credits_mutex; 905 uint16_t s_cur_credits; 906 uint16_t s_max_credits; 907 908 uint32_t capabilities; 909 910 struct smb_sign signing; /* SMB1 */ 911 void *sign_mech; /* mechanism info */ 912 void (*sign_fini)(struct smb_session *); 913 914 ksocket_t sock; 915 916 smb_slist_t s_req_list; 917 smb_llist_t s_xa_list; 918 smb_llist_t s_user_list; 919 smb_llist_t s_tree_list; 920 smb_idpool_t s_uid_pool; 921 smb_idpool_t s_tid_pool; 922 smb_txlst_t s_txlst; 923 924 volatile uint32_t s_tree_cnt; 925 volatile uint32_t s_file_cnt; 926 volatile uint32_t s_dir_cnt; 927 928 uint16_t secmode; 929 uint32_t sesskey; 930 uint32_t challenge_len; 931 unsigned char challenge_key[SMB_CHALLENGE_SZ]; 932 int64_t activity_timestamp; 933 /* 934 * Maximum negotiated buffer sizes between SMB client and server 935 * in SMB_SESSION_SETUP_ANDX 936 */ 937 int cmd_max_bytes; 938 int reply_max_bytes; 939 uint16_t smb_msg_size; 940 uint16_t smb_max_mpx; 941 smb_srqueue_t *s_srqueue; 942 uint64_t start_time; 943 unsigned char MAC_key[44]; 944 char ip_addr_str[INET6_ADDRSTRLEN]; 945 char clnt_uuid[16]; 946 char workstation[SMB_PI_MAX_HOST]; 947 } smb_session_t; 948 949 #define SMB_USER_MAGIC 0x55534552 /* 'USER' */ 950 #define SMB_USER_VALID(u) \ 951 ASSERT(((u) != NULL) && ((u)->u_magic == SMB_USER_MAGIC)) 952 953 #define SMB_USER_FLAG_GUEST SMB_ATF_GUEST 954 #define SMB_USER_FLAG_ANON SMB_ATF_ANON 955 #define SMB_USER_FLAG_ADMIN SMB_ATF_ADMIN 956 #define SMB_USER_FLAG_POWER_USER SMB_ATF_POWERUSER 957 #define SMB_USER_FLAG_BACKUP_OPERATOR SMB_ATF_BACKUPOP 958 959 #define SMB_USER_IS_ADMIN(U) (((U)->u_flags & SMB_USER_FLAG_ADMIN) != 0) 960 #define SMB_USER_IS_GUEST(U) (((U)->u_flags & SMB_USER_FLAG_GUEST) != 0) 961 962 #define SMB_USER_PRIV_TAKE_OWNERSHIP 0x00000001 963 #define SMB_USER_PRIV_BACKUP 0x00000002 964 #define SMB_USER_PRIV_RESTORE 0x00000004 965 #define SMB_USER_PRIV_SECURITY 0x00000008 966 967 968 typedef enum { 969 SMB_USER_STATE_LOGGING_ON = 0, 970 SMB_USER_STATE_LOGGED_ON, 971 SMB_USER_STATE_LOGGING_OFF, 972 SMB_USER_STATE_LOGGED_OFF, 973 SMB_USER_STATE_SENTINEL 974 } smb_user_state_t; 975 976 typedef struct smb_user { 977 list_node_t u_lnd; 978 uint32_t u_magic; 979 kmutex_t u_mutex; 980 smb_user_state_t u_state; 981 982 struct smb_server *u_server; 983 smb_session_t *u_session; 984 ksocket_t u_authsock; 985 timeout_id_t u_auth_tmo; 986 uint16_t u_name_len; 987 char *u_name; 988 uint16_t u_domain_len; 989 char *u_domain; 990 time_t u_logon_time; 991 cred_t *u_cred; 992 cred_t *u_privcred; 993 994 uint32_t u_refcnt; 995 uint32_t u_flags; 996 uint32_t u_privileges; 997 uint16_t u_uid; 998 uint32_t u_audit_sid; 999 1000 uint32_t u_sign_flags; 1001 struct smb_key u_sign_key; /* SMB2 signing */ 1002 } smb_user_t; 1003 1004 #define SMB_TREE_MAGIC 0x54524545 /* 'TREE' */ 1005 #define SMB_TREE_VALID(p) \ 1006 ASSERT((p != NULL) && ((p)->t_magic == SMB_TREE_MAGIC)) 1007 1008 #define SMB_TYPENAMELEN _ST_FSTYPSZ 1009 #define SMB_VOLNAMELEN 32 1010 1011 #define SMB_TREE_READONLY 0x00000001 1012 #define SMB_TREE_SUPPORTS_ACLS 0x00000002 1013 #define SMB_TREE_STREAMS 0x00000004 1014 #define SMB_TREE_CASEINSENSITIVE 0x00000008 1015 #define SMB_TREE_NO_CASESENSITIVE 0x00000010 1016 #define SMB_TREE_NO_EXPORT 0x00000020 1017 #define SMB_TREE_OPLOCKS 0x00000040 1018 #define SMB_TREE_SHORTNAMES 0x00000080 1019 #define SMB_TREE_XVATTR 0x00000100 1020 #define SMB_TREE_DIRENTFLAGS 0x00000200 1021 #define SMB_TREE_ACLONCREATE 0x00000400 1022 #define SMB_TREE_ACEMASKONACCESS 0x00000800 1023 #define SMB_TREE_NFS_MOUNTED 0x00001000 1024 #define SMB_TREE_UNICODE_ON_DISK 0x00002000 1025 #define SMB_TREE_CATIA 0x00004000 1026 #define SMB_TREE_ABE 0x00008000 1027 #define SMB_TREE_QUOTA 0x00010000 1028 #define SMB_TREE_DFSROOT 0x00020000 1029 #define SMB_TREE_SPARSE 0x00040000 1030 #define SMB_TREE_TRAVERSE_MOUNTS 0x00080000 1031 1032 typedef enum { 1033 SMB_TREE_STATE_CONNECTED = 0, 1034 SMB_TREE_STATE_DISCONNECTING, 1035 SMB_TREE_STATE_DISCONNECTED, 1036 SMB_TREE_STATE_SENTINEL 1037 } smb_tree_state_t; 1038 1039 typedef struct smb_tree { 1040 list_node_t t_lnd; 1041 uint32_t t_magic; 1042 kmutex_t t_mutex; 1043 smb_tree_state_t t_state; 1044 1045 struct smb_server *t_server; 1046 smb_session_t *t_session; 1047 /* 1048 * user whose uid was in the tree connect message 1049 * ("owner" in MS-CIFS parlance, see section 2.2.1.6 definition of FID) 1050 */ 1051 smb_user_t *t_owner; 1052 smb_node_t *t_snode; 1053 1054 smb_llist_t t_ofile_list; 1055 smb_idpool_t t_fid_pool; 1056 1057 smb_llist_t t_odir_list; 1058 smb_idpool_t t_odid_pool; 1059 1060 uint32_t t_refcnt; 1061 uint32_t t_flags; 1062 int32_t t_res_type; 1063 uint16_t t_tid; 1064 uint16_t t_umask; 1065 char t_sharename[MAXNAMELEN]; 1066 char t_resource[MAXPATHLEN]; 1067 char t_typename[SMB_TYPENAMELEN]; 1068 char t_volume[SMB_VOLNAMELEN]; 1069 acl_type_t t_acltype; 1070 uint32_t t_access; 1071 uint32_t t_execflags; 1072 time_t t_connect_time; 1073 volatile uint32_t t_open_files; 1074 } smb_tree_t; 1075 1076 #define SMB_TREE_VFS(tree) ((tree)->t_snode->vp->v_vfsp) 1077 #define SMB_TREE_FSID(tree) ((tree)->t_snode->vp->v_vfsp->vfs_fsid) 1078 1079 #define SMB_TREE_IS_READONLY(sr) \ 1080 ((sr) != NULL && (sr)->tid_tree != NULL && \ 1081 !((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS)) 1082 1083 #define SMB_TREE_IS_CASEINSENSITIVE(sr) \ 1084 (((sr) && (sr)->tid_tree) ? \ 1085 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0) 1086 1087 #define SMB_TREE_HAS_ACCESS(sr, acemask) \ 1088 ((sr) == NULL ? ACE_ALL_PERMS : ( \ 1089 (((sr) && (sr)->tid_tree) ? \ 1090 (((sr)->tid_tree->t_access) & (acemask)) : 0))) 1091 1092 #define SMB_TREE_SUPPORTS_CATIA(sr) \ 1093 (((sr) && (sr)->tid_tree) ? \ 1094 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0) 1095 1096 #define SMB_TREE_SUPPORTS_ABE(sr) \ 1097 (((sr) && (sr)->tid_tree) ? \ 1098 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_ABE) : 0) 1099 1100 #define SMB_TREE_IS_DFSROOT(sr) \ 1101 (((sr) && (sr)->tid_tree) ? \ 1102 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_DFSROOT) : 0) 1103 1104 #define SMB_TREE_SUPPORTS_SHORTNAMES(sr) \ 1105 (((sr) && (sr)->tid_tree) ? \ 1106 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_SHORTNAMES) : 0) 1107 1108 /* 1109 * SMB_TREE_CONTAINS_NODE is used to check if a node is on the same 1110 * file system as the tree's root filesystem, or if mount point traversal 1111 * should be allowed. Note that this is also called in some cases with 1112 * sr=NULL, where it is expected to evaluate to TRUE. 1113 */ 1114 1115 #define SMB_TREE_CONTAINS_NODE(sr, node) \ 1116 ((sr) == NULL || (sr)->tid_tree == NULL || \ 1117 SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node) || \ 1118 smb_tree_has_feature((sr)->tid_tree, SMB_TREE_TRAVERSE_MOUNTS)) 1119 1120 /* 1121 * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not. 1122 * The macro takes into account read-only settings in any of: 1123 * the tree, the node (pending) and the file-system object. 1124 * all of this is evaluated in smb_ofile_open() and after that 1125 * we can just test the f_flags & SMB_OFLAGS_READONLY 1126 */ 1127 #define SMB_OFILE_IS_READONLY(of) \ 1128 ((of)->f_flags & SMB_OFLAGS_READONLY) 1129 1130 /* 1131 * SMB_PATHFILE_IS_READONLY indicates whether or not a file is 1132 * readonly when the caller has a path rather than an ofile. 1133 */ 1134 #define SMB_PATHFILE_IS_READONLY(sr, node) \ 1135 (SMB_TREE_IS_READONLY((sr)) || \ 1136 smb_node_file_is_readonly((node))) 1137 1138 #define SMB_ODIR_MAGIC 0x4F444952 /* 'ODIR' */ 1139 #define SMB_ODIR_VALID(p) \ 1140 ASSERT((p != NULL) && ((p)->d_magic == SMB_ODIR_MAGIC)) 1141 1142 #define SMB_ODIR_BUFSIZE (8 * 1024) 1143 1144 #define SMB_ODIR_FLAG_WILDCARDS 0x0001 1145 #define SMB_ODIR_FLAG_IGNORE_CASE 0x0002 1146 #define SMB_ODIR_FLAG_XATTR 0x0004 1147 #define SMB_ODIR_FLAG_EDIRENT 0x0008 1148 #define SMB_ODIR_FLAG_CATIA 0x0010 1149 #define SMB_ODIR_FLAG_ABE 0x0020 1150 #define SMB_ODIR_FLAG_SHORTNAMES 0x0040 1151 1152 typedef enum { 1153 SMB_ODIR_STATE_OPEN = 0, 1154 SMB_ODIR_STATE_IN_USE, 1155 SMB_ODIR_STATE_CLOSING, 1156 SMB_ODIR_STATE_CLOSED, 1157 SMB_ODIR_STATE_SENTINEL 1158 } smb_odir_state_t; 1159 1160 typedef enum { 1161 SMB_ODIR_RESUME_CONT, 1162 SMB_ODIR_RESUME_IDX, 1163 SMB_ODIR_RESUME_COOKIE, 1164 SMB_ODIR_RESUME_FNAME 1165 } smb_odir_resume_type_t; 1166 1167 typedef struct smb_odir_resume { 1168 smb_odir_resume_type_t or_type; 1169 int or_idx; 1170 uint32_t or_cookie; 1171 char *or_fname; 1172 } smb_odir_resume_t; 1173 1174 /* 1175 * Flags used when opening an odir 1176 */ 1177 #define SMB_ODIR_OPENF_BACKUP_INTENT 0x01 1178 1179 typedef struct smb_odir { 1180 list_node_t d_lnd; 1181 uint32_t d_magic; 1182 kmutex_t d_mutex; 1183 smb_odir_state_t d_state; 1184 smb_session_t *d_session; 1185 smb_user_t *d_user; 1186 smb_tree_t *d_tree; 1187 smb_node_t *d_dnode; 1188 cred_t *d_cred; 1189 uint32_t d_opened_by_pid; 1190 uint16_t d_odid; 1191 uint16_t d_sattr; 1192 uint32_t d_refcnt; 1193 uint32_t d_flags; 1194 boolean_t d_eof; 1195 int d_bufsize; 1196 uint64_t d_offset; 1197 union { 1198 char *u_bufptr; 1199 struct edirent *u_edp; 1200 struct dirent64 *u_dp; 1201 } d_u; 1202 uint32_t d_last_cookie; 1203 uint32_t d_cookies[SMB_MAX_SEARCH]; 1204 char d_pattern[MAXNAMELEN]; 1205 char d_buf[SMB_ODIR_BUFSIZE]; 1206 char d_last_name[MAXNAMELEN]; 1207 } smb_odir_t; 1208 #define d_bufptr d_u.u_bufptr 1209 #define d_edp d_u.u_edp 1210 #define d_dp d_u.u_dp 1211 1212 typedef struct smb_odirent { 1213 char od_name[MAXNAMELEN]; /* on disk name */ 1214 ino64_t od_ino; 1215 uint32_t od_eflags; 1216 } smb_odirent_t; 1217 1218 #define SMB_OPIPE_MAGIC 0x50495045 /* 'PIPE' */ 1219 #define SMB_OPIPE_VALID(p) \ 1220 ASSERT(((p) != NULL) && (p)->p_magic == SMB_OPIPE_MAGIC) 1221 #define SMB_OPIPE_MAXNAME 32 1222 1223 /* 1224 * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used 1225 * at the interface between SMB and NDR RPC. 1226 */ 1227 typedef struct smb_opipe { 1228 uint32_t p_magic; 1229 kmutex_t p_mutex; 1230 kcondvar_t p_cv; 1231 struct smb_ofile *p_ofile; 1232 struct smb_server *p_server; 1233 uint32_t p_refcnt; 1234 ksocket_t p_socket; 1235 /* This is the "flat" name, without path prefix */ 1236 char p_name[SMB_OPIPE_MAXNAME]; 1237 } smb_opipe_t; 1238 1239 /* 1240 * The of_ftype of an open file should contain the SMB_FTYPE value 1241 * returned when the file/pipe was opened. The following 1242 * assumptions are currently made: 1243 * 1244 * File Type Node PipeInfo 1245 * --------- -------- -------- 1246 * SMB_FTYPE_DISK Valid Null 1247 * SMB_FTYPE_BYTE_PIPE Undefined Undefined 1248 * SMB_FTYPE_MESG_PIPE Null Valid 1249 * SMB_FTYPE_PRINTER Undefined Undefined 1250 * SMB_FTYPE_UNKNOWN Undefined Undefined 1251 */ 1252 1253 /* 1254 * Some flags for ofile structure 1255 * 1256 * SMB_OFLAGS_SET_DELETE_ON_CLOSE 1257 * Set this flag when the corresponding open operation whose 1258 * DELETE_ON_CLOSE bit of the CreateOptions is set. If any 1259 * open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE 1260 * will be set for the file node upon close. 1261 */ 1262 1263 #define SMB_OFLAGS_READONLY 0x0001 1264 #define SMB_OFLAGS_EXECONLY 0x0002 1265 #define SMB_OFLAGS_SET_DELETE_ON_CLOSE 0x0004 1266 #define SMB_OFLAGS_LLF_POS_VALID 0x0008 1267 1268 #define SMB_OFILE_MAGIC 0x4F464C45 /* 'OFLE' */ 1269 #define SMB_OFILE_VALID(p) \ 1270 ASSERT((p != NULL) && ((p)->f_magic == SMB_OFILE_MAGIC)) 1271 1272 typedef enum { 1273 SMB_OFILE_STATE_OPEN = 0, 1274 SMB_OFILE_STATE_CLOSING, 1275 SMB_OFILE_STATE_CLOSED, 1276 SMB_OFILE_STATE_SENTINEL 1277 } smb_ofile_state_t; 1278 1279 typedef struct smb_ofile { 1280 list_node_t f_lnd; /* t_ofile_list */ 1281 list_node_t f_nnd; /* n_ofile_list */ 1282 uint32_t f_magic; 1283 kmutex_t f_mutex; 1284 smb_ofile_state_t f_state; 1285 1286 struct smb_server *f_server; 1287 smb_session_t *f_session; 1288 smb_user_t *f_user; 1289 smb_tree_t *f_tree; 1290 smb_node_t *f_node; 1291 smb_odir_t *f_odir; 1292 smb_opipe_t *f_pipe; 1293 1294 uint32_t f_uniqid; 1295 uint32_t f_refcnt; 1296 uint64_t f_seek_pos; 1297 uint32_t f_flags; 1298 uint32_t f_granted_access; 1299 uint32_t f_share_access; 1300 uint32_t f_create_options; 1301 uint32_t f_opened_by_pid; 1302 uint16_t f_fid; 1303 uint16_t f_ftype; 1304 uint64_t f_llf_pos; 1305 int f_mode; 1306 cred_t *f_cr; 1307 pid_t f_pid; 1308 smb_attr_t f_pending_attr; 1309 boolean_t f_written; 1310 char f_quota_resume[SMB_SID_STRSZ]; 1311 smb_oplock_grant_t f_oplock_grant; 1312 smb_notify_t f_notify; 1313 } smb_ofile_t; 1314 1315 typedef struct smb_fileinfo { 1316 char fi_name[MAXNAMELEN]; 1317 char fi_shortname[SMB_SHORTNAMELEN]; 1318 uint32_t fi_cookie; /* Dir offset (of next entry) */ 1319 uint32_t fi_dosattr; /* DOS attributes */ 1320 uint64_t fi_nodeid; /* file system node id */ 1321 uint64_t fi_size; /* file size in bytes */ 1322 uint64_t fi_alloc_size; /* allocation size in bytes */ 1323 timestruc_t fi_atime; /* last access */ 1324 timestruc_t fi_mtime; /* last modification */ 1325 timestruc_t fi_ctime; /* last status change */ 1326 timestruc_t fi_crtime; /* file creation */ 1327 } smb_fileinfo_t; 1328 1329 typedef struct smb_streaminfo { 1330 uint64_t si_size; 1331 uint64_t si_alloc_size; 1332 char si_name[MAXPATHLEN]; 1333 } smb_streaminfo_t; 1334 1335 #define SMB_LOCK_MAGIC 0x4C4F434B /* 'LOCK' */ 1336 1337 typedef struct smb_lock { 1338 list_node_t l_lnd; 1339 uint32_t l_magic; 1340 kmutex_t l_mutex; 1341 kcondvar_t l_cv; 1342 1343 smb_ofile_t *l_file; 1344 1345 struct smb_lock *l_blocked_by; /* Debug info only */ 1346 1347 uint32_t l_conflicts; 1348 uint32_t l_flags; 1349 uint32_t l_pid; 1350 uint32_t l_type; 1351 uint64_t l_start; 1352 uint64_t l_length; 1353 clock_t l_end_time; 1354 } smb_lock_t; 1355 1356 #define SMB_LOCK_FLAG_INDEFINITE 0x0004 1357 #define SMB_LOCK_FLAG_CLOSED 0x0008 1358 #define SMB_LOCK_FLAG_CANCELLED 0x0010 1359 1360 #define SMB_LOCK_TYPE_READWRITE 101 1361 #define SMB_LOCK_TYPE_READONLY 102 1362 1363 typedef struct vardata_block { 1364 uint8_t vdb_tag; 1365 uint32_t vdb_len; 1366 struct uio vdb_uio; 1367 struct iovec vdb_iovec[MAX_IOVEC]; 1368 } smb_vdb_t; 1369 1370 #define SMB_WRMODE_WRITE_THRU 0x0001 1371 #define SMB_WRMODE_IS_STABLE(M) ((M) & SMB_WRMODE_WRITE_THRU) 1372 1373 #define SMB_RW_MAGIC 0x52445257 /* 'RDRW' */ 1374 1375 typedef struct smb_rw_param { 1376 uint32_t rw_magic; 1377 smb_vdb_t rw_vdb; 1378 uint64_t rw_offset; 1379 uint32_t rw_last_write; 1380 uint16_t rw_mode; 1381 uint32_t rw_count; /* bytes in this request */ 1382 uint16_t rw_mincnt; 1383 uint32_t rw_total; /* total bytes (write-raw) */ 1384 uint16_t rw_dsoff; /* SMB data offset */ 1385 uint8_t rw_andx; /* SMB secondary andx command */ 1386 } smb_rw_param_t; 1387 1388 typedef struct smb_pathname { 1389 char *pn_path; 1390 char *pn_pname; 1391 char *pn_fname; 1392 char *pn_sname; 1393 char *pn_stype; 1394 } smb_pathname_t; 1395 1396 /* 1397 * fs_query_info 1398 */ 1399 typedef struct smb_fqi { 1400 smb_pathname_t fq_path; 1401 uint16_t fq_sattr; 1402 smb_node_t *fq_dnode; 1403 smb_node_t *fq_fnode; 1404 smb_attr_t fq_fattr; 1405 char fq_last_comp[MAXNAMELEN]; 1406 } smb_fqi_t; 1407 1408 typedef struct dirop { 1409 smb_fqi_t fqi; 1410 smb_fqi_t dst_fqi; 1411 uint16_t info_level; 1412 uint16_t flags; 1413 } smb_arg_dirop_t; 1414 1415 typedef struct smb_queryinfo { 1416 smb_node_t *qi_node; /* NULL for pipes */ 1417 uint8_t qi_InfoType; 1418 uint8_t qi_InfoClass; 1419 uint8_t qi_delete_on_close; 1420 uint8_t qi_isdir; 1421 uint32_t qi_AddlInfo; 1422 uint32_t qi_Flags; 1423 mbuf_chain_t in_data; 1424 smb_attr_t qi_attr; 1425 uint32_t qi_namelen; 1426 char qi_shortname[SMB_SHORTNAMELEN]; 1427 char qi_name[MAXPATHLEN]; 1428 } smb_queryinfo_t; 1429 1430 typedef struct smb_setinfo { 1431 smb_node_t *si_node; 1432 mbuf_chain_t si_data; 1433 smb_attr_t si_attr; 1434 } smb_setinfo_t; 1435 1436 /* 1437 * smb_fssize_t 1438 * volume_units and volume avail are the total allocated and 1439 * available units on the volume. 1440 * caller_units and caller_avail are the allocated and available 1441 * units on the volume for the user associated with the calling 1442 * thread. 1443 */ 1444 typedef struct smb_fssize { 1445 uint64_t fs_volume_units; 1446 uint64_t fs_volume_avail; 1447 uint64_t fs_caller_units; 1448 uint64_t fs_caller_avail; 1449 uint32_t fs_sectors_per_unit; 1450 uint32_t fs_bytes_per_sector; 1451 } smb_fssize_t; 1452 1453 /* 1454 * SMB FsCtl operations (SMB2 Ioctl, and some SMB1 trans calls) 1455 */ 1456 typedef struct { 1457 uint32_t CtlCode; 1458 uint32_t InputCount; 1459 uint32_t OutputCount; 1460 uint32_t MaxOutputResp; 1461 mbuf_chain_t *in_mbc; 1462 mbuf_chain_t *out_mbc; 1463 } smb_fsctl_t; 1464 1465 typedef struct { 1466 uint64_t persistent; 1467 uint64_t temporal; 1468 } smb2fid_t; 1469 1470 typedef struct { 1471 uint32_t status; 1472 uint16_t errcls; 1473 uint16_t errcode; 1474 } smb_error_t; 1475 1476 typedef struct open_param { 1477 smb_fqi_t fqi; 1478 uint16_t omode; 1479 uint16_t ofun; 1480 uint32_t nt_flags; 1481 uint32_t timeo; 1482 uint32_t dattr; 1483 timestruc_t crtime; 1484 timestruc_t mtime; 1485 timestruc_t timewarp; 1486 /* 1487 * Careful: dsize is the desired (allocation) size before the 1488 * common open function, and the actual size afterwards. 1489 */ 1490 uint64_t dsize; /* alloc size, actual size */ 1491 uint32_t desired_access; 1492 uint32_t share_access; 1493 uint32_t create_options; 1494 uint32_t create_disposition; 1495 boolean_t create_timewarp; 1496 boolean_t created_readonly; 1497 uint32_t ftype; 1498 uint32_t devstate; 1499 uint32_t action_taken; 1500 uint64_t fileid; 1501 uint32_t rootdirfid; 1502 smb_ofile_t *dir; 1503 smb_opipe_t *pipe; /* for smb_opipe_open */ 1504 struct smb_sd *sd; /* for NTTransactCreate */ 1505 uint8_t op_oplock_level; /* requested/granted level */ 1506 boolean_t op_oplock_levelII; /* TRUE if levelII supported */ 1507 } smb_arg_open_t; 1508 1509 typedef struct smb_arg_lock { 1510 void *lvec; 1511 uint32_t lcnt; 1512 uint32_t lseq; 1513 } smb_arg_lock_t; 1514 1515 struct smb_async_req; 1516 1517 /* 1518 * SMB Request State Machine 1519 * ------------------------- 1520 * 1521 * T4 +------+ T0 1522 * +--------------------------->| FREE |---------------------------+ 1523 * | +------+ | 1524 * +-----------+ | 1525 * | COMPLETED | | 1526 * +-----------+ 1527 * ^ | 1528 * | T15 +-----------+ v 1529 * +------------+ T6 | | +--------------+ 1530 * | CLEANED_UP |<-----------------| CANCELLED | | INITIALIZING | 1531 * +------------+ | | +--------------+ 1532 * | ^ +-----------+ | 1533 * | | ^ ^ ^ ^ | 1534 * | | +-------------+ | | | | 1535 * | | T3 | | | | T13 | T1 1536 * | +-------------------------+ | | +----------------------+ | 1537 * +----------------------------+ | | | | | 1538 * T16 | | | | +-----------+ | | 1539 * | \/ | | T5 | | v 1540 * +-----------------+ | T12 +--------+ | T2 +-----------+ 1541 * | EVENT_OCCURRED |------------->| ACTIVE |<--------------------| SUBMITTED | 1542 * +-----------------+ | +--------+ | +-----------+ 1543 * ^ | | ^ | | 1544 * | | T8 | | | T7 | 1545 * | T10 T9 | +----------+ | +-------+ | T11 1546 * | | | +-------+ | | 1547 * | | | T14 | | | 1548 * | | v | v | 1549 * +----------------------+ +--------------+ 1550 * | WAITING_EVENT | | WAITING_LOCK | 1551 * +----------------------+ +--------------+ 1552 * 1553 * 1554 * 1555 * 1556 * 1557 * Transition T0 1558 * 1559 * This transition occurs when the request is allocated and is still under the 1560 * control of the session thread. 1561 * 1562 * Transition T1 1563 * 1564 * This transition occurs when the session thread dispatches a task to treat the 1565 * request. 1566 * 1567 * Transition T2 1568 * 1569 * 1570 * 1571 * Transition T3 1572 * 1573 * A request completes and smbsr_cleanup is called to release resources 1574 * associated with the request (but not the smb_request_t itself). This 1575 * includes references on smb_ofile_t, smb_node_t, and other structures. 1576 * CLEANED_UP state exists to detect if we attempt to cleanup a request 1577 * multiple times and to allow us to detect that we are accessing a 1578 * request that has already been cleaned up. 1579 * 1580 * Transition T4 1581 * 1582 * 1583 * 1584 * Transition T5 1585 * 1586 * 1587 * 1588 * Transition T6 1589 * 1590 * 1591 * 1592 * Transition T7 1593 * 1594 * 1595 * 1596 * Transition T8 1597 * 1598 * 1599 * 1600 * Transition T9 1601 * 1602 * 1603 * 1604 * Transition T10 1605 * 1606 * 1607 * 1608 * Transition T11 1609 * 1610 * 1611 * 1612 * Transition T12 1613 * 1614 * 1615 * 1616 * Transition T13 1617 * 1618 * 1619 * 1620 * Transition T14 1621 * 1622 * 1623 * 1624 * Transition T15 1625 * 1626 * Request processing is completed (control returns from smb_dispatch) 1627 * 1628 * Transition T16 1629 * 1630 * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx" 1631 * sections remain to be processed. 1632 * 1633 */ 1634 1635 #define SMB_REQ_MAGIC 0x534D4252 /* 'SMBR' */ 1636 #define SMB_REQ_VALID(p) ASSERT((p)->sr_magic == SMB_REQ_MAGIC) 1637 1638 typedef enum smb_req_state { 1639 SMB_REQ_STATE_FREE = 0, 1640 SMB_REQ_STATE_INITIALIZING, 1641 SMB_REQ_STATE_SUBMITTED, 1642 SMB_REQ_STATE_ACTIVE, 1643 SMB_REQ_STATE_WAITING_AUTH, 1644 SMB_REQ_STATE_WAITING_FCN1, 1645 SMB_REQ_STATE_WAITING_FCN2, 1646 SMB_REQ_STATE_WAITING_LOCK, 1647 SMB_REQ_STATE_WAITING_PIPE, 1648 SMB_REQ_STATE_COMPLETED, 1649 SMB_REQ_STATE_CANCEL_PENDING, 1650 SMB_REQ_STATE_CANCELLED, 1651 SMB_REQ_STATE_CLEANED_UP, 1652 SMB_REQ_STATE_SENTINEL 1653 } smb_req_state_t; 1654 1655 typedef struct smb_request { 1656 list_node_t sr_session_lnd; 1657 uint32_t sr_magic; 1658 kmutex_t sr_mutex; 1659 smb_req_state_t sr_state; 1660 struct smb_server *sr_server; 1661 pid_t *sr_pid; 1662 int32_t sr_gmtoff; 1663 smb_session_t *session; 1664 smb_kmod_cfg_t *sr_cfg; 1665 void (*cancel_method)(struct smb_request *); 1666 void *cancel_arg2; 1667 1668 list_node_t sr_waiters; /* smb_notify.c */ 1669 1670 /* Info from session service header */ 1671 uint32_t sr_req_length; /* Excluding NBT header */ 1672 1673 /* Request buffer excluding NBT header */ 1674 void *sr_request_buf; 1675 1676 struct mbuf_chain command; 1677 struct mbuf_chain reply; 1678 struct mbuf_chain raw_data; 1679 list_t sr_storage; 1680 struct smb_xa *r_xa; 1681 int andx_prev_wct; 1682 int cur_reply_offset; 1683 int orig_request_hdr; 1684 unsigned int reply_seqnum; /* reply sequence number */ 1685 unsigned char first_smb_com; /* command code */ 1686 unsigned char smb_com; /* command code */ 1687 1688 uint8_t smb_rcls; /* error code class */ 1689 uint8_t smb_reh; /* rsvd (AH DOS INT-24 ERR) */ 1690 uint16_t smb_err; /* error code */ 1691 smb_error_t smb_error; 1692 1693 uint8_t smb_flg; /* flags */ 1694 uint16_t smb_flg2; /* flags */ 1695 unsigned char smb_sig[8]; /* signiture */ 1696 uint16_t smb_tid; /* tree id # */ 1697 uint32_t smb_pid; /* caller's process id # */ 1698 uint16_t smb_uid; /* user id # */ 1699 uint16_t smb_mid; /* mutiplex id # */ 1700 unsigned char smb_wct; /* count of parameter words */ 1701 uint16_t smb_bcc; /* data byte count */ 1702 1703 /* 1704 * Beginning offsets (in the mbuf chain) for the 1705 * command and reply headers, and the next reply. 1706 */ 1707 uint32_t smb2_cmd_hdr; 1708 uint32_t smb2_reply_hdr; 1709 uint32_t smb2_next_reply; 1710 1711 /* 1712 * SMB2 header fields. [MS-SMB2 2.2.1.2] 1713 * XXX: Later do a union w smb1 members 1714 */ 1715 uint16_t smb2_credit_charge; 1716 uint16_t smb2_chan_seq; /* cmd only */ 1717 uint32_t smb2_status; 1718 uint16_t smb2_cmd_code; 1719 uint16_t smb2_credit_request; 1720 uint16_t smb2_credit_response; 1721 uint16_t smb2_total_credits; /* in compound */ 1722 uint32_t smb2_hdr_flags; 1723 uint32_t smb2_next_command; 1724 uint64_t smb2_messageid; 1725 uint64_t smb2_first_msgid; 1726 /* uint32_t smb2_pid; use smb_pid */ 1727 /* uint32_t smb2_tid; use smb_tid */ 1728 /* uint64_t smb2_ssnid; use smb_uid */ 1729 unsigned char smb2_sig[16]; /* signiture */ 1730 1731 uint64_t smb2_async_id; 1732 struct smb2_async_req *sr_async_req; 1733 1734 /* Parameters */ 1735 struct mbuf_chain smb_vwv; /* variable width value */ 1736 1737 /* Data */ 1738 struct mbuf_chain smb_data; 1739 1740 uint16_t smb_fid; /* not in hdr, but common */ 1741 1742 unsigned char andx_com; 1743 uint16_t andx_off; 1744 1745 struct smb_tree *tid_tree; 1746 struct smb_ofile *fid_ofile; 1747 smb_user_t *uid_user; 1748 1749 cred_t *user_cr; 1750 kthread_t *sr_worker; 1751 hrtime_t sr_time_submitted; 1752 hrtime_t sr_time_active; 1753 hrtime_t sr_time_start; 1754 int32_t sr_txb; 1755 uint32_t sr_seqnum; 1756 1757 union { 1758 smb_arg_negotiate_t *negprot; 1759 smb_arg_sessionsetup_t *ssetup; 1760 smb_arg_tcon_t tcon; 1761 smb_arg_dirop_t dirop; 1762 smb_arg_open_t open; 1763 smb_arg_lock_t lock; 1764 smb_rw_param_t *rw; 1765 smb_oplock_grant_t olbrk; /* for async oplock break */ 1766 int32_t timestamp; 1767 } arg; 1768 } smb_request_t; 1769 1770 #define sr_ssetup arg.ssetup 1771 #define sr_negprot arg.negprot 1772 #define sr_tcon arg.tcon 1773 #define sr_dirop arg.dirop 1774 #define sr_open arg.open 1775 #define sr_rw arg.rw 1776 #define sr_timestamp arg.timestamp 1777 1778 #define SMB_READ_PROTOCOL(hdr) \ 1779 LE_IN32(((smb_hdr_t *)(hdr))->protocol) 1780 1781 #define SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \ 1782 (SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC) 1783 1784 #define SMB_READ_COMMAND(hdr) \ 1785 (((smb_hdr_t *)(hdr))->command) 1786 1787 #define SMB_IS_NT_CANCEL(rd_sr) \ 1788 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NT_CANCEL) 1789 1790 #define SMB_IS_SESSION_SETUP_ANDX(rd_sr) \ 1791 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == \ 1792 SMB_COM_SESSION_SETUP_ANDX) 1793 1794 #define SMB_IS_NT_NEGOTIATE(rd_sr) \ 1795 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NEGOTIATE) 1796 1797 #define SMB_IS_TREE_CONNECT_ANDX(rd_sr) \ 1798 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_TREE_CONNECT_ANDX) 1799 1800 #define SMB_XA_FLAG_OPEN 0x0001 1801 #define SMB_XA_FLAG_CLOSE 0x0002 1802 #define SMB_XA_FLAG_COMPLETE 0x0004 1803 #define SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN)) 1804 1805 #define SMB_XA_MAGIC 0x534D4258 /* 'SMBX' */ 1806 1807 typedef struct smb_xa { 1808 list_node_t xa_lnd; 1809 uint32_t xa_magic; 1810 kmutex_t xa_mutex; 1811 1812 uint32_t xa_refcnt; 1813 uint32_t xa_flags; 1814 1815 struct smb_session *xa_session; 1816 1817 unsigned char smb_com; /* which TRANS type */ 1818 unsigned char smb_flg; /* flags */ 1819 uint16_t smb_flg2; /* flags */ 1820 uint16_t smb_tid; /* tree id number */ 1821 uint32_t smb_pid; /* caller's process id */ 1822 uint16_t smb_uid; /* user id number */ 1823 uint32_t smb_func; /* NT_TRANS function */ 1824 1825 uint16_t xa_smb_mid; /* mutiplex id number */ 1826 uint16_t xa_smb_fid; /* TRANS2 secondary */ 1827 1828 unsigned int reply_seqnum; /* reply sequence number */ 1829 1830 uint32_t smb_tpscnt; /* total parameter bytes being sent */ 1831 uint32_t smb_tdscnt; /* total data bytes being sent */ 1832 uint32_t smb_mprcnt; /* max parameter bytes to return */ 1833 uint32_t smb_mdrcnt; /* max data bytes to return */ 1834 uint32_t smb_msrcnt; /* max setup words to return */ 1835 uint32_t smb_flags; /* additional information: */ 1836 /* bit 0 - if set, disconnect TID in smb_tid */ 1837 /* bit 1 - if set, transaction is one way */ 1838 /* (no final response) */ 1839 int32_t smb_timeout; /* number of milliseconds to await completion */ 1840 uint32_t smb_suwcnt; /* set up word count */ 1841 1842 char *xa_pipe_name; 1843 1844 /* 1845 * These are the param and data count received so far, 1846 * used to decide if the whole trans is here yet. 1847 */ 1848 int req_disp_param; 1849 int req_disp_data; 1850 1851 struct mbuf_chain req_setup_mb; 1852 struct mbuf_chain req_param_mb; 1853 struct mbuf_chain req_data_mb; 1854 1855 struct mbuf_chain rep_setup_mb; 1856 struct mbuf_chain rep_param_mb; 1857 struct mbuf_chain rep_data_mb; 1858 } smb_xa_t; 1859 1860 1861 #define SDDF_NO_FLAGS 0 1862 #define SDDF_SUPPRESS_TID 0x0001 1863 #define SDDF_SUPPRESS_UID 0x0002 1864 1865 /* 1866 * SMB dispatch return codes. 1867 */ 1868 typedef enum { 1869 SDRC_SUCCESS = 0, 1870 SDRC_ERROR, 1871 SDRC_DROP_VC, 1872 SDRC_NO_REPLY, 1873 SDRC_SR_KEPT, 1874 SDRC_NOT_IMPLEMENTED 1875 } smb_sdrc_t; 1876 1877 #define VAR_BCC ((short)-1) 1878 1879 #define SMB_SERVER_MAGIC 0x53534552 /* 'SSER' */ 1880 #define SMB_SERVER_VALID(s) \ 1881 ASSERT(((s) != NULL) && ((s)->sv_magic == SMB_SERVER_MAGIC)) 1882 1883 #define SMB_LISTENER_MAGIC 0x4C53544E /* 'LSTN' */ 1884 #define SMB_LISTENER_VALID(ld) \ 1885 ASSERT(((ld) != NULL) && ((ld)->ld_magic == SMB_LISTENER_MAGIC)) 1886 1887 typedef struct { 1888 uint32_t ld_magic; 1889 struct smb_server *ld_sv; 1890 smb_thread_t ld_thread; 1891 ksocket_t ld_so; 1892 in_port_t ld_port; 1893 int ld_family; 1894 struct sockaddr_in ld_sin; 1895 struct sockaddr_in6 ld_sin6; 1896 smb_llist_t ld_session_list; 1897 } smb_listener_daemon_t; 1898 1899 #define SMB_SSETUP_CMD "authentication" 1900 #define SMB_TCON_CMD "share mapping" 1901 #define SMB_OPIPE_CMD "pipe open" 1902 #define SMB_THRESHOLD_REPORT_THROTTLE 50 1903 typedef struct smb_cmd_threshold { 1904 char *ct_cmd; 1905 kmutex_t ct_mutex; 1906 volatile uint32_t ct_active_cnt; 1907 volatile uint32_t ct_blocked_cnt; 1908 uint32_t ct_threshold; 1909 uint32_t ct_timeout; /* milliseconds */ 1910 kcondvar_t ct_cond; 1911 } smb_cmd_threshold_t; 1912 1913 typedef struct { 1914 kstat_named_t ls_files; 1915 kstat_named_t ls_trees; 1916 kstat_named_t ls_users; 1917 } smb_server_legacy_kstat_t; 1918 1919 typedef enum smb_server_state { 1920 SMB_SERVER_STATE_CREATED = 0, 1921 SMB_SERVER_STATE_CONFIGURED, 1922 SMB_SERVER_STATE_RUNNING, 1923 SMB_SERVER_STATE_STOPPING, 1924 SMB_SERVER_STATE_DELETING, 1925 SMB_SERVER_STATE_SENTINEL 1926 } smb_server_state_t; 1927 1928 typedef struct { 1929 /* protected by sv_mutex */ 1930 kcondvar_t sp_cv; 1931 uint32_t sp_cnt; 1932 smb_llist_t sp_list; 1933 smb_llist_t sp_fidlist; 1934 } smb_spool_t; 1935 1936 #define SMB_SERVER_STATE_VALID(S) \ 1937 ASSERT(((S) == SMB_SERVER_STATE_CREATED) || \ 1938 ((S) == SMB_SERVER_STATE_CONFIGURED) || \ 1939 ((S) == SMB_SERVER_STATE_RUNNING) || \ 1940 ((S) == SMB_SERVER_STATE_STOPPING) || \ 1941 ((S) == SMB_SERVER_STATE_DELETING)) 1942 1943 typedef struct smb_server { 1944 list_node_t sv_lnd; 1945 uint32_t sv_magic; 1946 kcondvar_t sv_cv; 1947 kmutex_t sv_mutex; 1948 smb_server_state_t sv_state; 1949 uint32_t sv_refcnt; 1950 pid_t sv_pid; 1951 zoneid_t sv_zid; 1952 smb_listener_daemon_t sv_nbt_daemon; 1953 smb_listener_daemon_t sv_tcp_daemon; 1954 krwlock_t sv_cfg_lock; 1955 smb_kmod_cfg_t sv_cfg; 1956 smb_session_t *sv_session; 1957 1958 struct smb_export sv_export; 1959 struct __door_handle *sv_lmshrd; 1960 1961 /* Internal door for up-calls to smbd */ 1962 struct __door_handle *sv_kdoor_hd; 1963 int sv_kdoor_id; /* init -1 */ 1964 uint64_t sv_kdoor_ncall; 1965 kmutex_t sv_kdoor_mutex; 1966 kcondvar_t sv_kdoor_cv; 1967 1968 int32_t si_gmtoff; 1969 1970 smb_thread_t si_thread_timers; 1971 1972 taskq_t *sv_worker_pool; 1973 taskq_t *sv_receiver_pool; 1974 1975 smb_node_t *si_root_smb_node; 1976 smb_llist_t sv_opipe_list; 1977 smb_llist_t sv_event_list; 1978 1979 /* Statistics */ 1980 hrtime_t sv_start_time; 1981 kstat_t *sv_ksp; 1982 volatile uint32_t sv_nbt_sess; 1983 volatile uint32_t sv_tcp_sess; 1984 volatile uint32_t sv_users; 1985 volatile uint32_t sv_trees; 1986 volatile uint32_t sv_files; 1987 volatile uint32_t sv_pipes; 1988 volatile uint64_t sv_txb; 1989 volatile uint64_t sv_rxb; 1990 volatile uint64_t sv_nreq; 1991 smb_srqueue_t sv_srqueue; 1992 smb_spool_t sp_info; 1993 smb_cmd_threshold_t sv_ssetup_ct; 1994 smb_cmd_threshold_t sv_tcon_ct; 1995 smb_cmd_threshold_t sv_opipe_ct; 1996 kstat_t *sv_legacy_ksp; 1997 kmutex_t sv_legacy_ksmtx; 1998 smb_disp_stats_t *sv_disp_stats1; 1999 smb_disp_stats_t *sv_disp_stats2; 2000 } smb_server_t; 2001 2002 #define SMB_EVENT_MAGIC 0x45564E54 /* EVNT */ 2003 #define SMB_EVENT_TIMEOUT 45 /* seconds */ 2004 #define SMB_EVENT_VALID(e) \ 2005 ASSERT(((e) != NULL) && ((e)->se_magic == SMB_EVENT_MAGIC)) 2006 typedef struct smb_event { 2007 list_node_t se_lnd; 2008 uint32_t se_magic; 2009 kmutex_t se_mutex; 2010 kcondvar_t se_cv; 2011 smb_server_t *se_server; 2012 uint32_t se_txid; 2013 boolean_t se_notified; 2014 int se_waittime; 2015 int se_timeout; 2016 int se_errno; 2017 } smb_event_t; 2018 2019 typedef struct smb_kspooldoc { 2020 list_node_t sd_lnd; 2021 uint32_t sd_magic; 2022 smb_inaddr_t sd_ipaddr; 2023 uint32_t sd_spool_num; 2024 uint16_t sd_fid; 2025 char sd_username[MAXNAMELEN]; 2026 char sd_path[MAXPATHLEN]; 2027 } smb_kspooldoc_t; 2028 2029 typedef struct smb_spoolfid { 2030 list_node_t sf_lnd; 2031 uint32_t sf_magic; 2032 uint16_t sf_fid; 2033 } smb_spoolfid_t; 2034 2035 #define SMB_INFO_NETBIOS_SESSION_SVC_RUNNING 0x0001 2036 #define SMB_INFO_NETBIOS_SESSION_SVC_FAILED 0x0002 2037 #define SMB_INFO_USER_LEVEL_SECURITY 0x40000000 2038 #define SMB_INFO_ENCRYPT_PASSWORDS 0x80000000 2039 2040 #define SMB_IS_STREAM(node) ((node)->n_unode) 2041 2042 typedef struct smb_tsd { 2043 void (*proc)(); 2044 void *arg; 2045 char name[100]; 2046 } smb_tsd_t; 2047 2048 typedef struct smb_disp_entry { 2049 char sdt_name[KSTAT_STRLEN]; 2050 smb_sdrc_t (*sdt_pre_op)(smb_request_t *); 2051 smb_sdrc_t (*sdt_function)(smb_request_t *); 2052 void (*sdt_post_op)(smb_request_t *); 2053 uint8_t sdt_com; 2054 char sdt_dialect; 2055 uint8_t sdt_flags; 2056 } smb_disp_entry_t; 2057 2058 typedef struct smb_xlate { 2059 int code; 2060 char *str; 2061 } smb_xlate_t; 2062 2063 /* 2064 * This structure is a helper for building RAP NetShareEnum response 2065 * 2066 * es_posix_uid UID of the user requesting the shares list which 2067 * is used to detect if the user has any autohome 2068 * es_bufsize size of the response buffer 2069 * es_buf pointer to the response buffer 2070 * es_ntotal total number of shares exported by server which 2071 * their OEM names is less then 13 chars 2072 * es_nsent number of shares that can fit in the specified buffer 2073 * es_datasize actual data size (share's data) which was encoded 2074 * in the response buffer 2075 */ 2076 typedef struct smb_enumshare_info { 2077 uid_t es_posix_uid; 2078 uint16_t es_bufsize; 2079 char *es_buf; 2080 uint16_t es_ntotal; 2081 uint16_t es_nsent; 2082 uint16_t es_datasize; 2083 } smb_enumshare_info_t; 2084 2085 #ifdef __cplusplus 2086 } 2087 #endif 2088 2089 #endif /* _SMBSRV_SMB_KTYPES_H */ 2090