Release tizen_2.0_beta
[framework/connectivity/connman.git] / src / ipconfig.c
index 7eea909..e26f209 100644 (file)
@@ -23,6 +23,7 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
 #include <stdio.h>
 #include <net/if.h>
 #include <net/if_arp.h>
@@ -39,7 +40,7 @@
 #include "connman.h"
 
 struct connman_ipconfig {
-       gint refcount;
+       int refcount;
        int index;
        enum connman_ipconfig_type type;
 
@@ -53,6 +54,7 @@ struct connman_ipconfig {
        struct connman_ipaddress *system;
 
        int ipv6_privacy_config;
+       char *last_dhcp_address;
 };
 
 struct connman_ipdevice {
@@ -145,6 +147,9 @@ static gboolean check_ipv6_address(const char *address)
        unsigned char buf[sizeof(struct in6_addr)];
        int err;
 
+       if (address == NULL)
+               return FALSE;
+
        err = inet_pton(AF_INET6, address, buf);
        if (err > 0)
                return TRUE;
@@ -153,8 +158,9 @@ static gboolean check_ipv6_address(const char *address)
 }
 
 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
-                               const char *address, const char *gateway,
-                                               unsigned char prefix_length)
+                               const char *address,
+                               unsigned char prefix_length,
+                               const char *gateway)
 {
        if (ipaddress == NULL)
                return -EINVAL;
@@ -283,6 +289,20 @@ static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevic
        return NULL;
 }
 
