1 let libubus = require("ubus");
2 import { open, readfile } from "fs";
3 import { wdev_create, wdev_remove, is_equal, vlist_new, phy_is_fullmac } from "common";
5 let ubus = libubus.connect();
7 hostapd.data.config = {};
9 hostapd.data.file_fields = {
12 accept_mac_file: true,
24 function iface_remove(cfg)
26 if (!cfg || !cfg.bss || !cfg.bss[0] || !cfg.bss[0].ifname)
29 hostapd.remove_iface(cfg.bss[0].ifname);
30 for (let bss in cfg.bss)
31 wdev_remove(bss.ifname);
34 function iface_gen_config(phy, config)
37 ${join("\n", config.radio.data)}
38 channel=${config.radio.channel}
41 for (let i = 0; i < length(config.bss); i++) {
42 let bss = config.bss[i];
43 let type = i > 0 ? "bss" : "interface";
47 ${join("\n", bss.data)}
54 function iface_restart(phy, config, old_config)
56 iface_remove(old_config);
59 if (!config.bss || !config.bss[0]) {
60 hostapd.printf(`No bss for phy ${phy}`);
64 let bss = config.bss[0];
65 let err = wdev_create(phy, bss.ifname, { mode: "ap" });
67 hostapd.printf(`Failed to create ${bss.ifname} on phy ${phy}: ${err}`);
68 let config_inline = iface_gen_config(phy, config);
70 let ubus = hostapd.data.ubus;
71 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: true });
72 if (hostapd.add_iface(`bss_config=${bss.ifname}:${config_inline}`) < 0)
73 hostapd.printf(`hostapd.add_iface failed for phy ${phy} ifname=${bss.ifname}`);
74 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: false });
77 function array_to_obj(arr, key, start)
82 for (let i = start; i < length(arr); i++) {
90 function find_array_idx(arr, key, val)
92 for (let i = 0; i < length(arr); i++)
93 if (arr[i][key] == val)
99 function bss_reload_psk(bss, config, old_config)
101 if (is_equal(old_config.hash.wpa_psk_file, config.hash.wpa_psk_file))
104 old_config.hash.wpa_psk_file = config.hash.wpa_psk_file;
105 if (!is_equal(old_config, config))
108 let ret = bss.ctrl("RELOAD_WPA_PSK");
111 hostapd.printf(`Reload WPA PSK file for bss ${config.ifname}: ${ret}`);
114 function iface_reload_config(phy, config, old_config)
116 if (!old_config || !is_equal(old_config.radio, config.radio))
119 if (is_equal(old_config.bss, config.bss))
122 if (!old_config.bss || !old_config.bss[0])
125 if (config.bss[0].ifname != old_config.bss[0].ifname)
128 let iface_name = config.bss[0].ifname;
129 let iface = hostapd.interfaces[iface_name];
133 let first_bss = hostapd.bss[iface_name];
137 let config_inline = iface_gen_config(phy, config);
139 bss_reload_psk(first_bss, config.bss[0], old_config.bss[0]);
140 if (!is_equal(config.bss[0], old_config.bss[0])) {
141 if (phy_is_fullmac(phy))
144 if (config.bss[0].bssid != old_config.bss[0].bssid)
147 hostapd.printf(`Reload config for bss '${config.bss[0].ifname}' on phy '${phy}'`);
148 if (first_bss.set_config(config_inline, 0) < 0) {
149 hostapd.printf(`Failed to set config`);
154 let new_cfg = array_to_obj(config.bss, "ifname", 1);
155 let old_cfg = array_to_obj(old_config.bss, "ifname", 1);
157 for (let name in old_cfg) {
158 let bss = hostapd.bss[name];
160 hostapd.printf(`bss '${name}' not found`);
164 if (!new_cfg[name]) {
165 hostapd.printf(`Remove bss '${name}' on phy '${phy}'`);
171 let new_cfg_data = new_cfg[name];
172 delete new_cfg[name];
174 if (is_equal(old_cfg[name], new_cfg_data))
177 hostapd.printf(`Reload config for bss '${name}' on phy '${phy}'`);
178 let idx = find_array_idx(config.bss, "ifname", name);
180 hostapd.printf(`bss index not found`);
184 if (bss.set_config(config_inline, idx) < 0) {
185 hostapd.printf(`Failed to set config`);
190 for (let name in new_cfg) {
191 hostapd.printf(`Add bss '${name}' on phy '${phy}'`);
193 let idx = find_array_idx(config.bss, "ifname", name);
195 hostapd.printf(`bss index not found`);
199 if (iface.add_bss(config_inline, idx) < 0) {
200 hostapd.printf(`Failed to add bss`);
208 function iface_set_config(phy, config)
210 let old_config = hostapd.data.config[phy];
212 hostapd.data.config[phy] = config;
215 return iface_remove(old_config);
217 let ret = iface_reload_config(phy, config, old_config);
219 hostapd.printf(`Reloaded settings for phy ${phy}`);
223 hostapd.printf(`Restart interface for phy ${phy}`);
224 return iface_restart(phy, config, old_config);
227 function config_add_bss(config, name)
235 push(config.bss, bss);
240 function iface_load_config(filename)
242 let f = open(filename, "r");
256 while ((line = trim(f.read("line"))) != null) {
257 let val = split(line, "=", 2);
261 if (val[0] == "interface") {
262 bss = config_add_bss(config, val[1]);
266 if (val[0] == "channel") {
267 config.radio.channel = val[1];
271 push(config.radio.data, line);
274 while ((line = trim(f.read("line"))) != null) {
275 let val = split(line, "=", 2);
279 if (val[0] == "bssid")
282 if (val[0] == "bss") {
283 bss = config_add_bss(config, val[1]);
287 if (hostapd.data.file_fields[val[0]])
288 bss.hash[val[0]] = hostapd.sha1(readfile(val[1]));
290 push(bss.data, line);
304 call: function(req) {
306 let phy_list = req.args.phy ? [ req.args.phy ] : keys(hostapd.data.config);
307 for (let phy_name in phy_list) {
308 let phy = hostapd.data.config[phy_name];
309 let config = iface_load_config(phy.orig_file);
310 iface_set_config(phy_name, config);
313 hostapd.printf(`Error reloading config: ${e}\n${e.stacktrace[0].context}`);
314 return libubus.STATUS_INVALID_ARGUMENT;
329 call: function(req) {
330 if (req.args.up == null || !req.args.phy)
331 return libubus.STATUS_INVALID_ARGUMENT;
333 let phy = req.args.phy;
334 let config = hostapd.data.config[phy];
335 if (!config || !config.bss || !config.bss[0] || !config.bss[0].ifname)
338 let iface = hostapd.interfaces[config.bss[0].ifname];
347 let freq = req.args.frequency;
349 return libubus.STATUS_INVALID_ARGUMENT;
351 let sec_offset = req.args.sec_chan_offset;
352 if (sec_offset != -1 && sec_offset != 1)
356 for (let line in config.radio.data) {
357 if (!sec_offset && match(line, /^ht_capab=.*HT40/)) {
358 sec_offset = null; // auto-detect
362 let val = match(line, /^(vht_oper_chwidth|he_oper_chwidth)=(\d+)/);
374 let freq_info = hostapd.freq_info(freq, sec_offset, width);
376 return libubus.STATUS_UNKNOWN_ERROR;
380 freq_info.csa_count = req.args.csa_count ?? 10;
381 ret = iface.switch_channel(freq_info);
384 ret = iface.start(freq_info);
387 return libubus.STATUS_UNKNOWN_ERROR;
398 call: function(req) {
399 let phy = req.args.phy;
400 let file = req.args.config;
401 let prev_file = req.args.prev_config;
404 return libubus.STATUS_INVALID_ARGUMENT;
407 if (prev_file && !hostapd.data.config[phy]) {
408 let config = iface_load_config(prev_file);
410 config.radio.data = [];
411 hostapd.data.config[phy] = config;
414 let config = iface_load_config(file);
416 hostapd.printf(`Set new config for phy ${phy}: ${file}`);
417 iface_set_config(phy, config);
419 hostapd.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`);
420 return libubus.STATUS_INVALID_ARGUMENT;
424 pid: hostapd.getpid()
433 call: function(req) {
434 if (!req.args.iface || !req.args.config)
435 return libubus.STATUS_INVALID_ARGUMENT;
437 if (hostapd.add_iface(`bss_config=${req.args.iface}:${req.args.config}`) < 0)
438 return libubus.STATUS_INVALID_ARGUMENT;
441 pid: hostapd.getpid()
449 call: function(req) {
451 return libubus.STATUS_INVALID_ARGUMENT;
453 hostapd.remove_iface(req.args.iface);
459 hostapd.data.ubus = ubus;
460 hostapd.data.obj = ubus.publish("hostapd", main_obj);
462 function bss_event(type, name, data) {
463 let ubus = hostapd.data.ubus;
467 hostapd.data.obj.notify(`bss.${type}`, data, null, null, null, -1);
468 ubus.call("service", "event", { type: `hostapd.${name}.${type}`, data: {} });
472 shutdown: function() {
473 for (let phy in hostapd.data.config)
474 iface_set_config(phy, null);
475 hostapd.ubus.disconnect();
477 bss_add: function(name, obj) {
478 bss_event("add", name);
480 bss_reload: function(name, obj, reconf) {
481 bss_event("reload", name, { reconf: reconf != 0 });
483 bss_remove: function(name, obj) {
484 bss_event("remove", name);