From 8f2be7b486e7b2ade4512b3c5b01c8371afa7d09 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 21 Jun 2021 13:47:22 +0200 Subject: [PATCH 1/4] ieee802154: add ieee802154_dst_filter --- sys/include/net/ieee802154.h | 19 +++++++++++++++ sys/net/link_layer/ieee802154/ieee802154.c | 27 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/sys/include/net/ieee802154.h b/sys/include/net/ieee802154.h index 86ba001199..7994e839bd 100644 --- a/sys/include/net/ieee802154.h +++ b/sys/include/net/ieee802154.h @@ -396,6 +396,25 @@ int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan); */ int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan); +/** + * @brief Check whether a frame pass the IEEE 802.15.4 frame filter. + * + * A frame passes the frame filter only if: + * - The PAN ID matches the PAN ID of the frame filter or the broadcast PAN ID + * - Either the Short or Extended Address matches the frame filter OR the + * Short Address is the broadcast address. + * + * @param[in] mhr MAC header (PSDU) + * @param[in] pan PAN ID of the frame filter. + * @param[in] short_addr Short Address of the frame filter. + * @param[in] ext_addr Extended Address of the frame filter. + * + * @return 0 if frame passes the frame filter. + * @return 1 if frame doesn't pass the frame filter. + */ +int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan, + network_uint16_t short_addr, const eui64_t *ext_addr); + /** * @brief Gets sequence number from MAC header. * diff --git a/sys/net/link_layer/ieee802154/ieee802154.c b/sys/net/link_layer/ieee802154/ieee802154.c index ae687703d6..d466ae8dfc 100644 --- a/sys/net/link_layer/ieee802154/ieee802154.c +++ b/sys/net/link_layer/ieee802154/ieee802154.c @@ -241,4 +241,31 @@ int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan) return 0; } +int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan, + network_uint16_t short_addr, const eui64_t *ext_addr) +{ + uint8_t dst_addr[IEEE802154_LONG_ADDRESS_LEN]; + le_uint16_t dst_pan; + uint8_t pan_bcast[] = IEEE802154_PANID_BCAST; + + int addr_len = ieee802154_get_dst(mhr, dst_addr, &dst_pan); + + /* filter PAN ID */ + if ((memcmp(pan_bcast, dst_pan.u8, 2) != 0) && + (memcmp(&pan, dst_pan.u8, 2) != 0)) { + return 1; + } + + /* check destination address */ + if (((addr_len == IEEE802154_SHORT_ADDRESS_LEN) && + (memcmp(&short_addr.u8, dst_addr, addr_len) == 0 || + memcmp(ieee802154_addr_bcast, dst_addr, addr_len) == 0)) || + ((addr_len == IEEE802154_LONG_ADDRESS_LEN) && + (memcmp(ext_addr->uint8, dst_addr, addr_len) == 0))) { + return 0; + } + + return 1; +} + /** @} */ From dfeb57b7f8e3038d57e637c5c25fb65322ee0935 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 21 Jun 2021 13:47:55 +0200 Subject: [PATCH 2/4] netdev_ieee802154: deprecate netdev_ieee802154_dst_filter --- drivers/include/net/netdev/ieee802154.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/include/net/netdev/ieee802154.h b/drivers/include/net/netdev/ieee802154.h index 6895ef6532..5b06d025c1 100644 --- a/drivers/include/net/netdev/ieee802154.h +++ b/drivers/include/net/netdev/ieee802154.h @@ -208,6 +208,9 @@ int netdev_ieee802154_set(netdev_ieee802154_t *dev, netopt_t opt, const void *va * this function is meant top be used by drivers that do not support address * filtering in hw * + * @deprecated This function is currently deprecated and will be removed + * after Release 2022.01. Use @ref ieee802154_dst_filter instead. + * * @param[in] dev network device descriptor * @param[in] mhr mac header * From d68b1a5f843da4f1e0d9f7f5d6957e91325aecdc Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 21 Jun 2021 13:48:29 +0200 Subject: [PATCH 3/4] nrf802154: use ieee802154_dst_filter in netdev implementation --- cpu/nrf52/radio/nrf802154/nrf802154.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cpu/nrf52/radio/nrf802154/nrf802154.c b/cpu/nrf52/radio/nrf802154/nrf802154.c index 38d81244eb..54fb8dae44 100644 --- a/cpu/nrf52/radio/nrf802154/nrf802154.c +++ b/cpu/nrf52/radio/nrf802154/nrf802154.c @@ -492,8 +492,10 @@ void isr_radio(void) /* only process packet if event callback is set and CRC is valid */ if ((nrf802154_dev->netdev.event_callback) && (NRF_RADIO->CRCSTATUS == 1) && - (netdev_ieee802154_dst_filter(nrf802154_dev, - &rxbuf[1]) == 0)) { + (ieee802154_dst_filter(&rxbuf[1], + nrf802154_dev->pan, + (network_uint16_t*) nrf802154_dev->short_addr, + nrf802154_dev->long_addr) == 0)) { _state |= RX_COMPLETE; } else { From 48ef2ec2da1e30ac9da6dbfe298e6318ebf21e78 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Tue, 22 Jun 2021 13:58:10 +0200 Subject: [PATCH 4/4] tests/ieee802154: Add unit tests for --- .../tests-ieee802154/tests-ieee802154.c | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/tests/unittests/tests-ieee802154/tests-ieee802154.c b/tests/unittests/tests-ieee802154/tests-ieee802154.c index ee202466a5..b3e666ba9f 100644 --- a/tests/unittests/tests-ieee802154/tests-ieee802154.c +++ b/tests/unittests/tests-ieee802154/tests-ieee802154.c @@ -953,6 +953,128 @@ static void test_ieee802154_get_dst_dst8_pancomp(void) TEST_ASSERT_EQUAL_INT(exp_pan.u16, res_pan.u16); } +static void test_ieee802154_dst_filter_pan_fail(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1); + const uint8_t mhr[] = { 0, + IEEE802154_FCF_DST_ADDR_SHORT, + TEST_UINT8, + exp_pan.u8[0], exp_pan.u8[1], + exp_addr.u8[1], exp_addr.u8[0] }; + TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr, + exp_pan.u16 + 1, + exp_addr, + &long_addr)); +} + +static void test_ieee802154_dst_filter_short_addr_fail(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1); + const uint8_t mhr[] = { 0, + IEEE802154_FCF_DST_ADDR_SHORT, + TEST_UINT8, + exp_pan.u8[0], exp_pan.u8[1], + exp_addr.u8[1], exp_addr.u8[0] }; + TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr, + exp_pan.u16, + byteorder_htons(exp_addr.u16 + 1), + &long_addr)); +} + +static void test_ieee802154_dst_filter_long_addr_fail(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1); + const eui64_t long_addr_fail = {.uint64.u64 = TEST_UINT64 + 1}; + const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP, + IEEE802154_FCF_DST_ADDR_LONG, + TEST_UINT8, + exp_pan.u8[0], exp_pan.u8[1], + long_addr.uint8[7], long_addr.uint8[6], + long_addr.uint8[5], long_addr.uint8[4], + long_addr.uint8[3], long_addr.uint8[2], + long_addr.uint8[1], long_addr.uint8[0] }; + TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr, + exp_pan.u16, + exp_addr, + &long_addr_fail)); +} + +static void test_ieee802154_dst_filter_pan_short(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1); + const uint8_t mhr[] = { 0, + IEEE802154_FCF_DST_ADDR_SHORT, + TEST_UINT8, + exp_pan.u8[0], exp_pan.u8[1], + exp_addr.u8[1], exp_addr.u8[0] }; + TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr, + exp_pan.u16, + exp_addr, + &long_addr)); +} + +static void test_ieee802154_dst_filter_bcast_short(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const uint8_t pan_bcast[] = IEEE802154_PANID_BCAST; + const uint8_t mhr[] = { 0, + IEEE802154_FCF_DST_ADDR_SHORT, + TEST_UINT8, + pan_bcast[0], pan_bcast[1], + exp_addr.u8[1], exp_addr.u8[0] }; + TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr, + TEST_UINT16, + exp_addr, + &long_addr)); +} + +static void test_ieee802154_dst_filter_pan_long(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1); + const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP, + IEEE802154_FCF_DST_ADDR_LONG, + TEST_UINT8, + exp_pan.u8[0], exp_pan.u8[1], + long_addr.uint8[7], long_addr.uint8[6], + long_addr.uint8[5], long_addr.uint8[4], + long_addr.uint8[3], long_addr.uint8[2], + long_addr.uint8[1], long_addr.uint8[0] }; + TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr, + exp_pan.u16, + exp_addr, + &long_addr)); +} + +static void test_ieee802154_dst_filter_bcast_long(void) +{ + const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16); + const eui64_t long_addr = {.uint64.u64 = TEST_UINT64}; + const uint8_t pan_bcast[] = IEEE802154_PANID_BCAST; + const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP, + IEEE802154_FCF_DST_ADDR_LONG, + TEST_UINT8, + pan_bcast[0], pan_bcast[1], + long_addr.uint8[7], long_addr.uint8[6], + long_addr.uint8[5], long_addr.uint8[4], + long_addr.uint8[3], long_addr.uint8[2], + long_addr.uint8[1], long_addr.uint8[0] }; + TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr, + TEST_UINT16, + exp_addr, + &long_addr)); +} + static void test_ieee802154_get_seq(void) { const uint8_t mhr[] = { 0x00, 0x00, TEST_UINT8 }; @@ -1088,6 +1210,13 @@ Test *tests_ieee802154_tests(void) new_TestFixture(test_ieee802154_get_dst_dst2_pancomp), new_TestFixture(test_ieee802154_get_dst_dst8), new_TestFixture(test_ieee802154_get_dst_dst8_pancomp), + new_TestFixture(test_ieee802154_dst_filter_pan_fail), + new_TestFixture(test_ieee802154_dst_filter_short_addr_fail), + new_TestFixture(test_ieee802154_dst_filter_long_addr_fail), + new_TestFixture(test_ieee802154_dst_filter_pan_short), + new_TestFixture(test_ieee802154_dst_filter_bcast_short), + new_TestFixture(test_ieee802154_dst_filter_pan_long), + new_TestFixture(test_ieee802154_dst_filter_bcast_long), new_TestFixture(test_ieee802154_get_seq), new_TestFixture(test_ieee802154_get_iid_addr_len_0), new_TestFixture(test_ieee802154_get_iid_addr_len_SIZE_MAX),