2 * Copyright (c) 2005,2018
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)
57 #define SNMPTREE_TYPES
62 #define NTPC_VERSION 3
66 #define NTPC_BIT_RESP 0x80
67 #define NTPC_BIT_ERROR 0x40
68 #define NTPC_BIT_MORE 0x20
70 #define NTPC_OPMASK 0x1f
71 #define NTPC_OP_READSTAT 1
72 #define NTPC_OP_READVAR 2
74 /* our module handle */
75 static struct lmodule *module;
78 static uint32_t ntp_debug;
79 #define DBG_DUMP_PKTS 0x01
80 #define DBG_DUMP_VARS 0x02
83 static const struct asn_oid oid_ntpMIB = OIDX_ntpMIB;
85 /* the Object Resource registration index */
86 static u_int reg_index;
88 /* last time we've fetch the system variables */
89 static uint64_t sysinfo_tick;
91 /* cached system variables */
92 static int32_t sys_leap;
94 static int32_t sys_stratum;
95 static int sysb_stratum;
96 static int32_t sys_precision;
97 static int sysb_precision;
98 static char *sys_rootdelay;
99 static char *sys_rootdispersion;
100 static char *sys_refid;
101 static char sys_reftime[8];
102 static int sysb_reftime;
103 static int32_t sys_poll;
104 static int sysb_poll;
105 static uint32_t sys_peer;
106 static int sysb_peer;
107 static u_char sys_clock[8];
108 static int sysb_clock;
109 static char *sys_system;
110 static char *sys_processor;
111 static int sysb_jitter;
112 static double sys_jitter;
113 static int sysb_stability;
114 static double sys_stability;
116 /* last time we've fetch the peer list */
117 static uint64_t peers_tick;
119 /* request sequence number generator */
120 static uint16_t seqno;
123 static int ntpd_sock;
124 static void *ntpd_fd;
127 /* required entries for macros */
129 TAILQ_ENTRY(peer) link;
131 int32_t config; /* config bit */
132 u_char srcadr[4]; /* PeerAddress */
133 uint32_t srcport; /* PeerPort */
134 u_char dstadr[4]; /* HostAddress */
135 uint32_t dstport; /* HostPort */
136 int32_t leap; /* Leap */
137 int32_t hmode; /* Mode */
138 int32_t stratum; /* Stratum */
139 int32_t ppoll; /* PeerPoll */
140 int32_t hpoll; /* HostPoll */
141 int32_t precision; /* Precision */
142 char *rootdelay; /* RootDelay */
143 char *rootdispersion;/* RootDispersion */
144 char *refid; /* RefId */
145 u_char reftime[8]; /* RefTime */
146 u_char orgtime[8]; /* OrgTime */
147 u_char rcvtime[8]; /* ReceiveTime */
148 u_char xmttime[8]; /* TransmitTime */
149 u_int32_t reach; /* Reach */
150 int32_t timer; /* Timer */
151 char *offset; /* Offset */
152 char *delay; /* Delay */
153 char *dispersion; /* Dispersion */
154 int32_t filt_entries;
156 TAILQ_HEAD(peer_list, peer);
159 static struct peer_list peers = TAILQ_HEAD_INITIALIZER(peers);
162 /* required fields */
163 struct asn_oid index;
164 TAILQ_ENTRY(filt) link;
170 TAILQ_HEAD(filt_list, filt);
172 /* list of filters */
173 static struct filt_list filts = TAILQ_HEAD_INITIALIZER(filts);
176 static u_char *ntp_host;
177 static u_char *ntp_port;
178 static uint32_t ntp_timeout;
180 static void ntpd_input(int, void *);
181 static int open_socket(void);
183 /* the initialization function */
185 ntp_init(struct lmodule *mod, int argc, char *argv[] __unused)
191 syslog(LOG_ERR, "bad number of arguments for %s", __func__);
195 ntp_host = strdup("localhost");
196 ntp_port = strdup("ntp");
197 ntp_timeout = 50; /* 0.5sec */
209 if (open_socket() != -1) {
210 ntpd_fd = fd_select(ntpd_sock, ntpd_input, NULL, module);
211 if (ntpd_fd == NULL) {
212 syslog(LOG_ERR, "fd_select failed on ntpd socket: %m");
216 reg_index = or_register(&oid_ntpMIB, "The MIB for NTP.", module);
220 * Called, when the module is to be unloaded after it was successfully loaded
226 or_unregister(reg_index);
227 fd_deselect(ntpd_fd);
232 const struct snmp_module config = {
233 .comment = "This module implements the NTP MIB",
238 .tree_size = ntp_CTREE_SIZE,
242 * Open the NTPD socket
247 struct addrinfo hints, *res, *res0;
251 memset(&hints, 0, sizeof(hints));
252 hints.ai_family = AF_INET;
253 hints.ai_socktype = SOCK_DGRAM;
255 error = getaddrinfo(ntp_host, ntp_port, &hints, &res0);
257 syslog(LOG_ERR, "%s(%s): %s", ntp_host, ntp_port,
258 gai_strerror(error));
263 cause = "no address";
264 errno = EADDRNOTAVAIL;
265 for (res = res0; res != NULL; res = res->ai_next) {
266 ntpd_sock = socket(res->ai_family, res->ai_socktype,
268 if (ntpd_sock == -1) {
272 if (connect(ntpd_sock, res->ai_addr, res->ai_addrlen) == -1) {
274 (void)close(ntpd_sock);
280 if (ntpd_sock == -1) {
281 syslog(LOG_ERR, "%s: %m", cause);
292 dump_packet(const u_char *pkt, size_t ret)
297 for (i = 0; i < ret; i += 8) {
299 for (j = 0; i + j < (size_t)ret && j < 8; j++)
300 sprintf(buf + strlen(buf), " %02x", pkt[i + j]);
301 syslog(LOG_INFO, "%04zu:%s", i, buf);
306 * Execute an NTP request.
309 ntpd_request(u_int op, u_int associd, const char *vars)
316 if ((rpkt = malloc(NTPC_MAX)) == NULL) {
317 syslog(LOG_ERR, "%m");
320 memset(rpkt, 0, NTPC_MAX);
323 *ptr++ = (NTPC_VERSION << 3) | NTPC_MODE;
334 *ptr++ = associd >> 8;
342 if (vlen > NTPC_DMAX) {
343 syslog(LOG_ERR, "NTP request too long (%zu)", vlen);
350 memcpy(ptr, vars, vlen);
353 /* skip data length (is already zero) */
356 while ((ptr - rpkt) % 4 != 0)
359 if (ntp_debug & DBG_DUMP_PKTS) {
360 syslog(LOG_INFO, "sending %zd bytes", ptr - rpkt);
361 dump_packet(rpkt, ptr - rpkt);
364 ret = send(ntpd_sock, rpkt, ptr - rpkt, 0);
366 syslog(LOG_ERR, "cannot send to ntpd: %m");
374 * Callback if packet arrived from NTPD
377 ntpd_read(uint16_t *op, uint16_t *associd, u_char **data, size_t *datalen)
379 u_char pkt[NTPC_MAX + 1];
384 u_int offset; /* current offset */
385 int more; /* more flag */
387 struct timeval inc, end, rem;
394 inc.tv_sec = ntp_timeout / 100;
395 inc.tv_usec = (ntp_timeout % 100) * 1000;
397 (void)gettimeofday(&end, NULL);
398 timeradd(&end, &inc, &end);
401 /* compute remaining time */
402 (void)gettimeofday(&rem, NULL);
403 if (timercmp(&rem, &end, >=)) {
408 timersub(&end, &rem, &rem);
413 FD_SET(ntpd_sock, &iset);
414 sel = select(ntpd_sock + 1, &iset, NULL, NULL, &rem);
418 syslog(LOG_ERR, "select ntpd_sock: %m");
423 syslog(LOG_ERR, "timeout on NTP connection");
429 ret = recv(ntpd_sock, pkt, sizeof(pkt), 0);
431 syslog(LOG_ERR, "error reading from ntpd: %m");
436 if (ntp_debug & DBG_DUMP_PKTS) {
437 syslog(LOG_INFO, "got %zd bytes", ret);
438 dump_packet(pkt, (size_t)ret);
442 if ((*ptr & 0x3f) != ((NTPC_VERSION << 3) | NTPC_MODE)) {
443 syslog(LOG_ERR, "unexpected packet version 0x%x", *ptr);
449 if (!(*ptr & NTPC_BIT_RESP)) {
450 syslog(LOG_ERR, "not a response packet");
453 if (*ptr & NTPC_BIT_ERROR) {
457 syslog(LOG_ERR, "error response: %.*s", (int)z, pkt + 12);
461 more = (*ptr & NTPC_BIT_MORE);
463 *op = *ptr++ & NTPC_OPMASK;
470 syslog(LOG_ERR, "expecting seqno %u, got %u", seqno, n);
480 *associd = *ptr++ << 8;
488 syslog(LOG_ERR, "offset: expecting %u, got %u", offset, n);
497 if ((size_t)ret < 12 + n) {
498 syslog(LOG_ERR, "packet too short");
502 nptr = realloc(*data, *datalen + n);
504 syslog(LOG_ERR, "cannot allocate memory: %m");
510 memcpy(*data + offset, ptr, n);
521 * Send a request and wait for the response
524 ntpd_dialog(u_int op, u_int associd, const char *vars, u_char **data,
530 if (ntpd_request(op, associd, vars) == -1)
532 if (ntpd_read(&rop, &rassocid, data, datalen) == -1)
536 syslog(LOG_ERR, "bad response op 0x%x", rop);
541 if (associd != rassocid) {
542 syslog(LOG_ERR, "response for wrong associd");
550 * Callback if packet arrived from NTPD
553 ntpd_input(int fd __unused, void *arg __unused)
560 if (ntpd_read(&op, &associd, &data, &datalen) == -1)
567 * Find the value of a variable
570 ntpd_parse(u_char **data, size_t *datalen, char **namep, char **valp)
573 u_char *end = ptr + *datalen;
577 /* skip leading spaces */
578 while (ptr < end && isspace((int)*ptr))
586 /* skip to space or '=' or ','*/
587 while (ptr < end && !isspace((int)*ptr) && *ptr != '=' && *ptr != ',')
593 while (ptr < end && isspace((int)*ptr))
596 if (ptr == end || endc == ',') {
599 *datalen -= ptr - *data;
608 while (ptr < end && *ptr != '"')
616 while (ptr < end && isspace((int)*ptr) && *ptr == ',')
621 /* skip to end of value */
622 while (ptr < end && *ptr != ',')
625 /* remove trailing blanks */
626 for (ptr1 = ptr; ptr1 > *valp; ptr1--)
627 if (!isspace((int)ptr1[-1]))
635 *datalen -= ptr - *data;
642 * Parse an int32 value
645 val_parse_int32(const char *val, int32_t *p, int32_t min, int32_t max, int base)
651 n = strtol(val, &end, base);
652 if (errno != 0 || *end != '\0')
654 if (n < min || n > max)
661 * Parse an uint32 value
664 val_parse_uint32(const char *val, uint32_t *p, uint32_t min, uint32_t max,
671 n = strtoul(val, &end, base);
672 if (errno != 0 || *end != '\0')
674 if (n < min || n > max)
684 val_parse_double(const char *val, double *p)
689 *p = strtod(val, &end);
690 if (errno != 0 || *end != '\0')
696 val_parse_ts(const char *val, char *buf)
701 if (strlen(val) > 2 && val[0] == '0' && val[1] == 'x') {
703 r = sscanf(val + 2, "%x.%x%n", &i, &f, &n);
704 if (r != 2 || (size_t)n != strlen(val + 2))
707 /* probably decimal */
708 r = sscanf(val, "%d.%d%n", &i, &f, &n);
709 if (r != 2 || (size_t)n != strlen(val))
724 * Parse an IP address. This resolves non-numeric names.
727 val_parse_ip(const char *val, u_char ip[4])
730 struct addrinfo hints, *res0;
731 struct sockaddr_in *sin_local;
733 r = sscanf(val, "%hhd.%hhd.%hhd.%hhd%n",
734 &ip[0], &ip[1], &ip[2], &ip[3], &n);
735 if (n == 4 && (size_t)n == strlen(val))
740 memset(&hints, 0, sizeof(hints));
741 hints.ai_family = AF_INET;
742 hints.ai_socktype = SOCK_DGRAM;
744 error = getaddrinfo(val, NULL, &hints, &res0);
746 syslog(LOG_ERR, "%s: %s", val, gai_strerror(error));
750 syslog(LOG_ERR, "%s: no address", val);
754 sin_local = (struct sockaddr_in *)(void *)res0->ai_addr;
755 ip[3] = sin_local->sin_addr.s_addr >> 24;
756 ip[2] = sin_local->sin_addr.s_addr >> 16;
757 ip[1] = sin_local->sin_addr.s_addr >> 8;
758 ip[0] = sin_local->sin_addr.s_addr >> 0;
776 if (ntpd_dialog(NTPC_OP_READVAR, 0,
777 "leap,stratum,precision,rootdelay,rootdispersion,refid,reftime,"
778 "poll,peer,clock,system,processor,jitter,stability",
787 sys_rootdelay = NULL;
788 free(sys_rootdispersion);
789 sys_rootdispersion = NULL;
799 sys_processor = NULL;
804 while (ntpd_parse(&ptr, &datalen, &name, &val)) {
805 if (ntp_debug & DBG_DUMP_VARS)
806 syslog(LOG_DEBUG, "%s: '%s'='%s'", __func__, name, val);
807 if (strcmp(name, "leap") == 0 ||
808 strcmp(name, "sys.leap") == 0) {
809 sysb_leap = val_parse_int32(val, &sys_leap,
812 } else if (strcmp(name, "stratum") == 0 ||
813 strcmp(name, "sys.stratum") == 0) {
814 sysb_stratum = val_parse_int32(val, &sys_stratum,
817 } else if (strcmp(name, "precision") == 0 ||
818 strcmp(name, "sys.precision") == 0) {
819 sysb_precision = val_parse_int32(val, &sys_precision,
820 INT32_MIN, INT32_MAX, 0);
822 } else if (strcmp(name, "rootdelay") == 0 ||
823 strcmp(name, "sys.rootdelay") == 0) {
824 sys_rootdelay = strdup(val);
826 } else if (strcmp(name, "rootdispersion") == 0 ||
827 strcmp(name, "sys.rootdispersion") == 0) {
828 sys_rootdispersion = strdup(val);
830 } else if (strcmp(name, "refid") == 0 ||
831 strcmp(name, "sys.refid") == 0) {
832 sys_refid = strdup(val);
834 } else if (strcmp(name, "reftime") == 0 ||
835 strcmp(name, "sys.reftime") == 0) {
836 sysb_reftime = val_parse_ts(val, sys_reftime);
838 } else if (strcmp(name, "poll") == 0 ||
839 strcmp(name, "sys.poll") == 0) {
840 sysb_poll = val_parse_int32(val, &sys_poll,
841 INT32_MIN, INT32_MAX, 0);
843 } else if (strcmp(name, "peer") == 0 ||
844 strcmp(name, "sys.peer") == 0) {
845 sysb_peer = val_parse_uint32(val, &sys_peer,
848 } else if (strcmp(name, "clock") == 0 ||
849 strcmp(name, "sys.clock") == 0) {
850 sysb_clock = val_parse_ts(val, sys_clock);
852 } else if (strcmp(name, "system") == 0 ||
853 strcmp(name, "sys.system") == 0) {
854 sys_system = strdup(val);
856 } else if (strcmp(name, "processor") == 0 ||
857 strcmp(name, "sys.processor") == 0) {
858 sys_processor = strdup(val);
860 } else if (strcmp(name, "jitter") == 0 ||
861 strcmp(name, "sys.jitter") == 0) {
862 sysb_jitter = val_parse_double(val, &sys_jitter);
864 } else if (strcmp(name, "stability") == 0 ||
865 strcmp(name, "sys.stability") == 0) {
866 sysb_stability = val_parse_double(val, &sys_stability);
875 parse_filt(char *val, uint16_t associd, int which)
882 for (w = strtok(val, " \t"); w != NULL; w = strtok(NULL, " \t")) {
883 TAILQ_FOREACH(f, &filts, link)
884 if (f->index.subs[0] == associd &&
885 f->index.subs[1] == (asn_subid_t)(cnt + 1))
888 f = malloc(sizeof(*f));
889 memset(f, 0, sizeof(*f));
891 f->index.subs[0] = associd;
892 f->index.subs[1] = cnt + 1;
894 INSERT_OBJECT_OID(f, &filts);
900 f->offset = strdup(w);
904 f->delay = strdup(w);
908 f->dispersion = strdup(w);
920 * Fetch the complete peer list
925 u_char *data, *pdata, *ptr;
926 size_t datalen, pdatalen;
933 /* free the old list */
934 while ((p = TAILQ_FIRST(&peers)) != NULL) {
935 TAILQ_REMOVE(&peers, p, link);
937 free(p->rootdispersion);
944 while ((f = TAILQ_FIRST(&filts)) != NULL) {
945 TAILQ_REMOVE(&filts, f, link);
952 /* fetch the list of associations */
953 if (ntpd_dialog(NTPC_OP_READSTAT, 0, NULL, &data, &datalen))
956 for (i = 0; i < (int)(datalen / 4); i++) {
957 associd = data[4 * i + 0] << 8;
958 associd |= data[4 * i + 1] << 0;
960 /* ask for the association variables */
961 if (ntpd_dialog(NTPC_OP_READVAR, associd,
962 "config,srcadr,srcport,dstadr,dstport,leap,hmode,stratum,"
963 "hpoll,ppoll,precision,rootdelay,rootdispersion,refid,"
964 "reftime,org,rec,xmt,reach,timer,offset,delay,dispersion,"
965 "filtdelay,filtoffset,filtdisp",
966 &pdata, &pdatalen)) {
971 /* now save and parse the data */
972 p = malloc(sizeof(*p));
975 syslog(LOG_ERR, "%m");
978 memset(p, 0, sizeof(*p));
980 INSERT_OBJECT_INT(p, &peers);
983 while (ntpd_parse(&ptr, &pdatalen, &name, &val)) {
984 if (ntp_debug & DBG_DUMP_VARS)
985 syslog(LOG_DEBUG, "%s: '%s'='%s'",
986 __func__, name, val);
987 if (strcmp(name, "config") == 0 ||
988 strcmp(name, "peer.config") == 0) {
989 val_parse_int32(val, &p->config, 0, 1, 0);
991 } else if (strcmp(name, "srcadr") == 0 ||
992 strcmp(name, "peer.srcadr") == 0) {
993 val_parse_ip(val, p->srcadr);
995 } else if (strcmp(name, "srcport") == 0 ||
996 strcmp(name, "peer.srcport") == 0) {
997 val_parse_uint32(val, &p->srcport,
1000 } else if (strcmp(name, "dstadr") == 0 ||
1001 strcmp(name, "peer.dstadr") == 0) {
1002 val_parse_ip(val, p->dstadr);
1004 } else if (strcmp(name, "dstport") == 0 ||
1005 strcmp(name, "peer.dstport") == 0) {
1006 val_parse_uint32(val, &p->dstport,
1009 } else if (strcmp(name, "leap") == 0 ||
1010 strcmp(name, "peer.leap") == 0) {
1011 val_parse_int32(val, &p->leap, 0, 3, 2);
1013 } else if (strcmp(name, "hmode") == 0 ||
1014 strcmp(name, "peer.hmode") == 0) {
1015 val_parse_int32(val, &p->hmode, 0, 7, 0);
1017 } else if (strcmp(name, "stratum") == 0 ||
1018 strcmp(name, "peer.stratum") == 0) {
1019 val_parse_int32(val, &p->stratum, 0, 255, 0);
1021 } else if (strcmp(name, "ppoll") == 0 ||
1022 strcmp(name, "peer.ppoll") == 0) {
1023 val_parse_int32(val, &p->ppoll,
1024 INT32_MIN, INT32_MAX, 0);
1026 } else if (strcmp(name, "hpoll") == 0 ||
1027 strcmp(name, "peer.hpoll") == 0) {
1028 val_parse_int32(val, &p->hpoll,
1029 INT32_MIN, INT32_MAX, 0);
1031 } else if (strcmp(name, "precision") == 0 ||
1032 strcmp(name, "peer.precision") == 0) {
1033 val_parse_int32(val, &p->hpoll,
1034 INT32_MIN, INT32_MAX, 0);
1036 } else if (strcmp(name, "rootdelay") == 0 ||
1037 strcmp(name, "peer.rootdelay") == 0) {
1038 p->rootdelay = strdup(val);
1040 } else if (strcmp(name, "rootdispersion") == 0 ||
1041 strcmp(name, "peer.rootdispersion") == 0) {
1042 p->rootdispersion = strdup(val);
1044 } else if (strcmp(name, "refid") == 0 ||
1045 strcmp(name, "peer.refid") == 0) {
1046 p->refid = strdup(val);
1048 } else if (strcmp(name, "reftime") == 0 ||
1049 strcmp(name, "sys.reftime") == 0) {
1050 val_parse_ts(val, p->reftime);
1052 } else if (strcmp(name, "org") == 0 ||
1053 strcmp(name, "sys.org") == 0) {
1054 val_parse_ts(val, p->orgtime);
1056 } else if (strcmp(name, "rec") == 0 ||
1057 strcmp(name, "sys.rec") == 0) {
1058 val_parse_ts(val, p->rcvtime);
1060 } else if (strcmp(name, "xmt") == 0 ||
1061 strcmp(name, "sys.xmt") == 0) {
1062 val_parse_ts(val, p->xmttime);
1064 } else if (strcmp(name, "reach") == 0 ||
1065 strcmp(name, "peer.reach") == 0) {
1066 val_parse_uint32(val, &p->reach,
1069 } else if (strcmp(name, "timer") == 0 ||
1070 strcmp(name, "peer.timer") == 0) {
1071 val_parse_int32(val, &p->timer,
1072 INT32_MIN, INT32_MAX, 0);
1074 } else if (strcmp(name, "offset") == 0 ||
1075 strcmp(name, "peer.offset") == 0) {
1076 p->offset = strdup(val);
1078 } else if (strcmp(name, "delay") == 0 ||
1079 strcmp(name, "peer.delay") == 0) {
1080 p->delay = strdup(val);
1082 } else if (strcmp(name, "dispersion") == 0 ||
1083 strcmp(name, "peer.dispersion") == 0) {
1084 p->dispersion = strdup(val);
1086 } else if (strcmp(name, "filtdelay") == 0 ||
1087 strcmp(name, "peer.filtdelay") == 0) {
1088 p->filt_entries = parse_filt(val, associd, 0);
1090 } else if (strcmp(name, "filtoffset") == 0 ||
1091 strcmp(name, "peer.filtoffset") == 0) {
1092 p->filt_entries = parse_filt(val, associd, 1);
1094 } else if (strcmp(name, "filtdisp") == 0 ||
1095 strcmp(name, "peer.filtdisp") == 0) {
1096 p->filt_entries = parse_filt(val, associd, 2);
1107 * System variables - read-only scalars only.
1110 op_ntpSystem(struct snmp_context *ctx __unused, struct snmp_value *value,
1111 u_int sub, u_int iidx __unused, enum snmp_op op)
1113 asn_subid_t which = value->var.subs[sub - 1];
1117 case SNMP_OP_GETNEXT:
1121 if (this_tick > sysinfo_tick) {
1122 if (fetch_sysinfo() == -1)
1123 return (SNMP_ERR_GENERR);
1124 sysinfo_tick = this_tick;
1129 case LEAF_ntpSysLeap:
1131 return (SNMP_ERR_NOSUCHNAME);
1132 value->v.integer = sys_leap;
1135 case LEAF_ntpSysStratum:
1137 return (SNMP_ERR_NOSUCHNAME);
1138 value->v.integer = sys_stratum;
1141 case LEAF_ntpSysPrecision:
1142 if (!sysb_precision)
1143 return (SNMP_ERR_NOSUCHNAME);
1144 value->v.integer = sys_precision;
1147 case LEAF_ntpSysRootDelay:
1148 if (sys_rootdelay == NULL)
1149 return (SNMP_ERR_NOSUCHNAME);
1150 return (string_get(value, sys_rootdelay, -1));
1152 case LEAF_ntpSysRootDispersion:
1153 if (sys_rootdispersion == NULL)
1154 return (SNMP_ERR_NOSUCHNAME);
1155 return (string_get(value, sys_rootdispersion, -1));
1157 case LEAF_ntpSysRefId:
1158 if (sys_refid == NULL)
1159 return (SNMP_ERR_NOSUCHNAME);
1160 return (string_get(value, sys_refid, -1));
1162 case LEAF_ntpSysRefTime:
1163 if (sysb_reftime == 0)
1164 return (SNMP_ERR_NOSUCHNAME);
1165 return (string_get(value, sys_reftime, 8));
1167 case LEAF_ntpSysPoll:
1169 return (SNMP_ERR_NOSUCHNAME);
1170 value->v.integer = sys_poll;
1173 case LEAF_ntpSysPeer:
1175 return (SNMP_ERR_NOSUCHNAME);
1176 value->v.uint32 = sys_peer;
1179 case LEAF_ntpSysClock:
1180 if (sysb_clock == 0)
1181 return (SNMP_ERR_NOSUCHNAME);
1182 return (string_get(value, sys_clock, 8));
1184 case LEAF_ntpSysSystem:
1185 if (sys_system == NULL)
1186 return (SNMP_ERR_NOSUCHNAME);
1187 return (string_get(value, sys_system, -1));
1189 case LEAF_ntpSysProcessor:
1190 if (sys_processor == NULL)
1191 return (SNMP_ERR_NOSUCHNAME);
1192 return (string_get(value, sys_processor, -1));
1197 return (SNMP_ERR_NOERROR);
1200 return (SNMP_ERR_NOT_WRITEABLE);
1202 case SNMP_OP_COMMIT:
1203 case SNMP_OP_ROLLBACK:
1210 op_ntpPeersVarTable(struct snmp_context *ctx __unused, struct snmp_value *value,
1211 u_int sub, u_int iidx, enum snmp_op op)
1213 asn_subid_t which = value->var.subs[sub - 1];
1217 if (this_tick > peers_tick) {
1218 if (fetch_peers() == -1)
1219 return (SNMP_ERR_GENERR);
1220 peers_tick = this_tick;
1225 case SNMP_OP_GETNEXT:
1226 t = NEXT_OBJECT_INT(&peers, &value->var, sub);
1228 return (SNMP_ERR_NOSUCHNAME);
1229 value->var.len = sub + 1;
1230 value->var.subs[sub] = t->index;
1234 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1236 return (SNMP_ERR_NOSUCHNAME);
1240 if (index_decode(&value->var, sub, iidx, &peer))
1241 return (SNMP_ERR_NO_CREATION);
1242 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1244 return (SNMP_ERR_NOT_WRITEABLE);
1245 return (SNMP_ERR_NO_CREATION);
1247 case SNMP_OP_COMMIT:
1248 case SNMP_OP_ROLLBACK:
1254 * Come here for GET and COMMIT
1258 case LEAF_ntpPeersConfigured:
1259 value->v.integer = t->config;
1262 case LEAF_ntpPeersPeerAddress:
1263 return (ip_get(value, t->srcadr));
1265 case LEAF_ntpPeersPeerPort:
1266 value->v.uint32 = t->srcport;
1269 case LEAF_ntpPeersHostAddress:
1270 return (ip_get(value, t->dstadr));
1272 case LEAF_ntpPeersHostPort:
1273 value->v.uint32 = t->dstport;
1276 case LEAF_ntpPeersLeap:
1277 value->v.integer = t->leap;
1280 case LEAF_ntpPeersMode:
1281 value->v.integer = t->hmode;
1284 case LEAF_ntpPeersStratum:
1285 value->v.integer = t->stratum;
1288 case LEAF_ntpPeersPeerPoll:
1289 value->v.integer = t->ppoll;
1292 case LEAF_ntpPeersHostPoll:
1293 value->v.integer = t->hpoll;
1296 case LEAF_ntpPeersPrecision:
1297 value->v.integer = t->precision;
1300 case LEAF_ntpPeersRootDelay:
1301 return (string_get(value, t->rootdelay, -1));
1303 case LEAF_ntpPeersRootDispersion:
1304 return (string_get(value, t->rootdispersion, -1));
1306 case LEAF_ntpPeersRefId:
1307 return (string_get(value, t->refid, -1));
1309 case LEAF_ntpPeersRefTime:
1310 return (string_get(value, t->reftime, 8));
1312 case LEAF_ntpPeersOrgTime:
1313 return (string_get(value, t->orgtime, 8));
1315 case LEAF_ntpPeersReceiveTime:
1316 return (string_get(value, t->rcvtime, 8));
1318 case LEAF_ntpPeersTransmitTime:
1319 return (string_get(value, t->xmttime, 8));
1321 case LEAF_ntpPeersReach:
1322 value->v.uint32 = t->reach;
1325 case LEAF_ntpPeersTimer:
1326 value->v.uint32 = t->timer;
1329 case LEAF_ntpPeersOffset:
1330 return (string_get(value, t->offset, -1));
1332 case LEAF_ntpPeersDelay:
1333 return (string_get(value, t->delay, -1));
1335 case LEAF_ntpPeersDispersion:
1336 return (string_get(value, t->dispersion, -1));
1341 return (SNMP_ERR_NOERROR);
1346 op_ntpFilterPeersVarTable(struct snmp_context *ctx __unused,
1347 struct snmp_value *value, u_int sub, u_int iidx, enum snmp_op op)
1349 asn_subid_t which = value->var.subs[sub - 1];
1353 if (this_tick > peers_tick) {
1354 if (fetch_peers() == -1)
1355 return (SNMP_ERR_GENERR);
1356 peers_tick = this_tick;
1361 case SNMP_OP_GETNEXT:
1362 t = NEXT_OBJECT_INT(&peers, &value->var, sub);
1364 return (SNMP_ERR_NOSUCHNAME);
1365 value->var.len = sub + 1;
1366 value->var.subs[sub] = t->index;
1370 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1372 return (SNMP_ERR_NOSUCHNAME);
1376 if (index_decode(&value->var, sub, iidx, &peer))
1377 return (SNMP_ERR_NO_CREATION);
1378 t = FIND_OBJECT_INT(&peers, &value->var, sub);
1380 return (SNMP_ERR_NOT_WRITEABLE);
1381 return (SNMP_ERR_NO_CREATION);
1383 case SNMP_OP_COMMIT:
1384 case SNMP_OP_ROLLBACK:
1390 * Come here for GET and COMMIT
1394 case LEAF_ntpFilterValidEntries:
1395 value->v.integer = t->filt_entries;
1401 return (SNMP_ERR_NOERROR);
1405 op_ntpFilterRegisterTable(struct snmp_context *ctx __unused, struct snmp_value *value __unused,
1406 u_int sub __unused, u_int iidx __unused, enum snmp_op op __unused)
1408 asn_subid_t which = value->var.subs[sub - 1];
1413 if (this_tick > peers_tick) {
1414 if (fetch_peers() == -1)
1415 return (SNMP_ERR_GENERR);
1416 peers_tick = this_tick;
1421 case SNMP_OP_GETNEXT:
1422 t = NEXT_OBJECT_OID(&filts, &value->var, sub);
1424 return (SNMP_ERR_NOSUCHNAME);
1425 index_append(&value->var, sub, &t->index);
1429 t = FIND_OBJECT_OID(&filts, &value->var, sub);
1431 return (SNMP_ERR_NOSUCHNAME);
1435 if (index_decode(&value->var, sub, iidx, &peer, &filt))
1436 return (SNMP_ERR_NO_CREATION);
1437 t = FIND_OBJECT_OID(&filts, &value->var, sub);
1439 return (SNMP_ERR_NOT_WRITEABLE);
1440 return (SNMP_ERR_NO_CREATION);
1442 case SNMP_OP_COMMIT:
1443 case SNMP_OP_ROLLBACK:
1449 * Come here for GET and COMMIT
1453 case LEAF_ntpFilterPeersOffset:
1454 return (string_get(value, t->offset, -1));
1456 case LEAF_ntpFilterPeersDelay:
1457 return (string_get(value, t->delay, -1));
1459 case LEAF_ntpFilterPeersDispersion:
1460 return (string_get(value, t->dispersion, -1));
1465 return (SNMP_ERR_NOERROR);
1469 * System variables - read-only scalars only.
1472 op_begemot_ntp(struct snmp_context *ctx __unused, struct snmp_value *value,
1473 u_int sub, u_int iidx __unused, enum snmp_op op)
1475 asn_subid_t which = value->var.subs[sub - 1];
1480 case SNMP_OP_GETNEXT:
1486 case LEAF_begemotNtpHost:
1487 return (string_get(value, ntp_host, -1));
1489 case LEAF_begemotNtpPort:
1490 return (string_get(value, ntp_port, -1));
1492 case LEAF_begemotNtpTimeout:
1493 value->v.uint32 = ntp_timeout;
1494 return (SNMP_ERR_NOERROR);
1496 case LEAF_begemotNtpDebug:
1497 value->v.uint32 = ntp_debug;
1498 return (SNMP_ERR_NOERROR);
1500 case LEAF_begemotNtpJitter:
1501 if (this_tick > sysinfo_tick) {
1502 if (fetch_sysinfo() == -1)
1503 return (SNMP_ERR_GENERR);
1504 sysinfo_tick = this_tick;
1507 return (SNMP_ERR_NOSUCHNAME);
1508 value->v.counter64 = sys_jitter / 1000 * (1ULL << 32);
1509 return (SNMP_ERR_NOERROR);
1511 case LEAF_begemotNtpStability:
1512 if (this_tick > sysinfo_tick) {
1513 if (fetch_sysinfo() == -1)
1514 return (SNMP_ERR_GENERR);
1515 sysinfo_tick = this_tick;
1517 if (!sysb_stability)
1518 return (SNMP_ERR_NOSUCHNAME);
1519 value->v.counter64 = sys_stability * (1ULL << 32);
1520 return (SNMP_ERR_NOERROR);
1527 case LEAF_begemotNtpHost:
1528 /* only at initialization */
1529 if (community != COMM_INITIALIZE)
1530 return (SNMP_ERR_NOT_WRITEABLE);
1532 if ((ret = string_save(value, ctx, -1, &ntp_host))
1533 != SNMP_ERR_NOERROR)
1535 return (SNMP_ERR_NOERROR);
1537 case LEAF_begemotNtpPort:
1538 /* only at initialization */
1539 if (community != COMM_INITIALIZE)
1540 return (SNMP_ERR_NOT_WRITEABLE);
1542 if ((ret = string_save(value, ctx, -1, &ntp_port))
1543 != SNMP_ERR_NOERROR)
1545 return (SNMP_ERR_NOERROR);
1547 case LEAF_begemotNtpTimeout:
1548 ctx->scratch->int1 = ntp_timeout;
1549 if (value->v.uint32 < 1)
1550 return (SNMP_ERR_WRONG_VALUE);
1551 ntp_timeout = value->v.integer;
1552 return (SNMP_ERR_NOERROR);
1554 case LEAF_begemotNtpDebug:
1555 ctx->scratch->int1 = ntp_debug;
1556 ntp_debug = value->v.integer;
1557 return (SNMP_ERR_NOERROR);
1561 case SNMP_OP_ROLLBACK:
1564 case LEAF_begemotNtpHost:
1565 string_rollback(ctx, &ntp_host);
1566 return (SNMP_ERR_NOERROR);
1568 case LEAF_begemotNtpPort:
1569 string_rollback(ctx, &ntp_port);
1570 return (SNMP_ERR_NOERROR);
1572 case LEAF_begemotNtpTimeout:
1573 ntp_timeout = ctx->scratch->int1;
1574 return (SNMP_ERR_NOERROR);
1576 case LEAF_begemotNtpDebug:
1577 ntp_debug = ctx->scratch->int1;
1578 return (SNMP_ERR_NOERROR);
1582 case SNMP_OP_COMMIT:
1585 case LEAF_begemotNtpHost:
1587 return (SNMP_ERR_NOERROR);
1589 case LEAF_begemotNtpPort:
1591 return (SNMP_ERR_NOERROR);
1593 case LEAF_begemotNtpTimeout:
1594 case LEAF_begemotNtpDebug:
1595 return (SNMP_ERR_NOERROR);