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