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.39 2002/01/14 13:34:07 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 *);
86 static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
87 static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
90 chan_set_istate(Channel *c, u_int next)
92 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
93 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
94 debug("channel %d: input %s -> %s", c->self, istates[c->istate],
99 chan_set_ostate(Channel *c, u_int next)
101 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
102 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
103 debug("channel %d: output %s -> %s", c->self, ostates[c->ostate],
109 * SSH1 specific implementation of event functions
113 chan_rcvd_oclose1(Channel *c)
115 debug("channel %d: rcvd oclose", c->self);
117 case CHAN_INPUT_WAIT_OCLOSE:
118 chan_set_istate(c, CHAN_INPUT_CLOSED);
120 case CHAN_INPUT_OPEN:
121 chan_shutdown_read(c);
123 chan_set_istate(c, CHAN_INPUT_CLOSED);
125 case CHAN_INPUT_WAIT_DRAIN:
126 /* both local read_failed and remote write_failed */
128 chan_set_istate(c, CHAN_INPUT_CLOSED);
131 error("channel %d: protocol error: rcvd_oclose for istate %d",
137 chan_read_failed_12(Channel *c)
139 debug("channel %d: read failed", c->self);
141 case CHAN_INPUT_OPEN:
142 chan_shutdown_read(c);
143 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
146 error("channel %d: chan_read_failed for istate %d",
152 chan_ibuf_empty1(Channel *c)
154 debug("channel %d: ibuf empty", c->self);
155 if (buffer_len(&c->input)) {
156 error("channel %d: chan_ibuf_empty for non empty buffer",
161 case CHAN_INPUT_WAIT_DRAIN:
163 if (!(c->flags & CHAN_CLOSE_SENT))
165 chan_set_istate(c, CHAN_INPUT_CLOSED);
168 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
172 error("channel %d: chan_ibuf_empty for istate %d",
178 chan_rcvd_ieof1(Channel *c)
180 debug("channel %d: rcvd ieof", c->self);
182 case CHAN_OUTPUT_OPEN:
183 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
185 case CHAN_OUTPUT_WAIT_IEOF:
186 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
189 error("channel %d: protocol error: rcvd_ieof for ostate %d",
195 chan_write_failed1(Channel *c)
197 debug("channel %d: write failed", c->self);
199 case CHAN_OUTPUT_OPEN:
200 chan_shutdown_write(c);
201 chan_send_oclose1(c);
202 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
204 case CHAN_OUTPUT_WAIT_DRAIN:
205 chan_shutdown_write(c);
206 chan_send_oclose1(c);
207 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
210 error("channel %d: chan_write_failed for ostate %d",
216 chan_obuf_empty1(Channel *c)
218 debug("channel %d: obuf empty", c->self);
219 if (buffer_len(&c->output)) {
220 error("channel %d: chan_obuf_empty for non empty buffer",
225 case CHAN_OUTPUT_WAIT_DRAIN:
226 chan_shutdown_write(c);
228 chan_send_oclose1(c);
229 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
232 error("channel %d: internal error: obuf_empty for ostate %d",
238 chan_send_ieof1(Channel *c)
240 debug("channel %d: send ieof", c->self);
242 case CHAN_INPUT_OPEN:
243 case CHAN_INPUT_WAIT_DRAIN:
244 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
245 packet_put_int(c->remote_id);
249 error("channel %d: cannot send ieof for istate %d",
255 chan_send_oclose1(Channel *c)
257 debug("channel %d: send oclose", c->self);
259 case CHAN_OUTPUT_OPEN:
260 case CHAN_OUTPUT_WAIT_DRAIN:
261 buffer_clear(&c->output);
262 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
263 packet_put_int(c->remote_id);
267 error("channel %d: cannot send oclose for ostate %d",
277 chan_rcvd_oclose2(Channel *c)
279 debug("channel %d: rcvd close", c->self);
280 if (c->flags & CHAN_CLOSE_RCVD)
281 error("channel %d: protocol error: close rcvd twice", c->self);
282 c->flags |= CHAN_CLOSE_RCVD;
283 if (c->type == SSH_CHANNEL_LARVAL) {
284 /* tear down larval channels immediately */
285 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
286 chan_set_istate(c, CHAN_INPUT_CLOSED);
290 case CHAN_OUTPUT_OPEN:
292 * wait until a data from the channel is consumed if a CLOSE
295 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
299 case CHAN_INPUT_OPEN:
300 chan_shutdown_read(c);
302 case CHAN_INPUT_WAIT_DRAIN:
306 chan_set_istate(c, CHAN_INPUT_CLOSED);
309 chan_ibuf_empty2(Channel *c)
314 chan_rcvd_ieof2(Channel *c)
316 debug("channel %d: rcvd eof", c->self);
317 if (c->ostate == CHAN_OUTPUT_OPEN)
318 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
321 chan_write_failed2(Channel *c)
323 debug("channel %d: write failed", c->self);
325 case CHAN_OUTPUT_OPEN:
326 chan_shutdown_write(c); /* ?? */
327 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
329 case CHAN_OUTPUT_WAIT_DRAIN:
330 chan_shutdown_write(c);
331 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
334 error("channel %d: chan_write_failed for ostate %d",
340 chan_obuf_empty2(Channel *c)
345 chan_send_eof2(Channel *c)
347 debug("channel %d: send eof", c->self);
349 case CHAN_INPUT_WAIT_DRAIN:
350 packet_start(SSH2_MSG_CHANNEL_EOF);
351 packet_put_int(c->remote_id);
355 error("channel %d: cannot send eof for istate %d",
361 chan_send_close2(Channel *c)
363 debug("channel %d: send close", c->self);
364 if (c->ostate != CHAN_OUTPUT_CLOSED ||
365 c->istate != CHAN_INPUT_CLOSED) {
366 error("channel %d: cannot send close for istate/ostate %d/%d",
367 c->self, c->istate, c->ostate);
368 } else if (c->flags & CHAN_CLOSE_SENT) {
369 error("channel %d: already sent close", c->self);
371 packet_start(SSH2_MSG_CHANNEL_CLOSE);
372 packet_put_int(c->remote_id);
374 c->flags |= CHAN_CLOSE_SENT;
381 chan_mark_dead(Channel *c)
383 c->type = SSH_CHANNEL_ZOMBIE;
387 chan_is_dead(Channel *c, int send)
389 if (c->type == SSH_CHANNEL_ZOMBIE) {
390 debug("channel %d: zombie", c->self);
393 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
396 debug("channel %d: is dead", c->self);
400 * we have to delay the close message if the efd (for stderr) is
403 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
404 buffer_len(&c->extended) > 0)
406 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
410 debug2("channel %d: active efd: %d len %d type %s",
411 c->self, c->efd, buffer_len(&c->extended),
412 c->extended_usage==CHAN_EXTENDED_READ ?
415 if (!(c->flags & CHAN_CLOSE_SENT)) {
419 /* channel would be dead if we sent a close */
420 if (c->flags & CHAN_CLOSE_RCVD) {
421 debug("channel %d: almost dead",
427 if ((c->flags & CHAN_CLOSE_SENT) &&
428 (c->flags & CHAN_CLOSE_RCVD)) {
429 debug("channel %d: is dead", c->self);
437 chan_init_iostates(Channel *c)
439 c->ostate = CHAN_OUTPUT_OPEN;
440 c->istate = CHAN_INPUT_OPEN;
449 chan_rcvd_oclose = chan_rcvd_oclose2;
450 chan_read_failed = chan_read_failed_12;
451 chan_ibuf_empty = chan_ibuf_empty2;
453 chan_rcvd_ieof = chan_rcvd_ieof2;
454 chan_write_failed = chan_write_failed2;
455 chan_obuf_empty = chan_obuf_empty2;
457 chan_rcvd_oclose = chan_rcvd_oclose1;
458 chan_read_failed = chan_read_failed_12;
459 chan_ibuf_empty = chan_ibuf_empty1;
461 chan_rcvd_ieof = chan_rcvd_ieof1;
462 chan_write_failed = chan_write_failed1;
463 chan_obuf_empty = chan_obuf_empty1;
469 chan_shutdown_write(Channel *c)
471 buffer_clear(&c->output);
472 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
474 /* shutdown failure is allowed if write failed already */
475 debug("channel %d: close_write", c->self);
477 if (shutdown(c->sock, SHUT_WR) < 0)
478 debug("channel %d: chan_shutdown_write: "
479 "shutdown() failed for fd%d: %.100s",
480 c->self, c->sock, strerror(errno));
482 if (channel_close_fd(&c->wfd) < 0)
483 log("channel %d: chan_shutdown_write: "
484 "close() failed for fd%d: %.100s",
485 c->self, c->wfd, strerror(errno));
489 chan_shutdown_read(Channel *c)
491 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
493 debug("channel %d: close_read", c->self);
496 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
497 * write side has been closed already. (bug on Linux)
498 * HP-UX may return ENOTCONN also.
500 if (shutdown(c->sock, SHUT_RD) < 0
501 && errno != ENOTCONN)
502 error("channel %d: chan_shutdown_read: "
503 "shutdown() failed for fd%d [i%d o%d]: %.100s",
504 c->self, c->sock, c->istate, c->ostate,
507 if (channel_close_fd(&c->rfd) < 0)
508 log("channel %d: chan_shutdown_read: "
509 "close() failed for fd%d: %.100s",
510 c->self, c->rfd, strerror(errno));