session.c 41.7 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2014  Intel Corporation. All rights reserved.
6
 *  Copyright (C) 2011-2014  BMW Car IT GmbH.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 *  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 <gdbus.h>

#include "connman.h"

static DBusConnection *connection;
static GHashTable *session_hash;
35
static GHashTable *service_hash;
36
static struct connman_session *ecall_session;
37 38
static uint32_t session_mark = 256;
static struct firewall_context *global_firewall = NULL;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

enum connman_session_state {
	CONNMAN_SESSION_STATE_DISCONNECTED   = 0,
	CONNMAN_SESSION_STATE_CONNECTED      = 1,
	CONNMAN_SESSION_STATE_ONLINE         = 2,
};

struct session_info {
	struct connman_session_config config;
	enum connman_session_state state;
};

struct connman_session {
	char *owner;
	char *session_path;
	char *notify_path;
	guint notify_watch;

57
	bool active;
58
	bool append_all;
59 60
	struct session_info *info;
	struct session_info *info_last;
61 62
	struct connman_service *service;
	struct connman_service *service_last;
63 64 65
	struct connman_session_config *policy_config;
	GSList *user_allowed_bearers;

66
	bool ecall;
67

68 69
	enum connman_session_id_type id_type;
	struct firewall_context *fw;
70
	int snat_id;
71 72 73
	uint32_t mark;
	int index;
	char *gateway;
74
	bool policy_routing;
75 76
};

77 78 79 80
struct connman_service_info {
	struct connman_service *service;
	GSList *sessions;
};
81

82 83 84 85
static struct connman_session_policy *policy;
static void session_activate(struct connman_session *session);
static void session_deactivate(struct connman_session *session);
static void update_session_state(struct connman_session *session);
86

