ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
forward.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file forward.h
6 * @brief Layer 3 (Network) - the interface forwarding plane (PROTOCORE_ENABLE_FORWARD).
7 *
8 * RFC 1812 sec 5 "INTERNET LAYER - FORWARDING". A frame that arrives on one interface runs the
9 * forwarding walk-through (sec 5.2) and leaves on every interface it is permitted to leave on. The
10 * steps this plane runs over opaque bytes are the access list (sec 5.3.9 "Packet Filtering and
11 * Access Lists"), the next hop (sec 5.2.4.3 "Next Hop Address"), the controls on forwarding
12 * (sec 5.3.11 "Controls on Forwarding"), and the drop under load (sec 5.3.6 "Congestion Control").
13 *
14 * The interfaces are layer 1's, over PROTOCORE_PHY_MAX_IFACES rows; this plane reads that registry
15 * and calls its send. A (src, dst) pair forwards when an ALLOW control matches and no DENY does; a
16 * frame never leaves on the interface it arrived on. An exceeded rate cap, or an interface that
17 * refuses the bytes, drops the frame for that next hop and counts it. Storage is static:
18 * PROTOCORE_FWD_MAX_RULES controls, PROTOCORE_FWD_MAX_ACL access-list entries,
19 * PROTOCORE_FWD_MAX_ROUTES policy routes.
20 *
21 * A policy route matches a frame by the same byte pattern the access list uses - any field at a
22 * known offset, an EtherType, an IP protocol, a port, an address prefix - and binds it to one
23 * egress interface ahead of the (src, dst) fan-out; the first matching route wins. The access list
24 * runs first, and the rate cap, the never-reflect step and the counted drop apply to the chosen
25 * next hop.
26 *
27 * The inspection hook (PROTOCORE_FWD_INSPECT) runs an application callback on every frame after the
28 * access list and before the route lookup. The callback returns a verdict that passes or drops it.
29 *
30 * The rate cap reads Clock.millis (server/clock/clock.h), the library's one time source; Clock.set_ms
31 * moves the window for every module at once.
32 *
33 * @author Douglas Quigg (dstroy0)
34 * @date 2026
35 */
36
37#ifndef PROTOCORE_FORWARD_H
38#define PROTOCORE_FORWARD_H
39
41
42#include "protocore_config.h"
43// An interface is a physical thing: its id, its kind, and how bytes reach the wire all live at L1.
44// This plane picks the interface a frame leaves on and asks L1 to put it there.
45
46EMBED_BEGIN_DECLS
47
48#if PROTOCORE_ENABLE_FORWARD
49
50/**
51 * @brief What an access-list entry or a control on forwarding does to a frame.
52 * RFC 1812 sec 5.3.9, sec 5.3.11.
53 */
54EMBED_ENUM_BYTE(protocore_fwd_action, PROTOCORE_FWD_DENY = 0, PROTOCORE_FWD_ALLOW = 1, );
55
56/** @brief Wildcard source interface: an entry carrying it matches a frame from any interface. */
57#define PROTOCORE_FWD_IF_ANY 0xFF
58
59/** @brief Forwarding counters, monotonic since the last reset. */
60typedef struct
61{
62 uint32_t frames_in; ///< frames offered to the forwarding algorithm (RFC 1812 sec 5.2.1)
63 uint32_t forwarded; ///< next hops that took the frame (RFC 1812 sec 5.2.4.3)
64 uint32_t blocked; ///< next hops refused by a DENY or by default-deny (RFC 1812 sec 5.3.11)
65 uint32_t rate_dropped; ///< frames dropped at a rate cap (RFC 1812 sec 5.3.6)
66 uint32_t send_fail; ///< next hops whose interface refused the bytes
67 uint32_t acl_denied; ///< frames dropped by the access list (RFC 1812 sec 5.3.9)
68 uint32_t policy_routed; ///< frames a policy route bound to one next hop
69 uint32_t inspect_dropped; ///< frames dropped by the inspection hook (PROTOCORE_FWD_INSPECT)
70} protocore_forward_stats;
71
72#if PROTOCORE_FWD_INSPECT
73/** @brief The verdict an inspection hook returns for a frame. */
74EMBED_ENUM_BYTE(protocore_fwd_verdict,
75 PROTOCORE_FWD_INSPECT_PASS = 0, ///< the frame continues to the route lookup and the fan-out
76 PROTOCORE_FWD_INSPECT_DROP = 1, ///< the frame is dropped and counted as inspect_dropped
77);
78
79/**
80 * @brief Ingress inspection hook: reads @p len bytes at @p data arriving on @p src_if and returns a
81 * ::protocore_fwd_verdict. Runs after the access list and before the route lookup.
82 */
83typedef protocore_fwd_verdict (*protocore_fwd_inspect_fn)(uint8_t src_if, const uint8_t *data, uint16_t len, void *ctx);
84#endif
85
86/** @brief One control on forwarding: the destination of a (src, dst) pair. RFC 1812 sec 5.3.11. */
87typedef struct
88{
89 uint8_t dst_if; ///< the destination interface the pair names
90 protocore_fwd_action action; ///< ALLOW forwards the pair, DENY blocks it
91 uint16_t rate_cap_per_sec; ///< frames per second the pair takes; 0 is uncapped (RFC 1812 sec 5.3.6)
92} FwdRuleArgs;
93
94/**
95 * @brief The byte pattern an access-list entry or a policy route matches on: @c patlen bytes at
96 * @c offset, compared under @c mask. RFC 1812 sec 5.3.9.
97 */
98typedef struct
99{
100 const uint8_t *pattern; ///< the bytes to match, read only during the call that stores them
101 const uint8_t *mask; ///< the bits of each pattern byte that are compared
102 uint16_t offset; ///< byte offset into the frame where the pattern starts
103 uint8_t patlen; ///< pattern length, up to PROTOCORE_FWD_ACL_PATLEN; 0 matches any content
104} FwdMatchArgs;
105
106/** @brief The access list's two verdicts: one entry's, and the one a frame matching none takes. */
107typedef struct
108{
109 protocore_fwd_action action; ///< what the entry being added does to a matching frame
110 protocore_fwd_action fallback; ///< what a frame matching no entry takes
111} FwdAclArgs;
112
113/** @brief The next hop a policy route binds a matched frame to. RFC 1812 sec 5.2.4.3. */
114typedef struct
115{
116 uint8_t egress_if; ///< the one interface a matched frame leaves on
117 uint16_t rate_cap_per_sec; ///< frames per second to that next hop; 0 is uncapped
118} FwdRouteArgs;
119
120/** @brief The received frame the forwarding algorithm runs on. RFC 1812 sec 5.2.1. */
121typedef struct
122{
123 const uint8_t *data; ///< the frame bytes, valid for the duration of the call
124 uint16_t len; ///< how many of them there are
125} FwdFrameArgs;
126
127#if PROTOCORE_FWD_INSPECT
128/** @brief The inspection hook and the pointer it is handed back. */
129typedef struct
130{
131 protocore_fwd_inspect_fn fn; ///< what runs on every frame; NULL leaves the hook off
132 void *ctx; ///< passed to @c fn unread by this module
133} FwdInspectArgs;
134#endif
135
136/** @brief The plane's own tables and the calls that reach them, described only in forward.c. */
137
138/**
139 * @brief The interface forwarding plane. RFC 1812 sec 5.2 "FORWARDING WALK-THROUGH".
140 *
141 * A caller sets the members a call takes, invokes it through ::Forward, and reads the outcome off
142 * the same handle. The tables are behind @ref internal.
143 *
144 * @var ForwardNs::src_if the interface a frame arrives on, and the one an entry scopes to;
145 * PROTOCORE_FWD_IF_ANY on an entry matches every interface
146 * @var ForwardNs::rule the destination of a (src, dst) pair (RFC 1812 sec 5.3.11)
147 * @var ForwardNs::match the byte pattern an entry or a route matches on (RFC 1812 sec 5.3.9)
148 * @var ForwardNs::acl the access list's entry verdict and its fallback (RFC 1812 sec 5.3.9)
149 * @var ForwardNs::route the next hop a policy route binds to (RFC 1812 sec 5.2.4.3)
150 * @var ForwardNs::frame the received frame the forwarding algorithm runs on
151 * @var ForwardNs::inspect the inspection hook and the pointer it is handed back
152 * @var ForwardNs::ok a call's true/false outcome
153 * @var ForwardNs::n next hops a frame reached
154 * @var ForwardNs::stats the counters a read reports
155 * @var ForwardNs::reset empty every control, entry and route, and zero the counters
156 * @var ForwardNs::add_rule add a control on forwarding for one (src, dst) pair
157 * @var ForwardNs::acl_set_default set what a frame matching no access-list entry takes
158 * @var ForwardNs::acl_add add an access-list entry; the first match decides
159 * @var ForwardNs::route_add add a policy route, taken ahead of the (src, dst) fan-out
160 * @var ForwardNs::set_inspector install the ingress inspection hook
161 * @var ForwardNs::ingress run one received frame through the forwarding walk-through
162 * @var ForwardNs::get_stats copy the counters onto the handle
163 */
164typedef struct
165{
166 uint8_t src_if; ///< the source interface a forwarding call names
167 FwdRuleArgs rule; ///< the (src, dst) pair a control governs
168 FwdMatchArgs match; ///< the byte pattern an entry or a route matches on
169 FwdAclArgs acl; ///< the access list's verdicts
170 FwdRouteArgs route; ///< the next hop a policy route binds to
171 FwdFrameArgs frame; ///< the frame the forwarding algorithm runs on
172#if PROTOCORE_FWD_INSPECT
173 FwdInspectArgs inspect; ///< the ingress inspection hook
174#endif
175 embed_bool ok;
176 uint8_t n;
177 protocore_forward_stats stats;
178#if PROTOCORE_FWD_INSPECT
179#endif
180} ForwardVars;
181
182/** @brief The operands and the outcome. */
183extern ForwardVars ForwardV;
184
185/** @brief The entries. */
186typedef struct
187{
188 void (*const reset)(uint8_t *work);
189 void (*const add_rule)(uint8_t *work);
190 void (*const acl_set_default)(uint8_t *work);
191 void (*const acl_add)(uint8_t *work);
192 void (*const route_add)(uint8_t *work);
193 void (*const set_inspector)(uint8_t *work);
194 void (*const ingress)(uint8_t *work);
195 void (*const get_stats)(uint8_t *work);
196} ForwardNs;
197
198// What the table binds, defined once in the .c and taking one parameter each: everything
199// else an entry needs is an operand in ForwardV or a region of the borrow at a fixed offset.
200void protocore_forward_reset(uint8_t *work);
201void protocore_forward_add_rule(uint8_t *work);
202void protocore_forward_acl_set_default(uint8_t *work);
203void protocore_forward_acl_add(uint8_t *work);
204void protocore_forward_route_add(uint8_t *work);
205#if PROTOCORE_FWD_INSPECT
206void protocore_forward_set_inspector(uint8_t *work);
207#endif
208void protocore_forward_ingress(uint8_t *work);
209void protocore_forward_get_stats(uint8_t *work);
210
211// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
212// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
213// `Forward.reset(work)` resolves to a named function and becomes a DIRECT call. An extern table
214// leaves the call indirect and the symbol live at every level, -O2 -flto included.
215static const ForwardNs Forward __attribute__((unused)) = {
216 .reset = protocore_forward_reset,
217 .add_rule = protocore_forward_add_rule,
218 .acl_set_default = protocore_forward_acl_set_default,
219 .acl_add = protocore_forward_acl_add,
220 .route_add = protocore_forward_route_add,
221#if PROTOCORE_FWD_INSPECT
222 .set_inspector = protocore_forward_set_inspector,
223#endif
224 .ingress = protocore_forward_ingress,
225 .get_stats = protocore_forward_get_stats,
226};
227
228/**
229 * @brief The PROTOCORE_FORWARD_BORROW bytes this module's state lives in.
230 *
231 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
232 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
233 * walks, so the state lasts the life of the program.
234 *
235 * @return the span.
236 */
237uint8_t *protocore_forward_span(void);
238
239#endif // PROTOCORE_ENABLE_FORWARD
240
241EMBED_END_DECLS
242
243#endif // PROTOCORE_FORWARD_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.