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