blob: 58b0e98d598d2ae5f2e8fc47ea8c84c9145bf69a [file] [log] [blame]
Damien Millere3b60b52006-07-10 21:08:03 +10001/* $OpenBSD: nchan.c,v 1.54 2006/07/08 21:47:12 stevesk Exp $ */
Damien Miller5428f641999-11-25 11:54:57 +11002/*
Damien Millerb51ed392002-01-22 23:29:03 +11003 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11004 *
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 Miller5428f641999-11-25 11:54:57 +110013 *
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 Millerd4a8b7e1999-10-27 13:42:43 +100026#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millere3b60b52006-07-10 21:08:03 +100028#include <sys/types.h>
29#include <sys/socket.h>
30
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "ssh1.h"
32#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033#include "buffer.h"
34#include "packet.h"
35#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100036#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
Ben Lindstrom3b670d02001-06-09 00:57:39 +000039/*
40 * SSH Protocol 1.5 aka New Channel Protocol
41 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
42 * Written by Markus Friedl in October 1999
43 *
44 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
45 * tear down of channels:
46 *
47 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100048 * CLOSE ->
49 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000050 *
51 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100052 * IEOF ->
53 * <- OCLOSE
54 * <- IEOF
55 * OCLOSE ->
56 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000057 *
58 * 2.0: the EOF messages are optional
59 *
60 * See the debugging output from 'ssh -v' and 'sshd -d' of
61 * ssh-1.2.27 as an example.
62 *
63 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000064
Damien Miller33b13562000-04-04 14:38:59 +100065/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066/*
Damien Miller95def091999-11-25 00:26:21 +110067 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068 */
Damien Miller33b13562000-04-04 14:38:59 +100069/*
70 * ACTIONS: should never update the channel states
71 */
Ben Lindstrombba81212001-06-25 05:01:22 +000072static void chan_send_ieof1(Channel *);
73static void chan_send_oclose1(Channel *);
74static void chan_send_close2(Channel *);
75static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100076
Damien Miller33b13562000-04-04 14:38:59 +100077/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000078static void chan_shutdown_write(Channel *);
79static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100080
Damien Millerabea8ee2002-01-22 23:27:11 +110081static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
82static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
83
84static void
85chan_set_istate(Channel *c, u_int next)
86{
87 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
88 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100089 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110090 istates[next]);
91 c->istate = next;
92}
93static void
94chan_set_ostate(Channel *c, u_int next)
95{
96 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
97 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100098 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +110099 ostates[next]);
100 c->ostate = next;
101}
102
Damien Miller33b13562000-04-04 14:38:59 +1000103/*
104 * SSH1 specific implementation of event functions
105 */
106
107static void
108chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100109{
Damien Millerfbdeece2003-09-02 22:52:31 +1000110 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100111 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100113 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114 break;
115 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000117 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100118 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100119 break;
120 case CHAN_INPUT_WAIT_DRAIN:
121 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000122 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100123 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124 break;
125 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000126 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000127 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100128 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129 }
130}
Damien Miller5144df92002-01-22 23:28:45 +1100131void
132chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100133{
Damien Millerfbdeece2003-09-02 22:52:31 +1000134 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100135 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100138 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 break;
140 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000141 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000142 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 break;
144 }
145}
Damien Miller5144df92002-01-22 23:28:45 +1100146void
147chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100148{
Damien Millerfbdeece2003-09-02 22:52:31 +1000149 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100150 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000151 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000152 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153 return;
154 }
Damien Miller95def091999-11-25 00:26:21 +1100155 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100157 if (compat20) {
158 if (!(c->flags & CHAN_CLOSE_SENT))
159 chan_send_eof2(c);
160 chan_set_istate(c, CHAN_INPUT_CLOSED);
161 } else {
162 chan_send_ieof1(c);
163 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
164 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165 break;
166 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000167 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000168 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169 break;
170 }
171}
Damien Miller33b13562000-04-04 14:38:59 +1000172static void
173chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100174{
Damien Millerfbdeece2003-09-02 22:52:31 +1000175 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100176 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100178 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179 break;
180 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100181 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182 break;
183 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000184 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000185 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 break;
187 }
188}
Damien Miller33b13562000-04-04 14:38:59 +1000189static void
190chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100191{
Damien Millerfbdeece2003-09-02 22:52:31 +1000192 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100193 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100195 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000196 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100197 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198 break;
199 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100200 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000201 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100202 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 break;
204 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000205 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000206 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207 break;
208 }
209}
Damien Miller5144df92002-01-22 23:28:45 +1100210void
211chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100212{
Damien Millerfbdeece2003-09-02 22:52:31 +1000213 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100214 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000215 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000216 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217 return;
218 }
Damien Miller95def091999-11-25 00:26:21 +1100219 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100221 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100222 if (!compat20)
223 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100224 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225 break;
226 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000227 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000228 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229 break;
230 }
231}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232static void
Damien Miller33b13562000-04-04 14:38:59 +1000233chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100234{
Damien Millerfbdeece2003-09-02 22:52:31 +1000235 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100236 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237 case CHAN_INPUT_OPEN:
238 case CHAN_INPUT_WAIT_DRAIN:
239 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
240 packet_put_int(c->remote_id);
241 packet_send();
242 break;
243 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000244 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000245 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246 break;
247 }
248}
249static void
Damien Miller33b13562000-04-04 14:38:59 +1000250chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100251{
Damien Millerfbdeece2003-09-02 22:52:31 +1000252 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100253 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254 case CHAN_OUTPUT_OPEN:
255 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100256 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
258 packet_put_int(c->remote_id);
259 packet_send();
260 break;
261 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000262 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264 break;
265 }
266}
Damien Miller33b13562000-04-04 14:38:59 +1000267
268/*
269 * the same for SSH2
270 */
271static void
Damien Millerebc11d32002-01-22 23:28:13 +1100272chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000273{
Damien Millerfbdeece2003-09-02 22:52:31 +1000274 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000275 if (c->flags & CHAN_CLOSE_RCVD)
276 error("channel %d: protocol error: close rcvd twice", c->self);
277 c->flags |= CHAN_CLOSE_RCVD;
278 if (c->type == SSH_CHANNEL_LARVAL) {
279 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100280 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
281 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000282 return;
283 }
284 switch (c->ostate) {
285 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000286 /*
287 * wait until a data from the channel is consumed if a CLOSE
288 * is received
289 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100290 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000291 break;
292 }
293 switch (c->istate) {
294 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000295 chan_shutdown_read(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 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000299 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100300 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000301 break;
302 }
Damien Miller33b13562000-04-04 14:38:59 +1000303}
304static void
Damien Millerebc11d32002-01-22 23:28:13 +1100305chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000306{
Damien Millerfbdeece2003-09-02 22:52:31 +1000307 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000308 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100309 if (c->ostate == CHAN_OUTPUT_OPEN)
310 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000311}
312static void
313chan_write_failed2(Channel *c)
314{
Damien Millerfbdeece2003-09-02 22:52:31 +1000315 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000316 switch (c->ostate) {
317 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000318 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000319 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100320 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000321 break;
322 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000323 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000324 c->self, c->ostate);
325 break;
326 }
327}
328static void
Damien Miller33b13562000-04-04 14:38:59 +1000329chan_send_eof2(Channel *c)
330{
Damien Millerfbdeece2003-09-02 22:52:31 +1000331 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000332 switch (c->istate) {
333 case CHAN_INPUT_WAIT_DRAIN:
334 packet_start(SSH2_MSG_CHANNEL_EOF);
335 packet_put_int(c->remote_id);
336 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000337 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000338 break;
339 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000340 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000341 c->self, c->istate);
342 break;
343 }
344}
345static void
346chan_send_close2(Channel *c)
347{
Damien Millerfbdeece2003-09-02 22:52:31 +1000348 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000349 if (c->ostate != CHAN_OUTPUT_CLOSED ||
350 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000351 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000352 c->self, c->istate, c->ostate);
353 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000354 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000355 } else {
356 packet_start(SSH2_MSG_CHANNEL_CLOSE);
357 packet_put_int(c->remote_id);
358 packet_send();
359 c->flags |= CHAN_CLOSE_SENT;
360 }
361}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000362
363/* shared */
364
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000365void
Damien Miller5144df92002-01-22 23:28:45 +1100366chan_rcvd_ieof(Channel *c)
367{
368 if (compat20)
369 chan_rcvd_eof2(c);
370 else
371 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100372 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000373 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000374 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100375 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100376}
377void
378chan_rcvd_oclose(Channel *c)
379{
380 if (compat20)
381 chan_rcvd_close2(c);
382 else
383 chan_rcvd_oclose1(c);
384}
385void
386chan_write_failed(Channel *c)
387{
388 if (compat20)
389 chan_write_failed2(c);
390 else
391 chan_write_failed1(c);
392}
393
394void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000395chan_mark_dead(Channel *c)
396{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000397 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000398}
399
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000400int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000401chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000402{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000403 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000404 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000405 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000406 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000407 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
408 return 0;
409 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000410 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000411 return 1;
412 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000413 if ((datafellows & SSH_BUG_EXTEOF) &&
414 c->extended_usage == CHAN_EXTENDED_WRITE &&
415 c->efd != -1 &&
416 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000417 debug2("channel %d: active efd: %d len %d",
418 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000419 return 0;
420 }
421 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000422 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000423 chan_send_close2(c);
424 } else {
425 /* channel would be dead if we sent a close */
426 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000427 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000428 c->self);
429 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000430 }
Damien Miller33b13562000-04-04 14:38:59 +1000431 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000432 }
433 if ((c->flags & CHAN_CLOSE_SENT) &&
434 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000435 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000436 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000437 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000438 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000439}
440
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441/* helper */
442static void
Damien Miller95def091999-11-25 00:26:21 +1100443chan_shutdown_write(Channel *c)
444{
Damien Miller76765c02002-01-22 23:21:15 +1100445 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000446 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
447 return;
Damien Miller5428f641999-11-25 11:54:57 +1100448 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000449 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000450 if (c->sock != -1) {
451 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000452 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000453 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000454 c->self, c->sock, strerror(errno));
455 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000456 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000457 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000458 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000459 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000460 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}
462static void
Damien Miller95def091999-11-25 00:26:21 +1100463chan_shutdown_read(Channel *c)
464{
Damien Miller33b13562000-04-04 14:38:59 +1000465 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
466 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000467 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000468 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000469 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000470 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
471 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000472 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000473 */
474 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000475 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000476 error("channel %d: chan_shutdown_read: "
477 "shutdown() failed for fd%d [i%d o%d]: %.100s",
478 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000479 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000480 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000481 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000482 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000483 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000484 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 }
486}