[Git][NTPsec/ntpsec][master] 4 commits: Another comment in ntp_sandbox about blocking SIGSYS

Hal Murray gitlab at mg.gitlab.com
Tue Jun 6 23:21:11 UTC 2017


Hal Murray pushed to branch master at NTPsec / ntpsec


Commits:
0ab49c87 by Hal Murray at 2017-06-06T16:19:49-07:00
Another comment in ntp_sandbox about blocking SIGSYS

- - - - -
a893edc7 by Hal Murray at 2017-06-06T16:19:49-07:00
Remove USE_PACKET_TIMESTAMP

It stopped working when ntp_packetstamp.c was split out
of ntp_io.c.  USE_PACKET_TIMESTAMP was defined in ntp_packetstamp.c
but referenced in ntp_io.c.

All modern systems support some form of capturing recv
time stamps via recvmsg.  The current code is using a time
stamp from shortly after the select returns.  We can restore
that action if necessary.  It seems to work OK on lightly
loaded systems.  (Very old code used SIGIO to capture
time stamps.)

- - - - -
c4dad229 by Hal Murray at 2017-06-06T16:19:49-07:00
Fix for Issue #335 (-Wsign-conversion)

- - - - -
7bb7a656 by Hal Murray at 2017-06-06T16:19:49-07:00
General cleanup of ntpd/ntp_packetstamp.c

It will crash if it can't use one of:
  SO_BINTIME, SO_TIMESTAMPNS,or SO_TIMESTAMP
It will crash if recvmsg doesn't get the expected timestamp data.
Hack around SO_BINTIME on FreeBSD not working with IPv6.

- - - - -


5 changed files:

- devel/ifdex-ignores
- ntpd/ntp_io.c
- ntpd/ntp_packetstamp.c
- ntpd/ntp_proto.c
- ntpd/ntp_sandbox.c


Changes:

=====================================
devel/ifdex-ignores
=====================================
--- a/devel/ifdex-ignores
+++ b/devel/ifdex-ignores
@@ -144,7 +144,6 @@ UNUSED_LOCAL		# Used to quiet compiler warnings
 UPDATE_GRACE		# ntp_io.c only
 USE_COMPILETIME_PIVOT	# Use build date in disambiguating time.
 
-USE_PACKET_TIMESTAMP    # setup and used in ntp_io
 USE_SCM_BINTIME		# to grab timestamp for recv packet
 USE_SCM_TIMESTAMP	# "
 USE_SCM_TIMESTAMPNS	# "


=====================================
ntpd/ntp_io.c
=====================================
--- a/ntpd/ntp_io.c
+++ b/ntpd/ntp_io.c
@@ -2040,9 +2040,7 @@ open_socket(
 		return INVALID_SOCKET;
 	}
 
