]> andersk Git - openssh.git/blame - packet.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / packet.c
CommitLineData
5d82ad35 1/* $OpenBSD: packet.c,v 1.166 2009/06/27 09:29:06 andreas Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
7e7327a1 8 *
bcbf86ec 9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
7e7327a1 16 * SSH2 packet format added by Markus Friedl.
a96070d4 17 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
7e7327a1 27 *
bcbf86ec 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 38 */
8efc0c15 39
40#include "includes.h"
a3dcf543 41
5b04a8bf 42#include <sys/types.h>
2f5b2528 43#include "openbsd-compat/sys-queue.h"
536c14e8 44#include <sys/param.h>
9794d008 45#include <sys/socket.h>
e264ac72 46#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
9794d008 49
9794d008 50#include <netinet/in.h>
a3dcf543 51#include <netinet/ip.h>
c5bca5d4 52#include <arpa/inet.h>
8efc0c15 53
028094f4 54#include <errno.h>
4c72fcfd 55#include <stdarg.h>
cf851879 56#include <stdio.h>
ffa517a8 57#include <stdlib.h>
00146caa 58#include <string.h>
5188ba17 59#include <unistd.h>
31652869 60#include <signal.h>
4c72fcfd 61
8efc0c15 62#include "xmalloc.h"
63#include "buffer.h"
64#include "packet.h"
8efc0c15 65#include "crc32.h"
8efc0c15 66#include "compress.h"
67#include "deattack.h"
a5efa1bb 68#include "channels.h"
7e7327a1 69#include "compat.h"
42f11eb2 70#include "ssh1.h"
7e7327a1 71#include "ssh2.h"
94ec8c6b 72#include "cipher.h"
31652869 73#include "key.h"
7e7327a1 74#include "kex.h"
b2552997 75#include "mac.h"
42f11eb2 76#include "log.h"
77#include "canohost.h"
2ac91be1 78#include "misc.h"
9459414c 79#include "ssh.h"
d0137ef8 80#include "roaming.h"
7e7327a1 81
82#ifdef PACKET_DEBUG
83#define DBG(x) x
84#else
85#define DBG(x)
86#endif
87
608bcf58 88#define PACKET_MAX_SIZE (256 * 1024)
89
dc1f1948 90struct packet_state {
91 u_int32_t seqnr;
92 u_int32_t packets;
93 u_int64_t blocks;
94 u_int64_t bytes;
95};
8efc0c15 96
dc1f1948 97struct packet {
98 TAILQ_ENTRY(packet) next;
99 u_char type;
100 Buffer payload;
101};
8efc0c15 102
dc1f1948 103struct session_state {
104 /*
105 * This variable contains the file descriptors used for
106 * communicating with the other side. connection_in is used for
107 * reading; connection_out for writing. These can be the same
108 * descriptor, in which case it is assumed to be a socket.
109 */
110 int connection_in;
111 int connection_out;
5260325f 112
dc1f1948 113 /* Protocol flags for the remote side. */
114 u_int remote_protocol_flags;
8efc0c15 115
dc1f1948 116 /* Encryption context for receiving data. Only used for decryption. */
117 CipherContext receive_context;
8efc0c15 118
dc1f1948 119 /* Encryption context for sending data. Only used for encryption. */
120 CipherContext send_context;
8efc0c15 121
dc1f1948 122 /* Buffer for raw input data from the socket. */
123 Buffer input;
8efc0c15 124
dc1f1948 125 /* Buffer for raw output data going to the socket. */
126 Buffer output;
8efc0c15 127
dc1f1948 128 /* Buffer for the partial outgoing packet being constructed. */
129 Buffer outgoing_packet;
8efc0c15 130
dc1f1948 131 /* Buffer for the incoming packet currently being processed. */
132 Buffer incoming_packet;
8efc0c15 133
dc1f1948 134 /* Scratch buffer for packet compression/decompression. */
135 Buffer compression_buffer;
136 int compression_buffer_ready;
9d6b7add 137
dc1f1948 138 /*
139 * Flag indicating whether packet compression/decompression is
140 * enabled.
141 */
142 int packet_compression;
8efc0c15 143
dc1f1948 144 /* default maximum packet size */
145 u_int max_packet_size;
8efc0c15 146
dc1f1948 147 /* Flag indicating whether this module has been initialized. */
148 int initialized;
07200973 149
dc1f1948 150 /* Set to true if the connection is interactive. */
151 int interactive_mode;
07200973 152
dc1f1948 153 /* Set to true if we are the server side. */
154 int server_side;
ac47b626 155
dc1f1948 156 /* Set to true if we are authenticated. */
157 int after_authentication;
bc2d97c8 158
dc1f1948 159 int keep_alive_timeouts;
ffd7b36b 160
dc1f1948 161 /* The maximum time that we will wait to send or receive a packet */
162 int packet_timeout_ms;
7e7327a1 163
dc1f1948 164 /* Session key information for Encryption and MAC */
165 Newkeys *newkeys[MODE_MAX];
166 struct packet_state p_read, p_send;
9459414c 167
dc1f1948 168 u_int64_t max_blocks_in, max_blocks_out;
169 u_int32_t rekey_limit;
b4b701be 170
dc1f1948 171 /* Session key for protocol v1 */
172 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
173 u_int ssh1_keylen;
608bcf58 174
dc1f1948 175 /* roundup current message to extra_pad bytes */
176 u_char extra_pad;
177
178 /* XXX discard incoming data after MAC error */
179 u_int packet_discard;
180 Mac *packet_discard_mac;
181
182 /* Used in packet_read_poll2() */
183 u_int packlen;
184
6a49252d 185 /* Used in packet_send2 */
186 int rekeying;
187
188 /* Used in packet_set_interactive */
189 int set_interactive_called;
190
191 /* Used in packet_set_maxsize */
192 int set_maxsize_called;
193
dc1f1948 194 TAILQ_HEAD(, packet) outgoing;
ffd7b36b 195};
dc1f1948 196
5d82ad35 197static struct session_state *active_state, *backup_state;
dc1f1948 198
199static struct session_state *
adb5cc1b 200alloc_session_state(void)
dc1f1948 201{
202 struct session_state *s = xcalloc(1, sizeof(*s));
203
204 s->connection_in = -1;
205 s->connection_out = -1;
206 s->max_packet_size = 32768;
207 s->packet_timeout_ms = -1;
208 return s;
209}
ffd7b36b 210
aa3378df 211/*
212 * Sets the descriptors used for communication. Disables encryption until
213 * packet_set_encryption_key is called.
214 */
8efc0c15 215void
216packet_set_connection(int fd_in, int fd_out)
217{
94ec8c6b 218 Cipher *none = cipher_by_name("none");
e1feb9bf 219
94ec8c6b 220 if (none == NULL)
221 fatal("packet_set_connection: cannot load cipher 'none'");
dc1f1948 222 if (active_state == NULL)
223 active_state = alloc_session_state();
224 active_state->connection_in = fd_in;
225 active_state->connection_out = fd_out;
226 cipher_init(&active_state->send_context, none, (const u_char *)"",
f6be21a0 227 0, NULL, 0, CIPHER_ENCRYPT);
dc1f1948 228 cipher_init(&active_state->receive_context, none, (const u_char *)"",
f6be21a0 229 0, NULL, 0, CIPHER_DECRYPT);
dc1f1948 230 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
231 if (!active_state->initialized) {
232 active_state->initialized = 1;
233 buffer_init(&active_state->input);
234 buffer_init(&active_state->output);
235 buffer_init(&active_state->outgoing_packet);
236 buffer_init(&active_state->incoming_packet);
237 TAILQ_INIT(&active_state->outgoing);
238 active_state->p_send.packets = active_state->p_read.packets = 0;
5260325f 239 }
8efc0c15 240}
241
bc2d97c8 242void
243packet_set_timeout(int timeout, int count)
244{
245 if (timeout == 0 || count == 0) {
dc1f1948 246 active_state->packet_timeout_ms = -1;
bc2d97c8 247 return;
248 }
249 if ((INT_MAX / 1000) / count < timeout)
dc1f1948 250 active_state->packet_timeout_ms = INT_MAX;
bc2d97c8 251 else
dc1f1948 252 active_state->packet_timeout_ms = timeout * count * 1000;
bc2d97c8 253}
254
608bcf58 255static void
256packet_stop_discard(void)
257{
dc1f1948 258 if (active_state->packet_discard_mac) {
608bcf58 259 char buf[1024];
260
261 memset(buf, 'a', sizeof(buf));
dc1f1948 262 while (buffer_len(&active_state->incoming_packet) <
263 PACKET_MAX_SIZE)
264 buffer_append(&active_state->incoming_packet, buf,
265 sizeof(buf));
266 (void) mac_compute(active_state->packet_discard_mac,
267 active_state->p_read.seqnr,
268 buffer_ptr(&active_state->incoming_packet),
608bcf58 269 PACKET_MAX_SIZE);
270 }
271 logit("Finished discarding for %.200s", get_remote_ipaddr());
272 cleanup_exit(255);
273}
274
275static void
276packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
277{
8b773163 278 if (enc == NULL || !cipher_is_cbc(enc->cipher))
608bcf58 279 packet_disconnect("Packet corrupt");
280 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
dc1f1948 281 active_state->packet_discard_mac = mac;
282 if (buffer_len(&active_state->input) >= discard)
608bcf58 283 packet_stop_discard();
dc1f1948 284 active_state->packet_discard = discard -
285 buffer_len(&active_state->input);
608bcf58 286}
287
48e671d5 288/* Returns 1 if remote host is connected via socket, 0 if not. */
289
290int
d5bb9418 291packet_connection_is_on_socket(void)
48e671d5 292{
293 struct sockaddr_storage from, to;
294 socklen_t fromlen, tolen;
295
296 /* filedescriptors in and out are the same, so it's a socket */
dc1f1948 297 if (active_state->connection_in == active_state->connection_out)
48e671d5 298 return 1;
299 fromlen = sizeof(from);
300 memset(&from, 0, sizeof(from));
dc1f1948 301 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
302 &fromlen) < 0)
48e671d5 303 return 0;
304 tolen = sizeof(to);
305 memset(&to, 0, sizeof(to));
dc1f1948 306 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
307 &tolen) < 0)
48e671d5 308 return 0;
309 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
310 return 0;
311 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
312 return 0;
313 return 1;
314}
315
762715ce 316/*
e050d348 317 * Exports an IV from the CipherContext required to export the key
318 * state back from the unprivileged child to the privileged parent
319 * process.
320 */
321
322void
323packet_get_keyiv(int mode, u_char *iv, u_int len)
324{
325 CipherContext *cc;
326
327 if (mode == MODE_OUT)
dc1f1948 328 cc = &active_state->send_context;
e050d348 329 else
dc1f1948 330 cc = &active_state->receive_context;
e050d348 331
332 cipher_get_keyiv(cc, iv, len);
333}
334
335int
336packet_get_keycontext(int mode, u_char *dat)
337{
338 CipherContext *cc;
762715ce 339
e050d348 340 if (mode == MODE_OUT)
dc1f1948 341 cc = &active_state->send_context;
e050d348 342 else
dc1f1948 343 cc = &active_state->receive_context;
e050d348 344
345 return (cipher_get_keycontext(cc, dat));
346}
347
348void
349packet_set_keycontext(int mode, u_char *dat)
350{
351 CipherContext *cc;
762715ce 352
e050d348 353 if (mode == MODE_OUT)
dc1f1948 354 cc = &active_state->send_context;
e050d348 355 else
dc1f1948 356 cc = &active_state->receive_context;
e050d348 357
358 cipher_set_keycontext(cc, dat);
359}
360
361int
362packet_get_keyiv_len(int mode)
363{
364 CipherContext *cc;
365
366 if (mode == MODE_OUT)
dc1f1948 367 cc = &active_state->send_context;
e050d348 368 else
dc1f1948 369 cc = &active_state->receive_context;
e050d348 370
371 return (cipher_get_keyiv_len(cc));
372}
a7137f6b 373
e050d348 374void
375packet_set_iv(int mode, u_char *dat)
376{
377 CipherContext *cc;
378
379 if (mode == MODE_OUT)
dc1f1948 380 cc = &active_state->send_context;
e050d348 381 else
dc1f1948 382 cc = &active_state->receive_context;
e050d348 383
384 cipher_set_keyiv(cc, dat);
385}
a7137f6b 386
e050d348 387int
0daa6547 388packet_get_ssh1_cipher(void)
e050d348 389{
dc1f1948 390 return (cipher_get_number(active_state->receive_context.cipher));
e050d348 391}
392
ffd7b36b 393void
e8e08a80 394packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
395 u_int64_t *bytes)
e050d348 396{
ffd7b36b 397 struct packet_state *state;
398
dc1f1948 399 state = (mode == MODE_IN) ?
400 &active_state->p_read : &active_state->p_send;
e8e08a80 401 if (seqnr)
402 *seqnr = state->seqnr;
403 if (blocks)
404 *blocks = state->blocks;
405 if (packets)
406 *packets = state->packets;
407 if (bytes)
408 *bytes = state->bytes;
e050d348 409}
410
411void
e8e08a80 412packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
413 u_int64_t bytes)
e050d348 414{
ffd7b36b 415 struct packet_state *state;
416
dc1f1948 417 state = (mode == MODE_IN) ?
418 &active_state->p_read : &active_state->p_send;
ffd7b36b 419 state->seqnr = seqnr;
420 state->blocks = blocks;
421 state->packets = packets;
e8e08a80 422 state->bytes = bytes;
e050d348 423}
424
48e671d5 425/* returns 1 if connection is via ipv4 */
426
427int
d5bb9418 428packet_connection_is_ipv4(void)
48e671d5 429{
430 struct sockaddr_storage to;
d45317d8 431 socklen_t tolen = sizeof(to);
48e671d5 432
433 memset(&to, 0, sizeof(to));
dc1f1948 434 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
435 &tolen) < 0)
48e671d5 436 return 0;
782e2103 437 if (to.ss_family == AF_INET)
438 return 1;
439#ifdef IPV4_IN_IPV6
aff51935 440 if (to.ss_family == AF_INET6 &&
782e2103 441 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
442 return 1;
443#endif
444 return 0;
48e671d5 445}
446
8efc0c15 447/* Sets the connection into non-blocking mode. */
448
449void
d5bb9418 450packet_set_nonblocking(void)
8efc0c15 451{
5260325f 452 /* Set the socket into non-blocking mode. */
dc1f1948 453 set_nonblock(active_state->connection_in);
8efc0c15 454
dc1f1948 455 if (active_state->connection_out != active_state->connection_in)
456 set_nonblock(active_state->connection_out);
8efc0c15 457}
458
459/* Returns the socket used for reading. */
460
461int
d5bb9418 462packet_get_connection_in(void)
8efc0c15 463{
dc1f1948 464 return active_state->connection_in;
8efc0c15 465}
466
467/* Returns the descriptor used for writing. */
468
469int
d5bb9418 470packet_get_connection_out(void)
8efc0c15 471{
dc1f1948 472 return active_state->connection_out;
8efc0c15 473}
474
475/* Closes the connection and clears and frees internal data structures. */
476
477void
d5bb9418 478packet_close(void)
8efc0c15 479{
dc1f1948 480 if (!active_state->initialized)
5260325f 481 return;
dc1f1948 482 active_state->initialized = 0;
483 if (active_state->connection_in == active_state->connection_out) {
484 shutdown(active_state->connection_out, SHUT_RDWR);
485 close(active_state->connection_out);
5260325f 486 } else {
dc1f1948 487 close(active_state->connection_in);
488 close(active_state->connection_out);
5260325f 489 }
dc1f1948 490 buffer_free(&active_state->input);
491 buffer_free(&active_state->output);
492 buffer_free(&active_state->outgoing_packet);
493 buffer_free(&active_state->incoming_packet);
494 if (active_state->compression_buffer_ready) {
495 buffer_free(&active_state->compression_buffer);
5260325f 496 buffer_compress_uninit();
497 }
dc1f1948 498 cipher_cleanup(&active_state->send_context);
499 cipher_cleanup(&active_state->receive_context);
8efc0c15 500}
501
502/* Sets remote side protocol flags. */
503
504void
1e3b8b07 505packet_set_protocol_flags(u_int protocol_flags)
8efc0c15 506{
dc1f1948 507 active_state->remote_protocol_flags = protocol_flags;
8efc0c15 508}
509
510/* Returns the remote protocol flags set earlier by the above function. */
511
1e3b8b07 512u_int
d5bb9418 513packet_get_protocol_flags(void)
8efc0c15 514{
dc1f1948 515 return active_state->remote_protocol_flags;
8efc0c15 516}
517
aa3378df 518/*
519 * Starts packet compression from the next packet on in both directions.
520 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
521 */
8efc0c15 522
396c147e 523static void
524packet_init_compression(void)
6ba22c93 525{
dc1f1948 526 if (active_state->compression_buffer_ready == 1)
6ba22c93 527 return;
dc1f1948 528 active_state->compression_buffer_ready = 1;
529 buffer_init(&active_state->compression_buffer);
6ba22c93 530}
531
8efc0c15 532void
533packet_start_compression(int level)
534{
dc1f1948 535 if (active_state->packet_compression && !compat20)
5260325f 536 fatal("Compression already enabled.");
dc1f1948 537 active_state->packet_compression = 1;
6ba22c93 538 packet_init_compression();
539 buffer_compress_init_send(level);
540 buffer_compress_init_recv();
8efc0c15 541}
542
aa3378df 543/*
544 * Causes any further packets to be encrypted using the given key. The same
545 * key is used for both sending and reception. However, both directions are
546 * encrypted independently of each other.
547 */
9459414c 548
8efc0c15 549void
1e3b8b07 550packet_set_encryption_key(const u_char *key, u_int keylen,
94ec8c6b 551 int number)
8efc0c15 552{
94ec8c6b 553 Cipher *cipher = cipher_by_number(number);
e1feb9bf 554
94ec8c6b 555 if (cipher == NULL)
556 fatal("packet_set_encryption_key: unknown cipher number %d", number);
7e7327a1 557 if (keylen < 20)
94ec8c6b 558 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
9459414c 559 if (keylen > SSH_SESSION_KEY_LENGTH)
560 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
dc1f1948 561 memcpy(active_state->ssh1_key, key, keylen);
562 active_state->ssh1_keylen = keylen;
563 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
564 0, CIPHER_ENCRYPT);
565 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
566 0, CIPHER_DECRYPT);
8efc0c15 567}
568
9459414c 569u_int
570packet_get_encryption_key(u_char *key)
571{
572 if (key == NULL)
dc1f1948 573 return (active_state->ssh1_keylen);
574 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
575 return (active_state->ssh1_keylen);
9459414c 576}
577
08dcb5d7 578/* Start constructing a packet to send. */
8efc0c15 579void
08dcb5d7 580packet_start(u_char type)
8efc0c15 581{
08dcb5d7 582 u_char buf[9];
583 int len;
7e7327a1 584
12bf85ed 585 DBG(debug("packet_start[%d]", type));
08dcb5d7 586 len = compat20 ? 6 : 9;
587 memset(buf, 0, len - 1);
588 buf[len - 1] = type;
dc1f1948 589 buffer_clear(&active_state->outgoing_packet);
590 buffer_append(&active_state->outgoing_packet, buf, len);
7e7327a1 591}
592
08dcb5d7 593/* Append payload. */
8efc0c15 594void
595packet_put_char(int value)
596{
5260325f 597 char ch = value;
e1feb9bf 598
dc1f1948 599 buffer_append(&active_state->outgoing_packet, &ch, 1);
8efc0c15 600}
a7137f6b 601
8efc0c15 602void
1e3b8b07 603packet_put_int(u_int value)
8efc0c15 604{
dc1f1948 605 buffer_put_int(&active_state->outgoing_packet, value);
8efc0c15 606}
a7137f6b 607
c6063ed7 608void
609packet_put_int64(u_int64_t value)
610{
611 buffer_put_int64(&active_state->outgoing_packet, value);
612}
613
8efc0c15 614void
6c0fa2b1 615packet_put_string(const void *buf, u_int len)
8efc0c15 616{
dc1f1948 617 buffer_put_string(&active_state->outgoing_packet, buf, len);
8efc0c15 618}
a7137f6b 619
7e7327a1 620void
621packet_put_cstring(const char *str)
622{
dc1f1948 623 buffer_put_cstring(&active_state->outgoing_packet, str);
7e7327a1 624}
a7137f6b 625
7e7327a1 626void
6c0fa2b1 627packet_put_raw(const void *buf, u_int len)
7e7327a1 628{
dc1f1948 629 buffer_append(&active_state->outgoing_packet, buf, len);
7e7327a1 630}
a7137f6b 631
8efc0c15 632void
5260325f 633packet_put_bignum(BIGNUM * value)
8efc0c15 634{
dc1f1948 635 buffer_put_bignum(&active_state->outgoing_packet, value);
8efc0c15 636}
a7137f6b 637
7e7327a1 638void
639packet_put_bignum2(BIGNUM * value)
640{
dc1f1948 641 buffer_put_bignum2(&active_state->outgoing_packet, value);
7e7327a1 642}
8efc0c15 643
aa3378df 644/*
645 * Finalizes and sends the packet. If the encryption key has been set,
646 * encrypts the packet before sending.
647 */
5260325f 648
396c147e 649static void
5ca51e19 650packet_send1(void)
8efc0c15 651{
a318bbf4 652 u_char buf[8], *cp;
5260325f 653 int i, padding, len;
1e3b8b07 654 u_int checksum;
ca75d7de 655 u_int32_t rnd = 0;
5260325f 656
aa3378df 657 /*
658 * If using packet compression, compress the payload of the outgoing
659 * packet.
660 */
dc1f1948 661 if (active_state->packet_compression) {
662 buffer_clear(&active_state->compression_buffer);
5260325f 663 /* Skip padding. */
dc1f1948 664 buffer_consume(&active_state->outgoing_packet, 8);
5260325f 665 /* padding */
dc1f1948 666 buffer_append(&active_state->compression_buffer,
667 "\0\0\0\0\0\0\0\0", 8);
668 buffer_compress(&active_state->outgoing_packet,
669 &active_state->compression_buffer);
670 buffer_clear(&active_state->outgoing_packet);
671 buffer_append(&active_state->outgoing_packet,
672 buffer_ptr(&active_state->compression_buffer),
673 buffer_len(&active_state->compression_buffer));
5260325f 674 }
675 /* Compute packet length without padding (add checksum, remove padding). */
dc1f1948 676 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
5260325f 677
1d1ffb87 678 /* Insert padding. Initialized to zero in packet_start1() */
5260325f 679 padding = 8 - len % 8;
dc1f1948 680 if (!active_state->send_context.plaintext) {
681 cp = buffer_ptr(&active_state->outgoing_packet);
5260325f 682 for (i = 0; i < padding; i++) {
683 if (i % 4 == 0)
ca75d7de 684 rnd = arc4random();
685 cp[7 - i] = rnd & 0xff;
686 rnd >>= 8;
5260325f 687 }
688 }
dc1f1948 689 buffer_consume(&active_state->outgoing_packet, 8 - padding);
5260325f 690
691 /* Add check bytes. */
dc1f1948 692 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
693 buffer_len(&active_state->outgoing_packet));
51e7a012 694 put_u32(buf, checksum);
dc1f1948 695 buffer_append(&active_state->outgoing_packet, buf, 4);
8efc0c15 696
697#ifdef PACKET_DEBUG
5260325f 698 fprintf(stderr, "packet_send plain: ");
dc1f1948 699 buffer_dump(&active_state->outgoing_packet);
8efc0c15 700#endif
701
5260325f 702 /* Append to output. */
51e7a012 703 put_u32(buf, len);
dc1f1948 704 buffer_append(&active_state->output, buf, 4);
705 cp = buffer_append_space(&active_state->output,
706 buffer_len(&active_state->outgoing_packet));
707 cipher_crypt(&active_state->send_context, cp,
708 buffer_ptr(&active_state->outgoing_packet),
709 buffer_len(&active_state->outgoing_packet));
5260325f 710
8efc0c15 711#ifdef PACKET_DEBUG
5260325f 712 fprintf(stderr, "encrypted: ");
dc1f1948 713 buffer_dump(&active_state->output);
8efc0c15 714#endif
dc1f1948 715 active_state->p_send.packets++;
716 active_state->p_send.bytes += len +
717 buffer_len(&active_state->outgoing_packet);
718 buffer_clear(&active_state->outgoing_packet);
8efc0c15 719
aa3378df 720 /*
7b9b0103 721 * Note that the packet is now only buffered in output. It won't be
aa3378df 722 * actually sent until packet_write_wait or packet_write_poll is
723 * called.
724 */
8efc0c15 725}
726
e050d348 727void
d1ac6175 728set_newkeys(int mode)
729{
730 Enc *enc;
731 Mac *mac;
732 Comp *comp;
733 CipherContext *cc;
ffd7b36b 734 u_int64_t *max_blocks;
ca75d7de 735 int crypt_type;
d1ac6175 736
a77673cc 737 debug2("set_newkeys: mode %d", mode);
d1ac6175 738
3ee832e5 739 if (mode == MODE_OUT) {
dc1f1948 740 cc = &active_state->send_context;
ca75d7de 741 crypt_type = CIPHER_ENCRYPT;
dc1f1948 742 active_state->p_send.packets = active_state->p_send.blocks = 0;
743 max_blocks = &active_state->max_blocks_out;
3ee832e5 744 } else {
dc1f1948 745 cc = &active_state->receive_context;
ca75d7de 746 crypt_type = CIPHER_DECRYPT;
dc1f1948 747 active_state->p_read.packets = active_state->p_read.blocks = 0;
748 max_blocks = &active_state->max_blocks_in;
3ee832e5 749 }
dc1f1948 750 if (active_state->newkeys[mode] != NULL) {
a77673cc 751 debug("set_newkeys: rekeying");
3ee832e5 752 cipher_cleanup(cc);
dc1f1948 753 enc = &active_state->newkeys[mode]->enc;
754 mac = &active_state->newkeys[mode]->mac;
755 comp = &active_state->newkeys[mode]->comp;
f444d0f8 756 mac_clear(mac);
a7ca6275 757 xfree(enc->name);
758 xfree(enc->iv);
759 xfree(enc->key);
760 xfree(mac->name);
761 xfree(mac->key);
762 xfree(comp->name);
dc1f1948 763 xfree(active_state->newkeys[mode]);
d1ac6175 764 }
dc1f1948 765 active_state->newkeys[mode] = kex_get_newkeys(mode);
766 if (active_state->newkeys[mode] == NULL)
d1ac6175 767 fatal("newkeys: no keys for mode %d", mode);
dc1f1948 768 enc = &active_state->newkeys[mode]->enc;
769 mac = &active_state->newkeys[mode]->mac;
770 comp = &active_state->newkeys[mode]->comp;
f444d0f8 771 if (mac_init(mac) == 0)
d1ac6175 772 mac->enabled = 1;
773 DBG(debug("cipher_init_context: %d", mode));
3ee832e5 774 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
ca75d7de 775 enc->iv, enc->block_size, crypt_type);
e050d348 776 /* Deleting the keys does not gain extra security */
777 /* memset(enc->iv, 0, enc->block_size);
a3de8da1 778 memset(enc->key, 0, enc->key_len);
779 memset(mac->key, 0, mac->key_len); */
07200973 780 if ((comp->type == COMP_ZLIB ||
dc1f1948 781 (comp->type == COMP_DELAYED &&
782 active_state->after_authentication)) && comp->enabled == 0) {
6ba22c93 783 packet_init_compression();
784 if (mode == MODE_OUT)
785 buffer_compress_init_send(6);
786 else
787 buffer_compress_init_recv();
d1ac6175 788 comp->enabled = 1;
d1ac6175 789 }
40729edd 790 /*
791 * The 2^(blocksize*2) limit is too expensive for 3DES,
792 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
793 */
794 if (enc->block_size >= 16)
795 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
796 else
797 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
dc1f1948 798 if (active_state->rekey_limit)
799 *max_blocks = MIN(*max_blocks,
800 active_state->rekey_limit / enc->block_size);
d1ac6175 801}
802
07200973 803/*
804 * Delayed compression for SSH2 is enabled after authentication:
0e13ec0f 805 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
07200973 806 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
807 */
808static void
809packet_enable_delayed_compress(void)
810{
811 Comp *comp = NULL;
812 int mode;
813
814 /*
815 * Remember that we are past the authentication step, so rekeying
816 * with COMP_DELAYED will turn on compression immediately.
817 */
dc1f1948 818 active_state->after_authentication = 1;
07200973 819 for (mode = 0; mode < MODE_MAX; mode++) {
a29c9898 820 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
dc1f1948 821 if (active_state->newkeys[mode] == NULL)
a29c9898 822 continue;
dc1f1948 823 comp = &active_state->newkeys[mode]->comp;
07200973 824 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
4145cbfa 825 packet_init_compression();
07200973 826 if (mode == MODE_OUT)
827 buffer_compress_init_send(6);
828 else
829 buffer_compress_init_recv();
830 comp->enabled = 1;
831 }
832 }
833}
834
7e7327a1 835/*
836 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
837 */
396c147e 838static void
ffd7b36b 839packet_send2_wrapped(void)
7e7327a1 840{
a318bbf4 841 u_char type, *cp, *macbuf = NULL;
b4b701be 842 u_char padlen, pad;
1e3b8b07 843 u_int packet_length = 0;
b4b701be 844 u_int i, len;
ca75d7de 845 u_int32_t rnd = 0;
7e7327a1 846 Enc *enc = NULL;
847 Mac *mac = NULL;
848 Comp *comp = NULL;
849 int block_size;
850
dc1f1948 851 if (active_state->newkeys[MODE_OUT] != NULL) {
852 enc = &active_state->newkeys[MODE_OUT]->enc;
853 mac = &active_state->newkeys[MODE_OUT]->mac;
854 comp = &active_state->newkeys[MODE_OUT]->comp;
7e7327a1 855 }
3ee832e5 856 block_size = enc ? enc->block_size : 8;
7e7327a1 857
dc1f1948 858 cp = buffer_ptr(&active_state->outgoing_packet);
a318bbf4 859 type = cp[5];
7e7327a1 860
861#ifdef PACKET_DEBUG
862 fprintf(stderr, "plain: ");
dc1f1948 863 buffer_dump(&active_state->outgoing_packet);
7e7327a1 864#endif
865
866 if (comp && comp->enabled) {
dc1f1948 867 len = buffer_len(&active_state->outgoing_packet);
7e7327a1 868 /* skip header, compress only payload */
dc1f1948 869 buffer_consume(&active_state->outgoing_packet, 5);
870 buffer_clear(&active_state->compression_buffer);
871 buffer_compress(&active_state->outgoing_packet,
872 &active_state->compression_buffer);
873 buffer_clear(&active_state->outgoing_packet);
874 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
875 buffer_append(&active_state->outgoing_packet,
876 buffer_ptr(&active_state->compression_buffer),
877 buffer_len(&active_state->compression_buffer));
7e7327a1 878 DBG(debug("compression: raw %d compressed %d", len,
dc1f1948 879 buffer_len(&active_state->outgoing_packet)));
7e7327a1 880 }
881
882 /* sizeof (packet_len + pad_len + payload) */
dc1f1948 883 len = buffer_len(&active_state->outgoing_packet);
7e7327a1 884
885 /*
886 * calc size of padding, alloc space, get random data,
887 * minimum padding is 4 bytes
888 */
889 padlen = block_size - (len % block_size);
890 if (padlen < 4)
891 padlen += block_size;
dc1f1948 892 if (active_state->extra_pad) {
b4b701be 893 /* will wrap if extra_pad+padlen > 255 */
dc1f1948 894 active_state->extra_pad =
895 roundup(active_state->extra_pad, block_size);
896 pad = active_state->extra_pad -
897 ((len + padlen) % active_state->extra_pad);
a49dfdec 898 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
dc1f1948 899 pad, len, padlen, active_state->extra_pad);
b4b701be 900 padlen += pad;
dc1f1948 901 active_state->extra_pad = 0;
b4b701be 902 }
dc1f1948 903 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
904 if (enc && !active_state->send_context.plaintext) {
1d1ffb87 905 /* random padding */
7e7327a1 906 for (i = 0; i < padlen; i++) {
907 if (i % 4 == 0)
ca75d7de 908 rnd = arc4random();
909 cp[i] = rnd & 0xff;
910 rnd >>= 8;
7e7327a1 911 }
1d1ffb87 912 } else {
913 /* clear padding */
914 memset(cp, 0, padlen);
7e7327a1 915 }
916 /* packet_length includes payload, padding and padding length field */
dc1f1948 917 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
918 cp = buffer_ptr(&active_state->outgoing_packet);
51e7a012 919 put_u32(cp, packet_length);
a318bbf4 920 cp[4] = padlen;
7e7327a1 921 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
922
923 /* compute MAC over seqnr and packet(length fields, payload, padding) */
924 if (mac && mac->enabled) {
dc1f1948 925 macbuf = mac_compute(mac, active_state->p_send.seqnr,
926 buffer_ptr(&active_state->outgoing_packet),
927 buffer_len(&active_state->outgoing_packet));
928 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
7e7327a1 929 }
930 /* encrypt packet and append to output buffer. */
dc1f1948 931 cp = buffer_append_space(&active_state->output,
932 buffer_len(&active_state->outgoing_packet));
933 cipher_crypt(&active_state->send_context, cp,
934 buffer_ptr(&active_state->outgoing_packet),
935 buffer_len(&active_state->outgoing_packet));
7e7327a1 936 /* append unencrypted MAC */
937 if (mac && mac->enabled)
dc1f1948 938 buffer_append(&active_state->output, macbuf, mac->mac_len);
7e7327a1 939#ifdef PACKET_DEBUG
940 fprintf(stderr, "encrypted: ");
dc1f1948 941 buffer_dump(&active_state->output);
7e7327a1 942#endif
6ae2364d 943 /* increment sequence number for outgoing packets */
dc1f1948 944 if (++active_state->p_send.seqnr == 0)
bbe88b6d 945 logit("outgoing seqnr wraps around");
dc1f1948 946 if (++active_state->p_send.packets == 0)
ffd7b36b 947 if (!(datafellows & SSH_BUG_NOREKEY))
948 fatal("XXX too many packets with same key");
dc1f1948 949 active_state->p_send.blocks += (packet_length + 4) / block_size;
950 active_state->p_send.bytes += packet_length + 4;
951 buffer_clear(&active_state->outgoing_packet);
7e7327a1 952
d1ac6175 953 if (type == SSH2_MSG_NEWKEYS)
954 set_newkeys(MODE_OUT);
dc1f1948 955 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
07200973 956 packet_enable_delayed_compress();
7e7327a1 957}
958
ffd7b36b 959static void
960packet_send2(void)
961{
ffd7b36b 962 struct packet *p;
963 u_char type, *cp;
964
dc1f1948 965 cp = buffer_ptr(&active_state->outgoing_packet);
ffd7b36b 966 type = cp[5];
967
968 /* during rekeying we can only send key exchange messages */
6a49252d 969 if (active_state->rekeying) {
ffd7b36b 970 if (!((type >= SSH2_MSG_TRANSPORT_MIN) &&
971 (type <= SSH2_MSG_TRANSPORT_MAX))) {
972 debug("enqueue packet: %u", type);
973 p = xmalloc(sizeof(*p));
974 p->type = type;
dc1f1948 975 memcpy(&p->payload, &active_state->outgoing_packet,
976 sizeof(Buffer));
977 buffer_init(&active_state->outgoing_packet);
978 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
ffd7b36b 979 return;
980 }
981 }
982
983 /* rekeying starts with sending KEXINIT */
984 if (type == SSH2_MSG_KEXINIT)
6a49252d 985 active_state->rekeying = 1;
ffd7b36b 986
987 packet_send2_wrapped();
988
989 /* after a NEWKEYS message we can send the complete queue */
990 if (type == SSH2_MSG_NEWKEYS) {
6a49252d 991 active_state->rekeying = 0;
dc1f1948 992 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
ffd7b36b 993 type = p->type;
994 debug("dequeue packet: %u", type);
dc1f1948 995 buffer_free(&active_state->outgoing_packet);
996 memcpy(&active_state->outgoing_packet, &p->payload,
ffd7b36b 997 sizeof(Buffer));
dc1f1948 998 TAILQ_REMOVE(&active_state->outgoing, p, next);
ffd7b36b 999 xfree(p);
1000 packet_send2_wrapped();
1001 }
1002 }
1003}
1004
7e7327a1 1005void
d5bb9418 1006packet_send(void)
7e7327a1 1007{
08dcb5d7 1008 if (compat20)
7e7327a1 1009 packet_send2();
1010 else
1011 packet_send1();
1012 DBG(debug("packet_send done"));
1013}
1014
aa3378df 1015/*
1016 * Waits until a packet has been received, and returns its type. Note that
1017 * no other data is processed until this returns, so this function should not
1018 * be used during the interactive session.
1019 */
8efc0c15 1020
1021int
54a5250f 1022packet_read_seqnr(u_int32_t *seqnr_p)
8efc0c15 1023{
d0137ef8 1024 int type, len, ret, ms_remain, cont;
20e04e90 1025 fd_set *setp;
5260325f 1026 char buf[8192];
bc2d97c8 1027 struct timeval timeout, start, *timeoutp = NULL;
5260325f 1028
849d3ceb 1029 DBG(debug("packet_read()"));
1030
dc1f1948 1031 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1032 NFDBITS), sizeof(fd_mask));
20e04e90 1033
5260325f 1034 /* Since we are blocking, ensure that all written packets have been sent. */
1035 packet_write_wait();
1036
1037 /* Stay in the loop until we have received a complete packet. */
1038 for (;;) {
1039 /* Try to read a packet from the buffer. */
54a5250f 1040 type = packet_read_poll_seqnr(seqnr_p);
08dcb5d7 1041 if (!compat20 && (
1d1ffb87 1042 type == SSH_SMSG_SUCCESS
5260325f 1043 || type == SSH_SMSG_FAILURE
1044 || type == SSH_CMSG_EOF
1d1ffb87 1045 || type == SSH_CMSG_EXIT_CONFIRMATION))
95500969 1046 packet_check_eom();
5260325f 1047 /* If we got a packet, return it. */
20e04e90 1048 if (type != SSH_MSG_NONE) {
1049 xfree(setp);
5260325f 1050 return type;
20e04e90 1051 }
aa3378df 1052 /*
1053 * Otherwise, wait for some data to arrive, add it to the
1054 * buffer, and try again.
1055 */
dc1f1948 1056 memset(setp, 0, howmany(active_state->connection_in + 1,
1057 NFDBITS) * sizeof(fd_mask));
1058 FD_SET(active_state->connection_in, setp);
aa3378df 1059
dc1f1948 1060 if (active_state->packet_timeout_ms > 0) {
1061 ms_remain = active_state->packet_timeout_ms;
bc2d97c8 1062 timeoutp = &timeout;
1063 }
5260325f 1064 /* Wait for some data to arrive. */
bc2d97c8 1065 for (;;) {
dc1f1948 1066 if (active_state->packet_timeout_ms != -1) {
bc2d97c8 1067 ms_to_timeval(&timeout, ms_remain);
1068 gettimeofday(&start, NULL);
1069 }
dc1f1948 1070 if ((ret = select(active_state->connection_in + 1, setp,
1071 NULL, NULL, timeoutp)) >= 0)
bc2d97c8 1072 break;
018fda87 1073 if (errno != EAGAIN && errno != EINTR &&
1074 errno != EWOULDBLOCK)
bc2d97c8 1075 break;
dc1f1948 1076 if (active_state->packet_timeout_ms == -1)
bc2d97c8 1077 continue;
1078 ms_subtract_diff(&start, &ms_remain);
1079 if (ms_remain <= 0) {
1080 ret = 0;
1081 break;
1082 }
1083 }
1084 if (ret == 0) {
1085 logit("Connection to %.200s timed out while "
1086 "waiting to read", get_remote_ipaddr());
1087 cleanup_exit(255);
1088 }
5260325f 1089 /* Read data from the socket. */
d0137ef8 1090 do {
1091 cont = 0;
1092 len = roaming_read(active_state->connection_in, buf,
1093 sizeof(buf), &cont);
1094 } while (len == 0 && cont);
89cafde6 1095 if (len == 0) {
bbe88b6d 1096 logit("Connection closed by %.200s", get_remote_ipaddr());
2362db19 1097 cleanup_exit(255);
89cafde6 1098 }
5260325f 1099 if (len < 0)
1100 fatal("Read from socket failed: %.100s", strerror(errno));
1101 /* Append it to the buffer. */
1102 packet_process_incoming(buf, len);
1103 }
1104 /* NOTREACHED */
8efc0c15 1105}
1106
24ca6821 1107int
54a5250f 1108packet_read(void)
24ca6821 1109{
54a5250f 1110 return packet_read_seqnr(NULL);
24ca6821 1111}
1112
aa3378df 1113/*
1114 * Waits until a packet has been received, verifies that its type matches
1115 * that given, and gives a fatal error and exits if there is a mismatch.
1116 */
8efc0c15 1117
1118void
54a5250f 1119packet_read_expect(int expected_type)
8efc0c15 1120{
5260325f 1121 int type;
8efc0c15 1122
54a5250f 1123 type = packet_read();
5260325f 1124 if (type != expected_type)
1125 packet_disconnect("Protocol error: expected packet type %d, got %d",
7e7327a1 1126 expected_type, type);
8efc0c15 1127}
1128
1129/* Checks if a full packet is available in the data received so far via
5260325f 1130 * packet_process_incoming. If so, reads the packet; otherwise returns
1131 * SSH_MSG_NONE. This does not wait for data from the connection.
1132 *
aacfb17b 1133 * SSH_MSG_DISCONNECT is handled specially here. Also,
1134 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1135 * to higher levels.
5260325f 1136 */
8efc0c15 1137
396c147e 1138static int
54a5250f 1139packet_read_poll1(void)
8efc0c15 1140{
1e3b8b07 1141 u_int len, padded_len;
a318bbf4 1142 u_char *cp, type;
1e3b8b07 1143 u_int checksum, stored_checksum;
5260325f 1144
5260325f 1145 /* Check if input size is less than minimum packet size. */
dc1f1948 1146 if (buffer_len(&active_state->input) < 4 + 8)
5260325f 1147 return SSH_MSG_NONE;
1148 /* Get length of incoming packet. */
dc1f1948 1149 cp = buffer_ptr(&active_state->input);
51e7a012 1150 len = get_u32(cp);
5260325f 1151 if (len < 1 + 2 + 2 || len > 256 * 1024)
2fe3c2db 1152 packet_disconnect("Bad packet length %u.", len);
5260325f 1153 padded_len = (len + 8) & ~7;
1154
1155 /* Check if the packet has been entirely received. */
dc1f1948 1156 if (buffer_len(&active_state->input) < 4 + padded_len)
5260325f 1157 return SSH_MSG_NONE;
1158
1159 /* The entire packet is in buffer. */
1160
1161 /* Consume packet length. */
dc1f1948 1162 buffer_consume(&active_state->input, 4);
5260325f 1163
08dcb5d7 1164 /*
1165 * Cryptographic attack detector for ssh
1166 * (C)1998 CORE-SDI, Buenos Aires Argentina
1167 * Ariel Futoransky(futo@core-sdi.com)
1168 */
dc1f1948 1169 if (!active_state->receive_context.plaintext) {
1170 switch (detect_attack(buffer_ptr(&active_state->input),
1171 padded_len)) {
f0d0e025 1172 case DEATTACK_DETECTED:
1173 packet_disconnect("crc32 compensation attack: "
1174 "network attack detected");
1175 case DEATTACK_DOS_DETECTED:
1176 packet_disconnect("deattack denial of "
1177 "service detected");
1178 }
1179 }
08dcb5d7 1180
1181 /* Decrypt data to incoming_packet. */
dc1f1948 1182 buffer_clear(&active_state->incoming_packet);
1183 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1184 cipher_crypt(&active_state->receive_context, cp,
1185 buffer_ptr(&active_state->input), padded_len);
08dcb5d7 1186
dc1f1948 1187 buffer_consume(&active_state->input, padded_len);
8efc0c15 1188
1189#ifdef PACKET_DEBUG
5260325f 1190 fprintf(stderr, "read_poll plain: ");
dc1f1948 1191 buffer_dump(&active_state->incoming_packet);
8efc0c15 1192#endif
5260325f 1193
1194 /* Compute packet checksum. */
dc1f1948 1195 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1196 buffer_len(&active_state->incoming_packet) - 4);
5260325f 1197
1198 /* Skip padding. */
dc1f1948 1199 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
5260325f 1200
1201 /* Test check bytes. */
dc1f1948 1202 if (len != buffer_len(&active_state->incoming_packet))
24ca6821 1203 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
dc1f1948 1204 len, buffer_len(&active_state->incoming_packet));
5260325f 1205
dc1f1948 1206 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
51e7a012 1207 stored_checksum = get_u32(cp);
5260325f 1208 if (checksum != stored_checksum)
1209 packet_disconnect("Corrupted check bytes on input.");
dc1f1948 1210 buffer_consume_end(&active_state->incoming_packet, 4);
1211
1212 if (active_state->packet_compression) {
1213 buffer_clear(&active_state->compression_buffer);
1214 buffer_uncompress(&active_state->incoming_packet,
1215 &active_state->compression_buffer);
1216 buffer_clear(&active_state->incoming_packet);
1217 buffer_append(&active_state->incoming_packet,
1218 buffer_ptr(&active_state->compression_buffer),
1219 buffer_len(&active_state->compression_buffer));
5260325f 1220 }
dc1f1948 1221 active_state->p_read.packets++;
1222 active_state->p_read.bytes += padded_len + 4;
1223 type = buffer_get_char(&active_state->incoming_packet);
750bbb35 1224 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1225 packet_disconnect("Invalid ssh1 packet type: %d", type);
08dcb5d7 1226 return type;
5260325f 1227}
1228
396c147e 1229static int
54a5250f 1230packet_read_poll2(u_int32_t *seqnr_p)
7e7327a1 1231{
1e3b8b07 1232 u_int padlen, need;
a318bbf4 1233 u_char *macbuf, *cp, type;
2ceb8101 1234 u_int maclen, block_size;
7e7327a1 1235 Enc *enc = NULL;
1236 Mac *mac = NULL;
1237 Comp *comp = NULL;
1238
dc1f1948 1239 if (active_state->packet_discard)
608bcf58 1240 return SSH_MSG_NONE;
1241
dc1f1948 1242 if (active_state->newkeys[MODE_IN] != NULL) {
1243 enc = &active_state->newkeys[MODE_IN]->enc;
1244 mac = &active_state->newkeys[MODE_IN]->mac;
1245 comp = &active_state->newkeys[MODE_IN]->comp;
7e7327a1 1246 }
1247 maclen = mac && mac->enabled ? mac->mac_len : 0;
3ee832e5 1248 block_size = enc ? enc->block_size : 8;
7e7327a1 1249
dc1f1948 1250 if (active_state->packlen == 0) {
7e7327a1 1251 /*
1252 * check if input size is less than the cipher block size,
1253 * decrypt first block and extract length of incoming packet
1254 */
dc1f1948 1255 if (buffer_len(&active_state->input) < block_size)
7e7327a1 1256 return SSH_MSG_NONE;
dc1f1948 1257 buffer_clear(&active_state->incoming_packet);
1258 cp = buffer_append_space(&active_state->incoming_packet,
7e7327a1 1259 block_size);
dc1f1948 1260 cipher_crypt(&active_state->receive_context, cp,
1261 buffer_ptr(&active_state->input), block_size);
1262 cp = buffer_ptr(&active_state->incoming_packet);
1263 active_state->packlen = get_u32(cp);
1264 if (active_state->packlen < 1 + 4 ||
1265 active_state->packlen > PACKET_MAX_SIZE) {
9adbb4a4 1266#ifdef PACKET_DEBUG
dc1f1948 1267 buffer_dump(&active_state->incoming_packet);
9adbb4a4 1268#endif
dc1f1948 1269 logit("Bad packet length %u.", active_state->packlen);
1270 packet_start_discard(enc, mac, active_state->packlen,
608bcf58 1271 PACKET_MAX_SIZE);
1272 return SSH_MSG_NONE;
7e7327a1 1273 }
dc1f1948 1274 DBG(debug("input: packet len %u", active_state->packlen+4));
1275 buffer_consume(&active_state->input, block_size);
7e7327a1 1276 }
1277 /* we have a partial packet of block_size bytes */
dc1f1948 1278 need = 4 + active_state->packlen - block_size;
7e7327a1 1279 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1280 need, maclen));
66af1c21 1281 if (need % block_size != 0) {
1282 logit("padding error: need %d block %d mod %d",
7e7327a1 1283 need, block_size, need % block_size);
dc1f1948 1284 packet_start_discard(enc, mac, active_state->packlen,
608bcf58 1285 PACKET_MAX_SIZE - block_size);
1286 return SSH_MSG_NONE;
66af1c21 1287 }
7e7327a1 1288 /*
1289 * check if the entire packet has been received and
1290 * decrypt into incoming_packet
1291 */
dc1f1948 1292 if (buffer_len(&active_state->input) < need + maclen)
7e7327a1 1293 return SSH_MSG_NONE;
1294#ifdef PACKET_DEBUG
1295 fprintf(stderr, "read_poll enc/full: ");
dc1f1948 1296 buffer_dump(&active_state->input);
7e7327a1 1297#endif
dc1f1948 1298 cp = buffer_append_space(&active_state->incoming_packet, need);
1299 cipher_crypt(&active_state->receive_context, cp,
1300 buffer_ptr(&active_state->input), need);
1301 buffer_consume(&active_state->input, need);
7e7327a1 1302 /*
1303 * compute MAC over seqnr and packet,
1304 * increment sequence number for incoming packet
1305 */
6ae2364d 1306 if (mac && mac->enabled) {
dc1f1948 1307 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1308 buffer_ptr(&active_state->incoming_packet),
1309 buffer_len(&active_state->incoming_packet));
1310 if (memcmp(macbuf, buffer_ptr(&active_state->input),
1311 mac->mac_len) != 0) {
608bcf58 1312 logit("Corrupted MAC on input.");
1313 if (need > PACKET_MAX_SIZE)
1314 fatal("internal error need %d", need);
dc1f1948 1315 packet_start_discard(enc, mac, active_state->packlen,
608bcf58 1316 PACKET_MAX_SIZE - need);
1317 return SSH_MSG_NONE;
1318 }
1319
dc1f1948 1320 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1321 buffer_consume(&active_state->input, mac->mac_len);
7e7327a1 1322 }
608bcf58 1323 /* XXX now it's safe to use fatal/packet_disconnect */
24ca6821 1324 if (seqnr_p != NULL)
dc1f1948 1325 *seqnr_p = active_state->p_read.seqnr;
1326 if (++active_state->p_read.seqnr == 0)
bbe88b6d 1327 logit("incoming seqnr wraps around");
dc1f1948 1328 if (++active_state->p_read.packets == 0)
ffd7b36b 1329 if (!(datafellows & SSH_BUG_NOREKEY))
1330 fatal("XXX too many packets with same key");
dc1f1948 1331 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1332 active_state->p_read.bytes += active_state->packlen + 4;
7e7327a1 1333
1334 /* get padlen */
dc1f1948 1335 cp = buffer_ptr(&active_state->incoming_packet);
a318bbf4 1336 padlen = cp[4];
7e7327a1 1337 DBG(debug("input: padlen %d", padlen));
1338 if (padlen < 4)
1339 packet_disconnect("Corrupted padlen %d on input.", padlen);
1340
1341 /* skip packet size + padlen, discard padding */
dc1f1948 1342 buffer_consume(&active_state->incoming_packet, 4 + 1);
1343 buffer_consume_end(&active_state->incoming_packet, padlen);
7e7327a1 1344
dc1f1948 1345 DBG(debug("input: len before de-compress %d",
1346 buffer_len(&active_state->incoming_packet)));
7e7327a1 1347 if (comp && comp->enabled) {
dc1f1948 1348 buffer_clear(&active_state->compression_buffer);
1349 buffer_uncompress(&active_state->incoming_packet,
1350 &active_state->compression_buffer);
1351 buffer_clear(&active_state->incoming_packet);
1352 buffer_append(&active_state->incoming_packet,
1353 buffer_ptr(&active_state->compression_buffer),
1354 buffer_len(&active_state->compression_buffer));
e1feb9bf 1355 DBG(debug("input: len after de-compress %d",
dc1f1948 1356 buffer_len(&active_state->incoming_packet)));
7e7327a1 1357 }
1358 /*
1359 * get packet type, implies consume.
1360 * return length of payload (without type field)
1361 */
dc1f1948 1362 type = buffer_get_char(&active_state->incoming_packet);
750bbb35 1363 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1364 packet_disconnect("Invalid ssh2 packet type: %d", type);
d1ac6175 1365 if (type == SSH2_MSG_NEWKEYS)
1366 set_newkeys(MODE_IN);
dc1f1948 1367 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1368 !active_state->server_side)
07200973 1369 packet_enable_delayed_compress();
7e7327a1 1370#ifdef PACKET_DEBUG
12bf85ed 1371 fprintf(stderr, "read/plain[%d]:\r\n", type);
dc1f1948 1372 buffer_dump(&active_state->incoming_packet);
7e7327a1 1373#endif
08dcb5d7 1374 /* reset for next packet */
dc1f1948 1375 active_state->packlen = 0;
08dcb5d7 1376 return type;
7e7327a1 1377}
1378
1379int
54a5250f 1380packet_read_poll_seqnr(u_int32_t *seqnr_p)
7e7327a1 1381{
0bc50167 1382 u_int reason, seqnr;
08dcb5d7 1383 u_char type;
7e7327a1 1384 char *msg;
7e7327a1 1385
08dcb5d7 1386 for (;;) {
1387 if (compat20) {
54a5250f 1388 type = packet_read_poll2(seqnr_p);
2abb1ef5 1389 if (type) {
dc1f1948 1390 active_state->keep_alive_timeouts = 0;
7e7327a1 1391 DBG(debug("received packet type %d", type));
2abb1ef5 1392 }
6aacefa7 1393 switch (type) {
aacfb17b 1394 case SSH2_MSG_IGNORE:
ac47b626 1395 debug3("Received SSH2_MSG_IGNORE");
aacfb17b 1396 break;
7e7327a1 1397 case SSH2_MSG_DEBUG:
1398 packet_get_char();
1399 msg = packet_get_string(NULL);
1400 debug("Remote: %.900s", msg);
1401 xfree(msg);
1402 msg = packet_get_string(NULL);
1403 xfree(msg);
1404 break;
1405 case SSH2_MSG_DISCONNECT:
1406 reason = packet_get_int();
1407 msg = packet_get_string(NULL);
bbe88b6d 1408 logit("Received disconnect from %s: %u: %.400s",
0bc50167 1409 get_remote_ipaddr(), reason, msg);
7e7327a1 1410 xfree(msg);
2362db19 1411 cleanup_exit(255);
7e7327a1 1412 break;
5a5f4c37 1413 case SSH2_MSG_UNIMPLEMENTED:
1414 seqnr = packet_get_int();
0bc50167 1415 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1416 seqnr);
aacfb17b 1417 break;
7e7327a1 1418 default:
1419 return type;
2b87da3b 1420 }
7e7327a1 1421 } else {
54a5250f 1422 type = packet_read_poll1();
6aacefa7 1423 switch (type) {
7e7327a1 1424 case SSH_MSG_IGNORE:
1425 break;
1426 case SSH_MSG_DEBUG:
1427 msg = packet_get_string(NULL);
1428 debug("Remote: %.900s", msg);
1429 xfree(msg);
1430 break;
1431 case SSH_MSG_DISCONNECT:
1432 msg = packet_get_string(NULL);
bbe88b6d 1433 logit("Received disconnect from %s: %.400s",
0bc50167 1434 get_remote_ipaddr(), msg);
2362db19 1435 cleanup_exit(255);
7e7327a1 1436 break;
1437 default:
08dcb5d7 1438 if (type)
7e7327a1 1439 DBG(debug("received packet type %d", type));
1440 return type;
2b87da3b 1441 }
7e7327a1 1442 }
1443 }
1444}
1445
24ca6821 1446int
54a5250f 1447packet_read_poll(void)
24ca6821 1448{
54a5250f 1449 return packet_read_poll_seqnr(NULL);
24ca6821 1450}
1451
aa3378df 1452/*
1453 * Buffers the given amount of input characters. This is intended to be used
1454 * together with packet_read_poll.
1455 */
8efc0c15 1456
1457void
1e3b8b07 1458packet_process_incoming(const char *buf, u_int len)
8efc0c15 1459{
dc1f1948 1460 if (active_state->packet_discard) {
1461 active_state->keep_alive_timeouts = 0; /* ?? */
1462 if (len >= active_state->packet_discard)
608bcf58 1463 packet_stop_discard();
dc1f1948 1464 active_state->packet_discard -= len;
608bcf58 1465 return;
1466 }
dc1f1948 1467 buffer_append(&active_state->input, buf, len);
8efc0c15 1468}
1469
1470/* Returns a character from the packet. */
1471
1e3b8b07 1472u_int
d5bb9418 1473packet_get_char(void)
8efc0c15 1474{
5260325f 1475 char ch;
e1feb9bf 1476
dc1f1948 1477 buffer_get(&active_state->incoming_packet, &ch, 1);
1e3b8b07 1478 return (u_char) ch;
8efc0c15 1479}
1480
1481/* Returns an integer from the packet data. */
1482
1e3b8b07 1483u_int
d5bb9418 1484packet_get_int(void)
8efc0c15 1485{
dc1f1948 1486 return buffer_get_int(&active_state->incoming_packet);
8efc0c15 1487}
1488
c6063ed7 1489/* Returns an 64 bit integer from the packet data. */
1490
1491u_int64_t
1492packet_get_int64(void)
1493{
1494 return buffer_get_int64(&active_state->incoming_packet);
1495}
1496
aa3378df 1497/*
1498 * Returns an arbitrary precision integer from the packet data. The integer
1499 * must have been initialized before this call.
1500 */
8efc0c15 1501
1502void
20b279e6 1503packet_get_bignum(BIGNUM * value)
8efc0c15 1504{
dc1f1948 1505 buffer_get_bignum(&active_state->incoming_packet, value);
8efc0c15 1506}
1507
7e7327a1 1508void
20b279e6 1509packet_get_bignum2(BIGNUM * value)
7e7327a1 1510{
dc1f1948 1511 buffer_get_bignum2(&active_state->incoming_packet, value);
7e7327a1 1512}
1513
6c0fa2b1 1514void *
2ceb8101 1515packet_get_raw(u_int *length_ptr)
7e7327a1 1516{
dc1f1948 1517 u_int bytes = buffer_len(&active_state->incoming_packet);
e1feb9bf 1518
7e7327a1 1519 if (length_ptr != NULL)
1520 *length_ptr = bytes;
dc1f1948 1521 return buffer_ptr(&active_state->incoming_packet);
7e7327a1 1522}
1523
6ae2364d 1524int
1525packet_remaining(void)
1526{
dc1f1948 1527 return buffer_len(&active_state->incoming_packet);
6ae2364d 1528}
1529
aa3378df 1530/*
1531 * Returns a string from the packet data. The string is allocated using
1532 * xmalloc; it is the responsibility of the calling program to free it when
1533 * no longer needed. The length_ptr argument may be NULL, or point to an
1534 * integer into which the length of the string is stored.
1535 */
8efc0c15 1536
6c0fa2b1 1537void *
1e3b8b07 1538packet_get_string(u_int *length_ptr)
8efc0c15 1539{
dc1f1948 1540 return buffer_get_string(&active_state->incoming_packet, length_ptr);
17f02f0a 1541}
1542
1543void *
1544packet_get_string_ptr(u_int *length_ptr)
1545{
dc1f1948 1546 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
8efc0c15 1547}
1548
aa3378df 1549/*
1550 * Sends a diagnostic message from the server to the client. This message
1551 * can be sent at any time (but not while constructing another message). The
1552 * message is printed immediately, but only if the client is being executed
1553 * in verbose mode. These messages are primarily intended to ease debugging
1554 * authentication problems. The length of the formatted message must not
1555 * exceed 1024 bytes. This will automatically call packet_write_wait.
1556 */
8efc0c15 1557
1558void
5260325f 1559packet_send_debug(const char *fmt,...)
8efc0c15 1560{
5260325f 1561 char buf[1024];
1562 va_list args;
1563
f72fc97f 1564 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1565 return;
1566
5260325f 1567 va_start(args, fmt);
1568 vsnprintf(buf, sizeof(buf), fmt, args);
1569 va_end(args);
1570
c4bc58eb 1571 if (compat20) {
1572 packet_start(SSH2_MSG_DEBUG);
1573 packet_put_char(0); /* bool: always display */
1574 packet_put_cstring(buf);
1575 packet_put_cstring("");
1576 } else {
1577 packet_start(SSH_MSG_DEBUG);
1578 packet_put_cstring(buf);
1579 }
5260325f 1580 packet_send();
1581 packet_write_wait();
8efc0c15 1582}
1583
aa3378df 1584/*
1585 * Logs the error plus constructs and sends a disconnect packet, closes the
1586 * connection, and exits. This function never returns. The error message
1587 * should not contain a newline. The length of the formatted message must
1588 * not exceed 1024 bytes.
1589 */
8efc0c15 1590
1591void
5260325f 1592packet_disconnect(const char *fmt,...)
1593{
1594 char buf[1024];
1595 va_list args;
1596 static int disconnecting = 0;
e1feb9bf 1597
5260325f 1598 if (disconnecting) /* Guard against recursive invocations. */
1599 fatal("packet_disconnect called recursively.");
1600 disconnecting = 1;
1601
aa3378df 1602 /*
1603 * Format the message. Note that the caller must make sure the
1604 * message is of limited size.
1605 */
5260325f 1606 va_start(args, fmt);
1607 vsnprintf(buf, sizeof(buf), fmt, args);
1608 va_end(args);
1609
2ccb7bde 1610 /* Display the error locally */
bbe88b6d 1611 logit("Disconnecting: %.100s", buf);
2ccb7bde 1612
5260325f 1613 /* Send the disconnect message to the other side, and wait for it to get sent. */
7e7327a1 1614 if (compat20) {
1615 packet_start(SSH2_MSG_DISCONNECT);
1616 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1617 packet_put_cstring(buf);
1618 packet_put_cstring("");
1619 } else {
1620 packet_start(SSH_MSG_DISCONNECT);
449c5ba5 1621 packet_put_cstring(buf);
7e7327a1 1622 }
5260325f 1623 packet_send();
1624 packet_write_wait();
1625
1626 /* Stop listening for connections. */
d6746a0b 1627 channel_close_all();
5260325f 1628
1629 /* Close the connection. */
1630 packet_close();
2362db19 1631 cleanup_exit(255);
8efc0c15 1632}
1633
aa3378df 1634/* Checks if there is any buffered output, and tries to write some of the output. */
8efc0c15 1635
1636void
d5bb9418 1637packet_write_poll(void)
8efc0c15 1638{
dc1f1948 1639 int len = buffer_len(&active_state->output);
d0137ef8 1640 int cont;
e1feb9bf 1641
5260325f 1642 if (len > 0) {
d0137ef8 1643 cont = 0;
1644 len = roaming_write(active_state->connection_out,
1645 buffer_ptr(&active_state->output), len, &cont);
d38d9a80 1646 if (len == -1) {
018fda87 1647 if (errno == EINTR || errno == EAGAIN ||
1648 errno == EWOULDBLOCK)
5260325f 1649 return;
d38d9a80 1650 fatal("Write failed: %.100s", strerror(errno));
5260325f 1651 }
d0137ef8 1652 if (len == 0 && !cont)
d38d9a80 1653 fatal("Write connection closed");
dc1f1948 1654 buffer_consume(&active_state->output, len);
5260325f 1655 }
8efc0c15 1656}
1657
aa3378df 1658/*
1659 * Calls packet_write_poll repeatedly until all pending output data has been
1660 * written.
1661 */
8efc0c15 1662
1663void
d5bb9418 1664packet_write_wait(void)
8efc0c15 1665{
20e04e90 1666 fd_set *setp;
bc2d97c8 1667 int ret, ms_remain;
1668 struct timeval start, timeout, *timeoutp = NULL;
20e04e90 1669
dc1f1948 1670 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1671 NFDBITS), sizeof(fd_mask));
5260325f 1672 packet_write_poll();
1673 while (packet_have_data_to_write()) {
dc1f1948 1674 memset(setp, 0, howmany(active_state->connection_out + 1,
1675 NFDBITS) * sizeof(fd_mask));
1676 FD_SET(active_state->connection_out, setp);
bc2d97c8 1677
dc1f1948 1678 if (active_state->packet_timeout_ms > 0) {
1679 ms_remain = active_state->packet_timeout_ms;
bc2d97c8 1680 timeoutp = &timeout;
1681 }
1682 for (;;) {
dc1f1948 1683 if (active_state->packet_timeout_ms != -1) {
bc2d97c8 1684 ms_to_timeval(&timeout, ms_remain);
1685 gettimeofday(&start, NULL);
1686 }
dc1f1948 1687 if ((ret = select(active_state->connection_out + 1,
1688 NULL, setp, NULL, timeoutp)) >= 0)
bc2d97c8 1689 break;
018fda87 1690 if (errno != EAGAIN && errno != EINTR &&
1691 errno != EWOULDBLOCK)
bc2d97c8 1692 break;
dc1f1948 1693 if (active_state->packet_timeout_ms == -1)
bc2d97c8 1694 continue;
1695 ms_subtract_diff(&start, &ms_remain);
1696 if (ms_remain <= 0) {
1697 ret = 0;
1698 break;
1699 }
1700 }
1701 if (ret == 0) {
1702 logit("Connection to %.200s timed out while "
1703 "waiting to write", get_remote_ipaddr());
1704 cleanup_exit(255);
1705 }
5260325f 1706 packet_write_poll();
1707 }
20e04e90 1708 xfree(setp);
8efc0c15 1709}
1710
1711/* Returns true if there is buffered data to write to the connection. */
1712
1713int
d5bb9418 1714packet_have_data_to_write(void)
8efc0c15 1715{
dc1f1948 1716 return buffer_len(&active_state->output) != 0;
8efc0c15 1717}
1718
1719/* Returns true if there is not too much data to write to the connection. */
1720
1721int
d5bb9418 1722packet_not_very_much_data_to_write(void)
8efc0c15 1723{
dc1f1948 1724 if (active_state->interactive_mode)
1725 return buffer_len(&active_state->output) < 16384;
5260325f 1726 else
dc1f1948 1727 return buffer_len(&active_state->output) < 128 * 1024;
8efc0c15 1728}
1729
9e637910 1730static void
48f636b2 1731packet_set_tos(int interactive)
1732{
00df6acd 1733#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
48f636b2 1734 int tos = interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
1735
1736 if (!packet_connection_is_on_socket() ||
1737 !packet_connection_is_ipv4())
1738 return;
dc1f1948 1739 if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos,
48f636b2 1740 sizeof(tos)) < 0)
1741 error("setsockopt IP_TOS %d: %.100s:",
1742 tos, strerror(errno));
4d0cb2e5 1743#endif
00df6acd 1744}
48f636b2 1745
8efc0c15 1746/* Informs that the current session is interactive. Sets IP flags for that. */
1747
1748void
b5c334cc 1749packet_set_interactive(int interactive)
8efc0c15 1750{
6a49252d 1751 if (active_state->set_interactive_called)
b5c334cc 1752 return;
6a49252d 1753 active_state->set_interactive_called = 1;
b5c334cc 1754
5260325f 1755 /* Record that we are in interactive mode. */
dc1f1948 1756 active_state->interactive_mode = interactive;
5260325f 1757
48e671d5 1758 /* Only set socket options if using a socket. */
1759 if (!packet_connection_is_on_socket())
aceb0423 1760 return;
dc1f1948 1761 set_nodelay(active_state->connection_in);
48f636b2 1762 packet_set_tos(interactive);
8efc0c15 1763}
1764
1765/* Returns true if the current connection is interactive. */
1766
1767int
d5bb9418 1768packet_is_interactive(void)
8efc0c15 1769{
dc1f1948 1770 return active_state->interactive_mode;
8efc0c15 1771}
9d6b7add 1772
f6be21a0 1773int
a27002e5 1774packet_set_maxsize(u_int s)
9d6b7add 1775{
6a49252d 1776 if (active_state->set_maxsize_called) {
bbe88b6d 1777 logit("packet_set_maxsize: called twice: old %d new %d",
dc1f1948 1778 active_state->max_packet_size, s);
5260325f 1779 return -1;
1780 }
1781 if (s < 4 * 1024 || s > 1024 * 1024) {
bbe88b6d 1782 logit("packet_set_maxsize: bad size %d", s);
5260325f 1783 return -1;
1784 }
6a49252d 1785 active_state->set_maxsize_called = 1;
76735fe3 1786 debug("packet_set_maxsize: setting to %d", s);
dc1f1948 1787 active_state->max_packet_size = s;
5260325f 1788 return s;
9d6b7add 1789}
a6215e53 1790
dc1f1948 1791int
1792packet_inc_alive_timeouts(void)
1793{
1794 return ++active_state->keep_alive_timeouts;
1795}
1796
1797void
1798packet_set_alive_timeouts(int ka)
1799{
1800 active_state->keep_alive_timeouts = ka;
1801}
1802
1803u_int
1804packet_get_maxsize(void)
1805{
1806 return active_state->max_packet_size;
1807}
1808
b4b701be 1809/* roundup current message to pad bytes */
1810void
1811packet_add_padding(u_char pad)
1812{
dc1f1948 1813 active_state->extra_pad = pad;
b4b701be 1814}
1815
a6215e53 1816/*
1817 * 9.2. Ignored Data Message
cd332296 1818 *
a6215e53 1819 * byte SSH_MSG_IGNORE
1820 * string data
cd332296 1821 *
a6215e53 1822 * All implementations MUST understand (and ignore) this message at any
1823 * time (after receiving the protocol version). No implementation is
1824 * required to send them. This message can be used as an additional
1825 * protection measure against advanced traffic analysis techniques.
1826 */
95ce5599 1827void
1828packet_send_ignore(int nbytes)
1829{
ca75d7de 1830 u_int32_t rnd = 0;
95ce5599 1831 int i;
1832
1833 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
a6215e53 1834 packet_put_int(nbytes);
184eed6a 1835 for (i = 0; i < nbytes; i++) {
a6215e53 1836 if (i % 4 == 0)
ca75d7de 1837 rnd = arc4random();
febd6f21 1838 packet_put_char((u_char)rnd & 0xff);
ca75d7de 1839 rnd >>= 8;
a6215e53 1840 }
1841}
ffd7b36b 1842
f6be21a0 1843#define MAX_PACKETS (1U<<31)
ffd7b36b 1844int
1845packet_need_rekeying(void)
1846{
1847 if (datafellows & SSH_BUG_NOREKEY)
1848 return 0;
1849 return
dc1f1948 1850 (active_state->p_send.packets > MAX_PACKETS) ||
1851 (active_state->p_read.packets > MAX_PACKETS) ||
1852 (active_state->max_blocks_out &&
1853 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1854 (active_state->max_blocks_in &&
1855 (active_state->p_read.blocks > active_state->max_blocks_in));
ffd7b36b 1856}
1857
1858void
1859packet_set_rekey_limit(u_int32_t bytes)
1860{
dc1f1948 1861 active_state->rekey_limit = bytes;
ffd7b36b 1862}
07200973 1863
1864void
1865packet_set_server(void)
1866{
dc1f1948 1867 active_state->server_side = 1;
07200973 1868}
1869
1870void
1871packet_set_authenticated(void)
1872{
dc1f1948 1873 active_state->after_authentication = 1;
1874}
1875
1876void *
1877packet_get_input(void)
1878{
1879 return (void *)&active_state->input;
1880}
1881
1882void *
1883packet_get_output(void)
1884{
1885 return (void *)&active_state->output;
1886}
1887
1888void *
1889packet_get_newkeys(int mode)
1890{
1891 return (void *)active_state->newkeys[mode];
07200973 1892}
5d82ad35 1893
1894/*
1895 * Save the state for the real connection, and use a separate state when
1896 * resuming a suspended connection.
1897 */
1898void
1899packet_backup_state(void)
1900{
1901 struct session_state *tmp;
1902
1903 close(active_state->connection_in);
1904 active_state->connection_in = -1;
1905 close(active_state->connection_out);
1906 active_state->connection_out = -1;
1907 if (backup_state)
1908 tmp = backup_state;
1909 else
1910 tmp = alloc_session_state();
1911 backup_state = active_state;
1912 active_state = tmp;
1913}
1914
1915/*
1916 * Swap in the old state when resuming a connecion.
1917 */
1918void
1919packet_restore_state(void)
1920{
1921 struct session_state *tmp;
1922 void *buf;
1923 u_int len;
1924
1925 tmp = backup_state;
1926 backup_state = active_state;
1927 active_state = tmp;
1928 active_state->connection_in = backup_state->connection_in;
1929 backup_state->connection_in = -1;
1930 active_state->connection_out = backup_state->connection_out;
1931 backup_state->connection_out = -1;
1932 len = buffer_len(&backup_state->input);
1933 if (len > 0) {
1934 buf = buffer_ptr(&backup_state->input);
1935 buffer_append(&active_state->input, buf, len);
1936 buffer_clear(&backup_state->input);
1937 add_recv_bytes(len);
1938 }
1939}
This page took 0.702999 seconds and 5 git commands to generate.