connection: Fix vpn on networks without gateway
[platform/upstream/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_host_route(data->ipv6_gateway->vpn_phy_index,
286                                                 data->ipv6_gateway->vpn_ip,
287                                                 data->ipv6_gateway->vpn_phy_ip);
288                 data->ipv6_gateway->active = TRUE;
289
290                 __connman_service_indicate_default(data->service);
291
292                 return;
293         }
294
295         index = __connman_service_get_index(data->service);
296
297         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
298                         g_strcmp0(data->ipv4_gateway->gateway,
299                                                         "0.0.0.0") == 0) {
300                 if (connman_inet_set_gateway_interface(index) < 0)
301                         return;
302                 goto done;
303         }
304
305         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
306                         g_strcmp0(data->ipv6_gateway->gateway,
307                                                         "::") == 0) {
308                 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
309                         return;
310                 goto done;
311         }
312
313         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
314                 status6 = connman_inet_set_ipv6_gateway_address(index,
315                                                 data->ipv6_gateway->gateway);
316
317         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
318                 status4 = connman_inet_set_gateway_address(index,
319                                                 data->ipv4_gateway->gateway);
320
321         if (status4 < 0 || status6 < 0)
322                 return;
323
324 done:
325         __connman_service_indicate_default(data->service);
326 }
327
328 static struct gateway_data *find_default_gateway(void)
329 {
330         struct gateway_data *found = NULL;
331         unsigned int order = 0;
332         GHashTableIter iter;
333         gpointer value, key;
334
335         g_hash_table_iter_init(&iter, gateway_hash);
336
337         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
338                 struct gateway_data *data = value;
339
340                 if (found == NULL || data->order > order) {
341                         found = data;
342                         order = data->order;
343                 }
344         }
345
346         return found;
347 }
348
349 static void remove_gateway(gpointer user_data)
350 {
351         struct gateway_data *data = user_data;
352
353         DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
354
355         if (data->ipv4_gateway != NULL) {
356                 g_free(data->ipv4_gateway->gateway);
357                 g_free(data->ipv4_gateway->vpn_ip);
358                 g_free(data->ipv4_gateway->vpn_phy_ip);
359                 g_free(data->ipv4_gateway);
360         }
361
362         if (data->ipv6_gateway != NULL) {
363                 g_free(data->ipv6_gateway->gateway);
364                 g_free(data->ipv6_gateway->vpn_ip);
365                 g_free(data->ipv6_gateway->vpn_phy_ip);
366                 g_free(data->ipv6_gateway);
367         }
368
369         g_free(data);
370 }
371
372 static void connection_delgateway(int index, const char *gateway)
373 {
374         struct gateway_config *config;
375         struct gateway_data *data;
376
377         DBG("index %d gateway %s", index, gateway);
378
379         config = find_gateway(index, gateway);
380         if (config != NULL)
381                 config->active = FALSE;
382
383         data = find_default_gateway();
384         if (data != NULL)
385                 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
386 }
387
388 static struct connman_rtnl connection_rtnl = {
389         .name           = "connection",
390         .newgateway     = connection_newgateway,
391         .delgateway     = connection_delgateway,
392 };
393
394 static struct gateway_data *find_active_gateway(void)
395 {
396         GHashTableIter iter;
397         gpointer value, key;
398
399         DBG("");
400
401         g_hash_table_iter_init(&iter, gateway_hash);
402
403         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
404                 struct gateway_data *data = value;
405
406                 if (data->ipv4_gateway != NULL &&
407                                 data->ipv4_gateway->active == TRUE)
408                         return data;
409
410                 if (data->ipv6_gateway != NULL &&
411                                 data->ipv6_gateway->active == TRUE)
412                         return data;
413         }
414
415         return NULL;
416 }
417
418 static void update_order(void)
419 {
420         GHashTableIter iter;
421         gpointer value, key;
422
423         DBG("");
424
425         g_hash_table_iter_init(&iter, gateway_hash);
426
427         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
428                 struct gateway_data *data = value;
429
430                 data->order = __connman_service_get_order(data->service);
431         }
432 }
433
434 void __connman_connection_gateway_activate(struct connman_service *service,
435                                         enum connman_ipconfig_type type)
436 {
437         struct gateway_data *data = NULL;
438
439         data = g_hash_table_lookup(gateway_hash, service);
440         if (data == NULL)
441                 return;
442
443         DBG("gateway %p/%p type %d", data->ipv4_gateway,
444                                         data->ipv6_gateway, type);
445
446         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
447                 data->ipv4_gateway->active = TRUE;
448         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
449                 data->ipv6_gateway->active = TRUE;
450 }
451
452 int __connman_connection_gateway_add(struct connman_service *service,
453                                         const char *gateway,
454                                         enum connman_ipconfig_type type,
455                                         const char *peer)
456 {
457         struct gateway_data *active_gateway = NULL;
458         struct gateway_data *new_gateway = NULL;
459         int index;
460
461         index = __connman_service_get_index(service);
462
463         DBG("service %p index %d gateway %s vpn ip %s type %d",
464                 service, index, gateway, peer, type);
465
466         /*
467          * If gateway is NULL, it's a point to point link and the default
468          * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
469          * interface
470          */
471         if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
472                 gateway = "0.0.0.0";
473
474         if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
475                 gateway = "::";
476
477         active_gateway = find_active_gateway();
478         new_gateway = add_gateway(service, index, gateway, type);
479         if (new_gateway == NULL)
480                 return -EINVAL;
481
482         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
483                         new_gateway->ipv6_gateway != NULL &&
484                         g_strcmp0(new_gateway->ipv6_gateway->gateway,
485                                                                 "::") != 0)
486                 connman_inet_add_ipv6_host_route(index,
487                                         new_gateway->ipv6_gateway->gateway,
488                                         NULL);
489
490         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
491                         new_gateway->ipv4_gateway != NULL &&
492                         g_strcmp0(new_gateway->ipv4_gateway->gateway,
493                                                         "0.0.0.0") != 0)
494                 connman_inet_add_host_route(index,
495                                         new_gateway->ipv4_gateway->gateway,
496                                         NULL);
497
498         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
499                                 new_gateway->ipv4_gateway != NULL) {
500                 __connman_service_nameserver_add_routes(service,
501                                         new_gateway->ipv4_gateway->gateway);
502                 __connman_service_ipconfig_indicate_state(service,
503                                                 CONNMAN_SERVICE_STATE_READY,
504                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
505         }
506
507         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
508                                 new_gateway->ipv6_gateway != NULL) {
509                 __connman_service_nameserver_add_routes(service,
510                                         new_gateway->ipv6_gateway->gateway);
511                 __connman_service_ipconfig_indicate_state(service,
512                                                 CONNMAN_SERVICE_STATE_READY,
513                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
514         }
515
516         if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
517                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
518                                         new_gateway->ipv4_gateway != NULL) {
519                         new_gateway->ipv4_gateway->vpn = TRUE;
520                         if (peer != NULL)
521                                 new_gateway->ipv4_gateway->vpn_ip =
522                                                         g_strdup(peer);
523                         else if (gateway != NULL)
524                                 new_gateway->ipv4_gateway->vpn_ip =
525                                                         g_strdup(gateway);
526                         if (active_gateway) {
527                                 const char *new_ipv4_gateway;
528
529                                 new_ipv4_gateway =
530                                         active_gateway->ipv4_gateway->gateway;
531                                 if (new_ipv4_gateway != NULL &&
532                                          g_strcmp0(new_ipv4_gateway,
533                                                         "0.0.0.0") != 0)
534                                         new_gateway->ipv4_gateway->vpn_phy_ip =
535                                                 g_strdup(new_ipv4_gateway);
536
537                                 new_gateway->ipv4_gateway->vpn_phy_index =
538                                                         active_gateway->index;
539                         }
540
541                 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
542                                         new_gateway->ipv6_gateway != NULL) {
543                         new_gateway->ipv6_gateway->vpn = TRUE;
544                         if (peer != NULL)
545                                 new_gateway->ipv6_gateway->vpn_ip =
546                                                         g_strdup(peer);
547                         else if (gateway != NULL)
548                                 new_gateway->ipv6_gateway->vpn_ip =
549                                                         g_strdup(gateway);
550                         if (active_gateway) {
551                                 const char *new_ipv6_gateway;
552
553                                 new_ipv6_gateway =
554                                         active_gateway->ipv6_gateway->gateway;
555                                 if (new_ipv6_gateway != NULL &&
556                                         g_strcmp0(new_ipv6_gateway, "::") != 0)
557                                         new_gateway->ipv6_gateway->vpn_phy_ip =
558                                                 g_strdup(new_ipv6_gateway);
559
560                                 new_gateway->ipv6_gateway->vpn_phy_index =
561                                                         active_gateway->index;
562                         }
563                 }
564         } else {
565                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
566                                         new_gateway->ipv4_gateway != NULL)
567                         new_gateway->ipv4_gateway->vpn = FALSE;
568
569                 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
570                                         new_gateway->ipv6_gateway != NULL)
571                         new_gateway->ipv6_gateway->vpn = FALSE;
572         }
573
574         if (active_gateway == NULL) {
575                 set_default_gateway(new_gateway, type);
576                 return 0;
577         }
578
579         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
580                                 new_gateway->ipv4_gateway != NULL &&
581                                 new_gateway->ipv4_gateway->vpn == TRUE) {
582                 connman_inet_add_host_route(active_gateway->index,
583                                         new_gateway->ipv4_gateway->gateway,
584                                         active_gateway->ipv4_gateway->gateway);
585                 connman_inet_clear_gateway_address(active_gateway->index,
586                                         active_gateway->ipv4_gateway->gateway);
587         }
588
589         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
590                                 new_gateway->ipv6_gateway != NULL &&
591                                 new_gateway->ipv6_gateway->vpn == TRUE) {
592                 connman_inet_add_ipv6_host_route(active_gateway->index,
593                                         new_gateway->ipv6_gateway->gateway,
594                                         active_gateway->ipv6_gateway->gateway);
595                 connman_inet_clear_ipv6_gateway_address(active_gateway->index,
596                                         active_gateway->ipv6_gateway->gateway);
597         }
598
599         return 0;
600 }
601
602 void __connman_connection_gateway_remove(struct connman_service *service,
603                                         enum connman_ipconfig_type type)
604 {
605         struct gateway_data *data = NULL;
606         gboolean set_default4 = FALSE, set_default6 = FALSE;
607         int do_ipv4 = FALSE, do_ipv6 = FALSE;
608         int err;
609
610         DBG("service %p type %d", service, type);
611
612         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
613                 do_ipv4 = TRUE;
614         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
615                 do_ipv6 = TRUE;
616         else
617                 do_ipv4 = do_ipv6 = TRUE;
618
619         __connman_service_nameserver_del_routes(service);
620
621         data = g_hash_table_lookup(gateway_hash, service);
622         if (data == NULL)
623                 return;
624
625         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
626                 set_default4 = data->ipv4_gateway->vpn;
627
628         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
629                 set_default6 = data->ipv6_gateway->vpn;
630
631         DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
632                 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
633                 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
634                 set_default4, set_default6);
635
636         if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
637                         data->ipv4_gateway->vpn == TRUE && data->index >= 0)
638                 connman_inet_del_host_route(data->index,
639                                                 data->ipv4_gateway->gateway);
640
641         if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
642                         data->ipv6_gateway->vpn == TRUE && data->index >= 0)
643                 connman_inet_del_ipv6_host_route(data->index,
644                                                 data->ipv6_gateway->gateway);
645
646         __connman_service_nameserver_del_routes(service);
647
648         err = disable_gateway(data, type);
649
650         /*
651          * We remove the service from the hash only if all the gateway
652          * settings are to be removed.
653          */
654         if (do_ipv4 == do_ipv6 ||
655                 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
656                         && do_ipv4 == TRUE) ||
657                 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
658                         && do_ipv6 == TRUE)
659                 )
660                 g_hash_table_remove(gateway_hash, service);
661         else
662                 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
663                         data->ipv4_gateway, do_ipv4,
664                         data->ipv6_gateway, do_ipv6);
665
666         /* with vpn this will be called after the network was deleted,
667          * we need to call set_default here because we will not recieve any
668          * gateway delete notification.
669          * We hit the same issue if remove_gateway() fails.
670          */
671         if (set_default4 || set_default6 || err < 0) {
672                 data = find_default_gateway();
673                 if (data != NULL)
674                         set_default_gateway(data, type);
675         }
676 }
677
678 gboolean __connman_connection_update_gateway(void)
679 {
680         struct gateway_data *active_gateway, *default_gateway;
681         gboolean updated = FALSE;
682
683         if (gateway_hash == NULL)
684                 return updated;
685
686         update_order();
687
688         active_gateway = find_active_gateway();
689         default_gateway = find_default_gateway();
690
691         if (active_gateway && active_gateway != default_gateway)
692                 updated = TRUE;
693
694         return updated;
695 }
696
697 int __connman_connection_init(void)
698 {
699         int err;
700
701         DBG("");
702
703         gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
704                                                         NULL, remove_gateway);
705
706         err = connman_rtnl_register(&connection_rtnl);
707         if (err < 0)
708                 connman_error("Failed to setup RTNL gateway driver");
709
710         return err;
711 }
712
713 void __connman_connection_cleanup(void)
714 {
715         GHashTableIter iter;
716         gpointer value, key;
717
718         DBG("");
719
720         connman_rtnl_unregister(&connection_rtnl);
721
722         g_hash_table_iter_init(&iter, gateway_hash);
723
724         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
725                 struct gateway_data *data = value;
726
727                 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
728         }
729
730         g_hash_table_destroy(gateway_hash);
731         gateway_hash = NULL;
732 }