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