1 /* $NetBSD: dispatch.c,v 1.13 2026/04/08 18:58:40 christos Exp $ */ 2 /* $OpenBSD: dispatch.c,v 1.35 2026/03/03 09:57:25 dtucker Exp $ */ 3 4 /* 5 * Copyright (c) 2000 Markus Friedl. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "includes.h" 29 __RCSID("$NetBSD: dispatch.c,v 1.13 2026/04/08 18:58:40 christos Exp $"); 30 #include <sys/types.h> 31 32 #include <signal.h> 33 #include <stdarg.h> 34 35 #include "ssh2.h" 36 #include "log.h" 37 #include "dispatch.h" 38 #include "packet.h" 39 #include "ssherr.h" 40 41 int 42 dispatch_protocol_error(int type, uint32_t seq, struct ssh *ssh) 43 { 44 int r; 45 46 logit_f("type %d seq %u", type, seq); 47 if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 || 48 (r = sshpkt_put_u32(ssh, seq)) != 0 || 49 (r = sshpkt_send(ssh)) != 0 || 50 (r = ssh_packet_write_wait(ssh)) < 0) 51 sshpkt_fatal(ssh, r, "%s", __func__); 52 return 0; 53 } 54 55 int 56 dispatch_protocol_ignore(int type, uint32_t seq, struct ssh *ssh) 57 { 58 logit_f("type %d seq %u", type, seq); 59 return 0; 60 } 61 62 void 63 ssh_dispatch_init(struct ssh *ssh, dispatch_fn *dflt) 64 { 65 u_int i; 66 for (i = 0; i < DISPATCH_MAX; i++) 67 ssh->dispatch[i] = dflt; 68 } 69 70 void 71 ssh_dispatch_range(struct ssh *ssh, u_int from, u_int to, dispatch_fn *fn) 72 { 73 u_int i; 74 75 for (i = from; i <= to; i++) { 76 if (i >= DISPATCH_MAX) 77 break; 78 ssh->dispatch[i] = fn; 79 } 80 } 81 82 void 83 ssh_dispatch_set(struct ssh *ssh, int type, dispatch_fn *fn) 84 { 85 ssh->dispatch[type] = fn; 86 } 87 88 int 89 ssh_dispatch_run(struct ssh *ssh, int mode, volatile sig_atomic_t *done) 90 { 91 int r; 92 u_char type; 93 uint32_t seqnr; 94 95 for (;;) { 96 if (mode == DISPATCH_BLOCK) { 97 r = ssh_packet_read_seqnr(ssh, &type, &seqnr); 98 if (r != 0) 99 return r; 100 } else { 101 r = ssh_packet_read_poll_seqnr(ssh, &type, &seqnr); 102 if (r != 0) 103 return r; 104 if (type == SSH_MSG_NONE) 105 return 0; 106 } 107 if (type > 0 && type < DISPATCH_MAX && 108 ssh->dispatch[type] != NULL) { 109 if (ssh->dispatch_skip_packets) { 110 debug2("skipped packet (type %u)", type); 111 ssh->dispatch_skip_packets--; 112 continue; 113 } 114 r = (*ssh->dispatch[type])(type, seqnr, ssh); 115 if (r != 0) 116 return r; 117 } else { 118 r = sshpkt_disconnect(ssh, 119 "protocol error: rcvd type %d", type); 120 if (r != 0) 121 return r; 122 return SSH_ERR_DISCONNECTED; 123 } 124 if (done != NULL && *done) 125 return 0; 126 } 127 } 128 129 void 130 ssh_dispatch_run_fatal(struct ssh *ssh, int mode, volatile sig_atomic_t *done) 131 { 132 int r; 133 134 if ((r = ssh_dispatch_run(ssh, mode, done)) != 0) 135 sshpkt_fatal(ssh, r, "%s", __func__); 136 } 137