2 * Copyright (c) 1999, 2000, 2001 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.32 2001/10/10 22:18:47 markus 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
66 /* events concerning the INPUT from socket for channel (istate) */
67 chan_event_fn *chan_rcvd_oclose = NULL;
68 chan_event_fn *chan_read_failed = NULL;
69 chan_event_fn *chan_ibuf_empty = NULL;
70 /* events concerning the OUTPUT from channel for socket (ostate) */
71 chan_event_fn *chan_rcvd_ieof = NULL;
72 chan_event_fn *chan_write_failed = NULL;
73 chan_event_fn *chan_obuf_empty = NULL;
75 * ACTIONS: should never update the channel states
77 static void chan_send_ieof1(Channel *);
78 static void chan_send_oclose1(Channel *);
79 static void chan_send_close2(Channel *);
80 static void chan_send_eof2(Channel *);
83 static void chan_shutdown_write(Channel *);
84 static void chan_shutdown_read(Channel *);
87 * SSH1 specific implementation of event functions
91 chan_rcvd_oclose1(Channel *c)
93 debug("channel %d: rcvd oclose", c->self);
95 case CHAN_INPUT_WAIT_OCLOSE:
96 debug("channel %d: input wait_oclose -> closed", c->self);
97 c->istate = CHAN_INPUT_CLOSED;
100 debug("channel %d: input open -> closed", c->self);
101 chan_shutdown_read(c);
103 c->istate = CHAN_INPUT_CLOSED;
105 case CHAN_INPUT_WAIT_DRAIN:
106 /* both local read_failed and remote write_failed */
107 log("channel %d: input drain -> closed", c->self);
109 c->istate = CHAN_INPUT_CLOSED;
112 error("channel %d: protocol error: rcvd_oclose for istate %d",
118 chan_read_failed_12(Channel *c)
120 debug("channel %d: read failed", c->self);
122 case CHAN_INPUT_OPEN:
123 debug("channel %d: input open -> drain", c->self);
124 chan_shutdown_read(c);
125 c->istate = CHAN_INPUT_WAIT_DRAIN;
127 if (buffer_len(&c->input) == 0) {
128 debug("channel %d: input: no drain shortcut", c->self);
134 error("channel %d: chan_read_failed for istate %d",
140 chan_ibuf_empty1(Channel *c)
142 debug("channel %d: ibuf empty", c->self);
143 if (buffer_len(&c->input)) {
144 error("channel %d: chan_ibuf_empty for non empty buffer",
149 case CHAN_INPUT_WAIT_DRAIN:
150 debug("channel %d: input drain -> wait_oclose", c->self);
152 c->istate = CHAN_INPUT_WAIT_OCLOSE;
155 error("channel %d: chan_ibuf_empty for istate %d",
161 chan_rcvd_ieof1(Channel *c)
163 debug("channel %d: rcvd ieof", c->self);
164 if (c->type != SSH_CHANNEL_OPEN) {
165 debug("channel %d: non-open", c->self);
166 if (c->istate == CHAN_INPUT_OPEN) {
167 debug("channel %d: non-open: input open -> wait_oclose",
169 chan_shutdown_read(c);
171 c->istate = CHAN_INPUT_WAIT_OCLOSE;
173 error("channel %d: non-open: istate %d != open",
176 if (c->ostate == CHAN_OUTPUT_OPEN) {
177 debug("channel %d: non-open: output open -> closed",
179 chan_send_oclose1(c);
180 c->ostate = CHAN_OUTPUT_CLOSED;
182 error("channel %d: non-open: ostate %d != open",
188 case CHAN_OUTPUT_OPEN:
189 debug("channel %d: output open -> drain", c->self);
190 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
192 case CHAN_OUTPUT_WAIT_IEOF:
193 debug("channel %d: output wait_ieof -> closed", c->self);
194 c->ostate = CHAN_OUTPUT_CLOSED;
197 error("channel %d: protocol error: rcvd_ieof for ostate %d",
203 chan_write_failed1(Channel *c)
205 debug("channel %d: write failed", c->self);
207 case CHAN_OUTPUT_OPEN:
208 debug("channel %d: output open -> wait_ieof", c->self);
209 chan_send_oclose1(c);
210 c->ostate = CHAN_OUTPUT_WAIT_IEOF;
212 case CHAN_OUTPUT_WAIT_DRAIN:
213 debug("channel %d: output wait_drain -> closed", c->self);
214 chan_send_oclose1(c);
215 c->ostate = CHAN_OUTPUT_CLOSED;
218 error("channel %d: chan_write_failed for ostate %d",
224 chan_obuf_empty1(Channel *c)
226 debug("channel %d: obuf empty", c->self);
227 if (buffer_len(&c->output)) {
228 error("channel %d: chan_obuf_empty for non empty buffer",
233 case CHAN_OUTPUT_WAIT_DRAIN:
234 debug("channel %d: output drain -> closed", c->self);
235 chan_send_oclose1(c);
236 c->ostate = CHAN_OUTPUT_CLOSED;
239 error("channel %d: internal error: obuf_empty for ostate %d",
245 chan_send_ieof1(Channel *c)
247 debug("channel %d: send ieof", c->self);
249 case CHAN_INPUT_OPEN:
250 case CHAN_INPUT_WAIT_DRAIN:
251 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
252 packet_put_int(c->remote_id);
256 error("channel %d: cannot send ieof for istate %d",
262 chan_send_oclose1(Channel *c)
264 debug("channel %d: send oclose", c->self);
266 case CHAN_OUTPUT_OPEN:
267 case CHAN_OUTPUT_WAIT_DRAIN:
268 chan_shutdown_write(c);
269 buffer_consume(&c->output, buffer_len(&c->output));
270 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
271 packet_put_int(c->remote_id);
275 error("channel %d: cannot send oclose for ostate %d",
285 chan_rcvd_oclose2(Channel *c)
287 debug("channel %d: rcvd close", c->self);
288 if (c->flags & CHAN_CLOSE_RCVD)
289 error("channel %d: protocol error: close rcvd twice", c->self);
290 c->flags |= CHAN_CLOSE_RCVD;
291 if (c->type == SSH_CHANNEL_LARVAL) {
292 /* tear down larval channels immediately */
293 c->ostate = CHAN_OUTPUT_CLOSED;
294 c->istate = CHAN_INPUT_CLOSED;
298 case CHAN_OUTPUT_OPEN:
300 * wait until a data from the channel is consumed if a CLOSE
303 debug("channel %d: output open -> drain", c->self);
304 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
308 case CHAN_INPUT_OPEN:
309 debug("channel %d: input open -> closed", c->self);
310 chan_shutdown_read(c);
312 case CHAN_INPUT_WAIT_DRAIN:
313 debug("channel %d: input drain -> closed", c->self);
317 c->istate = CHAN_INPUT_CLOSED;
320 chan_ibuf_empty2(Channel *c)
322 debug("channel %d: ibuf empty", c->self);
323 if (buffer_len(&c->input)) {
324 error("channel %d: chan_ibuf_empty for non empty buffer",
329 case CHAN_INPUT_WAIT_DRAIN:
330 debug("channel %d: input drain -> closed", c->self);
331 if (!(c->flags & CHAN_CLOSE_SENT))
333 c->istate = CHAN_INPUT_CLOSED;
336 error("channel %d: chan_ibuf_empty for istate %d",
342 chan_rcvd_ieof2(Channel *c)
344 debug("channel %d: rcvd eof", c->self);
345 if (c->ostate == CHAN_OUTPUT_OPEN) {
346 debug("channel %d: output open -> drain", c->self);
347 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
351 chan_write_failed2(Channel *c)
353 debug("channel %d: write failed", c->self);
355 case CHAN_OUTPUT_OPEN:
356 debug("channel %d: output open -> closed", c->self);
357 chan_shutdown_write(c); /* ?? */
358 c->ostate = CHAN_OUTPUT_CLOSED;
360 case CHAN_OUTPUT_WAIT_DRAIN:
361 debug("channel %d: output drain -> closed", c->self);
362 chan_shutdown_write(c);
363 c->ostate = CHAN_OUTPUT_CLOSED;
366 error("channel %d: chan_write_failed for ostate %d",
372 chan_obuf_empty2(Channel *c)
374 debug("channel %d: obuf empty", c->self);
375 if (buffer_len(&c->output)) {
376 error("channel %d: chan_obuf_empty for non empty buffer",
381 case CHAN_OUTPUT_WAIT_DRAIN:
382 debug("channel %d: output drain -> closed", c->self);
383 chan_shutdown_write(c);
384 c->ostate = CHAN_OUTPUT_CLOSED;
387 error("channel %d: chan_obuf_empty for ostate %d",
393 chan_send_eof2(Channel *c)
395 debug("channel %d: send eof", c->self);
397 case CHAN_INPUT_WAIT_DRAIN:
398 packet_start(SSH2_MSG_CHANNEL_EOF);
399 packet_put_int(c->remote_id);
403 error("channel %d: cannot send eof for istate %d",
409 chan_send_close2(Channel *c)
411 debug("channel %d: send close", c->self);
412 if (c->ostate != CHAN_OUTPUT_CLOSED ||
413 c->istate != CHAN_INPUT_CLOSED) {
414 error("channel %d: cannot send close for istate/ostate %d/%d",
415 c->self, c->istate, c->ostate);
416 } else if (c->flags & CHAN_CLOSE_SENT) {
417 error("channel %d: already sent close", c->self);
419 packet_start(SSH2_MSG_CHANNEL_CLOSE);
420 packet_put_int(c->remote_id);
422 c->flags |= CHAN_CLOSE_SENT;
429 chan_mark_dead(Channel *c)
431 c->type = SSH_CHANNEL_ZOMBIE;
435 chan_is_dead(Channel *c, int send)
437 if (c->type == SSH_CHANNEL_ZOMBIE) {
438 debug("channel %d: zombie", c->self);
441 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
444 debug("channel %d: is dead", c->self);
448 * we have to delay the close message if the efd (for stderr) is
451 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
452 buffer_len(&c->extended) > 0)
454 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
458 debug2("channel %d: active efd: %d len %d type %s",
459 c->self, c->efd, buffer_len(&c->extended),
460 c->extended_usage==CHAN_EXTENDED_READ ?
463 if (!(c->flags & CHAN_CLOSE_SENT)) {
467 /* channel would be dead if we sent a close */
468 if (c->flags & CHAN_CLOSE_RCVD) {
469 debug("channel %d: almost dead",
475 if ((c->flags & CHAN_CLOSE_SENT) &&
476 (c->flags & CHAN_CLOSE_RCVD)) {
477 debug("channel %d: is dead", c->self);
485 chan_init_iostates(Channel *c)
487 c->ostate = CHAN_OUTPUT_OPEN;
488 c->istate = CHAN_INPUT_OPEN;
497 chan_rcvd_oclose = chan_rcvd_oclose2;
498 chan_read_failed = chan_read_failed_12;
499 chan_ibuf_empty = chan_ibuf_empty2;
501 chan_rcvd_ieof = chan_rcvd_ieof2;
502 chan_write_failed = chan_write_failed2;
503 chan_obuf_empty = chan_obuf_empty2;
505 chan_rcvd_oclose = chan_rcvd_oclose1;
506 chan_read_failed = chan_read_failed_12;
507 chan_ibuf_empty = chan_ibuf_empty1;
509 chan_rcvd_ieof = chan_rcvd_ieof1;
510 chan_write_failed = chan_write_failed1;
511 chan_obuf_empty = chan_obuf_empty1;
517 chan_shutdown_write(Channel *c)
519 buffer_consume(&c->output, buffer_len(&c->output));
520 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
522 /* shutdown failure is allowed if write failed already */
523 debug("channel %d: close_write", c->self);
525 if (shutdown(c->sock, SHUT_WR) < 0)
526 debug("channel %d: chan_shutdown_write: "
527 "shutdown() failed for fd%d: %.100s",
528 c->self, c->sock, strerror(errno));
530 if (channel_close_fd(&c->wfd) < 0)
531 log("channel %d: chan_shutdown_write: "
532 "close() failed for fd%d: %.100s",
533 c->self, c->wfd, strerror(errno));
537 chan_shutdown_read(Channel *c)
539 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
541 debug("channel %d: close_read", c->self);
544 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
545 * write side has been closed already. (bug on Linux)
546 * HP-UX may return ENOTCONN also.
548 if (shutdown(c->sock, SHUT_RD) < 0
549 && errno != ENOTCONN)
550 error("channel %d: chan_shutdown_read: "
551 "shutdown() failed for fd%d [i%d o%d]: %.100s",
552 c->self, c->sock, c->istate, c->ostate,
555 if (channel_close_fd(&c->rfd) < 0)
556 log("channel %d: chan_shutdown_read: "
557 "close() failed for fd%d: %.100s",
558 c->self, c->rfd, strerror(errno));