connection.c 27 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  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 35
 *
 *  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 <string.h>
#include <net/if.h>

#include <gdbus.h>

#include "connman.h"

struct gateway_config {
36
	bool active;
37 38 39
	char *gateway;

	/* VPN extra data */
40
	bool vpn;
41 42 43 44 45 46 47 48 49 50
	char *vpn_ip;
	int vpn_phy_index;
	char *vpn_phy_ip;
};

struct gateway_data {
	int index;
	struct connman_service *service;
	struct gateway_config *ipv4_gateway;
	struct gateway_config *ipv6_gateway;
51
	bool default_checked;
52 53 54 55 56 57 58 59 60
};

static GHashTable *gateway_hash = NULL;

static struct gateway_config *find_gateway(int index, const char *gateway)
{
	GHashTableIter iter;
	gpointer value, key;

61
	if (!gateway)
62 63 64 65
		return NULL;

	g_hash_table_iter_init(&iter, gateway_hash);

66
	while (g_hash_table_iter_next(&iter, &key, &value)) {
67 68
		struct gateway_data *data = value;

69
		if (data->ipv4_gateway && data->index == index &&
70
				g_str_equal(data->ipv4_gateway->gateway,
71
					gateway))
72 73
			return data->ipv4_gateway;

74
		if (data->ipv6_gateway && data->index == index &&
75
				g_str_equal(data->ipv6_gateway->gateway,
76
					gateway))
77 78 79 80 81 82 83 84 85 86 87
			return data->ipv6_gateway;
	}

	return NULL;
}

static struct gateway_data *lookup_gateway_data(struct gateway_config *config)
{
	GHashTableIter iter;
	gpointer value, key;

88
	if (!config)
89 90 91 92
		return NULL;

	g_hash_table_iter_init(&iter, gateway_hash);

93
	while (g_hash_table_iter_next(&iter, &key, &value)) {
94 95
		struct gateway_data *data = value;

96
		if (data->ipv4_gateway &&
97 98 99
				data->ipv4_gateway == config)
			return data;

100
		if (data->ipv6_gateway &&
101 102 103 104 105 106 107 108 109 110 111 112
				data->ipv6_gateway == config)
			return data;
	}

	return NULL;
}

static struct gateway_data *find_vpn_gateway(int index, const char *gateway)
{
	GHashTableIter iter;
	gpointer value, key;

113
	if (!gateway)
114 115 116 117
		return NULL;

	g_hash_table_iter_init(&iter, gateway_hash);

118
	while (g_hash_table_iter_next(&iter, &key, &value)) {
119 120
		struct gateway_data *data = value;

121
		if (data->ipv4_gateway && data->index == index &&
122
				g_str_equal(data->ipv4_gateway->gateway,
123
					gateway))
124 125
			return data;

126
		if (data->ipv6_gateway && data->index == index &&
127
				g_str_equal(data->ipv6_gateway->gateway,
128
					gateway))
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
			return data;
	}

	return NULL;
}

struct get_gateway_params {
	char *vpn_gateway;
	int vpn_index;
};

static void get_gateway_cb(const char *gateway, int index, void *user_data)
{
	struct gateway_config *config;
	struct gateway_data *data;
	struct get_gateway_params *params = user_data;
	int family;
146 147
	
	struct connman_service *service;
148 149 150

	if (index < 0)
		goto out;
151 152 153 154 155
		
	service = __connman_service_lookup_from_index(params->vpn_index);
	
	if (!__connman_service_is_default_route(service)) {
		DBG("Not setting gateway config of non default service %s",
156
			service ? connman_service_get_identifier(service) : "");
157 158
		goto out;
	}
159 160 161 162 163

	DBG("phy index %d phy gw %s vpn index %d vpn gw %s", index, gateway,
		params->vpn_index, params->vpn_gateway);

	data = find_vpn_gateway(params->vpn_index, params->vpn_gateway);
164
	if (!data) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		DBG("Cannot find VPN link route, index %d addr %s",
			params->vpn_index, params->vpn_gateway);
		goto out;
	}

	family = connman_inet_check_ipaddress(params->vpn_gateway);

	if (family == AF_INET)
		config = data->ipv4_gateway;
	else if (family == AF_INET6)
		config = data->ipv6_gateway;
	else
		goto out;

	config->vpn_phy_index = index;

	DBG("vpn %s phy index %d", config->vpn_ip, config->vpn_phy_index);

