blob: 17f6f6e3df4346405c8ed9b2a0c4aa42cdcfa77e [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * packet.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Sat Mar 18 02:40:40 1995 ylo
11 *
12 * This file contains code implementing the packet protocol and communication
13 * with the other side. This same code is used both on client and server side.
14 *
15 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller5e7c10e1999-12-16 13:18:04 +110018RCSID("$Id: packet.c,v 1.8 1999/12/16 02:18:04 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "xmalloc.h"
21#include "buffer.h"
22#include "packet.h"
23#include "bufaux.h"
24#include "ssh.h"
25#include "crc32.h"
26#include "cipher.h"
27#include "getput.h"
28
29#include "compress.h"
30#include "deattack.h"
31
Damien Miller5428f641999-11-25 11:54:57 +110032/*
33 * This variable contains the file descriptors used for communicating with
34 * the other side. connection_in is used for reading; connection_out for
35 * writing. These can be the same descriptor, in which case it is assumed to
36 * be a socket.
37 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038static int connection_in = -1;
39static int connection_out = -1;
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
42 * Cipher type. This value is only used to determine whether to pad the
43 * packets with zeroes or random data.
44 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045static int cipher_type = SSH_CIPHER_NONE;
46
47/* Protocol flags for the remote side. */
48static unsigned int remote_protocol_flags = 0;
49
50/* Encryption context for receiving data. This is only used for decryption. */
51static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110052
53/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054static CipherContext send_context;
55
56/* Buffer for raw input data from the socket. */
57static Buffer input;
58
59/* Buffer for raw output data going to the socket. */
60static Buffer output;
61
62/* Buffer for the partial outgoing packet being constructed. */
63static Buffer outgoing_packet;
64
65/* Buffer for the incoming packet currently being processed. */
66static Buffer incoming_packet;
67
68/* Scratch buffer for packet compression/decompression. */
69static Buffer compression_buffer;
70
71/* Flag indicating whether packet compression/decompression is enabled. */
72static int packet_compression = 0;
73
Damien Miller6162d121999-11-21 13:23:52 +110074/* default maximum packet size */
75int max_packet_size = 32768;
76
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077/* Flag indicating whether this module has been initialized. */
78static int initialized = 0;
79
80/* Set to true if the connection is interactive. */
81static int interactive_mode = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Sets the descriptors used for communication. Disables encryption until
85 * packet_set_encryption_key is called.
86 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
88void
89packet_set_connection(int fd_in, int fd_out)
90{
Damien Miller95def091999-11-25 00:26:21 +110091 connection_in = fd_in;
92 connection_out = fd_out;
93 cipher_type = SSH_CIPHER_NONE;
94 cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 1);
95 cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 0);
96 if (!initialized) {
97 initialized = 1;
98 buffer_init(&input);
99 buffer_init(&output);
100 buffer_init(&outgoing_packet);
101 buffer_init(&incoming_packet);
102 }
103 /* Kludge: arrange the close function to be called from fatal(). */
104 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105}
106
107/* Sets the connection into non-blocking mode. */
108
109void
110packet_set_nonblocking()
111{
Damien Miller95def091999-11-25 00:26:21 +1100112 /* Set the socket into non-blocking mode. */
113 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
114 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 if (connection_out != connection_in) {
117 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
118 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
119 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120}
121
122/* Returns the socket used for reading. */
123
124int
125packet_get_connection_in()
126{
Damien Miller95def091999-11-25 00:26:21 +1100127 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128}
129
130/* Returns the descriptor used for writing. */
131
132int
133packet_get_connection_out()
134{
Damien Miller95def091999-11-25 00:26:21 +1100135 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136}
137
138/* Closes the connection and clears and frees internal data structures. */
139
140void
141packet_close()
142{
Damien Miller95def091999-11-25 00:26:21 +1100143 if (!initialized)
144 return;
145 initialized = 0;
146 if (connection_in == connection_out) {
147 shutdown(connection_out, SHUT_RDWR);
148 close(connection_out);
149 } else {
150 close(connection_in);
151 close(connection_out);
152 }
153 buffer_free(&input);
154 buffer_free(&output);
155 buffer_free(&outgoing_packet);
156 buffer_free(&incoming_packet);
157 if (packet_compression) {
158 buffer_free(&compression_buffer);
159 buffer_compress_uninit();
160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161}
162
163/* Sets remote side protocol flags. */
164
165void
166packet_set_protocol_flags(unsigned int protocol_flags)
167{
Damien Miller95def091999-11-25 00:26:21 +1100168 remote_protocol_flags = protocol_flags;
169 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170}
171
172/* Returns the remote protocol flags set earlier by the above function. */
173
174unsigned int
175packet_get_protocol_flags()
176{
Damien Miller95def091999-11-25 00:26:21 +1100177 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
181 * Starts packet compression from the next packet on in both directions.
182 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
183 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
185void
186packet_start_compression(int level)
187{
Damien Miller95def091999-11-25 00:26:21 +1100188 if (packet_compression)
189 fatal("Compression already enabled.");
190 packet_compression = 1;
191 buffer_init(&compression_buffer);
192 buffer_compress_init(level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193}
194
Damien Miller5428f641999-11-25 11:54:57 +1100195/*
196 * Encrypts the given number of bytes, copying from src to dest. bytes is
197 * known to be a multiple of 8.
198 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
200void
Damien Miller95def091999-11-25 00:26:21 +1100201packet_encrypt(CipherContext * cc, void *dest, void *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 unsigned int bytes)
203{
Damien Miller95def091999-11-25 00:26:21 +1100204 cipher_encrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205}
206
Damien Miller5428f641999-11-25 11:54:57 +1100207/*
208 * Decrypts the given number of bytes, copying from src to dest. bytes is
209 * known to be a multiple of 8.
210 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
212void
Damien Miller95def091999-11-25 00:26:21 +1100213packet_decrypt(CipherContext * cc, void *dest, void *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214 unsigned int bytes)
215{
Damien Miller95def091999-11-25 00:26:21 +1100216 int i;
217
218 if ((bytes % 8) != 0)
219 fatal("packet_decrypt: bad ciphertext length %d", bytes);
220
Damien Miller5428f641999-11-25 11:54:57 +1100221 /*
222 * Cryptographic attack detector for ssh - Modifications for packet.c
223 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
224 */
Damien Miller95def091999-11-25 00:26:21 +1100225
226 switch (cc->type) {
227 case SSH_CIPHER_NONE:
228 i = DEATTACK_OK;
229 break;
230 default:
231 i = detect_attack(src, bytes, NULL);
232 break;
233 }
234
235 if (i == DEATTACK_DETECTED)
236 packet_disconnect("crc32 compensation attack: network attack detected");
237
238 cipher_decrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Causes any further packets to be encrypted using the given key. The same
243 * key is used for both sending and reception. However, both directions are
244 * encrypted independently of each other.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
247void
248packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
Damien Miller7e8e8201999-11-16 13:37:16 +1100249 int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 /* All other ciphers use the same key in both directions for now. */
252 cipher_set_key(&receive_context, cipher, key, keylen, 0);
253 cipher_set_key(&send_context, cipher, key, keylen, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
256/* Starts constructing a packet to send. */
257
258void
259packet_start(int type)
260{
Damien Miller95def091999-11-25 00:26:21 +1100261 char buf[9];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Damien Miller95def091999-11-25 00:26:21 +1100263 buffer_clear(&outgoing_packet);
264 memset(buf, 0, 8);
265 buf[8] = type;
266 buffer_append(&outgoing_packet, buf, 9);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267}
268
269/* Appends a character to the packet data. */
270
271void
272packet_put_char(int value)
273{
Damien Miller95def091999-11-25 00:26:21 +1100274 char ch = value;
275 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
277
278/* Appends an integer to the packet data. */
279
280void
281packet_put_int(unsigned int value)
282{
Damien Miller95def091999-11-25 00:26:21 +1100283 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284}
285
286/* Appends a string to packet data. */
287
288void
289packet_put_string(const char *buf, unsigned int len)
290{
Damien Miller95def091999-11-25 00:26:21 +1100291 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292}
293
294/* Appends an arbitrary precision integer to packet data. */
295
296void
Damien Miller95def091999-11-25 00:26:21 +1100297packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298{
Damien Miller95def091999-11-25 00:26:21 +1100299 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Damien Miller5428f641999-11-25 11:54:57 +1100302/*
303 * Finalizes and sends the packet. If the encryption key has been set,
304 * encrypts the packet before sending.
305 */
Damien Miller95def091999-11-25 00:26:21 +1100306
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307void
308packet_send()
309{
Damien Miller95def091999-11-25 00:26:21 +1100310 char buf[8], *cp;
311 int i, padding, len;
312 unsigned int checksum;
313 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller5428f641999-11-25 11:54:57 +1100315 /*
316 * If using packet compression, compress the payload of the outgoing
317 * packet.
318 */
Damien Miller95def091999-11-25 00:26:21 +1100319 if (packet_compression) {
320 buffer_clear(&compression_buffer);
321 /* Skip padding. */
322 buffer_consume(&outgoing_packet, 8);
323 /* padding */
324 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
325 buffer_compress(&outgoing_packet, &compression_buffer);
326 buffer_clear(&outgoing_packet);
327 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
328 buffer_len(&compression_buffer));
329 }
330 /* Compute packet length without padding (add checksum, remove padding). */
331 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 /* Insert padding. */
334 padding = 8 - len % 8;
335 if (cipher_type != SSH_CIPHER_NONE) {
336 cp = buffer_ptr(&outgoing_packet);
337 for (i = 0; i < padding; i++) {
338 if (i % 4 == 0)
339 rand = arc4random();
340 cp[7 - i] = rand & 0xff;
341 rand >>= 8;
342 }
343 }
344 buffer_consume(&outgoing_packet, 8 - padding);
345
346 /* Add check bytes. */
347 checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet),
348 buffer_len(&outgoing_packet));
349 PUT_32BIT(buf, checksum);
350 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
352#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100353 fprintf(stderr, "packet_send plain: ");
354 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355#endif
356
Damien Miller95def091999-11-25 00:26:21 +1100357 /* Append to output. */
358 PUT_32BIT(buf, len);
359 buffer_append(&output, buf, 4);
360 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
361 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
362 buffer_len(&outgoing_packet));
363
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100365 fprintf(stderr, "encrypted: ");
366 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367#endif
368
Damien Miller95def091999-11-25 00:26:21 +1100369 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Damien Miller5428f641999-11-25 11:54:57 +1100371 /*
372 * Note that the packet is now only buffered in output. It won\'t be
373 * actually sent until packet_write_wait or packet_write_poll is
374 * called.
375 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376}
377
Damien Miller5428f641999-11-25 11:54:57 +1100378/*
379 * Waits until a packet has been received, and returns its type. Note that
380 * no other data is processed until this returns, so this function should not
381 * be used during the interactive session.
382 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
384int
385packet_read(int *payload_len_ptr)
386{
Damien Miller95def091999-11-25 00:26:21 +1100387 int type, len;
388 fd_set set;
389 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390
Damien Miller95def091999-11-25 00:26:21 +1100391 /* Since we are blocking, ensure that all written packets have been sent. */
392 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
Damien Miller95def091999-11-25 00:26:21 +1100394 /* Stay in the loop until we have received a complete packet. */
395 for (;;) {
396 /* Try to read a packet from the buffer. */
397 type = packet_read_poll(payload_len_ptr);
398 if (type == SSH_SMSG_SUCCESS
399 || type == SSH_SMSG_FAILURE
400 || type == SSH_CMSG_EOF
401 || type == SSH_CMSG_EXIT_CONFIRMATION)
402 packet_integrity_check(*payload_len_ptr, 0, type);
403 /* If we got a packet, return it. */
404 if (type != SSH_MSG_NONE)
405 return type;
Damien Miller5428f641999-11-25 11:54:57 +1100406 /*
407 * Otherwise, wait for some data to arrive, add it to the
408 * buffer, and try again.
409 */
Damien Miller95def091999-11-25 00:26:21 +1100410 FD_ZERO(&set);
411 FD_SET(connection_in, &set);
Damien Miller5428f641999-11-25 11:54:57 +1100412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* Wait for some data to arrive. */
414 select(connection_in + 1, &set, NULL, NULL, NULL);
Damien Miller5428f641999-11-25 11:54:57 +1100415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Read data from the socket. */
417 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100418 if (len == 0) {
419 log("Connection closed by %.200s", get_remote_ipaddr());
420 fatal_cleanup();
421 }
Damien Miller95def091999-11-25 00:26:21 +1100422 if (len < 0)
423 fatal("Read from socket failed: %.100s", strerror(errno));
424 /* Append it to the buffer. */
425 packet_process_incoming(buf, len);
426 }
427 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
429
Damien Miller5428f641999-11-25 11:54:57 +1100430/*
431 * Waits until a packet has been received, verifies that its type matches
432 * that given, and gives a fatal error and exits if there is a mismatch.
433 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
435void
436packet_read_expect(int *payload_len_ptr, int expected_type)
437{
Damien Miller95def091999-11-25 00:26:21 +1100438 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 type = packet_read(payload_len_ptr);
441 if (type != expected_type)
442 packet_disconnect("Protocol error: expected packet type %d, got %d",
443 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444}
445
446/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100447 * packet_process_incoming. If so, reads the packet; otherwise returns
448 * SSH_MSG_NONE. This does not wait for data from the connection.
449 *
450 * SSH_MSG_DISCONNECT is handled specially here. Also,
451 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
452 * to higher levels.
453 *
454 * The returned payload_len does include space consumed by:
455 * Packet length
456 * Padding
457 * Packet type
458 * Check bytes
459 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
461int
462packet_read_poll(int *payload_len_ptr)
463{
Damien Miller95def091999-11-25 00:26:21 +1100464 unsigned int len, padded_len;
465 unsigned char *ucp;
466 char buf[8], *cp;
467 unsigned int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469restart:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 /* Check if input size is less than minimum packet size. */
472 if (buffer_len(&input) < 4 + 8)
473 return SSH_MSG_NONE;
474 /* Get length of incoming packet. */
475 ucp = (unsigned char *) buffer_ptr(&input);
476 len = GET_32BIT(ucp);
477 if (len < 1 + 2 + 2 || len > 256 * 1024)
478 packet_disconnect("Bad packet length %d.", len);
479 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller95def091999-11-25 00:26:21 +1100481 /* Check if the packet has been entirely received. */
482 if (buffer_len(&input) < 4 + padded_len)
483 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Damien Miller95def091999-11-25 00:26:21 +1100485 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 /* Consume packet length. */
488 buffer_consume(&input, 4);
489
490 /* Copy data to incoming_packet. */
491 buffer_clear(&incoming_packet);
492 buffer_append_space(&incoming_packet, &cp, padded_len);
493 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
494 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495
496#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100497 fprintf(stderr, "read_poll plain: ");
498 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Compute packet checksum. */
502 checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet),
503 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Damien Miller95def091999-11-25 00:26:21 +1100505 /* Skip padding. */
506 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100507
Damien Miller95def091999-11-25 00:26:21 +1100508 /* Test check bytes. */
Damien Millerfd7c9111999-11-08 16:15:55 +1100509
Damien Miller95def091999-11-25 00:26:21 +1100510 if (len != buffer_len(&incoming_packet))
511 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
512 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller95def091999-11-25 00:26:21 +1100514 ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4;
515 stored_checksum = GET_32BIT(ucp);
516 if (checksum != stored_checksum)
517 packet_disconnect("Corrupted check bytes on input.");
518 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller95def091999-11-25 00:26:21 +1100520 /* If using packet compression, decompress the packet. */
521 if (packet_compression) {
522 buffer_clear(&compression_buffer);
523 buffer_uncompress(&incoming_packet, &compression_buffer);
524 buffer_clear(&incoming_packet);
525 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
526 buffer_len(&compression_buffer));
527 }
528 /* Get packet type. */
529 buffer_get(&incoming_packet, &buf[0], 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Miller95def091999-11-25 00:26:21 +1100531 /* Return length of payload (without type field). */
532 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Handle disconnect message. */
Damien Milleraae6c611999-12-06 11:47:28 +1100535 if ((unsigned char) buf[0] == SSH_MSG_DISCONNECT) {
536 log("Received disconnect: %.900s", packet_get_string(NULL));
537 fatal_cleanup();
538 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller95def091999-11-25 00:26:21 +1100540 /* Ignore ignore messages. */
541 if ((unsigned char) buf[0] == SSH_MSG_IGNORE)
542 goto restart;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Miller95def091999-11-25 00:26:21 +1100544 /* Send debug messages as debugging output. */
545 if ((unsigned char) buf[0] == SSH_MSG_DEBUG) {
546 debug("Remote: %.900s", packet_get_string(NULL));
547 goto restart;
548 }
549 /* Return type. */
550 return (unsigned char) buf[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551}
Damien Miller95def091999-11-25 00:26:21 +1100552
Damien Miller5428f641999-11-25 11:54:57 +1100553/*
554 * Buffers the given amount of input characters. This is intended to be used
555 * together with packet_read_poll.
556 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
558void
559packet_process_incoming(const char *buf, unsigned int len)
560{
Damien Miller95def091999-11-25 00:26:21 +1100561 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562}
563
564/* Returns a character from the packet. */
565
566unsigned int
567packet_get_char()
568{
Damien Miller95def091999-11-25 00:26:21 +1100569 char ch;
570 buffer_get(&incoming_packet, &ch, 1);
571 return (unsigned char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572}
573
574/* Returns an integer from the packet data. */
575
576unsigned int
577packet_get_int()
578{
Damien Miller95def091999-11-25 00:26:21 +1100579 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580}
581
Damien Miller5428f641999-11-25 11:54:57 +1100582/*
583 * Returns an arbitrary precision integer from the packet data. The integer
584 * must have been initialized before this call.
585 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
587void
Damien Miller95def091999-11-25 00:26:21 +1100588packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589{
Damien Miller95def091999-11-25 00:26:21 +1100590 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591}
592
Damien Miller5428f641999-11-25 11:54:57 +1100593/*
594 * Returns a string from the packet data. The string is allocated using
595 * xmalloc; it is the responsibility of the calling program to free it when
596 * no longer needed. The length_ptr argument may be NULL, or point to an
597 * integer into which the length of the string is stored.
598 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
Damien Miller5428f641999-11-25 11:54:57 +1100600char *
Damien Miller95def091999-11-25 00:26:21 +1100601packet_get_string(unsigned int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602{
Damien Miller95def091999-11-25 00:26:21 +1100603 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604}
605
Damien Miller5428f641999-11-25 11:54:57 +1100606/*
607 * Sends a diagnostic message from the server to the client. This message
608 * can be sent at any time (but not while constructing another message). The
609 * message is printed immediately, but only if the client is being executed
610 * in verbose mode. These messages are primarily intended to ease debugging
611 * authentication problems. The length of the formatted message must not
612 * exceed 1024 bytes. This will automatically call packet_write_wait.
613 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
615void
Damien Miller95def091999-11-25 00:26:21 +1100616packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617{
Damien Miller95def091999-11-25 00:26:21 +1100618 char buf[1024];
619 va_list args;
620
621 va_start(args, fmt);
622 vsnprintf(buf, sizeof(buf), fmt, args);
623 va_end(args);
624
625 packet_start(SSH_MSG_DEBUG);
626 packet_put_string(buf, strlen(buf));
627 packet_send();
628 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629}
630
Damien Miller5428f641999-11-25 11:54:57 +1100631/*
632 * Logs the error plus constructs and sends a disconnect packet, closes the
633 * connection, and exits. This function never returns. The error message
634 * should not contain a newline. The length of the formatted message must
635 * not exceed 1024 bytes.
636 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
638void
Damien Miller95def091999-11-25 00:26:21 +1100639packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640{
Damien Miller95def091999-11-25 00:26:21 +1100641 char buf[1024];
642 va_list args;
643 static int disconnecting = 0;
644 if (disconnecting) /* Guard against recursive invocations. */
645 fatal("packet_disconnect called recursively.");
646 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647
Damien Miller5428f641999-11-25 11:54:57 +1100648 /*
649 * Format the message. Note that the caller must make sure the
650 * message is of limited size.
651 */
Damien Miller95def091999-11-25 00:26:21 +1100652 va_start(args, fmt);
653 vsnprintf(buf, sizeof(buf), fmt, args);
654 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655
Damien Miller95def091999-11-25 00:26:21 +1100656 /* Send the disconnect message to the other side, and wait for it to get sent. */
657 packet_start(SSH_MSG_DISCONNECT);
658 packet_put_string(buf, strlen(buf));
659 packet_send();
660 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller95def091999-11-25 00:26:21 +1100662 /* Stop listening for connections. */
663 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller95def091999-11-25 00:26:21 +1100665 /* Close the connection. */
666 packet_close();
667
668 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +1100669 log("Disconnecting: %.100s", buf);
670 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671}
672
Damien Miller5428f641999-11-25 11:54:57 +1100673/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
675void
676packet_write_poll()
677{
Damien Miller95def091999-11-25 00:26:21 +1100678 int len = buffer_len(&output);
679 if (len > 0) {
680 len = write(connection_out, buffer_ptr(&output), len);
681 if (len <= 0) {
682 if (errno == EAGAIN)
683 return;
684 else
685 fatal("Write failed: %.100s", strerror(errno));
686 }
687 buffer_consume(&output, len);
688 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689}
690
Damien Miller5428f641999-11-25 11:54:57 +1100691/*
692 * Calls packet_write_poll repeatedly until all pending output data has been
693 * written.
694 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695
696void
697packet_write_wait()
698{
Damien Miller95def091999-11-25 00:26:21 +1100699 packet_write_poll();
700 while (packet_have_data_to_write()) {
701 fd_set set;
702 FD_ZERO(&set);
703 FD_SET(connection_out, &set);
704 select(connection_out + 1, NULL, &set, NULL, NULL);
705 packet_write_poll();
706 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707}
708
709/* Returns true if there is buffered data to write to the connection. */
710
711int
712packet_have_data_to_write()
713{
Damien Miller95def091999-11-25 00:26:21 +1100714 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
717/* Returns true if there is not too much data to write to the connection. */
718
719int
720packet_not_very_much_data_to_write()
721{
Damien Miller95def091999-11-25 00:26:21 +1100722 if (interactive_mode)
723 return buffer_len(&output) < 16384;
724 else
725 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726}
727
728/* Informs that the current session is interactive. Sets IP flags for that. */
729
730void
731packet_set_interactive(int interactive, int keepalives)
732{
Damien Miller95def091999-11-25 00:26:21 +1100733 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734
Damien Miller95def091999-11-25 00:26:21 +1100735 /* Record that we are in interactive mode. */
736 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller5428f641999-11-25 11:54:57 +1100738 /*
739 * Only set socket options if using a socket (as indicated by the
740 * descriptors being the same).
741 */
Damien Miller95def091999-11-25 00:26:21 +1100742 if (connection_in != connection_out)
743 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744
Damien Miller95def091999-11-25 00:26:21 +1100745 if (keepalives) {
746 /* Set keepalives if requested. */
747 if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
748 sizeof(on)) < 0)
749 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
750 }
751 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +1100752 /*
753 * Set IP options for an interactive connection. Use
754 * IPTOS_LOWDELAY and TCP_NODELAY.
755 */
Damien Miller95def091999-11-25 00:26:21 +1100756 int lowdelay = IPTOS_LOWDELAY;
757 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay,
758 sizeof(lowdelay)) < 0)
759 error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno));
760 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
761 sizeof(on)) < 0)
762 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
763 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100764 /*
765 * Set IP options for a non-interactive connection. Use
766 * IPTOS_THROUGHPUT.
767 */
Damien Miller95def091999-11-25 00:26:21 +1100768 int throughput = IPTOS_THROUGHPUT;
769 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
770 sizeof(throughput)) < 0)
771 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
772 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773}
774
775/* Returns true if the current connection is interactive. */
776
777int
778packet_is_interactive()
779{
Damien Miller95def091999-11-25 00:26:21 +1100780 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781}
Damien Miller6162d121999-11-21 13:23:52 +1100782
783int
784packet_set_maxsize(int s)
785{
Damien Miller95def091999-11-25 00:26:21 +1100786 static int called = 0;
787 if (called) {
788 log("packet_set_maxsize: called twice: old %d new %d", max_packet_size, s);
789 return -1;
790 }
791 if (s < 4 * 1024 || s > 1024 * 1024) {
792 log("packet_set_maxsize: bad size %d", s);
793 return -1;
794 }
795 log("packet_set_maxsize: setting to %d", s);
796 max_packet_size = s;
797 return s;
Damien Miller6162d121999-11-21 13:23:52 +1100798}