]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * packet.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Sat Mar 18 02:40:40 1995 ylo |
6ae2364d | 11 | * |
5260325f | 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. | |
7e7327a1 | 14 | * |
15 | * SSH2 packet format added by Markus Friedl. | |
16 | * | |
5260325f | 17 | */ |
8efc0c15 | 18 | |
19 | #include "includes.h" | |
20 | RCSID("$Id$"); | |
21 | ||
8ce64345 | 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 */ | |
32 | ||
8efc0c15 | 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" | |
7368a6c8 | 44 | #include "channels.h" |
8efc0c15 | 45 | |
7e7327a1 | 46 | #include "compat.h" |
47 | #include "ssh2.h" | |
48 | ||
7e7327a1 | 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 | ||
aa3378df | 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 | */ | |
8efc0c15 | 65 | static int connection_in = -1; |
66 | static int connection_out = -1; | |
67 | ||
aa3378df | 68 | /* |
69 | * Cipher type. This value is only used to determine whether to pad the | |
70 | * packets with zeroes or random data. | |
71 | */ | |
8efc0c15 | 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; | |
5260325f | 79 | |
80 | /* Encryption context for sending data. This is only used for encryption. */ | |
8efc0c15 | 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 | ||
9d6b7add | 101 | /* default maximum packet size */ |
102 | int max_packet_size = 32768; | |
103 | ||
8efc0c15 | 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 | ||
7e7327a1 | 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 | ||
aa3378df | 145 | /* |
146 | * Sets the descriptors used for communication. Disables encryption until | |
147 | * packet_set_encryption_key is called. | |
148 | */ | |
8efc0c15 | 149 | void |
150 | packet_set_connection(int fd_in, int fd_out) | |
151 | { | |
5260325f | 152 | connection_in = fd_in; |
153 | connection_out = fd_out; | |
154 | cipher_type = SSH_CIPHER_NONE; | |
8ce64345 | 155 | cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0); |
156 | cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0); | |
5260325f | 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); | |
8efc0c15 | 166 | } |
167 | ||
48e671d5 | 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)); | |
9bc5ddfe | 181 | if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0) |
48e671d5 | 182 | return 0; |
183 | tolen = sizeof(to); | |
184 | memset(&to, 0, sizeof(to)); | |
9bc5ddfe | 185 | if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0) |
48e671d5 | 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; | |
d45317d8 | 200 | socklen_t tolen = sizeof(to); |
48e671d5 | 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 | ||
8efc0c15 | 210 | /* Sets the connection into non-blocking mode. */ |
211 | ||
212 | void | |
213 | packet_set_nonblocking() | |
214 | { | |
5260325f | 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)); | |
8efc0c15 | 218 | |
5260325f | 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 | } | |
8efc0c15 | 223 | } |
224 | ||
225 | /* Returns the socket used for reading. */ | |
226 | ||
227 | int | |
228 | packet_get_connection_in() | |
229 | { | |
5260325f | 230 | return connection_in; |
8efc0c15 | 231 | } |
232 | ||
233 | /* Returns the descriptor used for writing. */ | |
234 | ||
235 | int | |
236 | packet_get_connection_out() | |
237 | { | |
5260325f | 238 | return connection_out; |
8efc0c15 | 239 | } |
240 | ||
241 | /* Closes the connection and clears and frees internal data structures. */ | |
242 | ||
243 | void | |
244 | packet_close() | |
245 | { | |
5260325f | 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 | } | |
8efc0c15 | 264 | } |
265 | ||
266 | /* Sets remote side protocol flags. */ | |
267 | ||
268 | void | |
269 | packet_set_protocol_flags(unsigned int protocol_flags) | |
270 | { | |
5260325f | 271 | remote_protocol_flags = protocol_flags; |
272 | channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0); | |
8efc0c15 | 273 | } |
274 | ||
275 | /* Returns the remote protocol flags set earlier by the above function. */ | |
276 | ||
277 | unsigned int | |
278 | packet_get_protocol_flags() | |
279 | { | |
5260325f | 280 | return remote_protocol_flags; |
8efc0c15 | 281 | } |
282 | ||
aa3378df | 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 | */ | |
8efc0c15 | 287 | |
7e7327a1 | 288 | /*** XXXXX todo: kex means re-init */ |
8efc0c15 | 289 | void |
290 | packet_start_compression(int level) | |
291 | { | |
5260325f | 292 | if (packet_compression) |
293 | fatal("Compression already enabled."); | |
294 | packet_compression = 1; | |
295 | buffer_init(&compression_buffer); | |
296 | buffer_compress_init(level); | |
8efc0c15 | 297 | } |
298 | ||
aa3378df | 299 | /* |
300 | * Encrypts the given number of bytes, copying from src to dest. bytes is | |
301 | * known to be a multiple of 8. | |
302 | */ | |
8efc0c15 | 303 | |
304 | void | |
5260325f | 305 | packet_encrypt(CipherContext * cc, void *dest, void *src, |
7e7327a1 | 306 | unsigned int bytes) |
8efc0c15 | 307 | { |
5260325f | 308 | cipher_encrypt(cc, dest, src, bytes); |
8efc0c15 | 309 | } |
310 | ||
aa3378df | 311 | /* |
312 | * Decrypts the given number of bytes, copying from src to dest. bytes is | |
313 | * known to be a multiple of 8. | |
314 | */ | |
8efc0c15 | 315 | |
316 | void | |
5260325f | 317 | packet_decrypt(CipherContext * cc, void *dest, void *src, |
7e7327a1 | 318 | unsigned int bytes) |
8efc0c15 | 319 | { |
5260325f | 320 | int i; |
321 | ||
322 | if ((bytes % 8) != 0) | |
323 | fatal("packet_decrypt: bad ciphertext length %d", bytes); | |
324 | ||
aa3378df | 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 | */ | |
5260325f | 329 | |
7e7327a1 | 330 | if (cc->type == SSH_CIPHER_NONE || compat20) { |
5260325f | 331 | i = DEATTACK_OK; |
7e7327a1 | 332 | } else { |
5260325f | 333 | i = detect_attack(src, bytes, NULL); |
5260325f | 334 | } |
5260325f | 335 | if (i == DEATTACK_DETECTED) |
336 | packet_disconnect("crc32 compensation attack: network attack detected"); | |
337 | ||
338 | cipher_decrypt(cc, dest, src, bytes); | |
8efc0c15 | 339 | } |
340 | ||
aa3378df | 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 | */ | |
8efc0c15 | 346 | |
347 | void | |
348 | packet_set_encryption_key(const unsigned char *key, unsigned int keylen, | |
7e7327a1 | 349 | int cipher) |
8efc0c15 | 350 | { |
7e7327a1 | 351 | if (keylen < 20) |
352 | fatal("keylen too small: %d", keylen); | |
353 | ||
5260325f | 354 | /* All other ciphers use the same key in both directions for now. */ |
8ce64345 | 355 | cipher_set_key(&receive_context, cipher, key, keylen); |
356 | cipher_set_key(&send_context, cipher, key, keylen); | |
8efc0c15 | 357 | } |
358 | ||
359 | /* Starts constructing a packet to send. */ | |
360 | ||
361 | void | |
7e7327a1 | 362 | packet_start1(int type) |
8efc0c15 | 363 | { |
5260325f | 364 | char buf[9]; |
8efc0c15 | 365 | |
5260325f | 366 | buffer_clear(&outgoing_packet); |
367 | memset(buf, 0, 8); | |
368 | buf[8] = type; | |
369 | buffer_append(&outgoing_packet, buf, 9); | |
8efc0c15 | 370 | } |
371 | ||
7e7327a1 | 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 | ||
8efc0c15 | 395 | /* Appends a character to the packet data. */ |
396 | ||
397 | void | |
398 | packet_put_char(int value) | |
399 | { | |
5260325f | 400 | char ch = value; |
401 | buffer_append(&outgoing_packet, &ch, 1); | |
8efc0c15 | 402 | } |
403 | ||
404 | /* Appends an integer to the packet data. */ | |
405 | ||
406 | void | |
407 | packet_put_int(unsigned int value) | |
408 | { | |
5260325f | 409 | buffer_put_int(&outgoing_packet, value); |
8efc0c15 | 410 | } |
411 | ||
412 | /* Appends a string to packet data. */ | |
413 | ||
414 | void | |
415 | packet_put_string(const char *buf, unsigned int len) | |
416 | { | |
5260325f | 417 | buffer_put_string(&outgoing_packet, buf, len); |
8efc0c15 | 418 | } |
7e7327a1 | 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 | ||
8efc0c15 | 431 | |
432 | /* Appends an arbitrary precision integer to packet data. */ | |
433 | ||
434 | void | |
5260325f | 435 | packet_put_bignum(BIGNUM * value) |
8efc0c15 | 436 | { |
5260325f | 437 | buffer_put_bignum(&outgoing_packet, value); |
8efc0c15 | 438 | } |
7e7327a1 | 439 | void |
440 | packet_put_bignum2(BIGNUM * value) | |
441 | { | |
442 | buffer_put_bignum2(&outgoing_packet, value); | |
443 | } | |
8efc0c15 | 444 | |
aa3378df | 445 | /* |
446 | * Finalizes and sends the packet. If the encryption key has been set, | |
447 | * encrypts the packet before sending. | |
448 | */ | |
5260325f | 449 | |
8efc0c15 | 450 | void |
7e7327a1 | 451 | packet_send1() |
8efc0c15 | 452 | { |
5260325f | 453 | char buf[8], *cp; |
454 | int i, padding, len; | |
455 | unsigned int checksum; | |
456 | u_int32_t rand = 0; | |
457 | ||
aa3378df | 458 | /* |
459 | * If using packet compression, compress the payload of the outgoing | |
460 | * packet. | |
461 | */ | |
5260325f | 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; | |
475 | ||
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); | |
8efc0c15 | 494 | |
495 | #ifdef PACKET_DEBUG | |
5260325f | 496 | fprintf(stderr, "packet_send plain: "); |
497 | buffer_dump(&outgoing_packet); | |
8efc0c15 | 498 | #endif |
499 | ||
5260325f | 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 | ||
8efc0c15 | 507 | #ifdef PACKET_DEBUG |
5260325f | 508 | fprintf(stderr, "encrypted: "); |
509 | buffer_dump(&output); | |
8efc0c15 | 510 | #endif |
511 | ||
5260325f | 512 | buffer_clear(&outgoing_packet); |
8efc0c15 | 513 | |
aa3378df | 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 | */ | |
8efc0c15 | 519 | } |
520 | ||
7e7327a1 | 521 | /* |
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; | |
6ae2364d | 532 | static unsigned int seqnr = 0; |
7e7327a1 | 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 | |
6ae2364d | 614 | /* increment sequence number for outgoing packets */ |
615 | if (++seqnr == 0) | |
616 | log("outgoing seqnr wraps around"); | |
7e7327a1 | 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 | ||
aa3378df | 654 | /* |
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 | */ | |
8efc0c15 | 659 | |
660 | int | |
661 | packet_read(int *payload_len_ptr) | |
662 | { | |
5260325f | 663 | int type, len; |
664 | fd_set set; | |
665 | char buf[8192]; | |
7e7327a1 | 666 | DBG(debug("packet_read()")); |
5260325f | 667 | |
668 | /* Since we are blocking, ensure that all written packets have been sent. */ | |
669 | packet_write_wait(); | |
670 | ||
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; | |
aa3378df | 683 | /* |
684 | * Otherwise, wait for some data to arrive, add it to the | |
685 | * buffer, and try again. | |
686 | */ | |
5260325f | 687 | FD_ZERO(&set); |
688 | FD_SET(connection_in, &set); | |
aa3378df | 689 | |
5260325f | 690 | /* Wait for some data to arrive. */ |
691 | select(connection_in + 1, &set, NULL, NULL, NULL); | |
aa3378df | 692 | |
5260325f | 693 | /* Read data from the socket. */ |
694 | len = read(connection_in, buf, sizeof(buf)); | |
89cafde6 | 695 | if (len == 0) { |
696 | log("Connection closed by %.200s", get_remote_ipaddr()); | |
697 | fatal_cleanup(); | |
698 | } | |
5260325f | 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 */ | |
8efc0c15 | 705 | } |
706 | ||
aa3378df | 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 | */ | |
8efc0c15 | 711 | |
712 | void | |
713 | packet_read_expect(int *payload_len_ptr, int expected_type) | |
714 | { | |
5260325f | 715 | int type; |
8efc0c15 | 716 | |
5260325f | 717 | type = packet_read(payload_len_ptr); |
718 | if (type != expected_type) | |
719 | packet_disconnect("Protocol error: expected packet type %d, got %d", | |
7e7327a1 | 720 | expected_type, type); |
8efc0c15 | 721 | } |
722 | ||
723 | /* Checks if a full packet is available in the data received so far via | |
5260325f | 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 | */ | |
8efc0c15 | 737 | |
738 | int | |
7e7327a1 | 739 | packet_read_poll1(int *payload_len_ptr) |
8efc0c15 | 740 | { |
5260325f | 741 | unsigned int len, padded_len; |
742 | unsigned char *ucp; | |
7e7327a1 | 743 | char buf[8], *cp; |
5260325f | 744 | unsigned int checksum, stored_checksum; |
745 | ||
5260325f | 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; | |
755 | ||
756 | /* Check if the packet has been entirely received. */ | |
757 | if (buffer_len(&input) < 4 + padded_len) | |
758 | return SSH_MSG_NONE; | |
759 | ||
760 | /* The entire packet is in buffer. */ | |
761 | ||
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); | |
8efc0c15 | 770 | |
771 | #ifdef PACKET_DEBUG | |
5260325f | 772 | fprintf(stderr, "read_poll plain: "); |
773 | buffer_dump(&incoming_packet); | |
8efc0c15 | 774 | #endif |
5260325f | 775 | |
776 | /* Compute packet checksum. */ | |
777 | checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet), | |
7e7327a1 | 778 | buffer_len(&incoming_packet) - 4); |
5260325f | 779 | |
780 | /* Skip padding. */ | |
781 | buffer_consume(&incoming_packet, 8 - len % 8); | |
782 | ||
783 | /* Test check bytes. */ | |
784 | ||
785 | if (len != buffer_len(&incoming_packet)) | |
786 | packet_disconnect("packet_read_poll: len %d != buffer_len %d.", | |
7e7327a1 | 787 | len, buffer_len(&incoming_packet)); |
5260325f | 788 | |
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); | |
794 | ||
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), | |
7e7327a1 | 801 | buffer_len(&compression_buffer)); |
5260325f | 802 | } |
803 | /* Get packet type. */ | |
804 | buffer_get(&incoming_packet, &buf[0], 1); | |
805 | ||
806 | /* Return length of payload (without type field). */ | |
807 | *payload_len_ptr = buffer_len(&incoming_packet); | |
808 | ||
5260325f | 809 | /* Return type. */ |
810 | return (unsigned char) buf[0]; | |
811 | } | |
812 | ||
7e7327a1 | 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 | */ | |
6ae2364d | 880 | if (mac && mac->enabled) { |
7e7327a1 | 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 | } | |
6ae2364d | 891 | if (++seqnr == 0) |
892 | log("incoming seqnr wraps around"); | |
7e7327a1 | 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 | ||
aa3378df | 1011 | /* |
1012 | * Buffers the given amount of input characters. This is intended to be used | |
1013 | * together with packet_read_poll. | |
1014 | */ | |
8efc0c15 | 1015 | |
1016 | void | |
1017 | packet_process_incoming(const char *buf, unsigned int len) | |
1018 | { | |
5260325f | 1019 | buffer_append(&input, buf, len); |
8efc0c15 | 1020 | } |
1021 | ||
1022 | /* Returns a character from the packet. */ | |
1023 | ||
1024 | unsigned int | |
1025 | packet_get_char() | |
1026 | { | |
5260325f | 1027 | char ch; |
1028 | buffer_get(&incoming_packet, &ch, 1); | |
1029 | return (unsigned char) ch; | |
8efc0c15 | 1030 | } |
1031 | ||
1032 | /* Returns an integer from the packet data. */ | |
1033 | ||
1034 | unsigned int | |
1035 | packet_get_int() | |
1036 | { | |
5260325f | 1037 | return buffer_get_int(&incoming_packet); |
8efc0c15 | 1038 | } |
1039 | ||
aa3378df | 1040 | /* |
1041 | * Returns an arbitrary precision integer from the packet data. The integer | |
1042 | * must have been initialized before this call. | |
1043 | */ | |
8efc0c15 | 1044 | |
1045 | void | |
5260325f | 1046 | packet_get_bignum(BIGNUM * value, int *length_ptr) |
8efc0c15 | 1047 | { |
5260325f | 1048 | *length_ptr = buffer_get_bignum(&incoming_packet, value); |
8efc0c15 | 1049 | } |
1050 | ||
7e7327a1 | 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 | ||
6ae2364d | 1066 | int |
1067 | packet_remaining(void) | |
1068 | { | |
1069 | return buffer_len(&incoming_packet); | |
1070 | } | |
1071 | ||
aa3378df | 1072 | /* |
1073 | * Returns a string from the packet data. The string is allocated using | |
1074 | * xmalloc; it is the responsibility of the calling program to free it when | |
1075 | * no longer needed. The length_ptr argument may be NULL, or point to an | |
1076 | * integer into which the length of the string is stored. | |
1077 | */ | |
8efc0c15 | 1078 | |
aa3378df | 1079 | char * |
5260325f | 1080 | packet_get_string(unsigned int *length_ptr) |
8efc0c15 | 1081 | { |
5260325f | 1082 | return buffer_get_string(&incoming_packet, length_ptr); |
8efc0c15 | 1083 | } |
1084 | ||
aa3378df | 1085 | /* |
1086 | * Sends a diagnostic message from the server to the client. This message | |
1087 | * can be sent at any time (but not while constructing another message). The | |
1088 | * message is printed immediately, but only if the client is being executed | |
1089 | * in verbose mode. These messages are primarily intended to ease debugging | |
1090 | * authentication problems. The length of the formatted message must not | |
1091 | * exceed 1024 bytes. This will automatically call packet_write_wait. | |
1092 | */ | |
8efc0c15 | 1093 | |
1094 | void | |
5260325f | 1095 | packet_send_debug(const char *fmt,...) |
8efc0c15 | 1096 | { |
5260325f | 1097 | char buf[1024]; |
1098 | va_list args; | |
1099 | ||
1100 | va_start(args, fmt); | |
1101 | vsnprintf(buf, sizeof(buf), fmt, args); | |
1102 | va_end(args); | |
1103 | ||
1104 | packet_start(SSH_MSG_DEBUG); | |
1105 | packet_put_string(buf, strlen(buf)); | |
1106 | packet_send(); | |
1107 | packet_write_wait(); | |
8efc0c15 | 1108 | } |
1109 | ||
aa3378df | 1110 | /* |
1111 | * Logs the error plus constructs and sends a disconnect packet, closes the | |
1112 | * connection, and exits. This function never returns. The error message | |
1113 | * should not contain a newline. The length of the formatted message must | |
1114 | * not exceed 1024 bytes. | |
1115 | */ | |
8efc0c15 | 1116 | |
1117 | void | |
5260325f | 1118 | packet_disconnect(const char *fmt,...) |
1119 | { | |
1120 | char buf[1024]; | |
1121 | va_list args; | |
1122 | static int disconnecting = 0; | |
1123 | if (disconnecting) /* Guard against recursive invocations. */ | |
1124 | fatal("packet_disconnect called recursively."); | |
1125 | disconnecting = 1; | |
1126 | ||
aa3378df | 1127 | /* |
1128 | * Format the message. Note that the caller must make sure the | |
1129 | * message is of limited size. | |
1130 | */ | |
5260325f | 1131 | va_start(args, fmt); |
1132 | vsnprintf(buf, sizeof(buf), fmt, args); | |
1133 | va_end(args); | |
1134 | ||
1135 | /* Send the disconnect message to the other side, and wait for it to get sent. */ | |
7e7327a1 | 1136 | if (compat20) { |
1137 | packet_start(SSH2_MSG_DISCONNECT); | |
1138 | packet_put_int(SSH2_DISCONNECT_PROTOCOL_ERROR); | |
1139 | packet_put_cstring(buf); | |
1140 | packet_put_cstring(""); | |
1141 | } else { | |
1142 | packet_start(SSH_MSG_DISCONNECT); | |
1143 | packet_put_string(buf, strlen(buf)); | |
1144 | } | |
5260325f | 1145 | packet_send(); |
1146 | packet_write_wait(); | |
1147 | ||
1148 | /* Stop listening for connections. */ | |
1149 | channel_stop_listening(); | |
1150 | ||
1151 | /* Close the connection. */ | |
1152 | packet_close(); | |
1153 | ||
1154 | /* Display the error locally and exit. */ | |
57112b5a | 1155 | log("Disconnecting: %.100s", buf); |
1156 | fatal_cleanup(); | |
8efc0c15 | 1157 | } |
1158 | ||
aa3378df | 1159 | /* Checks if there is any buffered output, and tries to write some of the output. */ |
8efc0c15 | 1160 | |
1161 | void | |
1162 | packet_write_poll() | |
1163 | { | |
5260325f | 1164 | int len = buffer_len(&output); |
1165 | if (len > 0) { | |
1166 | len = write(connection_out, buffer_ptr(&output), len); | |
1167 | if (len <= 0) { | |
1168 | if (errno == EAGAIN) | |
1169 | return; | |
1170 | else | |
1171 | fatal("Write failed: %.100s", strerror(errno)); | |
1172 | } | |
1173 | buffer_consume(&output, len); | |
1174 | } | |
8efc0c15 | 1175 | } |
1176 | ||
aa3378df | 1177 | /* |
1178 | * Calls packet_write_poll repeatedly until all pending output data has been | |
1179 | * written. | |
1180 | */ | |
8efc0c15 | 1181 | |
1182 | void | |
1183 | packet_write_wait() | |
1184 | { | |
5260325f | 1185 | packet_write_poll(); |
1186 | while (packet_have_data_to_write()) { | |
1187 | fd_set set; | |
1188 | FD_ZERO(&set); | |
1189 | FD_SET(connection_out, &set); | |
1190 | select(connection_out + 1, NULL, &set, NULL, NULL); | |
1191 | packet_write_poll(); | |
1192 | } | |
8efc0c15 | 1193 | } |
1194 | ||
1195 | /* Returns true if there is buffered data to write to the connection. */ | |
1196 | ||
1197 | int | |
1198 | packet_have_data_to_write() | |
1199 | { | |
5260325f | 1200 | return buffer_len(&output) != 0; |
8efc0c15 | 1201 | } |
1202 | ||
1203 | /* Returns true if there is not too much data to write to the connection. */ | |
1204 | ||
1205 | int | |
1206 | packet_not_very_much_data_to_write() | |
1207 | { | |
5260325f | 1208 | if (interactive_mode) |
1209 | return buffer_len(&output) < 16384; | |
1210 | else | |
1211 | return buffer_len(&output) < 128 * 1024; | |
8efc0c15 | 1212 | } |
1213 | ||
1214 | /* Informs that the current session is interactive. Sets IP flags for that. */ | |
1215 | ||
1216 | void | |
1217 | packet_set_interactive(int interactive, int keepalives) | |
1218 | { | |
5260325f | 1219 | int on = 1; |
1220 | ||
1221 | /* Record that we are in interactive mode. */ | |
1222 | interactive_mode = interactive; | |
1223 | ||
48e671d5 | 1224 | /* Only set socket options if using a socket. */ |
1225 | if (!packet_connection_is_on_socket()) | |
5260325f | 1226 | return; |
5260325f | 1227 | if (keepalives) { |
1228 | /* Set keepalives if requested. */ | |
1229 | if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on, | |
48e671d5 | 1230 | sizeof(on)) < 0) |
5260325f | 1231 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); |
1232 | } | |
48e671d5 | 1233 | /* |
1234 | * IPTOS_LOWDELAY, TCP_NODELAY and IPTOS_THROUGHPUT are IPv4 only | |
1235 | */ | |
1236 | if (!packet_connection_is_ipv4()) | |
1237 | return; | |
5260325f | 1238 | if (interactive) { |
aa3378df | 1239 | /* |
1240 | * Set IP options for an interactive connection. Use | |
1241 | * IPTOS_LOWDELAY and TCP_NODELAY. | |
1242 | */ | |
5260325f | 1243 | int lowdelay = IPTOS_LOWDELAY; |
1244 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay, | |
48e671d5 | 1245 | sizeof(lowdelay)) < 0) |
5260325f | 1246 | error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno)); |
1247 | if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on, | |
48e671d5 | 1248 | sizeof(on)) < 0) |
5260325f | 1249 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
1250 | } else { | |
aa3378df | 1251 | /* |
1252 | * Set IP options for a non-interactive connection. Use | |
1253 | * IPTOS_THROUGHPUT. | |
1254 | */ | |
5260325f | 1255 | int throughput = IPTOS_THROUGHPUT; |
1256 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput, | |
48e671d5 | 1257 | sizeof(throughput)) < 0) |
5260325f | 1258 | error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno)); |
1259 | } | |
8efc0c15 | 1260 | } |
1261 | ||
1262 | /* Returns true if the current connection is interactive. */ | |
1263 | ||
1264 | int | |
1265 | packet_is_interactive() | |
1266 | { | |
5260325f | 1267 | return interactive_mode; |
8efc0c15 | 1268 | } |
9d6b7add | 1269 | |
1270 | int | |
1271 | packet_set_maxsize(int s) | |
1272 | { | |
5260325f | 1273 | static int called = 0; |
1274 | if (called) { | |
7e7327a1 | 1275 | log("packet_set_maxsize: called twice: old %d new %d", |
1276 | max_packet_size, s); | |
5260325f | 1277 | return -1; |
1278 | } | |
1279 | if (s < 4 * 1024 || s > 1024 * 1024) { | |
1280 | log("packet_set_maxsize: bad size %d", s); | |
1281 | return -1; | |
1282 | } | |
1283 | log("packet_set_maxsize: setting to %d", s); | |
1284 | max_packet_size = s; | |
1285 | return s; | |
9d6b7add | 1286 | } |