3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
29 * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/ucred.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
49 #include "trans_udp.h"
53 static int udp_start(void);
54 static int udp_stop(int);
55 static void udp_close_port(struct tport *);
56 static int udp_init_port(struct tport *);
57 static ssize_t udp_send(struct tport *, const u_char *, size_t,
58 const struct sockaddr *, size_t);
59 static ssize_t udp_recv(struct tport *, struct port_input *);
62 const struct transport_def udp_trans = {
64 OIDX_begemotSnmpdTransUdp,
72 static struct transport *my_trans;
77 return (trans_register(&udp_trans, &my_trans));
81 udp_stop(int force __unused)
84 if (trans_unregister(my_trans) != 0)
85 return (SNMP_ERR_GENERR);
86 return (SNMP_ERR_NOERROR);
93 udp_input(int fd __unused, void *udata)
95 struct udp_port *p = udata;
97 p->input.peerlen = sizeof(p->ret);
98 snmpd_input(&p->input, &p->tport);
102 * Create a UDP socket and bind it to the given port
105 udp_init_port(struct tport *tp)
107 struct udp_port *p = (struct udp_port *)tp;
108 struct sockaddr_in addr;
112 if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
113 syslog(LOG_ERR, "creating UDP socket: %m");
114 return (SNMP_ERR_RES_UNAVAIL);
116 ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) |
118 memset(&addr, 0, sizeof(addr));
119 addr.sin_addr.s_addr = htonl(ip);
120 addr.sin_port = htons(p->port);
121 addr.sin_family = AF_INET;
122 addr.sin_len = sizeof(addr);
123 if (addr.sin_addr.s_addr == INADDR_ANY) {
124 if (setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on,
126 syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
129 return (SNMP_ERR_GENERR);
131 p->recvdstaddr = true;
133 if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) {
134 if (errno == EADDRNOTAVAIL) {
137 return (SNMP_ERR_INCONS_NAME);
139 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr),
143 return (SNMP_ERR_GENERR);
145 if ((p->input.id = fd_select(p->input.fd, udp_input,
149 return (SNMP_ERR_GENERR);
151 return (SNMP_ERR_NOERROR);
155 * Create a new SNMP Port object and start it, if we are not
156 * in initialization mode. The arguments are in host byte order.
159 udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp)
161 struct udp_port *port;
164 if (udp_port > 0xffff)
165 return (SNMP_ERR_NO_CREATION);
166 if ((port = malloc(sizeof(*port))) == NULL)
167 return (SNMP_ERR_GENERR);
168 memset(port, 0, sizeof(*port));
170 /* initialize common part */
171 port->tport.index.len = 5;
172 port->tport.index.subs[0] = addr[0];
173 port->tport.index.subs[1] = addr[1];
174 port->tport.index.subs[2] = addr[2];
175 port->tport.index.subs[3] = addr[3];
176 port->tport.index.subs[4] = udp_port;
178 port->addr[0] = addr[0];
179 port->addr[1] = addr[1];
180 port->addr[2] = addr[2];
181 port->addr[3] = addr[3];
182 port->port = udp_port;
185 port->input.id = NULL;
186 port->input.stream = 0;
187 port->input.cred = 0;
188 port->input.peer = (struct sockaddr *)&port->ret;
189 port->input.peerlen = sizeof(port->ret);
191 trans_insert_port(my_trans, &port->tport);
193 if (community != COMM_INITIALIZE &&
194 (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
195 udp_close_port(&port->tport);
199 return (SNMP_ERR_NOERROR);
206 udp_close_port(struct tport *tp)
208 struct udp_port *port = (struct udp_port *)tp;
210 snmpd_input_close(&port->input);
211 trans_remove_port(tp);
219 udp_send(struct tport *tp, const u_char *buf, size_t len,
220 const struct sockaddr *addr, size_t addrlen)
222 struct udp_port *p = (struct udp_port *)tp;
223 struct cmsghdr *cmsg;
225 char cbuf[CMSG_SPACE(sizeof(struct in_addr))];
228 iov.iov_base = __DECONST(void*, buf);
234 msg.msg_name = __DECONST(void *, addr);
235 msg.msg_namelen = addrlen;
237 if (p->recvdstaddr) {
238 msg.msg_control = cbuf;
239 msg.msg_controllen = sizeof(cbuf);
241 cmsg = CMSG_FIRSTHDR(&msg);
242 cmsg->cmsg_level = IPPROTO_IP;
243 cmsg->cmsg_type = IP_SENDSRCADDR;
244 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
245 memcpy(CMSG_DATA(cmsg), &p->dstaddr, sizeof(struct in_addr));
247 msg.msg_control = NULL;
248 msg.msg_controllen = 0;
251 return (sendmsg(p->input.fd, &msg, 0));
255 check_priv_dgram(struct port_input *pi, struct sockcred *cred)
258 /* process explicitly sends credentials */
260 pi->priv = (cred->sc_euid == 0);
266 * Input from a datagram socket.
267 * Each receive should return one datagram.
270 udp_recv(struct tport *tp, struct port_input *pi)
273 char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
274 CMSG_SPACE(sizeof(struct in_addr))];
275 struct udp_port *p = (struct udp_port *)tp;
279 struct cmsghdr *cmsg;
280 struct sockcred *cred = NULL;
282 if (pi->buf == NULL) {
283 /* no buffer yet - allocate one */
284 if ((pi->buf = buf_alloc(0)) == NULL) {
285 /* ups - could not get buffer. Read away input
287 (void)recvfrom(pi->fd, embuf, sizeof(embuf),
292 pi->buflen = buf_size(0);
295 /* try to get a message */
296 msg.msg_name = pi->peer;
297 msg.msg_namelen = pi->peerlen;
300 memset(cbuf, 0, sizeof(cbuf));
301 msg.msg_control = cbuf;
302 msg.msg_controllen = sizeof(cbuf);
305 iov[0].iov_base = pi->buf;
306 iov[0].iov_len = pi->buflen;
308 len = recvmsg(pi->fd, &msg, 0);
310 if (len == -1 || len == 0)
314 if (msg.msg_flags & MSG_TRUNC) {
315 /* truncated - drop */
316 snmpd_stats.silentDrops++;
317 snmpd_stats.inTooLong++;
321 pi->length = (size_t)len;
323 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
324 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
325 if (cmsg->cmsg_level == IPPROTO_IP &&
326 cmsg->cmsg_type == IP_RECVDSTADDR)
327 memcpy(&p->dstaddr, CMSG_DATA(cmsg),
328 sizeof(struct in_addr));
329 if (cmsg->cmsg_level == SOL_SOCKET &&
330 cmsg->cmsg_type == SCM_CREDS)
331 cred = (struct sockcred *)CMSG_DATA(cmsg);
335 check_priv_dgram(pi, cred);
344 op_snmp_port(struct snmp_context *ctx, struct snmp_value *value,
345 u_int sub, u_int iidx, enum snmp_op op)
347 asn_subid_t which = value->var.subs[sub-1];
354 case SNMP_OP_GETNEXT:
355 if ((p = (struct udp_port *)trans_next_port(my_trans,
356 &value->var, sub)) == NULL)
357 return (SNMP_ERR_NOSUCHNAME);
358 index_append(&value->var, sub, &p->tport.index);
362 if ((p = (struct udp_port *)trans_find_port(my_trans,
363 &value->var, sub)) == NULL)
364 return (SNMP_ERR_NOSUCHNAME);
368 p = (struct udp_port *)trans_find_port(my_trans,
370 ctx->scratch->int1 = (p != NULL);
372 if (which != LEAF_begemotSnmpdPortStatus)
374 if (!TRUTH_OK(value->v.integer))
375 return (SNMP_ERR_WRONG_VALUE);
377 ctx->scratch->int2 = TRUTH_GET(value->v.integer);
379 if (ctx->scratch->int2) {
380 /* open an SNMP port */
382 /* already open - do nothing */
383 return (SNMP_ERR_NOERROR);
385 if (index_decode(&value->var, sub, iidx, addr, &port))
386 return (SNMP_ERR_NO_CREATION);
387 return (udp_open_port(addr, port, &p));
390 /* close SNMP port - do in commit */
392 return (SNMP_ERR_NOERROR);
394 case SNMP_OP_ROLLBACK:
395 p = (struct udp_port *)trans_find_port(my_trans,
397 if (ctx->scratch->int1 == 0) {
399 if (ctx->scratch->int2 == 1) {
402 udp_close_port(&p->tport);
405 return (SNMP_ERR_NOERROR);
408 p = (struct udp_port *)trans_find_port(my_trans,
410 if (ctx->scratch->int1 == 1) {
412 if (ctx->scratch->int2 == 0) {
415 udp_close_port(&p->tport);
418 return (SNMP_ERR_NOERROR);
425 * Come here to fetch the value
429 case LEAF_begemotSnmpdPortStatus:
430 value->v.integer = 1;
437 return (SNMP_ERR_NOERROR);