out:
	g_free(params->vpn_gateway);
	g_free(params);
}

static void set_vpn_routes(struct gateway_data *new_gateway,
			struct connman_service *service,
			const char *gateway,
			enum connman_ipconfig_type type,
			const char *peer,
			struct gateway_data *active_gateway)
{
	struct gateway_config *config;
	struct connman_ipconfig *ipconfig;
	char *dest;

	DBG("new %p service %p gw %s type %d peer %s active %p",
		new_gateway, service, gateway, type, peer, active_gateway);

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
		ipconfig = __connman_service_get_ip4config(service);
		config = new_gateway->ipv4_gateway;
	} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
		ipconfig = __connman_service_get_ip6config(service);
		config = new_gateway->ipv6_gateway;
	} else
		return;

211
	if (config) {
212 213 214
		int index = __connman_ipconfig_get_index(ipconfig);
		struct get_gateway_params *params;

215 216
		config->vpn = true;
		if (peer)
217
			config->vpn_ip = g_strdup(peer);
218
		else if (gateway)
219 220 221
			config->vpn_ip = g_strdup(gateway);

		params = g_try_malloc(sizeof(struct get_gateway_params));
222
		if (!params)
223 224 225 226 227 228 229 230 231 232 233
			return;

		params->vpn_index = index;
		params->vpn_gateway = g_strdup(gateway);

		/*
		 * Find the gateway that is serving the VPN link
		 */
		__connman_inet_get_route(gateway, get_gateway_cb, params);
	}

234
	if (!active_gateway)
235 236 237 238 239 240 241 242 243
		return;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
		/*
		 * Special route to VPN server via gateway. This
		 * is needed so that we can access hosts behind
		 * the VPN. The route might already exist depending
		 * on network topology.
		 */
244
		if (!active_gateway->ipv4_gateway)
245 246
			return;

247 248 249 250 251 252 253 254 255

		/*
		 * If VPN server is on same subnet as we are, skip adding
		 * route.
		 */
		if (connman_inet_compare_subnet(active_gateway->index,
								gateway))
			return;

256 257 258 259 260 261 262 263 264 265 266 267 268
		DBG("active gw %s", active_gateway->ipv4_gateway->gateway);

		if (g_strcmp0(active_gateway->ipv4_gateway->gateway,
							"0.0.0.0") != 0)
			dest = active_gateway->ipv4_gateway->gateway;
		else
			dest = NULL;

		connman_inet_add_host_route(active_gateway->index, gateway,
									dest);

	} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {

269
		if (!active_gateway->ipv6_gateway)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
			return;

		DBG("active gw %s", active_gateway->ipv6_gateway->gateway);

		if (g_strcmp0(active_gateway->ipv6_gateway->gateway,
								"::") != 0)
			dest = active_gateway->ipv6_gateway->gateway;
		else
			dest = NULL;

		connman_inet_add_ipv6_host_route(active_gateway->index,
								gateway, dest);
	}
}

static int del_routes(struct gateway_data *data,
			enum connman_ipconfig_type type)
{
	int status4 = 0, status6 = 0;
289
	bool do_ipv4 = false, do_ipv6 = false;
290 291

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
292
		do_ipv4 = true;
293
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
294
		do_ipv6 = true;
295
	else
296
		do_ipv4 = do_ipv6 = true;
297

298 299
	if (do_ipv4 && data->ipv4_gateway) {
		if (data->ipv4_gateway->vpn) {
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
			status4 = connman_inet_clear_gateway_address(
						data->index,
						data->ipv4_gateway->vpn_ip);

		} else if (g_strcmp0(data->ipv4_gateway->gateway,
							"0.0.0.0") == 0) {
			status4 = connman_inet_clear_gateway_interface(
								data->index);
		} else {
			connman_inet_del_host_route(data->index,
						data->ipv4_gateway->gateway);
			status4 = connman_inet_clear_gateway_address(
						data->index,
						data->ipv4_gateway->gateway);
		}
	}

317 318
	if (do_ipv6 && data->ipv6_gateway) {
		if (data->ipv6_gateway->vpn) {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
			status6 = connman_inet_clear_ipv6_gateway_address(
						data->index,
						data->ipv6_gateway->vpn_ip);

		} else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
			status6 = connman_inet_clear_ipv6_gateway_interface(
								data->index);
		} else {
			connman_inet_del_ipv6_host_route(data->index,
						data->ipv6_gateway->gateway);
			status6 = connman_inet_clear_ipv6_gateway_address(
						data->index,
						data->ipv6_gateway->gateway);
		}
	}

	return (status4 < 0 ? status4 : status6);
}

