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 <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2024-02-01 15:31:46 +08:00 committed by Xiang Xiao
parent b343fd2adb
commit 8562dd5796

View File

@ -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