87
static void cleanup_service(gpointer data)
88
{
89
	struct connman_service_info *info = data;
90

91 92
	g_slist_free(info->sessions);
	g_free(info);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

static const char *state2string(enum connman_session_state state)
{
	switch (state) {
	case CONNMAN_SESSION_STATE_DISCONNECTED:
		return "disconnected";
	case CONNMAN_SESSION_STATE_CONNECTED:
		return "connected";
	case CONNMAN_SESSION_STATE_ONLINE:
		return "online";
	}

	return NULL;
}

static const char *type2string(enum connman_session_type type)
{
	switch (type) {
	case CONNMAN_SESSION_TYPE_UNKNOWN:
		return "";
	case CONNMAN_SESSION_TYPE_ANY:
		return "any";
	case CONNMAN_SESSION_TYPE_LOCAL:
		return "local";
	case CONNMAN_SESSION_TYPE_INTERNET:
		return "internet";
	}

	return NULL;
}

enum connman_session_roaming_policy connman_session_parse_roaming_policy(const char *policy)
{
	if (g_strcmp0(policy, "default") == 0)
		return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
	else if (g_strcmp0(policy, "always") == 0)
		return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
	else if (g_strcmp0(policy, "forbidden") == 0)
		return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
	else if (g_strcmp0(policy, "national") == 0)
		return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
	else if (g_strcmp0(policy, "international") == 0)
		return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
	else
		return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
}

enum connman_session_type connman_session_parse_connection_type(const char *type)
{
	if (g_strcmp0(type, "any") == 0)
		return CONNMAN_SESSION_TYPE_ANY;
	if (g_strcmp0(type, "local") == 0)
		return CONNMAN_SESSION_TYPE_LOCAL;
	else if (g_strcmp0(type, "internet") == 0)
		return CONNMAN_SESSION_TYPE_INTERNET;

	return CONNMAN_SESSION_TYPE_UNKNOWN;
}

static int bearer2service(const char *bearer, enum connman_service_type *type)
{
	if (g_strcmp0(bearer, "ethernet") == 0)
		*type = CONNMAN_SERVICE_TYPE_ETHERNET;
	else if (g_strcmp0(bearer, "wifi") == 0)
		*type = CONNMAN_SERVICE_TYPE_WIFI;
159 160
	else if (g_strcmp0(bearer, "gadget") == 0)
		*type = CONNMAN_SERVICE_TYPE_GADGET;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	else if (g_strcmp0(bearer, "bluetooth") == 0)
		*type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
	else if (g_strcmp0(bearer, "cellular") == 0)
		*type = CONNMAN_SERVICE_TYPE_CELLULAR;
	else if (g_strcmp0(bearer, "vpn") == 0)
		*type = CONNMAN_SERVICE_TYPE_VPN;
	else
		return -EINVAL;

	return 0;
}

static char *service2bearer(enum connman_service_type type)
{
	switch (type) {
	case CONNMAN_SERVICE_TYPE_ETHERNET:
		return "ethernet";
178 179
	case CONNMAN_SERVICE_TYPE_GADGET:
		return "gadget";
180 181 182 183 184 185 186 187 188 189
	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_VPN:
		return "vpn";
	case CONNMAN_SERVICE_TYPE_SYSTEM:
	case CONNMAN_SERVICE_TYPE_GPS:
190
	case CONNMAN_SERVICE_TYPE_P2P:
191
	case CONNMAN_SERVICE_TYPE_UNKNOWN:
192 193 194 195 196 197
		return "";
	}

	return "";
}

198 199 200 201 202 203 204 205 206 207
static int init_firewall(void)
{
	struct firewall_context *fw;
	int err;

	if (global_firewall)
		return 0;

	fw = __connman_firewall_create();

208 209
	err = __connman_firewall_add_rule(fw, NULL, NULL, "mangle", "INPUT",
				"-j CONNMARK --restore-mark");
210 211 212
	if (err < 0)
		goto err;

213 214
	err = __connman_firewall_add_rule(fw, NULL, NULL, "mangle",
				"POSTROUTING","-j CONNMARK --save-mark");
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	if (err < 0)
		goto err;

	err = __connman_firewall_enable(fw);
	if (err < 0)
		goto err;

	global_firewall = fw;

	return 0;

err:
	__connman_firewall_destroy(fw);

	return err;
}

static void cleanup_firewall(void)
{
	if (!global_firewall)
		return;

	__connman_firewall_disable(global_firewall);
	__connman_firewall_destroy(global_firewall);
}

static int init_firewall_session(struct connman_session *session)
{
	struct firewall_context *fw;
	int err;

	if (session->policy_config->id_type == CONNMAN_SESSION_ID_TYPE_UNKNOWN)
		return 0;

	DBG("");

	err = init_firewall();
	if (err < 0)
		return err;

	fw = __connman_firewall_create();
	if (!fw)
		return -ENOMEM;

	switch (session->policy_config->id_type) {
	case CONNMAN_SESSION_ID_TYPE_UID:
261 262
		err = __connman_firewall_add_rule(fw, NULL, NULL, "mangle",
				"OUTPUT",
263 264 265 266 267
				"-m owner --uid-owner %s -j MARK --set-mark %d",
						session->policy_config->id,
						session->mark);
		break;
	case CONNMAN_SESSION_ID_TYPE_GID:
268 269
		err = __connman_firewall_add_rule(fw, NULL, NULL, "mangle",
				"OUTPUT",
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
				"-m owner --gid-owner %s -j MARK --set-mark %d",
						session->policy_config->id,
						session->mark);
		break;
	case CONNMAN_SESSION_ID_TYPE_LSM:
	default:
		err = -EINVAL;
	}

	if (err < 0)
		goto err;

	session->id_type = session->policy_config->id_type;

	err = __connman_firewall_enable(fw);
	if (err)
		goto err;

	session->fw = fw;

	return 0;

err:
	__connman_firewall_destroy(fw);

	return err;
}

static void cleanup_firewall_session(struct connman_session *session)
{
	if (!session->fw)
		return;

	__connman_firewall_disable(session->fw);
	__connman_firewall_destroy(session->fw);

	session->fw = NULL;
}

static int init_routing_table(struct connman_session *session)
{
	int err;

	if (session->policy_config->id_type == CONNMAN_SESSION_ID_TYPE_UNKNOWN)
		return 0;

	DBG("");

	err = __connman_inet_add_fwmark_rule(session->mark,
						AF_INET, session->mark);
	if (err < 0)
		return err;

	err = __connman_inet_add_fwmark_rule(session->mark,
						AF_INET6, session->mark);
	if (err < 0)
		__connman_inet_del_fwmark_rule(session->mark,
						AF_INET, session->mark);
328
	session->policy_routing = true;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

	return err;
}

static void del_default_route(struct connman_session *session)
{
	if (!session->gateway)
		return;

	DBG("index %d routing table %d default gateway %s",
		session->index, session->mark, session->gateway);

	__connman_inet_del_default_from_table(session->mark,
					session->index, session->gateway);
	g_free(session->gateway);
	session->gateway = NULL;
	session->index = -1;
}

static void add_default_route(struct connman_session *session)
{
	struct connman_ipconfig *ipconfig;
	int err;

	if (!session->service)
		return;

	ipconfig = __connman_service_get_ip4config(session->service);
	session->index = __connman_ipconfig_get_index(ipconfig);
	session->gateway = g_strdup(__connman_ipconfig_get_gateway(ipconfig));

	DBG("index %d routing table %d default gateway %s",
		session->index, session->mark, session->gateway);

	err = __connman_inet_add_default_to_table(session->mark,
					session->index, session->gateway);
	if (err < 0)
		DBG("session %p %s", session, strerror(-err));
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
static void del_nat_rules(struct connman_session *session)
{
	int err;

	if (!session->fw || session->snat_id == 0)
		return;

	err = __connman_firewall_disable_rule(session->fw, session->snat_id);
	if (err < 0) {
		DBG("could not disable SNAT rule");
		return;
	}

	err = __connman_firewall_remove_rule(session->fw, session->snat_id);
	if (err < 0)
		DBG("could not remove SNAT rule");


	session->snat_id = 0;
}

static void add_nat_rules(struct connman_session *session)
{
	struct connman_ipconfig *ipconfig;
	const char *addr;
	char *ifname;
	int index, id, err;

	if (!session->fw)
		return;

	DBG("");

	ipconfig = __connman_service_get_ip4config(session->service);
	index = __connman_ipconfig_get_index(ipconfig);
	ifname = connman_inet_ifname(index);
	addr = __connman_ipconfig_get_local(ipconfig);

407
	id = __connman_firewall_add_rule(session->fw, NULL, NULL, "nat",
408
				"POSTROUTING", "-o %s -j SNAT --to-source %s",
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
				ifname, addr);
	g_free(ifname);
	if (id < 0) {
		DBG("failed to add SNAT rule");
		return;
	}

	err = __connman_firewall_enable_rule(session->fw, id);
	if (err < 0) {
		DBG("could not enable SNAT rule");
		__connman_firewall_remove_rule(session->fw, id);
		return;
	}

	session->snat_id = id;
}

426 427 428 429
static void cleanup_routing_table(struct connman_session *session)
{
	DBG("");

430 431
	if (session->policy_routing) {
		__connman_inet_del_fwmark_rule(session->mark,
432 433
					AF_INET6, session->mark);

434
		__connman_inet_del_fwmark_rule(session->mark,
435
					AF_INET, session->mark);
436 437
		session->policy_routing = false;
	}
438 439 440 441 442 443 444 445 446 447

	del_default_route(session);
}

static void update_routing_table(struct connman_session *session)
{
	del_default_route(session);
	add_default_route(session);
}

448 449 450 451 452 453
static void update_nat_rules(struct connman_session *session)
{
	del_nat_rules(session);
	add_nat_rules(session);
}

454 455
static void destroy_policy_config(struct connman_session *session)
{
456
	if (!policy) {
457 458 459 460
		g_free(session->policy_config);
		return;
	}

461
	policy->destroy(session);
462 463 464 465
}

static void free_session(struct connman_session *session)
{
466
	if (!session)
467 468 469 470 471 472 473 474 475 476 477 478
		return;

	if (session->notify_watch > 0)
		g_dbus_remove_watch(connection, session->notify_watch);

	destroy_policy_config(session);
	g_slist_free(session->info->config.allowed_bearers);
	g_free(session->owner);
	g_free(session->session_path);
	g_free(session->notify_path);
	g_free(session->info);
	g_free(session->info_last);
479
	g_free(session->gateway);
480 481 482 483

	g_free(session);
}

484 485 486 487 488 489 490 491 492 493 494
static void set_active_session(struct connman_session *session, bool enable)
{

	if (policy && policy->session_changed)
		policy->session_changed(session, enable,
					session->info->config.allowed_bearers);

	__connman_service_set_active_session(enable,
				session->info->config.allowed_bearers);
}

495 496 497 498 499 500 501 502 503
static void cleanup_session(gpointer user_data)
{
	struct connman_session *session = user_data;

	DBG("remove %s", session->session_path);

	cleanup_routing_table(session);
	cleanup_firewall_session(session);

504
	if (session->active)
505
		set_active_session(session, false);
506

507 508
	session_deactivate(session);
	update_session_state(session);
509

510
	g_slist_free(session->user_allowed_bearers);
511

512
	free_session(session);
513 514
}

515
struct creation_data {
516
	DBusMessage *pending;
517
	struct connman_session *session;
518

519
	/* user config */
520 521 522 523
	enum connman_session_type type;
	GSList *allowed_bearers;
};

524
static void cleanup_creation_data(struct creation_data *creation_data)
525
{
526
	if (!creation_data)
527 528
		return;

529 530
	if (creation_data->pending)
		dbus_message_unref(creation_data->pending);
531

532 533
	g_slist_free(creation_data->allowed_bearers);
	g_free(creation_data);
534 535 536 537
}

static int create_policy_config(struct connman_session *session,
				connman_session_config_func_t cb,
538
				struct creation_data *creation_data)
539 540 541
{
	struct connman_session_config *config;

542
	if (!policy) {
543
		config = connman_session_create_default_config();
544
		if (!config) {
545
			free_session(session);
546
			cleanup_creation_data(creation_data);
547 548 549
			return -ENOMEM;
		}

550
		return cb(session, config, creation_data, 0);
551 552
	}

553
	return policy->create(session, cb, creation_data);
554 555
}

556
int connman_session_policy_register(struct connman_session_policy *plugin)
557
{
558 559
	if (policy)
		return -EINVAL;
560

561 562
	if (!plugin->create || !plugin->destroy)
		return -EINVAL;
563

564
	DBG("name %s", plugin->name);
565

566
	policy = plugin;
567

568
	return 0;
569 570
}

571
void connman_session_policy_unregister(struct connman_session_policy *plugin)
572
{
573 574
	if (plugin != policy)
		return;
575 576 577

	DBG("name %s", policy->name);

578
	policy = NULL;
579 580
}

581 582 583 584 585 586 587
static int default_bearers[] = {
	CONNMAN_SERVICE_TYPE_ETHERNET,
	CONNMAN_SERVICE_TYPE_WIFI,
	CONNMAN_SERVICE_TYPE_BLUETOOTH,
	CONNMAN_SERVICE_TYPE_CELLULAR,
	CONNMAN_SERVICE_TYPE_GADGET,
};
588

589 590 591
static void add_default_bearer_types(GSList **list)
{
	unsigned int i;
592

593 594 595
	for (i = 0; i < G_N_ELEMENTS(default_bearers); i++)
		*list = g_slist_append(*list,
				GINT_TO_POINTER(default_bearers[i]));
596 597 598 599
}

void connman_session_set_default_config(struct connman_session_config *config)
{
600 601 602 603
	config->id_type = CONNMAN_SESSION_ID_TYPE_UNKNOWN;
	g_free(config->id);
	config->id = NULL;

604 605 606 607 608 609
	config->priority = FALSE;
	config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
	config->type = CONNMAN_SESSION_TYPE_ANY;
	config->ecall = FALSE;

	g_slist_free(config->allowed_bearers);
610
	add_default_bearer_types(&config->allowed_bearers);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
}

struct connman_session_config *connman_session_create_default_config(void)
{
	struct connman_session_config *config;

	config = g_new0(struct connman_session_config, 1);
	connman_session_set_default_config(config);

	return config;
}

static enum connman_session_type apply_policy_on_type(
			enum connman_session_type policy,
			enum connman_session_type type)
{
	if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
		return CONNMAN_SESSION_TYPE_UNKNOWN;

	if (policy == CONNMAN_SESSION_TYPE_ANY)
		return type;

	if (policy == CONNMAN_SESSION_TYPE_LOCAL)
		return CONNMAN_SESSION_TYPE_LOCAL;

	return CONNMAN_SESSION_TYPE_INTERNET;
}

int connman_session_parse_bearers(const char *token, GSList **list)
{
	enum connman_service_type bearer;
	int err;

	if (g_strcmp0(token, "") == 0)
		return 0;

647 648 649 650 651
	if (g_strcmp0(token, "*") == 0) {
		add_default_bearer_types(list);
		return 0;
	}

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	err = bearer2service(token, &bearer);
	if (err < 0)
		return err;

	*list = g_slist_append(*list, GINT_TO_POINTER(bearer));

	return 0;
}

static int parse_bearers(DBusMessageIter *iter, GSList **list)
{
	DBusMessageIter array;
	int type, err;

	dbus_message_iter_recurse(iter, &array);

	*list = NULL;

	while ((type = dbus_message_iter_get_arg_type(&array)) !=
			DBUS_TYPE_INVALID) {
		char *bearer_name = NULL;

		if (type != DBUS_TYPE_STRING) {
			g_slist_free(*list);
			*list = NULL;
			return -EINVAL;
		}

		dbus_message_iter_get_basic(&array, &bearer_name);

		err = connman_session_parse_bearers(bearer_name, list);
		if (err < 0) {
			g_slist_free(*list);
			*list = NULL;
			return err;
		}

		dbus_message_iter_next(&array);
	}

	return 0;
}

695
static void filter_bearer(GSList *policy_bearers,
696 697 698 699 700 701
				enum connman_service_type bearer,
				GSList **list)
{
	enum connman_service_type policy;
	GSList *it;

702
	if (!policy_bearers)
703
		return;
704

705
	for (it = policy_bearers; it; it = it->next) {
706 707
		policy = GPOINTER_TO_INT(it->data);

708
		if (policy != bearer)
709 710
			continue;

711 712
		*list = g_slist_append(*list, GINT_TO_POINTER(bearer));
		return;
713 714 715
	}
}

716
static void apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
717 718 719 720 721 722 723
				GSList **list)
{
	enum connman_service_type bearer;
	GSList *it;

	*list = NULL;

724
	for (it = bearers; it; it = it->next) {
725 726
		bearer = GPOINTER_TO_INT(it->data);

727
		filter_bearer(policy_bearers, bearer, list);
728 729 730 731 732 733 734 735 736 737 738 739 740 741
	}
}

const char *connman_session_get_owner(struct connman_session *session)
{
	return session->owner;
}

static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
{
	struct session_info *info = user_data;
	GSList *list;

	for (list = info->config.allowed_bearers;
742
			list; list = list->next) {
743 744 745
		enum connman_service_type bearer = GPOINTER_TO_INT(list->data);
		const char *name = __connman_service_type2string(bearer);

746
		if (!name)
747
			name = "";
748 749 750 751 752 753 754 755 756 757 758

		dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
						&name);
	}
}

