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