hush: fix a bug in argv restoration after sourcing a file
[oweals/busybox.git] / networking / ntpd.c
index f1f99bb2bb7d7d694d3364bdb33de73c1084df88..bfd5705fcd30a8ab2293eec6baa2c7a02ec6c119 100644 (file)
@@ -1,35 +1,74 @@
 /*
  * NTP client/server, based on OpenNTPD 3.9p1
  *
- * Author: Adam Tkac <vonsch@gmail.com>
+ * Busybox port author: Adam Tkac (C) 2009 <vonsch@gmail.com>
  *
- * Licensed under GPLv2, see file LICENSE in this source tree.
+ * OpenNTPd 3.9p1 copyright holders:
+ *   Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *   Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
+ *
+ * OpenNTPd code is licensed under ISC-style licence:
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ ***********************************************************************
  *
  * Parts of OpenNTPD clock syncronization code is replaced by
- * code which is based on ntp-4.2.6, whuch carries the following
+ * code which is based on ntp-4.2.6, which carries the following
  * copyright notice:
  *
- ***********************************************************************
- *                                                                     *
- * Copyright (c) University of Delaware 1992-2009                      *
- *                                                                     *
- * Permission to use, copy, modify, and distribute this software and   *
- * its documentation for any purpose with or without fee is hereby     *
- * granted, provided that the above copyright notice appears in all    *
- * copies and that both the copyright notice and this permission       *
- * notice appear in supporting documentation, and that the name        *
- * University of Delaware not be used in advertising or publicity      *
- * pertaining to distribution of the software without specific,        *
- * written prior permission. The University of Delaware makes no       *
- * representations about the suitability this software for any         *
- * purpose. It is provided "as is" without express or implied          *
- * warranty.                                                           *
- *                                                                     *
+ * Copyright (c) University of Delaware 1992-2009
+ *
+ * Permission to use, copy, modify, and distribute this software and
+ * its documentation for any purpose with or without fee is hereby
+ * granted, provided that the above copyright notice appears in all
+ * copies and that both the copyright notice and this permission
+ * notice appear in supporting documentation, and that the name
+ * University of Delaware not be used in advertising or publicity
+ * pertaining to distribution of the software without specific,
+ * written prior permission. The University of Delaware makes no
+ * representations about the suitability this software for any
+ * purpose. It is provided "as is" without express or implied warranty.
  ***********************************************************************
  */
+//config:config NTPD
+//config:      bool "ntpd"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:        The NTP client/server daemon.
+//config:
+//config:config FEATURE_NTPD_SERVER
+//config:      bool "Make ntpd usable as a NTP server"
+//config:      default y
+//config:      depends on NTPD
+//config:      help
+//config:        Make ntpd usable as a NTP server. If you disable this option
+//config:        ntpd will be usable only as a NTP client.
+//config:
+//config:config FEATURE_NTPD_CONF
+//config:      bool "Make ntpd understand /etc/ntp.conf"
+//config:      default y
+//config:      depends on NTPD
+//config:      help
+//config:        Make ntpd look in /etc/ntp.conf for peers. Only "server address"
+//config:        is supported.
+
+//applet:IF_NTPD(APPLET(ntpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_NTPD) += ntpd.o
 
 //usage:#define ntpd_trivial_usage
-//usage:       "[-dnqNw"IF_FEATURE_NTPD_SERVER("l")"] [-S PROG] [-p PEER]..."
+//usage:       "[-dnqNw"IF_FEATURE_NTPD_SERVER("l -I IFACE")"] [-S PROG] [-p PEER]..."
 //usage:#define ntpd_full_usage "\n\n"
 //usage:       "NTP client/server\n"
 //usage:     "\n       -d      Verbose"
 //usage:     "\n       -q      Quit after clock is set"
 //usage:     "\n       -N      Run at high priority"
 //usage:     "\n       -w      Do not set time (only query peers), implies -n"
-//usage:       IF_FEATURE_NTPD_SERVER(
-//usage:     "\n       -l      Run as server on port 123"
-//usage:       )
 //usage:     "\n       -S PROG Run PROG after stepping time, stratum change, and every 11 mins"
 //usage:     "\n       -p PEER Obtain time from PEER (may be repeated)"
+//usage:       IF_FEATURE_NTPD_CONF(
+//usage:     "\n               If -p is not given, 'server HOST' lines"
+//usage:     "\n               from /etc/ntp.conf are used"
+//usage:       )
+//usage:       IF_FEATURE_NTPD_SERVER(
+//usage:     "\n       -l      Also run as server on port 123"
+//usage:     "\n       -I IFACE Bind server to IFACE, implies -l"
+//usage:       )
+
+// -l and -p options are not compatible with "standard" ntpd:
+// it has them as "-l logfile" and "-p pidfile".
+// -S and -w are not compat either, "standard" ntpd has no such opts.
 
 #include "libbb.h"
 #include <math.h>
