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 <noreply@anthropic.com>
148 lines
4.7 KiB
C
148 lines
4.7 KiB
C
/*
|
|
* pf_notify.h — PF state change notification for offload
|
|
*
|
|
* Public header shared between pf_notify.ko and userspace (CMM).
|
|
* Defines the message format for /dev/pfnotify.
|
|
*
|
|
* Copyright 2026 Mono Technologies Inc.
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#ifndef PF_NOTIFY_H
|
|
#define PF_NOTIFY_H
|
|
|
|
#ifdef _KERNEL
|
|
#include <sys/types.h>
|
|
#else
|
|
#include <stdint.h>
|
|
#include <sys/types.h>
|
|
#endif
|
|
|
|
#define PFN_DEV_PATH "/dev/pfnotify"
|
|
#define PFN_DEV_NAME "pfnotify"
|
|
|
|
/* Event types (kernel -> userspace via read()) */
|
|
#define PFN_EVENT_INSERT 1 /* new PF state created */
|
|
#define PFN_EVENT_READY 2 /* state is offload-ready */
|
|
#define PFN_EVENT_DELETE 3 /* PF state removed */
|
|
#define PFN_EVENT_RESYNC 4 /* events were lost: drop every offloaded
|
|
flow; READY fires again for live states */
|
|
|
|
/*
|
|
* Offload vetoes (pfn_event.veto): the state's rules ask for something only
|
|
* pf_test() does per packet, so its flow must stay on the software path.
|
|
*/
|
|
#define PFN_VETO_ROUTE 0x0001 /* dup-to, or a route-to/reply-to without
|
|
an interface or in another family */
|
|
#define PFN_VETO_RTABLE 0x0002 /* policy routing to another FIB */
|
|
#define PFN_VETO_SHAPER 0x0004 /* dummynet pipe/queue (shaper, limiters) */
|
|
#define PFN_VETO_QUEUE 0x0008 /* ALTQ queue */
|
|
#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
|
|
* only while the main routing table sends those packets to the same
|
|
* interface and next hop, as pf would.
|
|
*/
|
|
#define PFN_RT_NONE 0
|
|
#define PFN_RT_ROUTETO 1 /* packets in the state's direction */
|
|
#define PFN_RT_REPLYTO 2 /* packets in the other direction */
|
|
|
|
/*
|
|
* Compact state key — mirrors pf_state_key fields needed by CMM.
|
|
* 40 bytes per key.
|
|
*/
|
|
struct pfn_state_key {
|
|
uint8_t addr[2][16]; /* pf_addr[0], pf_addr[1] */
|
|
uint16_t port[2]; /* network byte order */
|
|
uint8_t af; /* sa_family_t */
|
|
uint8_t proto; /* IPPROTO_* */
|
|
uint8_t pad[2];
|
|
};
|
|
|
|
/*
|
|
* PF state notification event.
|
|
*
|
|
* Carries everything CMM needs to create a cmm_conn and determine
|
|
* offload eligibility without any follow-up ioctl:
|
|
* - Both state keys (wire + stack) for NAT detection
|
|
* - Peer states for TCP ESTABLISHED / UDP bidirectional check
|
|
* - State ID for DELETE matching
|
|
* - The interface the state was created on (a floating state's own
|
|
* interface is "all"), for eligibility and the route check
|
|
* - Its route-to/reply-to target, if any
|
|
*
|
|
* Fixed 160 bytes for ring buffer alignment.
|
|
*/
|
|
struct pfn_event {
|
|
uint8_t type; /* PFN_EVENT_* */
|
|
uint8_t direction; /* PF_IN or PF_OUT */
|
|
uint8_t src_state; /* pf_state_peer src.state */
|
|
uint8_t dst_state; /* pf_state_peer dst.state */
|
|
uint8_t rt; /* PFN_RT_* */
|
|
uint8_t _pad0[3]; /* align id to 8 */
|
|
uint64_t id; /* pf_kstate.id */
|
|
uint32_t creatorid; /* pf_kstate.creatorid */
|
|
uint16_t state_flags; /* pf_kstate.state_flags */
|
|
uint16_t veto; /* PFN_VETO_* */
|
|
char ifname[16]; /* IFNAMSIZ */
|
|
struct pfn_state_key key[2]; /* [0]=wire, [1]=stack */
|
|
char rt_ifname[16]; /* route-to/reply-to interface */
|
|
uint8_t rt_addr[16]; /* and next hop, key[].af family */
|
|
uint8_t _pad2[8]; /* pad to 160 bytes */
|
|
};
|
|
|
|
#ifdef _KERNEL
|
|
_Static_assert(sizeof(struct pfn_event) == 160,
|
|
"pfn_event must be exactly 160 bytes");
|
|
#endif
|
|
|
|
/* --- Counter sync (userspace -> kernel via ioctl) --- */
|
|
|
|
#define PFN_COUNTER_BATCH_MAX 256
|
|
|
|
struct pfn_counter_entry {
|
|
uint64_t id; /* PF state ID */
|
|
uint32_t creatorid; /* PF state creator ID */
|
|
uint32_t pad;
|
|
uint64_t packets[2]; /* delta [0]=forward, [1]=reply */
|
|
uint64_t bytes[2]; /* delta [0]=forward, [1]=reply */
|
|
};
|
|
|
|
struct pfn_counter_update {
|
|
uint32_t count; /* number of entries */
|
|
uint32_t pad;
|
|
const struct pfn_counter_entry *entries; /* userspace pointer */
|
|
};
|
|
|
|
#ifdef _KERNEL
|
|
#include <sys/ioccom.h>
|
|
#endif
|
|
|
|
/* --- Offloaded states (userspace -> kernel via ioctl) --- */
|
|
|
|
/*
|
|
* CMM reports the states whose flow it put into hardware. Only those are
|
|
* made sloppy: their TCP windows go stale while the hardware forwards, and
|
|
* pf must not drop the packets that come back to the software path.
|
|
*/
|
|
struct pfn_state_ref {
|
|
uint64_t id;
|
|
uint32_t creatorid;
|
|
uint32_t pad;
|
|
};
|
|
|
|
struct pfn_offloaded {
|
|
uint32_t count; /* number of refs, <= PFN_COUNTER_BATCH_MAX */
|
|
uint32_t pad;
|
|
const struct pfn_state_ref *refs; /* userspace pointer */
|
|
};
|
|
|
|
#define PFN_IOC_UPDATE_COUNTERS _IOW('N', 1, struct pfn_counter_update)
|
|
#define PFN_IOC_OFFLOADED _IOW('N', 2, struct pfn_offloaded)
|
|
|
|
#endif /* PF_NOTIFY_H */
|