summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_wifi.uc
blob: 693d3c02e4fee848be4ed042440fb78477ac1285 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import * as struct from "struct";
let ubus, uloop, global, timer;
let ap_cache = {};

const ie_tags = {
	PWR_CAPABILITY: 33,
	HT_CAP: 45,
	EXT_CAPAB: 127,
	VHT_CAP: 191,
	__EXT_START: 0x100,
	HE_CAP: 0x100 | 35,
	VENDOR_WPS: 0x0050f204,
};

const ie_parser_proto = {
	reset: function() {
		this.offset = 0;
	},

	parseAt: function(offset) {
		let hdr = substr(this.buffer, offset, 2);
		if (length(hdr) != 2)
			return null;

		let data = this.hdr.unpack(hdr);
		if (length(data != 2))
			return null;

		let len = data[1];
		offset += 2;
		data[1] += 2;

		if (data[0] == 221 && len >= 4) {
			hdr = substr(this.buffer, offset, 4);
			if (length(hdr) != 4)
				return null;

			let val = this.vendor_hdr.unpack(hdr);
			if (length(val) != 1 || val[0] < 0x200)
				return null;

			data[0] = val[0];
			len -= 4;
			offset += 4;
		} else if (data[0] == 255 && len >= 1) {
			hdr = substr(this.buffer, offset, 2);
			if (length(hdr) != 2)
				return null;
			data[0] = 0x100 + this.hdr.unpack(hdr)[0];
			len -= 1;
			offset += 1;
		}

		data[2] = data[1];
		data[1] = substr(this.buffer, offset, len);
		if (length(data[1]) != len)
			return null;

		return data;
	},

	next: function() {
		let data = this.parseAt(this.offset);
		if (!data)
			return null;

		this.offset += data[2];
		return data;
	},

	foreach: function(cb) {
		let offset = 0;
		let data;

		while ((data = this.parseAt(offset)) != null) {
			offset += data[2];
			let ret = cb(data);
			if (type(ret) == "boolean" && !ret)
				break;
		}
	},
};

function ie_parser(data) {
	let parser = {
		offset: 0,
		buffer: data,
		hdr: struct.new("BB"),
		vendor_hdr: struct.new(">I"),
	};

	proto(parser, ie_parser_proto);

	return parser;
}

function format_fn(unpack_str, format)
{
	return (data) => {
		data = struct.unpack(unpack_str, data);
		if (data && data[0])
			data = data[0];
		else
			data = 0;
		return sprintf(format, data)
	};
}

let unpack;
unpack = {
	u8: format_fn("B", "%02x"),
	le16: format_fn("<H", "%04x"),
	le32: format_fn("<I", "%08x"),
	bytes: (data) => join("", map(split(data, ""), unpack.u8)),
};
let fingerprint_order = [
	"htcap", "htagg", "htmcs", "vhtcap", "vhtrxmcs", "vhttxmcs",
	"txpow", "extcap", "wps", "hemac", "hephy"
];

function format_wps_ie(data) {
	let offset = 0;
	let len = length(data);
	let s = struct.new("<HH");

	while (offset + 4 <= len) {
		let hdr = s.unpack(substr(data, offset, 4));
		let val = substr(data, offset + 4, hdr[1]);

		offset += 4 + hdr[1];
		if (hdr[0] != 0x1023)
			continue;

		if (length(val) != hdr[1])
			break;

		return replace(val, /[^A-Za-z0-9]/, "_");
	}

	return null;
}

function ie_fingerprint_str(id) {
	if (id >= 0x200)
		return sprintf("221(%08x)", id);
	if (id >= 0x100)
		return sprintf("255(%d)", id - 0x100);
	return sprintf("%d", id);
}

let vendor_ie_filter = [
	0x0050f2, // Microsoft WNN
	0x506f9a, // WBA
	0x8cfdf0, // Qualcom
	0x001018, // Broadcom
	0x000c43, // Ralink
];

