Expand description
SMB2 protocol loop ([MS-SMB2]).
Implements the full file-serving command surface on top of the shared
Vfs abstraction: NEGOTIATE, SESSION_SETUP (both NTLMSSP
legs), TREE_CONNECT/DISCONNECT, LOGOFF, CREATE, READ, WRITE, CLOSE, FLUSH,
LOCK, QUERY_DIRECTORY, QUERY_INFO and SET_INFO. Frames whose magic is
\xFESMB route here from crate::dispatch.
StructsΒ§
- Async
Crypto π - Per-session crypto material an async completion needs to sign/seal a frame off the request path (snapshotted at request time so the background task never borrows the connection).
- Async
Reply π - Plumbing every async-deferred command reply (LOCK / CHANGE_NOTIFY) shares: crypto material to sign/seal the final frame, the requestβs identifiers, the outbound queue to send it on, and its cancellation signal.
- Smb2
Conn - Per-connection SMB2 state.
EnumsΒ§
- Async
Start π - Block until a conflicting byte-range lock is released and the requested ranges can be acquired, then emit the final LOCK response ([MS-SMB2] Β§3.3.5.14). Cancellation yields STATUS_CANCELLED. How an async-capable command (LOCK / CHANGE_NOTIFY) starts: either an immediate reply, or a deferral parked under an async id whose final reply a background worker sends later ([MS-SMB2] Β§3.3.4.2).
- Ioctl
Reply π - The outcome of an IOCTL: either a reply body to encode, or silence because the connection is being terminated ([MS-SMB2] Β§3.3.5.15.12).
- Negotiate
Reply π - Handle one leg of SPNEGO/NTLMSSP session establishment ([MS-SMB2] Β§3.3.5.5). How a NEGOTIATE resolves: an immediate reply, or silence because a repeat NEGOTIATE on an already-negotiated connection terminates it ([MS-SMB2] Β§3.3.5.4).
- Routed π
- Route a request through the typestate pipeline (io::dispatch) and return the
(status, body)the legacy framing still wraps. Used by commands migrated ontoio::Command(docs/typestate_plan.md Phases 2β3). How a typestate dispatch resolves forprocess_single: a body that still flows the common signing/sealing tail, a pre-framed async PDU to send verbatim (the STATUS_PENDING interim), or nothing at all.
ConstantsΒ§
- KNOWN_
PIPES π - Known pipe names served on IPC$.
- LEASE_
ACK_ πTIMEOUT - How long a conflicting open waits for a lease-break acknowledgment before proceeding ([MS-SMB2] Β§3.3.1.4). Kept short under test to keep unit tests fast (no real client acks there).
- MAX_
TRANSACT_ πSIZE - MaxTransactSize advertised in our NEGOTIATE response (1 MiB); CHANGE_NOTIFY rejects an OutputBufferLength larger than this ([MS-SMB2] Β§3.3.5.19).
FunctionsΒ§
- arbitrate_
lease π - Arbitrate a caching lease for a lease-requesting CREATE ([MS-SMB2] Β§2.2.13.2). A sole opener gets the requested state (capped at RWH); an open reusing the same lease key shares the existing state; READ and HANDLE caching are shareable, so directory holders with distinct keys coexist, whereas a contending file open strips write caching from existing holders (RWH -> RH) via unsolicited LEASE_BREAKs and is itself capped at RH.
- begin_
change_ πnotify - Begin a CHANGE_NOTIFY ([MS-SMB2] Β§3.3.5.19): validate the directory open, register the watch, and defer with an interim STATUS_PENDING.
- begin_
lock π - Begin a LOCK ([MS-SMB2] Β§3.3.5.14): apply unlocks, try acquisitions, and either answer immediately or (for a blocking lock) spawn a waiter and defer.
- break_
crypto π - Snapshot the crypto material needed to protect an oplock break sent later to this holder.
- break_
dir_ πlease - Revoke caching bits (
clear) on a directory lease held ondirwhen its contents or state change, unless the change originates from the lease holder itself ([MS-SMB2] Β§3.3.1.4).owneris the file id of the open performing the change, used to suppress a self-break. - break_
dir_ πlease_ wait - Break a directory lease and wait (briefly) for the holder to acknowledge, as a HANDLE-caching break requires before the conflicting operation may proceed ([MS-SMB2] Β§3.3.1.4).
- break_
subtree_ πlease_ wait - Break HANDLE caching on every directory lease across the subtree rooted at
dirand wait (briefly) for each holder to acknowledge, as required before a directory rename that invalidates the subtreeβs cached handles may be evaluated ([MS-SMB2] Β§3.3.1.4).owneris the renaming open, suppressed from the break. - build_
async_ πframe - Build an async-header frame ([MS-SMB2] Β§3.3.4.2 async mode): SERVER_TO_REDIR|ASYNC_COMMAND flags with the AsyncId occupying the Reserved+TreeId slot. Returned unsigned and unsealed.
- build_
break_ πframe - Build a server-initiated OPLOCK_BREAK frame (unsolicited: MessageId all-ones, sync header). Returned unsigned and unsealed.
- build_
interface_ πlist - Encode NETWORK_INTERFACE_INFO entries (RSS/RDMA both disabled, 10 Gbps),
chaining
Nextlinks; each entry is a fixed 152 bytes (8-byte aligned). - cancel π
- Handle a CANCEL ([MS-SMB2] Β§3.3.5.16): find the pending op by AsyncId (async CANCEL) or MessageId (sync CANCEL) and signal it to complete with STATUS_CANCELLED. CANCEL itself carries no response.
- cipher_
is_ π256 - True for the AES-256 cipher variants (32-byte keys).
- cipher_
is_ πgcm - True for the GCM cipher variants ([MS-SMB2] Β§2.2.3.1.2).
- cipher_
key_ πlen - Key length in bytes for a negotiated cipher.
- close π
- close_
all_ πhandles - collect_
subdirs π - Collect subdirectories under
rootas(absolute_path, backslash_relative)pairs for recursive CHANGE_NOTIFY, capped to bound very deep trees. - copychunk_
total π - Read the TotalBytesWritten field out of a SRV_COPYCHUNK_RESPONSE body.
- create π
- decrypt_
transform π - do_
copychunk π - Perform a server-side copy: for each chunk read from the source open named
by the resume key and write into the target handle. Returns the
SRV_COPYCHUNK_RESPONSE body, or
(status, body)where the body carries the server limits on a limits violation ([MS-SMB2] Β§3.3.5.15.6). - durable_
reconnect π - Reclaim a preserved durable handle and re-open the file under its persistent id (single-node durable model: the on-disk file, not a live fd, is durable).
- durable_
reconnect_ πids - Extract
(persistent_id, create_guid)when a CREATE carries a durable reconnect context. - encrypt_
response π - error_
resp π - Build the generic ERROR response body ([MS-SMB2] Β§2.2.2): StructureSize 9 with no error data. Every failed command carries this so clients can parse the frame (a bare header breaks their compound parser). The body is padded to the structureβs declared 8-byte footprint.
- file_
id_ πbody_ offset - Body-relative offset of the 16-byte FileId within a request that carries
one, used to resolve the wildcard FileId in a related compound follow-up
([MS-SMB2] Β§3.3.5.2.7.2).
Nonefor commands with no FileId field. - filter_
to_ πmask - Translate an SMB completion filter ([MS-SMB2] Β§2.2.35) into an inotify watch mask.
- finalize_
async π - Sign then (optionally) seal a fully-built async frame per the sessionβs protection, mirroring the request pathβs sign-then-encrypt order.
- finalize_
break π - Seal an oplock/lease break for its holder. Break notifications carry SessionId 0 and are sent unsigned ([MS-SMB2] Β§3.3.4.6); encrypted sessions still wrap them in a transform header.
- g16 π
- g32 π
- gmac_
nonce π - AES-GMAC nonce for signing ([MS-SMB2] Β§3.1.4.1): MessageId in the low 8 bytes; in the top 4 bytes bit 0 marks a server-sent message and bit 1 an SMB2 CANCEL request.
- grant_
durable π - Record a durable-handle grant on the connection and return the response
create-context to echo. Directories and one-shot deletes are ineligible.
A durable open also requires a batch oplock or a handle-caching lease, so
lease_state(the granted lease bits, if any) gates the grant; persistence is only granted on a continuous-availability share (is_ca). - handle_
multiprotocol_ negotiate - Respond to an SMB1 multi-protocol NEGOTIATE that carries the
\xFESMBdialect marker: builds the SMB2 negotiate response so the client upgrades. - hex_str π
- ioctl π
- IOCTL / FSCTL dispatch ([MS-SMB2] Β§3.3.5.15). Kept as a free function so the typestate handler and its private FSCTL helpers all live in this module.
- mask_
to_ πaction - Map an inotify event mask to a FILE_NOTIFY_INFORMATION action ([MS-FSCC] Β§2.7.1).
- message_
signature π - The 16-byte SMB2 signature over
msg(signature field pre-zeroed): the negotiated 3.x algorithm (AES-CMAC / AES-GMAC / HMAC-SHA256), or HMAC-SHA256 for 2.x ([MS-SMB2] Β§3.1.4.1).server_senderpicks the AES-GMAC nonce bit. - negotiate π
- Negotiate a dialect ([MS-SMB2] Β§3.3.5.3-4): answer probes with the wildcard
response, pick a common dialect, validate 3.1.1 pre-auth contexts, and choose
cipher/compression. The response is framed and pre-auth-hashed by the shared
tail in
process_single. - network_
interface_ πinfo - Build a NETWORK_INTERFACE_INFO list ([MS-SMB2] Β§2.2.32.5) from the hostβs interfaces so a multichannel client can discover additional server IPs.
- next_
file_ πid - Allocate a 16-byte SMB2 FileId (counter in the first quadword).
- next_
offload_ πid - A fresh 16-byte identifier embedded in an offload (ODX) token, keying the source bytes captured by FSCTL_OFFLOAD_READ.
- next_
resume_ πnonce - Server-side-copy nonce appended to a resume key so repeated keys on one open stay distinct.
- next_
session_ πid - next_
tree_ πid - Allocate a fresh TreeId.
- parent_
dir π - The parent directory portion of a normalized share-relative path, or the
share root (
"") when the path has no parent component. - pipe_
close π - Remove a pipe handle;
truewhen it was one. - pipe_
create π - Open a virtual pipe;
Err(FILE_NOT_FOUND)for unknown names. - pipe_
read π - Drain up to the requested byte count from a pipeβs outbound queue;
Nonewhenfile_idis not a pipe. - pipe_
write π - Feed client bytes into a pipe RPC dispatcher;
Nonewhenfile_idis not a pipe. Returns the WRITE response body. - probe_
negotiate_ πresp - Build the wildcard-dialect NEGOTIATE response used to answer the invalid-status probe ([MS-SMB2] Β§2.2.3.1.1): DialectRevision 0x02FF with all other fields zero.
- process_
frame π - Execute one frame (possibly compound) and build its response (
Noneto stay silent). Chained requests ([MS-SMB2] Β§3.3.5.2) are dispatched in order and their replies concatenated with 8-byte alignment. - process_
single π - Process exactly one SMB2 request starting at
buf(its own header). - query_
directory π - query_
info π - read π
- response π
- Build a 64-byte SMB2 response header + body, echoing MessageId/TreeId and stamping the effective session id ([MS-SMB2] Β§3.3.4.1).
- run_
change_ πnotify - Watch
dir_pathfor one filesystem change (or cancellation) and emit the final CHANGE_NOTIFY response ([MS-SMB2] Β§2.2.36) on the outbound queue. - run_
lock_ πwait - seal_
pdu π - Seal
msginto a transform frame with explicit key material so both the request path and background async tasks can encrypt without borrowing the whole connection. - send_
lease_ πbreak - Build, protect and enqueue an unsolicited LEASE_BREAK notification
([MS-SMB2] Β§2.2.23.2) asking a holder to drop from
currenttonew. - send_
oplock_ πbreak - Build, protect and enqueue an unsolicited OPLOCK_BREAK notification
([MS-SMB2] Β§2.2.23.1) telling
holderto break down tolevel. - serve_
client - Serve an SMB2 client connection until EOF/error (used when a connection starts life directly in SMB2 rather than upgrading through SMB1).
- session_
setup π - set_
info π - share_
is_ πca - True when the tree behind
tidis a continuously-available share, which may grant persistent handles ([MS-SMB2] Β§3.3.5.9.11). - share_
is_ πipc - True when the tree behind
tidis the virtual IPC$ share. - share_
vfs π - Resolve the VFS backing
tid, mirroring the SMB1 path (share by name, unknown trees rejected before handlers run). - sig_
gmac π - sign_
pdu π - Stamp an SMB2 signature over
respin place ([MS-SMB2] Β§3.3.4.1.1). Sets the SIGNED flag and fills the 16-byte Signature field (bytes 48..64). - tf_
off_ πend - End offset (relative to frame start) of transform-wrapped payload.
- tree_
connect π - verify_
channel_ πsequence - Verify the requestβs ChannelSequence against the openβs ([MS-SMB2]
Β§3.3.5.2.10), updating the openβs counters. Returns
falsewhen the caller must fail the WRITE/SET_INFO/IOCTL with STATUS_FILE_NOT_AVAILABLE. An open with no tracked state always passes. - verify_
pdu_ πsignature - Verify a request PDUβs signature against
key([MS-SMB2] Β§3.3.5.2.4): recompute the negotiated signature over the PDU with the signature field zeroed and compare to the headerβs Signature. - vfs_err π
- via_
typestate π - watch_
one_ πevent - Await the first inotify event on
dir_path(mapped from the SMB completion filter) or a cancellation. Whenwatch_treeis set, subdirectories are watched too and the reported name is relative todir_path. Returns the(action, name)list on an event, orErr(status)carrying the completion status on cancel/handle-close/setup error. - write π