Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1 | /* |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | 66ac6a4 | 2002-01-22 23:22:44 +1100 | [diff] [blame] | 26 | RCSID("$OpenBSD: nchan.c,v 1.36 2002/01/10 12:47:59 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 28 | #include "ssh1.h" |
| 29 | #include "ssh2.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | #include "buffer.h" |
| 31 | #include "packet.h" |
| 32 | #include "channels.h" |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 33 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 34 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 36 | /* |
| 37 | * SSH Protocol 1.5 aka New Channel Protocol |
| 38 | * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored. |
| 39 | * Written by Markus Friedl in October 1999 |
| 40 | * |
| 41 | * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the |
| 42 | * tear down of channels: |
| 43 | * |
| 44 | * 1.3: strict request-ack-protocol: |
| 45 | * CLOSE -> |
| 46 | * <- CLOSE_CONFIRM |
| 47 | * |
| 48 | * 1.5: uses variations of: |
| 49 | * IEOF -> |
| 50 | * <- OCLOSE |
| 51 | * <- IEOF |
| 52 | * OCLOSE -> |
| 53 | * i.e. both sides have to close the channel |
| 54 | * |
| 55 | * 2.0: the EOF messages are optional |
| 56 | * |
| 57 | * See the debugging output from 'ssh -v' and 'sshd -d' of |
| 58 | * ssh-1.2.27 as an example. |
| 59 | * |
| 60 | */ |
Kevin Steves | c3422eb | 2001-09-20 19:33:33 +0000 | [diff] [blame] | 61 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 62 | /* functions manipulating channel states */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 64 | * EVENTS update channel input/output states execute ACTIONS |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | */ |
| 66 | /* events concerning the INPUT from socket for channel (istate) */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 67 | chan_event_fn *chan_rcvd_oclose = NULL; |
| 68 | chan_event_fn *chan_read_failed = NULL; |
| 69 | chan_event_fn *chan_ibuf_empty = NULL; |
| 70 | /* events concerning the OUTPUT from channel for socket (ostate) */ |
| 71 | chan_event_fn *chan_rcvd_ieof = NULL; |
| 72 | chan_event_fn *chan_write_failed = NULL; |
| 73 | chan_event_fn *chan_obuf_empty = NULL; |
| 74 | /* |
| 75 | * ACTIONS: should never update the channel states |
| 76 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 77 | static void chan_send_ieof1(Channel *); |
| 78 | static void chan_send_oclose1(Channel *); |
| 79 | static void chan_send_close2(Channel *); |
| 80 | static void chan_send_eof2(Channel *); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 81 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 82 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 83 | static void chan_shutdown_write(Channel *); |
| 84 | static void chan_shutdown_read(Channel *); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * SSH1 specific implementation of event functions |
| 88 | */ |
| 89 | |
| 90 | static void |
| 91 | chan_rcvd_oclose1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 93 | debug("channel %d: rcvd oclose", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | case CHAN_INPUT_WAIT_OCLOSE: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 96 | debug("channel %d: input wait_oclose -> closed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | c->istate = CHAN_INPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | break; |
| 99 | case CHAN_INPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 100 | debug("channel %d: input open -> closed", c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | chan_shutdown_read(c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 102 | chan_send_ieof1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | c->istate = CHAN_INPUT_CLOSED; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 104 | break; |
| 105 | case CHAN_INPUT_WAIT_DRAIN: |
| 106 | /* both local read_failed and remote write_failed */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 107 | log("channel %d: input drain -> closed", c->self); |
| 108 | chan_send_ieof1(c); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 109 | c->istate = CHAN_INPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | break; |
| 111 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 112 | error("channel %d: protocol error: rcvd_oclose for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 113 | c->self, c->istate); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 114 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | } |
| 116 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 117 | static void |
| 118 | chan_read_failed_12(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 120 | debug("channel %d: read failed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | case CHAN_INPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 123 | debug("channel %d: input open -> drain", c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | chan_shutdown_read(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 125 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 126 | break; |
| 127 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 128 | error("channel %d: chan_read_failed for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 129 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 133 | static void |
| 134 | chan_ibuf_empty1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 136 | debug("channel %d: ibuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | if (buffer_len(&c->input)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 138 | error("channel %d: chan_ibuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 139 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | return; |
| 141 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | case CHAN_INPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 144 | debug("channel %d: input drain -> wait_oclose", c->self); |
| 145 | chan_send_ieof1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | c->istate = CHAN_INPUT_WAIT_OCLOSE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | break; |
| 148 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 149 | error("channel %d: chan_ibuf_empty for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 150 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 154 | static void |
| 155 | chan_rcvd_ieof1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 157 | debug("channel %d: rcvd ieof", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | case CHAN_OUTPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 160 | debug("channel %d: output open -> drain", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | break; |
| 163 | case CHAN_OUTPUT_WAIT_IEOF: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 164 | debug("channel %d: output wait_ieof -> closed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | break; |
| 167 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 168 | error("channel %d: protocol error: rcvd_ieof for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 169 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 173 | static void |
| 174 | chan_write_failed1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 176 | debug("channel %d: write failed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 177 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | case CHAN_OUTPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 179 | debug("channel %d: output open -> wait_ieof", c->self); |
| 180 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | c->ostate = CHAN_OUTPUT_WAIT_IEOF; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 182 | break; |
| 183 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 184 | debug("channel %d: output wait_drain -> closed", c->self); |
| 185 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 186 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 187 | break; |
| 188 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 189 | error("channel %d: chan_write_failed for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 190 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 194 | static void |
| 195 | chan_obuf_empty1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 197 | debug("channel %d: obuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | if (buffer_len(&c->output)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 199 | error("channel %d: chan_obuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 200 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 201 | return; |
| 202 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 205 | debug("channel %d: output drain -> closed", c->self); |
| 206 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 207 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | break; |
| 209 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 210 | error("channel %d: internal error: obuf_empty for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 211 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 215 | static void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 216 | chan_send_ieof1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 217 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 218 | debug("channel %d: send ieof", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 219 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 220 | case CHAN_INPUT_OPEN: |
| 221 | case CHAN_INPUT_WAIT_DRAIN: |
| 222 | packet_start(SSH_MSG_CHANNEL_INPUT_EOF); |
| 223 | packet_put_int(c->remote_id); |
| 224 | packet_send(); |
| 225 | break; |
| 226 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 227 | error("channel %d: cannot send ieof for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 228 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | } |
| 232 | static void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 233 | chan_send_oclose1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 234 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 235 | debug("channel %d: send oclose", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | case CHAN_OUTPUT_OPEN: |
| 238 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 239 | chan_shutdown_write(c); |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 240 | buffer_clear(&c->output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE); |
| 242 | packet_put_int(c->remote_id); |
| 243 | packet_send(); |
| 244 | break; |
| 245 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 246 | error("channel %d: cannot send oclose for ostate %d", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 247 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * the same for SSH2 |
| 254 | */ |
| 255 | static void |
| 256 | chan_rcvd_oclose2(Channel *c) |
| 257 | { |
| 258 | debug("channel %d: rcvd close", c->self); |
| 259 | if (c->flags & CHAN_CLOSE_RCVD) |
| 260 | error("channel %d: protocol error: close rcvd twice", c->self); |
| 261 | c->flags |= CHAN_CLOSE_RCVD; |
| 262 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 263 | /* tear down larval channels immediately */ |
| 264 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 265 | c->istate = CHAN_INPUT_CLOSED; |
| 266 | return; |
| 267 | } |
| 268 | switch (c->ostate) { |
| 269 | case CHAN_OUTPUT_OPEN: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 270 | /* |
| 271 | * wait until a data from the channel is consumed if a CLOSE |
| 272 | * is received |
| 273 | */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 274 | debug("channel %d: output open -> drain", c->self); |
| 275 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
| 276 | break; |
| 277 | } |
| 278 | switch (c->istate) { |
| 279 | case CHAN_INPUT_OPEN: |
| 280 | debug("channel %d: input open -> closed", c->self); |
| 281 | chan_shutdown_read(c); |
| 282 | break; |
| 283 | case CHAN_INPUT_WAIT_DRAIN: |
| 284 | debug("channel %d: input drain -> closed", c->self); |
| 285 | chan_send_eof2(c); |
| 286 | break; |
| 287 | } |
| 288 | c->istate = CHAN_INPUT_CLOSED; |
| 289 | } |
| 290 | static void |
| 291 | chan_ibuf_empty2(Channel *c) |
| 292 | { |
| 293 | debug("channel %d: ibuf empty", c->self); |
| 294 | if (buffer_len(&c->input)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 295 | error("channel %d: chan_ibuf_empty for non empty buffer", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 296 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | switch (c->istate) { |
| 300 | case CHAN_INPUT_WAIT_DRAIN: |
| 301 | debug("channel %d: input drain -> closed", c->self); |
| 302 | if (!(c->flags & CHAN_CLOSE_SENT)) |
| 303 | chan_send_eof2(c); |
| 304 | c->istate = CHAN_INPUT_CLOSED; |
| 305 | break; |
| 306 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 307 | error("channel %d: chan_ibuf_empty for istate %d", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 308 | c->self, c->istate); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | static void |
| 313 | chan_rcvd_ieof2(Channel *c) |
| 314 | { |
| 315 | debug("channel %d: rcvd eof", c->self); |
| 316 | if (c->ostate == CHAN_OUTPUT_OPEN) { |
| 317 | debug("channel %d: output open -> drain", c->self); |
| 318 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
| 319 | } |
| 320 | } |
| 321 | static void |
| 322 | chan_write_failed2(Channel *c) |
| 323 | { |
| 324 | debug("channel %d: write failed", c->self); |
| 325 | switch (c->ostate) { |
| 326 | case CHAN_OUTPUT_OPEN: |
| 327 | debug("channel %d: output open -> closed", c->self); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 328 | chan_shutdown_write(c); /* ?? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 329 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 330 | break; |
| 331 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 332 | debug("channel %d: output drain -> closed", c->self); |
| 333 | chan_shutdown_write(c); |
| 334 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 335 | break; |
| 336 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 337 | error("channel %d: chan_write_failed for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 338 | c->self, c->ostate); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | static void |
| 343 | chan_obuf_empty2(Channel *c) |
| 344 | { |
| 345 | debug("channel %d: obuf empty", c->self); |
| 346 | if (buffer_len(&c->output)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 347 | error("channel %d: chan_obuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 348 | c->self); |
| 349 | return; |
| 350 | } |
| 351 | switch (c->ostate) { |
| 352 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 353 | debug("channel %d: output drain -> closed", c->self); |
| 354 | chan_shutdown_write(c); |
| 355 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 356 | break; |
| 357 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 358 | error("channel %d: chan_obuf_empty for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 359 | c->self, c->ostate); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | static void |
| 364 | chan_send_eof2(Channel *c) |
| 365 | { |
| 366 | debug("channel %d: send eof", c->self); |
| 367 | switch (c->istate) { |
| 368 | case CHAN_INPUT_WAIT_DRAIN: |
| 369 | packet_start(SSH2_MSG_CHANNEL_EOF); |
| 370 | packet_put_int(c->remote_id); |
| 371 | packet_send(); |
| 372 | break; |
| 373 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 374 | error("channel %d: cannot send eof for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 375 | c->self, c->istate); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | static void |
| 380 | chan_send_close2(Channel *c) |
| 381 | { |
| 382 | debug("channel %d: send close", c->self); |
| 383 | if (c->ostate != CHAN_OUTPUT_CLOSED || |
| 384 | c->istate != CHAN_INPUT_CLOSED) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 385 | error("channel %d: cannot send close for istate/ostate %d/%d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 386 | c->self, c->istate, c->ostate); |
| 387 | } else if (c->flags & CHAN_CLOSE_SENT) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 388 | error("channel %d: already sent close", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 389 | } else { |
| 390 | packet_start(SSH2_MSG_CHANNEL_CLOSE); |
| 391 | packet_put_int(c->remote_id); |
| 392 | packet_send(); |
| 393 | c->flags |= CHAN_CLOSE_SENT; |
| 394 | } |
| 395 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 396 | |
| 397 | /* shared */ |
| 398 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 399 | void |
| 400 | chan_mark_dead(Channel *c) |
| 401 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 402 | c->type = SSH_CHANNEL_ZOMBIE; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 405 | int |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 406 | chan_is_dead(Channel *c, int send) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 407 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 408 | if (c->type == SSH_CHANNEL_ZOMBIE) { |
| 409 | debug("channel %d: zombie", c->self); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 410 | return 1; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 411 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 412 | if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED) |
| 413 | return 0; |
| 414 | if (!compat20) { |
| 415 | debug("channel %d: is dead", c->self); |
| 416 | return 1; |
| 417 | } |
| 418 | /* |
| 419 | * we have to delay the close message if the efd (for stderr) is |
| 420 | * still active |
| 421 | */ |
| 422 | if (((c->extended_usage != CHAN_EXTENDED_IGNORE) && |
| 423 | buffer_len(&c->extended) > 0) |
| 424 | #if 0 |
| 425 | || ((c->extended_usage == CHAN_EXTENDED_READ) && |
| 426 | c->efd != -1) |
| 427 | #endif |
| 428 | ) { |
| 429 | debug2("channel %d: active efd: %d len %d type %s", |
| 430 | c->self, c->efd, buffer_len(&c->extended), |
| 431 | c->extended_usage==CHAN_EXTENDED_READ ? |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 432 | "read": "write"); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 433 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 434 | if (!(c->flags & CHAN_CLOSE_SENT)) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 435 | if (send) { |
| 436 | chan_send_close2(c); |
| 437 | } else { |
| 438 | /* channel would be dead if we sent a close */ |
| 439 | if (c->flags & CHAN_CLOSE_RCVD) { |
| 440 | debug("channel %d: almost dead", |
| 441 | c->self); |
| 442 | return 1; |
| 443 | } |
| 444 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 445 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 446 | if ((c->flags & CHAN_CLOSE_SENT) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 447 | (c->flags & CHAN_CLOSE_RCVD)) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 448 | debug("channel %d: is dead", c->self); |
| 449 | return 1; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 450 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 451 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 452 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 453 | } |
| 454 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 455 | void |
| 456 | chan_init_iostates(Channel *c) |
| 457 | { |
| 458 | c->ostate = CHAN_OUTPUT_OPEN; |
| 459 | c->istate = CHAN_INPUT_OPEN; |
| 460 | c->flags = 0; |
| 461 | } |
| 462 | |
| 463 | /* init */ |
| 464 | void |
| 465 | chan_init(void) |
| 466 | { |
| 467 | if (compat20) { |
| 468 | chan_rcvd_oclose = chan_rcvd_oclose2; |
| 469 | chan_read_failed = chan_read_failed_12; |
| 470 | chan_ibuf_empty = chan_ibuf_empty2; |
| 471 | |
| 472 | chan_rcvd_ieof = chan_rcvd_ieof2; |
| 473 | chan_write_failed = chan_write_failed2; |
| 474 | chan_obuf_empty = chan_obuf_empty2; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 475 | } else { |
| 476 | chan_rcvd_oclose = chan_rcvd_oclose1; |
| 477 | chan_read_failed = chan_read_failed_12; |
| 478 | chan_ibuf_empty = chan_ibuf_empty1; |
| 479 | |
| 480 | chan_rcvd_ieof = chan_rcvd_ieof1; |
| 481 | chan_write_failed = chan_write_failed1; |
| 482 | chan_obuf_empty = chan_obuf_empty1; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 483 | } |
| 484 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 485 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | /* helper */ |
| 487 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 488 | chan_shutdown_write(Channel *c) |
| 489 | { |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 490 | buffer_clear(&c->output); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 491 | if (compat20 && c->type == SSH_CHANNEL_LARVAL) |
| 492 | return; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 493 | /* shutdown failure is allowed if write failed already */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 494 | debug("channel %d: close_write", c->self); |
| 495 | if (c->sock != -1) { |
| 496 | if (shutdown(c->sock, SHUT_WR) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 497 | debug("channel %d: chan_shutdown_write: " |
| 498 | "shutdown() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 499 | c->self, c->sock, strerror(errno)); |
| 500 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 501 | if (channel_close_fd(&c->wfd) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 502 | log("channel %d: chan_shutdown_write: " |
| 503 | "close() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 504 | c->self, c->wfd, strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 505 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 506 | } |
| 507 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 508 | chan_shutdown_read(Channel *c) |
| 509 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 510 | if (compat20 && c->type == SSH_CHANNEL_LARVAL) |
| 511 | return; |
| 512 | debug("channel %d: close_read", c->self); |
| 513 | if (c->sock != -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 514 | /* |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 515 | * shutdown(sock, SHUT_READ) may return ENOTCONN if the |
| 516 | * write side has been closed already. (bug on Linux) |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 517 | * HP-UX may return ENOTCONN also. |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 518 | */ |
| 519 | if (shutdown(c->sock, SHUT_RD) < 0 |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 520 | && errno != ENOTCONN) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 521 | error("channel %d: chan_shutdown_read: " |
| 522 | "shutdown() failed for fd%d [i%d o%d]: %.100s", |
| 523 | c->self, c->sock, c->istate, c->ostate, |
Kevin Steves | c3422eb | 2001-09-20 19:33:33 +0000 | [diff] [blame] | 524 | strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 525 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 526 | if (channel_close_fd(&c->rfd) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 527 | log("channel %d: chan_shutdown_read: " |
| 528 | "close() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 529 | c->self, c->rfd, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 530 | } |
| 531 | } |