Annotation of src/usr.bin/ssh/nchan.c, Revision 1.69
1.69 ! djm 1: /* $OpenBSD: nchan.c,v 1.68 2018/10/04 00:10:11 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.66 djm 83: static const char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
84: static const char *istates[] = { "open", "drain", "wait_oclose", "closed" };
1.37 markus 85:
86: static void
87: chan_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);
1.49 markus 91: debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
1.37 markus 92: istates[next]);
93: c->istate = next;
94: }
1.64 djm 95:
1.37 markus 96: static void
97: chan_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);
1.49 markus 101: debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
1.37 markus 102: ostates[next]);
103: c->ostate = next;
104: }
105:
1.42 markus 106: void
1.66 djm 107: chan_read_failed(struct ssh *ssh, Channel *c)
1.6 markus 108: {
1.49 markus 109: debug2("channel %d: read failed", c->self);
1.6 markus 110: switch (c->istate) {
1.3 markus 111: case CHAN_INPUT_OPEN:
1.66 djm 112: chan_shutdown_read(ssh, c);
1.37 markus 113: chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
1.3 markus 114: break;
115: default:
1.28 markus 116: error("channel %d: chan_read_failed for istate %d",
1.13 markus 117: c->self, c->istate);
1.3 markus 118: break;
1.1 markus 119: }
120: }
1.64 djm 121:
1.42 markus 122: void
1.66 djm 123: chan_ibuf_empty(struct ssh *ssh, Channel *c)
1.6 markus 124: {
1.49 markus 125: debug2("channel %d: ibuf empty", c->self);
1.66 djm 126: if (sshbuf_len(c->input)) {
1.28 markus 127: error("channel %d: chan_ibuf_empty for non empty buffer",
1.13 markus 128: c->self);
1.3 markus 129: return;
130: }
1.6 markus 131: switch (c->istate) {
1.3 markus 132: case CHAN_INPUT_WAIT_DRAIN:
1.64 djm 133: if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
1.66 djm 134: chan_send_eof2(ssh, c);
1.64 djm 135: chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3 markus 136: break;
137: default:
1.28 markus 138: error("channel %d: chan_ibuf_empty for istate %d",
1.13 markus 139: c->self, c->istate);
1.3 markus 140: break;
1.1 markus 141: }
142: }
1.64 djm 143:
1.42 markus 144: void
1.66 djm 145: chan_obuf_empty(struct ssh *ssh, Channel *c)
1.6 markus 146: {
1.49 markus 147: debug2("channel %d: obuf empty", c->self);
1.66 djm 148: if (sshbuf_len(c->output)) {
1.28 markus 149: error("channel %d: chan_obuf_empty for non empty buffer",
1.13 markus 150: c->self);
1.3 markus 151: return;
152: }
1.6 markus 153: switch (c->ostate) {
1.3 markus 154: case CHAN_OUTPUT_WAIT_DRAIN:
1.66 djm 155: chan_shutdown_write(ssh, c);
1.37 markus 156: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3 markus 157: break;
158: default:
1.28 markus 159: error("channel %d: internal error: obuf_empty for ostate %d",
1.13 markus 160: c->self, c->ostate);
1.3 markus 161: break;
162: }
163: }
1.63 djm 164:
1.59 markus 165: void
1.66 djm 166: chan_rcvd_eow(struct ssh *ssh, Channel *c)
1.59 markus 167: {
168: debug2("channel %d: rcvd eow", c->self);
169: switch (c->istate) {
170: case CHAN_INPUT_OPEN:
1.66 djm 171: chan_shutdown_read(ssh, c);
1.59 markus 172: chan_set_istate(c, CHAN_INPUT_CLOSED);
173: break;
174: }
175: }
1.64 djm 176:
1.13 markus 177: static void
1.66 djm 178: chan_send_eof2(struct ssh *ssh, Channel *c)
1.6 markus 179: {
1.66 djm 180: int r;
181:
1.49 markus 182: debug2("channel %d: send eof", c->self);
1.13 markus 183: switch (c->istate) {
184: case CHAN_INPUT_WAIT_DRAIN:
1.67 djm 185: if (!c->have_remote_id)
186: fatal("%s: channel %d: no remote_id",
187: __func__, c->self);
1.66 djm 188: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
189: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
190: (r = sshpkt_send(ssh)) != 0)
191: fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
1.45 markus 192: c->flags |= CHAN_EOF_SENT;
1.13 markus 193: break;
194: default:
1.28 markus 195: error("channel %d: cannot send eof for istate %d",
1.13 markus 196: c->self, c->istate);
197: break;
198: }
1.1 markus 199: }
1.64 djm 200:
1.3 markus 201: static void
1.66 djm 202: chan_send_close2(struct ssh *ssh, Channel *c)
1.6 markus 203: {
1.66 djm 204: int r;
205:
1.49 markus 206: debug2("channel %d: send close", c->self);
1.13 markus 207: if (c->ostate != CHAN_OUTPUT_CLOSED ||
208: c->istate != CHAN_INPUT_CLOSED) {
1.28 markus 209: error("channel %d: cannot send close for istate/ostate %d/%d",
1.13 markus 210: c->self, c->istate, c->ostate);
211: } else if (c->flags & CHAN_CLOSE_SENT) {
1.28 markus 212: error("channel %d: already sent close", c->self);
1.13 markus 213: } else {
1.67 djm 214: if (!c->have_remote_id)
215: fatal("%s: channel %d: no remote_id",
216: __func__, c->self);
1.66 djm 217: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
218: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
219: (r = sshpkt_send(ssh)) != 0)
220: fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
1.13 markus 221: c->flags |= CHAN_CLOSE_SENT;
222: }
1.59 markus 223: }
1.64 djm 224:
1.59 markus 225: static void
1.66 djm 226: chan_send_eow2(struct ssh *ssh, Channel *c)
1.59 markus 227: {
1.66 djm 228: int r;
229:
1.59 markus 230: debug2("channel %d: send eow", c->self);
231: if (c->ostate == CHAN_OUTPUT_CLOSED) {
232: error("channel %d: must not sent eow on closed output",
233: c->self);
234: return;
235: }
1.61 markus 236: if (!(datafellows & SSH_NEW_OPENSSH))
237: return;
1.67 djm 238: if (!c->have_remote_id)
239: fatal("%s: channel %d: no remote_id", __func__, c->self);
1.66 djm 240: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
241: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
242: (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
243: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
244: (r = sshpkt_send(ssh)) != 0)
245: fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
1.1 markus 246: }
1.23 markus 247:
248: /* shared */
249:
1.24 markus 250: void
1.66 djm 251: chan_rcvd_ieof(struct ssh *ssh, Channel *c)
1.42 markus 252: {
1.64 djm 253: debug2("channel %d: rcvd eof", c->self);
254: c->flags |= CHAN_EOF_RCVD;
255: if (c->ostate == CHAN_OUTPUT_OPEN)
256: chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.44 markus 257: if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
1.66 djm 258: sshbuf_len(c->output) == 0 &&
1.45 markus 259: !CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.66 djm 260: chan_obuf_empty(ssh, c);
1.42 markus 261: }
1.64 djm 262:
1.42 markus 263: void
1.66 djm 264: chan_rcvd_oclose(struct ssh *ssh, Channel *c)
1.42 markus 265: {
1.64 djm 266: debug2("channel %d: rcvd close", c->self);
267: if (!(c->flags & CHAN_LOCAL)) {
268: if (c->flags & CHAN_CLOSE_RCVD)
269: error("channel %d: protocol error: close rcvd twice",
270: c->self);
271: c->flags |= CHAN_CLOSE_RCVD;
272: }
273: if (c->type == SSH_CHANNEL_LARVAL) {
274: /* tear down larval channels immediately */
275: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
276: chan_set_istate(c, CHAN_INPUT_CLOSED);
277: return;
278: }
279: switch (c->ostate) {
280: case CHAN_OUTPUT_OPEN:
281: /*
282: * wait until a data from the channel is consumed if a CLOSE
283: * is received
284: */
285: chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
286: break;
287: }
288: switch (c->istate) {
289: case CHAN_INPUT_OPEN:
1.66 djm 290: chan_shutdown_read(ssh, c);
1.69 ! djm 291: chan_shutdown_extended_read(ssh, c);
1.64 djm 292: chan_set_istate(c, CHAN_INPUT_CLOSED);
293: break;
294: case CHAN_INPUT_WAIT_DRAIN:
295: if (!(c->flags & CHAN_LOCAL))
1.66 djm 296: chan_send_eof2(ssh, c);
1.69 ! djm 297: chan_shutdown_extended_read(ssh, c);
1.64 djm 298: chan_set_istate(c, CHAN_INPUT_CLOSED);
299: break;
300: }
1.42 markus 301: }
1.64 djm 302:
1.42 markus 303: void
1.66 djm 304: chan_write_failed(struct ssh *ssh, Channel *c)
1.42 markus 305: {
1.64 djm 306: debug2("channel %d: write failed", c->self);
307: switch (c->ostate) {
308: case CHAN_OUTPUT_OPEN:
309: case CHAN_OUTPUT_WAIT_DRAIN:
1.66 djm 310: chan_shutdown_write(ssh, c);
1.64 djm 311: if (strcmp(c->ctype, "session") == 0)
1.66 djm 312: chan_send_eow2(ssh, c);
1.64 djm 313: chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
314: break;
315: default:
316: error("channel %d: chan_write_failed for ostate %d",
317: c->self, c->ostate);
318: break;
319: }
1.42 markus 320: }
321:
322: void
1.66 djm 323: chan_mark_dead(struct ssh *ssh, Channel *c)
1.24 markus 324: {
1.26 markus 325: c->type = SSH_CHANNEL_ZOMBIE;
1.24 markus 326: }
327:
1.23 markus 328: int
1.66 djm 329: chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
1.6 markus 330: {
1.26 markus 331: if (c->type == SSH_CHANNEL_ZOMBIE) {
1.49 markus 332: debug2("channel %d: zombie", c->self);
1.24 markus 333: return 1;
1.26 markus 334: }
1.23 markus 335: if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
336: return 0;
1.45 markus 337: if ((datafellows & SSH_BUG_EXTEOF) &&
338: c->extended_usage == CHAN_EXTENDED_WRITE &&
339: c->efd != -1 &&
1.66 djm 340: sshbuf_len(c->extended) > 0) {
341: debug2("channel %d: active efd: %d len %zu",
342: c->self, c->efd, sshbuf_len(c->extended));
1.45 markus 343: return 0;
344: }
1.63 djm 345: if (c->flags & CHAN_LOCAL) {
346: debug2("channel %d: is dead (local)", c->self);
347: return 1;
348: }
1.45 markus 349: if (!(c->flags & CHAN_CLOSE_SENT)) {
1.50 avsm 350: if (do_send) {
1.66 djm 351: chan_send_close2(ssh, c);
1.45 markus 352: } else {
353: /* channel would be dead if we sent a close */
354: if (c->flags & CHAN_CLOSE_RCVD) {
1.49 markus 355: debug2("channel %d: almost dead",
1.45 markus 356: c->self);
357: return 1;
1.32 markus 358: }
1.13 markus 359: }
1.45 markus 360: }
361: if ((c->flags & CHAN_CLOSE_SENT) &&
362: (c->flags & CHAN_CLOSE_RCVD)) {
1.49 markus 363: debug2("channel %d: is dead", c->self);
1.45 markus 364: return 1;
1.1 markus 365: }
1.23 markus 366: return 0;
1.13 markus 367: }
368:
369: /* helper */
370: static void
1.66 djm 371: chan_shutdown_write(struct ssh *ssh, Channel *c)
1.13 markus 372: {
1.66 djm 373: sshbuf_reset(c->output);
1.64 djm 374: if (c->type == SSH_CHANNEL_LARVAL)
1.13 markus 375: return;
376: /* shutdown failure is allowed if write failed already */
1.68 djm 377: debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
378: c->self, __func__, c->istate, c->ostate, c->sock, c->wfd, c->efd,
379: channel_format_extended_usage(c));
1.13 markus 380: if (c->sock != -1) {
1.68 djm 381: if (shutdown(c->sock, SHUT_WR) < 0) {
382: debug2("channel %d: %s: shutdown() failed for "
383: "fd %d [i%d o%d]: %.100s", c->self, __func__,
384: c->sock, c->istate, c->ostate,
385: strerror(errno));
386: }
1.13 markus 387: } else {
1.68 djm 388: if (channel_close_fd(ssh, &c->wfd) < 0) {
389: logit("channel %d: %s: close() failed for "
390: "fd %d [i%d o%d]: %.100s",
391: c->self, __func__, c->wfd, c->istate, c->ostate,
392: strerror(errno));
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.68 djm 402: debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
403: c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
404: channel_format_extended_usage(c));
1.13 markus 405: if (c->sock != -1) {
1.68 djm 406: if (shutdown(c->sock, SHUT_RD) < 0) {
407: error("channel %d: %s: shutdown() failed for "
408: "fd %d [i%d o%d]: %.100s",
409: c->self, __func__, c->sock, c->istate, c->ostate,
1.28 markus 410: strerror(errno));
1.68 djm 411: }
1.13 markus 412: } else {
1.68 djm 413: if (channel_close_fd(ssh, &c->rfd) < 0) {
414: logit("channel %d: %s: close() failed for "
415: "fd %d [i%d o%d]: %.100s",
416: c->self, __func__, c->rfd, c->istate, c->ostate,
417: strerror(errno));
418: }
1.69 ! djm 419: }
! 420: }
! 421:
! 422: static void
! 423: chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
! 424: {
! 425: if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
! 426: return;
! 427: if (c->extended_usage != CHAN_EXTENDED_READ &&
! 428: c->extended_usage != CHAN_EXTENDED_IGNORE)
! 429: return;
! 430: debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
! 431: c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
! 432: channel_format_extended_usage(c));
! 433: if (channel_close_fd(ssh, &c->efd) < 0) {
! 434: logit("channel %d: %s: close() failed for "
! 435: "extended fd %d [i%d o%d]: %.100s",
! 436: c->self, __func__, c->efd, c->istate, c->ostate,
! 437: strerror(errno));
1.13 markus 438: }
1.1 markus 439: }