+static struct gateway_data *lookup_gateway_data(struct gateway_config *config)
+{
+ GHashTableIter iter;
+ gpointer value, key;
+
+ if (!config)
+ return NULL;
+
+ g_hash_table_iter_init(&iter, gateway_hash);
+
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ struct gateway_data *data = value;
+
+ if (data->ipv4_gateway &&
+ data->ipv4_gateway == config)
+ return data;
+
+ if (data->ipv6_gateway &&
+ data->ipv6_gateway == config)
+ return data;
+ }
+
+ return NULL;
+}
+
+static struct gateway_data *find_vpn_gateway(int index, const char *gateway)
+{
+ GHashTableIter iter;
+ gpointer value, key;
+
+ if (!gateway)
+ return NULL;
+
+ g_hash_table_iter_init(&iter, gateway_hash);
+
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ struct gateway_data *data = value;
+
+ if (data->ipv4_gateway && data->index == index &&
+ g_str_equal(data->ipv4_gateway->gateway,
+ gateway))
+ return data;
+
+ if (data->ipv6_gateway && data->index == index &&
+ g_str_equal(data->ipv6_gateway->gateway,
+ gateway))
+ return data;
+ }
+
+ return NULL;
+}
+
+struct get_gateway_params {
+ char *vpn_gateway;
+ int vpn_index;
+};
+
+static void get_gateway_cb(const char *gateway, int index, void *user_data)
+{
+ struct gateway_config *config;
+ struct gateway_data *data;
+ struct get_gateway_params *params = user_data;
+ int family;
+
+ if (index < 0)
+ goto out;
+
+ DBG("phy index %d phy gw %s vpn index %d vpn gw %s", index, gateway,
+ params->vpn_index, params->vpn_gateway);
+
+ data = find_vpn_gateway(params->vpn_index, params->vpn_gateway);
+ if (!data) {
+ DBG("Cannot find VPN link route, index %d addr %s",
+ params->vpn_index, params->vpn_gateway);
+ goto out;
+ }
+
+ family = connman_inet_check_ipaddress(params->vpn_gateway);
+
+ if (family == AF_INET)
+ config = data->ipv4_gateway;
+ else if (family == AF_INET6)
+ config = data->ipv6_gateway;
+ else
+ goto out;
+
+ config->vpn_phy_index = index;
+
+ DBG("vpn %s phy index %d", config->vpn_ip, config->vpn_phy_index);
+
+out:
+ g_free(params->vpn_gateway);
+ g_free(params);
+}
+
+static void set_vpn_routes(struct gateway_data *new_gateway,
+ struct connman_service *service,
+ const char *gateway,
+ enum connman_ipconfig_type type,
+ const char *peer,
+ struct gateway_data *active_gateway)
+{
+ struct gateway_config *config;
+ struct connman_ipconfig *ipconfig;
+ char *dest;
+
+ DBG("new %p service %p gw %s type %d peer %s active %p",
+ new_gateway, service, gateway, type, peer, active_gateway);
+
+ if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
+ ipconfig = __connman_service_get_ip4config(service);
+ config = new_gateway->ipv4_gateway;
+ } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
+ ipconfig = __connman_service_get_ip6config(service);
+ config = new_gateway->ipv6_gateway;
+ } else
+ return;
+
+ if (config) {
+ int index = __connman_ipconfig_get_index(ipconfig);
+ struct get_gateway_params *params;
+
+ config->vpn = true;
+ if (peer)
+ config->vpn_ip = g_strdup(peer);
+ else if (gateway)
+ config->vpn_ip = g_strdup(gateway);
+
+ params = g_try_malloc(sizeof(struct get_gateway_params));
+ if (!params)
+ return;
+
+ params->vpn_index = index;
+ params->vpn_gateway = g_strdup(gateway);
+
+ /*
+ * Find the gateway that is serving the VPN link
+ */
+ __connman_inet_get_route(gateway, get_gateway_cb, params);
+ }
+
+ if (!active_gateway)
+ return;
+
+ if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
+ /*
+ * 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.
+ */
+ if (!active_gateway->ipv4_gateway)
+ return;
+
+ DBG("active gw %s", active_gateway->ipv4_gateway->gateway);
+
+ if (g_strcmp0(active_gateway->ipv4_gateway->gateway,
+ "0.0.0.0") != 0)
+ dest = active_gateway->ipv4_gateway->gateway;
+ else
+ dest = NULL;
+
+ connman_inet_add_host_route(active_gateway->index, gateway,
+ dest);
+
+ } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
+
+ if (!active_gateway->ipv6_gateway)
+ return;
+
+ DBG("active gw %s", active_gateway->ipv6_gateway->gateway);
+
+ if (g_strcmp0(active_gateway->ipv6_gateway->gateway,
+ "::") != 0)
+ dest = active_gateway->ipv6_gateway->gateway;
+ else
+ dest = NULL;
+
+ connman_inet_add_ipv6_host_route(active_gateway->index,
+ gateway, dest);
+ }
+}
+