|
ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
|
SSH connection protocol - multiplexed "session" channels (RFC 4254). More...
Go to the source code of this file.
Classes | |
| struct | SshChannel |
| Per-connection channel state. More... | |
Typedefs | |
| using | SshChannelDataCb = void(*)(uint8_t slot, uint32_t channel, const uint8_t *data, size_t len) |
| Application callback for inbound channel data (raw bytes), tagged with the channel id it arrived on. | |
| using | SshForwardOpenCb = int(*)(uint8_t slot, uint32_t channel, const char *host, size_t host_len, uint16_t port) |
"direct-tcpip" forward request: a client asked the server to open a TCP connection to host : port (ssh -L). The forwarding owner (which does the actual TCP I/O - this codec does not) decides whether to allow it; host is not NUL-terminated (host_len bytes). | |
| using | SshForwardDataCb = void(*)(uint8_t slot, uint32_t channel, const uint8_t *data, size_t len) |
| Inbound data on a direct-tcpip channel (the owner writes it to the forwarded TCP socket). Kept separate from the session data callback. | |
| using | SshRemoteForwardOpenCb = int(*)(uint8_t slot, const char *bind_addr, size_t addr_len, uint16_t bind_port) |
"tcpip-forward" remote-forward request (ssh -R): the client asks the server to listen on bind_addr : bind_port and open a channel back for each accepted connection (RFC 4254 §7.1). bind_addr is addr_len bytes (not NUL-terminated). The forwarding owner (which allocates the real listener - this codec does no I/O) decides. | |
| using | SshRemoteForwardCancelCb = int(*)(uint8_t slot, const char *bind_addr, size_t addr_len, uint16_t bind_port) |
| "cancel-tcpip-forward" request (RFC 4254 §7.1): drop a remote forward. | |
| using | SshForwardConfirmCb = void(*)(uint8_t slot, uint32_t channel, bool ok) |
Result of the client's reply to a server-initiated forwarded-tcpip channel: ok = true on CHANNEL_OPEN_CONFIRMATION (the bridge may start), false on CHANNEL_OPEN_FAILURE (the owner tears the bridge down). channel is the local id returned by pc_ssh_channel_open_forwarded(). | |
Enumerations | |
| enum class | SshChanType : uint8_t { SSH_CHAN_SESSION = 0 , SSH_CHAN_DIRECT_TCPIP = 1 , SSH_CHAN_FORWARDED_TCPIP = 2 , SSH_CHAN_SFTP = 3 , SSH_CHAN_SCP = 4 } |
| Channel type (RFC 4254). More... | |
Functions | |
| void | pc_ssh_channel_set_data_cb (SshChannelDataCb cb) |
| Install the inbound-data callback (session channels). | |
| void | pc_ssh_channel_set_forward_open_cb (SshForwardOpenCb cb) |
| Install the direct-tcpip forward open-policy callback (opt-in). | |
| void | pc_ssh_channel_set_forward_data_cb (SshForwardDataCb cb) |
| Install the direct-tcpip forward inbound-data callback. | |
| void | pc_ssh_channel_set_rforward_open_cb (SshRemoteForwardOpenCb cb) |
| Install the remote-forward (ssh -R) open-policy callback (opt-in). | |
| void | pc_ssh_channel_set_rforward_cancel_cb (SshRemoteForwardCancelCb cb) |
| Install the remote-forward (ssh -R) cancel callback (opt-in). | |
| void | pc_ssh_channel_set_forward_confirm_cb (SshForwardConfirmCb cb) |
| Install the forwarded-tcpip open-confirmation callback (opt-in, ssh -R). | |
| int | pc_ssh_channel_open_forwarded (uint8_t i, const char *conn_addr, uint16_t conn_port, const char *orig_addr, uint16_t orig_port, uint8_t *out, size_t *out_len, size_t cap) |
| Open a server-initiated "forwarded-tcpip" channel (RFC 4254 §7.2, ssh -R). | |
| int | pc_ssh_channel_handle_open_confirm (uint8_t i, const uint8_t *payload, size_t len) |
| Handle SSH_MSG_CHANNEL_OPEN_CONFIRMATION for a channel we opened (ssh -R). | |
| int | pc_ssh_channel_handle_open_failure (uint8_t i, const uint8_t *payload, size_t len) |
| Handle SSH_MSG_CHANNEL_OPEN_FAILURE for a channel we opened (ssh -R). | |
| int | ssh_global_request_handle (uint8_t i, const uint8_t *payload, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
| Handle SSH_MSG_GLOBAL_REQUEST (RFC 4254 §4). | |
| void | pc_ssh_channel_init (uint8_t i) |
Reset channel state for slot i. | |
| int | pc_ssh_channel_handle_open (uint8_t i, const uint8_t *payload, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
| Handle SSH_MSG_CHANNEL_OPEN and emit CHANNEL_OPEN_CONFIRMATION. | |
| int | pc_ssh_channel_handle_request (uint8_t i, const uint8_t *payload, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
| Handle SSH_MSG_CHANNEL_REQUEST. | |
| int | pc_ssh_channel_handle_data (uint8_t i, const uint8_t *payload, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
Handle SSH_MSG_CHANNEL_DATA: bounds-check, update the window, and invoke the data callback. If the local window is exhausted a CHANNEL_WINDOW_ADJUST is written to out (*out_len > 0). | |
| int | pc_ssh_channel_build_data (uint8_t i, uint32_t channel, const uint8_t *data, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
Build an SSH_MSG_CHANNEL_DATA message carrying data to the client on channel channel (a local channel id from a prior open). | |
| int | pc_ssh_channel_handle_window_adjust (uint8_t i, const uint8_t *payload, size_t len) |
| Handle SSH_MSG_CHANNEL_WINDOW_ADJUST (grows the peer window). | |
| int | pc_ssh_channel_build_close (uint8_t i, uint32_t channel, uint8_t *out, size_t *out_len, size_t cap) |
Build SSH_MSG_CHANNEL_EOF + SSH_MSG_CHANNEL_CLOSE for channel channel and mark it closed. | |
| int | pc_ssh_channel_handle_close (uint8_t i, const uint8_t *payload, size_t len, uint8_t *out, size_t *out_len, size_t cap) |
| Handle an inbound SSH_MSG_CHANNEL_CLOSE: route to the recipient channel, reply with EOF + CLOSE, and mark it closed. | |
Variables | |
| SshChannel | ssh_chan [MAX_SSH_CONNS][PC_SSH_MAX_CHANNELS] |
| Channel pool: PC_SSH_MAX_CHANNELS channels per SSH connection (BSS). Owned by this layer; src/ code routes through the functions below, never the array (tests inspect it white-box). Index: [connection slot][channel slot]. | |
SSH connection protocol - multiplexed "session" channels (RFC 4254).
After authentication the client opens one or more channels; this layer confirms each, accepts a shell/exec/pty request, and exchanges SSH_MSG_CHANNEL_DATA. Inbound channel data is surfaced to the application as a raw byte stream (no PTY emulation), tagged with its channel id; outbound data is framed back to the client on a given channel. Flow control follows RFC 4254 §5.2 (per-channel window tracking + WINDOW_ADJUST).
Up to PC_SSH_MAX_CHANNELS channels per connection are multiplexed, each with its own id, peer id, and windows. The default (1) is the original single-channel control/console link. Every inbound message is routed to its channel by the recipient channel id it carries.
Definition in file ssh_channel.h.
| using SshChannelDataCb = void (*)(uint8_t slot, uint32_t channel, const uint8_t *data, size_t len) |
Application callback for inbound channel data (raw bytes), tagged with the channel id it arrived on.
Definition at line 61 of file ssh_channel.h.
| using SshForwardOpenCb = int (*)(uint8_t slot, uint32_t channel, const char *host, size_t host_len, uint16_t port) |
"direct-tcpip" forward request: a client asked the server to open a TCP connection to host : port (ssh -L). The forwarding owner (which does the actual TCP I/O - this codec does not) decides whether to allow it; host is not NUL-terminated (host_len bytes).
If no callback is installed, all forward requests are refused - so forwarding is opt-in (no open relay by default).
Definition at line 77 of file ssh_channel.h.
| using SshForwardDataCb = void (*)(uint8_t slot, uint32_t channel, const uint8_t *data, size_t len) |
Inbound data on a direct-tcpip channel (the owner writes it to the forwarded TCP socket). Kept separate from the session data callback.
Definition at line 81 of file ssh_channel.h.
| using SshRemoteForwardOpenCb = int (*)(uint8_t slot, const char *bind_addr, size_t addr_len, uint16_t bind_port) |
"tcpip-forward" remote-forward request (ssh -R): the client asks the server to listen on bind_addr : bind_port and open a channel back for each accepted connection (RFC 4254 §7.1). bind_addr is addr_len bytes (not NUL-terminated). The forwarding owner (which allocates the real listener - this codec does no I/O) decides.
bind_port, or the port the owner picked when bind_port == 0), or < 0 to refuse. If no callback is installed every request is refused, so remote forwarding is opt-in (no listener is opened). Definition at line 99 of file ssh_channel.h.
| using SshRemoteForwardCancelCb = int (*)(uint8_t slot, const char *bind_addr, size_t addr_len, uint16_t bind_port) |
"cancel-tcpip-forward" request (RFC 4254 §7.1): drop a remote forward.
Definition at line 103 of file ssh_channel.h.
| using SshForwardConfirmCb = void (*)(uint8_t slot, uint32_t channel, bool ok) |
Result of the client's reply to a server-initiated forwarded-tcpip channel: ok = true on CHANNEL_OPEN_CONFIRMATION (the bridge may start), false on CHANNEL_OPEN_FAILURE (the owner tears the bridge down). channel is the local id returned by pc_ssh_channel_open_forwarded().
Definition at line 117 of file ssh_channel.h.
|
strong |
Channel type (RFC 4254).
Definition at line 32 of file ssh_channel.h.
| void pc_ssh_channel_set_data_cb | ( | SshChannelDataCb | cb | ) |
Install the inbound-data callback (session channels).
Definition at line 42 of file ssh_channel.cpp.
References SshChannelCtx::data_cb.
| void pc_ssh_channel_set_forward_open_cb | ( | SshForwardOpenCb | cb | ) |
Install the direct-tcpip forward open-policy callback (opt-in).
Definition at line 69 of file ssh_channel.cpp.
References SshChannelCtx::forward_open_cb.
| void pc_ssh_channel_set_forward_data_cb | ( | SshForwardDataCb | cb | ) |
Install the direct-tcpip forward inbound-data callback.
Definition at line 74 of file ssh_channel.cpp.
References SshChannelCtx::forward_data_cb.
| void pc_ssh_channel_set_rforward_open_cb | ( | SshRemoteForwardOpenCb | cb | ) |
Install the remote-forward (ssh -R) open-policy callback (opt-in).
Definition at line 79 of file ssh_channel.cpp.
References SshChannelCtx::rfwd_open_cb.
| void pc_ssh_channel_set_rforward_cancel_cb | ( | SshRemoteForwardCancelCb | cb | ) |
Install the remote-forward (ssh -R) cancel callback (opt-in).
Definition at line 84 of file ssh_channel.cpp.
References SshChannelCtx::rfwd_cancel_cb.
| void pc_ssh_channel_set_forward_confirm_cb | ( | SshForwardConfirmCb | cb | ) |
Install the forwarded-tcpip open-confirmation callback (opt-in, ssh -R).
Definition at line 89 of file ssh_channel.cpp.
References SshChannelCtx::forward_confirm_cb.
| int pc_ssh_channel_open_forwarded | ( | uint8_t | i, |
| const char * | conn_addr, | ||
| uint16_t | conn_port, | ||
| const char * | orig_addr, | ||
| uint16_t | orig_port, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Open a server-initiated "forwarded-tcpip" channel (RFC 4254 §7.2, ssh -R).
Allocates a local channel on connection i (state = pending, awaiting the client's confirmation) and builds the SSH_MSG_CHANNEL_OPEN in out. conn_addr / conn_port are the forward's bound address/port (the "address
that was connected"); orig_addr / orig_port are the peer that connected.
out too small). On success the caller emits out and, on the eventual confirm, bridges bytes on the returned channel. Definition at line 328 of file ssh_channel.cpp.
References SshChannel::local_id, SshChannel::local_window, MAX_SSH_CONNS, SshChannel::open, SshChannel::peer_id, SshChannel::peer_max_pkt, SshChannel::peer_window, SshChannel::pending, ssh_chan, SSH_CHAN_FORWARDED_TCPIP, SSH_CHAN_MAX_PACKET, SSH_CHAN_WINDOW, SSH_MSG_CHANNEL_OPEN, and SshChannel::type.
Referenced by pc_ssh_conn_open_forwarded().
| int pc_ssh_channel_handle_open_confirm | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len | ||
| ) |
Handle SSH_MSG_CHANNEL_OPEN_CONFIRMATION for a channel we opened (ssh -R).
Matches the pending channel by our recipient id, records the peer's channel id / window / max-packet, and marks it open. Fires the confirm callback (ok = true).
Definition at line 386 of file ssh_channel.cpp.
References SshChannelCtx::forward_confirm_cb, SshChannel::local_id, MAX_SSH_CONNS, SshChannel::open, SshChannel::peer_id, SshChannel::peer_max_pkt, SshChannel::peer_window, SshChannel::pending, and SSH_MSG_CHANNEL_OPEN_CONFIRM.
Referenced by pc_ssh_server_dispatch().
| int pc_ssh_channel_handle_open_failure | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len | ||
| ) |
Handle SSH_MSG_CHANNEL_OPEN_FAILURE for a channel we opened (ssh -R).
Frees the pending channel and fires the confirm callback (ok = false).
Definition at line 410 of file ssh_channel.cpp.
References SshChannelCtx::forward_confirm_cb, SshChannel::local_id, MAX_SSH_CONNS, SshChannel::open, SshChannel::pending, and SSH_MSG_CHANNEL_OPEN_FAILURE.
Referenced by pc_ssh_server_dispatch().
| int ssh_global_request_handle | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Handle SSH_MSG_GLOBAL_REQUEST (RFC 4254 §4).
Parses the request name and want_reply flag. "tcpip-forward" / "cancel-tcpip-forward" are routed to the remote-forward seam above (accepted only when a callback is installed); a "tcpip-forward" that bound port 0 gets its allocated port echoed in the reply (RFC 4254 §7.1). Any other request name is unrecognized: per §4 it is answered with SSH_MSG_REQUEST_FAILURE when want_reply is set, and silently ignored otherwise (never SSH_MSG_UNIMPLEMENTED - GLOBAL_REQUEST is a known message type; only the request name is unknown).
out with *out_len bytes, or *out_len is 0 when no reply is due), -1 if the message is malformed. Definition at line 222 of file ssh_channel.cpp.
References MAX_SSH_CONNS, SshChannelCtx::rfwd_cancel_cb, SshChannelCtx::rfwd_open_cb, SSH_MSG_GLOBAL_REQUEST, SSH_MSG_REQUEST_FAILURE, and SSH_MSG_REQUEST_SUCCESS.
Referenced by pc_ssh_server_dispatch().
| void pc_ssh_channel_init | ( | uint8_t | i | ) |
Reset channel state for slot i.
Definition at line 94 of file ssh_channel.cpp.
References MAX_SSH_CONNS, and ssh_chan.
Referenced by pc_ssh_conn_accept().
| int pc_ssh_channel_handle_open | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Handle SSH_MSG_CHANNEL_OPEN and emit CHANNEL_OPEN_CONFIRMATION.
Accepts a "session" channel; any other type yields CHANNEL_OPEN_FAILURE.
Definition at line 432 of file ssh_channel.cpp.
References SshChannelCtx::forward_open_cb, SshChannel::local_id, SshChannel::local_window, MAX_SSH_CONNS, SshChannel::open, SshChannel::peer_id, SshChannel::peer_max_pkt, SshChannel::peer_window, ssh_chan, SSH_CHAN_DIRECT_TCPIP, SSH_CHAN_SESSION, SSH_CHAN_WINDOW, SSH_MSG_CHANNEL_OPEN, and SshChannel::type.
Referenced by pc_ssh_server_dispatch().
| int pc_ssh_channel_handle_request | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Handle SSH_MSG_CHANNEL_REQUEST.
"shell", "exec", "pty-req", and "env" are accepted; anything else is refused - except that when PC_ENABLE_SSH_SFTP is set a subsystem "sftp" is accepted (the channel is tagged SSH_CHAN_SFTP and the sftp-open callback fires), and when PC_ENABLE_SSH_SCP is set an exec "scp …" is additionally tagged SSH_CHAN_SCP (the scp-open callback fires with the command). When want_reply is set, CHANNEL_SUCCESS / CHANNEL_FAILURE is written to out and *out_len > 0; otherwise *out_len is 0.
Definition at line 560 of file ssh_channel.cpp.
References MAX_SSH_CONNS, SshChannel::peer_id, SSH_MSG_CHANNEL_FAILURE, SSH_MSG_CHANNEL_REQUEST, and SSH_MSG_CHANNEL_SUCCESS.
Referenced by pc_ssh_server_dispatch().
| int pc_ssh_channel_handle_data | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Handle SSH_MSG_CHANNEL_DATA: bounds-check, update the window, and invoke the data callback. If the local window is exhausted a CHANNEL_WINDOW_ADJUST is written to out (*out_len > 0).
Definition at line 621 of file ssh_channel.cpp.
References SshChannelCtx::data_cb, SshChannelCtx::forward_data_cb, SshChannel::local_id, SshChannel::local_window, MAX_SSH_CONNS, SshChannel::peer_id, SSH_CHAN_DIRECT_TCPIP, SSH_CHAN_FORWARDED_TCPIP, SSH_CHAN_SCP, SSH_CHAN_SFTP, SSH_CHAN_WINDOW, SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_WINDOW_ADJUST, and SshChannel::type.
Referenced by pc_ssh_server_dispatch().
| int pc_ssh_channel_build_data | ( | uint8_t | i, |
| uint32_t | channel, | ||
| const uint8_t * | data, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Build an SSH_MSG_CHANNEL_DATA message carrying data to the client on channel channel (a local channel id from a prior open).
out is too small. Definition at line 710 of file ssh_channel.cpp.
References MAX_SSH_CONNS, SshChannel::peer_id, SshChannel::peer_max_pkt, SshChannel::peer_window, and SSH_MSG_CHANNEL_DATA.
Referenced by pc_ssh_conn_send().
| int pc_ssh_channel_handle_window_adjust | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len | ||
| ) |
Handle SSH_MSG_CHANNEL_WINDOW_ADJUST (grows the peer window).
Definition at line 740 of file ssh_channel.cpp.
References MAX_SSH_CONNS, SshChannel::peer_window, and SSH_MSG_CHANNEL_WINDOW_ADJUST.
Referenced by pc_ssh_server_dispatch().
| int pc_ssh_channel_build_close | ( | uint8_t | i, |
| uint32_t | channel, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Build SSH_MSG_CHANNEL_EOF + SSH_MSG_CHANNEL_CLOSE for channel channel and mark it closed.
out is too small. Definition at line 780 of file ssh_channel.cpp.
References MAX_SSH_CONNS.
Referenced by pc_ssh_conn_close_channel().
| int pc_ssh_channel_handle_close | ( | uint8_t | i, |
| const uint8_t * | payload, | ||
| size_t | len, | ||
| uint8_t * | out, | ||
| size_t * | out_len, | ||
| size_t | cap | ||
| ) |
Handle an inbound SSH_MSG_CHANNEL_CLOSE: route to the recipient channel, reply with EOF + CLOSE, and mark it closed.
Definition at line 789 of file ssh_channel.cpp.
References MAX_SSH_CONNS, and SSH_MSG_CHANNEL_CLOSE.
Referenced by pc_ssh_server_dispatch().
|
extern |
Channel pool: PC_SSH_MAX_CHANNELS channels per SSH connection (BSS). Owned by this layer; src/ code routes through the functions below, never the array (tests inspect it white-box). Index: [connection slot][channel slot].
Definition at line 18 of file ssh_channel.cpp.
Referenced by pc_ssh_channel_handle_open(), pc_ssh_channel_init(), and pc_ssh_channel_open_forwarded().