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 for (let bss in cfg.bss)
30 wdev_remove(bss.ifname);
33 function iface_gen_config(phy, config, start_disabled)
36 ${join("\n", config.radio.data)}
37 channel=${config.radio.channel}
40 for (let i = 0; i < length(config.bss); i++) {
41 let bss = config.bss[i];
42 let type = i > 0 ? "bss" : "interface";
43 let nasid = bss.nasid ?? replace(bss.bssid, ":", "");
48 ${join("\n", bss.data)}
49 nas_identifier=${nasid}
60 function iface_freq_info(iface, config, params)
62 let freq = params.frequency;
66 let sec_offset = params.sec_chan_offset;
67 if (sec_offset != -1 && sec_offset != 1)
71 for (let line in config.radio.data) {
72 if (!sec_offset && match(line, /^ht_capab=.*HT40/)) {
73 sec_offset = null; // auto-detect
77 let val = match(line, /^(vht_oper_chwidth|he_oper_chwidth)=(\d+)/);
89 return hostapd.freq_info(freq, sec_offset, width);
92 function iface_add(phy, config, phy_status)
94 let config_inline = iface_gen_config(phy, config, !!phy_status);
96 let bss = config.bss[0];
97 let ret = hostapd.add_iface(`bss_config=${phy}:${config_inline}`);
104 let iface = hostapd.interfaces[phy];
108 let freq_info = iface_freq_info(iface, config, phy_status);
110 return iface.start(freq_info) >= 0;
113 function iface_config_macaddr_list(config)
115 let macaddr_list = {};
116 for (let i = 0; i < length(config.bss); i++) {
117 let bss = config.bss[i];
118 if (!bss.default_macaddr)
119 macaddr_list[bss.bssid] = i;
125 function iface_update_supplicant_macaddr(phy, config)
127 let macaddr_list = [];
128 for (let i = 0; i < length(config.bss); i++)
129 push(macaddr_list, config.bss[i].bssid);
130 ubus.call("wpa_supplicant", "phy_set_macaddr_list", { phy: phy, macaddr: macaddr_list });
133 function iface_restart(phydev, config, old_config)
135 let phy = phydev.name;
137 hostapd.remove_iface(phy);
138 iface_remove(old_config);
139 iface_remove(config);
141 if (!config.bss || !config.bss[0]) {
142 hostapd.printf(`No bss for phy ${phy}`);
146 phydev.macaddr_init(iface_config_macaddr_list(config));
147 for (let i = 0; i < length(config.bss); i++) {
148 let bss = config.bss[i];
149 if (bss.default_macaddr)
150 bss.bssid = phydev.macaddr_next();
153 iface_update_supplicant_macaddr(phy, config);
155 let bss = config.bss[0];
156 let err = wdev_create(phy, bss.ifname, { mode: "ap" });
158 hostapd.printf(`Failed to create ${bss.ifname} on phy ${phy}: ${err}`);
160 let ubus = hostapd.data.ubus;
161 let phy_status = ubus.call("wpa_supplicant", "phy_status", { phy: phy });
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`);
169 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: true });
170 if (!iface_add(phy, config))
171 hostapd.printf(`hostapd.add_iface failed for phy ${phy} ifname=${bss.ifname}`);
172 ubus.call("wpa_supplicant", "phy_set_state", { phy: phy, stop: false });
175 function array_to_obj(arr, key, start)
180 for (let i = start; i < length(arr); i++) {
188 function find_array_idx(arr, key, val)
190 for (let i = 0; i < length(arr); i++)
191 if (arr[i][key] == val)
197 function bss_reload_psk(bss, config, old_config)
199 if (is_equal(old_config.hash.wpa_psk_file, config.hash.wpa_psk_file))
202 old_config.hash.wpa_psk_file = config.hash.wpa_psk_file;
203 if (!is_equal(old_config, config))
206 let ret = bss.ctrl("RELOAD_WPA_PSK");
209 hostapd.printf(`Reload WPA PSK file for bss ${config.ifname}: ${ret}`);
212 function remove_file_fields(config)
214 return filter(config, (line) => !hostapd.data.file_fields[split(line, "=")[0]]);
217 function bss_remove_file_fields(config)
221 for (let key in config)
222 new_cfg[key] = config[key];
223 new_cfg.data = remove_file_fields(new_cfg.data);
225 for (let key in config.hash)
226 new_cfg.hash[key] = config.hash[key];
227 delete new_cfg.hash.wpa_psk_file;
228 delete new_cfg.hash.vlan_file;
233 function bss_config_hash(config)
235 return hostapd.sha1(remove_file_fields(config) + "");
238 function bss_find_existing(config, prev_config, prev_hash)
240 let hash = bss_config_hash(config.data);
242 for (let i = 0; i < length(prev_config.bss); i++) {
243 if (!prev_hash[i] || hash != prev_hash[i])
253 function get_config_bss(config, idx)
255 if (!config.bss[idx]) {
256 hostapd.printf(`Invalid bss index ${idx}`);
260 let ifname = config.bss[idx].ifname;
262 hostapd.printf(`Could not find bss ${config.bss[idx].ifname}`);
264 return hostapd.bss[ifname];
267 function iface_reload_config(phydev, config, old_config)
269 let phy = phydev.name;
271 if (!old_config || !is_equal(old_config.radio, config.radio))
274 if (is_equal(old_config.bss, config.bss))
277 if (!old_config.bss || !old_config.bss[0])
280 let iface = hostapd.interfaces[phy];
281 let iface_name = old_config.bss[0].ifname;
283 hostapd.printf(`Could not find previous interface ${iface_name}`);
287 let first_bss = hostapd.bss[iface_name];
289 hostapd.printf(`Could not find bss of previous interface ${iface_name}`);
293 let macaddr_list = iface_config_macaddr_list(config);
295 let bss_list_cfg = [];
296 let prev_bss_hash = [];
298 for (let bss in old_config.bss) {
299 let hash = bss_config_hash(bss.data);
300 push(prev_bss_hash, bss_config_hash(bss.data));
303 // Step 1: find (possibly renamed) interfaces with the same config
304 // and store them in the new order (with gaps)
305 for (let i = 0; i < length(config.bss); i++) {
308 // For fullmac devices, the first interface needs to be preserved,
309 // since it's treated as the master
310 if (!i && phy_is_fullmac(phy)) {
312 prev_bss_hash[0] = null;
314 prev = bss_find_existing(config.bss[i], old_config, prev_bss_hash);
319 let cur_config = config.bss[i];
320 let prev_config = old_config.bss[prev];
322 let prev_bss = get_config_bss(old_config, prev);
326 // try to preserve MAC address of this BSS by reassigning another
328 if (cur_config.default_macaddr &&
329 !macaddr_list[prev_config.bssid]) {
330 macaddr_list[prev_config.bssid] = i;
331 cur_config.bssid = prev_config.bssid;
334 bss_list[i] = prev_bss;
335 bss_list_cfg[i] = old_config.bss[prev];
338 if (config.mbssid && !bss_list_cfg[0]) {
339 hostapd.printf("First BSS changed with MBSSID enabled");
343 // Step 2: if none were found, rename and preserve the first one
344 if (length(bss_list) == 0) {
345 // can't change the bssid of the first bss
346 if (config.bss[0].bssid != old_config.bss[0].bssid) {
347 if (!config.bss[0].default_macaddr) {
348 hostapd.printf(`BSSID of first interface changed: ${lc(old_config.bss[0].bssid)} -> ${lc(config.bss[0].bssid)}`);
352 config.bss[0].bssid = old_config.bss[0].bssid;
355 let prev_bss = get_config_bss(old_config, 0);
359 macaddr_list[config.bss[0].bssid] = 0;
360 bss_list[0] = prev_bss;
361 bss_list_cfg[0] = old_config.bss[0];
362 prev_bss_hash[0] = null;
365 // Step 3: delete all unused old interfaces
366 for (let i = 0; i < length(prev_bss_hash); i++) {
367 if (!prev_bss_hash[i])
370 let prev_bss = get_config_bss(old_config, i);
374 let ifname = old_config.bss[i].ifname;
375 hostapd.printf(`Remove bss '${ifname}' on phy '${phy}'`);
380 // Step 4: rename preserved interfaces, use temporary name on duplicates
381 let rename_list = [];
382 for (let i = 0; i < length(bss_list); i++) {
386 let old_ifname = bss_list_cfg[i].ifname;
387 let new_ifname = config.bss[i].ifname;
388 if (old_ifname == new_ifname)
391 if (hostapd.bss[new_ifname]) {
392 new_ifname = "tmp_" + substr(hostapd.sha1(new_ifname), 0, 8);
393 push(rename_list, i);
396 hostapd.printf(`Rename bss ${old_ifname} to ${new_ifname}`);
397 if (!bss_list[i].rename(new_ifname)) {
398 hostapd.printf(`Failed to rename bss ${old_ifname} to ${new_ifname}`);
402 bss_list_cfg[i].ifname = new_ifname;
405 // Step 5: rename interfaces with temporary names
406 for (let i in rename_list) {
407 let new_ifname = config.bss[i].ifname;
408 if (!bss_list[i].rename(new_ifname)) {
409 hostapd.printf(`Failed to rename bss to ${new_ifname}`);
412 bss_list_cfg[i].ifname = new_ifname;
415 // Step 6: assign BSSID for newly created interfaces
417 num_global: config.num_global_macaddr ?? 1,
418 mbssid: config.mbssid ?? 0,
420 macaddr_list = phydev.macaddr_init(macaddr_list, macaddr_data);
421 for (let i = 0; i < length(config.bss); i++) {
424 let bsscfg = config.bss[i];
426 let mac_idx = macaddr_list[bsscfg.bssid];
428 macaddr_list[bsscfg.bssid] = i;
432 // statically assigned bssid of the new interface is in conflict
433 // with the bssid of a reused interface. reassign the reused interface
434 if (!bsscfg.default_macaddr) {
435 // can't update bssid of the first BSS, need to restart
439 bsscfg = config.bss[mac_idx];
442 let addr = phydev.macaddr_next(i);
444 hostapd.printf(`Failed to generate mac address for phy ${phy}`);
450 let config_inline = iface_gen_config(phy, config);
452 // Step 7: fill in the gaps with new interfaces
453 for (let i = 0; i < length(config.bss); i++) {
454 let ifname = config.bss[i].ifname;
455 let bss = bss_list[i];
460 hostapd.printf(`Add bss ${ifname} on phy ${phy}`);
461 bss_list[i] = iface.add_bss(config_inline, i);
463 hostapd.printf(`Failed to add new bss ${ifname} on phy ${phy}`);
468 // Step 8: update interface bss order
469 if (!iface.set_bss_order(bss_list)) {
470 hostapd.printf(`Failed to update BSS order on phy '${phy}'`);
474 // Step 9: update config
475 for (let i = 0; i < length(config.bss); i++) {
476 if (!bss_list_cfg[i])
479 let ifname = config.bss[i].ifname;
480 let bss = bss_list[i];
482 if (is_equal(config.bss[i], bss_list_cfg[i]))
485 if (is_equal(bss_remove_file_fields(config.bss[i]),
486 bss_remove_file_fields(bss_list_cfg[i]))) {
487 hostapd.printf(`Update config data files for bss ${ifname}`);
488 if (bss.set_config(config_inline, i, true) < 0) {
489 hostapd.printf(`Could not update config data files for bss ${ifname}`);
492 bss.ctrl("RELOAD_WPA_PSK");
497 bss_reload_psk(bss, config.bss[i], bss_list_cfg[i]);
498 if (is_equal(config.bss[i], bss_list_cfg[i]))
501 hostapd.printf(`Reload config for bss '${config.bss[0].ifname}' on phy '${phy}'`);
502 if (bss.set_config(config_inline, i) < 0) {
503 hostapd.printf(`Failed to set config for bss ${ifname}`);
511 function iface_set_config(phy, config)
513 let old_config = hostapd.data.config[phy];
515 hostapd.data.config[phy] = config;
518 hostapd.remove_iface(phy);
519 return iface_remove(old_config);
522 let phydev = phy_open(phy);
524 hostapd.printf(`Failed to open phy ${phy}`);
529 let ret = iface_reload_config(phydev, config, old_config);
531 iface_update_supplicant_macaddr(phy, config);
532 hostapd.printf(`Reloaded settings for phy ${phy}`);
536 hostapd.printf(`Error reloading config: ${e}\n${e.stacktrace[0].context}`);
539 hostapd.printf(`Restart interface for phy ${phy}`);
540 let ret = iface_restart(phydev, config, old_config);
545 function config_add_bss(config, name)
553 push(config.bss, bss);
558 function iface_load_config(filename)
560 let f = open(filename, "r");
574 while ((line = rtrim(f.read("line"), "\n")) != null) {
575 let val = split(line, "=", 2);
579 if (val[0] == "interface") {
580 bss = config_add_bss(config, val[1]);
584 if (val[0] == "channel") {
585 config.radio.channel = val[1];
589 if (val[0] == "#num_global_macaddr" ||
591 config[val[0]] = int(val[1]);
593 push(config.radio.data, line);
596 while ((line = rtrim(f.read("line"), "\n")) != null) {
597 if (line == "#default_macaddr")
598 bss.default_macaddr = true;
600 let val = split(line, "=", 2);
604 if (val[0] == "bssid") {
605 bss.bssid = lc(val[1]);
609 if (val[0] == "nas_identifier")
612 if (val[0] == "bss") {
613 bss = config_add_bss(config, val[1]);
617 if (hostapd.data.file_fields[val[0]])
618 bss.hash[val[0]] = hostapd.sha1(readfile(val[1]));
620 push(bss.data, line);
627 function ex_wrap(func) {
633 hostapd.printf(`Exception in ubus function: ${e}\n${e.stacktrace[0].context}`);
635 return libubus.STATUS_UNKNOWN_ERROR;
644 call: ex_wrap(function(req) {
645 let phy_list = req.args.phy ? [ req.args.phy ] : keys(hostapd.data.config);
646 for (let phy_name in phy_list) {
647 let phy = hostapd.data.config[phy_name];
648 let config = iface_load_config(phy.orig_file);
649 iface_set_config(phy_name, config);
664 call: ex_wrap(function(req) {
665 if (req.args.up == null || !req.args.phy)
666 return libubus.STATUS_INVALID_ARGUMENT;
668 let phy = req.args.phy;
669 let config = hostapd.data.config[phy];
670 if (!config || !config.bss || !config.bss[0] || !config.bss[0].ifname)
673 let iface = hostapd.interfaces[phy];
682 if (!req.args.frequency)
683 return libubus.STATUS_INVALID_ARGUMENT;
685 let freq_info = iface_freq_info(iface, config, req.args);
687 return libubus.STATUS_UNKNOWN_ERROR;
691 freq_info.csa_count = req.args.csa_count ?? 10;
692 ret = iface.switch_channel(freq_info);
694 ret = iface.start(freq_info);
697 return libubus.STATUS_UNKNOWN_ERROR;
702 config_get_macaddr_list: {
706 call: ex_wrap(function(req) {
707 let phy = req.args.phy;
709 return libubus.STATUS_INVALID_ARGUMENT;
715 let config = hostapd.data.config[phy];
719 ret.macaddr = map(config.bss, (bss) => bss.bssid);
729 call: ex_wrap(function(req) {
730 let phy = req.args.phy;
731 let file = req.args.config;
732 let prev_file = req.args.prev_config;
735 return libubus.STATUS_INVALID_ARGUMENT;
737 if (prev_file && !hostapd.data.config[phy]) {
738 let config = iface_load_config(prev_file);
740 config.radio.data = [];
741 hostapd.data.config[phy] = config;
744 let config = iface_load_config(file);
746 hostapd.printf(`Set new config for phy ${phy}: ${file}`);
747 iface_set_config(phy, config);
750 pid: hostapd.getpid()
759 call: ex_wrap(function(req) {
760 if (!req.args.iface || !req.args.config)
761 return libubus.STATUS_INVALID_ARGUMENT;
763 if (hostapd.add_iface(`bss_config=${req.args.iface}:${req.args.config}`) < 0)
764 return libubus.STATUS_INVALID_ARGUMENT;
767 pid: hostapd.getpid()
775 call: ex_wrap(function(req) {
777 return libubus.STATUS_INVALID_ARGUMENT;
779 hostapd.remove_iface(req.args.iface);
785 hostapd.data.ubus = ubus;
786 hostapd.data.obj = ubus.publish("hostapd", main_obj);
787 hostapd.udebug_set("hostapd", hostapd.data.ubus);
789 function bss_event(type, name, data) {
790 let ubus = hostapd.data.ubus;
794 hostapd.data.obj.notify(`bss.${type}`, data, null, null, null, -1);
795 ubus.call("service", "event", { type: `hostapd.${name}.${type}`, data: {} });
799 shutdown: function() {
800 for (let phy in hostapd.data.config)
801 iface_set_config(phy, null);
802 hostapd.udebug_set(null);
803 hostapd.ubus.disconnect();
805 bss_add: function(name, obj) {
806 bss_event("add", name);
808 bss_reload: function(name, obj, reconf) {
809 bss_event("reload", name, { reconf: reconf != 0 });
811 bss_remove: function(name, obj) {
812 bss_event("remove", name);