trans_udp.c revision 312516
1193326Sed/*
2193326Sed * Copyright (c) 2003
3193326Sed *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4193326Sed *	All rights reserved.
5193326Sed *
6193326Sed * Author: Harti Brandt <harti@freebsd.org>
7193326Sed *
8193326Sed * Redistribution and use in source and binary forms, with or without
9193326Sed * modification, are permitted provided that the following conditions
10198092Srdivacky * are met:
11193326Sed * 1. Redistributions of source code must retain the above copyright
12193326Sed *    notice, this list of conditions and the following disclaimer.
13193326Sed * 2. Redistributions in binary form must reproduce the above copyright
14193326Sed *    notice, this list of conditions and the following disclaimer in the
15249423Sdim *    documentation and/or other materials provided with the distribution.
16206084Srdivacky *
17249423Sdim * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18206084Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19234353Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193326Sed * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21249423Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25263508Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26249423Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27249423Sdim * SUCH DAMAGE.
28249423Sdim *
29193326Sed * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $
30193326Sed *
31193326Sed * UDP transport
32251662Sdim */
33251662Sdim#include <sys/types.h>
34251662Sdim#include <sys/queue.h>
35251662Sdim#include <sys/ucred.h>
36251662Sdim
37224145Sdim#include <stdbool.h>
38193326Sed#include <stdlib.h>
39193326Sed#include <syslog.h>
40193326Sed#include <string.h>
41198092Srdivacky#include <errno.h>
42198092Srdivacky#include <unistd.h>
43193326Sed
44193326Sed#include <netinet/in.h>
45202379Srdivacky#include <arpa/inet.h>
46202379Srdivacky
47202379Srdivacky#include "snmpmod.h"
48202379Srdivacky#include "snmpd.h"
49193326Sed#include "trans_udp.h"
50193326Sed#include "tree.h"
51224145Sdim#include "oid.h"
52224145Sdim
53226633Sdimstatic int udp_start(void);
54234353Sdimstatic int udp_stop(int);
55221345Sdimstatic void udp_close_port(struct tport *);
56221345Sdimstatic int udp_init_port(struct tport *);
57221345Sdimstatic ssize_t udp_send(struct tport *, const u_char *, size_t,
58221345Sdim    const struct sockaddr *, size_t);
59221345Sdimstatic ssize_t udp_recv(struct tport *, struct port_input *);
60221345Sdim
61221345Sdim/* exported */
62221345Sdimconst struct transport_def udp_trans = {
63221345Sdim	"udp",
64249423Sdim	OIDX_begemotSnmpdTransUdp,
65221345Sdim	udp_start,
66221345Sdim	udp_stop,
67221345Sdim	udp_close_port,
68221345Sdim	udp_init_port,
69221345Sdim	udp_send,
70221345Sdim	udp_recv
71249423Sdim};
72221345Sdimstatic struct transport *my_trans;
73221345Sdim
74221345Sdimstatic int
75221345Sdimudp_start(void)
76210299Sed{
77221345Sdim	return (trans_register(&udp_trans, &my_trans));
78221345Sdim}
79221345Sdim
80224145Sdimstatic int
81221345Sdimudp_stop(int force __unused)
82221345Sdim{
83193326Sed	if (my_trans != NULL)
84193326Sed		if (trans_unregister(my_trans) != 0)
85193326Sed			return (SNMP_ERR_GENERR);
86193326Sed	return (SNMP_ERR_NOERROR);
87224145Sdim}
88224145Sdim
89198092Srdivacky/*
90193326Sed * A UDP port is ready
91203955Srdivacky */
92193326Sedstatic void
93198092Srdivackyudp_input(int fd __unused, void *udata)
94193326Sed{
95198092Srdivacky	struct udp_port *p = udata;
96198092Srdivacky
97224145Sdim	p->input.peerlen = sizeof(p->ret);
98198092Srdivacky	snmpd_input(&p->input, &p->tport);
99224145Sdim}
100224145Sdim
101224145Sdim/*
102224145Sdim * Create a UDP socket and bind it to the given port
103224145Sdim */
104224145Sdimstatic int
105224145Sdimudp_init_port(struct tport *tp)
106193326Sed{
107193326Sed	struct udp_port *p = (struct udp_port *)tp;
108224145Sdim	struct sockaddr_in addr;
109224145Sdim	u_int32_t ip;
110224145Sdim	const int on = 1;
111224145Sdim
112224145Sdim	if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
113224145Sdim		syslog(LOG_ERR, "creating UDP socket: %m");
114224145Sdim		return (SNMP_ERR_RES_UNAVAIL);
115224145Sdim	}
116224145Sdim	ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) |
117224145Sdim	    p->addr[3];
118224145Sdim	memset(&addr, 0, sizeof(addr));
119224145Sdim	addr.sin_addr.s_addr = htonl(ip);
120224145Sdim	addr.sin_port = htons(p->port);
121224145Sdim	addr.sin_family = AF_INET;
122224145Sdim	addr.sin_len = sizeof(addr);
123224145Sdim	if (addr.sin_addr.s_addr == INADDR_ANY) {
124224145Sdim		if (setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on,
125224145Sdim		    sizeof(on)) == -1) {
126224145Sdim			syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
127224145Sdim			close(p->input.fd);
128224145Sdim			p->input.fd = -1;
129224145Sdim			return (SNMP_ERR_GENERR);
130224145Sdim		}
131224145Sdim		p->recvdstaddr = true;
132224145Sdim	}
133224145Sdim	if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) {
134224145Sdim		if (errno == EADDRNOTAVAIL) {
135224145Sdim			close(p->input.fd);
136224145Sdim			p->input.fd = -1;
137224145Sdim			return (SNMP_ERR_INCONS_NAME);
138224145Sdim		}
139224145Sdim		syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr),
140224145Sdim		    p->port);
141224145Sdim		close(p->input.fd);
142224145Sdim		p->input.fd = -1;
143224145Sdim		return (SNMP_ERR_GENERR);
144224145Sdim	}
145224145Sdim	if ((p->input.id = fd_select(p->input.fd, udp_input,
146224145Sdim	    p, NULL)) == NULL) {
147224145Sdim		close(p->input.fd);
148224145Sdim		p->input.fd = -1;
149224145Sdim		return (SNMP_ERR_GENERR);
150224145Sdim	}
151224145Sdim	return (SNMP_ERR_NOERROR);
152224145Sdim}
153193326Sed
154193326Sed/*
155224145Sdim * Create a new SNMP Port object and start it, if we are not
156224145Sdim * in initialization mode. The arguments are in host byte order.
157224145Sdim */
158224145Sdimstatic int
159224145Sdimudp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp)
160224145Sdim{
161224145Sdim	struct udp_port *port;
162224145Sdim	int err;
163224145Sdim
164224145Sdim	if (udp_port > 0xffff)
165224145Sdim		return (SNMP_ERR_NO_CREATION);
166224145Sdim	if ((port = malloc(sizeof(*port))) == NULL)
167224145Sdim		return (SNMP_ERR_GENERR);
168224145Sdim	memset(port, 0, sizeof(*port));
169224145Sdim
170198092Srdivacky	/* initialize common part */
171224145Sdim	port->tport.index.len = 5;
172224145Sdim	port->tport.index.subs[0] = addr[0];
173224145Sdim	port->tport.index.subs[1] = addr[1];
174224145Sdim	port->tport.index.subs[2] = addr[2];
175224145Sdim	port->tport.index.subs[3] = addr[3];
176198092Srdivacky	port->tport.index.subs[4] = udp_port;
177193326Sed
178224145Sdim	port->addr[0] = addr[0];
179224145Sdim	port->addr[1] = addr[1];
180224145Sdim	port->addr[2] = addr[2];
181224145Sdim	port->addr[3] = addr[3];
182224145Sdim	port->port = udp_port;
183224145Sdim
184224145Sdim	port->input.fd = -1;
185224145Sdim	port->input.id = NULL;
186224145Sdim	port->input.stream = 0;
187224145Sdim	port->input.cred = 0;
188193326Sed	port->input.peer = (struct sockaddr *)&port->ret;
189193326Sed	port->input.peerlen = sizeof(port->ret);
190224145Sdim
191224145Sdim	trans_insert_port(my_trans, &port->tport);
192224145Sdim
193224145Sdim	if (community != COMM_INITIALIZE &&
194224145Sdim	    (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
195224145Sdim		udp_close_port(&port->tport);
196224145Sdim		return (err);
197224145Sdim	}
198224145Sdim	*pp = port;
199234353Sdim	return (SNMP_ERR_NOERROR);
200234353Sdim}
201234353Sdim
202224145Sdim/*
203224145Sdim * Close an SNMP port
204224145Sdim */
205224145Sdimstatic void
206224145Sdimudp_close_port(struct tport *tp)
207224145Sdim{
208224145Sdim	struct udp_port *port = (struct udp_port *)tp;
209224145Sdim
210224145Sdim	snmpd_input_close(&port->input);
211224145Sdim	trans_remove_port(tp);
212224145Sdim	free(port);
213224145Sdim}
214224145Sdim
215224145Sdim/*
216224145Sdim * Send something
217224145Sdim */
218224145Sdimstatic ssize_t
219224145Sdimudp_send(struct tport *tp, const u_char *buf, size_t len,
220224145Sdim    const struct sockaddr *addr, size_t addrlen)
221224145Sdim{
222224145Sdim	struct udp_port *p = (struct udp_port *)tp;
223224145Sdim	struct cmsghdr *cmsg;
224224145Sdim	struct msghdr msg;
225224145Sdim	char cbuf[CMSG_SPACE(sizeof(struct in_addr))];
226224145Sdim	struct iovec iov;
227224145Sdim
228224145Sdim	iov.iov_base = __DECONST(void*, buf);
229224145Sdim	iov.iov_len = len;
230224145Sdim
231224145Sdim	msg.msg_flags = 0;
232224145Sdim	msg.msg_iov = &iov;
233224145Sdim	msg.msg_iovlen = 1;
234224145Sdim	msg.msg_name = __DECONST(void *, addr);
235224145Sdim	msg.msg_namelen = addrlen;
236224145Sdim
237224145Sdim	if (p->recvdstaddr) {
238224145Sdim		msg.msg_control = cbuf;
239224145Sdim		msg.msg_controllen = sizeof(cbuf);
240224145Sdim
241224145Sdim		cmsg = CMSG_FIRSTHDR(&msg);
242224145Sdim		cmsg->cmsg_level = IPPROTO_IP;
243224145Sdim		cmsg->cmsg_type = IP_SENDSRCADDR;
244224145Sdim		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
245224145Sdim		memcpy(CMSG_DATA(cmsg), &p->dstaddr, sizeof(struct in_addr));
246224145Sdim	} else {
247224145Sdim		msg.msg_control = NULL;
248224145Sdim		msg.msg_controllen = 0;
249224145Sdim	}
250193326Sed
251224145Sdim	return (sendmsg(p->input.fd, &msg, 0));
252193326Sed}
253224145Sdim
254224145Sdimstatic void
255224145Sdimcheck_priv_dgram(struct port_input *pi, struct sockcred *cred)
256224145Sdim{
257224145Sdim
258224145Sdim	/* process explicitly sends credentials */
259224145Sdim	if (cred)
260224145Sdim		pi->priv = (cred->sc_euid == 0);
261224145Sdim	else
262224145Sdim		pi->priv = 0;
263263508Sdim}
264263508Sdim
265263508Sdim/*
266263508Sdim * Input from a datagram socket.
267263508Sdim * Each receive should return one datagram.
268193326Sed */
269193326Sedstatic ssize_t
270224145Sdimudp_recv(struct tport *tp, struct port_input *pi)
271249423Sdim{
272249423Sdim	u_char embuf[1000];
273249423Sdim	char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
274249423Sdim	    CMSG_SPACE(sizeof(struct in_addr))];
275249423Sdim	struct udp_port *p = (struct udp_port *)tp;
276249423Sdim	struct msghdr msg;
277249423Sdim	struct iovec iov[1];
278249423Sdim	ssize_t len;
279193326Sed	struct cmsghdr *cmsg;
280198092Srdivacky	struct sockcred *cred = NULL;
281193326Sed
282198092Srdivacky	if (pi->buf == NULL) {
283193326Sed		/* no buffer yet - allocate one */
284198092Srdivacky		if ((pi->buf = buf_alloc(0)) == NULL) {
285193326Sed			/* ups - could not get buffer. Read away input
286198092Srdivacky			 * and drop it */
287193326Sed			(void)recvfrom(pi->fd, embuf, sizeof(embuf),
288198092Srdivacky			    0, NULL, NULL);
289226633Sdim			/* return error */
290193326Sed			return (-1);
291193326Sed		}
292224145Sdim		pi->buflen = buf_size(0);
293224145Sdim	}
294224145Sdim
295198092Srdivacky	/* try to get a message */
296224145Sdim	msg.msg_name = pi->peer;
297224145Sdim	msg.msg_namelen = pi->peerlen;
298224145Sdim	msg.msg_iov = iov;
299224145Sdim	msg.msg_iovlen = 1;
300224145Sdim	memset(cbuf, 0, sizeof(cbuf));
301224145Sdim	msg.msg_control = cbuf;
302224145Sdim	msg.msg_controllen = sizeof(cbuf);
303224145Sdim	msg.msg_flags = 0;
304224145Sdim
305224145Sdim	iov[0].iov_base = pi->buf;
306224145Sdim	iov[0].iov_len = pi->buflen;
307224145Sdim
308224145Sdim	len = recvmsg(pi->fd, &msg, 0);
309224145Sdim
310224145Sdim	if (len == -1 || len == 0)
311224145Sdim		/* receive error */
312193326Sed		return (-1);
313193326Sed
314193326Sed	if (msg.msg_flags & MSG_TRUNC) {
315193326Sed		/* truncated - drop */
316204643Srdivacky		snmpd_stats.silentDrops++;
317193326Sed		snmpd_stats.inTooLong++;
318221345Sdim		return (-1);
319193326Sed	}
320193326Sed
321224145Sdim	pi->length = (size_t)len;
322193326Sed
323198092Srdivacky	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
324198092Srdivacky	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
325199990Srdivacky		if (cmsg->cmsg_level == IPPROTO_IP &&
326193326Sed		    cmsg->cmsg_type == IP_RECVDSTADDR)
327193326Sed			memcpy(&p->dstaddr, CMSG_DATA(cmsg),
328224145Sdim			    sizeof(struct in_addr));
329224145Sdim		if (cmsg->cmsg_level == SOL_SOCKET &&
330193326Sed		    cmsg->cmsg_type == SCM_CREDS)
331193326Sed			cred = (struct sockcred *)CMSG_DATA(cmsg);
332193326Sed	}
333224145Sdim
334224145Sdim	if (pi->cred)
335198092Srdivacky		check_priv_dgram(pi, cred);
336193326Sed
337193326Sed	return (0);
338193326Sed}
339193326Sed
340193326Sed/*
341193326Sed * Port table
342193326Sed */
343193326Sedint
344193326Sedop_snmp_port(struct snmp_context *ctx, struct snmp_value *value,
345193326Sed    u_int sub, u_int iidx, enum snmp_op op)
346193326Sed{
347193326Sed	asn_subid_t which = value->var.subs[sub-1];
348218893Sdim	struct udp_port *p;
349218893Sdim	u_int8_t addr[4];
350198092Srdivacky	u_int32_t port;
351198092Srdivacky
352224145Sdim	switch (op) {
353224145Sdim
354224145Sdim	  case SNMP_OP_GETNEXT:
355198092Srdivacky		if ((p = (struct udp_port *)trans_next_port(my_trans,
356226633Sdim		    &value->var, sub)) == NULL)
357249423Sdim			return (SNMP_ERR_NOSUCHNAME);
358249423Sdim		index_append(&value->var, sub, &p->tport.index);
359249423Sdim		break;
360249423Sdim
361226633Sdim	  case SNMP_OP_GET:
362193326Sed		if ((p = (struct udp_port *)trans_find_port(my_trans,
363193326Sed		    &value->var, sub)) == NULL)
364193326Sed			return (SNMP_ERR_NOSUCHNAME);
365224145Sdim		break;
366249423Sdim
367249423Sdim	  case SNMP_OP_SET:
368224145Sdim		p = (struct udp_port *)trans_find_port(my_trans,
369198092Srdivacky		    &value->var, sub);
370224145Sdim		ctx->scratch->int1 = (p != NULL);
371198092Srdivacky
372224145Sdim		if (which != LEAF_begemotSnmpdPortStatus)
373224145Sdim			abort();
374198092Srdivacky		if (!TRUTH_OK(value->v.integer))
375193326Sed			return (SNMP_ERR_WRONG_VALUE);
376193326Sed
377224145Sdim		ctx->scratch->int2 = TRUTH_GET(value->v.integer);
378224145Sdim
379249423Sdim		if (ctx->scratch->int2) {
380249423Sdim			/* open an SNMP port */
381249423Sdim			if (p != NULL)
382249423Sdim				/* already open - do nothing */
383249423Sdim				return (SNMP_ERR_NOERROR);
384249423Sdim
385249423Sdim			if (index_decode(&value->var, sub, iidx, addr, &port))
386249423Sdim				return (SNMP_ERR_NO_CREATION);
387249423Sdim			return (udp_open_port(addr, port, &p));
388249423Sdim
389249423Sdim		} else {
390201361Srdivacky			/* close SNMP port - do in commit */
391201361Srdivacky		}
392234353Sdim		return (SNMP_ERR_NOERROR);
393234353Sdim
394234353Sdim	  case SNMP_OP_ROLLBACK:
395234353Sdim		p = (struct udp_port *)trans_find_port(my_trans,
396221345Sdim		    &value->var, sub);
397193326Sed		if (ctx->scratch->int1 == 0) {
398193326Sed			/* did not exist */
399193326Sed			if (ctx->scratch->int2 == 1) {
400251662Sdim				/* created */
401251662Sdim				if (p != NULL)
402193326Sed					udp_close_port(&p->tport);
403224145Sdim			}
404224145Sdim		}
405224145Sdim		return (SNMP_ERR_NOERROR);
406193326Sed
407193326Sed	  case SNMP_OP_COMMIT:
408193326Sed		p = (struct udp_port *)trans_find_port(my_trans,
409224145Sdim		    &value->var, sub);
410224145Sdim		if (ctx->scratch->int1 == 1) {
411224145Sdim			/* did exist */
412221345Sdim			if (ctx->scratch->int2 == 0) {
413224145Sdim				/* delete */
414224145Sdim				if (p != NULL)
415193326Sed					udp_close_port(&p->tport);
416193326Sed			}
417224145Sdim		}
418224145Sdim		return (SNMP_ERR_NOERROR);
419224145Sdim
420224145Sdim	  default:
421224145Sdim		abort();
422221345Sdim	}
423224145Sdim
424224145Sdim	/*
425193326Sed	 * Come here to fetch the value
426198092Srdivacky	 */
427193326Sed	switch (which) {
428224145Sdim
429224145Sdim	  case LEAF_begemotSnmpdPortStatus:
430193326Sed		value->v.integer = 1;
431193326Sed		break;
432193326Sed
433224145Sdim	  default:
434224145Sdim		abort();
435193326Sed	}
436193326Sed
437224145Sdim	return (SNMP_ERR_NOERROR);
438224145Sdim}
439193326Sed