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 g_hash_table_replace(gateway_hash, service, data);
237 static void connection_newgateway(int index, const char *gateway)
239 struct gateway_config *config;
241 DBG("index %d gateway %s", index, gateway);
243 config = find_gateway(index, gateway);
247 config->active = TRUE;
250 static void set_default_gateway(struct gateway_data *data,
251 enum connman_ipconfig_type type)
254 int status4 = 0, status6 = 0;
255 int do_ipv4 = FALSE, do_ipv6 = FALSE;
257 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
259 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
262 do_ipv4 = do_ipv6 = TRUE;
264 DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
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;
276 __connman_service_indicate_default(data->service);
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;
290 __connman_service_indicate_default(data->service);
295 index = __connman_service_get_index(data->service);
297 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL &&
298 g_strcmp0(data->ipv4_gateway->gateway,
300 if (connman_inet_set_gateway_interface(index) < 0)
305 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL &&
306 g_strcmp0(data->ipv6_gateway->gateway,
308 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
313 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
314 status6 = connman_inet_set_ipv6_gateway_address(index,
315 data->ipv6_gateway->gateway);
317 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
318 status4 = connman_inet_set_gateway_address(index,
319 data->ipv4_gateway->gateway);
321 if (status4 < 0 || status6 < 0)
325 __connman_service_indicate_default(data->service);
328 static struct gateway_data *find_default_gateway(void)
330 struct gateway_data *found = NULL;
331 unsigned int order = 0;
335 g_hash_table_iter_init(&iter, gateway_hash);
337 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
338 struct gateway_data *data = value;
340 if (found == NULL || data->order > order) {
349 static void remove_gateway(gpointer user_data)
351 struct gateway_data *data = user_data;
353 DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
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);
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);
372 static void connection_delgateway(int index, const char *gateway)
374 struct gateway_config *config;
375 struct gateway_data *data;
377 DBG("index %d gateway %s", index, gateway);
379 config = find_gateway(index, gateway);
381 config->active = FALSE;
383 data = find_default_gateway();
385 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
388 static struct connman_rtnl connection_rtnl = {
389 .name = "connection",
390 .newgateway = connection_newgateway,
391 .delgateway = connection_delgateway,
394 static struct gateway_data *find_active_gateway(void)
401 g_hash_table_iter_init(&iter, gateway_hash);
403 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
404 struct gateway_data *data = value;
406 if (data->ipv4_gateway != NULL &&
407 data->ipv4_gateway->active == TRUE)
410 if (data->ipv6_gateway != NULL &&
411 data->ipv6_gateway->active == TRUE)
418 static void update_order(void)
425 g_hash_table_iter_init(&iter, gateway_hash);
427 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
428 struct gateway_data *data = value;
430 data->order = __connman_service_get_order(data->service);
434 void __connman_connection_gateway_activate(struct connman_service *service,
435 enum connman_ipconfig_type type)
437 struct gateway_data *data = NULL;
439 data = g_hash_table_lookup(gateway_hash, service);
443 DBG("gateway %p/%p type %d", data->ipv4_gateway,
444 data->ipv6_gateway, type);
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;
452 int __connman_connection_gateway_add(struct connman_service *service,
454 enum connman_ipconfig_type type,
457 struct gateway_data *active_gateway = NULL;
458 struct gateway_data *new_gateway = NULL;
461 index = __connman_service_get_index(service);
463 DBG("service %p index %d gateway %s vpn ip %s type %d",
464 service, index, gateway, peer, type);
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
471 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV4)
474 if (gateway == NULL && type == CONNMAN_IPCONFIG_TYPE_IPV6)
477 active_gateway = find_active_gateway();
478 new_gateway = add_gateway(service, index, gateway, type);
479 if (new_gateway == NULL)
482 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
483 new_gateway->ipv6_gateway != NULL &&
484 g_strcmp0(new_gateway->ipv6_gateway->gateway,
486 connman_inet_add_ipv6_host_route(index,
487 new_gateway->ipv6_gateway->gateway,
490 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
491 new_gateway->ipv4_gateway != NULL &&
492 g_strcmp0(new_gateway->ipv4_gateway->gateway,
494 connman_inet_add_host_route(index,
495 new_gateway->ipv4_gateway->gateway,
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);
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);
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;
521 new_gateway->ipv4_gateway->vpn_ip =
523 else if (gateway != NULL)
524 new_gateway->ipv4_gateway->vpn_ip =
526 if (active_gateway) {
527 const char *new_ipv4_gateway;
530 active_gateway->ipv4_gateway->gateway;
531 if (new_ipv4_gateway != NULL &&
532 g_strcmp0(new_ipv4_gateway,
534 new_gateway->ipv4_gateway->vpn_phy_ip =
535 g_strdup(new_ipv4_gateway);
537 new_gateway->ipv4_gateway->vpn_phy_index =
538 active_gateway->index;
541 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
542 new_gateway->ipv6_gateway != NULL) {
543 new_gateway->ipv6_gateway->vpn = TRUE;
545 new_gateway->ipv6_gateway->vpn_ip =
547 else if (gateway != NULL)
548 new_gateway->ipv6_gateway->vpn_ip =
550 if (active_gateway) {
551 const char *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);
560 new_gateway->ipv6_gateway->vpn_phy_index =
561 active_gateway->index;
565 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
566 new_gateway->ipv4_gateway != NULL)
567 new_gateway->ipv4_gateway->vpn = FALSE;
569 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
570 new_gateway->ipv6_gateway != NULL)
571 new_gateway->ipv6_gateway->vpn = FALSE;
574 if (active_gateway == NULL) {
575 set_default_gateway(new_gateway, type);
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);
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);
602 void __connman_connection_gateway_remove(struct connman_service *service,
603 enum connman_ipconfig_type type)
605 struct gateway_data *data = NULL;
606 gboolean set_default4 = FALSE, set_default6 = FALSE;
607 int do_ipv4 = FALSE, do_ipv6 = FALSE;
610 DBG("service %p type %d", service, type);
612 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
614 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
617 do_ipv4 = do_ipv6 = TRUE;
619 __connman_service_nameserver_del_routes(service);
621 data = g_hash_table_lookup(gateway_hash, service);
625 if (do_ipv4 == TRUE && data->ipv4_gateway != NULL)
626 set_default4 = data->ipv4_gateway->vpn;
628 if (do_ipv6 == TRUE && data->ipv6_gateway != NULL)
629 set_default6 = data->ipv6_gateway->vpn;
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);
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);
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);
646 __connman_service_nameserver_del_routes(service);
648 err = disable_gateway(data, type);
651 * We remove the service from the hash only if all the gateway
652 * settings are to be removed.
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
660 g_hash_table_remove(gateway_hash, service);
662 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
663 data->ipv4_gateway, do_ipv4,
664 data->ipv6_gateway, do_ipv6);
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.
671 if (set_default4 || set_default6 || err < 0) {
672 data = find_default_gateway();
674 set_default_gateway(data, type);
678 gboolean __connman_connection_update_gateway(void)
680 struct gateway_data *active_gateway, *default_gateway;
681 gboolean updated = FALSE;
683 if (gateway_hash == NULL)
688 active_gateway = find_active_gateway();
689 default_gateway = find_default_gateway();
691 if (active_gateway && active_gateway != default_gateway)
697 int __connman_connection_init(void)
703 gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
704 NULL, remove_gateway);
706 err = connman_rtnl_register(&connection_rtnl);
708 connman_error("Failed to setup RTNL gateway driver");
713 void __connman_connection_cleanup(void)
720 connman_rtnl_unregister(&connection_rtnl);
722 g_hash_table_iter_init(&iter, gateway_hash);
724 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
725 struct gateway_data *data = value;
727 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
730 g_hash_table_destroy(gateway_hash);