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