@@ -51,9 +99,6 @@
 #ifndef IPTOS_LOWDELAY
 # define IPTOS_LOWDELAY 0x10
 #endif
-#ifndef IP_PKTINFO
-# error "Sorry, your kernel has to support IP_PKTINFO"
-#endif
 
 
 /* Verbosity control (max level of -dddd options accepted).
  *
  * Made some changes to speed up re-syncing after our clock goes bad
  * (tested with suspending my laptop):
- * - if largish offset (>= STEP_THRESHOLD * 8 == 1 sec) is seen
+ * - if largish offset (>= STEP_THRESHOLD == 1 sec) is seen
  *   from a peer, schedule next query for this peer soon
  *   without drastically lowering poll interval for everybody.
  *   This makes us collect enough data for step much faster:
  *   datapoints after the step.
  */
 
-#define RETRY_INTERVAL     5    /* on error, retry in N secs */
-#define RESPONSE_INTERVAL 15    /* wait for reply up to N secs */
 #define INITIAL_SAMPLES    4    /* how many samples do we want for init */
 #define BAD_DELAY_GROWTH   4    /* drop packet if its delay grew by more than this */
 
-/* Clock discipline parameters and constants */
+#define RETRY_INTERVAL    32    /* on send/recv error, retry in N secs (need to be power of 2) */
+#define NOREPLY_INTERVAL 512    /* sent, but got no reply: cap next query by this many seconds */
+#define RESPONSE_INTERVAL 16    /* wait for reply up to N secs */
+#define HOSTNAME_INTERVAL  5    /* hostname lookup failed. Wait N secs for next try */
 
 /* Step threshold (sec). std ntpd uses 0.128.
- * Using exact power of 2 (1/8) results in smaller code */
-#define STEP_THRESHOLD  0.125
-#define WATCH_THRESHOLD 128     /* stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */
+ */
+#define STEP_THRESHOLD     1
+/* Slew threshold (sec): adjtimex() won't accept offsets larger than this.
+ * Using exact power of 2 (1/8) results in smaller code
+ */
+#define SLEW_THRESHOLD 0.125
+/* Stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */
+#define WATCH_THRESHOLD  128
 /* NB: set WATCH_THRESHOLD to ~60 when debugging to save time) */
 //UNUSED: #define PANIC_THRESHOLD 1000    /* panic threshold (sec) */
 
+/*
+ * If we got |offset| > BIGOFF from a peer, cap next query interval
+ * for this peer by this many seconds:
+ */
+#define BIGOFF          STEP_THRESHOLD
+#define BIGOFF_INTERVAL (1 << 7) /* 128 s */
+
 #define FREQ_TOLERANCE  0.000015 /* frequency tolerance (15 PPM) */
 #define BURSTPOLL       0       /* initial poll */
 #define MINPOLL         5       /* minimum poll interval. std ntpd uses 6 (6: 64 sec) */
-/* If we got largish offset from a peer, cap next query interval
- * for this peer by this many seconds:
- */
-#define BIGOFF_INTERVAL (1 << 6)
-/* If offset > discipline_jitter * POLLADJ_GATE, and poll interval is >= 2^BIGPOLL,
- * then it is decreased _at once_. (If < 2^BIGPOLL, it will be decreased _eventually_).
+/*
+ * If offset > discipline_jitter * POLLADJ_GATE, and poll interval is > 2^BIGPOLL,
+ * then it is decreased _at once_. (If <= 2^BIGPOLL, it will be decreased _eventually_).
  */
-#define BIGPOLL         10      /* 2^10 sec ~= 17 min */
+#define BIGPOLL         9       /* 2^9 sec ~= 8.5 min */
 #define MAXPOLL         12      /* maximum poll interval (12: 1.1h, 17: 36.4h). std ntpd uses 17 */
-/* Actively lower poll when we see such big offsets.
- * With STEP_THRESHOLD = 0.125, it means we try to sync more aggressively
- * if offset increases over ~0.04 sec */
-#define POLLDOWN_OFFSET (STEP_THRESHOLD / 3)
+/*
+ * Actively lower poll when we see such big offsets.
+ * With SLEW_THRESHOLD = 0.125, it means we try to sync more aggressively
+ * if offset increases over ~0.04 sec
+ */
+//#define POLLDOWN_OFFSET (SLEW_THRESHOLD / 3)
 #define MINDISP         0.01    /* minimum dispersion (sec) */
 #define MAXDISP         16      /* maximum dispersion (sec) */
 #define MAXSTRAT        16      /* maximum stratum (infinity metric) */
