1 /* $OpenBSD: nchan.c,v 1.63 2010/01/26 01:28:35 djm Exp $ */
3 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
35 #include "openbsd-compat/sys-queue.h"
45 * SSH Protocol 1.5 aka New Channel Protocol
46 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
47 * Written by Markus Friedl in October 1999
49 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
50 * tear down of channels:
52 * 1.3: strict request-ack-protocol:
56 * 1.5: uses variations of:
61 * i.e. both sides have to close the channel
63 * 2.0: the EOF messages are optional
65 * See the debugging output from 'ssh -v' and 'sshd -d' of
66 * ssh-1.2.27 as an example.
70 /* functions manipulating channel states */
72 * EVENTS update channel input/output states execute ACTIONS
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
*);
81 static void chan_send_eow2(Channel
*);
84 static void chan_shutdown_write(Channel
*);
85 static void chan_shutdown_read(Channel
*);
87 static char *ostates
[] = { "open", "drain", "wait_ieof", "closed" };
88 static char *istates
[] = { "open", "drain", "wait_oclose", "closed" };
91 chan_set_istate(Channel
*c
, u_int next
)
93 if (c
->istate
> CHAN_INPUT_CLOSED
|| next
> CHAN_INPUT_CLOSED
)
94 fatal("chan_set_istate: bad state %d -> %d", c
->istate
, next
);
95 debug2("channel %d: input %s -> %s", c
->self
, istates
[c
->istate
],
100 chan_set_ostate(Channel
*c
, u_int next
)
102 if (c
->ostate
> CHAN_OUTPUT_CLOSED
|| next
> CHAN_OUTPUT_CLOSED
)
103 fatal("chan_set_ostate: bad state %d -> %d", c
->ostate
, next
);
104 debug2("channel %d: output %s -> %s", c
->self
, ostates
[c
->ostate
],
110 * SSH1 specific implementation of event functions
114 chan_rcvd_oclose1(Channel
*c
)
116 debug2("channel %d: rcvd oclose", c
->self
);
118 case CHAN_INPUT_WAIT_OCLOSE
:
119 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
121 case CHAN_INPUT_OPEN
:
122 chan_shutdown_read(c
);
124 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
126 case CHAN_INPUT_WAIT_DRAIN
:
127 /* both local read_failed and remote write_failed */
129 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
132 error("channel %d: protocol error: rcvd_oclose for istate %d",
138 chan_read_failed(Channel
*c
)
140 debug2("channel %d: read failed", c
->self
);
142 case CHAN_INPUT_OPEN
:
143 chan_shutdown_read(c
);
144 chan_set_istate(c
, CHAN_INPUT_WAIT_DRAIN
);
147 error("channel %d: chan_read_failed for istate %d",
153 chan_ibuf_empty(Channel
*c
)
155 debug2("channel %d: ibuf empty", c
->self
);
156 if (buffer_len(&c
->input
)) {
157 error("channel %d: chan_ibuf_empty for non empty buffer",
162 case CHAN_INPUT_WAIT_DRAIN
:
164 if (!(c
->flags
& (CHAN_CLOSE_SENT
|CHAN_LOCAL
)))
166 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
169 chan_set_istate(c
, CHAN_INPUT_WAIT_OCLOSE
);
173 error("channel %d: chan_ibuf_empty for istate %d",
179 chan_rcvd_ieof1(Channel
*c
)
181 debug2("channel %d: rcvd ieof", c
->self
);
183 case CHAN_OUTPUT_OPEN
:
184 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
186 case CHAN_OUTPUT_WAIT_IEOF
:
187 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
190 error("channel %d: protocol error: rcvd_ieof for ostate %d",
196 chan_write_failed1(Channel
*c
)
198 debug2("channel %d: write failed", c
->self
);
200 case CHAN_OUTPUT_OPEN
:
201 chan_shutdown_write(c
);
202 chan_send_oclose1(c
);
203 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_IEOF
);
205 case CHAN_OUTPUT_WAIT_DRAIN
:
206 chan_shutdown_write(c
);
207 chan_send_oclose1(c
);
208 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
211 error("channel %d: chan_write_failed for ostate %d",
217 chan_obuf_empty(Channel
*c
)
219 debug2("channel %d: obuf empty", c
->self
);
220 if (buffer_len(&c
->output
)) {
221 error("channel %d: chan_obuf_empty for non empty buffer",
226 case CHAN_OUTPUT_WAIT_DRAIN
:
227 chan_shutdown_write(c
);
229 chan_send_oclose1(c
);
230 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
233 error("channel %d: internal error: obuf_empty for ostate %d",
239 chan_send_ieof1(Channel
*c
)
241 debug2("channel %d: send ieof", c
->self
);
243 case CHAN_INPUT_OPEN
:
244 case CHAN_INPUT_WAIT_DRAIN
:
245 packet_start(SSH_MSG_CHANNEL_INPUT_EOF
);
246 packet_put_int(c
->remote_id
);
250 error("channel %d: cannot send ieof for istate %d",
256 chan_send_oclose1(Channel
*c
)
258 debug2("channel %d: send oclose", c
->self
);
260 case CHAN_OUTPUT_OPEN
:
261 case CHAN_OUTPUT_WAIT_DRAIN
:
262 buffer_clear(&c
->output
);
263 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE
);
264 packet_put_int(c
->remote_id
);
268 error("channel %d: cannot send oclose for ostate %d",
278 chan_rcvd_close2(Channel
*c
)
280 debug2("channel %d: rcvd close", c
->self
);
281 if (!(c
->flags
& CHAN_LOCAL
)) {
282 if (c
->flags
& CHAN_CLOSE_RCVD
)
283 error("channel %d: protocol error: close rcvd twice",
285 c
->flags
|= CHAN_CLOSE_RCVD
;
287 if (c
->type
== SSH_CHANNEL_LARVAL
) {
288 /* tear down larval channels immediately */
289 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
290 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
294 case CHAN_OUTPUT_OPEN
:
296 * wait until a data from the channel is consumed if a CLOSE
299 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
303 case CHAN_INPUT_OPEN
:
304 chan_shutdown_read(c
);
305 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
307 case CHAN_INPUT_WAIT_DRAIN
:
308 if (!(c
->flags
& CHAN_LOCAL
))
310 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
316 chan_rcvd_eow(Channel
*c
)
318 debug2("channel %d: rcvd eow", c
->self
);
320 case CHAN_INPUT_OPEN
:
321 chan_shutdown_read(c
);
322 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
327 chan_rcvd_eof2(Channel
*c
)
329 debug2("channel %d: rcvd eof", c
->self
);
330 c
->flags
|= CHAN_EOF_RCVD
;
331 if (c
->ostate
== CHAN_OUTPUT_OPEN
)
332 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
335 chan_write_failed2(Channel
*c
)
337 debug2("channel %d: write failed", c
->self
);
339 case CHAN_OUTPUT_OPEN
:
340 case CHAN_OUTPUT_WAIT_DRAIN
:
341 chan_shutdown_write(c
);
342 if (strcmp(c
->ctype
, "session") == 0)
344 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
347 error("channel %d: chan_write_failed for ostate %d",
353 chan_send_eof2(Channel
*c
)
355 debug2("channel %d: send eof", c
->self
);
357 case CHAN_INPUT_WAIT_DRAIN
:
358 packet_start(SSH2_MSG_CHANNEL_EOF
);
359 packet_put_int(c
->remote_id
);
361 c
->flags
|= CHAN_EOF_SENT
;
364 error("channel %d: cannot send eof for istate %d",
370 chan_send_close2(Channel
*c
)
372 debug2("channel %d: send close", c
->self
);
373 if (c
->ostate
!= CHAN_OUTPUT_CLOSED
||
374 c
->istate
!= CHAN_INPUT_CLOSED
) {
375 error("channel %d: cannot send close for istate/ostate %d/%d",
376 c
->self
, c
->istate
, c
->ostate
);
377 } else if (c
->flags
& CHAN_CLOSE_SENT
) {
378 error("channel %d: already sent close", c
->self
);
380 packet_start(SSH2_MSG_CHANNEL_CLOSE
);
381 packet_put_int(c
->remote_id
);
383 c
->flags
|= CHAN_CLOSE_SENT
;
387 chan_send_eow2(Channel
*c
)
389 debug2("channel %d: send eow", c
->self
);
390 if (c
->ostate
== CHAN_OUTPUT_CLOSED
) {
391 error("channel %d: must not sent eow on closed output",
395 if (!(datafellows
& SSH_NEW_OPENSSH
))
397 packet_start(SSH2_MSG_CHANNEL_REQUEST
);
398 packet_put_int(c
->remote_id
);
399 packet_put_cstring("eow@openssh.com");
407 chan_rcvd_ieof(Channel
*c
)
413 if (c
->ostate
== CHAN_OUTPUT_WAIT_DRAIN
&&
414 buffer_len(&c
->output
) == 0 &&
415 !CHANNEL_EFD_OUTPUT_ACTIVE(c
))
419 chan_rcvd_oclose(Channel
*c
)
424 chan_rcvd_oclose1(c
);
427 chan_write_failed(Channel
*c
)
430 chan_write_failed2(c
);
432 chan_write_failed1(c
);
436 chan_mark_dead(Channel
*c
)
438 c
->type
= SSH_CHANNEL_ZOMBIE
;
442 chan_is_dead(Channel
*c
, int do_send
)
444 if (c
->type
== SSH_CHANNEL_ZOMBIE
) {
445 debug2("channel %d: zombie", c
->self
);
448 if (c
->istate
!= CHAN_INPUT_CLOSED
|| c
->ostate
!= CHAN_OUTPUT_CLOSED
)
451 debug2("channel %d: is dead", c
->self
);
454 if ((datafellows
& SSH_BUG_EXTEOF
) &&
455 c
->extended_usage
== CHAN_EXTENDED_WRITE
&&
457 buffer_len(&c
->extended
) > 0) {
458 debug2("channel %d: active efd: %d len %d",
459 c
->self
, c
->efd
, buffer_len(&c
->extended
));
462 if (c
->flags
& CHAN_LOCAL
) {
463 debug2("channel %d: is dead (local)", c
->self
);
466 if (!(c
->flags
& CHAN_CLOSE_SENT
)) {
470 /* channel would be dead if we sent a close */
471 if (c
->flags
& CHAN_CLOSE_RCVD
) {
472 debug2("channel %d: almost dead",
478 if ((c
->flags
& CHAN_CLOSE_SENT
) &&
479 (c
->flags
& CHAN_CLOSE_RCVD
)) {
480 debug2("channel %d: is dead", c
->self
);
488 chan_shutdown_write(Channel
*c
)
490 buffer_clear(&c
->output
);
491 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
493 /* shutdown failure is allowed if write failed already */
494 debug2("channel %d: close_write", c
->self
);
496 if (shutdown(c
->sock
, SHUT_WR
) < 0)
497 debug2("channel %d: chan_shutdown_write: "
498 "shutdown() failed for fd %d: %.100s",
499 c
->self
, c
->sock
, strerror(errno
));
501 if (channel_close_fd(&c
->wfd
) < 0)
502 logit("channel %d: chan_shutdown_write: "
503 "close() failed for fd %d: %.100s",
504 c
->self
, c
->wfd
, strerror(errno
));
508 chan_shutdown_read(Channel
*c
)
510 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
512 debug2("channel %d: close_read", c
->self
);
515 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
516 * write side has been closed already. (bug on Linux)
517 * HP-UX may return ENOTCONN also.
519 if (shutdown(c
->sock
, SHUT_RD
) < 0
520 && errno
!= ENOTCONN
)
521 error("channel %d: chan_shutdown_read: "
522 "shutdown() failed for fd %d [i%d o%d]: %.100s",
523 c
->self
, c
->sock
, c
->istate
, c
->ostate
,
526 if (channel_close_fd(&c
->rfd
) < 0)
527 logit("channel %d: chan_shutdown_read: "
528 "close() failed for fd %d: %.100s",
529 c
->self
, c
->rfd
, strerror(errno
));