function ie_fingerprint(data, mode) {
	let caps = {
		tags: [],
		vendor_list: {}
	};
	let parser = ie_parser(data);

	parser.foreach(function(ie) {
		let skip = false;
		let val = ie[1];
		switch (ie[0]) {
		case ie_tags.HT_CAP:
			caps.htcap = unpack.le16(substr(val, 0, 2));
			caps.htagg = unpack.u8(substr(val, 2, 1));
			caps.htmcs = unpack.le32(substr(val, 3, 4));
			break;
		case ie_tags.VHT_CAP:
			caps.vhtcap = unpack.le32(substr(val, 0, 4));
			caps.vhtrxmcs = unpack.le32(substr(val, 4, 4));
			caps.vhttxmcs = unpack.le32(substr(val, 8, 4));
			break;
		case ie_tags.EXT_CAPAB:
			caps.extcap = unpack.bytes(val);
			break;
		case ie_tags.PWR_CAPABILITY:
			caps.txpow = unpack.le16(val);
			break;
		case ie_tags.VENDOR_WPS:
			caps.wps = format_wps_ie(val);
			break;
		case ie_tags.HE_CAP:
			if (mode != "wifi6") {
				skip = true;
				break;
			}
			caps.hemac =
				unpack.le16(substr(val, 4, 2)) +
				unpack.le32(substr(val, 0, 4));
			caps.hephy =
				unpack.le16(substr(val, 15, 2)) +
				unpack.le32(substr(val, 11, 4)) +
				unpack.le32(substr(val, 7, 4));
			break;
		}
		if (ie[0] > 0x200) {
			let vendor = ie[0] >> 8;
			if (!(vendor in vendor_ie_filter))
				caps.vendor_list[sprintf("%06x", vendor)] = 1;
		}
		if (!skip)
			push(caps.tags, ie[0]);
		return null;
	});

	switch (mode) {
	case "wifi6":
		if (mode == "wifi6" && !caps.hemac)
			return null;
		break;
	case "wifi-vendor-oui":
		return caps.vendor_list;
	default:
		break;
	}

	let tags = map(caps.tags, ie_fingerprint_str);
	return
		join(",", tags) + "," +
		join(",", map(
			filter(fingerprint_order, (key) => !!caps[key]),
			(key) => `${key}:${caps[key]}`
		));
}

function fingerprint(mac, mode, ies) {
	switch (mode) {
	case "wifi4":
		if (!ies.assoc_ie)
			break;

		let assoc = ie_fingerprint(ies.assoc_ie, mode);
		if (!assoc)
			break;

		global.device_add_data(mac, `${mode}|${assoc}`);
		break;
	case "wifi-vendor-oui":
		let list = ie_fingerprint(ies.assoc_ie, mode);
		for (let oui in list) {
			global.device_add_data(mac, `${mode}-${oui}|1`);
		}
		break;
	case "wifi6":
	default:
		let val = ie_fingerprint(ies.assoc_ie, mode);
		if (!val)
			break;

		global.device_add_data(mac, `${mode}|${val}`);
		break;
	}
}

const fingerprint_modes = [ "wifi4", "wifi6", "wifi-vendor-oui" ];

function client_refresh(ap, mac, prev_cache)
{
	let ies = ubus.call(ap, "get_sta_ies", { address: mac });
	if (type(ies) != "object" || !ies.assoc_ie)
		return null;

	ies.assoc_ie = b64dec(ies.assoc_ie);
	if (ies.probe_ie)
		ies.probe_ie = b64dec(ies.probe_ie);

	for (let mode in fingerprint_modes)
		fingerprint(mac, mode, ies);

	return ies;
}

function refresh()
{
	let ap_objs = filter(ubus.list(), (name) => match(name, /^hostapd\./));
	let prev_cache = ap_cache;
	ap_cache = {};

	timer.set(30 * 1000);
	for (let ap in ap_objs) {
		try {
			let cur_cache = {};
			let prev_ap_cache = prev_cache[ap] ?? {};

			ap_cache[ap] = cur_cache;

			let clients = ubus.call(ap, "get_clients").clients;
			for (let client in clients) {
				let client_cache = prev_ap_cache[client];
				if (!client_cache || !client_cache.assoc_ie || !client_cache.probe_ie)
					client_cache = client_refresh(ap, client);
				global.device_refresh(client);
			}
		} catch (e) {
		}
	}
}

function init(gl) {
	global = gl;
	ubus = gl.ubus;
	uloop = gl.uloop;

	global.add_weight({
		wifi4: 2.0,
		wifi6: 3.0,
		"wifi-vendor-oui": 2.0
	});

	timer = uloop.timer(1000, refresh);
}

return { init, refresh };