简介
本文使用协议为SNTPv4.
SNTP(Simple Network Time Protocal简单网络时间协议),用于跨广域网或局域网同步时间的协议,具有较高的精确度(几十毫秒)。官方文档为RFC 4330,2006年的最新版本为SNTPv4,比前面的版本增加了对IPV6的支持,此外还新增了一个Kiss-o’-Death(KoD)报文。
SNTPv4是NTP(Network Time Protocol网络时间协议)的简化版,精度较低,安全机制较低,但是报文格式完全一致。
SNTPv4使用的是UDP 123端口,C/S模式,有单播,多播和广播三种报文发送方式。
报文结构
基本结构
SNTP4基于UDP网络,报文结构如下:
MAC | IP | UDP | NTP |
---|
其中NTP的报文结构如下:
1 2 3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode | Stratum | Poll | Precision |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Delay |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Dispersion |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reference Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Reference Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Originate Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Receive Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Transmit Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Key Identifier (optional) (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| |
| Message Digest (optional) (128) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 1. NTP Packet Header
字段说明
字段 | 含义 | 长度 | 说明 |
---|---|---|---|
LI | 闰秒标志 | 2比特 | 见Figure 2 |
VN | 版本号 | 3比特 | 表示NTP的版本号,当前为4 |
Mode | 模式 | 3比特 | 见Figure 3,一般服务端发送的报文模式值为4 |
Stratum | 层 | 1字节unsigned char | 见Figure 4,对本地时钟级别的整体识别 |
Poll | 轮询间隔 | 1字节无符号正数 | 指示数据包的最大时间间隔,以秒为单位,作为2的指数方的指数部分,取值范围从4-17,即16秒到131,072秒 |
Precision | 精度 | 1字节有符号正数 | 指示系统时钟的精确性,以秒为单位,作为2的指数方的指数部分,取值范围从-6到-20 |
Root Delay | 往返延迟 | 4字节浮点数 | 指示与主时钟参考源的总共往返延迟,以秒为单位,小数部分在16位以后,取值范围从负几毫秒到正几百毫秒 |
Root Dispersion | 误差 | 4字节浮点数 | 指示与主时钟参考源的误差,以秒为单位,小数部分在16位以后,取值范围从零毫秒到正几百毫秒 |
Reference Identifier | 时钟参考源 | 4字节ASCII字符串 | 见Figure 5 ,对于二级服务器,在IPV4环境下,取值为一级服务器的IP地址,在IPV6环境下,是一级服务器的NSAP地址 |
Reference Timestamp | 校准时间 | 8字节时间戳 | 见Figure 6,指示系统时钟最后一次校准的时间,建议填充非重要的低阶位随机值,便于环路检测与回放检测 |
Originate Timestamp | 原始时间 | 8字节时间戳 | 指示客户向服务器发起请求的时间 |
Receive Timestamp | 接收时间 | 8字节时间戳 | 指服务器收到客户请求的时间 |
Transmit Timestamp | 传输时间 | 8字节时间戳 | 指示服务器向客户发时间戳的时间 |
Authenticator | 认证 | 160字节可选 | 当需要进行SNTP认证时,该字段包含密钥和信息加密码 |
Figure 2 LI
0:无警告 | 1:最后一分钟是61秒; |
---|---|
2:最后一分钟是59秒; | 3:警告(时钟没有同步) |
Figure 3 Mode
0:保留 | 1:对称主动 | 2:对称被动 | 3:客户 |
---|---|---|---|
4:服务器 | 5:广播 | 6:保留为NTP控制信息 | 7:保留为用户定义 |
Figure 4 Stratum
Stratum值 | 含义 |
---|---|
0 | kiss-o’-death(比较重要,见2.4),表示故障信息 |
1 | 一级服务器(由radio clock同步) |
2-15 | 二级服务器(由NTP和SNTP同步) |
16-255 | 保留 |
Figure 5 Reference Identifier Codes
Code | External Reference Source | 含义 |
---|---|---|
LOCL | uncalibrated local clock | 未校准的本地时钟 |
CESM | calibrated Cesium clock | 校准铯钟 |
RBDM | calibrated Rubidium clock | 校准铷钟 |
PPS | calibrated quartz clock or other pulse-per-second source | 校准石英钟或其他脉冲每秒源 |
IRIG | Inter-Range Instrumentation Group | 跨系列仪表组 |
ACTS | NIST telephone modem service | NIST(美国国家标准)电话调制解调器服务 |
USNO | USNO telephone modem service | USNO(美国海军天文台)电话调制解调器服务 |
PTB | PTB (Germany) telephone modem service | PTB(德国)电话调制解调器服务 |
TDF | Allouis (France) Radio 164 kHz | Allouis(法国)无线电 164 kHz |
DCF | Mainflingen (Germany) Radio 77.5 kHz | Mainflingen(德国)无线电 77.5 kHz |
MSF | Rugby (UK) Radio 60 kHz | Rugby(英国)无线电 60 kHz |
WWV | Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz | Ft. Collins(美国)无线电 2.5、5、10、15、20 MHz |
WWVB | Boulder (US) Radio 60 kHz | 博尔德(美国)无线电 60 kHz |
WWVH | Kauai Hawaii (US) Radio 2.5, 5, 10, 15 MHz | 夏威夷考艾岛(美国)无线电 2.5、5、10、15 MHz |
CHU | Ottawa (Canada) Radio 3330, 7335, 14670 kHz | 渥太华(加拿大)无线电 3330、7335、14670 kHz |
LORC | LORAN-C radionavigation system | LORAN-C无线电导航系统 |
OMEG | OMEGA radionavigation system | 欧米茄无线电导航系统 |
GPS | Global Positioning Service | 全球定位服务 |
1 2 3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Seconds |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Seconds Fraction (0-padded) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 6 NTP Timestamp Format
request报文
上述字段中如果是客户端发的报文,那么绝大部分是0;如果是服务端发的报文,大部分字段才有意义。
LI,Stratum, Precision, Root Delay, Root Dispersion, Reference Identifier, Reference Timestamp 等都只在服务端有效。
下面是不同传输方式下的字段值示例
Field Name | Unicast/Manycast | Broadcast | |
---|---|---|---|
Request | Reply | ||
LI | ignore | as needed | as needed |
VN | 1-4 | copied from request | 4 |
Mode | 3 | 4 | 5 |
Stratum | ignore | 1 | 1 |
Poll | ignore | copied from request | log2 poll interval |
Precision | ignore | -log2 server significant bits | -log2 server significant bits |
significant | significant | ||
Root Delay | ignore | 0 | 0 |
Root Dispersion | ignore | 0 | 0 |
Reference Identifier | ignore | source ident | source ident |
Reference Timestamp | ignore | time of last source update | time of last source update |
Originate Timestamp | ignore | copied from transmit timestamp | 0 |
Receive Timestamp | ignore | time of day | 0 |
Transmit Timestamp | (see text) | time of day | time of day |
Authenticator | optional | optional | optional |
Kiss-o’-Death
KoD是一种特殊的报文,用于组织客户端发送请求。当网络拥堵或者出现恶意攻击时需要产生该报文。该报文为SNTP版本4所特有。
一般情况下Stratum字段为1,表示以及服务器,后面的Reference Identifier表示时钟来源;KoD报文Stratum字段为0,后面的Reference Identifier成为kiss codes,表示终止原因。具体的code如下:
Code | Meaning | 说明 |
---|---|---|
ACST | The association belongs to a anycast server | 连接属于任意播服务器 |
AUTH | Server authentication failed | 服务器认证失败 |
AUTO | Autokey sequence failed | 自动键序列失败 |
BCST | The association belongs to a broadcast server | 关联属于广播服务器 |
CRYP | Cryptographic authentication or identification failed | 密码验证或识别失败 |
DENY | Access denied by remote server | 远程服务器拒绝访问 |
DROP | Lost peer in symmetric mode | 在对称模式下丢失对等点 |
RSTR | Access denied due to local policy | 由于本地策略访问被拒绝 |
INIT | The association has not yet synchronized for the first time | 第一次关联还没有同步 |
MCST | The association belongs to a manycast server | 关联属于多播服务器 |
NKEY | No key found. Either the key was never installed or is not trusted | 未找到密钥。密钥从未安装或不可信 |
RATE | Rate exceeded. The server has temporarily denied access because client exceeded the rate threshold | 速率过大。服务器暂时拒绝访问 |
RMOT | Somebody is tinkering with the association from a remote host running ntpdc. Not to worry unless some rascal has stolen your keys | 有人在远程修改关联 |
STEP | A step change in system time has occurred but the association has not yet resynchronized | 系统时间发生了阶跃变化,但关联尚未重新同步 |