blob: 25de349517afad821076a8b5bbf183df9d7e9132 [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 Lindstromf6027d32002-03-22 01:42:04 +000040RCSID("$OpenBSD: packet.c,v 1.91 2002/03/18 17:16:38 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 Lindstromf6027d32002-03-22 01:42:04 +0000176/*
177 * 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;
199
200 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;
212
213 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;
280 if (to.ss_family != AF_INET)
281 return 0;
282 return 1;
283}
284
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285/* Sets the connection into non-blocking mode. */
286
287void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000288packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289{
Damien Miller95def091999-11-25 00:26:21 +1100290 /* Set the socket into non-blocking mode. */
291 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
292 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Miller95def091999-11-25 00:26:21 +1100294 if (connection_out != connection_in) {
295 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
296 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
297 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
300/* Returns the socket used for reading. */
301
302int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000303packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304{
Damien Miller95def091999-11-25 00:26:21 +1100305 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
308/* Returns the descriptor used for writing. */
309
310int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000311packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Miller95def091999-11-25 00:26:21 +1100313 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314}
315
316/* Closes the connection and clears and frees internal data structures. */
317
318void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000319packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Damien Miller95def091999-11-25 00:26:21 +1100321 if (!initialized)
322 return;
323 initialized = 0;
324 if (connection_in == connection_out) {
325 shutdown(connection_out, SHUT_RDWR);
326 close(connection_out);
327 } else {
328 close(connection_in);
329 close(connection_out);
330 }
331 buffer_free(&input);
332 buffer_free(&output);
333 buffer_free(&outgoing_packet);
334 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000335 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100336 buffer_free(&compression_buffer);
337 buffer_compress_uninit();
338 }
Damien Miller963f6b22002-02-19 15:21:23 +1100339 cipher_cleanup(&send_context);
340 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
343/* Sets remote side protocol flags. */
344
345void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000346packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347{
Damien Miller95def091999-11-25 00:26:21 +1100348 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
351/* Returns the remote protocol flags set earlier by the above function. */
352
Ben Lindstrom46c16222000-12-22 01:43:59 +0000353u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000354packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller95def091999-11-25 00:26:21 +1100356 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
Damien Miller5428f641999-11-25 11:54:57 +1100359/*
360 * Starts packet compression from the next packet on in both directions.
361 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
362 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Ben Lindstrombba81212001-06-25 05:01:22 +0000364static void
365packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000366{
367 if (compression_buffer_ready == 1)
368 return;
369 compression_buffer_ready = 1;
370 buffer_init(&compression_buffer);
371}
372
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373void
374packet_start_compression(int level)
375{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000376 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100377 fatal("Compression already enabled.");
378 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000379 packet_init_compression();
380 buffer_compress_init_send(level);
381 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
Damien Miller5428f641999-11-25 11:54:57 +1100384/*
Damien Miller5428f641999-11-25 11:54:57 +1100385 * Causes any further packets to be encrypted using the given key. The same
386 * key is used for both sending and reception. However, both directions are
387 * encrypted independently of each other.
388 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100391 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392{
Damien Miller874d77b2000-10-14 16:23:11 +1100393 Cipher *cipher = cipher_by_number(number);
394 if (cipher == NULL)
395 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000396 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100397 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Damien Miller963f6b22002-02-19 15:21:23 +1100398 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
399 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
401
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000402/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000404packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000406 u_char buf[9];
407 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Ben Lindstrom204e4882001-03-05 06:47:00 +0000409 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000410 len = compat20 ? 6 : 9;
411 memset(buf, 0, len - 1);
412 buf[len - 1] = type;
413 buffer_clear(&outgoing_packet);
414 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000415}
416
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000417/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418void
419packet_put_char(int value)
420{
Damien Miller95def091999-11-25 00:26:21 +1100421 char ch = value;
422 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000425packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426{
Damien Miller95def091999-11-25 00:26:21 +1100427 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100430packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller95def091999-11-25 00:26:21 +1100432 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
Damien Miller33b13562000-04-04 14:38:59 +1000434void
435packet_put_cstring(const char *str)
436{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000437 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000438}
Damien Miller33b13562000-04-04 14:38:59 +1000439void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100440packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000441{
442 buffer_append(&outgoing_packet, buf, len);
443}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444void
Damien Miller95def091999-11-25 00:26:21 +1100445packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446{
Damien Miller95def091999-11-25 00:26:21 +1100447 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448}
Damien Miller33b13562000-04-04 14:38:59 +1000449void
450packet_put_bignum2(BIGNUM * value)
451{
452 buffer_put_bignum2(&outgoing_packet, value);
453}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Damien Miller5428f641999-11-25 11:54:57 +1100455/*
456 * Finalizes and sends the packet. If the encryption key has been set,
457 * encrypts the packet before sending.
458 */
Damien Miller95def091999-11-25 00:26:21 +1100459
Ben Lindstrombba81212001-06-25 05:01:22 +0000460static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000461packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000463 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100464 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000465 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100466 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller5428f641999-11-25 11:54:57 +1100468 /*
469 * If using packet compression, compress the payload of the outgoing
470 * packet.
471 */
Damien Miller95def091999-11-25 00:26:21 +1100472 if (packet_compression) {
473 buffer_clear(&compression_buffer);
474 /* Skip padding. */
475 buffer_consume(&outgoing_packet, 8);
476 /* padding */
477 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
478 buffer_compress(&outgoing_packet, &compression_buffer);
479 buffer_clear(&outgoing_packet);
480 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100481 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100482 }
483 /* Compute packet length without padding (add checksum, remove padding). */
484 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485
Damien Millere247cc42000-05-07 12:03:14 +1000486 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100487 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100488 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100489 cp = buffer_ptr(&outgoing_packet);
490 for (i = 0; i < padding; i++) {
491 if (i % 4 == 0)
492 rand = arc4random();
493 cp[7 - i] = rand & 0xff;
494 rand >>= 8;
495 }
496 }
497 buffer_consume(&outgoing_packet, 8 - padding);
498
499 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100500 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000501 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100502 PUT_32BIT(buf, checksum);
503 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
505#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100506 fprintf(stderr, "packet_send plain: ");
507 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508#endif
509
Damien Miller95def091999-11-25 00:26:21 +1100510 /* Append to output. */
511 PUT_32BIT(buf, len);
512 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100513 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100514 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100515 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100516
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100518 fprintf(stderr, "encrypted: ");
519 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520#endif
521
Damien Miller95def091999-11-25 00:26:21 +1100522 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Miller5428f641999-11-25 11:54:57 +1100524 /*
525 * Note that the packet is now only buffered in output. It won\'t be
526 * actually sent until packet_write_wait or packet_write_poll is
527 * called.
528 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529}
530
Ben Lindstromf6027d32002-03-22 01:42:04 +0000531void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000532set_newkeys(int mode)
533{
534 Enc *enc;
535 Mac *mac;
536 Comp *comp;
537 CipherContext *cc;
Damien Miller963f6b22002-02-19 15:21:23 +1100538 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000539
540 debug("newkeys: mode %d", mode);
541
Damien Miller963f6b22002-02-19 15:21:23 +1100542 if (mode == MODE_OUT) {
543 cc = &send_context;
544 encrypt = CIPHER_ENCRYPT;
545 } else {
546 cc = &receive_context;
547 encrypt = CIPHER_DECRYPT;
548 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000549 if (newkeys[mode] != NULL) {
550 debug("newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100551 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000552 enc = &newkeys[mode]->enc;
553 mac = &newkeys[mode]->mac;
554 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000555 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000556 xfree(enc->name);
557 xfree(enc->iv);
558 xfree(enc->key);
559 xfree(mac->name);
560 xfree(mac->key);
561 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000562 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000563 }
564 newkeys[mode] = kex_get_newkeys(mode);
565 if (newkeys[mode] == NULL)
566 fatal("newkeys: no keys for mode %d", mode);
567 enc = &newkeys[mode]->enc;
568 mac = &newkeys[mode]->mac;
569 comp = &newkeys[mode]->comp;
570 if (mac->md != NULL)
571 mac->enabled = 1;
572 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100573 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
574 enc->iv, enc->block_size, encrypt);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000575 /* Deleting the keys does not gain extra security */
576 /* memset(enc->iv, 0, enc->block_size);
577 memset(enc->key, 0, enc->key_len); */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000578 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000579 packet_init_compression();
580 if (mode == MODE_OUT)
581 buffer_compress_init_send(6);
582 else
583 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000584 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000585 }
586}
587
Damien Miller5428f641999-11-25 11:54:57 +1100588/*
Damien Miller33b13562000-04-04 14:38:59 +1000589 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
590 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000591static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000592packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000593{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000594 u_char type, *cp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100595 u_char padlen, pad;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000596 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100597 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000598 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000599 Enc *enc = NULL;
600 Mac *mac = NULL;
601 Comp *comp = NULL;
602 int block_size;
603
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604 if (newkeys[MODE_OUT] != NULL) {
605 enc = &newkeys[MODE_OUT]->enc;
606 mac = &newkeys[MODE_OUT]->mac;
607 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000608 }
Damien Miller963f6b22002-02-19 15:21:23 +1100609 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000610
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000611 cp = buffer_ptr(&outgoing_packet);
612 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000613
614#ifdef PACKET_DEBUG
615 fprintf(stderr, "plain: ");
616 buffer_dump(&outgoing_packet);
617#endif
618
619 if (comp && comp->enabled) {
620 len = buffer_len(&outgoing_packet);
621 /* skip header, compress only payload */
622 buffer_consume(&outgoing_packet, 5);
623 buffer_clear(&compression_buffer);
624 buffer_compress(&outgoing_packet, &compression_buffer);
625 buffer_clear(&outgoing_packet);
626 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
627 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
628 buffer_len(&compression_buffer));
629 DBG(debug("compression: raw %d compressed %d", len,
630 buffer_len(&outgoing_packet)));
631 }
632
633 /* sizeof (packet_len + pad_len + payload) */
634 len = buffer_len(&outgoing_packet);
635
636 /*
637 * calc size of padding, alloc space, get random data,
638 * minimum padding is 4 bytes
639 */
640 padlen = block_size - (len % block_size);
641 if (padlen < 4)
642 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100643 if (extra_pad) {
644 /* will wrap if extra_pad+padlen > 255 */
645 extra_pad = roundup(extra_pad, block_size);
646 pad = extra_pad - ((len + padlen) % extra_pad);
647 debug("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
648 pad, len, padlen, extra_pad);
649 padlen += pad;
650 extra_pad = 0;
651 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100652 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100653 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000654 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000655 for (i = 0; i < padlen; i++) {
656 if (i % 4 == 0)
657 rand = arc4random();
658 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000659 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000660 }
Damien Millere247cc42000-05-07 12:03:14 +1000661 } else {
662 /* clear padding */
663 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000664 }
665 /* packet_length includes payload, padding and padding length field */
666 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000667 cp = buffer_ptr(&outgoing_packet);
668 PUT_32BIT(cp, packet_length);
669 cp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000670 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
671
672 /* compute MAC over seqnr and packet(length fields, payload, padding) */
673 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000674 macbuf = mac_compute(mac, send_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100675 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000676 buffer_len(&outgoing_packet));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000677 DBG(debug("done calc MAC out #%d", send_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000678 }
679 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100680 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100681 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000682 buffer_len(&outgoing_packet));
683 /* append unencrypted MAC */
684 if (mac && mac->enabled)
685 buffer_append(&output, (char *)macbuf, mac->mac_len);
686#ifdef PACKET_DEBUG
687 fprintf(stderr, "encrypted: ");
688 buffer_dump(&output);
689#endif
Damien Miller4af51302000-04-16 11:18:38 +1000690 /* increment sequence number for outgoing packets */
Ben Lindstromf6027d32002-03-22 01:42:04 +0000691 if (++send_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000692 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000693 buffer_clear(&outgoing_packet);
694
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000695 if (type == SSH2_MSG_NEWKEYS)
696 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000697}
698
699void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000700packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000701{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000702 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000703 packet_send2();
704 else
705 packet_send1();
706 DBG(debug("packet_send done"));
707}
708
Damien Miller33b13562000-04-04 14:38:59 +1000709/*
Damien Miller5428f641999-11-25 11:54:57 +1100710 * Waits until a packet has been received, and returns its type. Note that
711 * no other data is processed until this returns, so this function should not
712 * be used during the interactive session.
713 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714
715int
Damien Millerdff50992002-01-22 23:16:32 +1100716packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717{
Damien Miller95def091999-11-25 00:26:21 +1100718 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000719 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100720 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000721 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000723 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
724 sizeof(fd_mask));
725
Damien Miller95def091999-11-25 00:26:21 +1100726 /* Since we are blocking, ensure that all written packets have been sent. */
727 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Damien Miller95def091999-11-25 00:26:21 +1100729 /* Stay in the loop until we have received a complete packet. */
730 for (;;) {
731 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100732 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000733 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000734 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100735 || type == SSH_SMSG_FAILURE
736 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000737 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100738 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100739 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000740 if (type != SSH_MSG_NONE) {
741 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100742 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000743 }
Damien Miller5428f641999-11-25 11:54:57 +1100744 /*
745 * Otherwise, wait for some data to arrive, add it to the
746 * buffer, and try again.
747 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000748 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
749 sizeof(fd_mask));
750 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100751
Damien Miller95def091999-11-25 00:26:21 +1100752 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000753 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000754 (errno == EAGAIN || errno == EINTR))
755 ;
Damien Miller5428f641999-11-25 11:54:57 +1100756
Damien Miller95def091999-11-25 00:26:21 +1100757 /* Read data from the socket. */
758 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100759 if (len == 0) {
760 log("Connection closed by %.200s", get_remote_ipaddr());
761 fatal_cleanup();
762 }
Damien Miller95def091999-11-25 00:26:21 +1100763 if (len < 0)
764 fatal("Read from socket failed: %.100s", strerror(errno));
765 /* Append it to the buffer. */
766 packet_process_incoming(buf, len);
767 }
768 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769}
770
Damien Miller278f9072001-12-21 15:00:19 +1100771int
Damien Millerdff50992002-01-22 23:16:32 +1100772packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100773{
Damien Millerdff50992002-01-22 23:16:32 +1100774 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100775}
776
Damien Miller5428f641999-11-25 11:54:57 +1100777/*
778 * Waits until a packet has been received, verifies that its type matches
779 * that given, and gives a fatal error and exits if there is a mismatch.
780 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781
782void
Damien Millerdff50992002-01-22 23:16:32 +1100783packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784{
Damien Miller95def091999-11-25 00:26:21 +1100785 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
Damien Millerdff50992002-01-22 23:16:32 +1100787 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100788 if (type != expected_type)
789 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000790 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791}
792
793/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100794 * packet_process_incoming. If so, reads the packet; otherwise returns
795 * SSH_MSG_NONE. This does not wait for data from the connection.
796 *
797 * SSH_MSG_DISCONNECT is handled specially here. Also,
798 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
799 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100800 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Ben Lindstrombba81212001-06-25 05:01:22 +0000802static int
Damien Millerdff50992002-01-22 23:16:32 +1100803packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000805 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000806 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000807 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Check if input size is less than minimum packet size. */
810 if (buffer_len(&input) < 4 + 8)
811 return SSH_MSG_NONE;
812 /* Get length of incoming packet. */
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000813 cp = buffer_ptr(&input);
814 len = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100815 if (len < 1 + 2 + 2 || len > 256 * 1024)
816 packet_disconnect("Bad packet length %d.", len);
817 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller95def091999-11-25 00:26:21 +1100819 /* Check if the packet has been entirely received. */
820 if (buffer_len(&input) < 4 + padded_len)
821 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller95def091999-11-25 00:26:21 +1100823 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824
Damien Miller95def091999-11-25 00:26:21 +1100825 /* Consume packet length. */
826 buffer_consume(&input, 4);
827
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000828 /*
829 * Cryptographic attack detector for ssh
830 * (C)1998 CORE-SDI, Buenos Aires Argentina
831 * Ariel Futoransky(futo@core-sdi.com)
832 */
Damien Miller963f6b22002-02-19 15:21:23 +1100833 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000834 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
835 packet_disconnect("crc32 compensation attack: network attack detected");
836
837 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100838 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100839 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100840 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000841
Damien Miller95def091999-11-25 00:26:21 +1100842 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843
844#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100845 fprintf(stderr, "read_poll plain: ");
846 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller95def091999-11-25 00:26:21 +1100849 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100850 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000851 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852
Damien Miller95def091999-11-25 00:26:21 +1100853 /* Skip padding. */
854 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100855
Damien Miller95def091999-11-25 00:26:21 +1100856 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100857 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100858 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000859 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000861 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
862 stored_checksum = GET_32BIT(cp);
Damien Miller95def091999-11-25 00:26:21 +1100863 if (checksum != stored_checksum)
864 packet_disconnect("Corrupted check bytes on input.");
865 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Damien Miller95def091999-11-25 00:26:21 +1100867 if (packet_compression) {
868 buffer_clear(&compression_buffer);
869 buffer_uncompress(&incoming_packet, &compression_buffer);
870 buffer_clear(&incoming_packet);
871 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000872 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100873 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000874 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000875 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876}
Damien Miller95def091999-11-25 00:26:21 +1100877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static int
Damien Millerdff50992002-01-22 23:16:32 +1100879packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000880{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000881 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000882 u_int padlen, need;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000883 u_char *macbuf, *cp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000884 int maclen, block_size;
885 Enc *enc = NULL;
886 Mac *mac = NULL;
887 Comp *comp = NULL;
888
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000889 if (newkeys[MODE_IN] != NULL) {
890 enc = &newkeys[MODE_IN]->enc;
891 mac = &newkeys[MODE_IN]->mac;
892 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000893 }
894 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +1100895 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000896
897 if (packet_length == 0) {
898 /*
899 * check if input size is less than the cipher block size,
900 * decrypt first block and extract length of incoming packet
901 */
902 if (buffer_len(&input) < block_size)
903 return SSH_MSG_NONE;
904 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100905 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +1100906 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000907 block_size);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000908 cp = buffer_ptr(&incoming_packet);
909 packet_length = GET_32BIT(cp);
Damien Miller33b13562000-04-04 14:38:59 +1000910 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
911 buffer_dump(&incoming_packet);
912 packet_disconnect("Bad packet length %d.", packet_length);
913 }
914 DBG(debug("input: packet len %d", packet_length+4));
915 buffer_consume(&input, block_size);
916 }
917 /* we have a partial packet of block_size bytes */
918 need = 4 + packet_length - block_size;
919 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
920 need, maclen));
921 if (need % block_size != 0)
922 fatal("padding error: need %d block %d mod %d",
923 need, block_size, need % block_size);
924 /*
925 * check if the entire packet has been received and
926 * decrypt into incoming_packet
927 */
928 if (buffer_len(&input) < need + maclen)
929 return SSH_MSG_NONE;
930#ifdef PACKET_DEBUG
931 fprintf(stderr, "read_poll enc/full: ");
932 buffer_dump(&input);
933#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100934 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +1100935 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000936 buffer_consume(&input, need);
937 /*
938 * compute MAC over seqnr and packet,
939 * increment sequence number for incoming packet
940 */
Damien Miller4af51302000-04-16 11:18:38 +1000941 if (mac && mac->enabled) {
Ben Lindstromf6027d32002-03-22 01:42:04 +0000942 macbuf = mac_compute(mac, read_seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100943 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000944 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000945 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100946 packet_disconnect("Corrupted MAC on input.");
Ben Lindstromf6027d32002-03-22 01:42:04 +0000947 DBG(debug("MAC #%d ok", read_seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000948 buffer_consume(&input, mac->mac_len);
949 }
Damien Miller278f9072001-12-21 15:00:19 +1100950 if (seqnr_p != NULL)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000951 *seqnr_p = read_seqnr;
952 if (++read_seqnr == 0)
Damien Miller4af51302000-04-16 11:18:38 +1000953 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000954
955 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100956 cp = buffer_ptr(&incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000957 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +1000958 DBG(debug("input: padlen %d", padlen));
959 if (padlen < 4)
960 packet_disconnect("Corrupted padlen %d on input.", padlen);
961
962 /* skip packet size + padlen, discard padding */
963 buffer_consume(&incoming_packet, 4 + 1);
964 buffer_consume_end(&incoming_packet, padlen);
965
966 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
967 if (comp && comp->enabled) {
968 buffer_clear(&compression_buffer);
969 buffer_uncompress(&incoming_packet, &compression_buffer);
970 buffer_clear(&incoming_packet);
971 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
972 buffer_len(&compression_buffer));
973 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
974 }
975 /*
976 * get packet type, implies consume.
977 * return length of payload (without type field)
978 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000979 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000980 if (type == SSH2_MSG_NEWKEYS)
981 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +1000982#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000983 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000984 buffer_dump(&incoming_packet);
985#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000986 /* reset for next packet */
987 packet_length = 0;
988 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000989}
990
991int
Damien Millerdff50992002-01-22 23:16:32 +1100992packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000993{
Damien Miller659811f2002-01-22 23:23:11 +1100994 int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000995 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +1000996 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +1000997
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000998 for (;;) {
999 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001000 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001001 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001002 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001003 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001004 case SSH2_MSG_IGNORE:
1005 break;
1006 case SSH2_MSG_DEBUG:
1007 packet_get_char();
1008 msg = packet_get_string(NULL);
1009 debug("Remote: %.900s", msg);
1010 xfree(msg);
1011 msg = packet_get_string(NULL);
1012 xfree(msg);
1013 break;
1014 case SSH2_MSG_DISCONNECT:
1015 reason = packet_get_int();
1016 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001017 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
1018 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +10001019 xfree(msg);
1020 fatal_cleanup();
1021 break;
Damien Miller659811f2002-01-22 23:23:11 +11001022 case SSH2_MSG_UNIMPLEMENTED:
1023 seqnr = packet_get_int();
1024 debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
1025 break;
Damien Miller33b13562000-04-04 14:38:59 +10001026 default:
1027 return type;
1028 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001029 }
Damien Miller33b13562000-04-04 14:38:59 +10001030 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001031 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001032 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +10001033 case SSH_MSG_IGNORE:
1034 break;
1035 case SSH_MSG_DEBUG:
1036 msg = packet_get_string(NULL);
1037 debug("Remote: %.900s", msg);
1038 xfree(msg);
1039 break;
1040 case SSH_MSG_DISCONNECT:
1041 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001042 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1043 msg);
Damien Miller33b13562000-04-04 14:38:59 +10001044 fatal_cleanup();
1045 xfree(msg);
1046 break;
1047 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001048 if (type)
Damien Miller33b13562000-04-04 14:38:59 +10001049 DBG(debug("received packet type %d", type));
1050 return type;
1051 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001052 }
Damien Miller33b13562000-04-04 14:38:59 +10001053 }
1054 }
1055}
1056
Damien Miller278f9072001-12-21 15:00:19 +11001057int
Damien Millerdff50992002-01-22 23:16:32 +11001058packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +11001059{
Damien Millerdff50992002-01-22 23:16:32 +11001060 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001061}
1062
Damien Miller5428f641999-11-25 11:54:57 +11001063/*
1064 * Buffers the given amount of input characters. This is intended to be used
1065 * together with packet_read_poll.
1066 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
1068void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001069packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070{
Damien Miller95def091999-11-25 00:26:21 +11001071 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072}
1073
1074/* Returns a character from the packet. */
1075
Ben Lindstrom46c16222000-12-22 01:43:59 +00001076u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001077packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078{
Damien Miller95def091999-11-25 00:26:21 +11001079 char ch;
1080 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001081 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082}
1083
1084/* Returns an integer from the packet data. */
1085
Ben Lindstrom46c16222000-12-22 01:43:59 +00001086u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001087packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088{
Damien Miller95def091999-11-25 00:26:21 +11001089 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090}
1091
Damien Miller5428f641999-11-25 11:54:57 +11001092/*
1093 * Returns an arbitrary precision integer from the packet data. The integer
1094 * must have been initialized before this call.
1095 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096
1097void
Damien Millerd432ccf2002-01-22 23:14:44 +11001098packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099{
Damien Miller76e1e362002-01-22 23:15:57 +11001100 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101}
1102
Damien Miller33b13562000-04-04 14:38:59 +10001103void
Damien Millerd432ccf2002-01-22 23:14:44 +11001104packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001105{
Damien Miller76e1e362002-01-22 23:15:57 +11001106 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001107}
1108
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001109void *
Damien Miller33b13562000-04-04 14:38:59 +10001110packet_get_raw(int *length_ptr)
1111{
1112 int bytes = buffer_len(&incoming_packet);
1113 if (length_ptr != NULL)
1114 *length_ptr = bytes;
1115 return buffer_ptr(&incoming_packet);
1116}
1117
Damien Miller4af51302000-04-16 11:18:38 +10001118int
1119packet_remaining(void)
1120{
1121 return buffer_len(&incoming_packet);
1122}
1123
Damien Miller5428f641999-11-25 11:54:57 +11001124/*
1125 * Returns a string from the packet data. The string is allocated using
1126 * xmalloc; it is the responsibility of the calling program to free it when
1127 * no longer needed. The length_ptr argument may be NULL, or point to an
1128 * integer into which the length of the string is stored.
1129 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001131void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001132packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133{
Damien Miller95def091999-11-25 00:26:21 +11001134 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135}
1136
Damien Miller5428f641999-11-25 11:54:57 +11001137/*
1138 * Sends a diagnostic message from the server to the client. This message
1139 * can be sent at any time (but not while constructing another message). The
1140 * message is printed immediately, but only if the client is being executed
1141 * in verbose mode. These messages are primarily intended to ease debugging
1142 * authentication problems. The length of the formatted message must not
1143 * exceed 1024 bytes. This will automatically call packet_write_wait.
1144 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
1146void
Damien Miller95def091999-11-25 00:26:21 +11001147packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148{
Damien Miller95def091999-11-25 00:26:21 +11001149 char buf[1024];
1150 va_list args;
1151
Ben Lindstroma14ee472000-12-07 01:24:58 +00001152 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1153 return;
1154
Damien Miller95def091999-11-25 00:26:21 +11001155 va_start(args, fmt);
1156 vsnprintf(buf, sizeof(buf), fmt, args);
1157 va_end(args);
1158
Damien Miller7c8af4f2000-05-01 08:24:07 +10001159 if (compat20) {
1160 packet_start(SSH2_MSG_DEBUG);
1161 packet_put_char(0); /* bool: always display */
1162 packet_put_cstring(buf);
1163 packet_put_cstring("");
1164 } else {
1165 packet_start(SSH_MSG_DEBUG);
1166 packet_put_cstring(buf);
1167 }
Damien Miller95def091999-11-25 00:26:21 +11001168 packet_send();
1169 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170}
1171
Damien Miller5428f641999-11-25 11:54:57 +11001172/*
1173 * Logs the error plus constructs and sends a disconnect packet, closes the
1174 * connection, and exits. This function never returns. The error message
1175 * should not contain a newline. The length of the formatted message must
1176 * not exceed 1024 bytes.
1177 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
1179void
Damien Miller95def091999-11-25 00:26:21 +11001180packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181{
Damien Miller95def091999-11-25 00:26:21 +11001182 char buf[1024];
1183 va_list args;
1184 static int disconnecting = 0;
1185 if (disconnecting) /* Guard against recursive invocations. */
1186 fatal("packet_disconnect called recursively.");
1187 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
Damien Miller5428f641999-11-25 11:54:57 +11001189 /*
1190 * Format the message. Note that the caller must make sure the
1191 * message is of limited size.
1192 */
Damien Miller95def091999-11-25 00:26:21 +11001193 va_start(args, fmt);
1194 vsnprintf(buf, sizeof(buf), fmt, args);
1195 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196
Damien Miller95def091999-11-25 00:26:21 +11001197 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001198 if (compat20) {
1199 packet_start(SSH2_MSG_DISCONNECT);
1200 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1201 packet_put_cstring(buf);
1202 packet_put_cstring("");
1203 } else {
1204 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001205 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001206 }
Damien Miller95def091999-11-25 00:26:21 +11001207 packet_send();
1208 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
Damien Miller95def091999-11-25 00:26:21 +11001210 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001211 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212
Damien Miller95def091999-11-25 00:26:21 +11001213 /* Close the connection. */
1214 packet_close();
1215
1216 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001217 log("Disconnecting: %.100s", buf);
1218 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219}
1220
Damien Miller5428f641999-11-25 11:54:57 +11001221/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222
1223void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001224packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225{
Damien Miller95def091999-11-25 00:26:21 +11001226 int len = buffer_len(&output);
1227 if (len > 0) {
1228 len = write(connection_out, buffer_ptr(&output), len);
1229 if (len <= 0) {
1230 if (errno == EAGAIN)
1231 return;
1232 else
1233 fatal("Write failed: %.100s", strerror(errno));
1234 }
1235 buffer_consume(&output, len);
1236 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237}
1238
Damien Miller5428f641999-11-25 11:54:57 +11001239/*
1240 * Calls packet_write_poll repeatedly until all pending output data has been
1241 * written.
1242 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
1244void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001245packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001247 fd_set *setp;
1248
1249 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1250 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001251 packet_write_poll();
1252 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001253 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1254 sizeof(fd_mask));
1255 FD_SET(connection_out, setp);
1256 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001257 (errno == EAGAIN || errno == EINTR))
1258 ;
Damien Miller95def091999-11-25 00:26:21 +11001259 packet_write_poll();
1260 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001261 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262}
1263
1264/* Returns true if there is buffered data to write to the connection. */
1265
1266int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001267packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268{
Damien Miller95def091999-11-25 00:26:21 +11001269 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270}
1271
1272/* Returns true if there is not too much data to write to the connection. */
1273
1274int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001275packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276{
Damien Miller95def091999-11-25 00:26:21 +11001277 if (interactive_mode)
1278 return buffer_len(&output) < 16384;
1279 else
1280 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281}
1282
1283/* Informs that the current session is interactive. Sets IP flags for that. */
1284
1285void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001286packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001288 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001289#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001290 int lowdelay = IPTOS_LOWDELAY;
1291 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001292#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001294 if (called)
1295 return;
1296 called = 1;
1297
Damien Miller95def091999-11-25 00:26:21 +11001298 /* Record that we are in interactive mode. */
1299 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
Damien Miller34132e52000-01-14 15:45:46 +11001301 /* Only set socket options if using a socket. */
1302 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001303 return;
Damien Miller34132e52000-01-14 15:45:46 +11001304 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001305 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001306 */
Damien Miller95def091999-11-25 00:26:21 +11001307 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001308 /*
1309 * Set IP options for an interactive connection. Use
1310 * IPTOS_LOWDELAY and TCP_NODELAY.
1311 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001312#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001313 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001314 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom733a2352002-03-05 01:31:28 +00001315 &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001316 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001317 strerror(errno));
1318 }
Damien Miller615f9392000-05-17 22:53:33 +10001319#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001320 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001321 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001322 /*
1323 * Set IP options for a non-interactive connection. Use
1324 * IPTOS_THROUGHPUT.
1325 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001326#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom733a2352002-03-05 01:31:28 +00001327 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001328 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001329 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001330#endif
Damien Miller95def091999-11-25 00:26:21 +11001331 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332}
1333
1334/* Returns true if the current connection is interactive. */
1335
1336int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001337packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338{
Damien Miller95def091999-11-25 00:26:21 +11001339 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
Damien Miller6162d121999-11-21 13:23:52 +11001341
1342int
1343packet_set_maxsize(int s)
1344{
Damien Miller95def091999-11-25 00:26:21 +11001345 static int called = 0;
1346 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001347 log("packet_set_maxsize: called twice: old %d new %d",
1348 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001349 return -1;
1350 }
1351 if (s < 4 * 1024 || s > 1024 * 1024) {
1352 log("packet_set_maxsize: bad size %d", s);
1353 return -1;
1354 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001355 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001356 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001357 max_packet_size = s;
1358 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001359}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001360
Damien Miller9f643902001-11-12 11:02:52 +11001361/* roundup current message to pad bytes */
1362void
1363packet_add_padding(u_char pad)
1364{
1365 extra_pad = pad;
1366}
1367
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001368/*
1369 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001370 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001371 * byte SSH_MSG_IGNORE
1372 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001373 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001374 * All implementations MUST understand (and ignore) this message at any
1375 * time (after receiving the protocol version). No implementation is
1376 * required to send them. This message can be used as an additional
1377 * protection measure against advanced traffic analysis techniques.
1378 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001379void
1380packet_send_ignore(int nbytes)
1381{
1382 u_int32_t rand = 0;
1383 int i;
1384
1385 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001386 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001387 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001388 if (i % 4 == 0)
1389 rand = arc4random();
1390 packet_put_char(rand & 0xff);
1391 rand >>= 8;
1392 }
1393}