6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution of this software and documentation and use in source and
9 * binary forms, with or without modification, are permitted provided that
10 * the following conditions are met:
12 * 1. Redistributions of source code or documentation must retain the above
13 * copyright notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
19 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $Begemot: bsnmp/snmp_ntp/snmp_ntp.c,v 1.9 2005/10/06 07:15:01 brandt_h Exp $
32 * NTP interface for SNMPd.
35 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/select.h>
39 #include <sys/socket.h>
45 #elif defined(HAVE_INTTYPES_H)
60 #define NTPC_VERSION 3
64 #define NTPC_BIT_RESP 0x80
65 #define NTPC_BIT_ERROR 0x40
66 #define NTPC_BIT_MORE 0x20
68 #define NTPC_OPMASK 0x1f
69 #define NTPC_OP_READSTAT 1
70 #define NTPC_OP_READVAR 2
72 /* our module handle */
73 static struct lmodule *module;
76 static uint32_t ntp_debug;
77 #define DBG_DUMP_PKTS 0x01
78 #define DBG_DUMP_VARS 0x02
81 static const struct asn_oid oid_ntpMIB = OIDX_ntpMIB;
83 /* the Object Resource registration index */
84 static u_int reg_index;
86 /* last time we've fetch the system variables */
87 static uint64_t sysinfo_tick;
89 /* cached system variables */
90 static int32_t sys_leap;
92 static int32_t sys_stratum;
93 static int sysb_stratum;
94 static int32_t sys_precision;
95 static int sysb_precision;
96 static char *sys_rootdelay;
97 static char *sys_rootdispersion;
98 static char *sys_refid;
99 static char sys_reftime[8];
100 static int sysb_reftime;
101 static int32_t sys_poll;
102 static int sysb_poll;
103 static uint32_t sys_peer;
104 static int sysb_peer;
105 static u_char sys_clock[8];
106 static int sysb_clock;
107 static char *sys_system;
108 static char *sys_processor;
109 static int sysb_jitter;
110 static double sys_jitter;
111 static int sysb_stability;
112 static double sys_stability;
114 /* last time we've fetch the peer list */
115 static uint64_t peers_tick;
117 /* request sequence number generator */
118 static uint16_t seqno;
121 static int ntpd_sock;
122 static void *ntpd_fd;
125 /* required entries for macros */
127 TAILQ_ENTRY(peer) link;
129 int32_t config; /* config bit */
130 u_char srcadr[4]; /* PeerAddress */
131 uint32_t srcport; /* PeerPort */
132 u_char dstadr[4]; /* HostAddress */
133 uint32_t dstport; /* HostPort */
134 int32_t leap; /* Leap */
135 int32_t hmode; /* Mode */
136 int32_t stratum; /* Stratum */
137 int32_t ppoll; /* PeerPoll */
138 int32_t hpoll; /* HostPoll */
139 int32_t precision; /* Precision */
140 char *rootdelay; /* RootDelay */
141 char *rootdispersion;/* RootDispersion */
142 char *refid; /* RefId */
143 u_char reftime[8]; /* RefTime */
144 u_char orgtime[8]; /* OrgTime */
145 u_char rcvtime[8]; /* ReceiveTime */
146 u_char xmttime[8]; /* TransmitTime */
147 u_int32_t reach; /* Reach */
148 int32_t timer; /* Timer */
149 char *offset; /* Offset */
150 char *delay; /* Delay */
151 char *dispersion; /* Dispersion */
152 int32_t filt_entries;
154 TAILQ_HEAD(peer_list, peer);
157 static struct peer_list peers = TAILQ_HEAD_INITIALIZER(peers);
160 /* required fields */
161 struct asn_oid index;
162 TAILQ_ENTRY(filt) link;
168 TAILQ_HEAD(filt_list, filt);
170 /* list of filters */
171 static struct filt_list filts = TAILQ_HEAD_INITIALIZER(filts);
174 static u_char *ntp_host;
175 static u_char *ntp_port;
176 static uint32_t ntp_timeout;
178 static void ntpd_input(int, void *);
179 static int open_socket(void);
181 /* the initialization function */
183 ntp_init(struct lmodule *mod, int argc, char *argv[] __unused)
189 syslog(LOG_ERR, "bad number of arguments for %s", __func__);
193 ntp_host = strdup("localhost");
194 ntp_port = strdup("ntp");
195 ntp_timeout = 50; /* 0.5sec */
207 if (open_socket() != -1) {
208 ntpd_fd = fd_select(ntpd_sock, ntpd_input, NULL, module);
209 if (ntpd_fd == NULL) {
210 syslog(LOG_ERR, "fd_select failed on ntpd socket: %m");
214 reg_index = or_register(&oid_ntpMIB, "The MIB for NTP.", module);
218 * Called, when the module is to be unloaded after it was successfully loaded
224 or_unregister(reg_index);
225 fd_deselect(ntpd_fd);
230 const struct snmp_module config = {
231 .comment = "This module implements the NTP MIB",
236 .tree_size = ntp_CTREE_SIZE,
240 * Open the NTPD socket
245 struct addrinfo hints, *res, *res0;
249 memset(&hints, 0, sizeof(hints));
250 hints.ai_family = AF_INET;
251 hints.ai_socktype = SOCK_DGRAM;
253 error = getaddrinfo(ntp_host, ntp_port, &hints, &res0);
255 syslog(LOG_ERR, "%s(%s): %s", ntp_host, ntp_port,
256 gai_strerror(error));
261 cause = "no address";
262 errno = EADDRNOTAVAIL;
263 for (res = res0; res != NULL; res = res->ai_next) {
264 ntpd_sock = socket(res->ai_family, res->ai_socktype,
266 if (ntpd_sock == -1) {
270 if (connect(ntpd_sock, res->ai_addr, res->ai_addrlen) == -1) {
272 (void)close(ntpd_sock);
278 if (ntpd_sock == -1) {
279 syslog(LOG_ERR, "%s: %m", cause);
290 dump_packet(const u_char *pkt, size_t ret)
295 for (i = 0; i < ret; i += 8) {
297 for (j = 0; i + j < (size_t)ret && j < 8; j++)
298 sprintf(buf + strlen(buf), " %02x", pkt[i + j]);
299 syslog(LOG_INFO, "%04zu:%s", i, buf);
304 * Execute an NTP request.
307 ntpd_request(u_int op, u_int associd, const char *vars)
314 if ((rpkt = malloc(NTPC_MAX)) == NULL) {
315 syslog(LOG_ERR, "%m");
318 memset(rpkt, 0, NTPC_MAX);
321 *ptr++ = (NTPC_VERSION << 3) | NTPC_MODE;
332 *ptr++ = associd >> 8;
340 if (vlen > NTPC_DMAX) {
341 syslog(LOG_ERR, "NTP request too long (%zu)", vlen);
348 memcpy(ptr, vars, vlen);
351 /* skip data length (is already zero) */
354 while ((ptr - rpkt) % 4 != 0)
357 if (ntp_debug & DBG_DUMP_PKTS) {
358 syslog(LOG_INFO, "sending %zd bytes", ptr - rpkt);
359 dump_packet(rpkt, ptr - rpkt);
362 ret = send(ntpd_sock, rpkt, ptr - rpkt, 0);
364 syslog(LOG_ERR, "cannot send to ntpd: %m");
372 * Callback if packet arrived from NTPD
375 ntpd_read(uint16_t *op, uint16_t *associd, u_char **data, size_t *datalen)
377 u_char pkt[NTPC_MAX + 1];
382 u_int offset; /* current offset */
383 int more; /* more flag */
385 struct timeval inc, end, rem;
392 inc.tv_sec = ntp_timeout / 100;
393 inc.tv_usec = (ntp_timeout % 100) * 1000;
395 (void)gettimeofday(&end, NULL);
396 timeradd(&end, &inc, &end);
399 /* compute remaining time */
400 (void)gettimeofday(&rem, NULL);
401 if (timercmp(&rem, &end, >=)) {
406 timersub(&end, &rem, &rem);
411 FD_SET(ntpd_sock, &iset);
412 sel = select(ntpd_sock + 1, &iset, NULL, NULL, &rem);
416 syslog(LOG_ERR, "select ntpd_sock: %m");
421 syslog(LOG_ERR, "timeout on NTP connection");
427 ret = recv(ntpd_sock, pkt, sizeof(pkt), 0);
429 syslog(LOG_ERR, "error reading from ntpd: %m");
434 if (ntp_debug & DBG_DUMP_PKTS) {
435 syslog(LOG_INFO, "got %zd bytes", ret);
436 dump_packet(pkt, (size_t)ret);
440 if ((*ptr & 0x3f) != ((NTPC_VERSION << 3) | NTPC_MODE)) {
441 syslog(LOG_ERR, "unexpected packet version 0x%x", *ptr);
447 if (!(*ptr & NTPC_BIT_RESP)) {
448 syslog(LOG_ERR, "not a response packet");
451 if (*ptr & NTPC_BIT_ERROR) {
455 syslog(LOG_ERR, "error response: %.*s", (int)z, pkt + 12);
459 more = (*ptr & NTPC_BIT_MORE);
461 *op = *ptr++ & NTPC_OPMASK;
468 syslog(LOG_ERR, "expecting seqno %u, got %u", seqno, n);
478 *associd = *ptr++ << 8;
486 syslog(LOG_ERR, "offset: expecting %u, got %u", offset, n);
495 if ((size_t)ret < 12 + n) {
496 syslog(LOG_ERR, "packet too short");
500 nptr = realloc(*data, *datalen + n);
502 syslog(LOG_ERR, "cannot allocate memory: %m");
508 memcpy(*data + offset, ptr, n);
519 * Send a request and wait for the response
522 ntpd_dialog(u_int op, u_int associd, const char *vars, u_char **data,
528 if (ntpd_request(op, associd, vars) == -1)
530 if (ntpd_read(&rop, &rassocid, data, datalen) == -1)
534 syslog(LOG_ERR, "bad response op 0x%x", rop);
539 if (associd != rassocid) {
540 syslog(LOG_ERR, "response for wrong associd");
548 * Callback if packet arrived from NTPD
551 ntpd_input(int fd __unused, void *arg __unused)
558 if (ntpd_read(&op, &associd, &data, &datalen) == -1)
565 * Find the value of a variable
568 ntpd_parse(u_char **data, size_t *datalen, char **namep, char **valp)
571 u_char *end = ptr + *datalen;
575 /* skip leading spaces */
576 while (ptr < end && isspace((int)*ptr))
584 /* skip to space or '=' or ','*/
585 while (ptr < end && !isspace((int)*ptr) && *ptr != '=' && *ptr != ',')
591 while (ptr < end && isspace((int)*ptr))
594 if (ptr == end || endc == ',') {
597 *datalen -= ptr - *data;
606 while (ptr < end && *ptr != '"')
614 while (ptr < end && isspace((int)*ptr) && *ptr == ',')
619 /* skip to end of value */
620 while (ptr < end && *ptr != ',')
623 /* remove trailing blanks */
624 for (ptr1 = ptr; ptr1 > *valp; ptr1--)
625 if (!isspace((int)ptr1[-1]))
633 *datalen -= ptr - *data;
640 * Parse an int32 value
643 val_parse_int32(const char *val, int32_t *p, int32_t min, int32_t max, int base)
649 n = strtol(val, &end, base);
650 if (errno != 0 || *end != '\0')
652 if (n < min || n > max)
659 * Parse an uint32 value
662 val_parse_uint32(const char *val, uint32_t *p, uint32_t min, uint32_t max,
669 n = strtoul(val, &end, base);
670 if (errno != 0 || *end != '\0')
672 if (n < min || n > max)
682 val_parse_double(const char *val, double *p)
687 *p = strtod(val, &end);
688 if (errno != 0 || *end != '\0')
694 val_parse_ts(const char *val, char *buf)
699 if (strlen(val) > 2 && val[0] == '0' && val[1] == 'x') {
701 r = sscanf(val + 2, "%x.%x%n", &i, &f, &n);
702 if (r != 2 || (size_t)n != strlen(val + 2))
705 /* probably decimal */
706 r = sscanf(val, "%d.%d%n", &i, &f, &n);
707 if (r != 2 || (size_t)n != strlen(val))
722 * Parse an IP address. This resolves non-numeric names.
725 val_parse_ip(const char *val, u_char ip[4])
728 struct addrinfo hints, *res0;
729 struct sockaddr_in *sin_local;
731 r = sscanf(val, "%hhd.%hhd.%hhd.%hhd%n",
732 &ip[0], &ip[1], &ip[2], &ip[3], &n);
733 if (n == 4 && (size_t)n == strlen(val))
738 memset(&hints, 0, sizeof(hints));
739 hints.ai_family = AF_INET;
740 hints.ai_socktype = SOCK_DGRAM;
742 error = getaddrinfo(val, NULL, &hints, &res0);
744 syslog(LOG_ERR, "%s: %s", val, gai_strerror(error));
748 syslog(LOG_ERR, "%s: no address", val);
752 sin_local = (struct sockaddr_in *)(void *)res0->ai_addr;
753 ip[3] = sin_local->sin_addr.s_addr >> 24;
754 ip[2] = sin_local->sin_addr.s_addr >> 16;
755 ip[1] = sin_local->sin_addr.s_addr >> 8;
756 ip[0] = sin_local->sin_addr.s_addr >> 0;
774 if (ntpd_dialog(NTPC_OP_READVAR, 0,
775 "leap,stratum,precision,rootdelay,rootdispersion,refid,reftime,"
776 "poll,peer,clock,system,processor,jitter,stability",
785 sys_rootdelay = NULL;
786 free(sys_rootdispersion);
787 sys_rootdispersion = NULL;
797 sys_processor = NULL;
802 while (ntpd_parse(&ptr, &datalen, &name, &val)) {
803 if (ntp_debug & DBG_DUMP_VARS)
804 syslog(LOG_DEBUG, "%s: '%s'='%s'", __func__, name, val);
805 if (strcmp(name, "leap") == 0 ||
806 strcmp(name, "sys.leap") == 0) {
807 sysb_leap = val_parse_int32(val, &sys_leap,
810 } else if (strcmp(name, "stratum") == 0 ||
811 strcmp(name, "sys.stratum") == 0) {
812 sysb_stratum = val_parse_int32(val, &sys_stratum,
815 } else if (strcmp(name, "precision") == 0 ||
816 strcmp(name, "sys.precision") == 0) {
817 sysb_precision = val_parse_int32(val, &sys_precision,
818 INT32_MIN, INT32_MAX, 0);
820 } else if (strcmp(name, "rootdelay") == 0 ||
821 strcmp(name, "sys.rootdelay") == 0) {
822 sys_rootdelay = strdup(val);
824 } else if (strcmp(name, "rootdispersion") == 0 ||
825 strcmp(name, "sys.rootdispersion") == 0) {
826 sys_rootdispersion = strdup(val);
828 } else if (strcmp(name, "refid") == 0 ||
829 strcmp(name, "sys.refid") == 0) {
830 sys_refid = strdup(val);
832 } else if (strcmp(name, "reftime") == 0 ||
833 strcmp(name, "sys.reftime") == 0) {
834 sysb_reftime = val_parse_ts(val, sys_reftime);
836 } else if (strcmp(name, "poll") == 0 ||
837 strcmp(name, "sys.poll") == 0) {
838 sysb_poll = val_parse_int32(val, &sys_poll,
839 INT32_MIN, INT32_MAX, 0);
841 } else if (strcmp(name, "peer") == 0 ||
842 strcmp(name, "sys.peer") == 0) {
843 sysb_peer = val_parse_uint32(val, &sys_peer,
846 } else if (strcmp(name, "clock") == 0 ||
847 strcmp(name, "sys.clock") == 0) {
848 sysb_clock = val_parse_ts(val, sys_clock);
850 } else if (strcmp(name, "system") == 0 ||
851 strcmp(name, "sys.system") == 0) {
852 sys_system = strdup(val);
854 } else if (strcmp(name, "processor") == 0 ||
855 strcmp(name, "sys.processor") == 0) {
856 sys_processor = strdup(val);
858 } else if (strcmp(name, "jitter") == 0 ||
859 strcmp(name, "sys.jitter") == 0) {
860 sysb_jitter = val_parse_double(val, &sys_jitter);
862 } else if (strcmp(name, "stability") == 0 ||
863 strcmp(name, "sys.stability") == 0) {
864 sysb_stability = val_parse_double(val, &sys_stability);
873 parse_filt(char *val, uint16_t associd, int which)
880 for (w = strtok(val, " \t"); w != NULL; w = strtok(NULL, " \t")) {
881 TAILQ_FOREACH(f, &filts, link)
882 if (f->index.subs[0] == associd &&
883 f->index.subs[1] == (asn_subid_t)(cnt + 1))
886 f = malloc(sizeof(*f));
887 memset(f, 0, sizeof(*f));
889 f->index.subs[0] = associd;
890 f->index.subs[1] = cnt + 1;
892 INSERT_OBJECT_OID(f, &filts);
898 f->offset = strdup(w);
902 f->delay = strdup(w);
906 f->dispersion = strdup(w);
918 * Fetch the complete peer list
923 u_char *data, *pdata, *ptr;
924 size_t datalen, pdatalen;
931 /* free the old list */
932 while ((p = TAILQ_FIRST(&peers)) != NULL) {
933 TAILQ_REMOVE(&peers, p, link);
935 free(p->rootdispersion);
942 while ((f = TAILQ_FIRST(&filts)) != NULL) {
943 TAILQ_REMOVE(&filts, f, link);
950 /* fetch the list of associations */
951 if (ntpd_dialog(NTPC_OP_READSTAT, 0, NULL, &data, &datalen))
954 for (i = 0; i < (int)(datalen / 4); i++) {
955 associd = data[4 * i + 0] << 8;
956 associd |= data[4 * i + 1] << 0;
958 /* ask for the association variables */
959 if (ntpd_dialog(NTPC_OP_READVAR, associd,
960 "config,srcadr,srcport,dstadr,dstport,leap,hmode,stratum,"
961 "hpoll,ppoll,precision,rootdelay,rootdispersion,refid,"
962 "reftime,org,rec,xmt,reach,timer,offset,delay,dispersion,"
963 "filtdelay,filtoffset,filtdisp",
964 &pdata, &pdatalen)) {
969 /* now save and parse the data */
970 p = malloc(sizeof(*p));
973 syslog(LOG_ERR, "%m");
976 memset(p, 0, sizeof(*p));
978 INSERT_OBJECT_INT(p, &peers);
981 while (ntpd_parse(&ptr, &pdatalen, &name, &val)) {
982 if (ntp_debug & DBG_DUMP_VARS)
983 syslog(LOG_DEBUG, "%s: '%s'='%s'",
984 __func__, name, val);
985 if (strcmp(name, "config") == 0 ||
986 strcmp(name, "peer.config") == 0) {
987 val_parse_int32(val, &p->config, 0, 1, 0);
989 } else if (strcmp(name, "srcadr") == 0 ||
990 strcmp(name, "peer.srcadr") == 0) {
991 val_parse_ip(val, p->srcadr);
993 } else if (strcmp(name, "srcport") == 0 ||
994 strcmp(name, "peer.srcport") == 0) {
995 val_parse_uint32(val, &p->srcport,
998 } else if (strcmp(name, "dstadr") == 0 ||
999 strcmp(name, "peer.dstadr") == 0) {
1000 val_parse_ip(val, p->dstadr);
1002 } else if (strcmp(name, "dstport") == 0 ||
1003 strcmp(name, "peer.dstport") == 0) {
1004 val_parse_uint32(val, &p->dstport,
1007 } else if (strcmp(name, "leap") == 0 ||
1008 strcmp(name, "peer.leap") == 0) {
1009 val_parse_int32(val, &p->leap, 0, 3, 2);
1011 } else if (strcmp(name, "hmode") == 0 ||
1012 strcmp(name, "peer.hmode") == 0) {
1013 val_parse_int32(val, &p->hmode, 0, 7, 0);
1015 } else if (strcmp(name, "stratum") == 0 ||
1016 strcmp(name, "peer.stratum") == 0) {
1017 val_parse_int32(val, &p->stratum, 0, 255, 0);
1019 } else if (strcmp(name, "ppoll") == 0 ||
1020 strcmp(name, "peer.ppoll") == 0) {
1021 val_parse_int32(val, &p->ppoll,
1022 INT32_MIN, INT32_MAX, 0);
1024 } else if (strcmp(name, "hpoll") == 0 ||
1025 strcmp(name, "peer.hpoll") == 0) {
1026 val_parse_int32(val, &p->hpoll,
1027 INT32_MIN, INT32_MAX, 0);
1029 } else if (strcmp(name, "precision") == 0 ||
1030 strcmp(name, "peer.precision") == 0) {
1031 val_parse_int32(val, &p->hpoll,
1032 INT32_MIN, INT32_MAX, 0);
1034 } else if (strcmp(name, "rootdelay") == 0 ||
1035 strcmp(name, "peer.rootdelay") == 0) {
1036 p->rootdelay = strdup(val);
1038 } else if (strcmp(name, "rootdispersion") == 0 ||
1039 strcmp(name, "peer.rootdispersion") == 0) {
1040 p->rootdispersion = strdup(val);
1042 } else if (strcmp(name, "refid") == 0 ||
1043 strcmp(name, "peer.refid") == 0) {
1044 p->refid = strdup(val);
1046 } else if (strcmp(name, "reftime") == 0 ||
1047 strcmp(name, "sys.reftime") == 0) {
1048 val_parse_ts(val, p->reftime);
1050 } else if (strcmp(name, "org") == 0 ||
1051 strcmp(name, "sys.org") == 0) {
1052 val_parse_ts(val, p->orgtime);
1054 } else if (strcmp(name, "rec") == 0 ||
1055 strcmp(name, "sys.rec") == 0) {
1056 val_parse_ts(val, p->rcvtime);
1058 } else if (strcmp(name, "xmt") == 0 ||
1059 strcmp(name, "sys.xmt") == 0) {
1060 val_parse_ts(val, p->xmttime);
1062 } else if (strcmp(name, "reach") == 0 ||
1063 strcmp(name, "peer.reach") == 0) {
1064 val_parse_uint32(val, &p->reach,
1067 } else if (strcmp(name, "timer") == 0 ||
1068 strcmp(name, "peer.timer") == 0) {
1069 val_parse_int32(val, &p->timer,
1070 INT32_MIN, INT32_MAX, 0);
1072 } else if (strcmp(name, "offset") == 0 ||
1073 strcmp(name, "peer.offset") == 0) {
1074 p->offset = strdup(val);
1076 } else if (strcmp(name, "delay") == 0 ||
1077 strcmp(name, "peer.delay") == 0) {
1078 p->delay = strdup(val);
1080 } else if (strcmp(name, "dispersion") == 0 ||
1081 strcmp(name, "peer.dispersion") == 0) {
1082 p->dispersion = strdup(val);
1084 } else if (strcmp(name, "filtdelay") == 0 ||
1085 strcmp(name, "peer.filtdelay") == 0) {
1086 p->filt_entries = parse_filt(val, associd, 0);
1088 } else if (strcmp(name, "filtoffset") == 0 ||
1089 strcmp(name, "peer.filtoffset") == 0) {
1090 p->filt_entries = parse_filt(val, associd, 1);
1092 } else if (strcmp(name, "filtdisp") == 0 ||
1093 strcmp(name, "peer.filtdisp") == 0) {
1094 p->filt_entries = parse_filt(val, associd, 2);
1105 * System variables - read-only scalars only.
1108 op_ntpSystem(struct snmp_context *ctx __unused, struct snmp_value *value,
1109 u_int sub, u_int iidx __unused, enum snmp_op op)
1111 asn_subid_t which = value->var.subs[sub - 1];
1115 case SNMP_OP_GETNEXT:
1119 if (this_tick > sysinfo_tick) {
1120 if (fetch_sysinfo() == -1)
1121 return (SNMP_ERR_GENERR);
1122 sysinfo_tick = this_tick;
1127 case LEAF_ntpSysLeap:
1129 return (SNMP_ERR_NOSUCHNAME);
1130 value->v.integer = sys_leap;
1133 case LEAF_ntpSysStratum:
1135 return (SNMP_ERR_NOSUCHNAME);
1136 value->v.integer = sys_stratum;
1139 case LEAF_ntpSysPrecision:
1140 if (!sysb_precision)
1141 return (SNMP_ERR_NOSUCHNAME);
1142 value->v.integer = sys_precision;
1145 case LEAF_ntpSysRootDelay:
1146 if (sys_rootdelay == NULL)
1147 return (SNMP_ERR_NOSUCHNAME);
1148 return (string_get(value, sys_rootdelay, -1));
1150 case LEAF_ntpSysRootDispersion:
1151 if (sys_rootdispersion == NULL)
1152 return (SNMP_ERR_NOSUCHNAME);
1153 return (string_get(value, sys_rootdispersion, -1));
1155 case LEAF_ntpSysRefId:
1156 if (sys_refid == NULL)
1157 return (SNMP_ERR_NOSUCHNAME);
1158 return (string_get(value, sys_refid, -1));
1160 case LEAF_ntpSysRefTime:
1161 if (sysb_reftime == 0)
1162 return (SNMP_ERR_NOSUCHNAME);
1163 return (string_get(value, sys_reftime, 8));
1165 case LEAF_ntpSysPoll:
1167 return (SNMP_ERR_NOSUCHNAME);
1168 value->v.integer = sys_poll;
1171 case LEAF_ntpSysPeer:
1173 return (SNMP_ERR_NOSUCHNAME);
1174 value->v.uint32 = sys_peer;
1177 case LEAF_ntpSysClock:
1178 if (sysb_clock == 0)
1179 return (SNMP_ERR_NOSUCHNAME);
1180 return (string_get(value, sys_clock, 8));
1182 case LEAF_ntpSysSystem:
1183 if (sys_system == NULL)
1184 return (SNMP_ERR_NOSUCHNAME);
1185 return (string_get(value, sys_system, -1));
1187 case LEAF_ntpSysProcessor:
1188 if (sys_processor == NULL)
1189 return (SNMP_ERR_NOSUCHNAME);
1190 return (string_get(value, sys_processor, -1));
1195 return (SNMP_ERR_NOERROR);
1198 return (SNMP_ERR_NOT_WRITEABLE);
1200 case SNMP_OP_COMMIT:
1201 case SNMP_OP_ROLLBACK:
1208 op_ntpPeersVarTable(struct snmp_context *ctx __unused, struct snmp_value *value,
1209 u_int sub, u_int iidx, enum snmp_op op)
1211 asn_subid_t which = value->var.subs[sub - 1];
1215 if (this_tick > peers_tick) {
1216 if (fetch_peers() == -1)
1217 return (SNMP_ERR_GENERR);
1218 peers_tick = this_tick;
1223 case SNMP_OP_GETNEXT:
1224 t = NEXT_OBJECT_INT(&peers, &value->var, sub);
1226 return (SNMP_ERR_NOSUCHNAME);
1227 value->var.len = sub + 1;
1228 value->var.subs[sub] = t->index;
1232 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1234 return (SNMP_ERR_NOSUCHNAME);
1238 if (index_decode(&value->var, sub, iidx, &peer))
1239 return (SNMP_ERR_NO_CREATION);
1240 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1242 return (SNMP_ERR_NOT_WRITEABLE);
1243 return (SNMP_ERR_NO_CREATION);
1245 case SNMP_OP_COMMIT:
1246 case SNMP_OP_ROLLBACK:
1252 * Come here for GET and COMMIT
1256 case LEAF_ntpPeersConfigured:
1257 value->v.integer = t->config;
1260 case LEAF_ntpPeersPeerAddress:
1261 return (ip_get(value, t->srcadr));
1263 case LEAF_ntpPeersPeerPort:
1264 value->v.uint32 = t->srcport;
1267 case LEAF_ntpPeersHostAddress:
1268 return (ip_get(value, t->dstadr));
1270 case LEAF_ntpPeersHostPort:
1271 value->v.uint32 = t->dstport;
1274 case LEAF_ntpPeersLeap:
1275 value->v.integer = t->leap;
1278 case LEAF_ntpPeersMode:
1279 value->v.integer = t->hmode;
1282 case LEAF_ntpPeersStratum:
1283 value->v.integer = t->stratum;
1286 case LEAF_ntpPeersPeerPoll:
1287 value->v.integer = t->ppoll;
1290 case LEAF_ntpPeersHostPoll:
1291 value->v.integer = t->hpoll;
1294 case LEAF_ntpPeersPrecision:
1295 value->v.integer = t->precision;
1298 case LEAF_ntpPeersRootDelay:
1299 return (string_get(value, t->rootdelay, -1));
1301 case LEAF_ntpPeersRootDispersion:
1302 return (string_get(value, t->rootdispersion, -1));
1304 case LEAF_ntpPeersRefId:
1305 return (string_get(value, t->refid, -1));
1307 case LEAF_ntpPeersRefTime:
1308 return (string_get(value, t->reftime, 8));
1310 case LEAF_ntpPeersOrgTime:
1311 return (string_get(value, t->orgtime, 8));
1313 case LEAF_ntpPeersReceiveTime:
1314 return (string_get(value, t->rcvtime, 8));
1316 case LEAF_ntpPeersTransmitTime:
1317 return (string_get(value, t->xmttime, 8));
1319 case LEAF_ntpPeersReach:
1320 value->v.uint32 = t->reach;
1323 case LEAF_ntpPeersTimer:
1324 value->v.uint32 = t->timer;
1327 case LEAF_ntpPeersOffset:
1328 return (string_get(value, t->offset, -1));
1330 case LEAF_ntpPeersDelay:
1331 return (string_get(value, t->delay, -1));
1333 case LEAF_ntpPeersDispersion:
1334 return (string_get(value, t->dispersion, -1));
1339 return (SNMP_ERR_NOERROR);
1344 op_ntpFilterPeersVarTable(struct snmp_context *ctx __unused,
1345 struct snmp_value *value, u_int sub, u_int iidx, enum snmp_op op)
1347 asn_subid_t which = value->var.subs[sub - 1];
1351 if (this_tick > peers_tick) {
1352 if (fetch_peers() == -1)
1353 return (SNMP_ERR_GENERR);
1354 peers_tick = this_tick;
1359 case SNMP_OP_GETNEXT:
1360 t = NEXT_OBJECT_INT(&peers, &value->var, sub);
1362 return (SNMP_ERR_NOSUCHNAME);
1363 value->var.len = sub + 1;
1364 value->var.subs[sub] = t->index;
1368 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1370 return (SNMP_ERR_NOSUCHNAME);
1374 if (index_decode(&value->var, sub, iidx, &peer))
1375 return (SNMP_ERR_NO_CREATION);
1376 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1378 return (SNMP_ERR_NOT_WRITEABLE);
1379 return (SNMP_ERR_NO_CREATION);
1381 case SNMP_OP_COMMIT:
1382 case SNMP_OP_ROLLBACK:
1388 * Come here for GET and COMMIT
1392 case LEAF_ntpFilterValidEntries:
1393 value->v.integer = t->filt_entries;
1399 return (SNMP_ERR_NOERROR);
1403 op_ntpFilterRegisterTable(struct snmp_context *ctx __unused, struct snmp_value *value __unused,
1404 u_int sub __unused, u_int iidx __unused, enum snmp_op op __unused)
1406 asn_subid_t which = value->var.subs[sub - 1];
1411 if (this_tick > peers_tick) {
1412 if (fetch_peers() == -1)
1413 return (SNMP_ERR_GENERR);
1414 peers_tick = this_tick;
1419 case SNMP_OP_GETNEXT:
1420 t = NEXT_OBJECT_OID(&filts, &value->var, sub);
1422 return (SNMP_ERR_NOSUCHNAME);
1423 index_append(&value->var, sub, &t->index);
1427 t = FIND_OBJECT_OID(&filts, &value->var, sub);
1429 return (SNMP_ERR_NOSUCHNAME);
1433 if (index_decode(&value->var, sub, iidx, &peer, &filt))
1434 return (SNMP_ERR_NO_CREATION);
1435 t = FIND_OBJECT_OID(&filts, &value->var, sub);
1437 return (SNMP_ERR_NOT_WRITEABLE);
1438 return (SNMP_ERR_NO_CREATION);
1440 case SNMP_OP_COMMIT:
1441 case SNMP_OP_ROLLBACK:
1447 * Come here for GET and COMMIT
1451 case LEAF_ntpFilterPeersOffset:
1452 return (string_get(value, t->offset, -1));
1454 case LEAF_ntpFilterPeersDelay:
1455 return (string_get(value, t->delay, -1));
1457 case LEAF_ntpFilterPeersDispersion:
1458 return (string_get(value, t->dispersion, -1));
1463 return (SNMP_ERR_NOERROR);
1467 * System variables - read-only scalars only.
1470 op_begemot_ntp(struct snmp_context *ctx __unused, struct snmp_value *value,
1471 u_int sub, u_int iidx __unused, enum snmp_op op)
1473 asn_subid_t which = value->var.subs[sub - 1];
1478 case SNMP_OP_GETNEXT:
1484 case LEAF_begemotNtpHost:
1485 return (string_get(value, ntp_host, -1));
1487 case LEAF_begemotNtpPort:
1488 return (string_get(value, ntp_port, -1));
1490 case LEAF_begemotNtpTimeout:
1491 value->v.uint32 = ntp_timeout;
1492 return (SNMP_ERR_NOERROR);
1494 case LEAF_begemotNtpDebug:
1495 value->v.uint32 = ntp_debug;
1496 return (SNMP_ERR_NOERROR);
1498 case LEAF_begemotNtpJitter:
1499 if (this_tick > sysinfo_tick) {
1500 if (fetch_sysinfo() == -1)
1501 return (SNMP_ERR_GENERR);
1502 sysinfo_tick = this_tick;
1505 return (SNMP_ERR_NOSUCHNAME);
1506 value->v.counter64 = sys_jitter / 1000 * (1ULL << 32);
1507 return (SNMP_ERR_NOERROR);
1509 case LEAF_begemotNtpStability:
1510 if (this_tick > sysinfo_tick) {
1511 if (fetch_sysinfo() == -1)
1512 return (SNMP_ERR_GENERR);
1513 sysinfo_tick = this_tick;
1515 if (!sysb_stability)
1516 return (SNMP_ERR_NOSUCHNAME);
1517 value->v.counter64 = sys_stability * (1ULL << 32);
1518 return (SNMP_ERR_NOERROR);
1525 case LEAF_begemotNtpHost:
1526 /* only at initialization */
1527 if (community != COMM_INITIALIZE)
1528 return (SNMP_ERR_NOT_WRITEABLE);
1530 if ((ret = string_save(value, ctx, -1, &ntp_host))
1531 != SNMP_ERR_NOERROR)
1533 return (SNMP_ERR_NOERROR);
1535 case LEAF_begemotNtpPort:
1536 /* only at initialization */
1537 if (community != COMM_INITIALIZE)
1538 return (SNMP_ERR_NOT_WRITEABLE);
1540 if ((ret = string_save(value, ctx, -1, &ntp_port))
1541 != SNMP_ERR_NOERROR)
1543 return (SNMP_ERR_NOERROR);
1545 case LEAF_begemotNtpTimeout:
1546 ctx->scratch->int1 = ntp_timeout;
1547 if (value->v.uint32 < 1)
1548 return (SNMP_ERR_WRONG_VALUE);
1549 ntp_timeout = value->v.integer;
1550 return (SNMP_ERR_NOERROR);
1552 case LEAF_begemotNtpDebug:
1553 ctx->scratch->int1 = ntp_debug;
1554 ntp_debug = value->v.integer;
1555 return (SNMP_ERR_NOERROR);
1559 case SNMP_OP_ROLLBACK:
1562 case LEAF_begemotNtpHost:
1563 string_rollback(ctx, &ntp_host);
1564 return (SNMP_ERR_NOERROR);
1566 case LEAF_begemotNtpPort:
1567 string_rollback(ctx, &ntp_port);
1568 return (SNMP_ERR_NOERROR);
1570 case LEAF_begemotNtpTimeout:
1571 ntp_timeout = ctx->scratch->int1;
1572 return (SNMP_ERR_NOERROR);
1574 case LEAF_begemotNtpDebug:
1575 ntp_debug = ctx->scratch->int1;
1576 return (SNMP_ERR_NOERROR);
1580 case SNMP_OP_COMMIT:
1583 case LEAF_begemotNtpHost:
1585 return (SNMP_ERR_NOERROR);
1587 case LEAF_begemotNtpPort:
1589 return (SNMP_ERR_NOERROR);
1591 case LEAF_begemotNtpTimeout:
1592 case LEAF_begemotNtpDebug:
1593 return (SNMP_ERR_NOERROR);