5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <net/if_arp.h>
29 #include <linux/if_link.h>
34 #define IFF_LOWER_UP 0x10000
41 struct connman_ipconfig {
44 enum connman_ipconfig_type type;
46 struct connman_ipconfig *origin;
48 const struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
52 struct connman_ipaddress *address;
53 struct connman_ipaddress *system;
55 int ipv6_privacy_config;
58 struct connman_ipdevice {
80 struct connman_ipconfig *config_ipv4;
81 struct connman_ipconfig *config_ipv6;
83 gboolean ipv6_enabled;
87 static GHashTable *ipdevice_hash = NULL;
88 static GList *ipconfig_list = NULL;
90 struct connman_ipaddress *connman_ipaddress_alloc(int family)
92 struct connman_ipaddress *ipaddress;
94 ipaddress = g_try_new0(struct connman_ipaddress, 1);
95 if (ipaddress == NULL)
98 ipaddress->family = family;
99 ipaddress->prefixlen = 0;
100 ipaddress->local = NULL;
101 ipaddress->peer = NULL;
102 ipaddress->broadcast = NULL;
103 ipaddress->gateway = NULL;
108 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
110 if (ipaddress == NULL)
113 g_free(ipaddress->broadcast);
114 g_free(ipaddress->peer);
115 g_free(ipaddress->local);
116 g_free(ipaddress->gateway);
120 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
129 mask = inet_network(netmask);
132 /* a valid netmask must be 2^n - 1 */
133 if ((host & (host + 1)) != 0)
137 for (; mask; mask <<= 1)
143 static gboolean check_ipv6_address(const char *address)
145 unsigned char buf[sizeof(struct in6_addr)];
148 err = inet_pton(AF_INET6, address, buf);
155 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
156 const char *address, const char *gateway,
157 unsigned char prefix_length)
159 if (ipaddress == NULL)
162 if (check_ipv6_address(address) == FALSE)
165 if (check_ipv6_address(gateway) == FALSE)
168 DBG("prefix_len %d address %s gateway %s",
169 prefix_length, address, gateway);
171 ipaddress->family = CONNMAN_IPCONFIG_TYPE_IPV6;
173 ipaddress->prefixlen = prefix_length;
175 g_free(ipaddress->local);
176 ipaddress->local = g_strdup(address);
178 g_free(ipaddress->gateway);
179 ipaddress->gateway = g_strdup(gateway);
184 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
185 const char *address, const char *netmask, const char *gateway)
187 if (ipaddress == NULL)
190 ipaddress->family = CONNMAN_IPCONFIG_TYPE_IPV4;
192 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
194 g_free(ipaddress->local);
195 ipaddress->local = g_strdup(address);
197 g_free(ipaddress->gateway);
198 ipaddress->gateway = g_strdup(gateway);
203 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
206 if (ipaddress == NULL)
209 g_free(ipaddress->peer);
210 ipaddress->peer = g_strdup(peer);
213 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
215 if (ipaddress == NULL)
218 ipaddress->prefixlen = 0;
220 g_free(ipaddress->local);
221 ipaddress->local = NULL;
223 g_free(ipaddress->peer);
224 ipaddress->peer = NULL;
226 g_free(ipaddress->broadcast);
227 ipaddress->broadcast = NULL;
229 g_free(ipaddress->gateway);
230 ipaddress->gateway = NULL;
233 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
234 struct connman_ipaddress *source)
236 if (ipaddress == NULL || source == NULL)
239 ipaddress->family = source->family;
240 ipaddress->prefixlen = source->prefixlen;
242 g_free(ipaddress->local);
243 ipaddress->local = g_strdup(source->local);
245 g_free(ipaddress->peer);
246 ipaddress->peer = g_strdup(source->peer);
248 g_free(ipaddress->broadcast);
249 ipaddress->broadcast = g_strdup(source->broadcast);
251 g_free(ipaddress->gateway);
252 ipaddress->gateway = g_strdup(source->gateway);
255 static void free_address_list(struct connman_ipdevice *ipdevice)
259 for (list = ipdevice->address_list; list; list = list->next) {
260 struct connman_ipaddress *ipaddress = list->data;
262 connman_ipaddress_free(ipaddress);
266 g_slist_free(ipdevice->address_list);
267 ipdevice->address_list = NULL;
270 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
271 unsigned char prefixlen, const char *local)
275 for (list = ipdevice->address_list; list; list = list->next) {
276 struct connman_ipaddress *ipaddress = list->data;
278 if (g_strcmp0(ipaddress->local, local) == 0 &&
279 ipaddress->prefixlen == prefixlen)
286 static const char *type2str(unsigned short type)
291 case ARPHRD_LOOPBACK:
304 static const char *scope2str(unsigned char scope)
316 static gboolean get_ipv6_state(gchar *ifname)
321 gboolean enabled = FALSE;
324 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
326 path = g_strdup_printf(
327 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
332 f = fopen(path, "r");
337 if (fscanf(f, "%d", &disabled) > 0)
345 static void set_ipv6_state(gchar *ifname, gboolean enable)
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+");
374 static int get_ipv6_privacy(gchar *ifname)
383 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
389 f = fopen(path, "r");
396 if (fscanf(f, "%d", &value) <= 0)
404 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
405 * The privacy extension is described in RFC 3041 and RFC 4941
407 static void set_ipv6_privacy(gchar *ifname, int value)
415 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
424 f = fopen(path, "r+");
431 fprintf(f, "%d", value);
435 static void free_ipdevice(gpointer data)
437 struct connman_ipdevice *ipdevice = data;
439 connman_info("%s {remove} index %d", ipdevice->ifname,
442 if (ipdevice->config_ipv4 != NULL) {
443 connman_ipconfig_unref(ipdevice->config_ipv4);
444 ipdevice->config_ipv4 = NULL;
447 if (ipdevice->config_ipv6 != NULL) {
448 connman_ipconfig_unref(ipdevice->config_ipv6);
449 ipdevice->config_ipv6 = NULL;
452 free_address_list(ipdevice);
453 g_free(ipdevice->ipv4_gateway);
454 g_free(ipdevice->ipv6_gateway);
455 g_free(ipdevice->pac);
457 g_free(ipdevice->address);
459 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
460 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
462 g_free(ipdevice->ifname);
466 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
468 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
469 ipdevice->config_ipv6);
472 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
474 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
475 ipdevice->config_ipv6);
477 if (ipdevice->config_ipv4)
478 connman_inet_clear_address(ipdevice->index,
479 ipdevice->config_ipv4->address);
481 if (ipdevice->config_ipv6)
482 connman_inet_clear_ipv6_address(ipdevice->index,
483 ipdevice->config_ipv6->address->local,
484 ipdevice->config_ipv6->address->prefixlen);
487 static void update_stats(struct connman_ipdevice *ipdevice,
488 struct rtnl_link_stats *stats)
490 struct connman_service *service;
492 if (stats->rx_packets == 0 && stats->tx_packets == 0)
495 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
496 stats->rx_packets, stats->rx_bytes);
497 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
498 stats->tx_packets, stats->tx_bytes);
500 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
503 if (ipdevice->config_ipv4)
504 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
505 else if (ipdevice->config_ipv6)
506 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
513 ipdevice->rx_packets = stats->rx_packets;
514 ipdevice->tx_packets = stats->tx_packets;
515 ipdevice->rx_bytes = stats->rx_bytes;
516 ipdevice->tx_bytes = stats->tx_bytes;
517 ipdevice->rx_errors = stats->rx_errors;
518 ipdevice->tx_errors = stats->tx_errors;
519 ipdevice->rx_dropped = stats->rx_dropped;
520 ipdevice->tx_dropped = stats->tx_dropped;
522 __connman_service_notify(service,
523 ipdevice->rx_packets, ipdevice->tx_packets,
524 ipdevice->rx_bytes, ipdevice->tx_bytes,
525 ipdevice->rx_errors, ipdevice->tx_errors,
526 ipdevice->rx_dropped, ipdevice->tx_dropped);
529 void __connman_ipconfig_newlink(int index, unsigned short type,
530 unsigned int flags, const char *address,
532 struct rtnl_link_stats *stats)
534 struct connman_ipdevice *ipdevice;
537 gboolean up = FALSE, down = FALSE;
538 gboolean lower_up = FALSE, lower_down = FALSE;
540 DBG("index %d", index);
542 if (type == ARPHRD_LOOPBACK)
545 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
546 if (ipdevice != NULL)
549 ipdevice = g_try_new0(struct connman_ipdevice, 1);
550 if (ipdevice == NULL)
553 ipdevice->index = index;
554 ipdevice->ifname = connman_inet_ifname(index);
555 ipdevice->type = type;
557 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
558 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
560 ipdevice->address = g_strdup(address);
562 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
564 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
565 index, type, type2str(type));
570 update_stats(ipdevice, stats);
572 if (flags == ipdevice->flags)
575 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
582 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
583 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
584 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
585 (IFF_RUNNING | IFF_LOWER_UP))
587 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
591 ipdevice->flags = flags;
593 str = g_string_new(NULL);
598 g_string_append(str, "UP");
600 g_string_append(str, "DOWN");
602 if (flags & IFF_RUNNING)
603 g_string_append(str, ",RUNNING");
605 if (flags & IFF_LOWER_UP)
606 g_string_append(str, ",LOWER_UP");
608 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
611 g_string_free(str, TRUE);
613 for (list = g_list_first(ipconfig_list); list;
614 list = g_list_next(list)) {
615 struct connman_ipconfig *ipconfig = list->data;
617 if (index != ipconfig->index)
620 if (ipconfig->ops == NULL)
623 if (up == TRUE && ipconfig->ops->up)
624 ipconfig->ops->up(ipconfig);
625 if (lower_up == TRUE && ipconfig->ops->lower_up)
626 ipconfig->ops->lower_up(ipconfig);
628 if (lower_down == TRUE && ipconfig->ops->lower_down)
629 ipconfig->ops->lower_down(ipconfig);
630 if (down == TRUE && ipconfig->ops->down)
631 ipconfig->ops->down(ipconfig);
635 __connman_ipconfig_lower_up(ipdevice);
637 __connman_ipconfig_lower_down(ipdevice);
640 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
642 struct connman_ipdevice *ipdevice;
645 DBG("index %d", index);
647 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
648 if (ipdevice == NULL)
651 update_stats(ipdevice, stats);
653 for (list = g_list_first(ipconfig_list); list;
654 list = g_list_next(list)) {
655 struct connman_ipconfig *ipconfig = list->data;
657 if (index != ipconfig->index)
660 ipconfig->index = -1;
662 if (ipconfig->ops == NULL)
665 if (ipconfig->ops->lower_down)
666 ipconfig->ops->lower_down(ipconfig);
667 if (ipconfig->ops->down)
668 ipconfig->ops->down(ipconfig);
671 __connman_ipconfig_lower_down(ipdevice);
673 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
676 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
678 const struct connman_ipaddress *addr1 = a;
679 const struct connman_ipaddress *addr2 = b;
681 if (addr1->prefixlen != addr2->prefixlen)
682 return addr2->prefixlen - addr1->prefixlen;
684 return g_strcmp0(addr1->local, addr2->local);
687 void __connman_ipconfig_newaddr(int index, int family, const char *label,
688 unsigned char prefixlen, const char *address)
690 struct connman_ipdevice *ipdevice;
691 struct connman_ipaddress *ipaddress;
694 DBG("index %d", index);
696 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
697 if (ipdevice == NULL)
700 ipaddress = connman_ipaddress_alloc(family);
701 if (ipaddress == NULL)
704 ipaddress->prefixlen = prefixlen;
705 ipaddress->local = g_strdup(address);
707 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
708 check_duplicate_address)) {
709 connman_ipaddress_free(ipaddress);
713 ipdevice->address_list = g_slist_append(ipdevice->address_list,
716 connman_info("%s {add} address %s/%u label %s family %d",
717 ipdevice->ifname, address, prefixlen, label, family);
719 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
720 connman_ipaddress_copy(ipdevice->config_ipv4->system,
723 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
724 connman_ipaddress_copy(ipdevice->config_ipv6->system,
729 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
732 for (list = g_list_first(ipconfig_list); list;
733 list = g_list_next(list)) {
734 struct connman_ipconfig *ipconfig = list->data;
736 if (index != ipconfig->index)
739 if (ipconfig->ops == NULL)
742 if (ipconfig->ops->ip_bound)
743 ipconfig->ops->ip_bound(ipconfig);
747 void __connman_ipconfig_deladdr(int index, int family, const char *label,
748 unsigned char prefixlen, const char *address)
750 struct connman_ipdevice *ipdevice;
751 struct connman_ipaddress *ipaddress;
754 DBG("index %d", index);
756 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
757 if (ipdevice == NULL)
760 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
761 if (ipaddress == NULL)
764 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
767 connman_ipaddress_clear(ipaddress);
770 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
771 address, prefixlen, label);
773 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
776 if (g_slist_length(ipdevice->address_list) > 0)
779 for (list = g_list_first(ipconfig_list); list;
780 list = g_list_next(list)) {
781 struct connman_ipconfig *ipconfig = list->data;
783 if (index != ipconfig->index)
786 if (ipconfig->ops == NULL)
789 if (ipconfig->ops->ip_release)
790 ipconfig->ops->ip_release(ipconfig);
794 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
795 const char *dst, const char *gateway)
797 struct connman_ipdevice *ipdevice;
799 DBG("index %d", index);
801 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
802 if (ipdevice == NULL)
805 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
809 if (family == AF_INET6) {
810 g_free(ipdevice->ipv6_gateway);
811 ipdevice->ipv6_gateway = g_strdup(gateway);
813 if (ipdevice->config_ipv6 != NULL &&
814 ipdevice->config_ipv6->system != NULL) {
815 g_free(ipdevice->config_ipv6->system->gateway);
816 ipdevice->config_ipv6->system->gateway =
820 g_free(ipdevice->ipv4_gateway);
821 ipdevice->ipv4_gateway = g_strdup(gateway);
823 if (ipdevice->config_ipv4 != NULL &&
824 ipdevice->config_ipv4->system != NULL) {
825 g_free(ipdevice->config_ipv4->system->gateway);
826 ipdevice->config_ipv4->system->gateway =
831 for (list = ipdevice->address_list; list; list = list->next) {
832 struct connman_ipaddress *ipaddress = list->data;
834 g_free(ipaddress->gateway);
835 ipaddress->gateway = g_strdup(gateway);
838 for (config_list = g_list_first(ipconfig_list); config_list;
839 config_list = g_list_next(config_list)) {
840 struct connman_ipconfig *ipconfig = config_list->data;
842 if (index != ipconfig->index)
845 if (ipconfig->ops == NULL)
848 if (ipconfig->ops->ip_bound)
849 ipconfig->ops->ip_bound(ipconfig);
853 connman_info("%s {add} route %s gw %s scope %u <%s>",
854 ipdevice->ifname, dst, gateway,
855 scope, scope2str(scope));
858 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
859 const char *dst, const char *gateway)
861 struct connman_ipdevice *ipdevice;
863 DBG("index %d", index);
865 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
866 if (ipdevice == NULL)
869 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
873 if (family == AF_INET6) {
874 g_free(ipdevice->ipv6_gateway);
875 ipdevice->ipv6_gateway = NULL;
877 if (ipdevice->config_ipv6 != NULL &&
878 ipdevice->config_ipv6->system != NULL) {
879 g_free(ipdevice->config_ipv6->system->gateway);
880 ipdevice->config_ipv6->system->gateway = NULL;
883 g_free(ipdevice->ipv4_gateway);
884 ipdevice->ipv4_gateway = NULL;
886 if (ipdevice->config_ipv4 != NULL &&
887 ipdevice->config_ipv4->system != NULL) {
888 g_free(ipdevice->config_ipv4->system->gateway);
889 ipdevice->config_ipv4->system->gateway = NULL;
893 for (list = ipdevice->address_list; list; list = list->next) {
894 struct connman_ipaddress *ipaddress = list->data;
896 g_free(ipaddress->gateway);
897 ipaddress->gateway = NULL;
900 for (config_list = g_list_first(ipconfig_list); config_list;
901 config_list = g_list_next(config_list)) {
902 struct connman_ipconfig *ipconfig = config_list->data;
904 if (index != ipconfig->index)
907 if (ipconfig->ops == NULL)
910 if (ipconfig->ops->ip_release)
911 ipconfig->ops->ip_release(ipconfig);
915 connman_info("%s {del} route %s gw %s scope %u <%s>",
916 ipdevice->ifname, dst, gateway,
917 scope, scope2str(scope));
920 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
925 keys = g_hash_table_get_keys(ipdevice_hash);
929 for (list = g_list_first(keys); list; list = g_list_next(list)) {
930 int index = GPOINTER_TO_INT(list->data);
932 function(index, user_data);
938 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
939 struct connman_ipconfig *ipconfig)
941 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
944 unsigned short __connman_ipconfig_get_type_from_index(int index)
946 struct connman_ipdevice *ipdevice;
948 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
949 if (ipdevice == NULL)
952 return ipdevice->type;
955 unsigned int __connman_ipconfig_get_flags_from_index(int index)
957 struct connman_ipdevice *ipdevice;
959 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
960 if (ipdevice == NULL)
963 return ipdevice->flags;
966 const char *__connman_ipconfig_get_gateway_from_index(int index)
968 struct connman_ipdevice *ipdevice;
970 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
971 if (ipdevice == NULL)
974 if (ipdevice->ipv4_gateway != NULL)
975 return ipdevice->ipv4_gateway;
977 if (ipdevice->config_ipv4 != NULL &&
978 ipdevice->config_ipv4->address != NULL)
979 return ipdevice->config_ipv4->address->gateway;
981 if (ipdevice->ipv6_gateway != NULL)
982 return ipdevice->ipv6_gateway;
984 if (ipdevice->config_ipv6 != NULL &&
985 ipdevice->config_ipv6->address != NULL)
986 return ipdevice->config_ipv6->address->gateway;
991 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
993 ipconfig->index = index;
996 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
998 if (ipconfig->address == NULL)
1001 return ipconfig->address->local;
1004 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1006 if (ipconfig->address == NULL)
1009 g_free(ipconfig->address->local);
1010 ipconfig->address->local = g_strdup(address);
1013 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1015 if (ipconfig->address == NULL)
1018 return ipconfig->address->peer;
1021 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1023 if (ipconfig->address == NULL)
1026 g_free(ipconfig->address->peer);
1027 ipconfig->address->peer = g_strdup(address);
1030 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1032 if (ipconfig->address == NULL)
1035 return ipconfig->address->broadcast;
1038 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1040 if (ipconfig->address == NULL)
1043 g_free(ipconfig->address->broadcast);
1044 ipconfig->address->broadcast = g_strdup(broadcast);
1047 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1049 if (ipconfig->address == NULL)
1052 return ipconfig->address->gateway;
1055 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1057 struct connman_service *service;
1059 if (ipconfig->address == NULL)
1062 service = __connman_service_lookup_from_index(ipconfig->index);
1063 if (service != NULL)
1064 __connman_connection_gateway_remove(service);
1066 g_free(ipconfig->address->gateway);
1067 ipconfig->address->gateway = g_strdup(gateway);
1069 if (service != NULL && ipconfig->address->gateway != NULL) {
1070 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1071 __connman_connection_gateway_add(service,
1073 ipconfig->address->gateway,
1074 ipconfig->address->peer);
1075 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1076 __connman_connection_gateway_add(service,
1077 ipconfig->address->gateway,
1079 ipconfig->address->peer);
1084 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1086 if (ipconfig->address == NULL)
1089 return ipconfig->address->prefixlen;
1092 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1094 if (ipconfig->address == NULL)
1097 ipconfig->address->prefixlen = prefixlen;
1100 static struct connman_ipconfig *create_ipv6config(int index)
1102 struct connman_ipconfig *ipv6config;
1104 DBG("index %d", index);
1106 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1107 if (ipv6config == NULL)
1110 ipv6config->refcount = 1;
1112 ipv6config->index = index;
1113 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1114 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1115 ipv6config->ipv6_privacy_config = 0;
1117 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1118 if (ipv6config->address == NULL) {
1123 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1125 DBG("ipconfig %p", ipv6config);
1131 * connman_ipconfig_create:
1133 * Allocate a new ipconfig structure.
1135 * Returns: a newly-allocated #connman_ipconfig structure
1137 struct connman_ipconfig *connman_ipconfig_create(int index,
1138 enum connman_ipconfig_type type)
1140 struct connman_ipconfig *ipconfig;
1142 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1143 return create_ipv6config(index);
1145 DBG("index %d", index);
1147 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1148 if (ipconfig == NULL)
1151 ipconfig->refcount = 1;
1153 ipconfig->index = index;
1154 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1156 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1157 if (ipconfig->address == NULL) {
1162 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1164 DBG("ipconfig %p", ipconfig);
1171 * connman_ipconfig_ref:
1172 * @ipconfig: ipconfig structure
1174 * Increase reference counter of ipconfig
1176 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1178 DBG("ipconfig %p refcount %d", ipconfig,
1179 g_atomic_int_get(&ipconfig->refcount) + 1);
1181 g_atomic_int_inc(&ipconfig->refcount);
1187 * connman_ipconfig_unref:
1188 * @ipconfig: ipconfig structure
1190 * Decrease reference counter of ipconfig
1192 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1194 if (ipconfig == NULL)
1197 DBG("ipconfig %p refcount %d", ipconfig,
1198 g_atomic_int_get(&ipconfig->refcount) - 1);
1200 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1201 __connman_ipconfig_disable(ipconfig);
1203 connman_ipconfig_set_ops(ipconfig, NULL);
1205 if (ipconfig->origin != NULL) {
1206 connman_ipconfig_unref(ipconfig->origin);
1207 ipconfig->origin = NULL;
1210 connman_ipaddress_free(ipconfig->system);
1211 connman_ipaddress_free(ipconfig->address);
1217 * connman_ipconfig_get_data:
1218 * @ipconfig: ipconfig structure
1220 * Get private data pointer
1222 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1224 if (ipconfig == NULL)
1227 return ipconfig->ops_data;
1231 * connman_ipconfig_set_data:
1232 * @ipconfig: ipconfig structure
1233 * @data: data pointer
1235 * Set private data pointer
1237 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1239 ipconfig->ops_data = data;
1243 * connman_ipconfig_get_index:
1244 * @ipconfig: ipconfig structure
1246 * Get interface index
1248 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1250 if (ipconfig == NULL)
1253 if (ipconfig->origin != NULL)
1254 return ipconfig->origin->index;
1256 return ipconfig->index;
1260 * connman_ipconfig_get_ifname:
1261 * @ipconfig: ipconfig structure
1263 * Get interface name
1265 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1267 struct connman_ipdevice *ipdevice;
1269 if (ipconfig == NULL)
1272 if (ipconfig->index < 0)
1275 ipdevice = g_hash_table_lookup(ipdevice_hash,
1276 GINT_TO_POINTER(ipconfig->index));
1277 if (ipdevice == NULL)
1280 return ipdevice->ifname;
1284 * connman_ipconfig_set_ops:
1285 * @ipconfig: ipconfig structure
1286 * @ops: operation callbacks
1288 * Set the operation callbacks
1290 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1291 const struct connman_ipconfig_ops *ops)
1293 ipconfig->ops = ops;
1297 * connman_ipconfig_set_method:
1298 * @ipconfig: ipconfig structure
1299 * @method: configuration method
1301 * Set the configuration method
1303 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1304 enum connman_ipconfig_method method)
1306 ipconfig->method = method;
1311 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1313 if (ipconfig == NULL)
1314 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1316 return ipconfig->method;
1320 * connman_ipconfig_bind:
1321 * @ipconfig: ipconfig structure
1322 * @ipaddress: ipaddress structure
1324 * Bind IP address details to configuration
1326 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1327 struct connman_ipaddress *ipaddress)
1329 struct connman_ipconfig *origin;
1331 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1333 connman_ipaddress_copy(origin->address, ipaddress);
1335 connman_inet_set_address(origin->index, origin->address);
1338 void __connman_ipconfig_set_element_ipv6_gateway(
1339 struct connman_ipconfig *ipconfig,
1340 struct connman_element *element)
1342 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1343 element->ipv6.gateway = ipconfig->address->gateway;
1346 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1350 switch (ipconfig->method) {
1351 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1352 case CONNMAN_IPCONFIG_METHOD_OFF:
1353 case CONNMAN_IPCONFIG_METHOD_FIXED:
1354 case CONNMAN_IPCONFIG_METHOD_DHCP:
1355 case CONNMAN_IPCONFIG_METHOD_AUTO:
1357 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1358 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1359 return connman_inet_set_address(ipconfig->index,
1361 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1362 return connman_inet_set_ipv6_address(
1363 ipconfig->index, ipconfig->address);
1369 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1373 if (ipconfig == NULL)
1376 DBG("method %d", ipconfig->method);
1378 switch (ipconfig->method) {
1379 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1380 case CONNMAN_IPCONFIG_METHOD_OFF:
1381 case CONNMAN_IPCONFIG_METHOD_FIXED:
1382 case CONNMAN_IPCONFIG_METHOD_DHCP:
1383 case CONNMAN_IPCONFIG_METHOD_AUTO:
1385 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1386 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1387 return connman_inet_clear_address(ipconfig->index,
1389 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1390 return connman_inet_clear_ipv6_address(
1392 ipconfig->address->local,
1393 ipconfig->address->prefixlen);
1399 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1402 struct connman_ipdevice *ipdevice;
1404 DBG("ipconfig %p", ipconfig);
1406 if (ipconfig == NULL || ipconfig->index < 0)
1409 ipdevice = g_hash_table_lookup(ipdevice_hash,
1410 GINT_TO_POINTER(ipconfig->index));
1411 if (ipdevice == NULL)
1414 g_free(ipdevice->pac);
1415 ipdevice->pac = g_strdup(url);
1420 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1422 struct connman_ipdevice *ipdevice;
1424 DBG("ipconfig %p", ipconfig);
1426 if (ipconfig == NULL || ipconfig->index < 0)
1429 ipdevice = g_hash_table_lookup(ipdevice_hash,
1430 GINT_TO_POINTER(ipconfig->index));
1431 if (ipdevice == NULL)
1434 return ipdevice->pac;
1437 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1439 struct connman_ipdevice *ipdevice;
1443 ipdevice = g_hash_table_lookup(ipdevice_hash,
1444 GINT_TO_POINTER(ipconfig->index));
1445 if (ipdevice == NULL)
1448 set_ipv6_state(ipdevice->ifname, FALSE);
1451 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1453 struct connman_ipdevice *ipdevice;
1457 ipdevice = g_hash_table_lookup(ipdevice_hash,
1458 GINT_TO_POINTER(ipconfig->index));
1459 if (ipdevice == NULL)
1462 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1463 set_ipv6_privacy(ipdevice->ifname,
1464 ipconfig->ipv6_privacy_config);
1466 set_ipv6_state(ipdevice->ifname, TRUE);
1469 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1471 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1474 disable_ipv6(ipconfig);
1477 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1479 struct connman_ipdevice *ipdevice;
1480 gboolean up = FALSE, down = FALSE;
1481 gboolean lower_up = FALSE, lower_down = FALSE;
1482 enum connman_ipconfig_type type;
1484 DBG("ipconfig %p", ipconfig);
1486 if (ipconfig == NULL || ipconfig->index < 0)
1489 ipdevice = g_hash_table_lookup(ipdevice_hash,
1490 GINT_TO_POINTER(ipconfig->index));
1491 if (ipdevice == NULL)
1494 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1495 if (ipdevice->config_ipv4 == ipconfig)
1497 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1498 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1499 if (ipdevice->config_ipv6 == ipconfig)
1501 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1502 enable_ipv6(ipconfig);
1506 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1507 ipdevice->config_ipv4 != NULL) {
1508 ipconfig_list = g_list_remove(ipconfig_list,
1509 ipdevice->config_ipv4);
1511 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1513 connman_ipconfig_unref(ipdevice->config_ipv4);
1516 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1517 ipdevice->config_ipv6 != NULL) {
1518 ipconfig_list = g_list_remove(ipconfig_list,
1519 ipdevice->config_ipv6);
1521 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1523 connman_ipconfig_unref(ipdevice->config_ipv6);
1526 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1527 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1528 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1529 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1531 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1533 if (ipdevice->flags & IFF_UP)
1538 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1539 (IFF_RUNNING | IFF_LOWER_UP))
1541 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1544 if (up == TRUE && ipconfig->ops->up)
1545 ipconfig->ops->up(ipconfig);
1546 if (lower_up == TRUE && ipconfig->ops->lower_up)
1547 ipconfig->ops->lower_up(ipconfig);
1549 if (lower_down == TRUE && ipconfig->ops->lower_down)
1550 ipconfig->ops->lower_down(ipconfig);
1551 if (down == TRUE && ipconfig->ops->down)
1552 ipconfig->ops->down(ipconfig);
1557 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1559 struct connman_ipdevice *ipdevice;
1561 DBG("ipconfig %p", ipconfig);
1563 if (ipconfig == NULL || ipconfig->index < 0)
1566 ipdevice = g_hash_table_lookup(ipdevice_hash,
1567 GINT_TO_POINTER(ipconfig->index));
1568 if (ipdevice == NULL)
1571 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1574 if (ipdevice->config_ipv4 == ipconfig) {
1575 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1577 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1578 connman_ipconfig_unref(ipdevice->config_ipv4);
1579 ipdevice->config_ipv4 = NULL;
1583 if (ipdevice->config_ipv6 == ipconfig) {
1584 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1586 if (ipdevice->config_ipv6->method ==
1587 CONNMAN_IPCONFIG_METHOD_AUTO)
1588 disable_ipv6(ipdevice->config_ipv6);
1590 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1591 connman_ipconfig_unref(ipdevice->config_ipv6);
1592 ipdevice->config_ipv6 = NULL;
1599 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1602 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1604 case CONNMAN_IPCONFIG_METHOD_OFF:
1606 case CONNMAN_IPCONFIG_METHOD_FIXED:
1608 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1610 case CONNMAN_IPCONFIG_METHOD_DHCP:
1612 case CONNMAN_IPCONFIG_METHOD_AUTO:
1619 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1621 if (g_strcmp0(method, "off") == 0)
1622 return CONNMAN_IPCONFIG_METHOD_OFF;
1623 else if (g_strcmp0(method, "fixed") == 0)
1624 return CONNMAN_IPCONFIG_METHOD_FIXED;
1625 else if (g_strcmp0(method, "manual") == 0)
1626 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1627 else if (g_strcmp0(method, "dhcp") == 0)
1628 return CONNMAN_IPCONFIG_METHOD_DHCP;
1629 else if (g_strcmp0(method, "auto") == 0)
1630 return CONNMAN_IPCONFIG_METHOD_AUTO;
1632 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1635 static const char *privacy2string(int privacy)
1639 else if (privacy == 1)
1641 else if (privacy > 1)
1647 static int string2privacy(const char *privacy)
1649 if (g_strcmp0(privacy, "disabled") == 0)
1651 else if (g_strcmp0(privacy, "enabled") == 0)
1653 else if (g_strcmp0(privacy, "prefered") == 0)
1659 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1660 DBusMessageIter *iter)
1666 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1669 str = __connman_ipconfig_method2string(ipconfig->method);
1673 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1675 if (ipconfig->system == NULL)
1678 if (ipconfig->system->local != NULL) {
1680 struct in_addr netmask;
1683 connman_dbus_dict_append_basic(iter, "Address",
1684 DBUS_TYPE_STRING, &ipconfig->system->local);
1686 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1687 netmask.s_addr = htonl(addr);
1688 mask = inet_ntoa(netmask);
1689 connman_dbus_dict_append_basic(iter, "Netmask",
1690 DBUS_TYPE_STRING, &mask);
1693 if (ipconfig->system->gateway != NULL)
1694 connman_dbus_dict_append_basic(iter, "Gateway",
1695 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1698 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1699 DBusMessageIter *iter)
1701 const char *str, *privacy;
1705 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1708 str = __connman_ipconfig_method2string(ipconfig->method);
1712 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1714 if (ipconfig->system == NULL)
1717 if (ipconfig->system->local != NULL) {
1718 connman_dbus_dict_append_basic(iter, "Address",
1719 DBUS_TYPE_STRING, &ipconfig->system->local);
1720 connman_dbus_dict_append_basic(iter, "PrefixLength",
1722 &ipconfig->system->prefixlen);
1725 if (ipconfig->system->gateway != NULL)
1726 connman_dbus_dict_append_basic(iter, "Gateway",
1727 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1729 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1730 connman_dbus_dict_append_basic(iter, "Privacy",
1731 DBUS_TYPE_STRING, &privacy);
1734 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1735 DBusMessageIter *iter)
1737 const char *str, *privacy;
1741 str = __connman_ipconfig_method2string(ipconfig->method);
1745 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1747 switch (ipconfig->method) {
1748 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1749 case CONNMAN_IPCONFIG_METHOD_OFF:
1750 case CONNMAN_IPCONFIG_METHOD_DHCP:
1752 case CONNMAN_IPCONFIG_METHOD_FIXED:
1753 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1754 case CONNMAN_IPCONFIG_METHOD_AUTO:
1758 if (ipconfig->address == NULL)
1761 if (ipconfig->address->local != NULL) {
1762 connman_dbus_dict_append_basic(iter, "Address",
1763 DBUS_TYPE_STRING, &ipconfig->address->local);
1764 connman_dbus_dict_append_basic(iter, "PrefixLength",
1766 &ipconfig->address->prefixlen);
1769 if (ipconfig->address->gateway != NULL)
1770 connman_dbus_dict_append_basic(iter, "Gateway",
1771 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1773 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1774 connman_dbus_dict_append_basic(iter, "Privacy",
1775 DBUS_TYPE_STRING, &privacy);
1778 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1779 DBusMessageIter *iter)
1785 str = __connman_ipconfig_method2string(ipconfig->method);
1789 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1791 switch (ipconfig->method) {
1792 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1793 case CONNMAN_IPCONFIG_METHOD_OFF:
1794 case CONNMAN_IPCONFIG_METHOD_FIXED:
1795 case CONNMAN_IPCONFIG_METHOD_DHCP:
1796 case CONNMAN_IPCONFIG_METHOD_AUTO:
1798 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1802 if (ipconfig->address == NULL)
1805 if (ipconfig->address->local != NULL) {
1807 struct in_addr netmask;
1810 connman_dbus_dict_append_basic(iter, "Address",
1811 DBUS_TYPE_STRING, &ipconfig->address->local);
1813 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1814 netmask.s_addr = htonl(addr);
1815 mask = inet_ntoa(netmask);
1816 connman_dbus_dict_append_basic(iter, "Netmask",
1817 DBUS_TYPE_STRING, &mask);
1820 if (ipconfig->address->gateway != NULL)
1821 connman_dbus_dict_append_basic(iter, "Gateway",
1822 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1825 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1826 DBusMessageIter *array)
1828 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1829 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1830 *prefix_length_string = NULL, *privacy_string = NULL;
1831 int prefix_length = 0, privacy = 0;
1832 DBusMessageIter dict;
1834 DBG("ipconfig %p", ipconfig);
1836 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1839 dbus_message_iter_recurse(array, &dict);
1841 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1842 DBusMessageIter entry;
1846 dbus_message_iter_recurse(&dict, &entry);
1848 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1851 dbus_message_iter_get_basic(&entry, &key);
1852 dbus_message_iter_next(&entry);
1854 type = dbus_message_iter_get_arg_type(&entry);
1856 if (g_str_equal(key, "Method") == TRUE) {
1859 if (type != DBUS_TYPE_STRING)
1862 dbus_message_iter_get_basic(&entry, &str);
1863 method = __connman_ipconfig_string2method(str);
1864 } else if (g_str_equal(key, "Address") == TRUE) {
1865 if (type != DBUS_TYPE_STRING)
1868 dbus_message_iter_get_basic(&entry, &address);
1869 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1870 if (type != DBUS_TYPE_STRING)
1873 dbus_message_iter_get_basic(&entry,
1874 &prefix_length_string);
1876 prefix_length = atoi(prefix_length_string);
1877 if (prefix_length < 0 || prefix_length > 128)
1880 } else if (g_str_equal(key, "Netmask") == TRUE) {
1881 if (type != DBUS_TYPE_STRING)
1884 dbus_message_iter_get_basic(&entry, &netmask);
1885 } else if (g_str_equal(key, "Gateway") == TRUE) {
1886 if (type != DBUS_TYPE_STRING)
1889 dbus_message_iter_get_basic(&entry, &gateway);
1890 } else if (g_str_equal(key, "Privacy") == TRUE) {
1891 if (type != DBUS_TYPE_STRING)
1894 dbus_message_iter_get_basic(&entry, &privacy_string);
1895 privacy = string2privacy(privacy_string);
1897 dbus_message_iter_next(&dict);
1900 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1902 method, address, netmask, gateway, prefix_length,
1906 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1907 case CONNMAN_IPCONFIG_METHOD_FIXED:
1910 case CONNMAN_IPCONFIG_METHOD_OFF:
1911 ipconfig->method = method;
1912 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1913 disable_ipv6(ipconfig);
1916 case CONNMAN_IPCONFIG_METHOD_AUTO:
1917 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1920 ipconfig->method = method;
1921 if (privacy_string != NULL)
1922 ipconfig->ipv6_privacy_config = privacy;
1923 enable_ipv6(ipconfig);
1926 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1927 if (address == NULL)
1930 ipconfig->method = method;
1932 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1933 connman_ipaddress_set_ipv4(ipconfig->address,
1934 address, netmask, gateway);
1936 return connman_ipaddress_set_ipv6(
1937 ipconfig->address, address,
1938 gateway, prefix_length);
1941 case CONNMAN_IPCONFIG_METHOD_DHCP:
1942 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1945 ipconfig->method = method;
1952 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1953 DBusMessageIter *iter)
1955 struct connman_ipdevice *ipdevice;
1956 const char *method = "auto";
1958 connman_dbus_dict_append_basic(iter, "Method",
1959 DBUS_TYPE_STRING, &method);
1961 ipdevice = g_hash_table_lookup(ipdevice_hash,
1962 GINT_TO_POINTER(ipconfig->index));
1963 if (ipdevice == NULL)
1966 if (ipdevice->ifname != NULL)
1967 connman_dbus_dict_append_basic(iter, "Interface",
1968 DBUS_TYPE_STRING, &ipdevice->ifname);
1970 if (ipdevice->address != NULL)
1971 connman_dbus_dict_append_basic(iter, "Address",
1972 DBUS_TYPE_STRING, &ipdevice->address);
1974 if (ipdevice->mtu > 0)
1975 connman_dbus_dict_append_basic(iter, "MTU",
1976 DBUS_TYPE_UINT16, &ipdevice->mtu);
1979 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1980 GKeyFile *keyfile, const char *identifier, const char *prefix)
1985 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1987 key = g_strdup_printf("%smethod", prefix);
1988 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1989 if (method == NULL) {
1990 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1991 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1993 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1995 ipconfig->method = __connman_ipconfig_string2method(method);
1997 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1998 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2000 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2001 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2002 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2004 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2005 privacy = g_key_file_get_string(keyfile, identifier,
2007 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2011 __connman_ipconfig_enable(ipconfig);
2012 enable_ipv6(ipconfig);
2019 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2020 ipconfig->address->prefixlen = g_key_file_get_integer(
2021 keyfile, identifier, key, NULL);
2024 key = g_strdup_printf("%slocal_address", prefix);
2025 ipconfig->address->local = g_key_file_get_string(
2026 keyfile, identifier, key, NULL);
2029 key = g_strdup_printf("%speer_address", prefix);
2030 ipconfig->address->peer = g_key_file_get_string(
2031 keyfile, identifier, key, NULL);
2034 key = g_strdup_printf("%sbroadcast_address", prefix);
2035 ipconfig->address->broadcast = g_key_file_get_string(
2036 keyfile, identifier, key, NULL);
2039 key = g_strdup_printf("%sgateway", prefix);
2040 ipconfig->address->gateway = g_key_file_get_string(
2041 keyfile, identifier, key, NULL);
2047 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2048 GKeyFile *keyfile, const char *identifier, const char *prefix)
2053 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2055 method = __connman_ipconfig_method2string(ipconfig->method);
2057 key = g_strdup_printf("%smethod", prefix);
2058 g_key_file_set_string(keyfile, identifier, key, method);
2061 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2062 const char *privacy;
2063 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2064 key = g_strdup_printf("%sprivacy", prefix);
2065 g_key_file_set_string(keyfile, identifier, key, privacy);
2069 switch (ipconfig->method) {
2070 case CONNMAN_IPCONFIG_METHOD_FIXED:
2071 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2073 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2074 case CONNMAN_IPCONFIG_METHOD_OFF:
2075 case CONNMAN_IPCONFIG_METHOD_DHCP:
2076 case CONNMAN_IPCONFIG_METHOD_AUTO:
2080 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2081 g_key_file_set_integer(keyfile, identifier,
2082 key, ipconfig->address->prefixlen);
2085 key = g_strdup_printf("%slocal_address", prefix);
2086 if (ipconfig->address->local != NULL)
2087 g_key_file_set_string(keyfile, identifier,
2088 key, ipconfig->address->local);
2091 key = g_strdup_printf("%speer_address", prefix);
2092 if (ipconfig->address->peer != NULL)
2093 g_key_file_set_string(keyfile, identifier,
2094 key, ipconfig->address->peer);
2097 key = g_strdup_printf("%sbroadcast_address", prefix);
2098 if (ipconfig->address->broadcast != NULL)
2099 g_key_file_set_string(keyfile, identifier,
2100 key, ipconfig->address->broadcast);
2103 key = g_strdup_printf("%sgateway", prefix);
2104 if (ipconfig->address->gateway != NULL)
2105 g_key_file_set_string(keyfile, identifier,
2106 key, ipconfig->address->gateway);
2112 int __connman_ipconfig_init(void)
2116 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2117 NULL, free_ipdevice);
2122 void __connman_ipconfig_cleanup(void)
2126 g_hash_table_destroy(ipdevice_hash);
2127 ipdevice_hash = NULL;