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. |
| 14 | * |
| 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 18 | RCSID("$Id: packet.c,v 1.12 2000/03/09 10:27:50 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "xmalloc.h" |
| 21 | #include "buffer.h" |
| 22 | #include "packet.h" |
| 23 | #include "bufaux.h" |
| 24 | #include "ssh.h" |
| 25 | #include "crc32.h" |
| 26 | #include "cipher.h" |
| 27 | #include "getput.h" |
| 28 | |
| 29 | #include "compress.h" |
| 30 | #include "deattack.h" |
| 31 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 32 | /* |
| 33 | * This variable contains the file descriptors used for communicating with |
| 34 | * the other side. connection_in is used for reading; connection_out for |
| 35 | * writing. These can be the same descriptor, in which case it is assumed to |
| 36 | * be a socket. |
| 37 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | static int connection_in = -1; |
| 39 | static int connection_out = -1; |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Cipher type. This value is only used to determine whether to pad the |
| 43 | * packets with zeroes or random data. |
| 44 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | static int cipher_type = SSH_CIPHER_NONE; |
| 46 | |
| 47 | /* Protocol flags for the remote side. */ |
| 48 | static unsigned int remote_protocol_flags = 0; |
| 49 | |
| 50 | /* Encryption context for receiving data. This is only used for decryption. */ |
| 51 | static CipherContext receive_context; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | |
| 53 | /* Encryption context for sending data. This is only used for encryption. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | static CipherContext send_context; |
| 55 | |
| 56 | /* Buffer for raw input data from the socket. */ |
| 57 | static Buffer input; |
| 58 | |
| 59 | /* Buffer for raw output data going to the socket. */ |
| 60 | static Buffer output; |
| 61 | |
| 62 | /* Buffer for the partial outgoing packet being constructed. */ |
| 63 | static Buffer outgoing_packet; |
| 64 | |
| 65 | /* Buffer for the incoming packet currently being processed. */ |
| 66 | static Buffer incoming_packet; |
| 67 | |
| 68 | /* Scratch buffer for packet compression/decompression. */ |
| 69 | static Buffer compression_buffer; |
| 70 | |
| 71 | /* Flag indicating whether packet compression/decompression is enabled. */ |
| 72 | static int packet_compression = 0; |
| 73 | |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 74 | /* default maximum packet size */ |
| 75 | int max_packet_size = 32768; |
| 76 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 77 | /* Flag indicating whether this module has been initialized. */ |
| 78 | static int initialized = 0; |
| 79 | |
| 80 | /* Set to true if the connection is interactive. */ |
| 81 | static int interactive_mode = 0; |
| 82 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 83 | /* |
| 84 | * Sets the descriptors used for communication. Disables encryption until |
| 85 | * packet_set_encryption_key is called. |
| 86 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | |
| 88 | void |
| 89 | packet_set_connection(int fd_in, int fd_out) |
| 90 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | connection_in = fd_in; |
| 92 | connection_out = fd_out; |
| 93 | cipher_type = SSH_CIPHER_NONE; |
| 94 | cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 1); |
| 95 | cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 0); |
| 96 | if (!initialized) { |
| 97 | initialized = 1; |
| 98 | buffer_init(&input); |
| 99 | buffer_init(&output); |
| 100 | buffer_init(&outgoing_packet); |
| 101 | buffer_init(&incoming_packet); |
| 102 | } |
| 103 | /* Kludge: arrange the close function to be called from fatal(). */ |
| 104 | fatal_add_cleanup((void (*) (void *)) packet_close, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | } |
| 106 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 107 | /* Returns 1 if remote host is connected via socket, 0 if not. */ |
| 108 | |
| 109 | int |
| 110 | packet_connection_is_on_socket() |
| 111 | { |
| 112 | struct sockaddr_storage from, to; |
| 113 | socklen_t fromlen, tolen; |
| 114 | |
| 115 | /* filedescriptors in and out are the same, so it's a socket */ |
| 116 | if (connection_in == connection_out) |
| 117 | return 1; |
| 118 | fromlen = sizeof(from); |
| 119 | memset(&from, 0, sizeof(from)); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 120 | if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 121 | return 0; |
| 122 | tolen = sizeof(to); |
| 123 | memset(&to, 0, sizeof(to)); |
Damien Miller | f052aaf | 2000-01-22 19:47:21 +1100 | [diff] [blame] | 124 | if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 125 | return 0; |
| 126 | if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) |
| 127 | return 0; |
| 128 | if (from.ss_family != AF_INET && from.ss_family != AF_INET6) |
| 129 | return 0; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | /* returns 1 if connection is via ipv4 */ |
| 134 | |
| 135 | int |
| 136 | packet_connection_is_ipv4() |
| 137 | { |
| 138 | struct sockaddr_storage to; |
Damien Miller | 6fe375d | 2000-01-23 09:38:00 +1100 | [diff] [blame] | 139 | socklen_t tolen = sizeof(to); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 140 | |
| 141 | memset(&to, 0, sizeof(to)); |
| 142 | if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0) |
| 143 | return 0; |
| 144 | if (to.ss_family != AF_INET) |
| 145 | return 0; |
| 146 | return 1; |
| 147 | } |
| 148 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | /* Sets the connection into non-blocking mode. */ |
| 150 | |
| 151 | void |
| 152 | packet_set_nonblocking() |
| 153 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | /* Set the socket into non-blocking mode. */ |
| 155 | if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0) |
| 156 | error("fcntl O_NONBLOCK: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | if (connection_out != connection_in) { |
| 159 | if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0) |
| 160 | error("fcntl O_NONBLOCK: %.100s", strerror(errno)); |
| 161 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Returns the socket used for reading. */ |
| 165 | |
| 166 | int |
| 167 | packet_get_connection_in() |
| 168 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | return connection_in; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Returns the descriptor used for writing. */ |
| 173 | |
| 174 | int |
| 175 | packet_get_connection_out() |
| 176 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 177 | return connection_out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Closes the connection and clears and frees internal data structures. */ |
| 181 | |
| 182 | void |
| 183 | packet_close() |
| 184 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | if (!initialized) |
| 186 | return; |
| 187 | initialized = 0; |
| 188 | if (connection_in == connection_out) { |
| 189 | shutdown(connection_out, SHUT_RDWR); |
| 190 | close(connection_out); |
| 191 | } else { |
| 192 | close(connection_in); |
| 193 | close(connection_out); |
| 194 | } |
| 195 | buffer_free(&input); |
| 196 | buffer_free(&output); |
| 197 | buffer_free(&outgoing_packet); |
| 198 | buffer_free(&incoming_packet); |
| 199 | if (packet_compression) { |
| 200 | buffer_free(&compression_buffer); |
| 201 | buffer_compress_uninit(); |
| 202 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* Sets remote side protocol flags. */ |
| 206 | |
| 207 | void |
| 208 | packet_set_protocol_flags(unsigned int protocol_flags) |
| 209 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | remote_protocol_flags = protocol_flags; |
| 211 | channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Returns the remote protocol flags set earlier by the above function. */ |
| 215 | |
| 216 | unsigned int |
| 217 | packet_get_protocol_flags() |
| 218 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 219 | return remote_protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 220 | } |
| 221 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 222 | /* |
| 223 | * Starts packet compression from the next packet on in both directions. |
| 224 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
| 225 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 226 | |
| 227 | void |
| 228 | packet_start_compression(int level) |
| 229 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | if (packet_compression) |
| 231 | fatal("Compression already enabled."); |
| 232 | packet_compression = 1; |
| 233 | buffer_init(&compression_buffer); |
| 234 | buffer_compress_init(level); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 235 | } |
| 236 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 237 | /* |
| 238 | * Encrypts the given number of bytes, copying from src to dest. bytes is |
| 239 | * known to be a multiple of 8. |
| 240 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | |
| 242 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | packet_encrypt(CipherContext * cc, void *dest, void *src, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | unsigned int bytes) |
| 245 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | cipher_encrypt(cc, dest, src, bytes); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 247 | } |
| 248 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 249 | /* |
| 250 | * Decrypts the given number of bytes, copying from src to dest. bytes is |
| 251 | * known to be a multiple of 8. |
| 252 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 253 | |
| 254 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | packet_decrypt(CipherContext * cc, void *dest, void *src, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 256 | unsigned int bytes) |
| 257 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 258 | int i; |
| 259 | |
| 260 | if ((bytes % 8) != 0) |
| 261 | fatal("packet_decrypt: bad ciphertext length %d", bytes); |
| 262 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 263 | /* |
| 264 | * Cryptographic attack detector for ssh - Modifications for packet.c |
| 265 | * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com) |
| 266 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 267 | |
| 268 | switch (cc->type) { |
| 269 | case SSH_CIPHER_NONE: |
| 270 | i = DEATTACK_OK; |
| 271 | break; |
| 272 | default: |
| 273 | i = detect_attack(src, bytes, NULL); |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if (i == DEATTACK_DETECTED) |
| 278 | packet_disconnect("crc32 compensation attack: network attack detected"); |
| 279 | |
| 280 | cipher_decrypt(cc, dest, src, bytes); |
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 | * Causes any further packets to be encrypted using the given key. The same |
| 285 | * key is used for both sending and reception. However, both directions are |
| 286 | * encrypted independently of each other. |
| 287 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | |
| 289 | void |
| 290 | packet_set_encryption_key(const unsigned char *key, unsigned int keylen, |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 291 | int cipher) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 292 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 293 | /* All other ciphers use the same key in both directions for now. */ |
| 294 | cipher_set_key(&receive_context, cipher, key, keylen, 0); |
| 295 | cipher_set_key(&send_context, cipher, key, keylen, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Starts constructing a packet to send. */ |
| 299 | |
| 300 | void |
| 301 | packet_start(int type) |
| 302 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | char buf[9]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 304 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 305 | buffer_clear(&outgoing_packet); |
| 306 | memset(buf, 0, 8); |
| 307 | buf[8] = type; |
| 308 | buffer_append(&outgoing_packet, buf, 9); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Appends a character to the packet data. */ |
| 312 | |
| 313 | void |
| 314 | packet_put_char(int value) |
| 315 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | char ch = value; |
| 317 | buffer_append(&outgoing_packet, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* Appends an integer to the packet data. */ |
| 321 | |
| 322 | void |
| 323 | packet_put_int(unsigned int value) |
| 324 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 325 | buffer_put_int(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* Appends a string to packet data. */ |
| 329 | |
| 330 | void |
| 331 | packet_put_string(const char *buf, unsigned int len) |
| 332 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | buffer_put_string(&outgoing_packet, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Appends an arbitrary precision integer to packet data. */ |
| 337 | |
| 338 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | packet_put_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 340 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 341 | buffer_put_bignum(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 342 | } |
| 343 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 344 | /* |
| 345 | * Finalizes and sends the packet. If the encryption key has been set, |
| 346 | * encrypts the packet before sending. |
| 347 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 349 | void |
| 350 | packet_send() |
| 351 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 352 | char buf[8], *cp; |
| 353 | int i, padding, len; |
| 354 | unsigned int checksum; |
| 355 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 357 | /* |
| 358 | * If using packet compression, compress the payload of the outgoing |
| 359 | * packet. |
| 360 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | if (packet_compression) { |
| 362 | buffer_clear(&compression_buffer); |
| 363 | /* Skip padding. */ |
| 364 | buffer_consume(&outgoing_packet, 8); |
| 365 | /* padding */ |
| 366 | buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8); |
| 367 | buffer_compress(&outgoing_packet, &compression_buffer); |
| 368 | buffer_clear(&outgoing_packet); |
| 369 | buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), |
| 370 | buffer_len(&compression_buffer)); |
| 371 | } |
| 372 | /* Compute packet length without padding (add checksum, remove padding). */ |
| 373 | len = buffer_len(&outgoing_packet) + 4 - 8; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 374 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 375 | /* Insert padding. */ |
| 376 | padding = 8 - len % 8; |
| 377 | if (cipher_type != SSH_CIPHER_NONE) { |
| 378 | cp = buffer_ptr(&outgoing_packet); |
| 379 | for (i = 0; i < padding; i++) { |
| 380 | if (i % 4 == 0) |
| 381 | rand = arc4random(); |
| 382 | cp[7 - i] = rand & 0xff; |
| 383 | rand >>= 8; |
| 384 | } |
| 385 | } |
| 386 | buffer_consume(&outgoing_packet, 8 - padding); |
| 387 | |
| 388 | /* Add check bytes. */ |
| 389 | checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet), |
| 390 | buffer_len(&outgoing_packet)); |
| 391 | PUT_32BIT(buf, checksum); |
| 392 | buffer_append(&outgoing_packet, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 393 | |
| 394 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | fprintf(stderr, "packet_send plain: "); |
| 396 | buffer_dump(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 397 | #endif |
| 398 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | /* Append to output. */ |
| 400 | PUT_32BIT(buf, len); |
| 401 | buffer_append(&output, buf, 4); |
| 402 | buffer_append_space(&output, &cp, buffer_len(&outgoing_packet)); |
| 403 | packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet), |
| 404 | buffer_len(&outgoing_packet)); |
| 405 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 406 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 407 | fprintf(stderr, "encrypted: "); |
| 408 | buffer_dump(&output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 409 | #endif |
| 410 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 411 | buffer_clear(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 412 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 413 | /* |
| 414 | * Note that the packet is now only buffered in output. It won\'t be |
| 415 | * actually sent until packet_write_wait or packet_write_poll is |
| 416 | * called. |
| 417 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 418 | } |
| 419 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 420 | /* |
| 421 | * Waits until a packet has been received, and returns its type. Note that |
| 422 | * no other data is processed until this returns, so this function should not |
| 423 | * be used during the interactive session. |
| 424 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | |
| 426 | int |
| 427 | packet_read(int *payload_len_ptr) |
| 428 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 429 | int type, len; |
| 430 | fd_set set; |
| 431 | char buf[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 432 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 433 | /* Since we are blocking, ensure that all written packets have been sent. */ |
| 434 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 435 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | /* Stay in the loop until we have received a complete packet. */ |
| 437 | for (;;) { |
| 438 | /* Try to read a packet from the buffer. */ |
| 439 | type = packet_read_poll(payload_len_ptr); |
| 440 | if (type == SSH_SMSG_SUCCESS |
| 441 | || type == SSH_SMSG_FAILURE |
| 442 | || type == SSH_CMSG_EOF |
| 443 | || type == SSH_CMSG_EXIT_CONFIRMATION) |
| 444 | packet_integrity_check(*payload_len_ptr, 0, type); |
| 445 | /* If we got a packet, return it. */ |
| 446 | if (type != SSH_MSG_NONE) |
| 447 | return type; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 448 | /* |
| 449 | * Otherwise, wait for some data to arrive, add it to the |
| 450 | * buffer, and try again. |
| 451 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 452 | FD_ZERO(&set); |
| 453 | FD_SET(connection_in, &set); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 454 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 455 | /* Wait for some data to arrive. */ |
| 456 | select(connection_in + 1, &set, NULL, NULL, NULL); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 457 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 458 | /* Read data from the socket. */ |
| 459 | len = read(connection_in, buf, sizeof(buf)); |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 460 | if (len == 0) { |
| 461 | log("Connection closed by %.200s", get_remote_ipaddr()); |
| 462 | fatal_cleanup(); |
| 463 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 464 | if (len < 0) |
| 465 | fatal("Read from socket failed: %.100s", strerror(errno)); |
| 466 | /* Append it to the buffer. */ |
| 467 | packet_process_incoming(buf, len); |
| 468 | } |
| 469 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 470 | } |
| 471 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 472 | /* |
| 473 | * Waits until a packet has been received, verifies that its type matches |
| 474 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 475 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 476 | |
| 477 | void |
| 478 | packet_read_expect(int *payload_len_ptr, int expected_type) |
| 479 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 480 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 481 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 482 | type = packet_read(payload_len_ptr); |
| 483 | if (type != expected_type) |
| 484 | packet_disconnect("Protocol error: expected packet type %d, got %d", |
| 485 | expected_type, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* 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] | 489 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 490 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 491 | * |
| 492 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 493 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 494 | * to higher levels. |
| 495 | * |
| 496 | * The returned payload_len does include space consumed by: |
| 497 | * Packet length |
| 498 | * Padding |
| 499 | * Packet type |
| 500 | * Check bytes |
| 501 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 502 | |
| 503 | int |
| 504 | packet_read_poll(int *payload_len_ptr) |
| 505 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | unsigned int len, padded_len; |
| 507 | unsigned char *ucp; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 508 | char buf[8], *cp, *msg; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 509 | unsigned int checksum, stored_checksum; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 510 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 511 | restart: |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 512 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 513 | /* Check if input size is less than minimum packet size. */ |
| 514 | if (buffer_len(&input) < 4 + 8) |
| 515 | return SSH_MSG_NONE; |
| 516 | /* Get length of incoming packet. */ |
| 517 | ucp = (unsigned char *) buffer_ptr(&input); |
| 518 | len = GET_32BIT(ucp); |
| 519 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
| 520 | packet_disconnect("Bad packet length %d.", len); |
| 521 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 522 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 523 | /* Check if the packet has been entirely received. */ |
| 524 | if (buffer_len(&input) < 4 + padded_len) |
| 525 | return SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 526 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 527 | /* The entire packet is in buffer. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 528 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 529 | /* Consume packet length. */ |
| 530 | buffer_consume(&input, 4); |
| 531 | |
| 532 | /* Copy data to incoming_packet. */ |
| 533 | buffer_clear(&incoming_packet); |
| 534 | buffer_append_space(&incoming_packet, &cp, padded_len); |
| 535 | packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len); |
| 536 | buffer_consume(&input, padded_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | |
| 538 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | fprintf(stderr, "read_poll plain: "); |
| 540 | buffer_dump(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 541 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 542 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 543 | /* Compute packet checksum. */ |
| 544 | checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet), |
| 545 | buffer_len(&incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 546 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 547 | /* Skip padding. */ |
| 548 | buffer_consume(&incoming_packet, 8 - len % 8); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 549 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | /* Test check bytes. */ |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 551 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 552 | if (len != buffer_len(&incoming_packet)) |
| 553 | packet_disconnect("packet_read_poll: len %d != buffer_len %d.", |
| 554 | len, buffer_len(&incoming_packet)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 556 | ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4; |
| 557 | stored_checksum = GET_32BIT(ucp); |
| 558 | if (checksum != stored_checksum) |
| 559 | packet_disconnect("Corrupted check bytes on input."); |
| 560 | buffer_consume_end(&incoming_packet, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 561 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 562 | /* If using packet compression, decompress the packet. */ |
| 563 | if (packet_compression) { |
| 564 | buffer_clear(&compression_buffer); |
| 565 | buffer_uncompress(&incoming_packet, &compression_buffer); |
| 566 | buffer_clear(&incoming_packet); |
| 567 | buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), |
| 568 | buffer_len(&compression_buffer)); |
| 569 | } |
| 570 | /* Get packet type. */ |
| 571 | buffer_get(&incoming_packet, &buf[0], 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 572 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 573 | /* Return length of payload (without type field). */ |
| 574 | *payload_len_ptr = buffer_len(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 575 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 576 | /* Handle disconnect message. */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 577 | if ((unsigned char) buf[0] == SSH_MSG_DISCONNECT) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 578 | msg = packet_get_string(NULL); |
| 579 | log("Received disconnect: %.900s", msg); |
| 580 | xfree(msg); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 581 | fatal_cleanup(); |
| 582 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 583 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 584 | /* Ignore ignore messages. */ |
| 585 | if ((unsigned char) buf[0] == SSH_MSG_IGNORE) |
| 586 | goto restart; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 588 | /* Send debug messages as debugging output. */ |
| 589 | if ((unsigned char) buf[0] == SSH_MSG_DEBUG) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 590 | msg = packet_get_string(NULL); |
| 591 | debug("Remote: %.900s", msg); |
| 592 | xfree(msg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 593 | goto restart; |
| 594 | } |
| 595 | /* Return type. */ |
| 596 | return (unsigned char) buf[0]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 597 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 598 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 599 | /* |
| 600 | * Buffers the given amount of input characters. This is intended to be used |
| 601 | * together with packet_read_poll. |
| 602 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 603 | |
| 604 | void |
| 605 | packet_process_incoming(const char *buf, unsigned int len) |
| 606 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 607 | buffer_append(&input, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* Returns a character from the packet. */ |
| 611 | |
| 612 | unsigned int |
| 613 | packet_get_char() |
| 614 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 615 | char ch; |
| 616 | buffer_get(&incoming_packet, &ch, 1); |
| 617 | return (unsigned char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* Returns an integer from the packet data. */ |
| 621 | |
| 622 | unsigned int |
| 623 | packet_get_int() |
| 624 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 625 | return buffer_get_int(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 626 | } |
| 627 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 628 | /* |
| 629 | * Returns an arbitrary precision integer from the packet data. The integer |
| 630 | * must have been initialized before this call. |
| 631 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 632 | |
| 633 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 634 | packet_get_bignum(BIGNUM * value, int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 635 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 636 | *length_ptr = buffer_get_bignum(&incoming_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 637 | } |
| 638 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 639 | /* |
| 640 | * Returns a string from the packet data. The string is allocated using |
| 641 | * xmalloc; it is the responsibility of the calling program to free it when |
| 642 | * no longer needed. The length_ptr argument may be NULL, or point to an |
| 643 | * integer into which the length of the string is stored. |
| 644 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 645 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 646 | char * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 647 | packet_get_string(unsigned int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 648 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 649 | return buffer_get_string(&incoming_packet, length_ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 650 | } |
| 651 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 652 | /* |
| 653 | * Sends a diagnostic message from the server to the client. This message |
| 654 | * can be sent at any time (but not while constructing another message). The |
| 655 | * message is printed immediately, but only if the client is being executed |
| 656 | * in verbose mode. These messages are primarily intended to ease debugging |
| 657 | * authentication problems. The length of the formatted message must not |
| 658 | * exceed 1024 bytes. This will automatically call packet_write_wait. |
| 659 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 660 | |
| 661 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 662 | packet_send_debug(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 663 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 664 | char buf[1024]; |
| 665 | va_list args; |
| 666 | |
| 667 | va_start(args, fmt); |
| 668 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 669 | va_end(args); |
| 670 | |
| 671 | packet_start(SSH_MSG_DEBUG); |
| 672 | packet_put_string(buf, strlen(buf)); |
| 673 | packet_send(); |
| 674 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 675 | } |
| 676 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 677 | /* |
| 678 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 679 | * connection, and exits. This function never returns. The error message |
| 680 | * should not contain a newline. The length of the formatted message must |
| 681 | * not exceed 1024 bytes. |
| 682 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 683 | |
| 684 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 685 | packet_disconnect(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 686 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 687 | char buf[1024]; |
| 688 | va_list args; |
| 689 | static int disconnecting = 0; |
| 690 | if (disconnecting) /* Guard against recursive invocations. */ |
| 691 | fatal("packet_disconnect called recursively."); |
| 692 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 693 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 694 | /* |
| 695 | * Format the message. Note that the caller must make sure the |
| 696 | * message is of limited size. |
| 697 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 698 | va_start(args, fmt); |
| 699 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 700 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 701 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 702 | /* Send the disconnect message to the other side, and wait for it to get sent. */ |
| 703 | packet_start(SSH_MSG_DISCONNECT); |
| 704 | packet_put_string(buf, strlen(buf)); |
| 705 | packet_send(); |
| 706 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 707 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 708 | /* Stop listening for connections. */ |
| 709 | channel_stop_listening(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 710 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 711 | /* Close the connection. */ |
| 712 | packet_close(); |
| 713 | |
| 714 | /* Display the error locally and exit. */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 715 | log("Disconnecting: %.100s", buf); |
| 716 | fatal_cleanup(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 717 | } |
| 718 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 719 | /* 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] | 720 | |
| 721 | void |
| 722 | packet_write_poll() |
| 723 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 724 | int len = buffer_len(&output); |
| 725 | if (len > 0) { |
| 726 | len = write(connection_out, buffer_ptr(&output), len); |
| 727 | if (len <= 0) { |
| 728 | if (errno == EAGAIN) |
| 729 | return; |
| 730 | else |
| 731 | fatal("Write failed: %.100s", strerror(errno)); |
| 732 | } |
| 733 | buffer_consume(&output, len); |
| 734 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 735 | } |
| 736 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 737 | /* |
| 738 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 739 | * written. |
| 740 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | |
| 742 | void |
| 743 | packet_write_wait() |
| 744 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 745 | packet_write_poll(); |
| 746 | while (packet_have_data_to_write()) { |
| 747 | fd_set set; |
| 748 | FD_ZERO(&set); |
| 749 | FD_SET(connection_out, &set); |
| 750 | select(connection_out + 1, NULL, &set, NULL, NULL); |
| 751 | packet_write_poll(); |
| 752 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* Returns true if there is buffered data to write to the connection. */ |
| 756 | |
| 757 | int |
| 758 | packet_have_data_to_write() |
| 759 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 760 | return buffer_len(&output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* Returns true if there is not too much data to write to the connection. */ |
| 764 | |
| 765 | int |
| 766 | packet_not_very_much_data_to_write() |
| 767 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 768 | if (interactive_mode) |
| 769 | return buffer_len(&output) < 16384; |
| 770 | else |
| 771 | return buffer_len(&output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 775 | |
| 776 | void |
| 777 | packet_set_interactive(int interactive, int keepalives) |
| 778 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 779 | int on = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 780 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 781 | /* Record that we are in interactive mode. */ |
| 782 | interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 783 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 784 | /* Only set socket options if using a socket. */ |
| 785 | if (!packet_connection_is_on_socket()) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 786 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 787 | if (keepalives) { |
| 788 | /* Set keepalives if requested. */ |
| 789 | if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 790 | sizeof(on)) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 791 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); |
| 792 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 793 | /* |
| 794 | * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only |
| 795 | */ |
| 796 | if (!packet_connection_is_ipv4()) |
| 797 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 798 | if (interactive) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 799 | /* |
| 800 | * Set IP options for an interactive connection. Use |
| 801 | * IPTOS_LOWDELAY and TCP_NODELAY. |
| 802 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 803 | int lowdelay = IPTOS_LOWDELAY; |
| 804 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 805 | sizeof(lowdelay)) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 806 | error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno)); |
| 807 | if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 808 | sizeof(on)) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 809 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 810 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 811 | /* |
| 812 | * Set IP options for a non-interactive connection. Use |
| 813 | * IPTOS_THROUGHPUT. |
| 814 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 815 | int throughput = IPTOS_THROUGHPUT; |
| 816 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 817 | sizeof(throughput)) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 818 | error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno)); |
| 819 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* Returns true if the current connection is interactive. */ |
| 823 | |
| 824 | int |
| 825 | packet_is_interactive() |
| 826 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | return interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 828 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 829 | |
| 830 | int |
| 831 | packet_set_maxsize(int s) |
| 832 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 833 | static int called = 0; |
| 834 | if (called) { |
| 835 | log("packet_set_maxsize: called twice: old %d new %d", max_packet_size, s); |
| 836 | return -1; |
| 837 | } |
| 838 | if (s < 4 * 1024 || s > 1024 * 1024) { |
| 839 | log("packet_set_maxsize: bad size %d", s); |
| 840 | return -1; |
| 841 | } |
| 842 | log("packet_set_maxsize: setting to %d", s); |
| 843 | max_packet_size = s; |
| 844 | return s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 845 | } |