Skip to content

Commit 924e433

Browse files
committed
net: context: Set target network interface in send if needed
If we are sending a network packet and if the remote address is not set in the context (which means that connect() has not been called), then we must set the target network interface to a proper value. This is done so that when we select the local source address, we might select the wrong interface if we have multiple network interfaces in the system. In this case the packet would be always assigned to first network interface regardless of the destination address. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent bd97359 commit 924e433

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

subsys/net/ip/net_context.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,20 @@ static int context_sendto(struct net_context *context,
14621462
if (net_ipv6_is_addr_unspecified(&addr6->sin6_addr)) {
14631463
return -EDESTADDRREQ;
14641464
}
1465+
1466+
/* If application has not yet set the destination address
1467+
* i.e., by not calling connect(), then set the interface
1468+
* here so that the packet gets sent to the correct network
1469+
* interface. This issue can be seen if there are multiple
1470+
* network interfaces and we are trying to send data to
1471+
* second or later network interface.
1472+
*/
1473+
if (addr6 && net_ipv6_is_addr_unspecified(
1474+
&net_sin6(&context->remote)->sin6_addr)) {
1475+
iface = net_if_ipv6_select_src_iface(&addr6->sin6_addr);
1476+
net_context_set_iface(context, iface);
1477+
}
1478+
14651479
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
14661480
net_context_get_family(context) == AF_INET) {
14671481
const struct sockaddr_in *addr4 =
@@ -1488,6 +1502,19 @@ static int context_sendto(struct net_context *context,
14881502
if (!addr4->sin_addr.s_addr) {
14891503
return -EDESTADDRREQ;
14901504
}
1505+
1506+
/* If application has not yet set the destination address
1507+
* i.e., by not calling connect(), then set the interface
1508+
* here so that the packet gets sent to the correct network
1509+
* interface. This issue can be seen if there are multiple
1510+
* network interfaces and we are trying to send data to
1511+
* second or later network interface.
1512+
*/
1513+
if (addr4 && net_sin(&context->remote)->sin_addr.s_addr == 0U) {
1514+
iface = net_if_ipv4_select_src_iface(&addr4->sin_addr);
1515+
net_context_set_iface(context, iface);
1516+
}
1517+
14911518
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
14921519
net_context_get_family(context) == AF_PACKET) {
14931520
struct sockaddr_ll *ll_addr = (struct sockaddr_ll *)dst_addr;

0 commit comments

Comments
 (0)