]> andersk Git - openssh.git/blame - nchan.c
- markus@cvs.openbsd.org 2008/05/09 16:17:51
[openssh.git] / nchan.c
CommitLineData
3593bdc0 1/* $OpenBSD: nchan.c,v 1.58 2008/05/08 12:02:23 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 *);
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);
d7ac5f18 94 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
368e9dfc 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);
d7ac5f18 103 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
368e9dfc 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{
d7ac5f18 115 debug2("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}
70bef40e 136void
137chan_read_failed(Channel *c)
5260325f 138{
d7ac5f18 139 debug2("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}
70bef40e 151void
152chan_ibuf_empty(Channel *c)
5260325f 153{
d7ac5f18 154 debug2("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{
d7ac5f18 180 debug2("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{
d7ac5f18 197 debug2("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}
70bef40e 215void
216chan_obuf_empty(Channel *c)
5260325f 217{
d7ac5f18 218 debug2("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{
d7ac5f18 240 debug2("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{
d7ac5f18 257 debug2("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{
d7ac5f18 279 debug2("channel %d: rcvd close", c->self);
7e7327a1 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
668a91b7 310chan_rcvd_eof2(Channel *c)
5260325f 311{
d7ac5f18 312 debug2("channel %d: rcvd eof", c->self);
d2296ed7 313 c->flags |= CHAN_EOF_RCVD;
368e9dfc 314 if (c->ostate == CHAN_OUTPUT_OPEN)
315 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 316}
7e7327a1 317static void
318chan_write_failed2(Channel *c)
319{
d7ac5f18 320 debug2("channel %d: write failed", c->self);
7e7327a1 321 switch (c->ostate) {
322 case CHAN_OUTPUT_OPEN:
7e7327a1 323 case CHAN_OUTPUT_WAIT_DRAIN:
7e7327a1 324 chan_shutdown_write(c);
368e9dfc 325 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
7e7327a1 326 break;
327 default:
f048f3e2 328 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 329 c->self, c->ostate);
330 break;
331 }
332}
333static void
7e7327a1 334chan_send_eof2(Channel *c)
335{
d7ac5f18 336 debug2("channel %d: send eof", c->self);
7e7327a1 337 switch (c->istate) {
338 case CHAN_INPUT_WAIT_DRAIN:
339 packet_start(SSH2_MSG_CHANNEL_EOF);
340 packet_put_int(c->remote_id);
341 packet_send();
d2296ed7 342 c->flags |= CHAN_EOF_SENT;
7e7327a1 343 break;
344 default:
f048f3e2 345 error("channel %d: cannot send eof for istate %d",
7e7327a1 346 c->self, c->istate);
347 break;
348 }
349}
350static void
351chan_send_close2(Channel *c)
352{
d7ac5f18 353 debug2("channel %d: send close", c->self);
7e7327a1 354 if (c->ostate != CHAN_OUTPUT_CLOSED ||
355 c->istate != CHAN_INPUT_CLOSED) {
f048f3e2 356 error("channel %d: cannot send close for istate/ostate %d/%d",
7e7327a1 357 c->self, c->istate, c->ostate);
358 } else if (c->flags & CHAN_CLOSE_SENT) {
f048f3e2 359 error("channel %d: already sent close", c->self);
7e7327a1 360 } else {
361 packet_start(SSH2_MSG_CHANNEL_CLOSE);
362 packet_put_int(c->remote_id);
363 packet_send();
364 c->flags |= CHAN_CLOSE_SENT;
365 }
366}
ee55dacf 367
368/* shared */
369
70bef40e 370void
371chan_rcvd_ieof(Channel *c)
372{
373 if (compat20)
374 chan_rcvd_eof2(c);
375 else
376 chan_rcvd_ieof1(c);
32e7d71f 377 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
7203d6bb 378 buffer_len(&c->output) == 0 &&
d2296ed7 379 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
32e7d71f 380 chan_obuf_empty(c);
70bef40e 381}
382void
383chan_rcvd_oclose(Channel *c)
384{
385 if (compat20)
386 chan_rcvd_close2(c);
387 else
388 chan_rcvd_oclose1(c);
389}
390void
391chan_write_failed(Channel *c)
392{
393 if (compat20)
394 chan_write_failed2(c);
395 else
396 chan_write_failed1(c);
397}
398
719fc62f 399void
400chan_mark_dead(Channel *c)
401{
6fd8622b 402 c->type = SSH_CHANNEL_ZOMBIE;
719fc62f 403}
404
ee55dacf 405int
ca75d7de 406chan_is_dead(Channel *c, int do_send)
5260325f 407{
6fd8622b 408 if (c->type == SSH_CHANNEL_ZOMBIE) {
d7ac5f18 409 debug2("channel %d: zombie", c->self);
719fc62f 410 return 1;
6fd8622b 411 }
ee55dacf 412 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
413 return 0;
414 if (!compat20) {
d7ac5f18 415 debug2("channel %d: is dead", c->self);
ee55dacf 416 return 1;
417 }
d2296ed7 418 if ((datafellows & SSH_BUG_EXTEOF) &&
419 c->extended_usage == CHAN_EXTENDED_WRITE &&
420 c->efd != -1 &&
421 buffer_len(&c->extended) > 0) {
875ec275 422 debug2("channel %d: active efd: %d len %d",
423 c->self, c->efd, buffer_len(&c->extended));
d2296ed7 424 return 0;
425 }
426 if (!(c->flags & CHAN_CLOSE_SENT)) {
ca75d7de 427 if (do_send) {
d2296ed7 428 chan_send_close2(c);
429 } else {
430 /* channel would be dead if we sent a close */
431 if (c->flags & CHAN_CLOSE_RCVD) {
d7ac5f18 432 debug2("channel %d: almost dead",
d2296ed7 433 c->self);
434 return 1;
418e724c 435 }
7e7327a1 436 }
d2296ed7 437 }
438 if ((c->flags & CHAN_CLOSE_SENT) &&
439 (c->flags & CHAN_CLOSE_RCVD)) {
d7ac5f18 440 debug2("channel %d: is dead", c->self);
d2296ed7 441 return 1;
8efc0c15 442 }
ee55dacf 443 return 0;
8efc0c15 444}
7e7327a1 445
7e7327a1 446/* helper */
447static void
448chan_shutdown_write(Channel *c)
449{
9c50edcf 450 buffer_clear(&c->output);
7e7327a1 451 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
452 return;
453 /* shutdown failure is allowed if write failed already */
d7ac5f18 454 debug2("channel %d: close_write", c->self);
7e7327a1 455 if (c->sock != -1) {
456 if (shutdown(c->sock, SHUT_WR) < 0)
d7ac5f18 457 debug2("channel %d: chan_shutdown_write: "
f048f3e2 458 "shutdown() failed for fd%d: %.100s",
7e7327a1 459 c->self, c->sock, strerror(errno));
460 } else {
489aa2e9 461 if (channel_close_fd(&c->wfd) < 0)
bbe88b6d 462 logit("channel %d: chan_shutdown_write: "
f048f3e2 463 "close() failed for fd%d: %.100s",
7e7327a1 464 c->self, c->wfd, strerror(errno));
7e7327a1 465 }
466}
467static void
468chan_shutdown_read(Channel *c)
469{
470 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
471 return;
d7ac5f18 472 debug2("channel %d: close_read", c->self);
7e7327a1 473 if (c->sock != -1) {
2b87da3b 474 /*
47670e77 475 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
476 * write side has been closed already. (bug on Linux)
f49df8e9 477 * HP-UX may return ENOTCONN also.
47670e77 478 */
479 if (shutdown(c->sock, SHUT_RD) < 0
f49df8e9 480 && errno != ENOTCONN)
f048f3e2 481 error("channel %d: chan_shutdown_read: "
482 "shutdown() failed for fd%d [i%d o%d]: %.100s",
483 c->self, c->sock, c->istate, c->ostate,
1b73841d 484 strerror(errno));
7e7327a1 485 } else {
489aa2e9 486 if (channel_close_fd(&c->rfd) < 0)
bbe88b6d 487 logit("channel %d: chan_shutdown_read: "
f048f3e2 488 "close() failed for fd%d: %.100s",
7e7327a1 489 c->self, c->rfd, strerror(errno));
7e7327a1 490 }
8efc0c15 491}
This page took 1.3683 seconds and 5 git commands to generate.