blob: b11519e74f6c825ded9a7068b7db7760f082a173 [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.
Damien Miller33b13562000-04-04 14:38:59 +100014 *
15 * SSH2 packet format added by Markus Friedl.
16 *
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "includes.h"
Damien Miller1383bd82000-04-06 12:32:37 +100020RCSID("$Id: packet.c,v 1.16 2000/04/06 02:32:40 damien Exp $");
21
22#ifdef HAVE_OPENSSL
23# include <openssl/bn.h>
24# include <openssl/dh.h>
25# include <openssl/hmac.h>
26#endif /* HAVE_OPENSSL */
27#ifdef HAVE_SSL
28# include <ssl/bn.h>
29# include <ssl/dh.h>
30# include <ssl/hmac.h>
31#endif /* HAVE_SSL */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
33#include "xmalloc.h"
34#include "buffer.h"
35#include "packet.h"
36#include "bufaux.h"
37#include "ssh.h"
38#include "crc32.h"
39#include "cipher.h"
40#include "getput.h"
41
42#include "compress.h"
43#include "deattack.h"
Damien Millerb38eff82000-04-01 11:09:21 +100044#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller33b13562000-04-04 14:38:59 +100046#include "compat.h"
47#include "ssh2.h"
48
Damien Miller33b13562000-04-04 14:38:59 +100049#include "buffer.h"
50#include "kex.h"
51#include "hmac.h"
52
53#ifdef PACKET_DEBUG
54#define DBG(x) x
55#else
56#define DBG(x)
57#endif
58
Damien Miller5428f641999-11-25 11:54:57 +110059/*
60 * This variable contains the file descriptors used for communicating with
61 * the other side. connection_in is used for reading; connection_out for
62 * writing. These can be the same descriptor, in which case it is assumed to
63 * be a socket.
64 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065static int connection_in = -1;
66static int connection_out = -1;
67
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Cipher type. This value is only used to determine whether to pad the
70 * packets with zeroes or random data.
71 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072static int cipher_type = SSH_CIPHER_NONE;
73
74/* Protocol flags for the remote side. */
75static unsigned int remote_protocol_flags = 0;
76
77/* Encryption context for receiving data. This is only used for decryption. */
78static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110079
80/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static CipherContext send_context;
82
83/* Buffer for raw input data from the socket. */
84static Buffer input;
85
86/* Buffer for raw output data going to the socket. */
87static Buffer output;
88
89/* Buffer for the partial outgoing packet being constructed. */
90static Buffer outgoing_packet;
91
92/* Buffer for the incoming packet currently being processed. */
93static Buffer incoming_packet;
94
95/* Scratch buffer for packet compression/decompression. */
96static Buffer compression_buffer;
97
98/* Flag indicating whether packet compression/decompression is enabled. */
99static int packet_compression = 0;
100
Damien Miller6162d121999-11-21 13:23:52 +1100101/* default maximum packet size */
102int max_packet_size = 32768;
103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104/* Flag indicating whether this module has been initialized. */
105static int initialized = 0;
106
107/* Set to true if the connection is interactive. */
108static int interactive_mode = 0;
109
Damien Miller33b13562000-04-04 14:38:59 +1000110/* True if SSH2 packet format is used */
111int use_ssh2_packet_format = 0;
112
113/* Session key information for Encryption and MAC */
114Kex *kex = NULL;
115
116void
117packet_set_kex(Kex *k)
118{
119 if( k->mac[MODE_IN ].key == NULL ||
120 k->enc[MODE_IN ].key == NULL ||
121 k->enc[MODE_IN ].iv == NULL ||
122 k->mac[MODE_OUT].key == NULL ||
123 k->enc[MODE_OUT].key == NULL ||
124 k->enc[MODE_OUT].iv == NULL)
125 fatal("bad KEX");
126 kex = k;
127}
128void
129clear_enc_keys(Enc *enc, int len)
130{
131 memset(enc->iv, 0, len);
132 memset(enc->key, 0, len);
133 xfree(enc->iv);
134 xfree(enc->key);
135 enc->iv = NULL;
136 enc->key = NULL;
137}
138void
139packet_set_ssh2_format(void)
140{
141 debug("use_ssh2_packet_format");
142 use_ssh2_packet_format = 1;
143}
144
Damien Miller5428f641999-11-25 11:54:57 +1100145/*
146 * Sets the descriptors used for communication. Disables encryption until
147 * packet_set_encryption_key is called.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149void
150packet_set_connection(int fd_in, int fd_out)
151{
Damien Miller95def091999-11-25 00:26:21 +1100152 connection_in = fd_in;
153 connection_out = fd_out;
154 cipher_type = SSH_CIPHER_NONE;
Damien Miller1383bd82000-04-06 12:32:37 +1000155 cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0);
156 cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0);
Damien Miller95def091999-11-25 00:26:21 +1100157 if (!initialized) {
158 initialized = 1;
159 buffer_init(&input);
160 buffer_init(&output);
161 buffer_init(&outgoing_packet);
162 buffer_init(&incoming_packet);
163 }
164 /* Kludge: arrange the close function to be called from fatal(). */
165 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Damien Miller34132e52000-01-14 15:45:46 +1100168/* Returns 1 if remote host is connected via socket, 0 if not. */
169
170int
171packet_connection_is_on_socket()
172{
173 struct sockaddr_storage from, to;
174 socklen_t fromlen, tolen;
175
176 /* filedescriptors in and out are the same, so it's a socket */
177 if (connection_in == connection_out)
178 return 1;
179 fromlen = sizeof(from);
180 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100181 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100182 return 0;
183 tolen = sizeof(to);
184 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100185 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100186 return 0;
187 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
188 return 0;
189 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
190 return 0;
191 return 1;
192}
193
194/* returns 1 if connection is via ipv4 */
195
196int
197packet_connection_is_ipv4()
198{
199 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100200 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100201
202 memset(&to, 0, sizeof(to));
203 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
204 return 0;
205 if (to.ss_family != AF_INET)
206 return 0;
207 return 1;
208}
209
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210/* Sets the connection into non-blocking mode. */
211
212void
213packet_set_nonblocking()
214{
Damien Miller95def091999-11-25 00:26:21 +1100215 /* Set the socket into non-blocking mode. */
216 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
217 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 if (connection_out != connection_in) {
220 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
221 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
222 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223}
224
225/* Returns the socket used for reading. */
226
227int
228packet_get_connection_in()
229{
Damien Miller95def091999-11-25 00:26:21 +1100230 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
233/* Returns the descriptor used for writing. */
234
235int
236packet_get_connection_out()
237{
Damien Miller95def091999-11-25 00:26:21 +1100238 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
241/* Closes the connection and clears and frees internal data structures. */
242
243void
244packet_close()
245{
Damien Miller95def091999-11-25 00:26:21 +1100246 if (!initialized)
247 return;
248 initialized = 0;
249 if (connection_in == connection_out) {
250 shutdown(connection_out, SHUT_RDWR);
251 close(connection_out);
252 } else {
253 close(connection_in);
254 close(connection_out);
255 }
256 buffer_free(&input);
257 buffer_free(&output);
258 buffer_free(&outgoing_packet);
259 buffer_free(&incoming_packet);
260 if (packet_compression) {
261 buffer_free(&compression_buffer);
262 buffer_compress_uninit();
263 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264}
265
266/* Sets remote side protocol flags. */
267
268void
269packet_set_protocol_flags(unsigned int protocol_flags)
270{
Damien Miller95def091999-11-25 00:26:21 +1100271 remote_protocol_flags = protocol_flags;
272 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
275/* Returns the remote protocol flags set earlier by the above function. */
276
277unsigned int
278packet_get_protocol_flags()
279{
Damien Miller95def091999-11-25 00:26:21 +1100280 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281}
282
Damien Miller5428f641999-11-25 11:54:57 +1100283/*
284 * Starts packet compression from the next packet on in both directions.
285 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
286 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller33b13562000-04-04 14:38:59 +1000288/*** XXXXX todo: kex means re-init */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289void
290packet_start_compression(int level)
291{
Damien Miller95def091999-11-25 00:26:21 +1100292 if (packet_compression)
293 fatal("Compression already enabled.");
294 packet_compression = 1;
295 buffer_init(&compression_buffer);
296 buffer_compress_init(level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Damien Miller5428f641999-11-25 11:54:57 +1100299/*
300 * Encrypts the given number of bytes, copying from src to dest. bytes is
301 * known to be a multiple of 8.
302 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
304void
Damien Miller95def091999-11-25 00:26:21 +1100305packet_encrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000306 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Damien Miller95def091999-11-25 00:26:21 +1100308 cipher_encrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
312 * Decrypts the given number of bytes, copying from src to dest. bytes is
313 * known to be a multiple of 8.
314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
316void
Damien Miller95def091999-11-25 00:26:21 +1100317packet_decrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000318 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Damien Miller95def091999-11-25 00:26:21 +1100320 int i;
321
322 if ((bytes % 8) != 0)
323 fatal("packet_decrypt: bad ciphertext length %d", bytes);
324
Damien Miller5428f641999-11-25 11:54:57 +1100325 /*
326 * Cryptographic attack detector for ssh - Modifications for packet.c
327 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
328 */
Damien Miller95def091999-11-25 00:26:21 +1100329
Damien Miller33b13562000-04-04 14:38:59 +1000330 if (cc->type == SSH_CIPHER_NONE || compat20) {
Damien Miller95def091999-11-25 00:26:21 +1100331 i = DEATTACK_OK;
Damien Miller33b13562000-04-04 14:38:59 +1000332 } else {
Damien Miller95def091999-11-25 00:26:21 +1100333 i = detect_attack(src, bytes, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100334 }
Damien Miller95def091999-11-25 00:26:21 +1100335 if (i == DEATTACK_DETECTED)
336 packet_disconnect("crc32 compensation attack: network attack detected");
337
338 cipher_decrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339}
340
Damien Miller5428f641999-11-25 11:54:57 +1100341/*
342 * Causes any further packets to be encrypted using the given key. The same
343 * key is used for both sending and reception. However, both directions are
344 * encrypted independently of each other.
345 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
347void
348packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
Damien Miller33b13562000-04-04 14:38:59 +1000349 int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350{
Damien Miller33b13562000-04-04 14:38:59 +1000351 if (keylen < 20)
352 fatal("keylen too small: %d", keylen);
353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* All other ciphers use the same key in both directions for now. */
Damien Miller1383bd82000-04-06 12:32:37 +1000355 cipher_set_key(&receive_context, cipher, key, keylen);
356 cipher_set_key(&send_context, cipher, key, keylen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
359/* Starts constructing a packet to send. */
360
361void
Damien Miller33b13562000-04-04 14:38:59 +1000362packet_start1(int type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363{
Damien Miller95def091999-11-25 00:26:21 +1100364 char buf[9];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Damien Miller95def091999-11-25 00:26:21 +1100366 buffer_clear(&outgoing_packet);
367 memset(buf, 0, 8);
368 buf[8] = type;
369 buffer_append(&outgoing_packet, buf, 9);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
371
Damien Miller33b13562000-04-04 14:38:59 +1000372void
373packet_start2(int type)
374{
375 char buf[4+1+1];
376
377 buffer_clear(&outgoing_packet);
378 memset(buf, 0, sizeof buf);
379 /* buf[0..3] = payload_len; */
380 /* buf[4] = pad_len; */
381 buf[5] = type & 0xff;
382 buffer_append(&outgoing_packet, buf, sizeof buf);
383}
384
385void
386packet_start(int type)
387{
388 DBG(debug("packet_start[%d]",type));
389 if (use_ssh2_packet_format)
390 packet_start2(type);
391 else
392 packet_start1(type);
393}
394
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395/* Appends a character to the packet data. */
396
397void
398packet_put_char(int value)
399{
Damien Miller95def091999-11-25 00:26:21 +1100400 char ch = value;
401 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
404/* Appends an integer to the packet data. */
405
406void
407packet_put_int(unsigned int value)
408{
Damien Miller95def091999-11-25 00:26:21 +1100409 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410}
411
412/* Appends a string to packet data. */
413
414void
415packet_put_string(const char *buf, unsigned int len)
416{
Damien Miller95def091999-11-25 00:26:21 +1100417 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418}
Damien Miller33b13562000-04-04 14:38:59 +1000419void
420packet_put_cstring(const char *str)
421{
422 buffer_put_string(&outgoing_packet, str, strlen(str));
423}
424
425void
426packet_put_raw(const char *buf, unsigned int len)
427{
428 buffer_append(&outgoing_packet, buf, len);
429}
430
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
432/* Appends an arbitrary precision integer to packet data. */
433
434void
Damien Miller95def091999-11-25 00:26:21 +1100435packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller95def091999-11-25 00:26:21 +1100437 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438}
Damien Miller33b13562000-04-04 14:38:59 +1000439void
440packet_put_bignum2(BIGNUM * value)
441{
442 buffer_put_bignum2(&outgoing_packet, value);
443}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller5428f641999-11-25 11:54:57 +1100445/*
446 * Finalizes and sends the packet. If the encryption key has been set,
447 * encrypts the packet before sending.
448 */
Damien Miller95def091999-11-25 00:26:21 +1100449
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450void
Damien Miller33b13562000-04-04 14:38:59 +1000451packet_send1()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452{
Damien Miller95def091999-11-25 00:26:21 +1100453 char buf[8], *cp;
454 int i, padding, len;
455 unsigned int checksum;
456 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller5428f641999-11-25 11:54:57 +1100458 /*
459 * If using packet compression, compress the payload of the outgoing
460 * packet.
461 */
Damien Miller95def091999-11-25 00:26:21 +1100462 if (packet_compression) {
463 buffer_clear(&compression_buffer);
464 /* Skip padding. */
465 buffer_consume(&outgoing_packet, 8);
466 /* padding */
467 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
468 buffer_compress(&outgoing_packet, &compression_buffer);
469 buffer_clear(&outgoing_packet);
470 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
471 buffer_len(&compression_buffer));
472 }
473 /* Compute packet length without padding (add checksum, remove padding). */
474 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Damien Miller95def091999-11-25 00:26:21 +1100476 /* Insert padding. */
477 padding = 8 - len % 8;
478 if (cipher_type != SSH_CIPHER_NONE) {
479 cp = buffer_ptr(&outgoing_packet);
480 for (i = 0; i < padding; i++) {
481 if (i % 4 == 0)
482 rand = arc4random();
483 cp[7 - i] = rand & 0xff;
484 rand >>= 8;
485 }
486 }
487 buffer_consume(&outgoing_packet, 8 - padding);
488
489 /* Add check bytes. */
490 checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet),
491 buffer_len(&outgoing_packet));
492 PUT_32BIT(buf, checksum);
493 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
495#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100496 fprintf(stderr, "packet_send plain: ");
497 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498#endif
499
Damien Miller95def091999-11-25 00:26:21 +1100500 /* Append to output. */
501 PUT_32BIT(buf, len);
502 buffer_append(&output, buf, 4);
503 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
504 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
505 buffer_len(&outgoing_packet));
506
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100508 fprintf(stderr, "encrypted: ");
509 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510#endif
511
Damien Miller95def091999-11-25 00:26:21 +1100512 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller5428f641999-11-25 11:54:57 +1100514 /*
515 * Note that the packet is now only buffered in output. It won\'t be
516 * actually sent until packet_write_wait or packet_write_poll is
517 * called.
518 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519}
520
Damien Miller5428f641999-11-25 11:54:57 +1100521/*
Damien Miller33b13562000-04-04 14:38:59 +1000522 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
523 */
524void
525packet_send2()
526{
527 unsigned char *macbuf = NULL;
528 char *cp;
529 unsigned int packet_length = 0;
530 unsigned int i, padlen, len;
531 u_int32_t rand = 0;
532 static unsigned int seqnr = 0;
533 int type;
534 Enc *enc = NULL;
535 Mac *mac = NULL;
536 Comp *comp = NULL;
537 int block_size;
538
539 if (kex != NULL) {
540 enc = &kex->enc[MODE_OUT];
541 mac = &kex->mac[MODE_OUT];
542 comp = &kex->comp[MODE_OUT];
543 }
544 block_size = enc ? enc->block_size : 8;
545
546 cp = buffer_ptr(&outgoing_packet);
547 type = cp[5] & 0xff;
548
549#ifdef PACKET_DEBUG
550 fprintf(stderr, "plain: ");
551 buffer_dump(&outgoing_packet);
552#endif
553
554 if (comp && comp->enabled) {
555 len = buffer_len(&outgoing_packet);
556 /* skip header, compress only payload */
557 buffer_consume(&outgoing_packet, 5);
558 buffer_clear(&compression_buffer);
559 buffer_compress(&outgoing_packet, &compression_buffer);
560 buffer_clear(&outgoing_packet);
561 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
562 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
563 buffer_len(&compression_buffer));
564 DBG(debug("compression: raw %d compressed %d", len,
565 buffer_len(&outgoing_packet)));
566 }
567
568 /* sizeof (packet_len + pad_len + payload) */
569 len = buffer_len(&outgoing_packet);
570
571 /*
572 * calc size of padding, alloc space, get random data,
573 * minimum padding is 4 bytes
574 */
575 padlen = block_size - (len % block_size);
576 if (padlen < 4)
577 padlen += block_size;
578 buffer_append_space(&outgoing_packet, &cp, padlen);
579 if (enc && enc->type != SSH_CIPHER_NONE) {
580 for (i = 0; i < padlen; i++) {
581 if (i % 4 == 0)
582 rand = arc4random();
583 cp[i] = rand & 0xff;
584 rand <<= 8;
585 }
586 }
587 /* packet_length includes payload, padding and padding length field */
588 packet_length = buffer_len(&outgoing_packet) - 4;
589 cp = buffer_ptr(&outgoing_packet);
590 PUT_32BIT(cp, packet_length);
591 cp[4] = padlen & 0xff;
592 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
593
594 /* compute MAC over seqnr and packet(length fields, payload, padding) */
595 if (mac && mac->enabled) {
596 macbuf = hmac( mac->md, seqnr,
597 (unsigned char *) buffer_ptr(&outgoing_packet),
598 buffer_len(&outgoing_packet),
599 mac->key, mac->key_len
600 );
601 DBG(debug("done calc HMAC out #%d", seqnr));
602 }
603 /* encrypt packet and append to output buffer. */
604 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
605 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
606 buffer_len(&outgoing_packet));
607 /* append unencrypted MAC */
608 if (mac && mac->enabled)
609 buffer_append(&output, (char *)macbuf, mac->mac_len);
610#ifdef PACKET_DEBUG
611 fprintf(stderr, "encrypted: ");
612 buffer_dump(&output);
613#endif
614 /* increment sequence number for outgoing packets */
615 if (++seqnr == 0)
616 log("outgoing seqnr wraps around");
617 buffer_clear(&outgoing_packet);
618
619 if (type == SSH2_MSG_NEWKEYS) {
620 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
621 fatal("packet_send2: no KEX");
622 if (mac->md != NULL)
623 mac->enabled = 1;
624 debug("cipher_set_key_iv send_context");
625 cipher_set_key_iv(&send_context, enc->type,
626 enc->key, enc->key_len,
627 enc->iv, enc->iv_len);
628 clear_enc_keys(enc, kex->we_need);
629 if (comp->type != 0 && comp->enabled == 0) {
630 comp->enabled = 1;
631 if (! packet_compression)
632 packet_start_compression(6);
633 }
634 }
635}
636
637void
638packet_send()
639{
640 if (use_ssh2_packet_format)
641 packet_send2();
642 else
643 packet_send1();
644 DBG(debug("packet_send done"));
645}
646
647void
648packet_send_and_wait()
649{
650 packet_send();
651 packet_write_wait();
652}
653
654/*
Damien Miller5428f641999-11-25 11:54:57 +1100655 * Waits until a packet has been received, and returns its type. Note that
656 * no other data is processed until this returns, so this function should not
657 * be used during the interactive session.
658 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659
660int
661packet_read(int *payload_len_ptr)
662{
Damien Miller95def091999-11-25 00:26:21 +1100663 int type, len;
664 fd_set set;
665 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000666 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller95def091999-11-25 00:26:21 +1100668 /* Since we are blocking, ensure that all written packets have been sent. */
669 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670
Damien Miller95def091999-11-25 00:26:21 +1100671 /* Stay in the loop until we have received a complete packet. */
672 for (;;) {
673 /* Try to read a packet from the buffer. */
674 type = packet_read_poll(payload_len_ptr);
675 if (type == SSH_SMSG_SUCCESS
676 || type == SSH_SMSG_FAILURE
677 || type == SSH_CMSG_EOF
678 || type == SSH_CMSG_EXIT_CONFIRMATION)
679 packet_integrity_check(*payload_len_ptr, 0, type);
680 /* If we got a packet, return it. */
681 if (type != SSH_MSG_NONE)
682 return type;
Damien Miller5428f641999-11-25 11:54:57 +1100683 /*
684 * Otherwise, wait for some data to arrive, add it to the
685 * buffer, and try again.
686 */
Damien Miller95def091999-11-25 00:26:21 +1100687 FD_ZERO(&set);
688 FD_SET(connection_in, &set);
Damien Miller5428f641999-11-25 11:54:57 +1100689
Damien Miller95def091999-11-25 00:26:21 +1100690 /* Wait for some data to arrive. */
691 select(connection_in + 1, &set, NULL, NULL, NULL);
Damien Miller5428f641999-11-25 11:54:57 +1100692
Damien Miller95def091999-11-25 00:26:21 +1100693 /* Read data from the socket. */
694 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100695 if (len == 0) {
696 log("Connection closed by %.200s", get_remote_ipaddr());
697 fatal_cleanup();
698 }
Damien Miller95def091999-11-25 00:26:21 +1100699 if (len < 0)
700 fatal("Read from socket failed: %.100s", strerror(errno));
701 /* Append it to the buffer. */
702 packet_process_incoming(buf, len);
703 }
704 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
Damien Miller5428f641999-11-25 11:54:57 +1100707/*
708 * Waits until a packet has been received, verifies that its type matches
709 * that given, and gives a fatal error and exits if there is a mismatch.
710 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
712void
713packet_read_expect(int *payload_len_ptr, int expected_type)
714{
Damien Miller95def091999-11-25 00:26:21 +1100715 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
Damien Miller95def091999-11-25 00:26:21 +1100717 type = packet_read(payload_len_ptr);
718 if (type != expected_type)
719 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000720 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721}
722
723/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100724 * packet_process_incoming. If so, reads the packet; otherwise returns
725 * SSH_MSG_NONE. This does not wait for data from the connection.
726 *
727 * SSH_MSG_DISCONNECT is handled specially here. Also,
728 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
729 * to higher levels.
730 *
731 * The returned payload_len does include space consumed by:
732 * Packet length
733 * Padding
734 * Packet type
735 * Check bytes
736 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
738int
Damien Miller33b13562000-04-04 14:38:59 +1000739packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740{
Damien Miller95def091999-11-25 00:26:21 +1100741 unsigned int len, padded_len;
742 unsigned char *ucp;
Damien Miller33b13562000-04-04 14:38:59 +1000743 char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100744 unsigned int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745
Damien Miller95def091999-11-25 00:26:21 +1100746 /* Check if input size is less than minimum packet size. */
747 if (buffer_len(&input) < 4 + 8)
748 return SSH_MSG_NONE;
749 /* Get length of incoming packet. */
750 ucp = (unsigned char *) buffer_ptr(&input);
751 len = GET_32BIT(ucp);
752 if (len < 1 + 2 + 2 || len > 256 * 1024)
753 packet_disconnect("Bad packet length %d.", len);
754 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Check if the packet has been entirely received. */
757 if (buffer_len(&input) < 4 + padded_len)
758 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller95def091999-11-25 00:26:21 +1100760 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761
Damien Miller95def091999-11-25 00:26:21 +1100762 /* Consume packet length. */
763 buffer_consume(&input, 4);
764
765 /* Copy data to incoming_packet. */
766 buffer_clear(&incoming_packet);
767 buffer_append_space(&incoming_packet, &cp, padded_len);
768 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
769 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770
771#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100772 fprintf(stderr, "read_poll plain: ");
773 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775
Damien Miller95def091999-11-25 00:26:21 +1100776 /* Compute packet checksum. */
777 checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000778 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779
Damien Miller95def091999-11-25 00:26:21 +1100780 /* Skip padding. */
781 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100782
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Test check bytes. */
Damien Millerfd7c9111999-11-08 16:15:55 +1100784
Damien Miller95def091999-11-25 00:26:21 +1100785 if (len != buffer_len(&incoming_packet))
786 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000787 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788
Damien Miller95def091999-11-25 00:26:21 +1100789 ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4;
790 stored_checksum = GET_32BIT(ucp);
791 if (checksum != stored_checksum)
792 packet_disconnect("Corrupted check bytes on input.");
793 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794
Damien Miller95def091999-11-25 00:26:21 +1100795 /* If using packet compression, decompress the packet. */
796 if (packet_compression) {
797 buffer_clear(&compression_buffer);
798 buffer_uncompress(&incoming_packet, &compression_buffer);
799 buffer_clear(&incoming_packet);
800 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000801 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100802 }
803 /* Get packet type. */
804 buffer_get(&incoming_packet, &buf[0], 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller95def091999-11-25 00:26:21 +1100806 /* Return length of payload (without type field). */
807 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Return type. */
810 return (unsigned char) buf[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}
Damien Miller95def091999-11-25 00:26:21 +1100812
Damien Miller33b13562000-04-04 14:38:59 +1000813int
814packet_read_poll2(int *payload_len_ptr)
815{
816 unsigned int padlen, need;
817 unsigned char buf[8], *macbuf;
818 unsigned char *ucp;
819 char *cp;
820 static unsigned int packet_length = 0;
821 static unsigned int seqnr = 0;
822 int type;
823 int maclen, block_size;
824 Enc *enc = NULL;
825 Mac *mac = NULL;
826 Comp *comp = NULL;
827
828 if (kex != NULL) {
829 enc = &kex->enc[MODE_IN];
830 mac = &kex->mac[MODE_IN];
831 comp = &kex->comp[MODE_IN];
832 }
833 maclen = mac && mac->enabled ? mac->mac_len : 0;
834 block_size = enc ? enc->block_size : 8;
835
836 if (packet_length == 0) {
837 /*
838 * check if input size is less than the cipher block size,
839 * decrypt first block and extract length of incoming packet
840 */
841 if (buffer_len(&input) < block_size)
842 return SSH_MSG_NONE;
843 buffer_clear(&incoming_packet);
844 buffer_append_space(&incoming_packet, &cp, block_size);
845 packet_decrypt(&receive_context, cp, buffer_ptr(&input),
846 block_size);
847 ucp = (unsigned char *) buffer_ptr(&incoming_packet);
848 packet_length = GET_32BIT(ucp);
849 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
850 buffer_dump(&incoming_packet);
851 packet_disconnect("Bad packet length %d.", packet_length);
852 }
853 DBG(debug("input: packet len %d", packet_length+4));
854 buffer_consume(&input, block_size);
855 }
856 /* we have a partial packet of block_size bytes */
857 need = 4 + packet_length - block_size;
858 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
859 need, maclen));
860 if (need % block_size != 0)
861 fatal("padding error: need %d block %d mod %d",
862 need, block_size, need % block_size);
863 /*
864 * check if the entire packet has been received and
865 * decrypt into incoming_packet
866 */
867 if (buffer_len(&input) < need + maclen)
868 return SSH_MSG_NONE;
869#ifdef PACKET_DEBUG
870 fprintf(stderr, "read_poll enc/full: ");
871 buffer_dump(&input);
872#endif
873 buffer_append_space(&incoming_packet, &cp, need);
874 packet_decrypt(&receive_context, cp, buffer_ptr(&input), need);
875 buffer_consume(&input, need);
876 /*
877 * compute MAC over seqnr and packet,
878 * increment sequence number for incoming packet
879 */
880 if (mac && mac->enabled) {
881 macbuf = hmac( mac->md, seqnr,
882 (unsigned char *) buffer_ptr(&incoming_packet),
883 buffer_len(&incoming_packet),
884 mac->key, mac->key_len
885 );
886 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
887 packet_disconnect("Corrupted HMAC on input.");
888 DBG(debug("HMAC #%d ok", seqnr));
889 buffer_consume(&input, mac->mac_len);
890 }
891 if (++seqnr == 0)
892 log("incoming seqnr wraps around");
893
894 /* get padlen */
895 cp = buffer_ptr(&incoming_packet) + 4;
896 padlen = *cp & 0xff;
897 DBG(debug("input: padlen %d", padlen));
898 if (padlen < 4)
899 packet_disconnect("Corrupted padlen %d on input.", padlen);
900
901 /* skip packet size + padlen, discard padding */
902 buffer_consume(&incoming_packet, 4 + 1);
903 buffer_consume_end(&incoming_packet, padlen);
904
905 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
906 if (comp && comp->enabled) {
907 buffer_clear(&compression_buffer);
908 buffer_uncompress(&incoming_packet, &compression_buffer);
909 buffer_clear(&incoming_packet);
910 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
911 buffer_len(&compression_buffer));
912 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
913 }
914 /*
915 * get packet type, implies consume.
916 * return length of payload (without type field)
917 */
918 buffer_get(&incoming_packet, (char *)&buf[0], 1);
919 *payload_len_ptr = buffer_len(&incoming_packet);
920
921 /* reset for next packet */
922 packet_length = 0;
923
924 /* extract packet type */
925 type = (unsigned char)buf[0];
926
927 if (type == SSH2_MSG_NEWKEYS) {
928 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
929 fatal("packet_read_poll2: no KEX");
930 if (mac->md != NULL)
931 mac->enabled = 1;
932 debug("cipher_set_key_iv receive_context");
933 cipher_set_key_iv(&receive_context, enc->type,
934 enc->key, enc->key_len,
935 enc->iv, enc->iv_len);
936 clear_enc_keys(enc, kex->we_need);
937 if (comp->type != 0 && comp->enabled == 0) {
938 comp->enabled = 1;
939 if (! packet_compression)
940 packet_start_compression(6);
941 }
942 }
943
944#ifdef PACKET_DEBUG
945 fprintf(stderr, "read/plain[%d]:\r\n",type);
946 buffer_dump(&incoming_packet);
947#endif
948 return (unsigned char)type;
949}
950
951int
952packet_read_poll(int *payload_len_ptr)
953{
954 char *msg;
955 for (;;) {
956 int type = use_ssh2_packet_format ?
957 packet_read_poll2(payload_len_ptr):
958 packet_read_poll1(payload_len_ptr);
959
960 if(compat20) {
961 int reason;
962 if (type != 0)
963 DBG(debug("received packet type %d", type));
964 switch(type) {
965 case SSH2_MSG_IGNORE:
966 break;
967 case SSH2_MSG_DEBUG:
968 packet_get_char();
969 msg = packet_get_string(NULL);
970 debug("Remote: %.900s", msg);
971 xfree(msg);
972 msg = packet_get_string(NULL);
973 xfree(msg);
974 break;
975 case SSH2_MSG_DISCONNECT:
976 reason = packet_get_int();
977 msg = packet_get_string(NULL);
978 log("Received disconnect: %d: %.900s", reason, msg);
979 xfree(msg);
980 fatal_cleanup();
981 break;
982 default:
983 return type;
984 break;
985 }
986 } else {
987 switch(type) {
988 case SSH_MSG_IGNORE:
989 break;
990 case SSH_MSG_DEBUG:
991 msg = packet_get_string(NULL);
992 debug("Remote: %.900s", msg);
993 xfree(msg);
994 break;
995 case SSH_MSG_DISCONNECT:
996 msg = packet_get_string(NULL);
997 log("Received disconnect: %.900s", msg);
998 fatal_cleanup();
999 xfree(msg);
1000 break;
1001 default:
1002 if (type != 0)
1003 DBG(debug("received packet type %d", type));
1004 return type;
1005 break;
1006 }
1007 }
1008 }
1009}
1010
Damien Miller5428f641999-11-25 11:54:57 +11001011/*
1012 * Buffers the given amount of input characters. This is intended to be used
1013 * together with packet_read_poll.
1014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
1016void
1017packet_process_incoming(const char *buf, unsigned int len)
1018{
Damien Miller95def091999-11-25 00:26:21 +11001019 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020}
1021
1022/* Returns a character from the packet. */
1023
1024unsigned int
1025packet_get_char()
1026{
Damien Miller95def091999-11-25 00:26:21 +11001027 char ch;
1028 buffer_get(&incoming_packet, &ch, 1);
1029 return (unsigned char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030}
1031
1032/* Returns an integer from the packet data. */
1033
1034unsigned int
1035packet_get_int()
1036{
Damien Miller95def091999-11-25 00:26:21 +11001037 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038}
1039
Damien Miller5428f641999-11-25 11:54:57 +11001040/*
1041 * Returns an arbitrary precision integer from the packet data. The integer
1042 * must have been initialized before this call.
1043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
1045void
Damien Miller95def091999-11-25 00:26:21 +11001046packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047{
Damien Miller95def091999-11-25 00:26:21 +11001048 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049}
1050
Damien Miller33b13562000-04-04 14:38:59 +10001051void
1052packet_get_bignum2(BIGNUM * value, int *length_ptr)
1053{
1054 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
1055}
1056
1057char *
1058packet_get_raw(int *length_ptr)
1059{
1060 int bytes = buffer_len(&incoming_packet);
1061 if (length_ptr != NULL)
1062 *length_ptr = bytes;
1063 return buffer_ptr(&incoming_packet);
1064}
1065
Damien Miller5428f641999-11-25 11:54:57 +11001066/*
1067 * Returns a string from the packet data. The string is allocated using
1068 * xmalloc; it is the responsibility of the calling program to free it when
1069 * no longer needed. The length_ptr argument may be NULL, or point to an
1070 * integer into which the length of the string is stored.
1071 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
Damien Miller5428f641999-11-25 11:54:57 +11001073char *
Damien Miller95def091999-11-25 00:26:21 +11001074packet_get_string(unsigned int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075{
Damien Miller95def091999-11-25 00:26:21 +11001076 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077}
1078
Damien Miller5428f641999-11-25 11:54:57 +11001079/*
1080 * Sends a diagnostic message from the server to the client. This message
1081 * can be sent at any time (but not while constructing another message). The
1082 * message is printed immediately, but only if the client is being executed
1083 * in verbose mode. These messages are primarily intended to ease debugging
1084 * authentication problems. The length of the formatted message must not
1085 * exceed 1024 bytes. This will automatically call packet_write_wait.
1086 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087
1088void
Damien Miller95def091999-11-25 00:26:21 +11001089packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090{
Damien Miller95def091999-11-25 00:26:21 +11001091 char buf[1024];
1092 va_list args;
1093
1094 va_start(args, fmt);
1095 vsnprintf(buf, sizeof(buf), fmt, args);
1096 va_end(args);
1097
1098 packet_start(SSH_MSG_DEBUG);
1099 packet_put_string(buf, strlen(buf));
1100 packet_send();
1101 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102}
1103
Damien Miller5428f641999-11-25 11:54:57 +11001104/*
1105 * Logs the error plus constructs and sends a disconnect packet, closes the
1106 * connection, and exits. This function never returns. The error message
1107 * should not contain a newline. The length of the formatted message must
1108 * not exceed 1024 bytes.
1109 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110
1111void
Damien Miller95def091999-11-25 00:26:21 +11001112packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113{
Damien Miller95def091999-11-25 00:26:21 +11001114 char buf[1024];
1115 va_list args;
1116 static int disconnecting = 0;
1117 if (disconnecting) /* Guard against recursive invocations. */
1118 fatal("packet_disconnect called recursively.");
1119 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120
Damien Miller5428f641999-11-25 11:54:57 +11001121 /*
1122 * Format the message. Note that the caller must make sure the
1123 * message is of limited size.
1124 */
Damien Miller95def091999-11-25 00:26:21 +11001125 va_start(args, fmt);
1126 vsnprintf(buf, sizeof(buf), fmt, args);
1127 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128
Damien Miller95def091999-11-25 00:26:21 +11001129 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001130 if (compat20) {
1131 packet_start(SSH2_MSG_DISCONNECT);
1132 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1133 packet_put_cstring(buf);
1134 packet_put_cstring("");
1135 } else {
1136 packet_start(SSH_MSG_DISCONNECT);
1137 packet_put_string(buf, strlen(buf));
1138 }
Damien Miller95def091999-11-25 00:26:21 +11001139 packet_send();
1140 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141
Damien Miller95def091999-11-25 00:26:21 +11001142 /* Stop listening for connections. */
1143 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
Damien Miller95def091999-11-25 00:26:21 +11001145 /* Close the connection. */
1146 packet_close();
1147
1148 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001149 log("Disconnecting: %.100s", buf);
1150 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151}
1152
Damien Miller5428f641999-11-25 11:54:57 +11001153/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154
1155void
1156packet_write_poll()
1157{
Damien Miller95def091999-11-25 00:26:21 +11001158 int len = buffer_len(&output);
1159 if (len > 0) {
1160 len = write(connection_out, buffer_ptr(&output), len);
1161 if (len <= 0) {
1162 if (errno == EAGAIN)
1163 return;
1164 else
1165 fatal("Write failed: %.100s", strerror(errno));
1166 }
1167 buffer_consume(&output, len);
1168 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169}
1170
Damien Miller5428f641999-11-25 11:54:57 +11001171/*
1172 * Calls packet_write_poll repeatedly until all pending output data has been
1173 * written.
1174 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
1176void
1177packet_write_wait()
1178{
Damien Miller95def091999-11-25 00:26:21 +11001179 packet_write_poll();
1180 while (packet_have_data_to_write()) {
1181 fd_set set;
1182 FD_ZERO(&set);
1183 FD_SET(connection_out, &set);
1184 select(connection_out + 1, NULL, &set, NULL, NULL);
1185 packet_write_poll();
1186 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187}
1188
1189/* Returns true if there is buffered data to write to the connection. */
1190
1191int
1192packet_have_data_to_write()
1193{
Damien Miller95def091999-11-25 00:26:21 +11001194 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195}
1196
1197/* Returns true if there is not too much data to write to the connection. */
1198
1199int
1200packet_not_very_much_data_to_write()
1201{
Damien Miller95def091999-11-25 00:26:21 +11001202 if (interactive_mode)
1203 return buffer_len(&output) < 16384;
1204 else
1205 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206}
1207
1208/* Informs that the current session is interactive. Sets IP flags for that. */
1209
1210void
1211packet_set_interactive(int interactive, int keepalives)
1212{
Damien Miller95def091999-11-25 00:26:21 +11001213 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214
Damien Miller95def091999-11-25 00:26:21 +11001215 /* Record that we are in interactive mode. */
1216 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217
Damien Miller34132e52000-01-14 15:45:46 +11001218 /* Only set socket options if using a socket. */
1219 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001220 return;
Damien Miller95def091999-11-25 00:26:21 +11001221 if (keepalives) {
1222 /* Set keepalives if requested. */
1223 if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001224 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001225 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1226 }
Damien Miller34132e52000-01-14 15:45:46 +11001227 /*
1228 * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only
1229 */
1230 if (!packet_connection_is_ipv4())
1231 return;
Damien Miller95def091999-11-25 00:26:21 +11001232 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001233 /*
1234 * Set IP options for an interactive connection. Use
1235 * IPTOS_LOWDELAY and TCP_NODELAY.
1236 */
Damien Miller95def091999-11-25 00:26:21 +11001237 int lowdelay = IPTOS_LOWDELAY;
1238 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay,
Damien Miller34132e52000-01-14 15:45:46 +11001239 sizeof(lowdelay)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001240 error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno));
1241 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001242 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001243 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1244 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001245 /*
1246 * Set IP options for a non-interactive connection. Use
1247 * IPTOS_THROUGHPUT.
1248 */
Damien Miller95def091999-11-25 00:26:21 +11001249 int throughput = IPTOS_THROUGHPUT;
1250 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001251 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001252 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
1253 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
1255
1256/* Returns true if the current connection is interactive. */
1257
1258int
1259packet_is_interactive()
1260{
Damien Miller95def091999-11-25 00:26:21 +11001261 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262}
Damien Miller6162d121999-11-21 13:23:52 +11001263
1264int
1265packet_set_maxsize(int s)
1266{
Damien Miller95def091999-11-25 00:26:21 +11001267 static int called = 0;
1268 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001269 log("packet_set_maxsize: called twice: old %d new %d",
1270 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001271 return -1;
1272 }
1273 if (s < 4 * 1024 || s > 1024 * 1024) {
1274 log("packet_set_maxsize: bad size %d", s);
1275 return -1;
1276 }
1277 log("packet_set_maxsize: setting to %d", s);
1278 max_packet_size = s;
1279 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001280}