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;
218 delete new_cfg.hash.vlan_file;
223 function bss_config_hash(config)
225 return hostapd.sha1(remove_file_fields(config) + "");
228 function bss_find_existing(config, prev_config, prev_hash)
230 let hash = bss_config_hash(config.data);
232 for (let i = 0; i < length(prev_config.bss); i++) {
233 if (!prev_hash[i] || hash != prev_hash[i])
243 function get_config_bss(config, idx)
245 if (!config.bss[idx]) {
246 hostapd.printf(`Invalid bss index ${idx}`);
250 let ifname = config.bss[idx].ifname;
252 hostapd.printf(`Could not find bss ${config.bss[idx].ifname}`);
254 return hostapd.bss[ifname];
257 function iface_reload_config(phydev, config, old_config)
259 let phy = phydev.name;
261 if (!old_config || !is_equal(old_config.radio, config.radio))
264 if (is_equal(old_config.bss, config.bss))
267 if (!old_config.bss || !old_config.bss[0])
270 let iface_name = old_config.bss[0].ifname;
271 let iface = hostapd.interfaces[iface_name];
273 hostapd.printf(`Could not find previous interface ${iface_name}`);
277 let first_bss = hostapd.bss[iface_name];
279 hostapd.printf(`Could not find bss of previous interface ${iface_name}`);
283 let macaddr_list = iface_config_macaddr_list(config);
285 let bss_list_cfg = [];
286 let prev_bss_hash = [];
288 for (let bss in old_config.bss) {
289 let hash = bss_config_hash(bss.data);
290 push(prev_bss_hash, bss_config_hash(bss.data));
293 // Step 1: find (possibly renamed) interfaces with the same config
294 // and store them in the new order (with gaps)
295 for (let i = 0; i < length(config.bss); i++) {
298 // For fullmac devices, the first interface needs to be preserved,
299 // since it's treated as the master
300 if (!i && phy_is_fullmac(phy)) {
302 prev_bss_hash[0] = null;
304 prev = bss_find_existing(config.bss[i], old_config, prev_bss_hash);
309 let cur_config = config.bss[i];
310 let prev_config = old_config.bss[prev];
312 let prev_bss = get_config_bss(old_config, prev);
316 // try to preserve MAC address of this BSS by reassigning another
318 if (cur_config.default_macaddr &&
319 !macaddr_list[prev_config.bssid]) {
320 macaddr_list[prev_config.bssid] = i;
321 cur_config.bssid = prev_config.bssid;
324 bss_list[i] = prev_bss;
325 bss_list_cfg[i] = old_config.bss[prev];
328 if (config.mbssid && !bss_list_cfg[0]) {
329 hostapd.printf("First BSS changed with MBSSID enabled");
333 // Step 2: if none were found, rename and preserve the first one
334 if (length(bss_list) == 0) {
335 // can't change the bssid of the first bss
336 if (config.bss[0].bssid != old_config.bss[0].bssid) {
337 if (!config.bss[0].default_macaddr) {
338 hostapd.printf(`BSSID of first interface changed: ${lc(old_config.bss[0].bssid)} -> ${lc(config.bss[0].bssid)}`);
342 config.bss[0].bssid = old_config.bss[0].bssid;
345 let prev_bss = get_config_bss(old_config, 0);
349 macaddr_list[config.bss[0].bssid] = 0;
350 bss_list[0] = prev_bss;
351 bss_list_cfg[0] = old_config.bss[0];
352 prev_bss_hash[0] = null;
355 // Step 3: delete all unused old interfaces
356 for (let i = 0; i < length(prev_bss_hash); i++) {
357 if (!prev_bss_hash[i])
360 let prev_bss = get_config_bss(old_config, i);
364 let ifname = old_config.bss[i].ifname;
365 hostapd.printf(`Remove bss '${ifname}' on phy '${phy}'`);
370 // Step 4: rename preserved interfaces, use temporary name on duplicates
371 let rename_list = [];
372 for (let i = 0; i < length(bss_list); i++) {
376 let old_ifname = bss_list_cfg[i].ifname;
377 let new_ifname = config.bss[i].ifname;
378 if (old_ifname == new_ifname)
381 if (hostapd.bss[new_ifname]) {
382 new_ifname = "tmp_" + substr(hostapd.sha1(new_ifname), 0, 8);
383 push(rename_list, i);
386 hostapd.printf(`Rename bss ${old_ifname} to ${new_ifname}`);
387 if (!bss_list[i].rename(new_ifname)) {
388 hostapd.printf(`Failed to rename bss ${old_ifname} to ${new_ifname}`);
392 bss_list_cfg[i].ifname = new_ifname;
395 // Step 5: rename interfaces with temporary names
396 for (let i in rename_list) {
397 let new_ifname = config.bss[i].ifname;
398 if (!bss_list[i].rename(new_ifname)) {
399 hostapd.printf(`Failed to rename bss to ${new_ifname}`);
402 bss_list_cfg[i].ifname = new_ifname;
405 // Step 6: assign BSSID for newly created interfaces
407 num_global: config.num_global_macaddr ?? 1,
408 mbssid: config.mbssid ?? 0,
410 macaddr_list = phydev.macaddr_init(macaddr_list, macaddr_data);
411 for (let i = 0; i < length(config.bss); i++) {
414 let bsscfg = config.bss[i];
416 let mac_idx = macaddr_list[bsscfg.bssid];
418 macaddr_list[bsscfg.bssid] = i;
422 // statically assigned bssid of the new interface is in conflict
423 // with the bssid of a reused interface. reassign the reused interface
424 if (!bsscfg.default_macaddr) {
425 // can't update bssid of the first BSS, need to restart
429 bsscfg = config.bss[mac_idx];
432 let addr = phydev.macaddr_next(i);
434 hostapd.printf(`Failed to generate mac address for phy ${phy}`);
440 let config_inline = iface_gen_config(phy, config);
442 // Step 7: fill in the gaps with new interfaces
443 for (let i = 0; i < length(config.bss); i++) {
444 let ifname = config.bss[i].ifname;
445 let bss = bss_list[i];
450 hostapd.printf(`Add bss ${ifname} on phy ${phy}`);
451 bss_list[i] = iface.add_bss(config_inline, i);
453 hostapd.printf(`Failed to add new bss ${ifname} on phy ${phy}`);
458 // Step 8: update interface bss order
459 if (!iface.set_bss_order(bss_list)) {
460 hostapd.printf(`Failed to update BSS order on phy '${phy}'`);
464 // Step 9: update config
465 for (let i = 0; i < length(config.bss); i++) {
466 if (!bss_list_cfg[i])
469 let ifname = config.bss[i].ifname;
470 let bss = bss_list[i];
472 if (is_equal(config.bss[i], bss_list_cfg[i]))
475 if (is_equal(bss_remove_file_fields(config.bss[i]),
476 bss_remove_file_fields(bss_list_cfg[i]))) {
477 hostapd.printf(`Update config data files for bss ${ifname}`);
478 if (bss.set_config(config_inline, i, true) < 0) {
479 hostapd.printf(`Could not update config data files for bss ${ifname}`);
482 bss.ctrl("RELOAD_WPA_PSK");
487 bss_reload_psk(bss, config.bss[i], bss_list_cfg[i]);
488 if (is_equal(config.bss[i], bss_list_cfg[i]))
491 hostapd.printf(`Reload config for bss '${config.bss[0].ifname}' on phy '${phy}'`);
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);