wifi-scripts: fix creation of IBSS in legacy (non-HT) mode
[openwrt/staging/xback.git] / package / network / config / wifi-scripts / files / usr / share / hostap / wdev.uc
1 #!/usr/bin/env ucode
2 'use strict';
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");
6
7 let keep_devices = {};
8 let phy = shift(ARGV);
9 let command = shift(ARGV);
10 let phydev;
11
12 function iface_stop(wdev)
13 {
14 if (keep_devices[wdev.ifname])
15 return;
16
17 wdev_remove(wdev.ifname);
18 }
19
20 function iface_start(wdev)
21 {
22 let ifname = wdev.ifname;
23
24 if (readfile(`/sys/class/net/${ifname}/ifindex`)) {
25 wdev_set_up(ifname, false);
26 wdev_remove(ifname);
27 }
28 let wdev_config = {};
29 for (let key in wdev)
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);
35 let htmode = wdev.htmode || "NOHT";
36 if (wdev.freq)
37 system(`iw dev ${ifname} set freq ${wdev.freq} ${htmode}`);
38 if (wdev.mode == "adhoc") {
39 let cmd = ["iw", "dev", ifname, "ibss", "join", wdev.ssid, wdev.freq, htmode, "fixed-freq" ];
40 if (wdev.bssid)
41 push(cmd, wdev.bssid);
42 for (let key in [ "beacon-interval", "basic-rates", "mcast-rate", "keys" ])
43 if (wdev[key])
44 push(cmd, key, wdev[key]);
45 system(cmd);
46 } else if (wdev.mode == "mesh") {
47 let cmd = [ "iw", "dev", ifname, "mesh", "join", wdev.ssid, "freq", wdev.freq, htmode ];
48 for (let key in [ "mcast-rate", "beacon-interval" ])
49 if (wdev[key])
50 push(cmd, key, wdev[key]);
51 system(cmd);
52
53 wdev_set_mesh_params(ifname, wdev);
54 }
55 }
56
57 function iface_cb(new_if, old_if)
58 {
59 if (old_if && new_if && is_equal(old_if, new_if))
60 return;
61
62 if (old_if)
63 iface_stop(old_if);
64 if (new_if)
65 iface_start(new_if);
66 }
67
68 function drop_inactive(config)
69 {
70 for (let key in config) {
71 if (!readfile(`/sys/class/net/${key}/ifindex`))
72 delete config[key];
73 }
74 }
75
76 function add_ifname(config)
77 {
78 for (let key in config)
79 config[key].ifname = key;
80 }
81
82 function delete_ifname(config)
83 {
84 for (let key in config)
85 delete config[key].ifname;
86 }
87
88 function add_existing(phy, config)
89 {
90 let wdevs = glob(`/sys/class/ieee80211/${phy}/device/net/*`);
91 wdevs = map(wdevs, (arg) => basename(arg));
92 for (let wdev in wdevs) {
93 if (config[wdev])
94 continue;
95
96 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != phy)
97 continue;
98
99 if (trim(readfile(`/sys/class/net/${wdev}/operstate`)) == "down")
100 config[wdev] = {};
101 }
102 }
103
104 function usage()
105 {
106 warn(`Usage: ${basename(sourcepath())} <phy> <command> [<arguments>]
107
108 Commands:
109 set_config <config> [<device]...] - set phy configuration
110 get_macaddr <id> - get phy MAC address for vif index <id>
111 `);
112 exit(1);
113 }
114
115 const commands = {
116 set_config: function(args) {
117 let statefile = `/var/run/wdev-${phy}.json`;
118
119 let new_config = shift(args);
120 for (let dev in ARGV)
121 keep_devices[dev] = true;
122
123 if (!new_config)
124 usage();
125
126 new_config = json(new_config);
127 if (!new_config) {
128 warn("Invalid configuration\n");
129 exit(1);
130 }
131
132 let old_config = readfile(statefile);
133 if (old_config)
134 old_config = json(old_config);
135
136 let config = vlist_new(iface_cb);
137 if (type(old_config) == "object")
138 config.data = old_config;
139
140 add_existing(phy, config.data);
141 add_ifname(config.data);
142 drop_inactive(config.data);
143
144 let ubus = libubus.connect();
145 let data = ubus.call("hostapd", "config_get_macaddr_list", { phy: phy });
146 let macaddr_list = [];
147 if (type(data) == "object" && data.macaddr)
148 macaddr_list = data.macaddr;
149 ubus.disconnect();
150 phydev.macaddr_init(macaddr_list);
151
152 add_ifname(new_config);
153 config.update(new_config);
154
155 drop_inactive(config.data);
156 delete_ifname(config.data);
157 writefile(statefile, sprintf("%J", config.data));
158 },
159 get_macaddr: function(args) {
160 let data = {};
161
162 for (let arg in args) {
163 arg = split(arg, "=", 2);
164 data[arg[0]] = arg[1];
165 }
166
167 let macaddr = phydev.macaddr_generate(data);
168 if (!macaddr) {
169 warn(`Could not get MAC address for phy ${phy}\n`);
170 exit(1);
171 }
172
173 print(macaddr + "\n");
174 },
175 };
176
177 if (!phy || !command | !commands[command])
178 usage();
179
180 phydev = phy_open(phy);
181 if (!phydev) {
182 warn(`PHY ${phy} does not exist\n`);
183 exit(1);
184 }
185
186 commands[command](ARGV);