blob: 3875d8f9be50326ddb7fa121a18fc3e39d5055bb [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 Miller33b13562000-04-04 14:38:59 +100020RCSID("$Id: packet.c,v 1.14 2000/04/04 04:39:03 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "xmalloc.h"
23#include "buffer.h"
24#include "packet.h"
25#include "bufaux.h"
26#include "ssh.h"
27#include "crc32.h"
28#include "cipher.h"
29#include "getput.h"
30
31#include "compress.h"
32#include "deattack.h"
Damien Millerb38eff82000-04-01 11:09:21 +100033#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
Damien Miller33b13562000-04-04 14:38:59 +100035#include "compat.h"
36#include "ssh2.h"
37
38#include <ssl/bn.h>
39#include <ssl/dh.h>
40#include <ssl/hmac.h>
41#include "buffer.h"
42#include "kex.h"
43#include "hmac.h"
44
45#ifdef PACKET_DEBUG
46#define DBG(x) x
47#else
48#define DBG(x)
49#endif
50
Damien Miller5428f641999-11-25 11:54:57 +110051/*
52 * This variable contains the file descriptors used for communicating with
53 * the other side. connection_in is used for reading; connection_out for
54 * writing. These can be the same descriptor, in which case it is assumed to
55 * be a socket.
56 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057static int connection_in = -1;
58static int connection_out = -1;
59
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Cipher type. This value is only used to determine whether to pad the
62 * packets with zeroes or random data.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064static int cipher_type = SSH_CIPHER_NONE;
65
66/* Protocol flags for the remote side. */
67static unsigned int remote_protocol_flags = 0;
68
69/* Encryption context for receiving data. This is only used for decryption. */
70static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110071
72/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073static CipherContext send_context;
74
75/* Buffer for raw input data from the socket. */
76static Buffer input;
77
78/* Buffer for raw output data going to the socket. */
79static Buffer output;
80
81/* Buffer for the partial outgoing packet being constructed. */
82static Buffer outgoing_packet;
83
84/* Buffer for the incoming packet currently being processed. */
85static Buffer incoming_packet;
86
87/* Scratch buffer for packet compression/decompression. */
88static Buffer compression_buffer;
89
90/* Flag indicating whether packet compression/decompression is enabled. */
91static int packet_compression = 0;
92
Damien Miller6162d121999-11-21 13:23:52 +110093/* default maximum packet size */
94int max_packet_size = 32768;
95
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096/* Flag indicating whether this module has been initialized. */
97static int initialized = 0;
98
99/* Set to true if the connection is interactive. */
100static int interactive_mode = 0;
101
Damien Miller33b13562000-04-04 14:38:59 +1000102/* True if SSH2 packet format is used */
103int use_ssh2_packet_format = 0;
104
105/* Session key information for Encryption and MAC */
106Kex *kex = NULL;
107
108void
109packet_set_kex(Kex *k)
110{
111 if( k->mac[MODE_IN ].key == NULL ||
112 k->enc[MODE_IN ].key == NULL ||
113 k->enc[MODE_IN ].iv == NULL ||
114 k->mac[MODE_OUT].key == NULL ||
115 k->enc[MODE_OUT].key == NULL ||
116 k->enc[MODE_OUT].iv == NULL)
117 fatal("bad KEX");
118 kex = k;
119}
120void
121clear_enc_keys(Enc *enc, int len)
122{
123 memset(enc->iv, 0, len);
124 memset(enc->key, 0, len);
125 xfree(enc->iv);
126 xfree(enc->key);
127 enc->iv = NULL;
128 enc->key = NULL;
129}
130void
131packet_set_ssh2_format(void)
132{
133 debug("use_ssh2_packet_format");
134 use_ssh2_packet_format = 1;
135}
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/*
138 * Sets the descriptors used for communication. Disables encryption until
139 * packet_set_encryption_key is called.
140 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141void
142packet_set_connection(int fd_in, int fd_out)
143{
Damien Miller95def091999-11-25 00:26:21 +1100144 connection_in = fd_in;
145 connection_out = fd_out;
146 cipher_type = SSH_CIPHER_NONE;
147 cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 1);
148 cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 0);
149 if (!initialized) {
150 initialized = 1;
151 buffer_init(&input);
152 buffer_init(&output);
153 buffer_init(&outgoing_packet);
154 buffer_init(&incoming_packet);
155 }
156 /* Kludge: arrange the close function to be called from fatal(). */
157 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158}
159
Damien Miller34132e52000-01-14 15:45:46 +1100160/* Returns 1 if remote host is connected via socket, 0 if not. */
161
162int
163packet_connection_is_on_socket()
164{
165 struct sockaddr_storage from, to;
166 socklen_t fromlen, tolen;
167
168 /* filedescriptors in and out are the same, so it's a socket */
169 if (connection_in == connection_out)
170 return 1;
171 fromlen = sizeof(from);
172 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100173 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100174 return 0;
175 tolen = sizeof(to);
176 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100177 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100178 return 0;
179 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
180 return 0;
181 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
182 return 0;
183 return 1;
184}
185
186/* returns 1 if connection is via ipv4 */
187
188int
189packet_connection_is_ipv4()
190{
191 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100192 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100193
194 memset(&to, 0, sizeof(to));
195 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
196 return 0;
197 if (to.ss_family != AF_INET)
198 return 0;
199 return 1;
200}
201
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202/* Sets the connection into non-blocking mode. */
203
204void
205packet_set_nonblocking()
206{
Damien Miller95def091999-11-25 00:26:21 +1100207 /* Set the socket into non-blocking mode. */
208 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
209 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Miller95def091999-11-25 00:26:21 +1100211 if (connection_out != connection_in) {
212 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
213 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
214 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
217/* Returns the socket used for reading. */
218
219int
220packet_get_connection_in()
221{
Damien Miller95def091999-11-25 00:26:21 +1100222 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223}
224
225/* Returns the descriptor used for writing. */
226
227int
228packet_get_connection_out()
229{
Damien Miller95def091999-11-25 00:26:21 +1100230 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
233/* Closes the connection and clears and frees internal data structures. */
234
235void
236packet_close()
237{
Damien Miller95def091999-11-25 00:26:21 +1100238 if (!initialized)
239 return;
240 initialized = 0;
241 if (connection_in == connection_out) {
242 shutdown(connection_out, SHUT_RDWR);
243 close(connection_out);
244 } else {
245 close(connection_in);
246 close(connection_out);
247 }
248 buffer_free(&input);
249 buffer_free(&output);
250 buffer_free(&outgoing_packet);
251 buffer_free(&incoming_packet);
252 if (packet_compression) {
253 buffer_free(&compression_buffer);
254 buffer_compress_uninit();
255 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256}
257
258/* Sets remote side protocol flags. */
259
260void
261packet_set_protocol_flags(unsigned int protocol_flags)
262{
Damien Miller95def091999-11-25 00:26:21 +1100263 remote_protocol_flags = protocol_flags;
264 channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265}
266
267/* Returns the remote protocol flags set earlier by the above function. */
268
269unsigned int
270packet_get_protocol_flags()
271{
Damien Miller95def091999-11-25 00:26:21 +1100272 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
Damien Miller5428f641999-11-25 11:54:57 +1100275/*
276 * Starts packet compression from the next packet on in both directions.
277 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
278 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller33b13562000-04-04 14:38:59 +1000280/*** XXXXX todo: kex means re-init */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281void
282packet_start_compression(int level)
283{
Damien Miller95def091999-11-25 00:26:21 +1100284 if (packet_compression)
285 fatal("Compression already enabled.");
286 packet_compression = 1;
287 buffer_init(&compression_buffer);
288 buffer_compress_init(level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289}
290
Damien Miller5428f641999-11-25 11:54:57 +1100291/*
292 * Encrypts the given number of bytes, copying from src to dest. bytes is
293 * known to be a multiple of 8.
294 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
296void
Damien Miller95def091999-11-25 00:26:21 +1100297packet_encrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000298 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299{
Damien Miller95def091999-11-25 00:26:21 +1100300 cipher_encrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Damien Miller5428f641999-11-25 11:54:57 +1100303/*
304 * Decrypts the given number of bytes, copying from src to dest. bytes is
305 * known to be a multiple of 8.
306 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
308void
Damien Miller95def091999-11-25 00:26:21 +1100309packet_decrypt(CipherContext * cc, void *dest, void *src,
Damien Miller33b13562000-04-04 14:38:59 +1000310 unsigned int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Damien Miller95def091999-11-25 00:26:21 +1100312 int i;
313
314 if ((bytes % 8) != 0)
315 fatal("packet_decrypt: bad ciphertext length %d", bytes);
316
Damien Miller5428f641999-11-25 11:54:57 +1100317 /*
318 * Cryptographic attack detector for ssh - Modifications for packet.c
319 * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
320 */
Damien Miller95def091999-11-25 00:26:21 +1100321
Damien Miller33b13562000-04-04 14:38:59 +1000322 if (cc->type == SSH_CIPHER_NONE || compat20) {
Damien Miller95def091999-11-25 00:26:21 +1100323 i = DEATTACK_OK;
Damien Miller33b13562000-04-04 14:38:59 +1000324 } else {
Damien Miller95def091999-11-25 00:26:21 +1100325 i = detect_attack(src, bytes, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100326 }
Damien Miller95def091999-11-25 00:26:21 +1100327 if (i == DEATTACK_DETECTED)
328 packet_disconnect("crc32 compensation attack: network attack detected");
329
330 cipher_decrypt(cc, dest, src, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
332
Damien Miller5428f641999-11-25 11:54:57 +1100333/*
334 * Causes any further packets to be encrypted using the given key. The same
335 * key is used for both sending and reception. However, both directions are
336 * encrypted independently of each other.
337 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
339void
340packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
Damien Miller33b13562000-04-04 14:38:59 +1000341 int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Damien Miller33b13562000-04-04 14:38:59 +1000343 if (keylen < 20)
344 fatal("keylen too small: %d", keylen);
345
Damien Miller95def091999-11-25 00:26:21 +1100346 /* All other ciphers use the same key in both directions for now. */
347 cipher_set_key(&receive_context, cipher, key, keylen, 0);
348 cipher_set_key(&send_context, cipher, key, keylen, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
351/* Starts constructing a packet to send. */
352
353void
Damien Miller33b13562000-04-04 14:38:59 +1000354packet_start1(int type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355{
Damien Miller95def091999-11-25 00:26:21 +1100356 char buf[9];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Miller95def091999-11-25 00:26:21 +1100358 buffer_clear(&outgoing_packet);
359 memset(buf, 0, 8);
360 buf[8] = type;
361 buffer_append(&outgoing_packet, buf, 9);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362}
363
Damien Miller33b13562000-04-04 14:38:59 +1000364void
365packet_start2(int type)
366{
367 char buf[4+1+1];
368
369 buffer_clear(&outgoing_packet);
370 memset(buf, 0, sizeof buf);
371 /* buf[0..3] = payload_len; */
372 /* buf[4] = pad_len; */
373 buf[5] = type & 0xff;
374 buffer_append(&outgoing_packet, buf, sizeof buf);
375}
376
377void
378packet_start(int type)
379{
380 DBG(debug("packet_start[%d]",type));
381 if (use_ssh2_packet_format)
382 packet_start2(type);
383 else
384 packet_start1(type);
385}
386
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387/* Appends a character to the packet data. */
388
389void
390packet_put_char(int value)
391{
Damien Miller95def091999-11-25 00:26:21 +1100392 char ch = value;
393 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394}
395
396/* Appends an integer to the packet data. */
397
398void
399packet_put_int(unsigned int value)
400{
Damien Miller95def091999-11-25 00:26:21 +1100401 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
404/* Appends a string to packet data. */
405
406void
407packet_put_string(const char *buf, unsigned int len)
408{
Damien Miller95def091999-11-25 00:26:21 +1100409 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410}
Damien Miller33b13562000-04-04 14:38:59 +1000411void
412packet_put_cstring(const char *str)
413{
414 buffer_put_string(&outgoing_packet, str, strlen(str));
415}
416
417void
418packet_put_raw(const char *buf, unsigned int len)
419{
420 buffer_append(&outgoing_packet, buf, len);
421}
422
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
424/* Appends an arbitrary precision integer to packet data. */
425
426void
Damien Miller95def091999-11-25 00:26:21 +1100427packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428{
Damien Miller95def091999-11-25 00:26:21 +1100429 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}
Damien Miller33b13562000-04-04 14:38:59 +1000431void
432packet_put_bignum2(BIGNUM * value)
433{
434 buffer_put_bignum2(&outgoing_packet, value);
435}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Miller5428f641999-11-25 11:54:57 +1100437/*
438 * Finalizes and sends the packet. If the encryption key has been set,
439 * encrypts the packet before sending.
440 */
Damien Miller95def091999-11-25 00:26:21 +1100441
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442void
Damien Miller33b13562000-04-04 14:38:59 +1000443packet_send1()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444{
Damien Miller95def091999-11-25 00:26:21 +1100445 char buf[8], *cp;
446 int i, padding, len;
447 unsigned int checksum;
448 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Damien Miller5428f641999-11-25 11:54:57 +1100450 /*
451 * If using packet compression, compress the payload of the outgoing
452 * packet.
453 */
Damien Miller95def091999-11-25 00:26:21 +1100454 if (packet_compression) {
455 buffer_clear(&compression_buffer);
456 /* Skip padding. */
457 buffer_consume(&outgoing_packet, 8);
458 /* padding */
459 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
460 buffer_compress(&outgoing_packet, &compression_buffer);
461 buffer_clear(&outgoing_packet);
462 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
463 buffer_len(&compression_buffer));
464 }
465 /* Compute packet length without padding (add checksum, remove padding). */
466 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller95def091999-11-25 00:26:21 +1100468 /* Insert padding. */
469 padding = 8 - len % 8;
470 if (cipher_type != SSH_CIPHER_NONE) {
471 cp = buffer_ptr(&outgoing_packet);
472 for (i = 0; i < padding; i++) {
473 if (i % 4 == 0)
474 rand = arc4random();
475 cp[7 - i] = rand & 0xff;
476 rand >>= 8;
477 }
478 }
479 buffer_consume(&outgoing_packet, 8 - padding);
480
481 /* Add check bytes. */
482 checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet),
483 buffer_len(&outgoing_packet));
484 PUT_32BIT(buf, checksum);
485 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
487#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100488 fprintf(stderr, "packet_send plain: ");
489 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490#endif
491
Damien Miller95def091999-11-25 00:26:21 +1100492 /* Append to output. */
493 PUT_32BIT(buf, len);
494 buffer_append(&output, buf, 4);
495 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
496 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
497 buffer_len(&outgoing_packet));
498
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100500 fprintf(stderr, "encrypted: ");
501 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502#endif
503
Damien Miller95def091999-11-25 00:26:21 +1100504 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller5428f641999-11-25 11:54:57 +1100506 /*
507 * Note that the packet is now only buffered in output. It won\'t be
508 * actually sent until packet_write_wait or packet_write_poll is
509 * called.
510 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511}
512
Damien Miller5428f641999-11-25 11:54:57 +1100513/*
Damien Miller33b13562000-04-04 14:38:59 +1000514 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
515 */
516void
517packet_send2()
518{
519 unsigned char *macbuf = NULL;
520 char *cp;
521 unsigned int packet_length = 0;
522 unsigned int i, padlen, len;
523 u_int32_t rand = 0;
524 static unsigned int seqnr = 0;
525 int type;
526 Enc *enc = NULL;
527 Mac *mac = NULL;
528 Comp *comp = NULL;
529 int block_size;
530
531 if (kex != NULL) {
532 enc = &kex->enc[MODE_OUT];
533 mac = &kex->mac[MODE_OUT];
534 comp = &kex->comp[MODE_OUT];
535 }
536 block_size = enc ? enc->block_size : 8;
537
538 cp = buffer_ptr(&outgoing_packet);
539 type = cp[5] & 0xff;
540
541#ifdef PACKET_DEBUG
542 fprintf(stderr, "plain: ");
543 buffer_dump(&outgoing_packet);
544#endif
545
546 if (comp && comp->enabled) {
547 len = buffer_len(&outgoing_packet);
548 /* skip header, compress only payload */
549 buffer_consume(&outgoing_packet, 5);
550 buffer_clear(&compression_buffer);
551 buffer_compress(&outgoing_packet, &compression_buffer);
552 buffer_clear(&outgoing_packet);
553 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
554 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
555 buffer_len(&compression_buffer));
556 DBG(debug("compression: raw %d compressed %d", len,
557 buffer_len(&outgoing_packet)));
558 }
559
560 /* sizeof (packet_len + pad_len + payload) */
561 len = buffer_len(&outgoing_packet);
562
563 /*
564 * calc size of padding, alloc space, get random data,
565 * minimum padding is 4 bytes
566 */
567 padlen = block_size - (len % block_size);
568 if (padlen < 4)
569 padlen += block_size;
570 buffer_append_space(&outgoing_packet, &cp, padlen);
571 if (enc && enc->type != SSH_CIPHER_NONE) {
572 for (i = 0; i < padlen; i++) {
573 if (i % 4 == 0)
574 rand = arc4random();
575 cp[i] = rand & 0xff;
576 rand <<= 8;
577 }
578 }
579 /* packet_length includes payload, padding and padding length field */
580 packet_length = buffer_len(&outgoing_packet) - 4;
581 cp = buffer_ptr(&outgoing_packet);
582 PUT_32BIT(cp, packet_length);
583 cp[4] = padlen & 0xff;
584 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
585
586 /* compute MAC over seqnr and packet(length fields, payload, padding) */
587 if (mac && mac->enabled) {
588 macbuf = hmac( mac->md, seqnr,
589 (unsigned char *) buffer_ptr(&outgoing_packet),
590 buffer_len(&outgoing_packet),
591 mac->key, mac->key_len
592 );
593 DBG(debug("done calc HMAC out #%d", seqnr));
594 }
595 /* encrypt packet and append to output buffer. */
596 buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
597 packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
598 buffer_len(&outgoing_packet));
599 /* append unencrypted MAC */
600 if (mac && mac->enabled)
601 buffer_append(&output, (char *)macbuf, mac->mac_len);
602#ifdef PACKET_DEBUG
603 fprintf(stderr, "encrypted: ");
604 buffer_dump(&output);
605#endif
606 /* increment sequence number for outgoing packets */
607 if (++seqnr == 0)
608 log("outgoing seqnr wraps around");
609 buffer_clear(&outgoing_packet);
610
611 if (type == SSH2_MSG_NEWKEYS) {
612 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
613 fatal("packet_send2: no KEX");
614 if (mac->md != NULL)
615 mac->enabled = 1;
616 debug("cipher_set_key_iv send_context");
617 cipher_set_key_iv(&send_context, enc->type,
618 enc->key, enc->key_len,
619 enc->iv, enc->iv_len);
620 clear_enc_keys(enc, kex->we_need);
621 if (comp->type != 0 && comp->enabled == 0) {
622 comp->enabled = 1;
623 if (! packet_compression)
624 packet_start_compression(6);
625 }
626 }
627}
628
629void
630packet_send()
631{
632 if (use_ssh2_packet_format)
633 packet_send2();
634 else
635 packet_send1();
636 DBG(debug("packet_send done"));
637}
638
639void
640packet_send_and_wait()
641{
642 packet_send();
643 packet_write_wait();
644}
645
646/*
Damien Miller5428f641999-11-25 11:54:57 +1100647 * Waits until a packet has been received, and returns its type. Note that
648 * no other data is processed until this returns, so this function should not
649 * be used during the interactive session.
650 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651
652int
653packet_read(int *payload_len_ptr)
654{
Damien Miller95def091999-11-25 00:26:21 +1100655 int type, len;
656 fd_set set;
657 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000658 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659
Damien Miller95def091999-11-25 00:26:21 +1100660 /* Since we are blocking, ensure that all written packets have been sent. */
661 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Stay in the loop until we have received a complete packet. */
664 for (;;) {
665 /* Try to read a packet from the buffer. */
666 type = packet_read_poll(payload_len_ptr);
667 if (type == SSH_SMSG_SUCCESS
668 || type == SSH_SMSG_FAILURE
669 || type == SSH_CMSG_EOF
670 || type == SSH_CMSG_EXIT_CONFIRMATION)
671 packet_integrity_check(*payload_len_ptr, 0, type);
672 /* If we got a packet, return it. */
673 if (type != SSH_MSG_NONE)
674 return type;
Damien Miller5428f641999-11-25 11:54:57 +1100675 /*
676 * Otherwise, wait for some data to arrive, add it to the
677 * buffer, and try again.
678 */
Damien Miller95def091999-11-25 00:26:21 +1100679 FD_ZERO(&set);
680 FD_SET(connection_in, &set);
Damien Miller5428f641999-11-25 11:54:57 +1100681
Damien Miller95def091999-11-25 00:26:21 +1100682 /* Wait for some data to arrive. */
683 select(connection_in + 1, &set, NULL, NULL, NULL);
Damien Miller5428f641999-11-25 11:54:57 +1100684
Damien Miller95def091999-11-25 00:26:21 +1100685 /* Read data from the socket. */
686 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100687 if (len == 0) {
688 log("Connection closed by %.200s", get_remote_ipaddr());
689 fatal_cleanup();
690 }
Damien Miller95def091999-11-25 00:26:21 +1100691 if (len < 0)
692 fatal("Read from socket failed: %.100s", strerror(errno));
693 /* Append it to the buffer. */
694 packet_process_incoming(buf, len);
695 }
696 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697}
698
Damien Miller5428f641999-11-25 11:54:57 +1100699/*
700 * Waits until a packet has been received, verifies that its type matches
701 * that given, and gives a fatal error and exits if there is a mismatch.
702 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703
704void
705packet_read_expect(int *payload_len_ptr, int expected_type)
706{
Damien Miller95def091999-11-25 00:26:21 +1100707 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Damien Miller95def091999-11-25 00:26:21 +1100709 type = packet_read(payload_len_ptr);
710 if (type != expected_type)
711 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000712 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713}
714
715/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100716 * packet_process_incoming. If so, reads the packet; otherwise returns
717 * SSH_MSG_NONE. This does not wait for data from the connection.
718 *
719 * SSH_MSG_DISCONNECT is handled specially here. Also,
720 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
721 * to higher levels.
722 *
723 * The returned payload_len does include space consumed by:
724 * Packet length
725 * Padding
726 * Packet type
727 * Check bytes
728 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
730int
Damien Miller33b13562000-04-04 14:38:59 +1000731packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732{
Damien Miller95def091999-11-25 00:26:21 +1100733 unsigned int len, padded_len;
734 unsigned char *ucp;
Damien Miller33b13562000-04-04 14:38:59 +1000735 char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100736 unsigned int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller95def091999-11-25 00:26:21 +1100738 /* Check if input size is less than minimum packet size. */
739 if (buffer_len(&input) < 4 + 8)
740 return SSH_MSG_NONE;
741 /* Get length of incoming packet. */
742 ucp = (unsigned char *) buffer_ptr(&input);
743 len = GET_32BIT(ucp);
744 if (len < 1 + 2 + 2 || len > 256 * 1024)
745 packet_disconnect("Bad packet length %d.", len);
746 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Damien Miller95def091999-11-25 00:26:21 +1100748 /* Check if the packet has been entirely received. */
749 if (buffer_len(&input) < 4 + padded_len)
750 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Miller95def091999-11-25 00:26:21 +1100752 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 /* Consume packet length. */
755 buffer_consume(&input, 4);
756
757 /* Copy data to incoming_packet. */
758 buffer_clear(&incoming_packet);
759 buffer_append_space(&incoming_packet, &cp, padded_len);
760 packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
761 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762
763#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100764 fprintf(stderr, "read_poll plain: ");
765 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller95def091999-11-25 00:26:21 +1100768 /* Compute packet checksum. */
769 checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000770 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771
Damien Miller95def091999-11-25 00:26:21 +1100772 /* Skip padding. */
773 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100774
Damien Miller95def091999-11-25 00:26:21 +1100775 /* Test check bytes. */
Damien Millerfd7c9111999-11-08 16:15:55 +1100776
Damien Miller95def091999-11-25 00:26:21 +1100777 if (len != buffer_len(&incoming_packet))
778 packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000779 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Damien Miller95def091999-11-25 00:26:21 +1100781 ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4;
782 stored_checksum = GET_32BIT(ucp);
783 if (checksum != stored_checksum)
784 packet_disconnect("Corrupted check bytes on input.");
785 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
Damien Miller95def091999-11-25 00:26:21 +1100787 /* If using packet compression, decompress the packet. */
788 if (packet_compression) {
789 buffer_clear(&compression_buffer);
790 buffer_uncompress(&incoming_packet, &compression_buffer);
791 buffer_clear(&incoming_packet);
792 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000793 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100794 }
795 /* Get packet type. */
796 buffer_get(&incoming_packet, &buf[0], 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797
Damien Miller95def091999-11-25 00:26:21 +1100798 /* Return length of payload (without type field). */
799 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800
Damien Miller95def091999-11-25 00:26:21 +1100801 /* Return type. */
802 return (unsigned char) buf[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
Damien Miller95def091999-11-25 00:26:21 +1100804
Damien Miller33b13562000-04-04 14:38:59 +1000805int
806packet_read_poll2(int *payload_len_ptr)
807{
808 unsigned int padlen, need;
809 unsigned char buf[8], *macbuf;
810 unsigned char *ucp;
811 char *cp;
812 static unsigned int packet_length = 0;
813 static unsigned int seqnr = 0;
814 int type;
815 int maclen, block_size;
816 Enc *enc = NULL;
817 Mac *mac = NULL;
818 Comp *comp = NULL;
819
820 if (kex != NULL) {
821 enc = &kex->enc[MODE_IN];
822 mac = &kex->mac[MODE_IN];
823 comp = &kex->comp[MODE_IN];
824 }
825 maclen = mac && mac->enabled ? mac->mac_len : 0;
826 block_size = enc ? enc->block_size : 8;
827
828 if (packet_length == 0) {
829 /*
830 * check if input size is less than the cipher block size,
831 * decrypt first block and extract length of incoming packet
832 */
833 if (buffer_len(&input) < block_size)
834 return SSH_MSG_NONE;
835 buffer_clear(&incoming_packet);
836 buffer_append_space(&incoming_packet, &cp, block_size);
837 packet_decrypt(&receive_context, cp, buffer_ptr(&input),
838 block_size);
839 ucp = (unsigned char *) buffer_ptr(&incoming_packet);
840 packet_length = GET_32BIT(ucp);
841 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
842 buffer_dump(&incoming_packet);
843 packet_disconnect("Bad packet length %d.", packet_length);
844 }
845 DBG(debug("input: packet len %d", packet_length+4));
846 buffer_consume(&input, block_size);
847 }
848 /* we have a partial packet of block_size bytes */
849 need = 4 + packet_length - block_size;
850 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
851 need, maclen));
852 if (need % block_size != 0)
853 fatal("padding error: need %d block %d mod %d",
854 need, block_size, need % block_size);
855 /*
856 * check if the entire packet has been received and
857 * decrypt into incoming_packet
858 */
859 if (buffer_len(&input) < need + maclen)
860 return SSH_MSG_NONE;
861#ifdef PACKET_DEBUG
862 fprintf(stderr, "read_poll enc/full: ");
863 buffer_dump(&input);
864#endif
865 buffer_append_space(&incoming_packet, &cp, need);
866 packet_decrypt(&receive_context, cp, buffer_ptr(&input), need);
867 buffer_consume(&input, need);
868 /*
869 * compute MAC over seqnr and packet,
870 * increment sequence number for incoming packet
871 */
872 if (mac && mac->enabled) {
873 macbuf = hmac( mac->md, seqnr,
874 (unsigned char *) buffer_ptr(&incoming_packet),
875 buffer_len(&incoming_packet),
876 mac->key, mac->key_len
877 );
878 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
879 packet_disconnect("Corrupted HMAC on input.");
880 DBG(debug("HMAC #%d ok", seqnr));
881 buffer_consume(&input, mac->mac_len);
882 }
883 if (++seqnr == 0)
884 log("incoming seqnr wraps around");
885
886 /* get padlen */
887 cp = buffer_ptr(&incoming_packet) + 4;
888 padlen = *cp & 0xff;
889 DBG(debug("input: padlen %d", padlen));
890 if (padlen < 4)
891 packet_disconnect("Corrupted padlen %d on input.", padlen);
892
893 /* skip packet size + padlen, discard padding */
894 buffer_consume(&incoming_packet, 4 + 1);
895 buffer_consume_end(&incoming_packet, padlen);
896
897 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
898 if (comp && comp->enabled) {
899 buffer_clear(&compression_buffer);
900 buffer_uncompress(&incoming_packet, &compression_buffer);
901 buffer_clear(&incoming_packet);
902 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
903 buffer_len(&compression_buffer));
904 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
905 }
906 /*
907 * get packet type, implies consume.
908 * return length of payload (without type field)
909 */
910 buffer_get(&incoming_packet, (char *)&buf[0], 1);
911 *payload_len_ptr = buffer_len(&incoming_packet);
912
913 /* reset for next packet */
914 packet_length = 0;
915
916 /* extract packet type */
917 type = (unsigned char)buf[0];
918
919 if (type == SSH2_MSG_NEWKEYS) {
920 if (kex==NULL || mac==NULL || enc==NULL || comp==NULL)
921 fatal("packet_read_poll2: no KEX");
922 if (mac->md != NULL)
923 mac->enabled = 1;
924 debug("cipher_set_key_iv receive_context");
925 cipher_set_key_iv(&receive_context, enc->type,
926 enc->key, enc->key_len,
927 enc->iv, enc->iv_len);
928 clear_enc_keys(enc, kex->we_need);
929 if (comp->type != 0 && comp->enabled == 0) {
930 comp->enabled = 1;
931 if (! packet_compression)
932 packet_start_compression(6);
933 }
934 }
935
936#ifdef PACKET_DEBUG
937 fprintf(stderr, "read/plain[%d]:\r\n",type);
938 buffer_dump(&incoming_packet);
939#endif
940 return (unsigned char)type;
941}
942
943int
944packet_read_poll(int *payload_len_ptr)
945{
946 char *msg;
947 for (;;) {
948 int type = use_ssh2_packet_format ?
949 packet_read_poll2(payload_len_ptr):
950 packet_read_poll1(payload_len_ptr);
951
952 if(compat20) {
953 int reason;
954 if (type != 0)
955 DBG(debug("received packet type %d", type));
956 switch(type) {
957 case SSH2_MSG_IGNORE:
958 break;
959 case SSH2_MSG_DEBUG:
960 packet_get_char();
961 msg = packet_get_string(NULL);
962 debug("Remote: %.900s", msg);
963 xfree(msg);
964 msg = packet_get_string(NULL);
965 xfree(msg);
966 break;
967 case SSH2_MSG_DISCONNECT:
968 reason = packet_get_int();
969 msg = packet_get_string(NULL);
970 log("Received disconnect: %d: %.900s", reason, msg);
971 xfree(msg);
972 fatal_cleanup();
973 break;
974 default:
975 return type;
976 break;
977 }
978 } else {
979 switch(type) {
980 case SSH_MSG_IGNORE:
981 break;
982 case SSH_MSG_DEBUG:
983 msg = packet_get_string(NULL);
984 debug("Remote: %.900s", msg);
985 xfree(msg);
986 break;
987 case SSH_MSG_DISCONNECT:
988 msg = packet_get_string(NULL);
989 log("Received disconnect: %.900s", msg);
990 fatal_cleanup();
991 xfree(msg);
992 break;
993 default:
994 if (type != 0)
995 DBG(debug("received packet type %d", type));
996 return type;
997 break;
998 }
999 }
1000 }
1001}
1002
Damien Miller5428f641999-11-25 11:54:57 +11001003/*
1004 * Buffers the given amount of input characters. This is intended to be used
1005 * together with packet_read_poll.
1006 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007
1008void
1009packet_process_incoming(const char *buf, unsigned int len)
1010{
Damien Miller95def091999-11-25 00:26:21 +11001011 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012}
1013
1014/* Returns a character from the packet. */
1015
1016unsigned int
1017packet_get_char()
1018{
Damien Miller95def091999-11-25 00:26:21 +11001019 char ch;
1020 buffer_get(&incoming_packet, &ch, 1);
1021 return (unsigned char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022}
1023
1024/* Returns an integer from the packet data. */
1025
1026unsigned int
1027packet_get_int()
1028{
Damien Miller95def091999-11-25 00:26:21 +11001029 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030}
1031
Damien Miller5428f641999-11-25 11:54:57 +11001032/*
1033 * Returns an arbitrary precision integer from the packet data. The integer
1034 * must have been initialized before this call.
1035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
1037void
Damien Miller95def091999-11-25 00:26:21 +11001038packet_get_bignum(BIGNUM * value, int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039{
Damien Miller95def091999-11-25 00:26:21 +11001040 *length_ptr = buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041}
1042
Damien Miller33b13562000-04-04 14:38:59 +10001043void
1044packet_get_bignum2(BIGNUM * value, int *length_ptr)
1045{
1046 *length_ptr = buffer_get_bignum2(&incoming_packet, value);
1047}
1048
1049char *
1050packet_get_raw(int *length_ptr)
1051{
1052 int bytes = buffer_len(&incoming_packet);
1053 if (length_ptr != NULL)
1054 *length_ptr = bytes;
1055 return buffer_ptr(&incoming_packet);
1056}
1057
Damien Miller5428f641999-11-25 11:54:57 +11001058/*
1059 * Returns a string from the packet data. The string is allocated using
1060 * xmalloc; it is the responsibility of the calling program to free it when
1061 * no longer needed. The length_ptr argument may be NULL, or point to an
1062 * integer into which the length of the string is stored.
1063 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064
Damien Miller5428f641999-11-25 11:54:57 +11001065char *
Damien Miller95def091999-11-25 00:26:21 +11001066packet_get_string(unsigned int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067{
Damien Miller95def091999-11-25 00:26:21 +11001068 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069}
1070
Damien Miller5428f641999-11-25 11:54:57 +11001071/*
1072 * Sends a diagnostic message from the server to the client. This message
1073 * can be sent at any time (but not while constructing another message). The
1074 * message is printed immediately, but only if the client is being executed
1075 * in verbose mode. These messages are primarily intended to ease debugging
1076 * authentication problems. The length of the formatted message must not
1077 * exceed 1024 bytes. This will automatically call packet_write_wait.
1078 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079
1080void
Damien Miller95def091999-11-25 00:26:21 +11001081packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082{
Damien Miller95def091999-11-25 00:26:21 +11001083 char buf[1024];
1084 va_list args;
1085
1086 va_start(args, fmt);
1087 vsnprintf(buf, sizeof(buf), fmt, args);
1088 va_end(args);
1089
1090 packet_start(SSH_MSG_DEBUG);
1091 packet_put_string(buf, strlen(buf));
1092 packet_send();
1093 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094}
1095
Damien Miller5428f641999-11-25 11:54:57 +11001096/*
1097 * Logs the error plus constructs and sends a disconnect packet, closes the
1098 * connection, and exits. This function never returns. The error message
1099 * should not contain a newline. The length of the formatted message must
1100 * not exceed 1024 bytes.
1101 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
1103void
Damien Miller95def091999-11-25 00:26:21 +11001104packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105{
Damien Miller95def091999-11-25 00:26:21 +11001106 char buf[1024];
1107 va_list args;
1108 static int disconnecting = 0;
1109 if (disconnecting) /* Guard against recursive invocations. */
1110 fatal("packet_disconnect called recursively.");
1111 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller5428f641999-11-25 11:54:57 +11001113 /*
1114 * Format the message. Note that the caller must make sure the
1115 * message is of limited size.
1116 */
Damien Miller95def091999-11-25 00:26:21 +11001117 va_start(args, fmt);
1118 vsnprintf(buf, sizeof(buf), fmt, args);
1119 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120
Damien Miller95def091999-11-25 00:26:21 +11001121 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001122 if (compat20) {
1123 packet_start(SSH2_MSG_DISCONNECT);
1124 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1125 packet_put_cstring(buf);
1126 packet_put_cstring("");
1127 } else {
1128 packet_start(SSH_MSG_DISCONNECT);
1129 packet_put_string(buf, strlen(buf));
1130 }
Damien Miller95def091999-11-25 00:26:21 +11001131 packet_send();
1132 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133
Damien Miller95def091999-11-25 00:26:21 +11001134 /* Stop listening for connections. */
1135 channel_stop_listening();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
Damien Miller95def091999-11-25 00:26:21 +11001137 /* Close the connection. */
1138 packet_close();
1139
1140 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001141 log("Disconnecting: %.100s", buf);
1142 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143}
1144
Damien Miller5428f641999-11-25 11:54:57 +11001145/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146
1147void
1148packet_write_poll()
1149{
Damien Miller95def091999-11-25 00:26:21 +11001150 int len = buffer_len(&output);
1151 if (len > 0) {
1152 len = write(connection_out, buffer_ptr(&output), len);
1153 if (len <= 0) {
1154 if (errno == EAGAIN)
1155 return;
1156 else
1157 fatal("Write failed: %.100s", strerror(errno));
1158 }
1159 buffer_consume(&output, len);
1160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161}
1162
Damien Miller5428f641999-11-25 11:54:57 +11001163/*
1164 * Calls packet_write_poll repeatedly until all pending output data has been
1165 * written.
1166 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167
1168void
1169packet_write_wait()
1170{
Damien Miller95def091999-11-25 00:26:21 +11001171 packet_write_poll();
1172 while (packet_have_data_to_write()) {
1173 fd_set set;
1174 FD_ZERO(&set);
1175 FD_SET(connection_out, &set);
1176 select(connection_out + 1, NULL, &set, NULL, NULL);
1177 packet_write_poll();
1178 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179}
1180
1181/* Returns true if there is buffered data to write to the connection. */
1182
1183int
1184packet_have_data_to_write()
1185{
Damien Miller95def091999-11-25 00:26:21 +11001186 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187}
1188
1189/* Returns true if there is not too much data to write to the connection. */
1190
1191int
1192packet_not_very_much_data_to_write()
1193{
Damien Miller95def091999-11-25 00:26:21 +11001194 if (interactive_mode)
1195 return buffer_len(&output) < 16384;
1196 else
1197 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198}
1199
1200/* Informs that the current session is interactive. Sets IP flags for that. */
1201
1202void
1203packet_set_interactive(int interactive, int keepalives)
1204{
Damien Miller95def091999-11-25 00:26:21 +11001205 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
Damien Miller95def091999-11-25 00:26:21 +11001207 /* Record that we are in interactive mode. */
1208 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
Damien Miller34132e52000-01-14 15:45:46 +11001210 /* Only set socket options if using a socket. */
1211 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001212 return;
Damien Miller95def091999-11-25 00:26:21 +11001213 if (keepalives) {
1214 /* Set keepalives if requested. */
1215 if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001216 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001217 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1218 }
Damien Miller34132e52000-01-14 15:45:46 +11001219 /*
1220 * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only
1221 */
1222 if (!packet_connection_is_ipv4())
1223 return;
Damien Miller95def091999-11-25 00:26:21 +11001224 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001225 /*
1226 * Set IP options for an interactive connection. Use
1227 * IPTOS_LOWDELAY and TCP_NODELAY.
1228 */
Damien Miller95def091999-11-25 00:26:21 +11001229 int lowdelay = IPTOS_LOWDELAY;
1230 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay,
Damien Miller34132e52000-01-14 15:45:46 +11001231 sizeof(lowdelay)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001232 error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno));
1233 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001234 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001235 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1236 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001237 /*
1238 * Set IP options for a non-interactive connection. Use
1239 * IPTOS_THROUGHPUT.
1240 */
Damien Miller95def091999-11-25 00:26:21 +11001241 int throughput = IPTOS_THROUGHPUT;
1242 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001243 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001244 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
1245 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246}
1247
1248/* Returns true if the current connection is interactive. */
1249
1250int
1251packet_is_interactive()
1252{
Damien Miller95def091999-11-25 00:26:21 +11001253 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
Damien Miller6162d121999-11-21 13:23:52 +11001255
1256int
1257packet_set_maxsize(int s)
1258{
Damien Miller95def091999-11-25 00:26:21 +11001259 static int called = 0;
1260 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001261 log("packet_set_maxsize: called twice: old %d new %d",
1262 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001263 return -1;
1264 }
1265 if (s < 4 * 1024 || s > 1024 * 1024) {
1266 log("packet_set_maxsize: bad size %d", s);
1267 return -1;
1268 }
1269 log("packet_set_maxsize: setting to %d", s);
1270 max_packet_size = s;
1271 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001272}