static int disable_gateway(struct gateway_data *data,
			enum connman_ipconfig_type type)
{
341
	bool active = false;
342 343

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
344
		if (data->ipv4_gateway)
345 346
			active = data->ipv4_gateway->active;
	} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
347
		if (data->ipv6_gateway)
348 349
			active = data->ipv6_gateway->active;
	} else
350
		active = true;
351 352 353

	DBG("type %d active %d", type, active);

354
	if (active)
355 356 357 358 359 360 361 362 363 364 365 366
		return del_routes(data, type);

	return 0;
}

static struct gateway_data *add_gateway(struct connman_service *service,
					int index, const char *gateway,
					enum connman_ipconfig_type type)
{
	struct gateway_data *data, *old;
	struct gateway_config *config;

367
	if (!gateway || strlen(gateway) == 0)
368 369 370
		return NULL;

	data = g_try_new0(struct gateway_data, 1);
371
	if (!data)
372 373 374 375 376
		return NULL;

	data->index = index;

	config = g_try_new0(struct gateway_config, 1);
377
	if (!config) {
378 379 380 381 382 383 384
		g_free(data);
		return NULL;
	}

	config->gateway = g_strdup(gateway);
	config->vpn_ip = NULL;
	config->vpn_phy_ip = NULL;
385
	config->vpn = false;
386
	config->vpn_phy_index = -1;
387
	config->active = false;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		data->ipv4_gateway = config;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		data->ipv6_gateway = config;
	else {
		g_free(config->gateway);
		g_free(config);
		g_free(data);
		return NULL;
	}

	data->service = service;

	/*
	 * If the service is already in the hash, then we
	 * must not replace it blindly but disable the gateway
	 * of the type we are replacing and take the other type
	 * from old gateway settings.
	 */
	old = g_hash_table_lookup(gateway_hash, service);
409
	if (old) {
410 411 412 413 414 415 416 417 418 419 420 421
		DBG("Replacing gw %p ipv4 %p ipv6 %p", old,
			old->ipv4_gateway, old->ipv6_gateway);
		disable_gateway(old, type);
		if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
			data->ipv6_gateway = old->ipv6_gateway;
			old->ipv6_gateway = NULL;
		} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
			data->ipv4_gateway = old->ipv4_gateway;
			old->ipv4_gateway = NULL;
		}
	}

422
	connman_service_ref(data->service);
423 424 425 426 427 428 429 430 431 432
	g_hash_table_replace(gateway_hash, service, data);

	return data;
}

static void set_default_gateway(struct gateway_data *data,
				enum connman_ipconfig_type type)
{
	int index;
	int status4 = 0, status6 = 0;
433
	bool do_ipv4 = false, do_ipv6 = false;
434 435

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
436
		do_ipv4 = true;
437
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
438
		do_ipv6 = true;
439
	else
440
		do_ipv4 = do_ipv6 = true;
441 442 443 444

	DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
						data->ipv6_gateway);

445 446
	if (do_ipv4 && data->ipv4_gateway &&
					data->ipv4_gateway->vpn) {
447
		connman_inet_set_gateway_interface(data->index);
448
		data->ipv4_gateway->active = true;
449 450 451 452 453 454 455 456 457 458 459

		DBG("set %p index %d vpn %s index %d phy %s",
			data, data->index, data->ipv4_gateway->vpn_ip,
			data->ipv4_gateway->vpn_phy_index,
			data->ipv4_gateway->vpn_phy_ip);

		__connman_service_indicate_default(data->service);

		return;
	}

