Skip to content

Commit e27f8cc

Browse files
rluboskartben
authored andcommitted
net: context: Fix IPv4-to-IPv6 mapped sendto()
Commit 48897a9 fixed the address mapping for dual-stack sockets when sendmsg() was used, however the same similar issue appears (cannot check for AF_INET family for mapped addresses as those are AF_INET6) for regular sendto() calls. Therefore, for the sendto() case, where dst_addr is specified, check if the address is mapped with net_ipv6_addr_is_v4_mapped() function as well. This also fixes another bug in sendmsg() case, where msghdr->msg_name could've been dereferenced even if it was NULL (for example in case of a connected socket). Signed-off-by: Robert Lubos <[email protected]>
1 parent 91c8c07 commit e27f8cc

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

subsys/net/ip/net_context.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,15 +2415,16 @@ static int context_sendto(struct net_context *context,
24152415
*/
24162416
if (IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6) &&
24172417
IS_ENABLED(CONFIG_NET_IPV6) &&
2418-
net_context_get_family(context) == AF_INET6 &&
2419-
dst_addr != NULL &&
2420-
dst_addr->sa_family == AF_INET) {
2421-
family = AF_INET;
2422-
} else if (IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6) &&
2423-
IS_ENABLED(CONFIG_NET_IPV6) && msghdr != NULL) {
2424-
const struct sockaddr_in6 *addr6 = msghdr->msg_name;
2425-
2426-
if (net_ipv6_addr_is_v4_mapped(&addr6->sin6_addr)) {
2418+
net_context_get_family(context) == AF_INET6) {
2419+
const struct sockaddr_in6 *addr6 = NULL;
2420+
2421+
if (dst_addr != NULL) {
2422+
addr6 = (const struct sockaddr_in6 *)dst_addr;
2423+
} else if (msghdr != NULL) {
2424+
addr6 = msghdr->msg_name;
2425+
}
2426+
2427+
if (addr6 != NULL && net_ipv6_addr_is_v4_mapped(&addr6->sin6_addr)) {
24272428
family = AF_INET;
24282429
} else {
24292430
family = net_context_get_family(context);

0 commit comments

Comments
 (0)