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
);
104 static const struct blobmsg_policy wps_start_policy
[] = {
105 [WPS_START_MULTI_AP
] = { "multi_ap", BLOBMSG_TYPE_BOOL
},
109 wpas_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
110 struct ubus_request_data
*req
, const char *method
,
111 struct blob_attr
*msg
)
114 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
115 struct blob_attr
*tb
[__WPS_START_MAX
], *cur
;
118 blobmsg_parse(wps_start_policy
, __WPS_START_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
120 if (tb
[WPS_START_MULTI_AP
])
121 multi_ap
= blobmsg_get_bool(tb
[WPS_START_MULTI_AP
]);
123 rc
= wpas_wps_start_pbc(wpa_s
, NULL
, 0, multi_ap
);
126 return UBUS_STATUS_NOT_SUPPORTED
;
132 wpas_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
133 struct ubus_request_data
*req
, const char *method
,
134 struct blob_attr
*msg
)
137 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
139 rc
= wpas_wps_cancel(wpa_s
);
142 return UBUS_STATUS_NOT_SUPPORTED
;
148 static const struct ubus_method bss_methods
[] = {
150 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start
),
151 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel
),
153 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features
),
156 static struct ubus_object_type bss_object_type
=
157 UBUS_OBJECT_TYPE("wpas_bss", bss_methods
);
159 void wpas_ubus_add_bss(struct wpa_supplicant
*wpa_s
)
161 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
165 if (!wpas_ubus_init())
168 if (asprintf(&name
, "wpa_supplicant.%s", wpa_s
->ifname
) < 0)
172 obj
->type
= &bss_object_type
;
173 obj
->methods
= bss_object_type
.methods
;
174 obj
->n_methods
= bss_object_type
.n_methods
;
175 ret
= ubus_add_object(ctx
, obj
);
179 void wpas_ubus_free_bss(struct wpa_supplicant
*wpa_s
)
181 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
182 char *name
= (char *) obj
->name
;
188 ubus_remove_object(ctx
, obj
);
196 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
199 char *ifname
, *encryption
, *ssid
, *key
;
205 auth_type
= cred
->auth_type
;
207 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
208 auth_type
= WPS_AUTH_WPA2PSK
;
210 if (auth_type
!= WPS_AUTH_OPEN
&&
211 auth_type
!= WPS_AUTH_WPAPSK
&&
212 auth_type
!= WPS_AUTH_WPA2PSK
) {
213 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
214 "unsupported authentication type 0x%x",
219 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
220 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
221 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
222 "invalid Network Key length %lu",
223 (unsigned long) cred
->key_len
);
228 blob_buf_init(&b
, 0);
230 ifname_len
= strlen(wpa_s
->ifname
);
231 ifname
= blobmsg_alloc_string_buffer(&b
, "ifname", ifname_len
+ 1);
232 memcpy(ifname
, wpa_s
->ifname
, ifname_len
+ 1);
233 ifname
[ifname_len
] = '\0';
234 blobmsg_add_string_buffer(&b
);
237 case WPS_AUTH_WPA2PSK
:
240 case WPS_AUTH_WPAPSK
:
248 blobmsg_add_string(&b
, "encryption", encryption
);
250 ssid
= blobmsg_alloc_string_buffer(&b
, "ssid", cred
->ssid_len
+ 1);
251 memcpy(ssid
, cred
->ssid
, cred
->ssid_len
);
252 ssid
[cred
->ssid_len
] = '\0';
253 blobmsg_add_string_buffer(&b
);
255 if (cred
->key_len
> 0) {
256 key
= blobmsg_alloc_string_buffer(&b
, "key", cred
->key_len
+ 1);
257 memcpy(key
, cred
->key
, cred
->key_len
);
258 key
[cred
->key_len
] = '\0';
259 blobmsg_add_string_buffer(&b
);
262 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
263 ubus_send_event(ctx
, "wps_credentials", b
.head
);
265 #endif /* CONFIG_WPS */