hostapd: reset center_seg0_idx for 2.4 GHz
[openwrt/staging/hauke.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 if (freq_val >= 5900)
123 center_ofs = 0;
124 else if (freq_val >= 5745)
125 center_ofs = 20;
126 else
127 center_ofs = 35;
128 tmp_channel = channel - center_ofs;
129 tmp_channel &= ~((8 << width) - 1);
130 center_idx = tmp_channel + center_ofs + (4 << width) - 1;
131
132 if (freq_val < 3000)
133 ucv_object_add(ret, "center_seg0_idx", ucv_int64_new(0));
134 else
135 ucv_object_add(ret, "center_seg0_idx", ucv_int64_new(center_idx));
136 center_idx = (center_idx - channel) * 5 + freq_val;
137 ucv_object_add(ret, "center_freq1", ucv_int64_new(center_idx));
138
139 out:
140 return ret;
141 }
142
143 uc_value_t *uc_wpa_getpid(uc_vm_t *vm, size_t nargs)
144 {
145 return ucv_int64_new(getpid());
146 }
147
148 uc_value_t *uc_wpa_sha1(uc_vm_t *vm, size_t nargs)
149 {
150 u8 hash[SHA1_MAC_LEN];
151 char hash_hex[2 * ARRAY_SIZE(hash) + 1];
152 uc_value_t *val;
153 size_t *lens;
154 const u8 **args;
155 int i;
156
157 if (!nargs)
158 return NULL;
159
160 args = alloca(nargs * sizeof(*args));
161 lens = alloca(nargs * sizeof(*lens));
162 for (i = 0; i < nargs; i++) {
163 val = uc_fn_arg(i);
164 if (ucv_type(val) != UC_STRING)
165 return NULL;
166
167 args[i] = ucv_string_get(val);
168 lens[i] = ucv_string_length(val);
169 }
170
171 if (sha1_vector(nargs, args, lens, hash))
172 return NULL;
173
174 for (i = 0; i < ARRAY_SIZE(hash); i++)
175 sprintf(hash_hex + 2 * i, "%02x", hash[i]);
176
177 return ucv_string_new_length(hash_hex, 2 * ARRAY_SIZE(hash));
178 }
179
180 uc_vm_t *wpa_ucode_create_vm(void)
181 {
182 static uc_parse_config_t config = {
183 .strict_declarations = true,
184 .lstrip_blocks = true,
185 .trim_blocks = true,
186 .raw_mode = true
187 };
188
189 uc_search_path_init(&config.module_search_path);
190 uc_search_path_add(&config.module_search_path, HOSTAPD_UC_PATH "*.so");
191 uc_search_path_add(&config.module_search_path, HOSTAPD_UC_PATH "*.uc");
192
193 uc_vm_init(&vm, &config);
194
195 uc_stdlib_load(uc_vm_scope_get(&vm));
196 eloop_add_uloop();
197 gc_timer.cb = uc_gc_timer;
198
199 return &vm;
200 }
201
202 int wpa_ucode_run(const char *script)
203 {
204 uc_source_t *source;
205 uc_program_t *prog;
206 uc_value_t *ops;
207 char *err;
208 int ret;
209
210 source = uc_source_new_file(script);
211 if (!source)
212 return -1;
213
214 prog = uc_compile(vm.config, source, &err);
215 uc_source_put(source);
216 if (!prog) {
217 wpa_printf(MSG_ERROR, "Error loading ucode: %s\n", err);
218 return -1;
219 }
220
221 ret = uc_vm_execute(&vm, prog, &ops);
222 uc_program_put(prog);
223 if (ret || !ops)
224 return -1;
225
226 registry = ucv_array_new(&vm);
227 uc_vm_registry_set(&vm, "hostap.registry", registry);
228 ucv_array_set(registry, 0, ucv_get(ops));
229
230 return 0;
231 }
232
233 int wpa_ucode_call_prepare(const char *fname)
234 {
235 uc_value_t *obj, *func;
236
237 if (!registry)
238 return -1;
239
240 obj = ucv_array_get(registry, 0);
241 if (!obj)
242 return -1;
243
244 func = ucv_object_get(obj, fname, NULL);
245 if (!ucv_is_callable(func))
246 return -1;
247
248 uc_vm_stack_push(&vm, ucv_get(obj));
249 uc_vm_stack_push(&vm, ucv_get(func));
250
251 return 0;
252 }
253
254 uc_value_t *wpa_ucode_global_init(const char *name, uc_resource_type_t *global_type)
255 {
256 uc_value_t *global = uc_resource_new(global_type, NULL);
257 uc_value_t *proto;
258
259 uc_vm_registry_set(&vm, "hostap.global", global);
260 proto = ucv_prototype_get(global);
261 ucv_object_add(proto, "data", ucv_get(ucv_object_new(&vm)));
262
263 #define ADD_CONST(x) ucv_object_add(proto, #x, ucv_int64_new(x))
264 ADD_CONST(MSG_EXCESSIVE);
265 ADD_CONST(MSG_MSGDUMP);
266 ADD_CONST(MSG_DEBUG);
267 ADD_CONST(MSG_INFO);
268 ADD_CONST(MSG_WARNING);
269 ADD_CONST(MSG_ERROR);
270 #undef ADD_CONST
271
272 ucv_object_add(uc_vm_scope_get(&vm), name, ucv_get(global));
273
274 return global;
275 }
276
277 int wpa_ucode_registry_add(uc_value_t *reg, uc_value_t *val)
278 {
279 uc_value_t *data;
280 int i = 0;
281
282 while (ucv_array_get(reg, i))
283 i++;
284
285 ucv_array_set(reg, i, ucv_get(val));
286
287 return i + 1;
288 }
289
290 uc_value_t *wpa_ucode_registry_get(uc_value_t *reg, int idx)
291 {
292 if (!idx)
293 return NULL;
294
295 return ucv_array_get(reg, idx - 1);
296 }
297
298 uc_value_t *wpa_ucode_registry_remove(uc_value_t *reg, int idx)
299 {
300 uc_value_t *val = wpa_ucode_registry_get(reg, idx);
301
302 if (val)
303 ucv_array_set(reg, idx - 1, NULL);
304
305 return val;
306 }
307
308
309 uc_value_t *wpa_ucode_call(size_t nargs)
310 {
311 if (uc_vm_call(&vm, true, nargs) != EXCEPTION_NONE)
312 return NULL;
313
314 if (!gc_timer.pending)
315 uloop_timeout_set(&gc_timer, 10);
316
317 return uc_vm_stack_pop(&vm);
318 }
319
320 void wpa_ucode_free_vm(void)
321 {
322 if (!vm.config)
323 return;
324
325 uc_search_path_free(&vm.config->module_search_path);
326 uc_vm_free(&vm);
327 registry = NULL;
328 vm = (uc_vm_t){};
329 }