Annotation of src/usr.bin/ssh/packet.c, Revision 1.23
1.1 deraadt 1: /*
1.15 deraadt 2: *
3: * packet.c
4: *
5: * Author: Tatu Ylonen <ylo@cs.hut.fi>
6: *
7: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: * All rights reserved
9: *
10: * Created: Sat Mar 18 02:40:40 1995 ylo
11: *
12: * This file contains code implementing the packet protocol and communication
13: * with the other side. This same code is used both on client and server side.
14: *
15: */
1.1 deraadt 16:
17: #include "includes.h"
1.23 ! markus 18: RCSID("$Id: packet.c,v 1.22 2000/02/05 10:13:11 markus Exp $");
1.1 deraadt 19:
20: #include "xmalloc.h"
21: #include "buffer.h"
22: #include "packet.h"
23: #include "bufaux.h"
24: #include "ssh.h"
25: #include "crc32.h"
26: #include "cipher.h"
27: #include "getput.h"
28:
29: #include "compress.h"
1.9 dugsong 30: #include "deattack.h"
1.23 ! markus 31: #include "channels.h"
1.1 deraadt 32:
1.16 markus 33: /*
34: * This variable contains the file descriptors used for communicating with
35: * the other side. connection_in is used for reading; connection_out for
36: * writing. These can be the same descriptor, in which case it is assumed to
37: * be a socket.
38: */
1.1 deraadt 39: static int connection_in = -1;
40: static int connection_out = -1;
41:
1.16 markus 42: /*
43: * Cipher type. This value is only used to determine whether to pad the
44: * packets with zeroes or random data.
45: */
1.1 deraadt 46: static int cipher_type = SSH_CIPHER_NONE;
47:
48: /* Protocol flags for the remote side. */
49: static unsigned int remote_protocol_flags = 0;
50:
51: /* Encryption context for receiving data. This is only used for decryption. */
52: static CipherContext receive_context;
1.14 markus 53:
54: /* Encryption context for sending data. This is only used for encryption. */
1.1 deraadt 55: static CipherContext send_context;
56:
57: /* Buffer for raw input data from the socket. */
58: static Buffer input;
59:
60: /* Buffer for raw output data going to the socket. */
61: static Buffer output;
62:
63: /* Buffer for the partial outgoing packet being constructed. */
64: static Buffer outgoing_packet;
65:
66: /* Buffer for the incoming packet currently being processed. */
67: static Buffer incoming_packet;
68:
69: /* Scratch buffer for packet compression/decompression. */
70: static Buffer compression_buffer;
71:
72: /* Flag indicating whether packet compression/decompression is enabled. */
73: static int packet_compression = 0;
74:
1.12 markus 75: /* default maximum packet size */
76: int max_packet_size = 32768;
77:
1.1 deraadt 78: /* Flag indicating whether this module has been initialized. */
79: static int initialized = 0;
80:
81: /* Set to true if the connection is interactive. */
82: static int interactive_mode = 0;
83:
1.16 markus 84: /*
85: * Sets the descriptors used for communication. Disables encryption until
86: * packet_set_encryption_key is called.
87: */
1.1 deraadt 88:
1.2 provos 89: void
90: packet_set_connection(int fd_in, int fd_out)
1.1 deraadt 91: {
1.14 markus 92: connection_in = fd_in;
93: connection_out = fd_out;
94: cipher_type = SSH_CIPHER_NONE;
95: cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 1);
96: cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 0);
97: if (!initialized) {
98: initialized = 1;
99: buffer_init(&input);
100: buffer_init(&output);
101: buffer_init(&outgoing_packet);
102: buffer_init(&incoming_packet);
103: }
104: /* Kludge: arrange the close function to be called from fatal(). */
105: fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
1.1 deraadt 106: }
107:
1.19 markus 108: /* Returns 1 if remote host is connected via socket, 0 if not. */
109:
110: int
111: packet_connection_is_on_socket()
112: {
113: struct sockaddr_storage from, to;
114: socklen_t fromlen, tolen;
115:
116: /* filedescriptors in and out are the same, so it's a socket */
117: if (connection_in == connection_out)
118: return 1;
119: fromlen = sizeof(from);
120: memset(&from, 0, sizeof(from));
1.20 markus 121: if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
1.19 markus 122: return 0;
123: tolen = sizeof(to);
124: memset(&to, 0, sizeof(to));
1.20 markus 125: if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
1.19 markus 126: return 0;
127: if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
128: return 0;
129: if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
130: return 0;
131: return 1;
132: }
133:
134: /* returns 1 if connection is via ipv4 */
135:
136: int
137: packet_connection_is_ipv4()
138: {
139: struct sockaddr_storage to;
1.21 deraadt 140: socklen_t tolen = sizeof(to);
1.19 markus 141:
142: memset(&to, 0, sizeof(to));
143: if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
144: return 0;
145: if (to.ss_family != AF_INET)
146: return 0;
147: return 1;
148: }
149:
1.1 deraadt 150: /* Sets the connection into non-blocking mode. */
151:
1.2 provos 152: void
153: packet_set_nonblocking()
1.1 deraadt 154: {
1.14 markus 155: /* Set the socket into non-blocking mode. */
156: if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
157: error("fcntl O_NONBLOCK: %.100s", strerror(errno));
158:
159: if (connection_out != connection_in) {
160: if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
161: error("fcntl O_NONBLOCK: %.100s", strerror(errno));
162: }
1.1 deraadt 163: }
164:
165: /* Returns the socket used for reading. */
166:
1.2 provos 167: int
168: packet_get_connection_in()
1.1 deraadt 169: {
1.14 markus 170: return connection_in;
1.1 deraadt 171: }
172:
173: /* Returns the descriptor used for writing. */
174:
1.2 provos 175: int
176: packet_get_connection_out()
1.1 deraadt 177: {
1.14 markus 178: return connection_out;
1.1 deraadt 179: }
180:
181: /* Closes the connection and clears and frees internal data structures. */
182:
1.2 provos 183: void
184: packet_close()
1.1 deraadt 185: {
1.14 markus 186: if (!initialized)
187: return;
188: initialized = 0;
189: if (connection_in == connection_out) {
190: shutdown(connection_out, SHUT_RDWR);
191: close(connection_out);
192: } else {
193: close(connection_in);
194: close(connection_out);
195: }
196: buffer_free(&input);
197: buffer_free(&output);
198: buffer_free(&outgoing_packet);
199: buffer_free(&incoming_packet);
200: if (packet_compression) {
201: buffer_free(&compression_buffer);
202: buffer_compress_uninit();
203: }
1.1 deraadt 204: }
205:
206: /* Sets remote side protocol flags. */
207:
1.2 provos 208: void
209: packet_set_protocol_flags(unsigned int protocol_flags)
1.1 deraadt 210: {
1.14 markus 211: remote_protocol_flags = protocol_flags;
212: channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0);
1.1 deraadt 213: }
214:
215: /* Returns the remote protocol flags set earlier by the above function. */
216:
1.2 provos 217: unsigned int
218: packet_get_protocol_flags()
1.1 deraadt 219: {
1.14 markus 220: return remote_protocol_flags;
1.1 deraadt 221: }
222:
1.16 markus 223: /*
224: * Starts packet compression from the next packet on in both directions.
225: * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
226: */
1.1 deraadt 227:
1.2 provos 228: void
229: packet_start_compression(int level)
1.1 deraadt 230: {
1.14 markus 231: if (packet_compression)
232: fatal("Compression already enabled.");
233: packet_compression = 1;
234: buffer_init(&compression_buffer);
235: buffer_compress_init(level);
1.1 deraadt 236: }
237:
1.16 markus 238: /*
239: * Encrypts the given number of bytes, copying from src to dest. bytes is
240: * known to be a multiple of 8.
241: */
1.1 deraadt 242:
1.2 provos 243: void
1.14 markus 244: packet_encrypt(CipherContext * cc, void *dest, void *src,
1.2 provos 245: unsigned int bytes)
1.1 deraadt 246: {
1.14 markus 247: cipher_encrypt(cc, dest, src, bytes);
1.1 deraadt 248: }
249:
1.16 markus 250: /*
251: * Decrypts the given number of bytes, copying from src to dest. bytes is
252: * known to be a multiple of 8.
253: */
1.1 deraadt 254:
1.2 provos 255: void
1.14 markus 256: packet_decrypt(CipherContext * cc, void *dest, void *src,
1.2 provos 257: unsigned int bytes)
1.1 deraadt 258: {
1.14 markus 259: int i;
260:
261: if ((bytes % 8) != 0)
262: fatal("packet_decrypt: bad ciphertext length %d", bytes);
263:
1.16 markus 264: /*
265: * Cryptographic attack detector for ssh - Modifications for packet.c
266: * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com)
267: */
1.14 markus 268:
269: switch (cc->type) {
270: case SSH_CIPHER_NONE:
271: i = DEATTACK_OK;
272: break;
273: default:
274: i = detect_attack(src, bytes, NULL);
275: break;
276: }
277:
278: if (i == DEATTACK_DETECTED)
279: packet_disconnect("crc32 compensation attack: network attack detected");
280:
281: cipher_decrypt(cc, dest, src, bytes);
1.1 deraadt 282: }
283:
1.16 markus 284: /*
285: * Causes any further packets to be encrypted using the given key. The same
286: * key is used for both sending and reception. However, both directions are
287: * encrypted independently of each other.
288: */
1.1 deraadt 289:
1.2 provos 290: void
291: packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
1.11 markus 292: int cipher)
1.1 deraadt 293: {
1.14 markus 294: /* All other ciphers use the same key in both directions for now. */
295: cipher_set_key(&receive_context, cipher, key, keylen, 0);
296: cipher_set_key(&send_context, cipher, key, keylen, 1);
1.1 deraadt 297: }
298:
299: /* Starts constructing a packet to send. */
300:
1.2 provos 301: void
302: packet_start(int type)
1.1 deraadt 303: {
1.14 markus 304: char buf[9];
1.1 deraadt 305:
1.14 markus 306: buffer_clear(&outgoing_packet);
307: memset(buf, 0, 8);
308: buf[8] = type;
309: buffer_append(&outgoing_packet, buf, 9);
1.1 deraadt 310: }
311:
312: /* Appends a character to the packet data. */
313:
1.2 provos 314: void
315: packet_put_char(int value)
1.1 deraadt 316: {
1.14 markus 317: char ch = value;
318: buffer_append(&outgoing_packet, &ch, 1);
1.1 deraadt 319: }
320:
321: /* Appends an integer to the packet data. */
322:
1.2 provos 323: void
324: packet_put_int(unsigned int value)
1.1 deraadt 325: {
1.14 markus 326: buffer_put_int(&outgoing_packet, value);
1.1 deraadt 327: }
328:
329: /* Appends a string to packet data. */
330:
1.2 provos 331: void
332: packet_put_string(const char *buf, unsigned int len)
1.1 deraadt 333: {
1.14 markus 334: buffer_put_string(&outgoing_packet, buf, len);
1.1 deraadt 335: }
336:
337: /* Appends an arbitrary precision integer to packet data. */
338:
1.2 provos 339: void
1.14 markus 340: packet_put_bignum(BIGNUM * value)
1.1 deraadt 341: {
1.14 markus 342: buffer_put_bignum(&outgoing_packet, value);
1.1 deraadt 343: }
344:
1.16 markus 345: /*
346: * Finalizes and sends the packet. If the encryption key has been set,
347: * encrypts the packet before sending.
348: */
1.14 markus 349:
1.2 provos 350: void
351: packet_send()
1.1 deraadt 352: {
1.14 markus 353: char buf[8], *cp;
354: int i, padding, len;
355: unsigned int checksum;
356: u_int32_t rand = 0;
357:
1.16 markus 358: /*
359: * If using packet compression, compress the payload of the outgoing
360: * packet.
361: */
1.14 markus 362: if (packet_compression) {
363: buffer_clear(&compression_buffer);
364: /* Skip padding. */
365: buffer_consume(&outgoing_packet, 8);
366: /* padding */
367: buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
368: buffer_compress(&outgoing_packet, &compression_buffer);
369: buffer_clear(&outgoing_packet);
370: buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
371: buffer_len(&compression_buffer));
372: }
373: /* Compute packet length without padding (add checksum, remove padding). */
374: len = buffer_len(&outgoing_packet) + 4 - 8;
375:
376: /* Insert padding. */
377: padding = 8 - len % 8;
378: if (cipher_type != SSH_CIPHER_NONE) {
379: cp = buffer_ptr(&outgoing_packet);
380: for (i = 0; i < padding; i++) {
381: if (i % 4 == 0)
382: rand = arc4random();
383: cp[7 - i] = rand & 0xff;
384: rand >>= 8;
385: }
386: }
387: buffer_consume(&outgoing_packet, 8 - padding);
388:
389: /* Add check bytes. */
390: checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet),
391: buffer_len(&outgoing_packet));
392: PUT_32BIT(buf, checksum);
393: buffer_append(&outgoing_packet, buf, 4);
1.1 deraadt 394:
395: #ifdef PACKET_DEBUG
1.14 markus 396: fprintf(stderr, "packet_send plain: ");
397: buffer_dump(&outgoing_packet);
1.1 deraadt 398: #endif
399:
1.14 markus 400: /* Append to output. */
401: PUT_32BIT(buf, len);
402: buffer_append(&output, buf, 4);
403: buffer_append_space(&output, &cp, buffer_len(&outgoing_packet));
404: packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
405: buffer_len(&outgoing_packet));
406:
1.1 deraadt 407: #ifdef PACKET_DEBUG
1.14 markus 408: fprintf(stderr, "encrypted: ");
409: buffer_dump(&output);
1.1 deraadt 410: #endif
411:
1.14 markus 412: buffer_clear(&outgoing_packet);
1.1 deraadt 413:
1.16 markus 414: /*
415: * Note that the packet is now only buffered in output. It won\'t be
416: * actually sent until packet_write_wait or packet_write_poll is
417: * called.
418: */
1.1 deraadt 419: }
420:
1.16 markus 421: /*
422: * Waits until a packet has been received, and returns its type. Note that
423: * no other data is processed until this returns, so this function should not
424: * be used during the interactive session.
425: */
1.1 deraadt 426:
1.2 provos 427: int
428: packet_read(int *payload_len_ptr)
1.1 deraadt 429: {
1.14 markus 430: int type, len;
431: fd_set set;
432: char buf[8192];
433:
434: /* Since we are blocking, ensure that all written packets have been sent. */
435: packet_write_wait();
436:
437: /* Stay in the loop until we have received a complete packet. */
438: for (;;) {
439: /* Try to read a packet from the buffer. */
440: type = packet_read_poll(payload_len_ptr);
441: if (type == SSH_SMSG_SUCCESS
442: || type == SSH_SMSG_FAILURE
443: || type == SSH_CMSG_EOF
444: || type == SSH_CMSG_EXIT_CONFIRMATION)
445: packet_integrity_check(*payload_len_ptr, 0, type);
446: /* If we got a packet, return it. */
447: if (type != SSH_MSG_NONE)
448: return type;
1.16 markus 449: /*
450: * Otherwise, wait for some data to arrive, add it to the
451: * buffer, and try again.
452: */
1.14 markus 453: FD_ZERO(&set);
454: FD_SET(connection_in, &set);
1.16 markus 455:
1.14 markus 456: /* Wait for some data to arrive. */
457: select(connection_in + 1, &set, NULL, NULL, NULL);
1.16 markus 458:
1.14 markus 459: /* Read data from the socket. */
460: len = read(connection_in, buf, sizeof(buf));
1.18 markus 461: if (len == 0) {
462: log("Connection closed by %.200s", get_remote_ipaddr());
463: fatal_cleanup();
464: }
1.14 markus 465: if (len < 0)
466: fatal("Read from socket failed: %.100s", strerror(errno));
467: /* Append it to the buffer. */
468: packet_process_incoming(buf, len);
469: }
470: /* NOTREACHED */
1.1 deraadt 471: }
472:
1.16 markus 473: /*
474: * Waits until a packet has been received, verifies that its type matches
475: * that given, and gives a fatal error and exits if there is a mismatch.
476: */
1.1 deraadt 477:
1.2 provos 478: void
479: packet_read_expect(int *payload_len_ptr, int expected_type)
1.1 deraadt 480: {
1.14 markus 481: int type;
1.1 deraadt 482:
1.14 markus 483: type = packet_read(payload_len_ptr);
484: if (type != expected_type)
485: packet_disconnect("Protocol error: expected packet type %d, got %d",
486: expected_type, type);
1.1 deraadt 487: }
488:
489: /* Checks if a full packet is available in the data received so far via
1.14 markus 490: * packet_process_incoming. If so, reads the packet; otherwise returns
491: * SSH_MSG_NONE. This does not wait for data from the connection.
492: *
493: * SSH_MSG_DISCONNECT is handled specially here. Also,
494: * SSH_MSG_IGNORE messages are skipped by this function and are never returned
495: * to higher levels.
496: *
497: * The returned payload_len does include space consumed by:
498: * Packet length
499: * Padding
500: * Packet type
501: * Check bytes
502: */
1.1 deraadt 503:
1.2 provos 504: int
505: packet_read_poll(int *payload_len_ptr)
1.1 deraadt 506: {
1.14 markus 507: unsigned int len, padded_len;
508: unsigned char *ucp;
1.22 markus 509: char buf[8], *cp, *msg;
1.14 markus 510: unsigned int checksum, stored_checksum;
511:
512: restart:
513:
514: /* Check if input size is less than minimum packet size. */
515: if (buffer_len(&input) < 4 + 8)
516: return SSH_MSG_NONE;
517: /* Get length of incoming packet. */
518: ucp = (unsigned char *) buffer_ptr(&input);
519: len = GET_32BIT(ucp);
520: if (len < 1 + 2 + 2 || len > 256 * 1024)
521: packet_disconnect("Bad packet length %d.", len);
522: padded_len = (len + 8) & ~7;
523:
524: /* Check if the packet has been entirely received. */
525: if (buffer_len(&input) < 4 + padded_len)
526: return SSH_MSG_NONE;
527:
528: /* The entire packet is in buffer. */
529:
530: /* Consume packet length. */
531: buffer_consume(&input, 4);
532:
533: /* Copy data to incoming_packet. */
534: buffer_clear(&incoming_packet);
535: buffer_append_space(&incoming_packet, &cp, padded_len);
536: packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
537: buffer_consume(&input, padded_len);
1.1 deraadt 538:
539: #ifdef PACKET_DEBUG
1.14 markus 540: fprintf(stderr, "read_poll plain: ");
541: buffer_dump(&incoming_packet);
1.1 deraadt 542: #endif
543:
1.14 markus 544: /* Compute packet checksum. */
545: checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet),
546: buffer_len(&incoming_packet) - 4);
547:
548: /* Skip padding. */
549: buffer_consume(&incoming_packet, 8 - len % 8);
550:
551: /* Test check bytes. */
552:
553: if (len != buffer_len(&incoming_packet))
554: packet_disconnect("packet_read_poll: len %d != buffer_len %d.",
555: len, buffer_len(&incoming_packet));
556:
557: ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4;
558: stored_checksum = GET_32BIT(ucp);
559: if (checksum != stored_checksum)
560: packet_disconnect("Corrupted check bytes on input.");
561: buffer_consume_end(&incoming_packet, 4);
562:
563: /* If using packet compression, decompress the packet. */
564: if (packet_compression) {
565: buffer_clear(&compression_buffer);
566: buffer_uncompress(&incoming_packet, &compression_buffer);
567: buffer_clear(&incoming_packet);
568: buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
569: buffer_len(&compression_buffer));
570: }
571: /* Get packet type. */
572: buffer_get(&incoming_packet, &buf[0], 1);
573:
574: /* Return length of payload (without type field). */
575: *payload_len_ptr = buffer_len(&incoming_packet);
576:
577: /* Handle disconnect message. */
1.17 markus 578: if ((unsigned char) buf[0] == SSH_MSG_DISCONNECT) {
1.22 markus 579: msg = packet_get_string(NULL);
580: log("Received disconnect: %.900s", msg);
581: xfree(msg);
1.17 markus 582: fatal_cleanup();
583: }
1.14 markus 584:
585: /* Ignore ignore messages. */
586: if ((unsigned char) buf[0] == SSH_MSG_IGNORE)
587: goto restart;
588:
589: /* Send debug messages as debugging output. */
590: if ((unsigned char) buf[0] == SSH_MSG_DEBUG) {
1.22 markus 591: msg = packet_get_string(NULL);
592: debug("Remote: %.900s", msg);
593: xfree(msg);
1.14 markus 594: goto restart;
595: }
596: /* Return type. */
597: return (unsigned char) buf[0];
1.1 deraadt 598: }
1.14 markus 599:
1.16 markus 600: /*
601: * Buffers the given amount of input characters. This is intended to be used
602: * together with packet_read_poll.
603: */
1.1 deraadt 604:
1.2 provos 605: void
606: packet_process_incoming(const char *buf, unsigned int len)
1.1 deraadt 607: {
1.14 markus 608: buffer_append(&input, buf, len);
1.1 deraadt 609: }
610:
611: /* Returns a character from the packet. */
612:
1.2 provos 613: unsigned int
614: packet_get_char()
1.1 deraadt 615: {
1.14 markus 616: char ch;
617: buffer_get(&incoming_packet, &ch, 1);
618: return (unsigned char) ch;
1.1 deraadt 619: }
620:
621: /* Returns an integer from the packet data. */
622:
1.2 provos 623: unsigned int
624: packet_get_int()
1.1 deraadt 625: {
1.14 markus 626: return buffer_get_int(&incoming_packet);
1.1 deraadt 627: }
628:
1.16 markus 629: /*
630: * Returns an arbitrary precision integer from the packet data. The integer
631: * must have been initialized before this call.
632: */
1.1 deraadt 633:
1.2 provos 634: void
1.14 markus 635: packet_get_bignum(BIGNUM * value, int *length_ptr)
1.1 deraadt 636: {
1.14 markus 637: *length_ptr = buffer_get_bignum(&incoming_packet, value);
1.1 deraadt 638: }
639:
1.16 markus 640: /*
641: * Returns a string from the packet data. The string is allocated using
642: * xmalloc; it is the responsibility of the calling program to free it when
643: * no longer needed. The length_ptr argument may be NULL, or point to an
644: * integer into which the length of the string is stored.
645: */
1.1 deraadt 646:
1.16 markus 647: char *
1.14 markus 648: packet_get_string(unsigned int *length_ptr)
1.1 deraadt 649: {
1.14 markus 650: return buffer_get_string(&incoming_packet, length_ptr);
1.1 deraadt 651: }
652:
1.16 markus 653: /*
654: * Sends a diagnostic message from the server to the client. This message
655: * can be sent at any time (but not while constructing another message). The
656: * message is printed immediately, but only if the client is being executed
657: * in verbose mode. These messages are primarily intended to ease debugging
658: * authentication problems. The length of the formatted message must not
659: * exceed 1024 bytes. This will automatically call packet_write_wait.
660: */
1.1 deraadt 661:
1.2 provos 662: void
1.14 markus 663: packet_send_debug(const char *fmt,...)
1.1 deraadt 664: {
1.14 markus 665: char buf[1024];
666: va_list args;
667:
668: va_start(args, fmt);
669: vsnprintf(buf, sizeof(buf), fmt, args);
670: va_end(args);
671:
672: packet_start(SSH_MSG_DEBUG);
673: packet_put_string(buf, strlen(buf));
674: packet_send();
675: packet_write_wait();
1.1 deraadt 676: }
677:
1.16 markus 678: /*
679: * Logs the error plus constructs and sends a disconnect packet, closes the
680: * connection, and exits. This function never returns. The error message
681: * should not contain a newline. The length of the formatted message must
682: * not exceed 1024 bytes.
683: */
1.1 deraadt 684:
1.2 provos 685: void
1.14 markus 686: packet_disconnect(const char *fmt,...)
1.1 deraadt 687: {
1.14 markus 688: char buf[1024];
689: va_list args;
690: static int disconnecting = 0;
691: if (disconnecting) /* Guard against recursive invocations. */
692: fatal("packet_disconnect called recursively.");
693: disconnecting = 1;
694:
1.16 markus 695: /*
696: * Format the message. Note that the caller must make sure the
697: * message is of limited size.
698: */
1.14 markus 699: va_start(args, fmt);
700: vsnprintf(buf, sizeof(buf), fmt, args);
701: va_end(args);
702:
703: /* Send the disconnect message to the other side, and wait for it to get sent. */
704: packet_start(SSH_MSG_DISCONNECT);
705: packet_put_string(buf, strlen(buf));
706: packet_send();
707: packet_write_wait();
708:
709: /* Stop listening for connections. */
710: channel_stop_listening();
711:
712: /* Close the connection. */
713: packet_close();
1.1 deraadt 714:
1.14 markus 715: /* Display the error locally and exit. */
1.17 markus 716: log("Disconnecting: %.100s", buf);
717: fatal_cleanup();
1.1 deraadt 718: }
719:
1.16 markus 720: /* Checks if there is any buffered output, and tries to write some of the output. */
1.1 deraadt 721:
1.2 provos 722: void
723: packet_write_poll()
1.1 deraadt 724: {
1.14 markus 725: int len = buffer_len(&output);
726: if (len > 0) {
727: len = write(connection_out, buffer_ptr(&output), len);
728: if (len <= 0) {
729: if (errno == EAGAIN)
730: return;
731: else
732: fatal("Write failed: %.100s", strerror(errno));
733: }
734: buffer_consume(&output, len);
735: }
1.1 deraadt 736: }
737:
1.16 markus 738: /*
739: * Calls packet_write_poll repeatedly until all pending output data has been
740: * written.
741: */
1.1 deraadt 742:
1.2 provos 743: void
744: packet_write_wait()
1.1 deraadt 745: {
1.14 markus 746: packet_write_poll();
747: while (packet_have_data_to_write()) {
748: fd_set set;
749: FD_ZERO(&set);
750: FD_SET(connection_out, &set);
751: select(connection_out + 1, NULL, &set, NULL, NULL);
752: packet_write_poll();
753: }
1.1 deraadt 754: }
755:
756: /* Returns true if there is buffered data to write to the connection. */
757:
1.2 provos 758: int
759: packet_have_data_to_write()
1.1 deraadt 760: {
1.14 markus 761: return buffer_len(&output) != 0;
1.1 deraadt 762: }
763:
764: /* Returns true if there is not too much data to write to the connection. */
765:
1.2 provos 766: int
767: packet_not_very_much_data_to_write()
1.1 deraadt 768: {
1.14 markus 769: if (interactive_mode)
770: return buffer_len(&output) < 16384;
771: else
772: return buffer_len(&output) < 128 * 1024;
1.1 deraadt 773: }
774:
775: /* Informs that the current session is interactive. Sets IP flags for that. */
776:
1.2 provos 777: void
778: packet_set_interactive(int interactive, int keepalives)
1.1 deraadt 779: {
1.14 markus 780: int on = 1;
1.1 deraadt 781:
1.14 markus 782: /* Record that we are in interactive mode. */
783: interactive_mode = interactive;
1.1 deraadt 784:
1.19 markus 785: /* Only set socket options if using a socket. */
786: if (!packet_connection_is_on_socket())
1.14 markus 787: return;
788: if (keepalives) {
789: /* Set keepalives if requested. */
790: if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
1.19 markus 791: sizeof(on)) < 0)
1.14 markus 792: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
793: }
1.19 markus 794: /*
795: * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only
796: */
797: if (!packet_connection_is_ipv4())
798: return;
1.14 markus 799: if (interactive) {
1.16 markus 800: /*
801: * Set IP options for an interactive connection. Use
802: * IPTOS_LOWDELAY and TCP_NODELAY.
803: */
1.14 markus 804: int lowdelay = IPTOS_LOWDELAY;
805: if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay,
1.19 markus 806: sizeof(lowdelay)) < 0)
1.14 markus 807: error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno));
808: if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
1.19 markus 809: sizeof(on)) < 0)
1.14 markus 810: error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
811: } else {
1.16 markus 812: /*
813: * Set IP options for a non-interactive connection. Use
814: * IPTOS_THROUGHPUT.
815: */
1.14 markus 816: int throughput = IPTOS_THROUGHPUT;
817: if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
1.19 markus 818: sizeof(throughput)) < 0)
1.14 markus 819: error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
820: }
1.1 deraadt 821: }
822:
823: /* Returns true if the current connection is interactive. */
824:
1.2 provos 825: int
826: packet_is_interactive()
1.1 deraadt 827: {
1.14 markus 828: return interactive_mode;
1.12 markus 829: }
830:
831: int
832: packet_set_maxsize(int s)
833: {
1.14 markus 834: static int called = 0;
835: if (called) {
836: log("packet_set_maxsize: called twice: old %d new %d", max_packet_size, s);
837: return -1;
838: }
839: if (s < 4 * 1024 || s > 1024 * 1024) {
840: log("packet_set_maxsize: bad size %d", s);
841: return -1;
842: }
843: log("packet_set_maxsize: setting to %d", s);
844: max_packet_size = s;
845: return s;
1.1 deraadt 846: }