blob: e334c4b6933a2d1caf6d97522d59adb1cba7d21e [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 Miller4d007762002-02-05 11:52:54 +110040RCSID("$OpenBSD: packet.c,v 1.87 2002/01/24 21:13:23 stevesk 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 Miller4a8ed542002-01-22 23:33:31 +1100134 cipher_init(&send_context, none, "", 0, NULL, 0);
135 cipher_init(&receive_context, none, "", 0, NULL, 0);
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 Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
246/* Sets remote side protocol flags. */
247
248void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000249packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
254/* Returns the remote protocol flags set earlier by the above function. */
255
Ben Lindstrom46c16222000-12-22 01:43:59 +0000256u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000257packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258{
Damien Miller95def091999-11-25 00:26:21 +1100259 return remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260}
261
Damien Miller5428f641999-11-25 11:54:57 +1100262/*
263 * Starts packet compression from the next packet on in both directions.
264 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
265 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Ben Lindstrombba81212001-06-25 05:01:22 +0000267static void
268packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000269{
270 if (compression_buffer_ready == 1)
271 return;
272 compression_buffer_ready = 1;
273 buffer_init(&compression_buffer);
274}
275
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276void
277packet_start_compression(int level)
278{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000279 if (packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100280 fatal("Compression already enabled.");
281 packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000282 packet_init_compression();
283 buffer_compress_init_send(level);
284 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285}
286
Damien Miller5428f641999-11-25 11:54:57 +1100287/*
Damien Miller5428f641999-11-25 11:54:57 +1100288 * Causes any further packets to be encrypted using the given key. The same
289 * key is used for both sending and reception. However, both directions are
290 * encrypted independently of each other.
291 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000293packet_set_encryption_key(const u_char *key, u_int keylen,
Damien Miller874d77b2000-10-14 16:23:11 +1100294 int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Damien Miller874d77b2000-10-14 16:23:11 +1100296 Cipher *cipher = cipher_by_number(number);
297 if (cipher == NULL)
298 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000299 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100300 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
301 cipher_init(&receive_context, cipher, key, keylen, NULL, 0);
302 cipher_init(&send_context, cipher, key, keylen, NULL, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000305/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000307packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000309 u_char buf[9];
310 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Ben Lindstrom204e4882001-03-05 06:47:00 +0000312 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000313 len = compat20 ? 6 : 9;
314 memset(buf, 0, len - 1);
315 buf[len - 1] = type;
316 buffer_clear(&outgoing_packet);
317 buffer_append(&outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000318}
319
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000320/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321void
322packet_put_char(int value)
323{
Damien Miller95def091999-11-25 00:26:21 +1100324 char ch = value;
325 buffer_append(&outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000328packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329{
Damien Miller95def091999-11-25 00:26:21 +1100330 buffer_put_int(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100333packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Miller95def091999-11-25 00:26:21 +1100335 buffer_put_string(&outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
Damien Miller33b13562000-04-04 14:38:59 +1000337void
338packet_put_cstring(const char *str)
339{
Ben Lindstrom664408d2001-06-09 01:42:01 +0000340 buffer_put_cstring(&outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000341}
Damien Miller33b13562000-04-04 14:38:59 +1000342void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100343packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000344{
345 buffer_append(&outgoing_packet, buf, len);
346}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347void
Damien Miller95def091999-11-25 00:26:21 +1100348packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 buffer_put_bignum(&outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351}
Damien Miller33b13562000-04-04 14:38:59 +1000352void
353packet_put_bignum2(BIGNUM * value)
354{
355 buffer_put_bignum2(&outgoing_packet, value);
356}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Miller5428f641999-11-25 11:54:57 +1100358/*
359 * Finalizes and sends the packet. If the encryption key has been set,
360 * encrypts the packet before sending.
361 */
Damien Miller95def091999-11-25 00:26:21 +1100362
Ben Lindstrombba81212001-06-25 05:01:22 +0000363static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000364packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365{
Damien Miller95def091999-11-25 00:26:21 +1100366 char buf[8], *cp;
367 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000368 u_int checksum;
Damien Miller95def091999-11-25 00:26:21 +1100369 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Damien Miller5428f641999-11-25 11:54:57 +1100371 /*
372 * If using packet compression, compress the payload of the outgoing
373 * packet.
374 */
Damien Miller95def091999-11-25 00:26:21 +1100375 if (packet_compression) {
376 buffer_clear(&compression_buffer);
377 /* Skip padding. */
378 buffer_consume(&outgoing_packet, 8);
379 /* padding */
380 buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8);
381 buffer_compress(&outgoing_packet, &compression_buffer);
382 buffer_clear(&outgoing_packet);
383 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100384 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100385 }
386 /* Compute packet length without padding (add checksum, remove padding). */
387 len = buffer_len(&outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Millere247cc42000-05-07 12:03:14 +1000389 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100390 padding = 8 - len % 8;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000391 if (send_context.cipher->number != SSH_CIPHER_NONE) {
Damien Miller95def091999-11-25 00:26:21 +1100392 cp = buffer_ptr(&outgoing_packet);
393 for (i = 0; i < padding; i++) {
394 if (i % 4 == 0)
395 rand = arc4random();
396 cp[7 - i] = rand & 0xff;
397 rand >>= 8;
398 }
399 }
400 buffer_consume(&outgoing_packet, 8 - padding);
401
402 /* Add check bytes. */
Damien Miller708d21c2002-01-22 23:18:15 +1100403 checksum = ssh_crc32(buffer_ptr(&outgoing_packet),
Damien Millerad833b32000-08-23 10:46:23 +1000404 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100405 PUT_32BIT(buf, checksum);
406 buffer_append(&outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
408#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100409 fprintf(stderr, "packet_send plain: ");
410 buffer_dump(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411#endif
412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* Append to output. */
414 PUT_32BIT(buf, len);
415 buffer_append(&output, buf, 4);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100416 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000417 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100418 buffer_len(&outgoing_packet));
Damien Miller95def091999-11-25 00:26:21 +1100419
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100421 fprintf(stderr, "encrypted: ");
422 buffer_dump(&output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423#endif
424
Damien Miller95def091999-11-25 00:26:21 +1100425 buffer_clear(&outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Damien Miller5428f641999-11-25 11:54:57 +1100427 /*
428 * Note that the packet is now only buffered in output. It won\'t be
429 * actually sent until packet_write_wait or packet_write_poll is
430 * called.
431 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Ben Lindstrombba81212001-06-25 05:01:22 +0000434static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000435set_newkeys(int mode)
436{
437 Enc *enc;
438 Mac *mac;
439 Comp *comp;
440 CipherContext *cc;
441
442 debug("newkeys: mode %d", mode);
443
444 cc = (mode == MODE_OUT) ? &send_context : &receive_context;
445 if (newkeys[mode] != NULL) {
446 debug("newkeys: rekeying");
Ben Lindstrom238abf62001-04-04 17:52:53 +0000447 /* todo: free old keys, reset compression/cipher-ctxt; */
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000448 memset(cc, 0, sizeof(*cc));
449 enc = &newkeys[mode]->enc;
450 mac = &newkeys[mode]->mac;
451 comp = &newkeys[mode]->comp;
Ben Lindstroma3700052001-04-05 23:26:32 +0000452 memset(mac->key, 0, mac->key_len);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000453 xfree(enc->name);
454 xfree(enc->iv);
455 xfree(enc->key);
456 xfree(mac->name);
457 xfree(mac->key);
458 xfree(comp->name);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000459 xfree(newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000460 }
461 newkeys[mode] = kex_get_newkeys(mode);
462 if (newkeys[mode] == NULL)
463 fatal("newkeys: no keys for mode %d", mode);
464 enc = &newkeys[mode]->enc;
465 mac = &newkeys[mode]->mac;
466 comp = &newkeys[mode]->comp;
467 if (mac->md != NULL)
468 mac->enabled = 1;
469 DBG(debug("cipher_init_context: %d", mode));
470 cipher_init(cc, enc->cipher, enc->key, enc->cipher->key_len,
471 enc->iv, enc->cipher->block_size);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000472 memset(enc->iv, 0, enc->cipher->block_size);
473 memset(enc->key, 0, enc->cipher->key_len);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000474 if (comp->type != 0 && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000475 packet_init_compression();
476 if (mode == MODE_OUT)
477 buffer_compress_init_send(6);
478 else
479 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000480 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000481 }
482}
483
Damien Miller5428f641999-11-25 11:54:57 +1100484/*
Damien Miller33b13562000-04-04 14:38:59 +1000485 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
486 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000487static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000488packet_send2(void)
Damien Miller33b13562000-04-04 14:38:59 +1000489{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000490 static u_int32_t seqnr = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000491 u_char type, *ucp, *macbuf = NULL;
Damien Miller9f643902001-11-12 11:02:52 +1100492 u_char padlen, pad;
Damien Miller33b13562000-04-04 14:38:59 +1000493 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000494 u_int packet_length = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100495 u_int i, len;
Damien Miller33b13562000-04-04 14:38:59 +1000496 u_int32_t rand = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000497 Enc *enc = NULL;
498 Mac *mac = NULL;
499 Comp *comp = NULL;
500 int block_size;
501
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000502 if (newkeys[MODE_OUT] != NULL) {
503 enc = &newkeys[MODE_OUT]->enc;
504 mac = &newkeys[MODE_OUT]->mac;
505 comp = &newkeys[MODE_OUT]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000506 }
Damien Miller874d77b2000-10-14 16:23:11 +1100507 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000508
Damien Miller708d21c2002-01-22 23:18:15 +1100509 ucp = buffer_ptr(&outgoing_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000510 type = ucp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000511
512#ifdef PACKET_DEBUG
513 fprintf(stderr, "plain: ");
514 buffer_dump(&outgoing_packet);
515#endif
516
517 if (comp && comp->enabled) {
518 len = buffer_len(&outgoing_packet);
519 /* skip header, compress only payload */
520 buffer_consume(&outgoing_packet, 5);
521 buffer_clear(&compression_buffer);
522 buffer_compress(&outgoing_packet, &compression_buffer);
523 buffer_clear(&outgoing_packet);
524 buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);
525 buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),
526 buffer_len(&compression_buffer));
527 DBG(debug("compression: raw %d compressed %d", len,
528 buffer_len(&outgoing_packet)));
529 }
530
531 /* sizeof (packet_len + pad_len + payload) */
532 len = buffer_len(&outgoing_packet);
533
534 /*
535 * calc size of padding, alloc space, get random data,
536 * minimum padding is 4 bytes
537 */
538 padlen = block_size - (len % block_size);
539 if (padlen < 4)
540 padlen += block_size;
Damien Miller9f643902001-11-12 11:02:52 +1100541 if (extra_pad) {
542 /* will wrap if extra_pad+padlen > 255 */
543 extra_pad = roundup(extra_pad, block_size);
544 pad = extra_pad - ((len + padlen) % extra_pad);
545 debug("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
546 pad, len, padlen, extra_pad);
547 padlen += pad;
548 extra_pad = 0;
549 }
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100550 cp = buffer_append_space(&outgoing_packet, padlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100551 if (enc && enc->cipher->number != SSH_CIPHER_NONE) {
Damien Millere247cc42000-05-07 12:03:14 +1000552 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000553 for (i = 0; i < padlen; i++) {
554 if (i % 4 == 0)
555 rand = arc4random();
556 cp[i] = rand & 0xff;
Ben Lindstrom6a5cde02001-03-05 06:07:00 +0000557 rand >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000558 }
Damien Millere247cc42000-05-07 12:03:14 +1000559 } else {
560 /* clear padding */
561 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000562 }
563 /* packet_length includes payload, padding and padding length field */
564 packet_length = buffer_len(&outgoing_packet) - 4;
Damien Miller708d21c2002-01-22 23:18:15 +1100565 ucp = buffer_ptr(&outgoing_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000566 PUT_32BIT(ucp, packet_length);
567 ucp[4] = padlen;
Damien Miller33b13562000-04-04 14:38:59 +1000568 DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));
569
570 /* compute MAC over seqnr and packet(length fields, payload, padding) */
571 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000572 macbuf = mac_compute(mac, seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100573 buffer_ptr(&outgoing_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000574 buffer_len(&outgoing_packet));
Damien Miller874d77b2000-10-14 16:23:11 +1100575 DBG(debug("done calc MAC out #%d", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000576 }
577 /* encrypt packet and append to output buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100578 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000579 cipher_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000580 buffer_len(&outgoing_packet));
581 /* append unencrypted MAC */
582 if (mac && mac->enabled)
583 buffer_append(&output, (char *)macbuf, mac->mac_len);
584#ifdef PACKET_DEBUG
585 fprintf(stderr, "encrypted: ");
586 buffer_dump(&output);
587#endif
Damien Miller4af51302000-04-16 11:18:38 +1000588 /* increment sequence number for outgoing packets */
589 if (++seqnr == 0)
590 log("outgoing seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000591 buffer_clear(&outgoing_packet);
592
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000593 if (type == SSH2_MSG_NEWKEYS)
594 set_newkeys(MODE_OUT);
Damien Miller33b13562000-04-04 14:38:59 +1000595}
596
597void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000598packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +1000599{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000600 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +1000601 packet_send2();
602 else
603 packet_send1();
604 DBG(debug("packet_send done"));
605}
606
Damien Miller33b13562000-04-04 14:38:59 +1000607/*
Damien Miller5428f641999-11-25 11:54:57 +1100608 * Waits until a packet has been received, and returns its type. Note that
609 * no other data is processed until this returns, so this function should not
610 * be used during the interactive session.
611 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
613int
Damien Millerdff50992002-01-22 23:16:32 +1100614packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615{
Damien Miller95def091999-11-25 00:26:21 +1100616 int type, len;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000617 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +1100618 char buf[8192];
Damien Miller33b13562000-04-04 14:38:59 +1000619 DBG(debug("packet_read()"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000621 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) *
622 sizeof(fd_mask));
623
Damien Miller95def091999-11-25 00:26:21 +1100624 /* Since we are blocking, ensure that all written packets have been sent. */
625 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller95def091999-11-25 00:26:21 +1100627 /* Stay in the loop until we have received a complete packet. */
628 for (;;) {
629 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +1100630 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000631 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +1000632 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +1100633 || type == SSH_SMSG_FAILURE
634 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +1000635 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +1100636 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100637 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000638 if (type != SSH_MSG_NONE) {
639 xfree(setp);
Damien Miller95def091999-11-25 00:26:21 +1100640 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000641 }
Damien Miller5428f641999-11-25 11:54:57 +1100642 /*
643 * Otherwise, wait for some data to arrive, add it to the
644 * buffer, and try again.
645 */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000646 memset(setp, 0, howmany(connection_in + 1, NFDBITS) *
647 sizeof(fd_mask));
648 FD_SET(connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +1100649
Damien Miller95def091999-11-25 00:26:21 +1100650 /* Wait for some data to arrive. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +0000651 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000652 (errno == EAGAIN || errno == EINTR))
653 ;
Damien Miller5428f641999-11-25 11:54:57 +1100654
Damien Miller95def091999-11-25 00:26:21 +1100655 /* Read data from the socket. */
656 len = read(connection_in, buf, sizeof(buf));
Damien Miller5e7c10e1999-12-16 13:18:04 +1100657 if (len == 0) {
658 log("Connection closed by %.200s", get_remote_ipaddr());
659 fatal_cleanup();
660 }
Damien Miller95def091999-11-25 00:26:21 +1100661 if (len < 0)
662 fatal("Read from socket failed: %.100s", strerror(errno));
663 /* Append it to the buffer. */
664 packet_process_incoming(buf, len);
665 }
666 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667}
668
Damien Miller278f9072001-12-21 15:00:19 +1100669int
Damien Millerdff50992002-01-22 23:16:32 +1100670packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +1100671{
Damien Millerdff50992002-01-22 23:16:32 +1100672 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100673}
674
Damien Miller5428f641999-11-25 11:54:57 +1100675/*
676 * Waits until a packet has been received, verifies that its type matches
677 * that given, and gives a fatal error and exits if there is a mismatch.
678 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
680void
Damien Millerdff50992002-01-22 23:16:32 +1100681packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682{
Damien Miller95def091999-11-25 00:26:21 +1100683 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Millerdff50992002-01-22 23:16:32 +1100685 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100686 if (type != expected_type)
687 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +1000688 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689}
690
691/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +1100692 * packet_process_incoming. If so, reads the packet; otherwise returns
693 * SSH_MSG_NONE. This does not wait for data from the connection.
694 *
695 * SSH_MSG_DISCONNECT is handled specially here. Also,
696 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
697 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +1100698 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699
Ben Lindstrombba81212001-06-25 05:01:22 +0000700static int
Damien Millerdff50992002-01-22 23:16:32 +1100701packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000703 u_int len, padded_len;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000704 u_char *ucp, type;
705 char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000706 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Damien Miller95def091999-11-25 00:26:21 +1100708 /* Check if input size is less than minimum packet size. */
709 if (buffer_len(&input) < 4 + 8)
710 return SSH_MSG_NONE;
711 /* Get length of incoming packet. */
Damien Miller708d21c2002-01-22 23:18:15 +1100712 ucp = buffer_ptr(&input);
Damien Miller95def091999-11-25 00:26:21 +1100713 len = GET_32BIT(ucp);
714 if (len < 1 + 2 + 2 || len > 256 * 1024)
715 packet_disconnect("Bad packet length %d.", len);
716 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717
Damien Miller95def091999-11-25 00:26:21 +1100718 /* Check if the packet has been entirely received. */
719 if (buffer_len(&input) < 4 + padded_len)
720 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller95def091999-11-25 00:26:21 +1100722 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723
Damien Miller95def091999-11-25 00:26:21 +1100724 /* Consume packet length. */
725 buffer_consume(&input, 4);
726
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000727 /*
728 * Cryptographic attack detector for ssh
729 * (C)1998 CORE-SDI, Buenos Aires Argentina
730 * Ariel Futoransky(futo@core-sdi.com)
731 */
732 if (receive_context.cipher->number != SSH_CIPHER_NONE &&
733 detect_attack(buffer_ptr(&input), padded_len, NULL) == DEATTACK_DETECTED)
734 packet_disconnect("crc32 compensation attack: network attack detected");
735
736 /* Decrypt data to incoming_packet. */
Damien Miller95def091999-11-25 00:26:21 +1100737 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100738 cp = buffer_append_space(&incoming_packet, padded_len);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000739 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len);
740
Damien Miller95def091999-11-25 00:26:21 +1100741 buffer_consume(&input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742
743#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100744 fprintf(stderr, "read_poll plain: ");
745 buffer_dump(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Damien Miller95def091999-11-25 00:26:21 +1100748 /* Compute packet checksum. */
Damien Miller708d21c2002-01-22 23:18:15 +1100749 checksum = ssh_crc32(buffer_ptr(&incoming_packet),
Damien Miller33b13562000-04-04 14:38:59 +1000750 buffer_len(&incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Miller95def091999-11-25 00:26:21 +1100752 /* Skip padding. */
753 buffer_consume(&incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +1100754
Damien Miller95def091999-11-25 00:26:21 +1100755 /* Test check bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100756 if (len != buffer_len(&incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +1100757 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Damien Miller33b13562000-04-04 14:38:59 +1000758 len, buffer_len(&incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller708d21c2002-01-22 23:18:15 +1100760 ucp = (u_char *)buffer_ptr(&incoming_packet) + len - 4;
Damien Miller95def091999-11-25 00:26:21 +1100761 stored_checksum = GET_32BIT(ucp);
762 if (checksum != stored_checksum)
763 packet_disconnect("Corrupted check bytes on input.");
764 buffer_consume_end(&incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765
Damien Miller95def091999-11-25 00:26:21 +1100766 if (packet_compression) {
767 buffer_clear(&compression_buffer);
768 buffer_uncompress(&incoming_packet, &compression_buffer);
769 buffer_clear(&incoming_packet);
770 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
Damien Miller33b13562000-04-04 14:38:59 +1000771 buffer_len(&compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100772 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000773 type = buffer_get_char(&incoming_packet);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000774 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775}
Damien Miller95def091999-11-25 00:26:21 +1100776
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static int
Damien Millerdff50992002-01-22 23:16:32 +1100778packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000779{
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000780 static u_int32_t seqnr = 0;
781 static u_int packet_length = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000782 u_int padlen, need;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000783 u_char *macbuf, *ucp, type;
Damien Miller33b13562000-04-04 14:38:59 +1000784 char *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000785 int maclen, block_size;
786 Enc *enc = NULL;
787 Mac *mac = NULL;
788 Comp *comp = NULL;
789
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000790 if (newkeys[MODE_IN] != NULL) {
791 enc = &newkeys[MODE_IN]->enc;
792 mac = &newkeys[MODE_IN]->mac;
793 comp = &newkeys[MODE_IN]->comp;
Damien Miller33b13562000-04-04 14:38:59 +1000794 }
795 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100796 block_size = enc ? enc->cipher->block_size : 8;
Damien Miller33b13562000-04-04 14:38:59 +1000797
798 if (packet_length == 0) {
799 /*
800 * check if input size is less than the cipher block size,
801 * decrypt first block and extract length of incoming packet
802 */
803 if (buffer_len(&input) < block_size)
804 return SSH_MSG_NONE;
805 buffer_clear(&incoming_packet);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100806 cp = buffer_append_space(&incoming_packet, block_size);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000807 cipher_decrypt(&receive_context, cp, buffer_ptr(&input),
Damien Miller33b13562000-04-04 14:38:59 +1000808 block_size);
Damien Miller708d21c2002-01-22 23:18:15 +1100809 ucp = buffer_ptr(&incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000810 packet_length = GET_32BIT(ucp);
811 if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
812 buffer_dump(&incoming_packet);
813 packet_disconnect("Bad packet length %d.", packet_length);
814 }
815 DBG(debug("input: packet len %d", packet_length+4));
816 buffer_consume(&input, block_size);
817 }
818 /* we have a partial packet of block_size bytes */
819 need = 4 + packet_length - block_size;
820 DBG(debug("partial packet %d, need %d, maclen %d", block_size,
821 need, maclen));
822 if (need % block_size != 0)
823 fatal("padding error: need %d block %d mod %d",
824 need, block_size, need % block_size);
825 /*
826 * check if the entire packet has been received and
827 * decrypt into incoming_packet
828 */
829 if (buffer_len(&input) < need + maclen)
830 return SSH_MSG_NONE;
831#ifdef PACKET_DEBUG
832 fprintf(stderr, "read_poll enc/full: ");
833 buffer_dump(&input);
834#endif
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100835 cp = buffer_append_space(&incoming_packet, need);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000836 cipher_decrypt(&receive_context, cp, buffer_ptr(&input), need);
Damien Miller33b13562000-04-04 14:38:59 +1000837 buffer_consume(&input, need);
838 /*
839 * compute MAC over seqnr and packet,
840 * increment sequence number for incoming packet
841 */
Damien Miller4af51302000-04-16 11:18:38 +1000842 if (mac && mac->enabled) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000843 macbuf = mac_compute(mac, seqnr,
Damien Miller708d21c2002-01-22 23:18:15 +1100844 buffer_ptr(&incoming_packet),
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000845 buffer_len(&incoming_packet));
Damien Miller33b13562000-04-04 14:38:59 +1000846 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100847 packet_disconnect("Corrupted MAC on input.");
848 DBG(debug("MAC #%d ok", seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000849 buffer_consume(&input, mac->mac_len);
850 }
Damien Miller278f9072001-12-21 15:00:19 +1100851 if (seqnr_p != NULL)
852 *seqnr_p = seqnr;
Damien Miller4af51302000-04-16 11:18:38 +1000853 if (++seqnr == 0)
854 log("incoming seqnr wraps around");
Damien Miller33b13562000-04-04 14:38:59 +1000855
856 /* get padlen */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100857 cp = buffer_ptr(&incoming_packet);
858 cp += 4;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000859 padlen = (u_char) *cp;
Damien Miller33b13562000-04-04 14:38:59 +1000860 DBG(debug("input: padlen %d", padlen));
861 if (padlen < 4)
862 packet_disconnect("Corrupted padlen %d on input.", padlen);
863
864 /* skip packet size + padlen, discard padding */
865 buffer_consume(&incoming_packet, 4 + 1);
866 buffer_consume_end(&incoming_packet, padlen);
867
868 DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet)));
869 if (comp && comp->enabled) {
870 buffer_clear(&compression_buffer);
871 buffer_uncompress(&incoming_packet, &compression_buffer);
872 buffer_clear(&incoming_packet);
873 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
874 buffer_len(&compression_buffer));
875 DBG(debug("input: len after de-compress %d", buffer_len(&incoming_packet)));
876 }
877 /*
878 * get packet type, implies consume.
879 * return length of payload (without type field)
880 */
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000881 type = buffer_get_char(&incoming_packet);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000882 if (type == SSH2_MSG_NEWKEYS)
883 set_newkeys(MODE_IN);
Damien Miller33b13562000-04-04 14:38:59 +1000884#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +0000885 fprintf(stderr, "read/plain[%d]:\r\n", type);
Damien Miller33b13562000-04-04 14:38:59 +1000886 buffer_dump(&incoming_packet);
887#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000888 /* reset for next packet */
889 packet_length = 0;
890 return type;
Damien Miller33b13562000-04-04 14:38:59 +1000891}
892
893int
Damien Millerdff50992002-01-22 23:16:32 +1100894packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +1000895{
Damien Miller659811f2002-01-22 23:23:11 +1100896 int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000897 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +1000898 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +1000899
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000900 for (;;) {
901 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +1100902 type = packet_read_poll2(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000903 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000904 DBG(debug("received packet type %d", type));
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000905 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000906 case SSH2_MSG_IGNORE:
907 break;
908 case SSH2_MSG_DEBUG:
909 packet_get_char();
910 msg = packet_get_string(NULL);
911 debug("Remote: %.900s", msg);
912 xfree(msg);
913 msg = packet_get_string(NULL);
914 xfree(msg);
915 break;
916 case SSH2_MSG_DISCONNECT:
917 reason = packet_get_int();
918 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000919 log("Received disconnect from %s: %d: %.400s", get_remote_ipaddr(),
920 reason, msg);
Damien Miller33b13562000-04-04 14:38:59 +1000921 xfree(msg);
922 fatal_cleanup();
923 break;
Damien Miller659811f2002-01-22 23:23:11 +1100924 case SSH2_MSG_UNIMPLEMENTED:
925 seqnr = packet_get_int();
926 debug("Received SSH2_MSG_UNIMPLEMENTED for %d", seqnr);
927 break;
Damien Miller33b13562000-04-04 14:38:59 +1000928 default:
929 return type;
930 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000931 }
Damien Miller33b13562000-04-04 14:38:59 +1000932 } else {
Damien Millerdff50992002-01-22 23:16:32 +1100933 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000934 switch (type) {
Damien Miller33b13562000-04-04 14:38:59 +1000935 case SSH_MSG_IGNORE:
936 break;
937 case SSH_MSG_DEBUG:
938 msg = packet_get_string(NULL);
939 debug("Remote: %.900s", msg);
940 xfree(msg);
941 break;
942 case SSH_MSG_DISCONNECT:
943 msg = packet_get_string(NULL);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000944 log("Received disconnect from %s: %.400s", get_remote_ipaddr(),
945 msg);
Damien Miller33b13562000-04-04 14:38:59 +1000946 fatal_cleanup();
947 xfree(msg);
948 break;
949 default:
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000950 if (type)
Damien Miller33b13562000-04-04 14:38:59 +1000951 DBG(debug("received packet type %d", type));
952 return type;
953 break;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000954 }
Damien Miller33b13562000-04-04 14:38:59 +1000955 }
956 }
957}
958
Damien Miller278f9072001-12-21 15:00:19 +1100959int
Damien Millerdff50992002-01-22 23:16:32 +1100960packet_read_poll(void)
Damien Miller278f9072001-12-21 15:00:19 +1100961{
Damien Millerdff50992002-01-22 23:16:32 +1100962 return packet_read_poll_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +1100963}
964
Damien Miller5428f641999-11-25 11:54:57 +1100965/*
966 * Buffers the given amount of input characters. This is intended to be used
967 * together with packet_read_poll.
968 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
970void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000971packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972{
Damien Miller95def091999-11-25 00:26:21 +1100973 buffer_append(&input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974}
975
976/* Returns a character from the packet. */
977
Ben Lindstrom46c16222000-12-22 01:43:59 +0000978u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000979packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980{
Damien Miller95def091999-11-25 00:26:21 +1100981 char ch;
982 buffer_get(&incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000983 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984}
985
986/* Returns an integer from the packet data. */
987
Ben Lindstrom46c16222000-12-22 01:43:59 +0000988u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000989packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990{
Damien Miller95def091999-11-25 00:26:21 +1100991 return buffer_get_int(&incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
Damien Miller5428f641999-11-25 11:54:57 +1100994/*
995 * Returns an arbitrary precision integer from the packet data. The integer
996 * must have been initialized before this call.
997 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
999void
Damien Millerd432ccf2002-01-22 23:14:44 +11001000packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001{
Damien Miller76e1e362002-01-22 23:15:57 +11001002 buffer_get_bignum(&incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003}
1004
Damien Miller33b13562000-04-04 14:38:59 +10001005void
Damien Millerd432ccf2002-01-22 23:14:44 +11001006packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001007{
Damien Miller76e1e362002-01-22 23:15:57 +11001008 buffer_get_bignum2(&incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001009}
1010
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001011void *
Damien Miller33b13562000-04-04 14:38:59 +10001012packet_get_raw(int *length_ptr)
1013{
1014 int bytes = buffer_len(&incoming_packet);
1015 if (length_ptr != NULL)
1016 *length_ptr = bytes;
1017 return buffer_ptr(&incoming_packet);
1018}
1019
Damien Miller4af51302000-04-16 11:18:38 +10001020int
1021packet_remaining(void)
1022{
1023 return buffer_len(&incoming_packet);
1024}
1025
Damien Miller5428f641999-11-25 11:54:57 +11001026/*
1027 * Returns a string from the packet data. The string is allocated using
1028 * xmalloc; it is the responsibility of the calling program to free it when
1029 * no longer needed. The length_ptr argument may be NULL, or point to an
1030 * integer into which the length of the string is stored.
1031 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001033void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001034packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035{
Damien Miller95def091999-11-25 00:26:21 +11001036 return buffer_get_string(&incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037}
1038
Damien Miller5428f641999-11-25 11:54:57 +11001039/*
1040 * Sends a diagnostic message from the server to the client. This message
1041 * can be sent at any time (but not while constructing another message). The
1042 * message is printed immediately, but only if the client is being executed
1043 * in verbose mode. These messages are primarily intended to ease debugging
1044 * authentication problems. The length of the formatted message must not
1045 * exceed 1024 bytes. This will automatically call packet_write_wait.
1046 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047
1048void
Damien Miller95def091999-11-25 00:26:21 +11001049packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050{
Damien Miller95def091999-11-25 00:26:21 +11001051 char buf[1024];
1052 va_list args;
1053
Ben Lindstroma14ee472000-12-07 01:24:58 +00001054 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1055 return;
1056
Damien Miller95def091999-11-25 00:26:21 +11001057 va_start(args, fmt);
1058 vsnprintf(buf, sizeof(buf), fmt, args);
1059 va_end(args);
1060
Damien Miller7c8af4f2000-05-01 08:24:07 +10001061 if (compat20) {
1062 packet_start(SSH2_MSG_DEBUG);
1063 packet_put_char(0); /* bool: always display */
1064 packet_put_cstring(buf);
1065 packet_put_cstring("");
1066 } else {
1067 packet_start(SSH_MSG_DEBUG);
1068 packet_put_cstring(buf);
1069 }
Damien Miller95def091999-11-25 00:26:21 +11001070 packet_send();
1071 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072}
1073
Damien Miller5428f641999-11-25 11:54:57 +11001074/*
1075 * Logs the error plus constructs and sends a disconnect packet, closes the
1076 * connection, and exits. This function never returns. The error message
1077 * should not contain a newline. The length of the formatted message must
1078 * not exceed 1024 bytes.
1079 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080
1081void
Damien Miller95def091999-11-25 00:26:21 +11001082packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083{
Damien Miller95def091999-11-25 00:26:21 +11001084 char buf[1024];
1085 va_list args;
1086 static int disconnecting = 0;
1087 if (disconnecting) /* Guard against recursive invocations. */
1088 fatal("packet_disconnect called recursively.");
1089 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090
Damien Miller5428f641999-11-25 11:54:57 +11001091 /*
1092 * Format the message. Note that the caller must make sure the
1093 * message is of limited size.
1094 */
Damien Miller95def091999-11-25 00:26:21 +11001095 va_start(args, fmt);
1096 vsnprintf(buf, sizeof(buf), fmt, args);
1097 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098
Damien Miller95def091999-11-25 00:26:21 +11001099 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001100 if (compat20) {
1101 packet_start(SSH2_MSG_DISCONNECT);
1102 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1103 packet_put_cstring(buf);
1104 packet_put_cstring("");
1105 } else {
1106 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001107 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001108 }
Damien Miller95def091999-11-25 00:26:21 +11001109 packet_send();
1110 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111
Damien Miller95def091999-11-25 00:26:21 +11001112 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001113 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller95def091999-11-25 00:26:21 +11001115 /* Close the connection. */
1116 packet_close();
1117
1118 /* Display the error locally and exit. */
Damien Milleraae6c611999-12-06 11:47:28 +11001119 log("Disconnecting: %.100s", buf);
1120 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121}
1122
Damien Miller5428f641999-11-25 11:54:57 +11001123/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124
1125void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001126packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127{
Damien Miller95def091999-11-25 00:26:21 +11001128 int len = buffer_len(&output);
1129 if (len > 0) {
1130 len = write(connection_out, buffer_ptr(&output), len);
1131 if (len <= 0) {
1132 if (errno == EAGAIN)
1133 return;
1134 else
1135 fatal("Write failed: %.100s", strerror(errno));
1136 }
1137 buffer_consume(&output, len);
1138 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139}
1140
Damien Miller5428f641999-11-25 11:54:57 +11001141/*
1142 * Calls packet_write_poll repeatedly until all pending output data has been
1143 * written.
1144 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
1146void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001147packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001149 fd_set *setp;
1150
1151 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) *
1152 sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001153 packet_write_poll();
1154 while (packet_have_data_to_write()) {
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001155 memset(setp, 0, howmany(connection_out + 1, NFDBITS) *
1156 sizeof(fd_mask));
1157 FD_SET(connection_out, setp);
1158 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +00001159 (errno == EAGAIN || errno == EINTR))
1160 ;
Damien Miller95def091999-11-25 00:26:21 +11001161 packet_write_poll();
1162 }
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001163 xfree(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164}
1165
1166/* Returns true if there is buffered data to write to the connection. */
1167
1168int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001169packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170{
Damien Miller95def091999-11-25 00:26:21 +11001171 return buffer_len(&output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172}
1173
1174/* Returns true if there is not too much data to write to the connection. */
1175
1176int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001177packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178{
Damien Miller95def091999-11-25 00:26:21 +11001179 if (interactive_mode)
1180 return buffer_len(&output) < 16384;
1181 else
1182 return buffer_len(&output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183}
1184
1185/* Informs that the current session is interactive. Sets IP flags for that. */
1186
1187void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001188packet_set_interactive(int interactive)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001190 static int called = 0;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001191#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001192 int lowdelay = IPTOS_LOWDELAY;
1193 int throughput = IPTOS_THROUGHPUT;
Damien Miller0bcf9ea2001-02-27 14:03:30 +11001194#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001196 if (called)
1197 return;
1198 called = 1;
1199
Damien Miller95def091999-11-25 00:26:21 +11001200 /* Record that we are in interactive mode. */
1201 interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202
Damien Miller34132e52000-01-14 15:45:46 +11001203 /* Only set socket options if using a socket. */
1204 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +11001205 return;
Damien Miller34132e52000-01-14 15:45:46 +11001206 /*
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001207 * IPTOS_LOWDELAY and IPTOS_THROUGHPUT are IPv4 only
Damien Miller34132e52000-01-14 15:45:46 +11001208 */
Damien Miller95def091999-11-25 00:26:21 +11001209 if (interactive) {
Damien Miller5428f641999-11-25 11:54:57 +11001210 /*
1211 * Set IP options for an interactive connection. Use
1212 * IPTOS_LOWDELAY and TCP_NODELAY.
1213 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001214#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001215 if (packet_connection_is_ipv4()) {
Kevin Steves0c696152001-01-24 13:47:43 +00001216 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS,
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001217 (void *) &lowdelay, sizeof(lowdelay)) < 0)
Kevin Steves0c696152001-01-24 13:47:43 +00001218 error("setsockopt IPTOS_LOWDELAY: %.100s",
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001219 strerror(errno));
1220 }
Damien Miller615f9392000-05-17 22:53:33 +10001221#endif
Damien Miller398e1cf2002-02-05 11:52:13 +11001222 set_nodelay(connection_in);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001223 } else if (packet_connection_is_ipv4()) {
Damien Miller5428f641999-11-25 11:54:57 +11001224 /*
1225 * Set IP options for a non-interactive connection. Use
1226 * IPTOS_THROUGHPUT.
1227 */
Damien Miller2ae714f2000-07-11 09:29:50 +10001228#if defined(IP_TOS) && !defined(IP_TOS_IS_BROKEN)
Damien Miller95def091999-11-25 00:26:21 +11001229 if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput,
Damien Miller34132e52000-01-14 15:45:46 +11001230 sizeof(throughput)) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001231 error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno));
Damien Miller615f9392000-05-17 22:53:33 +10001232#endif
Damien Miller95def091999-11-25 00:26:21 +11001233 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234}
1235
1236/* Returns true if the current connection is interactive. */
1237
1238int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001239packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240{
Damien Miller95def091999-11-25 00:26:21 +11001241 return interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
Damien Miller6162d121999-11-21 13:23:52 +11001243
1244int
1245packet_set_maxsize(int s)
1246{
Damien Miller95def091999-11-25 00:26:21 +11001247 static int called = 0;
1248 if (called) {
Damien Miller33b13562000-04-04 14:38:59 +10001249 log("packet_set_maxsize: called twice: old %d new %d",
1250 max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001251 return -1;
1252 }
1253 if (s < 4 * 1024 || s > 1024 * 1024) {
1254 log("packet_set_maxsize: bad size %d", s);
1255 return -1;
1256 }
Ben Lindstromae3de4b2001-10-03 17:10:17 +00001257 called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001258 debug("packet_set_maxsize: setting to %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001259 max_packet_size = s;
1260 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001261}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001262
Damien Miller9f643902001-11-12 11:02:52 +11001263/* roundup current message to pad bytes */
1264void
1265packet_add_padding(u_char pad)
1266{
1267 extra_pad = pad;
1268}
1269
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001270/*
1271 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001272 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001273 * byte SSH_MSG_IGNORE
1274 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001275 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001276 * All implementations MUST understand (and ignore) this message at any
1277 * time (after receiving the protocol version). No implementation is
1278 * required to send them. This message can be used as an additional
1279 * protection measure against advanced traffic analysis techniques.
1280 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001281void
1282packet_send_ignore(int nbytes)
1283{
1284 u_int32_t rand = 0;
1285 int i;
1286
1287 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001288 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001289 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001290 if (i % 4 == 0)
1291 rand = arc4random();
1292 packet_put_char(rand & 0xff);
1293 rand >>= 8;
1294 }
1295}