1856ea928SPeter AvalosThis document describes the multiplexing protocol used by ssh(1)'s 2856ea928SPeter AvalosControlMaster connection-sharing. 3856ea928SPeter Avalos 4664f4763SzrjMultiplexing starts with a ssh(1) configured to act as a multiplexing 5664f4763Szrjmaster. This will cause ssh(1) to listen on a Unix domain socket for 6664f4763Szrjrequests from clients. Clients communicate over this socket using a 7664f4763Szrjsimple packetised protocol, where each message is proceeded with 8664f4763Szrja length and message type in SSH uint32 wire format: 9664f4763Szrj 10664f4763Szrj uint32 packet length 11664f4763Szrj uint32 packet type 12664f4763Szrj ... packet body 13664f4763Szrj 14664f4763SzrjMost messages from the client to the server contain a "request id" 15664f4763Szrjfield. This field is returned in replies as "client request id" to 16664f4763Szrjfacilitate matching of responses to requests. 17664f4763Szrj 18ee116499SAntonio Huete JimenezMany multiplexing (mux) client requests yield immediate responses from 19664f4763Szrjthe mux process; requesting a forwarding, performing an alive check or 20664f4763Szrjrequesting the master terminate itself fall in to this category. 21664f4763Szrj 22664f4763SzrjThe most common use of multiplexing however is to maintain multiple 23664f4763Szrjconcurrent sessions. These are supported via two separate modes: 24664f4763Szrj 25664f4763Szrj"Passenger" clients start by requesting a new session with a 26664f4763SzrjMUX_C_NEW_SESSION message and passing stdio file descriptors over the 27664f4763SzrjUnix domain control socket. The passenger client then waits until it is 28664f4763Szrjsignaled or the mux server closes the session. This mode is so named as 29664f4763Szrjthe client waits around while the mux server does all the driving. 30664f4763Szrj 31664f4763SzrjStdio forwarding (requested using MUX_C_NEW_STDIO_FWD) is another 32664f4763Szrjexample of passenger mode; the client passes the stdio file descriptors 33664f4763Szrjand passively waits for something to happen. 34664f4763Szrj 35664f4763Szrj"Proxy" clients, requested using MUX_C_PROXY, work quite differently. In 36664f4763Szrjthis mode, the mux client/server connection socket will stop speaking 37664f4763Szrjthe multiplexing protocol and start proxying SSH connection protocol 38664f4763Szrjmessages between the client and server. The client therefore must 39664f4763Szrjspeak a significant subset of the SSH protocol, but in return is able 40664f4763Szrjto access basically the full suite of connection protocol features. 41664f4763SzrjMoreover, as no file descriptor passing is required, the connection 420cbfa66cSDaniel Fojtsupporting a proxy client may itself be forwarded or relayed to another 43664f4763Szrjhost if necessary. 44856ea928SPeter Avalos 45856ea928SPeter Avalos1. Connection setup 46856ea928SPeter Avalos 47856ea928SPeter AvalosWhen a multiplexing connection is made to a ssh(1) operating as a 48664f4763SzrjControlMaster from a client ssh(1), the first action of each is send 49664f4763Szrja hello messages to its peer: 50856ea928SPeter Avalos 51856ea928SPeter Avalos uint32 MUX_MSG_HELLO 52856ea928SPeter Avalos uint32 protocol version 53856ea928SPeter Avalos string extension name [optional] 54856ea928SPeter Avalos string extension value [optional] 55856ea928SPeter Avalos ... 56856ea928SPeter Avalos 57664f4763SzrjThe current version of the mux protocol is 4. A client should refuse 58856ea928SPeter Avalosto connect to a master that speaks an unsupported protocol version. 59856ea928SPeter Avalos 60664f4763SzrjFollowing the version identifier are zero or more extensions represented 61664f4763Szrjas a name/value pair. No extensions are currently defined. 62856ea928SPeter Avalos 63664f4763Szrj2. Opening a passenger mode session 64664f4763Szrj 65664f4763SzrjTo open a new multiplexed session in passenger mode, a client sends the 66664f4763Szrjfollowing request: 67856ea928SPeter Avalos 689f304aafSPeter Avalos uint32 MUX_C_NEW_SESSION 69856ea928SPeter Avalos uint32 request id 70856ea928SPeter Avalos string reserved 71856ea928SPeter Avalos bool want tty flag 72856ea928SPeter Avalos bool want X11 forwarding flag 73856ea928SPeter Avalos bool want agent flag 74856ea928SPeter Avalos bool subsystem flag 75856ea928SPeter Avalos uint32 escape char 76856ea928SPeter Avalos string terminal type 77856ea928SPeter Avalos string command 78856ea928SPeter Avalos string environment string 0 [optional] 79856ea928SPeter Avalos ... 80856ea928SPeter Avalos 81856ea928SPeter AvalosTo disable the use of an escape character, "escape char" may be set 82856ea928SPeter Avalosto 0xffffffff. "terminal type" is generally set to the value of 83856ea928SPeter Avalos$TERM. zero or more environment strings may follow the command. 84856ea928SPeter Avalos 85856ea928SPeter AvalosThe client then sends its standard input, output and error file 86856ea928SPeter Avalosdescriptors (in that order) using Unix domain socket control messages. 87856ea928SPeter Avalos 88856ea928SPeter AvalosThe contents of "reserved" are currently ignored. 89856ea928SPeter Avalos 90856ea928SPeter AvalosIf successful, the server will reply with MUX_S_SESSION_OPENED 91856ea928SPeter Avalos 92856ea928SPeter Avalos uint32 MUX_S_SESSION_OPENED 93856ea928SPeter Avalos uint32 client request id 94856ea928SPeter Avalos uint32 session id 95856ea928SPeter Avalos 96856ea928SPeter AvalosOtherwise it will reply with an error: MUX_S_PERMISSION_DENIED or 97856ea928SPeter AvalosMUX_S_FAILURE. 98856ea928SPeter Avalos 99856ea928SPeter AvalosOnce the server has received the fds, it will respond with MUX_S_OK 100856ea928SPeter Avalosindicating that the session is up. The client now waits for the 101856ea928SPeter Avalossession to end. When it does, the server will send an exit status 102856ea928SPeter Avalosmessage: 103856ea928SPeter Avalos 104856ea928SPeter Avalos uint32 MUX_S_EXIT_MESSAGE 105856ea928SPeter Avalos uint32 session id 106856ea928SPeter Avalos uint32 exit value 107856ea928SPeter Avalos 108856ea928SPeter AvalosThe client should exit with this value to mimic the behaviour of a 109856ea928SPeter Avalosnon-multiplexed ssh(1) connection. Two additional cases that the 110856ea928SPeter Avalosclient must cope with are it receiving a signal itself and the 111856ea928SPeter Avalosserver disconnecting without sending an exit message. 112856ea928SPeter Avalos 1131c188a7fSPeter AvalosA master may also send a MUX_S_TTY_ALLOC_FAIL before MUX_S_EXIT_MESSAGE 1141c188a7fSPeter Avalosif remote TTY allocation was unsuccessful. The client may use this to 1151c188a7fSPeter Avalosreturn its local tty to "cooked" mode. 1161c188a7fSPeter Avalos 1171c188a7fSPeter Avalos uint32 MUX_S_TTY_ALLOC_FAIL 1181c188a7fSPeter Avalos uint32 session id 1191c188a7fSPeter Avalos 120664f4763Szrj3. Requesting passenger-mode stdio forwarding 121664f4763Szrj 122664f4763SzrjA client may request the master to establish a stdio forwarding: 123664f4763Szrj 124664f4763Szrj uint32 MUX_C_NEW_STDIO_FWD 125664f4763Szrj uint32 request id 126664f4763Szrj string reserved 127664f4763Szrj string connect host 128664f4763Szrj string connect port 129664f4763Szrj 130664f4763SzrjThe client then sends its standard input and output file descriptors 131664f4763Szrj(in that order) using Unix domain socket control messages. 132664f4763Szrj 133664f4763SzrjThe contents of "reserved" are currently ignored. 134664f4763Szrj 135664f4763SzrjA server may reply with a MUX_S_SESSION_OPENED, a MUX_S_PERMISSION_DENIED 136664f4763Szrjor a MUX_S_FAILURE. 137664f4763Szrj 138664f4763Szrj4. Health checks 139856ea928SPeter Avalos 140856ea928SPeter AvalosThe client may request a health check/PID report from a server: 141856ea928SPeter Avalos 142856ea928SPeter Avalos uint32 MUX_C_ALIVE_CHECK 143856ea928SPeter Avalos uint32 request id 144856ea928SPeter Avalos 145856ea928SPeter AvalosThe server replies with: 146856ea928SPeter Avalos 147856ea928SPeter Avalos uint32 MUX_S_ALIVE 148856ea928SPeter Avalos uint32 client request id 149856ea928SPeter Avalos uint32 server pid 150856ea928SPeter Avalos 151664f4763Szrj5. Remotely terminating a master 152856ea928SPeter Avalos 153856ea928SPeter AvalosA client may request that a master terminate immediately: 154856ea928SPeter Avalos 155856ea928SPeter Avalos uint32 MUX_C_TERMINATE 156856ea928SPeter Avalos uint32 request id 157856ea928SPeter Avalos 158856ea928SPeter AvalosThe server will reply with one of MUX_S_OK or MUX_S_PERMISSION_DENIED. 159856ea928SPeter Avalos 160664f4763Szrj6. Requesting establishment of port forwards 161856ea928SPeter Avalos 162856ea928SPeter AvalosA client may request the master to establish a port forward: 163856ea928SPeter Avalos 1649f304aafSPeter Avalos uint32 MUX_C_OPEN_FWD 165856ea928SPeter Avalos uint32 request id 166856ea928SPeter Avalos uint32 forwarding type 167856ea928SPeter Avalos string listen host 16899e85e0dSPeter Avalos uint32 listen port 169856ea928SPeter Avalos string connect host 17099e85e0dSPeter Avalos uint32 connect port 171856ea928SPeter Avalos 172856ea928SPeter Avalosforwarding type may be MUX_FWD_LOCAL, MUX_FWD_REMOTE, MUX_FWD_DYNAMIC. 173856ea928SPeter Avalos 174e9778795SPeter AvalosIf listen port is (unsigned int) -2, then the listen host is treated as 175e9778795SPeter Avalosa unix socket path name. 176e9778795SPeter Avalos 177e9778795SPeter AvalosIf connect port is (unsigned int) -2, then the connect host is treated 178e9778795SPeter Avalosas a unix socket path name. 179e9778795SPeter Avalos 180856ea928SPeter AvalosA server may reply with a MUX_S_OK, a MUX_S_REMOTE_PORT, a 181856ea928SPeter AvalosMUX_S_PERMISSION_DENIED or a MUX_S_FAILURE. 182856ea928SPeter Avalos 183856ea928SPeter AvalosFor dynamically allocated listen port the server replies with 184856ea928SPeter Avalos 185856ea928SPeter Avalos uint32 MUX_S_REMOTE_PORT 186856ea928SPeter Avalos uint32 client request id 187856ea928SPeter Avalos uint32 allocated remote listen port 188856ea928SPeter Avalos 189664f4763Szrj7. Requesting closure of port forwards 190856ea928SPeter Avalos 1919f304aafSPeter AvalosA client may request the master to close a port forward: 1929f304aafSPeter Avalos 1939f304aafSPeter Avalos uint32 MUX_C_CLOSE_FWD 194856ea928SPeter Avalos uint32 request id 19599e85e0dSPeter Avalos uint32 forwarding type 196856ea928SPeter Avalos string listen host 19799e85e0dSPeter Avalos uint32 listen port 198856ea928SPeter Avalos string connect host 19999e85e0dSPeter Avalos uint32 connect port 200856ea928SPeter Avalos 201856ea928SPeter AvalosA server may reply with a MUX_S_OK, a MUX_S_PERMISSION_DENIED or a 202856ea928SPeter AvalosMUX_S_FAILURE. 203856ea928SPeter Avalos 2041c188a7fSPeter Avalos8. Requesting shutdown of mux listener 2051c188a7fSPeter Avalos 2061c188a7fSPeter AvalosA client may request the master to stop accepting new multiplexing requests 2071c188a7fSPeter Avalosand remove its listener socket. 2081c188a7fSPeter Avalos 2091c188a7fSPeter Avalos uint32 MUX_C_STOP_LISTENING 2101c188a7fSPeter Avalos uint32 request id 2111c188a7fSPeter Avalos 2121c188a7fSPeter AvalosA server may reply with a MUX_S_OK, a MUX_S_PERMISSION_DENIED or a 2131c188a7fSPeter AvalosMUX_S_FAILURE. 2141c188a7fSPeter Avalos 215664f4763Szrj9. Requesting proxy mode 216664f4763Szrj 217ee116499SAntonio Huete JimenezA client may request that the control connection be placed in proxy 218664f4763Szrjmode: 219664f4763Szrj 220664f4763Szrj uint32 MUX_C_PROXY 221664f4763Szrj uint32 request id 222664f4763Szrj 223664f4763SzrjWhen a mux master receives this message, it will reply with a 224664f4763Szrjconfirmation: 225664f4763Szrj 226664f4763Szrj uint32 MUX_S_PROXY 227664f4763Szrj uint32 request id 228664f4763Szrj 229664f4763SzrjAnd go into proxy mode. All subsequent data over the connection will 230664f4763Szrjbe formatted as unencrypted, unpadded, SSH transport messages: 231664f4763Szrj 232664f4763Szrj uint32 packet length 233664f4763Szrj byte 0 (padding length) 234664f4763Szrj byte packet type 235664f4763Szrj byte[packet length - 2] ... 236664f4763Szrj 237664f4763SzrjThe mux master will accept most connection messages and global requests, 238664f4763Szrjand will translate channel identifiers to ensure that the proxy client has 239664f4763Szrjglobally unique channel numbers (i.e. a proxy client need not worry about 240664f4763Szrjcollisions with other clients). 241664f4763Szrj 242664f4763Szrj10. Status messages 243856ea928SPeter Avalos 244856ea928SPeter AvalosThe MUX_S_OK message is empty: 245856ea928SPeter Avalos 246856ea928SPeter Avalos uint32 MUX_S_OK 247856ea928SPeter Avalos uint32 client request id 248856ea928SPeter Avalos 249856ea928SPeter AvalosThe MUX_S_PERMISSION_DENIED and MUX_S_FAILURE include a reason: 250856ea928SPeter Avalos 251856ea928SPeter Avalos uint32 MUX_S_PERMISSION_DENIED 252856ea928SPeter Avalos uint32 client request id 253856ea928SPeter Avalos string reason 254856ea928SPeter Avalos 255856ea928SPeter Avalos uint32 MUX_S_FAILURE 256856ea928SPeter Avalos uint32 client request id 257856ea928SPeter Avalos string reason 258856ea928SPeter Avalos 259664f4763Szrj11. Protocol numbers 260856ea928SPeter Avalos 261856ea928SPeter Avalos#define MUX_MSG_HELLO 0x00000001 262856ea928SPeter Avalos#define MUX_C_NEW_SESSION 0x10000002 263856ea928SPeter Avalos#define MUX_C_ALIVE_CHECK 0x10000004 264856ea928SPeter Avalos#define MUX_C_TERMINATE 0x10000005 2659f304aafSPeter Avalos#define MUX_C_OPEN_FWD 0x10000006 2669f304aafSPeter Avalos#define MUX_C_CLOSE_FWD 0x10000007 2679f304aafSPeter Avalos#define MUX_C_NEW_STDIO_FWD 0x10000008 2681c188a7fSPeter Avalos#define MUX_C_STOP_LISTENING 0x10000009 269856ea928SPeter Avalos#define MUX_S_OK 0x80000001 270856ea928SPeter Avalos#define MUX_S_PERMISSION_DENIED 0x80000002 271856ea928SPeter Avalos#define MUX_S_FAILURE 0x80000003 272856ea928SPeter Avalos#define MUX_S_EXIT_MESSAGE 0x80000004 273856ea928SPeter Avalos#define MUX_S_ALIVE 0x80000005 274856ea928SPeter Avalos#define MUX_S_SESSION_OPENED 0x80000006 275856ea928SPeter Avalos#define MUX_S_REMOTE_PORT 0x80000007 2761c188a7fSPeter Avalos#define MUX_S_TTY_ALLOC_FAIL 0x80000008 277856ea928SPeter Avalos 278856ea928SPeter Avalos#define MUX_FWD_LOCAL 1 279856ea928SPeter Avalos#define MUX_FWD_REMOTE 2 280856ea928SPeter Avalos#define MUX_FWD_DYNAMIC 3 281856ea928SPeter Avalos 282856ea928SPeter AvalosXXX TODO 283856ea928SPeter AvalosXXX extended status (e.g. report open channels / forwards) 284856ea928SPeter AvalosXXX lock (maybe) 285856ea928SPeter AvalosXXX watch in/out traffic (pre/post crypto) 286856ea928SPeter AvalosXXX inject packet (what about replies) 287856ea928SPeter AvalosXXX server->client error/warning notifications 288856ea928SPeter AvalosXXX send signals via mux 289664f4763SzrjXXX ^Z support in passengers 290664f4763SzrjXXX extensions for multi-agent 291664f4763SzrjXXX extensions for multi-X11 292664f4763SzrjXXX session inspection via master 293664f4763SzrjXXX signals via mux request 294664f4763SzrjXXX list active connections via mux 295856ea928SPeter Avalos 296*ba1276acSMatthew Dillon$OpenBSD: PROTOCOL.mux,v 1.14 2024/01/08 05:11:18 djm Exp $ 297