blob: 16a68c5073171e3e13487b13970c84718b3beafd [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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
19static struct ubus_context *ctx;
20static struct blob_buf b;
21static int ctx_ref;
22
23static 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
28static 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
33static 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
39static 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
49static 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
55static 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
69static void wpas_ubus_ref_inc(void)
70{
71 ctx_ref++;
72}
73
74static 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
88static int
89wpas_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
103static int
104wpas_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
117enum {
118 WPS_START_MULTI_AP,
119 __WPS_START_MAX
120};
121
122static const struct blobmsg_policy wps_start_policy[] = {
123 [WPS_START_MULTI_AP] = { "multi_ap", BLOBMSG_TYPE_BOOL },
124};
125
126static int
127wpas_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
149static int
150wpas_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
166static 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
175static struct ubus_object_type bss_object_type =
176 UBUS_OBJECT_TYPE("wpas_bss", bss_methods);
177
178void 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
198void 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
214enum {
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
224static 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
233static int
234wpas_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 blob_buf_init(&b, 0);
268 blobmsg_add_u32(&b, "pid", getpid());
269 ubus_send_reply(ctx, req, b.head);
270
271 return UBUS_STATUS_OK;
272}
273
274enum {
275 WPAS_CONFIG_REM_IFACE,
276 __WPAS_CONFIG_REM_MAX
277};
278
279static const struct blobmsg_policy wpas_config_remove_policy[__WPAS_CONFIG_REM_MAX] = {
280 [WPAS_CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
281};
282
283static int
284wpas_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
285 struct ubus_request_data *req, const char *method,
286 struct blob_attr *msg)
287{
288 struct blob_attr *tb[__WPAS_CONFIG_REM_MAX];
289 struct wpa_global *global = get_wpa_global_from_object(obj);
290 struct wpa_supplicant *wpa_s = NULL;
291 unsigned int found = 0;
292
293 blobmsg_parse(wpas_config_remove_policy, __WPAS_CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
294
295 if (!tb[WPAS_CONFIG_REM_IFACE])
296 return UBUS_STATUS_INVALID_ARGUMENT;
297
298 /* find wpa_s object for to-be-removed interface */
299 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
300 if (!strncmp(wpa_s->ifname,
301 blobmsg_get_string(tb[WPAS_CONFIG_REM_IFACE]),
302 sizeof(wpa_s->ifname)))
303 {
304 found = 1;
305 break;
306 }
307 }
308
309 if (!found)
310 return UBUS_STATUS_INVALID_ARGUMENT;
311
312 if (wpa_supplicant_remove_iface(global, wpa_s, 0))
313 return UBUS_STATUS_INVALID_ARGUMENT;
314
315 return UBUS_STATUS_OK;
316}
317
318static const struct ubus_method wpas_daemon_methods[] = {
319 UBUS_METHOD("config_add", wpas_config_add, wpas_config_add_policy),
320 UBUS_METHOD("config_remove", wpas_config_remove, wpas_config_remove_policy),
321};
322
323static struct ubus_object_type wpas_daemon_object_type =
324 UBUS_OBJECT_TYPE("wpa_supplicant", wpas_daemon_methods);
325
326void wpas_ubus_add(struct wpa_global *global)
327{
328 struct ubus_object *obj = &global->ubus_global;
329 int ret;
330
331 if (!wpas_ubus_init())
332 return;
333
334 obj->name = strdup("wpa_supplicant");
335
336 obj->type = &wpas_daemon_object_type;
337 obj->methods = wpas_daemon_object_type.methods;
338 obj->n_methods = wpas_daemon_object_type.n_methods;
339 ret = ubus_add_object(ctx, obj);
340 wpas_ubus_ref_inc();
341}
342
343void wpas_ubus_free(struct wpa_global *global)
344{
345 struct ubus_object *obj = &global->ubus_global;
346 char *name = (char *) obj->name;
347
348 if (!ctx)
349 return;
350
351 if (obj->id) {
352 ubus_remove_object(ctx, obj);
353 wpas_ubus_ref_dec();
354 }
355
356 free(name);
357}
358
359
360#ifdef CONFIG_WPS
361void wpas_ubus_notify(struct wpa_supplicant *wpa_s, const struct wps_credential *cred)
362{
363 u16 auth_type;
364 char *ifname, *encryption, *ssid, *key;
365 size_t ifname_len;
366
367 if (!cred)
368 return;
369
370 auth_type = cred->auth_type;
371
372 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK))
373 auth_type = WPS_AUTH_WPA2PSK;
374
375 if (auth_type != WPS_AUTH_OPEN &&
376 auth_type != WPS_AUTH_WPAPSK &&
377 auth_type != WPS_AUTH_WPA2PSK) {
378 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
379 "unsupported authentication type 0x%x",
380 auth_type);
381 return;
382 }
383
384 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
385 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
386 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
387 "invalid Network Key length %lu",
388 (unsigned long) cred->key_len);
389 return;
390 }
391 }
392
393 blob_buf_init(&b, 0);
394
395 ifname_len = strlen(wpa_s->ifname);
396 ifname = blobmsg_alloc_string_buffer(&b, "ifname", ifname_len + 1);
397 memcpy(ifname, wpa_s->ifname, ifname_len + 1);
398 ifname[ifname_len] = '\0';
399 blobmsg_add_string_buffer(&b);
400
401 switch (auth_type) {
402 case WPS_AUTH_WPA2PSK:
403 encryption = "psk2";
404 break;
405 case WPS_AUTH_WPAPSK:
406 encryption = "psk";
407 break;
408 default:
409 encryption = "none";
410 break;
411 }
412
413 blobmsg_add_string(&b, "encryption", encryption);
414
415 ssid = blobmsg_alloc_string_buffer(&b, "ssid", cred->ssid_len + 1);
416 memcpy(ssid, cred->ssid, cred->ssid_len);
417 ssid[cred->ssid_len] = '\0';
418 blobmsg_add_string_buffer(&b);
419
420 if (cred->key_len > 0) {
421 key = blobmsg_alloc_string_buffer(&b, "key", cred->key_len + 1);
422 memcpy(key, cred->key, cred->key_len);
423 key[cred->key_len] = '\0';
424 blobmsg_add_string_buffer(&b);
425 }
426
427// ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
428 ubus_send_event(ctx, "wps_credentials", b.head);
429}
430#endif /* CONFIG_WPS */