blob: 78908c3c65e52606a8bd3ccee41a19318178b0bc [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: nchan.c,v 1.56 2006/07/22 20:48:23 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
Darren Tucker39972492006-07-12 22:22:46 +100031#include <errno.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Darren Tucker39972492006-07-12 22:22:46 +100033
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh1.h"
35#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036#include "buffer.h"
37#include "packet.h"
38#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100039#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Ben Lindstrom3b670d02001-06-09 00:57:39 +000042/*
43 * SSH Protocol 1.5 aka New Channel Protocol
44 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
45 * Written by Markus Friedl in October 1999
46 *
47 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
48 * tear down of channels:
49 *
50 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100051 * CLOSE ->
52 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000053 *
54 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100055 * IEOF ->
56 * <- OCLOSE
57 * <- IEOF
58 * OCLOSE ->
59 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000060 *
61 * 2.0: the EOF messages are optional
62 *
63 * See the debugging output from 'ssh -v' and 'sshd -d' of
64 * ssh-1.2.27 as an example.
65 *
66 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000067
Damien Miller33b13562000-04-04 14:38:59 +100068/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069/*
Damien Miller95def091999-11-25 00:26:21 +110070 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071 */
Damien Miller33b13562000-04-04 14:38:59 +100072/*
73 * ACTIONS: should never update the channel states
74 */
Ben Lindstrombba81212001-06-25 05:01:22 +000075static void chan_send_ieof1(Channel *);
76static void chan_send_oclose1(Channel *);
77static void chan_send_close2(Channel *);
78static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100079
Damien Miller33b13562000-04-04 14:38:59 +100080/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000081static void chan_shutdown_write(Channel *);
82static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100083
Damien Millerabea8ee2002-01-22 23:27:11 +110084static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
85static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
86
87static void
88chan_set_istate(Channel *c, u_int next)
89{
90 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
91 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100092 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110093 istates[next]);
94 c->istate = next;
95}
96static void
97chan_set_ostate(Channel *c, u_int next)
98{
99 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
100 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000101 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100102 ostates[next]);
103 c->ostate = next;
104}
105
Damien Miller33b13562000-04-04 14:38:59 +1000106/*
107 * SSH1 specific implementation of event functions
108 */
109
110static void
111chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100112{
Damien Millerfbdeece2003-09-02 22:52:31 +1000113 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100114 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100116 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 break;
118 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000120 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100121 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100122 break;
123 case CHAN_INPUT_WAIT_DRAIN:
124 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000125 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100126 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127 break;
128 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000129 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000130 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100131 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132 }
133}
Damien Miller5144df92002-01-22 23:28:45 +1100134void
135chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100136{
Damien Millerfbdeece2003-09-02 22:52:31 +1000137 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100138 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100141 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 break;
143 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000144 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000145 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146 break;
147 }
148}
Damien Miller5144df92002-01-22 23:28:45 +1100149void
150chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100151{
Damien Millerfbdeece2003-09-02 22:52:31 +1000152 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100153 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000154 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000155 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156 return;
157 }
Damien Miller95def091999-11-25 00:26:21 +1100158 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100160 if (compat20) {
161 if (!(c->flags & CHAN_CLOSE_SENT))
162 chan_send_eof2(c);
163 chan_set_istate(c, CHAN_INPUT_CLOSED);
164 } else {
165 chan_send_ieof1(c);
166 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
167 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168 break;
169 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000170 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000171 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 break;
173 }
174}
Damien Miller33b13562000-04-04 14:38:59 +1000175static void
176chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100177{
Damien Millerfbdeece2003-09-02 22:52:31 +1000178 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100179 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100181 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182 break;
183 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100184 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185 break;
186 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000187 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000188 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189 break;
190 }
191}
Damien Miller33b13562000-04-04 14:38:59 +1000192static void
193chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100194{
Damien Millerfbdeece2003-09-02 22:52:31 +1000195 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100196 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100198 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000199 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100200 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201 break;
202 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100203 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000204 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100205 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206 break;
207 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000208 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000209 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210 break;
211 }
212}
Damien Miller5144df92002-01-22 23:28:45 +1100213void
214chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100215{
Damien Millerfbdeece2003-09-02 22:52:31 +1000216 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100217 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000218 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000219 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220 return;
221 }
Damien Miller95def091999-11-25 00:26:21 +1100222 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100224 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100225 if (!compat20)
226 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100227 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228 break;
229 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000230 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000231 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232 break;
233 }
234}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235static void
Damien Miller33b13562000-04-04 14:38:59 +1000236chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100237{
Damien Millerfbdeece2003-09-02 22:52:31 +1000238 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100239 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240 case CHAN_INPUT_OPEN:
241 case CHAN_INPUT_WAIT_DRAIN:
242 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
243 packet_put_int(c->remote_id);
244 packet_send();
245 break;
246 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000247 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000248 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249 break;
250 }
251}
252static void
Damien Miller33b13562000-04-04 14:38:59 +1000253chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100254{
Damien Millerfbdeece2003-09-02 22:52:31 +1000255 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100256 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257 case CHAN_OUTPUT_OPEN:
258 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100259 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
261 packet_put_int(c->remote_id);
262 packet_send();
263 break;
264 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000265 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100266 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267 break;
268 }
269}
Damien Miller33b13562000-04-04 14:38:59 +1000270
271/*
272 * the same for SSH2
273 */
274static void
Damien Millerebc11d32002-01-22 23:28:13 +1100275chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000276{
Damien Millerfbdeece2003-09-02 22:52:31 +1000277 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000278 if (c->flags & CHAN_CLOSE_RCVD)
279 error("channel %d: protocol error: close rcvd twice", c->self);
280 c->flags |= CHAN_CLOSE_RCVD;
281 if (c->type == SSH_CHANNEL_LARVAL) {
282 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100283 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
284 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000285 return;
286 }
287 switch (c->ostate) {
288 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000289 /*
290 * wait until a data from the channel is consumed if a CLOSE
291 * is received
292 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100293 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000294 break;
295 }
296 switch (c->istate) {
297 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000298 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100299 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000300 break;
301 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000302 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100303 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000304 break;
305 }
Damien Miller33b13562000-04-04 14:38:59 +1000306}
307static void
Damien Millerebc11d32002-01-22 23:28:13 +1100308chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000309{
Damien Millerfbdeece2003-09-02 22:52:31 +1000310 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000311 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100312 if (c->ostate == CHAN_OUTPUT_OPEN)
313 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000314}
315static void
316chan_write_failed2(Channel *c)
317{
Damien Millerfbdeece2003-09-02 22:52:31 +1000318 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000319 switch (c->ostate) {
320 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000321 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000322 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100323 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000324 break;
325 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000326 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000327 c->self, c->ostate);
328 break;
329 }
330}
331static void
Damien Miller33b13562000-04-04 14:38:59 +1000332chan_send_eof2(Channel *c)
333{
Damien Millerfbdeece2003-09-02 22:52:31 +1000334 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000335 switch (c->istate) {
336 case CHAN_INPUT_WAIT_DRAIN:
337 packet_start(SSH2_MSG_CHANNEL_EOF);
338 packet_put_int(c->remote_id);
339 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000340 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000341 break;
342 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000343 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000344 c->self, c->istate);
345 break;
346 }
347}
348static void
349chan_send_close2(Channel *c)
350{
Damien Millerfbdeece2003-09-02 22:52:31 +1000351 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000352 if (c->ostate != CHAN_OUTPUT_CLOSED ||
353 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000354 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000355 c->self, c->istate, c->ostate);
356 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000357 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000358 } else {
359 packet_start(SSH2_MSG_CHANNEL_CLOSE);
360 packet_put_int(c->remote_id);
361 packet_send();
362 c->flags |= CHAN_CLOSE_SENT;
363 }
364}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000365
366/* shared */
367
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000368void
Damien Miller5144df92002-01-22 23:28:45 +1100369chan_rcvd_ieof(Channel *c)
370{
371 if (compat20)
372 chan_rcvd_eof2(c);
373 else
374 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100375 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000376 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000377 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100378 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100379}
380void
381chan_rcvd_oclose(Channel *c)
382{
383 if (compat20)
384 chan_rcvd_close2(c);
385 else
386 chan_rcvd_oclose1(c);
387}
388void
389chan_write_failed(Channel *c)
390{
391 if (compat20)
392 chan_write_failed2(c);
393 else
394 chan_write_failed1(c);
395}
396
397void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000398chan_mark_dead(Channel *c)
399{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000400 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000401}
402
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000403int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000404chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000405{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000406 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000407 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000408 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000409 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000410 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
411 return 0;
412 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000413 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000414 return 1;
415 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000416 if ((datafellows & SSH_BUG_EXTEOF) &&
417 c->extended_usage == CHAN_EXTENDED_WRITE &&
418 c->efd != -1 &&
419 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000420 debug2("channel %d: active efd: %d len %d",
421 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000422 return 0;
423 }
424 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000425 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000426 chan_send_close2(c);
427 } else {
428 /* channel would be dead if we sent a close */
429 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000430 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000431 c->self);
432 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000433 }
Damien Miller33b13562000-04-04 14:38:59 +1000434 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000435 }
436 if ((c->flags & CHAN_CLOSE_SENT) &&
437 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000438 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000439 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000440 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000441 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000442}
443
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444/* helper */
445static void
Damien Miller95def091999-11-25 00:26:21 +1100446chan_shutdown_write(Channel *c)
447{
Damien Miller76765c02002-01-22 23:21:15 +1100448 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000449 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
450 return;
Damien Miller5428f641999-11-25 11:54:57 +1100451 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000452 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000453 if (c->sock != -1) {
454 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000455 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000456 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000457 c->self, c->sock, strerror(errno));
458 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000459 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000460 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000461 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000462 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000463 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464}
465static void
Damien Miller95def091999-11-25 00:26:21 +1100466chan_shutdown_read(Channel *c)
467{
Damien Miller33b13562000-04-04 14:38:59 +1000468 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
469 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000470 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000471 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000472 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000473 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
474 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000475 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000476 */
477 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000478 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000479 error("channel %d: chan_shutdown_read: "
480 "shutdown() failed for fd%d [i%d o%d]: %.100s",
481 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000482 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000483 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000484 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000485 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000486 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000487 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 }
489}