Home | History | Annotate | Line # | Download | only in dist
nchan.c revision 1.1.1.6
      1  1.1.1.6  christos /* $OpenBSD: nchan.c,v 1.70 2019/06/28 13:35:04 deraadt Exp $ */
      2      1.1  christos /*
      3      1.1  christos  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
      4      1.1  christos  *
      5      1.1  christos  * Redistribution and use in source and binary forms, with or without
      6      1.1  christos  * modification, are permitted provided that the following conditions
      7      1.1  christos  * are met:
      8      1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  christos  *    documentation and/or other materials provided with the distribution.
     13      1.1  christos  *
     14      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     15      1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     16      1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     17      1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     18      1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     19      1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     20      1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     21      1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22      1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23      1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24      1.1  christos  */
     25      1.1  christos 
     26      1.1  christos #include <sys/types.h>
     27      1.1  christos #include <sys/socket.h>
     28      1.1  christos #include <sys/queue.h>
     29      1.1  christos 
     30      1.1  christos #include <errno.h>
     31      1.1  christos #include <string.h>
     32      1.1  christos #include <stdarg.h>
     33      1.1  christos 
     34      1.1  christos #include "ssh2.h"
     35  1.1.1.4  christos #include "sshbuf.h"
     36  1.1.1.4  christos #include "ssherr.h"
     37      1.1  christos #include "packet.h"
     38      1.1  christos #include "channels.h"
     39      1.1  christos #include "compat.h"
     40      1.1  christos #include "log.h"
     41      1.1  christos 
     42      1.1  christos /*
     43      1.1  christos  * SSH Protocol 1.5 aka New Channel Protocol
     44      1.1  christos  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
     45      1.1  christos  * Written by Markus Friedl in October 1999
     46      1.1  christos  *
     47      1.1  christos  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
     48      1.1  christos  * tear down of channels:
     49      1.1  christos  *
     50      1.1  christos  * 1.3:	strict request-ack-protocol:
     51      1.1  christos  *	CLOSE	->
     52      1.1  christos  *		<-  CLOSE_CONFIRM
     53      1.1  christos  *
     54      1.1  christos  * 1.5:	uses variations of:
     55      1.1  christos  *	IEOF	->
     56      1.1  christos  *		<-  OCLOSE
     57      1.1  christos  *		<-  IEOF
     58      1.1  christos  *	OCLOSE	->
     59      1.1  christos  *	i.e. both sides have to close the channel
     60      1.1  christos  *
     61      1.1  christos  * 2.0: the EOF messages are optional
     62      1.1  christos  *
     63      1.1  christos  * See the debugging output from 'ssh -v' and 'sshd -d' of
     64      1.1  christos  * ssh-1.2.27 as an example.
     65      1.1  christos  *
     66      1.1  christos  */
     67      1.1  christos 
     68      1.1  christos /* functions manipulating channel states */
     69      1.1  christos /*
     70      1.1  christos  * EVENTS update channel input/output states execute ACTIONS
     71      1.1  christos  */
     72      1.1  christos /*
     73      1.1  christos  * ACTIONS: should never update the channel states
     74      1.1  christos  */
     75  1.1.1.4  christos static void	chan_send_eof2(struct ssh *, Channel *);
     76  1.1.1.4  christos static void	chan_send_eow2(struct ssh *, Channel *);
     77      1.1  christos 
     78      1.1  christos /* helper */
     79  1.1.1.4  christos static void	chan_shutdown_write(struct ssh *, Channel *);
     80  1.1.1.4  christos static void	chan_shutdown_read(struct ssh *, Channel *);
     81  1.1.1.5  christos static void	chan_shutdown_extended_read(struct ssh *, Channel *);
     82      1.1  christos 
     83  1.1.1.4  christos static const char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
     84  1.1.1.4  christos static const char *istates[] = { "open", "drain", "wait_oclose", "closed" };
     85      1.1  christos 
     86      1.1  christos static void
     87      1.1  christos chan_set_istate(Channel *c, u_int next)
     88      1.1  christos {
     89      1.1  christos 	if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
     90      1.1  christos 		fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
     91      1.1  christos 	debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
     92      1.1  christos 	    istates[next]);
     93      1.1  christos 	c->istate = next;
     94      1.1  christos }
     95  1.1.1.4  christos 
     96      1.1  christos static void
     97      1.1  christos chan_set_ostate(Channel *c, u_int next)
     98      1.1  christos {
     99      1.1  christos 	if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
    100      1.1  christos 		fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
    101      1.1  christos 	debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
    102      1.1  christos 	    ostates[next]);
    103      1.1  christos 	c->ostate = next;
    104      1.1  christos }
    105      1.1  christos 
    106      1.1  christos void
    107  1.1.1.4  christos chan_read_failed(struct ssh *ssh, Channel *c)
    108      1.1  christos {
    109      1.1  christos 	debug2("channel %d: read failed", c->self);
    110      1.1  christos 	switch (c->istate) {
    111      1.1  christos 	case CHAN_INPUT_OPEN:
    112  1.1.1.4  christos 		chan_shutdown_read(ssh, c);
    113      1.1  christos 		chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
    114      1.1  christos 		break;
    115      1.1  christos 	default:
    116      1.1  christos 		error("channel %d: chan_read_failed for istate %d",
    117      1.1  christos 		    c->self, c->istate);
    118      1.1  christos 		break;
    119      1.1  christos 	}
    120      1.1  christos }
    121  1.1.1.4  christos 
    122      1.1  christos void
    123  1.1.1.4  christos chan_ibuf_empty(struct ssh *ssh, Channel *c)
    124      1.1  christos {
    125      1.1  christos 	debug2("channel %d: ibuf empty", c->self);
    126  1.1.1.4  christos 	if (sshbuf_len(c->input)) {
    127      1.1  christos 		error("channel %d: chan_ibuf_empty for non empty buffer",
    128      1.1  christos 		    c->self);
    129      1.1  christos 		return;
    130      1.1  christos 	}
    131      1.1  christos 	switch (c->istate) {
    132      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    133  1.1.1.4  christos 		if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
    134  1.1.1.4  christos 			chan_send_eof2(ssh, c);
    135  1.1.1.4  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    136      1.1  christos 		break;
    137      1.1  christos 	default:
    138      1.1  christos 		error("channel %d: chan_ibuf_empty for istate %d",
    139      1.1  christos 		    c->self, c->istate);
    140      1.1  christos 		break;
    141      1.1  christos 	}
    142      1.1  christos }
    143  1.1.1.4  christos 
    144      1.1  christos void
    145  1.1.1.4  christos chan_obuf_empty(struct ssh *ssh, Channel *c)
    146      1.1  christos {
    147      1.1  christos 	debug2("channel %d: obuf empty", c->self);
    148  1.1.1.4  christos 	if (sshbuf_len(c->output)) {
    149      1.1  christos 		error("channel %d: chan_obuf_empty for non empty buffer",
    150      1.1  christos 		    c->self);
    151      1.1  christos 		return;
    152      1.1  christos 	}
    153      1.1  christos 	switch (c->ostate) {
    154      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    155  1.1.1.4  christos 		chan_shutdown_write(ssh, c);
    156      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    157      1.1  christos 		break;
    158      1.1  christos 	default:
    159      1.1  christos 		error("channel %d: internal error: obuf_empty for ostate %d",
    160      1.1  christos 		    c->self, c->ostate);
    161      1.1  christos 		break;
    162      1.1  christos 	}
    163      1.1  christos }
    164  1.1.1.4  christos 
    165  1.1.1.4  christos void
    166  1.1.1.4  christos chan_rcvd_eow(struct ssh *ssh, Channel *c)
    167      1.1  christos {
    168  1.1.1.4  christos 	debug2("channel %d: rcvd eow", c->self);
    169      1.1  christos 	switch (c->istate) {
    170      1.1  christos 	case CHAN_INPUT_OPEN:
    171  1.1.1.4  christos 		chan_shutdown_read(ssh, c);
    172  1.1.1.4  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    173  1.1.1.4  christos 		break;
    174  1.1.1.4  christos 	}
    175  1.1.1.4  christos }
    176  1.1.1.4  christos 
    177  1.1.1.4  christos static void
    178  1.1.1.4  christos chan_send_eof2(struct ssh *ssh, Channel *c)
    179  1.1.1.4  christos {
    180  1.1.1.4  christos 	int r;
    181  1.1.1.4  christos 
    182  1.1.1.4  christos 	debug2("channel %d: send eof", c->self);
    183  1.1.1.4  christos 	switch (c->istate) {
    184      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    185  1.1.1.4  christos 		if (!c->have_remote_id)
    186  1.1.1.4  christos 			fatal("%s: channel %d: no remote_id",
    187  1.1.1.4  christos 			    __func__, c->self);
    188  1.1.1.4  christos 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
    189  1.1.1.4  christos 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
    190  1.1.1.4  christos 		    (r = sshpkt_send(ssh)) != 0)
    191  1.1.1.4  christos 			fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
    192  1.1.1.4  christos 		c->flags |= CHAN_EOF_SENT;
    193      1.1  christos 		break;
    194      1.1  christos 	default:
    195  1.1.1.4  christos 		error("channel %d: cannot send eof for istate %d",
    196      1.1  christos 		    c->self, c->istate);
    197      1.1  christos 		break;
    198      1.1  christos 	}
    199      1.1  christos }
    200  1.1.1.4  christos 
    201      1.1  christos static void
    202  1.1.1.4  christos chan_send_close2(struct ssh *ssh, Channel *c)
    203      1.1  christos {
    204  1.1.1.4  christos 	int r;
    205  1.1.1.4  christos 
    206  1.1.1.4  christos 	debug2("channel %d: send close", c->self);
    207  1.1.1.4  christos 	if (c->ostate != CHAN_OUTPUT_CLOSED ||
    208  1.1.1.4  christos 	    c->istate != CHAN_INPUT_CLOSED) {
    209  1.1.1.4  christos 		error("channel %d: cannot send close for istate/ostate %d/%d",
    210  1.1.1.4  christos 		    c->self, c->istate, c->ostate);
    211  1.1.1.4  christos 	} else if (c->flags & CHAN_CLOSE_SENT) {
    212  1.1.1.4  christos 		error("channel %d: already sent close", c->self);
    213  1.1.1.4  christos 	} else {
    214  1.1.1.4  christos 		if (!c->have_remote_id)
    215  1.1.1.4  christos 			fatal("%s: channel %d: no remote_id",
    216  1.1.1.4  christos 			    __func__, c->self);
    217  1.1.1.4  christos 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
    218  1.1.1.4  christos 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
    219  1.1.1.4  christos 		    (r = sshpkt_send(ssh)) != 0)
    220  1.1.1.4  christos 			fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
    221  1.1.1.4  christos 		c->flags |= CHAN_CLOSE_SENT;
    222      1.1  christos 	}
    223      1.1  christos }
    224      1.1  christos 
    225      1.1  christos static void
    226  1.1.1.4  christos chan_send_eow2(struct ssh *ssh, Channel *c)
    227  1.1.1.4  christos {
    228  1.1.1.4  christos 	int r;
    229  1.1.1.4  christos 
    230  1.1.1.4  christos 	debug2("channel %d: send eow", c->self);
    231  1.1.1.4  christos 	if (c->ostate == CHAN_OUTPUT_CLOSED) {
    232  1.1.1.4  christos 		error("channel %d: must not sent eow on closed output",
    233  1.1.1.4  christos 		    c->self);
    234  1.1.1.4  christos 		return;
    235  1.1.1.4  christos 	}
    236  1.1.1.4  christos 	if (!(datafellows & SSH_NEW_OPENSSH))
    237  1.1.1.4  christos 		return;
    238  1.1.1.4  christos 	if (!c->have_remote_id)
    239  1.1.1.4  christos 		fatal("%s: channel %d: no remote_id", __func__, c->self);
    240  1.1.1.4  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
    241  1.1.1.4  christos 	    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
    242  1.1.1.4  christos 	    (r = sshpkt_put_cstring(ssh, "eow (at) openssh.com")) != 0 ||
    243  1.1.1.4  christos 	    (r = sshpkt_put_u8(ssh, 0)) != 0 ||
    244  1.1.1.4  christos 	    (r = sshpkt_send(ssh)) != 0)
    245  1.1.1.4  christos 		fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
    246  1.1.1.4  christos }
    247  1.1.1.4  christos 
    248  1.1.1.4  christos /* shared */
    249  1.1.1.4  christos 
    250  1.1.1.4  christos void
    251  1.1.1.4  christos chan_rcvd_ieof(struct ssh *ssh, Channel *c)
    252  1.1.1.4  christos {
    253  1.1.1.4  christos 	debug2("channel %d: rcvd eof", c->self);
    254  1.1.1.4  christos 	c->flags |= CHAN_EOF_RCVD;
    255  1.1.1.4  christos 	if (c->ostate == CHAN_OUTPUT_OPEN)
    256  1.1.1.4  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
    257  1.1.1.4  christos 	if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
    258  1.1.1.4  christos 	    sshbuf_len(c->output) == 0 &&
    259  1.1.1.4  christos 	    !CHANNEL_EFD_OUTPUT_ACTIVE(c))
    260  1.1.1.4  christos 		chan_obuf_empty(ssh, c);
    261  1.1.1.4  christos }
    262  1.1.1.4  christos 
    263  1.1.1.4  christos void
    264  1.1.1.4  christos chan_rcvd_oclose(struct ssh *ssh, Channel *c)
    265      1.1  christos {
    266      1.1  christos 	debug2("channel %d: rcvd close", c->self);
    267  1.1.1.2      adam 	if (!(c->flags & CHAN_LOCAL)) {
    268  1.1.1.2      adam 		if (c->flags & CHAN_CLOSE_RCVD)
    269  1.1.1.2      adam 			error("channel %d: protocol error: close rcvd twice",
    270  1.1.1.2      adam 			    c->self);
    271  1.1.1.2      adam 		c->flags |= CHAN_CLOSE_RCVD;
    272  1.1.1.2      adam 	}
    273      1.1  christos 	if (c->type == SSH_CHANNEL_LARVAL) {
    274      1.1  christos 		/* tear down larval channels immediately */
    275      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    276      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    277      1.1  christos 		return;
    278      1.1  christos 	}
    279      1.1  christos 	switch (c->ostate) {
    280      1.1  christos 	case CHAN_OUTPUT_OPEN:
    281      1.1  christos 		/*
    282      1.1  christos 		 * wait until a data from the channel is consumed if a CLOSE
    283      1.1  christos 		 * is received
    284      1.1  christos 		 */
    285      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
    286      1.1  christos 		break;
    287      1.1  christos 	}
    288      1.1  christos 	switch (c->istate) {
    289      1.1  christos 	case CHAN_INPUT_OPEN:
    290  1.1.1.4  christos 		chan_shutdown_read(ssh, c);
    291  1.1.1.5  christos 		chan_shutdown_extended_read(ssh, c);
    292      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    293      1.1  christos 		break;
    294      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    295  1.1.1.2      adam 		if (!(c->flags & CHAN_LOCAL))
    296  1.1.1.4  christos 			chan_send_eof2(ssh, c);
    297  1.1.1.5  christos 		chan_shutdown_extended_read(ssh, c);
    298      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    299      1.1  christos 		break;
    300      1.1  christos 	}
    301      1.1  christos }
    302  1.1.1.2      adam 
    303      1.1  christos void
    304  1.1.1.4  christos chan_write_failed(struct ssh *ssh, Channel *c)
    305      1.1  christos {
    306      1.1  christos 	debug2("channel %d: write failed", c->self);
    307      1.1  christos 	switch (c->ostate) {
    308      1.1  christos 	case CHAN_OUTPUT_OPEN:
    309      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    310  1.1.1.4  christos 		chan_shutdown_write(ssh, c);
    311      1.1  christos 		if (strcmp(c->ctype, "session") == 0)
    312  1.1.1.4  christos 			chan_send_eow2(ssh, c);
    313      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    314      1.1  christos 		break;
    315      1.1  christos 	default:
    316      1.1  christos 		error("channel %d: chan_write_failed for ostate %d",
    317      1.1  christos 		    c->self, c->ostate);
    318      1.1  christos 		break;
    319      1.1  christos 	}
    320      1.1  christos }
    321      1.1  christos 
    322      1.1  christos void
    323  1.1.1.4  christos chan_mark_dead(struct ssh *ssh, Channel *c)
    324      1.1  christos {
    325      1.1  christos 	c->type = SSH_CHANNEL_ZOMBIE;
    326      1.1  christos }
    327      1.1  christos 
    328      1.1  christos int
    329  1.1.1.4  christos chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
    330      1.1  christos {
    331      1.1  christos 	if (c->type == SSH_CHANNEL_ZOMBIE) {
    332      1.1  christos 		debug2("channel %d: zombie", c->self);
    333      1.1  christos 		return 1;
    334      1.1  christos 	}
    335      1.1  christos 	if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
    336      1.1  christos 		return 0;
    337      1.1  christos 	if ((datafellows & SSH_BUG_EXTEOF) &&
    338      1.1  christos 	    c->extended_usage == CHAN_EXTENDED_WRITE &&
    339      1.1  christos 	    c->efd != -1 &&
    340  1.1.1.4  christos 	    sshbuf_len(c->extended) > 0) {
    341  1.1.1.4  christos 		debug2("channel %d: active efd: %d len %zu",
    342  1.1.1.4  christos 		    c->self, c->efd, sshbuf_len(c->extended));
    343      1.1  christos 		return 0;
    344      1.1  christos 	}
    345  1.1.1.2      adam 	if (c->flags & CHAN_LOCAL) {
    346  1.1.1.2      adam 		debug2("channel %d: is dead (local)", c->self);
    347  1.1.1.2      adam 		return 1;
    348  1.1.1.2      adam 	}
    349      1.1  christos 	if (!(c->flags & CHAN_CLOSE_SENT)) {
    350      1.1  christos 		if (do_send) {
    351  1.1.1.4  christos 			chan_send_close2(ssh, c);
    352      1.1  christos 		} else {
    353      1.1  christos 			/* channel would be dead if we sent a close */
    354      1.1  christos 			if (c->flags & CHAN_CLOSE_RCVD) {
    355      1.1  christos 				debug2("channel %d: almost dead",
    356      1.1  christos 				    c->self);
    357      1.1  christos 				return 1;
    358      1.1  christos 			}
    359      1.1  christos 		}
    360      1.1  christos 	}
    361      1.1  christos 	if ((c->flags & CHAN_CLOSE_SENT) &&
    362      1.1  christos 	    (c->flags & CHAN_CLOSE_RCVD)) {
    363      1.1  christos 		debug2("channel %d: is dead", c->self);
    364      1.1  christos 		return 1;
    365      1.1  christos 	}
    366      1.1  christos 	return 0;
    367      1.1  christos }
    368      1.1  christos 
    369      1.1  christos /* helper */
    370      1.1  christos static void
    371  1.1.1.4  christos chan_shutdown_write(struct ssh *ssh, Channel *c)
    372      1.1  christos {
    373  1.1.1.4  christos 	sshbuf_reset(c->output);
    374  1.1.1.4  christos 	if (c->type == SSH_CHANNEL_LARVAL)
    375      1.1  christos 		return;
    376      1.1  christos 	/* shutdown failure is allowed if write failed already */
    377  1.1.1.5  christos 	debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
    378  1.1.1.5  christos 	    c->self, __func__, c->istate, c->ostate, c->sock, c->wfd, c->efd,
    379  1.1.1.5  christos 	    channel_format_extended_usage(c));
    380      1.1  christos 	if (c->sock != -1) {
    381  1.1.1.6  christos 		if (shutdown(c->sock, SHUT_WR) == -1) {
    382  1.1.1.5  christos 			debug2("channel %d: %s: shutdown() failed for "
    383  1.1.1.5  christos 			    "fd %d [i%d o%d]: %.100s", c->self, __func__,
    384  1.1.1.5  christos 			    c->sock, c->istate, c->ostate,
    385  1.1.1.5  christos 			    strerror(errno));
    386  1.1.1.5  christos 		}
    387      1.1  christos 	} else {
    388  1.1.1.5  christos 		if (channel_close_fd(ssh, &c->wfd) < 0) {
    389  1.1.1.5  christos 			logit("channel %d: %s: close() failed for "
    390  1.1.1.5  christos 			    "fd %d [i%d o%d]: %.100s",
    391  1.1.1.5  christos 			    c->self, __func__, c->wfd, c->istate, c->ostate,
    392  1.1.1.5  christos 			    strerror(errno));
    393  1.1.1.5  christos 		}
    394      1.1  christos 	}
    395      1.1  christos }
    396  1.1.1.4  christos 
    397      1.1  christos static void
    398  1.1.1.4  christos chan_shutdown_read(struct ssh *ssh, Channel *c)
    399      1.1  christos {
    400  1.1.1.4  christos 	if (c->type == SSH_CHANNEL_LARVAL)
    401      1.1  christos 		return;
    402  1.1.1.5  christos 	debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
    403  1.1.1.5  christos 	    c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
    404  1.1.1.5  christos 	    channel_format_extended_usage(c));
    405      1.1  christos 	if (c->sock != -1) {
    406  1.1.1.6  christos 		if (shutdown(c->sock, SHUT_RD) == -1) {
    407  1.1.1.5  christos 			error("channel %d: %s: shutdown() failed for "
    408  1.1.1.5  christos 			    "fd %d [i%d o%d]: %.100s",
    409  1.1.1.5  christos 			    c->self, __func__, c->sock, c->istate, c->ostate,
    410      1.1  christos 			    strerror(errno));
    411  1.1.1.5  christos 		}
    412      1.1  christos 	} else {
    413  1.1.1.5  christos 		if (channel_close_fd(ssh, &c->rfd) < 0) {
    414  1.1.1.5  christos 			logit("channel %d: %s: close() failed for "
    415  1.1.1.5  christos 			    "fd %d [i%d o%d]: %.100s",
    416  1.1.1.5  christos 			    c->self, __func__, c->rfd, c->istate, c->ostate,
    417  1.1.1.5  christos 			    strerror(errno));
    418  1.1.1.5  christos 		}
    419  1.1.1.5  christos 	}
    420  1.1.1.5  christos }
    421  1.1.1.5  christos 
    422  1.1.1.5  christos static void
    423  1.1.1.5  christos chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
    424  1.1.1.5  christos {
    425  1.1.1.5  christos 	if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
    426  1.1.1.5  christos 		return;
    427  1.1.1.5  christos 	if (c->extended_usage != CHAN_EXTENDED_READ &&
    428  1.1.1.5  christos 	    c->extended_usage != CHAN_EXTENDED_IGNORE)
    429  1.1.1.5  christos 		return;
    430  1.1.1.5  christos 	debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
    431  1.1.1.5  christos 	    c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
    432  1.1.1.5  christos 	    channel_format_extended_usage(c));
    433  1.1.1.5  christos 	if (channel_close_fd(ssh, &c->efd) < 0) {
    434  1.1.1.5  christos 		logit("channel %d: %s: close() failed for "
    435  1.1.1.5  christos 		    "extended fd %d [i%d o%d]: %.100s",
    436  1.1.1.5  christos 		    c->self, __func__, c->efd, c->istate, c->ostate,
    437  1.1.1.5  christos 		    strerror(errno));
    438      1.1  christos 	}
    439      1.1  christos }
    440