summaryrefslogtreecommitdiff
path: root/src/connection.c
blob: 58adfeb664beb2a896b8786f122ee3c0ac219879 (plain)
1
2
3
4
5
6
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
159
160
161
162
163
164
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
211
212
213
214
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
261
262
263
264
265
266
267
268
269
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
328
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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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
647
648
649
650
651
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *  Copyright (C) 2011  BMW Car IT GmbH. 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 <string.h>
#include <net/if.h>

#include <gdbus.h>

#include "connman.h"

struct gateway_config {
	gboolean active;
	char *gateway;

	/* VPN extra data */
	gboolean vpn;
	char *vpn_ip;
	int vpn_phy_index;
	char *vpn_phy_ip;
};

struct gateway_data {
	int index;
	struct connman_service *service;
	unsigned int order;
	struct gateway_config *ipv4_gateway;
	struct gateway_config *ipv6_gateway;
};

static GHashTable *gateway_hash = NULL;

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

	if (gateway == NULL)
		return NULL;

	g_hash_table_iter_init(&iter, gateway_hash);

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

		if (data->ipv4_gateway != NULL && data->index == index &&
				g_str_equal(data->ipv4_gateway->gateway,
					gateway) == TRUE)
			return data->ipv4_gateway;

		if (data->ipv6_gateway != NULL && data->index == index &&
				g_str_equal(data->ipv6_gateway->gateway,
					gateway) == TRUE)
			return data->ipv6_gateway;
	}

	return NULL;
}

static int del_routes(struct gateway_data *data,
			enum connman_ipconfig_type type)
{
	int status4 = 0, status6 = 0;
	int do_ipv4 = FALSE, do_ipv6 = FALSE;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		do_ipv4 = TRUE;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		do_ipv6 = TRUE;
	else
		do_ipv4 = do_ipv6 = TRUE;

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL) {
		if (data->ipv4_gateway->vpn == TRUE) {
			if (data->ipv4_gateway->vpn_phy_index >= 0)
				connman_inet_del_host_route(
					data->ipv4_gateway->vpn_phy_index,
					data->ipv4_gateway->gateway);

			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);
		}
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL) {
		if (data->ipv6_gateway->vpn == TRUE) {
			if (data->ipv6_gateway->vpn_phy_index >= 0)
				connman_inet_del_host_route(
					data->ipv6_gateway->vpn_phy_index,
					data->ipv6_gateway->gateway);

			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)
{
	gboolean active = FALSE;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
		if (data->ipv4_gateway != NULL)
			active = data->ipv4_gateway->active;
	} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
		if (data->ipv6_gateway != NULL)
			active = data->ipv6_gateway->active;
	} else
		active = TRUE;

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

	if (active == TRUE)
		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;

	if (gateway == NULL || strlen(gateway) == 0)
		return NULL;

	data = g_try_new0(struct gateway_data, 1);
	if (data == NULL)
		return NULL;

	data->index = index;

	config = g_try_new0(struct gateway_config, 1);
	if (config == NULL) {
		g_free(data);
		return NULL;
	}

	config->gateway = g_strdup(gateway);
	config->vpn_ip = NULL;
	config->vpn_phy_ip = NULL;
	config->vpn = FALSE;
	config->vpn_phy_index = -1;
	config->active = FALSE;

	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;

	data->order = __connman_service_get_order(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);
	if (old != NULL) {
		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;
		}
	}

	connman_service_ref(service);
	g_hash_table_replace(gateway_hash, service, data);

	return data;
}

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

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

	config = find_gateway(index, gateway);
	if (config == NULL)
		return;

	config->active = TRUE;
}

