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

commit 61d6179db9a639deb4d296f5ea8a69477ac831d3
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 19 07:10:59 2026 +0100

    net/pkt: Accept a destination address on a raw packet socket.
    
    sendto() and sendmsg() with a struct sockaddr_ll on a SOCK_RAW packet
    socket failed with EAFNOSUPPORT.  An application must give the address
    when the socket is not bound, and many applications give it also when
    the socket is bound.  For a raw socket the frame holds its link layer
    header, so the address only selects the network device.
    
    pkt_sendmsg_is_valid() now accepts an address on a raw socket.  The
    address length must be at least the size of struct sockaddr_ll (else
    EINVAL), and sll_ifindex selects the network device (ENODEV if there is
    no such device).  As for a SOCK_DGRAM socket, pkt_sendmsg() then sets
    the interface index of the connection, so that devif_poll() of that
    device polls the connection.  A send without an address does not
    change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/pkt/pkt_conn.c               | 20 ++++++++++++++++----
 net/pkt/pkt_sendmsg_buffered.c   |  2 +-
 net/pkt/pkt_sendmsg_unbuffered.c |  2 +-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c
index 3d97e155b32..30e5472b67d 100644
--- a/net/pkt/pkt_conn.c
+++ b/net/pkt/pkt_conn.c
@@ -330,14 +330,26 @@ int pkt_sendmsg_is_valid(FAR struct socket *psock,
     }
   else if (psock->s_type == SOCK_RAW)
     {
+      /* The frame of a raw socket holds its link layer header.  An
+       * address is optional: if it is given, its interface index selects
+       * the network device.
+       */
+
       if (msg->msg_name != NULL)
         {
-          return -EAFNOSUPPORT;
-        }
+          if (msg->msg_namelen < sizeof(struct sockaddr_ll))
+            {
+              return -EINVAL;
+            }
 
-      /* Get the device driver that will service this transfer */
+          *dev = netdev_findbyindex(addr->sll_ifindex);
+        }
+      else
+        {
+          /* Get the device driver that will service this transfer */
 
-      *dev = pkt_find_device(psock->s_conn);
+          *dev = pkt_find_device(psock->s_conn);
+        }
     }
   else
     {
diff --git a/net/pkt/pkt_sendmsg_buffered.c b/net/pkt/pkt_sendmsg_buffered.c
index c6ce76a4980..c262058e62b 100644
--- a/net/pkt/pkt_sendmsg_buffered.c
+++ b/net/pkt/pkt_sendmsg_buffered.c
@@ -235,7 +235,7 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
     }
 
   conn = psock->s_conn;
-  if (psock->s_type == SOCK_DGRAM)
+  if (addr != NULL)
     {
       /* Set the interface index for devif_poll can match the conn */
 
diff --git a/net/pkt/pkt_sendmsg_unbuffered.c b/net/pkt/pkt_sendmsg_unbuffered.c
index 06d9e365b6a..0af0a75242e 100644
--- a/net/pkt/pkt_sendmsg_unbuffered.c
+++ b/net/pkt/pkt_sendmsg_unbuffered.c
@@ -213,7 +213,7 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
   addr = msg->msg_name;
   conn = psock->s_conn;
 
-  if (psock->s_type == SOCK_DGRAM)
+  if (addr != NULL)
     {
       /* Set the interface index for devif_poll can match the conn */
 

Reply via email to