[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.49

1.7       markus      1: /*
1.43      markus      2:  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
1.7       markus      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
1.1       markus     25: #include "includes.h"
1.49    ! markus     26: RCSID("$OpenBSD: nchan.c,v 1.48 2003/04/08 20:21:29 itojun Exp $");
1.1       markus     27:
1.22      markus     28: #include "ssh1.h"
                     29: #include "ssh2.h"
1.1       markus     30: #include "buffer.h"
1.3       markus     31: #include "packet.h"
1.1       markus     32: #include "channels.h"
1.13      markus     33: #include "compat.h"
1.22      markus     34: #include "log.h"
1.3       markus     35:
1.28      markus     36: /*
                     37:  * SSH Protocol 1.5 aka New Channel Protocol
                     38:  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
                     39:  * Written by Markus Friedl in October 1999
                     40:  *
                     41:  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
                     42:  * tear down of channels:
                     43:  *
                     44:  * 1.3:        strict request-ack-protocol:
                     45:  *     CLOSE   ->
                     46:  *             <-  CLOSE_CONFIRM
                     47:  *
                     48:  * 1.5:        uses variations of:
                     49:  *     IEOF    ->
                     50:  *             <-  OCLOSE
                     51:  *             <-  IEOF
                     52:  *     OCLOSE  ->
                     53:  *     i.e. both sides have to close the channel
                     54:  *
                     55:  * 2.0: the EOF messages are optional
                     56:  *
                     57:  * See the debugging output from 'ssh -v' and 'sshd -d' of
                     58:  * ssh-1.2.27 as an example.
                     59:  *
                     60:  */
                     61:
1.13      markus     62: /* functions manipulating channel states */
1.3       markus     63: /*
1.6       markus     64:  * EVENTS update channel input/output states execute ACTIONS
1.3       markus     65:  */
1.13      markus     66: /*
                     67:  * ACTIONS: should never update the channel states
                     68:  */
1.29      itojun     69: static void    chan_send_ieof1(Channel *);
                     70: static void    chan_send_oclose1(Channel *);
                     71: static void    chan_send_close2(Channel *);
                     72: static void    chan_send_eof2(Channel *);
1.13      markus     73:
                     74: /* helper */
1.29      itojun     75: static void    chan_shutdown_write(Channel *);
                     76: static void    chan_shutdown_read(Channel *);
1.13      markus     77:
1.37      markus     78: static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
                     79: static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
                     80:
                     81: static void
                     82: chan_set_istate(Channel *c, u_int next)
                     83: {
                     84:        if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
                     85:                fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
1.49    ! markus     86:        debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
1.37      markus     87:            istates[next]);
                     88:        c->istate = next;
                     89: }
                     90: static void
                     91: chan_set_ostate(Channel *c, u_int next)
                     92: {
                     93:        if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
                     94:                fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
1.49    ! markus     95:        debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
1.37      markus     96:            ostates[next]);
                     97:        c->ostate = next;
                     98: }
                     99:
1.13      markus    100: /*
                    101:  * SSH1 specific implementation of event functions
                    102:  */
1.6       markus    103:
1.13      markus    104: static void
                    105: chan_rcvd_oclose1(Channel *c)
1.6       markus    106: {
1.49    ! markus    107:        debug2("channel %d: rcvd oclose", c->self);
1.6       markus    108:        switch (c->istate) {
1.3       markus    109:        case CHAN_INPUT_WAIT_OCLOSE:
1.37      markus    110:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3       markus    111:                break;
                    112:        case CHAN_INPUT_OPEN:
                    113:                chan_shutdown_read(c);
1.13      markus    114:                chan_send_ieof1(c);
1.37      markus    115:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.10      markus    116:                break;
                    117:        case CHAN_INPUT_WAIT_DRAIN:
                    118:                /* both local read_failed and remote write_failed  */
1.13      markus    119:                chan_send_ieof1(c);
1.37      markus    120:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.3       markus    121:                break;
                    122:        default:
1.28      markus    123:                error("channel %d: protocol error: rcvd_oclose for istate %d",
1.13      markus    124:                    c->self, c->istate);
1.10      markus    125:                return;
1.3       markus    126:        }
1.1       markus    127: }
1.42      markus    128: void
                    129: chan_read_failed(Channel *c)
