Skip to content

Commit 46fadb8

Browse files
committed
openthread: platform: Correct handling of closing sockets
In this commit, `net_socket_service_register` is called when a platform module that has sockets is deinitialized, and it's socket/sockets are closed. This commit also handles a case when a module tries to join a multicast group and a subscription, from another module, is already present. Signed-off-by: Cristian Bulacu <cristian.bulacu@nxp.com>
1 parent d4b6469 commit 46fadb8

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

modules/openthread/platform/infra_if.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static uint32_t ail_iface_index;
2929
static struct net_icmp_ctx ra_ctx;
3030
static struct net_icmp_ctx rs_ctx;
3131
static struct net_icmp_ctx na_ctx;
32+
static struct in6_addr mcast_addr;
3233

3334
static void infra_if_handle_backbone_icmp6(struct otbr_msg_ctx *msg_ctx_ptr);
3435
static void handle_ra_from_ot(const uint8_t *buffer, uint16_t buffer_length);
@@ -118,15 +119,34 @@ otError otPlatGetInfraIfLinkLayerAddress(otInstance *aInstance, uint32_t aIfInde
118119
otError infra_if_init(otInstance *instance, struct net_if *ail_iface)
119120
{
120121
otError error = OT_ERROR_NONE;
121-
struct in6_addr addr = {0};
122+
int ret;
122123

123124
ot_instance = instance;
124125
ail_iface_ptr = ail_iface;
125126
ail_iface_index = (uint32_t)net_if_get_by_iface(ail_iface_ptr);
126-
net_ipv6_addr_create_ll_allrouters_mcast(&addr);
127-
VerifyOrExit(net_ipv6_mld_join(ail_iface, &addr) == 0, error = OT_ERROR_FAILED);
127+
128+
net_ipv6_addr_create_ll_allrouters_mcast(&mcast_addr);
129+
ret = net_ipv6_mld_join(ail_iface, &mcast_addr);
130+
131+
VerifyOrExit((ret == 0 || ret == -EALREADY), error = OT_ERROR_FAILED);
132+
133+
exit:
134+
return error;
135+
}
136+
137+
otError infra_if_deinit(void)
138+
{
139+
otError error = OT_ERROR_NONE;
140+
141+
ot_instance = NULL;
142+
ail_iface_index = 0;
143+
144+
VerifyOrExit(net_ipv6_mld_leave(ail_iface_ptr, &mcast_addr) == 0,
145+
error = OT_ERROR_FAILED);
128146

129147
exit:
148+
ail_iface_ptr = NULL;
149+
130150
return error;
131151
}
132152

modules/openthread/platform/mdns_socket.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <zephyr/net/socket_service.h>
2121
#include <openthread/nat64.h>
2222
#include "sockets_internal.h"
23+
#include <errno.h>
2324

2425
#define MULTICAST_PORT 5353
2526
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_IPV6)
@@ -108,6 +109,7 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
108109
struct ipv6_mreq mreq_v6 = {0};
109110
int mcast_hops = 255;
110111
int on = 1;
112+
int mcast_join_ret = 0;
111113

112114
struct sockaddr_in6 addr = {.sin6_family = AF_INET6,
113115
.sin6_port = htons(MULTICAST_PORT),
@@ -132,8 +134,9 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
132134
error = OT_ERROR_FAILED);
133135
mreq_v6.ipv6mr_ifindex = ail_iface_idx;
134136
net_ipv6_addr_create(&mreq_v6.ipv6mr_multiaddr, 0xff02, 0, 0, 0, 0, 0, 0, 0x00fb);
135-
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq_v6,
136-
sizeof(mreq_v6)) == 0,
137+
mcast_join_ret = zsock_setsockopt(mdns_sock_v6, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
138+
&mreq_v6, sizeof(mreq_v6));
139+
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
137140
error = OT_ERROR_FAILED);
138141
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
139142
&on, sizeof(on)) == 0,
@@ -170,6 +173,7 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
170173
struct ip_mreqn mreq_v4 = {0};
171174
int mcast_hops = 255;
172175
int on = 1;
176+
int mcast_join_ret = 0;
173177

174178
struct sockaddr_in addr = {.sin_family = AF_INET,
175179
.sin_port = htons(MULTICAST_PORT),
@@ -190,8 +194,9 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
190194
error = OT_ERROR_FAILED);
191195
mreq_v4.imr_ifindex = ail_iface_idx;
192196
mreq_v4.imr_multiaddr.s_addr = htonl(0xE00000FB);
193-
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq_v4,
194-
sizeof(mreq_v4)) == 0,
197+
mcast_join_ret = zsock_setsockopt(mdns_sock_v4, IPPROTO_IP, IP_ADD_MEMBERSHIP,
198+
&mreq_v4, sizeof(mreq_v4));
199+
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
195200
error = OT_ERROR_FAILED);
196201
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, IPPROTO_IP, IP_MULTICAST_LOOP, &on,
197202
sizeof(on)) == 0,
@@ -237,6 +242,10 @@ static otError mdns_socket_deinit(void)
237242
sockfd_udp[1].fd = -1;
238243
mdns_sock_v4 = -1;
239244
#endif /* CONFIG_NET_IPV4 */
245+
246+
VerifyOrExit(net_socket_service_register(&mdns_udp_service, sockfd_udp,
247+
ARRAY_SIZE(sockfd_udp), NULL) == 0,
248+
error = OT_ERROR_FAILED);
240249
exit:
241250
return error;
242251
}

modules/openthread/platform/platform-zephyr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ int notify_new_tx_frame(struct net_pkt *pkt);
120120

121121
#if defined(CONFIG_OPENTHREAD_ZEPHYR_BORDER_ROUTER)
122122
otError infra_if_init(otInstance *instance, struct net_if *ail_iface);
123+
otError infra_if_deinit(void);
123124
otError infra_if_start_icmp6_listener(void);
124125
void infra_if_stop_icmp6_listener(void);
125126
void udp_plat_init_sockfd(void);

modules/openthread/platform/trel.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void otPlatTrelDisable(otInstance *aInstance)
7777
sockfd_udp[0].fd = -1;
7878
trel_sock = -1;
7979

80+
net_socket_service_register(&trel_udp_service, sockfd_udp,
81+
ARRAY_SIZE(sockfd_udp), NULL);
82+
8083
trel_is_enabled = false;
8184

8285
exit:

modules/openthread/platform/udp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ void udp_plat_deinit(void)
6666
sockfd_udp[idx].fd = -1;
6767
}
6868
}
69+
70+
net_socket_service_register(&handle_udp_receive, sockfd_udp,
71+
ARRAY_SIZE(sockfd_udp), NULL);
6972
}
7073

7174
otError otPlatUdpSocket(otUdpSocket *aUdpSocket)

0 commit comments

Comments
 (0)