- interface packet interception capability added

This commit is contained in:
Wiesner András 2024-04-30 23:11:22 +02:00
parent 96bc37f271
commit 7af522335d
2 changed files with 75 additions and 44 deletions

View File

@ -68,6 +68,8 @@ EthInterface *ethintf_new(EthIODef *io) {
memset(&ethIntf->evtCbTable, 0, sizeof(EthIntfEventCbTable));
ethIntf->interceptCb = NULL;
ethIntf->linkState = false;
ethintf_register(ethIntf);
@ -88,8 +90,16 @@ static ThreadReturnType task_ethintf(ThreadParamType param) {
case ETH_IIE_RECV_NOTIFY: {
intf->ioDef->llRxRead(intf->ioDef); // read packets, will invoke RxStore
while (mq_avail(intf->rxQ) > 0) {
// get the raw packet
RawPckt rawPckt = mq_top(intf->rxQ);
mq_pop(intf->rxQ);
// packet interception, if defined
if (intf->interceptCb != NULL) {
intf->interceptCb(intf, &rawPckt);
}
// packet processing
packsieve_input(&intf->sieve, &rawPckt);
}
@ -123,7 +133,7 @@ void ethinf_receive(EthInterface *intf, const RawPckt *rawPckt) {
bool pushOK = mq_push(intf->rxQ, rawPckt);
if (!pushOK) {
dynmem_free(rawPckt->payload);
ERROR("Input queue full, packet dropped!\n");
ERROR("Interface RECEIVE queue full, packet dropped!\n");
}
}
@ -131,7 +141,7 @@ void ethinf_transmit(EthInterface *intf, const RawPckt *rawPckt) {
if (mq_push(intf->txQ, rawPckt)) { // push packet onto the message queue
ethinf_push_notification(intf, ETH_IIE_TRANSMIT_NOTIFY, 0); // notify processing thread of the peding transmit
} else {
ERROR("Interface transmit queue full!\n");
ERROR("Interface TRANSMIT queue full, packet dropped!\n");
}
}
@ -190,3 +200,7 @@ void ethinf_up(EthInterface *intf) {
void ethinf_down(EthInterface *intf) {
intf->up = false;
}
void ethinf_set_intercept_callback(EthInterface * intf, EthIntfInterceptCb cb) {
intf->interceptCb = cb;
}

View File

@ -3,15 +3,15 @@
#include <stdbool.h>
#include "packet_sieve.h"
#include "prefab/packet_parsers/packet_parsers.h"
#include "prefab/packet_parsers/ipv4_types.h"
#include "arp_cache.h"
#include "connection_block.h"
#include "msg_queue.h"
#include "gen_queue.h"
#include "msg_queue.h"
#include "packet_sieve.h"
#include "prefab/conn_blocks/ipv4/ip_assembler.h"
#include "prefab/packet_parsers/dhcp.h"
#include "prefab/packet_parsers/ipv4_types.h"
#include "prefab/packet_parsers/packet_parsers.h"
#include <etherlib_options.h>
@ -47,6 +47,13 @@ struct EthInterface_;
typedef void (*EthIntfEvtCb)(struct EthInterface_ *intf);
/**
* Function prototype for intercepting packets on an interface.
* @param intf pointer to corresponding Ethernet interface
* @param rawPckt pointer to the IMMUTABLE raw packet
*/
typedef void (*EthIntfInterceptCb)(struct EthInterface_ *intf, const RawPckt *rawPckt);
typedef enum {
ETH_EVT_LINK_CHANGE = 0,
ETH_EVT_IP_CHANGE = 1,
@ -83,6 +90,8 @@ typedef struct EthInterface_ {
EthIntfEventCbTable evtCbTable; ///< Event callback table
bool linkState; ///< Interface link state
bool up; ///< Is the interface up?
EthIntfInterceptCb interceptCb; ///< Intercept callback
} EthInterface;
/**
@ -152,6 +161,7 @@ void ethinf_set_link_state(EthInterface * intf, bool ls);
/**
* Get link state.
* @param intf pointer to Ethernet interface
* @return link state
*/
bool ethinf_get_link_state(EthInterface *intf);
@ -167,4 +177,11 @@ void ethinf_up(EthInterface *intf);
*/
void ethinf_down(EthInterface *intf);
/**
* Set callback for packet interception.
* @param intf pointer to Ethernet interface
* @param cb intercept callback pointer
*/
void ethinf_set_intercept_callback(EthInterface *intf, EthIntfInterceptCb cb);
#endif // ETHERLIB_ETH_INTERFACE_H