5 * Copyright (C) 2007-2013 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;
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;
60 char **last_dhcpv6_prefixes;
63 struct connman_ipdevice {
84 struct connman_ipconfig *config_ipv4;
85 struct connman_ipconfig *config_ipv6;
91 static GHashTable *ipdevice_hash = NULL;
92 static GList *ipconfig_list = NULL;
93 static bool 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 bool get_ipv6_state(gchar *ifname)
183 bool 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, bool 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(void)
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 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
356 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
359 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
361 struct connman_ipdevice *ipdevice;
368 ipdevice = g_hash_table_lookup(ipdevice_hash,
369 GINT_TO_POINTER(ipconfig->index));
373 ifname = connman_inet_ifname(ipconfig->index);
374 ret = get_ipv6_state(ifname);
380 static void free_ipdevice(gpointer data)
382 struct connman_ipdevice *ipdevice = data;
383 char *ifname = connman_inet_ifname(ipdevice->index);
385 connman_info("%s {remove} index %d", ifname, ipdevice->index);
387 if (ipdevice->config_ipv4) {
388 __connman_ipconfig_unref(ipdevice->config_ipv4);
389 ipdevice->config_ipv4 = NULL;
392 if (ipdevice->config_ipv6) {
393 __connman_ipconfig_unref(ipdevice->config_ipv6);
394 ipdevice->config_ipv6 = NULL;
397 free_address_list(ipdevice);
398 g_free(ipdevice->ipv4_gateway);
399 g_free(ipdevice->ipv6_gateway);
400 g_free(ipdevice->pac);
402 g_free(ipdevice->address);
404 set_ipv6_state(ifname, ipdevice->ipv6_enabled);
405 set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
411 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
413 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
414 ipdevice->config_ipv6);
417 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
419 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
420 ipdevice->config_ipv6);
422 if (ipdevice->config_ipv4)
423 connman_inet_clear_address(ipdevice->index,
424 ipdevice->config_ipv4->address);
426 if (ipdevice->config_ipv6)
427 connman_inet_clear_ipv6_address(ipdevice->index,
428 ipdevice->config_ipv6->address->local,
429 ipdevice->config_ipv6->address->prefixlen);
432 static void update_stats(struct connman_ipdevice *ipdevice,
433 const char *ifname, struct rtnl_link_stats *stats)
435 struct connman_service *service;
437 if (stats->rx_packets == 0 && stats->tx_packets == 0)
440 connman_info("%s {RX} %u packets %u bytes", ifname,
441 stats->rx_packets, stats->rx_bytes);
442 connman_info("%s {TX} %u packets %u bytes", ifname,
443 stats->tx_packets, stats->tx_bytes);
445 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
448 if (ipdevice->config_ipv4)
449 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
450 else if (ipdevice->config_ipv6)
451 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
458 ipdevice->rx_packets = stats->rx_packets;
459 ipdevice->tx_packets = stats->tx_packets;
460 ipdevice->rx_bytes = stats->rx_bytes;
461 ipdevice->tx_bytes = stats->tx_bytes;
462 ipdevice->rx_errors = stats->rx_errors;
463 ipdevice->tx_errors = stats->tx_errors;
464 ipdevice->rx_dropped = stats->rx_dropped;
465 ipdevice->tx_dropped = stats->tx_dropped;
467 __connman_service_notify(service,
468 ipdevice->rx_packets, ipdevice->tx_packets,
469 ipdevice->rx_bytes, ipdevice->tx_bytes,
470 ipdevice->rx_errors, ipdevice->tx_errors,
471 ipdevice->rx_dropped, ipdevice->tx_dropped);
474 void __connman_ipconfig_newlink(int index, unsigned short type,
475 unsigned int flags, const char *address,
477 struct rtnl_link_stats *stats)
479 struct connman_ipdevice *ipdevice;
480 GList *list, *ipconfig_copy;
482 bool up = false, down = false;
483 bool lower_up = false, lower_down = false;
486 DBG("index %d", index);
488 if (type == ARPHRD_LOOPBACK)
491 ifname = connman_inet_ifname(index);
493 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
497 ipdevice = g_try_new0(struct connman_ipdevice, 1);
501 ipdevice->index = index;
502 ipdevice->type = type;
504 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
505 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
507 ipdevice->address = g_strdup(address);
509 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
511 connman_info("%s {create} index %d type %d <%s>", ifname,
512 index, type, type2str(type));
517 update_stats(ipdevice, ifname, stats);
519 if (flags == ipdevice->flags)
522 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
529 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
530 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
531 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
532 (IFF_RUNNING | IFF_LOWER_UP))
534 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
538 ipdevice->flags = flags;
540 str = g_string_new(NULL);
545 g_string_append(str, "UP");
547 g_string_append(str, "DOWN");
549 if (flags & IFF_RUNNING)
550 g_string_append(str, ",RUNNING");
552 if (flags & IFF_LOWER_UP)
553 g_string_append(str, ",LOWER_UP");
555 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
557 g_string_free(str, TRUE);
559 ipconfig_copy = g_list_copy(ipconfig_list);
561 for (list = g_list_first(ipconfig_copy); list;
562 list = g_list_next(list)) {
563 struct connman_ipconfig *ipconfig = list->data;
565 if (index != ipconfig->index)
571 if (up && ipconfig->ops->up)
572 ipconfig->ops->up(ipconfig, ifname);
573 if (lower_up && ipconfig->ops->lower_up)
574 ipconfig->ops->lower_up(ipconfig, ifname);
576 if (lower_down && ipconfig->ops->lower_down)
577 ipconfig->ops->lower_down(ipconfig, ifname);
578 if (down && ipconfig->ops->down)
579 ipconfig->ops->down(ipconfig, ifname);
582 g_list_free(ipconfig_copy);
585 __connman_ipconfig_lower_up(ipdevice);
587 __connman_ipconfig_lower_down(ipdevice);
593 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
595 struct connman_ipdevice *ipdevice;
599 DBG("index %d", index);
601 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
605 ifname = connman_inet_ifname(index);
607 update_stats(ipdevice, ifname, stats);
609 for (list = g_list_first(ipconfig_list); list;
610 list = g_list_next(list)) {
611 struct connman_ipconfig *ipconfig = list->data;
613 if (index != ipconfig->index)
616 ipconfig->index = -1;
621 if (ipconfig->ops->lower_down)
622 ipconfig->ops->lower_down(ipconfig, ifname);
623 if (ipconfig->ops->down)
624 ipconfig->ops->down(ipconfig, ifname);
629 __connman_ipconfig_lower_down(ipdevice);
631 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
634 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
636 const struct connman_ipaddress *addr1 = a;
637 const struct connman_ipaddress *addr2 = b;
639 if (addr1->prefixlen != addr2->prefixlen)
640 return addr2->prefixlen - addr1->prefixlen;
642 return g_strcmp0(addr1->local, addr2->local);
645 void __connman_ipconfig_newaddr(int index, int family, const char *label,
646 unsigned char prefixlen, const char *address)
648 struct connman_ipdevice *ipdevice;
649 struct connman_ipaddress *ipaddress;
650 enum connman_ipconfig_type type;
654 DBG("index %d", index);
656 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
660 ipaddress = connman_ipaddress_alloc(family);
664 ipaddress->prefixlen = prefixlen;
665 ipaddress->local = g_strdup(address);
667 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
668 check_duplicate_address)) {
669 connman_ipaddress_free(ipaddress);
673 if (family == AF_INET)
674 type = CONNMAN_IPCONFIG_TYPE_IPV4;
675 else if (family == AF_INET6)
676 type = CONNMAN_IPCONFIG_TYPE_IPV6;
680 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
683 ifname = connman_inet_ifname(index);
684 connman_info("%s {add} address %s/%u label %s family %d",
685 ifname, address, prefixlen, label, family);
687 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
688 __connman_ippool_newaddr(index, address, prefixlen);
690 if (ipdevice->config_ipv4 && family == AF_INET)
691 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
694 else if (ipdevice->config_ipv6 && family == AF_INET6)
695 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
700 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
703 for (list = g_list_first(ipconfig_list); list;
704 list = g_list_next(list)) {
705 struct connman_ipconfig *ipconfig = list->data;
707 if (index != ipconfig->index)
710 if (type != ipconfig->type)
716 if (ipconfig->ops->ip_bound)
717 ipconfig->ops->ip_bound(ipconfig, ifname);
724 void __connman_ipconfig_deladdr(int index, int family, const char *label,
725 unsigned char prefixlen, const char *address)
727 struct connman_ipdevice *ipdevice;
728 struct connman_ipaddress *ipaddress;
729 enum connman_ipconfig_type type;
733 DBG("index %d", index);
735 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
739 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
743 if (family == AF_INET)
744 type = CONNMAN_IPCONFIG_TYPE_IPV4;
745 else if (family == AF_INET6)
746 type = CONNMAN_IPCONFIG_TYPE_IPV6;
750 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
753 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
754 __connman_ippool_deladdr(index, address, prefixlen);
756 connman_ipaddress_clear(ipaddress);
759 ifname = connman_inet_ifname(index);
760 connman_info("%s {del} address %s/%u label %s", ifname,
761 address, prefixlen, label);
763 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
766 if (g_slist_length(ipdevice->address_list) > 0)
769 for (list = g_list_first(ipconfig_list); list;
770 list = g_list_next(list)) {
771 struct connman_ipconfig *ipconfig = list->data;
773 if (index != ipconfig->index)
776 if (type != ipconfig->type)
782 if (ipconfig->ops->ip_release)
783 ipconfig->ops->ip_release(ipconfig, ifname);
790 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
791 const char *dst, const char *gateway)
793 struct connman_ipdevice *ipdevice;
796 DBG("index %d", index);
798 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
802 ifname = connman_inet_ifname(index);
804 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
805 g_strcmp0(dst, "::") == 0)) {
807 enum connman_ipconfig_type type;
809 if (family == AF_INET6) {
810 type = CONNMAN_IPCONFIG_TYPE_IPV6;
811 g_free(ipdevice->ipv6_gateway);
812 ipdevice->ipv6_gateway = g_strdup(gateway);
814 if (ipdevice->config_ipv6 &&
815 ipdevice->config_ipv6->system) {
816 g_free(ipdevice->config_ipv6->system->gateway);
817 ipdevice->config_ipv6->system->gateway =
820 } else if (family == AF_INET) {
821 type = CONNMAN_IPCONFIG_TYPE_IPV4;
822 g_free(ipdevice->ipv4_gateway);
823 ipdevice->ipv4_gateway = g_strdup(gateway);
825 if (ipdevice->config_ipv4 &&
826 ipdevice->config_ipv4->system) {
827 g_free(ipdevice->config_ipv4->system->gateway);
828 ipdevice->config_ipv4->system->gateway =
834 for (config_list = g_list_first(ipconfig_list); config_list;
835 config_list = g_list_next(config_list)) {
836 struct connman_ipconfig *ipconfig = config_list->data;
838 if (index != ipconfig->index)
841 if (type != ipconfig->type)
847 if (ipconfig->ops->route_set)
848 ipconfig->ops->route_set(ipconfig, ifname);
852 connman_info("%s {add} route %s gw %s scope %u <%s>",
853 ifname, dst, gateway, scope, scope2str(scope));
859 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
860 const char *dst, const char *gateway)
862 struct connman_ipdevice *ipdevice;
865 DBG("index %d", index);
867 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
871 ifname = connman_inet_ifname(index);
873 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
874 g_strcmp0(dst, "::") == 0)) {
876 enum connman_ipconfig_type type;
878 if (family == AF_INET6) {
879 type = CONNMAN_IPCONFIG_TYPE_IPV6;
880 g_free(ipdevice->ipv6_gateway);
881 ipdevice->ipv6_gateway = NULL;
883 if (ipdevice->config_ipv6 &&
884 ipdevice->config_ipv6->system) {
885 g_free(ipdevice->config_ipv6->system->gateway);
886 ipdevice->config_ipv6->system->gateway = NULL;
888 } else if (family == AF_INET) {
889 type = CONNMAN_IPCONFIG_TYPE_IPV4;
890 g_free(ipdevice->ipv4_gateway);
891 ipdevice->ipv4_gateway = NULL;
893 if (ipdevice->config_ipv4 &&
894 ipdevice->config_ipv4->system) {
895 g_free(ipdevice->config_ipv4->system->gateway);
896 ipdevice->config_ipv4->system->gateway = NULL;
901 for (config_list = g_list_first(ipconfig_list); config_list;
902 config_list = g_list_next(config_list)) {
903 struct connman_ipconfig *ipconfig = config_list->data;
905 if (index != ipconfig->index)
908 if (type != ipconfig->type)
914 if (ipconfig->ops->route_unset)
915 ipconfig->ops->route_unset(ipconfig, ifname);
919 connman_info("%s {del} route %s gw %s scope %u <%s>",
920 ifname, dst, gateway, scope, scope2str(scope));
926 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
931 keys = g_hash_table_get_keys(ipdevice_hash);
935 for (list = g_list_first(keys); list; list = g_list_next(list)) {
936 int index = GPOINTER_TO_INT(list->data);
938 function(index, user_data);
944 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
945 struct connman_ipconfig *ipconfig)
947 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
950 unsigned short __connman_ipconfig_get_type_from_index(int index)
952 struct connman_ipdevice *ipdevice;
954 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
958 return ipdevice->type;
961 unsigned int __connman_ipconfig_get_flags_from_index(int index)
963 struct connman_ipdevice *ipdevice;
965 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
969 return ipdevice->flags;
972 const char *__connman_ipconfig_get_gateway_from_index(int index,
973 enum connman_ipconfig_type type)
975 struct connman_ipdevice *ipdevice;
977 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
981 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
982 if (ipdevice->ipv4_gateway)
983 return ipdevice->ipv4_gateway;
985 if (ipdevice->config_ipv4 &&
986 ipdevice->config_ipv4->address)
987 return ipdevice->config_ipv4->address->gateway;
990 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
991 if (ipdevice->ipv6_gateway)
992 return ipdevice->ipv6_gateway;
994 if (ipdevice->config_ipv6 &&
995 ipdevice->config_ipv6->address)
996 return ipdevice->config_ipv6->address->gateway;
1002 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1004 ipconfig->index = index;
1007 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1009 if (!ipconfig->address)
1012 return ipconfig->address->local;
1015 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1016 const char *address)
1018 if (!ipconfig->address)
1021 g_free(ipconfig->address->local);
1022 ipconfig->address->local = g_strdup(address);
1025 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1027 if (!ipconfig->address)
1030 return ipconfig->address->peer;
1033 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1034 const char *address)
1036 if (!ipconfig->address)
1039 g_free(ipconfig->address->peer);
1040 ipconfig->address->peer = g_strdup(address);
1043 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1045 if (!ipconfig->address)
1048 return ipconfig->address->broadcast;
1051 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1052 const char *broadcast)
1054 if (!ipconfig->address)
1057 g_free(ipconfig->address->broadcast);
1058 ipconfig->address->broadcast = g_strdup(broadcast);
1061 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1063 if (!ipconfig->address)
1066 return ipconfig->address->gateway;
1069 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1070 const char *gateway)
1074 if (!ipconfig->address)
1076 g_free(ipconfig->address->gateway);
1077 ipconfig->address->gateway = g_strdup(gateway);
1080 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1082 struct connman_service *service;
1086 if (!ipconfig->address)
1089 service = __connman_service_lookup_from_index(ipconfig->index);
1093 DBG("type %d gw %s peer %s", ipconfig->type,
1094 ipconfig->address->gateway, ipconfig->address->peer);
1096 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1097 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1098 return __connman_connection_gateway_add(service,
1099 ipconfig->address->gateway,
1101 ipconfig->address->peer);
1106 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1108 struct connman_service *service;
1112 service = __connman_service_lookup_from_index(ipconfig->index);
1114 __connman_connection_gateway_remove(service, ipconfig->type);
1117 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1119 if (!ipconfig->address)
1122 return ipconfig->address->prefixlen;
1125 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1126 unsigned char prefixlen)
1128 if (!ipconfig->address)
1131 ipconfig->address->prefixlen = prefixlen;
1134 static struct connman_ipconfig *create_ipv6config(int index)
1136 struct connman_ipconfig *ipv6config;
1137 struct connman_ipdevice *ipdevice;
1139 DBG("index %d", index);
1141 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1145 ipv6config->refcount = 1;
1147 ipv6config->index = index;
1148 ipv6config->enabled = false;
1149 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1151 if (!is_ipv6_supported)
1152 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1154 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1156 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1158 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1160 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1161 if (!ipv6config->address) {
1166 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1168 DBG("ipconfig %p method %s", ipv6config,
1169 __connman_ipconfig_method2string(ipv6config->method));
1175 * connman_ipconfig_create:
1177 * Allocate a new ipconfig structure.
1179 * Returns: a newly-allocated #connman_ipconfig structure
1181 struct connman_ipconfig *__connman_ipconfig_create(int index,
1182 enum connman_ipconfig_type type)
1184 struct connman_ipconfig *ipconfig;
1186 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1187 return create_ipv6config(index);
1189 DBG("index %d", index);
1191 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1195 ipconfig->refcount = 1;
1197 ipconfig->index = index;
1198 ipconfig->enabled = false;
1199 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1201 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1202 if (!ipconfig->address) {
1207 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1209 DBG("ipconfig %p", ipconfig);
1216 * connman_ipconfig_ref:
1217 * @ipconfig: ipconfig structure
1219 * Increase reference counter of ipconfig
1221 struct connman_ipconfig *
1222 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1223 const char *file, int line, const char *caller)
1225 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1226 file, line, caller);
1228 __sync_fetch_and_add(&ipconfig->refcount, 1);
1234 * connman_ipconfig_unref:
1235 * @ipconfig: ipconfig structure
1237 * Decrease reference counter of ipconfig
1239 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1240 const char *file, int line, const char *caller)
1245 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1246 file, line, caller);
1248 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1251 if (__connman_ipconfig_disable(ipconfig) < 0)
1252 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1254 __connman_ipconfig_set_ops(ipconfig, NULL);
1256 if (ipconfig->origin && ipconfig->origin != ipconfig) {
1257 __connman_ipconfig_unref(ipconfig->origin);
1258 ipconfig->origin = NULL;
1261 connman_ipaddress_free(ipconfig->system);
1262 connman_ipaddress_free(ipconfig->address);
1263 g_free(ipconfig->last_dhcp_address);
1264 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1269 * connman_ipconfig_get_data:
1270 * @ipconfig: ipconfig structure
1272 * Get private data pointer
1274 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1279 return ipconfig->ops_data;
1283 * connman_ipconfig_set_data:
1284 * @ipconfig: ipconfig structure
1285 * @data: data pointer
1287 * Set private data pointer
1289 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1291 ipconfig->ops_data = data;
1295 * connman_ipconfig_get_index:
1296 * @ipconfig: ipconfig structure
1298 * Get interface index
1300 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1305 if (ipconfig->origin)
1306 return ipconfig->origin->index;
1308 return ipconfig->index;
1312 * connman_ipconfig_set_ops:
1313 * @ipconfig: ipconfig structure
1314 * @ops: operation callbacks
1316 * Set the operation callbacks
1318 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1319 const struct connman_ipconfig_ops *ops)
1321 ipconfig->ops = ops;
1325 * connman_ipconfig_set_method:
1326 * @ipconfig: ipconfig structure
1327 * @method: configuration method
1329 * Set the configuration method
1331 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1332 enum connman_ipconfig_method method)
1334 ipconfig->method = method;
1339 enum connman_ipconfig_method __connman_ipconfig_get_method(
1340 struct connman_ipconfig *ipconfig)
1343 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1345 return ipconfig->method;
1348 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1352 switch (ipconfig->method) {
1353 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1354 case CONNMAN_IPCONFIG_METHOD_OFF:
1356 case CONNMAN_IPCONFIG_METHOD_AUTO:
1357 case CONNMAN_IPCONFIG_METHOD_FIXED:
1358 case CONNMAN_IPCONFIG_METHOD_DHCP:
1359 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1360 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1361 return connman_inet_set_address(ipconfig->index,
1363 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1364 return connman_inet_set_ipv6_address(
1365 ipconfig->index, ipconfig->address);
1371 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1380 DBG("method %d", ipconfig->method);
1382 switch (ipconfig->method) {
1383 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1384 case CONNMAN_IPCONFIG_METHOD_OFF:
1386 case CONNMAN_IPCONFIG_METHOD_AUTO:
1387 case CONNMAN_IPCONFIG_METHOD_FIXED:
1388 case CONNMAN_IPCONFIG_METHOD_DHCP:
1389 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1390 err = __connman_ipconfig_address_unset(ipconfig);
1391 connman_ipaddress_clear(ipconfig->address);
1399 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1408 DBG("method %d", ipconfig->method);
1410 switch (ipconfig->method) {
1411 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1412 case CONNMAN_IPCONFIG_METHOD_OFF:
1414 case CONNMAN_IPCONFIG_METHOD_AUTO:
1415 case CONNMAN_IPCONFIG_METHOD_FIXED:
1416 case CONNMAN_IPCONFIG_METHOD_DHCP:
1417 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1418 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1419 err = connman_inet_clear_address(ipconfig->index,
1421 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1422 err = connman_inet_clear_ipv6_address(
1424 ipconfig->address->local,
1425 ipconfig->address->prefixlen);
1435 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1438 struct connman_ipdevice *ipdevice;
1440 DBG("ipconfig %p", ipconfig);
1442 if (!ipconfig || ipconfig->index < 0)
1445 ipdevice = g_hash_table_lookup(ipdevice_hash,
1446 GINT_TO_POINTER(ipconfig->index));
1450 g_free(ipdevice->pac);
1451 ipdevice->pac = g_strdup(url);
1456 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1458 struct connman_ipdevice *ipdevice;
1460 DBG("ipconfig %p", ipconfig);
1462 if (!ipconfig || ipconfig->index < 0)
1465 ipdevice = g_hash_table_lookup(ipdevice_hash,
1466 GINT_TO_POINTER(ipconfig->index));
1470 return ipdevice->pac;
1473 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1474 const char *address)
1479 g_free(ipconfig->last_dhcp_address);
1480 ipconfig->last_dhcp_address = g_strdup(address);
1483 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1488 return ipconfig->last_dhcp_address;
1491 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1497 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1498 ipconfig->last_dhcpv6_prefixes = prefixes;
1501 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1506 return ipconfig->last_dhcpv6_prefixes;
1509 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1511 struct connman_ipdevice *ipdevice;
1516 ipdevice = g_hash_table_lookup(ipdevice_hash,
1517 GINT_TO_POINTER(ipconfig->index));
1521 ifname = connman_inet_ifname(ipconfig->index);
1523 set_ipv6_state(ifname, false);
1528 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1530 struct connman_ipdevice *ipdevice;
1535 ipdevice = g_hash_table_lookup(ipdevice_hash,
1536 GINT_TO_POINTER(ipconfig->index));
1540 ifname = connman_inet_ifname(ipconfig->index);
1542 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1543 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1545 set_ipv6_state(ifname, true);
1550 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1552 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1555 enable_ipv6(ipconfig);
1558 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1560 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1563 disable_ipv6(ipconfig);
1566 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1571 switch (ipconfig->method) {
1572 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1573 case CONNMAN_IPCONFIG_METHOD_OFF:
1575 case CONNMAN_IPCONFIG_METHOD_AUTO:
1576 case CONNMAN_IPCONFIG_METHOD_FIXED:
1577 case CONNMAN_IPCONFIG_METHOD_DHCP:
1578 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1585 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1587 struct connman_ipdevice *ipdevice;
1588 bool up = false, down = false;
1589 bool lower_up = false, lower_down = false;
1590 enum connman_ipconfig_type type;
1593 DBG("ipconfig %p", ipconfig);
1595 if (!ipconfig || ipconfig->index < 0)
1598 ipdevice = g_hash_table_lookup(ipdevice_hash,
1599 GINT_TO_POINTER(ipconfig->index));
1603 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1604 if (ipdevice->config_ipv4 == ipconfig)
1606 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1607 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1608 if (ipdevice->config_ipv6 == ipconfig)
1610 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1614 ipconfig->enabled = true;
1616 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1617 ipdevice->config_ipv4) {
1618 ipconfig_list = g_list_remove(ipconfig_list,
1619 ipdevice->config_ipv4);
1621 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1623 __connman_ipconfig_unref(ipdevice->config_ipv4);
1626 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1627 ipdevice->config_ipv6) {
1628 ipconfig_list = g_list_remove(ipconfig_list,
1629 ipdevice->config_ipv6);
1631 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1633 __connman_ipconfig_unref(ipdevice->config_ipv6);
1636 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1637 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1638 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1639 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1641 enable_ipv6(ipdevice->config_ipv6);
1643 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1645 if (ipdevice->flags & IFF_UP)
1650 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1651 (IFF_RUNNING | IFF_LOWER_UP))
1653 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1656 ifname = connman_inet_ifname(ipconfig->index);
1658 if (up && ipconfig->ops->up)
1659 ipconfig->ops->up(ipconfig, ifname);
1660 if (lower_up && ipconfig->ops->lower_up)
1661 ipconfig->ops->lower_up(ipconfig, ifname);
1663 if (lower_down && ipconfig->ops->lower_down)
1664 ipconfig->ops->lower_down(ipconfig, ifname);
1665 if (down && ipconfig->ops->down)
1666 ipconfig->ops->down(ipconfig, ifname);
1673 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1675 struct connman_ipdevice *ipdevice;
1677 DBG("ipconfig %p", ipconfig);
1679 if (!ipconfig || ipconfig->index < 0)
1682 ipdevice = g_hash_table_lookup(ipdevice_hash,
1683 GINT_TO_POINTER(ipconfig->index));
1687 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1690 ipconfig->enabled = false;
1692 if (ipdevice->config_ipv4 == ipconfig) {
1693 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1695 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1696 __connman_ipconfig_unref(ipdevice->config_ipv4);
1697 ipdevice->config_ipv4 = NULL;
1701 if (ipdevice->config_ipv6 == ipconfig) {
1702 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1704 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1705 __connman_ipconfig_unref(ipdevice->config_ipv6);
1706 ipdevice->config_ipv6 = NULL;
1713 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1716 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1718 case CONNMAN_IPCONFIG_METHOD_OFF:
1720 case CONNMAN_IPCONFIG_METHOD_FIXED:
1722 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1724 case CONNMAN_IPCONFIG_METHOD_DHCP:
1726 case CONNMAN_IPCONFIG_METHOD_AUTO:
1733 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1735 if (g_strcmp0(method, "off") == 0)
1736 return CONNMAN_IPCONFIG_METHOD_OFF;
1737 else if (g_strcmp0(method, "fixed") == 0)
1738 return CONNMAN_IPCONFIG_METHOD_FIXED;
1739 else if (g_strcmp0(method, "manual") == 0)
1740 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1741 else if (g_strcmp0(method, "dhcp") == 0)
1742 return CONNMAN_IPCONFIG_METHOD_DHCP;
1743 else if (g_strcmp0(method, "auto") == 0)
1744 return CONNMAN_IPCONFIG_METHOD_AUTO;
1746 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1749 static const char *privacy2string(int privacy)
1753 else if (privacy == 1)
1759 static int string2privacy(const char *privacy)
1761 if (g_strcmp0(privacy, "disabled") == 0)
1763 else if (g_strcmp0(privacy, "enabled") == 0)
1765 else if (g_strcmp0(privacy, "preferred") == 0)
1767 else if (g_strcmp0(privacy, "prefered") == 0)
1773 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1775 struct connman_ipdevice *ipdevice;
1781 ipdevice = g_hash_table_lookup(ipdevice_hash,
1782 GINT_TO_POINTER(ipconfig->index));
1786 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1787 ipdevice->ipv6_privacy));
1792 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1800 DBG("ipconfig %p privacy %s", ipconfig, value);
1802 privacy = string2privacy(value);
1804 ipconfig->ipv6_privacy_config = privacy;
1806 enable_ipv6(ipconfig);
1811 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1812 DBusMessageIter *iter)
1814 struct connman_ipaddress *append_addr = NULL;
1819 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1822 str = __connman_ipconfig_method2string(ipconfig->method);
1826 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1828 switch (ipconfig->method) {
1829 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1830 case CONNMAN_IPCONFIG_METHOD_OFF:
1831 case CONNMAN_IPCONFIG_METHOD_AUTO:
1834 case CONNMAN_IPCONFIG_METHOD_FIXED:
1835 append_addr = ipconfig->address;
1838 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1839 case CONNMAN_IPCONFIG_METHOD_DHCP:
1840 append_addr = ipconfig->system;
1847 if (append_addr->local) {
1849 struct in_addr netmask;
1852 connman_dbus_dict_append_basic(iter, "Address",
1853 DBUS_TYPE_STRING, &append_addr->local);
1855 addr = 0xffffffff << (32 - append_addr->prefixlen);
1856 netmask.s_addr = htonl(addr);
1857 mask = inet_ntoa(netmask);
1858 connman_dbus_dict_append_basic(iter, "Netmask",
1859 DBUS_TYPE_STRING, &mask);
1862 if (append_addr->gateway)
1863 connman_dbus_dict_append_basic(iter, "Gateway",
1864 DBUS_TYPE_STRING, &append_addr->gateway);
1867 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1868 DBusMessageIter *iter,
1869 struct connman_ipconfig *ipconfig_ipv4)
1871 struct connman_ipaddress *append_addr = NULL;
1872 const char *str, *privacy;
1876 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1879 str = __connman_ipconfig_method2string(ipconfig->method);
1883 if (ipconfig_ipv4 &&
1884 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1885 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1889 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1891 switch (ipconfig->method) {
1892 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1893 case CONNMAN_IPCONFIG_METHOD_OFF:
1896 case CONNMAN_IPCONFIG_METHOD_FIXED:
1897 append_addr = ipconfig->address;
1900 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1901 case CONNMAN_IPCONFIG_METHOD_DHCP:
1902 case CONNMAN_IPCONFIG_METHOD_AUTO:
1903 append_addr = ipconfig->system;
1910 if (append_addr->local) {
1911 connman_dbus_dict_append_basic(iter, "Address",
1912 DBUS_TYPE_STRING, &append_addr->local);
1913 connman_dbus_dict_append_basic(iter, "PrefixLength",
1915 &append_addr->prefixlen);
1918 if (append_addr->gateway)
1919 connman_dbus_dict_append_basic(iter, "Gateway",
1920 DBUS_TYPE_STRING, &append_addr->gateway);
1922 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1923 connman_dbus_dict_append_basic(iter, "Privacy",
1924 DBUS_TYPE_STRING, &privacy);
1927 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1928 DBusMessageIter *iter)
1930 const char *str, *privacy;
1934 str = __connman_ipconfig_method2string(ipconfig->method);
1938 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1940 switch (ipconfig->method) {
1941 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1942 case CONNMAN_IPCONFIG_METHOD_OFF:
1943 case CONNMAN_IPCONFIG_METHOD_DHCP:
1945 case CONNMAN_IPCONFIG_METHOD_FIXED:
1946 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1947 case CONNMAN_IPCONFIG_METHOD_AUTO:
1951 if (!ipconfig->address)
1954 if (ipconfig->address->local) {
1955 connman_dbus_dict_append_basic(iter, "Address",
1956 DBUS_TYPE_STRING, &ipconfig->address->local);
1957 connman_dbus_dict_append_basic(iter, "PrefixLength",
1959 &ipconfig->address->prefixlen);
1962 if (ipconfig->address->gateway)
1963 connman_dbus_dict_append_basic(iter, "Gateway",
1964 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1966 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1967 connman_dbus_dict_append_basic(iter, "Privacy",
1968 DBUS_TYPE_STRING, &privacy);
1971 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1972 DBusMessageIter *iter)
1978 str = __connman_ipconfig_method2string(ipconfig->method);
1982 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1984 switch (ipconfig->method) {
1985 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1986 case CONNMAN_IPCONFIG_METHOD_OFF:
1987 case CONNMAN_IPCONFIG_METHOD_DHCP:
1988 case CONNMAN_IPCONFIG_METHOD_AUTO:
1990 case CONNMAN_IPCONFIG_METHOD_FIXED:
1991 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1995 if (!ipconfig->address)
1998 if (ipconfig->address->local) {
2000 struct in_addr netmask;
2003 connman_dbus_dict_append_basic(iter, "Address",
2004 DBUS_TYPE_STRING, &ipconfig->address->local);
2006 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2007 netmask.s_addr = htonl(addr);
2008 mask = inet_ntoa(netmask);
2009 connman_dbus_dict_append_basic(iter, "Netmask",
2010 DBUS_TYPE_STRING, &mask);
2013 if (ipconfig->address->gateway)
2014 connman_dbus_dict_append_basic(iter, "Gateway",
2015 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2018 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2019 DBusMessageIter *array)
2021 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2022 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2023 *privacy_string = NULL;
2024 int prefix_length = 0, privacy = 0;
2025 DBusMessageIter dict;
2028 DBG("ipconfig %p", ipconfig);
2030 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2033 dbus_message_iter_recurse(array, &dict);
2035 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2036 DBusMessageIter entry, value;
2040 dbus_message_iter_recurse(&dict, &entry);
2042 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2045 dbus_message_iter_get_basic(&entry, &key);
2046 dbus_message_iter_next(&entry);
2048 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2051 dbus_message_iter_recurse(&entry, &value);
2053 type = dbus_message_iter_get_arg_type(&value);
2055 if (g_str_equal(key, "Method")) {
2058 if (type != DBUS_TYPE_STRING)
2061 dbus_message_iter_get_basic(&value, &str);
2062 method = __connman_ipconfig_string2method(str);
2063 } else if (g_str_equal(key, "Address")) {
2064 if (type != DBUS_TYPE_STRING)
2067 dbus_message_iter_get_basic(&value, &address);
2068 } else if (g_str_equal(key, "PrefixLength")) {
2069 if (type != DBUS_TYPE_BYTE)
2072 dbus_message_iter_get_basic(&value, &prefix_length);
2074 if (prefix_length < 0 || prefix_length > 128)
2076 } else if (g_str_equal(key, "Netmask")) {
2077 if (type != DBUS_TYPE_STRING)
2080 dbus_message_iter_get_basic(&value, &netmask);
2081 } else if (g_str_equal(key, "Gateway")) {
2082 if (type != DBUS_TYPE_STRING)
2085 dbus_message_iter_get_basic(&value, &gateway);
2086 } else if (g_str_equal(key, "Privacy")) {
2087 if (type != DBUS_TYPE_STRING)
2090 dbus_message_iter_get_basic(&value, &privacy_string);
2091 privacy = string2privacy(privacy_string);
2094 dbus_message_iter_next(&dict);
2097 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2099 method, address, netmask, gateway, prefix_length,
2103 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2104 case CONNMAN_IPCONFIG_METHOD_FIXED:
2107 case CONNMAN_IPCONFIG_METHOD_OFF:
2108 ipconfig->method = method;
2112 case CONNMAN_IPCONFIG_METHOD_AUTO:
2113 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2116 ipconfig->method = method;
2118 ipconfig->ipv6_privacy_config = privacy;
2122 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2123 switch (ipconfig->type) {
2124 case CONNMAN_IPCONFIG_TYPE_IPV4:
2127 case CONNMAN_IPCONFIG_TYPE_IPV6:
2130 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2135 if ((address && connman_inet_check_ipaddress(address)
2138 connman_inet_check_ipaddress(netmask)
2141 connman_inet_check_ipaddress(gateway)
2145 ipconfig->method = method;
2147 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2148 connman_ipaddress_set_ipv4(ipconfig->address,
2149 address, netmask, gateway);
2151 return connman_ipaddress_set_ipv6(
2152 ipconfig->address, address,
2153 prefix_length, gateway);
2157 case CONNMAN_IPCONFIG_METHOD_DHCP:
2158 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2161 ipconfig->method = method;
2168 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2169 DBusMessageIter *iter)
2171 struct connman_ipdevice *ipdevice;
2172 const char *method = "auto";
2174 connman_dbus_dict_append_basic(iter, "Method",
2175 DBUS_TYPE_STRING, &method);
2177 ipdevice = g_hash_table_lookup(ipdevice_hash,
2178 GINT_TO_POINTER(ipconfig->index));
2182 if (ipconfig->index >= 0) {
2183 char *ifname = connman_inet_ifname(ipconfig->index);
2185 connman_dbus_dict_append_basic(iter, "Interface",
2186 DBUS_TYPE_STRING, &ifname);
2191 if (ipdevice->address)
2192 connman_dbus_dict_append_basic(iter, "Address",
2193 DBUS_TYPE_STRING, &ipdevice->address);
2195 if (ipdevice->mtu > 0)
2196 connman_dbus_dict_append_basic(iter, "MTU",
2197 DBUS_TYPE_UINT16, &ipdevice->mtu);
2200 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2201 GKeyFile *keyfile, const char *identifier, const char *prefix)
2207 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2209 key = g_strdup_printf("%smethod", prefix);
2210 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2212 switch (ipconfig->type) {
2213 case CONNMAN_IPCONFIG_TYPE_IPV4:
2214 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2216 case CONNMAN_IPCONFIG_TYPE_IPV6:
2217 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2219 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2220 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2224 ipconfig->method = __connman_ipconfig_string2method(method);
2226 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2227 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2229 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2233 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2234 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2237 pprefix = g_strdup_printf("%sprivacy", prefix);
2238 privacy = g_key_file_get_string(keyfile, identifier,
2240 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2245 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2246 ipconfig->last_dhcpv6_prefixes =
2247 g_key_file_get_string_list(keyfile, identifier, pprefix,
2249 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2250 g_free(ipconfig->last_dhcpv6_prefixes);
2251 ipconfig->last_dhcpv6_prefixes = NULL;
2259 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2260 ipconfig->address->prefixlen = g_key_file_get_integer(
2261 keyfile, identifier, key, NULL);
2264 key = g_strdup_printf("%slocal_address", prefix);
2265 g_free(ipconfig->address->local);
2266 ipconfig->address->local = g_key_file_get_string(
2267 keyfile, identifier, key, NULL);
2270 key = g_strdup_printf("%speer_address", prefix);
2271 g_free(ipconfig->address->peer);
2272 ipconfig->address->peer = g_key_file_get_string(
2273 keyfile, identifier, key, NULL);
2276 key = g_strdup_printf("%sbroadcast_address", prefix);
2277 g_free(ipconfig->address->broadcast);
2278 ipconfig->address->broadcast = g_key_file_get_string(
2279 keyfile, identifier, key, NULL);
2282 key = g_strdup_printf("%sgateway", prefix);
2283 g_free(ipconfig->address->gateway);
2284 ipconfig->address->gateway = g_key_file_get_string(
2285 keyfile, identifier, key, NULL);
2288 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2289 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2291 g_free(ipconfig->last_dhcp_address);
2292 ipconfig->last_dhcp_address = str;
2299 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2300 GKeyFile *keyfile, const char *identifier, const char *prefix)
2305 method = __connman_ipconfig_method2string(ipconfig->method);
2307 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2310 key = g_strdup_printf("%smethod", prefix);
2311 g_key_file_set_string(keyfile, identifier, key, method);
2315 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2316 const char *privacy;
2317 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2318 key = g_strdup_printf("%sprivacy", prefix);
2319 g_key_file_set_string(keyfile, identifier, key, privacy);
2322 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2323 if (ipconfig->last_dhcp_address &&
2324 strlen(ipconfig->last_dhcp_address) > 0)
2325 g_key_file_set_string(keyfile, identifier, key,
2326 ipconfig->last_dhcp_address);
2328 g_key_file_remove_key(keyfile, identifier, key, NULL);
2331 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2332 if (ipconfig->last_dhcpv6_prefixes &&
2333 ipconfig->last_dhcpv6_prefixes[0]) {
2335 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2337 g_key_file_set_string_list(keyfile, identifier, key,
2338 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2341 g_key_file_remove_key(keyfile, identifier, key, NULL);
2345 switch (ipconfig->method) {
2346 case CONNMAN_IPCONFIG_METHOD_FIXED:
2347 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2349 case CONNMAN_IPCONFIG_METHOD_DHCP:
2350 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2351 if (ipconfig->last_dhcp_address &&
2352 strlen(ipconfig->last_dhcp_address) > 0)
2353 g_key_file_set_string(keyfile, identifier, key,
2354 ipconfig->last_dhcp_address);
2356 g_key_file_remove_key(keyfile, identifier, key, NULL);
2359 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2360 case CONNMAN_IPCONFIG_METHOD_OFF:
2361 case CONNMAN_IPCONFIG_METHOD_AUTO:
2365 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2366 if (ipconfig->address->prefixlen != 0)
2367 g_key_file_set_integer(keyfile, identifier,
2368 key, ipconfig->address->prefixlen);
2371 key = g_strdup_printf("%slocal_address", prefix);
2372 if (ipconfig->address->local)
2373 g_key_file_set_string(keyfile, identifier,
2374 key, ipconfig->address->local);
2377 key = g_strdup_printf("%speer_address", prefix);
2378 if (ipconfig->address->peer)
2379 g_key_file_set_string(keyfile, identifier,
2380 key, ipconfig->address->peer);
2383 key = g_strdup_printf("%sbroadcast_address", prefix);
2384 if (ipconfig->address->broadcast)
2385 g_key_file_set_string(keyfile, identifier,
2386 key, ipconfig->address->broadcast);
2389 key = g_strdup_printf("%sgateway", prefix);
2390 if (ipconfig->address->gateway)
2391 g_key_file_set_string(keyfile, identifier,
2392 key, ipconfig->address->gateway);
2398 int __connman_ipconfig_init(void)
2402 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2403 NULL, free_ipdevice);
2405 is_ipv6_supported = connman_inet_is_ipv6_supported();
2410 void __connman_ipconfig_cleanup(void)
2414 g_hash_table_destroy(ipdevice_hash);
2415 ipdevice_hash = NULL;