48ea54
diff -up chrony-3.2/configure.timestamping chrony-3.2/configure
48ea54
--- chrony-3.2/configure.timestamping	2017-09-19 13:58:21.093194558 +0200
48ea54
+++ chrony-3.2/configure	2017-09-19 13:57:56.109116276 +0200
48ea54
@@ -683,15 +683,15 @@ if [ $feat_timestamping = "1" ] && [ $tr
48ea54
   test_code 'SW/HW timestamping' 'sys/types.h sys/socket.h linux/net_tstamp.h
48ea54
                                   linux/errqueue.h linux/ptp_clock.h' '' '' '
48ea54
     int val = SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE |
48ea54
-              SOF_TIMESTAMPING_RAW_HARDWARE | SOF_TIMESTAMPING_OPT_CMSG;
48ea54
-    return sizeof (struct scm_timestamping) + SCM_TSTAMP_SND + PTP_SYS_OFFSET +
48ea54
+              SOF_TIMESTAMPING_RAW_HARDWARE | 1;
48ea54
+    return 3 * sizeof (struct timespec) + 0 + PTP_SYS_OFFSET +
48ea54
            setsockopt(0, SOL_SOCKET, SO_SELECT_ERR_QUEUE + SO_TIMESTAMPING,
48ea54
                       &val, sizeof (val));'
48ea54
 then
48ea54
   add_def HAVE_LINUX_TIMESTAMPING
48ea54
   EXTRA_OBJECTS="$EXTRA_OBJECTS hwclock.o ntp_io_linux.o"
48ea54
 
48ea54
-  if test_code 'other timestamping options' \
48ea54
+  if true || test_code 'other timestamping options' \
48ea54
     'sys/types.h sys/socket.h linux/net_tstamp.h' '' '' '
48ea54
     struct scm_ts_pktinfo pktinfo;
48ea54
     pktinfo.if_index = pktinfo.pkt_length = 0;
48ea54
diff -up chrony-3.2/doc/chrony.conf.man.in.timestamping chrony-3.2/doc/chrony.conf.man.in
48ea54
--- chrony-3.2/doc/chrony.conf.man.in.timestamping	2017-09-15 10:17:41.000000000 +0200
48ea54
+++ chrony-3.2/doc/chrony.conf.man.in	2017-09-19 13:52:59.544187046 +0200
48ea54
@@ -3201,13 +3201,12 @@ timestamping. If the server or peer supp
48ea54
 be enabled by the \fBxleave\fP option in the \fBserver\fP or the
48ea54
 \fBpeer\fP directive.
48ea54
 .sp
48ea54
-This directive is supported on Linux 3.19 and newer. The NIC must support HW
48ea54
+This directive is supported on Linux. The NIC must support HW
48ea54
 timestamping, which can be verified with the \fBethtool \-T\fP command. The list of
48ea54
 capabilities should include \fISOF_TIMESTAMPING_RAW_HARDWARE\fP,
48ea54
 \fISOF_TIMESTAMPING_TX_HARDWARE\fP, and \fISOF_TIMESTAMPING_RX_HARDWARE\fP. Receive
48ea54
 filter \fIHWTSTAMP_FILTER_ALL\fP, or \fIHWTSTAMP_FILTER_NTP_ALL\fP, is necessary for
48ea54
-timestamping of received packets. Timestamping of packets received from bridged
48ea54
-and bonded interfaces is supported on Linux 4.13 and newer. When \fBchronyd\fP is
48ea54
+timestamping of received packets. When \fBchronyd\fP is
48ea54
 running, no other process (e.g. a PTP daemon) should be working with the NIC
48ea54
 clock.
48ea54
 .sp
48ea54
diff -up chrony-3.2/ntp_io_linux.c.timestamping chrony-3.2/ntp_io_linux.c
48ea54
--- chrony-3.2/ntp_io_linux.c.timestamping	2017-09-15 08:32:09.000000000 +0200
48ea54
+++ chrony-3.2/ntp_io_linux.c	2017-09-19 13:52:59.544187046 +0200
48ea54
@@ -35,6 +35,16 @@
48ea54
 #include <linux/sockios.h>
48ea54
 #include <net/if.h>
48ea54
 
48ea54
+/* Missing in older kernel headers */
48ea54
+#define SOF_TIMESTAMPING_OPT_CMSG (1<<10)
48ea54
+#define SOF_TIMESTAMPING_OPT_PKTINFO (1<<13)
48ea54
+#define SOF_TIMESTAMPING_OPT_TX_SWHW (1<<14)
48ea54
+#define SCM_TSTAMP_SND 0
48ea54
+#define HWTSTAMP_FILTER_NTP_ALL 15
48ea54
+#ifndef SCM_TIMESTAMPING_PKTINFO
48ea54
+#define SCM_TIMESTAMPING_PKTINFO 58
48ea54
+#endif
48ea54
+
48ea54
 #include "array.h"
48ea54
 #include "conf.h"
48ea54
 #include "hwclock.h"
48ea54
@@ -94,6 +104,10 @@ static int ts_tx_flags;
48ea54
 /* Flag indicating the socket options can't be changed in control messages */
48ea54
 static int permanent_ts_options;
48ea54
 
48ea54
+/* Index of a HW-timestamping interface, but only if the machine has not more
48ea54
+   than one */
48ea54
+static int single_hwts_if_index;
48ea54
+
48ea54
 /* ================================================== */
48ea54
 
48ea54
 static int
48ea54
@@ -278,7 +292,7 @@ update_interface_speed(struct Interface
48ea54
 
48ea54
 /* ================================================== */
48ea54
 
48ea54
-#if defined(HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO) || defined(HAVE_LINUX_TIMESTAMPING_OPT_TX_SWHW)
48ea54
+#if 1
48ea54
 static int
48ea54
 check_timestamping_option(int option)
48ea54
 {
48ea54
@@ -301,6 +315,61 @@ check_timestamping_option(int option)
48ea54
 
48ea54
 /* ================================================== */
48ea54
 
48ea54
+static int
48ea54
+get_single_hwts_index()
48ea54
+{
48ea54
+  struct ifaddrs *ifaddr, *ifa;
48ea54
+  struct ethtool_ts_info ts_info;
48ea54
+  struct ifreq req;
48ea54
+  int sock_fd, if_index, hwts_if_index = INVALID_IF_INDEX;
48ea54
+
48ea54
+  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
48ea54
+  if (sock_fd < 0)
48ea54
+    return INVALID_IF_INDEX;
48ea54
+
48ea54
+  if (getifaddrs(&ifaddr)) {
48ea54
+    DEBUG_LOG("getifaddrs() failed : %s", strerror(errno));
48ea54
+    close(sock_fd);
48ea54
+    return INVALID_IF_INDEX;
48ea54
+  }
48ea54
+
48ea54
+  for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
48ea54
+    memset(&req, 0, sizeof (req));
48ea54
+    memset(&ts_info, 0, sizeof (ts_info));
48ea54
+
48ea54
+    if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", ifa->ifa_name) >=
48ea54
+        sizeof (req.ifr_name))
48ea54
+      break;
48ea54
+
48ea54
+    if (ioctl(sock_fd, SIOCGIFINDEX, &req))
48ea54
+      break;
48ea54
+
48ea54
+    if_index = req.ifr_ifindex;
48ea54
+    ts_info.cmd = ETHTOOL_GET_TS_INFO;
48ea54
+    req.ifr_data = (char *)&ts_info;
48ea54
+
48ea54
+    if (ioctl(sock_fd, SIOCETHTOOL, &req))
48ea54
+      break;
48ea54
+
48ea54
+    if (ts_info.phc_index < 0)
48ea54
+      continue;
48ea54
+
48ea54
+    if (hwts_if_index != INVALID_IF_INDEX && hwts_if_index != if_index)
48ea54
+      break;
48ea54
+
48ea54
+    hwts_if_index = if_index;
48ea54
+  }
48ea54
+
48ea54
+  close(sock_fd);
48ea54
+  freeifaddrs(ifaddr);
48ea54
+
48ea54
+  if (ifa)
48ea54
+    return INVALID_IF_INDEX;
48ea54
+
48ea54
+  return hwts_if_index;
48ea54
+}
48ea54
+
48ea54
+/* ================================================== */
48ea54
 void
48ea54
 NIO_Linux_Initialise(void)
48ea54
 {
48ea54
@@ -345,8 +414,20 @@ NIO_Linux_Initialise(void)
48ea54
 #endif
48ea54
   }
48ea54
 
48ea54
-  /* Enable IP_PKTINFO in messages looped back to the error queue */
48ea54
-  ts_flags |= SOF_TIMESTAMPING_OPT_CMSG;
48ea54
+  single_hwts_if_index = INVALID_IF_INDEX;
48ea54
+
48ea54
+  /* Enable IP_PKTINFO in messages looped back to the error queue if possible.
48ea54
+     If not, HW timestamping of IPv4 packets can be supported only with one
48ea54
+     interface capable of HW timestamping. */
48ea54
+  if (check_timestamping_option(SOF_TIMESTAMPING_OPT_CMSG)) {
48ea54
+    ts_flags |= SOF_TIMESTAMPING_OPT_CMSG;
48ea54
+  } else if (ARR_GetSize(interfaces) > 0) {
48ea54
+    single_hwts_if_index = get_single_hwts_index();
48ea54
+    if (single_hwts_if_index == INVALID_IF_INDEX)
48ea54
+      LOG(LOGS_WARN, "Missing SOF_TIMESTAMPING_OPT_CMSG option for HW timestamping with multiple HW-timestamping interfaces");
48ea54
+    else
48ea54
+      LOG(LOGS_INFO, "Enabled single-interface HW-timestamping mode");
48ea54
+  }
48ea54
 
48ea54
   /* Kernels before 4.7 ignore timestamping flags set in control messages */
48ea54
   permanent_ts_options = !SYS_Linux_CheckKernelVersion(4, 7);
48ea54
@@ -590,7 +671,11 @@ NIO_Linux_ProcessMessage(NTP_Remote_Addr
48ea54
   for (cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) {
48ea54
 #ifdef HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO
48ea54
     if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING_PKTINFO) {
48ea54
-      struct scm_ts_pktinfo ts_pktinfo;
48ea54
+      struct {
48ea54
+	      __u32 if_index;
48ea54
+	      __u32 pkt_length;
48ea54
+	      __u32 reserved[2];
48ea54
+      } ts_pktinfo;
48ea54
 
48ea54
       memcpy(&ts_pktinfo, CMSG_DATA(cmsg), sizeof (ts_pktinfo));
48ea54
 
48ea54
@@ -602,11 +687,16 @@ NIO_Linux_ProcessMessage(NTP_Remote_Addr
48ea54
 #endif
48ea54
 
48ea54
     if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING) {
48ea54
-      struct scm_timestamping ts3;
48ea54
+      struct {
48ea54
+        struct timespec ts[3];
48ea54
+      } ts3;
48ea54
 
48ea54
       memcpy(&ts3, CMSG_DATA(cmsg), sizeof (ts3));
48ea54
 
48ea54
       if (!UTI_IsZeroTimespec(&ts3.ts[2])) {
48ea54
+        if (ts_if_index == INVALID_IF_INDEX)
48ea54
+          ts_if_index = single_hwts_if_index;
48ea54
+
48ea54
         iface = get_interface(ts_if_index);
48ea54
         if (iface) {
48ea54
           process_hw_timestamp(iface, &ts3.ts[2], local_ts, !is_tx ? length : 0,