summaryrefslogtreecommitdiffstats
path: root/package/network/services/hostapd/files/common.uc
blob: 8399d3ff960abcefaddb297ac1e850430427271e (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import * as nl80211 from "nl80211";
import * as rtnl from "rtnl";
import { readfile, glob, basename, readlink } from "fs";

const iftypes = {
	ap: nl80211.const.NL80211_IFTYPE_AP,
	mesh: nl80211.const.NL80211_IFTYPE_MESH_POINT,
	sta: nl80211.const.NL80211_IFTYPE_STATION,
	adhoc: nl80211.const.NL80211_IFTYPE_ADHOC,
	monitor: nl80211.const.NL80211_IFTYPE_MONITOR,
};

const mesh_params = {
	mesh_retry_timeout: "retry_timeout",
	mesh_confirm_timeout: "confirm_timeout",
	mesh_holding_timeout: "holding_timeout",
	mesh_max_peer_links: "max_peer_links",
	mesh_max_retries: "max_retries",
	mesh_ttl: "ttl",
	mesh_element_ttl: "element_ttl",
	mesh_auto_open_plinks: "auto_open_plinks",
	mesh_hwmp_max_preq_retries: "hwmp_max_preq_retries",
	mesh_path_refresh_time: "path_refresh_time",
	mesh_min_discovery_timeout: "min_discovery_timeout",
	mesh_hwmp_active_path_timeout: "hwmp_active_path_timeout",
	mesh_hwmp_preq_min_interval: "hwmp_preq_min_interval",
	mesh_hwmp_net_diameter_traversal_time: "hwmp_net_diam_trvs_time",
	mesh_hwmp_rootmode: "hwmp_rootmode",
	mesh_hwmp_rann_interval: "hwmp_rann_interval",
	mesh_gate_announcements: "gate_announcements",
	mesh_sync_offset_max_neighor: "sync_offset_max_neighbor",
	mesh_rssi_threshold: "rssi_threshold",
	mesh_hwmp_active_path_to_root_timeout: "hwmp_path_to_root_timeout",
	mesh_hwmp_root_interval: "hwmp_root_interval",
	mesh_hwmp_confirmation_interval: "hwmp_confirmation_interval",
	mesh_awake_window: "awake_window",
	mesh_plink_timeout: "plink_timeout",
	mesh_fwding: "forwarding",
	mesh_power_mode: "power_mode",
	mesh_nolearn: "nolearn"
};

function wdev_remove(name)
{
	nl80211.request(nl80211.const.NL80211_CMD_DEL_INTERFACE, 0, { dev: name });
}

function __phy_is_fullmac(phyidx)
{
	let data = nl80211.request(nl80211.const.NL80211_CMD_GET_WIPHY, 0, { wiphy: phyidx });

	return !data.software_iftypes.ap_vlan;
}

function phy_is_fullmac(phy)
{
	let phyidx = int(trim(readfile(`/sys/class/ieee80211/${phy}/index`)));

	return __phy_is_fullmac(phyidx);
}

function find_reusable_wdev(phyidx)
{
	if (!__phy_is_fullmac(phyidx))
		return null;

	let data = nl80211.request(
		nl80211.const.NL80211_CMD_GET_INTERFACE,
		nl80211.const.NLM_F_DUMP,
		{ wiphy: phyidx });
	for (let res in data)
		if (trim(readfile(`/sys/class/net/${res.ifname}/operstate`)) == "down")
			return res.ifname;
	return null;
}

function wdev_create(phy, name, data)
{
	let phyidx = int(readfile(`/sys/class/ieee80211/${phy}/index`));

	wdev_remove(name);

	if (!iftypes[data.mode])
		return `Invalid mode: ${data.mode}`;

	let req = {
		wiphy: phyidx,
		ifname: name,
		iftype: iftypes[data.mode],
	};

	if (data["4addr"])
		req["4addr"] = data["4addr"];
	if (data.macaddr)
		req.mac = data.macaddr;

	nl80211.error();

	let reuse_ifname = find_reusable_wdev(phyidx);
	if (reuse_ifname &&
	    (reuse_ifname == name ||
	     rtnl.request(rtnl.const.RTM_SETLINK, 0, { dev: reuse_ifname, ifname: name}) != false))
		nl80211.request(
			nl80211.const.NL80211_CMD_SET_INTERFACE, 0, {
				wiphy: phyidx,
				dev: name,
				iftype: iftypes[data.mode],
			});
	else
		nl80211.request(
			nl80211.const.NL80211_CMD_NEW_INTERFACE,
			nl80211.const.NLM_F_CREATE,
			req);

	let error = nl80211.error();
	if (error)
		return error;

	if (data.powersave != null) {
		nl80211.request(nl80211.const.NL80211_CMD_SET_POWER_SAVE, 0,
			{ dev: name, ps_state: data.powersave ? 1 : 0});
	}

	return null;
}

function wdev_set_mesh_params(name, data)
{
	let mesh_cfg = {};

	for (let key in mesh_params) {
		let val = data[key];
		if (val == null)
			continue;
		mesh_cfg[mesh_params[key]] = int(val);
	}

	if (!length(mesh_cfg))
		return null;

	nl80211.request(nl80211.const.NL80211_CMD_SET_MESH_CONFIG, 0,
		{ dev: name, mesh_params: mesh_cfg });

	return nl80211.error();
}

function phy_sysfs_file(phy, name)
{
	return trim(readfile(`/sys/class/ieee80211/${phy}/${name}`));
}

function macaddr_split(str)
{
	return map(split(str, ":"), (val) => hex(val));
}

function macaddr_join(addr)
{
	return join(":", map(addr, (val) => sprintf("%02x", val)));
}

function wdev_macaddr(wdev)
{
	return trim(readfile(`/sys/class/net/${wdev}/address`));
}

const phy_proto = {
	macaddr_init: function(used, options) {
		this.macaddr_options = options ?? {};
		this.macaddr_list = {};

		if (type(used) == "object")
			for (let addr in used)
				this.macaddr_list[addr] = used[addr];
		else
			for (let addr in used)
				this.macaddr_list[addr] = -1;

		this.for_each_wdev((wdev) => {
			let macaddr = wdev_macaddr(wdev);
			this.macaddr_list[macaddr] ??= -1;
		});

		return this.macaddr_list;
	},

	macaddr_generate: function(data) {
		let phy = this.name;
		let idx = int(data.id ?? 0);
		let mbssid = int(data.mbssid ?? 0) > 0;
		let num_global = int(data.num_global ?? 1);
		let use_global = !mbssid && idx < num_global;

		let base_addr = phy_sysfs_file(phy, "macaddress");
		if (!base_addr)
			return null;

		if (!idx && !mbssid)
			return base_addr;

		let base_mask = phy_sysfs_file(phy, "address_mask");
		if (!base_mask)
			return null;

		if (base_mask == "00:00:00:00:00:00" && idx >= num_global) {
			let addrs = split(phy_sysfs_file(phy, "addresses"), "\n");

			if (idx < length(addrs))
				return addrs[idx];

			base_mask = "ff:ff:ff:ff:ff:ff";
		}

		let addr = macaddr_split(base_addr);
		let mask = macaddr_split(base_mask);
		let type;

		if (mbssid)
			type = "b5";
		else if (use_global)
			type = "add";
		else if (mask[0] > 0)
			type = "b1";
		else if (mask[5] < 0xff)
			type = "b5";
		else
			type = "add";

		switch (type) {
		case "b1":
			if (!(addr[0] & 2))
				idx--;
			addr[0] |= 2;
			addr[0] ^= idx << 2;
			break;
		case "b5":
			if (mbssid)
				addr[0] |= 2;
			addr[5] ^= idx;
			break;
		default:
			for (let i = 5; i > 0; i--) {
				addr[i] += idx;
				if (addr[i] < 256)
					break;
				addr[i] %= 256;
			}
			break;
		}

		return macaddr_join(addr);
	},

	macaddr_next: function(val) {
		let data = this.macaddr_options ?? {};
		let list = this.macaddr_list;

		for (let i = 0; i < 32; i++) {
			data.id = i;

			let mac = this.macaddr_generate(data);
			if (!mac)
				return null;

			if (list[mac] != null)
				continue;

			list[mac] = val != null ? val : -1;
			return mac;
		}
	},

	for_each_wdev: function(cb) {
		let wdevs = glob(`/sys/class/ieee80211/${this.name}/device/net/*`);
		wdevs = map(wdevs, (arg) => basename(arg));
		for (let wdev in wdevs) {
			if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != this.name)
				continue;

			cb(wdev);
		}
	}
};