static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
{
	struct connman_service *service = user_data;
	struct connman_ipconfig *ipconfig_ipv4;

759
	if (!service)
760 761
		return;

762 763
	if (!__connman_service_is_connected_state(service,
						CONNMAN_IPCONFIG_TYPE_IPV4))
764 765 766
		return;

	ipconfig_ipv4 = __connman_service_get_ip4config(service);
767
	if (!ipconfig_ipv4)
768 769 770 771 772 773 774 775 776 777
		return;

	__connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
}

static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
{
	struct connman_service *service = user_data;
	struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;

778
	if (!service)
779 780
		return;

781 782
	if (!__connman_service_is_connected_state(service,
						CONNMAN_IPCONFIG_TYPE_IPV6))
783 784 785 786
		return;

	ipconfig_ipv4 = __connman_service_get_ip4config(service);
	ipconfig_ipv6 = __connman_service_get_ip6config(service);
787
	if (!ipconfig_ipv6)
788 789 790 791 792 793 794 795 796 797 798
		return;

	__connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
}

static void append_notify(DBusMessageIter *dict,
					struct connman_session *session)
{
	struct session_info *info = session->info;
	struct session_info *info_last = session->info_last;
	struct connman_service *service;
799 800 801 802
	enum connman_service_type type;
	const char *name, *bearer;
	char *ifname;
	int idx;
803

804
	if (session->append_all || info->state != info_last->state) {
805 806 807 808 809 810 811 812
		const char *state = state2string(info->state);

		connman_dbus_dict_append_basic(dict, "State",
						DBUS_TYPE_STRING,
						&state);
		info_last->state = info->state;
	}

813 814 815 816 817 818 819 820 821 822 823 824 825
	if (session->append_all || session->service != session->service_last) {
		if (session->service) {
			service = session->service;
			name = __connman_service_get_name(service);
			idx = __connman_service_get_index(service);

			ifname = connman_inet_ifname(idx);
			if (!ifname)
				ifname = g_strdup("");

			type = connman_service_get_type(service);
			bearer = service2bearer(type);
		} else {
826
			service = NULL;
827 828
			name = "";
			ifname = g_strdup("");
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
			bearer = "";
		}

		connman_dbus_dict_append_basic(dict, "Name",
						DBUS_TYPE_STRING,
						&name);

		connman_dbus_dict_append_dict(dict, "IPv4",
						append_ipconfig_ipv4,
						service);

		connman_dbus_dict_append_dict(dict, "IPv6",
						append_ipconfig_ipv6,
						service);

		connman_dbus_dict_append_basic(dict, "Interface",
						DBUS_TYPE_STRING,
						&ifname);

		connman_dbus_dict_append_basic(dict, "Bearer",
						DBUS_TYPE_STRING,
						&bearer);

852 853 854
		g_free(ifname);

		session->service_last = session->service;
855 856
	}

857
	if (session->append_all ||
858 859 860 861 862 863 864 865 866
			info->config.type != info_last->config.type) {
		const char *type = type2string(info->config.type);

		connman_dbus_dict_append_basic(dict, "ConnectionType",
						DBUS_TYPE_STRING,
						&type);
		info_last->config.type = info->config.type;
	}

867
	if (session->append_all ||
868 869 870 871 872 873 874 875
			info->config.allowed_bearers != info_last->config.allowed_bearers) {
		connman_dbus_dict_append_array(dict, "AllowedBearers",
						DBUS_TYPE_STRING,
						append_allowed_bearers,
						info);
		info_last->config.allowed_bearers = info->config.allowed_bearers;
	}

876
	session->append_all = false;
877 878
}

