/src/ntp-dev/ntpd/ntp_request.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * ntp_request.c - respond to information requests |
3 | | */ |
4 | | |
5 | | #ifdef HAVE_CONFIG_H |
6 | | # include <config.h> |
7 | | #endif |
8 | | |
9 | | #include "ntpd.h" |
10 | | #include "ntp_io.h" |
11 | | #include "ntp_request.h" |
12 | | #include "ntp_control.h" |
13 | | #include "ntp_refclock.h" |
14 | | #include "ntp_if.h" |
15 | | #include "ntp_stdlib.h" |
16 | | #include "ntp_assert.h" |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <stddef.h> |
20 | | #include <signal.h> |
21 | | #ifdef HAVE_NETINET_IN_H |
22 | | #include <netinet/in.h> |
23 | | #endif |
24 | | #include <arpa/inet.h> |
25 | | |
26 | | #include "recvbuff.h" |
27 | | |
28 | | #ifdef KERNEL_PLL |
29 | | #include "ntp_syscall.h" |
30 | | #endif /* KERNEL_PLL */ |
31 | | |
32 | | /* |
33 | | * Structure to hold request procedure information |
34 | | */ |
35 | | #define NOAUTH 0 |
36 | | #define AUTH 1 |
37 | | |
38 | 0 | #define NO_REQUEST (-1) |
39 | | /* |
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. |
43 | | */ |
44 | | static int client_v6_capable = 0; /* the client can handle longer messages */ |
45 | | |
46 | 0 | #define v6sizeof(type) (client_v6_capable ? sizeof(type) : v4sizeof(type)) |
47 | | |
48 | | struct req_proc { |
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 */ |
55 | | }; |
56 | | |
57 | | /* |
58 | | * Universal request codes |
59 | | */ |
60 | | static const struct req_proc univ_codes[] = { |
61 | | { NO_REQUEST, NOAUTH, 0, 0, NULL } |
62 | | }; |
63 | | |
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 *, restrict_op); |
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 *); |
108 | | #ifdef KERNEL_PLL |
109 | | static void get_kernel_info (sockaddr_u *, endpt *, struct req_pkt *); |
110 | | #endif /* KERNEL_PLL */ |
111 | | #ifdef REFCLOCK |
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 */ |
115 | | #ifdef REFCLOCK |
116 | | static void get_clkbug_info (sockaddr_u *, endpt *, struct req_pkt *); |
117 | | #endif /* REFCLOCK */ |
118 | | |
119 | | /* |
120 | | * ntpd request codes |
121 | | */ |
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), |
165 | | set_request_keyid }, |
166 | | { REQ_CONTROL_KEY, AUTH, sizeof(u_long), sizeof(u_long), |
167 | | set_control_keyid }, |
168 | | { REQ_GET_CTLSTATS, NOAUTH, 0, 0, get_ctl_stats }, |
169 | | #ifdef KERNEL_PLL |
170 | | { REQ_GET_KERNEL, NOAUTH, 0, 0, get_kernel_info }, |
171 | | #endif |
172 | | #ifdef REFCLOCK |
173 | | { REQ_GET_CLOCKINFO, NOAUTH, sizeof(u_int32), sizeof(u_int32), |
174 | | get_clock_info }, |
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), |
178 | | get_clkbug_info }, |
179 | | #endif |
180 | | { REQ_IF_STATS, AUTH, 0, 0, get_if_stats }, |
181 | | { REQ_IF_RELOAD, AUTH, 0, 0, do_if_reload }, |
182 | | |
183 | | { NO_REQUEST, NOAUTH, 0, 0, 0 } |
184 | | }; |
185 | | |
186 | | |
187 | | /* |
188 | | * Authentication keyid used to authenticate requests. Zero means we |
189 | | * don't allow writing anything. |
190 | | */ |
191 | | keyid_t info_auth_keyid; |
192 | | |
193 | | /* |
194 | | * Statistic counters to keep track of requests and responses. |
195 | | */ |
196 | | u_long numrequests; /* number of requests we've received */ |
197 | | u_long numresppkts; /* number of resp packets sent with data */ |
198 | | |
199 | | /* |
200 | | * lazy way to count errors, indexed by the error code |
201 | | */ |
202 | | u_long errorcounter[MAX_INFO_ERR + 1]; |
203 | | |
204 | | /* |
205 | | * A hack. To keep the authentication module clear of ntp-ism's, we |
206 | | * include a time reset variable for its stats here. |
207 | | */ |
208 | | u_long auth_timereset; |
209 | | |
210 | | /* |
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. |
216 | | */ |
217 | | static struct resp_pkt rpkt; |
218 | | static int reqver; |
219 | | static int seqno; |
220 | | static int nitems; |
221 | | static int itemsize; |
222 | | static int databytes; |
223 | | static char exbuf[RESP_DATA_SIZE]; |
224 | | static int usingexbuf; |
225 | | static sockaddr_u *toaddr; |
226 | | static endpt *frominter; |
227 | | |
228 | | /* |
229 | | * init_request - initialize request data |
230 | | */ |
231 | | void |
232 | | init_request (void) |
233 | 1 | { |
234 | 1 | size_t i; |
235 | | |
236 | 1 | numrequests = 0; |
237 | 1 | numresppkts = 0; |
238 | 1 | auth_timereset = 0; |
239 | 1 | info_auth_keyid = 0; /* by default, can't do this */ |
240 | | |
241 | 9 | for (i = 0; i < sizeof(errorcounter)/sizeof(errorcounter[0]); i++) |
242 | 8 | errorcounter[i] = 0; |
243 | 1 | } |
244 | | |
245 | | |
246 | | /* |
247 | | * req_ack - acknowledge request with no data |
248 | | */ |
249 | | static void |
250 | | req_ack( |
251 | | sockaddr_u *srcadr, |
252 | | endpt *inter, |
253 | | struct req_pkt *inpkt, |
254 | | int errcode |
255 | | ) |
256 | 0 | { |
257 | | /* |
258 | | * fill in the fields |
259 | | */ |
260 | 0 | rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, 0, reqver); |
261 | 0 | rpkt.auth_seq = AUTH_SEQ(0, 0); |
262 | 0 | rpkt.implementation = inpkt->implementation; |
263 | 0 | rpkt.request = inpkt->request; |
264 | 0 | rpkt.err_nitems = ERR_NITEMS(errcode, 0); |
265 | 0 | rpkt.mbz_itemsize = MBZ_ITEMSIZE(0); |
266 | | |
267 | | /* |
268 | | * send packet and bump counters |
269 | | */ |
270 | 0 | sendpkt(srcadr, inter, -1, (struct pkt *)&rpkt, RESP_HEADER_SIZE); |
271 | 0 | errorcounter[errcode]++; |
272 | 0 | } |
273 | | |
274 | | |
275 | | /* |
276 | | * prepare_pkt - prepare response packet for transmission, return pointer |
277 | | * to storage for data item. |
278 | | */ |
279 | | static void * |
280 | | prepare_pkt( |
281 | | sockaddr_u *srcadr, |
282 | | endpt *inter, |
283 | | struct req_pkt *pkt, |
284 | | size_t structsize |
285 | | ) |
286 | 0 | { |
287 | 0 | DPRINTF(4, ("request: preparing pkt\n")); |
288 | | |
289 | | /* |
290 | | * Fill in the implementation, request and itemsize fields |
291 | | * since these won't change. |
292 | | */ |
293 | 0 | rpkt.implementation = pkt->implementation; |
294 | 0 | rpkt.request = pkt->request; |
295 | 0 | rpkt.mbz_itemsize = MBZ_ITEMSIZE(structsize); |
296 | | |
297 | | /* |
298 | | * Compute the static data needed to carry on. |
299 | | */ |
300 | 0 | toaddr = srcadr; |
301 | 0 | frominter = inter; |
302 | 0 | seqno = 0; |
303 | 0 | nitems = 0; |
304 | 0 | itemsize = structsize; |
305 | 0 | databytes = 0; |
306 | 0 | usingexbuf = 0; |
307 | | |
308 | | /* |
309 | | * return the beginning of the packet buffer. |
310 | | */ |
311 | 0 | return &rpkt.u; |
312 | 0 | } |
313 | | |
314 | | |
315 | | /* |
316 | | * more_pkt - return a data pointer for a new item. |
317 | | */ |
318 | | static void * |
319 | | more_pkt(void) |
320 | 0 | { |
321 | | /* |
322 | | * If we were using the extra buffer, send the packet. |
323 | | */ |
324 | 0 | if (usingexbuf) { |
325 | 0 | DPRINTF(3, ("request: sending pkt\n")); |
326 | 0 | rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, MORE_BIT, reqver); |
327 | 0 | rpkt.auth_seq = AUTH_SEQ(0, seqno); |
328 | 0 | rpkt.err_nitems = htons((u_short)nitems); |
329 | 0 | sendpkt(toaddr, frominter, -1, (struct pkt *)&rpkt, |
330 | 0 | RESP_HEADER_SIZE + databytes); |
331 | 0 | numresppkts++; |
332 | | |
333 | | /* |
334 | | * Copy data out of exbuf into the packet. |
335 | | */ |
336 | 0 | memcpy(&rpkt.u.data[0], exbuf, (unsigned)itemsize); |
337 | 0 | seqno++; |
338 | 0 | databytes = 0; |
339 | 0 | nitems = 0; |
340 | 0 | usingexbuf = 0; |
341 | 0 | } |
342 | |
|
343 | 0 | databytes += itemsize; |
344 | 0 | nitems++; |
345 | 0 | if (databytes + itemsize <= RESP_DATA_SIZE) { |
346 | 0 | DPRINTF(4, ("request: giving him more data\n")); |
347 | | /* |
348 | | * More room in packet. Give him the |
349 | | * next address. |
350 | | */ |
351 | 0 | return &rpkt.u.data[databytes]; |
352 | 0 | } else { |
353 | | /* |
354 | | * No room in packet. Give him the extra |
355 | | * buffer unless this was the last in the sequence. |
356 | | */ |
357 | 0 | DPRINTF(4, ("request: into extra buffer\n")); |
358 | 0 | if (seqno == MAXSEQ) |
359 | 0 | return NULL; |
360 | 0 | else { |
361 | 0 | usingexbuf = 1; |
362 | 0 | return exbuf; |
363 | 0 | } |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | | |
368 | | /* |
369 | | * flush_pkt - we're done, return remaining information. |
370 | | */ |
371 | | static void |
372 | | flush_pkt(void) |
373 | 0 | { |
374 | 0 | DPRINTF(3, ("request: flushing packet, %d items\n", nitems)); |
375 | | /* |
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. |
378 | | */ |
379 | 0 | if (seqno == 0 && nitems == 0) |
380 | 0 | req_ack(toaddr, frominter, (struct req_pkt *)&rpkt, |
381 | 0 | INFO_ERR_NODATA); |
382 | 0 | else { |
383 | 0 | rpkt.rm_vn_mode = RM_VN_MODE(RESP_BIT, 0, reqver); |
384 | 0 | rpkt.auth_seq = AUTH_SEQ(0, seqno); |
385 | 0 | rpkt.err_nitems = htons((u_short)nitems); |
386 | 0 | sendpkt(toaddr, frominter, -1, (struct pkt *)&rpkt, |
387 | 0 | RESP_HEADER_SIZE+databytes); |
388 | 0 | numresppkts++; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | |
393 | | |
394 | | /* |
395 | | * Given a buffer, return the packet mode |
396 | | */ |
397 | | int |
398 | | get_packet_mode(struct recvbuf *rbufp) |
399 | 0 | { |
400 | 0 | struct req_pkt *inpkt = (struct req_pkt *)&rbufp->recv_pkt; |
401 | 0 | return (INFO_MODE(inpkt->rm_vn_mode)); |
402 | 0 | } |
403 | | |
404 | | |
405 | | /* |
406 | | * process_private - process private mode (7) packets |
407 | | */ |
408 | | void |
409 | | process_private( |
410 | | struct recvbuf *rbufp, |
411 | | int mod_okay |
412 | | ) |
413 | 0 | { |
414 | 0 | static u_long quiet_until; |
415 | 0 | struct req_pkt *inpkt; |
416 | 0 | struct req_pkt_tail *tailinpkt; |
417 | 0 | sockaddr_u *srcadr; |
418 | 0 | endpt *inter; |
419 | 0 | const struct req_proc *proc; |
420 | 0 | int ec; |
421 | 0 | short temp_size; |
422 | 0 | l_fp ftmp; |
423 | 0 | double dtemp; |
424 | 0 | size_t recv_len; |
425 | 0 | size_t noslop_len; |
426 | 0 | size_t mac_len; |
427 | | |
428 | | /* |
429 | | * Initialize pointers, for convenience |
430 | | */ |
431 | 0 | recv_len = rbufp->recv_length; |
432 | 0 | inpkt = (struct req_pkt *)&rbufp->recv_pkt; |
433 | 0 | srcadr = &rbufp->recv_srcadr; |
434 | 0 | inter = rbufp->dstadr; |
435 | |
|
436 | 0 | DPRINTF(3, ("process_private: impl %d req %d\n", |
437 | 0 | inpkt->implementation, inpkt->request)); |
438 | | |
439 | | /* |
440 | | * Do some sanity checks on the packet. Return a format |
441 | | * error if it fails. |
442 | | */ |
443 | 0 | ec = 0; |
444 | 0 | if ( (++ec, ISRESPONSE(inpkt->rm_vn_mode)) |
445 | 0 | || (++ec, ISMORE(inpkt->rm_vn_mode)) |
446 | 0 | || (++ec, INFO_VERSION(inpkt->rm_vn_mode) > NTP_VERSION) |
447 | 0 | || (++ec, INFO_VERSION(inpkt->rm_vn_mode) < NTP_OLDVERSION) |
448 | 0 | || (++ec, INFO_SEQ(inpkt->auth_seq) != 0) |
449 | 0 | || (++ec, INFO_ERR(inpkt->err_nitems) != 0) |
450 | 0 | || (++ec, INFO_MBZ(inpkt->mbz_itemsize) != 0) |
451 | 0 | || (++ec, rbufp->recv_length < (int)REQ_LEN_HDR) |
452 | 0 | ) { |
453 | 0 | NLOG(NLOG_SYSEVENT) |
454 | 0 | if (current_time >= quiet_until) { |
455 | 0 | msyslog(LOG_ERR, |
456 | 0 | "process_private: drop test %d" |
457 | 0 | " failed, pkt from %s", |
458 | 0 | ec, stoa(srcadr)); |
459 | 0 | quiet_until = current_time + 60; |
460 | 0 | } |
461 | 0 | return; |
462 | 0 | } |
463 | | |
464 | 0 | reqver = INFO_VERSION(inpkt->rm_vn_mode); |
465 | | |
466 | | /* |
467 | | * Get the appropriate procedure list to search. |
468 | | */ |
469 | 0 | if (inpkt->implementation == IMPL_UNIV) |
470 | 0 | proc = univ_codes; |
471 | 0 | else if ((inpkt->implementation == IMPL_XNTPD) || |
472 | 0 | (inpkt->implementation == IMPL_XNTPD_OLD)) |
473 | 0 | proc = ntp_codes; |
474 | 0 | else { |
475 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_IMPL); |
476 | 0 | return; |
477 | 0 | } |
478 | | |
479 | | /* |
480 | | * Search the list for the request codes. If it isn't one |
481 | | * we know, return an error. |
482 | | */ |
483 | 0 | while (proc->request_code != NO_REQUEST) { |
484 | 0 | if (proc->request_code == (short) inpkt->request) |
485 | 0 | break; |
486 | 0 | proc++; |
487 | 0 | } |
488 | 0 | if (proc->request_code == NO_REQUEST) { |
489 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_REQ); |
490 | 0 | return; |
491 | 0 | } |
492 | | |
493 | 0 | DPRINTF(4, ("found request in tables\n")); |
494 | | |
495 | | /* |
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). |
498 | | */ |
499 | | |
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. |
503 | | * |
504 | | * Handle the exception of REQ_CONFIG. It can have two data sizes. |
505 | | */ |
506 | 0 | temp_size = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
507 | 0 | if ((temp_size != proc->sizeofitem && |
508 | 0 | temp_size != proc->v6_sizeofitem) && |
509 | 0 | !(inpkt->implementation == IMPL_XNTPD && |
510 | 0 | inpkt->request == REQ_CONFIG && |
511 | 0 | temp_size == sizeof(struct old_conf_peer))) { |
512 | 0 | DPRINTF(3, ("process_private: wrong item size, received %d, should be %d or %d\n", |
513 | 0 | temp_size, proc->sizeofitem, proc->v6_sizeofitem)); |
514 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
515 | 0 | return; |
516 | 0 | } |
517 | 0 | if ((proc->sizeofitem != 0) && |
518 | 0 | ((size_t)(temp_size * INFO_NITEMS(inpkt->err_nitems)) > |
519 | 0 | (recv_len - REQ_LEN_HDR))) { |
520 | 0 | DPRINTF(3, ("process_private: not enough data\n")); |
521 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
522 | 0 | return; |
523 | 0 | } |
524 | | |
525 | 0 | switch (inpkt->implementation) { |
526 | 0 | case IMPL_XNTPD: |
527 | 0 | client_v6_capable = 1; |
528 | 0 | break; |
529 | 0 | case IMPL_XNTPD_OLD: |
530 | 0 | client_v6_capable = 0; |
531 | 0 | break; |
532 | 0 | default: |
533 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
534 | 0 | return; |
535 | 0 | } |
536 | | |
537 | | /* |
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 |
543 | | * time stamp. |
544 | | */ |
545 | 0 | if (proc->needs_auth && sys_authenticate) { |
546 | |
|
547 | 0 | if (recv_len < (REQ_LEN_HDR + |
548 | 0 | (INFO_ITEMSIZE(inpkt->mbz_itemsize) * |
549 | 0 | INFO_NITEMS(inpkt->err_nitems)) + |
550 | 0 | REQ_TAIL_MIN)) { |
551 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
552 | 0 | return; |
553 | 0 | } |
554 | | |
555 | | /* |
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. |
565 | | */ |
566 | 0 | noslop_len = REQ_LEN_HDR |
567 | 0 | + INFO_ITEMSIZE(inpkt->mbz_itemsize) * |
568 | 0 | INFO_NITEMS(inpkt->err_nitems) |
569 | 0 | + sizeof(inpkt->tstamp); |
570 | | /* 32-bit alignment */ |
571 | 0 | noslop_len = (noslop_len + 3) & ~3; |
572 | 0 | if (recv_len > (noslop_len + MAX_MAC_LEN)) |
573 | 0 | mac_len = 20; |
574 | 0 | else |
575 | 0 | mac_len = recv_len - noslop_len; |
576 | |
|
577 | 0 | tailinpkt = (void *)((char *)inpkt + recv_len - |
578 | 0 | (mac_len + sizeof(inpkt->tstamp))); |
579 | | |
580 | | /* |
581 | | * If this guy is restricted from doing this, don't let |
582 | | * him. If the wrong key was used, or packet doesn't |
583 | | * have mac, return. |
584 | | */ |
585 | | /* XXX: Use authistrustedip(), or equivalent. */ |
586 | 0 | if (!INFO_IS_AUTH(inpkt->auth_seq) || !info_auth_keyid |
587 | 0 | || ntohl(tailinpkt->keyid) != info_auth_keyid) { |
588 | 0 | DPRINTF(5, ("failed auth %d info_auth_keyid %u pkt keyid %u maclen %lu\n", |
589 | 0 | INFO_IS_AUTH(inpkt->auth_seq), |
590 | 0 | info_auth_keyid, |
591 | 0 | ntohl(tailinpkt->keyid), (u_long)mac_len)); |
592 | 0 | #ifdef DEBUG |
593 | 0 | msyslog(LOG_DEBUG, |
594 | 0 | "process_private: failed auth %d info_auth_keyid %u pkt keyid %u maclen %lu\n", |
595 | 0 | INFO_IS_AUTH(inpkt->auth_seq), |
596 | 0 | info_auth_keyid, |
597 | 0 | ntohl(tailinpkt->keyid), (u_long)mac_len); |
598 | 0 | #endif |
599 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH); |
600 | 0 | return; |
601 | 0 | } |
602 | 0 | if (recv_len > REQ_LEN_NOMAC + MAX_MAC_LEN) { |
603 | 0 | DPRINTF(5, ("bad pkt length %zu\n", recv_len)); |
604 | 0 | msyslog(LOG_ERR, |
605 | 0 | "process_private: bad pkt length %zu", |
606 | 0 | recv_len); |
607 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
608 | 0 | return; |
609 | 0 | } |
610 | 0 | if (!mod_okay || !authhavekey(info_auth_keyid)) { |
611 | 0 | DPRINTF(5, ("failed auth mod_okay %d\n", |
612 | 0 | mod_okay)); |
613 | 0 | #ifdef DEBUG |
614 | 0 | msyslog(LOG_DEBUG, |
615 | 0 | "process_private: failed auth mod_okay %d\n", |
616 | 0 | mod_okay); |
617 | 0 | #endif |
618 | 0 | if (!mod_okay) { |
619 | 0 | sys_restricted++; |
620 | 0 | } |
621 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH); |
622 | 0 | return; |
623 | 0 | } |
624 | | |
625 | | /* |
626 | | * calculate absolute time difference between xmit time stamp |
627 | | * and receive time stamp. If too large, too bad. |
628 | | */ |
629 | 0 | NTOHL_FP(&tailinpkt->tstamp, &ftmp); |
630 | 0 | L_SUB(&ftmp, &rbufp->recv_time); |
631 | 0 | LFPTOD(&ftmp, dtemp); |
632 | 0 | if (fabs(dtemp) > INFO_TS_MAXSKEW) { |
633 | | /* |
634 | | * He's a loser. Tell him. |
635 | | */ |
636 | 0 | DPRINTF(5, ("xmit/rcv timestamp delta %g > INFO_TS_MAXSKEW %g\n", |
637 | 0 | dtemp, INFO_TS_MAXSKEW)); |
638 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH); |
639 | 0 | return; |
640 | 0 | } |
641 | | |
642 | | /* |
643 | | * So far so good. See if decryption works out okay. |
644 | | */ |
645 | 0 | if (!authdecrypt(info_auth_keyid, (u_int32 *)inpkt, |
646 | 0 | recv_len - mac_len, mac_len)) { |
647 | 0 | DPRINTF(5, ("authdecrypt failed\n")); |
648 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_AUTH); |
649 | 0 | return; |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | 0 | DPRINTF(3, ("process_private: all okay, into handler\n")); |
654 | | /* |
655 | | * Packet is okay. Call the handler to send him data. |
656 | | */ |
657 | 0 | (proc->handler)(srcadr, inter, inpkt); |
658 | 0 | } |
659 | | |
660 | | |
661 | | /* |
662 | | * list_peers - send a list of the peers |
663 | | */ |
664 | | static void |
665 | | list_peers( |
666 | | sockaddr_u *srcadr, |
667 | | endpt *inter, |
668 | | struct req_pkt *inpkt |
669 | | ) |
670 | 0 | { |
671 | 0 | struct info_peer_list * ip; |
672 | 0 | const struct peer * pp; |
673 | |
|
674 | 0 | ip = (struct info_peer_list *)prepare_pkt(srcadr, inter, inpkt, |
675 | 0 | v6sizeof(struct info_peer_list)); |
676 | 0 | for (pp = peer_list; pp != NULL && ip != NULL; pp = pp->p_link) { |
677 | 0 | if (IS_IPV6(&pp->srcadr)) { |
678 | 0 | if (!client_v6_capable) |
679 | 0 | continue; |
680 | 0 | ip->addr6 = SOCK_ADDR6(&pp->srcadr); |
681 | 0 | ip->v6_flag = 1; |
682 | 0 | } else { |
683 | 0 | ip->addr = NSRCADR(&pp->srcadr); |
684 | 0 | if (client_v6_capable) |
685 | 0 | ip->v6_flag = 0; |
686 | 0 | } |
687 | | |
688 | 0 | ip->port = NSRCPORT(&pp->srcadr); |
689 | 0 | ip->hmode = pp->hmode; |
690 | 0 | ip->flags = 0; |
691 | 0 | if (pp->flags & FLAG_CONFIG) |
692 | 0 | ip->flags |= INFO_FLAG_CONFIG; |
693 | 0 | if (pp == sys_peer) |
694 | 0 | ip->flags |= INFO_FLAG_SYSPEER; |
695 | 0 | if (pp->status == CTL_PST_SEL_SYNCCAND) |
696 | 0 | ip->flags |= INFO_FLAG_SEL_CANDIDATE; |
697 | 0 | if (pp->status >= CTL_PST_SEL_SYSPEER) |
698 | 0 | ip->flags |= INFO_FLAG_SHORTLIST; |
699 | 0 | ip = (struct info_peer_list *)more_pkt(); |
700 | 0 | } /* for pp */ |
701 | |
|
702 | 0 | flush_pkt(); |
703 | 0 | } |
704 | | |
705 | | |
706 | | /* |
707 | | * list_peers_sum - return extended peer list |
708 | | */ |
709 | | static void |
710 | | list_peers_sum( |
711 | | sockaddr_u *srcadr, |
712 | | endpt *inter, |
713 | | struct req_pkt *inpkt |
714 | | ) |
715 | 0 | { |
716 | 0 | struct info_peer_summary * ips; |
717 | 0 | const struct peer * pp; |
718 | 0 | l_fp ltmp; |
719 | |
|
720 | 0 | DPRINTF(3, ("wants peer list summary\n")); |
721 | |
|
722 | 0 | ips = (struct info_peer_summary *)prepare_pkt(srcadr, inter, inpkt, |
723 | 0 | v6sizeof(struct info_peer_summary)); |
724 | 0 | for (pp = peer_list; pp != NULL && ips != NULL; pp = pp->p_link) { |
725 | 0 | DPRINTF(4, ("sum: got one\n")); |
726 | | /* |
727 | | * Be careful here not to return v6 peers when we |
728 | | * want only v4. |
729 | | */ |
730 | 0 | if (IS_IPV6(&pp->srcadr)) { |
731 | 0 | if (!client_v6_capable) |
732 | 0 | continue; |
733 | 0 | ips->srcadr6 = SOCK_ADDR6(&pp->srcadr); |
734 | 0 | ips->v6_flag = 1; |
735 | 0 | if (pp->dstadr) |
736 | 0 | ips->dstadr6 = SOCK_ADDR6(&pp->dstadr->sin); |
737 | 0 | else |
738 | 0 | ZERO(ips->dstadr6); |
739 | 0 | } else { |
740 | 0 | ips->srcadr = NSRCADR(&pp->srcadr); |
741 | 0 | if (client_v6_capable) |
742 | 0 | ips->v6_flag = 0; |
743 | | |
744 | 0 | if (pp->dstadr) { |
745 | 0 | if (!pp->processed) |
746 | 0 | ips->dstadr = NSRCADR(&pp->dstadr->sin); |
747 | 0 | else { |
748 | 0 | if (MDF_BCAST == pp->cast_flags) |
749 | 0 | ips->dstadr = NSRCADR(&pp->dstadr->bcast); |
750 | 0 | else if (pp->cast_flags) { |
751 | 0 | ips->dstadr = NSRCADR(&pp->dstadr->sin); |
752 | 0 | if (!ips->dstadr) |
753 | 0 | ips->dstadr = NSRCADR(&pp->dstadr->bcast); |
754 | 0 | } |
755 | 0 | } |
756 | 0 | } else { |
757 | 0 | ips->dstadr = 0; |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | 0 | ips->srcport = NSRCPORT(&pp->srcadr); |
762 | 0 | ips->stratum = pp->stratum; |
763 | 0 | ips->hpoll = pp->hpoll; |
764 | 0 | ips->ppoll = pp->ppoll; |
765 | 0 | ips->reach = pp->reach; |
766 | 0 | ips->flags = 0; |
767 | 0 | if (pp == sys_peer) |
768 | 0 | ips->flags |= INFO_FLAG_SYSPEER; |
769 | 0 | if (pp->flags & FLAG_CONFIG) |
770 | 0 | ips->flags |= INFO_FLAG_CONFIG; |
771 | 0 | if (pp->flags & FLAG_REFCLOCK) |
772 | 0 | ips->flags |= INFO_FLAG_REFCLOCK; |
773 | 0 | if (pp->flags & FLAG_PREFER) |
774 | 0 | ips->flags |= INFO_FLAG_PREFER; |
775 | 0 | if (pp->flags & FLAG_BURST) |
776 | 0 | ips->flags |= INFO_FLAG_BURST; |
777 | 0 | if (pp->status == CTL_PST_SEL_SYNCCAND) |
778 | 0 | ips->flags |= INFO_FLAG_SEL_CANDIDATE; |
779 | 0 | if (pp->status >= CTL_PST_SEL_SYSPEER) |
780 | 0 | ips->flags |= INFO_FLAG_SHORTLIST; |
781 | 0 | ips->hmode = pp->hmode; |
782 | 0 | ips->delay = HTONS_FP(DTOFP(pp->delay)); |
783 | 0 | DTOLFP(pp->offset, <mp); |
784 | 0 | HTONL_FP(<mp, &ips->offset); |
785 | 0 | ips->dispersion = HTONS_FP(DTOUFP(SQRT(pp->disp))); |
786 | |
|
787 | 0 | ips = (struct info_peer_summary *)more_pkt(); |
788 | 0 | } /* for pp */ |
789 | |
|
790 | 0 | flush_pkt(); |
791 | 0 | } |
792 | | |
793 | | |
794 | | /* |
795 | | * peer_info - send information for one or more peers |
796 | | */ |
797 | | static void |
798 | | peer_info ( |
799 | | sockaddr_u *srcadr, |
800 | | endpt *inter, |
801 | | struct req_pkt *inpkt |
802 | | ) |
803 | 0 | { |
804 | 0 | u_short items; |
805 | 0 | size_t item_sz; |
806 | 0 | char * datap; |
807 | 0 | struct info_peer_list ipl; |
808 | 0 | struct peer * pp; |
809 | 0 | struct info_peer * ip; |
810 | 0 | int i; |
811 | 0 | int j; |
812 | 0 | sockaddr_u addr; |
813 | 0 | l_fp ltmp; |
814 | |
|
815 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
816 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
817 | 0 | datap = inpkt->u.data; |
818 | 0 | if (item_sz != sizeof(ipl)) { |
819 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
820 | 0 | return; |
821 | 0 | } |
822 | 0 | ip = prepare_pkt(srcadr, inter, inpkt, |
823 | 0 | v6sizeof(struct info_peer)); |
824 | 0 | while (items-- > 0 && ip != NULL) { |
825 | 0 | ZERO(ipl); |
826 | 0 | memcpy(&ipl, datap, item_sz); |
827 | 0 | ZERO_SOCK(&addr); |
828 | 0 | NSRCPORT(&addr) = ipl.port; |
829 | 0 | if (client_v6_capable && ipl.v6_flag) { |
830 | 0 | AF(&addr) = AF_INET6; |
831 | 0 | SOCK_ADDR6(&addr) = ipl.addr6; |
832 | 0 | } else { |
833 | 0 | AF(&addr) = AF_INET; |
834 | 0 | NSRCADR(&addr) = ipl.addr; |
835 | 0 | } |
836 | | #ifdef ISC_PLATFORM_HAVESALEN |
837 | | addr.sa.sa_len = SOCKLEN(&addr); |
838 | | #endif |
839 | 0 | datap += item_sz; |
840 | |
|
841 | 0 | pp = findexistingpeer(&addr, NULL, NULL, -1, 0, NULL); |
842 | 0 | if (NULL == pp) |
843 | 0 | continue; |
844 | 0 | if (IS_IPV6(srcadr)) { |
845 | 0 | if (pp->dstadr) |
846 | 0 | ip->dstadr6 = |
847 | 0 | (MDF_BCAST == pp->cast_flags) |
848 | 0 | ? SOCK_ADDR6(&pp->dstadr->bcast) |
849 | 0 | : SOCK_ADDR6(&pp->dstadr->sin); |
850 | 0 | else |
851 | 0 | ZERO(ip->dstadr6); |
852 | |
|
853 | 0 | ip->srcadr6 = SOCK_ADDR6(&pp->srcadr); |
854 | 0 | ip->v6_flag = 1; |
855 | 0 | } else { |
856 | 0 | if (pp->dstadr) { |
857 | 0 | if (!pp->processed) |
858 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->sin); |
859 | 0 | else { |
860 | 0 | if (MDF_BCAST == pp->cast_flags) |
861 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->bcast); |
862 | 0 | else if (pp->cast_flags) { |
863 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->sin); |
864 | 0 | if (!ip->dstadr) |
865 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->bcast); |
866 | 0 | } |
867 | 0 | } |
868 | 0 | } else |
869 | 0 | ip->dstadr = 0; |
870 | |
|
871 | 0 | ip->srcadr = NSRCADR(&pp->srcadr); |
872 | 0 | if (client_v6_capable) |
873 | 0 | ip->v6_flag = 0; |
874 | 0 | } |
875 | 0 | ip->srcport = NSRCPORT(&pp->srcadr); |
876 | 0 | ip->flags = 0; |
877 | 0 | if (pp == sys_peer) |
878 | 0 | ip->flags |= INFO_FLAG_SYSPEER; |
879 | 0 | if (pp->flags & FLAG_CONFIG) |
880 | 0 | ip->flags |= INFO_FLAG_CONFIG; |
881 | 0 | if (pp->flags & FLAG_REFCLOCK) |
882 | 0 | ip->flags |= INFO_FLAG_REFCLOCK; |
883 | 0 | if (pp->flags & FLAG_PREFER) |
884 | 0 | ip->flags |= INFO_FLAG_PREFER; |
885 | 0 | if (pp->flags & FLAG_BURST) |
886 | 0 | ip->flags |= INFO_FLAG_BURST; |
887 | 0 | if (pp->status == CTL_PST_SEL_SYNCCAND) |
888 | 0 | ip->flags |= INFO_FLAG_SEL_CANDIDATE; |
889 | 0 | if (pp->status >= CTL_PST_SEL_SYSPEER) |
890 | 0 | ip->flags |= INFO_FLAG_SHORTLIST; |
891 | 0 | ip->leap = pp->leap; |
892 | 0 | ip->hmode = pp->hmode; |
893 | 0 | ip->pmode = pp->pmode; |
894 | 0 | ip->keyid = pp->keyid; |
895 | 0 | ip->stratum = pp->stratum; |
896 | 0 | ip->ppoll = pp->ppoll; |
897 | 0 | ip->hpoll = pp->hpoll; |
898 | 0 | ip->precision = pp->precision; |
899 | 0 | ip->version = pp->version; |
900 | 0 | ip->reach = pp->reach; |
901 | 0 | ip->unreach = (u_char)pp->unreach; |
902 | 0 | ip->flash = (u_char)pp->flash; |
903 | 0 | ip->flash2 = (u_short)pp->flash; |
904 | 0 | ip->estbdelay = HTONS_FP(DTOFP(pp->delay)); |
905 | 0 | ip->ttl = (u_char)pp->ttl; |
906 | 0 | ip->associd = htons(pp->associd); |
907 | 0 | ip->rootdelay = HTONS_FP(DTOUFP(pp->rootdelay)); |
908 | 0 | ip->rootdispersion = HTONS_FP(DTOUFP(pp->rootdisp)); |
909 | 0 | ip->refid = pp->refid; |
910 | 0 | HTONL_FP(&pp->reftime, &ip->reftime); |
911 | 0 | HTONL_FP(&pp->aorg, &ip->org); |
912 | 0 | HTONL_FP(&pp->rec, &ip->rec); |
913 | 0 | HTONL_FP(&pp->xmt, &ip->xmt); |
914 | 0 | j = pp->filter_nextpt - 1; |
915 | 0 | for (i = 0; i < NTP_SHIFT; i++, j--) { |
916 | 0 | if (j < 0) |
917 | 0 | j = NTP_SHIFT-1; |
918 | 0 | ip->filtdelay[i] = HTONS_FP(DTOFP(pp->filter_delay[j])); |
919 | 0 | DTOLFP(pp->filter_offset[j], <mp); |
920 | 0 | HTONL_FP(<mp, &ip->filtoffset[i]); |
921 | 0 | ip->order[i] = (u_char)((pp->filter_nextpt + |
922 | 0 | NTP_SHIFT - 1) - |
923 | 0 | pp->filter_order[i]); |
924 | 0 | if (ip->order[i] >= NTP_SHIFT) |
925 | 0 | ip->order[i] -= NTP_SHIFT; |
926 | 0 | } |
927 | 0 | DTOLFP(pp->offset, <mp); |
928 | 0 | HTONL_FP(<mp, &ip->offset); |
929 | 0 | ip->delay = HTONS_FP(DTOFP(pp->delay)); |
930 | 0 | ip->dispersion = HTONS_FP(DTOUFP(SQRT(pp->disp))); |
931 | 0 | ip->selectdisp = HTONS_FP(DTOUFP(SQRT(pp->jitter))); |
932 | 0 | ip = more_pkt(); |
933 | 0 | } |
934 | 0 | flush_pkt(); |
935 | 0 | } |
936 | | |
937 | | |
938 | | /* |
939 | | * peer_stats - send statistics for one or more peers |
940 | | */ |
941 | | static void |
942 | | peer_stats ( |
943 | | sockaddr_u *srcadr, |
944 | | endpt *inter, |
945 | | struct req_pkt *inpkt |
946 | | ) |
947 | 0 | { |
948 | 0 | u_short items; |
949 | 0 | size_t item_sz; |
950 | 0 | char * datap; |
951 | 0 | struct info_peer_list ipl; |
952 | 0 | struct peer * pp; |
953 | 0 | struct info_peer_stats *ip; |
954 | 0 | sockaddr_u addr; |
955 | |
|
956 | 0 | DPRINTF(1, ("peer_stats: called\n")); |
957 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
958 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
959 | 0 | datap = inpkt->u.data; |
960 | 0 | if (item_sz > sizeof(ipl)) { |
961 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
962 | 0 | return; |
963 | 0 | } |
964 | 0 | ip = prepare_pkt(srcadr, inter, inpkt, |
965 | 0 | v6sizeof(struct info_peer_stats)); |
966 | 0 | while (items-- > 0 && ip != NULL) { |
967 | 0 | ZERO(ipl); |
968 | 0 | memcpy(&ipl, datap, item_sz); |
969 | 0 | ZERO(addr); |
970 | 0 | NSRCPORT(&addr) = ipl.port; |
971 | 0 | if (client_v6_capable && ipl.v6_flag) { |
972 | 0 | AF(&addr) = AF_INET6; |
973 | 0 | SOCK_ADDR6(&addr) = ipl.addr6; |
974 | 0 | } else { |
975 | 0 | AF(&addr) = AF_INET; |
976 | 0 | NSRCADR(&addr) = ipl.addr; |
977 | 0 | } |
978 | | #ifdef ISC_PLATFORM_HAVESALEN |
979 | | addr.sa.sa_len = SOCKLEN(&addr); |
980 | | #endif |
981 | 0 | DPRINTF(1, ("peer_stats: looking for %s, %d, %d\n", |
982 | 0 | stoa(&addr), ipl.port, NSRCPORT(&addr))); |
983 | |
|
984 | 0 | datap += item_sz; |
985 | |
|
986 | 0 | pp = findexistingpeer(&addr, NULL, NULL, -1, 0, NULL); |
987 | 0 | if (NULL == pp) |
988 | 0 | continue; |
989 | | |
990 | 0 | DPRINTF(1, ("peer_stats: found %s\n", stoa(&addr))); |
991 | |
|
992 | 0 | if (IS_IPV4(&pp->srcadr)) { |
993 | 0 | if (pp->dstadr) { |
994 | 0 | if (!pp->processed) |
995 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->sin); |
996 | 0 | else { |
997 | 0 | if (MDF_BCAST == pp->cast_flags) |
998 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->bcast); |
999 | 0 | else if (pp->cast_flags) { |
1000 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->sin); |
1001 | 0 | if (!ip->dstadr) |
1002 | 0 | ip->dstadr = NSRCADR(&pp->dstadr->bcast); |
1003 | 0 | } |
1004 | 0 | } |
1005 | 0 | } else |
1006 | 0 | ip->dstadr = 0; |
1007 | | |
1008 | 0 | ip->srcadr = NSRCADR(&pp->srcadr); |
1009 | 0 | if (client_v6_capable) |
1010 | 0 | ip->v6_flag = 0; |
1011 | 0 | } else { |
1012 | 0 | if (pp->dstadr) |
1013 | 0 | ip->dstadr6 = |
1014 | 0 | (MDF_BCAST == pp->cast_flags) |
1015 | 0 | ? SOCK_ADDR6(&pp->dstadr->bcast) |
1016 | 0 | : SOCK_ADDR6(&pp->dstadr->sin); |
1017 | 0 | else |
1018 | 0 | ZERO(ip->dstadr6); |
1019 | |
|
1020 | 0 | ip->srcadr6 = SOCK_ADDR6(&pp->srcadr); |
1021 | 0 | ip->v6_flag = 1; |
1022 | 0 | } |
1023 | 0 | ip->srcport = NSRCPORT(&pp->srcadr); |
1024 | 0 | ip->flags = 0; |
1025 | 0 | if (pp == sys_peer) |
1026 | 0 | ip->flags |= INFO_FLAG_SYSPEER; |
1027 | 0 | if (pp->flags & FLAG_CONFIG) |
1028 | 0 | ip->flags |= INFO_FLAG_CONFIG; |
1029 | 0 | if (pp->flags & FLAG_REFCLOCK) |
1030 | 0 | ip->flags |= INFO_FLAG_REFCLOCK; |
1031 | 0 | if (pp->flags & FLAG_PREFER) |
1032 | 0 | ip->flags |= INFO_FLAG_PREFER; |
1033 | 0 | if (pp->flags & FLAG_BURST) |
1034 | 0 | ip->flags |= INFO_FLAG_BURST; |
1035 | 0 | if (pp->flags & FLAG_IBURST) |
1036 | 0 | ip->flags |= INFO_FLAG_IBURST; |
1037 | 0 | if (pp->status == CTL_PST_SEL_SYNCCAND) |
1038 | 0 | ip->flags |= INFO_FLAG_SEL_CANDIDATE; |
1039 | 0 | if (pp->status >= CTL_PST_SEL_SYSPEER) |
1040 | 0 | ip->flags |= INFO_FLAG_SHORTLIST; |
1041 | 0 | ip->flags = htons(ip->flags); |
1042 | 0 | ip->timereceived = htonl((u_int32)(current_time - pp->timereceived)); |
1043 | 0 | ip->timetosend = htonl(pp->nextdate - current_time); |
1044 | 0 | ip->timereachable = htonl((u_int32)(current_time - pp->timereachable)); |
1045 | 0 | ip->sent = htonl((u_int32)(pp->sent)); |
1046 | 0 | ip->processed = htonl((u_int32)(pp->processed)); |
1047 | 0 | ip->badauth = htonl((u_int32)(pp->badauth)); |
1048 | 0 | ip->bogusorg = htonl((u_int32)(pp->bogusorg)); |
1049 | 0 | ip->oldpkt = htonl((u_int32)(pp->oldpkt)); |
1050 | 0 | ip->seldisp = htonl((u_int32)(pp->seldisptoolarge)); |
1051 | 0 | ip->selbroken = htonl((u_int32)(pp->selbroken)); |
1052 | 0 | ip->candidate = pp->status; |
1053 | 0 | ip = (struct info_peer_stats *)more_pkt(); |
1054 | 0 | } |
1055 | 0 | flush_pkt(); |
1056 | 0 | } |
1057 | | |
1058 | | |
1059 | | /* |
1060 | | * sys_info - return system info |
1061 | | */ |
1062 | | static void |
1063 | | sys_info( |
1064 | | sockaddr_u *srcadr, |
1065 | | endpt *inter, |
1066 | | struct req_pkt *inpkt |
1067 | | ) |
1068 | 0 | { |
1069 | 0 | register struct info_sys *is; |
1070 | |
|
1071 | 0 | is = (struct info_sys *)prepare_pkt(srcadr, inter, inpkt, |
1072 | 0 | v6sizeof(struct info_sys)); |
1073 | |
|
1074 | 0 | if (sys_peer) { |
1075 | 0 | if (IS_IPV4(&sys_peer->srcadr)) { |
1076 | 0 | is->peer = NSRCADR(&sys_peer->srcadr); |
1077 | 0 | if (client_v6_capable) |
1078 | 0 | is->v6_flag = 0; |
1079 | 0 | } else if (client_v6_capable) { |
1080 | 0 | is->peer6 = SOCK_ADDR6(&sys_peer->srcadr); |
1081 | 0 | is->v6_flag = 1; |
1082 | 0 | } |
1083 | 0 | is->peer_mode = sys_peer->hmode; |
1084 | 0 | } else { |
1085 | 0 | is->peer = 0; |
1086 | 0 | if (client_v6_capable) { |
1087 | 0 | is->v6_flag = 0; |
1088 | 0 | } |
1089 | 0 | is->peer_mode = 0; |
1090 | 0 | } |
1091 | |
|
1092 | 0 | is->leap = sys_leap; |
1093 | 0 | is->stratum = sys_stratum; |
1094 | 0 | is->precision = sys_precision; |
1095 | 0 | is->rootdelay = htonl(DTOFP(sys_rootdelay)); |
1096 | 0 | is->rootdispersion = htonl(DTOUFP(sys_rootdisp)); |
1097 | 0 | is->frequency = htonl(DTOFP(sys_jitter)); |
1098 | 0 | is->stability = htonl(DTOUFP(clock_stability * 1e6)); |
1099 | 0 | is->refid = sys_refid; |
1100 | 0 | HTONL_FP(&sys_reftime, &is->reftime); |
1101 | |
|
1102 | 0 | is->poll = sys_poll; |
1103 | | |
1104 | 0 | is->flags = 0; |
1105 | 0 | if (sys_authenticate) |
1106 | 0 | is->flags |= INFO_FLAG_AUTHENTICATE; |
1107 | 0 | if (sys_bclient) |
1108 | 0 | is->flags |= INFO_FLAG_BCLIENT; |
1109 | 0 | #ifdef REFCLOCK |
1110 | 0 | if (cal_enable) |
1111 | 0 | is->flags |= INFO_FLAG_CAL; |
1112 | 0 | #endif /* REFCLOCK */ |
1113 | 0 | if (kern_enable) |
1114 | 0 | is->flags |= INFO_FLAG_KERNEL; |
1115 | 0 | if (mon_enabled != MON_OFF) |
1116 | 0 | is->flags |= INFO_FLAG_MONITOR; |
1117 | 0 | if (ntp_enable) |
1118 | 0 | is->flags |= INFO_FLAG_NTP; |
1119 | 0 | if (hardpps_enable) |
1120 | 0 | is->flags |= INFO_FLAG_PPS_SYNC; |
1121 | 0 | if (stats_control) |
1122 | 0 | is->flags |= INFO_FLAG_FILEGEN; |
1123 | 0 | is->bdelay = HTONS_FP(DTOFP(sys_bdelay)); |
1124 | 0 | HTONL_UF(sys_authdelay.l_uf, &is->authdelay); |
1125 | 0 | (void) more_pkt(); |
1126 | 0 | flush_pkt(); |
1127 | 0 | } |
1128 | | |
1129 | | |
1130 | | /* |
1131 | | * sys_stats - return system statistics |
1132 | | */ |
1133 | | static void |
1134 | | sys_stats( |
1135 | | sockaddr_u *srcadr, |
1136 | | endpt *inter, |
1137 | | struct req_pkt *inpkt |
1138 | | ) |
1139 | 0 | { |
1140 | 0 | register struct info_sys_stats *ss; |
1141 | |
|
1142 | 0 | ss = (struct info_sys_stats *)prepare_pkt(srcadr, inter, inpkt, |
1143 | 0 | sizeof(struct info_sys_stats)); |
1144 | 0 | ss->timeup = htonl((u_int32)current_time); |
1145 | 0 | ss->timereset = htonl((u_int32)(current_time - sys_stattime)); |
1146 | 0 | ss->denied = htonl((u_int32)sys_restricted); |
1147 | 0 | ss->oldversionpkt = htonl((u_int32)sys_oldversion); |
1148 | 0 | ss->newversionpkt = htonl((u_int32)sys_newversion); |
1149 | 0 | ss->unknownversion = htonl((u_int32)sys_declined); |
1150 | 0 | ss->badlength = htonl((u_int32)sys_badlength); |
1151 | 0 | ss->processed = htonl((u_int32)sys_processed); |
1152 | 0 | ss->badauth = htonl((u_int32)sys_badauth); |
1153 | 0 | ss->limitrejected = htonl((u_int32)sys_limitrejected); |
1154 | 0 | ss->received = htonl((u_int32)sys_received); |
1155 | 0 | ss->lamport = htonl((u_int32)sys_lamport); |
1156 | 0 | ss->tsrounding = htonl((u_int32)sys_tsrounding); |
1157 | 0 | (void) more_pkt(); |
1158 | 0 | flush_pkt(); |
1159 | 0 | } |
1160 | | |
1161 | | |
1162 | | /* |
1163 | | * mem_stats - return memory statistics |
1164 | | */ |
1165 | | static void |
1166 | | mem_stats( |
1167 | | sockaddr_u *srcadr, |
1168 | | endpt *inter, |
1169 | | struct req_pkt *inpkt |
1170 | | ) |
1171 | 0 | { |
1172 | 0 | register struct info_mem_stats *ms; |
1173 | 0 | register int i; |
1174 | |
|
1175 | 0 | ms = (struct info_mem_stats *)prepare_pkt(srcadr, inter, inpkt, |
1176 | 0 | sizeof(struct info_mem_stats)); |
1177 | |
|
1178 | 0 | ms->timereset = htonl((u_int32)(current_time - peer_timereset)); |
1179 | 0 | ms->totalpeermem = htons((u_short)total_peer_structs); |
1180 | 0 | ms->freepeermem = htons((u_short)peer_free_count); |
1181 | 0 | ms->findpeer_calls = htonl((u_int32)findpeer_calls); |
1182 | 0 | ms->allocations = htonl((u_int32)peer_allocations); |
1183 | 0 | ms->demobilizations = htonl((u_int32)peer_demobilizations); |
1184 | |
|
1185 | 0 | for (i = 0; i < NTP_HASH_SIZE; i++) |
1186 | 0 | ms->hashcount[i] = (u_char) |
1187 | 0 | max((u_int)peer_hash_count[i], UCHAR_MAX); |
1188 | |
|
1189 | 0 | (void) more_pkt(); |
1190 | 0 | flush_pkt(); |
1191 | 0 | } |
1192 | | |
1193 | | |
1194 | | /* |
1195 | | * io_stats - return io statistics |
1196 | | */ |
1197 | | static void |
1198 | | io_stats( |
1199 | | sockaddr_u *srcadr, |
1200 | | endpt *inter, |
1201 | | struct req_pkt *inpkt |
1202 | | ) |
1203 | 0 | { |
1204 | 0 | struct info_io_stats *io; |
1205 | |
|
1206 | 0 | io = (struct info_io_stats *)prepare_pkt(srcadr, inter, inpkt, |
1207 | 0 | sizeof(struct info_io_stats)); |
1208 | |
|
1209 | 0 | io->timereset = htonl((u_int32)(current_time - io_timereset)); |
1210 | 0 | io->totalrecvbufs = htons((u_short) total_recvbuffs()); |
1211 | 0 | io->freerecvbufs = htons((u_short) free_recvbuffs()); |
1212 | 0 | io->fullrecvbufs = htons((u_short) full_recvbuffs()); |
1213 | 0 | io->lowwater = htons((u_short) lowater_additions()); |
1214 | 0 | io->dropped = htonl((u_int32)packets_dropped); |
1215 | 0 | io->ignored = htonl((u_int32)packets_ignored); |
1216 | 0 | io->received = htonl((u_int32)packets_received); |
1217 | 0 | io->sent = htonl((u_int32)packets_sent); |
1218 | 0 | io->notsent = htonl((u_int32)packets_notsent); |
1219 | 0 | io->interrupts = htonl((u_int32)handler_calls); |
1220 | 0 | io->int_received = htonl((u_int32)handler_pkts); |
1221 | |
|
1222 | 0 | (void) more_pkt(); |
1223 | 0 | flush_pkt(); |
1224 | 0 | } |
1225 | | |
1226 | | |
1227 | | /* |
1228 | | * timer_stats - return timer statistics |
1229 | | */ |
1230 | | static void |
1231 | | timer_stats( |
1232 | | sockaddr_u * srcadr, |
1233 | | endpt * inter, |
1234 | | struct req_pkt * inpkt |
1235 | | ) |
1236 | 0 | { |
1237 | 0 | struct info_timer_stats * ts; |
1238 | 0 | u_long sincereset; |
1239 | |
|
1240 | 0 | ts = (struct info_timer_stats *)prepare_pkt(srcadr, inter, |
1241 | 0 | inpkt, sizeof(*ts)); |
1242 | |
|
1243 | 0 | sincereset = current_time - timer_timereset; |
1244 | 0 | ts->timereset = htonl((u_int32)sincereset); |
1245 | 0 | ts->alarms = ts->timereset; |
1246 | 0 | ts->overflows = htonl((u_int32)alarm_overflow); |
1247 | 0 | ts->xmtcalls = htonl((u_int32)timer_xmtcalls); |
1248 | |
|
1249 | 0 | (void) more_pkt(); |
1250 | 0 | flush_pkt(); |
1251 | 0 | } |
1252 | | |
1253 | | |
1254 | | /* |
1255 | | * loop_info - return the current state of the loop filter |
1256 | | */ |
1257 | | static void |
1258 | | loop_info( |
1259 | | sockaddr_u *srcadr, |
1260 | | endpt *inter, |
1261 | | struct req_pkt *inpkt |
1262 | | ) |
1263 | 0 | { |
1264 | 0 | struct info_loop *li; |
1265 | 0 | l_fp ltmp; |
1266 | |
|
1267 | 0 | li = (struct info_loop *)prepare_pkt(srcadr, inter, inpkt, |
1268 | 0 | sizeof(struct info_loop)); |
1269 | |
|
1270 | 0 | DTOLFP(last_offset, <mp); |
1271 | 0 | HTONL_FP(<mp, &li->last_offset); |
1272 | 0 | DTOLFP(drift_comp * 1e6, <mp); |
1273 | 0 | HTONL_FP(<mp, &li->drift_comp); |
1274 | 0 | li->compliance = htonl((u_int32)(tc_counter)); |
1275 | 0 | li->watchdog_timer = htonl((u_int32)(current_time - sys_epoch)); |
1276 | |
|
1277 | 0 | (void) more_pkt(); |
1278 | 0 | flush_pkt(); |
1279 | 0 | } |
1280 | | |
1281 | | |
1282 | | /* |
1283 | | * do_conf - add a peer to the configuration list |
1284 | | */ |
1285 | | static void |
1286 | | do_conf( |
1287 | | sockaddr_u *srcadr, |
1288 | | endpt *inter, |
1289 | | struct req_pkt *inpkt |
1290 | | ) |
1291 | 0 | { |
1292 | 0 | u_short items; |
1293 | 0 | size_t item_sz; |
1294 | 0 | u_int fl; |
1295 | 0 | char * datap; |
1296 | 0 | struct conf_peer temp_cp; |
1297 | 0 | sockaddr_u peeraddr; |
1298 | | |
1299 | | /* |
1300 | | * Do a check of everything to see that it looks |
1301 | | * okay. If not, complain about it. Note we are |
1302 | | * very picky here. |
1303 | | */ |
1304 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
1305 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
1306 | 0 | datap = inpkt->u.data; |
1307 | 0 | if (item_sz > sizeof(temp_cp)) { |
1308 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1309 | 0 | return; |
1310 | 0 | } |
1311 | | |
1312 | 0 | while (items-- > 0) { |
1313 | 0 | ZERO(temp_cp); |
1314 | 0 | memcpy(&temp_cp, datap, item_sz); |
1315 | 0 | ZERO_SOCK(&peeraddr); |
1316 | |
|
1317 | 0 | fl = 0; |
1318 | 0 | if (temp_cp.flags & CONF_FLAG_PREFER) |
1319 | 0 | fl |= FLAG_PREFER; |
1320 | 0 | if (temp_cp.flags & CONF_FLAG_BURST) |
1321 | 0 | fl |= FLAG_BURST; |
1322 | 0 | if (temp_cp.flags & CONF_FLAG_IBURST) |
1323 | 0 | fl |= FLAG_IBURST; |
1324 | | #ifdef AUTOKEY |
1325 | | if (temp_cp.flags & CONF_FLAG_SKEY) |
1326 | | fl |= FLAG_SKEY; |
1327 | | #endif /* AUTOKEY */ |
1328 | 0 | if (client_v6_capable && temp_cp.v6_flag) { |
1329 | 0 | AF(&peeraddr) = AF_INET6; |
1330 | 0 | SOCK_ADDR6(&peeraddr) = temp_cp.peeraddr6; |
1331 | 0 | } else { |
1332 | 0 | AF(&peeraddr) = AF_INET; |
1333 | 0 | NSRCADR(&peeraddr) = temp_cp.peeraddr; |
1334 | | /* |
1335 | | * Make sure the address is valid |
1336 | | */ |
1337 | 0 | if (!ISREFCLOCKADR(&peeraddr) && |
1338 | 0 | ISBADADR(&peeraddr)) { |
1339 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1340 | 0 | return; |
1341 | 0 | } |
1342 | |
|
1343 | 0 | } |
1344 | 0 | NSRCPORT(&peeraddr) = htons(NTP_PORT); |
1345 | | #ifdef ISC_PLATFORM_HAVESALEN |
1346 | | peeraddr.sa.sa_len = SOCKLEN(&peeraddr); |
1347 | | #endif |
1348 | | |
1349 | | /* check mode value: 0 <= hmode <= 6 |
1350 | | * |
1351 | | * There's no good global define for that limit, and |
1352 | | * using a magic define is as good (or bad, actually) as |
1353 | | * a magic number. So we use the highest possible peer |
1354 | | * mode, and that is MODE_BCLIENT. |
1355 | | * |
1356 | | * [Bug 3009] claims that a problem occurs for hmode > 7, |
1357 | | * but the code in ntp_peer.c indicates trouble for any |
1358 | | * hmode > 6 ( --> MODE_BCLIENT). |
1359 | | */ |
1360 | 0 | if (temp_cp.hmode > MODE_BCLIENT) { |
1361 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1362 | 0 | return; |
1363 | 0 | } |
1364 | | |
1365 | | /* Any more checks on the values? Unchecked at this |
1366 | | * point: |
1367 | | * - version |
1368 | | * - ttl |
1369 | | * - keyid |
1370 | | * |
1371 | | * - minpoll/maxpoll, but they are treated properly |
1372 | | * for all cases internally. Checking not necessary. |
1373 | | * |
1374 | | * Note that we ignore any previously-specified ippeerlimit. |
1375 | | * If we're told to create the peer, we create the peer. |
1376 | | */ |
1377 | | |
1378 | | /* finally create the peer */ |
1379 | 0 | if (peer_config(&peeraddr, NULL, NULL, -1, |
1380 | 0 | temp_cp.hmode, temp_cp.version, temp_cp.minpoll, |
1381 | 0 | temp_cp.maxpoll, fl, temp_cp.ttl, temp_cp.keyid, |
1382 | 0 | NULL) == 0) |
1383 | 0 | { |
1384 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
1385 | 0 | return; |
1386 | 0 | } |
1387 | | |
1388 | 0 | datap += item_sz; |
1389 | 0 | } |
1390 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
1391 | 0 | } |
1392 | | |
1393 | | |
1394 | | /* |
1395 | | * do_unconf - remove a peer from the configuration list |
1396 | | */ |
1397 | | static void |
1398 | | do_unconf( |
1399 | | sockaddr_u * srcadr, |
1400 | | endpt * inter, |
1401 | | struct req_pkt *inpkt |
1402 | | ) |
1403 | 0 | { |
1404 | 0 | u_short items; |
1405 | 0 | size_t item_sz; |
1406 | 0 | char * datap; |
1407 | 0 | struct conf_unpeer temp_cp; |
1408 | 0 | struct peer * p; |
1409 | 0 | sockaddr_u peeraddr; |
1410 | 0 | int loops; |
1411 | | |
1412 | | /* |
1413 | | * This is a bit unstructured, but I like to be careful. |
1414 | | * We check to see that every peer exists and is actually |
1415 | | * configured. If so, we remove them. If not, we return |
1416 | | * an error. |
1417 | | * |
1418 | | * [Bug 3011] Even if we checked all peers given in the request |
1419 | | * in a dry run, there's still a chance that the caller played |
1420 | | * unfair and gave the same peer multiple times. So we still |
1421 | | * have to be prepared for nasty surprises in the second run ;) |
1422 | | */ |
1423 | | |
1424 | | /* basic consistency checks */ |
1425 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
1426 | 0 | if (item_sz > sizeof(temp_cp)) { |
1427 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1428 | 0 | return; |
1429 | 0 | } |
1430 | | |
1431 | | /* now do two runs: first a dry run, then a busy one */ |
1432 | 0 | for (loops = 0; loops != 2; ++loops) { |
1433 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
1434 | 0 | datap = inpkt->u.data; |
1435 | 0 | while (items-- > 0) { |
1436 | | /* copy from request to local */ |
1437 | 0 | ZERO(temp_cp); |
1438 | 0 | memcpy(&temp_cp, datap, item_sz); |
1439 | | /* get address structure */ |
1440 | 0 | ZERO_SOCK(&peeraddr); |
1441 | 0 | if (client_v6_capable && temp_cp.v6_flag) { |
1442 | 0 | AF(&peeraddr) = AF_INET6; |
1443 | 0 | SOCK_ADDR6(&peeraddr) = temp_cp.peeraddr6; |
1444 | 0 | } else { |
1445 | 0 | AF(&peeraddr) = AF_INET; |
1446 | 0 | NSRCADR(&peeraddr) = temp_cp.peeraddr; |
1447 | 0 | } |
1448 | 0 | SET_PORT(&peeraddr, NTP_PORT); |
1449 | | #ifdef ISC_PLATFORM_HAVESALEN |
1450 | | peeraddr.sa.sa_len = SOCKLEN(&peeraddr); |
1451 | | #endif |
1452 | 0 | DPRINTF(1, ("searching for %s\n", |
1453 | 0 | stoa(&peeraddr))); |
1454 | | |
1455 | | /* search for matching configred(!) peer */ |
1456 | 0 | p = NULL; |
1457 | 0 | do { |
1458 | 0 | p = findexistingpeer( |
1459 | 0 | &peeraddr, NULL, p, -1, 0, NULL); |
1460 | 0 | } while (p && !(FLAG_CONFIG & p->flags)); |
1461 | | |
1462 | 0 | if (!loops && !p) { |
1463 | | /* Item not found in dry run -- bail! */ |
1464 | 0 | req_ack(srcadr, inter, inpkt, |
1465 | 0 | INFO_ERR_NODATA); |
1466 | 0 | return; |
1467 | 0 | } else if (loops && p) { |
1468 | | /* Item found in busy run -- remove! */ |
1469 | 0 | peer_clear(p, "GONE"); |
1470 | 0 | unpeer(p); |
1471 | 0 | } |
1472 | 0 | datap += item_sz; |
1473 | 0 | } |
1474 | 0 | } |
1475 | | |
1476 | | /* report success */ |
1477 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
1478 | 0 | } |
1479 | | |
1480 | | |
1481 | | /* |
1482 | | * set_sys_flag - set system flags |
1483 | | */ |
1484 | | static void |
1485 | | set_sys_flag( |
1486 | | sockaddr_u *srcadr, |
1487 | | endpt *inter, |
1488 | | struct req_pkt *inpkt |
1489 | | ) |
1490 | 0 | { |
1491 | 0 | setclr_flags(srcadr, inter, inpkt, 1); |
1492 | 0 | } |
1493 | | |
1494 | | |
1495 | | /* |
1496 | | * clr_sys_flag - clear system flags |
1497 | | */ |
1498 | | static void |
1499 | | clr_sys_flag( |
1500 | | sockaddr_u *srcadr, |
1501 | | endpt *inter, |
1502 | | struct req_pkt *inpkt |
1503 | | ) |
1504 | 0 | { |
1505 | 0 | setclr_flags(srcadr, inter, inpkt, 0); |
1506 | 0 | } |
1507 | | |
1508 | | |
1509 | | /* |
1510 | | * setclr_flags - do the grunge work of flag setting/clearing |
1511 | | */ |
1512 | | static void |
1513 | | setclr_flags( |
1514 | | sockaddr_u *srcadr, |
1515 | | endpt *inter, |
1516 | | struct req_pkt *inpkt, |
1517 | | u_long set |
1518 | | ) |
1519 | 0 | { |
1520 | 0 | struct conf_sys_flags *sf; |
1521 | 0 | u_int32 flags; |
1522 | |
|
1523 | 0 | if (INFO_NITEMS(inpkt->err_nitems) > 1) { |
1524 | 0 | msyslog(LOG_ERR, "setclr_flags: err_nitems > 1"); |
1525 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1526 | 0 | return; |
1527 | 0 | } |
1528 | | |
1529 | 0 | sf = (struct conf_sys_flags *)&inpkt->u; |
1530 | 0 | flags = ntohl(sf->flags); |
1531 | | |
1532 | 0 | if (flags & ~(SYS_FLAG_BCLIENT | SYS_FLAG_PPS | |
1533 | 0 | SYS_FLAG_NTP | SYS_FLAG_KERNEL | SYS_FLAG_MONITOR | |
1534 | 0 | SYS_FLAG_FILEGEN | SYS_FLAG_AUTH | SYS_FLAG_CAL)) { |
1535 | 0 | msyslog(LOG_ERR, "setclr_flags: extra flags: %#x", |
1536 | 0 | flags & ~(SYS_FLAG_BCLIENT | SYS_FLAG_PPS | |
1537 | 0 | SYS_FLAG_NTP | SYS_FLAG_KERNEL | |
1538 | 0 | SYS_FLAG_MONITOR | SYS_FLAG_FILEGEN | |
1539 | 0 | SYS_FLAG_AUTH | SYS_FLAG_CAL)); |
1540 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1541 | 0 | return; |
1542 | 0 | } |
1543 | | |
1544 | 0 | if (flags & SYS_FLAG_BCLIENT) |
1545 | 0 | proto_config(PROTO_BROADCLIENT, set, 0., NULL); |
1546 | 0 | if (flags & SYS_FLAG_PPS) |
1547 | 0 | proto_config(PROTO_PPS, set, 0., NULL); |
1548 | 0 | if (flags & SYS_FLAG_NTP) |
1549 | 0 | proto_config(PROTO_NTP, set, 0., NULL); |
1550 | 0 | if (flags & SYS_FLAG_KERNEL) |
1551 | 0 | proto_config(PROTO_KERNEL, set, 0., NULL); |
1552 | 0 | if (flags & SYS_FLAG_MONITOR) |
1553 | 0 | proto_config(PROTO_MONITOR, set, 0., NULL); |
1554 | 0 | if (flags & SYS_FLAG_FILEGEN) |
1555 | 0 | proto_config(PROTO_FILEGEN, set, 0., NULL); |
1556 | 0 | if (flags & SYS_FLAG_AUTH) |
1557 | 0 | proto_config(PROTO_AUTHENTICATE, set, 0., NULL); |
1558 | 0 | if (flags & SYS_FLAG_CAL) |
1559 | 0 | proto_config(PROTO_CAL, set, 0., NULL); |
1560 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
1561 | 0 | } |
1562 | | |
1563 | | /* There have been some issues with the restrict list processing, |
1564 | | * ranging from problems with deep recursion (resulting in stack |
1565 | | * overflows) and overfull reply buffers. |
1566 | | * |
1567 | | * To avoid this trouble the list reversal is done iteratively using a |
1568 | | * scratch pad. |
1569 | | */ |
1570 | | typedef struct RestrictStack RestrictStackT; |
1571 | | struct RestrictStack { |
1572 | | RestrictStackT *link; |
1573 | | size_t fcnt; |
1574 | | const restrict_u *pres[63]; |
1575 | | }; |
1576 | | |
1577 | | static size_t |
1578 | | getStackSheetSize( |
1579 | | RestrictStackT *sp |
1580 | | ) |
1581 | 0 | { |
1582 | 0 | if (sp) |
1583 | 0 | return sizeof(sp->pres)/sizeof(sp->pres[0]); |
1584 | 0 | return 0u; |
1585 | 0 | } |
1586 | | |
1587 | | static int/*BOOL*/ |
1588 | | pushRestriction( |
1589 | | RestrictStackT **spp, |
1590 | | const restrict_u *ptr |
1591 | | ) |
1592 | 0 | { |
1593 | 0 | RestrictStackT *sp; |
1594 | |
|
1595 | 0 | if (NULL == (sp = *spp) || 0 == sp->fcnt) { |
1596 | | /* need another sheet in the scratch pad */ |
1597 | 0 | sp = emalloc(sizeof(*sp)); |
1598 | 0 | sp->link = *spp; |
1599 | 0 | sp->fcnt = getStackSheetSize(sp); |
1600 | 0 | *spp = sp; |
1601 | 0 | } |
1602 | 0 | sp->pres[--sp->fcnt] = ptr; |
1603 | 0 | return TRUE; |
1604 | 0 | } |
1605 | | |
1606 | | static int/*BOOL*/ |
1607 | | popRestriction( |
1608 | | RestrictStackT **spp, |
1609 | | const restrict_u **opp |
1610 | | ) |
1611 | 0 | { |
1612 | 0 | RestrictStackT *sp; |
1613 | |
|
1614 | 0 | if (NULL == (sp = *spp) || sp->fcnt >= getStackSheetSize(sp)) |
1615 | 0 | return FALSE; |
1616 | | |
1617 | 0 | *opp = sp->pres[sp->fcnt++]; |
1618 | 0 | if (sp->fcnt >= getStackSheetSize(sp)) { |
1619 | | /* discard sheet from scratch pad */ |
1620 | 0 | *spp = sp->link; |
1621 | 0 | free(sp); |
1622 | 0 | } |
1623 | 0 | return TRUE; |
1624 | 0 | } |
1625 | | |
1626 | | static void |
1627 | | flushRestrictionStack( |
1628 | | RestrictStackT **spp |
1629 | | ) |
1630 | 0 | { |
1631 | 0 | RestrictStackT *sp; |
1632 | |
|
1633 | 0 | while (NULL != (sp = *spp)) { |
1634 | 0 | *spp = sp->link; |
1635 | 0 | free(sp); |
1636 | 0 | } |
1637 | 0 | } |
1638 | | |
1639 | | /* |
1640 | | * list_restrict4 - iterative helper for list_restrict dumps IPv4 |
1641 | | * restriction list in reverse order. |
1642 | | */ |
1643 | | static void |
1644 | | list_restrict4( |
1645 | | const restrict_u * res, |
1646 | | struct info_restrict ** ppir |
1647 | | ) |
1648 | 0 | { |
1649 | 0 | RestrictStackT * rpad; |
1650 | 0 | struct info_restrict * pir; |
1651 | |
|
1652 | 0 | pir = *ppir; |
1653 | 0 | for (rpad = NULL; res; res = res->link) |
1654 | 0 | if (!pushRestriction(&rpad, res)) |
1655 | 0 | break; |
1656 | | |
1657 | 0 | while (pir && popRestriction(&rpad, &res)) { |
1658 | 0 | pir->addr = htonl(res->u.v4.addr); |
1659 | 0 | if (client_v6_capable) |
1660 | 0 | pir->v6_flag = 0; |
1661 | 0 | pir->mask = htonl(res->u.v4.mask); |
1662 | 0 | pir->count = htonl(res->count); |
1663 | 0 | pir->rflags = htons(res->rflags); |
1664 | 0 | pir->mflags = htons(res->mflags); |
1665 | 0 | pir = (struct info_restrict *)more_pkt(); |
1666 | 0 | } |
1667 | 0 | flushRestrictionStack(&rpad); |
1668 | 0 | *ppir = pir; |
1669 | 0 | } |
1670 | | |
1671 | | /* |
1672 | | * list_restrict6 - iterative helper for list_restrict dumps IPv6 |
1673 | | * restriction list in reverse order. |
1674 | | */ |
1675 | | static void |
1676 | | list_restrict6( |
1677 | | const restrict_u * res, |
1678 | | struct info_restrict ** ppir |
1679 | | ) |
1680 | 0 | { |
1681 | 0 | RestrictStackT * rpad; |
1682 | 0 | struct info_restrict * pir; |
1683 | |
|
1684 | 0 | pir = *ppir; |
1685 | 0 | for (rpad = NULL; res; res = res->link) |
1686 | 0 | if (!pushRestriction(&rpad, res)) |
1687 | 0 | break; |
1688 | |
|
1689 | 0 | while (pir && popRestriction(&rpad, &res)) { |
1690 | 0 | pir->addr6 = res->u.v6.addr; |
1691 | 0 | pir->mask6 = res->u.v6.mask; |
1692 | 0 | pir->v6_flag = 1; |
1693 | 0 | pir->count = htonl(res->count); |
1694 | 0 | pir->rflags = htons(res->rflags); |
1695 | 0 | pir->mflags = htons(res->mflags); |
1696 | 0 | pir = (struct info_restrict *)more_pkt(); |
1697 | 0 | } |
1698 | 0 | flushRestrictionStack(&rpad); |
1699 | 0 | *ppir = pir; |
1700 | 0 | } |
1701 | | |
1702 | | |
1703 | | /* |
1704 | | * list_restrict - return the restrict list |
1705 | | */ |
1706 | | static void |
1707 | | list_restrict( |
1708 | | sockaddr_u *srcadr, |
1709 | | endpt *inter, |
1710 | | struct req_pkt *inpkt |
1711 | | ) |
1712 | 0 | { |
1713 | 0 | struct info_restrict *ir; |
1714 | |
|
1715 | 0 | DPRINTF(3, ("wants restrict list summary\n")); |
1716 | |
|
1717 | 0 | ir = (struct info_restrict *)prepare_pkt(srcadr, inter, inpkt, |
1718 | 0 | v6sizeof(struct info_restrict)); |
1719 | | |
1720 | | /* |
1721 | | * The restriction lists are kept sorted in the reverse order |
1722 | | * than they were originally. To preserve the output semantics, |
1723 | | * dump each list in reverse order. The workers take care of that. |
1724 | | */ |
1725 | 0 | list_restrict4(restrictlist4, &ir); |
1726 | 0 | if (client_v6_capable) |
1727 | 0 | list_restrict6(restrictlist6, &ir); |
1728 | 0 | flush_pkt(); |
1729 | 0 | } |
1730 | | |
1731 | | |
1732 | | /* |
1733 | | * do_resaddflags - add flags to a restrict entry (or create one) |
1734 | | */ |
1735 | | static void |
1736 | | do_resaddflags( |
1737 | | sockaddr_u *srcadr, |
1738 | | endpt *inter, |
1739 | | struct req_pkt *inpkt |
1740 | | ) |
1741 | 0 | { |
1742 | 0 | do_restrict(srcadr, inter, inpkt, RESTRICT_FLAGS); |
1743 | 0 | } |
1744 | | |
1745 | | |
1746 | | |
1747 | | /* |
1748 | | * do_ressubflags - remove flags from a restrict entry |
1749 | | */ |
1750 | | static void |
1751 | | do_ressubflags( |
1752 | | sockaddr_u *srcadr, |
1753 | | endpt *inter, |
1754 | | struct req_pkt *inpkt |
1755 | | ) |
1756 | 0 | { |
1757 | 0 | do_restrict(srcadr, inter, inpkt, RESTRICT_UNFLAG); |
1758 | 0 | } |
1759 | | |
1760 | | |
1761 | | /* |
1762 | | * do_unrestrict - remove a restrict entry from the list |
1763 | | */ |
1764 | | static void |
1765 | | do_unrestrict( |
1766 | | sockaddr_u *srcadr, |
1767 | | endpt *inter, |
1768 | | struct req_pkt *inpkt |
1769 | | ) |
1770 | 0 | { |
1771 | 0 | do_restrict(srcadr, inter, inpkt, RESTRICT_REMOVE); |
1772 | 0 | } |
1773 | | |
1774 | | |
1775 | | /* |
1776 | | * do_restrict - do the dirty stuff of dealing with restrictions |
1777 | | */ |
1778 | | static void |
1779 | | do_restrict( |
1780 | | sockaddr_u *srcadr, |
1781 | | endpt *inter, |
1782 | | struct req_pkt *inpkt, |
1783 | | restrict_op op |
1784 | | ) |
1785 | 0 | { |
1786 | 0 | char * datap; |
1787 | 0 | struct conf_restrict cr; |
1788 | 0 | u_short items; |
1789 | 0 | size_t item_sz; |
1790 | 0 | sockaddr_u matchaddr; |
1791 | 0 | sockaddr_u matchmask; |
1792 | 0 | int bad; |
1793 | |
|
1794 | 0 | switch(op) { |
1795 | 0 | case RESTRICT_FLAGS: |
1796 | 0 | case RESTRICT_UNFLAG: |
1797 | 0 | case RESTRICT_REMOVE: |
1798 | 0 | case RESTRICT_REMOVEIF: |
1799 | 0 | break; |
1800 | | |
1801 | 0 | default: |
1802 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1803 | 0 | return; |
1804 | 0 | } |
1805 | | |
1806 | | /* |
1807 | | * Do a check of the flags to make sure that only |
1808 | | * the NTPPORT flag is set, if any. If not, complain |
1809 | | * about it. Note we are very picky here. |
1810 | | */ |
1811 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
1812 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
1813 | 0 | datap = inpkt->u.data; |
1814 | 0 | if (item_sz > sizeof(cr)) { |
1815 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1816 | 0 | return; |
1817 | 0 | } |
1818 | | |
1819 | 0 | bad = 0; |
1820 | 0 | while (items-- > 0 && !bad) { |
1821 | 0 | memcpy(&cr, datap, item_sz); |
1822 | 0 | cr.flags = ntohs(cr.flags); |
1823 | 0 | cr.mflags = ntohs(cr.mflags); |
1824 | 0 | if (~RESM_NTPONLY & cr.mflags) |
1825 | 0 | bad |= 1; |
1826 | 0 | if (~RES_ALLFLAGS & cr.flags) |
1827 | 0 | bad |= 2; |
1828 | 0 | if (INADDR_ANY != cr.mask) { |
1829 | 0 | if (client_v6_capable && cr.v6_flag) { |
1830 | 0 | if (IN6_IS_ADDR_UNSPECIFIED(&cr.addr6)) |
1831 | 0 | bad |= 4; |
1832 | 0 | } else { |
1833 | 0 | if (INADDR_ANY == cr.addr) |
1834 | 0 | bad |= 8; |
1835 | 0 | } |
1836 | 0 | } |
1837 | 0 | datap += item_sz; |
1838 | 0 | } |
1839 | |
|
1840 | 0 | if (bad) { |
1841 | 0 | msyslog(LOG_ERR, "do_restrict: bad = %#x", bad); |
1842 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1843 | 0 | return; |
1844 | 0 | } |
1845 | | |
1846 | | /* |
1847 | | * Looks okay, try it out. Needs to reload data pointer and |
1848 | | * item counter. (Talos-CAN-0052) |
1849 | | */ |
1850 | 0 | ZERO_SOCK(&matchaddr); |
1851 | 0 | ZERO_SOCK(&matchmask); |
1852 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
1853 | 0 | datap = inpkt->u.data; |
1854 | |
|
1855 | 0 | while (items-- > 0) { |
1856 | 0 | memcpy(&cr, datap, item_sz); |
1857 | 0 | cr.flags = ntohs(cr.flags); |
1858 | 0 | cr.mflags = ntohs(cr.mflags); |
1859 | 0 | cr.ippeerlimit = ntohs(cr.ippeerlimit); |
1860 | 0 | if (client_v6_capable && cr.v6_flag) { |
1861 | 0 | AF(&matchaddr) = AF_INET6; |
1862 | 0 | AF(&matchmask) = AF_INET6; |
1863 | 0 | SOCK_ADDR6(&matchaddr) = cr.addr6; |
1864 | 0 | SOCK_ADDR6(&matchmask) = cr.mask6; |
1865 | 0 | } else { |
1866 | 0 | AF(&matchaddr) = AF_INET; |
1867 | 0 | AF(&matchmask) = AF_INET; |
1868 | 0 | NSRCADR(&matchaddr) = cr.addr; |
1869 | 0 | NSRCADR(&matchmask) = cr.mask; |
1870 | 0 | } |
1871 | 0 | hack_restrict(op, &matchaddr, &matchmask, cr.mflags, |
1872 | 0 | cr.ippeerlimit, cr.flags, 0); |
1873 | 0 | datap += item_sz; |
1874 | 0 | } |
1875 | |
|
1876 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
1877 | 0 | } |
1878 | | |
1879 | | |
1880 | | /* |
1881 | | * mon_getlist - return monitor data |
1882 | | */ |
1883 | | static void |
1884 | | mon_getlist( |
1885 | | sockaddr_u *srcadr, |
1886 | | endpt *inter, |
1887 | | struct req_pkt *inpkt |
1888 | | ) |
1889 | 0 | { |
1890 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
1891 | 0 | } |
1892 | | |
1893 | | |
1894 | | /* |
1895 | | * Module entry points and the flags they correspond with |
1896 | | */ |
1897 | | struct reset_entry { |
1898 | | int flag; /* flag this corresponds to */ |
1899 | | void (*handler)(void); /* routine to handle request */ |
1900 | | }; |
1901 | | |
1902 | | struct reset_entry reset_entries[] = { |
1903 | | { RESET_FLAG_ALLPEERS, peer_all_reset }, |
1904 | | { RESET_FLAG_IO, io_clr_stats }, |
1905 | | { RESET_FLAG_SYS, proto_clr_stats }, |
1906 | | { RESET_FLAG_MEM, peer_clr_stats }, |
1907 | | { RESET_FLAG_TIMER, timer_clr_stats }, |
1908 | | { RESET_FLAG_AUTH, reset_auth_stats }, |
1909 | | { RESET_FLAG_CTL, ctl_clr_stats }, |
1910 | | { 0, 0 } |
1911 | | }; |
1912 | | |
1913 | | /* |
1914 | | * reset_stats - reset statistic counters here and there |
1915 | | */ |
1916 | | static void |
1917 | | reset_stats( |
1918 | | sockaddr_u *srcadr, |
1919 | | endpt *inter, |
1920 | | struct req_pkt *inpkt |
1921 | | ) |
1922 | 0 | { |
1923 | 0 | struct reset_flags *rflags; |
1924 | 0 | u_long flags; |
1925 | 0 | struct reset_entry *rent; |
1926 | |
|
1927 | 0 | if (INFO_NITEMS(inpkt->err_nitems) > 1) { |
1928 | 0 | msyslog(LOG_ERR, "reset_stats: err_nitems > 1"); |
1929 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1930 | 0 | return; |
1931 | 0 | } |
1932 | | |
1933 | 0 | rflags = (struct reset_flags *)&inpkt->u; |
1934 | 0 | flags = ntohl(rflags->flags); |
1935 | |
|
1936 | 0 | if (flags & ~RESET_ALLFLAGS) { |
1937 | 0 | msyslog(LOG_ERR, "reset_stats: reset leaves %#lx", |
1938 | 0 | flags & ~RESET_ALLFLAGS); |
1939 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1940 | 0 | return; |
1941 | 0 | } |
1942 | | |
1943 | 0 | for (rent = reset_entries; rent->flag != 0; rent++) { |
1944 | 0 | if (flags & rent->flag) |
1945 | 0 | (*rent->handler)(); |
1946 | 0 | } |
1947 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
1948 | 0 | } |
1949 | | |
1950 | | |
1951 | | /* |
1952 | | * reset_peer - clear a peer's statistics |
1953 | | */ |
1954 | | static void |
1955 | | reset_peer( |
1956 | | sockaddr_u *srcadr, |
1957 | | endpt *inter, |
1958 | | struct req_pkt *inpkt |
1959 | | ) |
1960 | 0 | { |
1961 | 0 | u_short items; |
1962 | 0 | size_t item_sz; |
1963 | 0 | char * datap; |
1964 | 0 | struct conf_unpeer cp; |
1965 | 0 | struct peer * p; |
1966 | 0 | sockaddr_u peeraddr; |
1967 | 0 | int bad; |
1968 | | |
1969 | | /* |
1970 | | * We check first to see that every peer exists. If not, |
1971 | | * we return an error. |
1972 | | */ |
1973 | |
|
1974 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
1975 | 0 | item_sz = INFO_ITEMSIZE(inpkt->mbz_itemsize); |
1976 | 0 | datap = inpkt->u.data; |
1977 | 0 | if (item_sz > sizeof(cp)) { |
1978 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
1979 | 0 | return; |
1980 | 0 | } |
1981 | | |
1982 | 0 | bad = FALSE; |
1983 | 0 | while (items-- > 0 && !bad) { |
1984 | 0 | ZERO(cp); |
1985 | 0 | memcpy(&cp, datap, item_sz); |
1986 | 0 | ZERO_SOCK(&peeraddr); |
1987 | 0 | if (client_v6_capable && cp.v6_flag) { |
1988 | 0 | AF(&peeraddr) = AF_INET6; |
1989 | 0 | SOCK_ADDR6(&peeraddr) = cp.peeraddr6; |
1990 | 0 | } else { |
1991 | 0 | AF(&peeraddr) = AF_INET; |
1992 | 0 | NSRCADR(&peeraddr) = cp.peeraddr; |
1993 | 0 | } |
1994 | |
|
1995 | | #ifdef ISC_PLATFORM_HAVESALEN |
1996 | | peeraddr.sa.sa_len = SOCKLEN(&peeraddr); |
1997 | | #endif |
1998 | 0 | p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0, NULL); |
1999 | 0 | if (NULL == p) |
2000 | 0 | bad++; |
2001 | 0 | datap += item_sz; |
2002 | 0 | } |
2003 | |
|
2004 | 0 | if (bad) { |
2005 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2006 | 0 | return; |
2007 | 0 | } |
2008 | | |
2009 | | /* |
2010 | | * Now do it in earnest. Needs to reload data pointer and item |
2011 | | * counter. (Talos-CAN-0052) |
2012 | | */ |
2013 | | |
2014 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
2015 | 0 | datap = inpkt->u.data; |
2016 | 0 | while (items-- > 0) { |
2017 | 0 | ZERO(cp); |
2018 | 0 | memcpy(&cp, datap, item_sz); |
2019 | 0 | ZERO_SOCK(&peeraddr); |
2020 | 0 | if (client_v6_capable && cp.v6_flag) { |
2021 | 0 | AF(&peeraddr) = AF_INET6; |
2022 | 0 | SOCK_ADDR6(&peeraddr) = cp.peeraddr6; |
2023 | 0 | } else { |
2024 | 0 | AF(&peeraddr) = AF_INET; |
2025 | 0 | NSRCADR(&peeraddr) = cp.peeraddr; |
2026 | 0 | } |
2027 | 0 | SET_PORT(&peeraddr, 123); |
2028 | | #ifdef ISC_PLATFORM_HAVESALEN |
2029 | | peeraddr.sa.sa_len = SOCKLEN(&peeraddr); |
2030 | | #endif |
2031 | 0 | p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0, NULL); |
2032 | 0 | while (p != NULL) { |
2033 | 0 | peer_reset(p); |
2034 | 0 | p = findexistingpeer(&peeraddr, NULL, p, -1, 0, NULL); |
2035 | 0 | } |
2036 | 0 | datap += item_sz; |
2037 | 0 | } |
2038 | |
|
2039 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2040 | 0 | } |
2041 | | |
2042 | | |
2043 | | /* |
2044 | | * do_key_reread - reread the encryption key file |
2045 | | */ |
2046 | | static void |
2047 | | do_key_reread( |
2048 | | sockaddr_u *srcadr, |
2049 | | endpt *inter, |
2050 | | struct req_pkt *inpkt |
2051 | | ) |
2052 | 0 | { |
2053 | 0 | rereadkeys(); |
2054 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2055 | 0 | } |
2056 | | |
2057 | | |
2058 | | /* |
2059 | | * trust_key - make one or more keys trusted |
2060 | | */ |
2061 | | static void |
2062 | | trust_key( |
2063 | | sockaddr_u *srcadr, |
2064 | | endpt *inter, |
2065 | | struct req_pkt *inpkt |
2066 | | ) |
2067 | 0 | { |
2068 | 0 | do_trustkey(srcadr, inter, inpkt, 1); |
2069 | 0 | } |
2070 | | |
2071 | | |
2072 | | /* |
2073 | | * untrust_key - make one or more keys untrusted |
2074 | | */ |
2075 | | static void |
2076 | | untrust_key( |
2077 | | sockaddr_u *srcadr, |
2078 | | endpt *inter, |
2079 | | struct req_pkt *inpkt |
2080 | | ) |
2081 | 0 | { |
2082 | 0 | do_trustkey(srcadr, inter, inpkt, 0); |
2083 | 0 | } |
2084 | | |
2085 | | |
2086 | | /* |
2087 | | * do_trustkey - make keys either trustable or untrustable |
2088 | | */ |
2089 | | static void |
2090 | | do_trustkey( |
2091 | | sockaddr_u *srcadr, |
2092 | | endpt *inter, |
2093 | | struct req_pkt *inpkt, |
2094 | | u_long trust |
2095 | | ) |
2096 | 0 | { |
2097 | 0 | register uint32_t *kp; |
2098 | 0 | register int items; |
2099 | |
|
2100 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
2101 | 0 | kp = (uint32_t *)&inpkt->u; |
2102 | 0 | while (items-- > 0) { |
2103 | 0 | authtrust(*kp, trust); |
2104 | 0 | kp++; |
2105 | 0 | } |
2106 | |
|
2107 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2108 | 0 | } |
2109 | | |
2110 | | |
2111 | | /* |
2112 | | * get_auth_info - return some stats concerning the authentication module |
2113 | | */ |
2114 | | static void |
2115 | | get_auth_info( |
2116 | | sockaddr_u *srcadr, |
2117 | | endpt *inter, |
2118 | | struct req_pkt *inpkt |
2119 | | ) |
2120 | 0 | { |
2121 | 0 | register struct info_auth *ia; |
2122 | |
|
2123 | 0 | ia = (struct info_auth *)prepare_pkt(srcadr, inter, inpkt, |
2124 | 0 | sizeof(struct info_auth)); |
2125 | |
|
2126 | 0 | ia->numkeys = htonl((u_int32)authnumkeys); |
2127 | 0 | ia->numfreekeys = htonl((u_int32)authnumfreekeys); |
2128 | 0 | ia->keylookups = htonl((u_int32)authkeylookups); |
2129 | 0 | ia->keynotfound = htonl((u_int32)authkeynotfound); |
2130 | 0 | ia->encryptions = htonl((u_int32)authencryptions); |
2131 | 0 | ia->decryptions = htonl((u_int32)authdecryptions); |
2132 | 0 | ia->keyuncached = htonl((u_int32)authkeyuncached); |
2133 | 0 | ia->expired = htonl((u_int32)authkeyexpired); |
2134 | 0 | ia->timereset = htonl((u_int32)(current_time - auth_timereset)); |
2135 | | |
2136 | 0 | (void) more_pkt(); |
2137 | 0 | flush_pkt(); |
2138 | 0 | } |
2139 | | |
2140 | | |
2141 | | |
2142 | | /* |
2143 | | * reset_auth_stats - reset the authentication stat counters. Done here |
2144 | | * to keep ntp-isms out of the authentication module |
2145 | | */ |
2146 | | void |
2147 | | reset_auth_stats(void) |
2148 | 0 | { |
2149 | 0 | authkeylookups = 0; |
2150 | 0 | authkeynotfound = 0; |
2151 | 0 | authencryptions = 0; |
2152 | 0 | authdecryptions = 0; |
2153 | 0 | authkeyuncached = 0; |
2154 | 0 | auth_timereset = current_time; |
2155 | 0 | } |
2156 | | |
2157 | | |
2158 | | /* |
2159 | | * req_get_traps - return information about current trap holders |
2160 | | */ |
2161 | | static void |
2162 | | req_get_traps( |
2163 | | sockaddr_u *srcadr, |
2164 | | endpt *inter, |
2165 | | struct req_pkt *inpkt |
2166 | | ) |
2167 | 0 | { |
2168 | 0 | struct info_trap *it; |
2169 | 0 | struct ctl_trap *tr; |
2170 | 0 | size_t i; |
2171 | |
|
2172 | 0 | if (num_ctl_traps == 0) { |
2173 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2174 | 0 | return; |
2175 | 0 | } |
2176 | | |
2177 | 0 | it = (struct info_trap *)prepare_pkt(srcadr, inter, inpkt, |
2178 | 0 | v6sizeof(struct info_trap)); |
2179 | |
|
2180 | 0 | for (i = 0, tr = ctl_traps; it && i < COUNTOF(ctl_traps); i++, tr++) { |
2181 | 0 | if (tr->tr_flags & TRAP_INUSE) { |
2182 | 0 | if (IS_IPV4(&tr->tr_addr)) { |
2183 | 0 | if (tr->tr_localaddr == any_interface) |
2184 | 0 | it->local_address = 0; |
2185 | 0 | else |
2186 | 0 | it->local_address |
2187 | 0 | = NSRCADR(&tr->tr_localaddr->sin); |
2188 | 0 | it->trap_address = NSRCADR(&tr->tr_addr); |
2189 | 0 | if (client_v6_capable) |
2190 | 0 | it->v6_flag = 0; |
2191 | 0 | } else { |
2192 | 0 | if (!client_v6_capable) |
2193 | 0 | continue; |
2194 | 0 | it->local_address6 |
2195 | 0 | = SOCK_ADDR6(&tr->tr_localaddr->sin); |
2196 | 0 | it->trap_address6 = SOCK_ADDR6(&tr->tr_addr); |
2197 | 0 | it->v6_flag = 1; |
2198 | 0 | } |
2199 | 0 | it->trap_port = NSRCPORT(&tr->tr_addr); |
2200 | 0 | it->sequence = htons(tr->tr_sequence); |
2201 | 0 | it->settime = htonl((u_int32)(current_time - tr->tr_settime)); |
2202 | 0 | it->origtime = htonl((u_int32)(current_time - tr->tr_origtime)); |
2203 | 0 | it->resets = htonl((u_int32)tr->tr_resets); |
2204 | 0 | it->flags = htonl((u_int32)tr->tr_flags); |
2205 | 0 | it = (struct info_trap *)more_pkt(); |
2206 | 0 | } |
2207 | 0 | } |
2208 | 0 | flush_pkt(); |
2209 | 0 | } |
2210 | | |
2211 | | |
2212 | | /* |
2213 | | * req_set_trap - configure a trap |
2214 | | */ |
2215 | | static void |
2216 | | req_set_trap( |
2217 | | sockaddr_u *srcadr, |
2218 | | endpt *inter, |
2219 | | struct req_pkt *inpkt |
2220 | | ) |
2221 | 0 | { |
2222 | 0 | do_setclr_trap(srcadr, inter, inpkt, 1); |
2223 | 0 | } |
2224 | | |
2225 | | |
2226 | | |
2227 | | /* |
2228 | | * req_clr_trap - unconfigure a trap |
2229 | | */ |
2230 | | static void |
2231 | | req_clr_trap( |
2232 | | sockaddr_u *srcadr, |
2233 | | endpt *inter, |
2234 | | struct req_pkt *inpkt |
2235 | | ) |
2236 | 0 | { |
2237 | 0 | do_setclr_trap(srcadr, inter, inpkt, 0); |
2238 | 0 | } |
2239 | | |
2240 | | |
2241 | | |
2242 | | /* |
2243 | | * do_setclr_trap - do the grunge work of (un)configuring a trap |
2244 | | */ |
2245 | | static void |
2246 | | do_setclr_trap( |
2247 | | sockaddr_u *srcadr, |
2248 | | endpt *inter, |
2249 | | struct req_pkt *inpkt, |
2250 | | int set |
2251 | | ) |
2252 | 0 | { |
2253 | 0 | register struct conf_trap *ct; |
2254 | 0 | register endpt *linter; |
2255 | 0 | int res; |
2256 | 0 | sockaddr_u laddr; |
2257 | | |
2258 | | /* |
2259 | | * Prepare sockaddr |
2260 | | */ |
2261 | 0 | ZERO_SOCK(&laddr); |
2262 | 0 | AF(&laddr) = AF(srcadr); |
2263 | 0 | SET_PORT(&laddr, NTP_PORT); |
2264 | | |
2265 | | /* |
2266 | | * Restrict ourselves to one item only. This eliminates |
2267 | | * the error reporting problem. |
2268 | | */ |
2269 | 0 | if (INFO_NITEMS(inpkt->err_nitems) > 1) { |
2270 | 0 | msyslog(LOG_ERR, "do_setclr_trap: err_nitems > 1"); |
2271 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
2272 | 0 | return; |
2273 | 0 | } |
2274 | 0 | ct = (struct conf_trap *)&inpkt->u; |
2275 | | |
2276 | | /* |
2277 | | * Look for the local interface. If none, use the default. |
2278 | | */ |
2279 | 0 | if (ct->local_address == 0) { |
2280 | 0 | linter = any_interface; |
2281 | 0 | } else { |
2282 | 0 | if (IS_IPV4(&laddr)) |
2283 | 0 | NSRCADR(&laddr) = ct->local_address; |
2284 | 0 | else |
2285 | 0 | SOCK_ADDR6(&laddr) = ct->local_address6; |
2286 | 0 | linter = findinterface(&laddr); |
2287 | 0 | if (NULL == linter) { |
2288 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2289 | 0 | return; |
2290 | 0 | } |
2291 | 0 | } |
2292 | | |
2293 | 0 | if (IS_IPV4(&laddr)) |
2294 | 0 | NSRCADR(&laddr) = ct->trap_address; |
2295 | 0 | else |
2296 | 0 | SOCK_ADDR6(&laddr) = ct->trap_address6; |
2297 | 0 | if (ct->trap_port) |
2298 | 0 | NSRCPORT(&laddr) = ct->trap_port; |
2299 | 0 | else |
2300 | 0 | SET_PORT(&laddr, TRAPPORT); |
2301 | |
|
2302 | 0 | if (set) { |
2303 | 0 | res = ctlsettrap(&laddr, linter, 0, |
2304 | 0 | INFO_VERSION(inpkt->rm_vn_mode)); |
2305 | 0 | } else { |
2306 | 0 | res = ctlclrtrap(&laddr, linter, 0); |
2307 | 0 | } |
2308 | |
|
2309 | 0 | if (!res) { |
2310 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2311 | 0 | } else { |
2312 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2313 | 0 | } |
2314 | 0 | return; |
2315 | 0 | } |
2316 | | |
2317 | | /* |
2318 | | * Validate a request packet for a new request or control key: |
2319 | | * - only one item allowed |
2320 | | * - key must be valid (that is, known, and not in the autokey range) |
2321 | | */ |
2322 | | static void |
2323 | | set_keyid_checked( |
2324 | | keyid_t *into, |
2325 | | const char *what, |
2326 | | sockaddr_u *srcadr, |
2327 | | endpt *inter, |
2328 | | struct req_pkt *inpkt |
2329 | | ) |
2330 | 0 | { |
2331 | 0 | keyid_t *pkeyid; |
2332 | 0 | keyid_t tmpkey; |
2333 | | |
2334 | | /* restrict ourselves to one item only */ |
2335 | 0 | if (INFO_NITEMS(inpkt->err_nitems) > 1) { |
2336 | 0 | msyslog(LOG_ERR, "set_keyid_checked[%s]: err_nitems > 1", |
2337 | 0 | what); |
2338 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
2339 | 0 | return; |
2340 | 0 | } |
2341 | | |
2342 | | /* plug the new key from the packet */ |
2343 | 0 | pkeyid = (keyid_t *)&inpkt->u; |
2344 | 0 | tmpkey = ntohl(*pkeyid); |
2345 | | |
2346 | | /* validate the new key id, claim data error on failure */ |
2347 | 0 | if (tmpkey < 1 || tmpkey > NTP_MAXKEY || !auth_havekey(tmpkey)) { |
2348 | 0 | msyslog(LOG_ERR, "set_keyid_checked[%s]: invalid key id: %ld", |
2349 | 0 | what, (long)tmpkey); |
2350 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2351 | 0 | return; |
2352 | 0 | } |
2353 | | |
2354 | | /* if we arrive here, the key is good -- use it */ |
2355 | 0 | *into = tmpkey; |
2356 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2357 | 0 | } |
2358 | | |
2359 | | /* |
2360 | | * set_request_keyid - set the keyid used to authenticate requests |
2361 | | */ |
2362 | | static void |
2363 | | set_request_keyid( |
2364 | | sockaddr_u *srcadr, |
2365 | | endpt *inter, |
2366 | | struct req_pkt *inpkt |
2367 | | ) |
2368 | 0 | { |
2369 | 0 | set_keyid_checked(&info_auth_keyid, "request", |
2370 | 0 | srcadr, inter, inpkt); |
2371 | 0 | } |
2372 | | |
2373 | | |
2374 | | |
2375 | | /* |
2376 | | * set_control_keyid - set the keyid used to authenticate requests |
2377 | | */ |
2378 | | static void |
2379 | | set_control_keyid( |
2380 | | sockaddr_u *srcadr, |
2381 | | endpt *inter, |
2382 | | struct req_pkt *inpkt |
2383 | | ) |
2384 | 0 | { |
2385 | 0 | set_keyid_checked(&ctl_auth_keyid, "control", |
2386 | 0 | srcadr, inter, inpkt); |
2387 | 0 | } |
2388 | | |
2389 | | |
2390 | | |
2391 | | /* |
2392 | | * get_ctl_stats - return some stats concerning the control message module |
2393 | | */ |
2394 | | static void |
2395 | | get_ctl_stats( |
2396 | | sockaddr_u *srcadr, |
2397 | | endpt *inter, |
2398 | | struct req_pkt *inpkt |
2399 | | ) |
2400 | 0 | { |
2401 | 0 | register struct info_control *ic; |
2402 | |
|
2403 | 0 | ic = (struct info_control *)prepare_pkt(srcadr, inter, inpkt, |
2404 | 0 | sizeof(struct info_control)); |
2405 | |
|
2406 | 0 | ic->ctltimereset = htonl((u_int32)(current_time - ctltimereset)); |
2407 | 0 | ic->numctlreq = htonl((u_int32)numctlreq); |
2408 | 0 | ic->numctlbadpkts = htonl((u_int32)numctlbadpkts); |
2409 | 0 | ic->numctlresponses = htonl((u_int32)numctlresponses); |
2410 | 0 | ic->numctlfrags = htonl((u_int32)numctlfrags); |
2411 | 0 | ic->numctlerrors = htonl((u_int32)numctlerrors); |
2412 | 0 | ic->numctltooshort = htonl((u_int32)numctltooshort); |
2413 | 0 | ic->numctlinputresp = htonl((u_int32)numctlinputresp); |
2414 | 0 | ic->numctlinputfrag = htonl((u_int32)numctlinputfrag); |
2415 | 0 | ic->numctlinputerr = htonl((u_int32)numctlinputerr); |
2416 | 0 | ic->numctlbadoffset = htonl((u_int32)numctlbadoffset); |
2417 | 0 | ic->numctlbadversion = htonl((u_int32)numctlbadversion); |
2418 | 0 | ic->numctldatatooshort = htonl((u_int32)numctldatatooshort); |
2419 | 0 | ic->numctlbadop = htonl((u_int32)numctlbadop); |
2420 | 0 | ic->numasyncmsgs = htonl((u_int32)numasyncmsgs); |
2421 | |
|
2422 | 0 | (void) more_pkt(); |
2423 | 0 | flush_pkt(); |
2424 | 0 | } |
2425 | | |
2426 | | |
2427 | | #ifdef KERNEL_PLL |
2428 | | /* |
2429 | | * get_kernel_info - get kernel pll/pps information |
2430 | | */ |
2431 | | static void |
2432 | | get_kernel_info( |
2433 | | sockaddr_u *srcadr, |
2434 | | endpt *inter, |
2435 | | struct req_pkt *inpkt |
2436 | | ) |
2437 | 0 | { |
2438 | 0 | register struct info_kernel *ik; |
2439 | 0 | struct timex ntx; |
2440 | |
|
2441 | 0 | if (!pll_control) { |
2442 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2443 | 0 | return; |
2444 | 0 | } |
2445 | | |
2446 | 0 | ZERO(ntx); |
2447 | 0 | if (ntp_adjtime(&ntx) < 0) |
2448 | 0 | msyslog(LOG_ERR, "get_kernel_info: ntp_adjtime() failed: %m"); |
2449 | 0 | ik = (struct info_kernel *)prepare_pkt(srcadr, inter, inpkt, |
2450 | 0 | sizeof(struct info_kernel)); |
2451 | | |
2452 | | /* |
2453 | | * pll variables |
2454 | | */ |
2455 | 0 | ik->offset = htonl((u_int32)ntx.offset); |
2456 | 0 | ik->freq = htonl((u_int32)ntx.freq); |
2457 | 0 | ik->maxerror = htonl((u_int32)ntx.maxerror); |
2458 | 0 | ik->esterror = htonl((u_int32)ntx.esterror); |
2459 | 0 | ik->status = htons(ntx.status); |
2460 | 0 | ik->constant = htonl((u_int32)ntx.constant); |
2461 | 0 | ik->precision = htonl((u_int32)ntx.precision); |
2462 | 0 | ik->tolerance = htonl((u_int32)ntx.tolerance); |
2463 | | |
2464 | | /* |
2465 | | * pps variables |
2466 | | */ |
2467 | 0 | ik->ppsfreq = htonl((u_int32)ntx.ppsfreq); |
2468 | 0 | ik->jitter = htonl((u_int32)ntx.jitter); |
2469 | 0 | ik->shift = htons(ntx.shift); |
2470 | 0 | ik->stabil = htonl((u_int32)ntx.stabil); |
2471 | 0 | ik->jitcnt = htonl((u_int32)ntx.jitcnt); |
2472 | 0 | ik->calcnt = htonl((u_int32)ntx.calcnt); |
2473 | 0 | ik->errcnt = htonl((u_int32)ntx.errcnt); |
2474 | 0 | ik->stbcnt = htonl((u_int32)ntx.stbcnt); |
2475 | | |
2476 | 0 | (void) more_pkt(); |
2477 | 0 | flush_pkt(); |
2478 | 0 | } |
2479 | | #endif /* KERNEL_PLL */ |
2480 | | |
2481 | | |
2482 | | #ifdef REFCLOCK |
2483 | | /* |
2484 | | * get_clock_info - get info about a clock |
2485 | | */ |
2486 | | static void |
2487 | | get_clock_info( |
2488 | | sockaddr_u *srcadr, |
2489 | | endpt *inter, |
2490 | | struct req_pkt *inpkt |
2491 | | ) |
2492 | 0 | { |
2493 | 0 | register struct info_clock *ic; |
2494 | 0 | register u_int32 *clkaddr; |
2495 | 0 | register int items; |
2496 | 0 | struct refclockstat clock_stat; |
2497 | 0 | sockaddr_u addr; |
2498 | 0 | l_fp ltmp; |
2499 | |
|
2500 | 0 | ZERO_SOCK(&addr); |
2501 | 0 | AF(&addr) = AF_INET; |
2502 | | #ifdef ISC_PLATFORM_HAVESALEN |
2503 | | addr.sa.sa_len = SOCKLEN(&addr); |
2504 | | #endif |
2505 | 0 | SET_PORT(&addr, NTP_PORT); |
2506 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
2507 | 0 | clkaddr = &inpkt->u.u32[0]; |
2508 | |
|
2509 | 0 | ic = (struct info_clock *)prepare_pkt(srcadr, inter, inpkt, |
2510 | 0 | sizeof(struct info_clock)); |
2511 | |
|
2512 | 0 | while (items-- > 0 && ic) { |
2513 | 0 | NSRCADR(&addr) = *clkaddr++; |
2514 | 0 | if (!ISREFCLOCKADR(&addr) || NULL == |
2515 | 0 | findexistingpeer(&addr, NULL, NULL, -1, 0, NULL)) { |
2516 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2517 | 0 | return; |
2518 | 0 | } |
2519 | | |
2520 | 0 | clock_stat.kv_list = (struct ctl_var *)0; |
2521 | |
|
2522 | 0 | refclock_control(&addr, NULL, &clock_stat); |
2523 | |
|
2524 | 0 | ic->clockadr = NSRCADR(&addr); |
2525 | 0 | ic->type = clock_stat.type; |
2526 | 0 | ic->flags = clock_stat.flags; |
2527 | 0 | ic->lastevent = clock_stat.lastevent; |
2528 | 0 | ic->currentstatus = clock_stat.currentstatus; |
2529 | 0 | ic->polls = htonl((u_int32)clock_stat.polls); |
2530 | 0 | ic->noresponse = htonl((u_int32)clock_stat.noresponse); |
2531 | 0 | ic->badformat = htonl((u_int32)clock_stat.badformat); |
2532 | 0 | ic->baddata = htonl((u_int32)clock_stat.baddata); |
2533 | 0 | ic->timestarted = htonl((u_int32)clock_stat.timereset); |
2534 | 0 | DTOLFP(clock_stat.fudgetime1, <mp); |
2535 | 0 | HTONL_FP(<mp, &ic->fudgetime1); |
2536 | 0 | DTOLFP(clock_stat.fudgetime2, <mp); |
2537 | 0 | HTONL_FP(<mp, &ic->fudgetime2); |
2538 | 0 | ic->fudgeval1 = htonl((u_int32)clock_stat.fudgeval1); |
2539 | | /* [Bug3527] Backward Incompatible: ic->fudgeval2 is |
2540 | | * a string, instantiated via memcpy() so there is no |
2541 | | * endian issue to correct. |
2542 | | */ |
2543 | | #ifdef DISABLE_BUG3527_FIX |
2544 | | ic->fudgeval2 = htonl(clock_stat.fudgeval2); |
2545 | | #else |
2546 | 0 | ic->fudgeval2 = clock_stat.fudgeval2; |
2547 | 0 | #endif |
2548 | |
|
2549 | 0 | free_varlist(clock_stat.kv_list); |
2550 | |
|
2551 | 0 | ic = (struct info_clock *)more_pkt(); |
2552 | 0 | } |
2553 | 0 | flush_pkt(); |
2554 | 0 | } |
2555 | | |
2556 | | |
2557 | | |
2558 | | /* |
2559 | | * set_clock_fudge - get a clock's fudge factors |
2560 | | */ |
2561 | | static void |
2562 | | set_clock_fudge( |
2563 | | sockaddr_u *srcadr, |
2564 | | endpt *inter, |
2565 | | struct req_pkt *inpkt |
2566 | | ) |
2567 | 0 | { |
2568 | 0 | register struct conf_fudge *cf; |
2569 | 0 | register int items; |
2570 | 0 | struct refclockstat clock_stat; |
2571 | 0 | sockaddr_u addr; |
2572 | 0 | l_fp ltmp; |
2573 | |
|
2574 | 0 | ZERO(addr); |
2575 | 0 | ZERO(clock_stat); |
2576 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
2577 | 0 | cf = (struct conf_fudge *)&inpkt->u; |
2578 | |
|
2579 | 0 | while (items-- > 0) { |
2580 | 0 | AF(&addr) = AF_INET; |
2581 | 0 | NSRCADR(&addr) = cf->clockadr; |
2582 | | #ifdef ISC_PLATFORM_HAVESALEN |
2583 | | addr.sa.sa_len = SOCKLEN(&addr); |
2584 | | #endif |
2585 | 0 | SET_PORT(&addr, NTP_PORT); |
2586 | 0 | if (!ISREFCLOCKADR(&addr) || NULL == |
2587 | 0 | findexistingpeer(&addr, NULL, NULL, -1, 0, NULL)) { |
2588 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2589 | 0 | return; |
2590 | 0 | } |
2591 | | |
2592 | 0 | switch(ntohl(cf->which)) { |
2593 | 0 | case FUDGE_TIME1: |
2594 | 0 | NTOHL_FP(&cf->fudgetime, <mp); |
2595 | 0 | LFPTOD(<mp, clock_stat.fudgetime1); |
2596 | 0 | clock_stat.haveflags = CLK_HAVETIME1; |
2597 | 0 | break; |
2598 | 0 | case FUDGE_TIME2: |
2599 | 0 | NTOHL_FP(&cf->fudgetime, <mp); |
2600 | 0 | LFPTOD(<mp, clock_stat.fudgetime2); |
2601 | 0 | clock_stat.haveflags = CLK_HAVETIME2; |
2602 | 0 | break; |
2603 | 0 | case FUDGE_VAL1: |
2604 | 0 | clock_stat.fudgeval1 = ntohl(cf->fudgeval_flags); |
2605 | 0 | clock_stat.haveflags = CLK_HAVEVAL1; |
2606 | 0 | break; |
2607 | 0 | case FUDGE_VAL2: |
2608 | 0 | clock_stat.fudgeval2 = ntohl(cf->fudgeval_flags); |
2609 | 0 | clock_stat.haveflags = CLK_HAVEVAL2; |
2610 | 0 | break; |
2611 | 0 | case FUDGE_FLAGS: |
2612 | 0 | clock_stat.flags = (u_char) (ntohl(cf->fudgeval_flags) & 0xf); |
2613 | 0 | clock_stat.haveflags = |
2614 | 0 | (CLK_HAVEFLAG1|CLK_HAVEFLAG2|CLK_HAVEFLAG3|CLK_HAVEFLAG4); |
2615 | 0 | break; |
2616 | 0 | default: |
2617 | 0 | msyslog(LOG_ERR, "set_clock_fudge: default!"); |
2618 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); |
2619 | 0 | return; |
2620 | 0 | } |
2621 | | |
2622 | 0 | refclock_control(&addr, &clock_stat, (struct refclockstat *)0); |
2623 | 0 | } |
2624 | | |
2625 | 0 | req_ack(srcadr, inter, inpkt, INFO_OKAY); |
2626 | 0 | } |
2627 | | #endif |
2628 | | |
2629 | | #ifdef REFCLOCK |
2630 | | /* |
2631 | | * get_clkbug_info - get debugging info about a clock |
2632 | | */ |
2633 | | static void |
2634 | | get_clkbug_info( |
2635 | | sockaddr_u *srcadr, |
2636 | | endpt *inter, |
2637 | | struct req_pkt *inpkt |
2638 | | ) |
2639 | 0 | { |
2640 | 0 | register int i; |
2641 | 0 | register struct info_clkbug *ic; |
2642 | 0 | register u_int32 *clkaddr; |
2643 | 0 | register int items; |
2644 | 0 | struct refclockbug bug; |
2645 | 0 | sockaddr_u addr; |
2646 | |
|
2647 | 0 | ZERO_SOCK(&addr); |
2648 | 0 | AF(&addr) = AF_INET; |
2649 | | #ifdef ISC_PLATFORM_HAVESALEN |
2650 | | addr.sa.sa_len = SOCKLEN(&addr); |
2651 | | #endif |
2652 | 0 | SET_PORT(&addr, NTP_PORT); |
2653 | 0 | items = INFO_NITEMS(inpkt->err_nitems); |
2654 | 0 | clkaddr = (u_int32 *)&inpkt->u; |
2655 | |
|
2656 | 0 | ic = (struct info_clkbug *)prepare_pkt(srcadr, inter, inpkt, |
2657 | 0 | sizeof(struct info_clkbug)); |
2658 | |
|
2659 | 0 | while (items-- > 0 && ic) { |
2660 | 0 | NSRCADR(&addr) = *clkaddr++; |
2661 | 0 | if (!ISREFCLOCKADR(&addr) || NULL == |
2662 | 0 | findexistingpeer(&addr, NULL, NULL, -1, 0, NULL)) { |
2663 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2664 | 0 | return; |
2665 | 0 | } |
2666 | | |
2667 | 0 | ZERO(bug); |
2668 | 0 | refclock_buginfo(&addr, &bug); |
2669 | 0 | if (bug.nvalues == 0 && bug.ntimes == 0) { |
2670 | 0 | req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); |
2671 | 0 | return; |
2672 | 0 | } |
2673 | | |
2674 | 0 | ic->clockadr = NSRCADR(&addr); |
2675 | 0 | i = bug.nvalues; |
2676 | 0 | if (i > NUMCBUGVALUES) |
2677 | 0 | i = NUMCBUGVALUES; |
2678 | 0 | ic->nvalues = (u_char)i; |
2679 | 0 | ic->svalues = htons((u_short) (bug.svalues & ((1<<i)-1))); |
2680 | 0 | while (--i >= 0) |
2681 | 0 | ic->values[i] = htonl(bug.values[i]); |
2682 | |
|
2683 | 0 | i = bug.ntimes; |
2684 | 0 | if (i > NUMCBUGTIMES) |
2685 | 0 | i = NUMCBUGTIMES; |
2686 | 0 | ic->ntimes = (u_char)i; |
2687 | 0 | ic->stimes = htonl(bug.stimes); |
2688 | 0 | while (--i >= 0) { |
2689 | 0 | HTONL_FP(&bug.times[i], &ic->times[i]); |
2690 | 0 | } |
2691 | |
|
2692 | 0 | ic = (struct info_clkbug *)more_pkt(); |
2693 | 0 | } |
2694 | 0 | flush_pkt(); |
2695 | 0 | } |
2696 | | #endif |
2697 | | |
2698 | | /* |
2699 | | * receiver of interface structures |
2700 | | */ |
2701 | | static void |
2702 | | fill_info_if_stats(void *data, interface_info_t *interface_info) |
2703 | 0 | { |
2704 | 0 | struct info_if_stats **ifsp = (struct info_if_stats **)data; |
2705 | 0 | struct info_if_stats *ifs = *ifsp; |
2706 | 0 | endpt *ep = interface_info->ep; |
2707 | |
|
2708 | 0 | if (NULL == ifs) |
2709 | 0 | return; |
2710 | | |
2711 | 0 | ZERO(*ifs); |
2712 | | |
2713 | 0 | if (IS_IPV6(&ep->sin)) { |
2714 | 0 | if (!client_v6_capable) |
2715 | 0 | return; |
2716 | 0 | ifs->v6_flag = 1; |
2717 | 0 | ifs->unaddr.addr6 = SOCK_ADDR6(&ep->sin); |
2718 | 0 | ifs->unbcast.addr6 = SOCK_ADDR6(&ep->bcast); |
2719 | 0 | ifs->unmask.addr6 = SOCK_ADDR6(&ep->mask); |
2720 | 0 | } else { |
2721 | 0 | ifs->v6_flag = 0; |
2722 | 0 | ifs->unaddr.addr = SOCK_ADDR4(&ep->sin); |
2723 | 0 | ifs->unbcast.addr = SOCK_ADDR4(&ep->bcast); |
2724 | 0 | ifs->unmask.addr = SOCK_ADDR4(&ep->mask); |
2725 | 0 | } |
2726 | 0 | ifs->v6_flag = htonl(ifs->v6_flag); |
2727 | 0 | strlcpy(ifs->name, ep->name, sizeof(ifs->name)); |
2728 | 0 | ifs->family = htons(ep->family); |
2729 | 0 | ifs->flags = htonl(ep->flags); |
2730 | 0 | ifs->last_ttl = htonl(ep->last_ttl); |
2731 | 0 | ifs->num_mcast = htonl(ep->num_mcast); |
2732 | 0 | ifs->received = htonl(ep->received); |
2733 | 0 | ifs->sent = htonl(ep->sent); |
2734 | 0 | ifs->notsent = htonl(ep->notsent); |
2735 | 0 | ifs->ifindex = htonl(ep->ifindex); |
2736 | | /* scope no longer in endpt, in in6_addr typically */ |
2737 | 0 | ifs->scopeid = ifs->ifindex; |
2738 | 0 | ifs->ifnum = htonl(ep->ifnum); |
2739 | 0 | ifs->uptime = htonl(current_time - ep->starttime); |
2740 | 0 | ifs->ignore_packets = ep->ignore_packets; |
2741 | 0 | ifs->peercnt = htonl(ep->peercnt); |
2742 | 0 | ifs->action = interface_info->action; |
2743 | | |
2744 | 0 | *ifsp = (struct info_if_stats *)more_pkt(); |
2745 | 0 | } |
2746 | | |
2747 | | /* |
2748 | | * get_if_stats - get interface statistics |
2749 | | */ |
2750 | | static void |
2751 | | get_if_stats( |
2752 | | sockaddr_u *srcadr, |
2753 | | endpt *inter, |
2754 | | struct req_pkt *inpkt |
2755 | | ) |
2756 | 0 | { |
2757 | 0 | struct info_if_stats *ifs; |
2758 | |
|
2759 | 0 | DPRINTF(3, ("wants interface statistics\n")); |
2760 | |
|
2761 | 0 | ifs = (struct info_if_stats *)prepare_pkt(srcadr, inter, inpkt, |
2762 | 0 | v6sizeof(struct info_if_stats)); |
2763 | |
|
2764 | 0 | interface_enumerate(fill_info_if_stats, &ifs); |
2765 | | |
2766 | 0 | flush_pkt(); |
2767 | 0 | } |
2768 | | |
2769 | | static void |
2770 | | do_if_reload( |
2771 | | sockaddr_u *srcadr, |
2772 | | endpt *inter, |
2773 | | struct req_pkt *inpkt |
2774 | | ) |
2775 | 0 | { |
2776 | 0 | struct info_if_stats *ifs; |
2777 | |
|
2778 | 0 | DPRINTF(3, ("wants interface reload\n")); |
2779 | |
|
2780 | 0 | ifs = (struct info_if_stats *)prepare_pkt(srcadr, inter, inpkt, |
2781 | 0 | v6sizeof(struct info_if_stats)); |
2782 | |
|
2783 | 0 | interface_update(fill_info_if_stats, &ifs); |
2784 | | |
2785 | 0 | flush_pkt(); |
2786 | 0 | } |
2787 | | |