1.6       markus    130: {
1.49    ! markus    131:        debug2("channel %d: read failed", c->self);
1.6       markus    132:        switch (c->istate) {
1.3       markus    133:        case CHAN_INPUT_OPEN:
                    134:                chan_shutdown_read(c);
1.37      markus    135:                chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
1.3       markus    136:                break;
                    137:        default:
1.28      markus    138:                error("channel %d: chan_read_failed for istate %d",
1.13      markus    139:                    c->self, c->istate);
1.3       markus    140:                break;
1.1       markus    141:        }
                    142: }
1.42      markus    143: void
                    144: chan_ibuf_empty(Channel *c)
1.6       markus    145: {
1.49    ! markus    146:        debug2("channel %d: ibuf empty", c->self);
1.6       markus    147:        if (buffer_len(&c->input)) {
1.28      markus    148:                error("channel %d: chan_ibuf_empty for non empty buffer",
1.13      markus    149:                    c->self);
1.3       markus    150:                return;
                    151:        }
1.6       markus    152:        switch (c->istate) {
1.3       markus    153:        case CHAN_INPUT_WAIT_DRAIN:
1.39      markus    154:                if (compat20) {
                    155:                        if (!(c->flags & CHAN_CLOSE_SENT))
                    156:                                chan_send_eof2(c);
                    157:                        chan_set_istate(c, CHAN_INPUT_CLOSED);
                    158:                } else {
                    159:                        chan_send_ieof1(c);
                    160:                        chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
                    161:                }
1.3       markus    162:                break;
                    163:        default:
1.28      markus    164:                error("channel %d: chan_ibuf_empty for istate %d",
1.13      markus    165:                    c->self, c->istate);
1.3       markus    166:                break;
1.1       markus    167:        }
                    168: }
1.13      markus    169: static void
                    170: chan_rcvd_ieof1(Channel *c)
1.6       markus    171: {
1.49    ! markus    172:        debug2("channel %d: rcvd ieof", c->self);
1.6       markus    173:        switch (c->ostate) {
1.3       markus    174:        case CHAN_OUTPUT_OPEN:
1.37      markus    175:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.3       markus    176:                break;
                    177:        case CHAN_OUTPUT_WAIT_IEOF:
1.37      markus    178:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3       markus    179:                break;
                    180:        default:
1.28      markus    181:                error("channel %d: protocol error: rcvd_ieof for ostate %d",
1.13      markus    182:                    c->self, c->ostate);
1.3       markus    183:                break;
                    184:        }
                    185: }
1.13      markus    186: static void
                    187: chan_write_failed1(Channel *c)
1.6       markus    188: {
1.49    ! markus    189:        debug2("channel %d: write failed", c->self);
1.6       markus    190:        switch (c->ostate) {
1.3       markus    191:        case CHAN_OUTPUT_OPEN:
1.38      markus    192:                chan_shutdown_write(c);
1.13      markus    193:                chan_send_oclose1(c);
1.37      markus    194:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
1.3       markus    195:                break;
                    196:        case CHAN_OUTPUT_WAIT_DRAIN:
1.38      markus    197:                chan_shutdown_write(c);
1.13      markus    198:                chan_send_oclose1(c);
1.37      markus    199:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3       markus    200:                break;
                    201:        default:
1.28      markus    202:                error("channel %d: chan_write_failed for ostate %d",
1.13      markus    203:                    c->self, c->ostate);
1.3       markus    204:                break;
                    205:        }
                    206: }
1.42      markus    207: void
                    208: chan_obuf_empty(Channel *c)
