ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ipsec_db.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 ipsec_db.h
6 * @brief IPsec Security Policy Database (SPD) + Security Association Database (SAD) - RFC 4301.
7 *
8 * The ESP datapath (esp.h) is the crypto transform. This file is the two databases that decide, for a
9 * given packet, WHETHER and WITH WHICH SA to apply it. Both are pure, host-testable data structures with
10 * no heap and no lwIP dependency - the remaining device-side piece is only the IP input/output hook that
11 * feeds packets through these lookups.
12 *
13 * - SPD (RFC 4301 §4.4.1): an ordered list of policies matched against a packet's selectors (source /
14 * destination address ranges, protocol, port ranges). The FIRST matching policy wins and names an
15 * action - PROTECT (apply ESP with a bound SA), BYPASS (send in the clear), or DISCARD (drop).
16 * - SAD (RFC 4301 §4.4.2): the active Security Associations keyed by SPI. An inbound ESP packet is
17 * demuxed to its SA by SPI; an outbound PROTECT policy names the SA to encapsulate with. Each SA
18 * carries its key / salt, its outbound sequence counter, and its inbound anti-replay window.
19 *
20 * Selectors are value types (addresses stored inline, big-endian) so the databases persist independently
21 * of any wire buffer. @ref protocore_ipsec_selector_from_ts bridges an IKEv2-negotiated TSi/TSr pair (the
22 * traffic selectors carried in ikev2.h) into an SPD selector, per RFC 4301 §4.4.1.
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef PROTOCORE_IPSEC_DB_H
29#define PROTOCORE_IPSEC_DB_H
30
31#include "protocore_config.h" // the entry point: protocore_types.h for the widths
32
33#if PROTOCORE_ENABLE_IKEV2
34
35#include "services/security/ikev2/ikev2/ikev2.h" // the complete type a public struct below holds by value
36#include "services/system/esp/esp/esp.h" // the complete type a public struct below holds by value
37
38EMBED_BEGIN_DECLS
39
40// This module holds nothing between calls, so it carves no borrow and states none. An entry
41// takes one all the same, and never reads it, so every namespace in the tree is invoked the
42// same way.
43
44/** @brief Longest selector address (IPv6). IPv4 uses the low 4 bytes. */
45#define PROTOCORE_IPSEC_ADDR_MAX 16
46
47/** @brief Maximum policies in one SPD. */
48#define PROTOCORE_IPSEC_SPD_MAX 8
49
50/** @brief Maximum Security Associations in one SAD. */
51#define PROTOCORE_IPSEC_SAD_MAX 8
52
53/** @brief SPD policy action (RFC 4301 §4.4.1). */
54EMBED_ENUM_BYTE(IpsecAction,
55 IPSEC_ACTION_DISCARD = 0, ///< drop the packet
56 IPSEC_ACTION_BYPASS = 1, ///< forward without IPsec
57 IPSEC_ACTION_PROTECT = 2, ///< apply ESP with the bound SA
58);
59
60/**
61 * @brief A traffic selector as an SPD range (value type, addresses inline big-endian).
62 *
63 * A packet matches when its family and protocol agree and its source / destination addresses and ports
64 * each fall within the inclusive [lo, hi] ranges. A protocol of 0 or a port range of [0, 65535] is "any".
65 */
66typedef struct
67{
68 uint8_t addr_len; ///< 4 (IPv4) or 16 (IPv6); also selects the family
69 uint8_t ip_protocol; ///< 0 = any
70 uint8_t src_lo[PROTOCORE_IPSEC_ADDR_MAX];
71 uint8_t src_hi[PROTOCORE_IPSEC_ADDR_MAX];
72 uint8_t dst_lo[PROTOCORE_IPSEC_ADDR_MAX];
73 uint8_t dst_hi[PROTOCORE_IPSEC_ADDR_MAX];
74 uint16_t src_port_lo;
75 uint16_t src_port_hi;
76 uint16_t dst_port_lo;
77 uint16_t dst_port_hi;
78} IpsecSelector;
79
80/** @brief One SPD policy: a selector, its action, and (for PROTECT) the outbound SA's SPI. */
81typedef struct
82{
83 IpsecSelector sel;
84 IpsecAction action;
85 uint32_t sa_spi; ///< PROTECT: the SAD entry to encapsulate with (0 = not yet bound)
86} IpsecPolicy;
87
88/** @brief An ordered Security Policy Database (first match wins). */
89typedef struct
90{
91 IpsecPolicy entries[PROTOCORE_IPSEC_SPD_MAX];
92 size_t count;
93} IpsecSpd;
94
95/** @brief A concrete packet's 5-tuple, looked up against the SPD. Addresses point at big-endian octets. */
96typedef struct
97{
98 uint8_t addr_len; ///< 4 or 16 (must match the selector family)
99 uint8_t ip_protocol;
100 const uint8_t *src; ///< @ref addr_len octets
101 const uint8_t *dst; ///< @ref addr_len octets
102 uint16_t src_port;
103 uint16_t dst_port;
104} IpsecFlow;
105
106/** @brief One Security Association (RFC 4301 §4.4.2). */
107typedef struct
108{
109 uint32_t spi; ///< the SA's SPI (its SAD key)
110 uint8_t dst[PROTOCORE_IPSEC_ADDR_MAX]; ///< SA destination address
111 uint8_t addr_len; ///< 4 or 16
112 uint8_t key[PROTOCORE_ESP_KEY_LEN]; ///< AES-256 key (SK_ei / SK_er without salt)
113 uint8_t salt[PROTOCORE_ESP_SALT_LEN]; ///< AES-GCM salt (the key's tail)
114 uint32_t seq; ///< outbound: last sequence number issued (0 = none yet)
115 EspReplay replay; ///< inbound: anti-replay window
116 embed_bool inbound; ///< true = receive SA, false = send SA
117 embed_bool valid; ///< false = free slot
118} IpsecSaEntry;
119
120/** @brief The active Security Association Database, keyed by SPI. */
121typedef struct
122{
123 IpsecSaEntry entries[PROTOCORE_IPSEC_SAD_MAX];
124 size_t count;
125} IpsecSad;
126
127/** @brief What protocore_ipsec_spd_init takes: spd. */
128typedef struct
129{
130 IpsecSpd *spd;
131} IpsecDbProtocoreIpsecSpdInitArgs;
132
133/** @brief What protocore_ipsec_spd_add takes: spd, sel, action, sa_spi. */
134typedef struct
135{
136 IpsecSpd *spd;
137 const IpsecSelector *sel;
138 IpsecAction action;
139 uint32_t sa_spi; ///< for a PROTECT action, the SAD SPI to bind (ignored otherwise)
140} IpsecDbProtocoreIpsecSpdAddArgs;
141
142/** @brief What protocore_ipsec_spd_lookup takes: spd, flow. */
143typedef struct
144{
145 const IpsecSpd *spd;
146 const IpsecFlow *flow;
147} IpsecDbProtocoreIpsecSpdLookupArgs;
148
149/** @brief What protocore_ipsec_selector_match takes: sel, flow. */
150typedef struct
151{
152 const IpsecSelector *sel;
153 const IpsecFlow *flow;
154} IpsecDbProtocoreIpsecSelectorMatchArgs;
155
156/** @brief What protocore_ipsec_selector_from_ts takes: out, ts_src, ... */
157typedef struct
158{
159 IpsecSelector *out;
160 const IkeTrafficSelector *ts_src;
161 const IkeTrafficSelector *ts_dst;
162} IpsecDbProtocoreIpsecSelectorFromTsArgs;
163
164/** @brief What protocore_ipsec_sad_init takes: sad. */
165typedef struct
166{
167 IpsecSad *sad;
168} IpsecDbProtocoreIpsecSadInitArgs;
169
170/** @brief What protocore_ipsec_sad_add takes: sad, spi, dst, ... */
171typedef struct
172{
173 IpsecSad *sad;
174 uint32_t spi;
175 const uint8_t *dst;
176 uint8_t addr_len;
177 const uint8_t *key; ///< PROTOCORE_ESP_KEY_LEN bytes.
178 const uint8_t *salt; ///< PROTOCORE_ESP_SALT_LEN bytes.
179 embed_bool inbound;
180} IpsecDbProtocoreIpsecSadAddArgs;
181
182/** @brief What protocore_ipsec_sad_find takes: sad, spi. */
183typedef struct
184{
185 IpsecSad *sad;
186 uint32_t spi;
187} IpsecDbProtocoreIpsecSadFindArgs;
188
189/** @brief What protocore_ipsec_sad_remove takes: sad, spi. */
190typedef struct
191{
192 IpsecSad *sad;
193 uint32_t spi;
194} IpsecDbProtocoreIpsecSadRemoveArgs;
195
196/** @brief What protocore_ipsec_sad_next_seq takes: sa, seq_out. */
197typedef struct
198{
199 IpsecSaEntry *sa;
200 uint32_t *seq_out; ///< receives the sequence number to place in the packet
201} IpsecDbProtocoreIpsecSadNextSeqArgs;
202
203/**
204 * @brief IPsec Security Policy Database (SPD) + Security Association Database (SAD) - RFC 4301.
205 *
206 * A caller sets the members a call takes, invokes it through ::IpsecDb with the bytes it runs
207 * out of, and reads the outcome off the same handle.
208 *
209 * IpsecDb.protocore_ipsec_spd_init_args.spd = ...;
210 * IpsecDb.protocore_ipsec_spd_init(work);
211 *
212 * @var IpsecDbNs::protocore_ipsec_spd_init_args what protocore_ipsec_spd_init takes: spd
213 * @var IpsecDbNs::protocore_ipsec_spd_add_args what protocore_ipsec_spd_add takes: spd, sel, action, sa_spi
214 * @var IpsecDbNs::protocore_ipsec_spd_lookup_args what protocore_ipsec_spd_lookup takes: spd, flow
215 * @var IpsecDbNs::protocore_ipsec_selector_match_args what protocore_ipsec_selector_match takes: sel, flow
216 * @var IpsecDbNs::protocore_ipsec_selector_from_ts_args what protocore_ipsec_selector_from_ts takes: out, ts_src,
217 * @var IpsecDbNs::protocore_ipsec_sad_init_args what protocore_ipsec_sad_init takes: sad
218 * @var IpsecDbNs::protocore_ipsec_sad_add_args what protocore_ipsec_sad_add takes: sad, spi, dst,
219 * @var IpsecDbNs::protocore_ipsec_sad_find_args what protocore_ipsec_sad_find takes: sad, spi
220 * @var IpsecDbNs::protocore_ipsec_sad_remove_args what protocore_ipsec_sad_remove takes: sad, spi
221 * @var IpsecDbNs::protocore_ipsec_sad_next_seq_args what protocore_ipsec_sad_next_seq takes: sa, seq_out
222 * @var IpsecDbNs::ok true on success, false if spd is full or an argument is null
223 * @var IpsecDbNs::ptr the matching policy, or nullptr if none matches (the caller drops, ...
224 * @var IpsecDbNs::sa the SAD slot a find matched or an add filled, or nullptr
225 * @var IpsecDbNs::protocore_ipsec_spd_init empty an SPD (no policies)
226 * @var IpsecDbNs::protocore_ipsec_spd_add append a policy to the SPD (order is significant - first match wins ...
227 * @var IpsecDbNs::protocore_ipsec_spd_lookup find the first SPD policy whose selector matches flow (RFC 4301 ...
228 * @var IpsecDbNs::protocore_ipsec_selector_match true iff flow falls inside sel (family, protocol, address ranges, ...
229 * @var IpsecDbNs::protocore_ipsec_selector_from_ts fill out from an IKEv2-negotiated TSi / TSr pair (RFC 4301 §4.4.1
230 * ...
231 * @var IpsecDbNs::protocore_ipsec_sad_init empty a SAD (no SAs)
232 * @var IpsecDbNs::protocore_ipsec_sad_add install a Security Association keyed by spi. An inbound SA's ...
233 * @var IpsecDbNs::protocore_ipsec_sad_find look up a valid SA by SPI (inbound ESP demux, RFC 4301 §4.1). ...
234 * @var IpsecDbNs::protocore_ipsec_sad_remove remove the SA with spi (e.g. on an IKE DELETE). true if one was ...
235 * @var IpsecDbNs::protocore_ipsec_sad_next_seq allocate the next outbound sequence number for sa (RFC 4303 §3.3.3, ...
236 *
237 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
238 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
239 * a caller drives every namespace the same way.
240 */
241typedef struct
242{
243 IpsecDbProtocoreIpsecSpdInitArgs protocore_ipsec_spd_init_args;
244 IpsecDbProtocoreIpsecSpdAddArgs protocore_ipsec_spd_add_args;
245 IpsecDbProtocoreIpsecSpdLookupArgs protocore_ipsec_spd_lookup_args;
246 IpsecDbProtocoreIpsecSelectorMatchArgs protocore_ipsec_selector_match_args;
247 IpsecDbProtocoreIpsecSelectorFromTsArgs protocore_ipsec_selector_from_ts_args;
248 IpsecDbProtocoreIpsecSadInitArgs protocore_ipsec_sad_init_args;
249 IpsecDbProtocoreIpsecSadAddArgs protocore_ipsec_sad_add_args;
250 IpsecDbProtocoreIpsecSadFindArgs protocore_ipsec_sad_find_args;
251 IpsecDbProtocoreIpsecSadRemoveArgs protocore_ipsec_sad_remove_args;
252 IpsecDbProtocoreIpsecSadNextSeqArgs protocore_ipsec_sad_next_seq_args;
253 embed_bool ok;
254 const IpsecPolicy *ptr;
255 IpsecSaEntry *sa;
256} IpsecDbVars;
257
258/** @brief The operands and the outcome. */
259extern IpsecDbVars IpsecDbV;
260
261/** @brief The entries. */
262typedef struct
263{
264 void (*const protocore_ipsec_spd_init)(const uint8_t *work);
265 void (*const protocore_ipsec_spd_add)(const uint8_t *work);
266 void (*const protocore_ipsec_spd_lookup)(const uint8_t *work);
267 void (*const protocore_ipsec_selector_match)(const uint8_t *work);
268 void (*const protocore_ipsec_selector_from_ts)(const uint8_t *work);
269 void (*const protocore_ipsec_sad_init)(const uint8_t *work);
270 void (*const protocore_ipsec_sad_add)(const uint8_t *work);
271 void (*const protocore_ipsec_sad_find)(const uint8_t *work);
272 void (*const protocore_ipsec_sad_remove)(const uint8_t *work);
273 void (*const protocore_ipsec_sad_next_seq)(const uint8_t *work);
274} IpsecDbNs;
275
276// What the table binds, defined once in the .c and taking one parameter each: everything
277// else an entry needs is an operand in IpsecDbV or a region of the borrow at a fixed offset.
278void protocore_ipsec_db_protocore_ipsec_spd_init(const uint8_t *work);
279void protocore_ipsec_db_protocore_ipsec_spd_add(const uint8_t *work);
280void protocore_ipsec_db_protocore_ipsec_spd_lookup(const uint8_t *work);
281void protocore_ipsec_db_protocore_ipsec_selector_match(const uint8_t *work);
282void protocore_ipsec_db_protocore_ipsec_selector_from_ts(const uint8_t *work);
283void protocore_ipsec_db_protocore_ipsec_sad_init(const uint8_t *work);
284void protocore_ipsec_db_protocore_ipsec_sad_add(const uint8_t *work);
285void protocore_ipsec_db_protocore_ipsec_sad_find(const uint8_t *work);
286void protocore_ipsec_db_protocore_ipsec_sad_remove(const uint8_t *work);
287void protocore_ipsec_db_protocore_ipsec_sad_next_seq(const uint8_t *work);
288
289// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
290// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
291// `IpsecDb.protocore_ipsec_spd_init(work)` resolves to a named function and becomes a DIRECT call. An extern table
292// leaves the call indirect and the symbol live at every level, -O2 -flto included.
293static const IpsecDbNs IpsecDb __attribute__((unused)) = {
294 .protocore_ipsec_spd_init = protocore_ipsec_db_protocore_ipsec_spd_init,
295 .protocore_ipsec_spd_add = protocore_ipsec_db_protocore_ipsec_spd_add,
296 .protocore_ipsec_spd_lookup = protocore_ipsec_db_protocore_ipsec_spd_lookup,
297 .protocore_ipsec_selector_match = protocore_ipsec_db_protocore_ipsec_selector_match,
298 .protocore_ipsec_selector_from_ts = protocore_ipsec_db_protocore_ipsec_selector_from_ts,
299 .protocore_ipsec_sad_init = protocore_ipsec_db_protocore_ipsec_sad_init,
300 .protocore_ipsec_sad_add = protocore_ipsec_db_protocore_ipsec_sad_add,
301 .protocore_ipsec_sad_find = protocore_ipsec_db_protocore_ipsec_sad_find,
302 .protocore_ipsec_sad_remove = protocore_ipsec_db_protocore_ipsec_sad_remove,
303 .protocore_ipsec_sad_next_seq = protocore_ipsec_db_protocore_ipsec_sad_next_seq,
304};
305
306EMBED_END_DECLS
307
308#endif // PROTOCORE_ENABLE_IKEV2
309
310#endif // PROTOCORE_IPSEC_DB_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.
ESP (RFC 4303) packet transform with AES-256-GCM (RFC 4106) - the IPsec datapath's crypto core.
#define PROTOCORE_ESP_SALT_LEN
Implicit salt length (the tail of the ESP key, not on the wire).
Definition esp.h:47
#define PROTOCORE_ESP_KEY_LEN
AES-256 key length.
Definition esp.h:53
IKEv2 (RFC 7296): the message and payload codec, the key schedule, and the handshake driver.
Anti-replay sliding-window state for one inbound SA (zero-heap).
Definition esp.h:60