blob: 794659b8af6ab728d258c7f5e8d55fcb5db87eaa [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * 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.
Damien Miller33b13562000-04-04 14:38:59 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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".
Damien Miller33b13562000-04-04 14:38:59 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
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.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Miller963f6b22002-02-19 15:21:23 +110040RCSID("$OpenBSD: packet.c,v 1.88 2002/02/14 23:41:01 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "xmalloc.h"
43#include "buffer.h"
44#include "packet.h"
45#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "getput.h"
48
49#include "compress.h"
50#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000051#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller33b13562000-04-04 14:38:59 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100055#include "ssh2.h"
56
Damien Miller874d77b2000-10-14 16:23:11 +110057#include "cipher.h"
Damien Miller33b13562000-04-04 14:38:59 +100058#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000059#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "log.h"
61#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110062#include "misc.h"
Damien Miller33b13562000-04-04 14:38:59 +100063
64#ifdef PACKET_DEBUG
65#define DBG(x) x
66#else
67#define DBG(x)
68#endif
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * This variable contains the file descriptors used for communicating with
72 * the other side. connection_in is used for reading; connection_out for
73 * writing. These can be the same descriptor, in which case it is assumed to
74 * be a socket.
75 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076static int connection_in = -1;
77static int connection_out = -1;
78
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000080static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82/* Encryption context for receiving data. This is only used for decryption. */
83static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110084
85/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086static CipherContext send_context;
87
88/* Buffer for raw input data from the socket. */
89static Buffer input;
90
91/* Buffer for raw output data going to the socket. */
92static Buffer output;
93
94/* Buffer for the partial outgoing packet being constructed. */
95static Buffer outgoing_packet;
96
97/* Buffer for the incoming packet currently being processed. */
98static Buffer incoming_packet;
99
100/* Scratch buffer for packet compression/decompression. */
101static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000102static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* Flag indicating whether packet compression/decompression is enabled. */
105static int packet_compression = 0;
106
Damien Miller6162d121999-11-21 13:23:52 +1100107/* default maximum packet size */
108int max_packet_size = 32768;
109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110/* Flag indicating whether this module has been initialized. */
111static int initialized = 0;
112
113/* Set to true if the connection is interactive. */
114static int interactive_mode = 0;
115
Damien Miller33b13562000-04-04 14:38:59 +1000116/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000117Newkeys *newkeys[MODE_MAX];
Damien Miller33b13562000-04-04 14:38:59 +1000118
Damien Miller9f643902001-11-12 11:02:52 +1100119/* roundup current message to extra_pad bytes */
120static u_char extra_pad = 0;
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Sets the descriptors used for communication. Disables encryption until
124 * packet_set_encryption_key is called.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126void
127packet_set_connection(int fd_in, int fd_out)
128{
Damien Miller874d77b2000-10-14 16:23:11 +1100129 Cipher *none = cipher_by_name("none");
130 if (none == NULL)
131 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100132 connection_in = fd_in;
133 connection_out = fd_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100134 cipher_init(&send_context, none, "", 0, NULL, 0, CIPHER_ENCRYPT);
135 cipher_init(&receive_context, none, "", 0, NULL, 0, CIPHER_DECRYPT);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000136 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100137 if (!initialized) {
138 initialized = 1;
139 buffer_init(&input);
140 buffer_init(&output);
141 buffer_init(&outgoing_packet);
142 buffer_init(&incoming_packet);
143 }
144 /* Kludge: arrange the close function to be called from fatal(). */
145 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146}
147
Damien Miller34132e52000-01-14 15:45:46 +1100148/* Returns 1 if remote host is connected via socket, 0 if not. */
149
150int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000151packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100152{
153 struct sockaddr_storage from, to;
154 socklen_t fromlen, tolen;
155
156 /* filedescriptors in and out are the same, so it's a socket */
157 if (connection_in == connection_out)
158 return 1;
159 fromlen = sizeof(from);
160 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100161 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100162 return 0;
163 tolen = sizeof(to);
164 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100165 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100166 return 0;
167 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
168 return 0;
169 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
170 return 0;
171 return 1;
172}
173
174/* returns 1 if connection is via ipv4 */
175
176int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000177packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100178{
179 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100180 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100181
182 memset(&to, 0, sizeof(to));
183 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
184 return 0;
185 if (to.ss_family != AF_INET)
186 return 0;
187 return 1;
188}
189
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190/* Sets the connection into non-blocking mode. */
191
192void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000193packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Damien Miller95def091999-11-25 00:26:21 +1100195 /* Set the socket into non-blocking mode. */
196 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
197 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Miller95def091999-11-25 00:26:21 +1100199 if (connection_out != connection_in) {
200 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
201 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203}
204
205/* Returns the socket used for reading. */
206
207int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000208packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209{
Damien Miller95def091999-11-25 00:26:21 +1100210 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211}
212
213/* Returns the descriptor used for writing. */
214
215int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000216packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217{
Damien Miller95def091999-11-25 00:26:21 +1100218 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219}
220
221/* Closes the connection and clears and frees internal data structures. */
222
223void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000224packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Damien Miller95def091999-11-25 00:26:21 +1100226 if (!initialized)
227 return;
228 initialized = 0;
229 if (connection_in == connection_out) {
230 shutdown(connection_out, SHUT_RDWR);
231 close(connection_out);
232 } else {
233 close(connection_in);
234 close(connection_out);
235 }
236 buffer_free(&input);
237 buffer_free(&output);
238 buffer_free(&outgoing_packet);
239 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000240 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100241 buffer_free(&compression_buffer);
242 buffer_compress_uninit();
243 }
Damien Miller963f6b22002-02-19 15:21:23 +1100244 cipher_cleanup(&send_context);
245 cipher_cleanup(&receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
248/* Sets remote side protocol flags. */
249
250void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000251packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Damien Miller95def091999-11-25 00:26:21 +1100253 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
256/* Returns the remote protocol flags set earlier by the above function. */
257
Ben Lindstrom46c16222000-12-22 01:43:59 +0000258u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000259packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Miller95def091999-11-25 00:26:21 +1100261 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262}
263
Damien Miller5428f641999-11-25 11:54:57 +1100264/*
265 * Starts packet compression from the next packet on in both directions.
266 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
267 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static void
270packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000271{
272 if (compression_buffer_ready == 1)
273 return;
274 compression_buffer_ready = 1;
275 buffer_init(&compression_buffer);
276}
277
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278void
279packet_start_compression(int level)
280{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000281 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100282 fatal("Compression already enabled.");
283 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000284 packet_init_compression();
285 buffer_compress_init_send(level);
286 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287}
288
Damien Miller5428f641999-11-25 11:54:57 +1100289/*
Damien Miller5428f641999-11-25 11:54:57 +1100290 * Causes any further packets to be encrypted using the given key. The same
291 * key is used for both sending and reception. However, both directions are
292 * encrypted independently of each other.
293 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000295packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100296 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297{
Damien Miller874d77b2000-10-14 16:23:11 +1100298 Cipher *cipher = cipher_by_number(number);
299 if (cipher == NULL)
300 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000301 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100302 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Damien Miller963f6b22002-02-19 15:21:23 +1100303 cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT);
304 cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305}
306
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000307/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000309packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000311 u_char buf[9];
312 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Ben Lindstrom204e4882001-03-05 06:47:00 +0000314 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000315 len = compat20 ? 6 : 9;
316 memset(buf, 0, len - 1);
317 buf[len - 1] = type;
318 buffer_clear(&outgoing_packet);
319 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000320}
321
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000322/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323void
324packet_put_char(int value)
325{
Damien Miller95def091999-11-25 00:26:21 +1100326 char ch = value;
327 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000330packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Miller95def091999-11-25 00:26:21 +1100332 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100335packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336{
Damien Miller95def091999-11-25 00:26:21 +1100337 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
Damien Miller33b13562000-04-04 14:38:59 +1000339void
340packet_put_cstring(const char *str)
341{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000342 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000343}
Damien Miller33b13562000-04-04 14:38:59 +1000344void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100345packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000346{
347 buffer_append(&outgoing_packet, buf, len);
348}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349void
Damien Miller95def091999-11-25 00:26:21 +1100350packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
Damien Miller95def091999-11-25 00:26:21 +1100352 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353}
Damien Miller33b13562000-04-04 14:38:59 +1000354void
355packet_put_bignum2(BIGNUM * value)
356{
357 buffer_put_bignum2(&outgoing_packet, value);
358}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Miller5428f641999-11-25 11:54:57 +1100360/*
361 * Finalizes and sends the packet. If the encryption key has been set,
362 * encrypts the packet before sending.
363 */
Damien Miller95def091999-11-25 00:26:21 +1100364
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000366packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367{
Damien Miller95def091999-11-25 00:26:21 +1100368 char buf[8], *cp;
369 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000370 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100371 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller5428f641999-11-25 11:54:57 +1100373 /*
374 * If using packet compression, compress the payload of the outgoing
375 * packet.
376 */
Damien Miller95def091999-11-25 00:26:21 +1100377 if (packet_compression) {
378 buffer_clear(&compression_buffer);
379 /* Skip padding. */
380 buffer_consume(&outgoing_packet, 8);
381 /* padding */
382 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
383 buffer_compress(&outgoing_packet, &compression_buffer);
384 buffer_clear(&outgoing_packet);
385 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100386 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100387 }
388 /* Compute packet length without padding (add checksum, remove padding). */
389 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390
Damien Millere247cc42000-05-07 12:03:14 +1000391 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100392 padding = 8 - len % 8;
Damien Miller963f6b22002-02-19 15:21:23 +1100393 if (!send_context.plaintext) {
Damien Miller95def091999-11-25 00:26:21 +1100394 cp = buffer_ptr(&outgoing_packet);
395 for (i = 0; i < padding; i++) {
396 if (i % 4 == 0)
397 rand = arc4random();
398 cp[7 - i] = rand & 0xff;
399 rand >>= 8;
400 }
401 }
402 buffer_consume(&outgoing_packet, 8 - padding);
403
404 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100405 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000406 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100407 PUT_32BIT(buf, checksum);
408 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
410#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100411 fprintf(stderr, "packet_send plain: ");
412 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413#endif
414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Append to output. */
416 PUT_32BIT(buf, len);
417 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100418 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100419 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100420 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100423 fprintf(stderr, "encrypted: ");
424 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425#endif
426
Damien Miller95def091999-11-25 00:26:21 +1100427 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Damien Miller5428f641999-11-25 11:54:57 +1100429 /*
430 * Note that the packet is now only buffered in output. It won\'t be
431 * actually sent until packet_write_wait or packet_write_poll is
432 * called.
433 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Ben Lindstrombba81212001-06-25 05:01:22 +0000436static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000437set_newkeys(int mode)
438{
439 Enc *enc;
440 Mac *mac;
441 Comp *comp;
442 CipherContext *cc;
Damien Miller963f6b22002-02-19 15:21:23 +1100443 int encrypt;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000444
445 debug("newkeys: mode %d", mode);
446
Damien Miller963f6b22002-02-19 15:21:23 +1100447 if (mode == MODE_OUT) {
448 cc = &send_context;
449 encrypt = CIPHER_ENCRYPT;
450 } else {
451 cc = &receive_context;
452 encrypt = CIPHER_DECRYPT;
453 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000454 if (newkeys[mode] != NULL) {
455 debug("newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100456 cipher_cleanup(cc);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000457 enc = &newkeys[mode]->enc;
458 mac = &newkeys[mode]->mac;
459 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000460 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000461 xfree(enc->name);
462 xfree(enc->iv);
463 xfree(enc->key);
464 xfree(mac->name);
465 xfree(mac->key);
466 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000467 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000468 }
469 newkeys[mode] = kex_get_newkeys(mode);
470 if (newkeys[mode] == NULL)
471 fatal("newkeys: no keys for mode %d", mode);
472 enc = &newkeys[mode]->enc;
473 mac = &newkeys[mode]->mac;
474 comp = &newkeys[mode]->comp;
475 if (mac->md != NULL)
476 mac->enabled = 1;
477 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100478 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
479 enc->iv, enc->block_size, encrypt);
480 memset(enc->iv, 0, enc->block_size);
481 memset(enc->key, 0, enc->key_len);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000482 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000483 packet_init_compression();
484 if (mode == MODE_OUT)
485 buffer_compress_init_send(6);
486 else
487 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000488 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000489 }
490}
491
Damien Miller5428f641999-11-25 11:54:57 +1100492/*
Damien Miller33b13562000-04-04 14:38:59 +1000493 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
494 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000495static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000496packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000497{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000498 static u_int32_t seqnr = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000499 u_char type, *ucp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100500 u_char padlen, pad;
Damien Miller33b13562000-04-04 14:38:59 +1000501 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000502 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100503 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000504 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000505 Enc *enc = NULL;
506 Mac *mac = NULL;
507 Comp *comp = NULL;
508 int block_size;
509
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000510 if (newkeys[MODE_OUT] != NULL) {
511 enc = &newkeys[MODE_OUT]->enc;
512 mac = &newkeys[MODE_OUT]->mac;
513 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000514 }
Damien Miller963f6b22002-02-19 15:21:23 +1100515 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000516
Damien Miller708d21c2002-01-22 23:18:15 +1100517 ucp = buffer_ptr(&outgoing_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000518 type = ucp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000519
520#ifdef PACKET_DEBUG
521 fprintf(stderr, "plain: ");
522 buffer_dump(&outgoing_packet);
523#endif
524
525 if (comp && comp->enabled) {
526 len = buffer_len(&outgoing_packet);
527 /* skip header, compress only payload */
528 buffer_consume(&outgoing_packet, 5);
529 buffer_clear(&compression_buffer);
530 buffer_compress(&outgoing_packet, &compression_buffer);
531 buffer_clear(&outgoing_packet);
532 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
533 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
534 buffer_len(&compression_buffer));
535 DBG(debug("compression: raw %d compressed %d", len,
536 buffer_len(&outgoing_packet)));
537 }
538
539 /* sizeof (packet_len + pad_len + payload) */
540 len = buffer_len(&outgoing_packet);
541
542 /*
543 * calc size of padding, alloc space, get random data,
544 * minimum padding is 4 bytes
545 */
546 padlen = block_size - (len % block_size);
547 if (padlen < 4)
548 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100549 if (extra_pad) {
550 /* will wrap if extra_pad+padlen > 255 */
551 extra_pad = roundup(extra_pad, block_size);
552 pad = extra_pad - ((len + padlen) % extra_pad);
553 debug("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
554 pad, len, padlen, extra_pad);
555 padlen += pad;
556 extra_pad = 0;
557 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100558 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller963f6b22002-02-19 15:21:23 +1100559 if (enc && !send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000560 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000561 for (i = 0; i < padlen; i++) {
562 if (i % 4 == 0)
563 rand = arc4random();
564 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000565 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000566 }
Damien Millere247cc42000-05-07 12:03:14 +1000567 } else {
568 /* clear padding */
569 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000570 }
571 /* packet_length includes payload, padding and padding length field */
572 packet_length = buffer_len(&outgoing_packet) - 4;
Damien Miller708d21c2002-01-22 23:18:15 +1100573 ucp = buffer_ptr(&outgoing_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000574 PUT_32BIT(ucp, packet_length);
575 ucp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000576 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
577
578 /* compute MAC over seqnr and packet(length fields, payload, padding) */
579 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000580 macbuf = mac_compute(mac, seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100581 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000582 buffer_len(&outgoing_packet));
Damien Miller874d77b2000-10-14 16:23:11 +1100583 DBG(debug("done calc MAC out #%d", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000584 }
585 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100586 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Damien Miller963f6b22002-02-19 15:21:23 +1100587 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000588 buffer_len(&outgoing_packet));
589 /* append unencrypted MAC */
590 if (mac && mac->enabled)
591 buffer_append(&output, (char *)macbuf, mac->mac_len);
592#ifdef PACKET_DEBUG
593 fprintf(stderr, "encrypted: ");
594 buffer_dump(&output);
595#endif
Damien Miller4af51302000-04-16 11:18:38 +1000596 /* increment sequence number for outgoing packets */
597 if (++seqnr == 0)
598 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000599 buffer_clear(&outgoing_packet);
600
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000601 if (type == SSH2_MSG_NEWKEYS)
602 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000603}
604
605void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000606packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000607{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000608 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000609 packet_send2();
610 else
611 packet_send1();
612 DBG(debug("packet_send done"));
613}
614
Damien Miller33b13562000-04-04 14:38:59 +1000615/*
Damien Miller5428f641999-11-25 11:54:57 +1100616 * Waits until a packet has been received, and returns its type. Note that
617 * no other data is processed until this returns, so this function should not
618 * be used during the interactive session.
619 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
621int
Damien Millerdff50992002-01-22 23:16:32 +1100622packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623{
Damien Miller95def091999-11-25 00:26:21 +1100624 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000625 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100626 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000627 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000629 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
630 sizeof(fd_mask));
631
Damien Miller95def091999-11-25 00:26:21 +1100632 /* Since we are blocking, ensure that all written packets have been sent. */
633 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller95def091999-11-25 00:26:21 +1100635 /* Stay in the loop until we have received a complete packet. */
636 for (;;) {
637 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100638 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000639 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000640 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100641 || type == SSH_SMSG_FAILURE
642 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000643 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100644 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100645 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000646 if (type != SSH_MSG_NONE) {
647 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100648 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000649 }
Damien Miller5428f641999-11-25 11:54:57 +1100650 /*
651 * Otherwise, wait for some data to arrive, add it to the
652 * buffer, and try again.
653 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000654 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
655 sizeof(fd_mask));
656 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100657
Damien Miller95def091999-11-25 00:26:21 +1100658 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000659 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000660 (errno == EAGAIN || errno == EINTR))
661 ;
Damien Miller5428f641999-11-25 11:54:57 +1100662
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Read data from the socket. */
664 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100665 if (len == 0) {
666 log("Connection closed by %.200s", get_remote_ipaddr());
667 fatal_cleanup();
668 }
Damien Miller95def091999-11-25 00:26:21 +1100669 if (len < 0)
670 fatal("Read from socket failed: %.100s", strerror(errno));
671 /* Append it to the buffer. */
672 packet_process_incoming(buf, len);
673 }
674 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675}
676
Damien Miller278f9072001-12-21 15:00:19 +1100677int
Damien Millerdff50992002-01-22 23:16:32 +1100678packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100679{
Damien Millerdff50992002-01-22 23:16:32 +1100680 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100681}
682
Damien Miller5428f641999-11-25 11:54:57 +1100683/*
684 * Waits until a packet has been received, verifies that its type matches
685 * that given, and gives a fatal error and exits if there is a mismatch.
686 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
688void
Damien Millerdff50992002-01-22 23:16:32 +1100689packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690{
Damien Miller95def091999-11-25 00:26:21 +1100691 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692
Damien Millerdff50992002-01-22 23:16:32 +1100693 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100694 if (type != expected_type)
695 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000696 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697}
698
699/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100700 * packet_process_incoming. If so, reads the packet; otherwise returns
701 * SSH_MSG_NONE. This does not wait for data from the connection.
702 *
703 * SSH_MSG_DISCONNECT is handled specially here. Also,
704 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
705 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100706 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static int
Damien Millerdff50992002-01-22 23:16:32 +1100709packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000711 u_int len, padded_len;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000712 u_char *ucp, type;
713 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000714 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller95def091999-11-25 00:26:21 +1100716 /* Check if input size is less than minimum packet size. */
717 if (buffer_len(&input) < 4 + 8)
718 return SSH_MSG_NONE;
719 /* Get length of incoming packet. */
Damien Miller708d21c2002-01-22 23:18:15 +1100720 ucp = buffer_ptr(&input);
Damien Miller95def091999-11-25 00:26:21 +1100721 len = GET_32BIT(ucp);
722 if (len < 1 + 2 + 2 || len > 256 * 1024)
723 packet_disconnect("Bad packet length %d.", len);
724 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller95def091999-11-25 00:26:21 +1100726 /* Check if the packet has been entirely received. */
727 if (buffer_len(&input) < 4 + padded_len)
728 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller95def091999-11-25 00:26:21 +1100730 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller95def091999-11-25 00:26:21 +1100732 /* Consume packet length. */
733 buffer_consume(&input, 4);
734
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000735 /*
736 * Cryptographic attack detector for ssh
737 * (C)1998 CORE-SDI, Buenos Aires Argentina
738 * Ariel Futoransky(futo@core-sdi.com)
739 */
Damien Miller963f6b22002-02-19 15:21:23 +1100740 if (!receive_context.plaintext &&
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000741 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
742 packet_disconnect("crc32 compensation attack: network attack detected");
743
744 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100745 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100746 cp = buffer_append_space(&incoming_packet, padded_len);
Damien Miller963f6b22002-02-19 15:21:23 +1100747 cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000748
Damien Miller95def091999-11-25 00:26:21 +1100749 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750
751#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100752 fprintf(stderr, "read_poll plain: ");
753 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100757 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000758 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller95def091999-11-25 00:26:21 +1100760 /* Skip padding. */
761 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100762
Damien Miller95def091999-11-25 00:26:21 +1100763 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100764 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100765 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000766 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller708d21c2002-01-22 23:18:15 +1100768 ucp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller95def091999-11-25 00:26:21 +1100769 stored_checksum = GET_32BIT(ucp);
770 if (checksum != stored_checksum)
771 packet_disconnect("Corrupted check bytes on input.");
772 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773
Damien Miller95def091999-11-25 00:26:21 +1100774 if (packet_compression) {
775 buffer_clear(&compression_buffer);
776 buffer_uncompress(&incoming_packet, &compression_buffer);
777 buffer_clear(&incoming_packet);
778 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000779 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100780 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000781 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000782 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783}
Damien Miller95def091999-11-25 00:26:21 +1100784
Ben Lindstrombba81212001-06-25 05:01:22 +0000785static int
Damien Millerdff50992002-01-22 23:16:32 +1100786packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000787{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000788 static u_int32_t seqnr = 0;
789 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000790 u_int padlen, need;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000791 u_char *macbuf, *ucp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000792 char *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000793 int maclen, block_size;
794 Enc *enc = NULL;
795 Mac *mac = NULL;
796 Comp *comp = NULL;
797
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000798 if (newkeys[MODE_IN] != NULL) {
799 enc = &newkeys[MODE_IN]->enc;
800 mac = &newkeys[MODE_IN]->mac;
801 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000802 }
803 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +1100804 block_size = enc ? enc->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000805
806 if (packet_length == 0) {
807 /*
808 * check if input size is less than the cipher block size,
809 * decrypt first block and extract length of incoming packet
810 */
811 if (buffer_len(&input) < block_size)
812 return SSH_MSG_NONE;
813 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100814 cp = buffer_append_space(&incoming_packet, block_size);
Damien Miller963f6b22002-02-19 15:21:23 +1100815 cipher_crypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000816 block_size);
Damien Miller708d21c2002-01-22 23:18:15 +1100817 ucp = buffer_ptr(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000818 packet_length = GET_32BIT(ucp);
819 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
820 buffer_dump(&incoming_packet);
821 packet_disconnect("Bad packet length %d.", packet_length);
822 }
823 DBG(debug("input: packet len %d", packet_length+4));
824 buffer_consume(&input, block_size);
825 }
826 /* we have a partial packet of block_size bytes */
827 need = 4 + packet_length - block_size;
828 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
829 need, maclen));
830 if (need % block_size != 0)
831 fatal("padding error: need %d block %d mod %d",
832 need, block_size, need % block_size);
833 /*
834 * check if the entire packet has been received and
835 * decrypt into incoming_packet
836 */
837 if (buffer_len(&input) < need + maclen)
838 return SSH_MSG_NONE;
839#ifdef PACKET_DEBUG
840 fprintf(stderr, "read_poll enc/full: ");
841 buffer_dump(&input);
842#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100843 cp = buffer_append_space(&incoming_packet, need);
Damien Miller963f6b22002-02-19 15:21:23 +1100844 cipher_crypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000845 buffer_consume(&input, need);
846 /*
847 * compute MAC over seqnr and packet,
848 * increment sequence number for incoming packet
849 */
Damien Miller4af51302000-04-16 11:18:38 +1000850 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000851 macbuf = mac_compute(mac, seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100852 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000853 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000854 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100855 packet_disconnect("Corrupted MAC on input.");
856 DBG(debug("MAC #%d ok", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000857 buffer_consume(&input, mac->mac_len);
858 }
Damien Miller278f9072001-12-21 15:00:19 +1100859 if (seqnr_p != NULL)
860 *seqnr_p = seqnr;
Damien Miller4af51302000-04-16 11:18:38 +1000861 if (++seqnr == 0)
862 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000863
864 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100865 cp = buffer_ptr(&incoming_packet);
866 cp += 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000867 padlen = (u_char) *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000868 DBG(debug("input: padlen %d", padlen));
869 if (padlen < 4)
870 packet_disconnect("Corrupted padlen %d on input.", padlen);
871
872 /* skip packet size + padlen, discard padding */
873 buffer_consume(&incoming_packet, 4 + 1);
874 buffer_consume_end(&incoming_packet, padlen);
875
876 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
877 if (comp && comp->enabled) {
878 buffer_clear(&compression_buffer);
879 buffer_uncompress(&incoming_packet, &compression_buffer);
880 buffer_clear(&incoming_packet);
881 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
882 buffer_len(&compression_buffer));
883 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
884 }
885 /*
886 * get packet type, implies consume.
887 * return length of payload (without type field)
888 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000889 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000890 if (type == SSH2_MSG_NEWKEYS)
891 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +1000892#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000893 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000894 buffer_dump(&incoming_packet);
895#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000896 /* reset for next packet */
897 packet_length = 0;
898 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000899}
900
901int
Damien Millerdff50992002-01-22 23:16:32 +1100902packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000903{
Damien Miller659811f2002-01-22 23:23:11 +1100904 int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000905 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +1000906 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +1000907
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000908 for (;;) {
909 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +1100910 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000911 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000912 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000913 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000914 case SSH2_MSG_IGNORE:
915 break;
916 case SSH2_MSG_DEBUG:
917 packet_get_char();
918 msg = packet_get_string(NULL);
919 debug("Remote: %.900s", msg);
920 xfree(msg);
921 msg = packet_get_string(NULL);
922 xfree(msg);
923 break;
924 case SSH2_MSG_DISCONNECT:
925 reason = packet_get_int();
926 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000927 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
928 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +1000929 xfree(msg);
930 fatal_cleanup();
931 break;
Damien Miller659811f2002-01-22 23:23:11 +1100932 case SSH2_MSG_UNIMPLEMENTED:
933 seqnr = packet_get_int();
934 debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
935 break;
Damien Miller33b13562000-04-04 14:38:59 +1000936 default:
937 return type;
938 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000939 }
Damien Miller33b13562000-04-04 14:38:59 +1000940 } else {
Damien Millerdff50992002-01-22 23:16:32 +1100941 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000942 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000943 case SSH_MSG_IGNORE:
944 break;
945 case SSH_MSG_DEBUG:
946 msg = packet_get_string(NULL);
947 debug("Remote: %.900s", msg);
948 xfree(msg);
949 break;
950 case SSH_MSG_DISCONNECT:
951 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000952 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
953 msg);
Damien Miller33b13562000-04-04 14:38:59 +1000954 fatal_cleanup();
955 xfree(msg);
956 break;
957 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000958 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000959 DBG(debug("received packet type %d", type));
960 return type;
961 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000962 }
Damien Miller33b13562000-04-04 14:38:59 +1000963 }
964 }
965}
966
Damien Miller278f9072001-12-21 15:00:19 +1100967int
Damien Millerdff50992002-01-22 23:16:32 +1100968packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +1100969{
Damien Millerdff50992002-01-22 23:16:32 +1100970 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100971}
972
Damien Miller5428f641999-11-25 11:54:57 +1100973/*
974 * Buffers the given amount of input characters. This is intended to be used
975 * together with packet_read_poll.
976 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
978void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000979packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980{
Damien Miller95def091999-11-25 00:26:21 +1100981 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982}
983
984/* Returns a character from the packet. */
985
Ben Lindstrom46c16222000-12-22 01:43:59 +0000986u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000987packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988{
Damien Miller95def091999-11-25 00:26:21 +1100989 char ch;
990 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000991 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
994/* Returns an integer from the packet data. */
995
Ben Lindstrom46c16222000-12-22 01:43:59 +0000996u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000997packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998{
Damien Miller95def091999-11-25 00:26:21 +1100999 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000}
1001
Damien Miller5428f641999-11-25 11:54:57 +11001002/*
1003 * Returns an arbitrary precision integer from the packet data. The integer
1004 * must have been initialized before this call.
1005 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
1007void
Damien Millerd432ccf2002-01-22 23:14:44 +11001008packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009{
Damien Miller76e1e362002-01-22 23:15:57 +11001010 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011}
1012
Damien Miller33b13562000-04-04 14:38:59 +10001013void
Damien Millerd432ccf2002-01-22 23:14:44 +11001014packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001015{
Damien Miller76e1e362002-01-22 23:15:57 +11001016 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001017}
1018
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001019void *
Damien Miller33b13562000-04-04 14:38:59 +10001020packet_get_raw(int *length_ptr)
1021{
1022 int bytes = buffer_len(&incoming_packet);
1023 if (length_ptr != NULL)
1024 *length_ptr = bytes;
1025 return buffer_ptr(&incoming_packet);
1026}
1027
Damien Miller4af51302000-04-16 11:18:38 +10001028int
1029packet_remaining(void)
1030{
1031 return buffer_len(&incoming_packet);
1032}
1033
Damien Miller5428f641999-11-25 11:54:57 +11001034/*
1035 * Returns a string from the packet data. The string is allocated using
1036 * xmalloc; it is the responsibility of the calling program to free it when
1037 * no longer needed. The length_ptr argument may be NULL, or point to an
1038 * integer into which the length of the string is stored.
1039 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001041void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001042packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043{
Damien Miller95def091999-11-25 00:26:21 +11001044 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045}
1046
Damien Miller5428f641999-11-25 11:54:57 +11001047/*
1048 * Sends a diagnostic message from the server to the client. This message
1049 * can be sent at any time (but not while constructing another message). The
1050 * message is printed immediately, but only if the client is being executed
1051 * in verbose mode. These messages are primarily intended to ease debugging
1052 * authentication problems. The length of the formatted message must not
1053 * exceed 1024 bytes. This will automatically call packet_write_wait.
1054 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055
1056void
Damien Miller95def091999-11-25 00:26:21 +11001057packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058{
Damien Miller95def091999-11-25 00:26:21 +11001059 char buf[1024];
1060 va_list args;
1061
Ben Lindstroma14ee472000-12-07 01:24:58 +00001062 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1063 return;
1064
Damien Miller95def091999-11-25 00:26:21 +11001065 va_start(args, fmt);
1066 vsnprintf(buf, sizeof(buf), fmt, args);
1067 va_end(args);
1068
Damien Miller7c8af4f2000-05-01 08:24:07 +10001069 if (compat20) {
1070 packet_start(SSH2_MSG_DEBUG);
1071 packet_put_char(0); /* bool: always display */
1072 packet_put_cstring(buf);
1073 packet_put_cstring("");
1074 } else {
1075 packet_start(SSH_MSG_DEBUG);
1076 packet_put_cstring(buf);
1077 }
Damien Miller95def091999-11-25 00:26:21 +11001078 packet_send();
1079 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080}
1081
Damien Miller5428f641999-11-25 11:54:57 +11001082/*
1083 * Logs the error plus constructs and sends a disconnect packet, closes the
1084 * connection, and exits. This function never returns. The error message
1085 * should not contain a newline. The length of the formatted message must
1086 * not exceed 1024 bytes.
1087 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088
1089void
Damien Miller95def091999-11-25 00:26:21 +11001090packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091{
Damien Miller95def091999-11-25 00:26:21 +11001092 char buf[1024];
1093 va_list args;
1094 static int disconnecting = 0;
1095 if (disconnecting) /* Guard against recursive invocations. */
1096 fatal("packet_disconnect called recursively.");
1097 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098
Damien Miller5428f641999-11-25 11:54:57 +11001099 /*
1100 * Format the message. Note that the caller must make sure the
1101 * message is of limited size.
1102 */
Damien Miller95def091999-11-25 00:26:21 +11001103 va_start(args, fmt);
1104 vsnprintf(buf, sizeof(buf), fmt, args);
1105 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106
Damien Miller95def091999-11-25 00:26:21 +11001107 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001108 if (compat20) {
1109 packet_start(SSH2_MSG_DISCONNECT);
1110 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1111 packet_put_cstring(buf);
1112 packet_put_cstring("");
1113 } else {
1114 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001115 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001116 }
Damien Miller95def091999-11-25 00:26:21 +11001117 packet_send();
1118 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119
Damien Miller95def091999-11-25 00:26:21 +11001120 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001121 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122
Damien Miller95def091999-11-25 00:26:21 +11001123 /* Close the connection. */
1124 packet_close();
1125
1126 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001127 log("Disconnecting: %.100s", buf);
1128 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129}
1130
Damien Miller5428f641999-11-25 11:54:57 +11001131/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132
1133void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001134packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135{
Damien Miller95def091999-11-25 00:26:21 +11001136 int len = buffer_len(&output);
1137 if (len > 0) {
1138 len = write(connection_out, buffer_ptr(&output), len);
1139 if (len <= 0) {
1140 if (errno == EAGAIN)
1141 return;
1142 else
1143 fatal("Write failed: %.100s", strerror(errno));
1144 }
1145 buffer_consume(&output, len);
1146 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147}
1148
Damien Miller5428f641999-11-25 11:54:57 +11001149/*
1150 * Calls packet_write_poll repeatedly until all pending output data has been
1151 * written.
1152 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
1154void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001155packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001157 fd_set *setp;
1158
1159 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1160 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001161 packet_write_poll();
1162 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001163 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1164 sizeof(fd_mask));
1165 FD_SET(connection_out, setp);
1166 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001167 (errno == EAGAIN || errno == EINTR))
1168 ;
Damien Miller95def091999-11-25 00:26:21 +11001169 packet_write_poll();
1170 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001171 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172}
1173
1174/* Returns true if there is buffered data to write to the connection. */
1175
1176int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001177packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178{
Damien Miller95def091999-11-25 00:26:21 +11001179 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180}
1181
1182/* Returns true if there is not too much data to write to the connection. */
1183
1184int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001185packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186{
Damien Miller95def091999-11-25 00:26:21 +11001187 if (interactive_mode)
1188 return buffer_len(&output) < 16384;
1189 else
1190 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191}
1192
1193/* Informs that the current session is interactive. Sets IP flags for that. */
1194
1195void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001196packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001198 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001199#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001200 int lowdelay = IPTOS_LOWDELAY;
1201 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001202#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001204 if (called)
1205 return;
1206 called = 1;
1207
Damien Miller95def091999-11-25 00:26:21 +11001208 /* Record that we are in interactive mode. */
1209 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210
Damien Miller34132e52000-01-14 15:45:46 +11001211 /* Only set socket options if using a socket. */
1212 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001213 return;
Damien Miller34132e52000-01-14 15:45:46 +11001214 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001215 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001216 */
Damien Miller95def091999-11-25 00:26:21 +11001217 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001218 /*
1219 * Set IP options for an interactive connection. Use
1220 * IPTOS_LOWDELAY and TCP_NODELAY.
1221 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001222#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001223 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001224 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001225 (void *) &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001226 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001227 strerror(errno));
1228 }
Damien Miller615f9392000-05-17 22:53:33 +10001229#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001230 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001231 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001232 /*
1233 * Set IP options for a non-interactive connection. Use
1234 * IPTOS_THROUGHPUT.
1235 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001236#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001237 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001238 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001239 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001240#endif
Damien Miller95def091999-11-25 00:26:21 +11001241 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
1243
1244/* Returns true if the current connection is interactive. */
1245
1246int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001247packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248{
Damien Miller95def091999-11-25 00:26:21 +11001249 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250}
Damien Miller6162d121999-11-21 13:23:52 +11001251
1252int
1253packet_set_maxsize(int s)
1254{
Damien Miller95def091999-11-25 00:26:21 +11001255 static int called = 0;
1256 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001257 log("packet_set_maxsize: called twice: old %d new %d",
1258 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001259 return -1;
1260 }
1261 if (s < 4 * 1024 || s > 1024 * 1024) {
1262 log("packet_set_maxsize: bad size %d", s);
1263 return -1;
1264 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001265 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001266 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001267 max_packet_size = s;
1268 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001269}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001270
Damien Miller9f643902001-11-12 11:02:52 +11001271/* roundup current message to pad bytes */
1272void
1273packet_add_padding(u_char pad)
1274{
1275 extra_pad = pad;
1276}
1277
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001278/*
1279 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001280 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001281 * byte SSH_MSG_IGNORE
1282 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001283 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001284 * All implementations MUST understand (and ignore) this message at any
1285 * time (after receiving the protocol version). No implementation is
1286 * required to send them. This message can be used as an additional
1287 * protection measure against advanced traffic analysis techniques.
1288 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001289void
1290packet_send_ignore(int nbytes)
1291{
1292 u_int32_t rand = 0;
1293 int i;
1294
1295 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001296 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001297 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001298 if (i % 4 == 0)
1299 rand = arc4random();
1300 packet_put_char(rand & 0xff);
1301 rand >>= 8;
1302 }
1303}