From 9625c49c95e2ce58959255e065e568673dcd796c Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Fri, 25 Nov 2022 17:31:16 -0300 Subject: [PATCH] Fix sending ARP request for IP broadcasts --- mip/mip.c | 13 +++++++++---- mongoose.c | 7 ++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mip/mip.c b/mip/mip.c index b731a4b3..0954249a 100644 --- a/mip/mip.c +++ b/mip/mip.c @@ -229,9 +229,14 @@ static inline void arp_cache_dump(const uint8_t *p) { } #endif +static const uint8_t bcastmac[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; + static uint8_t *arp_cache_find(struct mip_if *ifp, uint32_t ip) { uint8_t *p = ifp->arp_cache; - if (ip == 0 || ip == 0xffffffffU) return NULL; + if (ip == 0) return NULL; + // use broadcast MAC for local and global broadcast IP + if (ip == 0xffffffffU || ip == (ifp->ip | ~ifp->mask)) + return (uint8_t *) bcastmac; for (uint8_t i = 0, j = p[1]; i < MIP_ARP_ENTRIES; i++, j = p[j + 1]) { if (memcmp(p + j + 2, &ip, sizeof(ip)) == 0) { p[1] = j, p[0] = p[j]; // Found entry! Point list head to us @@ -845,9 +850,9 @@ void mip_init(struct mg_mgr *mgr, struct mip_if *ifp) { } } -void mip_free(struct mip_if * ifp) { - free((char *)ifp->rx.ptr); - free((char *)ifp->tx.ptr); +void mip_free(struct mip_if *ifp) { + free((char *) ifp->rx.ptr); + free((char *) ifp->tx.ptr); } int mg_mkpipe(struct mg_mgr *m, mg_event_handler_t fn, void *d, bool udp) { diff --git a/mongoose.c b/mongoose.c index 890e622f..b110acf7 100644 --- a/mongoose.c +++ b/mongoose.c @@ -6759,9 +6759,14 @@ static inline void arp_cache_dump(const uint8_t *p) { } #endif +static const uint8_t bcastmac[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; + static uint8_t *arp_cache_find(struct mip_if *ifp, uint32_t ip) { uint8_t *p = ifp->arp_cache; - if (ip == 0 || ip == 0xffffffffU) return NULL; + if (ip == 0) return NULL; + // use broadcast MAC for local and global broadcast IP + if (ip == 0xffffffffU || ip == (ifp->ip | ~ifp->mask)) + return (uint8_t *) bcastmac; for (uint8_t i = 0, j = p[1]; i < MIP_ARP_ENTRIES; i++, j = p[j + 1]) { if (memcmp(p + j + 2, &ip, sizeof(ip)) == 0) { p[1] = j, p[0] = p[j]; // Found entry! Point list head to us