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);
1778 enable_ipv6(ipdevice->config_ipv6);
1781 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1782 __connman_ipconfig_unref(ipdevice->config_ipv6);
1783 ipdevice->config_ipv6 = NULL;
1790 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1793 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1795 case CONNMAN_IPCONFIG_METHOD_OFF:
1797 case CONNMAN_IPCONFIG_METHOD_FIXED:
1799 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1801 case CONNMAN_IPCONFIG_METHOD_DHCP:
1803 case CONNMAN_IPCONFIG_METHOD_AUTO:
1810 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1812 if (g_strcmp0(method, "off") == 0)
1813 return CONNMAN_IPCONFIG_METHOD_OFF;
1814 else if (g_strcmp0(method, "fixed") == 0)
1815 return CONNMAN_IPCONFIG_METHOD_FIXED;
1816 else if (g_strcmp0(method, "manual") == 0)
1817 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1818 else if (g_strcmp0(method, "dhcp") == 0)
1819 return CONNMAN_IPCONFIG_METHOD_DHCP;
1820 else if (g_strcmp0(method, "auto") == 0)
1821 return CONNMAN_IPCONFIG_METHOD_AUTO;
1823 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1826 static const char *privacy2string(int privacy)
1830 else if (privacy == 1)
1836 static int string2privacy(const char *privacy)
1838 if (g_strcmp0(privacy, "disabled") == 0)
1840 else if (g_strcmp0(privacy, "enabled") == 0)
1842 else if (g_strcmp0(privacy, "preferred") == 0)
1844 else if (g_strcmp0(privacy, "prefered") == 0)
1850 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1852 struct connman_ipdevice *ipdevice;
1858 ipdevice = g_hash_table_lookup(ipdevice_hash,
1859 GINT_TO_POINTER(ipconfig->index));
1863 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1864 ipdevice->ipv6_privacy));
1869 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1877 privacy = string2privacy(value);
1879 ipconfig->ipv6_privacy_config = privacy;
1881 enable_ipv6(ipconfig);
1886 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1887 DBusMessageIter *iter)
1889 struct connman_ipaddress *append_addr = NULL;
1892 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1895 str = __connman_ipconfig_method2string(ipconfig->method);
1899 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1901 switch (ipconfig->method) {
1902 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1903 case CONNMAN_IPCONFIG_METHOD_OFF:
1906 case CONNMAN_IPCONFIG_METHOD_FIXED:
1907 append_addr = ipconfig->address;
1910 case CONNMAN_IPCONFIG_METHOD_AUTO:
1911 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1912 case CONNMAN_IPCONFIG_METHOD_DHCP:
1913 append_addr = ipconfig->system;
1914 #if defined TIZEN_EXT
1915 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1916 if (append_addr && append_addr->local == NULL)
1917 append_addr = ipconfig->address;
1925 if (append_addr->local) {
1927 struct in_addr netmask;
1930 connman_dbus_dict_append_basic(iter, "Address",
1931 DBUS_TYPE_STRING, &append_addr->local);
1933 addr = 0xffffffff << (32 - append_addr->prefixlen);
1934 netmask.s_addr = htonl(addr);
1935 mask = inet_ntoa(netmask);
1936 connman_dbus_dict_append_basic(iter, "Netmask",
1937 DBUS_TYPE_STRING, &mask);
1940 if (append_addr->gateway)
1941 connman_dbus_dict_append_basic(iter, "Gateway",
1942 DBUS_TYPE_STRING, &append_addr->gateway);
1944 #if defined TIZEN_EXT
1945 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1947 server_ip = __connman_dhcp_get_server_address(ipconfig);
1949 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1950 DBUS_TYPE_STRING, &server_ip);
1953 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1954 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1959 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1960 DBusMessageIter *iter,
1961 struct connman_ipconfig *ipconfig_ipv4)
1963 struct connman_ipaddress *append_addr = NULL;
1964 const char *str, *privacy;
1966 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1969 str = __connman_ipconfig_method2string(ipconfig->method);
1973 if (ipconfig_ipv4 &&
1974 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1975 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1979 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1981 switch (ipconfig->method) {
1982 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1983 case CONNMAN_IPCONFIG_METHOD_OFF:
1986 case CONNMAN_IPCONFIG_METHOD_FIXED:
1987 append_addr = ipconfig->address;
1990 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1991 case CONNMAN_IPCONFIG_METHOD_DHCP:
1992 case CONNMAN_IPCONFIG_METHOD_AUTO:
1993 append_addr = ipconfig->system;
1994 #if defined TIZEN_EXT
1995 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1996 if (append_addr && append_addr->local == NULL)
1997 append_addr = ipconfig->address;
2005 if (append_addr->local) {
2006 connman_dbus_dict_append_basic(iter, "Address",
2007 DBUS_TYPE_STRING, &append_addr->local);
2008 connman_dbus_dict_append_basic(iter, "PrefixLength",
2010 &append_addr->prefixlen);
2013 if (append_addr->gateway)
2014 connman_dbus_dict_append_basic(iter, "Gateway",
2015 DBUS_TYPE_STRING, &append_addr->gateway);
2017 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2018 connman_dbus_dict_append_basic(iter, "Privacy",
2019 DBUS_TYPE_STRING, &privacy);
2022 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2023 DBusMessageIter *iter)
2025 const char *str, *privacy;
2027 str = __connman_ipconfig_method2string(ipconfig->method);
2031 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2033 switch (ipconfig->method) {
2034 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2035 case CONNMAN_IPCONFIG_METHOD_OFF:
2036 case CONNMAN_IPCONFIG_METHOD_DHCP:
2038 case CONNMAN_IPCONFIG_METHOD_FIXED:
2039 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2040 case CONNMAN_IPCONFIG_METHOD_AUTO:
2044 if (!ipconfig->address)
2047 if (ipconfig->address->local) {
2048 connman_dbus_dict_append_basic(iter, "Address",
2049 DBUS_TYPE_STRING, &ipconfig->address->local);
2050 connman_dbus_dict_append_basic(iter, "PrefixLength",
2052 &ipconfig->address->prefixlen);
2055 if (ipconfig->address->gateway)
2056 connman_dbus_dict_append_basic(iter, "Gateway",
2057 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2059 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2060 connman_dbus_dict_append_basic(iter, "Privacy",
2061 DBUS_TYPE_STRING, &privacy);
2064 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2065 DBusMessageIter *iter)
2069 str = __connman_ipconfig_method2string(ipconfig->method);
2073 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2075 switch (ipconfig->method) {
2076 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2077 case CONNMAN_IPCONFIG_METHOD_OFF:
2078 case CONNMAN_IPCONFIG_METHOD_DHCP:
2079 case CONNMAN_IPCONFIG_METHOD_AUTO:
2081 case CONNMAN_IPCONFIG_METHOD_FIXED:
2082 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2086 if (!ipconfig->address)
2089 if (ipconfig->address->local) {
2091 struct in_addr netmask;
2094 connman_dbus_dict_append_basic(iter, "Address",
2095 DBUS_TYPE_STRING, &ipconfig->address->local);
2097 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2098 netmask.s_addr = htonl(addr);
2099 mask = inet_ntoa(netmask);
2100 connman_dbus_dict_append_basic(iter, "Netmask",
2101 DBUS_TYPE_STRING, &mask);
2104 if (ipconfig->address->gateway)
2105 connman_dbus_dict_append_basic(iter, "Gateway",
2106 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2109 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2110 DBusMessageIter *array)
2112 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2113 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2114 *privacy_string = NULL;
2115 int prefix_length = 0, privacy = 0;
2116 DBusMessageIter dict;
2119 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2122 dbus_message_iter_recurse(array, &dict);
2124 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2125 DBusMessageIter entry, value;
2129 dbus_message_iter_recurse(&dict, &entry);
2131 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2134 dbus_message_iter_get_basic(&entry, &key);
2135 dbus_message_iter_next(&entry);
2137 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2140 dbus_message_iter_recurse(&entry, &value);
2142 type = dbus_message_iter_get_arg_type(&value);
2144 if (g_str_equal(key, "Method")) {
2147 if (type != DBUS_TYPE_STRING)
2150 dbus_message_iter_get_basic(&value, &str);
2151 method = __connman_ipconfig_string2method(str);
2152 } else if (g_str_equal(key, "Address")) {
2153 if (type != DBUS_TYPE_STRING)
2156 dbus_message_iter_get_basic(&value, &address);
2157 } else if (g_str_equal(key, "PrefixLength")) {
2158 if (type != DBUS_TYPE_BYTE)
2161 dbus_message_iter_get_basic(&value, &prefix_length);
2163 if (prefix_length < 0 || prefix_length > 128)
2165 } else if (g_str_equal(key, "Netmask")) {
2166 if (type != DBUS_TYPE_STRING)
2169 dbus_message_iter_get_basic(&value, &netmask);
2170 } else if (g_str_equal(key, "Gateway")) {
2171 if (type != DBUS_TYPE_STRING)
2174 dbus_message_iter_get_basic(&value, &gateway);
2175 } else if (g_str_equal(key, "Privacy")) {
2176 if (type != DBUS_TYPE_STRING)
2179 dbus_message_iter_get_basic(&value, &privacy_string);
2180 privacy = string2privacy(privacy_string);
2183 dbus_message_iter_next(&dict);
2186 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2188 method, address, netmask, gateway, prefix_length,
2192 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2193 case CONNMAN_IPCONFIG_METHOD_FIXED:
2196 case CONNMAN_IPCONFIG_METHOD_OFF:
2197 ipconfig->method = method;
2198 #if defined TIZEN_EXT
2199 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2200 disable_ipv6(ipconfig);
2205 case CONNMAN_IPCONFIG_METHOD_AUTO:
2206 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2209 ipconfig->method = method;
2211 ipconfig->ipv6_privacy_config = privacy;
2212 #if defined TIZEN_EXT
2213 enable_ipv6(ipconfig);
2218 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2219 switch (ipconfig->type) {
2220 case CONNMAN_IPCONFIG_TYPE_IPV4:
2223 case CONNMAN_IPCONFIG_TYPE_IPV6:
2226 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2227 case CONNMAN_IPCONFIG_TYPE_ALL:
2232 if ((address && connman_inet_check_ipaddress(address)
2235 connman_inet_check_ipaddress(netmask)
2238 connman_inet_check_ipaddress(gateway)
2242 ipconfig->method = method;
2244 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2245 connman_ipaddress_set_ipv4(ipconfig->address,
2246 address, netmask, gateway);
2248 return connman_ipaddress_set_ipv6(
2249 ipconfig->address, address,
2250 prefix_length, gateway);
2254 case CONNMAN_IPCONFIG_METHOD_DHCP:
2255 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2258 ipconfig->method = method;
2265 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2266 DBusMessageIter *iter)
2268 struct connman_ipdevice *ipdevice;
2269 const char *method = "auto";
2271 connman_dbus_dict_append_basic(iter, "Method",
2272 DBUS_TYPE_STRING, &method);
2274 ipdevice = g_hash_table_lookup(ipdevice_hash,
2275 GINT_TO_POINTER(ipconfig->index));
2279 if (ipconfig->index >= 0) {
2280 char *ifname = connman_inet_ifname(ipconfig->index);
2282 connman_dbus_dict_append_basic(iter, "Interface",
2283 DBUS_TYPE_STRING, &ifname);
2288 if (ipdevice->address)
2289 connman_dbus_dict_append_basic(iter, "Address",
2290 DBUS_TYPE_STRING, &ipdevice->address);
2292 if (ipdevice->mtu > 0)
2293 connman_dbus_dict_append_basic(iter, "MTU",
2294 DBUS_TYPE_UINT16, &ipdevice->mtu);
2297 void __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2298 GKeyFile *keyfile, const char *identifier, const char *prefix)
2302 struct ipconfig_store is = { .file = keyfile,
2303 .group = identifier,
2306 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2308 method = store_get_str(&is, "method");
2310 switch (ipconfig->type) {
2311 case CONNMAN_IPCONFIG_TYPE_IPV4:
2312 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2315 case CONNMAN_IPCONFIG_TYPE_IPV6:
2316 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2319 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2320 case CONNMAN_IPCONFIG_TYPE_ALL:
2321 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2325 ipconfig->method = __connman_ipconfig_string2method(method);
2329 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2330 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2332 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2333 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2334 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2337 privacy = store_get_str(&is, "privacy");
2338 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2342 g_strfreev(ipconfig->last_dhcpv6_prefixes);
2343 ipconfig->last_dhcpv6_prefixes =
2344 store_get_strs(&is, "DHCP.LastPrefixes");
2348 switch (ipconfig->method) {
2349 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2350 case CONNMAN_IPCONFIG_METHOD_OFF:
2353 case CONNMAN_IPCONFIG_METHOD_FIXED:
2354 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2355 ipconfig->address->prefixlen =
2356 store_get_int(&is, "netmask_prefixlen");
2358 g_free(ipconfig->address->local);
2359 ipconfig->address->local =
2360 store_get_str(&is, "local_address");
2362 g_free(ipconfig->address->peer);
2363 ipconfig->address->peer =
2364 store_get_str(&is, "peer_address");
2366 g_free(ipconfig->address->broadcast);
2367 ipconfig->address->broadcast =
2368 store_get_str(&is, "broadcast_address");
2370 g_free(ipconfig->address->gateway);
2371 ipconfig->address->gateway =
2372 store_get_str(&is, "gateway");
2375 case CONNMAN_IPCONFIG_METHOD_AUTO:
2376 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2380 * If the last used method for IPv4 was AUTO then we
2381 * try first DHCP. We will try also to use the last
2382 * used DHCP address, if exits.
2384 __connman_ipconfig_set_method(ipconfig,
2385 CONNMAN_IPCONFIG_METHOD_DHCP);
2388 case CONNMAN_IPCONFIG_METHOD_DHCP:
2389 str = store_get_str(&is, "DHCP.LastAddress");
2391 g_free(ipconfig->last_dhcp_address);
2392 ipconfig->last_dhcp_address = str;
2399 void __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2400 GKeyFile *keyfile, const char *identifier, const char *prefix)
2403 struct ipconfig_store is = { .file = keyfile,
2404 .group = identifier,
2407 method = __connman_ipconfig_method2string(ipconfig->method);
2408 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2410 store_set_str(&is, "method", method);
2412 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2413 store_set_str(&is, "privacy",
2414 privacy2string(ipconfig->ipv6_privacy_config));
2416 store_set_str(&is, "DHCP.LastAddress",
2417 ipconfig->last_dhcp_address);
2419 store_set_strs(&is, "DHCP.LastPrefixes",
2420 ipconfig->last_dhcpv6_prefixes);
2423 switch (ipconfig->method) {
2424 case CONNMAN_IPCONFIG_METHOD_FIXED:
2425 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2428 case CONNMAN_IPCONFIG_METHOD_DHCP:
2429 store_set_str(&is, "DHCP.LastAddress",
2430 ipconfig->last_dhcp_address);
2433 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2434 case CONNMAN_IPCONFIG_METHOD_OFF:
2435 case CONNMAN_IPCONFIG_METHOD_AUTO:
2439 store_set_int(&is, "netmask_prefixlen", ipconfig->address->prefixlen);
2440 store_set_str(&is, "local_address", ipconfig->address->local);
2441 store_set_str(&is, "peer_address", ipconfig->address->peer);
2442 store_set_str(&is, "broadcast_address", ipconfig->address->broadcast);
2443 store_set_str(&is, "gateway", ipconfig->address->gateway);
2446 int __connman_ipconfig_init(void)
2450 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2451 NULL, free_ipdevice);
2453 is_ipv6_supported = connman_inet_is_ipv6_supported();
2458 void __connman_ipconfig_cleanup(void)
2462 g_hash_table_destroy(ipdevice_hash);
2463 ipdevice_hash = NULL;