1 /* $OpenBSD: packet.c,v 1.176 2012/01/25 19:40:09 markus Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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.
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".
16 * SSH2 packet format added by Markus Friedl.
17 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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.
42 #include <sys/types.h>
43 #include "openbsd-compat/sys-queue.h"
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <arpa/inet.h>
88 #define PACKET_MAX_SIZE (256 * 1024)
98 TAILQ_ENTRY(packet) next;
103 struct session_state {
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.
113 /* Protocol flags for the remote side. */
114 u_int remote_protocol_flags;
116 /* Encryption context for receiving data. Only used for decryption. */
117 CipherContext receive_context;
119 /* Encryption context for sending data. Only used for encryption. */
120 CipherContext send_context;
122 /* Buffer for raw input data from the socket. */
125 /* Buffer for raw output data going to the socket. */
128 /* Buffer for the partial outgoing packet being constructed. */
129 Buffer outgoing_packet;
131 /* Buffer for the incoming packet currently being processed. */
132 Buffer incoming_packet;
134 /* Scratch buffer for packet compression/decompression. */
135 Buffer compression_buffer;
136 int compression_buffer_ready;
139 * Flag indicating whether packet compression/decompression is
142 int packet_compression;
144 /* default maximum packet size */
145 u_int max_packet_size;
147 /* Flag indicating whether this module has been initialized. */
150 /* Set to true if the connection is interactive. */
151 int interactive_mode;
153 /* Set to true if we are the server side. */
156 /* Set to true if we are authenticated. */
157 int after_authentication;
159 int keep_alive_timeouts;
161 /* The maximum time that we will wait to send or receive a packet */
162 int packet_timeout_ms;
164 /* Session key information for Encryption and MAC */
165 Newkeys *newkeys[MODE_MAX];
166 struct packet_state p_read, p_send;
168 u_int64_t max_blocks_in, max_blocks_out;
169 u_int32_t rekey_limit;
171 /* Session key for protocol v1 */
172 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
175 /* roundup current message to extra_pad bytes */
178 /* XXX discard incoming data after MAC error */
179 u_int packet_discard;
180 Mac *packet_discard_mac;
182 /* Used in packet_read_poll2() */
185 /* Used in packet_send2 */
188 /* Used in packet_set_interactive */
189 int set_interactive_called;
191 /* Used in packet_set_maxsize */
192 int set_maxsize_called;
194 TAILQ_HEAD(, packet) outgoing;
197 static struct session_state *active_state, *backup_state;
199 static struct session_state *
200 alloc_session_state(void)
202 struct session_state *s = xcalloc(1, sizeof(*s));
204 s->connection_in = -1;
205 s->connection_out = -1;
206 s->max_packet_size = 32768;
207 s->packet_timeout_ms = -1;
212 * Sets the descriptors used for communication. Disables encryption until
213 * packet_set_encryption_key is called.
216 packet_set_connection(int fd_in, int fd_out)
218 Cipher *none = cipher_by_name("none");
221 fatal("packet_set_connection: cannot load cipher 'none'");
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 *)"",
227 0, NULL, 0, CIPHER_ENCRYPT);
228 cipher_init(&active_state->receive_context, none, (const u_char *)"",
229 0, NULL, 0, CIPHER_DECRYPT);
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;
243 packet_set_timeout(int timeout, int count)
245 if (timeout <= 0 || count <= 0) {
246 active_state->packet_timeout_ms = -1;
249 if ((INT_MAX / 1000) / count < timeout)
250 active_state->packet_timeout_ms = INT_MAX;
252 active_state->packet_timeout_ms = timeout * count * 1000;
256 packet_stop_discard(void)
258 if (active_state->packet_discard_mac) {
261 memset(buf, 'a', sizeof(buf));
262 while (buffer_len(&active_state->incoming_packet) <
264 buffer_append(&active_state->incoming_packet, buf,
266 (void) mac_compute(active_state->packet_discard_mac,
267 active_state->p_read.seqnr,
268 buffer_ptr(&active_state->incoming_packet),
271 logit("Finished discarding for %.200s", get_remote_ipaddr());
276 packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
278 if (enc == NULL || !cipher_is_cbc(enc->cipher))
279 packet_disconnect("Packet corrupt");
280 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
281 active_state->packet_discard_mac = mac;
282 if (buffer_len(&active_state->input) >= discard)
283 packet_stop_discard();
284 active_state->packet_discard = discard -
285 buffer_len(&active_state->input);
288 /* Returns 1 if remote host is connected via socket, 0 if not. */
291 packet_connection_is_on_socket(void)
293 struct sockaddr_storage from, to;
294 socklen_t fromlen, tolen;
296 /* filedescriptors in and out are the same, so it's a socket */
297 if (active_state->connection_in == active_state->connection_out)
299 fromlen = sizeof(from);
300 memset(&from, 0, sizeof(from));
301 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
305 memset(&to, 0, sizeof(to));
306 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
309 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
311 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
317 * Exports an IV from the CipherContext required to export the key
318 * state back from the unprivileged child to the privileged parent
323 packet_get_keyiv(int mode, u_char *iv, u_int len)
327 if (mode == MODE_OUT)
328 cc = &active_state->send_context;
330 cc = &active_state->receive_context;
332 cipher_get_keyiv(cc, iv, len);
336 packet_get_keycontext(int mode, u_char *dat)
340 if (mode == MODE_OUT)
341 cc = &active_state->send_context;
343 cc = &active_state->receive_context;
345 return (cipher_get_keycontext(cc, dat));
349 packet_set_keycontext(int mode, u_char *dat)
353 if (mode == MODE_OUT)
354 cc = &active_state->send_context;
356 cc = &active_state->receive_context;
358 cipher_set_keycontext(cc, dat);
362 packet_get_keyiv_len(int mode)
366 if (mode == MODE_OUT)
367 cc = &active_state->send_context;
369 cc = &active_state->receive_context;
371 return (cipher_get_keyiv_len(cc));
375 packet_set_iv(int mode, u_char *dat)
379 if (mode == MODE_OUT)
380 cc = &active_state->send_context;
382 cc = &active_state->receive_context;
384 cipher_set_keyiv(cc, dat);
388 packet_get_ssh1_cipher(void)
390 return (cipher_get_number(active_state->receive_context.cipher));
394 packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
395 u_int32_t *packets, u_int64_t *bytes)
397 struct packet_state *state;
399 state = (mode == MODE_IN) ?
400 &active_state->p_read : &active_state->p_send;
402 *seqnr = state->seqnr;
404 *blocks = state->blocks;
406 *packets = state->packets;
408 *bytes = state->bytes;
412 packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
415 struct packet_state *state;
417 state = (mode == MODE_IN) ?
418 &active_state->p_read : &active_state->p_send;
419 state->seqnr = seqnr;
420 state->blocks = blocks;
421 state->packets = packets;
422 state->bytes = bytes;
426 packet_connection_af(void)
428 struct sockaddr_storage to;
429 socklen_t tolen = sizeof(to);
431 memset(&to, 0, sizeof(to));
432 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
436 if (to.ss_family == AF_INET6 &&
437 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
443 /* Sets the connection into non-blocking mode. */
446 packet_set_nonblocking(void)
448 /* Set the socket into non-blocking mode. */
449 set_nonblock(active_state->connection_in);
451 if (active_state->connection_out != active_state->connection_in)
452 set_nonblock(active_state->connection_out);
455 /* Returns the socket used for reading. */
458 packet_get_connection_in(void)
460 return active_state->connection_in;
463 /* Returns the descriptor used for writing. */
466 packet_get_connection_out(void)
468 return active_state->connection_out;
471 /* Closes the connection and clears and frees internal data structures. */
476 if (!active_state->initialized)
478 active_state->initialized = 0;
479 if (active_state->connection_in == active_state->connection_out) {
480 shutdown(active_state->connection_out, SHUT_RDWR);
481 close(active_state->connection_out);
483 close(active_state->connection_in);
484 close(active_state->connection_out);
486 buffer_free(&active_state->input);
487 buffer_free(&active_state->output);
488 buffer_free(&active_state->outgoing_packet);
489 buffer_free(&active_state->incoming_packet);
490 if (active_state->compression_buffer_ready) {
491 buffer_free(&active_state->compression_buffer);
492 buffer_compress_uninit();
494 cipher_cleanup(&active_state->send_context);
495 cipher_cleanup(&active_state->receive_context);
498 /* Sets remote side protocol flags. */
501 packet_set_protocol_flags(u_int protocol_flags)
503 active_state->remote_protocol_flags = protocol_flags;
506 /* Returns the remote protocol flags set earlier by the above function. */
509 packet_get_protocol_flags(void)
511 return active_state->remote_protocol_flags;
515 * Starts packet compression from the next packet on in both directions.
516 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
520 packet_init_compression(void)
522 if (active_state->compression_buffer_ready == 1)
524 active_state->compression_buffer_ready = 1;
525 buffer_init(&active_state->compression_buffer);
529 packet_start_compression(int level)
531 if (active_state->packet_compression && !compat20)
532 fatal("Compression already enabled.");
533 active_state->packet_compression = 1;
534 packet_init_compression();
535 buffer_compress_init_send(level);
536 buffer_compress_init_recv();
540 * Causes any further packets to be encrypted using the given key. The same
541 * key is used for both sending and reception. However, both directions are
542 * encrypted independently of each other.
546 packet_set_encryption_key(const u_char *key, u_int keylen, int number)
548 Cipher *cipher = cipher_by_number(number);
551 fatal("packet_set_encryption_key: unknown cipher number %d", number);
553 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
554 if (keylen > SSH_SESSION_KEY_LENGTH)
555 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
556 memcpy(active_state->ssh1_key, key, keylen);
557 active_state->ssh1_keylen = keylen;
558 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
560 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
565 packet_get_encryption_key(u_char *key)
568 return (active_state->ssh1_keylen);
569 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
570 return (active_state->ssh1_keylen);
573 /* Start constructing a packet to send. */
575 packet_start(u_char type)
580 DBG(debug("packet_start[%d]", type));
581 len = compat20 ? 6 : 9;
582 memset(buf, 0, len - 1);
584 buffer_clear(&active_state->outgoing_packet);
585 buffer_append(&active_state->outgoing_packet, buf, len);
588 /* Append payload. */
590 packet_put_char(int value)
594 buffer_append(&active_state->outgoing_packet, &ch, 1);
598 packet_put_int(u_int value)
600 buffer_put_int(&active_state->outgoing_packet, value);
604 packet_put_int64(u_int64_t value)
606 buffer_put_int64(&active_state->outgoing_packet, value);
610 packet_put_string(const void *buf, u_int len)
612 buffer_put_string(&active_state->outgoing_packet, buf, len);
616 packet_put_cstring(const char *str)
618 buffer_put_cstring(&active_state->outgoing_packet, str);
622 packet_put_raw(const void *buf, u_int len)
624 buffer_append(&active_state->outgoing_packet, buf, len);
628 packet_put_bignum(BIGNUM * value)
630 buffer_put_bignum(&active_state->outgoing_packet, value);
634 packet_put_bignum2(BIGNUM * value)
636 buffer_put_bignum2(&active_state->outgoing_packet, value);
639 #ifdef OPENSSL_HAS_ECC
641 packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
643 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
648 * Finalizes and sends the packet. If the encryption key has been set,
649 * encrypts the packet before sending.
661 * If using packet compression, compress the payload of the outgoing
664 if (active_state->packet_compression) {
665 buffer_clear(&active_state->compression_buffer);
667 buffer_consume(&active_state->outgoing_packet, 8);
669 buffer_append(&active_state->compression_buffer,
670 "\0\0\0\0\0\0\0\0", 8);
671 buffer_compress(&active_state->outgoing_packet,
672 &active_state->compression_buffer);
673 buffer_clear(&active_state->outgoing_packet);
674 buffer_append(&active_state->outgoing_packet,
675 buffer_ptr(&active_state->compression_buffer),
676 buffer_len(&active_state->compression_buffer));
678 /* Compute packet length without padding (add checksum, remove padding). */
679 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
681 /* Insert padding. Initialized to zero in packet_start1() */
682 padding = 8 - len % 8;
683 if (!active_state->send_context.plaintext) {
684 cp = buffer_ptr(&active_state->outgoing_packet);
685 for (i = 0; i < padding; i++) {
688 cp[7 - i] = rnd & 0xff;
692 buffer_consume(&active_state->outgoing_packet, 8 - padding);
694 /* Add check bytes. */
695 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
696 buffer_len(&active_state->outgoing_packet));
697 put_u32(buf, checksum);
698 buffer_append(&active_state->outgoing_packet, buf, 4);
701 fprintf(stderr, "packet_send plain: ");
702 buffer_dump(&active_state->outgoing_packet);
705 /* Append to output. */
707 buffer_append(&active_state->output, buf, 4);
708 cp = buffer_append_space(&active_state->output,
709 buffer_len(&active_state->outgoing_packet));
710 cipher_crypt(&active_state->send_context, cp,
711 buffer_ptr(&active_state->outgoing_packet),
712 buffer_len(&active_state->outgoing_packet));
715 fprintf(stderr, "encrypted: ");
716 buffer_dump(&active_state->output);
718 active_state->p_send.packets++;
719 active_state->p_send.bytes += len +
720 buffer_len(&active_state->outgoing_packet);
721 buffer_clear(&active_state->outgoing_packet);
724 * Note that the packet is now only buffered in output. It won't be
725 * actually sent until packet_write_wait or packet_write_poll is
731 set_newkeys(int mode)
737 u_int64_t *max_blocks;
740 debug2("set_newkeys: mode %d", mode);
742 if (mode == MODE_OUT) {
743 cc = &active_state->send_context;
744 crypt_type = CIPHER_ENCRYPT;
745 active_state->p_send.packets = active_state->p_send.blocks = 0;
746 max_blocks = &active_state->max_blocks_out;
748 cc = &active_state->receive_context;
749 crypt_type = CIPHER_DECRYPT;
750 active_state->p_read.packets = active_state->p_read.blocks = 0;
751 max_blocks = &active_state->max_blocks_in;
753 if (active_state->newkeys[mode] != NULL) {
754 debug("set_newkeys: rekeying");
756 enc = &active_state->newkeys[mode]->enc;
757 mac = &active_state->newkeys[mode]->mac;
758 comp = &active_state->newkeys[mode]->comp;
766 xfree(active_state->newkeys[mode]);
768 active_state->newkeys[mode] = kex_get_newkeys(mode);
769 if (active_state->newkeys[mode] == NULL)
770 fatal("newkeys: no keys for mode %d", mode);
771 enc = &active_state->newkeys[mode]->enc;
772 mac = &active_state->newkeys[mode]->mac;
773 comp = &active_state->newkeys[mode]->comp;
774 if (mac_init(mac) == 0)
776 DBG(debug("cipher_init_context: %d", mode));
777 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
778 enc->iv, enc->block_size, crypt_type);
779 /* Deleting the keys does not gain extra security */
780 /* memset(enc->iv, 0, enc->block_size);
781 memset(enc->key, 0, enc->key_len);
782 memset(mac->key, 0, mac->key_len); */
783 if ((comp->type == COMP_ZLIB ||
784 (comp->type == COMP_DELAYED &&
785 active_state->after_authentication)) && comp->enabled == 0) {
786 packet_init_compression();
787 if (mode == MODE_OUT)
788 buffer_compress_init_send(6);
790 buffer_compress_init_recv();
794 * The 2^(blocksize*2) limit is too expensive for 3DES,
795 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
797 if (enc->block_size >= 16)
798 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
800 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
801 if (active_state->rekey_limit)
802 *max_blocks = MIN(*max_blocks,
803 active_state->rekey_limit / enc->block_size);
807 * Delayed compression for SSH2 is enabled after authentication:
808 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
809 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
812 packet_enable_delayed_compress(void)
818 * Remember that we are past the authentication step, so rekeying
819 * with COMP_DELAYED will turn on compression immediately.
821 active_state->after_authentication = 1;
822 for (mode = 0; mode < MODE_MAX; mode++) {
823 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
824 if (active_state->newkeys[mode] == NULL)
826 comp = &active_state->newkeys[mode]->comp;
827 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
828 packet_init_compression();
829 if (mode == MODE_OUT)
830 buffer_compress_init_send(6);
832 buffer_compress_init_recv();
839 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
842 packet_send2_wrapped(void)
844 u_char type, *cp, *macbuf = NULL;
846 u_int packet_length = 0;
854 if (active_state->newkeys[MODE_OUT] != NULL) {
855 enc = &active_state->newkeys[MODE_OUT]->enc;
856 mac = &active_state->newkeys[MODE_OUT]->mac;
857 comp = &active_state->newkeys[MODE_OUT]->comp;
859 block_size = enc ? enc->block_size : 8;
861 cp = buffer_ptr(&active_state->outgoing_packet);
865 fprintf(stderr, "plain: ");
866 buffer_dump(&active_state->outgoing_packet);
869 if (comp && comp->enabled) {
870 len = buffer_len(&active_state->outgoing_packet);
871 /* skip header, compress only payload */
872 buffer_consume(&active_state->outgoing_packet, 5);
873 buffer_clear(&active_state->compression_buffer);
874 buffer_compress(&active_state->outgoing_packet,
875 &active_state->compression_buffer);
876 buffer_clear(&active_state->outgoing_packet);
877 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
878 buffer_append(&active_state->outgoing_packet,
879 buffer_ptr(&active_state->compression_buffer),
880 buffer_len(&active_state->compression_buffer));
881 DBG(debug("compression: raw %d compressed %d", len,
882 buffer_len(&active_state->outgoing_packet)));
885 /* sizeof (packet_len + pad_len + payload) */
886 len = buffer_len(&active_state->outgoing_packet);
889 * calc size of padding, alloc space, get random data,
890 * minimum padding is 4 bytes
892 padlen = block_size - (len % block_size);
894 padlen += block_size;
895 if (active_state->extra_pad) {
896 /* will wrap if extra_pad+padlen > 255 */
897 active_state->extra_pad =
898 roundup(active_state->extra_pad, block_size);
899 pad = active_state->extra_pad -
900 ((len + padlen) % active_state->extra_pad);
901 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
902 pad, len, padlen, active_state->extra_pad);
904 active_state->extra_pad = 0;
906 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
907 if (enc && !active_state->send_context.plaintext) {
909 for (i = 0; i < padlen; i++) {
917 memset(cp, 0, padlen);
919 /* packet_length includes payload, padding and padding length field */
920 packet_length = buffer_len(&active_state->outgoing_packet) - 4;
921 cp = buffer_ptr(&active_state->outgoing_packet);
922 put_u32(cp, packet_length);
924 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
926 /* compute MAC over seqnr and packet(length fields, payload, padding) */
927 if (mac && mac->enabled) {
928 macbuf = mac_compute(mac, active_state->p_send.seqnr,
929 buffer_ptr(&active_state->outgoing_packet),
930 buffer_len(&active_state->outgoing_packet));
931 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
933 /* encrypt packet and append to output buffer. */
934 cp = buffer_append_space(&active_state->output,
935 buffer_len(&active_state->outgoing_packet));
936 cipher_crypt(&active_state->send_context, cp,
937 buffer_ptr(&active_state->outgoing_packet),
938 buffer_len(&active_state->outgoing_packet));
939 /* append unencrypted MAC */
940 if (mac && mac->enabled)
941 buffer_append(&active_state->output, macbuf, mac->mac_len);
943 fprintf(stderr, "encrypted: ");
944 buffer_dump(&active_state->output);
946 /* increment sequence number for outgoing packets */
947 if (++active_state->p_send.seqnr == 0)
948 logit("outgoing seqnr wraps around");
949 if (++active_state->p_send.packets == 0)
950 if (!(datafellows & SSH_BUG_NOREKEY))
951 fatal("XXX too many packets with same key");
952 active_state->p_send.blocks += (packet_length + 4) / block_size;
953 active_state->p_send.bytes += packet_length + 4;
954 buffer_clear(&active_state->outgoing_packet);
956 if (type == SSH2_MSG_NEWKEYS)
957 set_newkeys(MODE_OUT);
958 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
959 packet_enable_delayed_compress();
960 return(packet_length);
966 static int packet_length = 0;
970 cp = buffer_ptr(&active_state->outgoing_packet);
973 /* during rekeying we can only send key exchange messages */
974 if (active_state->rekeying) {
975 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
976 (type > SSH2_MSG_TRANSPORT_MAX) ||
977 (type == SSH2_MSG_SERVICE_REQUEST) ||
978 (type == SSH2_MSG_SERVICE_ACCEPT)) {
979 debug("enqueue packet: %u", type);
980 p = xmalloc(sizeof(*p));
982 memcpy(&p->payload, &active_state->outgoing_packet,
984 buffer_init(&active_state->outgoing_packet);
985 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
986 return(sizeof(Buffer));
990 /* rekeying starts with sending KEXINIT */
991 if (type == SSH2_MSG_KEXINIT)
992 active_state->rekeying = 1;
994 packet_length = packet_send2_wrapped();
996 /* after a NEWKEYS message we can send the complete queue */
997 if (type == SSH2_MSG_NEWKEYS) {
998 active_state->rekeying = 0;
999 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
1001 debug("dequeue packet: %u", type);
1002 buffer_free(&active_state->outgoing_packet);
1003 memcpy(&active_state->outgoing_packet, &p->payload,
1005 TAILQ_REMOVE(&active_state->outgoing, p, next);
1007 packet_length += packet_send2_wrapped();
1010 return(packet_length);
1018 packet_len = packet_send2();
1021 DBG(debug("packet_send done"));
1026 * Waits until a packet has been received, and returns its type. Note that
1027 * no other data is processed until this returns, so this function should not
1028 * be used during the interactive session.
1032 packet_read_seqnr(u_int32_t *seqnr_p)
1034 int type, len, ret, ms_remain, cont;
1037 struct timeval timeout, start, *timeoutp = NULL;
1039 DBG(debug("packet_read()"));
1041 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1042 NFDBITS), sizeof(fd_mask));
1044 /* Since we are blocking, ensure that all written packets have been sent. */
1045 packet_write_wait();
1047 /* Stay in the loop until we have received a complete packet. */
1049 /* Try to read a packet from the buffer. */
1050 type = packet_read_poll_seqnr(seqnr_p);
1052 type == SSH_SMSG_SUCCESS
1053 || type == SSH_SMSG_FAILURE
1054 || type == SSH_CMSG_EOF
1055 || type == SSH_CMSG_EXIT_CONFIRMATION))
1057 /* If we got a packet, return it. */
1058 if (type != SSH_MSG_NONE) {
1063 * Otherwise, wait for some data to arrive, add it to the
1064 * buffer, and try again.
1066 memset(setp, 0, howmany(active_state->connection_in + 1,
1067 NFDBITS) * sizeof(fd_mask));
1068 FD_SET(active_state->connection_in, setp);
1070 if (active_state->packet_timeout_ms > 0) {
1071 ms_remain = active_state->packet_timeout_ms;
1072 timeoutp = &timeout;
1074 /* Wait for some data to arrive. */
1076 if (active_state->packet_timeout_ms != -1) {
1077 ms_to_timeval(&timeout, ms_remain);
1078 gettimeofday(&start, NULL);
1080 if ((ret = select(active_state->connection_in + 1, setp,
1081 NULL, NULL, timeoutp)) >= 0)
1083 if (errno != EAGAIN && errno != EINTR &&
1084 errno != EWOULDBLOCK)
1086 if (active_state->packet_timeout_ms == -1)
1088 ms_subtract_diff(&start, &ms_remain);
1089 if (ms_remain <= 0) {
1095 logit("Connection to %.200s timed out while "
1096 "waiting to read", get_remote_ipaddr());
1099 /* Read data from the socket. */
1102 len = roaming_read(active_state->connection_in, buf,
1103 sizeof(buf), &cont);
1104 } while (len == 0 && cont);
1106 logit("Connection closed by %.200s", get_remote_ipaddr());
1110 fatal("Read from socket failed: %.100s", strerror(errno));
1111 /* Append it to the buffer. */
1112 packet_process_incoming(buf, len);
1120 return packet_read_seqnr(NULL);
1124 * Waits until a packet has been received, verifies that its type matches
1125 * that given, and gives a fatal error and exits if there is a mismatch.
1129 packet_read_expect(int expected_type)
1133 type = packet_read();
1134 if (type != expected_type)
1135 packet_disconnect("Protocol error: expected packet type %d, got %d",
1136 expected_type, type);
1139 /* Checks if a full packet is available in the data received so far via
1140 * packet_process_incoming. If so, reads the packet; otherwise returns
1141 * SSH_MSG_NONE. This does not wait for data from the connection.
1143 * SSH_MSG_DISCONNECT is handled specially here. Also,
1144 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1149 packet_read_poll1(void)
1151 u_int len, padded_len;
1153 u_int checksum, stored_checksum;
1155 /* Check if input size is less than minimum packet size. */
1156 if (buffer_len(&active_state->input) < 4 + 8)
1157 return SSH_MSG_NONE;
1158 /* Get length of incoming packet. */
1159 cp = buffer_ptr(&active_state->input);
1161 if (len < 1 + 2 + 2 || len > 256 * 1024)
1162 packet_disconnect("Bad packet length %u.", len);
1163 padded_len = (len + 8) & ~7;
1165 /* Check if the packet has been entirely received. */
1166 if (buffer_len(&active_state->input) < 4 + padded_len)
1167 return SSH_MSG_NONE;
1169 /* The entire packet is in buffer. */
1171 /* Consume packet length. */
1172 buffer_consume(&active_state->input, 4);
1175 * Cryptographic attack detector for ssh
1176 * (C)1998 CORE-SDI, Buenos Aires Argentina
1177 * Ariel Futoransky(futo@core-sdi.com)
1179 if (!active_state->receive_context.plaintext) {
1180 switch (detect_attack(buffer_ptr(&active_state->input),
1182 case DEATTACK_DETECTED:
1183 packet_disconnect("crc32 compensation attack: "
1184 "network attack detected");
1185 case DEATTACK_DOS_DETECTED:
1186 packet_disconnect("deattack denial of "
1187 "service detected");
1191 /* Decrypt data to incoming_packet. */
1192 buffer_clear(&active_state->incoming_packet);
1193 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1194 cipher_crypt(&active_state->receive_context, cp,
1195 buffer_ptr(&active_state->input), padded_len);
1197 buffer_consume(&active_state->input, padded_len);
1200 fprintf(stderr, "read_poll plain: ");
1201 buffer_dump(&active_state->incoming_packet);
1204 /* Compute packet checksum. */
1205 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1206 buffer_len(&active_state->incoming_packet) - 4);
1209 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
1211 /* Test check bytes. */
1212 if (len != buffer_len(&active_state->incoming_packet))
1213 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
1214 len, buffer_len(&active_state->incoming_packet));
1216 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
1217 stored_checksum = get_u32(cp);
1218 if (checksum != stored_checksum)
1219 packet_disconnect("Corrupted check bytes on input.");
1220 buffer_consume_end(&active_state->incoming_packet, 4);
1222 if (active_state->packet_compression) {
1223 buffer_clear(&active_state->compression_buffer);
1224 buffer_uncompress(&active_state->incoming_packet,
1225 &active_state->compression_buffer);
1226 buffer_clear(&active_state->incoming_packet);
1227 buffer_append(&active_state->incoming_packet,
1228 buffer_ptr(&active_state->compression_buffer),
1229 buffer_len(&active_state->compression_buffer));
1231 active_state->p_read.packets++;
1232 active_state->p_read.bytes += padded_len + 4;
1233 type = buffer_get_char(&active_state->incoming_packet);
1234 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1235 packet_disconnect("Invalid ssh1 packet type: %d", type);
1240 packet_read_poll2(u_int32_t *seqnr_p)
1243 u_char *macbuf, *cp, type;
1244 u_int maclen, block_size;
1249 if (active_state->packet_discard)
1250 return SSH_MSG_NONE;
1252 if (active_state->newkeys[MODE_IN] != NULL) {
1253 enc = &active_state->newkeys[MODE_IN]->enc;
1254 mac = &active_state->newkeys[MODE_IN]->mac;
1255 comp = &active_state->newkeys[MODE_IN]->comp;
1257 maclen = mac && mac->enabled ? mac->mac_len : 0;
1258 block_size = enc ? enc->block_size : 8;
1260 if (active_state->packlen == 0) {
1262 * check if input size is less than the cipher block size,
1263 * decrypt first block and extract length of incoming packet
1265 if (buffer_len(&active_state->input) < block_size)
1266 return SSH_MSG_NONE;
1267 buffer_clear(&active_state->incoming_packet);
1268 cp = buffer_append_space(&active_state->incoming_packet,
1270 cipher_crypt(&active_state->receive_context, cp,
1271 buffer_ptr(&active_state->input), block_size);
1272 cp = buffer_ptr(&active_state->incoming_packet);
1273 active_state->packlen = get_u32(cp);
1274 if (active_state->packlen < 1 + 4 ||
1275 active_state->packlen > PACKET_MAX_SIZE) {
1277 buffer_dump(&active_state->incoming_packet);
1279 logit("Bad packet length %u.", active_state->packlen);
1280 packet_start_discard(enc, mac, active_state->packlen,
1282 return SSH_MSG_NONE;
1284 DBG(debug("input: packet len %u", active_state->packlen+4));
1285 buffer_consume(&active_state->input, block_size);
1287 /* we have a partial packet of block_size bytes */
1288 need = 4 + active_state->packlen - block_size;
1289 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
1291 if (need % block_size != 0) {
1292 logit("padding error: need %d block %d mod %d",
1293 need, block_size, need % block_size);
1294 packet_start_discard(enc, mac, active_state->packlen,
1295 PACKET_MAX_SIZE - block_size);
1296 return SSH_MSG_NONE;
1299 * check if the entire packet has been received and
1300 * decrypt into incoming_packet
1302 if (buffer_len(&active_state->input) < need + maclen)
1303 return SSH_MSG_NONE;
1305 fprintf(stderr, "read_poll enc/full: ");
1306 buffer_dump(&active_state->input);
1308 cp = buffer_append_space(&active_state->incoming_packet, need);
1309 cipher_crypt(&active_state->receive_context, cp,
1310 buffer_ptr(&active_state->input), need);
1311 buffer_consume(&active_state->input, need);
1313 * compute MAC over seqnr and packet,
1314 * increment sequence number for incoming packet
1316 if (mac && mac->enabled) {
1317 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1318 buffer_ptr(&active_state->incoming_packet),
1319 buffer_len(&active_state->incoming_packet));
1320 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
1321 mac->mac_len) != 0) {
1322 logit("Corrupted MAC on input.");
1323 if (need > PACKET_MAX_SIZE)
1324 fatal("internal error need %d", need);
1325 packet_start_discard(enc, mac, active_state->packlen,
1326 PACKET_MAX_SIZE - need);
1327 return SSH_MSG_NONE;
1330 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1331 buffer_consume(&active_state->input, mac->mac_len);
1333 /* XXX now it's safe to use fatal/packet_disconnect */
1334 if (seqnr_p != NULL)
1335 *seqnr_p = active_state->p_read.seqnr;
1336 if (++active_state->p_read.seqnr == 0)
1337 logit("incoming seqnr wraps around");
1338 if (++active_state->p_read.packets == 0)
1339 if (!(datafellows & SSH_BUG_NOREKEY))
1340 fatal("XXX too many packets with same key");
1341 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1342 active_state->p_read.bytes += active_state->packlen + 4;
1345 cp = buffer_ptr(&active_state->incoming_packet);
1347 DBG(debug("input: padlen %d", padlen));
1349 packet_disconnect("Corrupted padlen %d on input.", padlen);
1351 /* skip packet size + padlen, discard padding */
1352 buffer_consume(&active_state->incoming_packet, 4 + 1);
1353 buffer_consume_end(&active_state->incoming_packet, padlen);
1355 DBG(debug("input: len before de-compress %d",
1356 buffer_len(&active_state->incoming_packet)));
1357 if (comp && comp->enabled) {
1358 buffer_clear(&active_state->compression_buffer);
1359 buffer_uncompress(&active_state->incoming_packet,
1360 &active_state->compression_buffer);
1361 buffer_clear(&active_state->incoming_packet);
1362 buffer_append(&active_state->incoming_packet,
1363 buffer_ptr(&active_state->compression_buffer),
1364 buffer_len(&active_state->compression_buffer));
1365 DBG(debug("input: len after de-compress %d",
1366 buffer_len(&active_state->incoming_packet)));
1369 * get packet type, implies consume.
1370 * return length of payload (without type field)
1372 type = buffer_get_char(&active_state->incoming_packet);
1373 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1374 packet_disconnect("Invalid ssh2 packet type: %d", type);
1375 if (type == SSH2_MSG_NEWKEYS)
1376 set_newkeys(MODE_IN);
1377 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1378 !active_state->server_side)
1379 packet_enable_delayed_compress();
1381 fprintf(stderr, "read/plain[%d]:\r\n", type);
1382 buffer_dump(&active_state->incoming_packet);
1384 /* reset for next packet */
1385 active_state->packlen = 0;
1390 packet_read_poll_seqnr(u_int32_t *seqnr_p)
1392 u_int reason, seqnr;
1398 type = packet_read_poll2(seqnr_p);
1400 active_state->keep_alive_timeouts = 0;
1401 DBG(debug("received packet type %d", type));
1404 case SSH2_MSG_IGNORE:
1405 debug3("Received SSH2_MSG_IGNORE");
1407 case SSH2_MSG_DEBUG:
1409 msg = packet_get_string(NULL);
1410 debug("Remote: %.900s", msg);
1412 msg = packet_get_string(NULL);
1415 case SSH2_MSG_DISCONNECT:
1416 reason = packet_get_int();
1417 msg = packet_get_string(NULL);
1418 logit("Received disconnect from %s: %u: %.400s",
1419 get_remote_ipaddr(), reason, msg);
1423 case SSH2_MSG_UNIMPLEMENTED:
1424 seqnr = packet_get_int();
1425 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1432 type = packet_read_poll1();
1434 case SSH_MSG_IGNORE:
1437 msg = packet_get_string(NULL);
1438 debug("Remote: %.900s", msg);
1441 case SSH_MSG_DISCONNECT:
1442 msg = packet_get_string(NULL);
1443 logit("Received disconnect from %s: %.400s",
1444 get_remote_ipaddr(), msg);
1449 DBG(debug("received packet type %d", type));
1457 * Buffers the given amount of input characters. This is intended to be used
1458 * together with packet_read_poll.
1462 packet_process_incoming(const char *buf, u_int len)
1464 if (active_state->packet_discard) {
1465 active_state->keep_alive_timeouts = 0; /* ?? */
1466 if (len >= active_state->packet_discard)
1467 packet_stop_discard();
1468 active_state->packet_discard -= len;
1471 buffer_append(&active_state->input, buf, len);
1474 /* Returns a character from the packet. */
1477 packet_get_char(void)
1481 buffer_get(&active_state->incoming_packet, &ch, 1);
1485 /* Returns an integer from the packet data. */
1488 packet_get_int(void)
1490 return buffer_get_int(&active_state->incoming_packet);
1493 /* Returns an 64 bit integer from the packet data. */
1496 packet_get_int64(void)
1498 return buffer_get_int64(&active_state->incoming_packet);
1502 * Returns an arbitrary precision integer from the packet data. The integer
1503 * must have been initialized before this call.
1507 packet_get_bignum(BIGNUM * value)
1509 buffer_get_bignum(&active_state->incoming_packet, value);
1513 packet_get_bignum2(BIGNUM * value)
1515 buffer_get_bignum2(&active_state->incoming_packet, value);
1518 #ifdef OPENSSL_HAS_ECC
1520 packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1522 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1527 packet_get_raw(u_int *length_ptr)
1529 u_int bytes = buffer_len(&active_state->incoming_packet);
1531 if (length_ptr != NULL)
1532 *length_ptr = bytes;
1533 return buffer_ptr(&active_state->incoming_packet);
1537 packet_remaining(void)
1539 return buffer_len(&active_state->incoming_packet);
1543 * Returns a string from the packet data. The string is allocated using
1544 * xmalloc; it is the responsibility of the calling program to free it when
1545 * no longer needed. The length_ptr argument may be NULL, or point to an
1546 * integer into which the length of the string is stored.
1550 packet_get_string(u_int *length_ptr)
1552 return buffer_get_string(&active_state->incoming_packet, length_ptr);
1556 packet_get_string_ptr(u_int *length_ptr)
1558 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
1561 /* Ensures the returned string has no embedded \0 characters in it. */
1563 packet_get_cstring(u_int *length_ptr)
1565 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1569 * Sends a diagnostic message from the server to the client. This message
1570 * can be sent at any time (but not while constructing another message). The
1571 * message is printed immediately, but only if the client is being executed
1572 * in verbose mode. These messages are primarily intended to ease debugging
1573 * authentication problems. The length of the formatted message must not
1574 * exceed 1024 bytes. This will automatically call packet_write_wait.
1578 packet_send_debug(const char *fmt,...)
1583 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1586 va_start(args, fmt);
1587 vsnprintf(buf, sizeof(buf), fmt, args);
1591 packet_start(SSH2_MSG_DEBUG);
1592 packet_put_char(0); /* bool: always display */
1593 packet_put_cstring(buf);
1594 packet_put_cstring("");
1596 packet_start(SSH_MSG_DEBUG);
1597 packet_put_cstring(buf);
1600 packet_write_wait();
1604 * Logs the error plus constructs and sends a disconnect packet, closes the
1605 * connection, and exits. This function never returns. The error message
1606 * should not contain a newline. The length of the formatted message must
1607 * not exceed 1024 bytes.
1611 packet_disconnect(const char *fmt,...)
1615 static int disconnecting = 0;
1617 if (disconnecting) /* Guard against recursive invocations. */
1618 fatal("packet_disconnect called recursively.");
1622 * Format the message. Note that the caller must make sure the
1623 * message is of limited size.
1625 va_start(args, fmt);
1626 vsnprintf(buf, sizeof(buf), fmt, args);
1629 /* Display the error locally */
1630 logit("Disconnecting: %.100s", buf);
1632 /* Send the disconnect message to the other side, and wait for it to get sent. */
1634 packet_start(SSH2_MSG_DISCONNECT);
1635 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1636 packet_put_cstring(buf);
1637 packet_put_cstring("");
1639 packet_start(SSH_MSG_DISCONNECT);
1640 packet_put_cstring(buf);
1643 packet_write_wait();
1645 /* Stop listening for connections. */
1646 channel_close_all();
1648 /* Close the connection. */
1653 /* Checks if there is any buffered output, and tries to write some of the output. */
1656 packet_write_poll(void)
1658 int len = buffer_len(&active_state->output);
1663 len = roaming_write(active_state->connection_out,
1664 buffer_ptr(&active_state->output), len, &cont);
1666 if (errno == EINTR || errno == EAGAIN ||
1667 errno == EWOULDBLOCK)
1669 fatal("Write failed: %.100s", strerror(errno));
1671 if (len == 0 && !cont)
1672 fatal("Write connection closed");
1673 buffer_consume(&active_state->output, len);
1679 * Calls packet_write_poll repeatedly until all pending output data has been
1684 packet_write_wait(void)
1688 struct timeval start, timeout, *timeoutp = NULL;
1690 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1691 NFDBITS), sizeof(fd_mask));
1692 packet_write_poll();
1693 while (packet_have_data_to_write()) {
1694 memset(setp, 0, howmany(active_state->connection_out + 1,
1695 NFDBITS) * sizeof(fd_mask));
1696 FD_SET(active_state->connection_out, setp);
1698 if (active_state->packet_timeout_ms > 0) {
1699 ms_remain = active_state->packet_timeout_ms;
1700 timeoutp = &timeout;
1703 if (active_state->packet_timeout_ms != -1) {
1704 ms_to_timeval(&timeout, ms_remain);
1705 gettimeofday(&start, NULL);
1707 if ((ret = select(active_state->connection_out + 1,
1708 NULL, setp, NULL, timeoutp)) >= 0)
1710 if (errno != EAGAIN && errno != EINTR &&
1711 errno != EWOULDBLOCK)
1713 if (active_state->packet_timeout_ms == -1)
1715 ms_subtract_diff(&start, &ms_remain);
1716 if (ms_remain <= 0) {
1722 logit("Connection to %.200s timed out while "
1723 "waiting to write", get_remote_ipaddr());
1726 packet_write_poll();
1731 /* Returns true if there is buffered data to write to the connection. */
1734 packet_have_data_to_write(void)
1736 return buffer_len(&active_state->output) != 0;
1739 /* Returns true if there is not too much data to write to the connection. */
1742 packet_not_very_much_data_to_write(void)
1744 if (active_state->interactive_mode)
1745 return buffer_len(&active_state->output) < 16384;
1747 return buffer_len(&active_state->output) < 128 * 1024;
1751 packet_set_tos(int tos)
1753 #ifndef IP_TOS_IS_BROKEN
1754 if (!packet_connection_is_on_socket())
1756 switch (packet_connection_af()) {
1759 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1760 if (setsockopt(active_state->connection_in,
1761 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1762 error("setsockopt IP_TOS %d: %.100s:",
1763 tos, strerror(errno));
1765 # endif /* IP_TOS */
1768 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1769 if (setsockopt(active_state->connection_in,
1770 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1771 error("setsockopt IPV6_TCLASS %d: %.100s:",
1772 tos, strerror(errno));
1774 # endif /* IPV6_TCLASS */
1776 #endif /* IP_TOS_IS_BROKEN */
1779 /* Informs that the current session is interactive. Sets IP flags for that. */
1782 packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
1784 if (active_state->set_interactive_called)
1786 active_state->set_interactive_called = 1;
1788 /* Record that we are in interactive mode. */
1789 active_state->interactive_mode = interactive;
1791 /* Only set socket options if using a socket. */
1792 if (!packet_connection_is_on_socket())
1794 set_nodelay(active_state->connection_in);
1795 packet_set_tos(interactive ? qos_interactive : qos_bulk);
1798 /* Returns true if the current connection is interactive. */
1801 packet_is_interactive(void)
1803 return active_state->interactive_mode;
1807 packet_set_maxsize(u_int s)
1809 if (active_state->set_maxsize_called) {
1810 logit("packet_set_maxsize: called twice: old %d new %d",
1811 active_state->max_packet_size, s);
1814 if (s < 4 * 1024 || s > 1024 * 1024) {
1815 logit("packet_set_maxsize: bad size %d", s);
1818 active_state->set_maxsize_called = 1;
1819 debug("packet_set_maxsize: setting to %d", s);
1820 active_state->max_packet_size = s;
1825 packet_inc_alive_timeouts(void)
1827 return ++active_state->keep_alive_timeouts;
1831 packet_set_alive_timeouts(int ka)
1833 active_state->keep_alive_timeouts = ka;
1837 packet_get_maxsize(void)
1839 return active_state->max_packet_size;
1842 /* roundup current message to pad bytes */
1844 packet_add_padding(u_char pad)
1846 active_state->extra_pad = pad;
1850 * 9.2. Ignored Data Message
1852 * byte SSH_MSG_IGNORE
1855 * All implementations MUST understand (and ignore) this message at any
1856 * time (after receiving the protocol version). No implementation is
1857 * required to send them. This message can be used as an additional
1858 * protection measure against advanced traffic analysis techniques.
1861 packet_send_ignore(int nbytes)
1866 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1867 packet_put_int(nbytes);
1868 for (i = 0; i < nbytes; i++) {
1871 packet_put_char((u_char)rnd & 0xff);
1876 int rekey_requested = 0;
1878 packet_request_rekeying(void)
1880 rekey_requested = 1;
1883 #define MAX_PACKETS (1U<<31)
1885 packet_need_rekeying(void)
1887 if (datafellows & SSH_BUG_NOREKEY)
1889 if (rekey_requested == 1)
1891 rekey_requested = 0;
1895 (active_state->p_send.packets > MAX_PACKETS) ||
1896 (active_state->p_read.packets > MAX_PACKETS) ||
1897 (active_state->max_blocks_out &&
1898 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1899 (active_state->max_blocks_in &&
1900 (active_state->p_read.blocks > active_state->max_blocks_in));
1904 packet_set_rekey_limit(u_int32_t bytes)
1906 active_state->rekey_limit = bytes;
1910 packet_set_server(void)
1912 active_state->server_side = 1;
1916 packet_set_authenticated(void)
1918 active_state->after_authentication = 1;
1922 packet_get_input(void)
1924 return (void *)&active_state->input;
1928 packet_get_output(void)
1930 return (void *)&active_state->output;
1934 packet_get_newkeys(int mode)
1936 return (void *)active_state->newkeys[mode];
1940 * Save the state for the real connection, and use a separate state when
1941 * resuming a suspended connection.
1944 packet_backup_state(void)
1946 struct session_state *tmp;
1948 close(active_state->connection_in);
1949 active_state->connection_in = -1;
1950 close(active_state->connection_out);
1951 active_state->connection_out = -1;
1955 tmp = alloc_session_state();
1956 backup_state = active_state;
1961 * Swap in the old state when resuming a connecion.
1964 packet_restore_state(void)
1966 struct session_state *tmp;
1971 backup_state = active_state;
1973 active_state->connection_in = backup_state->connection_in;
1974 backup_state->connection_in = -1;
1975 active_state->connection_out = backup_state->connection_out;
1976 backup_state->connection_out = -1;
1977 len = buffer_len(&backup_state->input);
1979 buf = buffer_ptr(&backup_state->input);
1980 buffer_append(&active_state->input, buf, len);
1981 buffer_clear(&backup_state->input);
1982 add_recv_bytes(len);
1987 packet_authentication_state(void)
1989 return(active_state->after_authentication);