blob: 43816a65b26d0229b7b8f019b786bf66f8c9a7fc [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: nchan.c,v 1.55 2006/07/11 20:07:25 stevesk Exp $ */
Damien Miller5428f641999-11-25 11:54:57 +11002/*
Damien Millerb51ed392002-01-22 23:29:03 +11003 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11004 *
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.
Damien Miller5428f641999-11-25 11:54:57 +110013 *
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
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millere3b60b52006-07-10 21:08:03 +100028#include <sys/types.h>
29#include <sys/socket.h>
30
Darren Tucker39972492006-07-12 22:22:46 +100031#include <errno.h>
32
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "ssh1.h"
34#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035#include "buffer.h"
36#include "packet.h"
37#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100038#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Ben Lindstrom3b670d02001-06-09 00:57:39 +000041/*
42 * SSH Protocol 1.5 aka New Channel Protocol
43 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
44 * Written by Markus Friedl in October 1999
45 *
46 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
47 * tear down of channels:
48 *
49 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100050 * CLOSE ->
51 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000052 *
53 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100054 * IEOF ->
55 * <- OCLOSE
56 * <- IEOF
57 * OCLOSE ->
58 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000059 *
60 * 2.0: the EOF messages are optional
61 *
62 * See the debugging output from 'ssh -v' and 'sshd -d' of
63 * ssh-1.2.27 as an example.
64 *
65 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000066
Damien Miller33b13562000-04-04 14:38:59 +100067/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068/*
Damien Miller95def091999-11-25 00:26:21 +110069 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070 */
Damien Miller33b13562000-04-04 14:38:59 +100071/*
72 * ACTIONS: should never update the channel states
73 */
Ben Lindstrombba81212001-06-25 05:01:22 +000074static void chan_send_ieof1(Channel *);
75static void chan_send_oclose1(Channel *);
76static void chan_send_close2(Channel *);
77static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100078
Damien Miller33b13562000-04-04 14:38:59 +100079/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000080static void chan_shutdown_write(Channel *);
81static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100082
Damien Millerabea8ee2002-01-22 23:27:11 +110083static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
84static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
85
86static void
87chan_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);
Damien Millerfbdeece2003-09-02 22:52:31 +100091 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110092 istates[next]);
93 c->istate = next;
94}
95static void
96chan_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);
Damien Millerfbdeece2003-09-02 22:52:31 +1000100 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100101 ostates[next]);
102 c->ostate = next;
103}
104
Damien Miller33b13562000-04-04 14:38:59 +1000105/*
106 * SSH1 specific implementation of event functions
107 */
108
109static void
110chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100111{
Damien Millerfbdeece2003-09-02 22:52:31 +1000112 debug2("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100113 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100115 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 break;
117 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000119 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100120 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100121 break;
122 case CHAN_INPUT_WAIT_DRAIN:
123 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000124 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100125 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 break;
127 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000128 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000129 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100130 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131 }
132}
Damien Miller5144df92002-01-22 23:28:45 +1100133void
134chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100135{
Damien Millerfbdeece2003-09-02 22:52:31 +1000136 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100137 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100140 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 break;
142 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000143 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000144 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145 break;
146 }
147}
Damien Miller5144df92002-01-22 23:28:45 +1100148void
149chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100150{
Damien Millerfbdeece2003-09-02 22:52:31 +1000151 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100152 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000153 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000154 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155 return;
156 }
Damien Miller95def091999-11-25 00:26:21 +1100157 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158 case CHAN_INPUT_WAIT_DRAIN:
Damien Millerfcfc43b2002-01-22 23:27:45 +1100159 if (compat20) {
160 if (!(c->flags & CHAN_CLOSE_SENT))
161 chan_send_eof2(c);
162 chan_set_istate(c, CHAN_INPUT_CLOSED);
163 } else {
164 chan_send_ieof1(c);
165 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
166 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167 break;
168 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000169 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000170 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171 break;
172 }
173}
Damien Miller33b13562000-04-04 14:38:59 +1000174static void
175chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100176{
Damien Millerfbdeece2003-09-02 22:52:31 +1000177 debug2("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100178 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100180 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181 break;
182 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100183 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184 break;
185 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000186 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000187 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188 break;
189 }
190}
Damien Miller33b13562000-04-04 14:38:59 +1000191static void
192chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100193{
Damien Millerfbdeece2003-09-02 22:52:31 +1000194 debug2("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100195 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100197 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000198 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100199 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200 break;
201 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100202 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000203 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100204 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205 break;
206 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000207 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000208 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209 break;
210 }
211}
Damien Miller5144df92002-01-22 23:28:45 +1100212void
213chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100214{
Damien Millerfbdeece2003-09-02 22:52:31 +1000215 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100216 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000217 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000218 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219 return;
220 }
Damien Miller95def091999-11-25 00:26:21 +1100221 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100223 chan_shutdown_write(c);
Damien Millerfcfc43b2002-01-22 23:27:45 +1100224 if (!compat20)
225 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100226 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227 break;
228 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000229 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000230 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231 break;
232 }
233}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234static void
Damien Miller33b13562000-04-04 14:38:59 +1000235chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100236{
Damien Millerfbdeece2003-09-02 22:52:31 +1000237 debug2("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100238 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239 case CHAN_INPUT_OPEN:
240 case CHAN_INPUT_WAIT_DRAIN:
241 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
242 packet_put_int(c->remote_id);
243 packet_send();
244 break;
245 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000246 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000247 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248 break;
249 }
250}
251static void
Damien Miller33b13562000-04-04 14:38:59 +1000252chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100253{
Damien Millerfbdeece2003-09-02 22:52:31 +1000254 debug2("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100255 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256 case CHAN_OUTPUT_OPEN:
257 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100258 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
260 packet_put_int(c->remote_id);
261 packet_send();
262 break;
263 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000264 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100265 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266 break;
267 }
268}
Damien Miller33b13562000-04-04 14:38:59 +1000269
270/*
271 * the same for SSH2
272 */
273static void
Damien Millerebc11d32002-01-22 23:28:13 +1100274chan_rcvd_close2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000275{
Damien Millerfbdeece2003-09-02 22:52:31 +1000276 debug2("channel %d: rcvd close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000277 if (c->flags & CHAN_CLOSE_RCVD)
278 error("channel %d: protocol error: close rcvd twice", c->self);
279 c->flags |= CHAN_CLOSE_RCVD;
280 if (c->type == SSH_CHANNEL_LARVAL) {
281 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100282 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
283 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000284 return;
285 }
286 switch (c->ostate) {
287 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000288 /*
289 * wait until a data from the channel is consumed if a CLOSE
290 * is received
291 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100292 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000293 break;
294 }
295 switch (c->istate) {
296 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000297 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100298 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000299 break;
300 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000301 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100302 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000303 break;
304 }
Damien Miller33b13562000-04-04 14:38:59 +1000305}
306static void
Damien Millerebc11d32002-01-22 23:28:13 +1100307chan_rcvd_eof2(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000308{
Damien Millerfbdeece2003-09-02 22:52:31 +1000309 debug2("channel %d: rcvd eof", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000310 c->flags |= CHAN_EOF_RCVD;
Damien Millerabea8ee2002-01-22 23:27:11 +1100311 if (c->ostate == CHAN_OUTPUT_OPEN)
312 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000313}
314static void
315chan_write_failed2(Channel *c)
316{
Damien Millerfbdeece2003-09-02 22:52:31 +1000317 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000318 switch (c->ostate) {
319 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000320 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000321 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100322 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000323 break;
324 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000325 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000326 c->self, c->ostate);
327 break;
328 }
329}
330static void
Damien Miller33b13562000-04-04 14:38:59 +1000331chan_send_eof2(Channel *c)
332{
Damien Millerfbdeece2003-09-02 22:52:31 +1000333 debug2("channel %d: send eof", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000334 switch (c->istate) {
335 case CHAN_INPUT_WAIT_DRAIN:
336 packet_start(SSH2_MSG_CHANNEL_EOF);
337 packet_put_int(c->remote_id);
338 packet_send();
Ben Lindstromcf159442002-03-26 03:26:24 +0000339 c->flags |= CHAN_EOF_SENT;
Damien Miller33b13562000-04-04 14:38:59 +1000340 break;
341 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000342 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000343 c->self, c->istate);
344 break;
345 }
346}
347static void
348chan_send_close2(Channel *c)
349{
Damien Millerfbdeece2003-09-02 22:52:31 +1000350 debug2("channel %d: send close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000351 if (c->ostate != CHAN_OUTPUT_CLOSED ||
352 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000353 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000354 c->self, c->istate, c->ostate);
355 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000356 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000357 } else {
358 packet_start(SSH2_MSG_CHANNEL_CLOSE);
359 packet_put_int(c->remote_id);
360 packet_send();
361 c->flags |= CHAN_CLOSE_SENT;
362 }
363}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000364
365/* shared */
366
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000367void
Damien Miller5144df92002-01-22 23:28:45 +1100368chan_rcvd_ieof(Channel *c)
369{
370 if (compat20)
371 chan_rcvd_eof2(c);
372 else
373 chan_rcvd_ieof1(c);
Damien Miller73f10742002-01-22 23:34:52 +1100374 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000375 buffer_len(&c->output) == 0 &&
Ben Lindstromcf159442002-03-26 03:26:24 +0000376 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller73f10742002-01-22 23:34:52 +1100377 chan_obuf_empty(c);
Damien Miller5144df92002-01-22 23:28:45 +1100378}
379void
380chan_rcvd_oclose(Channel *c)
381{
382 if (compat20)
383 chan_rcvd_close2(c);
384 else
385 chan_rcvd_oclose1(c);
386}
387void
388chan_write_failed(Channel *c)
389{
390 if (compat20)
391 chan_write_failed2(c);
392 else
393 chan_write_failed1(c);
394}
395
396void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000397chan_mark_dead(Channel *c)
398{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000399 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000400}
401
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000402int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000403chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000404{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000405 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000406 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000407 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000408 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000409 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
410 return 0;
411 if (!compat20) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000412 debug2("channel %d: is dead", c->self);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000413 return 1;
414 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000415 if ((datafellows & SSH_BUG_EXTEOF) &&
416 c->extended_usage == CHAN_EXTENDED_WRITE &&
417 c->efd != -1 &&
418 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000419 debug2("channel %d: active efd: %d len %d",
420 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000421 return 0;
422 }
423 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000424 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000425 chan_send_close2(c);
426 } else {
427 /* channel would be dead if we sent a close */
428 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000429 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000430 c->self);
431 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000432 }
Damien Miller33b13562000-04-04 14:38:59 +1000433 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000434 }
435 if ((c->flags & CHAN_CLOSE_SENT) &&
436 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000437 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000438 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000439 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000440 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000441}
442
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443/* helper */
444static void
Damien Miller95def091999-11-25 00:26:21 +1100445chan_shutdown_write(Channel *c)
446{
Damien Miller76765c02002-01-22 23:21:15 +1100447 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000448 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
449 return;
Damien Miller5428f641999-11-25 11:54:57 +1100450 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000451 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000452 if (c->sock != -1) {
453 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000454 debug2("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000455 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000456 c->self, c->sock, strerror(errno));
457 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000458 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000459 logit("channel %d: chan_shutdown_write: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000460 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000461 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000462 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463}
464static void
Damien Miller95def091999-11-25 00:26:21 +1100465chan_shutdown_read(Channel *c)
466{
Damien Miller33b13562000-04-04 14:38:59 +1000467 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
468 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000469 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000470 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000471 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000472 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
473 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000474 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000475 */
476 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000477 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000478 error("channel %d: chan_shutdown_read: "
479 "shutdown() failed for fd%d [i%d o%d]: %.100s",
480 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000481 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000482 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000483 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000484 logit("channel %d: chan_shutdown_read: "
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000485 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000486 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 }
488}