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_reconnect_timeout(void *eloop_data
, void *user_ctx
)
35 if (ubus_reconnect(ctx
, NULL
)) {
36 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
43 static void wpas_ubus_connection_lost(struct ubus_context
*ctx
)
45 uloop_fd_delete(&ctx
->sock
);
46 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
49 static bool wpas_ubus_init(void)
55 ctx
= ubus_connect(NULL
);
59 ctx
->connection_lost
= wpas_ubus_connection_lost
;
65 static void wpas_ubus_ref_inc(void)
70 static void wpas_ubus_ref_dec(void)
79 uloop_fd_delete(&ctx
->sock
);
85 wpas_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
86 struct ubus_request_data
*req
, const char *method
,
87 struct blob_attr
*msg
)
89 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
92 blobmsg_add_u8(&b
, "ht_supported", ht_supported(wpa_s
->hw
.modes
));
93 blobmsg_add_u8(&b
, "vht_supported", vht_supported(wpa_s
->hw
.modes
));
94 ubus_send_reply(ctx
, req
, b
.head
);
100 wpas_bss_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
101 struct ubus_request_data
*req
, const char *method
,
102 struct blob_attr
*msg
)
104 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
106 if (wpa_supplicant_reload_configuration(wpa_s
))
107 return UBUS_STATUS_UNKNOWN_ERROR
;
118 static const struct blobmsg_policy wps_start_policy
[] = {
119 [WPS_START_MULTI_AP
] = { "multi_ap", BLOBMSG_TYPE_BOOL
},
123 wpas_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
124 struct ubus_request_data
*req
, const char *method
,
125 struct blob_attr
*msg
)
128 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
129 struct blob_attr
*tb
[__WPS_START_MAX
], *cur
;
132 blobmsg_parse(wps_start_policy
, __WPS_START_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
134 if (tb
[WPS_START_MULTI_AP
])
135 multi_ap
= blobmsg_get_bool(tb
[WPS_START_MULTI_AP
]);
137 rc
= wpas_wps_start_pbc(wpa_s
, NULL
, 0, multi_ap
);
140 return UBUS_STATUS_NOT_SUPPORTED
;
146 wpas_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
147 struct ubus_request_data
*req
, const char *method
,
148 struct blob_attr
*msg
)
151 struct wpa_supplicant
*wpa_s
= get_wpas_from_object(obj
);
153 rc
= wpas_wps_cancel(wpa_s
);
156 return UBUS_STATUS_NOT_SUPPORTED
;
162 static const struct ubus_method bss_methods
[] = {
163 UBUS_METHOD_NOARG("reload", wpas_bss_reload
),
164 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features
),
166 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start
),
167 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel
),
171 static struct ubus_object_type bss_object_type
=
172 UBUS_OBJECT_TYPE("wpas_bss", bss_methods
);
174 void wpas_ubus_add_bss(struct wpa_supplicant
*wpa_s
)
176 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
180 if (!wpas_ubus_init())
183 if (asprintf(&name
, "wpa_supplicant.%s", wpa_s
->ifname
) < 0)
187 obj
->type
= &bss_object_type
;
188 obj
->methods
= bss_object_type
.methods
;
189 obj
->n_methods
= bss_object_type
.n_methods
;
190 ret
= ubus_add_object(ctx
, obj
);
194 void wpas_ubus_free_bss(struct wpa_supplicant
*wpa_s
)
196 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
197 char *name
= (char *) obj
->name
;
203 ubus_remove_object(ctx
, obj
);
211 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
214 char *ifname
, *encryption
, *ssid
, *key
;
220 auth_type
= cred
->auth_type
;
222 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
223 auth_type
= WPS_AUTH_WPA2PSK
;
225 if (auth_type
!= WPS_AUTH_OPEN
&&
226 auth_type
!= WPS_AUTH_WPAPSK
&&
227 auth_type
!= WPS_AUTH_WPA2PSK
) {
228 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
229 "unsupported authentication type 0x%x",
234 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
235 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
236 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
237 "invalid Network Key length %lu",
238 (unsigned long) cred
->key_len
);
243 blob_buf_init(&b
, 0);
245 ifname_len
= strlen(wpa_s
->ifname
);
246 ifname
= blobmsg_alloc_string_buffer(&b
, "ifname", ifname_len
+ 1);
247 memcpy(ifname
, wpa_s
->ifname
, ifname_len
+ 1);
248 ifname
[ifname_len
] = '\0';
249 blobmsg_add_string_buffer(&b
);
252 case WPS_AUTH_WPA2PSK
:
255 case WPS_AUTH_WPAPSK
:
263 blobmsg_add_string(&b
, "encryption", encryption
);
265 ssid
= blobmsg_alloc_string_buffer(&b
, "ssid", cred
->ssid_len
+ 1);
266 memcpy(ssid
, cred
->ssid
, cred
->ssid_len
);
267 ssid
[cred
->ssid_len
] = '\0';
268 blobmsg_add_string_buffer(&b
);
270 if (cred
->key_len
> 0) {
271 key
= blobmsg_alloc_string_buffer(&b
, "key", cred
->key_len
+ 1);
272 memcpy(key
, cred
->key
, cred
->key_len
);
273 key
[cred
->key_len
] = '\0';
274 blobmsg_add_string_buffer(&b
);
277 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
278 ubus_send_event(ctx
, "wps_credentials", b
.head
);
280 #endif /* CONFIG_WPS */