[BACK]Return to nchan.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/nchan.c, Revision 1.64

1.64    ! djm         1: /* $OpenBSD: nchan.c,v 1.63 2010/01/26 01:28:35 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 "ssh1.h"
                     35: #include "ssh2.h"
1.1       markus     36: #include "buffer.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.29      itojun     75: static void    chan_send_eof2(Channel *);
1.59      markus     76: static void    chan_send_eow2(Channel *);
1.13      markus     77:
                     78: /* helper */
1.29      itojun     79: static void    chan_shutdown_write(Channel *);
                     80: static void    chan_shutdown_read(Channel *);
1.13      markus     81:
1.37      markus     82: static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
                     83: static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
                     84:
                     85: static void
                     86: chan_set_istate(Channel *c, u_int next)
                     87: {
                     88:        if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
                     89:                fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
1.49      markus     90:        debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
1.37      markus     91:            istates[next]);
                     92:        c->istate = next;
                     93: }
1.64    ! djm        94:
1.37      markus     95: static void
                     96: chan_set_ostate(Channel *c, u_int next)
                     97: {
                     98:        if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
                     99:                fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
1.49      markus    100:        debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
1.37      markus    101:            ostates[next]);
                    102:        c->ostate = next;
                    103: }
                    104:
1.42      markus    105: void
                    106: chan_read_failed(Channel *c)
1.6       markus    107: {
1.49      markus    108:        debug2("channel %d: read failed", c->self);
1.6       markus    109:        switch (c->istate) {
1.3       markus    110:        case CHAN_INPUT_OPEN:
                    111:                chan_shutdown_read(c);
1.37      markus    112:                chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
1.3       markus    113:                break;
                    114:        default:
1.28      markus    115:                error("channel %d: chan_read_failed for istate %d",
1.13      markus    116:                    c->self, c->istate);
1.3       markus    117:                break;
1.1       markus    118:        }
                    119: }
1.64    ! djm       120:
1.42      markus    121: void
                    122: chan_ibuf_empty(Channel *c)
1.6       markus    123: {
1.49      markus    124:        debug2("channel %d: ibuf empty", c->self);
1.6       markus    125:        if (buffer_len(&c->input)) {
1.28      markus    126:                error("channel %d: chan_ibuf_empty for non empty buffer",
1.13      markus    127:                    c->self);
1.3       markus    128:                return;
                    129:        }
1.6       markus    130:        switch (c->istate) {
1.3       markus    131:        case CHAN_INPUT_WAIT_DRAIN:
1.64    ! djm       132:                if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
        !           133:                        chan_send_eof2(c);
        !           134:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3       markus    135:                break;
                    136:        default:
1.28      markus    137:                error("channel %d: chan_ibuf_empty for istate %d",
1.13      markus    138:                    c->self, c->istate);
1.3       markus    139:                break;
1.1       markus    140:        }
                    141: }
1.64    ! djm       142:
1.42      markus    143: void
                    144: chan_obuf_empty(Channel *c)
1.6       markus    145: {
1.49      markus    146:        debug2("channel %d: obuf empty", c->self);
1.6       markus    147:        if (buffer_len(&c->output)) {
1.28      markus    148:                error("channel %d: chan_obuf_empty for non empty buffer",
1.13      markus    149:                    c->self);
1.3       markus    150:                return;
                    151:        }
1.6       markus    152:        switch (c->ostate) {
1.3       markus    153:        case CHAN_OUTPUT_WAIT_DRAIN:
1.38      markus    154:                chan_shutdown_write(c);
1.37      markus    155:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3       markus    156:                break;
                    157:        default:
1.28      markus    158:                error("channel %d: internal error: obuf_empty for ostate %d",
1.13      markus    159:                    c->self, c->ostate);
1.3       markus    160:                break;
                    161:        }
                    162: }
1.63      djm       163:
1.59      markus    164: void
                    165: chan_rcvd_eow(Channel *c)
                    166: {
                    167:        debug2("channel %d: rcvd eow", c->self);
                    168:        switch (c->istate) {
                    169:        case CHAN_INPUT_OPEN:
                    170:                chan_shutdown_read(c);
                    171:                chan_set_istate(c, CHAN_INPUT_CLOSED);
                    172:                break;
                    173:        }
                    174: }
1.64    ! djm       175:
1.13      markus    176: static void
                    177: chan_send_eof2(Channel *c)
