hostapd: reimplement AP/STA support via ucode
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / utils / ucode.c
1 #include <unistd.h>
2 #include "ucode.h"
3 #include "utils/eloop.h"
4 #include "crypto/crypto.h"
5 #include "crypto/sha1.h"
6 #include "common/ieee802_11_common.h"
7 #include <libubox/uloop.h>
8 #include <ucode/compiler.h>
9
10 static uc_value_t *registry;
11 static uc_vm_t vm;
12 static struct uloop_timeout gc_timer;
13
14 static void uc_gc_timer(struct uloop_timeout *timeout)
15 {
16 ucv_gc(&vm);
17 }
18
19 uc_value_t *uc_wpa_printf(uc_vm_t *vm, size_t nargs)
20 {
21 uc_value_t *level = uc_fn_arg(0);
22 uc_value_t *ret, **args;
23 uc_cfn_ptr_t _sprintf;
24 int l = MSG_INFO;
25 int i, start = 0;
26
27 _sprintf = uc_stdlib_function("sprintf");
28 if (!sprintf)
29 return NULL;
30
31 if (ucv_type(level) == UC_INTEGER) {
32 l = ucv_int64_get(level);
33 start++;
34 }
35
36 if (nargs <= start)
37 return NULL;
38
39 ret = _sprintf(vm, nargs - start);
40 if (ucv_type(ret) != UC_STRING)
41 return NULL;
42
43 wpa_printf(l, "%s", ucv_string_get(ret));
44 ucv_put(ret);
45
46 return NULL;
47 }
48
49 uc_value_t *uc_wpa_freq_info(uc_vm_t *vm, size_t nargs)
50 {
51 uc_value_t *freq = uc_fn_arg(0);
52 uc_value_t *sec = uc_fn_arg(1);
53 int width = ucv_uint64_get(uc_fn_arg(2));
54 int freq_val, center_idx, center_ofs;
55 enum oper_chan_width chanwidth;
56 enum hostapd_hw_mode hw_mode;
57 u8 op_class, channel, tmp_channel;
58 const char *modestr;
59 int sec_channel = 0;
60 uc_value_t *ret;
61
62 if (ucv_type(freq) != UC_INTEGER)
63 return NULL;
64
65 freq_val = ucv_int64_get(freq);
66 if (ucv_type(sec) == UC_INTEGER)
67 sec_channel = ucv_int64_get(sec);
68 else if (sec)
69 return NULL;
70 else if (freq_val > 4000)
71 sec_channel = (freq_val / 20) & 1 ? 1 : -1;
72 else
73 sec_channel = freq_val < 2442 ? 1 : -1;
74
75 if (sec_channel != -1 && sec_channel != 1 && sec_channel != 0)
76 return NULL;
77
78 switch (width) {
79 case 0:
80 chanwidth = CONF_OPER_CHWIDTH_USE_HT;
81 break;
82 case 1:
83 chanwidth = CONF_OPER_CHWIDTH_80MHZ;
84 break;
85 case 2:
86 chanwidth = CONF_OPER_CHWIDTH_160MHZ;
87 break;
88 default:
89 return NULL;
90 }
91
92 hw_mode = ieee80211_freq_to_channel_ext(freq_val, sec_channel,
93 chanwidth, &op_class, &channel);
94 switch (hw_mode) {
95 case HOSTAPD_MODE_IEEE80211B:
96 modestr = "b";
97 break;
98 case HOSTAPD_MODE_IEEE80211G:
99 modestr = "g";
100 break;
101 case HOSTAPD_MODE_IEEE80211A:
102 modestr = "a";
103 break;
104 case HOSTAPD_MODE_IEEE80211AD:
105 modestr = "ad";
106 break;
107 default:
108 return NULL;
109 }
110
111 ret = ucv_object_new(vm);
112 ucv_object_add(ret, "op_class", ucv_int64_new(op_class));
113 ucv_object_add(ret, "channel", ucv_int64_new(channel));
114 ucv_object_add(ret, "hw_mode", ucv_int64_new(hw_mode));
115 ucv_object_add(ret, "hw_mode_str", ucv_get(ucv_string_new(modestr)));
116 ucv_object_add(ret, "sec_channel", ucv_int64_new(sec_channel));
117 ucv_object_add(ret, "frequency", ucv_int64_new(freq_val));
118
119 if (!sec_channel)
120 return ret;
121
122 center_ofs = freq_val >= 5900 ? 0 : 3;
123 tmp_channel = channel - center_ofs;
124 tmp_channel &= ~((8 << width) - 1);
125 center_idx = tmp_channel + center_ofs + (4 << width) - 1;
126
127 ucv_object_add(ret, "center_seg0_idx", ucv_int64_new(center_idx));
128 center_idx = (center_idx - channel) * 5 + freq_val;
129 ucv_object_add(ret, "center_freq1", ucv_int64_new(center_idx));
130
131 out:
132 return ret;
133 }
134
135 uc_value_t *uc_wpa_getpid(uc_vm_t *vm, size_t nargs)
136 {
137 return ucv_int64_new(getpid());
138 }
139
140 uc_value_t *uc_wpa_sha1(uc_vm_t *vm, size_t nargs)
141 {
142 u8 hash[SHA1_MAC_LEN];
143 char hash_hex[2 * ARRAY_SIZE(hash) + 1];
144 uc_value_t *val;
145 size_t *lens;
146 const u8 **args;
147 int i;
148
149 if (!nargs)
150 return NULL;
151
152 args = alloca(nargs * sizeof(*args));
153 lens = alloca(nargs * sizeof(*lens));
154 for (i = 0; i < nargs; i++) {
155 val = uc_fn_arg(i);
156 if (ucv_type(val) != UC_STRING)
157 return NULL;
158
159 args[i] = ucv_string_get(val);
160 lens[i] = ucv_string_length(val);
161 }
162
163 if (sha1_vector(nargs, args, lens, hash))
164 return NULL;
165
166 for (i = 0; i < ARRAY_SIZE(hash); i++)
167 sprintf(hash_hex + 2 * i, "%02x", hash[i]);
168
169 return ucv_string_new_length(hash_hex, 2 * ARRAY_SIZE(hash));
170 }
171
172 uc_vm_t *wpa_ucode_create_vm(void)
173 {
174 static uc_parse_config_t config = {
175 .strict_declarations = true,
176 .lstrip_blocks = true,
177 .trim_blocks = true,
178 .raw_mode = true
179 };
180
181 uc_search_path_init(&config.module_search_path);
182 uc_search_path_add(&config.module_search_path, HOSTAPD_UC_PATH "*.so");
183 uc_search_path_add(&config.module_search_path, HOSTAPD_UC_PATH "*.uc");
184
185 uc_vm_init(&vm, &config);
186
187 uc_stdlib_load(uc_vm_scope_get(&vm));
188 eloop_add_uloop();
189 gc_timer.cb = uc_gc_timer;
190
191 return &vm;
192 }
193
194 int wpa_ucode_run(const char *script)
195 {
196 uc_source_t *source;
197 uc_program_t *prog;
198 uc_value_t *ops;
199 char *err;
200 int ret;
201
202 source = uc_source_new_file(script);
203 if (!source)
204 return -1;
205
206 prog = uc_compile(vm.config, source, &err);
207 uc_source_put(source);
208 if (!prog) {
209 wpa_printf(MSG_ERROR, "Error loading ucode: %s\n", err);
210 return -1;
211 }
212
213 ret = uc_vm_execute(&vm, prog, &ops);
214 uc_program_put(prog);
215 if (ret || !ops)
216 return -1;
217
218 registry = ucv_array_new(&vm);
219 uc_vm_registry_set(&vm, "hostap.registry", registry);
220 ucv_array_set(registry, 0, ucv_get(ops));
221
222 return 0;
223 }
224
225 int wpa_ucode_call_prepare(const char *fname)
226 {
227 uc_value_t *obj, *func;
228
229 if (!registry)
230 return -1;
231
232 obj = ucv_array_get(registry, 0);
233 if (!obj)
234 return -1;
235
236 func = ucv_object_get(obj, fname, NULL);
237 if (!ucv_is_callable(func))
238 return -1;
239
240 uc_vm_stack_push(&vm, ucv_get(obj));
241 uc_vm_stack_push(&vm, ucv_get(func));
242
243 return 0;
244 }
245
246 uc_value_t *wpa_ucode_global_init(const char *name, uc_resource_type_t *global_type)
247 {
248 uc_value_t *global = uc_resource_new(global_type, NULL);
249 uc_value_t *proto;
250
251 uc_vm_registry_set(&vm, "hostap.global", global);
252 proto = ucv_prototype_get(global);
253 ucv_object_add(proto, "data", ucv_get(ucv_object_new(&vm)));
254
255 #define ADD_CONST(x) ucv_object_add(proto, #x, ucv_int64_new(x))
256 ADD_CONST(MSG_EXCESSIVE);
257 ADD_CONST(MSG_MSGDUMP);
258 ADD_CONST(MSG_DEBUG);
259 ADD_CONST(MSG_INFO);
260 ADD_CONST(MSG_WARNING);
261 ADD_CONST(MSG_ERROR);
262 #undef ADD_CONST
263
264 ucv_object_add(uc_vm_scope_get(&vm), name, ucv_get(global));
265
266 return global;
267 }
268
269 static uc_value_t *wpa_ucode_prototype_clone(uc_value_t *uval)
270 {
271 uc_value_t *proto, *proto_new;
272
273 proto = ucv_prototype_get(uval);
274 proto_new = ucv_object_new(&vm);
275
276 ucv_object_foreach(proto, key, val)
277 ucv_object_add(proto_new, key, ucv_get(val));
278 ucv_prototype_set(uval, ucv_get(proto));
279
280 return proto;
281 }
282
283 void wpa_ucode_registry_add(uc_value_t *reg, uc_value_t *val, int *idx)
284 {
285 uc_value_t *data;
286 int i = 0;
287
288 while (ucv_array_get(reg, i))
289 i++;
290
291 ucv_array_set(reg, i, ucv_get(val));
292
293 data = ucv_object_new(&vm);
294 ucv_object_add(wpa_ucode_prototype_clone(val), "data", ucv_get(data));
295
296 *idx = i + 1;
297 }
298
299 uc_value_t *wpa_ucode_registry_get(uc_value_t *reg, int idx)
300 {
301 if (!idx)
302 return NULL;
303
304 return ucv_array_get(reg, idx - 1);
305 }
306
307 uc_value_t *wpa_ucode_registry_remove(uc_value_t *reg, int idx)
308 {
309 uc_value_t *val = wpa_ucode_registry_get(reg, idx);
310
311 if (val)
312 ucv_array_set(reg, idx - 1, NULL);
313
314 return val;
315 }
316
317
318 uc_value_t *wpa_ucode_call(size_t nargs)
319 {
320 if (uc_vm_call(&vm, true, nargs) != EXCEPTION_NONE)
321 return NULL;
322
323 if (!gc_timer.pending)
324 uloop_timeout_set(&gc_timer, 10);
325
326 return uc_vm_stack_pop(&vm);
327 }
328
329 void wpa_ucode_free_vm(void)
330 {
331 if (!vm.config)
332 return;
333
334 uc_search_path_free(&vm.config->module_search_path);
335 uc_vm_free(&vm);
336 registry = NULL;
337 vm = (uc_vm_t){};
338 }