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