1.6       markus    178: {
1.49      markus    179:        debug2("channel %d: send eof", c->self);
1.13      markus    180:        switch (c->istate) {
                    181:        case CHAN_INPUT_WAIT_DRAIN:
                    182:                packet_start(SSH2_MSG_CHANNEL_EOF);
                    183:                packet_put_int(c->remote_id);
                    184:                packet_send();
1.45      markus    185:                c->flags |= CHAN_EOF_SENT;
1.13      markus    186:                break;
                    187:        default:
1.28      markus    188:                error("channel %d: cannot send eof for istate %d",
1.13      markus    189:                    c->self, c->istate);
                    190:                break;
                    191:        }
1.1       markus    192: }
1.64    ! djm       193:
1.3       markus    194: static void
1.13      markus    195: chan_send_close2(Channel *c)
1.6       markus    196: {
1.49      markus    197:        debug2("channel %d: send close", c->self);
1.13      markus    198:        if (c->ostate != CHAN_OUTPUT_CLOSED ||
                    199:            c->istate != CHAN_INPUT_CLOSED) {
1.28      markus    200:                error("channel %d: cannot send close for istate/ostate %d/%d",
1.13      markus    201:                    c->self, c->istate, c->ostate);
                    202:        } else if (c->flags & CHAN_CLOSE_SENT) {
1.28      markus    203:                error("channel %d: already sent close", c->self);
1.13      markus    204:        } else {
                    205:                packet_start(SSH2_MSG_CHANNEL_CLOSE);
                    206:                packet_put_int(c->remote_id);
                    207:                packet_send();
                    208:                c->flags |= CHAN_CLOSE_SENT;
                    209:        }
1.59      markus    210: }
1.64    ! djm       211:
1.59      markus    212: static void
                    213: chan_send_eow2(Channel *c)
                    214: {
                    215:        debug2("channel %d: send eow", c->self);
                    216:        if (c->ostate == CHAN_OUTPUT_CLOSED) {
                    217:                error("channel %d: must not sent eow on closed output",
                    218:                    c->self);
                    219:                return;
                    220:        }
1.61      markus    221:        if (!(datafellows & SSH_NEW_OPENSSH))
                    222:                return;
1.59      markus    223:        packet_start(SSH2_MSG_CHANNEL_REQUEST);
                    224:        packet_put_int(c->remote_id);
                    225:        packet_put_cstring("eow@openssh.com");
                    226:        packet_put_char(0);
                    227:        packet_send();
1.1       markus    228: }
1.23      markus    229:
                    230: /* shared */
                    231:
1.24      markus    232: void
1.42      markus    233: chan_rcvd_ieof(Channel *c)
                    234: {
1.64    ! djm       235:        debug2("channel %d: rcvd eof", c->self);
        !           236:        c->flags |= CHAN_EOF_RCVD;
        !           237:        if (c->ostate == CHAN_OUTPUT_OPEN)
        !           238:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.44      markus    239:        if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
1.47      deraadt   240:            buffer_len(&c->output) == 0 &&
1.45      markus    241:            !CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.44      markus    242:                chan_obuf_empty(c);
1.42      markus    243: }
1.64    ! djm       244:
1.42      markus    245: void
                    246: chan_rcvd_oclose(Channel *c)
                    247: {
1.64    ! djm       248:        debug2("channel %d: rcvd close", c->self);
        !           249:        if (!(c->flags & CHAN_LOCAL)) {
        !           250:                if (c->flags & CHAN_CLOSE_RCVD)
        !           251:                        error("channel %d: protocol error: close rcvd twice",
        !           252:                            c->self);
        !           253:                c->flags |= CHAN_CLOSE_RCVD;
        !           254:        }
        !           255:        if (c->type == SSH_CHANNEL_LARVAL) {
        !           256:                /* tear down larval channels immediately */
        !           257:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
        !           258:                chan_set_istate(c, CHAN_INPUT_CLOSED);
        !           259:                return;
        !           260:        }
        !           261:        switch (c->ostate) {
        !           262:        case CHAN_OUTPUT_OPEN:
        !           263:                /*
        !           264:                 * wait until a data from the channel is consumed if a CLOSE
        !           265:                 * is received
        !           266:                 */
        !           267:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
        !           268:                break;
        !           269:        }
        !           270:        switch (c->istate) {
        !           271:        case CHAN_INPUT_OPEN:
        !           272:                chan_shutdown_read(c);
        !           273:                chan_set_istate(c, CHAN_INPUT_CLOSED);
        !           274:                break;
        !           275:        case CHAN_INPUT_WAIT_DRAIN:
        !           276:                if (!(c->flags & CHAN_LOCAL))
        !           277:                        chan_send_eof2(c);
        !           278:                chan_set_istate(c, CHAN_INPUT_CLOSED);
        !           279:                break;
        !           280:        }
1.42      markus    281: }
1.64    ! djm       282:
1.42      markus    283: void
                    284: chan_write_failed(Channel *c)
                    285: {
1.64    ! djm       286:        debug2("channel %d: write failed", c->self);
        !           287:        switch (c->ostate) {
        !           288:        case CHAN_OUTPUT_OPEN:
        !           289:        case CHAN_OUTPUT_WAIT_DRAIN:
        !           290:                chan_shutdown_write(c);
        !           291:                if (strcmp(c->ctype, "session") == 0)
        !           292:                        chan_send_eow2(c);
        !           293:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
        !           294:                break;
        !           295:        default:
        !           296:                error("channel %d: chan_write_failed for ostate %d",
        !           297:                    c->self, c->ostate);
        !           298:                break;
        !           299:        }
1.42      markus    300: }
                    301:
                    302: void
