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 $
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
50 #include <arpa/inet.h>
58 #define SNMPTREE_TYPES
59 #define SNMPENUM_FUNCS
63 extern const struct transport_def inet_trans;
66 struct inet_port_params;
69 typedef int create_func(struct inet_port *, struct inet_port_params *);
70 typedef void input_func(int, void *);
71 typedef int activate_func(struct inet_port *);
72 typedef void deactivate_func(struct inet_port *);
73 typedef void parse_ctrl_func(struct port_sock *, const struct msghdr *);
74 typedef void setsrc_func(struct port_sock *, struct msghdr *);
76 static create_func ipv4_create;
77 static input_func ipv4_input;
78 static activate_func ipv4_activate;
79 static deactivate_func ipv4_deactivate;
80 static parse_ctrl_func ipv4_parse_ctrl;
81 static setsrc_func ipv4_setsrc;
83 static create_func ipv6_create;
84 static input_func ipv6_input;
85 static activate_func ipv6_activate;
86 static deactivate_func ipv6_deactivate;
87 static parse_ctrl_func ipv6_parse_ctrl;
88 static setsrc_func ipv6_setsrc;
90 static create_func ipv6z_create;
92 static create_func dns_create;
93 static activate_func dns_activate;
94 static deactivate_func dns_deactivate;
97 /* common input stuff; must be first */
98 struct port_input input;
101 TAILQ_ENTRY(port_sock) link;
103 /** pointer to parent */
104 struct inet_port *port;
107 struct sockaddr_storage bind_addr;
109 /** reply destination */
110 struct sockaddr_storage ret_dest;
112 /** need to set source address in reply; set for INADDR_ANY */
115 /** address of the receive interface */
121 struct in6_pktinfo a6;
124 /** parse control message */
125 parse_ctrl_func *parse_ctrl;
127 /** set source address for a send() */
130 static_assert(offsetof(struct port_sock, input) == 0,
131 "input not first in port_sock");
134 * Table row for the inet ports.
136 * When actived each row can have one or several open sockets. For ipv6,
137 * ipv4 and ipv6z addresses it is always one, for dns addresses more than
138 * one socket can be open.
141 /** common i/o port stuff (must be first) */
144 /** transport protocol */
145 enum BegemotSnmpdTransportProto proto;
148 enum RowStatus row_status;
151 TAILQ_HEAD(, port_sock) socks;
153 /** value for InetAddressType::dns */
156 /** port number in dns case; network byte order */
162 /** activate a port */
163 activate_func *activate;
165 /** deactivate port */
166 deactivate_func *deactivate;
168 static_assert(offsetof(struct inet_port, tport) == 0,
169 "tport not first in inet_port");
171 /** to be used in bind_addr field */
172 #define AF_DNS AF_VENDOR00
174 /** registered transport */
175 static struct transport *my_trans;
185 /** length of the control data buffer */
186 static const size_t RECV_CBUF_SIZE =
187 MAX(CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
188 CMSG_SPACE(sizeof(struct in_addr)),
189 CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
190 CMSG_SPACE(sizeof(struct in6_pktinfo)));
192 /** length of the control data buffer */
193 static const size_t XMIT_CBUF_SIZE =
194 MAX(CMSG_SPACE(sizeof(struct in_addr)),
195 CMSG_SPACE(sizeof(struct in6_pktinfo)));
198 * Start the transport. This registers the transport with the
201 * \return SNMP error code
206 return (trans_register(&inet_trans, &my_trans));
210 * Stop the transport. This tries to unregister the transport which
211 * in turn fails if the list of ports is not empty.
213 * \return SNMP error code
216 inet_stop(int force __unused)
218 if (my_trans != NULL)
219 if (trans_unregister(my_trans) != 0)
220 return (SNMP_ERR_GENERR);
221 return (SNMP_ERR_NOERROR);
225 * Deactivate SNMP port.
227 * \param tp port to close
230 deactivate_port(struct inet_port *p)
236 * This function activates a port. For ports opened via the config files
237 * this is called just before entering the event loop. For ports create
238 * during runtime this is called when the RowStatus is set to Active or
239 * as second step for CreateAndGo.
241 * \param tp transport port
243 * \return SNMP error code
246 inet_activate(struct tport *tp)
248 struct inet_port *port = (struct inet_port *)tp;
250 return (port->activate(port));
254 * Close the SNMP port if it is open and destroy it.
256 * \param tp port to close
259 inet_destroy_port(struct tport *tp)
261 struct inet_port *port = (struct inet_port *)tp;
263 deactivate_port(port);
265 trans_remove_port(tp);
267 free(port->dns_addr);
272 * If the input struct has no buffer allocated yet, do it now. If allocation
273 * fails, read the data into a local buffer and drop it.
275 * \param pi input struct
277 * \return -1 if allocation fails, 0 otherwise
280 inet_alloc_buf(struct port_input *pi)
284 if (pi->buf == NULL) {
285 if ((pi->buf = buf_alloc(0)) == NULL) {
286 (void)recvfrom(pi->fd, drop_buf, sizeof(drop_buf),
290 pi->buflen = buf_size(0);
296 * Read message into input buffer. Get also the source address and any
297 * control data that is available. If the message is truncated, increment
298 * corresponding statistics.
300 * \param pi input object
301 * \param msg message object to fill
302 * \param cbuf control data buffer
304 * \return -1 when something goes wrong, 0 othersise
307 inet_read_msg(struct port_input *pi, struct msghdr *msg, char *cbuf)
311 iov[0].iov_base = pi->buf;
312 iov[0].iov_len = pi->buflen;
314 msg->msg_name = pi->peer;
315 msg->msg_namelen = pi->peerlen;
318 msg->msg_control = cbuf;
319 msg->msg_controllen = RECV_CBUF_SIZE;
322 memset(cbuf, 0, RECV_CBUF_SIZE);
324 const ssize_t len = recvmsg(pi->fd, msg, 0);
326 if (len == -1 || len == 0)
330 if (msg->msg_flags & MSG_TRUNC) {
331 /* truncated - drop */
332 snmpd_stats.silentDrops++;
333 snmpd_stats.inTooLong++;
337 pi->length = (size_t)len;
343 * Input available on socket.
345 * \param tp transport port
346 * \param pi input struct
348 * \return number of bytes received
351 inet_recv(struct tport *tp, struct port_input *pi)
353 struct inet_port *port = __containerof(tp, struct inet_port, tport);
354 struct port_sock *sock = __containerof(pi, struct port_sock, input);
356 assert(port->proto == BegemotSnmpdTransportProto_udp);
358 if (inet_alloc_buf(pi) == -1)
361 char cbuf[RECV_CBUF_SIZE];
364 if (inet_read_msg(pi, &msg, cbuf) == -1)
367 sock->parse_ctrl(sock, &msg);
376 * \param buf data to send
377 * \param len number of bytes to send
378 * \param addr destination address
379 * \param addlen destination address length
381 * \return number of bytes sent
384 inet_send2(struct tport *tp, const u_char *buf, size_t len,
385 struct port_input *pi)
387 struct inet_port *p = __containerof(tp, struct inet_port, tport);
388 struct port_sock *s = (pi == NULL) ? TAILQ_FIRST(&p->socks) :
389 __containerof(pi, struct port_sock, input);
393 iov.iov_base = __DECONST(void*, buf);
401 msg.msg_name = (void *)pi->peer;
402 msg.msg_namelen = pi->peerlen;
404 msg.msg_control = NULL;
405 msg.msg_controllen = 0;
407 char cbuf[XMIT_CBUF_SIZE];
408 if (s->set_ret_source) {
409 msg.msg_control = cbuf;
413 return (sendmsg(s->input.fd, &msg, 0));
416 /** exported to daemon */
417 const struct transport_def inet_trans = {
419 OIDX_begemotSnmpdTransInet,
429 struct inet_port_params {
431 struct asn_oid index;
433 /** internet address type */
434 enum InetAddressType type;
436 /** internet address */
439 /** length of address */
446 enum BegemotSnmpdTransportProto proto;
450 * IPv4 creation stuff. Parse the index, fill socket address and creates
453 * \param port the port to create
454 * \param params parameters from the SNMP SET
459 ipv4_create(struct inet_port *port, struct inet_port_params *params)
463 if (params->addr_len != 4)
464 return (SNMP_ERR_INCONS_VALUE);
466 memcpy(&ip, params->addr, 4);
467 struct port_sock *sock = calloc(1, sizeof(struct port_sock));
469 return (SNMP_ERR_GENERR);
471 snmpd_input_init(&sock->input);
473 TAILQ_INSERT_HEAD(&port->socks, sock, link);
475 struct sockaddr_in *sin =
476 (struct sockaddr_in *)&sock->bind_addr;
478 sin->sin_len = sizeof(struct sockaddr_in);
479 sin->sin_family = AF_INET;
480 sin->sin_addr.s_addr = htonl(ip);
481 sin->sin_port = htons(params->port);
485 return (SNMP_ERR_NOERROR);
489 * An IPv4 inet port is ready. Delegate to the generic code to read the data
492 * \param fd file descriptor that is ready
493 * \param udata inet_port pointer
496 ipv4_input(int fd __unused, void *udata)
498 struct port_sock *sock = udata;
500 sock->input.peerlen = sizeof(struct sockaddr_in);
501 snmpd_input(&sock->input, &sock->port->tport);
505 * Activate an IPv4 socket.
507 * \param sock thhe socket to activate
512 ipv4_activate_sock(struct port_sock *sock)
514 if ((sock->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
515 syslog(LOG_ERR, "creating UDP4 socket: %m");
516 return (SNMP_ERR_RES_UNAVAIL);
519 const struct sockaddr_in *sin =
520 (const struct sockaddr_in *)&sock->bind_addr;
522 if (sin->sin_addr.s_addr == INADDR_ANY) {
523 /* need to know from which address to return */
524 static const int on = 1;
526 if (setsockopt(sock->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on,
528 syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
529 (void)close(sock->input.fd);
531 return (SNMP_ERR_GENERR);
533 sock->set_ret_source = true;
536 if (bind(sock->input.fd, (const struct sockaddr *)sin, sizeof(*sin))) {
537 if (errno == EADDRNOTAVAIL) {
538 (void)close(sock->input.fd);
540 return (SNMP_ERR_INCONS_NAME);
542 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(sin->sin_addr),
543 ntohs(sin->sin_port));
544 (void)close(sock->input.fd);
546 return (SNMP_ERR_GENERR);
549 if ((sock->input.id = fd_select(sock->input.fd, ipv4_input,
550 sock, NULL)) == NULL) {
551 (void)close(sock->input.fd);
553 return (SNMP_ERR_GENERR);
555 sock->input.peer = (struct sockaddr *)&sock->ret_dest;
557 sock->parse_ctrl = ipv4_parse_ctrl;
558 sock->setsrc = ipv4_setsrc;
560 return (SNMP_ERR_NOERROR);
564 * Open an IPv4 socket. Make the socket, bind it and put it on the select
565 * list. The socket struct has already been created during creation.
569 * \return SNMP error code
572 ipv4_activate(struct inet_port *p)
574 struct port_sock *sock = TAILQ_FIRST(&p->socks);
576 assert(!TAILQ_NEXT(sock, link));
578 const int ret = ipv4_activate_sock(sock);
579 if (ret == SNMP_ERR_NOERROR)
580 p->row_status = RowStatus_active;
586 * Close an IPv4 socket. Keep the sock object.
591 ipv4_deactivate(struct inet_port *p)
593 struct port_sock *sock = TAILQ_FIRST(&p->socks);
595 assert(!TAILQ_NEXT(sock, link));
597 snmpd_input_close(&sock->input);
599 p->row_status = RowStatus_notInService;
603 * Parse the control data received with a UDPv4 packet. This may contain
604 * credentials (for a local connection) and the address of the interface
605 * the message was received on. If there are credentials set the priv flag
606 * if the effective UID is zero.
608 * \param sock the sock object
609 * \param msg the received message
612 ipv4_parse_ctrl(struct port_sock *sock, const struct msghdr *msg)
614 struct sockcred *cred = NULL;
616 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
617 cmsg = CMSG_NXTHDR(msg, cmsg)) {
619 if (cmsg->cmsg_level == IPPROTO_IP &&
620 cmsg->cmsg_type == IP_RECVDSTADDR) {
621 memcpy(&sock->ret_source.a4, CMSG_DATA(cmsg),
622 sizeof(struct in_addr));
624 } else if (cmsg->cmsg_level == SOL_SOCKET &&
625 cmsg->cmsg_type == SCM_CREDS) {
626 cred = (struct sockcred *)(void *)CMSG_DATA(cmsg);
630 sock->input.priv = 0;
631 if (sock->input.cred && cred)
632 /* remote end has sent credentials */
633 sock->input.priv = (cred->sc_euid == 0);
637 * Set the source address option for IPv4 sockets.
639 * \param sock socket object
643 ipv4_setsrc(struct port_sock *sock, struct msghdr *msg)
645 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
647 /* select outgoing interface by setting source address */
648 cmsg->cmsg_level = IPPROTO_IP;
649 cmsg->cmsg_type = IP_SENDSRCADDR;
650 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
651 memcpy(CMSG_DATA(cmsg), &sock->ret_source.a4,
652 sizeof(struct in_addr));
654 msg->msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
658 * Common part of IPv6 creation. This is used by both ipv6_create() and
661 * \param port the table row
662 * \param params creation parameters
663 * \param scope_id scope_id (0 or from index)
665 * \return SNMP_ERR_NOERROR if port has been created, error code otherwise
668 ipv6_create_common(struct inet_port *port, struct inet_port_params *params,
671 struct port_sock *sock = calloc(1, sizeof(struct port_sock));
674 return (SNMP_ERR_GENERR);
676 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&sock->bind_addr;
678 sin->sin6_len = sizeof(struct sockaddr_in6);
679 sin->sin6_family = AF_INET6;
680 sin->sin6_port = htons(params->port);
681 sin->sin6_flowinfo = 0;
682 sin->sin6_scope_id = scope_id;
684 memcpy(sin->sin6_addr.s6_addr, params->addr, 16);
686 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && scope_id == 0) {
687 char buf[INET6_ADDRSTRLEN];
688 syslog(LOG_INFO, "%s: link local address used without scope "
689 "index: %s", __func__, inet_ntop(AF_INET6,
690 &sin->sin6_addr, buf, sizeof(buf)));
692 return (SNMP_ERR_NO_CREATION);
697 snmpd_input_init(&sock->input);
698 TAILQ_INSERT_HEAD(&port->socks, sock, link);
700 return (SNMP_ERR_NOERROR);
704 * IPv6 creation stuff. Parse the index, fill socket address and creates
707 * \param port the port to create
708 * \param params parameters from the SNMP SET
713 ipv6_create(struct inet_port *port, struct inet_port_params *params)
715 if (params->addr_len != 16)
716 return (SNMP_ERR_INCONS_VALUE);
718 const int ret = ipv6_create_common(port, params, 0);
719 if (ret != SNMP_ERR_NOERROR)
722 return (SNMP_ERR_NOERROR);
726 * An IPv6 inet port is ready. Delegate to the generic code to read the data
729 * \param fd file descriptor that is ready
730 * \param udata inet_port pointer
733 ipv6_input(int fd __unused, void *udata)
735 struct port_sock *sock = udata;
737 sock->input.peerlen = sizeof(struct sockaddr_in6);
738 snmpd_input(&sock->input, &sock->port->tport);
742 * Activate an IPv6 socket.
744 * \param sock thhe socket to activate
749 ipv6_activate_sock(struct port_sock *sock)
751 if ((sock->input.fd = socket(PF_INET6, SOCK_DGRAM, 0)) == -1) {
752 syslog(LOG_ERR, "creating UDP6 socket: %m");
753 return (SNMP_ERR_RES_UNAVAIL);
756 const struct sockaddr_in6 *sin =
757 (const struct sockaddr_in6 *)&sock->bind_addr;
759 if (memcmp(&sin->sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) {
760 /* need to know from which address to return */
761 static const int on = 1;
763 if (setsockopt(sock->input.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
764 &on, sizeof(on)) == -1) {
765 syslog(LOG_ERR, "setsockopt(IP6_PKTINFO): %m");
766 (void)close(sock->input.fd);
768 return (SNMP_ERR_GENERR);
770 sock->set_ret_source = true;
773 if (bind(sock->input.fd, (const struct sockaddr *)sin, sizeof(*sin))) {
774 if (community != COMM_INITIALIZE && errno == EADDRNOTAVAIL) {
775 (void)close(sock->input.fd);
777 return (SNMP_ERR_INCONS_NAME);
779 char buf[INET6_ADDRSTRLEN];
780 syslog(LOG_ERR, "bind: %s:%u:%u %m", inet_ntop(AF_INET6,
781 &sin->sin6_addr, buf, sizeof(buf)), sin->sin6_scope_id,
782 ntohs(sin->sin6_port));
783 (void)close(sock->input.fd);
785 return (SNMP_ERR_GENERR);
787 if ((sock->input.id = fd_select(sock->input.fd, ipv6_input,
788 sock, NULL)) == NULL) {
789 (void)close(sock->input.fd);
791 return (SNMP_ERR_GENERR);
793 sock->input.peer = (struct sockaddr *)&sock->ret_dest;
795 sock->parse_ctrl = ipv6_parse_ctrl;
796 sock->setsrc = ipv6_setsrc;
798 return (SNMP_ERR_NOERROR);
802 * Open an IPv6 socket.
804 * \param port inet port
806 * \return SNMP error code
809 ipv6_activate(struct inet_port *p)
811 struct port_sock *sock = TAILQ_FIRST(&p->socks);
814 const int ret = ipv6_activate_sock(sock);
816 if (ret == SNMP_ERR_NOERROR)
817 p->row_status = RowStatus_active;
822 * Close an IPv6 socket. Keep the sock object.
827 ipv6_deactivate(struct inet_port *p)
829 struct port_sock *sock = TAILQ_FIRST(&p->socks);
831 assert(!TAILQ_NEXT(sock, link));
833 snmpd_input_close(&sock->input);
835 p->row_status = RowStatus_notInService;
839 * IPv6 specific part of message processing. The control data may contain
840 * credentials and packet info that contains the destination address of
843 * \param sock the sock object
844 * \param msg the received message
847 ipv6_parse_ctrl(struct port_sock *sock, const struct msghdr *msg)
849 struct sockcred *cred = NULL;
851 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
852 cmsg = CMSG_NXTHDR(msg, cmsg)) {
854 if (cmsg->cmsg_level == IPPROTO_IPV6 &&
855 cmsg->cmsg_type == IPV6_PKTINFO) {
856 const struct in6_pktinfo *info =
857 (const struct in6_pktinfo *)(const void *)
859 sock->ret_source.a6.ipi6_addr = info->ipi6_addr;
860 sock->ret_source.a6.ipi6_ifindex =
861 !IN6_IS_ADDR_LINKLOCAL(&info->ipi6_addr) ? 0:
863 } else if (cmsg->cmsg_level == SOL_SOCKET &&
864 cmsg->cmsg_type == SCM_CREDS) {
865 cred = (struct sockcred *)(void *)CMSG_DATA(cmsg);
869 sock->input.priv = 0;
870 if (sock->input.cred && cred)
871 /* remote end has sent credentials */
872 sock->input.priv = (cred->sc_euid == 0);
876 * Set the source address option for IPv4 sockets.
878 * \param sock socket object
882 ipv6_setsrc(struct port_sock *sock, struct msghdr *msg)
884 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
886 /* select outgoing interface by setting source address */
887 cmsg->cmsg_level = IPPROTO_IPV6;
888 cmsg->cmsg_type = IPV6_PKTINFO;
889 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
890 memcpy(CMSG_DATA(cmsg), &sock->ret_source.a6,
891 sizeof(struct in6_pktinfo));
893 msg->msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
897 * IPv6z creation stuff. Parse the index, fill socket address and creates
900 * \param port the port to create
901 * \param params parameters from the SNMP SET
906 ipv6z_create(struct inet_port *port, struct inet_port_params *params)
908 if (params->addr_len != 20)
909 return (SNMP_ERR_INCONS_VALUE);
912 for (u_int i = 16; i < 20; i++) {
914 scope_id |= params->addr[i];
917 const int ret = ipv6_create_common(port, params, scope_id);
918 if (ret != SNMP_ERR_NOERROR)
921 return (SNMP_ERR_NOERROR);
925 * DNS name creation stuff. Parse the index and save the string.
926 * This does not create a socket struct.
928 * \param port the port to create
929 * \param params parameters from the SNMP SET
934 dns_create(struct inet_port *port, struct inet_port_params *params)
936 if (params->addr_len > 64)
937 return (SNMP_ERR_INCONS_VALUE);
939 if (strnlen(params->addr, params->addr_len) !=
941 return (SNMP_ERR_INCONS_VALUE);
943 if ((port->dns_addr = realloc(params->addr,
944 params->addr_len + 1)) == NULL)
945 return (SNMP_ERR_GENERR);
947 port->dns_addr[params->addr_len] = '\0';
950 port->dns_port = htons(params->port);
952 return (SNMP_ERR_NOERROR);
956 * Open sockets. This loops through all the addresses returned by getaddrinfo
957 * and opens a socket for each of them.
959 * \param port inet port
961 * \return SNMP error code
964 dns_activate(struct inet_port *port)
966 struct addrinfo hints;
967 memset(&hints, 0, sizeof(hints));
968 hints.ai_family = AF_UNSPEC;
969 hints.ai_socktype = SOCK_DGRAM; // XXX udp-only
970 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE | AI_NUMERICSERV;
973 sprintf(portbuf, "%hu", ntohs(port->dns_port));
975 struct addrinfo *res0;
976 int error = getaddrinfo(port->dns_addr[0] == '\0'
977 ? NULL : port->dns_addr,
978 portbuf, &hints, &res0);
981 syslog(LOG_ERR, "cannot resolve address '%s': %s",
982 port->dns_addr, gai_strerror(error));
983 return (SNMP_ERR_GENERR);
986 for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) {
987 if (res->ai_family != AF_INET && res->ai_family != AF_INET6)
990 struct port_sock *sock = calloc(1, sizeof(struct port_sock));
992 return (SNMP_ERR_GENERR);
994 snmpd_input_init(&sock->input);
997 int ret = SNMP_ERR_NOERROR;
999 if (res->ai_family == AF_INET) {
1000 *(struct sockaddr_in *)&sock->bind_addr =
1001 *(struct sockaddr_in *)(void *)res->ai_addr;
1002 ret = ipv4_activate_sock(sock);
1004 *(struct sockaddr_in6 *)&sock->bind_addr =
1005 *(struct sockaddr_in6 *)(void *)res->ai_addr;
1006 ret = ipv6_activate_sock(sock);
1009 if (ret != SNMP_ERR_NOERROR)
1012 TAILQ_INSERT_HEAD(&port->socks, sock, link);
1015 if (!TAILQ_EMPTY(&port->socks))
1016 port->row_status = RowStatus_active;
1019 return (SNMP_ERR_GENERR);
1023 * Deactive the socket. Close all open sockets and delete all sock objects.
1025 * \param port inet port
1028 dns_deactivate(struct inet_port *port)
1030 while (!TAILQ_EMPTY(&port->socks)) {
1031 struct port_sock *sock = TAILQ_FIRST(&port->socks);
1032 TAILQ_REMOVE(&port->socks, sock, link);
1033 snmpd_input_close(&sock->input);
1036 port->row_status = RowStatus_notInService;
1040 inet_create(struct inet_port_params *params, struct inet_port **pp)
1042 int err = SNMP_ERR_NOERROR;
1043 struct inet_port *port = NULL;
1045 if (params->port > 0xffff) {
1046 err = SNMP_ERR_NO_CREATION;
1050 if ((port = malloc(sizeof(*port))) == NULL) {
1051 err = SNMP_ERR_GENERR;
1054 memset(port, 0, sizeof(*port));
1055 TAILQ_INIT(&port->socks);
1057 port->proto = params->proto;
1058 port->tport.index = params->index;
1060 switch (params->type) {
1062 case InetAddressType_ipv4:
1063 port->create = ipv4_create;
1064 port->activate = ipv4_activate;
1065 port->deactivate = ipv4_deactivate;
1068 case InetAddressType_ipv6:
1069 port->create = ipv6_create;
1070 port->activate = ipv6_activate;
1071 port->deactivate = ipv6_deactivate;
1074 case InetAddressType_ipv6z:
1075 port->create = ipv6z_create;
1076 port->activate = ipv6_activate;
1077 port->deactivate = ipv6_deactivate;
1080 case InetAddressType_dns:
1081 port->create = dns_create;
1082 port->activate = dns_activate;
1083 port->deactivate = dns_deactivate;
1087 err = SNMP_ERR_NO_CREATION;
1091 if ((err = port->create(port, params)) != SNMP_ERR_NOERROR)
1095 trans_insert_port(my_trans, &port->tport);
1099 free(port->dns_addr);
1105 create_and_go(struct snmp_context *ctx, struct inet_port_params *params)
1108 struct inet_port *port;
1110 if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR)
1113 port->row_status = RowStatus_createAndGo;
1114 ctx->scratch->ptr1 = port;
1116 if (community == COMM_INITIALIZE)
1119 return (inet_activate(&port->tport));
1123 create_and_wait(struct snmp_context *ctx, struct inet_port_params *params)
1126 struct inet_port *port;
1128 if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR)
1131 port->row_status = RowStatus_createAndWait;
1132 ctx->scratch->ptr1 = port;
1138 * This is called to set a RowStatus value in the port table during
1141 * When this is called during initialization time and the RowStatus is set
1142 * to CreateAndGo, the port is actually not activated. This is done when
1143 * the main code calls the init() for all ports later.
1146 inet_port_set(struct snmp_context *ctx, struct inet_port *port,
1147 struct inet_port_params *params, enum RowStatus nstatus)
1151 case RowStatus_createAndGo:
1153 return (SNMP_ERR_INCONS_VALUE);
1154 ctx->scratch->int1 = SET_CREATED;
1155 return (create_and_go(ctx, params));
1157 case RowStatus_createAndWait:
1159 return (SNMP_ERR_INCONS_VALUE);
1160 ctx->scratch->int1 = SET_CREATED;
1161 return (create_and_wait(ctx, params));
1163 case RowStatus_active:
1165 return (SNMP_ERR_INCONS_VALUE);
1167 switch (port->row_status) {
1169 case RowStatus_notReady:
1170 /* this can not happend */
1173 case RowStatus_notInService:
1174 ctx->scratch->int1 = SET_ACTIVATED;
1175 return (inet_activate(&port->tport));
1177 case RowStatus_active:
1178 return (SNMP_ERR_NOERROR);
1180 case RowStatus_createAndGo:
1181 case RowStatus_createAndWait:
1182 case RowStatus_destroy:
1187 case RowStatus_notInService:
1189 return (SNMP_ERR_INCONS_VALUE);
1191 switch (port->row_status) {
1193 case RowStatus_notReady:
1194 /* this can not happend */
1197 case RowStatus_notInService:
1198 return (SNMP_ERR_NOERROR);
1200 case RowStatus_active:
1201 /* this is done during commit */
1202 ctx->scratch->int1 = SET_DEACTIVATE;
1203 return (SNMP_ERR_NOERROR);
1205 case RowStatus_createAndGo:
1206 case RowStatus_createAndWait:
1207 case RowStatus_destroy:
1212 case RowStatus_destroy:
1213 /* this is done during commit */
1214 ctx->scratch->int1 = SET_DESTROY;
1215 return (SNMP_ERR_NOERROR);
1217 case RowStatus_notReady:
1218 return (SNMP_ERR_WRONG_VALUE);
1227 op_snmp_trans_inet(struct snmp_context *ctx, struct snmp_value *value,
1228 u_int sub, u_int iidx __unused, enum snmp_op op)
1230 asn_subid_t which = value->var.subs[sub - 1];
1231 struct inet_port *port;
1232 struct inet_port_params params;
1237 case SNMP_OP_GETNEXT:
1238 if ((port = (struct inet_port *)trans_next_port(my_trans,
1239 &value->var, sub)) == NULL)
1240 return (SNMP_ERR_NOSUCHNAME);
1241 index_append(&value->var, sub, &port->tport.index);
1245 if ((port = (struct inet_port *)trans_find_port(my_trans,
1246 &value->var, sub)) == NULL)
1247 return (SNMP_ERR_NOSUCHNAME);
1251 port = (struct inet_port *)trans_find_port(my_trans,
1254 if (which != LEAF_begemotSnmpdTransInetStatus)
1256 if (!isok_RowStatus(value->v.integer))
1257 return (SNMP_ERR_WRONG_VALUE);
1259 if (index_decode(&value->var, sub, iidx, ¶ms.type,
1260 ¶ms.addr, ¶ms.addr_len, ¶ms.port,
1262 return (SNMP_ERR_NO_CREATION);
1264 asn_slice_oid(¶ms.index, &value->var, sub, value->var.len);
1266 ret = inet_port_set(ctx, port, ¶ms,
1267 (enum RowStatus)value->v.integer);
1272 case SNMP_OP_ROLLBACK:
1273 if ((port = (struct inet_port *)trans_find_port(my_trans,
1274 &value->var, sub)) == NULL)
1278 switch (ctx->scratch->int1) {
1282 assert(port != NULL);
1283 inet_destroy_port(&port->tport);
1284 return (SNMP_ERR_NOERROR);
1288 assert(port != NULL);
1289 return (SNMP_ERR_NOERROR);
1292 deactivate_port(port);
1293 return (SNMP_ERR_NOERROR);
1295 case SET_DEACTIVATE:
1296 return (SNMP_ERR_NOERROR);
1300 case SNMP_OP_COMMIT:
1301 if ((port = (struct inet_port *)trans_find_port(my_trans,
1302 &value->var, sub)) == NULL)
1306 switch (ctx->scratch->int1) {
1310 assert(port != NULL);
1311 return (SNMP_ERR_NOERROR);
1315 assert(port != NULL);
1316 inet_destroy_port(&port->tport);
1317 return (SNMP_ERR_NOERROR);
1320 return (SNMP_ERR_NOERROR);
1322 case SET_DEACTIVATE:
1323 deactivate_port(port);
1324 return (SNMP_ERR_NOERROR);
1333 case LEAF_begemotSnmpdTransInetStatus:
1334 value->v.integer = port->row_status;
1341 return (SNMP_ERR_NOERROR);