blob: 0d0faddb3d889dea9aa3e53cfebc0e7cf2ac0abb [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: nchan.c,v 1.58 2008/05/08 12:02:23 djm 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
Damien Millerb84886b2008-05-19 15:05:07 +100035#include "openbsd-compat/sys-queue.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "ssh1.h"
37#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038#include "buffer.h"
39#include "packet.h"
40#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100041#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Ben Lindstrom3b670d02001-06-09 00:57:39 +000044/*
45 * SSH Protocol 1.5 aka New Channel Protocol
46 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
47 * Written by Markus Friedl in October 1999
48 *
49 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
50 * tear down of channels:
51 *
52 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100053 * CLOSE ->
54 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000055 *
56 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100057 * IEOF ->
58 * <- OCLOSE
59 * <- IEOF
60 * OCLOSE ->
61 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000062 *
63 * 2.0: the EOF messages are optional
64 *
65 * See the debugging output from 'ssh -v' and 'sshd -d' of
66 * ssh-1.2.27 as an example.
67 *
68 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000069
Damien Miller33b13562000-04-04 14:38:59 +100070/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071/*
Damien Miller95def091999-11-25 00:26:21 +110072 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073 */
Damien Miller33b13562000-04-04 14:38:59 +100074/*
75 * ACTIONS: should never update the channel states
76 */
Ben Lindstrombba81212001-06-25 05:01:22 +000077static void chan_send_ieof1(Channel *);
78static void chan_send_oclose1(Channel *);
79static void chan_send_close2(Channel *);
80static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100081
Damien Miller33b13562000-04-04 14:38:59 +100082/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void chan_shutdown_write(Channel *);
84static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100085
Damien Millerabea8ee2002-01-22 23:27:11 +110086static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
87static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
88
89static void
90chan_set_istate(Channel *c, u_int next)
91{
92 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
93 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100094 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110095 istates[next]);
96 c->istate = next;
97}
98static void
99chan_set_ostate(Channel *c, u_int next)
100{
101 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
102 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000103 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100104 ostates[next]);
105 c->ostate = next;
106}
107
Damien Miller33b13562000-04-04 14:38:59 +1000108/*
109 * SSH1 specific implementation of event functions
110 */
111
112static void
113chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100114{
Damien Millerfbdeece2003-09-02 22:52:31 +1000115 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100116 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100118 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 break;
120 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 chan_shutdown_read(c);
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 Miller34132e52000-01-14 15:45:46 +1100124 break;
125 case CHAN_INPUT_WAIT_DRAIN:
126 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000127 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100128 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129 break;
130 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000131 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000132 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100133 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 }
135}
Damien Miller5144df92002-01-22 23:28:45 +1100136void
137chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100138{
Damien Millerfbdeece2003-09-02 22:52:31 +1000139 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100140 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100143 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144 break;
145 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000146 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000147 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148 break;
149 }
150}
Damien Miller5144df92002-01-22 23:28:45 +1100151void
152chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100153{
Damien Millerfbdeece2003-09-02 22:52:31 +1000154 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100155 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000156 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000157 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158 return;
159 }
Damien Miller95def091999-11-25 00:26:21 +1100160 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100162 if (compat20) {
163 if (!(c->flags & CHAN_CLOSE_SENT))
164 chan_send_eof2(c);
165 chan_set_istate(c, CHAN_INPUT_CLOSED);
166 } else {
167 chan_send_ieof1(c);
168 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
169 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170 break;
171 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000172 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000173 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174 break;
175 }
176}
Damien Miller33b13562000-04-04 14:38:59 +1000177static void
178chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100179{
Damien Millerfbdeece2003-09-02 22:52:31 +1000180 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100181 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100183 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184 break;
185 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100186 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187 break;
188 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000189 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000190 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191 break;
192 }
193}
Damien Miller33b13562000-04-04 14:38:59 +1000194static void
195chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100196{
Damien Millerfbdeece2003-09-02 22:52:31 +1000197 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100198 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199 case CHAN_OUTPUT_OPEN:
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_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 break;
204 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100205 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000206 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100207 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208 break;
209 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000210 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000211 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212 break;
213 }
214}
Damien Miller5144df92002-01-22 23:28:45 +1100215void
216chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100217{
Damien Millerfbdeece2003-09-02 22:52:31 +1000218 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100219 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000220 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000221 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 return;
223 }
Damien Miller95def091999-11-25 00:26:21 +1100224 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100226 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100227 if (!compat20)
228 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100229 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230 break;
231 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000232 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000233 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234 break;
235 }
236}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237static void
Damien Miller33b13562000-04-04 14:38:59 +1000238chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100239{
Damien Millerfbdeece2003-09-02 22:52:31 +1000240 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100241 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242 case CHAN_INPUT_OPEN:
243 case CHAN_INPUT_WAIT_DRAIN:
244 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
245 packet_put_int(c->remote_id);
246 packet_send();
247 break;
248 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000249 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000250 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251 break;
252 }
253}
254static void
Damien Miller33b13562000-04-04 14:38:59 +1000255chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100256{
Damien Millerfbdeece2003-09-02 22:52:31 +1000257 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100258 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259 case CHAN_OUTPUT_OPEN:
260 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100261 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
263 packet_put_int(c->remote_id);
264 packet_send();
265 break;
266 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000267 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100268 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269 break;
270 }
271}
Damien Miller33b13562000-04-04 14:38:59 +1000272
273/*
274 * the same for SSH2
275 */
276static void
Damien Millerebc11d32002-01-22 23:28:13 +1100277chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000278{
Damien Millerfbdeece2003-09-02 22:52:31 +1000279 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000280 if (c->flags & CHAN_CLOSE_RCVD)
281 error("channel %d: protocol error: close rcvd twice", c->self);
282 c->flags |= CHAN_CLOSE_RCVD;
283 if (c->type == SSH_CHANNEL_LARVAL) {
284 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100285 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
286 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000287 return;
288 }
289 switch (c->ostate) {
290 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000291 /*
292 * wait until a data from the channel is consumed if a CLOSE
293 * is received
294 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100295 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000296 break;
297 }
298 switch (c->istate) {
299 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000300 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100301 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000302 break;
303 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000304 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100305 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000306 break;
307 }
Damien Miller33b13562000-04-04 14:38:59 +1000308}
309static void
Damien Millerebc11d32002-01-22 23:28:13 +1100310chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000311{
Damien Millerfbdeece2003-09-02 22:52:31 +1000312 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000313 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100314 if (c->ostate == CHAN_OUTPUT_OPEN)
315 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000316}
317static void
318chan_write_failed2(Channel *c)
319{
Damien Millerfbdeece2003-09-02 22:52:31 +1000320 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000321 switch (c->ostate) {
322 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000323 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000324 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100325 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000326 break;
327 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000328 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000329 c->self, c->ostate);
330 break;
331 }
332}
333static void
Damien Miller33b13562000-04-04 14:38:59 +1000334chan_send_eof2(Channel *c)
335{
Damien Millerfbdeece2003-09-02 22:52:31 +1000336 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000337 switch (c->istate) {
338 case CHAN_INPUT_WAIT_DRAIN:
339 packet_start(SSH2_MSG_CHANNEL_EOF);
340 packet_put_int(c->remote_id);
341 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000342 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000343 break;
344 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000345 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000346 c->self, c->istate);
347 break;
348 }
349}
350static void
351chan_send_close2(Channel *c)
352{
Damien Millerfbdeece2003-09-02 22:52:31 +1000353 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000354 if (c->ostate != CHAN_OUTPUT_CLOSED ||
355 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000356 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000357 c->self, c->istate, c->ostate);
358 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000359 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000360 } else {
361 packet_start(SSH2_MSG_CHANNEL_CLOSE);
362 packet_put_int(c->remote_id);
363 packet_send();
364 c->flags |= CHAN_CLOSE_SENT;
365 }
366}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000367
368/* shared */
369
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000370void
Damien Miller5144df92002-01-22 23:28:45 +1100371chan_rcvd_ieof(Channel *c)
372{
373 if (compat20)
374 chan_rcvd_eof2(c);
375 else
376 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100377 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000378 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000379 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100380 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100381}
382void
383chan_rcvd_oclose(Channel *c)
384{
385 if (compat20)
386 chan_rcvd_close2(c);
387 else
388 chan_rcvd_oclose1(c);
389}
390void
391chan_write_failed(Channel *c)
392{
393 if (compat20)
394 chan_write_failed2(c);
395 else
396 chan_write_failed1(c);
397}
398
399void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000400chan_mark_dead(Channel *c)
401{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000402 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000403}
404
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000405int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000406chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000407{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000408 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000409 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000410 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000411 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000412 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
413 return 0;
414 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000415 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000416 return 1;
417 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000418 if ((datafellows & SSH_BUG_EXTEOF) &&
419 c->extended_usage == CHAN_EXTENDED_WRITE &&
420 c->efd != -1 &&
421 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000422 debug2("channel %d: active efd: %d len %d",
423 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000424 return 0;
425 }
426 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000427 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000428 chan_send_close2(c);
429 } else {
430 /* channel would be dead if we sent a close */
431 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000432 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000433 c->self);
434 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000435 }
Damien Miller33b13562000-04-04 14:38:59 +1000436 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000437 }
438 if ((c->flags & CHAN_CLOSE_SENT) &&
439 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000440 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000441 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000442 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000443 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000444}
445
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446/* helper */
447static void
Damien Miller95def091999-11-25 00:26:21 +1100448chan_shutdown_write(Channel *c)
449{
Damien Miller76765c02002-01-22 23:21:15 +1100450 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000451 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
452 return;
Damien Miller5428f641999-11-25 11:54:57 +1100453 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000454 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000455 if (c->sock != -1) {
456 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000457 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000458 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000459 c->self, c->sock, strerror(errno));
460 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000461 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000462 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000463 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000464 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}
467static void
Damien Miller95def091999-11-25 00:26:21 +1100468chan_shutdown_read(Channel *c)
469{
Damien Miller33b13562000-04-04 14:38:59 +1000470 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
471 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000472 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000473 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000474 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000475 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
476 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000477 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000478 */
479 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000480 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000481 error("channel %d: chan_shutdown_read: "
482 "shutdown() failed for fd%d [i%d o%d]: %.100s",
483 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000484 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000485 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000486 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000487 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000488 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000489 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 }
491}