1 let libubus = require("ubus");
2 import { open, readfile } from "fs";
3 import { wdev_create, wdev_remove, is_equal, vlist_new, phy_is_fullmac, phy_open } 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, start_disabled)
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";
44 let nasid = bss.nasid ?? replace(bss.bssid, ":", "");
49 ${join("\n", bss.data)}
50 nas_identifier=${nasid}
61 function iface_freq_info(iface, config, params)
63 let freq = params.frequency;
67 let sec_offset = params.sec_chan_offset;
68 if (sec_offset != -1 && sec_offset != 1)
72 for (let line in config.radio.data) {
73 if (!sec_offset && match(line, /^ht_capab=.*HT40/)) {
74 sec_offset = null; // auto-detect
78 let val = match(line, /^(vht_oper_chwidth|he_oper_chwidth)=(\d+)/);
90 return hostapd.freq_info(freq, sec_offset, width);
93 function iface_add(phy, config, phy_status)
95 let config_inline = iface_gen_config(phy, config, !!phy_status);
97 let bss = config.bss[0];
98 let ret = hostapd.add_iface(`bss_config=${bss.ifname}:${config_inline}`);
105 let iface = hostapd.interfaces[bss.ifname];
109 let freq_info = iface_freq_info(iface, config, phy_status);
111 return iface.start(freq_info) >= 0;
114 function iface_config_macaddr_list(config)
116 let macaddr_list = {};
117 for (let i = 0; i < length(config.bss); i++) {
118 let bss = config.bss[i];
119 if (!bss.default_macaddr)
120 macaddr_list[bss.bssid] = i;
126 function iface_restart(phydev, config, old_config)
128 let phy = phydev.name;
130 iface_remove(old_config);
131 iface_remove(config);
133 if (!config.bss || !config.bss[0]) {
134 hostapd.printf(`No bss for phy ${phy}`);
138 phydev.macaddr_init(iface_config_macaddr_list(config));
139 for (let i = 0; i < length(config.bss); i++) {
140 let bss = config.bss[i];
141 if (bss.default_macaddr)
142 bss.bssid = phydev.macaddr_next();
145 let bss = config.bss[0];
146 let err = wdev_create(phy, bss.ifname, { mode: "ap" });
148 hostapd.printf(`Failed to create ${bss.ifname} on phy ${phy}: ${err}`);
150 let ubus = hostapd.data.ubus;
151 let phy_status = ubus.call("wpa_supplicant", "phy_status", { phy: phy });
152 if (phy_status && phy_status.state == "COMPLETED") {
153 if (iface_add(phy, config, phy_status))
156 hostapd.printf(`Failed to bring up phy ${phy} ifname=${bss.ifname} with supplicant provided frequency`);
159 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: true });
160 if (!iface_add(phy, config))
161 hostapd.printf(`hostapd.add_iface failed for phy ${phy} ifname=${bss.ifname}`);
162 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: false });
165 function array_to_obj(arr, key, start)
170 for (let i = start; i < length(arr); i++) {
178 function find_array_idx(arr, key, val)
180 for (let i = 0; i < length(arr); i++)
181 if (arr[i][key] == val)
187 function bss_reload_psk(bss, config, old_config)
189 if (is_equal(old_config.hash.wpa_psk_file, config.hash.wpa_psk_file))
192 old_config.hash.wpa_psk_file = config.hash.wpa_psk_file;
193 if (!is_equal(old_config, config))
196 let ret = bss.ctrl("RELOAD_WPA_PSK");
199 hostapd.printf(`Reload WPA PSK file for bss ${config.ifname}: ${ret}`);
202 function remove_file_fields(config)
204 return filter(config, (line) => !hostapd.data.file_fields[split(line, "=")[0]]);
207 function bss_remove_file_fields(config)
211 for (let key in config)
212 new_cfg[key] = config[key];
213 new_cfg.data = remove_file_fields(new_cfg.data);
215 for (let key in config.hash)
216 new_cfg.hash[key] = config.hash[key];
217 delete new_cfg.hash.wpa_psk_file;
222 function bss_config_hash(config)
224 return hostapd.sha1(remove_file_fields(config) + "");
227 function bss_find_existing(config, prev_config, prev_hash)
229 let hash = bss_config_hash(config.data);
231 for (let i = 0; i < length(prev_config.bss); i++) {
232 if (!prev_hash[i] || hash != prev_hash[i])
242 function get_config_bss(config, idx)
244 if (!config.bss[idx]) {
245 hostapd.printf(`Invalid bss index ${idx}`);
249 let ifname = config.bss[idx].ifname;
251 hostapd.printf(`Could not find bss ${config.bss[idx].ifname}`);
253 return hostapd.bss[ifname];
256 function iface_reload_config(phydev, config, old_config)
258 let phy = phydev.name;
260 if (!old_config || !is_equal(old_config.radio, config.radio))
263 if (is_equal(old_config.bss, config.bss))
266 if (!old_config.bss || !old_config.bss[0])
269 let iface_name = old_config.bss[0].ifname;
270 let iface = hostapd.interfaces[iface_name];
272 hostapd.printf(`Could not find previous interface ${iface_name}`);
276 let first_bss = hostapd.bss[iface_name];
278 hostapd.printf(`Could not find bss of previous interface ${iface_name}`);
282 let macaddr_list = iface_config_macaddr_list(config);
284 let bss_list_cfg = [];
285 let prev_bss_hash = [];
287 for (let bss in old_config.bss) {
288 let hash = bss_config_hash(bss.data);
289 push(prev_bss_hash, bss_config_hash(bss.data));
292 // Step 1: find (possibly renamed) interfaces with the same config
293 // and store them in the new order (with gaps)
294 for (let i = 0; i < length(config.bss); i++) {
297 // For fullmac devices, the first interface needs to be preserved,
298 // since it's treated as the master
299 if (!i && phy_is_fullmac(phy)) {
301 prev_bss_hash[0] = null;
303 prev = bss_find_existing(config.bss[i], old_config, prev_bss_hash);
308 let cur_config = config.bss[i];
309 let prev_config = old_config.bss[prev];
311 let prev_bss = get_config_bss(old_config, prev);
315 // try to preserve MAC address of this BSS by reassigning another
317 if (cur_config.default_macaddr &&
318 !macaddr_list[prev_config.bssid]) {
319 macaddr_list[prev_config.bssid] = i;
320 cur_config.bssid = prev_config.bssid;
323 bss_list[i] = prev_bss;
324 bss_list_cfg[i] = old_config.bss[prev];
327 if (config.mbssid && !bss_list_cfg[0]) {
328 hostapd.printf("First BSS changed with MBSSID enabled");
332 // Step 2: if none were found, rename and preserve the first one
333 if (length(bss_list) == 0) {
334 // can't change the bssid of the first bss
335 if (config.bss[0].bssid != old_config.bss[0].bssid) {
336 if (!config.bss[0].default_macaddr) {
337 hostapd.printf(`BSSID of first interface changed: ${lc(old_config.bss[0].bssid)} -> ${lc(config.bss[0].bssid)}`);
341 config.bss[0].bssid = old_config.bss[0].bssid;
344 let prev_bss = get_config_bss(old_config, 0);
348 macaddr_list[config.bss[0].bssid] = 0;
349 bss_list[0] = prev_bss;
350 bss_list_cfg[0] = old_config.bss[0];
351 prev_bss_hash[0] = null;
354 // Step 3: delete all unused old interfaces
355 for (let i = 0; i < length(prev_bss_hash); i++) {
356 if (!prev_bss_hash[i])
359 let prev_bss = get_config_bss(old_config, i);
363 let ifname = old_config.bss[i].ifname;
364 hostapd.printf(`Remove bss '${ifname}' on phy '${phy}'`);
369 // Step 4: rename preserved interfaces, use temporary name on duplicates
370 let rename_list = [];
371 for (let i = 0; i < length(bss_list); i++) {
375 let old_ifname = bss_list_cfg[i].ifname;
376 let new_ifname = config.bss[i].ifname;
377 if (old_ifname == new_ifname)
380 if (hostapd.bss[new_ifname]) {
381 new_ifname = "tmp_" + substr(hostapd.sha1(new_ifname), 0, 8);
382 push(rename_list, i);
385 hostapd.printf(`Rename bss ${old_ifname} to ${new_ifname}`);
386 if (!bss_list[i].rename(new_ifname)) {
387 hostapd.printf(`Failed to rename bss ${old_ifname} to ${new_ifname}`);
391 bss_list_cfg[i].ifname = new_ifname;
394 // Step 5: rename interfaces with temporary names
395 for (let i in rename_list) {
396 let new_ifname = config.bss[i].ifname;
397 if (!bss_list[i].rename(new_ifname)) {
398 hostapd.printf(`Failed to rename bss to ${new_ifname}`);
401 bss_list_cfg[i].ifname = new_ifname;
404 // Step 6: assign BSSID for newly created interfaces
406 num_global: config.num_global_macaddr ?? 1,
407 mbssid: config.mbssid ?? 0,
409 macaddr_list = phydev.macaddr_init(macaddr_list, macaddr_data);
410 for (let i = 0; i < length(config.bss); i++) {
413 let bsscfg = config.bss[i];
415 let mac_idx = macaddr_list[bsscfg.bssid];
417 macaddr_list[bsscfg.bssid] = i;
421 // statically assigned bssid of the new interface is in conflict
422 // with the bssid of a reused interface. reassign the reused interface
423 if (!bsscfg.default_macaddr) {
424 // can't update bssid of the first BSS, need to restart
428 bsscfg = config.bss[mac_idx];
431 let addr = phydev.macaddr_next(i);
433 hostapd.printf(`Failed to generate mac address for phy ${phy}`);
439 let config_inline = iface_gen_config(phy, config);
441 // Step 7: fill in the gaps with new interfaces
442 for (let i = 0; i < length(config.bss); i++) {
443 let ifname = config.bss[i].ifname;
444 let bss = bss_list[i];
449 hostapd.printf(`Add bss ${ifname} on phy ${phy}`);
450 bss_list[i] = iface.add_bss(config_inline, i);
452 hostapd.printf(`Failed to add new bss ${ifname} on phy ${phy}`);
457 // Step 8: update interface bss order
458 if (!iface.set_bss_order(bss_list)) {
459 hostapd.printf(`Failed to update BSS order on phy '${phy}'`);
463 // Step 9: update config
464 for (let i = 0; i < length(config.bss); i++) {
465 if (!bss_list_cfg[i])
468 let ifname = config.bss[i].ifname;
469 let bss = bss_list[i];
471 if (is_equal(config.bss[i], bss_list_cfg[i]))
474 if (is_equal(bss_remove_file_fields(config.bss[i]),
475 bss_remove_file_fields(bss_list_cfg[i]))) {
476 hostapd.printf(`Update config data files for bss ${ifname}`);
477 if (bss.set_config(config_inline, i, true) < 0) {
478 hostapd.printf(`Failed to update config data files for bss ${ifname}`);
481 bss.ctrl("RELOAD_WPA_PSK");
485 bss_reload_psk(bss, config.bss[i], bss_list_cfg[i]);
486 if (is_equal(config.bss[i], bss_list_cfg[i]))
489 hostapd.printf(`Reload config for bss '${config.bss[0].ifname}' on phy '${phy}'`);
490 hostapd.printf(`old: ${bss_remove_file_fields(bss_list_cfg[i])}`);
491 hostapd.printf(`new: ${bss_remove_file_fields(config.bss[i])}`);
492 if (bss.set_config(config_inline, i) < 0) {
493 hostapd.printf(`Failed to set config for bss ${ifname}`);
501 function iface_update_supplicant_macaddr(phy, config)
503 let macaddr_list = [];
504 for (let i = 0; i < length(config.bss); i++)
505 push(macaddr_list, config.bss[i].bssid);
506 ubus.call("wpa_supplicant", "phy_set_macaddr_list", { phy: phy, macaddr: macaddr_list });
509 function iface_set_config(phy, config)
511 let old_config = hostapd.data.config[phy];
513 hostapd.data.config[phy] = config;
516 return iface_remove(old_config);
518 let phydev = phy_open(phy);
520 hostapd.printf(`Failed to open phy ${phy}`);
525 let ret = iface_reload_config(phydev, config, old_config);
527 iface_update_supplicant_macaddr(phy, config);
528 hostapd.printf(`Reloaded settings for phy ${phy}`);
532 hostapd.printf(`Error reloading config: ${e}\n${e.stacktrace[0].context}`);
535 hostapd.printf(`Restart interface for phy ${phy}`);
536 let ret = iface_restart(phydev, config, old_config);
537 iface_update_supplicant_macaddr(phy, config);
542 function config_add_bss(config, name)
550 push(config.bss, bss);
555 function iface_load_config(filename)
557 let f = open(filename, "r");
571 while ((line = trim(f.read("line"))) != null) {
572 let val = split(line, "=", 2);
576 if (val[0] == "interface") {
577 bss = config_add_bss(config, val[1]);
581 if (val[0] == "channel") {
582 config.radio.channel = val[1];
586 if (val[0] == "#num_global_macaddr" ||
588 config[val[0]] = int(val[1]);
590 push(config.radio.data, line);
593 while ((line = trim(f.read("line"))) != null) {
594 if (line == "#default_macaddr")
595 bss.default_macaddr = true;
597 let val = split(line, "=", 2);
601 if (val[0] == "bssid") {
602 bss.bssid = lc(val[1]);
606 if (val[0] == "nas_identifier")
609 if (val[0] == "bss") {
610 bss = config_add_bss(config, val[1]);
614 if (hostapd.data.file_fields[val[0]])
615 bss.hash[val[0]] = hostapd.sha1(readfile(val[1]));
617 push(bss.data, line);
624 function ex_wrap(func) {
630 hostapd.printf(`Exception in ubus function: ${e}\n${e.stacktrace[0].context}`);
632 return libubus.STATUS_UNKNOWN_ERROR;
641 call: ex_wrap(function(req) {
642 let phy_list = req.args.phy ? [ req.args.phy ] : keys(hostapd.data.config);
643 for (let phy_name in phy_list) {
644 let phy = hostapd.data.config[phy_name];
645 let config = iface_load_config(phy.orig_file);
646 iface_set_config(phy_name, config);
661 call: ex_wrap(function(req) {
662 if (req.args.up == null || !req.args.phy)
663 return libubus.STATUS_INVALID_ARGUMENT;
665 let phy = req.args.phy;
666 let config = hostapd.data.config[phy];
667 if (!config || !config.bss || !config.bss[0] || !config.bss[0].ifname)
670 let iface = hostapd.interfaces[config.bss[0].ifname];
679 if (!req.args.frequency)
680 return libubus.STATUS_INVALID_ARGUMENT;
682 let freq_info = iface_freq_info(iface, config, req.args);
684 return libubus.STATUS_UNKNOWN_ERROR;
688 freq_info.csa_count = req.args.csa_count ?? 10;
689 ret = iface.switch_channel(freq_info);
691 ret = iface.start(freq_info);
694 return libubus.STATUS_UNKNOWN_ERROR;
699 config_get_macaddr_list: {
703 call: ex_wrap(function(req) {
704 let phy = req.args.phy;
706 return libubus.STATUS_INVALID_ARGUMENT;
712 let config = hostapd.data.config[phy];
716 ret.macaddr = map(config.bss, (bss) => bss.bssid);
726 call: ex_wrap(function(req) {
727 let phy = req.args.phy;
728 let file = req.args.config;
729 let prev_file = req.args.prev_config;
732 return libubus.STATUS_INVALID_ARGUMENT;
734 if (prev_file && !hostapd.data.config[phy]) {
735 let config = iface_load_config(prev_file);
737 config.radio.data = [];
738 hostapd.data.config[phy] = config;
741 let config = iface_load_config(file);
743 hostapd.printf(`Set new config for phy ${phy}: ${file}`);
744 iface_set_config(phy, config);
747 pid: hostapd.getpid()
756 call: ex_wrap(function(req) {
757 if (!req.args.iface || !req.args.config)
758 return libubus.STATUS_INVALID_ARGUMENT;
760 if (hostapd.add_iface(`bss_config=${req.args.iface}:${req.args.config}`) < 0)
761 return libubus.STATUS_INVALID_ARGUMENT;
764 pid: hostapd.getpid()
772 call: ex_wrap(function(req) {
774 return libubus.STATUS_INVALID_ARGUMENT;
776 hostapd.remove_iface(req.args.iface);
782 hostapd.data.ubus = ubus;
783 hostapd.data.obj = ubus.publish("hostapd", main_obj);
785 function bss_event(type, name, data) {
786 let ubus = hostapd.data.ubus;
790 hostapd.data.obj.notify(`bss.${type}`, data, null, null, null, -1);
791 ubus.call("service", "event", { type: `hostapd.${name}.${type}`, data: {} });
795 shutdown: function() {
796 for (let phy in hostapd.data.config)
797 iface_set_config(phy, null);
798 hostapd.ubus.disconnect();
800 bss_add: function(name, obj) {
801 bss_event("add", name);
803 bss_reload: function(name, obj, reconf) {
804 bss_event("reload", name, { reconf: reconf != 0 });
806 bss_remove: function(name, obj) {
807 bss_event("remove", name);