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