xref: /titanic_50/usr/src/cmd/ssh/include/channels.h (revision d80e6060324407c507b56eec0b706abbe697757d)
17c478bd9Sstevel@tonic-gate /*
2*d80e6060Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*	$OpenBSD: channels.h,v 1.70 2002/06/24 14:33:27 markus Exp $	*/
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #ifndef	_CHANNELS_H
87c478bd9Sstevel@tonic-gate #define	_CHANNELS_H
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #ifdef __cplusplus
137c478bd9Sstevel@tonic-gate extern "C" {
147c478bd9Sstevel@tonic-gate #endif
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate /*
187c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
197c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
207c478bd9Sstevel@tonic-gate  *                    All rights reserved
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
237c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
247c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
257c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
267c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
327c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
337c478bd9Sstevel@tonic-gate  * are met:
347c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
357c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
367c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
377c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
387c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
417c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
427c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
437c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
447c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
457c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
467c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
477c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
487c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
497c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include "buffer.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* Definitions for channel types. */
557c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_X11_LISTENER	1	/* Listening for inet X11 conn. */
567c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_PORT_LISTENER	2	/* Listening on a port. */
577c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_OPENING		3	/* waiting for confirmation */
587c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_OPEN		4	/* normal open two-way channel */
597c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_CLOSED		5	/* waiting for close confirmation */
607c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_AUTH_SOCKET		6	/* authentication socket */
617c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_X11_OPEN		7	/* reading first X11 packet */
627c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_INPUT_DRAINING	8	/* sending remaining data to conn */
637c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_OUTPUT_DRAINING	9	/* sending remaining data to app */
647c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_LARVAL		10	/* larval session */
657c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_RPORT_LISTENER	11	/* Listening to a R-style port  */
667c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_CONNECTING		12
677c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_DYNAMIC		13
687c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_ZOMBIE		14	/* Almost dead. */
697c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_MAX_TYPE		15
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define SSH_CHANNEL_PATH_LEN		256
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate struct Channel;
747c478bd9Sstevel@tonic-gate typedef struct Channel Channel;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate typedef void channel_callback_fn(int, void *);
777c478bd9Sstevel@tonic-gate typedef int channel_filter_fn(struct Channel *, char *, int);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct Channel {
807c478bd9Sstevel@tonic-gate 	int     type;		/* channel type/state */
817c478bd9Sstevel@tonic-gate 	int     self;		/* my own channel identifier */
827c478bd9Sstevel@tonic-gate 	int     remote_id;	/* channel identifier for remote peer */
837c478bd9Sstevel@tonic-gate 	u_int   istate;		/* input from channel (state of receive half) */
847c478bd9Sstevel@tonic-gate 	u_int   ostate;		/* output to channel  (state of transmit half) */
857c478bd9Sstevel@tonic-gate 	int	wait_for_exit;	/* no close till after exit-status is sent */
867c478bd9Sstevel@tonic-gate 	int     flags;		/* close sent/rcvd */
877c478bd9Sstevel@tonic-gate 	int     rfd;		/* read fd */
887c478bd9Sstevel@tonic-gate 	int     wfd;		/* write fd */
897c478bd9Sstevel@tonic-gate 	int     efd;		/* extended fd */
907c478bd9Sstevel@tonic-gate 	int     sock;		/* sock fd */
917c478bd9Sstevel@tonic-gate 	int     isatty;		/* rfd is a tty */
927c478bd9Sstevel@tonic-gate 	int     wfd_isatty;	/* wfd is a tty */
937c478bd9Sstevel@tonic-gate 	int     force_drain;	/* force close on iEOF */
947c478bd9Sstevel@tonic-gate 	int     delayed;		/* fdset hack */
957c478bd9Sstevel@tonic-gate 	Buffer  input;		/* data read from socket, to be sent over
967c478bd9Sstevel@tonic-gate 				 * encrypted connection */
977c478bd9Sstevel@tonic-gate 	Buffer  output;		/* data received over encrypted connection for
987c478bd9Sstevel@tonic-gate 				 * send on socket */
997c478bd9Sstevel@tonic-gate 	Buffer  extended;
1007c478bd9Sstevel@tonic-gate 	char    path[SSH_CHANNEL_PATH_LEN];
1017c478bd9Sstevel@tonic-gate 		/* path for unix domain sockets, or host name for forwards */
1027c478bd9Sstevel@tonic-gate 	int     listening_port;	/* port being listened for forwards */
1037c478bd9Sstevel@tonic-gate 	int     host_port;	/* remote port to connect for forwards */
1047c478bd9Sstevel@tonic-gate 	char   *remote_name;	/* remote hostname */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	u_int	remote_window;
1077c478bd9Sstevel@tonic-gate 	u_int	remote_maxpacket;
1087c478bd9Sstevel@tonic-gate 	u_int	local_window;
1097c478bd9Sstevel@tonic-gate 	u_int	local_window_max;
1107c478bd9Sstevel@tonic-gate 	u_int	local_consumed;
1117c478bd9Sstevel@tonic-gate 	u_int	local_maxpacket;
1127c478bd9Sstevel@tonic-gate 	int     extended_usage;
1137c478bd9Sstevel@tonic-gate 	int	single_connection;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	char   *ctype;		/* type */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* callback */
1187c478bd9Sstevel@tonic-gate 	channel_callback_fn	*confirm;
1197c478bd9Sstevel@tonic-gate 	channel_callback_fn	*detach_user;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/* filter */
1227c478bd9Sstevel@tonic-gate 	channel_filter_fn	*input_filter;
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define CHAN_EXTENDED_IGNORE		0
1267c478bd9Sstevel@tonic-gate #define CHAN_EXTENDED_READ		1
1277c478bd9Sstevel@tonic-gate #define CHAN_EXTENDED_WRITE		2
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* default window/packet sizes for tcp/x11-fwd-channel */
1307c478bd9Sstevel@tonic-gate #define CHAN_SES_PACKET_DEFAULT	(32*1024)
1317c478bd9Sstevel@tonic-gate #define CHAN_SES_WINDOW_DEFAULT	(4*CHAN_SES_PACKET_DEFAULT)
1327c478bd9Sstevel@tonic-gate #define CHAN_TCP_PACKET_DEFAULT	(32*1024)
1337c478bd9Sstevel@tonic-gate #define CHAN_TCP_WINDOW_DEFAULT	(4*CHAN_TCP_PACKET_DEFAULT)
1347c478bd9Sstevel@tonic-gate #define CHAN_X11_PACKET_DEFAULT	(16*1024)
1357c478bd9Sstevel@tonic-gate #define CHAN_X11_WINDOW_DEFAULT	(4*CHAN_X11_PACKET_DEFAULT)
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* possible input states */
1387c478bd9Sstevel@tonic-gate #define CHAN_INPUT_OPEN			0
1397c478bd9Sstevel@tonic-gate #define CHAN_INPUT_WAIT_DRAIN		1
1407c478bd9Sstevel@tonic-gate #define CHAN_INPUT_WAIT_OCLOSE		2
1417c478bd9Sstevel@tonic-gate #define CHAN_INPUT_CLOSED		3
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* possible output states */
1447c478bd9Sstevel@tonic-gate #define CHAN_OUTPUT_OPEN		0
1457c478bd9Sstevel@tonic-gate #define CHAN_OUTPUT_WAIT_DRAIN		1
1467c478bd9Sstevel@tonic-gate #define CHAN_OUTPUT_WAIT_IEOF		2
1477c478bd9Sstevel@tonic-gate #define CHAN_OUTPUT_CLOSED		3
1487c478bd9Sstevel@tonic-gate 
149*d80e6060Sjp161948 /*
150*d80e6060Sjp161948  * Other channel flag bits are specific to each type of channel and are
151*d80e6060Sjp161948  * defined locally with the code that uses them.
152*d80e6060Sjp161948  */
1537c478bd9Sstevel@tonic-gate #define CHAN_CLOSE_SENT			0x01
1547c478bd9Sstevel@tonic-gate #define CHAN_CLOSE_RCVD			0x02
1557c478bd9Sstevel@tonic-gate #define CHAN_EOF_SENT			0x04
1567c478bd9Sstevel@tonic-gate #define CHAN_EOF_RCVD			0x08
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* check whether 'efd' is still in use */
1597c478bd9Sstevel@tonic-gate #define CHANNEL_EFD_INPUT_ACTIVE(c) \
1607c478bd9Sstevel@tonic-gate 	(compat20 && c->extended_usage == CHAN_EXTENDED_READ && \
1617c478bd9Sstevel@tonic-gate 	(c->efd != -1 || \
1627c478bd9Sstevel@tonic-gate 	buffer_len(&c->extended) > 0))
1637c478bd9Sstevel@tonic-gate #define CHANNEL_EFD_OUTPUT_ACTIVE(c) \
1647c478bd9Sstevel@tonic-gate 	(compat20 && c->extended_usage == CHAN_EXTENDED_WRITE && \
1657c478bd9Sstevel@tonic-gate 	((c->efd != -1 && !(c->flags & (CHAN_EOF_RCVD|CHAN_CLOSE_RCVD))) || \
1667c478bd9Sstevel@tonic-gate 	buffer_len(&c->extended) > 0))
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* channel management */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate Channel	*channel_lookup(int);
1717c478bd9Sstevel@tonic-gate Channel *channel_new(char *, int, int, int, int, u_int, u_int, int, char *, int);
1727c478bd9Sstevel@tonic-gate void	 channel_set_fds(int, int, int, int, int, int, u_int);
1737c478bd9Sstevel@tonic-gate void	 channel_set_wait_for_exit(int, int);
1747c478bd9Sstevel@tonic-gate void	 channel_free(Channel *);
1757c478bd9Sstevel@tonic-gate void	 channel_free_all(void);
1767c478bd9Sstevel@tonic-gate void	 channel_stop_listening(void);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate void	 channel_send_open(int);
1797c478bd9Sstevel@tonic-gate void	 channel_request_start(int, char *, int);
1807c478bd9Sstevel@tonic-gate void	 channel_register_cleanup(int, channel_callback_fn *);
1817c478bd9Sstevel@tonic-gate void	 channel_register_confirm(int, channel_callback_fn *);
1827c478bd9Sstevel@tonic-gate void	 channel_register_filter(int, channel_filter_fn *);
1837c478bd9Sstevel@tonic-gate void	 channel_cancel_cleanup(int);
1847c478bd9Sstevel@tonic-gate int	 channel_close_fd(int *);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* protocol handler */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate void	 channel_input_close(int, u_int32_t, void *);
1897c478bd9Sstevel@tonic-gate void	 channel_input_close_confirmation(int, u_int32_t, void *);
1907c478bd9Sstevel@tonic-gate void	 channel_input_data(int, u_int32_t, void *);
1917c478bd9Sstevel@tonic-gate void	 channel_input_extended_data(int, u_int32_t, void *);
1927c478bd9Sstevel@tonic-gate void	 channel_input_ieof(int, u_int32_t, void *);
1937c478bd9Sstevel@tonic-gate void	 channel_input_oclose(int, u_int32_t, void *);
1947c478bd9Sstevel@tonic-gate void	 channel_input_open_confirmation(int, u_int32_t, void *);
1957c478bd9Sstevel@tonic-gate void	 channel_input_open_failure(int, u_int32_t, void *);
1967c478bd9Sstevel@tonic-gate void	 channel_input_port_open(int, u_int32_t, void *);
1977c478bd9Sstevel@tonic-gate void	 channel_input_window_adjust(int, u_int32_t, void *);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* file descriptor handling (read/write) */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate void	 channel_prepare_select(fd_set **, fd_set **, int *, int*, int);
2027c478bd9Sstevel@tonic-gate void     channel_after_select(fd_set *, fd_set *);
2037c478bd9Sstevel@tonic-gate void     channel_output_poll(void);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate int      channel_not_very_much_buffered_data(void);
2067c478bd9Sstevel@tonic-gate void     channel_close_all(void);
2077c478bd9Sstevel@tonic-gate int      channel_still_open(void);
2087c478bd9Sstevel@tonic-gate char	*channel_open_message(void);
2097c478bd9Sstevel@tonic-gate int	 channel_find_open(void);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /* tcp forwarding */
2127c478bd9Sstevel@tonic-gate void	 channel_set_af(int af);
2137c478bd9Sstevel@tonic-gate void     channel_permit_all_opens(void);
2147c478bd9Sstevel@tonic-gate void	 channel_add_permitted_opens(char *, int);
2157c478bd9Sstevel@tonic-gate void	 channel_clear_permitted_opens(void);
2167c478bd9Sstevel@tonic-gate void     channel_input_port_forward_request(int, int);
2177c478bd9Sstevel@tonic-gate int	 channel_connect_to(const char *, u_short);
2187c478bd9Sstevel@tonic-gate int	 channel_connect_by_listen_address(u_short);
2197c478bd9Sstevel@tonic-gate void	 channel_request_remote_forwarding(u_short, const char *, u_short);
2207c478bd9Sstevel@tonic-gate int	 channel_setup_local_fwd_listener(u_short, const char *, u_short, int);
2217c478bd9Sstevel@tonic-gate int	 channel_setup_remote_fwd_listener(const char *, u_short, int);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /* x11 forwarding */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate int	 x11_connect_display(void);
2267c478bd9Sstevel@tonic-gate int	 x11_create_display_inet(int, int, int, u_int *);
2277c478bd9Sstevel@tonic-gate void     x11_input_open(int, u_int32_t, void *);
2287c478bd9Sstevel@tonic-gate void	 x11_request_forwarding_with_spoofing(int, const char *, const char *);
2297c478bd9Sstevel@tonic-gate void	 deny_input_open(int, u_int32_t, void *);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* agent forwarding */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate void	 auth_request_forwarding(void);
2347c478bd9Sstevel@tonic-gate void	 auth_input_open_request(int, u_int32_t, void *);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* channel close */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate int	 chan_is_dead(Channel *, int);
2397c478bd9Sstevel@tonic-gate void	 chan_mark_dead(Channel *);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /* channel events */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate void	 chan_rcvd_oclose(Channel *);
2447c478bd9Sstevel@tonic-gate void	 chan_read_failed(Channel *);
2457c478bd9Sstevel@tonic-gate void	 chan_ibuf_empty(Channel *);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate void	 chan_rcvd_ieof(Channel *);
2487c478bd9Sstevel@tonic-gate void	 chan_write_failed(Channel *);
2497c478bd9Sstevel@tonic-gate void	 chan_obuf_empty(Channel *);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #ifdef __cplusplus
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate #endif
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate #endif /* _CHANNELS_H */
256