460 461
	if (do_ipv6 && data->ipv6_gateway &&
					data->ipv6_gateway->vpn) {
462
		connman_inet_set_ipv6_gateway_interface(data->index);
463
		data->ipv6_gateway->active = true;
464 465 466 467 468 469 470 471 472 473 474 475 476

		DBG("set %p index %d vpn %s index %d phy %s",
			data, data->index, data->ipv6_gateway->vpn_ip,
			data->ipv6_gateway->vpn_phy_index,
			data->ipv6_gateway->vpn_phy_ip);

		__connman_service_indicate_default(data->service);

		return;
	}

	index = __connman_service_get_index(data->service);

477
	if (do_ipv4 && data->ipv4_gateway &&
478 479 480 481
			g_strcmp0(data->ipv4_gateway->gateway,
							"0.0.0.0") == 0) {
		if (connman_inet_set_gateway_interface(index) < 0)
			return;
482
		data->ipv4_gateway->active = true;
483 484 485
		goto done;
	}

486
	if (do_ipv6 && data->ipv6_gateway &&
487 488 489 490
			g_strcmp0(data->ipv6_gateway->gateway,
							"::") == 0) {
		if (connman_inet_set_ipv6_gateway_interface(index) < 0)
			return;
491
		data->ipv6_gateway->active = true;
492 493 494
		goto done;
	}

495 496 497
	if (do_ipv6 && data->ipv6_gateway)
		status6 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
					index, data->ipv6_gateway->gateway);
498

499 500 501
	if (do_ipv4 && data->ipv4_gateway)
		status4 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
					index, data->ipv4_gateway->gateway);
502 503 504 505 506 507 508 509 510 511 512 513

	if (status4 < 0 || status6 < 0)
		return;

done:
	__connman_service_indicate_default(data->service);
}

static void unset_default_gateway(struct gateway_data *data,
				enum connman_ipconfig_type type)
{
	int index;
514
	bool do_ipv4 = false, do_ipv6 = false;
515 516

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
517
		do_ipv4 = true;
518
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
519
		do_ipv6 = true;
520
	else
521
		do_ipv4 = do_ipv6 = true;
522 523 524 525

	DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
						data->ipv6_gateway);

526 527
	if (do_ipv4 && data->ipv4_gateway &&
					data->ipv4_gateway->vpn) {
528
		connman_inet_clear_gateway_interface(data->index);
529
		data->ipv4_gateway->active = false;
530 531 532 533 534 535 536 537 538

		DBG("unset %p index %d vpn %s index %d phy %s",
			data, data->index, data->ipv4_gateway->vpn_ip,
			data->ipv4_gateway->vpn_phy_index,
			data->ipv4_gateway->vpn_phy_ip);

		return;
	}

539 540
	if (do_ipv6 && data->ipv6_gateway &&
					data->ipv6_gateway->vpn) {
541
		connman_inet_clear_ipv6_gateway_interface(data->index);
542
		data->ipv6_gateway->active = false;
543 544 545 546 547 548 549 550 551 552 553

		DBG("unset %p index %d vpn %s index %d phy %s",
			data, data->index, data->ipv6_gateway->vpn_ip,
			data->ipv6_gateway->vpn_phy_index,
			data->ipv6_gateway->vpn_phy_ip);

		return;
	}

	index = __connman_service_get_index(data->service);

554
	if (do_ipv4 && data->ipv4_gateway &&
555 556 557
			g_strcmp0(data->ipv4_gateway->gateway,
							"0.0.0.0") == 0) {
		connman_inet_clear_gateway_interface(index);
558
		data->ipv4_gateway->active = false;
559 560 561
		return;
	}

562
	if (do_ipv6 && data->ipv6_gateway &&
563 564 565
			g_strcmp0(data->ipv6_gateway->gateway,
							"::") == 0) {
		connman_inet_clear_ipv6_gateway_interface(index);
566
		data->ipv6_gateway->active = false;
567 568 569
		return;
	}

570
	if (do_ipv6 && data->ipv6_gateway)
571 572 573
		connman_inet_clear_ipv6_gateway_address(index,
						data->ipv6_gateway->gateway);

574
	if (do_ipv4 && data->ipv4_gateway)
575 576 577 578 579 580
		connman_inet_clear_gateway_address(index,
						data->ipv4_gateway->gateway);
}

