From d0ffa3e570db588cd1d13ba6142344fec7de908b Mon Sep 17 00:00:00 2001
From: Shenghao Yang <me@shenghaoyang.info>
Date: Tue, 17 Sep 2024 22:12:08 +0800
Subject: [PATCH] linuxptp: update to v4.4

- Replaced SF project and download URLs with nwtime ones.
  The project is now hosted at https://linuxptp.nwtime.org/.

- Removed 020-gcc14.patch. Missing include was fixed upstream
  in v4.2.

- Added patch to disable MAC library autodetection. That
  can silently pick up unwanted dependencies, depending on
  package build order. We can add linuxptp-<mac lib> variants
  of this package later if there are users of authenticated
  PTP.

Signed-off-by: Shenghao Yang <me@shenghaoyang.info>
---
 net/linuxptp/Makefile                         |  13 +-
 .../patches/001-fix_kbuild_output.patch       |   4 +-
 ...20-allow-disabling-MAC-autodetection.patch | 195 ++++++++++++++++++
 net/linuxptp/patches/020-gcc14.patch          |  10 -
 4 files changed, 204 insertions(+), 18 deletions(-)
 create mode 100644 net/linuxptp/patches/020-allow-disabling-MAC-autodetection.patch
 delete mode 100644 net/linuxptp/patches/020-gcc14.patch

diff --git a/net/linuxptp/Makefile b/net/linuxptp/Makefile
index e0b34d3d7e..67d88aea93 100644
--- a/net/linuxptp/Makefile
+++ b/net/linuxptp/Makefile
@@ -8,12 +8,12 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=linuxptp
-PKG_VERSION:=4.1
-PKG_RELEASE:=2
+PKG_VERSION:=4.4
+PKG_RELEASE:=1
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tgz
-PKG_SOURCE_URL:=@SF/$(PKG_NAME)/v$(PKG_VERSION)
-PKG_HASH:=e1743d44f8208897e30895da3579e670ff919b914feb4b5a949f3e421ddde535
+PKG_SOURCE_URL:=https://downloads.nwtime.org/linuxptp/
+PKG_HASH:=61757bc0a58d789b8fcbdddf56c88a0230597184a70dcb2ac05b4c6b619f7d5c
 
 PKG_MAINTAINER:=Wojciech Dubowik <Wojciech.Dubowik@westermo.com>
 PKG_LICENSE:=GPL-2.0-only
@@ -28,7 +28,7 @@ define Package/linuxptp
   CATEGORY:=Network
   SUBMENU:=Time Synchronization
   TITLE:=Linux Precision Time Protocol (PTP) daemon
-  URL:=http://linuxptp.sourceforge.net/
+  URL:=https://linuxptp.nwtime.org/
 endef
 
 define Package/linuxptp/description
@@ -42,7 +42,8 @@ EXTRA_CFLAGS += -DHAVE_CLOCK_ADJTIME -DHAVE_POSIX_SPAWN -DHAVE_ONESTEP_SYNC
 
 MAKE_VARS += \
 	EXTRA_CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS) $(EXTRA_CFLAGS)" \
-	KBUILD_OUTPUT="$(LINUX_DIR)"
+	KBUILD_OUTPUT="$(LINUX_DIR)" \
+	NO_AUTODETECT_SAD_MAC_LIB="y"
 
 define Package/linuxptp/install
 	$(INSTALL_DIR) $(1)/usr/sbin
diff --git a/net/linuxptp/patches/001-fix_kbuild_output.patch b/net/linuxptp/patches/001-fix_kbuild_output.patch
index 1b5e81e979..a43e5111a9 100644
--- a/net/linuxptp/patches/001-fix_kbuild_output.patch
+++ b/net/linuxptp/patches/001-fix_kbuild_output.patch
@@ -1,11 +1,11 @@
 --- a/incdefs.sh
 +++ b/incdefs.sh
