This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 32411a54ae271cf8423f67e153db51f9e1c310d0
Author: Daniel P. Carvalho <[email protected]>
AuthorDate: Thu Sep 10 22:32:39 2026 -0300

    netutils/ptpd: fix Delay_Req transmission over AF_PACKET
    
    1. On AF_PACKET SOCK_RAW sockets in NuttX, msg_name must be NULL because
       the destination MAC address is already contained in the Ethernet header.
       Previously, passing sockaddr_in caused sendmsg() to fail immediately
       with -EAFNOSUPPORT, completely blocking transmission of Delay_Req.
    2. Correct PTP primary multicast MAC address to 01:1b:19:00:00:00
       (IEEE 1588 Annex F) and ensure ether_type is in network byte order.
    3. Initialize delayreq_interval to 1 second default and guard against 0.
    4. Set logmessageinterval to 0x7f (IEEE 1588-2008 Table 23 sentinel for
       Delay_Req) instead of inheriting 0 from the announce header template.
    5. Update PTP version to 0x12 (2.1, minorVersionPTP=1) to match the
       value used by mature implementations such as linuxptp.
    
    Assisted-by: Claude:claude-sonnet-5
    Assisted-by: Gemini:gemini-3.8-flash-medium
    Signed-off-by: Daniel P. Carvalho <[email protected]>
---
 netutils/ptpd/ptpd.c  | 23 ++++++++++++++++-------
 netutils/ptpd/ptpv2.h |  9 +++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/netutils/ptpd/ptpd.c b/netutils/ptpd/ptpd.c
index 2e363bf24..bf0fe9189 100644
--- a/netutils/ptpd/ptpd.c
+++ b/netutils/ptpd/ptpd.c
@@ -666,7 +666,7 @@ static int ptp_initialize_state(FAR struct ptp_state_s 
*state)
       goto errout;
     }
 
-  state->own_identity.header.version = 2;
+  state->own_identity.header.version = PTP_VERSION_2_1;
   state->own_identity.header.domain = CONFIG_NETUTILS_PTPD_DOMAIN;
   state->own_identity.header.sourceidentity[0] = req.ifr_hwaddr.sa_data[0];
   state->own_identity.header.sourceidentity[1] = req.ifr_hwaddr.sa_data[1];
@@ -689,6 +689,7 @@ static int ptp_initialize_state(FAR struct ptp_state_s 
*state)
          sizeof(state->own_identity.gm_identity));
   state->own_identity.timesource = CONFIG_NETUTILS_PTPD_CLOCKSOURCE;
 
+  state->delayreq_interval = 1;
   clock_gettime(CLOCK_MONOTONIC, &state->last_received_multicast);
 
   return OK;
@@ -745,11 +746,11 @@ static int ptp_sendmsg(FAR struct ptp_state_s *state, FAR 
const void *buf,
 
   if (state->config->af == AF_PACKET)
     {
-      /* IEE802.1AS Multicast address for gptp */
+      /* IEEE 1588-2008 Annex F primary multicast MAC address */
 
       const uint8_t ptp_multicast_mac[ETHER_ADDR_LEN] =
       {
-        0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e
+        0x01, 0x1b, 0x19, 0x00, 0x00, 0x00
       };
 
       char raw[sizeof(struct ether_header) + sizeof(struct ptp_announce_s)];
@@ -762,15 +763,19 @@ static int ptp_sendmsg(FAR struct ptp_state_s *state, FAR 
const void *buf,
       header = (FAR struct ether_header *)&raw;
       memcpy(header->ether_dhost, ptp_multicast_mac, ETHER_ADDR_LEN);
       netlib_getmacaddr(state->config->interface, header->ether_shost);
-      header->ether_type = ETHERTYPE_PTP;
+      header->ether_type = htons(ETHERTYPE_PTP);
       memcpy(&raw[sizeof(*header)], buf, buflen);
       buflen += sizeof(*header);
 
       iov.iov_base = raw;
       iov.iov_len = buflen;
 
-      msg.msg_name = (FAR void *)addr;
-      msg.msg_namelen = addrlen;
+      /* For AF_PACKET SOCK_RAW, msg_name must be NULL as destination
+       * is specified in the Ethernet frame header.
+       */
+
+      msg.msg_name = NULL;
+      msg.msg_namelen = 0;
       msg.msg_iov = &iov;
       msg.msg_iovlen = 1;
       msg.msg_flags = 0;
@@ -920,6 +925,7 @@ static int ptp_send_delay_req(FAR struct ptp_state_s *state)
   req.header = state->own_identity.header;
   req.header.messagetype = PTP_MSGTYPE_DELAY_REQ;
   req.header.messagelength[1] = sizeof(req);
+  req.header.logmessageinterval = PTP_LOG_INTERVAL_DELAY_REQ;
   ptp_increment_sequence(&state->delay_req_seq, &req.header);
 
   ptp_gettime(state, &state->delayreq_time);
@@ -983,7 +989,10 @@ static int ptp_periodic_send(FAR struct ptp_state_s *state)
       clock_timespec_subtract(&time_now,
                               &state->last_transmitted_delayreq, &delta);
 
-      if (timespec_to_ms(&delta) > state->delayreq_interval * MSEC_PER_SEC)
+      long interval_s = (state->delayreq_interval > 0) ?
+                        state->delayreq_interval : 1;
+
+      if (timespec_to_ms(&delta) >= interval_s * MSEC_PER_SEC)
         {
           ptp_send_delay_req(state);
         }
diff --git a/netutils/ptpd/ptpv2.h b/netutils/ptpd/ptpv2.h
index d650ecadc..ebedfa397 100644
--- a/netutils/ptpd/ptpv2.h
+++ b/netutils/ptpd/ptpv2.h
@@ -65,6 +65,15 @@
 
 #define PTP_FLAGS0_TWOSTEP        (1 << 1)
 
+/* Special logMessageInterval values (IEEE 1588-2008 Table 23) */
+
+#define PTP_LOG_INTERVAL_DELAY_REQ 0x7f
+
+/* PTP versions (IEEE 1588-2008 / IEEE 1588-2019 Table 18) */
+
+#define PTP_VERSION_2_0           0x02
+#define PTP_VERSION_2_1           0x12
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/

Reply via email to