summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/select.uc
blob: fb0f687bb75960eb7c6e9168e55eaabbbf986c79 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// travelmate - uplink selection: walks the configured stations against the
// ranked scan of one radio, steps (op): log sta keep add try noscan

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2016-2026 Dirk Brenken <dev@brenken.org>

// bssid with upper-case hex digits
export function normbssid(b) {
	return replace(b ?? '', /[a-f]/g, (m) => uc(m));
};

// locally administered address, the usual evil-twin pattern
function laa(bssid) {
	return (hex(split(bssid, ':')[0]) & 2) != 0;
}

// bracket expression at p[i] == '[', returns the index after ']' and whether
// c matched, or null if there is no closing ']' (then '[' is literal)
function bracket(p, i, c) {
	let j = i + 1, neg = false, ok = false;

	if (substr(p, j, 1) == '!' || substr(p, j, 1) == '^') {
		neg = true;
		j++;
	}

	for (let first = true; j < length(p); first = false) {
		let a = substr(p, j, 1);

		if (a == ']' && !first)
			return { next: j + 1, ok: ok != neg };

		if (substr(p, j + 1, 1) == '-' && j + 2 < length(p) && substr(p, j + 2, 1) != ']') {
			let b = substr(p, j + 2, 1);

			if (ord(c) >= ord(a) && ord(c) <= ord(b))
				ok = true;

			j += 3;
		}
		else {
			if (c == a)
				ok = true;

			j++;
		}
	}

	return null;
}

// glob pattern match on bytes for trm_ssidfilter: *, ?, [...], backslash
export function glob(p, s, pi, si) {
	pi ??= 0;
	si ??= 0;

	while (pi < length(p)) {
		let c = substr(p, pi, 1);

		if (c == '*') {
			while (substr(p, pi, 1) == '*')
				pi++;

			if (pi == length(p))
				return true;

			for (let k = si; k <= length(s); k++)
				if (glob(p, s, pi, k))
					return true;

			return false;
		}

		if (si >= length(s))
			return false;

		if (c == '?') {
			pi++;
			si++;
			continue;
		}

		if (c == '[') {
			let r = bracket(p, pi, substr(s, si, 1));

			if (r) {
				if (!r.ok)
					return false;

				pi = r.next;
				si++;
				continue;
			}
		}

		if (c == '\\' && pi + 1 < length(p))
			c = substr(p, ++pi, 1);

		if (substr(s, si, 1) != c)
			return false;

		pi++;
		si++;
	}

	return si == length(s);
};

function log(out, class, msg) {
	push(out, { op: 'log', class, msg });
}

// an info that repeats every cycle as long as the scan stays the same, the
// daemon logs it once per run and as debug line afterwards
function note(out, msg) {
	push(out, { op: 'log', class: 'info', msg, once: true });
}

// open uplinks to autoadd, in scan order: open, named, above minquality,
// not filtered, not configured yet and within the autoadd quota
function autoadd(out, opts, entries, known) {
	let cfg = opts.cfg, radio = opts.radio, cnt = cfg.autoaddcnt;

	for (let e in entries) {
		if (e.quality < cfg.minquality)
			break;

		if (!e.open || e.hidden || known[radio + '\n' + e.raw])
			continue;

		// per bss: an LAA bss is skipped, another bss of the ssid may count
		if (cfg.eviltwin && laa(e.bssid)) {
			note(out, sprintf("skipped autoadd of LAA candidate (evil-twin) '%s/%s/%s'",
				radio, e.ssid, e.bssid));
			continue;
		}

		// per ssid: one decision, also for further bss of a filtered one
		known[radio + '\n' + e.raw] = true;

		let pattern = null;

		for (let p in cfg.ssidfilter) {
			if (glob(p, e.raw)) {
				pattern = p;
				break;
			}
		}

		if (pattern != null) {
			note(out, sprintf("open uplink filtered out '%s/%s/%s'", radio, e.raw, pattern));
			continue;
		}

		if (cfg.maxautoadd != 0 && cnt >= cfg.maxautoadd)
			break;

		push(out, { op: 'add', radio, essid: e.raw }, { op: 'try' });
		cnt++;
	}
}

// configured stations first, in their order; open uplinks are added and tried
// only as last resort, never while the current uplink is kept
export function plan(opts) {
	let out = [], cfg = opts.cfg, radio = opts.radio, cur = opts.current;
	let ifaces = opts.ifaces ?? {}, entries = null, known = {};

	for (let n, s in ifaces)
		known[s.device + '\n' + s.ssid] = true;

	for (let sta in opts.stalist ?? []) {
		let w = ifaces[sta.section] ?? {};
		let sradio = w.device ?? '', sessid = w.ssid ?? '', sbssid = normbssid(w.bssid);

		if (!length(sradio) || !length(sessid)) {
			note(out, sprintf("invalid wireless section '%s'", sta.section));
			continue;
		}

		// a station of another radio can't match this scan, don't scan or
		// walk the results for it
		if (sradio != radio)
			continue;

		push(out, { op: 'sta', section: sta.section, radio: sradio, bssid: sbssid, essid: sessid });

		if (cur && radio == cur.radio && sradio == cur.radio &&
		    sessid == cur.essid && sbssid == cur.bssid) {
			push(out, { op: 'keep' });
			return out;
		}

		log(out, 'debug', sprintf('f_main-5    ::: sta_radio: %s, sta_essid: "%s", sta_bssid: %s',
			sradio, sessid, length(sbssid) ? sbssid : '-'));

		entries ??= opts.scan() ?? [];

		if (!length(entries)) {
			push(out, { op: 'noscan', radio });
			return out;
		}

		for (let e in entries) {
			let named = !e.hidden && e.raw == sessid;

			log(out, 'debug', sprintf('f_main-6    ::: radio(sta/scan): %s/%s, essid(sta/scan): "%s"/%s, bssid(sta/scan): %s/%s, quality(min/scan): %d/%d, open: %s',
				sradio, radio, sessid, e.ssid, sbssid, e.bssid, cfg.minquality, e.quality, e.open ? '+' : '-'));

			if (e.quality < cfg.minquality)
				break;

			// a pinned uplink only matches its own bss, another bss of
			// the ssid (multi-ap network) is just passed over
			if (((named && (!length(sbssid) || e.bssid == sbssid)) ||
			     (e.bssid == sbssid && e.hidden)) && radio == sradio) {
				if (cfg.eviltwin && !length(sbssid) && !e.hidden && laa(e.bssid)) {
					note(out, sprintf("skipped LAA candidate (evil-twin) '%s/%s/%s => %s'",
						sradio, sessid, length(sbssid) ? sbssid : '-', e.bssid));
					continue;
				}

				push(out, { op: 'try' });
				cur = null;
				break;
			}
		}
	}

	if (cfg.autoadd) {
		entries ??= opts.scan() ?? [];

		if (!length(entries)) {
			push(out, { op: 'noscan', radio });
			return out;
		}

		autoadd(out, opts, entries, known);
	}

	return out;
};