3 import { vlist_new, is_equal, wdev_create, wdev_remove, wdev_generate_macaddr } from "/usr/share/hostap/common.uc";
4 import { readfile, writefile, basename, readlink, glob } from "fs";
10 "mesh_retry_timeout", "mesh_confirm_timeout", "mesh_holding_timeout", "mesh_max_peer_links",
11 "mesh_max_retries", "mesh_ttl", "mesh_element_ttl", "mesh_hwmp_max_preq_retries",
12 "mesh_path_refresh_time", "mesh_min_discovery_timeout", "mesh_hwmp_active_path_timeout",
13 "mesh_hwmp_preq_min_interval", "mesh_hwmp_net_diameter_traversal_time", "mesh_hwmp_rootmode",
14 "mesh_hwmp_rann_interval", "mesh_gate_announcements", "mesh_sync_offset_max_neighor",
15 "mesh_rssi_threshold", "mesh_hwmp_active_path_to_root_timeout", "mesh_hwmp_root_interval",
16 "mesh_hwmp_confirmation_interval", "mesh_awake_window", "mesh_plink_timeout",
17 "mesh_auto_open_plinks", "mesh_fwding", "mesh_power_mode"
20 function iface_stop(wdev)
22 if (keep_devices[wdev.ifname])
25 wdev_remove(wdev.ifname);
28 function iface_start(wdev)
30 let ifname = wdev.ifname;
32 if (readfile(`/sys/class/net/${ifname}/ifindex`)) {
33 system([ "ip", "link", "set", "dev", ifname, "down" ]);
36 wdev_create(phy, ifname, wdev);
37 system([ "ip", "link", "set", "dev", ifname, "up" ]);
39 system(`iw dev ${ifname} set freq ${wdev.freq} ${wdev.htmode}`);
40 if (wdev.mode == "adhoc") {
41 let cmd = ["iw", "dev", ifname, "ibss", "join", wdev.ssid, wdev.freq, wdev.htmode, "fixed-freq" ];
43 push(cmd, wdev.bssid);
44 for (let key in [ "beacon-interval", "basic-rates", "mcast-rate", "keys" ])
46 push(cmd, key, wdev[key]);
48 } else if (wdev.mode == "mesh") {
49 let cmd = [ "iw", "dev", ifname, "mesh", "join", wdev.ssid, "freq", wdev.freq, wdev.htmode ];
50 for (let key in [ "mcast-rate", "beacon-interval" ])
52 push(cmd, key, wdev[key]);
55 cmd = ["iw", "dev", ifname, "set", "mesh_param" ];
56 let len = length(cmd);
58 for (let param in mesh_params)
60 push(cmd, param, wdev[param]);
62 if (len == length(cmd))
70 function iface_cb(new_if, old_if)
72 if (old_if && new_if && is_equal(old_if, new_if))
81 function drop_inactive(config)
83 for (let key in config) {
84 if (!readfile(`/sys/class/net/${key}/ifindex`))
89 function add_ifname(config)
91 for (let key in config)
92 config[key].ifname = key;
95 function delete_ifname(config)
97 for (let key in config)
98 delete config[key].ifname;
101 function add_existing(phy, config)
103 let wdevs = glob(`/sys/class/ieee80211/${phy}/device/net/*`);
104 wdevs = map(wdevs, (arg) => basename(arg));
105 for (let wdev in wdevs) {
109 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != phy)
112 if (trim(readfile(`/sys/class/net/${wdev}/operstate`)) == "down")
119 warn(`Usage: ${basename(sourcepath())} <phy> <command> [<arguments>]
122 set_config <config> [<device]...] - set phy configuration
123 get_macaddr <id> - get phy MAC address for vif index <id>
129 set_config: function(args) {
130 let statefile = `/var/run/wdev-${phy}.json`;
132 let new_config = shift(args);
133 for (let dev in ARGV)
134 keep_devices[dev] = true;
139 new_config = json(new_config);
141 warn("Invalid configuration\n");
145 let old_config = readfile(statefile);
147 old_config = json(old_config);
149 let config = vlist_new(iface_cb);
150 if (type(old_config) == "object")
151 config.data = old_config;
153 add_existing(phy, config.data);
154 add_ifname(config.data);
155 drop_inactive(config.data);
157 add_ifname(new_config);
158 config.update(new_config);
160 drop_inactive(config.data);
161 delete_ifname(config.data);
162 writefile(statefile, sprintf("%J", config.data));
164 get_macaddr: function(args) {
167 for (let arg in args) {
168 arg = split(arg, "=", 2);
169 data[arg[0]] = arg[1];
172 let macaddr = wdev_generate_macaddr(phy, data);
174 warn(`Could not get MAC address for phy ${phy}\n`);
178 print(macaddr + "\n");
182 let command = shift(ARGV);
184 if (!phy || !command | !commands[command])
187 if (!readfile(`/sys/class/ieee80211/${phy}/index`)) {
188 warn(`PHY ${phy} does not exist\n`);
192 commands[command](ARGV);