@@ -248,6 +305,9 @@ typedef struct {
         * or when receive times out (if p_fd >= 0): */
        double           next_action_time;
        double           p_xmttime;
+       double           p_raw_delay;
+       /* p_raw_delay is set even by "high delay" packets */
+       /* lastpkt_delay isn't */
        double           lastpkt_recv_time;
        double           lastpkt_delay;
        double           lastpkt_rootdelay;
@@ -259,6 +319,7 @@ typedef struct {
        datapoint_t      filter_datapoint[NUM_DATAPOINTS];
        /* last sent packet: */
        msg_t            p_xmt_msg;
+       char             p_hostname[1];
 } peer_t;
 
 
@@ -276,6 +337,7 @@ enum {
        OPT_p = (1 << 5),
        OPT_S = (1 << 6),
        OPT_l = (1 << 7) * ENABLE_FEATURE_NTPD_SERVER,
+       OPT_I = (1 << 8) * ENABLE_FEATURE_NTPD_SERVER,
        /* We hijack some bits for other purposes */
        OPT_qq = (1 << 31),
 };
@@ -294,6 +356,7 @@ struct globals {
        llist_t  *ntp_peers;
 #if ENABLE_FEATURE_NTPD_SERVER
        int      listen_fd;
+       char     *if_name;
 # define G_listen_fd (G.listen_fd)
 #else
 # define G_listen_fd (-1)
@@ -345,8 +408,6 @@ struct globals {
         */
 #define G_precision_sec  0.002
        uint8_t  stratum;
-       /* Bool. After set to 1, never goes back to 0: */
-       smallint initial_poll_complete;
 
 #define STATE_NSET      0       /* initial state, "nothing is set" */
 //#define STATE_FSET    1       /* frequency set from file */
@@ -375,8 +436,6 @@ struct globals {
 };
 #define G (*ptr_to_globals)
 
-static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
-
 
 #define VERB1 if (MAX_VERBOSE && G.verbose)
 #define VERB2 if (MAX_VERBOSE >= 2 && G.verbose >= 2)
@@ -692,11 +751,11 @@ static void
 reset_peer_stats(peer_t *p, double offset)
 {
        int i;
-       bool small_ofs = fabs(offset) < 16 * STEP_THRESHOLD;
+       bool small_ofs = fabs(offset) < STEP_THRESHOLD;
 
        /* Used to set p->filter_datapoint[i].d_dispersion = MAXDISP
         * and clear reachable bits, but this proved to be too agressive:
-        * after step (tested with suspinding laptop for ~30 secs),
+        * after step (tested with suspending laptop for ~30 secs),
         * this caused all previous data to be considered invalid,
         * making us needing to collect full ~8 datapoins per peer
         * after step in order to start trusting them.
@@ -732,18 +791,51 @@ reset_peer_stats(peer_t *p, double offset)
        VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
 }
 
+static len_and_sockaddr*
+resolve_peer_hostname(peer_t *p)
+{
+       len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123);
+       if (lsa) {
+               free(p->p_lsa);
+               free(p->p_dotted);
+               p->p_lsa = lsa;
+               p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
+       } else {
+               /* error message is emitted by host2sockaddr() */
+               set_next(p, HOSTNAME_INTERVAL);
+       }
+       return lsa;
+}
+
 static void
-add_peers(char *s)
+add_peers(const char *s)
 {
+       llist_t *item;
        peer_t *p;
 
-       p = xzalloc(sizeof(*p));
-       p->p_lsa = xhost2sockaddr(s, 123);
-       p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa);
+       p = xzalloc(sizeof(*p) + strlen(s));
+       strcpy(p->p_hostname, s);
        p->p_fd = -1;
        p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
        p->next_action_time = G.cur_time; /* = set_next(p, 0); */
-       reset_peer_stats(p, 16 * STEP_THRESHOLD);
+       reset_peer_stats(p, STEP_THRESHOLD);
+
+       /* Names like N.<country2chars>.pool.ntp.org are randomly resolved
+        * to a pool of machines. Sometimes different N's resolve to the same IP.
+        * It is not useful to have two peers with same IP. We skip duplicates.
+        */
+       if (resolve_peer_hostname(p)) {
+               for (item = G.ntp_peers; item != NULL; item = item->link) {
+                       peer_t *pp = (peer_t *) item->data;
+                       if (pp->p_dotted && strcmp(p->p_dotted, pp->p_dotted) == 0) {
+                               bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted);
+                               free(p->p_lsa);
+                               free(p->p_dotted);
+                               free(p);
+                               return;
+                       }
+               }
+       }
 
        llist_add_to(&G.ntp_peers, p);
        G.peer_cnt++;
@@ -772,6 +864,11 @@ do_sendto(int fd,
 static void
 send_query_to_peer(peer_t *p)
 {
+       if (!p->p_lsa) {
+               if (!resolve_peer_hostname(p))
+                       return;
+       }
+
        /* Why do we need to bind()?
         * See what happens when we don't bind:
         *
@@ -807,7 +904,7 @@ send_query_to_peer(peer_t *p)
 #if ENABLE_FEATURE_IPV6
                if (family == AF_INET)
 #endif
-                       setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
+                       setsockopt_int(fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY);
                free(local_lsa);
        }
 
@@ -830,8 +927,8 @@ send_query_to_peer(peer_t *p)
         *
         * Save the real transmit timestamp locally.
         */
-       p->p_xmt_msg.m_xmttime.int_partl = random();
-       p->p_xmt_msg.m_xmttime.fractionl = random();
+       p->p_xmt_msg.m_xmttime.int_partl = rand();
+       p->p_xmt_msg.m_xmttime.fractionl = rand();
        p->p_xmttime = gettime1900d();
 
        /* Were doing it only if sendto worked, but
@@ -962,6 +1059,16 @@ step_time(double offset)
        }
 }
 
+static void clamp_pollexp_and_set_MAXSTRAT(void)
+{
+       if (G.poll_exp < MINPOLL)
+               G.poll_exp = MINPOLL;
+       if (G.poll_exp > BIGPOLL)
+               G.poll_exp = BIGPOLL;
+       G.polladj_count = 0;
+       G.stratum = MAXSTRAT;
+}
+
 
 /*
  * Selection and clustering, and their helpers
@@ -1043,7 +1150,7 @@ select_and_cluster(void)
 
        num_points = 0;
        item = G.ntp_peers;
-       if (G.initial_poll_complete) while (item != NULL) {
+       while (item != NULL) {
                double rd, offset;
 
                p = (peer_t *) item->data;
@@ -1443,9 +1550,7 @@ update_local_clock(peer_t *p)
                        exit(0);
                }
 
-               G.polladj_count = 0;
-               G.poll_exp = MINPOLL;
-               G.stratum = MAXSTRAT;
+               clamp_pollexp_and_set_MAXSTRAT();
 
                run_script("step", offset);
 
@@ -1459,14 +1564,12 @@ update_local_clock(peer_t *p)
 #endif
                abs_offset = offset = 0;
                set_new_values(STATE_SYNC, offset, recv_time);
-
        } else { /* abs_offset <= STEP_THRESHOLD */
 
-               if (G.poll_exp < MINPOLL && G.initial_poll_complete) {
-                       VERB4 bb_error_msg("small offset:%+f, disabling burst mode", offset);
-                       G.polladj_count = 0;
-                       G.poll_exp = MINPOLL;
-               }
+               /* The ratio is calculated before jitter is updated to make
+                * poll adjust code more sensitive to large offsets.
+                */
+               G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
 
                /* Compute the clock jitter as the RMS of exponentially
                 * weighted offset differences. Used by the poll adjust code.
@@ -1474,6 +1577,8 @@ update_local_clock(peer_t *p)
                etemp = SQUARE(G.discipline_jitter);
                dtemp = SQUARE(offset - G.last_update_offset);
                G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG);
+               if (G.discipline_jitter < G_precision_sec)
+                       G.discipline_jitter = G_precision_sec;
 
                switch (G.discipline_state) {
                case STATE_NSET:
@@ -1550,10 +1655,6 @@ update_local_clock(peer_t *p)
                }
        }
 
-       if (G.discipline_jitter < G_precision_sec)
-               G.discipline_jitter = G_precision_sec;
-       G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
-
        G.reftime = G.cur_time;
        G.ntp_status = p->lastpkt_status;
        G.refid = p->lastpkt_refid;
@@ -1606,14 +1707,7 @@ update_local_clock(peer_t *p)
        tmx.freq = G.discipline_freq_drift * 65536e6;
 #endif
        tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR;
-       tmx.offset = (offset * 1000000); /* usec */
-       tmx.status = STA_PLL;
-       if (G.ntp_status & LI_PLUSSEC)
-               tmx.status |= STA_INS;
-       if (G.ntp_status & LI_MINUSSEC)
-               tmx.status |= STA_DEL;
-
-       tmx.constant = G.poll_exp - 4;
+       tmx.constant = (int)G.poll_exp - 4;
        /* EXPERIMENTAL.
         * The below if statement should be unnecessary, but...
         * It looks like Linux kernel's PLL is far too gentle in changing
@@ -1624,8 +1718,27 @@ update_local_clock(peer_t *p)
         * To be on a safe side, let's do it only if offset is significantly
         * larger than jitter.
         */
-       if (tmx.constant > 0 && G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
+       if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
                tmx.constant--;
+       tmx.offset = (long)(offset * 1000000); /* usec */
+       if (SLEW_THRESHOLD < STEP_THRESHOLD) {
+               if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) {
+                       tmx.offset = (long)(SLEW_THRESHOLD * 1000000);
+                       tmx.constant--;
+               }
+               if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) {
+                       tmx.offset = -(long)(SLEW_THRESHOLD * 1000000);
+                       tmx.constant--;
+               }
+       }
+       if (tmx.constant < 0)
+               tmx.constant = 0;
+
+       tmx.status = STA_PLL;
+       if (G.ntp_status & LI_PLUSSEC)
+               tmx.status |= STA_INS;
+       if (G.ntp_status & LI_MINUSSEC)
+               tmx.status |= STA_DEL;
 
        //tmx.esterror = (uint32_t)(clock_jitter * 1e6);
        //tmx.maxerror = (uint32_t)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
@@ -1638,8 +1751,14 @@ update_local_clock(peer_t *p)
        VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x",
                                rc, tmx.freq, tmx.offset, tmx.status);
        G.kernel_freq_drift = tmx.freq / 65536;
-       VERB2 bb_error_msg("update from:%s offset:%+f jitter:%f clock drift:%+.3fppm tc:%d",
-                       p->p_dotted, offset, G.discipline_jitter, (double)tmx.freq / 65536, (int)tmx.constant);
+       VERB2 bb_error_msg("update from:%s offset:%+f delay:%f jitter:%f clock drift:%+.3fppm tc:%d",
+                       p->p_dotted,
+                       offset,
+                       p->lastpkt_delay,
+                       G.discipline_jitter,
+                       (double)tmx.freq / 65536,
+                       (int)tmx.constant
+       );
 
        return 1; /* "ok to increase poll interval" */
 }