-@@ -62,7 +62,7 @@ user_flags()
+@@ -152,7 +152,7 @@ user_flags()
  kernel_flags()
  {
  	prefix=""
 -	tstamp=/usr/include/linux/net_tstamp.h
 +	tstamp=/include/uapi/linux/net_tstamp.h
  	ptp_clock=/usr/include/linux/ptp_clock.h
+ 	if_team=/usr/include/linux/if_team.h
  
- 	if [ "x$KBUILD_OUTPUT" != "x" ]; then
diff --git a/net/linuxptp/patches/020-allow-disabling-MAC-autodetection.patch b/net/linuxptp/patches/020-allow-disabling-MAC-autodetection.patch
new file mode 100644
index 0000000000..ce714801df
--- /dev/null
+++ b/net/linuxptp/patches/020-allow-disabling-MAC-autodetection.patch
@@ -0,0 +1,195 @@
+Date: Sun, 15 Sep 2024 18:41:20 +0800
+To: linuxptp-devel@lists.nwtime.org
+From: Shenghao Yang <me@shenghaoyang.info>
+Subject: [PATCH] sad: allow disabling MAC library autodetection
+
+Avoids ptp4l linking against MAC libraries in a build
+environment with insufficient isolation (OpenWRT).
+
+NO_AUTODETECT_SAD_MAC_LIB can be set to any value to disable
+autodetection, and then one of the SAD_MAC_* variables can be
+used to choose a preferred MAC implementation.
+
+Signed-off-by: Shenghao Yang <me@shenghaoyang.info>
+---
+ incdefs.sh | 154 ++++++++++++++++++++++++++++-------------------------
+ 1 file changed, 81 insertions(+), 73 deletions(-)
+
+--- a/incdefs.sh
++++ b/incdefs.sh
+@@ -52,94 +52,102 @@ user_flags()
+ 	done
+ 
+ 	# Look for nettle support.
+-	for d in $dirs; do
+-		sdirs=$(find $d -type d -name "nettle")
+-		for s in $sdirs; do
+-			have_hmac="0"
+-			files=$(find $s -type f -name hmac.h)
+-			for f in $files; do
+-				if grep -q hmac_sha256_set_key $f; then
+-					have_hmac="1"
+-					break 1;
+-				fi
+-			done
+-			have_memops="0"
+-			files=$(find $s -type f -name memops.h)
+-			for f in $files; do
+-				if grep -q memeql_sec $f; then
+-					have_memops="1"
+-					break 1;
+-				fi
+-			done
+-			have_nettle_meta="0"
+-			files=$(find $s -type f -name nettle-meta.h)
+-			for f in $files; do
+-				if grep -q nettle_get_macs $f; then
+-					have_nettle_meta="1"
+-					break 1;
++	if [ "x$NO_AUTODETECT_SAD_MAC_LIB" = "x" ] || [ "x$SAD_MAC_NETTLE" != "x" ]; then
++		for d in $dirs; do
++			sdirs=$(find $d -type d -name "nettle")
++			for s in $sdirs; do
++				have_hmac="0"
++				files=$(find $s -type f -name hmac.h)
++				for f in $files; do
++					if grep -q hmac_sha256_set_key $f; then
++						have_hmac="1"
++						break 1;
++					fi
++				done
++				have_memops="0"
++				files=$(find $s -type f -name memops.h)
++				for f in $files; do
++					if grep -q memeql_sec $f; then
++						have_memops="1"
++						break 1;
++					fi
++				done
++				have_nettle_meta="0"
++				files=$(find $s -type f -name nettle-meta.h)
++				for f in $files; do
++					if grep -q nettle_get_macs $f; then
++						have_nettle_meta="1"
++						break 1;
++					fi
++				done
++				if [ $have_hmac = "1" ] &&
++				[ $have_memops = "1" ] &&
++				[ $have_nettle_meta = "1" ]; then
++					printf " -DHAVE_NETTLE"
++					break 2
+ 				fi
+ 			done
+-			if [ $have_hmac = "1" ] &&
+-			   [ $have_memops = "1" ] &&
+-			   [ $have_nettle_meta = "1" ]; then
+-				printf " -DHAVE_NETTLE"
+-				break 2
+-			fi
+ 		done
+-	done
++	fi
+ 
+ 	# Look for gnutls support.
+-	for d in $dirs; do
+-		sdirs=$(find $d -type d -name "gnutls")
+-		for s in $sdirs; do
+-			files=$(find $s -type f -name crypto.h)
+-			for f in $files; do
+-				if grep -q gnutls_hmac_init $f; then
+-					printf " -DHAVE_GNUTLS"
+-					break 3
+-				fi
++	if [ "x$NO_AUTODETECT_SAD_MAC_LIB" = "x" ] || [ "x$SAD_MAC_GNUTLS" != "x" ]; then
++		for d in $dirs; do
++			sdirs=$(find $d -type d -name "gnutls")
++			for s in $sdirs; do
++				files=$(find $s -type f -name crypto.h)
++				for f in $files; do
++					if grep -q gnutls_hmac_init $f; then
++						printf " -DHAVE_GNUTLS"
++						break 3
++					fi
++				done
+ 			done
+ 		done
+-	done
++	fi
+ 
+ 	# Look for gnupg support.
+-	for d in $dirs; do
+-		files=$(find $d -type f -name gcrypt.h)
+-		for f in $files; do
+-			if grep -q gcry_mac_open $f; then
+-				printf " -DHAVE_GNUPG"
+-				break 2
+-			fi
++	if [ "x$NO_AUTODETECT_SAD_MAC_LIB" = "x" ] ||  [ "x$SAD_MAC_GNUPG" != "x" ]; then
++		for d in $dirs; do
++			files=$(find $d -type f -name gcrypt.h)
++			for f in $files; do
++				if grep -q gcry_mac_open $f; then
++					printf " -DHAVE_GNUPG"
++					break 2
++				fi
++			done
+ 		done
+-	done
++	fi
+ 
+ 	# Look for openssl support.
+-	for d in $dirs; do
+-		sdirs=$(find $d -type d -name "openssl")
+-		for s in $sdirs; do
+-			have_crypto="0"
+-			files=$(find $s -type f -name crypto.h)
+-			for f in $files; do
+-				if grep -q CRYPTO_memcmp $f; then
+-					have_crypto="1"
+-					break 1;
++	if [ "x$NO_AUTODETECT_SAD_MAC_LIB" = "x" ] || [ "x$SAD_MAC_OPENSSL" != "x" ]; then
++		for d in $dirs; do
++			sdirs=$(find $d -type d -name "openssl")
++			for s in $sdirs; do
++				have_crypto="0"
++				files=$(find $s -type f -name crypto.h)
++				for f in $files; do
++					if grep -q CRYPTO_memcmp $f; then
++						have_crypto="1"
++						break 1;
++					fi
++				done
++				have_evp="0"
++				files=$(find $s -type f -name evp.h)
++				for f in $files; do
++					if grep -q EVP_MAC_init $f; then
++						have_evp="1"
++						break 1;
++					fi
++				done
++				if [ $have_crypto = "1" ] &&
++				[ $have_evp = "1" ]; then
++					printf " -DHAVE_OPENSSL"
++					break 2
+ 				fi
+ 			done
+-			have_evp="0"
+-			files=$(find $s -type f -name evp.h)
+-			for f in $files; do
+-				if grep -q EVP_MAC_init $f; then
+-					have_evp="1"
+-					break 1;
+-				fi
+-			done
+-			if [ $have_crypto = "1" ] &&
+-			   [ $have_evp = "1" ]; then
+-				printf " -DHAVE_OPENSSL"
+-				break 2
+-			fi
+ 		done
+-	done
++	fi
+ }
+ 
+ #
diff --git a/net/linuxptp/patches/020-gcc14.patch b/net/linuxptp/patches/020-gcc14.patch
deleted file mode 100644
index 13ac07310c..0000000000
--- a/net/linuxptp/patches/020-gcc14.patch
+++ /dev/null
@@ -1,10 +0,0 @@
---- a/interface.c
-+++ b/interface.c
-@@ -5,6 +5,7 @@
-  * @note SPDX-License-Identifier: GPL-2.0+
-  */
- #include <stdlib.h>
-+#include <string.h>
- #include "interface.h"
- 
- struct interface {
-- 
2.30.2