1.6       markus    209: {
1.49    ! markus    210:        debug2("channel %d: obuf empty", c->self);
1.6       markus    211:        if (buffer_len(&c->output)) {
1.28      markus    212:                error("channel %d: chan_obuf_empty for non empty buffer",
1.13      markus    213:                    c->self);
1.3       markus    214:                return;
                    215:        }
1.6       markus    216:        switch (c->ostate) {
1.3       markus    217:        case CHAN_OUTPUT_WAIT_DRAIN:
1.38      markus    218:                chan_shutdown_write(c);
1.39      markus    219:                if (!compat20)
                    220:                        chan_send_oclose1(c);
1.37      markus    221:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.3       markus    222:                break;
                    223:        default:
1.28      markus    224:                error("channel %d: internal error: obuf_empty for ostate %d",
1.13      markus    225:                    c->self, c->ostate);
1.3       markus    226:                break;
                    227:        }
                    228: }
                    229: static void
1.13      markus    230: chan_send_ieof1(Channel *c)
1.6       markus    231: {
1.49    ! markus    232:        debug2("channel %d: send ieof", c->self);
1.6       markus    233:        switch (c->istate) {
1.3       markus    234:        case CHAN_INPUT_OPEN:
                    235:        case CHAN_INPUT_WAIT_DRAIN:
                    236:                packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
                    237:                packet_put_int(c->remote_id);
                    238:                packet_send();
                    239:                break;
                    240:        default:
1.28      markus    241:                error("channel %d: cannot send ieof for istate %d",
1.13      markus    242:                    c->self, c->istate);
1.3       markus    243:                break;
1.1       markus    244:        }
                    245: }
1.3       markus    246: static void
1.13      markus    247: chan_send_oclose1(Channel *c)
1.6       markus    248: {
1.49    ! markus    249:        debug2("channel %d: send oclose", c->self);
1.6       markus    250:        switch (c->ostate) {
1.3       markus    251:        case CHAN_OUTPUT_OPEN:
                    252:        case CHAN_OUTPUT_WAIT_DRAIN:
1.34      markus    253:                buffer_clear(&c->output);
1.3       markus    254:                packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
                    255:                packet_put_int(c->remote_id);
                    256:                packet_send();
                    257:                break;
                    258:        default:
1.28      markus    259:                error("channel %d: cannot send oclose for ostate %d",
1.33      deraadt   260:                    c->self, c->ostate);
1.3       markus    261:                break;
1.1       markus    262:        }
                    263: }
1.6       markus    264:
1.13      markus    265: /*
                    266:  * the same for SSH2
                    267:  */
                    268: static void
1.40      markus    269: chan_rcvd_close2(Channel *c)
1.13      markus    270: {
1.49    ! markus    271:        debug2("channel %d: rcvd close", c->self);
1.13      markus    272:        if (c->flags & CHAN_CLOSE_RCVD)
                    273:                error("channel %d: protocol error: close rcvd twice", c->self);
                    274:        c->flags |= CHAN_CLOSE_RCVD;
                    275:        if (c->type == SSH_CHANNEL_LARVAL) {
                    276:                /* tear down larval channels immediately */
1.37      markus    277:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
                    278:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.13      markus    279:                return;
                    280:        }
                    281:        switch (c->ostate) {
                    282:        case CHAN_OUTPUT_OPEN:
1.28      markus    283:                /*
                    284:                 * wait until a data from the channel is consumed if a CLOSE
                    285:                 * is received
                    286:                 */
1.37      markus    287:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.13      markus    288:                break;
                    289:        }
                    290:        switch (c->istate) {
                    291:        case CHAN_INPUT_OPEN:
                    292:                chan_shutdown_read(c);
1.40      markus    293:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.13      markus    294:                break;
                    295:        case CHAN_INPUT_WAIT_DRAIN:
                    296:                chan_send_eof2(c);
1.40      markus    297:                chan_set_istate(c, CHAN_INPUT_CLOSED);
1.13      markus    298:                break;
                    299:        }
                    300: }
                    301: static void