-#ifdef USE_PACKET_TIMESTAMP
-	enable_packetstamps(fd);
-#endif /* USE_PACKET_TIMESTAMP */
+	enable_packetstamps(fd, addr);
 	
 	DPRINT(4, ("bind(%d) AF_INET%s, addr %s%%%u#%d, flags 0x%x\n",
 		   fd, IS_IPV6(addr) ? "6" : "", socktoa(addr),
@@ -2192,11 +2190,9 @@ read_network_packet(
 	socklen_t fromlen;
 	ssize_t buflen;
 	register struct recvbuf *rb;
-#ifdef USE_PACKET_TIMESTAMP
 	struct msghdr msghdr;
 	struct iovec iovec;
-	char control[CMSG_BUFSIZE];
-#endif
+	char control[100];   /* FIXME: Need space for time stamp plus overhead */
 
 	/*
 	 * Get a buffer and read the frame.  If we
@@ -2230,11 +2226,6 @@ read_network_packet(
 
 	fromlen = sizeof(rb->recv_srcadr);
 
-#ifndef USE_PACKET_TIMESTAMP
-	rb->recv_length = (size_t)recvfrom(fd, (char *)&rb->recv_space,
-				           sizeof(rb->recv_space), 0,
-				           &rb->recv_srcadr.sa, &fromlen);
-#else
 	iovec.iov_base        = &rb->recv_space;
 	iovec.iov_len         = sizeof(rb->recv_space);
 	msghdr.msg_name       = &rb->recv_srcadr;
@@ -2245,7 +2236,6 @@ read_network_packet(
 	msghdr.msg_control    = (void *)&control;
 	msghdr.msg_controllen = sizeof(control);
 	rb->recv_length       = recvmsg(fd, &msghdr, 0);
-#endif
 
 	buflen = (ssize_t)rb->recv_length;
 
@@ -2305,10 +2295,7 @@ read_network_packet(
 	rb->dstadr = itf;
 	rb->cast_flags = (uint8_t)(rb->fd == rb->dstadr->bfd ? MDF_BCAST : MDF_UCAST);
 	rb->fd = fd;
-#ifdef USE_PACKET_TIMESTAMP
-	/* pick up a network time stamp if possible */
 	ts = fetch_packetstamp(rb, &msghdr, ts);
-#endif
 	rb->recv_time = ts;
 	rb->receiver = receive;
 #ifdef REFCLOCK


=====================================
ntpd/ntp_packetstamp.c
=====================================
--- a/ntpd/ntp_packetstamp.c
+++ b/ntpd/ntp_packetstamp.c
@@ -17,38 +17,29 @@
 #include "ntp_stdlib.h"
 #include "timespecops.h"
 
-#if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR)
-#  define USE_PACKET_TIMESTAMP
-#  define USE_SCM_BINTIME
-/* UNUSED
- * #  ifdef OVERRIDE_BINTIME_CTLMSGBUF_SIZE
- * #   define CMSG_BUFSIZE OVERRIDE_BINTIME_CTLMSGBUF_SIZE
- * #  else
- * #   define CMSG_BUFSIZE  1536 * moderate default *
- * #  endif
- */
-#elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
-#  define USE_PACKET_TIMESTAMP
-#  define USE_SCM_TIMESTAMPNS
-/* UNUSED
- * #  ifdef OVERRIDE_TIMESTAMPNS_CTLMSGBUF_SIZE
- * #   define CMSG_BUFSIZE OVERRIDE_TIMESTAMPNS_CTLMSGBUF_SIZE
- * #  else
- * #   define CMSG_BUFSIZE  1536 * moderate default *
- * #  endif
+/* We handle 3 flavors of timestamp:
+ * SO_BINTIME/SCM_BINTIME          FreeBSD
+ *   bintime documentation is at
+ *   http://phk.freebsd.dk/pubs/timecounter.pdf
+ * SO_TIMESTAMPNS/SCM_TIMESTAMPNS  Linux
+ * SO_TIMESTAMP/SCM_TIMESTAMP      FreeBSD, NetBSD, OpenBSD, Linux
+ *
+ * Linux supports both SO_TIMESTAMP and SO_TIMESTAMPNS so it's
+ * important to check for SO_TIMESTAMPNS first to get the better accuracy.
+ *
+ * Note that the if/elif tests are done in several places.
+ * It's important that they all check in the same order to
+ * be consistent in case some systems support more than one.
+ *
+ * If SO_xxx exists, we assume that SCM_xxx does too.
+ * All flavors assume the CMSG_xxx macros exist.
  */
-#elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
-#  define USE_PACKET_TIMESTAMP
-#  define USE_SCM_TIMESTAMP
-/* UNUSED
- * #  ifdef OVERRIDE_TIMESTAMP_CTLMSGBUF_SIZE
- * #   define CMSG_BUFSIZE OVERRIDE_TIMESTAMP_CTLMSGBUF_SIZE
- * #  else
- * #   define CMSG_BUFSIZE  1536 * moderate default *
- * #  endif
+
+#ifdef SO_BINTIME
+/* SO_BINTIME doesn't work for IpV6, FreeBSD 11, 2017-Jan
+ * fortunately, FreeBSD also supports SO_TIMESTAMP
  */
-#else
-/* fill in for old/other timestamp interfaces */
+#undef SO_BINTIME
 #endif
 
 void
@@ -57,52 +48,43 @@ enable_packetstamps(
     sockaddr_u *	addr
     )
 {
+	const int	on = 1;
 
-#ifdef USE_SCM_TIMESTAMP
-	{
-		const int	on = 1;
-		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
-			       (const void*)&on, sizeof(on)))
-			msyslog(LOG_DEBUG,
-				"setsockopt SO_TIMESTAMP on fails on address %s: %m",
-				socktoa(addr));
-		else
-			DPRINT(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
-				   fd, socktoa(addr)));
-	}
-#elif defined (USE_SCM_TIMESTAMPNS)
-	{
-		const int	on = 1;
-		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
+#if defined(SO_BINTIME)
+	if (setsockopt(fd, SOL_SOCKET, SO_BINTIME, (const void*)&on, sizeof(on)))
+		msyslog(LOG_DEBUG,
+			"setsockopt SO_BINTIME on fails on address %s: %m",
+			socktoa(addr));
+	else
+		DPRINT(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
+			    fd, socktoa(addr)));
+		msyslog(LOG_INFO, "Using SO_BINTIME on %s", socktoa(addr));
+#elif defined (SO_TIMESTAMPNS)
+	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
 			       (const void *)&on, sizeof(on)))
