5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <net/if_arp.h>
30 #include <linux/if_link.h>
35 #define IFF_LOWER_UP 0x10000
39 #include <connman/ipaddress.h>
43 struct connman_ipconfig {
46 enum connman_ipconfig_type type;
48 struct connman_ipconfig *origin;
50 const struct connman_ipconfig_ops *ops;
53 connman_bool_t enabled;
54 enum connman_ipconfig_method method;
55 struct connman_ipaddress *address;
56 struct connman_ipaddress *system;
58 int ipv6_privacy_config;
59 char *last_dhcp_address;
62 struct connman_ipdevice {
84 struct connman_ipconfig *config_ipv4;
85 struct connman_ipconfig *config_ipv6;
87 gboolean ipv6_enabled;
91 static GHashTable *ipdevice_hash = NULL;
92 static GList *ipconfig_list = NULL;
93 static connman_bool_t is_ipv6_supported = FALSE;
95 void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
100 connman_ipaddress_clear(ipconfig->address);
103 static void free_address_list(struct connman_ipdevice *ipdevice)
107 for (list = ipdevice->address_list; list; list = list->next) {
108 struct connman_ipaddress *ipaddress = list->data;
110 connman_ipaddress_free(ipaddress);
114 g_slist_free(ipdevice->address_list);
115 ipdevice->address_list = NULL;
118 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
119 unsigned char prefixlen, const char *local)
123 for (list = ipdevice->address_list; list; list = list->next) {
124 struct connman_ipaddress *ipaddress = list->data;
126 if (g_strcmp0(ipaddress->local, local) == 0 &&
127 ipaddress->prefixlen == prefixlen)
134 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
137 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
139 case CONNMAN_IPCONFIG_TYPE_IPV4:
141 case CONNMAN_IPCONFIG_TYPE_IPV6:
148 static const char *type2str(unsigned short type)
153 case ARPHRD_LOOPBACK:
166 static const char *scope2str(unsigned char scope)
178 static gboolean get_ipv6_state(gchar *ifname)
183 gboolean enabled = FALSE;
186 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
188 path = g_strdup_printf(
189 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
194 f = fopen(path, "r");
199 if (fscanf(f, "%d", &disabled) > 0)
207 static void set_ipv6_state(gchar *ifname, gboolean enable)
213 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
215 path = g_strdup_printf(
216 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
221 f = fopen(path, "r+");
236 static int get_ipv6_privacy(gchar *ifname)
245 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
251 f = fopen(path, "r");
258 if (fscanf(f, "%d", &value) <= 0)
266 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
267 * The privacy extension is described in RFC 3041 and RFC 4941
269 static void set_ipv6_privacy(gchar *ifname, int value)
277 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
286 f = fopen(path, "r+");
293 fprintf(f, "%d", value);
297 static int get_rp_filter()
300 int value = -EINVAL, tmp;
302 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
305 if (fscanf(f, "%d", &tmp) == 1)
313 static void set_rp_filter(int value)
317 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
322 fprintf(f, "%d", value);
327 int __connman_ipconfig_set_rp_filter()
331 value = get_rp_filter();
338 connman_info("rp_filter set to 2 (loose mode routing), "
339 "old value was %d", value);
344 void __connman_ipconfig_unset_rp_filter(int old_value)
346 set_rp_filter(old_value);
348 connman_info("rp_filter restored to %d", old_value);
351 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
353 if (ipconfig == NULL)
356 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
359 static void free_ipdevice(gpointer data)
361 struct connman_ipdevice *ipdevice = data;
363 connman_info("%s {remove} index %d", ipdevice->ifname,
366 if (ipdevice->config_ipv4 != NULL) {
367 __connman_ipconfig_unref(ipdevice->config_ipv4);
368 ipdevice->config_ipv4 = NULL;
371 if (ipdevice->config_ipv6 != NULL) {
372 __connman_ipconfig_unref(ipdevice->config_ipv6);
373 ipdevice->config_ipv6 = NULL;
376 free_address_list(ipdevice);
377 g_free(ipdevice->ipv4_gateway);
378 g_free(ipdevice->ipv6_gateway);
379 g_free(ipdevice->pac);
381 g_free(ipdevice->address);
383 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
384 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
386 g_free(ipdevice->ifname);
390 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
392 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
393 ipdevice->config_ipv6);
395 if (ipdevice->config_ipv6 != NULL &&
396 ipdevice->config_ipv6->enabled == TRUE)
399 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
400 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
401 set_ipv6_state(ipdevice->ifname, FALSE);
405 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
407 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
408 ipdevice->config_ipv6);
410 if (ipdevice->config_ipv4)
411 connman_inet_clear_address(ipdevice->index,
412 ipdevice->config_ipv4->address);
414 if (ipdevice->config_ipv6)
415 connman_inet_clear_ipv6_address(ipdevice->index,
416 ipdevice->config_ipv6->address->local,
417 ipdevice->config_ipv6->address->prefixlen);
420 static void update_stats(struct connman_ipdevice *ipdevice,
421 struct rtnl_link_stats *stats)
423 struct connman_service *service;
425 if (stats->rx_packets == 0 && stats->tx_packets == 0)
428 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
429 stats->rx_packets, stats->rx_bytes);
430 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
431 stats->tx_packets, stats->tx_bytes);
433 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
436 if (ipdevice->config_ipv4)
437 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
438 else if (ipdevice->config_ipv6)
439 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
446 ipdevice->rx_packets = stats->rx_packets;
447 ipdevice->tx_packets = stats->tx_packets;
448 ipdevice->rx_bytes = stats->rx_bytes;
449 ipdevice->tx_bytes = stats->tx_bytes;
450 ipdevice->rx_errors = stats->rx_errors;
451 ipdevice->tx_errors = stats->tx_errors;
452 ipdevice->rx_dropped = stats->rx_dropped;
453 ipdevice->tx_dropped = stats->tx_dropped;
455 __connman_service_notify(service,
456 ipdevice->rx_packets, ipdevice->tx_packets,
457 ipdevice->rx_bytes, ipdevice->tx_bytes,
458 ipdevice->rx_errors, ipdevice->tx_errors,
459 ipdevice->rx_dropped, ipdevice->tx_dropped);
462 void __connman_ipconfig_newlink(int index, unsigned short type,
463 unsigned int flags, const char *address,
465 struct rtnl_link_stats *stats)
467 struct connman_ipdevice *ipdevice;
470 gboolean up = FALSE, down = FALSE;
471 gboolean lower_up = FALSE, lower_down = FALSE;
473 DBG("index %d", index);
475 if (type == ARPHRD_LOOPBACK)
478 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
479 if (ipdevice != NULL) {
480 char *ifname = connman_inet_ifname(index);
481 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
482 DBG("interface name changed %s -> %s",
483 ipdevice->ifname, ifname);
485 g_free(ipdevice->ifname);
486 ipdevice->ifname = ifname;
493 ipdevice = g_try_new0(struct connman_ipdevice, 1);
494 if (ipdevice == NULL)
497 ipdevice->index = index;
498 ipdevice->ifname = connman_inet_ifname(index);
499 ipdevice->type = type;
501 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
502 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
504 ipdevice->address = g_strdup(address);
506 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
508 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
509 index, type, type2str(type));
514 update_stats(ipdevice, stats);
516 if (flags == ipdevice->flags)
519 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
526 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
527 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
528 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
529 (IFF_RUNNING | IFF_LOWER_UP))
531 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
535 ipdevice->flags = flags;
537 str = g_string_new(NULL);
542 g_string_append(str, "UP");
544 g_string_append(str, "DOWN");
546 if (flags & IFF_RUNNING)
547 g_string_append(str, ",RUNNING");
549 if (flags & IFF_LOWER_UP)
550 g_string_append(str, ",LOWER_UP");
552 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
555 g_string_free(str, TRUE);
557 for (list = g_list_first(ipconfig_list); list;
558 list = g_list_next(list)) {
559 struct connman_ipconfig *ipconfig = list->data;
561 if (index != ipconfig->index)
564 if (ipconfig->ops == NULL)
567 if (up == TRUE && ipconfig->ops->up)
568 ipconfig->ops->up(ipconfig);
569 if (lower_up == TRUE && ipconfig->ops->lower_up)
570 ipconfig->ops->lower_up(ipconfig);
572 if (lower_down == TRUE && ipconfig->ops->lower_down)
573 ipconfig->ops->lower_down(ipconfig);
574 if (down == TRUE && ipconfig->ops->down)
575 ipconfig->ops->down(ipconfig);
579 __connman_ipconfig_lower_up(ipdevice);
581 __connman_ipconfig_lower_down(ipdevice);
584 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
586 struct connman_ipdevice *ipdevice;
589 DBG("index %d", index);
591 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
592 if (ipdevice == NULL)
595 update_stats(ipdevice, stats);
597 for (list = g_list_first(ipconfig_list); list;
598 list = g_list_next(list)) {
599 struct connman_ipconfig *ipconfig = list->data;
601 if (index != ipconfig->index)
604 ipconfig->index = -1;
606 if (ipconfig->ops == NULL)
609 if (ipconfig->ops->lower_down)
610 ipconfig->ops->lower_down(ipconfig);
611 if (ipconfig->ops->down)
612 ipconfig->ops->down(ipconfig);
615 __connman_ipconfig_lower_down(ipdevice);
617 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
620 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
622 const struct connman_ipaddress *addr1 = a;
623 const struct connman_ipaddress *addr2 = b;
625 if (addr1->prefixlen != addr2->prefixlen)
626 return addr2->prefixlen - addr1->prefixlen;
628 return g_strcmp0(addr1->local, addr2->local);
631 void __connman_ipconfig_newaddr(int index, int family, const char *label,
632 unsigned char prefixlen, const char *address)
634 struct connman_ipdevice *ipdevice;
635 struct connman_ipaddress *ipaddress;
636 enum connman_ipconfig_type type;
639 DBG("index %d", index);
641 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
642 if (ipdevice == NULL)
645 ipaddress = connman_ipaddress_alloc(family);
646 if (ipaddress == NULL)
649 ipaddress->prefixlen = prefixlen;
650 ipaddress->local = g_strdup(address);
652 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
653 check_duplicate_address)) {
654 connman_ipaddress_free(ipaddress);
658 if (family == AF_INET)
659 type = CONNMAN_IPCONFIG_TYPE_IPV4;
660 else if (family == AF_INET6)
661 type = CONNMAN_IPCONFIG_TYPE_IPV6;
665 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
668 connman_info("%s {add} address %s/%u label %s family %d",
669 ipdevice->ifname, address, prefixlen, label, family);
671 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
672 __connman_ippool_newaddr(index, address, prefixlen);
674 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
675 connman_ipaddress_copy(ipdevice->config_ipv4->system,
678 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
679 connman_ipaddress_copy(ipdevice->config_ipv6->system,
684 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
687 for (list = g_list_first(ipconfig_list); list;
688 list = g_list_next(list)) {
689 struct connman_ipconfig *ipconfig = list->data;
691 if (index != ipconfig->index)
694 if (type != ipconfig->type)
697 if (ipconfig->ops == NULL)
700 if (ipconfig->ops->ip_bound)
701 ipconfig->ops->ip_bound(ipconfig);
705 void __connman_ipconfig_deladdr(int index, int family, const char *label,
706 unsigned char prefixlen, const char *address)
708 struct connman_ipdevice *ipdevice;
709 struct connman_ipaddress *ipaddress;
710 enum connman_ipconfig_type type;
713 DBG("index %d", index);
715 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
716 if (ipdevice == NULL)
719 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
720 if (ipaddress == NULL)
723 if (family == AF_INET)
724 type = CONNMAN_IPCONFIG_TYPE_IPV4;
725 else if (family == AF_INET6)
726 type = CONNMAN_IPCONFIG_TYPE_IPV6;
730 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
733 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
734 __connman_ippool_deladdr(index, address, prefixlen);
736 connman_ipaddress_clear(ipaddress);
739 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
740 address, prefixlen, label);
742 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
745 if (g_slist_length(ipdevice->address_list) > 0)
748 for (list = g_list_first(ipconfig_list); list;
749 list = g_list_next(list)) {
750 struct connman_ipconfig *ipconfig = list->data;
752 if (index != ipconfig->index)
755 if (type != ipconfig->type)
758 if (ipconfig->ops == NULL)
761 if (ipconfig->ops->ip_release)
762 ipconfig->ops->ip_release(ipconfig);
766 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
767 const char *dst, const char *gateway)
769 struct connman_ipdevice *ipdevice;
771 DBG("index %d", index);
773 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
774 if (ipdevice == NULL)
777 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
778 g_strcmp0(dst, "::") == 0)) {
780 enum connman_ipconfig_type type;
782 if (family == AF_INET6) {
783 type = CONNMAN_IPCONFIG_TYPE_IPV6;
784 g_free(ipdevice->ipv6_gateway);
785 ipdevice->ipv6_gateway = g_strdup(gateway);
787 if (ipdevice->config_ipv6 != NULL &&
788 ipdevice->config_ipv6->system != NULL) {
789 g_free(ipdevice->config_ipv6->system->gateway);
790 ipdevice->config_ipv6->system->gateway =
793 } else if (family == AF_INET) {
794 type = CONNMAN_IPCONFIG_TYPE_IPV4;
795 g_free(ipdevice->ipv4_gateway);
796 ipdevice->ipv4_gateway = g_strdup(gateway);
798 if (ipdevice->config_ipv4 != NULL &&
799 ipdevice->config_ipv4->system != NULL) {
800 g_free(ipdevice->config_ipv4->system->gateway);
801 ipdevice->config_ipv4->system->gateway =
807 for (config_list = g_list_first(ipconfig_list); config_list;
808 config_list = g_list_next(config_list)) {
809 struct connman_ipconfig *ipconfig = config_list->data;
811 if (index != ipconfig->index)
814 if (type != ipconfig->type)
817 if (ipconfig->ops == NULL)
820 if (ipconfig->ops->route_set)
821 ipconfig->ops->route_set(ipconfig);
825 connman_info("%s {add} route %s gw %s scope %u <%s>",
826 ipdevice->ifname, dst, gateway,
827 scope, scope2str(scope));
830 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
831 const char *dst, const char *gateway)
833 struct connman_ipdevice *ipdevice;
835 DBG("index %d", index);
837 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
838 if (ipdevice == NULL)
841 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
842 g_strcmp0(dst, "::") == 0)) {
844 enum connman_ipconfig_type type;
846 if (family == AF_INET6) {
847 type = CONNMAN_IPCONFIG_TYPE_IPV6;
848 g_free(ipdevice->ipv6_gateway);
849 ipdevice->ipv6_gateway = NULL;
851 if (ipdevice->config_ipv6 != NULL &&
852 ipdevice->config_ipv6->system != NULL) {
853 g_free(ipdevice->config_ipv6->system->gateway);
854 ipdevice->config_ipv6->system->gateway = NULL;
856 } else if (family == AF_INET) {
857 type = CONNMAN_IPCONFIG_TYPE_IPV4;
858 g_free(ipdevice->ipv4_gateway);
859 ipdevice->ipv4_gateway = NULL;
861 if (ipdevice->config_ipv4 != NULL &&
862 ipdevice->config_ipv4->system != NULL) {
863 g_free(ipdevice->config_ipv4->system->gateway);
864 ipdevice->config_ipv4->system->gateway = NULL;
869 for (config_list = g_list_first(ipconfig_list); config_list;
870 config_list = g_list_next(config_list)) {
871 struct connman_ipconfig *ipconfig = config_list->data;
873 if (index != ipconfig->index)
876 if (type != ipconfig->type)
879 if (ipconfig->ops == NULL)
882 if (ipconfig->ops->route_unset)
883 ipconfig->ops->route_unset(ipconfig);
887 connman_info("%s {del} route %s gw %s scope %u <%s>",
888 ipdevice->ifname, dst, gateway,
889 scope, scope2str(scope));
892 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
897 keys = g_hash_table_get_keys(ipdevice_hash);
901 for (list = g_list_first(keys); list; list = g_list_next(list)) {
902 int index = GPOINTER_TO_INT(list->data);
904 function(index, user_data);
910 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
911 struct connman_ipconfig *ipconfig)
913 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
916 unsigned short __connman_ipconfig_get_type_from_index(int index)
918 struct connman_ipdevice *ipdevice;
920 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
921 if (ipdevice == NULL)
924 return ipdevice->type;
927 unsigned int __connman_ipconfig_get_flags_from_index(int index)
929 struct connman_ipdevice *ipdevice;
931 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
932 if (ipdevice == NULL)
935 return ipdevice->flags;
938 const char *__connman_ipconfig_get_gateway_from_index(int index,
939 enum connman_ipconfig_type type)
941 struct connman_ipdevice *ipdevice;
943 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
944 if (ipdevice == NULL)
947 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
948 if (ipdevice->ipv4_gateway != NULL)
949 return ipdevice->ipv4_gateway;
951 if (ipdevice->config_ipv4 != NULL &&
952 ipdevice->config_ipv4->address != NULL)
953 return ipdevice->config_ipv4->address->gateway;
956 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
957 if (ipdevice->ipv6_gateway != NULL)
958 return ipdevice->ipv6_gateway;
960 if (ipdevice->config_ipv6 != NULL &&
961 ipdevice->config_ipv6->address != NULL)
962 return ipdevice->config_ipv6->address->gateway;
968 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
970 ipconfig->index = index;
973 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
975 if (ipconfig->address == NULL)
978 return ipconfig->address->local;
981 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
983 if (ipconfig->address == NULL)
986 g_free(ipconfig->address->local);
987 ipconfig->address->local = g_strdup(address);
990 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
992 if (ipconfig->address == NULL)
995 return ipconfig->address->peer;
998 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1000 if (ipconfig->address == NULL)
1003 g_free(ipconfig->address->peer);
1004 ipconfig->address->peer = g_strdup(address);
1007 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1009 if (ipconfig->address == NULL)
1012 return ipconfig->address->broadcast;
1015 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1017 if (ipconfig->address == NULL)
1020 g_free(ipconfig->address->broadcast);
1021 ipconfig->address->broadcast = g_strdup(broadcast);
1024 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1026 if (ipconfig->address == NULL)
1029 return ipconfig->address->gateway;
1032 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1036 if (ipconfig->address == NULL)
1038 g_free(ipconfig->address->gateway);
1039 ipconfig->address->gateway = g_strdup(gateway);
1042 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1044 struct connman_service *service;
1048 if (ipconfig->address == NULL)
1051 service = __connman_service_lookup_from_index(ipconfig->index);
1052 if (service == NULL)
1055 __connman_connection_gateway_remove(service, ipconfig->type);
1057 DBG("type %d gw %s peer %s", ipconfig->type,
1058 ipconfig->address->gateway, ipconfig->address->peer);
1060 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1061 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1062 return __connman_connection_gateway_add(service,
1063 ipconfig->address->gateway,
1065 ipconfig->address->peer);
1070 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1072 struct connman_service *service;
1076 service = __connman_service_lookup_from_index(ipconfig->index);
1077 if (service != NULL)
1078 __connman_connection_gateway_remove(service, ipconfig->type);
1081 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1083 if (ipconfig->address == NULL)
1086 return ipconfig->address->prefixlen;
1089 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1091 if (ipconfig->address == NULL)
1094 ipconfig->address->prefixlen = prefixlen;
1097 static struct connman_ipconfig *create_ipv6config(int index)
1099 struct connman_ipconfig *ipv6config;
1100 struct connman_ipdevice *ipdevice;
1102 DBG("index %d", index);
1104 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1105 if (ipv6config == NULL)
1108 ipv6config->refcount = 1;
1110 ipv6config->index = index;
1111 ipv6config->enabled = FALSE;
1112 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1114 if (is_ipv6_supported == FALSE)
1115 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1117 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1119 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1120 if (ipdevice != NULL)
1121 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1123 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1124 if (ipv6config->address == NULL) {
1129 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1131 DBG("ipconfig %p method %s", ipv6config,
1132 __connman_ipconfig_method2string(ipv6config->method));
1138 * connman_ipconfig_create:
1140 * Allocate a new ipconfig structure.
1142 * Returns: a newly-allocated #connman_ipconfig structure
1144 struct connman_ipconfig *__connman_ipconfig_create(int index,
1145 enum connman_ipconfig_type type)
1147 struct connman_ipconfig *ipconfig;
1149 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1150 return create_ipv6config(index);
1152 DBG("index %d", index);
1154 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1155 if (ipconfig == NULL)
1158 ipconfig->refcount = 1;
1160 ipconfig->index = index;
1161 ipconfig->enabled = FALSE;
1162 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1164 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1165 if (ipconfig->address == NULL) {
1170 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1172 DBG("ipconfig %p", ipconfig);
1179 * connman_ipconfig_ref:
1180 * @ipconfig: ipconfig structure
1182 * Increase reference counter of ipconfig
1184 struct connman_ipconfig *
1185 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1186 const char *file, int line, const char *caller)
1188 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1189 file, line, caller);
1191 __sync_fetch_and_add(&ipconfig->refcount, 1);
1197 * connman_ipconfig_unref:
1198 * @ipconfig: ipconfig structure
1200 * Decrease reference counter of ipconfig
1202 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1203 const char *file, int line, const char *caller)
1205 if (ipconfig == NULL)
1208 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1209 file, line, caller);
1211 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1214 if (__connman_ipconfig_disable(ipconfig) < 0)
1215 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1217 __connman_ipconfig_set_ops(ipconfig, NULL);
1219 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1220 __connman_ipconfig_unref(ipconfig->origin);
1221 ipconfig->origin = NULL;
1224 connman_ipaddress_free(ipconfig->system);
1225 connman_ipaddress_free(ipconfig->address);
1226 g_free(ipconfig->last_dhcp_address);
1231 * connman_ipconfig_get_data:
1232 * @ipconfig: ipconfig structure
1234 * Get private data pointer
1236 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1238 if (ipconfig == NULL)
1241 return ipconfig->ops_data;
1245 * connman_ipconfig_set_data:
1246 * @ipconfig: ipconfig structure
1247 * @data: data pointer
1249 * Set private data pointer
1251 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1253 ipconfig->ops_data = data;
1257 * connman_ipconfig_get_index:
1258 * @ipconfig: ipconfig structure
1260 * Get interface index
1262 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1264 if (ipconfig == NULL)
1267 if (ipconfig->origin != NULL)
1268 return ipconfig->origin->index;
1270 return ipconfig->index;
1274 * connman_ipconfig_get_ifname:
1275 * @ipconfig: ipconfig structure
1277 * Get interface name
1279 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1281 struct connman_ipdevice *ipdevice;
1283 if (ipconfig == NULL)
1286 if (ipconfig->index < 0)
1289 ipdevice = g_hash_table_lookup(ipdevice_hash,
1290 GINT_TO_POINTER(ipconfig->index));
1291 if (ipdevice == NULL)
1294 return ipdevice->ifname;
1298 * connman_ipconfig_set_ops:
1299 * @ipconfig: ipconfig structure
1300 * @ops: operation callbacks
1302 * Set the operation callbacks
1304 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1305 const struct connman_ipconfig_ops *ops)
1307 ipconfig->ops = ops;
1311 * connman_ipconfig_set_method:
1312 * @ipconfig: ipconfig structure
1313 * @method: configuration method
1315 * Set the configuration method
1317 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1318 enum connman_ipconfig_method method)
1320 ipconfig->method = method;
1325 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1327 if (ipconfig == NULL)
1328 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1330 return ipconfig->method;
1333 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1337 switch (ipconfig->method) {
1338 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1339 case CONNMAN_IPCONFIG_METHOD_OFF:
1341 case CONNMAN_IPCONFIG_METHOD_AUTO:
1342 case CONNMAN_IPCONFIG_METHOD_FIXED:
1343 case CONNMAN_IPCONFIG_METHOD_DHCP:
1344 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1345 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1346 return connman_inet_set_address(ipconfig->index,
1348 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1349 return connman_inet_set_ipv6_address(
1350 ipconfig->index, ipconfig->address);
1356 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1362 if (ipconfig == NULL)
1365 DBG("method %d", ipconfig->method);
1367 switch (ipconfig->method) {
1368 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1369 case CONNMAN_IPCONFIG_METHOD_OFF:
1371 case CONNMAN_IPCONFIG_METHOD_AUTO:
1372 case CONNMAN_IPCONFIG_METHOD_FIXED:
1373 case CONNMAN_IPCONFIG_METHOD_DHCP:
1374 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1375 err = __connman_ipconfig_address_unset(ipconfig);
1376 connman_ipaddress_clear(ipconfig->address);
1384 int __connman_ipconfig_address_unset(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:
1399 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);
1420 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1423 struct connman_ipdevice *ipdevice;
1425 DBG("ipconfig %p", ipconfig);
1427 if (ipconfig == NULL || ipconfig->index < 0)
1430 ipdevice = g_hash_table_lookup(ipdevice_hash,
1431 GINT_TO_POINTER(ipconfig->index));
1432 if (ipdevice == NULL)
1435 g_free(ipdevice->pac);
1436 ipdevice->pac = g_strdup(url);
1441 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1443 struct connman_ipdevice *ipdevice;
1445 DBG("ipconfig %p", ipconfig);
1447 if (ipconfig == NULL || ipconfig->index < 0)
1450 ipdevice = g_hash_table_lookup(ipdevice_hash,
1451 GINT_TO_POINTER(ipconfig->index));
1452 if (ipdevice == NULL)
1455 return ipdevice->pac;
1458 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1459 const char *address)
1461 if (ipconfig == NULL)
1464 g_free(ipconfig->last_dhcp_address);
1465 ipconfig->last_dhcp_address = g_strdup(address);
1468 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1470 if (ipconfig == NULL)
1473 return ipconfig->last_dhcp_address;
1476 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1478 struct connman_ipdevice *ipdevice;
1482 ipdevice = g_hash_table_lookup(ipdevice_hash,
1483 GINT_TO_POINTER(ipconfig->index));
1484 if (ipdevice == NULL)
1487 set_ipv6_state(ipdevice->ifname, FALSE);
1490 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1492 struct connman_ipdevice *ipdevice;
1496 ipdevice = g_hash_table_lookup(ipdevice_hash,
1497 GINT_TO_POINTER(ipconfig->index));
1498 if (ipdevice == NULL)
1501 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1502 set_ipv6_privacy(ipdevice->ifname,
1503 ipconfig->ipv6_privacy_config);
1505 set_ipv6_state(ipdevice->ifname, TRUE);
1508 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1510 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1513 enable_ipv6(ipconfig);
1516 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1518 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1521 disable_ipv6(ipconfig);
1524 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1526 if (ipconfig == NULL)
1529 switch (ipconfig->method) {
1530 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1531 case CONNMAN_IPCONFIG_METHOD_OFF:
1533 case CONNMAN_IPCONFIG_METHOD_AUTO:
1534 case CONNMAN_IPCONFIG_METHOD_FIXED:
1535 case CONNMAN_IPCONFIG_METHOD_DHCP:
1536 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1543 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1545 struct connman_ipdevice *ipdevice;
1546 gboolean up = FALSE, down = FALSE;
1547 gboolean lower_up = FALSE, lower_down = FALSE;
1548 enum connman_ipconfig_type type;
1550 DBG("ipconfig %p", ipconfig);
1552 if (ipconfig == NULL || ipconfig->index < 0)
1555 ipdevice = g_hash_table_lookup(ipdevice_hash,
1556 GINT_TO_POINTER(ipconfig->index));
1557 if (ipdevice == NULL)
1560 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1561 if (ipdevice->config_ipv4 == ipconfig)
1563 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1564 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1565 if (ipdevice->config_ipv6 == ipconfig)
1567 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1568 enable_ipv6(ipconfig);
1572 ipconfig->enabled = TRUE;
1574 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1575 ipdevice->config_ipv4 != NULL) {
1576 ipconfig_list = g_list_remove(ipconfig_list,
1577 ipdevice->config_ipv4);
1579 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1581 __connman_ipconfig_unref(ipdevice->config_ipv4);
1584 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1585 ipdevice->config_ipv6 != NULL) {
1586 ipconfig_list = g_list_remove(ipconfig_list,
1587 ipdevice->config_ipv6);
1589 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1591 __connman_ipconfig_unref(ipdevice->config_ipv6);
1594 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1595 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1596 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1597 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1599 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1601 if (ipdevice->flags & IFF_UP)
1606 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1607 (IFF_RUNNING | IFF_LOWER_UP))
1609 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1612 if (up == TRUE && ipconfig->ops->up)
1613 ipconfig->ops->up(ipconfig);
1614 if (lower_up == TRUE && ipconfig->ops->lower_up)
1615 ipconfig->ops->lower_up(ipconfig);
1617 if (lower_down == TRUE && ipconfig->ops->lower_down)
1618 ipconfig->ops->lower_down(ipconfig);
1619 if (down == TRUE && ipconfig->ops->down)
1620 ipconfig->ops->down(ipconfig);
1625 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1627 struct connman_ipdevice *ipdevice;
1629 DBG("ipconfig %p", ipconfig);
1631 if (ipconfig == NULL || ipconfig->index < 0)
1634 ipdevice = g_hash_table_lookup(ipdevice_hash,
1635 GINT_TO_POINTER(ipconfig->index));
1636 if (ipdevice == NULL)
1639 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1642 ipconfig->enabled = FALSE;
1644 if (ipdevice->config_ipv4 == ipconfig) {
1645 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1647 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1648 __connman_ipconfig_unref(ipdevice->config_ipv4);
1649 ipdevice->config_ipv4 = NULL;
1653 if (ipdevice->config_ipv6 == ipconfig) {
1654 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1656 if (ipdevice->config_ipv6->method ==
1657 CONNMAN_IPCONFIG_METHOD_AUTO)
1658 disable_ipv6(ipdevice->config_ipv6);
1660 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1661 __connman_ipconfig_unref(ipdevice->config_ipv6);
1662 ipdevice->config_ipv6 = NULL;
1669 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1672 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1674 case CONNMAN_IPCONFIG_METHOD_OFF:
1676 case CONNMAN_IPCONFIG_METHOD_FIXED:
1678 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1680 case CONNMAN_IPCONFIG_METHOD_DHCP:
1682 case CONNMAN_IPCONFIG_METHOD_AUTO:
1689 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1691 if (g_strcmp0(method, "off") == 0)
1692 return CONNMAN_IPCONFIG_METHOD_OFF;
1693 else if (g_strcmp0(method, "fixed") == 0)
1694 return CONNMAN_IPCONFIG_METHOD_FIXED;
1695 else if (g_strcmp0(method, "manual") == 0)
1696 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1697 else if (g_strcmp0(method, "dhcp") == 0)
1698 return CONNMAN_IPCONFIG_METHOD_DHCP;
1699 else if (g_strcmp0(method, "auto") == 0)
1700 return CONNMAN_IPCONFIG_METHOD_AUTO;
1702 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1705 static const char *privacy2string(int privacy)
1709 else if (privacy == 1)
1711 else if (privacy > 1)
1717 static int string2privacy(const char *privacy)
1719 if (g_strcmp0(privacy, "disabled") == 0)
1721 else if (g_strcmp0(privacy, "enabled") == 0)
1723 else if (g_strcmp0(privacy, "prefered") == 0)
1729 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1730 DBusMessageIter *iter)
1732 struct connman_ipaddress *append_addr = NULL;
1737 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1740 str = __connman_ipconfig_method2string(ipconfig->method);
1744 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1746 switch (ipconfig->method) {
1747 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1748 case CONNMAN_IPCONFIG_METHOD_OFF:
1749 case CONNMAN_IPCONFIG_METHOD_AUTO:
1752 case CONNMAN_IPCONFIG_METHOD_FIXED:
1753 append_addr = ipconfig->address;
1756 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1757 case CONNMAN_IPCONFIG_METHOD_DHCP:
1758 append_addr = ipconfig->system;
1762 if (append_addr == NULL)
1765 if (append_addr->local != NULL) {
1767 struct in_addr netmask;
1770 connman_dbus_dict_append_basic(iter, "Address",
1771 DBUS_TYPE_STRING, &append_addr->local);
1773 addr = 0xffffffff << (32 - append_addr->prefixlen);
1774 netmask.s_addr = htonl(addr);
1775 mask = inet_ntoa(netmask);
1776 connman_dbus_dict_append_basic(iter, "Netmask",
1777 DBUS_TYPE_STRING, &mask);
1780 if (append_addr->gateway != NULL)
1781 connman_dbus_dict_append_basic(iter, "Gateway",
1782 DBUS_TYPE_STRING, &append_addr->gateway);
1785 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1786 DBusMessageIter *iter,
1787 struct connman_ipconfig *ipconfig_ipv4)
1789 struct connman_ipaddress *append_addr = NULL;
1790 const char *str, *privacy;
1794 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1797 str = __connman_ipconfig_method2string(ipconfig->method);
1801 if (ipconfig_ipv4 != NULL &&
1802 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1803 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1807 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1809 switch (ipconfig->method) {
1810 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1811 case CONNMAN_IPCONFIG_METHOD_OFF:
1814 case CONNMAN_IPCONFIG_METHOD_FIXED:
1815 append_addr = ipconfig->address;
1818 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1819 case CONNMAN_IPCONFIG_METHOD_DHCP:
1820 case CONNMAN_IPCONFIG_METHOD_AUTO:
1821 append_addr = ipconfig->system;
1825 if (append_addr == NULL)
1828 if (append_addr->local != NULL) {
1829 connman_dbus_dict_append_basic(iter, "Address",
1830 DBUS_TYPE_STRING, &append_addr->local);
1831 connman_dbus_dict_append_basic(iter, "PrefixLength",
1833 &append_addr->prefixlen);
1836 if (append_addr->gateway != NULL)
1837 connman_dbus_dict_append_basic(iter, "Gateway",
1838 DBUS_TYPE_STRING, &append_addr->gateway);
1840 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1841 connman_dbus_dict_append_basic(iter, "Privacy",
1842 DBUS_TYPE_STRING, &privacy);
1845 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1846 DBusMessageIter *iter)
1848 const char *str, *privacy;
1852 str = __connman_ipconfig_method2string(ipconfig->method);
1856 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1858 switch (ipconfig->method) {
1859 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1860 case CONNMAN_IPCONFIG_METHOD_OFF:
1861 case CONNMAN_IPCONFIG_METHOD_DHCP:
1863 case CONNMAN_IPCONFIG_METHOD_FIXED:
1864 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1865 case CONNMAN_IPCONFIG_METHOD_AUTO:
1869 if (ipconfig->address == NULL)
1872 if (ipconfig->address->local != NULL) {
1873 connman_dbus_dict_append_basic(iter, "Address",
1874 DBUS_TYPE_STRING, &ipconfig->address->local);
1875 connman_dbus_dict_append_basic(iter, "PrefixLength",
1877 &ipconfig->address->prefixlen);
1880 if (ipconfig->address->gateway != NULL)
1881 connman_dbus_dict_append_basic(iter, "Gateway",
1882 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1884 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1885 connman_dbus_dict_append_basic(iter, "Privacy",
1886 DBUS_TYPE_STRING, &privacy);
1889 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1890 DBusMessageIter *iter)
1896 str = __connman_ipconfig_method2string(ipconfig->method);
1900 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1902 switch (ipconfig->method) {
1903 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1904 case CONNMAN_IPCONFIG_METHOD_OFF:
1905 case CONNMAN_IPCONFIG_METHOD_DHCP:
1906 case CONNMAN_IPCONFIG_METHOD_AUTO:
1908 case CONNMAN_IPCONFIG_METHOD_FIXED:
1909 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1913 if (ipconfig->address == NULL)
1916 if (ipconfig->address->local != NULL) {
1918 struct in_addr netmask;
1921 connman_dbus_dict_append_basic(iter, "Address",
1922 DBUS_TYPE_STRING, &ipconfig->address->local);
1924 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1925 netmask.s_addr = htonl(addr);
1926 mask = inet_ntoa(netmask);
1927 connman_dbus_dict_append_basic(iter, "Netmask",
1928 DBUS_TYPE_STRING, &mask);
1931 if (ipconfig->address->gateway != NULL)
1932 connman_dbus_dict_append_basic(iter, "Gateway",
1933 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1936 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1937 DBusMessageIter *array)
1939 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1940 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1941 *privacy_string = NULL;
1942 int prefix_length = 0, privacy = 0;
1943 DBusMessageIter dict;
1945 DBG("ipconfig %p", ipconfig);
1947 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1950 dbus_message_iter_recurse(array, &dict);
1952 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1953 DBusMessageIter entry, value;
1957 dbus_message_iter_recurse(&dict, &entry);
1959 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1962 dbus_message_iter_get_basic(&entry, &key);
1963 dbus_message_iter_next(&entry);
1965 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
1968 dbus_message_iter_recurse(&entry, &value);
1970 type = dbus_message_iter_get_arg_type(&value);
1972 if (g_str_equal(key, "Method") == TRUE) {
1975 if (type != DBUS_TYPE_STRING)
1978 dbus_message_iter_get_basic(&value, &str);
1979 method = __connman_ipconfig_string2method(str);
1980 } else if (g_str_equal(key, "Address") == TRUE) {
1981 if (type != DBUS_TYPE_STRING)
1984 dbus_message_iter_get_basic(&value, &address);
1985 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1986 if (type != DBUS_TYPE_BYTE)
1989 dbus_message_iter_get_basic(&value, &prefix_length);
1991 if (prefix_length < 0 || prefix_length > 128)
1993 } else if (g_str_equal(key, "Netmask") == TRUE) {
1994 if (type != DBUS_TYPE_STRING)
1997 dbus_message_iter_get_basic(&value, &netmask);
1998 } else if (g_str_equal(key, "Gateway") == TRUE) {
1999 if (type != DBUS_TYPE_STRING)
2002 dbus_message_iter_get_basic(&value, &gateway);
2003 } else if (g_str_equal(key, "Privacy") == TRUE) {
2004 if (type != DBUS_TYPE_STRING)
2007 dbus_message_iter_get_basic(&value, &privacy_string);
2008 privacy = string2privacy(privacy_string);
2011 dbus_message_iter_next(&dict);
2014 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2016 method, address, netmask, gateway, prefix_length,
2020 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2021 case CONNMAN_IPCONFIG_METHOD_FIXED:
2024 case CONNMAN_IPCONFIG_METHOD_OFF:
2025 ipconfig->method = method;
2026 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2027 disable_ipv6(ipconfig);
2030 case CONNMAN_IPCONFIG_METHOD_AUTO:
2031 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2034 ipconfig->method = method;
2035 if (privacy_string != NULL)
2036 ipconfig->ipv6_privacy_config = privacy;
2037 enable_ipv6(ipconfig);
2040 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2041 if (address == NULL)
2044 ipconfig->method = method;
2046 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2047 connman_ipaddress_set_ipv4(ipconfig->address,
2048 address, netmask, gateway);
2050 return connman_ipaddress_set_ipv6(
2051 ipconfig->address, address,
2052 prefix_length, gateway);
2055 case CONNMAN_IPCONFIG_METHOD_DHCP:
2056 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2059 ipconfig->method = method;
2066 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2067 DBusMessageIter *iter)
2069 struct connman_ipdevice *ipdevice;
2070 const char *method = "auto";
2072 connman_dbus_dict_append_basic(iter, "Method",
2073 DBUS_TYPE_STRING, &method);
2075 ipdevice = g_hash_table_lookup(ipdevice_hash,
2076 GINT_TO_POINTER(ipconfig->index));
2077 if (ipdevice == NULL)
2080 if (ipdevice->ifname != NULL)
2081 connman_dbus_dict_append_basic(iter, "Interface",
2082 DBUS_TYPE_STRING, &ipdevice->ifname);
2084 if (ipdevice->address != NULL)
2085 connman_dbus_dict_append_basic(iter, "Address",
2086 DBUS_TYPE_STRING, &ipdevice->address);
2088 if (ipdevice->mtu > 0)
2089 connman_dbus_dict_append_basic(iter, "MTU",
2090 DBUS_TYPE_UINT16, &ipdevice->mtu);
2093 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2094 GKeyFile *keyfile, const char *identifier, const char *prefix)
2100 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2102 key = g_strdup_printf("%smethod", prefix);
2103 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2104 if (method == NULL) {
2105 switch (ipconfig->type) {
2106 case CONNMAN_IPCONFIG_TYPE_IPV4:
2107 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2109 case CONNMAN_IPCONFIG_TYPE_IPV6:
2110 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2112 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2113 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2117 ipconfig->method = __connman_ipconfig_string2method(method);
2119 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2120 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2122 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2123 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2124 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2126 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2127 privacy = g_key_file_get_string(keyfile, identifier,
2129 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2138 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2139 ipconfig->address->prefixlen = g_key_file_get_integer(
2140 keyfile, identifier, key, NULL);
2143 key = g_strdup_printf("%slocal_address", prefix);
2144 ipconfig->address->local = g_key_file_get_string(
2145 keyfile, identifier, key, NULL);
2148 key = g_strdup_printf("%speer_address", prefix);
2149 ipconfig->address->peer = g_key_file_get_string(
2150 keyfile, identifier, key, NULL);
2153 key = g_strdup_printf("%sbroadcast_address", prefix);
2154 ipconfig->address->broadcast = g_key_file_get_string(
2155 keyfile, identifier, key, NULL);
2158 key = g_strdup_printf("%sgateway", prefix);
2159 ipconfig->address->gateway = g_key_file_get_string(
2160 keyfile, identifier, key, NULL);
2163 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2164 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2166 g_free(ipconfig->last_dhcp_address);
2167 ipconfig->last_dhcp_address = str;
2174 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2175 GKeyFile *keyfile, const char *identifier, const char *prefix)
2180 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2182 method = __connman_ipconfig_method2string(ipconfig->method);
2184 key = g_strdup_printf("%smethod", prefix);
2185 g_key_file_set_string(keyfile, identifier, key, method);
2188 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2189 const char *privacy;
2190 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2191 key = g_strdup_printf("%sprivacy", prefix);
2192 g_key_file_set_string(keyfile, identifier, key, privacy);
2196 switch (ipconfig->method) {
2197 case CONNMAN_IPCONFIG_METHOD_FIXED:
2198 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2200 case CONNMAN_IPCONFIG_METHOD_DHCP:
2201 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2202 if (ipconfig->last_dhcp_address != NULL &&
2203 strlen(ipconfig->last_dhcp_address) > 0)
2204 g_key_file_set_string(keyfile, identifier, key,
2205 ipconfig->last_dhcp_address);
2207 g_key_file_remove_key(keyfile, identifier, key, NULL);
2210 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2211 case CONNMAN_IPCONFIG_METHOD_OFF:
2212 case CONNMAN_IPCONFIG_METHOD_AUTO:
2216 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2217 if (ipconfig->address->prefixlen != 0)
2218 g_key_file_set_integer(keyfile, identifier,
2219 key, ipconfig->address->prefixlen);
2222 key = g_strdup_printf("%slocal_address", prefix);
2223 if (ipconfig->address->local != NULL)
2224 g_key_file_set_string(keyfile, identifier,
2225 key, ipconfig->address->local);
2228 key = g_strdup_printf("%speer_address", prefix);
2229 if (ipconfig->address->peer != NULL)
2230 g_key_file_set_string(keyfile, identifier,
2231 key, ipconfig->address->peer);
2234 key = g_strdup_printf("%sbroadcast_address", prefix);
2235 if (ipconfig->address->broadcast != NULL)
2236 g_key_file_set_string(keyfile, identifier,
2237 key, ipconfig->address->broadcast);
2240 key = g_strdup_printf("%sgateway", prefix);
2241 if (ipconfig->address->gateway != NULL)
2242 g_key_file_set_string(keyfile, identifier,
2243 key, ipconfig->address->gateway);
2249 int __connman_ipconfig_init(void)
2253 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2254 NULL, free_ipdevice);
2256 is_ipv6_supported = connman_inet_is_ipv6_supported();
2261 void __connman_ipconfig_cleanup(void)
2265 g_hash_table_destroy(ipdevice_hash);
2266 ipdevice_hash = NULL;