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