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