Annotation of src/usr.bin/ssh/nchan.c, Revision 1.74
1.74 ! djm 1: /* $OpenBSD: nchan.c,v 1.73 2021/05/19 01:24:05 djm Exp $ */
1.7 markus 2: /*
1.43 markus 3: * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
1.7 markus 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.
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:
1.54 stevesk 26: #include <sys/types.h>
27: #include <sys/socket.h>
1.58 djm 28: #include <sys/queue.h>
1.55 stevesk 29:
30: #include <errno.h>
1.56 stevesk 31: #include <string.h>
1.57 deraadt 32: #include <stdarg.h>
1.1 markus 33:
1.22 markus 34: #include "ssh2.h"
1.66 djm 35: #include "sshbuf.h"
36: #include "ssherr.h"
1.3 markus 37: #include "packet.h"
1.1 markus 38: #include "channels.h"
1.13 markus 39: #include "compat.h"
1.22 markus 40: #include "log.h"
1.3 markus 41:
1.28 markus 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:
1.51 deraadt 51: * CLOSE ->
52: * <- CLOSE_CONFIRM
1.28 markus 53: *
54: * 1.5: uses variations of:
1.51 deraadt 55: * IEOF ->
56: * <- OCLOSE
57: * <- IEOF
58: * OCLOSE ->
59: * i.e. both sides have to close the channel
1.28 markus 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: */
67:
1.13 markus 68: /* functions manipulating channel states */
1.3 markus 69: /*
1.6 markus 70: * EVENTS update channel input/output states execute ACTIONS
1.3 markus 71: */
1.13 markus 72: /*
73: * ACTIONS: should never update the channel states
74: */
1.66 djm 75: static void chan_send_eof2(struct ssh *, Channel *);
76: static void chan_send_eow2(struct ssh *, Channel *);
1.13 markus 77:
78: /* helper */
1.66 djm 79: static void chan_shutdown_write(struct ssh *, Channel *);
80: static void chan_shutdown_read(struct ssh *, Channel *);
1.69 djm 81: static void chan_shutdown_extended_read(struct ssh *, Channel *);
1.13 markus 82:
1.74 ! djm 83: static const char * const ostates[] = {
! 84: "open", "drain", "wait_ieof", "closed",
! 85: };
! 86: static const char * const istates[] = {
! 87: "open", "drain", "wait_oclose", "closed",
! 88: };
1.37 markus 89:
90: static void
91: chan_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);
1.49 markus 95: debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
1.37 markus 96: istates[next]);
97: c->istate = next;
98: }
1.64 djm 99:
1.37 markus 100: static void
101: chan_set_ostate(Channel *c, u_int next)
102: {
103: if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
104: fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
1.49 markus 105: debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
1.37 markus 106: ostates[next]);
107: c->ostate = next;
108: }
109:
1.42 markus 110: void
1.66 djm 111: chan_read_failed(struct ssh *ssh, Channel *c)
1.6 markus 112: {
1.49 markus 113: debug2("channel %d: read failed", c->self);
1.6 markus 114: switch (c->istate) {
1.3 markus 115: case CHAN_INPUT_OPEN:
1.66 djm 116: chan_shutdown_read(ssh, c);
1.37 markus 117: chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
1.3 markus 118: break;
119: default:
1.28 markus 120: error("channel %d: chan_read_failed for istate %d",
1.13 markus 121: c->self, c->istate);
1.3 markus 122: break;
1.1 markus 123: }
124: }
1.64 djm 125:
1.42 markus 126: void
1.66 djm 127: chan_ibuf_empty(struct ssh *ssh, Channel *c)
1.6 markus 128: {
1.49 markus 129: debug2("channel %d: ibuf empty", c->self);
1.66 djm 130: if (sshbuf_len(c->input)) {
1.28 markus 131: error("channel %d: chan_ibuf_empty for non empty buffer",
1.13 markus 132: c->self);
1.3 markus 133: return;
134: }
1.6 markus 135: switch (c->istate) {
1.3 markus 136: case CHAN_INPUT_WAIT_DRAIN:
1.64 djm 137: if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
1.66 djm 138: chan_send_eof2(ssh, c);
1.64 djm 139: chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3 markus 140: break;
141: default:
1.28 markus 142: error("channel %d: chan_ibuf_empty for istate %d",
1.13 markus 143: c->self, c->istate);
1.3 markus 144: break;
1.1 markus 145: }
146: }
1.64 djm 147:
1.42 markus 148: void
1.66 djm 149: chan_obuf_empty(struct ssh *ssh, Channel *c)
1.6 markus 150: {
1.49 markus 151: debug2("channel %d: obuf empty", c->self);
1.66 djm 152: if (sshbuf_len(c->output)) {
1.28 markus 153: error("channel %d: chan_obuf_empty for non empty buffer",
1.13 markus 154: c->self);
1.3 markus 155: return;
156: }
1.6 markus 157: switch (c->ostate) {
1.3 markus 158: case CHAN_OUTPUT_WAIT_DRAIN:
1.66 djm 159: chan_shutdown_write(ssh, c);
1.37 markus 160: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3 markus 161: break;
162: default:
1.28 markus 163: error("channel %d: internal error: obuf_empty for ostate %d",
1.13 markus 164: c->self, c->ostate);
1.3 markus 165: break;
166: }
167: }
1.63 djm 168:
1.59 markus 169: void
1.66 djm 170: chan_rcvd_eow(struct ssh *ssh, Channel *c)
1.59 markus 171: {
172: debug2("channel %d: rcvd eow", c->self);
173: switch (c->istate) {
174: case CHAN_INPUT_OPEN:
1.66 djm 175: chan_shutdown_read(ssh, c);
1.59 markus 176: chan_set_istate(c, CHAN_INPUT_CLOSED);
177: break;
178: }
179: }
1.64 djm 180:
1.13 markus 181: static void
1.66 djm 182: chan_send_eof2(struct ssh *ssh, Channel *c)
1.6 markus 183: {
1.66 djm 184: int r;
185:
1.49 markus 186: debug2("channel %d: send eof", c->self);
1.13 markus 187: switch (c->istate) {
188: case CHAN_INPUT_WAIT_DRAIN:
1.67 djm 189: if (!c->have_remote_id)
1.71 djm 190: fatal_f("channel %d: no remote_id", c->self);
1.66 djm 191: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
192: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
193: (r = sshpkt_send(ssh)) != 0)
1.71 djm 194: fatal_fr(r, "send CHANNEL_EOF");
1.45 markus 195: c->flags |= CHAN_EOF_SENT;
1.13 markus 196: break;
197: default:
1.28 markus 198: error("channel %d: cannot send eof for istate %d",
1.13 markus 199: c->self, c->istate);
200: break;
201: }
1.1 markus 202: }
1.64 djm 203:
1.3 markus 204: static void
1.66 djm 205: chan_send_close2(struct ssh *ssh, Channel *c)
1.6 markus 206: {
1.66 djm 207: int r;
208:
1.49 markus 209: debug2("channel %d: send close", c->self);
1.13 markus 210: if (c->ostate != CHAN_OUTPUT_CLOSED ||
211: c->istate != CHAN_INPUT_CLOSED) {
1.28 markus 212: error("channel %d: cannot send close for istate/ostate %d/%d",
1.13 markus 213: c->self, c->istate, c->ostate);
214: } else if (c->flags & CHAN_CLOSE_SENT) {
1.28 markus 215: error("channel %d: already sent close", c->self);
1.13 markus 216: } else {
1.67 djm 217: if (!c->have_remote_id)
1.71 djm 218: fatal_f("channel %d: no remote_id", c->self);
1.66 djm 219: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
220: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
221: (r = sshpkt_send(ssh)) != 0)
1.71 djm 222: fatal_fr(r, "send CHANNEL_EOF");
1.13 markus 223: c->flags |= CHAN_CLOSE_SENT;
224: }
1.59 markus 225: }
1.64 djm 226:
1.59 markus 227: static void
1.66 djm 228: chan_send_eow2(struct ssh *ssh, Channel *c)
1.59 markus 229: {
1.66 djm 230: int r;
231:
1.59 markus 232: debug2("channel %d: send eow", c->self);
233: if (c->ostate == CHAN_OUTPUT_CLOSED) {
234: error("channel %d: must not sent eow on closed output",
235: c->self);
236: return;
237: }
1.72 djm 238: if (!(ssh->compat & SSH_NEW_OPENSSH))
1.61 markus 239: return;
1.67 djm 240: if (!c->have_remote_id)
1.71 djm 241: fatal_f("channel %d: no remote_id", c->self);
1.66 djm 242: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
243: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
244: (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
245: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
246: (r = sshpkt_send(ssh)) != 0)
1.71 djm 247: fatal_fr(r, "send CHANNEL_EOF");
1.1 markus 248: }
1.23 markus 249:
250: /* shared */
251:
1.24 markus 252: void
1.66 djm 253: chan_rcvd_ieof(struct ssh *ssh, Channel *c)
1.42 markus 254: {
1.64 djm 255: debug2("channel %d: rcvd eof", c->self);
256: c->flags |= CHAN_EOF_RCVD;
257: if (c->ostate == CHAN_OUTPUT_OPEN)
258: chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.44 markus 259: if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
1.66 djm 260: sshbuf_len(c->output) == 0 &&
1.45 markus 261: !CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.66 djm 262: chan_obuf_empty(ssh, c);
1.42 markus 263: }
1.64 djm 264:
1.42 markus 265: void
1.66 djm 266: chan_rcvd_oclose(struct ssh *ssh, Channel *c)
1.42 markus 267: {
1.64 djm 268: debug2("channel %d: rcvd close", c->self);
269: if (!(c->flags & CHAN_LOCAL)) {
270: if (c->flags & CHAN_CLOSE_RCVD)
271: error("channel %d: protocol error: close rcvd twice",
272: c->self);
273: c->flags |= CHAN_CLOSE_RCVD;
274: }
275: if (c->type == SSH_CHANNEL_LARVAL) {
276: /* tear down larval channels immediately */
277: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
278: chan_set_istate(c, CHAN_INPUT_CLOSED);
279: return;
280: }
281: switch (c->ostate) {
282: case CHAN_OUTPUT_OPEN:
283: /*
284: * wait until a data from the channel is consumed if a CLOSE
285: * is received
286: */
287: chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
288: break;
289: }
290: switch (c->istate) {
291: case CHAN_INPUT_OPEN:
1.66 djm 292: chan_shutdown_read(ssh, c);
1.69 djm 293: chan_shutdown_extended_read(ssh, c);
1.64 djm 294: chan_set_istate(c, CHAN_INPUT_CLOSED);
295: break;
296: case CHAN_INPUT_WAIT_DRAIN:
297: if (!(c->flags & CHAN_LOCAL))
1.66 djm 298: chan_send_eof2(ssh, c);
1.69 djm 299: chan_shutdown_extended_read(ssh, c);
1.64 djm 300: chan_set_istate(c, CHAN_INPUT_CLOSED);
301: break;
302: }
1.42 markus 303: }
1.64 djm 304:
1.42 markus 305: void
1.66 djm 306: chan_write_failed(struct ssh *ssh, Channel *c)
1.42 markus 307: {
1.64 djm 308: debug2("channel %d: write failed", c->self);
309: switch (c->ostate) {
310: case CHAN_OUTPUT_OPEN:
311: case CHAN_OUTPUT_WAIT_DRAIN:
1.66 djm 312: chan_shutdown_write(ssh, c);
1.64 djm 313: if (strcmp(c->ctype, "session") == 0)
1.66 djm 314: chan_send_eow2(ssh, c);
1.64 djm 315: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
316: break;
317: default:
318: error("channel %d: chan_write_failed for ostate %d",
319: c->self, c->ostate);
320: break;
321: }
1.42 markus 322: }
323:
324: void
1.66 djm 325: chan_mark_dead(struct ssh *ssh, Channel *c)
1.24 markus 326: {
1.26 markus 327: c->type = SSH_CHANNEL_ZOMBIE;
1.24 markus 328: }
329:
1.23 markus 330: int
1.66 djm 331: chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
1.6 markus 332: {
1.26 markus 333: if (c->type == SSH_CHANNEL_ZOMBIE) {
1.49 markus 334: debug2("channel %d: zombie", c->self);
1.24 markus 335: return 1;
1.26 markus 336: }
1.23 markus 337: if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
338: return 0;
1.72 djm 339: if ((ssh->compat & SSH_BUG_EXTEOF) &&
1.45 markus 340: c->extended_usage == CHAN_EXTENDED_WRITE &&
341: c->efd != -1 &&
1.66 djm 342: sshbuf_len(c->extended) > 0) {
343: debug2("channel %d: active efd: %d len %zu",
344: c->self, c->efd, sshbuf_len(c->extended));
1.45 markus 345: return 0;
346: }
1.63 djm 347: if (c->flags & CHAN_LOCAL) {
348: debug2("channel %d: is dead (local)", c->self);
349: return 1;
350: }
1.45 markus 351: if (!(c->flags & CHAN_CLOSE_SENT)) {
1.50 avsm 352: if (do_send) {
1.66 djm 353: chan_send_close2(ssh, c);
1.45 markus 354: } else {
355: /* channel would be dead if we sent a close */
356: if (c->flags & CHAN_CLOSE_RCVD) {
1.49 markus 357: debug2("channel %d: almost dead",
1.45 markus 358: c->self);
359: return 1;
1.32 markus 360: }
1.13 markus 361: }
1.45 markus 362: }
363: if ((c->flags & CHAN_CLOSE_SENT) &&
364: (c->flags & CHAN_CLOSE_RCVD)) {
1.49 markus 365: debug2("channel %d: is dead", c->self);
1.45 markus 366: return 1;
1.1 markus 367: }
1.23 markus 368: return 0;
1.13 markus 369: }
370:
371: /* helper */
372: static void
1.66 djm 373: chan_shutdown_write(struct ssh *ssh, Channel *c)
1.13 markus 374: {
1.66 djm 375: sshbuf_reset(c->output);
1.64 djm 376: if (c->type == SSH_CHANNEL_LARVAL)
1.13 markus 377: return;
378: /* shutdown failure is allowed if write failed already */
1.71 djm 379: debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
380: c->self, c->istate, c->ostate, c->sock, c->wfd, c->efd,
1.68 djm 381: channel_format_extended_usage(c));
1.13 markus 382: if (c->sock != -1) {
1.70 deraadt 383: if (shutdown(c->sock, SHUT_WR) == -1) {
1.71 djm 384: debug2_f("channel %d: shutdown() failed for "
385: "fd %d [i%d o%d]: %.100s", c->self, c->sock,
386: c->istate, c->ostate, strerror(errno));
1.68 djm 387: }
1.13 markus 388: } else {
1.73 djm 389: if (channel_close_fd(ssh, c, &c->wfd) < 0) {
1.71 djm 390: logit_f("channel %d: close() failed for "
391: "fd %d [i%d o%d]: %.100s", c->self, c->wfd,
392: c->istate, c->ostate, strerror(errno));
1.68 djm 393: }
1.13 markus 394: }
395: }
1.64 djm 396:
1.13 markus 397: static void
1.66 djm 398: chan_shutdown_read(struct ssh *ssh, Channel *c)
1.13 markus 399: {
1.64 djm 400: if (c->type == SSH_CHANNEL_LARVAL)
1.13 markus 401: return;
1.71 djm 402: debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
403: c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
1.68 djm 404: channel_format_extended_usage(c));
1.13 markus 405: if (c->sock != -1) {
1.70 deraadt 406: if (shutdown(c->sock, SHUT_RD) == -1) {
1.71 djm 407: error_f("channel %d: shutdown() failed for "
408: "fd %d [i%d o%d]: %.100s", c->self, c->sock,
409: c->istate, c->ostate, strerror(errno));
1.68 djm 410: }
1.13 markus 411: } else {
1.73 djm 412: if (channel_close_fd(ssh, c, &c->rfd) < 0) {
1.71 djm 413: logit_f("channel %d: close() failed for "
414: "fd %d [i%d o%d]: %.100s", c->self, c->rfd,
415: c->istate, c->ostate, strerror(errno));
1.68 djm 416: }
1.69 djm 417: }
418: }
419:
420: static void
421: chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
422: {
423: if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
424: return;
425: if (c->extended_usage != CHAN_EXTENDED_READ &&
426: c->extended_usage != CHAN_EXTENDED_IGNORE)
427: return;
1.71 djm 428: debug_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
429: c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
1.69 djm 430: channel_format_extended_usage(c));
1.73 djm 431: if (channel_close_fd(ssh, c, &c->efd) < 0) {
1.71 djm 432: logit_f("channel %d: close() failed for "
433: "extended fd %d [i%d o%d]: %.100s", c->self, c->efd,
434: c->istate, c->ostate, strerror(errno));
1.13 markus 435: }
1.1 markus 436: }