879
static bool compute_notifiable_changes(struct connman_session *session)
880 881 882 883
{
	struct session_info *info_last = session->info_last;
	struct session_info *info = session->info;

884 885
	if (session->append_all)
		return true;
886 887

	if (info->state != info_last->state)
888
		return true;
889

890
	if (session->service != session->service_last &&
891
			info->state >= CONNMAN_SESSION_STATE_CONNECTED)
892
		return true;
893 894 895

	if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
			info->config.type != info_last->config.type)
896
		return true;
897

898
	return false;
899 900 901 902 903 904 905 906
}

static gboolean session_notify(gpointer user_data)
{
	struct connman_session *session = user_data;
	DBusMessage *msg;
	DBusMessageIter array, dict;

907
	if (!compute_notifiable_changes(session))
908 909 910 911 912 913 914 915
		return FALSE;

	DBG("session %p owner %s notify_path %s", session,
		session->owner, session->notify_path);

	msg = dbus_message_new_method_call(session->owner, session->notify_path,
						CONNMAN_NOTIFICATION_INTERFACE,
						"Update");
916
	if (!msg)
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
		return FALSE;

	dbus_message_iter_init_append(msg, &array);
	connman_dbus_dict_open(&array, &dict);

	append_notify(&dict, session);

	connman_dbus_dict_close(&array, &dict);

	g_dbus_send_message(connection, msg);

	return FALSE;
}

