Annotation of src/usr.bin/ssh/packet.c, Revision 1.91
1.1 deraadt 1: /*
1.15 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.35 deraadt 5: * This file contains code implementing the packet protocol and communication
6: * with the other side. This same code is used both on client and server side.
1.29 markus 7: *
1.35 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.29 markus 13: *
1.25 markus 14: *
15: * SSH2 packet format added by Markus Friedl.
1.69 markus 16: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.25 markus 17: *
1.35 deraadt 18: * Redistribution and use in source and binary forms, with or without
19: * modification, are permitted provided that the following conditions
20: * are met:
21: * 1. Redistributions of source code must retain the above copyright
22: * notice, this list of conditions and the following disclaimer.
23: * 2. Redistributions in binary form must reproduce the above copyright
24: * notice, this list of conditions and the following disclaimer in the
25: * documentation and/or other materials provided with the distribution.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.15 deraadt 37: */
1.1 deraadt 38:
39: #include "includes.h"
1.91 ! markus 40: RCSID("$OpenBSD: packet.c,v 1.5 2002/03/12 18:03:57 markus Exp $");
1.1 deraadt 41:
42: #include "xmalloc.h"
43: #include "buffer.h"
44: #include "packet.h"
45: #include "bufaux.h"
46: #include "crc32.h"
47: #include "getput.h"
48:
49: #include "compress.h"
1.9 dugsong 50: #include "deattack.h"
1.64 markus 51: #include "channels.h"
1.1 deraadt 52:
1.25 markus 53: #include "compat.h"
1.45 markus 54: #include "ssh1.h"
1.25 markus 55: #include "ssh2.h"
56:
1.37 markus 57: #include "cipher.h"
1.25 markus 58: #include "kex.h"
1.50 markus 59: #include "mac.h"
1.46 markus 60: #include "log.h"
61: #include "canohost.h"
1.87 stevesk 62: #include "misc.h"
1.25 markus 63:
64: #ifdef PACKET_DEBUG
65: #define DBG(x) x
66: #else
67: #define DBG(x)
68: #endif
69:
1.16 markus 70: /*
71: * This variable contains the file descriptors used for communicating with
72: * the other side. connection_in is used for reading; connection_out for
73: * writing. These can be the same descriptor, in which case it is assumed to
74: * be a socket.
75: */
1.1 deraadt 76: static int connection_in = -1;
77: static int connection_out = -1;
78:
79: /* Protocol flags for the remote side. */
1.40 markus 80: static u_int remote_protocol_flags = 0;
1.1 deraadt 81:
82: /* Encryption context for receiving data. This is only used for decryption. */
83: static CipherContext receive_context;
1.14 markus 84:
85: /* Encryption context for sending data. This is only used for encryption. */
1.1 deraadt 86: static CipherContext send_context;
87:
88: /* Buffer for raw input data from the socket. */
1.91 ! markus 89: Buffer input;
1.1 deraadt 90:
91: /* Buffer for raw output data going to the socket. */
1.91 ! markus 92: Buffer output;
1.1 deraadt 93:
94: /* Buffer for the partial outgoing packet being constructed. */
95: static Buffer outgoing_packet;
96:
97: /* Buffer for the incoming packet currently being processed. */
98: static Buffer incoming_packet;
99:
100: /* Scratch buffer for packet compression/decompression. */
101: static Buffer compression_buffer;
1.60 markus 102: static int compression_buffer_ready = 0;
1.1 deraadt 103:
104: /* Flag indicating whether packet compression/decompression is enabled. */
105: static int packet_compression = 0;
106:
1.12 markus 107: /* default maximum packet size */
108: int max_packet_size = 32768;
109:
1.1 deraadt 110: /* Flag indicating whether this module has been initialized. */
111: static int initialized = 0;
112:
113: /* Set to true if the connection is interactive. */
114: static int interactive_mode = 0;
115:
1.25 markus 116: /* Session key information for Encryption and MAC */
1.57 markus 117: Newkeys *newkeys[MODE_MAX];
1.91 ! markus 118: static u_int32_t read_seqnr = 0;
! 119: static u_int32_t send_seqnr = 0;
1.25 markus 120:
1.71 markus 121: /* roundup current message to extra_pad bytes */
122: static u_char extra_pad = 0;
123:
1.16 markus 124: /*
125: * Sets the descriptors used for communication. Disables encryption until
126: * packet_set_encryption_key is called.
127: */
1.2 provos 128: void
129: packet_set_connection(int fd_in, int fd_out)
1.1 deraadt 130: {
1.37 markus 131: Cipher *none = cipher_by_name("none");
132: if (none == NULL)
133: fatal("packet_set_connection: cannot load cipher 'none'");
1.14 markus 134: connection_in = fd_in;
135: connection_out = fd_out;
1.88 markus 136: cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
137: cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
1.62 markus 138: newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
1.14 markus 139: if (!initialized) {
140: initialized = 1;
141: buffer_init(&input);
142: buffer_init(&output);
143: buffer_init(&outgoing_packet);
144: buffer_init(&incoming_packet);
145: }
146: /* Kludge: arrange the close function to be called from fatal(). */
147: fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
1.1 deraadt 148: }
149:
1.19 markus 150: /* Returns 1 if remote host is connected via socket, 0 if not. */
151:
152: int
1.73 itojun 153: packet_connection_is_on_socket(void)
1.19 markus 154: {
155: struct sockaddr_storage from, to;
156: socklen_t fromlen, tolen;
157:
158: /* filedescriptors in and out are the same, so it's a socket */
159: if (connection_in == connection_out)
160: return 1;
161: fromlen = sizeof(from);
162: memset(&from, 0, sizeof(from));
1.20 markus 163: if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
1.19 markus 164: return 0;
165: tolen = sizeof(to);
166: memset(&to, 0, sizeof(to));
1.20 markus 167: if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
1.19 markus 168: return 0;
169: if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
170: return 0;
171: if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
172: return 0;
173: return 1;
174: }
175:
1.91 ! markus 176: /*
! 177: * Exports an IV from the CipherContext required to export the key
! 178: * state back from the unprivileged child to the privileged parent
! 179: * process.
! 180: */
! 181:
! 182: void
! 183: packet_get_keyiv(int mode, u_char *iv, u_int len)
! 184: {
! 185: CipherContext *cc;
! 186:
! 187: if (mode == MODE_OUT)
! 188: cc = &send_context;
! 189: else
! 190: cc = &receive_context;
! 191:
! 192: cipher_get_keyiv(cc, iv, len);
! 193: }
! 194:
! 195: int
! 196: packet_get_keycontext(int mode, u_char *dat)
! 197: {
! 198: CipherContext *cc;
! 199:
! 200: if (mode == MODE_OUT)
! 201: cc = &send_context;
! 202: else
! 203: cc = &receive_context;
! 204:
! 205: return (cipher_get_keycontext(cc, dat));
! 206: }
! 207:
! 208: void
! 209: packet_set_keycontext(int mode, u_char *dat)
! 210: {
! 211: CipherContext *cc;
! 212:
! 213: if (mode == MODE_OUT)
! 214: cc = &send_context;
! 215: else
! 216: cc = &receive_context;
! 217:
! 218: cipher_set_keycontext(cc, dat);
! 219: }
! 220:
! 221: int
! 222: packet_get_keyiv_len(int mode)
! 223: {
! 224: CipherContext *cc;
! 225:
! 226: if (mode == MODE_OUT)
! 227: cc = &send_context;
! 228: else
! 229: cc = &receive_context;
! 230:
! 231: return (cipher_get_keyiv_len(cc));
! 232: }
! 233: void
! 234: packet_set_iv(int mode, u_char *dat)
! 235: {
! 236: CipherContext *cc;
! 237:
! 238: if (mode == MODE_OUT)
! 239: cc = &send_context;
! 240: else
! 241: cc = &receive_context;
! 242:
! 243: cipher_set_keyiv(cc, dat);
! 244: }
! 245: int
! 246: packet_get_ssh1_cipher()
! 247: {
! 248: return (cipher_get_number(receive_context.cipher));
! 249: }
! 250:
! 251:
! 252: u_int32_t
! 253: packet_get_seqnr(int mode)
! 254: {
! 255: return (mode == MODE_IN ? read_seqnr : send_seqnr);
! 256: }
! 257:
! 258: void
! 259: packet_set_seqnr(int mode, u_int32_t seqnr)
! 260: {
! 261: if (mode == MODE_IN)
! 262: read_seqnr = seqnr;
! 263: else if (mode == MODE_OUT)
! 264: send_seqnr = seqnr;
! 265: else
! 266: fatal("%s: bad mode %d", __FUNCTION__, mode);
! 267: }
! 268:
1.19 markus 269: /* returns 1 if connection is via ipv4 */
270:
271: int
1.73 itojun 272: packet_connection_is_ipv4(void)
1.19 markus 273: {
274: struct sockaddr_storage to;
1.21 deraadt 275: socklen_t tolen = sizeof(to);
1.19 markus 276:
277: memset(&to, 0, sizeof(to));
278: if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
279: return 0;
280: if (to.ss_family != AF_INET)
281: return 0;
282: return 1;
283: }
284:
1.1 deraadt 285: /* Sets the connection into non-blocking mode. */
286:
1.2 provos 287: void
1.73 itojun 288: packet_set_nonblocking(void)
1.1 deraadt 289: {
1.14 markus 290: /* Set the socket into non-blocking mode. */
291: if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
292: error("fcntl O_NONBLOCK: %.100s", strerror(errno));
293:
294: if (connection_out != connection_in) {
295: if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
296: error("fcntl O_NONBLOCK: %.100s", strerror(errno));
297: }
1.1 deraadt 298: }
299:
300: /* Returns the socket used for reading. */
301:
1.2 provos 302: int
1.73 itojun 303: packet_get_connection_in(void)
1.1 deraadt 304: {
1.14 markus 305: return connection_in;
1.1 deraadt 306: }
307:
308: /* Returns the descriptor used for writing. */
309:
1.2 provos 310: int
1.73 itojun 311: packet_get_connection_out(void)
1.1 deraadt 312: {
1.14 markus 313: return connection_out;
1.1 deraadt 314: }
315:
316: /* Closes the connection and clears and frees internal data structures. */
317:
1.2 provos 318: void
1.73 itojun 319: packet_close(void)
1.1 deraadt 320: {
1.14 markus 321: if (!initialized)
322: return;
323: initialized = 0;
324: if (connection_in == connection_out) {
325: shutdown(connection_out, SHUT_RDWR);
326: close(connection_out);
327: } else {
328: close(connection_in);
329: close(connection_out);
330: }
331: buffer_free(&input);
332: buffer_free(&output);
333: buffer_free(&outgoing_packet);
334: buffer_free(&incoming_packet);
1.60 markus 335: if (compression_buffer_ready) {
1.14 markus 336: buffer_free(&compression_buffer);
337: buffer_compress_uninit();
338: }
1.88 markus 339: cipher_cleanup(&send_context);
340: cipher_cleanup(&receive_context);
1.1 deraadt 341: }
342:
343: /* Sets remote side protocol flags. */
344:
1.2 provos 345: void
1.40 markus 346: packet_set_protocol_flags(u_int protocol_flags)
1.1 deraadt 347: {
1.14 markus 348: remote_protocol_flags = protocol_flags;
1.1 deraadt 349: }
350:
351: /* Returns the remote protocol flags set earlier by the above function. */
352:
1.40 markus 353: u_int
1.73 itojun 354: packet_get_protocol_flags(void)
1.1 deraadt 355: {
1.14 markus 356: return remote_protocol_flags;
1.1 deraadt 357: }
358:
1.16 markus 359: /*
360: * Starts packet compression from the next packet on in both directions.
361: * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
362: */
1.1 deraadt 363:
1.68 itojun 364: static void
365: packet_init_compression(void)
1.60 markus 366: {
367: if (compression_buffer_ready == 1)
368: return;
369: compression_buffer_ready = 1;
370: buffer_init(&compression_buffer);
371: }
372:
1.2 provos 373: void
374: packet_start_compression(int level)
1.1 deraadt 375: {
1.62 markus 376: if (packet_compression && !compat20)
1.14 markus 377: fatal("Compression already enabled.");
378: packet_compression = 1;
1.60 markus 379: packet_init_compression();
380: buffer_compress_init_send(level);
381: buffer_compress_init_recv();
1.1 deraadt 382: }
383:
1.16 markus 384: /*
385: * Causes any further packets to be encrypted using the given key. The same
386: * key is used for both sending and reception. However, both directions are
387: * encrypted independently of each other.
388: */
1.2 provos 389: void
1.40 markus 390: packet_set_encryption_key(const u_char *key, u_int keylen,
1.37 markus 391: int number)
1.1 deraadt 392: {
1.37 markus 393: Cipher *cipher = cipher_by_number(number);
394: if (cipher == NULL)
395: fatal("packet_set_encryption_key: unknown cipher number %d", number);
1.25 markus 396: if (keylen < 20)
1.37 markus 397: fatal("packet_set_encryption_key: keylen too small: %d", keylen);
1.88 markus 398: cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
399: cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
1.1 deraadt 400: }
401:
1.62 markus 402: /* Start constructing a packet to send. */
1.2 provos 403: void
1.62 markus 404: packet_start(u_char type)
1.1 deraadt 405: {
1.62 markus 406: u_char buf[9];
407: int len;
1.1 deraadt 408:
1.62 markus 409: DBG(debug("packet_start[%d]", type));
410: len = compat20 ? 6 : 9;
411: memset(buf, 0, len - 1);
412: buf[len - 1] = type;
1.14 markus 413: buffer_clear(&outgoing_packet);
1.62 markus 414: buffer_append(&outgoing_packet, buf, len);
1.25 markus 415: }
416:
1.62 markus 417: /* Append payload. */
1.2 provos 418: void
419: packet_put_char(int value)
1.1 deraadt 420: {
1.14 markus 421: char ch = value;
422: buffer_append(&outgoing_packet, &ch, 1);
1.1 deraadt 423: }
1.2 provos 424: void
1.40 markus 425: packet_put_int(u_int value)
1.1 deraadt 426: {
1.14 markus 427: buffer_put_int(&outgoing_packet, value);
1.1 deraadt 428: }
1.2 provos 429: void
1.76 stevesk 430: packet_put_string(const void *buf, u_int len)
1.1 deraadt 431: {
1.14 markus 432: buffer_put_string(&outgoing_packet, buf, len);
1.1 deraadt 433: }
1.24 markus 434: void
435: packet_put_cstring(const char *str)
436: {
1.65 markus 437: buffer_put_cstring(&outgoing_packet, str);
1.24 markus 438: }
439: void
1.76 stevesk 440: packet_put_raw(const void *buf, u_int len)
1.24 markus 441: {
442: buffer_append(&outgoing_packet, buf, len);
443: }
1.2 provos 444: void
1.14 markus 445: packet_put_bignum(BIGNUM * value)
1.1 deraadt 446: {
1.14 markus 447: buffer_put_bignum(&outgoing_packet, value);
1.1 deraadt 448: }
1.24 markus 449: void
450: packet_put_bignum2(BIGNUM * value)
451: {
452: buffer_put_bignum2(&outgoing_packet, value);
453: }
1.1 deraadt 454:
1.16 markus 455: /*
456: * Finalizes and sends the packet. If the encryption key has been set,
457: * encrypts the packet before sending.
458: */
1.14 markus 459:
1.68 itojun 460: static void
1.49 itojun 461: packet_send1(void)
1.1 deraadt 462: {
1.89 markus 463: u_char buf[8], *cp;
1.14 markus 464: int i, padding, len;
1.40 markus 465: u_int checksum;
1.14 markus 466: u_int32_t rand = 0;
467:
1.16 markus 468: /*
469: * If using packet compression, compress the payload of the outgoing
470: * packet.
471: */
1.14 markus 472: if (packet_compression) {
473: buffer_clear(&compression_buffer);
474: /* Skip padding. */
475: buffer_consume(&outgoing_packet, 8);
476: /* padding */
477: buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
478: buffer_compress(&outgoing_packet, &compression_buffer);
479: buffer_clear(&outgoing_packet);
480: buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
1.75 deraadt 481: buffer_len(&compression_buffer));
1.14 markus 482: }
483: /* Compute packet length without padding (add checksum, remove padding). */
484: len = buffer_len(&outgoing_packet) + 4 - 8;
485:
1.32 markus 486: /* Insert padding. Initialized to zero in packet_start1() */
1.14 markus 487: padding = 8 - len % 8;
1.88 markus 488: if (!send_context.plaintext) {
1.14 markus 489: cp = buffer_ptr(&outgoing_packet);
490: for (i = 0; i < padding; i++) {
491: if (i % 4 == 0)
492: rand = arc4random();
493: cp[7 - i] = rand & 0xff;
494: rand >>= 8;
495: }
496: }
497: buffer_consume(&outgoing_packet, 8 - padding);
498:
499: /* Add check bytes. */
1.83 stevesk 500: checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
1.34 deraadt 501: buffer_len(&outgoing_packet));
1.14 markus 502: PUT_32BIT(buf, checksum);
503: buffer_append(&outgoing_packet, buf, 4);
1.1 deraadt 504:
505: #ifdef PACKET_DEBUG
1.14 markus 506: fprintf(stderr, "packet_send plain: ");
507: buffer_dump(&outgoing_packet);
1.1 deraadt 508: #endif
509:
1.14 markus 510: /* Append to output. */
511: PUT_32BIT(buf, len);
512: buffer_append(&output, buf, 4);
1.76 stevesk 513: cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
1.88 markus 514: cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
1.75 deraadt 515: buffer_len(&outgoing_packet));
1.14 markus 516:
1.1 deraadt 517: #ifdef PACKET_DEBUG
1.14 markus 518: fprintf(stderr, "encrypted: ");
519: buffer_dump(&output);
1.1 deraadt 520: #endif
521:
1.14 markus 522: buffer_clear(&outgoing_packet);
1.1 deraadt 523:
1.16 markus 524: /*
525: * Note that the packet is now only buffered in output. It won\'t be
526: * actually sent until packet_write_wait or packet_write_poll is
527: * called.
528: */
1.1 deraadt 529: }
530:
1.91 ! markus 531: void
1.57 markus 532: set_newkeys(int mode)
533: {
534: Enc *enc;
535: Mac *mac;
536: Comp *comp;
537: CipherContext *cc;
1.88 markus 538: int encrypt;
1.57 markus 539:
540: debug("newkeys: mode %d", mode);
541:
1.88 markus 542: if (mode == MODE_OUT) {
543: cc = &send_context;
544: encrypt = CIPHER_ENCRYPT;
545: } else {
546: cc = &receive_context;
547: encrypt = CIPHER_DECRYPT;
548: }
1.57 markus 549: if (newkeys[mode] != NULL) {
550: debug("newkeys: rekeying");
1.88 markus 551: cipher_cleanup(cc);
1.59 markus 552: enc = &newkeys[mode]->enc;
553: mac = &newkeys[mode]->mac;
554: comp = &newkeys[mode]->comp;
1.61 markus 555: memset(mac->key, 0, mac->key_len);
1.59 markus 556: xfree(enc->name);
557: xfree(enc->iv);
558: xfree(enc->key);
559: xfree(mac->name);
560: xfree(mac->key);
561: xfree(comp->name);
1.58 markus 562: xfree(newkeys[mode]);
1.57 markus 563: }
564: newkeys[mode] = kex_get_newkeys(mode);
565: if (newkeys[mode] == NULL)
566: fatal("newkeys: no keys for mode %d", mode);
567: enc = &newkeys[mode]->enc;
568: mac = &newkeys[mode]->mac;
569: comp = &newkeys[mode]->comp;
570: if (mac->md != NULL)
571: mac->enabled = 1;
572: DBG(debug("cipher_init_context: %d", mode));
1.88 markus 573: cipher_init(cc, enc->cipher, enc->key, enc->key_len,
574: enc->iv, enc->block_size, encrypt);
1.91 ! markus 575: /* Deleting the keys does not gain extra security */
! 576: /* memset(enc->iv, 0, enc->block_size);
! 577: memset(enc->key, 0, enc->key_len); */
1.57 markus 578: if (comp->type != 0 && comp->enabled == 0) {
1.60 markus 579: packet_init_compression();
580: if (mode == MODE_OUT)
581: buffer_compress_init_send(6);
582: else
583: buffer_compress_init_recv();
1.57 markus 584: comp->enabled = 1;
585: }
586: }
587:
1.16 markus 588: /*
1.25 markus 589: * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
590: */
1.68 itojun 591: static void
1.49 itojun 592: packet_send2(void)
1.25 markus 593: {
1.89 markus 594: u_char type, *cp, *macbuf = NULL;
1.71 markus 595: u_char padlen, pad;
1.40 markus 596: u_int packet_length = 0;
1.71 markus 597: u_int i, len;
1.25 markus 598: u_int32_t rand = 0;
599: Enc *enc = NULL;
600: Mac *mac = NULL;
601: Comp *comp = NULL;
602: int block_size;
603:
1.57 markus 604: if (newkeys[MODE_OUT] != NULL) {
605: enc = &newkeys[MODE_OUT]->enc;
606: mac = &newkeys[MODE_OUT]->mac;
607: comp = &newkeys[MODE_OUT]->comp;
1.25 markus 608: }
1.88 markus 609: block_size = enc ? enc->block_size : 8;
1.25 markus 610:
1.89 markus 611: cp = buffer_ptr(&outgoing_packet);
612: type = cp[5];
1.25 markus 613:
614: #ifdef PACKET_DEBUG
615: fprintf(stderr, "plain: ");
616: buffer_dump(&outgoing_packet);
617: #endif
618:
619: if (comp && comp->enabled) {
620: len = buffer_len(&outgoing_packet);
621: /* skip header, compress only payload */
622: buffer_consume(&outgoing_packet, 5);
623: buffer_clear(&compression_buffer);
624: buffer_compress(&outgoing_packet, &compression_buffer);
625: buffer_clear(&outgoing_packet);
626: buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
627: buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
628: buffer_len(&compression_buffer));
629: DBG(debug("compression: raw %d compressed %d", len,
630: buffer_len(&outgoing_packet)));
631: }
632:
633: /* sizeof (packet_len + pad_len + payload) */
634: len = buffer_len(&outgoing_packet);
635:
636: /*
637: * calc size of padding, alloc space, get random data,
638: * minimum padding is 4 bytes
639: */
640: padlen = block_size - (len % block_size);
641: if (padlen < 4)
642: padlen += block_size;
1.71 markus 643: if (extra_pad) {
644: /* will wrap if extra_pad+padlen > 255 */
645: extra_pad = roundup(extra_pad, block_size);
646: pad = extra_pad - ((len + padlen) % extra_pad);
647: debug("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
648: pad, len, padlen, extra_pad);
649: padlen += pad;
650: extra_pad = 0;
651: }
1.76 stevesk 652: cp = buffer_append_space(&outgoing_packet, padlen);
1.88 markus 653: if (enc && !send_context.plaintext) {
1.32 markus 654: /* random padding */
1.25 markus 655: for (i = 0; i < padlen; i++) {
656: if (i % 4 == 0)
657: rand = arc4random();
658: cp[i] = rand & 0xff;
1.52 markus 659: rand >>= 8;
1.25 markus 660: }
1.32 markus 661: } else {
662: /* clear padding */
663: memset(cp, 0, padlen);
1.25 markus 664: }
665: /* packet_length includes payload, padding and padding length field */
666: packet_length = buffer_len(&outgoing_packet) - 4;
1.89 markus 667: cp = buffer_ptr(&outgoing_packet);
668: PUT_32BIT(cp, packet_length);
669: cp[4] = padlen;
1.25 markus 670: DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
671:
672: /* compute MAC over seqnr and packet(length fields, payload, padding) */
673: if (mac && mac->enabled) {
1.91 ! markus 674: macbuf = mac_compute(mac, send_seqnr,
1.83 stevesk 675: buffer_ptr(&outgoing_packet),
1.50 markus 676: buffer_len(&outgoing_packet));
1.91 ! markus 677: DBG(debug("done calc MAC out #%d", send_seqnr));
1.25 markus 678: }
679: /* encrypt packet and append to output buffer. */
1.76 stevesk 680: cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
1.88 markus 681: cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
1.25 markus 682: buffer_len(&outgoing_packet));
683: /* append unencrypted MAC */
684: if (mac && mac->enabled)
685: buffer_append(&output, (char *)macbuf, mac->mac_len);
686: #ifdef PACKET_DEBUG
687: fprintf(stderr, "encrypted: ");
688: buffer_dump(&output);
689: #endif
1.29 markus 690: /* increment sequence number for outgoing packets */
1.91 ! markus 691: if (++send_seqnr == 0)
1.29 markus 692: log("outgoing seqnr wraps around");
1.25 markus 693: buffer_clear(&outgoing_packet);
694:
1.57 markus 695: if (type == SSH2_MSG_NEWKEYS)
696: set_newkeys(MODE_OUT);
1.25 markus 697: }
698:
699: void
1.73 itojun 700: packet_send(void)
1.25 markus 701: {
1.62 markus 702: if (compat20)
1.25 markus 703: packet_send2();
704: else
705: packet_send1();
706: DBG(debug("packet_send done"));
707: }
708:
709: /*
1.16 markus 710: * Waits until a packet has been received, and returns its type. Note that
711: * no other data is processed until this returns, so this function should not
712: * be used during the interactive session.
713: */
1.1 deraadt 714:
1.2 provos 715: int
1.82 markus 716: packet_read_seqnr(u_int32_t *seqnr_p)
1.1 deraadt 717: {
1.14 markus 718: int type, len;
1.56 millert 719: fd_set *setp;
1.14 markus 720: char buf[8192];
1.25 markus 721: DBG(debug("packet_read()"));
1.14 markus 722:
1.56 millert 723: setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
724: sizeof(fd_mask));
725:
1.14 markus 726: /* Since we are blocking, ensure that all written packets have been sent. */
727: packet_write_wait();
728:
729: /* Stay in the loop until we have received a complete packet. */
730: for (;;) {
731: /* Try to read a packet from the buffer. */
1.82 markus 732: type = packet_read_poll_seqnr(seqnr_p);
1.62 markus 733: if (!compat20 && (
1.32 markus 734: type == SSH_SMSG_SUCCESS
1.14 markus 735: || type == SSH_SMSG_FAILURE
736: || type == SSH_CMSG_EOF
1.32 markus 737: || type == SSH_CMSG_EXIT_CONFIRMATION))
1.79 markus 738: packet_check_eom();
1.14 markus 739: /* If we got a packet, return it. */
1.56 millert 740: if (type != SSH_MSG_NONE) {
741: xfree(setp);
1.14 markus 742: return type;
1.56 millert 743: }
1.16 markus 744: /*
745: * Otherwise, wait for some data to arrive, add it to the
746: * buffer, and try again.
747: */
1.56 millert 748: memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
749: sizeof(fd_mask));
750: FD_SET(connection_in, setp);
1.16 markus 751:
1.14 markus 752: /* Wait for some data to arrive. */
1.56 millert 753: while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
1.51 deraadt 754: (errno == EAGAIN || errno == EINTR))
755: ;
1.16 markus 756:
1.14 markus 757: /* Read data from the socket. */
758: len = read(connection_in, buf, sizeof(buf));
1.18 markus 759: if (len == 0) {
760: log("Connection closed by %.200s", get_remote_ipaddr());
761: fatal_cleanup();
762: }
1.14 markus 763: if (len < 0)
764: fatal("Read from socket failed: %.100s", strerror(errno));
765: /* Append it to the buffer. */
766: packet_process_incoming(buf, len);
767: }
768: /* NOTREACHED */
1.1 deraadt 769: }
770:
1.77 djm 771: int
1.82 markus 772: packet_read(void)
1.77 djm 773: {
1.82 markus 774: return packet_read_seqnr(NULL);
1.77 djm 775: }
776:
1.16 markus 777: /*
778: * Waits until a packet has been received, verifies that its type matches
779: * that given, and gives a fatal error and exits if there is a mismatch.
780: */
1.1 deraadt 781:
1.2 provos 782: void
1.82 markus 783: packet_read_expect(int expected_type)
1.1 deraadt 784: {
1.14 markus 785: int type;
1.1 deraadt 786:
1.82 markus 787: type = packet_read();
1.14 markus 788: if (type != expected_type)
789: packet_disconnect("Protocol error: expected packet type %d, got %d",
1.25 markus 790: expected_type, type);
1.1 deraadt 791: }
792:
793: /* Checks if a full packet is available in the data received so far via
1.14 markus 794: * packet_process_incoming. If so, reads the packet; otherwise returns
795: * SSH_MSG_NONE. This does not wait for data from the connection.
796: *
797: * SSH_MSG_DISCONNECT is handled specially here. Also,
798: * SSH_MSG_IGNORE messages are skipped by this function and are never returned
799: * to higher levels.
800: */
1.1 deraadt 801:
1.68 itojun 802: static int
1.82 markus 803: packet_read_poll1(void)
1.1 deraadt 804: {
1.40 markus 805: u_int len, padded_len;
1.89 markus 806: u_char *cp, type;
1.40 markus 807: u_int checksum, stored_checksum;
1.14 markus 808:
809: /* Check if input size is less than minimum packet size. */
810: if (buffer_len(&input) < 4 + 8)
811: return SSH_MSG_NONE;
812: /* Get length of incoming packet. */
1.89 markus 813: cp = buffer_ptr(&input);
814: len = GET_32BIT(cp);
1.14 markus 815: if (len < 1 + 2 + 2 || len > 256 * 1024)
816: packet_disconnect("Bad packet length %d.", len);
817: padded_len = (len + 8) & ~7;
818:
819: /* Check if the packet has been entirely received. */
820: if (buffer_len(&input) < 4 + padded_len)
821: return SSH_MSG_NONE;
822:
823: /* The entire packet is in buffer. */
824:
825: /* Consume packet length. */
826: buffer_consume(&input, 4);
827:
1.62 markus 828: /*
829: * Cryptographic attack detector for ssh
830: * (C)1998 CORE-SDI, Buenos Aires Argentina
831: * Ariel Futoransky(futo@core-sdi.com)
832: */
1.88 markus 833: if (!receive_context.plaintext &&
1.62 markus 834: detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
835: packet_disconnect("crc32 compensation attack: network attack detected");
836:
837: /* Decrypt data to incoming_packet. */
1.14 markus 838: buffer_clear(&incoming_packet);
1.76 stevesk 839: cp = buffer_append_space(&incoming_packet, padded_len);
1.88 markus 840: cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
1.62 markus 841:
1.14 markus 842: buffer_consume(&input, padded_len);
1.1 deraadt 843:
844: #ifdef PACKET_DEBUG
1.14 markus 845: fprintf(stderr, "read_poll plain: ");
846: buffer_dump(&incoming_packet);
1.1 deraadt 847: #endif
848:
1.14 markus 849: /* Compute packet checksum. */
1.83 stevesk 850: checksum = ssh_crc32(buffer_ptr(&incoming_packet),
1.25 markus 851: buffer_len(&incoming_packet) - 4);
1.14 markus 852:
853: /* Skip padding. */
854: buffer_consume(&incoming_packet, 8 - len % 8);
855:
856: /* Test check bytes. */
857: if (len != buffer_len(&incoming_packet))
1.77 djm 858: packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
1.25 markus 859: len, buffer_len(&incoming_packet));
1.14 markus 860:
1.89 markus 861: cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
862: stored_checksum = GET_32BIT(cp);
1.14 markus 863: if (checksum != stored_checksum)
864: packet_disconnect("Corrupted check bytes on input.");
865: buffer_consume_end(&incoming_packet, 4);
866:
867: if (packet_compression) {
868: buffer_clear(&compression_buffer);
869: buffer_uncompress(&incoming_packet, &compression_buffer);
870: buffer_clear(&incoming_packet);
871: buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
1.25 markus 872: buffer_len(&compression_buffer));
1.14 markus 873: }
1.62 markus 874: type = buffer_get_char(&incoming_packet);
875: return type;
1.1 deraadt 876: }
1.14 markus 877:
1.68 itojun 878: static int
1.82 markus 879: packet_read_poll2(u_int32_t *seqnr_p)
1.25 markus 880: {
1.50 markus 881: static u_int packet_length = 0;
1.40 markus 882: u_int padlen, need;
1.89 markus 883: u_char *macbuf, *cp, type;
1.25 markus 884: int maclen, block_size;
885: Enc *enc = NULL;
886: Mac *mac = NULL;
887: Comp *comp = NULL;
888:
1.57 markus 889: if (newkeys[MODE_IN] != NULL) {
890: enc = &newkeys[MODE_IN]->enc;
891: mac = &newkeys[MODE_IN]->mac;
892: comp = &newkeys[MODE_IN]->comp;
1.25 markus 893: }
894: maclen = mac && mac->enabled ? mac->mac_len : 0;
1.88 markus 895: block_size = enc ? enc->block_size : 8;
1.25 markus 896:
897: if (packet_length == 0) {
898: /*
899: * check if input size is less than the cipher block size,
900: * decrypt first block and extract length of incoming packet
901: */
902: if (buffer_len(&input) < block_size)
903: return SSH_MSG_NONE;
904: buffer_clear(&incoming_packet);
1.76 stevesk 905: cp = buffer_append_space(&incoming_packet, block_size);
1.88 markus 906: cipher_crypt(&receive_context, cp, buffer_ptr(&input),
1.25 markus 907: block_size);
1.89 markus 908: cp = buffer_ptr(&incoming_packet);
909: packet_length = GET_32BIT(cp);
1.25 markus 910: if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
911: buffer_dump(&incoming_packet);
912: packet_disconnect("Bad packet length %d.", packet_length);
913: }
914: DBG(debug("input: packet len %d", packet_length+4));
915: buffer_consume(&input, block_size);
916: }
917: /* we have a partial packet of block_size bytes */
918: need = 4 + packet_length - block_size;
919: DBG(debug("partial packet %d, need %d, maclen %d", block_size,
920: need, maclen));
921: if (need % block_size != 0)
922: fatal("padding error: need %d block %d mod %d",
923: need, block_size, need % block_size);
924: /*
925: * check if the entire packet has been received and
926: * decrypt into incoming_packet
927: */
928: if (buffer_len(&input) < need + maclen)
929: return SSH_MSG_NONE;
930: #ifdef PACKET_DEBUG
931: fprintf(stderr, "read_poll enc/full: ");
932: buffer_dump(&input);
933: #endif
1.76 stevesk 934: cp = buffer_append_space(&incoming_packet, need);
1.88 markus 935: cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
1.25 markus 936: buffer_consume(&input, need);
937: /*
938: * compute MAC over seqnr and packet,
939: * increment sequence number for incoming packet
940: */
1.29 markus 941: if (mac && mac->enabled) {
1.91 ! markus 942: macbuf = mac_compute(mac, read_seqnr,
1.83 stevesk 943: buffer_ptr(&incoming_packet),
1.50 markus 944: buffer_len(&incoming_packet));
1.25 markus 945: if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
1.36 markus 946: packet_disconnect("Corrupted MAC on input.");
1.91 ! markus 947: DBG(debug("MAC #%d ok", read_seqnr));
1.25 markus 948: buffer_consume(&input, mac->mac_len);
949: }
1.77 djm 950: if (seqnr_p != NULL)
1.91 ! markus 951: *seqnr_p = read_seqnr;
! 952: if (++read_seqnr == 0)
1.29 markus 953: log("incoming seqnr wraps around");
1.25 markus 954:
955: /* get padlen */
1.76 stevesk 956: cp = buffer_ptr(&incoming_packet);
1.89 markus 957: padlen = cp[4];
1.25 markus 958: DBG(debug("input: padlen %d", padlen));
959: if (padlen < 4)
960: packet_disconnect("Corrupted padlen %d on input.", padlen);
961:
962: /* skip packet size + padlen, discard padding */
963: buffer_consume(&incoming_packet, 4 + 1);
964: buffer_consume_end(&incoming_packet, padlen);
965:
966: DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
967: if (comp && comp->enabled) {
968: buffer_clear(&compression_buffer);
969: buffer_uncompress(&incoming_packet, &compression_buffer);
970: buffer_clear(&incoming_packet);
971: buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
972: buffer_len(&compression_buffer));
973: DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
974: }
975: /*
976: * get packet type, implies consume.
977: * return length of payload (without type field)
978: */
1.62 markus 979: type = buffer_get_char(&incoming_packet);
1.57 markus 980: if (type == SSH2_MSG_NEWKEYS)
981: set_newkeys(MODE_IN);
1.25 markus 982: #ifdef PACKET_DEBUG
1.55 deraadt 983: fprintf(stderr, "read/plain[%d]:\r\n", type);
1.25 markus 984: buffer_dump(&incoming_packet);
985: #endif
1.62 markus 986: /* reset for next packet */
987: packet_length = 0;
988: return type;
1.25 markus 989: }
990:
991: int
1.82 markus 992: packet_read_poll_seqnr(u_int32_t *seqnr_p)
1.25 markus 993: {
1.84 markus 994: int reason, seqnr;
1.62 markus 995: u_char type;
1.25 markus 996: char *msg;
1.62 markus 997:
1.25 markus 998: for (;;) {
1.62 markus 999: if (compat20) {
1.82 markus 1000: type = packet_read_poll2(seqnr_p);
1.62 markus 1001: if (type)
1.25 markus 1002: DBG(debug("received packet type %d", type));
1.74 deraadt 1003: switch (type) {
1.25 markus 1004: case SSH2_MSG_IGNORE:
1005: break;
1006: case SSH2_MSG_DEBUG:
1007: packet_get_char();
1008: msg = packet_get_string(NULL);
1009: debug("Remote: %.900s", msg);
1010: xfree(msg);
1011: msg = packet_get_string(NULL);
1012: xfree(msg);
1013: break;
1014: case SSH2_MSG_DISCONNECT:
1015: reason = packet_get_int();
1016: msg = packet_get_string(NULL);
1.41 markus 1017: log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
1018: reason, msg);
1.25 markus 1019: xfree(msg);
1020: fatal_cleanup();
1.84 markus 1021: break;
1022: case SSH2_MSG_UNIMPLEMENTED:
1023: seqnr = packet_get_int();
1024: debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
1.25 markus 1025: break;
1026: default:
1027: return type;
1028: break;
1.48 stevesk 1029: }
1.25 markus 1030: } else {
1.82 markus 1031: type = packet_read_poll1();
1.74 deraadt 1032: switch (type) {
1.25 markus 1033: case SSH_MSG_IGNORE:
1034: break;
1035: case SSH_MSG_DEBUG:
1036: msg = packet_get_string(NULL);
1037: debug("Remote: %.900s", msg);
1038: xfree(msg);
1039: break;
1040: case SSH_MSG_DISCONNECT:
1041: msg = packet_get_string(NULL);
1.41 markus 1042: log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
1043: msg);
1.25 markus 1044: fatal_cleanup();
1045: xfree(msg);
1046: break;
1047: default:
1.62 markus 1048: if (type)
1.25 markus 1049: DBG(debug("received packet type %d", type));
1050: return type;
1051: break;
1.48 stevesk 1052: }
1.25 markus 1053: }
1054: }
1.77 djm 1055: }
1056:
1057: int
1.82 markus 1058: packet_read_poll(void)
1.77 djm 1059: {
1.82 markus 1060: return packet_read_poll_seqnr(NULL);
1.25 markus 1061: }
1062:
1.16 markus 1063: /*
1064: * Buffers the given amount of input characters. This is intended to be used
1065: * together with packet_read_poll.
1066: */
1.1 deraadt 1067:
1.2 provos 1068: void
1.40 markus 1069: packet_process_incoming(const char *buf, u_int len)
1.1 deraadt 1070: {
1.14 markus 1071: buffer_append(&input, buf, len);
1.1 deraadt 1072: }
1073:
1074: /* Returns a character from the packet. */
1075:
1.40 markus 1076: u_int
1.73 itojun 1077: packet_get_char(void)
1.1 deraadt 1078: {
1.14 markus 1079: char ch;
1080: buffer_get(&incoming_packet, &ch, 1);
1.40 markus 1081: return (u_char) ch;
1.1 deraadt 1082: }
1083:
1084: /* Returns an integer from the packet data. */
1085:
1.40 markus 1086: u_int
1.73 itojun 1087: packet_get_int(void)
1.1 deraadt 1088: {
1.14 markus 1089: return buffer_get_int(&incoming_packet);
1.1 deraadt 1090: }
1091:
1.16 markus 1092: /*
1093: * Returns an arbitrary precision integer from the packet data. The integer
1094: * must have been initialized before this call.
1095: */
1.1 deraadt 1096:
1.2 provos 1097: void
1.80 markus 1098: packet_get_bignum(BIGNUM * value)
1.1 deraadt 1099: {
1.81 markus 1100: buffer_get_bignum(&incoming_packet, value);
1.24 markus 1101: }
1102:
1103: void
1.80 markus 1104: packet_get_bignum2(BIGNUM * value)
1.24 markus 1105: {
1.81 markus 1106: buffer_get_bignum2(&incoming_packet, value);
1.24 markus 1107: }
1108:
1.76 stevesk 1109: void *
1.24 markus 1110: packet_get_raw(int *length_ptr)
1111: {
1112: int bytes = buffer_len(&incoming_packet);
1113: if (length_ptr != NULL)
1114: *length_ptr = bytes;
1115: return buffer_ptr(&incoming_packet);
1.28 markus 1116: }
1117:
1118: int
1119: packet_remaining(void)
1120: {
1121: return buffer_len(&incoming_packet);
1.1 deraadt 1122: }
1123:
1.16 markus 1124: /*
1125: * Returns a string from the packet data. The string is allocated using
1126: * xmalloc; it is the responsibility of the calling program to free it when
1127: * no longer needed. The length_ptr argument may be NULL, or point to an
1128: * integer into which the length of the string is stored.
1129: */
1.1 deraadt 1130:
1.76 stevesk 1131: void *
1.40 markus 1132: packet_get_string(u_int *length_ptr)
1.1 deraadt 1133: {
1.14 markus 1134: return buffer_get_string(&incoming_packet, length_ptr);
1.1 deraadt 1135: }
1136:
1.16 markus 1137: /*
1138: * Sends a diagnostic message from the server to the client. This message
1139: * can be sent at any time (but not while constructing another message). The
1140: * message is printed immediately, but only if the client is being executed
1141: * in verbose mode. These messages are primarily intended to ease debugging
1142: * authentication problems. The length of the formatted message must not
1143: * exceed 1024 bytes. This will automatically call packet_write_wait.
1144: */
1.1 deraadt 1145:
1.2 provos 1146: void
1.14 markus 1147: packet_send_debug(const char *fmt,...)
1.1 deraadt 1148: {
1.14 markus 1149: char buf[1024];
1150: va_list args;
1.39 markus 1151:
1152: if (compat20 && (datafellows & SSH_BUG_DEBUG))
1153: return;
1.14 markus 1154:
1155: va_start(args, fmt);
1156: vsnprintf(buf, sizeof(buf), fmt, args);
1157: va_end(args);
1158:
1.30 markus 1159: if (compat20) {
1160: packet_start(SSH2_MSG_DEBUG);
1161: packet_put_char(0); /* bool: always display */
1162: packet_put_cstring(buf);
1163: packet_put_cstring("");
1164: } else {
1165: packet_start(SSH_MSG_DEBUG);
1166: packet_put_cstring(buf);
1167: }
1.14 markus 1168: packet_send();
1169: packet_write_wait();
1.1 deraadt 1170: }
1171:
1.16 markus 1172: /*
1173: * Logs the error plus constructs and sends a disconnect packet, closes the
1174: * connection, and exits. This function never returns. The error message
1175: * should not contain a newline. The length of the formatted message must
1176: * not exceed 1024 bytes.
1177: */
1.1 deraadt 1178:
1.2 provos 1179: void
1.14 markus 1180: packet_disconnect(const char *fmt,...)
1.1 deraadt 1181: {
1.14 markus 1182: char buf[1024];
1183: va_list args;
1184: static int disconnecting = 0;
1185: if (disconnecting) /* Guard against recursive invocations. */
1186: fatal("packet_disconnect called recursively.");
1187: disconnecting = 1;
1188:
1.16 markus 1189: /*
1190: * Format the message. Note that the caller must make sure the
1191: * message is of limited size.
1192: */
1.14 markus 1193: va_start(args, fmt);
1194: vsnprintf(buf, sizeof(buf), fmt, args);
1195: va_end(args);
1196:
1197: /* Send the disconnect message to the other side, and wait for it to get sent. */
1.25 markus 1198: if (compat20) {
1199: packet_start(SSH2_MSG_DISCONNECT);
1200: packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1201: packet_put_cstring(buf);
1202: packet_put_cstring("");
1203: } else {
1204: packet_start(SSH_MSG_DISCONNECT);
1.65 markus 1205: packet_put_cstring(buf);
1.25 markus 1206: }
1.14 markus 1207: packet_send();
1208: packet_write_wait();
1209:
1210: /* Stop listening for connections. */
1.67 markus 1211: channel_close_all();
1.14 markus 1212:
1213: /* Close the connection. */
1214: packet_close();
1.1 deraadt 1215:
1.14 markus 1216: /* Display the error locally and exit. */
1.17 markus 1217: log("Disconnecting: %.100s", buf);
1218: fatal_cleanup();
1.1 deraadt 1219: }
1220:
1.16 markus 1221: /* Checks if there is any buffered output, and tries to write some of the output. */
1.1 deraadt 1222:
1.2 provos 1223: void
1.73 itojun 1224: packet_write_poll(void)
1.1 deraadt 1225: {
1.14 markus 1226: int len = buffer_len(&output);
1227: if (len > 0) {
1228: len = write(connection_out, buffer_ptr(&output), len);
1229: if (len <= 0) {
1230: if (errno == EAGAIN)
1231: return;
1232: else
1233: fatal("Write failed: %.100s", strerror(errno));
1234: }
1235: buffer_consume(&output, len);
1236: }
1.1 deraadt 1237: }
1238:
1.16 markus 1239: /*
1240: * Calls packet_write_poll repeatedly until all pending output data has been
1241: * written.
1242: */
1.1 deraadt 1243:
1.2 provos 1244: void
1.73 itojun 1245: packet_write_wait(void)
1.1 deraadt 1246: {
1.56 millert 1247: fd_set *setp;
1248:
1249: setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1250: sizeof(fd_mask));
1.14 markus 1251: packet_write_poll();
1252: while (packet_have_data_to_write()) {
1.56 millert 1253: memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1254: sizeof(fd_mask));
1255: FD_SET(connection_out, setp);
1256: while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
1.51 deraadt 1257: (errno == EAGAIN || errno == EINTR))
1258: ;
1.14 markus 1259: packet_write_poll();
1260: }
1.56 millert 1261: xfree(setp);
1.1 deraadt 1262: }
1263:
1264: /* Returns true if there is buffered data to write to the connection. */
1265:
1.2 provos 1266: int
1.73 itojun 1267: packet_have_data_to_write(void)
1.1 deraadt 1268: {
1.14 markus 1269: return buffer_len(&output) != 0;
1.1 deraadt 1270: }
1271:
1272: /* Returns true if there is not too much data to write to the connection. */
1273:
1.2 provos 1274: int
1.73 itojun 1275: packet_not_very_much_data_to_write(void)
1.1 deraadt 1276: {
1.14 markus 1277: if (interactive_mode)
1278: return buffer_len(&output) < 16384;
1279: else
1280: return buffer_len(&output) < 128 * 1024;
1.1 deraadt 1281: }
1282:
1283: /* Informs that the current session is interactive. Sets IP flags for that. */
1284:
1.2 provos 1285: void
1.43 markus 1286: packet_set_interactive(int interactive)
1.1 deraadt 1287: {
1.43 markus 1288: static int called = 0;
1.44 markus 1289: int lowdelay = IPTOS_LOWDELAY;
1290: int throughput = IPTOS_THROUGHPUT;
1291:
1.43 markus 1292: if (called)
1293: return;
1294: called = 1;
1.1 deraadt 1295:
1.14 markus 1296: /* Record that we are in interactive mode. */
1297: interactive_mode = interactive;
1.1 deraadt 1298:
1.19 markus 1299: /* Only set socket options if using a socket. */
1300: if (!packet_connection_is_on_socket())
1.14 markus 1301: return;
1.19 markus 1302: /*
1.42 markus 1303: * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
1.19 markus 1304: */
1.14 markus 1305: if (interactive) {
1.16 markus 1306: /*
1307: * Set IP options for an interactive connection. Use
1308: * IPTOS_LOWDELAY and TCP_NODELAY.
1309: */
1.42 markus 1310: if (packet_connection_is_ipv4()) {
1311: if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
1.90 stevesk 1312: &lowdelay, sizeof(lowdelay)) < 0)
1.42 markus 1313: error("setsockopt IPTOS_LOWDELAY: %.100s",
1314: strerror(errno));
1315: }
1.86 stevesk 1316: set_nodelay(connection_in);
1.42 markus 1317: } else if (packet_connection_is_ipv4()) {
1.16 markus 1318: /*
1319: * Set IP options for a non-interactive connection. Use
1320: * IPTOS_THROUGHPUT.
1321: */
1.90 stevesk 1322: if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &throughput,
1.19 markus 1323: sizeof(throughput)) < 0)
1.14 markus 1324: error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
1325: }
1.1 deraadt 1326: }
1327:
1328: /* Returns true if the current connection is interactive. */
1329:
1.2 provos 1330: int
1.73 itojun 1331: packet_is_interactive(void)
1.1 deraadt 1332: {
1.14 markus 1333: return interactive_mode;
1.12 markus 1334: }
1335:
1336: int
1337: packet_set_maxsize(int s)
1338: {
1.14 markus 1339: static int called = 0;
1340: if (called) {
1.25 markus 1341: log("packet_set_maxsize: called twice: old %d new %d",
1342: max_packet_size, s);
1.14 markus 1343: return -1;
1344: }
1345: if (s < 4 * 1024 || s > 1024 * 1024) {
1346: log("packet_set_maxsize: bad size %d", s);
1347: return -1;
1348: }
1.70 markus 1349: called = 1;
1.66 markus 1350: debug("packet_set_maxsize: setting to %d", s);
1.14 markus 1351: max_packet_size = s;
1352: return s;
1.53 markus 1353: }
1354:
1.71 markus 1355: /* roundup current message to pad bytes */
1356: void
1357: packet_add_padding(u_char pad)
1358: {
1359: extra_pad = pad;
1360: }
1361:
1.53 markus 1362: /*
1363: * 9.2. Ignored Data Message
1.61 markus 1364: *
1.53 markus 1365: * byte SSH_MSG_IGNORE
1366: * string data
1.61 markus 1367: *
1.53 markus 1368: * All implementations MUST understand (and ignore) this message at any
1369: * time (after receiving the protocol version). No implementation is
1370: * required to send them. This message can be used as an additional
1371: * protection measure against advanced traffic analysis techniques.
1372: */
1.54 markus 1373: void
1374: packet_send_ignore(int nbytes)
1375: {
1376: u_int32_t rand = 0;
1377: int i;
1378:
1379: packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
1.53 markus 1380: packet_put_int(nbytes);
1.75 deraadt 1381: for (i = 0; i < nbytes; i++) {
1.53 markus 1382: if (i % 4 == 0)
1383: rand = arc4random();
1384: packet_put_char(rand & 0xff);
1385: rand >>= 8;
1386: }
1.1 deraadt 1387: }