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 static void add_host_route(int family, int index, const char *gateway)
732 if (g_strcmp0(gateway, "0.0.0.0") != 0)
733 connman_inet_add_host_route(index, gateway, NULL);
736 * Add host route to P-t-P link so that services can
737 * be moved around and we can have some link to P-t-P
738 * network (although those P-t-P links have limited
739 * usage if default route is not directed to them)
742 if (connman_inet_get_dest_addr(index, &dest) == 0) {
743 connman_inet_add_host_route(index, dest, NULL);
750 if (g_strcmp0(gateway, "::") != 0)
751 connman_inet_add_ipv6_host_route(index, gateway, NULL);
753 /* P-t-P link, add route to destination */
755 if (connman_inet_ipv6_get_dest_addr(index,
757 connman_inet_add_ipv6_host_route(index, dest,
766 int __connman_connection_gateway_add(struct connman_service *service,
768 enum connman_ipconfig_type type,
771 struct gateway_data *active_gateway = NULL;
772 struct gateway_data *new_gateway = NULL;
773 enum connman_ipconfig_type type4 = CONNMAN_IPCONFIG_TYPE_UNKNOWN,
774 type6 = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
777 index = __connman_service_get_index(service);
780 * If gateway is NULL, it's a point to point link and the default
781 * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
784 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
787 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
790 DBG("service %p index %d gateway %s vpn ip %s type %d",
791 service, index, gateway, peer, type);
793 new_gateway = add_gateway(service, index, gateway, type);
794 if (new_gateway == NULL)
797 active_gateway = find_active_gateway();
799 DBG("active %p index %d new %p", active_gateway,
800 active_gateway ? active_gateway->index : -1, new_gateway);
802 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
803 new_gateway->ipv4_gateway != NULL) {
804 add_host_route(AF_INET, index, gateway);
805 __connman_service_nameserver_add_routes(service,
806 new_gateway->ipv4_gateway->gateway);
807 type4 = CONNMAN_IPCONFIG_TYPE_IPV4;
810 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
811 new_gateway->ipv6_gateway != NULL) {
812 add_host_route(AF_INET6, index, gateway);
813 __connman_service_nameserver_add_routes(service,
814 new_gateway->ipv6_gateway->gateway);
815 type6 = CONNMAN_IPCONFIG_TYPE_IPV6;
818 if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
819 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
820 new_gateway->ipv4_gateway != NULL)
821 set_vpn_routes(new_gateway->ipv4_gateway,
822 service, gateway, type, peer);
824 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
825 new_gateway->ipv6_gateway != NULL)
826 set_vpn_routes(new_gateway->ipv6_gateway,
827 service, gateway, type, peer);
830 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
831 new_gateway->ipv4_gateway != NULL)
832 new_gateway->ipv4_gateway->vpn = FALSE;
834 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
835 new_gateway->ipv6_gateway != NULL)
836 new_gateway->ipv6_gateway->vpn = FALSE;
839 if (active_gateway == NULL) {
840 set_default_gateway(new_gateway, type);
844 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
845 new_gateway->ipv4_gateway != NULL &&
846 new_gateway->ipv4_gateway->vpn == TRUE) {
847 if (__connman_service_is_split_routing(new_gateway->service) ==
849 connman_inet_clear_gateway_address(
850 active_gateway->index,
851 active_gateway->ipv4_gateway->gateway);
854 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
855 new_gateway->ipv6_gateway != NULL &&
856 new_gateway->ipv6_gateway->vpn == TRUE) {
857 if (__connman_service_is_split_routing(new_gateway->service) ==
859 connman_inet_clear_ipv6_gateway_address(
860 active_gateway->index,
861 active_gateway->ipv6_gateway->gateway);
865 if (type4 == CONNMAN_IPCONFIG_TYPE_IPV4)
866 __connman_service_ipconfig_indicate_state(service,
867 CONNMAN_SERVICE_STATE_READY,
868 CONNMAN_IPCONFIG_TYPE_IPV4);
870 if (type6 == CONNMAN_IPCONFIG_TYPE_IPV6)
871 __connman_service_ipconfig_indicate_state(service,
872 CONNMAN_SERVICE_STATE_READY,
873 CONNMAN_IPCONFIG_TYPE_IPV6);
877 void __connman_connection_gateway_remove(struct connman_service *service,
878 enum connman_ipconfig_type type)
880 struct gateway_data *data = NULL;
881 gboolean set_default4 = FALSE, set_default6 = FALSE;
882 int do_ipv4 = FALSE, do_ipv6 = FALSE;
885 DBG("service %p type %d", service, type);
887 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
889 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
892 do_ipv4 = do_ipv6 = TRUE;
894 __connman_service_nameserver_del_routes(service, type);
896 data = g_hash_table_lookup(gateway_hash, service);
900 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
901 set_default4 = data->ipv4_gateway->vpn;
903 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
904 set_default6 = data->ipv6_gateway->vpn;
906 DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
907 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
908 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
909 set_default4, set_default6);
911 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
912 data->ipv4_gateway->vpn == TRUE && data->index >= 0)
913 connman_inet_del_host_route(data->index,
914 data->ipv4_gateway->gateway);
916 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
917 data->ipv6_gateway->vpn == TRUE && data->index >= 0)
918 connman_inet_del_ipv6_host_route(data->index,
919 data->ipv6_gateway->gateway);
921 err = disable_gateway(data, type);
924 * We remove the service from the hash only if all the gateway
925 * settings are to be removed.
927 if (do_ipv4 == do_ipv6 ||
928 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
929 && do_ipv4 == TRUE) ||
930 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
933 connman_service_unref(service);
934 g_hash_table_remove(gateway_hash, service);
936 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
937 data->ipv4_gateway, do_ipv4,
938 data->ipv6_gateway, do_ipv6);
940 /* with vpn this will be called after the network was deleted,
941 * we need to call set_default here because we will not recieve any
942 * gateway delete notification.
943 * We hit the same issue if remove_gateway() fails.
945 if (set_default4 || set_default6 || err < 0) {
946 data = find_default_gateway();
948 set_default_gateway(data, type);
952 gboolean __connman_connection_update_gateway(void)
954 struct gateway_data *default_gateway;
955 gboolean updated = FALSE;
959 if (gateway_hash == NULL)
964 default_gateway = find_default_gateway();
966 __connman_service_update_ordering();
968 DBG("default %p", default_gateway);
971 * There can be multiple active gateways so we need to
974 g_hash_table_iter_init(&iter, gateway_hash);
976 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
977 struct gateway_data *active_gateway = value;
979 if (active_gateway == default_gateway)
982 if (active_gateway->ipv4_gateway != NULL &&
983 active_gateway->ipv4_gateway->active == TRUE) {
985 unset_default_gateway(active_gateway,
986 CONNMAN_IPCONFIG_TYPE_IPV4);
990 if (active_gateway->ipv6_gateway != NULL &&
991 active_gateway->ipv6_gateway->active == TRUE) {
993 unset_default_gateway(active_gateway,
994 CONNMAN_IPCONFIG_TYPE_IPV6);
999 if (updated && default_gateway != NULL) {
1000 if (default_gateway->ipv4_gateway)
1001 set_default_gateway(default_gateway,
1002 CONNMAN_IPCONFIG_TYPE_IPV4);
1004 if (default_gateway->ipv6_gateway)
1005 set_default_gateway(default_gateway,
1006 CONNMAN_IPCONFIG_TYPE_IPV6);
1012 int __connman_connection_init(void)
1018 gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1019 NULL, remove_gateway);
1021 err = connman_rtnl_register(&connection_rtnl);
1023 connman_error("Failed to setup RTNL gateway driver");
1028 void __connman_connection_cleanup(void)
1030 GHashTableIter iter;
1031 gpointer value, key;
1035 connman_rtnl_unregister(&connection_rtnl);
1037 g_hash_table_iter_init(&iter, gateway_hash);
1039 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1040 struct gateway_data *data = value;
1042 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
1045 g_hash_table_destroy(gateway_hash);
1046 gateway_hash = NULL;