5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 BMW Car IT GmbH. All rights reserved.
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.
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.
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
35 struct gateway_config {
48 struct connman_service *service;
50 struct gateway_config *ipv4_gateway;
51 struct gateway_config *ipv6_gateway;
52 connman_bool_t default_checked;
55 static GHashTable *gateway_hash = NULL;
57 static struct gateway_config *find_gateway(int index, const char *gateway)
65 g_hash_table_iter_init(&iter, gateway_hash);
67 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
68 struct gateway_data *data = value;
70 if (data->ipv4_gateway != NULL && data->index == index &&
71 g_str_equal(data->ipv4_gateway->gateway,
73 return data->ipv4_gateway;
75 if (data->ipv6_gateway != NULL && data->index == index &&
76 g_str_equal(data->ipv6_gateway->gateway,
78 return data->ipv6_gateway;
84 static struct gateway_data *lookup_gateway_data(struct gateway_config *config)
92 g_hash_table_iter_init(&iter, gateway_hash);
94 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
95 struct gateway_data *data = value;
97 if (data->ipv4_gateway != NULL &&
98 data->ipv4_gateway == config)
101 if (data->ipv6_gateway != NULL &&
102 data->ipv6_gateway == config)
110 * Find the gateway that is serving the VPN link
112 static struct gateway_data *find_phy_gateway(int index, const char *gateway)
120 g_hash_table_iter_init(&iter, gateway_hash);
122 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
123 struct gateway_data *data = value;
125 if (data->ipv4_gateway != NULL && data->index != index &&
126 g_str_equal(data->ipv4_gateway->gateway,
130 if (data->ipv6_gateway != NULL && data->index != index &&
131 g_str_equal(data->ipv6_gateway->gateway,
139 static void set_vpn_routes(struct gateway_config *config,
140 struct connman_service *service,
142 enum connman_ipconfig_type type,
145 struct gateway_data *data;
146 struct connman_ipconfig *ipconfig;
151 config->vpn_ip = g_strdup(peer);
152 else if (gateway != NULL)
153 config->vpn_ip = g_strdup(gateway);
155 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
156 ipconfig = __connman_service_get_ip4config(service);
157 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
158 ipconfig = __connman_service_get_ip6config(service);
162 index = __connman_ipconfig_get_index(ipconfig);
163 data = find_phy_gateway(index, gateway);
167 * data->service points now to original
168 * service that is serving the VPN link
170 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
172 __connman_service_get_ip4config(data->service);
173 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
175 __connman_service_get_ip6config(data->service);
179 if (ipconfig != NULL) {
182 address = __connman_ipconfig_get_local(ipconfig);
183 config->vpn_phy_ip = g_strdup(address);
186 config->vpn_phy_index = data->index;
189 DBG("vpn %s phy %s index %d", config->vpn_ip,
190 config->vpn_phy_ip, config->vpn_phy_index);
193 static int del_routes(struct gateway_data *data,
194 enum connman_ipconfig_type type)
196 int status4 = 0, status6 = 0;
197 int do_ipv4 = FALSE, do_ipv6 = FALSE;
199 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
201 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
204 do_ipv4 = do_ipv6 = TRUE;
206 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL) {
207 if (data->ipv4_gateway->vpn == TRUE) {
208 status4 = connman_inet_clear_gateway_address(
210 data->ipv4_gateway->vpn_ip);
212 } else if (g_strcmp0(data->ipv4_gateway->gateway,
214 status4 = connman_inet_clear_gateway_interface(
217 connman_inet_del_host_route(data->index,
218 data->ipv4_gateway->gateway);
219 status4 = connman_inet_clear_gateway_address(
221 data->ipv4_gateway->gateway);
225 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL) {
226 if (data->ipv6_gateway->vpn == TRUE) {
227 status6 = connman_inet_clear_ipv6_gateway_address(
229 data->ipv6_gateway->vpn_ip);
231 } else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
232 status6 = connman_inet_clear_ipv6_gateway_interface(
235 connman_inet_del_ipv6_host_route(data->index,
236 data->ipv6_gateway->gateway);
237 status6 = connman_inet_clear_ipv6_gateway_address(
239 data->ipv6_gateway->gateway);
243 return (status4 < 0 ? status4 : status6);
246 static int disable_gateway(struct gateway_data *data,
247 enum connman_ipconfig_type type)
249 gboolean active = FALSE;
251 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
252 if (data->ipv4_gateway != NULL)
253 active = data->ipv4_gateway->active;
254 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
255 if (data->ipv6_gateway != NULL)
256 active = data->ipv6_gateway->active;
260 DBG("type %d active %d", type, active);
263 return del_routes(data, type);
268 static struct gateway_data *add_gateway(struct connman_service *service,
269 int index, const char *gateway,
270 enum connman_ipconfig_type type)
272 struct gateway_data *data, *old;
273 struct gateway_config *config;
275 if (gateway == NULL || strlen(gateway) == 0)
278 data = g_try_new0(struct gateway_data, 1);
284 config = g_try_new0(struct gateway_config, 1);
285 if (config == NULL) {
290 config->gateway = g_strdup(gateway);
291 config->vpn_ip = NULL;
292 config->vpn_phy_ip = NULL;
294 config->vpn_phy_index = -1;
295 config->active = FALSE;
297 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
298 data->ipv4_gateway = config;
299 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
300 data->ipv6_gateway = config;
302 g_free(config->gateway);
308 data->service = service;
310 data->order = __connman_service_get_order(service);
313 * If the service is already in the hash, then we
314 * must not replace it blindly but disable the gateway
315 * of the type we are replacing and take the other type
316 * from old gateway settings.
318 old = g_hash_table_lookup(gateway_hash, service);
320 DBG("Replacing gw %p ipv4 %p ipv6 %p", old,
321 old->ipv4_gateway, old->ipv6_gateway);
322 disable_gateway(old, type);
323 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
324 data->ipv6_gateway = old->ipv6_gateway;
325 old->ipv6_gateway = NULL;
326 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
327 data->ipv4_gateway = old->ipv4_gateway;
328 old->ipv4_gateway = NULL;
332 * Only take a ref if we are adding new stuff to hash.
334 connman_service_ref(service);
337 g_hash_table_replace(gateway_hash, service, data);
342 static void set_default_gateway(struct gateway_data *data,
343 enum connman_ipconfig_type type)
346 int status4 = 0, status6 = 0;
347 int do_ipv4 = FALSE, do_ipv6 = FALSE;
349 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
351 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
354 do_ipv4 = do_ipv6 = TRUE;
356 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
359 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
360 data->ipv4_gateway->vpn == TRUE) {
361 connman_inet_set_gateway_address(data->index,
362 data->ipv4_gateway->vpn_ip);
363 data->ipv4_gateway->active = TRUE;
365 DBG("set %p index %d vpn %s index %d phy %s",
366 data, data->index, data->ipv4_gateway->vpn_ip,
367 data->ipv4_gateway->vpn_phy_index,
368 data->ipv4_gateway->vpn_phy_ip);
370 __connman_service_indicate_default(data->service);
375 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
376 data->ipv6_gateway->vpn == TRUE) {
377 connman_inet_set_ipv6_gateway_address(data->index,
378 data->ipv6_gateway->vpn_ip);
379 data->ipv6_gateway->active = TRUE;
381 DBG("set %p index %d vpn %s index %d phy %s",
382 data, data->index, data->ipv6_gateway->vpn_ip,
383 data->ipv6_gateway->vpn_phy_index,
384 data->ipv6_gateway->vpn_phy_ip);
386 __connman_service_indicate_default(data->service);
391 index = __connman_service_get_index(data->service);
393 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
394 g_strcmp0(data->ipv4_gateway->gateway,
396 if (connman_inet_set_gateway_interface(index) < 0)
401 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
402 g_strcmp0(data->ipv6_gateway->gateway,
404 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
409 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
410 status6 = connman_inet_set_ipv6_gateway_address(index,
411 data->ipv6_gateway->gateway);
413 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
414 status4 = connman_inet_set_gateway_address(index,
415 data->ipv4_gateway->gateway);
417 if (status4 < 0 || status6 < 0)
421 __connman_service_indicate_default(data->service);
424 static void unset_default_gateway(struct gateway_data *data,
425 enum connman_ipconfig_type type)
428 int do_ipv4 = FALSE, do_ipv6 = FALSE;
430 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
432 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
435 do_ipv4 = do_ipv6 = TRUE;
437 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
440 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
441 data->ipv4_gateway->vpn == TRUE) {
442 connman_inet_del_host_route(data->index,
443 data->ipv4_gateway->vpn_ip);
444 connman_inet_clear_gateway_address(data->index,
445 data->ipv4_gateway->vpn_ip);
446 data->ipv4_gateway->active = FALSE;
448 DBG("unset %p index %d vpn %s index %d phy %s",
449 data, data->index, data->ipv4_gateway->vpn_ip,
450 data->ipv4_gateway->vpn_phy_index,
451 data->ipv4_gateway->vpn_phy_ip);
456 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
457 data->ipv6_gateway->vpn == TRUE) {
458 connman_inet_del_ipv6_host_route(data->index,
459 data->ipv6_gateway->vpn_ip);
460 connman_inet_clear_ipv6_gateway_address(data->index,
461 data->ipv6_gateway->vpn_ip);
462 data->ipv6_gateway->active = FALSE;
464 DBG("unset %p index %d vpn %s index %d phy %s",
465 data, data->index, data->ipv6_gateway->vpn_ip,
466 data->ipv6_gateway->vpn_phy_index,
467 data->ipv6_gateway->vpn_phy_ip);
472 index = __connman_service_get_index(data->service);
474 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
475 g_strcmp0(data->ipv4_gateway->gateway,
477 connman_inet_clear_gateway_interface(index);
481 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
482 g_strcmp0(data->ipv6_gateway->gateway,
484 connman_inet_clear_ipv6_gateway_interface(index);
488 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
489 connman_inet_clear_ipv6_gateway_address(index,
490 data->ipv6_gateway->gateway);
492 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
493 connman_inet_clear_gateway_address(index,
494 data->ipv4_gateway->gateway);
497 static struct gateway_data *find_default_gateway(void)
499 struct gateway_data *found = NULL;
500 unsigned int order = 0;
504 g_hash_table_iter_init(&iter, gateway_hash);
506 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
507 struct gateway_data *data = value;
509 if (found == NULL || data->order > order) {
513 DBG("default %p order %d", found, order);
520 static gboolean choose_default_gateway(struct gateway_data *data,
521 struct gateway_data *candidate)
523 gboolean downgraded = FALSE;
526 * If the current default is not active, then we mark
527 * this one as default. If the other one is already active
528 * we mark this one as non default.
530 if (data->ipv4_gateway != NULL) {
531 if (candidate->ipv4_gateway != NULL &&
532 candidate->ipv4_gateway->active == FALSE) {
533 DBG("ipv4 downgrading %p", candidate);
534 unset_default_gateway(candidate,
535 CONNMAN_IPCONFIG_TYPE_IPV4);
537 if (candidate->ipv4_gateway != NULL &&
538 candidate->ipv4_gateway->active == TRUE &&
539 candidate->order > data->order) {
540 DBG("ipv4 downgrading this %p", data);
541 unset_default_gateway(data,
542 CONNMAN_IPCONFIG_TYPE_IPV4);
547 if (data->ipv6_gateway != NULL) {
548 if (candidate->ipv6_gateway != NULL &&
549 candidate->ipv6_gateway->active == FALSE) {
550 DBG("ipv6 downgrading %p", candidate);
551 unset_default_gateway(candidate,
552 CONNMAN_IPCONFIG_TYPE_IPV6);
555 if (candidate->ipv6_gateway != NULL &&
556 candidate->ipv6_gateway->active == TRUE &&
557 candidate->order > data->order) {
558 DBG("ipv6 downgrading this %p", data);
559 unset_default_gateway(data,
560 CONNMAN_IPCONFIG_TYPE_IPV6);
568 static void connection_newgateway(int index, const char *gateway)
570 struct gateway_config *config;
571 struct gateway_data *data;
574 gboolean found = FALSE;
576 DBG("index %d gateway %s", index, gateway);
578 config = find_gateway(index, gateway);
582 config->active = TRUE;
585 * It is possible that we have two default routes atm
586 * if there are two gateways waiting rtnl activation at the
589 data = lookup_gateway_data(config);
593 if (data->default_checked == TRUE)
597 * The next checks are only done once, otherwise setting
598 * the default gateway could lead into rtnl forever loop.
601 g_hash_table_iter_init(&iter, gateway_hash);
603 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
604 struct gateway_data *candidate = value;
606 if (candidate == data)
609 found = choose_default_gateway(data, candidate);
614 if (found == FALSE) {
615 if (data->ipv4_gateway != NULL)
616 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);
618 if (data->ipv6_gateway != NULL)
619 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
622 data->default_checked = TRUE;
625 static void remove_gateway(gpointer user_data)
627 struct gateway_data *data = user_data;
629 DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
631 if (data->ipv4_gateway != NULL) {
632 g_free(data->ipv4_gateway->gateway);
633 g_free(data->ipv4_gateway->vpn_ip);
634 g_free(data->ipv4_gateway->vpn_phy_ip);
635 g_free(data->ipv4_gateway);
638 if (data->ipv6_gateway != NULL) {
639 g_free(data->ipv6_gateway->gateway);
640 g_free(data->ipv6_gateway->vpn_ip);
641 g_free(data->ipv6_gateway->vpn_phy_ip);
642 g_free(data->ipv6_gateway);
648 static void connection_delgateway(int index, const char *gateway)
650 struct gateway_config *config;
651 struct gateway_data *data;
653 DBG("index %d gateway %s", index, gateway);
655 config = find_gateway(index, gateway);
657 config->active = FALSE;
659 data = find_default_gateway();
661 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
664 static struct connman_rtnl connection_rtnl = {
665 .name = "connection",
666 .newgateway = connection_newgateway,
667 .delgateway = connection_delgateway,
670 static struct gateway_data *find_active_gateway(void)
677 g_hash_table_iter_init(&iter, gateway_hash);
679 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
680 struct gateway_data *data = value;
682 if (data->ipv4_gateway != NULL &&
683 data->ipv4_gateway->active == TRUE)
686 if (data->ipv6_gateway != NULL &&
687 data->ipv6_gateway->active == TRUE)
694 static void update_order(void)
701 g_hash_table_iter_init(&iter, gateway_hash);
703 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
704 struct gateway_data *data = value;
706 data->order = __connman_service_get_order(data->service);
710 void __connman_connection_gateway_activate(struct connman_service *service,
711 enum connman_ipconfig_type type)
713 struct gateway_data *data = NULL;
715 data = g_hash_table_lookup(gateway_hash, service);
719 DBG("gateway %p/%p type %d", data->ipv4_gateway,
720 data->ipv6_gateway, type);
722 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
723 data->ipv4_gateway->active = TRUE;
724 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
725 data->ipv6_gateway->active = TRUE;
728 int __connman_connection_gateway_add(struct connman_service *service,
730 enum connman_ipconfig_type type,
733 struct gateway_data *active_gateway = NULL;
734 struct gateway_data *new_gateway = NULL;
735 enum connman_ipconfig_type type4 = CONNMAN_IPCONFIG_TYPE_UNKNOWN,
736 type6 = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
739 index = __connman_service_get_index(service);
742 * If gateway is NULL, it's a point to point link and the default
743 * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
746 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
749 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
752 DBG("service %p index %d gateway %s vpn ip %s type %d",
753 service, index, gateway, peer, type);
755 new_gateway = add_gateway(service, index, gateway, type);
756 if (new_gateway == NULL)
759 active_gateway = find_active_gateway();
761 DBG("active %p index %d new %p", active_gateway,
762 active_gateway ? active_gateway->index : -1, new_gateway);
764 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
765 new_gateway->ipv6_gateway != NULL &&
766 g_strcmp0(new_gateway->ipv6_gateway->gateway,
768 connman_inet_add_ipv6_host_route(index,
769 new_gateway->ipv6_gateway->gateway,
772 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
773 new_gateway->ipv4_gateway != NULL &&
774 g_strcmp0(new_gateway->ipv4_gateway->gateway,
776 connman_inet_add_host_route(index,
777 new_gateway->ipv4_gateway->gateway,
780 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
781 new_gateway->ipv4_gateway != NULL) {
782 __connman_service_nameserver_add_routes(service,
783 new_gateway->ipv4_gateway->gateway);
784 type4 = CONNMAN_IPCONFIG_TYPE_IPV4;
787 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
788 new_gateway->ipv6_gateway != NULL) {
789 __connman_service_nameserver_add_routes(service,
790 new_gateway->ipv6_gateway->gateway);
791 type6 = CONNMAN_IPCONFIG_TYPE_IPV6;
794 if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
795 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
796 new_gateway->ipv4_gateway != NULL)
797 set_vpn_routes(new_gateway->ipv4_gateway,
798 service, gateway, type, peer);
800 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
801 new_gateway->ipv6_gateway != NULL)
802 set_vpn_routes(new_gateway->ipv6_gateway,
803 service, gateway, type, peer);
806 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
807 new_gateway->ipv4_gateway != NULL)
808 new_gateway->ipv4_gateway->vpn = FALSE;
810 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
811 new_gateway->ipv6_gateway != NULL)
812 new_gateway->ipv6_gateway->vpn = FALSE;
815 if (active_gateway == NULL) {
816 set_default_gateway(new_gateway, type);
820 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
821 new_gateway->ipv4_gateway != NULL &&
822 new_gateway->ipv4_gateway->vpn == TRUE) {
823 connman_inet_clear_gateway_address(active_gateway->index,
824 active_gateway->ipv4_gateway->gateway);
827 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
828 new_gateway->ipv6_gateway != NULL &&
829 new_gateway->ipv6_gateway->vpn == TRUE) {
830 connman_inet_clear_ipv6_gateway_address(active_gateway->index,
831 active_gateway->ipv6_gateway->gateway);
835 if (type4 == CONNMAN_IPCONFIG_TYPE_IPV4)
836 __connman_service_ipconfig_indicate_state(service,
837 CONNMAN_SERVICE_STATE_READY,
838 CONNMAN_IPCONFIG_TYPE_IPV4);
840 if (type6 == CONNMAN_IPCONFIG_TYPE_IPV6)
841 __connman_service_ipconfig_indicate_state(service,
842 CONNMAN_SERVICE_STATE_READY,
843 CONNMAN_IPCONFIG_TYPE_IPV6);
847 void __connman_connection_gateway_remove(struct connman_service *service,
848 enum connman_ipconfig_type type)
850 struct gateway_data *data = NULL;
851 gboolean set_default4 = FALSE, set_default6 = FALSE;
852 int do_ipv4 = FALSE, do_ipv6 = FALSE;
855 DBG("service %p type %d", service, type);
857 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
859 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
862 do_ipv4 = do_ipv6 = TRUE;
864 __connman_service_nameserver_del_routes(service, type);
866 data = g_hash_table_lookup(gateway_hash, service);
870 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
871 set_default4 = data->ipv4_gateway->vpn;
873 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
874 set_default6 = data->ipv6_gateway->vpn;
876 DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
877 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
878 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
879 set_default4, set_default6);
881 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
882 data->ipv4_gateway->vpn == TRUE && data->index >= 0)
883 connman_inet_del_host_route(data->index,
884 data->ipv4_gateway->gateway);
886 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
887 data->ipv6_gateway->vpn == TRUE && data->index >= 0)
888 connman_inet_del_ipv6_host_route(data->index,
889 data->ipv6_gateway->gateway);
891 err = disable_gateway(data, type);
894 * We remove the service from the hash only if all the gateway
895 * settings are to be removed.
897 if (do_ipv4 == do_ipv6 ||
898 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
899 && do_ipv4 == TRUE) ||
900 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
903 connman_service_unref(service);
904 g_hash_table_remove(gateway_hash, service);
906 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
907 data->ipv4_gateway, do_ipv4,
908 data->ipv6_gateway, do_ipv6);
910 /* with vpn this will be called after the network was deleted,
911 * we need to call set_default here because we will not recieve any
912 * gateway delete notification.
913 * We hit the same issue if remove_gateway() fails.
915 if (set_default4 || set_default6 || err < 0) {
916 data = find_default_gateway();
918 set_default_gateway(data, type);
922 gboolean __connman_connection_update_gateway(void)
924 struct gateway_data *default_gateway;
925 gboolean updated = FALSE;
929 if (gateway_hash == NULL)
934 default_gateway = find_default_gateway();
936 DBG("default %p", default_gateway);
939 * There can be multiple active gateways so we need to
942 g_hash_table_iter_init(&iter, gateway_hash);
944 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
945 struct gateway_data *active_gateway = value;
947 if (active_gateway == default_gateway)
950 if (active_gateway->ipv4_gateway != NULL &&
951 active_gateway->ipv4_gateway->active == TRUE) {
953 unset_default_gateway(active_gateway,
954 CONNMAN_IPCONFIG_TYPE_IPV4);
958 if (active_gateway->ipv6_gateway != NULL &&
959 active_gateway->ipv6_gateway->active == TRUE) {
961 unset_default_gateway(active_gateway,
962 CONNMAN_IPCONFIG_TYPE_IPV6);
967 if (updated && default_gateway != NULL) {
968 if (default_gateway->ipv4_gateway)
969 set_default_gateway(default_gateway,
970 CONNMAN_IPCONFIG_TYPE_IPV4);
972 if (default_gateway->ipv6_gateway)
973 set_default_gateway(default_gateway,
974 CONNMAN_IPCONFIG_TYPE_IPV6);
980 int __connman_connection_init(void)
986 gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
987 NULL, remove_gateway);
989 err = connman_rtnl_register(&connection_rtnl);
991 connman_error("Failed to setup RTNL gateway driver");
996 void __connman_connection_cleanup(void)
1003 connman_rtnl_unregister(&connection_rtnl);
1005 g_hash_table_iter_init(&iter, gateway_hash);
1007 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1008 struct gateway_data *data = value;
1010 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
1013 g_hash_table_destroy(gateway_hash);
1014 gateway_hash = NULL;