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;
54 static GHashTable *gateway_hash = NULL;
56 static struct gateway_config *find_gateway(int index, const char *gateway)
64 g_hash_table_iter_init(&iter, gateway_hash);
66 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
67 struct gateway_data *data = value;
69 if (data->ipv4_gateway != NULL && data->index == index &&
70 g_str_equal(data->ipv4_gateway->gateway,
72 return data->ipv4_gateway;
74 if (data->ipv6_gateway != NULL && data->index == index &&
75 g_str_equal(data->ipv6_gateway->gateway,
77 return data->ipv6_gateway;
83 static int del_routes(struct gateway_data *data,
84 enum connman_ipconfig_type type)
86 int status4 = 0, status6 = 0;
87 int do_ipv4 = FALSE, do_ipv6 = FALSE;
89 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
91 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
94 do_ipv4 = do_ipv6 = TRUE;
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);
103 status4 = connman_inet_clear_gateway_address(
105 data->ipv4_gateway->vpn_ip);
107 } else if (g_strcmp0(data->ipv4_gateway->gateway,
109 status4 = connman_inet_clear_gateway_interface(
112 connman_inet_del_host_route(data->index,
113 data->ipv4_gateway->gateway);
114 status4 = connman_inet_clear_gateway_address(
116 data->ipv4_gateway->gateway);
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);
127 status6 = connman_inet_clear_ipv6_gateway_address(
129 data->ipv6_gateway->vpn_ip);
131 } else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
132 status6 = connman_inet_clear_ipv6_gateway_interface(
135 connman_inet_del_ipv6_host_route(data->index,
136 data->ipv6_gateway->gateway);
137 status6 = connman_inet_clear_ipv6_gateway_address(
139 data->ipv6_gateway->gateway);
143 return (status4 < 0 ? status4 : status6);
146 static int disable_gateway(struct gateway_data *data,
147 enum connman_ipconfig_type type)
149 gboolean active = FALSE;
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;
160 DBG("type %d active %d", type, active);
163 return del_routes(data, type);
168 static struct gateway_data *add_gateway(struct connman_service *service,
169 int index, const char *gateway,
170 enum connman_ipconfig_type type)
172 struct gateway_data *data, *old;
173 struct gateway_config *config;
175 if (gateway == NULL || strlen(gateway) == 0)
178 data = g_try_new0(struct gateway_data, 1);
184 config = g_try_new0(struct gateway_config, 1);
185 if (config == NULL) {
190 config->gateway = g_strdup(gateway);
191 config->vpn_ip = NULL;
192 config->vpn_phy_ip = NULL;
194 config->vpn_phy_index = -1;
195 config->active = FALSE;
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;
202 g_free(config->gateway);
208 data->service = service;
210 data->order = __connman_service_get_order(service);
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.
218 old = g_hash_table_lookup(gateway_hash, service);
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;
232 * Only take a ref if we are adding new stuff to hash.
234 connman_service_ref(service);
237 g_hash_table_replace(gateway_hash, service, data);
242 static void connection_newgateway(int index, const char *gateway)
244 struct gateway_config *config;
246 DBG("index %d gateway %s", index, gateway);
248 config = find_gateway(index, gateway);
252 config->active = TRUE;
255 static void set_default_gateway(struct gateway_data *data,
256 enum connman_ipconfig_type type)
259 int status4 = 0, status6 = 0;
260 int do_ipv4 = FALSE, do_ipv6 = FALSE;
262 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
264 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
267 do_ipv4 = do_ipv6 = TRUE;
269 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
272 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
273 data->ipv4_gateway->vpn == TRUE) {
274 connman_inet_set_gateway_address(data->index,
275 data->ipv4_gateway->vpn_ip);
276 connman_inet_add_host_route(data->ipv4_gateway->vpn_phy_index,
277 data->ipv4_gateway->vpn_ip,
278 data->ipv4_gateway->vpn_phy_ip);
279 data->ipv4_gateway->active = TRUE;
281 __connman_service_indicate_default(data->service);
286 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
287 data->ipv6_gateway->vpn == TRUE) {
288 connman_inet_set_ipv6_gateway_address(data->index,
289 data->ipv6_gateway->vpn_ip);
290 connman_inet_add_ipv6_host_route(
291 data->ipv6_gateway->vpn_phy_index,
292 data->ipv6_gateway->vpn_ip,
293 data->ipv6_gateway->vpn_phy_ip);
294 data->ipv6_gateway->active = TRUE;
296 __connman_service_indicate_default(data->service);
301 index = __connman_service_get_index(data->service);
303 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
304 g_strcmp0(data->ipv4_gateway->gateway,
306 if (connman_inet_set_gateway_interface(index) < 0)
311 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
312 g_strcmp0(data->ipv6_gateway->gateway,
314 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
319 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
320 status6 = connman_inet_set_ipv6_gateway_address(index,
321 data->ipv6_gateway->gateway);
323 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
324 status4 = connman_inet_set_gateway_address(index,
325 data->ipv4_gateway->gateway);
327 if (status4 < 0 || status6 < 0)
331 __connman_service_indicate_default(data->service);
334 static void unset_default_gateway(struct gateway_data *data,
335 enum connman_ipconfig_type type)
338 int do_ipv4 = FALSE, do_ipv6 = FALSE;
340 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
342 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
345 do_ipv4 = do_ipv6 = TRUE;
347 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
350 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
351 data->ipv4_gateway->vpn == TRUE) {
352 connman_inet_del_host_route(data->index,
353 data->ipv4_gateway->vpn_ip);
354 connman_inet_clear_gateway_address(data->index,
355 data->ipv4_gateway->vpn_ip);
356 data->ipv4_gateway->active = FALSE;
361 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
362 data->ipv6_gateway->vpn == TRUE) {
363 connman_inet_del_ipv6_host_route(data->index,
364 data->ipv6_gateway->vpn_ip);
365 connman_inet_clear_ipv6_gateway_address(data->index,
366 data->ipv6_gateway->vpn_ip);
367 data->ipv6_gateway->active = FALSE;
372 index = __connman_service_get_index(data->service);
374 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
375 g_strcmp0(data->ipv4_gateway->gateway,
377 connman_inet_clear_gateway_interface(index);
381 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
382 g_strcmp0(data->ipv6_gateway->gateway,
384 connman_inet_clear_ipv6_gateway_interface(index);
388 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
389 connman_inet_clear_ipv6_gateway_address(index,
390 data->ipv6_gateway->gateway);
392 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
393 connman_inet_clear_gateway_address(index,
394 data->ipv4_gateway->gateway);
397 static struct gateway_data *find_default_gateway(void)
399 struct gateway_data *found = NULL;
400 unsigned int order = 0;
404 g_hash_table_iter_init(&iter, gateway_hash);
406 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
407 struct gateway_data *data = value;
409 if (found == NULL || data->order > order) {
418 static void remove_gateway(gpointer user_data)
420 struct gateway_data *data = user_data;
422 DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
424 if (data->ipv4_gateway != NULL) {
425 g_free(data->ipv4_gateway->gateway);
426 g_free(data->ipv4_gateway->vpn_ip);
427 g_free(data->ipv4_gateway->vpn_phy_ip);
428 g_free(data->ipv4_gateway);
431 if (data->ipv6_gateway != NULL) {
432 g_free(data->ipv6_gateway->gateway);
433 g_free(data->ipv6_gateway->vpn_ip);
434 g_free(data->ipv6_gateway->vpn_phy_ip);
435 g_free(data->ipv6_gateway);
441 static void connection_delgateway(int index, const char *gateway)
443 struct gateway_config *config;
444 struct gateway_data *data;
446 DBG("index %d gateway %s", index, gateway);
448 config = find_gateway(index, gateway);
450 config->active = FALSE;
452 data = find_default_gateway();
454 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
457 static struct connman_rtnl connection_rtnl = {
458 .name = "connection",
459 .newgateway = connection_newgateway,
460 .delgateway = connection_delgateway,
463 static struct gateway_data *find_active_gateway(void)
470 g_hash_table_iter_init(&iter, gateway_hash);
472 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
473 struct gateway_data *data = value;
475 if (data->ipv4_gateway != NULL &&
476 data->ipv4_gateway->active == TRUE)
479 if (data->ipv6_gateway != NULL &&
480 data->ipv6_gateway->active == TRUE)
487 static void update_order(void)
494 g_hash_table_iter_init(&iter, gateway_hash);
496 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
497 struct gateway_data *data = value;
499 data->order = __connman_service_get_order(data->service);
503 void __connman_connection_gateway_activate(struct connman_service *service,
504 enum connman_ipconfig_type type)
506 struct gateway_data *data = NULL;
508 data = g_hash_table_lookup(gateway_hash, service);
512 DBG("gateway %p/%p type %d", data->ipv4_gateway,
513 data->ipv6_gateway, type);
515 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
516 data->ipv4_gateway->active = TRUE;
517 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
518 data->ipv6_gateway->active = TRUE;
521 int __connman_connection_gateway_add(struct connman_service *service,
523 enum connman_ipconfig_type type,
526 struct gateway_data *active_gateway = NULL;
527 struct gateway_data *new_gateway = NULL;
530 index = __connman_service_get_index(service);
532 DBG("service %p index %d gateway %s vpn ip %s type %d",
533 service, index, gateway, peer, type);
536 * If gateway is NULL, it's a point to point link and the default
537 * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
540 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
543 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
546 active_gateway = find_active_gateway();
547 new_gateway = add_gateway(service, index, gateway, type);
548 if (new_gateway == NULL)
551 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
552 new_gateway->ipv6_gateway != NULL &&
553 g_strcmp0(new_gateway->ipv6_gateway->gateway,
555 connman_inet_add_ipv6_host_route(index,
556 new_gateway->ipv6_gateway->gateway,
559 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
560 new_gateway->ipv4_gateway != NULL &&
561 g_strcmp0(new_gateway->ipv4_gateway->gateway,
563 connman_inet_add_host_route(index,
564 new_gateway->ipv4_gateway->gateway,
567 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
568 new_gateway->ipv4_gateway != NULL) {
569 __connman_service_nameserver_add_routes(service,
570 new_gateway->ipv4_gateway->gateway);
571 __connman_service_ipconfig_indicate_state(service,
572 CONNMAN_SERVICE_STATE_READY,
573 CONNMAN_IPCONFIG_TYPE_IPV4);
576 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
577 new_gateway->ipv6_gateway != NULL) {
578 __connman_service_nameserver_add_routes(service,
579 new_gateway->ipv6_gateway->gateway);
580 __connman_service_ipconfig_indicate_state(service,
581 CONNMAN_SERVICE_STATE_READY,
582 CONNMAN_IPCONFIG_TYPE_IPV6);
585 if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_VPN) {
586 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
587 new_gateway->ipv4_gateway != NULL) {
588 new_gateway->ipv4_gateway->vpn = TRUE;
590 new_gateway->ipv4_gateway->vpn_ip =
592 else if (gateway != NULL)
593 new_gateway->ipv4_gateway->vpn_ip =
595 if (active_gateway) {
596 const char *new_ipv4_gateway;
599 active_gateway->ipv4_gateway->gateway;
600 if (new_ipv4_gateway != NULL &&
601 g_strcmp0(new_ipv4_gateway,
603 new_gateway->ipv4_gateway->vpn_phy_ip =
604 g_strdup(new_ipv4_gateway);
606 new_gateway->ipv4_gateway->vpn_phy_index =
607 active_gateway->index;
610 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
611 new_gateway->ipv6_gateway != NULL) {
612 new_gateway->ipv6_gateway->vpn = TRUE;
614 new_gateway->ipv6_gateway->vpn_ip =
616 else if (gateway != NULL)
617 new_gateway->ipv6_gateway->vpn_ip =
619 if (active_gateway) {
620 const char *new_ipv6_gateway;
623 active_gateway->ipv6_gateway->gateway;
624 if (new_ipv6_gateway != NULL &&
625 g_strcmp0(new_ipv6_gateway, "::") != 0)
626 new_gateway->ipv6_gateway->vpn_phy_ip =
627 g_strdup(new_ipv6_gateway);
629 new_gateway->ipv6_gateway->vpn_phy_index =
630 active_gateway->index;
634 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
635 new_gateway->ipv4_gateway != NULL)
636 new_gateway->ipv4_gateway->vpn = FALSE;
638 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
639 new_gateway->ipv6_gateway != NULL)
640 new_gateway->ipv6_gateway->vpn = FALSE;
643 if (active_gateway == NULL) {
644 set_default_gateway(new_gateway, type);
648 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
649 new_gateway->ipv4_gateway != NULL &&
650 new_gateway->ipv4_gateway->vpn == TRUE) {
651 connman_inet_add_host_route(active_gateway->index,
652 new_gateway->ipv4_gateway->gateway,
653 active_gateway->ipv4_gateway->gateway);
654 connman_inet_clear_gateway_address(active_gateway->index,
655 active_gateway->ipv4_gateway->gateway);
658 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
659 new_gateway->ipv6_gateway != NULL &&
660 new_gateway->ipv6_gateway->vpn == TRUE) {
661 connman_inet_add_ipv6_host_route(active_gateway->index,
662 new_gateway->ipv6_gateway->gateway,
663 active_gateway->ipv6_gateway->gateway);
664 connman_inet_clear_ipv6_gateway_address(active_gateway->index,
665 active_gateway->ipv6_gateway->gateway);
671 void __connman_connection_gateway_remove(struct connman_service *service,
672 enum connman_ipconfig_type type)
674 struct gateway_data *data = NULL;
675 gboolean set_default4 = FALSE, set_default6 = FALSE;
676 int do_ipv4 = FALSE, do_ipv6 = FALSE;
679 DBG("service %p type %d", service, type);
681 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
683 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
686 do_ipv4 = do_ipv6 = TRUE;
688 __connman_service_nameserver_del_routes(service);
690 data = g_hash_table_lookup(gateway_hash, service);
694 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
695 set_default4 = data->ipv4_gateway->vpn;
697 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
698 set_default6 = data->ipv6_gateway->vpn;
700 DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
701 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
702 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
703 set_default4, set_default6);
705 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
706 data->ipv4_gateway->vpn == TRUE && data->index >= 0)
707 connman_inet_del_host_route(data->index,
708 data->ipv4_gateway->gateway);
710 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
711 data->ipv6_gateway->vpn == TRUE && data->index >= 0)
712 connman_inet_del_ipv6_host_route(data->index,
713 data->ipv6_gateway->gateway);
715 __connman_service_nameserver_del_routes(service);
717 err = disable_gateway(data, type);
720 * We remove the service from the hash only if all the gateway
721 * settings are to be removed.
723 if (do_ipv4 == do_ipv6 ||
724 (data->ipv4_gateway != NULL && data->ipv6_gateway == NULL
725 && do_ipv4 == TRUE) ||
726 (data->ipv6_gateway != NULL && data->ipv4_gateway == NULL
729 connman_service_unref(service);
730 g_hash_table_remove(gateway_hash, service);
732 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
733 data->ipv4_gateway, do_ipv4,
734 data->ipv6_gateway, do_ipv6);
736 /* with vpn this will be called after the network was deleted,
737 * we need to call set_default here because we will not recieve any
738 * gateway delete notification.
739 * We hit the same issue if remove_gateway() fails.
741 if (set_default4 || set_default6 || err < 0) {
742 data = find_default_gateway();
744 set_default_gateway(data, type);
748 gboolean __connman_connection_update_gateway(void)
750 struct gateway_data *active_gateway, *default_gateway;
751 gboolean updated = FALSE;
753 if (gateway_hash == NULL)
756 active_gateway = find_active_gateway();
760 default_gateway = find_default_gateway();
762 if (active_gateway && active_gateway != default_gateway) {
765 if (active_gateway->ipv4_gateway)
766 unset_default_gateway(active_gateway,
767 CONNMAN_IPCONFIG_TYPE_IPV4);
769 if (active_gateway->ipv6_gateway)
770 unset_default_gateway(active_gateway,
771 CONNMAN_IPCONFIG_TYPE_IPV6);
773 if (default_gateway) {
774 if (default_gateway->ipv4_gateway)
775 set_default_gateway(default_gateway,
776 CONNMAN_IPCONFIG_TYPE_IPV4);
778 if (default_gateway->ipv6_gateway)
779 set_default_gateway(default_gateway,
780 CONNMAN_IPCONFIG_TYPE_IPV6);
787 int __connman_connection_init(void)
793 gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
794 NULL, remove_gateway);
796 err = connman_rtnl_register(&connection_rtnl);
798 connman_error("Failed to setup RTNL gateway driver");
803 void __connman_connection_cleanup(void)
810 connman_rtnl_unregister(&connection_rtnl);
812 g_hash_table_iter_init(&iter, gateway_hash);
814 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
815 struct gateway_data *data = value;
817 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
820 g_hash_table_destroy(gateway_hash);