static void ipconfig_ipv4_changed(struct connman_session *session)
{
	connman_dbus_setting_changed_dict(session->owner, session->notify_path,
						"IPv4", append_ipconfig_ipv4,
935
						session->service);
936 937 938 939 940 941
}

static void ipconfig_ipv6_changed(struct connman_session *session)
{
	connman_dbus_setting_changed_dict(session->owner, session->notify_path,
						"IPv6", append_ipconfig_ipv6,
942
						session->service);
943 944
}

945
int connman_session_config_update(struct connman_session *session)
946 947
{
	struct session_info *info = session->info;
948 949
	GSList *allowed_bearers;
	int err;
950

951
	DBG("session %p", session);
952

953 954 955 956
	/*
	 * We update all configuration even though only one entry
	 * might have changed. We can still optimize this later.
	 */
957

958 959 960 961 962 963 964 965 966
	if (session->id_type != session->policy_config->id_type) {
		cleanup_firewall_session(session);
		err = init_firewall_session(session);
		if (err < 0) {
			connman_session_destroy(session);
			return err;
		}

		session->id_type = session->policy_config->id_type;
967 968
	}

969 970 971 972
	apply_policy_on_bearers(
		session->policy_config->allowed_bearers,
		session->user_allowed_bearers,
		&allowed_bearers);
973

974
	if (session->active)
975
		set_active_session(session, false);
976

977 978
	session->active = false;
	session_deactivate(session);
979

980 981
	g_slist_free(info->config.allowed_bearers);
	info->config.allowed_bearers = allowed_bearers;
982

983
	session_activate(session);
984

985 986 987
	info->config.type = apply_policy_on_type(
				session->policy_config->type,
				info->config.type);
988

989
	info->config.roaming_policy = session->policy_config->roaming_policy;
990

991 992 993
	info->config.ecall = session->policy_config->ecall;
	if (info->config.ecall)
		ecall_session = session;
994

995
	info->config.priority = session->policy_config->priority;
996

997
	session_notify(session);
998

999 1000
	return 0;
}
1001

1002 1003 1004 1005
static DBusMessage *connect_session(DBusConnection *conn,
					DBusMessage *msg, void *user_data)
{
	struct connman_session *session = user_data;
1006

1007
	DBG("session %p", session);
1008

1009 1010 1011
	if (ecall_session) {
		if (ecall_session->ecall && ecall_session != session)
			return __connman_error_failed(msg, EBUSY);
1012

1013 1014
		session->ecall = true;
	}
1015

1016 1017
	if (!session->active) {
		session->active = true;
1018
		set_active_session(session, true);
1019 1020
	}

1021 1022
	session_activate(session);

1023 1024 1025
	__connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_SESSION);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1026 1027
}

1028 1029
static DBusMessage *disconnect_session(DBusConnection *conn,
					DBusMessage *msg, void *user_data)
1030 1031 1032
{
	struct connman_session *session = user_data;

1033
	DBG("session %p", session);
1034

1035 1036 1037 1038 1039
	if (ecall_session) {
		if (ecall_session->ecall && ecall_session != session)
			return __connman_error_failed(msg, EBUSY);

		session->ecall = false;
1040 1041
	}

1042 1043
	if (session->active) {
		session->active = false;
1044
		set_active_session(session, false);
1045 1046 1047 1048 1049 1050
	}

	session_deactivate(session);
	update_session_state(session);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1051 1052
}

