blob: 258085d29193d5ee27efcaf9e3c7ba8cbb116e3c [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Ben Lindstrom8b08d812002-03-26 02:09:41 +000040RCSID("$OpenBSD: packet.c,v 1.93 2002/03/24 16:01:13 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000051#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller33b13562000-04-04 14:38:59 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100055#include "ssh2.h"
56
Damien Miller874d77b2000-10-14 16:23:11 +110057#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "log.h"
61#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110062#include "misc.h"
Damien Miller33b13562000-04-04 14:38:59 +100063
64#ifdef PACKET_DEBUG
65#define DBG(x) x
66#else
67#define DBG(x)
68#endif
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * This variable contains the file descriptors used for communicating with
72 * the other side. connection_in is used for reading; connection_out for
73 * writing. These can be the same descriptor, in which case it is assumed to
74 * be a socket.
75 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076static int connection_in = -1;
77static int connection_out = -1;
78
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000080static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82/* Encryption context for receiving data. This is only used for decryption. */
83static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110084
85/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086static CipherContext send_context;
87
88/* Buffer for raw input data from the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000089Buffer input;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
91/* Buffer for raw output data going to the socket. */
Ben Lindstromf6027d32002-03-22 01:42:04 +000092Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Buffer for the partial outgoing packet being constructed. */
95static Buffer outgoing_packet;
96
97/* Buffer for the incoming packet currently being processed. */
98static Buffer incoming_packet;
99
100/* Scratch buffer for packet compression/decompression. */
101static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000102static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* Flag indicating whether packet compression/decompression is enabled. */
105static int packet_compression = 0;
106
Damien Miller6162d121999-11-21 13:23:52 +1100107/* default maximum packet size */
108int max_packet_size = 32768;
109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110/* Flag indicating whether this module has been initialized. */
111static int initialized = 0;
112
113/* Set to true if the connection is interactive. */
114static int interactive_mode = 0;
115
Damien Miller33b13562000-04-04 14:38:59 +1000116/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000117Newkeys *newkeys[MODE_MAX];
Ben Lindstromf6027d32002-03-22 01:42:04 +0000118static u_int32_t read_seqnr = 0;
119static u_int32_t send_seqnr = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000120
Damien Miller9f643902001-11-12 11:02:52 +1100121/* roundup current message to extra_pad bytes */
122static u_char extra_pad = 0;
123
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * Sets the descriptors used for communication. Disables encryption until
126 * packet_set_encryption_key is called.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128void
129packet_set_connection(int fd_in, int fd_out)
130{
Damien Miller874d77b2000-10-14 16:23:11 +1100131 Cipher *none = cipher_by_name("none");
132 if (none == NULL)
133 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100134 connection_in = fd_in;
135 connection_out = fd_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100136 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
137 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000138 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100139 if (!initialized) {
140 initialized = 1;
141 buffer_init(&input);
142 buffer_init(&output);
143 buffer_init(&outgoing_packet);
144 buffer_init(&incoming_packet);
145 }
146 /* Kludge: arrange the close function to be called from fatal(). */
147 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148}
149
Damien Miller34132e52000-01-14 15:45:46 +1100150/* Returns 1 if remote host is connected via socket, 0 if not. */
151
152int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000153packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100154{
155 struct sockaddr_storage from, to;
156 socklen_t fromlen, tolen;
157
158 /* filedescriptors in and out are the same, so it's a socket */
159 if (connection_in == connection_out)
160 return 1;
161 fromlen = sizeof(from);
162 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100163 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100164 return 0;
165 tolen = sizeof(to);
166 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100167 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100168 return 0;
169 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
170 return 0;
171 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
172 return 0;
173 return 1;
174}
175
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000176/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000177 * Exports an IV from the CipherContext required to export the key
178 * state back from the unprivileged child to the privileged parent
179 * process.
180 */
181
182void
183packet_get_keyiv(int mode, u_char *iv, u_int len)
184{
185 CipherContext *cc;
186
187 if (mode == MODE_OUT)
188 cc = &send_context;
189 else
190 cc = &receive_context;
191
192 cipher_get_keyiv(cc, iv, len);
193}
194
195int
196packet_get_keycontext(int mode, u_char *dat)
197{
198 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000199
Ben Lindstromf6027d32002-03-22 01:42:04 +0000200 if (mode == MODE_OUT)
201 cc = &send_context;
202 else
203 cc = &receive_context;
204
205 return (cipher_get_keycontext(cc, dat));
206}
207
208void
209packet_set_keycontext(int mode, u_char *dat)
210{
211 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000212
Ben Lindstromf6027d32002-03-22 01:42:04 +0000213 if (mode == MODE_OUT)
214 cc = &send_context;
215 else
216 cc = &receive_context;
217
218 cipher_set_keycontext(cc, dat);
219}
220
221int
222packet_get_keyiv_len(int mode)
223{
224 CipherContext *cc;
225
226 if (mode == MODE_OUT)
227 cc = &send_context;
228 else
229 cc = &receive_context;
230
231 return (cipher_get_keyiv_len(cc));
232}
233void
234packet_set_iv(int mode, u_char *dat)
235{
236 CipherContext *cc;
237
238 if (mode == MODE_OUT)
239 cc = &send_context;
240 else
241 cc = &receive_context;
242
243 cipher_set_keyiv(cc, dat);
244}
245int
246packet_get_ssh1_cipher()
247{
248 return (cipher_get_number(receive_context.cipher));
249}
250
251
252u_int32_t
253packet_get_seqnr(int mode)
254{
255 return (mode == MODE_IN ? read_seqnr : send_seqnr);
256}
257
258void
259packet_set_seqnr(int mode, u_int32_t seqnr)
260{
261 if (mode == MODE_IN)
262 read_seqnr = seqnr;
263 else if (mode == MODE_OUT)
264 send_seqnr = seqnr;
265 else
266 fatal("%s: bad mode %d", __FUNCTION__, mode);
267}
268
Damien Miller34132e52000-01-14 15:45:46 +1100269/* returns 1 if connection is via ipv4 */
270
271int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000272packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100273{
274 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100275 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100276
277 memset(&to, 0, sizeof(to));
278 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
279 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000280 if (to.ss_family == AF_INET)
281 return 1;
282#ifdef IPV4_IN_IPV6
283 if (to.ss_family == AF_INET6 &&
284 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
285 return 1;
286#endif
287 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100288}
289
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290/* Sets the connection into non-blocking mode. */
291
292void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000293packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Damien Miller95def091999-11-25 00:26:21 +1100295 /* Set the socket into non-blocking mode. */
296 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
297 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller95def091999-11-25 00:26:21 +1100299 if (connection_out != connection_in) {
300 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
301 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
302 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
305/* Returns the socket used for reading. */
306
307int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000308packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309{
Damien Miller95def091999-11-25 00:26:21 +1100310 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
313/* Returns the descriptor used for writing. */
314
315int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000316packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317{
Damien Miller95def091999-11-25 00:26:21 +1100318 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319}
320
321/* Closes the connection and clears and frees internal data structures. */
322
323void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000324packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325{
Damien Miller95def091999-11-25 00:26:21 +1100326 if (!initialized)
327 return;
328 initialized = 0;
329 if (connection_in == connection_out) {
330 shutdown(connection_out, SHUT_RDWR);
331 close(connection_out);
332 } else {
333 close(connection_in);
334 close(connection_out);
335 }
336 buffer_free(&input);
337 buffer_free(&output);
338 buffer_free(&outgoing_packet);
339 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000340 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100341 buffer_free(&compression_buffer);
342 buffer_compress_uninit();
343 }
Damien Miller963f6b22002-02-19 15:21:23 +1100344 cipher_cleanup(&send_context);
345 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346}
347
348/* Sets remote side protocol flags. */
349
350void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000351packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352{
Damien Miller95def091999-11-25 00:26:21 +1100353 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354}
355
356/* Returns the remote protocol flags set earlier by the above function. */
357
Ben Lindstrom46c16222000-12-22 01:43:59 +0000358u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000359packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Damien Miller95def091999-11-25 00:26:21 +1100361 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362}
363
Damien Miller5428f641999-11-25 11:54:57 +1100364/*
365 * Starts packet compression from the next packet on in both directions.
366 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
367 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static void
370packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000371{
372 if (compression_buffer_ready == 1)
373 return;
374 compression_buffer_ready = 1;
375 buffer_init(&compression_buffer);
376}
377
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378void
379packet_start_compression(int level)
380{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000381 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100382 fatal("Compression already enabled.");
383 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000384 packet_init_compression();
385 buffer_compress_init_send(level);
386 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387}
388
Damien Miller5428f641999-11-25 11:54:57 +1100389/*
Damien Miller5428f641999-11-25 11:54:57 +1100390 * Causes any further packets to be encrypted using the given key. The same
391 * key is used for both sending and reception. However, both directions are
392 * encrypted independently of each other.
393 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000395packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100396 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397{
Damien Miller874d77b2000-10-14 16:23:11 +1100398 Cipher *cipher = cipher_by_number(number);
399 if (cipher == NULL)
400 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000401 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100402 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Damien Miller963f6b22002-02-19 15:21:23 +1100403 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
404 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405}
406
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000407/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000409packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000411 u_char buf[9];
412 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Ben Lindstrom204e4882001-03-05 06:47:00 +0000414 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000415 len = compat20 ? 6 : 9;
416 memset(buf, 0, len - 1);
417 buf[len - 1] = type;
418 buffer_clear(&outgoing_packet);
419 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000420}
421
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000422/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423void
424packet_put_char(int value)
425{
Damien Miller95def091999-11-25 00:26:21 +1100426 char ch = value;
427 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000430packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller95def091999-11-25 00:26:21 +1100432 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100435packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller95def091999-11-25 00:26:21 +1100437 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438}
Damien Miller33b13562000-04-04 14:38:59 +1000439void
440packet_put_cstring(const char *str)
441{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000442 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000443}
Damien Miller33b13562000-04-04 14:38:59 +1000444void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100445packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000446{
447 buffer_append(&outgoing_packet, buf, len);
448}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449void
Damien Miller95def091999-11-25 00:26:21 +1100450packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451{
Damien Miller95def091999-11-25 00:26:21 +1100452 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
Damien Miller33b13562000-04-04 14:38:59 +1000454void
455packet_put_bignum2(BIGNUM * value)
456{
457 buffer_put_bignum2(&outgoing_packet, value);
458}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller5428f641999-11-25 11:54:57 +1100460/*
461 * Finalizes and sends the packet. If the encryption key has been set,
462 * encrypts the packet before sending.
463 */
Damien Miller95def091999-11-25 00:26:21 +1100464
Ben Lindstrombba81212001-06-25 05:01:22 +0000465static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000466packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000468 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100469 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000470 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100471 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Damien Miller5428f641999-11-25 11:54:57 +1100473 /*
474 * If using packet compression, compress the payload of the outgoing
475 * packet.
476 */
Damien Miller95def091999-11-25 00:26:21 +1100477 if (packet_compression) {
478 buffer_clear(&compression_buffer);
479 /* Skip padding. */
480 buffer_consume(&outgoing_packet, 8);
481 /* padding */
482 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
483 buffer_compress(&outgoing_packet, &compression_buffer);
484 buffer_clear(&outgoing_packet);
485 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100486 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100487 }
488 /* Compute packet length without padding (add checksum, remove padding). */
489 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Millere247cc42000-05-07 12:03:14 +1000491 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100492 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100493 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100494 cp = buffer_ptr(&outgoing_packet);
495 for (i = 0; i < padding; i++) {
496 if (i % 4 == 0)
497 rand = arc4random();
498 cp[7 - i] = rand & 0xff;
499 rand >>= 8;
500 }
501 }
502 buffer_consume(&outgoing_packet, 8 - padding);
503
504 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100505 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000506 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100507 PUT_32BIT(buf, checksum);
508 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509
510#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100511 fprintf(stderr, "packet_send plain: ");
512 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513#endif
514
Damien Miller95def091999-11-25 00:26:21 +1100515 /* Append to output. */
516 PUT_32BIT(buf, len);
517 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100518 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100519 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100520 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100521
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100523 fprintf(stderr, "encrypted: ");
524 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525#endif
526
Damien Miller95def091999-11-25 00:26:21 +1100527 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller5428f641999-11-25 11:54:57 +1100529 /*
530 * Note that the packet is now only buffered in output. It won\'t be
531 * actually sent until packet_write_wait or packet_write_poll is
532 * called.
533 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534}
535
Ben Lindstromf6027d32002-03-22 01:42:04 +0000536void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000537set_newkeys(int mode)
538{
539 Enc *enc;
540 Mac *mac;
541 Comp *comp;
542 CipherContext *cc;
Damien Miller963f6b22002-02-19 15:21:23 +1100543 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000544
545 debug("newkeys: mode %d", mode);
546
Damien Miller963f6b22002-02-19 15:21:23 +1100547 if (mode == MODE_OUT) {
548 cc = &send_context;
549 encrypt = CIPHER_ENCRYPT;
550 } else {
551 cc = &receive_context;
552 encrypt = CIPHER_DECRYPT;
553 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000554 if (newkeys[mode] != NULL) {
555 debug("newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100556 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000557 enc = &newkeys[mode]->enc;
558 mac = &newkeys[mode]->mac;
559 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000560 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000561 xfree(enc->name);
562 xfree(enc->iv);
563 xfree(enc->key);
564 xfree(mac->name);
565 xfree(mac->key);
566 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000567 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000568 }
569 newkeys[mode] = kex_get_newkeys(mode);
570 if (newkeys[mode] == NULL)
571 fatal("newkeys: no keys for mode %d", mode);
572 enc = &newkeys[mode]->enc;
573 mac = &newkeys[mode]->mac;
574 comp = &newkeys[mode]->comp;
575 if (mac->md != NULL)
576 mac->enabled = 1;
577 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100578 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
579 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000580 /* Deleting the keys does not gain extra security */
581 /* memset(enc->iv, 0, enc->block_size);
582 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000583 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000584 packet_init_compression();
585 if (mode == MODE_OUT)
586 buffer_compress_init_send(6);
587 else
588 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000589 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000590 }
591}
592
Damien Miller5428f641999-11-25 11:54:57 +1100593/*
Damien Miller33b13562000-04-04 14:38:59 +1000594 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
595 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000596static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000597packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000598{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000599 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100600 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000601 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100602 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000603 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000604 Enc *enc = NULL;
605 Mac *mac = NULL;
606 Comp *comp = NULL;
607 int block_size;
608
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000609 if (newkeys[MODE_OUT] != NULL) {
610 enc = &newkeys[MODE_OUT]->enc;
611 mac = &newkeys[MODE_OUT]->mac;
612 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000613 }
Damien Miller963f6b22002-02-19 15:21:23 +1100614 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000615
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000616 cp = buffer_ptr(&outgoing_packet);
617 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000618
619#ifdef PACKET_DEBUG
620 fprintf(stderr, "plain: ");
621 buffer_dump(&outgoing_packet);
622#endif
623
624 if (comp && comp->enabled) {
625 len = buffer_len(&outgoing_packet);
626 /* skip header, compress only payload */
627 buffer_consume(&outgoing_packet, 5);
628 buffer_clear(&compression_buffer);
629 buffer_compress(&outgoing_packet, &compression_buffer);
630 buffer_clear(&outgoing_packet);
631 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
632 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
633 buffer_len(&compression_buffer));
634 DBG(debug("compression: raw %d compressed %d", len,
635 buffer_len(&outgoing_packet)));
636 }
637
638 /* sizeof (packet_len + pad_len + payload) */
639 len = buffer_len(&outgoing_packet);
640
641 /*
642 * calc size of padding, alloc space, get random data,
643 * minimum padding is 4 bytes
644 */
645 padlen = block_size - (len % block_size);
646 if (padlen < 4)
647 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100648 if (extra_pad) {
649 /* will wrap if extra_pad+padlen > 255 */
650 extra_pad = roundup(extra_pad, block_size);
651 pad = extra_pad - ((len + padlen) % extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000652 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Damien Miller9f643902001-11-12 11:02:52 +1100653 pad, len, padlen, extra_pad);
654 padlen += pad;
655 extra_pad = 0;
656 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100657 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100658 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000659 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000660 for (i = 0; i < padlen; i++) {
661 if (i % 4 == 0)
662 rand = arc4random();
663 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000664 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000665 }
Damien Millere247cc42000-05-07 12:03:14 +1000666 } else {
667 /* clear padding */
668 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000669 }
670 /* packet_length includes payload, padding and padding length field */
671 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000672 cp = buffer_ptr(&outgoing_packet);
673 PUT_32BIT(cp, packet_length);
674 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000675 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
676
677 /* compute MAC over seqnr and packet(length fields, payload, padding) */
678 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000679 macbuf = mac_compute(mac, send_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100680 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000681 buffer_len(&outgoing_packet));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000682 DBG(debug("done calc MAC out #%d", send_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000683 }
684 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100685 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100686 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000687 buffer_len(&outgoing_packet));
688 /* append unencrypted MAC */
689 if (mac && mac->enabled)
690 buffer_append(&output, (char *)macbuf, mac->mac_len);
691#ifdef PACKET_DEBUG
692 fprintf(stderr, "encrypted: ");
693 buffer_dump(&output);
694#endif
Damien Miller4af51302000-04-16 11:18:38 +1000695 /* increment sequence number for outgoing packets */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000696 if (++send_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000697 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000698 buffer_clear(&outgoing_packet);
699
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000700 if (type == SSH2_MSG_NEWKEYS)
701 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000702}
703
704void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000705packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000706{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000707 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000708 packet_send2();
709 else
710 packet_send1();
711 DBG(debug("packet_send done"));
712}
713
Damien Miller33b13562000-04-04 14:38:59 +1000714/*
Damien Miller5428f641999-11-25 11:54:57 +1100715 * Waits until a packet has been received, and returns its type. Note that
716 * no other data is processed until this returns, so this function should not
717 * be used during the interactive session.
718 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
720int
Damien Millerdff50992002-01-22 23:16:32 +1100721packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722{
Damien Miller95def091999-11-25 00:26:21 +1100723 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000724 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100725 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000726 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000728 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
729 sizeof(fd_mask));
730
Damien Miller95def091999-11-25 00:26:21 +1100731 /* Since we are blocking, ensure that all written packets have been sent. */
732 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733
Damien Miller95def091999-11-25 00:26:21 +1100734 /* Stay in the loop until we have received a complete packet. */
735 for (;;) {
736 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100737 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000738 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000739 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100740 || type == SSH_SMSG_FAILURE
741 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000742 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100743 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100744 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000745 if (type != SSH_MSG_NONE) {
746 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100747 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000748 }
Damien Miller5428f641999-11-25 11:54:57 +1100749 /*
750 * Otherwise, wait for some data to arrive, add it to the
751 * buffer, and try again.
752 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000753 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
754 sizeof(fd_mask));
755 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100756
Damien Miller95def091999-11-25 00:26:21 +1100757 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000758 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000759 (errno == EAGAIN || errno == EINTR))
760 ;
Damien Miller5428f641999-11-25 11:54:57 +1100761
Damien Miller95def091999-11-25 00:26:21 +1100762 /* Read data from the socket. */
763 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100764 if (len == 0) {
765 log("Connection closed by %.200s", get_remote_ipaddr());
766 fatal_cleanup();
767 }
Damien Miller95def091999-11-25 00:26:21 +1100768 if (len < 0)
769 fatal("Read from socket failed: %.100s", strerror(errno));
770 /* Append it to the buffer. */
771 packet_process_incoming(buf, len);
772 }
773 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774}
775
Damien Miller278f9072001-12-21 15:00:19 +1100776int
Damien Millerdff50992002-01-22 23:16:32 +1100777packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100778{
Damien Millerdff50992002-01-22 23:16:32 +1100779 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100780}
781
Damien Miller5428f641999-11-25 11:54:57 +1100782/*
783 * Waits until a packet has been received, verifies that its type matches
784 * that given, and gives a fatal error and exits if there is a mismatch.
785 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
787void
Damien Millerdff50992002-01-22 23:16:32 +1100788packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789{
Damien Miller95def091999-11-25 00:26:21 +1100790 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791
Damien Millerdff50992002-01-22 23:16:32 +1100792 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100793 if (type != expected_type)
794 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000795 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796}
797
798/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100799 * packet_process_incoming. If so, reads the packet; otherwise returns
800 * SSH_MSG_NONE. This does not wait for data from the connection.
801 *
802 * SSH_MSG_DISCONNECT is handled specially here. Also,
803 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
804 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100805 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Ben Lindstrombba81212001-06-25 05:01:22 +0000807static int
Damien Millerdff50992002-01-22 23:16:32 +1100808packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000810 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000811 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000812 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813
Damien Miller95def091999-11-25 00:26:21 +1100814 /* Check if input size is less than minimum packet size. */
815 if (buffer_len(&input) < 4 + 8)
816 return SSH_MSG_NONE;
817 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000818 cp = buffer_ptr(&input);
819 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100820 if (len < 1 + 2 + 2 || len > 256 * 1024)
821 packet_disconnect("Bad packet length %d.", len);
822 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823
Damien Miller95def091999-11-25 00:26:21 +1100824 /* Check if the packet has been entirely received. */
825 if (buffer_len(&input) < 4 + padded_len)
826 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827
Damien Miller95def091999-11-25 00:26:21 +1100828 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829
Damien Miller95def091999-11-25 00:26:21 +1100830 /* Consume packet length. */
831 buffer_consume(&input, 4);
832
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000833 /*
834 * Cryptographic attack detector for ssh
835 * (C)1998 CORE-SDI, Buenos Aires Argentina
836 * Ariel Futoransky(futo@core-sdi.com)
837 */
Damien Miller963f6b22002-02-19 15:21:23 +1100838 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000839 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
840 packet_disconnect("crc32 compensation attack: network attack detected");
841
842 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100843 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100844 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100845 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000846
Damien Miller95def091999-11-25 00:26:21 +1100847 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
849#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100850 fprintf(stderr, "read_poll plain: ");
851 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Miller95def091999-11-25 00:26:21 +1100854 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100855 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000856 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857
Damien Miller95def091999-11-25 00:26:21 +1100858 /* Skip padding. */
859 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100860
Damien Miller95def091999-11-25 00:26:21 +1100861 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100862 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100863 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000864 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000866 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
867 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100868 if (checksum != stored_checksum)
869 packet_disconnect("Corrupted check bytes on input.");
870 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Damien Miller95def091999-11-25 00:26:21 +1100872 if (packet_compression) {
873 buffer_clear(&compression_buffer);
874 buffer_uncompress(&incoming_packet, &compression_buffer);
875 buffer_clear(&incoming_packet);
876 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000877 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100878 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000879 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000880 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881}
Damien Miller95def091999-11-25 00:26:21 +1100882
Ben Lindstrombba81212001-06-25 05:01:22 +0000883static int
Damien Millerdff50992002-01-22 23:16:32 +1100884packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000885{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000886 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000887 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000888 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000889 int maclen, block_size;
890 Enc *enc = NULL;
891 Mac *mac = NULL;
892 Comp *comp = NULL;
893
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000894 if (newkeys[MODE_IN] != NULL) {
895 enc = &newkeys[MODE_IN]->enc;
896 mac = &newkeys[MODE_IN]->mac;
897 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000898 }
899 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +1100900 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000901
902 if (packet_length == 0) {
903 /*
904 * check if input size is less than the cipher block size,
905 * decrypt first block and extract length of incoming packet
906 */
907 if (buffer_len(&input) < block_size)
908 return SSH_MSG_NONE;
909 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100910 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +1100911 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000912 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000913 cp = buffer_ptr(&incoming_packet);
914 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +1000915 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
916 buffer_dump(&incoming_packet);
917 packet_disconnect("Bad packet length %d.", packet_length);
918 }
919 DBG(debug("input: packet len %d", packet_length+4));
920 buffer_consume(&input, block_size);
921 }
922 /* we have a partial packet of block_size bytes */
923 need = 4 + packet_length - block_size;
924 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
925 need, maclen));
926 if (need % block_size != 0)
927 fatal("padding error: need %d block %d mod %d",
928 need, block_size, need % block_size);
929 /*
930 * check if the entire packet has been received and
931 * decrypt into incoming_packet
932 */
933 if (buffer_len(&input) < need + maclen)
934 return SSH_MSG_NONE;
935#ifdef PACKET_DEBUG
936 fprintf(stderr, "read_poll enc/full: ");
937 buffer_dump(&input);
938#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100939 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +1100940 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000941 buffer_consume(&input, need);
942 /*
943 * compute MAC over seqnr and packet,
944 * increment sequence number for incoming packet
945 */
Damien Miller4af51302000-04-16 11:18:38 +1000946 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000947 macbuf = mac_compute(mac, read_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100948 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000949 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000950 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100951 packet_disconnect("Corrupted MAC on input.");
Ben Lindstromf6027d32002-03-22 01:42:04 +0000952 DBG(debug("MAC #%d ok", read_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000953 buffer_consume(&input, mac->mac_len);
954 }
Damien Miller278f9072001-12-21 15:00:19 +1100955 if (seqnr_p != NULL)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000956 *seqnr_p = read_seqnr;
957 if (++read_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000958 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000959
960 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100961 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000962 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +1000963 DBG(debug("input: padlen %d", padlen));
964 if (padlen < 4)
965 packet_disconnect("Corrupted padlen %d on input.", padlen);
966
967 /* skip packet size + padlen, discard padding */
968 buffer_consume(&incoming_packet, 4 + 1);
969 buffer_consume_end(&incoming_packet, padlen);
970
971 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
972 if (comp && comp->enabled) {
973 buffer_clear(&compression_buffer);
974 buffer_uncompress(&incoming_packet, &compression_buffer);
975 buffer_clear(&incoming_packet);
976 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
977 buffer_len(&compression_buffer));
978 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
979 }
980 /*
981 * get packet type, implies consume.
982 * return length of payload (without type field)
983 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000984 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000985 if (type == SSH2_MSG_NEWKEYS)
986 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +1000987#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000988 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000989 buffer_dump(&incoming_packet);
990#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000991 /* reset for next packet */
992 packet_length = 0;
993 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000994}
995
996int
Damien Millerdff50992002-01-22 23:16:32 +1100997packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000998{
Damien Miller659811f2002-01-22 23:23:11 +1100999 int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001000 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001001 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001002
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001003 for (;;) {
1004 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001005 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001006 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001007 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001008 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001009 case SSH2_MSG_IGNORE:
1010 break;
1011 case SSH2_MSG_DEBUG:
1012 packet_get_char();
1013 msg = packet_get_string(NULL);
1014 debug("Remote: %.900s", msg);
1015 xfree(msg);
1016 msg = packet_get_string(NULL);
1017 xfree(msg);
1018 break;
1019 case SSH2_MSG_DISCONNECT:
1020 reason = packet_get_int();
1021 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001022 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
1023 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001024 xfree(msg);
1025 fatal_cleanup();
1026 break;
Damien Miller659811f2002-01-22 23:23:11 +11001027 case SSH2_MSG_UNIMPLEMENTED:
1028 seqnr = packet_get_int();
1029 debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
1030 break;
Damien Miller33b13562000-04-04 14:38:59 +10001031 default:
1032 return type;
1033 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001034 }
Damien Miller33b13562000-04-04 14:38:59 +10001035 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001036 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001037 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001038 case SSH_MSG_IGNORE:
1039 break;
1040 case SSH_MSG_DEBUG:
1041 msg = packet_get_string(NULL);
1042 debug("Remote: %.900s", msg);
1043 xfree(msg);
1044 break;
1045 case SSH_MSG_DISCONNECT:
1046 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001047 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1048 msg);
Damien Miller33b13562000-04-04 14:38:59 +10001049 fatal_cleanup();
1050 xfree(msg);
1051 break;
1052 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001053 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001054 DBG(debug("received packet type %d", type));
1055 return type;
1056 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001057 }
Damien Miller33b13562000-04-04 14:38:59 +10001058 }
1059 }
1060}
1061
Damien Miller278f9072001-12-21 15:00:19 +11001062int
Damien Millerdff50992002-01-22 23:16:32 +11001063packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001064{
Damien Millerdff50992002-01-22 23:16:32 +11001065 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001066}
1067
Damien Miller5428f641999-11-25 11:54:57 +11001068/*
1069 * Buffers the given amount of input characters. This is intended to be used
1070 * together with packet_read_poll.
1071 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
1073void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001074packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075{
Damien Miller95def091999-11-25 00:26:21 +11001076 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077}
1078
1079/* Returns a character from the packet. */
1080
Ben Lindstrom46c16222000-12-22 01:43:59 +00001081u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001082packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083{
Damien Miller95def091999-11-25 00:26:21 +11001084 char ch;
1085 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001086 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087}
1088
1089/* Returns an integer from the packet data. */
1090
Ben Lindstrom46c16222000-12-22 01:43:59 +00001091u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001092packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093{
Damien Miller95def091999-11-25 00:26:21 +11001094 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095}
1096
Damien Miller5428f641999-11-25 11:54:57 +11001097/*
1098 * Returns an arbitrary precision integer from the packet data. The integer
1099 * must have been initialized before this call.
1100 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
1102void
Damien Millerd432ccf2002-01-22 23:14:44 +11001103packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104{
Damien Miller76e1e362002-01-22 23:15:57 +11001105 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106}
1107
Damien Miller33b13562000-04-04 14:38:59 +10001108void
Damien Millerd432ccf2002-01-22 23:14:44 +11001109packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001110{
Damien Miller76e1e362002-01-22 23:15:57 +11001111 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001112}
1113
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001114void *
Damien Miller33b13562000-04-04 14:38:59 +10001115packet_get_raw(int *length_ptr)
1116{
1117 int bytes = buffer_len(&incoming_packet);
1118 if (length_ptr != NULL)
1119 *length_ptr = bytes;
1120 return buffer_ptr(&incoming_packet);
1121}
1122
Damien Miller4af51302000-04-16 11:18:38 +10001123int
1124packet_remaining(void)
1125{
1126 return buffer_len(&incoming_packet);
1127}
1128
Damien Miller5428f641999-11-25 11:54:57 +11001129/*
1130 * Returns a string from the packet data. The string is allocated using
1131 * xmalloc; it is the responsibility of the calling program to free it when
1132 * no longer needed. The length_ptr argument may be NULL, or point to an
1133 * integer into which the length of the string is stored.
1134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001136void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001137packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138{
Damien Miller95def091999-11-25 00:26:21 +11001139 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140}
1141
Damien Miller5428f641999-11-25 11:54:57 +11001142/*
1143 * Sends a diagnostic message from the server to the client. This message
1144 * can be sent at any time (but not while constructing another message). The
1145 * message is printed immediately, but only if the client is being executed
1146 * in verbose mode. These messages are primarily intended to ease debugging
1147 * authentication problems. The length of the formatted message must not
1148 * exceed 1024 bytes. This will automatically call packet_write_wait.
1149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001150
1151void
Damien Miller95def091999-11-25 00:26:21 +11001152packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153{
Damien Miller95def091999-11-25 00:26:21 +11001154 char buf[1024];
1155 va_list args;
1156
Ben Lindstroma14ee472000-12-07 01:24:58 +00001157 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1158 return;
1159
Damien Miller95def091999-11-25 00:26:21 +11001160 va_start(args, fmt);
1161 vsnprintf(buf, sizeof(buf), fmt, args);
1162 va_end(args);
1163
Damien Miller7c8af4f2000-05-01 08:24:07 +10001164 if (compat20) {
1165 packet_start(SSH2_MSG_DEBUG);
1166 packet_put_char(0); /* bool: always display */
1167 packet_put_cstring(buf);
1168 packet_put_cstring("");
1169 } else {
1170 packet_start(SSH_MSG_DEBUG);
1171 packet_put_cstring(buf);
1172 }
Damien Miller95def091999-11-25 00:26:21 +11001173 packet_send();
1174 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175}
1176
Damien Miller5428f641999-11-25 11:54:57 +11001177/*
1178 * Logs the error plus constructs and sends a disconnect packet, closes the
1179 * connection, and exits. This function never returns. The error message
1180 * should not contain a newline. The length of the formatted message must
1181 * not exceed 1024 bytes.
1182 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183
1184void
Damien Miller95def091999-11-25 00:26:21 +11001185packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186{
Damien Miller95def091999-11-25 00:26:21 +11001187 char buf[1024];
1188 va_list args;
1189 static int disconnecting = 0;
1190 if (disconnecting) /* Guard against recursive invocations. */
1191 fatal("packet_disconnect called recursively.");
1192 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
Damien Miller5428f641999-11-25 11:54:57 +11001194 /*
1195 * Format the message. Note that the caller must make sure the
1196 * message is of limited size.
1197 */
Damien Miller95def091999-11-25 00:26:21 +11001198 va_start(args, fmt);
1199 vsnprintf(buf, sizeof(buf), fmt, args);
1200 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001203 if (compat20) {
1204 packet_start(SSH2_MSG_DISCONNECT);
1205 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1206 packet_put_cstring(buf);
1207 packet_put_cstring("");
1208 } else {
1209 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001210 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001211 }
Damien Miller95def091999-11-25 00:26:21 +11001212 packet_send();
1213 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214
Damien Miller95def091999-11-25 00:26:21 +11001215 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001216 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217
Damien Miller95def091999-11-25 00:26:21 +11001218 /* Close the connection. */
1219 packet_close();
1220
1221 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001222 log("Disconnecting: %.100s", buf);
1223 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224}
1225
Damien Miller5428f641999-11-25 11:54:57 +11001226/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227
1228void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001229packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230{
Damien Miller95def091999-11-25 00:26:21 +11001231 int len = buffer_len(&output);
1232 if (len > 0) {
1233 len = write(connection_out, buffer_ptr(&output), len);
1234 if (len <= 0) {
1235 if (errno == EAGAIN)
1236 return;
1237 else
1238 fatal("Write failed: %.100s", strerror(errno));
1239 }
1240 buffer_consume(&output, len);
1241 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
1243
Damien Miller5428f641999-11-25 11:54:57 +11001244/*
1245 * Calls packet_write_poll repeatedly until all pending output data has been
1246 * written.
1247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248
1249void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001250packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001252 fd_set *setp;
1253
1254 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1255 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001256 packet_write_poll();
1257 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001258 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1259 sizeof(fd_mask));
1260 FD_SET(connection_out, setp);
1261 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001262 (errno == EAGAIN || errno == EINTR))
1263 ;
Damien Miller95def091999-11-25 00:26:21 +11001264 packet_write_poll();
1265 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001266 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267}
1268
1269/* Returns true if there is buffered data to write to the connection. */
1270
1271int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001272packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273{
Damien Miller95def091999-11-25 00:26:21 +11001274 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275}
1276
1277/* Returns true if there is not too much data to write to the connection. */
1278
1279int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001280packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281{
Damien Miller95def091999-11-25 00:26:21 +11001282 if (interactive_mode)
1283 return buffer_len(&output) < 16384;
1284 else
1285 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286}
1287
1288/* Informs that the current session is interactive. Sets IP flags for that. */
1289
1290void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001291packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001293 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001294#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001295 int lowdelay = IPTOS_LOWDELAY;
1296 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001297#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001299 if (called)
1300 return;
1301 called = 1;
1302
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Record that we are in interactive mode. */
1304 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305
Damien Miller34132e52000-01-14 15:45:46 +11001306 /* Only set socket options if using a socket. */
1307 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001308 return;
Damien Miller34132e52000-01-14 15:45:46 +11001309 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001310 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001311 */
Damien Miller95def091999-11-25 00:26:21 +11001312 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001313 /*
1314 * Set IP options for an interactive connection. Use
1315 * IPTOS_LOWDELAY and TCP_NODELAY.
1316 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001317#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001318 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001319 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom733a2352002-03-05 01:31:28 +00001320 &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001321 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001322 strerror(errno));
1323 }
Damien Miller615f9392000-05-17 22:53:33 +10001324#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001325 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001326 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001327 /*
1328 * Set IP options for a non-interactive connection. Use
1329 * IPTOS_THROUGHPUT.
1330 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001331#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom733a2352002-03-05 01:31:28 +00001332 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001333 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001334 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001335#endif
Damien Miller95def091999-11-25 00:26:21 +11001336 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337}
1338
1339/* Returns true if the current connection is interactive. */
1340
1341int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001342packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343{
Damien Miller95def091999-11-25 00:26:21 +11001344 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001345}
Damien Miller6162d121999-11-21 13:23:52 +11001346
1347int
1348packet_set_maxsize(int s)
1349{
Damien Miller95def091999-11-25 00:26:21 +11001350 static int called = 0;
1351 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001352 log("packet_set_maxsize: called twice: old %d new %d",
1353 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001354 return -1;
1355 }
1356 if (s < 4 * 1024 || s > 1024 * 1024) {
1357 log("packet_set_maxsize: bad size %d", s);
1358 return -1;
1359 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001360 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001361 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001362 max_packet_size = s;
1363 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001364}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001365
Damien Miller9f643902001-11-12 11:02:52 +11001366/* roundup current message to pad bytes */
1367void
1368packet_add_padding(u_char pad)
1369{
1370 extra_pad = pad;
1371}
1372
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001373/*
1374 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001375 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001376 * byte SSH_MSG_IGNORE
1377 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001378 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001379 * All implementations MUST understand (and ignore) this message at any
1380 * time (after receiving the protocol version). No implementation is
1381 * required to send them. This message can be used as an additional
1382 * protection measure against advanced traffic analysis techniques.
1383 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001384void
1385packet_send_ignore(int nbytes)
1386{
1387 u_int32_t rand = 0;
1388 int i;
1389
1390 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001391 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001392 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001393 if (i % 4 == 0)
1394 rand = arc4random();
1395 packet_put_char(rand & 0xff);
1396 rand >>= 8;
1397 }
1398}