5 * Copyright (C) 2007-2012 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)
109 static struct gateway_data *find_vpn_gateway(int index, const char *gateway)
117 g_hash_table_iter_init(&iter, gateway_hash);
119 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
120 struct gateway_data *data = value;
122 if (data->ipv4_gateway != NULL && data->index == index &&
123 g_str_equal(data->ipv4_gateway->gateway,
127 if (data->ipv6_gateway != NULL && data->index == index &&
128 g_str_equal(data->ipv6_gateway->gateway,
136 struct get_gateway_params {
141 static void get_gateway_cb(const char *gateway, int index, void *user_data)
143 struct gateway_config *config;
144 struct gateway_data *data;
145 struct get_gateway_params *params = user_data;
151 DBG("phy index %d phy gw %s vpn index %d vpn gw %s", index, gateway,
152 params->vpn_index, params->vpn_gateway);
154 data = find_vpn_gateway(params->vpn_index, params->vpn_gateway);
156 DBG("Cannot find VPN link route, index %d addr %s",
157 params->vpn_index, params->vpn_gateway);
161 family = connman_inet_check_ipaddress(params->vpn_gateway);
163 if (family == AF_INET)
164 config = data->ipv4_gateway;
165 else if (family == AF_INET6)
166 config = data->ipv6_gateway;
170 config->vpn_phy_index = index;
172 DBG("vpn %s phy index %d", config->vpn_ip, config->vpn_phy_index);
175 g_free(params->vpn_gateway);
179 static void set_vpn_routes(struct gateway_data *new_gateway,
180 struct connman_service *service,
182 enum connman_ipconfig_type type,
184 struct gateway_data *active_gateway)
186 struct gateway_config *config;
187 struct connman_ipconfig *ipconfig;
190 DBG("new %p service %p gw %s type %d peer %s active %p",
191 new_gateway, service, gateway, type, peer, active_gateway);
193 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
194 ipconfig = __connman_service_get_ip4config(service);
195 config = new_gateway->ipv4_gateway;
196 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
197 ipconfig = __connman_service_get_ip6config(service);
198 config = new_gateway->ipv6_gateway;
202 if (config != NULL) {
203 int index = __connman_ipconfig_get_index(ipconfig);
204 struct get_gateway_params *params;
208 config->vpn_ip = g_strdup(peer);
209 else if (gateway != NULL)
210 config->vpn_ip = g_strdup(gateway);
212 params = g_try_malloc(sizeof(struct get_gateway_params));
216 params->vpn_index = index;
217 params->vpn_gateway = g_strdup(gateway);
220 * Find the gateway that is serving the VPN link
222 __connman_inet_get_route(gateway, get_gateway_cb, params);
225 if (active_gateway == NULL)
228 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
230 * Special route to VPN server via gateway. This
231 * is needed so that we can access hosts behind
232 * the VPN. The route might already exist depending
233 * on network topology.
235 if (active_gateway->ipv4_gateway == NULL)
238 DBG("active gw %s", active_gateway->ipv4_gateway->gateway);
240 if (g_strcmp0(active_gateway->ipv4_gateway->gateway,
242 dest = active_gateway->ipv4_gateway->gateway;
246 connman_inet_add_host_route(active_gateway->index, gateway,
249 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
251 if (active_gateway->ipv6_gateway == NULL)
254 DBG("active gw %s", active_gateway->ipv6_gateway->gateway);
256 if (g_strcmp0(active_gateway->ipv6_gateway->gateway,
258 dest = active_gateway->ipv6_gateway->gateway;
262 connman_inet_add_ipv6_host_route(active_gateway->index,
267 static int del_routes(struct gateway_data *data,
268 enum connman_ipconfig_type type)
270 int status4 = 0, status6 = 0;
271 int do_ipv4 = FALSE, do_ipv6 = FALSE;
273 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
275 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
278 do_ipv4 = do_ipv6 = TRUE;
280 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL) {
281 if (data->ipv4_gateway->vpn == TRUE) {
282 status4 = connman_inet_clear_gateway_address(
284 data->ipv4_gateway->vpn_ip);
286 } else if (g_strcmp0(data->ipv4_gateway->gateway,
288 status4 = connman_inet_clear_gateway_interface(
291 connman_inet_del_host_route(data->index,
292 data->ipv4_gateway->gateway);
293 status4 = connman_inet_clear_gateway_address(
295 data->ipv4_gateway->gateway);
299 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL) {
300 if (data->ipv6_gateway->vpn == TRUE) {
301 status6 = connman_inet_clear_ipv6_gateway_address(
303 data->ipv6_gateway->vpn_ip);
305 } else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
306 status6 = connman_inet_clear_ipv6_gateway_interface(
309 connman_inet_del_ipv6_host_route(data->index,
310 data->ipv6_gateway->gateway);
311 status6 = connman_inet_clear_ipv6_gateway_address(
313 data->ipv6_gateway->gateway);
317 return (status4 < 0 ? status4 : status6);
320 static int disable_gateway(struct gateway_data *data,
321 enum connman_ipconfig_type type)
323 gboolean active = FALSE;
325 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
326 if (data->ipv4_gateway != NULL)
327 active = data->ipv4_gateway->active;
328 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
329 if (data->ipv6_gateway != NULL)
330 active = data->ipv6_gateway->active;
334 DBG("type %d active %d", type, active);
337 return del_routes(data, type);
342 static struct gateway_data *add_gateway(struct connman_service *service,
343 int index, const char *gateway,
344 enum connman_ipconfig_type type)
346 struct gateway_data *data, *old;
347 struct gateway_config *config;
349 if (gateway == NULL || strlen(gateway) == 0)
352 data = g_try_new0(struct gateway_data, 1);
358 config = g_try_new0(struct gateway_config, 1);
359 if (config == NULL) {
364 config->gateway = g_strdup(gateway);
365 config->vpn_ip = NULL;
366 config->vpn_phy_ip = NULL;
368 config->vpn_phy_index = -1;
369 config->active = FALSE;
371 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
372 data->ipv4_gateway = config;
373 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
374 data->ipv6_gateway = config;
376 g_free(config->gateway);
382 data->service = service;
384 data->order = __connman_service_get_order(service);
387 * If the service is already in the hash, then we
388 * must not replace it blindly but disable the gateway
389 * of the type we are replacing and take the other type
390 * from old gateway settings.
392 old = g_hash_table_lookup(gateway_hash, service);
394 DBG("Replacing gw %p ipv4 %p ipv6 %p", old,
395 old->ipv4_gateway, old->ipv6_gateway);
396 disable_gateway(old, type);
397 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
398 data->ipv6_gateway = old->ipv6_gateway;
399 old->ipv6_gateway = NULL;
400 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
401 data->ipv4_gateway = old->ipv4_gateway;
402 old->ipv4_gateway = NULL;
406 * Only take a ref if we are adding new stuff to hash.
408 connman_service_ref(service);
411 g_hash_table_replace(gateway_hash, service, data);
416 static void set_default_gateway(struct gateway_data *data,
417 enum connman_ipconfig_type type)
420 int status4 = 0, status6 = 0;
421 int do_ipv4 = FALSE, do_ipv6 = FALSE;
423 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
425 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
428 do_ipv4 = do_ipv6 = TRUE;
430 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
433 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
434 data->ipv4_gateway->vpn == TRUE) {
435 connman_inet_set_gateway_interface(data->index);
436 data->ipv4_gateway->active = TRUE;
438 DBG("set %p index %d vpn %s index %d phy %s",
439 data, data->index, data->ipv4_gateway->vpn_ip,
440 data->ipv4_gateway->vpn_phy_index,
441 data->ipv4_gateway->vpn_phy_ip);
443 __connman_service_indicate_default(data->service);
448 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
449 data->ipv6_gateway->vpn == TRUE) {
450 connman_inet_set_ipv6_gateway_interface(data->index);
451 data->ipv6_gateway->active = TRUE;
453 DBG("set %p index %d vpn %s index %d phy %s",
454 data, data->index, data->ipv6_gateway->vpn_ip,
455 data->ipv6_gateway->vpn_phy_index,
456 data->ipv6_gateway->vpn_phy_ip);
458 __connman_service_indicate_default(data->service);
463 index = __connman_service_get_index(data->service);
465 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
466 g_strcmp0(data->ipv4_gateway->gateway,
468 if (connman_inet_set_gateway_interface(index) < 0)
473 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
474 g_strcmp0(data->ipv6_gateway->gateway,
476 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
481 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
482 status6 = connman_inet_set_ipv6_gateway_address(index,
483 data->ipv6_gateway->gateway);
485 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
486 status4 = connman_inet_set_gateway_address(index,
487 data->ipv4_gateway->gateway);
489 if (status4 < 0 || status6 < 0)
493 __connman_service_indicate_default(data->service);
496 static void unset_default_gateway(struct gateway_data *data,
497 enum connman_ipconfig_type type)
500 int do_ipv4 = FALSE, do_ipv6 = FALSE;
502 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
504 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
507 do_ipv4 = do_ipv6 = TRUE;
509 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
512 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
513 data->ipv4_gateway->vpn == TRUE) {
514 connman_inet_clear_gateway_interface(data->index);
515 data->ipv4_gateway->active = FALSE;
517 DBG("unset %p index %d vpn %s index %d phy %s",
518 data, data->index, data->ipv4_gateway->vpn_ip,
519 data->ipv4_gateway->vpn_phy_index,
520 data->ipv4_gateway->vpn_phy_ip);
525 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
526 data->ipv6_gateway->vpn == TRUE) {
527 connman_inet_clear_ipv6_gateway_interface(data->index);
528 data->ipv6_gateway->active = FALSE;
530 DBG("unset %p index %d vpn %s index %d phy %s",
531 data, data->index, data->ipv6_gateway->vpn_ip,
532 data->ipv6_gateway->vpn_phy_index,
533 data->ipv6_gateway->vpn_phy_ip);
538 index = __connman_service_get_index(data->service);
540 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
541 g_strcmp0(data->ipv4_gateway->gateway,
543 connman_inet_clear_gateway_interface(index);
547 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
548 g_strcmp0(data->ipv6_gateway->gateway,
550 connman_inet_clear_ipv6_gateway_interface(index);
554 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
555 connman_inet_clear_ipv6_gateway_address(index,
556 data->ipv6_gateway->gateway);
558 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
559 connman_inet_clear_gateway_address(index,
560 data->ipv4_gateway->gateway);
563 static struct gateway_data *find_default_gateway(void)
565 struct gateway_data *found = NULL;
566 unsigned int order = 0;
570 g_hash_table_iter_init(&iter, gateway_hash);
572 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
573 struct gateway_data *data = value;
575 if (found == NULL || data->order > order) {
579 DBG("default %p order %d", found, order);
586 static gboolean choose_default_gateway(struct gateway_data *data,
587 struct gateway_data *candidate)
589 gboolean downgraded = FALSE;
592 * If the current default is not active, then we mark
593 * this one as default. If the other one is already active
594 * we mark this one as non default.
596 if (data->ipv4_gateway != NULL) {
597 if (candidate->ipv4_gateway != NULL &&
598 candidate->ipv4_gateway->active == FALSE) {
599 DBG("ipv4 downgrading %p", candidate);
600 unset_default_gateway(candidate,
601 CONNMAN_IPCONFIG_TYPE_IPV4);
603 if (candidate->ipv4_gateway != NULL &&
604 candidate->ipv4_gateway->active == TRUE &&
605 candidate->order > data->order) {
606 DBG("ipv4 downgrading this %p", data);
607 unset_default_gateway(data,
608 CONNMAN_IPCONFIG_TYPE_IPV4);
613 if (data->ipv6_gateway != NULL) {
614 if (candidate->ipv6_gateway != NULL &&
615 candidate->ipv6_gateway->active == FALSE) {
616 DBG("ipv6 downgrading %p", candidate);
617 unset_default_gateway(candidate,
618 CONNMAN_IPCONFIG_TYPE_IPV6);
621 if (candidate->ipv6_gateway != NULL &&
622 candidate->ipv6_gateway->active == TRUE &&
623 candidate->order > data->order) {
624 DBG("ipv6 downgrading this %p", data);
625 unset_default_gateway(data,
626 CONNMAN_IPCONFIG_TYPE_IPV6);
634 static void connection_newgateway(int index, const char *gateway)
636 struct gateway_config *config;
637 struct gateway_data *data;
640 gboolean found = FALSE;
642 DBG("index %d gateway %s", index, gateway);
644 config = find_gateway(index, gateway);
648 config->active = TRUE;
651 * It is possible that we have two default routes atm
652 * if there are two gateways waiting rtnl activation at the
655 data = lookup_gateway_data(config);
659 if (data->default_checked == TRUE)
663 * The next checks are only done once, otherwise setting
664 * the default gateway could lead into rtnl forever loop.
667 g_hash_table_iter_init(&iter, gateway_hash);
669 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
670 struct gateway_data *candidate = value;
672 if (candidate == data)
675 found = choose_default_gateway(data, candidate);
680 if (found == FALSE) {
681 if (data->ipv4_gateway != NULL)
682 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);
684 if (data->ipv6_gateway != NULL)
685 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
688 data->default_checked = TRUE;
691 static void remove_gateway(gpointer user_data)
693 struct gateway_data *data = user_data;
695 DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
697 if (data->ipv4_gateway != NULL) {
698 g_free(data->ipv4_gateway->gateway);
699 g_free(data->ipv4_gateway->vpn_ip);
700 g_free(data->ipv4_gateway->vpn_phy_ip);
701 g_free(data->ipv4_gateway);
704 if (data->ipv6_gateway != NULL) {
705 g_free(data->ipv6_gateway->gateway);
706 g_free(data->ipv6_gateway->vpn_ip);
707 g_free(data->ipv6_gateway->vpn_phy_ip);
708 g_free(data->ipv6_gateway);
714 static void connection_delgateway(int index, const char *gateway)
716 struct gateway_config *config;
717 struct gateway_data *data;
719 DBG("index %d gateway %s", index, gateway);
721 config = find_gateway(index, gateway);
723 config->active = FALSE;
725 data = find_default_gateway();
727 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
730 static struct connman_rtnl connection_rtnl = {
731 .name = "connection",
732 .newgateway = connection_newgateway,
733 .delgateway = connection_delgateway,
736 static struct gateway_data *find_active_gateway(void)
743 g_hash_table_iter_init(&iter, gateway_hash);
745 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
746 struct gateway_data *data = value;
748 if (data->ipv4_gateway != NULL &&
749 data->ipv4_gateway->active == TRUE)
752 if (data->ipv6_gateway != NULL &&
753 data->ipv6_gateway->active == TRUE)
760 static void update_order(void)
767 g_hash_table_iter_init(&iter, gateway_hash);
769 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
770 struct gateway_data *data = value;
772 data->order = __connman_service_get_order(data->service);
776 void __connman_connection_gateway_activate(struct connman_service *service,
777 enum connman_ipconfig_type type)
779 struct gateway_data *data = NULL;
781 data = g_hash_table_lookup(gateway_hash, service);
785 DBG("gateway %p/%p type %d", data->ipv4_gateway,
786 data->ipv6_gateway, type);
788 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
789 data->ipv4_gateway->active = TRUE;
790 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
791 data->ipv6_gateway->active = TRUE;
794 static void add_host_route(int family, int index, const char *gateway,
795 enum connman_service_type service_type)
799 if (g_strcmp0(gateway, "0.0.0.0") != 0) {
801 * We must not set route to the phy dev gateway in
802 * VPN link. The packets to VPN link might be routed
803 * back to itself and not routed into phy link gateway.
805 if (service_type != CONNMAN_SERVICE_TYPE_VPN)
806 connman_inet_add_host_route(index, gateway,
810 * Add host route to P-t-P link so that services can
811 * be moved around and we can have some link to P-t-P
812 * network (although those P-t-P links have limited
813 * usage if default route is not directed to them)
816 if (connman_inet_get_dest_addr(index, &dest) == 0) {
817 connman_inet_add_host_route(index, dest, NULL);
824 if (g_strcmp0(gateway, "::") != 0) {
825 if (service_type != CONNMAN_SERVICE_TYPE_VPN)
826 connman_inet_add_ipv6_host_route(index,
829 /* P-t-P link, add route to destination */
831 if (connman_inet_ipv6_get_dest_addr(index,
833 connman_inet_add_ipv6_host_route(index, dest,
842 int __connman_connection_gateway_add(struct connman_service *service,
844 enum connman_ipconfig_type type,
847 struct gateway_data *active_gateway = NULL;
848 struct gateway_data *new_gateway = NULL;
849 enum connman_ipconfig_type type4 = CONNMAN_IPCONFIG_TYPE_UNKNOWN,
850 type6 = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
851 enum connman_service_type service_type =
852 connman_service_get_type(service);
855 index = __connman_service_get_index(service);
858 * If gateway is NULL, it's a point to point link and the default
859 * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
862 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
865 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
868 DBG("service %p index %d gateway %s vpn ip %s type %d",
869 service, index, gateway, peer, type);
871 new_gateway = add_gateway(service, index, gateway, type);
872 if (new_gateway == NULL)
875 active_gateway = find_active_gateway();
877 DBG("active %p index %d new %p", active_gateway,
878 active_gateway ? active_gateway->index : -1, new_gateway);
880 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
881 new_gateway->ipv4_gateway != NULL) {
882 add_host_route(AF_INET, index, gateway, service_type);
883 __connman_service_nameserver_add_routes(service,
884 new_gateway->ipv4_gateway->gateway);
885 type4 = CONNMAN_IPCONFIG_TYPE_IPV4;
888 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
889 new_gateway->ipv6_gateway != NULL) {
890 add_host_route(AF_INET6, index, gateway, service_type);
891 __connman_service_nameserver_add_routes(service,
892 new_gateway->ipv6_gateway->gateway);
893 type6 = CONNMAN_IPCONFIG_TYPE_IPV6;
896 if (service_type == CONNMAN_SERVICE_TYPE_VPN) {
898 set_vpn_routes(new_gateway, service, gateway, type, peer,
902 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
903 new_gateway->ipv4_gateway != NULL)
904 new_gateway->ipv4_gateway->vpn = FALSE;
906 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
907 new_gateway->ipv6_gateway != NULL)
908 new_gateway->ipv6_gateway->vpn = FALSE;
911 if (active_gateway == NULL) {
912 set_default_gateway(new_gateway, type);
916 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
917 new_gateway->ipv4_gateway != NULL &&
918 new_gateway->ipv4_gateway->vpn == TRUE) {
919 if (__connman_service_is_split_routing(new_gateway->service) ==
921 connman_inet_clear_gateway_address(
922 active_gateway->index,
923 active_gateway->ipv4_gateway->gateway);
926 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
927 new_gateway->ipv6_gateway != NULL &&
928 new_gateway->ipv6_gateway->vpn == TRUE) {
929 if (__connman_service_is_split_routing(new_gateway->service) ==
931 connman_inet_clear_ipv6_gateway_address(
932 active_gateway->index,
933 active_gateway->ipv6_gateway->gateway);
937 if (type4 == CONNMAN_IPCONFIG_TYPE_IPV4)
938 __connman_service_ipconfig_indicate_state(service,
939 CONNMAN_SERVICE_STATE_READY,
940 CONNMAN_IPCONFIG_TYPE_IPV4);
942 if (type6 == CONNMAN_IPCONFIG_TYPE_IPV6)
943 __connman_service_ipconfig_indicate_state(service,
944 CONNMAN_SERVICE_STATE_READY,
945 CONNMAN_IPCONFIG_TYPE_IPV6);
949 void __connman_connection_gateway_remove(struct connman_service *service,
950 enum connman_ipconfig_type type)
952 struct gateway_data *data = NULL;
953 gboolean set_default4 = FALSE, set_default6 = FALSE;
954 int do_ipv4 = FALSE, do_ipv6 = FALSE;
957 DBG("service %p type %d", service, type);
959 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
961 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
964 do_ipv4 = do_ipv6 = TRUE;
966 __connman_service_nameserver_del_routes(service, type);
968 data = g_hash_table_lookup(gateway_hash, service);
972 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
973 set_default4 = data->ipv4_gateway->vpn;
975 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
976 set_default6 = data->ipv6_gateway->vpn;
978 DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
979 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
980 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
981 set_default4, set_default6);
983 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
984 data->ipv4_gateway->vpn == TRUE && data->index >= 0)
985 connman_inet_del_host_route(data->index,
986 data->ipv4_gateway->gateway);
988 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
989 data->ipv6_gateway->vpn == TRUE && data->index >= 0)
990 connman_inet_del_ipv6_host_route(data->index,
991 data->ipv6_gateway->gateway);
993 err = disable_gateway(data, type);
996 * We remove the service from the hash only if all the gateway
997 * settings are to be removed.
999 if (do_ipv4 == do_ipv6 ||
1000 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
1001 && do_ipv4 == TRUE) ||
1002 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
1005 connman_service_unref(service);
1006 g_hash_table_remove(gateway_hash, service);
1008 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
1009 data->ipv4_gateway, do_ipv4,
1010 data->ipv6_gateway, do_ipv6);
1012 /* with vpn this will be called after the network was deleted,
1013 * we need to call set_default here because we will not recieve any
1014 * gateway delete notification.
1015 * We hit the same issue if remove_gateway() fails.
1017 if (set_default4 || set_default6 || err < 0) {
1018 data = find_default_gateway();
1020 set_default_gateway(data, type);
1024 gboolean __connman_connection_update_gateway(void)
1026 struct gateway_data *default_gateway;
1027 gboolean updated = FALSE;
1028 GHashTableIter iter;
1029 gpointer value, key;
1031 if (gateway_hash == NULL)
1036 default_gateway = find_default_gateway();
1038 __connman_service_update_ordering();
1040 DBG("default %p", default_gateway);
1043 * There can be multiple active gateways so we need to
1046 g_hash_table_iter_init(&iter, gateway_hash);
1048 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1049 struct gateway_data *active_gateway = value;
1051 if (active_gateway == default_gateway)
1054 if (active_gateway->ipv4_gateway != NULL &&
1055 active_gateway->ipv4_gateway->active == TRUE) {
1057 unset_default_gateway(active_gateway,
1058 CONNMAN_IPCONFIG_TYPE_IPV4);
1062 if (active_gateway->ipv6_gateway != NULL &&
1063 active_gateway->ipv6_gateway->active == TRUE) {
1065 unset_default_gateway(active_gateway,
1066 CONNMAN_IPCONFIG_TYPE_IPV6);
1071 if (updated && default_gateway != NULL) {
1072 if (default_gateway->ipv4_gateway)
1073 set_default_gateway(default_gateway,
1074 CONNMAN_IPCONFIG_TYPE_IPV4);
1076 if (default_gateway->ipv6_gateway)
1077 set_default_gateway(default_gateway,
1078 CONNMAN_IPCONFIG_TYPE_IPV6);
1084 int __connman_connection_get_vpn_index(int phy_index)
1086 GHashTableIter iter;
1087 gpointer value, key;
1089 g_hash_table_iter_init(&iter, gateway_hash);
1091 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1092 struct gateway_data *data = value;
1094 if (data->ipv4_gateway != NULL &&
1095 data->ipv4_gateway->vpn_phy_index == phy_index)
1098 if (data->ipv6_gateway != NULL &&
1099 data->ipv6_gateway->vpn_phy_index == phy_index)
1106 int __connman_connection_init(void)
1112 gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1113 NULL, remove_gateway);
1115 err = connman_rtnl_register(&connection_rtnl);
1117 connman_error("Failed to setup RTNL gateway driver");
1122 void __connman_connection_cleanup(void)
1124 GHashTableIter iter;
1125 gpointer value, key;
1129 connman_rtnl_unregister(&connection_rtnl);
1131 g_hash_table_iter_init(&iter, gateway_hash);
1133 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1134 struct gateway_data *data = value;
1136 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
1139 g_hash_table_destroy(gateway_hash);
1140 gateway_hash = NULL;