Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions modules/openthread/platform/dhcp6_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ void otPlatInfraIfDhcp6PdClientSend(otInstance *aInstance,
VerifyOrExit(dhcpv6_pd_client_sock != -1 && aInfraIfIndex == ail_iface_idx);
uint16_t length = otMessageGetLength(aMessage);
struct net_sockaddr_in6 dest_sock_addr = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(DHCPV6_SERVER_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
.sin6_port = net_htons(DHCPV6_SERVER_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
Comment on lines 65 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, if you'd add a trailing comma to the last item, it would be formatted like:

	struct net_sockaddr_in6 dest_sock_addr = {
		.sin6_family = NET_AF_INET6,
		.sin6_port = net_htons(DHCPV6_SERVER_PORT),
		.sin6_addr = NET_IN6ADDR_ANY_INIT,
		.sin6_scope_id = 0,
	};

Which is a best nicer IMO.


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

Expand Down Expand Up @@ -116,9 +116,9 @@ static void dhcpv6_pd_client_socket_init(uint32_t infra_if_index)
int hop_limit = 255;
int on = 1;
struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(DHCPV6_CLIENT_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
.sin6_port = net_htons(DHCPV6_CLIENT_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};

dhcpv6_pd_client_sock = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
VerifyOrExit(dhcpv6_pd_client_sock >= 0);
Expand Down Expand Up @@ -163,6 +163,9 @@ static void dhcpv6_pd_client_socket_deinit(uint32_t infra_if_index)

sockfd_udp[0].fd = -1;
dhcpv6_pd_client_sock = -1;

net_socket_service_register(&dhcpv6_pd_client_udp_receive, sockfd_udp,
ARRAY_SIZE(sockfd_udp), NULL);
exit:
return;
}
Expand Down
40 changes: 30 additions & 10 deletions modules/openthread/platform/infra_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static uint32_t ail_iface_index;
static struct net_icmp_ctx ra_ctx;
static struct net_icmp_ctx rs_ctx;
static struct net_icmp_ctx na_ctx;
static struct net_in6_addr mcast_addr;

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

ot_instance = instance;
ail_iface_ptr = ail_iface;
ail_iface_index = (uint32_t)net_if_get_by_iface(ail_iface_ptr);
net_ipv6_addr_create_ll_allrouters_mcast(&addr);
VerifyOrExit(net_ipv6_mld_join(ail_iface, &addr) == 0, error = OT_ERROR_FAILED);

net_ipv6_addr_create_ll_allrouters_mcast(&mcast_addr);
ret = net_ipv6_mld_join(ail_iface, &mcast_addr);

VerifyOrExit((ret == 0 || ret == -EALREADY), error = OT_ERROR_FAILED);

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

otError infra_if_deinit(void)
{
otError error = OT_ERROR_NONE;

ot_instance = NULL;
ail_iface_index = 0;

VerifyOrExit(net_ipv6_mld_leave(ail_iface_ptr, &mcast_addr) == 0,
error = OT_ERROR_FAILED);

exit:
ail_iface_ptr = NULL;

return error;
}

static void handle_ra_from_ot(const uint8_t *buffer, uint16_t buffer_length)
{
struct net_if *ot_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
Expand Down Expand Up @@ -280,14 +300,14 @@ void infra_if_stop_icmp6_listener(void)
otError infra_if_nat64_init(void)
{
otError error = OT_ERROR_NONE;
struct sockaddr_in anyaddr = {.sin_family = AF_INET,
.sin_port = 0,
.sin_addr = INADDR_ANY_INIT};
struct net_sockaddr_in anyaddr = {.sin_family = NET_AF_INET,
.sin_port = 0,
.sin_addr = NET_INADDR_ANY_INIT};

raw_infra_if_sock = zsock_socket(AF_INET, SOCK_RAW, IPPROTO_IP);
raw_infra_if_sock = zsock_socket(NET_AF_INET, NET_SOCK_RAW, NET_IPPROTO_IP);
VerifyOrExit(raw_infra_if_sock >= 0, error = OT_ERROR_FAILED);
VerifyOrExit(zsock_bind(raw_infra_if_sock, (struct sockaddr *)&anyaddr,
sizeof(struct sockaddr_in)) == 0,
VerifyOrExit(zsock_bind(raw_infra_if_sock, (struct net_sockaddr *)&anyaddr,
sizeof(struct net_sockaddr_in)) == 0,
error = OT_ERROR_FAILED);

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

ot_pkt = net_pkt_alloc_with_buffer(ail_iface_ptr, len, AF_INET, 0, K_NO_WAIT);
ot_pkt = net_pkt_alloc_with_buffer(ail_iface_ptr, len, NET_AF_INET, 0, K_NO_WAIT);
VerifyOrExit(ot_pkt != NULL, error = OT_ERROR_FAILED);

VerifyOrExit(net_pkt_write(ot_pkt, req->buffer, len) == 0, error = OT_ERROR_FAILED);
Expand Down
55 changes: 32 additions & 23 deletions modules/openthread/platform/mdns_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <zephyr/net/socket_service.h>
#include <openthread/nat64.h>
#include "sockets_internal.h"
#include <errno.h>

#define MULTICAST_PORT 5353
#define MAX_SERVICES CONFIG_OPENTHREAD_ZEPHYR_BORDER_ROUTER_MAX_MDNS_SERVICES
Expand Down Expand Up @@ -104,13 +105,14 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
struct net_ipv6_mreq mreq_v6 = {0};
int mcast_hops = 255;
int on = 1;
int mcast_join_ret = 0;

struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(MULTICAST_PORT),
.sin6_addr = {{{0}}},
.sin6_scope_id = 0};
.sin6_port = net_htons(MULTICAST_PORT),
.sin6_addr = {{{0}}},
.sin6_scope_id = 0};

mdns_sock_v6 = zsock_socket(NET_AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
mdns_sock_v6 = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
VerifyOrExit(mdns_sock_v6 >= 0, error = OT_ERROR_FAILED);
VerifyOrExit(net_if_get_name(net_if_get_by_index(ail_iface_idx), name,
CONFIG_NET_INTERFACE_NAME_LEN) > 0,
Expand All @@ -130,8 +132,9 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
error = OT_ERROR_FAILED);
mreq_v6.ipv6mr_ifindex = ail_iface_idx;
net_ipv6_addr_create(&mreq_v6.ipv6mr_multiaddr, 0xff02, 0, 0, 0, 0, 0, 0, 0x00fb);
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, NET_IPPROTO_IPV6, ZSOCK_IPV6_ADD_MEMBERSHIP,
&mreq_v6, sizeof(mreq_v6)) == 0,
mcast_join_ret = zsock_setsockopt(mdns_sock_v6, NET_IPPROTO_IPV6, ZSOCK_IPV6_ADD_MEMBERSHIP,
&mreq_v6, sizeof(mreq_v6));
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
error = OT_ERROR_FAILED);
VerifyOrExit(zsock_setsockopt(mdns_sock_v6, NET_IPPROTO_IPV6, ZSOCK_IPV6_MULTICAST_LOOP,
&on, sizeof(on)) == 0,
Expand Down Expand Up @@ -168,10 +171,11 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
struct net_ip_mreqn mreq_v4 = {0};
int mcast_hops = 255;
int on = 1;
int mcast_join_ret = 0;

struct net_sockaddr_in addr = {.sin_family = NET_AF_INET,
.sin_port = net_htons(MULTICAST_PORT),
.sin_addr = {{{0}}}};
.sin_port = net_htons(MULTICAST_PORT),
.sin_addr = {{{0}}}};

mdns_sock_v4 = zsock_socket(NET_AF_INET, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
VerifyOrExit(mdns_sock_v4 >= 0, error = OT_ERROR_FAILED);
Expand All @@ -190,8 +194,9 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
error = OT_ERROR_FAILED);
mreq_v4.imr_ifindex = ail_iface_idx;
mreq_v4.imr_multiaddr.s_addr = net_htonl(0xE00000FB);
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_IP_ADD_MEMBERSHIP,
&mreq_v4, sizeof(mreq_v4)) == 0,
mcast_join_ret = zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_IP_ADD_MEMBERSHIP,
&mreq_v4, sizeof(mreq_v4));
VerifyOrExit(mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY),
error = OT_ERROR_FAILED);
VerifyOrExit(zsock_setsockopt(mdns_sock_v4, NET_IPPROTO_IP, ZSOCK_IP_MULTICAST_LOOP,
&on, sizeof(on)) == 0,
Expand Down Expand Up @@ -237,6 +242,10 @@ static otError mdns_socket_deinit(void)
sockfd_udp[1].fd = -1;
mdns_sock_v4 = -1;
#endif /* CONFIG_NET_IPV4 */

VerifyOrExit(net_socket_service_register(&mdns_udp_service, sockfd_udp,
ARRAY_SIZE(sockfd_udp), NULL) == 0,
error = OT_ERROR_FAILED);
exit:
return error;
}
Expand All @@ -246,13 +255,13 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)
uint16_t length = otMessageGetLength(message);
struct otbr_msg_ctx *req = NULL;
struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(MULTICAST_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
.sin6_port = net_htons(MULTICAST_PORT),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
#if defined(CONFIG_NET_IPV4)
struct net_sockaddr_in addr_v4 = {.sin_family = NET_AF_INET,
.sin_port = net_htons(MULTICAST_PORT),
.sin_addr = NET_INADDR_ANY_INIT};
.sin_port = net_htons(MULTICAST_PORT),
.sin_addr = NET_INADDR_ANY_INIT};
#endif /* CONFIG_NET_IPV4*/

