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