2 * ntp_request.c - respond to information requests
11 #include "ntp_request.h"
12 #include "ntp_control.h"
13 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
16 #include "ntp_assert.h"
21 #ifdef HAVE_NETINET_IN_H
22 #include <netinet/in.h>
24 #include <arpa/inet.h>
29 #include "ntp_syscall.h"
30 #endif /* KERNEL_PLL */
33 * Structure to hold request procedure information
38 #define NO_REQUEST (-1)
40 * Because we now have v6 addresses in the messages, we need to compensate
41 * for the larger size. Therefore, we introduce the alternate size to
42 * keep us friendly with older implementations. A little ugly.
44 static int client_v6_capable = 0; /* the client can handle longer messages */
46 #define v6sizeof(type) (client_v6_capable ? sizeof(type) : v4sizeof(type))
49 short request_code; /* defined request code */
50 short needs_auth; /* true when authentication needed */
51 short sizeofitem; /* size of request data item (older size)*/
52 short v6_sizeofitem; /* size of request data item (new size)*/
53 void (*handler) (sockaddr_u *, endpt *,
54 struct req_pkt *); /* routine to handle request */
58 * Universal request codes
60 static const struct req_proc univ_codes[] = {
61 { NO_REQUEST, NOAUTH, 0, 0, NULL }
64 static void req_ack (sockaddr_u *, endpt *, struct req_pkt *, int);
65 static void * prepare_pkt (sockaddr_u *, endpt *,
66 struct req_pkt *, size_t);
67 static void * more_pkt (void);
68 static void flush_pkt (void);
69 static void list_peers (sockaddr_u *, endpt *, struct req_pkt *);
70 static void list_peers_sum (sockaddr_u *, endpt *, struct req_pkt *);
71 static void peer_info (sockaddr_u *, endpt *, struct req_pkt *);
72 static void peer_stats (sockaddr_u *, endpt *, struct req_pkt *);
73 static void sys_info (sockaddr_u *, endpt *, struct req_pkt *);
74 static void sys_stats (sockaddr_u *, endpt *, struct req_pkt *);
75 static void mem_stats (sockaddr_u *, endpt *, struct req_pkt *);
76 static void io_stats (sockaddr_u *, endpt *, struct req_pkt *);
77 static void timer_stats (sockaddr_u *, endpt *, struct req_pkt *);
78 static void loop_info (sockaddr_u *, endpt *, struct req_pkt *);
79 static void do_conf (sockaddr_u *, endpt *, struct req_pkt *);
80 static void do_unconf (sockaddr_u *, endpt *, struct req_pkt *);
81 static void set_sys_flag (sockaddr_u *, endpt *, struct req_pkt *);
82 static void clr_sys_flag (sockaddr_u *, endpt *, struct req_pkt *);
83 static void setclr_flags (sockaddr_u *, endpt *, struct req_pkt *, u_long);
84 static void list_restrict4 (const restrict_u *, struct info_restrict **);
85 static void list_restrict6 (const restrict_u *, struct info_restrict **);
86 static void list_restrict (sockaddr_u *, endpt *, struct req_pkt *);
87 static void do_resaddflags (sockaddr_u *, endpt *, struct req_pkt *);
88 static void do_ressubflags (sockaddr_u *, endpt *, struct req_pkt *);
89 static void do_unrestrict (sockaddr_u *, endpt *, struct req_pkt *);
90 static void do_restrict (sockaddr_u *, endpt *, struct req_pkt *, int);
91 static void mon_getlist (sockaddr_u *, endpt *, struct req_pkt *);
92 static void reset_stats (sockaddr_u *, endpt *, struct req_pkt *);
93 static void reset_peer (sockaddr_u *, endpt *, struct req_pkt *);
94 static void do_key_reread (sockaddr_u *, endpt *, struct req_pkt *);
95 static void trust_key (sockaddr_u *, endpt *, struct req_pkt *);
96 static void untrust_key (sockaddr_u *, endpt *, struct req_pkt *);
97 static void do_trustkey (sockaddr_u *, endpt *, struct req_pkt *, u_long);
98 static void get_auth_info (sockaddr_u *, endpt *, struct req_pkt *);
99 static void req_get_traps (sockaddr_u *, endpt *, struct req_pkt *);
100 static void req_set_trap (sockaddr_u *, endpt *, struct req_pkt *);
101 static void req_clr_trap (sockaddr_u *, endpt *, struct req_pkt *);
102 static void do_setclr_trap (sockaddr_u *, endpt *, struct req_pkt *, int);
103 static void set_request_keyid (sockaddr_u *, endpt *, struct req_pkt *);
104 static void set_control_keyid (sockaddr_u *, endpt *, struct req_pkt *);
105 static void get_ctl_stats (sockaddr_u *, endpt *, struct req_pkt *);
106 static void get_if_stats (sockaddr_u *, endpt *, struct req_pkt *);
107 static void do_if_reload (sockaddr_u *, endpt *, struct req_pkt *);
109 static void get_kernel_info (sockaddr_u *, endpt *, struct req_pkt *);
110 #endif /* KERNEL_PLL */
112 static void get_clock_info (sockaddr_u *, endpt *, struct req_pkt *);
113 static void set_clock_fudge (sockaddr_u *, endpt *, struct req_pkt *);
114 #endif /* REFCLOCK */
116 static void get_clkbug_info (sockaddr_u *, endpt *, struct req_pkt *);
117 #endif /* REFCLOCK */
122 static const struct req_proc ntp_codes[] = {
123 { REQ_PEER_LIST, NOAUTH, 0, 0, list_peers },
124 { REQ_PEER_LIST_SUM, NOAUTH, 0, 0, list_peers_sum },
125 { REQ_PEER_INFO, NOAUTH, v4sizeof(struct info_peer_list),
126 sizeof(struct info_peer_list), peer_info},
127 { REQ_PEER_STATS, NOAUTH, v4sizeof(struct info_peer_list),
128 sizeof(struct info_peer_list), peer_stats},
129 { REQ_SYS_INFO, NOAUTH, 0, 0, sys_info },
130 { REQ_SYS_STATS, NOAUTH, 0, 0, sys_stats },
131 { REQ_IO_STATS, NOAUTH, 0, 0, io_stats },
132 { REQ_MEM_STATS, NOAUTH, 0, 0, mem_stats },
133 { REQ_LOOP_INFO, NOAUTH, 0, 0, loop_info },
134 { REQ_TIMER_STATS, NOAUTH, 0, 0, timer_stats },
135 { REQ_CONFIG, AUTH, v4sizeof(struct conf_peer),
136 sizeof(struct conf_peer), do_conf },
137 { REQ_UNCONFIG, AUTH, v4sizeof(struct conf_unpeer),
138 sizeof(struct conf_unpeer), do_unconf },
139 { REQ_SET_SYS_FLAG, AUTH, sizeof(struct conf_sys_flags),
140 sizeof(struct conf_sys_flags), set_sys_flag },
141 { REQ_CLR_SYS_FLAG, AUTH, sizeof(struct conf_sys_flags),
142 sizeof(struct conf_sys_flags), clr_sys_flag },
143 { REQ_GET_RESTRICT, NOAUTH, 0, 0, list_restrict },
144 { REQ_RESADDFLAGS, AUTH, v4sizeof(struct conf_restrict),
145 sizeof(struct conf_restrict), do_resaddflags },
146 { REQ_RESSUBFLAGS, AUTH, v4sizeof(struct conf_restrict),
147 sizeof(struct conf_restrict), do_ressubflags },
148 { REQ_UNRESTRICT, AUTH, v4sizeof(struct conf_restrict),
149 sizeof(struct conf_restrict), do_unrestrict },
150 { REQ_MON_GETLIST, NOAUTH, 0, 0, mon_getlist },
151 { REQ_MON_GETLIST_1, NOAUTH, 0, 0, mon_getlist },
152 { REQ_RESET_STATS, AUTH, sizeof(struct reset_flags), 0, reset_stats },
153 { REQ_RESET_PEER, AUTH, v4sizeof(struct conf_unpeer),
154 sizeof(struct conf_unpeer), reset_peer },
155 { REQ_REREAD_KEYS, AUTH, 0, 0, do_key_reread },
156 { REQ_TRUSTKEY, AUTH, sizeof(u_long), sizeof(u_long), trust_key },
157 { REQ_UNTRUSTKEY, AUTH, sizeof(u_long), sizeof(u_long), untrust_key },
158 { REQ_AUTHINFO, NOAUTH, 0, 0, get_auth_info },
159 { REQ_TRAPS, NOAUTH, 0, 0, req_get_traps },
160 { REQ_ADD_TRAP, AUTH, v4sizeof(struct conf_trap),
161 sizeof(struct conf_trap), req_set_trap },
162 { REQ_CLR_TRAP, AUTH, v4sizeof(struct conf_trap),
163 sizeof(struct conf_trap), req_clr_trap },
164 { REQ_REQUEST_KEY, AUTH, sizeof(u_long), sizeof(u_long),
166 { REQ_CONTROL_KEY, AUTH, sizeof(u_long), sizeof(u_long),
168 { REQ_GET_CTLSTATS, NOAUTH, 0, 0, get_ctl_stats },
170 { REQ_GET_KERNEL, NOAUTH, 0, 0, get_kernel_info },
173 { REQ_GET_CLOCKINFO, NOAUTH, sizeof(u_int32), sizeof(u_int32),
175 { REQ_SET_CLKFUDGE, AUTH, sizeof(struct conf_fudge),
176 sizeof(struct conf_fudge), set_clock_fudge },
177 { REQ_GET_CLKBUGINFO, NOAUTH, sizeof(u_int32), sizeof(u_int32),
180 { REQ_IF_STATS, AUTH, 0, 0, get_if_stats },
181 { REQ_IF_RELOAD, AUTH, 0, 0, do_if_reload },
183 { NO_REQUEST, NOAUTH, 0, 0, 0 }
188 * Authentication keyid used to authenticate requests. Zero means we
189 * don't allow writing anything.
191 keyid_t info_auth_keyid;
194 * Statistic counters to keep track of requests and responses.
196 u_long numrequests; /* number of requests we've received */
197 u_long numresppkts; /* number of resp packets sent with data */
200 * lazy way to count errors, indexed by the error code
202 u_long errorcounter[MAX_INFO_ERR + 1];
205 * A hack. To keep the authentication module clear of ntp-ism's, we
206 * include a time reset variable for its stats here.
208 u_long auth_timereset;
211 * Response packet used by these routines. Also some state information
212 * so that we can handle packet formatting within a common set of
213 * subroutines. Note we try to enter data in place whenever possible,
214 * but the need to set the more bit correctly means we occasionally
215 * use the extra buffer and copy.
217 static struct resp_pkt rpkt;
222 static int databytes;
223 static char exbuf[RESP_DATA_SIZE];
224 static int usingexbuf;
225 static sockaddr_u *toaddr;
226 static endpt *frominter;
229 * init_request - initialize request data
239 info_auth_keyid = 0; /* by default, can't do this */
241 for (i = 0; i < sizeof(errorcounter)/sizeof(errorcounter[0]); i++)
247 * req_ack - acknowledge request with no data
253 struct req_pkt *inpkt,
260 rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, 0, reqver);
261 rpkt.auth_seq = AUTH_SEQ(0, 0);
262 rpkt.implementation = inpkt->implementation;
263 rpkt.request = inpkt->request;
264 rpkt.err_nitems = ERR_NITEMS(errcode, 0);
265 rpkt.mbz_itemsize = MBZ_ITEMSIZE(0);
268 * send packet and bump counters
270 sendpkt(srcadr, inter, -1, (struct pkt *)&rpkt, RESP_HEADER_SIZE);
271 errorcounter[errcode]++;
276 * prepare_pkt - prepare response packet for transmission, return pointer
277 * to storage for data item.
287 DPRINTF(4, ("request: preparing pkt\n"));
290 * Fill in the implementation, request and itemsize fields
291 * since these won't change.
293 rpkt.implementation = pkt->implementation;
294 rpkt.request = pkt->request;
295 rpkt.mbz_itemsize = MBZ_ITEMSIZE(structsize);
298 * Compute the static data needed to carry on.
304 itemsize = structsize;
309 * return the beginning of the packet buffer.
316 * more_pkt - return a data pointer for a new item.
322 * If we were using the extra buffer, send the packet.
325 DPRINTF(3, ("request: sending pkt\n"));
326 rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, MORE_BIT, reqver);
327 rpkt.auth_seq = AUTH_SEQ(0, seqno);
328 rpkt.err_nitems = htons((u_short)nitems);
329 sendpkt(toaddr, frominter, -1, (struct pkt *)&rpkt,
330 RESP_HEADER_SIZE + databytes);
334 * Copy data out of exbuf into the packet.
336 memcpy(&rpkt.u.data[0], exbuf, (unsigned)itemsize);
343 databytes += itemsize;
345 if (databytes + itemsize <= RESP_DATA_SIZE) {
346 DPRINTF(4, ("request: giving him more data\n"));
348 * More room in packet. Give him the
351 return &rpkt.u.data[databytes];
354 * No room in packet. Give him the extra
355 * buffer unless this was the last in the sequence.
357 DPRINTF(4, ("request: into extra buffer\n"));
369 * flush_pkt - we're done, return remaining information.
374 DPRINTF(3, ("request: flushing packet, %d items\n", nitems));
376 * Must send the last packet. If nothing in here and nothing
377 * has been sent, send an error saying no data to be found.
379 if (seqno == 0 && nitems == 0)
380 req_ack(toaddr, frominter, (struct req_pkt *)&rpkt,
383 rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, 0, reqver);
384 rpkt.auth_seq = AUTH_SEQ(0, seqno);
385 rpkt.err_nitems = htons((u_short)nitems);
386 sendpkt(toaddr, frominter, -1, (struct pkt *)&rpkt,
387 RESP_HEADER_SIZE+databytes);
395 * Given a buffer, return the packet mode
398 get_packet_mode(struct recvbuf *rbufp)
400 struct req_pkt *inpkt = (struct req_pkt *)&rbufp->recv_pkt;
401 return (INFO_MODE(inpkt->rm_vn_mode));
406 * process_private - process private mode (7) packets
410 struct recvbuf *rbufp,
414 static u_long quiet_until;
415 struct req_pkt *inpkt;
416 struct req_pkt_tail *tailinpkt;
419 const struct req_proc *proc;
429 * Initialize pointers, for convenience
431 recv_len = rbufp->recv_length;
432 inpkt = (struct req_pkt *)&rbufp->recv_pkt;
433 srcadr = &rbufp->recv_srcadr;
434 inter = rbufp->dstadr;
436 DPRINTF(3, ("process_private: impl %d req %d\n",
437 inpkt->implementation, inpkt->request));
440 * Do some sanity checks on the packet. Return a format
444 if ( (++ec, ISRESPONSE(inpkt->rm_vn_mode))
445 || (++ec, ISMORE(inpkt->rm_vn_mode))
446 || (++ec, INFO_VERSION(inpkt->rm_vn_mode) > NTP_VERSION)
447 || (++ec, INFO_VERSION(inpkt->rm_vn_mode) < NTP_OLDVERSION)
448 || (++ec, INFO_SEQ(inpkt->auth_seq) != 0)
449 || (++ec, INFO_ERR(inpkt->err_nitems) != 0)
450 || (++ec, INFO_MBZ(inpkt->mbz_itemsize) != 0)
451 || (++ec, rbufp->recv_length < (int)REQ_LEN_HDR)
454 if (current_time >= quiet_until) {
456 "process_private: drop test %d"
457 " failed, pkt from %s",
459 quiet_until = current_time + 60;
464 reqver = INFO_VERSION(inpkt->rm_vn_mode);
467 * Get the appropriate procedure list to search.
469 if (inpkt->implementation == IMPL_UNIV)
471 else if ((inpkt->implementation == IMPL_XNTPD) ||
472 (inpkt->implementation == IMPL_XNTPD_OLD))
475 req_ack(srcadr, inter, inpkt, INFO_ERR_IMPL);
480 * Search the list for the request codes. If it isn't one
481 * we know, return an error.
483 while (proc->request_code != NO_REQUEST) {
484 if (proc->request_code == (short) inpkt->request)
488 if (proc->request_code == NO_REQUEST) {
489 req_ack(srcadr, inter, inpkt, INFO_ERR_REQ);
493 DPRINTF(4, ("found request in tables\n"));
496 * If we need data, check to see if we have some. If we
497 * don't, check to see that there is none (picky, picky).
500 /* This part is a bit tricky, we want to be sure that the size
501 * returned is either the old or the new size. We also can find
502 * out if the client can accept both types of messages this way.
504 * Handle the exception of REQ_CONFIG. It can have two data sizes.
506 temp_size = INFO_ITEMSIZE(inpkt->mbz_itemsize);
507 if ((temp_size != proc->sizeofitem &&
508 temp_size != proc->v6_sizeofitem) &&
509 !(inpkt->implementation == IMPL_XNTPD &&
510 inpkt->request == REQ_CONFIG &&
511 temp_size == sizeof(struct old_conf_peer))) {
512 DPRINTF(3, ("process_private: wrong item size, received %d, should be %d or %d\n",
513 temp_size, proc->sizeofitem, proc->v6_sizeofitem));
514 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
517 if ((proc->sizeofitem != 0) &&
518 ((size_t)(temp_size * INFO_NITEMS(inpkt->err_nitems)) >
519 (recv_len - REQ_LEN_HDR))) {
520 DPRINTF(3, ("process_private: not enough data\n"));
521 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
525 switch (inpkt->implementation) {
527 client_v6_capable = 1;
530 client_v6_capable = 0;
533 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
538 * If we need to authenticate, do so. Note that an
539 * authenticatable packet must include a mac field, must
540 * have used key info_auth_keyid and must have included
541 * a time stamp in the appropriate field. The time stamp
542 * must be within INFO_TS_MAXSKEW of the receive
545 if (proc->needs_auth && sys_authenticate) {
547 if (recv_len < (REQ_LEN_HDR +
548 (INFO_ITEMSIZE(inpkt->mbz_itemsize) *
549 INFO_NITEMS(inpkt->err_nitems)) +
551 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
556 * For 16-octet digests, regardless of itemsize and
557 * nitems, authenticated requests are a fixed size
558 * with the timestamp, key ID, and digest located
559 * at the end of the packet. Because the key ID
560 * determining the digest size precedes the digest,
561 * for larger digests the fixed size request scheme
562 * is abandoned and the timestamp, key ID, and digest
563 * are located relative to the start of the packet,
564 * with the digest size determined by the packet size.
566 noslop_len = REQ_LEN_HDR
567 + INFO_ITEMSIZE(inpkt->mbz_itemsize) *
568 INFO_NITEMS(inpkt->err_nitems)
569 + sizeof(inpkt->tstamp);
570 /* 32-bit alignment */
571 noslop_len = (noslop_len + 3) & ~3;
572 if (recv_len > (noslop_len + MAX_MAC_LEN))
575 mac_len = recv_len - noslop_len;
577 tailinpkt = (void *)((char *)inpkt + recv_len -
578 (mac_len + sizeof(inpkt->tstamp)));
581 * If this guy is restricted from doing this, don't let
582 * him. If the wrong key was used, or packet doesn't
585 if (!INFO_IS_AUTH(inpkt->auth_seq) || !info_auth_keyid
586 || ntohl(tailinpkt->keyid) != info_auth_keyid) {
587 DPRINTF(5, ("failed auth %d info_auth_keyid %u pkt keyid %u maclen %lu\n",
588 INFO_IS_AUTH(inpkt->auth_seq),
590 ntohl(tailinpkt->keyid), (u_long)mac_len));
593 "process_private: failed auth %d info_auth_keyid %u pkt keyid %u maclen %lu\n",
594 INFO_IS_AUTH(inpkt->auth_seq),
596 ntohl(tailinpkt->keyid), (u_long)mac_len);
598 req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH);
601 if (recv_len > REQ_LEN_NOMAC + MAX_MAC_LEN) {
602 DPRINTF(5, ("bad pkt length %zu\n", recv_len));
604 "process_private: bad pkt length %zu",
606 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
609 if (!mod_okay || !authhavekey(info_auth_keyid)) {
610 DPRINTF(5, ("failed auth mod_okay %d\n",
614 "process_private: failed auth mod_okay %d\n",
620 req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH);
625 * calculate absolute time difference between xmit time stamp
626 * and receive time stamp. If too large, too bad.
628 NTOHL_FP(&tailinpkt->tstamp, &ftmp);
629 L_SUB(&ftmp, &rbufp->recv_time);
630 LFPTOD(&ftmp, dtemp);
631 if (fabs(dtemp) > INFO_TS_MAXSKEW) {
633 * He's a loser. Tell him.
635 DPRINTF(5, ("xmit/rcv timestamp delta %g > INFO_TS_MAXSKEW %g\n",
636 dtemp, INFO_TS_MAXSKEW));
637 req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH);
642 * So far so good. See if decryption works out okay.
644 if (!authdecrypt(info_auth_keyid, (u_int32 *)inpkt,
645 recv_len - mac_len, mac_len)) {
646 DPRINTF(5, ("authdecrypt failed\n"));
647 req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH);
652 DPRINTF(3, ("process_private: all okay, into handler\n"));
654 * Packet is okay. Call the handler to send him data.
656 (proc->handler)(srcadr, inter, inpkt);
661 * list_peers - send a list of the peers
667 struct req_pkt *inpkt
670 struct info_peer_list * ip;
671 const struct peer * pp;
673 ip = (struct info_peer_list *)prepare_pkt(srcadr, inter, inpkt,
674 v6sizeof(struct info_peer_list));
675 for (pp = peer_list; pp != NULL && ip != NULL; pp = pp->p_link) {
676 if (IS_IPV6(&pp->srcadr)) {
677 if (!client_v6_capable)
679 ip->addr6 = SOCK_ADDR6(&pp->srcadr);
682 ip->addr = NSRCADR(&pp->srcadr);
683 if (client_v6_capable)
687 ip->port = NSRCPORT(&pp->srcadr);
688 ip->hmode = pp->hmode;
690 if (pp->flags & FLAG_CONFIG)
691 ip->flags |= INFO_FLAG_CONFIG;
693 ip->flags |= INFO_FLAG_SYSPEER;
694 if (pp->status == CTL_PST_SEL_SYNCCAND)
695 ip->flags |= INFO_FLAG_SEL_CANDIDATE;
696 if (pp->status >= CTL_PST_SEL_SYSPEER)
697 ip->flags |= INFO_FLAG_SHORTLIST;
698 ip = (struct info_peer_list *)more_pkt();
706 * list_peers_sum - return extended peer list
712 struct req_pkt *inpkt
715 struct info_peer_summary * ips;
716 const struct peer * pp;
719 DPRINTF(3, ("wants peer list summary\n"));
721 ips = (struct info_peer_summary *)prepare_pkt(srcadr, inter, inpkt,
722 v6sizeof(struct info_peer_summary));
723 for (pp = peer_list; pp != NULL && ips != NULL; pp = pp->p_link) {
724 DPRINTF(4, ("sum: got one\n"));
726 * Be careful here not to return v6 peers when we
729 if (IS_IPV6(&pp->srcadr)) {
730 if (!client_v6_capable)
732 ips->srcadr6 = SOCK_ADDR6(&pp->srcadr);
735 ips->dstadr6 = SOCK_ADDR6(&pp->dstadr->sin);
739 ips->srcadr = NSRCADR(&pp->srcadr);
740 if (client_v6_capable)
745 ips->dstadr = NSRCADR(&pp->dstadr->sin);
747 if (MDF_BCAST == pp->cast_flags)
748 ips->dstadr = NSRCADR(&pp->dstadr->bcast);
749 else if (pp->cast_flags) {
750 ips->dstadr = NSRCADR(&pp->dstadr->sin);
752 ips->dstadr = NSRCADR(&pp->dstadr->bcast);
760 ips->srcport = NSRCPORT(&pp->srcadr);
761 ips->stratum = pp->stratum;
762 ips->hpoll = pp->hpoll;
763 ips->ppoll = pp->ppoll;
764 ips->reach = pp->reach;
767 ips->flags |= INFO_FLAG_SYSPEER;
768 if (pp->flags & FLAG_CONFIG)
769 ips->flags |= INFO_FLAG_CONFIG;
770 if (pp->flags & FLAG_REFCLOCK)
771 ips->flags |= INFO_FLAG_REFCLOCK;
772 if (pp->flags & FLAG_PREFER)
773 ips->flags |= INFO_FLAG_PREFER;
774 if (pp->flags & FLAG_BURST)
775 ips->flags |= INFO_FLAG_BURST;
776 if (pp->status == CTL_PST_SEL_SYNCCAND)
777 ips->flags |= INFO_FLAG_SEL_CANDIDATE;
778 if (pp->status >= CTL_PST_SEL_SYSPEER)
779 ips->flags |= INFO_FLAG_SHORTLIST;
780 ips->hmode = pp->hmode;
781 ips->delay = HTONS_FP(DTOFP(pp->delay));
782 DTOLFP(pp->offset, <mp);
783 HTONL_FP(<mp, &ips->offset);
784 ips->dispersion = HTONS_FP(DTOUFP(SQRT(pp->disp)));
786 ips = (struct info_peer_summary *)more_pkt();
794 * peer_info - send information for one or more peers
800 struct req_pkt *inpkt
806 struct info_peer_list ipl;
808 struct info_peer * ip;
814 items = INFO_NITEMS(inpkt->err_nitems);
815 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
816 datap = inpkt->u.data;
817 if (item_sz != sizeof(ipl)) {
818 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
821 ip = prepare_pkt(srcadr, inter, inpkt,
822 v6sizeof(struct info_peer));
823 while (items-- > 0 && ip != NULL) {
825 memcpy(&ipl, datap, item_sz);
827 NSRCPORT(&addr) = ipl.port;
828 if (client_v6_capable && ipl.v6_flag) {
829 AF(&addr) = AF_INET6;
830 SOCK_ADDR6(&addr) = ipl.addr6;
833 NSRCADR(&addr) = ipl.addr;
835 #ifdef ISC_PLATFORM_HAVESALEN
836 addr.sa.sa_len = SOCKLEN(&addr);
840 pp = findexistingpeer(&addr, NULL, NULL, -1, 0);
843 if (IS_IPV6(srcadr)) {
846 (MDF_BCAST == pp->cast_flags)
847 ? SOCK_ADDR6(&pp->dstadr->bcast)
848 : SOCK_ADDR6(&pp->dstadr->sin);
852 ip->srcadr6 = SOCK_ADDR6(&pp->srcadr);
857 ip->dstadr = NSRCADR(&pp->dstadr->sin);
859 if (MDF_BCAST == pp->cast_flags)
860 ip->dstadr = NSRCADR(&pp->dstadr->bcast);
861 else if (pp->cast_flags) {
862 ip->dstadr = NSRCADR(&pp->dstadr->sin);
864 ip->dstadr = NSRCADR(&pp->dstadr->bcast);
870 ip->srcadr = NSRCADR(&pp->srcadr);
871 if (client_v6_capable)
874 ip->srcport = NSRCPORT(&pp->srcadr);
877 ip->flags |= INFO_FLAG_SYSPEER;
878 if (pp->flags & FLAG_CONFIG)
879 ip->flags |= INFO_FLAG_CONFIG;
880 if (pp->flags & FLAG_REFCLOCK)
881 ip->flags |= INFO_FLAG_REFCLOCK;
882 if (pp->flags & FLAG_PREFER)
883 ip->flags |= INFO_FLAG_PREFER;
884 if (pp->flags & FLAG_BURST)
885 ip->flags |= INFO_FLAG_BURST;
886 if (pp->status == CTL_PST_SEL_SYNCCAND)
887 ip->flags |= INFO_FLAG_SEL_CANDIDATE;
888 if (pp->status >= CTL_PST_SEL_SYSPEER)
889 ip->flags |= INFO_FLAG_SHORTLIST;
891 ip->hmode = pp->hmode;
892 ip->keyid = pp->keyid;
893 ip->stratum = pp->stratum;
894 ip->ppoll = pp->ppoll;
895 ip->hpoll = pp->hpoll;
896 ip->precision = pp->precision;
897 ip->version = pp->version;
898 ip->reach = pp->reach;
899 ip->unreach = (u_char)pp->unreach;
900 ip->flash = (u_char)pp->flash;
901 ip->flash2 = (u_short)pp->flash;
902 ip->estbdelay = HTONS_FP(DTOFP(pp->delay));
903 ip->ttl = (u_char)pp->ttl;
904 ip->associd = htons(pp->associd);
905 ip->rootdelay = HTONS_FP(DTOUFP(pp->rootdelay));
906 ip->rootdispersion = HTONS_FP(DTOUFP(pp->rootdisp));
907 ip->refid = pp->refid;
908 HTONL_FP(&pp->reftime, &ip->reftime);
909 HTONL_FP(&pp->aorg, &ip->org);
910 HTONL_FP(&pp->rec, &ip->rec);
911 HTONL_FP(&pp->xmt, &ip->xmt);
912 j = pp->filter_nextpt - 1;
913 for (i = 0; i < NTP_SHIFT; i++, j--) {
916 ip->filtdelay[i] = HTONS_FP(DTOFP(pp->filter_delay[j]));
917 DTOLFP(pp->filter_offset[j], <mp);
918 HTONL_FP(<mp, &ip->filtoffset[i]);
919 ip->order[i] = (u_char)((pp->filter_nextpt +
921 pp->filter_order[i]);
922 if (ip->order[i] >= NTP_SHIFT)
923 ip->order[i] -= NTP_SHIFT;
925 DTOLFP(pp->offset, <mp);
926 HTONL_FP(<mp, &ip->offset);
927 ip->delay = HTONS_FP(DTOFP(pp->delay));
928 ip->dispersion = HTONS_FP(DTOUFP(SQRT(pp->disp)));
929 ip->selectdisp = HTONS_FP(DTOUFP(SQRT(pp->jitter)));
937 * peer_stats - send statistics for one or more peers
943 struct req_pkt *inpkt
949 struct info_peer_list ipl;
951 struct info_peer_stats *ip;
954 DPRINTF(1, ("peer_stats: called\n"));
955 items = INFO_NITEMS(inpkt->err_nitems);
956 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
957 datap = inpkt->u.data;
958 if (item_sz > sizeof(ipl)) {
959 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
962 ip = prepare_pkt(srcadr, inter, inpkt,
963 v6sizeof(struct info_peer_stats));
964 while (items-- > 0 && ip != NULL) {
966 memcpy(&ipl, datap, item_sz);
968 NSRCPORT(&addr) = ipl.port;
969 if (client_v6_capable && ipl.v6_flag) {
970 AF(&addr) = AF_INET6;
971 SOCK_ADDR6(&addr) = ipl.addr6;
974 NSRCADR(&addr) = ipl.addr;
976 #ifdef ISC_PLATFORM_HAVESALEN
977 addr.sa.sa_len = SOCKLEN(&addr);
979 DPRINTF(1, ("peer_stats: looking for %s, %d, %d\n",
980 stoa(&addr), ipl.port, NSRCPORT(&addr)));
984 pp = findexistingpeer(&addr, NULL, NULL, -1, 0);
988 DPRINTF(1, ("peer_stats: found %s\n", stoa(&addr)));
990 if (IS_IPV4(&pp->srcadr)) {
993 ip->dstadr = NSRCADR(&pp->dstadr->sin);
995 if (MDF_BCAST == pp->cast_flags)
996 ip->dstadr = NSRCADR(&pp->dstadr->bcast);
997 else if (pp->cast_flags) {
998 ip->dstadr = NSRCADR(&pp->dstadr->sin);
1000 ip->dstadr = NSRCADR(&pp->dstadr->bcast);
1006 ip->srcadr = NSRCADR(&pp->srcadr);
1007 if (client_v6_capable)
1012 (MDF_BCAST == pp->cast_flags)
1013 ? SOCK_ADDR6(&pp->dstadr->bcast)
1014 : SOCK_ADDR6(&pp->dstadr->sin);
1018 ip->srcadr6 = SOCK_ADDR6(&pp->srcadr);
1021 ip->srcport = NSRCPORT(&pp->srcadr);
1024 ip->flags |= INFO_FLAG_SYSPEER;
1025 if (pp->flags & FLAG_CONFIG)
1026 ip->flags |= INFO_FLAG_CONFIG;
1027 if (pp->flags & FLAG_REFCLOCK)
1028 ip->flags |= INFO_FLAG_REFCLOCK;
1029 if (pp->flags & FLAG_PREFER)
1030 ip->flags |= INFO_FLAG_PREFER;
1031 if (pp->flags & FLAG_BURST)
1032 ip->flags |= INFO_FLAG_BURST;
1033 if (pp->flags & FLAG_IBURST)
1034 ip->flags |= INFO_FLAG_IBURST;
1035 if (pp->status == CTL_PST_SEL_SYNCCAND)
1036 ip->flags |= INFO_FLAG_SEL_CANDIDATE;
1037 if (pp->status >= CTL_PST_SEL_SYSPEER)
1038 ip->flags |= INFO_FLAG_SHORTLIST;
1039 ip->flags = htons(ip->flags);
1040 ip->timereceived = htonl((u_int32)(current_time - pp->timereceived));
1041 ip->timetosend = htonl(pp->nextdate - current_time);
1042 ip->timereachable = htonl((u_int32)(current_time - pp->timereachable));
1043 ip->sent = htonl((u_int32)(pp->sent));
1044 ip->processed = htonl((u_int32)(pp->processed));
1045 ip->badauth = htonl((u_int32)(pp->badauth));
1046 ip->bogusorg = htonl((u_int32)(pp->bogusorg));
1047 ip->oldpkt = htonl((u_int32)(pp->oldpkt));
1048 ip->seldisp = htonl((u_int32)(pp->seldisptoolarge));
1049 ip->selbroken = htonl((u_int32)(pp->selbroken));
1050 ip->candidate = pp->status;
1051 ip = (struct info_peer_stats *)more_pkt();
1058 * sys_info - return system info
1064 struct req_pkt *inpkt
1067 register struct info_sys *is;
1069 is = (struct info_sys *)prepare_pkt(srcadr, inter, inpkt,
1070 v6sizeof(struct info_sys));
1073 if (IS_IPV4(&sys_peer->srcadr)) {
1074 is->peer = NSRCADR(&sys_peer->srcadr);
1075 if (client_v6_capable)
1077 } else if (client_v6_capable) {
1078 is->peer6 = SOCK_ADDR6(&sys_peer->srcadr);
1081 is->peer_mode = sys_peer->hmode;
1084 if (client_v6_capable) {
1090 is->leap = sys_leap;
1091 is->stratum = sys_stratum;
1092 is->precision = sys_precision;
1093 is->rootdelay = htonl(DTOFP(sys_rootdelay));
1094 is->rootdispersion = htonl(DTOUFP(sys_rootdisp));
1095 is->frequency = htonl(DTOFP(sys_jitter));
1096 is->stability = htonl(DTOUFP(clock_stability * 1e6));
1097 is->refid = sys_refid;
1098 HTONL_FP(&sys_reftime, &is->reftime);
1100 is->poll = sys_poll;
1103 if (sys_authenticate)
1104 is->flags |= INFO_FLAG_AUTHENTICATE;
1106 is->flags |= INFO_FLAG_BCLIENT;
1109 is->flags |= INFO_FLAG_CAL;
1110 #endif /* REFCLOCK */
1112 is->flags |= INFO_FLAG_KERNEL;
1113 if (mon_enabled != MON_OFF)
1114 is->flags |= INFO_FLAG_MONITOR;
1116 is->flags |= INFO_FLAG_NTP;
1118 is->flags |= INFO_FLAG_PPS_SYNC;
1120 is->flags |= INFO_FLAG_FILEGEN;
1121 is->bdelay = HTONS_FP(DTOFP(sys_bdelay));
1122 HTONL_UF(sys_authdelay.l_uf, &is->authdelay);
1129 * sys_stats - return system statistics
1135 struct req_pkt *inpkt
1138 register struct info_sys_stats *ss;
1140 ss = (struct info_sys_stats *)prepare_pkt(srcadr, inter, inpkt,
1141 sizeof(struct info_sys_stats));
1142 ss->timeup = htonl((u_int32)current_time);
1143 ss->timereset = htonl((u_int32)(current_time - sys_stattime));
1144 ss->denied = htonl((u_int32)sys_restricted);
1145 ss->oldversionpkt = htonl((u_int32)sys_oldversion);
1146 ss->newversionpkt = htonl((u_int32)sys_newversion);
1147 ss->unknownversion = htonl((u_int32)sys_declined);
1148 ss->badlength = htonl((u_int32)sys_badlength);
1149 ss->processed = htonl((u_int32)sys_processed);
1150 ss->badauth = htonl((u_int32)sys_badauth);
1151 ss->limitrejected = htonl((u_int32)sys_limitrejected);
1152 ss->received = htonl((u_int32)sys_received);
1159 * mem_stats - return memory statistics
1165 struct req_pkt *inpkt
1168 register struct info_mem_stats *ms;
1171 ms = (struct info_mem_stats *)prepare_pkt(srcadr, inter, inpkt,
1172 sizeof(struct info_mem_stats));
1174 ms->timereset = htonl((u_int32)(current_time - peer_timereset));
1175 ms->totalpeermem = htons((u_short)total_peer_structs);
1176 ms->freepeermem = htons((u_short)peer_free_count);
1177 ms->findpeer_calls = htonl((u_int32)findpeer_calls);
1178 ms->allocations = htonl((u_int32)peer_allocations);
1179 ms->demobilizations = htonl((u_int32)peer_demobilizations);
1181 for (i = 0; i < NTP_HASH_SIZE; i++)
1182 ms->hashcount[i] = (u_char)
1183 max((u_int)peer_hash_count[i], UCHAR_MAX);
1191 * io_stats - return io statistics
1197 struct req_pkt *inpkt
1200 struct info_io_stats *io;
1202 io = (struct info_io_stats *)prepare_pkt(srcadr, inter, inpkt,
1203 sizeof(struct info_io_stats));
1205 io->timereset = htonl((u_int32)(current_time - io_timereset));
1206 io->totalrecvbufs = htons((u_short) total_recvbuffs());
1207 io->freerecvbufs = htons((u_short) free_recvbuffs());
1208 io->fullrecvbufs = htons((u_short) full_recvbuffs());
1209 io->lowwater = htons((u_short) lowater_additions());
1210 io->dropped = htonl((u_int32)packets_dropped);
1211 io->ignored = htonl((u_int32)packets_ignored);
1212 io->received = htonl((u_int32)packets_received);
1213 io->sent = htonl((u_int32)packets_sent);
1214 io->notsent = htonl((u_int32)packets_notsent);
1215 io->interrupts = htonl((u_int32)handler_calls);
1216 io->int_received = htonl((u_int32)handler_pkts);
1224 * timer_stats - return timer statistics
1228 sockaddr_u * srcadr,
1230 struct req_pkt * inpkt
1233 struct info_timer_stats * ts;
1236 ts = (struct info_timer_stats *)prepare_pkt(srcadr, inter,
1237 inpkt, sizeof(*ts));
1239 sincereset = current_time - timer_timereset;
1240 ts->timereset = htonl((u_int32)sincereset);
1241 ts->alarms = ts->timereset;
1242 ts->overflows = htonl((u_int32)alarm_overflow);
1243 ts->xmtcalls = htonl((u_int32)timer_xmtcalls);
1251 * loop_info - return the current state of the loop filter
1257 struct req_pkt *inpkt
1260 struct info_loop *li;
1263 li = (struct info_loop *)prepare_pkt(srcadr, inter, inpkt,
1264 sizeof(struct info_loop));
1266 DTOLFP(last_offset, <mp);
1267 HTONL_FP(<mp, &li->last_offset);
1268 DTOLFP(drift_comp * 1e6, <mp);
1269 HTONL_FP(<mp, &li->drift_comp);
1270 li->compliance = htonl((u_int32)(tc_counter));
1271 li->watchdog_timer = htonl((u_int32)(current_time - sys_epoch));
1279 * do_conf - add a peer to the configuration list
1285 struct req_pkt *inpkt
1292 struct conf_peer temp_cp;
1293 sockaddr_u peeraddr;
1296 * Do a check of everything to see that it looks
1297 * okay. If not, complain about it. Note we are
1300 items = INFO_NITEMS(inpkt->err_nitems);
1301 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
1302 datap = inpkt->u.data;
1303 if (item_sz > sizeof(temp_cp)) {
1304 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1308 while (items-- > 0) {
1310 memcpy(&temp_cp, datap, item_sz);
1311 ZERO_SOCK(&peeraddr);
1314 if (temp_cp.flags & CONF_FLAG_PREFER)
1316 if (temp_cp.flags & CONF_FLAG_BURST)
1318 if (temp_cp.flags & CONF_FLAG_IBURST)
1321 if (temp_cp.flags & CONF_FLAG_SKEY)
1323 #endif /* AUTOKEY */
1324 if (client_v6_capable && temp_cp.v6_flag) {
1325 AF(&peeraddr) = AF_INET6;
1326 SOCK_ADDR6(&peeraddr) = temp_cp.peeraddr6;
1328 AF(&peeraddr) = AF_INET;
1329 NSRCADR(&peeraddr) = temp_cp.peeraddr;
1331 * Make sure the address is valid
1333 if (!ISREFCLOCKADR(&peeraddr) &&
1334 ISBADADR(&peeraddr)) {
1335 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1340 NSRCPORT(&peeraddr) = htons(NTP_PORT);
1341 #ifdef ISC_PLATFORM_HAVESALEN
1342 peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
1345 /* check mode value: 0 <= hmode <= 6
1347 * There's no good global define for that limit, and
1348 * using a magic define is as good (or bad, actually) as
1349 * a magic number. So we use the highest possible peer
1350 * mode, and that is MODE_BCLIENT.
1352 * [Bug 3009] claims that a problem occurs for hmode > 7,
1353 * but the code in ntp_peer.c indicates trouble for any
1354 * hmode > 6 ( --> MODE_BCLIENT).
1356 if (temp_cp.hmode > MODE_BCLIENT) {
1357 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1361 /* Any more checks on the values? Unchecked at this
1367 * - minpoll/maxpoll, but they are treated properly
1368 * for all cases internally. Checking not necessary.
1371 /* finally create the peer */
1372 if (peer_config(&peeraddr, NULL, NULL,
1373 temp_cp.hmode, temp_cp.version, temp_cp.minpoll,
1374 temp_cp.maxpoll, fl, temp_cp.ttl, temp_cp.keyid,
1377 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
1383 req_ack(srcadr, inter, inpkt, INFO_OKAY);
1388 * do_unconf - remove a peer from the configuration list
1392 sockaddr_u * srcadr,
1394 struct req_pkt *inpkt
1400 struct conf_unpeer temp_cp;
1402 sockaddr_u peeraddr;
1406 * This is a bit unstructured, but I like to be careful.
1407 * We check to see that every peer exists and is actually
1408 * configured. If so, we remove them. If not, we return
1411 * [Bug 3011] Even if we checked all peers given in the request
1412 * in a dry run, there's still a chance that the caller played
1413 * unfair and gave the same peer multiple times. So we still
1414 * have to be prepared for nasty surprises in the second run ;)
1417 /* basic consistency checks */
1418 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
1419 if (item_sz > sizeof(temp_cp)) {
1420 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1424 /* now do two runs: first a dry run, then a busy one */
1425 for (loops = 0; loops != 2; ++loops) {
1426 items = INFO_NITEMS(inpkt->err_nitems);
1427 datap = inpkt->u.data;
1428 while (items-- > 0) {
1429 /* copy from request to local */
1431 memcpy(&temp_cp, datap, item_sz);
1432 /* get address structure */
1433 ZERO_SOCK(&peeraddr);
1434 if (client_v6_capable && temp_cp.v6_flag) {
1435 AF(&peeraddr) = AF_INET6;
1436 SOCK_ADDR6(&peeraddr) = temp_cp.peeraddr6;
1438 AF(&peeraddr) = AF_INET;
1439 NSRCADR(&peeraddr) = temp_cp.peeraddr;
1441 SET_PORT(&peeraddr, NTP_PORT);
1442 #ifdef ISC_PLATFORM_HAVESALEN
1443 peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
1445 DPRINTF(1, ("searching for %s\n",
1448 /* search for matching configred(!) peer */
1451 p = findexistingpeer(
1452 &peeraddr, NULL, p, -1, 0);
1453 } while (p && !(FLAG_CONFIG & p->flags));
1456 /* Item not found in dry run -- bail! */
1457 req_ack(srcadr, inter, inpkt,
1460 } else if (loops && p) {
1461 /* Item found in busy run -- remove! */
1462 peer_clear(p, "GONE");
1469 /* report success */
1470 req_ack(srcadr, inter, inpkt, INFO_OKAY);
1475 * set_sys_flag - set system flags
1481 struct req_pkt *inpkt
1484 setclr_flags(srcadr, inter, inpkt, 1);
1489 * clr_sys_flag - clear system flags
1495 struct req_pkt *inpkt
1498 setclr_flags(srcadr, inter, inpkt, 0);
1503 * setclr_flags - do the grunge work of flag setting/clearing
1509 struct req_pkt *inpkt,
1513 struct conf_sys_flags *sf;
1516 if (INFO_NITEMS(inpkt->err_nitems) > 1) {
1517 msyslog(LOG_ERR, "setclr_flags: err_nitems > 1");
1518 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1522 sf = (struct conf_sys_flags *)&inpkt->u;
1523 flags = ntohl(sf->flags);
1525 if (flags & ~(SYS_FLAG_BCLIENT | SYS_FLAG_PPS |
1526 SYS_FLAG_NTP | SYS_FLAG_KERNEL | SYS_FLAG_MONITOR |
1527 SYS_FLAG_FILEGEN | SYS_FLAG_AUTH | SYS_FLAG_CAL)) {
1528 msyslog(LOG_ERR, "setclr_flags: extra flags: %#x",
1529 flags & ~(SYS_FLAG_BCLIENT | SYS_FLAG_PPS |
1530 SYS_FLAG_NTP | SYS_FLAG_KERNEL |
1531 SYS_FLAG_MONITOR | SYS_FLAG_FILEGEN |
1532 SYS_FLAG_AUTH | SYS_FLAG_CAL));
1533 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1537 if (flags & SYS_FLAG_BCLIENT)
1538 proto_config(PROTO_BROADCLIENT, set, 0., NULL);
1539 if (flags & SYS_FLAG_PPS)
1540 proto_config(PROTO_PPS, set, 0., NULL);
1541 if (flags & SYS_FLAG_NTP)
1542 proto_config(PROTO_NTP, set, 0., NULL);
1543 if (flags & SYS_FLAG_KERNEL)
1544 proto_config(PROTO_KERNEL, set, 0., NULL);
1545 if (flags & SYS_FLAG_MONITOR)
1546 proto_config(PROTO_MONITOR, set, 0., NULL);
1547 if (flags & SYS_FLAG_FILEGEN)
1548 proto_config(PROTO_FILEGEN, set, 0., NULL);
1549 if (flags & SYS_FLAG_AUTH)
1550 proto_config(PROTO_AUTHENTICATE, set, 0., NULL);
1551 if (flags & SYS_FLAG_CAL)
1552 proto_config(PROTO_CAL, set, 0., NULL);
1553 req_ack(srcadr, inter, inpkt, INFO_OKAY);
1556 /* There have been some issues with the restrict list processing,
1557 * ranging from problems with deep recursion (resulting in stack
1558 * overflows) and overfull reply buffers.
1560 * To avoid this trouble the list reversal is done iteratively using a
1563 typedef struct RestrictStack RestrictStackT;
1564 struct RestrictStack {
1565 RestrictStackT *link;
1567 const restrict_u *pres[63];
1576 return sizeof(sp->pres)/sizeof(sp->pres[0]);
1582 RestrictStackT **spp,
1583 const restrict_u *ptr
1588 if (NULL == (sp = *spp) || 0 == sp->fcnt) {
1589 /* need another sheet in the scratch pad */
1590 sp = emalloc(sizeof(*sp));
1592 sp->fcnt = getStackSheetSize(sp);
1595 sp->pres[--sp->fcnt] = ptr;
1601 RestrictStackT **spp,
1602 const restrict_u **opp
1607 if (NULL == (sp = *spp) || sp->fcnt >= getStackSheetSize(sp))
1610 *opp = sp->pres[sp->fcnt++];
1611 if (sp->fcnt >= getStackSheetSize(sp)) {
1612 /* discard sheet from scratch pad */
1620 flushRestrictionStack(
1621 RestrictStackT **spp
1626 while (NULL != (sp = *spp)) {
1633 * list_restrict4 - iterative helper for list_restrict dumps IPv4
1634 * restriction list in reverse order.
1638 const restrict_u * res,
1639 struct info_restrict ** ppir
1642 RestrictStackT * rpad;
1643 struct info_restrict * pir;
1646 for (rpad = NULL; res; res = res->link)
1647 if (!pushRestriction(&rpad, res))
1650 while (pir && popRestriction(&rpad, &res)) {
1651 pir->addr = htonl(res->u.v4.addr);
1652 if (client_v6_capable)
1654 pir->mask = htonl(res->u.v4.mask);
1655 pir->count = htonl(res->count);
1656 pir->flags = htons(res->flags);
1657 pir->mflags = htons(res->mflags);
1658 pir = (struct info_restrict *)more_pkt();
1660 flushRestrictionStack(&rpad);
1665 * list_restrict6 - iterative helper for list_restrict dumps IPv6
1666 * restriction list in reverse order.
1670 const restrict_u * res,
1671 struct info_restrict ** ppir
1674 RestrictStackT * rpad;
1675 struct info_restrict * pir;
1678 for (rpad = NULL; res; res = res->link)
1679 if (!pushRestriction(&rpad, res))
1682 while (pir && popRestriction(&rpad, &res)) {
1683 pir->addr6 = res->u.v6.addr;
1684 pir->mask6 = res->u.v6.mask;
1686 pir->count = htonl(res->count);
1687 pir->flags = htons(res->flags);
1688 pir->mflags = htons(res->mflags);
1689 pir = (struct info_restrict *)more_pkt();
1691 flushRestrictionStack(&rpad);
1697 * list_restrict - return the restrict list
1703 struct req_pkt *inpkt
1706 struct info_restrict *ir;
1708 DPRINTF(3, ("wants restrict list summary\n"));
1710 ir = (struct info_restrict *)prepare_pkt(srcadr, inter, inpkt,
1711 v6sizeof(struct info_restrict));
1714 * The restriction lists are kept sorted in the reverse order
1715 * than they were originally. To preserve the output semantics,
1716 * dump each list in reverse order. The workers take care of that.
1718 list_restrict4(restrictlist4, &ir);
1719 if (client_v6_capable)
1720 list_restrict6(restrictlist6, &ir);
1726 * do_resaddflags - add flags to a restrict entry (or create one)
1732 struct req_pkt *inpkt
1735 do_restrict(srcadr, inter, inpkt, RESTRICT_FLAGS);
1741 * do_ressubflags - remove flags from a restrict entry
1747 struct req_pkt *inpkt
1750 do_restrict(srcadr, inter, inpkt, RESTRICT_UNFLAG);
1755 * do_unrestrict - remove a restrict entry from the list
1761 struct req_pkt *inpkt
1764 do_restrict(srcadr, inter, inpkt, RESTRICT_REMOVE);
1769 * do_restrict - do the dirty stuff of dealing with restrictions
1775 struct req_pkt *inpkt,
1780 struct conf_restrict cr;
1783 sockaddr_u matchaddr;
1784 sockaddr_u matchmask;
1788 * Do a check of the flags to make sure that only
1789 * the NTPPORT flag is set, if any. If not, complain
1790 * about it. Note we are very picky here.
1792 items = INFO_NITEMS(inpkt->err_nitems);
1793 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
1794 datap = inpkt->u.data;
1795 if (item_sz > sizeof(cr)) {
1796 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1801 while (items-- > 0 && !bad) {
1802 memcpy(&cr, datap, item_sz);
1803 cr.flags = ntohs(cr.flags);
1804 cr.mflags = ntohs(cr.mflags);
1805 if (~RESM_NTPONLY & cr.mflags)
1807 if (~RES_ALLFLAGS & cr.flags)
1809 if (INADDR_ANY != cr.mask) {
1810 if (client_v6_capable && cr.v6_flag) {
1811 if (IN6_IS_ADDR_UNSPECIFIED(&cr.addr6))
1814 if (INADDR_ANY == cr.addr)
1822 msyslog(LOG_ERR, "do_restrict: bad = %#x", bad);
1823 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1828 * Looks okay, try it out. Needs to reload data pointer and
1829 * item counter. (Talos-CAN-0052)
1831 ZERO_SOCK(&matchaddr);
1832 ZERO_SOCK(&matchmask);
1833 items = INFO_NITEMS(inpkt->err_nitems);
1834 datap = inpkt->u.data;
1836 while (items-- > 0) {
1837 memcpy(&cr, datap, item_sz);
1838 cr.flags = ntohs(cr.flags);
1839 cr.mflags = ntohs(cr.mflags);
1840 if (client_v6_capable && cr.v6_flag) {
1841 AF(&matchaddr) = AF_INET6;
1842 AF(&matchmask) = AF_INET6;
1843 SOCK_ADDR6(&matchaddr) = cr.addr6;
1844 SOCK_ADDR6(&matchmask) = cr.mask6;
1846 AF(&matchaddr) = AF_INET;
1847 AF(&matchmask) = AF_INET;
1848 NSRCADR(&matchaddr) = cr.addr;
1849 NSRCADR(&matchmask) = cr.mask;
1851 hack_restrict(op, &matchaddr, &matchmask, cr.mflags,
1856 req_ack(srcadr, inter, inpkt, INFO_OKAY);
1861 * mon_getlist - return monitor data
1867 struct req_pkt *inpkt
1870 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
1875 * Module entry points and the flags they correspond with
1877 struct reset_entry {
1878 int flag; /* flag this corresponds to */
1879 void (*handler)(void); /* routine to handle request */
1882 struct reset_entry reset_entries[] = {
1883 { RESET_FLAG_ALLPEERS, peer_all_reset },
1884 { RESET_FLAG_IO, io_clr_stats },
1885 { RESET_FLAG_SYS, proto_clr_stats },
1886 { RESET_FLAG_MEM, peer_clr_stats },
1887 { RESET_FLAG_TIMER, timer_clr_stats },
1888 { RESET_FLAG_AUTH, reset_auth_stats },
1889 { RESET_FLAG_CTL, ctl_clr_stats },
1894 * reset_stats - reset statistic counters here and there
1900 struct req_pkt *inpkt
1903 struct reset_flags *rflags;
1905 struct reset_entry *rent;
1907 if (INFO_NITEMS(inpkt->err_nitems) > 1) {
1908 msyslog(LOG_ERR, "reset_stats: err_nitems > 1");
1909 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1913 rflags = (struct reset_flags *)&inpkt->u;
1914 flags = ntohl(rflags->flags);
1916 if (flags & ~RESET_ALLFLAGS) {
1917 msyslog(LOG_ERR, "reset_stats: reset leaves %#lx",
1918 flags & ~RESET_ALLFLAGS);
1919 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1923 for (rent = reset_entries; rent->flag != 0; rent++) {
1924 if (flags & rent->flag)
1927 req_ack(srcadr, inter, inpkt, INFO_OKAY);
1932 * reset_peer - clear a peer's statistics
1938 struct req_pkt *inpkt
1944 struct conf_unpeer cp;
1946 sockaddr_u peeraddr;
1950 * We check first to see that every peer exists. If not,
1951 * we return an error.
1954 items = INFO_NITEMS(inpkt->err_nitems);
1955 item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize);
1956 datap = inpkt->u.data;
1957 if (item_sz > sizeof(cp)) {
1958 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
1963 while (items-- > 0 && !bad) {
1965 memcpy(&cp, datap, item_sz);
1966 ZERO_SOCK(&peeraddr);
1967 if (client_v6_capable && cp.v6_flag) {
1968 AF(&peeraddr) = AF_INET6;
1969 SOCK_ADDR6(&peeraddr) = cp.peeraddr6;
1971 AF(&peeraddr) = AF_INET;
1972 NSRCADR(&peeraddr) = cp.peeraddr;
1975 #ifdef ISC_PLATFORM_HAVESALEN
1976 peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
1978 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
1985 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
1990 * Now do it in earnest. Needs to reload data pointer and item
1991 * counter. (Talos-CAN-0052)
1994 items = INFO_NITEMS(inpkt->err_nitems);
1995 datap = inpkt->u.data;
1996 while (items-- > 0) {
1998 memcpy(&cp, datap, item_sz);
1999 ZERO_SOCK(&peeraddr);
2000 if (client_v6_capable && cp.v6_flag) {
2001 AF(&peeraddr) = AF_INET6;
2002 SOCK_ADDR6(&peeraddr) = cp.peeraddr6;
2004 AF(&peeraddr) = AF_INET;
2005 NSRCADR(&peeraddr) = cp.peeraddr;
2007 SET_PORT(&peeraddr, 123);
2008 #ifdef ISC_PLATFORM_HAVESALEN
2009 peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
2011 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
2014 p = findexistingpeer(&peeraddr, NULL, p, -1, 0);
2019 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2024 * do_key_reread - reread the encryption key file
2030 struct req_pkt *inpkt
2034 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2039 * trust_key - make one or more keys trusted
2045 struct req_pkt *inpkt
2048 do_trustkey(srcadr, inter, inpkt, 1);
2053 * untrust_key - make one or more keys untrusted
2059 struct req_pkt *inpkt
2062 do_trustkey(srcadr, inter, inpkt, 0);
2067 * do_trustkey - make keys either trustable or untrustable
2073 struct req_pkt *inpkt,
2077 register uint32_t *kp;
2080 items = INFO_NITEMS(inpkt->err_nitems);
2081 kp = (uint32_t *)&inpkt->u;
2082 while (items-- > 0) {
2083 authtrust(*kp, trust);
2087 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2092 * get_auth_info - return some stats concerning the authentication module
2098 struct req_pkt *inpkt
2101 register struct info_auth *ia;
2103 ia = (struct info_auth *)prepare_pkt(srcadr, inter, inpkt,
2104 sizeof(struct info_auth));
2106 ia->numkeys = htonl((u_int32)authnumkeys);
2107 ia->numfreekeys = htonl((u_int32)authnumfreekeys);
2108 ia->keylookups = htonl((u_int32)authkeylookups);
2109 ia->keynotfound = htonl((u_int32)authkeynotfound);
2110 ia->encryptions = htonl((u_int32)authencryptions);
2111 ia->decryptions = htonl((u_int32)authdecryptions);
2112 ia->keyuncached = htonl((u_int32)authkeyuncached);
2113 ia->expired = htonl((u_int32)authkeyexpired);
2114 ia->timereset = htonl((u_int32)(current_time - auth_timereset));
2123 * reset_auth_stats - reset the authentication stat counters. Done here
2124 * to keep ntp-isms out of the authentication module
2127 reset_auth_stats(void)
2130 authkeynotfound = 0;
2131 authencryptions = 0;
2132 authdecryptions = 0;
2133 authkeyuncached = 0;
2134 auth_timereset = current_time;
2139 * req_get_traps - return information about current trap holders
2145 struct req_pkt *inpkt
2148 struct info_trap *it;
2149 struct ctl_trap *tr;
2152 if (num_ctl_traps == 0) {
2153 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2157 it = (struct info_trap *)prepare_pkt(srcadr, inter, inpkt,
2158 v6sizeof(struct info_trap));
2160 for (i = 0, tr = ctl_traps; it && i < COUNTOF(ctl_traps); i++, tr++) {
2161 if (tr->tr_flags & TRAP_INUSE) {
2162 if (IS_IPV4(&tr->tr_addr)) {
2163 if (tr->tr_localaddr == any_interface)
2164 it->local_address = 0;
2167 = NSRCADR(&tr->tr_localaddr->sin);
2168 it->trap_address = NSRCADR(&tr->tr_addr);
2169 if (client_v6_capable)
2172 if (!client_v6_capable)
2175 = SOCK_ADDR6(&tr->tr_localaddr->sin);
2176 it->trap_address6 = SOCK_ADDR6(&tr->tr_addr);
2179 it->trap_port = NSRCPORT(&tr->tr_addr);
2180 it->sequence = htons(tr->tr_sequence);
2181 it->settime = htonl((u_int32)(current_time - tr->tr_settime));
2182 it->origtime = htonl((u_int32)(current_time - tr->tr_origtime));
2183 it->resets = htonl((u_int32)tr->tr_resets);
2184 it->flags = htonl((u_int32)tr->tr_flags);
2185 it = (struct info_trap *)more_pkt();
2193 * req_set_trap - configure a trap
2199 struct req_pkt *inpkt
2202 do_setclr_trap(srcadr, inter, inpkt, 1);
2208 * req_clr_trap - unconfigure a trap
2214 struct req_pkt *inpkt
2217 do_setclr_trap(srcadr, inter, inpkt, 0);
2223 * do_setclr_trap - do the grunge work of (un)configuring a trap
2229 struct req_pkt *inpkt,
2233 register struct conf_trap *ct;
2234 register endpt *linter;
2242 AF(&laddr) = AF(srcadr);
2243 SET_PORT(&laddr, NTP_PORT);
2246 * Restrict ourselves to one item only. This eliminates
2247 * the error reporting problem.
2249 if (INFO_NITEMS(inpkt->err_nitems) > 1) {
2250 msyslog(LOG_ERR, "do_setclr_trap: err_nitems > 1");
2251 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
2254 ct = (struct conf_trap *)&inpkt->u;
2257 * Look for the local interface. If none, use the default.
2259 if (ct->local_address == 0) {
2260 linter = any_interface;
2262 if (IS_IPV4(&laddr))
2263 NSRCADR(&laddr) = ct->local_address;
2265 SOCK_ADDR6(&laddr) = ct->local_address6;
2266 linter = findinterface(&laddr);
2267 if (NULL == linter) {
2268 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2273 if (IS_IPV4(&laddr))
2274 NSRCADR(&laddr) = ct->trap_address;
2276 SOCK_ADDR6(&laddr) = ct->trap_address6;
2278 NSRCPORT(&laddr) = ct->trap_port;
2280 SET_PORT(&laddr, TRAPPORT);
2283 res = ctlsettrap(&laddr, linter, 0,
2284 INFO_VERSION(inpkt->rm_vn_mode));
2286 res = ctlclrtrap(&laddr, linter, 0);
2290 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2292 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2298 * Validate a request packet for a new request or control key:
2299 * - only one item allowed
2300 * - key must be valid (that is, known, and not in the autokey range)
2308 struct req_pkt *inpkt
2314 /* restrict ourselves to one item only */
2315 if (INFO_NITEMS(inpkt->err_nitems) > 1) {
2316 msyslog(LOG_ERR, "set_keyid_checked[%s]: err_nitems > 1",
2318 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
2322 /* plug the new key from the packet */
2323 pkeyid = (keyid_t *)&inpkt->u;
2324 tmpkey = ntohl(*pkeyid);
2326 /* validate the new key id, claim data error on failure */
2327 if (tmpkey < 1 || tmpkey > NTP_MAXKEY || !auth_havekey(tmpkey)) {
2328 msyslog(LOG_ERR, "set_keyid_checked[%s]: invalid key id: %ld",
2329 what, (long)tmpkey);
2330 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2334 /* if we arrive here, the key is good -- use it */
2336 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2340 * set_request_keyid - set the keyid used to authenticate requests
2346 struct req_pkt *inpkt
2349 set_keyid_checked(&info_auth_keyid, "request",
2350 srcadr, inter, inpkt);
2356 * set_control_keyid - set the keyid used to authenticate requests
2362 struct req_pkt *inpkt
2365 set_keyid_checked(&ctl_auth_keyid, "control",
2366 srcadr, inter, inpkt);
2372 * get_ctl_stats - return some stats concerning the control message module
2378 struct req_pkt *inpkt
2381 register struct info_control *ic;
2383 ic = (struct info_control *)prepare_pkt(srcadr, inter, inpkt,
2384 sizeof(struct info_control));
2386 ic->ctltimereset = htonl((u_int32)(current_time - ctltimereset));
2387 ic->numctlreq = htonl((u_int32)numctlreq);
2388 ic->numctlbadpkts = htonl((u_int32)numctlbadpkts);
2389 ic->numctlresponses = htonl((u_int32)numctlresponses);
2390 ic->numctlfrags = htonl((u_int32)numctlfrags);
2391 ic->numctlerrors = htonl((u_int32)numctlerrors);
2392 ic->numctltooshort = htonl((u_int32)numctltooshort);
2393 ic->numctlinputresp = htonl((u_int32)numctlinputresp);
2394 ic->numctlinputfrag = htonl((u_int32)numctlinputfrag);
2395 ic->numctlinputerr = htonl((u_int32)numctlinputerr);
2396 ic->numctlbadoffset = htonl((u_int32)numctlbadoffset);
2397 ic->numctlbadversion = htonl((u_int32)numctlbadversion);
2398 ic->numctldatatooshort = htonl((u_int32)numctldatatooshort);
2399 ic->numctlbadop = htonl((u_int32)numctlbadop);
2400 ic->numasyncmsgs = htonl((u_int32)numasyncmsgs);
2409 * get_kernel_info - get kernel pll/pps information
2415 struct req_pkt *inpkt
2418 register struct info_kernel *ik;
2422 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2427 if (ntp_adjtime(&ntx) < 0)
2428 msyslog(LOG_ERR, "get_kernel_info: ntp_adjtime() failed: %m");
2429 ik = (struct info_kernel *)prepare_pkt(srcadr, inter, inpkt,
2430 sizeof(struct info_kernel));
2435 ik->offset = htonl((u_int32)ntx.offset);
2436 ik->freq = htonl((u_int32)ntx.freq);
2437 ik->maxerror = htonl((u_int32)ntx.maxerror);
2438 ik->esterror = htonl((u_int32)ntx.esterror);
2439 ik->status = htons(ntx.status);
2440 ik->constant = htonl((u_int32)ntx.constant);
2441 ik->precision = htonl((u_int32)ntx.precision);
2442 ik->tolerance = htonl((u_int32)ntx.tolerance);
2447 ik->ppsfreq = htonl((u_int32)ntx.ppsfreq);
2448 ik->jitter = htonl((u_int32)ntx.jitter);
2449 ik->shift = htons(ntx.shift);
2450 ik->stabil = htonl((u_int32)ntx.stabil);
2451 ik->jitcnt = htonl((u_int32)ntx.jitcnt);
2452 ik->calcnt = htonl((u_int32)ntx.calcnt);
2453 ik->errcnt = htonl((u_int32)ntx.errcnt);
2454 ik->stbcnt = htonl((u_int32)ntx.stbcnt);
2459 #endif /* KERNEL_PLL */
2464 * get_clock_info - get info about a clock
2470 struct req_pkt *inpkt
2473 register struct info_clock *ic;
2474 register u_int32 *clkaddr;
2476 struct refclockstat clock_stat;
2481 AF(&addr) = AF_INET;
2482 #ifdef ISC_PLATFORM_HAVESALEN
2483 addr.sa.sa_len = SOCKLEN(&addr);
2485 SET_PORT(&addr, NTP_PORT);
2486 items = INFO_NITEMS(inpkt->err_nitems);
2487 clkaddr = &inpkt->u.u32[0];
2489 ic = (struct info_clock *)prepare_pkt(srcadr, inter, inpkt,
2490 sizeof(struct info_clock));
2492 while (items-- > 0 && ic) {
2493 NSRCADR(&addr) = *clkaddr++;
2494 if (!ISREFCLOCKADR(&addr) || NULL ==
2495 findexistingpeer(&addr, NULL, NULL, -1, 0)) {
2496 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2500 clock_stat.kv_list = (struct ctl_var *)0;
2502 refclock_control(&addr, NULL, &clock_stat);
2504 ic->clockadr = NSRCADR(&addr);
2505 ic->type = clock_stat.type;
2506 ic->flags = clock_stat.flags;
2507 ic->lastevent = clock_stat.lastevent;
2508 ic->currentstatus = clock_stat.currentstatus;
2509 ic->polls = htonl((u_int32)clock_stat.polls);
2510 ic->noresponse = htonl((u_int32)clock_stat.noresponse);
2511 ic->badformat = htonl((u_int32)clock_stat.badformat);
2512 ic->baddata = htonl((u_int32)clock_stat.baddata);
2513 ic->timestarted = htonl((u_int32)clock_stat.timereset);
2514 DTOLFP(clock_stat.fudgetime1, <mp);
2515 HTONL_FP(<mp, &ic->fudgetime1);
2516 DTOLFP(clock_stat.fudgetime2, <mp);
2517 HTONL_FP(<mp, &ic->fudgetime2);
2518 ic->fudgeval1 = htonl((u_int32)clock_stat.fudgeval1);
2519 ic->fudgeval2 = htonl(clock_stat.fudgeval2);
2521 free_varlist(clock_stat.kv_list);
2523 ic = (struct info_clock *)more_pkt();
2531 * set_clock_fudge - get a clock's fudge factors
2537 struct req_pkt *inpkt
2540 register struct conf_fudge *cf;
2542 struct refclockstat clock_stat;
2548 items = INFO_NITEMS(inpkt->err_nitems);
2549 cf = (struct conf_fudge *)&inpkt->u;
2551 while (items-- > 0) {
2552 AF(&addr) = AF_INET;
2553 NSRCADR(&addr) = cf->clockadr;
2554 #ifdef ISC_PLATFORM_HAVESALEN
2555 addr.sa.sa_len = SOCKLEN(&addr);
2557 SET_PORT(&addr, NTP_PORT);
2558 if (!ISREFCLOCKADR(&addr) || NULL ==
2559 findexistingpeer(&addr, NULL, NULL, -1, 0)) {
2560 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2564 switch(ntohl(cf->which)) {
2566 NTOHL_FP(&cf->fudgetime, <mp);
2567 LFPTOD(<mp, clock_stat.fudgetime1);
2568 clock_stat.haveflags = CLK_HAVETIME1;
2571 NTOHL_FP(&cf->fudgetime, <mp);
2572 LFPTOD(<mp, clock_stat.fudgetime2);
2573 clock_stat.haveflags = CLK_HAVETIME2;
2576 clock_stat.fudgeval1 = ntohl(cf->fudgeval_flags);
2577 clock_stat.haveflags = CLK_HAVEVAL1;
2580 clock_stat.fudgeval2 = ntohl(cf->fudgeval_flags);
2581 clock_stat.haveflags = CLK_HAVEVAL2;
2584 clock_stat.flags = (u_char) (ntohl(cf->fudgeval_flags) & 0xf);
2585 clock_stat.haveflags =
2586 (CLK_HAVEFLAG1|CLK_HAVEFLAG2|CLK_HAVEFLAG3|CLK_HAVEFLAG4);
2589 msyslog(LOG_ERR, "set_clock_fudge: default!");
2590 req_ack(srcadr, inter, inpkt, INFO_ERR_FMT);
2594 refclock_control(&addr, &clock_stat, (struct refclockstat *)0);
2597 req_ack(srcadr, inter, inpkt, INFO_OKAY);
2603 * get_clkbug_info - get debugging info about a clock
2609 struct req_pkt *inpkt
2613 register struct info_clkbug *ic;
2614 register u_int32 *clkaddr;
2616 struct refclockbug bug;
2620 AF(&addr) = AF_INET;
2621 #ifdef ISC_PLATFORM_HAVESALEN
2622 addr.sa.sa_len = SOCKLEN(&addr);
2624 SET_PORT(&addr, NTP_PORT);
2625 items = INFO_NITEMS(inpkt->err_nitems);
2626 clkaddr = (u_int32 *)&inpkt->u;
2628 ic = (struct info_clkbug *)prepare_pkt(srcadr, inter, inpkt,
2629 sizeof(struct info_clkbug));
2631 while (items-- > 0 && ic) {
2632 NSRCADR(&addr) = *clkaddr++;
2633 if (!ISREFCLOCKADR(&addr) || NULL ==
2634 findexistingpeer(&addr, NULL, NULL, -1, 0)) {
2635 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2640 refclock_buginfo(&addr, &bug);
2641 if (bug.nvalues == 0 && bug.ntimes == 0) {
2642 req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
2646 ic->clockadr = NSRCADR(&addr);
2648 if (i > NUMCBUGVALUES)
2650 ic->nvalues = (u_char)i;
2651 ic->svalues = htons((u_short) (bug.svalues & ((1<<i)-1)));
2653 ic->values[i] = htonl(bug.values[i]);
2656 if (i > NUMCBUGTIMES)
2658 ic->ntimes = (u_char)i;
2659 ic->stimes = htonl(bug.stimes);
2661 HTONL_FP(&bug.times[i], &ic->times[i]);
2664 ic = (struct info_clkbug *)more_pkt();
2671 * receiver of interface structures
2674 fill_info_if_stats(void *data, interface_info_t *interface_info)
2676 struct info_if_stats **ifsp = (struct info_if_stats **)data;
2677 struct info_if_stats *ifs = *ifsp;
2678 endpt *ep = interface_info->ep;
2685 if (IS_IPV6(&ep->sin)) {
2686 if (!client_v6_capable)
2689 ifs->unaddr.addr6 = SOCK_ADDR6(&ep->sin);
2690 ifs->unbcast.addr6 = SOCK_ADDR6(&ep->bcast);
2691 ifs->unmask.addr6 = SOCK_ADDR6(&ep->mask);
2694 ifs->unaddr.addr = SOCK_ADDR4(&ep->sin);
2695 ifs->unbcast.addr = SOCK_ADDR4(&ep->bcast);
2696 ifs->unmask.addr = SOCK_ADDR4(&ep->mask);
2698 ifs->v6_flag = htonl(ifs->v6_flag);
2699 strlcpy(ifs->name, ep->name, sizeof(ifs->name));
2700 ifs->family = htons(ep->family);
2701 ifs->flags = htonl(ep->flags);
2702 ifs->last_ttl = htonl(ep->last_ttl);
2703 ifs->num_mcast = htonl(ep->num_mcast);
2704 ifs->received = htonl(ep->received);
2705 ifs->sent = htonl(ep->sent);
2706 ifs->notsent = htonl(ep->notsent);
2707 ifs->ifindex = htonl(ep->ifindex);
2708 /* scope no longer in endpt, in in6_addr typically */
2709 ifs->scopeid = ifs->ifindex;
2710 ifs->ifnum = htonl(ep->ifnum);
2711 ifs->uptime = htonl(current_time - ep->starttime);
2712 ifs->ignore_packets = ep->ignore_packets;
2713 ifs->peercnt = htonl(ep->peercnt);
2714 ifs->action = interface_info->action;
2716 *ifsp = (struct info_if_stats *)more_pkt();
2720 * get_if_stats - get interface statistics
2726 struct req_pkt *inpkt
2729 struct info_if_stats *ifs;
2731 DPRINTF(3, ("wants interface statistics\n"));
2733 ifs = (struct info_if_stats *)prepare_pkt(srcadr, inter, inpkt,
2734 v6sizeof(struct info_if_stats));
2736 interface_enumerate(fill_info_if_stats, &ifs);
2745 struct req_pkt *inpkt
2748 struct info_if_stats *ifs;
2750 DPRINTF(3, ("wants interface reload\n"));
2752 ifs = (struct info_if_stats *)prepare_pkt(srcadr, inter, inpkt,
2753 v6sizeof(struct info_if_stats));
2755 interface_update(fill_info_if_stats, &ifs);