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