-			msyslog(LOG_DEBUG,
-				"setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
+		msyslog(LOG_DEBUG,
+			"setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
 				socktoa(addr));
-		else
-			DPRINT(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
-				   fd, socktoa(addr)));
-	}
-#elif defined(USE_SCM_BINTIME)
-	{
-		const int	on = 1;
-		if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
+	else
+		DPRINT(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
+				    fd, socktoa(addr)));
+		msyslog(LOG_INFO, "Using SO_TIMESTAMPNS on %s", socktoa(addr));
+#elif defined(SO_TIMESTAMP)
+	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
 			       (const void*)&on, sizeof(on)))
-			msyslog(LOG_DEBUG,
-				"setsockopt SO_BINTIME on fails on address %s: %m",
-				socktoa(addr));
-		else
-			DPRINT(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
-				   fd, socktoa(addr)));
-	}
+		msyslog(LOG_DEBUG,
+			"setsockopt SO_TIMESTAMP on fails on address %s: %m",
+			socktoa(addr));
+	else
+		DPRINT(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
+			    fd, socktoa(addr)));
+		msyslog(LOG_INFO, "Using SO_TIMESTAMP on %s", socktoa(addr));
 #else
-        /* probably Slowlaris */
-        UNUSED_ARG(fd);
-        UNUSED_ARG(addr);
+# error "Can't get packet timestamp"
 #endif
 }
 
 
-#ifdef USE_PACKET_TIMESTAMP
 /*
  * extract timestamps from control message buffer
  */