@@ -1650,28 +1769,52 @@ update_local_clock(peer_t *p)
  * (helpers first)
  */
 static unsigned
-retry_interval(void)
+poll_interval(int upper_bound)
 {
-       /* Local problem, want to retry soon */
-       unsigned interval, r;
-       interval = RETRY_INTERVAL;
-       r = random();
-       interval += r % (unsigned)(RETRY_INTERVAL / 4);
-       VERB4 bb_error_msg("chose retry interval:%u", interval);
+       unsigned interval, r, mask;
+       interval = 1 << G.poll_exp;
+       if (interval > upper_bound)
+               interval = upper_bound;
+       mask = ((interval-1) >> 4) | 1;
+       r = rand();
+       interval += r & mask; /* ~ random(0..1) * interval/16 */
+       VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d)", interval, G.poll_exp);
        return interval;
 }
-static unsigned
-poll_interval(int exponent)
+static void
+adjust_poll(int count)
 {
-       unsigned interval, r;
-       exponent = G.poll_exp + exponent;
-       if (exponent < 0)
-               exponent = 0;
-       interval = 1 << exponent;
-       r = random();
-       interval += ((r & (interval-1)) >> 4) + ((r >> 8) & 1); /* + 1/16 of interval, max */
-       VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d exp:%d)", interval, G.poll_exp, exponent);
-       return interval;
+       G.polladj_count += count;
+       if (G.polladj_count > POLLADJ_LIMIT) {
+               G.polladj_count = 0;
+               if (G.poll_exp < MAXPOLL) {
+                       G.poll_exp++;
+                       VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
+                                       G.discipline_jitter, G.poll_exp);
+               }
+       } else if (G.polladj_count < -POLLADJ_LIMIT || (count < 0 && G.poll_exp > BIGPOLL)) {
+               G.polladj_count = 0;
+               if (G.poll_exp > MINPOLL) {
+                       llist_t *item;
+
+                       G.poll_exp--;
+                       /* Correct p->next_action_time in each peer
+                        * which waits for sending, so that they send earlier.
+                        * Old pp->next_action_time are on the order
+                        * of t + (1 << old_poll_exp) + small_random,
+                        * we simply need to subtract ~half of that.
+                        */
+                       for (item = G.ntp_peers; item != NULL; item = item->link) {
+                               peer_t *pp = (peer_t *) item->data;
+                               if (pp->p_fd < 0)
+                                       pp->next_action_time -= (1 << G.poll_exp);
+                       }
+                       VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
+                                       G.discipline_jitter, G.poll_exp);
+               }
+       } else {
+               VERB4 bb_error_msg("polladj: count:%d", G.polladj_count);
+       }
 }
 static NOINLINE void
 recv_and_process_peer_pkt(peer_t *p)