+const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
+{
+       switch (type) {
+       case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
+               return "unknown";
+       case CONNMAN_IPCONFIG_TYPE_IPV4:
+               return "IPv4";
+       case CONNMAN_IPCONFIG_TYPE_IPV6:
+               return "IPv6";
+       }
+
+       return NULL;
+}
+
 static const char *type2str(unsigned short type)
 {
        switch (type) {
@@ -432,6 +452,60 @@ static void set_ipv6_privacy(gchar *ifname, int value)
        fclose(f);
 }
 
+static int get_rp_filter()
+{
+       FILE *f;
+       int value = -EINVAL, tmp;
+
+       f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
+
+       if (f != NULL) {
+               if (fscanf(f, "%d", &tmp) == 1)
+                       value = tmp;
+               fclose(f);
+       }
+
+       return value;
+}
+
+static void set_rp_filter(int value)
+{
+       FILE *f;
+
+       f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
+
+       if (f == NULL)
+               return;
+
+       fprintf(f, "%d", value);
+
+       fclose(f);
+}
+
+int __connman_ipconfig_set_rp_filter()
+{
+       int value;
+
+       value = get_rp_filter();
+
+       if (value < 0)
+               return value;
+
+       set_rp_filter(2);
+
+       connman_info("rp_filter set to 2 (loose mode routing), "
+                       "old value was %d", value);
+
+       return value;
+}
+
+void __connman_ipconfig_unset_rp_filter(int old_value)
+{
+       set_rp_filter(old_value);
+
+       connman_info("rp_filter restored to %d", old_value);
+}
+
 static void free_ipdevice(gpointer data)
 {
        struct connman_ipdevice *ipdevice = data;
@@ -631,6 +705,13 @@ update:
                        ipconfig->ops->down(ipconfig);
        }
 
+#if defined TIZEN_EXT
+       if (g_strcmp0(ipdevice->address, address) != 0) {
+               g_free(ipdevice->address);
+               ipdevice->address = g_strdup(address);
+       }
+#endif
+
        if (lower_up)
                __connman_ipconfig_lower_up(ipdevice);
        if (lower_down)
@@ -689,6 +770,7 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label,
 {
        struct connman_ipdevice *ipdevice;
        struct connman_ipaddress *ipaddress;
+       enum connman_ipconfig_type type;
        GList *list;
 
        DBG("index %d", index);
@@ -710,6 +792,13 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label,
                return;
        }
 
+       if (family == AF_INET)
+               type = CONNMAN_IPCONFIG_TYPE_IPV4;
+       else if (family == AF_INET6)
+               type = CONNMAN_IPCONFIG_TYPE_IPV6;
+       else
+               return;
+
        ipdevice->address_list = g_slist_append(ipdevice->address_list,
                                                                ipaddress);
 
@@ -736,6 +825,9 @@ void __connman_ipconfig_newaddr(int index, int family, const char *label,
                if (index != ipconfig->index)
                        continue;
 
+               if (type != ipconfig->type)
+                       continue;
+
                if (ipconfig->ops == NULL)
                        continue;
 
@@ -749,6 +841,7 @@ void __connman_ipconfig_deladdr(int index, int family, const char *label,
 {
        struct connman_ipdevice *ipdevice;
        struct connman_ipaddress *ipaddress;
+       enum connman_ipconfig_type type;
        GList *list;
 
        DBG("index %d", index);
@@ -761,6 +854,13 @@ void __connman_ipconfig_deladdr(int index, int family, const char *label,
        if (ipaddress == NULL)
                return;
 
+       if (family == AF_INET)
+               type = CONNMAN_IPCONFIG_TYPE_IPV4;
+       else if (family == AF_INET6)
+               type = CONNMAN_IPCONFIG_TYPE_IPV6;
+       else
+               return;
+
        ipdevice->address_list = g_slist_remove(ipdevice->address_list,
                                                                ipaddress);
 
@@ -783,6 +883,9 @@ void __connman_ipconfig_deladdr(int index, int family, const char *label,
                if (index != ipconfig->index)
                        continue;
 
+               if (type != ipconfig->type)
+                       continue;
+
                if (ipconfig->ops == NULL)
                        continue;
 
@@ -802,11 +905,14 @@ void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
        if (ipdevice == NULL)
                return;
 
-       if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
+       if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
+                                               g_strcmp0(dst, "::") == 0)) {
                GSList *list;
                GList *config_list;
+               enum connman_ipconfig_type type;
 
                if (family == AF_INET6) {
+                       type = CONNMAN_IPCONFIG_TYPE_IPV6;
                        g_free(ipdevice->ipv6_gateway);
                        ipdevice->ipv6_gateway = g_strdup(gateway);
 
@@ -816,7 +922,8 @@ void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
                                ipdevice->config_ipv6->system->gateway =
                                        g_strdup(gateway);
                        }
-               } else {
+               } else if (family == AF_INET) {
+                       type = CONNMAN_IPCONFIG_TYPE_IPV4;
                        g_free(ipdevice->ipv4_gateway);
                        ipdevice->ipv4_gateway = g_strdup(gateway);
 
@@ -826,7 +933,8 @@ void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
                                ipdevice->config_ipv4->system->gateway =
                                        g_strdup(gateway);
                        }
-               }
+               } else
+                       return;
 
                for (list = ipdevice->address_list; list; list = list->next) {
                        struct connman_ipaddress *ipaddress = list->data;
@@ -842,6 +950,9 @@ void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
                        if (index != ipconfig->index)
                                continue;
 
+                       if (type != ipconfig->type)
+                               continue;
+
                        if (ipconfig->ops == NULL)
                                continue;
 
@@ -866,11 +977,14 @@ void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
        if (ipdevice == NULL)
                return;
 
-       if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
+       if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
+                                               g_strcmp0(dst, "::") == 0)) {
                GSList *list;
                GList *config_list;
+               enum connman_ipconfig_type type;
 
                if (family == AF_INET6) {
+                       type = CONNMAN_IPCONFIG_TYPE_IPV6;
                        g_free(ipdevice->ipv6_gateway);
                        ipdevice->ipv6_gateway = NULL;
 
@@ -879,7 +993,8 @@ void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
                                g_free(ipdevice->config_ipv6->system->gateway);
                                ipdevice->config_ipv6->system->gateway = NULL;
                        }
-               } else {
+               } else if (family == AF_INET) {
+                       type = CONNMAN_IPCONFIG_TYPE_IPV4;
                        g_free(ipdevice->ipv4_gateway);
                        ipdevice->ipv4_gateway = NULL;
 
@@ -888,7 +1003,8 @@ void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
                                g_free(ipdevice->config_ipv4->system->gateway);
                                ipdevice->config_ipv4->system->gateway = NULL;
                        }
-               }
+               } else
+                       return;
 
                for (list = ipdevice->address_list; list; list = list->next) {
                        struct connman_ipaddress *ipaddress = list->data;
@@ -904,6 +1020,9 @@ void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
                        if (index != ipconfig->index)
                                continue;
 
+                       if (type != ipconfig->type)
+                               continue;
+
                        if (ipconfig->ops == NULL)
                                continue;
 
@@ -1062,30 +1181,41 @@ void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const cha
        ipconfig->address->gateway = g_strdup(gateway);
 }
 
