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