1 /* $OpenBSD: nchan.c,v 1.57 2006/08/03 03:34:42 deraadt 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>
44 * SSH Protocol 1.5 aka New Channel Protocol
45 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
46 * Written by Markus Friedl in October 1999
48 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
49 * tear down of channels:
51 * 1.3: strict request-ack-protocol:
55 * 1.5: uses variations of:
60 * i.e. both sides have to close the channel
62 * 2.0: the EOF messages are optional
64 * See the debugging output from 'ssh -v' and 'sshd -d' of
65 * ssh-1.2.27 as an example.
69 /* functions manipulating channel states */
71 * EVENTS update channel input/output states execute ACTIONS
74 * ACTIONS: should never update the channel states
76 static void chan_send_ieof1(Channel *);
77 static void chan_send_oclose1(Channel *);
78 static void chan_send_close2(Channel *);
79 static void chan_send_eof2(Channel *);
82 static void chan_shutdown_write(Channel *);
83 static void chan_shutdown_read(Channel *);
85 static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
86 static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
89 chan_set_istate(Channel *c, u_int next)
91 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
92 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
93 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
98 chan_set_ostate(Channel *c, u_int next)
100 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
101 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
102 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
108 * SSH1 specific implementation of event functions
112 chan_rcvd_oclose1(Channel *c)
114 debug2("channel %d: rcvd oclose", c->self);
116 case CHAN_INPUT_WAIT_OCLOSE:
117 chan_set_istate(c, CHAN_INPUT_CLOSED);
119 case CHAN_INPUT_OPEN:
120 chan_shutdown_read(c);
122 chan_set_istate(c, CHAN_INPUT_CLOSED);
124 case CHAN_INPUT_WAIT_DRAIN:
125 /* both local read_failed and remote write_failed */
127 chan_set_istate(c, CHAN_INPUT_CLOSED);
130 error("channel %d: protocol error: rcvd_oclose for istate %d",
136 chan_read_failed(Channel *c)
138 debug2("channel %d: read failed", c->self);
140 case CHAN_INPUT_OPEN:
141 chan_shutdown_read(c);
142 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
145 error("channel %d: chan_read_failed for istate %d",
151 chan_ibuf_empty(Channel *c)
153 debug2("channel %d: ibuf empty", c->self);
154 if (buffer_len(&c->input)) {
155 error("channel %d: chan_ibuf_empty for non empty buffer",
160 case CHAN_INPUT_WAIT_DRAIN:
162 if (!(c->flags & CHAN_CLOSE_SENT))
164 chan_set_istate(c, CHAN_INPUT_CLOSED);
167 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
171 error("channel %d: chan_ibuf_empty for istate %d",
177 chan_rcvd_ieof1(Channel *c)
179 debug2("channel %d: rcvd ieof", c->self);
181 case CHAN_OUTPUT_OPEN:
182 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
184 case CHAN_OUTPUT_WAIT_IEOF:
185 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
188 error("channel %d: protocol error: rcvd_ieof for ostate %d",
194 chan_write_failed1(Channel *c)
196 debug2("channel %d: write failed", c->self);
198 case CHAN_OUTPUT_OPEN:
199 chan_shutdown_write(c);
200 chan_send_oclose1(c);
201 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
203 case CHAN_OUTPUT_WAIT_DRAIN:
204 chan_shutdown_write(c);
205 chan_send_oclose1(c);
206 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
209 error("channel %d: chan_write_failed for ostate %d",
215 chan_obuf_empty(Channel *c)
217 debug2("channel %d: obuf empty", c->self);
218 if (buffer_len(&c->output)) {
219 error("channel %d: chan_obuf_empty for non empty buffer",
224 case CHAN_OUTPUT_WAIT_DRAIN:
225 chan_shutdown_write(c);
227 chan_send_oclose1(c);
228 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
231 error("channel %d: internal error: obuf_empty for ostate %d",
237 chan_send_ieof1(Channel *c)
239 debug2("channel %d: send ieof", c->self);
241 case CHAN_INPUT_OPEN:
242 case CHAN_INPUT_WAIT_DRAIN:
243 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
244 packet_put_int(c->remote_id);
248 error("channel %d: cannot send ieof for istate %d",
254 chan_send_oclose1(Channel *c)
256 debug2("channel %d: send oclose", c->self);
258 case CHAN_OUTPUT_OPEN:
259 case CHAN_OUTPUT_WAIT_DRAIN:
260 buffer_clear(&c->output);
261 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
262 packet_put_int(c->remote_id);
266 error("channel %d: cannot send oclose for ostate %d",
276 chan_rcvd_close2(Channel *c)
278 debug2("channel %d: rcvd close", c->self);
279 if (c->flags & CHAN_CLOSE_RCVD)
280 error("channel %d: protocol error: close rcvd twice", c->self);
281 c->flags |= CHAN_CLOSE_RCVD;
282 if (c->type == SSH_CHANNEL_LARVAL) {
283 /* tear down larval channels immediately */
284 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
285 chan_set_istate(c, CHAN_INPUT_CLOSED);
289 case CHAN_OUTPUT_OPEN:
291 * wait until a data from the channel is consumed if a CLOSE
294 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
298 case CHAN_INPUT_OPEN:
299 chan_shutdown_read(c);
300 chan_set_istate(c, CHAN_INPUT_CLOSED);
302 case CHAN_INPUT_WAIT_DRAIN:
304 chan_set_istate(c, CHAN_INPUT_CLOSED);
309 chan_rcvd_eof2(Channel *c)
311 debug2("channel %d: rcvd eof", c->self);
312 c->flags |= CHAN_EOF_RCVD;
313 if (c->ostate == CHAN_OUTPUT_OPEN)
314 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
317 chan_write_failed2(Channel *c)
319 debug2("channel %d: write failed", c->self);
321 case CHAN_OUTPUT_OPEN:
322 case CHAN_OUTPUT_WAIT_DRAIN:
323 chan_shutdown_write(c);
324 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
327 error("channel %d: chan_write_failed for ostate %d",
333 chan_send_eof2(Channel *c)
335 debug2("channel %d: send eof", c->self);
337 case CHAN_INPUT_WAIT_DRAIN:
338 packet_start(SSH2_MSG_CHANNEL_EOF);
339 packet_put_int(c->remote_id);
341 c->flags |= CHAN_EOF_SENT;
344 error("channel %d: cannot send eof for istate %d",
350 chan_send_close2(Channel *c)
352 debug2("channel %d: send close", c->self);
353 if (c->ostate != CHAN_OUTPUT_CLOSED ||
354 c->istate != CHAN_INPUT_CLOSED) {
355 error("channel %d: cannot send close for istate/ostate %d/%d",
356 c->self, c->istate, c->ostate);
357 } else if (c->flags & CHAN_CLOSE_SENT) {
358 error("channel %d: already sent close", c->self);
360 packet_start(SSH2_MSG_CHANNEL_CLOSE);
361 packet_put_int(c->remote_id);
363 c->flags |= CHAN_CLOSE_SENT;
370 chan_rcvd_ieof(Channel *c)
376 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
377 buffer_len(&c->output) == 0 &&
378 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
382 chan_rcvd_oclose(Channel *c)
387 chan_rcvd_oclose1(c);
390 chan_write_failed(Channel *c)
393 chan_write_failed2(c);
395 chan_write_failed1(c);
399 chan_mark_dead(Channel *c)
401 c->type = SSH_CHANNEL_ZOMBIE;
405 chan_is_dead(Channel *c, int do_send)
407 if (c->type == SSH_CHANNEL_ZOMBIE) {
408 debug2("channel %d: zombie", c->self);
411 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
414 debug2("channel %d: is dead", c->self);
417 if ((datafellows & SSH_BUG_EXTEOF) &&
418 c->extended_usage == CHAN_EXTENDED_WRITE &&
420 buffer_len(&c->extended) > 0) {
421 debug2("channel %d: active efd: %d len %d",
422 c->self, c->efd, buffer_len(&c->extended));
425 if (!(c->flags & CHAN_CLOSE_SENT)) {
429 /* channel would be dead if we sent a close */
430 if (c->flags & CHAN_CLOSE_RCVD) {
431 debug2("channel %d: almost dead",
437 if ((c->flags & CHAN_CLOSE_SENT) &&
438 (c->flags & CHAN_CLOSE_RCVD)) {
439 debug2("channel %d: is dead", c->self);
447 chan_shutdown_write(Channel *c)
449 buffer_clear(&c->output);
450 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
452 /* shutdown failure is allowed if write failed already */
453 debug2("channel %d: close_write", c->self);
455 if (shutdown(c->sock, SHUT_WR) < 0)
456 debug2("channel %d: chan_shutdown_write: "
457 "shutdown() failed for fd%d: %.100s",
458 c->self, c->sock, strerror(errno));
460 if (channel_close_fd(&c->wfd) < 0)
461 logit("channel %d: chan_shutdown_write: "
462 "close() failed for fd%d: %.100s",
463 c->self, c->wfd, strerror(errno));
467 chan_shutdown_read(Channel *c)
469 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
471 debug2("channel %d: close_read", c->self);
474 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
475 * write side has been closed already. (bug on Linux)
476 * HP-UX may return ENOTCONN also.
478 if (shutdown(c->sock, SHUT_RD) < 0
479 && errno != ENOTCONN)
480 error("channel %d: chan_shutdown_read: "
481 "shutdown() failed for fd%d [i%d o%d]: %.100s",
482 c->self, c->sock, c->istate, c->ostate,
485 if (channel_close_fd(&c->rfd) < 0)
486 logit("channel %d: chan_shutdown_read: "
487 "close() failed for fd%d: %.100s",
488 c->self, c->rfd, strerror(errno));