trans_udp.c revision 312265
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#include <sys/ucred.h> 36 37#include <stdlib.h> 38#include <syslog.h> 39#include <string.h> 40#include <errno.h> 41#include <unistd.h> 42 43#include <netinet/in.h> 44#include <arpa/inet.h> 45 46#include "snmpmod.h" 47#include "snmpd.h" 48#include "trans_udp.h" 49#include "tree.h" 50#include "oid.h" 51 52static int udp_start(void); 53static int udp_stop(int); 54static void udp_close_port(struct tport *); 55static int udp_init_port(struct tport *); 56static ssize_t udp_send(struct tport *, const u_char *, size_t, 57 const struct sockaddr *, size_t); 58static ssize_t udp_recv(struct tport *, struct port_input *); 59 60/* exported */ 61const struct transport_def udp_trans = { 62 "udp", 63 OIDX_begemotSnmpdTransUdp, 64 udp_start, 65 udp_stop, 66 udp_close_port, 67 udp_init_port, 68 udp_send, 69 udp_recv 70}; 71static struct transport *my_trans; 72 73static int 74udp_start(void) 75{ 76 return (trans_register(&udp_trans, &my_trans)); 77} 78 79static int 80udp_stop(int force __unused) 81{ 82 if (my_trans != NULL) 83 if (trans_unregister(my_trans) != 0) 84 return (SNMP_ERR_GENERR); 85 return (SNMP_ERR_NOERROR); 86} 87 88/* 89 * A UDP port is ready 90 */ 91static void 92udp_input(int fd __unused, void *udata) 93{ 94 struct udp_port *p = udata; 95 96 p->input.peerlen = sizeof(p->ret); 97 snmpd_input(&p->input, &p->tport); 98} 99 100/* 101 * Create a UDP socket and bind it to the given port 102 */ 103static int 104udp_init_port(struct tport *tp) 105{ 106 struct udp_port *p = (struct udp_port *)tp; 107 struct sockaddr_in addr; 108 u_int32_t ip; 109 const int on = 1; 110 111 if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { 112 syslog(LOG_ERR, "creating UDP socket: %m"); 113 return (SNMP_ERR_RES_UNAVAIL); 114 } 115 ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) | 116 p->addr[3]; 117 memset(&addr, 0, sizeof(addr)); 118 addr.sin_addr.s_addr = htonl(ip); 119 addr.sin_port = htons(p->port); 120 addr.sin_family = AF_INET; 121 addr.sin_len = sizeof(addr); 122 if (addr.sin_addr.s_addr == INADDR_ANY && 123 setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on, 124 sizeof(on)) == -1) { 125 syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m"); 126 close(p->input.fd); 127 p->input.fd = -1; 128 return (SNMP_ERR_GENERR); 129 } 130 if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) { 131 if (errno == EADDRNOTAVAIL) { 132 close(p->input.fd); 133 p->input.fd = -1; 134 return (SNMP_ERR_INCONS_NAME); 135 } 136 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr), 137 p->port); 138 close(p->input.fd); 139 p->input.fd = -1; 140 return (SNMP_ERR_GENERR); 141 } 142 if ((p->input.id = fd_select(p->input.fd, udp_input, 143 p, NULL)) == NULL) { 144 close(p->input.fd); 145 p->input.fd = -1; 146 return (SNMP_ERR_GENERR); 147 } 148 return (SNMP_ERR_NOERROR); 149} 150 151/* 152 * Create a new SNMP Port object and start it, if we are not 153 * in initialization mode. The arguments are in host byte order. 154 */ 155static int 156udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp) 157{ 158 struct udp_port *port; 159 int err; 160 161 if (udp_port > 0xffff) 162 return (SNMP_ERR_NO_CREATION); 163 if ((port = malloc(sizeof(*port))) == NULL) 164 return (SNMP_ERR_GENERR); 165 memset(port, 0, sizeof(*port)); 166 167 /* initialize common part */ 168 port->tport.index.len = 5; 169 port->tport.index.subs[0] = addr[0]; 170 port->tport.index.subs[1] = addr[1]; 171 port->tport.index.subs[2] = addr[2]; 172 port->tport.index.subs[3] = addr[3]; 173 port->tport.index.subs[4] = udp_port; 174 175 port->addr[0] = addr[0]; 176 port->addr[1] = addr[1]; 177 port->addr[2] = addr[2]; 178 port->addr[3] = addr[3]; 179 port->port = udp_port; 180 181 port->input.fd = -1; 182 port->input.id = NULL; 183 port->input.stream = 0; 184 port->input.cred = 0; 185 port->input.peer = (struct sockaddr *)&port->ret; 186 port->input.peerlen = sizeof(port->ret); 187 188 trans_insert_port(my_trans, &port->tport); 189 190 if (community != COMM_INITIALIZE && 191 (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) { 192 udp_close_port(&port->tport); 193 return (err); 194 } 195 *pp = port; 196 return (SNMP_ERR_NOERROR); 197} 198 199/* 200 * Close an SNMP port 201 */ 202static void 203udp_close_port(struct tport *tp) 204{ 205 struct udp_port *port = (struct udp_port *)tp; 206 207 snmpd_input_close(&port->input); 208 trans_remove_port(tp); 209 free(port); 210} 211 212/* 213 * Send something 214 */ 215static ssize_t 216udp_send(struct tport *tp, const u_char *buf, size_t len, 217 const struct sockaddr *addr, size_t addrlen) 218{ 219 struct udp_port *p = (struct udp_port *)tp; 220 struct cmsghdr *cmsg; 221 struct in_addr *src_addr; 222 struct msghdr msg; 223 char cbuf[CMSG_SPACE(sizeof(struct in_addr))]; 224 struct iovec iov; 225 226 iov.iov_base = __DECONST(void*, buf); 227 iov.iov_len = len; 228 229 msg.msg_iov = &iov; 230 msg.msg_iovlen = 1; 231 msg.msg_name = __DECONST(void *, addr); 232 msg.msg_namelen = addrlen; 233 msg.msg_control = cbuf; 234 msg.msg_controllen = sizeof(cbuf); 235 236 cmsg = CMSG_FIRSTHDR(&msg); 237 cmsg->cmsg_level = IPPROTO_IP; 238 cmsg->cmsg_type = IP_SENDSRCADDR; 239 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 240 src_addr = (struct in_addr *)(void*)CMSG_DATA(cmsg); 241 memcpy(src_addr, &p->recv_addr, sizeof(struct in_addr)); 242 243 return (sendmsg(p->input.fd, &msg, 0)); 244} 245 246static void 247check_priv_dgram(struct port_input *pi, struct sockcred *cred) 248{ 249 250 /* process explicitly sends credentials */ 251 if (cred) 252 pi->priv = (cred->sc_euid == 0); 253 else 254 pi->priv = 0; 255} 256 257/* 258 * Input from a datagram socket. 259 * Each receive should return one datagram. 260 */ 261static ssize_t 262recv_dgram(struct port_input *pi, struct in_addr *laddr) 263{ 264 u_char embuf[1000]; 265 char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + 266 CMSG_SPACE(sizeof(struct in_addr))]; 267 struct msghdr msg; 268 struct iovec iov[1]; 269 ssize_t len; 270 struct cmsghdr *cmsg; 271 struct sockcred *cred = NULL; 272 273 if (pi->buf == NULL) { 274 /* no buffer yet - allocate one */ 275 if ((pi->buf = buf_alloc(0)) == NULL) { 276 /* ups - could not get buffer. Read away input 277 * and drop it */ 278 (void)recvfrom(pi->fd, embuf, sizeof(embuf), 279 0, NULL, NULL); 280 /* return error */ 281 return (-1); 282 } 283 pi->buflen = buf_size(0); 284 } 285 286 /* try to get a message */ 287 msg.msg_name = pi->peer; 288 msg.msg_namelen = pi->peerlen; 289 msg.msg_iov = iov; 290 msg.msg_iovlen = 1; 291 memset(cbuf, 0, sizeof(cbuf)); 292 msg.msg_control = cbuf; 293 msg.msg_controllen = sizeof(cbuf); 294 msg.msg_flags = 0; 295 296 iov[0].iov_base = pi->buf; 297 iov[0].iov_len = pi->buflen; 298 299 len = recvmsg(pi->fd, &msg, 0); 300 301 if (len == -1 || len == 0) 302 /* receive error */ 303 return (-1); 304 305 if (msg.msg_flags & MSG_TRUNC) { 306 /* truncated - drop */ 307 snmpd_stats.silentDrops++; 308 snmpd_stats.inTooLong++; 309 return (-1); 310 } 311 312 pi->length = (size_t)len; 313 314 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 315 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 316 if (cmsg->cmsg_level == IPPROTO_IP && 317 cmsg->cmsg_type == IP_RECVDSTADDR) 318 memcpy(laddr, CMSG_DATA(cmsg), sizeof(struct in_addr)); 319 if (cmsg->cmsg_level == SOL_SOCKET && 320 cmsg->cmsg_type == SCM_CREDS) 321 cred = (struct sockcred *)CMSG_DATA(cmsg); 322 } 323 324 if (pi->cred) 325 check_priv_dgram(pi, cred); 326 327 return (0); 328} 329 330/* 331 * Receive something 332 */ 333static ssize_t 334udp_recv(struct tport *tp, struct port_input *pi) 335{ 336 struct udp_port *p = (struct udp_port *)tp; 337 struct in_addr *laddr; 338 struct msghdr msg; 339 char cbuf[CMSG_SPACE(sizeof(struct in_addr))]; 340 struct cmsghdr *cmsgp; 341 ssize_t ret; 342 343 memset(cbuf, 0, sizeof(cbuf)); 344 345 msg.msg_control = cbuf; 346 msg.msg_controllen = sizeof(cbuf); 347 348 cmsgp = CMSG_FIRSTHDR(&msg); 349 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 350 cmsgp->cmsg_level = IPPROTO_IP; 351 cmsgp->cmsg_type = IP_SENDSRCADDR; 352 laddr = (struct in_addr *)CMSG_DATA(cmsgp); 353 354 ret = recv_dgram(pi, laddr); 355 356 memcpy(&p->recv_addr, laddr, sizeof(struct in_addr)); 357 358 if (laddr->s_addr == INADDR_ANY) { 359 msg.msg_control = NULL; 360 msg.msg_controllen = 0; 361 } 362 363 return (ret); 364} 365 366/* 367 * Port table 368 */ 369int 370op_snmp_port(struct snmp_context *ctx, struct snmp_value *value, 371 u_int sub, u_int iidx, enum snmp_op op) 372{ 373 asn_subid_t which = value->var.subs[sub-1]; 374 struct udp_port *p; 375 u_int8_t addr[4]; 376 u_int32_t port; 377 378 switch (op) { 379 380 case SNMP_OP_GETNEXT: 381 if ((p = (struct udp_port *)trans_next_port(my_trans, 382 &value->var, sub)) == NULL) 383 return (SNMP_ERR_NOSUCHNAME); 384 index_append(&value->var, sub, &p->tport.index); 385 break; 386 387 case SNMP_OP_GET: 388 if ((p = (struct udp_port *)trans_find_port(my_trans, 389 &value->var, sub)) == NULL) 390 return (SNMP_ERR_NOSUCHNAME); 391 break; 392 393 case SNMP_OP_SET: 394 p = (struct udp_port *)trans_find_port(my_trans, 395 &value->var, sub); 396 ctx->scratch->int1 = (p != NULL); 397 398 if (which != LEAF_begemotSnmpdPortStatus) 399 abort(); 400 if (!TRUTH_OK(value->v.integer)) 401 return (SNMP_ERR_WRONG_VALUE); 402 403 ctx->scratch->int2 = TRUTH_GET(value->v.integer); 404 405 if (ctx->scratch->int2) { 406 /* open an SNMP port */ 407 if (p != NULL) 408 /* already open - do nothing */ 409 return (SNMP_ERR_NOERROR); 410 411 if (index_decode(&value->var, sub, iidx, addr, &port)) 412 return (SNMP_ERR_NO_CREATION); 413 return (udp_open_port(addr, port, &p)); 414 415 } else { 416 /* close SNMP port - do in commit */ 417 } 418 return (SNMP_ERR_NOERROR); 419 420 case SNMP_OP_ROLLBACK: 421 p = (struct udp_port *)trans_find_port(my_trans, 422 &value->var, sub); 423 if (ctx->scratch->int1 == 0) { 424 /* did not exist */ 425 if (ctx->scratch->int2 == 1) { 426 /* created */ 427 if (p != NULL) 428 udp_close_port(&p->tport); 429 } 430 } 431 return (SNMP_ERR_NOERROR); 432 433 case SNMP_OP_COMMIT: 434 p = (struct udp_port *)trans_find_port(my_trans, 435 &value->var, sub); 436 if (ctx->scratch->int1 == 1) { 437 /* did exist */ 438 if (ctx->scratch->int2 == 0) { 439 /* delete */ 440 if (p != NULL) 441 udp_close_port(&p->tport); 442 } 443 } 444 return (SNMP_ERR_NOERROR); 445 446 default: 447 abort(); 448 } 449 450 /* 451 * Come here to fetch the value 452 */ 453 switch (which) { 454 455 case LEAF_begemotSnmpdPortStatus: 456 value->v.integer = 1; 457 break; 458 459 default: 460 abort(); 461 } 462 463 return (SNMP_ERR_NOERROR); 464} 465