From ae932c9c46d406495e536be23802c5558cb7dcf5 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Sat, 28 Nov 2026 04:22:57 -0500 Subject: [PATCH] Offload: follow next-hop MAC changes; veto af-to cmm put a next hop's MAC into CDX when it programmed the route and then only watched the next hop's address and interface. A next hop that changed its MAC (a new ISP modem, an IP reused by another LAN host) kept receiving its flows at the old MAC, possibly another host's, and new flows reused the stale route until cmm was restarted. Neighbors no route used also stayed cached as resolved, so a later route to the same IP got the old MAC without asking the kernel. cmm_neigh_check() now re-reads the ARP and NDP tables (one dump per family) and marks every cached neighbor whose MAC changed or is gone; cmm_route_check_neighbors() deregisters the routes using it and their offloaded flows, which re-offload once the next hop resolves again. It runs every maintenance cycle (30 s) and after route-socket changes, and cmm_route_handle_change() also treats a different MAC as a change. The last reference to a neighbor frees it. The link-layer table parser is shared by both lookups and bounds-checks each entry's length. pf_notify vetoes af-to (NAT64) states: their two keys are in different address families, which cmm cannot program. Co-Authored-By: Claude Opus 5.5 --- ARCHITECTURE.md | 7 +- README.md | 6 +- RELEASE.md | 13 +- fastpath/cmm/cmm_conn.c | 4 + fastpath/cmm/cmm_neigh.c | 256 ++++++++++++++++++++------------- fastpath/cmm/cmm_neigh.h | 6 +- fastpath/cmm/cmm_route.c | 56 +++++++- fastpath/cmm/cmm_route.h | 4 + fastpath/pf_notify/pf_notify.c | 2 + fastpath/pf_notify/pf_notify.h | 2 + 10 files changed, 239 insertions(+), 117 deletions(-) diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 46e3db1..58347e8 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -95,7 +95,7 @@ Each event carries the state's keys, the interface it was created on reply-to target, and a veto mask (`PFN_VETO_*`) for rule actions only pf_test() can carry out per packet: dup-to, another FIB, a dummynet pipe or queue, ALTQ, `log (all)` or pflow, scrub options, set-tos, max-mss, -min-ttl, max-pkt-size. CMM never offloads a vetoed flow. +min-ttl, max-pkt-size, af-to. CMM never offloads a vetoed flow. Exposes `/dev/pfnotify` (read-write, non-blocking). Required by CMM — CMM will not start without this module loaded. Uses a 128K-entry ring @@ -150,8 +150,9 @@ next-hop neighbors, and programs CDX with offload entries. 5. PF deletes a state (close, timeout, `pfctl -k`) → DELETE → CMM removes the flow from hardware 6. CDX timer expires or TCP FIN → FCI event → CMM clears offload flag -7. A route change tears down the flows using it; while PF's states are - alive, the maintenance timer (30s) re-runs step 2 and re-offloads +7. A route change, or a next hop's MAC changing in the ARP/NDP table + (checked every 30s), tears down the flows using it; while PF's states + are alive, the maintenance timer (30s) re-runs step 2 and re-offloads Source: `fastpath/cmm/` diff --git a/README.md b/README.md index 72aa339..1f6452a 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,9 @@ MSS clamping, `log (all)`) stay in software. Caveats for offloaded flows: - VLAN interface counters and per-rule counters miss hardware traffic (physical port counters and per-state counters include it). Captive portal accounting probably does too. -- cmm does not notice a next hop's MAC address changing (a new ISP modem, a - LAN host with a new NIC but the same IP) and keeps sending its flows to - the old one. Run `service cmm restart` after such a change. +- When a next hop's MAC address changes (a new ISP modem, a LAN host with a + new NIC but the same IP), its offloaded flows can go to the old MAC for + up to 30 seconds, until cmm's next check of the ARP/NDP table. - Not tested: IPv6, links with different MTUs (PPPoE, VPN), CARP/pfsync. - After updating `mono-gateway`, reboot so the loaded `pf_notify.ko` matches the new cmm; until then nothing is offloaded. diff --git a/RELEASE.md b/RELEASE.md index 99646ad..14f3436 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -351,7 +351,8 @@ A flow goes into hardware only where that does what pf would have done - pf passed it in and out, and none of its rules asks for something only pf does per packet: dup-to, another FIB, a shaper pipe or queue, ALTQ, - `log (all)` or pflow, scrub options, set-tos, max-mss, min-ttl; + `log (all)` or pflow, scrub options, set-tos, max-mss, min-ttl, af-to + (NAT64); - the routing table sends each direction out of the interface pf's state for it is on. A route-to or reply-to counts only while it names the same interface and next hop as the routing table, as OPNsense's automatic @@ -372,11 +373,11 @@ count in hardware, and cmm copies the hardware's counts into pf's states. IPsec is not offloaded (the vendor's IPsec offload was removed); it runs in FreeBSD's stack, with CAAM through crypto(9). -Known gap: cmm resolves a next hop's MAC address when it programs the route -and only reacts to changes of the next hop's address or interface -(`cmm_route_handle_change()`; `cmm_neigh_invalidate()` has no caller), so a -next hop that changes its MAC keeps receiving at the old one until cmm is -restarted. +A next hop's MAC address goes into CDX when its route is programmed. Every +30 seconds, and after route-socket changes, cmm re-reads the ARP and NDP +tables and tears down routes whose next hop moved to another MAC or left +the table (`cmm_neigh_check()`, `cmm_route_check_neighbors()`): its IP may +belong to another host by then. Neighbors no route uses are not cached. After updating `mono-gateway` with cmm on, reboot: until then the new cmm and the loaded `pf_notify.ko` differ, and cmm offloads nothing (it logs diff --git a/fastpath/cmm/cmm_conn.c b/fastpath/cmm/cmm_conn.c index 82e2efa..9e545a2 100644 --- a/fastpath/cmm/cmm_conn.c +++ b/fastpath/cmm/cmm_conn.c @@ -909,6 +909,10 @@ cmm_conn_maintenance(struct cmm_global *g) int i; unsigned int retried = 0; + /* First drop flows whose next hop moved to another MAC: the retry + below offloads them again with the new one */ + cmm_route_check_neighbors(g); + for (i = 0; i < CONN_HASH_SIZE; i++) { for (pos = list_first(&conn_hash[i]); pos != &conn_hash[i]; pos = list_next(pos)) { diff --git a/fastpath/cmm/cmm_neigh.c b/fastpath/cmm/cmm_neigh.c index 07c1b76..a58c6fd 100644 --- a/fastpath/cmm/cmm_neigh.c +++ b/fastpath/cmm/cmm_neigh.c @@ -66,117 +66,164 @@ neigh_find(sa_family_t af, const void *ip, int ifindex) } /* - * Resolve neighbor by walking the kernel's link-layer table - * via sysctl(NET_RT_FLAGS). Each entry is an rt_msghdr followed - * by sockaddrs: DST (sockaddr_in/in6) + GATEWAY (sockaddr_dl). - * + * Read the kernel's link-layer table for af (ARP or NDP) the way arp(8) + * does, with sysctl(NET_RT_FLAGS): rt_msghdrs, each with a DST sockaddr and + * a GATEWAY sockaddr_dl. *bufp is NULL for an empty table; the caller frees + * it. Returns -1 if the table cannot be read. + */ +static int +lltable_dump(sa_family_t af, char **bufp, size_t *lenp) +{ + int mib[6] = { CTL_NET, PF_ROUTE, 0, af, NET_RT_FLAGS, 0 }; + size_t needed; + char *buf; + + *bufp = NULL; + *lenp = 0; + if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) + return (-1); + if (needed == 0) + return (0); + needed += needed / 8; /* room for entries added meanwhile */ + buf = malloc(needed); + if (buf == NULL) + return (-1); + if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { + free(buf); + return (-1); + } + *bufp = buf; + *lenp = needed; + return (0); +} + +/* The MAC address the table in buf gives ip; -1 if it has none (no entry, + or one still being resolved). */ +static int +lltable_find(const char *buf, size_t len, sa_family_t af, const uint8_t *ip, + uint8_t *mac) +{ + const char *next, *lim; + struct rt_msghdr *rtm; + struct cmm_rtsock_addrs addrs; + struct sockaddr_dl *sdl; + + lim = buf + len; + for (next = buf; next + sizeof(*rtm) <= lim; ) { + rtm = (struct rt_msghdr *)(uintptr_t)next; + if (rtm->rtm_msglen < sizeof(*rtm) || + rtm->rtm_msglen > (size_t)(lim - next)) + break; + next += rtm->rtm_msglen; + + cmm_rtsock_parse_addrs(rtm, rtm->rtm_msglen, &addrs); + if (addrs.dst == NULL || addrs.gateway == NULL || + addrs.gateway->sa_family != AF_LINK || + addrs.dst->sa_family != af) + continue; + if (af == AF_INET) { + if (memcmp(&((struct sockaddr_in *)addrs.dst)->sin_addr, + ip, 4) != 0) + continue; + } else { + if (memcmp(&((struct sockaddr_in6 *)addrs.dst)->sin6_addr, + ip, 16) != 0) + continue; + } + sdl = (struct sockaddr_dl *)addrs.gateway; + if (sdl->sdl_alen != ETHER_ADDR_LEN) + continue; + memcpy(mac, LLADDR(sdl), ETHER_ADDR_LEN); + return (0); + } + return (-1); +} + +/* A PPPoE peer has no ARP entry: its MAC comes from the session. */ +static int +neigh_pppoe_peer(const struct cmm_neigh *neigh, uint8_t *mac) +{ + struct cmm_interface *itf; + + itf = cmm_itf_find_by_index(neigh->ifindex); + if (itf == NULL || !(itf->itf_flags & ITF_F_PPPOE)) + return (-1); + memcpy(mac, itf->pppoe_peer_mac, ETHER_ADDR_LEN); + return (0); +} + +/* + * Resolve neighbor from the kernel's link-layer table. * Returns 0 on success, -1 on failure. */ static int neigh_resolve(struct cmm_global *g __unused, struct cmm_neigh *neigh) { - int mib[6]; - size_t needed; - char *buf, *next, *lim; - struct rt_msghdr *rtm; - struct cmm_rtsock_addrs addrs; + char *buf; + size_t len; + int rc; - /* - * PPPoE is point-to-point — no ARP. The peer MAC is known - * from PPPoE session negotiation, stored in the interface. - */ - { - struct cmm_interface *itf; - - itf = cmm_itf_find_by_index(neigh->ifindex); - if (itf != NULL && (itf->itf_flags & ITF_F_PPPOE)) { - memcpy(neigh->macaddr, itf->pppoe_peer_mac, - ETHER_ADDR_LEN); - neigh->state = NEIGH_RESOLVED; - return (0); - } + if (neigh_pppoe_peer(neigh, neigh->macaddr) == 0) { + neigh->state = NEIGH_RESOLVED; + return (0); } - - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = neigh->family; - mib[4] = NET_RT_FLAGS; - mib[5] = 0; - - if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { + if (lltable_dump(neigh->family, &buf, &len) < 0) { neigh->state = NEIGH_FAILED; return (-1); } - - if (needed == 0) { - neigh->state = NEIGH_INCOMPLETE; - return (-1); - } - - buf = malloc(needed); - if (buf == NULL) { - neigh->state = NEIGH_FAILED; - return (-1); - } - - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { - free(buf); - neigh->state = NEIGH_FAILED; - return (-1); - } - - lim = buf + needed; - for (next = buf; next < lim; ) { - rtm = (struct rt_msghdr *)next; - if (rtm->rtm_msglen == 0) - break; - next += rtm->rtm_msglen; - - cmm_rtsock_parse_addrs(rtm, rtm->rtm_msglen, &addrs); - - if (addrs.dst == NULL || addrs.gateway == NULL) - continue; - if (addrs.gateway->sa_family != AF_LINK) - continue; - - /* Match destination IP */ - if (neigh->family == AF_INET) { - struct sockaddr_in *sin; - - if (addrs.dst->sa_family != AF_INET) - continue; - sin = (struct sockaddr_in *)addrs.dst; - if (memcmp(&sin->sin_addr, neigh->ipaddr, 4) != 0) - continue; - } else { - struct sockaddr_in6 *sin6; - - if (addrs.dst->sa_family != AF_INET6) - continue; - sin6 = (struct sockaddr_in6 *)addrs.dst; - if (memcmp(&sin6->sin6_addr, neigh->ipaddr, 16) != 0) - continue; - } - - /* Found — extract MAC from sockaddr_dl gateway */ - { - struct sockaddr_dl *sdl; - - sdl = (struct sockaddr_dl *)addrs.gateway; - if (sdl->sdl_alen != ETHER_ADDR_LEN) - continue; - - memcpy(neigh->macaddr, LLADDR(sdl), ETHER_ADDR_LEN); - neigh->state = NEIGH_RESOLVED; - free(buf); - return (0); - } - } - + rc = (buf == NULL) ? -1 : + lltable_find(buf, len, neigh->family, neigh->ipaddr, neigh->macaddr); free(buf); - neigh->state = NEIGH_INCOMPLETE; - return (-1); + neigh->state = (rc == 0) ? NEIGH_RESOLVED : NEIGH_INCOMPLETE; + return (rc); +} + +/* + * A route's next-hop MAC goes into CDX when the route is programmed, and + * only the next hop's address and interface are watched after that. Re-read + * the ARP and NDP tables and mark STALE every resolved neighbor whose MAC + * changed or whose entry is gone: its IP may now belong to another host, + * which must not receive the old one's traffic. cmm_route_check_neighbors() + * then tears down the routes using it. Returns the number marked. + */ +int +cmm_neigh_check(void) +{ + static const sa_family_t afs[] = { AF_INET, AF_INET6 }; + struct cmm_neigh *neigh; + struct list_head *pos; + uint8_t mac[ETHER_ADDR_LEN]; + char *buf; + size_t len; + int marked = 0, i; + unsigned int f; + + for (f = 0; f < sizeof(afs) / sizeof(afs[0]); f++) { + /* A table that cannot be read says nothing about its entries */ + if (lltable_dump(afs[f], &buf, &len) < 0) + continue; + for (i = 0; i < NEIGH_HASH_TOTAL; i++) { + for (pos = list_first(&neigh_hash[i]); + pos != &neigh_hash[i]; pos = list_next(pos)) { + neigh = container_of(pos, struct cmm_neigh, + entry); + if (neigh->family != afs[f] || + neigh->state != NEIGH_RESOLVED) + continue; + if (neigh_pppoe_peer(neigh, mac) != 0 && + (buf == NULL || lltable_find(buf, len, + afs[f], neigh->ipaddr, mac) != 0)) + memset(mac, 0, sizeof(mac)); + if (memcmp(mac, neigh->macaddr, + ETHER_ADDR_LEN) == 0) + continue; + neigh->state = NEIGH_STALE; + marked++; + } + } + free(buf); + } + return (marked); } int @@ -276,6 +323,11 @@ cmm_neigh_get(struct cmm_global *g, sa_family_t af, return (NULL); } +/* + * Release a reference. The last one frees the entry: a neighbor no route + * uses is not kept, so the next route to it reads its MAC afresh instead of + * one cached from a host that may have left. + */ void cmm_neigh_put(struct cmm_neigh *neigh) { @@ -283,6 +335,10 @@ cmm_neigh_put(struct cmm_neigh *neigh) return; if (neigh->refcount > 0) neigh->refcount--; + if (neigh->refcount == 0) { + list_del(&neigh->entry); + free(neigh); + } } void diff --git a/fastpath/cmm/cmm_neigh.h b/fastpath/cmm/cmm_neigh.h index 33ef1f7..1384463 100644 --- a/fastpath/cmm/cmm_neigh.h +++ b/fastpath/cmm/cmm_neigh.h @@ -44,9 +44,13 @@ void cmm_neigh_fini(void); struct cmm_neigh *cmm_neigh_get(struct cmm_global *g, sa_family_t af, const void *ip, int ifindex); -/* Release reference */ +/* Release reference; the last one frees the entry */ void cmm_neigh_put(struct cmm_neigh *neigh); +/* Mark STALE every resolved neighbor whose MAC changed or is gone from the + kernel's ARP/NDP table; returns the number marked */ +int cmm_neigh_check(void); + /* Invalidate all neighbors on a given interface */ void cmm_neigh_flush_ifindex(int ifindex); diff --git a/fastpath/cmm/cmm_route.c b/fastpath/cmm/cmm_route.c index 491e1b4..72131a7 100644 --- a/fastpath/cmm/cmm_route.c +++ b/fastpath/cmm/cmm_route.c @@ -324,8 +324,8 @@ cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm, struct cmm_rtsock_addrs addrs; struct cmm_route *rt; struct list_head *pos; - uint8_t old_gw[16]; - int old_oif, alen, i; + uint8_t old_gw[16], old_mac[ETHER_ADDR_LEN]; + int old_oif, alen, i, mac_same; cmm_rtsock_parse_addrs(rtm, msglen, &addrs); @@ -345,9 +345,14 @@ cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm, pos != &route_hash[i]; pos = list_next(pos)) { rt = container_of(pos, struct cmm_route, entry); - /* Save old values before re-resolving */ + /* Save old values before re-resolving; a programmed + route's neighbor holds the MAC CDX was given */ old_oif = rt->oif_index; memcpy(old_gw, rt->gw, sizeof(old_gw)); + memset(old_mac, 0, sizeof(old_mac)); + if (rt->neigh != NULL) + memcpy(old_mac, rt->neigh->macaddr, + sizeof(old_mac)); /* Drop old neighbor, re-resolve */ if (rt->neigh != NULL) { @@ -360,8 +365,11 @@ cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm, /* If nothing changed, skip CDX invalidation */ alen = (rt->family == AF_INET) ? 4 : 16; + mac_same = !rt->fpp_programmed || (rt->neigh != NULL && + memcmp(rt->neigh->macaddr, old_mac, + sizeof(old_mac)) == 0); if (rt->oif_index == old_oif && - memcmp(rt->gw, old_gw, alen) == 0) + memcmp(rt->gw, old_gw, alen) == 0 && mac_same) continue; /* Route changed — tear down stale CDX state */ @@ -376,6 +384,46 @@ cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm, /* Re-evaluate tunnel and socket registrations after route changes */ cmm_tunnel_route_update(g); cmm_socket_route_update(g); + + /* An ARP/NDP change may come as a route message too */ + cmm_route_check_neighbors(g); +} + +/* + * Tear down every route whose next hop cmm_neigh_check() found at another + * MAC, or gone: CDX would keep sending the route's flows to the old MAC. + * All of them first, while the neighbor is still marked; the flows + * re-offload once the next hop resolves again (conn_try_offload() looks the + * neighbor up afresh). Run every maintenance cycle and after route changes. + */ +void +cmm_route_check_neighbors(struct cmm_global *g) +{ + struct cmm_route *rt; + struct list_head *pos; + int i, count = 0; + + if (cmm_neigh_check() == 0) + return; + for (i = 0; i < ROUTE_HASH_TOTAL; i++) { + for (pos = list_first(&route_hash[i]); + pos != &route_hash[i]; pos = list_next(pos)) { + rt = container_of(pos, struct cmm_route, entry); + if (rt->neigh == NULL || rt->neigh->state != NEIGH_STALE) + continue; + if (rt->fpp_programmed) { + cmm_route_invalidate_conns(g, rt); + cmm_fe_route_deregister(g, rt); + count++; + } + cmm_neigh_put(rt->neigh); + rt->neigh = NULL; + } + } + if (count > 0) + cmm_print(CMM_LOG_INFO, + "route: %d route(s) deregistered: next-hop MAC changed", + count); } /* diff --git a/fastpath/cmm/cmm_route.h b/fastpath/cmm/cmm_route.h index ea0cee6..f2289de 100644 --- a/fastpath/cmm/cmm_route.h +++ b/fastpath/cmm/cmm_route.h @@ -57,6 +57,10 @@ void cmm_route_gc(struct cmm_global *g); void cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm, size_t msglen); +/* Deregister routes whose next-hop MAC changed or is gone (and their + offloaded connections) */ +void cmm_route_check_neighbors(struct cmm_global *g); + /* Invalidate all routes using a specific output interface */ void cmm_route_invalidate_by_oif(struct cmm_global *g, int oif_index); diff --git a/fastpath/pf_notify/pf_notify.c b/fastpath/pf_notify/pf_notify.c index 8b08d47..b7e10af 100644 --- a/fastpath/pf_notify/pf_notify.c +++ b/fastpath/pf_notify/pf_notify.c @@ -198,6 +198,8 @@ pfn_veto(const struct pf_kstate *s) s->act.max_mss != 0 || s->act.min_ttl != 0 || s->act.max_pkt_size != 0 || s->act.set_tos != 0) veto |= PFN_VETO_NORMALIZE; + if (s->key[PF_SK_WIRE]->af != s->key[PF_SK_STACK]->af) + veto |= PFN_VETO_AFTO; return (veto); } diff --git a/fastpath/pf_notify/pf_notify.h b/fastpath/pf_notify/pf_notify.h index 83c0a52..62576dd 100644 --- a/fastpath/pf_notify/pf_notify.h +++ b/fastpath/pf_notify/pf_notify.h @@ -40,6 +40,8 @@ #define PFN_VETO_LOG 0x0010 /* log all, or pflow accounting */ #define PFN_VETO_NORMALIZE 0x0020 /* scrub, set-tos, set-prio, max-mss, min-ttl, max-pkt-size */ +#define PFN_VETO_AFTO 0x0040 /* af-to: translated between IPv4 and + IPv6, which CDX cannot do */ /* * A state's route-to or reply-to (pfn_event.rt). CMM offloads such a flow