]> andersk Git - openssh.git/blame - nchan.c
- markus@cvs.openbsd.org 2002/03/25 21:04:02
[openssh.git] / nchan.c
CommitLineData
aa3378df 1/*
8ab5f6b2 2 * Copyright (c) 1999, 2000, 2001, 2002 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"
32e7d71f 26RCSID("$OpenBSD: nchan.c,v 1.44 2002/01/21 23:27: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 */
7e7327a1 66/*
67 * ACTIONS: should never update the channel states
68 */
396c147e 69static void chan_send_ieof1(Channel *);
70static void chan_send_oclose1(Channel *);
71static void chan_send_close2(Channel *);
72static void chan_send_eof2(Channel *);
7e7327a1 73
7e7327a1 74/* helper */
396c147e 75static void chan_shutdown_write(Channel *);
76static void chan_shutdown_read(Channel *);
7e7327a1 77
368e9dfc 78static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
79static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
80
81static void
82chan_set_istate(Channel *c, u_int next)
83{
84 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
85 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
86 debug("channel %d: input %s -> %s", c->self, istates[c->istate],
87 istates[next]);
88 c->istate = next;
89}
90static void
91chan_set_ostate(Channel *c, u_int next)
92{
93 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
94 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
95 debug("channel %d: output %s -> %s", c->self, ostates[c->ostate],
96 ostates[next]);
97 c->ostate = next;
98}
99
7e7327a1 100/*
101 * SSH1 specific implementation of event functions
102 */
103
104static void
105chan_rcvd_oclose1(Channel *c)
5260325f 106{
7e7327a1 107 debug("channel %d: rcvd oclose", c->self);
5260325f 108 switch (c->istate) {
8efc0c15 109 case CHAN_INPUT_WAIT_OCLOSE:
368e9dfc 110 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 111 break;
112 case CHAN_INPUT_OPEN:
8efc0c15 113 chan_shutdown_read(c);
7e7327a1 114 chan_send_ieof1(c);
368e9dfc 115 chan_set_istate(c, CHAN_INPUT_CLOSED);
48e671d5 116 break;
117 case CHAN_INPUT_WAIT_DRAIN:
118 /* both local read_failed and remote write_failed */
7e7327a1 119 chan_send_ieof1(c);
368e9dfc 120 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 121 break;
122 default:
f048f3e2 123 error("channel %d: protocol error: rcvd_oclose for istate %d",
7e7327a1 124 c->self, c->istate);
48e671d5 125 return;
8efc0c15 126 }
127}
70bef40e 128void
129chan_read_failed(Channel *c)
5260325f 130{
7e7327a1 131 debug("channel %d: read failed", c->self);
5260325f 132 switch (c->istate) {
8efc0c15 133 case CHAN_INPUT_OPEN:
8efc0c15 134 chan_shutdown_read(c);
368e9dfc 135 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
8efc0c15 136 break;
137 default:
f048f3e2 138 error("channel %d: chan_read_failed for istate %d",
7e7327a1 139 c->self, c->istate);
8efc0c15 140 break;
141 }
142}
70bef40e 143void
144chan_ibuf_empty(Channel *c)
5260325f 145{
7e7327a1 146 debug("channel %d: ibuf empty", c->self);
5260325f 147 if (buffer_len(&c->input)) {
f048f3e2 148 error("channel %d: chan_ibuf_empty for non empty buffer",
7e7327a1 149 c->self);
8efc0c15 150 return;
151 }
5260325f 152 switch (c->istate) {
8efc0c15 153 case CHAN_INPUT_WAIT_DRAIN:
3057c23b 154 if (compat20) {
155 if (!(c->flags & CHAN_CLOSE_SENT))
156 chan_send_eof2(c);
157 chan_set_istate(c, CHAN_INPUT_CLOSED);
158 } else {
159 chan_send_ieof1(c);
160 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
161 }
8efc0c15 162 break;
163 default:
f048f3e2 164 error("channel %d: chan_ibuf_empty for istate %d",
7e7327a1 165 c->self, c->istate);
8efc0c15 166 break;
167 }
168}
7e7327a1 169static void
170chan_rcvd_ieof1(Channel *c)
5260325f 171{
7e7327a1 172 debug("channel %d: rcvd ieof", c->self);
5260325f 173 switch (c->ostate) {
8efc0c15 174 case CHAN_OUTPUT_OPEN:
368e9dfc 175 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 176 break;
177 case CHAN_OUTPUT_WAIT_IEOF:
368e9dfc 178 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 179 break;
180 default:
f048f3e2 181 error("channel %d: protocol error: rcvd_ieof for ostate %d",
7e7327a1 182 c->self, c->ostate);
8efc0c15 183 break;
184 }
185}
7e7327a1 186static void
187chan_write_failed1(Channel *c)
5260325f 188{
7e7327a1 189 debug("channel %d: write failed", c->self);
5260325f 190 switch (c->ostate) {
8efc0c15 191 case CHAN_OUTPUT_OPEN:
1e5e896c 192 chan_shutdown_write(c);
7e7327a1 193 chan_send_oclose1(c);
368e9dfc 194 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
8efc0c15 195 break;
196 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 197 chan_shutdown_write(c);
7e7327a1 198 chan_send_oclose1(c);
368e9dfc 199 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 200 break;
201 default:
f048f3e2 202 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 203 c->self, c->ostate);
8efc0c15 204 break;
205 }
206}
70bef40e 207void
208chan_obuf_empty(Channel *c)
5260325f 209{
7e7327a1 210 debug("channel %d: obuf empty", c->self);
5260325f 211 if (buffer_len(&c->output)) {
f048f3e2 212 error("channel %d: chan_obuf_empty for non empty buffer",
7e7327a1 213 c->self);
8efc0c15 214 return;
215 }
5260325f 216 switch (c->ostate) {
8efc0c15 217 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 218 chan_shutdown_write(c);
3057c23b 219 if (!compat20)
220 chan_send_oclose1(c);
368e9dfc 221 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 222 break;
223 default:
f048f3e2 224 error("channel %d: internal error: obuf_empty for ostate %d",
7e7327a1 225 c->self, c->ostate);
8efc0c15 226 break;
227 }
228}
8efc0c15 229static void
7e7327a1 230chan_send_ieof1(Channel *c)
5260325f 231{
7e7327a1 232 debug("channel %d: send ieof", c->self);
5260325f 233 switch (c->istate) {
8efc0c15 234 case CHAN_INPUT_OPEN:
235 case CHAN_INPUT_WAIT_DRAIN:
236 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
237 packet_put_int(c->remote_id);
238 packet_send();
239 break;
240 default:
f048f3e2 241 error("channel %d: cannot send ieof for istate %d",
7e7327a1 242 c->self, c->istate);
8efc0c15 243 break;
244 }
245}
246static void
7e7327a1 247chan_send_oclose1(Channel *c)
5260325f 248{
7e7327a1 249 debug("channel %d: send oclose", c->self);
5260325f 250 switch (c->ostate) {
8efc0c15 251 case CHAN_OUTPUT_OPEN:
252 case CHAN_OUTPUT_WAIT_DRAIN:
9c50edcf 253 buffer_clear(&c->output);
8efc0c15 254 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
255 packet_put_int(c->remote_id);
256 packet_send();
257 break;
258 default:
f048f3e2 259 error("channel %d: cannot send oclose for ostate %d",
184eed6a 260 c->self, c->ostate);
8efc0c15 261 break;
262 }
263}
5260325f 264
7e7327a1 265/*
266 * the same for SSH2
267 */
8efc0c15 268static void
668a91b7 269chan_rcvd_close2(Channel *c)
5260325f 270{
7e7327a1 271 debug("channel %d: rcvd close", c->self);
272 if (c->flags & CHAN_CLOSE_RCVD)
273 error("channel %d: protocol error: close rcvd twice", c->self);
274 c->flags |= CHAN_CLOSE_RCVD;
275 if (c->type == SSH_CHANNEL_LARVAL) {
276 /* tear down larval channels immediately */
368e9dfc 277 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
278 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 279 return;
280 }
281 switch (c->ostate) {
282 case CHAN_OUTPUT_OPEN:
f048f3e2 283 /*
284 * wait until a data from the channel is consumed if a CLOSE
285 * is received
286 */
368e9dfc 287 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
7e7327a1 288 break;
289 }
290 switch (c->istate) {
291 case CHAN_INPUT_OPEN:
7e7327a1 292 chan_shutdown_read(c);
668a91b7 293 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 294 break;
295 case CHAN_INPUT_WAIT_DRAIN:
7e7327a1 296 chan_send_eof2(c);
668a91b7 297 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 298 break;
299 }
8efc0c15 300}
301static void
668a91b7 302chan_rcvd_eof2(Channel *c)
5260325f 303{
7e7327a1 304 debug("channel %d: rcvd eof", c->self);
368e9dfc 305 if (c->ostate == CHAN_OUTPUT_OPEN)
306 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 307}
7e7327a1 308static void
309chan_write_failed2(Channel *c)
310{
311 debug("channel %d: write failed", c->self);
312 switch (c->ostate) {
313 case CHAN_OUTPUT_OPEN:
7e7327a1 314 case CHAN_OUTPUT_WAIT_DRAIN:
7e7327a1 315 chan_shutdown_write(c);
368e9dfc 316 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
7e7327a1 317 break;
318 default:
f048f3e2 319 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 320 c->self, c->ostate);
321 break;
322 }
323}
324static void
7e7327a1 325chan_send_eof2(Channel *c)
326{
327 debug("channel %d: send eof", c->self);
328 switch (c->istate) {
329 case CHAN_INPUT_WAIT_DRAIN:
330 packet_start(SSH2_MSG_CHANNEL_EOF);
331 packet_put_int(c->remote_id);
332 packet_send();
333 break;
334 default:
f048f3e2 335 error("channel %d: cannot send eof for istate %d",
7e7327a1 336 c->self, c->istate);
337 break;
338 }
339}
340static void
341chan_send_close2(Channel *c)
342{
343 debug("channel %d: send close", c->self);
344 if (c->ostate != CHAN_OUTPUT_CLOSED ||
345 c->istate != CHAN_INPUT_CLOSED) {
f048f3e2 346 error("channel %d: cannot send close for istate/ostate %d/%d",
7e7327a1 347 c->self, c->istate, c->ostate);
348 } else if (c->flags & CHAN_CLOSE_SENT) {
f048f3e2 349 error("channel %d: already sent close", c->self);
7e7327a1 350 } else {
351 packet_start(SSH2_MSG_CHANNEL_CLOSE);
352 packet_put_int(c->remote_id);
353 packet_send();
354 c->flags |= CHAN_CLOSE_SENT;
355 }
356}
ee55dacf 357
358/* shared */
359
70bef40e 360void
361chan_rcvd_ieof(Channel *c)
362{
363 if (compat20)
364 chan_rcvd_eof2(c);
365 else
366 chan_rcvd_ieof1(c);
32e7d71f 367 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
368 buffer_len(&c->output) == 0)
369 chan_obuf_empty(c);
70bef40e 370}
371void
372chan_rcvd_oclose(Channel *c)
373{
374 if (compat20)
375 chan_rcvd_close2(c);
376 else
377 chan_rcvd_oclose1(c);
378}
379void
380chan_write_failed(Channel *c)
381{
382 if (compat20)
383 chan_write_failed2(c);
384 else
385 chan_write_failed1(c);
386}
387
719fc62f 388void
389chan_mark_dead(Channel *c)
390{
6fd8622b 391 c->type = SSH_CHANNEL_ZOMBIE;
719fc62f 392}
393
ee55dacf 394int
418e724c 395chan_is_dead(Channel *c, int send)
5260325f 396{
6fd8622b 397 if (c->type == SSH_CHANNEL_ZOMBIE) {
398 debug("channel %d: zombie", c->self);
719fc62f 399 return 1;
6fd8622b 400 }
ee55dacf 401 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
402 return 0;
403 if (!compat20) {
404 debug("channel %d: is dead", c->self);
405 return 1;
406 }
407 /*
408 * we have to delay the close message if the efd (for stderr) is
409 * still active
410 */
411 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
412 buffer_len(&c->extended) > 0)
413#if 0
414 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
415 c->efd != -1)
416#endif
417 ) {
418 debug2("channel %d: active efd: %d len %d type %s",
419 c->self, c->efd, buffer_len(&c->extended),
420 c->extended_usage==CHAN_EXTENDED_READ ?
184eed6a 421 "read": "write");
ee55dacf 422 } else {
7e7327a1 423 if (!(c->flags & CHAN_CLOSE_SENT)) {
418e724c 424 if (send) {
425 chan_send_close2(c);
426 } else {
427 /* channel would be dead if we sent a close */
428 if (c->flags & CHAN_CLOSE_RCVD) {
429 debug("channel %d: almost dead",
430 c->self);
431 return 1;
432 }
433 }
7e7327a1 434 }
6ae2364d 435 if ((c->flags & CHAN_CLOSE_SENT) &&
7e7327a1 436 (c->flags & CHAN_CLOSE_RCVD)) {
ee55dacf 437 debug("channel %d: is dead", c->self);
438 return 1;
6ae2364d 439 }
8efc0c15 440 }
ee55dacf 441 return 0;
8efc0c15 442}
7e7327a1 443
7e7327a1 444/* helper */
445static void
446chan_shutdown_write(Channel *c)
447{
9c50edcf 448 buffer_clear(&c->output);
7e7327a1 449 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
450 return;
451 /* shutdown failure is allowed if write failed already */
452 debug("channel %d: close_write", c->self);
453 if (c->sock != -1) {
454 if (shutdown(c->sock, SHUT_WR) < 0)
f048f3e2 455 debug("channel %d: chan_shutdown_write: "
456 "shutdown() failed for fd%d: %.100s",
7e7327a1 457 c->self, c->sock, strerror(errno));
458 } else {
489aa2e9 459 if (channel_close_fd(&c->wfd) < 0)
f048f3e2 460 log("channel %d: chan_shutdown_write: "
461 "close() failed for fd%d: %.100s",
7e7327a1 462 c->self, c->wfd, strerror(errno));
7e7327a1 463 }
464}
465static void
466chan_shutdown_read(Channel *c)
467{
468 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
469 return;
470 debug("channel %d: close_read", c->self);
471 if (c->sock != -1) {
2b87da3b 472 /*
47670e77 473 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
474 * write side has been closed already. (bug on Linux)
f49df8e9 475 * HP-UX may return ENOTCONN also.
47670e77 476 */
477 if (shutdown(c->sock, SHUT_RD) < 0
f49df8e9 478 && errno != ENOTCONN)
f048f3e2 479 error("channel %d: chan_shutdown_read: "
480 "shutdown() failed for fd%d [i%d o%d]: %.100s",
481 c->self, c->sock, c->istate, c->ostate,
1b73841d 482 strerror(errno));
7e7327a1 483 } else {
489aa2e9 484 if (channel_close_fd(&c->rfd) < 0)
f048f3e2 485 log("channel %d: chan_shutdown_read: "
486 "close() failed for fd%d: %.100s",
7e7327a1 487 c->self, c->rfd, strerror(errno));
7e7327a1 488 }
8efc0c15 489}
This page took 0.215282 seconds and 5 git commands to generate.