Repository navigation
Expand file tree
/
Copy pathdevices.c
More file actions
200 lines (179 loc) · 7.53 KB
/
Copy pathdevices.c
File metadata and controls
200 lines (179 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "devices.h"
#include "sloth_clock.h"
#include "sensor_health.h"
#include "oui.h"
#include "camera_join.h"
/* ── MAC helpers ─────────────────────────────────────────── */
static int parse_mac(const char *s, uint8_t mac[6]) {
if (!s || !s[0]) return 0;
unsigned b[6];
if (sscanf(s, "%x:%x:%x:%x:%x:%x",
&b[0], &b[1], &b[2], &b[3], &b[4], &b[5]) != 6)
return 0;
for (int i = 0; i < 6; i++) mac[i] = (uint8_t)b[i];
return 1;
}
static int mac_eq(const uint8_t a[6], const uint8_t b[6]) {
return memcmp(a, b, 6) == 0;
}
static int mac_is_zero(const uint8_t a[6]) {
static const uint8_t zero[6] = {0};
return memcmp(a, zero, 6) == 0;
}
/* ── Upsert ──────────────────────────────────────────────── */
/* Find or append a device record for `mac` in dev[0..*n], expanding *n.
* Returns NULL if the table is full and the MAC isn't already present. */
static device_t *upsert(device_t *dev, int *n, const uint8_t mac[6]) {
if (mac_is_zero(mac)) return NULL;
for (int i = 0; i < *n; i++) {
if (mac_eq(dev[i].mac, mac)) return &dev[i];
}
/* Drop-on-full rather than LRU: a NEW device is refused. Different
* mechanism from the tables above, same sensor-health meaning — an
* observation did not make it in (#91 slice 3). */
if (*n >= MAX_DEVICES) { sh_evict_note(SH_EVICT_DEVICE); return NULL; }
device_t *d = &dev[(*n)++];
memset(d, 0, sizeof(*d));
memcpy(d->mac, mac, 6);
return d;
}
static void set_str(char *dst, int dstsz, const char *src) {
if (!src || !src[0] || !dst || dstsz <= 0) return;
if (dst[0]) return; /* prefer first non-empty value */
snprintf(dst, (size_t)dstsz, "%s", src);
}
static const char *short_vendor(const char *v) {
return (v && v[0]) ? v : "";
}
/* ── Sources ─────────────────────────────────────────────── */
/* A device record keeps one address. The neighbour table can hold
* several for one MAC since #176 (IPv4 from ARP, IPv6 from the NDP
* cache): the IPv4 one is kept over any IPv6, and a global IPv6 over a
* link-local, because that is the order a reader can act on them -
* leases, names and flows are keyed by the IPv4, and fe80:: says only
* which link. Equal rank keeps the first seen. */
static int ip_rank(const char *ip) {
if (!ip[0]) return 0;
if (!strchr(ip, ':')) return 3;
if (ip[0] == 'f' && ip[1] == 'e' &&
(ip[2] == '8' || ip[2] == '9' || ip[2] == 'a' || ip[2] == 'b'))
return 1;
return 2;
}
static void merge_arp(const sloth_state_t *s, device_t *dev, int *n, time_t now) {
for (int i = 0; i < s->arp_count; i++) {
const arp_entry_t *a = &s->arp_entries[i];
device_t *d = upsert(dev, n, a->mac);
if (!d) continue;
if (ip_rank(a->ip) > ip_rank(d->ip))
snprintf(d->ip, sizeof(d->ip), "%s", a->ip);
set_str(d->vendor, sizeof(d->vendor), short_vendor(oui_lookup(a->mac)));
d->sources |= DEV_SRC_ARP;
d->last_seen = now;
}
}
static void merge_dhcp(const sloth_state_t *s, device_t *dev, int *n, time_t now) {
/* DHCP leases lack the MAC, so bridge via ARP: find any device whose
IP matches the lease, then attach the hostname. */
(void)now;
for (int i = 0; i < s->dhcp_count; i++) {
const dhcp_lease_t *L = &s->dhcp_leases[i];
if (!L->hostname[0] && !L->ip[0]) continue;
for (int j = 0; j < *n; j++) {
if (strcmp(dev[j].ip, L->ip) == 0) {
set_str(dev[j].hostname, sizeof(dev[j].hostname), L->hostname);
dev[j].sources |= DEV_SRC_DHCP;
break;
}
}
}
}
static void merge_beacons(const sloth_state_t *s, device_t *dev, int *n, time_t now) {
for (int i = 0; i < s->beacon_count; i++) {
const beacon_ap_t *b = &s->beacon_aps[i];
device_t *d = upsert(dev, n, b->bssid);
if (!d) continue;
d->is_ap = 1;
d->signal_dbm = b->signal_dbm;
if (b->dist.valid) d->dist = b->dist;
set_str(d->vendor, sizeof(d->vendor), short_vendor(oui_lookup(b->bssid)));
d->sources |= DEV_SRC_BEACON;
if (b->last_seen > d->last_seen) d->last_seen = b->last_seen;
(void)now;
}
}
static void merge_probes(const sloth_state_t *s, device_t *dev, int *n, time_t now) {
for (int i = 0; i < s->probe_count; i++) {
const probe_client_t *p = &s->probe_clients[i];
device_t *d = upsert(dev, n, p->mac);
if (!d) continue;
d->probe_count += p->frame_count;
if (p->ssid[0])
snprintf(d->last_ssid, sizeof(d->last_ssid), "%s", p->ssid);
d->signal_dbm = p->signal_dbm;
if (p->dist.valid) d->dist = p->dist;
set_str(d->vendor, sizeof(d->vendor), short_vendor(oui_lookup(p->mac)));
d->sources |= DEV_SRC_PROBE;
if (p->last_seen > d->last_seen) d->last_seen = p->last_seen;
(void)now;
}
}
static void merge_stas(const sloth_state_t *s, device_t *dev, int *n, time_t now) {
for (int i = 0; i < s->wifi_sta_count; i++) {
const wifi_sta_t *st = &s->wifi_stas[i];
uint8_t mac[6];
if (!parse_mac(st->mac, mac)) continue;
device_t *d = upsert(dev, n, mac);
if (!d) continue;
d->signal_dbm = st->signal_dbm;
set_str(d->vendor, sizeof(d->vendor), short_vendor(oui_lookup(mac)));
d->sources |= DEV_SRC_STA;
d->last_seen = now;
}
}
/* ── Public ──────────────────────────────────────────────── */
void devices_update(sloth_state_t *s) {
if (!s) return;
time_t now = sloth_wall();
device_t buf[MAX_DEVICES];
int n = 0;
/* Order matters: ARP gives us IPs first, then DHCP bridges hostnames
into matching records. */
merge_arp (s, buf, &n, now);
merge_beacons(s, buf, &n, now);
merge_probes(s, buf, &n, now);
merge_stas (s, buf, &n, now);
/* After every MAC-keyed source, so a camera row joins the record
* ARP already gave its IP rather than creating an IP-only twin of
* it, and before DHCP so an IP-only camera record can still pick
* up its lease hostname (#106 round 2). */
camera_join (s, buf, &n, now);
merge_dhcp (s, buf, &n, now);
/* Sort newest-first by last_seen. */
for (int i = 0; i < n - 1; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (buf[j].last_seen > buf[max].last_seen) max = j;
}
if (max != i) {
device_t tmp = buf[i];
buf[i] = buf[max];
buf[max] = tmp;
}
}
for (int i = 0; i < n; i++) s->devices[i] = buf[i];
s->device_count = n;
if (s->device_sel >= n) s->device_sel = n > 0 ? n - 1 : 0;
if (s->device_sel < 0) s->device_sel = 0;
/* Second pass: risk-score each device against the current sloth
* state. Runs after the write into s->devices because the scoring
* function inspects s->alerts / s->cleartext_creds and needs to
* see the same view the operator will. See roadmap #16 phase 4. */
for (int i = 0; i < s->device_count; i++) {
device_t *d = &s->devices[i];
d->risk_level = device_risk_score(d, s, &d->risk_signals);
}
}