]> andersk Git - openssh.git/blobdiff - packet.c
- (tim) [configure.ac] Remove setting IP_TOS_IS_BROKEN for Cygwin. The problem
[openssh.git] / packet.c
index 90ad5ff6792bcf344b5dc3e634a004c246d6bd86..5afc84ce0bc22715ae03d01f3131366c9f47817d 100644 (file)
--- a/packet.c
+++ b/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.155 2008/06/13 09:44:36 deraadt Exp $ */
+/* $OpenBSD: packet.c,v 1.160 2009/02/13 11:50:21 markus Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -84,6 +84,8 @@
 #define DBG(x)
 #endif
 
+#define PACKET_MAX_SIZE (256 * 1024)
+
 /*
  * This variable contains the file descriptors used for communicating with
  * the other side.  connection_in is used for reading; connection_out for
@@ -147,6 +149,7 @@ static struct packet_state {
        u_int32_t seqnr;
        u_int32_t packets;
        u_int64_t blocks;
+       u_int64_t bytes;
 } p_read, p_send;
 
 static u_int64_t max_blocks_in, max_blocks_out;
@@ -159,6 +162,10 @@ static u_int ssh1_keylen;
 /* roundup current message to extra_pad bytes */
 static u_char extra_pad = 0;
 
+/* XXX discard incoming data after MAC error */
+static u_int packet_discard = 0;
+static Mac *packet_discard_mac = NULL;
+
 struct packet {
        TAILQ_ENTRY(packet) next;
        u_char type;
@@ -191,6 +198,7 @@ packet_set_connection(int fd_in, int fd_out)
                buffer_init(&outgoing_packet);
                buffer_init(&incoming_packet);
                TAILQ_INIT(&outgoing);
+               p_send.packets = p_read.packets = 0;
        }
 }
 
@@ -207,6 +215,36 @@ packet_set_timeout(int timeout, int count)
                packet_timeout_ms = timeout * count * 1000;
 }
 
+static void
+packet_stop_discard(void)
+{
+       if (packet_discard_mac) {
+               char buf[1024];
+               
+               memset(buf, 'a', sizeof(buf));
+               while (buffer_len(&incoming_packet) < PACKET_MAX_SIZE)
+                       buffer_append(&incoming_packet, buf, sizeof(buf));
+               (void) mac_compute(packet_discard_mac,
+                   p_read.seqnr,
+                   buffer_ptr(&incoming_packet),
+                   PACKET_MAX_SIZE);
+       }
+       logit("Finished discarding for %.200s", get_remote_ipaddr());
+       cleanup_exit(255);
+}
+
+static void
+packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard)
+{
+       if (enc == NULL || !cipher_is_cbc(enc->cipher))
+               packet_disconnect("Packet corrupt");
+       if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
+               packet_discard_mac = mac;
+       if (buffer_len(&input) >= discard)
+               packet_stop_discard();
+       packet_discard = discard - buffer_len(&input);
+}
+
 /* Returns 1 if remote host is connected via socket, 0 if not. */
 
 int
@@ -311,18 +349,25 @@ packet_get_ssh1_cipher(void)
 }
 
 void
-packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets)
+packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets,
+    u_int64_t *bytes)
 {
        struct packet_state *state;
 
        state = (mode == MODE_IN) ? &p_read : &p_send;
-       *seqnr = state->seqnr;
-       *blocks = state->blocks;
-       *packets = state->packets;
+       if (seqnr)
+               *seqnr = state->seqnr;
+       if (blocks)
+               *blocks = state->blocks;
+       if (packets)
+               *packets = state->packets;
+       if (bytes)
+               *bytes = state->bytes;
 }
 
 void
-packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
+packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets,
+    u_int64_t bytes)
 {
        struct packet_state *state;
 
@@ -330,6 +375,7 @@ packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets)
        state->seqnr = seqnr;
        state->blocks = blocks;
        state->packets = packets;
+       state->bytes = bytes;
 }
 
 /* returns 1 if connection is via ipv4 */
@@ -608,7 +654,8 @@ packet_send1(void)
        fprintf(stderr, "encrypted: ");
        buffer_dump(&output);
 #endif