static void set_default_gateway(struct gateway_data *data,
				enum connman_ipconfig_type type)
{
	int index;
	int status4 = 0, status6 = 0;
	int do_ipv4 = FALSE, do_ipv6 = FALSE;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		do_ipv4 = TRUE;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		do_ipv6 = TRUE;
	else
		do_ipv4 = do_ipv6 = TRUE;

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

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
					data->ipv4_gateway->vpn == TRUE) {
		connman_inet_set_gateway_address(data->index,
						data->ipv4_gateway->vpn_ip);
		connman_inet_add_host_route(data->ipv4_gateway->vpn_phy_index,
					data->ipv4_gateway->vpn_ip,
					data->ipv4_gateway->vpn_phy_ip);
		data->ipv4_gateway->active = TRUE;

		__connman_service_indicate_default(data->service);

		return;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
					data->ipv6_gateway->vpn == TRUE) {
		connman_inet_set_ipv6_gateway_address(data->index,
						data->ipv6_gateway->vpn_ip);
		connman_inet_add_ipv6_host_route(
					data->ipv6_gateway->vpn_phy_index,
					data->ipv6_gateway->vpn_ip,
					data->ipv6_gateway->vpn_phy_ip);
		data->ipv6_gateway->active = TRUE;

		__connman_service_indicate_default(data->service);

		return;
	}

	index = __connman_service_get_index(data->service);

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
			g_strcmp0(data->ipv4_gateway->gateway,
							"0.0.0.0") == 0) {
		if (connman_inet_set_gateway_interface(index) < 0)
			return;
		goto done;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
			g_strcmp0(data->ipv6_gateway->gateway,
							"::") == 0) {
		if (connman_inet_set_ipv6_gateway_interface(index) < 0)
			return;
		goto done;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
		status6 = connman_inet_set_ipv6_gateway_address(index,
						data->ipv6_gateway->gateway);

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
		status4 = connman_inet_set_gateway_address(index,
						data->ipv4_gateway->gateway);

	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;
	int do_ipv4 = FALSE, do_ipv6 = FALSE;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		do_ipv4 = TRUE;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		do_ipv6 = TRUE;
	else
		do_ipv4 = do_ipv6 = TRUE;

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

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
					data->ipv4_gateway->vpn == TRUE) {
		connman_inet_del_host_route(data->index,
						data->ipv4_gateway->vpn_ip);
		connman_inet_clear_gateway_address(data->index,
						data->ipv4_gateway->vpn_ip);
		data->ipv4_gateway->active = FALSE;

		return;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
					data->ipv6_gateway->vpn == TRUE) {
		connman_inet_del_ipv6_host_route(data->index,
						data->ipv6_gateway->vpn_ip);
		connman_inet_clear_ipv6_gateway_address(data->index,
						data->ipv6_gateway->vpn_ip);
		data->ipv6_gateway->active = FALSE;

		return;
	}

	index = __connman_service_get_index(data->service);

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
			g_strcmp0(data->ipv4_gateway->gateway,
							"0.0.0.0") == 0) {
		connman_inet_clear_gateway_interface(index);
		return;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
			g_strcmp0(data->ipv6_gateway->gateway,
							"::") == 0) {
		connman_inet_clear_ipv6_gateway_interface(index);
		return;
	}

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
		connman_inet_clear_ipv6_gateway_address(index,
						data->ipv6_gateway->gateway);

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
		connman_inet_clear_gateway_address(index,
						data->ipv4_gateway->gateway);
}

static struct gateway_data *find_default_gateway(void)
{
	struct gateway_data *found = NULL;
	unsigned int order = 0;
	GHashTableIter iter;
	gpointer value, key;

	g_hash_table_iter_init(&iter, gateway_hash);

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

		if (found == NULL || data->order > order) {
			found = data;
			order = data->order;
		}
	}

	return found;
}

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);

	if (data->ipv4_gateway != NULL) {
		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);
	}

	if (data->ipv6_gateway != NULL) {
		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);
	}

	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);
	if (config != NULL)
		config->active = FALSE;

	data = find_default_gateway();
	if (data != NULL)
		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);

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

		if (data->ipv4_gateway != NULL &&
				data->ipv4_gateway->active == TRUE)
			return data;

		if (data->ipv6_gateway != NULL &&
				data->ipv6_gateway->active == TRUE)
			return data;
	}

	return NULL;
}

