PROTOCOL.mux 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. This document describes the multiplexing protocol used by ssh(1)'s
  2. ControlMaster connection-sharing.
  3. Multiplexing starts with a ssh(1) configured to act as a multiplexing
  4. master. This will cause ssh(1) to listen on a Unix domain socket for
  5. requests from clients. Clients communicate over this socket using a
  6. simple packetised protocol, where each message is proceeded with
  7. a length and message type in SSH uint32 wire format:
  8. uint32 packet length
  9. uint32 packet type
  10. ... packet body
  11. Most messages from the client to the server contain a "request id"
  12. field. This field is returned in replies as "client request id" to
  13. facilitate matching of responses to requests.
  14. Many muliplexing (mux) client requests yield immediate responses from
  15. the mux process; requesting a forwarding, performing an alive check or
  16. requesting the master terminate itself fall in to this category.
  17. The most common use of multiplexing however is to maintain multiple
  18. concurrent sessions. These are supported via two separate modes:
  19. "Passenger" clients start by requesting a new session with a
  20. MUX_C_NEW_SESSION message and passing stdio file descriptors over the
  21. Unix domain control socket. The passenger client then waits until it is
  22. signaled or the mux server closes the session. This mode is so named as
  23. the client waits around while the mux server does all the driving.
  24. Stdio forwarding (requested using MUX_C_NEW_STDIO_FWD) is another
  25. example of passenger mode; the client passes the stdio file descriptors
  26. and passively waits for something to happen.
  27. "Proxy" clients, requested using MUX_C_PROXY, work quite differently. In
  28. this mode, the mux client/server connection socket will stop speaking
  29. the multiplexing protocol and start proxying SSH connection protocol
  30. messages between the client and server. The client therefore must
  31. speak a significant subset of the SSH protocol, but in return is able
  32. to access basically the full suite of connection protocol features.
  33. Moreover, as no file descriptor passing is required, the connection
  34. supporting a proxy client may itself be forwarded or relayed to another
  35. host if necessary.
  36. 1. Connection setup
  37. When a multiplexing connection is made to a ssh(1) operating as a
  38. ControlMaster from a client ssh(1), the first action of each is send
  39. a hello messages to its peer:
  40. uint32 MUX_MSG_HELLO
  41. uint32 protocol version
  42. string extension name [optional]
  43. string extension value [optional]
  44. ...
  45. The current version of the mux protocol is 4. A client should refuse
  46. to connect to a master that speaks an unsupported protocol version.
  47. Following the version identifier are zero or more extensions represented
  48. as a name/value pair. No extensions are currently defined.
  49. 2. Opening a passenger mode session
  50. To open a new multiplexed session in passenger mode, a client sends the
  51. following request:
  52. uint32 MUX_C_NEW_SESSION
  53. uint32 request id
  54. string reserved
  55. bool want tty flag
  56. bool want X11 forwarding flag
  57. bool want agent flag
  58. bool subsystem flag
  59. uint32 escape char
  60. string terminal type
  61. string command
  62. string environment string 0 [optional]
  63. ...
  64. To disable the use of an escape character, "escape char" may be set
  65. to 0xffffffff. "terminal type" is generally set to the value of
  66. $TERM. zero or more environment strings may follow the command.
  67. The client then sends its standard input, output and error file
  68. descriptors (in that order) using Unix domain socket control messages.
  69. The contents of "reserved" are currently ignored.
  70. If successful, the server will reply with MUX_S_SESSION_OPENED
  71. uint32 MUX_S_SESSION_OPENED
  72. uint32 client request id
  73. uint32 session id
  74. Otherwise it will reply with an error: MUX_S_PERMISSION_DENIED or
  75. MUX_S_FAILURE.
  76. Once the server has received the fds, it will respond with MUX_S_OK
  77. indicating that the session is up. The client now waits for the
  78. session to end. When it does, the server will send an exit status
  79. message:
  80. uint32 MUX_S_EXIT_MESSAGE
  81. uint32 session id
  82. uint32 exit value
  83. The client should exit with this value to mimic the behaviour of a
  84. non-multiplexed ssh(1) connection. Two additional cases that the
  85. client must cope with are it receiving a signal itself and the
  86. server disconnecting without sending an exit message.
  87. A master may also send a MUX_S_TTY_ALLOC_FAIL before MUX_S_EXIT_MESSAGE
  88. if remote TTY allocation was unsuccessful. The client may use this to
  89. return its local tty to "cooked" mode.
  90. uint32 MUX_S_TTY_ALLOC_FAIL
  91. uint32 session id
  92. 3. Requesting passenger-mode stdio forwarding
  93. A client may request the master to establish a stdio forwarding:
  94. uint32 MUX_C_NEW_STDIO_FWD
  95. uint32 request id
  96. string reserved
  97. string connect host
  98. string connect port
  99. The client then sends its standard input and output file descriptors
  100. (in that order) using Unix domain socket control messages.
  101. The contents of "reserved" are currently ignored.
  102. A server may reply with a MUX_S_SESSION_OPENED, a MUX_S_PERMISSION_DENIED
  103. or a MUX_S_FAILURE.
  104. 4. Health checks
  105. The client may request a health check/PID report from a server:
  106. uint32 MUX_C_ALIVE_CHECK
  107. uint32 request id
  108. The server replies with:
  109. uint32 MUX_S_ALIVE
  110. uint32 client request id
  111. uint32 server pid
  112. 5. Remotely terminating a master
  113. A client may request that a master terminate immediately:
  114. uint32 MUX_C_TERMINATE
  115. uint32 request id
  116. The server will reply with one of MUX_S_OK or MUX_S_PERMISSION_DENIED.
  117. 6. Requesting establishment of port forwards
  118. A client may request the master to establish a port forward:
  119. uint32 MUX_C_OPEN_FWD
  120. uint32 request id
  121. uint32 forwarding type
  122. string listen host
  123. uint32 listen port
  124. string connect host
  125. uint32 connect port
  126. forwarding type may be MUX_FWD_LOCAL, MUX_FWD_REMOTE, MUX_FWD_DYNAMIC.
  127. If listen port is (unsigned int) -2, then the listen host is treated as
  128. a unix socket path name.
  129. If connect port is (unsigned int) -2, then the connect host is treated
  130. as a unix socket path name.
  131. A server may reply with a MUX_S_OK, a MUX_S_REMOTE_PORT, a
  132. MUX_S_PERMISSION_DENIED or a MUX_S_FAILURE.
  133. For dynamically allocated listen port the server replies with
  134. uint32 MUX_S_REMOTE_PORT
  135. uint32 client request id
  136. uint32 allocated remote listen port
  137. 7. Requesting closure of port forwards
  138. Note: currently unimplemented (server will always reply with MUX_S_FAILURE).
  139. A client may request the master to close a port forward:
  140. uint32 MUX_C_CLOSE_FWD
  141. uint32 request id
  142. uint32 forwarding type
  143. string listen host
  144. uint32 listen port
  145. string connect host
  146. uint32 connect port
  147. A server may reply with a MUX_S_OK, a MUX_S_PERMISSION_DENIED or a
  148. MUX_S_FAILURE.
  149. 8. Requesting shutdown of mux listener
  150. A client may request the master to stop accepting new multiplexing requests
  151. and remove its listener socket.
  152. uint32 MUX_C_STOP_LISTENING
  153. uint32 request id
  154. A server may reply with a MUX_S_OK, a MUX_S_PERMISSION_DENIED or a
  155. MUX_S_FAILURE.
  156. 9. Requesting proxy mode
  157. A client may request that the the control connection be placed in proxy
  158. mode:
  159. uint32 MUX_C_PROXY
  160. uint32 request id
  161. When a mux master receives this message, it will reply with a
  162. confirmation:
  163. uint32 MUX_S_PROXY
  164. uint32 request id
  165. And go into proxy mode. All subsequent data over the connection will
  166. be formatted as unencrypted, unpadded, SSH transport messages:
  167. uint32 packet length
  168. byte 0 (padding length)
  169. byte packet type
  170. byte[packet length - 2] ...
  171. The mux master will accept most connection messages and global requests,
  172. and will translate channel identifiers to ensure that the proxy client has
  173. globally unique channel numbers (i.e. a proxy client need not worry about
  174. collisions with other clients).
  175. 10. Status messages
  176. The MUX_S_OK message is empty:
  177. uint32 MUX_S_OK
  178. uint32 client request id
  179. The MUX_S_PERMISSION_DENIED and MUX_S_FAILURE include a reason:
  180. uint32 MUX_S_PERMISSION_DENIED
  181. uint32 client request id
  182. string reason
  183. uint32 MUX_S_FAILURE
  184. uint32 client request id
  185. string reason
  186. 11. Protocol numbers
  187. #define MUX_MSG_HELLO 0x00000001
  188. #define MUX_C_NEW_SESSION 0x10000002
  189. #define MUX_C_ALIVE_CHECK 0x10000004
  190. #define MUX_C_TERMINATE 0x10000005
  191. #define MUX_C_OPEN_FWD 0x10000006
  192. #define MUX_C_CLOSE_FWD 0x10000007
  193. #define MUX_C_NEW_STDIO_FWD 0x10000008
  194. #define MUX_C_STOP_LISTENING 0x10000009
  195. #define MUX_S_OK 0x80000001
  196. #define MUX_S_PERMISSION_DENIED 0x80000002
  197. #define MUX_S_FAILURE 0x80000003
  198. #define MUX_S_EXIT_MESSAGE 0x80000004
  199. #define MUX_S_ALIVE 0x80000005
  200. #define MUX_S_SESSION_OPENED 0x80000006
  201. #define MUX_S_REMOTE_PORT 0x80000007
  202. #define MUX_S_TTY_ALLOC_FAIL 0x80000008
  203. #define MUX_FWD_LOCAL 1
  204. #define MUX_FWD_REMOTE 2
  205. #define MUX_FWD_DYNAMIC 3
  206. XXX TODO
  207. XXX extended status (e.g. report open channels / forwards)
  208. XXX lock (maybe)
  209. XXX watch in/out traffic (pre/post crypto)
  210. XXX inject packet (what about replies)
  211. XXX server->client error/warning notifications
  212. XXX send signals via mux
  213. XXX ^Z support in passengers
  214. XXX extensions for multi-agent
  215. XXX extensions for multi-X11
  216. XXX session inspection via master
  217. XXX signals via mux request
  218. XXX list active connections via mux
  219. $OpenBSD: PROTOCOL.mux,v 1.12 2020/03/13 03:17:07 djm Exp $