static struct gateway_data *find_default_gateway(void)
{
581
	struct connman_service *service;
582

583
	service = connman_service_get_default();
584 585
	if (!service)
		return NULL;
586

587
	return g_hash_table_lookup(gateway_hash, service);
588 589
}

590
static bool choose_default_gateway(struct gateway_data *data,
591 592
					struct gateway_data *candidate)
{
593
	bool downgraded = false;
594 595 596 597 598 599

	/*
	 * If the current default is not active, then we mark
	 * this one as default. If the other one is already active
	 * we mark this one as non default.
	 */
600 601 602
	if (data->ipv4_gateway && candidate->ipv4_gateway) {

		if (!candidate->ipv4_gateway->active) {
603 604 605 606
			DBG("ipv4 downgrading %p", candidate);
			unset_default_gateway(candidate,
						CONNMAN_IPCONFIG_TYPE_IPV4);
		}
607 608 609 610

		if (candidate->ipv4_gateway->active &&
				__connman_service_compare(candidate->service,
							data->service) < 0) {
611
			DBG("ipv4 downgrading this %p", data);
612
			unset_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);
613
			downgraded = true;
614 615 616
		}
	}

617 618
	if (data->ipv6_gateway && candidate->ipv6_gateway) {
		if (!candidate->ipv6_gateway->active) {
619 620 621 622 623
			DBG("ipv6 downgrading %p", candidate);
			unset_default_gateway(candidate,
						CONNMAN_IPCONFIG_TYPE_IPV6);
		}

624 625 626
		if (candidate->ipv6_gateway->active &&
			__connman_service_compare(candidate->service,
						data->service) < 0) {
627
			DBG("ipv6 downgrading this %p", data);
628
			unset_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
629
			downgraded = true;
630 631 632 633 634 635 636 637 638 639 640 641
		}
	}

	return downgraded;
}

static void connection_newgateway(int index, const char *gateway)
{
	struct gateway_config *config;
	struct gateway_data *data;
	GHashTableIter iter;
	gpointer value, key;
642
	bool found = false;
643 644 645 646

	DBG("index %d gateway %s", index, gateway);

	config = find_gateway(index, gateway);
647
	if (!config)
648 649
		return;

650
	config->active = true;
651 652 653 654 655 656 657

	/*
	 * It is possible that we have two default routes atm
	 * if there are two gateways waiting rtnl activation at the
	 * same time.
	 */
	data = lookup_gateway_data(config);
658
	if (!data)
659 660
		return;

661
	if (data->default_checked)
662 663 664 665 666 667 668 669 670
		return;

	/*
	 * The next checks are only done once, otherwise setting
	 * the default gateway could lead into rtnl forever loop.
	 */

	g_hash_table_iter_init(&iter, gateway_hash);

671
	while (g_hash_table_iter_next(&iter, &key, &value)) {
672 673 674 675 676 677
		struct gateway_data *candidate = value;

		if (candidate == data)
			continue;

		found = choose_default_gateway(data, candidate);
678
		if (found)
679 680 681
			break;
	}

682 683
	if (!found) {
		if (data->ipv4_gateway)
684 685
			set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);

686
		if (data->ipv6_gateway)
687 688 689
			set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
	}

690
	data->default_checked = true;
691 692 693 694 695 696 697 698
}

static void remove_gateway(gpointer user_data)
{
	struct gateway_data *data = user_data;

	DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);

699
	if (data->ipv4_gateway) {
700 701 702 703 704 705
		g_free(data->ipv4_gateway->gateway);
		g_free(data->ipv4_gateway->vpn_ip);
		g_free(data->ipv4_gateway->vpn_phy_ip);
		g_free(data->ipv4_gateway);
	}

706
	if (data->ipv6_gateway) {
707 708 709 710 711 712
		g_free(data->ipv6_gateway->gateway);
		g_free(data->ipv6_gateway->vpn_ip);
		g_free(data->ipv6_gateway->vpn_phy_ip);
		g_free(data->ipv6_gateway);
	}

713 714
	connman_service_unref(data->service);

715 716 717 718 719 720 721 722 723 724 725
	g_free(data);
}

