Skip to content

Commit 7df232c

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. Also, covered network namespace changes done in #99169 Signed-off-by: Cristian Bulacu <cristian.bulacu@nxp.com>
1 parent 20ecf72 commit 7df232c

File tree

6 files changed

+87
-48
lines changed

6 files changed

+87
-48
lines changed

modules/openthread/platform/dhcp6_pd.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ void otPlatInfraIfDhcp6PdClientSend(otInstance *aInstance,
6363
VerifyOrExit(dhcpv6_pd_client_sock != -1 && aInfraIfIndex == ail_iface_idx);
6464
uint16_t length = otMessageGetLength(aMessage);
6565
struct net_sockaddr_in6 dest_sock_addr = {.sin6_family = NET_AF_INET6,
66-
.sin6_port = net_htons(DHCPV6_SERVER_PORT),
67-
.sin6_addr = NET_IN6ADDR_ANY_INIT,
68-
.sin6_scope_id = 0};
66+
.sin6_port = net_htons(DHCPV6_SERVER_PORT),
67+
.sin6_addr = NET_IN6ADDR_ANY_INIT,
68+
.sin6_scope_id = 0};
6969

7070
memcpy(&dest_sock_addr.sin6_addr, aDestAddress, sizeof(otIp6Address));
7171

@@ -116,9 +116,9 @@ static void dhcpv6_pd_client_socket_init(uint32_t infra_if_index)
116116
int hop_limit = 255;
117117
int on = 1;
118118
struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
119-
.sin6_port = net_htons(DHCPV6_CLIENT_PORT),
120-
.sin6_addr = NET_IN6ADDR_ANY_INIT,
121-
.sin6_scope_id = 0};
119+
.sin6_port = net_htons(DHCPV6_CLIENT_PORT),
120+
.sin6_addr = NET_IN6ADDR_ANY_INIT,
121+
.sin6_scope_id = 0};
122122

123123
dhcpv6_pd_client_sock = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
124124
VerifyOrExit(dhcpv6_pd_client_sock >= 0);
@@ -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: 30 additions & 10 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 net_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));
@@ -280,14 +300,14 @@ void infra_if_stop_icmp6_listener(void)
280300
otError infra_if_nat64_init(void)
281301
{
282302
otError error = OT_ERROR_NONE;
283-
struct sockaddr_in anyaddr = {.sin_family = AF_INET,
284-
.sin_port = 0,
285-
.sin_addr = INADDR_ANY_INIT};
303+
struct net_sockaddr_in anyaddr = {.sin_family = NET_AF_INET,
304+
.sin_port = 0,
305+
.sin_addr = NET_INADDR_ANY_INIT};
286306

287-
raw_infra_if_sock = zsock_socket(AF_INET, SOCK_RAW, IPPROTO_IP);
307+
raw_infra_if_sock = zsock_socket(NET_AF_INET, NET_SOCK_RAW, NET_IPPROTO_IP);
288308
VerifyOrExit(raw_infra_if_sock >= 0, error = OT_ERROR_FAILED);
289-
VerifyOrExit(zsock_bind(raw_infra_if_sock, (struct sockaddr *)&anyaddr,
290-
sizeof(struct sockaddr_in)) == 0,
309+
VerifyOrExit(zsock_bind(raw_infra_if_sock, (struct net_sockaddr *)&anyaddr,
310+
sizeof(struct net_sockaddr_in)) == 0,
291311
error = OT_ERROR_FAILED);
292312

293313
sockfd_raw[0].fd = raw_infra_if_sock;
@@ -315,7 +335,7 @@ static void raw_receive_handler(struct net_socket_service_event *evt)
315335
len = zsock_recv(raw_infra_if_sock, req->buffer, sizeof(req->buffer), 0);
316336
VerifyOrExit(len >= 0, error = OT_ERROR_FAILED);
317337

318-
ot_pkt = net_pkt_alloc_with_buffer(ail_iface_ptr, len, AF_INET, 0, K_NO_WAIT);
338+
ot_pkt = net_pkt_alloc_with_buffer(ail_iface_ptr, len, NET_AF_INET, 0, K_NO_WAIT);
319339
VerifyOrExit(ot_pkt != NULL, error = OT_ERROR_FAILED);
320340

321341
VerifyOrExit(net_pkt_write(ot_pkt, req->buffer, len) == 0, error = OT_ERROR_FAILED);

modules/openthread/platform/mdns_socket.c

Lines changed: 32 additions & 23 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,13 +109,14 @@ 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,
113-
.sin6_port = net_htons(MULTICAST_PORT),
114-
.sin6_addr = {{{0}}},
115-
.sin6_scope_id = 0};
115+
.sin6_port = net_htons(MULTICAST_PORT),
116+
.sin6_addr = {{{0}}},
117+
.sin6_scope_id = 0};
116118

