5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <net/if_arp.h>
29 #include <linux/if_link.h>
34 #define IFF_LOWER_UP 0x10000
41 struct connman_ipconfig {
44 enum connman_ipconfig_type type;
46 struct connman_ipconfig *origin;
48 const struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
52 struct connman_ipaddress *address;
53 struct connman_ipaddress *system;
55 int ipv6_privacy_config;
58 struct connman_ipdevice {
80 struct connman_ipconfig *config_ipv4;
81 struct connman_ipconfig *config_ipv6;
83 gboolean ipv6_enabled;
87 static GHashTable *ipdevice_hash = NULL;
88 static GList *ipconfig_list = NULL;
90 struct connman_ipaddress *connman_ipaddress_alloc(int family)
92 struct connman_ipaddress *ipaddress;
94 ipaddress = g_try_new0(struct connman_ipaddress, 1);
95 if (ipaddress == NULL)
98 ipaddress->family = family;
99 ipaddress->prefixlen = 0;
100 ipaddress->local = NULL;
101 ipaddress->peer = NULL;
102 ipaddress->broadcast = NULL;
103 ipaddress->gateway = NULL;
108 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
110 if (ipaddress == NULL)
113 g_free(ipaddress->broadcast);
114 g_free(ipaddress->peer);
115 g_free(ipaddress->local);
116 g_free(ipaddress->gateway);
120 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
129 mask = inet_network(netmask);
132 /* a valid netmask must be 2^n - 1 */
133 if ((host & (host + 1)) != 0)
137 for (; mask; mask <<= 1)
143 static gboolean check_ipv6_address(const char *address)
145 unsigned char buf[sizeof(struct in6_addr)];
148 err = inet_pton(AF_INET6, address, buf);
155 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
156 const char *address, const char *gateway,
157 unsigned char prefix_length)
159 if (ipaddress == NULL)
162 if (check_ipv6_address(address) == FALSE)
165 if (check_ipv6_address(gateway) == FALSE)
168 DBG("prefix_len %d address %s gateway %s",
169 prefix_length, address, gateway);
171 ipaddress->prefixlen = prefix_length;
173 g_free(ipaddress->local);
174 ipaddress->local = g_strdup(address);
176 g_free(ipaddress->gateway);
177 ipaddress->gateway = g_strdup(gateway);
182 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
183 const char *address, const char *netmask, const char *gateway)
185 if (ipaddress == NULL)
188 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
190 g_free(ipaddress->local);
191 ipaddress->local = g_strdup(address);
193 g_free(ipaddress->gateway);
194 ipaddress->gateway = g_strdup(gateway);
197 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
199 if (ipaddress == NULL)
202 ipaddress->prefixlen = 0;
204 g_free(ipaddress->local);
205 ipaddress->local = NULL;
207 g_free(ipaddress->peer);
208 ipaddress->peer = NULL;
210 g_free(ipaddress->broadcast);
211 ipaddress->broadcast = NULL;
213 g_free(ipaddress->gateway);
214 ipaddress->gateway = NULL;
217 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
218 struct connman_ipaddress *source)
220 if (ipaddress == NULL || source == NULL)
223 ipaddress->family = source->family;
224 ipaddress->prefixlen = source->prefixlen;
226 g_free(ipaddress->local);
227 ipaddress->local = g_strdup(source->local);
229 g_free(ipaddress->peer);
230 ipaddress->peer = g_strdup(source->peer);
232 g_free(ipaddress->broadcast);
233 ipaddress->broadcast = g_strdup(source->broadcast);
235 g_free(ipaddress->gateway);
236 ipaddress->gateway = g_strdup(source->gateway);
239 static void free_address_list(struct connman_ipdevice *ipdevice)
243 for (list = ipdevice->address_list; list; list = list->next) {
244 struct connman_ipaddress *ipaddress = list->data;
246 connman_ipaddress_free(ipaddress);
250 g_slist_free(ipdevice->address_list);
251 ipdevice->address_list = NULL;
254 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
255 unsigned char prefixlen, const char *local)
259 for (list = ipdevice->address_list; list; list = list->next) {
260 struct connman_ipaddress *ipaddress = list->data;
262 if (g_strcmp0(ipaddress->local, local) == 0 &&
263 ipaddress->prefixlen == prefixlen)
270 static const char *type2str(unsigned short type)
275 case ARPHRD_LOOPBACK:
288 static const char *scope2str(unsigned char scope)
300 static gboolean get_ipv6_state(gchar *ifname)
305 gboolean enabled = FALSE;
308 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
310 path = g_strdup_printf(
311 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
316 f = fopen(path, "r");
321 if (fscanf(f, "%d", &disabled) > 0)
329 static void set_ipv6_state(gchar *ifname, gboolean enable)
335 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
337 path = g_strdup_printf(
338 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
343 f = fopen(path, "r+");
358 static int get_ipv6_privacy(gchar *ifname)
367 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
373 f = fopen(path, "r");
380 if (fscanf(f, "%d", &value) <= 0)
388 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
389 * The privacy extension is described in RFC 3041 and RFC 4941
391 static void set_ipv6_privacy(gchar *ifname, int value)
399 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
408 f = fopen(path, "r+");
415 fprintf(f, "%d", value);
419 static void free_ipdevice(gpointer data)
421 struct connman_ipdevice *ipdevice = data;
423 connman_info("%s {remove} index %d", ipdevice->ifname,
426 if (ipdevice->config_ipv4 != NULL) {
427 connman_ipconfig_unref(ipdevice->config_ipv4);
428 ipdevice->config_ipv4 = NULL;
431 if (ipdevice->config_ipv6 != NULL) {
432 connman_ipconfig_unref(ipdevice->config_ipv6);
433 ipdevice->config_ipv6 = NULL;
436 free_address_list(ipdevice);
437 g_free(ipdevice->ipv4_gateway);
438 g_free(ipdevice->ipv6_gateway);
439 g_free(ipdevice->pac);
441 g_free(ipdevice->address);
443 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
444 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
446 g_free(ipdevice->ifname);
450 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
452 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
453 ipdevice->config_ipv6);
456 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
458 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
459 ipdevice->config_ipv6);
461 if (ipdevice->config_ipv4)
462 connman_inet_clear_address(ipdevice->index,
463 ipdevice->config_ipv4->address);
465 if (ipdevice->config_ipv6)
466 connman_inet_clear_ipv6_address(ipdevice->index,
467 ipdevice->config_ipv6->address->local,
468 ipdevice->config_ipv6->address->prefixlen);
471 static void update_stats(struct connman_ipdevice *ipdevice,
472 struct rtnl_link_stats *stats)
474 struct connman_service *service;
476 if (stats->rx_packets == 0 && stats->tx_packets == 0)
479 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
480 stats->rx_packets, stats->rx_bytes);
481 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
482 stats->tx_packets, stats->tx_bytes);
484 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
487 if (ipdevice->config_ipv4)
488 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
489 else if (ipdevice->config_ipv6)
490 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
497 ipdevice->rx_packets = stats->rx_packets;
498 ipdevice->tx_packets = stats->tx_packets;
499 ipdevice->rx_bytes = stats->rx_bytes;
500 ipdevice->tx_bytes = stats->tx_bytes;
501 ipdevice->rx_errors = stats->rx_errors;
502 ipdevice->tx_errors = stats->tx_errors;
503 ipdevice->rx_dropped = stats->rx_dropped;
504 ipdevice->tx_dropped = stats->tx_dropped;
506 __connman_service_notify(service,
507 ipdevice->rx_packets, ipdevice->tx_packets,
508 ipdevice->rx_bytes, ipdevice->tx_bytes,
509 ipdevice->rx_errors, ipdevice->tx_errors,
510 ipdevice->rx_dropped, ipdevice->tx_dropped);
513 void __connman_ipconfig_newlink(int index, unsigned short type,
514 unsigned int flags, const char *address,
516 struct rtnl_link_stats *stats)
518 struct connman_ipdevice *ipdevice;
521 gboolean up = FALSE, down = FALSE;
522 gboolean lower_up = FALSE, lower_down = FALSE;
524 DBG("index %d", index);
526 if (type == ARPHRD_LOOPBACK)
529 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
530 if (ipdevice != NULL)
533 ipdevice = g_try_new0(struct connman_ipdevice, 1);
534 if (ipdevice == NULL)
537 ipdevice->index = index;
538 ipdevice->ifname = connman_inet_ifname(index);
539 ipdevice->type = type;
541 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
542 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
544 ipdevice->address = g_strdup(address);
546 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
548 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
549 index, type, type2str(type));
554 update_stats(ipdevice, stats);
556 if (flags == ipdevice->flags)
559 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
566 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
567 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
568 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
569 (IFF_RUNNING | IFF_LOWER_UP))
571 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
575 ipdevice->flags = flags;
577 str = g_string_new(NULL);
582 g_string_append(str, "UP");
584 g_string_append(str, "DOWN");
586 if (flags & IFF_RUNNING)
587 g_string_append(str, ",RUNNING");
589 if (flags & IFF_LOWER_UP)
590 g_string_append(str, ",LOWER_UP");
592 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
595 g_string_free(str, TRUE);
597 for (list = g_list_first(ipconfig_list); list;
598 list = g_list_next(list)) {
599 struct connman_ipconfig *ipconfig = list->data;
601 if (index != ipconfig->index)
604 if (ipconfig->ops == NULL)
607 if (up == TRUE && ipconfig->ops->up)
608 ipconfig->ops->up(ipconfig);
609 if (lower_up == TRUE && ipconfig->ops->lower_up)
610 ipconfig->ops->lower_up(ipconfig);
612 if (lower_down == TRUE && ipconfig->ops->lower_down)
613 ipconfig->ops->lower_down(ipconfig);
614 if (down == TRUE && ipconfig->ops->down)
615 ipconfig->ops->down(ipconfig);
619 __connman_ipconfig_lower_up(ipdevice);
621 __connman_ipconfig_lower_down(ipdevice);
624 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
626 struct connman_ipdevice *ipdevice;
629 DBG("index %d", index);
631 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
632 if (ipdevice == NULL)
635 update_stats(ipdevice, stats);
637 for (list = g_list_first(ipconfig_list); list;
638 list = g_list_next(list)) {
639 struct connman_ipconfig *ipconfig = list->data;
641 if (index != ipconfig->index)
644 ipconfig->index = -1;
646 if (ipconfig->ops == NULL)
649 if (ipconfig->ops->lower_down)
650 ipconfig->ops->lower_down(ipconfig);
651 if (ipconfig->ops->down)
652 ipconfig->ops->down(ipconfig);
655 __connman_ipconfig_lower_down(ipdevice);
657 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
660 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
662 const struct connman_ipaddress *addr1 = a;
663 const struct connman_ipaddress *addr2 = b;
665 if (addr1->prefixlen != addr2->prefixlen)
666 return addr2->prefixlen - addr1->prefixlen;
668 return g_strcmp0(addr1->local, addr2->local);
671 void __connman_ipconfig_newaddr(int index, int family, const char *label,
672 unsigned char prefixlen, const char *address)
674 struct connman_ipdevice *ipdevice;
675 struct connman_ipaddress *ipaddress;
678 DBG("index %d", index);
680 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
681 if (ipdevice == NULL)
684 ipaddress = connman_ipaddress_alloc(family);
685 if (ipaddress == NULL)
688 ipaddress->prefixlen = prefixlen;
689 ipaddress->local = g_strdup(address);
691 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
692 check_duplicate_address)) {
693 connman_ipaddress_free(ipaddress);
697 ipdevice->address_list = g_slist_append(ipdevice->address_list,
700 connman_info("%s {add} address %s/%u label %s family %d",
701 ipdevice->ifname, address, prefixlen, label, family);
703 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
704 connman_ipaddress_copy(ipdevice->config_ipv4->system,
707 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
708 connman_ipaddress_copy(ipdevice->config_ipv6->system,
713 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
716 for (list = g_list_first(ipconfig_list); list;
717 list = g_list_next(list)) {
718 struct connman_ipconfig *ipconfig = list->data;
720 if (index != ipconfig->index)
723 if (ipconfig->ops == NULL)
726 if (ipconfig->ops->ip_bound)
727 ipconfig->ops->ip_bound(ipconfig);
731 void __connman_ipconfig_deladdr(int index, int family, const char *label,
732 unsigned char prefixlen, const char *address)
734 struct connman_ipdevice *ipdevice;
735 struct connman_ipaddress *ipaddress;
738 DBG("index %d", index);
740 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
741 if (ipdevice == NULL)
744 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
745 if (ipaddress == NULL)
748 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
751 connman_ipaddress_clear(ipaddress);
754 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
755 address, prefixlen, label);
757 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
760 if (g_slist_length(ipdevice->address_list) > 0)
763 for (list = g_list_first(ipconfig_list); list;
764 list = g_list_next(list)) {
765 struct connman_ipconfig *ipconfig = list->data;
767 if (index != ipconfig->index)
770 if (ipconfig->ops == NULL)
773 if (ipconfig->ops->ip_release)
774 ipconfig->ops->ip_release(ipconfig);
778 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
779 const char *dst, const char *gateway)
781 struct connman_ipdevice *ipdevice;
783 DBG("index %d", index);
785 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
786 if (ipdevice == NULL)
789 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
793 if (family == AF_INET6) {
794 g_free(ipdevice->ipv6_gateway);
795 ipdevice->ipv6_gateway = g_strdup(gateway);
797 if (ipdevice->config_ipv6 != NULL &&
798 ipdevice->config_ipv6->system != NULL) {
799 g_free(ipdevice->config_ipv6->system->gateway);
800 ipdevice->config_ipv6->system->gateway =
804 g_free(ipdevice->ipv4_gateway);
805 ipdevice->ipv4_gateway = g_strdup(gateway);
807 if (ipdevice->config_ipv4 != NULL &&
808 ipdevice->config_ipv4->system != NULL) {
809 g_free(ipdevice->config_ipv4->system->gateway);
810 ipdevice->config_ipv4->system->gateway =
815 for (list = ipdevice->address_list; list; list = list->next) {
816 struct connman_ipaddress *ipaddress = list->data;
818 g_free(ipaddress->gateway);
819 ipaddress->gateway = g_strdup(gateway);
822 for (config_list = g_list_first(ipconfig_list); config_list;
823 config_list = g_list_next(config_list)) {
824 struct connman_ipconfig *ipconfig = config_list->data;
826 if (index != ipconfig->index)
829 if (ipconfig->ops == NULL)
832 if (ipconfig->ops->ip_bound)
833 ipconfig->ops->ip_bound(ipconfig);
837 connman_info("%s {add} route %s gw %s scope %u <%s>",
838 ipdevice->ifname, dst, gateway,
839 scope, scope2str(scope));
842 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
843 const char *dst, const char *gateway)
845 struct connman_ipdevice *ipdevice;
847 DBG("index %d", index);
849 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
850 if (ipdevice == NULL)
853 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
857 if (family == AF_INET6) {
858 g_free(ipdevice->ipv6_gateway);
859 ipdevice->ipv6_gateway = NULL;
861 if (ipdevice->config_ipv6 != NULL &&
862 ipdevice->config_ipv6->system != NULL) {
863 g_free(ipdevice->config_ipv6->system->gateway);
864 ipdevice->config_ipv6->system->gateway = NULL;
867 g_free(ipdevice->ipv4_gateway);
868 ipdevice->ipv4_gateway = NULL;
870 if (ipdevice->config_ipv4 != NULL &&
871 ipdevice->config_ipv4->system != NULL) {
872 g_free(ipdevice->config_ipv4->system->gateway);
873 ipdevice->config_ipv4->system->gateway = NULL;
877 for (list = ipdevice->address_list; list; list = list->next) {
878 struct connman_ipaddress *ipaddress = list->data;
880 g_free(ipaddress->gateway);
881 ipaddress->gateway = NULL;
884 for (config_list = g_list_first(ipconfig_list); config_list;
885 config_list = g_list_next(config_list)) {
886 struct connman_ipconfig *ipconfig = config_list->data;
888 if (index != ipconfig->index)
891 if (ipconfig->ops == NULL)
894 if (ipconfig->ops->ip_release)
895 ipconfig->ops->ip_release(ipconfig);
899 connman_info("%s {del} route %s gw %s scope %u <%s>",
900 ipdevice->ifname, dst, gateway,
901 scope, scope2str(scope));
904 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
909 keys = g_hash_table_get_keys(ipdevice_hash);
913 for (list = g_list_first(keys); list; list = g_list_next(list)) {
914 int index = GPOINTER_TO_INT(list->data);
916 function(index, user_data);
922 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
923 struct connman_ipconfig *ipconfig)
925 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
928 unsigned short __connman_ipconfig_get_type_from_index(int index)
930 struct connman_ipdevice *ipdevice;
932 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
933 if (ipdevice == NULL)
936 return ipdevice->type;
939 unsigned int __connman_ipconfig_get_flags_from_index(int index)
941 struct connman_ipdevice *ipdevice;
943 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
944 if (ipdevice == NULL)
947 return ipdevice->flags;
950 const char *__connman_ipconfig_get_gateway_from_index(int index)
952 struct connman_ipdevice *ipdevice;
954 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
955 if (ipdevice == NULL)
958 if (ipdevice->ipv4_gateway != NULL)
959 return ipdevice->ipv4_gateway;
961 if (ipdevice->config_ipv4 != NULL &&
962 ipdevice->config_ipv4->address != NULL)
963 return ipdevice->config_ipv4->address->gateway;
965 if (ipdevice->ipv6_gateway != NULL)
966 return ipdevice->ipv6_gateway;
968 if (ipdevice->config_ipv6 != NULL &&
969 ipdevice->config_ipv6->address != NULL)
970 return ipdevice->config_ipv6->address->gateway;
975 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
977 ipconfig->index = index;
980 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
982 if (ipconfig->address == NULL)
985 return ipconfig->address->local;
988 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
990 if (ipconfig->address == NULL)
993 g_free(ipconfig->address->local);
994 ipconfig->address->local = g_strdup(address);
997 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
999 if (ipconfig->address == NULL)
1002 return ipconfig->address->peer;
1005 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1007 if (ipconfig->address == NULL)
1010 g_free(ipconfig->address->peer);
1011 ipconfig->address->peer = g_strdup(address);
1014 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1016 if (ipconfig->address == NULL)
1019 return ipconfig->address->broadcast;
1022 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1024 if (ipconfig->address == NULL)
1027 g_free(ipconfig->address->broadcast);
1028 ipconfig->address->broadcast = g_strdup(broadcast);
1031 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1033 if (ipconfig->address == NULL)
1036 return ipconfig->address->gateway;
1039 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1041 struct connman_service *service;
1043 if (ipconfig->address == NULL)
1046 service = __connman_service_lookup_from_index(ipconfig->index);
1047 if (service != NULL)
1048 __connman_connection_gateway_remove(service);
1050 g_free(ipconfig->address->gateway);
1051 ipconfig->address->gateway = g_strdup(gateway);
1053 if (service != NULL && ipconfig->address->gateway != NULL) {
1054 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1055 __connman_connection_gateway_add(service,
1057 ipconfig->address->gateway,
1058 ipconfig->address->peer);
1059 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1060 __connman_connection_gateway_add(service,
1061 ipconfig->address->gateway,
1063 ipconfig->address->peer);
1068 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1070 if (ipconfig->address == NULL)
1073 return ipconfig->address->prefixlen;
1076 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1078 if (ipconfig->address == NULL)
1081 ipconfig->address->prefixlen = prefixlen;
1084 static struct connman_ipconfig *create_ipv6config(int index)
1086 struct connman_ipconfig *ipv6config;
1088 DBG("index %d", index);
1090 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1091 if (ipv6config == NULL)
1094 ipv6config->refcount = 1;
1096 ipv6config->index = index;
1097 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1098 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1099 ipv6config->ipv6_privacy_config = 0;
1101 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1102 if (ipv6config->address == NULL) {
1107 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1109 DBG("ipconfig %p", ipv6config);
1115 * connman_ipconfig_create:
1117 * Allocate a new ipconfig structure.
1119 * Returns: a newly-allocated #connman_ipconfig structure
1121 struct connman_ipconfig *connman_ipconfig_create(int index,
1122 enum connman_ipconfig_type type)
1124 struct connman_ipconfig *ipconfig;
1126 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1127 return create_ipv6config(index);
1129 DBG("index %d", index);
1131 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1132 if (ipconfig == NULL)
1135 ipconfig->refcount = 1;
1137 ipconfig->index = index;
1138 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1140 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1141 if (ipconfig->address == NULL) {
1146 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1148 DBG("ipconfig %p", ipconfig);
1155 * connman_ipconfig_ref:
1156 * @ipconfig: ipconfig structure
1158 * Increase reference counter of ipconfig
1160 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1162 DBG("ipconfig %p refcount %d", ipconfig,
1163 g_atomic_int_get(&ipconfig->refcount) + 1);
1165 g_atomic_int_inc(&ipconfig->refcount);
1171 * connman_ipconfig_unref:
1172 * @ipconfig: ipconfig structure
1174 * Decrease reference counter of ipconfig
1176 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1178 if (ipconfig == NULL)
1181 DBG("ipconfig %p refcount %d", ipconfig,
1182 g_atomic_int_get(&ipconfig->refcount) - 1);
1184 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1185 __connman_ipconfig_disable(ipconfig);
1187 connman_ipconfig_set_ops(ipconfig, NULL);
1189 if (ipconfig->origin != NULL) {
1190 connman_ipconfig_unref(ipconfig->origin);
1191 ipconfig->origin = NULL;
1194 connman_ipaddress_free(ipconfig->system);
1195 connman_ipaddress_free(ipconfig->address);
1201 * connman_ipconfig_get_data:
1202 * @ipconfig: ipconfig structure
1204 * Get private data pointer
1206 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1208 if (ipconfig == NULL)
1211 return ipconfig->ops_data;
1215 * connman_ipconfig_set_data:
1216 * @ipconfig: ipconfig structure
1217 * @data: data pointer
1219 * Set private data pointer
1221 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1223 ipconfig->ops_data = data;
1227 * connman_ipconfig_get_index:
1228 * @ipconfig: ipconfig structure
1230 * Get interface index
1232 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1234 if (ipconfig == NULL)
1237 if (ipconfig->origin != NULL)
1238 return ipconfig->origin->index;
1240 return ipconfig->index;
1244 * connman_ipconfig_get_ifname:
1245 * @ipconfig: ipconfig structure
1247 * Get interface name
1249 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1251 struct connman_ipdevice *ipdevice;
1253 if (ipconfig == NULL)
1256 if (ipconfig->index < 0)
1259 ipdevice = g_hash_table_lookup(ipdevice_hash,
1260 GINT_TO_POINTER(ipconfig->index));
1261 if (ipdevice == NULL)
1264 return ipdevice->ifname;
1268 * connman_ipconfig_set_ops:
1269 * @ipconfig: ipconfig structure
1270 * @ops: operation callbacks
1272 * Set the operation callbacks
1274 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1275 const struct connman_ipconfig_ops *ops)
1277 ipconfig->ops = ops;
1281 * connman_ipconfig_set_method:
1282 * @ipconfig: ipconfig structure
1283 * @method: configuration method
1285 * Set the configuration method
1287 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1288 enum connman_ipconfig_method method)
1290 ipconfig->method = method;
1295 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1297 if (ipconfig == NULL)
1298 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1300 return ipconfig->method;
1304 * connman_ipconfig_bind:
1305 * @ipconfig: ipconfig structure
1306 * @ipaddress: ipaddress structure
1308 * Bind IP address details to configuration
1310 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1311 struct connman_ipaddress *ipaddress)
1313 struct connman_ipconfig *origin;
1315 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1317 connman_ipaddress_copy(origin->address, ipaddress);
1319 connman_inet_set_address(origin->index, origin->address);
1322 void __connman_ipconfig_set_element_ipv6_gateway(
1323 struct connman_ipconfig *ipconfig,
1324 struct connman_element *element)
1326 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1327 element->ipv6.gateway = ipconfig->address->gateway;
1330 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1334 switch (ipconfig->method) {
1335 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1336 case CONNMAN_IPCONFIG_METHOD_OFF:
1337 case CONNMAN_IPCONFIG_METHOD_FIXED:
1338 case CONNMAN_IPCONFIG_METHOD_DHCP:
1339 case CONNMAN_IPCONFIG_METHOD_AUTO:
1341 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1342 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1343 return connman_inet_set_address(ipconfig->index,
1345 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1346 return connman_inet_set_ipv6_address(
1347 ipconfig->index, ipconfig->address);
1353 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1357 if (ipconfig == NULL)
1360 DBG("method %d", ipconfig->method);
1362 switch (ipconfig->method) {
1363 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1364 case CONNMAN_IPCONFIG_METHOD_OFF:
1365 case CONNMAN_IPCONFIG_METHOD_FIXED:
1366 case CONNMAN_IPCONFIG_METHOD_DHCP:
1367 case CONNMAN_IPCONFIG_METHOD_AUTO:
1369 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1370 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1371 return connman_inet_clear_address(ipconfig->index,
1373 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1374 return connman_inet_clear_ipv6_address(
1376 ipconfig->address->local,
1377 ipconfig->address->prefixlen);
1383 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1386 struct connman_ipdevice *ipdevice;
1388 DBG("ipconfig %p", ipconfig);
1390 if (ipconfig == NULL || ipconfig->index < 0)
1393 ipdevice = g_hash_table_lookup(ipdevice_hash,
1394 GINT_TO_POINTER(ipconfig->index));
1395 if (ipdevice == NULL)
1398 g_free(ipdevice->pac);
1399 ipdevice->pac = g_strdup(url);
1404 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1406 struct connman_ipdevice *ipdevice;
1408 DBG("ipconfig %p", ipconfig);
1410 if (ipconfig == NULL || ipconfig->index < 0)
1413 ipdevice = g_hash_table_lookup(ipdevice_hash,
1414 GINT_TO_POINTER(ipconfig->index));
1415 if (ipdevice == NULL)
1418 return ipdevice->pac;
1421 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1423 struct connman_ipdevice *ipdevice;
1427 ipdevice = g_hash_table_lookup(ipdevice_hash,
1428 GINT_TO_POINTER(ipconfig->index));
1429 if (ipdevice == NULL)
1432 set_ipv6_state(ipdevice->ifname, FALSE);
1435 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1437 struct connman_ipdevice *ipdevice;
1441 ipdevice = g_hash_table_lookup(ipdevice_hash,
1442 GINT_TO_POINTER(ipconfig->index));
1443 if (ipdevice == NULL)
1446 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1447 set_ipv6_privacy(ipdevice->ifname,
1448 ipconfig->ipv6_privacy_config);
1450 set_ipv6_state(ipdevice->ifname, TRUE);
1453 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1455 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1458 disable_ipv6(ipconfig);
1461 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1463 struct connman_ipdevice *ipdevice;
1464 gboolean up = FALSE, down = FALSE;
1465 gboolean lower_up = FALSE, lower_down = FALSE;
1466 enum connman_ipconfig_type type;
1468 DBG("ipconfig %p", ipconfig);
1470 if (ipconfig == NULL || ipconfig->index < 0)
1473 ipdevice = g_hash_table_lookup(ipdevice_hash,
1474 GINT_TO_POINTER(ipconfig->index));
1475 if (ipdevice == NULL)
1478 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1479 if (ipdevice->config_ipv4 == ipconfig)
1481 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1482 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1483 if (ipdevice->config_ipv6 == ipconfig)
1485 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1486 enable_ipv6(ipconfig);
1490 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1491 ipdevice->config_ipv4 != NULL) {
1492 ipconfig_list = g_list_remove(ipconfig_list,
1493 ipdevice->config_ipv4);
1495 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1497 connman_ipconfig_unref(ipdevice->config_ipv4);
1500 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1501 ipdevice->config_ipv6 != NULL) {
1502 ipconfig_list = g_list_remove(ipconfig_list,
1503 ipdevice->config_ipv6);
1505 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1507 connman_ipconfig_unref(ipdevice->config_ipv6);
1510 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1511 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1512 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1513 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1515 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1517 if (ipdevice->flags & IFF_UP)
1522 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1523 (IFF_RUNNING | IFF_LOWER_UP))
1525 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1528 if (up == TRUE && ipconfig->ops->up)
1529 ipconfig->ops->up(ipconfig);
1530 if (lower_up == TRUE && ipconfig->ops->lower_up)
1531 ipconfig->ops->lower_up(ipconfig);
1533 if (lower_down == TRUE && ipconfig->ops->lower_down)
1534 ipconfig->ops->lower_down(ipconfig);
1535 if (down == TRUE && ipconfig->ops->down)
1536 ipconfig->ops->down(ipconfig);
1541 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1543 struct connman_ipdevice *ipdevice;
1545 DBG("ipconfig %p", ipconfig);
1547 if (ipconfig == NULL || ipconfig->index < 0)
1550 ipdevice = g_hash_table_lookup(ipdevice_hash,
1551 GINT_TO_POINTER(ipconfig->index));
1552 if (ipdevice == NULL)
1555 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1558 if (ipdevice->config_ipv4 == ipconfig) {
1559 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1561 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1562 connman_ipconfig_unref(ipdevice->config_ipv4);
1563 ipdevice->config_ipv4 = NULL;
1567 if (ipdevice->config_ipv6 == ipconfig) {
1568 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1570 if (ipdevice->config_ipv6->method ==
1571 CONNMAN_IPCONFIG_METHOD_AUTO)
1572 disable_ipv6(ipdevice->config_ipv6);
1574 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1575 connman_ipconfig_unref(ipdevice->config_ipv6);
1576 ipdevice->config_ipv6 = NULL;
1583 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1586 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1588 case CONNMAN_IPCONFIG_METHOD_OFF:
1590 case CONNMAN_IPCONFIG_METHOD_FIXED:
1592 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1594 case CONNMAN_IPCONFIG_METHOD_DHCP:
1596 case CONNMAN_IPCONFIG_METHOD_AUTO:
1603 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1605 if (g_strcmp0(method, "off") == 0)
1606 return CONNMAN_IPCONFIG_METHOD_OFF;
1607 else if (g_strcmp0(method, "fixed") == 0)
1608 return CONNMAN_IPCONFIG_METHOD_FIXED;
1609 else if (g_strcmp0(method, "manual") == 0)
1610 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1611 else if (g_strcmp0(method, "dhcp") == 0)
1612 return CONNMAN_IPCONFIG_METHOD_DHCP;
1613 else if (g_strcmp0(method, "auto") == 0)
1614 return CONNMAN_IPCONFIG_METHOD_AUTO;
1616 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1619 static const char *privacy2string(int privacy)
1623 else if (privacy == 1)
1625 else if (privacy > 1)
1631 static int string2privacy(const char *privacy)
1633 if (g_strcmp0(privacy, "disabled") == 0)
1635 else if (g_strcmp0(privacy, "enabled") == 0)
1637 else if (g_strcmp0(privacy, "prefered") == 0)
1643 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1644 DBusMessageIter *iter)
1650 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1653 str = __connman_ipconfig_method2string(ipconfig->method);
1657 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1659 if (ipconfig->system == NULL)
1662 if (ipconfig->system->local != NULL) {
1664 struct in_addr netmask;
1667 connman_dbus_dict_append_basic(iter, "Address",
1668 DBUS_TYPE_STRING, &ipconfig->system->local);
1670 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1671 netmask.s_addr = htonl(addr);
1672 mask = inet_ntoa(netmask);
1673 connman_dbus_dict_append_basic(iter, "Netmask",
1674 DBUS_TYPE_STRING, &mask);
1677 if (ipconfig->system->gateway != NULL)
1678 connman_dbus_dict_append_basic(iter, "Gateway",
1679 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1682 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1683 DBusMessageIter *iter)
1685 const char *str, *privacy;
1689 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1692 str = __connman_ipconfig_method2string(ipconfig->method);
1696 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1698 if (ipconfig->system == NULL)
1701 if (ipconfig->system->local != NULL) {
1702 connman_dbus_dict_append_basic(iter, "Address",
1703 DBUS_TYPE_STRING, &ipconfig->system->local);
1704 connman_dbus_dict_append_basic(iter, "PrefixLength",
1706 &ipconfig->system->prefixlen);
1709 if (ipconfig->system->gateway != NULL)
1710 connman_dbus_dict_append_basic(iter, "Gateway",
1711 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1713 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1714 connman_dbus_dict_append_basic(iter, "Privacy",
1715 DBUS_TYPE_STRING, &privacy);
1718 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1719 DBusMessageIter *iter)
1721 const char *str, *privacy;
1725 str = __connman_ipconfig_method2string(ipconfig->method);
1729 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1731 switch (ipconfig->method) {
1732 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1733 case CONNMAN_IPCONFIG_METHOD_OFF:
1734 case CONNMAN_IPCONFIG_METHOD_DHCP:
1736 case CONNMAN_IPCONFIG_METHOD_FIXED:
1737 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1738 case CONNMAN_IPCONFIG_METHOD_AUTO:
1742 if (ipconfig->address == NULL)
1745 if (ipconfig->address->local != NULL) {
1746 connman_dbus_dict_append_basic(iter, "Address",
1747 DBUS_TYPE_STRING, &ipconfig->address->local);
1748 connman_dbus_dict_append_basic(iter, "PrefixLength",
1750 &ipconfig->address->prefixlen);
1753 if (ipconfig->address->gateway != NULL)
1754 connman_dbus_dict_append_basic(iter, "Gateway",
1755 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1757 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1758 connman_dbus_dict_append_basic(iter, "Privacy",
1759 DBUS_TYPE_STRING, &privacy);
1762 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1763 DBusMessageIter *iter)
1769 str = __connman_ipconfig_method2string(ipconfig->method);
1773 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1775 switch (ipconfig->method) {
1776 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1777 case CONNMAN_IPCONFIG_METHOD_OFF:
1778 case CONNMAN_IPCONFIG_METHOD_FIXED:
1779 case CONNMAN_IPCONFIG_METHOD_DHCP:
1780 case CONNMAN_IPCONFIG_METHOD_AUTO:
1782 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1786 if (ipconfig->address == NULL)
1789 if (ipconfig->address->local != NULL) {
1791 struct in_addr netmask;
1794 connman_dbus_dict_append_basic(iter, "Address",
1795 DBUS_TYPE_STRING, &ipconfig->address->local);
1797 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1798 netmask.s_addr = htonl(addr);
1799 mask = inet_ntoa(netmask);
1800 connman_dbus_dict_append_basic(iter, "Netmask",
1801 DBUS_TYPE_STRING, &mask);
1804 if (ipconfig->address->gateway != NULL)
1805 connman_dbus_dict_append_basic(iter, "Gateway",
1806 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1809 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1810 DBusMessageIter *array)
1812 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1813 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1814 *prefix_length_string = NULL, *privacy_string = NULL;
1815 int prefix_length = 0, privacy = 0;
1816 DBusMessageIter dict;
1818 DBG("ipconfig %p", ipconfig);
1820 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1823 dbus_message_iter_recurse(array, &dict);
1825 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1826 DBusMessageIter entry;
1830 dbus_message_iter_recurse(&dict, &entry);
1832 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1835 dbus_message_iter_get_basic(&entry, &key);
1836 dbus_message_iter_next(&entry);
1838 type = dbus_message_iter_get_arg_type(&entry);
1840 if (g_str_equal(key, "Method") == TRUE) {
1843 if (type != DBUS_TYPE_STRING)
1846 dbus_message_iter_get_basic(&entry, &str);
1847 method = __connman_ipconfig_string2method(str);
1848 } else if (g_str_equal(key, "Address") == TRUE) {
1849 if (type != DBUS_TYPE_STRING)
1852 dbus_message_iter_get_basic(&entry, &address);
1853 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1854 if (type != DBUS_TYPE_STRING)
1857 dbus_message_iter_get_basic(&entry,
1858 &prefix_length_string);
1860 prefix_length = atoi(prefix_length_string);
1861 if (prefix_length < 0 || prefix_length > 128)
1864 } else if (g_str_equal(key, "Netmask") == TRUE) {
1865 if (type != DBUS_TYPE_STRING)
1868 dbus_message_iter_get_basic(&entry, &netmask);
1869 } else if (g_str_equal(key, "Gateway") == TRUE) {
1870 if (type != DBUS_TYPE_STRING)
1873 dbus_message_iter_get_basic(&entry, &gateway);
1874 } else if (g_str_equal(key, "Privacy") == TRUE) {
1875 if (type != DBUS_TYPE_STRING)
1878 dbus_message_iter_get_basic(&entry, &privacy_string);
1879 privacy = string2privacy(privacy_string);
1881 dbus_message_iter_next(&dict);
1884 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1886 method, address, netmask, gateway, prefix_length,
1890 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1891 case CONNMAN_IPCONFIG_METHOD_FIXED:
1894 case CONNMAN_IPCONFIG_METHOD_OFF:
1895 ipconfig->method = method;
1896 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1897 disable_ipv6(ipconfig);
1900 case CONNMAN_IPCONFIG_METHOD_AUTO:
1901 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1904 ipconfig->method = method;
1905 if (privacy_string != NULL)
1906 ipconfig->ipv6_privacy_config = privacy;
1907 enable_ipv6(ipconfig);
1910 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1911 if (address == NULL)
1914 ipconfig->method = method;
1916 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1917 connman_ipaddress_set_ipv4(ipconfig->address,
1918 address, netmask, gateway);
1920 return connman_ipaddress_set_ipv6(
1921 ipconfig->address, address,
1922 gateway, prefix_length);
1925 case CONNMAN_IPCONFIG_METHOD_DHCP:
1926 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1929 ipconfig->method = method;
1936 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1937 DBusMessageIter *iter)
1939 struct connman_ipdevice *ipdevice;
1940 const char *method = "auto";
1942 connman_dbus_dict_append_basic(iter, "Method",
1943 DBUS_TYPE_STRING, &method);
1945 ipdevice = g_hash_table_lookup(ipdevice_hash,
1946 GINT_TO_POINTER(ipconfig->index));
1947 if (ipdevice == NULL)
1950 if (ipdevice->ifname != NULL)
1951 connman_dbus_dict_append_basic(iter, "Interface",
1952 DBUS_TYPE_STRING, &ipdevice->ifname);
1954 if (ipdevice->address != NULL)
1955 connman_dbus_dict_append_basic(iter, "Address",
1956 DBUS_TYPE_STRING, &ipdevice->address);
1958 if (ipdevice->mtu > 0)
1959 connman_dbus_dict_append_basic(iter, "MTU",
1960 DBUS_TYPE_UINT16, &ipdevice->mtu);
1963 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1964 GKeyFile *keyfile, const char *identifier, const char *prefix)
1969 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1971 key = g_strdup_printf("%smethod", prefix);
1972 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1973 if (method == NULL) {
1974 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1975 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1977 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1979 ipconfig->method = __connman_ipconfig_string2method(method);
1981 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1982 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1984 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1985 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
1986 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
1988 char *pprefix = g_strdup_printf("%sprivacy", prefix);
1989 privacy = g_key_file_get_string(keyfile, identifier,
1991 ipconfig->ipv6_privacy_config = string2privacy(privacy);
1995 __connman_ipconfig_enable(ipconfig);
1996 enable_ipv6(ipconfig);
2003 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2004 ipconfig->address->prefixlen = g_key_file_get_integer(
2005 keyfile, identifier, key, NULL);
2008 key = g_strdup_printf("%slocal_address", prefix);
2009 ipconfig->address->local = g_key_file_get_string(
2010 keyfile, identifier, key, NULL);
2013 key = g_strdup_printf("%speer_address", prefix);
2014 ipconfig->address->peer = g_key_file_get_string(
2015 keyfile, identifier, key, NULL);
2018 key = g_strdup_printf("%sbroadcast_address", prefix);
2019 ipconfig->address->broadcast = g_key_file_get_string(
2020 keyfile, identifier, key, NULL);
2023 key = g_strdup_printf("%sgateway", prefix);
2024 ipconfig->address->gateway = g_key_file_get_string(
2025 keyfile, identifier, key, NULL);
2031 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2032 GKeyFile *keyfile, const char *identifier, const char *prefix)
2037 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2039 method = __connman_ipconfig_method2string(ipconfig->method);
2041 key = g_strdup_printf("%smethod", prefix);
2042 g_key_file_set_string(keyfile, identifier, key, method);
2045 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2046 g_key_file_set_integer(keyfile, identifier,
2047 key, ipconfig->address->prefixlen);
2050 key = g_strdup_printf("%slocal_address", prefix);
2051 if (ipconfig->address->local != NULL)
2052 g_key_file_set_string(keyfile, identifier,
2053 key, ipconfig->address->local);
2056 key = g_strdup_printf("%speer_address", prefix);
2057 if (ipconfig->address->peer != NULL)
2058 g_key_file_set_string(keyfile, identifier,
2059 key, ipconfig->address->peer);
2062 key = g_strdup_printf("%sbroadcast_address", prefix);
2063 if (ipconfig->address->broadcast != NULL)
2064 g_key_file_set_string(keyfile, identifier,
2065 key, ipconfig->address->broadcast);
2068 key = g_strdup_printf("%sgateway", prefix);
2069 if (ipconfig->address->gateway != NULL)
2070 g_key_file_set_string(keyfile, identifier,
2071 key, ipconfig->address->gateway);
2074 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2075 const char *privacy;
2076 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2077 key = g_strdup_printf("%sprivacy", prefix);
2078 g_key_file_set_string(keyfile, identifier, key, privacy);
2085 int __connman_ipconfig_init(void)
2089 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2090 NULL, free_ipdevice);
2095 void __connman_ipconfig_cleanup(void)
2099 g_hash_table_destroy(ipdevice_hash);
2100 ipdevice_hash = NULL;