]> git.ipfire.org Git - thirdparty/chrony.git/blob - candm.h
cmdmon: add smoothing command
[thirdparty/chrony.git] / candm.h
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 Definitions for the network protocol used for command and monitoring
25 of the timeserver.
26
27 */
28
29 #ifndef GOT_CANDM_H
30 #define GOT_CANDM_H
31
32 #include "sysincl.h"
33 #include "addressing.h"
34 #include "hash.h"
35
36 /* This is the default port to use for CANDM, if no alternative is
37 defined */
38 #define DEFAULT_CANDM_PORT 323
39
40 /* Request codes */
41 #define REQ_NULL 0
42 #define REQ_ONLINE 1
43 #define REQ_OFFLINE 2
44 #define REQ_BURST 3
45 #define REQ_MODIFY_MINPOLL 4
46 #define REQ_MODIFY_MAXPOLL 5
47 #define REQ_DUMP 6
48 #define REQ_MODIFY_MAXDELAY 7
49 #define REQ_MODIFY_MAXDELAYRATIO 8
50 #define REQ_MODIFY_MAXUPDATESKEW 9
51 #define REQ_LOGON 10
52 #define REQ_SETTIME 11
53 #define REQ_LOCAL 12
54 #define REQ_MANUAL 13
55 #define REQ_N_SOURCES 14
56 #define REQ_SOURCE_DATA 15
57 #define REQ_REKEY 16
58 #define REQ_ALLOW 17
59 #define REQ_ALLOWALL 18
60 #define REQ_DENY 19
61 #define REQ_DENYALL 20
62 #define REQ_CMDALLOW 21
63 #define REQ_CMDALLOWALL 22
64 #define REQ_CMDDENY 23
65 #define REQ_CMDDENYALL 24
66 #define REQ_ACCHECK 25
67 #define REQ_CMDACCHECK 26
68 #define REQ_ADD_SERVER 27
69 #define REQ_ADD_PEER 28
70 #define REQ_DEL_SOURCE 29
71 #define REQ_WRITERTC 30
72 #define REQ_DFREQ 31
73 #define REQ_DOFFSET 32
74 #define REQ_TRACKING 33
75 #define REQ_SOURCESTATS 34
76 #define REQ_RTCREPORT 35
77 #define REQ_TRIMRTC 36
78 #define REQ_CYCLELOGS 37
79 #define REQ_SUBNETS_ACCESSED 38
80 #define REQ_CLIENT_ACCESSES 39
81 #define REQ_CLIENT_ACCESSES_BY_INDEX 40
82 #define REQ_MANUAL_LIST 41
83 #define REQ_MANUAL_DELETE 42
84 #define REQ_MAKESTEP 43
85 #define REQ_ACTIVITY 44
86 #define REQ_MODIFY_MINSTRATUM 45
87 #define REQ_MODIFY_POLLTARGET 46
88 #define REQ_MODIFY_MAXDELAYDEVRATIO 47
89 #define REQ_RESELECT 48
90 #define REQ_RESELECTDISTANCE 49
91 #define REQ_MODIFY_MAKESTEP 50
92 #define REQ_SMOOTHING 51
93 #define N_REQUEST_TYPES 52
94
95 /* Special utoken value used to log on with first exchange being the
96 password. (This time value has long since gone by) */
97 #define SPECIAL_UTOKEN 0x10101010
98
99 /* Structure used to exchange timevals independent on size of time_t */
100 typedef struct {
101 uint32_t tv_sec_high;
102 uint32_t tv_sec_low;
103 uint32_t tv_nsec;
104 } Timeval;
105
106 /* This is used in tv_sec_high for 32-bit timestamps */
107 #define TV_NOHIGHSEC 0x7fffffff
108
109 /* 32-bit floating-point format consisting of 7-bit signed exponent
110 and 25-bit signed coefficient without hidden bit.
111 The result is calculated as: 2^(exp - 25) * coef */
112 typedef struct {
113 int32_t f;
114 } Float;
115
116 /* The EOR (end of record) fields are used by the offsetof operator in
117 pktlength.c, to get the number of bytes that ought to be
118 transmitted for each packet type. */
119
120 typedef struct {
121 int32_t EOR;
122 } REQ_Null;
123
124 typedef struct {
125 IPAddr mask;
126 IPAddr address;
127 int32_t EOR;
128 } REQ_Online;
129
130 typedef struct {
131 IPAddr mask;
132 IPAddr address;
133 int32_t EOR;
134 } REQ_Offline;
135
136 typedef struct {
137 IPAddr mask;
138 IPAddr address;
139 int32_t n_good_samples;
140 int32_t n_total_samples;
141 int32_t EOR;
142 } REQ_Burst;
143
144 typedef struct {
145 IPAddr address;
146 int32_t new_minpoll;
147 int32_t EOR;
148 } REQ_Modify_Minpoll;
149
150 typedef struct {
151 IPAddr address;
152 int32_t new_maxpoll;
153 int32_t EOR;
154 } REQ_Modify_Maxpoll;
155
156 typedef struct {
157 int32_t pad;
158 int32_t EOR;
159 } REQ_Dump;
160
161 typedef struct {
162 IPAddr address;
163 Float new_max_delay;
164 int32_t EOR;
165 } REQ_Modify_Maxdelay;
166
167 typedef struct {
168 IPAddr address;
169 Float new_max_delay_ratio;
170 int32_t EOR;
171 } REQ_Modify_Maxdelayratio;
172
173 typedef struct {
174 IPAddr address;
175 Float new_max_delay_dev_ratio;
176 int32_t EOR;
177 } REQ_Modify_Maxdelaydevratio;
178
179 typedef struct {
180 IPAddr address;
181 int32_t new_min_stratum;
182 int32_t EOR;
183 } REQ_Modify_Minstratum;
184
185 typedef struct {
186 IPAddr address;
187 int32_t new_poll_target;
188 int32_t EOR;
189 } REQ_Modify_Polltarget;
190
191 typedef struct {
192 Float new_max_update_skew;
193 int32_t EOR;
194 } REQ_Modify_Maxupdateskew;
195
196 typedef struct {
197 int32_t limit;
198 Float threshold;
199 int32_t EOR;
200 } REQ_Modify_Makestep;
201
202 typedef struct {
203 Timeval ts;
204 int32_t EOR;
205 } REQ_Logon;
206
207 typedef struct {
208 Timeval ts;
209 int32_t EOR;
210 } REQ_Settime;
211
212 typedef struct {
213 int32_t on_off;
214 int32_t stratum;
215 int32_t EOR;
216 } REQ_Local;
217
218 typedef struct {
219 int32_t option;
220 int32_t EOR;
221 } REQ_Manual;
222
223 typedef struct {
224 int32_t index;
225 int32_t EOR;
226 } REQ_Source_Data;
227
228 typedef struct {
229 IPAddr ip;
230 int32_t subnet_bits;
231 int32_t EOR;
232 } REQ_Allow_Deny;
233
234 typedef struct {
235 IPAddr ip;
236 int32_t EOR;
237 } REQ_Ac_Check;
238
239 /* Flags used in NTP source requests */
240 #define REQ_ADDSRC_ONLINE 0x1
241 #define REQ_ADDSRC_AUTOOFFLINE 0x2
242 #define REQ_ADDSRC_IBURST 0x4
243 #define REQ_ADDSRC_PREFER 0x8
244 #define REQ_ADDSRC_NOSELECT 0x10
245
246 typedef struct {
247 IPAddr ip_addr;
248 uint32_t port;
249 int32_t minpoll;
250 int32_t maxpoll;
251 int32_t presend_minpoll;
252 uint32_t authkey;
253 Float max_delay;
254 Float max_delay_ratio;
255 uint32_t flags;
256 int32_t EOR;
257 } REQ_NTP_Source;
258
259 typedef struct {
260 IPAddr ip_addr;
261 int32_t EOR;
262 } REQ_Del_Source;
263
264 typedef struct {
265 Float dfreq;
266 int32_t EOR;
267 } REQ_Dfreq;
268
269 typedef struct {
270 int32_t sec;
271 int32_t usec;
272 int32_t EOR;
273 } REQ_Doffset;
274
275 typedef struct {
276 uint32_t index;
277 int32_t EOR;
278 } REQ_Sourcestats;
279
280 /* This is based on the response size rather than the
281 request size */
282 #define MAX_CLIENT_ACCESSES 8
283
284 typedef struct {
285 uint32_t first_index;
286 uint32_t n_indices;
287 int32_t EOR;
288 } REQ_ClientAccessesByIndex;
289
290 typedef struct {
291 int32_t index;
292 int32_t EOR;
293 } REQ_ManualDelete;
294
295 typedef struct {
296 Float distance;
297 int32_t EOR;
298 } REQ_ReselectDistance;
299
300 /* ================================================== */
301
302 #define PKT_TYPE_CMD_REQUEST 1
303 #define PKT_TYPE_CMD_REPLY 2
304
305 /* This version number needs to be incremented whenever the packet
306 size and/or the format of any of the existing messages is changed.
307 Other changes, e.g. new command types, should be handled cleanly by
308 client.c and cmdmon.c anyway, so the version can stay the same.
309
310 Version 1 : original version with fixed size packets
311
312 Version 2 : both command and reply packet sizes made capable of
313 being variable length.
314
315 Version 3 : NTP_Source message lengthened (auto_offline)
316
317 Version 4 : IPv6 addressing added, 64-bit time values, sourcestats
318 and tracking reports extended, added flags to NTP source request,
319 trimmed source report, replaced fixed-point format with floating-point
320 and used also instead of integer microseconds, new commands: modify stratum,
321 modify polltarget, modify maxdelaydevratio, reselect, reselectdistance
322
323 Version 5 : auth data moved to the end of the packet to allow hashes with
324 different sizes, extended sources, tracking and activity reports, dropped
325 subnets accessed and client accesses
326
327 Version 6 : added padding to requests to prevent amplification attack,
328 changed maximum number of samples in manual list to 16, new commands: modify
329 makestep
330 */
331
332 #define PROTO_VERSION_NUMBER 6
333
334 /* The oldest protocol versions that are compatible enough with the current
335 version to report a version mismatch for the server and the client */
336 #define PROTO_VERSION_MISMATCH_COMPAT_SERVER 5
337 #define PROTO_VERSION_MISMATCH_COMPAT_CLIENT 4
338
339 /* The first protocol version using padding in requests */
340 #define PROTO_VERSION_PADDING 6
341
342 /* The maximum length of padding in request packet, currently
343 defined by CLIENT_ACCESSES_BY_INDEX and MANUAL_LIST */
344 #define MAX_PADDING_LENGTH 396
345
346 /* ================================================== */
347
348 typedef struct {
349 uint8_t version; /* Protocol version */
350 uint8_t pkt_type; /* What sort of packet this is */
351 uint8_t res1;
352 uint8_t res2;
353 uint16_t command; /* Which command is being issued */
354 uint16_t attempt; /* How many resends the client has done
355 (count up from zero for same sequence
356 number) */
357 uint32_t sequence; /* Client's sequence number */
358 uint32_t utoken; /* Unique token per incarnation of daemon */
359 uint32_t token; /* Command token (to prevent replay attack) */
360
361 union {
362 REQ_Null null;
363 REQ_Online online;
364 REQ_Offline offline;
365 REQ_Burst burst;
366 REQ_Modify_Minpoll modify_minpoll;
367 REQ_Modify_Maxpoll modify_maxpoll;
368 REQ_Dump dump;
369 REQ_Modify_Maxdelay modify_maxdelay;
370 REQ_Modify_Maxdelayratio modify_maxdelayratio;
371 REQ_Modify_Maxdelaydevratio modify_maxdelaydevratio;
372 REQ_Modify_Minstratum modify_minstratum;
373 REQ_Modify_Polltarget modify_polltarget;
374 REQ_Modify_Maxupdateskew modify_maxupdateskew;
375 REQ_Modify_Makestep modify_makestep;
376 REQ_Logon logon;
377 REQ_Settime settime;
378 REQ_Local local;
379 REQ_Manual manual;
380 REQ_Source_Data source_data;
381 REQ_Allow_Deny allow_deny;
382 REQ_Ac_Check ac_check;
383 REQ_NTP_Source ntp_source;
384 REQ_Del_Source del_source;
385 REQ_Dfreq dfreq;
386 REQ_Doffset doffset;
387 REQ_Sourcestats sourcestats;
388 REQ_ClientAccessesByIndex client_accesses_by_index;
389 REQ_ManualDelete manual_delete;
390 REQ_ReselectDistance reselect_distance;
391 } data; /* Command specific parameters */
392
393 /* The following fields only set the maximum size of the packet.
394 There are no holes between them and the actual data. */
395
396 /* Padding used to prevent traffic amplification */
397 uint8_t padding[MAX_PADDING_LENGTH];
398
399 /* Authentication data */
400 uint8_t auth[MAX_HASH_LENGTH];
401
402 } CMD_Request;
403
404 /* ================================================== */
405 /* Authority codes for command types */
406
407 #define PERMIT_OPEN 0
408 #define PERMIT_LOCAL 1
409 #define PERMIT_AUTH 2
410
411 /* ================================================== */
412
413 /* Reply codes */
414 #define RPY_NULL 1
415 #define RPY_N_SOURCES 2
416 #define RPY_SOURCE_DATA 3
417 #define RPY_MANUAL_TIMESTAMP 4
418 #define RPY_TRACKING 5
419 #define RPY_SOURCESTATS 6
420 #define RPY_RTC 7
421 #define RPY_SUBNETS_ACCESSED 8
422 #define RPY_CLIENT_ACCESSES 9
423 #define RPY_CLIENT_ACCESSES_BY_INDEX 10
424 #define RPY_MANUAL_LIST 11
425 #define RPY_ACTIVITY 12
426 #define RPY_SMOOTHING 13
427 #define N_REPLY_TYPES 14
428
429 /* Status codes */
430 #define STT_SUCCESS 0
431 #define STT_FAILED 1
432 #define STT_UNAUTH 2
433 #define STT_INVALID 3
434 #define STT_NOSUCHSOURCE 4
435 #define STT_INVALIDTS 5
436 #define STT_NOTENABLED 6
437 #define STT_BADSUBNET 7
438 #define STT_ACCESSALLOWED 8
439 #define STT_ACCESSDENIED 9
440 /* Deprecated */
441 #define STT_NOHOSTACCESS 10
442 #define STT_SOURCEALREADYKNOWN 11
443 #define STT_TOOMANYSOURCES 12
444 #define STT_NORTC 13
445 #define STT_BADRTCFILE 14
446 #define STT_INACTIVE 15
447 #define STT_BADSAMPLE 16
448 #define STT_INVALIDAF 17
449 #define STT_BADPKTVERSION 18
450 #define STT_BADPKTLENGTH 19
451
452 typedef struct {
453 int32_t EOR;
454 } RPY_Null;
455
456 typedef struct {
457 uint32_t n_sources;
458 int32_t EOR;
459 } RPY_N_Sources;
460
461 #define RPY_SD_MD_CLIENT 0
462 #define RPY_SD_MD_PEER 1
463 #define RPY_SD_MD_REF 2
464
465 #define RPY_SD_ST_SYNC 0
466 #define RPY_SD_ST_UNREACH 1
467 #define RPY_SD_ST_FALSETICKER 2
468 #define RPY_SD_ST_JITTERY 3
469 #define RPY_SD_ST_CANDIDATE 4
470 #define RPY_SD_ST_OUTLIER 5
471
472 #define RPY_SD_FLAG_NOSELECT 0x1
473 #define RPY_SD_FLAG_PREFER 0x2
474
475 typedef struct {
476 IPAddr ip_addr;
477 int16_t poll;
478 uint16_t stratum;
479 uint16_t state;
480 uint16_t mode;
481 uint16_t flags;
482 uint16_t reachability;
483 uint32_t since_sample;
484 Float orig_latest_meas;
485 Float latest_meas;
486 Float latest_meas_err;
487 int32_t EOR;
488 } RPY_Source_Data;
489
490 typedef struct {
491 uint32_t ref_id;
492 IPAddr ip_addr;
493 uint16_t stratum;
494 uint16_t leap_status;
495 Timeval ref_time;
496 Float current_correction;
497 Float last_offset;
498 Float rms_offset;
499 Float freq_ppm;
500 Float resid_freq_ppm;
501 Float skew_ppm;
502 Float root_delay;
503 Float root_dispersion;
504 Float last_update_interval;
505 int32_t EOR;
506 } RPY_Tracking;
507
508 typedef struct {
509 uint32_t ref_id;
510 IPAddr ip_addr;
511 uint32_t n_samples;
512 uint32_t n_runs;
513 uint32_t span_seconds;
514 Float sd;
515 Float resid_freq_ppm;
516 Float skew_ppm;
517 Float est_offset;
518 Float est_offset_err;
519 int32_t EOR;
520 } RPY_Sourcestats;
521
522 typedef struct {
523 Timeval ref_time;
524 uint16_t n_samples;
525 uint16_t n_runs;
526 uint32_t span_seconds;
527 Float rtc_seconds_fast;
528 Float rtc_gain_rate_ppm;
529 int32_t EOR;
530 } RPY_Rtc;
531
532 typedef struct {
533 uint32_t centiseconds;
534 Float dfreq_ppm;
535 Float new_afreq_ppm;
536 int32_t EOR;
537 } RPY_ManualTimestamp;
538
539 typedef struct {
540 IPAddr ip;
541 uint32_t client_hits;
542 uint32_t peer_hits;
543 uint32_t cmd_hits_auth;
544 uint32_t cmd_hits_normal;
545 uint32_t cmd_hits_bad;
546 uint32_t last_ntp_hit_ago;
547 uint32_t last_cmd_hit_ago;
548 } RPY_ClientAccesses_Client;
549
550 typedef struct {
551 uint32_t n_indices; /* how many indices there are in the server's table */
552 uint32_t next_index; /* the index 1 beyond those processed on this call */
553 uint32_t n_clients; /* the number of valid entries in the following array */
554 RPY_ClientAccesses_Client clients[MAX_CLIENT_ACCESSES];
555 int32_t EOR;
556 } RPY_ClientAccessesByIndex;
557
558 #define MAX_MANUAL_LIST_SAMPLES 16
559
560 typedef struct {
561 Timeval when;
562 Float slewed_offset;
563 Float orig_offset;
564 Float residual;
565 } RPY_ManualListSample;
566
567 typedef struct {
568 uint32_t n_samples;
569 RPY_ManualListSample samples[MAX_MANUAL_LIST_SAMPLES];
570 int32_t EOR;
571 } RPY_ManualList;
572
573 typedef struct {
574 int32_t online;
575 int32_t offline;
576 int32_t burst_online;
577 int32_t burst_offline;
578 int32_t unresolved;
579 int32_t EOR;
580 } RPY_Activity;
581
582 #define RPY_SMT_FLAG_ACTIVE 0x1
583 #define RPY_SMT_FLAG_LEAPONLY 0x2
584
585 typedef struct {
586 uint32_t flags;
587 Float offset;
588 Float freq_ppm;
589 Float wander_ppm;
590 Float last_update_ago;
591 Float remaining_time;
592 int32_t EOR;
593 } RPY_Smoothing;
594
595 typedef struct {
596 uint8_t version;
597 uint8_t pkt_type;
598 uint8_t res1;
599 uint8_t res2;
600 uint16_t command; /* Which command is being replied to */
601 uint16_t reply; /* Which format of reply this is */
602 uint16_t status; /* Status of command processing */
603 uint16_t pad1; /* Padding for compatibility and 4 byte alignment */
604 uint16_t pad2;
605 uint16_t pad3;
606 uint32_t sequence; /* Echo of client's sequence number */
607 uint32_t utoken; /* Unique token per incarnation of daemon */
608 uint32_t token; /* New command token (only if command was successfully
609 authenticated) */
610 union {
611 RPY_Null null;
612 RPY_N_Sources n_sources;
613 RPY_Source_Data source_data;
614 RPY_ManualTimestamp manual_timestamp;
615 RPY_Tracking tracking;
616 RPY_Sourcestats sourcestats;
617 RPY_Rtc rtc;
618 RPY_ClientAccessesByIndex client_accesses_by_index;
619 RPY_ManualList manual_list;
620 RPY_Activity activity;
621 RPY_Smoothing smoothing;
622 } data; /* Reply specific parameters */
623
624 /* authentication of the packet, there is no hole after the actual data
625 from the data union, this field only sets the maximum auth size */
626 uint8_t auth[MAX_HASH_LENGTH];
627
628 } CMD_Reply;
629
630 /* ================================================== */
631
632 #endif /* GOT_CANDM_H */