2015-06-18 12:18:26 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
2019-10-25 00:43:08 +02:00
|
|
|
* Copyright (C) 2013-2019 Intel Corporation. All rights reserved.
|
2015-06-18 12:18:26 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <stdio.h>
|
2016-05-16 21:24:21 +02:00
|
|
|
#include <errno.h>
|
2016-06-10 11:50:20 +02:00
|
|
|
#include <limits.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <alloca.h>
|
2015-06-18 12:18:26 +02:00
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2019-04-03 18:34:22 +02:00
|
|
|
#include "src/missing.h"
|
2019-11-07 23:33:51 +01:00
|
|
|
#include "src/module.h"
|
2016-05-16 19:37:48 +02:00
|
|
|
#include "src/ie.h"
|
|
|
|
#include "src/crypto.h"
|
2016-05-11 22:52:43 +02:00
|
|
|
#include "src/iwd.h"
|
|
|
|
#include "src/common.h"
|
2015-06-18 12:18:26 +02:00
|
|
|
#include "src/storage.h"
|
|
|
|
#include "src/scan.h"
|
2016-05-12 05:30:10 +02:00
|
|
|
#include "src/dbus.h"
|
2016-05-16 04:37:12 +02:00
|
|
|
#include "src/agent.h"
|
2018-09-04 22:45:37 +02:00
|
|
|
#include "src/netdev.h"
|
2016-05-16 19:37:48 +02:00
|
|
|
#include "src/wiphy.h"
|
2018-09-04 22:45:37 +02:00
|
|
|
#include "src/station.h"
|
2018-04-18 07:03:34 +02:00
|
|
|
#include "src/eap.h"
|
2018-07-22 14:15:18 +02:00
|
|
|
#include "src/knownnetworks.h"
|
2016-05-12 05:30:10 +02:00
|
|
|
#include "src/network.h"
|
2019-01-29 21:36:12 +01:00
|
|
|
#include "src/blacklist.h"
|
2019-06-26 19:42:50 +02:00
|
|
|
#include "src/util.h"
|
2015-06-18 12:18:26 +02:00
|
|
|
|
2019-08-09 19:03:27 +02:00
|
|
|
static uint32_t known_networks_watch;
|
2020-06-12 21:14:12 +02:00
|
|
|
static uint32_t anqp_watch;
|
2019-08-09 19:03:27 +02:00
|
|
|
|
2016-05-16 23:14:26 +02:00
|
|
|
struct network {
|
2019-08-09 00:48:05 +02:00
|
|
|
char ssid[33];
|
2019-08-09 07:26:09 +02:00
|
|
|
enum security security;
|
2016-05-16 23:14:26 +02:00
|
|
|
char *object_path;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station;
|
2016-06-17 00:54:13 +02:00
|
|
|
struct network_info *info;
|
2016-05-16 23:14:26 +02:00
|
|
|
unsigned char *psk;
|
2018-08-07 23:29:06 +02:00
|
|
|
char *passphrase;
|
2016-05-16 23:14:26 +02:00
|
|
|
unsigned int agent_request;
|
|
|
|
struct l_queue *bss_list;
|
|
|
|
struct l_settings *settings;
|
2018-04-18 07:03:34 +02:00
|
|
|
struct l_queue *secrets;
|
2019-03-01 19:55:03 +01:00
|
|
|
struct l_queue *blacklist; /* temporary blacklist for BSS's */
|
2019-06-26 19:42:50 +02:00
|
|
|
uint8_t hessid[6];
|
|
|
|
char **nai_realms;
|
2019-07-12 19:30:27 +02:00
|
|
|
uint8_t *rc_ie;
|
2016-05-16 23:14:26 +02:00
|
|
|
bool update_psk:1; /* Whether PSK should be written to storage */
|
2018-11-05 11:56:34 +01:00
|
|
|
bool ask_passphrase:1; /* Whether we should force-ask agent */
|
2019-07-19 20:18:52 +02:00
|
|
|
bool is_hs20:1;
|
2020-06-12 21:14:12 +02:00
|
|
|
bool anqp_pending:1; /* Set if there is a pending ANQP request */
|
2016-06-10 11:50:20 +02:00
|
|
|
int rank;
|
2020-06-12 21:14:12 +02:00
|
|
|
/* Holds DBus Connect() message if it comes in before ANQP finishes */
|
|
|
|
struct l_dbus_message *connect_after_anqp;
|
2016-05-16 23:14:26 +02:00
|
|
|
};
|
|
|
|
|
2016-09-20 00:03:51 +02:00
|
|
|
static bool network_settings_load(struct network *network)
|
|
|
|
{
|
|
|
|
if (network->settings)
|
|
|
|
return true;
|
|
|
|
|
2019-08-19 20:12:00 +02:00
|
|
|
if (network->info)
|
2019-08-15 22:15:13 +02:00
|
|
|
network->settings = network_info_open_settings(network->info);
|
2016-09-20 00:03:51 +02:00
|
|
|
|
|
|
|
return network->settings != NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
static void network_reset_psk(struct network *network)
|
2016-09-20 00:04:51 +02:00
|
|
|
{
|
2019-03-22 19:44:17 +01:00
|
|
|
if (network->psk)
|
|
|
|
explicit_bzero(network->psk, 32);
|
2016-09-20 00:04:51 +02:00
|
|
|
|
2016-10-10 21:52:43 +02:00
|
|
|
l_free(network->psk);
|
|
|
|
network->psk = NULL;
|
2019-03-22 19:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void network_reset_passphrase(struct network *network)
|
|
|
|
{
|
|
|
|
if (network->passphrase)
|
|
|
|
explicit_bzero(network->passphrase,
|
|
|
|
strlen(network->passphrase));
|
2016-10-10 21:52:43 +02:00
|
|
|
|
2018-08-07 23:29:06 +02:00
|
|
|
l_free(network->passphrase);
|
|
|
|
network->passphrase = NULL;
|
2019-03-22 19:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void network_settings_close(struct network *network)
|
|
|
|
{
|
|
|
|
if (!network->settings)
|
|
|
|
return;
|
|
|
|
|
|
|
|
network_reset_psk(network);
|
|
|
|
network_reset_passphrase(network);
|
2018-08-07 23:29:06 +02:00
|
|
|
|
2016-09-20 00:04:51 +02:00
|
|
|
l_settings_free(network->settings);
|
|
|
|
network->settings = NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:33:16 +02:00
|
|
|
static bool network_secret_check_cacheable(void *data, void *user_data)
|
|
|
|
{
|
|
|
|
struct eap_secret_info *secret = data;
|
|
|
|
|
2018-09-19 21:31:59 +02:00
|
|
|
if (secret->cache_policy == EAP_CACHE_NEVER) {
|
|
|
|
eap_secret_info_free(secret);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-08-09 02:33:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 14:15:17 +02:00
|
|
|
void network_connected(struct network *network)
|
2015-06-18 12:18:26 +02:00
|
|
|
{
|
2019-08-15 22:15:13 +02:00
|
|
|
enum security security = network_get_security(network);
|
2019-08-09 00:48:05 +02:00
|
|
|
const char *ssid = network_get_ssid(network);
|
2015-06-18 12:18:26 +02:00
|
|
|
int err;
|
2016-02-10 00:08:43 +01:00
|
|
|
|
2019-08-15 22:15:13 +02:00
|
|
|
if (!network->info) {
|
2016-06-07 05:29:51 +02:00
|
|
|
/*
|
2017-10-31 23:02:59 +01:00
|
|
|
* This is an open network seen for the first time:
|
|
|
|
*
|
2018-09-19 18:48:42 +02:00
|
|
|
* Write a settings file to keep track of the
|
2016-06-07 05:29:51 +02:00
|
|
|
* last connected time. This will also make iwd autoconnect
|
|
|
|
* to this network in the future.
|
|
|
|
*/
|
2018-09-19 18:48:42 +02:00
|
|
|
if (!network->settings)
|
|
|
|
network->settings = l_settings_new();
|
2016-06-07 05:29:51 +02:00
|
|
|
|
2019-08-15 22:15:13 +02:00
|
|
|
storage_network_sync(security, ssid, network->settings);
|
|
|
|
} else {
|
|
|
|
err = network_info_touch(network->info);
|
|
|
|
if (err < 0)
|
|
|
|
l_error("Error %i touching network config", err);
|
2019-09-12 18:53:35 +02:00
|
|
|
|
|
|
|
/* Syncs frequencies of already known network*/
|
|
|
|
known_network_frequency_sync(network->info);
|
2016-10-10 22:59:36 +02:00
|
|
|
}
|
2015-06-18 12:18:26 +02:00
|
|
|
|
2018-08-09 02:33:16 +02:00
|
|
|
l_queue_foreach_remove(network->secrets,
|
|
|
|
network_secret_check_cacheable, network);
|
2019-03-01 19:55:03 +01:00
|
|
|
|
|
|
|
l_queue_clear(network->blacklist, NULL);
|
2015-06-18 12:18:26 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 19:55:18 +02:00
|
|
|
void network_disconnected(struct network *network)
|
|
|
|
{
|
|
|
|
network_settings_close(network);
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:11:14 +02:00
|
|
|
/* First 64 entries calculated by 1 / pow(n, 0.3) for n >= 1 */
|
|
|
|
static const double rankmod_table[] = {
|
2015-06-22 22:08:02 +02:00
|
|
|
1.0000000000, 0.8122523964, 0.7192230933, 0.6597539554,
|
2015-06-18 13:11:14 +02:00
|
|
|
0.6170338627, 0.5841906811, 0.5577898253, 0.5358867313,
|
|
|
|
0.5172818580, 0.5011872336, 0.4870596972, 0.4745102806,
|
|
|
|
0.4632516708, 0.4530661223, 0.4437850034, 0.4352752816,
|
|
|
|
0.4274303178, 0.4201634287, 0.4134032816, 0.4070905315,
|
|
|
|
0.4011753236, 0.3956154062, 0.3903746872, 0.3854221125,
|
|
|
|
0.3807307877, 0.3762772797, 0.3720410580, 0.3680040435,
|
|
|
|
0.3641502401, 0.3604654325, 0.3569369365, 0.3535533906,
|
|
|
|
0.3503045821, 0.3471812999, 0.3441752105, 0.3412787518,
|
|
|
|
0.3384850430, 0.3357878061, 0.3331812996, 0.3306602598,
|
|
|
|
0.3282198502, 0.3258556179, 0.3235634544, 0.3213395618,
|
|
|
|
0.3191804229, 0.3170827751, 0.3150435863, 0.3130600345,
|
|
|
|
0.3111294892, 0.3092494947, 0.3074177553, 0.3056321221,
|
|
|
|
0.3038905808, 0.3021912409, 0.3005323264, 0.2989121662,
|
|
|
|
0.2973291870, 0.2957819051, 0.2942689208, 0.2927889114,
|
|
|
|
0.2913406263, 0.2899228820, 0.2885345572, 0.2871745887,
|
|
|
|
};
|
|
|
|
|
2016-06-09 19:55:20 +02:00
|
|
|
bool network_rankmod(const struct network *network, double *rankmod)
|
2015-06-18 13:11:14 +02:00
|
|
|
{
|
2016-09-26 18:55:38 +02:00
|
|
|
int n;
|
2016-06-17 00:54:13 +02:00
|
|
|
int nmax;
|
2015-06-18 13:11:14 +02:00
|
|
|
|
2016-09-26 18:55:38 +02:00
|
|
|
/*
|
|
|
|
* Current policy is that only networks successfully connected
|
|
|
|
* to at least once are autoconnectable. Known Networks that
|
|
|
|
* we have never connected to are not.
|
|
|
|
*/
|
2019-08-21 21:06:10 +02:00
|
|
|
if (!network->info || !network->info->connected_time)
|
2016-09-26 18:55:38 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-09 08:06:10 +02:00
|
|
|
n = known_network_offset(network->info);
|
|
|
|
if (n < 0)
|
2016-06-10 11:50:20 +02:00
|
|
|
return false;
|
2015-06-18 13:11:14 +02:00
|
|
|
|
2016-06-10 11:50:20 +02:00
|
|
|
nmax = L_ARRAY_SIZE(rankmod_table);
|
2015-06-18 13:11:14 +02:00
|
|
|
|
2016-06-10 11:50:20 +02:00
|
|
|
if (n >= nmax)
|
|
|
|
n = nmax - 1;
|
2015-06-18 13:11:14 +02:00
|
|
|
|
2016-06-10 11:50:20 +02:00
|
|
|
*rankmod = rankmod_table[n];
|
2015-06-18 13:11:14 +02:00
|
|
|
|
2016-06-10 11:50:20 +02:00
|
|
|
return true;
|
2015-06-18 13:11:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:45:37 +02:00
|
|
|
struct network *network_create(struct station *station, const char *ssid,
|
2016-05-16 04:11:01 +02:00
|
|
|
enum security security)
|
|
|
|
{
|
|
|
|
struct network *network;
|
|
|
|
|
|
|
|
network = l_new(struct network, 1);
|
2018-09-04 22:45:37 +02:00
|
|
|
network->station = station;
|
2019-08-09 00:48:05 +02:00
|
|
|
strcpy(network->ssid, ssid);
|
2019-08-09 07:26:09 +02:00
|
|
|
network->security = security;
|
2019-08-09 09:21:54 +02:00
|
|
|
|
|
|
|
network->info = known_networks_find(ssid, security);
|
|
|
|
if (network->info)
|
|
|
|
network->info->seen_count++;
|
2016-05-16 04:11:01 +02:00
|
|
|
|
|
|
|
network->bss_list = l_queue_new();
|
2019-03-01 19:55:03 +01:00
|
|
|
network->blacklist = l_queue_new();
|
2016-05-16 04:11:01 +02:00
|
|
|
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
|
2016-05-28 05:27:12 +02:00
|
|
|
const char *network_get_ssid(const struct network *network)
|
2016-05-12 05:00:58 +02:00
|
|
|
{
|
2019-08-09 00:48:05 +02:00
|
|
|
return network->ssid;
|
2016-05-12 05:00:58 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 05:27:12 +02:00
|
|
|
const char *network_get_path(const struct network *network)
|
2016-05-12 05:07:38 +02:00
|
|
|
{
|
|
|
|
return network->object_path;
|
|
|
|
}
|
|
|
|
|
2016-05-28 05:27:12 +02:00
|
|
|
enum security network_get_security(const struct network *network)
|
2016-05-12 05:10:18 +02:00
|
|
|
{
|
2019-08-09 07:26:09 +02:00
|
|
|
return network->security;
|
2016-05-12 05:10:18 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
const uint8_t *network_get_psk(struct network *network)
|
2016-05-16 21:22:24 +02:00
|
|
|
{
|
2019-09-17 22:23:15 +02:00
|
|
|
if (network->psk)
|
|
|
|
return network->psk;
|
|
|
|
|
|
|
|
network->psk = l_malloc(32);
|
|
|
|
|
2019-10-18 00:33:56 +02:00
|
|
|
if (crypto_psk_from_passphrase(network->passphrase,
|
2019-09-17 22:23:15 +02:00
|
|
|
(unsigned char *)network->ssid,
|
2019-10-18 00:33:56 +02:00
|
|
|
strlen(network->ssid),
|
|
|
|
network->psk) < 0) {
|
|
|
|
l_free(network->psk);
|
|
|
|
network->psk = NULL;
|
|
|
|
}
|
2019-09-17 22:23:15 +02:00
|
|
|
|
2018-12-03 15:40:44 +01:00
|
|
|
return network->psk;
|
2016-05-16 21:22:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:29:06 +02:00
|
|
|
const char *network_get_passphrase(const struct network *network)
|
|
|
|
{
|
|
|
|
return network->passphrase;
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:23:15 +02:00
|
|
|
bool network_set_passphrase(struct network *network, const char *passphrase)
|
|
|
|
{
|
|
|
|
if (network_get_security(network) != SECURITY_PSK)
|
|
|
|
return false;
|
|
|
|
|
2019-09-18 18:02:58 +02:00
|
|
|
if (!crypto_passphrase_is_valid(passphrase))
|
|
|
|
return false;
|
|
|
|
|
2019-09-17 22:23:15 +02:00
|
|
|
if (!network_settings_load(network))
|
|
|
|
network->settings = l_settings_new();
|
|
|
|
|
|
|
|
network_reset_passphrase(network);
|
|
|
|
network->passphrase = l_strdup(passphrase);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:03:34 +02:00
|
|
|
struct l_queue *network_get_secrets(const struct network *network)
|
|
|
|
{
|
|
|
|
return network->secrets;
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:20:09 +02:00
|
|
|
bool network_set_psk(struct network *network, const uint8_t *psk)
|
|
|
|
{
|
2019-08-09 07:26:09 +02:00
|
|
|
if (network_get_security(network) != SECURITY_PSK)
|
2016-09-21 23:20:09 +02:00
|
|
|
return false;
|
|
|
|
|
2018-06-22 03:08:59 +02:00
|
|
|
if (!network_settings_load(network))
|
2019-08-28 20:02:21 +02:00
|
|
|
network->settings = l_settings_new();
|
2018-06-22 03:08:59 +02:00
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
network_reset_psk(network);
|
2016-09-21 23:20:09 +02:00
|
|
|
network->psk = l_memdup(psk, 32);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-09 19:55:22 +02:00
|
|
|
int network_get_signal_strength(const struct network *network)
|
|
|
|
{
|
|
|
|
struct scan_bss *best_bss = l_queue_peek_head(network->bss_list);
|
|
|
|
|
|
|
|
return best_bss->signal_strength;
|
|
|
|
}
|
|
|
|
|
2016-05-28 05:27:12 +02:00
|
|
|
struct l_settings *network_get_settings(const struct network *network)
|
2016-05-16 21:36:32 +02:00
|
|
|
{
|
|
|
|
return network->settings;
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:29:23 +02:00
|
|
|
static bool network_set_8021x_secrets(struct network *network)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
if (!network->settings)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(network->secrets); entry;
|
|
|
|
entry = entry->next) {
|
|
|
|
struct eap_secret_info *secret = entry->data;
|
|
|
|
char *setting;
|
|
|
|
|
|
|
|
switch (secret->type) {
|
|
|
|
case EAP_SECRET_LOCAL_PKEY_PASSPHRASE:
|
|
|
|
case EAP_SECRET_REMOTE_PASSWORD:
|
|
|
|
if (!l_settings_set_string(network->settings,
|
|
|
|
"Security", secret->id,
|
|
|
|
secret->value))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EAP_SECRET_REMOTE_USER_PASSWORD:
|
|
|
|
if (!l_settings_set_string(network->settings,
|
2018-06-14 03:45:23 +02:00
|
|
|
"Security", secret->id,
|
2018-04-26 11:29:23 +02:00
|
|
|
secret->value))
|
|
|
|
return false;
|
|
|
|
|
2018-06-14 03:45:23 +02:00
|
|
|
if (secret->id2)
|
|
|
|
setting = secret->id2;
|
|
|
|
else {
|
|
|
|
setting = alloca(strlen(secret->id) + 10);
|
|
|
|
sprintf(setting, "%s-Password", secret->id);
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:29:23 +02:00
|
|
|
if (!l_settings_set_string(network->settings,
|
|
|
|
"Security", setting,
|
|
|
|
secret->value + 1 +
|
|
|
|
strlen(secret->value)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
static int network_load_psk(struct network *network, bool need_passphrase)
|
2018-06-15 01:08:54 +02:00
|
|
|
{
|
2019-08-09 00:48:05 +02:00
|
|
|
const char *ssid = network_get_ssid(network);
|
2019-08-09 07:26:09 +02:00
|
|
|
enum security security = network_get_security(network);
|
2020-09-16 11:17:54 +02:00
|
|
|
size_t psk_len;
|
|
|
|
uint8_t *psk = l_settings_get_bytes(network->settings, "Security",
|
|
|
|
"PreSharedKey", &psk_len);
|
2018-11-05 11:56:34 +01:00
|
|
|
char *passphrase = l_settings_get_string(network->settings,
|
|
|
|
"Security", "Passphrase");
|
2018-12-03 15:40:44 +01:00
|
|
|
int r;
|
2018-06-15 01:08:54 +02:00
|
|
|
|
2018-12-03 15:40:44 +01:00
|
|
|
/* PSK can be generated from the passphrase but not the other way */
|
2020-09-16 11:17:54 +02:00
|
|
|
if ((!psk || need_passphrase) && !passphrase) {
|
|
|
|
l_free(psk);
|
2018-06-15 01:08:54 +02:00
|
|
|
return -ENOKEY;
|
2020-09-16 11:17:54 +02:00
|
|
|
}
|
2018-06-15 01:08:54 +02:00
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
network_reset_passphrase(network);
|
|
|
|
network_reset_psk(network);
|
2018-11-05 11:56:34 +01:00
|
|
|
network->passphrase = passphrase;
|
2018-12-03 15:40:44 +01:00
|
|
|
|
|
|
|
if (psk) {
|
|
|
|
char *path;
|
|
|
|
|
2020-09-16 11:17:54 +02:00
|
|
|
if (psk_len == 32) {
|
|
|
|
network->psk = psk;
|
2018-12-03 15:40:44 +01:00
|
|
|
return 0;
|
2020-09-16 11:17:54 +02:00
|
|
|
}
|
2019-03-22 19:44:17 +01:00
|
|
|
|
2021-02-08 23:09:11 +01:00
|
|
|
l_free(psk);
|
|
|
|
|
2019-08-09 07:26:09 +02:00
|
|
|
path = storage_get_network_file_path(security, ssid);
|
2018-12-03 15:40:44 +01:00
|
|
|
l_error("%s: invalid PreSharedKey format", path);
|
|
|
|
l_free(path);
|
|
|
|
|
|
|
|
if (!passphrase)
|
2019-03-22 19:44:17 +01:00
|
|
|
return -EINVAL;
|
2018-12-03 15:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
network->psk = l_malloc(32);
|
2019-08-09 00:48:05 +02:00
|
|
|
r = crypto_psk_from_passphrase(passphrase, (uint8_t *) ssid,
|
|
|
|
strlen(ssid), network->psk);
|
2018-12-03 15:40:44 +01:00
|
|
|
if (!r) {
|
|
|
|
network->update_psk = true;
|
2018-11-05 11:56:34 +01:00
|
|
|
return 0;
|
2018-12-03 15:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r == -ERANGE || r == -EINVAL)
|
|
|
|
l_error("PSK generation failed: invalid passphrase format");
|
|
|
|
else
|
|
|
|
l_error("PSK generation failed: %s. "
|
|
|
|
"Ensure Crypto Engine is properly configured",
|
|
|
|
strerror(-r));
|
2018-06-15 01:08:54 +02:00
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
network_reset_passphrase(network);
|
|
|
|
network_reset_psk(network);
|
2018-11-05 11:56:34 +01:00
|
|
|
return -EINVAL;
|
2018-06-15 01:08:54 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 04:04:02 +02:00
|
|
|
void network_sync_psk(struct network *network)
|
|
|
|
{
|
2018-08-10 03:00:46 +02:00
|
|
|
struct l_settings *fs_settings;
|
2019-08-09 00:48:05 +02:00
|
|
|
const char *ssid = network_get_ssid(network);
|
2016-05-16 04:04:02 +02:00
|
|
|
|
|
|
|
if (!network->update_psk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
network->update_psk = false;
|
2018-08-10 03:00:46 +02:00
|
|
|
|
2019-08-09 00:48:05 +02:00
|
|
|
fs_settings = storage_network_open(SECURITY_PSK, ssid);
|
2018-11-05 11:56:34 +01:00
|
|
|
|
|
|
|
if (network->psk) {
|
2020-09-16 11:17:54 +02:00
|
|
|
l_settings_set_bytes(network->settings, "Security",
|
|
|
|
"PreSharedKey",
|
|
|
|
network->psk, 32);
|
2018-08-10 03:00:46 +02:00
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
if (fs_settings)
|
2020-09-16 11:17:54 +02:00
|
|
|
l_settings_set_bytes(fs_settings, "Security",
|
|
|
|
"PreSharedKey",
|
|
|
|
network->psk, 32);
|
2018-11-05 11:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (network->passphrase) {
|
2018-08-14 01:25:46 +02:00
|
|
|
l_settings_set_string(network->settings, "Security",
|
|
|
|
"Passphrase",
|
|
|
|
network->passphrase);
|
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
if (fs_settings)
|
2018-08-14 01:25:46 +02:00
|
|
|
l_settings_set_string(fs_settings, "Security",
|
|
|
|
"Passphrase",
|
|
|
|
network->passphrase);
|
2018-11-05 11:56:34 +01:00
|
|
|
}
|
2018-08-14 01:25:46 +02:00
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
if (fs_settings) {
|
2019-08-09 00:48:05 +02:00
|
|
|
storage_network_sync(SECURITY_PSK, ssid, fs_settings);
|
2018-08-10 18:42:31 +02:00
|
|
|
l_settings_free(fs_settings);
|
|
|
|
} else
|
2019-08-09 00:48:05 +02:00
|
|
|
storage_network_sync(SECURITY_PSK, ssid, network->settings);
|
2018-11-05 11:56:34 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 20:11:54 +02:00
|
|
|
const struct network_info *network_get_info(const struct network *network)
|
|
|
|
{
|
|
|
|
return network->info;
|
|
|
|
}
|
|
|
|
|
2019-09-12 18:53:35 +02:00
|
|
|
static void add_known_frequency(void *data, void *user_data)
|
|
|
|
{
|
|
|
|
struct scan_bss *bss = data;
|
|
|
|
struct network_info *info = user_data;
|
|
|
|
|
|
|
|
known_network_add_frequency(info, bss->frequency);
|
|
|
|
}
|
|
|
|
|
2019-08-19 20:11:54 +02:00
|
|
|
void network_set_info(struct network *network, struct network_info *info)
|
|
|
|
{
|
2019-08-19 23:35:13 +02:00
|
|
|
if (info) {
|
|
|
|
network->info = info;
|
|
|
|
network->info->seen_count++;
|
2019-09-12 18:53:35 +02:00
|
|
|
|
|
|
|
l_queue_foreach(network->bss_list, add_known_frequency, info);
|
2019-08-19 23:35:13 +02:00
|
|
|
} else {
|
|
|
|
network->info->seen_count--;
|
|
|
|
network->info = NULL;
|
|
|
|
}
|
2019-08-19 20:11:54 +02:00
|
|
|
|
|
|
|
l_dbus_property_changed(dbus_get_bus(), network_get_path(network),
|
|
|
|
IWD_NETWORK_INTERFACE, "KnownNetwork");
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:29:13 +01:00
|
|
|
static inline bool __bss_is_sae(const struct scan_bss *bss,
|
|
|
|
const struct ie_rsn_info *rsn)
|
|
|
|
{
|
|
|
|
if (rsn->akm_suites & IE_RSN_AKM_SUITE_SAE_SHA256)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bss_is_sae(const struct scan_bss *bss)
|
2018-11-05 11:56:34 +01:00
|
|
|
{
|
|
|
|
struct ie_rsn_info rsn;
|
2018-08-10 18:42:31 +02:00
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
memset(&rsn, 0, sizeof(rsn));
|
|
|
|
scan_bss_get_rsn_info(bss, &rsn);
|
|
|
|
|
2018-11-15 18:29:13 +01:00
|
|
|
return __bss_is_sae(bss, &rsn);
|
2016-05-16 04:04:02 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 21:24:21 +02:00
|
|
|
int network_autoconnect(struct network *network, struct scan_bss *bss)
|
|
|
|
{
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
|
|
|
struct wiphy *wiphy = station_get_wiphy(station);
|
2018-11-15 17:58:35 +01:00
|
|
|
enum security security = network_get_security(network);
|
2018-11-15 18:37:20 +01:00
|
|
|
struct ie_rsn_info rsn;
|
2018-06-15 01:08:54 +02:00
|
|
|
bool is_rsn;
|
|
|
|
int ret;
|
2016-05-16 21:24:21 +02:00
|
|
|
|
2020-06-12 21:14:12 +02:00
|
|
|
/* already waiting for an agent request, connect in progress */
|
|
|
|
if (network->agent_request)
|
|
|
|
return -EALREADY;
|
|
|
|
|
2018-11-15 17:58:35 +01:00
|
|
|
switch (security) {
|
2016-05-16 21:24:21 +02:00
|
|
|
case SECURITY_NONE:
|
2018-06-15 01:08:54 +02:00
|
|
|
is_rsn = false;
|
2016-05-16 21:24:21 +02:00
|
|
|
break;
|
|
|
|
case SECURITY_PSK:
|
2018-11-05 11:56:34 +01:00
|
|
|
if (network->ask_passphrase)
|
2018-06-15 01:08:54 +02:00
|
|
|
return -ENOKEY;
|
|
|
|
|
|
|
|
/* Fall through */
|
|
|
|
case SECURITY_8021X:
|
|
|
|
is_rsn = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:25:46 +02:00
|
|
|
if (!network_settings_load(network))
|
|
|
|
return -ENOKEY;
|
|
|
|
|
2018-11-15 18:31:39 +01:00
|
|
|
ret = -EPERM;
|
2019-04-11 00:58:21 +02:00
|
|
|
if (!network->info->is_autoconnectable)
|
2018-11-15 18:31:39 +01:00
|
|
|
goto close_settings;
|
|
|
|
|
2018-11-15 18:37:20 +01:00
|
|
|
if (!is_rsn)
|
|
|
|
goto done;
|
2016-05-16 21:24:21 +02:00
|
|
|
|
2018-11-15 18:37:20 +01:00
|
|
|
memset(&rsn, 0, sizeof(rsn));
|
|
|
|
scan_bss_get_rsn_info(bss, &rsn);
|
2016-05-16 21:24:21 +02:00
|
|
|
|
2018-11-15 18:37:20 +01:00
|
|
|
if (!wiphy_select_cipher(wiphy, rsn.pairwise_ciphers) ||
|
|
|
|
!wiphy_select_cipher(wiphy, rsn.group_cipher)) {
|
2020-01-21 07:21:38 +01:00
|
|
|
l_debug("Cipher mismatch");
|
2018-11-15 18:37:20 +01:00
|
|
|
ret = -ENETUNREACH;
|
|
|
|
goto close_settings;
|
2018-08-14 01:25:46 +02:00
|
|
|
}
|
2016-05-16 21:24:21 +02:00
|
|
|
|
2018-11-15 18:37:20 +01:00
|
|
|
if (security == SECURITY_PSK) {
|
|
|
|
ret = network_load_psk(network, __bss_is_sae(bss, &rsn));
|
|
|
|
if (ret < 0)
|
|
|
|
goto close_settings;
|
|
|
|
} else if (security == SECURITY_8021X) {
|
2018-04-18 07:03:34 +02:00
|
|
|
struct l_queue *missing_secrets = NULL;
|
|
|
|
|
2018-06-15 01:08:54 +02:00
|
|
|
ret = eap_check_settings(network->settings, network->secrets,
|
|
|
|
"EAP-", true, &missing_secrets);
|
|
|
|
if (ret < 0)
|
|
|
|
goto close_settings;
|
2017-10-31 23:02:59 +01:00
|
|
|
|
2018-06-15 01:08:54 +02:00
|
|
|
ret = -ENOKEY;
|
|
|
|
if (!l_queue_isempty(missing_secrets)) {
|
2018-04-18 07:03:34 +02:00
|
|
|
l_queue_destroy(missing_secrets, eap_secret_info_free);
|
2018-06-15 01:08:54 +02:00
|
|
|
goto close_settings;
|
2018-04-18 07:03:34 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 01:08:54 +02:00
|
|
|
if (!network_set_8021x_secrets(network))
|
|
|
|
goto close_settings;
|
2018-01-23 20:42:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 18:37:20 +01:00
|
|
|
done:
|
2018-09-05 06:26:54 +02:00
|
|
|
return __station_connect_network(station, network, bss);
|
2018-06-15 01:08:54 +02:00
|
|
|
|
|
|
|
close_settings:
|
|
|
|
network_settings_close(network);
|
|
|
|
return ret;
|
2016-05-16 21:24:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 22:21:39 +02:00
|
|
|
void network_connect_failed(struct network *network, bool in_handshake)
|
2016-05-16 22:43:32 +02:00
|
|
|
{
|
|
|
|
/*
|
2020-06-12 22:21:39 +02:00
|
|
|
* Connection failed during the handshake phase. If PSK try asking
|
|
|
|
* for the passphrase once more
|
2016-05-16 22:43:32 +02:00
|
|
|
*/
|
2020-06-12 22:21:39 +02:00
|
|
|
if (network_get_security(network) == SECURITY_PSK && in_handshake) {
|
2016-05-16 22:43:32 +02:00
|
|
|
network->update_psk = false;
|
2018-11-05 11:56:34 +01:00
|
|
|
network->ask_passphrase = true;
|
2016-05-16 22:43:32 +02:00
|
|
|
}
|
2018-04-18 07:03:34 +02:00
|
|
|
|
|
|
|
l_queue_destroy(network->secrets, eap_secret_info_free);
|
|
|
|
network->secrets = NULL;
|
2019-03-01 19:55:03 +01:00
|
|
|
|
|
|
|
l_queue_clear(network->blacklist, NULL);
|
2016-05-16 22:43:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
static bool hotspot_info_matches(struct network *network,
|
|
|
|
const struct network_info *info)
|
2019-08-19 20:11:57 +02:00
|
|
|
{
|
|
|
|
struct scan_bss *bss;
|
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
if (!network->is_hs20 || !info->is_hotspot)
|
2019-08-19 20:11:57 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
bss = network_bss_select(network, true);
|
|
|
|
|
|
|
|
if (network_info_match_hessid(info, bss->hessid))
|
|
|
|
return true;
|
2019-08-19 20:11:57 +02:00
|
|
|
|
|
|
|
if (network_info_match_roaming_consortium(info, bss->rc_ie,
|
2019-09-06 20:11:01 +02:00
|
|
|
bss->rc_ie[1] + 2,
|
|
|
|
NULL))
|
2019-08-19 23:35:13 +02:00
|
|
|
return true;
|
2019-08-19 20:11:57 +02:00
|
|
|
|
|
|
|
return false;
|
2019-08-19 23:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool match_hotspot_network(const struct network_info *info,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
|
|
|
|
|
|
|
if (!hotspot_info_matches(network, info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
network_set_info(network, (struct network_info *) info);
|
2019-08-19 20:11:57 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:06:41 +02:00
|
|
|
bool network_bss_add(struct network *network, struct scan_bss *bss)
|
|
|
|
{
|
2019-04-11 21:14:25 +02:00
|
|
|
if (!l_queue_insert(network->bss_list, bss, scan_bss_rank_compare,
|
|
|
|
NULL))
|
|
|
|
return false;
|
|
|
|
|
2019-08-09 18:40:07 +02:00
|
|
|
if (network->info)
|
|
|
|
known_network_add_frequency(network->info, bss->frequency);
|
2019-04-11 21:14:25 +02:00
|
|
|
|
2019-08-19 20:11:57 +02:00
|
|
|
/* Done if BSS is not HS20 or we already have network_info set */
|
|
|
|
if (!bss->hs20_capable)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
network->is_hs20 = true;
|
2019-06-26 19:42:50 +02:00
|
|
|
|
2019-08-19 20:11:57 +02:00
|
|
|
if (network->info)
|
|
|
|
return true;
|
2019-07-12 19:30:27 +02:00
|
|
|
|
2019-08-19 20:11:57 +02:00
|
|
|
/* Set the network_info to a matching hotspot entry, if found */
|
|
|
|
known_networks_foreach(match_hotspot_network, network);
|
2019-07-19 20:18:52 +02:00
|
|
|
|
2019-04-11 21:14:25 +02:00
|
|
|
return true;
|
2016-05-16 23:06:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 23:13:36 +02:00
|
|
|
bool network_bss_list_isempty(struct network *network)
|
|
|
|
{
|
|
|
|
return l_queue_isempty(network->bss_list);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:13:52 +02:00
|
|
|
void network_bss_list_clear(struct network *network)
|
|
|
|
{
|
|
|
|
l_queue_destroy(network->bss_list, NULL);
|
|
|
|
network->bss_list = l_queue_new();
|
|
|
|
}
|
|
|
|
|
2021-02-02 05:43:34 +01:00
|
|
|
struct scan_bss *network_bss_list_pop(struct network *network)
|
|
|
|
{
|
|
|
|
return l_queue_pop_head(network->bss_list);
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:26:41 +02:00
|
|
|
struct scan_bss *network_bss_find_by_addr(struct network *network,
|
|
|
|
const uint8_t *addr)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *bss_entry;
|
|
|
|
|
|
|
|
for (bss_entry = l_queue_get_entries(network->bss_list); bss_entry;
|
|
|
|
bss_entry = bss_entry->next) {
|
|
|
|
struct scan_bss *bss = bss_entry->data;
|
|
|
|
|
|
|
|
if (!memcmp(bss->addr, addr, sizeof(bss->addr)))
|
|
|
|
return bss;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-01 19:55:03 +01:00
|
|
|
static bool match_bss(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
struct scan_bss *network_bss_select(struct network *network,
|
|
|
|
bool fallback_to_blacklist)
|
2016-05-16 19:37:48 +02:00
|
|
|
{
|
|
|
|
struct l_queue *bss_list = network->bss_list;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct wiphy *wiphy = station_get_wiphy(network->station);
|
2016-05-16 19:37:48 +02:00
|
|
|
const struct l_queue_entry *bss_entry;
|
2019-01-31 00:13:57 +01:00
|
|
|
struct scan_bss *candidate = NULL;
|
2016-05-16 19:37:48 +02:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
for (bss_entry = l_queue_get_entries(bss_list); bss_entry;
|
|
|
|
bss_entry = bss_entry->next) {
|
|
|
|
struct scan_bss *bss = bss_entry->data;
|
2016-11-02 22:23:22 +01:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
switch (network_get_security(network)) {
|
|
|
|
case SECURITY_PSK:
|
|
|
|
case SECURITY_8021X:
|
2019-01-29 21:36:12 +01:00
|
|
|
if (!wiphy_can_connect(wiphy, bss))
|
|
|
|
continue;
|
2019-01-31 00:13:57 +01:00
|
|
|
/* fall through */
|
|
|
|
case SECURITY_NONE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-29 21:36:12 +01:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
/*
|
|
|
|
* We only want to record the first (best) candidate. In case
|
|
|
|
* all our BSS's are blacklisted but we still want to connect
|
|
|
|
* we want to hold only this first candidate
|
|
|
|
*/
|
|
|
|
if (!candidate)
|
|
|
|
candidate = bss;
|
2019-01-29 21:36:12 +01:00
|
|
|
|
2019-03-01 19:55:03 +01:00
|
|
|
/* check if temporarily blacklisted */
|
|
|
|
if (l_queue_find(network->blacklist, match_bss, bss))
|
|
|
|
continue;
|
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
if (!blacklist_contains_bss(bss->addr))
|
2019-01-29 21:36:12 +01:00
|
|
|
return bss;
|
2019-01-31 00:13:57 +01:00
|
|
|
}
|
2016-05-16 19:37:48 +02:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
/*
|
|
|
|
* No BSS was found, but if we are falling back to blacklisted BSS's we
|
|
|
|
* can just use the first connectable candidate found above.
|
|
|
|
*/
|
|
|
|
if (fallback_to_blacklist)
|
|
|
|
return candidate;
|
2016-05-16 19:37:48 +02:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
return NULL;
|
2016-05-16 19:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void passphrase_callback(enum agent_result result,
|
|
|
|
const char *passphrase,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2019-08-09 00:48:05 +02:00
|
|
|
const char *ssid = network_get_ssid(network);
|
2016-05-16 19:37:48 +02:00
|
|
|
struct scan_bss *bss;
|
2018-12-03 15:40:44 +01:00
|
|
|
int r;
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
l_debug("result %d", result);
|
|
|
|
|
|
|
|
network->agent_request = 0;
|
|
|
|
|
2017-05-12 02:48:07 +02:00
|
|
|
/*
|
|
|
|
* agent will release its reference to message after invoking this
|
|
|
|
* callback. So if we want this message, we need to take a reference
|
|
|
|
* to it
|
|
|
|
*/
|
|
|
|
l_dbus_message_ref(message);
|
|
|
|
|
2016-05-16 19:37:48 +02:00
|
|
|
if (result != AGENT_RESULT_OK) {
|
|
|
|
dbus_pending_reply(&message, dbus_error_aborted(message));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
bss = network_bss_select(network, true);
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
/* Did all good BSSes go away while we waited */
|
|
|
|
if (!bss) {
|
|
|
|
dbus_pending_reply(&message, dbus_error_failed(message));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
network_reset_psk(network);
|
2018-12-03 15:40:44 +01:00
|
|
|
network->psk = l_malloc(32);
|
|
|
|
r = crypto_psk_from_passphrase(passphrase,
|
2019-08-09 00:48:05 +02:00
|
|
|
(uint8_t *) ssid, strlen(ssid),
|
2018-12-03 15:40:44 +01:00
|
|
|
network->psk);
|
|
|
|
if (r) {
|
|
|
|
struct l_dbus_message *error;
|
|
|
|
|
|
|
|
l_free(network->psk);
|
|
|
|
network->psk = NULL;
|
|
|
|
|
|
|
|
if (r == -ERANGE || r == -EINVAL)
|
|
|
|
error = dbus_error_invalid_format(message);
|
|
|
|
else {
|
|
|
|
l_error("PSK generation failed: %s. "
|
|
|
|
"Ensure Crypto Engine is properly configured",
|
|
|
|
strerror(-r));
|
|
|
|
error = dbus_error_failed(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_pending_reply(&message, error);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:44:17 +01:00
|
|
|
network_reset_passphrase(network);
|
2018-08-07 23:29:06 +02:00
|
|
|
network->passphrase = l_strdup(passphrase);
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to store the PSK in our permanent store. However, before
|
|
|
|
* we do that, make sure the PSK works. We write to the store only
|
|
|
|
* when we are connected
|
|
|
|
*/
|
|
|
|
network->update_psk = true;
|
|
|
|
|
2018-09-05 06:26:54 +02:00
|
|
|
station_connect_network(station, network, bss, message);
|
2017-05-12 02:48:07 +02:00
|
|
|
l_dbus_message_unref(message);
|
2016-05-16 19:37:48 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
network_settings_close(network);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *network_connect_psk(struct network *network,
|
|
|
|
struct scan_bss *bss,
|
|
|
|
struct l_dbus_message *message)
|
|
|
|
{
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2018-08-14 01:25:46 +02:00
|
|
|
/*
|
|
|
|
* A legacy psk file may only contain the PreSharedKey entry. For SAE
|
|
|
|
* networks the raw Passphrase is required. So in this case where
|
2018-11-05 11:56:34 +01:00
|
|
|
* the psk is found but no Passphrase, we ask the agent. The psk file
|
|
|
|
* will then be re-written to contain the raw passphrase.
|
2018-08-14 01:25:46 +02:00
|
|
|
*/
|
2018-11-05 11:56:34 +01:00
|
|
|
bool need_passphrase = bss_is_sae(bss);
|
|
|
|
|
|
|
|
if (!network_settings_load(network)) {
|
|
|
|
network->settings = l_settings_new();
|
|
|
|
network->ask_passphrase = true;
|
|
|
|
} else if (!network->ask_passphrase)
|
|
|
|
network->ask_passphrase =
|
|
|
|
network_load_psk(network, need_passphrase) < 0;
|
|
|
|
|
2019-08-16 01:44:24 +02:00
|
|
|
l_debug("ask_passphrase: %s",
|
|
|
|
network->ask_passphrase ? "true" : "false");
|
|
|
|
|
2018-11-05 11:56:34 +01:00
|
|
|
if (network->ask_passphrase) {
|
|
|
|
network->ask_passphrase = false;
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
network->agent_request =
|
|
|
|
agent_request_passphrase(network->object_path,
|
|
|
|
passphrase_callback,
|
2017-08-18 00:20:39 +02:00
|
|
|
message, network, NULL);
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
if (!network->agent_request)
|
|
|
|
return dbus_error_no_agent(message);
|
|
|
|
} else
|
2018-09-05 06:26:54 +02:00
|
|
|
station_connect_network(station, network, bss, message);
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:03:34 +02:00
|
|
|
struct eap_secret_request {
|
|
|
|
struct network *network;
|
|
|
|
struct eap_secret_info *secret;
|
|
|
|
struct l_queue *pending_secrets;
|
|
|
|
void (*callback)(enum agent_result result,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct eap_secret_request *req);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void eap_secret_request_free(void *data)
|
|
|
|
{
|
|
|
|
struct eap_secret_request *req = data;
|
|
|
|
|
|
|
|
eap_secret_info_free(req->secret);
|
|
|
|
l_queue_destroy(req->pending_secrets, eap_secret_info_free);
|
|
|
|
l_free(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eap_secret_info_match_local(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct eap_secret_info *info = a;
|
|
|
|
|
|
|
|
return info->type == EAP_SECRET_LOCAL_PKEY_PASSPHRASE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void eap_password_callback(enum agent_result result, const char *value,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct eap_secret_request *req = user_data;
|
|
|
|
|
|
|
|
req->network->agent_request = 0;
|
2020-03-06 20:16:28 +01:00
|
|
|
|
|
|
|
if (value) {
|
|
|
|
if (strlen(value) < IWD_MAX_PASSWORD_LEN)
|
|
|
|
req->secret->value = l_strdup(value);
|
|
|
|
else {
|
|
|
|
l_error("EAP password too long");
|
|
|
|
result = AGENT_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 07:03:34 +02:00
|
|
|
|
|
|
|
req->callback(result, message, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void eap_user_password_callback(enum agent_result result,
|
|
|
|
const char *user, const char *passwd,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct eap_secret_request *req = user_data;
|
|
|
|
|
|
|
|
req->network->agent_request = 0;
|
|
|
|
|
|
|
|
if (user && passwd) {
|
|
|
|
size_t len1 = strlen(user) + 1;
|
|
|
|
size_t len2 = strlen(passwd) + 1;
|
|
|
|
|
2020-03-06 20:16:28 +01:00
|
|
|
if (len2 > IWD_MAX_PASSWORD_LEN) {
|
|
|
|
l_error("EAP password too long");
|
|
|
|
result = AGENT_RESULT_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:03:34 +02:00
|
|
|
req->secret->value = l_malloc(len1 + len2);
|
|
|
|
memcpy(req->secret->value, user, len1);
|
|
|
|
memcpy(req->secret->value + len1, passwd, len2);
|
|
|
|
}
|
|
|
|
|
2020-03-06 20:16:28 +01:00
|
|
|
done:
|
2018-04-18 07:03:34 +02:00
|
|
|
req->callback(result, message, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eap_send_agent_req(struct network *network,
|
|
|
|
struct l_queue *pending_secrets,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *callback)
|
|
|
|
{
|
|
|
|
struct eap_secret_request *req;
|
|
|
|
struct eap_secret_info *info;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request the locally-verifiable data first, i.e.
|
|
|
|
* the private key encryption passphrases so that we don't bother
|
|
|
|
* asking for any other data if these passphrases turn out to
|
|
|
|
* be wrong.
|
|
|
|
*/
|
|
|
|
info = l_queue_remove_if(pending_secrets, eap_secret_info_match_local,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
info = l_queue_pop_head(pending_secrets);
|
|
|
|
|
|
|
|
req = l_new(struct eap_secret_request, 1);
|
|
|
|
req->network = network;
|
|
|
|
req->secret = info;
|
|
|
|
req->pending_secrets = pending_secrets;
|
|
|
|
req->callback = callback;
|
|
|
|
|
|
|
|
switch (info->type) {
|
|
|
|
case EAP_SECRET_LOCAL_PKEY_PASSPHRASE:
|
|
|
|
network->agent_request = agent_request_pkey_passphrase(
|
|
|
|
network->object_path,
|
|
|
|
eap_password_callback,
|
|
|
|
message, req,
|
|
|
|
eap_secret_request_free);
|
|
|
|
break;
|
|
|
|
case EAP_SECRET_REMOTE_PASSWORD:
|
|
|
|
network->agent_request = agent_request_user_password(
|
|
|
|
network->object_path,
|
|
|
|
info->parameter,
|
|
|
|
eap_password_callback,
|
|
|
|
message, req,
|
|
|
|
eap_secret_request_free);
|
|
|
|
break;
|
|
|
|
case EAP_SECRET_REMOTE_USER_PASSWORD:
|
|
|
|
network->agent_request = agent_request_user_name_password(
|
|
|
|
network->object_path,
|
|
|
|
eap_user_password_callback,
|
|
|
|
message, req,
|
|
|
|
eap_secret_request_free);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (network->agent_request)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
eap_secret_request_free(req);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *network_connect_8021x(struct network *network,
|
|
|
|
struct scan_bss *bss,
|
|
|
|
struct l_dbus_message *message);
|
|
|
|
|
|
|
|
static void eap_secret_done(enum agent_result result,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct eap_secret_request *req)
|
|
|
|
{
|
|
|
|
struct network *network = req->network;
|
|
|
|
struct eap_secret_info *secret = req->secret;
|
|
|
|
struct l_queue *pending = req->pending_secrets;
|
|
|
|
struct scan_bss *bss;
|
|
|
|
|
|
|
|
l_debug("result %d", result);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Agent will release its reference to message after invoking this
|
|
|
|
* callback. So if we want this message, we need to take a reference
|
|
|
|
* to it.
|
|
|
|
*/
|
|
|
|
l_dbus_message_ref(message);
|
|
|
|
|
|
|
|
if (result != AGENT_RESULT_OK) {
|
|
|
|
dbus_pending_reply(&message, dbus_error_aborted(message));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
bss = network_bss_select(network, true);
|
2018-04-18 07:03:34 +02:00
|
|
|
|
|
|
|
/* Did all good BSSes go away while we waited */
|
|
|
|
if (!bss) {
|
|
|
|
dbus_pending_reply(&message, dbus_error_failed(message));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!network->secrets)
|
|
|
|
network->secrets = l_queue_new();
|
|
|
|
|
|
|
|
l_queue_push_tail(network->secrets, secret);
|
|
|
|
|
|
|
|
req->secret = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have any other missing secrets in the queue, send the
|
|
|
|
* next request immediately unless we've just received a passphrase
|
|
|
|
* for a local private key. In that case we will first call
|
|
|
|
* network_connect_8021x to have it validate the new passphrase.
|
|
|
|
*/
|
|
|
|
if (secret->type == EAP_SECRET_LOCAL_PKEY_PASSPHRASE ||
|
|
|
|
l_queue_isempty(req->pending_secrets)) {
|
|
|
|
struct l_dbus_message *reply;
|
|
|
|
|
|
|
|
reply = network_connect_8021x(network, bss, message);
|
|
|
|
if (reply)
|
|
|
|
dbus_pending_reply(&message, reply);
|
|
|
|
else
|
|
|
|
l_dbus_message_unref(message);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->pending_secrets = NULL;
|
|
|
|
|
|
|
|
if (eap_send_agent_req(network, pending, message,
|
|
|
|
eap_secret_done)) {
|
|
|
|
l_dbus_message_unref(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_pending_reply(&message, dbus_error_no_agent(message));
|
|
|
|
err:
|
|
|
|
network_settings_close(network);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *network_connect_8021x(struct network *network,
|
|
|
|
struct scan_bss *bss,
|
|
|
|
struct l_dbus_message *message)
|
|
|
|
{
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2018-04-28 04:28:44 +02:00
|
|
|
int r;
|
2018-04-18 07:03:34 +02:00
|
|
|
struct l_queue *missing_secrets = NULL;
|
2018-04-26 11:29:23 +02:00
|
|
|
struct l_dbus_message *reply;
|
2018-04-18 07:03:34 +02:00
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
|
|
|
r = eap_check_settings(network->settings, network->secrets, "EAP-",
|
|
|
|
true, &missing_secrets);
|
2018-04-28 04:28:44 +02:00
|
|
|
if (r) {
|
|
|
|
if (r == -EUNATCH)
|
|
|
|
reply = dbus_error_not_available(message);
|
|
|
|
else if (r == -ENOTSUP)
|
|
|
|
reply = dbus_error_not_supported(message);
|
|
|
|
else if (r == -EACCES)
|
|
|
|
reply = dbus_error_failed(message);
|
|
|
|
else
|
|
|
|
reply = dbus_error_not_configured(message);
|
2018-04-18 07:03:34 +02:00
|
|
|
|
2018-04-26 11:29:23 +02:00
|
|
|
goto error;
|
2018-04-18 07:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l_debug("supplied %u secrets, %u more needed for EAP",
|
|
|
|
l_queue_length(network->secrets),
|
|
|
|
l_queue_length(missing_secrets));
|
|
|
|
|
|
|
|
if (l_queue_isempty(missing_secrets)) {
|
2018-04-26 11:29:23 +02:00
|
|
|
if (!network_set_8021x_secrets(network)) {
|
|
|
|
reply = dbus_error_failed(message);
|
|
|
|
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-09-05 06:26:54 +02:00
|
|
|
station_connect_network(station, network, bss, message);
|
2018-04-18 07:03:34 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eap_send_agent_req(network, missing_secrets, message,
|
|
|
|
eap_secret_done))
|
|
|
|
return NULL;
|
|
|
|
|
2018-04-26 11:29:23 +02:00
|
|
|
reply = dbus_error_no_agent(message);
|
|
|
|
|
|
|
|
error:
|
2018-04-18 07:03:34 +02:00
|
|
|
network_settings_close(network);
|
|
|
|
|
2018-04-26 11:29:23 +02:00
|
|
|
l_queue_destroy(network->secrets, eap_secret_info_free);
|
|
|
|
network->secrets = NULL;
|
|
|
|
|
|
|
|
return reply;
|
2018-04-18 07:03:34 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 19:37:48 +02:00
|
|
|
static struct l_dbus_message *network_connect(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2016-05-16 19:37:48 +02:00
|
|
|
struct scan_bss *bss;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
2019-07-24 03:12:58 +02:00
|
|
|
if (network == station_get_connected_network(station))
|
|
|
|
/*
|
|
|
|
* The requested network is already connected, return success.
|
|
|
|
*/
|
|
|
|
return l_dbus_message_new_method_return(message);
|
|
|
|
|
2020-10-08 10:49:09 +02:00
|
|
|
if (network->agent_request)
|
|
|
|
return dbus_error_busy(message);
|
|
|
|
|
2016-05-16 19:37:48 +02:00
|
|
|
/*
|
|
|
|
* Select the best BSS to use at this time. If we have to query the
|
|
|
|
* agent this may not be the final choice because BSS visibility can
|
|
|
|
* change while we wait for the agent.
|
|
|
|
*/
|
2019-01-31 00:13:57 +01:00
|
|
|
bss = network_bss_select(network, true);
|
2016-05-16 19:37:48 +02:00
|
|
|
|
|
|
|
/* None of the BSSes is compatible with our stack */
|
|
|
|
if (!bss)
|
|
|
|
return dbus_error_not_supported(message);
|
|
|
|
|
2016-06-17 00:54:13 +02:00
|
|
|
switch (network_get_security(network)) {
|
2016-05-16 19:37:48 +02:00
|
|
|
case SECURITY_PSK:
|
|
|
|
return network_connect_psk(network, bss, message);
|
|
|
|
case SECURITY_NONE:
|
2018-09-05 06:26:54 +02:00
|
|
|
station_connect_network(station, network, bss, message);
|
2016-05-16 19:37:48 +02:00
|
|
|
return NULL;
|
|
|
|
case SECURITY_8021X:
|
2020-06-12 21:14:12 +02:00
|
|
|
if (network->connect_after_anqp)
|
|
|
|
return dbus_error_busy(message);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is an ongoing ANQP request we must wait for that to
|
|
|
|
* finish. Save the message and wait for the ANQP watch to
|
|
|
|
* fire
|
|
|
|
*/
|
|
|
|
if (network->anqp_pending) {
|
|
|
|
network->connect_after_anqp =
|
|
|
|
l_dbus_message_ref(message);
|
|
|
|
l_debug("Pending ANQP request, delaying connect to %s",
|
|
|
|
network->ssid);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-31 23:02:59 +01:00
|
|
|
if (!network_settings_load(network))
|
|
|
|
return dbus_error_not_configured(message);
|
|
|
|
|
2018-04-18 07:03:34 +02:00
|
|
|
return network_connect_8021x(network, bss, message);
|
2016-05-16 19:37:48 +02:00
|
|
|
default:
|
|
|
|
return dbus_error_not_supported(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:22:42 +01:00
|
|
|
/*
|
|
|
|
* Returns an error message in case an error occurs. Otherwise this function
|
|
|
|
* returns NULL and takes a reference to message. Callers should unref
|
|
|
|
* their copy in this case
|
|
|
|
*/
|
|
|
|
struct l_dbus_message *network_connect_new_hidden_network(
|
|
|
|
struct network *network,
|
|
|
|
struct l_dbus_message *message)
|
2018-07-11 00:46:55 +02:00
|
|
|
{
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2018-07-11 00:46:55 +02:00
|
|
|
struct scan_bss *bss;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
2021-02-01 18:22:42 +01:00
|
|
|
if (network->agent_request)
|
|
|
|
return dbus_error_busy(message);
|
2020-10-08 10:49:09 +02:00
|
|
|
|
2018-07-22 14:15:18 +02:00
|
|
|
/*
|
2018-08-09 16:33:44 +02:00
|
|
|
* This is not a Known Network. If connection succeeds, either
|
|
|
|
* network_sync_psk or network_connected will save this network
|
|
|
|
* as hidden and trigger an update to the hidden networks count.
|
2018-07-22 14:15:18 +02:00
|
|
|
*/
|
2018-07-11 00:46:57 +02:00
|
|
|
|
2019-01-31 00:13:57 +01:00
|
|
|
bss = network_bss_select(network, true);
|
2021-02-01 18:22:42 +01:00
|
|
|
/* This should never happened for the hidden networks. */
|
|
|
|
if (!bss)
|
|
|
|
return dbus_error_not_supported(message);
|
2018-07-11 00:46:55 +02:00
|
|
|
|
2018-07-11 23:09:42 +02:00
|
|
|
network->settings = l_settings_new();
|
|
|
|
l_settings_set_bool(network->settings, "Settings", "Hidden", true);
|
|
|
|
|
2018-07-11 00:46:55 +02:00
|
|
|
switch (network_get_security(network)) {
|
|
|
|
case SECURITY_PSK:
|
2021-02-01 18:22:42 +01:00
|
|
|
return network_connect_psk(network, bss, message);
|
2018-07-11 00:46:55 +02:00
|
|
|
case SECURITY_NONE:
|
2021-02-01 18:22:42 +01:00
|
|
|
station_connect_network(station, network, bss, message);
|
|
|
|
return NULL;
|
2018-07-11 00:46:55 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:22:42 +01:00
|
|
|
return dbus_error_not_supported(message);
|
2018-07-11 00:46:55 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 19:55:03 +01:00
|
|
|
void network_blacklist_add(struct network *network, struct scan_bss *bss)
|
|
|
|
{
|
|
|
|
l_queue_push_head(network->blacklist, bss);
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:49:09 +02:00
|
|
|
const struct iovec *network_get_extra_ies(struct network *network,
|
|
|
|
size_t *num_elems)
|
|
|
|
{
|
|
|
|
struct scan_bss *bss = network_bss_select(network, false);
|
|
|
|
|
|
|
|
return network_info_get_extra_ies(network->info, bss, num_elems);
|
|
|
|
}
|
|
|
|
|
2016-05-16 19:37:48 +02:00
|
|
|
static bool network_property_get_name(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
|
|
|
|
2019-08-09 00:48:05 +02:00
|
|
|
l_dbus_message_builder_append_basic(builder, 's',
|
|
|
|
network_get_ssid(network));
|
2016-05-16 19:37:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool network_property_is_connected(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
2016-05-16 19:37:48 +02:00
|
|
|
bool connected;
|
|
|
|
|
2018-09-04 22:45:37 +02:00
|
|
|
connected = station_get_connected_network(station) == network;
|
2016-05-16 19:37:48 +02:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &connected);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:24:32 +01:00
|
|
|
static bool network_property_get_device(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
2018-09-04 22:45:37 +02:00
|
|
|
struct station *station = network->station;
|
|
|
|
struct netdev *netdev = station_get_netdev(station);
|
2016-11-02 15:24:32 +01:00
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o',
|
2018-09-04 22:45:37 +02:00
|
|
|
netdev_get_path(netdev));
|
2016-11-02 15:24:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:24:33 +01:00
|
|
|
static bool network_property_get_type(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
|
|
|
enum security security = network_get_security(network);
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's',
|
|
|
|
security_to_str(security));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:10:27 +02:00
|
|
|
static bool network_property_get_known_network(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = user_data;
|
|
|
|
|
2019-08-09 09:21:54 +02:00
|
|
|
if (!network->info)
|
2018-08-14 21:10:27 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o',
|
2019-08-15 22:15:13 +02:00
|
|
|
network_info_get_path(network->info));
|
2018-08-14 21:10:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 04:30:10 +02:00
|
|
|
bool network_register(struct network *network, const char *path)
|
|
|
|
{
|
|
|
|
if (!l_dbus_object_add_interface(dbus_get_bus(), path,
|
|
|
|
IWD_NETWORK_INTERFACE, network)) {
|
|
|
|
l_info("Unable to register %s interface",
|
|
|
|
IWD_NETWORK_INTERFACE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-21 18:51:55 +02:00
|
|
|
if (!l_dbus_object_add_interface(dbus_get_bus(), path,
|
|
|
|
L_DBUS_INTERFACE_PROPERTIES, network))
|
|
|
|
l_info("Unable to register %s interface",
|
|
|
|
L_DBUS_INTERFACE_PROPERTIES);
|
|
|
|
|
2018-11-01 22:37:11 +01:00
|
|
|
network->object_path = l_strdup(path);
|
2016-05-16 04:30:10 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:49:34 +02:00
|
|
|
static void network_unregister(struct network *network, int reason)
|
2016-05-16 04:37:12 +02:00
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
|
2016-06-10 11:49:34 +02:00
|
|
|
agent_request_cancel(network->agent_request, reason);
|
2016-05-16 04:37:12 +02:00
|
|
|
network_settings_close(network);
|
|
|
|
|
|
|
|
l_dbus_unregister_object(dbus, network->object_path);
|
|
|
|
|
|
|
|
l_free(network->object_path);
|
|
|
|
network->object_path = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:49:34 +02:00
|
|
|
void network_remove(struct network *network, int reason)
|
2016-05-16 04:37:12 +02:00
|
|
|
{
|
|
|
|
if (network->object_path)
|
2016-06-10 11:49:34 +02:00
|
|
|
network_unregister(network, reason);
|
2016-05-16 04:37:12 +02:00
|
|
|
|
2018-04-18 07:03:34 +02:00
|
|
|
l_queue_destroy(network->secrets, eap_secret_info_free);
|
|
|
|
network->secrets = NULL;
|
|
|
|
|
2019-08-09 09:21:54 +02:00
|
|
|
if (network->info)
|
|
|
|
network->info->seen_count -= 1;
|
2016-06-17 00:54:13 +02:00
|
|
|
|
2019-08-09 09:21:54 +02:00
|
|
|
l_queue_destroy(network->bss_list, NULL);
|
2019-03-01 19:55:03 +01:00
|
|
|
l_queue_destroy(network->blacklist, NULL);
|
|
|
|
|
2019-06-26 19:42:50 +02:00
|
|
|
if (network->nai_realms)
|
|
|
|
l_strv_free(network->nai_realms);
|
|
|
|
|
2019-07-12 19:30:27 +02:00
|
|
|
if (network->rc_ie)
|
|
|
|
l_free(network->rc_ie);
|
|
|
|
|
2016-05-16 04:37:12 +02:00
|
|
|
l_free(network);
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:50:20 +02:00
|
|
|
int network_rank_compare(const void *a, const void *b, void *user)
|
|
|
|
{
|
|
|
|
const struct network *new_network = a;
|
|
|
|
const struct network *network = b;
|
|
|
|
|
2020-08-14 15:40:33 +02:00
|
|
|
return (network->rank > new_network->rank) ? 1 : -1;
|
2016-06-10 11:50:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 21:42:35 +02:00
|
|
|
void network_rank_update(struct network *network, bool connected)
|
2016-06-10 11:50:20 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Theoretically there may be difference between the BSS selection
|
2016-09-21 21:00:59 +02:00
|
|
|
* here and in network_bss_select but those should be rare cases.
|
2016-06-10 11:50:20 +02:00
|
|
|
*/
|
|
|
|
struct scan_bss *best_bss = l_queue_peek_head(network->bss_list);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rank should separate networks into four groups that use
|
|
|
|
* non-overlapping ranges for:
|
|
|
|
* - current connected network,
|
|
|
|
* - other networks we've connected to before,
|
|
|
|
* - networks with preprovisioned settings file that we haven't
|
|
|
|
* used yet,
|
|
|
|
* - other networks.
|
|
|
|
*
|
|
|
|
* Within the 2nd group the last connection time is the main factor,
|
|
|
|
* for the other two groups it's the BSS rank - mainly signal strength.
|
|
|
|
*/
|
2019-08-09 09:21:54 +02:00
|
|
|
if (connected) {
|
|
|
|
network->rank = INT_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!network->info) { /* Not known, assign negative rank */
|
|
|
|
network->rank = (int) best_bss->rank - USHRT_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-21 21:06:10 +02:00
|
|
|
if (network->info->connected_time != 0) {
|
2019-08-09 08:06:10 +02:00
|
|
|
int n = known_network_offset(network->info);
|
2016-06-17 00:54:13 +02:00
|
|
|
|
2019-10-25 18:53:21 +02:00
|
|
|
L_WARN_ON(n < 0);
|
|
|
|
|
2016-06-17 00:54:13 +02:00
|
|
|
if (n >= (int) L_ARRAY_SIZE(rankmod_table))
|
|
|
|
n = L_ARRAY_SIZE(rankmod_table) - 1;
|
|
|
|
|
2019-08-09 09:21:54 +02:00
|
|
|
network->rank = rankmod_table[n] * best_bss->rank + USHRT_MAX;
|
|
|
|
} else
|
|
|
|
network->rank = best_bss->rank;
|
2016-06-10 11:50:20 +02:00
|
|
|
}
|
2016-06-17 00:54:13 +02:00
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
static void network_unset_hotspot(struct network *network, void *user_data)
|
|
|
|
{
|
|
|
|
struct network_info *info = user_data;
|
|
|
|
|
2019-08-28 18:14:29 +02:00
|
|
|
if (network->info != info)
|
2019-08-19 23:35:13 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
network_set_info(network, NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:04:58 +01:00
|
|
|
static void emit_known_network_removed(struct station *station, void *user_data)
|
2018-08-14 21:10:27 +02:00
|
|
|
{
|
|
|
|
struct network_info *info = user_data;
|
2021-02-02 05:43:34 +01:00
|
|
|
bool was_hidden = info->is_hidden;
|
2020-11-27 17:04:58 +01:00
|
|
|
struct network *connected_network;
|
2021-02-02 05:43:34 +01:00
|
|
|
struct network *network = NULL;
|
2018-08-14 21:10:27 +02:00
|
|
|
|
2020-11-27 17:04:58 +01:00
|
|
|
/* Clear network info, as this network is no longer known */
|
|
|
|
if (info->is_hotspot)
|
|
|
|
station_network_foreach(station, network_unset_hotspot, info);
|
|
|
|
else {
|
2019-08-19 23:35:13 +02:00
|
|
|
network = station_network_find(station, info->ssid, info->type);
|
|
|
|
if (!network)
|
|
|
|
return;
|
|
|
|
|
|
|
|
network_set_info(network, NULL);
|
|
|
|
}
|
2018-08-14 21:10:27 +02:00
|
|
|
|
2020-11-27 17:04:58 +01:00
|
|
|
connected_network = station_get_connected_network(station);
|
|
|
|
if (connected_network && connected_network->info == NULL)
|
|
|
|
station_disconnect(station);
|
2021-02-02 05:43:34 +01:00
|
|
|
|
|
|
|
if (network && was_hidden)
|
|
|
|
station_hide_network(station, network);
|
2019-08-19 23:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void network_update_hotspot(struct network *network, void *user_data)
|
|
|
|
{
|
|
|
|
struct network_info *info = user_data;
|
|
|
|
|
|
|
|
match_hotspot_network(info, network);
|
2018-08-14 21:10:27 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:38:28 +02:00
|
|
|
static void match_known_network(struct station *station, void *user_data)
|
2018-08-09 16:33:45 +02:00
|
|
|
{
|
2019-08-09 19:38:28 +02:00
|
|
|
struct network_info *info = user_data;
|
2019-08-19 23:35:13 +02:00
|
|
|
struct network *network;
|
|
|
|
|
|
|
|
if (!info->is_hotspot) {
|
|
|
|
network = station_network_find(station, info->ssid, info->type);
|
|
|
|
if (!network)
|
|
|
|
return;
|
2019-08-09 09:21:54 +02:00
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
network_set_info(network, info);
|
2019-08-09 19:38:28 +02:00
|
|
|
return;
|
2019-08-19 23:35:13 +02:00
|
|
|
}
|
2018-08-09 16:33:45 +02:00
|
|
|
|
2019-08-19 23:35:13 +02:00
|
|
|
/* This is a new hotspot network */
|
|
|
|
station_network_foreach(station, network_update_hotspot, info);
|
2018-08-09 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:03:27 +02:00
|
|
|
static void known_networks_changed(enum known_networks_event event,
|
|
|
|
const struct network_info *info,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case KNOWN_NETWORKS_EVENT_ADDED:
|
2019-08-09 19:38:28 +02:00
|
|
|
station_foreach(match_known_network, (void *) info);
|
2019-09-12 18:53:35 +02:00
|
|
|
|
|
|
|
/* Syncs frequencies of newly known network */
|
2019-09-13 19:27:26 +02:00
|
|
|
known_network_frequency_sync((struct network_info *)info);
|
2019-08-09 19:03:27 +02:00
|
|
|
break;
|
|
|
|
case KNOWN_NETWORKS_EVENT_REMOVED:
|
2020-11-27 17:04:58 +01:00
|
|
|
station_foreach(emit_known_network_removed, (void *) info);
|
2019-08-09 19:03:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:14:12 +02:00
|
|
|
static void anqp_watch_changed(enum station_anqp_state state,
|
|
|
|
struct network *network, void *user_data)
|
|
|
|
{
|
|
|
|
network->anqp_pending = state == STATION_ANQP_STARTED;
|
|
|
|
|
|
|
|
if (state == STATION_ANQP_FINISHED && network->connect_after_anqp) {
|
|
|
|
struct l_dbus_message *reply;
|
|
|
|
|
|
|
|
l_debug("ANQP complete, resuming connect to %s", network->ssid);
|
|
|
|
|
|
|
|
if (!network_settings_load(network)) {
|
|
|
|
reply = dbus_error_not_configured(
|
|
|
|
network->connect_after_anqp);
|
|
|
|
dbus_pending_reply(&network->connect_after_anqp, reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = network_connect_8021x(network,
|
|
|
|
network_bss_select(network, true),
|
|
|
|
network->connect_after_anqp);
|
|
|
|
|
|
|
|
if (reply)
|
|
|
|
l_dbus_send(dbus_get_bus(), reply);
|
|
|
|
|
|
|
|
l_dbus_message_unref(network->connect_after_anqp);
|
|
|
|
network->connect_after_anqp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:00:12 +02:00
|
|
|
static void setup_network_interface(struct l_dbus_interface *interface)
|
|
|
|
{
|
|
|
|
l_dbus_interface_method(interface, "Connect", 0,
|
|
|
|
network_connect,
|
|
|
|
"", "");
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "Name", 0, "s",
|
|
|
|
network_property_get_name, NULL);
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "Connected", 0, "b",
|
|
|
|
network_property_is_connected,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "Device", 0, "o",
|
|
|
|
network_property_get_device, NULL);
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "Type", 0, "s",
|
|
|
|
network_property_get_type, NULL);
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "KnownNetwork", 0, "o",
|
|
|
|
network_property_get_known_network, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int network_init(void)
|
|
|
|
{
|
|
|
|
if (!l_dbus_register_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE,
|
|
|
|
setup_network_interface, NULL, false))
|
|
|
|
l_error("Unable to register %s interface",
|
|
|
|
IWD_NETWORK_INTERFACE);
|
|
|
|
|
2019-08-09 19:03:27 +02:00
|
|
|
known_networks_watch =
|
|
|
|
known_networks_watch_add(known_networks_changed, NULL, NULL);
|
|
|
|
|
2020-06-12 21:14:12 +02:00
|
|
|
anqp_watch = station_add_anqp_watch(anqp_watch_changed, NULL, NULL);
|
|
|
|
|
2019-05-22 17:00:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void network_exit(void)
|
|
|
|
{
|
2019-08-09 19:03:27 +02:00
|
|
|
known_networks_watch_remove(known_networks_watch);
|
|
|
|
known_networks_watch = 0;
|
|
|
|
|
2020-06-12 21:14:12 +02:00
|
|
|
station_remove_anqp_watch(anqp_watch);
|
|
|
|
anqp_watch = 0;
|
|
|
|
|
2019-05-22 17:00:12 +02:00
|
|
|
l_dbus_unregister_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
IWD_MODULE(network, network_init, network_exit)
|
2019-08-09 19:03:27 +02:00
|
|
|
IWD_MODULE_DEPENDS(network, known_networks)
|