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