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
);
214 WPAS_CONFIG_HOSTAPD_CTRL
,
220 static const struct blobmsg_policy wpas_config_add_policy
[__WPAS_CONFIG_MAX
] = {
221 [WPAS_CONFIG_DRIVER
] = { "driver", BLOBMSG_TYPE_STRING
},
222 [WPAS_CONFIG_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
223 [WPAS_CONFIG_BRIDGE
] = { "bridge", BLOBMSG_TYPE_STRING
},
224 [WPAS_CONFIG_HOSTAPD_CTRL
] = { "hostapd_ctrl", BLOBMSG_TYPE_STRING
},
225 [WPAS_CONFIG_CTRL
] = { "ctrl", BLOBMSG_TYPE_STRING
},
226 [WPAS_CONFIG_FILE
] = { "config", BLOBMSG_TYPE_STRING
},
230 wpas_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
231 struct ubus_request_data
*req
, const char *method
,
232 struct blob_attr
*msg
)
234 struct blob_attr
*tb
[__WPAS_CONFIG_MAX
];
235 struct wpa_global
*global
= get_wpa_global_from_object(obj
);
236 struct wpa_interface
*iface
;
238 blobmsg_parse(wpas_config_add_policy
, __WPAS_CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
240 if (!tb
[WPAS_CONFIG_FILE
] || !tb
[WPAS_CONFIG_IFACE
] || !tb
[WPAS_CONFIG_DRIVER
])
241 return UBUS_STATUS_INVALID_ARGUMENT
;
243 iface
= os_zalloc(sizeof(struct wpa_interface
));
245 return UBUS_STATUS_UNKNOWN_ERROR
;
247 iface
->driver
= blobmsg_get_string(tb
[WPAS_CONFIG_DRIVER
]);
248 iface
->ifname
= blobmsg_get_string(tb
[WPAS_CONFIG_IFACE
]);
249 iface
->confname
= blobmsg_get_string(tb
[WPAS_CONFIG_FILE
]);
251 if (tb
[WPAS_CONFIG_BRIDGE
])
252 iface
->bridge_ifname
= blobmsg_get_string(tb
[WPAS_CONFIG_BRIDGE
]);
254 if (tb
[WPAS_CONFIG_CTRL
])
255 iface
->ctrl_interface
= blobmsg_get_string(tb
[WPAS_CONFIG_CTRL
]);
257 if (tb
[WPAS_CONFIG_HOSTAPD_CTRL
])
258 iface
->hostapd_ctrl
= blobmsg_get_string(tb
[WPAS_CONFIG_HOSTAPD_CTRL
]);
260 if (!wpa_supplicant_add_iface(global
, iface
, NULL
))
261 return UBUS_STATUS_INVALID_ARGUMENT
;
263 blob_buf_init(&b
, 0);
264 blobmsg_add_u32(&b
, "pid", getpid());
265 ubus_send_reply(ctx
, req
, b
.head
);
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
;
327 if (!wpas_ubus_init())
330 obj
->name
= strdup("wpa_supplicant");
332 obj
->type
= &wpas_daemon_object_type
;
333 obj
->methods
= wpas_daemon_object_type
.methods
;
334 obj
->n_methods
= wpas_daemon_object_type
.n_methods
;
335 ret
= ubus_add_object(ctx
, obj
);
339 void wpas_ubus_free(struct wpa_global
*global
)
341 struct ubus_object
*obj
= &global
->ubus_global
;
342 char *name
= (char *) obj
->name
;
348 ubus_remove_object(ctx
, obj
);
357 void wpas_ubus_notify(struct wpa_supplicant
*wpa_s
, const struct wps_credential
*cred
)
360 char *ifname
, *encryption
, *ssid
, *key
;
366 auth_type
= cred
->auth_type
;
368 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
))
369 auth_type
= WPS_AUTH_WPA2PSK
;
371 if (auth_type
!= WPS_AUTH_OPEN
&&
372 auth_type
!= WPS_AUTH_WPAPSK
&&
373 auth_type
!= WPS_AUTH_WPA2PSK
) {
374 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
375 "unsupported authentication type 0x%x",
380 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
381 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
382 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
383 "invalid Network Key length %lu",
384 (unsigned long) cred
->key_len
);
389 blob_buf_init(&b
, 0);
391 ifname_len
= strlen(wpa_s
->ifname
);
392 ifname
= blobmsg_alloc_string_buffer(&b
, "ifname", ifname_len
+ 1);
393 memcpy(ifname
, wpa_s
->ifname
, ifname_len
+ 1);
394 ifname
[ifname_len
] = '\0';
395 blobmsg_add_string_buffer(&b
);
398 case WPS_AUTH_WPA2PSK
:
401 case WPS_AUTH_WPAPSK
:
409 blobmsg_add_string(&b
, "encryption", encryption
);
411 ssid
= blobmsg_alloc_string_buffer(&b
, "ssid", cred
->ssid_len
+ 1);
412 memcpy(ssid
, cred
->ssid
, cred
->ssid_len
);
413 ssid
[cred
->ssid_len
] = '\0';
414 blobmsg_add_string_buffer(&b
);
416 if (cred
->key_len
> 0) {
417 key
= blobmsg_alloc_string_buffer(&b
, "key", cred
->key_len
+ 1);
418 memcpy(key
, cred
->key
, cred
->key_len
);
419 key
[cred
->key_len
] = '\0';
420 blobmsg_add_string_buffer(&b
);
423 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
424 ubus_send_event(ctx
, "wps_credentials", b
.head
);
426 #endif /* CONFIG_WPS */