hostapd: add ubus reload
[openwrt/staging/stintel.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
185 if (asprintf(&name, "wpa_supplicant.%s", wpa_s->ifname) < 0)
186 return;
187
188 obj->name = name;
189 obj->type = &bss_object_type;
190 obj->methods = bss_object_type.methods;
191 obj->n_methods = bss_object_type.n_methods;
192 ret = ubus_add_object(ctx, obj);
193 wpas_ubus_ref_inc();
194 }
195
196 void wpas_ubus_free_bss(struct wpa_supplicant *wpa_s)
197 {
198 struct ubus_object *obj = &wpa_s->ubus.obj;
199 char *name = (char *) obj->name;
200
201 if (!ctx)
202 return;
203
204 if (obj->id) {
205 ubus_remove_object(ctx, obj);
206 wpas_ubus_ref_dec();
207 }
208
209 free(name);
210 }
211
212 enum {
213 WPAS_CONFIG_DRIVER,
214 WPAS_CONFIG_IFACE,
215 WPAS_CONFIG_BRIDGE,
216 WPAS_CONFIG_HOSTAPD_CTRL,
217 WPAS_CONFIG_CTRL,
218 WPAS_CONFIG_FILE,
219 __WPAS_CONFIG_MAX
220 };
221
222 static const struct blobmsg_policy wpas_config_add_policy[__WPAS_CONFIG_MAX] = {
223 [WPAS_CONFIG_DRIVER] = { "driver", BLOBMSG_TYPE_STRING },
224 [WPAS_CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
225 [WPAS_CONFIG_BRIDGE] = { "bridge", BLOBMSG_TYPE_STRING },
226 [WPAS_CONFIG_HOSTAPD_CTRL] = { "hostapd_ctrl", BLOBMSG_TYPE_STRING },
227 [WPAS_CONFIG_CTRL] = { "ctrl", BLOBMSG_TYPE_STRING },
228 [WPAS_CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
229 };
230
231 static int
232 wpas_config_add(struct ubus_context *ctx, struct ubus_object *obj,
233 struct ubus_request_data *req, const char *method,
234 struct blob_attr *msg)
235 {
236 struct blob_attr *tb[__WPAS_CONFIG_MAX];
237 struct wpa_global *global = get_wpa_global_from_object(obj);
238 struct wpa_interface *iface;
239
240 blobmsg_parse(wpas_config_add_policy, __WPAS_CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
241
242 if (!tb[WPAS_CONFIG_FILE] || !tb[WPAS_CONFIG_IFACE] || !tb[WPAS_CONFIG_DRIVER])
243 return UBUS_STATUS_INVALID_ARGUMENT;
244
245 iface = os_zalloc(sizeof(struct wpa_interface));
246 if (iface == NULL)
247 return UBUS_STATUS_UNKNOWN_ERROR;
248
249 iface->driver = blobmsg_get_string(tb[WPAS_CONFIG_DRIVER]);
250 iface->ifname = blobmsg_get_string(tb[WPAS_CONFIG_IFACE]);
251 iface->confname = blobmsg_get_string(tb[WPAS_CONFIG_FILE]);
252
253 if (tb[WPAS_CONFIG_BRIDGE])
254 iface->bridge_ifname = blobmsg_get_string(tb[WPAS_CONFIG_BRIDGE]);
255
256 if (tb[WPAS_CONFIG_CTRL])
257 iface->ctrl_interface = blobmsg_get_string(tb[WPAS_CONFIG_CTRL]);
258
259 if (tb[WPAS_CONFIG_HOSTAPD_CTRL])
260 iface->hostapd_ctrl = blobmsg_get_string(tb[WPAS_CONFIG_HOSTAPD_CTRL]);
261
262 if (!wpa_supplicant_add_iface(global, iface, NULL))
263 return UBUS_STATUS_INVALID_ARGUMENT;
264
265 return UBUS_STATUS_OK;
266 }
267
268 enum {
269 WPAS_CONFIG_REM_IFACE,
270 __WPAS_CONFIG_REM_MAX
271 };
272
273 static const struct blobmsg_policy wpas_config_remove_policy[__WPAS_CONFIG_REM_MAX] = {
274 [WPAS_CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
275 };
276
277 static int
278 wpas_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
279 struct ubus_request_data *req, const char *method,
280 struct blob_attr *msg)
281 {
282 struct blob_attr *tb[__WPAS_CONFIG_REM_MAX];
283 struct wpa_global *global = get_wpa_global_from_object(obj);
284 struct wpa_supplicant *wpa_s = NULL;
285 unsigned int found = 0;
286
287 blobmsg_parse(wpas_config_remove_policy, __WPAS_CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
288
289 if (!tb[WPAS_CONFIG_REM_IFACE])
290 return UBUS_STATUS_INVALID_ARGUMENT;
291
292 /* find wpa_s object for to-be-removed interface */
293 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
294 if (!strncmp(wpa_s->ifname,
295 blobmsg_get_string(tb[WPAS_CONFIG_REM_IFACE]),
296 sizeof(wpa_s->ifname)))
297 {
298 found = 1;
299 break;
300 }
301 }
302
303 if (!found)
304 return UBUS_STATUS_INVALID_ARGUMENT;
305
306 if (wpa_supplicant_remove_iface(global, wpa_s, 0))
307 return UBUS_STATUS_INVALID_ARGUMENT;
308
309 return UBUS_STATUS_OK;
310 }
311
312 static const struct ubus_method wpas_daemon_methods[] = {
313 UBUS_METHOD("config_add", wpas_config_add, wpas_config_add_policy),
314 UBUS_METHOD("config_remove", wpas_config_remove, wpas_config_remove_policy),
315 };
316
317 static struct ubus_object_type wpas_daemon_object_type =
318 UBUS_OBJECT_TYPE("wpa_supplicant", wpas_daemon_methods);
319
320 void wpas_ubus_add(struct wpa_global *global)
321 {
322 struct ubus_object *obj = &global->ubus_global;
323 int name_len;
324 int ret;
325
326 if (!wpas_ubus_init())
327 return;
328
329 name_len = strlen("wpa_supplicant") + 1;
330 if (global->params.name)
331 name_len += strlen(global->params.name) + 1;
332 obj->name = malloc(name_len);
333 strcpy(obj->name, "wpa_supplicant");
334
335 if (global->params.name)
336 {
337 strcat(obj->name, ".");
338 strcat(obj->name, global->params.name);
339 }
340
341 obj->type = &wpas_daemon_object_type;
342 obj->methods = wpas_daemon_object_type.methods;
343 obj->n_methods = wpas_daemon_object_type.n_methods;
344 ret = ubus_add_object(ctx, obj);
345 wpas_ubus_ref_inc();
346 }
347
348 void wpas_ubus_free(struct wpa_global *global)
349 {
350 struct ubus_object *obj = &global->ubus_global;
351 char *name = (char *) obj->name;
352
353 if (!ctx)
354 return;
355
356 if (obj->id) {
357 ubus_remove_object(ctx, obj);
358 wpas_ubus_ref_dec();
359 }
360
361 free(name);
362 }
363
364
365 #ifdef CONFIG_WPS
366 void wpas_ubus_notify(struct wpa_supplicant *wpa_s, const struct wps_credential *cred)
367 {
368 u16 auth_type;
369 char *ifname, *encryption, *ssid, *key;
370 size_t ifname_len;
371
372 if (!cred)
373 return;
374
375 auth_type = cred->auth_type;
376
377 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK))
378 auth_type = WPS_AUTH_WPA2PSK;
379
380 if (auth_type != WPS_AUTH_OPEN &&
381 auth_type != WPS_AUTH_WPAPSK &&
382 auth_type != WPS_AUTH_WPA2PSK) {
383 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
384 "unsupported authentication type 0x%x",
385 auth_type);
386 return;
387 }
388
389 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
390 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
391 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
392 "invalid Network Key length %lu",
393 (unsigned long) cred->key_len);
394 return;
395 }
396 }
397
398 blob_buf_init(&b, 0);
399
400 ifname_len = strlen(wpa_s->ifname);
401 ifname = blobmsg_alloc_string_buffer(&b, "ifname", ifname_len + 1);
402 memcpy(ifname, wpa_s->ifname, ifname_len + 1);
403 ifname[ifname_len] = '\0';
404 blobmsg_add_string_buffer(&b);
405
406 switch (auth_type) {
407 case WPS_AUTH_WPA2PSK:
408 encryption = "psk2";
409 break;
410 case WPS_AUTH_WPAPSK:
411 encryption = "psk";
412 break;
413 default:
414 encryption = "none";
415 break;
416 }
417
418 blobmsg_add_string(&b, "encryption", encryption);
419
420 ssid = blobmsg_alloc_string_buffer(&b, "ssid", cred->ssid_len + 1);
421 memcpy(ssid, cred->ssid, cred->ssid_len);
422 ssid[cred->ssid_len] = '\0';
423 blobmsg_add_string_buffer(&b);
424
425 if (cred->key_len > 0) {
426 key = blobmsg_alloc_string_buffer(&b, "key", cred->key_len + 1);
427 memcpy(key, cred->key, cred->key_len);
428 key[cred->key_len] = '\0';
429 blobmsg_add_string_buffer(&b);
430 }
431
432 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
433 ubus_send_event(ctx, "wps_credentials", b.head);
434 }
435 #endif /* CONFIG_WPS */