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
;
184 if (!wpas_ubus_init())
187 if (asprintf(&name
, "wpa_supplicant.%s", wpa_s
->ifname
) < 0)
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
);
198 void wpas_ubus_free_bss(struct wpa_supplicant
*wpa_s
)
200 struct ubus_object
*obj
= &wpa_s
->ubus
.obj
;
201 char *name
= (char *) obj
->name
;
207 ubus_remove_object(ctx
, obj
);
218 WPAS_CONFIG_HOSTAPD_CTRL
,
224 static 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
},
234 wpas_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
235 struct ubus_request_data
*req
, const char *method
,
236 struct blob_attr
*msg
)
238 struct blob_attr
*tb
[__WPAS_CONFIG_MAX
];
239 struct wpa_global
*global
= get_wpa_global_from_object(obj
);
240 struct wpa_interface
*iface
;
242 blobmsg_parse(wpas_config_add_policy
, __WPAS_CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
244 if (!tb
[WPAS_CONFIG_FILE
] || !tb
[WPAS_CONFIG_IFACE
] || !tb
[WPAS_CONFIG_DRIVER
])
245 return UBUS_STATUS_INVALID_ARGUMENT
;
247 iface
= os_zalloc(sizeof(struct wpa_interface
));
249 return UBUS_STATUS_UNKNOWN_ERROR
;
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
]);
255 if (tb
[WPAS_CONFIG_BRIDGE
])
256 iface
->bridge_ifname
= blobmsg_get_string(tb
[WPAS_CONFIG_BRIDGE
]);
258 if (tb
[WPAS_CONFIG_CTRL
])
259 iface
->ctrl_interface
= blobmsg_get_string(tb
[WPAS_CONFIG_CTRL
]);
261 if (tb
[WPAS_CONFIG_HOSTAPD_CTRL
])
262 iface
->hostapd_ctrl
= blobmsg_get_string(tb
[WPAS_CONFIG_HOSTAPD_CTRL
]);
264 if (!wpa_supplicant_add_iface(global
, iface
, NULL
))
265 return UBUS_STATUS_INVALID_ARGUMENT
;
267 return UBUS_STATUS_OK
;
271 WPAS_CONFIG_REM_IFACE
,
272 __WPAS_CONFIG_REM_MAX
275 static const struct blobmsg_policy wpas_config_remove_policy
[__WPAS_CONFIG_REM_MAX
] = {
276 [WPAS_CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
280 wpas_config_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
281 struct ubus_request_data
*req
, const char *method
,
282 struct blob_attr
*msg
)
284 struct blob_attr
*tb
[__WPAS_CONFIG_REM_MAX
];
285 struct wpa_global
*global
= get_wpa_global_from_object(obj
);
286 struct wpa_supplicant
*wpa_s
= NULL
;
287 unsigned int found
= 0;
289 blobmsg_parse(wpas_config_remove_policy
, __WPAS_CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
291 if (!tb
[WPAS_CONFIG_REM_IFACE
])
292 return UBUS_STATUS_INVALID_ARGUMENT
;
294 /* find wpa_s object for to-be-removed interface */
295 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
296 if (!strncmp(wpa_s
->ifname
,
297 blobmsg_get_string(tb
[WPAS_CONFIG_REM_IFACE
]),
298 sizeof(wpa_s
->ifname
)))
306 return UBUS_STATUS_INVALID_ARGUMENT
;
308 if (wpa_supplicant_remove_iface(global
, wpa_s
, 0))
309 return UBUS_STATUS_INVALID_ARGUMENT
;
311 return UBUS_STATUS_OK
;
314 static const struct ubus_method wpas_daemon_methods
[] = {
315 UBUS_METHOD("config_add", wpas_config_add
, wpas_config_add_policy
),
316 UBUS_METHOD("config_remove", wpas_config_remove
, wpas_config_remove_policy
),
319 static struct ubus_object_type wpas_daemon_object_type
=
320 UBUS_OBJECT_TYPE("wpa_supplicant", wpas_daemon_methods
);
322 void wpas_ubus_add(struct wpa_global
*global
)
324 struct ubus_object
*obj
= &global
->ubus_global
;
329 if (!wpas_ubus_init())
332 name_len
= strlen("wpa_supplicant") + 1;
333 if (global
->params
.name
)
334 name_len
+= strlen(global
->params
.name
) + 1;
336 name
= malloc(name_len
);
337 strcpy(name
, "wpa_supplicant");
339 if (global
->params
.name
)
342 strcat(name
, global
->params
.name
);
347 obj
->type
= &wpas_daemon_object_type
;
348 obj
->methods
= wpas_daemon_object_type
.methods
;
349 obj
->n_methods
= wpas_daemon_object_type
.n_methods
;
350 ret
= ubus_add_object(ctx
, obj
);
354 void wpas_ubus_free(struct wpa_global
*global
)
356 struct ubus_object
*obj
= &global
->ubus_global
;
357 char *name
= (char *) obj
->name
;
363 ubus_remove_object(ctx
, obj
);
372 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
375 char *ifname
, *encryption
, *ssid
, *key
;
381 auth_type
= cred
->auth_type
;
383 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
384 auth_type
= WPS_AUTH_WPA2PSK
;
386 if (auth_type
!= WPS_AUTH_OPEN
&&
387 auth_type
!= WPS_AUTH_WPAPSK
&&
388 auth_type
!= WPS_AUTH_WPA2PSK
) {
389 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
390 "unsupported authentication type 0x%x",
395 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
396 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
397 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
398 "invalid Network Key length %lu",
399 (unsigned long) cred
->key_len
);
404 blob_buf_init(&b
, 0);
406 ifname_len
= strlen(wpa_s
->ifname
);
407 ifname
= blobmsg_alloc_string_buffer(&b
, "ifname", ifname_len
+ 1);
408 memcpy(ifname
, wpa_s
->ifname
, ifname_len
+ 1);
409 ifname
[ifname_len
] = '\0';
410 blobmsg_add_string_buffer(&b
);
413 case WPS_AUTH_WPA2PSK
:
416 case WPS_AUTH_WPAPSK
:
424 blobmsg_add_string(&b
, "encryption", encryption
);
426 ssid
= blobmsg_alloc_string_buffer(&b
, "ssid", cred
->ssid_len
+ 1);
427 memcpy(ssid
, cred
->ssid
, cred
->ssid_len
);
428 ssid
[cred
->ssid_len
] = '\0';
429 blobmsg_add_string_buffer(&b
);
431 if (cred
->key_len
> 0) {
432 key
= blobmsg_alloc_string_buffer(&b
, "key", cred
->key_len
+ 1);
433 memcpy(key
, cred
->key
, cred
->key_len
);
434 key
[cred
->key_len
] = '\0';
435 blobmsg_add_string_buffer(&b
);
438 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
439 ubus_send_event(ctx
, "wps_credentials", b
.head
);
441 #endif /* CONFIG_WPS */