static void update_order(void)
{
	GHashTableIter iter;
	gpointer value, key;

	DBG("");

	g_hash_table_iter_init(&iter, gateway_hash);

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

		data->order = __connman_service_get_order(data->service);
	}
}

void __connman_connection_gateway_activate(struct connman_service *service,
					enum connman_ipconfig_type type)
{
	struct gateway_data *data = NULL;

	data = g_hash_table_lookup(gateway_hash, service);
	if (data == NULL)
		return;

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

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		data->ipv4_gateway->active = TRUE;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		data->ipv6_gateway->active = TRUE;
}

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;
	int index;

	index = __connman_service_get_index(service);

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

	/*
	 * 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
	 */
	if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
		gateway = "0.0.0.0";

	if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
		gateway = "::";

	active_gateway = find_active_gateway();
	new_gateway = add_gateway(service, index, gateway, type);
	if (new_gateway == NULL)
		return -EINVAL;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
			new_gateway->ipv6_gateway != NULL &&
			g_strcmp0(new_gateway->ipv6_gateway->gateway,
								"::") != 0)
		connman_inet_add_ipv6_host_route(index,
					new_gateway->ipv6_gateway->gateway,
					NULL);

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
			new_gateway->ipv4_gateway != NULL &&
			g_strcmp0(new_gateway->ipv4_gateway->gateway,
							"0.0.0.0") != 0)
		connman_inet_add_host_route(index,
					new_gateway->ipv4_gateway->gateway,
					NULL);

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
				new_gateway->ipv4_gateway != NULL) {
		__connman_service_nameserver_add_routes(service,
					new_gateway->ipv4_gateway->gateway);
		__connman_service_ipconfig_indicate_state(service,
						CONNMAN_SERVICE_STATE_READY,
						CONNMAN_IPCONFIG_TYPE_IPV4);
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
				new_gateway->ipv6_gateway != NULL) {
		__connman_service_nameserver_add_routes(service,
					new_gateway->ipv6_gateway->gateway);
		__connman_service_ipconfig_indicate_state(service,
						CONNMAN_SERVICE_STATE_READY,
						CONNMAN_IPCONFIG_TYPE_IPV6);
	}

	if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
		if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
					new_gateway->ipv4_gateway != NULL) {
			new_gateway->ipv4_gateway->vpn = TRUE;
			if (peer != NULL)
				new_gateway->ipv4_gateway->vpn_ip =
							g_strdup(peer);
			else if (gateway != NULL)
				new_gateway->ipv4_gateway->vpn_ip =
							g_strdup(gateway);
			if (active_gateway) {
				const char *new_ipv4_gateway;

				new_ipv4_gateway =
					active_gateway->ipv4_gateway->gateway;
				if (new_ipv4_gateway != NULL &&
					 g_strcmp0(new_ipv4_gateway,
							"0.0.0.0") != 0)
					new_gateway->ipv4_gateway->vpn_phy_ip =
						g_strdup(new_ipv4_gateway);

				new_gateway->ipv4_gateway->vpn_phy_index =
							active_gateway->index;
			}

		} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
					new_gateway->ipv6_gateway != NULL) {
			new_gateway->ipv6_gateway->vpn = TRUE;
			if (peer != NULL)
				new_gateway->ipv6_gateway->vpn_ip =
							g_strdup(peer);
			else if (gateway != NULL)
				new_gateway->ipv6_gateway->vpn_ip =
							g_strdup(gateway);
			if (active_gateway) {
				const char *new_ipv6_gateway;

				new_ipv6_gateway =
					active_gateway->ipv6_gateway->gateway;
				if (new_ipv6_gateway != NULL &&
					g_strcmp0(new_ipv6_gateway, "::") != 0)
					new_gateway->ipv6_gateway->vpn_phy_ip =
						g_strdup(new_ipv6_gateway);

				new_gateway->ipv6_gateway->vpn_phy_index =
							active_gateway->index;
			}
		}
	} else {
		if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
					new_gateway->ipv4_gateway != NULL)
			new_gateway->ipv4_gateway->vpn = FALSE;

		if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
					new_gateway->ipv6_gateway != NULL)
			new_gateway->ipv6_gateway->vpn = FALSE;
	}

	if (active_gateway == NULL) {
		set_default_gateway(new_gateway, type);
		return 0;
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
				new_gateway->ipv4_gateway != NULL &&
				new_gateway->ipv4_gateway->vpn == TRUE) {
		connman_inet_add_host_route(active_gateway->index,
					new_gateway->ipv4_gateway->gateway,
					active_gateway->ipv4_gateway->gateway);
		connman_inet_clear_gateway_address(active_gateway->index,
					active_gateway->ipv4_gateway->gateway);
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
				new_gateway->ipv6_gateway != NULL &&
				new_gateway->ipv6_gateway->vpn == TRUE) {
		connman_inet_add_ipv6_host_route(active_gateway->index,
					new_gateway->ipv6_gateway->gateway,
					active_gateway->ipv6_gateway->gateway);
		connman_inet_clear_ipv6_gateway_address(active_gateway->index,
					active_gateway->ipv6_gateway->gateway);
	}

	return 0;
}