VerifyOrExit(length <= OTBR_MESSAGE_SIZE);
Expand All @@ -267,11 +276,11 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)

VerifyOrExit(zsock_sendto(mdns_sock_v6, req->buffer, length, 0,
(struct net_sockaddr *)&addr_v6,
sizeof(addr_v6)) > 0);
sizeof(addr_v6)) > 0);
#if defined(CONFIG_NET_IPV4)
VerifyOrExit(zsock_sendto(mdns_sock_v4, req->buffer, length, 0,
(struct net_sockaddr *)&addr_v4,
sizeof(addr_v4)) > 0);
sizeof(addr_v4)) > 0);
#endif /* CONFIG_NET_IPV4 */
exit:
otMessageFree(message);
Expand All @@ -290,9 +299,9 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a
struct net_sockaddr_in addr_v4 = {0};
#endif /* CONFIG_NET_IPV4*/
struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(aAddress->mPort),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
.sin6_port = net_htons(aAddress->mPort),
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};

VerifyOrExit(length <= OTBR_MESSAGE_SIZE);
VerifyOrExit(openthread_border_router_allocate_message((void **)&req) == OT_ERROR_NONE);
Expand All @@ -310,7 +319,7 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a

VerifyOrExit(zsock_sendto(mdns_sock_v6, req->buffer, length, 0,
(struct net_sockaddr *)&addr_v6,
sizeof(addr_v6)) > 0);
sizeof(addr_v6)) > 0);
#if defined(CONFIG_NET_IPV4)
if (send_ipv4) {
VerifyOrExit(zsock_sendto(mdns_sock_v4, req->buffer, length, 0,
Expand Down Expand Up @@ -345,7 +354,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
if (family == NET_AF_INET) {
addrlen = sizeof(addr_v4);
len = zsock_recvfrom(mdns_sock_v4, req->buffer, sizeof(req->buffer), 0,
(struct net_sockaddr *)&addr_v4, &addrlen);
(struct net_sockaddr *)&addr_v4, &addrlen);
VerifyOrExit(len > 0);
otIp4ToIp4MappedIp6Address((otIp4Address *)&addr_v4.sin_addr.s_addr,
&req->addr_info.mAddress);
Expand All @@ -354,7 +363,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
#endif /* CONFIG_NET_IPV4 */
addrlen = sizeof(addr_v6);
len = zsock_recvfrom(mdns_sock_v6, req->buffer, sizeof(req->buffer), 0,
(struct net_sockaddr *)&addr_v6, &addrlen);
(struct net_sockaddr *)&addr_v6, &addrlen);
VerifyOrExit(len > 0);
memcpy(&req->addr_info.mAddress, &addr_v6.sin6_addr,
sizeof(req->addr_info.mAddress));
Expand Down
3 changes: 2 additions & 1 deletion modules/openthread/platform/platform-zephyr.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ int notify_new_tx_frame(struct net_pkt *pkt);

#if defined(CONFIG_OPENTHREAD_ZEPHYR_BORDER_ROUTER)
otError infra_if_init(otInstance *instance, struct net_if *ail_iface);
otError infra_if_deinit(void);
otError infra_if_start_icmp6_listener(void);
void infra_if_stop_icmp6_listener(void);
void udp_plat_init_sockfd(void);
Expand All @@ -129,7 +130,7 @@ otError mdns_plat_socket_init(otInstance *ot_instance, uint32_t ail_iface_idx);
void mdns_plat_monitor_interface(struct net_if *ail_iface);
otError border_agent_init(otInstance *instance);
void border_agent_deinit(void);
otError trel_plat_init(otInstance *instance, struct net_if *ail_iface_ptr);
otError trel_plat_init(otInstance *instance, struct net_if *ail_iface);
otError dhcpv6_pd_client_init(otInstance *ot_instance, uint32_t ail_iface_index);
otError dns_upstream_resolver_init(otInstance *ot_instance);
void openthread_border_router_set_nat64_translator_enabled(bool enable);
Expand Down
31 changes: 21 additions & 10 deletions modules/openthread/platform/trel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_ip.h>
#include "sockets_internal.h"
#include <platform-zephyr.h>

#define MAX_SERVICES CONFIG_OPENTHREAD_ZEPHYR_BORDER_ROUTER_TREL_SERVICES

static struct zsock_pollfd sockfd_udp[MAX_SERVICES];
static int trel_sock = -1;
static struct otInstance *ot_instance_ptr;
static struct net_if *ail_iface_ptr;
static otPlatTrelCounters trel_counters;
static bool trel_is_enabled;
static void trel_receive_handler(struct net_socket_service_event *evt);
Expand All @@ -30,9 +32,9 @@ NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(trel_udp_service, trel_receive_handler, MA
void otPlatTrelEnable(otInstance *aInstance, uint16_t *aUdpPort)
{
struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6,
.sin6_port = 0,
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
.sin6_port = 0,
.sin6_addr = NET_IN6ADDR_ANY_INIT,
.sin6_scope_id = 0};
net_socklen_t len = sizeof(addr);

trel_sock = zsock_socket(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP);
Expand All @@ -47,6 +49,11 @@ void otPlatTrelEnable(otInstance *aInstance, uint16_t *aUdpPort)

trel_is_enabled = true;

if (ail_iface_ptr != NULL && net_if_is_up(ail_iface_ptr)) {
(void)trel_plat_init(ot_instance_ptr, ail_iface_ptr);
}


Comment on lines +55 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, single empty line is sufficient

Suggested change

exit:
return;

Expand All @@ -63,6 +70,9 @@ void otPlatTrelDisable(otInstance *aInstance)
sockfd_udp[0].fd = -1;
trel_sock = -1;

net_socket_service_register(&trel_udp_service, sockfd_udp,
ARRAY_SIZE(sockfd_udp), NULL);

trel_is_enabled = false;

exit:
Expand All @@ -75,15 +85,15 @@ void otPlatTrelSend(otInstance *aInstance, const uint8_t *aUdpPayload, uint16_t
VerifyOrExit(trel_is_enabled);

struct net_sockaddr_in6 dest_sock_addr = {.sin6_family = NET_AF_INET6,
.sin6_port = net_htons(aDestSockAddr->mPort),
.sin6_addr = {{{0}}},
.sin6_scope_id = 0};
.sin6_port = net_htons(aDestSockAddr->mPort),
.sin6_addr = {{{0}}},
.sin6_scope_id = 0};

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

if (zsock_sendto(trel_sock, aUdpPayload, aUdpPayloadLen, 0,
(struct net_sockaddr *)&dest_sock_addr,
sizeof(dest_sock_addr)) == aUdpPayloadLen) {
(struct net_sockaddr *)&dest_sock_addr,
sizeof(dest_sock_addr)) == aUdpPayloadLen) {
trel_counters.mTxBytes += aUdpPayloadLen;
trel_counters.mTxPackets++;
} else {
Expand Down Expand Up @@ -119,7 +129,7 @@ static void trel_receive_handler(struct net_socket_service_event *evt)
VerifyOrExit(openthread_border_router_allocate_message((void **)&req) == OT_ERROR_NONE);

len = zsock_recvfrom(trel_sock, req->buffer, sizeof(req->buffer), 0,
(struct net_sockaddr *)&addr, &addrlen);
(struct net_sockaddr *)&addr, &addrlen);
VerifyOrExit(len > 0);

trel_counters.mRxBytes += len;
Expand All @@ -141,13 +151,14 @@ static void process_trel_message(struct otbr_msg_ctx *msg_ctx_ptr)
&msg_ctx_ptr->sock_addr);
}

otError trel_plat_init(otInstance *instance, struct net_if *ail_iface_ptr)
otError trel_plat_init(otInstance *instance, struct net_if *ail_iface)
{
otError error = OT_ERROR_NONE;
struct net_ifreq if_req = {0};
char name[CONFIG_NET_INTERFACE_NAME_LEN + 1] = {0};

ot_instance_ptr = instance;
ail_iface_ptr = ail_iface;

VerifyOrExit(net_if_get_name(ail_iface_ptr, name,
CONFIG_NET_INTERFACE_NAME_LEN) > 0,
Expand Down
3 changes: 3 additions & 0 deletions modules/openthread/platform/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void udp_plat_deinit(void)
sockfd_udp[idx].fd = -1;
}
}

net_socket_service_register(&handle_udp_receive, sockfd_udp,
ARRAY_SIZE(sockfd_udp), NULL);
}

otError otPlatUdpSocket(otUdpSocket *aUdpSocket)
Expand Down
Loading