net: context: Select proper network interface when binding

Use the destination address to select the proper network interface
when binding. The default network interface cannot be used here
as then the packet might be sent to wrong network interface.

Fixes #9935

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2018-09-13 15:06:35 +03:00
parent 9fe378b7e7
commit fc27a81ed2

View File

@ -432,7 +432,8 @@ int net_context_bind(struct net_context *context, const struct sockaddr *addr,
ptr = &maddr->address.in6_addr;
} else if (net_is_ipv6_addr_unspecified(&addr6->sin6_addr)) {
iface = net_if_get_default();
iface = net_if_ipv6_select_src_iface(
&net_sin6(&context->remote)->sin6_addr);
ptr = (struct in6_addr *)net_ipv6_unspecified_address();
} else {
@ -519,7 +520,8 @@ int net_context_bind(struct net_context *context, const struct sockaddr *addr,
ptr = &maddr->address.in_addr;
} else if (addr4->sin_addr.s_addr == INADDR_ANY) {
iface = net_if_get_default();
iface = net_if_ipv4_select_src_iface(
&net_sin(&context->remote)->sin_addr);
ptr = (struct in_addr *)net_ipv4_unspecified_address();
} else {
@ -695,11 +697,6 @@ int net_context_connect(struct net_context *context,
return -EBADF;
}
ret = bind_default(context);
if (ret) {
return ret;
}
if (addr->sa_family != net_context_get_family(context)) {
NET_ASSERT_INFO(addr->sa_family == \
net_context_get_family(context),
@ -754,6 +751,17 @@ int net_context_connect(struct net_context *context,
rport = addr6->sin6_port;
/* The binding must be done after we have set the remote
* address but before checking the local address. Otherwise
* the laddr might not be set properly which would then cause
* issues when doing net_tcp_connect(). This issue was seen
* with socket tests and when connecting to loopback interface.
*/
ret = bind_default(context);
if (ret) {
return ret;
}
net_sin6_ptr(&context->local)->sin6_family = AF_INET6;
net_sin6(&local_addr)->sin6_family = AF_INET6;
net_sin6(&local_addr)->sin6_port = lport =
@ -795,6 +803,11 @@ int net_context_connect(struct net_context *context,
rport = addr4->sin_port;
ret = bind_default(context);
if (ret) {
return ret;
}
net_sin_ptr(&context->local)->sin_family = AF_INET;
net_sin(&local_addr)->sin_family = AF_INET;
net_sin(&local_addr)->sin_port = lport =