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);
788 #if defined TIZEN_EXT
790 struct connman_service *service;
791 struct connman_ipconfig *local_ipconfig;
793 service = connman_service_get_default();
797 local_ipconfig = __connman_service_get_ip4config(service);
801 local = __connman_ipconfig_get_local(local_ipconfig);
805 DBG("local %s", local);
807 if (g_strcmp0(local, address) != 0)
810 __connman_connection_update_default_gateway();
817 void __connman_ipconfig_deladdr(int index, int family, const char *label,
818 unsigned char prefixlen, const char *address)
820 struct connman_ipdevice *ipdevice;
821 struct connman_ipaddress *ipaddress;
822 enum connman_ipconfig_type type;
826 DBG("index %d", index);
828 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
832 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
836 if (family == AF_INET)
837 type = CONNMAN_IPCONFIG_TYPE_IPV4;
838 else if (family == AF_INET6)
839 type = CONNMAN_IPCONFIG_TYPE_IPV6;
843 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
846 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
847 __connman_ippool_deladdr(index, address, prefixlen);
849 connman_ipaddress_clear(ipaddress);
852 ifname = connman_inet_ifname(index);
853 connman_info("%s {del} address %s/%u label %s", ifname,
854 address, prefixlen, label);
856 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
859 if (g_slist_length(ipdevice->address_list) > 0)
862 for (list = g_list_first(ipconfig_list); list;
863 list = g_list_next(list)) {
864 struct connman_ipconfig *ipconfig = list->data;
866 if (index != ipconfig->index)
869 if (type != ipconfig->type)
875 if (ipconfig->ops->ip_release)
876 ipconfig->ops->ip_release(ipconfig, ifname);
883 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
884 const char *dst, const char *gateway)
886 struct connman_ipdevice *ipdevice;
889 DBG("index %d", index);
891 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
895 ifname = connman_inet_ifname(index);
897 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
898 g_strcmp0(dst, "::") == 0)) {
900 enum connman_ipconfig_type type;
902 if (family == AF_INET6) {
903 type = CONNMAN_IPCONFIG_TYPE_IPV6;
904 g_free(ipdevice->ipv6_gateway);
905 ipdevice->ipv6_gateway = g_strdup(gateway);
907 if (ipdevice->config_ipv6 &&
908 ipdevice->config_ipv6->system) {
909 g_free(ipdevice->config_ipv6->system->gateway);
910 ipdevice->config_ipv6->system->gateway =
913 } else if (family == AF_INET) {
914 type = CONNMAN_IPCONFIG_TYPE_IPV4;
915 g_free(ipdevice->ipv4_gateway);
916 ipdevice->ipv4_gateway = g_strdup(gateway);
918 if (ipdevice->config_ipv4 &&
919 ipdevice->config_ipv4->system) {
920 g_free(ipdevice->config_ipv4->system->gateway);
921 ipdevice->config_ipv4->system->gateway =
927 for (config_list = g_list_first(ipconfig_list); config_list;
928 config_list = g_list_next(config_list)) {
929 struct connman_ipconfig *ipconfig = config_list->data;
931 if (index != ipconfig->index)
934 if (type != ipconfig->type)
940 if (ipconfig->ops->route_set)
941 ipconfig->ops->route_set(ipconfig, ifname);
945 connman_info("%s {add} route %s gw %s scope %u <%s>",
946 ifname, dst, gateway, scope, scope2str(scope));
952 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
953 const char *dst, const char *gateway)
955 struct connman_ipdevice *ipdevice;
958 DBG("index %d", index);
960 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
964 ifname = connman_inet_ifname(index);
966 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
967 g_strcmp0(dst, "::") == 0)) {
969 enum connman_ipconfig_type type;
971 if (family == AF_INET6) {
972 type = CONNMAN_IPCONFIG_TYPE_IPV6;
973 g_free(ipdevice->ipv6_gateway);
974 ipdevice->ipv6_gateway = NULL;
976 if (ipdevice->config_ipv6 &&
977 ipdevice->config_ipv6->system) {
978 g_free(ipdevice->config_ipv6->system->gateway);
979 ipdevice->config_ipv6->system->gateway = NULL;
981 } else if (family == AF_INET) {
982 type = CONNMAN_IPCONFIG_TYPE_IPV4;
983 g_free(ipdevice->ipv4_gateway);
984 ipdevice->ipv4_gateway = NULL;
986 if (ipdevice->config_ipv4 &&
987 ipdevice->config_ipv4->system) {
988 g_free(ipdevice->config_ipv4->system->gateway);
989 ipdevice->config_ipv4->system->gateway = NULL;
994 for (config_list = g_list_first(ipconfig_list); config_list;
995 config_list = g_list_next(config_list)) {
996 struct connman_ipconfig *ipconfig = config_list->data;
998 if (index != ipconfig->index)
1001 if (type != ipconfig->type)
1007 if (ipconfig->ops->route_unset)
1008 ipconfig->ops->route_unset(ipconfig, ifname);
1012 connman_info("%s {del} route %s gw %s scope %u <%s>",
1013 ifname, dst, gateway, scope, scope2str(scope));
1019 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1024 keys = g_hash_table_get_keys(ipdevice_hash);
1028 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1029 int index = GPOINTER_TO_INT(list->data);
1031 function(index, user_data);
1037 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1038 struct connman_ipconfig *ipconfig)
1040 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1043 unsigned short __connman_ipconfig_get_type_from_index(int index)
1045 struct connman_ipdevice *ipdevice;
1047 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1051 return ipdevice->type;
1054 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1056 struct connman_ipdevice *ipdevice;
1058 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1062 return ipdevice->flags;
1065 const char *__connman_ipconfig_get_gateway_from_index(int index,
1066 enum connman_ipconfig_type type)
1068 struct connman_ipdevice *ipdevice;
1070 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1074 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1075 if (ipdevice->ipv4_gateway)
1076 return ipdevice->ipv4_gateway;
1078 if (ipdevice->config_ipv4 &&
1079 ipdevice->config_ipv4->address)
1080 return ipdevice->config_ipv4->address->gateway;
1083 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1084 if (ipdevice->ipv6_gateway)
1085 return ipdevice->ipv6_gateway;
1087 if (ipdevice->config_ipv6 &&
1088 ipdevice->config_ipv6->address)
1089 return ipdevice->config_ipv6->address->gateway;
1095 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1097 ipconfig->index = index;
1100 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1102 if (!ipconfig->address)
1105 return ipconfig->address->local;
1108 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1109 const char *address)
1111 if (!ipconfig->address)
1114 g_free(ipconfig->address->local);
1115 ipconfig->address->local = g_strdup(address);
1118 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1120 if (!ipconfig->address)
1123 return ipconfig->address->peer;
1126 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1127 const char *address)
1129 if (!ipconfig->address)
1132 g_free(ipconfig->address->peer);
1133 ipconfig->address->peer = g_strdup(address);
1136 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1138 if (!ipconfig->address)
1141 return ipconfig->address->broadcast;
1144 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1145 const char *broadcast)
1147 if (!ipconfig->address)
1150 g_free(ipconfig->address->broadcast);
1151 ipconfig->address->broadcast = g_strdup(broadcast);
1154 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1156 if (!ipconfig->address)
1159 return ipconfig->address->gateway;
1162 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1163 const char *gateway)
1167 if (!ipconfig->address)
1169 g_free(ipconfig->address->gateway);
1170 ipconfig->address->gateway = g_strdup(gateway);
1173 #if defined TIZEN_EXT
1174 void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig,
1175 int dhcp_lease_duration)
1177 ipconfig->dhcp_lease_duration = dhcp_lease_duration;
1181 #if defined TIZEN_EXT
1182 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1184 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1187 #if !defined TIZEN_EXT
1188 struct connman_service *service;
1193 if (!ipconfig->address)
1196 #if !defined TIZEN_EXT
1197 service = __connman_service_lookup_from_index(ipconfig->index);
1202 DBG("type %d gw %s peer %s", ipconfig->type,
1203 ipconfig->address->gateway, ipconfig->address->peer);
1205 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1206 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1207 return __connman_connection_gateway_add(service,
1208 ipconfig->address->gateway,
1210 ipconfig->address->peer);
1215 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1217 struct connman_service *service;
1221 service = __connman_service_lookup_from_index(ipconfig->index);
1223 __connman_connection_gateway_remove(service, ipconfig->type);
1226 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1228 if (!ipconfig->address)
1231 return ipconfig->address->prefixlen;
1234 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1235 unsigned char prefixlen)
1237 if (!ipconfig->address)
1240 ipconfig->address->prefixlen = prefixlen;
1243 static struct connman_ipconfig *create_ipv6config(int index)
1245 struct connman_ipconfig *ipv6config;
1246 struct connman_ipdevice *ipdevice;
1248 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1252 ipv6config->refcount = 1;
1254 ipv6config->index = index;
1255 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1257 if (!is_ipv6_supported)
1258 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1260 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1262 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1264 #if !defined TIZEN_EXT
1265 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1267 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy = 2;
1270 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1271 if (!ipv6config->address) {
1276 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1278 DBG("ipconfig %p index %d method %s", ipv6config, index,
1279 __connman_ipconfig_method2string(ipv6config->method));
1285 * connman_ipconfig_create:
1287 * Allocate a new ipconfig structure.
1289 * Returns: a newly-allocated #connman_ipconfig structure
1291 struct connman_ipconfig *__connman_ipconfig_create(int index,
1292 enum connman_ipconfig_type type)
1294 struct connman_ipconfig *ipconfig;
1296 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1297 return create_ipv6config(index);
1299 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1303 ipconfig->refcount = 1;
1305 ipconfig->index = index;
1306 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1308 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1309 if (!ipconfig->address) {
1314 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1315 #if defined TIZEN_EXT
1316 if (!simplified_log)
1318 DBG("ipconfig %p index %d", ipconfig, index);
1325 * connman_ipconfig_ref:
1326 * @ipconfig: ipconfig structure
1328 * Increase reference counter of ipconfig
1330 struct connman_ipconfig *
1331 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1332 const char *file, int line, const char *caller)
1334 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1335 file, line, caller);
1337 __sync_fetch_and_add(&ipconfig->refcount, 1);
1343 * connman_ipconfig_unref:
1344 * @ipconfig: ipconfig structure
1346 * Decrease reference counter of ipconfig
1348 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1349 const char *file, int line, const char *caller)
1353 #if defined TIZEN_EXT
1354 if (!simplified_log)
1356 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1357 file, line, caller);
1359 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1362 if (__connman_ipconfig_disable(ipconfig) < 0)
1363 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1365 __connman_ipconfig_set_ops(ipconfig, NULL);
1367 connman_ipaddress_free(ipconfig->system);
1368 connman_ipaddress_free(ipconfig->address);
1369 g_free(ipconfig->last_dhcp_address);
1370 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1375 * connman_ipconfig_get_data:
1376 * @ipconfig: ipconfig structure
1378 * Get private data pointer
1380 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1385 return ipconfig->ops_data;
1389 * connman_ipconfig_set_data:
1390 * @ipconfig: ipconfig structure
1391 * @data: data pointer
1393 * Set private data pointer
1395 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1397 ipconfig->ops_data = data;
1401 * connman_ipconfig_get_index:
1402 * @ipconfig: ipconfig structure
1404 * Get interface index
1406 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1411 return ipconfig->index;
1415 * connman_ipconfig_set_ops:
1416 * @ipconfig: ipconfig structure
1417 * @ops: operation callbacks
1419 * Set the operation callbacks
1421 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1422 const struct connman_ipconfig_ops *ops)
1424 ipconfig->ops = ops;
1428 * connman_ipconfig_set_method:
1429 * @ipconfig: ipconfig structure
1430 * @method: configuration method
1432 * Set the configuration method
1434 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1435 enum connman_ipconfig_method method)
1437 ipconfig->method = method;
1442 enum connman_ipconfig_method __connman_ipconfig_get_method(
1443 struct connman_ipconfig *ipconfig)
1446 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1448 return ipconfig->method;
1451 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1453 switch (ipconfig->method) {
1454 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1455 case CONNMAN_IPCONFIG_METHOD_OFF:
1457 case CONNMAN_IPCONFIG_METHOD_AUTO:
1458 case CONNMAN_IPCONFIG_METHOD_FIXED:
1459 case CONNMAN_IPCONFIG_METHOD_DHCP:
1460 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1461 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1462 return connman_inet_set_address(ipconfig->index,
1464 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1465 return connman_inet_set_ipv6_address(
1466 ipconfig->index, ipconfig->address);
1472 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1479 switch (ipconfig->method) {
1480 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1481 case CONNMAN_IPCONFIG_METHOD_OFF:
1483 case CONNMAN_IPCONFIG_METHOD_AUTO:
1484 case CONNMAN_IPCONFIG_METHOD_DHCP:
1485 err = __connman_ipconfig_address_unset(ipconfig);
1486 connman_ipaddress_clear(ipconfig->address);
1489 case CONNMAN_IPCONFIG_METHOD_FIXED:
1490 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1491 return __connman_ipconfig_address_unset(ipconfig);
1497 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1504 #if defined TIZEN_EXT
1505 DBG("ipconfig method %d type %d", ipconfig->method, ipconfig->type);
1507 DBG("method %d", ipconfig->method);
1510 switch (ipconfig->method) {
1511 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1512 case CONNMAN_IPCONFIG_METHOD_OFF:
1514 case CONNMAN_IPCONFIG_METHOD_AUTO:
1515 case CONNMAN_IPCONFIG_METHOD_FIXED:
1516 case CONNMAN_IPCONFIG_METHOD_DHCP:
1517 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1518 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1519 err = connman_inet_clear_address(ipconfig->index,
1521 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1522 err = connman_inet_clear_ipv6_address(
1524 ipconfig->address->local,
1525 ipconfig->address->prefixlen);
1535 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1538 struct connman_ipdevice *ipdevice;
1540 if (!ipconfig || ipconfig->index < 0)
1543 ipdevice = g_hash_table_lookup(ipdevice_hash,
1544 GINT_TO_POINTER(ipconfig->index));
1548 g_free(ipdevice->pac);
1549 ipdevice->pac = g_strdup(url);
1554 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1556 struct connman_ipdevice *ipdevice;
1558 if (!ipconfig || ipconfig->index < 0)
1561 ipdevice = g_hash_table_lookup(ipdevice_hash,
1562 GINT_TO_POINTER(ipconfig->index));
1566 return ipdevice->pac;
1569 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1570 const char *address)
1575 g_free(ipconfig->last_dhcp_address);
1576 ipconfig->last_dhcp_address = g_strdup(address);
1579 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1584 return ipconfig->last_dhcp_address;
1587 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1593 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1594 ipconfig->last_dhcpv6_prefixes = prefixes;
1597 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1602 return ipconfig->last_dhcpv6_prefixes;
1605 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1607 struct connman_ipdevice *ipdevice;
1612 ipdevice = g_hash_table_lookup(ipdevice_hash,
1613 GINT_TO_POINTER(ipconfig->index));
1617 ifname = connman_inet_ifname(ipconfig->index);
1619 set_ipv6_state(ifname, false);
1624 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1626 struct connman_ipdevice *ipdevice;
1631 ipdevice = g_hash_table_lookup(ipdevice_hash,
1632 GINT_TO_POINTER(ipconfig->index));
1636 ifname = connman_inet_ifname(ipconfig->index);
1638 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1639 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1641 set_ipv6_state(ifname, true);
1646 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1648 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1651 enable_ipv6(ipconfig);
1654 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1656 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1659 disable_ipv6(ipconfig);
1662 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1667 switch (ipconfig->method) {
1668 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1669 case CONNMAN_IPCONFIG_METHOD_OFF:
1671 case CONNMAN_IPCONFIG_METHOD_AUTO:
1672 case CONNMAN_IPCONFIG_METHOD_FIXED:
1673 case CONNMAN_IPCONFIG_METHOD_DHCP:
1674 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1681 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1683 struct connman_ipdevice *ipdevice;
1684 bool up = false, down = false;
1685 bool lower_up = false, lower_down = false;
1686 enum connman_ipconfig_type type;
1689 DBG("ipconfig %p", ipconfig);
1691 if (!ipconfig || ipconfig->index < 0)
1694 ipdevice = g_hash_table_lookup(ipdevice_hash,
1695 GINT_TO_POINTER(ipconfig->index));
1699 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1700 if (ipdevice->config_ipv4 == ipconfig)
1702 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1703 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1704 if (ipdevice->config_ipv6 == ipconfig)
1706 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1710 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1711 ipdevice->config_ipv4) {
1712 ipconfig_list = g_list_remove(ipconfig_list,
1713 ipdevice->config_ipv4);
1715 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1717 __connman_ipconfig_unref(ipdevice->config_ipv4);
1720 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1721 ipdevice->config_ipv6) {
1722 ipconfig_list = g_list_remove(ipconfig_list,
1723 ipdevice->config_ipv6);
1725 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1727 __connman_ipconfig_unref(ipdevice->config_ipv6);
1730 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1731 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1732 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1733 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1735 enable_ipv6(ipdevice->config_ipv6);
1737 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1739 if (ipdevice->flags & IFF_UP)
1744 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1745 (IFF_RUNNING | IFF_LOWER_UP))
1747 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1750 ifname = connman_inet_ifname(ipconfig->index);
1752 if (up && ipconfig->ops->up)
1753 ipconfig->ops->up(ipconfig, ifname);
1754 if (lower_up && ipconfig->ops->lower_up)
1755 ipconfig->ops->lower_up(ipconfig, ifname);
1757 if (lower_down && ipconfig->ops->lower_down)
1758 ipconfig->ops->lower_down(ipconfig, ifname);
1759 if (down && ipconfig->ops->down)
1760 ipconfig->ops->down(ipconfig, ifname);
1767 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1769 struct connman_ipdevice *ipdevice;
1770 #if defined TIZEN_EXT
1771 if (!simplified_log)
1773 DBG("ipconfig %p", ipconfig);
1775 if (!ipconfig || ipconfig->index < 0)
1778 ipdevice = g_hash_table_lookup(ipdevice_hash,
1779 GINT_TO_POINTER(ipconfig->index));
1783 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1786 if (ipdevice->config_ipv4 == ipconfig) {
1787 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1789 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1790 __connman_ipconfig_unref(ipdevice->config_ipv4);
1791 ipdevice->config_ipv4 = NULL;
1795 if (ipdevice->config_ipv6 == ipconfig) {
1796 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1798 #if defined TIZEN_EXT
1799 if (ipdevice->config_ipv6->method ==
1800 CONNMAN_IPCONFIG_METHOD_AUTO)
1801 disable_ipv6(ipdevice->config_ipv6);
1803 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1804 __connman_ipconfig_unref(ipdevice->config_ipv6);
1805 ipdevice->config_ipv6 = NULL;
1812 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1815 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1817 case CONNMAN_IPCONFIG_METHOD_OFF:
1819 case CONNMAN_IPCONFIG_METHOD_FIXED:
1821 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1823 case CONNMAN_IPCONFIG_METHOD_DHCP:
1825 case CONNMAN_IPCONFIG_METHOD_AUTO:
1832 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1834 if (g_strcmp0(method, "off") == 0)
1835 return CONNMAN_IPCONFIG_METHOD_OFF;
1836 else if (g_strcmp0(method, "fixed") == 0)
1837 return CONNMAN_IPCONFIG_METHOD_FIXED;
1838 else if (g_strcmp0(method, "manual") == 0)
1839 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1840 else if (g_strcmp0(method, "dhcp") == 0)
1841 return CONNMAN_IPCONFIG_METHOD_DHCP;
1842 else if (g_strcmp0(method, "auto") == 0)
1843 return CONNMAN_IPCONFIG_METHOD_AUTO;
1845 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1848 static const char *privacy2string(int privacy)
1852 else if (privacy == 1)
1858 static int string2privacy(const char *privacy)
1860 if (g_strcmp0(privacy, "disabled") == 0)
1862 else if (g_strcmp0(privacy, "enabled") == 0)
1864 else if (g_strcmp0(privacy, "preferred") == 0)
1866 else if (g_strcmp0(privacy, "prefered") == 0)
1872 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1874 struct connman_ipdevice *ipdevice;
1880 ipdevice = g_hash_table_lookup(ipdevice_hash,
1881 GINT_TO_POINTER(ipconfig->index));
1885 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1886 ipdevice->ipv6_privacy));
1891 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1899 privacy = string2privacy(value);
1901 ipconfig->ipv6_privacy_config = privacy;
1903 enable_ipv6(ipconfig);
1908 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1909 DBusMessageIter *iter)
1911 struct connman_ipaddress *append_addr = NULL;
1914 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1917 str = __connman_ipconfig_method2string(ipconfig->method);
1921 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1923 switch (ipconfig->method) {
1924 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1925 case CONNMAN_IPCONFIG_METHOD_OFF:
1928 case CONNMAN_IPCONFIG_METHOD_FIXED:
1929 append_addr = ipconfig->address;
1932 case CONNMAN_IPCONFIG_METHOD_AUTO:
1933 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1934 case CONNMAN_IPCONFIG_METHOD_DHCP:
1935 append_addr = ipconfig->system;
1936 #if defined TIZEN_EXT
1937 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1938 if (append_addr && append_addr->local == NULL)
1939 append_addr = ipconfig->address;
1947 if (append_addr->local) {
1949 struct in_addr netmask;
1952 connman_dbus_dict_append_basic(iter, "Address",
1953 DBUS_TYPE_STRING, &append_addr->local);
1955 addr = 0xffffffff << (32 - append_addr->prefixlen);
1956 netmask.s_addr = htonl(addr);
1957 mask = inet_ntoa(netmask);
1958 connman_dbus_dict_append_basic(iter, "Netmask",
1959 DBUS_TYPE_STRING, &mask);
1962 if (append_addr->gateway)
1963 connman_dbus_dict_append_basic(iter, "Gateway",
1964 DBUS_TYPE_STRING, &append_addr->gateway);
1966 #if defined TIZEN_EXT
1967 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1969 server_ip = __connman_dhcp_get_server_address(ipconfig);
1971 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1972 DBUS_TYPE_STRING, &server_ip);
1975 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1976 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1981 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1982 DBusMessageIter *iter,
1983 struct connman_ipconfig *ipconfig_ipv4)
1985 struct connman_ipaddress *append_addr = NULL;
1986 const char *str, *privacy;
1988 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1991 str = __connman_ipconfig_method2string(ipconfig->method);
1995 if (ipconfig_ipv4 &&
1996 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1997 if (__connman_6to4_check(ipconfig_ipv4) == 1)
2001 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2003 switch (ipconfig->method) {
2004 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2005 case CONNMAN_IPCONFIG_METHOD_OFF:
2008 case CONNMAN_IPCONFIG_METHOD_FIXED:
2009 append_addr = ipconfig->address;
2012 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2013 case CONNMAN_IPCONFIG_METHOD_DHCP:
2014 case CONNMAN_IPCONFIG_METHOD_AUTO:
2015 append_addr = ipconfig->system;
2016 #if defined TIZEN_EXT
2017 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
2018 if (append_addr && append_addr->local == NULL)
2019 append_addr = ipconfig->address;
2027 if (append_addr->local) {
2028 connman_dbus_dict_append_basic(iter, "Address",
2029 DBUS_TYPE_STRING, &append_addr->local);
2030 connman_dbus_dict_append_basic(iter, "PrefixLength",
2032 &append_addr->prefixlen);
2035 if (append_addr->gateway)
2036 connman_dbus_dict_append_basic(iter, "Gateway",
2037 DBUS_TYPE_STRING, &append_addr->gateway);
2039 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2040 connman_dbus_dict_append_basic(iter, "Privacy",
2041 DBUS_TYPE_STRING, &privacy);
2044 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2045 DBusMessageIter *iter)
2047 const char *str, *privacy;
2049 str = __connman_ipconfig_method2string(ipconfig->method);
2053 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2055 switch (ipconfig->method) {
2056 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2057 case CONNMAN_IPCONFIG_METHOD_OFF:
2058 case CONNMAN_IPCONFIG_METHOD_DHCP:
2060 case CONNMAN_IPCONFIG_METHOD_FIXED:
2061 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2062 case CONNMAN_IPCONFIG_METHOD_AUTO:
2066 if (!ipconfig->address)
2069 if (ipconfig->address->local) {
2070 connman_dbus_dict_append_basic(iter, "Address",
2071 DBUS_TYPE_STRING, &ipconfig->address->local);
2072 connman_dbus_dict_append_basic(iter, "PrefixLength",
2074 &ipconfig->address->prefixlen);
2077 if (ipconfig->address->gateway)
2078 connman_dbus_dict_append_basic(iter, "Gateway",
2079 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2081 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2082 connman_dbus_dict_append_basic(iter, "Privacy",
2083 DBUS_TYPE_STRING, &privacy);
2086 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2087 DBusMessageIter *iter)
2091 str = __connman_ipconfig_method2string(ipconfig->method);
2095 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2097 switch (ipconfig->method) {
2098 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2099 case CONNMAN_IPCONFIG_METHOD_OFF:
2100 case CONNMAN_IPCONFIG_METHOD_DHCP:
2101 case CONNMAN_IPCONFIG_METHOD_AUTO:
2103 case CONNMAN_IPCONFIG_METHOD_FIXED:
2104 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2108 if (!ipconfig->address)
2111 if (ipconfig->address->local) {
2113 struct in_addr netmask;
2116 connman_dbus_dict_append_basic(iter, "Address",
2117 DBUS_TYPE_STRING, &ipconfig->address->local);
2119 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2120 netmask.s_addr = htonl(addr);
2121 mask = inet_ntoa(netmask);
2122 connman_dbus_dict_append_basic(iter, "Netmask",
2123 DBUS_TYPE_STRING, &mask);
2126 if (ipconfig->address->gateway)
2127 connman_dbus_dict_append_basic(iter, "Gateway",
2128 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2131 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2132 DBusMessageIter *array)
2134 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2135 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2136 *privacy_string = NULL;
2137 int prefix_length = 0, privacy = 0;
2138 DBusMessageIter dict;
2141 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2144 dbus_message_iter_recurse(array, &dict);
2146 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2147 DBusMessageIter entry, value;
2151 dbus_message_iter_recurse(&dict, &entry);
2153 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2156 dbus_message_iter_get_basic(&entry, &key);
2157 dbus_message_iter_next(&entry);
2159 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2162 dbus_message_iter_recurse(&entry, &value);
2164 type = dbus_message_iter_get_arg_type(&value);
2166 if (g_str_equal(key, "Method")) {
2169 if (type != DBUS_TYPE_STRING)
2172 dbus_message_iter_get_basic(&value, &str);
2173 method = __connman_ipconfig_string2method(str);
2174 } else if (g_str_equal(key, "Address")) {
2175 if (type != DBUS_TYPE_STRING)
2178 dbus_message_iter_get_basic(&value, &address);
2179 } else if (g_str_equal(key, "PrefixLength")) {
2180 if (type != DBUS_TYPE_BYTE)
2183 dbus_message_iter_get_basic(&value, &prefix_length);
2185 if (prefix_length < 0 || prefix_length > 128)
2187 } else if (g_str_equal(key, "Netmask")) {
2188 if (type != DBUS_TYPE_STRING)
2191 dbus_message_iter_get_basic(&value, &netmask);
2192 } else if (g_str_equal(key, "Gateway")) {
2193 if (type != DBUS_TYPE_STRING)
2196 dbus_message_iter_get_basic(&value, &gateway);
2197 } else if (g_str_equal(key, "Privacy")) {
2198 if (type != DBUS_TYPE_STRING)
2201 dbus_message_iter_get_basic(&value, &privacy_string);
2202 privacy = string2privacy(privacy_string);
2205 dbus_message_iter_next(&dict);
2208 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2210 method, address, netmask, gateway, prefix_length,
2214 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2215 case CONNMAN_IPCONFIG_METHOD_FIXED:
2218 case CONNMAN_IPCONFIG_METHOD_OFF:
2219 ipconfig->method = method;
2220 #if defined TIZEN_EXT
2221 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2222 disable_ipv6(ipconfig);
2227 case CONNMAN_IPCONFIG_METHOD_AUTO:
2228 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2231 ipconfig->method = method;
2233 ipconfig->ipv6_privacy_config = privacy;
2234 #if defined TIZEN_EXT
2235 enable_ipv6(ipconfig);
2240 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2241 switch (ipconfig->type) {
2242 case CONNMAN_IPCONFIG_TYPE_IPV4:
2245 case CONNMAN_IPCONFIG_TYPE_IPV6:
2248 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2249 case CONNMAN_IPCONFIG_TYPE_ALL:
2254 if ((address && connman_inet_check_ipaddress(address)
2257 connman_inet_check_ipaddress(netmask)
2260 connman_inet_check_ipaddress(gateway)
2264 ipconfig->method = method;
2266 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2267 connman_ipaddress_set_ipv4(ipconfig->address,
2268 address, netmask, gateway);
2270 return connman_ipaddress_set_ipv6(
2271 ipconfig->address, address,
2272 prefix_length, gateway);
2276 case CONNMAN_IPCONFIG_METHOD_DHCP:
2277 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2280 ipconfig->method = method;
2287 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2288 DBusMessageIter *iter)
2290 struct connman_ipdevice *ipdevice;
2291 const char *method = "auto";
2293 connman_dbus_dict_append_basic(iter, "Method",
2294 DBUS_TYPE_STRING, &method);
2296 ipdevice = g_hash_table_lookup(ipdevice_hash,
2297 GINT_TO_POINTER(ipconfig->index));
2301 if (ipconfig->index >= 0) {
2302 char *ifname = connman_inet_ifname(ipconfig->index);
2304 connman_dbus_dict_append_basic(iter, "Interface",
2305 DBUS_TYPE_STRING, &ifname);
2310 if (ipdevice->address)
2311 connman_dbus_dict_append_basic(iter, "Address",
2312 DBUS_TYPE_STRING, &ipdevice->address);
2314 if (ipdevice->mtu > 0)
2315 connman_dbus_dict_append_basic(iter, "MTU",
2316 DBUS_TYPE_UINT16, &ipdevice->mtu);
2319 void __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2320 GKeyFile *keyfile, const char *identifier, const char *prefix)
2324 struct ipconfig_store is = { .file = keyfile,
2325 .group = identifier,
2328 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2330 method = store_get_str(&is, "method");
2332 switch (ipconfig->type) {
2333 case CONNMAN_IPCONFIG_TYPE_IPV4:
2334 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2337 case CONNMAN_IPCONFIG_TYPE_IPV6:
2338 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2341 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2342 case CONNMAN_IPCONFIG_TYPE_ALL:
2343 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2347 ipconfig->method = __connman_ipconfig_string2method(method);
2351 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2352 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2354 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2355 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2356 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2359 privacy = store_get_str(&is, "privacy");
2360 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2364 g_strfreev(ipconfig->last_dhcpv6_prefixes);
2365 ipconfig->last_dhcpv6_prefixes =
2366 store_get_strs(&is, "DHCP.LastPrefixes");
2370 switch (ipconfig->method) {
2371 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2372 case CONNMAN_IPCONFIG_METHOD_OFF:
2375 case CONNMAN_IPCONFIG_METHOD_FIXED:
2376 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2377 ipconfig->address->prefixlen =
2378 store_get_int(&is, "netmask_prefixlen");
2380 g_free(ipconfig->address->local);
2381 ipconfig->address->local =
2382 store_get_str(&is, "local_address");
2384 g_free(ipconfig->address->peer);
2385 ipconfig->address->peer =
2386 store_get_str(&is, "peer_address");
2388 g_free(ipconfig->address->broadcast);
2389 ipconfig->address->broadcast =
2390 store_get_str(&is, "broadcast_address");
2392 g_free(ipconfig->address->gateway);
2393 ipconfig->address->gateway =
2394 store_get_str(&is, "gateway");
2397 case CONNMAN_IPCONFIG_METHOD_AUTO:
2398 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2402 * If the last used method for IPv4 was AUTO then we
2403 * try first DHCP. We will try also to use the last
2404 * used DHCP address, if exits.
2406 __connman_ipconfig_set_method(ipconfig,
2407 CONNMAN_IPCONFIG_METHOD_DHCP);
2410 case CONNMAN_IPCONFIG_METHOD_DHCP:
2411 str = store_get_str(&is, "DHCP.LastAddress");
2413 g_free(ipconfig->last_dhcp_address);
2414 ipconfig->last_dhcp_address = str;
2421 void __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2422 GKeyFile *keyfile, const char *identifier, const char *prefix)
2425 struct ipconfig_store is = { .file = keyfile,
2426 .group = identifier,
2429 method = __connman_ipconfig_method2string(ipconfig->method);
2430 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2432 store_set_str(&is, "method", method);
2434 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2435 store_set_str(&is, "privacy",
2436 privacy2string(ipconfig->ipv6_privacy_config));
2438 store_set_str(&is, "DHCP.LastAddress",
2439 ipconfig->last_dhcp_address);
2441 store_set_strs(&is, "DHCP.LastPrefixes",
2442 ipconfig->last_dhcpv6_prefixes);
2445 switch (ipconfig->method) {
2446 case CONNMAN_IPCONFIG_METHOD_FIXED:
2447 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2450 case CONNMAN_IPCONFIG_METHOD_DHCP:
2451 store_set_str(&is, "DHCP.LastAddress",
2452 ipconfig->last_dhcp_address);
2455 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2456 case CONNMAN_IPCONFIG_METHOD_OFF:
2457 case CONNMAN_IPCONFIG_METHOD_AUTO:
2461 store_set_int(&is, "netmask_prefixlen", ipconfig->address->prefixlen);
2462 store_set_str(&is, "local_address", ipconfig->address->local);
2463 store_set_str(&is, "peer_address", ipconfig->address->peer);
2464 store_set_str(&is, "broadcast_address", ipconfig->address->broadcast);
2465 store_set_str(&is, "gateway", ipconfig->address->gateway);
2468 int __connman_ipconfig_init(void)
2472 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2473 NULL, free_ipdevice);
2475 is_ipv6_supported = connman_inet_is_ipv6_supported();
2480 void __connman_ipconfig_cleanup(void)
2484 g_hash_table_destroy(ipdevice_hash);
2485 ipdevice_hash = NULL;