hostapd: fix segfault in wpa_supplicant ubus
[openwrt/staging/dedeckeh.git] / package / network / services / hostapd / src / wpa_supplicant / ubus.c
1 /*
2 * wpa_supplicant / ubus support
3 * Copyright (c) 2018, Daniel Golle <daniel@makrotopia.org>
4 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/wpabuf.h"
14 #include "common/ieee802_11_defs.h"
15 #include "wpa_supplicant_i.h"
16 #include "wps_supplicant.h"
17 #include "ubus.h"
18
19 static struct ubus_context *ctx;
20 static struct blob_buf b;
21 static int ctx_ref;
22
23 static inline struct wpa_global *get_wpa_global_from_object(struct ubus_object *obj)
24 {
25 return container_of(obj, struct wpa_global, ubus_global);
26 }
27
28 static inline struct wpa_supplicant *get_wpas_from_object(struct ubus_object *obj)
29 {
30 return container_of(obj, struct wpa_supplicant, ubus.obj);
31 }
32
33 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
34 {
35 struct ubus_context *ctx = eloop_ctx;
36 ubus_handle_event(ctx);
37 }
38
39 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
40 {
41 if (ubus_reconnect(ctx, NULL)) {
42 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
43 return;
44 }
45
46 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
47 }
48
49 static void wpas_ubus_connection_lost(struct ubus_context *ctx)
50 {
51 eloop_unregister_read_sock(ctx->sock.fd);
52 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
53 }
54
55 static bool wpas_ubus_init(void)
56 {
57 if (ctx)
58 return true;
59
60 ctx = ubus_connect(NULL);
61 if (!ctx)
62 return false;
63
64 ctx->connection_lost = wpas_ubus_connection_lost;
65 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
66 return true;
67 }
68
69 static void wpas_ubus_ref_inc(void)
70 {
71 ctx_ref++;
72 }
73
74 static void wpas_ubus_ref_dec(void)
75 {
76 ctx_ref--;
77 if (!ctx)
78 return;
79
80 if (ctx_ref)
81 return;
82
83 eloop_unregister_read_sock(ctx->sock.fd);
84 ubus_free(ctx);
85 ctx = NULL;
86 }
87
88 static int
89 wpas_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
90 struct ubus_request_data *req, const char *method,
91 struct blob_attr *msg)
92 {
93 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
94
95 blob_buf_init(&b, 0);
96 blobmsg_add_u8(&b, "ht_supported", ht_supported(wpa_s->hw.modes));
97 blobmsg_add_u8(&b, "vht_supported", vht_supported(wpa_s->hw.modes));
98 ubus_send_reply(ctx, req, b.head);
99
100 return 0;
101 }
102
103 static int
104 wpas_bss_reload(struct ubus_context *ctx, struct ubus_object *obj,
105 struct ubus_request_data *req, const char *method,
106 struct blob_attr *msg)
107 {
108 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
109
110 if (wpa_supplicant_reload_configuration(wpa_s))
111 return UBUS_STATUS_UNKNOWN_ERROR;
112 else
113 return 0;
114 }
115
116 #ifdef CONFIG_WPS
117 enum {
118 WPS_START_MULTI_AP,
119 __WPS_START_MAX
120 };
121
122 static const struct blobmsg_policy wps_start_policy[] = {
123 [WPS_START_MULTI_AP] = { "multi_ap", BLOBMSG_TYPE_BOOL },
124 };
125
126 static int
127 wpas_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
128 struct ubus_request_data *req, const char *method,
129 struct blob_attr *msg)
130 {
131 int rc;
132 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
133 struct blob_attr *tb[__WPS_START_MAX], *cur;
134 int multi_ap = 0;
135
136 blobmsg_parse(wps_start_policy, __WPS_START_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
137
138 if (tb[WPS_START_MULTI_AP])
139 multi_ap = blobmsg_get_bool(tb[WPS_START_MULTI_AP]);
140
141 rc = wpas_wps_start_pbc(wpa_s, NULL, 0, multi_ap);
142
143 if (rc != 0)
144 return UBUS_STATUS_NOT_SUPPORTED;
145
146 return 0;
147 }
148
149 static int
150 wpas_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
151 struct ubus_request_data *req, const char *method,
152 struct blob_attr *msg)
153 {
154 int rc;
155 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
156
157 rc = wpas_wps_cancel(wpa_s);
158
159 if (rc != 0)
160 return UBUS_STATUS_NOT_SUPPORTED;
161
162 return 0;
163 }
164 #endif
165
166 static const struct ubus_method bss_methods[] = {
167 UBUS_METHOD_NOARG("reload", wpas_bss_reload),
168 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features),
169 #ifdef CONFIG_WPS
170 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start),
171 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel),
172 #endif
173 };
174
175 static struct ubus_object_type bss_object_type =
176 UBUS_OBJECT_TYPE("wpas_bss", bss_methods);
177
178 void wpas_ubus_add_bss(struct wpa_supplicant *wpa_s)
179 {
180 struct ubus_object *obj = &wpa_s->ubus.obj;
181 char *name;
182 int ret;
183
184 if (!wpas_ubus_init())
185 return;
186
187 if (asprintf(&name, "wpa_supplicant.%s", wpa_s->ifname) < 0)
188 return;
189
190 obj->name = name;
191 obj->type = &bss_object_type;
192 obj->methods = bss_object_type.methods;
193 obj->n_methods = bss_object_type.n_methods;
194 ret = ubus_add_object(ctx, obj);
195 wpas_ubus_ref_inc();
196 }
197
198 void wpas_ubus_free_bss(struct wpa_supplicant *wpa_s)
199 {
200 struct ubus_object *obj = &wpa_s->ubus.obj;
201 char *name = (char *) obj->name;
202
203 if (!ctx)
204 return;
205
206 if (obj->id) {
207 ubus_remove_object(ctx, obj);
208 wpas_ubus_ref_dec();
209 }
210
211 free(name);
212 }
213
214 enum {
215 WPAS_CONFIG_DRIVER,
216 WPAS_CONFIG_IFACE,
217 WPAS_CONFIG_BRIDGE,
218 WPAS_CONFIG_HOSTAPD_CTRL,
219 WPAS_CONFIG_CTRL,
220 WPAS_CONFIG_FILE,
221 __WPAS_CONFIG_MAX
222 };
223
224 static const struct blobmsg_policy wpas_config_add_policy[__WPAS_CONFIG_MAX] = {
225 [WPAS_CONFIG_DRIVER] = { "driver", BLOBMSG_TYPE_STRING },
226 [WPAS_CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
227 [WPAS_CONFIG_BRIDGE] = { "bridge", BLOBMSG_TYPE_STRING },
228 [WPAS_CONFIG_HOSTAPD_CTRL] = { "hostapd_ctrl", BLOBMSG_TYPE_STRING },
229 [WPAS_CONFIG_CTRL] = { "ctrl", BLOBMSG_TYPE_STRING },
230 [WPAS_CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
231 };
232
233 static int
234 wpas_config_add(struct ubus_context *ctx, struct ubus_object *obj,
235 struct ubus_request_data *req, const char *method,
236 struct blob_attr *msg)
237 {
238 struct blob_attr *tb[__WPAS_CONFIG_MAX];
239 struct wpa_global *global = get_wpa_global_from_object(obj);
240 struct wpa_interface *iface;
241
242 blobmsg_parse(wpas_config_add_policy, __WPAS_CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
243
244 if (!tb[WPAS_CONFIG_FILE] || !tb[WPAS_CONFIG_IFACE] || !tb[WPAS_CONFIG_DRIVER])
245 return UBUS_STATUS_INVALID_ARGUMENT;
246
247 iface = os_zalloc(sizeof(struct wpa_interface));
248 if (iface == NULL)
249 return UBUS_STATUS_UNKNOWN_ERROR;
250
251 iface->driver = blobmsg_get_string(tb[WPAS_CONFIG_DRIVER]);
252 iface->ifname = blobmsg_get_string(tb[WPAS_CONFIG_IFACE]);
253 iface->confname = blobmsg_get_string(tb[WPAS_CONFIG_FILE]);
254
255 if (tb[WPAS_CONFIG_BRIDGE])
256 iface->bridge_ifname = blobmsg_get_string(tb[WPAS_CONFIG_BRIDGE]);
257
258 if (tb[WPAS_CONFIG_CTRL])
259 iface->ctrl_interface = blobmsg_get_string(tb[WPAS_CONFIG_CTRL]);
260
261 if (tb[WPAS_CONFIG_HOSTAPD_CTRL])
262 iface->hostapd_ctrl = blobmsg_get_string(tb[WPAS_CONFIG_HOSTAPD_CTRL]);
263
264 if (!wpa_supplicant_add_iface(global, iface, NULL))
265 return UBUS_STATUS_INVALID_ARGUMENT;
266
267 return UBUS_STATUS_OK;
268 }
269
270 enum {
271 WPAS_CONFIG_REM_IFACE,
272 __WPAS_CONFIG_REM_MAX
273 };
274
275 static const struct blobmsg_policy wpas_config_remove_policy[__WPAS_CONFIG_REM_MAX] = {
276 [WPAS_CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
277 };
278
279 static int
280 wpas_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
281 struct ubus_request_data *req, const char *method,
282 struct blob_attr *msg)
283 {
284 struct blob_attr *tb[__WPAS_CONFIG_REM_MAX];
285 struct wpa_global *global = get_wpa_global_from_object(obj);
286 struct wpa_supplicant *wpa_s = NULL;
287 unsigned int found = 0;
288
289 blobmsg_parse(wpas_config_remove_policy, __WPAS_CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
290
291 if (!tb[WPAS_CONFIG_REM_IFACE])
292 return UBUS_STATUS_INVALID_ARGUMENT;
293
294 /* find wpa_s object for to-be-removed interface */
295 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
296 if (!strncmp(wpa_s->ifname,
297 blobmsg_get_string(tb[WPAS_CONFIG_REM_IFACE]),
298 sizeof(wpa_s->ifname)))
299 {
300 found = 1;
301 break;
302 }
303 }
304
305 if (!found)
306 return UBUS_STATUS_INVALID_ARGUMENT;
307
308 if (wpa_supplicant_remove_iface(global, wpa_s, 0))
309 return UBUS_STATUS_INVALID_ARGUMENT;
310
311 return UBUS_STATUS_OK;
312 }
313
314 static const struct ubus_method wpas_daemon_methods[] = {
315 UBUS_METHOD("config_add", wpas_config_add, wpas_config_add_policy),
316 UBUS_METHOD("config_remove", wpas_config_remove, wpas_config_remove_policy),
317 };
318
319 static struct ubus_object_type wpas_daemon_object_type =
320 UBUS_OBJECT_TYPE("wpa_supplicant", wpas_daemon_methods);
321
322 void wpas_ubus_add(struct wpa_global *global)
323 {
324 struct ubus_object *obj = &global->ubus_global;
325 char *name;
326 int name_len;
327 int ret;
328
329 if (!wpas_ubus_init())
330 return;
331
332 name_len = strlen("wpa_supplicant") + 1;
333 if (global->params.name)
334 name_len += strlen(global->params.name) + 1;
335
336 name = malloc(name_len);
337 strcpy(name, "wpa_supplicant");
338
339 if (global->params.name)
340 {
341 strcat(name, ".");
342 strcat(name, global->params.name);
343 }
344
345 obj->name = name;
346
347 obj->type = &wpas_daemon_object_type;
348 obj->methods = wpas_daemon_object_type.methods;
349 obj->n_methods = wpas_daemon_object_type.n_methods;
350 ret = ubus_add_object(ctx, obj);
351 wpas_ubus_ref_inc();
352 }
353
354 void wpas_ubus_free(struct wpa_global *global)
355 {
356 struct ubus_object *obj = &global->ubus_global;
357 char *name = (char *) obj->name;
358
359 if (!ctx)
360 return;
361
362 if (obj->id) {
363 ubus_remove_object(ctx, obj);
364 wpas_ubus_ref_dec();
365 }
366
367 free(name);
368 }
369
370
371 #ifdef CONFIG_WPS
372 void wpas_ubus_notify(struct wpa_supplicant *wpa_s, const struct wps_credential *cred)
373 {
374 u16 auth_type;
375 char *ifname, *encryption, *ssid, *key;
376 size_t ifname_len;
377
378 if (!cred)
379 return;
380
381 auth_type = cred->auth_type;
382
383 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK))
384 auth_type = WPS_AUTH_WPA2PSK;
385
386 if (auth_type != WPS_AUTH_OPEN &&
387 auth_type != WPS_AUTH_WPAPSK &&
388 auth_type != WPS_AUTH_WPA2PSK) {
389 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
390 "unsupported authentication type 0x%x",
391 auth_type);
392 return;
393 }
394
395 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
396 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
397 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
398 "invalid Network Key length %lu",
399 (unsigned long) cred->key_len);
400 return;
401 }
402 }
403
404 blob_buf_init(&b, 0);
405
406 ifname_len = strlen(wpa_s->ifname);
407 ifname = blobmsg_alloc_string_buffer(&b, "ifname", ifname_len + 1);
408 memcpy(ifname, wpa_s->ifname, ifname_len + 1);
409 ifname[ifname_len] = '\0';
410 blobmsg_add_string_buffer(&b);
411
412 switch (auth_type) {
413 case WPS_AUTH_WPA2PSK:
414 encryption = "psk2";
415 break;
416 case WPS_AUTH_WPAPSK:
417 encryption = "psk";
418 break;
419 default:
420 encryption = "none";
421 break;
422 }
423
424 blobmsg_add_string(&b, "encryption", encryption);
425
426 ssid = blobmsg_alloc_string_buffer(&b, "ssid", cred->ssid_len + 1);
427 memcpy(ssid, cred->ssid, cred->ssid_len);
428 ssid[cred->ssid_len] = '\0';
429 blobmsg_add_string_buffer(&b);
430
431 if (cred->key_len > 0) {
432 key = blobmsg_alloc_string_buffer(&b, "key", cred->key_len + 1);
433 memcpy(key, cred->key, cred->key_len);
434 key[cred->key_len] = '\0';
435 blobmsg_add_string_buffer(&b);
436 }
437
438 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
439 ubus_send_event(ctx, "wps_credentials", b.head);
440 }
441 #endif /* CONFIG_WPS */