developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 1 | let libubus = require("ubus"); |
| 2 | import { open, readfile } from "fs"; |
developer | 70180b0 | 2023-11-14 17:01:47 +0800 | [diff] [blame] | 3 | import { wdev_create, wdev_set_mesh_params, wdev_remove, is_equal, wdev_set_up, vlist_new, phy_open } from "common"; |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 4 | |
| 5 | let ubus = libubus.connect(); |
| 6 | |
| 7 | wpas.data.config = {}; |
| 8 | wpas.data.iface_phy = {}; |
| 9 | wpas.data.macaddr_list = {}; |
| 10 | |
| 11 | function iface_stop(iface) |
| 12 | { |
| 13 | let ifname = iface.config.iface; |
| 14 | |
| 15 | if (!iface.running) |
| 16 | return; |
| 17 | |
| 18 | delete wpas.data.iface_phy[ifname]; |
| 19 | wpas.remove_iface(ifname); |
| 20 | wdev_remove(ifname); |
| 21 | iface.running = false; |
| 22 | } |
| 23 | |
| 24 | function iface_start(phydev, iface, macaddr_list) |
| 25 | { |
| 26 | let phy = phydev.name; |
| 27 | |
| 28 | if (iface.running) |
| 29 | return; |
| 30 | |
| 31 | let ifname = iface.config.iface; |
| 32 | let wdev_config = {}; |
| 33 | for (let field in iface.config) |
| 34 | wdev_config[field] = iface.config[field]; |
| 35 | if (!wdev_config.macaddr) |
| 36 | wdev_config.macaddr = phydev.macaddr_next(); |
| 37 | |
| 38 | wpas.data.iface_phy[ifname] = phy; |
| 39 | wdev_remove(ifname); |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 40 | let ret = wdev_create("phy0", ifname, wdev_config); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 41 | if (ret) |
| 42 | wpas.printf(`Failed to create device ${ifname}: ${ret}`); |
developer | 70180b0 | 2023-11-14 17:01:47 +0800 | [diff] [blame] | 43 | wdev_set_up(ifname, true); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 44 | wpas.add_iface(iface.config); |
| 45 | iface.running = true; |
| 46 | } |
| 47 | |
| 48 | function iface_cb(new_if, old_if) |
| 49 | { |
| 50 | if (old_if && new_if && is_equal(old_if.config, new_if.config)) { |
| 51 | new_if.running = old_if.running; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if (new_if && old_if) |
| 56 | wpas.printf(`Update configuration for interface ${old_if.config.iface}`); |
| 57 | else if (old_if) |
| 58 | wpas.printf(`Remove interface ${old_if.config.iface}`); |
| 59 | |
| 60 | if (old_if) |
| 61 | iface_stop(old_if); |
| 62 | } |
| 63 | |
| 64 | function prepare_config(config) |
| 65 | { |
| 66 | config.config_data = readfile(config.config); |
| 67 | |
| 68 | return { config: config }; |
| 69 | } |
| 70 | |
| 71 | function set_config(phy_name, config_list) |
| 72 | { |
| 73 | let phy = wpas.data.config[phy_name]; |
| 74 | |
| 75 | if (!phy) { |
| 76 | phy = vlist_new(iface_cb, false); |
| 77 | wpas.data.config[phy_name] = phy; |
| 78 | } |
| 79 | |
| 80 | let values = []; |
| 81 | for (let config in config_list) |
| 82 | push(values, [ config.iface, prepare_config(config) ]); |
| 83 | |
| 84 | phy.update(values); |
| 85 | } |
| 86 | |
| 87 | function start_pending(phy_name) |
| 88 | { |
| 89 | let phy = wpas.data.config[phy_name]; |
| 90 | let ubus = wpas.data.ubus; |
| 91 | |
| 92 | if (!phy || !phy.data) |
| 93 | return; |
| 94 | |
| 95 | let phydev = phy_open(phy_name); |
| 96 | if (!phydev) { |
| 97 | wpas.printf(`Could not open phy ${phy_name}`); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | let macaddr_list = wpas.data.macaddr_list[phy_name]; |
| 102 | phydev.macaddr_init(macaddr_list); |
| 103 | |
| 104 | for (let ifname in phy.data) |
| 105 | iface_start(phydev, phy.data[ifname]); |
| 106 | } |
| 107 | |
| 108 | let main_obj = { |
| 109 | phy_set_state: { |
| 110 | args: { |
| 111 | phy: "", |
| 112 | stop: true, |
| 113 | }, |
| 114 | call: function(req) { |
| 115 | if (!req.args.phy || req.args.stop == null) |
| 116 | return libubus.STATUS_INVALID_ARGUMENT; |
| 117 | |
| 118 | let phy = wpas.data.config[req.args.phy]; |
| 119 | if (!phy) |
| 120 | return libubus.STATUS_NOT_FOUND; |
| 121 | |
| 122 | try { |
| 123 | if (req.args.stop) { |
| 124 | for (let ifname in phy.data) |
| 125 | iface_stop(phy.data[ifname]); |
| 126 | } else { |
| 127 | start_pending(req.args.phy); |
| 128 | } |
| 129 | } catch (e) { |
| 130 | wpas.printf(`Error chaging state: ${e}\n${e.stacktrace[0].context}`); |
| 131 | return libubus.STATUS_INVALID_ARGUMENT; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | }, |
| 136 | phy_set_macaddr_list: { |
| 137 | args: { |
| 138 | phy: "", |
| 139 | macaddr: [], |
| 140 | }, |
| 141 | call: function(req) { |
| 142 | let phy = req.args.phy; |
| 143 | if (!phy) |
| 144 | return libubus.STATUS_INVALID_ARGUMENT; |
| 145 | |
| 146 | wpas.data.macaddr_list[phy] = req.args.macaddr; |
| 147 | return 0; |
| 148 | } |
| 149 | }, |
| 150 | phy_status: { |
| 151 | args: { |
| 152 | phy: "" |
| 153 | }, |
| 154 | call: function(req) { |
| 155 | if (!req.args.phy) |
| 156 | return libubus.STATUS_INVALID_ARGUMENT; |
| 157 | |
| 158 | let phy = wpas.data.config[req.args.phy]; |
| 159 | if (!phy) |
| 160 | return libubus.STATUS_NOT_FOUND; |
| 161 | |
| 162 | for (let ifname in phy.data) { |
| 163 | try { |
| 164 | let iface = wpas.interfaces[ifname]; |
| 165 | if (!iface) |
| 166 | continue; |
| 167 | |
| 168 | let status = iface.status(); |
| 169 | if (!status) |
| 170 | continue; |
| 171 | |
| 172 | if (status.state == "INTERFACE_DISABLED") |
| 173 | continue; |
| 174 | |
| 175 | status.ifname = ifname; |
| 176 | return status; |
| 177 | } catch (e) { |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return libubus.STATUS_NOT_FOUND; |
| 183 | } |
| 184 | }, |
| 185 | config_set: { |
| 186 | args: { |
| 187 | phy: "", |
| 188 | config: [], |
| 189 | defer: true, |
| 190 | }, |
| 191 | call: function(req) { |
| 192 | if (!req.args.phy) |
| 193 | return libubus.STATUS_INVALID_ARGUMENT; |
| 194 | |
| 195 | wpas.printf(`Set new config for phy ${req.args.phy}`); |
| 196 | try { |
| 197 | if (req.args.config) |
| 198 | set_config(req.args.phy, req.args.config); |
| 199 | |
| 200 | if (!req.args.defer) |
| 201 | start_pending(req.args.phy); |
| 202 | } catch (e) { |
| 203 | wpas.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`); |
| 204 | return libubus.STATUS_INVALID_ARGUMENT; |
| 205 | } |
| 206 | |
| 207 | return { |
| 208 | pid: wpas.getpid() |
| 209 | }; |
| 210 | } |
| 211 | }, |
| 212 | config_add: { |
| 213 | args: { |
| 214 | driver: "", |
| 215 | iface: "", |
| 216 | bridge: "", |
| 217 | hostapd_ctrl: "", |
| 218 | ctrl: "", |
| 219 | config: "", |
| 220 | }, |
| 221 | call: function(req) { |
| 222 | if (!req.args.iface || !req.args.config) |
| 223 | return libubus.STATUS_INVALID_ARGUMENT; |
| 224 | |
| 225 | if (wpas.add_iface(req.args) < 0) |
| 226 | return libubus.STATUS_INVALID_ARGUMENT; |
| 227 | |
| 228 | return { |
| 229 | pid: wpas.getpid() |
| 230 | }; |
| 231 | } |
| 232 | }, |
| 233 | config_remove: { |
| 234 | args: { |
| 235 | iface: "" |
| 236 | }, |
| 237 | call: function(req) { |
| 238 | if (!req.args.iface) |
| 239 | return libubus.STATUS_INVALID_ARGUMENT; |
| 240 | |
| 241 | wpas.remove_iface(req.args.iface); |
| 242 | return 0; |
| 243 | } |
| 244 | }, |
| 245 | }; |
| 246 | |
| 247 | wpas.data.ubus = ubus; |
| 248 | wpas.data.obj = ubus.publish("wpa_supplicant", main_obj); |
developer | ab5e775 | 2023-12-21 16:18:59 +0800 | [diff] [blame] | 249 | wpas.udebug_set("wpa_supplicant", wpas.data.ubus); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 250 | |
| 251 | function iface_event(type, name, data) { |
| 252 | let ubus = wpas.data.ubus; |
| 253 | |
| 254 | data ??= {}; |
| 255 | data.name = name; |
| 256 | wpas.data.obj.notify(`iface.${type}`, data, null, null, null, -1); |
| 257 | ubus.call("service", "event", { type: `wpa_supplicant.${name}.${type}`, data: {} }); |
| 258 | } |
| 259 | |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 260 | function iface_hostapd_notify(phy, ifname, iface, state, link_id) |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 261 | { |
| 262 | let ubus = wpas.data.ubus; |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 263 | let msg = { phy: phy }; |
| 264 | |
developer | ab5e775 | 2023-12-21 16:18:59 +0800 | [diff] [blame] | 265 | wpas.printf(`ucode: mtk: wpa_s in state ${state} notifies hostapd`); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 266 | switch (state) { |
| 267 | case "DISCONNECTED": |
| 268 | case "AUTHENTICATING": |
| 269 | case "SCANNING": |
| 270 | msg.up = false; |
| 271 | break; |
| 272 | case "INTERFACE_DISABLED": |
| 273 | case "INACTIVE": |
| 274 | msg.up = true; |
| 275 | break; |
| 276 | case "COMPLETED": |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 277 | let status = iface.status(link_id); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 278 | msg.up = true; |
| 279 | msg.frequency = status.frequency; |
| 280 | msg.sec_chan_offset = status.sec_chan_offset; |
developer | ab5e775 | 2023-12-21 16:18:59 +0800 | [diff] [blame] | 281 | msg.ch_width = status.ch_width; |
| 282 | msg.bw320_offset = status.bw320_offset; |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 283 | msg.band_idx = status.band_idx; |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 284 | break; |
| 285 | default: |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | ubus.call("hostapd", "apsta_state", msg); |
| 290 | } |
| 291 | |
| 292 | function iface_channel_switch(phy, ifname, iface, info) |
| 293 | { |
| 294 | let msg = { |
| 295 | phy: phy, |
| 296 | up: true, |
| 297 | csa: true, |
| 298 | csa_count: info.csa_count ? info.csa_count - 1 : 0, |
| 299 | frequency: info.frequency, |
developer | ab5e775 | 2023-12-21 16:18:59 +0800 | [diff] [blame] | 300 | ch_width: info.ch_width, |
| 301 | bw320_offset: info.bw320_offset, |
developer | a20cdc2 | 2024-05-31 18:57:31 +0800 | [diff] [blame] | 302 | band_idx: info.band_idx, |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 303 | sec_chan_offset: info.sec_chan_offset, |
developer | 05f3b2b | 2024-08-19 19:17:34 +0800 | [diff] [blame] | 304 | punct_bitmap: info.punct_bitmap, |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 305 | }; |
| 306 | ubus.call("hostapd", "apsta_state", msg); |
| 307 | } |
| 308 | |
| 309 | return { |
| 310 | shutdown: function() { |
| 311 | for (let phy in wpas.data.config) |
| 312 | set_config(phy, []); |
| 313 | wpas.ubus.disconnect(); |
| 314 | }, |
| 315 | iface_add: function(name, obj) { |
| 316 | iface_event("add", name); |
| 317 | }, |
| 318 | iface_remove: function(name, obj) { |
| 319 | iface_event("remove", name); |
| 320 | }, |
| 321 | state: function(ifname, iface, state) { |
| 322 | let phy = wpas.data.iface_phy[ifname]; |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 323 | let ret = iface.get_valid_links(); |
| 324 | let link_id = 0, valid_links = ret.valid_links; |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 325 | if (!phy) { |
| 326 | wpas.printf(`no PHY for ifname ${ifname}`); |
| 327 | return; |
| 328 | } |
| 329 | |
developer | 66e89bc | 2024-04-23 14:50:01 +0800 | [diff] [blame] | 330 | if (valid_links) { |
| 331 | while (valid_links) { |
| 332 | if (valid_links & 1) |
| 333 | iface_hostapd_notify(phy, ifname, iface, state, link_id); |
| 334 | |
| 335 | link_id++; |
| 336 | valid_links >>= 1; |
| 337 | } |
| 338 | } else { |
| 339 | iface_hostapd_notify(phy, ifname, iface, state, -1); |
| 340 | } |
developer | 70180b0 | 2023-11-14 17:01:47 +0800 | [diff] [blame] | 341 | |
| 342 | if (state != "COMPLETED") |
| 343 | return; |
| 344 | |
| 345 | let phy_data = wpas.data.config[phy]; |
| 346 | if (!phy_data) |
| 347 | return; |
| 348 | |
| 349 | let iface_data = phy_data.data[ifname]; |
| 350 | if (!iface_data) |
| 351 | return; |
| 352 | |
| 353 | let wdev_config = iface_data.config; |
| 354 | if (!wdev_config || wdev_config.mode != "mesh") |
| 355 | return; |
| 356 | |
| 357 | wdev_set_mesh_params(ifname, wdev_config); |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 358 | }, |
| 359 | event: function(ifname, iface, ev, info) { |
| 360 | let phy = wpas.data.iface_phy[ifname]; |
| 361 | if (!phy) { |
| 362 | wpas.printf(`no PHY for ifname ${ifname}`); |
| 363 | return; |
| 364 | } |
| 365 | |
developer | a20cdc2 | 2024-05-31 18:57:31 +0800 | [diff] [blame] | 366 | if (ev == "CH_SWITCH_STARTED" || ev == "LINK_CH_SWITCH_STARTED") |
developer | e92ee3c | 2023-10-25 17:01:28 +0800 | [diff] [blame] | 367 | iface_channel_switch(phy, ifname, iface, info); |
| 368 | } |
| 369 | }; |