117-
mdns_sock_v6 = zsock_socket(NET_AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
119+
mdns_sock_v6 = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
118120
VerifyOrExit(mdns_sock_v6 >= 0, error = OT_ERROR_FAILED);
119121
VerifyOrExit(net_if_get_name(net_if_get_by_index(ail_iface_idx), name,
120122
CONFIG_NET_INTERFACE_NAME_LEN) > 0,
@@ -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, NET_IPPROTO_IPV6, ZSOCK_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,10 +175,11 @@ 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,
177-
.sin_port = net_htons(MULTICAST_PORT),
178-
.sin_addr = {{{0}}}};
181+
.sin_port = net_htons(MULTICAST_PORT),
182+
.sin_addr = {{{0}}}};
179183

180184
mdns_sock_v4 = zsock_socket(NET_AF_INET, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
181185
VerifyOrExit(mdns_sock_v4 >= 0, error = OT_ERROR_FAILED);
@@ -194,8 +198,9 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
194198
error = OT_ERROR_FAILED);
195199
mreq_v4.imr_ifindex = ail_iface_idx;
196200
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,
201+
mcast_join_ret = zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_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
}
@@ -250,13 +259,13 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)
250259
uint16_t length = otMessageGetLength(message);
251260
struct otbr_msg_ctx *req = NULL;
252261
struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6,
253-
.sin6_port = net_htons(MULTICAST_PORT),
254-
.sin6_addr = NET_IN6ADDR_ANY_INIT,
255-
.sin6_scope_id = 0};
262+
.sin6_port = net_htons(MULTICAST_PORT),
263+
.sin6_addr = NET_IN6ADDR_ANY_INIT,
264+
.sin6_scope_id = 0};
256265
#if defined(CONFIG_NET_IPV4)
257266
struct net_sockaddr_in addr_v4 = {.sin_family = NET_AF_INET,
258-
.sin_port = net_htons(MULTICAST_PORT),
259-
.sin_addr = NET_INADDR_ANY_INIT};
267+
.sin_port = net_htons(MULTICAST_PORT),
268+
.sin_addr = NET_INADDR_ANY_INIT};
260269
#endif /* CONFIG_NET_IPV4*/
261270

262271
VerifyOrExit(length <= OTBR_MESSAGE_SIZE);
@@ -271,11 +280,11 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)
271280

272281
VerifyOrExit(zsock_sendto(mdns_sock_v6, req->buffer, length, 0,
273282
(struct net_sockaddr *)&addr_v6,
274-
sizeof(addr_v6)) > 0);
283+
sizeof(addr_v6)) > 0);
275284
#if defined(CONFIG_NET_IPV4)
276285
VerifyOrExit(zsock_sendto(mdns_sock_v4, req->buffer, length, 0,
277286
(struct net_sockaddr *)&addr_v4,
278-
sizeof(addr_v4)) > 0);
287+
sizeof(addr_v4)) > 0);
279288
#endif /* CONFIG_NET_IPV4 */
280289
exit:
281290
otMessageFree(message);
@@ -294,9 +303,9 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a
294303
struct net_sockaddr_in addr_v4 = {0};
295304
#endif /* CONFIG_NET_IPV4*/
296305
struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6,
297-
.sin6_port = net_htons(aAddress->mPort),
298-
.sin6_addr = NET_IN6ADDR_ANY_INIT,
299-
.sin6_scope_id = 0};
306+
.sin6_port = net_htons(aAddress->mPort),
307+
.sin6_addr = NET_IN6ADDR_ANY_INIT,
308+
.sin6_scope_id = 0};
300309

301310
VerifyOrExit(length <= OTBR_MESSAGE_SIZE);
302311
VerifyOrExit(openthread_border_router_allocate_message((void **)&req) == OT_ERROR_NONE);
@@ -314,7 +323,7 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a
314323

