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_supplicant
*get_wpas_from_object(struct ubus_object
*obj
)
25 return container_of(obj
, struct wpa_supplicant
, ubus
.obj
);
28 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
30 struct ubus_context
*ctx
= eloop_ctx
;
31 ubus_handle_event(ctx
);
34 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
36 if (ubus_reconnect(ctx
, NULL
)) {
37 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
41 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
44 static void wpas_ubus_connection_lost(struct ubus_context
*ctx
)
46 eloop_unregister_read_sock(ctx
->sock
.fd
);
47 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
50 static bool wpas_ubus_init(void)
55 ctx
= ubus_connect(NULL
);
59 ctx
->connection_lost
= wpas_ubus_connection_lost
;
60 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
64 static void wpas_ubus_ref_inc(void)
69 static void wpas_ubus_ref_dec(void)
78 eloop_unregister_read_sock(ctx
->sock
.fd
);
84 wpas_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
85 struct ubus_request_data
*req
, const char *method
,
86 struct blob_attr
*msg
)
88 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
91 blobmsg_add_u8(&b
, "ht_supported", ht_supported(wpa_s
->hw
.modes
));
92 blobmsg_add_u8(&b
, "vht_supported", vht_supported(wpa_s
->hw
.modes
));
93 ubus_send_reply(ctx
, req
, b
.head
);
100 wpas_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
101 struct ubus_request_data
*req
, const char *method
,
102 struct blob_attr
*msg
)
105 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
107 rc
= wpas_wps_start_pbc(wpa_s
, NULL
, 0);
110 return UBUS_STATUS_NOT_SUPPORTED
;
116 wpas_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
117 struct ubus_request_data
*req
, const char *method
,
118 struct blob_attr
*msg
)
121 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
123 rc
= wpas_wps_cancel(wpa_s
);
126 return UBUS_STATUS_NOT_SUPPORTED
;
132 static const struct ubus_method bss_methods
[] = {
134 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start
),
135 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel
),
137 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features
),
140 static struct ubus_object_type bss_object_type
=
141 UBUS_OBJECT_TYPE("wpas_bss", bss_methods
);
143 void wpas_ubus_add_bss(struct wpa_supplicant
*wpa_s
)
145 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
149 if (!wpas_ubus_init())
152 if (asprintf(&name
, "wpa_supplicant.%s", wpa_s
->ifname
) < 0)
156 obj
->type
= &bss_object_type
;
157 obj
->methods
= bss_object_type
.methods
;
158 obj
->n_methods
= bss_object_type
.n_methods
;
159 ret
= ubus_add_object(ctx
, obj
);
163 void wpas_ubus_free_bss(struct wpa_supplicant
*wpa_s
)
165 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
166 char *name
= (char *) obj
->name
;
172 ubus_remove_object(ctx
, obj
);
180 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
183 char *ifname
, *encryption
, *ssid
, *key
;
189 auth_type
= cred
->auth_type
;
191 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
192 auth_type
= WPS_AUTH_WPA2PSK
;
194 if (auth_type
!= WPS_AUTH_OPEN
&&
195 auth_type
!= WPS_AUTH_WPAPSK
&&
196 auth_type
!= WPS_AUTH_WPA2PSK
) {
197 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
198 "unsupported authentication type 0x%x",
203 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
204 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
205 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
206 "invalid Network Key length %lu",
207 (unsigned long) cred
->key_len
);
212 blob_buf_init(&b
, 0);
214 ifname_len
= strlen(wpa_s
->ifname
);
215 ifname
= blobmsg_alloc_string_buffer(&b
, "ifname", ifname_len
+ 1);
216 memcpy(ifname
, wpa_s
->ifname
, ifname_len
+ 1);
217 ifname
[ifname_len
] = '\0';
218 blobmsg_add_string_buffer(&b
);
221 case WPS_AUTH_WPA2PSK
:
224 case WPS_AUTH_WPAPSK
:
232 blobmsg_add_string(&b
, "encryption", encryption
);
234 ssid
= blobmsg_alloc_string_buffer(&b
, "ssid", cred
->ssid_len
+ 1);
235 memcpy(ssid
, cred
->ssid
, cred
->ssid_len
);
236 ssid
[cred
->ssid_len
] = '\0';
237 blobmsg_add_string_buffer(&b
);
239 if (cred
->key_len
> 0) {
240 key
= blobmsg_alloc_string_buffer(&b
, "key", cred
->key_len
+ 1);
241 memcpy(key
, cred
->key
, cred
->key_len
);
242 key
[cred
->key_len
] = '\0';
243 blobmsg_add_string_buffer(&b
);
246 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
247 ubus_send_event(ctx
, "wps_credentials", b
.head
);
249 #endif /* CONFIG_WPS */