function phy_open(phy)
{
	let phyidx = readfile(`/sys/class/ieee80211/${phy}/index`);
	if (!phyidx)
		return null;

	return proto({
		name: phy,
		idx: int(phyidx)
	}, phy_proto);
}

const vlist_proto = {
	update: function(values, arg) {
		let data = this.data;
		let cb = this.cb;
		let seq = { };
		let new_data = {};
		let old_data = {};

		this.data = new_data;

		if (type(values) == "object") {
			for (let key in values) {
				old_data[key] = data[key];
				new_data[key] = values[key];
				delete data[key];
			}
		} else {
			for (let val in values) {
				let cur_key = val[0];
				let cur_obj = val[1];

				old_data[cur_key] = data[cur_key];
				new_data[cur_key] = val[1];
				delete data[cur_key];
			}
		}

		for (let key in data) {
			cb(null, data[key], arg);
			delete data[key];
		}
		for (let key in new_data)
			cb(new_data[key], old_data[key], arg);
	}
};

function is_equal(val1, val2) {
	let t1 = type(val1);

	if (t1 != type(val2))
		return false;

	if (t1 == "array") {
		if (length(val1) != length(val2))
			return false;

		for (let i = 0; i < length(val1); i++)
			if (!is_equal(val1[i], val2[i]))
				return false;

		return true;
	} else if (t1 == "object") {
		for (let key in val1)
			if (!is_equal(val1[key], val2[key]))
				return false;
		for (let key in val2)
			if (val1[key] == null)
				return false;
		return true;
	} else {
		return val1 == val2;
	}
}

function vlist_new(cb) {
	return proto({
			cb: cb,
			data: {}
		}, vlist_proto);
}

export { wdev_remove, wdev_create, wdev_set_mesh_params, is_equal, vlist_new, phy_is_fullmac, phy_open };