1.40      markus    302: chan_rcvd_eof2(Channel *c)
1.13      markus    303: {
1.49    ! markus    304:        debug2("channel %d: rcvd eof", c->self);
1.45      markus    305:        c->flags |= CHAN_EOF_RCVD;
1.37      markus    306:        if (c->ostate == CHAN_OUTPUT_OPEN)
                    307:                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
1.13      markus    308: }
                    309: static void
                    310: chan_write_failed2(Channel *c)
                    311: {
1.49    ! markus    312:        debug2("channel %d: write failed", c->self);
1.13      markus    313:        switch (c->ostate) {
                    314:        case CHAN_OUTPUT_OPEN:
                    315:        case CHAN_OUTPUT_WAIT_DRAIN:
                    316:                chan_shutdown_write(c);
1.37      markus    317:                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
1.13      markus    318:                break;
                    319:        default:
1.28      markus    320:                error("channel %d: chan_write_failed for ostate %d",
1.13      markus    321:                    c->self, c->ostate);
                    322:                break;
                    323:        }
                    324: }
                    325: static void
                    326: chan_send_eof2(Channel *c)
1.6       markus    327: {
1.49    ! markus    328:        debug2("channel %d: send eof", c->self);
1.13      markus    329:        switch (c->istate) {
                    330:        case CHAN_INPUT_WAIT_DRAIN:
                    331:                packet_start(SSH2_MSG_CHANNEL_EOF);
                    332:                packet_put_int(c->remote_id);
                    333:                packet_send();
1.45      markus    334:                c->flags |= CHAN_EOF_SENT;
1.13      markus    335:                break;
                    336:        default:
1.28      markus    337:                error("channel %d: cannot send eof for istate %d",
1.13      markus    338:                    c->self, c->istate);
                    339:                break;
                    340:        }
1.1       markus    341: }
1.3       markus    342: static void
1.13      markus    343: chan_send_close2(Channel *c)
1.6       markus    344: {
1.49    ! markus    345:        debug2("channel %d: send close", c->self);
1.13      markus    346:        if (c->ostate != CHAN_OUTPUT_CLOSED ||
                    347:            c->istate != CHAN_INPUT_CLOSED) {
1.28      markus    348:                error("channel %d: cannot send close for istate/ostate %d/%d",
1.13      markus    349:                    c->self, c->istate, c->ostate);
                    350:        } else if (c->flags & CHAN_CLOSE_SENT) {
1.28      markus    351:                error("channel %d: already sent close", c->self);
1.13      markus    352:        } else {
                    353:                packet_start(SSH2_MSG_CHANNEL_CLOSE);
                    354:                packet_put_int(c->remote_id);
                    355:                packet_send();
                    356:                c->flags |= CHAN_CLOSE_SENT;
                    357:        }
1.1       markus    358: }
1.23      markus    359:
                    360: /* shared */
                    361:
1.24      markus    362: void
1.42      markus    363: chan_rcvd_ieof(Channel *c)
                    364: {
                    365:        if (compat20)
                    366:                chan_rcvd_eof2(c);
                    367:        else
                    368:                chan_rcvd_ieof1(c);
1.44      markus    369:        if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
1.47      deraadt   370:            buffer_len(&c->output) == 0 &&
1.45      markus    371:            !CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.44      markus    372:                chan_obuf_empty(c);
1.42      markus    373: }
                    374: void
                    375: chan_rcvd_oclose(Channel *c)
                    376: {
                    377:        if (compat20)
                    378:                chan_rcvd_close2(c);
                    379:        else
                    380:                chan_rcvd_oclose1(c);
                    381: }
                    382: void
                    383: chan_write_failed(Channel *c)
                    384: {
                    385:        if (compat20)
                    386:                chan_write_failed2(c);
                    387:        else
                    388:                chan_write_failed1(c);
                    389: }
                    390:
                    391: void
1.24      markus    392: chan_mark_dead(Channel *c)
                    393: {
1.26      markus    394:        c->type = SSH_CHANNEL_ZOMBIE;
1.24      markus    395: }
                    396:
