e79f2420c07a0af702b3317aa160e28bce71c45f
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / ap / ucode.c
1 #include <sys/un.h>
2
3 #include "utils/includes.h"
4 #include "utils/common.h"
5 #include "utils/ucode.h"
6 #include "hostapd.h"
7 #include "beacon.h"
8 #include "hw_features.h"
9 #include "ap_drv_ops.h"
10 #include "dfs.h"
11 #include "acs.h"
12 #include <libubox/uloop.h>
13
14 static uc_resource_type_t *global_type, *bss_type, *iface_type;
15 static struct hapd_interfaces *interfaces;
16 static uc_value_t *global, *bss_registry, *iface_registry;
17 static uc_vm_t *vm;
18
19 static uc_value_t *
20 hostapd_ucode_bss_get_uval(struct hostapd_data *hapd)
21 {
22 uc_value_t *val;
23
24 if (hapd->ucode.idx)
25 return wpa_ucode_registry_get(bss_registry, hapd->ucode.idx);
26
27 val = uc_resource_new(bss_type, hapd);
28 hapd->ucode.idx = wpa_ucode_registry_add(bss_registry, val);
29
30 return val;
31 }
32
33 static uc_value_t *
34 hostapd_ucode_iface_get_uval(struct hostapd_iface *hapd)
35 {
36 uc_value_t *val;
37
38 if (hapd->ucode.idx)
39 return wpa_ucode_registry_get(iface_registry, hapd->ucode.idx);
40
41 val = uc_resource_new(iface_type, hapd);
42 hapd->ucode.idx = wpa_ucode_registry_add(iface_registry, val);
43
44 return val;
45 }
46
47 static void
48 hostapd_ucode_update_bss_list(struct hostapd_iface *iface, uc_value_t *if_bss, uc_value_t *bss)
49 {
50 uc_value_t *list;
51 int i;
52
53 list = ucv_array_new(vm);
54 for (i = 0; i < iface->num_bss; i++) {
55 struct hostapd_data *hapd = iface->bss[i];
56 uc_value_t *val = hostapd_ucode_bss_get_uval(hapd);
57
58 ucv_array_set(list, i, ucv_get(ucv_string_new(hapd->conf->iface)));
59 ucv_object_add(bss, hapd->conf->iface, ucv_get(val));
60 }
61 ucv_object_add(if_bss, iface->phy, ucv_get(list));
62 }
63
64 static void
65 hostapd_ucode_update_interfaces(void)
66 {
67 uc_value_t *ifs = ucv_object_new(vm);
68 uc_value_t *if_bss = ucv_array_new(vm);
69 uc_value_t *bss = ucv_object_new(vm);
70 int i;
71
72 for (i = 0; i < interfaces->count; i++) {
73 struct hostapd_iface *iface = interfaces->iface[i];
74
75 ucv_object_add(ifs, iface->phy, ucv_get(hostapd_ucode_iface_get_uval(iface)));
76 hostapd_ucode_update_bss_list(iface, if_bss, bss);
77 }
78
79 ucv_object_add(ucv_prototype_get(global), "interfaces", ucv_get(ifs));
80 ucv_object_add(ucv_prototype_get(global), "interface_bss", ucv_get(if_bss));
81 ucv_object_add(ucv_prototype_get(global), "bss", ucv_get(bss));
82 ucv_gc(vm);
83 }
84
85 static uc_value_t *
86 uc_hostapd_add_iface(uc_vm_t *vm, size_t nargs)
87 {
88 uc_value_t *iface = uc_fn_arg(0);
89 int ret;
90
91 if (ucv_type(iface) != UC_STRING)
92 return ucv_int64_new(-1);
93
94 ret = hostapd_add_iface(interfaces, ucv_string_get(iface));
95 hostapd_ucode_update_interfaces();
96
97 return ucv_int64_new(ret);
98 }
99
100 static uc_value_t *
101 uc_hostapd_remove_iface(uc_vm_t *vm, size_t nargs)
102 {
103 uc_value_t *iface = uc_fn_arg(0);
104
105 if (ucv_type(iface) != UC_STRING)
106 return NULL;
107
108 hostapd_remove_iface(interfaces, ucv_string_get(iface));
109 hostapd_ucode_update_interfaces();
110
111 return NULL;
112 }
113
114 static struct hostapd_vlan *
115 bss_conf_find_vlan(struct hostapd_bss_config *bss, int id)
116 {
117 struct hostapd_vlan *vlan;
118
119 for (vlan = bss->vlan; vlan; vlan = vlan->next)
120 if (vlan->vlan_id == id)
121 return vlan;
122
123 return NULL;
124 }
125
126 static int
127 bss_conf_rename_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan,
128 const char *ifname)
129 {
130 if (!strcmp(ifname, vlan->ifname))
131 return 0;
132
133 hostapd_drv_if_rename(hapd, WPA_IF_AP_VLAN, vlan->ifname, ifname);
134 os_strlcpy(vlan->ifname, ifname, sizeof(vlan->ifname));
135
136 return 0;
137 }
138
139 static int
140 bss_reload_vlans(struct hostapd_data *hapd, struct hostapd_bss_config *bss)
141 {
142 struct hostapd_bss_config *old_bss = hapd->conf;
143 struct hostapd_vlan *vlan, *vlan_new, *wildcard;
144 char ifname[IFNAMSIZ + 1], vlan_ifname[IFNAMSIZ + 1], *pos;
145 int ret;
146
147 vlan = bss_conf_find_vlan(old_bss, VLAN_ID_WILDCARD);
148 wildcard = bss_conf_find_vlan(bss, VLAN_ID_WILDCARD);
149 if (!!vlan != !!wildcard)
150 return -1;
151
152 if (vlan && wildcard && strcmp(vlan->ifname, wildcard->ifname) != 0)
153 strcpy(vlan->ifname, wildcard->ifname);
154 else
155 wildcard = NULL;
156
157 for (vlan = bss->vlan; vlan; vlan = vlan->next) {
158 if (vlan->vlan_id == VLAN_ID_WILDCARD ||
159 vlan->dynamic_vlan > 0)
160 continue;
161
162 if (!bss_conf_find_vlan(old_bss, vlan->vlan_id))
163 return -1;
164 }
165
166 for (vlan = old_bss->vlan; vlan; vlan = vlan->next) {
167 if (vlan->vlan_id == VLAN_ID_WILDCARD)
168 continue;
169
170 if (vlan->dynamic_vlan == 0) {
171 vlan_new = bss_conf_find_vlan(bss, vlan->vlan_id);
172 if (!vlan_new)
173 return -1;
174
175 if (bss_conf_rename_vlan(hapd, vlan, vlan_new->ifname))
176 return -1;
177
178 continue;
179 }
180
181 if (!wildcard)
182 continue;
183
184 os_strlcpy(ifname, wildcard->ifname, sizeof(ifname));
185 pos = os_strchr(ifname, '#');
186 if (!pos)
187 return -1;
188
189 *pos++ = '\0';
190 ret = os_snprintf(vlan_ifname, sizeof(vlan_ifname), "%s%d%s",
191 ifname, vlan->vlan_id, pos);
192 if (os_snprintf_error(sizeof(vlan_ifname), ret))
193 return -1;
194
195 if (bss_conf_rename_vlan(hapd, vlan, vlan_ifname))
196 return -1;
197 }
198
199 return 0;
200 }
201
202 static uc_value_t *
203 uc_hostapd_bss_set_config(uc_vm_t *vm, size_t nargs)
204 {
205 struct hostapd_data *hapd = uc_fn_thisval("hostapd.bss");
206 struct hostapd_bss_config *old_bss;
207 struct hostapd_iface *iface;
208 struct hostapd_config *conf;
209 uc_value_t *file = uc_fn_arg(0);
210 uc_value_t *index = uc_fn_arg(1);
211 uc_value_t *files_only = uc_fn_arg(2);
212 unsigned int i, idx = 0;
213 int ret = -1;
214
215 if (!hapd || ucv_type(file) != UC_STRING)
216 goto out;
217
218 if (ucv_type(index) == UC_INTEGER)
219 idx = ucv_int64_get(index);
220
221 iface = hapd->iface;
222 conf = interfaces->config_read_cb(ucv_string_get(file));
223 if (!conf)
224 goto out;
225
226 if (idx > conf->num_bss || !conf->bss[idx])
227 goto free;
228
229 if (ucv_boolean_get(files_only)) {
230 struct hostapd_bss_config *bss = conf->bss[idx];
231 struct hostapd_bss_config *old_bss = hapd->conf;
232
233 #define swap_field(name) \
234 do { \
235 void *ptr = old_bss->name; \
236 old_bss->name = bss->name; \
237 bss->name = ptr; \
238 } while (0)
239
240 swap_field(ssid.wpa_psk_file);
241 ret = bss_reload_vlans(hapd, bss);
242 goto done;
243 }
244
245 hostapd_bss_deinit_no_free(hapd);
246 hostapd_drv_stop_ap(hapd);
247 hostapd_free_hapd_data(hapd);
248
249 old_bss = hapd->conf;
250 for (i = 0; i < iface->conf->num_bss; i++)
251 if (iface->conf->bss[i] == hapd->conf)
252 iface->conf->bss[i] = conf->bss[idx];
253 hapd->conf = conf->bss[idx];
254 conf->bss[idx] = old_bss;
255
256 hostapd_setup_bss(hapd, hapd == iface->bss[0], true);
257 hostapd_ucode_update_interfaces();
258
259 done:
260 ret = 0;
261 free:
262 hostapd_config_free(conf);
263 out:
264 return ucv_int64_new(ret);
265 }
266
267 static void
268 hostapd_remove_iface_bss_conf(struct hostapd_config *iconf,
269 struct hostapd_bss_config *conf)
270 {
271 int i;
272
273 for (i = 0; i < iconf->num_bss; i++)
274 if (iconf->bss[i] == conf)
275 break;
276
277 if (i == iconf->num_bss)
278 return;
279
280 for (i++; i < iconf->num_bss; i++)
281 iconf->bss[i - 1] = iconf->bss[i];
282 iconf->num_bss--;
283 }
284
285
286 static uc_value_t *
287 uc_hostapd_bss_delete(uc_vm_t *vm, size_t nargs)
288 {
289 struct hostapd_data *hapd = uc_fn_thisval("hostapd.bss");
290 struct hostapd_iface *iface;
291 int i, idx;
292
293 if (!hapd)
294 return NULL;
295
296 iface = hapd->iface;
297 if (iface->num_bss == 1) {
298 wpa_printf(MSG_ERROR, "trying to delete last bss of an iface: %s\n", hapd->conf->iface);
299 return NULL;
300 }
301
302 for (idx = 0; idx < iface->num_bss; idx++)
303 if (iface->bss[idx] == hapd)
304 break;
305
306 if (idx == iface->num_bss)
307 return NULL;
308
309 for (i = idx + 1; i < iface->num_bss; i++)
310 iface->bss[i - 1] = iface->bss[i];
311
312 iface->num_bss--;
313
314 iface->bss[0]->interface_added = 0;
315 hostapd_drv_set_first_bss(iface->bss[0]);
316 hapd->interface_added = 1;
317
318 hostapd_drv_stop_ap(hapd);
319 hostapd_bss_deinit(hapd);
320 hostapd_remove_iface_bss_conf(iface->conf, hapd->conf);
321 hostapd_config_free_bss(hapd->conf);
322 os_free(hapd);
323
324 hostapd_ucode_update_interfaces();
325 ucv_gc(vm);
326
327 return NULL;
328 }
329
330 static uc_value_t *
331 uc_hostapd_iface_add_bss(uc_vm_t *vm, size_t nargs)
332 {
333 struct hostapd_iface *iface = uc_fn_thisval("hostapd.iface");
334 struct hostapd_bss_config *bss;
335 struct hostapd_config *conf;
336 struct hostapd_data *hapd;
337 uc_value_t *file = uc_fn_arg(0);
338 uc_value_t *index = uc_fn_arg(1);
339 unsigned int idx = 0;
340 uc_value_t *ret = NULL;
341
342 if (!iface || ucv_type(file) != UC_STRING)
343 goto out;
344
345 if (ucv_type(index) == UC_INTEGER)
346 idx = ucv_int64_get(index);
347
348 conf = interfaces->config_read_cb(ucv_string_get(file));
349 if (!conf || idx > conf->num_bss || !conf->bss[idx])
350 goto out;
351
352 bss = conf->bss[idx];
353 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
354 if (!hapd)
355 goto out;
356
357 hapd->driver = iface->bss[0]->driver;
358 hapd->drv_priv = iface->bss[0]->drv_priv;
359 if (interfaces->ctrl_iface_init &&
360 interfaces->ctrl_iface_init(hapd) < 0)
361 goto free_hapd;
362
363 if (iface->state == HAPD_IFACE_ENABLED &&
364 hostapd_setup_bss(hapd, -1, true))
365 goto deinit_ctrl;
366
367 iface->bss = os_realloc_array(iface->bss, iface->num_bss + 1,
368 sizeof(*iface->bss));
369 iface->bss[iface->num_bss++] = hapd;
370
371 iface->conf->bss = os_realloc_array(iface->conf->bss,
372 iface->conf->num_bss + 1,
373 sizeof(*iface->conf->bss));
374 iface->conf->bss[iface->conf->num_bss] = bss;
375 conf->bss[idx] = NULL;
376 ret = hostapd_ucode_bss_get_uval(hapd);
377 hostapd_ucode_update_interfaces();
378 goto out;
379
380 deinit_ctrl:
381 if (interfaces->ctrl_iface_deinit)
382 interfaces->ctrl_iface_deinit(hapd);
383 free_hapd:
384 hostapd_free_hapd_data(hapd);
385 os_free(hapd);
386 out:
387 hostapd_config_free(conf);
388 return ret;
389 }
390
391 static uc_value_t *
392 uc_hostapd_iface_set_bss_order(uc_vm_t *vm, size_t nargs)
393 {
394 struct hostapd_iface *iface = uc_fn_thisval("hostapd.iface");
395 uc_value_t *bss_list = uc_fn_arg(0);
396 struct hostapd_data **new_bss;
397 struct hostapd_bss_config **new_conf;
398
399 if (!iface)
400 return NULL;
401
402 if (ucv_type(bss_list) != UC_ARRAY ||
403 ucv_array_length(bss_list) != iface->num_bss)
404 return NULL;
405
406 new_bss = calloc(iface->num_bss, sizeof(*new_bss));
407 new_conf = calloc(iface->num_bss, sizeof(*new_conf));
408 for (size_t i = 0; i < iface->num_bss; i++) {
409 struct hostapd_data *bss;
410
411 bss = ucv_resource_data(ucv_array_get(bss_list, i), "hostapd.bss");
412 if (bss->iface != iface)
413 goto free;
414
415 for (size_t k = 0; k < i; k++)
416 if (new_bss[k] == bss)
417 goto free;
418
419 new_bss[i] = bss;
420 new_conf[i] = bss->conf;
421 }
422
423 new_bss[0]->interface_added = 0;
424 for (size_t i = 1; i < iface->num_bss; i++)
425 new_bss[i]->interface_added = 1;
426
427 free(iface->bss);
428 iface->bss = new_bss;
429
430 free(iface->conf->bss);
431 iface->conf->bss = new_conf;
432 iface->conf->num_bss = iface->num_bss;
433 hostapd_drv_set_first_bss(iface->bss[0]);
434
435 return ucv_boolean_new(true);
436
437 free:
438 free(new_bss);
439 free(new_conf);
440 return NULL;
441 }
442
443 static uc_value_t *
444 uc_hostapd_bss_ctrl(uc_vm_t *vm, size_t nargs)
445 {
446 struct hostapd_data *hapd = uc_fn_thisval("hostapd.bss");
447 uc_value_t *arg = uc_fn_arg(0);
448 struct sockaddr_storage from = {};
449 static char reply[4096];
450 int reply_len;
451
452 if (!hapd || !interfaces->ctrl_iface_recv ||
453 ucv_type(arg) != UC_STRING)
454 return NULL;
455
456 reply_len = interfaces->ctrl_iface_recv(hapd, ucv_string_get(arg),
457 reply, sizeof(reply),
458 &from, sizeof(from));
459 if (reply_len < 0)
460 return NULL;
461
462 if (reply_len && reply[reply_len - 1] == '\n')
463 reply_len--;
464
465 return ucv_string_new_length(reply, reply_len);
466 }
467
468 static uc_value_t *
469 uc_hostapd_iface_stop(uc_vm_t *vm, size_t nargs)
470 {
471 struct hostapd_iface *iface = uc_fn_thisval("hostapd.iface");
472 int i;
473
474 switch (iface->state) {
475 case HAPD_IFACE_ENABLED:
476 case HAPD_IFACE_DISABLED:
477 break;
478 #ifdef CONFIG_ACS
479 case HAPD_IFACE_ACS:
480 acs_cleanup(iface);
481 iface->scan_cb = NULL;
482 /* fallthrough */
483 #endif
484 default:
485 hostapd_disable_iface(iface);
486 break;
487 }
488
489 if (iface->state != HAPD_IFACE_ENABLED)
490 hostapd_disable_iface(iface);
491
492 for (i = 0; i < iface->num_bss; i++) {
493 struct hostapd_data *hapd = iface->bss[i];
494
495 hostapd_drv_stop_ap(hapd);
496 hapd->beacon_set_done = 0;
497 }
498
499 return NULL;
500 }
501
502 static uc_value_t *
503 uc_hostapd_iface_start(uc_vm_t *vm, size_t nargs)
504 {
505 struct hostapd_iface *iface = uc_fn_thisval("hostapd.iface");
506 uc_value_t *info = uc_fn_arg(0);
507 struct hostapd_config *conf;
508 bool changed = false;
509 uint64_t intval;
510 int i;
511
512 if (!iface)
513 return NULL;
514
515 if (!info) {
516 iface->freq = 0;
517 goto out;
518 }
519
520 if (ucv_type(info) != UC_OBJECT)
521 return NULL;
522
523 #define UPDATE_VAL(field, name) \
524 if ((intval = ucv_int64_get(ucv_object_get(info, name, NULL))) && \
525 !errno && intval != conf->field) do { \
526 conf->field = intval; \
527 changed = true; \
528 } while(0)
529
530 conf = iface->conf;
531 UPDATE_VAL(op_class, "op_class");
532 UPDATE_VAL(hw_mode, "hw_mode");
533 UPDATE_VAL(channel, "channel");
534 UPDATE_VAL(secondary_channel, "sec_channel");
535 if (!changed &&
536 (iface->bss[0]->beacon_set_done ||
537 iface->state == HAPD_IFACE_DFS))
538 return ucv_boolean_new(true);
539
540 intval = ucv_int64_get(ucv_object_get(info, "center_seg0_idx", NULL));
541 if (!errno)
542 hostapd_set_oper_centr_freq_seg0_idx(conf, intval);
543
544 intval = ucv_int64_get(ucv_object_get(info, "center_seg1_idx", NULL));
545 if (!errno)
546 hostapd_set_oper_centr_freq_seg1_idx(conf, intval);
547
548 intval = ucv_int64_get(ucv_object_get(info, "oper_chwidth", NULL));
549 if (!errno)
550 hostapd_set_oper_chwidth(conf, intval);
551
552 intval = ucv_int64_get(ucv_object_get(info, "frequency", NULL));
553 if (!errno)
554 iface->freq = intval;
555 else
556 iface->freq = 0;
557 conf->acs = 0;
558
559 out:
560 switch (iface->state) {
561 case HAPD_IFACE_DISABLED:
562 break;
563 case HAPD_IFACE_ENABLED:
564 if (!hostapd_is_dfs_required(iface) ||
565 hostapd_is_dfs_chan_available(iface))
566 break;
567 wpa_printf(MSG_INFO, "DFS CAC required on new channel, restart interface");
568 /* fallthrough */
569 default:
570 hostapd_disable_iface(iface);
571 break;
572 }
573
574 if (conf->channel && !iface->freq)
575 iface->freq = hostapd_hw_get_freq(iface->bss[0], conf->channel);
576
577 if (iface->state != HAPD_IFACE_ENABLED) {
578 hostapd_enable_iface(iface);
579 return ucv_boolean_new(true);
580 }
581
582 for (i = 0; i < iface->num_bss; i++) {
583 struct hostapd_data *hapd = iface->bss[i];
584 int ret;
585
586 hapd->conf->start_disabled = 0;
587 hostapd_set_freq(hapd, conf->hw_mode, iface->freq,
588 conf->channel,
589 conf->enable_edmg,
590 conf->edmg_channel,
591 conf->ieee80211n,
592 conf->ieee80211ac,
593 conf->ieee80211ax,
594 conf->ieee80211be,
595 conf->secondary_channel,
596 hostapd_get_oper_chwidth(conf),
597 hostapd_get_oper_centr_freq_seg0_idx(conf),
598 hostapd_get_oper_centr_freq_seg1_idx(conf));
599
600 ieee802_11_set_beacon(hapd);
601 }
602
603 return ucv_boolean_new(true);
604 }
605
606 static uc_value_t *
607 uc_hostapd_iface_switch_channel(uc_vm_t *vm, size_t nargs)
608 {
609 struct hostapd_iface *iface = uc_fn_thisval("hostapd.iface");
610 uc_value_t *info = uc_fn_arg(0);
611 struct hostapd_config *conf;
612 struct csa_settings csa = {};
613 uint64_t intval;
614 int i, ret = 0;
615
616 if (!iface || ucv_type(info) != UC_OBJECT)
617 return NULL;
618
619 conf = iface->conf;
620 if ((intval = ucv_int64_get(ucv_object_get(info, "csa_count", NULL))) && !errno)
621 csa.cs_count = intval;
622 if ((intval = ucv_int64_get(ucv_object_get(info, "sec_channel", NULL))) && !errno)
623 csa.freq_params.sec_channel_offset = intval;
624
625 csa.freq_params.ht_enabled = conf->ieee80211n;
626 csa.freq_params.vht_enabled = conf->ieee80211ac;
627 csa.freq_params.he_enabled = conf->ieee80211ax;
628 #ifdef CONFIG_IEEE80211BE
629 csa.freq_params.eht_enabled = conf->ieee80211be;
630 #endif
631 intval = ucv_int64_get(ucv_object_get(info, "oper_chwidth", NULL));
632 if (errno)
633 intval = hostapd_get_oper_chwidth(conf);
634 if (intval)
635 csa.freq_params.bandwidth = 40 << intval;
636 else
637 csa.freq_params.bandwidth = csa.freq_params.sec_channel_offset ? 40 : 20;
638
639 if ((intval = ucv_int64_get(ucv_object_get(info, "frequency", NULL))) && !errno)
640 csa.freq_params.freq = intval;
641 if ((intval = ucv_int64_get(ucv_object_get(info, "center_freq1", NULL))) && !errno)
642 csa.freq_params.center_freq1 = intval;
643 if ((intval = ucv_int64_get(ucv_object_get(info, "center_freq2", NULL))) && !errno)
644 csa.freq_params.center_freq2 = intval;
645
646 for (i = 0; i < iface->num_bss; i++)
647 ret = hostapd_switch_channel(iface->bss[i], &csa);
648
649 return ucv_boolean_new(!ret);
650 }
651
652 static uc_value_t *
653 uc_hostapd_bss_rename(uc_vm_t *vm, size_t nargs)
654 {
655 struct hostapd_data *hapd = uc_fn_thisval("hostapd.bss");
656 uc_value_t *ifname_arg = uc_fn_arg(0);
657 char prev_ifname[IFNAMSIZ + 1];
658 struct sta_info *sta;
659 const char *ifname;
660 int ret;
661
662 if (!hapd || ucv_type(ifname_arg) != UC_STRING)
663 return NULL;
664
665 os_strlcpy(prev_ifname, hapd->conf->iface, sizeof(prev_ifname));
666 ifname = ucv_string_get(ifname_arg);
667
668 hostapd_ubus_free_bss(hapd);
669 if (interfaces->ctrl_iface_deinit)
670 interfaces->ctrl_iface_deinit(hapd);
671
672 ret = hostapd_drv_if_rename(hapd, WPA_IF_AP_BSS, NULL, ifname);
673 if (ret)
674 goto out;
675
676 for (sta = hapd->sta_list; sta; sta = sta->next) {
677 char cur_name[IFNAMSIZ + 1], new_name[IFNAMSIZ + 1];
678
679 if (!(sta->flags & WLAN_STA_WDS) || sta->pending_wds_enable)
680 continue;
681
682 snprintf(cur_name, sizeof(cur_name), "%s.sta%d", prev_ifname, sta->aid);
683 snprintf(new_name, sizeof(new_name), "%s.sta%d", ifname, sta->aid);
684 hostapd_drv_if_rename(hapd, WPA_IF_AP_VLAN, cur_name, new_name);
685 }
686
687 if (!strncmp(hapd->conf->ssid.vlan, hapd->conf->iface, sizeof(hapd->conf->ssid.vlan)))
688 os_strlcpy(hapd->conf->ssid.vlan, ifname, sizeof(hapd->conf->ssid.vlan));
689 os_strlcpy(hapd->conf->iface, ifname, sizeof(hapd->conf->iface));
690 hostapd_ubus_add_bss(hapd);
691
692 hostapd_ucode_update_interfaces();
693 out:
694 if (interfaces->ctrl_iface_init)
695 interfaces->ctrl_iface_init(hapd);
696
697 return ret ? NULL : ucv_boolean_new(true);
698 }
699
700
701 int hostapd_ucode_init(struct hapd_interfaces *ifaces)
702 {
703 static const uc_function_list_t global_fns[] = {
704 { "printf", uc_wpa_printf },
705 { "getpid", uc_wpa_getpid },
706 { "sha1", uc_wpa_sha1 },
707 { "freq_info", uc_wpa_freq_info },
708 { "add_iface", uc_hostapd_add_iface },
709 { "remove_iface", uc_hostapd_remove_iface },
710 };
711 static const uc_function_list_t bss_fns[] = {
712 { "ctrl", uc_hostapd_bss_ctrl },
713 { "set_config", uc_hostapd_bss_set_config },
714 { "rename", uc_hostapd_bss_rename },
715 { "delete", uc_hostapd_bss_delete },
716 };
717 static const uc_function_list_t iface_fns[] = {
718 { "set_bss_order", uc_hostapd_iface_set_bss_order },
719 { "add_bss", uc_hostapd_iface_add_bss },
720 { "stop", uc_hostapd_iface_stop },
721 { "start", uc_hostapd_iface_start },
722 { "switch_channel", uc_hostapd_iface_switch_channel },
723 };
724 uc_value_t *data, *proto;
725
726 interfaces = ifaces;
727 vm = wpa_ucode_create_vm();
728
729 global_type = uc_type_declare(vm, "hostapd.global", global_fns, NULL);
730 bss_type = uc_type_declare(vm, "hostapd.bss", bss_fns, NULL);
731 iface_type = uc_type_declare(vm, "hostapd.iface", iface_fns, NULL);
732
733 bss_registry = ucv_array_new(vm);
734 uc_vm_registry_set(vm, "hostap.bss_registry", bss_registry);
735
736 iface_registry = ucv_array_new(vm);
737 uc_vm_registry_set(vm, "hostap.iface_registry", iface_registry);
738
739 global = wpa_ucode_global_init("hostapd", global_type);
740
741 if (wpa_ucode_run(HOSTAPD_UC_PATH "hostapd.uc"))
742 goto free_vm;
743 ucv_gc(vm);
744
745 return 0;
746
747 free_vm:
748 wpa_ucode_free_vm();
749 return -1;
750 }
751
752 void hostapd_ucode_free(void)
753 {
754 if (wpa_ucode_call_prepare("shutdown") == 0)
755 ucv_put(wpa_ucode_call(0));
756 wpa_ucode_free_vm();
757 }
758
759 void hostapd_ucode_free_iface(struct hostapd_iface *iface)
760 {
761 wpa_ucode_registry_remove(iface_registry, iface->ucode.idx);
762 }
763
764 void hostapd_ucode_add_bss(struct hostapd_data *hapd)
765 {
766 uc_value_t *val;
767
768 if (wpa_ucode_call_prepare("bss_add"))
769 return;
770
771 val = hostapd_ucode_bss_get_uval(hapd);
772 uc_value_push(ucv_get(ucv_string_new(hapd->conf->iface)));
773 uc_value_push(ucv_get(val));
774 ucv_put(wpa_ucode_call(2));
775 ucv_gc(vm);
776 }
777
778 void hostapd_ucode_reload_bss(struct hostapd_data *hapd)
779 {
780 uc_value_t *val;
781
782 if (wpa_ucode_call_prepare("bss_reload"))
783 return;
784
785 val = hostapd_ucode_bss_get_uval(hapd);
786 uc_value_push(ucv_get(ucv_string_new(hapd->conf->iface)));
787 uc_value_push(ucv_get(val));
788 ucv_put(wpa_ucode_call(2));
789 ucv_gc(vm);
790 }
791
792 void hostapd_ucode_free_bss(struct hostapd_data *hapd)
793 {
794 uc_value_t *val;
795
796 val = wpa_ucode_registry_remove(bss_registry, hapd->ucode.idx);
797 if (!val)
798 return;
799
800 hapd->ucode.idx = 0;
801 if (wpa_ucode_call_prepare("bss_remove"))
802 return;
803
804 uc_value_push(ucv_string_new(hapd->conf->iface));
805 uc_value_push(ucv_get(val));
806 ucv_put(wpa_ucode_call(2));
807 ucv_gc(vm);
808 }