Skip to content

Commit

Permalink
Allow overriding netif->input (#64)
Browse files Browse the repository at this point in the history
Instead of statically calling ethernet_input() (which is actually
defined to be ethernet_input_LWIP2), store a pointer to it in
netif->input and call that.

This makes it easy to hook into the processing of input packets and
implement a little firewall. Of course any code doing this should save
the original pointer and call it when applicable. For an example, see:

  https://github.com/martin-ger/esp_wifi_repeater/blob/2499913094a707b79ea4e357273a32755e31c723/user/user_main.c#L693-L697

Also remove various lines where netif->input would be reset. Set it
once during netif_add(), and then trust that it wouldn't get changed
without reason.
  • Loading branch information
rovo89 authored Nov 3, 2023
1 parent 8e3c4ee commit e4051de
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
7 changes: 1 addition & 6 deletions glue-esp/lwip-esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,6 @@ static int netif_is_new (struct netif* netif)
else
netif->num = SOFTAP_IF;

netif->input = ethernet_input;

if (netif_esp[netif->num] == netif)
{
uprint(DBG "netif (%d): already added\n", netif->num);
Expand Down Expand Up @@ -505,9 +503,7 @@ struct netif* netif_add (
#endif /* ENABLE_LOOPBACK */
netif->state = state;

uassert(packet_incoming == ethernet_input);
(void)packet_incoming;
netif->input = ethernet_input;
netif->input = packet_incoming;

#if LWIP_NETIF_HWADDRHINT
#error
Expand Down Expand Up @@ -559,7 +555,6 @@ void netif_set_addr (struct netif* netif, ip_addr_t* ipaddr, ip_addr_t* netmask,
netif->ip_addr.addr = ipaddr->addr;
netif->netmask.addr = netmask->addr;
netif->gw.addr = gw->addr;
uassert(netif->input == ethernet_input);

// ask blobs
struct ip_info set;
Expand Down
17 changes: 3 additions & 14 deletions glue-lwip/lwip-git.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,6 @@ void esp2glue_pbuf_freed (void* pbuf)
pbuf_free((struct pbuf*)pbuf);
}

static err_t new_input (struct pbuf *p, struct netif *inp)
{
(void)p;
(void)inp;
//uerror("internal error, new-netif->input() cannot be called\n");
uassert(0);
return ERR_ABRT;
}

void esp2glue_netif_set_default (int netif_idx)
{
if (netif_idx == STATION_IF || netif_idx == SOFTAP_IF)
Expand Down Expand Up @@ -354,9 +345,6 @@ static void netif_sta_status_callback (struct netif* netif)
static void netif_init_common (struct netif* netif)
{
netif->flags |= NETIF_FLAG_IGMP;
// irrelevant,not used since esp-lwip receive data and call esp2glue_ethernet_input()
netif->input = new_input;
// meaningfull:
netif->output = etharp_output;
netif->linkoutput = new_linkoutput;

Expand Down Expand Up @@ -453,7 +441,7 @@ void esp2glue_lwip_init (void)
{
netif_add(&netif_git[i], &aip, &amask, &agw, /*state*/NULL,
i == STATION_IF? netif_init_sta: netif_init_ap,
/*useless input*/NULL);
ethernet_input);
netif_git[i].hwaddr_len = NETIF_MAX_HWADDR_LEN;
memset(netif_git[i].hwaddr, 0, NETIF_MAX_HWADDR_LEN);
}
Expand Down Expand Up @@ -491,7 +479,8 @@ err_glue_t esp2glue_ethernet_input (int netif_idx, void* received)
//display_ip32(" ip=", netif_git[netif_idx].ip_addr.addr);
//nl();

return git2glue_err(ethernet_input((struct pbuf*)received, &netif_git[netif_idx]));
struct netif* netif = &netif_git[netif_idx];
return git2glue_err(netif->input((struct pbuf*)received, netif));
}

void esp2glue_dhcps_start (struct ip_info* info)
Expand Down

0 comments on commit e4051de

Please sign in to comment.