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_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
240 if (ipconfig == NULL)
243 connman_ipaddress_clear(ipconfig->address);
246 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
247 struct connman_ipaddress *source)
249 if (ipaddress == NULL || source == NULL)
252 ipaddress->family = source->family;
253 ipaddress->prefixlen = source->prefixlen;
255 g_free(ipaddress->local);
256 ipaddress->local = g_strdup(source->local);
258 g_free(ipaddress->peer);
259 ipaddress->peer = g_strdup(source->peer);
261 g_free(ipaddress->broadcast);
262 ipaddress->broadcast = g_strdup(source->broadcast);
264 g_free(ipaddress->gateway);
265 ipaddress->gateway = g_strdup(source->gateway);
268 static void free_address_list(struct connman_ipdevice *ipdevice)
272 for (list = ipdevice->address_list; list; list = list->next) {
273 struct connman_ipaddress *ipaddress = list->data;
275 connman_ipaddress_free(ipaddress);
279 g_slist_free(ipdevice->address_list);
280 ipdevice->address_list = NULL;
283 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
284 unsigned char prefixlen, const char *local)
288 for (list = ipdevice->address_list; list; list = list->next) {
289 struct connman_ipaddress *ipaddress = list->data;
291 if (g_strcmp0(ipaddress->local, local) == 0 &&
292 ipaddress->prefixlen == prefixlen)
299 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
302 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
304 case CONNMAN_IPCONFIG_TYPE_IPV4:
306 case CONNMAN_IPCONFIG_TYPE_IPV6:
313 static const char *type2str(unsigned short type)
318 case ARPHRD_LOOPBACK:
331 static const char *scope2str(unsigned char scope)
343 static gboolean get_ipv6_state(gchar *ifname)
348 gboolean enabled = FALSE;
351 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
353 path = g_strdup_printf(
354 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
359 f = fopen(path, "r");
364 if (fscanf(f, "%d", &disabled) > 0)
372 static void set_ipv6_state(gchar *ifname, gboolean enable)
378 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
380 path = g_strdup_printf(
381 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
386 f = fopen(path, "r+");
401 static int get_ipv6_privacy(gchar *ifname)
410 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
416 f = fopen(path, "r");
423 if (fscanf(f, "%d", &value) <= 0)
431 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
432 * The privacy extension is described in RFC 3041 and RFC 4941
434 static void set_ipv6_privacy(gchar *ifname, int value)
442 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
451 f = fopen(path, "r+");
458 fprintf(f, "%d", value);
462 static int get_rp_filter()
465 int value = -EINVAL, tmp;
467 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
470 if (fscanf(f, "%d", &tmp) == 1)
478 static void set_rp_filter(int value)
482 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
487 fprintf(f, "%d", value);
492 int __connman_ipconfig_set_rp_filter()
496 value = get_rp_filter();
503 connman_info("rp_filter set to 2 (loose mode routing), "
504 "old value was %d", value);
509 void __connman_ipconfig_unset_rp_filter(int old_value)
511 set_rp_filter(old_value);
513 connman_info("rp_filter restored to %d", old_value);
516 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
518 if (ipconfig == NULL)
521 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
524 static void free_ipdevice(gpointer data)
526 struct connman_ipdevice *ipdevice = data;
528 connman_info("%s {remove} index %d", ipdevice->ifname,
531 if (ipdevice->config_ipv4 != NULL) {
532 __connman_ipconfig_unref(ipdevice->config_ipv4);
533 ipdevice->config_ipv4 = NULL;
536 if (ipdevice->config_ipv6 != NULL) {
537 __connman_ipconfig_unref(ipdevice->config_ipv6);
538 ipdevice->config_ipv6 = NULL;
541 free_address_list(ipdevice);
542 g_free(ipdevice->ipv4_gateway);
543 g_free(ipdevice->ipv6_gateway);
544 g_free(ipdevice->pac);
546 g_free(ipdevice->address);
548 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
549 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
551 g_free(ipdevice->ifname);
555 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
557 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
558 ipdevice->config_ipv6);
560 if (ipdevice->config_ipv6 != NULL &&
561 ipdevice->config_ipv6->enabled == TRUE)
564 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
565 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
566 set_ipv6_state(ipdevice->ifname, FALSE);
570 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
572 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
573 ipdevice->config_ipv6);
575 if (ipdevice->config_ipv4)
576 connman_inet_clear_address(ipdevice->index,
577 ipdevice->config_ipv4->address);
579 if (ipdevice->config_ipv6)
580 connman_inet_clear_ipv6_address(ipdevice->index,
581 ipdevice->config_ipv6->address->local,
582 ipdevice->config_ipv6->address->prefixlen);
585 static void update_stats(struct connman_ipdevice *ipdevice,
586 struct rtnl_link_stats *stats)
588 struct connman_service *service;
590 if (stats->rx_packets == 0 && stats->tx_packets == 0)
593 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
594 stats->rx_packets, stats->rx_bytes);
595 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
596 stats->tx_packets, stats->tx_bytes);
598 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
601 if (ipdevice->config_ipv4)
602 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
603 else if (ipdevice->config_ipv6)
604 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
611 ipdevice->rx_packets = stats->rx_packets;
612 ipdevice->tx_packets = stats->tx_packets;
613 ipdevice->rx_bytes = stats->rx_bytes;
614 ipdevice->tx_bytes = stats->tx_bytes;
615 ipdevice->rx_errors = stats->rx_errors;
616 ipdevice->tx_errors = stats->tx_errors;
617 ipdevice->rx_dropped = stats->rx_dropped;
618 ipdevice->tx_dropped = stats->tx_dropped;
620 __connman_service_notify(service,
621 ipdevice->rx_packets, ipdevice->tx_packets,
622 ipdevice->rx_bytes, ipdevice->tx_bytes,
623 ipdevice->rx_errors, ipdevice->tx_errors,
624 ipdevice->rx_dropped, ipdevice->tx_dropped);
627 void __connman_ipconfig_newlink(int index, unsigned short type,
628 unsigned int flags, const char *address,
630 struct rtnl_link_stats *stats)
632 struct connman_ipdevice *ipdevice;
635 gboolean up = FALSE, down = FALSE;
636 gboolean lower_up = FALSE, lower_down = FALSE;
638 DBG("index %d", index);
640 if (type == ARPHRD_LOOPBACK)
643 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
644 if (ipdevice != NULL) {
645 char *ifname = connman_inet_ifname(index);
646 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
647 DBG("interface name changed %s -> %s",
648 ipdevice->ifname, ifname);
650 g_free(ipdevice->ifname);
651 ipdevice->ifname = ifname;
658 ipdevice = g_try_new0(struct connman_ipdevice, 1);
659 if (ipdevice == NULL)
662 ipdevice->index = index;
663 ipdevice->ifname = connman_inet_ifname(index);
664 ipdevice->type = type;
666 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
667 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
669 ipdevice->address = g_strdup(address);
671 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
673 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
674 index, type, type2str(type));
679 update_stats(ipdevice, stats);
681 if (flags == ipdevice->flags)
684 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
691 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
692 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
693 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
694 (IFF_RUNNING | IFF_LOWER_UP))
696 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
700 ipdevice->flags = flags;
702 str = g_string_new(NULL);
707 g_string_append(str, "UP");
709 g_string_append(str, "DOWN");
711 if (flags & IFF_RUNNING)
712 g_string_append(str, ",RUNNING");
714 if (flags & IFF_LOWER_UP)
715 g_string_append(str, ",LOWER_UP");
717 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
720 g_string_free(str, TRUE);
722 for (list = g_list_first(ipconfig_list); list;
723 list = g_list_next(list)) {
724 struct connman_ipconfig *ipconfig = list->data;
726 if (index != ipconfig->index)
729 if (ipconfig->ops == NULL)
732 if (up == TRUE && ipconfig->ops->up)
733 ipconfig->ops->up(ipconfig);
734 if (lower_up == TRUE && ipconfig->ops->lower_up)
735 ipconfig->ops->lower_up(ipconfig);
737 if (lower_down == TRUE && ipconfig->ops->lower_down)
738 ipconfig->ops->lower_down(ipconfig);
739 if (down == TRUE && ipconfig->ops->down)
740 ipconfig->ops->down(ipconfig);
744 __connman_ipconfig_lower_up(ipdevice);
746 __connman_ipconfig_lower_down(ipdevice);
749 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
751 struct connman_ipdevice *ipdevice;
754 DBG("index %d", index);
756 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
757 if (ipdevice == NULL)
760 update_stats(ipdevice, stats);
762 for (list = g_list_first(ipconfig_list); list;
763 list = g_list_next(list)) {
764 struct connman_ipconfig *ipconfig = list->data;
766 if (index != ipconfig->index)
769 ipconfig->index = -1;
771 if (ipconfig->ops == NULL)
774 if (ipconfig->ops->lower_down)
775 ipconfig->ops->lower_down(ipconfig);
776 if (ipconfig->ops->down)
777 ipconfig->ops->down(ipconfig);
780 __connman_ipconfig_lower_down(ipdevice);
782 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
785 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
787 const struct connman_ipaddress *addr1 = a;
788 const struct connman_ipaddress *addr2 = b;
790 if (addr1->prefixlen != addr2->prefixlen)
791 return addr2->prefixlen - addr1->prefixlen;
793 return g_strcmp0(addr1->local, addr2->local);
796 void __connman_ipconfig_newaddr(int index, int family, const char *label,
797 unsigned char prefixlen, const char *address)
799 struct connman_ipdevice *ipdevice;
800 struct connman_ipaddress *ipaddress;
801 enum connman_ipconfig_type type;
804 DBG("index %d", index);
806 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
807 if (ipdevice == NULL)
810 ipaddress = connman_ipaddress_alloc(family);
811 if (ipaddress == NULL)
814 ipaddress->prefixlen = prefixlen;
815 ipaddress->local = g_strdup(address);
817 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
818 check_duplicate_address)) {
819 connman_ipaddress_free(ipaddress);
823 if (family == AF_INET)
824 type = CONNMAN_IPCONFIG_TYPE_IPV4;
825 else if (family == AF_INET6)
826 type = CONNMAN_IPCONFIG_TYPE_IPV6;
830 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
833 connman_info("%s {add} address %s/%u label %s family %d",
834 ipdevice->ifname, address, prefixlen, label, family);
836 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
837 __connman_ippool_newaddr(index, address, prefixlen);
839 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
840 connman_ipaddress_copy(ipdevice->config_ipv4->system,
843 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
844 connman_ipaddress_copy(ipdevice->config_ipv6->system,
849 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
852 for (list = g_list_first(ipconfig_list); list;
853 list = g_list_next(list)) {
854 struct connman_ipconfig *ipconfig = list->data;
856 if (index != ipconfig->index)
859 if (type != ipconfig->type)
862 if (ipconfig->ops == NULL)
865 if (ipconfig->ops->ip_bound)
866 ipconfig->ops->ip_bound(ipconfig);
870 void __connman_ipconfig_deladdr(int index, int family, const char *label,
871 unsigned char prefixlen, const char *address)
873 struct connman_ipdevice *ipdevice;
874 struct connman_ipaddress *ipaddress;
875 enum connman_ipconfig_type type;
878 DBG("index %d", index);
880 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
881 if (ipdevice == NULL)
884 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
885 if (ipaddress == NULL)
888 if (family == AF_INET)
889 type = CONNMAN_IPCONFIG_TYPE_IPV4;
890 else if (family == AF_INET6)
891 type = CONNMAN_IPCONFIG_TYPE_IPV6;
895 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
898 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
899 __connman_ippool_deladdr(index, address, prefixlen);
901 connman_ipaddress_clear(ipaddress);
904 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
905 address, prefixlen, label);
907 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
910 if (g_slist_length(ipdevice->address_list) > 0)
913 for (list = g_list_first(ipconfig_list); list;
914 list = g_list_next(list)) {
915 struct connman_ipconfig *ipconfig = list->data;
917 if (index != ipconfig->index)
920 if (type != ipconfig->type)
923 if (ipconfig->ops == NULL)
926 if (ipconfig->ops->ip_release)
927 ipconfig->ops->ip_release(ipconfig);
931 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
932 const char *dst, const char *gateway)
934 struct connman_ipdevice *ipdevice;
936 DBG("index %d", index);
938 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
939 if (ipdevice == NULL)
942 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
943 g_strcmp0(dst, "::") == 0)) {
945 enum connman_ipconfig_type type;
947 if (family == AF_INET6) {
948 type = CONNMAN_IPCONFIG_TYPE_IPV6;
949 g_free(ipdevice->ipv6_gateway);
950 ipdevice->ipv6_gateway = g_strdup(gateway);
952 if (ipdevice->config_ipv6 != NULL &&
953 ipdevice->config_ipv6->system != NULL) {
954 g_free(ipdevice->config_ipv6->system->gateway);
955 ipdevice->config_ipv6->system->gateway =
958 } else if (family == AF_INET) {
959 type = CONNMAN_IPCONFIG_TYPE_IPV4;
960 g_free(ipdevice->ipv4_gateway);
961 ipdevice->ipv4_gateway = g_strdup(gateway);
963 if (ipdevice->config_ipv4 != NULL &&
964 ipdevice->config_ipv4->system != NULL) {
965 g_free(ipdevice->config_ipv4->system->gateway);
966 ipdevice->config_ipv4->system->gateway =
972 for (config_list = g_list_first(ipconfig_list); config_list;
973 config_list = g_list_next(config_list)) {
974 struct connman_ipconfig *ipconfig = config_list->data;
976 if (index != ipconfig->index)
979 if (type != ipconfig->type)
982 if (ipconfig->ops == NULL)
985 if (ipconfig->ops->route_set)
986 ipconfig->ops->route_set(ipconfig);
990 connman_info("%s {add} route %s gw %s scope %u <%s>",
991 ipdevice->ifname, dst, gateway,
992 scope, scope2str(scope));
995 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
996 const char *dst, const char *gateway)
998 struct connman_ipdevice *ipdevice;
1000 DBG("index %d", index);
1002 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1003 if (ipdevice == NULL)
1006 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
1007 g_strcmp0(dst, "::") == 0)) {
1009 enum connman_ipconfig_type type;
1011 if (family == AF_INET6) {
1012 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1013 g_free(ipdevice->ipv6_gateway);
1014 ipdevice->ipv6_gateway = NULL;
1016 if (ipdevice->config_ipv6 != NULL &&
1017 ipdevice->config_ipv6->system != NULL) {
1018 g_free(ipdevice->config_ipv6->system->gateway);
1019 ipdevice->config_ipv6->system->gateway = NULL;
1021 } else if (family == AF_INET) {
1022 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1023 g_free(ipdevice->ipv4_gateway);
1024 ipdevice->ipv4_gateway = NULL;
1026 if (ipdevice->config_ipv4 != NULL &&
1027 ipdevice->config_ipv4->system != NULL) {
1028 g_free(ipdevice->config_ipv4->system->gateway);
1029 ipdevice->config_ipv4->system->gateway = NULL;
1034 for (config_list = g_list_first(ipconfig_list); config_list;
1035 config_list = g_list_next(config_list)) {
1036 struct connman_ipconfig *ipconfig = config_list->data;
1038 if (index != ipconfig->index)
1041 if (type != ipconfig->type)
1044 if (ipconfig->ops == NULL)
1047 if (ipconfig->ops->route_unset)
1048 ipconfig->ops->route_unset(ipconfig);
1052 connman_info("%s {del} route %s gw %s scope %u <%s>",
1053 ipdevice->ifname, dst, gateway,
1054 scope, scope2str(scope));
1057 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1062 keys = g_hash_table_get_keys(ipdevice_hash);
1066 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1067 int index = GPOINTER_TO_INT(list->data);
1069 function(index, user_data);
1075 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1076 struct connman_ipconfig *ipconfig)
1078 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1081 unsigned short __connman_ipconfig_get_type_from_index(int index)
1083 struct connman_ipdevice *ipdevice;
1085 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1086 if (ipdevice == NULL)
1089 return ipdevice->type;
1092 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1094 struct connman_ipdevice *ipdevice;
1096 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1097 if (ipdevice == NULL)
1100 return ipdevice->flags;
1103 const char *__connman_ipconfig_get_gateway_from_index(int index,
1104 enum connman_ipconfig_type type)
1106 struct connman_ipdevice *ipdevice;
1108 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1109 if (ipdevice == NULL)
1112 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1113 if (ipdevice->ipv4_gateway != NULL)
1114 return ipdevice->ipv4_gateway;
1116 if (ipdevice->config_ipv4 != NULL &&
1117 ipdevice->config_ipv4->address != NULL)
1118 return ipdevice->config_ipv4->address->gateway;
1121 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1122 if (ipdevice->ipv6_gateway != NULL)
1123 return ipdevice->ipv6_gateway;
1125 if (ipdevice->config_ipv6 != NULL &&
1126 ipdevice->config_ipv6->address != NULL)
1127 return ipdevice->config_ipv6->address->gateway;
1133 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1135 ipconfig->index = index;
1138 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1140 if (ipconfig->address == NULL)
1143 return ipconfig->address->local;
1146 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1148 if (ipconfig->address == NULL)
1151 g_free(ipconfig->address->local);
1152 ipconfig->address->local = g_strdup(address);
1155 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1157 if (ipconfig->address == NULL)
1160 return ipconfig->address->peer;
1163 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1165 if (ipconfig->address == NULL)
1168 g_free(ipconfig->address->peer);
1169 ipconfig->address->peer = g_strdup(address);
1172 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1174 if (ipconfig->address == NULL)
1177 return ipconfig->address->broadcast;
1180 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1182 if (ipconfig->address == NULL)
1185 g_free(ipconfig->address->broadcast);
1186 ipconfig->address->broadcast = g_strdup(broadcast);
1189 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1191 if (ipconfig->address == NULL)
1194 return ipconfig->address->gateway;
1197 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1201 if (ipconfig->address == NULL)
1203 g_free(ipconfig->address->gateway);
1204 ipconfig->address->gateway = g_strdup(gateway);
1207 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1209 struct connman_service *service;
1213 if (ipconfig->address == NULL)
1216 service = __connman_service_lookup_from_index(ipconfig->index);
1217 if (service == NULL)
1220 __connman_connection_gateway_remove(service, ipconfig->type);
1222 DBG("type %d gw %s peer %s", ipconfig->type,
1223 ipconfig->address->gateway, ipconfig->address->peer);
1225 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1226 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1227 return __connman_connection_gateway_add(service,
1228 ipconfig->address->gateway,
1230 ipconfig->address->peer);
1235 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1237 struct connman_service *service;
1241 service = __connman_service_lookup_from_index(ipconfig->index);
1242 if (service != NULL)
1243 __connman_connection_gateway_remove(service, ipconfig->type);
1246 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1248 if (ipconfig->address == NULL)
1251 return ipconfig->address->prefixlen;
1254 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1256 if (ipconfig->address == NULL)
1259 ipconfig->address->prefixlen = prefixlen;
1262 static struct connman_ipconfig *create_ipv6config(int index)
1264 struct connman_ipconfig *ipv6config;
1265 struct connman_ipdevice *ipdevice;
1267 DBG("index %d", index);
1269 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1270 if (ipv6config == NULL)
1273 ipv6config->refcount = 1;
1275 ipv6config->index = index;
1276 ipv6config->enabled = FALSE;
1277 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1279 if (is_ipv6_supported == FALSE)
1280 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1282 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1284 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1285 if (ipdevice != NULL)
1286 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1288 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1289 if (ipv6config->address == NULL) {
1294 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1296 DBG("ipconfig %p method %s", ipv6config,
1297 __connman_ipconfig_method2string(ipv6config->method));
1303 * connman_ipconfig_create:
1305 * Allocate a new ipconfig structure.
1307 * Returns: a newly-allocated #connman_ipconfig structure
1309 struct connman_ipconfig *__connman_ipconfig_create(int index,
1310 enum connman_ipconfig_type type)
1312 struct connman_ipconfig *ipconfig;
1314 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1315 return create_ipv6config(index);
1317 DBG("index %d", index);
1319 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1320 if (ipconfig == NULL)
1323 ipconfig->refcount = 1;
1325 ipconfig->index = index;
1326 ipconfig->enabled = FALSE;
1327 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1329 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1330 if (ipconfig->address == NULL) {
1335 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1337 DBG("ipconfig %p", ipconfig);
1344 * connman_ipconfig_ref:
1345 * @ipconfig: ipconfig structure
1347 * Increase reference counter of ipconfig
1349 struct connman_ipconfig *
1350 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1351 const char *file, int line, const char *caller)
1353 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1354 file, line, caller);
1356 __sync_fetch_and_add(&ipconfig->refcount, 1);
1362 * connman_ipconfig_unref:
1363 * @ipconfig: ipconfig structure
1365 * Decrease reference counter of ipconfig
1367 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1368 const char *file, int line, const char *caller)
1370 if (ipconfig == NULL)
1373 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1374 file, line, caller);
1376 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1379 if (__connman_ipconfig_disable(ipconfig) < 0)
1380 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1382 __connman_ipconfig_set_ops(ipconfig, NULL);
1384 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1385 __connman_ipconfig_unref(ipconfig->origin);
1386 ipconfig->origin = NULL;
1389 connman_ipaddress_free(ipconfig->system);
1390 connman_ipaddress_free(ipconfig->address);
1391 g_free(ipconfig->last_dhcp_address);
1396 * connman_ipconfig_get_data:
1397 * @ipconfig: ipconfig structure
1399 * Get private data pointer
1401 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1403 if (ipconfig == NULL)
1406 return ipconfig->ops_data;
1410 * connman_ipconfig_set_data:
1411 * @ipconfig: ipconfig structure
1412 * @data: data pointer
1414 * Set private data pointer
1416 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1418 ipconfig->ops_data = data;
1422 * connman_ipconfig_get_index:
1423 * @ipconfig: ipconfig structure
1425 * Get interface index
1427 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1429 if (ipconfig == NULL)
1432 if (ipconfig->origin != NULL)
1433 return ipconfig->origin->index;
1435 return ipconfig->index;
1439 * connman_ipconfig_get_ifname:
1440 * @ipconfig: ipconfig structure
1442 * Get interface name
1444 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1446 struct connman_ipdevice *ipdevice;
1448 if (ipconfig == NULL)
1451 if (ipconfig->index < 0)
1454 ipdevice = g_hash_table_lookup(ipdevice_hash,
1455 GINT_TO_POINTER(ipconfig->index));
1456 if (ipdevice == NULL)
1459 return ipdevice->ifname;
1463 * connman_ipconfig_set_ops:
1464 * @ipconfig: ipconfig structure
1465 * @ops: operation callbacks
1467 * Set the operation callbacks
1469 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1470 const struct connman_ipconfig_ops *ops)
1472 ipconfig->ops = ops;
1476 * connman_ipconfig_set_method:
1477 * @ipconfig: ipconfig structure
1478 * @method: configuration method
1480 * Set the configuration method
1482 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1483 enum connman_ipconfig_method method)
1485 ipconfig->method = method;
1490 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1492 if (ipconfig == NULL)
1493 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1495 return ipconfig->method;
1498 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1502 switch (ipconfig->method) {
1503 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1504 case CONNMAN_IPCONFIG_METHOD_OFF:
1506 case CONNMAN_IPCONFIG_METHOD_AUTO:
1507 case CONNMAN_IPCONFIG_METHOD_FIXED:
1508 case CONNMAN_IPCONFIG_METHOD_DHCP:
1509 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1510 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1511 return connman_inet_set_address(ipconfig->index,
1513 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1514 return connman_inet_set_ipv6_address(
1515 ipconfig->index, ipconfig->address);
1521 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1527 if (ipconfig == NULL)
1530 DBG("method %d", ipconfig->method);
1532 switch (ipconfig->method) {
1533 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1534 case CONNMAN_IPCONFIG_METHOD_OFF:
1536 case CONNMAN_IPCONFIG_METHOD_AUTO:
1537 case CONNMAN_IPCONFIG_METHOD_FIXED:
1538 case CONNMAN_IPCONFIG_METHOD_DHCP:
1539 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1540 err = __connman_ipconfig_address_unset(ipconfig);
1541 connman_ipaddress_clear(ipconfig->address);
1549 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1555 if (ipconfig == NULL)
1558 DBG("method %d", ipconfig->method);
1560 switch (ipconfig->method) {
1561 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1562 case CONNMAN_IPCONFIG_METHOD_OFF:
1564 case CONNMAN_IPCONFIG_METHOD_AUTO:
1565 case CONNMAN_IPCONFIG_METHOD_FIXED:
1566 case CONNMAN_IPCONFIG_METHOD_DHCP:
1567 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1568 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1569 err = connman_inet_clear_address(ipconfig->index,
1571 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1572 err = connman_inet_clear_ipv6_address(
1574 ipconfig->address->local,
1575 ipconfig->address->prefixlen);
1585 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1588 struct connman_ipdevice *ipdevice;
1590 DBG("ipconfig %p", ipconfig);
1592 if (ipconfig == NULL || ipconfig->index < 0)
1595 ipdevice = g_hash_table_lookup(ipdevice_hash,
1596 GINT_TO_POINTER(ipconfig->index));
1597 if (ipdevice == NULL)
1600 g_free(ipdevice->pac);
1601 ipdevice->pac = g_strdup(url);
1606 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1608 struct connman_ipdevice *ipdevice;
1610 DBG("ipconfig %p", ipconfig);
1612 if (ipconfig == NULL || ipconfig->index < 0)
1615 ipdevice = g_hash_table_lookup(ipdevice_hash,
1616 GINT_TO_POINTER(ipconfig->index));
1617 if (ipdevice == NULL)
1620 return ipdevice->pac;
1623 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1624 const char *address)
1626 if (ipconfig == NULL)
1629 g_free(ipconfig->last_dhcp_address);
1630 ipconfig->last_dhcp_address = g_strdup(address);
1633 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1635 if (ipconfig == NULL)
1638 return ipconfig->last_dhcp_address;
1641 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1643 struct connman_ipdevice *ipdevice;
1647 ipdevice = g_hash_table_lookup(ipdevice_hash,
1648 GINT_TO_POINTER(ipconfig->index));
1649 if (ipdevice == NULL)
1652 set_ipv6_state(ipdevice->ifname, FALSE);
1655 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1657 struct connman_ipdevice *ipdevice;
1661 ipdevice = g_hash_table_lookup(ipdevice_hash,
1662 GINT_TO_POINTER(ipconfig->index));
1663 if (ipdevice == NULL)
1666 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1667 set_ipv6_privacy(ipdevice->ifname,
1668 ipconfig->ipv6_privacy_config);
1670 set_ipv6_state(ipdevice->ifname, TRUE);
1673 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1675 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1678 enable_ipv6(ipconfig);
1681 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1683 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1686 disable_ipv6(ipconfig);
1689 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1691 if (ipconfig == NULL)
1694 switch (ipconfig->method) {
1695 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1696 case CONNMAN_IPCONFIG_METHOD_OFF:
1698 case CONNMAN_IPCONFIG_METHOD_AUTO:
1699 case CONNMAN_IPCONFIG_METHOD_FIXED:
1700 case CONNMAN_IPCONFIG_METHOD_DHCP:
1701 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1708 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1710 struct connman_ipdevice *ipdevice;
1711 gboolean up = FALSE, down = FALSE;
1712 gboolean lower_up = FALSE, lower_down = FALSE;
1713 enum connman_ipconfig_type type;
1715 DBG("ipconfig %p", ipconfig);
1717 if (ipconfig == NULL || ipconfig->index < 0)
1720 ipdevice = g_hash_table_lookup(ipdevice_hash,
1721 GINT_TO_POINTER(ipconfig->index));
1722 if (ipdevice == NULL)
1725 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1726 if (ipdevice->config_ipv4 == ipconfig)
1728 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1729 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1730 if (ipdevice->config_ipv6 == ipconfig)
1732 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1733 enable_ipv6(ipconfig);
1737 ipconfig->enabled = TRUE;
1739 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1740 ipdevice->config_ipv4 != NULL) {
1741 ipconfig_list = g_list_remove(ipconfig_list,
1742 ipdevice->config_ipv4);
1744 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1746 __connman_ipconfig_unref(ipdevice->config_ipv4);
1749 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1750 ipdevice->config_ipv6 != NULL) {
1751 ipconfig_list = g_list_remove(ipconfig_list,
1752 ipdevice->config_ipv6);
1754 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1756 __connman_ipconfig_unref(ipdevice->config_ipv6);
1759 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1760 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1761 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1762 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1764 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1766 if (ipdevice->flags & IFF_UP)
1771 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1772 (IFF_RUNNING | IFF_LOWER_UP))
1774 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1777 if (up == TRUE && ipconfig->ops->up)
1778 ipconfig->ops->up(ipconfig);
1779 if (lower_up == TRUE && ipconfig->ops->lower_up)
1780 ipconfig->ops->lower_up(ipconfig);
1782 if (lower_down == TRUE && ipconfig->ops->lower_down)
1783 ipconfig->ops->lower_down(ipconfig);
1784 if (down == TRUE && ipconfig->ops->down)
1785 ipconfig->ops->down(ipconfig);
1790 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1792 struct connman_ipdevice *ipdevice;
1794 DBG("ipconfig %p", ipconfig);
1796 if (ipconfig == NULL || ipconfig->index < 0)
1799 ipdevice = g_hash_table_lookup(ipdevice_hash,
1800 GINT_TO_POINTER(ipconfig->index));
1801 if (ipdevice == NULL)
1804 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1807 ipconfig->enabled = FALSE;
1809 if (ipdevice->config_ipv4 == ipconfig) {
1810 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1812 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1813 __connman_ipconfig_unref(ipdevice->config_ipv4);
1814 ipdevice->config_ipv4 = NULL;
1818 if (ipdevice->config_ipv6 == ipconfig) {
1819 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1821 if (ipdevice->config_ipv6->method ==
1822 CONNMAN_IPCONFIG_METHOD_AUTO)
1823 disable_ipv6(ipdevice->config_ipv6);
1825 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1826 __connman_ipconfig_unref(ipdevice->config_ipv6);
1827 ipdevice->config_ipv6 = NULL;
1834 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1837 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1839 case CONNMAN_IPCONFIG_METHOD_OFF:
1841 case CONNMAN_IPCONFIG_METHOD_FIXED:
1843 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1845 case CONNMAN_IPCONFIG_METHOD_DHCP:
1847 case CONNMAN_IPCONFIG_METHOD_AUTO:
1854 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1856 if (g_strcmp0(method, "off") == 0)
1857 return CONNMAN_IPCONFIG_METHOD_OFF;
1858 else if (g_strcmp0(method, "fixed") == 0)
1859 return CONNMAN_IPCONFIG_METHOD_FIXED;
1860 else if (g_strcmp0(method, "manual") == 0)
1861 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1862 else if (g_strcmp0(method, "dhcp") == 0)
1863 return CONNMAN_IPCONFIG_METHOD_DHCP;
1864 else if (g_strcmp0(method, "auto") == 0)
1865 return CONNMAN_IPCONFIG_METHOD_AUTO;
1867 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1870 static const char *privacy2string(int privacy)
1874 else if (privacy == 1)
1876 else if (privacy > 1)
1882 static int string2privacy(const char *privacy)
1884 if (g_strcmp0(privacy, "disabled") == 0)
1886 else if (g_strcmp0(privacy, "enabled") == 0)
1888 else if (g_strcmp0(privacy, "prefered") == 0)
1894 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1895 DBusMessageIter *iter)
1897 struct connman_ipaddress *append_addr = NULL;
1902 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1905 str = __connman_ipconfig_method2string(ipconfig->method);
1909 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1911 switch (ipconfig->method) {
1912 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1913 case CONNMAN_IPCONFIG_METHOD_OFF:
1914 case CONNMAN_IPCONFIG_METHOD_AUTO:
1917 case CONNMAN_IPCONFIG_METHOD_FIXED:
1918 append_addr = ipconfig->address;
1921 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1922 case CONNMAN_IPCONFIG_METHOD_DHCP:
1923 append_addr = ipconfig->system;
1927 if (append_addr == NULL)
1930 if (append_addr->local != NULL) {
1932 struct in_addr netmask;
1935 connman_dbus_dict_append_basic(iter, "Address",
1936 DBUS_TYPE_STRING, &append_addr->local);
1938 addr = 0xffffffff << (32 - append_addr->prefixlen);
1939 netmask.s_addr = htonl(addr);
1940 mask = inet_ntoa(netmask);
1941 connman_dbus_dict_append_basic(iter, "Netmask",
1942 DBUS_TYPE_STRING, &mask);
1945 if (append_addr->gateway != NULL)
1946 connman_dbus_dict_append_basic(iter, "Gateway",
1947 DBUS_TYPE_STRING, &append_addr->gateway);
1950 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1951 DBusMessageIter *iter,
1952 struct connman_ipconfig *ipconfig_ipv4)
1954 struct connman_ipaddress *append_addr = NULL;
1955 const char *str, *privacy;
1959 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1962 str = __connman_ipconfig_method2string(ipconfig->method);
1966 if (ipconfig_ipv4 != NULL &&
1967 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1968 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1972 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1974 switch (ipconfig->method) {
1975 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1976 case CONNMAN_IPCONFIG_METHOD_OFF:
1979 case CONNMAN_IPCONFIG_METHOD_FIXED:
1980 append_addr = ipconfig->address;
1983 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1984 case CONNMAN_IPCONFIG_METHOD_DHCP:
1985 case CONNMAN_IPCONFIG_METHOD_AUTO:
1986 append_addr = ipconfig->system;
1990 if (append_addr == NULL)
1993 if (append_addr->local != NULL) {
1994 connman_dbus_dict_append_basic(iter, "Address",
1995 DBUS_TYPE_STRING, &append_addr->local);
1996 connman_dbus_dict_append_basic(iter, "PrefixLength",
1998 &append_addr->prefixlen);
2001 if (append_addr->gateway != NULL)
2002 connman_dbus_dict_append_basic(iter, "Gateway",
2003 DBUS_TYPE_STRING, &append_addr->gateway);
2005 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2006 connman_dbus_dict_append_basic(iter, "Privacy",
2007 DBUS_TYPE_STRING, &privacy);
2010 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2011 DBusMessageIter *iter)
2013 const char *str, *privacy;
2017 str = __connman_ipconfig_method2string(ipconfig->method);
2021 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2023 switch (ipconfig->method) {
2024 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2025 case CONNMAN_IPCONFIG_METHOD_OFF:
2026 case CONNMAN_IPCONFIG_METHOD_DHCP:
2028 case CONNMAN_IPCONFIG_METHOD_FIXED:
2029 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2030 case CONNMAN_IPCONFIG_METHOD_AUTO:
2034 if (ipconfig->address == NULL)
2037 if (ipconfig->address->local != NULL) {
2038 connman_dbus_dict_append_basic(iter, "Address",
2039 DBUS_TYPE_STRING, &ipconfig->address->local);
2040 connman_dbus_dict_append_basic(iter, "PrefixLength",
2042 &ipconfig->address->prefixlen);
2045 if (ipconfig->address->gateway != NULL)
2046 connman_dbus_dict_append_basic(iter, "Gateway",
2047 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2049 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2050 connman_dbus_dict_append_basic(iter, "Privacy",
2051 DBUS_TYPE_STRING, &privacy);
2054 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2055 DBusMessageIter *iter)
2061 str = __connman_ipconfig_method2string(ipconfig->method);
2065 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2067 switch (ipconfig->method) {
2068 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2069 case CONNMAN_IPCONFIG_METHOD_OFF:
2070 case CONNMAN_IPCONFIG_METHOD_DHCP:
2071 case CONNMAN_IPCONFIG_METHOD_AUTO:
2073 case CONNMAN_IPCONFIG_METHOD_FIXED:
2074 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2078 if (ipconfig->address == NULL)
2081 if (ipconfig->address->local != NULL) {
2083 struct in_addr netmask;
2086 connman_dbus_dict_append_basic(iter, "Address",
2087 DBUS_TYPE_STRING, &ipconfig->address->local);
2089 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2090 netmask.s_addr = htonl(addr);
2091 mask = inet_ntoa(netmask);
2092 connman_dbus_dict_append_basic(iter, "Netmask",
2093 DBUS_TYPE_STRING, &mask);
2096 if (ipconfig->address->gateway != NULL)
2097 connman_dbus_dict_append_basic(iter, "Gateway",
2098 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2101 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2102 DBusMessageIter *array)
2104 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2105 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2106 *privacy_string = NULL;
2107 int prefix_length = 0, privacy = 0;
2108 DBusMessageIter dict;
2110 DBG("ipconfig %p", ipconfig);
2112 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2115 dbus_message_iter_recurse(array, &dict);
2117 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2118 DBusMessageIter entry, value;
2122 dbus_message_iter_recurse(&dict, &entry);
2124 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2127 dbus_message_iter_get_basic(&entry, &key);
2128 dbus_message_iter_next(&entry);
2130 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2133 dbus_message_iter_recurse(&entry, &value);
2135 type = dbus_message_iter_get_arg_type(&value);
2137 if (g_str_equal(key, "Method") == TRUE) {
2140 if (type != DBUS_TYPE_STRING)
2143 dbus_message_iter_get_basic(&value, &str);
2144 method = __connman_ipconfig_string2method(str);
2145 } else if (g_str_equal(key, "Address") == TRUE) {
2146 if (type != DBUS_TYPE_STRING)
2149 dbus_message_iter_get_basic(&value, &address);
2150 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2151 if (type != DBUS_TYPE_BYTE)
2154 dbus_message_iter_get_basic(&value, &prefix_length);
2156 if (prefix_length < 0 || prefix_length > 128)
2158 } else if (g_str_equal(key, "Netmask") == TRUE) {
2159 if (type != DBUS_TYPE_STRING)
2162 dbus_message_iter_get_basic(&value, &netmask);
2163 } else if (g_str_equal(key, "Gateway") == TRUE) {
2164 if (type != DBUS_TYPE_STRING)
2167 dbus_message_iter_get_basic(&value, &gateway);
2168 } else if (g_str_equal(key, "Privacy") == TRUE) {
2169 if (type != DBUS_TYPE_STRING)
2172 dbus_message_iter_get_basic(&value, &privacy_string);
2173 privacy = string2privacy(privacy_string);
2176 dbus_message_iter_next(&dict);
2179 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2181 method, address, netmask, gateway, prefix_length,
2185 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2186 case CONNMAN_IPCONFIG_METHOD_FIXED:
2189 case CONNMAN_IPCONFIG_METHOD_OFF:
2190 ipconfig->method = method;
2191 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2192 disable_ipv6(ipconfig);
2195 case CONNMAN_IPCONFIG_METHOD_AUTO:
2196 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2199 ipconfig->method = method;
2200 if (privacy_string != NULL)
2201 ipconfig->ipv6_privacy_config = privacy;
2202 enable_ipv6(ipconfig);
2205 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2206 if (address == NULL)
2209 ipconfig->method = method;
2211 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2212 connman_ipaddress_set_ipv4(ipconfig->address,
2213 address, netmask, gateway);
2215 return connman_ipaddress_set_ipv6(
2216 ipconfig->address, address,
2217 prefix_length, gateway);
2220 case CONNMAN_IPCONFIG_METHOD_DHCP:
2221 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2224 ipconfig->method = method;
2231 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2232 DBusMessageIter *iter)
2234 struct connman_ipdevice *ipdevice;
2235 const char *method = "auto";
2237 connman_dbus_dict_append_basic(iter, "Method",
2238 DBUS_TYPE_STRING, &method);
2240 ipdevice = g_hash_table_lookup(ipdevice_hash,
2241 GINT_TO_POINTER(ipconfig->index));
2242 if (ipdevice == NULL)
2245 if (ipdevice->ifname != NULL)
2246 connman_dbus_dict_append_basic(iter, "Interface",
2247 DBUS_TYPE_STRING, &ipdevice->ifname);
2249 if (ipdevice->address != NULL)
2250 connman_dbus_dict_append_basic(iter, "Address",
2251 DBUS_TYPE_STRING, &ipdevice->address);
2253 if (ipdevice->mtu > 0)
2254 connman_dbus_dict_append_basic(iter, "MTU",
2255 DBUS_TYPE_UINT16, &ipdevice->mtu);
2258 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2259 GKeyFile *keyfile, const char *identifier, const char *prefix)
2265 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2267 key = g_strdup_printf("%smethod", prefix);
2268 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2269 if (method == NULL) {
2270 switch (ipconfig->type) {
2271 case CONNMAN_IPCONFIG_TYPE_IPV4:
2272 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2274 case CONNMAN_IPCONFIG_TYPE_IPV6:
2275 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2277 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2278 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2282 ipconfig->method = __connman_ipconfig_string2method(method);
2284 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2285 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2287 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2288 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2289 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2291 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2292 privacy = g_key_file_get_string(keyfile, identifier,
2294 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2303 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2304 ipconfig->address->prefixlen = g_key_file_get_integer(
2305 keyfile, identifier, key, NULL);
2308 key = g_strdup_printf("%slocal_address", prefix);
2309 ipconfig->address->local = g_key_file_get_string(
2310 keyfile, identifier, key, NULL);
2313 key = g_strdup_printf("%speer_address", prefix);
2314 ipconfig->address->peer = g_key_file_get_string(
2315 keyfile, identifier, key, NULL);
2318 key = g_strdup_printf("%sbroadcast_address", prefix);
2319 ipconfig->address->broadcast = g_key_file_get_string(
2320 keyfile, identifier, key, NULL);
2323 key = g_strdup_printf("%sgateway", prefix);
2324 ipconfig->address->gateway = g_key_file_get_string(
2325 keyfile, identifier, key, NULL);
2328 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2329 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2331 g_free(ipconfig->last_dhcp_address);
2332 ipconfig->last_dhcp_address = str;
2339 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2340 GKeyFile *keyfile, const char *identifier, const char *prefix)
2345 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2347 method = __connman_ipconfig_method2string(ipconfig->method);
2349 key = g_strdup_printf("%smethod", prefix);
2350 g_key_file_set_string(keyfile, identifier, key, method);
2353 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2354 const char *privacy;
2355 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2356 key = g_strdup_printf("%sprivacy", prefix);
2357 g_key_file_set_string(keyfile, identifier, key, privacy);
2361 switch (ipconfig->method) {
2362 case CONNMAN_IPCONFIG_METHOD_FIXED:
2363 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2365 case CONNMAN_IPCONFIG_METHOD_DHCP:
2366 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2367 if (ipconfig->last_dhcp_address != NULL &&
2368 strlen(ipconfig->last_dhcp_address) > 0)
2369 g_key_file_set_string(keyfile, identifier, key,
2370 ipconfig->last_dhcp_address);
2372 g_key_file_remove_key(keyfile, identifier, key, NULL);
2375 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2376 case CONNMAN_IPCONFIG_METHOD_OFF:
2377 case CONNMAN_IPCONFIG_METHOD_AUTO:
2381 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2382 if (ipconfig->address->prefixlen != 0)
2383 g_key_file_set_integer(keyfile, identifier,
2384 key, ipconfig->address->prefixlen);
2387 key = g_strdup_printf("%slocal_address", prefix);
2388 if (ipconfig->address->local != NULL)
2389 g_key_file_set_string(keyfile, identifier,
2390 key, ipconfig->address->local);
2393 key = g_strdup_printf("%speer_address", prefix);
2394 if (ipconfig->address->peer != NULL)
2395 g_key_file_set_string(keyfile, identifier,
2396 key, ipconfig->address->peer);
2399 key = g_strdup_printf("%sbroadcast_address", prefix);
2400 if (ipconfig->address->broadcast != NULL)
2401 g_key_file_set_string(keyfile, identifier,
2402 key, ipconfig->address->broadcast);
2405 key = g_strdup_printf("%sgateway", prefix);
2406 if (ipconfig->address->gateway != NULL)
2407 g_key_file_set_string(keyfile, identifier,
2408 key, ipconfig->address->gateway);
2414 int __connman_ipconfig_init(void)
2418 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2419 NULL, free_ipdevice);
2421 is_ipv6_supported = connman_inet_is_ipv6_supported();
2426 void __connman_ipconfig_cleanup(void)
2430 g_hash_table_destroy(ipdevice_hash);
2431 ipdevice_hash = NULL;