blob: ca9a56b596dc437ec7d8e34f8fba1a2a767082f4 [file] [log] [blame]
Damien Miller5428f641999-11-25 11:54:57 +11001/*
Damien Millerb51ed392002-01-22 23:29:03 +11002 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller5428f641999-11-25 11:54:57 +110012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "ssh1.h"
28#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "buffer.h"
30#include "packet.h"
31#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100032#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
Ben Lindstrom3b670d02001-06-09 00:57:39 +000035/*
36 * SSH Protocol 1.5 aka New Channel Protocol
37 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
38 * Written by Markus Friedl in October 1999
39 *
40 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
41 * tear down of channels:
42 *
43 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100044 * CLOSE ->
45 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000046 *
47 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100048 * IEOF ->
49 * <- OCLOSE
50 * <- IEOF
51 * OCLOSE ->
52 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000053 *
54 * 2.0: the EOF messages are optional
55 *
56 * See the debugging output from 'ssh -v' and 'sshd -d' of
57 * ssh-1.2.27 as an example.
58 *
59 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000060
Damien Miller33b13562000-04-04 14:38:59 +100061/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062/*
Damien Miller95def091999-11-25 00:26:21 +110063 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064 */
Damien Miller33b13562000-04-04 14:38:59 +100065/*
66 * ACTIONS: should never update the channel states
67 */
Ben Lindstrombba81212001-06-25 05:01:22 +000068static void chan_send_ieof1(Channel *);
69static void chan_send_oclose1(Channel *);
70static void chan_send_close2(Channel *);
71static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100072
Damien Miller33b13562000-04-04 14:38:59 +100073/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000074static void chan_shutdown_write(Channel *);
75static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100076
Damien Millerabea8ee2002-01-22 23:27:11 +110077static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
78static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
79
80static void
81chan_set_istate(Channel *c, u_int next)
82{
83 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
84 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100085 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110086 istates[next]);
87 c->istate = next;
88}
89static void
90chan_set_ostate(Channel *c, u_int next)
91{
92 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
93 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100094 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +110095 ostates[next]);
96 c->ostate = next;
97}
98
Damien Miller33b13562000-04-04 14:38:59 +100099/*
100 * SSH1 specific implementation of event functions
101 */
102
103static void
104chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100105{
Damien Millerfbdeece2003-09-02 22:52:31 +1000106 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100107 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100109 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110 break;
111 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000113 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100114 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100115 break;
116 case CHAN_INPUT_WAIT_DRAIN:
117 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000118 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100119 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 break;
121 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000122 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000123 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100124 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 }
126}
Damien Miller5144df92002-01-22 23:28:45 +1100127void
128chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100129{
Damien Millerfbdeece2003-09-02 22:52:31 +1000130 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100131 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100134 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135 break;
136 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000137 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000138 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 break;
140 }
141}
Damien Miller5144df92002-01-22 23:28:45 +1100142void
143chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100144{
Damien Millerfbdeece2003-09-02 22:52:31 +1000145 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100146 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000147 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000148 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149 return;
150 }
Damien Miller95def091999-11-25 00:26:21 +1100151 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100153 if (compat20) {
154 if (!(c->flags & CHAN_CLOSE_SENT))
155 chan_send_eof2(c);
156 chan_set_istate(c, CHAN_INPUT_CLOSED);
157 } else {
158 chan_send_ieof1(c);
159 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 break;
162 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000163 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000164 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165 break;
166 }
167}
Damien Miller33b13562000-04-04 14:38:59 +1000168static void
169chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100170{
Damien Millerfbdeece2003-09-02 22:52:31 +1000171 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100172 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100174 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175 break;
176 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100177 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178 break;
179 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000180 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000181 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182 break;
183 }
184}
Damien Miller33b13562000-04-04 14:38:59 +1000185static void
186chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100187{
Damien Millerfbdeece2003-09-02 22:52:31 +1000188 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100189 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100191 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000192 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100193 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194 break;
195 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100196 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000197 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100198 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199 break;
200 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000201 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000202 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 break;
204 }
205}
Damien Miller5144df92002-01-22 23:28:45 +1100206void
207chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100208{
Damien Millerfbdeece2003-09-02 22:52:31 +1000209 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100210 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000211 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000212 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213 return;
214 }
Damien Miller95def091999-11-25 00:26:21 +1100215 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100217 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100218 if (!compat20)
219 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100220 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221 break;
222 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000223 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000224 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225 break;
226 }
227}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228static void
Damien Miller33b13562000-04-04 14:38:59 +1000229chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100230{
Damien Millerfbdeece2003-09-02 22:52:31 +1000231 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100232 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233 case CHAN_INPUT_OPEN:
234 case CHAN_INPUT_WAIT_DRAIN:
235 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
236 packet_put_int(c->remote_id);
237 packet_send();
238 break;
239 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000240 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000241 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242 break;
243 }
244}
245static void
Damien Miller33b13562000-04-04 14:38:59 +1000246chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100247{
Damien Millerfbdeece2003-09-02 22:52:31 +1000248 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100249 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 case CHAN_OUTPUT_OPEN:
251 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100252 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
254 packet_put_int(c->remote_id);
255 packet_send();
256 break;
257 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000258 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100259 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260 break;
261 }
262}
Damien Miller33b13562000-04-04 14:38:59 +1000263
264/*
265 * the same for SSH2
266 */
267static void
Damien Millerebc11d32002-01-22 23:28:13 +1100268chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000269{
Damien Millerfbdeece2003-09-02 22:52:31 +1000270 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000271 if (c->flags & CHAN_CLOSE_RCVD)
272 error("channel %d: protocol error: close rcvd twice", c->self);
273 c->flags |= CHAN_CLOSE_RCVD;
274 if (c->type == SSH_CHANNEL_LARVAL) {
275 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100276 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
277 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000278 return;
279 }
280 switch (c->ostate) {
281 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000282 /*
283 * wait until a data from the channel is consumed if a CLOSE
284 * is received
285 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100286 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000287 break;
288 }
289 switch (c->istate) {
290 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000291 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100292 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000293 break;
294 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000295 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100296 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000297 break;
298 }
Damien Miller33b13562000-04-04 14:38:59 +1000299}
300static void
Damien Millerebc11d32002-01-22 23:28:13 +1100301chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000302{
Damien Millerfbdeece2003-09-02 22:52:31 +1000303 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000304 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100305 if (c->ostate == CHAN_OUTPUT_OPEN)
306 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000307}
308static void
309chan_write_failed2(Channel *c)
310{
Damien Millerfbdeece2003-09-02 22:52:31 +1000311 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000312 switch (c->ostate) {
313 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000314 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000315 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100316 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000317 break;
318 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000319 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000320 c->self, c->ostate);
321 break;
322 }
323}
324static void
Damien Miller33b13562000-04-04 14:38:59 +1000325chan_send_eof2(Channel *c)
326{
Damien Millerfbdeece2003-09-02 22:52:31 +1000327 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000328 switch (c->istate) {
329 case CHAN_INPUT_WAIT_DRAIN:
330 packet_start(SSH2_MSG_CHANNEL_EOF);
331 packet_put_int(c->remote_id);
332 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000333 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000334 break;
335 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000336 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000337 c->self, c->istate);
338 break;
339 }
340}
341static void
342chan_send_close2(Channel *c)
343{
Damien Millerfbdeece2003-09-02 22:52:31 +1000344 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000345 if (c->ostate != CHAN_OUTPUT_CLOSED ||
346 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000347 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000348 c->self, c->istate, c->ostate);
349 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000350 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000351 } else {
352 packet_start(SSH2_MSG_CHANNEL_CLOSE);
353 packet_put_int(c->remote_id);
354 packet_send();
355 c->flags |= CHAN_CLOSE_SENT;
356 }
357}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000358
359/* shared */
360
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000361void
Damien Miller5144df92002-01-22 23:28:45 +1100362chan_rcvd_ieof(Channel *c)
363{
364 if (compat20)
365 chan_rcvd_eof2(c);
366 else
367 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100368 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000369 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000370 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100371 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100372}
373void
374chan_rcvd_oclose(Channel *c)
375{
376 if (compat20)
377 chan_rcvd_close2(c);
378 else
379 chan_rcvd_oclose1(c);
380}
381void
382chan_write_failed(Channel *c)
383{
384 if (compat20)
385 chan_write_failed2(c);
386 else
387 chan_write_failed1(c);
388}
389
390void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000391chan_mark_dead(Channel *c)
392{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000393 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000394}
395
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000396int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000397chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000398{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000399 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000400 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000401 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000402 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000403 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
404 return 0;
405 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000406 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000407 return 1;
408 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000409 if ((datafellows & SSH_BUG_EXTEOF) &&
410 c->extended_usage == CHAN_EXTENDED_WRITE &&
411 c->efd != -1 &&
412 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000413 debug2("channel %d: active efd: %d len %d",
414 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000415 return 0;
416 }
417 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000418 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000419 chan_send_close2(c);
420 } else {
421 /* channel would be dead if we sent a close */
422 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000423 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000424 c->self);
425 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000426 }
Damien Miller33b13562000-04-04 14:38:59 +1000427 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000428 }
429 if ((c->flags & CHAN_CLOSE_SENT) &&
430 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000431 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000432 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000433 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000434 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000435}
436
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437/* helper */
438static void
Damien Miller95def091999-11-25 00:26:21 +1100439chan_shutdown_write(Channel *c)
440{
Damien Miller76765c02002-01-22 23:21:15 +1100441 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000442 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
443 return;
Damien Miller5428f641999-11-25 11:54:57 +1100444 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000445 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000446 if (c->sock != -1) {
447 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000448 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000449 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000450 c->self, c->sock, strerror(errno));
451 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000452 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000453 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000454 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000455 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000456 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
458static void
Damien Miller95def091999-11-25 00:26:21 +1100459chan_shutdown_read(Channel *c)
460{
Damien Miller33b13562000-04-04 14:38:59 +1000461 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
462 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000463 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000464 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000465 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000466 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
467 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000468 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000469 */
470 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000471 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000472 error("channel %d: chan_shutdown_read: "
473 "shutdown() failed for fd%d [i%d o%d]: %.100s",
474 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000475 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000476 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000477 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000478 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000479 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000480 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481 }
482}