@@ -1680,7 +1823,8 @@ recv_and_process_peer_pkt(peer_t *p)
        ssize_t     size;
        msg_t       msg;
        double      T1, T2, T3, T4;
-       double      dv, offset;
+       double      offset;
+       double      prev_delay, delay;
        unsigned    interval;
        datapoint_t *datapoint;
        peer_t      *q;
@@ -1691,19 +1835,23 @@ recv_and_process_peer_pkt(peer_t *p)
         * ntp servers reply from their *other IP*.
         * TODO: maybe we should check at least what we can: from.port == 123?
         */
+ recv_again:
        size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
-       if (size == -1) {
-               bb_perror_msg("recv(%s) error", p->p_dotted);
-               if (errno == EHOSTUNREACH || errno == EHOSTDOWN
-                || errno == ENETUNREACH || errno == ENETDOWN
-                || errno == ECONNREFUSED || errno == EADDRNOTAVAIL
-                || errno == EAGAIN
-               ) {
-//TODO: always do this?
-                       interval = retry_interval();
-                       goto set_next_and_ret;
-               }
-               xfunc_die();
+       if (size < 0) {
+               if (errno == EINTR)
+                       /* Signal caught */
+                       goto recv_again;
+               if (errno == EAGAIN)
+                       /* There was no packet after all
+                        * (poll() returning POLLIN for a fd
+                        * is not a ironclad guarantee that data is there)
+                        */
+                       return;
+               /*
+                * If you need a different handling for a specific
+                * errno, always explain it in comment.
+                */
+               bb_perror_msg_and_die("recv(%s) error", p->p_dotted);
        }
 
        if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
@@ -1729,10 +1877,15 @@ recv_and_process_peer_pkt(peer_t *p)
         || msg.m_stratum == 0
         || msg.m_stratum > NTP_MAXSTRATUM
        ) {
-// 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
                bb_error_msg("reply from %s: peer is unsynced", p->p_dotted);
+               /*
+                * 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.
+                * If poll interval is small, increase it.
+                */
+               if (G.poll_exp < BIGPOLL)
+                       goto increase_interval;
                goto pick_normal_interval;
        }
 