static void connection_delgateway(int index, const char *gateway)
{
	struct gateway_config *config;
	struct gateway_data *data;

	DBG("index %d gateway %s", index, gateway);

	config = find_gateway(index, gateway);
726 727
	if (config)
		config->active = false;
728 729

	data = find_default_gateway();
730
	if (data)
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
		set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
}

static struct connman_rtnl connection_rtnl = {
	.name		= "connection",
	.newgateway	= connection_newgateway,
	.delgateway	= connection_delgateway,
};

static struct gateway_data *find_active_gateway(void)
{
	GHashTableIter iter;
	gpointer value, key;

	DBG("");

	g_hash_table_iter_init(&iter, gateway_hash);

749
	while (g_hash_table_iter_next(&iter, &key, &value)) {
750 751
		struct gateway_data *data = value;

752 753
		if (data->ipv4_gateway &&
				data->ipv4_gateway->active)
754 755
			return data;

756 757
		if (data->ipv6_gateway &&
				data->ipv6_gateway->active)
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
			return data;
	}

	return NULL;
}

static void add_host_route(int family, int index, const char *gateway,
			enum connman_service_type service_type)
{
	switch (family) {
	case AF_INET:
		if (g_strcmp0(gateway, "0.0.0.0") != 0) {
			/*
			 * We must not set route to the phy dev gateway in
			 * VPN link. The packets to VPN link might be routed
			 * back to itself and not routed into phy link gateway.
			 */
			if (service_type != CONNMAN_SERVICE_TYPE_VPN)
				connman_inet_add_host_route(index, gateway,
									NULL);
		} else {
			/*
			 * Add host route to P-t-P link so that services can
			 * be moved around and we can have some link to P-t-P
			 * network (although those P-t-P links have limited
			 * usage if default route is not directed to them)
			 */
			char *dest;
			if (connman_inet_get_dest_addr(index, &dest) == 0) {
				connman_inet_add_host_route(index, dest, NULL);
				g_free(dest);
			}
		}
		break;

	case AF_INET6:
		if (g_strcmp0(gateway, "::") != 0) {
			if (service_type != CONNMAN_SERVICE_TYPE_VPN)
				connman_inet_add_ipv6_host_route(index,
								gateway, NULL);
		} else {
			/* P-t-P link, add route to destination */
			char *dest;
			if (connman_inet_ipv6_get_dest_addr(index,
								&dest) == 0) {
				connman_inet_add_ipv6_host_route(index, dest,
								NULL);
				g_free(dest);
			}
		}
		break;
	}
}

int __connman_connection_gateway_add(struct connman_service *service,
					const char *gateway,
					enum connman_ipconfig_type type,
					const char *peer)
{
	struct gateway_data *active_gateway = NULL;
	struct gateway_data *new_gateway = NULL;
	enum connman_ipconfig_type type4 = CONNMAN_IPCONFIG_TYPE_UNKNOWN,
		type6 = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
	enum connman_service_type service_type =
					connman_service_get_type(service);
	int index;

	index = __connman_service_get_index(service);

	/*
	 * If gateway is NULL, it's a point to point link and the default
	 * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
	 * interface
	 */
832
	if (!gateway && type == CONNMAN_IPCONFIG_TYPE_IPV4)
833 834
		gateway = "0.0.0.0";

835
	if (!gateway && type == CONNMAN_IPCONFIG_TYPE_IPV6)
836 837 838 839 840 841
		gateway = "::";

	DBG("service %p index %d gateway %s vpn ip %s type %d",
		service, index, gateway, peer, type);

	new_gateway = add_gateway(service, index, gateway, type);
842
	if (!new_gateway)
843 844 845 846 847 848 849 850
		return -EINVAL;

	active_gateway = find_active_gateway();

	DBG("active %p index %d new %p", active_gateway,
		active_gateway ? active_gateway->index : -1, new_gateway);

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
851
				new_gateway->ipv4_gateway) {
852 853 854 855 856 857 858
		add_host_route(AF_INET, index, gateway, service_type);
		__connman_service_nameserver_add_routes(service,
					new_gateway->ipv4_gateway->gateway);
		type4 = CONNMAN_IPCONFIG_TYPE_IPV4;
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
859
				new_gateway->ipv6_gateway) {
860 861 862 863 864 865 866 867 868 869 870
		add_host_route(AF_INET6, index, gateway, service_type);
		__connman_service_nameserver_add_routes(service,
					new_gateway->ipv6_gateway->gateway);
		type6 = CONNMAN_IPCONFIG_TYPE_IPV6;
	}

	if (service_type == CONNMAN_SERVICE_TYPE_VPN) {

		set_vpn_routes(new_gateway, service, gateway, type, peer,
							active_gateway);

871 872
		if (!__connman_service_is_default_route(service)) {
			DBG("Not adding gateways for non default VPN %s",
873
				service ? connman_service_get_identifier(service) :
874 875 876
				"");
			goto done;
		}
877 878
	} else {
		if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
879 880
					new_gateway->ipv4_gateway)
			new_gateway->ipv4_gateway->vpn = false;
