mirror of
https://git.kernel.org/pub/scm/network/wireless/iwd.git
synced 2025-01-03 19:02:34 +01:00
wsc: Split out enrollee state machine to own object
Create struct wsc_enrollee which is allocated with wsc_enrollee_new, taking a done callback as a parameter. The callback is always called so there's no need for a separate destroy callback. The object only lives until the done callback happens so wsc_enrollee_cancel/destroy can only be used before this. Looks like the rest of the file is simplified thanks to this.
This commit is contained in:
parent
02f7d425b3
commit
b10264dcc7
792
src/wsc.c
792
src/wsc.c
@ -51,9 +51,20 @@
|
||||
|
||||
static uint32_t netdev_watch = 0;
|
||||
|
||||
struct wsc_enrollee {
|
||||
struct netdev *netdev;
|
||||
struct wsc_credentials_info creds[3];
|
||||
uint32_t n_creds;
|
||||
struct l_settings *eap_settings;
|
||||
wsc_done_cb_t done_cb;
|
||||
void *done_data;
|
||||
bool disconnecting : 1;
|
||||
};
|
||||
|
||||
struct wsc {
|
||||
struct netdev *netdev;
|
||||
struct station *station;
|
||||
struct wsc_enrollee *enrollee;
|
||||
struct l_dbus_message *pending;
|
||||
struct l_dbus_message *pending_cancel;
|
||||
uint8_t *wsc_ies;
|
||||
@ -62,16 +73,6 @@ struct wsc {
|
||||
uint32_t scan_id;
|
||||
struct scan_bss *target;
|
||||
uint32_t station_state_watch;
|
||||
struct wsc_credentials_info creds[3];
|
||||
uint32_t n_creds;
|
||||
struct l_settings *eap_settings;
|
||||
char *pin;
|
||||
|
||||
wsc_done_cb_t done_cb;
|
||||
void *done_data;
|
||||
uint16_t config_method;
|
||||
|
||||
bool wsc_association : 1;
|
||||
};
|
||||
|
||||
static struct l_dbus_message *wsc_error_session_overlap(
|
||||
@ -113,9 +114,364 @@ static struct l_dbus_message *wsc_error_time_expired(struct l_dbus_message *msg)
|
||||
"No APs in PIN mode found in "
|
||||
"the alloted time");
|
||||
}
|
||||
|
||||
static void wsc_enrollee_free(struct wsc_enrollee *wsce)
|
||||
{
|
||||
l_settings_free(wsce->eap_settings);
|
||||
explicit_bzero(wsce->creds, sizeof(wsce->creds));
|
||||
l_free(wsce);
|
||||
}
|
||||
|
||||
static void wsc_enrollee_connect_cb(struct netdev *netdev,
|
||||
enum netdev_result result,
|
||||
void *event_data, void *user_data)
|
||||
{
|
||||
struct wsc_enrollee *wsce = user_data;
|
||||
|
||||
l_debug("%d, result: %d", netdev_get_ifindex(wsce->netdev), result);
|
||||
|
||||
if (wsce->disconnecting)
|
||||
return; /* Free the state in the disconnect callback */
|
||||
|
||||
if (!wsce->done_cb)
|
||||
goto done;
|
||||
|
||||
if (result == NETDEV_RESULT_HANDSHAKE_FAILED && wsce->n_creds > 0) {
|
||||
wsce->done_cb(0, wsce->creds, wsce->n_creds, wsce->done_data);
|
||||
goto done;
|
||||
}
|
||||
|
||||
switch (result) {
|
||||
case NETDEV_RESULT_ABORTED:
|
||||
wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
|
||||
break;
|
||||
case NETDEV_RESULT_HANDSHAKE_FAILED:
|
||||
wsce->done_cb(-ENOKEY, NULL, 0, wsce->done_data);
|
||||
break;
|
||||
default:
|
||||
wsce->done_cb(-EIO, NULL, 0, wsce->done_data);
|
||||
break;
|
||||
}
|
||||
|
||||
done:
|
||||
wsc_enrollee_free(wsce);
|
||||
}
|
||||
|
||||
static void wsc_enrollee_credential_obtained(struct wsc_enrollee *wsce,
|
||||
const struct wsc_credential *cred)
|
||||
{
|
||||
uint16_t auth_mask;
|
||||
unsigned int i;
|
||||
|
||||
l_debug("Obtained credenials for SSID: %s, address: %s",
|
||||
util_ssid_to_utf8(cred->ssid_len, cred->ssid),
|
||||
util_address_to_string(cred->addr));
|
||||
|
||||
l_debug("auth_type: %02x, encryption_type: %02x",
|
||||
cred->auth_type, cred->encryption_type);
|
||||
|
||||
if (getenv("IWD_WSC_DEBUG_KEYS"))
|
||||
l_debug("Key (%u): %.*s", cred->network_key_len,
|
||||
cred->network_key_len, cred->network_key);
|
||||
|
||||
if (wsce->n_creds == L_ARRAY_SIZE(wsce->creds)) {
|
||||
l_warn("Maximum number of credentials obtained, ignoring...");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!util_ssid_is_utf8(cred->ssid_len, cred->ssid)) {
|
||||
l_warn("Ignoring Credentials with non-UTF8 SSID");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsce->creds[wsce->n_creds].ssid, cred->ssid, cred->ssid_len);
|
||||
wsce->creds[wsce->n_creds].ssid[cred->ssid_len] = '\0';
|
||||
|
||||
/* We only support open/personal wpa/personal wpa2 */
|
||||
auth_mask = WSC_AUTHENTICATION_TYPE_OPEN |
|
||||
WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
|
||||
WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL;
|
||||
if ((cred->auth_type & auth_mask) == 0) {
|
||||
l_warn("Ignoring Credentials with unsupported auth_type");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cred->auth_type & WSC_AUTHENTICATION_TYPE_OPEN) {
|
||||
auth_mask &= ~WSC_AUTHENTICATION_TYPE_OPEN;
|
||||
|
||||
if (cred->auth_type & auth_mask) {
|
||||
l_warn("Ignoring mixed open/wpa credentials");
|
||||
return;
|
||||
}
|
||||
|
||||
wsce->creds[wsce->n_creds].security = SECURITY_NONE;
|
||||
} else
|
||||
wsce->creds[wsce->n_creds].security = SECURITY_PSK;
|
||||
|
||||
switch (wsce->creds[wsce->n_creds].security) {
|
||||
case SECURITY_NONE:
|
||||
if (cred->network_key_len != 0) {
|
||||
l_warn("ignoring invalid open key length");
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
case SECURITY_PSK:
|
||||
if (cred->network_key_len == 64) {
|
||||
unsigned char *decoded;
|
||||
const char *hex = (const char *) cred->network_key;
|
||||
|
||||
decoded = l_util_from_hexstring(hex, NULL);
|
||||
if (!decoded) {
|
||||
l_warn("Ignoring non-hex network_key");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsce->creds[wsce->n_creds].psk, decoded, 32);
|
||||
explicit_bzero(decoded, 32);
|
||||
l_free(decoded);
|
||||
} else {
|
||||
strncpy(wsce->creds[wsce->n_creds].passphrase,
|
||||
(const char *) cred->network_key,
|
||||
cred->network_key_len);
|
||||
wsce->creds[wsce->n_creds].has_passphrase = true;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < wsce->n_creds; i++) {
|
||||
if (strcmp(wsce->creds[i].ssid,
|
||||
wsce->creds[wsce->n_creds].ssid))
|
||||
continue;
|
||||
|
||||
l_warn("Found duplicate credentials for SSID: %s",
|
||||
wsce->creds[i].ssid);
|
||||
explicit_bzero(&wsce->creds[wsce->n_creds],
|
||||
sizeof(wsce->creds[wsce->n_creds]));
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsce->creds[wsce->n_creds].addr, cred->addr, 6);
|
||||
wsce->n_creds += 1;
|
||||
}
|
||||
|
||||
static void wsc_enrollee_netdev_event(struct netdev *netdev,
|
||||
enum netdev_event event,
|
||||
void *event_data, void *user_data)
|
||||
{
|
||||
struct wsc_enrollee *wsce = user_data;
|
||||
|
||||
switch (event) {
|
||||
case NETDEV_EVENT_AUTHENTICATING:
|
||||
case NETDEV_EVENT_ASSOCIATING:
|
||||
break;
|
||||
case NETDEV_EVENT_LOST_BEACON:
|
||||
l_debug("Lost beacon");
|
||||
break;
|
||||
case NETDEV_EVENT_DISCONNECT_BY_AP:
|
||||
l_debug("Disconnect by AP");
|
||||
wsc_enrollee_connect_cb(wsce->netdev,
|
||||
NETDEV_RESULT_HANDSHAKE_FAILED,
|
||||
event_data, wsce);
|
||||
break;
|
||||
case NETDEV_EVENT_RSSI_THRESHOLD_LOW:
|
||||
case NETDEV_EVENT_RSSI_THRESHOLD_HIGH:
|
||||
break;
|
||||
default:
|
||||
l_debug("Unexpected event: %d", event);
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
static void wsc_enrollee_handshake_event(struct handshake_state *hs,
|
||||
enum handshake_event event,
|
||||
void *user_data, ...)
|
||||
{
|
||||
struct wsc_enrollee *wsce = user_data;
|
||||
va_list args;
|
||||
|
||||
va_start(args, user_data);
|
||||
|
||||
switch (event) {
|
||||
case HANDSHAKE_EVENT_FAILED:
|
||||
netdev_handshake_failed(hs, va_arg(args, int));
|
||||
break;
|
||||
case HANDSHAKE_EVENT_EAP_NOTIFY:
|
||||
{
|
||||
unsigned int eap_event = va_arg(args, unsigned int);
|
||||
|
||||
switch (eap_event) {
|
||||
case EAP_WSC_EVENT_CREDENTIAL_OBTAINED:
|
||||
wsc_enrollee_credential_obtained(wsce,
|
||||
va_arg(args, const struct wsc_credential *));
|
||||
break;
|
||||
default:
|
||||
l_debug("Got event: %d", eap_event);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static inline enum wsc_rf_band freq_to_rf_band(uint32_t freq)
|
||||
{
|
||||
enum scan_band band;
|
||||
|
||||
scan_freq_to_channel(freq, &band);
|
||||
|
||||
switch (band) {
|
||||
case SCAN_BAND_2_4_GHZ:
|
||||
return WSC_RF_BAND_2_4_GHZ;
|
||||
case SCAN_BAND_5_GHZ:
|
||||
return WSC_RF_BAND_5_0_GHZ;
|
||||
}
|
||||
|
||||
return WSC_RF_BAND_2_4_GHZ;
|
||||
}
|
||||
|
||||
static int wsc_enrollee_connect(struct wsc_enrollee *wsce, struct scan_bss *bss,
|
||||
const char *pin)
|
||||
{
|
||||
struct handshake_state *hs;
|
||||
struct l_settings *settings = l_settings_new();
|
||||
int r;
|
||||
struct wsc_association_request request;
|
||||
uint8_t *pdu;
|
||||
size_t pdu_len;
|
||||
struct iovec ie_iov;
|
||||
|
||||
hs = netdev_handshake_state_new(wsce->netdev);
|
||||
|
||||
l_settings_set_string(settings, "Security", "EAP-Identity",
|
||||
"WFA-SimpleConfig-Enrollee-1-0");
|
||||
l_settings_set_string(settings, "Security", "EAP-Method", "WSC");
|
||||
|
||||
l_settings_set_uint(settings, "WSC", "RFBand",
|
||||
freq_to_rf_band(bss->frequency));
|
||||
l_settings_set_uint(settings, "WSC", "ConfigurationMethods",
|
||||
WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
|
||||
WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
|
||||
WSC_CONFIGURATION_METHOD_KEYPAD);
|
||||
l_settings_set_string(settings, "WSC", "PrimaryDeviceType",
|
||||
"0-00000000-0");
|
||||
l_settings_set_string(settings, "WSC", "EnrolleeMAC",
|
||||
util_address_to_string(netdev_get_address(wsce->netdev)));
|
||||
|
||||
if (pin) {
|
||||
enum wsc_device_password_id dpid;
|
||||
|
||||
if (strlen(pin) == 4 || wsc_pin_is_checksum_valid(pin))
|
||||
dpid = WSC_DEVICE_PASSWORD_ID_DEFAULT;
|
||||
else
|
||||
dpid = WSC_DEVICE_PASSWORD_ID_USER_SPECIFIED;
|
||||
|
||||
l_settings_set_uint(settings, "WSC", "DevicePasswordId", dpid);
|
||||
l_settings_set_string(settings, "WSC", "DevicePassword", pin);
|
||||
}
|
||||
|
||||
handshake_state_set_event_func(hs, wsc_enrollee_handshake_event, wsce);
|
||||
handshake_state_set_8021x_config(hs, settings);
|
||||
wsce->eap_settings = settings;
|
||||
|
||||
request.version2 = true;
|
||||
request.request_type = WSC_REQUEST_TYPE_ENROLLEE_OPEN_8021X;
|
||||
|
||||
pdu = wsc_build_association_request(&request, &pdu_len);
|
||||
if (!pdu) {
|
||||
r = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ie_iov.iov_base = ie_tlv_encapsulate_wsc_payload(pdu, pdu_len,
|
||||
&ie_iov.iov_len);
|
||||
l_free(pdu);
|
||||
|
||||
if (!ie_iov.iov_base) {
|
||||
r = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
r = netdev_connect(wsce->netdev, bss, hs, &ie_iov, 1,
|
||||
wsc_enrollee_netdev_event,
|
||||
wsc_enrollee_connect_cb, wsce);
|
||||
l_free(ie_iov.iov_base);
|
||||
|
||||
if (r == 0)
|
||||
return 0;
|
||||
|
||||
error:
|
||||
handshake_state_free(hs);
|
||||
return r;
|
||||
}
|
||||
|
||||
struct wsc_enrollee *wsc_enrollee_new(struct netdev *netdev,
|
||||
struct scan_bss *target,
|
||||
const char *pin,
|
||||
wsc_done_cb_t done_cb, void *user_data)
|
||||
{
|
||||
struct wsc_enrollee *wsce;
|
||||
|
||||
wsce = l_new(struct wsc_enrollee, 1);
|
||||
wsce->netdev = netdev;
|
||||
wsce->done_cb = done_cb;
|
||||
wsce->done_data = user_data;
|
||||
|
||||
if (wsc_enrollee_connect(wsce, target, pin) == 0)
|
||||
return wsce;
|
||||
|
||||
wsc_enrollee_free(wsce);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void wsc_enrollee_disconnect_cb(struct netdev *netdev, bool result,
|
||||
void *user_data)
|
||||
{
|
||||
struct wsc_enrollee *wsce = user_data;
|
||||
|
||||
wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
|
||||
wsc_enrollee_free(wsce);
|
||||
}
|
||||
|
||||
void wsc_enrollee_cancel(struct wsc_enrollee *wsce, bool defer_cb)
|
||||
{
|
||||
if (defer_cb) {
|
||||
wsce->disconnecting = true;
|
||||
netdev_disconnect(wsce->netdev, wsc_enrollee_disconnect_cb,
|
||||
wsce);
|
||||
} else {
|
||||
wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
|
||||
wsce->done_cb = NULL;
|
||||
/*
|
||||
* Results in a call to
|
||||
* wsc_enrollee_connect_cb -> wsc_enrollee_free
|
||||
*/
|
||||
netdev_disconnect(wsce->netdev, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* To be called instead of wsc_enrollee_cancel if we know the netdev is gone */
|
||||
void wsc_enrollee_destroy(struct wsc_enrollee *wsce)
|
||||
{
|
||||
/*
|
||||
* Note: wsc_enrollee_cancel(wsce, true) may have been called before
|
||||
* but since the netdev is going away wsc_enrollee_disconnect_cb
|
||||
* is not happening anymore.
|
||||
*/
|
||||
wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
|
||||
wsc_enrollee_free(wsce);
|
||||
}
|
||||
|
||||
static void wsc_try_credentials(struct wsc *wsc,
|
||||
struct wsc_credentials_info *creds,
|
||||
unsigned int n_creds)
|
||||
struct wsc_credentials_info *creds,
|
||||
unsigned int n_creds)
|
||||
{
|
||||
unsigned int i;
|
||||
struct network *network;
|
||||
@ -203,352 +559,34 @@ static void wsc_store_credentials(struct wsc_credentials_info *creds,
|
||||
}
|
||||
}
|
||||
|
||||
static void wsc_disconnect_cb(struct netdev *netdev, bool success,
|
||||
void *user_data)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
struct l_dbus_message *reply;
|
||||
|
||||
l_debug("%p, success: %d", wsc, success);
|
||||
|
||||
wsc->wsc_association = false;
|
||||
|
||||
reply = l_dbus_message_new_method_return(wsc->pending_cancel);
|
||||
l_dbus_message_set_arguments(reply, "");
|
||||
dbus_pending_reply(&wsc->pending_cancel, reply);
|
||||
|
||||
station_set_autoconnect(wsc->station, true);
|
||||
}
|
||||
|
||||
static void wsc_connect_cleanup(struct wsc *wsc)
|
||||
{
|
||||
wsc->wsc_association = false;
|
||||
|
||||
l_settings_free(wsc->eap_settings);
|
||||
wsc->eap_settings = NULL;
|
||||
|
||||
if (wsc->pin) {
|
||||
explicit_bzero(wsc->pin, strlen(wsc->pin));
|
||||
l_free(wsc->pin);
|
||||
wsc->pin = NULL;
|
||||
}
|
||||
|
||||
wsc->target = NULL;
|
||||
}
|
||||
|
||||
static void wsc_connect_cb(struct netdev *netdev, enum netdev_result result,
|
||||
void *event_data, void *user_data)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
|
||||
l_debug("%d, result: %d", netdev_get_ifindex(wsc->netdev), result);
|
||||
|
||||
wsc_connect_cleanup(wsc);
|
||||
|
||||
if (result == NETDEV_RESULT_HANDSHAKE_FAILED && wsc->n_creds > 0) {
|
||||
struct wsc_credentials_info creds[L_ARRAY_SIZE(wsc->creds)];
|
||||
uint32_t n_creds = wsc->n_creds;
|
||||
|
||||
/*
|
||||
* Once we call done_cb, the state is assumed to be wiped,
|
||||
* so use a temporary array for creds here
|
||||
*/
|
||||
memcpy(creds, wsc->creds, sizeof(creds));
|
||||
explicit_bzero(wsc->creds, sizeof(creds));
|
||||
wsc->n_creds = 0;
|
||||
wsc->done_cb(0, creds, n_creds, wsc->done_data);
|
||||
explicit_bzero(creds, sizeof(creds));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (result) {
|
||||
case NETDEV_RESULT_ABORTED:
|
||||
wsc->done_cb(-ECANCELED, NULL, 0, wsc->done_data);
|
||||
return;
|
||||
case NETDEV_RESULT_HANDSHAKE_FAILED:
|
||||
wsc->done_cb(-ENOKEY, NULL, 0, wsc->done_data);
|
||||
break;
|
||||
default:
|
||||
wsc->done_cb(-EIO, NULL, 0, wsc->done_data);
|
||||
break;
|
||||
}
|
||||
|
||||
if (wsc->station)
|
||||
station_set_autoconnect(wsc->station, true);
|
||||
}
|
||||
|
||||
static void wsc_credential_obtained(struct wsc *wsc,
|
||||
const struct wsc_credential *cred)
|
||||
{
|
||||
uint16_t auth_mask;
|
||||
unsigned int i;
|
||||
|
||||
l_debug("Obtained credenials for SSID: %s, address: %s",
|
||||
util_ssid_to_utf8(cred->ssid_len, cred->ssid),
|
||||
util_address_to_string(cred->addr));
|
||||
|
||||
l_debug("auth_type: %02x, encryption_type: %02x",
|
||||
cred->auth_type, cred->encryption_type);
|
||||
|
||||
if (getenv("IWD_WSC_DEBUG_KEYS"))
|
||||
l_debug("Key (%u): %.*s", cred->network_key_len,
|
||||
cred->network_key_len, cred->network_key);
|
||||
|
||||
if (wsc->n_creds == L_ARRAY_SIZE(wsc->creds)) {
|
||||
l_warn("Maximum number of credentials obtained, ignoring...");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!util_ssid_is_utf8(cred->ssid_len, cred->ssid)) {
|
||||
l_warn("Ignoring Credentials with non-UTF8 SSID");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsc->creds[wsc->n_creds].ssid, cred->ssid, cred->ssid_len);
|
||||
wsc->creds[wsc->n_creds].ssid[cred->ssid_len] = '\0';
|
||||
|
||||
/* We only support open/personal wpa/personal wpa2 */
|
||||
auth_mask = WSC_AUTHENTICATION_TYPE_OPEN |
|
||||
WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
|
||||
WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL;
|
||||
if ((cred->auth_type & auth_mask) == 0) {
|
||||
l_warn("Ignoring Credentials with unsupported auth_type");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cred->auth_type & WSC_AUTHENTICATION_TYPE_OPEN) {
|
||||
auth_mask &= ~WSC_AUTHENTICATION_TYPE_OPEN;
|
||||
|
||||
if (cred->auth_type & auth_mask) {
|
||||
l_warn("Ignoring mixed open/wpa credentials");
|
||||
return;
|
||||
}
|
||||
|
||||
wsc->creds[wsc->n_creds].security = SECURITY_NONE;
|
||||
} else
|
||||
wsc->creds[wsc->n_creds].security = SECURITY_PSK;
|
||||
|
||||
switch (wsc->creds[wsc->n_creds].security) {
|
||||
case SECURITY_NONE:
|
||||
if (cred->network_key_len != 0) {
|
||||
l_warn("ignoring invalid open key length");
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
case SECURITY_PSK:
|
||||
if (cred->network_key_len == 64) {
|
||||
unsigned char *decoded;
|
||||
const char *hex = (const char *) cred->network_key;
|
||||
|
||||
decoded = l_util_from_hexstring(hex, NULL);
|
||||
if (!decoded) {
|
||||
l_warn("Ignoring non-hex network_key");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsc->creds[wsc->n_creds].psk, decoded, 32);
|
||||
explicit_bzero(decoded, 32);
|
||||
l_free(decoded);
|
||||
} else {
|
||||
strncpy(wsc->creds[wsc->n_creds].passphrase,
|
||||
(const char *) cred->network_key,
|
||||
cred->network_key_len);
|
||||
wsc->creds[wsc->n_creds].has_passphrase = true;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < wsc->n_creds; i++) {
|
||||
if (strcmp(wsc->creds[i].ssid, wsc->creds[wsc->n_creds].ssid))
|
||||
continue;
|
||||
|
||||
l_warn("Found duplicate credentials for SSID: %s",
|
||||
wsc->creds[i].ssid);
|
||||
explicit_bzero(&wsc->creds[wsc->n_creds],
|
||||
sizeof(wsc->creds[wsc->n_creds]));
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(wsc->creds[wsc->n_creds].addr, cred->addr, 6);
|
||||
wsc->n_creds += 1;
|
||||
}
|
||||
|
||||
static void wsc_netdev_event(struct netdev *netdev, enum netdev_event event,
|
||||
void *event_data, void *user_data)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
|
||||
switch (event) {
|
||||
case NETDEV_EVENT_AUTHENTICATING:
|
||||
case NETDEV_EVENT_ASSOCIATING:
|
||||
break;
|
||||
case NETDEV_EVENT_LOST_BEACON:
|
||||
l_debug("Lost beacon");
|
||||
break;
|
||||
case NETDEV_EVENT_DISCONNECT_BY_AP:
|
||||
l_debug("Disconnect by AP");
|
||||
wsc_connect_cb(wsc->netdev, NETDEV_RESULT_HANDSHAKE_FAILED,
|
||||
event_data, wsc);
|
||||
break;
|
||||
case NETDEV_EVENT_RSSI_THRESHOLD_LOW:
|
||||
case NETDEV_EVENT_RSSI_THRESHOLD_HIGH:
|
||||
break;
|
||||
default:
|
||||
l_debug("Unexpected event: %d", event);
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
static void wsc_handshake_event(struct handshake_state *hs,
|
||||
enum handshake_event event, void *user_data,
|
||||
...)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
va_list args;
|
||||
|
||||
va_start(args, user_data);
|
||||
|
||||
switch (event) {
|
||||
case HANDSHAKE_EVENT_FAILED:
|
||||
netdev_handshake_failed(hs, va_arg(args, int));
|
||||
break;
|
||||
case HANDSHAKE_EVENT_EAP_NOTIFY:
|
||||
{
|
||||
unsigned int eap_event = va_arg(args, unsigned int);
|
||||
|
||||
switch (eap_event) {
|
||||
case EAP_WSC_EVENT_CREDENTIAL_OBTAINED:
|
||||
wsc_credential_obtained(wsc,
|
||||
va_arg(args, const struct wsc_credential *));
|
||||
break;
|
||||
default:
|
||||
l_debug("Got event: %d", eap_event);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static inline enum wsc_rf_band freq_to_rf_band(uint32_t freq)
|
||||
{
|
||||
enum scan_band band;
|
||||
|
||||
scan_freq_to_channel(freq, &band);
|
||||
|
||||
switch (band) {
|
||||
case SCAN_BAND_2_4_GHZ:
|
||||
return WSC_RF_BAND_2_4_GHZ;
|
||||
case SCAN_BAND_5_GHZ:
|
||||
return WSC_RF_BAND_5_0_GHZ;
|
||||
}
|
||||
|
||||
return WSC_RF_BAND_2_4_GHZ;
|
||||
}
|
||||
|
||||
static void wsc_connect(struct wsc *wsc)
|
||||
{
|
||||
struct handshake_state *hs;
|
||||
struct l_settings *settings = l_settings_new();
|
||||
struct scan_bss *bss = wsc->target;
|
||||
int r;
|
||||
struct wsc_association_request request;
|
||||
uint8_t *pdu;
|
||||
size_t pdu_len;
|
||||
struct iovec ie_iov;
|
||||
|
||||
wsc->target = NULL;
|
||||
|
||||
hs = netdev_handshake_state_new(wsc->netdev);
|
||||
|
||||
l_settings_set_string(settings, "Security", "EAP-Identity",
|
||||
"WFA-SimpleConfig-Enrollee-1-0");
|
||||
l_settings_set_string(settings, "Security", "EAP-Method", "WSC");
|
||||
|
||||
l_settings_set_uint(settings, "WSC", "RFBand",
|
||||
freq_to_rf_band(bss->frequency));
|
||||
l_settings_set_uint(settings, "WSC", "ConfigurationMethods",
|
||||
WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
|
||||
WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
|
||||
WSC_CONFIGURATION_METHOD_KEYPAD);
|
||||
l_settings_set_string(settings, "WSC", "PrimaryDeviceType",
|
||||
"0-00000000-0");
|
||||
l_settings_set_string(settings, "WSC", "EnrolleeMAC",
|
||||
util_address_to_string(netdev_get_address(wsc->netdev)));
|
||||
|
||||
if (wsc->config_method == WSC_CONFIGURATION_METHOD_KEYPAD) {
|
||||
enum wsc_device_password_id dpid;
|
||||
|
||||
if (strlen(wsc->pin) == 4 ||
|
||||
wsc_pin_is_checksum_valid(wsc->pin))
|
||||
dpid = WSC_DEVICE_PASSWORD_ID_DEFAULT;
|
||||
else
|
||||
dpid = WSC_DEVICE_PASSWORD_ID_USER_SPECIFIED;
|
||||
|
||||
l_settings_set_uint(settings, "WSC", "DevicePasswordId", dpid);
|
||||
l_settings_set_string(settings, "WSC", "DevicePassword",
|
||||
wsc->pin);
|
||||
}
|
||||
|
||||
handshake_state_set_event_func(hs, wsc_handshake_event, wsc);
|
||||
handshake_state_set_8021x_config(hs, settings);
|
||||
wsc->eap_settings = settings;
|
||||
|
||||
request.version2 = true;
|
||||
request.request_type = WSC_REQUEST_TYPE_ENROLLEE_OPEN_8021X;
|
||||
|
||||
pdu = wsc_build_association_request(&request, &pdu_len);
|
||||
if (!pdu) {
|
||||
r = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ie_iov.iov_base = ie_tlv_encapsulate_wsc_payload(pdu, pdu_len,
|
||||
&ie_iov.iov_len);
|
||||
l_free(pdu);
|
||||
|
||||
if (!ie_iov.iov_base) {
|
||||
r = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
r = netdev_connect(wsc->netdev, bss, hs, &ie_iov, 1, wsc_netdev_event,
|
||||
wsc_connect_cb, wsc);
|
||||
l_free(ie_iov.iov_base);
|
||||
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
wsc->wsc_association = true;
|
||||
return;
|
||||
error:
|
||||
handshake_state_free(hs);
|
||||
wsc_connect_cleanup(wsc);
|
||||
wsc->done_cb(r, NULL, 0, wsc->done_data);
|
||||
}
|
||||
|
||||
/* Done callback for when WSC is triggered by DBus methods */
|
||||
static void wsc_dbus_done_cb(int err, struct wsc_credentials_info *creds,
|
||||
unsigned int n_creds, void *user_data)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
|
||||
wsc->enrollee = NULL;
|
||||
wsc->target = NULL;
|
||||
|
||||
l_debug("err=%i", err);
|
||||
|
||||
if (err && wsc->station)
|
||||
station_set_autoconnect(wsc->station, true);
|
||||
|
||||
switch (err) {
|
||||
case 0:
|
||||
break;
|
||||
case -ECANCELED:
|
||||
dbus_pending_reply(&wsc->pending,
|
||||
/* Send reply if we haven't already sent one e.g. in Cancel() */
|
||||
if (wsc->pending)
|
||||
dbus_pending_reply(&wsc->pending,
|
||||
dbus_error_aborted(wsc->pending));
|
||||
|
||||
if (wsc->pending_cancel)
|
||||
dbus_pending_reply(&wsc->pending_cancel,
|
||||
l_dbus_message_new_method_return(
|
||||
wsc->pending_cancel));
|
||||
|
||||
return;
|
||||
case -ENOKEY:
|
||||
dbus_pending_reply(&wsc->pending,
|
||||
@ -568,6 +606,21 @@ static void wsc_dbus_done_cb(int err, struct wsc_credentials_info *creds,
|
||||
wsc_try_credentials(wsc, creds, n_creds);
|
||||
}
|
||||
|
||||
static void wsc_connect(struct wsc *wsc)
|
||||
{
|
||||
const char *pin = NULL;
|
||||
|
||||
if (!strcmp(l_dbus_message_get_member(wsc->pending), "StartPin"))
|
||||
l_dbus_message_get_arguments(wsc->pending, "s", &pin);
|
||||
|
||||
wsc->enrollee = wsc_enrollee_new(wsc->netdev, wsc->target, pin,
|
||||
wsc_dbus_done_cb, wsc);
|
||||
if (wsc->enrollee)
|
||||
return;
|
||||
|
||||
wsc_dbus_done_cb(-EIO, NULL, 0, wsc);
|
||||
}
|
||||
|
||||
static void station_state_watch(enum station_state state, void *userdata)
|
||||
{
|
||||
struct wsc *wsc = userdata;
|
||||
@ -594,22 +647,6 @@ static void wsc_check_can_connect(struct wsc *wsc, struct scan_bss *target)
|
||||
wsc->target = target;
|
||||
station_set_autoconnect(wsc->station, false);
|
||||
|
||||
if (!strcmp(l_dbus_message_get_member(wsc->pending), "StartPin")) {
|
||||
char *pin;
|
||||
|
||||
wsc->config_method = WSC_CONFIGURATION_METHOD_KEYPAD;
|
||||
|
||||
if (!l_dbus_message_get_arguments(wsc->pending, "s", &pin))
|
||||
goto error;
|
||||
|
||||
wsc->pin = l_strdup(pin);
|
||||
} else
|
||||
wsc->config_method =
|
||||
WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON;
|
||||
|
||||
wsc->done_cb = wsc_dbus_done_cb;
|
||||
wsc->done_data = wsc;
|
||||
|
||||
switch (station_get_state(wsc->station)) {
|
||||
case STATION_STATE_DISCONNECTED:
|
||||
wsc_connect(wsc);
|
||||
@ -633,7 +670,7 @@ static void wsc_check_can_connect(struct wsc *wsc, struct scan_bss *target)
|
||||
break;
|
||||
}
|
||||
error:
|
||||
wsc_connect_cleanup(wsc);
|
||||
wsc->target = NULL;
|
||||
dbus_pending_reply(&wsc->pending, dbus_error_failed(wsc->pending));
|
||||
}
|
||||
|
||||
@ -1089,13 +1126,15 @@ static struct l_dbus_message *wsc_cancel(struct l_dbus *dbus,
|
||||
void *user_data)
|
||||
{
|
||||
struct wsc *wsc = user_data;
|
||||
struct l_dbus_message *reply;
|
||||
|
||||
l_debug("");
|
||||
|
||||
if (!wsc->pending)
|
||||
return dbus_error_not_available(message);
|
||||
|
||||
if (wsc->pending_cancel)
|
||||
return dbus_error_busy(message);
|
||||
|
||||
wsc_cancel_scan(wsc);
|
||||
|
||||
if (wsc->station_state_watch) {
|
||||
@ -1105,25 +1144,14 @@ static struct l_dbus_message *wsc_cancel(struct l_dbus *dbus,
|
||||
wsc->target = NULL;
|
||||
}
|
||||
|
||||
if (wsc->wsc_association) {
|
||||
int r;
|
||||
|
||||
r = netdev_disconnect(wsc->netdev, wsc_disconnect_cb, wsc);
|
||||
if (r == 0) {
|
||||
wsc->pending_cancel = l_dbus_message_ref(message);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
l_warn("Unable to initiate disconnect: %s", strerror(-r));
|
||||
wsc->wsc_association = false;
|
||||
}
|
||||
|
||||
dbus_pending_reply(&wsc->pending, dbus_error_aborted(wsc->pending));
|
||||
|
||||
reply = l_dbus_message_new_method_return(message);
|
||||
l_dbus_message_set_arguments(reply, "");
|
||||
|
||||
return reply;
|
||||
if (wsc->enrollee) {
|
||||
wsc_enrollee_cancel(wsc->enrollee, true);
|
||||
wsc->pending_cancel = l_dbus_message_ref(message);
|
||||
return NULL;
|
||||
} else
|
||||
return l_dbus_message_new_method_return(message);
|
||||
}
|
||||
|
||||
static void setup_wsc_interface(struct l_dbus_interface *interface)
|
||||
@ -1143,7 +1171,6 @@ static void wsc_free(void *userdata)
|
||||
struct wsc *wsc = userdata;
|
||||
|
||||
wsc_cancel_scan(wsc);
|
||||
wsc_connect_cleanup(wsc);
|
||||
|
||||
if (wsc->station_state_watch) {
|
||||
station_remove_state_watch(wsc->station,
|
||||
@ -1159,6 +1186,9 @@ static void wsc_free(void *userdata)
|
||||
dbus_pending_reply(&wsc->pending_cancel,
|
||||
dbus_error_aborted(wsc->pending_cancel));
|
||||
|
||||
if (wsc->enrollee)
|
||||
wsc_enrollee_destroy(wsc->enrollee);
|
||||
|
||||
l_free(wsc);
|
||||
}
|
||||
|
||||
|
@ -31,5 +31,14 @@ struct wsc_credentials_info {
|
||||
bool has_passphrase;
|
||||
};
|
||||
|
||||
struct wsc_enrollee;
|
||||
|
||||
typedef void (*wsc_done_cb_t)(int err, struct wsc_credentials_info *creds,
|
||||
unsigned int n_creds, void *user_data);
|
||||
|
||||
struct wsc_enrollee *wsc_enrollee_new(struct netdev *netdev,
|
||||
struct scan_bss *target,
|
||||
const char *pin,
|
||||
wsc_done_cb_t done_cb, void *user_data);
|
||||
void wsc_enrollee_cancel(struct wsc_enrollee *wsce, bool defer_cb);
|
||||
void wsc_enrollee_destroy(struct wsc_enrollee *wsce);
|
||||
|
Loading…
Reference in New Issue
Block a user