diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c index d7a80706764..6251cadcfa4 100644 --- a/net/tcp/tcp_conn.c +++ b/net/tcp/tcp_conn.c @@ -155,8 +155,8 @@ static inline FAR struct tcp_conn_s *tcp_ipv4_listener(in_addr_t ipaddr, ****************************************************************************/ #if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDEV_MULTINIC) -static inline FAR struct tcp_conn_s *tcp_ipv6_ listener(net_ipv6addr_t ipaddr, - uint16_t portno) +static inline FAR struct tcp_conn_s * +tcp_ipv6_listener(const net_ipv6addr_t ipaddr, uint16_t portno) { FAR struct tcp_conn_s *conn; int i; @@ -436,7 +436,7 @@ static inline FAR struct tcp_conn_s * FAR struct tcp_conn_s *conn; net_ipv6addr_t *srcipaddr; #ifdef CONFIG_NETDEV_MULTINIC - net_ipv6addr_t destipaddr; + net_ipv6addr_t *destipaddr; #endif conn = (FAR struct tcp_conn_s *)g_active_tcp_connections.head; @@ -575,10 +575,16 @@ static inline int tcp_ipv6_bind(FAR struct tcp_conn_s *conn, /* Verify or select a local port */ #ifdef CONFIG_NETDEV_MULTINIC + /* The port number must be unique for this address binding */ + port = tcp_selectport(PF_INET6, - (FAR const union ip_addr_u ipaddr *)addr->sin6_addr.in6_u.u6_addr16, + (FAR const union ip_addr_u *)addr->sin6_addr.in6_u.u6_addr16, ntohs(addr->sin6_port)); #else + /* There is only one network device; the port number can be globally + * unique. + */ + port = tcp_selectport(ntohs(addr->sin6_port)); #endif @@ -1099,19 +1105,56 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const struct sockaddr *addr) */ flags = net_lock(); + #ifdef CONFIG_NETDEV_MULTINIC -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - port = tcp_selectport(conn->domain, &conn->u, ntohs(conn->lport)); -#elif defined(CONFIG_NET_IPv4) - port = tcp_selectport(PF_INET, &conn->u, ntohs(conn->lport)); -#else - port = tcp_selectport(PF_INET6, &conn->u, ntohs(conn->lport)); + /* If there are multiple network devices, then we need to pass the local, + * bound address. This is needed because port unique-ness is only for a + * given network. + * + * This is complicated by the fact that the local address may be either an + * IPv4 or an IPv6 address. + */ + +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + if (conn->domain == PF_INET) #endif -#else + { + /* Select a port that is unique for this IPv4 local address */ + + port = tcp_selectport(PF_INET, + (FAR const union ip_addr_u *)&conn->u.ipv4.laddr, + ntohs(conn->lport)); + } +#endif /* CONFIG_NET_IPv4 */ + +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + else +#endif + { + /* Select a port that is unique for this IPv6 local address */ + + port = tcp_selectport(PF_INET6, + (FAR const union ip_addr_u *)conn->u.ipv6.laddr, + ntohs(conn->lport)); + } +#endif /* CONFIG_NET_IPv6 */ + +#else /* CONFIG_NETDEV_MULTINIC */ + /* Select the next available port number. This is only one network device + * so we do not have to bother with all of the IPv4/IPv6 local address + * silliness. + */ + port = tcp_selectport(ntohs(conn->lport)); -#endif + +#endif /* CONFIG_NETDEV_MULTINIC */ + net_unlock(flags); + /* Did we have a port assignment? */ + if (port < 0) { return port; diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c index 2592aa7dcab..af28a723594 100644 --- a/net/udp/udp_conn.c +++ b/net/udp/udp_conn.c @@ -619,7 +619,7 @@ int udp_bind(FAR struct udp_conn_s *conn, FAR const struct sockaddr *addr) * interfaces for receiving (Sending will use the default port). */ - net_ipv6addr_copy(conn->u.ipv6.laddr, ipaddr->sin6_addr.in6_u.u6_addr16); + net_ipv6addr_copy(conn->u.ipv6.laddr, inaddr->sin6_addr.in6_u.u6_addr16); #endif /* CONFIG_NETDEV_MULTINIC */ } #endif /* CONFIG_NET_IPv6 */