blob: 065f8a52aa6dc807293fa654b020d2836cad06c2 [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 Millerd432ccf2002-01-22 23:14:44 +110040RCSID("$OpenBSD: packet.c,v 1.80 2001/12/28 13:57:33 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 Miller33b13562000-04-04 14:38:59 +100062
63#ifdef PACKET_DEBUG
64#define DBG(x) x
65#else
66#define DBG(x)
67#endif
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static int connection_in = -1;
76static int connection_out = -1;
77
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078/* Protocol flags for the remote side. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000079static u_int remote_protocol_flags = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81/* Encryption context for receiving data. This is only used for decryption. */
82static CipherContext receive_context;
Damien Miller95def091999-11-25 00:26:21 +110083
84/* Encryption context for sending data. This is only used for encryption. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085static CipherContext send_context;
86
87/* Buffer for raw input data from the socket. */
88static Buffer input;
89
90/* Buffer for raw output data going to the socket. */
91static Buffer output;
92
93/* Buffer for the partial outgoing packet being constructed. */
94static Buffer outgoing_packet;
95
96/* Buffer for the incoming packet currently being processed. */
97static Buffer incoming_packet;
98
99/* Scratch buffer for packet compression/decompression. */
100static Buffer compression_buffer;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000101static int compression_buffer_ready = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
103/* Flag indicating whether packet compression/decompression is enabled. */
104static int packet_compression = 0;
105
Damien Miller6162d121999-11-21 13:23:52 +1100106/* default maximum packet size */
107int max_packet_size = 32768;
108
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109/* Flag indicating whether this module has been initialized. */
110static int initialized = 0;
111
112/* Set to true if the connection is interactive. */
113static int interactive_mode = 0;
114
Damien Miller33b13562000-04-04 14:38:59 +1000115/* Session key information for Encryption and MAC */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000116Newkeys *newkeys[MODE_MAX];
Damien Miller33b13562000-04-04 14:38:59 +1000117
Damien Miller9f643902001-11-12 11:02:52 +1100118/* roundup current message to extra_pad bytes */
119static u_char extra_pad = 0;
120
Damien Miller5428f641999-11-25 11:54:57 +1100121/*
122 * Sets the descriptors used for communication. Disables encryption until
123 * packet_set_encryption_key is called.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125void
126packet_set_connection(int fd_in, int fd_out)
127{
Damien Miller874d77b2000-10-14 16:23:11 +1100128 Cipher *none = cipher_by_name("none");
129 if (none == NULL)
130 fatal("packet_set_connection: cannot load cipher 'none'");
Damien Miller95def091999-11-25 00:26:21 +1100131 connection_in = fd_in;
132 connection_out = fd_out;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000133 cipher_init(&send_context, none, (u_char *) "", 0, NULL, 0);
134 cipher_init(&receive_context, none, (u_char *) "", 0, NULL, 0);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000135 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100136 if (!initialized) {
137 initialized = 1;
138 buffer_init(&input);
139 buffer_init(&output);
140 buffer_init(&outgoing_packet);
141 buffer_init(&incoming_packet);
142 }
143 /* Kludge: arrange the close function to be called from fatal(). */
144 fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Miller34132e52000-01-14 15:45:46 +1100147/* Returns 1 if remote host is connected via socket, 0 if not. */
148
149int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000150packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100151{
152 struct sockaddr_storage from, to;
153 socklen_t fromlen, tolen;
154
155 /* filedescriptors in and out are the same, so it's a socket */
156 if (connection_in == connection_out)
157 return 1;
158 fromlen = sizeof(from);
159 memset(&from, 0, sizeof(from));
Damien Millerf052aaf2000-01-22 19:47:21 +1100160 if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100161 return 0;
162 tolen = sizeof(to);
163 memset(&to, 0, sizeof(to));
Damien Millerf052aaf2000-01-22 19:47:21 +1100164 if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100165 return 0;
166 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
167 return 0;
168 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
169 return 0;
170 return 1;
171}
172
173/* returns 1 if connection is via ipv4 */
174
175int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000176packet_connection_is_ipv4(void)
Damien Miller34132e52000-01-14 15:45:46 +1100177{
178 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100179 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100180
181 memset(&to, 0, sizeof(to));
182 if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0)
183 return 0;
184 if (to.ss_family != AF_INET)
185 return 0;
186 return 1;
187}
188
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189/* Sets the connection into non-blocking mode. */
190
191void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000192packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
Damien Miller95def091999-11-25 00:26:21 +1100194 /* Set the socket into non-blocking mode. */
195 if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0)
196 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Damien Miller95def091999-11-25 00:26:21 +1100198 if (connection_out != connection_in) {
199 if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0)
200 error("fcntl O_NONBLOCK: %.100s", strerror(errno));
201 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
203
204/* Returns the socket used for reading. */
205
206int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000207packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 return connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210}
211
212/* Returns the descriptor used for writing. */
213
214int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000215packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216{
Damien Miller95def091999-11-25 00:26:21 +1100217 return connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218}
219
220/* Closes the connection and clears and frees internal data structures. */
221
222void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000223packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Damien Miller95def091999-11-25 00:26:21 +1100225 if (!initialized)
226 return;
227 initialized = 0;
228 if (connection_in == connection_out) {
229 shutdown(connection_out, SHUT_RDWR);
230 close(connection_out);
231 } else {
232 close(connection_in);
233 close(connection_out);
234 }
235 buffer_free(&input);
236 buffer_free(&output);
237 buffer_free(&outgoing_packet);
238 buffer_free(&incoming_packet);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000239 if (compression_buffer_ready) {
Damien Miller95def091999-11-25 00:26:21 +1100240 buffer_free(&compression_buffer);
241 buffer_compress_uninit();
242 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
245/* Sets remote side protocol flags. */
246
247void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000248packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251}
252
253/* Returns the remote protocol flags set earlier by the above function. */
254
Ben Lindstrom46c16222000-12-22 01:43:59 +0000255u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000256packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257{
Damien Miller95def091999-11-25 00:26:21 +1100258 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259}
260
Damien Miller5428f641999-11-25 11:54:57 +1100261/*
262 * Starts packet compression from the next packet on in both directions.
263 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
264 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Ben Lindstrombba81212001-06-25 05:01:22 +0000266static void
267packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000268{
269 if (compression_buffer_ready == 1)
270 return;
271 compression_buffer_ready = 1;
272 buffer_init(&compression_buffer);
273}
274
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275void
276packet_start_compression(int level)
277{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000278 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100279 fatal("Compression already enabled.");
280 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000281 packet_init_compression();
282 buffer_compress_init_send(level);
283 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284}
285
Damien Miller5428f641999-11-25 11:54:57 +1100286/*
Damien Miller5428f641999-11-25 11:54:57 +1100287 * Causes any further packets to be encrypted using the given key. The same
288 * key is used for both sending and reception. However, both directions are
289 * encrypted independently of each other.
290 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000292packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100293 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Damien Miller874d77b2000-10-14 16:23:11 +1100295 Cipher *cipher = cipher_by_number(number);
296 if (cipher == NULL)
297 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000298 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100299 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
300 cipher_init(&receive_context, cipher, key, keylen, NULL, 0);
301 cipher_init(&send_context, cipher, key, keylen, NULL, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000304/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000306packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000308 u_char buf[9];
309 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Ben Lindstrom204e4882001-03-05 06:47:00 +0000311 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000312 len = compat20 ? 6 : 9;
313 memset(buf, 0, len - 1);
314 buf[len - 1] = type;
315 buffer_clear(&outgoing_packet);
316 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000317}
318
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000319/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320void
321packet_put_char(int value)
322{
Damien Miller95def091999-11-25 00:26:21 +1100323 char ch = value;
324 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000327packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Damien Miller95def091999-11-25 00:26:21 +1100329 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100332packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Damien Miller95def091999-11-25 00:26:21 +1100334 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335}
Damien Miller33b13562000-04-04 14:38:59 +1000336void
337packet_put_cstring(const char *str)
338{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000339 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000340}
Damien Miller33b13562000-04-04 14:38:59 +1000341void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100342packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000343{
344 buffer_append(&outgoing_packet, buf, len);
345}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346void
Damien Miller95def091999-11-25 00:26:21 +1100347packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348{
Damien Miller95def091999-11-25 00:26:21 +1100349 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
Damien Miller33b13562000-04-04 14:38:59 +1000351void
352packet_put_bignum2(BIGNUM * value)
353{
354 buffer_put_bignum2(&outgoing_packet, value);
355}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller5428f641999-11-25 11:54:57 +1100357/*
358 * Finalizes and sends the packet. If the encryption key has been set,
359 * encrypts the packet before sending.
360 */
Damien Miller95def091999-11-25 00:26:21 +1100361
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000363packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Miller95def091999-11-25 00:26:21 +1100365 char buf[8], *cp;
366 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000367 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100368 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369
Damien Miller5428f641999-11-25 11:54:57 +1100370 /*
371 * If using packet compression, compress the payload of the outgoing
372 * packet.
373 */
Damien Miller95def091999-11-25 00:26:21 +1100374 if (packet_compression) {
375 buffer_clear(&compression_buffer);
376 /* Skip padding. */
377 buffer_consume(&outgoing_packet, 8);
378 /* padding */
379 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
380 buffer_compress(&outgoing_packet, &compression_buffer);
381 buffer_clear(&outgoing_packet);
382 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100383 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100384 }
385 /* Compute packet length without padding (add checksum, remove padding). */
386 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Damien Millere247cc42000-05-07 12:03:14 +1000388 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100389 padding = 8 - len % 8;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000390 if (send_context.cipher->number != SSH_CIPHER_NONE) {
Damien Miller95def091999-11-25 00:26:21 +1100391 cp = buffer_ptr(&outgoing_packet);
392 for (i = 0; i < padding; i++) {
393 if (i % 4 == 0)
394 rand = arc4random();
395 cp[7 - i] = rand & 0xff;
396 rand >>= 8;
397 }
398 }
399 buffer_consume(&outgoing_packet, 8 - padding);
400
401 /* Add check bytes. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000402 checksum = ssh_crc32((u_char *) buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000403 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100404 PUT_32BIT(buf, checksum);
405 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
407#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100408 fprintf(stderr, "packet_send plain: ");
409 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410#endif
411
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Append to output. */
413 PUT_32BIT(buf, len);
414 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100415 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000416 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100417 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100418
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100420 fprintf(stderr, "encrypted: ");
421 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422#endif
423
Damien Miller95def091999-11-25 00:26:21 +1100424 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425
Damien Miller5428f641999-11-25 11:54:57 +1100426 /*
427 * Note that the packet is now only buffered in output. It won\'t be
428 * actually sent until packet_write_wait or packet_write_poll is
429 * called.
430 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431}
432
Ben Lindstrombba81212001-06-25 05:01:22 +0000433static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000434set_newkeys(int mode)
435{
436 Enc *enc;
437 Mac *mac;
438 Comp *comp;
439 CipherContext *cc;
440
441 debug("newkeys: mode %d", mode);
442
443 cc = (mode == MODE_OUT) ? &send_context : &receive_context;
444 if (newkeys[mode] != NULL) {
445 debug("newkeys: rekeying");
Ben Lindstrom238abf62001-04-04 17:52:53 +0000446 /* todo: free old keys, reset compression/cipher-ctxt; */
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000447 memset(cc, 0, sizeof(*cc));
448 enc = &newkeys[mode]->enc;
449 mac = &newkeys[mode]->mac;
450 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000451 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000452 xfree(enc->name);
453 xfree(enc->iv);
454 xfree(enc->key);
455 xfree(mac->name);
456 xfree(mac->key);
457 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000458 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000459 }
460 newkeys[mode] = kex_get_newkeys(mode);
461 if (newkeys[mode] == NULL)
462 fatal("newkeys: no keys for mode %d", mode);
463 enc = &newkeys[mode]->enc;
464 mac = &newkeys[mode]->mac;
465 comp = &newkeys[mode]->comp;
466 if (mac->md != NULL)
467 mac->enabled = 1;
468 DBG(debug("cipher_init_context: %d", mode));
469 cipher_init(cc, enc->cipher, enc->key, enc->cipher->key_len,
470 enc->iv, enc->cipher->block_size);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000471 memset(enc->iv, 0, enc->cipher->block_size);
472 memset(enc->key, 0, enc->cipher->key_len);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000473 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000474 packet_init_compression();
475 if (mode == MODE_OUT)
476 buffer_compress_init_send(6);
477 else
478 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000479 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000480 }
481}
482
Damien Miller5428f641999-11-25 11:54:57 +1100483/*
Damien Miller33b13562000-04-04 14:38:59 +1000484 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
485 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000487packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000488{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000489 static u_int32_t seqnr = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000490 u_char type, *ucp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100491 u_char padlen, pad;
Damien Miller33b13562000-04-04 14:38:59 +1000492 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000493 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100494 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000495 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000496 Enc *enc = NULL;
497 Mac *mac = NULL;
498 Comp *comp = NULL;
499 int block_size;
500
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000501 if (newkeys[MODE_OUT] != NULL) {
502 enc = &newkeys[MODE_OUT]->enc;
503 mac = &newkeys[MODE_OUT]->mac;
504 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000505 }
Damien Miller874d77b2000-10-14 16:23:11 +1100506 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000507
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000508 ucp = (u_char *) buffer_ptr(&outgoing_packet);
509 type = ucp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000510
511#ifdef PACKET_DEBUG
512 fprintf(stderr, "plain: ");
513 buffer_dump(&outgoing_packet);
514#endif
515
516 if (comp && comp->enabled) {
517 len = buffer_len(&outgoing_packet);
518 /* skip header, compress only payload */
519 buffer_consume(&outgoing_packet, 5);
520 buffer_clear(&compression_buffer);
521 buffer_compress(&outgoing_packet, &compression_buffer);
522 buffer_clear(&outgoing_packet);
523 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
524 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
525 buffer_len(&compression_buffer));
526 DBG(debug("compression: raw %d compressed %d", len,
527 buffer_len(&outgoing_packet)));
528 }
529
530 /* sizeof (packet_len + pad_len + payload) */
531 len = buffer_len(&outgoing_packet);
532
533 /*
534 * calc size of padding, alloc space, get random data,
535 * minimum padding is 4 bytes
536 */
537 padlen = block_size - (len % block_size);
538 if (padlen < 4)
539 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100540 if (extra_pad) {
541 /* will wrap if extra_pad+padlen > 255 */
542 extra_pad = roundup(extra_pad, block_size);
543 pad = extra_pad - ((len + padlen) % extra_pad);
544 debug("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
545 pad, len, padlen, extra_pad);
546 padlen += pad;
547 extra_pad = 0;
548 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100549 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100550 if (enc && enc->cipher->number != SSH_CIPHER_NONE) {
Damien Millere247cc42000-05-07 12:03:14 +1000551 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000552 for (i = 0; i < padlen; i++) {
553 if (i % 4 == 0)
554 rand = arc4random();
555 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000556 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000557 }
Damien Millere247cc42000-05-07 12:03:14 +1000558 } else {
559 /* clear padding */
560 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000561 }
562 /* packet_length includes payload, padding and padding length field */
563 packet_length = buffer_len(&outgoing_packet) - 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000564 ucp = (u_char *)buffer_ptr(&outgoing_packet);
565 PUT_32BIT(ucp, packet_length);
566 ucp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000567 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
568
569 /* compute MAC over seqnr and packet(length fields, payload, padding) */
570 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000571 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000572 (u_char *) buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000573 buffer_len(&outgoing_packet));
Damien Miller874d77b2000-10-14 16:23:11 +1100574 DBG(debug("done calc MAC out #%d", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000575 }
576 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100577 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000578 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000579 buffer_len(&outgoing_packet));
580 /* append unencrypted MAC */
581 if (mac && mac->enabled)
582 buffer_append(&output, (char *)macbuf, mac->mac_len);
583#ifdef PACKET_DEBUG
584 fprintf(stderr, "encrypted: ");
585 buffer_dump(&output);
586#endif
Damien Miller4af51302000-04-16 11:18:38 +1000587 /* increment sequence number for outgoing packets */
588 if (++seqnr == 0)
589 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000590 buffer_clear(&outgoing_packet);
591
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000592 if (type == SSH2_MSG_NEWKEYS)
593 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000594}
595
596void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000597packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000598{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000599 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000600 packet_send2();
601 else
602 packet_send1();
603 DBG(debug("packet_send done"));
604}
605
Damien Miller33b13562000-04-04 14:38:59 +1000606/*
Damien Miller5428f641999-11-25 11:54:57 +1100607 * Waits until a packet has been received, and returns its type. Note that
608 * no other data is processed until this returns, so this function should not
609 * be used during the interactive session.
610 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
612int
Damien Miller278f9072001-12-21 15:00:19 +1100613packet_read_seqnr(int *payload_len_ptr, u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Damien Miller95def091999-11-25 00:26:21 +1100615 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000616 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100617 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000618 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000620 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
621 sizeof(fd_mask));
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Since we are blocking, ensure that all written packets have been sent. */
624 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Damien Miller95def091999-11-25 00:26:21 +1100626 /* Stay in the loop until we have received a complete packet. */
627 for (;;) {
628 /* Try to read a packet from the buffer. */
Damien Miller278f9072001-12-21 15:00:19 +1100629 type = packet_read_poll_seqnr(payload_len_ptr, seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000630 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000631 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100632 || type == SSH_SMSG_FAILURE
633 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000634 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100635 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100636 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000637 if (type != SSH_MSG_NONE) {
638 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100639 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000640 }
Damien Miller5428f641999-11-25 11:54:57 +1100641 /*
642 * Otherwise, wait for some data to arrive, add it to the
643 * buffer, and try again.
644 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000645 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
646 sizeof(fd_mask));
647 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100648
Damien Miller95def091999-11-25 00:26:21 +1100649 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000650 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000651 (errno == EAGAIN || errno == EINTR))
652 ;
Damien Miller5428f641999-11-25 11:54:57 +1100653
Damien Miller95def091999-11-25 00:26:21 +1100654 /* Read data from the socket. */
655 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100656 if (len == 0) {
657 log("Connection closed by %.200s", get_remote_ipaddr());
658 fatal_cleanup();
659 }
Damien Miller95def091999-11-25 00:26:21 +1100660 if (len < 0)
661 fatal("Read from socket failed: %.100s", strerror(errno));
662 /* Append it to the buffer. */
663 packet_process_incoming(buf, len);
664 }
665 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666}
667
Damien Miller278f9072001-12-21 15:00:19 +1100668int
669packet_read(int *payload_len_ptr)
670{
671 return packet_read_seqnr(payload_len_ptr, NULL);
672}
673
Damien Miller5428f641999-11-25 11:54:57 +1100674/*
675 * Waits until a packet has been received, verifies that its type matches
676 * that given, and gives a fatal error and exits if there is a mismatch.
677 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
679void
680packet_read_expect(int *payload_len_ptr, int expected_type)
681{
Damien Miller95def091999-11-25 00:26:21 +1100682 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683
Damien Miller95def091999-11-25 00:26:21 +1100684 type = packet_read(payload_len_ptr);
685 if (type != expected_type)
686 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000687 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688}
689
690/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100691 * packet_process_incoming. If so, reads the packet; otherwise returns
692 * SSH_MSG_NONE. This does not wait for data from the connection.
693 *
694 * SSH_MSG_DISCONNECT is handled specially here. Also,
695 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
696 * to higher levels.
697 *
698 * The returned payload_len does include space consumed by:
699 * Packet length
700 * Padding
701 * Packet type
702 * Check bytes
703 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Ben Lindstrombba81212001-06-25 05:01:22 +0000705static int
Damien Miller33b13562000-04-04 14:38:59 +1000706packet_read_poll1(int *payload_len_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000708 u_int len, padded_len;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000709 u_char *ucp, type;
710 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000711 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller95def091999-11-25 00:26:21 +1100713 /* Check if input size is less than minimum packet size. */
714 if (buffer_len(&input) < 4 + 8)
715 return SSH_MSG_NONE;
716 /* Get length of incoming packet. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000717 ucp = (u_char *) buffer_ptr(&input);
Damien Miller95def091999-11-25 00:26:21 +1100718 len = GET_32BIT(ucp);
719 if (len < 1 + 2 + 2 || len > 256 * 1024)
720 packet_disconnect("Bad packet length %d.", len);
721 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Damien Miller95def091999-11-25 00:26:21 +1100723 /* Check if the packet has been entirely received. */
724 if (buffer_len(&input) < 4 + padded_len)
725 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Damien Miller95def091999-11-25 00:26:21 +1100729 /* Consume packet length. */
730 buffer_consume(&input, 4);
731
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000732 /*
733 * Cryptographic attack detector for ssh
734 * (C)1998 CORE-SDI, Buenos Aires Argentina
735 * Ariel Futoransky(futo@core-sdi.com)
736 */
737 if (receive_context.cipher->number != SSH_CIPHER_NONE &&
738 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
739 packet_disconnect("crc32 compensation attack: network attack detected");
740
741 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100742 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100743 cp = buffer_append_space(&incoming_packet, padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000744 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
745
Damien Miller95def091999-11-25 00:26:21 +1100746 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
748#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100749 fprintf(stderr, "read_poll plain: ");
750 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 /* Compute packet checksum. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000754 checksum = ssh_crc32((u_char *) buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000755 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756
Damien Miller95def091999-11-25 00:26:21 +1100757 /* Skip padding. */
758 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100759
Damien Miller95def091999-11-25 00:26:21 +1100760 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100761 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100762 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000763 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764
Ben Lindstrom46c16222000-12-22 01:43:59 +0000765 ucp = (u_char *) buffer_ptr(&incoming_packet) + len - 4;
Damien Miller95def091999-11-25 00:26:21 +1100766 stored_checksum = GET_32BIT(ucp);
767 if (checksum != stored_checksum)
768 packet_disconnect("Corrupted check bytes on input.");
769 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770
Damien Miller95def091999-11-25 00:26:21 +1100771 if (packet_compression) {
772 buffer_clear(&compression_buffer);
773 buffer_uncompress(&incoming_packet, &compression_buffer);
774 buffer_clear(&incoming_packet);
775 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000776 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100777 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000778 type = buffer_get_char(&incoming_packet);
Damien Miller95def091999-11-25 00:26:21 +1100779 *payload_len_ptr = buffer_len(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000780 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781}
Damien Miller95def091999-11-25 00:26:21 +1100782
Ben Lindstrombba81212001-06-25 05:01:22 +0000783static int
Damien Miller278f9072001-12-21 15:00:19 +1100784packet_read_poll2(int *payload_len_ptr, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000785{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000786 static u_int32_t seqnr = 0;
787 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000788 u_int padlen, need;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000789 u_char *macbuf, *ucp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000790 char *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000791 int maclen, block_size;
792 Enc *enc = NULL;
793 Mac *mac = NULL;
794 Comp *comp = NULL;
795
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000796 if (newkeys[MODE_IN] != NULL) {
797 enc = &newkeys[MODE_IN]->enc;
798 mac = &newkeys[MODE_IN]->mac;
799 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000800 }
801 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100802 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000803
804 if (packet_length == 0) {
805 /*
806 * check if input size is less than the cipher block size,
807 * decrypt first block and extract length of incoming packet
808 */
809 if (buffer_len(&input) < block_size)
810 return SSH_MSG_NONE;
811 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100812 cp = buffer_append_space(&incoming_packet, block_size);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000813 cipher_decrypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000814 block_size);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000815 ucp = (u_char *) buffer_ptr(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000816 packet_length = GET_32BIT(ucp);
817 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
818 buffer_dump(&incoming_packet);
819 packet_disconnect("Bad packet length %d.", packet_length);
820 }
821 DBG(debug("input: packet len %d", packet_length+4));
822 buffer_consume(&input, block_size);
823 }
824 /* we have a partial packet of block_size bytes */
825 need = 4 + packet_length - block_size;
826 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
827 need, maclen));
828 if (need % block_size != 0)
829 fatal("padding error: need %d block %d mod %d",
830 need, block_size, need % block_size);
831 /*
832 * check if the entire packet has been received and
833 * decrypt into incoming_packet
834 */
835 if (buffer_len(&input) < need + maclen)
836 return SSH_MSG_NONE;
837#ifdef PACKET_DEBUG
838 fprintf(stderr, "read_poll enc/full: ");
839 buffer_dump(&input);
840#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100841 cp = buffer_append_space(&incoming_packet, need);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000842 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000843 buffer_consume(&input, need);
844 /*
845 * compute MAC over seqnr and packet,
846 * increment sequence number for incoming packet
847 */
Damien Miller4af51302000-04-16 11:18:38 +1000848 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000849 macbuf = mac_compute(mac, seqnr,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000850 (u_char *) buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000851 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000852 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100853 packet_disconnect("Corrupted MAC on input.");
854 DBG(debug("MAC #%d ok", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000855 buffer_consume(&input, mac->mac_len);
856 }
Damien Miller278f9072001-12-21 15:00:19 +1100857 if (seqnr_p != NULL)
858 *seqnr_p = seqnr;
Damien Miller4af51302000-04-16 11:18:38 +1000859 if (++seqnr == 0)
860 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000861
862 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100863 cp = buffer_ptr(&incoming_packet);
864 cp += 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000865 padlen = (u_char) *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000866 DBG(debug("input: padlen %d", padlen));
867 if (padlen < 4)
868 packet_disconnect("Corrupted padlen %d on input.", padlen);
869
870 /* skip packet size + padlen, discard padding */
871 buffer_consume(&incoming_packet, 4 + 1);
872 buffer_consume_end(&incoming_packet, padlen);
873
874 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
875 if (comp && comp->enabled) {
876 buffer_clear(&compression_buffer);
877 buffer_uncompress(&incoming_packet, &compression_buffer);
878 buffer_clear(&incoming_packet);
879 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
880 buffer_len(&compression_buffer));
881 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
882 }
883 /*
884 * get packet type, implies consume.
885 * return length of payload (without type field)
886 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000887 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000888 if (type == SSH2_MSG_NEWKEYS)
889 set_newkeys(MODE_IN);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000890 *payload_len_ptr = buffer_len(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000891#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000892 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000893 buffer_dump(&incoming_packet);
894#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000895 /* reset for next packet */
896 packet_length = 0;
897 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000898}
899
900int
Damien Miller278f9072001-12-21 15:00:19 +1100901packet_read_poll_seqnr(int *payload_len_ptr, u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000902{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000903 int reason;
904 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +1000905 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +1000906
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000907 for (;;) {
908 if (compat20) {
Damien Miller278f9072001-12-21 15:00:19 +1100909 type = packet_read_poll2(payload_len_ptr, seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000910 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000911 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000912 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000913 case SSH2_MSG_IGNORE:
914 break;
915 case SSH2_MSG_DEBUG:
916 packet_get_char();
917 msg = packet_get_string(NULL);
918 debug("Remote: %.900s", msg);
919 xfree(msg);
920 msg = packet_get_string(NULL);
921 xfree(msg);
922 break;
923 case SSH2_MSG_DISCONNECT:
924 reason = packet_get_int();
925 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000926 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
927 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +1000928 xfree(msg);
929 fatal_cleanup();
930 break;
931 default:
932 return type;
933 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000934 }
Damien Miller33b13562000-04-04 14:38:59 +1000935 } else {
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000936 type = packet_read_poll1(payload_len_ptr);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000937 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000938 case SSH_MSG_IGNORE:
939 break;
940 case SSH_MSG_DEBUG:
941 msg = packet_get_string(NULL);
942 debug("Remote: %.900s", msg);
943 xfree(msg);
944 break;
945 case SSH_MSG_DISCONNECT:
946 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000947 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
948 msg);
Damien Miller33b13562000-04-04 14:38:59 +1000949 fatal_cleanup();
950 xfree(msg);
951 break;
952 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000953 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000954 DBG(debug("received packet type %d", type));
955 return type;
956 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000957 }
Damien Miller33b13562000-04-04 14:38:59 +1000958 }
959 }
960}
961
Damien Miller278f9072001-12-21 15:00:19 +1100962int
963packet_read_poll(int *payload_len_ptr)
964{
965 return packet_read_poll_seqnr(payload_len_ptr, NULL);
966}
967
Damien Miller5428f641999-11-25 11:54:57 +1100968/*
969 * Buffers the given amount of input characters. This is intended to be used
970 * together with packet_read_poll.
971 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
973void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000974packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975{
Damien Miller95def091999-11-25 00:26:21 +1100976 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977}
978
979/* Returns a character from the packet. */
980
Ben Lindstrom46c16222000-12-22 01:43:59 +0000981u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000982packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983{
Damien Miller95def091999-11-25 00:26:21 +1100984 char ch;
985 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000986 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987}
988
989/* Returns an integer from the packet data. */
990
Ben Lindstrom46c16222000-12-22 01:43:59 +0000991u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000992packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993{
Damien Miller95def091999-11-25 00:26:21 +1100994 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995}
996
Damien Miller5428f641999-11-25 11:54:57 +1100997/*
998 * Returns an arbitrary precision integer from the packet data. The integer
999 * must have been initialized before this call.
1000 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
1002void
Damien Millerd432ccf2002-01-22 23:14:44 +11001003packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004{
Damien Millerd432ccf2002-01-22 23:14:44 +11001005 (void)buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006}
1007
Damien Miller33b13562000-04-04 14:38:59 +10001008void
Damien Millerd432ccf2002-01-22 23:14:44 +11001009packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001010{
Damien Millerd432ccf2002-01-22 23:14:44 +11001011 (void)buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001012}
1013
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001014void *
Damien Miller33b13562000-04-04 14:38:59 +10001015packet_get_raw(int *length_ptr)
1016{
1017 int bytes = buffer_len(&incoming_packet);
1018 if (length_ptr != NULL)
1019 *length_ptr = bytes;
1020 return buffer_ptr(&incoming_packet);
1021}
1022
Damien Miller4af51302000-04-16 11:18:38 +10001023int
1024packet_remaining(void)
1025{
1026 return buffer_len(&incoming_packet);
1027}
1028
Damien Miller5428f641999-11-25 11:54:57 +11001029/*
1030 * Returns a string from the packet data. The string is allocated using
1031 * xmalloc; it is the responsibility of the calling program to free it when
1032 * no longer needed. The length_ptr argument may be NULL, or point to an
1033 * integer into which the length of the string is stored.
1034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001036void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001037packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038{
Damien Miller95def091999-11-25 00:26:21 +11001039 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040}
1041
Damien Miller5428f641999-11-25 11:54:57 +11001042/*
1043 * Sends a diagnostic message from the server to the client. This message
1044 * can be sent at any time (but not while constructing another message). The
1045 * message is printed immediately, but only if the client is being executed
1046 * in verbose mode. These messages are primarily intended to ease debugging
1047 * authentication problems. The length of the formatted message must not
1048 * exceed 1024 bytes. This will automatically call packet_write_wait.
1049 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050
1051void
Damien Miller95def091999-11-25 00:26:21 +11001052packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053{
Damien Miller95def091999-11-25 00:26:21 +11001054 char buf[1024];
1055 va_list args;
1056
Ben Lindstroma14ee472000-12-07 01:24:58 +00001057 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1058 return;
1059
Damien Miller95def091999-11-25 00:26:21 +11001060 va_start(args, fmt);
1061 vsnprintf(buf, sizeof(buf), fmt, args);
1062 va_end(args);
1063
Damien Miller7c8af4f2000-05-01 08:24:07 +10001064 if (compat20) {
1065 packet_start(SSH2_MSG_DEBUG);
1066 packet_put_char(0); /* bool: always display */
1067 packet_put_cstring(buf);
1068 packet_put_cstring("");
1069 } else {
1070 packet_start(SSH_MSG_DEBUG);
1071 packet_put_cstring(buf);
1072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 packet_send();
1074 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075}
1076
Damien Miller5428f641999-11-25 11:54:57 +11001077/*
1078 * Logs the error plus constructs and sends a disconnect packet, closes the
1079 * connection, and exits. This function never returns. The error message
1080 * should not contain a newline. The length of the formatted message must
1081 * not exceed 1024 bytes.
1082 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083
1084void
Damien Miller95def091999-11-25 00:26:21 +11001085packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086{
Damien Miller95def091999-11-25 00:26:21 +11001087 char buf[1024];
1088 va_list args;
1089 static int disconnecting = 0;
1090 if (disconnecting) /* Guard against recursive invocations. */
1091 fatal("packet_disconnect called recursively.");
1092 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093
Damien Miller5428f641999-11-25 11:54:57 +11001094 /*
1095 * Format the message. Note that the caller must make sure the
1096 * message is of limited size.
1097 */
Damien Miller95def091999-11-25 00:26:21 +11001098 va_start(args, fmt);
1099 vsnprintf(buf, sizeof(buf), fmt, args);
1100 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller95def091999-11-25 00:26:21 +11001102 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001103 if (compat20) {
1104 packet_start(SSH2_MSG_DISCONNECT);
1105 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1106 packet_put_cstring(buf);
1107 packet_put_cstring("");
1108 } else {
1109 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001110 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001111 }
Damien Miller95def091999-11-25 00:26:21 +11001112 packet_send();
1113 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller95def091999-11-25 00:26:21 +11001115 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001116 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117
Damien Miller95def091999-11-25 00:26:21 +11001118 /* Close the connection. */
1119 packet_close();
1120
1121 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001122 log("Disconnecting: %.100s", buf);
1123 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124}
1125
Damien Miller5428f641999-11-25 11:54:57 +11001126/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
1128void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001129packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130{
Damien Miller95def091999-11-25 00:26:21 +11001131 int len = buffer_len(&output);
1132 if (len > 0) {
1133 len = write(connection_out, buffer_ptr(&output), len);
1134 if (len <= 0) {
1135 if (errno == EAGAIN)
1136 return;
1137 else
1138 fatal("Write failed: %.100s", strerror(errno));
1139 }
1140 buffer_consume(&output, len);
1141 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142}
1143
Damien Miller5428f641999-11-25 11:54:57 +11001144/*
1145 * Calls packet_write_poll repeatedly until all pending output data has been
1146 * written.
1147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148
1149void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001150packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001152 fd_set *setp;
1153
1154 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1155 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001156 packet_write_poll();
1157 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001158 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1159 sizeof(fd_mask));
1160 FD_SET(connection_out, setp);
1161 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001162 (errno == EAGAIN || errno == EINTR))
1163 ;
Damien Miller95def091999-11-25 00:26:21 +11001164 packet_write_poll();
1165 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001166 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167}
1168
1169/* Returns true if there is buffered data to write to the connection. */
1170
1171int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001172packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173{
Damien Miller95def091999-11-25 00:26:21 +11001174 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175}
1176
1177/* Returns true if there is not too much data to write to the connection. */
1178
1179int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001180packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181{
Damien Miller95def091999-11-25 00:26:21 +11001182 if (interactive_mode)
1183 return buffer_len(&output) < 16384;
1184 else
1185 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186}
1187
1188/* Informs that the current session is interactive. Sets IP flags for that. */
1189
1190void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001191packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001193 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001194#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001195 int lowdelay = IPTOS_LOWDELAY;
1196 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001197#endif
Damien Miller95def091999-11-25 00:26:21 +11001198 int on = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001200 if (called)
1201 return;
1202 called = 1;
1203
Damien Miller95def091999-11-25 00:26:21 +11001204 /* Record that we are in interactive mode. */
1205 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
Damien Miller34132e52000-01-14 15:45:46 +11001207 /* Only set socket options if using a socket. */
1208 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001209 return;
Damien Miller34132e52000-01-14 15:45:46 +11001210 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001211 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001212 */
Damien Miller95def091999-11-25 00:26:21 +11001213 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001214 /*
1215 * Set IP options for an interactive connection. Use
1216 * IPTOS_LOWDELAY and TCP_NODELAY.
1217 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001218#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001219 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001220 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001221 (void *) &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001222 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001223 strerror(errno));
1224 }
Damien Miller615f9392000-05-17 22:53:33 +10001225#endif
Damien Miller95def091999-11-25 00:26:21 +11001226 if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on,
Damien Miller34132e52000-01-14 15:45:46 +11001227 sizeof(on)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001228 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001229 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001230 /*
1231 * Set IP options for a non-interactive connection. Use
1232 * IPTOS_THROUGHPUT.
1233 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001234#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001235 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001236 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001237 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001238#endif
Damien Miller95def091999-11-25 00:26:21 +11001239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240}
1241
1242/* Returns true if the current connection is interactive. */
1243
1244int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001245packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Damien Miller95def091999-11-25 00:26:21 +11001247 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248}
Damien Miller6162d121999-11-21 13:23:52 +11001249
1250int
1251packet_set_maxsize(int s)
1252{
Damien Miller95def091999-11-25 00:26:21 +11001253 static int called = 0;
1254 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001255 log("packet_set_maxsize: called twice: old %d new %d",
1256 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001257 return -1;
1258 }
1259 if (s < 4 * 1024 || s > 1024 * 1024) {
1260 log("packet_set_maxsize: bad size %d", s);
1261 return -1;
1262 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001263 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001264 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001265 max_packet_size = s;
1266 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001267}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001268
Damien Miller9f643902001-11-12 11:02:52 +11001269/* roundup current message to pad bytes */
1270void
1271packet_add_padding(u_char pad)
1272{
1273 extra_pad = pad;
1274}
1275
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001276/*
1277 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001278 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001279 * byte SSH_MSG_IGNORE
1280 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001281 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001282 * All implementations MUST understand (and ignore) this message at any
1283 * time (after receiving the protocol version). No implementation is
1284 * required to send them. This message can be used as an additional
1285 * protection measure against advanced traffic analysis techniques.
1286 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001287void
1288packet_send_ignore(int nbytes)
1289{
1290 u_int32_t rand = 0;
1291 int i;
1292
1293 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001294 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001295 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001296 if (i % 4 == 0)
1297 rand = arc4random();
1298 packet_put_char(rand & 0xff);
1299 rand >>= 8;
1300 }
1301}