6308fd54e2d332140c3e914f3d5689c3a2340a8c
[openwrt/staging/neocturne.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 } from "common";
4
5 let ubus = libubus.connect();
6
7 wpas.data.config = {};
8 wpas.data.iface_phy = {};
9
10 function iface_stop(iface)
11 {
12 let ifname = iface.config.iface;
13
14 if (!iface.running)
15 return;
16
17 delete wpas.data.iface_phy[ifname];
18 wpas.remove_iface(ifname);
19 wdev_remove(ifname);
20 iface.running = false;
21 }
22
23 function iface_start(phy, iface)
24 {
25 if (iface.running)
26 return;
27
28 let ifname = iface.config.iface;
29
30 wpas.data.iface_phy[ifname] = phy;
31 wdev_remove(ifname);
32 let ret = wdev_create(phy, ifname, iface.config);
33 if (ret)
34 wpas.printf(`Failed to create device ${ifname}: ${ret}`);
35 wpas.add_iface(iface.config);
36 iface.running = true;
37 }
38
39 function iface_cb(new_if, old_if)
40 {
41 if (old_if && new_if && is_equal(old_if.config, new_if.config)) {
42 new_if.running = old_if.running;
43 return;
44 }
45
46 if (old_if)
47 iface_stop(old_if);
48 }
49
50 function prepare_config(config)
51 {
52 config.config_data = readfile(config.config);
53
54 return { config: config };
55 }
56
57 function set_config(phy_name, config_list)
58 {
59 let phy = wpas.data.config[phy_name];
60
61 if (!phy) {
62 phy = vlist_new(iface_cb, false);
63 wpas.data.config[phy_name] = phy;
64 }
65
66 let values = [];
67 for (let config in config_list)
68 push(values, [ config.iface, prepare_config(config) ]);
69
70 phy.update(values);
71 }
72
73 function start_pending(phy_name)
74 {
75 let phy = wpas.data.config[phy_name];
76
77 for (let ifname in phy.data)
78 iface_start(phy_name, phy.data[ifname]);
79 }
80
81 let main_obj = {
82 phy_set_state: {
83 args: {
84 phy: "",
85 stop: true,
86 },
87 call: function(req) {
88 if (!req.args.phy || req.args.stop == null)
89 return libubus.STATUS_INVALID_ARGUMENT;
90
91 let phy = wpas.data.config[req.args.phy];
92 if (!phy)
93 return libubus.STATUS_NOT_FOUND;
94
95 if (req.args.stop) {
96 for (let ifname in phy.data)
97 iface_stop(phy.data[ifname]);
98 } else {
99 start_pending(req.args.phy);
100 }
101 return 0;
102 }
103 },
104 config_set: {
105 args: {
106 phy: "",
107 config: [],
108 defer: true,
109 },
110 call: function(req) {
111 if (!req.args.phy)
112 return libubus.STATUS_INVALID_ARGUMENT;
113
114 try {
115 if (req.args.config)
116 set_config(req.args.phy, req.args.config);
117
118 if (!req.args.defer)
119 start_pending(req.args.phy);
120 } catch (e) {
121 wpas.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`);
122 return libubus.STATUS_INVALID_ARGUMENT;
123 }
124
125 return {
126 pid: wpas.getpid()
127 };
128 }
129 },
130 config_add: {
131 args: {
132 driver: "",
133 iface: "",
134 bridge: "",
135 hostapd_ctrl: "",
136 ctrl: "",
137 config: "",
138 },
139 call: function(req) {
140 if (!req.args.iface || !req.args.config)
141 return libubus.STATUS_INVALID_ARGUMENT;
142
143 if (wpas.add_iface(req.args) < 0)
144 return libubus.STATUS_INVALID_ARGUMENT;
145
146 return {
147 pid: wpas.getpid()
148 };
149 }
150 },
151 config_remove: {
152 args: {
153 iface: ""
154 },
155 call: function(req) {
156 if (!req.args.iface)
157 return libubus.STATUS_INVALID_ARGUMENT;
158
159 wpas.remove_iface(req.args.iface);
160 return 0;
161 }
162 },
163 };
164
165 wpas.data.ubus = ubus;
166 wpas.data.obj = ubus.publish("wpa_supplicant", main_obj);
167
168 function iface_event(type, name, data) {
169 let ubus = wpas.data.ubus;
170
171 data ??= {};
172 data.name = name;
173 wpas.data.obj.notify(`iface.${type}`, data, null, null, null, -1);
174 ubus.call("service", "event", { type: `wpa_supplicant.${name}.${type}`, data: {} });
175 }
176
177 function iface_hostapd_notify(phy, ifname, iface, state)
178 {
179 let ubus = wpas.data.ubus;
180 let status = iface.status();
181 let msg = { phy: phy };
182
183 switch (state) {
184 case "DISCONNECTED":
185 case "AUTHENTICATING":
186 msg.up = false;
187 break;
188 case "INTERFACE_DISABLED":
189 case "INACTIVE":
190 msg.up = true;
191 break;
192 case "COMPLETED":
193 msg.up = true;
194 msg.frequency = status.frequency;
195 msg.sec_chan_offset = status.sec_chan_offset;
196 break;
197 default:
198 return;
199 }
200
201 ubus.call("hostapd", "apsta_state", msg);
202 }
203
204 function iface_channel_switch(phy, ifname, iface, info)
205 {
206 let msg = {
207 phy: phy,
208 up: true,
209 csa: true,
210 csa_count: info.csa_count ? info.csa_count - 1 : 0,
211 frequency: info.frequency,
212 sec_chan_offset: info.sec_chan_offset,
213 };
214 ubus.call("hostapd", "apsta_state", msg);
215 }
216
217 return {
218 shutdown: function() {
219 for (let phy in wpas.data.config)
220 set_config(phy, []);
221 wpas.ubus.disconnect();
222 },
223 iface_add: function(name, obj) {
224 obj.data.name = name;
225 iface_event("add", name);
226 },
227 iface_remove: function(name, obj) {
228 iface_event("remove", name);
229 },
230 state: function(iface, state) {
231 let ifname = iface.data.name;
232 let phy = wpas.data.iface_phy[ifname];
233 if (!phy) {
234 wpas.printf(`no PHY for ifname ${ifname}`);
235 return;
236 }
237
238 iface_hostapd_notify(phy, ifname, iface, state);
239 },
240 event: function(iface, ev, info) {
241 let ifname = iface.data.name;
242 let phy = wpas.data.iface_phy[ifname];
243 if (!phy) {
244 wpas.printf(`no PHY for ifname ${ifname}`);
245 return;
246 }
247
248 if (ev == "CH_SWITCH_STARTED")
249 iface_channel_switch(phy, ifname, iface, info);
250 }
251 };