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