@@ -114,19 +96,17 @@ fetch_packetstamp(
 	)
 {
 	struct cmsghdr *	cmsghdr;
-#ifdef USE_SCM_BINTIME
+#if defined(SO_BINTIME)
 	struct bintime *	btp;
-#endif
-#ifdef USE_SCM_TIMESTAMPNS
+#elif defined(SO_TIMESTAMPNS)
 	struct timespec *	tsp;
-#endif
-#ifdef USE_SCM_TIMESTAMP
+#elif defined(SO_TIMESTAMP)
 	struct timeval *	tvp;
 #endif
 	unsigned long		ticks;
 	double			fuzz;
 	l_fp			lfpfuzz;
-	l_fp			nts = 0;
+	l_fp			nts = 0;  /* network time stamp */
 #ifdef ENABLE_DEBUG_TIMING
 	l_fp			dts;
 #endif
@@ -135,97 +115,92 @@ fetch_packetstamp(
 	UNUSED_ARG(rb);
 #endif
 
+/* There should be only one cmsg. */
 	cmsghdr = CMSG_FIRSTHDR(msghdr);
-	while (cmsghdr != NULL) {
-		switch (cmsghdr->cmsg_type)
-		{
-#ifdef USE_SCM_BINTIME
-		case SCM_BINTIME:
-#endif  /* USE_SCM_BINTIME */
-#ifdef USE_SCM_TIMESTAMPNS
-		case SCM_TIMESTAMPNS:
-#endif	/* USE_SCM_TIMESTAMPNS */
-#ifdef USE_SCM_TIMESTAMP
-		case SCM_TIMESTAMP:
-#endif	/* USE_SCM_TIMESTAMP */
-#if defined(USE_SCM_BINTIME) || defined (USE_SCM_TIMESTAMPNS) || defined(USE_SCM_TIMESTAMP)
-			switch (cmsghdr->cmsg_type)
-			{
-#ifdef USE_SCM_BINTIME
-			case SCM_BINTIME:
-				btp = (struct bintime *)CMSG_DATA(cmsghdr);
-				/*
-				 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
-				 */
-				setlfpuint(nts, btp->sec + JAN_1970);
-				setlfpfrac(nts, (uint32_t)(btp->frac >> 32));
-				if (sys_tick > measured_tick &&
-				    sys_tick > S_PER_NS) {
-				    ticks = (unsigned long)(lfpfrac(nts) / (unsigned long)(sys_tick * FRAC));
-				    setlfpfrac(nts, (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC)));
-				}
-                                DPRINT(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
-					   (long)btp->sec, (unsigned long)((lfpfrac(nts) / FRAC) * 1e9)));
-				break;
-#endif  /* USE_SCM_BINTIME */
-#ifdef USE_SCM_TIMESTAMPNS
-			case SCM_TIMESTAMPNS:
-				tsp = (struct timespec *)CMSG_DATA(cmsghdr);
-				if (sys_tick > measured_tick &&
-				    sys_tick > S_PER_NS) {
-					ticks = (unsigned long)
-                                            ((tsp->tv_nsec * S_PER_NS) /
-						       sys_tick);
-					tsp->tv_nsec = (long)(ticks * NS_PER_S *
-							      sys_tick);
-				}
-				DPRINT(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
-					   tsp->tv_sec, tsp->tv_nsec));
-				nts = tspec_stamp_to_lfp(*tsp);
-				break;
-#endif	/* USE_SCM_TIMESTAMPNS */
-#ifdef USE_SCM_TIMESTAMP
-			case SCM_TIMESTAMP:
-				tvp = (struct timeval *)CMSG_DATA(cmsghdr);
-				if (sys_tick > measured_tick &&
-				    sys_tick > 1e-6) {
-					ticks = (unsigned long)((tvp->tv_usec * 1e-6) /
-						       sys_tick);
-					tvp->tv_usec = (long)(ticks * US_PER_S *
-							      sys_tick);
-				}
-				DPRINT(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
-					   (intmax_t)tvp->tv_sec, (long)tvp->tv_usec));
-				nts = tspec_stamp_to_lfp(tval_to_tspec(*tvp));
-				break;
-#endif  /* USE_SCM_TIMESTAMP */
-			default:
-                                /* huh? */
-                                break;
-			}
-			fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
-			lfpfuzz = dtolfp(fuzz);
-			nts += lfpfuzz;
+	if (NULL == cmsghdr) {
+		DPRINT(4, ("fetch_timestamp: can't find timestamp\n"));
+		msyslog(LOG_ERR,
+			"fetch_timestamp: no msghdrs, %s",
+			socktoa(&rb->recv_srcadr));
+		exit(2);
+		/* return ts;	** Kludge to use time from select. */
+	}
+#if defined(SO_BINTIME)
+	if (SCM_BINTIME != cmsghdr->cmsg_type) {
+#elif defined(SO_TIMESTAMPNS)
+	if (SCM_TIMESTAMPNS != cmsghdr->cmsg_type) {
+#elif defined(SO_TIMESTAMP)
+	if (SCM_TIMESTAMP != cmsghdr->cmsg_type) {
+#else
+# error "Can't get packet timestamp"
+#endif
+		DPRINT(4,	
+                        ("fetch_timestamp: strange control message 0x%x\n",
+			     (unsigned)cmsghdr->cmsg_type));
+		msyslog(LOG_ERR,
+			"fetch_timestamp: strange control message 0x%x",
+                             (unsigned)cmsghdr->cmsg_type);
+		exit(2);
+		/* Could loop and skip strange types. */
+		/* cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr); */
+	}
+
+/* Debugging hacks */
+{
+  static bool once = false;
+  if (!once) {
+    once = true;
+    msyslog(LOG_INFO, "stamp: sys_tick %.3f, measured_tick: %.3f",
+      sys_tick*1E9, measured_tick*1E9);
+  }
+}
+
+/* cmsghdr now points to a timestamp slot */
+
+#if defined(SO_BINTIME)
+	btp = (struct bintime *)CMSG_DATA(cmsghdr);
+	setlfpuint(nts, btp->sec + JAN_1970);
+	setlfpfrac(nts, (uint32_t)(btp->frac >> 32));
+	if (sys_tick > measured_tick && sys_tick > S_PER_NS) {
+	    ticks = (unsigned long)(lfpfrac(nts) / (unsigned long)(sys_tick * FRAC));
+	    setlfpfrac(nts, (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC)));
+	}
+	DPRINT(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
+		(long)btp->sec, (unsigned long)((lfpfrac(nts) / FRAC) * 1e9)));
+#elif defined(SO_TIMESTAMPNS)
+	tsp = (struct timespec *)CMSG_DATA(cmsghdr);
+	if (sys_tick > measured_tick && sys_tick > S_PER_NS) {
+	    ticks = (unsigned long) ((tsp->tv_nsec * S_PER_NS) / sys_tick);
+	    tsp->tv_nsec = (long) (ticks * NS_PER_S * sys_tick);
+	}
+	DPRINT(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
+		tsp->tv_sec, tsp->tv_nsec));
+	nts = tspec_stamp_to_lfp(*tsp);
+#elif defined(SO_TIMESTAMP)
+	tvp = (struct timeval *)CMSG_DATA(cmsghdr);
+	if (sys_tick > measured_tick && sys_tick > S_PER_NS) {
+	    ticks = (unsigned long) ((tvp->tv_usec * S_PER_NS) / sys_tick);
+	    tvp->tv_usec = (long)(ticks * US_PER_S * sys_tick);
+	}
+	DPRINT(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
+		(intmax_t)tvp->tv_sec, (long)tvp->tv_usec));
+	nts = tspec_stamp_to_lfp(tval_to_tspec(*tvp));
+#else
+# error "Can't get packet timestamp"
+#endif
+	fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
+	lfpfuzz = dtolfp(fuzz);
+	nts += lfpfuzz;
 #ifdef ENABLE_DEBUG_TIMING
