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