881 882

		if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
883 884
					new_gateway->ipv6_gateway)
			new_gateway->ipv6_gateway->vpn = false;
885 886
	}

887
	if (!active_gateway) {
888 889 890 891 892
		set_default_gateway(new_gateway, type);
		goto done;
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
893 894 895
				new_gateway->ipv4_gateway &&
				new_gateway->ipv4_gateway->vpn) {
		if (!__connman_service_is_split_routing(new_gateway->service))
896 897 898 899 900 901
			connman_inet_clear_gateway_address(
					active_gateway->index,
					active_gateway->ipv4_gateway->gateway);
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
902 903 904
				new_gateway->ipv6_gateway &&
				new_gateway->ipv6_gateway->vpn) {
		if (!__connman_service_is_split_routing(new_gateway->service))
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
			connman_inet_clear_ipv6_gateway_address(
					active_gateway->index,
					active_gateway->ipv6_gateway->gateway);
	}

done:
	if (type4 == CONNMAN_IPCONFIG_TYPE_IPV4)
		__connman_service_ipconfig_indicate_state(service,
						CONNMAN_SERVICE_STATE_READY,
						CONNMAN_IPCONFIG_TYPE_IPV4);

	if (type6 == CONNMAN_IPCONFIG_TYPE_IPV6)
		__connman_service_ipconfig_indicate_state(service,
						CONNMAN_SERVICE_STATE_READY,
						CONNMAN_IPCONFIG_TYPE_IPV6);
	return 0;
}

void __connman_connection_gateway_remove(struct connman_service *service,
					enum connman_ipconfig_type type)
{
	struct gateway_data *data = NULL;
927 928
	bool set_default4 = false, set_default6 = false;
        bool do_ipv4 = false, do_ipv6 = false;
929 930 931 932 933
	int err;

	DBG("service %p type %d", service, type);

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
934
		do_ipv4 = true;
935
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
936
		do_ipv6 = true;
937
	else
938
		do_ipv4 = do_ipv6 = true;
939 940 941 942

	__connman_service_nameserver_del_routes(service, type);

	data = g_hash_table_lookup(gateway_hash, service);
943
	if (!data)
944 945
		return;

946
	if (do_ipv4 && data->ipv4_gateway)
947 948
		set_default4 = data->ipv4_gateway->vpn;

949
	if (do_ipv6 && data->ipv6_gateway)
950 951 952 953 954 955 956
		set_default6 = data->ipv6_gateway->vpn;

	DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
		data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
		data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
		set_default4, set_default6);

957 958
	if (do_ipv4 && data->ipv4_gateway &&
			data->ipv4_gateway->vpn && data->index >= 0)
959
		connman_inet_del_host_route(data->ipv4_gateway->vpn_phy_index,
960 961
						data->ipv4_gateway->gateway);

962 963
	if (do_ipv6 && data->ipv6_gateway &&
			data->ipv6_gateway->vpn && data->index >= 0)