1053 1054
static DBusMessage *change_session(DBusConnection *conn,
					DBusMessage *msg, void *user_data)
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
{
	struct connman_session *session = user_data;
	struct session_info *info = session->info;
	DBusMessageIter iter, value;
	const char *name;
	const char *val;
	GSList *allowed_bearers;
	int err;

	DBG("session %p", session);
1065
	if (!dbus_message_iter_init(msg, &iter))
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		return __connman_error_invalid_arguments(msg);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		return __connman_error_invalid_arguments(msg);

	dbus_message_iter_get_basic(&iter, &name);
	dbus_message_iter_next(&iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
		return __connman_error_invalid_arguments(msg);

	dbus_message_iter_recurse(&iter, &value);

	switch (dbus_message_iter_get_arg_type(&value)) {
	case DBUS_TYPE_ARRAY:
1081
		if (g_str_equal(name, "AllowedBearers")) {
1082 1083
			err = parse_bearers(&value, &allowed_bearers);
			if (err < 0)
1084 1085
				return __connman_error_failed(msg, -err);

1086
			if (session->active)
1087
				set_active_session(session, false);
1088 1089 1090

			session->active = false;
			session_deactivate(session);
1091 1092 1093 1094

			g_slist_free(info->config.allowed_bearers);
			session->user_allowed_bearers = allowed_bearers;

1095
			apply_policy_on_bearers(
1096 1097 1098 1099
					session->policy_config->allowed_bearers,
					session->user_allowed_bearers,
					&info->config.allowed_bearers);

1100
			session_activate(session);
1101 1102 1103 1104 1105
		} else {
			goto err;
		}
		break;
	case DBUS_TYPE_STRING:
1106
		if (g_str_equal(name, "ConnectionType")) {
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
			dbus_message_iter_get_basic(&value, &val);
			info->config.type = apply_policy_on_type(
				session->policy_config->type,
				connman_session_parse_connection_type(val));
		} else {
			goto err;
		}
		break;
	default:
		goto err;
	}

1119
	session_notify(session);
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);

err:
	return __connman_error_invalid_arguments(msg);
}

static void release_session(gpointer key, gpointer value, gpointer user_data)
{
	struct connman_session *session = value;
	DBusMessage *message;

	DBG("owner %s path %s", session->owner, session->notify_path);

	if (session->notify_watch > 0)
		g_dbus_remove_watch(connection, session->notify_watch);

	g_dbus_unregister_interface(connection, session->session_path,
						CONNMAN_SESSION_INTERFACE);

	message = dbus_message_new_method_call(session->owner,
						session->notify_path,
						CONNMAN_NOTIFICATION_INTERFACE,
						"Release");
1144
	if (!message)
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		return;

	dbus_message_set_no_reply(message, TRUE);

	g_dbus_send_message(connection, message);
}

static int session_disconnect(struct connman_session *session)
{
	DBG("session %p, %s", session, session->owner);

	if (session->notify_watch > 0)
		g_dbus_remove_watch(connection, session->notify_watch);

	g_dbus_unregister_interface(connection, session->session_path,
						CONNMAN_SESSION_INTERFACE);

	g_hash_table_remove(session_hash, session->session_path);

	return 0;
}

static void owner_disconnect(DBusConnection *conn, void *user_data)
{
	struct connman_session *session = user_data;

	DBG("session %p, %s died", session, session->owner);

	session_disconnect(session);
}

