]> andersk Git - openssh.git/blame - nchan.c
- markus@cvs.openbsd.org 2002/01/14 13:40:10
[openssh.git] / nchan.c
CommitLineData
aa3378df 1/*
a96070d4 2 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
aa3378df 3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
aa3378df 12 *
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.
23 */
24
8efc0c15 25#include "includes.h"
668a91b7 26RCSID("$OpenBSD: nchan.c,v 1.40 2002/01/14 13:40:10 markus Exp $");
8efc0c15 27
42f11eb2 28#include "ssh1.h"
29#include "ssh2.h"
8efc0c15 30#include "buffer.h"
31#include "packet.h"
32#include "channels.h"
7e7327a1 33#include "compat.h"
42f11eb2 34#include "log.h"
8efc0c15 35
f048f3e2 36/*
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
40 *
41 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
42 * tear down of channels:
43 *
44 * 1.3: strict request-ack-protocol:
45 * CLOSE ->
46 * <- CLOSE_CONFIRM
47 *
48 * 1.5: uses variations of:
49 * IEOF ->
50 * <- OCLOSE
51 * <- IEOF
52 * OCLOSE ->
53 * i.e. both sides have to close the channel
54 *
55 * 2.0: the EOF messages are optional
56 *
57 * See the debugging output from 'ssh -v' and 'sshd -d' of
58 * ssh-1.2.27 as an example.
59 *
60 */
1b73841d 61
7e7327a1 62/* functions manipulating channel states */
8efc0c15 63/*
5260325f 64 * EVENTS update channel input/output states execute ACTIONS
8efc0c15 65 */
66/* events concerning the INPUT from socket for channel (istate) */
7e7327a1 67chan_event_fn *chan_rcvd_oclose = NULL;
68chan_event_fn *chan_read_failed = NULL;
69chan_event_fn *chan_ibuf_empty = NULL;
70/* events concerning the OUTPUT from channel for socket (ostate) */
71chan_event_fn *chan_rcvd_ieof = NULL;
72chan_event_fn *chan_write_failed = NULL;
73chan_event_fn *chan_obuf_empty = NULL;
74/*
75 * ACTIONS: should never update the channel states
76 */
396c147e 77static void chan_send_ieof1(Channel *);
78static void chan_send_oclose1(Channel *);
79static void chan_send_close2(Channel *);
80static void chan_send_eof2(Channel *);
7e7327a1 81
7e7327a1 82/* helper */
396c147e 83static void chan_shutdown_write(Channel *);
84static void chan_shutdown_read(Channel *);
7e7327a1 85
368e9dfc 86static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
87static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
88
89static void
90chan_set_istate(Channel *c, u_int next)
91{
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],
95 istates[next]);
96 c->istate = next;
97}
98static void
99chan_set_ostate(Channel *c, u_int next)
100{
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],
104 ostates[next]);
105 c->ostate = next;
106}
107
7e7327a1 108/*
109 * SSH1 specific implementation of event functions
110 */
111
112static void
113chan_rcvd_oclose1(Channel *c)
5260325f 114{
7e7327a1 115 debug("channel %d: rcvd oclose", c->self);
5260325f 116 switch (c->istate) {
8efc0c15 117 case CHAN_INPUT_WAIT_OCLOSE:
368e9dfc 118 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 119 break;
120 case CHAN_INPUT_OPEN:
8efc0c15 121 chan_shutdown_read(c);
7e7327a1 122 chan_send_ieof1(c);
368e9dfc 123 chan_set_istate(c, CHAN_INPUT_CLOSED);
48e671d5 124 break;
125 case CHAN_INPUT_WAIT_DRAIN:
126 /* both local read_failed and remote write_failed */
7e7327a1 127 chan_send_ieof1(c);
368e9dfc 128 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 129 break;
130 default:
f048f3e2 131 error("channel %d: protocol error: rcvd_oclose for istate %d",
7e7327a1 132 c->self, c->istate);
48e671d5 133 return;
8efc0c15 134 }
135}
7e7327a1 136static void
137chan_read_failed_12(Channel *c)
5260325f 138{
7e7327a1 139 debug("channel %d: read failed", c->self);
5260325f 140 switch (c->istate) {
8efc0c15 141 case CHAN_INPUT_OPEN:
8efc0c15 142 chan_shutdown_read(c);
368e9dfc 143 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
8efc0c15 144 break;
145 default:
f048f3e2 146 error("channel %d: chan_read_failed for istate %d",
7e7327a1 147 c->self, c->istate);
8efc0c15 148 break;
149 }
150}
7e7327a1 151static void
152chan_ibuf_empty1(Channel *c)
5260325f 153{
7e7327a1 154 debug("channel %d: ibuf empty", c->self);
5260325f 155 if (buffer_len(&c->input)) {
f048f3e2 156 error("channel %d: chan_ibuf_empty for non empty buffer",
7e7327a1 157 c->self);
8efc0c15 158 return;
159 }
5260325f 160 switch (c->istate) {
8efc0c15 161 case CHAN_INPUT_WAIT_DRAIN:
3057c23b 162 if (compat20) {
163 if (!(c->flags & CHAN_CLOSE_SENT))
164 chan_send_eof2(c);
165 chan_set_istate(c, CHAN_INPUT_CLOSED);
166 } else {
167 chan_send_ieof1(c);
168 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
169 }
8efc0c15 170 break;
171 default:
f048f3e2 172 error("channel %d: chan_ibuf_empty for istate %d",
7e7327a1 173 c->self, c->istate);
8efc0c15 174 break;
175 }
176}
7e7327a1 177static void
178chan_rcvd_ieof1(Channel *c)
5260325f 179{
7e7327a1 180 debug("channel %d: rcvd ieof", c->self);
5260325f 181 switch (c->ostate) {
8efc0c15 182 case CHAN_OUTPUT_OPEN:
368e9dfc 183 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 184 break;
185 case CHAN_OUTPUT_WAIT_IEOF:
368e9dfc 186 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 187 break;
188 default:
f048f3e2 189 error("channel %d: protocol error: rcvd_ieof for ostate %d",
7e7327a1 190 c->self, c->ostate);
8efc0c15 191 break;
192 }
193}
7e7327a1 194static void
195chan_write_failed1(Channel *c)
5260325f 196{
7e7327a1 197 debug("channel %d: write failed", c->self);
5260325f 198 switch (c->ostate) {
8efc0c15 199 case CHAN_OUTPUT_OPEN:
1e5e896c 200 chan_shutdown_write(c);
7e7327a1 201 chan_send_oclose1(c);
368e9dfc 202 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
8efc0c15 203 break;
204 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 205 chan_shutdown_write(c);
7e7327a1 206 chan_send_oclose1(c);
368e9dfc 207 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 208 break;
209 default:
f048f3e2 210 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 211 c->self, c->ostate);
8efc0c15 212 break;
213 }
214}
7e7327a1 215static void
216chan_obuf_empty1(Channel *c)
5260325f 217{
7e7327a1 218 debug("channel %d: obuf empty", c->self);
5260325f 219 if (buffer_len(&c->output)) {
f048f3e2 220 error("channel %d: chan_obuf_empty for non empty buffer",
7e7327a1 221 c->self);
8efc0c15 222 return;
223 }
5260325f 224 switch (c->ostate) {
8efc0c15 225 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 226 chan_shutdown_write(c);
3057c23b 227 if (!compat20)
228 chan_send_oclose1(c);
368e9dfc 229 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 230 break;
231 default:
f048f3e2 232 error("channel %d: internal error: obuf_empty for ostate %d",
7e7327a1 233 c->self, c->ostate);
8efc0c15 234 break;
235 }
236}
8efc0c15 237static void
7e7327a1 238chan_send_ieof1(Channel *c)
5260325f 239{
7e7327a1 240 debug("channel %d: send ieof", c->self);
5260325f 241 switch (c->istate) {
8efc0c15 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);
246 packet_send();
247 break;
248 default:
f048f3e2 249 error("channel %d: cannot send ieof for istate %d",
7e7327a1 250 c->self, c->istate);
8efc0c15 251 break;
252 }
253}
254static void
7e7327a1 255chan_send_oclose1(Channel *c)
5260325f 256{
7e7327a1 257 debug("channel %d: send oclose", c->self);
5260325f 258 switch (c->ostate) {
8efc0c15 259 case CHAN_OUTPUT_OPEN:
260 case CHAN_OUTPUT_WAIT_DRAIN:
9c50edcf 261 buffer_clear(&c->output);
8efc0c15 262 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
263 packet_put_int(c->remote_id);
264 packet_send();
265 break;
266 default:
f048f3e2 267 error("channel %d: cannot send oclose for ostate %d",
184eed6a 268 c->self, c->ostate);
8efc0c15 269 break;
270 }
271}
5260325f 272
7e7327a1 273/*
274 * the same for SSH2
275 */
8efc0c15 276static void
668a91b7 277chan_rcvd_close2(Channel *c)
5260325f 278{
7e7327a1 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 */
368e9dfc 285 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
286 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 287 return;
288 }
289 switch (c->ostate) {
290 case CHAN_OUTPUT_OPEN:
f048f3e2 291 /*
292 * wait until a data from the channel is consumed if a CLOSE
293 * is received
294 */
368e9dfc 295 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
7e7327a1 296 break;
297 }
298 switch (c->istate) {
299 case CHAN_INPUT_OPEN:
7e7327a1 300 chan_shutdown_read(c);
668a91b7 301 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 302 break;
303 case CHAN_INPUT_WAIT_DRAIN:
7e7327a1 304 chan_send_eof2(c);
668a91b7 305 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 306 break;
307 }
8efc0c15 308}
309static void
7e7327a1 310chan_ibuf_empty2(Channel *c)
311{
3057c23b 312 chan_ibuf_empty1(c);
7e7327a1 313}
314static void
668a91b7 315chan_rcvd_eof2(Channel *c)
5260325f 316{
7e7327a1 317 debug("channel %d: rcvd eof", c->self);
368e9dfc 318 if (c->ostate == CHAN_OUTPUT_OPEN)
319 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 320}
7e7327a1 321static void
322chan_write_failed2(Channel *c)
323{
324 debug("channel %d: write failed", c->self);
325 switch (c->ostate) {
326 case CHAN_OUTPUT_OPEN:
1d1ffb87 327 chan_shutdown_write(c); /* ?? */
368e9dfc 328 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
7e7327a1 329 break;
330 case CHAN_OUTPUT_WAIT_DRAIN:
7e7327a1 331 chan_shutdown_write(c);
368e9dfc 332 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
7e7327a1 333 break;
334 default:
f048f3e2 335 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 336 c->self, c->ostate);
337 break;
338 }
339}
340static void
341chan_obuf_empty2(Channel *c)
342{
3057c23b 343 chan_obuf_empty1(c);
7e7327a1 344}
345static void
346chan_send_eof2(Channel *c)
347{
348 debug("channel %d: send eof", c->self);
349 switch (c->istate) {
350 case CHAN_INPUT_WAIT_DRAIN:
351 packet_start(SSH2_MSG_CHANNEL_EOF);
352 packet_put_int(c->remote_id);
353 packet_send();
354 break;
355 default:
f048f3e2 356 error("channel %d: cannot send eof for istate %d",
7e7327a1 357 c->self, c->istate);
358 break;
359 }
360}
361static void
362chan_send_close2(Channel *c)
363{
364 debug("channel %d: send close", c->self);
365 if (c->ostate != CHAN_OUTPUT_CLOSED ||
366 c->istate != CHAN_INPUT_CLOSED) {
f048f3e2 367 error("channel %d: cannot send close for istate/ostate %d/%d",
7e7327a1 368 c->self, c->istate, c->ostate);
369 } else if (c->flags & CHAN_CLOSE_SENT) {
f048f3e2 370 error("channel %d: already sent close", c->self);
7e7327a1 371 } else {
372 packet_start(SSH2_MSG_CHANNEL_CLOSE);
373 packet_put_int(c->remote_id);
374 packet_send();
375 c->flags |= CHAN_CLOSE_SENT;
376 }
377}
ee55dacf 378
379/* shared */
380
719fc62f 381void
382chan_mark_dead(Channel *c)
383{
6fd8622b 384 c->type = SSH_CHANNEL_ZOMBIE;
719fc62f 385}
386
ee55dacf 387int
418e724c 388chan_is_dead(Channel *c, int send)
5260325f 389{
6fd8622b 390 if (c->type == SSH_CHANNEL_ZOMBIE) {
391 debug("channel %d: zombie", c->self);
719fc62f 392 return 1;
6fd8622b 393 }
ee55dacf 394 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
395 return 0;
396 if (!compat20) {
397 debug("channel %d: is dead", c->self);
398 return 1;
399 }
400 /*
401 * we have to delay the close message if the efd (for stderr) is
402 * still active
403 */
404 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
405 buffer_len(&c->extended) > 0)
406#if 0
407 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
408 c->efd != -1)
409#endif
410 ) {
411 debug2("channel %d: active efd: %d len %d type %s",
412 c->self, c->efd, buffer_len(&c->extended),
413 c->extended_usage==CHAN_EXTENDED_READ ?
184eed6a 414 "read": "write");
ee55dacf 415 } else {
7e7327a1 416 if (!(c->flags & CHAN_CLOSE_SENT)) {
418e724c 417 if (send) {
418 chan_send_close2(c);
419 } else {
420 /* channel would be dead if we sent a close */
421 if (c->flags & CHAN_CLOSE_RCVD) {
422 debug("channel %d: almost dead",
423 c->self);
424 return 1;
425 }
426 }
7e7327a1 427 }
6ae2364d 428 if ((c->flags & CHAN_CLOSE_SENT) &&
7e7327a1 429 (c->flags & CHAN_CLOSE_RCVD)) {
ee55dacf 430 debug("channel %d: is dead", c->self);
431 return 1;
6ae2364d 432 }
8efc0c15 433 }
ee55dacf 434 return 0;
8efc0c15 435}
7e7327a1 436
8efc0c15 437void
5260325f 438chan_init_iostates(Channel *c)
439{
440 c->ostate = CHAN_OUTPUT_OPEN;
441 c->istate = CHAN_INPUT_OPEN;
7e7327a1 442 c->flags = 0;
443}
444
445/* init */
446void
447chan_init(void)
448{
449 if (compat20) {
668a91b7 450 chan_rcvd_oclose = chan_rcvd_close2;
7e7327a1 451 chan_read_failed = chan_read_failed_12;
452 chan_ibuf_empty = chan_ibuf_empty2;
453
668a91b7 454 chan_rcvd_ieof = chan_rcvd_eof2;
7e7327a1 455 chan_write_failed = chan_write_failed2;
456 chan_obuf_empty = chan_obuf_empty2;
7e7327a1 457 } else {
458 chan_rcvd_oclose = chan_rcvd_oclose1;
459 chan_read_failed = chan_read_failed_12;
460 chan_ibuf_empty = chan_ibuf_empty1;
461
462 chan_rcvd_ieof = chan_rcvd_ieof1;
463 chan_write_failed = chan_write_failed1;
464 chan_obuf_empty = chan_obuf_empty1;
7e7327a1 465 }
466}
467
468/* helper */
469static void
470chan_shutdown_write(Channel *c)
471{
9c50edcf 472 buffer_clear(&c->output);
7e7327a1 473 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
474 return;
475 /* shutdown failure is allowed if write failed already */
476 debug("channel %d: close_write", c->self);
477 if (c->sock != -1) {
478 if (shutdown(c->sock, SHUT_WR) < 0)
f048f3e2 479 debug("channel %d: chan_shutdown_write: "
480 "shutdown() failed for fd%d: %.100s",
7e7327a1 481 c->self, c->sock, strerror(errno));
482 } else {
489aa2e9 483 if (channel_close_fd(&c->wfd) < 0)
f048f3e2 484 log("channel %d: chan_shutdown_write: "
485 "close() failed for fd%d: %.100s",
7e7327a1 486 c->self, c->wfd, strerror(errno));
7e7327a1 487 }
488}
489static void
490chan_shutdown_read(Channel *c)
491{
492 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
493 return;
494 debug("channel %d: close_read", c->self);
495 if (c->sock != -1) {
2b87da3b 496 /*
47670e77 497 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
498 * write side has been closed already. (bug on Linux)
f49df8e9 499 * HP-UX may return ENOTCONN also.
47670e77 500 */
501 if (shutdown(c->sock, SHUT_RD) < 0
f49df8e9 502 && errno != ENOTCONN)
f048f3e2 503 error("channel %d: chan_shutdown_read: "
504 "shutdown() failed for fd%d [i%d o%d]: %.100s",
505 c->self, c->sock, c->istate, c->ostate,
1b73841d 506 strerror(errno));
7e7327a1 507 } else {
489aa2e9 508 if (channel_close_fd(&c->rfd) < 0)
f048f3e2 509 log("channel %d: chan_shutdown_read: "
510 "close() failed for fd%d: %.100s",
7e7327a1 511 c->self, c->rfd, strerror(errno));
7e7327a1 512 }
8efc0c15 513}
This page took 0.208842 seconds and 5 git commands to generate.