2 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: nchan.c,v 1.51 2004/07/11 17:48:47 deraadt Exp $");
37 * SSH Protocol 1.5 aka New Channel Protocol
38 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
39 * Written by Markus Friedl in October 1999
41 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
42 * tear down of channels:
44 * 1.3: strict request-ack-protocol:
48 * 1.5: uses variations of:
53 * i.e. both sides have to close the channel
55 * 2.0: the EOF messages are optional
57 * See the debugging output from 'ssh -v' and 'sshd -d' of
58 * ssh-1.2.27 as an example.
62 /* functions manipulating channel states */
64 * EVENTS update channel input/output states execute ACTIONS
67 * ACTIONS: should never update the channel states
69 static void chan_send_ieof1(Channel *);
70 static void chan_send_oclose1(Channel *);
71 static void chan_send_close2(Channel *);
72 static void chan_send_eof2(Channel *);
75 static void chan_shutdown_write(Channel *);
76 static void chan_shutdown_read(Channel *);
78 static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
79 static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
82 chan_set_istate(Channel *c, u_int next)
84 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
85 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
86 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
91 chan_set_ostate(Channel *c, u_int next)
93 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
94 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
95 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
101 * SSH1 specific implementation of event functions
105 chan_rcvd_oclose1(Channel *c)
107 debug2("channel %d: rcvd oclose", c->self);
109 case CHAN_INPUT_WAIT_OCLOSE:
110 chan_set_istate(c, CHAN_INPUT_CLOSED);
112 case CHAN_INPUT_OPEN:
113 chan_shutdown_read(c);
115 chan_set_istate(c, CHAN_INPUT_CLOSED);
117 case CHAN_INPUT_WAIT_DRAIN:
118 /* both local read_failed and remote write_failed */
120 chan_set_istate(c, CHAN_INPUT_CLOSED);
123 error("channel %d: protocol error: rcvd_oclose for istate %d",
129 chan_read_failed(Channel *c)
131 debug2("channel %d: read failed", c->self);
133 case CHAN_INPUT_OPEN:
134 chan_shutdown_read(c);
135 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
138 error("channel %d: chan_read_failed for istate %d",
144 chan_ibuf_empty(Channel *c)
146 debug2("channel %d: ibuf empty", c->self);
147 if (buffer_len(&c->input)) {
148 error("channel %d: chan_ibuf_empty for non empty buffer",
153 case CHAN_INPUT_WAIT_DRAIN:
155 if (!(c->flags & CHAN_CLOSE_SENT))
157 chan_set_istate(c, CHAN_INPUT_CLOSED);
160 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
164 error("channel %d: chan_ibuf_empty for istate %d",
170 chan_rcvd_ieof1(Channel *c)
172 debug2("channel %d: rcvd ieof", c->self);
174 case CHAN_OUTPUT_OPEN:
175 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
177 case CHAN_OUTPUT_WAIT_IEOF:
178 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
181 error("channel %d: protocol error: rcvd_ieof for ostate %d",
187 chan_write_failed1(Channel *c)
189 debug2("channel %d: write failed", c->self);
191 case CHAN_OUTPUT_OPEN:
192 chan_shutdown_write(c);
193 chan_send_oclose1(c);
194 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
196 case CHAN_OUTPUT_WAIT_DRAIN:
197 chan_shutdown_write(c);
198 chan_send_oclose1(c);
199 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
202 error("channel %d: chan_write_failed for ostate %d",
208 chan_obuf_empty(Channel *c)
210 debug2("channel %d: obuf empty", c->self);
211 if (buffer_len(&c->output)) {
212 error("channel %d: chan_obuf_empty for non empty buffer",
217 case CHAN_OUTPUT_WAIT_DRAIN:
218 chan_shutdown_write(c);
220 chan_send_oclose1(c);
221 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
224 error("channel %d: internal error: obuf_empty for ostate %d",
230 chan_send_ieof1(Channel *c)
232 debug2("channel %d: send ieof", c->self);
234 case CHAN_INPUT_OPEN:
235 case CHAN_INPUT_WAIT_DRAIN:
236 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
237 packet_put_int(c->remote_id);
241 error("channel %d: cannot send ieof for istate %d",
247 chan_send_oclose1(Channel *c)
249 debug2("channel %d: send oclose", c->self);
251 case CHAN_OUTPUT_OPEN:
252 case CHAN_OUTPUT_WAIT_DRAIN:
253 buffer_clear(&c->output);
254 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
255 packet_put_int(c->remote_id);
259 error("channel %d: cannot send oclose for ostate %d",
269 chan_rcvd_close2(Channel *c)
271 debug2("channel %d: rcvd close", c->self);
272 if (c->flags & CHAN_CLOSE_RCVD)
273 error("channel %d: protocol error: close rcvd twice", c->self);
274 c->flags |= CHAN_CLOSE_RCVD;
275 if (c->type == SSH_CHANNEL_LARVAL) {
276 /* tear down larval channels immediately */
277 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
278 chan_set_istate(c, CHAN_INPUT_CLOSED);
282 case CHAN_OUTPUT_OPEN:
284 * wait until a data from the channel is consumed if a CLOSE
287 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
291 case CHAN_INPUT_OPEN:
292 chan_shutdown_read(c);
293 chan_set_istate(c, CHAN_INPUT_CLOSED);
295 case CHAN_INPUT_WAIT_DRAIN:
297 chan_set_istate(c, CHAN_INPUT_CLOSED);
302 chan_rcvd_eof2(Channel *c)
304 debug2("channel %d: rcvd eof", c->self);
305 c->flags |= CHAN_EOF_RCVD;
306 if (c->ostate == CHAN_OUTPUT_OPEN)
307 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
310 chan_write_failed2(Channel *c)
312 debug2("channel %d: write failed", c->self);
314 case CHAN_OUTPUT_OPEN:
315 case CHAN_OUTPUT_WAIT_DRAIN:
316 chan_shutdown_write(c);
317 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
320 error("channel %d: chan_write_failed for ostate %d",
326 chan_send_eof2(Channel *c)
328 debug2("channel %d: send eof", c->self);
330 case CHAN_INPUT_WAIT_DRAIN:
331 packet_start(SSH2_MSG_CHANNEL_EOF);
332 packet_put_int(c->remote_id);
334 c->flags |= CHAN_EOF_SENT;
337 error("channel %d: cannot send eof for istate %d",
343 chan_send_close2(Channel *c)
345 debug2("channel %d: send close", c->self);
346 if (c->ostate != CHAN_OUTPUT_CLOSED ||
347 c->istate != CHAN_INPUT_CLOSED) {
348 error("channel %d: cannot send close for istate/ostate %d/%d",
349 c->self, c->istate, c->ostate);
350 } else if (c->flags & CHAN_CLOSE_SENT) {
351 error("channel %d: already sent close", c->self);
353 packet_start(SSH2_MSG_CHANNEL_CLOSE);
354 packet_put_int(c->remote_id);
356 c->flags |= CHAN_CLOSE_SENT;
363 chan_rcvd_ieof(Channel *c)
369 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
370 buffer_len(&c->output) == 0 &&
371 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
375 chan_rcvd_oclose(Channel *c)
380 chan_rcvd_oclose1(c);
383 chan_write_failed(Channel *c)
386 chan_write_failed2(c);
388 chan_write_failed1(c);
392 chan_mark_dead(Channel *c)
394 c->type = SSH_CHANNEL_ZOMBIE;
398 chan_is_dead(Channel *c, int do_send)
400 if (c->type == SSH_CHANNEL_ZOMBIE) {
401 debug2("channel %d: zombie", c->self);
404 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
407 debug2("channel %d: is dead", c->self);
410 if ((datafellows & SSH_BUG_EXTEOF) &&
411 c->extended_usage == CHAN_EXTENDED_WRITE &&
413 buffer_len(&c->extended) > 0) {
414 debug2("channel %d: active efd: %d len %d",
415 c->self, c->efd, buffer_len(&c->extended));
418 if (!(c->flags & CHAN_CLOSE_SENT)) {
422 /* channel would be dead if we sent a close */
423 if (c->flags & CHAN_CLOSE_RCVD) {
424 debug2("channel %d: almost dead",
430 if ((c->flags & CHAN_CLOSE_SENT) &&
431 (c->flags & CHAN_CLOSE_RCVD)) {
432 debug2("channel %d: is dead", c->self);
440 chan_shutdown_write(Channel *c)
442 buffer_clear(&c->output);
443 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
445 /* shutdown failure is allowed if write failed already */
446 debug2("channel %d: close_write", c->self);
448 if (shutdown(c->sock, SHUT_WR) < 0)
449 debug2("channel %d: chan_shutdown_write: "
450 "shutdown() failed for fd%d: %.100s",
451 c->self, c->sock, strerror(errno));
453 if (channel_close_fd(&c->wfd) < 0)
454 logit("channel %d: chan_shutdown_write: "
455 "close() failed for fd%d: %.100s",
456 c->self, c->wfd, strerror(errno));
460 chan_shutdown_read(Channel *c)
462 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
464 debug2("channel %d: close_read", c->self);
467 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
468 * write side has been closed already. (bug on Linux)
469 * HP-UX may return ENOTCONN also.
471 if (shutdown(c->sock, SHUT_RD) < 0
472 && errno != ENOTCONN)
473 error("channel %d: chan_shutdown_read: "
474 "shutdown() failed for fd%d [i%d o%d]: %.100s",
475 c->self, c->sock, c->istate, c->ostate,
478 if (channel_close_fd(&c->rfd) < 0)
479 logit("channel %d: chan_shutdown_read: "
480 "close() failed for fd%d: %.100s",
481 c->self, c->rfd, strerror(errno));