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.git


The following commit(s) were added to refs/heads/master by this push:
     new 03cd1dee7e0 net/netlink: Answer a request with NLM_F_ACK.
03cd1dee7e0 is described below

commit 03cd1dee7e072cf7fb0b9b886e49fa7363a6648c
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 14:33:02 2026 +0100

    net/netlink: Answer a request with NLM_F_ACK.
    
    A netlink request with the NLM_F_ACK flag asks for an answer: Linux
    sends an NLMSG_ERROR message with error 0 (an ACK) when the request
    succeeds.  NuttX sent nothing, so a program that waits for the ACK of a
    request, for example after RTM_NEWADDR, waited for ever.
    
    netlink_sendmsg() now adds the ACK to the responses of the socket when
    the request succeeded and has NLM_F_ACK.  As on Linux, the ACK holds
    the header of the request (NLM_F_CAPPED), its sequence number, and the
    port ID of the socket.  A dump (NLM_F_DUMP) ends with NLMSG_DONE and
    gets no ACK, as on Linux.
    
    A request that fails still returns its error from sendto() and gets no
    message, as before.  Linux instead returns success and sends the error
    in the NLMSG_ERROR message; programs that check the result of sendto()
    see the error in both cases.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/netlink/netlink.h        | 21 ++++++++++++++++++++
 net/netlink/netlink_conn.c   | 47 ++++++++++++++++++++++++++++++++++++++++++++
 net/netlink/netlink_sockif.c | 17 ++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/net/netlink/netlink.h b/net/netlink/netlink.h
index 68a68ad1e66..538c92571ef 100644
--- a/net/netlink/netlink.h
+++ b/net/netlink/netlink.h
@@ -413,6 +413,27 @@ void netlink_notifier_signal(FAR struct netlink_conn_s 
*conn);
 int netlink_add_terminator(NETLINK_HANDLE handle,
                            FAR const struct nlmsghdr *req, int group);
 
+/****************************************************************************
+ * Name: netlink_add_ack
+ *
+ * Description:
+ *   Add one NLMSG_ERROR response with error 0 (an ACK) for a request.
+ *
+ * Input Parameters:
+ *   handle - The handle previously provided to the sendto() implementation
+ *            for the protocol.  This is an opaque reference to the Netlink
+ *            socket state structure.
+ *   req    - The request message header.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned if the ACK was successfully added to the
+ *   response list.
+ *   A negated error value is returned if an unexpected error occurred.
+ *
+ ****************************************************************************/
+
+int netlink_add_ack(NETLINK_HANDLE handle, FAR const struct nlmsghdr *req);
+
 /****************************************************************************
  * Name: netlink_tryget_response
  *
diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c
index addcce09179..eb2b87ef7f0 100644
--- a/net/netlink/netlink_conn.c
+++ b/net/netlink/netlink_conn.c
@@ -282,6 +282,53 @@ void netlink_add_response(NETLINK_HANDLE handle,
   netlink_unlock();
 }
 
+/****************************************************************************
+ * Name: netlink_add_ack
+ *
+ * Description:
+ *   Add one NLMSG_ERROR response with error 0 (an ACK) for a request.
+ *
+ * Input Parameters:
+ *   handle - The handle previously provided to the sendto() implementation
+ *            for the protocol.  This is an opaque reference to the Netlink
+ *            socket state structure.
+ *   req    - The request message header.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned if the ACK was successfully added to the
+ *   response list.
+ *   A negated error value is returned if an unexpected error occurred.
+ *
+ ****************************************************************************/
+
+int netlink_add_ack(NETLINK_HANDLE handle, FAR const struct nlmsghdr *req)
+{
+  FAR struct netlink_conn_s *conn = handle;
+  FAR struct netlink_response_s *resp;
+  FAR struct nlmsgerr *err;
+
+  resp = kmm_zalloc(SIZEOF_NETLINK_RESPONSE_S(sizeof(struct nlmsgerr)));
+  if (resp == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  /* As Linux, the ACK holds only the header of the request */
+
+  resp->msg.nlmsg_len   = NLMSG_LENGTH(sizeof(struct nlmsgerr));
+  resp->msg.nlmsg_type  = NLMSG_ERROR;
+  resp->msg.nlmsg_flags = NLM_F_CAPPED;
+  resp->msg.nlmsg_seq   = req->nlmsg_seq;
+  resp->msg.nlmsg_pid   = conn->pid;
+
+  err        = NLMSG_DATA(&resp->msg);
+  err->error = 0;
+  memcpy(&err->msg, req, sizeof(struct nlmsghdr));
+
+  netlink_add_response(handle, resp);
+  return OK;
+}
+
 /****************************************************************************
  * Name: netlink_add_terminator
  *
diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c
index 2894c439049..fc5aee8e2b0 100644
--- a/net/netlink/netlink_sockif.c
+++ b/net/netlink/netlink_sockif.c
@@ -679,6 +679,23 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock,
         break;
     }
 
+  /* As Linux, answer a request with NLM_F_ACK with an ACK.  A dump ends
+   * with NLMSG_DONE instead.  A request that fails still returns its error
+   * from sendto() and gets no ACK.
+   */
+
+  if (ret >= 0 && (nlmsg->nlmsg_flags & NLM_F_ACK) != 0 &&
+      (nlmsg->nlmsg_flags & NLM_F_DUMP) == 0)
+    {
+      int err;
+
+      err = netlink_add_ack(conn, nlmsg);
+      if (err < 0)
+        {
+          ret = err;
+        }
+    }
+
   return ret;
 }
 

Reply via email to