1 /* $OpenBSD: nchan.c,v 1.55 2006/07/11 20:07:25 stevesk 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>
42 * SSH Protocol 1.5 aka New Channel Protocol
43 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
44 * Written by Markus Friedl in October 1999
46 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
47 * tear down of channels:
49 * 1.3: strict request-ack-protocol:
53 * 1.5: uses variations of:
58 * i.e. both sides have to close the channel
60 * 2.0: the EOF messages are optional
62 * See the debugging output from 'ssh -v' and 'sshd -d' of
63 * ssh-1.2.27 as an example.
67 /* functions manipulating channel states */
69 * EVENTS update channel input/output states execute ACTIONS
72 * ACTIONS: should never update the channel states
74 static void chan_send_ieof1(Channel
*);
75 static void chan_send_oclose1(Channel
*);
76 static void chan_send_close2(Channel
*);
77 static void chan_send_eof2(Channel
*);
80 static void chan_shutdown_write(Channel
*);
81 static void chan_shutdown_read(Channel
*);
83 static char *ostates
[] = { "open", "drain", "wait_ieof", "closed" };
84 static char *istates
[] = { "open", "drain", "wait_oclose", "closed" };
87 chan_set_istate(Channel
*c
, u_int next
)
89 if (c
->istate
> CHAN_INPUT_CLOSED
|| next
> CHAN_INPUT_CLOSED
)
90 fatal("chan_set_istate: bad state %d -> %d", c
->istate
, next
);
91 debug2("channel %d: input %s -> %s", c
->self
, istates
[c
->istate
],
96 chan_set_ostate(Channel
*c
, u_int next
)
98 if (c
->ostate
> CHAN_OUTPUT_CLOSED
|| next
> CHAN_OUTPUT_CLOSED
)
99 fatal("chan_set_ostate: bad state %d -> %d", c
->ostate
, next
);
100 debug2("channel %d: output %s -> %s", c
->self
, ostates
[c
->ostate
],
106 * SSH1 specific implementation of event functions
110 chan_rcvd_oclose1(Channel
*c
)
112 debug2("channel %d: rcvd oclose", c
->self
);
114 case CHAN_INPUT_WAIT_OCLOSE
:
115 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
117 case CHAN_INPUT_OPEN
:
118 chan_shutdown_read(c
);
120 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
122 case CHAN_INPUT_WAIT_DRAIN
:
123 /* both local read_failed and remote write_failed */
125 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
128 error("channel %d: protocol error: rcvd_oclose for istate %d",
134 chan_read_failed(Channel
*c
)
136 debug2("channel %d: read failed", c
->self
);
138 case CHAN_INPUT_OPEN
:
139 chan_shutdown_read(c
);
140 chan_set_istate(c
, CHAN_INPUT_WAIT_DRAIN
);
143 error("channel %d: chan_read_failed for istate %d",
149 chan_ibuf_empty(Channel
*c
)
151 debug2("channel %d: ibuf empty", c
->self
);
152 if (buffer_len(&c
->input
)) {
153 error("channel %d: chan_ibuf_empty for non empty buffer",
158 case CHAN_INPUT_WAIT_DRAIN
:
160 if (!(c
->flags
& CHAN_CLOSE_SENT
))
162 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
165 chan_set_istate(c
, CHAN_INPUT_WAIT_OCLOSE
);
169 error("channel %d: chan_ibuf_empty for istate %d",
175 chan_rcvd_ieof1(Channel
*c
)
177 debug2("channel %d: rcvd ieof", c
->self
);
179 case CHAN_OUTPUT_OPEN
:
180 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
182 case CHAN_OUTPUT_WAIT_IEOF
:
183 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
186 error("channel %d: protocol error: rcvd_ieof for ostate %d",
192 chan_write_failed1(Channel
*c
)
194 debug2("channel %d: write failed", c
->self
);
196 case CHAN_OUTPUT_OPEN
:
197 chan_shutdown_write(c
);
198 chan_send_oclose1(c
);
199 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_IEOF
);
201 case CHAN_OUTPUT_WAIT_DRAIN
:
202 chan_shutdown_write(c
);
203 chan_send_oclose1(c
);
204 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
207 error("channel %d: chan_write_failed for ostate %d",
213 chan_obuf_empty(Channel
*c
)
215 debug2("channel %d: obuf empty", c
->self
);
216 if (buffer_len(&c
->output
)) {
217 error("channel %d: chan_obuf_empty for non empty buffer",
222 case CHAN_OUTPUT_WAIT_DRAIN
:
223 chan_shutdown_write(c
);
225 chan_send_oclose1(c
);
226 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
229 error("channel %d: internal error: obuf_empty for ostate %d",
235 chan_send_ieof1(Channel
*c
)
237 debug2("channel %d: send ieof", c
->self
);
239 case CHAN_INPUT_OPEN
:
240 case CHAN_INPUT_WAIT_DRAIN
:
241 packet_start(SSH_MSG_CHANNEL_INPUT_EOF
);
242 packet_put_int(c
->remote_id
);
246 error("channel %d: cannot send ieof for istate %d",
252 chan_send_oclose1(Channel
*c
)
254 debug2("channel %d: send oclose", c
->self
);
256 case CHAN_OUTPUT_OPEN
:
257 case CHAN_OUTPUT_WAIT_DRAIN
:
258 buffer_clear(&c
->output
);
259 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE
);
260 packet_put_int(c
->remote_id
);
264 error("channel %d: cannot send oclose for ostate %d",
274 chan_rcvd_close2(Channel
*c
)
276 debug2("channel %d: rcvd close", c
->self
);
277 if (c
->flags
& CHAN_CLOSE_RCVD
)
278 error("channel %d: protocol error: close rcvd twice", c
->self
);
279 c
->flags
|= CHAN_CLOSE_RCVD
;
280 if (c
->type
== SSH_CHANNEL_LARVAL
) {
281 /* tear down larval channels immediately */
282 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
283 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
287 case CHAN_OUTPUT_OPEN
:
289 * wait until a data from the channel is consumed if a CLOSE
292 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
296 case CHAN_INPUT_OPEN
:
297 chan_shutdown_read(c
);
298 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
300 case CHAN_INPUT_WAIT_DRAIN
:
302 chan_set_istate(c
, CHAN_INPUT_CLOSED
);
307 chan_rcvd_eof2(Channel
*c
)
309 debug2("channel %d: rcvd eof", c
->self
);
310 c
->flags
|= CHAN_EOF_RCVD
;
311 if (c
->ostate
== CHAN_OUTPUT_OPEN
)
312 chan_set_ostate(c
, CHAN_OUTPUT_WAIT_DRAIN
);
315 chan_write_failed2(Channel
*c
)
317 debug2("channel %d: write failed", c
->self
);
319 case CHAN_OUTPUT_OPEN
:
320 case CHAN_OUTPUT_WAIT_DRAIN
:
321 chan_shutdown_write(c
);
322 chan_set_ostate(c
, CHAN_OUTPUT_CLOSED
);
325 error("channel %d: chan_write_failed for ostate %d",
331 chan_send_eof2(Channel
*c
)
333 debug2("channel %d: send eof", c
->self
);
335 case CHAN_INPUT_WAIT_DRAIN
:
336 packet_start(SSH2_MSG_CHANNEL_EOF
);
337 packet_put_int(c
->remote_id
);
339 c
->flags
|= CHAN_EOF_SENT
;
342 error("channel %d: cannot send eof for istate %d",
348 chan_send_close2(Channel
*c
)
350 debug2("channel %d: send close", c
->self
);
351 if (c
->ostate
!= CHAN_OUTPUT_CLOSED
||
352 c
->istate
!= CHAN_INPUT_CLOSED
) {
353 error("channel %d: cannot send close for istate/ostate %d/%d",
354 c
->self
, c
->istate
, c
->ostate
);
355 } else if (c
->flags
& CHAN_CLOSE_SENT
) {
356 error("channel %d: already sent close", c
->self
);
358 packet_start(SSH2_MSG_CHANNEL_CLOSE
);
359 packet_put_int(c
->remote_id
);
361 c
->flags
|= CHAN_CLOSE_SENT
;
368 chan_rcvd_ieof(Channel
*c
)
374 if (c
->ostate
== CHAN_OUTPUT_WAIT_DRAIN
&&
375 buffer_len(&c
->output
) == 0 &&
376 !CHANNEL_EFD_OUTPUT_ACTIVE(c
))
380 chan_rcvd_oclose(Channel
*c
)
385 chan_rcvd_oclose1(c
);
388 chan_write_failed(Channel
*c
)
391 chan_write_failed2(c
);
393 chan_write_failed1(c
);
397 chan_mark_dead(Channel
*c
)
399 c
->type
= SSH_CHANNEL_ZOMBIE
;
403 chan_is_dead(Channel
*c
, int do_send
)
405 if (c
->type
== SSH_CHANNEL_ZOMBIE
) {
406 debug2("channel %d: zombie", c
->self
);
409 if (c
->istate
!= CHAN_INPUT_CLOSED
|| c
->ostate
!= CHAN_OUTPUT_CLOSED
)
412 debug2("channel %d: is dead", c
->self
);
415 if ((datafellows
& SSH_BUG_EXTEOF
) &&
416 c
->extended_usage
== CHAN_EXTENDED_WRITE
&&
418 buffer_len(&c
->extended
) > 0) {
419 debug2("channel %d: active efd: %d len %d",
420 c
->self
, c
->efd
, buffer_len(&c
->extended
));
423 if (!(c
->flags
& CHAN_CLOSE_SENT
)) {
427 /* channel would be dead if we sent a close */
428 if (c
->flags
& CHAN_CLOSE_RCVD
) {
429 debug2("channel %d: almost dead",
435 if ((c
->flags
& CHAN_CLOSE_SENT
) &&
436 (c
->flags
& CHAN_CLOSE_RCVD
)) {
437 debug2("channel %d: is dead", c
->self
);
445 chan_shutdown_write(Channel
*c
)
447 buffer_clear(&c
->output
);
448 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
450 /* shutdown failure is allowed if write failed already */
451 debug2("channel %d: close_write", c
->self
);
453 if (shutdown(c
->sock
, SHUT_WR
) < 0)
454 debug2("channel %d: chan_shutdown_write: "
455 "shutdown() failed for fd%d: %.100s",
456 c
->self
, c
->sock
, strerror(errno
));
458 if (channel_close_fd(&c
->wfd
) < 0)
459 logit("channel %d: chan_shutdown_write: "
460 "close() failed for fd%d: %.100s",
461 c
->self
, c
->wfd
, strerror(errno
));
465 chan_shutdown_read(Channel
*c
)
467 if (compat20
&& c
->type
== SSH_CHANNEL_LARVAL
)
469 debug2("channel %d: close_read", c
->self
);
472 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
473 * write side has been closed already. (bug on Linux)
474 * HP-UX may return ENOTCONN also.
476 if (shutdown(c
->sock
, SHUT_RD
) < 0
477 && errno
!= ENOTCONN
)
478 error("channel %d: chan_shutdown_read: "
479 "shutdown() failed for fd%d [i%d o%d]: %.100s",
480 c
->self
, c
->sock
, c
->istate
, c
->ostate
,
483 if (channel_close_fd(&c
->rfd
) < 0)
484 logit("channel %d: chan_shutdown_read: "
485 "close() failed for fd%d: %.100s",
486 c
->self
, c
->rfd
, strerror(errno
));