964 965
		connman_inet_del_ipv6_host_route(
					data->ipv6_gateway->vpn_phy_index,
966 967 968 969 970 971 972 973 974
						data->ipv6_gateway->gateway);

	err = disable_gateway(data, type);

	/*
	 * We remove the service from the hash only if all the gateway
	 * settings are to be removed.
	 */
	if (do_ipv4 == do_ipv6 ||
975 976 977
		(data->ipv4_gateway && !data->ipv6_gateway
			&& do_ipv4) ||
		(data->ipv6_gateway && !data->ipv4_gateway
978
			&& do_ipv6)) {
979 980 981 982 983 984 985 986 987 988 989 990 991
		g_hash_table_remove(gateway_hash, service);
	} else
		DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
			data->ipv4_gateway, do_ipv4,
			data->ipv6_gateway, do_ipv6);

	/* with vpn this will be called after the network was deleted,
	 * we need to call set_default here because we will not recieve any
	 * gateway delete notification.
	 * We hit the same issue if remove_gateway() fails.
	 */
	if (set_default4 || set_default6 || err < 0) {
		data = find_default_gateway();
992
		if (data)
993 994 995 996
			set_default_gateway(data, type);
	}
}

997
bool __connman_connection_update_gateway(void)
998 999
{
	struct gateway_data *default_gateway;
1000
	bool updated = false;
1001 1002 1003
	GHashTableIter iter;
	gpointer value, key;

1004
	if (!gateway_hash)
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		return updated;

	default_gateway = find_default_gateway();

	DBG("default %p", default_gateway);

	/*
	 * There can be multiple active gateways so we need to
	 * check them all.
	 */
	g_hash_table_iter_init(&iter, gateway_hash);

1017
	while (g_hash_table_iter_next(&iter, &key, &value)) {
1018 1019 1020 1021 1022
		struct gateway_data *active_gateway = value;

		if (active_gateway == default_gateway)
			continue;

1023 1024
		if (active_gateway->ipv4_gateway &&
				active_gateway->ipv4_gateway->active) {
1025 1026 1027

			unset_default_gateway(active_gateway,
						CONNMAN_IPCONFIG_TYPE_IPV4);
1028
			updated = true;
1029 1030
		}

1031 1032
		if (active_gateway->ipv6_gateway &&
				active_gateway->ipv6_gateway->active) {
1033 1034 1035

			unset_default_gateway(active_gateway,
						CONNMAN_IPCONFIG_TYPE_IPV6);
1036
			updated = true;
1037 1038 1039
		}
	}

1040 1041 1042 1043 1044 1045 1046
	/*
	 * Set default gateway if it has been updated or if it has not been
	 * set as active yet.
	 */
	if (default_gateway) {
		if (default_gateway->ipv4_gateway &&
			(updated || !default_gateway->ipv4_gateway->active))
1047 1048 1049
			set_default_gateway(default_gateway,
					CONNMAN_IPCONFIG_TYPE_IPV4);

1050 1051
		if (default_gateway->ipv6_gateway &&
			(updated || !default_gateway->ipv6_gateway->active))
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
			set_default_gateway(default_gateway,
					CONNMAN_IPCONFIG_TYPE_IPV6);
	}

	return updated;
}

int __connman_connection_get_vpn_index(int phy_index)
{
	GHashTableIter iter;
	gpointer value, key;

	g_hash_table_iter_init(&iter, gateway_hash);

1066
	while (g_hash_table_iter_next(&iter, &key, &value)) {
1067 1068
		struct gateway_data *data = value;

1069
		if (data->ipv4_gateway &&
1070 1071 1072
				data->ipv4_gateway->vpn_phy_index == phy_index)
			return data->index;

1073
		if (data->ipv6_gateway &&
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
				data->ipv6_gateway->vpn_phy_index == phy_index)
			return data->index;
	}

	return -1;
}

int __connman_connection_init(void)
{
	int err;

	DBG("");

	gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
							NULL, remove_gateway);

	err = connman_rtnl_register(&connection_rtnl);
	if (err < 0)
		connman_error("Failed to setup RTNL gateway driver");

	return err;
}

void __connman_connection_cleanup(void)
{
	GHashTableIter iter;
	gpointer value, key;

	DBG("");

	connman_rtnl_unregister(&connection_rtnl);

	g_hash_table_iter_init(&iter, gateway_hash);

1108
	while (g_hash_table_iter_next(&iter, &key, &value)) {
1109 1110 1111 1112 1113 1114 1115 1116
		struct gateway_data *data = value;

		disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
	}

	g_hash_table_destroy(gateway_hash);
	gateway_hash = NULL;
}