djm@openbsd.org | 930e8d2 | 2017-04-30 23:28:41 +0000 | [diff] [blame] | 1 | /* $OpenBSD: nchan.c,v 1.65 2017/04/30 23:28:42 djm 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 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 35 | #include "openbsd-compat/sys-queue.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 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_eof2(Channel *); |
Damien Miller | bab9bd4 | 2008-05-19 16:06:47 +1000 | [diff] [blame] | 77 | static void chan_send_eow2(Channel *); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 78 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 79 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 80 | static void chan_shutdown_write(Channel *); |
| 81 | static void chan_shutdown_read(Channel *); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 82 | |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 83 | static char *ostates[] = { "open", "drain", "wait_ieof", "closed" }; |
| 84 | static char *istates[] = { "open", "drain", "wait_oclose", "closed" }; |
| 85 | |
| 86 | static void |
| 87 | chan_set_istate(Channel *c, u_int next) |
| 88 | { |
| 89 | if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED) |
| 90 | fatal("chan_set_istate: bad state %d -> %d", c->istate, next); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 91 | debug2("channel %d: input %s -> %s", c->self, istates[c->istate], |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 92 | istates[next]); |
| 93 | c->istate = next; |
| 94 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 95 | |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 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 | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 106 | void |
| 107 | chan_read_failed(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 109 | debug2("channel %d: read failed", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | case CHAN_INPUT_OPEN: |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | chan_shutdown_read(c); |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 113 | chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | break; |
| 115 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 116 | error("channel %d: chan_read_failed for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 117 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 121 | |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 122 | void |
| 123 | chan_ibuf_empty(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 125 | debug2("channel %d: ibuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | if (buffer_len(&c->input)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 127 | error("channel %d: chan_ibuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 128 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | return; |
| 130 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | case CHAN_INPUT_WAIT_DRAIN: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 133 | if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL))) |
| 134 | chan_send_eof2(c); |
| 135 | chan_set_istate(c, CHAN_INPUT_CLOSED); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | break; |
| 137 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 138 | error("channel %d: chan_ibuf_empty for istate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 139 | c->self, c->istate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 143 | |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 144 | void |
| 145 | chan_obuf_empty(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 147 | debug2("channel %d: obuf empty", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | if (buffer_len(&c->output)) { |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 149 | error("channel %d: chan_obuf_empty for non empty buffer", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 150 | c->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | return; |
| 152 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | switch (c->ostate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 154 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 89a9232 | 2002-01-22 23:27:28 +1100 | [diff] [blame] | 155 | chan_shutdown_write(c); |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 156 | chan_set_ostate(c, CHAN_OUTPUT_CLOSED); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | break; |
| 158 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 159 | error("channel %d: internal error: obuf_empty for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 160 | c->self, c->ostate); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 164 | |
| 165 | void |
| 166 | chan_rcvd_eow(Channel *c) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 168 | debug2("channel %d: rcvd eow", c->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | switch (c->istate) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | case CHAN_INPUT_OPEN: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 171 | chan_shutdown_read(c); |
| 172 | chan_set_istate(c, CHAN_INPUT_CLOSED); |
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 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 177 | static void |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 178 | chan_send_eof2(Channel *c) |
| 179 | { |
| 180 | debug2("channel %d: send eof", c->self); |
| 181 | switch (c->istate) { |
| 182 | case CHAN_INPUT_WAIT_DRAIN: |
| 183 | packet_start(SSH2_MSG_CHANNEL_EOF); |
| 184 | packet_put_int(c->remote_id); |
| 185 | packet_send(); |
| 186 | c->flags |= CHAN_EOF_SENT; |
| 187 | break; |
| 188 | default: |
| 189 | error("channel %d: cannot send eof for istate %d", |
| 190 | c->self, c->istate); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | chan_send_close2(Channel *c) |
| 197 | { |
| 198 | debug2("channel %d: send close", c->self); |
| 199 | if (c->ostate != CHAN_OUTPUT_CLOSED || |
| 200 | c->istate != CHAN_INPUT_CLOSED) { |
| 201 | error("channel %d: cannot send close for istate/ostate %d/%d", |
| 202 | c->self, c->istate, c->ostate); |
| 203 | } else if (c->flags & CHAN_CLOSE_SENT) { |
| 204 | error("channel %d: already sent close", c->self); |
| 205 | } else { |
| 206 | packet_start(SSH2_MSG_CHANNEL_CLOSE); |
| 207 | packet_put_int(c->remote_id); |
| 208 | packet_send(); |
| 209 | c->flags |= CHAN_CLOSE_SENT; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | chan_send_eow2(Channel *c) |
| 215 | { |
| 216 | debug2("channel %d: send eow", c->self); |
| 217 | if (c->ostate == CHAN_OUTPUT_CLOSED) { |
| 218 | error("channel %d: must not sent eow on closed output", |
| 219 | c->self); |
| 220 | return; |
| 221 | } |
| 222 | if (!(datafellows & SSH_NEW_OPENSSH)) |
| 223 | return; |
| 224 | packet_start(SSH2_MSG_CHANNEL_REQUEST); |
| 225 | packet_put_int(c->remote_id); |
| 226 | packet_put_cstring("eow@openssh.com"); |
| 227 | packet_put_char(0); |
| 228 | packet_send(); |
| 229 | } |
| 230 | |
| 231 | /* shared */ |
| 232 | |
| 233 | void |
| 234 | chan_rcvd_ieof(Channel *c) |
| 235 | { |
| 236 | debug2("channel %d: rcvd eof", c->self); |
| 237 | c->flags |= CHAN_EOF_RCVD; |
| 238 | if (c->ostate == CHAN_OUTPUT_OPEN) |
| 239 | chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN); |
| 240 | if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN && |
| 241 | buffer_len(&c->output) == 0 && |
| 242 | !CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
| 243 | chan_obuf_empty(c); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | chan_rcvd_oclose(Channel *c) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 248 | { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 249 | debug2("channel %d: rcvd close", c->self); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 250 | if (!(c->flags & CHAN_LOCAL)) { |
| 251 | if (c->flags & CHAN_CLOSE_RCVD) |
| 252 | error("channel %d: protocol error: close rcvd twice", |
| 253 | c->self); |
| 254 | c->flags |= CHAN_CLOSE_RCVD; |
| 255 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 256 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 257 | /* tear down larval channels immediately */ |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 258 | chan_set_ostate(c, CHAN_OUTPUT_CLOSED); |
| 259 | chan_set_istate(c, CHAN_INPUT_CLOSED); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | switch (c->ostate) { |
| 263 | case CHAN_OUTPUT_OPEN: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 264 | /* |
| 265 | * wait until a data from the channel is consumed if a CLOSE |
| 266 | * is received |
| 267 | */ |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 268 | chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | switch (c->istate) { |
| 272 | case CHAN_INPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 273 | chan_shutdown_read(c); |
Damien Miller | ebc11d3 | 2002-01-22 23:28:13 +1100 | [diff] [blame] | 274 | chan_set_istate(c, CHAN_INPUT_CLOSED); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 275 | break; |
| 276 | case CHAN_INPUT_WAIT_DRAIN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 277 | if (!(c->flags & CHAN_LOCAL)) |
| 278 | chan_send_eof2(c); |
Damien Miller | ebc11d3 | 2002-01-22 23:28:13 +1100 | [diff] [blame] | 279 | chan_set_istate(c, CHAN_INPUT_CLOSED); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 280 | break; |
| 281 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 282 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 283 | |
Damien Miller | bab9bd4 | 2008-05-19 16:06:47 +1000 | [diff] [blame] | 284 | void |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 285 | chan_write_failed(Channel *c) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 286 | { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 287 | debug2("channel %d: write failed", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 288 | switch (c->ostate) { |
| 289 | case CHAN_OUTPUT_OPEN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 290 | case CHAN_OUTPUT_WAIT_DRAIN: |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 291 | chan_shutdown_write(c); |
Darren Tucker | 8748b96 | 2008-07-02 22:32:43 +1000 | [diff] [blame] | 292 | if (strcmp(c->ctype, "session") == 0) |
| 293 | chan_send_eow2(c); |
Damien Miller | abea8ee | 2002-01-22 23:27:11 +1100 | [diff] [blame] | 294 | chan_set_ostate(c, CHAN_OUTPUT_CLOSED); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 295 | break; |
| 296 | default: |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 297 | error("channel %d: chan_write_failed for ostate %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 298 | c->self, c->ostate); |
| 299 | break; |
| 300 | } |
| 301 | } |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 302 | |
| 303 | void |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 304 | chan_mark_dead(Channel *c) |
| 305 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 306 | c->type = SSH_CHANNEL_ZOMBIE; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 309 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 310 | chan_is_dead(Channel *c, int do_send) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 311 | { |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 312 | if (c->type == SSH_CHANNEL_ZOMBIE) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 313 | debug2("channel %d: zombie", c->self); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 314 | return 1; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 315 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 316 | if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED) |
| 317 | return 0; |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 318 | if ((datafellows & SSH_BUG_EXTEOF) && |
| 319 | c->extended_usage == CHAN_EXTENDED_WRITE && |
| 320 | c->efd != -1 && |
| 321 | buffer_len(&c->extended) > 0) { |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 322 | debug2("channel %d: active efd: %d len %d", |
| 323 | c->self, c->efd, buffer_len(&c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 326 | if (c->flags & CHAN_LOCAL) { |
| 327 | debug2("channel %d: is dead (local)", c->self); |
| 328 | return 1; |
| 329 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 330 | if (!(c->flags & CHAN_CLOSE_SENT)) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 331 | if (do_send) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 332 | chan_send_close2(c); |
| 333 | } else { |
| 334 | /* channel would be dead if we sent a close */ |
| 335 | if (c->flags & CHAN_CLOSE_RCVD) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 336 | debug2("channel %d: almost dead", |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 337 | c->self); |
| 338 | return 1; |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 339 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 340 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 341 | } |
| 342 | if ((c->flags & CHAN_CLOSE_SENT) && |
| 343 | (c->flags & CHAN_CLOSE_RCVD)) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 344 | debug2("channel %d: is dead", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 345 | return 1; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 346 | } |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 347 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 348 | } |
| 349 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 350 | /* helper */ |
| 351 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 352 | chan_shutdown_write(Channel *c) |
| 353 | { |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 354 | buffer_clear(&c->output); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 355 | if (c->type == SSH_CHANNEL_LARVAL) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 356 | return; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 357 | /* shutdown failure is allowed if write failed already */ |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 358 | debug2("channel %d: close_write", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 359 | if (c->sock != -1) { |
| 360 | if (shutdown(c->sock, SHUT_WR) < 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 361 | debug2("channel %d: chan_shutdown_write: " |
Darren Tucker | b57fab6 | 2008-11-11 16:32:25 +1100 | [diff] [blame] | 362 | "shutdown() failed for fd %d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 363 | c->self, c->sock, strerror(errno)); |
| 364 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 365 | if (channel_close_fd(&c->wfd) < 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 366 | logit("channel %d: chan_shutdown_write: " |
Darren Tucker | b57fab6 | 2008-11-11 16:32:25 +1100 | [diff] [blame] | 367 | "close() failed for fd %d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 368 | c->self, c->wfd, strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 369 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 370 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 371 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 372 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | chan_shutdown_read(Channel *c) |
| 374 | { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 375 | if (c->type == SSH_CHANNEL_LARVAL) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 376 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 377 | debug2("channel %d: close_read", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 378 | if (c->sock != -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 379 | /* |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 380 | * shutdown(sock, SHUT_READ) may return ENOTCONN if the |
| 381 | * write side has been closed already. (bug on Linux) |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 382 | * HP-UX may return ENOTCONN also. |
Damien Miller | 0f091bd | 2000-08-07 15:47:48 +1000 | [diff] [blame] | 383 | */ |
| 384 | if (shutdown(c->sock, SHUT_RD) < 0 |
Kevin Steves | 871f662 | 2001-09-18 16:08:24 +0000 | [diff] [blame] | 385 | && errno != ENOTCONN) |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 386 | error("channel %d: chan_shutdown_read: " |
Darren Tucker | b57fab6 | 2008-11-11 16:32:25 +1100 | [diff] [blame] | 387 | "shutdown() failed for fd %d [i%d o%d]: %.100s", |
Ben Lindstrom | 3b670d0 | 2001-06-09 00:57:39 +0000 | [diff] [blame] | 388 | c->self, c->sock, c->istate, c->ostate, |
Kevin Steves | c3422eb | 2001-09-20 19:33:33 +0000 | [diff] [blame] | 389 | strerror(errno)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 390 | } else { |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 391 | if (channel_close_fd(&c->rfd) < 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 392 | logit("channel %d: chan_shutdown_read: " |
Darren Tucker | b57fab6 | 2008-11-11 16:32:25 +1100 | [diff] [blame] | 393 | "close() failed for fd %d: %.100s", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 394 | c->self, c->rfd, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | } |
| 396 | } |