1 import * as nl80211 from "nl80211";
2 import * as rtnl from "rtnl";
3 import { readfile, glob, basename, readlink } from "fs";
6 ap: nl80211.const.NL80211_IFTYPE_AP,
7 mesh: nl80211.const.NL80211_IFTYPE_MESH_POINT,
8 sta: nl80211.const.NL80211_IFTYPE_STATION,
9 adhoc: nl80211.const.NL80211_IFTYPE_ADHOC,
10 monitor: nl80211.const.NL80211_IFTYPE_MONITOR,
14 mesh_retry_timeout: "retry_timeout",
15 mesh_confirm_timeout: "confirm_timeout",
16 mesh_holding_timeout: "holding_timeout",
17 mesh_max_peer_links: "max_peer_links",
18 mesh_max_retries: "max_retries",
20 mesh_element_ttl: "element_ttl",
21 mesh_auto_open_plinks: "auto_open_plinks",
22 mesh_hwmp_max_preq_retries: "hwmp_max_preq_retries",
23 mesh_path_refresh_time: "path_refresh_time",
24 mesh_min_discovery_timeout: "min_discovery_timeout",
25 mesh_hwmp_active_path_timeout: "hwmp_active_path_timeout",
26 mesh_hwmp_preq_min_interval: "hwmp_preq_min_interval",
27 mesh_hwmp_net_diameter_traversal_time: "hwmp_net_diam_trvs_time",
28 mesh_hwmp_rootmode: "hwmp_rootmode",
29 mesh_hwmp_rann_interval: "hwmp_rann_interval",
30 mesh_gate_announcements: "gate_announcements",
31 mesh_sync_offset_max_neighor: "sync_offset_max_neighbor",
32 mesh_rssi_threshold: "rssi_threshold",
33 mesh_hwmp_active_path_to_root_timeout: "hwmp_path_to_root_timeout",
34 mesh_hwmp_root_interval: "hwmp_root_interval",
35 mesh_hwmp_confirmation_interval: "hwmp_confirmation_interval",
36 mesh_awake_window: "awake_window",
37 mesh_plink_timeout: "plink_timeout",
38 mesh_fwding: "forwarding",
39 mesh_power_mode: "power_mode",
40 mesh_nolearn: "nolearn"
43 function wdev_remove(name)
45 nl80211.request(nl80211.const.NL80211_CMD_DEL_INTERFACE, 0, { dev: name });
48 function __phy_is_fullmac(phyidx)
50 let data = nl80211.request(nl80211.const.NL80211_CMD_GET_WIPHY, 0, { wiphy: phyidx });
52 return !data.software_iftypes.ap_vlan;
55 function phy_is_fullmac(phy)
57 let phyidx = int(trim(readfile(`/sys/class/ieee80211/${phy}/index`)));
59 return __phy_is_fullmac(phyidx);
62 function find_reusable_wdev(phyidx)
64 if (!__phy_is_fullmac(phyidx))
67 let data = nl80211.request(
68 nl80211.const.NL80211_CMD_GET_INTERFACE,
69 nl80211.const.NLM_F_DUMP,
72 if (trim(readfile(`/sys/class/net/${res.ifname}/operstate`)) == "down")
77 function wdev_create(phy, name, data)
79 let phyidx = int(readfile(`/sys/class/ieee80211/${phy}/index`));
83 if (!iftypes[data.mode])
84 return `Invalid mode: ${data.mode}`;
89 iftype: iftypes[data.mode],
93 req["4addr"] = data["4addr"];
95 req.mac = data.macaddr;
99 let reuse_ifname = find_reusable_wdev(phyidx);
101 (reuse_ifname == name ||
102 rtnl.request(rtnl.const.RTM_SETLINK, 0, { dev: reuse_ifname, ifname: name}) != false))
104 nl80211.const.NL80211_CMD_SET_INTERFACE, 0, {
107 iftype: iftypes[data.mode],
111 nl80211.const.NL80211_CMD_NEW_INTERFACE,
112 nl80211.const.NLM_F_CREATE,
115 let error = nl80211.error();
119 if (data.powersave != null) {
120 nl80211.request(nl80211.const.NL80211_CMD_SET_POWER_SAVE, 0,
121 { dev: name, ps_state: data.powersave ? 1 : 0});
127 function wdev_set_mesh_params(name, data)
131 for (let key in mesh_params) {
135 mesh_cfg[mesh_params[key]] = int(val);
138 if (!length(mesh_cfg))
141 nl80211.request(nl80211.const.NL80211_CMD_SET_MESH_CONFIG, 0,
142 { dev: name, mesh_params: mesh_cfg });
144 return nl80211.error();
147 function wdev_set_up(name, up)
149 rtnl.request(rtnl.const.RTM_SETLINK, 0, { dev: name, change: 1, flags: up ? 1 : 0 });
152 function phy_sysfs_file(phy, name)
154 return trim(readfile(`/sys/class/ieee80211/${phy}/${name}`));
157 function macaddr_split(str)
159 return map(split(str, ":"), (val) => hex(val));
162 function macaddr_join(addr)
164 return join(":", map(addr, (val) => sprintf("%02x", val)));
167 function wdev_macaddr(wdev)
169 return trim(readfile(`/sys/class/net/${wdev}/address`));
173 macaddr_init: function(used, options) {
174 this.macaddr_options = options ?? {};
175 this.macaddr_list = {};
177 if (type(used) == "object")
178 for (let addr in used)
179 this.macaddr_list[addr] = used[addr];
181 for (let addr in used)
182 this.macaddr_list[addr] = -1;
184 this.for_each_wdev((wdev) => {
185 let macaddr = wdev_macaddr(wdev);
186 this.macaddr_list[macaddr] ??= -1;
189 return this.macaddr_list;
192 macaddr_generate: function(data) {
194 let idx = int(data.id ?? 0);
195 let mbssid = int(data.mbssid ?? 0) > 0;
196 let num_global = int(data.num_global ?? 1);
197 let use_global = !mbssid && idx < num_global;
199 let base_addr = phy_sysfs_file(phy, "macaddress");
206 let base_mask = phy_sysfs_file(phy, "address_mask");
210 if (base_mask == "00:00:00:00:00:00" && idx >= num_global) {
211 let addrs = split(phy_sysfs_file(phy, "addresses"), "\n");
213 if (idx < length(addrs))
216 base_mask = "ff:ff:ff:ff:ff:ff";
219 let addr = macaddr_split(base_addr);
220 let mask = macaddr_split(base_mask);
227 else if (mask[0] > 0)
229 else if (mask[5] < 0xff)
247 for (let i = 5; i > 0; i--) {
256 return macaddr_join(addr);
259 macaddr_next: function(val) {
260 let data = this.macaddr_options ?? {};
261 let list = this.macaddr_list;
263 for (let i = 0; i < 32; i++) {
266 let mac = this.macaddr_generate(data);
270 if (list[mac] != null)
273 list[mac] = val != null ? val : -1;
278 for_each_wdev: function(cb) {
279 let wdevs = glob(`/sys/class/ieee80211/${this.name}/device/net/*`);
280 wdevs = map(wdevs, (arg) => basename(arg));
281 for (let wdev in wdevs) {
282 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != this.name)
290 function phy_open(phy)
292 let phyidx = readfile(`/sys/class/ieee80211/${phy}/index`);
302 const vlist_proto = {
303 update: function(values, arg) {
304 let data = this.data;
310 this.data = new_data;
312 if (type(values) == "object") {
313 for (let key in values) {
314 old_data[key] = data[key];
315 new_data[key] = values[key];
319 for (let val in values) {
320 let cur_key = val[0];
321 let cur_obj = val[1];
323 old_data[cur_key] = data[cur_key];
324 new_data[cur_key] = val[1];
325 delete data[cur_key];
329 for (let key in data) {
330 cb(null, data[key], arg);
333 for (let key in new_data)
334 cb(new_data[key], old_data[key], arg);
338 function is_equal(val1, val2) {
341 if (t1 != type(val2))
345 if (length(val1) != length(val2))
348 for (let i = 0; i < length(val1); i++)
349 if (!is_equal(val1[i], val2[i]))
353 } else if (t1 == "object") {
354 for (let key in val1)
355 if (!is_equal(val1[key], val2[key]))
357 for (let key in val2)
358 if (val1[key] == null)
366 function vlist_new(cb) {
373 export { wdev_remove, wdev_create, wdev_set_mesh_params, wdev_set_up, is_equal, vlist_new, phy_is_fullmac, phy_open };