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