connection: Fix IPv6 host route setting when using VPN
[framework/connectivity/connman.git] / src / connection.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  BMW Car IT GmbH. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <string.h>
29 #include <net/if.h>
30
31 #include <gdbus.h>
32
33 #include "connman.h"
34
35 struct gateway_config {
36         gboolean active;
37         char *gateway;
38
39         /* VPN extra data */
40         gboolean vpn;
41         char *vpn_ip;
42         int vpn_phy_index;
43         char *vpn_phy_ip;
44 };
45
46 struct gateway_data {
47         int index;
48         struct connman_service *service;
49         unsigned int order;
50         struct gateway_config *ipv4_gateway;
51         struct gateway_config *ipv6_gateway;
52 };
53
54 static GHashTable *gateway_hash = NULL;
55
56 static struct gateway_config *find_gateway(int index, const char *gateway)
57 {
58         GHashTableIter iter;
59         gpointer value, key;
60
61         if (gateway == NULL)
62                 return NULL;
63
64         g_hash_table_iter_init(&iter, gateway_hash);
65
66         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
67                 struct gateway_data *data = value;
68
69                 if (data->ipv4_gateway != NULL && data->index == index &&
70                                 g_str_equal(data->ipv4_gateway->gateway,
71                                         gateway) == TRUE)
72                         return data->ipv4_gateway;
73
74                 if (data->ipv6_gateway != NULL && data->index == index &&
75                                 g_str_equal(data->ipv6_gateway->gateway,
76                                         gateway) == TRUE)
77                         return data->ipv6_gateway;
78         }
79
80         return NULL;
81 }
82
83 static int del_routes(struct gateway_data *data,
84                         enum connman_ipconfig_type type)
85 {
86         int status4 = 0, status6 = 0;
87         int do_ipv4 = FALSE, do_ipv6 = FALSE;
88
89         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
90                 do_ipv4 = TRUE;
91         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
92                 do_ipv6 = TRUE;
93         else
94                 do_ipv4 = do_ipv6 = TRUE;
95
96         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL) {
97                 if (data->ipv4_gateway->vpn == TRUE) {
98                         if (data->ipv4_gateway->vpn_phy_index >= 0)
99                                 connman_inet_del_host_route(
100                                         data->ipv4_gateway->vpn_phy_index,
101                                         data->ipv4_gateway->gateway);
102
103                         status4 = connman_inet_clear_gateway_address(
104                                                 data->index,
105                                                 data->ipv4_gateway->vpn_ip);
106
107                 } else if (g_strcmp0(data->ipv4_gateway->gateway,
108                                                         "0.0.0.0") == 0) {
109                         status4 = connman_inet_clear_gateway_interface(
110                                                                 data->index);
111                 } else {
112                         connman_inet_del_host_route(data->index,
113                                                 data->ipv4_gateway->gateway);
114                         status4 = connman_inet_clear_gateway_address(
115                                                 data->index,
116                                                 data->ipv4_gateway->gateway);
117                 }
118         }
119
120         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL) {
121                 if (data->ipv6_gateway->vpn == TRUE) {
122                         if (data->ipv6_gateway->vpn_phy_index >= 0)
123                                 connman_inet_del_host_route(
124                                         data->ipv6_gateway->vpn_phy_index,
125                                         data->ipv6_gateway->gateway);
126
127                         status6 = connman_inet_clear_ipv6_gateway_address(
128                                                 data->index,
129                                                 data->ipv6_gateway->vpn_ip);
130
131                 } else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
132                         status6 = connman_inet_clear_ipv6_gateway_interface(
133                                                                 data->index);
134                 } else {
135                         connman_inet_del_ipv6_host_route(data->index,
136                                                 data->ipv6_gateway->gateway);
137                         status6 = connman_inet_clear_ipv6_gateway_address(
138                                                 data->index,
139                                                 data->ipv6_gateway->gateway);
140                 }
141         }
142
143         return (status4 < 0 ? status4 : status6);
144 }
145
146 static int disable_gateway(struct gateway_data *data,
147                         enum connman_ipconfig_type type)
148 {
149         gboolean active = FALSE;
150
151         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
152                 if (data->ipv4_gateway != NULL)
153                         active = data->ipv4_gateway->active;
154         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
155                 if (data->ipv6_gateway != NULL)
156                         active = data->ipv6_gateway->active;
157         } else
158                 active = TRUE;
159
160         DBG("type %d active %d", type, active);
161
162         if (active == TRUE)
163                 return del_routes(data, type);
164
165         return 0;
166 }
167
168 static struct gateway_data *add_gateway(struct connman_service *service,
169                                         int index, const char *gateway,
170                                         enum connman_ipconfig_type type)
171 {
172         struct gateway_data *data, *old;
173         struct gateway_config *config;
174
175         if (gateway == NULL || strlen(gateway) == 0)
176                 return NULL;
177
178         data = g_try_new0(struct gateway_data, 1);
179         if (data == NULL)
180                 return NULL;
181
182         data->index = index;
183
184         config = g_try_new0(struct gateway_config, 1);
185         if (config == NULL) {
186                 g_free(data);
187                 return NULL;
188         }
189
190         config->gateway = g_strdup(gateway);
191         config->vpn_ip = NULL;
192         config->vpn_phy_ip = NULL;
193         config->vpn = FALSE;
194         config->vpn_phy_index = -1;
195         config->active = FALSE;
196
197         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
198                 data->ipv4_gateway = config;
199         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
200                 data->ipv6_gateway = config;
201         else {
202                 g_free(config->gateway);
203                 g_free(config);
204                 g_free(data);
205                 return NULL;
206         }
207
208         data->service = service;
209
210         data->order = __connman_service_get_order(service);
211
212         /*
213          * If the service is already in the hash, then we
214          * must not replace it blindly but disable the gateway
215          * of the type we are replacing and take the other type
216          * from old gateway settings.
217          */
218         old = g_hash_table_lookup(gateway_hash, service);
219         if (old != NULL) {
220                 DBG("Replacing gw %p ipv4 %p ipv6 %p", old,
221                         old->ipv4_gateway, old->ipv6_gateway);
222                 disable_gateway(old, type);
223                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
224                         data->ipv6_gateway = old->ipv6_gateway;
225                         old->ipv6_gateway = NULL;
226                 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
227                         data->ipv4_gateway = old->ipv4_gateway;
228                         old->ipv4_gateway = NULL;
229                 }
230         }
231
232         g_hash_table_replace(gateway_hash, service, data);
233
234         return data;
235 }
236
237 static void connection_newgateway(int index, const char *gateway)
238 {
239         struct gateway_config *config;
240
241         DBG("index %d gateway %s", index, gateway);
242
243         config = find_gateway(index, gateway);
244         if (config == NULL)
245                 return;
246
247         config->active = TRUE;
248 }
249
250 static void set_default_gateway(struct gateway_data *data,
251                                 enum connman_ipconfig_type type)
252 {
253         int index;
254         int status4 = 0, status6 = 0;
255         int do_ipv4 = FALSE, do_ipv6 = FALSE;
256
257         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
258                 do_ipv4 = TRUE;
259         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
260                 do_ipv6 = TRUE;
261         else
262                 do_ipv4 = do_ipv6 = TRUE;
263
264         DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
265                                                 data->ipv6_gateway);
266
267         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
268                                         data->ipv4_gateway->vpn == TRUE) {
269                 connman_inet_set_gateway_address(data->index,
270                                                 data->ipv4_gateway->vpn_ip);
271                 connman_inet_add_host_route(data->ipv4_gateway->vpn_phy_index,
272                                         data->ipv4_gateway->vpn_ip,
273                                         data->ipv4_gateway->vpn_phy_ip);
274                 data->ipv4_gateway->active = TRUE;
275
276                 __connman_service_indicate_default(data->service);
277
278                 return;
279         }
280
281         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
282                                         data->ipv6_gateway->vpn == TRUE) {
283                 connman_inet_set_ipv6_gateway_address(data->index,
284                                                 data->ipv6_gateway->vpn_ip);
285                 connman_inet_add_ipv6_host_route(
286                                         data->ipv6_gateway->vpn_phy_index,
287                                         data->ipv6_gateway->vpn_ip,
288                                         data->ipv6_gateway->vpn_phy_ip);
289                 data->ipv6_gateway->active = TRUE;
290
291                 __connman_service_indicate_default(data->service);
292
293                 return;
294         }
295
296         index = __connman_service_get_index(data->service);
297
298         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
299                         g_strcmp0(data->ipv4_gateway->gateway,
300                                                         "0.0.0.0") == 0) {
301                 if (connman_inet_set_gateway_interface(index) < 0)
302                         return;
303                 goto done;
304         }
305
306         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
307                         g_strcmp0(data->ipv6_gateway->gateway,
308                                                         "::") == 0) {
309                 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
310                         return;
311                 goto done;
312         }
313
314         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
315                 status6 = connman_inet_set_ipv6_gateway_address(index,
316                                                 data->ipv6_gateway->gateway);
317
318         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
319                 status4 = connman_inet_set_gateway_address(index,
320                                                 data->ipv4_gateway->gateway);
321
322         if (status4 < 0 || status6 < 0)
323                 return;
324
325 done:
326         __connman_service_indicate_default(data->service);
327 }
328
329 static struct gateway_data *find_default_gateway(void)
330 {
331         struct gateway_data *found = NULL;
332         unsigned int order = 0;
333         GHashTableIter iter;
334         gpointer value, key;
335
336         g_hash_table_iter_init(&iter, gateway_hash);
337
338         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
339                 struct gateway_data *data = value;
340
341                 if (found == NULL || data->order > order) {
342                         found = data;
343                         order = data->order;
344                 }
345         }
346
347         return found;
348 }
349
350 static void remove_gateway(gpointer user_data)
351 {
352         struct gateway_data *data = user_data;
353
354         DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
355
356         if (data->ipv4_gateway != NULL) {
357                 g_free(data->ipv4_gateway->gateway);
358                 g_free(data->ipv4_gateway->vpn_ip);
359                 g_free(data->ipv4_gateway->vpn_phy_ip);
360                 g_free(data->ipv4_gateway);
361         }
362
363         if (data->ipv6_gateway != NULL) {
364                 g_free(data->ipv6_gateway->gateway);
365                 g_free(data->ipv6_gateway->vpn_ip);
366                 g_free(data->ipv6_gateway->vpn_phy_ip);
367                 g_free(data->ipv6_gateway);
368         }
369
370         g_free(data);
371 }
372
373 static void connection_delgateway(int index, const char *gateway)
374 {
375         struct gateway_config *config;
376         struct gateway_data *data;
377
378         DBG("index %d gateway %s", index, gateway);
379
380         config = find_gateway(index, gateway);
381         if (config != NULL)
382                 config->active = FALSE;
383
384         data = find_default_gateway();
385         if (data != NULL)
386                 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
387 }
388
389 static struct connman_rtnl connection_rtnl = {
390         .name           = "connection",
391         .newgateway     = connection_newgateway,
392         .delgateway     = connection_delgateway,
393 };
394
395 static struct gateway_data *find_active_gateway(void)
396 {
397         GHashTableIter iter;
398         gpointer value, key;
399
400         DBG("");
401
402         g_hash_table_iter_init(&iter, gateway_hash);
403
404         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
405                 struct gateway_data *data = value;
406
407                 if (data->ipv4_gateway != NULL &&
408                                 data->ipv4_gateway->active == TRUE)
409                         return data;
410
411                 if (data->ipv6_gateway != NULL &&
412                                 data->ipv6_gateway->active == TRUE)
413                         return data;
414         }
415
416         return NULL;
417 }
418
419 static void update_order(void)
420 {
421         GHashTableIter iter;
422         gpointer value, key;
423
424         DBG("");
425
426         g_hash_table_iter_init(&iter, gateway_hash);
427
428         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
429                 struct gateway_data *data = value;
430
431                 data->order = __connman_service_get_order(data->service);
432         }
433 }
434
435 void __connman_connection_gateway_activate(struct connman_service *service,
436                                         enum connman_ipconfig_type type)
437 {
438         struct gateway_data *data = NULL;
439
440         data = g_hash_table_lookup(gateway_hash, service);
441         if (data == NULL)
442                 return;
443
444         DBG("gateway %p/%p type %d", data->ipv4_gateway,
445                                         data->ipv6_gateway, type);
446
447         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
448                 data->ipv4_gateway->active = TRUE;
449         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
450                 data->ipv6_gateway->active = TRUE;
451 }
452
453 int __connman_connection_gateway_add(struct connman_service *service,
454                                         const char *gateway,
455                                         enum connman_ipconfig_type type,
456                                         const char *peer)
457 {
458         struct gateway_data *active_gateway = NULL;
459         struct gateway_data *new_gateway = NULL;
460         int index;
461
462         index = __connman_service_get_index(service);
463
464         DBG("service %p index %d gateway %s vpn ip %s type %d",
465                 service, index, gateway, peer, type);
466
467         /*
468          * If gateway is NULL, it's a point to point link and the default
469          * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
470          * interface
471          */
472         if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
473                 gateway = "0.0.0.0";
474
475         if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
476                 gateway = "::";
477
478         active_gateway = find_active_gateway();
479         new_gateway = add_gateway(service, index, gateway, type);
480         if (new_gateway == NULL)
481                 return -EINVAL;
482
483         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
484                         new_gateway->ipv6_gateway != NULL &&
485                         g_strcmp0(new_gateway->ipv6_gateway->gateway,
486                                                                 "::") != 0)
487                 connman_inet_add_ipv6_host_route(index,
488                                         new_gateway->ipv6_gateway->gateway,
489                                         NULL);
490
491         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
492                         new_gateway->ipv4_gateway != NULL &&
493                         g_strcmp0(new_gateway->ipv4_gateway->gateway,
494                                                         "0.0.0.0") != 0)
495                 connman_inet_add_host_route(index,
496                                         new_gateway->ipv4_gateway->gateway,
497                                         NULL);
498
499         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
500                                 new_gateway->ipv4_gateway != NULL) {
501                 __connman_service_nameserver_add_routes(service,
502                                         new_gateway->ipv4_gateway->gateway);
503                 __connman_service_ipconfig_indicate_state(service,
504                                                 CONNMAN_SERVICE_STATE_READY,
505                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
506         }
507
508         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
509                                 new_gateway->ipv6_gateway != NULL) {
510                 __connman_service_nameserver_add_routes(service,
511                                         new_gateway->ipv6_gateway->gateway);
512                 __connman_service_ipconfig_indicate_state(service,
513                                                 CONNMAN_SERVICE_STATE_READY,
514                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
515         }
516
517         if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
518                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
519                                         new_gateway->ipv4_gateway != NULL) {
520                         new_gateway->ipv4_gateway->vpn = TRUE;
521                         if (peer != NULL)
522                                 new_gateway->ipv4_gateway->vpn_ip =
523                                                         g_strdup(peer);
524                         else if (gateway != NULL)
525                                 new_gateway->ipv4_gateway->vpn_ip =
526                                                         g_strdup(gateway);
527                         if (active_gateway) {
528                                 const char *new_ipv4_gateway;
529
530                                 new_ipv4_gateway =
531                                         active_gateway->ipv4_gateway->gateway;
532                                 if (new_ipv4_gateway != NULL &&
533                                          g_strcmp0(new_ipv4_gateway,
534                                                         "0.0.0.0") != 0)
535                                         new_gateway->ipv4_gateway->vpn_phy_ip =
536                                                 g_strdup(new_ipv4_gateway);
537
538                                 new_gateway->ipv4_gateway->vpn_phy_index =
539                                                         active_gateway->index;
540                         }
541
542                 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
543                                         new_gateway->ipv6_gateway != NULL) {
544                         new_gateway->ipv6_gateway->vpn = TRUE;
545                         if (peer != NULL)
546                                 new_gateway->ipv6_gateway->vpn_ip =
547                                                         g_strdup(peer);
548                         else if (gateway != NULL)
549                                 new_gateway->ipv6_gateway->vpn_ip =
550                                                         g_strdup(gateway);
551                         if (active_gateway) {
552                                 const char *new_ipv6_gateway;
553
554                                 new_ipv6_gateway =
555                                         active_gateway->ipv6_gateway->gateway;
556                                 if (new_ipv6_gateway != NULL &&
557                                         g_strcmp0(new_ipv6_gateway, "::") != 0)
558                                         new_gateway->ipv6_gateway->vpn_phy_ip =
559                                                 g_strdup(new_ipv6_gateway);
560
561                                 new_gateway->ipv6_gateway->vpn_phy_index =
562                                                         active_gateway->index;
563                         }
564                 }
565         } else {
566                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
567                                         new_gateway->ipv4_gateway != NULL)
568                         new_gateway->ipv4_gateway->vpn = FALSE;
569
570                 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
571                                         new_gateway->ipv6_gateway != NULL)
572                         new_gateway->ipv6_gateway->vpn = FALSE;
573         }
574
575         if (active_gateway == NULL) {
576                 set_default_gateway(new_gateway, type);
577                 return 0;
578         }
579
580         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
581                                 new_gateway->ipv4_gateway != NULL &&
582                                 new_gateway->ipv4_gateway->vpn == TRUE) {
583                 connman_inet_add_host_route(active_gateway->index,
584                                         new_gateway->ipv4_gateway->gateway,
585                                         active_gateway->ipv4_gateway->gateway);
586                 connman_inet_clear_gateway_address(active_gateway->index,
587                                         active_gateway->ipv4_gateway->gateway);
588         }
589
590         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
591                                 new_gateway->ipv6_gateway != NULL &&
592                                 new_gateway->ipv6_gateway->vpn == TRUE) {
593                 connman_inet_add_ipv6_host_route(active_gateway->index,
594                                         new_gateway->ipv6_gateway->gateway,
595                                         active_gateway->ipv6_gateway->gateway);
596                 connman_inet_clear_ipv6_gateway_address(active_gateway->index,
597                                         active_gateway->ipv6_gateway->gateway);
598         }
599
600         return 0;
601 }
602
603 void __connman_connection_gateway_remove(struct connman_service *service,
604                                         enum connman_ipconfig_type type)
605 {
606         struct gateway_data *data = NULL;
607         gboolean set_default4 = FALSE, set_default6 = FALSE;
608         int do_ipv4 = FALSE, do_ipv6 = FALSE;
609         int err;
610
611         DBG("service %p type %d", service, type);
612
613         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
614                 do_ipv4 = TRUE;
615         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
616                 do_ipv6 = TRUE;
617         else
618                 do_ipv4 = do_ipv6 = TRUE;
619
620         __connman_service_nameserver_del_routes(service);
621
622         data = g_hash_table_lookup(gateway_hash, service);
623         if (data == NULL)
624                 return;
625
626         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
627                 set_default4 = data->ipv4_gateway->vpn;
628
629         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
630                 set_default6 = data->ipv6_gateway->vpn;
631
632         DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
633                 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
634                 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
635                 set_default4, set_default6);
636
637         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
638                         data->ipv4_gateway->vpn == TRUE && data->index >= 0)
639                 connman_inet_del_host_route(data->index,
640                                                 data->ipv4_gateway->gateway);
641
642         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
643                         data->ipv6_gateway->vpn == TRUE && data->index >= 0)
644                 connman_inet_del_ipv6_host_route(data->index,
645                                                 data->ipv6_gateway->gateway);
646
647         __connman_service_nameserver_del_routes(service);
648
649         err = disable_gateway(data, type);
650
651         /*
652          * We remove the service from the hash only if all the gateway
653          * settings are to be removed.
654          */
655         if (do_ipv4 == do_ipv6 ||
656                 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
657                         && do_ipv4 == TRUE) ||
658                 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
659                         && do_ipv6 == TRUE)
660                 )
661                 g_hash_table_remove(gateway_hash, service);
662         else
663                 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
664                         data->ipv4_gateway, do_ipv4,
665                         data->ipv6_gateway, do_ipv6);
666
667         /* with vpn this will be called after the network was deleted,
668          * we need to call set_default here because we will not recieve any
669          * gateway delete notification.
670          * We hit the same issue if remove_gateway() fails.
671          */
672         if (set_default4 || set_default6 || err < 0) {
673                 data = find_default_gateway();
674                 if (data != NULL)
675                         set_default_gateway(data, type);
676         }
677 }
678
679 gboolean __connman_connection_update_gateway(void)
680 {
681         struct gateway_data *active_gateway, *default_gateway;
682         gboolean updated = FALSE;
683
684         if (gateway_hash == NULL)
685                 return updated;
686
687         update_order();
688
689         active_gateway = find_active_gateway();
690         default_gateway = find_default_gateway();
691
692         if (active_gateway && active_gateway != default_gateway)
693                 updated = TRUE;
694
695         return updated;
696 }
697
698 int __connman_connection_init(void)
699 {
700         int err;
701
702         DBG("");
703
704         gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
705                                                         NULL, remove_gateway);
706
707         err = connman_rtnl_register(&connection_rtnl);
708         if (err < 0)
709                 connman_error("Failed to setup RTNL gateway driver");
710
711         return err;
712 }
713
714 void __connman_connection_cleanup(void)
715 {
716         GHashTableIter iter;
717         gpointer value, key;
718
719         DBG("");
720
721         connman_rtnl_unregister(&connection_rtnl);
722
723         g_hash_table_iter_init(&iter, gateway_hash);
724
725         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
726                 struct gateway_data *data = value;
727
728                 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
729         }
730
731         g_hash_table_destroy(gateway_hash);
732         gateway_hash = NULL;
733 }