5 * Copyright (C) 2007-2010 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
42 struct connman_ipconfig {
45 enum connman_ipconfig_type type;
47 struct connman_ipconfig *origin;
49 const struct connman_ipconfig_ops *ops;
52 enum connman_ipconfig_method method;
53 struct connman_ipaddress *address;
54 struct connman_ipaddress *system;
56 int ipv6_privacy_config;
57 char *last_dhcp_address;
60 struct connman_ipdevice {
82 struct connman_ipconfig *config_ipv4;
83 struct connman_ipconfig *config_ipv6;
85 gboolean ipv6_enabled;
89 static GHashTable *ipdevice_hash = NULL;
90 static GList *ipconfig_list = NULL;
92 struct connman_ipaddress *connman_ipaddress_alloc(int family)
94 struct connman_ipaddress *ipaddress;
96 ipaddress = g_try_new0(struct connman_ipaddress, 1);
97 if (ipaddress == NULL)
100 ipaddress->family = family;
101 ipaddress->prefixlen = 0;
102 ipaddress->local = NULL;
103 ipaddress->peer = NULL;
104 ipaddress->broadcast = NULL;
105 ipaddress->gateway = NULL;
110 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
112 if (ipaddress == NULL)
115 g_free(ipaddress->broadcast);
116 g_free(ipaddress->peer);
117 g_free(ipaddress->local);
118 g_free(ipaddress->gateway);
122 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
131 mask = inet_network(netmask);
134 /* a valid netmask must be 2^n - 1 */
135 if ((host & (host + 1)) != 0)
139 for (; mask; mask <<= 1)
145 static gboolean check_ipv6_address(const char *address)
147 unsigned char buf[sizeof(struct in6_addr)];
153 err = inet_pton(AF_INET6, address, buf);
160 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
162 unsigned char prefix_length,
165 if (ipaddress == NULL)
168 if (check_ipv6_address(address) == FALSE)
171 if (check_ipv6_address(gateway) == FALSE)
174 DBG("prefix_len %d address %s gateway %s",
175 prefix_length, address, gateway);
177 ipaddress->family = AF_INET6;
179 ipaddress->prefixlen = prefix_length;
181 g_free(ipaddress->local);
182 ipaddress->local = g_strdup(address);
184 g_free(ipaddress->gateway);
185 ipaddress->gateway = g_strdup(gateway);
190 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
191 const char *address, const char *netmask, const char *gateway)
193 if (ipaddress == NULL)
196 ipaddress->family = AF_INET;
198 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
200 g_free(ipaddress->local);
201 ipaddress->local = g_strdup(address);
203 g_free(ipaddress->gateway);
204 ipaddress->gateway = g_strdup(gateway);
209 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
212 if (ipaddress == NULL)
215 g_free(ipaddress->peer);
216 ipaddress->peer = g_strdup(peer);
219 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
221 if (ipaddress == NULL)
224 ipaddress->prefixlen = 0;
226 g_free(ipaddress->local);
227 ipaddress->local = NULL;
229 g_free(ipaddress->peer);
230 ipaddress->peer = NULL;
232 g_free(ipaddress->broadcast);
233 ipaddress->broadcast = NULL;
235 g_free(ipaddress->gateway);
236 ipaddress->gateway = NULL;
239 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
240 struct connman_ipaddress *source)
242 if (ipaddress == NULL || source == NULL)
245 ipaddress->family = source->family;
246 ipaddress->prefixlen = source->prefixlen;
248 g_free(ipaddress->local);
249 ipaddress->local = g_strdup(source->local);
251 g_free(ipaddress->peer);
252 ipaddress->peer = g_strdup(source->peer);
254 g_free(ipaddress->broadcast);
255 ipaddress->broadcast = g_strdup(source->broadcast);
257 g_free(ipaddress->gateway);
258 ipaddress->gateway = g_strdup(source->gateway);
261 static void free_address_list(struct connman_ipdevice *ipdevice)
265 for (list = ipdevice->address_list; list; list = list->next) {
266 struct connman_ipaddress *ipaddress = list->data;
268 connman_ipaddress_free(ipaddress);
272 g_slist_free(ipdevice->address_list);
273 ipdevice->address_list = NULL;
276 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
277 unsigned char prefixlen, const char *local)
281 for (list = ipdevice->address_list; list; list = list->next) {
282 struct connman_ipaddress *ipaddress = list->data;
284 if (g_strcmp0(ipaddress->local, local) == 0 &&
285 ipaddress->prefixlen == prefixlen)
292 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
295 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
297 case CONNMAN_IPCONFIG_TYPE_IPV4:
299 case CONNMAN_IPCONFIG_TYPE_IPV6:
306 static const char *type2str(unsigned short type)
311 case ARPHRD_LOOPBACK:
324 static const char *scope2str(unsigned char scope)
336 static gboolean get_ipv6_state(gchar *ifname)
341 gboolean enabled = FALSE;
344 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
346 path = g_strdup_printf(
347 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
352 f = fopen(path, "r");
357 if (fscanf(f, "%d", &disabled) > 0)
365 static void set_ipv6_state(gchar *ifname, gboolean enable)
371 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
373 path = g_strdup_printf(
374 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
379 f = fopen(path, "r+");
394 static int get_ipv6_privacy(gchar *ifname)
403 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
409 f = fopen(path, "r");
416 if (fscanf(f, "%d", &value) <= 0)
424 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
425 * The privacy extension is described in RFC 3041 and RFC 4941
427 static void set_ipv6_privacy(gchar *ifname, int value)
435 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
444 f = fopen(path, "r+");
451 fprintf(f, "%d", value);
455 static int get_rp_filter()
458 int value = -EINVAL, tmp;
460 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
463 if (fscanf(f, "%d", &tmp) == 1)
471 static void set_rp_filter(int value)
475 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
480 fprintf(f, "%d", value);
485 int __connman_ipconfig_set_rp_filter()
489 value = get_rp_filter();
496 connman_info("rp_filter set to 2 (loose mode routing), "
497 "old value was %d", value);
502 void __connman_ipconfig_unset_rp_filter(int old_value)
504 set_rp_filter(old_value);
506 connman_info("rp_filter restored to %d", old_value);
509 static void free_ipdevice(gpointer data)
511 struct connman_ipdevice *ipdevice = data;
513 connman_info("%s {remove} index %d", ipdevice->ifname,
516 if (ipdevice->config_ipv4 != NULL) {
517 connman_ipconfig_unref(ipdevice->config_ipv4);
518 ipdevice->config_ipv4 = NULL;
521 if (ipdevice->config_ipv6 != NULL) {
522 connman_ipconfig_unref(ipdevice->config_ipv6);
523 ipdevice->config_ipv6 = NULL;
526 free_address_list(ipdevice);
527 g_free(ipdevice->ipv4_gateway);
528 g_free(ipdevice->ipv6_gateway);
529 g_free(ipdevice->pac);
531 g_free(ipdevice->address);
533 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
534 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
536 g_free(ipdevice->ifname);
540 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
542 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
543 ipdevice->config_ipv6);
546 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
548 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
549 ipdevice->config_ipv6);
551 if (ipdevice->config_ipv4)
552 connman_inet_clear_address(ipdevice->index,
553 ipdevice->config_ipv4->address);
555 if (ipdevice->config_ipv6)
556 connman_inet_clear_ipv6_address(ipdevice->index,
557 ipdevice->config_ipv6->address->local,
558 ipdevice->config_ipv6->address->prefixlen);
561 static void update_stats(struct connman_ipdevice *ipdevice,
562 struct rtnl_link_stats *stats)
564 struct connman_service *service;
566 if (stats->rx_packets == 0 && stats->tx_packets == 0)
569 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
570 stats->rx_packets, stats->rx_bytes);
571 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
572 stats->tx_packets, stats->tx_bytes);
574 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
577 if (ipdevice->config_ipv4)
578 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
579 else if (ipdevice->config_ipv6)
580 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
587 ipdevice->rx_packets = stats->rx_packets;
588 ipdevice->tx_packets = stats->tx_packets;
589 ipdevice->rx_bytes = stats->rx_bytes;
590 ipdevice->tx_bytes = stats->tx_bytes;
591 ipdevice->rx_errors = stats->rx_errors;
592 ipdevice->tx_errors = stats->tx_errors;
593 ipdevice->rx_dropped = stats->rx_dropped;
594 ipdevice->tx_dropped = stats->tx_dropped;
596 __connman_service_notify(service,
597 ipdevice->rx_packets, ipdevice->tx_packets,
598 ipdevice->rx_bytes, ipdevice->tx_bytes,
599 ipdevice->rx_errors, ipdevice->tx_errors,
600 ipdevice->rx_dropped, ipdevice->tx_dropped);
603 void __connman_ipconfig_newlink(int index, unsigned short type,
604 unsigned int flags, const char *address,
606 struct rtnl_link_stats *stats)
608 struct connman_ipdevice *ipdevice;
611 gboolean up = FALSE, down = FALSE;
612 gboolean lower_up = FALSE, lower_down = FALSE;
614 DBG("index %d", index);
616 if (type == ARPHRD_LOOPBACK)
619 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
620 if (ipdevice != NULL)
623 ipdevice = g_try_new0(struct connman_ipdevice, 1);
624 if (ipdevice == NULL)
627 ipdevice->index = index;
628 ipdevice->ifname = connman_inet_ifname(index);
629 ipdevice->type = type;
631 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
632 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
634 ipdevice->address = g_strdup(address);
636 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
638 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
639 index, type, type2str(type));
644 update_stats(ipdevice, stats);
646 if (flags == ipdevice->flags)
649 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
656 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
657 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
658 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
659 (IFF_RUNNING | IFF_LOWER_UP))
661 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
665 ipdevice->flags = flags;
667 str = g_string_new(NULL);
672 g_string_append(str, "UP");
674 g_string_append(str, "DOWN");
676 if (flags & IFF_RUNNING)
677 g_string_append(str, ",RUNNING");
679 if (flags & IFF_LOWER_UP)
680 g_string_append(str, ",LOWER_UP");
682 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
685 g_string_free(str, TRUE);
687 for (list = g_list_first(ipconfig_list); list;
688 list = g_list_next(list)) {
689 struct connman_ipconfig *ipconfig = list->data;
691 if (index != ipconfig->index)
694 if (ipconfig->ops == NULL)
697 if (up == TRUE && ipconfig->ops->up)
698 ipconfig->ops->up(ipconfig);
699 if (lower_up == TRUE && ipconfig->ops->lower_up)
700 ipconfig->ops->lower_up(ipconfig);
702 if (lower_down == TRUE && ipconfig->ops->lower_down)
703 ipconfig->ops->lower_down(ipconfig);
704 if (down == TRUE && ipconfig->ops->down)
705 ipconfig->ops->down(ipconfig);
708 #if defined TIZEN_EXT
709 if (g_strcmp0(ipdevice->address, address) != 0) {
710 g_free(ipdevice->address);
711 ipdevice->address = g_strdup(address);
716 __connman_ipconfig_lower_up(ipdevice);
718 __connman_ipconfig_lower_down(ipdevice);
721 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
723 struct connman_ipdevice *ipdevice;
726 DBG("index %d", index);
728 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
729 if (ipdevice == NULL)
732 update_stats(ipdevice, stats);
734 for (list = g_list_first(ipconfig_list); list;
735 list = g_list_next(list)) {
736 struct connman_ipconfig *ipconfig = list->data;
738 if (index != ipconfig->index)
741 ipconfig->index = -1;
743 if (ipconfig->ops == NULL)
746 if (ipconfig->ops->lower_down)
747 ipconfig->ops->lower_down(ipconfig);
748 if (ipconfig->ops->down)
749 ipconfig->ops->down(ipconfig);
752 __connman_ipconfig_lower_down(ipdevice);
754 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
757 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
759 const struct connman_ipaddress *addr1 = a;
760 const struct connman_ipaddress *addr2 = b;
762 if (addr1->prefixlen != addr2->prefixlen)
763 return addr2->prefixlen - addr1->prefixlen;
765 return g_strcmp0(addr1->local, addr2->local);
768 void __connman_ipconfig_newaddr(int index, int family, const char *label,
769 unsigned char prefixlen, const char *address)
771 struct connman_ipdevice *ipdevice;
772 struct connman_ipaddress *ipaddress;
773 enum connman_ipconfig_type type;
776 DBG("index %d", index);
778 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
779 if (ipdevice == NULL)
782 ipaddress = connman_ipaddress_alloc(family);
783 if (ipaddress == NULL)
786 ipaddress->prefixlen = prefixlen;
787 ipaddress->local = g_strdup(address);
789 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
790 check_duplicate_address)) {
791 connman_ipaddress_free(ipaddress);
795 if (family == AF_INET)
796 type = CONNMAN_IPCONFIG_TYPE_IPV4;
797 else if (family == AF_INET6)
798 type = CONNMAN_IPCONFIG_TYPE_IPV6;
802 ipdevice->address_list = g_slist_append(ipdevice->address_list,
805 connman_info("%s {add} address %s/%u label %s family %d",
806 ipdevice->ifname, address, prefixlen, label, family);
808 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
809 connman_ipaddress_copy(ipdevice->config_ipv4->system,
812 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
813 connman_ipaddress_copy(ipdevice->config_ipv6->system,
818 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
821 for (list = g_list_first(ipconfig_list); list;
822 list = g_list_next(list)) {
823 struct connman_ipconfig *ipconfig = list->data;
825 if (index != ipconfig->index)
828 if (type != ipconfig->type)
831 if (ipconfig->ops == NULL)
834 if (ipconfig->ops->ip_bound)
835 ipconfig->ops->ip_bound(ipconfig);
839 void __connman_ipconfig_deladdr(int index, int family, const char *label,
840 unsigned char prefixlen, const char *address)
842 struct connman_ipdevice *ipdevice;
843 struct connman_ipaddress *ipaddress;
844 enum connman_ipconfig_type type;
847 DBG("index %d", index);
849 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
850 if (ipdevice == NULL)
853 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
854 if (ipaddress == NULL)
857 if (family == AF_INET)
858 type = CONNMAN_IPCONFIG_TYPE_IPV4;
859 else if (family == AF_INET6)
860 type = CONNMAN_IPCONFIG_TYPE_IPV6;
864 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
867 connman_ipaddress_clear(ipaddress);
870 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
871 address, prefixlen, label);
873 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
876 if (g_slist_length(ipdevice->address_list) > 0)
879 for (list = g_list_first(ipconfig_list); list;
880 list = g_list_next(list)) {
881 struct connman_ipconfig *ipconfig = list->data;
883 if (index != ipconfig->index)
886 if (type != ipconfig->type)
889 if (ipconfig->ops == NULL)
892 if (ipconfig->ops->ip_release)
893 ipconfig->ops->ip_release(ipconfig);
897 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
898 const char *dst, const char *gateway)
900 struct connman_ipdevice *ipdevice;
902 DBG("index %d", index);
904 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
905 if (ipdevice == NULL)
908 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
909 g_strcmp0(dst, "::") == 0)) {
912 enum connman_ipconfig_type type;
914 if (family == AF_INET6) {
915 type = CONNMAN_IPCONFIG_TYPE_IPV6;
916 g_free(ipdevice->ipv6_gateway);
917 ipdevice->ipv6_gateway = g_strdup(gateway);
919 if (ipdevice->config_ipv6 != NULL &&
920 ipdevice->config_ipv6->system != NULL) {
921 g_free(ipdevice->config_ipv6->system->gateway);
922 ipdevice->config_ipv6->system->gateway =
925 } else if (family == AF_INET) {
926 type = CONNMAN_IPCONFIG_TYPE_IPV4;
927 g_free(ipdevice->ipv4_gateway);
928 ipdevice->ipv4_gateway = g_strdup(gateway);
930 if (ipdevice->config_ipv4 != NULL &&
931 ipdevice->config_ipv4->system != NULL) {
932 g_free(ipdevice->config_ipv4->system->gateway);
933 ipdevice->config_ipv4->system->gateway =
939 for (list = ipdevice->address_list; list; list = list->next) {
940 struct connman_ipaddress *ipaddress = list->data;
942 g_free(ipaddress->gateway);
943 ipaddress->gateway = g_strdup(gateway);
946 for (config_list = g_list_first(ipconfig_list); config_list;
947 config_list = g_list_next(config_list)) {
948 struct connman_ipconfig *ipconfig = config_list->data;
950 if (index != ipconfig->index)
953 if (type != ipconfig->type)
956 if (ipconfig->ops == NULL)
959 if (ipconfig->ops->ip_bound)
960 ipconfig->ops->ip_bound(ipconfig);
964 connman_info("%s {add} route %s gw %s scope %u <%s>",
965 ipdevice->ifname, dst, gateway,
966 scope, scope2str(scope));
969 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
970 const char *dst, const char *gateway)
972 struct connman_ipdevice *ipdevice;
974 DBG("index %d", index);
976 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
977 if (ipdevice == NULL)
980 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
981 g_strcmp0(dst, "::") == 0)) {
984 enum connman_ipconfig_type type;
986 if (family == AF_INET6) {
987 type = CONNMAN_IPCONFIG_TYPE_IPV6;
988 g_free(ipdevice->ipv6_gateway);
989 ipdevice->ipv6_gateway = NULL;
991 if (ipdevice->config_ipv6 != NULL &&
992 ipdevice->config_ipv6->system != NULL) {
993 g_free(ipdevice->config_ipv6->system->gateway);
994 ipdevice->config_ipv6->system->gateway = NULL;
996 } else if (family == AF_INET) {
997 type = CONNMAN_IPCONFIG_TYPE_IPV4;
998 g_free(ipdevice->ipv4_gateway);
999 ipdevice->ipv4_gateway = NULL;
1001 if (ipdevice->config_ipv4 != NULL &&
1002 ipdevice->config_ipv4->system != NULL) {
1003 g_free(ipdevice->config_ipv4->system->gateway);
1004 ipdevice->config_ipv4->system->gateway = NULL;
1009 for (list = ipdevice->address_list; list; list = list->next) {
1010 struct connman_ipaddress *ipaddress = list->data;
1012 g_free(ipaddress->gateway);
1013 ipaddress->gateway = NULL;
1016 for (config_list = g_list_first(ipconfig_list); config_list;
1017 config_list = g_list_next(config_list)) {
1018 struct connman_ipconfig *ipconfig = config_list->data;
1020 if (index != ipconfig->index)
1023 if (type != ipconfig->type)
1026 if (ipconfig->ops == NULL)
1029 if (ipconfig->ops->ip_release)
1030 ipconfig->ops->ip_release(ipconfig);
1034 connman_info("%s {del} route %s gw %s scope %u <%s>",
1035 ipdevice->ifname, dst, gateway,
1036 scope, scope2str(scope));
1039 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1044 keys = g_hash_table_get_keys(ipdevice_hash);
1048 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1049 int index = GPOINTER_TO_INT(list->data);
1051 function(index, user_data);
1057 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1058 struct connman_ipconfig *ipconfig)
1060 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1063 unsigned short __connman_ipconfig_get_type_from_index(int index)
1065 struct connman_ipdevice *ipdevice;
1067 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1068 if (ipdevice == NULL)
1071 return ipdevice->type;
1074 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1076 struct connman_ipdevice *ipdevice;
1078 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1079 if (ipdevice == NULL)
1082 return ipdevice->flags;
1085 const char *__connman_ipconfig_get_gateway_from_index(int index)
1087 struct connman_ipdevice *ipdevice;
1089 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1090 if (ipdevice == NULL)
1093 if (ipdevice->ipv4_gateway != NULL)
1094 return ipdevice->ipv4_gateway;
1096 if (ipdevice->config_ipv4 != NULL &&
1097 ipdevice->config_ipv4->address != NULL)
1098 return ipdevice->config_ipv4->address->gateway;
1100 if (ipdevice->ipv6_gateway != NULL)
1101 return ipdevice->ipv6_gateway;
1103 if (ipdevice->config_ipv6 != NULL &&
1104 ipdevice->config_ipv6->address != NULL)
1105 return ipdevice->config_ipv6->address->gateway;
1110 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1112 ipconfig->index = index;
1115 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1117 if (ipconfig->address == NULL)
1120 return ipconfig->address->local;
1123 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1125 if (ipconfig->address == NULL)
1128 g_free(ipconfig->address->local);
1129 ipconfig->address->local = g_strdup(address);
1132 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1134 if (ipconfig->address == NULL)
1137 return ipconfig->address->peer;
1140 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1142 if (ipconfig->address == NULL)
1145 g_free(ipconfig->address->peer);
1146 ipconfig->address->peer = g_strdup(address);
1149 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1151 if (ipconfig->address == NULL)
1154 return ipconfig->address->broadcast;
1157 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1159 if (ipconfig->address == NULL)
1162 g_free(ipconfig->address->broadcast);
1163 ipconfig->address->broadcast = g_strdup(broadcast);
1166 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1168 if (ipconfig->address == NULL)
1171 return ipconfig->address->gateway;
1174 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1178 if (ipconfig->address == NULL)
1180 g_free(ipconfig->address->gateway);
1181 ipconfig->address->gateway = g_strdup(gateway);
1184 #if defined TIZEN_EXT
1186 * Description: __connman_service_lookup_from_index cannot find correct service
1188 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1190 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1193 #if !defined TIZEN_EXT
1194 struct connman_service *service;
1199 if (ipconfig->address == NULL)
1202 #if !defined TIZEN_EXT
1203 service = __connman_service_lookup_from_index(ipconfig->index);
1205 if (service == NULL)
1208 __connman_connection_gateway_remove(service, ipconfig->type);
1210 DBG("type %d gw %s peer %s", ipconfig->type,
1211 ipconfig->address->gateway, ipconfig->address->peer);
1213 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1214 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1215 return __connman_connection_gateway_add(service,
1216 ipconfig->address->gateway,
1218 ipconfig->address->peer);
1223 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1225 struct connman_service *service;
1229 service = __connman_service_lookup_from_index(ipconfig->index);
1230 if (service != NULL)
1231 __connman_connection_gateway_remove(service, ipconfig->type);
1234 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1236 if (ipconfig->address == NULL)
1239 return ipconfig->address->prefixlen;
1242 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1244 if (ipconfig->address == NULL)
1247 ipconfig->address->prefixlen = prefixlen;
1250 static struct connman_ipconfig *create_ipv6config(int index)
1252 struct connman_ipconfig *ipv6config;
1254 DBG("index %d", index);
1256 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1257 if (ipv6config == NULL)
1260 ipv6config->refcount = 1;
1262 ipv6config->index = index;
1263 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1264 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1265 ipv6config->ipv6_privacy_config = 0;
1267 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1268 if (ipv6config->address == NULL) {
1273 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1275 DBG("ipconfig %p", ipv6config);
1281 * connman_ipconfig_create:
1283 * Allocate a new ipconfig structure.
1285 * Returns: a newly-allocated #connman_ipconfig structure
1287 struct connman_ipconfig *connman_ipconfig_create(int index,
1288 enum connman_ipconfig_type type)
1290 struct connman_ipconfig *ipconfig;
1292 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1293 return create_ipv6config(index);
1295 DBG("index %d", index);
1297 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1298 if (ipconfig == NULL)
1301 ipconfig->refcount = 1;
1303 ipconfig->index = index;
1304 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1306 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1307 if (ipconfig->address == NULL) {
1312 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1314 DBG("ipconfig %p", ipconfig);
1321 * connman_ipconfig_ref:
1322 * @ipconfig: ipconfig structure
1324 * Increase reference counter of ipconfig
1326 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1328 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1330 __sync_fetch_and_add(&ipconfig->refcount, 1);
1336 * connman_ipconfig_unref:
1337 * @ipconfig: ipconfig structure
1339 * Decrease reference counter of ipconfig
1341 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1343 if (ipconfig == NULL)
1346 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1348 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1351 if (__connman_ipconfig_disable(ipconfig) < 0)
1352 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1354 connman_ipconfig_set_ops(ipconfig, NULL);
1356 if (ipconfig->origin != NULL) {
1357 connman_ipconfig_unref(ipconfig->origin);
1358 ipconfig->origin = NULL;
1361 connman_ipaddress_free(ipconfig->system);
1362 connman_ipaddress_free(ipconfig->address);
1363 g_free(ipconfig->last_dhcp_address);
1368 * connman_ipconfig_get_data:
1369 * @ipconfig: ipconfig structure
1371 * Get private data pointer
1373 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1375 if (ipconfig == NULL)
1378 return ipconfig->ops_data;
1382 * connman_ipconfig_set_data:
1383 * @ipconfig: ipconfig structure
1384 * @data: data pointer
1386 * Set private data pointer
1388 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1390 ipconfig->ops_data = data;
1394 * connman_ipconfig_get_index:
1395 * @ipconfig: ipconfig structure
1397 * Get interface index
1399 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1401 if (ipconfig == NULL)
1404 if (ipconfig->origin != NULL)
1405 return ipconfig->origin->index;
1407 return ipconfig->index;
1411 * connman_ipconfig_get_ifname:
1412 * @ipconfig: ipconfig structure
1414 * Get interface name
1416 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1418 struct connman_ipdevice *ipdevice;
1420 if (ipconfig == NULL)
1423 if (ipconfig->index < 0)
1426 ipdevice = g_hash_table_lookup(ipdevice_hash,
1427 GINT_TO_POINTER(ipconfig->index));
1428 if (ipdevice == NULL)
1431 return ipdevice->ifname;
1435 * connman_ipconfig_set_ops:
1436 * @ipconfig: ipconfig structure
1437 * @ops: operation callbacks
1439 * Set the operation callbacks
1441 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1442 const struct connman_ipconfig_ops *ops)
1444 ipconfig->ops = ops;
1448 * connman_ipconfig_set_method:
1449 * @ipconfig: ipconfig structure
1450 * @method: configuration method
1452 * Set the configuration method
1454 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1455 enum connman_ipconfig_method method)
1457 ipconfig->method = method;
1462 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1464 if (ipconfig == NULL)
1465 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1467 return ipconfig->method;
1470 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1474 switch (ipconfig->method) {
1475 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1476 case CONNMAN_IPCONFIG_METHOD_OFF:
1477 case CONNMAN_IPCONFIG_METHOD_AUTO:
1479 case CONNMAN_IPCONFIG_METHOD_FIXED:
1480 case CONNMAN_IPCONFIG_METHOD_DHCP:
1481 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1482 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1483 return connman_inet_set_address(ipconfig->index,
1485 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1486 return connman_inet_set_ipv6_address(
1487 ipconfig->index, ipconfig->address);
1493 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1499 if (ipconfig == NULL)
1502 DBG("method %d", ipconfig->method);
1504 switch (ipconfig->method) {
1505 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1506 case CONNMAN_IPCONFIG_METHOD_OFF:
1507 case CONNMAN_IPCONFIG_METHOD_AUTO:
1509 case CONNMAN_IPCONFIG_METHOD_FIXED:
1510 case CONNMAN_IPCONFIG_METHOD_DHCP:
1511 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1512 err = __connman_ipconfig_address_unset(ipconfig);
1513 connman_ipaddress_clear(ipconfig->address);
1521 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1527 if (ipconfig == NULL)
1530 DBG("method %d", ipconfig->method);
1532 switch (ipconfig->method) {
1533 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1534 case CONNMAN_IPCONFIG_METHOD_OFF:
1535 case CONNMAN_IPCONFIG_METHOD_AUTO:
1537 case CONNMAN_IPCONFIG_METHOD_FIXED:
1538 case CONNMAN_IPCONFIG_METHOD_DHCP:
1539 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1540 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1541 err = connman_inet_clear_address(ipconfig->index,
1543 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1544 err = connman_inet_clear_ipv6_address(
1546 ipconfig->address->local,
1547 ipconfig->address->prefixlen);
1557 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1560 struct connman_ipdevice *ipdevice;
1562 DBG("ipconfig %p", ipconfig);
1564 if (ipconfig == NULL || ipconfig->index < 0)
1567 ipdevice = g_hash_table_lookup(ipdevice_hash,
1568 GINT_TO_POINTER(ipconfig->index));
1569 if (ipdevice == NULL)
1572 g_free(ipdevice->pac);
1573 ipdevice->pac = g_strdup(url);
1578 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1580 struct connman_ipdevice *ipdevice;
1582 DBG("ipconfig %p", ipconfig);
1584 if (ipconfig == NULL || ipconfig->index < 0)
1587 ipdevice = g_hash_table_lookup(ipdevice_hash,
1588 GINT_TO_POINTER(ipconfig->index));
1589 if (ipdevice == NULL)
1592 return ipdevice->pac;
1595 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1596 const char *address)
1598 if (ipconfig == NULL)
1601 g_free(ipconfig->last_dhcp_address);
1602 ipconfig->last_dhcp_address = g_strdup(address);
1605 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1607 if (ipconfig == NULL)
1610 return ipconfig->last_dhcp_address;
1613 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1615 struct connman_ipdevice *ipdevice;
1619 ipdevice = g_hash_table_lookup(ipdevice_hash,
1620 GINT_TO_POINTER(ipconfig->index));
1621 if (ipdevice == NULL)
1624 set_ipv6_state(ipdevice->ifname, FALSE);
1627 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1629 struct connman_ipdevice *ipdevice;
1633 ipdevice = g_hash_table_lookup(ipdevice_hash,
1634 GINT_TO_POINTER(ipconfig->index));
1635 if (ipdevice == NULL)
1638 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1639 set_ipv6_privacy(ipdevice->ifname,
1640 ipconfig->ipv6_privacy_config);
1642 set_ipv6_state(ipdevice->ifname, TRUE);
1645 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1647 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1650 enable_ipv6(ipconfig);
1653 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1655 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1658 disable_ipv6(ipconfig);
1661 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1663 struct connman_ipdevice *ipdevice;
1664 gboolean up = FALSE, down = FALSE;
1665 gboolean lower_up = FALSE, lower_down = FALSE;
1666 enum connman_ipconfig_type type;
1668 DBG("ipconfig %p", ipconfig);
1670 if (ipconfig == NULL || ipconfig->index < 0)
1673 ipdevice = g_hash_table_lookup(ipdevice_hash,
1674 GINT_TO_POINTER(ipconfig->index));
1675 if (ipdevice == NULL)
1678 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1679 if (ipdevice->config_ipv4 == ipconfig)
1681 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1682 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1683 if (ipdevice->config_ipv6 == ipconfig)
1685 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1686 enable_ipv6(ipconfig);
1690 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1691 ipdevice->config_ipv4 != NULL) {
1692 ipconfig_list = g_list_remove(ipconfig_list,
1693 ipdevice->config_ipv4);
1695 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1697 connman_ipconfig_unref(ipdevice->config_ipv4);
1700 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1701 ipdevice->config_ipv6 != NULL) {
1702 ipconfig_list = g_list_remove(ipconfig_list,
1703 ipdevice->config_ipv6);
1705 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1707 connman_ipconfig_unref(ipdevice->config_ipv6);
1710 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1711 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1712 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1713 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1715 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1717 if (ipdevice->flags & IFF_UP)
1722 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1723 (IFF_RUNNING | IFF_LOWER_UP))
1725 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1728 if (up == TRUE && ipconfig->ops->up)
1729 ipconfig->ops->up(ipconfig);
1730 if (lower_up == TRUE && ipconfig->ops->lower_up)
1731 ipconfig->ops->lower_up(ipconfig);
1733 if (lower_down == TRUE && ipconfig->ops->lower_down)
1734 ipconfig->ops->lower_down(ipconfig);
1735 if (down == TRUE && ipconfig->ops->down)
1736 ipconfig->ops->down(ipconfig);
1741 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1743 struct connman_ipdevice *ipdevice;
1745 DBG("ipconfig %p", ipconfig);
1747 if (ipconfig == NULL || ipconfig->index < 0)
1750 ipdevice = g_hash_table_lookup(ipdevice_hash,
1751 GINT_TO_POINTER(ipconfig->index));
1752 if (ipdevice == NULL)
1755 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1758 if (ipdevice->config_ipv4 == ipconfig) {
1759 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1761 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1762 connman_ipconfig_unref(ipdevice->config_ipv4);
1763 ipdevice->config_ipv4 = NULL;
1767 if (ipdevice->config_ipv6 == ipconfig) {
1768 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1770 if (ipdevice->config_ipv6->method ==
1771 CONNMAN_IPCONFIG_METHOD_AUTO)
1772 disable_ipv6(ipdevice->config_ipv6);
1774 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1775 connman_ipconfig_unref(ipdevice->config_ipv6);
1776 ipdevice->config_ipv6 = NULL;
1783 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1786 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1788 case CONNMAN_IPCONFIG_METHOD_OFF:
1790 case CONNMAN_IPCONFIG_METHOD_FIXED:
1792 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1794 case CONNMAN_IPCONFIG_METHOD_DHCP:
1796 case CONNMAN_IPCONFIG_METHOD_AUTO:
1803 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1805 if (g_strcmp0(method, "off") == 0)
1806 return CONNMAN_IPCONFIG_METHOD_OFF;
1807 else if (g_strcmp0(method, "fixed") == 0)
1808 return CONNMAN_IPCONFIG_METHOD_FIXED;
1809 else if (g_strcmp0(method, "manual") == 0)
1810 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1811 else if (g_strcmp0(method, "dhcp") == 0)
1812 return CONNMAN_IPCONFIG_METHOD_DHCP;
1813 else if (g_strcmp0(method, "auto") == 0)
1814 return CONNMAN_IPCONFIG_METHOD_AUTO;
1816 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1819 static const char *privacy2string(int privacy)
1823 else if (privacy == 1)
1825 else if (privacy > 1)
1831 static int string2privacy(const char *privacy)
1833 if (g_strcmp0(privacy, "disabled") == 0)
1835 else if (g_strcmp0(privacy, "enabled") == 0)
1837 else if (g_strcmp0(privacy, "prefered") == 0)
1843 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1844 DBusMessageIter *iter)
1850 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1853 str = __connman_ipconfig_method2string(ipconfig->method);
1857 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1859 if (ipconfig->system == NULL)
1862 if (ipconfig->system->local != NULL) {
1864 struct in_addr netmask;
1867 connman_dbus_dict_append_basic(iter, "Address",
1868 DBUS_TYPE_STRING, &ipconfig->system->local);
1870 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1871 netmask.s_addr = htonl(addr);
1872 mask = inet_ntoa(netmask);
1873 connman_dbus_dict_append_basic(iter, "Netmask",
1874 DBUS_TYPE_STRING, &mask);
1877 if (ipconfig->system->gateway != NULL)
1878 connman_dbus_dict_append_basic(iter, "Gateway",
1879 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1882 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1883 DBusMessageIter *iter,
1884 struct connman_ipconfig *ipconfig_ipv4)
1886 const char *str, *privacy;
1890 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1893 str = __connman_ipconfig_method2string(ipconfig->method);
1897 if (ipconfig_ipv4 != NULL &&
1898 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1899 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1903 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1905 if (ipconfig->system == NULL)
1908 if (ipconfig->system->local != NULL) {
1909 connman_dbus_dict_append_basic(iter, "Address",
1910 DBUS_TYPE_STRING, &ipconfig->system->local);
1911 connman_dbus_dict_append_basic(iter, "PrefixLength",
1913 &ipconfig->system->prefixlen);
1916 if (ipconfig->system->gateway != NULL)
1917 connman_dbus_dict_append_basic(iter, "Gateway",
1918 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1920 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1921 connman_dbus_dict_append_basic(iter, "Privacy",
1922 DBUS_TYPE_STRING, &privacy);
1925 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1926 DBusMessageIter *iter)
1928 const char *str, *privacy;
1932 str = __connman_ipconfig_method2string(ipconfig->method);
1936 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1938 switch (ipconfig->method) {
1939 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1940 case CONNMAN_IPCONFIG_METHOD_OFF:
1941 case CONNMAN_IPCONFIG_METHOD_DHCP:
1943 case CONNMAN_IPCONFIG_METHOD_FIXED:
1944 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1945 case CONNMAN_IPCONFIG_METHOD_AUTO:
1949 if (ipconfig->address == NULL)
1952 if (ipconfig->address->local != NULL) {
1953 connman_dbus_dict_append_basic(iter, "Address",
1954 DBUS_TYPE_STRING, &ipconfig->address->local);
1955 connman_dbus_dict_append_basic(iter, "PrefixLength",
1957 &ipconfig->address->prefixlen);
1960 if (ipconfig->address->gateway != NULL)
1961 connman_dbus_dict_append_basic(iter, "Gateway",
1962 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1964 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1965 connman_dbus_dict_append_basic(iter, "Privacy",
1966 DBUS_TYPE_STRING, &privacy);
1969 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1970 DBusMessageIter *iter)
1976 str = __connman_ipconfig_method2string(ipconfig->method);
1980 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1982 switch (ipconfig->method) {
1983 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1984 case CONNMAN_IPCONFIG_METHOD_OFF:
1985 case CONNMAN_IPCONFIG_METHOD_DHCP:
1986 case CONNMAN_IPCONFIG_METHOD_AUTO:
1988 case CONNMAN_IPCONFIG_METHOD_FIXED:
1989 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1993 if (ipconfig->address == NULL)
1996 if (ipconfig->address->local != NULL) {
1998 struct in_addr netmask;
2001 connman_dbus_dict_append_basic(iter, "Address",
2002 DBUS_TYPE_STRING, &ipconfig->address->local);
2004 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2005 netmask.s_addr = htonl(addr);
2006 mask = inet_ntoa(netmask);
2007 connman_dbus_dict_append_basic(iter, "Netmask",
2008 DBUS_TYPE_STRING, &mask);
2011 if (ipconfig->address->gateway != NULL)
2012 connman_dbus_dict_append_basic(iter, "Gateway",
2013 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2016 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2017 DBusMessageIter *array)
2019 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2020 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2021 *prefix_length_string = NULL, *privacy_string = NULL;
2022 int prefix_length = 0, privacy = 0;
2023 DBusMessageIter dict;
2025 DBG("ipconfig %p", ipconfig);
2027 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2030 dbus_message_iter_recurse(array, &dict);
2032 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2033 DBusMessageIter entry;
2037 dbus_message_iter_recurse(&dict, &entry);
2039 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2042 dbus_message_iter_get_basic(&entry, &key);
2043 dbus_message_iter_next(&entry);
2045 type = dbus_message_iter_get_arg_type(&entry);
2047 if (g_str_equal(key, "Method") == TRUE) {
2050 if (type != DBUS_TYPE_STRING)
2053 dbus_message_iter_get_basic(&entry, &str);
2054 method = __connman_ipconfig_string2method(str);
2055 } else if (g_str_equal(key, "Address") == TRUE) {
2056 if (type != DBUS_TYPE_STRING)
2059 dbus_message_iter_get_basic(&entry, &address);
2060 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2061 if (type != DBUS_TYPE_STRING)
2064 dbus_message_iter_get_basic(&entry,
2065 &prefix_length_string);
2067 prefix_length = atoi(prefix_length_string);
2068 if (prefix_length < 0 || prefix_length > 128)
2071 } else if (g_str_equal(key, "Netmask") == TRUE) {
2072 if (type != DBUS_TYPE_STRING)
2075 dbus_message_iter_get_basic(&entry, &netmask);
2076 } else if (g_str_equal(key, "Gateway") == TRUE) {
2077 if (type != DBUS_TYPE_STRING)
2080 dbus_message_iter_get_basic(&entry, &gateway);
2081 } else if (g_str_equal(key, "Privacy") == TRUE) {
2082 if (type != DBUS_TYPE_STRING)
2085 dbus_message_iter_get_basic(&entry, &privacy_string);
2086 privacy = string2privacy(privacy_string);
2088 dbus_message_iter_next(&dict);
2091 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2093 method, address, netmask, gateway, prefix_length,
2097 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2098 case CONNMAN_IPCONFIG_METHOD_FIXED:
2101 case CONNMAN_IPCONFIG_METHOD_OFF:
2102 ipconfig->method = method;
2103 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2104 disable_ipv6(ipconfig);
2107 case CONNMAN_IPCONFIG_METHOD_AUTO:
2108 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2111 ipconfig->method = method;
2112 if (privacy_string != NULL)
2113 ipconfig->ipv6_privacy_config = privacy;
2114 enable_ipv6(ipconfig);
2117 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2118 if (address == NULL)
2121 ipconfig->method = method;
2123 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2124 connman_ipaddress_set_ipv4(ipconfig->address,
2125 address, netmask, gateway);
2127 return connman_ipaddress_set_ipv6(
2128 ipconfig->address, address,
2129 prefix_length, gateway);
2132 case CONNMAN_IPCONFIG_METHOD_DHCP:
2133 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2136 ipconfig->method = method;
2143 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2144 DBusMessageIter *iter)
2146 struct connman_ipdevice *ipdevice;
2147 const char *method = "auto";
2149 connman_dbus_dict_append_basic(iter, "Method",
2150 DBUS_TYPE_STRING, &method);
2152 ipdevice = g_hash_table_lookup(ipdevice_hash,
2153 GINT_TO_POINTER(ipconfig->index));
2154 if (ipdevice == NULL)
2157 if (ipdevice->ifname != NULL)
2158 connman_dbus_dict_append_basic(iter, "Interface",
2159 DBUS_TYPE_STRING, &ipdevice->ifname);
2161 if (ipdevice->address != NULL)
2162 connman_dbus_dict_append_basic(iter, "Address",
2163 DBUS_TYPE_STRING, &ipdevice->address);
2165 if (ipdevice->mtu > 0)
2166 connman_dbus_dict_append_basic(iter, "MTU",
2167 DBUS_TYPE_UINT16, &ipdevice->mtu);
2170 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2171 GKeyFile *keyfile, const char *identifier, const char *prefix)
2177 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2179 key = g_strdup_printf("%smethod", prefix);
2180 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2181 if (method == NULL) {
2182 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2183 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2185 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2187 ipconfig->method = __connman_ipconfig_string2method(method);
2189 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2190 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2192 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2193 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2194 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2196 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2197 privacy = g_key_file_get_string(keyfile, identifier,
2199 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2203 __connman_ipconfig_enable(ipconfig);
2204 enable_ipv6(ipconfig);
2211 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2212 ipconfig->address->prefixlen = g_key_file_get_integer(
2213 keyfile, identifier, key, NULL);
2216 key = g_strdup_printf("%slocal_address", prefix);
2217 ipconfig->address->local = g_key_file_get_string(
2218 keyfile, identifier, key, NULL);
2221 key = g_strdup_printf("%speer_address", prefix);
2222 ipconfig->address->peer = g_key_file_get_string(
2223 keyfile, identifier, key, NULL);
2226 key = g_strdup_printf("%sbroadcast_address", prefix);
2227 ipconfig->address->broadcast = g_key_file_get_string(
2228 keyfile, identifier, key, NULL);
2231 key = g_strdup_printf("%sgateway", prefix);
2232 ipconfig->address->gateway = g_key_file_get_string(
2233 keyfile, identifier, key, NULL);
2236 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2237 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2239 g_free(ipconfig->last_dhcp_address);
2240 ipconfig->last_dhcp_address = str;
2247 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2248 GKeyFile *keyfile, const char *identifier, const char *prefix)
2253 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2255 method = __connman_ipconfig_method2string(ipconfig->method);
2257 key = g_strdup_printf("%smethod", prefix);
2258 g_key_file_set_string(keyfile, identifier, key, method);
2261 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2262 const char *privacy;
2263 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2264 key = g_strdup_printf("%sprivacy", prefix);
2265 g_key_file_set_string(keyfile, identifier, key, privacy);
2269 switch (ipconfig->method) {
2270 case CONNMAN_IPCONFIG_METHOD_FIXED:
2271 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2273 case CONNMAN_IPCONFIG_METHOD_DHCP:
2274 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2275 if (ipconfig->last_dhcp_address != NULL &&
2276 strlen(ipconfig->last_dhcp_address) > 0)
2277 g_key_file_set_string(keyfile, identifier, key,
2278 ipconfig->last_dhcp_address);
2280 g_key_file_remove_key(keyfile, identifier, key, NULL);
2283 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2284 case CONNMAN_IPCONFIG_METHOD_OFF:
2285 case CONNMAN_IPCONFIG_METHOD_AUTO:
2289 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2290 g_key_file_set_integer(keyfile, identifier,
2291 key, ipconfig->address->prefixlen);
2294 key = g_strdup_printf("%slocal_address", prefix);
2295 if (ipconfig->address->local != NULL)
2296 g_key_file_set_string(keyfile, identifier,
2297 key, ipconfig->address->local);
2300 key = g_strdup_printf("%speer_address", prefix);
2301 if (ipconfig->address->peer != NULL)
2302 g_key_file_set_string(keyfile, identifier,
2303 key, ipconfig->address->peer);
2306 key = g_strdup_printf("%sbroadcast_address", prefix);
2307 if (ipconfig->address->broadcast != NULL)
2308 g_key_file_set_string(keyfile, identifier,
2309 key, ipconfig->address->broadcast);
2312 key = g_strdup_printf("%sgateway", prefix);
2313 if (ipconfig->address->gateway != NULL)
2314 g_key_file_set_string(keyfile, identifier,
2315 key, ipconfig->address->gateway);
2321 int __connman_ipconfig_init(void)
2325 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2326 NULL, free_ipdevice);
2331 void __connman_ipconfig_cleanup(void)
2335 g_hash_table_destroy(ipdevice_hash);
2336 ipdevice_hash = NULL;