|
0
|
1 /*
|
|
|
2 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
|
|
|
3 *
|
|
|
4 * SPDX-License-Identifier: BSD-2-Clause
|
|
|
5 */
|
|
|
6
|
|
|
7 #include <sys/types.h>
|
|
|
8 #include <sys/tree.h>
|
|
|
9
|
|
|
10 #include <inttypes.h>
|
|
|
11 #include <stdlib.h>
|
|
|
12 #include <syslog.h>
|
|
|
13 #include <time.h>
|
|
|
14
|
|
|
15 #include "botblockd.h"
|
|
|
16
|
|
|
17 /*
|
|
|
18 * blocked_at carries three states together with is_blocked:
|
|
|
19 * - is_blocked, blocked_at != 0: pf entry active since blocked_at
|
|
|
20 * - !is_blocked, blocked_at != 0: probation (block expired but
|
|
|
21 * counters and the suspect record persist for re-block evaluation)
|
|
|
22 * - !is_blocked, blocked_at == 0: fresh suspect, never blocked
|
|
|
23 *
|
|
|
24 * Times are monotime() seconds, counted from boot. No block can be
|
|
|
25 * placed within the first second of uptime, so 0 stays free as the
|
|
|
26 * never-blocked marker.
|
|
|
27 *
|
|
|
28 * Field order keeps the 4-byte and 1-byte fields together so the
|
|
|
29 * struct is 64 bytes on 64-bit platforms, a malloc(3) size class.
|
|
|
30 */
|
|
|
31 struct suspect {
|
|
|
32 uint32_t ip; /* network byte order, RB key */
|
|
|
33 uint32_t errors; /* saturates at UINT32_MAX */
|
|
|
34 uint32_t successes; /* saturates at UINT32_MAX */
|
|
|
35 bool is_blocked;
|
|
|
36 RB_ENTRY(suspect) entry;
|
|
|
37 time_t first_seen_at;
|
|
|
38 time_t last_error_at;
|
|
|
39 time_t blocked_at;
|
|
|
40 };
|
|
|
41
|
|
|
42 RB_HEAD(suspect_tree, suspect);
|
|
|
43
|
|
|
44 /*
|
|
|
45 * suspect_cmp - order two suspects by address, for the tree
|
|
|
46 *
|
|
|
47 * Parameters:
|
|
|
48 * a, b - suspects to compare
|
|
|
49 *
|
|
|
50 * Returns: -1, 0 or 1 as a's address is below, equal to or above b's
|
|
|
51 */
|
|
|
52 static int
|
|
|
53 suspect_cmp(const struct suspect *a, const struct suspect *b)
|
|
|
54 {
|
|
|
55 if (a->ip < b->ip)
|
|
|
56 return (-1);
|
|
|
57 if (a->ip > b->ip)
|
|
|
58 return (1);
|
|
|
59 return (0);
|
|
|
60 }
|
|
|
61
|
|
|
62 RB_GENERATE_STATIC(suspect_tree, suspect, entry, suspect_cmp);
|
|
|
63
|
|
|
64 static struct suspect_tree suspects = RB_INITIALIZER(&suspects);
|
|
|
65
|
|
|
66 /*
|
|
|
67 * suspect_drop - log why a suspect is forgotten, then remove it
|
|
|
68 *
|
|
|
69 * Parameters:
|
|
|
70 * s - suspect to remove and free
|
|
|
71 * reason - log text after the address
|
|
|
72 */
|
|
|
73 static void
|
|
|
74 suspect_drop(struct suspect *s, const char *reason)
|
|
|
75 {
|
|
|
76 syslog(LOG_INFO, "%s %s", ip_str(s->ip), reason);
|
|
|
77 RB_REMOVE(suspect_tree, &suspects, s);
|
|
|
78 free(s);
|
|
|
79 }
|
|
|
80
|
|
|
81 /*
|
|
|
82 * suspect_block - log a verdict and add the suspect to the pf table
|
|
|
83 *
|
|
|
84 * The suspect counts as blocked only if pf_block succeeds. Otherwise
|
|
|
85 * it is left as it was, and the next sweep reaches the same verdict
|
|
|
86 * and tries again.
|
|
|
87 *
|
|
|
88 * Parameters:
|
|
|
89 * s - suspect to block
|
|
|
90 * now - current monotime()
|
|
|
91 * reason - log text after the address
|
|
|
92 */
|
|
|
93 static void
|
|
|
94 suspect_block(struct suspect *s, time_t now, const char *reason)
|
|
|
95 {
|
|
|
96 syslog(LOG_NOTICE, "%s %s (%" PRIu32 " err, %" PRIu32 " ok)",
|
|
|
97 ip_str(s->ip), reason, s->errors, s->successes);
|
|
|
98 if (pf_block(s->ip) != 0)
|
|
|
99 return;
|
|
|
100 s->is_blocked = true;
|
|
|
101 s->blocked_at = now;
|
|
|
102 }
|
|
|
103
|
|
|
104 /*
|
|
|
105 * process_request - fold one parsed log request into the suspect pool
|
|
|
106 *
|
|
|
107 * Pure counter update. Whitelisted sources are dropped up front so the
|
|
|
108 * pool stays clean of them. Error status: ensure a suspect entry exists,
|
|
|
109 * then increment errors and refresh last_error_at. Non-error status:
|
|
|
110 * increment successes only if the IP is already a suspect; good traffic
|
|
|
111 * alone never enters the pool.
|
|
|
112 *
|
|
|
113 * Parameters:
|
|
|
114 * ip - source address (network byte order)
|
|
|
115 * status - HTTP status code
|
|
|
116 */
|
|
|
117 void
|
|
|
118 process_request(uint32_t ip, int status)
|
|
|
119 {
|
|
|
120 struct suspect key, *s;
|
|
|
121 time_t now;
|
|
|
122
|
|
|
123 if (is_whitelisted(ip))
|
|
|
124 return;
|
|
|
125
|
|
|
126 now = monotime();
|
|
|
127
|
|
|
128 key.ip = ip;
|
|
|
129 s = RB_FIND(suspect_tree, &suspects, &key);
|
|
|
130
|
|
|
131 if (status_is_error(status)) {
|
|
|
132 if (s == NULL) {
|
|
|
133 s = calloc(1, sizeof(*s));
|
|
|
134 if (s == NULL)
|
|
|
135 return;
|
|
|
136 s->ip = ip;
|
|
|
137 s->first_seen_at = now;
|
|
|
138 RB_INSERT(suspect_tree, &suspects, s);
|
|
|
139 syslog(LOG_INFO, "%s suspect (%d)", ip_str(s->ip), status);
|
|
|
140 }
|
|
|
141 if (s->errors < UINT32_MAX)
|
|
|
142 s->errors++;
|
|
|
143 s->last_error_at = now;
|
|
|
144 } else if (s != NULL && s->successes < UINT32_MAX) {
|
|
|
145 s->successes++;
|
|
|
146 }
|
|
|
147 }
|
|
|
148
|
|
|
149 /*
|
|
|
150 * sweep - evaluate every suspect entry and apply state transitions
|
|
|
151 *
|
|
|
152 * All block/unblock/cleanup decisions happen here; the per-line path
|
|
|
153 * does pure counter updates. Per suspect, takes one of:
|
|
|
154 * - drop on whitelist hit (unblocking first if needed),
|
|
|
155 * - lift block once block_secs has elapsed (probation start, record
|
|
|
156 * kept, counters intact),
|
|
|
157 * - on probation: re-block on new errors with bad balance, else drop
|
|
|
158 * once block_secs + cooldown_secs has elapsed,
|
|
|
159 * - fresh: drop if no errors for cooldown_secs, else apply the
|
|
|
160 * inspection-window verdict.
|
|
|
161 *
|
|
|
162 * Counter preservation across block boundaries is intentional: a
|
|
|
163 * heavily-bad balance cannot be flipped by silence alone, only by
|
|
|
164 * actual successful traffic during probation.
|
|
|
165 */
|
|
|
166 void
|
|
|
167 sweep(void)
|
|
|
168 {
|
|
|
169 struct suspect *s, *next_s;
|
|
|
170 time_t now;
|
|
|
171
|
|
|
172 now = monotime();
|
|
|
173
|
|
|
174 /*
|
|
|
175 * Each suspect falls into exactly one of four cases, tested in
|
|
|
176 * priority order; every case ends in continue so the cascade does
|
|
|
177 * not fall through. The order matters: a whitelist hit wins over
|
|
|
178 * any block state, and an active block is handled before the
|
|
|
179 * probation and fresh-suspect cases that key off blocked_at.
|
|
|
180 */
|
|
|
181 RB_FOREACH_SAFE(s, suspect_tree, &suspects, next_s) {
|
|
|
182 /*
|
|
|
183 * Case 1: became whitelisted after entering the pool (added to
|
|
|
184 * the whitelist file and reloaded). Lift any live block so no
|
|
|
185 * stale pf entry is left behind, then forget the suspect.
|
|
|
186 */
|
|
|
187 if (is_whitelisted(s->ip)) {
|
|
|
188 if (s->is_blocked && pf_unblock(s->ip) != 0)
|
|
|
189 syslog(LOG_WARNING, "cannot unblock %s",
|
|
|
190 ip_str(s->ip));
|
|
|
191 suspect_drop(s, "cleared whitelisted");
|
|
|
192 continue;
|
|
|
193 }
|
|
|
194
|
|
|
195 /*
|
|
|
196 * Case 2: currently blocked. Hold the pf entry until
|
|
|
197 * block_secs has elapsed, then lift it into probation -- the
|
|
|
198 * record and counters are kept so a repeat offender is caught
|
|
|
199 * by case 3 rather than starting over as a fresh suspect.
|
|
|
200 */
|
|
|
201 if (s->is_blocked) {
|
|
|
202 if (now - s->blocked_at > cfg.block_secs) {
|
|
|
203 if (pf_unblock(s->ip) != 0)
|
|
|
204 syslog(LOG_WARNING, "cannot unblock %s",
|
|
|
205 ip_str(s->ip));
|
|
|
206 s->is_blocked = false;
|
|
|
207 syslog(LOG_NOTICE, "%s probation start",
|
|
|
208 ip_str(s->ip));
|
|
|
209 }
|
|
|
210 continue;
|
|
|
211 }
|
|
|
212
|
|
|
213 /*
|
|
|
214 * Case 3: probation -- block already lifted, record retained.
|
|
|
215 * Re-block at once if the IP has errored again since the block
|
|
|
216 * was placed and its balance is still bad; otherwise forget it
|
|
|
217 * once a full block_secs + cooldown_secs has passed clean.
|
|
|
218 */
|
|
|
219 if (s->blocked_at != 0) {
|
|
|
220 if (s->last_error_at > s->blocked_at &&
|
|
|
221 s->errors > s->successes)
|
|
|
222 suspect_block(s, now, "probation violation");
|
|
|
223 else if (now - s->blocked_at >
|
|
|
224 (time_t)cfg.block_secs + cfg.cooldown_secs)
|
|
|
225 suspect_drop(s, "cleared probation");
|
|
|
226 continue;
|
|
|
227 }
|
|
|
228
|
|
|
229 /*
|
|
|
230 * Case 4: fresh suspect, never blocked (blocked_at == 0).
|
|
|
231 * First, forget it if it has gone quiet for cooldown_secs --
|
|
|
232 * a stray error that never recurred.
|
|
|
233 */
|
|
|
234 if (now - s->last_error_at > cfg.cooldown_secs) {
|
|
|
235 suspect_drop(s, "cleared cooldown");
|
|
|
236 continue;
|
|
|
237 }
|
|
|
238
|
|
|
239 /*
|
|
|
240 * Otherwise, once the inspection window closes -- by elapsed
|
|
|
241 * time or by request count, whichever comes first -- render a
|
|
|
242 * verdict: block if errors reached min_errors and outnumber
|
|
|
243 * successes. The window is not restarted: once it has closed,
|
|
|
244 * the counters keep accumulating and the suspect is judged
|
|
|
245 * again at every sweep until it is blocked or goes quiet for
|
|
|
246 * cooldown_secs.
|
|
|
247 */
|
|
|
248 if (now - s->first_seen_at > cfg.inspect_secs ||
|
|
|
249 (uint64_t)s->errors + s->successes >=
|
|
|
250 (uint64_t)cfg.inspect_count) {
|
|
|
251 if (s->errors >= (uint32_t)cfg.min_errors &&
|
|
|
252 s->errors > s->successes)
|
|
|
253 suspect_block(s, now, "window verdict");
|
|
|
254 }
|
|
|
255 }
|
|
|
256 }
|