channels.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* $OpenBSD: channels.h,v 1.135 2020/09/20 05:47:25 djm Exp $ */
  2. /*
  3. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  4. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  5. * All rights reserved
  6. *
  7. * As far as I am concerned, the code I have written for this software
  8. * can be used freely for any purpose. Any derived versions of this
  9. * software must be clearly marked as such, and if the derived work is
  10. * incompatible with the protocol description in the RFC file, it must be
  11. * called by a name other than "ssh" or "Secure Shell".
  12. */
  13. /*
  14. * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef CHANNEL_H
  37. #define CHANNEL_H
  38. /* Definitions for channel types. */
  39. #define SSH_CHANNEL_X11_LISTENER 1 /* Listening for inet X11 conn. */
  40. #define SSH_CHANNEL_PORT_LISTENER 2 /* Listening on a port. */
  41. #define SSH_CHANNEL_OPENING 3 /* waiting for confirmation */
  42. #define SSH_CHANNEL_OPEN 4 /* normal open two-way channel */
  43. #define SSH_CHANNEL_CLOSED 5 /* waiting for close confirmation */
  44. #define SSH_CHANNEL_AUTH_SOCKET 6 /* authentication socket */
  45. #define SSH_CHANNEL_X11_OPEN 7 /* reading first X11 packet */
  46. #define SSH_CHANNEL_LARVAL 10 /* larval session */
  47. #define SSH_CHANNEL_RPORT_LISTENER 11 /* Listening to a R-style port */
  48. #define SSH_CHANNEL_CONNECTING 12
  49. #define SSH_CHANNEL_DYNAMIC 13
  50. #define SSH_CHANNEL_ZOMBIE 14 /* Almost dead. */
  51. #define SSH_CHANNEL_MUX_LISTENER 15 /* Listener for mux conn. */
  52. #define SSH_CHANNEL_MUX_CLIENT 16 /* Conn. to mux client */
  53. #define SSH_CHANNEL_ABANDONED 17 /* Abandoned session, eg mux */
  54. #define SSH_CHANNEL_UNIX_LISTENER 18 /* Listening on a domain socket. */
  55. #define SSH_CHANNEL_RUNIX_LISTENER 19 /* Listening to a R-style domain socket. */
  56. #define SSH_CHANNEL_MUX_PROXY 20 /* proxy channel for mux-client */
  57. #define SSH_CHANNEL_RDYNAMIC_OPEN 21 /* reverse SOCKS, parsing request */
  58. #define SSH_CHANNEL_RDYNAMIC_FINISH 22 /* reverse SOCKS, finishing connect */
  59. #define SSH_CHANNEL_MAX_TYPE 23
  60. #define CHANNEL_CANCEL_PORT_STATIC -1
  61. /* TCP forwarding */
  62. #define FORWARD_DENY 0
  63. #define FORWARD_REMOTE (1)
  64. #define FORWARD_LOCAL (1<<1)
  65. #define FORWARD_ALLOW (FORWARD_REMOTE|FORWARD_LOCAL)
  66. #define FORWARD_ADM 0x100
  67. #define FORWARD_USER 0x101
  68. struct ssh;
  69. struct Channel;
  70. typedef struct Channel Channel;
  71. struct fwd_perm_list;
  72. typedef void channel_open_fn(struct ssh *, int, int, void *);
  73. typedef void channel_callback_fn(struct ssh *, int, void *);
  74. typedef int channel_infilter_fn(struct ssh *, struct Channel *, char *, int);
  75. typedef void channel_filter_cleanup_fn(struct ssh *, int, void *);
  76. typedef u_char *channel_outfilter_fn(struct ssh *, struct Channel *,
  77. u_char **, size_t *);
  78. /* Channel success/failure callbacks */
  79. typedef void channel_confirm_cb(struct ssh *, int, struct Channel *, void *);
  80. typedef void channel_confirm_abandon_cb(struct ssh *, struct Channel *, void *);
  81. struct channel_confirm {
  82. TAILQ_ENTRY(channel_confirm) entry;
  83. channel_confirm_cb *cb;
  84. channel_confirm_abandon_cb *abandon_cb;
  85. void *ctx;
  86. };
  87. TAILQ_HEAD(channel_confirms, channel_confirm);
  88. /* Context for non-blocking connects */
  89. struct channel_connect {
  90. char *host;
  91. int port;
  92. struct addrinfo *ai, *aitop;
  93. };
  94. /* Callbacks for mux channels back into client-specific code */
  95. typedef int mux_callback_fn(struct ssh *, struct Channel *);
  96. /*
  97. * NB. channel IDs on the wire and in c->remote_id are uint32, but local
  98. * channel IDs (e.g. c->self) only ever use the int32 subset of this range,
  99. * because we use local channel ID -1 for housekeeping. Remote channels have
  100. * a dedicated "have_remote_id" flag to indicate their validity.
  101. */
  102. struct Channel {
  103. int type; /* channel type/state */
  104. int self; /* my own channel identifier */
  105. uint32_t remote_id; /* channel identifier for remote peer */
  106. int have_remote_id; /* non-zero if remote_id is valid */
  107. u_int istate; /* input from channel (state of receive half) */
  108. u_int ostate; /* output to channel (state of transmit half) */
  109. int flags; /* close sent/rcvd */
  110. int rfd; /* read fd */
  111. int wfd; /* write fd */
  112. int efd; /* extended fd */
  113. int sock; /* sock fd */
  114. int ctl_chan; /* control channel (multiplexed connections) */
  115. int isatty; /* rfd is a tty */
  116. #ifdef _AIX
  117. int wfd_isatty; /* wfd is a tty */
  118. #endif
  119. int client_tty; /* (client) TTY has been requested */
  120. int force_drain; /* force close on iEOF */
  121. time_t notbefore; /* Pause IO until deadline (time_t) */
  122. int delayed; /* post-select handlers for newly created
  123. * channels are delayed until the first call
  124. * to a matching pre-select handler.
  125. * this way post-select handlers are not
  126. * accidentally called if a FD gets reused */
  127. struct sshbuf *input; /* data read from socket, to be sent over
  128. * encrypted connection */
  129. struct sshbuf *output; /* data received over encrypted connection for
  130. * send on socket */
  131. struct sshbuf *extended;
  132. char *path;
  133. /* path for unix domain sockets, or host name for forwards */
  134. int listening_port; /* port being listened for forwards */
  135. char *listening_addr; /* addr being listened for forwards */
  136. int host_port; /* remote port to connect for forwards */
  137. char *remote_name; /* remote hostname */
  138. u_int remote_window;
  139. u_int remote_maxpacket;
  140. u_int local_window;
  141. u_int local_window_max;
  142. u_int local_consumed;
  143. u_int local_maxpacket;
  144. int dynamic_window;
  145. int extended_usage;
  146. int single_connection;
  147. u_int tcpwinsz;
  148. char *ctype; /* type */
  149. /* callback */
  150. channel_open_fn *open_confirm;
  151. void *open_confirm_ctx;
  152. channel_callback_fn *detach_user;
  153. int detach_close;
  154. struct channel_confirms status_confirms;
  155. /* filter */
  156. channel_infilter_fn *input_filter;
  157. channel_outfilter_fn *output_filter;
  158. void *filter_ctx;
  159. channel_filter_cleanup_fn *filter_cleanup;
  160. /* keep boundaries */
  161. int datagram;
  162. /* non-blocking connect */
  163. /* XXX make this a pointer so the structure can be opaque */
  164. struct channel_connect connect_ctx;
  165. /* multiplexing protocol hook, called for each packet received */
  166. mux_callback_fn *mux_rcb;
  167. void *mux_ctx;
  168. int mux_pause;
  169. int mux_downstream_id;
  170. };
  171. #define CHAN_EXTENDED_IGNORE 0
  172. #define CHAN_EXTENDED_READ 1
  173. #define CHAN_EXTENDED_WRITE 2
  174. /* default window/packet sizes for tcp/x11-fwd-channel */
  175. /* XXX(jhj): doubled */
  176. #define CHAN_SES_PACKET_DEFAULT (64*1024)
  177. #define CHAN_SES_WINDOW_DEFAULT (128*CHAN_SES_PACKET_DEFAULT)
  178. #define CHAN_TCP_PACKET_DEFAULT (64*1024)
  179. #define CHAN_TCP_WINDOW_DEFAULT (128*CHAN_TCP_PACKET_DEFAULT)
  180. #define CHAN_X11_PACKET_DEFAULT (32*1024)
  181. #define CHAN_X11_WINDOW_DEFAULT (8*CHAN_X11_PACKET_DEFAULT)
  182. /* possible input states */
  183. #define CHAN_INPUT_OPEN 0
  184. #define CHAN_INPUT_WAIT_DRAIN 1
  185. #define CHAN_INPUT_WAIT_OCLOSE 2
  186. #define CHAN_INPUT_CLOSED 3
  187. /* possible output states */
  188. #define CHAN_OUTPUT_OPEN 0
  189. #define CHAN_OUTPUT_WAIT_DRAIN 1
  190. #define CHAN_OUTPUT_WAIT_IEOF 2
  191. #define CHAN_OUTPUT_CLOSED 3
  192. #define CHAN_CLOSE_SENT 0x01
  193. #define CHAN_CLOSE_RCVD 0x02
  194. #define CHAN_EOF_SENT 0x04
  195. #define CHAN_EOF_RCVD 0x08
  196. #define CHAN_LOCAL 0x10
  197. /* Read buffer size */
  198. #define CHAN_RBUF CHAN_SES_PACKET_DEFAULT
  199. /* Maximum channel input buffer size */
  200. #define CHAN_INPUT_MAX (16*1024*1024)
  201. /* Hard limit on number of channels */
  202. #define CHANNELS_MAX_CHANNELS (16*1024)
  203. /* check whether 'efd' is still in use */
  204. #define CHANNEL_EFD_INPUT_ACTIVE(c) \
  205. (c->extended_usage == CHAN_EXTENDED_READ && \
  206. (c->efd != -1 || \
  207. sshbuf_len(c->extended) > 0))
  208. #define CHANNEL_EFD_OUTPUT_ACTIVE(c) \
  209. (c->extended_usage == CHAN_EXTENDED_WRITE && \
  210. c->efd != -1 && (!(c->flags & (CHAN_EOF_RCVD|CHAN_CLOSE_RCVD)) || \
  211. sshbuf_len(c->extended) > 0))
  212. /* Add channel management structures to SSH transport instance */
  213. void channel_init_channels(struct ssh *ssh);
  214. /* channel management */
  215. Channel *channel_by_id(struct ssh *, int);
  216. Channel *channel_by_remote_id(struct ssh *, u_int);
  217. Channel *channel_lookup(struct ssh *, int);
  218. Channel *channel_new(struct ssh *, char *, int, int, int, int,
  219. u_int, u_int, int, char *, int);
  220. void channel_set_fds(struct ssh *, int, int, int, int, int,
  221. int, int, u_int);
  222. void channel_free(struct ssh *, Channel *);
  223. void channel_free_all(struct ssh *);
  224. void channel_stop_listening(struct ssh *);
  225. void channel_send_open(struct ssh *, int);
  226. void channel_request_start(struct ssh *, int, char *, int);
  227. void channel_register_cleanup(struct ssh *, int,
  228. channel_callback_fn *, int);
  229. void channel_register_open_confirm(struct ssh *, int,
  230. channel_open_fn *, void *);
  231. void channel_register_filter(struct ssh *, int, channel_infilter_fn *,
  232. channel_outfilter_fn *, channel_filter_cleanup_fn *, void *);
  233. void channel_register_status_confirm(struct ssh *, int,
  234. channel_confirm_cb *, channel_confirm_abandon_cb *, void *);
  235. void channel_cancel_cleanup(struct ssh *, int);
  236. int channel_close_fd(struct ssh *, int *);
  237. void channel_send_window_changes(struct ssh *);
  238. /* mux proxy support */
  239. int channel_proxy_downstream(struct ssh *, Channel *mc);
  240. int channel_proxy_upstream(Channel *, int, u_int32_t, struct ssh *);
  241. /* protocol handler */
  242. int channel_input_data(int, u_int32_t, struct ssh *);
  243. int channel_input_extended_data(int, u_int32_t, struct ssh *);
  244. int channel_input_ieof(int, u_int32_t, struct ssh *);
  245. int channel_input_oclose(int, u_int32_t, struct ssh *);
  246. int channel_input_open_confirmation(int, u_int32_t, struct ssh *);
  247. int channel_input_open_failure(int, u_int32_t, struct ssh *);
  248. int channel_input_port_open(int, u_int32_t, struct ssh *);
  249. int channel_input_window_adjust(int, u_int32_t, struct ssh *);
  250. int channel_input_status_confirm(int, u_int32_t, struct ssh *);
  251. /* file descriptor handling (read/write) */
  252. void channel_prepare_select(struct ssh *, fd_set **, fd_set **, int *,
  253. u_int*, time_t*);
  254. void channel_after_select(struct ssh *, fd_set *, fd_set *);
  255. void channel_output_poll(struct ssh *);
  256. int channel_not_very_much_buffered_data(struct ssh *);
  257. void channel_close_all(struct ssh *);
  258. int channel_still_open(struct ssh *);
  259. const char *channel_format_extended_usage(const Channel *);
  260. char *channel_open_message(struct ssh *);
  261. int channel_find_open(struct ssh *);
  262. /* tcp forwarding */
  263. struct Forward;
  264. struct ForwardOptions;
  265. void channel_set_af(struct ssh *, int af);
  266. void channel_permit_all(struct ssh *, int);
  267. void channel_add_permission(struct ssh *, int, int, char *, int);
  268. void channel_clear_permission(struct ssh *, int, int);
  269. void channel_disable_admin(struct ssh *, int);
  270. void channel_update_permission(struct ssh *, int, int);
  271. Channel *channel_connect_to_port(struct ssh *, const char *, u_short,
  272. char *, char *, int *, const char **);
  273. Channel *channel_connect_to_path(struct ssh *, const char *, char *, char *);
  274. Channel *channel_connect_stdio_fwd(struct ssh *, const char*,
  275. u_short, int, int);
  276. Channel *channel_connect_by_listen_address(struct ssh *, const char *,
  277. u_short, char *, char *);
  278. Channel *channel_connect_by_listen_path(struct ssh *, const char *,
  279. char *, char *);
  280. int channel_request_remote_forwarding(struct ssh *, struct Forward *);
  281. int channel_setup_local_fwd_listener(struct ssh *, struct Forward *,
  282. struct ForwardOptions *);
  283. int channel_request_rforward_cancel(struct ssh *, struct Forward *);
  284. int channel_setup_remote_fwd_listener(struct ssh *, struct Forward *,
  285. int *, struct ForwardOptions *);
  286. int channel_cancel_rport_listener(struct ssh *, struct Forward *);
  287. int channel_cancel_lport_listener(struct ssh *, struct Forward *,
  288. int, struct ForwardOptions *);
  289. int permitopen_port(const char *);
  290. /* x11 forwarding */
  291. void channel_set_x11_refuse_time(struct ssh *, u_int);
  292. int x11_connect_display(struct ssh *);
  293. int x11_create_display_inet(struct ssh *, int, int, int, u_int *, int **);
  294. void x11_request_forwarding_with_spoofing(struct ssh *, int,
  295. const char *, const char *, const char *, int);
  296. /* channel close */
  297. int chan_is_dead(struct ssh *, Channel *, int);
  298. void chan_mark_dead(struct ssh *, Channel *);
  299. /* channel events */
  300. void chan_rcvd_oclose(struct ssh *, Channel *);
  301. void chan_rcvd_eow(struct ssh *, Channel *);
  302. void chan_read_failed(struct ssh *, Channel *);
  303. void chan_ibuf_empty(struct ssh *, Channel *);
  304. void chan_rcvd_ieof(struct ssh *, Channel *);
  305. void chan_write_failed(struct ssh *, Channel *);
  306. void chan_obuf_empty(struct ssh *, Channel *);
  307. /* hpn handler */
  308. void channel_set_hpn(int, int);
  309. #endif