xref: /netbsd/crypto/external/bsd/openssh/dist/nchan.c (revision 84f89c72)
1*84f89c72Schristos /*	$NetBSD: nchan.c,v 1.14 2022/02/23 19:07:20 christos Exp $	*/
2*84f89c72Schristos /* $OpenBSD: nchan.c,v 1.74 2022/02/01 23:32:51 djm Exp $ */
3ca32bd8dSchristos /*
4ca32bd8dSchristos  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
5ca32bd8dSchristos  *
6ca32bd8dSchristos  * Redistribution and use in source and binary forms, with or without
7ca32bd8dSchristos  * modification, are permitted provided that the following conditions
8ca32bd8dSchristos  * are met:
9ca32bd8dSchristos  * 1. Redistributions of source code must retain the above copyright
10ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer.
11ca32bd8dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
12ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer in the
13ca32bd8dSchristos  *    documentation and/or other materials provided with the distribution.
14ca32bd8dSchristos  *
15ca32bd8dSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16ca32bd8dSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17ca32bd8dSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18ca32bd8dSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19ca32bd8dSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20ca32bd8dSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21ca32bd8dSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22ca32bd8dSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23ca32bd8dSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24ca32bd8dSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ca32bd8dSchristos  */
26ca32bd8dSchristos 
27313c6c94Schristos #include "includes.h"
28*84f89c72Schristos __RCSID("$NetBSD: nchan.c,v 1.14 2022/02/23 19:07:20 christos Exp $");
29ca32bd8dSchristos #include <sys/types.h>
30ca32bd8dSchristos #include <sys/socket.h>
31ca32bd8dSchristos #include <sys/queue.h>
32ca32bd8dSchristos 
33ca32bd8dSchristos #include <errno.h>
34ca32bd8dSchristos #include <string.h>
35ca32bd8dSchristos #include <stdarg.h>
36ca32bd8dSchristos 
37ca32bd8dSchristos #include "ssh2.h"
3845a1cd19Schristos #include "sshbuf.h"
3945a1cd19Schristos #include "ssherr.h"
40ca32bd8dSchristos #include "packet.h"
41ca32bd8dSchristos #include "channels.h"
42ca32bd8dSchristos #include "compat.h"
43ca32bd8dSchristos #include "log.h"
44ca32bd8dSchristos 
45ca32bd8dSchristos /*
46ca32bd8dSchristos  * SSH Protocol 1.5 aka New Channel Protocol
47ca32bd8dSchristos  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
48ca32bd8dSchristos  * Written by Markus Friedl in October 1999
49ca32bd8dSchristos  *
50ca32bd8dSchristos  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
51ca32bd8dSchristos  * tear down of channels:
52ca32bd8dSchristos  *
53ca32bd8dSchristos  * 1.3:	strict request-ack-protocol:
54ca32bd8dSchristos  *	CLOSE	->
55ca32bd8dSchristos  *		<-  CLOSE_CONFIRM
56ca32bd8dSchristos  *
57ca32bd8dSchristos  * 1.5:	uses variations of:
58ca32bd8dSchristos  *	IEOF	->
59ca32bd8dSchristos  *		<-  OCLOSE
60ca32bd8dSchristos  *		<-  IEOF
61ca32bd8dSchristos  *	OCLOSE	->
62ca32bd8dSchristos  *	i.e. both sides have to close the channel
63ca32bd8dSchristos  *
64ca32bd8dSchristos  * 2.0: the EOF messages are optional
65ca32bd8dSchristos  *
66ca32bd8dSchristos  * See the debugging output from 'ssh -v' and 'sshd -d' of
67ca32bd8dSchristos  * ssh-1.2.27 as an example.
68ca32bd8dSchristos  *
69ca32bd8dSchristos  */
70ca32bd8dSchristos 
71ca32bd8dSchristos /* functions manipulating channel states */
72ca32bd8dSchristos /*
73ca32bd8dSchristos  * EVENTS update channel input/output states execute ACTIONS
74ca32bd8dSchristos  */
75ca32bd8dSchristos /*
76ca32bd8dSchristos  * ACTIONS: should never update the channel states
77ca32bd8dSchristos  */
7845a1cd19Schristos static void	chan_send_eof2(struct ssh *, Channel *);
7945a1cd19Schristos static void	chan_send_eow2(struct ssh *, Channel *);
80ca32bd8dSchristos 
81ca32bd8dSchristos /* helper */
8245a1cd19Schristos static void	chan_shutdown_write(struct ssh *, Channel *);
8345a1cd19Schristos static void	chan_shutdown_read(struct ssh *, Channel *);
848f892fcaSchristos static void	chan_shutdown_extended_read(struct ssh *, Channel *);
85ca32bd8dSchristos 
86*84f89c72Schristos static const char * const ostates[] = {
87*84f89c72Schristos 	"open", "drain", "wait_ieof", "closed",
88*84f89c72Schristos };
89*84f89c72Schristos static const char * const istates[] = {
90*84f89c72Schristos 	"open", "drain", "wait_oclose", "closed",
91*84f89c72Schristos };
92ca32bd8dSchristos 
93ca32bd8dSchristos static void
chan_set_istate(Channel * c,u_int next)94ca32bd8dSchristos chan_set_istate(Channel *c, u_int next)
95ca32bd8dSchristos {
96ca32bd8dSchristos 	if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
97ca32bd8dSchristos 		fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
98ca32bd8dSchristos 	debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
99ca32bd8dSchristos 	    istates[next]);
100ca32bd8dSchristos 	c->istate = next;
101ca32bd8dSchristos }
10245a1cd19Schristos 
103ca32bd8dSchristos static void
chan_set_ostate(Channel * c,u_int next)104ca32bd8dSchristos chan_set_ostate(Channel *c, u_int next)
105ca32bd8dSchristos {
106ca32bd8dSchristos 	if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
107ca32bd8dSchristos 		fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
108ca32bd8dSchristos 	debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
109ca32bd8dSchristos 	    ostates[next]);
110ca32bd8dSchristos 	c->ostate = next;
111ca32bd8dSchristos }
112ca32bd8dSchristos 
113ca32bd8dSchristos void
chan_read_failed(struct ssh * ssh,Channel * c)11445a1cd19Schristos chan_read_failed(struct ssh *ssh, Channel *c)
115ca32bd8dSchristos {
116ca32bd8dSchristos 	debug2("channel %d: read failed", c->self);
117ca32bd8dSchristos 	switch (c->istate) {
118ca32bd8dSchristos 	case CHAN_INPUT_OPEN:
11945a1cd19Schristos 		chan_shutdown_read(ssh, c);
120ca32bd8dSchristos 		chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
121ca32bd8dSchristos 		break;
122ca32bd8dSchristos 	default:
123ca32bd8dSchristos 		error("channel %d: chan_read_failed for istate %d",
124ca32bd8dSchristos 		    c->self, c->istate);
125ca32bd8dSchristos 		break;
126ca32bd8dSchristos 	}
127ca32bd8dSchristos }
12845a1cd19Schristos 
129ca32bd8dSchristos void
chan_ibuf_empty(struct ssh * ssh,Channel * c)13045a1cd19Schristos chan_ibuf_empty(struct ssh *ssh, Channel *c)
131ca32bd8dSchristos {
132ca32bd8dSchristos 	debug2("channel %d: ibuf empty", c->self);
13345a1cd19Schristos 	if (sshbuf_len(c->input)) {
134ca32bd8dSchristos 		error("channel %d: chan_ibuf_empty for non empty buffer",
135ca32bd8dSchristos 		    c->self);
136ca32bd8dSchristos 		return;
137ca32bd8dSchristos 	}
138ca32bd8dSchristos 	switch (c->istate) {
139ca32bd8dSchristos 	case CHAN_INPUT_WAIT_DRAIN:
14034b27b53Sadam 		if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
14145a1cd19Schristos 			chan_send_eof2(ssh, c);
142ca32bd8dSchristos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
143ca32bd8dSchristos 		break;
144ca32bd8dSchristos 	default:
145ca32bd8dSchristos 		error("channel %d: chan_ibuf_empty for istate %d",
146ca32bd8dSchristos 		    c->self, c->istate);
147ca32bd8dSchristos 		break;
148ca32bd8dSchristos 	}
149ca32bd8dSchristos }
15045a1cd19Schristos 
151ca32bd8dSchristos void
chan_obuf_empty(struct ssh * ssh,Channel * c)15245a1cd19Schristos chan_obuf_empty(struct ssh *ssh, Channel *c)
153ca32bd8dSchristos {
154ca32bd8dSchristos 	debug2("channel %d: obuf empty", c->self);
15545a1cd19Schristos 	if (sshbuf_len(c->output)) {
156ca32bd8dSchristos 		error("channel %d: chan_obuf_empty for non empty buffer",
157ca32bd8dSchristos 		    c->self);
158ca32bd8dSchristos 		return;
159ca32bd8dSchristos 	}
160ca32bd8dSchristos 	switch (c->ostate) {
161ca32bd8dSchristos 	case CHAN_OUTPUT_WAIT_DRAIN:
16245a1cd19Schristos 		chan_shutdown_write(ssh, c);
163ca32bd8dSchristos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
164ca32bd8dSchristos 		break;
165ca32bd8dSchristos 	default:
166ca32bd8dSchristos 		error("channel %d: internal error: obuf_empty for ostate %d",
167ca32bd8dSchristos 		    c->self, c->ostate);
168ca32bd8dSchristos 		break;
169ca32bd8dSchristos 	}
170ca32bd8dSchristos }
17145a1cd19Schristos 
17245a1cd19Schristos void
chan_rcvd_eow(struct ssh * ssh,Channel * c)17345a1cd19Schristos chan_rcvd_eow(struct ssh *ssh, Channel *c)
174ca32bd8dSchristos {
17545a1cd19Schristos 	debug2("channel %d: rcvd eow", c->self);
176ca32bd8dSchristos 	switch (c->istate) {
177ca32bd8dSchristos 	case CHAN_INPUT_OPEN:
17845a1cd19Schristos 		chan_shutdown_read(ssh, c);
17945a1cd19Schristos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
180ca32bd8dSchristos 		break;
181ca32bd8dSchristos 	}
182ca32bd8dSchristos }
183ca32bd8dSchristos 
184ca32bd8dSchristos static void
chan_send_eof2(struct ssh * ssh,Channel * c)18545a1cd19Schristos chan_send_eof2(struct ssh *ssh, Channel *c)
18645a1cd19Schristos {
18745a1cd19Schristos 	int r;
18845a1cd19Schristos 
18945a1cd19Schristos 	debug2("channel %d: send eof", c->self);
19045a1cd19Schristos 	switch (c->istate) {
19145a1cd19Schristos 	case CHAN_INPUT_WAIT_DRAIN:
19245a1cd19Schristos 		if (!c->have_remote_id)
193c305e046Schristos 			fatal_f("channel %d: no remote_id", c->self);
19445a1cd19Schristos 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
19545a1cd19Schristos 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
19645a1cd19Schristos 		    (r = sshpkt_send(ssh)) != 0)
197c305e046Schristos 			fatal_fr(r, "send CHANNEL_EOF");
19845a1cd19Schristos 		c->flags |= CHAN_EOF_SENT;
19945a1cd19Schristos 		break;
20045a1cd19Schristos 	default:
20145a1cd19Schristos 		error("channel %d: cannot send eof for istate %d",
20245a1cd19Schristos 		    c->self, c->istate);
20345a1cd19Schristos 		break;
20445a1cd19Schristos 	}
20545a1cd19Schristos }
20645a1cd19Schristos 
20745a1cd19Schristos static void
chan_send_close2(struct ssh * ssh,Channel * c)20845a1cd19Schristos chan_send_close2(struct ssh *ssh, Channel *c)
20945a1cd19Schristos {
21045a1cd19Schristos 	int r;
21145a1cd19Schristos 
21245a1cd19Schristos 	debug2("channel %d: send close", c->self);
21345a1cd19Schristos 	if (c->ostate != CHAN_OUTPUT_CLOSED ||
21445a1cd19Schristos 	    c->istate != CHAN_INPUT_CLOSED) {
21545a1cd19Schristos 		error("channel %d: cannot send close for istate/ostate %d/%d",
21645a1cd19Schristos 		    c->self, c->istate, c->ostate);
21745a1cd19Schristos 	} else if (c->flags & CHAN_CLOSE_SENT) {
21845a1cd19Schristos 		error("channel %d: already sent close", c->self);
21945a1cd19Schristos 	} else {
22045a1cd19Schristos 		if (!c->have_remote_id)
221c305e046Schristos 			fatal_f("channel %d: no remote_id", c->self);
22245a1cd19Schristos 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
22345a1cd19Schristos 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
22445a1cd19Schristos 		    (r = sshpkt_send(ssh)) != 0)
225c305e046Schristos 			fatal_fr(r, "send CHANNEL_EOF");
22645a1cd19Schristos 		c->flags |= CHAN_CLOSE_SENT;
22745a1cd19Schristos 	}
22845a1cd19Schristos }
22945a1cd19Schristos 
23045a1cd19Schristos static void
chan_send_eow2(struct ssh * ssh,Channel * c)23145a1cd19Schristos chan_send_eow2(struct ssh *ssh, Channel *c)
23245a1cd19Schristos {
23345a1cd19Schristos 	int r;
23445a1cd19Schristos 
23545a1cd19Schristos 	debug2("channel %d: send eow", c->self);
23645a1cd19Schristos 	if (c->ostate == CHAN_OUTPUT_CLOSED) {
23745a1cd19Schristos 		error("channel %d: must not sent eow on closed output",
23845a1cd19Schristos 		    c->self);
23945a1cd19Schristos 		return;
24045a1cd19Schristos 	}
241c305e046Schristos 	if (!(ssh->compat & SSH_NEW_OPENSSH))
24245a1cd19Schristos 		return;
24345a1cd19Schristos 	if (!c->have_remote_id)
244c305e046Schristos 		fatal_f("channel %d: no remote_id", c->self);
24545a1cd19Schristos 	if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
24645a1cd19Schristos 	    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
24745a1cd19Schristos 	    (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
24845a1cd19Schristos 	    (r = sshpkt_put_u8(ssh, 0)) != 0 ||
24945a1cd19Schristos 	    (r = sshpkt_send(ssh)) != 0)
250c305e046Schristos 		fatal_fr(r, "send CHANNEL_EOF");
25145a1cd19Schristos }
25245a1cd19Schristos 
25345a1cd19Schristos /* shared */
25445a1cd19Schristos 
25545a1cd19Schristos void
chan_rcvd_ieof(struct ssh * ssh,Channel * c)25645a1cd19Schristos chan_rcvd_ieof(struct ssh *ssh, Channel *c)
25745a1cd19Schristos {
25845a1cd19Schristos 	debug2("channel %d: rcvd eof", c->self);
25945a1cd19Schristos 	c->flags |= CHAN_EOF_RCVD;
26045a1cd19Schristos 	if (c->ostate == CHAN_OUTPUT_OPEN)
26145a1cd19Schristos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
26245a1cd19Schristos 	if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
26345a1cd19Schristos 	    sshbuf_len(c->output) == 0 &&
26445a1cd19Schristos 	    !CHANNEL_EFD_OUTPUT_ACTIVE(c))
26545a1cd19Schristos 		chan_obuf_empty(ssh, c);
26645a1cd19Schristos }
26745a1cd19Schristos 
26845a1cd19Schristos void
chan_rcvd_oclose(struct ssh * ssh,Channel * c)26945a1cd19Schristos chan_rcvd_oclose(struct ssh *ssh, Channel *c)
270ca32bd8dSchristos {
271ca32bd8dSchristos 	debug2("channel %d: rcvd close", c->self);
27234b27b53Sadam 	if (!(c->flags & CHAN_LOCAL)) {
273ca32bd8dSchristos 		if (c->flags & CHAN_CLOSE_RCVD)
27434b27b53Sadam 			error("channel %d: protocol error: close rcvd twice",
27534b27b53Sadam 			    c->self);
276ca32bd8dSchristos 		c->flags |= CHAN_CLOSE_RCVD;
27734b27b53Sadam 	}
278ca32bd8dSchristos 	if (c->type == SSH_CHANNEL_LARVAL) {
279ca32bd8dSchristos 		/* tear down larval channels immediately */
280ca32bd8dSchristos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
281ca32bd8dSchristos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
282ca32bd8dSchristos 		return;
283ca32bd8dSchristos 	}
284ca32bd8dSchristos 	switch (c->ostate) {
285ca32bd8dSchristos 	case CHAN_OUTPUT_OPEN:
286ca32bd8dSchristos 		/*
287ca32bd8dSchristos 		 * wait until a data from the channel is consumed if a CLOSE
288ca32bd8dSchristos 		 * is received
289ca32bd8dSchristos 		 */
290ca32bd8dSchristos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
291ca32bd8dSchristos 		break;
292ca32bd8dSchristos 	}
293ca32bd8dSchristos 	switch (c->istate) {
294ca32bd8dSchristos 	case CHAN_INPUT_OPEN:
29545a1cd19Schristos 		chan_shutdown_read(ssh, c);
2968f892fcaSchristos 		chan_shutdown_extended_read(ssh, c);
297ca32bd8dSchristos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
298ca32bd8dSchristos 		break;
299ca32bd8dSchristos 	case CHAN_INPUT_WAIT_DRAIN:
30034b27b53Sadam 		if (!(c->flags & CHAN_LOCAL))
30145a1cd19Schristos 			chan_send_eof2(ssh, c);
3028f892fcaSchristos 		chan_shutdown_extended_read(ssh, c);
303ca32bd8dSchristos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
304ca32bd8dSchristos 		break;
305ca32bd8dSchristos 	}
306ca32bd8dSchristos }
30734b27b53Sadam 
308ca32bd8dSchristos void
chan_write_failed(struct ssh * ssh,Channel * c)30945a1cd19Schristos chan_write_failed(struct ssh *ssh, Channel *c)
310ca32bd8dSchristos {
311ca32bd8dSchristos 	debug2("channel %d: write failed", c->self);
312ca32bd8dSchristos 	switch (c->ostate) {
313ca32bd8dSchristos 	case CHAN_OUTPUT_OPEN:
314ca32bd8dSchristos 	case CHAN_OUTPUT_WAIT_DRAIN:
31545a1cd19Schristos 		chan_shutdown_write(ssh, c);
316ca32bd8dSchristos 		if (strcmp(c->ctype, "session") == 0)
31745a1cd19Schristos 			chan_send_eow2(ssh, c);
318ca32bd8dSchristos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
319ca32bd8dSchristos 		break;
320ca32bd8dSchristos 	default:
321ca32bd8dSchristos 		error("channel %d: chan_write_failed for ostate %d",
322ca32bd8dSchristos 		    c->self, c->ostate);
323ca32bd8dSchristos 		break;
324ca32bd8dSchristos 	}
325ca32bd8dSchristos }
326ca32bd8dSchristos 
327ca32bd8dSchristos void
chan_mark_dead(struct ssh * ssh,Channel * c)32845a1cd19Schristos chan_mark_dead(struct ssh *ssh, Channel *c)
329ca32bd8dSchristos {
330ca32bd8dSchristos 	c->type = SSH_CHANNEL_ZOMBIE;
331ca32bd8dSchristos }
332ca32bd8dSchristos 
333ca32bd8dSchristos int
chan_is_dead(struct ssh * ssh,Channel * c,int do_send)33445a1cd19Schristos chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
335ca32bd8dSchristos {
336ca32bd8dSchristos 	if (c->type == SSH_CHANNEL_ZOMBIE) {
337ca32bd8dSchristos 		debug2("channel %d: zombie", c->self);
338ca32bd8dSchristos 		return 1;
339ca32bd8dSchristos 	}
340ca32bd8dSchristos 	if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
341ca32bd8dSchristos 		return 0;
342c305e046Schristos 	if ((ssh->compat & SSH_BUG_EXTEOF) &&
343ca32bd8dSchristos 	    c->extended_usage == CHAN_EXTENDED_WRITE &&
344ca32bd8dSchristos 	    c->efd != -1 &&
34545a1cd19Schristos 	    sshbuf_len(c->extended) > 0) {
34645a1cd19Schristos 		debug2("channel %d: active efd: %d len %zu",
34745a1cd19Schristos 		    c->self, c->efd, sshbuf_len(c->extended));
348ca32bd8dSchristos 		return 0;
349ca32bd8dSchristos 	}
35034b27b53Sadam 	if (c->flags & CHAN_LOCAL) {
35134b27b53Sadam 		debug2("channel %d: is dead (local)", c->self);
35234b27b53Sadam 		return 1;
35334b27b53Sadam 	}
354ca32bd8dSchristos 	if (!(c->flags & CHAN_CLOSE_SENT)) {
355ca32bd8dSchristos 		if (do_send) {
35645a1cd19Schristos 			chan_send_close2(ssh, c);
357ca32bd8dSchristos 		} else {
358ca32bd8dSchristos 			/* channel would be dead if we sent a close */
359ca32bd8dSchristos 			if (c->flags & CHAN_CLOSE_RCVD) {
360ca32bd8dSchristos 				debug2("channel %d: almost dead",
361ca32bd8dSchristos 				    c->self);
362ca32bd8dSchristos 				return 1;
363ca32bd8dSchristos 			}
364ca32bd8dSchristos 		}
365ca32bd8dSchristos 	}
366ca32bd8dSchristos 	if ((c->flags & CHAN_CLOSE_SENT) &&
367ca32bd8dSchristos 	    (c->flags & CHAN_CLOSE_RCVD)) {
368ca32bd8dSchristos 		debug2("channel %d: is dead", c->self);
369ca32bd8dSchristos 		return 1;
370ca32bd8dSchristos 	}
371ca32bd8dSchristos 	return 0;
372ca32bd8dSchristos }
373ca32bd8dSchristos 
374ca32bd8dSchristos /* helper */
375ca32bd8dSchristos static void
chan_shutdown_write(struct ssh * ssh,Channel * c)37645a1cd19Schristos chan_shutdown_write(struct ssh *ssh, Channel *c)
377ca32bd8dSchristos {
37845a1cd19Schristos 	sshbuf_reset(c->output);
37945a1cd19Schristos 	if (c->type == SSH_CHANNEL_LARVAL)
380ca32bd8dSchristos 		return;
381ca32bd8dSchristos 	/* shutdown failure is allowed if write failed already */
382c305e046Schristos 	debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
383c305e046Schristos 	    c->self, c->istate, c->ostate, c->sock, c->wfd, c->efd,
3848f892fcaSchristos 	    channel_format_extended_usage(c));
385ca32bd8dSchristos 	if (c->sock != -1) {
3863f0e61bfSchristos 		if (shutdown(c->sock, SHUT_WR) == -1) {
387c305e046Schristos 			debug2_f("channel %d: shutdown() failed for "
388c305e046Schristos 			    "fd %d [i%d o%d]: %.100s", c->self, c->sock,
389c305e046Schristos 			    c->istate, c->ostate, strerror(errno));
3908f892fcaSchristos 		}
391ca32bd8dSchristos 	} else {
39219a1dfedSchristos 		if (channel_close_fd(ssh, c, &c->wfd) < 0) {
393c305e046Schristos 			logit_f("channel %d: close() failed for "
394c305e046Schristos 			    "fd %d [i%d o%d]: %.100s", c->self, c->wfd,
395c305e046Schristos 			    c->istate, c->ostate, strerror(errno));
3968f892fcaSchristos 		}
397ca32bd8dSchristos 	}
398ca32bd8dSchristos }
39945a1cd19Schristos 
400ca32bd8dSchristos static void
chan_shutdown_read(struct ssh * ssh,Channel * c)40145a1cd19Schristos chan_shutdown_read(struct ssh *ssh, Channel *c)
402ca32bd8dSchristos {
40345a1cd19Schristos 	if (c->type == SSH_CHANNEL_LARVAL)
404ca32bd8dSchristos 		return;
405c305e046Schristos 	debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
406c305e046Schristos 	    c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
4078f892fcaSchristos 	    channel_format_extended_usage(c));
408ca32bd8dSchristos 	if (c->sock != -1) {
4093f0e61bfSchristos 		if (shutdown(c->sock, SHUT_RD) == -1) {
410c305e046Schristos 			error_f("channel %d: shutdown() failed for "
411c305e046Schristos 			    "fd %d [i%d o%d]: %.100s", c->self, c->sock,
412c305e046Schristos 			    c->istate, c->ostate, strerror(errno));
4138f892fcaSchristos 		}
414ca32bd8dSchristos 	} else {
41519a1dfedSchristos 		if (channel_close_fd(ssh, c, &c->rfd) < 0) {
416c305e046Schristos 			logit_f("channel %d: close() failed for "
417c305e046Schristos 			    "fd %d [i%d o%d]: %.100s", c->self, c->rfd,
418c305e046Schristos 			    c->istate, c->ostate, strerror(errno));
4198f892fcaSchristos 		}
4208f892fcaSchristos 	}
4218f892fcaSchristos }
4228f892fcaSchristos 
4238f892fcaSchristos static void
chan_shutdown_extended_read(struct ssh * ssh,Channel * c)4248f892fcaSchristos chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
4258f892fcaSchristos {
4268f892fcaSchristos 	if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
4278f892fcaSchristos 		return;
4288f892fcaSchristos 	if (c->extended_usage != CHAN_EXTENDED_READ &&
4298f892fcaSchristos 	    c->extended_usage != CHAN_EXTENDED_IGNORE)
4308f892fcaSchristos 		return;
431c305e046Schristos 	debug_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
432c305e046Schristos 	    c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
4338f892fcaSchristos 	    channel_format_extended_usage(c));
43419a1dfedSchristos 	if (channel_close_fd(ssh, c, &c->efd) < 0) {
435c305e046Schristos 		logit_f("channel %d: close() failed for "
436c305e046Schristos 		    "extended fd %d [i%d o%d]: %.100s", c->self, c->efd,
437c305e046Schristos 		    c->istate, c->ostate, strerror(errno));
438ca32bd8dSchristos 	}
439ca32bd8dSchristos }
440