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>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
48 #include "trans_udp.h"
52 static int udp_start(void);
53 static int udp_stop(int);
54 static void udp_close_port(struct tport *);
55 static int udp_init_port(struct tport *);
56 static ssize_t udp_send(struct tport *, const u_char *, size_t,
57 const struct sockaddr *, size_t);
58 static ssize_t udp_recv(struct tport *, struct port_input *);
61 const struct transport_def udp_trans = {
63 OIDX_begemotSnmpdTransUdp,
71 static struct transport *my_trans;
76 return (trans_register(&udp_trans, &my_trans));
80 udp_stop(int force __unused)
83 if (trans_unregister(my_trans) != 0)
84 return (SNMP_ERR_GENERR);
85 return (SNMP_ERR_NOERROR);
92 udp_input(int fd __unused, void *udata)
94 struct udp_port *p = udata;
96 p->input.peerlen = sizeof(p->ret);
97 snmpd_input(&p->input, &p->tport);
101 * Create a UDP socket and bind it to the given port
104 udp_init_port(struct tport *tp)
106 struct udp_port *p = (struct udp_port *)tp;
107 struct sockaddr_in addr;
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);
115 ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) |
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,
125 syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
128 return (SNMP_ERR_GENERR);
130 if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) {
131 if (errno == EADDRNOTAVAIL) {
134 return (SNMP_ERR_INCONS_NAME);
136 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr),
140 return (SNMP_ERR_GENERR);
142 if ((p->input.id = fd_select(p->input.fd, udp_input,
146 return (SNMP_ERR_GENERR);
148 return (SNMP_ERR_NOERROR);
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.
156 udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp)
158 struct udp_port *port;
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));
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;
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;
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);
188 trans_insert_port(my_trans, &port->tport);
190 if (community != COMM_INITIALIZE &&
191 (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
192 udp_close_port(&port->tport);
196 return (SNMP_ERR_NOERROR);
203 udp_close_port(struct tport *tp)
205 struct udp_port *port = (struct udp_port *)tp;
207 snmpd_input_close(&port->input);
208 trans_remove_port(tp);
216 udp_send(struct tport *tp, const u_char *buf, size_t len,
217 const struct sockaddr *addr, size_t addrlen)
219 struct udp_port *p = (struct udp_port *)tp;
220 struct cmsghdr *cmsg;
221 struct in_addr *src_addr;
223 char cbuf[CMSG_SPACE(sizeof(struct in_addr))];
226 iov.iov_base = __DECONST(void*, buf);
232 msg.msg_name = __DECONST(void *, addr);
233 msg.msg_namelen = addrlen;
234 msg.msg_control = cbuf;
235 msg.msg_controllen = sizeof(cbuf);
237 cmsg = CMSG_FIRSTHDR(&msg);
238 cmsg->cmsg_level = IPPROTO_IP;
239 cmsg->cmsg_type = IP_SENDSRCADDR;
240 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
241 src_addr = (struct in_addr *)(void*)CMSG_DATA(cmsg);
242 memcpy(src_addr, &p->recv_addr, sizeof(struct in_addr));
244 return (sendmsg(p->input.fd, &msg, 0));
248 check_priv_dgram(struct port_input *pi, struct sockcred *cred)
251 /* process explicitly sends credentials */
253 pi->priv = (cred->sc_euid == 0);
259 * Input from a datagram socket.
260 * Each receive should return one datagram.
263 recv_dgram(struct port_input *pi, struct in_addr *laddr)
266 char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
267 CMSG_SPACE(sizeof(struct in_addr))];
271 struct cmsghdr *cmsg;
272 struct sockcred *cred = NULL;
274 if (pi->buf == NULL) {
275 /* no buffer yet - allocate one */
276 if ((pi->buf = buf_alloc(0)) == NULL) {
277 /* ups - could not get buffer. Read away input
279 (void)recvfrom(pi->fd, embuf, sizeof(embuf),
284 pi->buflen = buf_size(0);
287 /* try to get a message */
288 msg.msg_name = pi->peer;
289 msg.msg_namelen = pi->peerlen;
292 memset(cbuf, 0, sizeof(cbuf));
293 msg.msg_control = cbuf;
294 msg.msg_controllen = sizeof(cbuf);
297 iov[0].iov_base = pi->buf;
298 iov[0].iov_len = pi->buflen;
300 len = recvmsg(pi->fd, &msg, 0);
302 if (len == -1 || len == 0)
306 if (msg.msg_flags & MSG_TRUNC) {
307 /* truncated - drop */
308 snmpd_stats.silentDrops++;
309 snmpd_stats.inTooLong++;
313 pi->length = (size_t)len;
315 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
316 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
317 if (cmsg->cmsg_level == IPPROTO_IP &&
318 cmsg->cmsg_type == IP_RECVDSTADDR)
319 memcpy(laddr, CMSG_DATA(cmsg), sizeof(struct in_addr));
320 if (cmsg->cmsg_level == SOL_SOCKET &&
321 cmsg->cmsg_type == SCM_CREDS)
322 cred = (struct sockcred *)CMSG_DATA(cmsg);
326 check_priv_dgram(pi, cred);
335 udp_recv(struct tport *tp, struct port_input *pi)
337 struct udp_port *p = (struct udp_port *)tp;
338 struct cmsghdr *cmsgp;
339 struct in_addr *laddr;
341 char cbuf[CMSG_SPACE(sizeof(struct in_addr))];
344 memset(cbuf, 0, sizeof(cbuf));
346 msg.msg_control = cbuf;
347 msg.msg_controllen = sizeof(cbuf);
349 cmsgp = CMSG_FIRSTHDR(&msg);
350 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
351 cmsgp->cmsg_level = IPPROTO_IP;
352 cmsgp->cmsg_type = IP_SENDSRCADDR;
353 laddr = (struct in_addr *)CMSG_DATA(cmsgp);
355 ret = recv_dgram(pi, laddr);
357 memcpy(&p->recv_addr, laddr, sizeof(struct in_addr));
359 if (laddr->s_addr == INADDR_ANY) {
360 msg.msg_control = NULL;
361 msg.msg_controllen = 0;
371 op_snmp_port(struct snmp_context *ctx, struct snmp_value *value,
372 u_int sub, u_int iidx, enum snmp_op op)
374 asn_subid_t which = value->var.subs[sub-1];
381 case SNMP_OP_GETNEXT:
382 if ((p = (struct udp_port *)trans_next_port(my_trans,
383 &value->var, sub)) == NULL)
384 return (SNMP_ERR_NOSUCHNAME);
385 index_append(&value->var, sub, &p->tport.index);
389 if ((p = (struct udp_port *)trans_find_port(my_trans,
390 &value->var, sub)) == NULL)
391 return (SNMP_ERR_NOSUCHNAME);
395 p = (struct udp_port *)trans_find_port(my_trans,
397 ctx->scratch->int1 = (p != NULL);
399 if (which != LEAF_begemotSnmpdPortStatus)
401 if (!TRUTH_OK(value->v.integer))
402 return (SNMP_ERR_WRONG_VALUE);
404 ctx->scratch->int2 = TRUTH_GET(value->v.integer);
406 if (ctx->scratch->int2) {
407 /* open an SNMP port */
409 /* already open - do nothing */
410 return (SNMP_ERR_NOERROR);
412 if (index_decode(&value->var, sub, iidx, addr, &port))
413 return (SNMP_ERR_NO_CREATION);
414 return (udp_open_port(addr, port, &p));
417 /* close SNMP port - do in commit */
419 return (SNMP_ERR_NOERROR);
421 case SNMP_OP_ROLLBACK:
422 p = (struct udp_port *)trans_find_port(my_trans,
424 if (ctx->scratch->int1 == 0) {
426 if (ctx->scratch->int2 == 1) {
429 udp_close_port(&p->tport);
432 return (SNMP_ERR_NOERROR);
435 p = (struct udp_port *)trans_find_port(my_trans,
437 if (ctx->scratch->int1 == 1) {
439 if (ctx->scratch->int2 == 0) {
442 udp_close_port(&p->tport);
445 return (SNMP_ERR_NOERROR);
452 * Come here to fetch the value
456 case LEAF_begemotSnmpdPortStatus:
457 value->v.integer = 1;
464 return (SNMP_ERR_NOERROR);