2009-11-22 02:43:55 +00:00
|
|
|
/*
|
|
|
|
* NTP client/server, based on OpenNTPD 3.9p1
|
|
|
|
*
|
|
|
|
* Author: Adam Tkac <vonsch@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <netinet/ip.h> /* For IPTOS_LOWDELAY definition */
|
2009-11-25 13:52:47 +00:00
|
|
|
#ifndef IPTOS_LOWDELAY
|
|
|
|
# define IPTOS_LOWDELAY 0x10
|
|
|
|
#endif
|
2009-11-22 02:43:55 +00:00
|
|
|
#ifndef IP_PKTINFO
|
|
|
|
# error "Sorry, your kernel has to support IP_PKTINFO"
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
|
|
|
|
/* Sync to peers every N secs */
|
2009-12-17 22:29:33 +00:00
|
|
|
#define INTERVAL_QUERY_NORMAL 30
|
|
|
|
#define INTERVAL_QUERY_PATHETIC 60
|
|
|
|
#define INTERVAL_QUERY_AGRESSIVE 5
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
/* Bad if *less than* TRUSTLEVEL_BADPEER */
|
2009-12-17 22:29:33 +00:00
|
|
|
#define TRUSTLEVEL_BADPEER 6
|
|
|
|
#define TRUSTLEVEL_PATHETIC 2
|
|
|
|
#define TRUSTLEVEL_AGRESSIVE 8
|
|
|
|
#define TRUSTLEVEL_MAX 10
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
#define QSCALE_OFF_MIN 0.05
|
|
|
|
#define QSCALE_OFF_MAX 0.50
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-30 17:38:05 +00:00
|
|
|
/* Single query might take N secs max */
|
2009-12-17 22:29:33 +00:00
|
|
|
#define QUERYTIME_MAX 15
|
2009-12-02 22:17:45 +00:00
|
|
|
/* Min offset for settime at start. "man ntpd" says it's 128 ms */
|
2009-12-17 22:29:33 +00:00
|
|
|
#define STEPTIME_MIN_OFFSET 0.128
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t int_partl;
|
|
|
|
uint32_t fractionl;
|
|
|
|
} l_fixedpt_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t int_parts;
|
|
|
|
uint16_t fractions;
|
|
|
|
} s_fixedpt_t;
|
|
|
|
|
2009-11-24 06:07:42 +00:00
|
|
|
enum {
|
|
|
|
NTP_DIGESTSIZE = 16,
|
|
|
|
NTP_MSGSIZE_NOAUTH = 48,
|
|
|
|
NTP_MSGSIZE = (NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE),
|
|
|
|
};
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2009-12-02 00:51:24 +00:00
|
|
|
uint8_t m_status; /* status of local clock and leap info */
|
|
|
|
uint8_t m_stratum; /* stratum level */
|
|
|
|
uint8_t m_ppoll; /* poll value */
|
2009-12-30 17:38:05 +00:00
|
|
|
int8_t m_precision_exp;
|
2009-12-02 00:51:24 +00:00
|
|
|
s_fixedpt_t m_rootdelay;
|
|
|
|
s_fixedpt_t m_dispersion;
|
|
|
|
uint32_t m_refid;
|
|
|
|
l_fixedpt_t m_reftime;
|
|
|
|
l_fixedpt_t m_orgtime;
|
|
|
|
l_fixedpt_t m_rectime;
|
|
|
|
l_fixedpt_t m_xmttime;
|
|
|
|
uint32_t m_keyid;
|
|
|
|
uint8_t m_digest[NTP_DIGESTSIZE];
|
2009-12-30 17:38:05 +00:00
|
|
|
} msg_t;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
enum {
|
2009-12-17 22:29:33 +00:00
|
|
|
NTP_VERSION = 4,
|
|
|
|
NTP_MAXSTRATUM = 15,
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
/* Status Masks */
|
2009-12-17 22:29:33 +00:00
|
|
|
MODE_MASK = (7 << 0),
|
|
|
|
VERSION_MASK = (7 << 3),
|
|
|
|
VERSION_SHIFT = 3,
|
|
|
|
LI_MASK = (3 << 6),
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-30 17:38:05 +00:00
|
|
|
/* Leap Second Codes (high order two bits of m_status) */
|
|
|
|
LI_NOWARNING = (0 << 6), /* no warning */
|
|
|
|
LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */
|
|
|
|
LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */
|
|
|
|
LI_ALARM = (3 << 6), /* alarm condition */
|
|
|
|
|
2009-11-22 02:43:55 +00:00
|
|
|
/* Mode values */
|
2009-12-17 22:29:33 +00:00
|
|
|
MODE_RES0 = 0, /* reserved */
|
|
|
|
MODE_SYM_ACT = 1, /* symmetric active */
|
|
|
|
MODE_SYM_PAS = 2, /* symmetric passive */
|
|
|
|
MODE_CLIENT = 3, /* client */
|
|
|
|
MODE_SERVER = 4, /* server */
|
|
|
|
MODE_BROADCAST = 5, /* broadcast */
|
|
|
|
MODE_RES1 = 6, /* reserved for NTP control message */
|
|
|
|
MODE_RES2 = 7, /* reserved for private use */
|
2009-11-22 02:43:55 +00:00
|
|
|
};
|
|
|
|
|
2009-11-24 15:03:47 +00:00
|
|
|
#define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2009-12-17 22:29:33 +00:00
|
|
|
double d_offset;
|
|
|
|
double d_delay;
|
|
|
|
//UNUSED: double d_error;
|
2009-12-18 17:50:29 +00:00
|
|
|
time_t d_rcv_time;
|
2009-12-17 22:29:33 +00:00
|
|
|
uint32_t d_refid4;
|
|
|
|
uint8_t d_leap;
|
|
|
|
uint8_t d_stratum;
|
|
|
|
uint8_t d_good;
|
2009-12-30 17:38:05 +00:00
|
|
|
} datapoint_t;
|
2009-12-17 22:29:33 +00:00
|
|
|
|
|
|
|
#define NUM_DATAPOINTS 8
|
2009-11-22 02:43:55 +00:00
|
|
|
typedef struct {
|
2009-12-17 22:29:33 +00:00
|
|
|
len_and_sockaddr *p_lsa;
|
|
|
|
char *p_dotted;
|
|
|
|
/* When to send new query (if p_fd == -1)
|
|
|
|
* or when receive times out (if p_fd >= 0): */
|
|
|
|
time_t next_action_time;
|
|
|
|
int p_fd;
|
|
|
|
uint8_t p_datapoint_idx;
|
2009-12-30 17:38:05 +00:00
|
|
|
uint8_t p_trustlevel;
|
2009-12-17 22:29:33 +00:00
|
|
|
double p_xmttime;
|
2009-12-30 17:38:05 +00:00
|
|
|
datapoint_t update;
|
|
|
|
datapoint_t p_datapoint[NUM_DATAPOINTS];
|
|
|
|
msg_t p_xmt_msg;
|
|
|
|
} peer_t;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-23 15:27:16 +00:00
|
|
|
enum {
|
|
|
|
OPT_n = (1 << 0),
|
2009-12-02 22:17:45 +00:00
|
|
|
OPT_q = (1 << 1),
|
|
|
|
OPT_N = (1 << 2),
|
|
|
|
OPT_x = (1 << 3),
|
2009-11-23 15:27:16 +00:00
|
|
|
/* Insert new options above this line. */
|
|
|
|
/* Non-compat options: */
|
2009-11-25 13:52:47 +00:00
|
|
|
OPT_p = (1 << 4),
|
|
|
|
OPT_l = (1 << 5) * ENABLE_FEATURE_NTPD_SERVER,
|
2009-11-23 15:27:16 +00:00
|
|
|
};
|
|
|
|
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
struct globals {
|
2009-12-30 17:38:05 +00:00
|
|
|
/* total round trip delay to currently selected reference clock */
|
2009-12-17 22:29:33 +00:00
|
|
|
double rootdelay;
|
2009-12-30 17:38:05 +00:00
|
|
|
/* reference timestamp: time when the system clock was last set or corrected */
|
2009-12-17 22:29:33 +00:00
|
|
|
double reftime;
|
|
|
|
llist_t *ntp_peers;
|
2009-11-22 02:43:55 +00:00
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
2009-12-17 22:29:33 +00:00
|
|
|
int listen_fd;
|
2009-11-22 02:43:55 +00:00
|
|
|
#endif
|
2009-12-17 22:29:33 +00:00
|
|
|
unsigned verbose;
|
|
|
|
unsigned peer_cnt;
|
|
|
|
unsigned scale;
|
|
|
|
uint32_t refid;
|
|
|
|
uint32_t refid4;
|
|
|
|
uint8_t synced;
|
|
|
|
uint8_t leap;
|
2009-12-18 17:50:29 +00:00
|
|
|
#define G_precision_exp -6
|
|
|
|
// int8_t precision_exp;
|
2009-12-17 22:29:33 +00:00
|
|
|
uint8_t stratum;
|
|
|
|
uint8_t time_was_stepped;
|
|
|
|
uint8_t first_adj_done;
|
2009-11-22 02:43:55 +00:00
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
|
|
|
|
static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2009-12-30 17:38:05 +00:00
|
|
|
set_next(peer_t *p, unsigned t)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-02 22:17:45 +00:00
|
|
|
p->next_action_time = time(NULL) + t;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-02 22:17:45 +00:00
|
|
|
add_peers(char *s)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t *p;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
p = xzalloc(sizeof(*p));
|
2009-12-17 22:29:33 +00:00
|
|
|
p->p_lsa = xhost2sockaddr(s, 123);
|
|
|
|
p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa);
|
|
|
|
p->p_fd = -1;
|
2009-12-30 17:38:05 +00:00
|
|
|
p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
|
|
|
|
p->p_trustlevel = TRUSTLEVEL_PATHETIC;
|
2009-12-02 22:17:45 +00:00
|
|
|
p->next_action_time = time(NULL); /* = set_next(p, 0); */
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
llist_add_to(&G.ntp_peers, p);
|
|
|
|
G.peer_cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
2009-12-02 22:17:45 +00:00
|
|
|
gettime1900d(void)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-11-24 06:07:42 +00:00
|
|
|
struct timeval tv;
|
2009-11-22 02:43:55 +00:00
|
|
|
gettimeofday(&tv, NULL); /* never fails */
|
2009-11-24 15:03:47 +00:00
|
|
|
return (tv.tv_sec + 1.0e-6 * tv.tv_usec + OFFSET_1900_1970);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
d_to_tv(double d, struct timeval *tv)
|
|
|
|
{
|
|
|
|
tv->tv_sec = (long)d;
|
|
|
|
tv->tv_usec = (d - tv->tv_sec) * 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
lfp_to_d(l_fixedpt_t lfp)
|
|
|
|
{
|
2009-11-24 06:07:42 +00:00
|
|
|
double ret;
|
2009-11-22 02:43:55 +00:00
|
|
|
lfp.int_partl = ntohl(lfp.int_partl);
|
|
|
|
lfp.fractionl = ntohl(lfp.fractionl);
|
2009-11-24 06:07:42 +00:00
|
|
|
ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX);
|
2009-11-22 02:43:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
#if 0 //UNUSED
|
2009-11-22 02:43:55 +00:00
|
|
|
static double
|
|
|
|
sfp_to_d(s_fixedpt_t sfp)
|
|
|
|
{
|
2009-11-24 06:07:42 +00:00
|
|
|
double ret;
|
2009-11-22 02:43:55 +00:00
|
|
|
sfp.int_parts = ntohs(sfp.int_parts);
|
|
|
|
sfp.fractions = ntohs(sfp.fractions);
|
2009-11-24 06:07:42 +00:00
|
|
|
ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX);
|
2009-11-22 02:43:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2009-12-02 00:51:24 +00:00
|
|
|
#endif
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
2009-11-24 06:07:42 +00:00
|
|
|
static l_fixedpt_t
|
|
|
|
d_to_lfp(double d)
|
|
|
|
{
|
|
|
|
l_fixedpt_t lfp;
|
|
|
|
lfp.int_partl = (uint32_t)d;
|
|
|
|
lfp.fractionl = (uint32_t)((d - lfp.int_partl) * UINT_MAX);
|
|
|
|
lfp.int_partl = htonl(lfp.int_partl);
|
|
|
|
lfp.fractionl = htonl(lfp.fractionl);
|
|
|
|
return lfp;
|
|
|
|
}
|
|
|
|
|
2009-11-22 02:43:55 +00:00
|
|
|
static s_fixedpt_t
|
|
|
|
d_to_sfp(double d)
|
|
|
|
{
|
2009-11-24 06:07:42 +00:00
|
|
|
s_fixedpt_t sfp;
|
|
|
|
sfp.int_parts = (uint16_t)d;
|
|
|
|
sfp.fractions = (uint16_t)((d - sfp.int_parts) * USHRT_MAX);
|
|
|
|
sfp.int_parts = htons(sfp.int_parts);
|
|
|
|
sfp.fractions = htons(sfp.fractions);
|
2009-11-22 02:43:55 +00:00
|
|
|
return sfp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
static unsigned
|
2009-11-22 02:43:55 +00:00
|
|
|
error_interval(void)
|
|
|
|
{
|
2009-12-02 00:51:24 +00:00
|
|
|
unsigned interval, r;
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = INTERVAL_QUERY_PATHETIC * QSCALE_OFF_MAX / QSCALE_OFF_MIN;
|
2009-12-02 00:51:24 +00:00
|
|
|
r = (unsigned)random() % (unsigned)(interval / 10);
|
2009-11-22 02:43:55 +00:00
|
|
|
return (interval + r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-02 00:51:24 +00:00
|
|
|
do_sendto(int fd,
|
2009-11-22 02:43:55 +00:00
|
|
|
const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen,
|
2009-12-30 17:38:05 +00:00
|
|
|
msg_t *msg, ssize_t len)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (!from) {
|
2009-11-24 13:04:15 +00:00
|
|
|
ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen);
|
2009-11-22 02:43:55 +00:00
|
|
|
} else {
|
2009-11-24 13:04:15 +00:00
|
|
|
ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
if (ret != len) {
|
|
|
|
bb_perror_msg("send failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-30 17:38:05 +00:00
|
|
|
send_query_to_peer(peer_t *p)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-11-24 15:03:47 +00:00
|
|
|
// Why do we need to bind()?
|
|
|
|
// See what happens when we don't bind:
|
|
|
|
//
|
|
|
|
// socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
|
|
|
|
// setsockopt(3, SOL_IP, IP_TOS, [16], 4) = 0
|
|
|
|
// gettimeofday({1259071266, 327885}, NULL) = 0
|
|
|
|
// sendto(3, "xxx", 48, MSG_DONTWAIT, {sa_family=AF_INET, sin_port=htons(123), sin_addr=inet_addr("10.34.32.125")}, 16) = 48
|
|
|
|
// ^^^ we sent it from some source port picked by kernel.
|
|
|
|
// time(NULL) = 1259071266
|
|
|
|
// write(2, "ntpd: entering poll 15 secs\n", 28) = 28
|
|
|
|
// poll([{fd=3, events=POLLIN}], 1, 15000) = 1 ([{fd=3, revents=POLLIN}])
|
|
|
|
// recv(3, "yyy", 68, MSG_DONTWAIT) = 48
|
|
|
|
// ^^^ this recv will receive packets to any local port!
|
|
|
|
//
|
|
|
|
// Uncomment this and use strace to see it in action:
|
|
|
|
#define PROBE_LOCAL_ADDR // { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); }
|
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
if (p->p_fd == -1) {
|
2009-11-24 15:03:47 +00:00
|
|
|
int fd, family;
|
|
|
|
len_and_sockaddr *local_lsa;
|
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
family = p->p_lsa->u.sa.sa_family;
|
|
|
|
p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM);
|
2009-11-24 15:03:47 +00:00
|
|
|
/* local_lsa has "null" address and port 0 now.
|
|
|
|
* bind() ensures we have a *particular port* selected by kernel
|
2009-12-17 22:29:33 +00:00
|
|
|
* and remembered in p->p_fd, thus later recv(p->p_fd)
|
2009-11-24 15:03:47 +00:00
|
|
|
* receives only packets sent to this port.
|
|
|
|
*/
|
|
|
|
PROBE_LOCAL_ADDR
|
|
|
|
xbind(fd, &local_lsa->u.sa, local_lsa->len);
|
|
|
|
PROBE_LOCAL_ADDR
|
2009-11-22 02:43:55 +00:00
|
|
|
#if ENABLE_FEATURE_IPV6
|
2009-11-24 15:03:47 +00:00
|
|
|
if (family == AF_INET)
|
2009-11-22 02:43:55 +00:00
|
|
|
#endif
|
2009-11-24 15:03:47 +00:00
|
|
|
setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
|
|
|
|
free(local_lsa);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send out a random 64-bit number as our transmit time. The NTP
|
|
|
|
* server will copy said number into the originate field on the
|
|
|
|
* response that it sends us. This is totally legal per the SNTP spec.
|
|
|
|
*
|
|
|
|
* The impact of this is two fold: we no longer send out the current
|
|
|
|
* system time for the world to see (which may aid an attacker), and
|
|
|
|
* it gives us a (not very secure) way of knowing that we're not
|
|
|
|
* getting spoofed by an attacker that can't capture our traffic
|
|
|
|
* but can spoof packets from the NTP server we're communicating with.
|
|
|
|
*
|
|
|
|
* Save the real transmit timestamp locally.
|
|
|
|
*/
|
2009-12-30 17:38:05 +00:00
|
|
|
p->p_xmt_msg.m_xmttime.int_partl = random();
|
|
|
|
p->p_xmt_msg.m_xmttime.fractionl = random();
|
2009-12-17 22:29:33 +00:00
|
|
|
p->p_xmttime = gettime1900d();
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len,
|
2009-12-30 17:38:05 +00:00
|
|
|
&p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1
|
2009-12-02 00:51:24 +00:00
|
|
|
) {
|
2009-12-17 22:29:33 +00:00
|
|
|
close(p->p_fd);
|
|
|
|
p->p_fd = -1;
|
2009-11-22 02:43:55 +00:00
|
|
|
set_next(p, INTERVAL_QUERY_PATHETIC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
if (G.verbose)
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("sent query to %s", p->p_dotted);
|
2009-12-02 22:17:45 +00:00
|
|
|
set_next(p, QUERYTIME_MAX);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
|
|
|
|
/* Time is stepped only once, when the first packet from a peer is received.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
step_time_once(double offset)
|
|
|
|
{
|
2009-12-18 17:50:29 +00:00
|
|
|
double dtime;
|
2009-12-02 22:17:45 +00:00
|
|
|
llist_t *item;
|
|
|
|
struct timeval tv;
|
|
|
|
char buf[80];
|
|
|
|
time_t tval;
|
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
if (G.time_was_stepped)
|
2009-12-02 22:17:45 +00:00
|
|
|
goto bail;
|
2009-12-17 22:29:33 +00:00
|
|
|
G.time_was_stepped = 1;
|
2009-12-02 22:17:45 +00:00
|
|
|
|
|
|
|
/* if the offset is small, don't step, slew (later) */
|
|
|
|
if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL); /* never fails */
|
2009-12-18 17:50:29 +00:00
|
|
|
dtime = offset + tv.tv_sec;
|
|
|
|
dtime += 1.0e-6 * tv.tv_usec;
|
|
|
|
d_to_tv(dtime, &tv);
|
2009-12-02 22:17:45 +00:00
|
|
|
|
|
|
|
if (settimeofday(&tv, NULL) == -1)
|
|
|
|
bb_perror_msg_and_die("settimeofday");
|
|
|
|
|
|
|
|
tval = tv.tv_sec;
|
|
|
|
strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
|
|
|
|
|
|
|
|
bb_error_msg("setting clock to %s (offset %fs)", buf, offset);
|
|
|
|
|
|
|
|
for (item = G.ntp_peers; item != NULL; item = item->link) {
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t *p = (peer_t *) item->data;
|
2009-12-18 17:50:29 +00:00
|
|
|
p->next_action_time -= (time_t)offset;
|
2009-12-02 22:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bail:
|
|
|
|
if (option_mask32 & OPT_q)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Time is periodically slewed when we collect enough
|
|
|
|
* good data points.
|
|
|
|
*/
|
2009-11-22 02:43:55 +00:00
|
|
|
static int
|
2009-12-02 00:51:24 +00:00
|
|
|
compare_offsets(const void *aa, const void *bb)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-30 17:38:05 +00:00
|
|
|
const peer_t *const *a = aa;
|
|
|
|
const peer_t *const *b = bb;
|
2009-12-17 22:29:33 +00:00
|
|
|
if ((*a)->update.d_offset < (*b)->update.d_offset)
|
2009-11-22 02:43:55 +00:00
|
|
|
return -1;
|
2009-12-17 22:29:33 +00:00
|
|
|
return ((*a)->update.d_offset > (*b)->update.d_offset);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
2009-12-02 22:17:45 +00:00
|
|
|
static unsigned
|
2009-11-22 02:43:55 +00:00
|
|
|
updated_scale(double offset)
|
|
|
|
{
|
|
|
|
if (offset < 0)
|
|
|
|
offset = -offset;
|
|
|
|
if (offset > QSCALE_OFF_MAX)
|
|
|
|
return 1;
|
|
|
|
if (offset < QSCALE_OFF_MIN)
|
|
|
|
return QSCALE_OFF_MAX / QSCALE_OFF_MIN;
|
|
|
|
return QSCALE_OFF_MAX / offset;
|
|
|
|
}
|
|
|
|
static void
|
2009-12-02 00:51:24 +00:00
|
|
|
slew_time(void)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-02 12:43:06 +00:00
|
|
|
llist_t *item;
|
|
|
|
double offset_median;
|
|
|
|
struct timeval tv;
|
2009-11-24 13:04:15 +00:00
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
{
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t **peers = xzalloc(sizeof(peers[0]) * G.peer_cnt);
|
2009-12-02 12:43:06 +00:00
|
|
|
unsigned goodpeer_cnt = 0;
|
2009-12-02 00:51:24 +00:00
|
|
|
unsigned middle;
|
|
|
|
|
|
|
|
for (item = G.ntp_peers; item != NULL; item = item->link) {
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t *p = (peer_t *) item->data;
|
|
|
|
if (p->p_trustlevel < TRUSTLEVEL_BADPEER)
|
2009-12-02 00:51:24 +00:00
|
|
|
continue;
|
2009-12-17 22:29:33 +00:00
|
|
|
if (!p->update.d_good) {
|
2009-12-02 22:17:45 +00:00
|
|
|
free(peers);
|
2009-12-02 12:43:06 +00:00
|
|
|
return;
|
2009-12-02 22:17:45 +00:00
|
|
|
}
|
2009-12-02 12:43:06 +00:00
|
|
|
peers[goodpeer_cnt++] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (goodpeer_cnt == 0) {
|
|
|
|
free(peers);
|
|
|
|
goto clear_good;
|
2009-12-02 00:51:24 +00:00
|
|
|
}
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-02 12:43:06 +00:00
|
|
|
qsort(peers, goodpeer_cnt, sizeof(peers[0]), compare_offsets);
|
2009-12-02 00:51:24 +00:00
|
|
|
|
2009-12-02 12:43:06 +00:00
|
|
|
middle = goodpeer_cnt / 2;
|
|
|
|
if (middle != 0 && (goodpeer_cnt & 1) == 0) {
|
2009-12-17 22:29:33 +00:00
|
|
|
offset_median = (peers[middle-1]->update.d_offset + peers[middle]->update.d_offset) / 2;
|
|
|
|
G.rootdelay = (peers[middle-1]->update.d_delay + peers[middle]->update.d_delay) / 2;
|
|
|
|
G.stratum = 1 + MAX(peers[middle-1]->update.d_stratum, peers[middle]->update.d_stratum);
|
2009-12-02 00:51:24 +00:00
|
|
|
} else {
|
2009-12-17 22:29:33 +00:00
|
|
|
offset_median = peers[middle]->update.d_offset;
|
|
|
|
G.rootdelay = peers[middle]->update.d_delay;
|
|
|
|
G.stratum = 1 + peers[middle]->update.d_stratum;
|
2009-12-02 00:51:24 +00:00
|
|
|
}
|
2009-12-17 22:29:33 +00:00
|
|
|
G.leap = peers[middle]->update.d_leap;
|
|
|
|
G.refid4 = peers[middle]->update.d_refid4;
|
2009-12-02 00:51:24 +00:00
|
|
|
G.refid =
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
2009-12-17 22:29:33 +00:00
|
|
|
peers[middle]->p_lsa->u.sa.sa_family != AF_INET ?
|
2009-12-02 00:51:24 +00:00
|
|
|
G.refid4 :
|
|
|
|
#endif
|
2009-12-17 22:29:33 +00:00
|
|
|
peers[middle]->p_lsa->u.sin.sin_addr.s_addr;
|
2009-12-02 00:51:24 +00:00
|
|
|
free(peers);
|
2009-11-24 13:04:15 +00:00
|
|
|
}
|
2009-12-02 22:17:45 +00:00
|
|
|
//TODO: if (offset_median > BIG) step_time(offset_median)?
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
G.scale = updated_scale(offset_median);
|
|
|
|
|
|
|
|
bb_error_msg("adjusting clock by %fs, our stratum is %u, time scale %u",
|
|
|
|
offset_median, G.stratum, G.scale);
|
2009-12-02 00:51:24 +00:00
|
|
|
|
2009-12-01 01:32:01 +00:00
|
|
|
errno = 0;
|
2009-11-24 13:04:15 +00:00
|
|
|
d_to_tv(offset_median, &tv);
|
2009-12-02 22:17:45 +00:00
|
|
|
if (adjtime(&tv, &tv) == -1)
|
|
|
|
bb_perror_msg_and_die("adjtime failed");
|
|
|
|
if (G.verbose >= 2)
|
|
|
|
bb_error_msg("old adjust: %d.%06u", (int)tv.tv_sec, (unsigned)tv.tv_usec);
|
|
|
|
|
|
|
|
if (G.first_adj_done) {
|
|
|
|
uint8_t synced = (tv.tv_sec == 0 && tv.tv_usec == 0);
|
|
|
|
if (synced != G.synced) {
|
|
|
|
G.synced = synced;
|
|
|
|
bb_error_msg("clock is %ssynced", synced ? "" : "un");
|
2009-12-02 00:51:24 +00:00
|
|
|
}
|
2009-11-24 13:04:15 +00:00
|
|
|
}
|
2009-12-02 22:17:45 +00:00
|
|
|
G.first_adj_done = 1;
|
2009-11-24 13:04:15 +00:00
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
G.reftime = gettime1900d();
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
clear_good:
|
2009-11-22 02:43:55 +00:00
|
|
|
for (item = G.ntp_peers; item != NULL; item = item->link) {
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t *p = (peer_t *) item->data;
|
2009-12-17 22:29:33 +00:00
|
|
|
p->update.d_good = 0;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-30 17:38:05 +00:00
|
|
|
update_peer_data(peer_t *p)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-02 00:51:24 +00:00
|
|
|
/* Clock filter.
|
2009-12-17 22:29:33 +00:00
|
|
|
* Find the datapoint with the lowest delay.
|
2009-12-02 00:51:24 +00:00
|
|
|
* Use that as the peer update.
|
|
|
|
* Invalidate it and all older ones.
|
2009-11-22 02:43:55 +00:00
|
|
|
*/
|
2009-12-02 00:51:24 +00:00
|
|
|
int i;
|
2009-12-02 12:43:06 +00:00
|
|
|
int best = -1;
|
|
|
|
int good = 0;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
for (i = 0; i < NUM_DATAPOINTS; i++) {
|
|
|
|
if (p->p_datapoint[i].d_good) {
|
2009-11-22 02:43:55 +00:00
|
|
|
good++;
|
2009-12-17 22:29:33 +00:00
|
|
|
if (best < 0 || p->p_datapoint[i].d_delay < p->p_datapoint[best].d_delay)
|
2009-11-22 02:43:55 +00:00
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
if (good < 8) //FIXME: was it meant to be NUM_DATAPOINTS, not 8?
|
2009-11-22 02:43:55 +00:00
|
|
|
return;
|
|
|
|
|
2009-12-18 17:50:29 +00:00
|
|
|
p->update = p->p_datapoint[best]; /* struct copy */
|
2009-12-02 00:51:24 +00:00
|
|
|
slew_time();
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
for (i = 0; i < NUM_DATAPOINTS; i++)
|
2009-12-18 17:50:29 +00:00
|
|
|
if (p->p_datapoint[i].d_rcv_time <= p->p_datapoint[best].d_rcv_time)
|
2009-12-17 22:29:33 +00:00
|
|
|
p->p_datapoint[i].d_good = 0;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
static unsigned
|
|
|
|
scale_interval(unsigned requested)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-02 00:51:24 +00:00
|
|
|
unsigned interval, r;
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = requested * G.scale;
|
2009-12-02 00:51:24 +00:00
|
|
|
r = (unsigned)random() % (unsigned)(MAX(5, interval / 10));
|
2009-11-22 02:43:55 +00:00
|
|
|
return (interval + r);
|
|
|
|
}
|
|
|
|
static void
|
2009-12-30 17:38:05 +00:00
|
|
|
recv_and_process_peer_pkt(peer_t *p)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-12-30 17:38:05 +00:00
|
|
|
ssize_t size;
|
|
|
|
msg_t msg;
|
|
|
|
double T1, T2, T3, T4;
|
|
|
|
unsigned interval;
|
|
|
|
datapoint_t *datapoint;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 15:03:47 +00:00
|
|
|
/* We can recvfrom here and check from.IP, but some multihomed
|
|
|
|
* ntp servers reply from their *other IP*.
|
|
|
|
* TODO: maybe we should check at least what we can: from.port == 123?
|
|
|
|
*/
|
2009-12-17 22:29:33 +00:00
|
|
|
size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
|
2009-11-22 02:43:55 +00:00
|
|
|
if (size == -1) {
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_perror_msg("recv(%s) error", p->p_dotted);
|
2009-11-22 02:43:55 +00:00
|
|
|
if (errno == EHOSTUNREACH || errno == EHOSTDOWN
|
|
|
|
|| errno == ENETUNREACH || errno == ENETDOWN
|
|
|
|
|| errno == ECONNREFUSED || errno == EADDRNOTAVAIL
|
2009-11-24 13:04:15 +00:00
|
|
|
|| errno == EAGAIN
|
2009-11-22 02:43:55 +00:00
|
|
|
) {
|
|
|
|
//TODO: always do this?
|
|
|
|
set_next(p, error_interval());
|
2009-12-02 00:51:24 +00:00
|
|
|
goto close_sock;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("malformed packet received from %s", p->p_dotted);
|
2009-11-22 02:43:55 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-12-30 17:38:05 +00:00
|
|
|
if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl
|
|
|
|
|| msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl
|
2009-11-22 02:43:55 +00:00
|
|
|
) {
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
if ((msg.m_status & LI_ALARM) == LI_ALARM
|
|
|
|
|| msg.m_stratum == 0
|
|
|
|
|| msg.m_stratum > NTP_MAXSTRATUM
|
2009-11-22 02:43:55 +00:00
|
|
|
) {
|
2009-12-17 11:17:41 +00:00
|
|
|
// TODO: stratum 0 responses may have commands in 32-bit m_refid field:
|
|
|
|
// "DENY", "RSTR" - peer does not like us at all
|
|
|
|
// "RATE" - peer is overloaded, reduce polling freq
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = error_interval();
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("reply from %s: not synced, next query in %us", p->p_dotted, interval);
|
2009-12-02 00:51:24 +00:00
|
|
|
goto close_sock;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From RFC 2030 (with a correction to the delay math):
|
|
|
|
*
|
2009-12-17 22:29:33 +00:00
|
|
|
* Timestamp Name ID When Generated
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
* Originate Timestamp T1 time request sent by client
|
|
|
|
* Receive Timestamp T2 time request received by server
|
|
|
|
* Transmit Timestamp T3 time reply sent by server
|
|
|
|
* Destination Timestamp T4 time reply received by client
|
2009-11-22 02:43:55 +00:00
|
|
|
*
|
2009-12-17 22:29:33 +00:00
|
|
|
* The roundtrip delay and local clock offset are defined as
|
2009-11-22 02:43:55 +00:00
|
|
|
*
|
2009-12-17 22:29:33 +00:00
|
|
|
* delay = (T4 - T1) - (T3 - T2); offset = ((T2 - T1) + (T3 - T4)) / 2
|
2009-11-22 02:43:55 +00:00
|
|
|
*/
|
2009-12-17 22:29:33 +00:00
|
|
|
T1 = p->p_xmttime;
|
2009-12-02 00:51:24 +00:00
|
|
|
T2 = lfp_to_d(msg.m_rectime);
|
|
|
|
T3 = lfp_to_d(msg.m_xmttime);
|
2009-12-30 17:38:05 +00:00
|
|
|
T4 = gettime1900d();
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
datapoint = &p->p_datapoint[p->p_datapoint_idx];
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
datapoint->d_offset = ((T2 - T1) + (T3 - T4)) / 2;
|
|
|
|
datapoint->d_delay = (T4 - T1) - (T3 - T2);
|
|
|
|
if (datapoint->d_delay < 0) {
|
|
|
|
bb_error_msg("reply from %s: negative delay %f", p->p_dotted, datapoint->d_delay);
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = error_interval();
|
|
|
|
set_next(p, interval);
|
2009-12-02 00:51:24 +00:00
|
|
|
goto close_sock;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
2009-12-17 22:29:33 +00:00
|
|
|
//UNUSED: datapoint->d_error = (T2 - T1) - (T3 - T4);
|
2009-12-18 17:50:29 +00:00
|
|
|
datapoint->d_rcv_time = (time_t)(T4 - OFFSET_1900_1970); /* = time(NULL); */
|
2009-12-17 22:29:33 +00:00
|
|
|
datapoint->d_good = 1;
|
|
|
|
|
|
|
|
datapoint->d_leap = (msg.m_status & LI_MASK);
|
2009-12-30 17:38:05 +00:00
|
|
|
//UNUSED: datapoint->o_precision = msg.m_precision_exp;
|
2009-12-17 22:29:33 +00:00
|
|
|
//UNUSED: datapoint->o_rootdelay = sfp_to_d(msg.m_rootdelay);
|
|
|
|
//UNUSED: datapoint->o_rootdispersion = sfp_to_d(msg.m_dispersion);
|
|
|
|
//UNUSED: datapoint->d_refid = ntohl(msg.m_refid);
|
|
|
|
datapoint->d_refid4 = msg.m_xmttime.fractionl;
|
|
|
|
//UNUSED: datapoint->o_reftime = lfp_to_d(msg.m_reftime);
|
|
|
|
//UNUSED: datapoint->o_poll = msg.m_ppoll;
|
|
|
|
datapoint->d_stratum = msg.m_stratum;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-30 17:38:05 +00:00
|
|
|
if (p->p_trustlevel < TRUSTLEVEL_PATHETIC)
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = scale_interval(INTERVAL_QUERY_PATHETIC);
|
2009-12-30 17:38:05 +00:00
|
|
|
else if (p->p_trustlevel < TRUSTLEVEL_AGRESSIVE)
|
2009-11-22 02:43:55 +00:00
|
|
|
interval = scale_interval(INTERVAL_QUERY_AGRESSIVE);
|
|
|
|
else
|
|
|
|
interval = scale_interval(INTERVAL_QUERY_NORMAL);
|
|
|
|
|
|
|
|
set_next(p, interval);
|
|
|
|
|
2009-12-30 17:38:05 +00:00
|
|
|
/* Every received reply which we do not discard increases trust */
|
|
|
|
if (p->p_trustlevel < TRUSTLEVEL_MAX) {
|
|
|
|
p->p_trustlevel++;
|
|
|
|
if (p->p_trustlevel == TRUSTLEVEL_BADPEER)
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("peer %s now valid", p->p_dotted);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
if (G.verbose)
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("reply from %s: offset %f delay %f, next query in %us", p->p_dotted,
|
|
|
|
datapoint->d_offset, datapoint->d_delay, interval);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
update_peer_data(p);
|
2009-12-02 01:30:31 +00:00
|
|
|
//TODO: do it after all peers had a chance to return at least one reply?
|
2009-12-17 22:29:33 +00:00
|
|
|
step_time_once(datapoint->d_offset);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
p->p_datapoint_idx++;
|
|
|
|
if (p->p_datapoint_idx >= NUM_DATAPOINTS)
|
|
|
|
p->p_datapoint_idx = 0;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
close_sock:
|
2009-12-02 22:17:45 +00:00
|
|
|
/* We do not expect any more packets from this peer for now.
|
2009-12-02 00:51:24 +00:00
|
|
|
* Closing the socket informs kernel about it.
|
|
|
|
* We open a new socket when we send a new query.
|
|
|
|
*/
|
2009-12-17 22:29:33 +00:00
|
|
|
close(p->p_fd);
|
|
|
|
p->p_fd = -1;
|
2009-11-22 02:43:55 +00:00
|
|
|
bail:
|
2009-12-02 00:51:24 +00:00
|
|
|
return;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
|
|
|
static void
|
2009-11-24 13:04:15 +00:00
|
|
|
recv_and_process_client_pkt(void /*int fd*/)
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
2009-11-24 06:07:42 +00:00
|
|
|
ssize_t size;
|
|
|
|
uint8_t version;
|
|
|
|
double rectime;
|
|
|
|
len_and_sockaddr *to;
|
|
|
|
struct sockaddr *from;
|
2009-12-30 17:38:05 +00:00
|
|
|
msg_t msg;
|
2009-11-24 06:07:42 +00:00
|
|
|
uint8_t query_status;
|
|
|
|
uint8_t query_ppoll;
|
|
|
|
l_fixedpt_t query_xmttime;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
to = get_sock_lsa(G.listen_fd);
|
|
|
|
from = xzalloc(to->len);
|
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
size = recv_from_to(G.listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len);
|
2009-11-22 02:43:55 +00:00
|
|
|
if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
|
2009-11-24 06:07:42 +00:00
|
|
|
char *addr;
|
2009-11-24 13:04:15 +00:00
|
|
|
if (size < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
goto bail;
|
2009-12-02 22:17:45 +00:00
|
|
|
bb_perror_msg_and_die("recv");
|
2009-11-24 13:04:15 +00:00
|
|
|
}
|
2009-11-24 06:07:42 +00:00
|
|
|
addr = xmalloc_sockaddr2dotted_noport(from);
|
2009-12-02 22:17:45 +00:00
|
|
|
bb_error_msg("malformed packet received from %s: size %u", addr, (int)size);
|
2009-11-22 02:43:55 +00:00
|
|
|
free(addr);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-12-02 00:51:24 +00:00
|
|
|
query_status = msg.m_status;
|
|
|
|
query_ppoll = msg.m_ppoll;
|
|
|
|
query_xmttime = msg.m_xmttime;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 06:07:42 +00:00
|
|
|
/* Build a reply packet */
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
2009-12-02 00:51:24 +00:00
|
|
|
msg.m_status = G.synced ? G.leap : LI_ALARM;
|
|
|
|
msg.m_status |= (query_status & VERSION_MASK);
|
|
|
|
msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ?
|
2009-11-22 02:43:55 +00:00
|
|
|
MODE_SERVER : MODE_SYM_PAS;
|
2009-12-02 00:51:24 +00:00
|
|
|
msg.m_stratum = G.stratum;
|
|
|
|
msg.m_ppoll = query_ppoll;
|
2009-12-30 17:38:05 +00:00
|
|
|
msg.m_precision_exp = G_precision_exp;
|
2009-12-02 22:17:45 +00:00
|
|
|
rectime = gettime1900d();
|
2009-12-02 00:51:24 +00:00
|
|
|
msg.m_xmttime = msg.m_rectime = d_to_lfp(rectime);
|
|
|
|
msg.m_reftime = d_to_lfp(G.reftime);
|
2009-12-02 22:17:45 +00:00
|
|
|
//msg.m_xmttime = d_to_lfp(gettime1900d()); // = msg.m_rectime
|
2009-12-02 00:51:24 +00:00
|
|
|
msg.m_orgtime = query_xmttime;
|
|
|
|
msg.m_rootdelay = d_to_sfp(G.rootdelay);
|
2009-11-24 06:07:42 +00:00
|
|
|
version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */
|
2009-12-02 00:51:24 +00:00
|
|
|
msg.m_refid = (version > (3 << VERSION_SHIFT)) ? G.refid4 : G.refid;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
/* We reply from the local address packet was sent to,
|
2009-11-22 02:43:55 +00:00
|
|
|
* this makes to/from look swapped here: */
|
2009-12-02 00:51:24 +00:00
|
|
|
do_sendto(G.listen_fd,
|
2009-11-24 06:07:42 +00:00
|
|
|
/*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len,
|
|
|
|
&msg, size);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
bail:
|
|
|
|
free(to);
|
|
|
|
free(from);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Upstream ntpd's options:
|
|
|
|
*
|
|
|
|
* -4 Force DNS resolution of host names to the IPv4 namespace.
|
|
|
|
* -6 Force DNS resolution of host names to the IPv6 namespace.
|
|
|
|
* -a Require cryptographic authentication for broadcast client,
|
|
|
|
* multicast client and symmetric passive associations.
|
|
|
|
* This is the default.
|
|
|
|
* -A Do not require cryptographic authentication for broadcast client,
|
|
|
|
* multicast client and symmetric passive associations.
|
|
|
|
* This is almost never a good idea.
|
|
|
|
* -b Enable the client to synchronize to broadcast servers.
|
|
|
|
* -c conffile
|
|
|
|
* Specify the name and path of the configuration file,
|
|
|
|
* default /etc/ntp.conf
|
|
|
|
* -d Specify debugging mode. This option may occur more than once,
|
|
|
|
* with each occurrence indicating greater detail of display.
|
|
|
|
* -D level
|
|
|
|
* Specify debugging level directly.
|
|
|
|
* -f driftfile
|
|
|
|
* Specify the name and path of the frequency file.
|
|
|
|
* This is the same operation as the "driftfile FILE"
|
|
|
|
* configuration command.
|
|
|
|
* -g Normally, ntpd exits with a message to the system log
|
|
|
|
* if the offset exceeds the panic threshold, which is 1000 s
|
|
|
|
* by default. This option allows the time to be set to any value
|
|
|
|
* without restriction; however, this can happen only once.
|
|
|
|
* If the threshold is exceeded after that, ntpd will exit
|
|
|
|
* with a message to the system log. This option can be used
|
|
|
|
* with the -q and -x options. See the tinker command for other options.
|
|
|
|
* -i jaildir
|
|
|
|
* Chroot the server to the directory jaildir. This option also implies
|
|
|
|
* that the server attempts to drop root privileges at startup
|
|
|
|
* (otherwise, chroot gives very little additional security).
|
|
|
|
* You may need to also specify a -u option.
|
|
|
|
* -k keyfile
|
|
|
|
* Specify the name and path of the symmetric key file,
|
|
|
|
* default /etc/ntp/keys. This is the same operation
|
|
|
|
* as the "keys FILE" configuration command.
|
|
|
|
* -l logfile
|
|
|
|
* Specify the name and path of the log file. The default
|
|
|
|
* is the system log file. This is the same operation as
|
|
|
|
* the "logfile FILE" configuration command.
|
|
|
|
* -L Do not listen to virtual IPs. The default is to listen.
|
|
|
|
* -n Don't fork.
|
|
|
|
* -N To the extent permitted by the operating system,
|
|
|
|
* run the ntpd at the highest priority.
|
|
|
|
* -p pidfile
|
|
|
|
* Specify the name and path of the file used to record the ntpd
|
|
|
|
* process ID. This is the same operation as the "pidfile FILE"
|
|
|
|
* configuration command.
|
|
|
|
* -P priority
|
|
|
|
* To the extent permitted by the operating system,
|
|
|
|
* run the ntpd at the specified priority.
|
|
|
|
* -q Exit the ntpd just after the first time the clock is set.
|
|
|
|
* This behavior mimics that of the ntpdate program, which is
|
|
|
|
* to be retired. The -g and -x options can be used with this option.
|
|
|
|
* Note: The kernel time discipline is disabled with this option.
|
|
|
|
* -r broadcastdelay
|
|
|
|
* Specify the default propagation delay from the broadcast/multicast
|
|
|
|
* server to this client. This is necessary only if the delay
|
|
|
|
* cannot be computed automatically by the protocol.
|
|
|
|
* -s statsdir
|
|
|
|
* Specify the directory path for files created by the statistics
|
|
|
|
* facility. This is the same operation as the "statsdir DIR"
|
|
|
|
* configuration command.
|
|
|
|
* -t key
|
|
|
|
* Add a key number to the trusted key list. This option can occur
|
|
|
|
* more than once.
|
|
|
|
* -u user[:group]
|
|
|
|
* Specify a user, and optionally a group, to switch to.
|
|
|
|
* -v variable
|
|
|
|
* -V variable
|
|
|
|
* Add a system variable listed by default.
|
|
|
|
* -x Normally, the time is slewed if the offset is less than the step
|
|
|
|
* threshold, which is 128 ms by default, and stepped if above
|
|
|
|
* the threshold. This option sets the threshold to 600 s, which is
|
|
|
|
* well within the accuracy window to set the clock manually.
|
|
|
|
* Note: since the slew rate of typical Unix kernels is limited
|
|
|
|
* to 0.5 ms/s, each second of adjustment requires an amortization
|
|
|
|
* interval of 2000 s. Thus, an adjustment as much as 600 s
|
|
|
|
* will take almost 14 days to complete. This option can be used
|
|
|
|
* with the -g and -q options. See the tinker command for other options.
|
|
|
|
* Note: The kernel time discipline is disabled with this option.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* By doing init in a separate function we decrease stack usage
|
|
|
|
* in main loop.
|
|
|
|
*/
|
|
|
|
static NOINLINE void ntp_init(char **argv)
|
|
|
|
{
|
|
|
|
unsigned opts;
|
|
|
|
llist_t *peers;
|
|
|
|
|
2009-11-24 06:07:42 +00:00
|
|
|
srandom(getpid());
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
if (getuid())
|
2009-11-25 13:52:47 +00:00
|
|
|
bb_error_msg_and_die(bb_msg_you_must_be_root);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
peers = NULL;
|
|
|
|
opt_complementary = "dd:p::"; /* d: counter, p: list */
|
|
|
|
opts = getopt32(argv,
|
2009-12-02 22:17:45 +00:00
|
|
|
"nqNx" /* compat */
|
2009-11-22 02:43:55 +00:00
|
|
|
"p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
|
|
|
|
"d" /* compat */
|
2009-12-02 22:17:45 +00:00
|
|
|
"46aAbgL", /* compat, ignored */
|
2009-11-22 02:43:55 +00:00
|
|
|
&peers, &G.verbose);
|
2009-11-25 13:52:47 +00:00
|
|
|
if (!(opts & (OPT_p|OPT_l)))
|
|
|
|
bb_show_usage();
|
2009-12-02 22:17:45 +00:00
|
|
|
if (opts & OPT_x) /* disable stepping, only slew is allowed */
|
2009-12-17 22:29:33 +00:00
|
|
|
G.time_was_stepped = 1;
|
2009-11-27 01:35:15 +00:00
|
|
|
while (peers)
|
|
|
|
add_peers(llist_pop(&peers));
|
|
|
|
if (!(opts & OPT_n)) {
|
|
|
|
bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
|
|
|
|
logmode = LOGMODE_NONE;
|
|
|
|
}
|
2009-11-22 02:43:55 +00:00
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
|
|
|
G.listen_fd = -1;
|
|
|
|
if (opts & OPT_l) {
|
|
|
|
G.listen_fd = create_and_bind_dgram_or_die(NULL, 123);
|
|
|
|
socket_want_pktinfo(G.listen_fd);
|
|
|
|
setsockopt(G.listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-25 13:52:47 +00:00
|
|
|
/* I hesitate to set -20 prio. -15 should be high enough for timekeeping */
|
|
|
|
if (opts & OPT_N)
|
|
|
|
setpriority(PRIO_PROCESS, 0, -15);
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
/* Set some globals */
|
2009-12-02 22:17:45 +00:00
|
|
|
#if 0
|
2009-12-18 17:50:29 +00:00
|
|
|
/* With constant b = 100, G.precision_exp is also constant -6.
|
2009-12-02 22:17:45 +00:00
|
|
|
* Uncomment this and you'll see */
|
2009-11-22 02:43:55 +00:00
|
|
|
{
|
|
|
|
int prec = 0;
|
|
|
|
int b;
|
2009-12-02 22:17:45 +00:00
|
|
|
# if 0
|
2009-11-22 02:43:55 +00:00
|
|
|
struct timespec tp;
|
|
|
|
/* We can use sys_clock_getres but assuming 10ms tick should be fine */
|
|
|
|
clock_getres(CLOCK_REALTIME, &tp);
|
|
|
|
tp.tv_sec = 0;
|
|
|
|
tp.tv_nsec = 10000000;
|
2009-12-17 22:29:33 +00:00
|
|
|
b = 1000000000 / tp.tv_nsec; /* convert to Hz */
|
2009-12-02 22:17:45 +00:00
|
|
|
# else
|
2009-11-22 02:43:55 +00:00
|
|
|
b = 100; /* b = 1000000000/10000000 = 100 */
|
2009-12-02 22:17:45 +00:00
|
|
|
# endif
|
2009-11-22 02:43:55 +00:00
|
|
|
while (b > 1)
|
|
|
|
prec--, b >>= 1;
|
2009-12-18 17:50:29 +00:00
|
|
|
//G.precision_exp = prec;
|
|
|
|
bb_error_msg("G.precision_exp:%d", prec); /* -6 */
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
2009-12-02 22:17:45 +00:00
|
|
|
#endif
|
2009-11-22 02:43:55 +00:00
|
|
|
G.scale = 1;
|
|
|
|
|
|
|
|
bb_signals((1 << SIGTERM) | (1 << SIGINT), record_signo);
|
|
|
|
bb_signals((1 << SIGPIPE) | (1 << SIGHUP), SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int ntpd_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
struct globals g;
|
|
|
|
struct pollfd *pfd;
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t **idx2peer;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
memset(&g, 0, sizeof(g));
|
|
|
|
SET_PTR_TO_GLOBALS(&g);
|
|
|
|
|
|
|
|
ntp_init(argv);
|
|
|
|
|
2009-11-23 15:27:16 +00:00
|
|
|
{
|
|
|
|
/* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */
|
2009-12-30 17:38:05 +00:00
|
|
|
unsigned cnt = g.peer_cnt + ENABLE_FEATURE_NTPD_SERVER;
|
|
|
|
idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt);
|
|
|
|
pfd = xzalloc(sizeof(pfd[0]) * cnt);
|
2009-11-23 15:27:16 +00:00
|
|
|
}
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
while (!bb_got_signal) {
|
|
|
|
llist_t *item;
|
2009-11-24 13:04:15 +00:00
|
|
|
unsigned i, j;
|
2009-11-22 02:43:55 +00:00
|
|
|
unsigned sent_cnt, trial_cnt;
|
|
|
|
int nfds, timeout;
|
2009-11-24 13:04:15 +00:00
|
|
|
time_t cur_time, nextaction;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
/* Nothing between here and poll() blocks for any significant time */
|
|
|
|
|
|
|
|
cur_time = time(NULL);
|
|
|
|
nextaction = cur_time + 3600;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
|
|
|
if (g.listen_fd != -1) {
|
|
|
|
pfd[0].fd = g.listen_fd;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-24 13:04:15 +00:00
|
|
|
/* Pass over peer list, send requests, time out on receives */
|
2009-11-22 02:43:55 +00:00
|
|
|
sent_cnt = trial_cnt = 0;
|
|
|
|
for (item = g.ntp_peers; item != NULL; item = item->link) {
|
2009-12-30 17:38:05 +00:00
|
|
|
peer_t *p = (peer_t *) item->data;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
/* Overflow-safe "if (p->next_action_time <= cur_time) ..." */
|
|
|
|
if ((int)(cur_time - p->next_action_time) >= 0) {
|
2009-12-17 22:29:33 +00:00
|
|
|
if (p->p_fd == -1) {
|
2009-12-02 22:17:45 +00:00
|
|
|
/* Time to send new req */
|
|
|
|
trial_cnt++;
|
|
|
|
if (send_query_to_peer(p) == 0)
|
|
|
|
sent_cnt++;
|
|
|
|
} else {
|
|
|
|
/* Timed out waiting for reply */
|
2009-12-17 22:29:33 +00:00
|
|
|
close(p->p_fd);
|
|
|
|
p->p_fd = -1;
|
2009-12-02 22:17:45 +00:00
|
|
|
timeout = error_interval();
|
|
|
|
bb_error_msg("timed out waiting for %s, "
|
2009-12-17 22:29:33 +00:00
|
|
|
"next query in %us", p->p_dotted, timeout);
|
2009-12-30 17:38:05 +00:00
|
|
|
if (p->p_trustlevel >= TRUSTLEVEL_BADPEER) {
|
|
|
|
p->p_trustlevel /= 2;
|
|
|
|
if (p->p_trustlevel < TRUSTLEVEL_BADPEER)
|
2009-12-17 22:29:33 +00:00
|
|
|
bb_error_msg("peer %s now invalid", p->p_dotted);
|
2009-12-02 22:17:45 +00:00
|
|
|
}
|
|
|
|
set_next(p, timeout);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-02 22:17:45 +00:00
|
|
|
if (p->next_action_time < nextaction)
|
|
|
|
nextaction = p->next_action_time;
|
2009-11-24 13:04:15 +00:00
|
|
|
|
2009-12-17 22:29:33 +00:00
|
|
|
if (p->p_fd >= 0) {
|
2009-11-24 13:04:15 +00:00
|
|
|
/* Wait for reply from this peer */
|
2009-12-17 22:29:33 +00:00
|
|
|
pfd[i].fd = p->p_fd;
|
2009-11-22 02:43:55 +00:00
|
|
|
pfd[i].events = POLLIN;
|
2009-11-24 13:04:15 +00:00
|
|
|
idx2peer[i] = p;
|
2009-11-22 02:43:55 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 15:27:16 +00:00
|
|
|
if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
|
2009-12-01 01:32:01 +00:00
|
|
|
step_time_once(0); /* no good peers, don't wait */
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
timeout = nextaction - cur_time;
|
2009-12-02 00:51:24 +00:00
|
|
|
if (timeout < 1)
|
|
|
|
timeout = 1;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
/* Here we may block */
|
2009-12-02 00:51:24 +00:00
|
|
|
if (g.verbose >= 2)
|
2009-12-02 22:17:45 +00:00
|
|
|
bb_error_msg("poll %us, sockets:%u", timeout, i);
|
2009-11-22 02:43:55 +00:00
|
|
|
nfds = poll(pfd, i, timeout * 1000);
|
2009-11-24 13:04:15 +00:00
|
|
|
if (nfds <= 0)
|
|
|
|
continue;
|
2009-11-22 02:43:55 +00:00
|
|
|
|
2009-11-24 13:04:15 +00:00
|
|
|
/* Process any received packets */
|
2009-11-22 02:43:55 +00:00
|
|
|
j = 0;
|
|
|
|
#if ENABLE_FEATURE_NTPD_SERVER
|
2009-11-24 13:04:15 +00:00
|
|
|
if (g.listen_fd != -1) {
|
|
|
|
if (pfd[0].revents /* & (POLLIN|POLLERR)*/) {
|
2009-11-22 02:43:55 +00:00
|
|
|
nfds--;
|
2009-11-24 13:04:15 +00:00
|
|
|
recv_and_process_client_pkt(/*g.listen_fd*/);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
2009-11-24 13:04:15 +00:00
|
|
|
j = 1;
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-11-24 13:04:15 +00:00
|
|
|
for (; nfds != 0 && j < i; j++) {
|
|
|
|
if (pfd[j].revents /* & (POLLIN|POLLERR)*/) {
|
2009-11-22 02:43:55 +00:00
|
|
|
nfds--;
|
2009-11-24 13:04:15 +00:00
|
|
|
recv_and_process_peer_pkt(idx2peer[j]);
|
2009-11-22 02:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* while (!bb_got_signal) */
|
|
|
|
|
|
|
|
kill_myself_with_sig(bb_got_signal);
|
|
|
|
}
|