option-tester: Add many changes
[ntpsec.git] / docs / mode6.adoc
blob73450034aa69814b38baa624005bd4601f7cfc6d
1 = Mode 6 Protocol
2 include::include-html.ad[]
4 [cols="10%,90%",frame="none",grid="none",style="verse"]
5 |==============================
6 |image:pic/which-way-is.up.jpg[]|
7 {millshome}pictures.html[from 'Pogo', Walt Kelly]
9 Keeping control of the situation.
11 |==============================
13 == Related Links
15 include::includes/hand.adoc[]
17 == Table of Contents
19 * link:#intro[Introduction]
20 * link:#packet[Mode 6 packet structure]
21 * link:#varlists[Variable-Value Lists]
22 * link:#requests[Mode 6 Requests]
23 * link:#authentication[Authentication]
25 '''''
27 [[intro]]
28 == Introduction
30 This page describes the Mode 6 protocol used to get status information
31 from a running ntpd and configure some of its behaviors on the fly.
32 The protocol is normally used by the 'ntpq' and 'ntpmon' program
33 distributed with the suite. It is fully documented here so that other
34 clients can be written.
36 (Note that the most efficient way to write a new client is to use
37 the Python Mode 6 libraries included in the distribution. Both 'ntpq'
38 and 'ntpmon' use these.)
40 [[packet]]
41 == Mode 6 packet structure
43 The protocol uses UDP packets transmitted and received over port 123.
44 They use the same structure (header, plus extension, plus optional
45 MAC) as time synchronization messages, but the layout and semantics of
46 the header fields are different.  They are distinguished from time
47 synchronization packets by their Mode field, which has the value 6
48 (110).
50 .Mode 6 packet structure
51 -------------------------------------------------------------------------
52        0                   1                   2                   3
53        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
54       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55       |LI | VN  |Mode |R|E|M| Opcode  |         Sequence              |
56       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57       |             Status            |      Association ID           |
58       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59       |             Offset            |          Count                |
60       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61       |                                                               |
62       .                                                               .
63       .                  Payload (variable length)                    .
64       .                                                               .
65       |                                                               |
66       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67       |                          Key Identifier                       |
68       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69       |                                                               |
70       |                            MAC (128)                          |
71       |                                                               |
72       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 -------------------------------------------------------------------------
75 In mode 6 packets, the leap indicator (LI) is ignored and normally
76 zero.  The version (VN) is the NTP protocol major version, currently
77 4.  Mode is 6.  The following field interpretations are specific to
78 mode 6:
80 |=====================================================================
81 | Response bit   | 1 in a response, 0 in a request
82 | Error bit      | 1 in an error response, 0 otherwise
83 | More           | 1 if payload is continued in next packet, 0 otherwise
84 | Sequence       | Sequence number for multi-packet reassembly
85 | Status         | System status word
86 | Association ID | Association ID of peer, or 0 for the ntpd host
87 | Offset         | Octet offset of this fragment in the response
88 | Count          | Octet count of fragment payload
89 |=====================================================================
91 Requests to ntpd are single UDP packets; ntpd expects them to be
92 padded to a 8-octet boundary.  Responses may be multiple UDP packets;
93 they may arrive out of order, and the client is responsible for
94 reassembling the payloads.
96 All multibyte numeric fields are interpreted as big-endian
97 2's-complement integers.
99 The maximum length of the Mode 6 payload is constrained by the
100 minimum-maximum UDP payload size of 576. As of late 2018 there is no
101 language in the NTP RFCs pinning it down. A draft RFC on Mode 6 says
102 it's 500 octets, which is far in excess of any plausible request or
103 response size in the actual protocol.
105 [[varlists]]
106 == Variable-Value Lists
108 Several requests and responses (in fact, all but one) use a common
109 textual payload format consisting of a comma-separated list of items.
110 An item may be a textual (ASCII) variable name, or a textual variable
111 name followed by an equals sign followed by a textual value. Following
112 any comma the format may insert a newline; these are not significant
113 to the meaning of the payload, but are placed so that if the payload
114 is dumped to an 80 character terminal window the lines will be folded
115 in a way convenient for visibility.
117 Values may be decimal numeric literals, decimal float literals, hex
118 numeric literals led with "0x", binary literals consisting of exactly
119 two of digits 0 and 1, NTP date stamps, or string literals enclosed in
120 double quotes.
122 NTP date stamps are represented by hexadecimal fixed-point literals
123 led with "0x", distinguished from hexadecimal integers by the embedded
124 radix point (".").  They represent seconds (and fractional seconds)
125 since the epoch of the current NTP era.  NTP era zero began at 00:00
126 of January 1st 1900.
128 String literals never contain newlines or other control
129 characters. One quirk of the format is that a bare variable name
130 without a following "=" may be interpreted in context as an
131 instruction to set a string-valued variable to the empty string.
133 Textual responses may end with padding NULs; clients should ignore
134 these.
136 In RFC 5234 ABNF:
138 -----------------------------------------------------------
139 varlist = item [itemtail] LF *%x00
141 itemtail = "," [1LF] item [itemtail]
143 item = name / name "=" value
145 value = 1*DIGIT / 1*DIGIT "." 1*DIGIT  / "0x" 1*HEXDIG / 2BIT / quoted-string
147 quoted-string = %x22 *(%21 | %x23-7E) %x22
148 -----------------------------------------------------------
150 [[requests]]
151 == Mode 6 Requests
153 Request-response types are distinguished by operation codes.  The
154 table below lists them all. The "Auth?" column tells whether a
155 request requires authentication from the client.
157 [options="header"]
158 |=====================================================================
159 |Name                   |Value  | Auth? | Use
160 |CTL_OP_READSTAT        | 1     | No    | read system or peer status
161 |CTL_OP_READVAR         | 2     | No    | read system or peer variables
162 |CTL_OP_WRITEVAR        | 3     | Yes   | write variables
163 |CTL_OP_READCLOCK       | 4     | No    | read clock variables
164 |CTL_OP_WRITECLOCK      | 5     | -     | write clock variables
165 |CTL_OP_SETTRAP         | 6     | -     | set trap address (obsolete, unused)
166 |CTL_OP_ASYNCMSG        | 7     | -     | asynchronous message (unused)
167 |CTL_OP_CONFIGURE       | 8     | Yes   | runtime configuration
168 |CTL_OP_READ_MRU        | 10    | No    | retrieve MRU (mrulist)
169 |CTL_OP_READ_ORDLIST_A  | 11    | Yes   | ordered list req. auth.
170 |CTL_OP_REQ_NONCE       | 12    | No    | request a client nonce
171 |CTL_OP_UNSETTRAP       | 31    | -     | unset trap (obsolete, unused)
172 |=====================================================================
174 The CTL_OP_SETTRAP and CTL_OP_UNSETTRAP opcodes relate to an obsolete
175 notification facility supported in some older versions of the
176 software. CTL_OP_WRITECLOCK is unimplemented and will throw a
177 permission error. CTL_OP_ASYNCMSG is reserved for expansion. The
178 remaining opcodes are as follows:
180 === CTL_OP_READSTAT
182 This requests ntpd to ship up a list of association IDs and status
183 words for all peers currently associated with the ntpd instance. It
184 does not require authentication.
186 The normal use case for this request is to discover the current list
187 of associations preparatory to querying peer variables for each
188 association.
190 There is no request payload.  The response payload is not textual.  It
191 consists of a sequence of pairs, each consisting of 16-bit association
192 ID followed by 16-bit status word, both unsigned in network
193 (big-endian) byte order.  There is no padding in the response. The
194 number of IDs is implicitly given by the payload length in octets,
195 divided by 4.
197 Interpretation of the peer status word is described
198 link:decode.html#peer[here].
200 === CTL_OP_READVAR
202 This requests ntpd to ship up a list of peer variable settings for an
203 association specified by ID, or system variables if the ID is zero. It
204 does not require authentication.
206 The request payload may be empty or a textual variable list of
207 variables to be reported in the response. An empty request payload
208 calls for a report on all known variables.
210 The response payload is a textual varlist.
212 === CTL_OP_WRITEVAR
214 Some system variable are defined as being settable from a mode 6
215 client. This request provides a general way to do that. It requires
216 authentication.
218 The request payload is a textual list of variable settings.  Any
219 variable name that is unknown or not settable immediately terminates
220 processing of the payload. This request is only valid with an
221 association ID of 0. There is no response payload.
223 No system variables are currently settable.  This may change in a
224 future release.
226 === CTL_OP_READCLOCK
228 This requests ntpd to ship up a list of peer variable settings for a
229 reference clock association specified by ID. It does not require
230 authentication.
232 The request payload may be empty or a textual variable list of
233 variables to be reported in the response. An empty request payload
234 calls for a report on all known variables.
236 The response payload is a textual varlist.
238 === CTL_OP_CONFIGURE
240 This request is used to change the configuration of ntpd without
241 restarting the daemon.  It requires authentication.
243 The request payload should be a line of text in the syntax of the
244 ntp.conf configuration file.  The response payload will begin with either
245 an error message or the string "Config Succeeded", followed by a NUL.
247 Note: Due to an implementation error, the response packet may and
248 typically will contain trailing garbage. Implementations should
249 expect this and truncate it at the first NUL.
251 === CTL_OP_READ_MRU
253 This request is used to retrieve information about recent traffic
254 between ntpd and its clients and peers; in NTP-speak this traffic summary is
255 called the "MRU list", where MRU stands for "most recently used".  It does
256 not require authentication.
258 //Keep this in sync with the big comment in ntpd/ntp_control.c,
259 //from which it is derived.
261 A finite and variable number of entries are retrieved per request, to
262 avoid having responses with such large numbers of packets that socket
263 buffers are overflowed and packets lost.  The entries are retrieved
264 oldest-first, taking into account that the MRU list will be changing
265 between each request.  We can expect to see duplicate entries for
266 addresses updated in the MRU list during the fetch operation.  In the
267 end, the client can assemble a close approximation of the MRU list at
268 the point in time the last response was sent by ntpd.  The only
269 difference is it may be longer, containing some number of oldest
270 entries which have since been reclaimed.  If necessary, the protocol
271 could be extended to zap those from the client snapshot at the end,
272 but so far that doesn't seem useful.
274 To accommodate the changing MRU list, the starting point for requests
275 after the first request is supplied as a series of last seen
276 timestamps and associated addresses, the newest ones the client has
277 received.  As long as at least one of those entries hasn't been
278 bumped to the head of the MRU list, ntpd can pick up at that point.
279 Otherwise, the request is failed and it is up to ntpq to back up and
280 provide the next newest entry's timestamps and addresses, conceivably
281 backing up all the way to the starting point.
283 The request payload is a textual varlist that must include
284 some of the following variables and may include others:
286 nonce::         Regurgitated nonce retrieved by the client
287                 previously using CTL_OP_REQ_NONCE, demonstrating
288                 ability to receive traffic sent to its address.
290 frags::         Limit on datagrams (fragments) in response.  Used
291                 by newer ntpq versions instead of limit= when
292                 retrieving multiple entries.
294 limit::         Limit on MRU entries returned.  One of frags= or limit=
295                 must be provided.  limit=1 is a special case: Instead of
296                 fetching beginning with the supplied starting point's
297                 newer neighbor, fetch the supplied entry, and in that
298                 case the #.last timestamp can be zero.  This enables
299                 fetching a single entry by IP address.  When limit is
300                 not one and frags= is provided, the fragment limit
301                 controls.
303 mincount::      (decimal) Return entries with packet count >= mincount.
305 mindrop::       (decimal) Return entries with drop count >= mindrop.
307 minscore::      (float) Return entries with score >= minscore.
309 maxlstint::     (decimal seconds) Return entries with lstint <= maxlstint.
310                 (lstint is now-time of most recent packet)
312 minlstint::     (decimal seconds) Return entries with lstint >= minlstint.
313                 (lstint is now-time of most recent packet)
315 laddr::         Return entries associated with the server's IP
316                 address given.  No port specification is needed,
317                 and any supplied is ignored.
319 recent::        Set the reporting start point to retrieve roughly
320                 a specified number of most recent entries
321                 'Roughly' because the logic cannot anticipate
322                 update volume. Use this to volume-limit the
323                 response when you are monitoring something like
324                 a pool server with a very long MRU list.
326 resall::        0x-prefixed hex restrict bits, which must all be
327                 lit for an MRU entry to be included.
328                 Has precedence over any resany=.
330 resany::        0x-prefixed hex restrict bits, at least one of
331                 which must be list for an MRU entry to be
332                 included.
334 last.0::        0x-prefixed hex l_fp timestamp of newest entry
335                 which client previously received.
337 addr.0::        text of newest entry's IP address and port,
338                 IPv6 addresses in bracketed form: [::]:123
340 last.1::        timestamp of 2nd newest entry client has.
342 addr.1::        address of 2nd newest entry.
344 More entries may follow; ntpq provides as many last/addr pairs as will
345 fit in a single request packet, except for the first request in a MRU
346 fetch operation.
348 The response begins with a new nonce value to be used for any
349 followup request.  Following the nonce is the next newer entry than
350 referred to by last.0 and addr.0, if the "0" entry has not been
351 bumped to the front.  If it has, the first entry returned will be the
352 next entry newer than referred to by last.1 and addr.1, and so on.
353 If none of the referenced entries remain unchanged, the request fails
354 and ntpq backs up to the next earlier set of entries to resync.
356 Except for the first response, each response begins with confirmation
357 of the entry that precedes the first additional entry provided:
359 last.older::    hex l_fp timestamp matching one of the input
360                 .last timestamps, which entry now precedes the
361                 response 0. entry in the MRU list.
363 addr.older::    text of address corresponding to older.last.
365 And in any case, a successful response contains sets of values
366 comprising entries, with the oldest numbered 0 and incrementing from
367 there:
369 addr.#::        text of IPv4 or IPv6 address and port
371 last.#::        hex l_fp timestamp of last receipt
373 first.#::       hex l_fp timestamp of first receipt
375 ct.#::          count of packets received
377 mv.#::          mode and version
379 rs.#::          restriction mask (RES_* bits)
381 The client should accept the values in any order, and ignore .#
382 values which it does not understand, to allow a smooth path to
383 future changes without requiring a new opcode.  To ensure this,
384 ntpd occasionally issues a randomly-generated tag=value pair. All
385 such noise tags are three letters long.
387 Clients can rely on all *.0 values preceding any *.1 values, that is
388 all values for a given index number are together in the response.
390 The end of the response list is noted with one or two tag=value
391 pairs.  Unconditionally:
393 now::           0x-prefixed l_fp timestamp at the server marking
394                 the end of the operation.
396 If any entries were returned, now= is followed by:
398 last.newest::   hex l_fp identical to last.# of the prior entry.
400 Portions of the response side of the protocol (specifically the
401 last.older, addr.older, and last.newest attributes) can be ignored by a
402 client that is willing to accumulate an entire set of MRU list
403 fragments and then perform stale-record elimination of its own before
404 displaying or passing on the report (that is, as opposed to
405 incremental display with an attempt to suppress stale records on the
406 fly).
408 === CTL_OP_READ_ORDLIST_A
410 This request is used for two purposes: to retrieve restriction lists
411 and to retrieve interface statistics.  For the former use, the request
412 payload should be the string "addr_restrictions"; for the latter case,
413 the request payload should be "ifstats" or empty.  Both uses
414 require authentication.  The response payload is, in both cases, a
415 textual varlist.
417 A response payload consists of a list of attribute stanzas. Each
418 stanza consists of the attributes with tags of the form "name.#', with
419 # being replaced by a zero-origin integer literal that is the index of
420 the stanza.
422 Attributes within each stanza are deliberately issued in a random
423 order, and ntpd occasionally issues an attribute with a
424 randomly-generated name and value. This is an attempt to prevent Mode
425 6 clients from making brittle assumptions about the inventory of
426 attributes and their transmission order.
428 Clients can rely on all *.0 values preceding any *.1 values, that is
429 all values for a given index number are together in the response.
431 In a reslist stanza, elicited by "addr_restrictions", the elements are
432 as follows:
434 addr.#:: Address to which the restriction applies. May be IPV4 or
435          IPV6.  Has no port suffix
437 flags.#:: Space-separated list of flag names applying to the address.
438           These flag names are the same as those used in the
439           "restrict" directive of the configuration syntax.
441 hits.#:: The number of times this rule has been matched.  Not updated
442          for default rules.
444 mask.#:: Subnet mask qualifying the address to express a range.
446 In an ifstats stanza, elicited by "ifstats" or an empty string,
447 attributes are as follows:
449 addr.#:: Address of the interface. May be IPV4 or
450          IPV6. Has a port suffix.  May be a wildcard; extreme cases
451          are 0.0.0.0 and [::].
453 bcast.#:: Either a broadcast address associated with the interface or empty.
455 en.#:: Integer literal. 1 if packets on this interface are processed, 0
456        if they are to be ignored.
458 flags.#:: A hex literal that is a mask of flag bits on.
459           Flag mask values are described in a following table.
461 name.#:: The interface name, such as would occur in an ifconfig listing.
463 pc.#:: Count of peers using this interface.
465 rx.#:: Packet reception count.
467 tl.#:: Last time-to-live specified on a send.
469 tx.#:: Packet transmission count.
471 txerr.#:: Packet transmission error count.
473 up.#:: Uptime in seconds.
475 .Interface flag bits in the flags.# attribute
476 |==========================================================================
477 |INT_UP         | 0x001 | Interface is up
478 |INT_PPP        | 0x002 | Point-to-point interface
479 |INT_LOOPBACK   | 0x004 | the loopback interface
480 |INT_BROADCAST  | 0x008 | can broadcast out this interface
481 |INT_MULTICAST  | 0x010 | can multicast out this interface (not used)
482 |INT_BCASTOPEN  | 0x020 | broadcast receive socket is open
483 |INT_MCASTOPEN  | 0x040 | multicasting enabled (not used)
484 |INT_WILDCARD   | 0x080 | wildcard interface - usually skipped
485 |INT_MCASTIF    | 0x100 | bound directly to MCAST address
486 |INT_PRIVACY    | 0x200 | RFC 4941 IPv6 privacy address
487 |INT_BCASTXMIT  | 0x400 | socket setup to allow broadcasts
488 |==========================================================================
490 === CTL_OP_REQ_NONCE
492 This request is used to initialize an MRU-list conversation.  It
493 informs ntpd that the Mode 6 client is ready to receive.  It does
494 not require authentication.
496 The request retrieves a nonce specific to this client, which will be
497 played back to ntpd to demonstrate that the client is capable of
498 receiving responses at the source IP address that requested the nonce,
499 and is thereby unlikely to be forging the source address.  This check
500 prevents CTL_OP_READ_MRU from being used for flooding attacks.
502 The request has no payload.  The response will be a textual varlist
503 containing one string-valued variable, "nonce". The value need not by
504 interpreted by the client, only replayed as part of a following MRU-list
505 request.
507 Each nonce becomes invalid 16 seconds after the request for it was
508 received by ntpd. While the issue time is encoded in the nonce, it
509 is safer practice not to rely on the nonce format but instead to track
510 the last nonce transmission time in your client and re-request based
511 on that.
513 [[auth]]
514 == Authentication
516 Authenticated requests require a link:authentic.html#mac[MAC]
517 (message authentication code) trailer following the payload data, if
518 any. Pad Such requests to an 8-octet boundary, with those bytes not
519 included in the header count field.
521 Ordinary requests with MACs will not receive a MAC with the
522 response packet.
524 MD5 and SHA-1 are primarily available for legacy support.
525 MD5 is deprecated by RFC 8573 and not usable for MACs on FIPS 140-2
526 compliant systems.
528 == Compatibility Notes
530 The "recent" parameter of CTL_OP_READ_MRU is not supported in versions
531 prior to NTPsec 0.9.6.
533 Export of the count of control requests (ss_numctlreq) is new in NTPsec.
535 '''''
537 include::includes/footer.adoc[]