@@ -1740,12 +1893,6 @@ recv_and_process_peer_pkt(peer_t *p)
 //     if (msg.m_rootdelay / 2 + msg.m_rootdisp >= MAXDISP || p->lastpkt_reftime > msg.m_xmt)
 //             return;                 /* invalid header values */
 
-       p->lastpkt_status = msg.m_status;
-       p->lastpkt_stratum = msg.m_stratum;
-       p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay);
-       p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp);
-       p->lastpkt_refid = msg.m_refid;
-
        /*
         * From RFC 2030 (with a correction to the delay math):
         *
@@ -1765,28 +1912,35 @@ recv_and_process_peer_pkt(peer_t *p)
        T3 = lfp_to_d(msg.m_xmttime);
        T4 = G.cur_time;
 
-       p->lastpkt_recv_time = T4;
-       VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
-
        /* The delay calculation is a special case. In cases where the
         * server and client clocks are running at different rates and
         * with very fast networks, the delay can appear negative. In
         * order to avoid violating the Principle of Least Astonishment,
         * the delay is clamped not less than the system precision.
         */
-       dv = p->lastpkt_delay;
-       p->lastpkt_delay = (T4 - T1) - (T3 - T2);
-       if (p->lastpkt_delay < G_precision_sec)
-               p->lastpkt_delay = G_precision_sec;
+       delay = (T4 - T1) - (T3 - T2);
+       if (delay < G_precision_sec)
+               delay = G_precision_sec;
        /*
         * If this packet's delay is much bigger than the last one,
         * it's better to just ignore it than use its much less precise value.
         */
-       if (p->reachable_bits && p->lastpkt_delay > dv * BAD_DELAY_GROWTH) {
-               bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, p->lastpkt_delay);
+       prev_delay = p->p_raw_delay;
+       p->p_raw_delay = delay;
+       if (p->reachable_bits && delay > prev_delay * BAD_DELAY_GROWTH) {
+               bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, delay);
                goto pick_normal_interval;
        }
 
+       p->lastpkt_delay = delay;
+       p->lastpkt_recv_time = T4;
+       VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
+       p->lastpkt_status = msg.m_status;
+       p->lastpkt_stratum = msg.m_stratum;
+       p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay);
+       p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp);
+       p->lastpkt_refid = msg.m_refid;
+
        p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
        datapoint = &p->filter_datapoint[p->datapoint_idx];
        datapoint->d_recv_time = T4;
@@ -1820,21 +1974,36 @@ recv_and_process_peer_pkt(peer_t *p)
        /* Muck with statictics and update the clock */
        filter_datapoints(p);
        q = select_and_cluster();
-       rc = -1;
+       rc = 0;
        if (q) {
-               rc = 0;
                if (!(option_mask32 & OPT_w)) {
                        rc = update_local_clock(q);
+#if 0
+//Disabled this because there is a case where largish offsets
+//are unavoidable: if network round-trip delay is, say, ~0.6s,
+//error in offset estimation would be ~delay/2 ~= 0.3s.
+//Thus, offsets will be usually in -0.3...0.3s range.
+//In this case, this code would keep poll interval small,
+//but it won't be helping.
+//BIGOFF check below deals with a case of seeing multi-second offsets.
+
                        /* If drift is dangerously large, immediately
                         * drop poll interval one step down.
                         */
                        if (fabs(q->filter_offset) >= POLLDOWN_OFFSET) {
                                VERB4 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
-                               goto poll_down;
+                               adjust_poll(-POLLADJ_LIMIT * 3);
+                               rc = 0;
                        }
+#endif
                }
+       } else {
+               /* No peer selected.
+                * If poll interval is small, increase it.
+                */
+               if (G.poll_exp < BIGPOLL)
+                       goto increase_interval;
        }
