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;
92 static connman_bool_t is_ipv6_supported = FALSE;
94 struct connman_ipaddress *connman_ipaddress_alloc(int family)
96 struct connman_ipaddress *ipaddress;
98 ipaddress = g_try_new0(struct connman_ipaddress, 1);
99 if (ipaddress == NULL)
102 ipaddress->family = family;
103 ipaddress->prefixlen = 0;
104 ipaddress->local = NULL;
105 ipaddress->peer = NULL;
106 ipaddress->broadcast = NULL;
107 ipaddress->gateway = NULL;
112 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
114 if (ipaddress == NULL)
117 g_free(ipaddress->broadcast);
118 g_free(ipaddress->peer);
119 g_free(ipaddress->local);
120 g_free(ipaddress->gateway);
124 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
133 mask = inet_network(netmask);
136 /* a valid netmask must be 2^n - 1 */
137 if ((host & (host + 1)) != 0)
141 for (; mask; mask <<= 1)
147 static gboolean check_ipv6_address(const char *address)
149 unsigned char buf[sizeof(struct in6_addr)];
155 err = inet_pton(AF_INET6, address, buf);
162 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
164 unsigned char prefix_length,
167 if (ipaddress == NULL)
170 if (check_ipv6_address(address) == FALSE)
173 DBG("prefix_len %d address %s gateway %s",
174 prefix_length, address, gateway);
176 ipaddress->family = AF_INET6;
178 ipaddress->prefixlen = prefix_length;
180 g_free(ipaddress->local);
181 ipaddress->local = g_strdup(address);
183 g_free(ipaddress->gateway);
184 ipaddress->gateway = g_strdup(gateway);
189 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
190 const char *address, const char *netmask, const char *gateway)
192 if (ipaddress == NULL)
195 ipaddress->family = AF_INET;
197 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
199 g_free(ipaddress->local);
200 ipaddress->local = g_strdup(address);
202 g_free(ipaddress->gateway);
203 ipaddress->gateway = g_strdup(gateway);
208 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
211 if (ipaddress == NULL)
214 g_free(ipaddress->peer);
215 ipaddress->peer = g_strdup(peer);
218 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
220 if (ipaddress == NULL)
223 ipaddress->prefixlen = 0;
225 g_free(ipaddress->local);
226 ipaddress->local = NULL;
228 g_free(ipaddress->peer);
229 ipaddress->peer = NULL;
231 g_free(ipaddress->broadcast);
232 ipaddress->broadcast = NULL;
234 g_free(ipaddress->gateway);
235 ipaddress->gateway = NULL;
238 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
239 struct connman_ipaddress *source)
241 if (ipaddress == NULL || source == NULL)
244 ipaddress->family = source->family;
245 ipaddress->prefixlen = source->prefixlen;
247 g_free(ipaddress->local);
248 ipaddress->local = g_strdup(source->local);
250 g_free(ipaddress->peer);
251 ipaddress->peer = g_strdup(source->peer);
253 g_free(ipaddress->broadcast);
254 ipaddress->broadcast = g_strdup(source->broadcast);
256 g_free(ipaddress->gateway);
257 ipaddress->gateway = g_strdup(source->gateway);
260 static void free_address_list(struct connman_ipdevice *ipdevice)
264 for (list = ipdevice->address_list; list; list = list->next) {
265 struct connman_ipaddress *ipaddress = list->data;
267 connman_ipaddress_free(ipaddress);
271 g_slist_free(ipdevice->address_list);
272 ipdevice->address_list = NULL;
275 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
276 unsigned char prefixlen, const char *local)
280 for (list = ipdevice->address_list; list; list = list->next) {
281 struct connman_ipaddress *ipaddress = list->data;
283 if (g_strcmp0(ipaddress->local, local) == 0 &&
284 ipaddress->prefixlen == prefixlen)
291 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
294 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
296 case CONNMAN_IPCONFIG_TYPE_IPV4:
298 case CONNMAN_IPCONFIG_TYPE_IPV6:
305 static const char *type2str(unsigned short type)
310 case ARPHRD_LOOPBACK:
323 static const char *scope2str(unsigned char scope)
335 static gboolean get_ipv6_state(gchar *ifname)
340 gboolean enabled = FALSE;
343 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
345 path = g_strdup_printf(
346 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
351 f = fopen(path, "r");
356 if (fscanf(f, "%d", &disabled) > 0)
364 static void set_ipv6_state(gchar *ifname, gboolean enable)
370 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
372 path = g_strdup_printf(
373 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
378 f = fopen(path, "r+");
393 static int get_ipv6_privacy(gchar *ifname)
402 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
408 f = fopen(path, "r");
415 if (fscanf(f, "%d", &value) <= 0)
423 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
424 * The privacy extension is described in RFC 3041 and RFC 4941
426 static void set_ipv6_privacy(gchar *ifname, int value)
434 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
443 f = fopen(path, "r+");
450 fprintf(f, "%d", value);
454 static int get_rp_filter()
457 int value = -EINVAL, tmp;
459 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
462 if (fscanf(f, "%d", &tmp) == 1)
470 static void set_rp_filter(int value)
474 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
479 fprintf(f, "%d", value);
484 int __connman_ipconfig_set_rp_filter()
488 value = get_rp_filter();
495 connman_info("rp_filter set to 2 (loose mode routing), "
496 "old value was %d", value);
501 void __connman_ipconfig_unset_rp_filter(int old_value)
503 set_rp_filter(old_value);
505 connman_info("rp_filter restored to %d", old_value);
508 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
510 if (ipconfig == NULL)
513 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
516 static void free_ipdevice(gpointer data)
518 struct connman_ipdevice *ipdevice = data;
520 connman_info("%s {remove} index %d", ipdevice->ifname,
523 if (ipdevice->config_ipv4 != NULL) {
524 __connman_ipconfig_unref(ipdevice->config_ipv4);
525 ipdevice->config_ipv4 = NULL;
528 if (ipdevice->config_ipv6 != NULL) {
529 __connman_ipconfig_unref(ipdevice->config_ipv6);
530 ipdevice->config_ipv6 = NULL;
533 free_address_list(ipdevice);
534 g_free(ipdevice->ipv4_gateway);
535 g_free(ipdevice->ipv6_gateway);
536 g_free(ipdevice->pac);
538 g_free(ipdevice->address);
540 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
541 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
543 g_free(ipdevice->ifname);
547 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
549 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
550 ipdevice->config_ipv6);
552 if (ipdevice->config_ipv6 != NULL &&
553 ipdevice->config_ipv6->enabled == TRUE)
556 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
557 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
558 set_ipv6_state(ipdevice->ifname, FALSE);
562 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
564 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
565 ipdevice->config_ipv6);
567 if (ipdevice->config_ipv4)
568 connman_inet_clear_address(ipdevice->index,
569 ipdevice->config_ipv4->address);
571 if (ipdevice->config_ipv6)
572 connman_inet_clear_ipv6_address(ipdevice->index,
573 ipdevice->config_ipv6->address->local,
574 ipdevice->config_ipv6->address->prefixlen);
577 static void update_stats(struct connman_ipdevice *ipdevice,
578 struct rtnl_link_stats *stats)
580 struct connman_service *service;
582 if (stats->rx_packets == 0 && stats->tx_packets == 0)
585 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
586 stats->rx_packets, stats->rx_bytes);
587 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
588 stats->tx_packets, stats->tx_bytes);
590 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
593 if (ipdevice->config_ipv4)
594 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
595 else if (ipdevice->config_ipv6)
596 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
603 ipdevice->rx_packets = stats->rx_packets;
604 ipdevice->tx_packets = stats->tx_packets;
605 ipdevice->rx_bytes = stats->rx_bytes;
606 ipdevice->tx_bytes = stats->tx_bytes;
607 ipdevice->rx_errors = stats->rx_errors;
608 ipdevice->tx_errors = stats->tx_errors;
609 ipdevice->rx_dropped = stats->rx_dropped;
610 ipdevice->tx_dropped = stats->tx_dropped;
612 __connman_service_notify(service,
613 ipdevice->rx_packets, ipdevice->tx_packets,
614 ipdevice->rx_bytes, ipdevice->tx_bytes,
615 ipdevice->rx_errors, ipdevice->tx_errors,
616 ipdevice->rx_dropped, ipdevice->tx_dropped);
619 void __connman_ipconfig_newlink(int index, unsigned short type,
620 unsigned int flags, const char *address,
622 struct rtnl_link_stats *stats)
624 struct connman_ipdevice *ipdevice;
627 gboolean up = FALSE, down = FALSE;
628 gboolean lower_up = FALSE, lower_down = FALSE;
630 DBG("index %d", index);
632 if (type == ARPHRD_LOOPBACK)
635 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
636 if (ipdevice != NULL) {
637 char *ifname = connman_inet_ifname(index);
638 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
639 DBG("interface name changed %s -> %s",
640 ipdevice->ifname, ifname);
642 g_free(ipdevice->ifname);
643 ipdevice->ifname = ifname;
650 ipdevice = g_try_new0(struct connman_ipdevice, 1);
651 if (ipdevice == NULL)
654 ipdevice->index = index;
655 ipdevice->ifname = connman_inet_ifname(index);
656 ipdevice->type = type;
658 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
659 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
661 ipdevice->address = g_strdup(address);
663 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
665 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
666 index, type, type2str(type));
671 update_stats(ipdevice, stats);
673 if (flags == ipdevice->flags)
676 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
683 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
684 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
685 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
686 (IFF_RUNNING | IFF_LOWER_UP))
688 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
692 ipdevice->flags = flags;
694 str = g_string_new(NULL);
699 g_string_append(str, "UP");
701 g_string_append(str, "DOWN");
703 if (flags & IFF_RUNNING)
704 g_string_append(str, ",RUNNING");
706 if (flags & IFF_LOWER_UP)
707 g_string_append(str, ",LOWER_UP");
709 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
712 g_string_free(str, TRUE);
714 for (list = g_list_first(ipconfig_list); list;
715 list = g_list_next(list)) {
716 struct connman_ipconfig *ipconfig = list->data;
718 if (index != ipconfig->index)
721 if (ipconfig->ops == NULL)
724 if (up == TRUE && ipconfig->ops->up)
725 ipconfig->ops->up(ipconfig);
726 if (lower_up == TRUE && ipconfig->ops->lower_up)
727 ipconfig->ops->lower_up(ipconfig);
729 if (lower_down == TRUE && ipconfig->ops->lower_down)
730 ipconfig->ops->lower_down(ipconfig);
731 if (down == TRUE && ipconfig->ops->down)
732 ipconfig->ops->down(ipconfig);
736 __connman_ipconfig_lower_up(ipdevice);
738 __connman_ipconfig_lower_down(ipdevice);
741 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
743 struct connman_ipdevice *ipdevice;
746 DBG("index %d", index);
748 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
749 if (ipdevice == NULL)
752 update_stats(ipdevice, stats);
754 for (list = g_list_first(ipconfig_list); list;
755 list = g_list_next(list)) {
756 struct connman_ipconfig *ipconfig = list->data;
758 if (index != ipconfig->index)
761 ipconfig->index = -1;
763 if (ipconfig->ops == NULL)
766 if (ipconfig->ops->lower_down)
767 ipconfig->ops->lower_down(ipconfig);
768 if (ipconfig->ops->down)
769 ipconfig->ops->down(ipconfig);
772 __connman_ipconfig_lower_down(ipdevice);
774 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
777 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
779 const struct connman_ipaddress *addr1 = a;
780 const struct connman_ipaddress *addr2 = b;
782 if (addr1->prefixlen != addr2->prefixlen)
783 return addr2->prefixlen - addr1->prefixlen;
785 return g_strcmp0(addr1->local, addr2->local);
788 void __connman_ipconfig_newaddr(int index, int family, const char *label,
789 unsigned char prefixlen, const char *address)
791 struct connman_ipdevice *ipdevice;
792 struct connman_ipaddress *ipaddress;
793 enum connman_ipconfig_type type;
796 DBG("index %d", index);
798 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
799 if (ipdevice == NULL)
802 ipaddress = connman_ipaddress_alloc(family);
803 if (ipaddress == NULL)
806 ipaddress->prefixlen = prefixlen;
807 ipaddress->local = g_strdup(address);
809 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
810 check_duplicate_address)) {
811 connman_ipaddress_free(ipaddress);
815 if (family == AF_INET)
816 type = CONNMAN_IPCONFIG_TYPE_IPV4;
817 else if (family == AF_INET6)
818 type = CONNMAN_IPCONFIG_TYPE_IPV6;
822 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
825 connman_info("%s {add} address %s/%u label %s family %d",
826 ipdevice->ifname, address, prefixlen, label, family);
828 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
829 __connman_ippool_newaddr(index, address, prefixlen);
831 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
832 connman_ipaddress_copy(ipdevice->config_ipv4->system,
835 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
836 connman_ipaddress_copy(ipdevice->config_ipv6->system,
841 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
844 for (list = g_list_first(ipconfig_list); list;
845 list = g_list_next(list)) {
846 struct connman_ipconfig *ipconfig = list->data;
848 if (index != ipconfig->index)
851 if (type != ipconfig->type)
854 if (ipconfig->ops == NULL)
857 if (ipconfig->ops->ip_bound)
858 ipconfig->ops->ip_bound(ipconfig);
862 void __connman_ipconfig_deladdr(int index, int family, const char *label,
863 unsigned char prefixlen, const char *address)
865 struct connman_ipdevice *ipdevice;
866 struct connman_ipaddress *ipaddress;
867 enum connman_ipconfig_type type;
870 DBG("index %d", index);
872 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
873 if (ipdevice == NULL)
876 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
877 if (ipaddress == NULL)
880 if (family == AF_INET)
881 type = CONNMAN_IPCONFIG_TYPE_IPV4;
882 else if (family == AF_INET6)
883 type = CONNMAN_IPCONFIG_TYPE_IPV6;
887 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
890 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
891 __connman_ippool_deladdr(index, address, prefixlen);
893 connman_ipaddress_clear(ipaddress);
896 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
897 address, prefixlen, label);
899 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
902 if (g_slist_length(ipdevice->address_list) > 0)
905 for (list = g_list_first(ipconfig_list); list;
906 list = g_list_next(list)) {
907 struct connman_ipconfig *ipconfig = list->data;
909 if (index != ipconfig->index)
912 if (type != ipconfig->type)
915 if (ipconfig->ops == NULL)
918 if (ipconfig->ops->ip_release)
919 ipconfig->ops->ip_release(ipconfig);
923 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
924 const char *dst, const char *gateway)
926 struct connman_ipdevice *ipdevice;
928 DBG("index %d", index);
930 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
931 if (ipdevice == NULL)
934 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
935 g_strcmp0(dst, "::") == 0)) {
937 enum connman_ipconfig_type type;
939 if (family == AF_INET6) {
940 type = CONNMAN_IPCONFIG_TYPE_IPV6;
941 g_free(ipdevice->ipv6_gateway);
942 ipdevice->ipv6_gateway = g_strdup(gateway);
944 if (ipdevice->config_ipv6 != NULL &&
945 ipdevice->config_ipv6->system != NULL) {
946 g_free(ipdevice->config_ipv6->system->gateway);
947 ipdevice->config_ipv6->system->gateway =
950 } else if (family == AF_INET) {
951 type = CONNMAN_IPCONFIG_TYPE_IPV4;
952 g_free(ipdevice->ipv4_gateway);
953 ipdevice->ipv4_gateway = g_strdup(gateway);
955 if (ipdevice->config_ipv4 != NULL &&
956 ipdevice->config_ipv4->system != NULL) {
957 g_free(ipdevice->config_ipv4->system->gateway);
958 ipdevice->config_ipv4->system->gateway =
964 for (config_list = g_list_first(ipconfig_list); config_list;
965 config_list = g_list_next(config_list)) {
966 struct connman_ipconfig *ipconfig = config_list->data;
968 if (index != ipconfig->index)
971 if (type != ipconfig->type)
974 if (ipconfig->ops == NULL)
977 if (ipconfig->ops->route_set)
978 ipconfig->ops->route_set(ipconfig);
982 connman_info("%s {add} route %s gw %s scope %u <%s>",
983 ipdevice->ifname, dst, gateway,
984 scope, scope2str(scope));
987 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
988 const char *dst, const char *gateway)
990 struct connman_ipdevice *ipdevice;
992 DBG("index %d", index);
994 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
995 if (ipdevice == NULL)
998 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
999 g_strcmp0(dst, "::") == 0)) {
1001 enum connman_ipconfig_type type;
1003 if (family == AF_INET6) {
1004 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1005 g_free(ipdevice->ipv6_gateway);
1006 ipdevice->ipv6_gateway = NULL;
1008 if (ipdevice->config_ipv6 != NULL &&
1009 ipdevice->config_ipv6->system != NULL) {
1010 g_free(ipdevice->config_ipv6->system->gateway);
1011 ipdevice->config_ipv6->system->gateway = NULL;
1013 } else if (family == AF_INET) {
1014 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1015 g_free(ipdevice->ipv4_gateway);
1016 ipdevice->ipv4_gateway = NULL;
1018 if (ipdevice->config_ipv4 != NULL &&
1019 ipdevice->config_ipv4->system != NULL) {
1020 g_free(ipdevice->config_ipv4->system->gateway);
1021 ipdevice->config_ipv4->system->gateway = NULL;
1026 for (config_list = g_list_first(ipconfig_list); config_list;
1027 config_list = g_list_next(config_list)) {
1028 struct connman_ipconfig *ipconfig = config_list->data;
1030 if (index != ipconfig->index)
1033 if (type != ipconfig->type)
1036 if (ipconfig->ops == NULL)
1039 if (ipconfig->ops->route_unset)
1040 ipconfig->ops->route_unset(ipconfig);
1044 connman_info("%s {del} route %s gw %s scope %u <%s>",
1045 ipdevice->ifname, dst, gateway,
1046 scope, scope2str(scope));
1049 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1054 keys = g_hash_table_get_keys(ipdevice_hash);
1058 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1059 int index = GPOINTER_TO_INT(list->data);
1061 function(index, user_data);
1067 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1068 struct connman_ipconfig *ipconfig)
1070 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1073 unsigned short __connman_ipconfig_get_type_from_index(int index)
1075 struct connman_ipdevice *ipdevice;
1077 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1078 if (ipdevice == NULL)
1081 return ipdevice->type;
1084 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1086 struct connman_ipdevice *ipdevice;
1088 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1089 if (ipdevice == NULL)
1092 return ipdevice->flags;
1095 const char *__connman_ipconfig_get_gateway_from_index(int index,
1096 enum connman_ipconfig_type type)
1098 struct connman_ipdevice *ipdevice;
1100 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1101 if (ipdevice == NULL)
1104 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1105 if (ipdevice->ipv4_gateway != NULL)
1106 return ipdevice->ipv4_gateway;
1108 if (ipdevice->config_ipv4 != NULL &&
1109 ipdevice->config_ipv4->address != NULL)
1110 return ipdevice->config_ipv4->address->gateway;
1113 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1114 if (ipdevice->ipv6_gateway != NULL)
1115 return ipdevice->ipv6_gateway;
1117 if (ipdevice->config_ipv6 != NULL &&
1118 ipdevice->config_ipv6->address != NULL)
1119 return ipdevice->config_ipv6->address->gateway;
1125 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1127 ipconfig->index = index;
1130 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1132 if (ipconfig->address == NULL)
1135 return ipconfig->address->local;
1138 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1140 if (ipconfig->address == NULL)
1143 g_free(ipconfig->address->local);
1144 ipconfig->address->local = g_strdup(address);
1147 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1149 if (ipconfig->address == NULL)
1152 return ipconfig->address->peer;
1155 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1157 if (ipconfig->address == NULL)
1160 g_free(ipconfig->address->peer);
1161 ipconfig->address->peer = g_strdup(address);
1164 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1166 if (ipconfig->address == NULL)
1169 return ipconfig->address->broadcast;
1172 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1174 if (ipconfig->address == NULL)
1177 g_free(ipconfig->address->broadcast);
1178 ipconfig->address->broadcast = g_strdup(broadcast);
1181 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1183 if (ipconfig->address == NULL)
1186 return ipconfig->address->gateway;
1189 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1193 if (ipconfig->address == NULL)
1195 g_free(ipconfig->address->gateway);
1196 ipconfig->address->gateway = g_strdup(gateway);
1199 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1201 struct connman_service *service;
1205 if (ipconfig->address == NULL)
1208 service = __connman_service_lookup_from_index(ipconfig->index);
1209 if (service == NULL)
1212 __connman_connection_gateway_remove(service, ipconfig->type);
1214 DBG("type %d gw %s peer %s", ipconfig->type,
1215 ipconfig->address->gateway, ipconfig->address->peer);
1217 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1218 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1219 return __connman_connection_gateway_add(service,
1220 ipconfig->address->gateway,
1222 ipconfig->address->peer);
1227 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1229 struct connman_service *service;
1233 service = __connman_service_lookup_from_index(ipconfig->index);
1234 if (service != NULL)
1235 __connman_connection_gateway_remove(service, ipconfig->type);
1238 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1240 if (ipconfig->address == NULL)
1243 return ipconfig->address->prefixlen;
1246 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1248 if (ipconfig->address == NULL)
1251 ipconfig->address->prefixlen = prefixlen;
1254 static struct connman_ipconfig *create_ipv6config(int index)
1256 struct connman_ipconfig *ipv6config;
1257 struct connman_ipdevice *ipdevice;
1259 DBG("index %d", index);
1261 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1262 if (ipv6config == NULL)
1265 ipv6config->refcount = 1;
1267 ipv6config->index = index;
1268 ipv6config->enabled = FALSE;
1269 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1271 if (is_ipv6_supported == FALSE)
1272 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1274 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1276 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1277 if (ipdevice != NULL)
1278 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1280 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1281 if (ipv6config->address == NULL) {
1286 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1288 DBG("ipconfig %p method %s", ipv6config,
1289 __connman_ipconfig_method2string(ipv6config->method));
1295 * connman_ipconfig_create:
1297 * Allocate a new ipconfig structure.
1299 * Returns: a newly-allocated #connman_ipconfig structure
1301 struct connman_ipconfig *__connman_ipconfig_create(int index,
1302 enum connman_ipconfig_type type)
1304 struct connman_ipconfig *ipconfig;
1306 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1307 return create_ipv6config(index);
1309 DBG("index %d", index);
1311 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1312 if (ipconfig == NULL)
1315 ipconfig->refcount = 1;
1317 ipconfig->index = index;
1318 ipconfig->enabled = FALSE;
1319 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1321 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1322 if (ipconfig->address == NULL) {
1327 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1329 DBG("ipconfig %p", ipconfig);
1336 * connman_ipconfig_ref:
1337 * @ipconfig: ipconfig structure
1339 * Increase reference counter of ipconfig
1341 struct connman_ipconfig *
1342 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1343 const char *file, int line, const char *caller)
1345 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1346 file, line, caller);
1348 __sync_fetch_and_add(&ipconfig->refcount, 1);
1354 * connman_ipconfig_unref:
1355 * @ipconfig: ipconfig structure
1357 * Decrease reference counter of ipconfig
1359 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1360 const char *file, int line, const char *caller)
1362 if (ipconfig == NULL)
1365 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1366 file, line, caller);
1368 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1371 if (__connman_ipconfig_disable(ipconfig) < 0)
1372 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1374 __connman_ipconfig_set_ops(ipconfig, NULL);
1376 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1377 __connman_ipconfig_unref(ipconfig->origin);
1378 ipconfig->origin = NULL;
1381 connman_ipaddress_free(ipconfig->system);
1382 connman_ipaddress_free(ipconfig->address);
1383 g_free(ipconfig->last_dhcp_address);
1388 * connman_ipconfig_get_data:
1389 * @ipconfig: ipconfig structure
1391 * Get private data pointer
1393 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1395 if (ipconfig == NULL)
1398 return ipconfig->ops_data;
1402 * connman_ipconfig_set_data:
1403 * @ipconfig: ipconfig structure
1404 * @data: data pointer
1406 * Set private data pointer
1408 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1410 ipconfig->ops_data = data;
1414 * connman_ipconfig_get_index:
1415 * @ipconfig: ipconfig structure
1417 * Get interface index
1419 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1421 if (ipconfig == NULL)
1424 if (ipconfig->origin != NULL)
1425 return ipconfig->origin->index;
1427 return ipconfig->index;
1431 * connman_ipconfig_get_ifname:
1432 * @ipconfig: ipconfig structure
1434 * Get interface name
1436 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1438 struct connman_ipdevice *ipdevice;
1440 if (ipconfig == NULL)
1443 if (ipconfig->index < 0)
1446 ipdevice = g_hash_table_lookup(ipdevice_hash,
1447 GINT_TO_POINTER(ipconfig->index));
1448 if (ipdevice == NULL)
1451 return ipdevice->ifname;
1455 * connman_ipconfig_set_ops:
1456 * @ipconfig: ipconfig structure
1457 * @ops: operation callbacks
1459 * Set the operation callbacks
1461 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1462 const struct connman_ipconfig_ops *ops)
1464 ipconfig->ops = ops;
1468 * connman_ipconfig_set_method:
1469 * @ipconfig: ipconfig structure
1470 * @method: configuration method
1472 * Set the configuration method
1474 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1475 enum connman_ipconfig_method method)
1477 ipconfig->method = method;
1482 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1484 if (ipconfig == NULL)
1485 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1487 return ipconfig->method;
1490 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1494 switch (ipconfig->method) {
1495 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1496 case CONNMAN_IPCONFIG_METHOD_OFF:
1498 case CONNMAN_IPCONFIG_METHOD_AUTO:
1499 case CONNMAN_IPCONFIG_METHOD_FIXED:
1500 case CONNMAN_IPCONFIG_METHOD_DHCP:
1501 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1502 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1503 return connman_inet_set_address(ipconfig->index,
1505 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1506 return connman_inet_set_ipv6_address(
1507 ipconfig->index, ipconfig->address);
1513 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1519 if (ipconfig == NULL)
1522 DBG("method %d", ipconfig->method);
1524 switch (ipconfig->method) {
1525 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1526 case CONNMAN_IPCONFIG_METHOD_OFF:
1528 case CONNMAN_IPCONFIG_METHOD_AUTO:
1529 case CONNMAN_IPCONFIG_METHOD_FIXED:
1530 case CONNMAN_IPCONFIG_METHOD_DHCP:
1531 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1532 err = __connman_ipconfig_address_unset(ipconfig);
1533 connman_ipaddress_clear(ipconfig->address);
1541 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1547 if (ipconfig == NULL)
1550 DBG("method %d", ipconfig->method);
1552 switch (ipconfig->method) {
1553 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1554 case CONNMAN_IPCONFIG_METHOD_OFF:
1556 case CONNMAN_IPCONFIG_METHOD_AUTO:
1557 case CONNMAN_IPCONFIG_METHOD_FIXED:
1558 case CONNMAN_IPCONFIG_METHOD_DHCP:
1559 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1560 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1561 err = connman_inet_clear_address(ipconfig->index,
1563 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1564 err = connman_inet_clear_ipv6_address(
1566 ipconfig->address->local,
1567 ipconfig->address->prefixlen);
1577 int __connman_ipconfig_set_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 g_free(ipdevice->pac);
1593 ipdevice->pac = g_strdup(url);
1598 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1600 struct connman_ipdevice *ipdevice;
1602 DBG("ipconfig %p", ipconfig);
1604 if (ipconfig == NULL || ipconfig->index < 0)
1607 ipdevice = g_hash_table_lookup(ipdevice_hash,
1608 GINT_TO_POINTER(ipconfig->index));
1609 if (ipdevice == NULL)
1612 return ipdevice->pac;
1615 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1616 const char *address)
1618 if (ipconfig == NULL)
1621 g_free(ipconfig->last_dhcp_address);
1622 ipconfig->last_dhcp_address = g_strdup(address);
1625 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1627 if (ipconfig == NULL)
1630 return ipconfig->last_dhcp_address;
1633 static void disable_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 set_ipv6_state(ipdevice->ifname, FALSE);
1647 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1649 struct connman_ipdevice *ipdevice;
1653 ipdevice = g_hash_table_lookup(ipdevice_hash,
1654 GINT_TO_POINTER(ipconfig->index));
1655 if (ipdevice == NULL)
1658 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1659 set_ipv6_privacy(ipdevice->ifname,
1660 ipconfig->ipv6_privacy_config);
1662 set_ipv6_state(ipdevice->ifname, TRUE);
1665 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1667 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1670 enable_ipv6(ipconfig);
1673 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1675 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1678 disable_ipv6(ipconfig);
1681 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1683 if (ipconfig == NULL)
1686 switch (ipconfig->method) {
1687 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1688 case CONNMAN_IPCONFIG_METHOD_OFF:
1690 case CONNMAN_IPCONFIG_METHOD_AUTO:
1691 case CONNMAN_IPCONFIG_METHOD_FIXED:
1692 case CONNMAN_IPCONFIG_METHOD_DHCP:
1693 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1700 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1702 struct connman_ipdevice *ipdevice;
1703 gboolean up = FALSE, down = FALSE;
1704 gboolean lower_up = FALSE, lower_down = FALSE;
1705 enum connman_ipconfig_type type;
1707 DBG("ipconfig %p", ipconfig);
1709 if (ipconfig == NULL || ipconfig->index < 0)
1712 ipdevice = g_hash_table_lookup(ipdevice_hash,
1713 GINT_TO_POINTER(ipconfig->index));
1714 if (ipdevice == NULL)
1717 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1718 if (ipdevice->config_ipv4 == ipconfig)
1720 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1721 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1722 if (ipdevice->config_ipv6 == ipconfig)
1724 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1725 enable_ipv6(ipconfig);
1729 ipconfig->enabled = TRUE;
1731 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1732 ipdevice->config_ipv4 != NULL) {
1733 ipconfig_list = g_list_remove(ipconfig_list,
1734 ipdevice->config_ipv4);
1736 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1738 __connman_ipconfig_unref(ipdevice->config_ipv4);
1741 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1742 ipdevice->config_ipv6 != NULL) {
1743 ipconfig_list = g_list_remove(ipconfig_list,
1744 ipdevice->config_ipv6);
1746 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1748 __connman_ipconfig_unref(ipdevice->config_ipv6);
1751 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1752 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1753 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1754 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1756 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1758 if (ipdevice->flags & IFF_UP)
1763 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1764 (IFF_RUNNING | IFF_LOWER_UP))
1766 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1769 if (up == TRUE && ipconfig->ops->up)
1770 ipconfig->ops->up(ipconfig);
1771 if (lower_up == TRUE && ipconfig->ops->lower_up)
1772 ipconfig->ops->lower_up(ipconfig);
1774 if (lower_down == TRUE && ipconfig->ops->lower_down)
1775 ipconfig->ops->lower_down(ipconfig);
1776 if (down == TRUE && ipconfig->ops->down)
1777 ipconfig->ops->down(ipconfig);
1782 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1784 struct connman_ipdevice *ipdevice;
1786 DBG("ipconfig %p", ipconfig);
1788 if (ipconfig == NULL || ipconfig->index < 0)
1791 ipdevice = g_hash_table_lookup(ipdevice_hash,
1792 GINT_TO_POINTER(ipconfig->index));
1793 if (ipdevice == NULL)
1796 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1799 ipconfig->enabled = FALSE;
1801 if (ipdevice->config_ipv4 == ipconfig) {
1802 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1804 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1805 __connman_ipconfig_unref(ipdevice->config_ipv4);
1806 ipdevice->config_ipv4 = NULL;
1810 if (ipdevice->config_ipv6 == ipconfig) {
1811 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1813 if (ipdevice->config_ipv6->method ==
1814 CONNMAN_IPCONFIG_METHOD_AUTO)
1815 disable_ipv6(ipdevice->config_ipv6);
1817 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1818 __connman_ipconfig_unref(ipdevice->config_ipv6);
1819 ipdevice->config_ipv6 = NULL;
1826 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1829 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1831 case CONNMAN_IPCONFIG_METHOD_OFF:
1833 case CONNMAN_IPCONFIG_METHOD_FIXED:
1835 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1837 case CONNMAN_IPCONFIG_METHOD_DHCP:
1839 case CONNMAN_IPCONFIG_METHOD_AUTO:
1846 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1848 if (g_strcmp0(method, "off") == 0)
1849 return CONNMAN_IPCONFIG_METHOD_OFF;
1850 else if (g_strcmp0(method, "fixed") == 0)
1851 return CONNMAN_IPCONFIG_METHOD_FIXED;
1852 else if (g_strcmp0(method, "manual") == 0)
1853 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1854 else if (g_strcmp0(method, "dhcp") == 0)
1855 return CONNMAN_IPCONFIG_METHOD_DHCP;
1856 else if (g_strcmp0(method, "auto") == 0)
1857 return CONNMAN_IPCONFIG_METHOD_AUTO;
1859 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1862 static const char *privacy2string(int privacy)
1866 else if (privacy == 1)
1868 else if (privacy > 1)
1874 static int string2privacy(const char *privacy)
1876 if (g_strcmp0(privacy, "disabled") == 0)
1878 else if (g_strcmp0(privacy, "enabled") == 0)
1880 else if (g_strcmp0(privacy, "prefered") == 0)
1886 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1887 DBusMessageIter *iter)
1889 struct connman_ipaddress *append_addr = NULL;
1894 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1897 str = __connman_ipconfig_method2string(ipconfig->method);
1901 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1903 switch (ipconfig->method) {
1904 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1905 case CONNMAN_IPCONFIG_METHOD_OFF:
1906 case CONNMAN_IPCONFIG_METHOD_AUTO:
1909 case CONNMAN_IPCONFIG_METHOD_FIXED:
1910 append_addr = ipconfig->address;
1913 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1914 case CONNMAN_IPCONFIG_METHOD_DHCP:
1915 append_addr = ipconfig->system;
1919 if (append_addr == NULL)
1922 if (append_addr->local != NULL) {
1924 struct in_addr netmask;
1927 connman_dbus_dict_append_basic(iter, "Address",
1928 DBUS_TYPE_STRING, &append_addr->local);
1930 addr = 0xffffffff << (32 - append_addr->prefixlen);
1931 netmask.s_addr = htonl(addr);
1932 mask = inet_ntoa(netmask);
1933 connman_dbus_dict_append_basic(iter, "Netmask",
1934 DBUS_TYPE_STRING, &mask);
1937 if (append_addr->gateway != NULL)
1938 connman_dbus_dict_append_basic(iter, "Gateway",
1939 DBUS_TYPE_STRING, &append_addr->gateway);
1942 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1943 DBusMessageIter *iter,
1944 struct connman_ipconfig *ipconfig_ipv4)
1946 struct connman_ipaddress *append_addr = NULL;
1947 const char *str, *privacy;
1951 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1954 str = __connman_ipconfig_method2string(ipconfig->method);
1958 if (ipconfig_ipv4 != NULL &&
1959 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1960 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1964 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1966 switch (ipconfig->method) {
1967 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1968 case CONNMAN_IPCONFIG_METHOD_OFF:
1971 case CONNMAN_IPCONFIG_METHOD_FIXED:
1972 append_addr = ipconfig->address;
1975 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1976 case CONNMAN_IPCONFIG_METHOD_DHCP:
1977 case CONNMAN_IPCONFIG_METHOD_AUTO:
1978 append_addr = ipconfig->system;
1982 if (append_addr == NULL)
1985 if (append_addr->local != NULL) {
1986 connman_dbus_dict_append_basic(iter, "Address",
1987 DBUS_TYPE_STRING, &append_addr->local);
1988 connman_dbus_dict_append_basic(iter, "PrefixLength",
1990 &append_addr->prefixlen);
1993 if (append_addr->gateway != NULL)
1994 connman_dbus_dict_append_basic(iter, "Gateway",
1995 DBUS_TYPE_STRING, &append_addr->gateway);
1997 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1998 connman_dbus_dict_append_basic(iter, "Privacy",
1999 DBUS_TYPE_STRING, &privacy);
2002 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2003 DBusMessageIter *iter)
2005 const char *str, *privacy;
2009 str = __connman_ipconfig_method2string(ipconfig->method);
2013 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2015 switch (ipconfig->method) {
2016 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2017 case CONNMAN_IPCONFIG_METHOD_OFF:
2018 case CONNMAN_IPCONFIG_METHOD_DHCP:
2020 case CONNMAN_IPCONFIG_METHOD_FIXED:
2021 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2022 case CONNMAN_IPCONFIG_METHOD_AUTO:
2026 if (ipconfig->address == NULL)
2029 if (ipconfig->address->local != NULL) {
2030 connman_dbus_dict_append_basic(iter, "Address",
2031 DBUS_TYPE_STRING, &ipconfig->address->local);
2032 connman_dbus_dict_append_basic(iter, "PrefixLength",
2034 &ipconfig->address->prefixlen);
2037 if (ipconfig->address->gateway != NULL)
2038 connman_dbus_dict_append_basic(iter, "Gateway",
2039 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2041 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2042 connman_dbus_dict_append_basic(iter, "Privacy",
2043 DBUS_TYPE_STRING, &privacy);
2046 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2047 DBusMessageIter *iter)
2053 str = __connman_ipconfig_method2string(ipconfig->method);
2057 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2059 switch (ipconfig->method) {
2060 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2061 case CONNMAN_IPCONFIG_METHOD_OFF:
2062 case CONNMAN_IPCONFIG_METHOD_DHCP:
2063 case CONNMAN_IPCONFIG_METHOD_AUTO:
2065 case CONNMAN_IPCONFIG_METHOD_FIXED:
2066 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2070 if (ipconfig->address == NULL)
2073 if (ipconfig->address->local != NULL) {
2075 struct in_addr netmask;
2078 connman_dbus_dict_append_basic(iter, "Address",
2079 DBUS_TYPE_STRING, &ipconfig->address->local);
2081 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2082 netmask.s_addr = htonl(addr);
2083 mask = inet_ntoa(netmask);
2084 connman_dbus_dict_append_basic(iter, "Netmask",
2085 DBUS_TYPE_STRING, &mask);
2088 if (ipconfig->address->gateway != NULL)
2089 connman_dbus_dict_append_basic(iter, "Gateway",
2090 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2093 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2094 DBusMessageIter *array)
2096 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2097 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2098 *prefix_length_string = NULL, *privacy_string = NULL;
2099 int prefix_length = 0, privacy = 0;
2100 DBusMessageIter dict;
2102 DBG("ipconfig %p", ipconfig);
2104 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2107 dbus_message_iter_recurse(array, &dict);
2109 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2110 DBusMessageIter entry, value;
2114 dbus_message_iter_recurse(&dict, &entry);
2116 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2119 dbus_message_iter_get_basic(&entry, &key);
2120 dbus_message_iter_next(&entry);
2122 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2125 dbus_message_iter_recurse(&entry, &value);
2127 type = dbus_message_iter_get_arg_type(&value);
2129 if (g_str_equal(key, "Method") == TRUE) {
2132 if (type != DBUS_TYPE_STRING)
2135 dbus_message_iter_get_basic(&value, &str);
2136 method = __connman_ipconfig_string2method(str);
2137 } else if (g_str_equal(key, "Address") == TRUE) {
2138 if (type != DBUS_TYPE_STRING)
2141 dbus_message_iter_get_basic(&value, &address);
2142 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2143 if (type != DBUS_TYPE_STRING)
2146 dbus_message_iter_get_basic(&value,
2147 &prefix_length_string);
2149 prefix_length = atoi(prefix_length_string);
2150 if (prefix_length < 0 || prefix_length > 128)
2152 } else if (g_str_equal(key, "Netmask") == TRUE) {
2153 if (type != DBUS_TYPE_STRING)
2156 dbus_message_iter_get_basic(&value, &netmask);
2157 } else if (g_str_equal(key, "Gateway") == TRUE) {
2158 if (type != DBUS_TYPE_STRING)
2161 dbus_message_iter_get_basic(&value, &gateway);
2162 } else if (g_str_equal(key, "Privacy") == TRUE) {
2163 if (type != DBUS_TYPE_STRING)
2166 dbus_message_iter_get_basic(&value, &privacy_string);
2167 privacy = string2privacy(privacy_string);
2170 dbus_message_iter_next(&dict);
2173 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2175 method, address, netmask, gateway, prefix_length,
2179 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2180 case CONNMAN_IPCONFIG_METHOD_FIXED:
2183 case CONNMAN_IPCONFIG_METHOD_OFF:
2184 ipconfig->method = method;
2185 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2186 disable_ipv6(ipconfig);
2189 case CONNMAN_IPCONFIG_METHOD_AUTO:
2190 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2193 ipconfig->method = method;
2194 if (privacy_string != NULL)
2195 ipconfig->ipv6_privacy_config = privacy;
2196 enable_ipv6(ipconfig);
2199 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2200 if (address == NULL)
2203 ipconfig->method = method;
2205 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2206 connman_ipaddress_set_ipv4(ipconfig->address,
2207 address, netmask, gateway);
2209 return connman_ipaddress_set_ipv6(
2210 ipconfig->address, address,
2211 prefix_length, gateway);
2214 case CONNMAN_IPCONFIG_METHOD_DHCP:
2215 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2218 ipconfig->method = method;
2225 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2226 DBusMessageIter *iter)
2228 struct connman_ipdevice *ipdevice;
2229 const char *method = "auto";
2231 connman_dbus_dict_append_basic(iter, "Method",
2232 DBUS_TYPE_STRING, &method);
2234 ipdevice = g_hash_table_lookup(ipdevice_hash,
2235 GINT_TO_POINTER(ipconfig->index));
2236 if (ipdevice == NULL)
2239 if (ipdevice->ifname != NULL)
2240 connman_dbus_dict_append_basic(iter, "Interface",
2241 DBUS_TYPE_STRING, &ipdevice->ifname);
2243 if (ipdevice->address != NULL)
2244 connman_dbus_dict_append_basic(iter, "Address",
2245 DBUS_TYPE_STRING, &ipdevice->address);
2247 if (ipdevice->mtu > 0)
2248 connman_dbus_dict_append_basic(iter, "MTU",
2249 DBUS_TYPE_UINT16, &ipdevice->mtu);
2252 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2253 GKeyFile *keyfile, const char *identifier, const char *prefix)
2259 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2261 key = g_strdup_printf("%smethod", prefix);
2262 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2263 if (method == NULL) {
2264 switch (ipconfig->type) {
2265 case CONNMAN_IPCONFIG_TYPE_IPV4:
2266 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2268 case CONNMAN_IPCONFIG_TYPE_IPV6:
2269 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2271 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2272 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2276 ipconfig->method = __connman_ipconfig_string2method(method);
2278 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2279 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2281 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2282 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2283 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2285 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2286 privacy = g_key_file_get_string(keyfile, identifier,
2288 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2297 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2298 ipconfig->address->prefixlen = g_key_file_get_integer(
2299 keyfile, identifier, key, NULL);
2302 key = g_strdup_printf("%slocal_address", prefix);
2303 ipconfig->address->local = g_key_file_get_string(
2304 keyfile, identifier, key, NULL);
2307 key = g_strdup_printf("%speer_address", prefix);
2308 ipconfig->address->peer = g_key_file_get_string(
2309 keyfile, identifier, key, NULL);
2312 key = g_strdup_printf("%sbroadcast_address", prefix);
2313 ipconfig->address->broadcast = g_key_file_get_string(
2314 keyfile, identifier, key, NULL);
2317 key = g_strdup_printf("%sgateway", prefix);
2318 ipconfig->address->gateway = g_key_file_get_string(
2319 keyfile, identifier, key, NULL);
2322 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2323 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2325 g_free(ipconfig->last_dhcp_address);
2326 ipconfig->last_dhcp_address = str;
2333 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2334 GKeyFile *keyfile, const char *identifier, const char *prefix)
2339 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2341 method = __connman_ipconfig_method2string(ipconfig->method);
2343 key = g_strdup_printf("%smethod", prefix);
2344 g_key_file_set_string(keyfile, identifier, key, method);
2347 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2348 const char *privacy;
2349 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2350 key = g_strdup_printf("%sprivacy", prefix);
2351 g_key_file_set_string(keyfile, identifier, key, privacy);
2355 switch (ipconfig->method) {
2356 case CONNMAN_IPCONFIG_METHOD_FIXED:
2357 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2359 case CONNMAN_IPCONFIG_METHOD_DHCP:
2360 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2361 if (ipconfig->last_dhcp_address != NULL &&
2362 strlen(ipconfig->last_dhcp_address) > 0)
2363 g_key_file_set_string(keyfile, identifier, key,
2364 ipconfig->last_dhcp_address);
2366 g_key_file_remove_key(keyfile, identifier, key, NULL);
2369 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2370 case CONNMAN_IPCONFIG_METHOD_OFF:
2371 case CONNMAN_IPCONFIG_METHOD_AUTO:
2375 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2376 if (ipconfig->address->prefixlen != 0)
2377 g_key_file_set_integer(keyfile, identifier,
2378 key, ipconfig->address->prefixlen);
2381 key = g_strdup_printf("%slocal_address", prefix);
2382 if (ipconfig->address->local != NULL)
2383 g_key_file_set_string(keyfile, identifier,
2384 key, ipconfig->address->local);
2387 key = g_strdup_printf("%speer_address", prefix);
2388 if (ipconfig->address->peer != NULL)
2389 g_key_file_set_string(keyfile, identifier,
2390 key, ipconfig->address->peer);
2393 key = g_strdup_printf("%sbroadcast_address", prefix);
2394 if (ipconfig->address->broadcast != NULL)
2395 g_key_file_set_string(keyfile, identifier,
2396 key, ipconfig->address->broadcast);
2399 key = g_strdup_printf("%sgateway", prefix);
2400 if (ipconfig->address->gateway != NULL)
2401 g_key_file_set_string(keyfile, identifier,
2402 key, ipconfig->address->gateway);
2408 int __connman_ipconfig_init(void)
2412 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2413 NULL, free_ipdevice);
2415 is_ipv6_supported = connman_inet_is_ipv6_supported();
2420 void __connman_ipconfig_cleanup(void)
2424 g_hash_table_destroy(ipdevice_hash);
2425 ipdevice_hash = NULL;