+#if defined TIZEN_EXT
+/*
+ * Description: __connman_service_lookup_from_index cannot find correct service
+ */
+int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
+#else
 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
+#endif
 {
+#if !defined TIZEN_EXT
        struct connman_service *service;
+#endif
 
        DBG("");
 
        if (ipconfig->address == NULL)
                return -EINVAL;
 
+#if !defined TIZEN_EXT
        service = __connman_service_lookup_from_index(ipconfig->index);
+#endif
        if (service == NULL)
                return -EINVAL;
 
-       __connman_connection_gateway_remove(service);
+       __connman_connection_gateway_remove(service, ipconfig->type);
 
-       if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
-               return __connman_connection_gateway_add(service, NULL,
-                                               ipconfig->address->gateway,
-                                                       ipconfig->address->peer);
-       } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
+       DBG("type %d gw %s peer %s", ipconfig->type,
+               ipconfig->address->gateway, ipconfig->address->peer);
+
+       if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
+                               ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
                return __connman_connection_gateway_add(service,
                                                ipconfig->address->gateway,
-                                                       NULL, ipconfig->address->peer);
-       }
+                                               ipconfig->type,
+                                               ipconfig->address->peer);
 
        return 0;
 }
@@ -1098,7 +1228,7 @@ void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
 
        service = __connman_service_lookup_from_index(ipconfig->index);
        if (service != NULL)
-               __connman_connection_gateway_remove(service);
+               __connman_connection_gateway_remove(service, ipconfig->type);
 }
 
 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
