Offload: route-to matching the routing table; the state's own interface
Two problems found while preparing the gateway test. pf_notify reported s->kif as a state's interface. A floating state (OPNsense's default) has kif "all", so cmm's check that the routes leave by the states' interfaces failed for every flow and nothing would have been offloaded. Events now carry orig_kif, the interface the state was created on. OPNsense's automatic rules put route-to on the force-gateway rule, which matches every NATed flow leaving WAN, and reply-to on WAN rules (port forwards). The blanket route veto kept all of it on the software path. pf_notify now passes a route-to/reply-to target in the event (which grows to 160 bytes, the ring to 20 MiB), and cmm offloads such a flow only while the main routing table sends those packets out of the same interface to the same next hop: route-to for the original direction, reply-to for the reply. A route change already tears flows down and re-runs that check. dup-to, a target without an interface, and one in another address family still veto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f1a16265a8
commit
0130ae74d8
4 changed files with 109 additions and 12 deletions
|
|
@ -194,6 +194,20 @@ route_leaves_by(const struct cmm_route *rt, const char *ifname)
|
|||
return (itf != NULL && strcmp(itf->ifname, ifname) == 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Does rt send its packets where a route-to/reply-to would: out of the
|
||||
* interface named ifname, to the next hop addr? cmm_route keeps the
|
||||
* destination itself as the next hop of a directly connected route.
|
||||
*/
|
||||
static int
|
||||
route_sends_to(const struct cmm_route *rt, const char *ifname,
|
||||
const uint8_t *addr)
|
||||
{
|
||||
|
||||
return (route_leaves_by(rt, ifname) &&
|
||||
memcmp(rt->gw, addr, rt->family == AF_INET ? 4 : 16) == 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tell pf_notify which states are now forwarded in hardware: it makes them
|
||||
* sloppy, as their TCP windows stop being tracked.
|
||||
|
|
@ -221,6 +235,8 @@ conn_report_offloaded(struct cmm_global *g, const struct cmm_conn *conn)
|
|||
static int
|
||||
conn_try_offload(struct cmm_global *g, struct cmm_conn *conn)
|
||||
{
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Only a flow pf passed in and out, with nothing in its rules that
|
||||
* only pf_test() can do (pf_notify's veto), goes into hardware.
|
||||
|
|
@ -305,8 +321,8 @@ conn_try_offload(struct cmm_global *g, struct cmm_conn *conn)
|
|||
* The hardware forwards by the main routing table. pf must have sent
|
||||
* the flow the same way: out where the original direction's route
|
||||
* leaves, and back in where the reply's route leaves. Anything else
|
||||
* (route-to, policy routing, a bridge member filtered by pf) stays
|
||||
* on the software path.
|
||||
* (policy routing, a bridge member filtered by pf) stays on the
|
||||
* software path.
|
||||
*/
|
||||
if (!route_leaves_by(conn->orig_route, conn->st[CONN_ST_OUT].ifname) ||
|
||||
!route_leaves_by(conn->rep_route, conn->st[CONN_ST_IN].ifname)) {
|
||||
|
|
@ -316,6 +332,36 @@ conn_try_offload(struct cmm_global *g, struct cmm_conn *conn)
|
|||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* A route-to (packets in the state's direction: the original one) or
|
||||
* reply-to (the reply) is offloaded only while the routing table sends
|
||||
* those packets to the same interface and next hop. OPNsense puts both
|
||||
* on its automatic rules (force gateway, reply-to on WAN). When a
|
||||
* route changes, cmm_route_handle_change() tears the flow down and it
|
||||
* comes back here.
|
||||
*/
|
||||
for (i = 0; i < 2; i++) {
|
||||
const struct cmm_route *rt;
|
||||
char nh[INET6_ADDRSTRLEN];
|
||||
|
||||
if (conn->st[i].rt == PFN_RT_NONE)
|
||||
continue;
|
||||
rt = (conn->st[i].rt == PFN_RT_ROUTETO) ?
|
||||
conn->orig_route : conn->rep_route;
|
||||
if (route_sends_to(rt, conn->st[i].rt_ifname,
|
||||
conn->st[i].rt_addr))
|
||||
continue;
|
||||
if (inet_ntop(conn->af, conn->st[i].rt_addr, nh,
|
||||
sizeof(nh)) == NULL)
|
||||
strlcpy(nh, "?", sizeof(nh));
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"conn: %s (%s %s) differs from the routing table: "
|
||||
"not offloaded",
|
||||
conn->st[i].rt == PFN_RT_ROUTETO ? "route-to" : "reply-to",
|
||||
conn->st[i].rt_ifname, nh);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set input interface for each route: the input for one direction
|
||||
* is the output of the opposite direction. CDX needs this to
|
||||
|
|
@ -741,6 +787,11 @@ handle_pf_ready(struct cmm_global *g, const struct pfn_event *ev)
|
|||
conn->st[slot].seen = 1;
|
||||
strlcpy(conn->st[slot].ifname, ev->ifname,
|
||||
sizeof(conn->st[slot].ifname));
|
||||
conn->st[slot].rt = ev->rt;
|
||||
strlcpy(conn->st[slot].rt_ifname, ev->rt_ifname,
|
||||
sizeof(conn->st[slot].rt_ifname));
|
||||
memcpy(conn->st[slot].rt_addr, ev->rt_addr,
|
||||
sizeof(conn->st[slot].rt_addr));
|
||||
|
||||
if (ev->veto != 0) {
|
||||
conn_veto(g, conn, "pf rules need the software path");
|
||||
|
|
|
|||
|
|
@ -49,14 +49,18 @@ struct cmm_conn {
|
|||
* The flow's pf states: a forwarded flow has one on its way in (on the
|
||||
* ingress interface) and one on its way out (on the egress interface,
|
||||
* with the NAT). Offload needs both, neither vetoed, and the egress
|
||||
* the main routing table gives; a DELETE of either removes it.
|
||||
* the main routing table gives, also for a route-to/reply-to; a
|
||||
* DELETE of either removes it.
|
||||
*/
|
||||
struct {
|
||||
uint64_t id;
|
||||
uint32_t creatorid;
|
||||
uint16_t veto; /* PFN_VETO_* */
|
||||
uint8_t seen;
|
||||
uint8_t rt; /* PFN_RT_* */
|
||||
char ifname[IFNAMSIZ];
|
||||
char rt_ifname[IFNAMSIZ];
|
||||
uint8_t rt_addr[16]; /* route-to/reply-to next hop */
|
||||
} st[2]; /* CONN_ST_IN, CONN_ST_OUT */
|
||||
|
||||
/* Offload state */
|
||||
|
|
|
|||
|
|
@ -167,8 +167,24 @@ pfn_veto(const struct pf_kstate *s)
|
|||
{
|
||||
uint16_t veto = 0;
|
||||
|
||||
if (s->act.rt != PF_NOPFROUTE || s->act.rt_kif != NULL)
|
||||
switch (s->act.rt) {
|
||||
case PF_NOPFROUTE:
|
||||
if (s->act.rt_kif != NULL)
|
||||
veto |= PFN_VETO_ROUTE;
|
||||
break;
|
||||
case PF_ROUTETO:
|
||||
case PF_REPLYTO:
|
||||
/* CMM compares the target with the routing table (pfn_event.rt);
|
||||
one it cannot compare stays on the software path */
|
||||
if (s->act.rt_kif == NULL ||
|
||||
s->act.rt_af != s->key[PF_SK_WIRE]->af ||
|
||||
s->act.rt_af != s->key[PF_SK_STACK]->af)
|
||||
veto |= PFN_VETO_ROUTE;
|
||||
break;
|
||||
default: /* dup-to: pf copies every packet */
|
||||
veto |= PFN_VETO_ROUTE;
|
||||
break;
|
||||
}
|
||||
if (s->act.rtableid >= 0)
|
||||
veto |= PFN_VETO_RTABLE;
|
||||
if (s->act.dnpipe != 0 || s->act.dnrpipe != 0 ||
|
||||
|
|
@ -188,6 +204,7 @@ pfn_veto(const struct pf_kstate *s)
|
|||
static void
|
||||
pfn_fill_event(struct pfn_event *ev, uint8_t type, struct pf_kstate *s)
|
||||
{
|
||||
struct pfi_kkif *kif;
|
||||
|
||||
memset(ev, 0, sizeof(*ev));
|
||||
ev->type = type;
|
||||
|
|
@ -198,7 +215,17 @@ pfn_fill_event(struct pfn_event *ev, uint8_t type, struct pf_kstate *s)
|
|||
ev->creatorid = s->creatorid;
|
||||
ev->state_flags = s->state_flags;
|
||||
ev->veto = pfn_veto(s);
|
||||
strlcpy(ev->ifname, s->kif->pfik_name, sizeof(ev->ifname));
|
||||
/* A floating state's kif is "all": orig_kif is where it was created */
|
||||
kif = (s->orig_kif != NULL) ? s->orig_kif : s->kif;
|
||||
strlcpy(ev->ifname, kif->pfik_name, sizeof(ev->ifname));
|
||||
if (!(ev->veto & PFN_VETO_ROUTE) &&
|
||||
(s->act.rt == PF_ROUTETO || s->act.rt == PF_REPLYTO)) {
|
||||
ev->rt = (s->act.rt == PF_ROUTETO) ?
|
||||
PFN_RT_ROUTETO : PFN_RT_REPLYTO;
|
||||
strlcpy(ev->rt_ifname, s->act.rt_kif->pfik_name,
|
||||
sizeof(ev->rt_ifname));
|
||||
memcpy(ev->rt_addr, &s->act.rt_addr, sizeof(ev->rt_addr));
|
||||
}
|
||||
|
||||
/* Wire key (PF_SK_WIRE = 0) */
|
||||
memcpy(ev->key[0].addr[0], &s->key[PF_SK_WIRE]->addr[0],
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@
|
|||
* 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 /* route-to, reply-to, dup-to */
|
||||
#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 */
|
||||
|
|
@ -40,6 +41,15 @@
|
|||
#define PFN_VETO_NORMALIZE 0x0020 /* scrub, set-tos, set-prio, max-mss,
|
||||
min-ttl, max-pkt-size */
|
||||
|
||||
/*
|
||||
* 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.
|
||||
|
|
@ -60,28 +70,33 @@ struct pfn_state_key {
|
|||
* - Both state keys (wire + stack) for NAT detection
|
||||
* - Peer states for TCP ESTABLISHED / UDP bidirectional check
|
||||
* - State ID for DELETE matching
|
||||
* - Interface name for eligibility filtering
|
||||
* - 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 128 bytes for ring buffer alignment.
|
||||
* 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 _pad0[4]; /* align id to 8 */
|
||||
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 */
|
||||
uint8_t _pad2[8]; /* pad to 128 bytes */
|
||||
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) == 128,
|
||||
"pfn_event must be exactly 128 bytes");
|
||||
_Static_assert(sizeof(struct pfn_event) == 160,
|
||||
"pfn_event must be exactly 160 bytes");
|
||||
#endif
|
||||
|
||||
/* --- Counter sync (userspace -> kernel via ioctl) --- */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue