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
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 enum connman_ipconfig_method method;
53 struct connman_ipaddress *address;
54 struct connman_ipaddress *system;
56 int ipv6_privacy_config;
59 struct connman_ipdevice {
81 struct connman_ipconfig *config_ipv4;
82 struct connman_ipconfig *config_ipv6;
84 gboolean ipv6_enabled;
88 static GHashTable *ipdevice_hash = NULL;
89 static GList *ipconfig_list = NULL;
91 struct connman_ipaddress *connman_ipaddress_alloc(int family)
93 struct connman_ipaddress *ipaddress;
95 ipaddress = g_try_new0(struct connman_ipaddress, 1);
96 if (ipaddress == NULL)
99 ipaddress->family = family;
100 ipaddress->prefixlen = 0;
101 ipaddress->local = NULL;
102 ipaddress->peer = NULL;
103 ipaddress->broadcast = NULL;
104 ipaddress->gateway = NULL;
109 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
111 if (ipaddress == NULL)
114 g_free(ipaddress->broadcast);
115 g_free(ipaddress->peer);
116 g_free(ipaddress->local);
117 g_free(ipaddress->gateway);
121 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
130 mask = inet_network(netmask);
133 /* a valid netmask must be 2^n - 1 */
134 if ((host & (host + 1)) != 0)
138 for (; mask; mask <<= 1)
144 static gboolean check_ipv6_address(const char *address)
146 unsigned char buf[sizeof(struct in6_addr)];
152 err = inet_pton(AF_INET6, address, buf);
159 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
161 unsigned char prefix_length,
164 if (ipaddress == NULL)
167 if (check_ipv6_address(address) == FALSE)
170 if (check_ipv6_address(gateway) == FALSE)
173 DBG("prefix_len %d address %s gateway %s",
174 prefix_length, address, gateway);
176 ipaddress->family = AF_INET6;
178 ipaddress->prefixlen = prefix_length;
180 g_free(ipaddress->local);
181 ipaddress->local = g_strdup(address);
183 g_free(ipaddress->gateway);
184 ipaddress->gateway = g_strdup(gateway);
189 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
190 const char *address, const char *netmask, const char *gateway)
192 if (ipaddress == NULL)
195 ipaddress->family = AF_INET;
197 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
199 g_free(ipaddress->local);
200 ipaddress->local = g_strdup(address);
202 g_free(ipaddress->gateway);
203 ipaddress->gateway = g_strdup(gateway);
208 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
211 if (ipaddress == NULL)
214 g_free(ipaddress->peer);
215 ipaddress->peer = g_strdup(peer);
218 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
220 if (ipaddress == NULL)
223 ipaddress->prefixlen = 0;
225 g_free(ipaddress->local);
226 ipaddress->local = NULL;
228 g_free(ipaddress->peer);
229 ipaddress->peer = NULL;
231 g_free(ipaddress->broadcast);
232 ipaddress->broadcast = NULL;
234 g_free(ipaddress->gateway);
235 ipaddress->gateway = NULL;
238 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
239 struct connman_ipaddress *source)
241 if (ipaddress == NULL || source == NULL)
244 ipaddress->family = source->family;
245 ipaddress->prefixlen = source->prefixlen;
247 g_free(ipaddress->local);
248 ipaddress->local = g_strdup(source->local);
250 g_free(ipaddress->peer);
251 ipaddress->peer = g_strdup(source->peer);
253 g_free(ipaddress->broadcast);
254 ipaddress->broadcast = g_strdup(source->broadcast);
256 g_free(ipaddress->gateway);
257 ipaddress->gateway = g_strdup(source->gateway);
260 static void free_address_list(struct connman_ipdevice *ipdevice)
264 for (list = ipdevice->address_list; list; list = list->next) {
265 struct connman_ipaddress *ipaddress = list->data;
267 connman_ipaddress_free(ipaddress);
271 g_slist_free(ipdevice->address_list);
272 ipdevice->address_list = NULL;
275 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
276 unsigned char prefixlen, const char *local)
280 for (list = ipdevice->address_list; list; list = list->next) {
281 struct connman_ipaddress *ipaddress = list->data;
283 if (g_strcmp0(ipaddress->local, local) == 0 &&
284 ipaddress->prefixlen == prefixlen)
291 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
294 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
296 case CONNMAN_IPCONFIG_TYPE_IPV4:
298 case CONNMAN_IPCONFIG_TYPE_IPV6:
305 static const char *type2str(unsigned short type)
310 case ARPHRD_LOOPBACK:
323 static const char *scope2str(unsigned char scope)
335 static gboolean get_ipv6_state(gchar *ifname)
340 gboolean enabled = FALSE;
343 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
345 path = g_strdup_printf(
346 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
351 f = fopen(path, "r");
356 if (fscanf(f, "%d", &disabled) > 0)
364 static void set_ipv6_state(gchar *ifname, gboolean enable)
370 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
372 path = g_strdup_printf(
373 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
378 f = fopen(path, "r+");
393 static int get_ipv6_privacy(gchar *ifname)
402 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
408 f = fopen(path, "r");
415 if (fscanf(f, "%d", &value) <= 0)
423 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
424 * The privacy extension is described in RFC 3041 and RFC 4941
426 static void set_ipv6_privacy(gchar *ifname, int value)
434 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
443 f = fopen(path, "r+");
450 fprintf(f, "%d", value);
454 static void free_ipdevice(gpointer data)
456 struct connman_ipdevice *ipdevice = data;
458 connman_info("%s {remove} index %d", ipdevice->ifname,
461 if (ipdevice->config_ipv4 != NULL) {
462 connman_ipconfig_unref(ipdevice->config_ipv4);
463 ipdevice->config_ipv4 = NULL;
466 if (ipdevice->config_ipv6 != NULL) {
467 connman_ipconfig_unref(ipdevice->config_ipv6);
468 ipdevice->config_ipv6 = NULL;
471 free_address_list(ipdevice);
472 g_free(ipdevice->ipv4_gateway);
473 g_free(ipdevice->ipv6_gateway);
474 g_free(ipdevice->pac);
476 g_free(ipdevice->address);
478 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
479 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
481 g_free(ipdevice->ifname);
485 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
487 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
488 ipdevice->config_ipv6);
491 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
493 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
494 ipdevice->config_ipv6);
496 if (ipdevice->config_ipv4)
497 connman_inet_clear_address(ipdevice->index,
498 ipdevice->config_ipv4->address);
500 if (ipdevice->config_ipv6)
501 connman_inet_clear_ipv6_address(ipdevice->index,
502 ipdevice->config_ipv6->address->local,
503 ipdevice->config_ipv6->address->prefixlen);
506 static void update_stats(struct connman_ipdevice *ipdevice,
507 struct rtnl_link_stats *stats)
509 struct connman_service *service;
511 if (stats->rx_packets == 0 && stats->tx_packets == 0)
514 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
515 stats->rx_packets, stats->rx_bytes);
516 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
517 stats->tx_packets, stats->tx_bytes);
519 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
522 if (ipdevice->config_ipv4)
523 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
524 else if (ipdevice->config_ipv6)
525 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
532 ipdevice->rx_packets = stats->rx_packets;
533 ipdevice->tx_packets = stats->tx_packets;
534 ipdevice->rx_bytes = stats->rx_bytes;
535 ipdevice->tx_bytes = stats->tx_bytes;
536 ipdevice->rx_errors = stats->rx_errors;
537 ipdevice->tx_errors = stats->tx_errors;
538 ipdevice->rx_dropped = stats->rx_dropped;
539 ipdevice->tx_dropped = stats->tx_dropped;
541 __connman_service_notify(service,
542 ipdevice->rx_packets, ipdevice->tx_packets,
543 ipdevice->rx_bytes, ipdevice->tx_bytes,
544 ipdevice->rx_errors, ipdevice->tx_errors,
545 ipdevice->rx_dropped, ipdevice->tx_dropped);
548 void __connman_ipconfig_newlink(int index, unsigned short type,
549 unsigned int flags, const char *address,
551 struct rtnl_link_stats *stats)
553 struct connman_ipdevice *ipdevice;
556 gboolean up = FALSE, down = FALSE;
557 gboolean lower_up = FALSE, lower_down = FALSE;
559 DBG("index %d", index);
561 if (type == ARPHRD_LOOPBACK)
564 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
565 if (ipdevice != NULL)
568 ipdevice = g_try_new0(struct connman_ipdevice, 1);
569 if (ipdevice == NULL)
572 ipdevice->index = index;
573 ipdevice->ifname = connman_inet_ifname(index);
574 ipdevice->type = type;
576 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
577 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
579 ipdevice->address = g_strdup(address);
581 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
583 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
584 index, type, type2str(type));
589 update_stats(ipdevice, stats);
591 if (flags == ipdevice->flags)
594 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
601 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
602 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
603 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
604 (IFF_RUNNING | IFF_LOWER_UP))
606 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
610 ipdevice->flags = flags;
612 str = g_string_new(NULL);
617 g_string_append(str, "UP");
619 g_string_append(str, "DOWN");
621 if (flags & IFF_RUNNING)
622 g_string_append(str, ",RUNNING");
624 if (flags & IFF_LOWER_UP)
625 g_string_append(str, ",LOWER_UP");
627 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
630 g_string_free(str, TRUE);
632 for (list = g_list_first(ipconfig_list); list;
633 list = g_list_next(list)) {
634 struct connman_ipconfig *ipconfig = list->data;
636 if (index != ipconfig->index)
639 if (ipconfig->ops == NULL)
642 if (up == TRUE && ipconfig->ops->up)
643 ipconfig->ops->up(ipconfig);
644 if (lower_up == TRUE && ipconfig->ops->lower_up)
645 ipconfig->ops->lower_up(ipconfig);
647 if (lower_down == TRUE && ipconfig->ops->lower_down)
648 ipconfig->ops->lower_down(ipconfig);
649 if (down == TRUE && ipconfig->ops->down)
650 ipconfig->ops->down(ipconfig);
654 __connman_ipconfig_lower_up(ipdevice);
656 __connman_ipconfig_lower_down(ipdevice);
659 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
661 struct connman_ipdevice *ipdevice;
664 DBG("index %d", index);
666 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
667 if (ipdevice == NULL)
670 update_stats(ipdevice, stats);
672 for (list = g_list_first(ipconfig_list); list;
673 list = g_list_next(list)) {
674 struct connman_ipconfig *ipconfig = list->data;
676 if (index != ipconfig->index)
679 ipconfig->index = -1;
681 if (ipconfig->ops == NULL)
684 if (ipconfig->ops->lower_down)
685 ipconfig->ops->lower_down(ipconfig);
686 if (ipconfig->ops->down)
687 ipconfig->ops->down(ipconfig);
690 __connman_ipconfig_lower_down(ipdevice);
692 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
695 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
697 const struct connman_ipaddress *addr1 = a;
698 const struct connman_ipaddress *addr2 = b;
700 if (addr1->prefixlen != addr2->prefixlen)
701 return addr2->prefixlen - addr1->prefixlen;
703 return g_strcmp0(addr1->local, addr2->local);
706 void __connman_ipconfig_newaddr(int index, int family, const char *label,
707 unsigned char prefixlen, const char *address)
709 struct connman_ipdevice *ipdevice;
710 struct connman_ipaddress *ipaddress;
711 enum connman_ipconfig_type type;
714 DBG("index %d", index);
716 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
717 if (ipdevice == NULL)
720 ipaddress = connman_ipaddress_alloc(family);
721 if (ipaddress == NULL)
724 ipaddress->prefixlen = prefixlen;
725 ipaddress->local = g_strdup(address);
727 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
728 check_duplicate_address)) {
729 connman_ipaddress_free(ipaddress);
733 if (family == AF_INET)
734 type = CONNMAN_IPCONFIG_TYPE_IPV4;
735 else if (family == AF_INET6)
736 type = CONNMAN_IPCONFIG_TYPE_IPV6;
740 ipdevice->address_list = g_slist_append(ipdevice->address_list,
743 connman_info("%s {add} address %s/%u label %s family %d",
744 ipdevice->ifname, address, prefixlen, label, family);
746 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
747 connman_ipaddress_copy(ipdevice->config_ipv4->system,
750 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
751 connman_ipaddress_copy(ipdevice->config_ipv6->system,
756 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
759 for (list = g_list_first(ipconfig_list); list;
760 list = g_list_next(list)) {
761 struct connman_ipconfig *ipconfig = list->data;
763 if (index != ipconfig->index)
766 if (type != ipconfig->type)
769 if (ipconfig->ops == NULL)
772 if (ipconfig->ops->ip_bound)
773 ipconfig->ops->ip_bound(ipconfig);
777 void __connman_ipconfig_deladdr(int index, int family, const char *label,
778 unsigned char prefixlen, const char *address)
780 struct connman_ipdevice *ipdevice;
781 struct connman_ipaddress *ipaddress;
782 enum connman_ipconfig_type type;
785 DBG("index %d", index);
787 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
788 if (ipdevice == NULL)
791 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
792 if (ipaddress == NULL)
795 if (family == AF_INET)
796 type = CONNMAN_IPCONFIG_TYPE_IPV4;
797 else if (family == AF_INET6)
798 type = CONNMAN_IPCONFIG_TYPE_IPV6;
802 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
805 connman_ipaddress_clear(ipaddress);
808 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
809 address, prefixlen, label);
811 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
814 if (g_slist_length(ipdevice->address_list) > 0)
817 for (list = g_list_first(ipconfig_list); list;
818 list = g_list_next(list)) {
819 struct connman_ipconfig *ipconfig = list->data;
821 if (index != ipconfig->index)
824 if (type != ipconfig->type)
827 if (ipconfig->ops == NULL)
830 if (ipconfig->ops->ip_release)
831 ipconfig->ops->ip_release(ipconfig);
835 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
836 const char *dst, const char *gateway)
838 struct connman_ipdevice *ipdevice;
840 DBG("index %d", index);
842 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
843 if (ipdevice == NULL)
846 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
847 g_strcmp0(dst, "::") == 0)) {
850 enum connman_ipconfig_type type;
852 if (family == AF_INET6) {
853 type = CONNMAN_IPCONFIG_TYPE_IPV6;
854 g_free(ipdevice->ipv6_gateway);
855 ipdevice->ipv6_gateway = g_strdup(gateway);
857 if (ipdevice->config_ipv6 != NULL &&
858 ipdevice->config_ipv6->system != NULL) {
859 g_free(ipdevice->config_ipv6->system->gateway);
860 ipdevice->config_ipv6->system->gateway =
863 } else if (family == AF_INET) {
864 type = CONNMAN_IPCONFIG_TYPE_IPV4;
865 g_free(ipdevice->ipv4_gateway);
866 ipdevice->ipv4_gateway = g_strdup(gateway);
868 if (ipdevice->config_ipv4 != NULL &&
869 ipdevice->config_ipv4->system != NULL) {
870 g_free(ipdevice->config_ipv4->system->gateway);
871 ipdevice->config_ipv4->system->gateway =
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 = g_strdup(gateway);
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 (type != ipconfig->type)
894 if (ipconfig->ops == NULL)
897 if (ipconfig->ops->ip_bound)
898 ipconfig->ops->ip_bound(ipconfig);
902 connman_info("%s {add} route %s gw %s scope %u <%s>",
903 ipdevice->ifname, dst, gateway,
904 scope, scope2str(scope));
907 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
908 const char *dst, const char *gateway)
910 struct connman_ipdevice *ipdevice;
912 DBG("index %d", index);
914 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
915 if (ipdevice == NULL)
918 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
919 g_strcmp0(dst, "::") == 0)) {
922 enum connman_ipconfig_type type;
924 if (family == AF_INET6) {
925 type = CONNMAN_IPCONFIG_TYPE_IPV6;
926 g_free(ipdevice->ipv6_gateway);
927 ipdevice->ipv6_gateway = NULL;
929 if (ipdevice->config_ipv6 != NULL &&
930 ipdevice->config_ipv6->system != NULL) {
931 g_free(ipdevice->config_ipv6->system->gateway);
932 ipdevice->config_ipv6->system->gateway = NULL;
934 } else if (family == AF_INET) {
935 type = CONNMAN_IPCONFIG_TYPE_IPV4;
936 g_free(ipdevice->ipv4_gateway);
937 ipdevice->ipv4_gateway = NULL;
939 if (ipdevice->config_ipv4 != NULL &&
940 ipdevice->config_ipv4->system != NULL) {
941 g_free(ipdevice->config_ipv4->system->gateway);
942 ipdevice->config_ipv4->system->gateway = NULL;
947 for (list = ipdevice->address_list; list; list = list->next) {
948 struct connman_ipaddress *ipaddress = list->data;
950 g_free(ipaddress->gateway);
951 ipaddress->gateway = NULL;
954 for (config_list = g_list_first(ipconfig_list); config_list;
955 config_list = g_list_next(config_list)) {
956 struct connman_ipconfig *ipconfig = config_list->data;
958 if (index != ipconfig->index)
961 if (type != ipconfig->type)
964 if (ipconfig->ops == NULL)
967 if (ipconfig->ops->ip_release)
968 ipconfig->ops->ip_release(ipconfig);
972 connman_info("%s {del} route %s gw %s scope %u <%s>",
973 ipdevice->ifname, dst, gateway,
974 scope, scope2str(scope));
977 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
982 keys = g_hash_table_get_keys(ipdevice_hash);
986 for (list = g_list_first(keys); list; list = g_list_next(list)) {
987 int index = GPOINTER_TO_INT(list->data);
989 function(index, user_data);
995 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
996 struct connman_ipconfig *ipconfig)
998 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1001 unsigned short __connman_ipconfig_get_type_from_index(int index)
1003 struct connman_ipdevice *ipdevice;
1005 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1006 if (ipdevice == NULL)
1009 return ipdevice->type;
1012 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1014 struct connman_ipdevice *ipdevice;
1016 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1017 if (ipdevice == NULL)
1020 return ipdevice->flags;
1023 const char *__connman_ipconfig_get_gateway_from_index(int index)
1025 struct connman_ipdevice *ipdevice;
1027 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1028 if (ipdevice == NULL)
1031 if (ipdevice->ipv4_gateway != NULL)
1032 return ipdevice->ipv4_gateway;
1034 if (ipdevice->config_ipv4 != NULL &&
1035 ipdevice->config_ipv4->address != NULL)
1036 return ipdevice->config_ipv4->address->gateway;
1038 if (ipdevice->ipv6_gateway != NULL)
1039 return ipdevice->ipv6_gateway;
1041 if (ipdevice->config_ipv6 != NULL &&
1042 ipdevice->config_ipv6->address != NULL)
1043 return ipdevice->config_ipv6->address->gateway;
1048 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1050 ipconfig->index = index;
1053 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1055 if (ipconfig->address == NULL)
1058 return ipconfig->address->local;
1061 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1063 if (ipconfig->address == NULL)
1066 g_free(ipconfig->address->local);
1067 ipconfig->address->local = g_strdup(address);
1070 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1072 if (ipconfig->address == NULL)
1075 return ipconfig->address->peer;
1078 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1080 if (ipconfig->address == NULL)
1083 g_free(ipconfig->address->peer);
1084 ipconfig->address->peer = g_strdup(address);
1087 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1089 if (ipconfig->address == NULL)
1092 return ipconfig->address->broadcast;
1095 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1097 if (ipconfig->address == NULL)
1100 g_free(ipconfig->address->broadcast);
1101 ipconfig->address->broadcast = g_strdup(broadcast);
1104 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1106 if (ipconfig->address == NULL)
1109 return ipconfig->address->gateway;
1112 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1116 if (ipconfig->address == NULL)
1118 g_free(ipconfig->address->gateway);
1119 ipconfig->address->gateway = g_strdup(gateway);
1122 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1124 struct connman_service *service;
1128 if (ipconfig->address == NULL)
1131 service = __connman_service_lookup_from_index(ipconfig->index);
1132 if (service == NULL)
1135 __connman_connection_gateway_remove(service, ipconfig->type);
1137 DBG("type %d gw %s peer %s", ipconfig->type,
1138 ipconfig->address->gateway, ipconfig->address->peer);
1140 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1141 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1142 return __connman_connection_gateway_add(service,
1143 ipconfig->address->gateway,
1145 ipconfig->address->peer);
1150 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1152 struct connman_service *service;
1156 service = __connman_service_lookup_from_index(ipconfig->index);
1157 if (service != NULL)
1158 __connman_connection_gateway_remove(service, ipconfig->type);
1161 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1163 if (ipconfig->address == NULL)
1166 return ipconfig->address->prefixlen;
1169 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1171 if (ipconfig->address == NULL)
1174 ipconfig->address->prefixlen = prefixlen;
1177 static struct connman_ipconfig *create_ipv6config(int index)
1179 struct connman_ipconfig *ipv6config;
1181 DBG("index %d", index);
1183 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1184 if (ipv6config == NULL)
1187 ipv6config->refcount = 1;
1189 ipv6config->index = index;
1190 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1191 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1192 ipv6config->ipv6_privacy_config = 0;
1194 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1195 if (ipv6config->address == NULL) {
1200 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1202 DBG("ipconfig %p", ipv6config);
1208 * connman_ipconfig_create:
1210 * Allocate a new ipconfig structure.
1212 * Returns: a newly-allocated #connman_ipconfig structure
1214 struct connman_ipconfig *connman_ipconfig_create(int index,
1215 enum connman_ipconfig_type type)
1217 struct connman_ipconfig *ipconfig;
1219 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1220 return create_ipv6config(index);
1222 DBG("index %d", index);
1224 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1225 if (ipconfig == NULL)
1228 ipconfig->refcount = 1;
1230 ipconfig->index = index;
1231 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1233 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1234 if (ipconfig->address == NULL) {
1239 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1241 DBG("ipconfig %p", ipconfig);
1248 * connman_ipconfig_ref:
1249 * @ipconfig: ipconfig structure
1251 * Increase reference counter of ipconfig
1253 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1255 DBG("ipconfig %p refcount %d", ipconfig,
1256 g_atomic_int_get(&ipconfig->refcount) + 1);
1258 g_atomic_int_inc(&ipconfig->refcount);
1264 * connman_ipconfig_unref:
1265 * @ipconfig: ipconfig structure
1267 * Decrease reference counter of ipconfig
1269 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1271 if (ipconfig == NULL)
1274 DBG("ipconfig %p refcount %d", ipconfig,
1275 g_atomic_int_get(&ipconfig->refcount) - 1);
1277 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1278 __connman_ipconfig_disable(ipconfig);
1280 connman_ipconfig_set_ops(ipconfig, NULL);
1282 if (ipconfig->origin != NULL) {
1283 connman_ipconfig_unref(ipconfig->origin);
1284 ipconfig->origin = NULL;
1287 connman_ipaddress_free(ipconfig->system);
1288 connman_ipaddress_free(ipconfig->address);
1294 * connman_ipconfig_get_data:
1295 * @ipconfig: ipconfig structure
1297 * Get private data pointer
1299 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1301 if (ipconfig == NULL)
1304 return ipconfig->ops_data;
1308 * connman_ipconfig_set_data:
1309 * @ipconfig: ipconfig structure
1310 * @data: data pointer
1312 * Set private data pointer
1314 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1316 ipconfig->ops_data = data;
1320 * connman_ipconfig_get_index:
1321 * @ipconfig: ipconfig structure
1323 * Get interface index
1325 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1327 if (ipconfig == NULL)
1330 if (ipconfig->origin != NULL)
1331 return ipconfig->origin->index;
1333 return ipconfig->index;
1337 * connman_ipconfig_get_ifname:
1338 * @ipconfig: ipconfig structure
1340 * Get interface name
1342 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1344 struct connman_ipdevice *ipdevice;
1346 if (ipconfig == NULL)
1349 if (ipconfig->index < 0)
1352 ipdevice = g_hash_table_lookup(ipdevice_hash,
1353 GINT_TO_POINTER(ipconfig->index));
1354 if (ipdevice == NULL)
1357 return ipdevice->ifname;
1361 * connman_ipconfig_set_ops:
1362 * @ipconfig: ipconfig structure
1363 * @ops: operation callbacks
1365 * Set the operation callbacks
1367 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1368 const struct connman_ipconfig_ops *ops)
1370 ipconfig->ops = ops;
1374 * connman_ipconfig_set_method:
1375 * @ipconfig: ipconfig structure
1376 * @method: configuration method
1378 * Set the configuration method
1380 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1381 enum connman_ipconfig_method method)
1383 ipconfig->method = method;
1388 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1390 if (ipconfig == NULL)
1391 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1393 return ipconfig->method;
1396 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1400 switch (ipconfig->method) {
1401 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1402 case CONNMAN_IPCONFIG_METHOD_OFF:
1403 case CONNMAN_IPCONFIG_METHOD_AUTO:
1405 case CONNMAN_IPCONFIG_METHOD_FIXED:
1406 case CONNMAN_IPCONFIG_METHOD_DHCP:
1407 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1408 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1409 return connman_inet_set_address(ipconfig->index,
1411 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1412 return connman_inet_set_ipv6_address(
1413 ipconfig->index, ipconfig->address);
1419 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1425 if (ipconfig == NULL)
1428 DBG("method %d", ipconfig->method);
1430 switch (ipconfig->method) {
1431 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1432 case CONNMAN_IPCONFIG_METHOD_OFF:
1433 case CONNMAN_IPCONFIG_METHOD_AUTO:
1435 case CONNMAN_IPCONFIG_METHOD_FIXED:
1436 case CONNMAN_IPCONFIG_METHOD_DHCP:
1437 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1438 err = __connman_ipconfig_address_unset(ipconfig);
1439 connman_ipaddress_clear(ipconfig->address);
1447 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1453 if (ipconfig == NULL)
1456 DBG("method %d", ipconfig->method);
1458 switch (ipconfig->method) {
1459 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1460 case CONNMAN_IPCONFIG_METHOD_OFF:
1461 case CONNMAN_IPCONFIG_METHOD_AUTO:
1463 case CONNMAN_IPCONFIG_METHOD_FIXED:
1464 case CONNMAN_IPCONFIG_METHOD_DHCP:
1465 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1466 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1467 err = connman_inet_clear_address(ipconfig->index,
1469 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1470 err = connman_inet_clear_ipv6_address(
1472 ipconfig->address->local,
1473 ipconfig->address->prefixlen);
1483 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1486 struct connman_ipdevice *ipdevice;
1488 DBG("ipconfig %p", ipconfig);
1490 if (ipconfig == NULL || ipconfig->index < 0)
1493 ipdevice = g_hash_table_lookup(ipdevice_hash,
1494 GINT_TO_POINTER(ipconfig->index));
1495 if (ipdevice == NULL)
1498 g_free(ipdevice->pac);
1499 ipdevice->pac = g_strdup(url);
1504 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1506 struct connman_ipdevice *ipdevice;
1508 DBG("ipconfig %p", ipconfig);
1510 if (ipconfig == NULL || ipconfig->index < 0)
1513 ipdevice = g_hash_table_lookup(ipdevice_hash,
1514 GINT_TO_POINTER(ipconfig->index));
1515 if (ipdevice == NULL)
1518 return ipdevice->pac;
1521 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1523 struct connman_ipdevice *ipdevice;
1527 ipdevice = g_hash_table_lookup(ipdevice_hash,
1528 GINT_TO_POINTER(ipconfig->index));
1529 if (ipdevice == NULL)
1532 set_ipv6_state(ipdevice->ifname, FALSE);
1535 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1537 struct connman_ipdevice *ipdevice;
1541 ipdevice = g_hash_table_lookup(ipdevice_hash,
1542 GINT_TO_POINTER(ipconfig->index));
1543 if (ipdevice == NULL)
1546 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1547 set_ipv6_privacy(ipdevice->ifname,
1548 ipconfig->ipv6_privacy_config);
1550 set_ipv6_state(ipdevice->ifname, TRUE);
1553 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1555 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1558 enable_ipv6(ipconfig);
1561 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1563 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1566 disable_ipv6(ipconfig);
1569 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1571 struct connman_ipdevice *ipdevice;
1572 gboolean up = FALSE, down = FALSE;
1573 gboolean lower_up = FALSE, lower_down = FALSE;
1574 enum connman_ipconfig_type type;
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 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1587 if (ipdevice->config_ipv4 == ipconfig)
1589 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1590 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1591 if (ipdevice->config_ipv6 == ipconfig)
1593 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1594 enable_ipv6(ipconfig);
1598 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1599 ipdevice->config_ipv4 != NULL) {
1600 ipconfig_list = g_list_remove(ipconfig_list,
1601 ipdevice->config_ipv4);
1603 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1605 connman_ipconfig_unref(ipdevice->config_ipv4);
1608 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1609 ipdevice->config_ipv6 != NULL) {
1610 ipconfig_list = g_list_remove(ipconfig_list,
1611 ipdevice->config_ipv6);
1613 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1615 connman_ipconfig_unref(ipdevice->config_ipv6);
1618 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1619 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1620 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1621 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1623 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1625 if (ipdevice->flags & IFF_UP)
1630 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1631 (IFF_RUNNING | IFF_LOWER_UP))
1633 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1636 if (up == TRUE && ipconfig->ops->up)
1637 ipconfig->ops->up(ipconfig);
1638 if (lower_up == TRUE && ipconfig->ops->lower_up)
1639 ipconfig->ops->lower_up(ipconfig);
1641 if (lower_down == TRUE && ipconfig->ops->lower_down)
1642 ipconfig->ops->lower_down(ipconfig);
1643 if (down == TRUE && ipconfig->ops->down)
1644 ipconfig->ops->down(ipconfig);
1649 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1651 struct connman_ipdevice *ipdevice;
1653 DBG("ipconfig %p", ipconfig);
1655 if (ipconfig == NULL || ipconfig->index < 0)
1658 ipdevice = g_hash_table_lookup(ipdevice_hash,
1659 GINT_TO_POINTER(ipconfig->index));
1660 if (ipdevice == NULL)
1663 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1666 if (ipdevice->config_ipv4 == ipconfig) {
1667 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1669 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1670 connman_ipconfig_unref(ipdevice->config_ipv4);
1671 ipdevice->config_ipv4 = NULL;
1675 if (ipdevice->config_ipv6 == ipconfig) {
1676 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1678 if (ipdevice->config_ipv6->method ==
1679 CONNMAN_IPCONFIG_METHOD_AUTO)
1680 disable_ipv6(ipdevice->config_ipv6);
1682 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1683 connman_ipconfig_unref(ipdevice->config_ipv6);
1684 ipdevice->config_ipv6 = NULL;
1691 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1694 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1696 case CONNMAN_IPCONFIG_METHOD_OFF:
1698 case CONNMAN_IPCONFIG_METHOD_FIXED:
1700 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1702 case CONNMAN_IPCONFIG_METHOD_DHCP:
1704 case CONNMAN_IPCONFIG_METHOD_AUTO:
1711 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1713 if (g_strcmp0(method, "off") == 0)
1714 return CONNMAN_IPCONFIG_METHOD_OFF;
1715 else if (g_strcmp0(method, "fixed") == 0)
1716 return CONNMAN_IPCONFIG_METHOD_FIXED;
1717 else if (g_strcmp0(method, "manual") == 0)
1718 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1719 else if (g_strcmp0(method, "dhcp") == 0)
1720 return CONNMAN_IPCONFIG_METHOD_DHCP;
1721 else if (g_strcmp0(method, "auto") == 0)
1722 return CONNMAN_IPCONFIG_METHOD_AUTO;
1724 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1727 static const char *privacy2string(int privacy)
1731 else if (privacy == 1)
1733 else if (privacy > 1)
1739 static int string2privacy(const char *privacy)
1741 if (g_strcmp0(privacy, "disabled") == 0)
1743 else if (g_strcmp0(privacy, "enabled") == 0)
1745 else if (g_strcmp0(privacy, "prefered") == 0)
1751 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1752 DBusMessageIter *iter)
1758 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1761 str = __connman_ipconfig_method2string(ipconfig->method);
1765 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1767 if (ipconfig->system == NULL)
1770 if (ipconfig->system->local != NULL) {
1772 struct in_addr netmask;
1775 connman_dbus_dict_append_basic(iter, "Address",
1776 DBUS_TYPE_STRING, &ipconfig->system->local);
1778 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1779 netmask.s_addr = htonl(addr);
1780 mask = inet_ntoa(netmask);
1781 connman_dbus_dict_append_basic(iter, "Netmask",
1782 DBUS_TYPE_STRING, &mask);
1785 if (ipconfig->system->gateway != NULL)
1786 connman_dbus_dict_append_basic(iter, "Gateway",
1787 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1790 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1791 DBusMessageIter *iter,
1792 struct connman_ipconfig *ipconfig_ipv4)
1794 const char *str, *privacy;
1798 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1801 str = __connman_ipconfig_method2string(ipconfig->method);
1805 if (ipconfig_ipv4 != NULL &&
1806 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1807 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1811 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1813 if (ipconfig->system == NULL)
1816 if (ipconfig->system->local != NULL) {
1817 connman_dbus_dict_append_basic(iter, "Address",
1818 DBUS_TYPE_STRING, &ipconfig->system->local);
1819 connman_dbus_dict_append_basic(iter, "PrefixLength",
1821 &ipconfig->system->prefixlen);
1824 if (ipconfig->system->gateway != NULL)
1825 connman_dbus_dict_append_basic(iter, "Gateway",
1826 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1828 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1829 connman_dbus_dict_append_basic(iter, "Privacy",
1830 DBUS_TYPE_STRING, &privacy);
1833 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1834 DBusMessageIter *iter)
1836 const char *str, *privacy;
1840 str = __connman_ipconfig_method2string(ipconfig->method);
1844 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1846 switch (ipconfig->method) {
1847 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1848 case CONNMAN_IPCONFIG_METHOD_OFF:
1849 case CONNMAN_IPCONFIG_METHOD_DHCP:
1851 case CONNMAN_IPCONFIG_METHOD_FIXED:
1852 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1853 case CONNMAN_IPCONFIG_METHOD_AUTO:
1857 if (ipconfig->address == NULL)
1860 if (ipconfig->address->local != NULL) {
1861 connman_dbus_dict_append_basic(iter, "Address",
1862 DBUS_TYPE_STRING, &ipconfig->address->local);
1863 connman_dbus_dict_append_basic(iter, "PrefixLength",
1865 &ipconfig->address->prefixlen);
1868 if (ipconfig->address->gateway != NULL)
1869 connman_dbus_dict_append_basic(iter, "Gateway",
1870 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1872 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1873 connman_dbus_dict_append_basic(iter, "Privacy",
1874 DBUS_TYPE_STRING, &privacy);
1877 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1878 DBusMessageIter *iter)
1884 str = __connman_ipconfig_method2string(ipconfig->method);
1888 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1890 switch (ipconfig->method) {
1891 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1892 case CONNMAN_IPCONFIG_METHOD_OFF:
1893 case CONNMAN_IPCONFIG_METHOD_FIXED:
1894 case CONNMAN_IPCONFIG_METHOD_DHCP:
1895 case CONNMAN_IPCONFIG_METHOD_AUTO:
1897 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1901 if (ipconfig->address == NULL)
1904 if (ipconfig->address->local != NULL) {
1906 struct in_addr netmask;
1909 connman_dbus_dict_append_basic(iter, "Address",
1910 DBUS_TYPE_STRING, &ipconfig->address->local);
1912 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1913 netmask.s_addr = htonl(addr);
1914 mask = inet_ntoa(netmask);
1915 connman_dbus_dict_append_basic(iter, "Netmask",
1916 DBUS_TYPE_STRING, &mask);
1919 if (ipconfig->address->gateway != NULL)
1920 connman_dbus_dict_append_basic(iter, "Gateway",
1921 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1924 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1925 DBusMessageIter *array)
1927 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1928 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1929 *prefix_length_string = NULL, *privacy_string = NULL;
1930 int prefix_length = 0, privacy = 0;
1931 DBusMessageIter dict;
1933 DBG("ipconfig %p", ipconfig);
1935 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1938 dbus_message_iter_recurse(array, &dict);
1940 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1941 DBusMessageIter entry;
1945 dbus_message_iter_recurse(&dict, &entry);
1947 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1950 dbus_message_iter_get_basic(&entry, &key);
1951 dbus_message_iter_next(&entry);
1953 type = dbus_message_iter_get_arg_type(&entry);
1955 if (g_str_equal(key, "Method") == TRUE) {
1958 if (type != DBUS_TYPE_STRING)
1961 dbus_message_iter_get_basic(&entry, &str);
1962 method = __connman_ipconfig_string2method(str);
1963 } else if (g_str_equal(key, "Address") == TRUE) {
1964 if (type != DBUS_TYPE_STRING)
1967 dbus_message_iter_get_basic(&entry, &address);
1968 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1969 if (type != DBUS_TYPE_STRING)
1972 dbus_message_iter_get_basic(&entry,
1973 &prefix_length_string);
1975 prefix_length = atoi(prefix_length_string);
1976 if (prefix_length < 0 || prefix_length > 128)
1979 } else if (g_str_equal(key, "Netmask") == TRUE) {
1980 if (type != DBUS_TYPE_STRING)
1983 dbus_message_iter_get_basic(&entry, &netmask);
1984 } else if (g_str_equal(key, "Gateway") == TRUE) {
1985 if (type != DBUS_TYPE_STRING)
1988 dbus_message_iter_get_basic(&entry, &gateway);
1989 } else if (g_str_equal(key, "Privacy") == TRUE) {
1990 if (type != DBUS_TYPE_STRING)
1993 dbus_message_iter_get_basic(&entry, &privacy_string);
1994 privacy = string2privacy(privacy_string);
1996 dbus_message_iter_next(&dict);
1999 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2001 method, address, netmask, gateway, prefix_length,
2005 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2006 case CONNMAN_IPCONFIG_METHOD_FIXED:
2009 case CONNMAN_IPCONFIG_METHOD_OFF:
2010 ipconfig->method = method;
2011 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2012 disable_ipv6(ipconfig);
2015 case CONNMAN_IPCONFIG_METHOD_AUTO:
2016 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2019 ipconfig->method = method;
2020 if (privacy_string != NULL)
2021 ipconfig->ipv6_privacy_config = privacy;
2022 enable_ipv6(ipconfig);
2025 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2026 if (address == NULL)
2029 ipconfig->method = method;
2031 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2032 connman_ipaddress_set_ipv4(ipconfig->address,
2033 address, netmask, gateway);
2035 return connman_ipaddress_set_ipv6(
2036 ipconfig->address, address,
2037 prefix_length, gateway);
2040 case CONNMAN_IPCONFIG_METHOD_DHCP:
2041 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2044 ipconfig->method = method;
2051 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2052 DBusMessageIter *iter)
2054 struct connman_ipdevice *ipdevice;
2055 const char *method = "auto";
2057 connman_dbus_dict_append_basic(iter, "Method",
2058 DBUS_TYPE_STRING, &method);
2060 ipdevice = g_hash_table_lookup(ipdevice_hash,
2061 GINT_TO_POINTER(ipconfig->index));
2062 if (ipdevice == NULL)
2065 if (ipdevice->ifname != NULL)
2066 connman_dbus_dict_append_basic(iter, "Interface",
2067 DBUS_TYPE_STRING, &ipdevice->ifname);
2069 if (ipdevice->address != NULL)
2070 connman_dbus_dict_append_basic(iter, "Address",
2071 DBUS_TYPE_STRING, &ipdevice->address);
2073 if (ipdevice->mtu > 0)
2074 connman_dbus_dict_append_basic(iter, "MTU",
2075 DBUS_TYPE_UINT16, &ipdevice->mtu);
2078 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2079 GKeyFile *keyfile, const char *identifier, const char *prefix)
2084 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2086 key = g_strdup_printf("%smethod", prefix);
2087 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2088 if (method == NULL) {
2089 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2090 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2092 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2094 ipconfig->method = __connman_ipconfig_string2method(method);
2096 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2097 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2099 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2100 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2101 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2103 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2104 privacy = g_key_file_get_string(keyfile, identifier,
2106 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2110 __connman_ipconfig_enable(ipconfig);
2111 enable_ipv6(ipconfig);
2118 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2119 ipconfig->address->prefixlen = g_key_file_get_integer(
2120 keyfile, identifier, key, NULL);
2123 key = g_strdup_printf("%slocal_address", prefix);
2124 ipconfig->address->local = g_key_file_get_string(
2125 keyfile, identifier, key, NULL);
2128 key = g_strdup_printf("%speer_address", prefix);
2129 ipconfig->address->peer = g_key_file_get_string(
2130 keyfile, identifier, key, NULL);
2133 key = g_strdup_printf("%sbroadcast_address", prefix);
2134 ipconfig->address->broadcast = g_key_file_get_string(
2135 keyfile, identifier, key, NULL);
2138 key = g_strdup_printf("%sgateway", prefix);
2139 ipconfig->address->gateway = g_key_file_get_string(
2140 keyfile, identifier, key, NULL);
2146 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2147 GKeyFile *keyfile, const char *identifier, const char *prefix)
2152 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2154 method = __connman_ipconfig_method2string(ipconfig->method);
2156 key = g_strdup_printf("%smethod", prefix);
2157 g_key_file_set_string(keyfile, identifier, key, method);
2160 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2161 const char *privacy;
2162 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2163 key = g_strdup_printf("%sprivacy", prefix);
2164 g_key_file_set_string(keyfile, identifier, key, privacy);
2168 switch (ipconfig->method) {
2169 case CONNMAN_IPCONFIG_METHOD_FIXED:
2170 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2172 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2173 case CONNMAN_IPCONFIG_METHOD_OFF:
2174 case CONNMAN_IPCONFIG_METHOD_DHCP:
2175 case CONNMAN_IPCONFIG_METHOD_AUTO:
2179 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2180 g_key_file_set_integer(keyfile, identifier,
2181 key, ipconfig->address->prefixlen);
2184 key = g_strdup_printf("%slocal_address", prefix);
2185 if (ipconfig->address->local != NULL)
2186 g_key_file_set_string(keyfile, identifier,
2187 key, ipconfig->address->local);
2190 key = g_strdup_printf("%speer_address", prefix);
2191 if (ipconfig->address->peer != NULL)
2192 g_key_file_set_string(keyfile, identifier,
2193 key, ipconfig->address->peer);
2196 key = g_strdup_printf("%sbroadcast_address", prefix);
2197 if (ipconfig->address->broadcast != NULL)
2198 g_key_file_set_string(keyfile, identifier,
2199 key, ipconfig->address->broadcast);
2202 key = g_strdup_printf("%sgateway", prefix);
2203 if (ipconfig->address->gateway != NULL)
2204 g_key_file_set_string(keyfile, identifier,
2205 key, ipconfig->address->gateway);
2211 int __connman_ipconfig_init(void)
2215 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2216 NULL, free_ipdevice);
2221 void __connman_ipconfig_cleanup(void)
2225 g_hash_table_destroy(ipdevice_hash);
2226 ipdevice_hash = NULL;