1.24      markus    303: chan_mark_dead(Channel *c)
                    304: {
1.26      markus    305:        c->type = SSH_CHANNEL_ZOMBIE;
1.24      markus    306: }
                    307:
1.23      markus    308: int
1.50      avsm      309: chan_is_dead(Channel *c, int do_send)
1.6       markus    310: {
1.26      markus    311:        if (c->type == SSH_CHANNEL_ZOMBIE) {
1.49      markus    312:                debug2("channel %d: zombie", c->self);
1.24      markus    313:                return 1;
1.26      markus    314:        }
1.23      markus    315:        if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
                    316:                return 0;
1.45      markus    317:        if ((datafellows & SSH_BUG_EXTEOF) &&
                    318:            c->extended_usage == CHAN_EXTENDED_WRITE &&
                    319:            c->efd != -1 &&
                    320:            buffer_len(&c->extended) > 0) {
1.46      markus    321:                debug2("channel %d: active efd: %d len %d",
                    322:                    c->self, c->efd, buffer_len(&c->extended));
1.45      markus    323:                return 0;
                    324:        }
1.63      djm       325:        if (c->flags & CHAN_LOCAL) {
                    326:                debug2("channel %d: is dead (local)", c->self);
                    327:                return 1;
                    328:        }
1.45      markus    329:        if (!(c->flags & CHAN_CLOSE_SENT)) {
1.50      avsm      330:                if (do_send) {
1.45      markus    331:                        chan_send_close2(c);
                    332:                } else {
                    333:                        /* channel would be dead if we sent a close */
                    334:                        if (c->flags & CHAN_CLOSE_RCVD) {
1.49      markus    335:                                debug2("channel %d: almost dead",
1.45      markus    336:                                    c->self);
                    337:                                return 1;
1.32      markus    338:                        }
1.13      markus    339:                }
1.45      markus    340:        }
                    341:        if ((c->flags & CHAN_CLOSE_SENT) &&
                    342:            (c->flags & CHAN_CLOSE_RCVD)) {
1.49      markus    343:                debug2("channel %d: is dead", c->self);
1.45      markus    344:                return 1;
1.1       markus    345:        }
1.23      markus    346:        return 0;
1.13      markus    347: }
                    348:
                    349: /* helper */
                    350: static void
                    351: chan_shutdown_write(Channel *c)
                    352: {
1.34      markus    353:        buffer_clear(&c->output);
1.64    ! djm       354:        if (c->type == SSH_CHANNEL_LARVAL)
1.13      markus    355:                return;
                    356:        /* shutdown failure is allowed if write failed already */
1.49      markus    357:        debug2("channel %d: close_write", c->self);
1.13      markus    358:        if (c->sock != -1) {
                    359:                if (shutdown(c->sock, SHUT_WR) < 0)
1.49      markus    360:                        debug2("channel %d: chan_shutdown_write: "
1.62      stevesk   361:                            "shutdown() failed for fd %d: %.100s",
1.13      markus    362:                            c->self, c->sock, strerror(errno));
                    363:        } else {
1.31      markus    364:                if (channel_close_fd(&c->wfd) < 0)
1.48      itojun    365:                        logit("channel %d: chan_shutdown_write: "
1.62      stevesk   366:                            "close() failed for fd %d: %.100s",
1.13      markus    367:                            c->self, c->wfd, strerror(errno));
                    368:        }
                    369: }
1.64    ! djm       370:
1.13      markus    371: static void
                    372: chan_shutdown_read(Channel *c)
                    373: {
1.64    ! djm       374:        if (c->type == SSH_CHANNEL_LARVAL)
1.13      markus    375:                return;
1.49      markus    376:        debug2("channel %d: close_read", c->self);
1.13      markus    377:        if (c->sock != -1) {
                    378:                if (shutdown(c->sock, SHUT_RD) < 0)
1.28      markus    379:                        error("channel %d: chan_shutdown_read: "
1.62      stevesk   380:                            "shutdown() failed for fd %d [i%d o%d]: %.100s",
1.28      markus    381:                            c->self, c->sock, c->istate, c->ostate,
                    382:                            strerror(errno));
1.13      markus    383:        } else {
1.31      markus    384:                if (channel_close_fd(&c->rfd) < 0)
1.48      itojun    385:                        logit("channel %d: chan_shutdown_read: "
1.62      stevesk   386:                            "close() failed for fd %d: %.100s",
1.13      markus    387:                            c->self, c->rfd, strerror(errno));
                    388:        }
1.1       markus    389: }