-
+       p_send.packets++;
+       p_send.bytes += len + buffer_len(&outgoing_packet);
        buffer_clear(&outgoing_packet);
 
        /*
@@ -834,6 +881,7 @@ packet_send2_wrapped(void)
                if (!(datafellows & SSH_BUG_NOREKEY))
                        fatal("XXX too many packets with same key");
        p_send.blocks += (packet_length + 4) / block_size;
+       p_send.bytes += packet_length + 4;
        buffer_clear(&outgoing_packet);
 
        if (type == SSH2_MSG_NEWKEYS)
@@ -956,7 +1004,8 @@ packet_read_seqnr(u_int32_t *seqnr_p)
                        if ((ret = select(connection_in + 1, setp, NULL,
                            NULL, timeoutp)) >= 0)
                                break;
-                       if (errno != EAGAIN && errno != EINTR)
+                       if (errno != EAGAIN && errno != EINTR &&
+                           errno != EWOULDBLOCK)
                                break;
                        if (packet_timeout_ms == -1)
                                continue;
@@ -1095,6 +1144,8 @@ packet_read_poll1(void)
                buffer_append(&incoming_packet, buffer_ptr(&compression_buffer),
                    buffer_len(&compression_buffer));
        }
+       p_read.packets++;
+       p_read.bytes += padded_len + 4;
        type = buffer_get_char(&incoming_packet);
        if (type < SSH_MSG_MIN || type > SSH_MSG_MAX)
                packet_disconnect("Invalid ssh1 packet type: %d", type);
@@ -1112,6 +1163,9 @@ packet_read_poll2(u_int32_t *seqnr_p)
        Mac *mac   = NULL;
        Comp *comp = NULL;
 
+       if (packet_discard)
+               return SSH_MSG_NONE;
+
        if (newkeys[MODE_IN] != NULL) {
                enc  = &newkeys[MODE_IN]->enc;
                mac  = &newkeys[MODE_IN]->mac;
@@ -1133,11 +1187,14 @@ packet_read_poll2(u_int32_t *seqnr_p)
                    block_size);
                cp = buffer_ptr(&incoming_packet);
                packet_length = get_u32(cp);
-               if (packet_length < 1 + 4 || packet_length > 256 * 1024) {
+               if (packet_length < 1 + 4 || packet_length > PACKET_MAX_SIZE) {
 #ifdef PACKET_DEBUG
                        buffer_dump(&incoming_packet);
 #endif
-                       packet_disconnect("Bad packet length %u.", packet_length);
+                       logit("Bad packet length %u.", packet_length);
+                       packet_start_discard(enc, mac, packet_length,
+                           PACKET_MAX_SIZE);
+                       return SSH_MSG_NONE;
                }
                DBG(debug("input: packet len %u", packet_length+4));
                buffer_consume(&input, block_size);
@@ -1146,9 +1203,13 @@ packet_read_poll2(u_int32_t *seqnr_p)
        need = 4 + packet_length - block_size;
        DBG(debug("partial packet %d, need %d, maclen %d", block_size,
            need, maclen));
-       if (need % block_size != 0)
-               fatal("padding error: need %d block %d mod %d",
+       if (need % block_size != 0) {
+               logit("padding error: need %d block %d mod %d",
                    need, block_size, need % block_size);
+               packet_start_discard(enc, mac, packet_length,
+                   PACKET_MAX_SIZE - block_size);
+               return SSH_MSG_NONE;
+       }
        /*
         * check if the entire packet has been received and
         * decrypt into incoming_packet
@@ -1170,11 +1231,19 @@ packet_read_poll2(u_int32_t *seqnr_p)
                macbuf = mac_compute(mac, p_read.seqnr,
                    buffer_ptr(&incoming_packet),
                    buffer_len(&incoming_packet));
-               if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
-                       packet_disconnect("Corrupted MAC on input.");
+               if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0) {
+                       logit("Corrupted MAC on input.");
+                       if (need > PACKET_MAX_SIZE)
+                               fatal("internal error need %d", need);
+                       packet_start_discard(enc, mac, packet_length,
+                           PACKET_MAX_SIZE - need);
+                       return SSH_MSG_NONE;
+               }
+                               
                DBG(debug("MAC #%d ok", p_read.seqnr));
                buffer_consume(&input, mac->mac_len);
        }
+       /* XXX now it's safe to use fatal/packet_disconnect */
        if (seqnr_p != NULL)
                *seqnr_p = p_read.seqnr;
        if (++p_read.seqnr == 0)
@@ -1183,6 +1252,7 @@ packet_read_poll2(u_int32_t *seqnr_p)
                if (!(datafellows & SSH_BUG_NOREKEY))
                        fatal("XXX too many packets with same key");
        p_read.blocks += (packet_length + 4) / block_size;
+       p_read.bytes += packet_length + 4;
 
        /* get padlen */
        cp = buffer_ptr(&incoming_packet);
@@ -1306,6 +1376,13 @@ packet_read_poll(void)
 void
 packet_process_incoming(const char *buf, u_int len)
 {
+       if (packet_discard) {
+               keep_alive_timeouts = 0; /* ?? */
+               if (len >= packet_discard)
+                       packet_stop_discard();
+               packet_discard -= len;
+               return;
+       }
        buffer_append(&input, buf, len);
 }
 
@@ -1474,16 +1551,19 @@ packet_write_poll(void)
 
        if (len > 0) {
                len = write(connection_out, buffer_ptr(&output), len);
-               if (len <= 0) {
-                       if (errno == EAGAIN)
+               if (len == -1) {
+                       if (errno == EINTR || errno == EAGAIN ||
+                           errno == EWOULDBLOCK)
                                return;
-                       else
-                               fatal("Write failed: %.100s", strerror(errno));
+                       fatal("Write failed: %.100s", strerror(errno));
                }
+               if (len == 0)
+                       fatal("Write connection closed");
                buffer_consume(&output, len);
        }
 }
 
+
 /*
  * Calls packet_write_poll repeatedly until all pending output data has been
  * written.
@@ -1516,7 +1596,8 @@ packet_write_wait(void)
                        if ((ret = select(connection_out + 1, NULL, setp,
                            NULL, timeoutp)) >= 0)
                                break;
-                       if (errno != EAGAIN && errno != EINTR)
+                       if (errno != EAGAIN && errno != EINTR &&
+                           errno != EWOULDBLOCK)
                                break;
                        if (packet_timeout_ms == -1)
                                continue;
This page took 0.052906 seconds and 4 git commands to generate.