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(null, 60);
7 hostapd.data.config = {};
8 hostapd.data.pending_config = {};
10 hostapd.data.file_fields = {
13 accept_mac_file: true,
25 function iface_remove(cfg)
27 if (!cfg || !cfg.bss || !cfg.bss[0] || !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=${phy}:${config_inline}`);
105 let iface = hostapd.interfaces[phy];
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_update_supplicant_macaddr(phy, config)
128 let macaddr_list = [];
129 for (let i = 0; i < length(config.bss); i++)
130 push(macaddr_list, config.bss[i].bssid);
131 ubus.defer("wpa_supplicant", "phy_set_macaddr_list", { phy: phy, macaddr: macaddr_list });
134 function __iface_pending_next(pending, state, ret, data)
136 let config = pending.config;
137 let phydev = pending.phydev;
138 let phy = pending.phy;
139 let bss = config.bss[0];
142 pending.defer.abort();
143 delete pending.defer;
146 let macaddr_list = [];
147 for (let i = 0; i < length(config.bss); i++)
148 push(macaddr_list, config.bss[i].bssid);
149 pending.call("wpa_supplicant", "phy_set_macaddr_list", { phy: phy, macaddr: macaddr_list });
152 let err = wdev_create(phy, bss.ifname, { mode: "ap" });
154 hostapd.printf(`Failed to create ${bss.ifname} on phy ${phy}: ${err}`);
158 pending.call("wpa_supplicant", "phy_status", { phy: phy });
161 let phy_status = data;
162 if (phy_status && phy_status.state == "COMPLETED") {
163 if (iface_add(phy, config, phy_status))
166 hostapd.printf(`Failed to bring up phy ${phy} ifname=${bss.ifname} with supplicant provided frequency`);
168 pending.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: true });
169 return "wpas_stopped";
171 if (!iface_add(phy, config))
172 hostapd.printf(`hostapd.add_iface failed for phy ${phy} ifname=${bss.ifname}`);
173 pending.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: false });
177 delete hostapd.data.pending_config[phy];
182 function iface_pending_next(ret, data)
188 this.next_state = __iface_pending_next(cfg, this.next_state, ret, data);
189 if (!this.next_state) {
190 __iface_pending_next(cfg, "done");
193 pending = !this.defer;
197 function iface_pending_abort()
199 this.next_state = "done";
203 function iface_pending_ubus_call(obj, method, arg)
205 let ubus = hostapd.data.ubus;
207 this.defer = ubus.defer(obj, method, arg, (ret, data) => { delete pending.defer; pending.next(ret, data) });
210 const iface_pending_proto = {
211 next: iface_pending_next,
212 call: iface_pending_ubus_call,
213 abort: iface_pending_abort,
216 function iface_pending_init(phydev, config)
218 let phy = phydev.name;
220 let pending = proto({
225 next: iface_pending_next,
226 }, iface_pending_proto);
228 hostapd.data.pending_config[phy] = pending;
232 function iface_restart(phydev, config, old_config)
234 let phy = phydev.name;
235 let pending = hostapd.data.pending_config[phy];
240 hostapd.remove_iface(phy);
241 iface_remove(old_config);
242 iface_remove(config);
244 if (!config.bss || !config.bss[0]) {
245 hostapd.printf(`No bss for phy ${phy}`);
249 phydev.macaddr_init(iface_config_macaddr_list(config));
250 for (let i = 0; i < length(config.bss); i++) {
251 let bss = config.bss[i];
252 if (bss.default_macaddr)
253 bss.bssid = phydev.macaddr_next();
256 iface_pending_init(phydev, config);
259 function array_to_obj(arr, key, start)
264 for (let i = start; i < length(arr); i++) {
272 function find_array_idx(arr, key, val)
274 for (let i = 0; i < length(arr); i++)
275 if (arr[i][key] == val)
281 function bss_reload_psk(bss, config, old_config)
283 if (is_equal(old_config.hash.wpa_psk_file, config.hash.wpa_psk_file))
286 old_config.hash.wpa_psk_file = config.hash.wpa_psk_file;
287 if (!is_equal(old_config, config))
290 let ret = bss.ctrl("RELOAD_WPA_PSK");
293 hostapd.printf(`Reload WPA PSK file for bss ${config.ifname}: ${ret}`);
296 function remove_file_fields(config)
298 return filter(config, (line) => !hostapd.data.file_fields[split(line, "=")[0]]);
301 function bss_remove_file_fields(config)
305 for (let key in config)
306 new_cfg[key] = config[key];
307 new_cfg.data = remove_file_fields(new_cfg.data);
309 for (let key in config.hash)
310 new_cfg.hash[key] = config.hash[key];
311 delete new_cfg.hash.wpa_psk_file;
312 delete new_cfg.hash.vlan_file;
317 function bss_config_hash(config)
319 return hostapd.sha1(remove_file_fields(config) + "");
322 function bss_find_existing(config, prev_config, prev_hash)
324 let hash = bss_config_hash(config.data);
326 for (let i = 0; i < length(prev_config.bss); i++) {
327 if (!prev_hash[i] || hash != prev_hash[i])
337 function get_config_bss(config, idx)
339 if (!config.bss[idx]) {
340 hostapd.printf(`Invalid bss index ${idx}`);
344 let ifname = config.bss[idx].ifname;
346 hostapd.printf(`Could not find bss ${config.bss[idx].ifname}`);
348 return hostapd.bss[ifname];
351 function iface_reload_config(phydev, config, old_config)
353 let phy = phydev.name;
355 if (!old_config || !is_equal(old_config.radio, config.radio))
358 if (is_equal(old_config.bss, config.bss))
361 if (hostapd.data.pending_config[phy])
364 if (!old_config.bss || !old_config.bss[0])
367 let iface = hostapd.interfaces[phy];
368 let iface_name = old_config.bss[0].ifname;
370 hostapd.printf(`Could not find previous interface ${iface_name}`);
374 let first_bss = hostapd.bss[iface_name];
376 hostapd.printf(`Could not find bss of previous interface ${iface_name}`);
380 let macaddr_list = iface_config_macaddr_list(config);
382 let bss_list_cfg = [];
383 let prev_bss_hash = [];
385 for (let bss in old_config.bss) {
386 let hash = bss_config_hash(bss.data);
387 push(prev_bss_hash, bss_config_hash(bss.data));
390 // Step 1: find (possibly renamed) interfaces with the same config
391 // and store them in the new order (with gaps)
392 for (let i = 0; i < length(config.bss); i++) {
395 // For fullmac devices, the first interface needs to be preserved,
396 // since it's treated as the master
397 if (!i && phy_is_fullmac(phy)) {
399 prev_bss_hash[0] = null;
401 prev = bss_find_existing(config.bss[i], old_config, prev_bss_hash);
406 let cur_config = config.bss[i];
407 let prev_config = old_config.bss[prev];
409 let prev_bss = get_config_bss(old_config, prev);
413 // try to preserve MAC address of this BSS by reassigning another
415 if (cur_config.default_macaddr &&
416 !macaddr_list[prev_config.bssid]) {
417 macaddr_list[prev_config.bssid] = i;
418 cur_config.bssid = prev_config.bssid;
421 bss_list[i] = prev_bss;
422 bss_list_cfg[i] = old_config.bss[prev];
425 if (config.mbssid && !bss_list_cfg[0]) {
426 hostapd.printf("First BSS changed with MBSSID enabled");
430 // Step 2: if none were found, rename and preserve the first one
431 if (length(bss_list) == 0) {
432 // can't change the bssid of the first bss
433 if (config.bss[0].bssid != old_config.bss[0].bssid) {
434 if (!config.bss[0].default_macaddr) {
435 hostapd.printf(`BSSID of first interface changed: ${lc(old_config.bss[0].bssid)} -> ${lc(config.bss[0].bssid)}`);
439 config.bss[0].bssid = old_config.bss[0].bssid;
442 let prev_bss = get_config_bss(old_config, 0);
446 macaddr_list[config.bss[0].bssid] = 0;
447 bss_list[0] = prev_bss;
448 bss_list_cfg[0] = old_config.bss[0];
449 prev_bss_hash[0] = null;
452 // Step 3: delete all unused old interfaces
453 for (let i = 0; i < length(prev_bss_hash); i++) {
454 if (!prev_bss_hash[i])
457 let prev_bss = get_config_bss(old_config, i);
461 let ifname = old_config.bss[i].ifname;
462 hostapd.printf(`Remove bss '${ifname}' on phy '${phy}'`);
467 // Step 4: rename preserved interfaces, use temporary name on duplicates
468 let rename_list = [];
469 for (let i = 0; i < length(bss_list); i++) {
473 let old_ifname = bss_list_cfg[i].ifname;
474 let new_ifname = config.bss[i].ifname;
475 if (old_ifname == new_ifname)
478 if (hostapd.bss[new_ifname]) {
479 new_ifname = "tmp_" + substr(hostapd.sha1(new_ifname), 0, 8);
480 push(rename_list, i);
483 hostapd.printf(`Rename bss ${old_ifname} to ${new_ifname}`);
484 if (!bss_list[i].rename(new_ifname)) {
485 hostapd.printf(`Failed to rename bss ${old_ifname} to ${new_ifname}`);
489 bss_list_cfg[i].ifname = new_ifname;
492 // Step 5: rename interfaces with temporary names
493 for (let i in rename_list) {
494 let new_ifname = config.bss[i].ifname;
495 if (!bss_list[i].rename(new_ifname)) {
496 hostapd.printf(`Failed to rename bss to ${new_ifname}`);
499 bss_list_cfg[i].ifname = new_ifname;
502 // Step 6: assign BSSID for newly created interfaces
504 num_global: config.num_global_macaddr ?? 1,
505 mbssid: config.mbssid ?? 0,
507 macaddr_list = phydev.macaddr_init(macaddr_list, macaddr_data);
508 for (let i = 0; i < length(config.bss); i++) {
511 let bsscfg = config.bss[i];
513 let mac_idx = macaddr_list[bsscfg.bssid];
515 macaddr_list[bsscfg.bssid] = i;
519 // statically assigned bssid of the new interface is in conflict
520 // with the bssid of a reused interface. reassign the reused interface
521 if (!bsscfg.default_macaddr) {
522 // can't update bssid of the first BSS, need to restart
526 bsscfg = config.bss[mac_idx];
529 let addr = phydev.macaddr_next(i);
531 hostapd.printf(`Failed to generate mac address for phy ${phy}`);
537 let config_inline = iface_gen_config(phy, config);
539 // Step 7: fill in the gaps with new interfaces
540 for (let i = 0; i < length(config.bss); i++) {
541 let ifname = config.bss[i].ifname;
542 let bss = bss_list[i];
547 hostapd.printf(`Add bss ${ifname} on phy ${phy}`);
548 bss_list[i] = iface.add_bss(config_inline, i);
550 hostapd.printf(`Failed to add new bss ${ifname} on phy ${phy}`);
555 // Step 8: update interface bss order
556 if (!iface.set_bss_order(bss_list)) {
557 hostapd.printf(`Failed to update BSS order on phy '${phy}'`);
561 // Step 9: update config
562 for (let i = 0; i < length(config.bss); i++) {
563 if (!bss_list_cfg[i])
566 let ifname = config.bss[i].ifname;
567 let bss = bss_list[i];
569 if (is_equal(config.bss[i], bss_list_cfg[i]))
572 if (is_equal(bss_remove_file_fields(config.bss[i]),
573 bss_remove_file_fields(bss_list_cfg[i]))) {
574 hostapd.printf(`Update config data files for bss ${ifname}`);
575 if (bss.set_config(config_inline, i, true) < 0) {
576 hostapd.printf(`Could not update config data files for bss ${ifname}`);
579 bss.ctrl("RELOAD_WPA_PSK");
584 bss_reload_psk(bss, config.bss[i], bss_list_cfg[i]);
585 if (is_equal(config.bss[i], bss_list_cfg[i]))
588 hostapd.printf(`Reload config for bss '${config.bss[0].ifname}' on phy '${phy}'`);
589 if (bss.set_config(config_inline, i) < 0) {
590 hostapd.printf(`Failed to set config for bss ${ifname}`);
598 function iface_set_config(phy, config)
600 let old_config = hostapd.data.config[phy];
602 hostapd.data.config[phy] = config;
605 hostapd.remove_iface(phy);
606 return iface_remove(old_config);
609 let phydev = phy_open(phy);
611 hostapd.printf(`Failed to open phy ${phy}`);
616 let ret = iface_reload_config(phydev, config, old_config);
618 iface_update_supplicant_macaddr(phy, config);
619 hostapd.printf(`Reloaded settings for phy ${phy}`);
623 hostapd.printf(`Error reloading config: ${e}\n${e.stacktrace[0].context}`);
626 hostapd.printf(`Restart interface for phy ${phy}`);
627 let ret = iface_restart(phydev, config, old_config);
632 function config_add_bss(config, name)
640 push(config.bss, bss);
645 function iface_load_config(filename)
647 let f = open(filename, "r");
661 while ((line = rtrim(f.read("line"), "\n")) != null) {
662 let val = split(line, "=", 2);
666 if (val[0] == "interface") {
667 bss = config_add_bss(config, val[1]);
671 if (val[0] == "channel") {
672 config.radio.channel = val[1];
676 if (val[0] == "#num_global_macaddr" ||
678 config[val[0]] = int(val[1]);
680 push(config.radio.data, line);
683 while ((line = rtrim(f.read("line"), "\n")) != null) {
684 if (line == "#default_macaddr")
685 bss.default_macaddr = true;
687 let val = split(line, "=", 2);
691 if (val[0] == "bssid") {
692 bss.bssid = lc(val[1]);
696 if (val[0] == "nas_identifier")
699 if (val[0] == "bss") {
700 bss = config_add_bss(config, val[1]);
704 if (hostapd.data.file_fields[val[0]])
705 bss.hash[val[0]] = hostapd.sha1(readfile(val[1]));
707 push(bss.data, line);
714 function ex_wrap(func) {
720 hostapd.printf(`Exception in ubus function: ${e}\n${e.stacktrace[0].context}`);
722 return libubus.STATUS_UNKNOWN_ERROR;
731 call: ex_wrap(function(req) {
732 let phy_list = req.args.phy ? [ req.args.phy ] : keys(hostapd.data.config);
733 for (let phy_name in phy_list) {
734 let phy = hostapd.data.config[phy_name];
735 let config = iface_load_config(phy.orig_file);
736 iface_set_config(phy_name, config);
751 call: ex_wrap(function(req) {
752 if (req.args.up == null || !req.args.phy)
753 return libubus.STATUS_INVALID_ARGUMENT;
755 let phy = req.args.phy;
756 let config = hostapd.data.config[phy];
757 if (!config || !config.bss || !config.bss[0] || !config.bss[0].ifname)
760 let iface = hostapd.interfaces[phy];
769 if (!req.args.frequency)
770 return libubus.STATUS_INVALID_ARGUMENT;
772 let freq_info = iface_freq_info(iface, config, req.args);
774 return libubus.STATUS_UNKNOWN_ERROR;
778 freq_info.csa_count = req.args.csa_count ?? 10;
779 ret = iface.switch_channel(freq_info);
781 ret = iface.start(freq_info);
784 return libubus.STATUS_UNKNOWN_ERROR;
789 config_get_macaddr_list: {
793 call: ex_wrap(function(req) {
794 let phy = req.args.phy;
796 return libubus.STATUS_INVALID_ARGUMENT;
802 let config = hostapd.data.config[phy];
806 ret.macaddr = map(config.bss, (bss) => bss.bssid);
816 call: ex_wrap(function(req) {
817 let phy = req.args.phy;
818 let file = req.args.config;
819 let prev_file = req.args.prev_config;
822 return libubus.STATUS_INVALID_ARGUMENT;
824 if (prev_file && !hostapd.data.config[phy]) {
825 let config = iface_load_config(prev_file);
827 config.radio.data = [];
828 hostapd.data.config[phy] = config;
831 let config = iface_load_config(file);
833 hostapd.printf(`Set new config for phy ${phy}: ${file}`);
834 iface_set_config(phy, config);
837 pid: hostapd.getpid()
846 call: ex_wrap(function(req) {
847 if (!req.args.iface || !req.args.config)
848 return libubus.STATUS_INVALID_ARGUMENT;
850 if (hostapd.add_iface(`bss_config=${req.args.iface}:${req.args.config}`) < 0)
851 return libubus.STATUS_INVALID_ARGUMENT;
854 pid: hostapd.getpid()
862 call: ex_wrap(function(req) {
864 return libubus.STATUS_INVALID_ARGUMENT;
866 hostapd.remove_iface(req.args.iface);
872 hostapd.data.ubus = ubus;
873 hostapd.data.obj = ubus.publish("hostapd", main_obj);
874 hostapd.udebug_set("hostapd", hostapd.data.ubus);
876 function bss_event(type, name, data) {
877 let ubus = hostapd.data.ubus;
881 hostapd.data.obj.notify(`bss.${type}`, data, null, null, null, -1);
882 ubus.call("service", "event", { type: `hostapd.${name}.${type}`, data: {} });
886 shutdown: function() {
887 for (let phy in hostapd.data.config)
888 iface_set_config(phy, null);
889 hostapd.udebug_set(null);
890 hostapd.ubus.disconnect();
892 afc_request: function(iface, data) {
893 let ret = ubus.call("afc", "request", { data });
894 if (type(ret) != "object")
898 bss_add: function(name, obj) {
899 bss_event("add", name);
901 bss_reload: function(name, obj, reconf) {
902 bss_event("reload", name, { reconf: reconf != 0 });
904 bss_remove: function(name, obj) {
905 bss_event("remove", name);