-       /* else: no peer selected, rc = -1: we want to poll more often */
 
        if (rc != 0) {
                /* Adjust the poll interval by comparing the current offset
@@ -1846,51 +2015,21 @@ recv_and_process_peer_pkt(peer_t *p)
                if (rc > 0 && G.offset_to_jitter_ratio <= POLLADJ_GATE) {
                        /* was += G.poll_exp but it is a bit
                         * too optimistic for my taste at high poll_exp's */
-                       G.polladj_count += MINPOLL;
-                       if (G.polladj_count > POLLADJ_LIMIT) {
-                               G.polladj_count = 0;
-                               if (G.poll_exp < MAXPOLL) {
-                                       G.poll_exp++;
-                                       VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
-                                                       G.discipline_jitter, G.poll_exp);
-                               }
-                       } else {
-                               VERB4 bb_error_msg("polladj: incr:%d", G.polladj_count);
-                       }
+ increase_interval:
+                       adjust_poll(MINPOLL);
                } else {
-                       G.polladj_count -= G.poll_exp * 2;
-                       if (G.polladj_count < -POLLADJ_LIMIT || G.poll_exp >= BIGPOLL) {
- poll_down:
-                               G.polladj_count = 0;
-                               if (G.poll_exp > MINPOLL) {
-                                       llist_t *item;
-
-                                       G.poll_exp--;
-                                       /* Correct p->next_action_time in each peer
-                                        * which waits for sending, so that they send earlier.
-                                        * Old pp->next_action_time are on the order
-                                        * of t + (1 << old_poll_exp) + small_random,
-                                        * we simply need to subtract ~half of that.
-                                        */
-                                       for (item = G.ntp_peers; item != NULL; item = item->link) {
-                                               peer_t *pp = (peer_t *) item->data;
-                                               if (pp->p_fd < 0)
-                                                       pp->next_action_time -= (1 << G.poll_exp);
-                                       }
-                                       VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
-                                                       G.discipline_jitter, G.poll_exp);
-                               }
-                       } else {
-                               VERB4 bb_error_msg("polladj: decr:%d", G.polladj_count);
-                       }
+                       VERB3 if (rc > 0)
+                               bb_error_msg("want smaller interval: offset/jitter = %u",
+                                       G.offset_to_jitter_ratio);
+                       adjust_poll(-G.poll_exp * 2);
                }
        }
 
        /* Decide when to send new query for this peer */
  pick_normal_interval:
-       interval = poll_interval(0);
-       if (fabs(offset) >= STEP_THRESHOLD * 8 && interval > BIGOFF_INTERVAL) {
-               /* If we are synced, offsets are less than STEP_THRESHOLD,
+       interval = poll_interval(INT_MAX);
+       if (fabs(offset) >= BIGOFF && interval > BIGOFF_INTERVAL) {
+               /* If we are synced, offsets are less than SLEW_THRESHOLD,
                 * or at the very least not much larger than it.
                 * Now we see a largish one.
                 * Either this peer is feeling bad, or packet got corrupted,
@@ -1904,7 +2043,6 @@ recv_and_process_peer_pkt(peer_t *p)
                interval = BIGOFF_INTERVAL;
        }
 
- set_next_and_ret:
        set_next(p, interval);
 }
 
@@ -1937,6 +2075,13 @@ recv_and_process_client_pkt(void /*int fd*/)
                goto bail;
        }
 
+       /* Respond only to client and symmetric active packets */
+       if ((msg.m_status & MODE_MASK) != MODE_CLIENT
+        && (msg.m_status & MODE_MASK) != MODE_SYM_ACT
+       ) {
+               goto bail;
+       }
+
        query_status = msg.m_status;
        query_xmttime = msg.m_xmttime;
 
@@ -2069,12 +2214,13 @@ static NOINLINE void ntp_init(char **argv)
        unsigned opts;
        llist_t *peers;
 
-       srandom(getpid());
+       srand(getpid());
 
        if (getuid())
                bb_error_msg_and_die(bb_msg_you_must_be_root);
 
        /* Set some globals */
+       G.discipline_jitter = G_precision_sec;
        G.stratum = MAXSTRAT;
        if (BURSTPOLL != 0)
                G.poll_exp = BURSTPOLL; /* speeds up initial sync */
