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