wifi: fix applying mesh parameters when wpa_supplicant is in use
[openwrt/staging/hauke.git] / package / network / services / hostapd / files / common.uc
1 import * as nl80211 from "nl80211";
2 import * as rtnl from "rtnl";
3 import { readfile, glob, basename, readlink } from "fs";
4
5 const iftypes = {
6 ap: nl80211.const.NL80211_IFTYPE_AP,
7 mesh: nl80211.const.NL80211_IFTYPE_MESH_POINT,
8 sta: nl80211.const.NL80211_IFTYPE_STATION,
9 adhoc: nl80211.const.NL80211_IFTYPE_ADHOC,
10 monitor: nl80211.const.NL80211_IFTYPE_MONITOR,
11 };
12
13 const mesh_params = {
14 mesh_retry_timeout: "retry_timeout",
15 mesh_confirm_timeout: "confirm_timeout",
16 mesh_holding_timeout: "holding_timeout",
17 mesh_max_peer_links: "max_peer_links",
18 mesh_max_retries: "max_retries",
19 mesh_ttl: "ttl",
20 mesh_element_ttl: "element_ttl",
21 mesh_auto_open_plinks: "auto_open_plinks",
22 mesh_hwmp_max_preq_retries: "hwmp_max_preq_retries",
23 mesh_path_refresh_time: "path_refresh_time",
24 mesh_min_discovery_timeout: "min_discovery_timeout",
25 mesh_hwmp_active_path_timeout: "hwmp_active_path_timeout",
26 mesh_hwmp_preq_min_interval: "hwmp_preq_min_interval",
27 mesh_hwmp_net_diameter_traversal_time: "hwmp_net_diam_trvs_time",
28 mesh_hwmp_rootmode: "hwmp_rootmode",
29 mesh_hwmp_rann_interval: "hwmp_rann_interval",
30 mesh_gate_announcements: "gate_announcements",
31 mesh_sync_offset_max_neighor: "sync_offset_max_neighbor",
32 mesh_rssi_threshold: "rssi_threshold",
33 mesh_hwmp_active_path_to_root_timeout: "hwmp_path_to_root_timeout",
34 mesh_hwmp_root_interval: "hwmp_root_interval",
35 mesh_hwmp_confirmation_interval: "hwmp_confirmation_interval",
36 mesh_awake_window: "awake_window",
37 mesh_plink_timeout: "plink_timeout",
38 mesh_fwding: "forwarding",
39 mesh_power_mode: "power_mode",
40 mesh_nolearn: "nolearn"
41 };
42
43 function wdev_remove(name)
44 {
45 nl80211.request(nl80211.const.NL80211_CMD_DEL_INTERFACE, 0, { dev: name });
46 }
47
48 function __phy_is_fullmac(phyidx)
49 {
50 let data = nl80211.request(nl80211.const.NL80211_CMD_GET_WIPHY, 0, { wiphy: phyidx });
51
52 return !data.software_iftypes.ap_vlan;
53 }
54
55 function phy_is_fullmac(phy)
56 {
57 let phyidx = int(trim(readfile(`/sys/class/ieee80211/${phy}/index`)));
58
59 return __phy_is_fullmac(phyidx);
60 }
61
62 function find_reusable_wdev(phyidx)
63 {
64 if (!__phy_is_fullmac(phyidx))
65 return null;
66
67 let data = nl80211.request(
68 nl80211.const.NL80211_CMD_GET_INTERFACE,
69 nl80211.const.NLM_F_DUMP,
70 { wiphy: phyidx });
71 for (let res in data)
72 if (trim(readfile(`/sys/class/net/${res.ifname}/operstate`)) == "down")
73 return res.ifname;
74 return null;
75 }
76
77 function wdev_create(phy, name, data)
78 {
79 let phyidx = int(readfile(`/sys/class/ieee80211/${phy}/index`));
80
81 wdev_remove(name);
82
83 if (!iftypes[data.mode])
84 return `Invalid mode: ${data.mode}`;
85
86 let req = {
87 wiphy: phyidx,
88 ifname: name,
89 iftype: iftypes[data.mode],
90 };
91
92 if (data["4addr"])
93 req["4addr"] = data["4addr"];
94 if (data.macaddr)
95 req.mac = data.macaddr;
96
97 nl80211.error();
98
99 let reuse_ifname = find_reusable_wdev(phyidx);
100 if (reuse_ifname &&
101 (reuse_ifname == name ||
102 rtnl.request(rtnl.const.RTM_SETLINK, 0, { dev: reuse_ifname, ifname: name}) != false))
103 nl80211.request(
104 nl80211.const.NL80211_CMD_SET_INTERFACE, 0, {
105 wiphy: phyidx,
106 dev: name,
107 iftype: iftypes[data.mode],
108 });
109 else
110 nl80211.request(
111 nl80211.const.NL80211_CMD_NEW_INTERFACE,
112 nl80211.const.NLM_F_CREATE,
113 req);
114
115 let error = nl80211.error();
116 if (error)
117 return error;
118
119 if (data.powersave != null) {
120 nl80211.request(nl80211.const.NL80211_CMD_SET_POWER_SAVE, 0,
121 { dev: name, ps_state: data.powersave ? 1 : 0});
122 }
123
124 return null;
125 }
126
127 function wdev_set_mesh_params(name, data)
128 {
129 let mesh_cfg = {};
130
131 for (let key in mesh_params) {
132 let val = data[key];
133 if (val == null)
134 continue;
135 mesh_cfg[mesh_params[key]] = int(val);
136 }
137
138 if (!length(mesh_cfg))
139 return null;
140
141 nl80211.request(nl80211.const.NL80211_CMD_SET_MESH_CONFIG, 0,
142 { dev: name, mesh_params: mesh_cfg });
143
144 return nl80211.error();
145 }
146
147 function phy_sysfs_file(phy, name)
148 {
149 return trim(readfile(`/sys/class/ieee80211/${phy}/${name}`));
150 }
151
152 function macaddr_split(str)
153 {
154 return map(split(str, ":"), (val) => hex(val));
155 }
156
157 function macaddr_join(addr)
158 {
159 return join(":", map(addr, (val) => sprintf("%02x", val)));
160 }
161
162 function wdev_macaddr(wdev)
163 {
164 return trim(readfile(`/sys/class/net/${wdev}/address`));
165 }
166
167 const phy_proto = {
168 macaddr_init: function(used, options) {
169 this.macaddr_options = options ?? {};
170 this.macaddr_list = {};
171
172 if (type(used) == "object")
173 for (let addr in used)
174 this.macaddr_list[addr] = used[addr];
175 else
176 for (let addr in used)
177 this.macaddr_list[addr] = -1;
178
179 this.for_each_wdev((wdev) => {
180 let macaddr = wdev_macaddr(wdev);
181 this.macaddr_list[macaddr] ??= -1;
182 });
183
184 return this.macaddr_list;
185 },
186
187 macaddr_generate: function(data) {
188 let phy = this.name;
189 let idx = int(data.id ?? 0);
190 let mbssid = int(data.mbssid ?? 0) > 0;
191 let num_global = int(data.num_global ?? 1);
192 let use_global = !mbssid && idx < num_global;
193
194 let base_addr = phy_sysfs_file(phy, "macaddress");
195 if (!base_addr)
196 return null;
197
198 if (!idx && !mbssid)
199 return base_addr;
200
201 let base_mask = phy_sysfs_file(phy, "address_mask");
202 if (!base_mask)
203 return null;
204
205 if (base_mask == "00:00:00:00:00:00" && idx >= num_global) {
206 let addrs = split(phy_sysfs_file(phy, "addresses"), "\n");
207
208 if (idx < length(addrs))
209 return addrs[idx];
210
211 base_mask = "ff:ff:ff:ff:ff:ff";
212 }
213
214 let addr = macaddr_split(base_addr);
215 let mask = macaddr_split(base_mask);
216 let type;
217
218 if (mbssid)
219 type = "b5";
220 else if (use_global)
221 type = "add";
222 else if (mask[0] > 0)
223 type = "b1";
224 else if (mask[5] < 0xff)
225 type = "b5";
226 else
227 type = "add";
228
229 switch (type) {
230 case "b1":
231 if (!(addr[0] & 2))
232 idx--;
233 addr[0] |= 2;
234 addr[0] ^= idx << 2;
235 break;
236 case "b5":
237 if (mbssid)
238 addr[0] |= 2;
239 addr[5] ^= idx;
240 break;
241 default:
242 for (let i = 5; i > 0; i--) {
243 addr[i] += idx;
244 if (addr[i] < 256)
245 break;
246 addr[i] %= 256;
247 }
248 break;
249 }
250
251 return macaddr_join(addr);
252 },
253
254 macaddr_next: function(val) {
255 let data = this.macaddr_options ?? {};
256 let list = this.macaddr_list;
257
258 for (let i = 0; i < 32; i++) {
259 data.id = i;
260
261 let mac = this.macaddr_generate(data);
262 if (!mac)
263 return null;
264
265 if (list[mac] != null)
266 continue;
267
268 list[mac] = val != null ? val : -1;
269 return mac;
270 }
271 },
272
273 for_each_wdev: function(cb) {
274 let wdevs = glob(`/sys/class/ieee80211/${this.name}/device/net/*`);
275 wdevs = map(wdevs, (arg) => basename(arg));
276 for (let wdev in wdevs) {
277 if (basename(readlink(`/sys/class/net/${wdev}/phy80211`)) != this.name)
278 continue;
279
280 cb(wdev);
281 }
282 }
283 };
284
285 function phy_open(phy)
286 {
287 let phyidx = readfile(`/sys/class/ieee80211/${phy}/index`);
288 if (!phyidx)
289 return null;
290
291 return proto({
292 name: phy,
293 idx: int(phyidx)
294 }, phy_proto);
295 }
296
297 const vlist_proto = {
298 update: function(values, arg) {
299 let data = this.data;
300 let cb = this.cb;
301 let seq = { };
302 let new_data = {};
303 let old_data = {};
304
305 this.data = new_data;
306
307 if (type(values) == "object") {
308 for (let key in values) {
309 old_data[key] = data[key];
310 new_data[key] = values[key];
311 delete data[key];
312 }
313 } else {
314 for (let val in values) {
315 let cur_key = val[0];
316 let cur_obj = val[1];
317
318 old_data[cur_key] = data[cur_key];
319 new_data[cur_key] = val[1];
320 delete data[cur_key];
321 }
322 }
323
324 for (let key in data) {
325 cb(null, data[key], arg);
326 delete data[key];
327 }
328 for (let key in new_data)
329 cb(new_data[key], old_data[key], arg);
330 }
331 };
332
333 function is_equal(val1, val2) {
334 let t1 = type(val1);
335
336 if (t1 != type(val2))
337 return false;
338
339 if (t1 == "array") {
340 if (length(val1) != length(val2))
341 return false;
342
343 for (let i = 0; i < length(val1); i++)
344 if (!is_equal(val1[i], val2[i]))
345 return false;
346
347 return true;
348 } else if (t1 == "object") {
349 for (let key in val1)
350 if (!is_equal(val1[key], val2[key]))
351 return false;
352 for (let key in val2)
353 if (val1[key] == null)
354 return false;
355 return true;
356 } else {
357 return val1 == val2;
358 }
359 }
360
361 function vlist_new(cb) {
362 return proto({
363 cb: cb,
364 data: {}
365 }, vlist_proto);
366 }
367
368 export { wdev_remove, wdev_create, wdev_set_mesh_params, is_equal, vlist_new, phy_is_fullmac, phy_open };