3 import { vlist_new, is_equal, wdev_create, wdev_set_mesh_params, wdev_remove, wdev_set_up, 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);
12 function iface_stop(wdev)
14 if (keep_devices[wdev.ifname])
17 wdev_remove(wdev.ifname);
20 function iface_start(wdev)
22 let ifname = wdev.ifname;
24 if (readfile(`/sys/class/net/${ifname}/ifindex`)) {
25 wdev_set_up(ifname, false);
30 wdev_config[key] = wdev[key];
31 if (!wdev_config.macaddr && wdev.mode != "monitor")
32 wdev_config.macaddr = phydev.macaddr_next();
33 wdev_create(phy, ifname, wdev_config);
34 wdev_set_up(ifname, true);
36 system(`iw dev ${ifname} set freq ${wdev.freq} ${wdev.htmode}`);
37 if (wdev.mode == "adhoc") {
38 let cmd = ["iw", "dev", ifname, "ibss", "join", wdev.ssid, wdev.freq, wdev.htmode, "fixed-freq" ];
40 push(cmd, wdev.bssid);
41 for (let key in [ "beacon-interval", "basic-rates", "mcast-rate", "keys" ])
43 push(cmd, key, wdev[key]);
45 } else if (wdev.mode == "mesh") {
46 let cmd = [ "iw", "dev", ifname, "mesh", "join", wdev.ssid, "freq", wdev.freq, wdev.htmode ];
47 for (let key in [ "mcast-rate", "beacon-interval" ])
49 push(cmd, key, wdev[key]);
52 wdev_set_mesh_params(ifname, wdev);
56 function iface_cb(new_if, old_if)
58 if (old_if && new_if && is_equal(old_if, new_if))
67 function drop_inactive(config)
69 for (let key in config) {
70 if (!readfile(`/sys/class/net/${key}/ifindex`))
75 function add_ifname(config)
77 for (let key in config)
78 config[key].ifname = key;
81 function delete_ifname(config)
83 for (let key in config)
84 delete config[key].ifname;
87 function add_existing(phy, config)
89 let wdevs = glob(`/sys/class/ieee80211/${phy}/device/net/*`);
90 wdevs = map(wdevs, (arg) => basename(arg));
91 for (let wdev in wdevs) {
95 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != phy)
98 if (trim(readfile(`/sys/class/net/${wdev}/operstate`)) == "down")
105 warn(`Usage: ${basename(sourcepath())} <phy> <command> [<arguments>]
108 set_config <config> [<device]...] - set phy configuration
109 get_macaddr <id> - get phy MAC address for vif index <id>
115 set_config: function(args) {
116 let statefile = `/var/run/wdev-${phy}.json`;
118 let new_config = shift(args);
119 for (let dev in ARGV)
120 keep_devices[dev] = true;
125 new_config = json(new_config);
127 warn("Invalid configuration\n");
131 let old_config = readfile(statefile);
133 old_config = json(old_config);
135 let config = vlist_new(iface_cb);
136 if (type(old_config) == "object")
137 config.data = old_config;
139 add_existing(phy, config.data);
140 add_ifname(config.data);
141 drop_inactive(config.data);
143 let ubus = libubus.connect();
144 let data = ubus.call("hostapd", "config_get_macaddr_list", { phy: phy });
145 let macaddr_list = [];
146 if (type(data) == "object" && data.macaddr)
147 macaddr_list = data.macaddr;
149 phydev.macaddr_init(macaddr_list);
151 add_ifname(new_config);
152 config.update(new_config);
154 drop_inactive(config.data);
155 delete_ifname(config.data);
156 writefile(statefile, sprintf("%J", config.data));
158 get_macaddr: function(args) {
161 for (let arg in args) {
162 arg = split(arg, "=", 2);
163 data[arg[0]] = arg[1];
166 let macaddr = phydev.macaddr_generate(data);
168 warn(`Could not get MAC address for phy ${phy}\n`);
172 print(macaddr + "\n");
176 if (!phy || !command | !commands[command])
179 phydev = phy_open(phy);
181 warn(`PHY ${phy} does not exist\n`);
185 commands[command](ARGV);