1 /* 2 * Copyright (c) 2003 3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4 * All rights reserved. 5 * 6 * Author: Harti Brandt <harti@freebsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $ 30 * 31 * UDP transport 32 */ 33 #include <sys/types.h> 34 #include <sys/queue.h> 35 36 #include <stdlib.h> 37 #include <syslog.h> 38 #include <string.h> 39 #include <errno.h> 40 #include <unistd.h> 41 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 44 45 #include "snmpmod.h" 46 #include "snmpd.h" 47 #include "trans_udp.h" 48 #include "tree.h" 49 #include "oid.h" 50 51 static int udp_start(void); 52 static int udp_stop(int); 53 static void udp_close_port(struct tport *); 54 static int udp_init_port(struct tport *); 55 static ssize_t udp_send(struct tport *, const u_char *, size_t, 56 const struct sockaddr *, size_t); 57 58 /* exported */ 59 const struct transport_def udp_trans = { 60 "udp", 61 OIDX_begemotSnmpdTransUdp, 62 udp_start, 63 udp_stop, 64 udp_close_port, 65 udp_init_port, 66 udp_send 67 }; 68 static struct transport *my_trans; 69 70 static int 71 udp_start(void) 72 { 73 return (trans_register(&udp_trans, &my_trans)); 74 } 75 76 static int 77 udp_stop(int force __unused) 78 { 79 if (my_trans != NULL) 80 if (trans_unregister(my_trans) != 0) 81 return (SNMP_ERR_GENERR); 82 return (SNMP_ERR_NOERROR); 83 } 84 85 /* 86 * A UDP port is ready 87 */ 88 static void 89 udp_input(int fd __unused, void *udata) 90 { 91 struct udp_port *p = udata; 92 93 p->input.peerlen = sizeof(p->ret); 94 snmpd_input(&p->input, &p->tport); 95 } 96 97 /* 98 * Create a UDP socket and bind it to the given port 99 */ 100 static int 101 udp_init_port(struct tport *tp) 102 { 103 struct udp_port *p = (struct udp_port *)tp; 104 struct sockaddr_in addr; 105 u_int32_t ip; 106 107 if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { 108 syslog(LOG_ERR, "creating UDP socket: %m"); 109 return (SNMP_ERR_RES_UNAVAIL); 110 } 111 ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) | 112 p->addr[3]; 113 memset(&addr, 0, sizeof(addr)); 114 addr.sin_addr.s_addr = htonl(ip); 115 addr.sin_port = htons(p->port); 116 addr.sin_family = AF_INET; 117 addr.sin_len = sizeof(addr); 118 if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) { 119 if (errno == EADDRNOTAVAIL) { 120 close(p->input.fd); 121 p->input.fd = -1; 122 return (SNMP_ERR_INCONS_NAME); 123 } 124 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr), 125 p->port); 126 close(p->input.fd); 127 p->input.fd = -1; 128 return (SNMP_ERR_GENERR); 129 } 130 if ((p->input.id = fd_select(p->input.fd, udp_input, 131 p, NULL)) == NULL) { 132 close(p->input.fd); 133 p->input.fd = -1; 134 return (SNMP_ERR_GENERR); 135 } 136 return (SNMP_ERR_NOERROR); 137 } 138 139 /* 140 * Create a new SNMP Port object and start it, if we are not 141 * in initialization mode. The arguments are in host byte order. 142 */ 143 static int 144 udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp) 145 { 146 struct udp_port *port; 147 int err; 148 149 if (udp_port > 0xffff) 150 return (SNMP_ERR_NO_CREATION); 151 if ((port = malloc(sizeof(*port))) == NULL) 152 return (SNMP_ERR_GENERR); 153 memset(port, 0, sizeof(*port)); 154 155 /* initialize common part */ 156 port->tport.index.len = 5; 157 port->tport.index.subs[0] = addr[0]; 158 port->tport.index.subs[1] = addr[1]; 159 port->tport.index.subs[2] = addr[2]; 160 port->tport.index.subs[3] = addr[3]; 161 port->tport.index.subs[4] = udp_port; 162 163 port->addr[0] = addr[0]; 164 port->addr[1] = addr[1]; 165 port->addr[2] = addr[2]; 166 port->addr[3] = addr[3]; 167 port->port = udp_port; 168 169 port->input.fd = -1; 170 port->input.id = NULL; 171 port->input.stream = 0; 172 port->input.cred = 0; 173 port->input.peer = (struct sockaddr *)&port->ret; 174 port->input.peerlen = sizeof(port->ret); 175 176 trans_insert_port(my_trans, &port->tport); 177 178 if (community != COMM_INITIALIZE && 179 (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) { 180 udp_close_port(&port->tport); 181 return (err); 182 } 183 *pp = port; 184 return (SNMP_ERR_NOERROR); 185 } 186 187 /* 188 * Close an SNMP port 189 */ 190 static void 191 udp_close_port(struct tport *tp) 192 { 193 struct udp_port *port = (struct udp_port *)tp; 194 195 snmpd_input_close(&port->input); 196 trans_remove_port(tp); 197 free(port); 198 } 199 200 /* 201 * Send something 202 */ 203 static ssize_t 204 udp_send(struct tport *tp, const u_char *buf, size_t len, 205 const struct sockaddr *addr, size_t addrlen) 206 { 207 struct udp_port *p = (struct udp_port *)tp; 208 209 return (sendto(p->input.fd, buf, len, 0, addr, addrlen)); 210 } 211 212 /* 213 * Port table 214 */ 215 int 216 op_snmp_port(struct snmp_context *ctx, struct snmp_value *value, 217 u_int sub, u_int iidx, enum snmp_op op) 218 { 219 asn_subid_t which = value->var.subs[sub-1]; 220 struct udp_port *p; 221 u_int8_t addr[4]; 222 u_int32_t port; 223 224 switch (op) { 225 226 case SNMP_OP_GETNEXT: 227 if ((p = (struct udp_port *)trans_next_port(my_trans, 228 &value->var, sub)) == NULL) 229 return (SNMP_ERR_NOSUCHNAME); 230 index_append(&value->var, sub, &p->tport.index); 231 break; 232 233 case SNMP_OP_GET: 234 if ((p = (struct udp_port *)trans_find_port(my_trans, 235 &value->var, sub)) == NULL) 236 return (SNMP_ERR_NOSUCHNAME); 237 break; 238 239 case SNMP_OP_SET: 240 p = (struct udp_port *)trans_find_port(my_trans, 241 &value->var, sub); 242 ctx->scratch->int1 = (p != NULL); 243 244 if (which != LEAF_begemotSnmpdPortStatus) 245 abort(); 246 if (!TRUTH_OK(value->v.integer)) 247 return (SNMP_ERR_WRONG_VALUE); 248 249 ctx->scratch->int2 = TRUTH_GET(value->v.integer); 250 251 if (ctx->scratch->int2) { 252 /* open an SNMP port */ 253 if (p != NULL) 254 /* already open - do nothing */ 255 return (SNMP_ERR_NOERROR); 256 257 if (index_decode(&value->var, sub, iidx, addr, &port)) 258 return (SNMP_ERR_NO_CREATION); 259 return (udp_open_port(addr, port, &p)); 260 261 } else { 262 /* close SNMP port - do in commit */ 263 } 264 return (SNMP_ERR_NOERROR); 265 266 case SNMP_OP_ROLLBACK: 267 p = (struct udp_port *)trans_find_port(my_trans, 268 &value->var, sub); 269 if (ctx->scratch->int1 == 0) { 270 /* did not exist */ 271 if (ctx->scratch->int2 == 1) { 272 /* created */ 273 if (p != NULL) 274 udp_close_port(&p->tport); 275 } 276 } 277 return (SNMP_ERR_NOERROR); 278 279 case SNMP_OP_COMMIT: 280 p = (struct udp_port *)trans_find_port(my_trans, 281 &value->var, sub); 282 if (ctx->scratch->int1 == 1) { 283 /* did exist */ 284 if (ctx->scratch->int2 == 0) { 285 /* delete */ 286 if (p != NULL) 287 udp_close_port(&p->tport); 288 } 289 } 290 return (SNMP_ERR_NOERROR); 291 292 default: 293 abort(); 294 } 295 296 /* 297 * Come here to fetch the value 298 */ 299 switch (which) { 300 301 case LEAF_begemotSnmpdPortStatus: 302 value->v.integer = 1; 303 break; 304 305 default: 306 abort(); 307 } 308 309 return (SNMP_ERR_NOERROR); 310 } 311