Skip to content

Commit 4ce9f1a

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 ba7f889 commit 4ce9f1a

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

modules/openthread/platform/dhcp6_pd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ static void dhcpv6_pd_client_socket_deinit(uint32_t infra_if_index)
163163

164164
sockfd_udp[0].fd = -1;
165165
dhcpv6_pd_client_sock = -1;
166+
167+
net_socket_service_register(&dhcpv6_pd_client_udp_receive, sockfd_udp,
168+
ARRAY_SIZE(sockfd_udp), NULL);
166169
exit:
167170
return;
168171
}

modules/openthread/platform/infra_if.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static uint32_t ail_iface_index;
3535
static struct net_icmp_ctx ra_ctx;
3636
static struct net_icmp_ctx rs_ctx;
3737
static struct net_icmp_ctx na_ctx;
38+
static struct in6_addr mcast_addr;
3839

3940
static void infra_if_handle_backbone_icmp6(struct otbr_msg_ctx *msg_ctx_ptr);
4041
static void handle_ra_from_ot(const uint8_t *buffer, uint16_t buffer_length);
@@ -133,13 +134,16 @@ otError otPlatGetInfraIfLinkLayerAddress(otInstance *aInstance, uint32_t aIfInde
133134
otError infra_if_init(otInstance *instance, struct net_if *ail_iface)
134135
{
135136
otError error = OT_ERROR_NONE;
136-
struct net_in6_addr addr = {0};
137+
int ret;
137138

138139
ot_instance = instance;
139140
ail_iface_ptr = ail_iface;
140141
ail_iface_index = (uint32_t)net_if_get_by_iface(ail_iface_ptr);
141-
net_ipv6_addr_create_ll_allrouters_mcast(&addr);
142-
VerifyOrExit(net_ipv6_mld_join(ail_iface, &addr) == 0, error = OT_ERROR_FAILED);
142+
143+
net_ipv6_addr_create_ll_allrouters_mcast(&mcast_addr);
144+
ret = net_ipv6_mld_join(ail_iface, &mcast_addr);
145+
146+
VerifyOrExit((ret == 0 || ret == -EALREADY), error = OT_ERROR_FAILED);
143147

144148
for (uint8_t i = 0; i < MAX_SERVICES; i++) {
145149
sockfd_raw[i].fd = -1;
@@ -148,6 +152,22 @@ otError infra_if_init(otInstance *instance, struct net_if *ail_iface)
148152
return error;
149153
}
150154

155+
otError infra_if_deinit(void)
156+
{
157+
otError error = OT_ERROR_NONE;
158+
159+
ot_instance = NULL;
160+
ail_iface_index = 0;
161+
162+
VerifyOrExit(net_ipv6_mld_leave(ail_iface_ptr, &mcast_addr) == 0,
163+
error = OT_ERROR_FAILED);
164+
165+
exit:
166+
ail_iface_ptr = NULL;
167+
168+
return error;
169+
}
170+
151171
static void handle_ra_from_ot(const uint8_t *buffer, uint16_t buffer_length)
152172
{
153173
struct net_if *ot_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));

modules/openthread/platform/mdns_socket.c

Lines changed: 14 additions & 5 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 net_ipv6_mreq mreq_v6 = {0};
109110
int mcast_hops = 255;
110111
int on = 1;
112+
int mcast_join_ret = 0;
111113

112114
struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
113115
.sin6_port = net_htons(MULTICAST_PORT),
@@ -134,8 +136,9 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
134136
error = OT_ERROR_FAILED);
135137
mreq_v6.ipv6mr_ifindex = ail_iface_idx;
136138
net_ipv6_addr_create(&mreq_v6.ipv6mr_multiaddr, 0xff02, 0, 0, 0, 0, 0, 0, 0x00fb);
137-
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, NET_IPPROTO_IPV6, ZSOCK_IPV6_ADD_MEMBERSHIP,
138-
&mreq_v6, sizeof(mreq_v6)) == 0,
139+
mcast_join_ret = zsock_setsockopt(mdns_sock_v6, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
140+
&mreq_v6, sizeof(mreq_v6));
141+
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
139142
error = OT_ERROR_FAILED);
140143
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, NET_IPPROTO_IPV6, ZSOCK_IPV6_MULTICAST_LOOP,
141144
&on, sizeof(on)) == 0,
@@ -172,6 +175,7 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
172175
struct net_ip_mreqn mreq_v4 = {0};
173176
int mcast_hops = 255;
174177
int on = 1;
178+
int mcast_join_ret = 0;
175179

176180
struct net_sockaddr_in addr = {.sin_family = NET_AF_INET,
177181
.sin_port = net_htons(MULTICAST_PORT),
@@ -193,9 +197,10 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
193197
&on, sizeof(on)) == 0,
194198
error = OT_ERROR_FAILED);
195199
mreq_v4.imr_ifindex = ail_iface_idx;
196-
mreq_v4.imr_multiaddr.s_addr = net_htonl(0xE00000FB);
197-
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_IP_ADD_MEMBERSHIP,
198-
&mreq_v4, sizeof(mreq_v4)) == 0,
200+
mreq_v4.imr_multiaddr.s_addr = htonl(0xE00000FB);
201+
mcast_join_ret = zsock_setsockopt(mdns_sock_v4, IPPROTO_IP, IP_ADD_MEMBERSHIP,
202+
&mreq_v4, sizeof(mreq_v4));
203+
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
199204
error = OT_ERROR_FAILED);
200205
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_IP_MULTICAST_LOOP,
201206
&on, sizeof(on)) == 0,
@@ -241,6 +246,10 @@ static otError mdns_socket_deinit(void)
241246
sockfd_udp[1].fd = -1;
242247
mdns_sock_v4 = -1;
243248
#endif /* CONFIG_NET_IPV4 */
249+
250+
VerifyOrExit(net_socket_service_register(&mdns_udp_service, sockfd_udp,
251+
ARRAY_SIZE(sockfd_udp), NULL) == 0,
252+
error = OT_ERROR_FAILED);
244253
exit:
245254
return error;
246255
}

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
@@ -70,6 +70,9 @@ void otPlatTrelDisable(otInstance *aInstance)
7070
sockfd_udp[0].fd = -1;
7171
trel_sock = -1;
7272

73+
net_socket_service_register(&trel_udp_service, sockfd_udp,
74+
ARRAY_SIZE(sockfd_udp), NULL);
75+
7376
trel_is_enabled = false;
7477

7578
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)