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 #define PROC_IPV4_CONF_PREFIX "/proc/sys/net/ipv4/conf"
266 #define PROC_IPV6_CONF_PREFIX "/proc/sys/net/ipv6/conf"
268 static int read_conf_value(const char *prefix, const char *ifname,
269 const char *suffix, int *value)
275 path = g_build_filename(prefix, ifname ? ifname : "all", suffix, NULL);
280 f = fopen(path, "r");
284 errno = 0; /* Avoid stale errno values with fscanf */
286 err = fscanf(f, "%d", value);
287 if (err <= 0 && errno)
294 connman_error("failed to read %s", path);
301 static int read_ipv4_conf_value(const char *ifname, const char *suffix,
304 return read_conf_value(PROC_IPV4_CONF_PREFIX, ifname, suffix, value);
307 static int read_ipv6_conf_value(const char *ifname, const char *suffix,
310 return read_conf_value(PROC_IPV6_CONF_PREFIX, ifname, suffix, value);
313 static int write_conf_value(const char *prefix, const char *ifname,
314 const char *suffix, int value) {
319 path = g_build_filename(prefix, ifname ? ifname : "all", suffix, NULL);
323 f = fopen(path, "r+");
327 rval = fprintf(f, "%d", value);
332 connman_error("failed to set %s value %d", path, value);
339 static int write_ipv4_conf_value(const char *ifname, const char *suffix,
342 return write_conf_value(PROC_IPV4_CONF_PREFIX, ifname, suffix, value);
345 static int write_ipv6_conf_value(const char *ifname, const char *suffix,
348 return write_conf_value(PROC_IPV6_CONF_PREFIX, ifname, suffix, value);
351 static bool get_ipv6_state(gchar *ifname)
354 bool enabled = false;
356 if (read_ipv6_conf_value(ifname, "disable_ipv6", &disabled) > 0)
362 static int set_ipv6_state(gchar *ifname, bool enable)
364 int disabled = enable ? 0 : 1;
366 DBG("%s %d", ifname, disabled);
368 return write_ipv6_conf_value(ifname, "disable_ipv6", disabled);
371 static int get_ipv6_privacy(gchar *ifname)
378 if (read_ipv6_conf_value(ifname, "use_tempaddr", &value) < 0)
384 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
385 * The privacy extension is described in RFC 3041 and RFC 4941
387 static int set_ipv6_privacy(gchar *ifname, int value)
395 return write_ipv6_conf_value(ifname, "use_tempaddr", value);
398 static int get_rp_filter(void)
402 if (read_ipv4_conf_value(NULL, "rp_filter", &value) < 0)
408 static int set_rp_filter(int value)
410 /* 0 = no validation, 1 = strict mode, 2 = loose mode */
423 return write_ipv4_conf_value(NULL, "rp_filter", value);
426 int __connman_ipconfig_set_rp_filter()
430 value = get_rp_filter();
437 connman_info("rp_filter set to 2 (loose mode routing), "
438 "old value was %d", value);
443 void __connman_ipconfig_unset_rp_filter(int old_value)
445 set_rp_filter(old_value);
447 connman_info("rp_filter restored to %d", old_value);
450 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
455 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
458 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
460 struct connman_ipdevice *ipdevice;
467 ipdevice = g_hash_table_lookup(ipdevice_hash,
468 GINT_TO_POINTER(ipconfig->index));
472 ifname = connman_inet_ifname(ipconfig->index);
473 ret = get_ipv6_state(ifname);
479 static void free_ipdevice(gpointer data)
481 struct connman_ipdevice *ipdevice = data;
482 char *ifname = connman_inet_ifname(ipdevice->index);
484 connman_info("%s {remove} index %d", ifname, ipdevice->index);
486 if (ipdevice->config_ipv4) {
487 __connman_ipconfig_unref(ipdevice->config_ipv4);
488 ipdevice->config_ipv4 = NULL;
491 if (ipdevice->config_ipv6) {
492 __connman_ipconfig_unref(ipdevice->config_ipv6);
493 ipdevice->config_ipv6 = NULL;
496 free_address_list(ipdevice);
497 g_free(ipdevice->ipv4_gateway);
498 g_free(ipdevice->ipv6_gateway);
499 g_free(ipdevice->pac);
501 g_free(ipdevice->address);
504 set_ipv6_state(ifname, ipdevice->ipv6_enabled);
505 set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
512 static void update_stats(struct connman_ipdevice *ipdevice,
513 const char *ifname, struct rtnl_link_stats *stats)
515 struct connman_service *service;
517 if (stats->rx_packets == 0 && stats->tx_packets == 0)
520 connman_info("%s {RX} %u packets %u bytes", ifname,
521 stats->rx_packets, stats->rx_bytes);
522 connman_info("%s {TX} %u packets %u bytes", ifname,
523 stats->tx_packets, stats->tx_bytes);
525 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
528 service = __connman_service_lookup_from_index(ipdevice->index);
530 DBG("service %p", service);
535 ipdevice->rx_packets = stats->rx_packets;
536 ipdevice->tx_packets = stats->tx_packets;
537 ipdevice->rx_bytes = stats->rx_bytes;
538 ipdevice->tx_bytes = stats->tx_bytes;
539 ipdevice->rx_errors = stats->rx_errors;
540 ipdevice->tx_errors = stats->tx_errors;
541 ipdevice->rx_dropped = stats->rx_dropped;
542 ipdevice->tx_dropped = stats->tx_dropped;
544 __connman_service_notify(service,
545 ipdevice->rx_packets, ipdevice->tx_packets,
546 ipdevice->rx_bytes, ipdevice->tx_bytes,
547 ipdevice->rx_errors, ipdevice->tx_errors,
548 ipdevice->rx_dropped, ipdevice->tx_dropped);
551 void __connman_ipconfig_newlink(int index, unsigned short type,
552 unsigned int flags, const char *address,
554 struct rtnl_link_stats *stats)
556 struct connman_ipdevice *ipdevice;
557 GList *list, *ipconfig_copy;
559 bool up = false, down = false;
560 bool lower_up = false, lower_down = false;
563 DBG("index %d", index);
565 if (type == ARPHRD_LOOPBACK)
568 ifname = connman_inet_ifname(index);
570 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
574 ipdevice = g_try_new0(struct connman_ipdevice, 1);
578 ipdevice->index = index;
579 ipdevice->type = type;
581 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
582 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
584 ipdevice->address = g_strdup(address);
586 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
588 connman_info("%s {create} index %d type %d <%s>", ifname,
589 index, type, type2str(type));
592 #if defined TIZEN_EXT
593 if (g_strcmp0(ipdevice->address, address) != 0) {
594 /* If an original address is built-in physical device,
595 * it's hardly get an address at a initial creation
597 g_free(ipdevice->address);
598 ipdevice->address = g_strdup(address);
604 update_stats(ipdevice, ifname, stats);
606 if (flags == ipdevice->flags)
609 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
616 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
617 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
618 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
619 (IFF_RUNNING | IFF_LOWER_UP))
621 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
625 ipdevice->flags = flags;
627 str = g_string_new(NULL);
632 g_string_append(str, "UP");
634 g_string_append(str, "DOWN");
636 if (flags & IFF_RUNNING)
637 g_string_append(str, ",RUNNING");
639 if (flags & IFF_LOWER_UP)
640 g_string_append(str, ",LOWER_UP");
642 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
644 g_string_free(str, TRUE);
646 ipconfig_copy = g_list_copy(ipconfig_list);
648 for (list = g_list_first(ipconfig_copy); list;
649 list = g_list_next(list)) {
650 struct connman_ipconfig *ipconfig = list->data;
652 if (index != ipconfig->index)
658 if (up && ipconfig->ops->up)
659 ipconfig->ops->up(ipconfig, ifname);
660 if (lower_up && ipconfig->ops->lower_up)
661 ipconfig->ops->lower_up(ipconfig, ifname);
663 if (lower_down && ipconfig->ops->lower_down)
664 ipconfig->ops->lower_down(ipconfig, ifname);
665 if (down && ipconfig->ops->down)
666 ipconfig->ops->down(ipconfig, ifname);
669 g_list_free(ipconfig_copy);
675 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
677 struct connman_ipdevice *ipdevice;
681 DBG("index %d", index);
683 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
687 ifname = connman_inet_ifname(index);
689 update_stats(ipdevice, ifname, stats);
691 for (list = g_list_first(ipconfig_list); list;
692 list = g_list_next(list)) {
693 struct connman_ipconfig *ipconfig = list->data;
695 if (index != ipconfig->index)
698 ipconfig->index = -1;
703 if (ipconfig->ops->lower_down)
704 ipconfig->ops->lower_down(ipconfig, ifname);
705 if (ipconfig->ops->down)
706 ipconfig->ops->down(ipconfig, ifname);
711 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
714 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
716 const struct connman_ipaddress *addr1 = a;
717 const struct connman_ipaddress *addr2 = b;
719 if (addr1->prefixlen != addr2->prefixlen)
720 return addr2->prefixlen - addr1->prefixlen;
722 return g_strcmp0(addr1->local, addr2->local);
725 static bool is_index_p2p_service(int index)
727 struct connman_service *service;
728 enum connman_service_type type;
730 service = __connman_service_lookup_from_index(index);
734 type = connman_service_get_type(service);
736 case CONNMAN_SERVICE_TYPE_P2P:
737 case CONNMAN_SERVICE_TYPE_VPN:
744 int __connman_ipconfig_newaddr(int index, int family, const char *label,
745 unsigned char prefixlen, const char *address)
747 struct connman_ipdevice *ipdevice;
748 struct connman_ipaddress *ipaddress;
749 enum connman_ipconfig_type type;
753 DBG("index %d", index);
755 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
759 ipaddress = connman_ipaddress_alloc(family);
763 ipaddress->prefixlen = prefixlen;
764 ipaddress->local = g_strdup(address);
766 if (is_index_p2p_service(index))
767 connman_ipaddress_set_p2p(ipaddress, true);
769 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
770 check_duplicate_address)) {
771 connman_ipaddress_free(ipaddress);
775 if (family == AF_INET)
776 type = CONNMAN_IPCONFIG_TYPE_IPV4;
777 else if (family == AF_INET6)
778 type = CONNMAN_IPCONFIG_TYPE_IPV6;
782 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
785 ifname = connman_inet_ifname(index);
786 connman_info("%s {add} address %s/%u label %s family %d",
787 ifname, address, prefixlen, label, family);
789 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
790 __connman_ippool_newaddr(index, address, prefixlen);
792 if (ipdevice->config_ipv4 && family == AF_INET)
793 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
796 else if (ipdevice->config_ipv6 && family == AF_INET6)
797 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
802 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
805 for (list = g_list_first(ipconfig_list); list;
806 list = g_list_next(list)) {
807 struct connman_ipconfig *ipconfig = list->data;
809 if (index != ipconfig->index)
812 if (type != ipconfig->type)
818 if (ipconfig->ops->ip_bound)
819 ipconfig->ops->ip_bound(ipconfig, ifname);
827 void __connman_ipconfig_deladdr(int index, int family, const char *label,
828 unsigned char prefixlen, const char *address)
830 struct connman_ipdevice *ipdevice;
831 struct connman_ipaddress *ipaddress;
832 enum connman_ipconfig_type type;
836 DBG("index %d", index);
838 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
842 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
846 if (family == AF_INET)
847 type = CONNMAN_IPCONFIG_TYPE_IPV4;
848 else if (family == AF_INET6)
849 type = CONNMAN_IPCONFIG_TYPE_IPV6;
853 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
856 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
857 __connman_ippool_deladdr(index, address, prefixlen);
859 connman_ipaddress_clear(ipaddress);
862 ifname = connman_inet_ifname(index);
863 connman_info("%s {del} address %s/%u label %s", ifname,
864 address, prefixlen, label);
866 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
869 if (g_slist_length(ipdevice->address_list) > 0)
872 for (list = g_list_first(ipconfig_list); list;
873 list = g_list_next(list)) {
874 struct connman_ipconfig *ipconfig = list->data;
876 if (index != ipconfig->index)
879 if (type != ipconfig->type)
885 if (ipconfig->ops->ip_release)
886 ipconfig->ops->ip_release(ipconfig, ifname);
893 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
894 const char *dst, const char *gateway)
896 struct connman_ipdevice *ipdevice;
899 DBG("index %d", index);
901 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
905 ifname = connman_inet_ifname(index);
907 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
908 g_strcmp0(dst, "::") == 0)) {
910 enum connman_ipconfig_type type;
912 if (family == AF_INET6) {
913 type = CONNMAN_IPCONFIG_TYPE_IPV6;
914 g_free(ipdevice->ipv6_gateway);
915 ipdevice->ipv6_gateway = g_strdup(gateway);
917 if (ipdevice->config_ipv6 &&
918 ipdevice->config_ipv6->system) {
919 g_free(ipdevice->config_ipv6->system->gateway);
920 ipdevice->config_ipv6->system->gateway =
923 } else if (family == AF_INET) {
924 type = CONNMAN_IPCONFIG_TYPE_IPV4;
925 g_free(ipdevice->ipv4_gateway);
926 ipdevice->ipv4_gateway = g_strdup(gateway);
928 if (ipdevice->config_ipv4 &&
929 ipdevice->config_ipv4->system) {
930 g_free(ipdevice->config_ipv4->system->gateway);
931 ipdevice->config_ipv4->system->gateway =
937 for (config_list = g_list_first(ipconfig_list); config_list;
938 config_list = g_list_next(config_list)) {
939 struct connman_ipconfig *ipconfig = config_list->data;
941 if (index != ipconfig->index)
944 if (type != ipconfig->type)
950 if (ipconfig->ops->route_set)
951 ipconfig->ops->route_set(ipconfig, ifname);
955 connman_info("%s {add} route %s gw %s scope %u <%s>",
956 ifname, dst, gateway, scope, scope2str(scope));
962 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
963 const char *dst, const char *gateway)
965 struct connman_ipdevice *ipdevice;
968 DBG("index %d", index);
970 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
974 ifname = connman_inet_ifname(index);
976 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
977 g_strcmp0(dst, "::") == 0)) {
979 enum connman_ipconfig_type type;
981 if (family == AF_INET6) {
982 type = CONNMAN_IPCONFIG_TYPE_IPV6;
983 g_free(ipdevice->ipv6_gateway);
984 ipdevice->ipv6_gateway = NULL;
986 if (ipdevice->config_ipv6 &&
987 ipdevice->config_ipv6->system) {
988 g_free(ipdevice->config_ipv6->system->gateway);
989 ipdevice->config_ipv6->system->gateway = NULL;
991 } else if (family == AF_INET) {
992 type = CONNMAN_IPCONFIG_TYPE_IPV4;
993 g_free(ipdevice->ipv4_gateway);
994 ipdevice->ipv4_gateway = NULL;
996 if (ipdevice->config_ipv4 &&
997 ipdevice->config_ipv4->system) {
998 g_free(ipdevice->config_ipv4->system->gateway);
999 ipdevice->config_ipv4->system->gateway = NULL;
1004 for (config_list = g_list_first(ipconfig_list); config_list;
1005 config_list = g_list_next(config_list)) {
1006 struct connman_ipconfig *ipconfig = config_list->data;
1008 if (index != ipconfig->index)
1011 if (type != ipconfig->type)
1017 if (ipconfig->ops->route_unset)
1018 ipconfig->ops->route_unset(ipconfig, ifname);
1022 connman_info("%s {del} route %s gw %s scope %u <%s>",
1023 ifname, dst, gateway, scope, scope2str(scope));
1029 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1034 keys = g_hash_table_get_keys(ipdevice_hash);
1038 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1039 int index = GPOINTER_TO_INT(list->data);
1041 function(index, user_data);
1047 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1048 struct connman_ipconfig *ipconfig)
1050 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1053 unsigned short __connman_ipconfig_get_type_from_index(int index)
1055 struct connman_ipdevice *ipdevice;
1057 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1061 return ipdevice->type;
1064 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1066 struct connman_ipdevice *ipdevice;
1068 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1072 return ipdevice->flags;
1075 const char *__connman_ipconfig_get_gateway_from_index(int index,
1076 enum connman_ipconfig_type type)
1078 struct connman_ipdevice *ipdevice;
1080 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1084 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1085 if (ipdevice->ipv4_gateway)
1086 return ipdevice->ipv4_gateway;
1088 if (ipdevice->config_ipv4 &&
1089 ipdevice->config_ipv4->address)
1090 return ipdevice->config_ipv4->address->gateway;
1093 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1094 if (ipdevice->ipv6_gateway)
1095 return ipdevice->ipv6_gateway;
1097 if (ipdevice->config_ipv6 &&
1098 ipdevice->config_ipv6->address)
1099 return ipdevice->config_ipv6->address->gateway;
1105 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1107 ipconfig->index = index;
1110 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1112 if (!ipconfig->address)
1115 return ipconfig->address->local;
1118 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1119 const char *address)
1121 if (!ipconfig->address)
1124 g_free(ipconfig->address->local);
1125 ipconfig->address->local = g_strdup(address);
1128 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1130 if (!ipconfig->address)
1133 return ipconfig->address->peer;
1136 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1137 const char *address)
1139 if (!ipconfig->address)
1142 g_free(ipconfig->address->peer);
1143 ipconfig->address->peer = g_strdup(address);
1146 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1148 if (!ipconfig->address)
1151 return ipconfig->address->broadcast;
1154 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1155 const char *broadcast)
1157 if (!ipconfig->address)
1160 g_free(ipconfig->address->broadcast);
1161 ipconfig->address->broadcast = g_strdup(broadcast);
1164 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1166 if (!ipconfig->address)
1169 return ipconfig->address->gateway;
1172 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1173 const char *gateway)
1177 if (!ipconfig->address)
1179 g_free(ipconfig->address->gateway);
1180 ipconfig->address->gateway = g_strdup(gateway);
1183 #if defined TIZEN_EXT
1184 void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig,
1185 int dhcp_lease_duration)
1187 ipconfig->dhcp_lease_duration = dhcp_lease_duration;
1191 #if defined TIZEN_EXT
1192 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1194 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1197 #if !defined TIZEN_EXT
1198 struct connman_service *service;
1203 if (!ipconfig->address)
1206 #if !defined TIZEN_EXT
1207 service = __connman_service_lookup_from_index(ipconfig->index);
1212 DBG("type %d gw %s peer %s", ipconfig->type,
1213 ipconfig->address->gateway, ipconfig->address->peer);
1215 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1216 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1217 return __connman_connection_gateway_add(service,
1218 ipconfig->address->gateway,
1220 ipconfig->address->peer);
1225 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1227 struct connman_service *service;
1231 service = __connman_service_lookup_from_index(ipconfig->index);
1233 __connman_connection_gateway_remove(service, ipconfig->type);
1236 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1238 if (!ipconfig->address)
1241 return ipconfig->address->prefixlen;
1244 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1245 unsigned char prefixlen)
1247 if (!ipconfig->address)
1250 ipconfig->address->prefixlen = prefixlen;
1253 static void ipconfig_set_p2p(int index, struct connman_ipconfig *ipconfig)
1255 if (!is_index_p2p_service(index))
1258 connman_ipaddress_set_p2p(ipconfig->address, true);
1259 connman_ipaddress_set_p2p(ipconfig->system, true);
1262 static struct connman_ipconfig *create_ipv6config(int index)
1264 struct connman_ipconfig *ipv6config;
1265 struct connman_ipdevice *ipdevice;
1267 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1271 ipv6config->refcount = 1;
1273 ipv6config->index = index;
1274 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1276 if (!is_ipv6_supported)
1277 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1279 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1281 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1283 #if !defined TIZEN_EXT
1284 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1286 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy = 2;
1289 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1290 if (!ipv6config->address) {
1295 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1297 ipconfig_set_p2p(index, ipv6config);
1299 DBG("ipconfig %p index %d method %s", ipv6config, index,
1300 __connman_ipconfig_method2string(ipv6config->method));
1306 * connman_ipconfig_create:
1308 * Allocate a new ipconfig structure.
1310 * Returns: a newly-allocated #connman_ipconfig structure
1312 struct connman_ipconfig *__connman_ipconfig_create(int index,
1313 enum connman_ipconfig_type type)
1315 struct connman_ipconfig *ipconfig;
1317 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1318 return create_ipv6config(index);
1320 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1324 ipconfig->refcount = 1;
1326 ipconfig->index = index;
1327 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1329 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1330 if (!ipconfig->address) {
1335 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1337 ipconfig_set_p2p(index, ipconfig);
1339 #if defined TIZEN_EXT
1340 if (!simplified_log)
1342 DBG("ipconfig %p index %d", ipconfig, index);
1349 * connman_ipconfig_ref:
1350 * @ipconfig: ipconfig structure
1352 * Increase reference counter of ipconfig
1354 struct connman_ipconfig *
1355 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1356 const char *file, int line, const char *caller)
1358 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1359 file, line, caller);
1361 __sync_fetch_and_add(&ipconfig->refcount, 1);
1367 * connman_ipconfig_unref:
1368 * @ipconfig: ipconfig structure
1370 * Decrease reference counter of ipconfig
1372 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1373 const char *file, int line, const char *caller)
1377 #if defined TIZEN_EXT
1378 if (!simplified_log)
1380 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1381 file, line, caller);
1383 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1386 if (__connman_ipconfig_disable(ipconfig) < 0)
1387 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1389 __connman_ipconfig_set_ops(ipconfig, NULL);
1391 connman_ipaddress_free(ipconfig->system);
1392 connman_ipaddress_free(ipconfig->address);
1393 g_free(ipconfig->last_dhcp_address);
1394 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1399 * connman_ipconfig_get_data:
1400 * @ipconfig: ipconfig structure
1402 * Get private data pointer
1404 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1409 return ipconfig->ops_data;
1413 * connman_ipconfig_set_data:
1414 * @ipconfig: ipconfig structure
1415 * @data: data pointer
1417 * Set private data pointer
1419 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1421 ipconfig->ops_data = data;
1425 * connman_ipconfig_get_index:
1426 * @ipconfig: ipconfig structure
1428 * Get interface index
1430 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1435 return ipconfig->index;
1439 * connman_ipconfig_set_ops:
1440 * @ipconfig: ipconfig structure
1441 * @ops: operation callbacks
1443 * Set the operation callbacks
1445 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1446 const struct connman_ipconfig_ops *ops)
1448 ipconfig->ops = ops;
1452 * connman_ipconfig_set_method:
1453 * @ipconfig: ipconfig structure
1454 * @method: configuration method
1456 * Set the configuration method
1458 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1459 enum connman_ipconfig_method method)
1461 ipconfig->method = method;
1466 enum connman_ipconfig_method __connman_ipconfig_get_method(
1467 struct connman_ipconfig *ipconfig)
1470 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1472 return ipconfig->method;
1475 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1477 switch (ipconfig->method) {
1478 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1479 case CONNMAN_IPCONFIG_METHOD_OFF:
1481 case CONNMAN_IPCONFIG_METHOD_AUTO:
1482 case CONNMAN_IPCONFIG_METHOD_FIXED:
1483 case CONNMAN_IPCONFIG_METHOD_DHCP:
1484 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1485 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1486 return connman_inet_set_address(ipconfig->index,
1488 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1489 return connman_inet_set_ipv6_address(
1490 ipconfig->index, ipconfig->address);
1496 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1503 switch (ipconfig->method) {
1504 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1505 case CONNMAN_IPCONFIG_METHOD_OFF:
1507 case CONNMAN_IPCONFIG_METHOD_AUTO:
1508 case CONNMAN_IPCONFIG_METHOD_DHCP:
1509 err = __connman_ipconfig_address_unset(ipconfig);
1510 connman_ipaddress_clear(ipconfig->address);
1513 case CONNMAN_IPCONFIG_METHOD_FIXED:
1514 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1515 return __connman_ipconfig_address_unset(ipconfig);
1521 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1528 #if defined TIZEN_EXT
1529 DBG("ipconfig method %d type %d", ipconfig->method, ipconfig->type);
1531 DBG("method %d", ipconfig->method);
1534 switch (ipconfig->method) {
1535 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1536 case CONNMAN_IPCONFIG_METHOD_OFF:
1538 case CONNMAN_IPCONFIG_METHOD_AUTO:
1539 case CONNMAN_IPCONFIG_METHOD_FIXED:
1540 case CONNMAN_IPCONFIG_METHOD_DHCP:
1541 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1542 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1543 err = connman_inet_clear_address(ipconfig->index,
1545 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1546 err = connman_inet_clear_ipv6_address(ipconfig->index,
1557 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1560 struct connman_ipdevice *ipdevice;
1562 if (!ipconfig || ipconfig->index < 0)
1565 ipdevice = g_hash_table_lookup(ipdevice_hash,
1566 GINT_TO_POINTER(ipconfig->index));
1570 g_free(ipdevice->pac);
1571 ipdevice->pac = g_strdup(url);
1576 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1578 struct connman_ipdevice *ipdevice;
1580 if (!ipconfig || ipconfig->index < 0)
1583 ipdevice = g_hash_table_lookup(ipdevice_hash,
1584 GINT_TO_POINTER(ipconfig->index));
1588 return ipdevice->pac;
1591 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1592 const char *address)
1597 g_free(ipconfig->last_dhcp_address);
1598 ipconfig->last_dhcp_address = g_strdup(address);
1601 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1606 return ipconfig->last_dhcp_address;
1609 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1615 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1616 ipconfig->last_dhcpv6_prefixes = prefixes;
1619 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1624 return ipconfig->last_dhcpv6_prefixes;
1627 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1629 struct connman_ipdevice *ipdevice;
1634 ipdevice = g_hash_table_lookup(ipdevice_hash,
1635 GINT_TO_POINTER(ipconfig->index));
1639 ifname = connman_inet_ifname(ipconfig->index);
1644 set_ipv6_state(ifname, false);
1649 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1651 struct connman_ipdevice *ipdevice;
1656 ipdevice = g_hash_table_lookup(ipdevice_hash,
1657 GINT_TO_POINTER(ipconfig->index));
1661 ifname = connman_inet_ifname(ipconfig->index);
1666 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1667 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1669 set_ipv6_state(ifname, true);
1674 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1676 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1679 enable_ipv6(ipconfig);
1682 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1684 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1687 disable_ipv6(ipconfig);
1690 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1695 switch (ipconfig->method) {
1696 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1697 case CONNMAN_IPCONFIG_METHOD_OFF:
1699 case CONNMAN_IPCONFIG_METHOD_AUTO:
1700 case CONNMAN_IPCONFIG_METHOD_FIXED:
1701 case CONNMAN_IPCONFIG_METHOD_DHCP:
1702 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1709 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1711 struct connman_ipdevice *ipdevice;
1712 bool up = false, down = false;
1713 bool lower_up = false, lower_down = false;
1714 enum connman_ipconfig_type type;
1717 DBG("ipconfig %p", ipconfig);
1719 if (!ipconfig || ipconfig->index < 0)
1722 ipdevice = g_hash_table_lookup(ipdevice_hash,
1723 GINT_TO_POINTER(ipconfig->index));
1727 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1728 if (ipdevice->config_ipv4 == ipconfig)
1730 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1731 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1732 if (ipdevice->config_ipv6 == ipconfig)
1734 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1738 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1739 ipdevice->config_ipv4) {
1740 ipconfig_list = g_list_remove(ipconfig_list,
1741 ipdevice->config_ipv4);
1743 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1745 __connman_ipconfig_unref(ipdevice->config_ipv4);
1747 g_free(ipdevice->ipv4_gateway);
1748 ipdevice->ipv4_gateway = NULL;
1751 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1752 ipdevice->config_ipv6) {
1753 ipconfig_list = g_list_remove(ipconfig_list,
1754 ipdevice->config_ipv6);
1756 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1758 __connman_ipconfig_unref(ipdevice->config_ipv6);
1760 g_free(ipdevice->ipv6_gateway);
1761 ipdevice->ipv6_gateway = NULL;
1764 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1765 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1766 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1767 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1769 enable_ipv6(ipdevice->config_ipv6);
1771 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1773 if (ipdevice->flags & IFF_UP)
1778 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1779 (IFF_RUNNING | IFF_LOWER_UP))
1781 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1784 ifname = connman_inet_ifname(ipconfig->index);
1786 if (up && ipconfig->ops->up)
1787 ipconfig->ops->up(ipconfig, ifname);
1788 if (lower_up && ipconfig->ops->lower_up)
1789 ipconfig->ops->lower_up(ipconfig, ifname);
1791 if (lower_down && ipconfig->ops->lower_down)
1792 ipconfig->ops->lower_down(ipconfig, ifname);
1793 if (down && ipconfig->ops->down)
1794 ipconfig->ops->down(ipconfig, ifname);
1801 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1803 struct connman_ipdevice *ipdevice;
1804 #if defined TIZEN_EXT
1805 if (!simplified_log)
1807 DBG("ipconfig %p", ipconfig);
1809 if (!ipconfig || ipconfig->index < 0)
1812 ipdevice = g_hash_table_lookup(ipdevice_hash,
1813 GINT_TO_POINTER(ipconfig->index));
1817 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1820 if (ipdevice->config_ipv4 == ipconfig) {
1821 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1823 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1824 __connman_ipconfig_unref(ipdevice->config_ipv4);
1825 ipdevice->config_ipv4 = NULL;
1827 g_free(ipdevice->ipv4_gateway);
1828 ipdevice->ipv4_gateway = NULL;
1833 if (ipdevice->config_ipv6 == ipconfig) {
1834 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1836 #if defined TIZEN_EXT
1837 if (ipdevice->config_ipv6->method ==
1838 CONNMAN_IPCONFIG_METHOD_AUTO) {
1839 disable_ipv6(ipdevice->config_ipv6);
1840 enable_ipv6(ipdevice->config_ipv6);
1843 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1844 __connman_ipconfig_unref(ipdevice->config_ipv6);
1845 ipdevice->config_ipv6 = NULL;
1847 g_free(ipdevice->ipv6_gateway);
1848 ipdevice->ipv6_gateway = NULL;
1856 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1859 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1861 case CONNMAN_IPCONFIG_METHOD_OFF:
1863 case CONNMAN_IPCONFIG_METHOD_FIXED:
1865 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1867 case CONNMAN_IPCONFIG_METHOD_DHCP:
1869 case CONNMAN_IPCONFIG_METHOD_AUTO:
1876 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1878 if (g_strcmp0(method, "off") == 0)
1879 return CONNMAN_IPCONFIG_METHOD_OFF;
1880 else if (g_strcmp0(method, "fixed") == 0)
1881 return CONNMAN_IPCONFIG_METHOD_FIXED;
1882 else if (g_strcmp0(method, "manual") == 0)
1883 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1884 else if (g_strcmp0(method, "dhcp") == 0)
1885 return CONNMAN_IPCONFIG_METHOD_DHCP;
1886 else if (g_strcmp0(method, "auto") == 0)
1887 return CONNMAN_IPCONFIG_METHOD_AUTO;
1889 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1892 static const char *privacy2string(int privacy)
1896 else if (privacy == 1)
1902 static int string2privacy(const char *privacy)
1904 if (g_strcmp0(privacy, "disabled") == 0)
1906 else if (g_strcmp0(privacy, "enabled") == 0)
1908 else if (g_strcmp0(privacy, "preferred") == 0)
1910 else if (g_strcmp0(privacy, "prefered") == 0)
1916 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1918 struct connman_ipdevice *ipdevice;
1924 ipdevice = g_hash_table_lookup(ipdevice_hash,
1925 GINT_TO_POINTER(ipconfig->index));
1929 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1930 ipdevice->ipv6_privacy));
1935 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1943 privacy = string2privacy(value);
1945 ipconfig->ipv6_privacy_config = privacy;
1947 enable_ipv6(ipconfig);
1952 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1953 DBusMessageIter *iter)
1955 struct connman_ipaddress *append_addr = NULL;
1958 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1961 str = __connman_ipconfig_method2string(ipconfig->method);
1965 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1967 switch (ipconfig->method) {
1968 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1969 case CONNMAN_IPCONFIG_METHOD_OFF:
1972 case CONNMAN_IPCONFIG_METHOD_FIXED:
1973 append_addr = ipconfig->address;
1976 case CONNMAN_IPCONFIG_METHOD_AUTO:
1977 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1978 case CONNMAN_IPCONFIG_METHOD_DHCP:
1979 append_addr = ipconfig->system;
1980 #if defined TIZEN_EXT
1981 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1982 if (append_addr && append_addr->local == NULL)
1983 append_addr = ipconfig->address;
1991 if (append_addr->local) {
1993 struct in_addr netmask;
1996 connman_dbus_dict_append_basic(iter, "Address",
1997 DBUS_TYPE_STRING, &append_addr->local);
1999 addr = 0xffffffff << (32 - append_addr->prefixlen);
2000 netmask.s_addr = htonl(addr);
2001 mask = inet_ntoa(netmask);
2002 connman_dbus_dict_append_basic(iter, "Netmask",
2003 DBUS_TYPE_STRING, &mask);
2006 if (append_addr->gateway)
2007 connman_dbus_dict_append_basic(iter, "Gateway",
2008 DBUS_TYPE_STRING, &append_addr->gateway);
2010 #if defined TIZEN_EXT
2011 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2013 server_ip = __connman_dhcp_get_server_address(ipconfig);
2015 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
2016 DBUS_TYPE_STRING, &server_ip);
2019 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
2020 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
2025 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
2026 DBusMessageIter *iter,
2027 struct connman_ipconfig *ipconfig_ipv4)
2029 struct connman_ipaddress *append_addr = NULL;
2030 const char *str, *privacy;
2032 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2035 str = __connman_ipconfig_method2string(ipconfig->method);
2039 if (ipconfig_ipv4 &&
2040 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2041 if (__connman_6to4_check(ipconfig_ipv4) == 1)
2045 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2047 switch (ipconfig->method) {
2048 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2049 case CONNMAN_IPCONFIG_METHOD_OFF:
2052 case CONNMAN_IPCONFIG_METHOD_FIXED:
2053 append_addr = ipconfig->address;
2056 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2057 case CONNMAN_IPCONFIG_METHOD_DHCP:
2058 case CONNMAN_IPCONFIG_METHOD_AUTO:
2059 append_addr = ipconfig->system;
2060 #if defined TIZEN_EXT
2061 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
2062 if (append_addr && append_addr->local == NULL)
2063 append_addr = ipconfig->address;
2071 if (append_addr->local) {
2072 connman_dbus_dict_append_basic(iter, "Address",
2073 DBUS_TYPE_STRING, &append_addr->local);
2074 connman_dbus_dict_append_basic(iter, "PrefixLength",
2076 &append_addr->prefixlen);
2079 if (append_addr->gateway)
2080 connman_dbus_dict_append_basic(iter, "Gateway",
2081 DBUS_TYPE_STRING, &append_addr->gateway);
2083 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2084 connman_dbus_dict_append_basic(iter, "Privacy",
2085 DBUS_TYPE_STRING, &privacy);
2088 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2089 DBusMessageIter *iter)
2091 const char *str, *privacy;
2093 str = __connman_ipconfig_method2string(ipconfig->method);
2097 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2099 switch (ipconfig->method) {
2100 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2101 case CONNMAN_IPCONFIG_METHOD_OFF:
2102 case CONNMAN_IPCONFIG_METHOD_DHCP:
2104 case CONNMAN_IPCONFIG_METHOD_FIXED:
2105 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2106 case CONNMAN_IPCONFIG_METHOD_AUTO:
2110 if (!ipconfig->address)
2113 if (ipconfig->address->local) {
2114 connman_dbus_dict_append_basic(iter, "Address",
2115 DBUS_TYPE_STRING, &ipconfig->address->local);
2116 connman_dbus_dict_append_basic(iter, "PrefixLength",
2118 &ipconfig->address->prefixlen);
2121 if (ipconfig->address->gateway)
2122 connman_dbus_dict_append_basic(iter, "Gateway",
2123 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2125 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2126 connman_dbus_dict_append_basic(iter, "Privacy",
2127 DBUS_TYPE_STRING, &privacy);
2130 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2131 DBusMessageIter *iter)
2135 str = __connman_ipconfig_method2string(ipconfig->method);
2139 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2141 switch (ipconfig->method) {
2142 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2143 case CONNMAN_IPCONFIG_METHOD_OFF:
2144 case CONNMAN_IPCONFIG_METHOD_DHCP:
2145 case CONNMAN_IPCONFIG_METHOD_AUTO:
2147 case CONNMAN_IPCONFIG_METHOD_FIXED:
2148 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2152 if (!ipconfig->address)
2155 if (ipconfig->address->local) {
2157 struct in_addr netmask;
2160 connman_dbus_dict_append_basic(iter, "Address",
2161 DBUS_TYPE_STRING, &ipconfig->address->local);
2163 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2164 netmask.s_addr = htonl(addr);
2165 mask = inet_ntoa(netmask);
2166 connman_dbus_dict_append_basic(iter, "Netmask",
2167 DBUS_TYPE_STRING, &mask);
2170 if (ipconfig->address->gateway)
2171 connman_dbus_dict_append_basic(iter, "Gateway",
2172 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2175 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2176 DBusMessageIter *array)
2178 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2179 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2180 *privacy_string = NULL;
2181 int prefix_length = 0, privacy = 0;
2182 DBusMessageIter dict;
2185 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2188 dbus_message_iter_recurse(array, &dict);
2190 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2191 DBusMessageIter entry, value;
2195 dbus_message_iter_recurse(&dict, &entry);
2197 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2200 dbus_message_iter_get_basic(&entry, &key);
2201 dbus_message_iter_next(&entry);
2203 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2206 dbus_message_iter_recurse(&entry, &value);
2208 type = dbus_message_iter_get_arg_type(&value);
2210 if (g_str_equal(key, "Method")) {
2213 if (type != DBUS_TYPE_STRING)
2216 dbus_message_iter_get_basic(&value, &str);
2217 method = __connman_ipconfig_string2method(str);
2218 } else if (g_str_equal(key, "Address")) {
2219 if (type != DBUS_TYPE_STRING)
2222 dbus_message_iter_get_basic(&value, &address);
2223 } else if (g_str_equal(key, "PrefixLength")) {
2224 if (type != DBUS_TYPE_BYTE)
2227 dbus_message_iter_get_basic(&value, &prefix_length);
2229 if (prefix_length < 0 || prefix_length > 128)
2231 } else if (g_str_equal(key, "Netmask")) {
2232 if (type != DBUS_TYPE_STRING)
2235 dbus_message_iter_get_basic(&value, &netmask);
2236 } else if (g_str_equal(key, "Gateway")) {
2237 if (type != DBUS_TYPE_STRING)
2240 dbus_message_iter_get_basic(&value, &gateway);
2241 } else if (g_str_equal(key, "Privacy")) {
2242 if (type != DBUS_TYPE_STRING)
2245 dbus_message_iter_get_basic(&value, &privacy_string);
2246 privacy = string2privacy(privacy_string);
2249 dbus_message_iter_next(&dict);
2252 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2254 method, address, netmask, gateway, prefix_length,
2258 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2259 case CONNMAN_IPCONFIG_METHOD_FIXED:
2262 case CONNMAN_IPCONFIG_METHOD_OFF:
2263 ipconfig->method = method;
2264 #if defined TIZEN_EXT
2265 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2266 disable_ipv6(ipconfig);
2271 case CONNMAN_IPCONFIG_METHOD_AUTO:
2272 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2275 ipconfig->method = method;
2277 ipconfig->ipv6_privacy_config = privacy;
2278 #if defined TIZEN_EXT
2279 enable_ipv6(ipconfig);
2284 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2285 switch (ipconfig->type) {
2286 case CONNMAN_IPCONFIG_TYPE_IPV4:
2289 case CONNMAN_IPCONFIG_TYPE_IPV6:
2292 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2293 case CONNMAN_IPCONFIG_TYPE_ALL:
2298 if ((address && connman_inet_check_ipaddress(address)
2301 connman_inet_check_ipaddress(netmask)
2304 connman_inet_check_ipaddress(gateway)
2308 ipconfig->method = method;
2310 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2311 connman_ipaddress_set_ipv4(ipconfig->address,
2312 address, netmask, gateway);
2314 return connman_ipaddress_set_ipv6(
2315 ipconfig->address, address,
2316 prefix_length, gateway);
2320 case CONNMAN_IPCONFIG_METHOD_DHCP:
2321 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2324 ipconfig->method = method;
2331 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2332 DBusMessageIter *iter)
2334 struct connman_ipdevice *ipdevice;
2335 const char *method = "auto";
2337 connman_dbus_dict_append_basic(iter, "Method",
2338 DBUS_TYPE_STRING, &method);
2340 ipdevice = g_hash_table_lookup(ipdevice_hash,
2341 GINT_TO_POINTER(ipconfig->index));
2345 if (ipconfig->index >= 0) {
2346 char *ifname = connman_inet_ifname(ipconfig->index);
2348 connman_dbus_dict_append_basic(iter, "Interface",
2349 DBUS_TYPE_STRING, &ifname);
2354 if (ipdevice->address)
2355 connman_dbus_dict_append_basic(iter, "Address",
2356 DBUS_TYPE_STRING, &ipdevice->address);
2358 if (ipdevice->mtu > 0)
2359 connman_dbus_dict_append_basic(iter, "MTU",
2360 DBUS_TYPE_UINT16, &ipdevice->mtu);
2363 void __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2364 GKeyFile *keyfile, const char *identifier, const char *prefix)
2368 struct ipconfig_store is = { .file = keyfile,
2369 .group = identifier,
2372 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2374 method = store_get_str(&is, "method");
2376 switch (ipconfig->type) {
2377 case CONNMAN_IPCONFIG_TYPE_IPV4:
2378 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2381 case CONNMAN_IPCONFIG_TYPE_IPV6:
2382 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2385 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2386 case CONNMAN_IPCONFIG_TYPE_ALL:
2387 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2391 ipconfig->method = __connman_ipconfig_string2method(method);
2395 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2396 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2398 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2399 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2400 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2403 privacy = store_get_str(&is, "privacy");
2404 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2408 g_strfreev(ipconfig->last_dhcpv6_prefixes);
2409 ipconfig->last_dhcpv6_prefixes =
2410 store_get_strs(&is, "DHCP.LastPrefixes");
2414 switch (ipconfig->method) {
2415 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2416 case CONNMAN_IPCONFIG_METHOD_OFF:
2419 case CONNMAN_IPCONFIG_METHOD_FIXED:
2420 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2421 ipconfig->address->prefixlen =
2422 store_get_int(&is, "netmask_prefixlen");
2424 g_free(ipconfig->address->local);
2425 ipconfig->address->local =
2426 store_get_str(&is, "local_address");
2428 g_free(ipconfig->address->peer);
2429 ipconfig->address->peer =
2430 store_get_str(&is, "peer_address");
2432 g_free(ipconfig->address->broadcast);
2433 ipconfig->address->broadcast =
2434 store_get_str(&is, "broadcast_address");
2436 g_free(ipconfig->address->gateway);
2437 ipconfig->address->gateway =
2438 store_get_str(&is, "gateway");
2441 case CONNMAN_IPCONFIG_METHOD_AUTO:
2442 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2446 * If the last used method for IPv4 was AUTO then we
2447 * try first DHCP. We will try also to use the last
2448 * used DHCP address, if exits.
2450 __connman_ipconfig_set_method(ipconfig,
2451 CONNMAN_IPCONFIG_METHOD_DHCP);
2454 case CONNMAN_IPCONFIG_METHOD_DHCP:
2455 str = store_get_str(&is, "DHCP.LastAddress");
2457 g_free(ipconfig->last_dhcp_address);
2458 ipconfig->last_dhcp_address = str;
2465 void __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2466 GKeyFile *keyfile, const char *identifier, const char *prefix)
2469 struct ipconfig_store is = { .file = keyfile,
2470 .group = identifier,
2473 method = __connman_ipconfig_method2string(ipconfig->method);
2474 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2476 store_set_str(&is, "method", method);
2478 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2479 store_set_str(&is, "privacy",
2480 privacy2string(ipconfig->ipv6_privacy_config));
2482 store_set_str(&is, "DHCP.LastAddress",
2483 ipconfig->last_dhcp_address);
2485 store_set_strs(&is, "DHCP.LastPrefixes",
2486 ipconfig->last_dhcpv6_prefixes);
2489 switch (ipconfig->method) {
2490 case CONNMAN_IPCONFIG_METHOD_FIXED:
2491 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2494 case CONNMAN_IPCONFIG_METHOD_DHCP:
2495 store_set_str(&is, "DHCP.LastAddress",
2496 ipconfig->last_dhcp_address);
2499 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2500 case CONNMAN_IPCONFIG_METHOD_OFF:
2501 case CONNMAN_IPCONFIG_METHOD_AUTO:
2505 store_set_int(&is, "netmask_prefixlen", ipconfig->address->prefixlen);
2506 store_set_str(&is, "local_address", ipconfig->address->local);
2507 store_set_str(&is, "peer_address", ipconfig->address->peer);
2508 store_set_str(&is, "broadcast_address", ipconfig->address->broadcast);
2509 store_set_str(&is, "gateway", ipconfig->address->gateway);
2512 int __connman_ipconfig_init(void)
2516 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2517 NULL, free_ipdevice);
2519 is_ipv6_supported = connman_inet_is_ipv6_supported();
2524 void __connman_ipconfig_cleanup(void)
2528 g_hash_table_destroy(ipdevice_hash);
2529 ipdevice_hash = NULL;