5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <net/if_arp.h>
30 #include <linux/if_link.h>
35 #define IFF_LOWER_UP 0x10000
39 #include <connman/ipaddress.h>
43 struct connman_ipconfig {
46 enum connman_ipconfig_type type;
48 const struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
52 struct connman_ipaddress *address;
53 struct connman_ipaddress *system;
56 int dhcp_lease_duration;
59 int ipv6_privacy_config;
60 char *last_dhcp_address;
61 char **last_dhcpv6_prefixes;
64 struct connman_ipdevice {
85 struct connman_ipconfig *config_ipv4;
86 struct connman_ipconfig *config_ipv6;
92 struct ipconfig_store {
98 static GHashTable *ipdevice_hash = NULL;
99 static GList *ipconfig_list = NULL;
100 static bool is_ipv6_supported = false;
102 static void store_set_str(struct ipconfig_store *store,
103 const char *key, const char *val)
108 if (!val || strlen(val) == 0)
111 pk = g_strdup_printf("%s%s", store->prefix, key);
112 g_key_file_set_string(store->file, store->group, pk, val);
116 static char *store_get_str(struct ipconfig_store *store, const char *key)
120 pk = g_strdup_printf("%s%s", store->prefix, key);
121 val = g_key_file_get_string(store->file, store->group, pk, NULL);
127 static void store_set_strs(struct ipconfig_store *store,
128 const char *key, char **val)
136 len = g_strv_length(val);
140 pk = g_strdup_printf("%s%s", store->prefix, key);
141 g_key_file_set_string_list(store->file, store->group,
142 pk, (const gchar **)val, len);
146 static char **store_get_strs(struct ipconfig_store *store, const char *key)
151 pk = g_strdup_printf("%s%s", store->prefix, key);
152 val = g_key_file_get_string_list(store->file, store->group,
156 if (val && len == 0) {
164 static void store_set_int(struct ipconfig_store *store,
165 const char *key, int val)
172 pk = g_strdup_printf("%s%s", store->prefix, key);
173 g_key_file_set_integer(store->file, store->group, pk, val);
177 static int store_get_int(struct ipconfig_store *store, const char *key)
182 pk = g_strdup_printf("%s%s", store->prefix, key);
183 val = g_key_file_get_integer(store->file, store->group, pk, 0);
189 static void free_address_list(struct connman_ipdevice *ipdevice)
193 for (list = ipdevice->address_list; list; list = list->next) {
194 struct connman_ipaddress *ipaddress = list->data;
196 connman_ipaddress_free(ipaddress);
200 g_slist_free(ipdevice->address_list);
201 ipdevice->address_list = NULL;
204 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
205 unsigned char prefixlen, const char *local)
209 for (list = ipdevice->address_list; list; list = list->next) {
210 struct connman_ipaddress *ipaddress = list->data;
212 if (g_strcmp0(ipaddress->local, local) == 0 &&
213 ipaddress->prefixlen == prefixlen)
220 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
223 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
224 case CONNMAN_IPCONFIG_TYPE_ALL:
226 case CONNMAN_IPCONFIG_TYPE_IPV4:
228 case CONNMAN_IPCONFIG_TYPE_IPV6:
235 static const char *type2str(unsigned short type)
240 case ARPHRD_LOOPBACK:
253 static const char *scope2str(unsigned char scope)
265 static bool get_ipv6_state(gchar *ifname)
270 bool enabled = false;
273 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
275 path = g_strdup_printf(
276 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
281 f = fopen(path, "r");
286 if (fscanf(f, "%d", &disabled) > 0)
294 static void set_ipv6_state(gchar *ifname, bool enable)
300 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
302 path = g_strdup_printf(
303 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
308 f = fopen(path, "r+");
323 static int get_ipv6_privacy(gchar *ifname)
332 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
338 f = fopen(path, "r");
345 if (fscanf(f, "%d", &value) <= 0)
353 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
354 * The privacy extension is described in RFC 3041 and RFC 4941
356 static void set_ipv6_privacy(gchar *ifname, int value)
364 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
373 f = fopen(path, "r+");
380 fprintf(f, "%d", value);
384 static int get_rp_filter(void)
387 int value = -EINVAL, tmp;
389 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
392 if (fscanf(f, "%d", &tmp) == 1)
400 static void set_rp_filter(int value)
404 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
409 fprintf(f, "%d", value);
414 int __connman_ipconfig_set_rp_filter()
418 value = get_rp_filter();
425 connman_info("rp_filter set to 2 (loose mode routing), "
426 "old value was %d", value);
431 void __connman_ipconfig_unset_rp_filter(int old_value)
433 set_rp_filter(old_value);
435 connman_info("rp_filter restored to %d", old_value);
438 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
443 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
446 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
448 struct connman_ipdevice *ipdevice;
455 ipdevice = g_hash_table_lookup(ipdevice_hash,
456 GINT_TO_POINTER(ipconfig->index));
460 ifname = connman_inet_ifname(ipconfig->index);
461 ret = get_ipv6_state(ifname);
467 static void free_ipdevice(gpointer data)
469 struct connman_ipdevice *ipdevice = data;
470 char *ifname = connman_inet_ifname(ipdevice->index);
472 connman_info("%s {remove} index %d", ifname, ipdevice->index);
474 if (ipdevice->config_ipv4) {
475 __connman_ipconfig_unref(ipdevice->config_ipv4);
476 ipdevice->config_ipv4 = NULL;
479 if (ipdevice->config_ipv6) {
480 __connman_ipconfig_unref(ipdevice->config_ipv6);
481 ipdevice->config_ipv6 = NULL;
484 free_address_list(ipdevice);
485 g_free(ipdevice->ipv4_gateway);
486 g_free(ipdevice->ipv6_gateway);
487 g_free(ipdevice->pac);
489 g_free(ipdevice->address);
492 set_ipv6_state(ifname, ipdevice->ipv6_enabled);
493 set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
500 static void update_stats(struct connman_ipdevice *ipdevice,
501 const char *ifname, struct rtnl_link_stats *stats)
503 struct connman_service *service;
505 if (stats->rx_packets == 0 && stats->tx_packets == 0)
508 connman_info("%s {RX} %u packets %u bytes", ifname,
509 stats->rx_packets, stats->rx_bytes);
510 connman_info("%s {TX} %u packets %u bytes", ifname,
511 stats->tx_packets, stats->tx_bytes);
513 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
516 service = __connman_service_lookup_from_index(ipdevice->index);
518 DBG("service %p", service);
523 ipdevice->rx_packets = stats->rx_packets;
524 ipdevice->tx_packets = stats->tx_packets;
525 ipdevice->rx_bytes = stats->rx_bytes;
526 ipdevice->tx_bytes = stats->tx_bytes;
527 ipdevice->rx_errors = stats->rx_errors;
528 ipdevice->tx_errors = stats->tx_errors;
529 ipdevice->rx_dropped = stats->rx_dropped;
530 ipdevice->tx_dropped = stats->tx_dropped;
532 __connman_service_notify(service,
533 ipdevice->rx_packets, ipdevice->tx_packets,
534 ipdevice->rx_bytes, ipdevice->tx_bytes,
535 ipdevice->rx_errors, ipdevice->tx_errors,
536 ipdevice->rx_dropped, ipdevice->tx_dropped);
539 void __connman_ipconfig_newlink(int index, unsigned short type,
540 unsigned int flags, const char *address,
542 struct rtnl_link_stats *stats)
544 struct connman_ipdevice *ipdevice;
545 GList *list, *ipconfig_copy;
547 bool up = false, down = false;
548 bool lower_up = false, lower_down = false;
551 DBG("index %d", index);
553 if (type == ARPHRD_LOOPBACK)
556 ifname = connman_inet_ifname(index);
558 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
562 ipdevice = g_try_new0(struct connman_ipdevice, 1);
566 ipdevice->index = index;
567 ipdevice->type = type;
569 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
570 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
572 ipdevice->address = g_strdup(address);
574 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
576 connman_info("%s {create} index %d type %d <%s>", ifname,
577 index, type, type2str(type));
580 #if defined TIZEN_EXT
581 if (g_strcmp0(ipdevice->address, address) != 0) {
582 /* If an original address is built-in physical device,
583 * it's hardly get an address at a initial creation
585 g_free(ipdevice->address);
586 ipdevice->address = g_strdup(address);
592 update_stats(ipdevice, ifname, stats);
594 if (flags == ipdevice->flags)
597 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
604 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
605 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
606 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
607 (IFF_RUNNING | IFF_LOWER_UP))
609 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
613 ipdevice->flags = flags;
615 str = g_string_new(NULL);
620 g_string_append(str, "UP");
622 g_string_append(str, "DOWN");
624 if (flags & IFF_RUNNING)
625 g_string_append(str, ",RUNNING");
627 if (flags & IFF_LOWER_UP)
628 g_string_append(str, ",LOWER_UP");
630 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
632 g_string_free(str, TRUE);
634 ipconfig_copy = g_list_copy(ipconfig_list);
636 for (list = g_list_first(ipconfig_copy); list;
637 list = g_list_next(list)) {
638 struct connman_ipconfig *ipconfig = list->data;
640 if (index != ipconfig->index)
646 if (up && ipconfig->ops->up)
647 ipconfig->ops->up(ipconfig, ifname);
648 if (lower_up && ipconfig->ops->lower_up)
649 ipconfig->ops->lower_up(ipconfig, ifname);
651 if (lower_down && ipconfig->ops->lower_down)
652 ipconfig->ops->lower_down(ipconfig, ifname);
653 if (down && ipconfig->ops->down)
654 ipconfig->ops->down(ipconfig, ifname);
657 g_list_free(ipconfig_copy);
663 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
665 struct connman_ipdevice *ipdevice;
669 DBG("index %d", index);
671 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
675 ifname = connman_inet_ifname(index);
677 update_stats(ipdevice, ifname, stats);
679 for (list = g_list_first(ipconfig_list); list;
680 list = g_list_next(list)) {
681 struct connman_ipconfig *ipconfig = list->data;
683 if (index != ipconfig->index)
686 ipconfig->index = -1;
691 if (ipconfig->ops->lower_down)
692 ipconfig->ops->lower_down(ipconfig, ifname);
693 if (ipconfig->ops->down)
694 ipconfig->ops->down(ipconfig, ifname);
699 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
702 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
704 const struct connman_ipaddress *addr1 = a;
705 const struct connman_ipaddress *addr2 = b;
707 if (addr1->prefixlen != addr2->prefixlen)
708 return addr2->prefixlen - addr1->prefixlen;
710 return g_strcmp0(addr1->local, addr2->local);
713 int __connman_ipconfig_newaddr(int index, int family, const char *label,
714 unsigned char prefixlen, const char *address)
716 struct connman_ipdevice *ipdevice;
717 struct connman_ipaddress *ipaddress;
718 enum connman_ipconfig_type type;
722 DBG("index %d", index);
724 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
728 ipaddress = connman_ipaddress_alloc(family);
732 ipaddress->prefixlen = prefixlen;
733 ipaddress->local = g_strdup(address);
735 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
736 check_duplicate_address)) {
737 connman_ipaddress_free(ipaddress);
741 if (family == AF_INET)
742 type = CONNMAN_IPCONFIG_TYPE_IPV4;
743 else if (family == AF_INET6)
744 type = CONNMAN_IPCONFIG_TYPE_IPV6;
748 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
751 ifname = connman_inet_ifname(index);
752 connman_info("%s {add} address %s/%u label %s family %d",
753 ifname, address, prefixlen, label, family);
755 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
756 __connman_ippool_newaddr(index, address, prefixlen);
758 if (ipdevice->config_ipv4 && family == AF_INET)
759 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
762 else if (ipdevice->config_ipv6 && family == AF_INET6)
763 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
768 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
771 for (list = g_list_first(ipconfig_list); list;
772 list = g_list_next(list)) {
773 struct connman_ipconfig *ipconfig = list->data;
775 if (index != ipconfig->index)
778 if (type != ipconfig->type)
784 if (ipconfig->ops->ip_bound)
785 ipconfig->ops->ip_bound(ipconfig, ifname);
793 void __connman_ipconfig_deladdr(int index, int family, const char *label,
794 unsigned char prefixlen, const char *address)
796 struct connman_ipdevice *ipdevice;
797 struct connman_ipaddress *ipaddress;
798 enum connman_ipconfig_type type;
802 DBG("index %d", index);
804 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
808 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
812 if (family == AF_INET)
813 type = CONNMAN_IPCONFIG_TYPE_IPV4;
814 else if (family == AF_INET6)
815 type = CONNMAN_IPCONFIG_TYPE_IPV6;
819 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
822 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
823 __connman_ippool_deladdr(index, address, prefixlen);
825 connman_ipaddress_clear(ipaddress);
828 ifname = connman_inet_ifname(index);
829 connman_info("%s {del} address %s/%u label %s", ifname,
830 address, prefixlen, label);
832 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
835 if (g_slist_length(ipdevice->address_list) > 0)
838 for (list = g_list_first(ipconfig_list); list;
839 list = g_list_next(list)) {
840 struct connman_ipconfig *ipconfig = list->data;
842 if (index != ipconfig->index)
845 if (type != ipconfig->type)
851 if (ipconfig->ops->ip_release)
852 ipconfig->ops->ip_release(ipconfig, ifname);
859 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
860 const char *dst, const char *gateway)
862 struct connman_ipdevice *ipdevice;
865 DBG("index %d", index);
867 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
871 ifname = connman_inet_ifname(index);
873 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
874 g_strcmp0(dst, "::") == 0)) {
876 enum connman_ipconfig_type type;
878 if (family == AF_INET6) {
879 type = CONNMAN_IPCONFIG_TYPE_IPV6;
880 g_free(ipdevice->ipv6_gateway);
881 ipdevice->ipv6_gateway = g_strdup(gateway);
883 if (ipdevice->config_ipv6 &&
884 ipdevice->config_ipv6->system) {
885 g_free(ipdevice->config_ipv6->system->gateway);
886 ipdevice->config_ipv6->system->gateway =
889 } else if (family == AF_INET) {
890 type = CONNMAN_IPCONFIG_TYPE_IPV4;
891 g_free(ipdevice->ipv4_gateway);
892 ipdevice->ipv4_gateway = g_strdup(gateway);
894 if (ipdevice->config_ipv4 &&
895 ipdevice->config_ipv4->system) {
896 g_free(ipdevice->config_ipv4->system->gateway);
897 ipdevice->config_ipv4->system->gateway =
903 for (config_list = g_list_first(ipconfig_list); config_list;
904 config_list = g_list_next(config_list)) {
905 struct connman_ipconfig *ipconfig = config_list->data;
907 if (index != ipconfig->index)
910 if (type != ipconfig->type)
916 if (ipconfig->ops->route_set)
917 ipconfig->ops->route_set(ipconfig, ifname);
921 connman_info("%s {add} route %s gw %s scope %u <%s>",
922 ifname, dst, gateway, scope, scope2str(scope));
928 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
929 const char *dst, const char *gateway)
931 struct connman_ipdevice *ipdevice;
934 DBG("index %d", index);
936 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
940 ifname = connman_inet_ifname(index);
942 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
943 g_strcmp0(dst, "::") == 0)) {
945 enum connman_ipconfig_type type;
947 if (family == AF_INET6) {
948 type = CONNMAN_IPCONFIG_TYPE_IPV6;
949 g_free(ipdevice->ipv6_gateway);
950 ipdevice->ipv6_gateway = NULL;
952 if (ipdevice->config_ipv6 &&
953 ipdevice->config_ipv6->system) {
954 g_free(ipdevice->config_ipv6->system->gateway);
955 ipdevice->config_ipv6->system->gateway = NULL;
957 } else if (family == AF_INET) {
958 type = CONNMAN_IPCONFIG_TYPE_IPV4;
959 g_free(ipdevice->ipv4_gateway);
960 ipdevice->ipv4_gateway = NULL;
962 if (ipdevice->config_ipv4 &&
963 ipdevice->config_ipv4->system) {
964 g_free(ipdevice->config_ipv4->system->gateway);
965 ipdevice->config_ipv4->system->gateway = NULL;
970 for (config_list = g_list_first(ipconfig_list); config_list;
971 config_list = g_list_next(config_list)) {
972 struct connman_ipconfig *ipconfig = config_list->data;
974 if (index != ipconfig->index)
977 if (type != ipconfig->type)
983 if (ipconfig->ops->route_unset)
984 ipconfig->ops->route_unset(ipconfig, ifname);
988 connman_info("%s {del} route %s gw %s scope %u <%s>",
989 ifname, dst, gateway, scope, scope2str(scope));
995 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1000 keys = g_hash_table_get_keys(ipdevice_hash);
1004 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1005 int index = GPOINTER_TO_INT(list->data);
1007 function(index, user_data);
1013 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1014 struct connman_ipconfig *ipconfig)
1016 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1019 unsigned short __connman_ipconfig_get_type_from_index(int index)
1021 struct connman_ipdevice *ipdevice;
1023 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1027 return ipdevice->type;
1030 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1032 struct connman_ipdevice *ipdevice;
1034 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1038 return ipdevice->flags;
1041 const char *__connman_ipconfig_get_gateway_from_index(int index,
1042 enum connman_ipconfig_type type)
1044 struct connman_ipdevice *ipdevice;
1046 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1050 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1051 if (ipdevice->ipv4_gateway)
1052 return ipdevice->ipv4_gateway;
1054 if (ipdevice->config_ipv4 &&
1055 ipdevice->config_ipv4->address)
1056 return ipdevice->config_ipv4->address->gateway;
1059 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1060 if (ipdevice->ipv6_gateway)
1061 return ipdevice->ipv6_gateway;
1063 if (ipdevice->config_ipv6 &&
1064 ipdevice->config_ipv6->address)
1065 return ipdevice->config_ipv6->address->gateway;
1071 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1073 ipconfig->index = index;
1076 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1078 if (!ipconfig->address)
1081 return ipconfig->address->local;
1084 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1085 const char *address)
1087 if (!ipconfig->address)
1090 g_free(ipconfig->address->local);
1091 ipconfig->address->local = g_strdup(address);
1094 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1096 if (!ipconfig->address)
1099 return ipconfig->address->peer;
1102 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1103 const char *address)
1105 if (!ipconfig->address)
1108 g_free(ipconfig->address->peer);
1109 ipconfig->address->peer = g_strdup(address);
1112 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1114 if (!ipconfig->address)
1117 return ipconfig->address->broadcast;
1120 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1121 const char *broadcast)
1123 if (!ipconfig->address)
1126 g_free(ipconfig->address->broadcast);
1127 ipconfig->address->broadcast = g_strdup(broadcast);
1130 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1132 if (!ipconfig->address)
1135 return ipconfig->address->gateway;
1138 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1139 const char *gateway)
1143 if (!ipconfig->address)
1145 g_free(ipconfig->address->gateway);
1146 ipconfig->address->gateway = g_strdup(gateway);
1149 #if defined TIZEN_EXT
1150 void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig,
1151 int dhcp_lease_duration)
1153 ipconfig->dhcp_lease_duration = dhcp_lease_duration;
1157 #if defined TIZEN_EXT
1158 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1160 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1163 #if !defined TIZEN_EXT
1164 struct connman_service *service;
1169 if (!ipconfig->address)
1172 #if !defined TIZEN_EXT
1173 service = __connman_service_lookup_from_index(ipconfig->index);
1178 DBG("type %d gw %s peer %s", ipconfig->type,
1179 ipconfig->address->gateway, ipconfig->address->peer);
1181 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1182 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1183 return __connman_connection_gateway_add(service,
1184 ipconfig->address->gateway,
1186 ipconfig->address->peer);
1191 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1193 struct connman_service *service;
1197 service = __connman_service_lookup_from_index(ipconfig->index);
1199 __connman_connection_gateway_remove(service, ipconfig->type);
1202 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1204 if (!ipconfig->address)
1207 return ipconfig->address->prefixlen;
1210 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1211 unsigned char prefixlen)
1213 if (!ipconfig->address)
1216 ipconfig->address->prefixlen = prefixlen;
1219 static struct connman_ipconfig *create_ipv6config(int index)
1221 struct connman_ipconfig *ipv6config;
1222 struct connman_ipdevice *ipdevice;
1224 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1228 ipv6config->refcount = 1;
1230 ipv6config->index = index;
1231 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1233 if (!is_ipv6_supported)
1234 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1236 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1238 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1240 #if !defined TIZEN_EXT
1241 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1243 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy = 2;
1246 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1247 if (!ipv6config->address) {
1252 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1254 DBG("ipconfig %p index %d method %s", ipv6config, index,
1255 __connman_ipconfig_method2string(ipv6config->method));
1261 * connman_ipconfig_create:
1263 * Allocate a new ipconfig structure.
1265 * Returns: a newly-allocated #connman_ipconfig structure
1267 struct connman_ipconfig *__connman_ipconfig_create(int index,
1268 enum connman_ipconfig_type type)
1270 struct connman_ipconfig *ipconfig;
1272 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1273 return create_ipv6config(index);
1275 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1279 ipconfig->refcount = 1;
1281 ipconfig->index = index;
1282 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1284 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1285 if (!ipconfig->address) {
1290 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1291 #if defined TIZEN_EXT
1292 if (!simplified_log)
1294 DBG("ipconfig %p index %d", ipconfig, index);
1301 * connman_ipconfig_ref:
1302 * @ipconfig: ipconfig structure
1304 * Increase reference counter of ipconfig
1306 struct connman_ipconfig *
1307 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1308 const char *file, int line, const char *caller)
1310 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1311 file, line, caller);
1313 __sync_fetch_and_add(&ipconfig->refcount, 1);
1319 * connman_ipconfig_unref:
1320 * @ipconfig: ipconfig structure
1322 * Decrease reference counter of ipconfig
1324 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1325 const char *file, int line, const char *caller)
1329 #if defined TIZEN_EXT
1330 if (!simplified_log)
1332 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1333 file, line, caller);
1335 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1338 if (__connman_ipconfig_disable(ipconfig) < 0)
1339 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1341 __connman_ipconfig_set_ops(ipconfig, NULL);
1343 connman_ipaddress_free(ipconfig->system);
1344 connman_ipaddress_free(ipconfig->address);
1345 g_free(ipconfig->last_dhcp_address);
1346 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1351 * connman_ipconfig_get_data:
1352 * @ipconfig: ipconfig structure
1354 * Get private data pointer
1356 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1361 return ipconfig->ops_data;
1365 * connman_ipconfig_set_data:
1366 * @ipconfig: ipconfig structure
1367 * @data: data pointer
1369 * Set private data pointer
1371 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1373 ipconfig->ops_data = data;
1377 * connman_ipconfig_get_index:
1378 * @ipconfig: ipconfig structure
1380 * Get interface index
1382 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1387 return ipconfig->index;
1391 * connman_ipconfig_set_ops:
1392 * @ipconfig: ipconfig structure
1393 * @ops: operation callbacks
1395 * Set the operation callbacks
1397 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1398 const struct connman_ipconfig_ops *ops)
1400 ipconfig->ops = ops;
1404 * connman_ipconfig_set_method:
1405 * @ipconfig: ipconfig structure
1406 * @method: configuration method
1408 * Set the configuration method
1410 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1411 enum connman_ipconfig_method method)
1413 ipconfig->method = method;
1418 enum connman_ipconfig_method __connman_ipconfig_get_method(
1419 struct connman_ipconfig *ipconfig)
1422 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1424 return ipconfig->method;
1427 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1429 switch (ipconfig->method) {
1430 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1431 case CONNMAN_IPCONFIG_METHOD_OFF:
1433 case CONNMAN_IPCONFIG_METHOD_AUTO:
1434 case CONNMAN_IPCONFIG_METHOD_FIXED:
1435 case CONNMAN_IPCONFIG_METHOD_DHCP:
1436 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1437 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1438 return connman_inet_set_address(ipconfig->index,
1440 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1441 return connman_inet_set_ipv6_address(
1442 ipconfig->index, ipconfig->address);
1448 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1455 switch (ipconfig->method) {
1456 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1457 case CONNMAN_IPCONFIG_METHOD_OFF:
1459 case CONNMAN_IPCONFIG_METHOD_AUTO:
1460 case CONNMAN_IPCONFIG_METHOD_DHCP:
1461 err = __connman_ipconfig_address_unset(ipconfig);
1462 connman_ipaddress_clear(ipconfig->address);
1465 case CONNMAN_IPCONFIG_METHOD_FIXED:
1466 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1467 return __connman_ipconfig_address_unset(ipconfig);
1473 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1480 #if defined TIZEN_EXT
1481 DBG("ipconfig method %d type %d", ipconfig->method, ipconfig->type);
1483 DBG("method %d", ipconfig->method);
1486 switch (ipconfig->method) {
1487 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1488 case CONNMAN_IPCONFIG_METHOD_OFF:
1490 case CONNMAN_IPCONFIG_METHOD_AUTO:
1491 case CONNMAN_IPCONFIG_METHOD_FIXED:
1492 case CONNMAN_IPCONFIG_METHOD_DHCP:
1493 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1494 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1495 err = connman_inet_clear_address(ipconfig->index,
1497 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1498 err = connman_inet_clear_ipv6_address(
1500 ipconfig->address->local,
1501 ipconfig->address->prefixlen);
1511 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1514 struct connman_ipdevice *ipdevice;
1516 if (!ipconfig || ipconfig->index < 0)
1519 ipdevice = g_hash_table_lookup(ipdevice_hash,
1520 GINT_TO_POINTER(ipconfig->index));
1524 g_free(ipdevice->pac);
1525 ipdevice->pac = g_strdup(url);
1530 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1532 struct connman_ipdevice *ipdevice;
1534 if (!ipconfig || ipconfig->index < 0)
1537 ipdevice = g_hash_table_lookup(ipdevice_hash,
1538 GINT_TO_POINTER(ipconfig->index));
1542 return ipdevice->pac;
1545 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1546 const char *address)
1551 g_free(ipconfig->last_dhcp_address);
1552 ipconfig->last_dhcp_address = g_strdup(address);
1555 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1560 return ipconfig->last_dhcp_address;
1563 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1569 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1570 ipconfig->last_dhcpv6_prefixes = prefixes;
1573 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1578 return ipconfig->last_dhcpv6_prefixes;
1581 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1583 struct connman_ipdevice *ipdevice;
1588 ipdevice = g_hash_table_lookup(ipdevice_hash,
1589 GINT_TO_POINTER(ipconfig->index));
1593 ifname = connman_inet_ifname(ipconfig->index);
1595 set_ipv6_state(ifname, false);
1600 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1602 struct connman_ipdevice *ipdevice;
1607 ipdevice = g_hash_table_lookup(ipdevice_hash,
1608 GINT_TO_POINTER(ipconfig->index));
1612 ifname = connman_inet_ifname(ipconfig->index);
1614 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1615 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1617 set_ipv6_state(ifname, true);
1622 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1624 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1627 enable_ipv6(ipconfig);
1630 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1632 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1635 disable_ipv6(ipconfig);
1638 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1643 switch (ipconfig->method) {
1644 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1645 case CONNMAN_IPCONFIG_METHOD_OFF:
1647 case CONNMAN_IPCONFIG_METHOD_AUTO:
1648 case CONNMAN_IPCONFIG_METHOD_FIXED:
1649 case CONNMAN_IPCONFIG_METHOD_DHCP:
1650 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1657 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1659 struct connman_ipdevice *ipdevice;
1660 bool up = false, down = false;
1661 bool lower_up = false, lower_down = false;
1662 enum connman_ipconfig_type type;
1665 DBG("ipconfig %p", ipconfig);
1667 if (!ipconfig || ipconfig->index < 0)
1670 ipdevice = g_hash_table_lookup(ipdevice_hash,
1671 GINT_TO_POINTER(ipconfig->index));
1675 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1676 if (ipdevice->config_ipv4 == ipconfig)
1678 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1679 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1680 if (ipdevice->config_ipv6 == ipconfig)
1682 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1686 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1687 ipdevice->config_ipv4) {
1688 ipconfig_list = g_list_remove(ipconfig_list,
1689 ipdevice->config_ipv4);
1691 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1693 __connman_ipconfig_unref(ipdevice->config_ipv4);
1696 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1697 ipdevice->config_ipv6) {
1698 ipconfig_list = g_list_remove(ipconfig_list,
1699 ipdevice->config_ipv6);
1701 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1703 __connman_ipconfig_unref(ipdevice->config_ipv6);
1706 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1707 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1708 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1709 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1711 enable_ipv6(ipdevice->config_ipv6);
1713 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1715 if (ipdevice->flags & IFF_UP)
1720 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1721 (IFF_RUNNING | IFF_LOWER_UP))
1723 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1726 ifname = connman_inet_ifname(ipconfig->index);
1728 if (up && ipconfig->ops->up)
1729 ipconfig->ops->up(ipconfig, ifname);
1730 if (lower_up && ipconfig->ops->lower_up)
1731 ipconfig->ops->lower_up(ipconfig, ifname);
1733 if (lower_down && ipconfig->ops->lower_down)
1734 ipconfig->ops->lower_down(ipconfig, ifname);
1735 if (down && ipconfig->ops->down)
1736 ipconfig->ops->down(ipconfig, ifname);
1743 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1745 struct connman_ipdevice *ipdevice;
1746 #if defined TIZEN_EXT
1747 if (!simplified_log)
1749 DBG("ipconfig %p", ipconfig);
1751 if (!ipconfig || ipconfig->index < 0)
1754 ipdevice = g_hash_table_lookup(ipdevice_hash,
1755 GINT_TO_POINTER(ipconfig->index));
1759 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1762 if (ipdevice->config_ipv4 == ipconfig) {
1763 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1765 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1766 __connman_ipconfig_unref(ipdevice->config_ipv4);
1767 ipdevice->config_ipv4 = NULL;
1771 if (ipdevice->config_ipv6 == ipconfig) {
1772 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1774 #if defined TIZEN_EXT
1775 if (ipdevice->config_ipv6->method ==
1776 CONNMAN_IPCONFIG_METHOD_AUTO)
1777 disable_ipv6(ipdevice->config_ipv6);
1779 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1780 __connman_ipconfig_unref(ipdevice->config_ipv6);
1781 ipdevice->config_ipv6 = NULL;
1788 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1791 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1793 case CONNMAN_IPCONFIG_METHOD_OFF:
1795 case CONNMAN_IPCONFIG_METHOD_FIXED:
1797 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1799 case CONNMAN_IPCONFIG_METHOD_DHCP:
1801 case CONNMAN_IPCONFIG_METHOD_AUTO:
1808 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1810 if (g_strcmp0(method, "off") == 0)
1811 return CONNMAN_IPCONFIG_METHOD_OFF;
1812 else if (g_strcmp0(method, "fixed") == 0)
1813 return CONNMAN_IPCONFIG_METHOD_FIXED;
1814 else if (g_strcmp0(method, "manual") == 0)
1815 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1816 else if (g_strcmp0(method, "dhcp") == 0)
1817 return CONNMAN_IPCONFIG_METHOD_DHCP;
1818 else if (g_strcmp0(method, "auto") == 0)
1819 return CONNMAN_IPCONFIG_METHOD_AUTO;
1821 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1824 static const char *privacy2string(int privacy)
1828 else if (privacy == 1)
1834 static int string2privacy(const char *privacy)
1836 if (g_strcmp0(privacy, "disabled") == 0)
1838 else if (g_strcmp0(privacy, "enabled") == 0)
1840 else if (g_strcmp0(privacy, "preferred") == 0)
1842 else if (g_strcmp0(privacy, "prefered") == 0)
1848 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1850 struct connman_ipdevice *ipdevice;
1856 ipdevice = g_hash_table_lookup(ipdevice_hash,
1857 GINT_TO_POINTER(ipconfig->index));
1861 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1862 ipdevice->ipv6_privacy));
1867 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1875 privacy = string2privacy(value);
1877 ipconfig->ipv6_privacy_config = privacy;
1879 enable_ipv6(ipconfig);
1884 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1885 DBusMessageIter *iter)
1887 struct connman_ipaddress *append_addr = NULL;
1890 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1893 str = __connman_ipconfig_method2string(ipconfig->method);
1897 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1899 switch (ipconfig->method) {
1900 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1901 case CONNMAN_IPCONFIG_METHOD_OFF:
1904 case CONNMAN_IPCONFIG_METHOD_FIXED:
1905 append_addr = ipconfig->address;
1908 case CONNMAN_IPCONFIG_METHOD_AUTO:
1909 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1910 case CONNMAN_IPCONFIG_METHOD_DHCP:
1911 append_addr = ipconfig->system;
1912 #if defined TIZEN_EXT
1913 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1914 if (append_addr && append_addr->local == NULL)
1915 append_addr = ipconfig->address;
1923 if (append_addr->local) {
1925 struct in_addr netmask;
1928 connman_dbus_dict_append_basic(iter, "Address",
1929 DBUS_TYPE_STRING, &append_addr->local);
1931 addr = 0xffffffff << (32 - append_addr->prefixlen);
1932 netmask.s_addr = htonl(addr);
1933 mask = inet_ntoa(netmask);
1934 connman_dbus_dict_append_basic(iter, "Netmask",
1935 DBUS_TYPE_STRING, &mask);
1938 if (append_addr->gateway)
1939 connman_dbus_dict_append_basic(iter, "Gateway",
1940 DBUS_TYPE_STRING, &append_addr->gateway);
1942 #if defined TIZEN_EXT
1943 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1945 server_ip = __connman_dhcp_get_server_address(ipconfig);
1947 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1948 DBUS_TYPE_STRING, &server_ip);
1951 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1952 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1957 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1958 DBusMessageIter *iter,
1959 struct connman_ipconfig *ipconfig_ipv4)
1961 struct connman_ipaddress *append_addr = NULL;
1962 const char *str, *privacy;
1964 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1967 str = __connman_ipconfig_method2string(ipconfig->method);
1971 if (ipconfig_ipv4 &&
1972 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1973 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1977 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1979 switch (ipconfig->method) {
1980 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1981 case CONNMAN_IPCONFIG_METHOD_OFF:
1984 case CONNMAN_IPCONFIG_METHOD_FIXED:
1985 append_addr = ipconfig->address;
1988 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1989 case CONNMAN_IPCONFIG_METHOD_DHCP:
1990 case CONNMAN_IPCONFIG_METHOD_AUTO:
1991 append_addr = ipconfig->system;
1992 #if defined TIZEN_EXT
1993 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1994 if (append_addr && append_addr->local == NULL)
1995 append_addr = ipconfig->address;
2003 if (append_addr->local) {
2004 connman_dbus_dict_append_basic(iter, "Address",
2005 DBUS_TYPE_STRING, &append_addr->local);
2006 connman_dbus_dict_append_basic(iter, "PrefixLength",
2008 &append_addr->prefixlen);
2011 if (append_addr->gateway)
2012 connman_dbus_dict_append_basic(iter, "Gateway",
2013 DBUS_TYPE_STRING, &append_addr->gateway);
2015 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2016 connman_dbus_dict_append_basic(iter, "Privacy",
2017 DBUS_TYPE_STRING, &privacy);
2020 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2021 DBusMessageIter *iter)
2023 const char *str, *privacy;
2025 str = __connman_ipconfig_method2string(ipconfig->method);
2029 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2031 switch (ipconfig->method) {
2032 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2033 case CONNMAN_IPCONFIG_METHOD_OFF:
2034 case CONNMAN_IPCONFIG_METHOD_DHCP:
2036 case CONNMAN_IPCONFIG_METHOD_FIXED:
2037 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2038 case CONNMAN_IPCONFIG_METHOD_AUTO:
2042 if (!ipconfig->address)
2045 if (ipconfig->address->local) {
2046 connman_dbus_dict_append_basic(iter, "Address",
2047 DBUS_TYPE_STRING, &ipconfig->address->local);
2048 connman_dbus_dict_append_basic(iter, "PrefixLength",
2050 &ipconfig->address->prefixlen);
2053 if (ipconfig->address->gateway)
2054 connman_dbus_dict_append_basic(iter, "Gateway",
2055 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2057 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2058 connman_dbus_dict_append_basic(iter, "Privacy",
2059 DBUS_TYPE_STRING, &privacy);
2062 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2063 DBusMessageIter *iter)
2067 str = __connman_ipconfig_method2string(ipconfig->method);
2071 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2073 switch (ipconfig->method) {
2074 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2075 case CONNMAN_IPCONFIG_METHOD_OFF:
2076 case CONNMAN_IPCONFIG_METHOD_DHCP:
2077 case CONNMAN_IPCONFIG_METHOD_AUTO:
2079 case CONNMAN_IPCONFIG_METHOD_FIXED:
2080 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2084 if (!ipconfig->address)
2087 if (ipconfig->address->local) {
2089 struct in_addr netmask;
2092 connman_dbus_dict_append_basic(iter, "Address",
2093 DBUS_TYPE_STRING, &ipconfig->address->local);
2095 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2096 netmask.s_addr = htonl(addr);
2097 mask = inet_ntoa(netmask);
2098 connman_dbus_dict_append_basic(iter, "Netmask",
2099 DBUS_TYPE_STRING, &mask);
2102 if (ipconfig->address->gateway)
2103 connman_dbus_dict_append_basic(iter, "Gateway",
2104 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2107 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2108 DBusMessageIter *array)
2110 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2111 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2112 *privacy_string = NULL;
2113 int prefix_length = 0, privacy = 0;
2114 DBusMessageIter dict;
2117 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2120 dbus_message_iter_recurse(array, &dict);
2122 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2123 DBusMessageIter entry, value;
2127 dbus_message_iter_recurse(&dict, &entry);
2129 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2132 dbus_message_iter_get_basic(&entry, &key);
2133 dbus_message_iter_next(&entry);
2135 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2138 dbus_message_iter_recurse(&entry, &value);
2140 type = dbus_message_iter_get_arg_type(&value);
2142 if (g_str_equal(key, "Method")) {
2145 if (type != DBUS_TYPE_STRING)
2148 dbus_message_iter_get_basic(&value, &str);
2149 method = __connman_ipconfig_string2method(str);
2150 } else if (g_str_equal(key, "Address")) {
2151 if (type != DBUS_TYPE_STRING)
2154 dbus_message_iter_get_basic(&value, &address);
2155 } else if (g_str_equal(key, "PrefixLength")) {
2156 if (type != DBUS_TYPE_BYTE)
2159 dbus_message_iter_get_basic(&value, &prefix_length);
2161 if (prefix_length < 0 || prefix_length > 128)
2163 } else if (g_str_equal(key, "Netmask")) {
2164 if (type != DBUS_TYPE_STRING)
2167 dbus_message_iter_get_basic(&value, &netmask);
2168 } else if (g_str_equal(key, "Gateway")) {
2169 if (type != DBUS_TYPE_STRING)
2172 dbus_message_iter_get_basic(&value, &gateway);
2173 } else if (g_str_equal(key, "Privacy")) {
2174 if (type != DBUS_TYPE_STRING)
2177 dbus_message_iter_get_basic(&value, &privacy_string);
2178 privacy = string2privacy(privacy_string);
2181 dbus_message_iter_next(&dict);
2184 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2186 method, address, netmask, gateway, prefix_length,
2190 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2191 case CONNMAN_IPCONFIG_METHOD_FIXED:
2194 case CONNMAN_IPCONFIG_METHOD_OFF:
2195 ipconfig->method = method;
2196 #if defined TIZEN_EXT
2197 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2198 disable_ipv6(ipconfig);
2203 case CONNMAN_IPCONFIG_METHOD_AUTO:
2204 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2207 ipconfig->method = method;
2209 ipconfig->ipv6_privacy_config = privacy;
2210 #if defined TIZEN_EXT
2211 enable_ipv6(ipconfig);
2216 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2217 switch (ipconfig->type) {
2218 case CONNMAN_IPCONFIG_TYPE_IPV4:
2221 case CONNMAN_IPCONFIG_TYPE_IPV6:
2224 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2225 case CONNMAN_IPCONFIG_TYPE_ALL:
2230 if ((address && connman_inet_check_ipaddress(address)
2233 connman_inet_check_ipaddress(netmask)
2236 connman_inet_check_ipaddress(gateway)
2240 ipconfig->method = method;
2242 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2243 connman_ipaddress_set_ipv4(ipconfig->address,
2244 address, netmask, gateway);
2246 return connman_ipaddress_set_ipv6(
2247 ipconfig->address, address,
2248 prefix_length, gateway);
2252 case CONNMAN_IPCONFIG_METHOD_DHCP:
2253 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2256 ipconfig->method = method;
2263 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2264 DBusMessageIter *iter)
2266 struct connman_ipdevice *ipdevice;
2267 const char *method = "auto";
2269 connman_dbus_dict_append_basic(iter, "Method",
2270 DBUS_TYPE_STRING, &method);
2272 ipdevice = g_hash_table_lookup(ipdevice_hash,
2273 GINT_TO_POINTER(ipconfig->index));
2277 if (ipconfig->index >= 0) {
2278 char *ifname = connman_inet_ifname(ipconfig->index);
2280 connman_dbus_dict_append_basic(iter, "Interface",
2281 DBUS_TYPE_STRING, &ifname);
2286 if (ipdevice->address)
2287 connman_dbus_dict_append_basic(iter, "Address",
2288 DBUS_TYPE_STRING, &ipdevice->address);
2290 if (ipdevice->mtu > 0)
2291 connman_dbus_dict_append_basic(iter, "MTU",
2292 DBUS_TYPE_UINT16, &ipdevice->mtu);
2295 void __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2296 GKeyFile *keyfile, const char *identifier, const char *prefix)
2300 struct ipconfig_store is = { .file = keyfile,
2301 .group = identifier,
2304 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2306 method = store_get_str(&is, "method");
2308 switch (ipconfig->type) {
2309 case CONNMAN_IPCONFIG_TYPE_IPV4:
2310 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2313 case CONNMAN_IPCONFIG_TYPE_IPV6:
2314 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2317 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2318 case CONNMAN_IPCONFIG_TYPE_ALL:
2319 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2323 ipconfig->method = __connman_ipconfig_string2method(method);
2327 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2328 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2330 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2331 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2332 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2335 privacy = store_get_str(&is, "privacy");
2336 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2340 g_strfreev(ipconfig->last_dhcpv6_prefixes);
2341 ipconfig->last_dhcpv6_prefixes =
2342 store_get_strs(&is, "DHCP.LastPrefixes");
2346 switch (ipconfig->method) {
2347 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2348 case CONNMAN_IPCONFIG_METHOD_OFF:
2351 case CONNMAN_IPCONFIG_METHOD_FIXED:
2352 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2353 ipconfig->address->prefixlen =
2354 store_get_int(&is, "netmask_prefixlen");
2356 g_free(ipconfig->address->local);
2357 ipconfig->address->local =
2358 store_get_str(&is, "local_address");
2360 g_free(ipconfig->address->peer);
2361 ipconfig->address->peer =
2362 store_get_str(&is, "peer_address");
2364 g_free(ipconfig->address->broadcast);
2365 ipconfig->address->broadcast =
2366 store_get_str(&is, "broadcast_address");
2368 g_free(ipconfig->address->gateway);
2369 ipconfig->address->gateway =
2370 store_get_str(&is, "gateway");
2373 case CONNMAN_IPCONFIG_METHOD_AUTO:
2374 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2378 * If the last used method for IPv4 was AUTO then we
2379 * try first DHCP. We will try also to use the last
2380 * used DHCP address, if exits.
2382 __connman_ipconfig_set_method(ipconfig,
2383 CONNMAN_IPCONFIG_METHOD_DHCP);
2386 case CONNMAN_IPCONFIG_METHOD_DHCP:
2387 str = store_get_str(&is, "DHCP.LastAddress");
2389 g_free(ipconfig->last_dhcp_address);
2390 ipconfig->last_dhcp_address = str;
2397 void __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2398 GKeyFile *keyfile, const char *identifier, const char *prefix)
2401 struct ipconfig_store is = { .file = keyfile,
2402 .group = identifier,
2405 method = __connman_ipconfig_method2string(ipconfig->method);
2406 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2408 store_set_str(&is, "method", method);
2410 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2411 store_set_str(&is, "privacy",
2412 privacy2string(ipconfig->ipv6_privacy_config));
2414 store_set_str(&is, "DHCP.LastAddress",
2415 ipconfig->last_dhcp_address);
2417 store_set_strs(&is, "DHCP.LastPrefixes",
2418 ipconfig->last_dhcpv6_prefixes);
2421 switch (ipconfig->method) {
2422 case CONNMAN_IPCONFIG_METHOD_FIXED:
2423 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2426 case CONNMAN_IPCONFIG_METHOD_DHCP:
2427 store_set_str(&is, "DHCP.LastAddress",
2428 ipconfig->last_dhcp_address);
2431 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2432 case CONNMAN_IPCONFIG_METHOD_OFF:
2433 case CONNMAN_IPCONFIG_METHOD_AUTO:
2437 store_set_int(&is, "netmask_prefixlen", ipconfig->address->prefixlen);
2438 store_set_str(&is, "local_address", ipconfig->address->local);
2439 store_set_str(&is, "peer_address", ipconfig->address->peer);
2440 store_set_str(&is, "broadcast_address", ipconfig->address->broadcast);
2441 store_set_str(&is, "gateway", ipconfig->address->gateway);
2444 int __connman_ipconfig_init(void)
2448 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2449 NULL, free_ipdevice);
2451 is_ipv6_supported = connman_inet_is_ipv6_supported();
2456 void __connman_ipconfig_cleanup(void)
2460 g_hash_table_destroy(ipdevice_hash);
2461 ipdevice_hash = NULL;