net: lwm2m: Fix address length at connect

If NET_IPV4 and NET_SOCKETS_PACKET is enabled, NET_SOCKADDR_MAX_SIZE will
be bigger than the ipv4 address length.
This is a problem when DTLS is used as the address comparison will fail
because of the different length of the received and the stored address.
This is also a problem if NET_IPV6 and NET_IPV4 is enabled and the remote
address is a ipv4 address

Signed-off-by: Benjamin Bigler <benjamin.bigler@securiton.ch>
This commit is contained in:
Benjamin Bigler 2022-03-29 14:42:00 +02:00 committed by Carles Cufí
commit 493b940248

View file

@ -5589,6 +5589,7 @@ static int load_tls_credential(struct lwm2m_ctx *client_ctx, uint16_t res_id,
int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
{
socklen_t addr_len;
int flags;
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
int ret;
@ -5661,9 +5662,17 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
}
}
#endif /* CONFIG_LWM2M_DTLS_SUPPORT */
if ((client_ctx->remote_addr).sa_family == AF_INET) {
addr_len = sizeof(struct sockaddr_in);
} else if ((client_ctx->remote_addr).sa_family == AF_INET6) {
addr_len = sizeof(struct sockaddr_in6);
} else {
lwm2m_engine_context_close(client_ctx);
return -EPROTONOSUPPORT;
}
if (connect(client_ctx->sock_fd, &client_ctx->remote_addr,
NET_SOCKADDR_MAX_SIZE) < 0) {
addr_len) < 0) {
LOG_ERR("Cannot connect UDP (-%d)", errno);
lwm2m_engine_context_close(client_ctx);
return -errno;