]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bsnmp/snmpd/trans_inet.c
MFC r345797:
[FreeBSD/FreeBSD.git] / contrib / bsnmp / snmpd / trans_inet.c
1 /*
2  * Copyright (c) 2018
3  *      Hartmut Brandt.
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  * Internet transport
32  */
33
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37
38 #include <assert.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <stdbool.h>
42 #include <stddef.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47
48 #include <stdio.h>
49
50 #include <arpa/inet.h>
51
52 #include "asn1.h"
53 #include "snmp.h"
54 #include "snmpmod.h"
55
56 #include "snmpd.h"
57
58 #define SNMPTREE_TYPES
59 #define SNMPENUM_FUNCS
60 #include "tree.h"
61 #include "oid.h"
62
63 extern const struct transport_def inet_trans;
64
65 struct inet_port;
66 struct inet_port_params;
67 struct port_sock;
68
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 *);
75
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;
82
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;
89
90 static create_func ipv6z_create;
91
92 static create_func dns_create;
93 static activate_func dns_activate;
94 static deactivate_func dns_deactivate;
95
96 struct port_sock {
97         /* common input stuff; must be first */
98         struct port_input input;
99
100         /** link field */
101         TAILQ_ENTRY(port_sock) link;
102
103         /** pointer to parent */
104         struct inet_port *port;
105
106         /** bind address */
107         struct sockaddr_storage bind_addr;
108
109         /** reply destination */
110         struct sockaddr_storage ret_dest;
111
112         /** need to set source address in reply; set for INADDR_ANY */
113         bool set_ret_source;
114
115         /** address of the receive interface */
116         union {
117                 /** IPv4 case */
118                 struct in_addr  a4;
119
120                 /** IPv6 case */
121                 struct in6_pktinfo a6;
122         } ret_source;
123
124         /** parse control message */
125         parse_ctrl_func *parse_ctrl;
126
127         /** set source address for a send() */
128         setsrc_func *setsrc;
129 };
130 static_assert(offsetof(struct port_sock, input) == 0,
131     "input not first in port_sock");
132
133 /**
134  * Table row for the inet ports.
135  *
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.
139  */
140 struct inet_port {
141         /** common i/o port stuff (must be first) */
142         struct tport tport;
143
144         /** transport protocol */
145         enum BegemotSnmpdTransportProto proto;
146
147         /** row status */
148         enum RowStatus row_status;
149
150         /** socket list */
151         TAILQ_HEAD(, port_sock) socks;
152
153         /** value for InetAddressType::dns */
154         char *dns_addr;
155
156         /** port number in dns case; network byte order */
157         uint16_t dns_port;
158
159         /** create a port */
160         create_func *create;
161
162         /** activate a port */
163         activate_func *activate;
164
165         /** deactivate port */
166         deactivate_func *deactivate;
167 };
168 static_assert(offsetof(struct inet_port, tport) == 0,
169     "tport not first in inet_port");
170
171 /** to be used in bind_addr field */
172 #define AF_DNS  AF_VENDOR00
173
174 /** registered transport */
175 static struct transport *my_trans;
176
177 /** set operation */
178 enum {
179         SET_CREATED,
180         SET_ACTIVATED,
181         SET_DEACTIVATE,
182         SET_DESTROY,
183 };
184
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)));
191
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)));
196
197 /**
198  * Start the transport. This registers the transport with the
199  * transport table.
200  *
201  * \return SNMP error code
202  */
203 static int
204 inet_start(void)
205 {
206         return (trans_register(&inet_trans, &my_trans));
207 }
208
209 /**
210  * Stop the transport. This tries to unregister the transport which
211  * in turn fails if the list of ports is not empty.
212  *
213  * \return SNMP error code
214  */
215 static int
216 inet_stop(int force __unused)
217 {
218         if (my_trans != NULL)
219                 if (trans_unregister(my_trans) != 0)
220                         return (SNMP_ERR_GENERR);
221         return (SNMP_ERR_NOERROR);
222 }
223
224 /**
225  * Deactivate SNMP port.
226  *
227  * \param tp    port to close
228  */
229 static void
230 deactivate_port(struct inet_port *p)
231 {
232         p->deactivate(p);
233 }
234
235 /*
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.
240  *
241  * \param tp    transport port
242  *
243  * \return SNMP error code
244  */
245 static int
246 inet_activate(struct tport *tp)
247 {
248         struct inet_port *port = (struct inet_port *)tp;
249
250         return (port->activate(port));
251 }
252
253 /*
254  * Close the SNMP port if it is open and destroy it.
255  *
256  * \param tp    port to close
257  */
258 static void
259 inet_destroy_port(struct tport *tp)
260 {
261         struct inet_port *port = (struct inet_port *)tp;
262
263         deactivate_port(port);
264
265         trans_remove_port(tp);
266
267         free(port->dns_addr);
268         free(port);
269 }
270
271 /**
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.
274  *
275  * \param pi    input struct
276  *
277  * \return -1 if allocation fails, 0 otherwise
278  */
279 static int
280 inet_alloc_buf(struct port_input *pi)
281 {
282         char drop_buf[2000];
283
284         if (pi->buf == NULL) {
285                 if ((pi->buf = buf_alloc(0)) == NULL) {
286                         (void)recvfrom(pi->fd, drop_buf, sizeof(drop_buf),
287                             0, NULL, NULL);
288                         return (-1);
289                 }
290                 pi->buflen = buf_size(0);
291         }
292         return (0);
293 }
294
295 /**
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.
299  *
300  * \param pi    input object
301  * \param msg   message object to fill
302  * \param cbuf  control data buffer
303  *
304  * \return -1 when something goes wrong, 0 othersise
305  */
306 static int
307 inet_read_msg(struct port_input *pi, struct msghdr *msg, char *cbuf)
308 {
309         struct iovec iov[1];
310
311         iov[0].iov_base = pi->buf;
312         iov[0].iov_len = pi->buflen;
313
314         msg->msg_name = pi->peer;
315         msg->msg_namelen = pi->peerlen;
316         msg->msg_iov = iov;
317         msg->msg_iovlen = 1;
318         msg->msg_control = cbuf;
319         msg->msg_controllen = RECV_CBUF_SIZE;
320         msg->msg_flags = 0;
321
322         memset(cbuf, 0, RECV_CBUF_SIZE);
323
324         const ssize_t len = recvmsg(pi->fd, msg, 0);
325
326         if (len == -1 || len == 0)
327                 /* receive error */
328                 return (-1);
329
330         if (msg->msg_flags & MSG_TRUNC) {
331                 /* truncated - drop */
332                 snmpd_stats.silentDrops++;
333                 snmpd_stats.inTooLong++;
334                 return (-1);
335         }
336
337         pi->length = (size_t)len;
338
339         return (0);
340 }
341
342 /*
343  * Input available on socket.
344  *
345  * \param tp    transport port
346  * \param pi    input struct
347  *
348  * \return number of bytes received
349  */
350 static ssize_t
351 inet_recv(struct tport *tp, struct port_input *pi)
352 {
353         struct inet_port *port = __containerof(tp, struct inet_port, tport);
354         struct port_sock *sock = __containerof(pi, struct port_sock, input);
355
356         assert(port->proto == BegemotSnmpdTransportProto_udp);
357
358         if (inet_alloc_buf(pi) == -1)
359                 return (-1);
360
361         char cbuf[RECV_CBUF_SIZE];
362         struct msghdr msg;
363
364         if (inet_read_msg(pi, &msg, cbuf) == -1)
365                 return (-1);
366
367         sock->parse_ctrl(sock, &msg);
368
369         return (0);
370 }
371
372 /*
373  * Send message.
374  *
375  * \param tp            port
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
380  *
381  * \return number of bytes sent
382  */
383 static ssize_t
384 inet_send2(struct tport *tp, const u_char *buf, size_t len,
385     struct port_input *pi)
386 {
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);
390
391         struct iovec iov;
392
393         iov.iov_base = __DECONST(void*, buf);
394         iov.iov_len = len;
395
396         struct msghdr msg;
397
398         msg.msg_flags = 0;
399         msg.msg_iov = &iov;
400         msg.msg_iovlen = 1;
401         msg.msg_name = (void *)pi->peer;
402         msg.msg_namelen = pi->peerlen;
403
404         msg.msg_control = NULL;
405         msg.msg_controllen = 0;
406
407         char cbuf[XMIT_CBUF_SIZE];
408         if (s->set_ret_source) {
409                 msg.msg_control = cbuf;
410                 s->setsrc(s, &msg);
411         }
412
413         return (sendmsg(s->input.fd, &msg, 0));
414 }
415
416 /** exported to daemon */
417 const struct transport_def inet_trans = {
418         "inet",
419         OIDX_begemotSnmpdTransInet,
420         inet_start,
421         inet_stop,
422         inet_destroy_port,
423         inet_activate,
424         NULL,
425         inet_recv,
426         inet_send2,
427 };
428
429 struct inet_port_params {
430         /** index oid */
431         struct asn_oid index;
432
433         /** internet address type */
434         enum InetAddressType type;
435
436         /** internet address */
437         u_char *addr;
438
439         /** length of address */
440         size_t addr_len;
441
442         /** port number */
443         uint32_t port;
444
445         /** protocol */
446         enum BegemotSnmpdTransportProto proto;
447 };
448
449 /**
450  * IPv4 creation stuff. Parse the index, fill socket address and creates
451  * a port_sock.
452  *
453  * \param port          the port to create
454  * \param params        parameters from the SNMP SET
455  *
456  * \return SNMP error
457  */
458 static int
459 ipv4_create(struct inet_port *port, struct inet_port_params *params)
460 {
461         uint32_t ip;
462
463         if (params->addr_len != 4)
464                 return (SNMP_ERR_INCONS_VALUE);
465
466         memcpy(&ip, params->addr, 4);
467         struct port_sock *sock = calloc(1, sizeof(struct port_sock));
468         if (sock == NULL)
469                 return (SNMP_ERR_GENERR);
470
471         snmpd_input_init(&sock->input);
472
473         TAILQ_INSERT_HEAD(&port->socks, sock, link);
474
475         struct sockaddr_in *sin =
476             (struct sockaddr_in *)&sock->bind_addr;
477
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);
482
483         sock->port = port;
484
485         return (SNMP_ERR_NOERROR);
486 }
487
488 /*
489  * An IPv4 inet port is ready. Delegate to the generic code to read the data
490  * and react.
491  *
492  * \param fd    file descriptor that is ready
493  * \param udata inet_port pointer
494  */
495 static void
496 ipv4_input(int fd __unused, void *udata)
497 {
498         struct port_sock *sock = udata;
499
500         sock->input.peerlen = sizeof(struct sockaddr_in);
501         snmpd_input(&sock->input, &sock->port->tport);
502 }
503
504 /**
505  * Activate an IPv4 socket.
506  *
507  * \param sock  thhe socket to activate
508  *
509  * \return error code
510  */
511 static int
512 ipv4_activate_sock(struct port_sock *sock)
513 {
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);
517         }
518
519         const struct sockaddr_in *sin =
520             (const struct sockaddr_in *)&sock->bind_addr;
521
522         if (sin->sin_addr.s_addr == INADDR_ANY) {
523                 /* need to know from which address to return */
524                 static const int on = 1;
525
526                 if (setsockopt(sock->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on,
527                     sizeof(on)) == -1) {
528                         syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
529                         (void)close(sock->input.fd);
530                         sock->input.fd = -1;
531                         return (SNMP_ERR_GENERR);
532                 }
533                 sock->set_ret_source = true;
534         }
535
536         if (bind(sock->input.fd, (const struct sockaddr *)sin, sizeof(*sin))) {
537                 if (errno == EADDRNOTAVAIL) {
538                         (void)close(sock->input.fd);
539                         sock->input.fd = -1;
540                         return (SNMP_ERR_INCONS_NAME);
541                 }
542                 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(sin->sin_addr),
543                     ntohs(sin->sin_port));
544                 (void)close(sock->input.fd);
545                 sock->input.fd = -1;
546                 return (SNMP_ERR_GENERR);
547         }
548
549         if ((sock->input.id = fd_select(sock->input.fd, ipv4_input,
550             sock, NULL)) == NULL) {
551                 (void)close(sock->input.fd);
552                 sock->input.fd = -1;
553                 return (SNMP_ERR_GENERR);
554         }
555         sock->input.peer = (struct sockaddr *)&sock->ret_dest;
556
557         sock->parse_ctrl = ipv4_parse_ctrl;
558         sock->setsrc = ipv4_setsrc;
559
560         return (SNMP_ERR_NOERROR);
561 }
562
563 /**
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.
566  *
567  * \param p     inet port
568  *
569  * \return SNMP error code
570  */
571 static int
572 ipv4_activate(struct inet_port *p)
573 {
574         struct port_sock *sock = TAILQ_FIRST(&p->socks);
575         assert(sock);
576         assert(!TAILQ_NEXT(sock, link));
577
578         const int ret = ipv4_activate_sock(sock);
579         if (ret == SNMP_ERR_NOERROR)
580                 p->row_status = RowStatus_active;
581
582         return (ret);
583 }
584
585 /**
586  * Close an IPv4 socket. Keep the sock object.
587  *
588  * \param p     inet port
589  */
590 static void
591 ipv4_deactivate(struct inet_port *p)
592 {
593         struct port_sock *sock = TAILQ_FIRST(&p->socks);
594         assert(sock);
595         assert(!TAILQ_NEXT(sock, link));
596
597         snmpd_input_close(&sock->input);
598
599         p->row_status = RowStatus_notInService;
600 }
601
602 /**
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.
607  *
608  * \param sock  the sock object
609  * \param msg   the received message
610  */
611 static void
612 ipv4_parse_ctrl(struct port_sock *sock, const struct msghdr *msg)
613 {
614         struct sockcred *cred = NULL;
615
616         for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
617             cmsg = CMSG_NXTHDR(msg, cmsg)) {
618
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));
623
624                 } else if (cmsg->cmsg_level == SOL_SOCKET &&
625                     cmsg->cmsg_type == SCM_CREDS) {
626                         cred = (struct sockcred *)(void *)CMSG_DATA(cmsg);
627                 }
628         }
629
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);
634 }
635
636 /**
637  * Set the source address option for IPv4 sockets.
638  *
639  * \param sock  socket object
640  * \param msg   message
641  */
642 static void
643 ipv4_setsrc(struct port_sock *sock, struct msghdr *msg)
644 {
645         struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
646
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));
653
654         msg->msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
655 }
656
657 /**
658  * Common part of IPv6 creation. This is used by both ipv6_create() and
659  * ipv6z_create().
660  *
661  * \param port          the table row
662  * \param params        creation parameters
663  * \param scope_id      scope_id (0 or from index)
664  *
665  * \return SNMP_ERR_NOERROR if port has been created, error code otherwise
666  */
667 static int
668 ipv6_create_common(struct inet_port *port, struct inet_port_params *params,
669     u_int scope_id)
670 {
671         struct port_sock *sock = calloc(1, sizeof(struct port_sock));
672
673         if (sock == NULL)
674                 return (SNMP_ERR_GENERR);
675
676         struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&sock->bind_addr;
677
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;
683
684         memcpy(sin->sin6_addr.s6_addr, params->addr, 16);
685
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)));
691                 free(sock);
692                 return (SNMP_ERR_NO_CREATION);
693         }
694
695         sock->port = port;
696
697         snmpd_input_init(&sock->input);
698         TAILQ_INSERT_HEAD(&port->socks, sock, link);
699
700         return (SNMP_ERR_NOERROR);
701 }
702
703 /**
704  * IPv6 creation stuff. Parse the index, fill socket address and creates
705  * a port_sock.
706  *
707  * \param port          the port to create
708  * \param params        parameters from the SNMP SET
709  *
710  * \return SNMP error
711  */
712 static int
713 ipv6_create(struct inet_port *port, struct inet_port_params *params)
714 {
715         if (params->addr_len != 16)
716                 return (SNMP_ERR_INCONS_VALUE);
717
718         const int ret = ipv6_create_common(port, params, 0);
719         if (ret != SNMP_ERR_NOERROR)
720                 return (ret);
721
722         return (SNMP_ERR_NOERROR);
723 }
724
725 /*
726  * An IPv6 inet port is ready. Delegate to the generic code to read the data
727  * and react.
728  *
729  * \param fd    file descriptor that is ready
730  * \param udata inet_port pointer
731  */
732 static void
733 ipv6_input(int fd __unused, void *udata)
734 {
735         struct port_sock *sock = udata;
736
737         sock->input.peerlen = sizeof(struct sockaddr_in6);
738         snmpd_input(&sock->input, &sock->port->tport);
739 }
740
741 /**
742  * Activate an IPv6 socket.
743  *
744  * \param sock  thhe socket to activate
745  *
746  * \return error code
747  */
748 static int
749 ipv6_activate_sock(struct port_sock *sock)
750 {
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);
754         }
755
756         const struct sockaddr_in6 *sin =
757             (const struct sockaddr_in6 *)&sock->bind_addr;
758
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;
762
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);
767                         sock->input.fd = -1;
768                         return (SNMP_ERR_GENERR);
769                 }
770                 sock->set_ret_source = true;
771         }
772
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);
776                         sock->input.fd = -1;
777                         return (SNMP_ERR_INCONS_NAME);
778                 }
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);
784                 sock->input.fd = -1;
785                 return (SNMP_ERR_GENERR);
786         }
787         if ((sock->input.id = fd_select(sock->input.fd, ipv6_input,
788             sock, NULL)) == NULL) {
789                 (void)close(sock->input.fd);
790                 sock->input.fd = -1;
791                 return (SNMP_ERR_GENERR);
792         }
793         sock->input.peer = (struct sockaddr *)&sock->ret_dest;
794
795         sock->parse_ctrl = ipv6_parse_ctrl;
796         sock->setsrc = ipv6_setsrc;
797
798         return (SNMP_ERR_NOERROR);
799 }
800
801 /**
802  * Open an IPv6 socket.
803  *
804  * \param port  inet port
805  *
806  * \return SNMP error code
807  */
808 static int
809 ipv6_activate(struct inet_port *p)
810 {
811         struct port_sock *sock = TAILQ_FIRST(&p->socks);
812         assert(sock);
813
814         const int ret = ipv6_activate_sock(sock);
815
816         if (ret == SNMP_ERR_NOERROR)
817                 p->row_status = RowStatus_active;
818         return (ret);
819 }
820
821 /**
822  * Close an IPv6 socket. Keep the sock object.
823  *
824  * \param p     inet port
825  */
826 static void
827 ipv6_deactivate(struct inet_port *p)
828 {
829         struct port_sock *sock = TAILQ_FIRST(&p->socks);
830         assert(sock);
831         assert(!TAILQ_NEXT(sock, link));
832
833         snmpd_input_close(&sock->input);
834
835         p->row_status = RowStatus_notInService;
836 }
837
838 /**
839  * IPv6 specific part of message processing. The control data may contain
840  * credentials and packet info that contains the destination address of
841  * the packet.
842  *
843  * \param sock  the sock object
844  * \param msg   the received message
845  */
846 static void
847 ipv6_parse_ctrl(struct port_sock *sock, const struct msghdr *msg)
848 {
849         struct sockcred *cred = NULL;
850
851         for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
852             cmsg = CMSG_NXTHDR(msg, cmsg)) {
853
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 *)
858                             CMSG_DATA(cmsg);
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:
862                             info->ipi6_ifindex;
863                 } else if (cmsg->cmsg_level == SOL_SOCKET &&
864                     cmsg->cmsg_type == SCM_CREDS) {
865                         cred = (struct sockcred *)(void *)CMSG_DATA(cmsg);
866                 }
867         }
868
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);
873 }
874
875 /**
876  * Set the source address option for IPv4 sockets.
877  *
878  * \param sock  socket object
879  * \param msg   message
880  */
881 static void
882 ipv6_setsrc(struct port_sock *sock, struct msghdr *msg)
883 {
884         struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
885
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));
892
893         msg->msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
894 }
895
896 /**
897  * IPv6z creation stuff. Parse the index, fill socket address and creates
898  * a port_sock.
899  *
900  * \param port          the port to create
901  * \param params        parameters from the SNMP SET
902  *
903  * \return SNMP error
904  */
905 static int
906 ipv6z_create(struct inet_port *port, struct inet_port_params *params)
907 {
908         if (params->addr_len != 20)
909                 return (SNMP_ERR_INCONS_VALUE);
910
911         u_int scope_id = 0;
912         for (u_int i = 16; i < 20; i++) {
913                 scope_id <<= 8;
914                 scope_id |= params->addr[i];
915         }
916
917         const int ret = ipv6_create_common(port, params, scope_id);
918         if (ret != SNMP_ERR_NOERROR)
919                 return (ret);
920
921         return (SNMP_ERR_NOERROR);
922 }
923
924 /**
925  * DNS name creation stuff. Parse the index and save the string.
926  * This does not create a socket struct.
927  *
928  * \param port          the port to create
929  * \param params        parameters from the SNMP SET
930  *
931  * \return SNMP error
932  */
933 static int
934 dns_create(struct inet_port *port, struct inet_port_params *params)
935 {
936         if (params->addr_len > 64)
937                 return (SNMP_ERR_INCONS_VALUE);
938
939         if (strnlen(params->addr, params->addr_len) !=
940             params->addr_len)
941                 return (SNMP_ERR_INCONS_VALUE);
942
943         if ((port->dns_addr = realloc(params->addr,
944             params->addr_len + 1)) == NULL)
945                 return (SNMP_ERR_GENERR);
946
947         port->dns_addr[params->addr_len] = '\0';
948         params->addr = NULL;
949
950         port->dns_port = htons(params->port);
951
952         return (SNMP_ERR_NOERROR);
953 }
954
955 /**
956  * Open sockets. This loops through all the addresses returned by getaddrinfo
957  * and opens a socket for each of them.
958  *
959  * \param port  inet port
960  *
961  * \return SNMP error code
962  */
963 static int
964 dns_activate(struct inet_port *port)
965 {
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;
971
972         char portbuf[8];
973         sprintf(portbuf, "%hu", ntohs(port->dns_port));
974
975         struct addrinfo *res0;
976         int error = getaddrinfo(port->dns_addr[0] == '\0'
977             ? NULL : port->dns_addr,
978             portbuf, &hints, &res0);
979
980         if (error) {
981                 syslog(LOG_ERR, "cannot resolve address '%s': %s",
982                     port->dns_addr, gai_strerror(error));
983                 return (SNMP_ERR_GENERR);
984         }
985
986         for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) {
987                 if (res->ai_family != AF_INET && res->ai_family != AF_INET6)
988                         continue;
989
990                 struct port_sock *sock = calloc(1, sizeof(struct port_sock));
991                 if (sock == NULL)
992                         return (SNMP_ERR_GENERR);
993
994                 snmpd_input_init(&sock->input);
995                 sock->port = port;
996
997                 int ret = SNMP_ERR_NOERROR;
998
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);
1003                 } else {
1004                         *(struct sockaddr_in6 *)&sock->bind_addr =
1005                             *(struct sockaddr_in6 *)(void *)res->ai_addr;
1006                         ret = ipv6_activate_sock(sock);
1007                 }
1008
1009                 if (ret != SNMP_ERR_NOERROR)
1010                         free(sock);
1011                 else
1012                         TAILQ_INSERT_HEAD(&port->socks, sock, link);
1013         }
1014
1015         if (!TAILQ_EMPTY(&port->socks))
1016                 port->row_status = RowStatus_active;
1017
1018         freeaddrinfo(res0);
1019         return (SNMP_ERR_GENERR);
1020 }
1021
1022 /**
1023  * Deactive the socket. Close all open sockets and delete all sock objects.
1024  *
1025  * \param port  inet port
1026  */
1027 static void
1028 dns_deactivate(struct inet_port *port)
1029 {
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);
1034                 free(sock);
1035         }
1036         port->row_status = RowStatus_notInService;
1037 }
1038
1039 static int
1040 inet_create(struct inet_port_params *params, struct inet_port **pp)
1041 {
1042         int err = SNMP_ERR_NOERROR;
1043         struct inet_port *port = NULL;
1044
1045         if (params->port > 0xffff) {
1046                 err = SNMP_ERR_NO_CREATION;
1047                 goto fail;
1048         }
1049
1050         if ((port = malloc(sizeof(*port))) == NULL) {
1051                 err =  SNMP_ERR_GENERR;
1052                 goto fail;
1053         }
1054         memset(port, 0, sizeof(*port));
1055         TAILQ_INIT(&port->socks);
1056
1057         port->proto = params->proto;
1058         port->tport.index = params->index;
1059
1060         switch (params->type) {
1061
1062           case InetAddressType_ipv4:
1063                 port->create = ipv4_create;
1064                 port->activate = ipv4_activate;
1065                 port->deactivate = ipv4_deactivate;
1066                 break;
1067
1068           case InetAddressType_ipv6:
1069                 port->create = ipv6_create;
1070                 port->activate = ipv6_activate;
1071                 port->deactivate = ipv6_deactivate;
1072                 break;
1073
1074           case InetAddressType_ipv6z:
1075                 port->create = ipv6z_create;
1076                 port->activate = ipv6_activate;
1077                 port->deactivate = ipv6_deactivate;
1078                 break;
1079
1080           case InetAddressType_dns:
1081                 port->create = dns_create;
1082                 port->activate = dns_activate;
1083                 port->deactivate = dns_deactivate;
1084                 break;
1085
1086           default:
1087                 err = SNMP_ERR_NO_CREATION;
1088                 goto fail;
1089         }
1090
1091         if ((err = port->create(port, params)) != SNMP_ERR_NOERROR)
1092                 goto fail;
1093
1094         *pp = port;
1095         trans_insert_port(my_trans, &port->tport);
1096         return (err);
1097
1098 fail:
1099         free(port->dns_addr);
1100         free(port);
1101         return (err);
1102 }
1103
1104 static int
1105 create_and_go(struct snmp_context *ctx, struct inet_port_params *params)
1106 {
1107         int err;
1108         struct inet_port *port;
1109
1110         if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR)
1111                 return (err);
1112
1113         port->row_status = RowStatus_createAndGo;
1114         ctx->scratch->ptr1 = port;
1115
1116         if (community == COMM_INITIALIZE)
1117                 return (err);
1118
1119         return (inet_activate(&port->tport));
1120 }
1121
1122 static int
1123 create_and_wait(struct snmp_context *ctx, struct inet_port_params *params)
1124 {
1125         int err;
1126         struct inet_port *port;
1127
1128         if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR)
1129                 return (err);
1130
1131         port->row_status = RowStatus_createAndWait;
1132         ctx->scratch->ptr1 = port;
1133
1134         return (err);
1135 }
1136
1137 /**
1138  * This is called to set a RowStatus value in the port table during
1139  * SET processing.
1140  *
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.
1144  */
1145 static int
1146 inet_port_set(struct snmp_context *ctx, struct inet_port *port,
1147     struct inet_port_params *params, enum RowStatus nstatus)
1148 {
1149         switch (nstatus) {
1150
1151           case RowStatus_createAndGo:
1152                 if (port != NULL)
1153                         return (SNMP_ERR_INCONS_VALUE);
1154                 ctx->scratch->int1 = SET_CREATED;
1155                 return (create_and_go(ctx, params));
1156
1157           case RowStatus_createAndWait:
1158                 if (port != NULL)
1159                         return (SNMP_ERR_INCONS_VALUE);
1160                 ctx->scratch->int1 = SET_CREATED;
1161                 return (create_and_wait(ctx, params));
1162
1163           case RowStatus_active:
1164                 if (port == NULL)
1165                         return (SNMP_ERR_INCONS_VALUE);
1166
1167                 switch (port->row_status) {
1168
1169                   case RowStatus_notReady:
1170                         /* this can not happend */
1171                         abort();
1172
1173                   case RowStatus_notInService:
1174                         ctx->scratch->int1 = SET_ACTIVATED;
1175                         return (inet_activate(&port->tport));
1176
1177                   case RowStatus_active:
1178                         return (SNMP_ERR_NOERROR);
1179
1180                   case RowStatus_createAndGo:
1181                   case RowStatus_createAndWait:
1182                   case RowStatus_destroy:
1183                         abort();
1184                 }
1185                 break;
1186
1187           case RowStatus_notInService:
1188                 if (port == NULL)
1189                         return (SNMP_ERR_INCONS_VALUE);
1190
1191                 switch (port->row_status) {
1192
1193                   case RowStatus_notReady:
1194                         /* this can not happend */
1195                         abort();
1196
1197                   case RowStatus_notInService:
1198                         return (SNMP_ERR_NOERROR);
1199
1200                   case RowStatus_active:
1201                         /* this is done during commit */
1202                         ctx->scratch->int1 = SET_DEACTIVATE;
1203                         return (SNMP_ERR_NOERROR);
1204
1205                   case RowStatus_createAndGo:
1206                   case RowStatus_createAndWait:
1207                   case RowStatus_destroy:
1208                         abort();
1209                 }
1210                 break;
1211                 
1212           case RowStatus_destroy:
1213                 /* this is done during commit */
1214                 ctx->scratch->int1 = SET_DESTROY;
1215                 return (SNMP_ERR_NOERROR);
1216
1217           case RowStatus_notReady:
1218                 return (SNMP_ERR_WRONG_VALUE);
1219         }
1220         abort();
1221 }
1222
1223 /*
1224  * Port table
1225  */
1226 int
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)
1229 {
1230         asn_subid_t which = value->var.subs[sub - 1];
1231         struct inet_port *port;
1232         struct inet_port_params params;
1233         int ret;
1234
1235         switch (op) {
1236
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);
1242                 goto fetch;
1243
1244           case SNMP_OP_GET:
1245                 if ((port = (struct inet_port *)trans_find_port(my_trans,
1246                     &value->var, sub)) == NULL)
1247                         return (SNMP_ERR_NOSUCHNAME);
1248                 goto fetch;
1249
1250           case SNMP_OP_SET:
1251                 port = (struct inet_port *)trans_find_port(my_trans,
1252                     &value->var, sub);
1253
1254                 if (which != LEAF_begemotSnmpdTransInetStatus)
1255                         abort();
1256                 if (!isok_RowStatus(value->v.integer))
1257                         return (SNMP_ERR_WRONG_VALUE);
1258
1259                 if (index_decode(&value->var, sub, iidx, &params.type,
1260                     &params.addr, &params.addr_len, &params.port,
1261                     &params.proto))
1262                         return (SNMP_ERR_NO_CREATION);
1263
1264                 asn_slice_oid(&params.index, &value->var, sub, value->var.len);
1265
1266                 ret = inet_port_set(ctx, port, &params,
1267                     (enum RowStatus)value->v.integer);
1268
1269                 free(params.addr);
1270                 return (ret);
1271
1272           case SNMP_OP_ROLLBACK:
1273                 if ((port = (struct inet_port *)trans_find_port(my_trans,
1274                     &value->var, sub)) == NULL)
1275                         /* cannot happen */
1276                         abort();
1277
1278                 switch (ctx->scratch->int1) {
1279
1280                   case SET_CREATED:
1281                         /* newly created */
1282                         assert(port != NULL);
1283                         inet_destroy_port(&port->tport);
1284                         return (SNMP_ERR_NOERROR);
1285
1286                   case SET_DESTROY:
1287                         /* do it now */
1288                         assert(port != NULL);
1289                         return (SNMP_ERR_NOERROR);
1290
1291                   case SET_ACTIVATED:
1292                         deactivate_port(port);
1293                         return (SNMP_ERR_NOERROR);
1294
1295                   case SET_DEACTIVATE:
1296                         return (SNMP_ERR_NOERROR);
1297                 }
1298                 abort();
1299
1300           case SNMP_OP_COMMIT:
1301                 if ((port = (struct inet_port *)trans_find_port(my_trans,
1302                     &value->var, sub)) == NULL)
1303                         /* cannot happen */
1304                         abort();
1305
1306                 switch (ctx->scratch->int1) {
1307
1308                   case SET_CREATED:
1309                         /* newly created */
1310                         assert(port != NULL);
1311                         return (SNMP_ERR_NOERROR);
1312
1313                   case SET_DESTROY:
1314                         /* do it now */
1315                         assert(port != NULL);
1316                         inet_destroy_port(&port->tport);
1317                         return (SNMP_ERR_NOERROR);
1318
1319                   case SET_ACTIVATED:
1320                         return (SNMP_ERR_NOERROR);
1321
1322                   case SET_DEACTIVATE:
1323                         deactivate_port(port);
1324                         return (SNMP_ERR_NOERROR);
1325                 }
1326                 abort();
1327         }
1328         abort();
1329
1330   fetch:
1331         switch (which) {
1332
1333           case LEAF_begemotSnmpdTransInetStatus:
1334                 value->v.integer = port->row_status;
1335                 break;
1336
1337           default:
1338                 abort();
1339         }
1340
1341         return (SNMP_ERR_NOERROR);
1342 }