2 /* $OpenBSD: nchan.c,v 1.62 2008/11/07 18:50:18 stevesk Exp $ */
4 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 __RCSID("$NetBSD: nchan.c,v 1.6 2009/02/16 20:53:54 christos Exp $");
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/queue.h>
46 * SSH Protocol 1.5 aka New Channel Protocol
47 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
48 * Written by Markus Friedl in October 1999
50 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
51 * tear down of channels:
53 * 1.3: strict request-ack-protocol:
57 * 1.5: uses variations of:
62 * i.e. both sides have to close the channel
64 * 2.0: the EOF messages are optional
66 * See the debugging output from 'ssh -v' and 'sshd -d' of
67 * ssh-1.2.27 as an example.
71 /* functions manipulating channel states */
73 * EVENTS update channel input/output states execute ACTIONS
76 * ACTIONS: should never update the channel states
78 static void chan_send_ieof1(Channel
*);
79 static void chan_send_oclose1(Channel
*);
80 static void chan_send_close2(Channel
*);
81 static void chan_send_eof2(Channel
*);
82 static void chan_send_eow2(Channel
*);
85 static void chan_shutdown_write(Channel
*);
86 static void chan_shutdown_read(Channel
*);
88 static char *ostates
[] = { "open", "drain", "wait_ieof", "closed" };
89 static char *istates
[] = { "open", "drain", "wait_oclose", "closed" };
92 chan_set_istate(Channel
*c
, u_int next
)
94 if (c
->istate
> CHAN_INPUT_CLOSED
|| next
> CHAN_INPUT_CLOSED
)
95 fatal("chan_set_istate: bad state %d -> %d", c
->istate
, next
);
96 debug2("channel %d: input %s -> %s", c
->self
, istates
[c
->istate
],
101 chan_set_ostate(Channel
*c
, u_int next
)
103 if (c
->ostate
> CHAN_OUTPUT_CLOSED
|| next
> CHAN_OUTPUT_CLOSED
)
104 fatal("chan_set_ostate: bad state %d -> %d", c
->ostate
, next
);
105 debug2("channel %d: output %s -> %s", c
->self
, ostates
[c
->ostate
],
111 * SSH1 specific implementation of event functions
115 chan_rcvd_oclose1(Channel
*c
)
117 debug2("channel %d: rcvd oclose", c
->self
);
119 case CHAN_INPUT_WAIT_OCLOSE
:
120 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
122 case CHAN_INPUT_OPEN
:
123 chan_shutdown_read(c
);
125 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
127 case CHAN_INPUT_WAIT_DRAIN
:
128 /* both local read_failed and remote write_failed */
130 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
133 error("channel %d: protocol error: rcvd_oclose for istate %d",
139 chan_read_failed(Channel
*c
)
141 debug2("channel %d: read failed", c
->self
);
143 case CHAN_INPUT_OPEN
:
144 chan_shutdown_read(c
);
145 chan_set_istate(c
, CHAN_INPUT_WAIT_DRAIN
);
148 error("channel %d: chan_read_failed for istate %d",
154 chan_ibuf_empty(Channel
*c
)
156 debug2("channel %d: ibuf empty", c
->self
);
157 if (buffer_len(&c
->input
)) {
158 error("channel %d: chan_ibuf_empty for non empty buffer",
163 case CHAN_INPUT_WAIT_DRAIN
:
165 if (!(c
->flags
& CHAN_CLOSE_SENT
))
167 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
170 chan_set_istate(c
, CHAN_INPUT_WAIT_OCLOSE
);
174 error("channel %d: chan_ibuf_empty for istate %d",
180 chan_rcvd_ieof1(Channel
*c
)
182 debug2("channel %d: rcvd ieof", c
->self
);
184 case CHAN_OUTPUT_OPEN
:
185 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
187 case CHAN_OUTPUT_WAIT_IEOF
:
188 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
191 error("channel %d: protocol error: rcvd_ieof for ostate %d",
197 chan_write_failed1(Channel
*c
)
199 debug2("channel %d: write failed", c
->self
);
201 case CHAN_OUTPUT_OPEN
:
202 chan_shutdown_write(c
);
203 chan_send_oclose1(c
);
204 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_IEOF
);
206 case CHAN_OUTPUT_WAIT_DRAIN
:
207 chan_shutdown_write(c
);
208 chan_send_oclose1(c
);
209 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
212 error("channel %d: chan_write_failed for ostate %d",
218 chan_obuf_empty(Channel
*c
)
220 debug2("channel %d: obuf empty", c
->self
);
221 if (buffer_len(&c
->output
)) {
222 error("channel %d: chan_obuf_empty for non empty buffer",
227 case CHAN_OUTPUT_WAIT_DRAIN
:
228 chan_shutdown_write(c
);
230 chan_send_oclose1(c
);
231 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
234 error("channel %d: internal error: obuf_empty for ostate %d",
240 chan_send_ieof1(Channel
*c
)
242 debug2("channel %d: send ieof", c
->self
);
244 case CHAN_INPUT_OPEN
:
245 case CHAN_INPUT_WAIT_DRAIN
:
246 packet_start(SSH_MSG_CHANNEL_INPUT_EOF
);
247 packet_put_int(c
->remote_id
);
251 error("channel %d: cannot send ieof for istate %d",
257 chan_send_oclose1(Channel
*c
)
259 debug2("channel %d: send oclose", c
->self
);
261 case CHAN_OUTPUT_OPEN
:
262 case CHAN_OUTPUT_WAIT_DRAIN
:
263 buffer_clear(&c
->output
);
264 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE
);
265 packet_put_int(c
->remote_id
);
269 error("channel %d: cannot send oclose for ostate %d",
279 chan_rcvd_close2(Channel
*c
)
281 debug2("channel %d: rcvd close", c
->self
);
282 if (c
->flags
& CHAN_CLOSE_RCVD
)
283 error("channel %d: protocol error: close rcvd twice", c
->self
);
284 c
->flags
|= CHAN_CLOSE_RCVD
;
285 if (c
->type
== SSH_CHANNEL_LARVAL
) {
286 /* tear down larval channels immediately */
287 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
288 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
292 case CHAN_OUTPUT_OPEN
:
294 * wait until a data from the channel is consumed if a CLOSE
297 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
301 case CHAN_INPUT_OPEN
:
302 chan_shutdown_read(c
);
303 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
305 case CHAN_INPUT_WAIT_DRAIN
:
307 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
312 chan_rcvd_eow(Channel
*c
)
314 debug2("channel %d: rcvd eow", c
->self
);
316 case CHAN_INPUT_OPEN
:
317 chan_shutdown_read(c
);
318 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
323 chan_rcvd_eof2(Channel
*c
)
325 debug2("channel %d: rcvd eof", c
->self
);
326 c
->flags
|= CHAN_EOF_RCVD
;
327 if (c
->ostate
== CHAN_OUTPUT_OPEN
)
328 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
331 chan_write_failed2(Channel
*c
)
333 debug2("channel %d: write failed", c
->self
);
335 case CHAN_OUTPUT_OPEN
:
336 case CHAN_OUTPUT_WAIT_DRAIN
:
337 chan_shutdown_write(c
);
338 if (strcmp(c
->ctype
, "session") == 0)
340 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
343 error("channel %d: chan_write_failed for ostate %d",
349 chan_send_eof2(Channel
*c
)
351 debug2("channel %d: send eof", c
->self
);
353 case CHAN_INPUT_WAIT_DRAIN
:
354 packet_start(SSH2_MSG_CHANNEL_EOF
);
355 packet_put_int(c
->remote_id
);
357 c
->flags
|= CHAN_EOF_SENT
;
360 error("channel %d: cannot send eof for istate %d",
366 chan_send_close2(Channel
*c
)
368 debug2("channel %d: send close", c
->self
);
369 if (c
->ostate
!= CHAN_OUTPUT_CLOSED
||
370 c
->istate
!= CHAN_INPUT_CLOSED
) {
371 error("channel %d: cannot send close for istate/ostate %d/%d",
372 c
->self
, c
->istate
, c
->ostate
);
373 } else if (c
->flags
& CHAN_CLOSE_SENT
) {
374 error("channel %d: already sent close", c
->self
);
376 packet_start(SSH2_MSG_CHANNEL_CLOSE
);
377 packet_put_int(c
->remote_id
);
379 c
->flags
|= CHAN_CLOSE_SENT
;
383 chan_send_eow2(Channel
*c
)
385 debug2("channel %d: send eow", c
->self
);
386 if (c
->ostate
== CHAN_OUTPUT_CLOSED
) {
387 error("channel %d: must not sent eow on closed output",
391 if (!(datafellows
& SSH_NEW_OPENSSH
))
393 packet_start(SSH2_MSG_CHANNEL_REQUEST
);
394 packet_put_int(c
->remote_id
);
395 packet_put_cstring("eow@openssh.com");
403 chan_rcvd_ieof(Channel
*c
)
409 if (c
->ostate
== CHAN_OUTPUT_WAIT_DRAIN
&&
410 buffer_len(&c
->output
) == 0 &&
411 !CHANNEL_EFD_OUTPUT_ACTIVE(c
))
415 chan_rcvd_oclose(Channel
*c
)
420 chan_rcvd_oclose1(c
);
423 chan_write_failed(Channel
*c
)
426 chan_write_failed2(c
);
428 chan_write_failed1(c
);
432 chan_mark_dead(Channel
*c
)
434 c
->type
= SSH_CHANNEL_ZOMBIE
;
438 chan_is_dead(Channel
*c
, int do_send
)
440 if (c
->type
== SSH_CHANNEL_ZOMBIE
) {
441 debug2("channel %d: zombie", c
->self
);
444 if (c
->istate
!= CHAN_INPUT_CLOSED
|| c
->ostate
!= CHAN_OUTPUT_CLOSED
)
447 debug2("channel %d: is dead", c
->self
);
450 if ((datafellows
& SSH_BUG_EXTEOF
) &&
451 c
->extended_usage
== CHAN_EXTENDED_WRITE
&&
453 buffer_len(&c
->extended
) > 0) {
454 debug2("channel %d: active efd: %d len %d",
455 c
->self
, c
->efd
, buffer_len(&c
->extended
));
458 if (!(c
->flags
& CHAN_CLOSE_SENT
)) {
462 /* channel would be dead if we sent a close */
463 if (c
->flags
& CHAN_CLOSE_RCVD
) {
464 debug2("channel %d: almost dead",
470 if ((c
->flags
& CHAN_CLOSE_SENT
) &&
471 (c
->flags
& CHAN_CLOSE_RCVD
)) {
472 debug2("channel %d: is dead", c
->self
);
480 chan_shutdown_write(Channel
*c
)
482 buffer_clear(&c
->output
);
483 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
485 /* shutdown failure is allowed if write failed already */
486 debug2("channel %d: close_write", c
->self
);
488 if (shutdown(c
->sock
, SHUT_WR
) < 0)
489 debug2("channel %d: chan_shutdown_write: "
490 "shutdown() failed for fd %d: %.100s",
491 c
->self
, c
->sock
, strerror(errno
));
493 if (channel_close_fd(&c
->wfd
) < 0)
494 logit("channel %d: chan_shutdown_write: "
495 "close() failed for fd %d: %.100s",
496 c
->self
, c
->wfd
, strerror(errno
));
500 chan_shutdown_read(Channel
*c
)
502 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
504 debug2("channel %d: close_read", c
->self
);
506 if (shutdown(c
->sock
, SHUT_RD
) < 0)
507 error("channel %d: chan_shutdown_read: "
508 "shutdown() failed for fd %d [i%d o%d]: %.100s",
509 c
->self
, c
->sock
, c
->istate
, c
->ostate
,
512 if (channel_close_fd(&c
->rfd
) < 0)
513 logit("channel %d: chan_shutdown_read: "
514 "close() failed for fd %d: %.100s",
515 c
->self
, c
->rfd
, strerror(errno
));