Skip to content

Commit d1858be

Browse files
Cristib05nashif
authored andcommitted
openthread: platform: infra_if: Add packet filtering rule for NAT64
This commit aims to implement IPv4 packet filtering for NAT64 translator. If packets are consumed, those are not processed anymore by network stack. NAT64 packets are created by OpenThread stack and sent on backbone interface using a raw socket. This commit attempts to fix an issue where a TCP connection is initiated by an OpenThread node, but discarded by network stack since there is no active known connection. Signed-off-by: Cristian Bulacu <[email protected]>
1 parent 1adf521 commit d1858be

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

modules/openthread/platform/infra_if.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#if defined(CONFIG_OPENTHREAD_NAT64_TRANSLATOR)
2828
#include <zephyr/net/icmp.h>
29+
#include <zephyr/net/net_pkt_filter.h>
2930
#include <openthread/nat64.h>
3031
#endif /* CONFIG_OPENTHREAD_NAT64_TRANSLATOR */
3132

@@ -45,9 +46,19 @@ static void handle_ra_from_ot(const uint8_t *buffer, uint16_t buffer_length);
4546
static struct zsock_pollfd sockfd_raw[MAX_SERVICES];
4647
static void raw_receive_handler(struct net_socket_service_event *evt);
4748
static void remove_checksums_for_eth_offloading(uint8_t *buf, uint16_t len);
49+
static bool infra_if_nat64_try_consume_packet(struct npf_test *test, struct net_pkt *pkt);
4850
static int raw_infra_if_sock = -1;
4951

5052
NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(handle_infra_if_raw_recv, raw_receive_handler, MAX_SERVICES);
53+
54+
struct ot_nat64_pkt_filter_test {
55+
struct npf_test test;
56+
};
57+
/* Packet filtering rules for NAT64 translator section */
58+
static struct ot_nat64_pkt_filter_test ot_nat64_drop_rule_check = {
59+
.test.fn = infra_if_nat64_try_consume_packet};
60+
/* Drop all traffic destined to and consumed by NAT64 translator */
61+
static NPF_RULE(ot_nat64_drop_pkt_process, NET_DROP, ot_nat64_drop_rule_check);
5162
#endif /* CONFIG_OPENTHREAD_NAT64_TRANSLATOR */
5263

5364
otError otPlatInfraIfSendIcmp6Nd(uint32_t aInfraIfIndex, const otIp6Address *aDestAddress,
@@ -318,6 +329,9 @@ otError infra_if_nat64_init(void)
318329
ARRAY_SIZE(sockfd_raw), NULL) == 0,
319330
error = OT_ERROR_FAILED);
320331

332+
npf_insert_ipv4_recv_rule(&ot_nat64_drop_pkt_process);
333+
npf_append_ipv4_recv_rule(&npf_default_ok);
334+
321335
exit:
322336
return error;
323337
}
@@ -417,4 +431,44 @@ static void remove_checksums_for_eth_offloading(uint8_t *buf, uint16_t len)
417431
break;
418432
}
419433
}
434+
435+
static bool infra_if_nat64_try_consume_packet(struct npf_test *test, struct net_pkt *pkt)
436+
{
437+
ARG_UNUSED(test);
438+
439+
struct net_buf *buf = NULL;
440+
otMessage *message = NULL;
441+
otMessageSettings settings;
442+
443+
openthread_mutex_lock();
444+
445+
if (ot_instance == NULL ||
446+
otNat64GetTranslatorState(ot_instance) != OT_NAT64_STATE_ACTIVE) {
447+
ExitNow();
448+
}
449+
450+
settings.mPriority = OT_MESSAGE_PRIORITY_NORMAL;
451+
settings.mLinkSecurityEnabled = true;
452+
453+
message = otIp4NewMessage(ot_instance, &settings);
454+
VerifyOrExit(message != NULL);
455+
456+
for (buf = pkt->buffer; buf; buf = buf->frags) {
457+
if (otMessageAppend(message, buf->data, buf->len) != OT_ERROR_NONE) {
458+
otMessageFree(message);
459+
ExitNow();
460+
}
461+
}
462+
463+
if (otNat64Send(ot_instance, message) == OT_ERROR_NONE) {
464+
net_pkt_unref(pkt);
465+
openthread_mutex_unlock();
466+
return true;
467+
}
468+
469+
exit:
470+
openthread_mutex_unlock();
471+
return false;
472+
}
473+
420474
#endif /* CONFIG_OPENTHREAD_NAT64_TRANSLATOR */

0 commit comments

Comments
 (0)