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

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

commit 21bab599339afa0b5980987f54d81119c2d056e6
Author: zhangshuai39 <[email protected]>
AuthorDate: Thu Jun 4 10:10:04 2026 +0800

    net: use connection laddr as IPv4 source for TCP and UDP send.
    
    The IPv4 send paths in tcp_sendcommon() and udp_send() built the
    outgoing IP header with dev->d_ipaddr as the source address.  On a
    multi-interface host this is wrong when the connection's bound
    local IP lives on a different interface than the one the packet is
    leaving on: the peer sees a source IP that does not match the
    destination it originally addressed and rejects the segment.
    
    Use conn->u.ipv4.laddr instead, mirroring the existing IPv6 paths
    in the same functions which use netdev_ipv6_srcaddr() with the
    same intent.  TCP can use laddr unconditionally because it is
    always populated (tcp_alloc_accept copies ip->destipaddr; tcp_connect
    fills from conn->dev->d_ipaddr when INADDR_ANY).  UDP keeps a
    fallback to dev->d_ipaddr because INADDR_ANY-bound sockets are
    common.
    
    Single-interface devices and INADDR_ANY-bound UDP sockets are
    unaffected: the new path produces the same value as before.
    
    Signed-off-by: zhangshuai39 <[email protected]>
---
 net/tcp/tcp_send.c | 10 +++++++++-
 net/udp/udp_send.c |  6 +++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/net/tcp/tcp_send.c b/net/tcp/tcp_send.c
index b6e86080275..d8209bb202f 100644
--- a/net/tcp/tcp_send.c
+++ b/net/tcp/tcp_send.c
@@ -261,9 +261,17 @@ static void tcp_sendcommon(FAR struct net_driver_s *dev,
   else
 #endif
     {
+      /* Copy out of the address union: taking the address of the IPv4
+       * member makes GCC 11/12 narrow the whole union to 4 bytes and
+       * warn about the 16-byte IPv6 read in the branch above.
+       */
+
+      in_addr_t laddr = conn->u.ipv4.laddr;
+      in_addr_t raddr = conn->u.ipv4.raddr;
+
       ninfo("do IPv4 IP header build!\n");
       ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_TCP,
-                        &dev->d_ipaddr, &conn->u.ipv4.raddr,
+                        &laddr, &raddr,
                         conn->sconn.s_ttl, conn->sconn.s_tos, NULL);
 
       /* Calculate TCP checksum. */
diff --git a/net/udp/udp_send.c b/net/udp/udp_send.c
index 4eb2575ec80..190c1e491b0 100644
--- a/net/udp/udp_send.c
+++ b/net/udp/udp_send.c
@@ -153,6 +153,7 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct 
udp_conn_s *conn)
   FAR const uint16_t *laddr;
 #endif
 #ifdef CONFIG_NET_IPv4
+  FAR const in_addr_t *src;
   in_addr_t raddr;
 #endif
 
@@ -188,8 +189,11 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct 
udp_conn_s *conn)
 
           dev->d_len        = dev->d_sndlen + IPv4UDP_HDRLEN;
 
+          src = net_ipv4addr_cmp(conn->u.ipv4.laddr, INADDR_ANY) ?
+                &dev->d_ipaddr : &conn->u.ipv4.laddr;
+
           ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_UDP,
-                            &dev->d_ipaddr, &raddr, conn->sconn.s_ttl,
+                            src, &raddr, conn->sconn.s_ttl,
                             conn->sconn.s_tos, NULL);
 
 #ifdef CONFIG_NET_STATISTICS

Reply via email to