2 * wpa_supplicant / ubus support
3 * Copyright (c) 2018, Daniel Golle <daniel@makrotopia.org>
4 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
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"
19 static struct ubus_context
*ctx
;
20 static struct blob_buf b
;
23 static inline struct wpa_global
*get_wpa_global_from_object(struct ubus_object
*obj
)
25 return container_of(obj
, struct wpa_global
, ubus_global
);
28 static inline struct wpa_supplicant
*get_wpas_from_object(struct ubus_object
*obj
)
30 return container_of(obj
, struct wpa_supplicant
, ubus
.obj
);
33 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
35 struct ubus_context
*ctx
= eloop_ctx
;
36 ubus_handle_event(ctx
);
39 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
41 if (ubus_reconnect(ctx
, NULL
)) {
42 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
46 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
49 static void wpas_ubus_connection_lost(struct ubus_context
*ctx
)
51 eloop_unregister_read_sock(ctx
->sock
.fd
);
52 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
55 static bool wpas_ubus_init(void)
60 ctx
= ubus_connect(NULL
);
64 ctx
->connection_lost
= wpas_ubus_connection_lost
;
65 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
69 static void wpas_ubus_ref_inc(void)
74 static void wpas_ubus_ref_dec(void)
83 eloop_unregister_read_sock(ctx
->sock
.fd
);
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
)
93 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
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
);
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
)
108 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
110 if (wpa_supplicant_reload_configuration(wpa_s
))
111 return UBUS_STATUS_UNKNOWN_ERROR
;
122 static const struct blobmsg_policy wps_start_policy
[] = {
123 [WPS_START_MULTI_AP
] = { "multi_ap", BLOBMSG_TYPE_BOOL
},
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
)
132 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
133 struct blob_attr
*tb
[__WPS_START_MAX
], *cur
;
136 blobmsg_parse(wps_start_policy
, __WPS_START_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
138 if (tb
[WPS_START_MULTI_AP
])
139 multi_ap
= blobmsg_get_bool(tb
[WPS_START_MULTI_AP
]);
141 rc
= wpas_wps_start_pbc(wpa_s
, NULL
, 0, multi_ap
);
144 return UBUS_STATUS_NOT_SUPPORTED
;
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
)
155 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
157 rc
= wpas_wps_cancel(wpa_s
);
160 return UBUS_STATUS_NOT_SUPPORTED
;
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
),
170 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start
),
171 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel
),
175 static struct ubus_object_type bss_object_type
=
176 UBUS_OBJECT_TYPE("wpas_bss", bss_methods
);
178 void wpas_ubus_add_bss(struct wpa_supplicant
*wpa_s
)
180 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
185 if (asprintf(&name
, "wpa_supplicant.%s", wpa_s
->ifname
) < 0)
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
);
196 void wpas_ubus_free_bss(struct wpa_supplicant
*wpa_s
)
198 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
199 char *name
= (char *) obj
->name
;
205 ubus_remove_object(ctx
, obj
);
216 WPAS_CONFIG_HOSTAPD_CTRL
,
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
},
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
)
236 struct blob_attr
*tb
[__WPAS_CONFIG_MAX
];
237 struct wpa_global
*global
= get_wpa_global_from_object(obj
);
238 struct wpa_interface
*iface
;
240 blobmsg_parse(wpas_config_add_policy
, __WPAS_CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
242 if (!tb
[WPAS_CONFIG_FILE
] || !tb
[WPAS_CONFIG_IFACE
] || !tb
[WPAS_CONFIG_DRIVER
])
243 return UBUS_STATUS_INVALID_ARGUMENT
;
245 iface
= os_zalloc(sizeof(struct wpa_interface
));
247 return UBUS_STATUS_UNKNOWN_ERROR
;
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
]);
253 if (tb
[WPAS_CONFIG_BRIDGE
])
254 iface
->bridge_ifname
= blobmsg_get_string(tb
[WPAS_CONFIG_BRIDGE
]);
256 if (tb
[WPAS_CONFIG_CTRL
])
257 iface
->ctrl_interface
= blobmsg_get_string(tb
[WPAS_CONFIG_CTRL
]);
259 if (tb
[WPAS_CONFIG_HOSTAPD_CTRL
])
260 iface
->hostapd_ctrl
= blobmsg_get_string(tb
[WPAS_CONFIG_HOSTAPD_CTRL
]);
262 if (!wpa_supplicant_add_iface(global
, iface
, NULL
))
263 return UBUS_STATUS_INVALID_ARGUMENT
;
265 return UBUS_STATUS_OK
;
269 WPAS_CONFIG_REM_IFACE
,
270 __WPAS_CONFIG_REM_MAX
273 static const struct blobmsg_policy wpas_config_remove_policy
[__WPAS_CONFIG_REM_MAX
] = {
274 [WPAS_CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
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
)
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;
287 blobmsg_parse(wpas_config_remove_policy
, __WPAS_CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
289 if (!tb
[WPAS_CONFIG_REM_IFACE
])
290 return UBUS_STATUS_INVALID_ARGUMENT
;
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
)))
304 return UBUS_STATUS_INVALID_ARGUMENT
;
306 if (wpa_supplicant_remove_iface(global
, wpa_s
, 0))
307 return UBUS_STATUS_INVALID_ARGUMENT
;
309 return UBUS_STATUS_OK
;
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
),
317 static struct ubus_object_type wpas_daemon_object_type
=
318 UBUS_OBJECT_TYPE("wpa_supplicant", wpas_daemon_methods
);
320 void wpas_ubus_add(struct wpa_global
*global
)
322 struct ubus_object
*obj
= &global
->ubus_global
;
326 if (!wpas_ubus_init())
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");
335 if (global
->params
.name
)
337 strcat(obj
->name
, ".");
338 strcat(obj
->name
, global
->params
.name
);
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
);
348 void wpas_ubus_free(struct wpa_global
*global
)
350 struct ubus_object
*obj
= &global
->ubus_global
;
351 char *name
= (char *) obj
->name
;
357 ubus_remove_object(ctx
, obj
);
366 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
369 char *ifname
, *encryption
, *ssid
, *key
;
375 auth_type
= cred
->auth_type
;
377 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
378 auth_type
= WPS_AUTH_WPA2PSK
;
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",
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
);
398 blob_buf_init(&b
, 0);
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
);
407 case WPS_AUTH_WPA2PSK
:
410 case WPS_AUTH_WPAPSK
:
418 blobmsg_add_string(&b
, "encryption", encryption
);
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
);
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
);
432 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
433 ubus_send_event(ctx
, "wps_credentials", b
.head
);
435 #endif /* CONFIG_WPS */