static DBusMessage *destroy_session(DBusConnection *conn,
					DBusMessage *msg, void *user_data)
{
	struct connman_session *session = user_data;

	DBG("session %p", session);

1183
	if (ecall_session && ecall_session != session)
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
		return __connman_error_failed(msg, EBUSY);

	session_disconnect(session);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static const GDBusMethodTable session_methods[] = {
	{ GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
	{ GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
	{ GDBUS_METHOD("Disconnect", NULL, NULL,
			disconnect_session ) },
	{ GDBUS_METHOD("Change",
			GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
			NULL, change_session) },
	{ },
};

1202 1203 1204
static int session_policy_config_cb(struct connman_session *session,
				struct connman_session_config *config,
				void *user_data, int err)
1205
{
1206 1207 1208
	struct creation_data *creation_data = user_data;
	struct session_info *info, *info_last;
	DBusMessage *reply;
1209

1210
	DBG("session %p config %p", session, config);
1211

1212 1213
	if (err < 0)
		goto err;
1214

1215
	session->policy_config = config;
1216

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	session->mark = session_mark++;
	session->index = -1;

	err = init_firewall_session(session);
	if (err < 0)
		goto err;

	err = init_routing_table(session);
	if (err < 0)
		goto err;
1227 1228 1229 1230

	info = session->info;
	info_last = session->info_last;

1231
	if (session->policy_config->ecall)
1232 1233 1234 1235 1236
		ecall_session = session;

	info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
	info->config.type = apply_policy_on_type(
				session->policy_config->type,
1237
				creation_data->type);
1238 1239 1240
	info->config.priority = session->policy_config->priority;
	info->config.roaming_policy = session->policy_config->roaming_policy;

1241 1242
	session->user_allowed_bearers = creation_data->allowed_bearers;
	creation_data->allowed_bearers = NULL;
1243

1244
	apply_policy_on_bearers(
1245 1246 1247 1248 1249 1250 1251 1252
			session->policy_config->allowed_bearers,
			session->user_allowed_bearers,
			&info->config.allowed_bearers);

	g_hash_table_replace(session_hash, session->session_path, session);

	DBG("add %s", session->session_path);

1253
	if (!g_dbus_register_interface(connection, session->session_path,
1254
					CONNMAN_SESSION_INTERFACE,
1255 1256
					session_methods, NULL, NULL,
					session, NULL)) {
1257 1258 1259
		connman_error("Failed to register %s", session->session_path);
		g_hash_table_remove(session_hash, session->session_path);
		err = -EINVAL;
1260
		goto err;
1261 1262
	}

1263
	reply = g_dbus_create_reply(creation_data->pending,
1264 1265 1266
				DBUS_TYPE_OBJECT_PATH, &session->session_path,
				DBUS_TYPE_INVALID);
	g_dbus_send_message(connection, reply);
1267
	creation_data->pending = NULL;
1268 1269 1270 1271 1272 1273

	info_last->state = info->state;
	info_last->config.priority = info->config.priority;
	info_last->config.roaming_policy = info->config.roaming_policy;
	info_last->config.allowed_bearers = info->config.allowed_bearers;

1274
	session->append_all = true;
1275

1276 1277
	cleanup_creation_data(creation_data);

1278
	session_activate(session);
1279

1280
	return 0;
1281 1282 1283 1284 1285 1286 1287 1288

err:
	reply = __connman_error_failed(creation_data->pending, -err);
	g_dbus_send_message(connection, reply);
	creation_data->pending = NULL;

	cleanup_session(session);
	cleanup_creation_data(creation_data);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298

	return err;
}

int __connman_session_create(DBusMessage *msg)
{
	const char *owner, *notify_path;
	char *session_path = NULL;
	DBusMessageIter iter, array;
	struct connman_session *session = NULL;
1299 1300 1301 1302 1303
	struct creation_data *creation_data = NULL;
	bool user_allowed_bearers = false;
	bool user_connection_type = false;
	int err, i;
	char *str;
1304 1305 1306 1307 1308

	owner = dbus_message_get_sender(msg);

	DBG("owner %s", owner);

1309
	if (ecall_session && ecall_session->ecall) {
1310 1311 1312 1313 1314 1315 1316 1317
		/*
		 * If there is an emergency call already going on,
		 * ignore session creation attempt
		 */
		err = -EBUSY;
		goto err;
	}

1318 1319
	creation_data = g_try_new0(struct creation_data, 1);
	if (!creation_data) {
1320 1321 1322 1323
		err = -ENOMEM;
		goto err;
	}

1324
	creation_data->pending = dbus_message_ref(msg);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340

	dbus_message_iter_init(msg, &iter);
	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *key, *val;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &key);

		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &value);

		switch (dbus_message_iter_get_arg_type(&value)) {
		case DBUS_TYPE_ARRAY:
1341
			if (g_str_equal(key, "AllowedBearers")) {
1342
				err = parse_bearers(&value,
1343
					&creation_data->allowed_bearers);
1344 1345 1346
				if (err < 0)
					goto err;

1347
				user_allowed_bearers = true;
1348 1349 1350 1351 1352 1353
			} else {
				err = -EINVAL;
				goto err;
			}
			break;
		case DBUS_TYPE_STRING:
1354
			if (g_str_equal(key, "ConnectionType")) {
1355
				dbus_message_iter_get_basic(&value, &val);
1356
				creation_data->type =
1357 1358
					connman_session_parse_connection_type(val);

1359
				user_connection_type = true;
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
			} else {
				err = -EINVAL;
				goto err;
			}
		}
		dbus_message_iter_next(&array);
	}

	/*
	 * If the user hasn't provided a configuration, we set
	 * the default configuration.
	 *
	 * For AllowedBearers this is '*', ...
	 */
1374
	if (!user_allowed_bearers) {
1375
		add_default_bearer_types(&creation_data->allowed_bearers);
1376
		if (!creation_data->allowed_bearers) {
1377 1378 1379 1380 1381 1382
			err = -ENOMEM;
			goto err;
		}
	}

	/* ... and for ConnectionType it is 'any'. */
1383 1384
	if (!user_connection_type)
		creation_data->type = CONNMAN_SESSION_TYPE_ANY;
1385 1386 1387 1388

	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, &notify_path);

1389
	if (!notify_path) {
1390 1391 1392 1393
		err = -EINVAL;
		goto err;
	}

1394 1395 1396 1397 1398 1399 1400 1401
	str = g_strdup(owner);
	for (i = 0; str[i] != '\0'; i++)
		if (str[i] == ':' || str[i] == '.')
			str[i] = '_';
	session_path = g_strdup_printf("/sessions/%s%s", str, notify_path);
	g_free(str);

	if (!session_path) {
1402 1403 1404 1405 1406
		err = -ENOMEM;
		goto err;
	}

	session = g_hash_table_lookup(session_hash, session_path);
1407
	if (session) {
1408 1409 1410 1411 1412 1413 1414
		g_free(session_path);
		session = NULL;
		err = -EEXIST;
		goto err;
	}

	session = g_try_new0(struct connman_session, 1);
1415
	if (!session) {
1416 1417 1418 1419 1420
		g_free(session_path);
		err = -ENOMEM;
		goto err;
	}

1421
	creation_data->session = session;
1422 1423 1424
	session->session_path = session_path;

	session->info = g_try_new0(struct session_info, 1);
1425
	if (!session->info) {
1426 1427 1428 1429 1430
		err = -ENOMEM;
		goto err;
	}

	session->info_last = g_try_new0(struct session_info, 1);
1431
	if (!session->info_last) {
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
		err = -ENOMEM;
		goto err;
	}

	session->owner = g_strdup(owner);
	session->notify_path = g_strdup(notify_path);
	session->notify_watch =
		g_dbus_add_disconnect_watch(connection, session->owner,
					owner_disconnect, session, NULL);

1442 1443
	err = create_policy_config(session, session_policy_config_cb,
					creation_data);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	if (err < 0 && err != -EINPROGRESS)
		return err;

	return -EINPROGRESS;

err:
	connman_error("Failed to create session");

	free_session(session);

1454
	cleanup_creation_data(creation_data);
1455 1456 1457
	return err;
}

1458 1459 1460 1461 1462 1463 1464 1465
bool __connman_session_policy_autoconnect(enum connman_service_connect_reason reason)
{
	if (!policy || !policy->autoconnect)
		return true;

	return policy->autoconnect(reason);
}

