nchan.c revision 1.11 1 1.11 christos /* $NetBSD: nchan.c,v 1.11 2019/10/12 18:32:22 christos Exp $ */
2 1.11 christos /* $OpenBSD: nchan.c,v 1.70 2019/06/28 13:35:04 deraadt 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.11 2019/10/12 18:32:22 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.4 christos static const char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
87 1.4 christos static const char *istates[] = { "open", "drain", "wait_oclose", "closed" };
88 1.1 christos
89 1.1 christos static void
90 1.1 christos chan_set_istate(Channel *c, u_int next)
91 1.1 christos {
92 1.1 christos if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
93 1.1 christos fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
94 1.1 christos debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
95 1.1 christos istates[next]);
96 1.1 christos c->istate = next;
97 1.1 christos }
98 1.9 christos
99 1.1 christos static void
100 1.1 christos chan_set_ostate(Channel *c, u_int next)
101 1.1 christos {
102 1.1 christos if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
103 1.1 christos fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
104 1.1 christos debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
105 1.1 christos ostates[next]);
106 1.1 christos c->ostate = next;
107 1.1 christos }
108 1.1 christos
109 1.1 christos void
110 1.9 christos chan_read_failed(struct ssh *ssh, Channel *c)
111 1.1 christos {
112 1.1 christos debug2("channel %d: read failed", c->self);
113 1.1 christos switch (c->istate) {
114 1.1 christos case CHAN_INPUT_OPEN:
115 1.9 christos chan_shutdown_read(ssh, c);
116 1.1 christos chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
117 1.1 christos break;
118 1.1 christos default:
119 1.1 christos error("channel %d: chan_read_failed for istate %d",
120 1.1 christos c->self, c->istate);
121 1.1 christos break;
122 1.1 christos }
123 1.1 christos }
124 1.9 christos
125 1.1 christos void
126 1.9 christos chan_ibuf_empty(struct ssh *ssh, Channel *c)
127 1.1 christos {
128 1.1 christos debug2("channel %d: ibuf empty", c->self);
129 1.9 christos if (sshbuf_len(c->input)) {
130 1.1 christos error("channel %d: chan_ibuf_empty for non empty buffer",
131 1.1 christos c->self);
132 1.1 christos return;
133 1.1 christos }
134 1.1 christos switch (c->istate) {
135 1.1 christos case CHAN_INPUT_WAIT_DRAIN:
136 1.9 christos if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
137 1.9 christos chan_send_eof2(ssh, c);
138 1.9 christos chan_set_istate(c, CHAN_INPUT_CLOSED);
139 1.1 christos break;
140 1.1 christos default:
141 1.1 christos error("channel %d: chan_ibuf_empty for istate %d",
142 1.1 christos c->self, c->istate);
143 1.1 christos break;
144 1.1 christos }
145 1.1 christos }
146 1.9 christos
147 1.1 christos void
148 1.9 christos chan_obuf_empty(struct ssh *ssh, Channel *c)
149 1.1 christos {
150 1.1 christos debug2("channel %d: obuf empty", c->self);
151 1.9 christos if (sshbuf_len(c->output)) {
152 1.1 christos error("channel %d: chan_obuf_empty for non empty buffer",
153 1.1 christos c->self);
154 1.1 christos return;
155 1.1 christos }
156 1.1 christos switch (c->ostate) {
157 1.1 christos case CHAN_OUTPUT_WAIT_DRAIN:
158 1.9 christos chan_shutdown_write(ssh, c);
159 1.1 christos chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
160 1.1 christos break;
161 1.1 christos default:
162 1.1 christos error("channel %d: internal error: obuf_empty for ostate %d",
163 1.1 christos c->self, c->ostate);
164 1.1 christos break;
165 1.1 christos }
166 1.1 christos }
167 1.9 christos
168 1.9 christos void
169 1.9 christos chan_rcvd_eow(struct ssh *ssh, Channel *c)
170 1.9 christos {
171 1.9 christos debug2("channel %d: rcvd eow", c->self);
172 1.9 christos switch (c->istate) {
173 1.9 christos case CHAN_INPUT_OPEN:
174 1.9 christos chan_shutdown_read(ssh, c);
175 1.9 christos chan_set_istate(c, CHAN_INPUT_CLOSED);
176 1.9 christos break;
177 1.9 christos }
178 1.9 christos }
179 1.9 christos
180 1.1 christos static void
181 1.9 christos chan_send_eof2(struct ssh *ssh, Channel *c)
182 1.1 christos {
183 1.9 christos int r;
184 1.9 christos
185 1.9 christos debug2("channel %d: send eof", c->self);
186 1.1 christos switch (c->istate) {
187 1.1 christos case CHAN_INPUT_WAIT_DRAIN:
188 1.9 christos if (!c->have_remote_id)
189 1.9 christos fatal("%s: channel %d: no remote_id",
190 1.9 christos __func__, c->self);
191 1.9 christos if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
192 1.9 christos (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
193 1.9 christos (r = sshpkt_send(ssh)) != 0)
194 1.9 christos fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
195 1.9 christos c->flags |= CHAN_EOF_SENT;
196 1.1 christos break;
197 1.1 christos default:
198 1.9 christos error("channel %d: cannot send eof for istate %d",
199 1.1 christos c->self, c->istate);
200 1.1 christos break;
201 1.1 christos }
202 1.1 christos }
203 1.9 christos
204 1.1 christos static void
205 1.9 christos chan_send_close2(struct ssh *ssh, Channel *c)
206 1.1 christos {
207 1.9 christos int r;
208 1.9 christos
209 1.9 christos debug2("channel %d: send close", c->self);
210 1.9 christos if (c->ostate != CHAN_OUTPUT_CLOSED ||
211 1.9 christos c->istate != CHAN_INPUT_CLOSED) {
212 1.9 christos error("channel %d: cannot send close for istate/ostate %d/%d",
213 1.9 christos c->self, c->istate, c->ostate);
214 1.9 christos } else if (c->flags & CHAN_CLOSE_SENT) {
215 1.9 christos error("channel %d: already sent close", c->self);
216 1.9 christos } else {
217 1.9 christos if (!c->have_remote_id)
218 1.9 christos fatal("%s: channel %d: no remote_id",
219 1.9 christos __func__, c->self);
220 1.9 christos if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
221 1.9 christos (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
222 1.9 christos (r = sshpkt_send(ssh)) != 0)
223 1.9 christos fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
224 1.9 christos c->flags |= CHAN_CLOSE_SENT;
225 1.1 christos }
226 1.1 christos }
227 1.1 christos
228 1.1 christos static void
229 1.9 christos chan_send_eow2(struct ssh *ssh, Channel *c)
230 1.9 christos {
231 1.9 christos int r;
232 1.9 christos
233 1.9 christos debug2("channel %d: send eow", c->self);
234 1.9 christos if (c->ostate == CHAN_OUTPUT_CLOSED) {
235 1.9 christos error("channel %d: must not sent eow on closed output",
236 1.9 christos c->self);
237 1.9 christos return;
238 1.9 christos }
239 1.9 christos if (!(datafellows & SSH_NEW_OPENSSH))
240 1.9 christos return;
241 1.9 christos if (!c->have_remote_id)
242 1.9 christos fatal("%s: channel %d: no remote_id", __func__, c->self);
243 1.9 christos if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
244 1.9 christos (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
245 1.9 christos (r = sshpkt_put_cstring(ssh, "eow (at) openssh.com")) != 0 ||
246 1.9 christos (r = sshpkt_put_u8(ssh, 0)) != 0 ||
247 1.9 christos (r = sshpkt_send(ssh)) != 0)
248 1.9 christos fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
249 1.9 christos }
250 1.9 christos
251 1.9 christos /* shared */
252 1.9 christos
253 1.9 christos void
254 1.9 christos chan_rcvd_ieof(struct ssh *ssh, Channel *c)
255 1.9 christos {
256 1.9 christos debug2("channel %d: rcvd eof", c->self);
257 1.9 christos c->flags |= CHAN_EOF_RCVD;
258 1.9 christos if (c->ostate == CHAN_OUTPUT_OPEN)
259 1.9 christos chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
260 1.9 christos if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
261 1.9 christos sshbuf_len(c->output) == 0 &&
262 1.9 christos !CHANNEL_EFD_OUTPUT_ACTIVE(c))
263 1.9 christos chan_obuf_empty(ssh, c);
264 1.9 christos }
265 1.9 christos
266 1.9 christos void
267 1.9 christos chan_rcvd_oclose(struct ssh *ssh, Channel *c)
268 1.1 christos {
269 1.1 christos debug2("channel %d: rcvd close", c->self);
270 1.3 adam if (!(c->flags & CHAN_LOCAL)) {
271 1.3 adam if (c->flags & CHAN_CLOSE_RCVD)
272 1.3 adam error("channel %d: protocol error: close rcvd twice",
273 1.3 adam c->self);
274 1.3 adam c->flags |= CHAN_CLOSE_RCVD;
275 1.3 adam }
276 1.1 christos if (c->type == SSH_CHANNEL_LARVAL) {
277 1.1 christos /* tear down larval channels immediately */
278 1.1 christos chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
279 1.1 christos chan_set_istate(c, CHAN_INPUT_CLOSED);
280 1.1 christos return;
281 1.1 christos }
282 1.1 christos switch (c->ostate) {
283 1.1 christos case CHAN_OUTPUT_OPEN:
284 1.1 christos /*
285 1.1 christos * wait until a data from the channel is consumed if a CLOSE
286 1.1 christos * is received
287 1.1 christos */
288 1.1 christos chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
289 1.1 christos break;
290 1.1 christos }
291 1.1 christos switch (c->istate) {
292 1.1 christos case CHAN_INPUT_OPEN:
293 1.9 christos chan_shutdown_read(ssh, c);
294 1.10 christos chan_shutdown_extended_read(ssh, c);
295 1.1 christos chan_set_istate(c, CHAN_INPUT_CLOSED);
296 1.1 christos break;
297 1.1 christos case CHAN_INPUT_WAIT_DRAIN:
298 1.3 adam if (!(c->flags & CHAN_LOCAL))
299 1.9 christos chan_send_eof2(ssh, c);
300 1.10 christos chan_shutdown_extended_read(ssh, c);
301 1.1 christos chan_set_istate(c, CHAN_INPUT_CLOSED);
302 1.1 christos break;
303 1.1 christos }
304 1.1 christos }
305 1.3 adam
306 1.1 christos void
307 1.9 christos chan_write_failed(struct ssh *ssh, Channel *c)
308 1.1 christos {
309 1.1 christos debug2("channel %d: write failed", c->self);
310 1.1 christos switch (c->ostate) {
311 1.1 christos case CHAN_OUTPUT_OPEN:
312 1.1 christos case CHAN_OUTPUT_WAIT_DRAIN:
313 1.9 christos chan_shutdown_write(ssh, c);
314 1.1 christos if (strcmp(c->ctype, "session") == 0)
315 1.9 christos chan_send_eow2(ssh, c);
316 1.1 christos chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
317 1.1 christos break;
318 1.1 christos default:
319 1.1 christos error("channel %d: chan_write_failed for ostate %d",
320 1.1 christos c->self, c->ostate);
321 1.1 christos break;
322 1.1 christos }
323 1.1 christos }
324 1.1 christos
325 1.1 christos void
326 1.9 christos chan_mark_dead(struct ssh *ssh, Channel *c)
327 1.1 christos {
328 1.1 christos c->type = SSH_CHANNEL_ZOMBIE;
329 1.1 christos }
330 1.1 christos
331 1.1 christos int
332 1.9 christos chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
333 1.1 christos {
334 1.1 christos if (c->type == SSH_CHANNEL_ZOMBIE) {
335 1.1 christos debug2("channel %d: zombie", c->self);
336 1.1 christos return 1;
337 1.1 christos }
338 1.1 christos if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
339 1.1 christos return 0;
340 1.1 christos if ((datafellows & SSH_BUG_EXTEOF) &&
341 1.1 christos c->extended_usage == CHAN_EXTENDED_WRITE &&
342 1.1 christos c->efd != -1 &&
343 1.9 christos sshbuf_len(c->extended) > 0) {
344 1.9 christos debug2("channel %d: active efd: %d len %zu",
345 1.9 christos c->self, c->efd, sshbuf_len(c->extended));
346 1.1 christos return 0;
347 1.1 christos }
348 1.3 adam if (c->flags & CHAN_LOCAL) {
349 1.3 adam debug2("channel %d: is dead (local)", c->self);
350 1.3 adam return 1;
351 1.3 adam }
352 1.1 christos if (!(c->flags & CHAN_CLOSE_SENT)) {
353 1.1 christos if (do_send) {
354 1.9 christos chan_send_close2(ssh, c);
355 1.1 christos } else {
356 1.1 christos /* channel would be dead if we sent a close */
357 1.1 christos if (c->flags & CHAN_CLOSE_RCVD) {
358 1.1 christos debug2("channel %d: almost dead",
359 1.1 christos c->self);
360 1.1 christos return 1;
361 1.1 christos }
362 1.1 christos }
363 1.1 christos }
364 1.1 christos if ((c->flags & CHAN_CLOSE_SENT) &&
365 1.1 christos (c->flags & CHAN_CLOSE_RCVD)) {
366 1.1 christos debug2("channel %d: is dead", c->self);
367 1.1 christos return 1;
368 1.1 christos }
369 1.1 christos return 0;
370 1.1 christos }
371 1.1 christos
372 1.1 christos /* helper */
373 1.1 christos static void
374 1.9 christos chan_shutdown_write(struct ssh *ssh, Channel *c)
375 1.1 christos {
376 1.9 christos sshbuf_reset(c->output);
377 1.9 christos if (c->type == SSH_CHANNEL_LARVAL)
378 1.1 christos return;
379 1.1 christos /* shutdown failure is allowed if write failed already */
380 1.10 christos debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
381 1.10 christos c->self, __func__, c->istate, c->ostate, c->sock, c->wfd, c->efd,
382 1.10 christos channel_format_extended_usage(c));
383 1.1 christos if (c->sock != -1) {
384 1.11 christos if (shutdown(c->sock, SHUT_WR) == -1) {
385 1.10 christos debug2("channel %d: %s: shutdown() failed for "
386 1.10 christos "fd %d [i%d o%d]: %.100s", c->self, __func__,
387 1.10 christos c->sock, c->istate, c->ostate,
388 1.10 christos strerror(errno));
389 1.10 christos }
390 1.1 christos } else {
391 1.10 christos if (channel_close_fd(ssh, &c->wfd) < 0) {
392 1.10 christos logit("channel %d: %s: close() failed for "
393 1.10 christos "fd %d [i%d o%d]: %.100s",
394 1.10 christos c->self, __func__, c->wfd, c->istate, c->ostate,
395 1.10 christos 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.10 christos debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
406 1.10 christos c->self, __func__, 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.10 christos error("channel %d: %s: shutdown() failed for "
411 1.10 christos "fd %d [i%d o%d]: %.100s",
412 1.10 christos c->self, __func__, c->sock, c->istate, c->ostate,
413 1.1 christos strerror(errno));
414 1.10 christos }
415 1.1 christos } else {
416 1.10 christos if (channel_close_fd(ssh, &c->rfd) < 0) {
417 1.10 christos logit("channel %d: %s: close() failed for "
418 1.10 christos "fd %d [i%d o%d]: %.100s",
419 1.10 christos c->self, __func__, c->rfd, c->istate, c->ostate,
420 1.10 christos strerror(errno));
421 1.10 christos }
422 1.10 christos }
423 1.10 christos }
424 1.10 christos
425 1.10 christos static void
426 1.10 christos chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
427 1.10 christos {
428 1.10 christos if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
429 1.10 christos return;
430 1.10 christos if (c->extended_usage != CHAN_EXTENDED_READ &&
431 1.10 christos c->extended_usage != CHAN_EXTENDED_IGNORE)
432 1.10 christos return;
433 1.10 christos debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
434 1.10 christos c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
435 1.10 christos channel_format_extended_usage(c));
436 1.10 christos if (channel_close_fd(ssh, &c->efd) < 0) {
437 1.10 christos logit("channel %d: %s: close() failed for "
438 1.10 christos "extended fd %d [i%d o%d]: %.100s",
439 1.10 christos c->self, __func__, c->efd, c->istate, c->ostate,
440 1.10 christos strerror(errno));
441 1.1 christos }
442 1.1 christos }
443