1 #ifndef _IPXE_NTP_H
2 #define _IPXE_NTP_H
3 
4 /** @file
5  *
6  * Network Time Protocol
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/in.h>
14 #include <ipxe/interface.h>
15 
16 /** NTP port */
17 #define NTP_PORT 123
18 
19 /** An NTP short-format timestamp */
20 struct ntp_short {
21 	/** Seconds */
22 	uint16_t seconds;
23 	/** Fraction of a second */
24 	uint16_t fraction;
25 } __attribute__ (( packed ));
26 
27 /** An NTP timestamp */
28 struct ntp_timestamp {
29 	/** Seconds */
30 	uint32_t seconds;
31 	/** Fraction of a second */
32 	uint32_t fraction;
33 } __attribute__ (( packed ));
34 
35 /** An NTP reference identifier */
36 union ntp_id {
37 	/** Textual identifier */
38 	char text[4];
39 	/** IPv4 address */
40 	struct in_addr in;
41 	/** Opaque integer */
42 	uint32_t opaque;
43 };
44 
45 /** An NTP header */
46 struct ntp_header {
47 	/** Flags */
48 	uint8_t flags;
49 	/** Stratum */
50 	uint8_t stratum;
51 	/** Polling rate */
52 	int8_t poll;
53 	/** Precision */
54 	int8_t precision;
55 	/** Root delay */
56 	struct ntp_short delay;
57 	/** Root dispersion */
58 	struct ntp_short dispersion;
59 	/** Reference clock identifier */
60 	union ntp_id id;
61 	/** Reference timestamp */
62 	struct ntp_timestamp reference;
63 	/** Originate timestamp */
64 	struct ntp_timestamp originate;
65 	/** Receive timestamp */
66 	struct ntp_timestamp receive;
67 	/** Transmit timestamp */
68 	struct ntp_timestamp transmit;
69 } __attribute__ (( packed ));
70 
71 /** Leap second indicator: unknown */
72 #define NTP_FL_LI_UNKNOWN 0xc0
73 
74 /** NTP version: 1 */
75 #define NTP_FL_VN_1 0x20
76 
77 /** NTP mode: client */
78 #define NTP_FL_MODE_CLIENT 0x03
79 
80 /** NTP mode: server */
81 #define NTP_FL_MODE_SERVER 0x04
82 
83 /** NTP mode mask */
84 #define NTP_FL_MODE_MASK 0x07
85 
86 /** NTP timestamp for start of Unix epoch */
87 #define NTP_EPOCH 2208988800UL
88 
89 /** NTP fraction of a second magic value
90  *
91  * This is a policy decision.
92  */
93 #define NTP_FRACTION_MAGIC 0x69505845UL
94 
95 /** NTP minimum retransmission timeout
96  *
97  * This is a policy decision.
98  */
99 #define NTP_MIN_TIMEOUT ( 1 * TICKS_PER_SEC )
100 
101 /** NTP maximum retransmission timeout
102  *
103  * This is a policy decision.
104  */
105 #define NTP_MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
106 
107 extern int start_ntp ( struct interface *job, const char *hostname );
108 
109 #endif /* _IPXE_NTP_H */
110