blob: b78d6a76485c762a4bcd10f6a3dae8c0f483d756 [file] [log] [blame]
Damien Miller456e6f02008-11-03 19:20:10 +11001/* $OpenBSD: nchan.c,v 1.61 2008/09/11 14:22:37 markus 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 Millerbab9bd42008-05-19 16:06:47 +100081static void chan_send_eow2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100082
Damien Miller33b13562000-04-04 14:38:59 +100083/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000084static void chan_shutdown_write(Channel *);
85static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100086
Damien Millerabea8ee2002-01-22 23:27:11 +110087static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
88static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
89
90static void
91chan_set_istate(Channel *c, u_int next)
92{
93 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
94 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100095 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110096 istates[next]);
97 c->istate = next;
98}
99static void
100chan_set_ostate(Channel *c, u_int next)
101{
102 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
103 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000104 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100105 ostates[next]);
106 c->ostate = next;
107}
108
Damien Miller33b13562000-04-04 14:38:59 +1000109/*
110 * SSH1 specific implementation of event functions
111 */
112
113static void
114chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100115{
Damien Millerfbdeece2003-09-02 22:52:31 +1000116 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100117 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100119 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 break;
121 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000123 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100124 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100125 break;
126 case CHAN_INPUT_WAIT_DRAIN:
127 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000128 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100129 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130 break;
131 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000132 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000133 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100134 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135 }
136}
Damien Miller5144df92002-01-22 23:28:45 +1100137void
138chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100139{
Damien Millerfbdeece2003-09-02 22:52:31 +1000140 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100141 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100144 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145 break;
146 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000147 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000148 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149 break;
150 }
151}
Damien Miller5144df92002-01-22 23:28:45 +1100152void
153chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100154{
Damien Millerfbdeece2003-09-02 22:52:31 +1000155 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100156 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000157 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000158 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 return;
160 }
Damien Miller95def091999-11-25 00:26:21 +1100161 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100163 if (compat20) {
164 if (!(c->flags & CHAN_CLOSE_SENT))
165 chan_send_eof2(c);
166 chan_set_istate(c, CHAN_INPUT_CLOSED);
167 } else {
168 chan_send_ieof1(c);
169 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
170 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171 break;
172 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000173 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000174 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175 break;
176 }
177}
Damien Miller33b13562000-04-04 14:38:59 +1000178static void
179chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100180{
Damien Millerfbdeece2003-09-02 22:52:31 +1000181 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100182 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100184 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185 break;
186 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100187 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188 break;
189 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000190 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000191 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192 break;
193 }
194}
Damien Miller33b13562000-04-04 14:38:59 +1000195static void
196chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100197{
Damien Millerfbdeece2003-09-02 22:52:31 +1000198 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100199 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100201 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000202 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100203 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204 break;
205 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100206 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000207 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100208 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209 break;
210 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000211 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000212 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213 break;
214 }
215}
Damien Miller5144df92002-01-22 23:28:45 +1100216void
217chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100218{
Damien Millerfbdeece2003-09-02 22:52:31 +1000219 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100220 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000221 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000222 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223 return;
224 }
Damien Miller95def091999-11-25 00:26:21 +1100225 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100227 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100228 if (!compat20)
229 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100230 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231 break;
232 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000233 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000234 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235 break;
236 }
237}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238static void
Damien Miller33b13562000-04-04 14:38:59 +1000239chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100240{
Damien Millerfbdeece2003-09-02 22:52:31 +1000241 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100242 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 case CHAN_INPUT_OPEN:
244 case CHAN_INPUT_WAIT_DRAIN:
245 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
246 packet_put_int(c->remote_id);
247 packet_send();
248 break;
249 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000250 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000251 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252 break;
253 }
254}
255static void
Damien Miller33b13562000-04-04 14:38:59 +1000256chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100257{
Damien Millerfbdeece2003-09-02 22:52:31 +1000258 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100259 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260 case CHAN_OUTPUT_OPEN:
261 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100262 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
264 packet_put_int(c->remote_id);
265 packet_send();
266 break;
267 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000268 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100269 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 break;
271 }
272}
Damien Miller33b13562000-04-04 14:38:59 +1000273
274/*
275 * the same for SSH2
276 */
277static void
Damien Millerebc11d32002-01-22 23:28:13 +1100278chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000279{
Damien Millerfbdeece2003-09-02 22:52:31 +1000280 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000281 if (c->flags & CHAN_CLOSE_RCVD)
282 error("channel %d: protocol error: close rcvd twice", c->self);
283 c->flags |= CHAN_CLOSE_RCVD;
284 if (c->type == SSH_CHANNEL_LARVAL) {
285 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100286 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
287 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000288 return;
289 }
290 switch (c->ostate) {
291 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000292 /*
293 * wait until a data from the channel is consumed if a CLOSE
294 * is received
295 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100296 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000297 break;
298 }
299 switch (c->istate) {
300 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000301 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100302 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000303 break;
304 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000305 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100306 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000307 break;
308 }
Damien Miller33b13562000-04-04 14:38:59 +1000309}
Damien Millerbab9bd42008-05-19 16:06:47 +1000310void
311chan_rcvd_eow(Channel *c)
312{
313 debug2("channel %d: rcvd eow", c->self);
314 switch (c->istate) {
315 case CHAN_INPUT_OPEN:
316 chan_shutdown_read(c);
317 chan_set_istate(c, CHAN_INPUT_CLOSED);
318 break;
319 }
320}
Damien Miller33b13562000-04-04 14:38:59 +1000321static void
Damien Millerebc11d32002-01-22 23:28:13 +1100322chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000323{
Damien Millerfbdeece2003-09-02 22:52:31 +1000324 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000325 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100326 if (c->ostate == CHAN_OUTPUT_OPEN)
327 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000328}
329static void
330chan_write_failed2(Channel *c)
331{
Damien Millerfbdeece2003-09-02 22:52:31 +1000332 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000333 switch (c->ostate) {
334 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000335 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000336 chan_shutdown_write(c);
Darren Tucker8748b962008-07-02 22:32:43 +1000337 if (strcmp(c->ctype, "session") == 0)
338 chan_send_eow2(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100339 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000340 break;
341 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000342 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000343 c->self, c->ostate);
344 break;
345 }
346}
347static void
Damien Miller33b13562000-04-04 14:38:59 +1000348chan_send_eof2(Channel *c)
349{
Damien Millerfbdeece2003-09-02 22:52:31 +1000350 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000351 switch (c->istate) {
352 case CHAN_INPUT_WAIT_DRAIN:
353 packet_start(SSH2_MSG_CHANNEL_EOF);
354 packet_put_int(c->remote_id);
355 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000356 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000357 break;
358 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000359 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000360 c->self, c->istate);
361 break;
362 }
363}
364static void
365chan_send_close2(Channel *c)
366{
Damien Millerfbdeece2003-09-02 22:52:31 +1000367 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000368 if (c->ostate != CHAN_OUTPUT_CLOSED ||
369 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000370 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000371 c->self, c->istate, c->ostate);
372 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000373 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000374 } else {
375 packet_start(SSH2_MSG_CHANNEL_CLOSE);
376 packet_put_int(c->remote_id);
377 packet_send();
378 c->flags |= CHAN_CLOSE_SENT;
379 }
380}
Damien Millerbab9bd42008-05-19 16:06:47 +1000381static void
382chan_send_eow2(Channel *c)
383{
384 debug2("channel %d: send eow", c->self);
385 if (c->ostate == CHAN_OUTPUT_CLOSED) {
386 error("channel %d: must not sent eow on closed output",
387 c->self);
388 return;
389 }
Damien Miller456e6f02008-11-03 19:20:10 +1100390 if (!(datafellows & SSH_NEW_OPENSSH))
391 return;
Damien Millerbab9bd42008-05-19 16:06:47 +1000392 packet_start(SSH2_MSG_CHANNEL_REQUEST);
393 packet_put_int(c->remote_id);
394 packet_put_cstring("eow@openssh.com");
395 packet_put_char(0);
396 packet_send();
397}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000398
399/* shared */
400
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000401void
Damien Miller5144df92002-01-22 23:28:45 +1100402chan_rcvd_ieof(Channel *c)
403{
404 if (compat20)
405 chan_rcvd_eof2(c);
406 else
407 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100408 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000409 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000410 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100411 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100412}
413void
414chan_rcvd_oclose(Channel *c)
415{
416 if (compat20)
417 chan_rcvd_close2(c);
418 else
419 chan_rcvd_oclose1(c);
420}
421void
422chan_write_failed(Channel *c)
423{
424 if (compat20)
425 chan_write_failed2(c);
426 else
427 chan_write_failed1(c);
428}
429
430void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000431chan_mark_dead(Channel *c)
432{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000433 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000434}
435
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000436int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000437chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000438{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000439 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000440 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000441 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000442 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000443 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
444 return 0;
445 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000446 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000447 return 1;
448 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000449 if ((datafellows & SSH_BUG_EXTEOF) &&
450 c->extended_usage == CHAN_EXTENDED_WRITE &&
451 c->efd != -1 &&
452 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000453 debug2("channel %d: active efd: %d len %d",
454 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000455 return 0;
456 }
457 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000458 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000459 chan_send_close2(c);
460 } else {
461 /* channel would be dead if we sent a close */
462 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000463 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000464 c->self);
465 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000466 }
Damien Miller33b13562000-04-04 14:38:59 +1000467 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000468 }
469 if ((c->flags & CHAN_CLOSE_SENT) &&
470 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000471 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000472 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000473 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000474 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000475}
476
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477/* helper */
478static void
Damien Miller95def091999-11-25 00:26:21 +1100479chan_shutdown_write(Channel *c)
480{
Damien Miller76765c02002-01-22 23:21:15 +1100481 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000482 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
483 return;
Damien Miller5428f641999-11-25 11:54:57 +1100484 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000485 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000486 if (c->sock != -1) {
487 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000488 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000489 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000490 c->self, c->sock, strerror(errno));
491 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000492 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000493 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000494 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000495 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000496 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497}
498static void
Damien Miller95def091999-11-25 00:26:21 +1100499chan_shutdown_read(Channel *c)
500{
Damien Miller33b13562000-04-04 14:38:59 +1000501 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
502 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000503 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000504 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000505 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000506 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
507 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000508 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000509 */
510 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000511 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000512 error("channel %d: chan_shutdown_read: "
513 "shutdown() failed for fd%d [i%d o%d]: %.100s",
514 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000515 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000516 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000517 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000518 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000519 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000520 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521 }
522}