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" |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: nchan.c,v 1.31 2001/07/17 21:04:57 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 | */ |
| 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; |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 126 | #if 0 |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 127 | if (buffer_len(&c->input) == 0) { |
| 128 | debug("channel %d: input: no drain shortcut", c->self); |
| 129 | chan_ibuf_empty(c); |
| 130 | } |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 131 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | break; |
| 133 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 134 | error("channel %d: chan_read_failed for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 135 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 139 | static void |
| 140 | chan_ibuf_empty1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 142 | debug("channel %d: ibuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | if (buffer_len(&c->input)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 144 | error("channel %d: chan_ibuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 145 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | return; |
| 147 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | case CHAN_INPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 150 | debug("channel %d: input drain -> wait_oclose", c->self); |
| 151 | chan_send_ieof1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | c->istate = CHAN_INPUT_WAIT_OCLOSE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | break; |
| 154 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 155 | error("channel %d: chan_ibuf_empty for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 156 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 160 | static void |
| 161 | chan_rcvd_ieof1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 163 | debug("channel %d: rcvd ieof", c->self); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 164 | if (c->type != SSH_CHANNEL_OPEN) { |
| 165 | debug("channel %d: non-open", c->self); |
| 166 | if (c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 167 | debug("channel %d: non-open: input open -> wait_oclose", |
| 168 | c->self); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 169 | chan_shutdown_read(c); |
| 170 | chan_send_ieof1(c); |
| 171 | c->istate = CHAN_INPUT_WAIT_OCLOSE; |
| 172 | } else { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 173 | error("channel %d: non-open: istate %d != open", |
| 174 | c->self, c->istate); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 175 | } |
| 176 | if (c->ostate == CHAN_OUTPUT_OPEN) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 177 | debug("channel %d: non-open: output open -> closed", |
| 178 | c->self); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 179 | chan_send_oclose1(c); |
| 180 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 181 | } else { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 182 | error("channel %d: non-open: ostate %d != open", |
| 183 | c->self, c->ostate); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 184 | } |
| 185 | return; |
| 186 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 188 | case CHAN_OUTPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 189 | debug("channel %d: output open -> drain", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | break; |
| 192 | case CHAN_OUTPUT_WAIT_IEOF: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 193 | debug("channel %d: output wait_ieof -> closed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 195 | break; |
| 196 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 197 | error("channel %d: protocol error: rcvd_ieof for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 198 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 202 | static void |
| 203 | chan_write_failed1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 205 | debug("channel %d: write failed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | case CHAN_OUTPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 208 | debug("channel %d: output open -> wait_ieof", c->self); |
| 209 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | c->ostate = CHAN_OUTPUT_WAIT_IEOF; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | break; |
| 212 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 213 | debug("channel %d: output wait_drain -> closed", c->self); |
| 214 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | break; |
| 217 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 218 | error("channel %d: chan_write_failed for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 219 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 223 | static void |
| 224 | chan_obuf_empty1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 226 | debug("channel %d: obuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 227 | if (buffer_len(&c->output)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 228 | error("channel %d: chan_obuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 229 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | return; |
| 231 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 232 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 233 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 234 | debug("channel %d: output drain -> closed", c->self); |
| 235 | chan_send_oclose1(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | c->ostate = CHAN_OUTPUT_CLOSED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | break; |
| 238 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 239 | error("channel %d: internal error: obuf_empty for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 240 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | static void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 245 | chan_send_ieof1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 247 | debug("channel %d: send ieof", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 248 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 249 | case CHAN_INPUT_OPEN: |
| 250 | case CHAN_INPUT_WAIT_DRAIN: |
| 251 | packet_start(SSH_MSG_CHANNEL_INPUT_EOF); |
| 252 | packet_put_int(c->remote_id); |
| 253 | packet_send(); |
| 254 | break; |
| 255 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 256 | error("channel %d: cannot send ieof for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 257 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | } |
| 261 | static void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 262 | chan_send_oclose1(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 264 | debug("channel %d: send oclose", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 266 | case CHAN_OUTPUT_OPEN: |
| 267 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 268 | chan_shutdown_write(c); |
| 269 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 270 | packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE); |
| 271 | packet_put_int(c->remote_id); |
| 272 | packet_send(); |
| 273 | break; |
| 274 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 275 | error("channel %d: cannot send oclose for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 276 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * the same for SSH2 |
| 283 | */ |
| 284 | static void |
| 285 | chan_rcvd_oclose2(Channel *c) |
| 286 | { |
| 287 | debug("channel %d: rcvd close", c->self); |
| 288 | if (c->flags & CHAN_CLOSE_RCVD) |
| 289 | error("channel %d: protocol error: close rcvd twice", c->self); |
| 290 | c->flags |= CHAN_CLOSE_RCVD; |
| 291 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 292 | /* tear down larval channels immediately */ |
| 293 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 294 | c->istate = CHAN_INPUT_CLOSED; |
| 295 | return; |
| 296 | } |
| 297 | switch (c->ostate) { |
| 298 | case CHAN_OUTPUT_OPEN: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 299 | /* |
| 300 | * wait until a data from the channel is consumed if a CLOSE |
| 301 | * is received |
| 302 | */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 303 | debug("channel %d: output open -> drain", c->self); |
| 304 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
| 305 | break; |
| 306 | } |
| 307 | switch (c->istate) { |
| 308 | case CHAN_INPUT_OPEN: |
| 309 | debug("channel %d: input open -> closed", c->self); |
| 310 | chan_shutdown_read(c); |
| 311 | break; |
| 312 | case CHAN_INPUT_WAIT_DRAIN: |
| 313 | debug("channel %d: input drain -> closed", c->self); |
| 314 | chan_send_eof2(c); |
| 315 | break; |
| 316 | } |
| 317 | c->istate = CHAN_INPUT_CLOSED; |
| 318 | } |
| 319 | static void |
| 320 | chan_ibuf_empty2(Channel *c) |
| 321 | { |
| 322 | debug("channel %d: ibuf empty", c->self); |
| 323 | if (buffer_len(&c->input)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 324 | error("channel %d: chan_ibuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 325 | c->self); |
| 326 | return; |
| 327 | } |
| 328 | switch (c->istate) { |
| 329 | case CHAN_INPUT_WAIT_DRAIN: |
| 330 | debug("channel %d: input drain -> closed", c->self); |
| 331 | if (!(c->flags & CHAN_CLOSE_SENT)) |
| 332 | chan_send_eof2(c); |
| 333 | c->istate = CHAN_INPUT_CLOSED; |
| 334 | break; |
| 335 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 336 | error("channel %d: chan_ibuf_empty for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 337 | c->self, c->istate); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | static void |
| 342 | chan_rcvd_ieof2(Channel *c) |
| 343 | { |
| 344 | debug("channel %d: rcvd eof", c->self); |
| 345 | if (c->ostate == CHAN_OUTPUT_OPEN) { |
| 346 | debug("channel %d: output open -> drain", c->self); |
| 347 | c->ostate = CHAN_OUTPUT_WAIT_DRAIN; |
| 348 | } |
| 349 | } |
| 350 | static void |
| 351 | chan_write_failed2(Channel *c) |
| 352 | { |
| 353 | debug("channel %d: write failed", c->self); |
| 354 | switch (c->ostate) { |
| 355 | case CHAN_OUTPUT_OPEN: |
| 356 | debug("channel %d: output open -> closed", c->self); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 357 | chan_shutdown_write(c); /* ?? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 358 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 359 | break; |
| 360 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 361 | debug("channel %d: output drain -> closed", c->self); |
| 362 | chan_shutdown_write(c); |
| 363 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 364 | break; |
| 365 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 366 | error("channel %d: chan_write_failed for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 367 | c->self, c->ostate); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | static void |
| 372 | chan_obuf_empty2(Channel *c) |
| 373 | { |
| 374 | debug("channel %d: obuf empty", c->self); |
| 375 | if (buffer_len(&c->output)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 376 | error("channel %d: chan_obuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 377 | c->self); |
| 378 | return; |
| 379 | } |
| 380 | switch (c->ostate) { |
| 381 | case CHAN_OUTPUT_WAIT_DRAIN: |
| 382 | debug("channel %d: output drain -> closed", c->self); |
| 383 | chan_shutdown_write(c); |
| 384 | c->ostate = CHAN_OUTPUT_CLOSED; |
| 385 | break; |
| 386 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 387 | error("channel %d: chan_obuf_empty for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 388 | c->self, c->ostate); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | static void |
| 393 | chan_send_eof2(Channel *c) |
| 394 | { |
| 395 | debug("channel %d: send eof", c->self); |
| 396 | switch (c->istate) { |
| 397 | case CHAN_INPUT_WAIT_DRAIN: |
| 398 | packet_start(SSH2_MSG_CHANNEL_EOF); |
| 399 | packet_put_int(c->remote_id); |
| 400 | packet_send(); |
| 401 | break; |
| 402 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 403 | error("channel %d: cannot send eof for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 404 | c->self, c->istate); |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | static void |
| 409 | chan_send_close2(Channel *c) |
| 410 | { |
| 411 | debug("channel %d: send close", c->self); |
| 412 | if (c->ostate != CHAN_OUTPUT_CLOSED || |
| 413 | c->istate != CHAN_INPUT_CLOSED) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 414 | error("channel %d: cannot send close for istate/ostate %d/%d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 415 | c->self, c->istate, c->ostate); |
| 416 | } else if (c->flags & CHAN_CLOSE_SENT) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 417 | error("channel %d: already sent close", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 418 | } else { |
| 419 | packet_start(SSH2_MSG_CHANNEL_CLOSE); |
| 420 | packet_put_int(c->remote_id); |
| 421 | packet_send(); |
| 422 | c->flags |= CHAN_CLOSE_SENT; |
| 423 | } |
| 424 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 425 | |
| 426 | /* shared */ |
| 427 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 428 | void |
| 429 | chan_mark_dead(Channel *c) |
| 430 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 431 | c->type = SSH_CHANNEL_ZOMBIE; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 434 | int |
| 435 | chan_is_dead(Channel *c) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 436 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 437 | if (c->type == SSH_CHANNEL_ZOMBIE) { |
| 438 | debug("channel %d: zombie", c->self); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 439 | return 1; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 440 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 441 | if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED) |
| 442 | return 0; |
| 443 | if (!compat20) { |
| 444 | debug("channel %d: is dead", c->self); |
| 445 | return 1; |
| 446 | } |
| 447 | /* |
| 448 | * we have to delay the close message if the efd (for stderr) is |
| 449 | * still active |
| 450 | */ |
| 451 | if (((c->extended_usage != CHAN_EXTENDED_IGNORE) && |
| 452 | buffer_len(&c->extended) > 0) |
| 453 | #if 0 |
| 454 | || ((c->extended_usage == CHAN_EXTENDED_READ) && |
| 455 | c->efd != -1) |
| 456 | #endif |
| 457 | ) { |
| 458 | debug2("channel %d: active efd: %d len %d type %s", |
| 459 | c->self, c->efd, buffer_len(&c->extended), |
| 460 | c->extended_usage==CHAN_EXTENDED_READ ? |
| 461 | "read": "write"); |
| 462 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 463 | if (!(c->flags & CHAN_CLOSE_SENT)) { |
| 464 | chan_send_close2(c); |
| 465 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 466 | if ((c->flags & CHAN_CLOSE_SENT) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 467 | (c->flags & CHAN_CLOSE_RCVD)) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 468 | debug("channel %d: is dead", c->self); |
| 469 | return 1; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 470 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 471 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 472 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 473 | } |
| 474 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 475 | void |
| 476 | chan_init_iostates(Channel *c) |
| 477 | { |
| 478 | c->ostate = CHAN_OUTPUT_OPEN; |
| 479 | c->istate = CHAN_INPUT_OPEN; |
| 480 | c->flags = 0; |
| 481 | } |
| 482 | |
| 483 | /* init */ |
| 484 | void |
| 485 | chan_init(void) |
| 486 | { |
| 487 | if (compat20) { |
| 488 | chan_rcvd_oclose = chan_rcvd_oclose2; |
| 489 | chan_read_failed = chan_read_failed_12; |
| 490 | chan_ibuf_empty = chan_ibuf_empty2; |
| 491 | |
| 492 | chan_rcvd_ieof = chan_rcvd_ieof2; |
| 493 | chan_write_failed = chan_write_failed2; |
| 494 | chan_obuf_empty = chan_obuf_empty2; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 495 | } else { |
| 496 | chan_rcvd_oclose = chan_rcvd_oclose1; |
| 497 | chan_read_failed = chan_read_failed_12; |
| 498 | chan_ibuf_empty = chan_ibuf_empty1; |
| 499 | |
| 500 | chan_rcvd_ieof = chan_rcvd_ieof1; |
| 501 | chan_write_failed = chan_write_failed1; |
| 502 | chan_obuf_empty = chan_obuf_empty1; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 503 | } |
| 504 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 505 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 506 | /* helper */ |
| 507 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 508 | chan_shutdown_write(Channel *c) |
| 509 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 510 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 511 | if (compat20 && c->type == SSH_CHANNEL_LARVAL) |
| 512 | return; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 513 | /* shutdown failure is allowed if write failed already */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 514 | debug("channel %d: close_write", c->self); |
| 515 | if (c->sock != -1) { |
| 516 | if (shutdown(c->sock, SHUT_WR) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 517 | debug("channel %d: chan_shutdown_write: " |
| 518 | "shutdown() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 519 | c->self, c->sock, strerror(errno)); |
| 520 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 521 | if (channel_close_fd(&c->wfd) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 522 | log("channel %d: chan_shutdown_write: " |
| 523 | "close() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 524 | c->self, c->wfd, strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 525 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 526 | } |
| 527 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 528 | chan_shutdown_read(Channel *c) |
| 529 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 530 | if (compat20 && c->type == SSH_CHANNEL_LARVAL) |
| 531 | return; |
| 532 | debug("channel %d: close_read", c->self); |
| 533 | if (c->sock != -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 534 | /* |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 535 | * shutdown(sock, SHUT_READ) may return ENOTCONN if the |
| 536 | * write side has been closed already. (bug on Linux) |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 537 | * HP-UX may return ENOTCONN also. |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 538 | */ |
| 539 | if (shutdown(c->sock, SHUT_RD) < 0 |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 540 | && errno != ENOTCONN) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 541 | error("channel %d: chan_shutdown_read: " |
| 542 | "shutdown() failed for fd%d [i%d o%d]: %.100s", |
| 543 | c->self, c->sock, c->istate, c->ostate, |
| 544 | strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 545 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 546 | if (channel_close_fd(&c->rfd) < 0) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 547 | log("channel %d: chan_shutdown_read: " |
| 548 | "close() failed for fd%d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 549 | c->self, c->rfd, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 550 | } |
| 551 | } |