1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
void connman_session_destroy(struct connman_session *session)
{
	DBG("session %p", session);

	session_disconnect(session);
}

int __connman_session_destroy(DBusMessage *msg)
{
	const char *owner, *session_path;
	struct connman_session *session;

	owner = dbus_message_get_sender(msg);

	DBG("owner %s", owner);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
							DBUS_TYPE_INVALID);
1484
	if (!session_path)
1485 1486 1487
		return -EINVAL;

	session = g_hash_table_lookup(session_hash, session_path);
1488
	if (!session)
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
		return -EINVAL;

	if (g_strcmp0(owner, session->owner) != 0)
		return -EACCES;

	connman_session_destroy(session);

	return 0;
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
int connman_session_connect(struct connman_service *service)
{
	DBG("service %p name %s", service, __connman_service_get_name(service));

	return __connman_service_connect(service,
				CONNMAN_SERVICE_CONNECT_REASON_SESSION);
}

int connman_session_disconnect(struct connman_service *service)
{
	DBG("service %p", service);

	return __connman_service_disconnect(service);
}

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
static enum connman_session_state service_to_session_state(
					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:
		return CONNMAN_SESSION_STATE_CONNECTED;
	case CONNMAN_SERVICE_STATE_ONLINE:
		return CONNMAN_SESSION_STATE_ONLINE;
	}

	return CONNMAN_SESSION_STATE_DISCONNECTED;
}

static void update_session_state(struct connman_session *session)
{
	enum connman_service_state service_state;
	enum connman_session_state state = CONNMAN_SESSION_STATE_DISCONNECTED;

	if (session->service) {
1540
		service_state = connman_service_get_state(session->service);
1541 1542 1543 1544 1545 1546 1547 1548
		state = service_to_session_state(service_state);
		session->info->state = state;
	}
	session->info->state = state;

	DBG("session %p state %s", session, state2string(state));

	update_routing_table(session);
1549
	update_nat_rules(session);
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	session_notify(session);
}

static bool session_match_service(struct connman_session *session,
				struct connman_service *service)
{
	enum connman_service_type bearer_type;
	enum connman_service_type service_type;
	GSList *list;

1560 1561 1562
	if (policy && policy->allowed)
		return policy->allowed(session, service);

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	for (list = session->info->config.allowed_bearers; list; list = list->next) {
		bearer_type = GPOINTER_TO_INT(list->data);
		service_type = connman_service_get_type(service);

		if (bearer_type == service_type)
			return true;
	}

	return false;
}

static bool is_session_connected(struct connman_session *session,
				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:
		if (session->info->config.type == CONNMAN_SESSION_TYPE_INTERNET)
			return false;
	case CONNMAN_SERVICE_STATE_ONLINE:
		return true;
	}

	return false;
}

static void session_activate(struct connman_session *session)
1597 1598 1599 1600
{
	GHashTableIter iter;
	gpointer key, value;

1601 1602
	if (!service_hash)
		return;
1603

1604
	g_hash_table_iter_init(&iter, service_hash);
1605
	while (g_hash_table_iter_next(&iter, &key, &value)) {
1606 1607
		struct connman_service_info *info = value;
		enum connman_service_state state;
1608

1609
		state = connman_service_get_state(info->service);
1610

1611 1612 1613
		if (is_session_connected(session, state) &&
				session_match_service(session, info->service)) {
			DBG("session %p add service %p", session, info->service);
1614

1615 1616 1617 1618
			info->sessions = g_slist_prepend(info->sessions,
							session);
			session->service = info->service;
			update_session_state(session);
1619

1620 1621
			return;
		}
1622
	}
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

	session_notify(session);
}

static void session_deactivate(struct connman_session *session)
{
	struct connman_service_info *info;

	if (!service_hash)
		return;

	if (!session->service)
		return;

	info = g_hash_table_lookup(service_hash, session->service);
	if (!info)
		return;

	info->sessions = g_slist_remove(info->sessions, session);
	session->service = NULL;

	session->info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1645 1646
}

1647 1648 1649
static void handle_service_state_online(struct connman_service *service,
					enum connman_service_state state,
					struct connman_service_info *info)
1650 1651 1652 1653 1654
{
	GHashTableIter iter;
	gpointer key, value;

	g_hash_table_iter_init(&iter, session_hash);
1655 1656
	while (g_hash_table_iter_next(&iter, &key, &value)) {
		struct connman_session *session = value;
1657
		bool connected;
1658

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
		connected = is_session_connected(session, state);

		if (session->service == service) {
			if (!connected) {
				DBG("session %p remove service %p", session, service);
				info->sessions = g_slist_remove(info->sessions,
							session);
				session->service = NULL;
				update_session_state(session);
			}
		} else if (connected && session_match_service(session, service)) {
			DBG("session %p add service %p", session, service);

			info->sessions = g_slist_prepend(info->sessions,
							session);
			session->service = service;
			update_session_state(session);
		}
	}
}

static void handle_service_state_offline(struct connman_service *service,
					struct connman_service_info *info)
{
	GSList *list;
1684

1685 1686
	for (list = info->sessions; list; list = list->next) {
		struct connman_session *session = list->data;
1687

1688 1689 1690 1691 1692
		if (session->service != service) {
			connman_warn("session %p should have session %p assigned",
					session, service);
			continue;
		}
1693

1694 1695 1696 1697
		DBG("session %p remove service %p", session, service);

		session->service = NULL;
		update_session_state(session);
1698 1699 1700
	}
}

1701

1702
static void service_state_changed(struct connman_service *service,
1703
				enum connman_service_state state)
1704
{
1705 1706
	struct connman_service_info *info;