1.23      markus    397: int
1.32      markus    398: chan_is_dead(Channel *c, int send)
1.6       markus    399: {
1.26      markus    400:        if (c->type == SSH_CHANNEL_ZOMBIE) {
1.49    ! markus    401:                debug2("channel %d: zombie", c->self);
1.24      markus    402:                return 1;
1.26      markus    403:        }
1.23      markus    404:        if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
                    405:                return 0;
                    406:        if (!compat20) {
1.49    ! markus    407:                debug2("channel %d: is dead", c->self);
1.23      markus    408:                return 1;
                    409:        }
1.45      markus    410:        if ((datafellows & SSH_BUG_EXTEOF) &&
                    411:            c->extended_usage == CHAN_EXTENDED_WRITE &&
                    412:            c->efd != -1 &&
                    413:            buffer_len(&c->extended) > 0) {
1.46      markus    414:                debug2("channel %d: active efd: %d len %d",
                    415:                    c->self, c->efd, buffer_len(&c->extended));
1.45      markus    416:                return 0;
                    417:        }
                    418:        if (!(c->flags & CHAN_CLOSE_SENT)) {
                    419:                if (send) {
                    420:                        chan_send_close2(c);
                    421:                } else {
                    422:                        /* channel would be dead if we sent a close */
                    423:                        if (c->flags & CHAN_CLOSE_RCVD) {
1.49    ! markus    424:                                debug2("channel %d: almost dead",
1.45      markus    425:                                    c->self);
                    426:                                return 1;
1.32      markus    427:                        }
1.13      markus    428:                }
1.45      markus    429:        }
                    430:        if ((c->flags & CHAN_CLOSE_SENT) &&
                    431:            (c->flags & CHAN_CLOSE_RCVD)) {
1.49    ! markus    432:                debug2("channel %d: is dead", c->self);
1.45      markus    433:                return 1;
1.1       markus    434:        }
1.23      markus    435:        return 0;
1.13      markus    436: }
                    437:
                    438: /* helper */
                    439: static void
                    440: chan_shutdown_write(Channel *c)
                    441: {
1.34      markus    442:        buffer_clear(&c->output);
1.13      markus    443:        if (compat20 && c->type == SSH_CHANNEL_LARVAL)
                    444:                return;
                    445:        /* shutdown failure is allowed if write failed already */
1.49    ! markus    446:        debug2("channel %d: close_write", c->self);
1.13      markus    447:        if (c->sock != -1) {
                    448:                if (shutdown(c->sock, SHUT_WR) < 0)
1.49    ! markus    449:                        debug2("channel %d: chan_shutdown_write: "
1.28      markus    450:                            "shutdown() failed for fd%d: %.100s",
1.13      markus    451:                            c->self, c->sock, strerror(errno));
                    452:        } else {
1.31      markus    453:                if (channel_close_fd(&c->wfd) < 0)
1.48      itojun    454:                        logit("channel %d: chan_shutdown_write: "
1.28      markus    455:                            "close() failed for fd%d: %.100s",
1.13      markus    456:                            c->self, c->wfd, strerror(errno));
                    457:        }
                    458: }
                    459: static void
                    460: chan_shutdown_read(Channel *c)
                    461: {
                    462:        if (compat20 && c->type == SSH_CHANNEL_LARVAL)
                    463:                return;
1.49    ! markus    464:        debug2("channel %d: close_read", c->self);
1.13      markus    465:        if (c->sock != -1) {
                    466:                if (shutdown(c->sock, SHUT_RD) < 0)
1.28      markus    467:                        error("channel %d: chan_shutdown_read: "
                    468:                            "shutdown() failed for fd%d [i%d o%d]: %.100s",
                    469:                            c->self, c->sock, c->istate, c->ostate,
                    470:                            strerror(errno));
1.13      markus    471:        } else {
1.31      markus    472:                if (channel_close_fd(&c->rfd) < 0)
1.48      itojun    473:                        logit("channel %d: chan_shutdown_read: "
1.28      markus    474:                            "close() failed for fd%d: %.100s",
1.13      markus    475:                            c->self, c->rfd, strerror(errno));
                    476:        }
1.1       markus    477: }