blob: a16350c65ddc810e02784c75ef0991d14044831b [file] [log] [blame]
/*
*
* Connection Manager
*
* Copyright (C) 2007-2014 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 <errno.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <gdbus.h>
#include <ctype.h>
#include <stdint.h>
#include <connman/storage.h>
#include <connman/setting.h>
#include <connman/agent.h>
#include "src/shared/util.h"
#include "connman.h"
#define CONNECT_TIMEOUT 120
#define VPN_AUTOCONNECT_TIMEOUT_DEFAULT 1
#define VPN_AUTOCONNECT_TIMEOUT_STEP 30
#define VPN_AUTOCONNECT_TIMEOUT_ATTEMPTS_THRESHOLD 270
static DBusConnection *connection = NULL;
static GList *service_list = NULL;
static GHashTable *service_hash = NULL;
static GSList *counter_list = NULL;
static unsigned int autoconnect_id = 0;
static unsigned int vpn_autoconnect_id = 0;
static struct connman_service *current_default = NULL;
static bool services_dirty = false;
static bool enable_online_to_ready_transition = false;
static unsigned int online_check_initial_interval = 0;
static unsigned int online_check_max_interval = 0;
struct connman_stats {
bool valid;
bool enabled;
struct connman_stats_data data_last;
struct connman_stats_data data;
GTimer *timer;
};
struct connman_stats_counter {
bool append_all;
struct connman_stats stats;
struct connman_stats stats_roaming;
};
struct connman_service {
int refcount;
char *identifier;
char *path;
enum connman_service_type type;
enum connman_service_security security;
enum connman_service_state state;
enum connman_service_state state_ipv4;
enum connman_service_state state_ipv6;
enum connman_service_error error;
enum connman_service_connect_reason connect_reason;
uint8_t strength;
bool favorite;
bool immutable;
bool hidden;
bool ignore;
bool autoconnect;
struct timeval modified;
unsigned int order;
char *name;
char *passphrase;
bool roaming;
struct connman_ipconfig *ipconfig_ipv4;
struct connman_ipconfig *ipconfig_ipv6;
struct connman_network *network;
struct connman_provider *provider;
char **nameservers;
char **nameservers_config;
char **nameservers_auto;
int nameservers_timeout;
char **domains;
bool mdns;
bool mdns_config;
char *hostname;
char *domainname;
char **timeservers;
char **timeservers_config;
/* 802.1x settings from the config files */
char *eap;
char *identity;
char *anonymous_identity;
char *agent_identity;
char *ca_cert_file;
char *subject_match;
char *altsubject_match;
char *domain_suffix_match;
char *domain_match;
char *client_cert_file;
char *private_key_file;
char *private_key_passphrase;
char *phase2;
DBusMessage *pending;
DBusMessage *provider_pending;
guint timeout;
struct connman_stats stats;
struct connman_stats stats_roaming;
GHashTable *counter_table;
enum connman_service_proxy_method proxy;
enum connman_service_proxy_method proxy_config;
char **proxies;
char **excludes;
char *pac;
bool wps;
bool wps_advertizing;
guint online_timeout;
unsigned int online_check_interval_ipv4;
unsigned int online_check_interval_ipv6;
bool do_split_routing;
bool new_service;
bool hidden_service;
char *config_file;
char *config_entry;
};
static bool allow_property_changed(struct connman_service *service);
static struct connman_ipconfig *create_ip4config(struct connman_service *service,
int index, enum connman_ipconfig_method method);
static struct connman_ipconfig *create_ip6config(struct connman_service *service,
int index);
static void dns_changed(struct connman_service *service);
static void vpn_auto_connect(void);
struct find_data {
const char *path;
struct connman_service *service;
};
static void compare_path(gpointer value, gpointer user_data)
{
struct connman_service *service = value;
struct find_data *data = user_data;
if (data->service)
return;
if (g_strcmp0(service->path, data->path) == 0)
data->service = service;
}
static struct connman_service *find_service(const char *path)
{
struct find_data data = { .path = path, .service = NULL };
DBG("path %s", path);
g_list_foreach(service_list, compare_path, &data);
return data.service;
}
static const char *reason2string(enum connman_service_connect_reason reason)
{
switch (reason) {
case CONNMAN_SERVICE_CONNECT_REASON_NONE:
return "none";
case CONNMAN_SERVICE_CONNECT_REASON_USER:
return "user";
case CONNMAN_SERVICE_CONNECT_REASON_AUTO:
return "auto";
case CONNMAN_SERVICE_CONNECT_REASON_SESSION:
return "session";
case CONNMAN_SERVICE_CONNECT_REASON_NATIVE:
return "native";
}
return "unknown";
}
const char *__connman_service_type2string(enum connman_service_type type)
{
switch (type) {
case CONNMAN_SERVICE_TYPE_UNKNOWN:
break;
case CONNMAN_SERVICE_TYPE_SYSTEM:
return "system";
case CONNMAN_SERVICE_TYPE_ETHERNET:
return "ethernet";
case CONNMAN_SERVICE_TYPE_WIFI:
return "wifi";
case CONNMAN_SERVICE_TYPE_BLUETOOTH:
return "bluetooth";
case CONNMAN_SERVICE_TYPE_CELLULAR:
return "cellular";
case CONNMAN_SERVICE_TYPE_GPS:
return "gps";
case CONNMAN_SERVICE_TYPE_VPN:
return "vpn";
case CONNMAN_SERVICE_TYPE_GADGET:
return "gadget";
case CONNMAN_SERVICE_TYPE_P2P:
return "p2p";
}
return NULL;
}
enum connman_service_type __connman_service_string2type(const char *str)
{
if (!str)
return CONNMAN_SERVICE_TYPE_UNKNOWN;
if (strcmp(str, "ethernet") == 0)
return CONNMAN_SERVICE_TYPE_ETHERNET;
if (strcmp(str, "gadget") == 0)
return CONNMAN_SERVICE_TYPE_GADGET;
if (strcmp(str, "wifi") == 0)
return CONNMAN_SERVICE_TYPE_WIFI;
if (strcmp(str, "cellular") == 0)
return CONNMAN_SERVICE_TYPE_CELLULAR;
if (strcmp(str, "bluetooth") == 0)
return CONNMAN_SERVICE_TYPE_BLUETOOTH;
if (strcmp(str, "vpn") == 0)
return CONNMAN_SERVICE_TYPE_VPN;
if (strcmp(str, "gps") == 0)
return CONNMAN_SERVICE_TYPE_GPS;
if (strcmp(str, "system") == 0)
return CONNMAN_SERVICE_TYPE_SYSTEM;
if (strcmp(str, "p2p") == 0)
return CONNMAN_SERVICE_TYPE_P2P;
return CONNMAN_SERVICE_TYPE_UNKNOWN;
}
enum connman_service_security __connman_service_string2security(const char *str)
{
if (!str)
return CONNMAN_SERVICE_SECURITY_UNKNOWN;
if (!strcmp(str, "psk"))
return CONNMAN_SERVICE_SECURITY_PSK;
if (!strcmp(str, "ieee8021x") || !strcmp(str, "8021x"))
return CONNMAN_SERVICE_SECURITY_8021X;
if (!strcmp(str, "none") || !strcmp(str, "open"))
return CONNMAN_SERVICE_SECURITY_NONE;
if (!strcmp(str, "wep"))
return CONNMAN_SERVICE_SECURITY_WEP;
return CONNMAN_SERVICE_SECURITY_UNKNOWN;
}
static const char *security2string(enum connman_service_security security)
{
switch (security) {
case CONNMAN_SERVICE_SECURITY_UNKNOWN:
break;
case CONNMAN_SERVICE_SECURITY_NONE:
return "none";
case CONNMAN_SERVICE_SECURITY_WEP:
return "wep";
case CONNMAN_SERVICE_SECURITY_PSK:
case CONNMAN_SERVICE_SECURITY_WPA:
case CONNMAN_SERVICE_SECURITY_RSN:
return "psk";
case CONNMAN_SERVICE_SECURITY_8021X:
return "ieee8021x";
}
return NULL;
}
static const char *state2string(enum connman_service_state state)
{
switch (state) {
case CONNMAN_SERVICE_STATE_UNKNOWN:
break;
case CONNMAN_SERVICE_STATE_IDLE:
return "idle";
case CONNMAN_SERVICE_STATE_ASSOCIATION:
return "association";
case CONNMAN_SERVICE_STATE_CONFIGURATION:
return "configuration";
case CONNMAN_SERVICE_STATE_READY:
return "ready";
case CONNMAN_SERVICE_STATE_ONLINE:
return "online";
case CONNMAN_SERVICE_STATE_DISCONNECT:
return "disconnect";
case CONNMAN_SERVICE_STATE_FAILURE:
return "failure";
}
return NULL;
}
static const char *error2string(enum connman_service_error error)
{
switch (error) {
case CONNMAN_SERVICE_ERROR_UNKNOWN:
break;
case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
return "out-of-range";
case CONNMAN_SERVICE_ERROR_PIN_MISSING:
return "pin-missing";
case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
return "dhcp-failed";
case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
return "connect-failed";
case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
return "login-failed";
case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
return "auth-failed";
case CONNMAN_SERVICE_ERROR_INVALID_KEY:
return "invalid-key";
case CONNMAN_SERVICE_ERROR_BLOCKED:
return "blocked";
}
return NULL;
}
static const char *proxymethod2string(enum connman_service_proxy_method method)
{
switch (method) {
case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
return "direct";
case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
return "manual";
case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
return "auto";
case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
break;
}
return NULL;
}
static enum connman_service_proxy_method string2proxymethod(const char *method)
{
if (g_strcmp0(method, "direct") == 0)
return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
else if (g_strcmp0(method, "auto") == 0)
return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
else if (g_strcmp0(method, "manual") == 0)
return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
else
return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
}
void __connman_service_split_routing_changed(struct connman_service *service)
{
dbus_bool_t split_routing;
if (!service->path)
return;
if (!allow_property_changed(service))
return;
split_routing = service->do_split_routing;
if (!connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "SplitRouting",
DBUS_TYPE_BOOLEAN, &split_routing))
connman_warn("cannot send SplitRouting property change on %s",
service->identifier);
}
void __connman_service_set_split_routing(struct connman_service *service,
bool value)
{
if (service->type != CONNMAN_SERVICE_TYPE_VPN)
return;
service->do_split_routing = value;
if (service->do_split_routing)
service->order = 0;
else
service->order = 10;
/*
* In order to make sure the value is propagated also when loading the
* VPN service signal the value regardless of the value change.
*/
__connman_service_split_routing_changed(service);
}
int __connman_service_load_modifiable(struct connman_service *service)
{
GKeyFile *keyfile;
GError *error = NULL;
gchar *str;
bool autoconnect;
DBG("service %p", service);
keyfile = connman_storage_load_service(service->identifier);
if (!keyfile)
return -EIO;
switch (service->type) {
case CONNMAN_SERVICE_TYPE_UNKNOWN:
case CONNMAN_SERVICE_TYPE_SYSTEM:
case CONNMAN_SERVICE_TYPE_GPS:
case CONNMAN_SERVICE_TYPE_P2P:
break;
case CONNMAN_SERVICE_TYPE_VPN:
__connman_service_set_split_routing(service,
g_key_file_get_boolean(keyfile,
service->identifier,
"SplitRouting", NULL));
/* fall through */
case CONNMAN_SERVICE_TYPE_WIFI:
case CONNMAN_SERVICE_TYPE_GADGET:
case CONNMAN_SERVICE_TYPE_BLUETOOTH:
case CONNMAN_SERVICE_TYPE_CELLULAR:
case CONNMAN_SERVICE_TYPE_ETHERNET:
autoconnect = g_key_file_get_boolean(keyfile,
service->identifier, "AutoConnect", &error);
if (!error)
service->autoconnect = autoconnect;
g_clear_error(&error);
break;
}
str = g_key_file_get_string(keyfile,
service->identifier, "Modified", NULL);
if (str) {
util_iso8601_to_timeval(str, &service->modified);
g_free(str);
}
g_key_file_free(keyfile);
return 0;
}
static int service_load(struct connman_service *service)
{
GKeyFile *keyfile;
GError *error = NULL;
gsize length;
gchar *str;
bool autoconnect;
unsigned int ssid_len;
int err = 0;
DBG("service %p", service);
keyfile = connman_storage_load_service(service->identifier);
if (!keyfile) {
service->new_service = true;
return -EIO;
} else
service->new_service = false;
switch (service->type) {
case CONNMAN_SERVICE_TYPE_UNKNOWN:
case CONNMAN_SERVICE_TYPE_SYSTEM:
case CONNMAN_SERVICE_TYPE_GPS:
case CONNMAN_SERVICE_TYPE_P2P:
break;
case CONNMAN_SERVICE_TYPE_VPN:
__connman_service_set_split_routing(service,
g_key_file_get_boolean(keyfile,
service->identifier,
"SplitRouting", NULL));
autoconnect = g_key_file_get_boolean(keyfile,
service->identifier, "AutoConnect", &error);
if (!error)
service->autoconnect = autoconnect;
g_clear_error(&error);
break;
case CONNMAN_SERVICE_TYPE_WIFI:
if (!service->name) {
gchar *name;
name = g_key_file_get_string(keyfile,
service->identifier, "Name", NULL);
if (name) {
g_free(service->name);
service->name = name;
}
if (service->network)
connman_network_set_name(service->network,
name);
}
if (service->network &&
!connman_network_get_blob(service->network,
"WiFi.SSID", &ssid_len)) {
gchar *hex_ssid;
hex_ssid = g_key_file_get_string(keyfile,
service->identifier,
"SSID", NULL);
if (hex_ssid) {
gchar *ssid;
unsigned int i, j = 0, hex;
size_t hex_ssid_len = strlen(hex_ssid);
ssid = g_try_malloc0(hex_ssid_len / 2);
if (!ssid) {
g_free(hex_ssid);
err = -ENOMEM;
goto done;
}
for (i = 0; i < hex_ssid_len; i += 2) {
sscanf(hex_ssid + i, "%02x", &hex);
ssid[j++] = hex;
}
connman_network_set_blob(service->network,
"WiFi.SSID", ssid, hex_ssid_len / 2);
g_free(ssid);
}
g_free(hex_ssid);
}
/* fall through */
case CONNMAN_SERVICE_TYPE_GADGET:
case CONNMAN_SERVICE_TYPE_BLUETOOTH:
case CONNMAN_SERVICE_TYPE_CELLULAR:
service->favorite = g_key_file_get_boolean(keyfile,
service->identifier, "Favorite", NULL);
/* fall through */
case CONNMAN_SERVICE_TYPE_ETHERNET:
autoconnect = g_key_file_get_boolean(keyfile,
service->identifier, "AutoConnect", &error);
if (!error)
service->autoconnect = autoconnect;
g_clear_error(&error);
break;
}
str = g_key_file_get_string(keyfile,
service->identifier, "Modified", NULL);
if (str) {
util_iso8601_to_timeval(str, &service->modified);
g_free(str);
}
str = g_key_file_get_string(keyfile,
service->identifier, "Passphrase", NULL);
if (str) {
char *dec = g_strcompress(str);
g_free(str);
g_free(service->passphrase);
service->passphrase = dec;
}
if (service->ipconfig_ipv4)
__connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
service->identifier, "IPv4.");
if (service->ipconfig_ipv6)
__connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
service->identifier, "IPv6.");
service->nameservers_config = g_key_file_get_string_list(keyfile,
service->identifier, "Nameservers", &length, NULL);
if (service->nameservers_config && length == 0) {
g_strfreev(service->nameservers_config);
service->nameservers_config = NULL;
}
service->timeservers_config = g_key_file_get_string_list(keyfile,
service->identifier, "Timeservers", &length, NULL);
if (service->timeservers_config && length == 0) {
g_strfreev(service->timeservers_config);
service->timeservers_config = NULL;
}
service->domains = g_key_file_get_string_list(keyfile,
service->identifier, "Domains", &length, NULL);
if (service->domains && length == 0) {
g_strfreev(service->domains);
service->domains = NULL;
}
str = g_key_file_get_string(keyfile,
service->identifier, "Proxy.Method", NULL);
if (str)
service->proxy_config = string2proxymethod(str);
g_free(str);
service->proxies = g_key_file_get_string_list(keyfile,
service->identifier, "Proxy.Servers", &length, NULL);
if (service->proxies && length == 0) {
g_strfreev(service->proxies);
service->proxies = NULL;
}
service->excludes = g_key_file_get_string_list(keyfile,
service->identifier, "Proxy.Excludes", &length, NULL);
if (service->excludes && length == 0) {
g_strfreev(service->excludes);
service->excludes = NULL;
}
str = g_key_file_get_string(keyfile,
service->identifier, "Proxy.URL", NULL);
if (str) {
g_free(service->pac);
service->pac = str;
}
service->mdns_config = g_key_file_get_boolean(keyfile,
service->identifier, "mDNS", NULL);
service->hidden_service = g_key_file_get_boolean(keyfile,
service->identifier, "Hidden", NULL);
done:
g_key_file_free(keyfile);
return err;
}
static int service_save(struct connman_service *service)
{
GKeyFile *keyfile;
gchar *str;
guint freq;
const char *cst_str = NULL;
int err = 0;
DBG("service %p new %d", service, service->new_service);
if (service->new_service)
return -ESRCH;
keyfile = g_key_file_new();
if (!keyfile)
return -EIO;
if (service->name)
g_key_file_set_string(keyfile, service->identifier,
"Name", service->name);
switch (service->type) {
case CONNMAN_SERVICE_TYPE_UNKNOWN:
case CONNMAN_SERVICE_TYPE_SYSTEM:
case CONNMAN_SERVICE_TYPE_GPS:
case CONNMAN_SERVICE_TYPE_P2P:
break;
case CONNMAN_SERVICE_TYPE_VPN:
g_key_file_set_boolean(keyfile, service->identifier,
"SplitRouting", service->do_split_routing);
if (service->favorite)
g_key_file_set_boolean(keyfile, service->identifier,
"AutoConnect", service->autoconnect);
break;
case CONNMAN_SERVICE_TYPE_WIFI:
if (service->network) {
const unsigned char *ssid;
unsigned int ssid_len = 0;
ssid = connman_network_get_blob(service->network,
"WiFi.SSID", &ssid_len);
if (ssid && ssid_len > 0 && ssid[0] != '\0') {
char *identifier = service->identifier;
GString *ssid_str;
unsigned int i;
ssid_str = g_string_sized_new(ssid_len * 2);
if (!ssid_str) {
err = -ENOMEM;
goto done;
}
for (i = 0; i < ssid_len; i++)
g_string_append_printf(ssid_str,
"%02x", ssid[i]);
g_key_file_set_string(keyfile, identifier,
"SSID", ssid_str->str);
g_string_free(ssid_str, TRUE);
}
freq = connman_network_get_frequency(service->network);
g_key_file_set_integer(keyfile, service->identifier,
"Frequency", freq);
}
/* fall through */
case CONNMAN_SERVICE_TYPE_GADGET:
case CONNMAN_SERVICE_TYPE_BLUETOOTH:
case CONNMAN_SERVICE_TYPE_CELLULAR:
g_key_file_set_boolean(keyfile, service->identifier,
"Favorite", service->favorite);
/* fall through */
case CONNMAN_SERVICE_TYPE_ETHERNET:
if (service->favorite)
g_key_file_set_boolean(keyfile, service->identifier,
"AutoConnect", service->autoconnect);
break;
}
str = util_timeval_to_iso8601(&service->modified);
if (str) {
g_key_file_set_string(keyfile, service->identifier,
"Modified", str);
g_free(str);
}
if (service->passphrase && strlen(service->passphrase) > 0) {
char *enc = g_strescape(service->passphrase, NULL);
g_key_file_set_string(keyfile, service->identifier,
"Passphrase", enc);
g_free(enc);
}
if (service->ipconfig_ipv4)
__connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
service->identifier, "IPv4.");
if (service->ipconfig_ipv6)
__connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
service->identifier, "IPv6.");
if (service->nameservers_config) {
guint len = g_strv_length(service->nameservers_config);
g_key_file_set_string_list(keyfile, service->identifier,
"Nameservers",
(const gchar **) service->nameservers_config, len);
}
if (service->timeservers_config) {
guint len = g_strv_length(service->timeservers_config);
g_key_file_set_string_list(keyfile, service->identifier,
"Timeservers",
(const gchar **) service->timeservers_config, len);
}
if (service->domains) {
guint len = g_strv_length(service->domains);
g_key_file_set_string_list(keyfile, service->identifier,
"Domains",
(const gchar **) service->domains, len);
}
cst_str = proxymethod2string(service->proxy_config);
if (cst_str)
g_key_file_set_string(keyfile, service->identifier,
"Proxy.Method", cst_str);
if (service->proxies) {
guint len = g_strv_length(service->proxies);
g_key_file_set_string_list(keyfile, service->identifier,
"Proxy.Servers",
(const gchar **) service->proxies, len);
}
if (service->excludes) {
guint len = g_strv_length(service->excludes);
g_key_file_set_string_list(keyfile, service->identifier,
"Proxy.Excludes",
(const gchar **) service->excludes, len);
}
if (service->pac && strlen(service->pac) > 0)
g_key_file_set_string(keyfile, service->identifier,
"Proxy.URL", service->pac);
if (service->mdns_config)
g_key_file_set_boolean(keyfile, service->identifier,
"mDNS", TRUE);
if (service->hidden_service)
g_key_file_set_boolean(keyfile, service->identifier,
"Hidden", TRUE);
if (service->config_file && strlen(service->config_file) > 0)
g_key_file_set_string(keyfile, service->identifier,
"Config.file", service->config_file);
if (service->config_entry && strlen(service->config_entry) > 0)
g_key_file_set_string(keyfile, service->identifier,
"Config.ident", service->config_entry);
done:
__connman_storage_save_service(keyfile, service->identifier);
g_key_file_free(keyfile);
return err;
}
void __connman_service_save(struct connman_service *service)
{
if (!service)
return;
service_save(service);
}
static enum connman_service_state combine_state(
enum connman_service_state state_a,
enum connman_service_state state_b)
{
enum connman_service_state result;
if (state_a == state_b) {
result = state_a;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
result = state_b;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
result = state_a;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
result = state_b;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
result = state_a;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
result = state_a;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
result = state_b;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_READY) {
result = state_a;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_READY) {
result = state_b;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
result = state_a;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
result = state_b;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
result = state_a;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
result = state_b;
goto done;
}
if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
result = state_a;
goto done;
}
if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
result = state_b;
goto done;
}
result = CONNMAN_SERVICE_STATE_FAILURE;
done:
return result;
}
static bool is_connecting(enum connman_service_state state)
{
switch (state) {
case CONNMAN_SERVICE_STATE_UNKNOWN:
case CONNMAN_SERVICE_STATE_IDLE:
case CONNMAN_SERVICE_STATE_FAILURE:
case CONNMAN_SERVICE_STATE_DISCONNECT:
case CONNMAN_SERVICE_STATE_READY:
case CONNMAN_SERVICE_STATE_ONLINE:
break;
case CONNMAN_SERVICE_STATE_ASSOCIATION:
case CONNMAN_SERVICE_STATE_CONFIGURATION:
return true;
}
return false;
}
static bool is_connected(enum connman_service_state state)
{
switch (state) {
case CONNMAN_SERVICE_STATE_UNKNOWN:
case CONNMAN_SERVICE_STATE_IDLE:
case CONNMAN_SERVICE_STATE_ASSOCIATION:
case CONNMAN_SERVICE_STATE_CONFIGURATION:
case CONNMAN_SERVICE_STATE_DISCONNECT:
case CONNMAN_SERVICE_STATE_FAILURE:
break;
case CONNMAN_SERVICE_STATE_READY:
case CONNMAN_SERVICE_STATE_ONLINE:
return true;
}
return false;
}
static bool is_idle(enum connman_service_state state)
{
switch (state) {
case CONNMAN_SERVICE_STATE_IDLE:
case CONNMAN_SERVICE_STATE_DISCONNECT:
case CONNMAN_SERVICE_STATE_FAILURE:
return true;
case CONNMAN_SERVICE_STATE_UNKNOWN:
case CONNMAN_SERVICE_STATE_ASSOCIATION:
case CONNMAN_SERVICE_STATE_CONFIGURATION:
case CONNMAN_SERVICE_STATE_READY:
case CONNMAN_SERVICE_STATE_ONLINE:
break;
}
return false;
}
static int nameservers_changed_cb(void *user_data)
{
struct connman_service *service = user_data;
DBG("service %p", service);
service->nameservers_timeout = 0;
if ((is_idle(service->state) && !service->nameservers) ||
is_connected(service->state))
dns_changed(service);
return FALSE;
}
static void nameservers_changed(struct connman_service *service)
{
if (!service->nameservers_timeout)
service->nameservers_timeout = g_idle_add(nameservers_changed_cb,
service);
}
static bool nameserver_available(struct connman_service *service,
enum connman_ipconfig_type type,
const char *ns)
{
int family;
family = connman_inet_check_ipaddress(ns);
if (family == AF_INET) {
if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
return false;
return is_connected(service->state_ipv4);
}
if (family == AF_INET6) {
if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
return false;
return is_connected(service->state_ipv6);
}
return false;
}
static int searchdomain_add_all(struct connman_service *service)
{
int index, i = 0;
if (!is_connected(service->state))
return -ENOTCONN;
index = __connman_service_get_index(service);
if (index < 0)
return -ENXIO;
if (service->domains) {
while (service->domains[i]) {
connman_resolver_append(index, service->domains[i],
NULL);
i++;
}
return 0;
}
if (service->domainname)
connman_resolver_append(index, service->domainname, NULL);
return 0;
}
static int searchdomain_remove_all(struct connman_service *service)
{
int index, i = 0;
if (!is_connected(service->state))
return -ENOTCONN;
index = __connman_service_get_index(service);
if (index < 0)
return -ENXIO;
while (service->domains && service->domains[i]) {
connman_resolver_remove(index, service->domains[i], NULL);
i++;
}
if (service->domainname)
connman_resolver_remove(index, service->domainname, NULL);
return 0;
}
static int nameserver_add(struct connman_service *service,
enum connman_ipconfig_type type,
const char *nameserver)
{
int index, ret;
if (!nameserver_available(service, type, nameserver))
return 0;
index = __connman_service_get_index(service);
if (index < 0)
return -ENXIO;
ret = connman_resolver_append(index, NULL, nameserver);
if (ret >= 0)
nameservers_changed(service);
return ret;
}
static int nameserver_add_all(struct connman_service *service,
enum connman_ipconfig_type type)
{
int i = 0;
if (service->nameservers_config) {
while (service->nameservers_config[i]) {
nameserver_add(service, type,
service->nameservers_config[i]);
i++;
}
} else if (service->nameservers) {
while (service->nameservers[i]) {
nameserver_add(service, type,
service->nameservers[i]);
i++;
}
}
if (!i)
__connman_resolver_append_fallback_nameservers();
searchdomain_add_all(service);
return 0;
}
static int nameserver_remove(struct connman_service *service,
enum connman_ipconfig_type type,
const char *nameserver)
{
int index, ret;
if (!nameserver_available(service, type, nameserver))
return 0;
index = __connman_service_get_index(service);
if (index < 0)
return -ENXIO;
ret = connman_resolver_remove(index, NULL, nameserver);
if (ret >= 0)
nameservers_changed(service);
return ret;
}
static int nameserver_remove_all(struct connman_service *service,
enum connman_ipconfig_type type)
{
int index, i = 0;
index = __connman_service_get_index(service);
if (index < 0)
return -ENXIO;
while (service->nameservers_config && service->nameservers_config[i]) {
nameserver_remove(service, type,
service->nameservers_config[i]);
i++;
}
i = 0;
while (service->nameservers && service->nameservers[i]) {
nameserver_remove(service, type, service->nameservers[i]);
i++;
}
searchdomain_remove_all(service);
return 0;
}
/*
* The is_auto variable is set to true when IPv6 autoconf nameservers are
* inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
* for details) and not through service.c
*/
int __connman_service_nameserver_append(struct connman_service *service,
const char *nameserver, bool is_auto)
{
char **nameservers;
int len, i;
DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
if (!nameserver)
return -EINVAL;
if (is_auto)
nameservers = service->nameservers_auto;
else
nameservers = service->nameservers;
if (nameservers) {
for (i = 0; nameservers[i]; i++) {
if (g_strcmp0(nameservers[i], nameserver) == 0)
return -EEXIST;
}
len = g_strv_length(nameservers);
nameservers = g_try_renew(char *, nameservers, len + 2);
} else {
len = 0;
nameservers = g_try_new0(char *, len + 2);
}
if (!nameservers)
return -ENOMEM;
nameservers[len] = g_strdup(nameserver);
nameservers[len + 1] = NULL;
if (is_auto) {
service->nameservers_auto = nameservers;
} else {
service->nameservers = nameservers;
nameserver_add(service, CONNMAN_IPCONFIG_TYPE_ALL, nameserver);
}
nameservers_changed(service);
searchdomain_add_all(service);
return 0;
}
int __connman_service_nameserver_remove(struct connman_service *service,
const char *nameserver, bool is_auto)
{
char **servers, **nameservers;
bool found = false;
int len, i, j;
DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
if (!nameserver)
return -EINVAL;
if (is_auto)
nameservers = service->nameservers_auto;
else
nameservers = service->nameservers;
if (!nameservers)
return 0;
for (i = 0; nameservers[i]; i++)
if (g_strcmp0(nameservers[i], nameserver) == 0) {
found = true;
break;
}
if (!found)
return 0;
len = g_strv_length(nameservers);
if (len == 1) {
servers = NULL;
goto set_servers;
}
servers = g_try_new0(char *, len);
if (!servers)
return -ENOMEM;
for (i = 0, j = 0; i < len; i++) {
if (g_strcmp0(nameservers[i], nameserver)) {
servers[j] = nameservers[i];
j++;
} else
g_free(nameservers[i]);
nameservers[i] = NULL;
}
servers[len - 1] = NULL;
set_servers:
g_strfreev(nameservers);
nameservers = servers;
if (is_auto) {
service->nameservers_auto = nameservers;
} else {
service->nameservers = nameservers;
nameserver_remove(service, CONNMAN_IPCONFIG_TYPE_ALL,
nameserver);
}
return 0;
}
void __connman_service_nameserver_clear(struct connman_service *service)
{
nameserver_remove_all(service, CONNMAN_IPCONFIG_TYPE_ALL);
g_strfreev(service->nameservers);
service->nameservers = NULL;
nameserver_add_all(service, CONNMAN_IPCONFIG_TYPE_ALL);
}
static void add_nameserver_route(int family, int index, char *nameserver,
const char *gw)
{
switch (family) {
case AF_INET:
if (connman_inet_compare_subnet(index, nameserver))
break;
if (connman_inet_add_host_route(index, nameserver, gw) < 0)
/* For P-t-P link the above route add will fail */
connman_inet_add_host_route(index, nameserver, NULL);
break;
case AF_INET6:
if (connman_inet_add_ipv6_host_route(index, nameserver,
gw) < 0)
connman_inet_add_ipv6_host_route(index, nameserver,
NULL);
break;
}
}
static void nameserver_add_routes(int index, char **nameservers,
const char *gw)
{
int i, ns_family, gw_family;
gw_family = connman_inet_check_ipaddress(gw);
if (gw_family < 0)
return;
for (i = 0; nameservers[i]; i++) {
ns_family = connman_inet_check_ipaddress(nameservers[i]);
if (ns_family < 0 || ns_family != gw_family)
continue;
add_nameserver_route(ns_family, index, nameservers[i], gw);
}
}
static void nameserver_del_routes(int index, char **nameservers,
enum connman_ipconfig_type type)
{
int i, family;
for (i = 0; nameservers[i]; i++) {
family = connman_inet_check_ipaddress(nameservers[i]);
if (family < 0)
continue;
switch (family) {
case AF_INET:
if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
connman_inet_del_host_route(index,
nameservers[i]);
break;
case AF_INET6:
if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
connman_inet_del_ipv6_host_route(index,
nameservers[i]);
break;
}
}
}
void __connman_service_nameserver_add_routes(struct connman_service *service,
const char *gw)
{
int index;
if (!service)
return;
index = __connman_service_get_index(service);
if (service->nameservers_config) {
/*
* Configured nameserver takes preference over the
* discoverd nameserver gathered from DHCP, VPN, etc.
*/
nameserver_add_routes(index, service->nameservers_config, gw);
} else if (service->nameservers) {
/*
* We add nameservers host routes for nameservers that
* are not on our subnet. For those who are, the subnet
* route will be installed by the time the dns proxy code
* tries to reach them. The subnet route is installed
* when setting the interface IP address.
*/
nameserver_add_routes(index, service->nameservers, gw);
}
}
void __connman_service_nameserver_del_routes(struct connman_service *service,
enum connman_ipconfig_type type)
{
int index;
if (!service)
return;
index = __connman_service_get_index(service);
if (service->nameservers_config)
nameserver_del_routes(index, service->nameservers_config,
type);
else if (service->nameservers)
nameserver_del_routes(index, service->nameservers, type);
}
static bool check_proxy_setup(struct connman_service *service)
{
/*
* We start WPAD if we haven't got a PAC URL from DHCP and
* if our proxy manual configuration is either empty or set
* to AUTO with an empty URL.
*/
if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
return true;
if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
(service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
service->pac))
return true;
if (__connman_wpad_start(service) < 0) {
service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
__connman_notifier_proxy_changed(service);
return true;
}
return false;
}
static void cancel_online_check(struct connman_service *service)
{
if (service->online_timeout == 0)
return;
g_source_remove(service->online_timeout);
service->online_timeout = 0;
connman_service_unref(service);
}
static void start_online_check(struct connman_service *service,
enum connman_ipconfig_type type)
{
if (!connman_setting_get_bool("EnableOnlineCheck")) {
connman_info("Online check disabled. "
"Default service remains in READY state.");
return;
}
enable_online_to_ready_transition =
connman_setting_get_bool("EnableOnlineToReadyTransition");
online_check_initial_interval =
connman_setting_get_uint("OnlineCheckInitialInterval");
online_check_max_interval =
connman_setting_get_uint("OnlineCheckMaxInterval");
if (type != CONNMAN_IPCONFIG_TYPE_IPV4 || check_proxy_setup(service)) {
cancel_online_check(service);
__connman_service_wispr_start(service, type);
}
}
static void address_updated(struct connman_service *service,
enum connman_ipconfig_type type)
{
if (is_connected(service->state) &&
service == connman_service_get_default()) {
nameserver_remove_all(service, type);
nameserver_add_all(service, type);
start_online_check(service, type);
__connman_timeserver_sync(service);
}
}
static struct connman_stats *stats_get(struct connman_service *service)
{
if (service->roaming)
return &service->stats_roaming;
else
return &service->stats;
}
static bool stats_enabled(struct connman_service *service)
{
struct connman_stats *stats = stats_get(service);
return stats->enabled;
}
static void stats_start(struct connman_service *service)
{
struct connman_stats *stats = stats_get(service);
DBG("service %p", service);
if (!stats->timer)
return;
stats->enabled = true;
stats->data_last.time = stats->data.time;
g_timer_start(stats->timer);
}
static void stats_stop(struct connman_service *service)
{
struct connman_stats *stats = stats_get(service);
unsigned int seconds;
DBG("service %p", service);
if (!stats->timer)
return;
if (!stats->enabled)
return;
g_timer_stop(stats->timer);
seconds = g_timer_elapsed(stats->timer, NULL);
stats->data.time = stats->data_last.time + seconds;
stats->enabled = false;
}
static void reset_stats(struct connman_service *service)
{
DBG("service %p", service);
/* home */
service->stats.valid = false;
service->stats.data.rx_packets = 0;
service->stats.data.tx_packets = 0;
service->stats.data.rx_bytes = 0;
service->stats.data.tx_bytes = 0;
service->stats.data.rx_errors = 0;
service->stats.data.tx_errors = 0;
service->stats.data.rx_dropped = 0;
service->stats.data.tx_dropped = 0;
service->stats.data.time = 0;
service->stats.data_last.time = 0;
g_timer_reset(service->stats.timer);
/* roaming */
service->stats_roaming.valid = false;
service->stats_roaming.data.rx_packets = 0;
service->stats_roaming.data.tx_packets = 0;
service->stats_roaming.data.rx_bytes = 0;
service->stats_roaming.data.tx_bytes = 0;
service->stats_roaming.data.rx_errors = 0;
service->stats_roaming.data.tx_errors = 0;
service->stats_roaming.data.rx_dropped = 0;
service->stats_roaming.data.tx_dropped = 0;
service->stats_roaming.data.time = 0;
service->stats_roaming.data_last.time = 0;
g_timer_reset(service->stats_roaming.timer);
}
struct connman_service *connman_service_get_default(void)
{
struct connman_service *service;
if (!service_list)
return NULL;
service = service_list->data;
if (!is_connected(service->state))
return NULL;
return service;
}
bool __connman_service_index_is_default(int index)
{
struct connman_service *service;
if (index < 0)
return false;
service = connman_service_get_default();
return __connman_service_get_index(service) == index;
}
static void default_changed(void)
{
struct connman_service *service = connman_service_get_default();
if (service == current_default)
return;
DBG("current default %p %s", current_default,
current_default ? current_default->identifier : "");
DBG("new default %p %s", service, service ? service->identifier : "");
__connman_service_timeserver_changed(current_default, NULL);
current_default = service;
if (service) {
if (service->hostname &&
connman_setting_get_bool("AllowHostnameUpdates"))
__connman_utsname_set_hostname(service->hostname);
if (service->domainname &&
connman_setting_get_bool("AllowDomainnameUpdates"))
__connman_utsname_set_domainname(service->domainname);
if (__connman_service_is_connected_state(service,
CONNMAN_IPCONFIG_TYPE_IPV4))
__connman_service_wispr_start(service,
CONNMAN_IPCONFIG_TYPE_IPV4);
if (__connman_service_is_connected_state(service,
CONNMAN_IPCONFIG_TYPE_IPV6))
__connman_service_wispr_start(service,
CONNMAN_IPCONFIG_TYPE_IPV6);
/*
* Connect VPN automatically when new default service
* is set and connected, unless new default is VPN
*/
if (is_connected(service->state) &&
service->type != CONNMAN_SERVICE_TYPE_VPN) {
DBG("running vpn_auto_connect");
vpn_auto_connect();
}
}
__connman_notifier_default_changed(service);
}
static void state_changed(struct connman_service *service)
{
const char *str;
__connman_notifier_service_state_changed(service, service->state);
str = state2string(service->state);
if (!str)
return;
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "State",
DBUS_TYPE_STRING, &str);
}
static void strength_changed(struct connman_service *service)
{
if (service->strength == 0)
return;
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "Strength",
DBUS_TYPE_BYTE, &service->strength);
}
static void favorite_changed(struct connman_service *service)
{
dbus_bool_t favorite;
if (!service->path)
return;
if (!allow_property_changed(service))
return;
favorite = service->favorite;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "Favorite",
DBUS_TYPE_BOOLEAN, &favorite);
}
static void immutable_changed(struct connman_service *service)
{
dbus_bool_t immutable;
if (!service->path)
return;
if (!allow_property_changed(service))
return;
immutable = service->immutable;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "Immutable",
DBUS_TYPE_BOOLEAN, &immutable);
}
static void roaming_changed(struct connman_service *service)
{
dbus_bool_t roaming;
if (!service->path)
return;
if (!allow_property_changed(service))
return;
roaming = service->roaming;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "Roaming",
DBUS_TYPE_BOOLEAN, &roaming);
}
static void autoconnect_changed(struct connman_service *service)
{
dbus_bool_t autoconnect;
if (!service->path)
return;
if (!allow_property_changed(service))
return;
autoconnect = service->autoconnect;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "AutoConnect",
DBUS_TYPE_BOOLEAN, &autoconnect);
}
bool connman_service_set_autoconnect(struct connman_service *service,
bool autoconnect)
{
if (service->autoconnect == autoconnect)
return false;
service->autoconnect = autoconnect;
autoconnect_changed(service);
connman_network_set_autoconnect(service->network, autoconnect);
return true;
}
static void append_security(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
const char *str;
str = security2string(service->security);
if (str)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
/*
* Some access points incorrectly advertise WPS even when they
* are configured as open or no security, so filter
* appropriately.
*/
if (service->wps) {
switch (service->security) {
case CONNMAN_SERVICE_SECURITY_PSK:
case CONNMAN_SERVICE_SECURITY_WPA:
case CONNMAN_SERVICE_SECURITY_RSN:
str = "wps";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
break;
case CONNMAN_SERVICE_SECURITY_UNKNOWN:
case CONNMAN_SERVICE_SECURITY_NONE:
case CONNMAN_SERVICE_SECURITY_WEP:
case CONNMAN_SERVICE_SECURITY_8021X:
break;
}
if (service->wps_advertizing) {
str = "wps_advertising";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
}
}
static void security_changed(struct connman_service *service)
{
if (!service->path)
return;
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE, "Security",
DBUS_TYPE_STRING, append_security, service);
}
static void append_ethernet(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (service->ipconfig_ipv4)
__connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
iter);
else if (service->ipconfig_ipv6)
__connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
iter);
}
static void append_ipv4(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!is_connected(service->state_ipv4))
return;
if (service->ipconfig_ipv4)
__connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
}
static void append_ipv6(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!is_connected(service->state_ipv6))
return;
if (service->ipconfig_ipv6)
__connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
service->ipconfig_ipv4);
}
static void append_ipv4config(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (service->ipconfig_ipv4)
__connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
iter);
}
static void append_ipv6config(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (service->ipconfig_ipv6)
__connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
iter);
}
static void append_nameservers(DBusMessageIter *iter,
struct connman_service *service, char **servers)
{
int i;
bool available = true;
for (i = 0; servers[i]; i++) {
if (service)
available = nameserver_available(service,
CONNMAN_IPCONFIG_TYPE_ALL,
servers[i]);
if (available)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &servers[i]);
}
}
static void append_dns(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!is_connected(service->state))
return;
if (service->nameservers_config) {
append_nameservers(iter, service, service->nameservers_config);
return;
} else {
if (service->nameservers)
append_nameservers(iter, service,
service->nameservers);
if (service->nameservers_auto)
append_nameservers(iter, service,
service->nameservers_auto);
if (!service->nameservers && !service->nameservers_auto) {
char **ns;
DBG("append fallback nameservers");
ns = connman_setting_get_string_list("FallbackNameservers");
if (ns)
append_nameservers(iter, service, ns);
}
}
}
static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!service->nameservers_config)
return;
append_nameservers(iter, NULL, service->nameservers_config);
}
static void append_ts(DBusMessageIter *iter, void *user_data)
{
GSList *list = user_data;
while (list) {
char *timeserver = list->data;
if (timeserver)
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
&timeserver);
list = g_slist_next(list);
}
}
static void append_tsconfig(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
int i;
if (!service->timeservers_config)
return;
for (i = 0; service->timeservers_config[i]; i++) {
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING,
&service->timeservers_config[i]);
}
}
static void append_domainconfig(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
int i;
if (!service->domains)
return;
for (i = 0; service->domains[i]; i++)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &service->domains[i]);
}
static void append_domain(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!is_connected(service->state) &&
!is_connecting(service->state))
return;
if (service->domains)
append_domainconfig(iter, user_data);
else if (service->domainname)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &service->domainname);
}
static void append_proxies(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
int i;
if (!service->proxies)
return;
for (i = 0; service->proxies[i]; i++)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &service->proxies[i]);
}
static void append_excludes(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
int i;
if (!service->excludes)
return;
for (i = 0; service->excludes[i]; i++)
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &service->excludes[i]);
}
static void append_proxy(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
enum connman_service_proxy_method proxy;
const char *pac = NULL;
const char *method = proxymethod2string(
CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
if (!is_connected(service->state))
return;
proxy = connman_service_get_proxy_method(service);
switch (proxy) {
case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
return;
case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
goto done;
case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
connman_dbus_dict_append_array(iter, "Servers",
DBUS_TYPE_STRING, append_proxies,
service);
connman_dbus_dict_append_array(iter, "Excludes",
DBUS_TYPE_STRING, append_excludes,
service);
break;
case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
/* Maybe DHCP, or WPAD, has provided an url for a pac file */
if (service->ipconfig_ipv4)
pac = __connman_ipconfig_get_proxy_autoconfig(
service->ipconfig_ipv4);
else if (service->ipconfig_ipv6)
pac = __connman_ipconfig_get_proxy_autoconfig(
service->ipconfig_ipv6);
if (!service->pac && !pac)
goto done;
if (service->pac)
pac = service->pac;
connman_dbus_dict_append_basic(iter, "URL",
DBUS_TYPE_STRING, &pac);
break;
}
method = proxymethod2string(proxy);
done:
connman_dbus_dict_append_basic(iter, "Method",
DBUS_TYPE_STRING, &method);
}
static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
const char *method;
if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
return;
switch (service->proxy_config) {
case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
return;
case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
break;
case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
if (service->proxies)
connman_dbus_dict_append_array(iter, "Servers",
DBUS_TYPE_STRING,
append_proxies, service);
if (service->excludes)
connman_dbus_dict_append_array(iter, "Excludes",
DBUS_TYPE_STRING,
append_excludes, service);
break;
case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
if (service->pac)
connman_dbus_dict_append_basic(iter, "URL",
DBUS_TYPE_STRING, &service->pac);
break;
}
method = proxymethod2string(service->proxy_config);
connman_dbus_dict_append_basic(iter, "Method",
DBUS_TYPE_STRING, &method);
}
static void append_provider(DBusMessageIter *iter, void *user_data)
{
struct connman_service *service = user_data;
if (!is_connected(service->state))
return;
if (service->provider)
__connman_provider_append_properties(service->provider, iter);
}
static void settings_changed(struct connman_service *service,
struct connman_ipconfig *ipconfig)
{
enum connman_ipconfig_type type;
type = __connman_ipconfig_get_config_type(ipconfig);
__connman_notifier_ipconfig_changed(service, ipconfig);
if (!allow_property_changed(service))
return;
if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE, "IPv4",
append_ipv4, service);
else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE, "IPv6",
append_ipv6, service);
}
static void ipv4_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE,
"IPv4.Configuration",
append_ipv4config,
service);
}
void __connman_service_notify_ipv4_configuration(
struct connman_service *service)
{
if (!service)
return;
ipv4_configuration_changed(service);
}
static void ipv6_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE,
"IPv6.Configuration",
append_ipv6config,
service);
}
static void dns_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE, "Nameservers",
DBUS_TYPE_STRING, append_dns, service);
}
static void dns_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE,
"Nameservers.Configuration",
DBUS_TYPE_STRING, append_dnsconfig, service);
dns_changed(service);
}
static void domain_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE, "Domains",
DBUS_TYPE_STRING, append_domain, service);
}
static void domain_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE,
"Domains.Configuration",
DBUS_TYPE_STRING, append_domainconfig, service);
}
static void proxy_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE, "Proxy",
append_proxy, service);
}
static void proxy_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
append_proxyconfig, service);
proxy_changed(service);
}
static void mdns_changed(struct connman_service *service)
{
dbus_bool_t mdns = service->mdns;
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "mDNS", DBUS_TYPE_BOOLEAN,
&mdns);
}
static void mdns_configuration_changed(struct connman_service *service)
{
dbus_bool_t mdns_config = service->mdns_config;
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_basic(service->path,
CONNMAN_SERVICE_INTERFACE, "mDNS.Configuration",
DBUS_TYPE_BOOLEAN, &mdns_config);
}
static int set_mdns(struct connman_service *service,
bool enabled)
{
int result;
result = __connman_resolver_set_mdns(
__connman_service_get_index(service), enabled);
if (result == 0) {
if (service->mdns != enabled) {
service->mdns = enabled;
mdns_changed(service);
}
}
return result;
}
static void timeservers_configuration_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_array(service->path,
CONNMAN_SERVICE_INTERFACE,
"Timeservers.Configuration",
DBUS_TYPE_STRING,
append_tsconfig, service);
}
static void link_changed(struct connman_service *service)
{
if (!allow_property_changed(service))
return;
connman_dbus_property_changed_dict(service->path,
CONNMAN_SERVICE_INTERFACE, "Ethernet",
append_ethernet, service);
}
static void stats_append_counters(DBusMessageIter *dict,
struct connman_stats_data *stats,
struct connman_stats_data *counters,
bool append_all)
{
if (counters->rx_packets != stats->rx_packets || append_all) {
counters->rx_packets = stats->rx_packets;
connman_dbus_dict_append_basic(dict, "RX.Packets",
DBUS_TYPE_UINT32, &stats->rx_packets);
}
if (counters->tx_packets != stats->tx_packets || append_all) {
counters->tx_packets = stats->tx_packets;
connman_dbus_dict_append_basic(dict, "TX.Packets",
DBUS_TYPE_UINT32, &stats->tx_packets);
}
if (counters->rx_bytes != stats->rx_bytes || append_all) {
counters->rx_bytes = stats->rx_bytes;
connman_dbus_dict_append_basic(dict, "RX.Bytes",
DBUS_TYPE_UINT32, &stats->rx_bytes);
}
if (counters->tx_bytes != stats->tx_bytes || append_all) {
counters->tx_bytes = stats->tx_bytes;
connman_dbus_dict_append_basic(dict, "TX.Bytes",
DBUS_TYPE_UINT32, &stats->tx_bytes);
}
if (counters->rx_errors != stats->rx_errors || append_all) {
counters->rx_errors = stats->rx_errors;
connman_dbus_dict_append_basic(dict, "RX.Errors",
DBUS_TYPE_UINT32, &stats->rx_errors);
}
if (counters->tx_errors != stats->tx_errors || append_all) {
counters->tx_errors = stats->tx_errors;
connman_dbus_dict_append_basic(dict, "TX.Errors",
DBUS_TYPE_UINT32, &stats->tx_errors);
}
if (counters->rx_dropped != stats->rx_dropped || append_all) {
counters->rx_dropped = stats->rx_dropped;
connman_dbus_dict_append_basic(dict, "RX.Dropped",
DBUS_TYPE_UINT32, &stats->rx_dropped);
}
if (counters->tx_dropped != stats->tx_dropped || append_all) {
counters->tx_dropped = stats->tx_dropped;
connman_dbus_dict_append_basic(dict, "TX.Dropped",
DBUS_TYPE_UINT32, &stats->tx_dropped);
}
if (counters->time != stats->time || append_all) {
counters->time = stats->time;
connman_dbus_dict_append_basic(dict, "Time",
DBUS_TYPE_UINT32, &stats->time);
}
}
static void stats_append(struct connman_service *service,
const char *counter,
struct connman_stats_counter *counters,
bool append_all)
{
DBusMessageIter array, dict;
DBusMessage *msg;
DBG("service %p counter %s", service, counter);
msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
if (!msg)
return;
dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
&service->path, DBUS_TYPE_INVALID);
dbus_message_iter_init_append(msg, &array);
/* home counter */
connman_dbus_dict_open(&array, &dict);
stats_append_counters(&dict, &service->stats.data,
&counters->stats.data, append_all);
connman_dbus_dict_close(&array, &dict);
/* roaming counter */
connman_dbus_dict_open(&array, &dict);
stats_append_counters(&dict, &service->stats_roaming.data,
&counters->stats_roaming.data, append_all);
connman_dbus_dict_close(&array, &dict);
__connman_counter_send_usage(counter, msg);
}
static void stats_update(struct connman_service *service,
unsigned int rx_packets, unsigned int tx_packets,
unsigned int rx_bytes, unsigned int tx_bytes,
unsigned int rx_errors, unsigned int tx_errors,
unsigned int rx_dropped, unsigned int tx_dropped)
{
struct connman_stats *stats = stats_get(service);
struct connman_stats_data *data_last = &stats->data_last;
struct connman_stats_data *data = &stats->data;
unsigned int seconds;
DBG("service %p", service);
if (stats->valid) {
data->rx_packets +=
rx_packets - data_last->rx_packets;
data->tx_packets +=
tx_packets - data_last->tx_packets;
data->rx_bytes +=
rx_bytes - data_last->rx_bytes;
data->tx_bytes +=
tx_bytes - data_last->tx_bytes;
data->rx_errors +=
rx_errors - data_last->rx_errors;
data->tx_errors +=
tx_errors - data_last->tx_errors;
data->rx_dropped +=
rx_dropped - data_last->rx_dropped;
data->tx_dropped +=
tx_dropped - data_last->tx_dropped;
} else {
stats->valid = true;
}
data_last->rx_packets = rx_packets;
data_last->tx_packets = tx_packets;
data_last->rx_bytes = rx_bytes;
data_last->tx_bytes = tx_bytes;
data_last->rx_errors = rx_errors;
data_last->tx_errors = tx_errors;
data_last->rx_dropped = rx_dropped;
data_last->tx_dropped = tx_dropped;
seconds = g_timer_elapsed(stats->timer, NULL);
stats->data.time = stats->data_last.time + seconds;
}
void __connman_service_notify(struct connman_service *service,
unsigned int rx_packets, unsigned int tx_packets,
unsigned int rx_bytes, unsigned int tx_bytes,
unsigned int rx_errors, unsigned int tx_errors,
unsigned int rx_dropped, unsigned int tx_dropped)
{
GHashTableIter iter;
gpointer key, value;
const char *counter;
struct connman_stats_counter *counters;
struct connman_stats_data *data;
int err;
if (!service)
return;
if (!is_connected(service->state))
return;
stats_update(service,
rx_packets, tx_packets,
rx_bytes, tx_bytes,
rx_errors, tx_errors,
rx_dropped, tx_dropped);
data = &stats_get(service)->data;
err = __connman_stats_update(service, service->roaming, data);
if (err < 0)
connman_error("Failed to store statistics for %s",
service->identifier);
g_hash_table_iter_init(&iter, service->counter_table);
while (g_hash_table_iter_next(&iter, &key, &value)) {
counter = key;
counters = value;
stats_append(service, counter, counters, counters->append_all);
counters->append_all = false;
}
}
int __connman_service_counter_register(const char *counter)
{
struct connman_service *service;
GList *list;
struct connman_stats_counter *counters;
DBG("counter %s", counter);
counter_list = g_slist_prepend(counter_list, (gpointer)counter);
for (list = service_list; list; list = list->next) {
service = list->data;
counters = g_try_new0(struct connman_stats_counter, 1);
if (!counters)
return -ENOMEM;
counters->append_all = true;
g_hash_table_replace(service->counter_table, (gpointer)counter,
counters);
}
return 0;
}
void __connman_service_counter_unregister(const char *counter)
{
struct connman_service *service;
GList *list;
DBG("counter %s", counter);
for (list = service_list; list; list = list->next) {
service = list->data;
g_hash_table_remove(service->counter_table, counter);
}
counter_list = g_slist_remove(counter_list, counter);
}
int connman_service_iterate_services(connman_service_iterate_cb cb,
void *user_data)
{
GList *list;
int ret = 0;
for (list = service_list; list && ret == 0; list = list->next)
ret = cb((struct connman_service *)list->data, user_data);
return ret;
}
static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
struct connman_service *service)
{
dbus_bool_t val;
const char *str;
GSList *list;
str = __connman_service_type2string(service->type);
if (str)
connman_dbus_dict_append_basic(dict, "Type",
DBUS_TYPE_STRING, &str);
connman_dbus_dict_append_array(dict, "Security",
DBUS_TYPE_STRING, append_security, service);
str = state2string(service->state);
if (str)
connman_dbus_dict_append_basic(dict, "State",
DBUS_TYPE_STRING, &str);
str = error2string(service->error);
if (str)
connman_dbus_dict_append_basic(dict, "Error",
DBUS_TYPE_STRING, &str);
if (service->strength > 0)
connman_dbus_dict_append_basic(dict, "Strength",
DBUS_TYPE_BYTE, &service->strength);
val = service->favorite;
connman_dbus_dict_append_basic(dict, "Favorite",
DBUS_TYPE_BOOLEAN, &val);
val = service->immutable;
connman_dbus_dict_append_basic(dict, "Immutable",
DBUS_TYPE_BOOLEAN, &val);
if (service->favorite)
val = service->autoconnect;
else
val = service->favorite;
connman_dbus_dict_append_basic(dict, "AutoConnect",
DBUS_TYPE_BOOLEAN, &val);
if (service->name)
connman_dbus_dict_append_basic(dict, "Name",
DBUS_TYPE_STRING, &service->name);
switch (service->type) {
case CONNMAN_SERVICE_TYPE_UNKNOWN:
case CONNMAN_SERVICE_TYPE_SYSTEM:
case CONNMAN_SERVICE_TYPE_GPS:
case CONNMAN_SERVICE_TYPE_VPN:
case CONNMAN_SERVICE_TYPE_P2P:
break;
case CONNMAN_SERVICE_TYPE_CELLULAR:
val = service->roaming;
connman_dbus_dict_append_basic(dict, "Roaming",
DBUS_TYPE_BOOLEAN, &val);
connman_dbus_dict_append_dict(dict, "Ethernet",
append_ethernet, service);
break;
case CONNMAN_SERVICE_TYPE_WIFI:
case CONNMAN_SERVICE_TYPE_ETHERNET:
case CONNMAN_SERVICE_TYPE_BLUETOOTH:
case CONNMAN_SERVICE_TYPE_GADGET:
connman_dbus_dict_append_dict(dict, "Ethernet",
append_ethernet, service);
break;
}
connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
append_ipv4config, service);
connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
append_ipv6config, service);
connman_dbus_dict_append_array(dict, "Nameservers",
DBUS_TYPE_STRING, append_dns, service);
connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
DBUS_TYPE_STRING, append_dnsconfig, service);
if (service->state == CONNMAN_SERVICE_STATE_READY ||
service->state == CONNMAN_SERVICE_STATE_ONLINE)
list = __connman_timeserver_get_all(service);
else
list = NULL;
connman_dbus_dict_append_array(dict, "Timeservers",
DBUS_TYPE_STRING, append_ts, list);
g_slist_free_full(list, g_free);
connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
DBUS_TYPE_STRING, append_tsconfig, service);
connman_dbus_dict_append_array(dict, "Domains",
DBUS_TYPE_STRING, append_domain, service);
connman_dbus_dict_append_array(dict, "Domains.Configuration",
DBUS_TYPE_STRING, append_domainconfig, service);
connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
append_proxyconfig, service);
val = service->mdns;
connman_dbus_dict_append_basic(dict, "mDNS", DBUS_TYPE_BOOLEAN,
&val);
val = service->mdns_config;
connman_dbus_dict_append_basic(dict, "mDNS.Configuration",
DBUS_TYPE_BOOLEAN, &val);
connman_dbus_dict_append_dict(dict, "Provider",
append_provider, service);
if (service->network)
connman_network_append_acddbus(dict, service->network);
}
static void append_struct_service(DBusMessageIter *iter,
connman_dbus_append_cb_t function,
struct connman_service *service)
{
DBusMessageIter entry, dict;
dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
&service->path);
connman_dbus_dict_open(&entry, &dict);
if (function)
function(&dict, service);
connman_dbus_dict_close(&entry, &dict);
dbus_message_iter_close_container(iter, &entry);
}
static void append_dict_properties(DBusMessageIter *dict, void *user_data)
{
struct connman_service *service = user_data;
append_properties(dict, TRUE, service);
}
static void append_struct(gpointer value, gpointer user_data)
{
struct connman_service *service = value;
DBusMessageIter *iter = user_data;
if (!service->path)
return;
append_struct_service(iter, append_dict_properties, service);
}
void __connman_service_list_struct(DBusMessageIter *iter)
{
g_list_foreach(service_list, append_struct, iter);
}
bool __connman_service_is_hidden(struct connman_service *service)
{
return service->hidden;
}
bool
__connman_service_is_split_routing(struct connman_service *service)
{
return service->do_split_routing;
}
bool __connman_service_index_is_split_routing(int index)
{
struct connman_service *service;
if (index < 0)
return false;
service = __connman_service_lookup_from_index(index);
if (!service)
return false;
return __connman_service_is_split_routing(service);
}
int __connman_service_get_index(struct connman_service *service)
{
if (!service)
return -1;
if (service->network)
return connman_network_get_index(service->network);
else if (service->provider)
return connman_provider_get_index(service->provider);
return -1;
}
void __connman_service_set_hidden(struct connman_service *service)
{
if (!service || service->hidden)
return;
service->hidden_service = true;
}
void __connman_service_set_hostname(struct connman_service *service,
const char *hostname)
{
if (!service || service->hidden)
return;
g_free(service->hostname);
service->hostname = NULL;
if (hostname && g_str_is_ascii(hostname))
service->hostname = g_strdup(hostname);
}
const char *__connman_service_get_hostname(struct connman_service *service)
{
if (!service)
return NULL;
return service->hostname;
}
void __connman_service_set_domainname(struct connman_service *service,
const char *domainname)
{
if (!service || service->hidden)
return;
g_free(service->domainname);
service->domainname = NULL;
if (domainname && g_str_is_ascii(domainname))
service->domainname = g_strdup(domainname);
domain_changed(service);
}
const char *connman_service_get_domainname(struct connman_service *service)
{
if (!service)
return NULL;
if (service->domains)
return service->domains[0];
else
return service->domainname;
}
const char *connman_service_get_dbuspath(struct connman_service *service)
{
if (!service)
return NULL;
return service->path;
}
char **connman_service_get_nameservers(struct connman_service *service)
{
if (!service)
return NULL;
if (service->nameservers_config)
return g_strdupv(service->nameservers_config);
else if (service->nameservers ||
service->nameservers_auto) {
int len = 0, len_auto = 0, i;
char **nameservers;
if (service->nameservers)
len = g_strv_length(service->nameservers);
if (service->nameservers_auto)
len_auto = g_strv_length(service->nameservers_auto);
nameservers = g_try_new0(char *, len + len_auto + 1);
if (!nameservers)
return NULL;
for (i = 0; i < len; i++)
nameservers[i] = g_strdup(service->nameservers[i]);
for (i = 0; i < len_auto; i++)
nameservers[i + len] =
g_strdup(service->nameservers_auto[i]);
return nameservers;
}
return g_strdupv(connman_setting_get_string_list("FallbackNameservers"));
}
char **connman_service_get_timeservers_config(struct connman_service *service)
{
if (!service)
return NULL;
return service->timeservers_config;
}
char **connman_service_get_timeservers(struct connman_service *service)