315324
VerifyOrExit(zsock_sendto(mdns_sock_v6, req->buffer, length, 0,
316325
(struct net_sockaddr *)&addr_v6,
317-
sizeof(addr_v6)) > 0);
326+
sizeof(addr_v6)) > 0);
318327
#if defined(CONFIG_NET_IPV4)
319328
if (send_ipv4) {
320329
VerifyOrExit(zsock_sendto(mdns_sock_v4, req->buffer, length, 0,
@@ -349,7 +358,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
349358
if (family == NET_AF_INET) {
350359
addrlen = sizeof(addr_v4);
351360
len = zsock_recvfrom(mdns_sock_v4, req->buffer, sizeof(req->buffer), 0,
352-
(struct net_sockaddr *)&addr_v4, &addrlen);
361+
(struct net_sockaddr *)&addr_v4, &addrlen);
353362
VerifyOrExit(len > 0);
354363
otIp4ToIp4MappedIp6Address((otIp4Address *)&addr_v4.sin_addr.s_addr,
355364
&req->addr_info.mAddress);
@@ -358,7 +367,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
358367
#endif /* CONFIG_NET_IPV4 */
359368
addrlen = sizeof(addr_v6);
360369
len = zsock_recvfrom(mdns_sock_v6, req->buffer, sizeof(req->buffer), 0,
361-
(struct net_sockaddr *)&addr_v6, &addrlen);
370+
(struct net_sockaddr *)&addr_v6, &addrlen);
362371
VerifyOrExit(len > 0);
363372
memcpy(&req->addr_info.mAddress, &addr_v6.sin6_addr,
364373
sizeof(req->addr_info.mAddress));

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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(trel_udp_service, trel_receive_handler, MA
3232
void otPlatTrelEnable(otInstance *aInstance, uint16_t *aUdpPort)
3333
{
3434
struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
35-
.sin6_port = 0,
36-
.sin6_addr = NET_IN6ADDR_ANY_INIT,
37-
.sin6_scope_id = 0};
35+
.sin6_port = 0,
36+
.sin6_addr = NET_IN6ADDR_ANY_INIT,
37+
.sin6_scope_id = 0};
3838
net_socklen_t len = sizeof(addr);
3939

4040
trel_sock = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
@@ -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:
@@ -82,15 +85,15 @@ void otPlatTrelSend(otInstance *aInstance, const uint8_t *aUdpPayload, uint16_t
8285
VerifyOrExit(trel_is_enabled);
8386

8487
struct net_sockaddr_in6 dest_sock_addr = {.sin6_family = NET_AF_INET6,
85-
.sin6_port = net_htons(aDestSockAddr->mPort),
86-
.sin6_addr = {{{0}}},
87-
.sin6_scope_id = 0};
88+
.sin6_port = net_htons(aDestSockAddr->mPort),
89+
.sin6_addr = {{{0}}},
90+
.sin6_scope_id = 0};
8891

8992
memcpy(&dest_sock_addr.sin6_addr, &aDestSockAddr->mAddress, sizeof(otIp6Address));
9093

9194
if (zsock_sendto(trel_sock, aUdpPayload, aUdpPayloadLen, 0,
92-
(struct net_sockaddr *)&dest_sock_addr,
93-
sizeof(dest_sock_addr)) == aUdpPayloadLen) {
95+
(struct net_sockaddr *)&dest_sock_addr,
96+
sizeof(dest_sock_addr)) == aUdpPayloadLen) {
9497
trel_counters.mTxBytes += aUdpPayloadLen;
9598
trel_counters.mTxPackets++;
9699
} else {
@@ -126,7 +129,7 @@ static void trel_receive_handler(struct net_socket_service_event *evt)
126129
VerifyOrExit(openthread_border_router_allocate_message((void **)&req) == OT_ERROR_NONE);
127130

128131
len = zsock_recvfrom(trel_sock, req->buffer, sizeof(req->buffer), 0,
129-
(struct net_sockaddr *)&addr, &addrlen);
132+
(struct net_sockaddr *)&addr, &addrlen);
130133
VerifyOrExit(len > 0);
131134

132135
trel_counters.mRxBytes += len;

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)