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 phy_sysfs_file(phy, name)
149 return trim(readfile(`/sys/class/ieee80211/${phy}/${name}`));
152 function macaddr_split(str)
154 return map(split(str, ":"), (val) => hex(val));
157 function macaddr_join(addr)
159 return join(":", map(addr, (val) => sprintf("%02x", val)));
162 function wdev_macaddr(wdev)
164 return trim(readfile(`/sys/class/net/${wdev}/address`));
168 macaddr_init: function(used, options) {
169 this.macaddr_options = options ?? {};
170 this.macaddr_list = {};
172 if (type(used) == "object")
173 for (let addr in used)
174 this.macaddr_list[addr] = used[addr];
176 for (let addr in used)
177 this.macaddr_list[addr] = -1;
179 this.for_each_wdev((wdev) => {
180 let macaddr = wdev_macaddr(wdev);
181 this.macaddr_list[macaddr] ??= -1;
184 return this.macaddr_list;
187 macaddr_generate: function(data) {
189 let idx = int(data.id ?? 0);
190 let mbssid = int(data.mbssid ?? 0) > 0;
191 let num_global = int(data.num_global ?? 1);
192 let use_global = !mbssid && idx < num_global;
194 let base_addr = phy_sysfs_file(phy, "macaddress");
201 let base_mask = phy_sysfs_file(phy, "address_mask");
205 if (base_mask == "00:00:00:00:00:00" && idx >= num_global) {
206 let addrs = split(phy_sysfs_file(phy, "addresses"), "\n");
208 if (idx < length(addrs))
211 base_mask = "ff:ff:ff:ff:ff:ff";
214 let addr = macaddr_split(base_addr);
215 let mask = macaddr_split(base_mask);
222 else if (mask[0] > 0)
224 else if (mask[5] < 0xff)
242 for (let i = 5; i > 0; i--) {
251 return macaddr_join(addr);
254 macaddr_next: function(val) {
255 let data = this.macaddr_options ?? {};
256 let list = this.macaddr_list;
258 for (let i = 0; i < 32; i++) {
261 let mac = this.macaddr_generate(data);
265 if (list[mac] != null)
268 list[mac] = val != null ? val : -1;
273 for_each_wdev: function(cb) {
274 let wdevs = glob(`/sys/class/ieee80211/${this.name}/device/net/*`);
275 wdevs = map(wdevs, (arg) => basename(arg));
276 for (let wdev in wdevs) {
277 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != this.name)
285 function phy_open(phy)
287 let phyidx = readfile(`/sys/class/ieee80211/${phy}/index`);
297 const vlist_proto = {
298 update: function(values, arg) {
299 let data = this.data;
305 this.data = new_data;
307 if (type(values) == "object") {
308 for (let key in values) {
309 old_data[key] = data[key];
310 new_data[key] = values[key];
314 for (let val in values) {
315 let cur_key = val[0];
316 let cur_obj = val[1];
318 old_data[cur_key] = data[cur_key];
319 new_data[cur_key] = val[1];
320 delete data[cur_key];
324 for (let key in data) {
325 cb(null, data[key], arg);
328 for (let key in new_data)
329 cb(new_data[key], old_data[key], arg);
333 function is_equal(val1, val2) {
336 if (t1 != type(val2))
340 if (length(val1) != length(val2))
343 for (let i = 0; i < length(val1); i++)
344 if (!is_equal(val1[i], val2[i]))
348 } else if (t1 == "object") {
349 for (let key in val1)
350 if (!is_equal(val1[key], val2[key]))
352 for (let key in val2)
353 if (val1[key] == null)
361 function vlist_new(cb) {
368 export { wdev_remove, wdev_create, wdev_set_mesh_params, is_equal, vlist_new, phy_is_fullmac, phy_open };