blob: ad461f4af6a36c57362a773601e2f4ea8645653e [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: nchan.c,v 1.57 2006/08/03 03:34:42 deraadt 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>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <stdarg.h>
Darren Tucker39972492006-07-12 22:22:46 +100034
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "ssh1.h"
36#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037#include "buffer.h"
38#include "packet.h"
39#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100040#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Ben Lindstrom3b670d02001-06-09 00:57:39 +000043/*
44 * SSH Protocol 1.5 aka New Channel Protocol
45 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
46 * Written by Markus Friedl in October 1999
47 *
48 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
49 * tear down of channels:
50 *
51 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100052 * CLOSE ->
53 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000054 *
55 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100056 * IEOF ->
57 * <- OCLOSE
58 * <- IEOF
59 * OCLOSE ->
60 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000061 *
62 * 2.0: the EOF messages are optional
63 *
64 * See the debugging output from 'ssh -v' and 'sshd -d' of
65 * ssh-1.2.27 as an example.
66 *
67 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000068
Damien Miller33b13562000-04-04 14:38:59 +100069/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070/*
Damien Miller95def091999-11-25 00:26:21 +110071 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072 */
Damien Miller33b13562000-04-04 14:38:59 +100073/*
74 * ACTIONS: should never update the channel states
75 */
Ben Lindstrombba81212001-06-25 05:01:22 +000076static void chan_send_ieof1(Channel *);
77static void chan_send_oclose1(Channel *);
78static void chan_send_close2(Channel *);
79static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100080
Damien Miller33b13562000-04-04 14:38:59 +100081/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000082static void chan_shutdown_write(Channel *);
83static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100084
Damien Millerabea8ee2002-01-22 23:27:11 +110085static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
86static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
87
88static void
89chan_set_istate(Channel *c, u_int next)
90{
91 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
92 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100093 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110094 istates[next]);
95 c->istate = next;
96}
97static void
98chan_set_ostate(Channel *c, u_int next)
99{
100 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
101 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000102 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100103 ostates[next]);
104 c->ostate = next;
105}
106
Damien Miller33b13562000-04-04 14:38:59 +1000107/*
108 * SSH1 specific implementation of event functions
109 */
110
111static void
112chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100113{
Damien Millerfbdeece2003-09-02 22:52:31 +1000114 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100115 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100117 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 break;
119 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000121 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100122 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100123 break;
124 case CHAN_INPUT_WAIT_DRAIN:
125 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000126 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100127 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 break;
129 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000130 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000131 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100132 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133 }
134}
Damien Miller5144df92002-01-22 23:28:45 +1100135void
136chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100137{
Damien Millerfbdeece2003-09-02 22:52:31 +1000138 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100139 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100142 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 break;
144 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000145 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000146 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147 break;
148 }
149}
Damien Miller5144df92002-01-22 23:28:45 +1100150void
151chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100152{
Damien Millerfbdeece2003-09-02 22:52:31 +1000153 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100154 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000155 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000156 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157 return;
158 }
Damien Miller95def091999-11-25 00:26:21 +1100159 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100161 if (compat20) {
162 if (!(c->flags & CHAN_CLOSE_SENT))
163 chan_send_eof2(c);
164 chan_set_istate(c, CHAN_INPUT_CLOSED);
165 } else {
166 chan_send_ieof1(c);
167 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
168 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169 break;
170 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000171 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000172 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 break;
174 }
175}
Damien Miller33b13562000-04-04 14:38:59 +1000176static void
177chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100178{
Damien Millerfbdeece2003-09-02 22:52:31 +1000179 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100180 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100182 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183 break;
184 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100185 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 break;
187 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000188 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000189 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190 break;
191 }
192}
Damien Miller33b13562000-04-04 14:38:59 +1000193static void
194chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100195{
Damien Millerfbdeece2003-09-02 22:52:31 +1000196 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100197 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100199 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000200 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100201 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 break;
203 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100204 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000205 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100206 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207 break;
208 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000209 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000210 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211 break;
212 }
213}
Damien Miller5144df92002-01-22 23:28:45 +1100214void
215chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100216{
Damien Millerfbdeece2003-09-02 22:52:31 +1000217 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100218 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000219 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000220 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221 return;
222 }
Damien Miller95def091999-11-25 00:26:21 +1100223 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100225 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100226 if (!compat20)
227 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100228 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229 break;
230 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000231 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000232 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233 break;
234 }
235}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236static void
Damien Miller33b13562000-04-04 14:38:59 +1000237chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100238{
Damien Millerfbdeece2003-09-02 22:52:31 +1000239 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100240 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241 case CHAN_INPUT_OPEN:
242 case CHAN_INPUT_WAIT_DRAIN:
243 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
244 packet_put_int(c->remote_id);
245 packet_send();
246 break;
247 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000248 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000249 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 break;
251 }
252}
253static void
Damien Miller33b13562000-04-04 14:38:59 +1000254chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100255{
Damien Millerfbdeece2003-09-02 22:52:31 +1000256 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100257 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258 case CHAN_OUTPUT_OPEN:
259 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100260 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
262 packet_put_int(c->remote_id);
263 packet_send();
264 break;
265 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000266 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100267 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268 break;
269 }
270}
Damien Miller33b13562000-04-04 14:38:59 +1000271
272/*
273 * the same for SSH2
274 */
275static void
Damien Millerebc11d32002-01-22 23:28:13 +1100276chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000277{
Damien Millerfbdeece2003-09-02 22:52:31 +1000278 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000279 if (c->flags & CHAN_CLOSE_RCVD)
280 error("channel %d: protocol error: close rcvd twice", c->self);
281 c->flags |= CHAN_CLOSE_RCVD;
282 if (c->type == SSH_CHANNEL_LARVAL) {
283 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100284 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
285 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000286 return;
287 }
288 switch (c->ostate) {
289 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000290 /*
291 * wait until a data from the channel is consumed if a CLOSE
292 * is received
293 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100294 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000295 break;
296 }
297 switch (c->istate) {
298 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000299 chan_shutdown_read(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 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000303 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100304 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000305 break;
306 }
Damien Miller33b13562000-04-04 14:38:59 +1000307}
308static void
Damien Millerebc11d32002-01-22 23:28:13 +1100309chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000310{
Damien Millerfbdeece2003-09-02 22:52:31 +1000311 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000312 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100313 if (c->ostate == CHAN_OUTPUT_OPEN)
314 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000315}
316static void
317chan_write_failed2(Channel *c)
318{
Damien Millerfbdeece2003-09-02 22:52:31 +1000319 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000320 switch (c->ostate) {
321 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000322 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000323 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100324 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000325 break;
326 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000327 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000328 c->self, c->ostate);
329 break;
330 }
331}
332static void
Damien Miller33b13562000-04-04 14:38:59 +1000333chan_send_eof2(Channel *c)
334{
Damien Millerfbdeece2003-09-02 22:52:31 +1000335 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000336 switch (c->istate) {
337 case CHAN_INPUT_WAIT_DRAIN:
338 packet_start(SSH2_MSG_CHANNEL_EOF);
339 packet_put_int(c->remote_id);
340 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000341 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000342 break;
343 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000344 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000345 c->self, c->istate);
346 break;
347 }
348}
349static void
350chan_send_close2(Channel *c)
351{
Damien Millerfbdeece2003-09-02 22:52:31 +1000352 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000353 if (c->ostate != CHAN_OUTPUT_CLOSED ||
354 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000355 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000356 c->self, c->istate, c->ostate);
357 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000358 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000359 } else {
360 packet_start(SSH2_MSG_CHANNEL_CLOSE);
361 packet_put_int(c->remote_id);
362 packet_send();
363 c->flags |= CHAN_CLOSE_SENT;
364 }
365}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000366
367/* shared */
368
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000369void
Damien Miller5144df92002-01-22 23:28:45 +1100370chan_rcvd_ieof(Channel *c)
371{
372 if (compat20)
373 chan_rcvd_eof2(c);
374 else
375 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100376 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000377 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000378 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100379 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100380}
381void
382chan_rcvd_oclose(Channel *c)
383{
384 if (compat20)
385 chan_rcvd_close2(c);
386 else
387 chan_rcvd_oclose1(c);
388}
389void
390chan_write_failed(Channel *c)
391{
392 if (compat20)
393 chan_write_failed2(c);
394 else
395 chan_write_failed1(c);
396}
397
398void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000399chan_mark_dead(Channel *c)
400{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000401 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000402}
403
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000404int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000405chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000406{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000407 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000408 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000409 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000410 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000411 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
412 return 0;
413 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000414 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000415 return 1;
416 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000417 if ((datafellows & SSH_BUG_EXTEOF) &&
418 c->extended_usage == CHAN_EXTENDED_WRITE &&
419 c->efd != -1 &&
420 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000421 debug2("channel %d: active efd: %d len %d",
422 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000423 return 0;
424 }
425 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000426 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000427 chan_send_close2(c);
428 } else {
429 /* channel would be dead if we sent a close */
430 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000431 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000432 c->self);
433 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000434 }
Damien Miller33b13562000-04-04 14:38:59 +1000435 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000436 }
437 if ((c->flags & CHAN_CLOSE_SENT) &&
438 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000439 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000440 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000441 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000442 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000443}
444
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445/* helper */
446static void
Damien Miller95def091999-11-25 00:26:21 +1100447chan_shutdown_write(Channel *c)
448{
Damien Miller76765c02002-01-22 23:21:15 +1100449 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000450 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
451 return;
Damien Miller5428f641999-11-25 11:54:57 +1100452 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000453 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000454 if (c->sock != -1) {
455 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000456 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000457 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000458 c->self, c->sock, strerror(errno));
459 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000460 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000461 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000462 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000463 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000464 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465}
466static void
Damien Miller95def091999-11-25 00:26:21 +1100467chan_shutdown_read(Channel *c)
468{
Damien Miller33b13562000-04-04 14:38:59 +1000469 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
470 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000471 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000472 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000473 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000474 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
475 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000476 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000477 */
478 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000479 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000480 error("channel %d: chan_shutdown_read: "
481 "shutdown() failed for fd%d [i%d o%d]: %.100s",
482 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000483 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000484 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000485 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000486 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000487 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000488 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 }
490}