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