Skip to content

Commit

Permalink
feat: add rx whitelist in config
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Nov 29, 2023
1 parent 821cc36 commit a019b14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
10 changes: 9 additions & 1 deletion include/zenoh-pico/transport/raweth/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,25 @@ typedef struct {
_Bool _has_vlan;
} _zp_raweth_cfg_entry;

typedef struct {
uint8_t _mac[_ZP_MAC_ADDR_LENGTH];
} _zp_raweth_cfg_whitelist_val;

// Ethertype to use in frame
extern const uint16_t _ZP_RAWETH_CFG_ETHTYPE;

// Source mac address
extern const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH];

// Sort the keyexpr alphabetically to use binary search (if size ~100+), otherwise use simple linear search
// Main config array
extern const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[];

// Mac address rx whitelist array
extern const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[];

// Array size
extern const size_t _ZP_RAWETH_CFG_SIZE;
extern const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE;

#endif // Z_FEATURE_RAWETH_TRANSPORT == 1
#endif // ZENOH_PICO_RAWETH_CONFIG_H
20 changes: 16 additions & 4 deletions src/system/unix/link/raweth.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/config.h"
#include "zenoh-pico/system/platform/unix.h"
#include "zenoh-pico/transport/raweth/config.h"
#include "zenoh-pico/utils/logging.h"
#include "zenoh-pico/utils/pointers.h"

Expand Down Expand Up @@ -94,14 +95,25 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t
}

size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr) {
// Read from socket
size_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL);
if (bytesRead < 0) {
if ((bytesRead < 0) || (bytesRead < sizeof(_zp_eth_header_t))) {
return SIZE_MAX;
}
// Address filtering
_zp_eth_header_t *header = (_zp_eth_header_t *)buff;
_Bool is_valid = false;
for (size_t i = 0; i < _ZP_RAWETH_CFG_WHITELIST_SIZE; i++) {
if (memcmp(&header->smac, _ZP_RAWETH_CFG_WHITELIST[i]._mac, _ZP_MAC_ADDR_LENGTH) == 0) { // Test byte ordering
is_valid = true;
}
}
// Ignore packet from unknown sources
if (!is_valid) {
return SIZE_MAX;
}
// Soft Filtering ?

// Copy sender mac if needed
if ((addr != NULL) && (bytesRead > 2 * ETH_ALEN)) {
if (addr != NULL) {
*addr = _z_bytes_make(sizeof(ETH_ALEN));
(void)memcpy((uint8_t *)addr->start, (buff + ETH_ALEN), sizeof(ETH_ALEN));
}
Expand Down
7 changes: 7 additions & 0 deletions src/transport/raweth/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[] = {
{{0, {0}, "another/keyexpr"}, 0x43, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab}, true}, // entry2
};

// Should be generated
const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[] = {
{{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
{{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}},
};

// Don't modify
const size_t _ZP_RAWETH_CFG_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_ARRAY);
const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_WHITELIST);

#endif // Z_FEATURE_RAWETH_TRANSPORT == 1

0 comments on commit a019b14

Please sign in to comment.