Debian Codegraph @

test/rist/test_eap_authenticator_restart.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Regression for the EAP-SRP re-authentication lockout after an
5 * *authenticator* (listener/hub) restart.
6 *
7 * process_eap_request_identity() refused every EAP_REQUEST_IDENTITY once the
8 * authenticatee reached EAP_AUTH_STATE_SUCCESS (hardening against a forged
9 * single-packet reset). That is correct for a lone forged
10 * packet, but it also permanently locks out the *legitimate* case: when the
11 * authenticator restarts it loses all session state, treats the caller as a
12 * brand-new peer and drives a fresh handshake by re-sending IDENTITY. A caller
13 * still parked in SUCCESS refused those forever, so the hub sat in "Waiting for
14 * EAP authentication to happen" and the data plane carried 0 packets until an
15 * operator restarted the caller.
16 *
17 * This is distinct from the caller *socket* rebind (test_caller_socket_rebind):
18 * that recovers only from listener *silence* (NAT rebind / dead socket). Here
19 * the restarted authenticator is NOT silent -- it actively re-challenges -- so
20 * the silence rebind never fires. We prove that by pinning a long
21 * session_timeout so the rebind path is disabled for the whole test; recovery
22 * can therefore only come from the identity-request re-auth path under test.
23 *
24 * Topology (single hop, in-process):
25 * sender(listener/authenticator, 127.0.0.1:PORT) <-- receiver(caller/authenticatee)
26 *
27 * Sequence:
28 * 1. Spin up authenticator + authenticatee, wait for the SRP handshake.
29 * 2. Push a data burst and confirm the caller decodes it.
30 * 3. Destroy the authenticator and immediately recreate it on the same port
31 * (a firmware upgrade / service restart). The caller never goes silent.
32 * 4. Wait for the fresh authenticator to re-challenge and the caller to
33 * re-authenticate on its own.
34 * 5. Push another burst and confirm it flows.
35 *
36 * PASS when post-restart payload flows AND rebind_attempts == 0 (recovery came
37 * from the identity re-auth path, not the silence rebind). Before the fix the
38 * caller stays locked in SUCCESS, no data flows, and the test fails.
39 *
40 * Exit codes:
41 * 0 - caller re-authenticated against the restarted authenticator, data flowed
42 * 1 - caller never recovered (locked out) / no data after restart
43 * 2 - setup never reached an initial handshake / data path
44 * 99 - setup error
45 */
46
47 #include "librist/librist.h"
48 #include "librist/librist_srp.h"
49 #include "rist-private.h"
50 #include <inttypes.h>
51 #include <stdbool.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #ifdef _WIN32
56 #include <windows.h>
57 #else
58 #include <unistd.h>
59 #endif
60
61 #define SRP_USER "restartuser"
62 #define SRP_PASS "restartpass-9137"
63
64 /* Long enough that try_caller_socket_rebind (needs silence beyond
65 * max(session_timeout, 4*keepalive)) cannot fire during this test, so recovery
66 * is attributable solely to the identity-request re-auth path. */
67 #define SESSION_TIMEOUT_MS 30000
68 #define KEEPALIVE_MS 500
69
70 static struct rist_logging_settings *log_settings = NULL;
71 static pthread_mutex_t cb_lock;
72 static unsigned rx_data_count;
73
74 static int rx_data_cb(void *arg, struct rist_data_block *b) {
75 (void)arg;
76 pthread_mutex_lock(&cb_lock);
77 rx_data_count++;
78 pthread_mutex_unlock(&cb_lock);
79 rist_receiver_data_block_free2(&b);
80 return 0;
81 }
82
83 static unsigned rx_data_snapshot(void) {
84 pthread_mutex_lock(&cb_lock);
85 unsigned v = rx_data_count;
86 pthread_mutex_unlock(&cb_lock);
87 return v;
88 }
89
90 static void send_burst(struct rist_ctx *tx, uint32_t start, int count) {
91 for (int i = 0; i < count; i++) {
92 uint32_t v = start + (uint32_t)i;
93 struct rist_data_block blk;
94 memset(&blk, 0, sizeof(blk));
95 blk.payload = &v;
96 blk.payload_len = sizeof(v);
97 blk.virt_src_port = 1968;
98 rist_sender_data_write(tx, &blk);
99 usleep(20000); /* ~50 pkt/s */
100 }
101 }
102
103 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
104 (void)arg;
105 const char *tag = level <= RIST_LOG_ERROR ? "E" :
106 level <= RIST_LOG_WARN ? "W" :
107 level <= RIST_LOG_INFO ? "I" : "D";
108 fprintf(stderr, "%s| %s", tag, msg);
109 return 0;
110 }
111
112 /* Listener == SRP authenticator. */
113 static struct rist_ctx *spawn_authenticator(int port) {
114 struct rist_ctx *tx = NULL;
115 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0)
116 return NULL;
117 char url[256];
118 snprintf(url, sizeof(url),
119 "rist://@127.0.0.1:%d?session-timeout=%d&keepalive-interval=%d",
120 port, SESSION_TIMEOUT_MS, KEEPALIVE_MS);
121 struct rist_peer_config *pcfg = NULL;
122 if (rist_parse_address2(url, (void *)&pcfg) != 0) { rist_destroy(tx); return NULL; }
123 struct rist_peer *peer = NULL;
124 if (rist_peer_create(tx, &peer, pcfg) != 0) { free(pcfg); rist_destroy(tx); return NULL; }
125 free(pcfg);
126 if (rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) { rist_destroy(tx); return NULL; }
127 if (rist_start(tx) != 0) { rist_destroy(tx); return NULL; }
128 return tx;
129 }
130
131 int main(void) {
132 const int listen_port = 22012;
133 memset(&rx_data_count, 0, sizeof(rx_data_count));
134 pthread_mutex_init(&cb_lock, NULL);
135
136 if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0) {
137 fprintf(stderr, "logging setup failed\n");
138 return 99;
139 }
140
141 fprintf(stderr, "== spawning first authenticator (listener) on :%d ==\n", listen_port);
142 struct rist_ctx *tx1 = spawn_authenticator(listen_port);
143 if (!tx1) { fprintf(stderr, "authenticator create failed\n"); return 99; }
144 usleep(200000);
145
146 fprintf(stderr, "== spawning caller (authenticatee) ==\n");
147 struct rist_ctx *rx = NULL;
148 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) {
149 fprintf(stderr, "rx ctx create failed\n"); rist_destroy(tx1); return 99;
150 }
151 char rx_url[256];
152 snprintf(rx_url, sizeof(rx_url),
153 "rist://127.0.0.1:%d?session-timeout=%d&keepalive-interval=%d"
154 "&buffer=200&cname=restart-test",
155 listen_port, SESSION_TIMEOUT_MS, KEEPALIVE_MS);
156 struct rist_peer_config *rx_pcfg = NULL;
157 if (rist_parse_address2(rx_url, (void *)&rx_pcfg) != 0) {
158 fprintf(stderr, "rx url parse failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99;
159 }
160 struct rist_peer *rx_peer = NULL;
161 if (rist_peer_create(rx, &rx_peer, rx_pcfg) != 0) {
162 fprintf(stderr, "rx peer create failed\n"); free(rx_pcfg); rist_destroy(rx); rist_destroy(tx1); return 99;
163 }
164 free(rx_pcfg);
165 if (rist_enable_eap_srp_2(rx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
166 fprintf(stderr, "rx SRP enable failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99;
167 }
168 if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0) {
169 fprintf(stderr, "rx data callback set failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99;
170 }
171 if (rist_start(rx) != 0) {
172 fprintf(stderr, "rx start failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99;
173 }
174
175 /* wait for the initial SRP handshake */
176 usleep(5000000);
177
178 /* Confirm the pre-restart data path carries payload. */
179 send_burst(tx1, 0, 25);
180 usleep(1000000);
181 if (rx_data_snapshot() == 0) {
182 fprintf(stderr, "INDETERMINATE: no data before restart; setup broken.\n");
183 rist_destroy(rx); rist_destroy(tx1); free(log_settings); return 2;
184 }
185 fprintf(stderr, "== initial auth + data path OK (%u blocks) ==\n", rx_data_snapshot());
186
187 /* Restart the authenticator: destroy and immediately recreate on the same
188 * port. The caller never goes silent (long session_timeout), so the socket
189 * rebind cannot fire -- only the identity re-auth path can recover us. */
190 fprintf(stderr, "== restarting authenticator (fresh EAP state, same port) ==\n");
191 rist_destroy(tx1);
192 usleep(200000); /* brief gap, well under session_timeout/keepalive windows */
193 struct rist_ctx *tx2 = spawn_authenticator(listen_port);
194 if (!tx2) {
195 fprintf(stderr, "second authenticator create failed\n");
196 rist_destroy(rx); free(log_settings); return 99;
197 }
198
199 /* Give the fresh authenticator time to re-challenge (IDENTITY) and the
200 * caller time to notice the persistence and re-run the SRP handshake. */
201 usleep(8000000);
202
203 unsigned data_before = rx_data_snapshot();
204 send_burst(tx2, 1000, 50);
205 usleep(1200000);
206 unsigned data_after = rx_data_snapshot();
207
208 uint32_t rebinds = rx_peer->rebind_attempts;
209 bool dead = rx_peer->dead;
210
211 rist_destroy(rx);
212 rist_destroy(tx2);
213 free(log_settings);
214
215 fprintf(stderr, "== after restart: data_before=%u data_after=%u "
216 "rebind_attempts=%"PRIu32" dead=%d ==\n",
217 data_before, data_after, rebinds, dead);
218
219 if (data_after <= data_before) {
220 fprintf(stderr, "FAIL: caller did NOT recover after the authenticator "
221 "restart; no data flowed (locked in SUCCESS, refusing "
222 "the fresh IDENTITY forever).\n");
223 return 1;
224 }
225 if (rebinds != 0) {
226 fprintf(stderr, "FAIL: recovery came from the socket rebind "
227 "(rebind_attempts=%"PRIu32"), not the identity re-auth "
228 "path this test targets; tighten the timing.\n", rebinds);
229 return 1;
230 }
231
232 fprintf(stderr, "PASS: caller re-authenticated against the restarted "
233 "authenticator on its own (%u blocks flowed after restart, "
234 "no socket rebind) -- no operator intervention.\n",
235 data_after - data_before);
236 return 0;
237 }