-			dts = ts;
-			dts -= nts;
-			collect_timing(rb, "input processing delay", 1, dts);
-			DPRINT(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
-				   lfptoa(dts, 9)));
+	dts = ts;
+	dts -= nts;
+	collect_timing(rb, "input processing delay", 1, dts);
+	DPRINT(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
+		lfptoa(dts, 9)));
 #endif	/* ENABLE_DEBUG_TIMING */
-			ts = nts;  /* network time stamp */
-			break;
-#endif	/* USE_SCM_BINTIME || USE_SCM_TIMESTAMPNS || USE_SCM_TIMESTAMP */
+	ts = nts;
 
-		default:
-			DPRINT(4,
-			       ("fetch_timestamp: skipping control message 0x%x\n",
-				(unsigned)cmsghdr->cmsg_type));
-		}
-		cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
-	}
 	return ts;
 }
-#endif	/* USE_PACKET_TIMESTAMP */
 
 // end


=====================================
ntpd/ntp_proto.c
=====================================
--- a/ntpd/ntp_proto.c
+++ b/ntpd/ntp_proto.c
@@ -2534,7 +2534,7 @@ void dns_take_status(struct peer* peer, DNS_Status status) {
 	if (0 == hpoll)
 		return; /* hpoll already in use by new server */
 	peer->hpoll = hpoll;
-	peer->nextdate = current_time + (1 << hpoll);
+	peer->nextdate = current_time + (1U << hpoll);
 }
 
 


=====================================
ntpd/ntp_sandbox.c
=====================================
--- a/ntpd/ntp_sandbox.c
+++ b/ntpd/ntp_sandbox.c
@@ -430,9 +430,6 @@ int scmp_sc[] = {
  * catchTrap - get here if something missing from list above
  * (or a bad guy finds a way in)
  *
- * You won't get here if SIGSYS is blocked.
- * That happens in pthread_create()  See above at ENABLE_DNS_LOOKUP
- *
  * The list above is a moving target.  Most syscalls will be
  * obvious but libc (and friends) can remap things and
  * getaddrinfo does all sorts of syscalls.



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/c3869fe513715f070562bcfb1a68910facab8a32...7bb7a656cbabd4be451d35c6a6058fac9ca8a56d

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/c3869fe513715f070562bcfb1a68910facab8a32...7bb7a656cbabd4be451d35c6a6058fac9ca8a56d
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170606/8a018c6a/attachment.html>


More information about the vc mailing list