@@ -1195,10 +1325,9 @@ struct connman_ipconfig *connman_ipconfig_create(int index,
  */
 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
 {
-       DBG("ipconfig %p refcount %d", ipconfig,
-                               g_atomic_int_get(&ipconfig->refcount) + 1);
+       DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
 
-       g_atomic_int_inc(&ipconfig->refcount);
+       __sync_fetch_and_add(&ipconfig->refcount, 1);
 
        return ipconfig;
 }
@@ -1214,23 +1343,25 @@ void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
        if (ipconfig == NULL)
                return;
 
-       DBG("ipconfig %p refcount %d", ipconfig,
-                       g_atomic_int_get(&ipconfig->refcount) - 1);
+       DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
 
-       if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
-               __connman_ipconfig_disable(ipconfig);
+       if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
+               return;
 
-               connman_ipconfig_set_ops(ipconfig, NULL);
+       if (__connman_ipconfig_disable(ipconfig) < 0)
+               ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
 
-               if (ipconfig->origin != NULL) {
-                       connman_ipconfig_unref(ipconfig->origin);
-                       ipconfig->origin = NULL;
-               }
+       connman_ipconfig_set_ops(ipconfig, NULL);
 
-               connman_ipaddress_free(ipconfig->system);
-               connman_ipaddress_free(ipconfig->address);
-               g_free(ipconfig);
+       if (ipconfig->origin != NULL) {
+               connman_ipconfig_unref(ipconfig->origin);
+               ipconfig->origin = NULL;
        }
+
+       connman_ipaddress_free(ipconfig->system);
+       connman_ipaddress_free(ipconfig->address);
+       g_free(ipconfig->last_dhcp_address);
+       g_free(ipconfig);
 }
 
 /**
@@ -1361,6 +1492,36 @@ int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
 
 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
 {
+       int err;
+
+       DBG("");
+
+       if (ipconfig == NULL)
+               return 0;
+
+       DBG("method %d", ipconfig->method);
+
+       switch (ipconfig->method) {
+       case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
+       case CONNMAN_IPCONFIG_METHOD_OFF:
+       case CONNMAN_IPCONFIG_METHOD_AUTO:
+               break;
+       case CONNMAN_IPCONFIG_METHOD_FIXED:
+       case CONNMAN_IPCONFIG_METHOD_DHCP:
+       case CONNMAN_IPCONFIG_METHOD_MANUAL:
+               err = __connman_ipconfig_address_unset(ipconfig);
+               connman_ipaddress_clear(ipconfig->address);
+
+               return err;
+       }
+
+       return 0;
+}
+
+int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
+{
+       int err;
+
        DBG("");
 
        if (ipconfig == NULL)
@@ -1377,13 +1538,17 @@ int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
        case CONNMAN_IPCONFIG_METHOD_DHCP:
        case CONNMAN_IPCONFIG_METHOD_MANUAL:
                if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
-                       return connman_inet_clear_address(ipconfig->index,
+                       err = connman_inet_clear_address(ipconfig->index,
                                                        ipconfig->address);
                else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
-                       return connman_inet_clear_ipv6_address(
+                       err = connman_inet_clear_ipv6_address(
                                                ipconfig->index,
                                                ipconfig->address->local,
                                                ipconfig->address->prefixlen);
+               else
+                       err = -EINVAL;
+
+               return err;
        }
 
        return 0;
@@ -1427,6 +1592,24 @@ const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipc
        return ipdevice->pac;
 }
 
+void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
+                                       const char *address)
+{
+       if (ipconfig == NULL)
+               return;
+
+       g_free(ipconfig->last_dhcp_address);
+       ipconfig->last_dhcp_address = g_strdup(address);
+}
+
+char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
+{
+       if (ipconfig == NULL)
+               return NULL;
+
+       return ipconfig->last_dhcp_address;
+}
+
 static void disable_ipv6(struct connman_ipconfig *ipconfig)
 {
        struct connman_ipdevice *ipdevice;
@@ -1459,6 +1642,14 @@ static void enable_ipv6(struct connman_ipconfig *ipconfig)
        set_ipv6_state(ipdevice->ifname, TRUE);
 }
 
+void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
+{
+       if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
+               return;
+
+       enable_ipv6(ipconfig);
+}
+
 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
 {
        if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
@@ -1689,7 +1880,8 @@ void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
 }
 
 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
-                                                       DBusMessageIter *iter)
+                                       DBusMessageIter *iter,
+                                       struct connman_ipconfig *ipconfig_ipv4)
 {
        const char *str, *privacy;
 
@@ -1702,6 +1894,12 @@ void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
        if (str == NULL)
                return;
 
+       if (ipconfig_ipv4 != NULL &&
+                       ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
+               if (__connman_6to4_check(ipconfig_ipv4) == 1)
+                       str = "6to4";
+       }
+
        connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
 
        if (ipconfig->system == NULL)
@@ -1784,10 +1982,10 @@ void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
        switch (ipconfig->method) {
        case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
        case CONNMAN_IPCONFIG_METHOD_OFF:
-       case CONNMAN_IPCONFIG_METHOD_FIXED:
        case CONNMAN_IPCONFIG_METHOD_DHCP:
        case CONNMAN_IPCONFIG_METHOD_AUTO:
                return;
+       case CONNMAN_IPCONFIG_METHOD_FIXED:
        case CONNMAN_IPCONFIG_METHOD_MANUAL:
                break;
        }
@@ -1928,7 +2126,7 @@ int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
                else
                        return connman_ipaddress_set_ipv6(
                                        ipconfig->address, address,
-                                               gateway, prefix_length);
+                                               prefix_length, gateway);
                break;
 
        case CONNMAN_IPCONFIG_METHOD_DHCP:
@@ -1974,6 +2172,7 @@ int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
 {
        char *method;
        char *key;
+       char *str;
 
        DBG("ipconfig %p identifier %s", ipconfig, identifier);
 
@@ -2034,6 +2233,14 @@ int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
                                keyfile, identifier, key, NULL);
        g_free(key);
 
+       key = g_strdup_printf("%sDHCP.LastAddress", prefix);
+       str = g_key_file_get_string(keyfile, identifier, key, NULL);
+       if (str != NULL) {
+               g_free(ipconfig->last_dhcp_address);
+               ipconfig->last_dhcp_address = str;
+       }
+       g_free(key);
+
        return 0;
 }
 
@@ -2063,9 +2270,18 @@ int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
        case CONNMAN_IPCONFIG_METHOD_FIXED:
        case CONNMAN_IPCONFIG_METHOD_MANUAL:
                break;
+       case CONNMAN_IPCONFIG_METHOD_DHCP:
+               key = g_strdup_printf("%sDHCP.LastAddress", prefix);
+               if (ipconfig->last_dhcp_address != NULL &&
+                               strlen(ipconfig->last_dhcp_address) > 0)
+                       g_key_file_set_string(keyfile, identifier, key,
+                                       ipconfig->last_dhcp_address);
+               else
+                       g_key_file_remove_key(keyfile, identifier, key, NULL);
+               g_free(key);
+               /* fall through */
        case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
        case CONNMAN_IPCONFIG_METHOD_OFF:
-       case CONNMAN_IPCONFIG_METHOD_DHCP:
        case CONNMAN_IPCONFIG_METHOD_AUTO:
                return 0;
        }