]> andersk Git - openssh.git/blame - packet.c
- deraadt@cvs.openbsd.org 2002/06/19 00:27:55
[openssh.git] / packet.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * This file contains code implementing the packet protocol and communication
6 * with the other side. This same code is used both on client and server side.
7e7327a1 7 *
bcbf86ec 8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
7e7327a1 15 * SSH2 packet format added by Markus Friedl.
a96070d4 16 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
7e7327a1 26 *
bcbf86ec 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 37 */
8efc0c15 38
39#include "includes.h"
91647fe6 40RCSID("$OpenBSD: packet.c,v 1.94 2002/06/04 23:02:06 markus Exp $");
8efc0c15 41
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
8efc0c15 46#include "crc32.h"
8efc0c15 47#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
a5efa1bb 51#include "channels.h"
8efc0c15 52
7e7327a1 53#include "compat.h"
42f11eb2 54#include "ssh1.h"
7e7327a1 55#include "ssh2.h"
56
94ec8c6b 57#include "cipher.h"
7e7327a1 58#include "kex.h"
b2552997 59#include "mac.h"
42f11eb2 60#include "log.h"
61#include "canohost.h"
2ac91be1 62#include "misc.h"
7e7327a1 63
64#ifdef PACKET_DEBUG
65#define DBG(x) x
66#else
67#define DBG(x)
68#endif
69
aa3378df 70/*
71 * This variable contains the file descriptors used for communicating with
72 * the other side. connection_in is used for reading; connection_out for
73 * writing. These can be the same descriptor, in which case it is assumed to
74 * be a socket.
75 */
8efc0c15 76static int connection_in = -1;
77static int connection_out = -1;
78
8efc0c15 79/* Protocol flags for the remote side. */
1e3b8b07 80static u_int remote_protocol_flags = 0;
8efc0c15 81
82/* Encryption context for receiving data. This is only used for decryption. */
83static CipherContext receive_context;
5260325f 84
85/* Encryption context for sending data. This is only used for encryption. */
8efc0c15 86static CipherContext send_context;
87
88/* Buffer for raw input data from the socket. */
e050d348 89Buffer input;
8efc0c15 90
91/* Buffer for raw output data going to the socket. */
e050d348 92Buffer output;
8efc0c15 93
94/* Buffer for the partial outgoing packet being constructed. */
95static Buffer outgoing_packet;
96
97/* Buffer for the incoming packet currently being processed. */
98static Buffer incoming_packet;
99
100/* Scratch buffer for packet compression/decompression. */
101static Buffer compression_buffer;
6ba22c93 102static int compression_buffer_ready = 0;
8efc0c15 103
104/* Flag indicating whether packet compression/decompression is enabled. */
105static int packet_compression = 0;
106
9d6b7add 107/* default maximum packet size */
108int max_packet_size = 32768;
109
8efc0c15 110/* Flag indicating whether this module has been initialized. */
111static int initialized = 0;
112
113/* Set to true if the connection is interactive. */
114static int interactive_mode = 0;
115
7e7327a1 116/* Session key information for Encryption and MAC */
d1ac6175 117Newkeys *newkeys[MODE_MAX];
e050d348 118static u_int32_t read_seqnr = 0;
119static u_int32_t send_seqnr = 0;
7e7327a1 120
b4b701be 121/* roundup current message to extra_pad bytes */
122static u_char extra_pad = 0;
123
aa3378df 124/*
125 * Sets the descriptors used for communication. Disables encryption until
126 * packet_set_encryption_key is called.
127 */
8efc0c15 128void
129packet_set_connection(int fd_in, int fd_out)
130{
94ec8c6b 131 Cipher *none = cipher_by_name("none");
132 if (none == NULL)
133 fatal("packet_set_connection: cannot load cipher 'none'");
5260325f 134 connection_in = fd_in;
135 connection_out = fd_out;
3ee832e5 136 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
137 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
08dcb5d7 138 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
5260325f 139 if (!initialized) {
140 initialized = 1;
141 buffer_init(&input);
142 buffer_init(&output);
143 buffer_init(&outgoing_packet);
144 buffer_init(&incoming_packet);
145 }
146 /* Kludge: arrange the close function to be called from fatal(). */
147 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
8efc0c15 148}
149
48e671d5 150/* Returns 1 if remote host is connected via socket, 0 if not. */
151
152int
d5bb9418 153packet_connection_is_on_socket(void)
48e671d5 154{
155 struct sockaddr_storage from, to;
156 socklen_t fromlen, tolen;
157
158 /* filedescriptors in and out are the same, so it's a socket */
159 if (connection_in == connection_out)
160 return 1;
161 fromlen = sizeof(from);
162 memset(&from, 0, sizeof(from));
9bc5ddfe 163 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
48e671d5 164 return 0;
165 tolen = sizeof(to);
166 memset(&to, 0, sizeof(to));
9bc5ddfe 167 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
48e671d5 168 return 0;
169 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
170 return 0;
171 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
172 return 0;
173 return 1;
174}
175
762715ce 176/*
e050d348 177 * Exports an IV from the CipherContext required to export the key
178 * state back from the unprivileged child to the privileged parent
179 * process.
180 */
181
182void
183packet_get_keyiv(int mode, u_char *iv, u_int len)
184{
185 CipherContext *cc;
186
187 if (mode == MODE_OUT)
188 cc = &send_context;
189 else
190 cc = &receive_context;
191
192 cipher_get_keyiv(cc, iv, len);
193}
194
195int
196packet_get_keycontext(int mode, u_char *dat)
197{
198 CipherContext *cc;
762715ce 199
e050d348 200 if (mode == MODE_OUT)
201 cc = &send_context;
202 else
203 cc = &receive_context;
204
205 return (cipher_get_keycontext(cc, dat));
206}
207
208void
209packet_set_keycontext(int mode, u_char *dat)
210{
211 CipherContext *cc;
762715ce 212
e050d348 213 if (mode == MODE_OUT)
214 cc = &send_context;
215 else
216 cc = &receive_context;
217
218 cipher_set_keycontext(cc, dat);
219}
220
221int
222packet_get_keyiv_len(int mode)
223{
224 CipherContext *cc;
225
226 if (mode == MODE_OUT)
227 cc = &send_context;
228 else
229 cc = &receive_context;
230
231 return (cipher_get_keyiv_len(cc));
232}
233void
234packet_set_iv(int mode, u_char *dat)
235{
236 CipherContext *cc;
237
238 if (mode == MODE_OUT)
239 cc = &send_context;
240 else
241 cc = &receive_context;
242
243 cipher_set_keyiv(cc, dat);
244}
245int
246packet_get_ssh1_cipher()
247{
248 return (cipher_get_number(receive_context.cipher));
249}
250
251
252u_int32_t
253packet_get_seqnr(int mode)
254{
255 return (mode == MODE_IN ? read_seqnr : send_seqnr);
256}
257
258void
259packet_set_seqnr(int mode, u_int32_t seqnr)
260{
261 if (mode == MODE_IN)
262 read_seqnr = seqnr;
263 else if (mode == MODE_OUT)
264 send_seqnr = seqnr;
265 else
91647fe6 266 fatal("packet_set_seqnr: bad mode %d", mode);
e050d348 267}
268
48e671d5 269/* returns 1 if connection is via ipv4 */
270
271int
d5bb9418 272packet_connection_is_ipv4(void)
48e671d5 273{
274 struct sockaddr_storage to;
d45317d8 275 socklen_t tolen = sizeof(to);
48e671d5 276
277 memset(&to, 0, sizeof(to));
278 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
279 return 0;
782e2103 280 if (to.ss_family == AF_INET)
281 return 1;
282#ifdef IPV4_IN_IPV6
283 if (to.ss_family == AF_INET6 &&
284 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
285 return 1;
286#endif
287 return 0;
48e671d5 288}
289
8efc0c15 290/* Sets the connection into non-blocking mode. */
291
292void
d5bb9418 293packet_set_nonblocking(void)
8efc0c15 294{
5260325f 295 /* Set the socket into non-blocking mode. */
296 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
297 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
8efc0c15 298
5260325f 299 if (connection_out != connection_in) {
300 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
301 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
302 }
8efc0c15 303}
304
305/* Returns the socket used for reading. */
306
307int
d5bb9418 308packet_get_connection_in(void)
8efc0c15 309{
5260325f 310 return connection_in;
8efc0c15 311}
312
313/* Returns the descriptor used for writing. */
314
315int
d5bb9418 316packet_get_connection_out(void)
8efc0c15 317{
5260325f 318 return connection_out;
8efc0c15 319}
320
321/* Closes the connection and clears and frees internal data structures. */
322
323void
d5bb9418 324packet_close(void)
8efc0c15 325{
5260325f 326 if (!initialized)
327 return;
328 initialized = 0;
329 if (connection_in == connection_out) {
330 shutdown(connection_out, SHUT_RDWR);
331 close(connection_out);
332 } else {
333 close(connection_in);
334 close(connection_out);
335 }
336 buffer_free(&input);
337 buffer_free(&output);
338 buffer_free(&outgoing_packet);
339 buffer_free(&incoming_packet);
6ba22c93 340 if (compression_buffer_ready) {
5260325f 341 buffer_free(&compression_buffer);
342 buffer_compress_uninit();
343 }
3ee832e5 344 cipher_cleanup(&send_context);
345 cipher_cleanup(&receive_context);
8efc0c15 346}
347
348/* Sets remote side protocol flags. */
349
350void
1e3b8b07 351packet_set_protocol_flags(u_int protocol_flags)
8efc0c15 352{
5260325f 353 remote_protocol_flags = protocol_flags;
8efc0c15 354}
355
356/* Returns the remote protocol flags set earlier by the above function. */
357
1e3b8b07 358u_int
d5bb9418 359packet_get_protocol_flags(void)
8efc0c15 360{
5260325f 361 return remote_protocol_flags;
8efc0c15 362}
363
aa3378df 364/*
365 * Starts packet compression from the next packet on in both directions.
366 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
367 */
8efc0c15 368
396c147e 369static void
370packet_init_compression(void)
6ba22c93 371{
372 if (compression_buffer_ready == 1)
373 return;
374 compression_buffer_ready = 1;
375 buffer_init(&compression_buffer);
376}
377
8efc0c15 378void
379packet_start_compression(int level)
380{
08dcb5d7 381 if (packet_compression && !compat20)
5260325f 382 fatal("Compression already enabled.");
383 packet_compression = 1;
6ba22c93 384 packet_init_compression();
385 buffer_compress_init_send(level);
386 buffer_compress_init_recv();
8efc0c15 387}
388
aa3378df 389/*
390 * Causes any further packets to be encrypted using the given key. The same
391 * key is used for both sending and reception. However, both directions are
392 * encrypted independently of each other.
393 */
8efc0c15 394void
1e3b8b07 395packet_set_encryption_key(const u_char *key, u_int keylen,
94ec8c6b 396 int number)
8efc0c15 397{
94ec8c6b 398 Cipher *cipher = cipher_by_number(number);
399 if (cipher == NULL)
400 fatal("packet_set_encryption_key: unknown cipher number %d", number);
7e7327a1 401 if (keylen < 20)
94ec8c6b 402 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
3ee832e5 403 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
404 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
8efc0c15 405}
406
08dcb5d7 407/* Start constructing a packet to send. */
8efc0c15 408void
08dcb5d7 409packet_start(u_char type)
8efc0c15 410{
08dcb5d7 411 u_char buf[9];
412 int len;
7e7327a1 413
12bf85ed 414 DBG(debug("packet_start[%d]", type));
08dcb5d7 415 len = compat20 ? 6 : 9;
416 memset(buf, 0, len - 1);
417 buf[len - 1] = type;
418 buffer_clear(&outgoing_packet);
419 buffer_append(&outgoing_packet, buf, len);
7e7327a1 420}
421
08dcb5d7 422/* Append payload. */
8efc0c15 423void
424packet_put_char(int value)
425{
5260325f 426 char ch = value;
427 buffer_append(&outgoing_packet, &ch, 1);
8efc0c15 428}
8efc0c15 429void
1e3b8b07 430packet_put_int(u_int value)
8efc0c15 431{
5260325f 432 buffer_put_int(&outgoing_packet, value);
8efc0c15 433}
8efc0c15 434void
6c0fa2b1 435packet_put_string(const void *buf, u_int len)
8efc0c15 436{
5260325f 437 buffer_put_string(&outgoing_packet, buf, len);
8efc0c15 438}
7e7327a1 439void
440packet_put_cstring(const char *str)
441{
449c5ba5 442 buffer_put_cstring(&outgoing_packet, str);
7e7327a1 443}
7e7327a1 444void
6c0fa2b1 445packet_put_raw(const void *buf, u_int len)
7e7327a1 446{
447 buffer_append(&outgoing_packet, buf, len);
448}
8efc0c15 449void
5260325f 450packet_put_bignum(BIGNUM * value)
8efc0c15 451{
5260325f 452 buffer_put_bignum(&outgoing_packet, value);
8efc0c15 453}
7e7327a1 454void
455packet_put_bignum2(BIGNUM * value)
456{
457 buffer_put_bignum2(&outgoing_packet, value);
458}
8efc0c15 459
aa3378df 460/*
461 * Finalizes and sends the packet. If the encryption key has been set,
462 * encrypts the packet before sending.
463 */
5260325f 464
396c147e 465static void
5ca51e19 466packet_send1(void)
8efc0c15 467{
a318bbf4 468 u_char buf[8], *cp;
5260325f 469 int i, padding, len;
1e3b8b07 470 u_int checksum;
5260325f 471 u_int32_t rand = 0;
472
aa3378df 473 /*
474 * If using packet compression, compress the payload of the outgoing
475 * packet.
476 */
5260325f 477 if (packet_compression) {
478 buffer_clear(&compression_buffer);
479 /* Skip padding. */
480 buffer_consume(&outgoing_packet, 8);
481 /* padding */
482 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
483 buffer_compress(&outgoing_packet, &compression_buffer);
484 buffer_clear(&outgoing_packet);
485 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
184eed6a 486 buffer_len(&compression_buffer));
5260325f 487 }
488 /* Compute packet length without padding (add checksum, remove padding). */
489 len = buffer_len(&outgoing_packet) + 4 - 8;
490
1d1ffb87 491 /* Insert padding. Initialized to zero in packet_start1() */
5260325f 492 padding = 8 - len % 8;
3ee832e5 493 if (!send_context.plaintext) {
5260325f 494 cp = buffer_ptr(&outgoing_packet);
495 for (i = 0; i < padding; i++) {
496 if (i % 4 == 0)
497 rand = arc4random();
498 cp[7 - i] = rand & 0xff;
499 rand >>= 8;
500 }
501 }
502 buffer_consume(&outgoing_packet, 8 - padding);
503
504 /* Add check bytes. */
20905a8e 505 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
2e73a022 506 buffer_len(&outgoing_packet));
5260325f 507 PUT_32BIT(buf, checksum);
508 buffer_append(&outgoing_packet, buf, 4);
8efc0c15 509
510#ifdef PACKET_DEBUG
5260325f 511 fprintf(stderr, "packet_send plain: ");
512 buffer_dump(&outgoing_packet);
8efc0c15 513#endif
514
5260325f 515 /* Append to output. */
516 PUT_32BIT(buf, len);
517 buffer_append(&output, buf, 4);
6c0fa2b1 518 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
3ee832e5 519 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
184eed6a 520 buffer_len(&outgoing_packet));
5260325f 521
8efc0c15 522#ifdef PACKET_DEBUG
5260325f 523 fprintf(stderr, "encrypted: ");
524 buffer_dump(&output);
8efc0c15 525#endif
526
5260325f 527 buffer_clear(&outgoing_packet);
8efc0c15 528
aa3378df 529 /*
530 * Note that the packet is now only buffered in output. It won\'t be
531 * actually sent until packet_write_wait or packet_write_poll is
532 * called.
533 */
8efc0c15 534}
535
e050d348 536void
d1ac6175 537set_newkeys(int mode)
538{
539 Enc *enc;
540 Mac *mac;
541 Comp *comp;
542 CipherContext *cc;
3ee832e5 543 int encrypt;
d1ac6175 544
545 debug("newkeys: mode %d", mode);
546
3ee832e5 547 if (mode == MODE_OUT) {
548 cc = &send_context;
549 encrypt = CIPHER_ENCRYPT;
550 } else {
551 cc = &receive_context;
552 encrypt = CIPHER_DECRYPT;
553 }
d1ac6175 554 if (newkeys[mode] != NULL) {
555 debug("newkeys: rekeying");
3ee832e5 556 cipher_cleanup(cc);
a7ca6275 557 enc = &newkeys[mode]->enc;
558 mac = &newkeys[mode]->mac;
559 comp = &newkeys[mode]->comp;
cd332296 560 memset(mac->key, 0, mac->key_len);
a7ca6275 561 xfree(enc->name);
562 xfree(enc->iv);
563 xfree(enc->key);
564 xfree(mac->name);
565 xfree(mac->key);
566 xfree(comp->name);
d8ee838b 567 xfree(newkeys[mode]);
d1ac6175 568 }
569 newkeys[mode] = kex_get_newkeys(mode);
570 if (newkeys[mode] == NULL)
571 fatal("newkeys: no keys for mode %d", mode);
572 enc = &newkeys[mode]->enc;
573 mac = &newkeys[mode]->mac;
574 comp = &newkeys[mode]->comp;
575 if (mac->md != NULL)
576 mac->enabled = 1;
577 DBG(debug("cipher_init_context: %d", mode));
3ee832e5 578 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
579 enc->iv, enc->block_size, encrypt);
e050d348 580 /* Deleting the keys does not gain extra security */
581 /* memset(enc->iv, 0, enc->block_size);
582 memset(enc->key, 0, enc->key_len); */
d1ac6175 583 if (comp->type != 0 && comp->enabled == 0) {
6ba22c93 584 packet_init_compression();
585 if (mode == MODE_OUT)
586 buffer_compress_init_send(6);
587 else
588 buffer_compress_init_recv();
d1ac6175 589 comp->enabled = 1;
d1ac6175 590 }
591}
592
7e7327a1 593/*
594 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
595 */
396c147e 596static void
5ca51e19 597packet_send2(void)
7e7327a1 598{
a318bbf4 599 u_char type, *cp, *macbuf = NULL;
b4b701be 600 u_char padlen, pad;
1e3b8b07 601 u_int packet_length = 0;
b4b701be 602 u_int i, len;
7e7327a1 603 u_int32_t rand = 0;
7e7327a1 604 Enc *enc = NULL;
605 Mac *mac = NULL;
606 Comp *comp = NULL;
607 int block_size;
608
d1ac6175 609 if (newkeys[MODE_OUT] != NULL) {
610 enc = &newkeys[MODE_OUT]->enc;
611 mac = &newkeys[MODE_OUT]->mac;
612 comp = &newkeys[MODE_OUT]->comp;
7e7327a1 613 }
3ee832e5 614 block_size = enc ? enc->block_size : 8;
7e7327a1 615
a318bbf4 616 cp = buffer_ptr(&outgoing_packet);
617 type = cp[5];
7e7327a1 618
619#ifdef PACKET_DEBUG
620 fprintf(stderr, "plain: ");
621 buffer_dump(&outgoing_packet);
622#endif
623
624 if (comp && comp->enabled) {
625 len = buffer_len(&outgoing_packet);
626 /* skip header, compress only payload */
627 buffer_consume(&outgoing_packet, 5);
628 buffer_clear(&compression_buffer);
629 buffer_compress(&outgoing_packet, &compression_buffer);
630 buffer_clear(&outgoing_packet);
631 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
632 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
633 buffer_len(&compression_buffer));
634 DBG(debug("compression: raw %d compressed %d", len,
635 buffer_len(&outgoing_packet)));
636 }
637
638 /* sizeof (packet_len + pad_len + payload) */
639 len = buffer_len(&outgoing_packet);
640
641 /*
642 * calc size of padding, alloc space, get random data,
643 * minimum padding is 4 bytes
644 */
645 padlen = block_size - (len % block_size);
646 if (padlen < 4)
647 padlen += block_size;
b4b701be 648 if (extra_pad) {
649 /* will wrap if extra_pad+padlen > 255 */
650 extra_pad = roundup(extra_pad, block_size);
651 pad = extra_pad - ((len + padlen) % extra_pad);
a49dfdec 652 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
b4b701be 653 pad, len, padlen, extra_pad);
654 padlen += pad;
655 extra_pad = 0;
656 }
6c0fa2b1 657 cp = buffer_append_space(&outgoing_packet, padlen);
3ee832e5 658 if (enc && !send_context.plaintext) {
1d1ffb87 659 /* random padding */
7e7327a1 660 for (i = 0; i < padlen; i++) {
661 if (i % 4 == 0)
662 rand = arc4random();
663 cp[i] = rand & 0xff;
6e18cb71 664 rand >>= 8;
7e7327a1 665 }
1d1ffb87 666 } else {
667 /* clear padding */
668 memset(cp, 0, padlen);
7e7327a1 669 }
670 /* packet_length includes payload, padding and padding length field */
671 packet_length = buffer_len(&outgoing_packet) - 4;
a318bbf4 672 cp = buffer_ptr(&outgoing_packet);
673 PUT_32BIT(cp, packet_length);
674 cp[4] = padlen;
7e7327a1 675 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
676
677 /* compute MAC over seqnr and packet(length fields, payload, padding) */
678 if (mac && mac->enabled) {
e050d348 679 macbuf = mac_compute(mac, send_seqnr,
20905a8e 680 buffer_ptr(&outgoing_packet),
b2552997 681 buffer_len(&outgoing_packet));
e050d348 682 DBG(debug("done calc MAC out #%d", send_seqnr));
7e7327a1 683 }
684 /* encrypt packet and append to output buffer. */
6c0fa2b1 685 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
3ee832e5 686 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
7e7327a1 687 buffer_len(&outgoing_packet));
688 /* append unencrypted MAC */
689 if (mac && mac->enabled)
690 buffer_append(&output, (char *)macbuf, mac->mac_len);
691#ifdef PACKET_DEBUG
692 fprintf(stderr, "encrypted: ");
693 buffer_dump(&output);
694#endif
6ae2364d 695 /* increment sequence number for outgoing packets */
e050d348 696 if (++send_seqnr == 0)
6ae2364d 697 log("outgoing seqnr wraps around");
7e7327a1 698 buffer_clear(&outgoing_packet);
699
d1ac6175 700 if (type == SSH2_MSG_NEWKEYS)
701 set_newkeys(MODE_OUT);
7e7327a1 702}
703
704void
d5bb9418 705packet_send(void)
7e7327a1 706{
08dcb5d7 707 if (compat20)
7e7327a1 708 packet_send2();
709 else
710 packet_send1();
711 DBG(debug("packet_send done"));
712}
713
aa3378df 714/*
715 * Waits until a packet has been received, and returns its type. Note that
716 * no other data is processed until this returns, so this function should not
717 * be used during the interactive session.
718 */
8efc0c15 719
720int
54a5250f 721packet_read_seqnr(u_int32_t *seqnr_p)
8efc0c15 722{
5260325f 723 int type, len;
20e04e90 724 fd_set *setp;
5260325f 725 char buf[8192];
7e7327a1 726 DBG(debug("packet_read()"));
5260325f 727
20e04e90 728 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
729 sizeof(fd_mask));
730
5260325f 731 /* Since we are blocking, ensure that all written packets have been sent. */
732 packet_write_wait();
733
734 /* Stay in the loop until we have received a complete packet. */
735 for (;;) {
736 /* Try to read a packet from the buffer. */
54a5250f 737 type = packet_read_poll_seqnr(seqnr_p);
08dcb5d7 738 if (!compat20 && (
1d1ffb87 739 type == SSH_SMSG_SUCCESS
5260325f 740 || type == SSH_SMSG_FAILURE
741 || type == SSH_CMSG_EOF
1d1ffb87 742 || type == SSH_CMSG_EXIT_CONFIRMATION))
95500969 743 packet_check_eom();
5260325f 744 /* If we got a packet, return it. */
20e04e90 745 if (type != SSH_MSG_NONE) {
746 xfree(setp);
5260325f 747 return type;
20e04e90 748 }
aa3378df 749 /*
750 * Otherwise, wait for some data to arrive, add it to the
751 * buffer, and try again.
752 */
20e04e90 753 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
754 sizeof(fd_mask));
755 FD_SET(connection_in, setp);
aa3378df 756
5260325f 757 /* Wait for some data to arrive. */
20e04e90 758 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
fd193ca4 759 (errno == EAGAIN || errno == EINTR))
760 ;
aa3378df 761
5260325f 762 /* Read data from the socket. */
763 len = read(connection_in, buf, sizeof(buf));
89cafde6 764 if (len == 0) {
765 log("Connection closed by %.200s", get_remote_ipaddr());
766 fatal_cleanup();
767 }
5260325f 768 if (len < 0)
769 fatal("Read from socket failed: %.100s", strerror(errno));
770 /* Append it to the buffer. */
771 packet_process_incoming(buf, len);
772 }
773 /* NOTREACHED */
8efc0c15 774}
775
24ca6821 776int
54a5250f 777packet_read(void)
24ca6821 778{
54a5250f 779 return packet_read_seqnr(NULL);
24ca6821 780}
781
aa3378df 782/*
783 * Waits until a packet has been received, verifies that its type matches
784 * that given, and gives a fatal error and exits if there is a mismatch.
785 */
8efc0c15 786
787void
54a5250f 788packet_read_expect(int expected_type)
8efc0c15 789{
5260325f 790 int type;
8efc0c15 791
54a5250f 792 type = packet_read();
5260325f 793 if (type != expected_type)
794 packet_disconnect("Protocol error: expected packet type %d, got %d",
7e7327a1 795 expected_type, type);
8efc0c15 796}
797
798/* Checks if a full packet is available in the data received so far via
5260325f 799 * packet_process_incoming. If so, reads the packet; otherwise returns
800 * SSH_MSG_NONE. This does not wait for data from the connection.
801 *
802 * SSH_MSG_DISCONNECT is handled specially here. Also,
803 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
804 * to higher levels.
5260325f 805 */
8efc0c15 806
396c147e 807static int
54a5250f 808packet_read_poll1(void)
8efc0c15 809{
1e3b8b07 810 u_int len, padded_len;
a318bbf4 811 u_char *cp, type;
1e3b8b07 812 u_int checksum, stored_checksum;
5260325f 813
5260325f 814 /* Check if input size is less than minimum packet size. */
815 if (buffer_len(&input) < 4 + 8)
816 return SSH_MSG_NONE;
817 /* Get length of incoming packet. */
a318bbf4 818 cp = buffer_ptr(&input);
819 len = GET_32BIT(cp);
5260325f 820 if (len < 1 + 2 + 2 || len > 256 * 1024)
821 packet_disconnect("Bad packet length %d.", len);
822 padded_len = (len + 8) & ~7;
823
824 /* Check if the packet has been entirely received. */
825 if (buffer_len(&input) < 4 + padded_len)
826 return SSH_MSG_NONE;
827
828 /* The entire packet is in buffer. */
829
830 /* Consume packet length. */
831 buffer_consume(&input, 4);
832
08dcb5d7 833 /*
834 * Cryptographic attack detector for ssh
835 * (C)1998 CORE-SDI, Buenos Aires Argentina
836 * Ariel Futoransky(futo@core-sdi.com)
837 */
3ee832e5 838 if (!receive_context.plaintext &&
08dcb5d7 839 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
840 packet_disconnect("crc32 compensation attack: network attack detected");
841
842 /* Decrypt data to incoming_packet. */
5260325f 843 buffer_clear(&incoming_packet);
6c0fa2b1 844 cp = buffer_append_space(&incoming_packet, padded_len);
3ee832e5 845 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
08dcb5d7 846
5260325f 847 buffer_consume(&input, padded_len);
8efc0c15 848
849#ifdef PACKET_DEBUG
5260325f 850 fprintf(stderr, "read_poll plain: ");
851 buffer_dump(&incoming_packet);
8efc0c15 852#endif
5260325f 853
854 /* Compute packet checksum. */
20905a8e 855 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
7e7327a1 856 buffer_len(&incoming_packet) - 4);
5260325f 857
858 /* Skip padding. */
859 buffer_consume(&incoming_packet, 8 - len % 8);
860
861 /* Test check bytes. */
5260325f 862 if (len != buffer_len(&incoming_packet))
24ca6821 863 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
7e7327a1 864 len, buffer_len(&incoming_packet));
5260325f 865
a318bbf4 866 cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
867 stored_checksum = GET_32BIT(cp);
5260325f 868 if (checksum != stored_checksum)
869 packet_disconnect("Corrupted check bytes on input.");
870 buffer_consume_end(&incoming_packet, 4);
871
5260325f 872 if (packet_compression) {
873 buffer_clear(&compression_buffer);
874 buffer_uncompress(&incoming_packet, &compression_buffer);
875 buffer_clear(&incoming_packet);
876 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
7e7327a1 877 buffer_len(&compression_buffer));
5260325f 878 }
08dcb5d7 879 type = buffer_get_char(&incoming_packet);
08dcb5d7 880 return type;
5260325f 881}
882
396c147e 883static int
54a5250f 884packet_read_poll2(u_int32_t *seqnr_p)
7e7327a1 885{
b2552997 886 static u_int packet_length = 0;
1e3b8b07 887 u_int padlen, need;
a318bbf4 888 u_char *macbuf, *cp, type;
7e7327a1 889 int maclen, block_size;
890 Enc *enc = NULL;
891 Mac *mac = NULL;
892 Comp *comp = NULL;
893
d1ac6175 894 if (newkeys[MODE_IN] != NULL) {
895 enc = &newkeys[MODE_IN]->enc;
896 mac = &newkeys[MODE_IN]->mac;
897 comp = &newkeys[MODE_IN]->comp;
7e7327a1 898 }
899 maclen = mac && mac->enabled ? mac->mac_len : 0;
3ee832e5 900 block_size = enc ? enc->block_size : 8;
7e7327a1 901
902 if (packet_length == 0) {
903 /*
904 * check if input size is less than the cipher block size,
905 * decrypt first block and extract length of incoming packet
906 */
907 if (buffer_len(&input) < block_size)
908 return SSH_MSG_NONE;
909 buffer_clear(&incoming_packet);
6c0fa2b1 910 cp = buffer_append_space(&incoming_packet, block_size);
3ee832e5 911 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
7e7327a1 912 block_size);
a318bbf4 913 cp = buffer_ptr(&incoming_packet);
914 packet_length = GET_32BIT(cp);
7e7327a1 915 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
916 buffer_dump(&incoming_packet);
917 packet_disconnect("Bad packet length %d.", packet_length);
918 }
919 DBG(debug("input: packet len %d", packet_length+4));
920 buffer_consume(&input, block_size);
921 }
922 /* we have a partial packet of block_size bytes */
923 need = 4 + packet_length - block_size;
924 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
925 need, maclen));
926 if (need % block_size != 0)
927 fatal("padding error: need %d block %d mod %d",
928 need, block_size, need % block_size);
929 /*
930 * check if the entire packet has been received and
931 * decrypt into incoming_packet
932 */
933 if (buffer_len(&input) < need + maclen)
934 return SSH_MSG_NONE;
935#ifdef PACKET_DEBUG
936 fprintf(stderr, "read_poll enc/full: ");
937 buffer_dump(&input);
938#endif
6c0fa2b1 939 cp = buffer_append_space(&incoming_packet, need);
3ee832e5 940 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
7e7327a1 941 buffer_consume(&input, need);
942 /*
943 * compute MAC over seqnr and packet,
944 * increment sequence number for incoming packet
945 */
6ae2364d 946 if (mac && mac->enabled) {
e050d348 947 macbuf = mac_compute(mac, read_seqnr,
20905a8e 948 buffer_ptr(&incoming_packet),
b2552997 949 buffer_len(&incoming_packet));
7e7327a1 950 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
94ec8c6b 951 packet_disconnect("Corrupted MAC on input.");
e050d348 952 DBG(debug("MAC #%d ok", read_seqnr));
7e7327a1 953 buffer_consume(&input, mac->mac_len);
954 }
24ca6821 955 if (seqnr_p != NULL)
e050d348 956 *seqnr_p = read_seqnr;
957 if (++read_seqnr == 0)
6ae2364d 958 log("incoming seqnr wraps around");
7e7327a1 959
960 /* get padlen */
6c0fa2b1 961 cp = buffer_ptr(&incoming_packet);
a318bbf4 962 padlen = cp[4];
7e7327a1 963 DBG(debug("input: padlen %d", padlen));
964 if (padlen < 4)
965 packet_disconnect("Corrupted padlen %d on input.", padlen);
966
967 /* skip packet size + padlen, discard padding */
968 buffer_consume(&incoming_packet, 4 + 1);
969 buffer_consume_end(&incoming_packet, padlen);
970
971 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
972 if (comp && comp->enabled) {
973 buffer_clear(&compression_buffer);
974 buffer_uncompress(&incoming_packet, &compression_buffer);
975 buffer_clear(&incoming_packet);
976 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
977 buffer_len(&compression_buffer));
978 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
979 }
980 /*
981 * get packet type, implies consume.
982 * return length of payload (without type field)
983 */
08dcb5d7 984 type = buffer_get_char(&incoming_packet);
d1ac6175 985 if (type == SSH2_MSG_NEWKEYS)
986 set_newkeys(MODE_IN);
7e7327a1 987#ifdef PACKET_DEBUG
12bf85ed 988 fprintf(stderr, "read/plain[%d]:\r\n", type);
7e7327a1 989 buffer_dump(&incoming_packet);
990#endif
08dcb5d7 991 /* reset for next packet */
992 packet_length = 0;
993 return type;
7e7327a1 994}
995
996int
54a5250f 997packet_read_poll_seqnr(u_int32_t *seqnr_p)
7e7327a1 998{
5a5f4c37 999 int reason, seqnr;
08dcb5d7 1000 u_char type;
7e7327a1 1001 char *msg;
7e7327a1 1002
08dcb5d7 1003 for (;;) {
1004 if (compat20) {
54a5250f 1005 type = packet_read_poll2(seqnr_p);
08dcb5d7 1006 if (type)
7e7327a1 1007 DBG(debug("received packet type %d", type));
6aacefa7 1008 switch (type) {
7e7327a1 1009 case SSH2_MSG_IGNORE:
1010 break;
1011 case SSH2_MSG_DEBUG:
1012 packet_get_char();
1013 msg = packet_get_string(NULL);
1014 debug("Remote: %.900s", msg);
1015 xfree(msg);
1016 msg = packet_get_string(NULL);
1017 xfree(msg);
1018 break;
1019 case SSH2_MSG_DISCONNECT:
1020 reason = packet_get_int();
1021 msg = packet_get_string(NULL);
7c49df64 1022 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
1023 reason, msg);
7e7327a1 1024 xfree(msg);
1025 fatal_cleanup();
1026 break;
5a5f4c37 1027 case SSH2_MSG_UNIMPLEMENTED:
1028 seqnr = packet_get_int();
1029 debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
1030 break;
7e7327a1 1031 default:
1032 return type;
1033 break;
2b87da3b 1034 }
7e7327a1 1035 } else {
54a5250f 1036 type = packet_read_poll1();
6aacefa7 1037 switch (type) {
7e7327a1 1038 case SSH_MSG_IGNORE:
1039 break;
1040 case SSH_MSG_DEBUG:
1041 msg = packet_get_string(NULL);
1042 debug("Remote: %.900s", msg);
1043 xfree(msg);
1044 break;
1045 case SSH_MSG_DISCONNECT:
1046 msg = packet_get_string(NULL);
7c49df64 1047 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1048 msg);
7e7327a1 1049 fatal_cleanup();
1050 xfree(msg);
1051 break;
1052 default:
08dcb5d7 1053 if (type)
7e7327a1 1054 DBG(debug("received packet type %d", type));
1055 return type;
1056 break;
2b87da3b 1057 }
7e7327a1 1058 }
1059 }
1060}
1061
24ca6821 1062int
54a5250f 1063packet_read_poll(void)
24ca6821 1064{
54a5250f 1065 return packet_read_poll_seqnr(NULL);
24ca6821 1066}
1067
aa3378df 1068/*
1069 * Buffers the given amount of input characters. This is intended to be used
1070 * together with packet_read_poll.
1071 */
8efc0c15 1072
1073void
1e3b8b07 1074packet_process_incoming(const char *buf, u_int len)
8efc0c15 1075{
5260325f 1076 buffer_append(&input, buf, len);
8efc0c15 1077}
1078
1079/* Returns a character from the packet. */
1080
1e3b8b07 1081u_int
d5bb9418 1082packet_get_char(void)
8efc0c15 1083{
5260325f 1084 char ch;
1085 buffer_get(&incoming_packet, &ch, 1);
1e3b8b07 1086 return (u_char) ch;
8efc0c15 1087}
1088
1089/* Returns an integer from the packet data. */
1090
1e3b8b07 1091u_int
d5bb9418 1092packet_get_int(void)
8efc0c15 1093{
5260325f 1094 return buffer_get_int(&incoming_packet);
8efc0c15 1095}
1096
aa3378df 1097/*
1098 * Returns an arbitrary precision integer from the packet data. The integer
1099 * must have been initialized before this call.
1100 */
8efc0c15 1101
1102void
20b279e6 1103packet_get_bignum(BIGNUM * value)
8efc0c15 1104{
4ef6f649 1105 buffer_get_bignum(&incoming_packet, value);
8efc0c15 1106}
1107
7e7327a1 1108void
20b279e6 1109packet_get_bignum2(BIGNUM * value)
7e7327a1 1110{
4ef6f649 1111 buffer_get_bignum2(&incoming_packet, value);
7e7327a1 1112}
1113
6c0fa2b1 1114void *
7e7327a1 1115packet_get_raw(int *length_ptr)
1116{
1117 int bytes = buffer_len(&incoming_packet);
1118 if (length_ptr != NULL)
1119 *length_ptr = bytes;
1120 return buffer_ptr(&incoming_packet);
1121}
1122
6ae2364d 1123int
1124packet_remaining(void)
1125{
1126 return buffer_len(&incoming_packet);
1127}
1128
aa3378df 1129/*
1130 * Returns a string from the packet data. The string is allocated using
1131 * xmalloc; it is the responsibility of the calling program to free it when
1132 * no longer needed. The length_ptr argument may be NULL, or point to an
1133 * integer into which the length of the string is stored.
1134 */
8efc0c15 1135
6c0fa2b1 1136void *
1e3b8b07 1137packet_get_string(u_int *length_ptr)
8efc0c15 1138{
5260325f 1139 return buffer_get_string(&incoming_packet, length_ptr);
8efc0c15 1140}
1141
aa3378df 1142/*
1143 * Sends a diagnostic message from the server to the client. This message
1144 * can be sent at any time (but not while constructing another message). The
1145 * message is printed immediately, but only if the client is being executed
1146 * in verbose mode. These messages are primarily intended to ease debugging
1147 * authentication problems. The length of the formatted message must not
1148 * exceed 1024 bytes. This will automatically call packet_write_wait.
1149 */
8efc0c15 1150
1151void
5260325f 1152packet_send_debug(const char *fmt,...)
8efc0c15 1153{
5260325f 1154 char buf[1024];
1155 va_list args;
1156
f72fc97f 1157 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1158 return;
1159
5260325f 1160 va_start(args, fmt);
1161 vsnprintf(buf, sizeof(buf), fmt, args);
1162 va_end(args);
1163
c4bc58eb 1164 if (compat20) {
1165 packet_start(SSH2_MSG_DEBUG);
1166 packet_put_char(0); /* bool: always display */
1167 packet_put_cstring(buf);
1168 packet_put_cstring("");
1169 } else {
1170 packet_start(SSH_MSG_DEBUG);
1171 packet_put_cstring(buf);
1172 }
5260325f 1173 packet_send();
1174 packet_write_wait();
8efc0c15 1175}
1176
aa3378df 1177/*
1178 * Logs the error plus constructs and sends a disconnect packet, closes the
1179 * connection, and exits. This function never returns. The error message
1180 * should not contain a newline. The length of the formatted message must
1181 * not exceed 1024 bytes.
1182 */
8efc0c15 1183
1184void
5260325f 1185packet_disconnect(const char *fmt,...)
1186{
1187 char buf[1024];
1188 va_list args;
1189 static int disconnecting = 0;
1190 if (disconnecting) /* Guard against recursive invocations. */
1191 fatal("packet_disconnect called recursively.");
1192 disconnecting = 1;
1193
aa3378df 1194 /*
1195 * Format the message. Note that the caller must make sure the
1196 * message is of limited size.
1197 */
5260325f 1198 va_start(args, fmt);
1199 vsnprintf(buf, sizeof(buf), fmt, args);
1200 va_end(args);
1201
1202 /* Send the disconnect message to the other side, and wait for it to get sent. */
7e7327a1 1203 if (compat20) {
1204 packet_start(SSH2_MSG_DISCONNECT);
1205 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1206 packet_put_cstring(buf);
1207 packet_put_cstring("");
1208 } else {
1209 packet_start(SSH_MSG_DISCONNECT);
449c5ba5 1210 packet_put_cstring(buf);
7e7327a1 1211 }
5260325f 1212 packet_send();
1213 packet_write_wait();
1214
1215 /* Stop listening for connections. */
d6746a0b 1216 channel_close_all();
5260325f 1217
1218 /* Close the connection. */
1219 packet_close();
1220
1221 /* Display the error locally and exit. */
57112b5a 1222 log("Disconnecting: %.100s", buf);
1223 fatal_cleanup();
8efc0c15 1224}
1225
aa3378df 1226/* Checks if there is any buffered output, and tries to write some of the output. */
8efc0c15 1227
1228void
d5bb9418 1229packet_write_poll(void)
8efc0c15 1230{
5260325f 1231 int len = buffer_len(&output);
1232 if (len > 0) {
1233 len = write(connection_out, buffer_ptr(&output), len);
1234 if (len <= 0) {
1235 if (errno == EAGAIN)
1236 return;
1237 else
1238 fatal("Write failed: %.100s", strerror(errno));
1239 }
1240 buffer_consume(&output, len);
1241 }
8efc0c15 1242}
1243
aa3378df 1244/*
1245 * Calls packet_write_poll repeatedly until all pending output data has been
1246 * written.
1247 */
8efc0c15 1248
1249void
d5bb9418 1250packet_write_wait(void)
8efc0c15 1251{
20e04e90 1252 fd_set *setp;
1253
1254 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1255 sizeof(fd_mask));
5260325f 1256 packet_write_poll();
1257 while (packet_have_data_to_write()) {
20e04e90 1258 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1259 sizeof(fd_mask));
1260 FD_SET(connection_out, setp);
1261 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
fd193ca4 1262 (errno == EAGAIN || errno == EINTR))
1263 ;
5260325f 1264 packet_write_poll();
1265 }
20e04e90 1266 xfree(setp);
8efc0c15 1267}
1268
1269/* Returns true if there is buffered data to write to the connection. */
1270
1271int
d5bb9418 1272packet_have_data_to_write(void)
8efc0c15 1273{
5260325f 1274 return buffer_len(&output) != 0;
8efc0c15 1275}
1276
1277/* Returns true if there is not too much data to write to the connection. */
1278
1279int
d5bb9418 1280packet_not_very_much_data_to_write(void)
8efc0c15 1281{
5260325f 1282 if (interactive_mode)
1283 return buffer_len(&output) < 16384;
1284 else
1285 return buffer_len(&output) < 128 * 1024;
8efc0c15 1286}
1287
1288/* Informs that the current session is interactive. Sets IP flags for that. */
1289
1290void
b5c334cc 1291packet_set_interactive(int interactive)
8efc0c15 1292{
b5c334cc 1293 static int called = 0;
64e0e67e 1294#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
b5c334cc 1295 int lowdelay = IPTOS_LOWDELAY;
1296 int throughput = IPTOS_THROUGHPUT;
64e0e67e 1297#endif
5260325f 1298
b5c334cc 1299 if (called)
1300 return;
1301 called = 1;
1302
5260325f 1303 /* Record that we are in interactive mode. */
1304 interactive_mode = interactive;
1305
48e671d5 1306 /* Only set socket options if using a socket. */
1307 if (!packet_connection_is_on_socket())
5260325f 1308 return;
48e671d5 1309 /*
f546c780 1310 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
48e671d5 1311 */
5260325f 1312 if (interactive) {
aa3378df 1313 /*
1314 * Set IP options for an interactive connection. Use
1315 * IPTOS_LOWDELAY and TCP_NODELAY.
1316 */
132dd316 1317#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
f546c780 1318 if (packet_connection_is_ipv4()) {
0e3c1f95 1319 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
db518d9b 1320 &lowdelay, sizeof(lowdelay)) < 0)
0e3c1f95 1321 error("setsockopt IPTOS_LOWDELAY: %.100s",
f546c780 1322 strerror(errno));
1323 }
c04f75f1 1324#endif
bcc0381e 1325 set_nodelay(connection_in);
f546c780 1326 } else if (packet_connection_is_ipv4()) {
aa3378df 1327 /*
1328 * Set IP options for a non-interactive connection. Use
1329 * IPTOS_THROUGHPUT.
1330 */
132dd316 1331#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
db518d9b 1332 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
48e671d5 1333 sizeof(throughput)) < 0)
5260325f 1334 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
c04f75f1 1335#endif
5260325f 1336 }
8efc0c15 1337}
1338
1339/* Returns true if the current connection is interactive. */
1340
1341int
d5bb9418 1342packet_is_interactive(void)
8efc0c15 1343{
5260325f 1344 return interactive_mode;
8efc0c15 1345}
9d6b7add 1346
1347int
1348packet_set_maxsize(int s)
1349{
5260325f 1350 static int called = 0;
1351 if (called) {
7e7327a1 1352 log("packet_set_maxsize: called twice: old %d new %d",
1353 max_packet_size, s);
5260325f 1354 return -1;
1355 }
1356 if (s < 4 * 1024 || s > 1024 * 1024) {
1357 log("packet_set_maxsize: bad size %d", s);
1358 return -1;
1359 }
b6350327 1360 called = 1;
76735fe3 1361 debug("packet_set_maxsize: setting to %d", s);
5260325f 1362 max_packet_size = s;
1363 return s;
9d6b7add 1364}
a6215e53 1365
b4b701be 1366/* roundup current message to pad bytes */
1367void
1368packet_add_padding(u_char pad)
1369{
1370 extra_pad = pad;
1371}
1372
a6215e53 1373/*
1374 * 9.2. Ignored Data Message
cd332296 1375 *
a6215e53 1376 * byte SSH_MSG_IGNORE
1377 * string data
cd332296 1378 *
a6215e53 1379 * All implementations MUST understand (and ignore) this message at any
1380 * time (after receiving the protocol version). No implementation is
1381 * required to send them. This message can be used as an additional
1382 * protection measure against advanced traffic analysis techniques.
1383 */
95ce5599 1384void
1385packet_send_ignore(int nbytes)
1386{
1387 u_int32_t rand = 0;
1388 int i;
1389
1390 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
a6215e53 1391 packet_put_int(nbytes);
184eed6a 1392 for (i = 0; i < nbytes; i++) {
a6215e53 1393 if (i % 4 == 0)
1394 rand = arc4random();
1395 packet_put_char(rand & 0xff);
1396 rand >>= 8;
1397 }
1398}
This page took 1.959063 seconds and 5 git commands to generate.