5 * Copyright (C) 2007-2012 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 connman_bool_t enabled;
53 enum connman_ipconfig_method method;
54 struct connman_ipaddress *address;
55 struct connman_ipaddress *system;
57 int ipv6_privacy_config;
58 char *last_dhcp_address;
61 struct connman_ipdevice {
83 struct connman_ipconfig *config_ipv4;
84 struct connman_ipconfig *config_ipv6;
86 gboolean ipv6_enabled;
90 static GHashTable *ipdevice_hash = NULL;
91 static GList *ipconfig_list = NULL;
93 struct connman_ipaddress *connman_ipaddress_alloc(int family)
95 struct connman_ipaddress *ipaddress;
97 ipaddress = g_try_new0(struct connman_ipaddress, 1);
98 if (ipaddress == NULL)
101 ipaddress->family = family;
102 ipaddress->prefixlen = 0;
103 ipaddress->local = NULL;
104 ipaddress->peer = NULL;
105 ipaddress->broadcast = NULL;
106 ipaddress->gateway = NULL;
111 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
113 if (ipaddress == NULL)
116 g_free(ipaddress->broadcast);
117 g_free(ipaddress->peer);
118 g_free(ipaddress->local);
119 g_free(ipaddress->gateway);
123 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
132 mask = inet_network(netmask);
135 /* a valid netmask must be 2^n - 1 */
136 if ((host & (host + 1)) != 0)
140 for (; mask; mask <<= 1)
146 static gboolean check_ipv6_address(const char *address)
148 unsigned char buf[sizeof(struct in6_addr)];
154 err = inet_pton(AF_INET6, address, buf);
161 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
163 unsigned char prefix_length,
166 if (ipaddress == NULL)
169 if (check_ipv6_address(address) == FALSE)
172 if (check_ipv6_address(gateway) == FALSE)
175 DBG("prefix_len %d address %s gateway %s",
176 prefix_length, address, gateway);
178 ipaddress->family = AF_INET6;
180 ipaddress->prefixlen = prefix_length;
182 g_free(ipaddress->local);
183 ipaddress->local = g_strdup(address);
185 g_free(ipaddress->gateway);
186 ipaddress->gateway = g_strdup(gateway);
191 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
192 const char *address, const char *netmask, const char *gateway)
194 if (ipaddress == NULL)
197 ipaddress->family = AF_INET;
199 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
201 g_free(ipaddress->local);
202 ipaddress->local = g_strdup(address);
204 g_free(ipaddress->gateway);
205 ipaddress->gateway = g_strdup(gateway);
210 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
213 if (ipaddress == NULL)
216 g_free(ipaddress->peer);
217 ipaddress->peer = g_strdup(peer);
220 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
222 if (ipaddress == NULL)
225 ipaddress->prefixlen = 0;
227 g_free(ipaddress->local);
228 ipaddress->local = NULL;
230 g_free(ipaddress->peer);
231 ipaddress->peer = NULL;
233 g_free(ipaddress->broadcast);
234 ipaddress->broadcast = NULL;
236 g_free(ipaddress->gateway);
237 ipaddress->gateway = NULL;
240 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
241 struct connman_ipaddress *source)
243 if (ipaddress == NULL || source == NULL)
246 ipaddress->family = source->family;
247 ipaddress->prefixlen = source->prefixlen;
249 g_free(ipaddress->local);
250 ipaddress->local = g_strdup(source->local);
252 g_free(ipaddress->peer);
253 ipaddress->peer = g_strdup(source->peer);
255 g_free(ipaddress->broadcast);
256 ipaddress->broadcast = g_strdup(source->broadcast);
258 g_free(ipaddress->gateway);
259 ipaddress->gateway = g_strdup(source->gateway);
262 static void free_address_list(struct connman_ipdevice *ipdevice)
266 for (list = ipdevice->address_list; list; list = list->next) {
267 struct connman_ipaddress *ipaddress = list->data;
269 connman_ipaddress_free(ipaddress);
273 g_slist_free(ipdevice->address_list);
274 ipdevice->address_list = NULL;
277 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
278 unsigned char prefixlen, const char *local)
282 for (list = ipdevice->address_list; list; list = list->next) {
283 struct connman_ipaddress *ipaddress = list->data;
285 if (g_strcmp0(ipaddress->local, local) == 0 &&
286 ipaddress->prefixlen == prefixlen)
293 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
296 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
298 case CONNMAN_IPCONFIG_TYPE_IPV4:
300 case CONNMAN_IPCONFIG_TYPE_IPV6:
307 static const char *type2str(unsigned short type)
312 case ARPHRD_LOOPBACK:
325 static const char *scope2str(unsigned char scope)
337 static gboolean get_ipv6_state(gchar *ifname)
342 gboolean enabled = FALSE;
345 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
347 path = g_strdup_printf(
348 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
353 f = fopen(path, "r");
358 if (fscanf(f, "%d", &disabled) > 0)
366 static void set_ipv6_state(gchar *ifname, gboolean enable)
372 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
374 path = g_strdup_printf(
375 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
380 f = fopen(path, "r+");
395 static int get_ipv6_privacy(gchar *ifname)
404 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
410 f = fopen(path, "r");
417 if (fscanf(f, "%d", &value) <= 0)
425 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
426 * The privacy extension is described in RFC 3041 and RFC 4941
428 static void set_ipv6_privacy(gchar *ifname, int value)
436 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
445 f = fopen(path, "r+");
452 fprintf(f, "%d", value);
456 static int get_rp_filter()
459 int value = -EINVAL, tmp;
461 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
464 if (fscanf(f, "%d", &tmp) == 1)
472 static void set_rp_filter(int value)
476 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
481 fprintf(f, "%d", value);
486 int __connman_ipconfig_set_rp_filter()
490 value = get_rp_filter();
497 connman_info("rp_filter set to 2 (loose mode routing), "
498 "old value was %d", value);
503 void __connman_ipconfig_unset_rp_filter(int old_value)
505 set_rp_filter(old_value);
507 connman_info("rp_filter restored to %d", old_value);
510 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
512 if (ipconfig == NULL)
515 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
518 static void free_ipdevice(gpointer data)
520 struct connman_ipdevice *ipdevice = data;
522 connman_info("%s {remove} index %d", ipdevice->ifname,
525 if (ipdevice->config_ipv4 != NULL) {
526 __connman_ipconfig_unref(ipdevice->config_ipv4);
527 ipdevice->config_ipv4 = NULL;
530 if (ipdevice->config_ipv6 != NULL) {
531 __connman_ipconfig_unref(ipdevice->config_ipv6);
532 ipdevice->config_ipv6 = NULL;
535 free_address_list(ipdevice);
536 g_free(ipdevice->ipv4_gateway);
537 g_free(ipdevice->ipv6_gateway);
538 g_free(ipdevice->pac);
540 g_free(ipdevice->address);
542 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
543 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
545 g_free(ipdevice->ifname);
549 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
551 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
552 ipdevice->config_ipv6);
554 if (ipdevice->config_ipv6 != NULL &&
555 ipdevice->config_ipv6->enabled == TRUE)
558 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
559 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
560 set_ipv6_state(ipdevice->ifname, FALSE);
564 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
566 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
567 ipdevice->config_ipv6);
569 if (ipdevice->config_ipv4)
570 connman_inet_clear_address(ipdevice->index,
571 ipdevice->config_ipv4->address);
573 if (ipdevice->config_ipv6)
574 connman_inet_clear_ipv6_address(ipdevice->index,
575 ipdevice->config_ipv6->address->local,
576 ipdevice->config_ipv6->address->prefixlen);
579 static void update_stats(struct connman_ipdevice *ipdevice,
580 struct rtnl_link_stats *stats)
582 struct connman_service *service;
584 if (stats->rx_packets == 0 && stats->tx_packets == 0)
587 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
588 stats->rx_packets, stats->rx_bytes);
589 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
590 stats->tx_packets, stats->tx_bytes);
592 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
595 if (ipdevice->config_ipv4)
596 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
597 else if (ipdevice->config_ipv6)
598 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
605 ipdevice->rx_packets = stats->rx_packets;
606 ipdevice->tx_packets = stats->tx_packets;
607 ipdevice->rx_bytes = stats->rx_bytes;
608 ipdevice->tx_bytes = stats->tx_bytes;
609 ipdevice->rx_errors = stats->rx_errors;
610 ipdevice->tx_errors = stats->tx_errors;
611 ipdevice->rx_dropped = stats->rx_dropped;
612 ipdevice->tx_dropped = stats->tx_dropped;
614 __connman_service_notify(service,
615 ipdevice->rx_packets, ipdevice->tx_packets,
616 ipdevice->rx_bytes, ipdevice->tx_bytes,
617 ipdevice->rx_errors, ipdevice->tx_errors,
618 ipdevice->rx_dropped, ipdevice->tx_dropped);
621 void __connman_ipconfig_newlink(int index, unsigned short type,
622 unsigned int flags, const char *address,
624 struct rtnl_link_stats *stats)
626 struct connman_ipdevice *ipdevice;
629 gboolean up = FALSE, down = FALSE;
630 gboolean lower_up = FALSE, lower_down = FALSE;
632 DBG("index %d", index);
634 if (type == ARPHRD_LOOPBACK)
637 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
638 if (ipdevice != NULL)
641 ipdevice = g_try_new0(struct connman_ipdevice, 1);
642 if (ipdevice == NULL)
645 ipdevice->index = index;
646 ipdevice->ifname = connman_inet_ifname(index);
647 ipdevice->type = type;
649 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
650 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
652 ipdevice->address = g_strdup(address);
654 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
656 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
657 index, type, type2str(type));
662 update_stats(ipdevice, stats);
664 if (flags == ipdevice->flags)
667 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
674 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
675 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
676 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
677 (IFF_RUNNING | IFF_LOWER_UP))
679 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
683 ipdevice->flags = flags;
685 str = g_string_new(NULL);
690 g_string_append(str, "UP");
692 g_string_append(str, "DOWN");
694 if (flags & IFF_RUNNING)
695 g_string_append(str, ",RUNNING");
697 if (flags & IFF_LOWER_UP)
698 g_string_append(str, ",LOWER_UP");
700 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
703 g_string_free(str, TRUE);
705 for (list = g_list_first(ipconfig_list); list;
706 list = g_list_next(list)) {
707 struct connman_ipconfig *ipconfig = list->data;
709 if (index != ipconfig->index)
712 if (ipconfig->ops == NULL)
715 if (up == TRUE && ipconfig->ops->up)
716 ipconfig->ops->up(ipconfig);
717 if (lower_up == TRUE && ipconfig->ops->lower_up)
718 ipconfig->ops->lower_up(ipconfig);
720 if (lower_down == TRUE && ipconfig->ops->lower_down)
721 ipconfig->ops->lower_down(ipconfig);
722 if (down == TRUE && ipconfig->ops->down)
723 ipconfig->ops->down(ipconfig);
727 __connman_ipconfig_lower_up(ipdevice);
729 __connman_ipconfig_lower_down(ipdevice);
732 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
734 struct connman_ipdevice *ipdevice;
737 DBG("index %d", index);
739 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
740 if (ipdevice == NULL)
743 update_stats(ipdevice, stats);
745 for (list = g_list_first(ipconfig_list); list;
746 list = g_list_next(list)) {
747 struct connman_ipconfig *ipconfig = list->data;
749 if (index != ipconfig->index)
752 ipconfig->index = -1;
754 if (ipconfig->ops == NULL)
757 if (ipconfig->ops->lower_down)
758 ipconfig->ops->lower_down(ipconfig);
759 if (ipconfig->ops->down)
760 ipconfig->ops->down(ipconfig);
763 __connman_ipconfig_lower_down(ipdevice);
765 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
768 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
770 const struct connman_ipaddress *addr1 = a;
771 const struct connman_ipaddress *addr2 = b;
773 if (addr1->prefixlen != addr2->prefixlen)
774 return addr2->prefixlen - addr1->prefixlen;
776 return g_strcmp0(addr1->local, addr2->local);
779 void __connman_ipconfig_newaddr(int index, int family, const char *label,
780 unsigned char prefixlen, const char *address)
782 struct connman_ipdevice *ipdevice;
783 struct connman_ipaddress *ipaddress;
784 enum connman_ipconfig_type type;
787 DBG("index %d", index);
789 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
790 if (ipdevice == NULL)
793 ipaddress = connman_ipaddress_alloc(family);
794 if (ipaddress == NULL)
797 ipaddress->prefixlen = prefixlen;
798 ipaddress->local = g_strdup(address);
800 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
801 check_duplicate_address)) {
802 connman_ipaddress_free(ipaddress);
806 if (family == AF_INET)
807 type = CONNMAN_IPCONFIG_TYPE_IPV4;
808 else if (family == AF_INET6)
809 type = CONNMAN_IPCONFIG_TYPE_IPV6;
813 ipdevice->address_list = g_slist_append(ipdevice->address_list,
816 connman_info("%s {add} address %s/%u label %s family %d",
817 ipdevice->ifname, address, prefixlen, label, family);
819 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
820 __connman_ippool_newaddr(index, address, prefixlen);
822 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
823 connman_ipaddress_copy(ipdevice->config_ipv4->system,
826 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
827 connman_ipaddress_copy(ipdevice->config_ipv6->system,
832 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
835 for (list = g_list_first(ipconfig_list); list;
836 list = g_list_next(list)) {
837 struct connman_ipconfig *ipconfig = list->data;
839 if (index != ipconfig->index)
842 if (type != ipconfig->type)
845 if (ipconfig->ops == NULL)
848 if (ipconfig->ops->ip_bound)
849 ipconfig->ops->ip_bound(ipconfig);
853 void __connman_ipconfig_deladdr(int index, int family, const char *label,
854 unsigned char prefixlen, const char *address)
856 struct connman_ipdevice *ipdevice;
857 struct connman_ipaddress *ipaddress;
858 enum connman_ipconfig_type type;
861 DBG("index %d", index);
863 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
864 if (ipdevice == NULL)
867 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
868 if (ipaddress == NULL)
871 if (family == AF_INET)
872 type = CONNMAN_IPCONFIG_TYPE_IPV4;
873 else if (family == AF_INET6)
874 type = CONNMAN_IPCONFIG_TYPE_IPV6;
878 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
881 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
882 __connman_ippool_deladdr(index, address, prefixlen);
884 connman_ipaddress_clear(ipaddress);
887 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
888 address, prefixlen, label);
890 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
893 if (g_slist_length(ipdevice->address_list) > 0)
896 for (list = g_list_first(ipconfig_list); list;
897 list = g_list_next(list)) {
898 struct connman_ipconfig *ipconfig = list->data;
900 if (index != ipconfig->index)
903 if (type != ipconfig->type)
906 if (ipconfig->ops == NULL)
909 if (ipconfig->ops->ip_release)
910 ipconfig->ops->ip_release(ipconfig);
914 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
915 const char *dst, const char *gateway)
917 struct connman_ipdevice *ipdevice;
919 DBG("index %d", index);
921 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
922 if (ipdevice == NULL)
925 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
926 g_strcmp0(dst, "::") == 0)) {
928 enum connman_ipconfig_type type;
930 if (family == AF_INET6) {
931 type = CONNMAN_IPCONFIG_TYPE_IPV6;
932 g_free(ipdevice->ipv6_gateway);
933 ipdevice->ipv6_gateway = g_strdup(gateway);
935 if (ipdevice->config_ipv6 != NULL &&
936 ipdevice->config_ipv6->system != NULL) {
937 g_free(ipdevice->config_ipv6->system->gateway);
938 ipdevice->config_ipv6->system->gateway =
941 } else if (family == AF_INET) {
942 type = CONNMAN_IPCONFIG_TYPE_IPV4;
943 g_free(ipdevice->ipv4_gateway);
944 ipdevice->ipv4_gateway = g_strdup(gateway);
946 if (ipdevice->config_ipv4 != NULL &&
947 ipdevice->config_ipv4->system != NULL) {
948 g_free(ipdevice->config_ipv4->system->gateway);
949 ipdevice->config_ipv4->system->gateway =
955 for (config_list = g_list_first(ipconfig_list); config_list;
956 config_list = g_list_next(config_list)) {
957 struct connman_ipconfig *ipconfig = config_list->data;
959 if (index != ipconfig->index)
962 if (type != ipconfig->type)
965 if (ipconfig->ops == NULL)
968 if (ipconfig->ops->route_set)
969 ipconfig->ops->route_set(ipconfig);
973 connman_info("%s {add} route %s gw %s scope %u <%s>",
974 ipdevice->ifname, dst, gateway,
975 scope, scope2str(scope));
978 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
979 const char *dst, const char *gateway)
981 struct connman_ipdevice *ipdevice;
983 DBG("index %d", index);
985 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
986 if (ipdevice == NULL)
989 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
990 g_strcmp0(dst, "::") == 0)) {
992 enum connman_ipconfig_type type;
994 if (family == AF_INET6) {
995 type = CONNMAN_IPCONFIG_TYPE_IPV6;
996 g_free(ipdevice->ipv6_gateway);
997 ipdevice->ipv6_gateway = NULL;
999 if (ipdevice->config_ipv6 != NULL &&
1000 ipdevice->config_ipv6->system != NULL) {
1001 g_free(ipdevice->config_ipv6->system->gateway);
1002 ipdevice->config_ipv6->system->gateway = NULL;
1004 } else if (family == AF_INET) {
1005 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1006 g_free(ipdevice->ipv4_gateway);
1007 ipdevice->ipv4_gateway = NULL;
1009 if (ipdevice->config_ipv4 != NULL &&
1010 ipdevice->config_ipv4->system != NULL) {
1011 g_free(ipdevice->config_ipv4->system->gateway);
1012 ipdevice->config_ipv4->system->gateway = NULL;
1017 for (config_list = g_list_first(ipconfig_list); config_list;
1018 config_list = g_list_next(config_list)) {
1019 struct connman_ipconfig *ipconfig = config_list->data;
1021 if (index != ipconfig->index)
1024 if (type != ipconfig->type)
1027 if (ipconfig->ops == NULL)
1030 if (ipconfig->ops->route_unset)
1031 ipconfig->ops->route_unset(ipconfig);
1035 connman_info("%s {del} route %s gw %s scope %u <%s>",
1036 ipdevice->ifname, dst, gateway,
1037 scope, scope2str(scope));
1040 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1045 keys = g_hash_table_get_keys(ipdevice_hash);
1049 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1050 int index = GPOINTER_TO_INT(list->data);
1052 function(index, user_data);
1058 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1059 struct connman_ipconfig *ipconfig)
1061 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1064 unsigned short __connman_ipconfig_get_type_from_index(int index)
1066 struct connman_ipdevice *ipdevice;
1068 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1069 if (ipdevice == NULL)
1072 return ipdevice->type;
1075 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1077 struct connman_ipdevice *ipdevice;
1079 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1080 if (ipdevice == NULL)
1083 return ipdevice->flags;
1086 const char *__connman_ipconfig_get_gateway_from_index(int index,
1087 enum connman_ipconfig_type type)
1089 struct connman_ipdevice *ipdevice;
1091 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1092 if (ipdevice == NULL)
1095 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1096 if (ipdevice->ipv4_gateway != NULL)
1097 return ipdevice->ipv4_gateway;
1099 if (ipdevice->config_ipv4 != NULL &&
1100 ipdevice->config_ipv4->address != NULL)
1101 return ipdevice->config_ipv4->address->gateway;
1104 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1105 if (ipdevice->ipv6_gateway != NULL)
1106 return ipdevice->ipv6_gateway;
1108 if (ipdevice->config_ipv6 != NULL &&
1109 ipdevice->config_ipv6->address != NULL)
1110 return ipdevice->config_ipv6->address->gateway;
1116 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1118 ipconfig->index = index;
1121 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1123 if (ipconfig->address == NULL)
1126 return ipconfig->address->local;
1129 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1131 if (ipconfig->address == NULL)
1134 g_free(ipconfig->address->local);
1135 ipconfig->address->local = g_strdup(address);
1138 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1140 if (ipconfig->address == NULL)
1143 return ipconfig->address->peer;
1146 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1148 if (ipconfig->address == NULL)
1151 g_free(ipconfig->address->peer);
1152 ipconfig->address->peer = g_strdup(address);
1155 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1157 if (ipconfig->address == NULL)
1160 return ipconfig->address->broadcast;
1163 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1165 if (ipconfig->address == NULL)
1168 g_free(ipconfig->address->broadcast);
1169 ipconfig->address->broadcast = g_strdup(broadcast);
1172 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1174 if (ipconfig->address == NULL)
1177 return ipconfig->address->gateway;
1180 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1184 if (ipconfig->address == NULL)
1186 g_free(ipconfig->address->gateway);
1187 ipconfig->address->gateway = g_strdup(gateway);
1190 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1192 struct connman_service *service;
1196 if (ipconfig->address == NULL)
1199 service = __connman_service_lookup_from_index(ipconfig->index);
1200 if (service == NULL)
1203 __connman_connection_gateway_remove(service, ipconfig->type);
1205 DBG("type %d gw %s peer %s", ipconfig->type,
1206 ipconfig->address->gateway, ipconfig->address->peer);
1208 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1209 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1210 return __connman_connection_gateway_add(service,
1211 ipconfig->address->gateway,
1213 ipconfig->address->peer);
1218 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1220 struct connman_service *service;
1224 service = __connman_service_lookup_from_index(ipconfig->index);
1225 if (service != NULL)
1226 __connman_connection_gateway_remove(service, ipconfig->type);
1229 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1231 if (ipconfig->address == NULL)
1234 return ipconfig->address->prefixlen;
1237 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1239 if (ipconfig->address == NULL)
1242 ipconfig->address->prefixlen = prefixlen;
1245 static struct connman_ipconfig *create_ipv6config(int index)
1247 struct connman_ipconfig *ipv6config;
1248 struct connman_ipdevice *ipdevice;
1250 DBG("index %d", index);
1252 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1253 if (ipv6config == NULL)
1256 ipv6config->refcount = 1;
1258 ipv6config->index = index;
1259 ipv6config->enabled = FALSE;
1260 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1261 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1263 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1264 if (ipdevice != NULL)
1265 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
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->enabled = FALSE;
1305 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1307 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1308 if (ipconfig->address == NULL) {
1313 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1315 DBG("ipconfig %p", ipconfig);
1322 * connman_ipconfig_ref:
1323 * @ipconfig: ipconfig structure
1325 * Increase reference counter of ipconfig
1327 struct connman_ipconfig *
1328 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1329 const char *file, int line, const char *caller)
1331 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1332 file, line, caller);
1334 __sync_fetch_and_add(&ipconfig->refcount, 1);
1340 * connman_ipconfig_unref:
1341 * @ipconfig: ipconfig structure
1343 * Decrease reference counter of ipconfig
1345 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1346 const char *file, int line, const char *caller)
1348 if (ipconfig == NULL)
1351 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1352 file, line, caller);
1354 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1357 if (__connman_ipconfig_disable(ipconfig) < 0)
1358 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1360 __connman_ipconfig_set_ops(ipconfig, NULL);
1362 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1363 __connman_ipconfig_unref(ipconfig->origin);
1364 ipconfig->origin = NULL;
1367 connman_ipaddress_free(ipconfig->system);
1368 connman_ipaddress_free(ipconfig->address);
1369 g_free(ipconfig->last_dhcp_address);
1374 * connman_ipconfig_get_data:
1375 * @ipconfig: ipconfig structure
1377 * Get private data pointer
1379 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1381 if (ipconfig == NULL)
1384 return ipconfig->ops_data;
1388 * connman_ipconfig_set_data:
1389 * @ipconfig: ipconfig structure
1390 * @data: data pointer
1392 * Set private data pointer
1394 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1396 ipconfig->ops_data = data;
1400 * connman_ipconfig_get_index:
1401 * @ipconfig: ipconfig structure
1403 * Get interface index
1405 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1407 if (ipconfig == NULL)
1410 if (ipconfig->origin != NULL)
1411 return ipconfig->origin->index;
1413 return ipconfig->index;
1417 * connman_ipconfig_get_ifname:
1418 * @ipconfig: ipconfig structure
1420 * Get interface name
1422 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1424 struct connman_ipdevice *ipdevice;
1426 if (ipconfig == NULL)
1429 if (ipconfig->index < 0)
1432 ipdevice = g_hash_table_lookup(ipdevice_hash,
1433 GINT_TO_POINTER(ipconfig->index));
1434 if (ipdevice == NULL)
1437 return ipdevice->ifname;
1441 * connman_ipconfig_set_ops:
1442 * @ipconfig: ipconfig structure
1443 * @ops: operation callbacks
1445 * Set the operation callbacks
1447 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1448 const struct connman_ipconfig_ops *ops)
1450 ipconfig->ops = ops;
1454 * connman_ipconfig_set_method:
1455 * @ipconfig: ipconfig structure
1456 * @method: configuration method
1458 * Set the configuration method
1460 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1461 enum connman_ipconfig_method method)
1463 ipconfig->method = method;
1468 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1470 if (ipconfig == NULL)
1471 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1473 return ipconfig->method;
1476 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1480 switch (ipconfig->method) {
1481 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1482 case CONNMAN_IPCONFIG_METHOD_OFF:
1484 case CONNMAN_IPCONFIG_METHOD_AUTO:
1485 case CONNMAN_IPCONFIG_METHOD_FIXED:
1486 case CONNMAN_IPCONFIG_METHOD_DHCP:
1487 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1488 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1489 return connman_inet_set_address(ipconfig->index,
1491 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1492 return connman_inet_set_ipv6_address(
1493 ipconfig->index, ipconfig->address);
1499 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1505 if (ipconfig == NULL)
1508 DBG("method %d", ipconfig->method);
1510 switch (ipconfig->method) {
1511 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1512 case CONNMAN_IPCONFIG_METHOD_OFF:
1514 case CONNMAN_IPCONFIG_METHOD_AUTO:
1515 case CONNMAN_IPCONFIG_METHOD_FIXED:
1516 case CONNMAN_IPCONFIG_METHOD_DHCP:
1517 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1518 err = __connman_ipconfig_address_unset(ipconfig);
1519 connman_ipaddress_clear(ipconfig->address);
1527 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1533 if (ipconfig == NULL)
1536 DBG("method %d", ipconfig->method);
1538 switch (ipconfig->method) {
1539 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1540 case CONNMAN_IPCONFIG_METHOD_OFF:
1542 case CONNMAN_IPCONFIG_METHOD_AUTO:
1543 case CONNMAN_IPCONFIG_METHOD_FIXED:
1544 case CONNMAN_IPCONFIG_METHOD_DHCP:
1545 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1546 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1547 err = connman_inet_clear_address(ipconfig->index,
1549 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1550 err = connman_inet_clear_ipv6_address(
1552 ipconfig->address->local,
1553 ipconfig->address->prefixlen);
1563 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1566 struct connman_ipdevice *ipdevice;
1568 DBG("ipconfig %p", ipconfig);
1570 if (ipconfig == NULL || ipconfig->index < 0)
1573 ipdevice = g_hash_table_lookup(ipdevice_hash,
1574 GINT_TO_POINTER(ipconfig->index));
1575 if (ipdevice == NULL)
1578 g_free(ipdevice->pac);
1579 ipdevice->pac = g_strdup(url);
1584 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1586 struct connman_ipdevice *ipdevice;
1588 DBG("ipconfig %p", ipconfig);
1590 if (ipconfig == NULL || ipconfig->index < 0)
1593 ipdevice = g_hash_table_lookup(ipdevice_hash,
1594 GINT_TO_POINTER(ipconfig->index));
1595 if (ipdevice == NULL)
1598 return ipdevice->pac;
1601 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1602 const char *address)
1604 if (ipconfig == NULL)
1607 g_free(ipconfig->last_dhcp_address);
1608 ipconfig->last_dhcp_address = g_strdup(address);
1611 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1613 if (ipconfig == NULL)
1616 return ipconfig->last_dhcp_address;
1619 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1621 struct connman_ipdevice *ipdevice;
1625 ipdevice = g_hash_table_lookup(ipdevice_hash,
1626 GINT_TO_POINTER(ipconfig->index));
1627 if (ipdevice == NULL)
1630 set_ipv6_state(ipdevice->ifname, FALSE);
1633 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1635 struct connman_ipdevice *ipdevice;
1639 ipdevice = g_hash_table_lookup(ipdevice_hash,
1640 GINT_TO_POINTER(ipconfig->index));
1641 if (ipdevice == NULL)
1644 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1645 set_ipv6_privacy(ipdevice->ifname,
1646 ipconfig->ipv6_privacy_config);
1648 set_ipv6_state(ipdevice->ifname, TRUE);
1651 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1653 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1656 enable_ipv6(ipconfig);
1659 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1661 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1664 disable_ipv6(ipconfig);
1667 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1669 struct connman_ipdevice *ipdevice;
1670 gboolean up = FALSE, down = FALSE;
1671 gboolean lower_up = FALSE, lower_down = FALSE;
1672 enum connman_ipconfig_type type;
1674 DBG("ipconfig %p", ipconfig);
1676 if (ipconfig == NULL || ipconfig->index < 0)
1679 ipdevice = g_hash_table_lookup(ipdevice_hash,
1680 GINT_TO_POINTER(ipconfig->index));
1681 if (ipdevice == NULL)
1684 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1685 if (ipdevice->config_ipv4 == ipconfig)
1687 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1688 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1689 if (ipdevice->config_ipv6 == ipconfig)
1691 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1692 enable_ipv6(ipconfig);
1696 ipconfig->enabled = TRUE;
1698 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1699 ipdevice->config_ipv4 != NULL) {
1700 ipconfig_list = g_list_remove(ipconfig_list,
1701 ipdevice->config_ipv4);
1703 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1705 __connman_ipconfig_unref(ipdevice->config_ipv4);
1708 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1709 ipdevice->config_ipv6 != NULL) {
1710 ipconfig_list = g_list_remove(ipconfig_list,
1711 ipdevice->config_ipv6);
1713 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1715 __connman_ipconfig_unref(ipdevice->config_ipv6);
1718 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1719 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1720 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1721 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1723 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1725 if (ipdevice->flags & IFF_UP)
1730 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1731 (IFF_RUNNING | IFF_LOWER_UP))
1733 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1736 if (up == TRUE && ipconfig->ops->up)
1737 ipconfig->ops->up(ipconfig);
1738 if (lower_up == TRUE && ipconfig->ops->lower_up)
1739 ipconfig->ops->lower_up(ipconfig);
1741 if (lower_down == TRUE && ipconfig->ops->lower_down)
1742 ipconfig->ops->lower_down(ipconfig);
1743 if (down == TRUE && ipconfig->ops->down)
1744 ipconfig->ops->down(ipconfig);
1749 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1751 struct connman_ipdevice *ipdevice;
1753 DBG("ipconfig %p", ipconfig);
1755 if (ipconfig == NULL || ipconfig->index < 0)
1758 ipdevice = g_hash_table_lookup(ipdevice_hash,
1759 GINT_TO_POINTER(ipconfig->index));
1760 if (ipdevice == NULL)
1763 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1766 ipconfig->enabled = FALSE;
1768 if (ipdevice->config_ipv4 == ipconfig) {
1769 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1771 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1772 __connman_ipconfig_unref(ipdevice->config_ipv4);
1773 ipdevice->config_ipv4 = NULL;
1777 if (ipdevice->config_ipv6 == ipconfig) {
1778 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1780 if (ipdevice->config_ipv6->method ==
1781 CONNMAN_IPCONFIG_METHOD_AUTO)
1782 disable_ipv6(ipdevice->config_ipv6);
1784 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1785 __connman_ipconfig_unref(ipdevice->config_ipv6);
1786 ipdevice->config_ipv6 = NULL;
1793 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1796 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1798 case CONNMAN_IPCONFIG_METHOD_OFF:
1800 case CONNMAN_IPCONFIG_METHOD_FIXED:
1802 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1804 case CONNMAN_IPCONFIG_METHOD_DHCP:
1806 case CONNMAN_IPCONFIG_METHOD_AUTO:
1813 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1815 if (g_strcmp0(method, "off") == 0)
1816 return CONNMAN_IPCONFIG_METHOD_OFF;
1817 else if (g_strcmp0(method, "fixed") == 0)
1818 return CONNMAN_IPCONFIG_METHOD_FIXED;
1819 else if (g_strcmp0(method, "manual") == 0)
1820 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1821 else if (g_strcmp0(method, "dhcp") == 0)
1822 return CONNMAN_IPCONFIG_METHOD_DHCP;
1823 else if (g_strcmp0(method, "auto") == 0)
1824 return CONNMAN_IPCONFIG_METHOD_AUTO;
1826 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1829 static const char *privacy2string(int privacy)
1833 else if (privacy == 1)
1835 else if (privacy > 1)
1841 static int string2privacy(const char *privacy)
1843 if (g_strcmp0(privacy, "disabled") == 0)
1845 else if (g_strcmp0(privacy, "enabled") == 0)
1847 else if (g_strcmp0(privacy, "prefered") == 0)
1853 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1854 DBusMessageIter *iter)
1856 struct connman_ipaddress *append_addr = NULL;
1861 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1864 str = __connman_ipconfig_method2string(ipconfig->method);
1868 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1870 append_addr = ipconfig->system;
1872 switch (ipconfig->method) {
1873 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1874 case CONNMAN_IPCONFIG_METHOD_OFF:
1877 case CONNMAN_IPCONFIG_METHOD_FIXED:
1878 if (append_addr == NULL)
1879 append_addr = ipconfig->address;
1882 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1883 case CONNMAN_IPCONFIG_METHOD_DHCP:
1884 case CONNMAN_IPCONFIG_METHOD_AUTO:
1888 if (append_addr == NULL)
1891 if (append_addr->local != NULL) {
1893 struct in_addr netmask;
1896 connman_dbus_dict_append_basic(iter, "Address",
1897 DBUS_TYPE_STRING, &append_addr->local);
1899 addr = 0xffffffff << (32 - append_addr->prefixlen);
1900 netmask.s_addr = htonl(addr);
1901 mask = inet_ntoa(netmask);
1902 connman_dbus_dict_append_basic(iter, "Netmask",
1903 DBUS_TYPE_STRING, &mask);
1906 if (append_addr->gateway != NULL)
1907 connman_dbus_dict_append_basic(iter, "Gateway",
1908 DBUS_TYPE_STRING, &append_addr->gateway);
1911 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1912 DBusMessageIter *iter,
1913 struct connman_ipconfig *ipconfig_ipv4)
1915 struct connman_ipaddress *append_addr = NULL;
1916 const char *str, *privacy;
1920 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1923 str = __connman_ipconfig_method2string(ipconfig->method);
1927 if (ipconfig_ipv4 != NULL &&
1928 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1929 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1933 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1935 append_addr = ipconfig->system;
1937 switch (ipconfig->method) {
1938 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1939 case CONNMAN_IPCONFIG_METHOD_OFF:
1942 case CONNMAN_IPCONFIG_METHOD_FIXED:
1943 if (append_addr == NULL)
1944 append_addr = ipconfig->address;
1947 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1948 case CONNMAN_IPCONFIG_METHOD_DHCP:
1949 case CONNMAN_IPCONFIG_METHOD_AUTO:
1953 if (append_addr == NULL)
1956 if (append_addr->local != NULL) {
1957 connman_dbus_dict_append_basic(iter, "Address",
1958 DBUS_TYPE_STRING, &append_addr->local);
1959 connman_dbus_dict_append_basic(iter, "PrefixLength",
1961 &append_addr->prefixlen);
1964 if (append_addr->gateway != NULL)
1965 connman_dbus_dict_append_basic(iter, "Gateway",
1966 DBUS_TYPE_STRING, &append_addr->gateway);
1968 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1969 connman_dbus_dict_append_basic(iter, "Privacy",
1970 DBUS_TYPE_STRING, &privacy);
1973 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1974 DBusMessageIter *iter)
1976 const char *str, *privacy;
1980 str = __connman_ipconfig_method2string(ipconfig->method);
1984 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1986 switch (ipconfig->method) {
1987 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1988 case CONNMAN_IPCONFIG_METHOD_OFF:
1989 case CONNMAN_IPCONFIG_METHOD_DHCP:
1991 case CONNMAN_IPCONFIG_METHOD_FIXED:
1992 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1993 case CONNMAN_IPCONFIG_METHOD_AUTO:
1997 if (ipconfig->address == NULL)
2000 if (ipconfig->address->local != NULL) {
2001 connman_dbus_dict_append_basic(iter, "Address",
2002 DBUS_TYPE_STRING, &ipconfig->address->local);
2003 connman_dbus_dict_append_basic(iter, "PrefixLength",
2005 &ipconfig->address->prefixlen);
2008 if (ipconfig->address->gateway != NULL)
2009 connman_dbus_dict_append_basic(iter, "Gateway",
2010 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2012 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2013 connman_dbus_dict_append_basic(iter, "Privacy",
2014 DBUS_TYPE_STRING, &privacy);
2017 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2018 DBusMessageIter *iter)
2024 str = __connman_ipconfig_method2string(ipconfig->method);
2028 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2030 switch (ipconfig->method) {
2031 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2032 case CONNMAN_IPCONFIG_METHOD_OFF:
2033 case CONNMAN_IPCONFIG_METHOD_DHCP:
2034 case CONNMAN_IPCONFIG_METHOD_AUTO:
2036 case CONNMAN_IPCONFIG_METHOD_FIXED:
2037 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2041 if (ipconfig->address == NULL)
2044 if (ipconfig->address->local != NULL) {
2046 struct in_addr netmask;
2049 connman_dbus_dict_append_basic(iter, "Address",
2050 DBUS_TYPE_STRING, &ipconfig->address->local);
2052 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2053 netmask.s_addr = htonl(addr);
2054 mask = inet_ntoa(netmask);
2055 connman_dbus_dict_append_basic(iter, "Netmask",
2056 DBUS_TYPE_STRING, &mask);
2059 if (ipconfig->address->gateway != NULL)
2060 connman_dbus_dict_append_basic(iter, "Gateway",
2061 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2064 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2065 DBusMessageIter *array)
2067 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2068 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2069 *prefix_length_string = NULL, *privacy_string = NULL;
2070 int prefix_length = 0, privacy = 0;
2071 DBusMessageIter dict;
2073 DBG("ipconfig %p", ipconfig);
2075 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2078 dbus_message_iter_recurse(array, &dict);
2080 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2081 DBusMessageIter entry, value;
2085 dbus_message_iter_recurse(&dict, &entry);
2087 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2090 dbus_message_iter_get_basic(&entry, &key);
2091 dbus_message_iter_next(&entry);
2093 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2096 dbus_message_iter_recurse(&entry, &value);
2098 type = dbus_message_iter_get_arg_type(&value);
2100 if (g_str_equal(key, "Method") == TRUE) {
2103 if (type != DBUS_TYPE_STRING)
2106 dbus_message_iter_get_basic(&value, &str);
2107 method = __connman_ipconfig_string2method(str);
2108 } else if (g_str_equal(key, "Address") == TRUE) {
2109 if (type != DBUS_TYPE_STRING)
2112 dbus_message_iter_get_basic(&value, &address);
2113 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2114 if (type != DBUS_TYPE_STRING)
2117 dbus_message_iter_get_basic(&value,
2118 &prefix_length_string);
2120 prefix_length = atoi(prefix_length_string);
2121 if (prefix_length < 0 || prefix_length > 128)
2123 } else if (g_str_equal(key, "Netmask") == TRUE) {
2124 if (type != DBUS_TYPE_STRING)
2127 dbus_message_iter_get_basic(&value, &netmask);
2128 } else if (g_str_equal(key, "Gateway") == TRUE) {
2129 if (type != DBUS_TYPE_STRING)
2132 dbus_message_iter_get_basic(&value, &gateway);
2133 } else if (g_str_equal(key, "Privacy") == TRUE) {
2134 if (type != DBUS_TYPE_STRING)
2137 dbus_message_iter_get_basic(&value, &privacy_string);
2138 privacy = string2privacy(privacy_string);
2141 dbus_message_iter_next(&dict);
2144 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2146 method, address, netmask, gateway, prefix_length,
2150 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2151 case CONNMAN_IPCONFIG_METHOD_FIXED:
2154 case CONNMAN_IPCONFIG_METHOD_OFF:
2155 ipconfig->method = method;
2156 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2157 disable_ipv6(ipconfig);
2160 case CONNMAN_IPCONFIG_METHOD_AUTO:
2161 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2164 ipconfig->method = method;
2165 if (privacy_string != NULL)
2166 ipconfig->ipv6_privacy_config = privacy;
2167 enable_ipv6(ipconfig);
2170 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2171 if (address == NULL)
2174 ipconfig->method = method;
2176 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2177 connman_ipaddress_set_ipv4(ipconfig->address,
2178 address, netmask, gateway);
2180 return connman_ipaddress_set_ipv6(
2181 ipconfig->address, address,
2182 prefix_length, gateway);
2185 case CONNMAN_IPCONFIG_METHOD_DHCP:
2186 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2189 ipconfig->method = method;
2196 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2197 DBusMessageIter *iter)
2199 struct connman_ipdevice *ipdevice;
2200 const char *method = "auto";
2202 connman_dbus_dict_append_basic(iter, "Method",
2203 DBUS_TYPE_STRING, &method);
2205 ipdevice = g_hash_table_lookup(ipdevice_hash,
2206 GINT_TO_POINTER(ipconfig->index));
2207 if (ipdevice == NULL)
2210 if (ipdevice->ifname != NULL)
2211 connman_dbus_dict_append_basic(iter, "Interface",
2212 DBUS_TYPE_STRING, &ipdevice->ifname);
2214 if (ipdevice->address != NULL)
2215 connman_dbus_dict_append_basic(iter, "Address",
2216 DBUS_TYPE_STRING, &ipdevice->address);
2218 if (ipdevice->mtu > 0)
2219 connman_dbus_dict_append_basic(iter, "MTU",
2220 DBUS_TYPE_UINT16, &ipdevice->mtu);
2223 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2224 GKeyFile *keyfile, const char *identifier, const char *prefix)
2230 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2232 key = g_strdup_printf("%smethod", prefix);
2233 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2234 if (method == NULL) {
2235 switch (ipconfig->type) {
2236 case CONNMAN_IPCONFIG_TYPE_IPV4:
2237 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2239 case CONNMAN_IPCONFIG_TYPE_IPV6:
2240 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2242 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2243 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2247 ipconfig->method = __connman_ipconfig_string2method(method);
2249 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2250 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2252 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2253 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2254 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2256 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2257 privacy = g_key_file_get_string(keyfile, identifier,
2259 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2268 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2269 ipconfig->address->prefixlen = g_key_file_get_integer(
2270 keyfile, identifier, key, NULL);
2273 key = g_strdup_printf("%slocal_address", prefix);
2274 ipconfig->address->local = g_key_file_get_string(
2275 keyfile, identifier, key, NULL);
2278 key = g_strdup_printf("%speer_address", prefix);
2279 ipconfig->address->peer = g_key_file_get_string(
2280 keyfile, identifier, key, NULL);
2283 key = g_strdup_printf("%sbroadcast_address", prefix);
2284 ipconfig->address->broadcast = g_key_file_get_string(
2285 keyfile, identifier, key, NULL);
2288 key = g_strdup_printf("%sgateway", prefix);
2289 ipconfig->address->gateway = g_key_file_get_string(
2290 keyfile, identifier, key, NULL);
2293 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2294 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2296 g_free(ipconfig->last_dhcp_address);
2297 ipconfig->last_dhcp_address = str;
2304 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2305 GKeyFile *keyfile, const char *identifier, const char *prefix)
2310 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2312 method = __connman_ipconfig_method2string(ipconfig->method);
2314 key = g_strdup_printf("%smethod", prefix);
2315 g_key_file_set_string(keyfile, identifier, key, method);
2318 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2319 const char *privacy;
2320 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2321 key = g_strdup_printf("%sprivacy", prefix);
2322 g_key_file_set_string(keyfile, identifier, key, privacy);
2326 switch (ipconfig->method) {
2327 case CONNMAN_IPCONFIG_METHOD_FIXED:
2328 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2330 case CONNMAN_IPCONFIG_METHOD_DHCP:
2331 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2332 if (ipconfig->last_dhcp_address != NULL &&
2333 strlen(ipconfig->last_dhcp_address) > 0)
2334 g_key_file_set_string(keyfile, identifier, key,
2335 ipconfig->last_dhcp_address);
2337 g_key_file_remove_key(keyfile, identifier, key, NULL);
2340 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2341 case CONNMAN_IPCONFIG_METHOD_OFF:
2342 case CONNMAN_IPCONFIG_METHOD_AUTO:
2346 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2347 g_key_file_set_integer(keyfile, identifier,
2348 key, ipconfig->address->prefixlen);
2351 key = g_strdup_printf("%slocal_address", prefix);
2352 if (ipconfig->address->local != NULL)
2353 g_key_file_set_string(keyfile, identifier,
2354 key, ipconfig->address->local);
2357 key = g_strdup_printf("%speer_address", prefix);
2358 if (ipconfig->address->peer != NULL)
2359 g_key_file_set_string(keyfile, identifier,
2360 key, ipconfig->address->peer);
2363 key = g_strdup_printf("%sbroadcast_address", prefix);
2364 if (ipconfig->address->broadcast != NULL)
2365 g_key_file_set_string(keyfile, identifier,
2366 key, ipconfig->address->broadcast);
2369 key = g_strdup_printf("%sgateway", prefix);
2370 if (ipconfig->address->gateway != NULL)
2371 g_key_file_set_string(keyfile, identifier,
2372 key, ipconfig->address->gateway);
2378 int __connman_ipconfig_init(void)
2382 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2383 NULL, free_ipdevice);
2388 void __connman_ipconfig_cleanup(void)
2392 g_hash_table_destroy(ipdevice_hash);
2393 ipdevice_hash = NULL;