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)];
151 err = inet_pton(AF_INET6, address, buf);
158 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
160 unsigned char prefix_length,
163 if (ipaddress == NULL)
166 if (check_ipv6_address(address) == FALSE)
169 if (check_ipv6_address(gateway) == FALSE)
172 DBG("prefix_len %d address %s gateway %s",
173 prefix_length, address, gateway);
175 ipaddress->family = AF_INET6;
177 ipaddress->prefixlen = prefix_length;
179 g_free(ipaddress->local);
180 ipaddress->local = g_strdup(address);
182 g_free(ipaddress->gateway);
183 ipaddress->gateway = g_strdup(gateway);
188 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
189 const char *address, const char *netmask, const char *gateway)
191 if (ipaddress == NULL)
194 ipaddress->family = AF_INET;
196 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
198 g_free(ipaddress->local);
199 ipaddress->local = g_strdup(address);
201 g_free(ipaddress->gateway);
202 ipaddress->gateway = g_strdup(gateway);
207 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
210 if (ipaddress == NULL)
213 g_free(ipaddress->peer);
214 ipaddress->peer = g_strdup(peer);
217 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
219 if (ipaddress == NULL)
222 ipaddress->prefixlen = 0;
224 g_free(ipaddress->local);
225 ipaddress->local = NULL;
227 g_free(ipaddress->peer);
228 ipaddress->peer = NULL;
230 g_free(ipaddress->broadcast);
231 ipaddress->broadcast = NULL;
233 g_free(ipaddress->gateway);
234 ipaddress->gateway = NULL;
237 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
238 struct connman_ipaddress *source)
240 if (ipaddress == NULL || source == NULL)
243 ipaddress->family = source->family;
244 ipaddress->prefixlen = source->prefixlen;
246 g_free(ipaddress->local);
247 ipaddress->local = g_strdup(source->local);
249 g_free(ipaddress->peer);
250 ipaddress->peer = g_strdup(source->peer);
252 g_free(ipaddress->broadcast);
253 ipaddress->broadcast = g_strdup(source->broadcast);
255 g_free(ipaddress->gateway);
256 ipaddress->gateway = g_strdup(source->gateway);
259 static void free_address_list(struct connman_ipdevice *ipdevice)
263 for (list = ipdevice->address_list; list; list = list->next) {
264 struct connman_ipaddress *ipaddress = list->data;
266 connman_ipaddress_free(ipaddress);
270 g_slist_free(ipdevice->address_list);
271 ipdevice->address_list = NULL;
274 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
275 unsigned char prefixlen, const char *local)
279 for (list = ipdevice->address_list; list; list = list->next) {
280 struct connman_ipaddress *ipaddress = list->data;
282 if (g_strcmp0(ipaddress->local, local) == 0 &&
283 ipaddress->prefixlen == prefixlen)
290 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
293 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
295 case CONNMAN_IPCONFIG_TYPE_IPV4:
297 case CONNMAN_IPCONFIG_TYPE_IPV6:
304 static const char *type2str(unsigned short type)
309 case ARPHRD_LOOPBACK:
322 static const char *scope2str(unsigned char scope)
334 static gboolean get_ipv6_state(gchar *ifname)
339 gboolean enabled = FALSE;
342 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
344 path = g_strdup_printf(
345 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
350 f = fopen(path, "r");
355 if (fscanf(f, "%d", &disabled) > 0)
363 static void set_ipv6_state(gchar *ifname, gboolean enable)
369 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
371 path = g_strdup_printf(
372 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
377 f = fopen(path, "r+");
392 static int get_ipv6_privacy(gchar *ifname)
401 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
407 f = fopen(path, "r");
414 if (fscanf(f, "%d", &value) <= 0)
422 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
423 * The privacy extension is described in RFC 3041 and RFC 4941
425 static void set_ipv6_privacy(gchar *ifname, int value)
433 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
442 f = fopen(path, "r+");
449 fprintf(f, "%d", value);
453 static void free_ipdevice(gpointer data)
455 struct connman_ipdevice *ipdevice = data;
457 connman_info("%s {remove} index %d", ipdevice->ifname,
460 if (ipdevice->config_ipv4 != NULL) {
461 connman_ipconfig_unref(ipdevice->config_ipv4);
462 ipdevice->config_ipv4 = NULL;
465 if (ipdevice->config_ipv6 != NULL) {
466 connman_ipconfig_unref(ipdevice->config_ipv6);
467 ipdevice->config_ipv6 = NULL;
470 free_address_list(ipdevice);
471 g_free(ipdevice->ipv4_gateway);
472 g_free(ipdevice->ipv6_gateway);
473 g_free(ipdevice->pac);
475 g_free(ipdevice->address);
477 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
478 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
480 g_free(ipdevice->ifname);
484 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
486 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
487 ipdevice->config_ipv6);
490 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
492 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
493 ipdevice->config_ipv6);
495 if (ipdevice->config_ipv4)
496 connman_inet_clear_address(ipdevice->index,
497 ipdevice->config_ipv4->address);
499 if (ipdevice->config_ipv6)
500 connman_inet_clear_ipv6_address(ipdevice->index,
501 ipdevice->config_ipv6->address->local,
502 ipdevice->config_ipv6->address->prefixlen);
505 static void update_stats(struct connman_ipdevice *ipdevice,
506 struct rtnl_link_stats *stats)
508 struct connman_service *service;
510 if (stats->rx_packets == 0 && stats->tx_packets == 0)
513 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
514 stats->rx_packets, stats->rx_bytes);
515 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
516 stats->tx_packets, stats->tx_bytes);
518 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
521 if (ipdevice->config_ipv4)
522 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
523 else if (ipdevice->config_ipv6)
524 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
531 ipdevice->rx_packets = stats->rx_packets;
532 ipdevice->tx_packets = stats->tx_packets;
533 ipdevice->rx_bytes = stats->rx_bytes;
534 ipdevice->tx_bytes = stats->tx_bytes;
535 ipdevice->rx_errors = stats->rx_errors;
536 ipdevice->tx_errors = stats->tx_errors;
537 ipdevice->rx_dropped = stats->rx_dropped;
538 ipdevice->tx_dropped = stats->tx_dropped;
540 __connman_service_notify(service,
541 ipdevice->rx_packets, ipdevice->tx_packets,
542 ipdevice->rx_bytes, ipdevice->tx_bytes,
543 ipdevice->rx_errors, ipdevice->tx_errors,
544 ipdevice->rx_dropped, ipdevice->tx_dropped);
547 void __connman_ipconfig_newlink(int index, unsigned short type,
548 unsigned int flags, const char *address,
550 struct rtnl_link_stats *stats)
552 struct connman_ipdevice *ipdevice;
555 gboolean up = FALSE, down = FALSE;
556 gboolean lower_up = FALSE, lower_down = FALSE;
558 DBG("index %d", index);
560 if (type == ARPHRD_LOOPBACK)
563 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
564 if (ipdevice != NULL)
567 ipdevice = g_try_new0(struct connman_ipdevice, 1);
568 if (ipdevice == NULL)
571 ipdevice->index = index;
572 ipdevice->ifname = connman_inet_ifname(index);
573 ipdevice->type = type;
575 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
576 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
578 ipdevice->address = g_strdup(address);
580 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
582 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
583 index, type, type2str(type));
588 update_stats(ipdevice, stats);
590 if (flags == ipdevice->flags)
593 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
600 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
601 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
602 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
603 (IFF_RUNNING | IFF_LOWER_UP))
605 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
609 ipdevice->flags = flags;
611 str = g_string_new(NULL);
616 g_string_append(str, "UP");
618 g_string_append(str, "DOWN");
620 if (flags & IFF_RUNNING)
621 g_string_append(str, ",RUNNING");
623 if (flags & IFF_LOWER_UP)
624 g_string_append(str, ",LOWER_UP");
626 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
629 g_string_free(str, TRUE);
631 for (list = g_list_first(ipconfig_list); list;
632 list = g_list_next(list)) {
633 struct connman_ipconfig *ipconfig = list->data;
635 if (index != ipconfig->index)
638 if (ipconfig->ops == NULL)
641 if (up == TRUE && ipconfig->ops->up)
642 ipconfig->ops->up(ipconfig);
643 if (lower_up == TRUE && ipconfig->ops->lower_up)
644 ipconfig->ops->lower_up(ipconfig);
646 if (lower_down == TRUE && ipconfig->ops->lower_down)
647 ipconfig->ops->lower_down(ipconfig);
648 if (down == TRUE && ipconfig->ops->down)
649 ipconfig->ops->down(ipconfig);
653 __connman_ipconfig_lower_up(ipdevice);
655 __connman_ipconfig_lower_down(ipdevice);
658 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
660 struct connman_ipdevice *ipdevice;
663 DBG("index %d", index);
665 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
666 if (ipdevice == NULL)
669 update_stats(ipdevice, stats);
671 for (list = g_list_first(ipconfig_list); list;
672 list = g_list_next(list)) {
673 struct connman_ipconfig *ipconfig = list->data;
675 if (index != ipconfig->index)
678 ipconfig->index = -1;
680 if (ipconfig->ops == NULL)
683 if (ipconfig->ops->lower_down)
684 ipconfig->ops->lower_down(ipconfig);
685 if (ipconfig->ops->down)
686 ipconfig->ops->down(ipconfig);
689 __connman_ipconfig_lower_down(ipdevice);
691 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
694 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
696 const struct connman_ipaddress *addr1 = a;
697 const struct connman_ipaddress *addr2 = b;
699 if (addr1->prefixlen != addr2->prefixlen)
700 return addr2->prefixlen - addr1->prefixlen;
702 return g_strcmp0(addr1->local, addr2->local);
705 void __connman_ipconfig_newaddr(int index, int family, const char *label,
706 unsigned char prefixlen, const char *address)
708 struct connman_ipdevice *ipdevice;
709 struct connman_ipaddress *ipaddress;
712 DBG("index %d", index);
714 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
715 if (ipdevice == NULL)
718 ipaddress = connman_ipaddress_alloc(family);
719 if (ipaddress == NULL)
722 ipaddress->prefixlen = prefixlen;
723 ipaddress->local = g_strdup(address);
725 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
726 check_duplicate_address)) {
727 connman_ipaddress_free(ipaddress);
731 ipdevice->address_list = g_slist_append(ipdevice->address_list,
734 connman_info("%s {add} address %s/%u label %s family %d",
735 ipdevice->ifname, address, prefixlen, label, family);
737 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
738 connman_ipaddress_copy(ipdevice->config_ipv4->system,
741 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
742 connman_ipaddress_copy(ipdevice->config_ipv6->system,
747 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
750 for (list = g_list_first(ipconfig_list); list;
751 list = g_list_next(list)) {
752 struct connman_ipconfig *ipconfig = list->data;
754 if (index != ipconfig->index)
757 if (ipconfig->ops == NULL)
760 if (ipconfig->ops->ip_bound)
761 ipconfig->ops->ip_bound(ipconfig);
765 void __connman_ipconfig_deladdr(int index, int family, const char *label,
766 unsigned char prefixlen, const char *address)
768 struct connman_ipdevice *ipdevice;
769 struct connman_ipaddress *ipaddress;
772 DBG("index %d", index);
774 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
775 if (ipdevice == NULL)
778 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
779 if (ipaddress == NULL)
782 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
785 connman_ipaddress_clear(ipaddress);
788 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
789 address, prefixlen, label);
791 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
794 if (g_slist_length(ipdevice->address_list) > 0)
797 for (list = g_list_first(ipconfig_list); list;
798 list = g_list_next(list)) {
799 struct connman_ipconfig *ipconfig = list->data;
801 if (index != ipconfig->index)
804 if (ipconfig->ops == NULL)
807 if (ipconfig->ops->ip_release)
808 ipconfig->ops->ip_release(ipconfig);
812 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
813 const char *dst, const char *gateway)
815 struct connman_ipdevice *ipdevice;
817 DBG("index %d", index);
819 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
820 if (ipdevice == NULL)
823 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
824 g_strcmp0(dst, "::") == 0)) {
828 if (family == AF_INET6) {
829 g_free(ipdevice->ipv6_gateway);
830 ipdevice->ipv6_gateway = g_strdup(gateway);
832 if (ipdevice->config_ipv6 != NULL &&
833 ipdevice->config_ipv6->system != NULL) {
834 g_free(ipdevice->config_ipv6->system->gateway);
835 ipdevice->config_ipv6->system->gateway =
838 } else if (family == AF_INET) {
839 g_free(ipdevice->ipv4_gateway);
840 ipdevice->ipv4_gateway = g_strdup(gateway);
842 if (ipdevice->config_ipv4 != NULL &&
843 ipdevice->config_ipv4->system != NULL) {
844 g_free(ipdevice->config_ipv4->system->gateway);
845 ipdevice->config_ipv4->system->gateway =
851 for (list = ipdevice->address_list; list; list = list->next) {
852 struct connman_ipaddress *ipaddress = list->data;
854 g_free(ipaddress->gateway);
855 ipaddress->gateway = g_strdup(gateway);
858 for (config_list = g_list_first(ipconfig_list); config_list;
859 config_list = g_list_next(config_list)) {
860 struct connman_ipconfig *ipconfig = config_list->data;
862 if (index != ipconfig->index)
865 if (ipconfig->ops == NULL)
868 if (ipconfig->ops->ip_bound)
869 ipconfig->ops->ip_bound(ipconfig);
873 connman_info("%s {add} route %s gw %s scope %u <%s>",
874 ipdevice->ifname, dst, gateway,
875 scope, scope2str(scope));
878 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
879 const char *dst, const char *gateway)
881 struct connman_ipdevice *ipdevice;
883 DBG("index %d", index);
885 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
886 if (ipdevice == NULL)
889 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
890 g_strcmp0(dst, "::") == 0)) {
894 if (family == AF_INET6) {
895 g_free(ipdevice->ipv6_gateway);
896 ipdevice->ipv6_gateway = NULL;
898 if (ipdevice->config_ipv6 != NULL &&
899 ipdevice->config_ipv6->system != NULL) {
900 g_free(ipdevice->config_ipv6->system->gateway);
901 ipdevice->config_ipv6->system->gateway = NULL;
903 } else if (family == AF_INET) {
904 g_free(ipdevice->ipv4_gateway);
905 ipdevice->ipv4_gateway = NULL;
907 if (ipdevice->config_ipv4 != NULL &&
908 ipdevice->config_ipv4->system != NULL) {
909 g_free(ipdevice->config_ipv4->system->gateway);
910 ipdevice->config_ipv4->system->gateway = NULL;
915 for (list = ipdevice->address_list; list; list = list->next) {
916 struct connman_ipaddress *ipaddress = list->data;
918 g_free(ipaddress->gateway);
919 ipaddress->gateway = NULL;
922 for (config_list = g_list_first(ipconfig_list); config_list;
923 config_list = g_list_next(config_list)) {
924 struct connman_ipconfig *ipconfig = config_list->data;
926 if (index != ipconfig->index)
929 if (ipconfig->ops == NULL)
932 if (ipconfig->ops->ip_release)
933 ipconfig->ops->ip_release(ipconfig);
937 connman_info("%s {del} route %s gw %s scope %u <%s>",
938 ipdevice->ifname, dst, gateway,
939 scope, scope2str(scope));
942 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
947 keys = g_hash_table_get_keys(ipdevice_hash);
951 for (list = g_list_first(keys); list; list = g_list_next(list)) {
952 int index = GPOINTER_TO_INT(list->data);
954 function(index, user_data);
960 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
961 struct connman_ipconfig *ipconfig)
963 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
966 unsigned short __connman_ipconfig_get_type_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 return ipdevice->type;
977 unsigned int __connman_ipconfig_get_flags_from_index(int index)
979 struct connman_ipdevice *ipdevice;
981 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
982 if (ipdevice == NULL)
985 return ipdevice->flags;
988 const char *__connman_ipconfig_get_gateway_from_index(int index)
990 struct connman_ipdevice *ipdevice;
992 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
993 if (ipdevice == NULL)
996 if (ipdevice->ipv4_gateway != NULL)
997 return ipdevice->ipv4_gateway;
999 if (ipdevice->config_ipv4 != NULL &&
1000 ipdevice->config_ipv4->address != NULL)
1001 return ipdevice->config_ipv4->address->gateway;
1003 if (ipdevice->ipv6_gateway != NULL)
1004 return ipdevice->ipv6_gateway;
1006 if (ipdevice->config_ipv6 != NULL &&
1007 ipdevice->config_ipv6->address != NULL)
1008 return ipdevice->config_ipv6->address->gateway;
1013 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1015 ipconfig->index = index;
1018 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1020 if (ipconfig->address == NULL)
1023 return ipconfig->address->local;
1026 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1028 if (ipconfig->address == NULL)
1031 g_free(ipconfig->address->local);
1032 ipconfig->address->local = g_strdup(address);
1035 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1037 if (ipconfig->address == NULL)
1040 return ipconfig->address->peer;
1043 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1045 if (ipconfig->address == NULL)
1048 g_free(ipconfig->address->peer);
1049 ipconfig->address->peer = g_strdup(address);
1052 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1054 if (ipconfig->address == NULL)
1057 return ipconfig->address->broadcast;
1060 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1062 if (ipconfig->address == NULL)
1065 g_free(ipconfig->address->broadcast);
1066 ipconfig->address->broadcast = g_strdup(broadcast);
1069 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1071 if (ipconfig->address == NULL)
1074 return ipconfig->address->gateway;
1077 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1081 if (ipconfig->address == NULL)
1083 g_free(ipconfig->address->gateway);
1084 ipconfig->address->gateway = g_strdup(gateway);
1087 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1089 struct connman_service *service;
1093 if (ipconfig->address == NULL)
1096 service = __connman_service_lookup_from_index(ipconfig->index);
1097 if (service == NULL)
1100 __connman_connection_gateway_remove(service);
1102 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1103 return __connman_connection_gateway_add(service, NULL,
1104 ipconfig->address->gateway,
1105 ipconfig->address->peer);
1106 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1107 return __connman_connection_gateway_add(service,
1108 ipconfig->address->gateway,
1109 NULL, ipconfig->address->peer);
1115 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1117 struct connman_service *service;
1121 service = __connman_service_lookup_from_index(ipconfig->index);
1122 if (service != NULL)
1123 __connman_connection_gateway_remove(service);
1126 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1128 if (ipconfig->address == NULL)
1131 return ipconfig->address->prefixlen;
1134 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1136 if (ipconfig->address == NULL)
1139 ipconfig->address->prefixlen = prefixlen;
1142 static struct connman_ipconfig *create_ipv6config(int index)
1144 struct connman_ipconfig *ipv6config;
1146 DBG("index %d", index);
1148 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1149 if (ipv6config == NULL)
1152 ipv6config->refcount = 1;
1154 ipv6config->index = index;
1155 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1156 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1157 ipv6config->ipv6_privacy_config = 0;
1159 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1160 if (ipv6config->address == NULL) {
1165 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1167 DBG("ipconfig %p", ipv6config);
1173 * connman_ipconfig_create:
1175 * Allocate a new ipconfig structure.
1177 * Returns: a newly-allocated #connman_ipconfig structure
1179 struct connman_ipconfig *connman_ipconfig_create(int index,
1180 enum connman_ipconfig_type type)
1182 struct connman_ipconfig *ipconfig;
1184 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1185 return create_ipv6config(index);
1187 DBG("index %d", index);
1189 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1190 if (ipconfig == NULL)
1193 ipconfig->refcount = 1;
1195 ipconfig->index = index;
1196 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1198 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1199 if (ipconfig->address == NULL) {
1204 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1206 DBG("ipconfig %p", ipconfig);
1213 * connman_ipconfig_ref:
1214 * @ipconfig: ipconfig structure
1216 * Increase reference counter of ipconfig
1218 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1220 DBG("ipconfig %p refcount %d", ipconfig,
1221 g_atomic_int_get(&ipconfig->refcount) + 1);
1223 g_atomic_int_inc(&ipconfig->refcount);
1229 * connman_ipconfig_unref:
1230 * @ipconfig: ipconfig structure
1232 * Decrease reference counter of ipconfig
1234 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1236 if (ipconfig == NULL)
1239 DBG("ipconfig %p refcount %d", ipconfig,
1240 g_atomic_int_get(&ipconfig->refcount) - 1);
1242 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1243 __connman_ipconfig_disable(ipconfig);
1245 connman_ipconfig_set_ops(ipconfig, NULL);
1247 if (ipconfig->origin != NULL) {
1248 connman_ipconfig_unref(ipconfig->origin);
1249 ipconfig->origin = NULL;
1252 connman_ipaddress_free(ipconfig->system);
1253 connman_ipaddress_free(ipconfig->address);
1259 * connman_ipconfig_get_data:
1260 * @ipconfig: ipconfig structure
1262 * Get private data pointer
1264 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1266 if (ipconfig == NULL)
1269 return ipconfig->ops_data;
1273 * connman_ipconfig_set_data:
1274 * @ipconfig: ipconfig structure
1275 * @data: data pointer
1277 * Set private data pointer
1279 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1281 ipconfig->ops_data = data;
1285 * connman_ipconfig_get_index:
1286 * @ipconfig: ipconfig structure
1288 * Get interface index
1290 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1292 if (ipconfig == NULL)
1295 if (ipconfig->origin != NULL)
1296 return ipconfig->origin->index;
1298 return ipconfig->index;
1302 * connman_ipconfig_get_ifname:
1303 * @ipconfig: ipconfig structure
1305 * Get interface name
1307 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1309 struct connman_ipdevice *ipdevice;
1311 if (ipconfig == NULL)
1314 if (ipconfig->index < 0)
1317 ipdevice = g_hash_table_lookup(ipdevice_hash,
1318 GINT_TO_POINTER(ipconfig->index));
1319 if (ipdevice == NULL)
1322 return ipdevice->ifname;
1326 * connman_ipconfig_set_ops:
1327 * @ipconfig: ipconfig structure
1328 * @ops: operation callbacks
1330 * Set the operation callbacks
1332 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1333 const struct connman_ipconfig_ops *ops)
1335 ipconfig->ops = ops;
1339 * connman_ipconfig_set_method:
1340 * @ipconfig: ipconfig structure
1341 * @method: configuration method
1343 * Set the configuration method
1345 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1346 enum connman_ipconfig_method method)
1348 ipconfig->method = method;
1353 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1355 if (ipconfig == NULL)
1356 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1358 return ipconfig->method;
1361 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1365 switch (ipconfig->method) {
1366 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1367 case CONNMAN_IPCONFIG_METHOD_OFF:
1368 case CONNMAN_IPCONFIG_METHOD_AUTO:
1370 case CONNMAN_IPCONFIG_METHOD_FIXED:
1371 case CONNMAN_IPCONFIG_METHOD_DHCP:
1372 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1373 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1374 return connman_inet_set_address(ipconfig->index,
1376 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1377 return connman_inet_set_ipv6_address(
1378 ipconfig->index, ipconfig->address);
1384 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1390 if (ipconfig == NULL)
1393 DBG("method %d", ipconfig->method);
1395 switch (ipconfig->method) {
1396 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1397 case CONNMAN_IPCONFIG_METHOD_OFF:
1398 case CONNMAN_IPCONFIG_METHOD_AUTO:
1400 case CONNMAN_IPCONFIG_METHOD_FIXED:
1401 case CONNMAN_IPCONFIG_METHOD_DHCP:
1402 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1403 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1404 err = connman_inet_clear_address(ipconfig->index,
1406 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1407 err = connman_inet_clear_ipv6_address(
1409 ipconfig->address->local,
1410 ipconfig->address->prefixlen);
1414 connman_ipaddress_clear(ipconfig->address);
1422 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1425 struct connman_ipdevice *ipdevice;
1427 DBG("ipconfig %p", ipconfig);
1429 if (ipconfig == NULL || ipconfig->index < 0)
1432 ipdevice = g_hash_table_lookup(ipdevice_hash,
1433 GINT_TO_POINTER(ipconfig->index));
1434 if (ipdevice == NULL)
1437 g_free(ipdevice->pac);
1438 ipdevice->pac = g_strdup(url);
1443 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1445 struct connman_ipdevice *ipdevice;
1447 DBG("ipconfig %p", ipconfig);
1449 if (ipconfig == NULL || ipconfig->index < 0)
1452 ipdevice = g_hash_table_lookup(ipdevice_hash,
1453 GINT_TO_POINTER(ipconfig->index));
1454 if (ipdevice == NULL)
1457 return ipdevice->pac;
1460 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1462 struct connman_ipdevice *ipdevice;
1466 ipdevice = g_hash_table_lookup(ipdevice_hash,
1467 GINT_TO_POINTER(ipconfig->index));
1468 if (ipdevice == NULL)
1471 set_ipv6_state(ipdevice->ifname, FALSE);
1474 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1476 struct connman_ipdevice *ipdevice;
1480 ipdevice = g_hash_table_lookup(ipdevice_hash,
1481 GINT_TO_POINTER(ipconfig->index));
1482 if (ipdevice == NULL)
1485 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1486 set_ipv6_privacy(ipdevice->ifname,
1487 ipconfig->ipv6_privacy_config);
1489 set_ipv6_state(ipdevice->ifname, TRUE);
1492 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1494 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1497 disable_ipv6(ipconfig);
1500 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1502 struct connman_ipdevice *ipdevice;
1503 gboolean up = FALSE, down = FALSE;
1504 gboolean lower_up = FALSE, lower_down = FALSE;
1505 enum connman_ipconfig_type type;
1507 DBG("ipconfig %p", ipconfig);
1509 if (ipconfig == NULL || ipconfig->index < 0)
1512 ipdevice = g_hash_table_lookup(ipdevice_hash,
1513 GINT_TO_POINTER(ipconfig->index));
1514 if (ipdevice == NULL)
1517 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1518 if (ipdevice->config_ipv4 == ipconfig)
1520 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1521 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1522 if (ipdevice->config_ipv6 == ipconfig)
1524 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1525 enable_ipv6(ipconfig);
1529 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1530 ipdevice->config_ipv4 != NULL) {
1531 ipconfig_list = g_list_remove(ipconfig_list,
1532 ipdevice->config_ipv4);
1534 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1536 connman_ipconfig_unref(ipdevice->config_ipv4);
1539 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1540 ipdevice->config_ipv6 != NULL) {
1541 ipconfig_list = g_list_remove(ipconfig_list,
1542 ipdevice->config_ipv6);
1544 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1546 connman_ipconfig_unref(ipdevice->config_ipv6);
1549 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1550 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1551 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1552 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1554 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1556 if (ipdevice->flags & IFF_UP)
1561 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1562 (IFF_RUNNING | IFF_LOWER_UP))
1564 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1567 if (up == TRUE && ipconfig->ops->up)
1568 ipconfig->ops->up(ipconfig);
1569 if (lower_up == TRUE && ipconfig->ops->lower_up)
1570 ipconfig->ops->lower_up(ipconfig);
1572 if (lower_down == TRUE && ipconfig->ops->lower_down)
1573 ipconfig->ops->lower_down(ipconfig);
1574 if (down == TRUE && ipconfig->ops->down)
1575 ipconfig->ops->down(ipconfig);
1580 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1582 struct connman_ipdevice *ipdevice;
1584 DBG("ipconfig %p", ipconfig);
1586 if (ipconfig == NULL || ipconfig->index < 0)
1589 ipdevice = g_hash_table_lookup(ipdevice_hash,
1590 GINT_TO_POINTER(ipconfig->index));
1591 if (ipdevice == NULL)
1594 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1597 if (ipdevice->config_ipv4 == ipconfig) {
1598 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1600 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1601 connman_ipconfig_unref(ipdevice->config_ipv4);
1602 ipdevice->config_ipv4 = NULL;
1606 if (ipdevice->config_ipv6 == ipconfig) {
1607 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1609 if (ipdevice->config_ipv6->method ==
1610 CONNMAN_IPCONFIG_METHOD_AUTO)
1611 disable_ipv6(ipdevice->config_ipv6);
1613 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1614 connman_ipconfig_unref(ipdevice->config_ipv6);
1615 ipdevice->config_ipv6 = NULL;
1622 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1625 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1627 case CONNMAN_IPCONFIG_METHOD_OFF:
1629 case CONNMAN_IPCONFIG_METHOD_FIXED:
1631 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1633 case CONNMAN_IPCONFIG_METHOD_DHCP:
1635 case CONNMAN_IPCONFIG_METHOD_AUTO:
1642 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1644 if (g_strcmp0(method, "off") == 0)
1645 return CONNMAN_IPCONFIG_METHOD_OFF;
1646 else if (g_strcmp0(method, "fixed") == 0)
1647 return CONNMAN_IPCONFIG_METHOD_FIXED;
1648 else if (g_strcmp0(method, "manual") == 0)
1649 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1650 else if (g_strcmp0(method, "dhcp") == 0)
1651 return CONNMAN_IPCONFIG_METHOD_DHCP;
1652 else if (g_strcmp0(method, "auto") == 0)
1653 return CONNMAN_IPCONFIG_METHOD_AUTO;
1655 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1658 static const char *privacy2string(int privacy)
1662 else if (privacy == 1)
1664 else if (privacy > 1)
1670 static int string2privacy(const char *privacy)
1672 if (g_strcmp0(privacy, "disabled") == 0)
1674 else if (g_strcmp0(privacy, "enabled") == 0)
1676 else if (g_strcmp0(privacy, "prefered") == 0)
1682 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1683 DBusMessageIter *iter)
1689 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1692 str = __connman_ipconfig_method2string(ipconfig->method);
1696 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1698 if (ipconfig->system == NULL)
1701 if (ipconfig->system->local != NULL) {
1703 struct in_addr netmask;
1706 connman_dbus_dict_append_basic(iter, "Address",
1707 DBUS_TYPE_STRING, &ipconfig->system->local);
1709 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1710 netmask.s_addr = htonl(addr);
1711 mask = inet_ntoa(netmask);
1712 connman_dbus_dict_append_basic(iter, "Netmask",
1713 DBUS_TYPE_STRING, &mask);
1716 if (ipconfig->system->gateway != NULL)
1717 connman_dbus_dict_append_basic(iter, "Gateway",
1718 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1721 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1722 DBusMessageIter *iter,
1723 struct connman_ipconfig *ipconfig_ipv4)
1725 const char *str, *privacy;
1729 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1732 str = __connman_ipconfig_method2string(ipconfig->method);
1736 if (ipconfig_ipv4 != NULL &&
1737 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1738 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1742 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1744 if (ipconfig->system == NULL)
1747 if (ipconfig->system->local != NULL) {
1748 connman_dbus_dict_append_basic(iter, "Address",
1749 DBUS_TYPE_STRING, &ipconfig->system->local);
1750 connman_dbus_dict_append_basic(iter, "PrefixLength",
1752 &ipconfig->system->prefixlen);
1755 if (ipconfig->system->gateway != NULL)
1756 connman_dbus_dict_append_basic(iter, "Gateway",
1757 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1759 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1760 connman_dbus_dict_append_basic(iter, "Privacy",
1761 DBUS_TYPE_STRING, &privacy);
1764 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1765 DBusMessageIter *iter)
1767 const char *str, *privacy;
1771 str = __connman_ipconfig_method2string(ipconfig->method);
1775 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1777 switch (ipconfig->method) {
1778 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1779 case CONNMAN_IPCONFIG_METHOD_OFF:
1780 case CONNMAN_IPCONFIG_METHOD_DHCP:
1782 case CONNMAN_IPCONFIG_METHOD_FIXED:
1783 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1784 case CONNMAN_IPCONFIG_METHOD_AUTO:
1788 if (ipconfig->address == NULL)
1791 if (ipconfig->address->local != NULL) {
1792 connman_dbus_dict_append_basic(iter, "Address",
1793 DBUS_TYPE_STRING, &ipconfig->address->local);
1794 connman_dbus_dict_append_basic(iter, "PrefixLength",
1796 &ipconfig->address->prefixlen);
1799 if (ipconfig->address->gateway != NULL)
1800 connman_dbus_dict_append_basic(iter, "Gateway",
1801 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1803 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1804 connman_dbus_dict_append_basic(iter, "Privacy",
1805 DBUS_TYPE_STRING, &privacy);
1808 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1809 DBusMessageIter *iter)
1815 str = __connman_ipconfig_method2string(ipconfig->method);
1819 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1821 switch (ipconfig->method) {
1822 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1823 case CONNMAN_IPCONFIG_METHOD_OFF:
1824 case CONNMAN_IPCONFIG_METHOD_FIXED:
1825 case CONNMAN_IPCONFIG_METHOD_DHCP:
1826 case CONNMAN_IPCONFIG_METHOD_AUTO:
1828 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1832 if (ipconfig->address == NULL)
1835 if (ipconfig->address->local != NULL) {
1837 struct in_addr netmask;
1840 connman_dbus_dict_append_basic(iter, "Address",
1841 DBUS_TYPE_STRING, &ipconfig->address->local);
1843 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1844 netmask.s_addr = htonl(addr);
1845 mask = inet_ntoa(netmask);
1846 connman_dbus_dict_append_basic(iter, "Netmask",
1847 DBUS_TYPE_STRING, &mask);
1850 if (ipconfig->address->gateway != NULL)
1851 connman_dbus_dict_append_basic(iter, "Gateway",
1852 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1855 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1856 DBusMessageIter *array)
1858 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1859 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1860 *prefix_length_string = NULL, *privacy_string = NULL;
1861 int prefix_length = 0, privacy = 0;
1862 DBusMessageIter dict;
1864 DBG("ipconfig %p", ipconfig);
1866 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1869 dbus_message_iter_recurse(array, &dict);
1871 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1872 DBusMessageIter entry;
1876 dbus_message_iter_recurse(&dict, &entry);
1878 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1881 dbus_message_iter_get_basic(&entry, &key);
1882 dbus_message_iter_next(&entry);
1884 type = dbus_message_iter_get_arg_type(&entry);
1886 if (g_str_equal(key, "Method") == TRUE) {
1889 if (type != DBUS_TYPE_STRING)
1892 dbus_message_iter_get_basic(&entry, &str);
1893 method = __connman_ipconfig_string2method(str);
1894 } else if (g_str_equal(key, "Address") == TRUE) {
1895 if (type != DBUS_TYPE_STRING)
1898 dbus_message_iter_get_basic(&entry, &address);
1899 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1900 if (type != DBUS_TYPE_STRING)
1903 dbus_message_iter_get_basic(&entry,
1904 &prefix_length_string);
1906 prefix_length = atoi(prefix_length_string);
1907 if (prefix_length < 0 || prefix_length > 128)
1910 } else if (g_str_equal(key, "Netmask") == TRUE) {
1911 if (type != DBUS_TYPE_STRING)
1914 dbus_message_iter_get_basic(&entry, &netmask);
1915 } else if (g_str_equal(key, "Gateway") == TRUE) {
1916 if (type != DBUS_TYPE_STRING)
1919 dbus_message_iter_get_basic(&entry, &gateway);
1920 } else if (g_str_equal(key, "Privacy") == TRUE) {
1921 if (type != DBUS_TYPE_STRING)
1924 dbus_message_iter_get_basic(&entry, &privacy_string);
1925 privacy = string2privacy(privacy_string);
1927 dbus_message_iter_next(&dict);
1930 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1932 method, address, netmask, gateway, prefix_length,
1936 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1937 case CONNMAN_IPCONFIG_METHOD_FIXED:
1940 case CONNMAN_IPCONFIG_METHOD_OFF:
1941 ipconfig->method = method;
1942 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1943 disable_ipv6(ipconfig);
1946 case CONNMAN_IPCONFIG_METHOD_AUTO:
1947 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1950 ipconfig->method = method;
1951 if (privacy_string != NULL)
1952 ipconfig->ipv6_privacy_config = privacy;
1953 enable_ipv6(ipconfig);
1956 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1957 if (address == NULL)
1960 ipconfig->method = method;
1962 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1963 connman_ipaddress_set_ipv4(ipconfig->address,
1964 address, netmask, gateway);
1966 return connman_ipaddress_set_ipv6(
1967 ipconfig->address, address,
1968 prefix_length, gateway);
1971 case CONNMAN_IPCONFIG_METHOD_DHCP:
1972 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1975 ipconfig->method = method;
1982 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1983 DBusMessageIter *iter)
1985 struct connman_ipdevice *ipdevice;
1986 const char *method = "auto";
1988 connman_dbus_dict_append_basic(iter, "Method",
1989 DBUS_TYPE_STRING, &method);
1991 ipdevice = g_hash_table_lookup(ipdevice_hash,
1992 GINT_TO_POINTER(ipconfig->index));
1993 if (ipdevice == NULL)
1996 if (ipdevice->ifname != NULL)
1997 connman_dbus_dict_append_basic(iter, "Interface",
1998 DBUS_TYPE_STRING, &ipdevice->ifname);
2000 if (ipdevice->address != NULL)
2001 connman_dbus_dict_append_basic(iter, "Address",
2002 DBUS_TYPE_STRING, &ipdevice->address);
2004 if (ipdevice->mtu > 0)
2005 connman_dbus_dict_append_basic(iter, "MTU",
2006 DBUS_TYPE_UINT16, &ipdevice->mtu);
2009 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2010 GKeyFile *keyfile, const char *identifier, const char *prefix)
2015 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2017 key = g_strdup_printf("%smethod", prefix);
2018 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2019 if (method == NULL) {
2020 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2021 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2023 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2025 ipconfig->method = __connman_ipconfig_string2method(method);
2027 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2028 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2030 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2031 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2032 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2034 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2035 privacy = g_key_file_get_string(keyfile, identifier,
2037 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2041 __connman_ipconfig_enable(ipconfig);
2042 enable_ipv6(ipconfig);
2049 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2050 ipconfig->address->prefixlen = g_key_file_get_integer(
2051 keyfile, identifier, key, NULL);
2054 key = g_strdup_printf("%slocal_address", prefix);
2055 ipconfig->address->local = g_key_file_get_string(
2056 keyfile, identifier, key, NULL);
2059 key = g_strdup_printf("%speer_address", prefix);
2060 ipconfig->address->peer = g_key_file_get_string(
2061 keyfile, identifier, key, NULL);
2064 key = g_strdup_printf("%sbroadcast_address", prefix);
2065 ipconfig->address->broadcast = g_key_file_get_string(
2066 keyfile, identifier, key, NULL);
2069 key = g_strdup_printf("%sgateway", prefix);
2070 ipconfig->address->gateway = g_key_file_get_string(
2071 keyfile, identifier, key, NULL);
2077 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2078 GKeyFile *keyfile, const char *identifier, const char *prefix)
2083 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2085 method = __connman_ipconfig_method2string(ipconfig->method);
2087 key = g_strdup_printf("%smethod", prefix);
2088 g_key_file_set_string(keyfile, identifier, key, method);
2091 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2092 const char *privacy;
2093 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2094 key = g_strdup_printf("%sprivacy", prefix);
2095 g_key_file_set_string(keyfile, identifier, key, privacy);
2099 switch (ipconfig->method) {
2100 case CONNMAN_IPCONFIG_METHOD_FIXED:
2101 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2103 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2104 case CONNMAN_IPCONFIG_METHOD_OFF:
2105 case CONNMAN_IPCONFIG_METHOD_DHCP:
2106 case CONNMAN_IPCONFIG_METHOD_AUTO:
2110 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2111 g_key_file_set_integer(keyfile, identifier,
2112 key, ipconfig->address->prefixlen);
2115 key = g_strdup_printf("%slocal_address", prefix);
2116 if (ipconfig->address->local != NULL)
2117 g_key_file_set_string(keyfile, identifier,
2118 key, ipconfig->address->local);
2121 key = g_strdup_printf("%speer_address", prefix);
2122 if (ipconfig->address->peer != NULL)
2123 g_key_file_set_string(keyfile, identifier,
2124 key, ipconfig->address->peer);
2127 key = g_strdup_printf("%sbroadcast_address", prefix);
2128 if (ipconfig->address->broadcast != NULL)
2129 g_key_file_set_string(keyfile, identifier,
2130 key, ipconfig->address->broadcast);
2133 key = g_strdup_printf("%sgateway", prefix);
2134 if (ipconfig->address->gateway != NULL)
2135 g_key_file_set_string(keyfile, identifier,
2136 key, ipconfig->address->gateway);
2142 int __connman_ipconfig_init(void)
2146 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2147 NULL, free_ipdevice);
2152 void __connman_ipconfig_cleanup(void)
2156 g_hash_table_destroy(ipdevice_hash);
2157 ipdevice_hash = NULL;