blob: 0d27e759237da5de8a00d3a4d00542f57ef34347 [file] [log] [blame]
Damien Millerce986542013-07-18 16:12:44 +10001/* $OpenBSD: packet.c,v 1.188 2013/07/12 00:19:58 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains code implementing the packet protocol and communication
7 * with the other side. This same code is used both on client and server side.
Damien Miller33b13562000-04-04 14:38:59 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller33b13562000-04-04 14:38:59 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000017 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Miller68f8e992006-03-15 11:24:12 +110041
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/types.h>
Damien Millera0898b82003-04-09 21:05:52 +100043#include "openbsd-compat/sys-queue.h"
Damien Miller8dbffe72006-08-05 11:02:17 +100044#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100046#ifdef HAVE_SYS_TIME_H
47# include <sys/time.h>
48#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100049
Damien Miller8ec8c3e2006-07-10 20:35:38 +100050#include <netinet/in.h>
Damien Miller68f8e992006-03-15 11:24:12 +110051#include <netinet/ip.h>
Darren Tuckerdace2332006-09-22 19:22:17 +100052#include <arpa/inet.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Darren Tucker5d196262006-07-12 22:15:16 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100060#include <signal.h>
Darren Tuckerc53c2af2013-05-16 20:28:16 +100061#include <time.h>
Darren Tucker5d196262006-07-12 22:15:16 +100062
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "xmalloc.h"
64#include "buffer.h"
65#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "compress.h"
68#include "deattack.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000069#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100070#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "ssh1.h"
Damien Miller33b13562000-04-04 14:38:59 +100072#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110073#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100074#include "key.h"
Damien Miller33b13562000-04-04 14:38:59 +100075#include "kex.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000076#include "mac.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "canohost.h"
Damien Miller4d007762002-02-05 11:52:54 +110079#include "misc.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000080#include "ssh.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100081#include "roaming.h"
Damien Miller33b13562000-04-04 14:38:59 +100082
83#ifdef PACKET_DEBUG
84#define DBG(x) x
85#else
86#define DBG(x)
87#endif
88
Damien Miller13ae44c2009-01-28 16:38:41 +110089#define PACKET_MAX_SIZE (256 * 1024)
90
Darren Tuckerf7288d72009-06-21 18:12:20 +100091struct packet_state {
Damien Millera5539d22003-04-09 20:50:06 +100092 u_int32_t seqnr;
93 u_int32_t packets;
94 u_int64_t blocks;
Damien Millerb61f3fc2008-07-11 17:36:48 +100095 u_int64_t bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +100096};
Damien Miller13ae44c2009-01-28 16:38:41 +110097
Damien Millera5539d22003-04-09 20:50:06 +100098struct packet {
99 TAILQ_ENTRY(packet) next;
100 u_char type;
101 Buffer payload;
102};
Darren Tuckerf7288d72009-06-21 18:12:20 +1000103
104struct session_state {
105 /*
106 * This variable contains the file descriptors used for
107 * communicating with the other side. connection_in is used for
108 * reading; connection_out for writing. These can be the same
109 * descriptor, in which case it is assumed to be a socket.
110 */
111 int connection_in;
112 int connection_out;
113
114 /* Protocol flags for the remote side. */
115 u_int remote_protocol_flags;
116
117 /* Encryption context for receiving data. Only used for decryption. */
118 CipherContext receive_context;
119
120 /* Encryption context for sending data. Only used for encryption. */
121 CipherContext send_context;
122
123 /* Buffer for raw input data from the socket. */
124 Buffer input;
125
126 /* Buffer for raw output data going to the socket. */
127 Buffer output;
128
129 /* Buffer for the partial outgoing packet being constructed. */
130 Buffer outgoing_packet;
131
132 /* Buffer for the incoming packet currently being processed. */
133 Buffer incoming_packet;
134
135 /* Scratch buffer for packet compression/decompression. */
136 Buffer compression_buffer;
137 int compression_buffer_ready;
138
139 /*
140 * Flag indicating whether packet compression/decompression is
141 * enabled.
142 */
143 int packet_compression;
144
145 /* default maximum packet size */
146 u_int max_packet_size;
147
148 /* Flag indicating whether this module has been initialized. */
149 int initialized;
150
151 /* Set to true if the connection is interactive. */
152 int interactive_mode;
153
154 /* Set to true if we are the server side. */
155 int server_side;
156
157 /* Set to true if we are authenticated. */
158 int after_authentication;
159
160 int keep_alive_timeouts;
161
162 /* The maximum time that we will wait to send or receive a packet */
163 int packet_timeout_ms;
164
165 /* Session key information for Encryption and MAC */
166 Newkeys *newkeys[MODE_MAX];
167 struct packet_state p_read, p_send;
168
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000169 /* Volume-based rekeying */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000170 u_int64_t max_blocks_in, max_blocks_out;
171 u_int32_t rekey_limit;
172
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000173 /* Time-based rekeying */
174 time_t rekey_interval; /* how often in seconds */
175 time_t rekey_time; /* time of last rekeying */
176
Darren Tuckerf7288d72009-06-21 18:12:20 +1000177 /* Session key for protocol v1 */
178 u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
179 u_int ssh1_keylen;
180
181 /* roundup current message to extra_pad bytes */
182 u_char extra_pad;
183
184 /* XXX discard incoming data after MAC error */
185 u_int packet_discard;
186 Mac *packet_discard_mac;
187
188 /* Used in packet_read_poll2() */
189 u_int packlen;
190
Darren Tucker7b935c72009-06-21 18:59:36 +1000191 /* Used in packet_send2 */
192 int rekeying;
193
194 /* Used in packet_set_interactive */
195 int set_interactive_called;
196
197 /* Used in packet_set_maxsize */
198 int set_maxsize_called;
199
Darren Tuckerf7288d72009-06-21 18:12:20 +1000200 TAILQ_HEAD(, packet) outgoing;
201};
202
Darren Tuckere841eb02009-07-06 07:11:13 +1000203static struct session_state *active_state, *backup_state;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000204
205static struct session_state *
Darren Tuckerb422afa2009-06-21 18:58:46 +1000206alloc_session_state(void)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000207{
Damien Miller7a221a12010-11-20 15:14:29 +1100208 struct session_state *s = xcalloc(1, sizeof(*s));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000209
Damien Miller7a221a12010-11-20 15:14:29 +1100210 s->connection_in = -1;
211 s->connection_out = -1;
212 s->max_packet_size = 32768;
213 s->packet_timeout_ms = -1;
214 return s;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000215}
Damien Millera5539d22003-04-09 20:50:06 +1000216
Damien Miller5428f641999-11-25 11:54:57 +1100217/*
218 * Sets the descriptors used for communication. Disables encryption until
219 * packet_set_encryption_key is called.
220 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221void
222packet_set_connection(int fd_in, int fd_out)
223{
Damien Millerea111192013-04-23 19:24:32 +1000224 const Cipher *none = cipher_by_name("none");
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000225
Damien Miller874d77b2000-10-14 16:23:11 +1100226 if (none == NULL)
227 fatal("packet_set_connection: cannot load cipher 'none'");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000228 if (active_state == NULL)
229 active_state = alloc_session_state();
230 active_state->connection_in = fd_in;
231 active_state->connection_out = fd_out;
232 cipher_init(&active_state->send_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000233 0, NULL, 0, CIPHER_ENCRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000234 cipher_init(&active_state->receive_context, none, (const u_char *)"",
Darren Tucker1f8311c2004-05-13 16:39:33 +1000235 0, NULL, 0, CIPHER_DECRYPT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000236 active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL;
237 if (!active_state->initialized) {
238 active_state->initialized = 1;
239 buffer_init(&active_state->input);
240 buffer_init(&active_state->output);
241 buffer_init(&active_state->outgoing_packet);
242 buffer_init(&active_state->incoming_packet);
243 TAILQ_INIT(&active_state->outgoing);
244 active_state->p_send.packets = active_state->p_read.packets = 0;
Damien Miller95def091999-11-25 00:26:21 +1100245 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
Darren Tucker3fc464e2008-06-13 06:42:45 +1000248void
249packet_set_timeout(int timeout, int count)
250{
Damien Miller8ed4de82011-12-19 10:52:50 +1100251 if (timeout <= 0 || count <= 0) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000252 active_state->packet_timeout_ms = -1;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000253 return;
254 }
255 if ((INT_MAX / 1000) / count < timeout)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000256 active_state->packet_timeout_ms = INT_MAX;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000257 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000258 active_state->packet_timeout_ms = timeout * count * 1000;
Darren Tucker3fc464e2008-06-13 06:42:45 +1000259}
260
Damien Miller13ae44c2009-01-28 16:38:41 +1100261static void
262packet_stop_discard(void)
263{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000264 if (active_state->packet_discard_mac) {
Damien Miller13ae44c2009-01-28 16:38:41 +1100265 char buf[1024];
266
267 memset(buf, 'a', sizeof(buf));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000268 while (buffer_len(&active_state->incoming_packet) <
269 PACKET_MAX_SIZE)
270 buffer_append(&active_state->incoming_packet, buf,
271 sizeof(buf));
272 (void) mac_compute(active_state->packet_discard_mac,
273 active_state->p_read.seqnr,
274 buffer_ptr(&active_state->incoming_packet),
Damien Miller13ae44c2009-01-28 16:38:41 +1100275 PACKET_MAX_SIZE);
276 }
277 logit("Finished discarding for %.200s", get_remote_ipaddr());
278 cleanup_exit(255);
279}
280
281static void
282packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
283{
Damien Milleraf43a7a2012-12-12 10:46:31 +1100284 if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm))
Damien Miller13ae44c2009-01-28 16:38:41 +1100285 packet_disconnect("Packet corrupt");
286 if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000287 active_state->packet_discard_mac = mac;
288 if (buffer_len(&active_state->input) >= discard)
Damien Miller13ae44c2009-01-28 16:38:41 +1100289 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +1000290 active_state->packet_discard = discard -
291 buffer_len(&active_state->input);
Damien Miller13ae44c2009-01-28 16:38:41 +1100292}
293
Damien Miller34132e52000-01-14 15:45:46 +1100294/* Returns 1 if remote host is connected via socket, 0 if not. */
295
296int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000297packet_connection_is_on_socket(void)
Damien Miller34132e52000-01-14 15:45:46 +1100298{
299 struct sockaddr_storage from, to;
300 socklen_t fromlen, tolen;
301
302 /* filedescriptors in and out are the same, so it's a socket */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000303 if (active_state->connection_in == active_state->connection_out)
Damien Miller34132e52000-01-14 15:45:46 +1100304 return 1;
305 fromlen = sizeof(from);
306 memset(&from, 0, sizeof(from));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000307 if (getpeername(active_state->connection_in, (struct sockaddr *)&from,
308 &fromlen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100309 return 0;
310 tolen = sizeof(to);
311 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000312 if (getpeername(active_state->connection_out, (struct sockaddr *)&to,
313 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100314 return 0;
315 if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
316 return 0;
317 if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
318 return 0;
319 return 1;
320}
321
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000322/*
Ben Lindstromf6027d32002-03-22 01:42:04 +0000323 * Exports an IV from the CipherContext required to export the key
324 * state back from the unprivileged child to the privileged parent
325 * process.
326 */
327
328void
329packet_get_keyiv(int mode, u_char *iv, u_int len)
330{
331 CipherContext *cc;
332
333 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000334 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000335 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000336 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000337
338 cipher_get_keyiv(cc, iv, len);
339}
340
341int
342packet_get_keycontext(int mode, u_char *dat)
343{
344 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000345
Ben Lindstromf6027d32002-03-22 01:42:04 +0000346 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000347 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000348 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000349 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000350
351 return (cipher_get_keycontext(cc, dat));
352}
353
354void
355packet_set_keycontext(int mode, u_char *dat)
356{
357 CipherContext *cc;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000358
Ben Lindstromf6027d32002-03-22 01:42:04 +0000359 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000360 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000361 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000362 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000363
364 cipher_set_keycontext(cc, dat);
365}
366
367int
368packet_get_keyiv_len(int mode)
369{
370 CipherContext *cc;
371
372 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000373 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000374 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000375 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000376
377 return (cipher_get_keyiv_len(cc));
378}
Damien Miller4f7becb2006-03-26 14:10:14 +1100379
Ben Lindstromf6027d32002-03-22 01:42:04 +0000380void
381packet_set_iv(int mode, u_char *dat)
382{
383 CipherContext *cc;
384
385 if (mode == MODE_OUT)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000386 cc = &active_state->send_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000387 else
Darren Tuckerf7288d72009-06-21 18:12:20 +1000388 cc = &active_state->receive_context;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000389
390 cipher_set_keyiv(cc, dat);
391}
Damien Miller4f7becb2006-03-26 14:10:14 +1100392
Ben Lindstromf6027d32002-03-22 01:42:04 +0000393int
Damien Miller2b92d322003-06-11 22:05:06 +1000394packet_get_ssh1_cipher(void)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000395{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000396 return (cipher_get_number(active_state->receive_context.cipher));
Ben Lindstromf6027d32002-03-22 01:42:04 +0000397}
398
Damien Millera5539d22003-04-09 20:50:06 +1000399void
Damien Miller7a221a12010-11-20 15:14:29 +1100400packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks,
401 u_int32_t *packets, u_int64_t *bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000402{
Damien Millera5539d22003-04-09 20:50:06 +1000403 struct packet_state *state;
404
Darren Tuckerf7288d72009-06-21 18:12:20 +1000405 state = (mode == MODE_IN) ?
406 &active_state->p_read : &active_state->p_send;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000407 if (seqnr)
408 *seqnr = state->seqnr;
409 if (blocks)
410 *blocks = state->blocks;
411 if (packets)
412 *packets = state->packets;
413 if (bytes)
414 *bytes = state->bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000415}
416
417void
Damien Millerb61f3fc2008-07-11 17:36:48 +1000418packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
419 u_int64_t bytes)
Ben Lindstromf6027d32002-03-22 01:42:04 +0000420{
Damien Millera5539d22003-04-09 20:50:06 +1000421 struct packet_state *state;
422
Darren Tuckerf7288d72009-06-21 18:12:20 +1000423 state = (mode == MODE_IN) ?
424 &active_state->p_read : &active_state->p_send;
Damien Millera5539d22003-04-09 20:50:06 +1000425 state->seqnr = seqnr;
426 state->blocks = blocks;
427 state->packets = packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000428 state->bytes = bytes;
Ben Lindstromf6027d32002-03-22 01:42:04 +0000429}
430
Damien Millerd2ac5d72011-05-15 08:43:13 +1000431static int
432packet_connection_af(void)
Damien Miller34132e52000-01-14 15:45:46 +1100433{
434 struct sockaddr_storage to;
Damien Miller6fe375d2000-01-23 09:38:00 +1100435 socklen_t tolen = sizeof(to);
Damien Miller34132e52000-01-14 15:45:46 +1100436
437 memset(&to, 0, sizeof(to));
Darren Tuckerf7288d72009-06-21 18:12:20 +1000438 if (getsockname(active_state->connection_out, (struct sockaddr *)&to,
439 &tolen) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100440 return 0;
Damien Milleraa100c52002-04-26 16:54:34 +1000441#ifdef IPV4_IN_IPV6
Damien Millera8e06ce2003-11-21 23:48:55 +1100442 if (to.ss_family == AF_INET6 &&
Damien Milleraa100c52002-04-26 16:54:34 +1000443 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
Damien Millerd2ac5d72011-05-15 08:43:13 +1000444 return AF_INET;
Damien Milleraa100c52002-04-26 16:54:34 +1000445#endif
Damien Millerd2ac5d72011-05-15 08:43:13 +1000446 return to.ss_family;
Damien Miller34132e52000-01-14 15:45:46 +1100447}
448
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449/* Sets the connection into non-blocking mode. */
450
451void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000452packet_set_nonblocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
Damien Miller95def091999-11-25 00:26:21 +1100454 /* Set the socket into non-blocking mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000455 set_nonblock(active_state->connection_in);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456
Darren Tuckerf7288d72009-06-21 18:12:20 +1000457 if (active_state->connection_out != active_state->connection_in)
458 set_nonblock(active_state->connection_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459}
460
461/* Returns the socket used for reading. */
462
463int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000464packet_get_connection_in(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000466 return active_state->connection_in;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467}
468
469/* Returns the descriptor used for writing. */
470
471int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000472packet_get_connection_out(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000474 return active_state->connection_out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
477/* Closes the connection and clears and frees internal data structures. */
478
479void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000480packet_close(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000482 if (!active_state->initialized)
Damien Miller95def091999-11-25 00:26:21 +1100483 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000484 active_state->initialized = 0;
485 if (active_state->connection_in == active_state->connection_out) {
486 shutdown(active_state->connection_out, SHUT_RDWR);
487 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100488 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000489 close(active_state->connection_in);
490 close(active_state->connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100491 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000492 buffer_free(&active_state->input);
493 buffer_free(&active_state->output);
494 buffer_free(&active_state->outgoing_packet);
495 buffer_free(&active_state->incoming_packet);
496 if (active_state->compression_buffer_ready) {
497 buffer_free(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100498 buffer_compress_uninit();
499 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000500 cipher_cleanup(&active_state->send_context);
501 cipher_cleanup(&active_state->receive_context);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502}
503
504/* Sets remote side protocol flags. */
505
506void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000507packet_set_protocol_flags(u_int protocol_flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000509 active_state->remote_protocol_flags = protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510}
511
512/* Returns the remote protocol flags set earlier by the above function. */
513
Ben Lindstrom46c16222000-12-22 01:43:59 +0000514u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000515packet_get_protocol_flags(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000517 return active_state->remote_protocol_flags;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518}
519
Damien Miller5428f641999-11-25 11:54:57 +1100520/*
521 * Starts packet compression from the next packet on in both directions.
522 * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
523 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static void
526packet_init_compression(void)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000527{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000528 if (active_state->compression_buffer_ready == 1)
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000529 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000530 active_state->compression_buffer_ready = 1;
531 buffer_init(&active_state->compression_buffer);
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000532}
533
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534void
535packet_start_compression(int level)
536{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000537 if (active_state->packet_compression && !compat20)
Damien Miller95def091999-11-25 00:26:21 +1100538 fatal("Compression already enabled.");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000539 active_state->packet_compression = 1;
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000540 packet_init_compression();
541 buffer_compress_init_send(level);
542 buffer_compress_init_recv();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543}
544
Damien Miller5428f641999-11-25 11:54:57 +1100545/*
Damien Miller5428f641999-11-25 11:54:57 +1100546 * Causes any further packets to be encrypted using the given key. The same
547 * key is used for both sending and reception. However, both directions are
548 * encrypted independently of each other.
549 */
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000550
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551void
Damien Miller7a221a12010-11-20 15:14:29 +1100552packet_set_encryption_key(const u_char *key, u_int keylen, int number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553{
Damien Millerea111192013-04-23 19:24:32 +1000554 const Cipher *cipher = cipher_by_number(number);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000555
Damien Miller874d77b2000-10-14 16:23:11 +1100556 if (cipher == NULL)
557 fatal("packet_set_encryption_key: unknown cipher number %d", number);
Damien Miller33b13562000-04-04 14:38:59 +1000558 if (keylen < 20)
Damien Miller874d77b2000-10-14 16:23:11 +1100559 fatal("packet_set_encryption_key: keylen too small: %d", keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000560 if (keylen > SSH_SESSION_KEY_LENGTH)
561 fatal("packet_set_encryption_key: keylen too big: %d", keylen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000562 memcpy(active_state->ssh1_key, key, keylen);
563 active_state->ssh1_keylen = keylen;
564 cipher_init(&active_state->send_context, cipher, key, keylen, NULL,
565 0, CIPHER_ENCRYPT);
566 cipher_init(&active_state->receive_context, cipher, key, keylen, NULL,
567 0, CIPHER_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568}
569
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000570u_int
571packet_get_encryption_key(u_char *key)
572{
573 if (key == NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +1000574 return (active_state->ssh1_keylen);
575 memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen);
576 return (active_state->ssh1_keylen);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000577}
578
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000579/* Start constructing a packet to send. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580void
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000581packet_start(u_char type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582{
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000583 u_char buf[9];
584 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Ben Lindstrom204e4882001-03-05 06:47:00 +0000586 DBG(debug("packet_start[%d]", type));
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000587 len = compat20 ? 6 : 9;
588 memset(buf, 0, len - 1);
589 buf[len - 1] = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000590 buffer_clear(&active_state->outgoing_packet);
591 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000592}
593
Ben Lindstrom80c6d772001-06-05 21:09:18 +0000594/* Append payload. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595void
596packet_put_char(int value)
597{
Damien Miller95def091999-11-25 00:26:21 +1100598 char ch = value;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000599
Darren Tuckerf7288d72009-06-21 18:12:20 +1000600 buffer_append(&active_state->outgoing_packet, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
Damien Miller4f7becb2006-03-26 14:10:14 +1100602
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000604packet_put_int(u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000606 buffer_put_int(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607}
Damien Miller4f7becb2006-03-26 14:10:14 +1100608
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609void
Darren Tucker761c3892009-06-21 18:16:26 +1000610packet_put_int64(u_int64_t value)
611{
612 buffer_put_int64(&active_state->outgoing_packet, value);
613}
614
615void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100616packet_put_string(const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000618 buffer_put_string(&active_state->outgoing_packet, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}
Damien Miller4f7becb2006-03-26 14:10:14 +1100620
Damien Miller33b13562000-04-04 14:38:59 +1000621void
622packet_put_cstring(const char *str)
623{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000624 buffer_put_cstring(&active_state->outgoing_packet, str);
Damien Miller33b13562000-04-04 14:38:59 +1000625}
Damien Miller4f7becb2006-03-26 14:10:14 +1100626
Damien Miller33b13562000-04-04 14:38:59 +1000627void
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100628packet_put_raw(const void *buf, u_int len)
Damien Miller33b13562000-04-04 14:38:59 +1000629{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000630 buffer_append(&active_state->outgoing_packet, buf, len);
Damien Miller33b13562000-04-04 14:38:59 +1000631}
Damien Miller4f7becb2006-03-26 14:10:14 +1100632
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633void
Damien Miller95def091999-11-25 00:26:21 +1100634packet_put_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000636 buffer_put_bignum(&active_state->outgoing_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637}
Damien Miller4f7becb2006-03-26 14:10:14 +1100638
Damien Miller33b13562000-04-04 14:38:59 +1000639void
640packet_put_bignum2(BIGNUM * value)
641{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000642 buffer_put_bignum2(&active_state->outgoing_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +1000643}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller6af914a2010-09-10 11:39:26 +1000645#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000646void
647packet_put_ecpoint(const EC_GROUP *curve, const EC_POINT *point)
648{
649 buffer_put_ecpoint(&active_state->outgoing_packet, curve, point);
650}
Damien Miller6af914a2010-09-10 11:39:26 +1000651#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000652
Damien Miller5428f641999-11-25 11:54:57 +1100653/*
654 * Finalizes and sends the packet. If the encryption key has been set,
655 * encrypts the packet before sending.
656 */
Damien Miller95def091999-11-25 00:26:21 +1100657
Ben Lindstrombba81212001-06-25 05:01:22 +0000658static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000659packet_send1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000661 u_char buf[8], *cp;
Damien Miller95def091999-11-25 00:26:21 +1100662 int i, padding, len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000663 u_int checksum;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000664 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller5428f641999-11-25 11:54:57 +1100666 /*
667 * If using packet compression, compress the payload of the outgoing
668 * packet.
669 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000670 if (active_state->packet_compression) {
671 buffer_clear(&active_state->compression_buffer);
Damien Miller95def091999-11-25 00:26:21 +1100672 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000673 buffer_consume(&active_state->outgoing_packet, 8);
Damien Miller95def091999-11-25 00:26:21 +1100674 /* padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000675 buffer_append(&active_state->compression_buffer,
676 "\0\0\0\0\0\0\0\0", 8);
677 buffer_compress(&active_state->outgoing_packet,
678 &active_state->compression_buffer);
679 buffer_clear(&active_state->outgoing_packet);
680 buffer_append(&active_state->outgoing_packet,
681 buffer_ptr(&active_state->compression_buffer),
682 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100683 }
684 /* Compute packet length without padding (add checksum, remove padding). */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000685 len = buffer_len(&active_state->outgoing_packet) + 4 - 8;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686
Damien Millere247cc42000-05-07 12:03:14 +1000687 /* Insert padding. Initialized to zero in packet_start1() */
Damien Miller95def091999-11-25 00:26:21 +1100688 padding = 8 - len % 8;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000689 if (!active_state->send_context.plaintext) {
690 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Miller95def091999-11-25 00:26:21 +1100691 for (i = 0; i < padding; i++) {
692 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000693 rnd = arc4random();
694 cp[7 - i] = rnd & 0xff;
695 rnd >>= 8;
Damien Miller95def091999-11-25 00:26:21 +1100696 }
697 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000698 buffer_consume(&active_state->outgoing_packet, 8 - padding);
Damien Miller95def091999-11-25 00:26:21 +1100699
700 /* Add check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000701 checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet),
702 buffer_len(&active_state->outgoing_packet));
Damien Miller3f941882006-03-31 23:13:02 +1100703 put_u32(buf, checksum);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000704 buffer_append(&active_state->outgoing_packet, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705
706#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100707 fprintf(stderr, "packet_send plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000708 buffer_dump(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709#endif
710
Damien Miller95def091999-11-25 00:26:21 +1100711 /* Append to output. */
Damien Miller3f941882006-03-31 23:13:02 +1100712 put_u32(buf, len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000713 buffer_append(&active_state->output, buf, 4);
714 cp = buffer_append_space(&active_state->output,
715 buffer_len(&active_state->outgoing_packet));
716 cipher_crypt(&active_state->send_context, cp,
717 buffer_ptr(&active_state->outgoing_packet),
Damien Miller1d75abf2013-01-09 16:12:19 +1100718 buffer_len(&active_state->outgoing_packet), 0, 0);
Damien Miller95def091999-11-25 00:26:21 +1100719
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +1100721 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000722 buffer_dump(&active_state->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +1000724 active_state->p_send.packets++;
725 active_state->p_send.bytes += len +
726 buffer_len(&active_state->outgoing_packet);
727 buffer_clear(&active_state->outgoing_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
Damien Miller5428f641999-11-25 11:54:57 +1100729 /*
Damien Miller788f2122005-11-05 15:14:59 +1100730 * Note that the packet is now only buffered in output. It won't be
Damien Miller5428f641999-11-25 11:54:57 +1100731 * actually sent until packet_write_wait or packet_write_poll is
732 * called.
733 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734}
735
Ben Lindstromf6027d32002-03-22 01:42:04 +0000736void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000737set_newkeys(int mode)
738{
739 Enc *enc;
740 Mac *mac;
741 Comp *comp;
742 CipherContext *cc;
Damien Millera5539d22003-04-09 20:50:06 +1000743 u_int64_t *max_blocks;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000744 int crypt_type;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000745
Ben Lindstrom064496f2002-12-23 02:04:22 +0000746 debug2("set_newkeys: mode %d", mode);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000747
Damien Miller963f6b22002-02-19 15:21:23 +1100748 if (mode == MODE_OUT) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000749 cc = &active_state->send_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000750 crypt_type = CIPHER_ENCRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000751 active_state->p_send.packets = active_state->p_send.blocks = 0;
752 max_blocks = &active_state->max_blocks_out;
Damien Miller963f6b22002-02-19 15:21:23 +1100753 } else {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000754 cc = &active_state->receive_context;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755 crypt_type = CIPHER_DECRYPT;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000756 active_state->p_read.packets = active_state->p_read.blocks = 0;
757 max_blocks = &active_state->max_blocks_in;
Damien Miller963f6b22002-02-19 15:21:23 +1100758 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000759 if (active_state->newkeys[mode] != NULL) {
Ben Lindstrom064496f2002-12-23 02:04:22 +0000760 debug("set_newkeys: rekeying");
Damien Miller963f6b22002-02-19 15:21:23 +1100761 cipher_cleanup(cc);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000762 enc = &active_state->newkeys[mode]->enc;
763 mac = &active_state->newkeys[mode]->mac;
764 comp = &active_state->newkeys[mode]->comp;
Damien Millere45796f2007-06-11 14:01:42 +1000765 mac_clear(mac);
Damien Miller1d75abf2013-01-09 16:12:19 +1100766 memset(enc->iv, 0, enc->iv_len);
Darren Tucker302889a2012-10-05 10:42:53 +1000767 memset(enc->key, 0, enc->key_len);
768 memset(mac->key, 0, mac->key_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000769 free(enc->name);
770 free(enc->iv);
771 free(enc->key);
772 free(mac->name);
773 free(mac->key);
774 free(comp->name);
775 free(active_state->newkeys[mode]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000776 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000777 active_state->newkeys[mode] = kex_get_newkeys(mode);
778 if (active_state->newkeys[mode] == NULL)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000779 fatal("newkeys: no keys for mode %d", mode);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000780 enc = &active_state->newkeys[mode]->enc;
781 mac = &active_state->newkeys[mode]->mac;
782 comp = &active_state->newkeys[mode]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100783 if (cipher_authlen(enc->cipher) == 0 && mac_init(mac) == 0)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000784 mac->enabled = 1;
785 DBG(debug("cipher_init_context: %d", mode));
Damien Miller963f6b22002-02-19 15:21:23 +1100786 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
Damien Miller1d75abf2013-01-09 16:12:19 +1100787 enc->iv, enc->iv_len, crypt_type);
Ben Lindstromf6027d32002-03-22 01:42:04 +0000788 /* Deleting the keys does not gain extra security */
789 /* memset(enc->iv, 0, enc->block_size);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000790 memset(enc->key, 0, enc->key_len);
791 memset(mac->key, 0, mac->key_len); */
Damien Miller9786e6e2005-07-26 21:54:56 +1000792 if ((comp->type == COMP_ZLIB ||
Darren Tuckerf7288d72009-06-21 18:12:20 +1000793 (comp->type == COMP_DELAYED &&
794 active_state->after_authentication)) && comp->enabled == 0) {
Ben Lindstromfb50cdf2001-04-05 23:20:46 +0000795 packet_init_compression();
796 if (mode == MODE_OUT)
797 buffer_compress_init_send(6);
798 else
799 buffer_compress_init_recv();
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000800 comp->enabled = 1;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000801 }
Darren Tucker81a0b372003-07-14 17:31:06 +1000802 /*
803 * The 2^(blocksize*2) limit is too expensive for 3DES,
804 * blowfish, etc, so enforce a 1GB limit for small blocksizes.
805 */
806 if (enc->block_size >= 16)
807 *max_blocks = (u_int64_t)1 << (enc->block_size*2);
808 else
809 *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000810 if (active_state->rekey_limit)
811 *max_blocks = MIN(*max_blocks,
812 active_state->rekey_limit / enc->block_size);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000813}
814
Damien Miller5428f641999-11-25 11:54:57 +1100815/*
Damien Miller9786e6e2005-07-26 21:54:56 +1000816 * Delayed compression for SSH2 is enabled after authentication:
Darren Tuckerf676c572006-08-05 18:51:08 +1000817 * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
Damien Miller9786e6e2005-07-26 21:54:56 +1000818 * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
819 */
820static void
821packet_enable_delayed_compress(void)
822{
823 Comp *comp = NULL;
824 int mode;
825
826 /*
827 * Remember that we are past the authentication step, so rekeying
828 * with COMP_DELAYED will turn on compression immediately.
829 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000830 active_state->after_authentication = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +1000831 for (mode = 0; mode < MODE_MAX; mode++) {
Darren Tucker4aa665b2006-09-21 13:00:25 +1000832 /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000833 if (active_state->newkeys[mode] == NULL)
Darren Tucker4aa665b2006-09-21 13:00:25 +1000834 continue;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000835 comp = &active_state->newkeys[mode]->comp;
Damien Miller9786e6e2005-07-26 21:54:56 +1000836 if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
Damien Millerb5c01252005-08-12 22:10:28 +1000837 packet_init_compression();
Damien Miller9786e6e2005-07-26 21:54:56 +1000838 if (mode == MODE_OUT)
839 buffer_compress_init_send(6);
840 else
841 buffer_compress_init_recv();
842 comp->enabled = 1;
843 }
844 }
845}
846
847/*
Damien Miller33b13562000-04-04 14:38:59 +1000848 * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
849 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000850static void
Damien Millera5539d22003-04-09 20:50:06 +1000851packet_send2_wrapped(void)
Damien Miller33b13562000-04-04 14:38:59 +1000852{
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000853 u_char type, *cp, *macbuf = NULL;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100854 u_char padlen, pad = 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100855 u_int i, len, authlen = 0, aadlen = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000856 u_int32_t rnd = 0;
Damien Miller33b13562000-04-04 14:38:59 +1000857 Enc *enc = NULL;
858 Mac *mac = NULL;
859 Comp *comp = NULL;
860 int block_size;
861
Darren Tuckerf7288d72009-06-21 18:12:20 +1000862 if (active_state->newkeys[MODE_OUT] != NULL) {
863 enc = &active_state->newkeys[MODE_OUT]->enc;
864 mac = &active_state->newkeys[MODE_OUT]->mac;
865 comp = &active_state->newkeys[MODE_OUT]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100866 /* disable mac for authenticated encryption */
867 if ((authlen = cipher_authlen(enc->cipher)) != 0)
868 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +1000869 }
Damien Miller963f6b22002-02-19 15:21:23 +1100870 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +1100871 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +1000872
Darren Tuckerf7288d72009-06-21 18:12:20 +1000873 cp = buffer_ptr(&active_state->outgoing_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000874 type = cp[5];
Damien Miller33b13562000-04-04 14:38:59 +1000875
876#ifdef PACKET_DEBUG
877 fprintf(stderr, "plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000878 buffer_dump(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000879#endif
880
881 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000882 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000883 /* skip header, compress only payload */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000884 buffer_consume(&active_state->outgoing_packet, 5);
885 buffer_clear(&active_state->compression_buffer);
886 buffer_compress(&active_state->outgoing_packet,
887 &active_state->compression_buffer);
888 buffer_clear(&active_state->outgoing_packet);
889 buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5);
890 buffer_append(&active_state->outgoing_packet,
891 buffer_ptr(&active_state->compression_buffer),
892 buffer_len(&active_state->compression_buffer));
Damien Miller33b13562000-04-04 14:38:59 +1000893 DBG(debug("compression: raw %d compressed %d", len,
Darren Tuckerf7288d72009-06-21 18:12:20 +1000894 buffer_len(&active_state->outgoing_packet)));
Damien Miller33b13562000-04-04 14:38:59 +1000895 }
896
897 /* sizeof (packet_len + pad_len + payload) */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000898 len = buffer_len(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000899
900 /*
901 * calc size of padding, alloc space, get random data,
902 * minimum padding is 4 bytes
903 */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100904 len -= aadlen; /* packet length is not encrypted for EtM modes */
Damien Miller33b13562000-04-04 14:38:59 +1000905 padlen = block_size - (len % block_size);
906 if (padlen < 4)
907 padlen += block_size;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000908 if (active_state->extra_pad) {
Damien Miller9f643902001-11-12 11:02:52 +1100909 /* will wrap if extra_pad+padlen > 255 */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000910 active_state->extra_pad =
911 roundup(active_state->extra_pad, block_size);
912 pad = active_state->extra_pad -
913 ((len + padlen) % active_state->extra_pad);
Ben Lindstrom8b08d812002-03-26 02:09:41 +0000914 debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000915 pad, len, padlen, active_state->extra_pad);
Damien Miller9f643902001-11-12 11:02:52 +1100916 padlen += pad;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000917 active_state->extra_pad = 0;
Damien Miller9f643902001-11-12 11:02:52 +1100918 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000919 cp = buffer_append_space(&active_state->outgoing_packet, padlen);
920 if (enc && !active_state->send_context.plaintext) {
Damien Millere247cc42000-05-07 12:03:14 +1000921 /* random padding */
Damien Miller33b13562000-04-04 14:38:59 +1000922 for (i = 0; i < padlen; i++) {
923 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000924 rnd = arc4random();
925 cp[i] = rnd & 0xff;
926 rnd >>= 8;
Damien Miller33b13562000-04-04 14:38:59 +1000927 }
Damien Millere247cc42000-05-07 12:03:14 +1000928 } else {
929 /* clear padding */
930 memset(cp, 0, padlen);
Damien Miller33b13562000-04-04 14:38:59 +1000931 }
Damien Milleraf43a7a2012-12-12 10:46:31 +1100932 /* sizeof (packet_len + pad_len + payload + padding) */
933 len = buffer_len(&active_state->outgoing_packet);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000934 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100935 /* packet_length includes payload, padding and padding length field */
936 put_u32(cp, len - 4);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +0000937 cp[4] = padlen;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100938 DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
939 len, padlen, aadlen));
Damien Miller33b13562000-04-04 14:38:59 +1000940
941 /* compute MAC over seqnr and packet(length fields, payload, padding) */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100942 if (mac && mac->enabled && !mac->etm) {
Darren Tuckerf7288d72009-06-21 18:12:20 +1000943 macbuf = mac_compute(mac, active_state->p_send.seqnr,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100944 buffer_ptr(&active_state->outgoing_packet), len);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000945 DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr));
Damien Miller33b13562000-04-04 14:38:59 +1000946 }
947 /* encrypt packet and append to output buffer. */
Damien Miller1d75abf2013-01-09 16:12:19 +1100948 cp = buffer_append_space(&active_state->output, len + authlen);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000949 cipher_crypt(&active_state->send_context, cp,
950 buffer_ptr(&active_state->outgoing_packet),
Damien Miller1d75abf2013-01-09 16:12:19 +1100951 len - aadlen, aadlen, authlen);
Damien Miller33b13562000-04-04 14:38:59 +1000952 /* append unencrypted MAC */
Damien Milleraf43a7a2012-12-12 10:46:31 +1100953 if (mac && mac->enabled) {
954 if (mac->etm) {
955 /* EtM: compute mac over aadlen + cipher text */
956 macbuf = mac_compute(mac,
957 active_state->p_send.seqnr, cp, len);
958 DBG(debug("done calc MAC(EtM) out #%d",
959 active_state->p_send.seqnr));
960 }
Darren Tuckerf7288d72009-06-21 18:12:20 +1000961 buffer_append(&active_state->output, macbuf, mac->mac_len);
Damien Milleraf43a7a2012-12-12 10:46:31 +1100962 }
Damien Miller33b13562000-04-04 14:38:59 +1000963#ifdef PACKET_DEBUG
964 fprintf(stderr, "encrypted: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000965 buffer_dump(&active_state->output);
Damien Miller33b13562000-04-04 14:38:59 +1000966#endif
Damien Miller4af51302000-04-16 11:18:38 +1000967 /* increment sequence number for outgoing packets */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000968 if (++active_state->p_send.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +1000969 logit("outgoing seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +1000970 if (++active_state->p_send.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +1000971 if (!(datafellows & SSH_BUG_NOREKEY))
972 fatal("XXX too many packets with same key");
Damien Milleraf43a7a2012-12-12 10:46:31 +1100973 active_state->p_send.blocks += len / block_size;
974 active_state->p_send.bytes += len;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000975 buffer_clear(&active_state->outgoing_packet);
Damien Miller33b13562000-04-04 14:38:59 +1000976
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000977 if (type == SSH2_MSG_NEWKEYS)
978 set_newkeys(MODE_OUT);
Darren Tuckerf7288d72009-06-21 18:12:20 +1000979 else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +1000980 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +1000981}
982
Damien Millera5539d22003-04-09 20:50:06 +1000983static void
984packet_send2(void)
985{
Damien Millera5539d22003-04-09 20:50:06 +1000986 struct packet *p;
987 u_char type, *cp;
988
Darren Tuckerf7288d72009-06-21 18:12:20 +1000989 cp = buffer_ptr(&active_state->outgoing_packet);
Damien Millera5539d22003-04-09 20:50:06 +1000990 type = cp[5];
991
992 /* during rekeying we can only send key exchange messages */
Darren Tucker7b935c72009-06-21 18:59:36 +1000993 if (active_state->rekeying) {
Damien Miller1de2cfe2012-02-11 08:18:43 +1100994 if ((type < SSH2_MSG_TRANSPORT_MIN) ||
995 (type > SSH2_MSG_TRANSPORT_MAX) ||
996 (type == SSH2_MSG_SERVICE_REQUEST) ||
997 (type == SSH2_MSG_SERVICE_ACCEPT)) {
Damien Millera5539d22003-04-09 20:50:06 +1000998 debug("enqueue packet: %u", type);
999 p = xmalloc(sizeof(*p));
1000 p->type = type;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001001 memcpy(&p->payload, &active_state->outgoing_packet,
1002 sizeof(Buffer));
1003 buffer_init(&active_state->outgoing_packet);
1004 TAILQ_INSERT_TAIL(&active_state->outgoing, p, next);
Damien Millera5539d22003-04-09 20:50:06 +10001005 return;
1006 }
1007 }
1008
1009 /* rekeying starts with sending KEXINIT */
1010 if (type == SSH2_MSG_KEXINIT)
Darren Tucker7b935c72009-06-21 18:59:36 +10001011 active_state->rekeying = 1;
Damien Millera5539d22003-04-09 20:50:06 +10001012
1013 packet_send2_wrapped();
1014
1015 /* after a NEWKEYS message we can send the complete queue */
1016 if (type == SSH2_MSG_NEWKEYS) {
Darren Tucker7b935c72009-06-21 18:59:36 +10001017 active_state->rekeying = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001018 active_state->rekey_time = monotime();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001019 while ((p = TAILQ_FIRST(&active_state->outgoing))) {
Damien Millera5539d22003-04-09 20:50:06 +10001020 type = p->type;
1021 debug("dequeue packet: %u", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001022 buffer_free(&active_state->outgoing_packet);
1023 memcpy(&active_state->outgoing_packet, &p->payload,
Damien Millera5539d22003-04-09 20:50:06 +10001024 sizeof(Buffer));
Darren Tuckerf7288d72009-06-21 18:12:20 +10001025 TAILQ_REMOVE(&active_state->outgoing, p, next);
Darren Tuckera627d422013-06-02 07:31:17 +10001026 free(p);
Damien Millera5539d22003-04-09 20:50:06 +10001027 packet_send2_wrapped();
1028 }
1029 }
1030}
1031
Damien Miller33b13562000-04-04 14:38:59 +10001032void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001033packet_send(void)
Damien Miller33b13562000-04-04 14:38:59 +10001034{
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001035 if (compat20)
Damien Miller33b13562000-04-04 14:38:59 +10001036 packet_send2();
1037 else
1038 packet_send1();
1039 DBG(debug("packet_send done"));
1040}
1041
Damien Miller33b13562000-04-04 14:38:59 +10001042/*
Damien Miller5428f641999-11-25 11:54:57 +11001043 * Waits until a packet has been received, and returns its type. Note that
1044 * no other data is processed until this returns, so this function should not
1045 * be used during the interactive session.
1046 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047
1048int
Damien Millerdff50992002-01-22 23:16:32 +11001049packet_read_seqnr(u_int32_t *seqnr_p)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050{
Damien Millerce986542013-07-18 16:12:44 +10001051 int type, len, ret, cont, ms_remain = 0;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001052 fd_set *setp;
Damien Miller95def091999-11-25 00:26:21 +11001053 char buf[8192];
Darren Tucker3fc464e2008-06-13 06:42:45 +10001054 struct timeval timeout, start, *timeoutp = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055
Darren Tucker99bb7612008-06-13 22:02:50 +10001056 DBG(debug("packet_read()"));
1057
Darren Tuckerf7288d72009-06-21 18:12:20 +10001058 setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1,
1059 NFDBITS), sizeof(fd_mask));
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001060
Damien Miller95def091999-11-25 00:26:21 +11001061 /* Since we are blocking, ensure that all written packets have been sent. */
1062 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 /* Stay in the loop until we have received a complete packet. */
1065 for (;;) {
1066 /* Try to read a packet from the buffer. */
Damien Millerdff50992002-01-22 23:16:32 +11001067 type = packet_read_poll_seqnr(seqnr_p);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001068 if (!compat20 && (
Damien Millere247cc42000-05-07 12:03:14 +10001069 type == SSH_SMSG_SUCCESS
Damien Miller95def091999-11-25 00:26:21 +11001070 || type == SSH_SMSG_FAILURE
1071 || type == SSH_CMSG_EOF
Damien Millere247cc42000-05-07 12:03:14 +10001072 || type == SSH_CMSG_EXIT_CONFIRMATION))
Damien Miller48b03fc2002-01-22 23:11:40 +11001073 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001074 /* If we got a packet, return it. */
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001075 if (type != SSH_MSG_NONE) {
Darren Tuckera627d422013-06-02 07:31:17 +10001076 free(setp);
Damien Miller95def091999-11-25 00:26:21 +11001077 return type;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001078 }
Damien Miller5428f641999-11-25 11:54:57 +11001079 /*
1080 * Otherwise, wait for some data to arrive, add it to the
1081 * buffer, and try again.
1082 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001083 memset(setp, 0, howmany(active_state->connection_in + 1,
1084 NFDBITS) * sizeof(fd_mask));
1085 FD_SET(active_state->connection_in, setp);
Damien Miller5428f641999-11-25 11:54:57 +11001086
Darren Tuckerf7288d72009-06-21 18:12:20 +10001087 if (active_state->packet_timeout_ms > 0) {
1088 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001089 timeoutp = &timeout;
1090 }
Damien Miller95def091999-11-25 00:26:21 +11001091 /* Wait for some data to arrive. */
Darren Tucker3fc464e2008-06-13 06:42:45 +10001092 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001093 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001094 ms_to_timeval(&timeout, ms_remain);
1095 gettimeofday(&start, NULL);
1096 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001097 if ((ret = select(active_state->connection_in + 1, setp,
1098 NULL, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001099 break;
Damien Millerea437422009-10-02 11:49:03 +10001100 if (errno != EAGAIN && errno != EINTR &&
1101 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001102 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001103 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001104 continue;
1105 ms_subtract_diff(&start, &ms_remain);
1106 if (ms_remain <= 0) {
1107 ret = 0;
1108 break;
1109 }
1110 }
1111 if (ret == 0) {
1112 logit("Connection to %.200s timed out while "
1113 "waiting to read", get_remote_ipaddr());
1114 cleanup_exit(255);
1115 }
Damien Miller95def091999-11-25 00:26:21 +11001116 /* Read data from the socket. */
Darren Tuckerc5564e12009-06-21 18:53:53 +10001117 do {
1118 cont = 0;
1119 len = roaming_read(active_state->connection_in, buf,
1120 sizeof(buf), &cont);
1121 } while (len == 0 && cont);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001122 if (len == 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001123 logit("Connection closed by %.200s", get_remote_ipaddr());
Darren Tucker3e33cec2003-10-02 16:12:36 +10001124 cleanup_exit(255);
Damien Miller5e7c10e1999-12-16 13:18:04 +11001125 }
Damien Miller95def091999-11-25 00:26:21 +11001126 if (len < 0)
1127 fatal("Read from socket failed: %.100s", strerror(errno));
1128 /* Append it to the buffer. */
1129 packet_process_incoming(buf, len);
1130 }
1131 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132}
1133
Damien Miller278f9072001-12-21 15:00:19 +11001134int
Damien Millerdff50992002-01-22 23:16:32 +11001135packet_read(void)
Damien Miller278f9072001-12-21 15:00:19 +11001136{
Damien Millerdff50992002-01-22 23:16:32 +11001137 return packet_read_seqnr(NULL);
Damien Miller278f9072001-12-21 15:00:19 +11001138}
1139
Damien Miller5428f641999-11-25 11:54:57 +11001140/*
1141 * Waits until a packet has been received, verifies that its type matches
1142 * that given, and gives a fatal error and exits if there is a mismatch.
1143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
1145void
Damien Millerdff50992002-01-22 23:16:32 +11001146packet_read_expect(int expected_type)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147{
Damien Miller95def091999-11-25 00:26:21 +11001148 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
Damien Millerdff50992002-01-22 23:16:32 +11001150 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001151 if (type != expected_type)
1152 packet_disconnect("Protocol error: expected packet type %d, got %d",
Damien Miller33b13562000-04-04 14:38:59 +10001153 expected_type, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154}
1155
1156/* Checks if a full packet is available in the data received so far via
Damien Miller95def091999-11-25 00:26:21 +11001157 * packet_process_incoming. If so, reads the packet; otherwise returns
1158 * SSH_MSG_NONE. This does not wait for data from the connection.
1159 *
Damien Miller5ed3d572008-02-10 22:27:47 +11001160 * SSH_MSG_DISCONNECT is handled specially here. Also,
1161 * SSH_MSG_IGNORE messages are skipped by this function and are never returned
1162 * to higher levels.
Damien Miller95def091999-11-25 00:26:21 +11001163 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164
Ben Lindstrombba81212001-06-25 05:01:22 +00001165static int
Damien Millerdff50992002-01-22 23:16:32 +11001166packet_read_poll1(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001168 u_int len, padded_len;
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001169 u_char *cp, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001170 u_int checksum, stored_checksum;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171
Damien Miller95def091999-11-25 00:26:21 +11001172 /* Check if input size is less than minimum packet size. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001173 if (buffer_len(&active_state->input) < 4 + 8)
Damien Miller95def091999-11-25 00:26:21 +11001174 return SSH_MSG_NONE;
1175 /* Get length of incoming packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001176 cp = buffer_ptr(&active_state->input);
Damien Miller3f941882006-03-31 23:13:02 +11001177 len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001178 if (len < 1 + 2 + 2 || len > 256 * 1024)
Ben Lindstrom0cc2a472002-11-09 15:41:39 +00001179 packet_disconnect("Bad packet length %u.", len);
Damien Miller95def091999-11-25 00:26:21 +11001180 padded_len = (len + 8) & ~7;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181
Damien Miller95def091999-11-25 00:26:21 +11001182 /* Check if the packet has been entirely received. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001183 if (buffer_len(&active_state->input) < 4 + padded_len)
Damien Miller95def091999-11-25 00:26:21 +11001184 return SSH_MSG_NONE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185
Damien Miller95def091999-11-25 00:26:21 +11001186 /* The entire packet is in buffer. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187
Damien Miller95def091999-11-25 00:26:21 +11001188 /* Consume packet length. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001189 buffer_consume(&active_state->input, 4);
Damien Miller95def091999-11-25 00:26:21 +11001190
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001191 /*
1192 * Cryptographic attack detector for ssh
1193 * (C)1998 CORE-SDI, Buenos Aires Argentina
1194 * Ariel Futoransky(futo@core-sdi.com)
1195 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001196 if (!active_state->receive_context.plaintext) {
1197 switch (detect_attack(buffer_ptr(&active_state->input),
1198 padded_len)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +10001199 case DEATTACK_DETECTED:
1200 packet_disconnect("crc32 compensation attack: "
1201 "network attack detected");
1202 case DEATTACK_DOS_DETECTED:
1203 packet_disconnect("deattack denial of "
1204 "service detected");
1205 }
1206 }
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001207
1208 /* Decrypt data to incoming_packet. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001209 buffer_clear(&active_state->incoming_packet);
1210 cp = buffer_append_space(&active_state->incoming_packet, padded_len);
1211 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001212 buffer_ptr(&active_state->input), padded_len, 0, 0);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001213
Darren Tuckerf7288d72009-06-21 18:12:20 +10001214 buffer_consume(&active_state->input, padded_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215
1216#ifdef PACKET_DEBUG
Damien Miller95def091999-11-25 00:26:21 +11001217 fprintf(stderr, "read_poll plain: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001218 buffer_dump(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220
Damien Miller95def091999-11-25 00:26:21 +11001221 /* Compute packet checksum. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001222 checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet),
1223 buffer_len(&active_state->incoming_packet) - 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224
Damien Miller95def091999-11-25 00:26:21 +11001225 /* Skip padding. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001226 buffer_consume(&active_state->incoming_packet, 8 - len % 8);
Damien Millerfd7c9111999-11-08 16:15:55 +11001227
Damien Miller95def091999-11-25 00:26:21 +11001228 /* Test check bytes. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001229 if (len != buffer_len(&active_state->incoming_packet))
Damien Miller278f9072001-12-21 15:00:19 +11001230 packet_disconnect("packet_read_poll1: len %d != buffer_len %d.",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001231 len, buffer_len(&active_state->incoming_packet));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232
Darren Tuckerf7288d72009-06-21 18:12:20 +10001233 cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4;
Damien Miller3f941882006-03-31 23:13:02 +11001234 stored_checksum = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +11001235 if (checksum != stored_checksum)
1236 packet_disconnect("Corrupted check bytes on input.");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001237 buffer_consume_end(&active_state->incoming_packet, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238
Darren Tuckerf7288d72009-06-21 18:12:20 +10001239 if (active_state->packet_compression) {
1240 buffer_clear(&active_state->compression_buffer);
1241 buffer_uncompress(&active_state->incoming_packet,
1242 &active_state->compression_buffer);
1243 buffer_clear(&active_state->incoming_packet);
1244 buffer_append(&active_state->incoming_packet,
1245 buffer_ptr(&active_state->compression_buffer),
1246 buffer_len(&active_state->compression_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001247 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001248 active_state->p_read.packets++;
1249 active_state->p_read.bytes += padded_len + 4;
1250 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001251 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
1252 packet_disconnect("Invalid ssh1 packet type: %d", type);
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001253 return type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
Damien Miller95def091999-11-25 00:26:21 +11001255
Ben Lindstrombba81212001-06-25 05:01:22 +00001256static int
Damien Millerdff50992002-01-22 23:16:32 +11001257packet_read_poll2(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001258{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001259 u_int padlen, need;
Damien Milleraf43a7a2012-12-12 10:46:31 +11001260 u_char *macbuf = NULL, *cp, type;
Damien Miller1d75abf2013-01-09 16:12:19 +11001261 u_int maclen, authlen = 0, aadlen = 0, block_size;
Damien Miller33b13562000-04-04 14:38:59 +10001262 Enc *enc = NULL;
1263 Mac *mac = NULL;
1264 Comp *comp = NULL;
1265
Darren Tuckerf7288d72009-06-21 18:12:20 +10001266 if (active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001267 return SSH_MSG_NONE;
1268
Darren Tuckerf7288d72009-06-21 18:12:20 +10001269 if (active_state->newkeys[MODE_IN] != NULL) {
1270 enc = &active_state->newkeys[MODE_IN]->enc;
1271 mac = &active_state->newkeys[MODE_IN]->mac;
1272 comp = &active_state->newkeys[MODE_IN]->comp;
Damien Miller1d75abf2013-01-09 16:12:19 +11001273 /* disable mac for authenticated encryption */
1274 if ((authlen = cipher_authlen(enc->cipher)) != 0)
1275 mac = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10001276 }
1277 maclen = mac && mac->enabled ? mac->mac_len : 0;
Damien Miller963f6b22002-02-19 15:21:23 +11001278 block_size = enc ? enc->block_size : 8;
Damien Miller1d75abf2013-01-09 16:12:19 +11001279 aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
Damien Miller33b13562000-04-04 14:38:59 +10001280
Damien Milleraf43a7a2012-12-12 10:46:31 +11001281 if (aadlen && active_state->packlen == 0) {
1282 if (buffer_len(&active_state->input) < 4)
1283 return SSH_MSG_NONE;
1284 cp = buffer_ptr(&active_state->input);
1285 active_state->packlen = get_u32(cp);
1286 if (active_state->packlen < 1 + 4 ||
1287 active_state->packlen > PACKET_MAX_SIZE) {
1288#ifdef PACKET_DEBUG
1289 buffer_dump(&active_state->input);
1290#endif
1291 logit("Bad packet length %u.", active_state->packlen);
1292 packet_disconnect("Packet corrupt");
1293 }
Damien Miller8c05da32012-12-13 07:18:59 +11001294 buffer_clear(&active_state->incoming_packet);
Damien Milleraf43a7a2012-12-12 10:46:31 +11001295 } else if (active_state->packlen == 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001296 /*
1297 * check if input size is less than the cipher block size,
1298 * decrypt first block and extract length of incoming packet
1299 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001300 if (buffer_len(&active_state->input) < block_size)
Damien Miller33b13562000-04-04 14:38:59 +10001301 return SSH_MSG_NONE;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001302 buffer_clear(&active_state->incoming_packet);
1303 cp = buffer_append_space(&active_state->incoming_packet,
Damien Miller33b13562000-04-04 14:38:59 +10001304 block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001305 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001306 buffer_ptr(&active_state->input), block_size, 0, 0);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001307 cp = buffer_ptr(&active_state->incoming_packet);
1308 active_state->packlen = get_u32(cp);
1309 if (active_state->packlen < 1 + 4 ||
1310 active_state->packlen > PACKET_MAX_SIZE) {
Darren Tuckera8151da2003-09-22 21:06:46 +10001311#ifdef PACKET_DEBUG
Darren Tuckerf7288d72009-06-21 18:12:20 +10001312 buffer_dump(&active_state->incoming_packet);
Darren Tuckera8151da2003-09-22 21:06:46 +10001313#endif
Darren Tuckerf7288d72009-06-21 18:12:20 +10001314 logit("Bad packet length %u.", active_state->packlen);
1315 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001316 PACKET_MAX_SIZE);
1317 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001318 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001319 buffer_consume(&active_state->input, block_size);
Damien Miller33b13562000-04-04 14:38:59 +10001320 }
Damien Milleraf43a7a2012-12-12 10:46:31 +11001321 DBG(debug("input: packet len %u", active_state->packlen+4));
1322 if (aadlen) {
1323 /* only the payload is encrypted */
1324 need = active_state->packlen;
1325 } else {
1326 /*
1327 * the payload size and the payload are encrypted, but we
1328 * have a partial packet of block_size bytes
1329 */
1330 need = 4 + active_state->packlen - block_size;
1331 }
Damien Miller1d75abf2013-01-09 16:12:19 +11001332 DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
1333 " aadlen %d", block_size, need, maclen, authlen, aadlen));
Darren Tucker99d11a32008-12-01 21:40:48 +11001334 if (need % block_size != 0) {
1335 logit("padding error: need %d block %d mod %d",
Damien Miller33b13562000-04-04 14:38:59 +10001336 need, block_size, need % block_size);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001337 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001338 PACKET_MAX_SIZE - block_size);
1339 return SSH_MSG_NONE;
Darren Tucker99d11a32008-12-01 21:40:48 +11001340 }
Damien Miller33b13562000-04-04 14:38:59 +10001341 /*
1342 * check if the entire packet has been received and
Damien Milleraf43a7a2012-12-12 10:46:31 +11001343 * decrypt into incoming_packet:
1344 * 'aadlen' bytes are unencrypted, but authenticated.
Damien Miller1d75abf2013-01-09 16:12:19 +11001345 * 'need' bytes are encrypted, followed by either
1346 * 'authlen' bytes of authentication tag or
Damien Milleraf43a7a2012-12-12 10:46:31 +11001347 * 'maclen' bytes of message authentication code.
Damien Miller33b13562000-04-04 14:38:59 +10001348 */
Damien Miller1d75abf2013-01-09 16:12:19 +11001349 if (buffer_len(&active_state->input) < aadlen + need + authlen + maclen)
Damien Miller33b13562000-04-04 14:38:59 +10001350 return SSH_MSG_NONE;
1351#ifdef PACKET_DEBUG
1352 fprintf(stderr, "read_poll enc/full: ");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001353 buffer_dump(&active_state->input);
Damien Miller33b13562000-04-04 14:38:59 +10001354#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +11001355 /* EtM: compute mac over encrypted input */
1356 if (mac && mac->enabled && mac->etm)
1357 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1358 buffer_ptr(&active_state->input), aadlen + need);
1359 cp = buffer_append_space(&active_state->incoming_packet, aadlen + need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001360 cipher_crypt(&active_state->receive_context, cp,
Damien Miller1d75abf2013-01-09 16:12:19 +11001361 buffer_ptr(&active_state->input), need, aadlen, authlen);
1362 buffer_consume(&active_state->input, aadlen + need + authlen);
Damien Miller33b13562000-04-04 14:38:59 +10001363 /*
1364 * compute MAC over seqnr and packet,
1365 * increment sequence number for incoming packet
1366 */
Damien Miller4af51302000-04-16 11:18:38 +10001367 if (mac && mac->enabled) {
Damien Milleraf43a7a2012-12-12 10:46:31 +11001368 if (!mac->etm)
1369 macbuf = mac_compute(mac, active_state->p_read.seqnr,
1370 buffer_ptr(&active_state->incoming_packet),
1371 buffer_len(&active_state->incoming_packet));
Damien Millerea1651c2010-07-16 13:58:37 +10001372 if (timingsafe_bcmp(macbuf, buffer_ptr(&active_state->input),
Darren Tuckerf7288d72009-06-21 18:12:20 +10001373 mac->mac_len) != 0) {
Damien Miller13ae44c2009-01-28 16:38:41 +11001374 logit("Corrupted MAC on input.");
1375 if (need > PACKET_MAX_SIZE)
1376 fatal("internal error need %d", need);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001377 packet_start_discard(enc, mac, active_state->packlen,
Damien Miller13ae44c2009-01-28 16:38:41 +11001378 PACKET_MAX_SIZE - need);
1379 return SSH_MSG_NONE;
1380 }
1381
Darren Tuckerf7288d72009-06-21 18:12:20 +10001382 DBG(debug("MAC #%d ok", active_state->p_read.seqnr));
1383 buffer_consume(&active_state->input, mac->mac_len);
Damien Miller33b13562000-04-04 14:38:59 +10001384 }
Damien Miller13ae44c2009-01-28 16:38:41 +11001385 /* XXX now it's safe to use fatal/packet_disconnect */
Damien Miller278f9072001-12-21 15:00:19 +11001386 if (seqnr_p != NULL)
Darren Tuckerf7288d72009-06-21 18:12:20 +10001387 *seqnr_p = active_state->p_read.seqnr;
1388 if (++active_state->p_read.seqnr == 0)
Damien Miller996acd22003-04-09 20:59:48 +10001389 logit("incoming seqnr wraps around");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001390 if (++active_state->p_read.packets == 0)
Damien Millera5539d22003-04-09 20:50:06 +10001391 if (!(datafellows & SSH_BUG_NOREKEY))
1392 fatal("XXX too many packets with same key");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001393 active_state->p_read.blocks += (active_state->packlen + 4) / block_size;
1394 active_state->p_read.bytes += active_state->packlen + 4;
Damien Miller33b13562000-04-04 14:38:59 +10001395
1396 /* get padlen */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001397 cp = buffer_ptr(&active_state->incoming_packet);
Ben Lindstrom4a7714a2002-02-26 18:04:38 +00001398 padlen = cp[4];
Damien Miller33b13562000-04-04 14:38:59 +10001399 DBG(debug("input: padlen %d", padlen));
1400 if (padlen < 4)
1401 packet_disconnect("Corrupted padlen %d on input.", padlen);
1402
1403 /* skip packet size + padlen, discard padding */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001404 buffer_consume(&active_state->incoming_packet, 4 + 1);
1405 buffer_consume_end(&active_state->incoming_packet, padlen);
Damien Miller33b13562000-04-04 14:38:59 +10001406
Darren Tuckerf7288d72009-06-21 18:12:20 +10001407 DBG(debug("input: len before de-compress %d",
1408 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001409 if (comp && comp->enabled) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001410 buffer_clear(&active_state->compression_buffer);
1411 buffer_uncompress(&active_state->incoming_packet,
1412 &active_state->compression_buffer);
1413 buffer_clear(&active_state->incoming_packet);
1414 buffer_append(&active_state->incoming_packet,
1415 buffer_ptr(&active_state->compression_buffer),
1416 buffer_len(&active_state->compression_buffer));
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001417 DBG(debug("input: len after de-compress %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001418 buffer_len(&active_state->incoming_packet)));
Damien Miller33b13562000-04-04 14:38:59 +10001419 }
1420 /*
1421 * get packet type, implies consume.
1422 * return length of payload (without type field)
1423 */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001424 type = buffer_get_char(&active_state->incoming_packet);
Darren Tuckerb2694f02004-11-05 20:27:54 +11001425 if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN)
1426 packet_disconnect("Invalid ssh2 packet type: %d", type);
Ben Lindstrom2d90e002001-04-04 02:00:54 +00001427 if (type == SSH2_MSG_NEWKEYS)
1428 set_newkeys(MODE_IN);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001429 else if (type == SSH2_MSG_USERAUTH_SUCCESS &&
1430 !active_state->server_side)
Damien Miller9786e6e2005-07-26 21:54:56 +10001431 packet_enable_delayed_compress();
Damien Miller33b13562000-04-04 14:38:59 +10001432#ifdef PACKET_DEBUG
Ben Lindstrom204e4882001-03-05 06:47:00 +00001433 fprintf(stderr, "read/plain[%d]:\r\n", type);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001434 buffer_dump(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001435#endif
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001436 /* reset for next packet */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001437 active_state->packlen = 0;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001438 return type;
Damien Miller33b13562000-04-04 14:38:59 +10001439}
1440
1441int
Damien Millerdff50992002-01-22 23:16:32 +11001442packet_read_poll_seqnr(u_int32_t *seqnr_p)
Damien Miller33b13562000-04-04 14:38:59 +10001443{
Ben Lindstrom3f584742002-06-23 21:49:25 +00001444 u_int reason, seqnr;
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001445 u_char type;
Damien Miller33b13562000-04-04 14:38:59 +10001446 char *msg;
Damien Miller33b13562000-04-04 14:38:59 +10001447
Ben Lindstrom80c6d772001-06-05 21:09:18 +00001448 for (;;) {
1449 if (compat20) {
Damien Millerdff50992002-01-22 23:16:32 +11001450 type = packet_read_poll2(seqnr_p);
Darren Tucker136e56f2008-06-08 12:49:30 +10001451 if (type) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001452 active_state->keep_alive_timeouts = 0;
Damien Miller33b13562000-04-04 14:38:59 +10001453 DBG(debug("received packet type %d", type));
Darren Tucker136e56f2008-06-08 12:49:30 +10001454 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001455 switch (type) {
Damien Miller5ed3d572008-02-10 22:27:47 +11001456 case SSH2_MSG_IGNORE:
Damien Miller58226f62008-03-07 18:33:30 +11001457 debug3("Received SSH2_MSG_IGNORE");
Damien Miller5ed3d572008-02-10 22:27:47 +11001458 break;
Damien Miller33b13562000-04-04 14:38:59 +10001459 case SSH2_MSG_DEBUG:
1460 packet_get_char();
1461 msg = packet_get_string(NULL);
1462 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001463 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001464 msg = packet_get_string(NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001465 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001466 break;
1467 case SSH2_MSG_DISCONNECT:
1468 reason = packet_get_int();
1469 msg = packet_get_string(NULL);
Damien Millerd5edefd2013-04-23 15:21:39 +10001470 /* Ignore normal client exit notifications */
1471 do_log2(active_state->server_side &&
1472 reason == SSH2_DISCONNECT_BY_APPLICATION ?
1473 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1474 "Received disconnect from %s: %u: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001475 get_remote_ipaddr(), reason, msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001476 free(msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001477 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001478 break;
Damien Miller659811f2002-01-22 23:23:11 +11001479 case SSH2_MSG_UNIMPLEMENTED:
1480 seqnr = packet_get_int();
Ben Lindstrom3f584742002-06-23 21:49:25 +00001481 debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
1482 seqnr);
Damien Miller5ed3d572008-02-10 22:27:47 +11001483 break;
Damien Miller33b13562000-04-04 14:38:59 +10001484 default:
1485 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001486 }
Damien Miller33b13562000-04-04 14:38:59 +10001487 } else {
Damien Millerdff50992002-01-22 23:16:32 +11001488 type = packet_read_poll1();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001489 switch (type) {
Damien Millerce986542013-07-18 16:12:44 +10001490 case SSH_MSG_NONE:
1491 return SSH_MSG_NONE;
Damien Miller33b13562000-04-04 14:38:59 +10001492 case SSH_MSG_IGNORE:
1493 break;
1494 case SSH_MSG_DEBUG:
1495 msg = packet_get_string(NULL);
1496 debug("Remote: %.900s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001497 free(msg);
Damien Miller33b13562000-04-04 14:38:59 +10001498 break;
1499 case SSH_MSG_DISCONNECT:
1500 msg = packet_get_string(NULL);
Damien Miller894926e2013-02-12 11:03:58 +11001501 error("Received disconnect from %s: %.400s",
Ben Lindstrom3f584742002-06-23 21:49:25 +00001502 get_remote_ipaddr(), msg);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001503 cleanup_exit(255);
Damien Miller33b13562000-04-04 14:38:59 +10001504 break;
1505 default:
Damien Millerce986542013-07-18 16:12:44 +10001506 DBG(debug("received packet type %d", type));
Damien Miller33b13562000-04-04 14:38:59 +10001507 return type;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001508 }
Damien Miller33b13562000-04-04 14:38:59 +10001509 }
1510 }
1511}
1512
Damien Miller5428f641999-11-25 11:54:57 +11001513/*
1514 * Buffers the given amount of input characters. This is intended to be used
1515 * together with packet_read_poll.
1516 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
1518void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001519packet_process_incoming(const char *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001521 if (active_state->packet_discard) {
1522 active_state->keep_alive_timeouts = 0; /* ?? */
1523 if (len >= active_state->packet_discard)
Damien Miller13ae44c2009-01-28 16:38:41 +11001524 packet_stop_discard();
Darren Tuckerf7288d72009-06-21 18:12:20 +10001525 active_state->packet_discard -= len;
Damien Miller13ae44c2009-01-28 16:38:41 +11001526 return;
1527 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001528 buffer_append(&active_state->input, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529}
1530
1531/* Returns a character from the packet. */
1532
Ben Lindstrom46c16222000-12-22 01:43:59 +00001533u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001534packet_get_char(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001535{
Damien Miller95def091999-11-25 00:26:21 +11001536 char ch;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001537
Darren Tuckerf7288d72009-06-21 18:12:20 +10001538 buffer_get(&active_state->incoming_packet, &ch, 1);
Ben Lindstrom46c16222000-12-22 01:43:59 +00001539 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540}
1541
1542/* Returns an integer from the packet data. */
1543
Ben Lindstrom46c16222000-12-22 01:43:59 +00001544u_int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001545packet_get_int(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001547 return buffer_get_int(&active_state->incoming_packet);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001548}
1549
Darren Tucker761c3892009-06-21 18:16:26 +10001550/* Returns an 64 bit integer from the packet data. */
1551
1552u_int64_t
1553packet_get_int64(void)
1554{
1555 return buffer_get_int64(&active_state->incoming_packet);
1556}
1557
Damien Miller5428f641999-11-25 11:54:57 +11001558/*
1559 * Returns an arbitrary precision integer from the packet data. The integer
1560 * must have been initialized before this call.
1561 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562
1563void
Damien Millerd432ccf2002-01-22 23:14:44 +11001564packet_get_bignum(BIGNUM * value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001566 buffer_get_bignum(&active_state->incoming_packet, value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567}
1568
Damien Miller33b13562000-04-04 14:38:59 +10001569void
Damien Millerd432ccf2002-01-22 23:14:44 +11001570packet_get_bignum2(BIGNUM * value)
Damien Miller33b13562000-04-04 14:38:59 +10001571{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001572 buffer_get_bignum2(&active_state->incoming_packet, value);
Damien Miller33b13562000-04-04 14:38:59 +10001573}
1574
Damien Miller6af914a2010-09-10 11:39:26 +10001575#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001576void
1577packet_get_ecpoint(const EC_GROUP *curve, EC_POINT *point)
1578{
1579 buffer_get_ecpoint(&active_state->incoming_packet, curve, point);
1580}
Damien Miller6af914a2010-09-10 11:39:26 +10001581#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001582
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001583void *
Damien Millereccb9de2005-06-17 12:59:34 +10001584packet_get_raw(u_int *length_ptr)
Damien Miller33b13562000-04-04 14:38:59 +10001585{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001586 u_int bytes = buffer_len(&active_state->incoming_packet);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001587
Damien Miller33b13562000-04-04 14:38:59 +10001588 if (length_ptr != NULL)
1589 *length_ptr = bytes;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001590 return buffer_ptr(&active_state->incoming_packet);
Damien Miller33b13562000-04-04 14:38:59 +10001591}
1592
Damien Miller4af51302000-04-16 11:18:38 +10001593int
1594packet_remaining(void)
1595{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001596 return buffer_len(&active_state->incoming_packet);
Damien Miller4af51302000-04-16 11:18:38 +10001597}
1598
Damien Miller5428f641999-11-25 11:54:57 +11001599/*
1600 * Returns a string from the packet data. The string is allocated using
1601 * xmalloc; it is the responsibility of the calling program to free it when
1602 * no longer needed. The length_ptr argument may be NULL, or point to an
1603 * integer into which the length of the string is stored.
1604 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001606void *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001607packet_get_string(u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001609 return buffer_get_string(&active_state->incoming_packet, length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001610}
1611
Damien Millerdb255ca2008-05-19 14:59:37 +10001612void *
1613packet_get_string_ptr(u_int *length_ptr)
1614{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001615 return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr);
Damien Millerdb255ca2008-05-19 14:59:37 +10001616}
1617
Damien Millerda108ec2010-08-31 22:36:39 +10001618/* Ensures the returned string has no embedded \0 characters in it. */
1619char *
1620packet_get_cstring(u_int *length_ptr)
1621{
1622 return buffer_get_cstring(&active_state->incoming_packet, length_ptr);
1623}
1624
Damien Miller5428f641999-11-25 11:54:57 +11001625/*
1626 * Sends a diagnostic message from the server to the client. This message
1627 * can be sent at any time (but not while constructing another message). The
1628 * message is printed immediately, but only if the client is being executed
1629 * in verbose mode. These messages are primarily intended to ease debugging
1630 * authentication problems. The length of the formatted message must not
1631 * exceed 1024 bytes. This will automatically call packet_write_wait.
1632 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001633
1634void
Damien Miller95def091999-11-25 00:26:21 +11001635packet_send_debug(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001636{
Damien Miller95def091999-11-25 00:26:21 +11001637 char buf[1024];
1638 va_list args;
1639
Ben Lindstroma14ee472000-12-07 01:24:58 +00001640 if (compat20 && (datafellows & SSH_BUG_DEBUG))
1641 return;
1642
Damien Miller95def091999-11-25 00:26:21 +11001643 va_start(args, fmt);
1644 vsnprintf(buf, sizeof(buf), fmt, args);
1645 va_end(args);
1646
Damien Miller7c8af4f2000-05-01 08:24:07 +10001647 if (compat20) {
1648 packet_start(SSH2_MSG_DEBUG);
1649 packet_put_char(0); /* bool: always display */
1650 packet_put_cstring(buf);
1651 packet_put_cstring("");
1652 } else {
1653 packet_start(SSH_MSG_DEBUG);
1654 packet_put_cstring(buf);
1655 }
Damien Miller95def091999-11-25 00:26:21 +11001656 packet_send();
1657 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658}
1659
Damien Miller5428f641999-11-25 11:54:57 +11001660/*
1661 * Logs the error plus constructs and sends a disconnect packet, closes the
1662 * connection, and exits. This function never returns. The error message
1663 * should not contain a newline. The length of the formatted message must
1664 * not exceed 1024 bytes.
1665 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001666
1667void
Damien Miller95def091999-11-25 00:26:21 +11001668packet_disconnect(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001669{
Damien Miller95def091999-11-25 00:26:21 +11001670 char buf[1024];
1671 va_list args;
1672 static int disconnecting = 0;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001673
Damien Miller95def091999-11-25 00:26:21 +11001674 if (disconnecting) /* Guard against recursive invocations. */
1675 fatal("packet_disconnect called recursively.");
1676 disconnecting = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001677
Damien Miller5428f641999-11-25 11:54:57 +11001678 /*
1679 * Format the message. Note that the caller must make sure the
1680 * message is of limited size.
1681 */
Damien Miller95def091999-11-25 00:26:21 +11001682 va_start(args, fmt);
1683 vsnprintf(buf, sizeof(buf), fmt, args);
1684 va_end(args);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001686 /* Display the error locally */
Damien Miller996acd22003-04-09 20:59:48 +10001687 logit("Disconnecting: %.100s", buf);
Ben Lindstrom9bda7ae2002-11-09 15:46:24 +00001688
Damien Miller95def091999-11-25 00:26:21 +11001689 /* Send the disconnect message to the other side, and wait for it to get sent. */
Damien Miller33b13562000-04-04 14:38:59 +10001690 if (compat20) {
1691 packet_start(SSH2_MSG_DISCONNECT);
1692 packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR);
1693 packet_put_cstring(buf);
1694 packet_put_cstring("");
1695 } else {
1696 packet_start(SSH_MSG_DISCONNECT);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001697 packet_put_cstring(buf);
Damien Miller33b13562000-04-04 14:38:59 +10001698 }
Damien Miller95def091999-11-25 00:26:21 +11001699 packet_send();
1700 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001701
Damien Miller95def091999-11-25 00:26:21 +11001702 /* Stop listening for connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +00001703 channel_close_all();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001704
Damien Miller95def091999-11-25 00:26:21 +11001705 /* Close the connection. */
1706 packet_close();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001707 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001708}
1709
Damien Miller5428f641999-11-25 11:54:57 +11001710/* Checks if there is any buffered output, and tries to write some of the output. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001711
1712void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001713packet_write_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001715 int len = buffer_len(&active_state->output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001716 int cont;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001717
Damien Miller95def091999-11-25 00:26:21 +11001718 if (len > 0) {
Darren Tuckerc5564e12009-06-21 18:53:53 +10001719 cont = 0;
1720 len = roaming_write(active_state->connection_out,
1721 buffer_ptr(&active_state->output), len, &cont);
Damien Millerd874fa52008-07-05 09:40:56 +10001722 if (len == -1) {
Damien Millerea437422009-10-02 11:49:03 +10001723 if (errno == EINTR || errno == EAGAIN ||
1724 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001725 return;
Damien Millerd874fa52008-07-05 09:40:56 +10001726 fatal("Write failed: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001727 }
Darren Tuckerc5564e12009-06-21 18:53:53 +10001728 if (len == 0 && !cont)
Damien Millerd874fa52008-07-05 09:40:56 +10001729 fatal("Write connection closed");
Darren Tuckerf7288d72009-06-21 18:12:20 +10001730 buffer_consume(&active_state->output, len);
Damien Miller95def091999-11-25 00:26:21 +11001731 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001732}
1733
Damien Miller5428f641999-11-25 11:54:57 +11001734/*
1735 * Calls packet_write_poll repeatedly until all pending output data has been
1736 * written.
1737 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
1739void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001740packet_write_wait(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741{
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001742 fd_set *setp;
Damien Millerce986542013-07-18 16:12:44 +10001743 int ret, ms_remain = 0;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001744 struct timeval start, timeout, *timeoutp = NULL;
Ben Lindstromcb978aa2001-03-05 07:07:49 +00001745
Darren Tuckerf7288d72009-06-21 18:12:20 +10001746 setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1,
1747 NFDBITS), sizeof(fd_mask));
Damien Miller95def091999-11-25 00:26:21 +11001748 packet_write_poll();
1749 while (packet_have_data_to_write()) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001750 memset(setp, 0, howmany(active_state->connection_out + 1,
1751 NFDBITS) * sizeof(fd_mask));
1752 FD_SET(active_state->connection_out, setp);
Darren Tucker3fc464e2008-06-13 06:42:45 +10001753
Darren Tuckerf7288d72009-06-21 18:12:20 +10001754 if (active_state->packet_timeout_ms > 0) {
1755 ms_remain = active_state->packet_timeout_ms;
Darren Tucker3fc464e2008-06-13 06:42:45 +10001756 timeoutp = &timeout;
1757 }
1758 for (;;) {
Darren Tuckerf7288d72009-06-21 18:12:20 +10001759 if (active_state->packet_timeout_ms != -1) {
Darren Tucker3fc464e2008-06-13 06:42:45 +10001760 ms_to_timeval(&timeout, ms_remain);
1761 gettimeofday(&start, NULL);
1762 }
Darren Tuckerf7288d72009-06-21 18:12:20 +10001763 if ((ret = select(active_state->connection_out + 1,
1764 NULL, setp, NULL, timeoutp)) >= 0)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001765 break;
Damien Millerea437422009-10-02 11:49:03 +10001766 if (errno != EAGAIN && errno != EINTR &&
1767 errno != EWOULDBLOCK)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001768 break;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001769 if (active_state->packet_timeout_ms == -1)
Darren Tucker3fc464e2008-06-13 06:42:45 +10001770 continue;
1771 ms_subtract_diff(&start, &ms_remain);
1772 if (ms_remain <= 0) {
1773 ret = 0;
1774 break;
1775 }
1776 }
1777 if (ret == 0) {
1778 logit("Connection to %.200s timed out while "
1779 "waiting to write", get_remote_ipaddr());
1780 cleanup_exit(255);
1781 }
Damien Miller95def091999-11-25 00:26:21 +11001782 packet_write_poll();
1783 }
Darren Tuckera627d422013-06-02 07:31:17 +10001784 free(setp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001785}
1786
1787/* Returns true if there is buffered data to write to the connection. */
1788
1789int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001790packet_have_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001791{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001792 return buffer_len(&active_state->output) != 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001793}
1794
1795/* Returns true if there is not too much data to write to the connection. */
1796
1797int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001798packet_not_very_much_data_to_write(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001800 if (active_state->interactive_mode)
1801 return buffer_len(&active_state->output) < 16384;
Damien Miller95def091999-11-25 00:26:21 +11001802 else
Darren Tuckerf7288d72009-06-21 18:12:20 +10001803 return buffer_len(&active_state->output) < 128 * 1024;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804}
1805
Ben Lindstromfaa1ea82002-12-23 02:42:52 +00001806static void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001807packet_set_tos(int tos)
Ben Lindstroma7433982002-12-23 02:41:41 +00001808{
Damien Millerd2ac5d72011-05-15 08:43:13 +10001809#ifndef IP_TOS_IS_BROKEN
1810 if (!packet_connection_is_on_socket())
Ben Lindstroma7433982002-12-23 02:41:41 +00001811 return;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001812 switch (packet_connection_af()) {
1813# ifdef IP_TOS
1814 case AF_INET:
1815 debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1816 if (setsockopt(active_state->connection_in,
1817 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
1818 error("setsockopt IP_TOS %d: %.100s:",
1819 tos, strerror(errno));
1820 break;
1821# endif /* IP_TOS */
1822# ifdef IPV6_TCLASS
1823 case AF_INET6:
1824 debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1825 if (setsockopt(active_state->connection_in,
1826 IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
1827 error("setsockopt IPV6_TCLASS %d: %.100s:",
1828 tos, strerror(errno));
1829 break;
Damien Millerd2ac5d72011-05-15 08:43:13 +10001830# endif /* IPV6_TCLASS */
Damien Miller23f425b2011-05-15 08:58:15 +10001831 }
Damien Millerd2ac5d72011-05-15 08:43:13 +10001832#endif /* IP_TOS_IS_BROKEN */
Damien Miller5924ceb2003-11-22 15:02:42 +11001833}
Ben Lindstroma7433982002-12-23 02:41:41 +00001834
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001835/* Informs that the current session is interactive. Sets IP flags for that. */
1836
1837void
Damien Miller0dac6fb2010-11-20 15:19:38 +11001838packet_set_interactive(int interactive, int qos_interactive, int qos_bulk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001839{
Darren Tucker7b935c72009-06-21 18:59:36 +10001840 if (active_state->set_interactive_called)
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001841 return;
Darren Tucker7b935c72009-06-21 18:59:36 +10001842 active_state->set_interactive_called = 1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001843
Damien Miller95def091999-11-25 00:26:21 +11001844 /* Record that we are in interactive mode. */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001845 active_state->interactive_mode = interactive;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846
Damien Miller34132e52000-01-14 15:45:46 +11001847 /* Only set socket options if using a socket. */
1848 if (!packet_connection_is_on_socket())
Ben Lindstrom93b6b772003-04-27 17:55:33 +00001849 return;
Darren Tuckerf7288d72009-06-21 18:12:20 +10001850 set_nodelay(active_state->connection_in);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001851 packet_set_tos(interactive ? qos_interactive : qos_bulk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852}
1853
1854/* Returns true if the current connection is interactive. */
1855
1856int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001857packet_is_interactive(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001858{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001859 return active_state->interactive_mode;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860}
Damien Miller6162d121999-11-21 13:23:52 +11001861
Darren Tucker1f8311c2004-05-13 16:39:33 +10001862int
Darren Tucker502d3842003-06-28 12:38:01 +10001863packet_set_maxsize(u_int s)
Damien Miller6162d121999-11-21 13:23:52 +11001864{
Darren Tucker7b935c72009-06-21 18:59:36 +10001865 if (active_state->set_maxsize_called) {
Damien Miller996acd22003-04-09 20:59:48 +10001866 logit("packet_set_maxsize: called twice: old %d new %d",
Darren Tuckerf7288d72009-06-21 18:12:20 +10001867 active_state->max_packet_size, s);
Damien Miller95def091999-11-25 00:26:21 +11001868 return -1;
1869 }
1870 if (s < 4 * 1024 || s > 1024 * 1024) {
Damien Miller996acd22003-04-09 20:59:48 +10001871 logit("packet_set_maxsize: bad size %d", s);
Damien Miller95def091999-11-25 00:26:21 +11001872 return -1;
1873 }
Darren Tucker7b935c72009-06-21 18:59:36 +10001874 active_state->set_maxsize_called = 1;
Ben Lindstrom16d45b32001-06-13 04:39:18 +00001875 debug("packet_set_maxsize: setting to %d", s);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001876 active_state->max_packet_size = s;
Damien Miller95def091999-11-25 00:26:21 +11001877 return s;
Damien Miller6162d121999-11-21 13:23:52 +11001878}
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001879
Darren Tuckerf7288d72009-06-21 18:12:20 +10001880int
1881packet_inc_alive_timeouts(void)
1882{
1883 return ++active_state->keep_alive_timeouts;
1884}
1885
1886void
1887packet_set_alive_timeouts(int ka)
1888{
1889 active_state->keep_alive_timeouts = ka;
1890}
1891
1892u_int
1893packet_get_maxsize(void)
1894{
1895 return active_state->max_packet_size;
1896}
1897
Damien Miller9f643902001-11-12 11:02:52 +11001898/* roundup current message to pad bytes */
1899void
1900packet_add_padding(u_char pad)
1901{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001902 active_state->extra_pad = pad;
Damien Miller9f643902001-11-12 11:02:52 +11001903}
1904
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001905/*
1906 * 9.2. Ignored Data Message
Ben Lindstroma3700052001-04-05 23:26:32 +00001907 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001908 * byte SSH_MSG_IGNORE
1909 * string data
Ben Lindstroma3700052001-04-05 23:26:32 +00001910 *
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001911 * All implementations MUST understand (and ignore) this message at any
1912 * time (after receiving the protocol version). No implementation is
1913 * required to send them. This message can be used as an additional
1914 * protection measure against advanced traffic analysis techniques.
1915 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001916void
1917packet_send_ignore(int nbytes)
1918{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001919 u_int32_t rnd = 0;
Ben Lindstrome229b252001-03-05 06:28:06 +00001920 int i;
1921
1922 packet_start(compat20 ? SSH2_MSG_IGNORE : SSH_MSG_IGNORE);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001923 packet_put_int(nbytes);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001924 for (i = 0; i < nbytes; i++) {
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001925 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001926 rnd = arc4random();
Damien Miller8ba29fe2006-03-26 14:25:19 +11001927 packet_put_char((u_char)rnd & 0xff);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001928 rnd >>= 8;
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001929 }
1930}
Damien Millera5539d22003-04-09 20:50:06 +10001931
Darren Tucker1f8311c2004-05-13 16:39:33 +10001932#define MAX_PACKETS (1U<<31)
Damien Millera5539d22003-04-09 20:50:06 +10001933int
1934packet_need_rekeying(void)
1935{
1936 if (datafellows & SSH_BUG_NOREKEY)
1937 return 0;
1938 return
Darren Tuckerf7288d72009-06-21 18:12:20 +10001939 (active_state->p_send.packets > MAX_PACKETS) ||
1940 (active_state->p_read.packets > MAX_PACKETS) ||
1941 (active_state->max_blocks_out &&
1942 (active_state->p_send.blocks > active_state->max_blocks_out)) ||
1943 (active_state->max_blocks_in &&
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001944 (active_state->p_read.blocks > active_state->max_blocks_in)) ||
1945 (active_state->rekey_interval != 0 && active_state->rekey_time +
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001946 active_state->rekey_interval <= monotime());
Damien Millera5539d22003-04-09 20:50:06 +10001947}
1948
1949void
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001950packet_set_rekey_limits(u_int32_t bytes, time_t seconds)
Damien Millera5539d22003-04-09 20:50:06 +10001951{
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001952 debug3("rekey after %lld bytes, %d seconds", (long long)bytes,
1953 (int)seconds);
Darren Tuckerf7288d72009-06-21 18:12:20 +10001954 active_state->rekey_limit = bytes;
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001955 active_state->rekey_interval = seconds;
1956 /*
1957 * We set the time here so that in post-auth privsep slave we count
1958 * from the completion of the authentication.
1959 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001960 active_state->rekey_time = monotime();
Darren Tuckerc53c2af2013-05-16 20:28:16 +10001961}
1962
1963time_t
1964packet_get_rekey_timeout(void)
1965{
1966 time_t seconds;
1967
1968 seconds = active_state->rekey_time + active_state->rekey_interval -
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001969 monotime();
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001970 return (seconds <= 0 ? 1 : seconds);
Damien Millera5539d22003-04-09 20:50:06 +10001971}
Damien Miller9786e6e2005-07-26 21:54:56 +10001972
1973void
1974packet_set_server(void)
1975{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001976 active_state->server_side = 1;
Damien Miller9786e6e2005-07-26 21:54:56 +10001977}
1978
1979void
1980packet_set_authenticated(void)
1981{
Darren Tuckerf7288d72009-06-21 18:12:20 +10001982 active_state->after_authentication = 1;
1983}
1984
1985void *
1986packet_get_input(void)
1987{
1988 return (void *)&active_state->input;
1989}
1990
1991void *
1992packet_get_output(void)
1993{
1994 return (void *)&active_state->output;
1995}
1996
1997void *
1998packet_get_newkeys(int mode)
1999{
2000 return (void *)active_state->newkeys[mode];
Damien Miller9786e6e2005-07-26 21:54:56 +10002001}
Darren Tuckere841eb02009-07-06 07:11:13 +10002002
2003/*
2004 * Save the state for the real connection, and use a separate state when
2005 * resuming a suspended connection.
2006 */
2007void
2008packet_backup_state(void)
2009{
2010 struct session_state *tmp;
2011
2012 close(active_state->connection_in);
2013 active_state->connection_in = -1;
2014 close(active_state->connection_out);
2015 active_state->connection_out = -1;
2016 if (backup_state)
2017 tmp = backup_state;
2018 else
2019 tmp = alloc_session_state();
2020 backup_state = active_state;
2021 active_state = tmp;
2022}
2023
2024/*
2025 * Swap in the old state when resuming a connecion.
2026 */
2027void
2028packet_restore_state(void)
2029{
2030 struct session_state *tmp;
2031 void *buf;
2032 u_int len;
2033
2034 tmp = backup_state;
2035 backup_state = active_state;
2036 active_state = tmp;
2037 active_state->connection_in = backup_state->connection_in;
2038 backup_state->connection_in = -1;
2039 active_state->connection_out = backup_state->connection_out;
2040 backup_state->connection_out = -1;
2041 len = buffer_len(&backup_state->input);
2042 if (len > 0) {
2043 buf = buffer_ptr(&backup_state->input);
2044 buffer_append(&active_state->input, buf, len);
2045 buffer_clear(&backup_state->input);
2046 add_recv_bytes(len);
2047 }
2048}