connection: Set host route to VPN server
[framework/connectivity/connman.git] / src / connection.c
index a2acc51..f4abd57 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *  Copyright (C) 2011  BMW Car IT GmbH. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -360,6 +360,8 @@ static void set_default_gateway(struct gateway_data *data,
                                        data->ipv4_gateway->vpn == TRUE) {
                connman_inet_set_gateway_address(data->index,
                                                data->ipv4_gateway->vpn_ip);
+               connman_inet_add_host_route(data->index,
+                                       data->ipv4_gateway->vpn_ip, NULL);
                data->ipv4_gateway->active = TRUE;
 
                DBG("set %p index %d vpn %s index %d phy %s",
@@ -376,6 +378,8 @@ static void set_default_gateway(struct gateway_data *data,
                                        data->ipv6_gateway->vpn == TRUE) {
                connman_inet_set_ipv6_gateway_address(data->index,
                                                data->ipv6_gateway->vpn_ip);
+               connman_inet_add_ipv6_host_route(data->index,
+                                       data->ipv6_gateway->vpn_ip, NULL);
                data->ipv6_gateway->active = TRUE;
 
                DBG("set %p index %d vpn %s index %d phy %s",
@@ -828,15 +832,31 @@ int __connman_connection_gateway_add(struct connman_service *service,
        }
 
        if (service_type == CONNMAN_SERVICE_TYPE_VPN) {
-               if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
-                                       new_gateway->ipv4_gateway != NULL)
-                       set_vpn_routes(new_gateway->ipv4_gateway,
-                               service, gateway, type, peer);
 
-               else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
-                                       new_gateway->ipv6_gateway != NULL)
-                       set_vpn_routes(new_gateway->ipv6_gateway,
-                               service, gateway, type, peer);
+               if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
+                       if (new_gateway->ipv4_gateway != NULL)
+                               set_vpn_routes(new_gateway->ipv4_gateway,
+                                       service, gateway, type, peer);
+
+                       /*
+                        * Special route to VPN server via gateway. This
+                        * is needed so that we can access hosts behind
+                        * the VPN. The route might already exist depending
+                        * on network topology.
+                        */
+                       connman_inet_add_host_route(active_gateway->index,
+                                       gateway,
+                                       active_gateway->ipv4_gateway->gateway);
+
+               } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
+                       if (new_gateway->ipv6_gateway != NULL)
+                               set_vpn_routes(new_gateway->ipv6_gateway,
+                                       service, gateway, type, peer);
+
+                       connman_inet_add_ipv6_host_route(active_gateway->index,
+                                       gateway,
+                                       active_gateway->ipv6_gateway->gateway);
+               }
 
        } else {
                if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
@@ -1021,6 +1041,28 @@ gboolean __connman_connection_update_gateway(void)
        return updated;
 }
 
+int __connman_connection_get_vpn_index(int phy_index)
+{
+       GHashTableIter iter;
+       gpointer value, key;
+
+       g_hash_table_iter_init(&iter, gateway_hash);
+
+       while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+               struct gateway_data *data = value;
+
+               if (data->ipv4_gateway != NULL &&
+                               data->ipv4_gateway->vpn_phy_index == phy_index)
+                       return data->index;
+
+               if (data->ipv6_gateway != NULL &&
+                               data->ipv6_gateway->vpn_phy_index == phy_index)
+                       return data->index;
+       }
+
+       return -1;
+}
+
 int __connman_connection_init(void)
 {
        int err;