@@ -2082,40 +2228,72 @@ static NOINLINE void ntp_init(char **argv)
 
        /* Parse options */
        peers = NULL;
-       opt_complementary = "dd:p::wn"; /* d: counter; p: list; -w implies -n */
+       opt_complementary = "dd:wn"  /* -d: counter; -p: list; -w implies -n */
+               IF_FEATURE_NTPD_SERVER(":Il"); /* -I implies -l */
        opts = getopt32(argv,
                        "nqNx" /* compat */
-                       "wp:S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
+                       "wp:*S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
+                       IF_FEATURE_NTPD_SERVER("I:") /* compat */
                        "d" /* compat */
                        "46aAbgL", /* compat, ignored */
-                       &peers, &G.script_name, &G.verbose);
-       if (!(opts & (OPT_p|OPT_l)))
-               bb_show_usage();
+                       &peers, &G.script_name,
+#if ENABLE_FEATURE_NTPD_SERVER
+                       &G.if_name,
+#endif
+                       &G.verbose);
+
 //     if (opts & OPT_x) /* disable stepping, only slew is allowed */
 //             G.time_was_stepped = 1;
-       if (peers) {
-               while (peers)
-                       add_peers(llist_pop(&peers));
-       } else {
-               /* -l but no peers: "stratum 1 server" mode */
-               G.stratum = 1;
-       }
-       if (!(opts & OPT_n)) {
-               bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
-               logmode = LOGMODE_NONE;
-       }
+
 #if ENABLE_FEATURE_NTPD_SERVER
        G_listen_fd = -1;
        if (opts & OPT_l) {
                G_listen_fd = create_and_bind_dgram_or_die(NULL, 123);
+               if (G.if_name) {
+                       if (setsockopt_bindtodevice(G_listen_fd, G.if_name))
+                               xfunc_die();
+               }
                socket_want_pktinfo(G_listen_fd);
-               setsockopt(G_listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
+               setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY);
        }
 #endif
        /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */
        if (opts & OPT_N)
                setpriority(PRIO_PROCESS, 0, -15);
 
+       if (!(opts & OPT_n)) {
+               bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
+               logmode = LOGMODE_NONE;
+       }
+
+       if (peers) {
+               while (peers)
+                       add_peers(llist_pop(&peers));
+       }
+#if ENABLE_FEATURE_NTPD_CONF
+       else {
+               parser_t *parser;
+               char *token[3];
+
+               parser = config_open("/etc/ntp.conf");
+               while (config_read(parser, token, 3, 1, "# \t", PARSE_NORMAL)) {
+                       if (strcmp(token[0], "server") == 0 && token[1]) {
+                               add_peers(token[1]);
+                               continue;
+                       }
+                       bb_error_msg("skipping %s:%u: unimplemented command '%s'",
+                               "/etc/ntp.conf", parser->lineno, token[0]
+                       );
+               }
+               config_close(parser);
+       }
+#endif
+       if (G.peer_cnt == 0) {
+               if (!(opts & OPT_l))
+                       bb_show_usage();
+               /* -l but no peers: "stratum 1 server" mode */
+               G.stratum = 1;
+       }
        /* If network is up, syncronization occurs in ~10 seconds.
         * We give "ntpd -q" 10 seconds to get first reply,
         * then another 50 seconds to finish syncing.
@@ -2199,16 +2377,26 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
                                if (p->p_fd == -1) {
                                        /* Time to send new req */
                                        if (--cnt == 0) {
-                                               G.initial_poll_complete = 1;
+                                               VERB4 bb_error_msg("disabling burst mode");
+                                               G.polladj_count = 0;
+                                               G.poll_exp = MINPOLL;
                                        }
                                        send_query_to_peer(p);
                                } else {
                                        /* Timed out waiting for reply */
                                        close(p->p_fd);
                                        p->p_fd = -1;
-                                       timeout = poll_interval(-2); /* -2: try a bit sooner */
+                                       /* If poll interval is small, increase it */
+                                       if (G.poll_exp < BIGPOLL)
+                                               adjust_poll(MINPOLL);
+                                       timeout = poll_interval(NOREPLY_INTERVAL);
                                        bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us",
                                                        p->p_dotted, p->reachable_bits, timeout);
+
+                                       /* What if don't see it because it changed its IP? */
+                                       if (p->reachable_bits == 0)
+                                               resolve_peer_hostname(p);
+
                                        set_next(p, timeout);
                                }
                        }
@@ -2297,9 +2485,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
                                        goto have_reachable_peer;
                        }
                        /* No peer responded for last 8 packets, panic */
-                       G.polladj_count = 0;
-                       G.poll_exp = MINPOLL;
-                       G.stratum = MAXSTRAT;
+                       clamp_pollexp_and_set_MAXSTRAT();
                        run_script("unsync", 0.0);
  have_reachable_peer: ;
                }