5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <net/if_arp.h>
30 #include <linux/if_link.h>
35 #define IFF_LOWER_UP 0x10000
42 struct connman_ipconfig {
45 enum connman_ipconfig_type type;
47 struct connman_ipconfig *origin;
49 const struct connman_ipconfig_ops *ops;
52 connman_bool_t enabled;
53 enum connman_ipconfig_method method;
54 struct connman_ipaddress *address;
55 struct connman_ipaddress *system;
57 int ipv6_privacy_config;
58 char *last_dhcp_address;
61 struct connman_ipdevice {
83 struct connman_ipconfig *config_ipv4;
84 struct connman_ipconfig *config_ipv6;
86 gboolean ipv6_enabled;
90 static GHashTable *ipdevice_hash = NULL;
91 static GList *ipconfig_list = NULL;
93 struct connman_ipaddress *connman_ipaddress_alloc(int family)
95 struct connman_ipaddress *ipaddress;
97 ipaddress = g_try_new0(struct connman_ipaddress, 1);
98 if (ipaddress == NULL)
101 ipaddress->family = family;
102 ipaddress->prefixlen = 0;
103 ipaddress->local = NULL;
104 ipaddress->peer = NULL;
105 ipaddress->broadcast = NULL;
106 ipaddress->gateway = NULL;
111 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
113 if (ipaddress == NULL)
116 g_free(ipaddress->broadcast);
117 g_free(ipaddress->peer);
118 g_free(ipaddress->local);
119 g_free(ipaddress->gateway);
123 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
132 mask = inet_network(netmask);
135 /* a valid netmask must be 2^n - 1 */
136 if ((host & (host + 1)) != 0)
140 for (; mask; mask <<= 1)
146 static gboolean check_ipv6_address(const char *address)
148 unsigned char buf[sizeof(struct in6_addr)];
154 err = inet_pton(AF_INET6, address, buf);
161 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
163 unsigned char prefix_length,
166 if (ipaddress == NULL)
169 if (check_ipv6_address(address) == FALSE)
172 DBG("prefix_len %d address %s gateway %s",
173 prefix_length, address, gateway);
175 ipaddress->family = AF_INET6;
177 ipaddress->prefixlen = prefix_length;
179 g_free(ipaddress->local);
180 ipaddress->local = g_strdup(address);
182 g_free(ipaddress->gateway);
183 ipaddress->gateway = g_strdup(gateway);
188 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
189 const char *address, const char *netmask, const char *gateway)
191 if (ipaddress == NULL)
194 ipaddress->family = AF_INET;
196 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
198 g_free(ipaddress->local);
199 ipaddress->local = g_strdup(address);
201 g_free(ipaddress->gateway);
202 ipaddress->gateway = g_strdup(gateway);
207 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
210 if (ipaddress == NULL)
213 g_free(ipaddress->peer);
214 ipaddress->peer = g_strdup(peer);
217 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
219 if (ipaddress == NULL)
222 ipaddress->prefixlen = 0;
224 g_free(ipaddress->local);
225 ipaddress->local = NULL;
227 g_free(ipaddress->peer);
228 ipaddress->peer = NULL;
230 g_free(ipaddress->broadcast);
231 ipaddress->broadcast = NULL;
233 g_free(ipaddress->gateway);
234 ipaddress->gateway = NULL;
237 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
238 struct connman_ipaddress *source)
240 if (ipaddress == NULL || source == NULL)
243 ipaddress->family = source->family;
244 ipaddress->prefixlen = source->prefixlen;
246 g_free(ipaddress->local);
247 ipaddress->local = g_strdup(source->local);
249 g_free(ipaddress->peer);
250 ipaddress->peer = g_strdup(source->peer);
252 g_free(ipaddress->broadcast);
253 ipaddress->broadcast = g_strdup(source->broadcast);
255 g_free(ipaddress->gateway);
256 ipaddress->gateway = g_strdup(source->gateway);
259 static void free_address_list(struct connman_ipdevice *ipdevice)
263 for (list = ipdevice->address_list; list; list = list->next) {
264 struct connman_ipaddress *ipaddress = list->data;
266 connman_ipaddress_free(ipaddress);
270 g_slist_free(ipdevice->address_list);
271 ipdevice->address_list = NULL;
274 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
275 unsigned char prefixlen, const char *local)
279 for (list = ipdevice->address_list; list; list = list->next) {
280 struct connman_ipaddress *ipaddress = list->data;
282 if (g_strcmp0(ipaddress->local, local) == 0 &&
283 ipaddress->prefixlen == prefixlen)
290 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
293 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
295 case CONNMAN_IPCONFIG_TYPE_IPV4:
297 case CONNMAN_IPCONFIG_TYPE_IPV6:
304 static const char *type2str(unsigned short type)
309 case ARPHRD_LOOPBACK:
322 static const char *scope2str(unsigned char scope)
334 static gboolean get_ipv6_state(gchar *ifname)
339 gboolean enabled = FALSE;
342 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
344 path = g_strdup_printf(
345 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
350 f = fopen(path, "r");
355 if (fscanf(f, "%d", &disabled) > 0)
363 static void set_ipv6_state(gchar *ifname, gboolean enable)
369 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
371 path = g_strdup_printf(
372 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
377 f = fopen(path, "r+");
392 static int get_ipv6_privacy(gchar *ifname)
401 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
407 f = fopen(path, "r");
414 if (fscanf(f, "%d", &value) <= 0)
422 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
423 * The privacy extension is described in RFC 3041 and RFC 4941
425 static void set_ipv6_privacy(gchar *ifname, int value)
433 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
442 f = fopen(path, "r+");
449 fprintf(f, "%d", value);
453 static int get_rp_filter()
456 int value = -EINVAL, tmp;
458 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
461 if (fscanf(f, "%d", &tmp) == 1)
469 static void set_rp_filter(int value)
473 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
478 fprintf(f, "%d", value);
483 int __connman_ipconfig_set_rp_filter()
487 value = get_rp_filter();
494 connman_info("rp_filter set to 2 (loose mode routing), "
495 "old value was %d", value);
500 void __connman_ipconfig_unset_rp_filter(int old_value)
502 set_rp_filter(old_value);
504 connman_info("rp_filter restored to %d", old_value);
507 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
509 if (ipconfig == NULL)
512 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
515 static void free_ipdevice(gpointer data)
517 struct connman_ipdevice *ipdevice = data;
519 connman_info("%s {remove} index %d", ipdevice->ifname,
522 if (ipdevice->config_ipv4 != NULL) {
523 __connman_ipconfig_unref(ipdevice->config_ipv4);
524 ipdevice->config_ipv4 = NULL;
527 if (ipdevice->config_ipv6 != NULL) {
528 __connman_ipconfig_unref(ipdevice->config_ipv6);
529 ipdevice->config_ipv6 = NULL;
532 free_address_list(ipdevice);
533 g_free(ipdevice->ipv4_gateway);
534 g_free(ipdevice->ipv6_gateway);
535 g_free(ipdevice->pac);
537 g_free(ipdevice->address);
539 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
540 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
542 g_free(ipdevice->ifname);
546 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
548 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
549 ipdevice->config_ipv6);
551 if (ipdevice->config_ipv6 != NULL &&
552 ipdevice->config_ipv6->enabled == TRUE)
555 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
556 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
557 set_ipv6_state(ipdevice->ifname, FALSE);
561 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
563 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
564 ipdevice->config_ipv6);
566 if (ipdevice->config_ipv4)
567 connman_inet_clear_address(ipdevice->index,
568 ipdevice->config_ipv4->address);
570 if (ipdevice->config_ipv6)
571 connman_inet_clear_ipv6_address(ipdevice->index,
572 ipdevice->config_ipv6->address->local,
573 ipdevice->config_ipv6->address->prefixlen);
576 static void update_stats(struct connman_ipdevice *ipdevice,
577 struct rtnl_link_stats *stats)
579 struct connman_service *service;
581 if (stats->rx_packets == 0 && stats->tx_packets == 0)
584 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
585 stats->rx_packets, stats->rx_bytes);
586 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
587 stats->tx_packets, stats->tx_bytes);
589 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
592 if (ipdevice->config_ipv4)
593 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
594 else if (ipdevice->config_ipv6)
595 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
602 ipdevice->rx_packets = stats->rx_packets;
603 ipdevice->tx_packets = stats->tx_packets;
604 ipdevice->rx_bytes = stats->rx_bytes;
605 ipdevice->tx_bytes = stats->tx_bytes;
606 ipdevice->rx_errors = stats->rx_errors;
607 ipdevice->tx_errors = stats->tx_errors;
608 ipdevice->rx_dropped = stats->rx_dropped;
609 ipdevice->tx_dropped = stats->tx_dropped;
611 __connman_service_notify(service,
612 ipdevice->rx_packets, ipdevice->tx_packets,
613 ipdevice->rx_bytes, ipdevice->tx_bytes,
614 ipdevice->rx_errors, ipdevice->tx_errors,
615 ipdevice->rx_dropped, ipdevice->tx_dropped);
618 void __connman_ipconfig_newlink(int index, unsigned short type,
619 unsigned int flags, const char *address,
621 struct rtnl_link_stats *stats)
623 struct connman_ipdevice *ipdevice;
626 gboolean up = FALSE, down = FALSE;
627 gboolean lower_up = FALSE, lower_down = FALSE;
629 DBG("index %d", index);
631 if (type == ARPHRD_LOOPBACK)
634 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
635 if (ipdevice != NULL) {
636 char *ifname = connman_inet_ifname(index);
637 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
638 DBG("interface name changed %s -> %s",
639 ipdevice->ifname, ifname);
641 g_free(ipdevice->ifname);
642 ipdevice->ifname = ifname;
649 ipdevice = g_try_new0(struct connman_ipdevice, 1);
650 if (ipdevice == NULL)
653 ipdevice->index = index;
654 ipdevice->ifname = connman_inet_ifname(index);
655 ipdevice->type = type;
657 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
658 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
660 ipdevice->address = g_strdup(address);
662 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
664 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
665 index, type, type2str(type));
670 update_stats(ipdevice, stats);
672 if (flags == ipdevice->flags)
675 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
682 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
683 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
684 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
685 (IFF_RUNNING | IFF_LOWER_UP))
687 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
691 ipdevice->flags = flags;
693 str = g_string_new(NULL);
698 g_string_append(str, "UP");
700 g_string_append(str, "DOWN");
702 if (flags & IFF_RUNNING)
703 g_string_append(str, ",RUNNING");
705 if (flags & IFF_LOWER_UP)
706 g_string_append(str, ",LOWER_UP");
708 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
711 g_string_free(str, TRUE);
713 for (list = g_list_first(ipconfig_list); list;
714 list = g_list_next(list)) {
715 struct connman_ipconfig *ipconfig = list->data;
717 if (index != ipconfig->index)
720 if (ipconfig->ops == NULL)
723 if (up == TRUE && ipconfig->ops->up)
724 ipconfig->ops->up(ipconfig);
725 if (lower_up == TRUE && ipconfig->ops->lower_up)
726 ipconfig->ops->lower_up(ipconfig);
728 if (lower_down == TRUE && ipconfig->ops->lower_down)
729 ipconfig->ops->lower_down(ipconfig);
730 if (down == TRUE && ipconfig->ops->down)
731 ipconfig->ops->down(ipconfig);
735 __connman_ipconfig_lower_up(ipdevice);
737 __connman_ipconfig_lower_down(ipdevice);
740 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
742 struct connman_ipdevice *ipdevice;
745 DBG("index %d", index);
747 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
748 if (ipdevice == NULL)
751 update_stats(ipdevice, stats);
753 for (list = g_list_first(ipconfig_list); list;
754 list = g_list_next(list)) {
755 struct connman_ipconfig *ipconfig = list->data;
757 if (index != ipconfig->index)
760 ipconfig->index = -1;
762 if (ipconfig->ops == NULL)
765 if (ipconfig->ops->lower_down)
766 ipconfig->ops->lower_down(ipconfig);
767 if (ipconfig->ops->down)
768 ipconfig->ops->down(ipconfig);
771 __connman_ipconfig_lower_down(ipdevice);
773 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
776 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
778 const struct connman_ipaddress *addr1 = a;
779 const struct connman_ipaddress *addr2 = b;
781 if (addr1->prefixlen != addr2->prefixlen)
782 return addr2->prefixlen - addr1->prefixlen;
784 return g_strcmp0(addr1->local, addr2->local);
787 void __connman_ipconfig_newaddr(int index, int family, const char *label,
788 unsigned char prefixlen, const char *address)
790 struct connman_ipdevice *ipdevice;
791 struct connman_ipaddress *ipaddress;
792 enum connman_ipconfig_type type;
795 DBG("index %d", index);
797 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
798 if (ipdevice == NULL)
801 ipaddress = connman_ipaddress_alloc(family);
802 if (ipaddress == NULL)
805 ipaddress->prefixlen = prefixlen;
806 ipaddress->local = g_strdup(address);
808 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
809 check_duplicate_address)) {
810 connman_ipaddress_free(ipaddress);
814 if (family == AF_INET)
815 type = CONNMAN_IPCONFIG_TYPE_IPV4;
816 else if (family == AF_INET6)
817 type = CONNMAN_IPCONFIG_TYPE_IPV6;
821 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
824 connman_info("%s {add} address %s/%u label %s family %d",
825 ipdevice->ifname, address, prefixlen, label, family);
827 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
828 __connman_ippool_newaddr(index, address, prefixlen);
830 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
831 connman_ipaddress_copy(ipdevice->config_ipv4->system,
834 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
835 connman_ipaddress_copy(ipdevice->config_ipv6->system,
840 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
843 for (list = g_list_first(ipconfig_list); list;
844 list = g_list_next(list)) {
845 struct connman_ipconfig *ipconfig = list->data;
847 if (index != ipconfig->index)
850 if (type != ipconfig->type)
853 if (ipconfig->ops == NULL)
856 if (ipconfig->ops->ip_bound)
857 ipconfig->ops->ip_bound(ipconfig);
861 void __connman_ipconfig_deladdr(int index, int family, const char *label,
862 unsigned char prefixlen, const char *address)
864 struct connman_ipdevice *ipdevice;
865 struct connman_ipaddress *ipaddress;
866 enum connman_ipconfig_type type;
869 DBG("index %d", index);
871 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
872 if (ipdevice == NULL)
875 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
876 if (ipaddress == NULL)
879 if (family == AF_INET)
880 type = CONNMAN_IPCONFIG_TYPE_IPV4;
881 else if (family == AF_INET6)
882 type = CONNMAN_IPCONFIG_TYPE_IPV6;
886 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
889 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
890 __connman_ippool_deladdr(index, address, prefixlen);
892 connman_ipaddress_clear(ipaddress);
895 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
896 address, prefixlen, label);
898 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
901 if (g_slist_length(ipdevice->address_list) > 0)
904 for (list = g_list_first(ipconfig_list); list;
905 list = g_list_next(list)) {
906 struct connman_ipconfig *ipconfig = list->data;
908 if (index != ipconfig->index)
911 if (type != ipconfig->type)
914 if (ipconfig->ops == NULL)
917 if (ipconfig->ops->ip_release)
918 ipconfig->ops->ip_release(ipconfig);
922 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
923 const char *dst, const char *gateway)
925 struct connman_ipdevice *ipdevice;
927 DBG("index %d", index);
929 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
930 if (ipdevice == NULL)
933 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
934 g_strcmp0(dst, "::") == 0)) {
936 enum connman_ipconfig_type type;
938 if (family == AF_INET6) {
939 type = CONNMAN_IPCONFIG_TYPE_IPV6;
940 g_free(ipdevice->ipv6_gateway);
941 ipdevice->ipv6_gateway = g_strdup(gateway);
943 if (ipdevice->config_ipv6 != NULL &&
944 ipdevice->config_ipv6->system != NULL) {
945 g_free(ipdevice->config_ipv6->system->gateway);
946 ipdevice->config_ipv6->system->gateway =
949 } else if (family == AF_INET) {
950 type = CONNMAN_IPCONFIG_TYPE_IPV4;
951 g_free(ipdevice->ipv4_gateway);
952 ipdevice->ipv4_gateway = g_strdup(gateway);
954 if (ipdevice->config_ipv4 != NULL &&
955 ipdevice->config_ipv4->system != NULL) {
956 g_free(ipdevice->config_ipv4->system->gateway);
957 ipdevice->config_ipv4->system->gateway =
963 for (config_list = g_list_first(ipconfig_list); config_list;
964 config_list = g_list_next(config_list)) {
965 struct connman_ipconfig *ipconfig = config_list->data;
967 if (index != ipconfig->index)
970 if (type != ipconfig->type)
973 if (ipconfig->ops == NULL)
976 if (ipconfig->ops->route_set)
977 ipconfig->ops->route_set(ipconfig);
981 connman_info("%s {add} route %s gw %s scope %u <%s>",
982 ipdevice->ifname, dst, gateway,
983 scope, scope2str(scope));
986 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
987 const char *dst, const char *gateway)
989 struct connman_ipdevice *ipdevice;
991 DBG("index %d", index);
993 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
994 if (ipdevice == NULL)
997 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
998 g_strcmp0(dst, "::") == 0)) {
1000 enum connman_ipconfig_type type;
1002 if (family == AF_INET6) {
1003 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1004 g_free(ipdevice->ipv6_gateway);
1005 ipdevice->ipv6_gateway = NULL;
1007 if (ipdevice->config_ipv6 != NULL &&
1008 ipdevice->config_ipv6->system != NULL) {
1009 g_free(ipdevice->config_ipv6->system->gateway);
1010 ipdevice->config_ipv6->system->gateway = NULL;
1012 } else if (family == AF_INET) {
1013 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1014 g_free(ipdevice->ipv4_gateway);
1015 ipdevice->ipv4_gateway = NULL;
1017 if (ipdevice->config_ipv4 != NULL &&
1018 ipdevice->config_ipv4->system != NULL) {
1019 g_free(ipdevice->config_ipv4->system->gateway);
1020 ipdevice->config_ipv4->system->gateway = NULL;
1025 for (config_list = g_list_first(ipconfig_list); config_list;
1026 config_list = g_list_next(config_list)) {
1027 struct connman_ipconfig *ipconfig = config_list->data;
1029 if (index != ipconfig->index)
1032 if (type != ipconfig->type)
1035 if (ipconfig->ops == NULL)
1038 if (ipconfig->ops->route_unset)
1039 ipconfig->ops->route_unset(ipconfig);
1043 connman_info("%s {del} route %s gw %s scope %u <%s>",
1044 ipdevice->ifname, dst, gateway,
1045 scope, scope2str(scope));
1048 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1053 keys = g_hash_table_get_keys(ipdevice_hash);
1057 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1058 int index = GPOINTER_TO_INT(list->data);
1060 function(index, user_data);
1066 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1067 struct connman_ipconfig *ipconfig)
1069 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1072 unsigned short __connman_ipconfig_get_type_from_index(int index)
1074 struct connman_ipdevice *ipdevice;
1076 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1077 if (ipdevice == NULL)
1080 return ipdevice->type;
1083 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1085 struct connman_ipdevice *ipdevice;
1087 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1088 if (ipdevice == NULL)
1091 return ipdevice->flags;
1094 const char *__connman_ipconfig_get_gateway_from_index(int index,
1095 enum connman_ipconfig_type type)
1097 struct connman_ipdevice *ipdevice;
1099 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1100 if (ipdevice == NULL)
1103 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1104 if (ipdevice->ipv4_gateway != NULL)
1105 return ipdevice->ipv4_gateway;
1107 if (ipdevice->config_ipv4 != NULL &&
1108 ipdevice->config_ipv4->address != NULL)
1109 return ipdevice->config_ipv4->address->gateway;
1112 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1113 if (ipdevice->ipv6_gateway != NULL)
1114 return ipdevice->ipv6_gateway;
1116 if (ipdevice->config_ipv6 != NULL &&
1117 ipdevice->config_ipv6->address != NULL)
1118 return ipdevice->config_ipv6->address->gateway;
1124 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1126 ipconfig->index = index;
1129 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1131 if (ipconfig->address == NULL)
1134 return ipconfig->address->local;
1137 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1139 if (ipconfig->address == NULL)
1142 g_free(ipconfig->address->local);
1143 ipconfig->address->local = g_strdup(address);
1146 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1148 if (ipconfig->address == NULL)
1151 return ipconfig->address->peer;
1154 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1156 if (ipconfig->address == NULL)
1159 g_free(ipconfig->address->peer);
1160 ipconfig->address->peer = g_strdup(address);
1163 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1165 if (ipconfig->address == NULL)
1168 return ipconfig->address->broadcast;
1171 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1173 if (ipconfig->address == NULL)
1176 g_free(ipconfig->address->broadcast);
1177 ipconfig->address->broadcast = g_strdup(broadcast);
1180 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1182 if (ipconfig->address == NULL)
1185 return ipconfig->address->gateway;
1188 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1192 if (ipconfig->address == NULL)
1194 g_free(ipconfig->address->gateway);
1195 ipconfig->address->gateway = g_strdup(gateway);
1198 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1200 struct connman_service *service;
1204 if (ipconfig->address == NULL)
1207 service = __connman_service_lookup_from_index(ipconfig->index);
1208 if (service == NULL)
1211 __connman_connection_gateway_remove(service, ipconfig->type);
1213 DBG("type %d gw %s peer %s", ipconfig->type,
1214 ipconfig->address->gateway, ipconfig->address->peer);
1216 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1217 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1218 return __connman_connection_gateway_add(service,
1219 ipconfig->address->gateway,
1221 ipconfig->address->peer);
1226 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1228 struct connman_service *service;
1232 service = __connman_service_lookup_from_index(ipconfig->index);
1233 if (service != NULL)
1234 __connman_connection_gateway_remove(service, ipconfig->type);
1237 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1239 if (ipconfig->address == NULL)
1242 return ipconfig->address->prefixlen;
1245 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1247 if (ipconfig->address == NULL)
1250 ipconfig->address->prefixlen = prefixlen;
1253 static struct connman_ipconfig *create_ipv6config(int index)
1255 struct connman_ipconfig *ipv6config;
1256 struct connman_ipdevice *ipdevice;
1258 DBG("index %d", index);
1260 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1261 if (ipv6config == NULL)
1264 ipv6config->refcount = 1;
1266 ipv6config->index = index;
1267 ipv6config->enabled = FALSE;
1268 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1269 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1271 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1272 if (ipdevice != NULL)
1273 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1275 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1276 if (ipv6config->address == NULL) {
1281 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1283 DBG("ipconfig %p", ipv6config);
1289 * connman_ipconfig_create:
1291 * Allocate a new ipconfig structure.
1293 * Returns: a newly-allocated #connman_ipconfig structure
1295 struct connman_ipconfig *__connman_ipconfig_create(int index,
1296 enum connman_ipconfig_type type)
1298 struct connman_ipconfig *ipconfig;
1300 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1301 return create_ipv6config(index);
1303 DBG("index %d", index);
1305 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1306 if (ipconfig == NULL)
1309 ipconfig->refcount = 1;
1311 ipconfig->index = index;
1312 ipconfig->enabled = FALSE;
1313 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1315 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1316 if (ipconfig->address == NULL) {
1321 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1323 DBG("ipconfig %p", ipconfig);
1330 * connman_ipconfig_ref:
1331 * @ipconfig: ipconfig structure
1333 * Increase reference counter of ipconfig
1335 struct connman_ipconfig *
1336 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1337 const char *file, int line, const char *caller)
1339 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1340 file, line, caller);
1342 __sync_fetch_and_add(&ipconfig->refcount, 1);
1348 * connman_ipconfig_unref:
1349 * @ipconfig: ipconfig structure
1351 * Decrease reference counter of ipconfig
1353 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1354 const char *file, int line, const char *caller)
1356 if (ipconfig == NULL)
1359 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1360 file, line, caller);
1362 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1365 if (__connman_ipconfig_disable(ipconfig) < 0)
1366 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1368 __connman_ipconfig_set_ops(ipconfig, NULL);
1370 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1371 __connman_ipconfig_unref(ipconfig->origin);
1372 ipconfig->origin = NULL;
1375 connman_ipaddress_free(ipconfig->system);
1376 connman_ipaddress_free(ipconfig->address);
1377 g_free(ipconfig->last_dhcp_address);
1382 * connman_ipconfig_get_data:
1383 * @ipconfig: ipconfig structure
1385 * Get private data pointer
1387 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1389 if (ipconfig == NULL)
1392 return ipconfig->ops_data;
1396 * connman_ipconfig_set_data:
1397 * @ipconfig: ipconfig structure
1398 * @data: data pointer
1400 * Set private data pointer
1402 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1404 ipconfig->ops_data = data;
1408 * connman_ipconfig_get_index:
1409 * @ipconfig: ipconfig structure
1411 * Get interface index
1413 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1415 if (ipconfig == NULL)
1418 if (ipconfig->origin != NULL)
1419 return ipconfig->origin->index;
1421 return ipconfig->index;
1425 * connman_ipconfig_get_ifname:
1426 * @ipconfig: ipconfig structure
1428 * Get interface name
1430 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1432 struct connman_ipdevice *ipdevice;
1434 if (ipconfig == NULL)
1437 if (ipconfig->index < 0)
1440 ipdevice = g_hash_table_lookup(ipdevice_hash,
1441 GINT_TO_POINTER(ipconfig->index));
1442 if (ipdevice == NULL)
1445 return ipdevice->ifname;
1449 * connman_ipconfig_set_ops:
1450 * @ipconfig: ipconfig structure
1451 * @ops: operation callbacks
1453 * Set the operation callbacks
1455 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1456 const struct connman_ipconfig_ops *ops)
1458 ipconfig->ops = ops;
1462 * connman_ipconfig_set_method:
1463 * @ipconfig: ipconfig structure
1464 * @method: configuration method
1466 * Set the configuration method
1468 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1469 enum connman_ipconfig_method method)
1471 ipconfig->method = method;
1476 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1478 if (ipconfig == NULL)
1479 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1481 return ipconfig->method;
1484 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1488 switch (ipconfig->method) {
1489 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1490 case CONNMAN_IPCONFIG_METHOD_OFF:
1492 case CONNMAN_IPCONFIG_METHOD_AUTO:
1493 case CONNMAN_IPCONFIG_METHOD_FIXED:
1494 case CONNMAN_IPCONFIG_METHOD_DHCP:
1495 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1496 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1497 return connman_inet_set_address(ipconfig->index,
1499 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1500 return connman_inet_set_ipv6_address(
1501 ipconfig->index, ipconfig->address);
1507 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1513 if (ipconfig == NULL)
1516 DBG("method %d", ipconfig->method);
1518 switch (ipconfig->method) {
1519 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1520 case CONNMAN_IPCONFIG_METHOD_OFF:
1522 case CONNMAN_IPCONFIG_METHOD_AUTO:
1523 case CONNMAN_IPCONFIG_METHOD_FIXED:
1524 case CONNMAN_IPCONFIG_METHOD_DHCP:
1525 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1526 err = __connman_ipconfig_address_unset(ipconfig);
1527 connman_ipaddress_clear(ipconfig->address);
1535 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1541 if (ipconfig == NULL)
1544 DBG("method %d", ipconfig->method);
1546 switch (ipconfig->method) {
1547 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1548 case CONNMAN_IPCONFIG_METHOD_OFF:
1550 case CONNMAN_IPCONFIG_METHOD_AUTO:
1551 case CONNMAN_IPCONFIG_METHOD_FIXED:
1552 case CONNMAN_IPCONFIG_METHOD_DHCP:
1553 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1554 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1555 err = connman_inet_clear_address(ipconfig->index,
1557 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1558 err = connman_inet_clear_ipv6_address(
1560 ipconfig->address->local,
1561 ipconfig->address->prefixlen);
1571 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1574 struct connman_ipdevice *ipdevice;
1576 DBG("ipconfig %p", ipconfig);
1578 if (ipconfig == NULL || ipconfig->index < 0)
1581 ipdevice = g_hash_table_lookup(ipdevice_hash,
1582 GINT_TO_POINTER(ipconfig->index));
1583 if (ipdevice == NULL)
1586 g_free(ipdevice->pac);
1587 ipdevice->pac = g_strdup(url);
1592 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1594 struct connman_ipdevice *ipdevice;
1596 DBG("ipconfig %p", ipconfig);
1598 if (ipconfig == NULL || ipconfig->index < 0)
1601 ipdevice = g_hash_table_lookup(ipdevice_hash,
1602 GINT_TO_POINTER(ipconfig->index));
1603 if (ipdevice == NULL)
1606 return ipdevice->pac;
1609 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1610 const char *address)
1612 if (ipconfig == NULL)
1615 g_free(ipconfig->last_dhcp_address);
1616 ipconfig->last_dhcp_address = g_strdup(address);
1619 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1621 if (ipconfig == NULL)
1624 return ipconfig->last_dhcp_address;
1627 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1629 struct connman_ipdevice *ipdevice;
1633 ipdevice = g_hash_table_lookup(ipdevice_hash,
1634 GINT_TO_POINTER(ipconfig->index));
1635 if (ipdevice == NULL)
1638 set_ipv6_state(ipdevice->ifname, FALSE);
1641 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1643 struct connman_ipdevice *ipdevice;
1647 ipdevice = g_hash_table_lookup(ipdevice_hash,
1648 GINT_TO_POINTER(ipconfig->index));
1649 if (ipdevice == NULL)
1652 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1653 set_ipv6_privacy(ipdevice->ifname,
1654 ipconfig->ipv6_privacy_config);
1656 set_ipv6_state(ipdevice->ifname, TRUE);
1659 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1661 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1664 enable_ipv6(ipconfig);
1667 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1669 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1672 disable_ipv6(ipconfig);
1675 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1677 if (ipconfig == NULL)
1680 switch (ipconfig->method) {
1681 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1682 case CONNMAN_IPCONFIG_METHOD_OFF:
1684 case CONNMAN_IPCONFIG_METHOD_AUTO:
1685 case CONNMAN_IPCONFIG_METHOD_FIXED:
1686 case CONNMAN_IPCONFIG_METHOD_DHCP:
1687 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1694 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1696 struct connman_ipdevice *ipdevice;
1697 gboolean up = FALSE, down = FALSE;
1698 gboolean lower_up = FALSE, lower_down = FALSE;
1699 enum connman_ipconfig_type type;
1701 DBG("ipconfig %p", ipconfig);
1703 if (ipconfig == NULL || ipconfig->index < 0)
1706 ipdevice = g_hash_table_lookup(ipdevice_hash,
1707 GINT_TO_POINTER(ipconfig->index));
1708 if (ipdevice == NULL)
1711 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1712 if (ipdevice->config_ipv4 == ipconfig)
1714 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1715 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1716 if (ipdevice->config_ipv6 == ipconfig)
1718 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1719 enable_ipv6(ipconfig);
1723 ipconfig->enabled = TRUE;
1725 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1726 ipdevice->config_ipv4 != NULL) {
1727 ipconfig_list = g_list_remove(ipconfig_list,
1728 ipdevice->config_ipv4);
1730 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1732 __connman_ipconfig_unref(ipdevice->config_ipv4);
1735 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1736 ipdevice->config_ipv6 != NULL) {
1737 ipconfig_list = g_list_remove(ipconfig_list,
1738 ipdevice->config_ipv6);
1740 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1742 __connman_ipconfig_unref(ipdevice->config_ipv6);
1745 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1746 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1747 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1748 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1750 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1752 if (ipdevice->flags & IFF_UP)
1757 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1758 (IFF_RUNNING | IFF_LOWER_UP))
1760 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1763 if (up == TRUE && ipconfig->ops->up)
1764 ipconfig->ops->up(ipconfig);
1765 if (lower_up == TRUE && ipconfig->ops->lower_up)
1766 ipconfig->ops->lower_up(ipconfig);
1768 if (lower_down == TRUE && ipconfig->ops->lower_down)
1769 ipconfig->ops->lower_down(ipconfig);
1770 if (down == TRUE && ipconfig->ops->down)
1771 ipconfig->ops->down(ipconfig);
1776 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1778 struct connman_ipdevice *ipdevice;
1780 DBG("ipconfig %p", ipconfig);
1782 if (ipconfig == NULL || ipconfig->index < 0)
1785 ipdevice = g_hash_table_lookup(ipdevice_hash,
1786 GINT_TO_POINTER(ipconfig->index));
1787 if (ipdevice == NULL)
1790 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1793 ipconfig->enabled = FALSE;
1795 if (ipdevice->config_ipv4 == ipconfig) {
1796 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1798 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1799 __connman_ipconfig_unref(ipdevice->config_ipv4);
1800 ipdevice->config_ipv4 = NULL;
1804 if (ipdevice->config_ipv6 == ipconfig) {
1805 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1807 if (ipdevice->config_ipv6->method ==
1808 CONNMAN_IPCONFIG_METHOD_AUTO)
1809 disable_ipv6(ipdevice->config_ipv6);
1811 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1812 __connman_ipconfig_unref(ipdevice->config_ipv6);
1813 ipdevice->config_ipv6 = NULL;
1820 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1823 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1825 case CONNMAN_IPCONFIG_METHOD_OFF:
1827 case CONNMAN_IPCONFIG_METHOD_FIXED:
1829 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1831 case CONNMAN_IPCONFIG_METHOD_DHCP:
1833 case CONNMAN_IPCONFIG_METHOD_AUTO:
1840 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1842 if (g_strcmp0(method, "off") == 0)
1843 return CONNMAN_IPCONFIG_METHOD_OFF;
1844 else if (g_strcmp0(method, "fixed") == 0)
1845 return CONNMAN_IPCONFIG_METHOD_FIXED;
1846 else if (g_strcmp0(method, "manual") == 0)
1847 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1848 else if (g_strcmp0(method, "dhcp") == 0)
1849 return CONNMAN_IPCONFIG_METHOD_DHCP;
1850 else if (g_strcmp0(method, "auto") == 0)
1851 return CONNMAN_IPCONFIG_METHOD_AUTO;
1853 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1856 static const char *privacy2string(int privacy)
1860 else if (privacy == 1)
1862 else if (privacy > 1)
1868 static int string2privacy(const char *privacy)
1870 if (g_strcmp0(privacy, "disabled") == 0)
1872 else if (g_strcmp0(privacy, "enabled") == 0)
1874 else if (g_strcmp0(privacy, "prefered") == 0)
1880 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1881 DBusMessageIter *iter)
1883 struct connman_ipaddress *append_addr = NULL;
1888 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1891 str = __connman_ipconfig_method2string(ipconfig->method);
1895 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1897 switch (ipconfig->method) {
1898 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1899 case CONNMAN_IPCONFIG_METHOD_OFF:
1900 case CONNMAN_IPCONFIG_METHOD_AUTO:
1903 case CONNMAN_IPCONFIG_METHOD_FIXED:
1904 append_addr = ipconfig->address;
1907 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1908 case CONNMAN_IPCONFIG_METHOD_DHCP:
1909 append_addr = ipconfig->system;
1913 if (append_addr == NULL)
1916 if (append_addr->local != NULL) {
1918 struct in_addr netmask;
1921 connman_dbus_dict_append_basic(iter, "Address",
1922 DBUS_TYPE_STRING, &append_addr->local);
1924 addr = 0xffffffff << (32 - append_addr->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 (append_addr->gateway != NULL)
1932 connman_dbus_dict_append_basic(iter, "Gateway",
1933 DBUS_TYPE_STRING, &append_addr->gateway);
1936 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1937 DBusMessageIter *iter,
1938 struct connman_ipconfig *ipconfig_ipv4)
1940 struct connman_ipaddress *append_addr = NULL;
1941 const char *str, *privacy;
1945 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1948 str = __connman_ipconfig_method2string(ipconfig->method);
1952 if (ipconfig_ipv4 != NULL &&
1953 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1954 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1958 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1960 switch (ipconfig->method) {
1961 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1962 case CONNMAN_IPCONFIG_METHOD_OFF:
1965 case CONNMAN_IPCONFIG_METHOD_FIXED:
1966 append_addr = ipconfig->address;
1969 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1970 case CONNMAN_IPCONFIG_METHOD_DHCP:
1971 case CONNMAN_IPCONFIG_METHOD_AUTO:
1972 append_addr = ipconfig->system;
1976 if (append_addr == NULL)
1979 if (append_addr->local != NULL) {
1980 connman_dbus_dict_append_basic(iter, "Address",
1981 DBUS_TYPE_STRING, &append_addr->local);
1982 connman_dbus_dict_append_basic(iter, "PrefixLength",
1984 &append_addr->prefixlen);
1987 if (append_addr->gateway != NULL)
1988 connman_dbus_dict_append_basic(iter, "Gateway",
1989 DBUS_TYPE_STRING, &append_addr->gateway);
1991 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1992 connman_dbus_dict_append_basic(iter, "Privacy",
1993 DBUS_TYPE_STRING, &privacy);
1996 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1997 DBusMessageIter *iter)
1999 const char *str, *privacy;
2003 str = __connman_ipconfig_method2string(ipconfig->method);
2007 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2009 switch (ipconfig->method) {
2010 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2011 case CONNMAN_IPCONFIG_METHOD_OFF:
2012 case CONNMAN_IPCONFIG_METHOD_DHCP:
2014 case CONNMAN_IPCONFIG_METHOD_FIXED:
2015 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2016 case CONNMAN_IPCONFIG_METHOD_AUTO:
2020 if (ipconfig->address == NULL)
2023 if (ipconfig->address->local != NULL) {
2024 connman_dbus_dict_append_basic(iter, "Address",
2025 DBUS_TYPE_STRING, &ipconfig->address->local);
2026 connman_dbus_dict_append_basic(iter, "PrefixLength",
2028 &ipconfig->address->prefixlen);
2031 if (ipconfig->address->gateway != NULL)
2032 connman_dbus_dict_append_basic(iter, "Gateway",
2033 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2035 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2036 connman_dbus_dict_append_basic(iter, "Privacy",
2037 DBUS_TYPE_STRING, &privacy);
2040 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2041 DBusMessageIter *iter)
2047 str = __connman_ipconfig_method2string(ipconfig->method);
2051 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2053 switch (ipconfig->method) {
2054 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2055 case CONNMAN_IPCONFIG_METHOD_OFF:
2056 case CONNMAN_IPCONFIG_METHOD_DHCP:
2057 case CONNMAN_IPCONFIG_METHOD_AUTO:
2059 case CONNMAN_IPCONFIG_METHOD_FIXED:
2060 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2064 if (ipconfig->address == NULL)
2067 if (ipconfig->address->local != NULL) {
2069 struct in_addr netmask;
2072 connman_dbus_dict_append_basic(iter, "Address",
2073 DBUS_TYPE_STRING, &ipconfig->address->local);
2075 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2076 netmask.s_addr = htonl(addr);
2077 mask = inet_ntoa(netmask);
2078 connman_dbus_dict_append_basic(iter, "Netmask",
2079 DBUS_TYPE_STRING, &mask);
2082 if (ipconfig->address->gateway != NULL)
2083 connman_dbus_dict_append_basic(iter, "Gateway",
2084 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2087 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2088 DBusMessageIter *array)
2090 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2091 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2092 *prefix_length_string = NULL, *privacy_string = NULL;
2093 int prefix_length = 0, privacy = 0;
2094 DBusMessageIter dict;
2096 DBG("ipconfig %p", ipconfig);
2098 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2101 dbus_message_iter_recurse(array, &dict);
2103 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2104 DBusMessageIter entry, value;
2108 dbus_message_iter_recurse(&dict, &entry);
2110 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2113 dbus_message_iter_get_basic(&entry, &key);
2114 dbus_message_iter_next(&entry);
2116 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2119 dbus_message_iter_recurse(&entry, &value);
2121 type = dbus_message_iter_get_arg_type(&value);
2123 if (g_str_equal(key, "Method") == TRUE) {
2126 if (type != DBUS_TYPE_STRING)
2129 dbus_message_iter_get_basic(&value, &str);
2130 method = __connman_ipconfig_string2method(str);
2131 } else if (g_str_equal(key, "Address") == TRUE) {
2132 if (type != DBUS_TYPE_STRING)
2135 dbus_message_iter_get_basic(&value, &address);
2136 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2137 if (type != DBUS_TYPE_STRING)
2140 dbus_message_iter_get_basic(&value,
2141 &prefix_length_string);
2143 prefix_length = atoi(prefix_length_string);
2144 if (prefix_length < 0 || prefix_length > 128)
2146 } else if (g_str_equal(key, "Netmask") == TRUE) {
2147 if (type != DBUS_TYPE_STRING)
2150 dbus_message_iter_get_basic(&value, &netmask);
2151 } else if (g_str_equal(key, "Gateway") == TRUE) {
2152 if (type != DBUS_TYPE_STRING)
2155 dbus_message_iter_get_basic(&value, &gateway);
2156 } else if (g_str_equal(key, "Privacy") == TRUE) {
2157 if (type != DBUS_TYPE_STRING)
2160 dbus_message_iter_get_basic(&value, &privacy_string);
2161 privacy = string2privacy(privacy_string);
2164 dbus_message_iter_next(&dict);
2167 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2169 method, address, netmask, gateway, prefix_length,
2173 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2174 case CONNMAN_IPCONFIG_METHOD_FIXED:
2177 case CONNMAN_IPCONFIG_METHOD_OFF:
2178 ipconfig->method = method;
2179 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2180 disable_ipv6(ipconfig);
2183 case CONNMAN_IPCONFIG_METHOD_AUTO:
2184 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2187 ipconfig->method = method;
2188 if (privacy_string != NULL)
2189 ipconfig->ipv6_privacy_config = privacy;
2190 enable_ipv6(ipconfig);
2193 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2194 if (address == NULL)
2197 ipconfig->method = method;
2199 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2200 connman_ipaddress_set_ipv4(ipconfig->address,
2201 address, netmask, gateway);
2203 return connman_ipaddress_set_ipv6(
2204 ipconfig->address, address,
2205 prefix_length, gateway);
2208 case CONNMAN_IPCONFIG_METHOD_DHCP:
2209 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2212 ipconfig->method = method;
2219 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2220 DBusMessageIter *iter)
2222 struct connman_ipdevice *ipdevice;
2223 const char *method = "auto";
2225 connman_dbus_dict_append_basic(iter, "Method",
2226 DBUS_TYPE_STRING, &method);
2228 ipdevice = g_hash_table_lookup(ipdevice_hash,
2229 GINT_TO_POINTER(ipconfig->index));
2230 if (ipdevice == NULL)
2233 if (ipdevice->ifname != NULL)
2234 connman_dbus_dict_append_basic(iter, "Interface",
2235 DBUS_TYPE_STRING, &ipdevice->ifname);
2237 if (ipdevice->address != NULL)
2238 connman_dbus_dict_append_basic(iter, "Address",
2239 DBUS_TYPE_STRING, &ipdevice->address);
2241 if (ipdevice->mtu > 0)
2242 connman_dbus_dict_append_basic(iter, "MTU",
2243 DBUS_TYPE_UINT16, &ipdevice->mtu);
2246 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2247 GKeyFile *keyfile, const char *identifier, const char *prefix)
2253 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2255 key = g_strdup_printf("%smethod", prefix);
2256 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2257 if (method == NULL) {
2258 switch (ipconfig->type) {
2259 case CONNMAN_IPCONFIG_TYPE_IPV4:
2260 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2262 case CONNMAN_IPCONFIG_TYPE_IPV6:
2263 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2265 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2266 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2270 ipconfig->method = __connman_ipconfig_string2method(method);
2272 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2273 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2275 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2276 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2277 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2279 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2280 privacy = g_key_file_get_string(keyfile, identifier,
2282 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2291 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2292 ipconfig->address->prefixlen = g_key_file_get_integer(
2293 keyfile, identifier, key, NULL);
2296 key = g_strdup_printf("%slocal_address", prefix);
2297 ipconfig->address->local = g_key_file_get_string(
2298 keyfile, identifier, key, NULL);
2301 key = g_strdup_printf("%speer_address", prefix);
2302 ipconfig->address->peer = g_key_file_get_string(
2303 keyfile, identifier, key, NULL);
2306 key = g_strdup_printf("%sbroadcast_address", prefix);
2307 ipconfig->address->broadcast = g_key_file_get_string(
2308 keyfile, identifier, key, NULL);
2311 key = g_strdup_printf("%sgateway", prefix);
2312 ipconfig->address->gateway = g_key_file_get_string(
2313 keyfile, identifier, key, NULL);
2316 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2317 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2319 g_free(ipconfig->last_dhcp_address);
2320 ipconfig->last_dhcp_address = str;
2327 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2328 GKeyFile *keyfile, const char *identifier, const char *prefix)
2333 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2335 method = __connman_ipconfig_method2string(ipconfig->method);
2337 key = g_strdup_printf("%smethod", prefix);
2338 g_key_file_set_string(keyfile, identifier, key, method);
2341 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2342 const char *privacy;
2343 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2344 key = g_strdup_printf("%sprivacy", prefix);
2345 g_key_file_set_string(keyfile, identifier, key, privacy);
2349 switch (ipconfig->method) {
2350 case CONNMAN_IPCONFIG_METHOD_FIXED:
2351 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2353 case CONNMAN_IPCONFIG_METHOD_DHCP:
2354 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2355 if (ipconfig->last_dhcp_address != NULL &&
2356 strlen(ipconfig->last_dhcp_address) > 0)
2357 g_key_file_set_string(keyfile, identifier, key,
2358 ipconfig->last_dhcp_address);
2360 g_key_file_remove_key(keyfile, identifier, key, NULL);
2363 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2364 case CONNMAN_IPCONFIG_METHOD_OFF:
2365 case CONNMAN_IPCONFIG_METHOD_AUTO:
2369 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2370 if (ipconfig->address->prefixlen != 0)
2371 g_key_file_set_integer(keyfile, identifier,
2372 key, ipconfig->address->prefixlen);
2375 key = g_strdup_printf("%slocal_address", prefix);
2376 if (ipconfig->address->local != NULL)
2377 g_key_file_set_string(keyfile, identifier,
2378 key, ipconfig->address->local);
2381 key = g_strdup_printf("%speer_address", prefix);
2382 if (ipconfig->address->peer != NULL)
2383 g_key_file_set_string(keyfile, identifier,
2384 key, ipconfig->address->peer);
2387 key = g_strdup_printf("%sbroadcast_address", prefix);
2388 if (ipconfig->address->broadcast != NULL)
2389 g_key_file_set_string(keyfile, identifier,
2390 key, ipconfig->address->broadcast);
2393 key = g_strdup_printf("%sgateway", prefix);
2394 if (ipconfig->address->gateway != NULL)
2395 g_key_file_set_string(keyfile, identifier,
2396 key, ipconfig->address->gateway);
2402 int __connman_ipconfig_init(void)
2406 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2407 NULL, free_ipdevice);
2412 void __connman_ipconfig_cleanup(void)
2416 g_hash_table_destroy(ipdevice_hash);
2417 ipdevice_hash = NULL;