1254721Semaste/* 2254721Semaste * Copyright (c) 2003 3254721Semaste * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4254721Semaste * All rights reserved. 5254721Semaste * 6254721Semaste * Author: Harti Brandt <harti@freebsd.org> 7254721Semaste * 8254721Semaste * Redistribution and use in source and binary forms, with or without 9254721Semaste * modification, are permitted provided that the following conditions 10254721Semaste * are met: 11254721Semaste * 1. Redistributions of source code must retain the above copyright 12254721Semaste * notice, this list of conditions and the following disclaimer. 13254721Semaste * 2. Redistributions in binary form must reproduce the above copyright 14254721Semaste * notice, this list of conditions and the following disclaimer in the 15254721Semaste * documentation and/or other materials provided with the distribution. 16254721Semaste * 17254721Semaste * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19296417Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20254721Semaste * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27254721Semaste * SUCH DAMAGE. 28254721Semaste * 29254721Semaste * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $ 30254721Semaste * 31254721Semaste * UDP transport 32254721Semaste */ 33254721Semaste#include <sys/types.h> 34254721Semaste#include <sys/queue.h> 35254721Semaste 36254721Semaste#include <stdlib.h> 37254721Semaste#include <syslog.h> 38254721Semaste#include <string.h> 39254721Semaste#include <errno.h> 40254721Semaste#include <unistd.h> 41254721Semaste 42254721Semaste#include <netinet/in.h> 43254721Semaste#include <arpa/inet.h> 44254721Semaste 45254721Semaste#include "snmpmod.h" 46254721Semaste#include "snmpd.h" 47296417Sdim#include "trans_udp.h" 48254721Semaste#include "tree.h" 49254721Semaste#include "oid.h" 50254721Semaste 51254721Semastestatic int udp_start(void); 52254721Semastestatic int udp_stop(int); 53254721Semastestatic void udp_close_port(struct tport *); 54254721Semastestatic int udp_init_port(struct tport *); 55254721Semastestatic ssize_t udp_send(struct tport *, const u_char *, size_t, 56254721Semaste const struct sockaddr *, size_t); 57254721Semaste 58254721Semaste/* exported */ 59254721Semasteconst struct transport_def udp_trans = { 60254721Semaste "udp", 61254721Semaste OIDX_begemotSnmpdTransUdp, 62254721Semaste udp_start, 63254721Semaste udp_stop, 64254721Semaste udp_close_port, 65296417Sdim udp_init_port, 66296417Sdim udp_send 67296417Sdim}; 68254721Semastestatic struct transport *my_trans; 69254721Semaste 70288943Sdimstatic int 71288943Sdimudp_start(void) 72288943Sdim{ 73288943Sdim return (trans_register(&udp_trans, &my_trans)); 74254721Semaste} 75254721Semaste 76254721Semastestatic int 77254721Semasteudp_stop(int force __unused) 78254721Semaste{ 79254721Semaste if (my_trans != NULL) 80254721Semaste if (trans_unregister(my_trans) != 0) 81254721Semaste return (SNMP_ERR_GENERR); 82254721Semaste return (SNMP_ERR_NOERROR); 83254721Semaste} 84254721Semaste 85254721Semaste/* 86254721Semaste * A UDP port is ready 87254721Semaste */ 88254721Semastestatic void 89254721Semasteudp_input(int fd __unused, void *udata) 90254721Semaste{ 91254721Semaste struct udp_port *p = udata; 92254721Semaste 93254721Semaste p->input.peerlen = sizeof(p->ret); 94254721Semaste snmpd_input(&p->input, &p->tport); 95254721Semaste} 96254721Semaste 97254721Semaste/* 98254721Semaste * Create a UDP socket and bind it to the given port 99254721Semaste */ 100254721Semastestatic int 101296417Sdimudp_init_port(struct tport *tp) 102254721Semaste{ 103254721Semaste struct udp_port *p = (struct udp_port *)tp; 104254721Semaste struct sockaddr_in addr; 105254721Semaste u_int32_t ip; 106254721Semaste const int on = 1; 107254721Semaste 108254721Semaste if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { 109254721Semaste syslog(LOG_ERR, "creating UDP socket: %m"); 110254721Semaste return (SNMP_ERR_RES_UNAVAIL); 111254721Semaste } 112254721Semaste ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) | 113254721Semaste p->addr[3]; 114296417Sdim memset(&addr, 0, sizeof(addr)); 115254721Semaste addr.sin_addr.s_addr = htonl(ip); 116254721Semaste addr.sin_port = htons(p->port); 117254721Semaste addr.sin_family = AF_INET; 118254721Semaste addr.sin_len = sizeof(addr); 119254721Semaste if (addr.sin_addr.s_addr == INADDR_ANY && 120254721Semaste setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on, 121254721Semaste sizeof(on)) == -1) { 122254721Semaste syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m"); 123254721Semaste close(p->input.fd); 124254721Semaste p->input.fd = -1; 125254721Semaste return (SNMP_ERR_GENERR); 126254721Semaste } 127254721Semaste if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) { 128254721Semaste if (errno == EADDRNOTAVAIL) { 129296417Sdim close(p->input.fd); 130254721Semaste p->input.fd = -1; 131254721Semaste return (SNMP_ERR_INCONS_NAME); 132296417Sdim } 133254721Semaste syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr), 134296417Sdim p->port); 135296417Sdim close(p->input.fd); 136296417Sdim p->input.fd = -1; 137296417Sdim return (SNMP_ERR_GENERR); 138254721Semaste } 139254721Semaste if ((p->input.id = fd_select(p->input.fd, udp_input, 140296417Sdim p, NULL)) == NULL) { 141254721Semaste close(p->input.fd); 142254721Semaste p->input.fd = -1; 143254721Semaste return (SNMP_ERR_GENERR); 144254721Semaste } 145254721Semaste return (SNMP_ERR_NOERROR); 146254721Semaste} 147254721Semaste 148254721Semaste/* 149254721Semaste * Create a new SNMP Port object and start it, if we are not 150254721Semaste * in initialization mode. The arguments are in host byte order. 151254721Semaste */ 152254721Semastestatic int 153254721Semasteudp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp) 154254721Semaste{ 155254721Semaste struct udp_port *port; 156254721Semaste int err; 157254721Semaste 158254721Semaste if (udp_port > 0xffff) 159254721Semaste return (SNMP_ERR_NO_CREATION); 160254721Semaste if ((port = malloc(sizeof(*port))) == NULL) 161254721Semaste return (SNMP_ERR_GENERR); 162254721Semaste memset(port, 0, sizeof(*port)); 163254721Semaste 164254721Semaste /* initialize common part */ 165254721Semaste port->tport.index.len = 5; 166254721Semaste port->tport.index.subs[0] = addr[0]; 167254721Semaste port->tport.index.subs[1] = addr[1]; 168254721Semaste port->tport.index.subs[2] = addr[2]; 169254721Semaste port->tport.index.subs[3] = addr[3]; 170254721Semaste port->tport.index.subs[4] = udp_port; 171254721Semaste 172254721Semaste port->addr[0] = addr[0]; 173254721Semaste port->addr[1] = addr[1]; 174254721Semaste port->addr[2] = addr[2]; 175288943Sdim port->addr[3] = addr[3]; 176288943Sdim port->port = udp_port; 177288943Sdim 178254721Semaste port->input.fd = -1; 179254721Semaste port->input.id = NULL; 180254721Semaste port->input.stream = 0; 181254721Semaste port->input.cred = 0; 182254721Semaste port->input.peer = (struct sockaddr *)&port->ret; 183254721Semaste port->input.peerlen = sizeof(port->ret); 184254721Semaste 185254721Semaste trans_insert_port(my_trans, &port->tport); 186254721Semaste 187276479Sdim if (community != COMM_INITIALIZE && 188276479Sdim (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) { 189276479Sdim udp_close_port(&port->tport); 190276479Sdim return (err); 191276479Sdim } 192276479Sdim *pp = port; 193276479Sdim return (SNMP_ERR_NOERROR); 194254721Semaste} 195254721Semaste 196296417Sdim/* 197296417Sdim * Close an SNMP port 198254721Semaste */ 199296417Sdimstatic void 200296417Sdimudp_close_port(struct tport *tp) 201254721Semaste{ 202254721Semaste struct udp_port *port = (struct udp_port *)tp; 203254721Semaste 204254721Semaste snmpd_input_close(&port->input); 205254721Semaste trans_remove_port(tp); 206254721Semaste free(port); 207254721Semaste} 208254721Semaste 209254721Semaste/* 210254721Semaste * Send something 211254721Semaste */ 212254721Semastestatic ssize_t 213254721Semasteudp_send(struct tport *tp, const u_char *buf, size_t len, 214254721Semaste const struct sockaddr *addr, size_t addrlen) 215254721Semaste{ 216254721Semaste struct udp_port *p = (struct udp_port *)tp; 217254721Semaste 218254721Semaste return (sendto(p->input.fd, buf, len, 0, addr, addrlen)); 219254721Semaste} 220254721Semaste 221254721Semaste/* 222254721Semaste * Port table 223254721Semaste */ 224296417Sdimint 225op_snmp_port(struct snmp_context *ctx, struct snmp_value *value, 226 u_int sub, u_int iidx, enum snmp_op op) 227{ 228 asn_subid_t which = value->var.subs[sub-1]; 229 struct udp_port *p; 230 u_int8_t addr[4]; 231 u_int32_t port; 232 233 switch (op) { 234 235 case SNMP_OP_GETNEXT: 236 if ((p = (struct udp_port *)trans_next_port(my_trans, 237 &value->var, sub)) == NULL) 238 return (SNMP_ERR_NOSUCHNAME); 239 index_append(&value->var, sub, &p->tport.index); 240 break; 241 242 case SNMP_OP_GET: 243 if ((p = (struct udp_port *)trans_find_port(my_trans, 244 &value->var, sub)) == NULL) 245 return (SNMP_ERR_NOSUCHNAME); 246 break; 247 248 case SNMP_OP_SET: 249 p = (struct udp_port *)trans_find_port(my_trans, 250 &value->var, sub); 251 ctx->scratch->int1 = (p != NULL); 252 253 if (which != LEAF_begemotSnmpdPortStatus) 254 abort(); 255 if (!TRUTH_OK(value->v.integer)) 256 return (SNMP_ERR_WRONG_VALUE); 257 258 ctx->scratch->int2 = TRUTH_GET(value->v.integer); 259 260 if (ctx->scratch->int2) { 261 /* open an SNMP port */ 262 if (p != NULL) 263 /* already open - do nothing */ 264 return (SNMP_ERR_NOERROR); 265 266 if (index_decode(&value->var, sub, iidx, addr, &port)) 267 return (SNMP_ERR_NO_CREATION); 268 return (udp_open_port(addr, port, &p)); 269 270 } else { 271 /* close SNMP port - do in commit */ 272 } 273 return (SNMP_ERR_NOERROR); 274 275 case SNMP_OP_ROLLBACK: 276 p = (struct udp_port *)trans_find_port(my_trans, 277 &value->var, sub); 278 if (ctx->scratch->int1 == 0) { 279 /* did not exist */ 280 if (ctx->scratch->int2 == 1) { 281 /* created */ 282 if (p != NULL) 283 udp_close_port(&p->tport); 284 } 285 } 286 return (SNMP_ERR_NOERROR); 287 288 case SNMP_OP_COMMIT: 289 p = (struct udp_port *)trans_find_port(my_trans, 290 &value->var, sub); 291 if (ctx->scratch->int1 == 1) { 292 /* did exist */ 293 if (ctx->scratch->int2 == 0) { 294 /* delete */ 295 if (p != NULL) 296 udp_close_port(&p->tport); 297 } 298 } 299 return (SNMP_ERR_NOERROR); 300 301 default: 302 abort(); 303 } 304 305 /* 306 * Come here to fetch the value 307 */ 308 switch (which) { 309 310 case LEAF_begemotSnmpdPortStatus: 311 value->v.integer = 1; 312 break; 313 314 default: 315 abort(); 316 } 317 318 return (SNMP_ERR_NOERROR); 319} 320