hostapd: rework reload support and MAC address handling
[openwrt/staging/hauke.git] / package / network / services / hostapd / files / wpa_supplicant.uc
1 let libubus = require("ubus");
2 import { open, readfile } from "fs";
3 import { wdev_create, wdev_remove, is_equal, vlist_new, phy_open } from "common";
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);
40 let ret = wdev_create(phy, ifname, wdev_config);
41 if (ret)
42 wpas.printf(`Failed to create device ${ifname}: ${ret}`);
43 wpas.add_iface(iface.config);
44 iface.running = true;
45 }
46
47 function iface_cb(new_if, old_if)
48 {
49 if (old_if && new_if && is_equal(old_if.config, new_if.config)) {
50 new_if.running = old_if.running;
51 return;
52 }
53
54 if (new_if && old_if)
55 wpas.printf(`Update configuration for interface ${old_if.config.iface}`);
56 else if (old_if)
57 wpas.printf(`Remove interface ${old_if.config.iface}`);
58
59 if (old_if)
60 iface_stop(old_if);
61 }
62
63 function prepare_config(config)
64 {
65 config.config_data = readfile(config.config);
66
67 return { config: config };
68 }
69
70 function set_config(phy_name, config_list)
71 {
72 let phy = wpas.data.config[phy_name];
73
74 if (!phy) {
75 phy = vlist_new(iface_cb, false);
76 wpas.data.config[phy_name] = phy;
77 }
78
79 let values = [];
80 for (let config in config_list)
81 push(values, [ config.iface, prepare_config(config) ]);
82
83 phy.update(values);
84 }
85
86 function start_pending(phy_name)
87 {
88 let phy = wpas.data.config[phy_name];
89 let ubus = wpas.data.ubus;
90
91 if (!phy || !phy.data)
92 return;
93
94 let phydev = phy_open(phy_name);
95 if (!phydev) {
96 wpas.printf(`Could not open phy ${phy_name}`);
97 return;
98 }
99
100 let macaddr_list = wpas.data.macaddr_list[phy_name];
101 phydev.macaddr_init(macaddr_list);
102
103 for (let ifname in phy.data)
104 iface_start(phydev, phy.data[ifname]);
105 }
106
107 let main_obj = {
108 phy_set_state: {
109 args: {
110 phy: "",
111 stop: true,
112 },
113 call: function(req) {
114 if (!req.args.phy || req.args.stop == null)
115 return libubus.STATUS_INVALID_ARGUMENT;
116
117 let phy = wpas.data.config[req.args.phy];
118 if (!phy)
119 return libubus.STATUS_NOT_FOUND;
120
121 try {
122 if (req.args.stop) {
123 for (let ifname in phy.data)
124 iface_stop(phy.data[ifname]);
125 } else {
126 start_pending(req.args.phy);
127 }
128 } catch (e) {
129 wpas.printf(`Error chaging state: ${e}\n${e.stacktrace[0].context}`);
130 return libubus.STATUS_INVALID_ARGUMENT;
131 }
132 return 0;
133 }
134 },
135 phy_set_macaddr_list: {
136 args: {
137 phy: "",
138 macaddr: [],
139 },
140 call: function(req) {
141 let phy = req.args.phy;
142 if (!phy)
143 return libubus.STATUS_INVALID_ARGUMENT;
144
145 wpas.data.macaddr_list[phy] = req.args.macaddr;
146 return 0;
147 }
148 },
149 phy_status: {
150 args: {
151 phy: ""
152 },
153 call: function(req) {
154 if (!req.args.phy)
155 return libubus.STATUS_INVALID_ARGUMENT;
156
157 let phy = wpas.data.config[req.args.phy];
158 if (!phy)
159 return libubus.STATUS_NOT_FOUND;
160
161 for (let ifname in phy.data) {
162 try {
163 let iface = wpas.interfaces[ifname];
164 if (!iface)
165 continue;
166
167 let status = iface.status();
168 if (!status)
169 continue;
170
171 if (status.state == "INTERFACE_DISABLED")
172 continue;
173
174 status.ifname = ifname;
175 return status;
176 } catch (e) {
177 continue;
178 }
179 }
180
181 return libubus.STATUS_NOT_FOUND;
182 }
183 },
184 config_set: {
185 args: {
186 phy: "",
187 config: [],
188 defer: true,
189 },
190 call: function(req) {
191 if (!req.args.phy)
192 return libubus.STATUS_INVALID_ARGUMENT;
193
194 wpas.printf(`Set new config for phy ${req.args.phy}`);
195 try {
196 if (req.args.config)
197 set_config(req.args.phy, req.args.config);
198
199 if (!req.args.defer)
200 start_pending(req.args.phy);
201 } catch (e) {
202 wpas.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`);
203 return libubus.STATUS_INVALID_ARGUMENT;
204 }
205
206 return {
207 pid: wpas.getpid()
208 };
209 }
210 },
211 config_add: {
212 args: {
213 driver: "",
214 iface: "",
215 bridge: "",
216 hostapd_ctrl: "",
217 ctrl: "",
218 config: "",
219 },
220 call: function(req) {
221 if (!req.args.iface || !req.args.config)
222 return libubus.STATUS_INVALID_ARGUMENT;
223
224 if (wpas.add_iface(req.args) < 0)
225 return libubus.STATUS_INVALID_ARGUMENT;
226
227 return {
228 pid: wpas.getpid()
229 };
230 }
231 },
232 config_remove: {
233 args: {
234 iface: ""
235 },
236 call: function(req) {
237 if (!req.args.iface)
238 return libubus.STATUS_INVALID_ARGUMENT;
239
240 wpas.remove_iface(req.args.iface);
241 return 0;
242 }
243 },
244 };
245
246 wpas.data.ubus = ubus;
247 wpas.data.obj = ubus.publish("wpa_supplicant", main_obj);
248
249 function iface_event(type, name, data) {
250 let ubus = wpas.data.ubus;
251
252 data ??= {};
253 data.name = name;
254 wpas.data.obj.notify(`iface.${type}`, data, null, null, null, -1);
255 ubus.call("service", "event", { type: `wpa_supplicant.${name}.${type}`, data: {} });
256 }
257
258 function iface_hostapd_notify(phy, ifname, iface, state)
259 {
260 let ubus = wpas.data.ubus;
261 let status = iface.status();
262 let msg = { phy: phy };
263
264 switch (state) {
265 case "DISCONNECTED":
266 case "AUTHENTICATING":
267 case "SCANNING":
268 msg.up = false;
269 break;
270 case "INTERFACE_DISABLED":
271 case "INACTIVE":
272 msg.up = true;
273 break;
274 case "COMPLETED":
275 msg.up = true;
276 msg.frequency = status.frequency;
277 msg.sec_chan_offset = status.sec_chan_offset;
278 break;
279 default:
280 return;
281 }
282
283 ubus.call("hostapd", "apsta_state", msg);
284 }
285
286 function iface_channel_switch(phy, ifname, iface, info)
287 {
288 let msg = {
289 phy: phy,
290 up: true,
291 csa: true,
292 csa_count: info.csa_count ? info.csa_count - 1 : 0,
293 frequency: info.frequency,
294 sec_chan_offset: info.sec_chan_offset,
295 };
296 ubus.call("hostapd", "apsta_state", msg);
297 }
298
299 return {
300 shutdown: function() {
301 for (let phy in wpas.data.config)
302 set_config(phy, []);
303 wpas.ubus.disconnect();
304 },
305 iface_add: function(name, obj) {
306 iface_event("add", name);
307 },
308 iface_remove: function(name, obj) {
309 iface_event("remove", name);
310 },
311 state: function(ifname, iface, state) {
312 let phy = wpas.data.iface_phy[ifname];
313 if (!phy) {
314 wpas.printf(`no PHY for ifname ${ifname}`);
315 return;
316 }
317
318 iface_hostapd_notify(phy, ifname, iface, state);
319 },
320 event: function(ifname, iface, ev, info) {
321 let phy = wpas.data.iface_phy[ifname];
322 if (!phy) {
323 wpas.printf(`no PHY for ifname ${ifname}`);
324 return;
325 }
326
327 if (ev == "CH_SWITCH_STARTED")
328 iface_channel_switch(phy, ifname, iface, info);
329 }
330 };