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