void __connman_connection_gateway_remove(struct connman_service *service,
					enum connman_ipconfig_type type)
{
	struct gateway_data *data = NULL;
	gboolean set_default4 = FALSE, set_default6 = FALSE;
	int do_ipv4 = FALSE, do_ipv6 = FALSE;
	int err;

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

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		do_ipv4 = TRUE;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		do_ipv6 = TRUE;
	else
		do_ipv4 = do_ipv6 = TRUE;

	__connman_service_nameserver_del_routes(service);

	data = g_hash_table_lookup(gateway_hash, service);
	if (data == NULL)
		return;

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
		set_default4 = data->ipv4_gateway->vpn;

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
		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);

	if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
			data->ipv4_gateway->vpn == TRUE && data->index >= 0)
		connman_inet_del_host_route(data->index,
						data->ipv4_gateway->gateway);

	if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
			data->ipv6_gateway->vpn == TRUE && data->index >= 0)
		connman_inet_del_ipv6_host_route(data->index,
						data->ipv6_gateway->gateway);

	__connman_service_nameserver_del_routes(service);

	err = disable_gateway(data, type);

	/*
	 * We may refcount service twice seperately for ipv4 and ipv6
	 * then we need to unref accordingly.
	 */
	if (do_ipv4 == do_ipv6 && data->ipv4_gateway != NULL &&
					data->ipv6_gateway != NULL)
		connman_service_unref(service);

	/*
	 * We remove the service from the hash only if all the gateway
	 * settings are to be removed.
	 */
	if (do_ipv4 == do_ipv6 ||
		(data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
			&& do_ipv4 == TRUE) ||
		(data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
			&& do_ipv6 == TRUE)
		) {
		connman_service_unref(service);
		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();
		if (data != NULL)
			set_default_gateway(data, type);
	}
}

gboolean __connman_connection_update_gateway(void)
{
	struct gateway_data *active_gateway, *default_gateway;
	gboolean updated = FALSE;

	if (gateway_hash == NULL)
		return updated;

	active_gateway = find_active_gateway();

	update_order();

	default_gateway = find_default_gateway();

	if (active_gateway && active_gateway != default_gateway) {
		updated = TRUE;

		if (active_gateway->ipv4_gateway)
			unset_default_gateway(active_gateway,
					CONNMAN_IPCONFIG_TYPE_IPV4);

		if (active_gateway->ipv6_gateway)
			unset_default_gateway(active_gateway,
					CONNMAN_IPCONFIG_TYPE_IPV6);

		if (default_gateway) {
			if (default_gateway->ipv4_gateway)
				set_default_gateway(default_gateway,
						CONNMAN_IPCONFIG_TYPE_IPV4);

			if (default_gateway->ipv6_gateway)
				set_default_gateway(default_gateway,
						CONNMAN_IPCONFIG_TYPE_IPV6);
		}
	}

	return updated;
}

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);

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

		disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
	}

	g_hash_table_destroy(gateway_hash);
	gateway_hash = NULL;
}