1/* $OpenBSD: packet.h,v 1.96 2023/12/18 14:45:17 djm Exp $ */
2
3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 *                    All rights reserved
7 * Interface for the packet protocol functions.
8 *
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose.  Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 */
15
16#ifndef PACKET_H
17#define PACKET_H
18
19#include <termios.h>
20
21#ifdef WITH_OPENSSL
22# include <openssl/bn.h>
23# ifdef OPENSSL_HAS_ECC
24#  include <openssl/ec.h>
25# else /* OPENSSL_HAS_ECC */
26#  define EC_KEY	void
27#  define EC_GROUP	void
28#  define EC_POINT	void
29# endif /* OPENSSL_HAS_ECC */
30#else /* WITH_OPENSSL */
31# define BIGNUM		void
32# define EC_KEY		void
33# define EC_GROUP	void
34# define EC_POINT	void
35#endif /* WITH_OPENSSL */
36
37#include <signal.h>
38#include "openbsd-compat/sys-queue.h"
39
40struct kex;
41struct sshkey;
42struct sshbuf;
43struct session_state;	/* private session data */
44
45#include "dispatch.h"	/* typedef, DISPATCH_MAX */
46
47struct key_entry {
48	TAILQ_ENTRY(key_entry) next;
49	struct sshkey *key;
50};
51
52struct ssh {
53	/* Session state */
54	struct session_state *state;
55
56	/* Key exchange */
57	struct kex *kex;
58
59	/* cached local and remote ip addresses and ports */
60	char *remote_ipaddr;
61	int remote_port;
62	char *local_ipaddr;
63	int local_port;
64	char *rdomain_in;
65
66	/* Optional preamble for log messages (e.g. username) */
67	char *log_preamble;
68
69	/* Dispatcher table */
70	dispatch_fn *dispatch[DISPATCH_MAX];
71	/* number of packets to ignore in the dispatcher */
72	int dispatch_skip_packets;
73
74	/* datafellows */
75	int compat;
76
77	/* Lists for private and public keys */
78	TAILQ_HEAD(, key_entry) private_keys;
79	TAILQ_HEAD(, key_entry) public_keys;
80
81	/* Client/Server authentication context */
82	void *authctxt;
83
84	/* Channels context */
85	struct ssh_channels *chanctxt;
86
87	/* APP data */
88	void *app_data;
89};
90
91typedef int (ssh_packet_hook_fn)(struct ssh *, struct sshbuf *,
92    u_char *, void *);
93
94struct ssh *ssh_alloc_session_state(void);
95struct ssh *ssh_packet_set_connection(struct ssh *, int, int);
96void     ssh_packet_set_timeout(struct ssh *, int, int);
97int	 ssh_packet_stop_discard(struct ssh *);
98int	 ssh_packet_connection_af(struct ssh *);
99void     ssh_packet_set_nonblocking(struct ssh *);
100int      ssh_packet_get_connection_in(struct ssh *);
101int      ssh_packet_get_connection_out(struct ssh *);
102void     ssh_packet_close(struct ssh *);
103void	 ssh_packet_set_input_hook(struct ssh *, ssh_packet_hook_fn *, void *);
104void	 ssh_packet_clear_keys(struct ssh *);
105void	 ssh_clear_newkeys(struct ssh *, int);
106
107int	 ssh_packet_is_rekeying(struct ssh *);
108int	 ssh_packet_check_rekey(struct ssh *);
109void     ssh_packet_set_protocol_flags(struct ssh *, u_int);
110u_int	 ssh_packet_get_protocol_flags(struct ssh *);
111void	 ssh_packet_set_tos(struct ssh *, int);
112void     ssh_packet_set_interactive(struct ssh *, int, int, int);
113int      ssh_packet_is_interactive(struct ssh *);
114void     ssh_packet_set_server(struct ssh *);
115void     ssh_packet_set_authenticated(struct ssh *);
116void     ssh_packet_set_mux(struct ssh *);
117int	 ssh_packet_get_mux(struct ssh *);
118int	 ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
119    __attribute__((format(printf, 2, 3)));
120
121int	 ssh_packet_log_type(u_char);
122
123int	 ssh_packet_send2_wrapped(struct ssh *);
124int	 ssh_packet_send2(struct ssh *);
125
126int      ssh_packet_read(struct ssh *);
127int      ssh_packet_read_poll(struct ssh *);
128int ssh_packet_read_poll2(struct ssh *, u_char *, u_int32_t *seqnr_p);
129int	 ssh_packet_process_incoming(struct ssh *, const char *buf, u_int len);
130int	 ssh_packet_process_read(struct ssh *, int);
131int      ssh_packet_read_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
132int      ssh_packet_read_poll_seqnr(struct ssh *, u_char *, u_int32_t *seqnr_p);
133
134const void *ssh_packet_get_string_ptr(struct ssh *, u_int *length_ptr);
135void     ssh_packet_disconnect(struct ssh *, const char *fmt, ...)
136	__attribute__((format(printf, 2, 3)))
137	__attribute__((noreturn));
138void     ssh_packet_send_debug(struct ssh *, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
139
140int	 ssh_set_newkeys(struct ssh *, int mode);
141void	 ssh_packet_get_bytes(struct ssh *, u_int64_t *, u_int64_t *);
142
143int	 ssh_packet_write_poll(struct ssh *);
144int	 ssh_packet_write_wait(struct ssh *);
145int      ssh_packet_have_data_to_write(struct ssh *);
146int      ssh_packet_not_very_much_data_to_write(struct ssh *);
147int	 ssh_packet_interactive_data_to_write(struct ssh *);
148
149int	 ssh_packet_connection_is_on_socket(struct ssh *);
150int	 ssh_packet_remaining(struct ssh *);
151
152void	 ssh_tty_make_modes(struct ssh *, int, struct termios *);
153void	 ssh_tty_parse_modes(struct ssh *, int);
154
155void	 ssh_packet_set_alive_timeouts(struct ssh *, int);
156int	 ssh_packet_inc_alive_timeouts(struct ssh *);
157int	 ssh_packet_set_maxsize(struct ssh *, u_int);
158u_int	 ssh_packet_get_maxsize(struct ssh *);
159
160int	 ssh_packet_get_state(struct ssh *, struct sshbuf *);
161int	 ssh_packet_set_state(struct ssh *, struct sshbuf *);
162
163const char *ssh_remote_ipaddr(struct ssh *);
164int	 ssh_remote_port(struct ssh *);
165const char *ssh_local_ipaddr(struct ssh *);
166int	 ssh_local_port(struct ssh *);
167const char *ssh_packet_rdomain_in(struct ssh *);
168
169void	 ssh_packet_set_rekey_limits(struct ssh *, u_int64_t, u_int32_t);
170time_t	 ssh_packet_get_rekey_timeout(struct ssh *);
171
172void	*ssh_packet_get_input(struct ssh *);
173void	*ssh_packet_get_output(struct ssh *);
174
175/* new API */
176int	sshpkt_start(struct ssh *ssh, u_char type);
177int	sshpkt_send(struct ssh *ssh);
178int     sshpkt_disconnect(struct ssh *, const char *fmt, ...)
179	    __attribute__((format(printf, 2, 3)));
180int	sshpkt_add_padding(struct ssh *, u_char);
181void	sshpkt_fatal(struct ssh *ssh, int r, const char *fmt, ...)
182	    __attribute__((format(printf, 3, 4)))
183	    __attribute__((noreturn));
184int	sshpkt_msg_ignore(struct ssh *, u_int);
185
186int	sshpkt_put(struct ssh *ssh, const void *v, size_t len);
187int	sshpkt_putb(struct ssh *ssh, const struct sshbuf *b);
188int	sshpkt_put_u8(struct ssh *ssh, u_char val);
189int	sshpkt_put_u32(struct ssh *ssh, u_int32_t val);
190int	sshpkt_put_u64(struct ssh *ssh, u_int64_t val);
191int	sshpkt_put_string(struct ssh *ssh, const void *v, size_t len);
192int	sshpkt_put_cstring(struct ssh *ssh, const void *v);
193int	sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v);
194int	sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g);
195int	sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v);
196
197int	sshpkt_get(struct ssh *ssh, void *valp, size_t len);
198int	sshpkt_get_u8(struct ssh *ssh, u_char *valp);
199int	sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp);
200int	sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp);
201int	sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp);
202int	sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
203int	sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
204int	sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
205int	sshpkt_getb_froms(struct ssh *ssh, struct sshbuf **valp);
206int	sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
207int	sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
208int	sshpkt_get_end(struct ssh *ssh);
209void	sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
210const u_char	*sshpkt_ptr(struct ssh *, size_t *lenp);
211
212#if !defined(WITH_OPENSSL)
213# undef BIGNUM
214# undef EC_KEY
215# undef EC_GROUP
216# undef EC_POINT
217#elif !defined(OPENSSL_HAS_ECC)
218# undef EC_KEY
219# undef EC_GROUP
220# undef EC_POINT
221#endif
222
223#endif				/* PACKET_H */
224