From 8562dd5796e2d30d117f54197af4042dd0c4bebd Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Thu, 1 Feb 2024 15:31:46 +0800 Subject: [PATCH] net/udp: Fix source address selection under IPv6 https://github.com/apache/nuttx/pull/11384 has changed the source address selection of UDP from raddr to laddr, but not all UDP connections have laddr set, then we need to fallback to get source from raddr. Signed-off-by: Zhe Weng --- net/udp/udp_send.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/net/udp/udp_send.c b/net/udp/udp_send.c index 2e3180af949..dc5bc8c0774 100644 --- a/net/udp/udp_send.c +++ b/net/udp/udp_send.c @@ -86,6 +86,9 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn) { FAR struct udp_hdr_s *udp; +#ifdef CONFIG_NET_IPv6 + FAR const uint16_t *laddr; +#endif #ifdef CONFIG_NET_IPv4 in_addr_t raddr; #endif @@ -148,9 +151,16 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn) dev->d_len = dev->d_sndlen + UDP_HDRLEN; + /* We use the laddr if the conn is bounded to an address, otherwise + * find a suitable source address corresponding to the raddr + */ + + laddr = !net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr) ? + conn->u.ipv6.laddr : + netdev_ipv6_srcaddr(dev, conn->u.ipv6.raddr); + ipv6_build_header(IPv6BUF, dev->d_len, IP_PROTO_UDP, - netdev_ipv6_srcaddr(dev, conn->u.ipv6.laddr), - conn->u.ipv6.raddr, + laddr, conn->u.ipv6.raddr, conn->sconn.ttl, conn->sconn.s_tclass); /* The total length to send is the size of the application data