blob: ecf59c5dbcc814731484223e55bf0dde455f79c3 [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"
Darren Tucker3f9fdc72004-06-22 12:56:01 +100026RCSID("$OpenBSD: nchan.c,v 1.50 2004/06/21 17:36:31 avsm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "ssh1.h"
29#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "buffer.h"
31#include "packet.h"
32#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100033#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
Ben Lindstrom3b670d02001-06-09 00:57:39 +000036/*
37 * SSH Protocol 1.5 aka New Channel Protocol
38 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
39 * Written by Markus Friedl in October 1999
40 *
41 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
42 * tear down of channels:
43 *
44 * 1.3: strict request-ack-protocol:
45 * CLOSE ->
46 * <- CLOSE_CONFIRM
47 *
48 * 1.5: uses variations of:
49 * IEOF ->
50 * <- OCLOSE
51 * <- IEOF
52 * OCLOSE ->
53 * i.e. both sides have to close the channel
54 *
55 * 2.0: the EOF messages are optional
56 *
57 * See the debugging output from 'ssh -v' and 'sshd -d' of
58 * ssh-1.2.27 as an example.
59 *
60 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000061
Damien Miller33b13562000-04-04 14:38:59 +100062/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063/*
Damien Miller95def091999-11-25 00:26:21 +110064 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065 */
Damien Miller33b13562000-04-04 14:38:59 +100066/*
67 * ACTIONS: should never update the channel states
68 */
Ben Lindstrombba81212001-06-25 05:01:22 +000069static void chan_send_ieof1(Channel *);
70static void chan_send_oclose1(Channel *);
71static void chan_send_close2(Channel *);
72static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100073
Damien Miller33b13562000-04-04 14:38:59 +100074/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000075static void chan_shutdown_write(Channel *);
76static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100077
Damien Millerabea8ee2002-01-22 23:27:11 +110078static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
79static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
80
81static void
82chan_set_istate(Channel *c, u_int next)
83{
84 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
85 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100086 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110087 istates[next]);
88 c->istate = next;
89}
90static void
91chan_set_ostate(Channel *c, u_int next)
92{
93 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
94 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100095 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +110096 ostates[next]);
97 c->ostate = next;
98}
99
Damien Miller33b13562000-04-04 14:38:59 +1000100/*
101 * SSH1 specific implementation of event functions
102 */
103
104static void
105chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100106{
Damien Millerfbdeece2003-09-02 22:52:31 +1000107 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100108 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100110 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111 break;
112 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000114 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100115 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100116 break;
117 case CHAN_INPUT_WAIT_DRAIN:
118 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000119 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100120 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 break;
122 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000123 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000124 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100125 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 }
127}
Damien Miller5144df92002-01-22 23:28:45 +1100128void
129chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100130{
Damien Millerfbdeece2003-09-02 22:52:31 +1000131 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100132 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100135 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136 break;
137 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000138 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000139 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 break;
141 }
142}
Damien Miller5144df92002-01-22 23:28:45 +1100143void
144chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100145{
Damien Millerfbdeece2003-09-02 22:52:31 +1000146 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100147 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000148 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000149 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150 return;
151 }
Damien Miller95def091999-11-25 00:26:21 +1100152 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100154 if (compat20) {
155 if (!(c->flags & CHAN_CLOSE_SENT))
156 chan_send_eof2(c);
157 chan_set_istate(c, CHAN_INPUT_CLOSED);
158 } else {
159 chan_send_ieof1(c);
160 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
161 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162 break;
163 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000164 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000165 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166 break;
167 }
168}
Damien Miller33b13562000-04-04 14:38:59 +1000169static void
170chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100171{
Damien Millerfbdeece2003-09-02 22:52:31 +1000172 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100173 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100175 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176 break;
177 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100178 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179 break;
180 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000181 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000182 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183 break;
184 }
185}
Damien Miller33b13562000-04-04 14:38:59 +1000186static void
187chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100188{
Damien Millerfbdeece2003-09-02 22:52:31 +1000189 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100190 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100192 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000193 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100194 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195 break;
196 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100197 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000198 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100199 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200 break;
201 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000202 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000203 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204 break;
205 }
206}
Damien Miller5144df92002-01-22 23:28:45 +1100207void
208chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100209{
Damien Millerfbdeece2003-09-02 22:52:31 +1000210 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100211 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000212 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000213 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214 return;
215 }
Damien Miller95def091999-11-25 00:26:21 +1100216 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100218 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100219 if (!compat20)
220 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100221 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 break;
223 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000224 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000225 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226 break;
227 }
228}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229static void
Damien Miller33b13562000-04-04 14:38:59 +1000230chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100231{
Damien Millerfbdeece2003-09-02 22:52:31 +1000232 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100233 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234 case CHAN_INPUT_OPEN:
235 case CHAN_INPUT_WAIT_DRAIN:
236 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
237 packet_put_int(c->remote_id);
238 packet_send();
239 break;
240 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000241 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000242 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 break;
244 }
245}
246static void
Damien Miller33b13562000-04-04 14:38:59 +1000247chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100248{
Damien Millerfbdeece2003-09-02 22:52:31 +1000249 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100250 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251 case CHAN_OUTPUT_OPEN:
252 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100253 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
255 packet_put_int(c->remote_id);
256 packet_send();
257 break;
258 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000259 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100260 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261 break;
262 }
263}
Damien Miller33b13562000-04-04 14:38:59 +1000264
265/*
266 * the same for SSH2
267 */
268static void
Damien Millerebc11d32002-01-22 23:28:13 +1100269chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000270{
Damien Millerfbdeece2003-09-02 22:52:31 +1000271 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000272 if (c->flags & CHAN_CLOSE_RCVD)
273 error("channel %d: protocol error: close rcvd twice", c->self);
274 c->flags |= CHAN_CLOSE_RCVD;
275 if (c->type == SSH_CHANNEL_LARVAL) {
276 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100277 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
278 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000279 return;
280 }
281 switch (c->ostate) {
282 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000283 /*
284 * wait until a data from the channel is consumed if a CLOSE
285 * is received
286 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100287 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000288 break;
289 }
290 switch (c->istate) {
291 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000292 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100293 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000294 break;
295 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000296 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100297 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000298 break;
299 }
Damien Miller33b13562000-04-04 14:38:59 +1000300}
301static void
Damien Millerebc11d32002-01-22 23:28:13 +1100302chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000303{
Damien Millerfbdeece2003-09-02 22:52:31 +1000304 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000305 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100306 if (c->ostate == CHAN_OUTPUT_OPEN)
307 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000308}
309static void
310chan_write_failed2(Channel *c)
311{
Damien Millerfbdeece2003-09-02 22:52:31 +1000312 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000313 switch (c->ostate) {
314 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000315 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000316 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100317 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000318 break;
319 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000320 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000321 c->self, c->ostate);
322 break;
323 }
324}
325static void
Damien Miller33b13562000-04-04 14:38:59 +1000326chan_send_eof2(Channel *c)
327{
Damien Millerfbdeece2003-09-02 22:52:31 +1000328 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000329 switch (c->istate) {
330 case CHAN_INPUT_WAIT_DRAIN:
331 packet_start(SSH2_MSG_CHANNEL_EOF);
332 packet_put_int(c->remote_id);
333 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000334 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000335 break;
336 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000337 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000338 c->self, c->istate);
339 break;
340 }
341}
342static void
343chan_send_close2(Channel *c)
344{
Damien Millerfbdeece2003-09-02 22:52:31 +1000345 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000346 if (c->ostate != CHAN_OUTPUT_CLOSED ||
347 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000348 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000349 c->self, c->istate, c->ostate);
350 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000351 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000352 } else {
353 packet_start(SSH2_MSG_CHANNEL_CLOSE);
354 packet_put_int(c->remote_id);
355 packet_send();
356 c->flags |= CHAN_CLOSE_SENT;
357 }
358}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000359
360/* shared */
361
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000362void
Damien Miller5144df92002-01-22 23:28:45 +1100363chan_rcvd_ieof(Channel *c)
364{
365 if (compat20)
366 chan_rcvd_eof2(c);
367 else
368 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100369 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000370 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000371 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100372 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100373}
374void
375chan_rcvd_oclose(Channel *c)
376{
377 if (compat20)
378 chan_rcvd_close2(c);
379 else
380 chan_rcvd_oclose1(c);
381}
382void
383chan_write_failed(Channel *c)
384{
385 if (compat20)
386 chan_write_failed2(c);
387 else
388 chan_write_failed1(c);
389}
390
391void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000392chan_mark_dead(Channel *c)
393{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000394 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000395}
396
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000397int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000398chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000399{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000400 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000401 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000402 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000403 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000404 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
405 return 0;
406 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000407 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000408 return 1;
409 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000410 if ((datafellows & SSH_BUG_EXTEOF) &&
411 c->extended_usage == CHAN_EXTENDED_WRITE &&
412 c->efd != -1 &&
413 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000414 debug2("channel %d: active efd: %d len %d",
415 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000416 return 0;
417 }
418 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000419 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000420 chan_send_close2(c);
421 } else {
422 /* channel would be dead if we sent a close */
423 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000424 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000425 c->self);
426 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000427 }
Damien Miller33b13562000-04-04 14:38:59 +1000428 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000429 }
430 if ((c->flags & CHAN_CLOSE_SENT) &&
431 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000432 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000433 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000434 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000435 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000436}
437
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438/* helper */
439static void
Damien Miller95def091999-11-25 00:26:21 +1100440chan_shutdown_write(Channel *c)
441{
Damien Miller76765c02002-01-22 23:21:15 +1100442 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000443 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
444 return;
Damien Miller5428f641999-11-25 11:54:57 +1100445 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000446 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000447 if (c->sock != -1) {
448 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000449 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000450 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000451 c->self, c->sock, strerror(errno));
452 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000453 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000454 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000455 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000456 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000457 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458}
459static void
Damien Miller95def091999-11-25 00:26:21 +1100460chan_shutdown_read(Channel *c)
461{
Damien Miller33b13562000-04-04 14:38:59 +1000462 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
463 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000464 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000465 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000466 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000467 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
468 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000469 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000470 */
471 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000472 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000473 error("channel %d: chan_shutdown_read: "
474 "shutdown() failed for fd%d [i%d o%d]: %.100s",
475 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000476 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000477 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000478 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000479 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000480 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000481 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482 }
483}