3 import { vlist_new, is_equal, wdev_create, wdev_remove, phy_open } from "/usr/share/hostap/common.uc";
4 import { readfile, writefile, basename, readlink, glob } from "fs";
5 let libubus = require("ubus");
9 let command = shift(ARGV);
13 "mesh_retry_timeout", "mesh_confirm_timeout", "mesh_holding_timeout", "mesh_max_peer_links",
14 "mesh_max_retries", "mesh_ttl", "mesh_element_ttl", "mesh_hwmp_max_preq_retries",
15 "mesh_path_refresh_time", "mesh_min_discovery_timeout", "mesh_hwmp_active_path_timeout",
16 "mesh_hwmp_preq_min_interval", "mesh_hwmp_net_diameter_traversal_time", "mesh_hwmp_rootmode",
17 "mesh_hwmp_rann_interval", "mesh_gate_announcements", "mesh_sync_offset_max_neighor",
18 "mesh_rssi_threshold", "mesh_hwmp_active_path_to_root_timeout", "mesh_hwmp_root_interval",
19 "mesh_hwmp_confirmation_interval", "mesh_awake_window", "mesh_plink_timeout",
20 "mesh_auto_open_plinks", "mesh_fwding", "mesh_power_mode"
23 function iface_stop(wdev)
25 if (keep_devices[wdev.ifname])
28 wdev_remove(wdev.ifname);
31 function iface_start(wdev)
33 let ifname = wdev.ifname;
35 if (readfile(`/sys/class/net/${ifname}/ifindex`)) {
36 system([ "ip", "link", "set", "dev", ifname, "down" ]);
41 wdev_config[key] = wdev[key];
42 if (!wdev_config.macaddr && wdev.mode != "monitor")
43 wdev_config.macaddr = phydev.macaddr_next();
44 wdev_create(phy, ifname, wdev);
45 system([ "ip", "link", "set", "dev", ifname, "up" ]);
47 system(`iw dev ${ifname} set freq ${wdev.freq} ${wdev.htmode}`);
48 if (wdev.mode == "adhoc") {
49 let cmd = ["iw", "dev", ifname, "ibss", "join", wdev.ssid, wdev.freq, wdev.htmode, "fixed-freq" ];
51 push(cmd, wdev.bssid);
52 for (let key in [ "beacon-interval", "basic-rates", "mcast-rate", "keys" ])
54 push(cmd, key, wdev[key]);
56 } else if (wdev.mode == "mesh") {
57 let cmd = [ "iw", "dev", ifname, "mesh", "join", wdev.ssid, "freq", wdev.freq, wdev.htmode ];
58 for (let key in [ "mcast-rate", "beacon-interval" ])
60 push(cmd, key, wdev[key]);
63 cmd = ["iw", "dev", ifname, "set", "mesh_param" ];
64 let len = length(cmd);
66 for (let param in mesh_params)
68 push(cmd, param, wdev[param]);
70 if (len == length(cmd))
78 function iface_cb(new_if, old_if)
80 if (old_if && new_if && is_equal(old_if, new_if))
89 function drop_inactive(config)
91 for (let key in config) {
92 if (!readfile(`/sys/class/net/${key}/ifindex`))
97 function add_ifname(config)
99 for (let key in config)
100 config[key].ifname = key;
103 function delete_ifname(config)
105 for (let key in config)
106 delete config[key].ifname;
109 function add_existing(phy, config)
111 let wdevs = glob(`/sys/class/ieee80211/${phy}/device/net/*`);
112 wdevs = map(wdevs, (arg) => basename(arg));
113 for (let wdev in wdevs) {
117 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != phy)
120 if (trim(readfile(`/sys/class/net/${wdev}/operstate`)) == "down")
127 warn(`Usage: ${basename(sourcepath())} <phy> <command> [<arguments>]
130 set_config <config> [<device]...] - set phy configuration
131 get_macaddr <id> - get phy MAC address for vif index <id>
137 set_config: function(args) {
138 let statefile = `/var/run/wdev-${phy}.json`;
140 let new_config = shift(args);
141 for (let dev in ARGV)
142 keep_devices[dev] = true;
147 new_config = json(new_config);
149 warn("Invalid configuration\n");
153 let old_config = readfile(statefile);
155 old_config = json(old_config);
157 let config = vlist_new(iface_cb);
158 if (type(old_config) == "object")
159 config.data = old_config;
161 add_existing(phy, config.data);
162 add_ifname(config.data);
163 drop_inactive(config.data);
165 let ubus = libubus.connect();
166 let data = ubus.call("hostapd", "config_get_macaddr_list", { phy: phy });
167 let macaddr_list = [];
168 if (type(data) == "object" && data.macaddr)
169 macaddr_list = data.macaddr;
171 phydev.macaddr_init(macaddr_list);
173 add_ifname(new_config);
174 config.update(new_config);
176 drop_inactive(config.data);
177 delete_ifname(config.data);
178 writefile(statefile, sprintf("%J", config.data));
180 get_macaddr: function(args) {
183 for (let arg in args) {
184 arg = split(arg, "=", 2);
185 data[arg[0]] = arg[1];
188 let macaddr = phydev.macaddr_generate(data);
190 warn(`Could not get MAC address for phy ${phy}\n`);
194 print(macaddr + "\n");
198 if (!phy || !command | !commands[command])
201 phydev = phy_open(phy);
203 warn(`PHY ${phy} does not exist\n`);
207 commands[command](ARGV);