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;
713 DBG("index %d", index);
715 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
716 if (ipdevice == NULL)
719 ipaddress = connman_ipaddress_alloc(family);
720 if (ipaddress == NULL)
723 ipaddress->prefixlen = prefixlen;
724 ipaddress->local = g_strdup(address);
726 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
727 check_duplicate_address)) {
728 connman_ipaddress_free(ipaddress);
732 ipdevice->address_list = g_slist_append(ipdevice->address_list,
735 connman_info("%s {add} address %s/%u label %s family %d",
736 ipdevice->ifname, address, prefixlen, label, family);
738 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
739 connman_ipaddress_copy(ipdevice->config_ipv4->system,
742 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
743 connman_ipaddress_copy(ipdevice->config_ipv6->system,
748 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
751 for (list = g_list_first(ipconfig_list); list;
752 list = g_list_next(list)) {
753 struct connman_ipconfig *ipconfig = list->data;
755 if (index != ipconfig->index)
758 if (ipconfig->ops == NULL)
761 if (ipconfig->ops->ip_bound)
762 ipconfig->ops->ip_bound(ipconfig);
766 void __connman_ipconfig_deladdr(int index, int family, const char *label,
767 unsigned char prefixlen, const char *address)
769 struct connman_ipdevice *ipdevice;
770 struct connman_ipaddress *ipaddress;
773 DBG("index %d", index);
775 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
776 if (ipdevice == NULL)
779 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
780 if (ipaddress == NULL)
783 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
786 connman_ipaddress_clear(ipaddress);
789 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
790 address, prefixlen, label);
792 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
795 if (g_slist_length(ipdevice->address_list) > 0)
798 for (list = g_list_first(ipconfig_list); list;
799 list = g_list_next(list)) {
800 struct connman_ipconfig *ipconfig = list->data;
802 if (index != ipconfig->index)
805 if (ipconfig->ops == NULL)
808 if (ipconfig->ops->ip_release)
809 ipconfig->ops->ip_release(ipconfig);
813 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
814 const char *dst, const char *gateway)
816 struct connman_ipdevice *ipdevice;
818 DBG("index %d", index);
820 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
821 if (ipdevice == NULL)
824 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
825 g_strcmp0(dst, "::") == 0)) {
829 if (family == AF_INET6) {
830 g_free(ipdevice->ipv6_gateway);
831 ipdevice->ipv6_gateway = g_strdup(gateway);
833 if (ipdevice->config_ipv6 != NULL &&
834 ipdevice->config_ipv6->system != NULL) {
835 g_free(ipdevice->config_ipv6->system->gateway);
836 ipdevice->config_ipv6->system->gateway =
839 } else if (family == AF_INET) {
840 g_free(ipdevice->ipv4_gateway);
841 ipdevice->ipv4_gateway = g_strdup(gateway);
843 if (ipdevice->config_ipv4 != NULL &&
844 ipdevice->config_ipv4->system != NULL) {
845 g_free(ipdevice->config_ipv4->system->gateway);
846 ipdevice->config_ipv4->system->gateway =
852 for (list = ipdevice->address_list; list; list = list->next) {
853 struct connman_ipaddress *ipaddress = list->data;
855 g_free(ipaddress->gateway);
856 ipaddress->gateway = g_strdup(gateway);
859 for (config_list = g_list_first(ipconfig_list); config_list;
860 config_list = g_list_next(config_list)) {
861 struct connman_ipconfig *ipconfig = config_list->data;
863 if (index != ipconfig->index)
866 if (ipconfig->ops == NULL)
869 if (ipconfig->ops->ip_bound)
870 ipconfig->ops->ip_bound(ipconfig);
874 connman_info("%s {add} route %s gw %s scope %u <%s>",
875 ipdevice->ifname, dst, gateway,
876 scope, scope2str(scope));
879 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
880 const char *dst, const char *gateway)
882 struct connman_ipdevice *ipdevice;
884 DBG("index %d", index);
886 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
887 if (ipdevice == NULL)
890 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
891 g_strcmp0(dst, "::") == 0)) {
895 if (family == AF_INET6) {
896 g_free(ipdevice->ipv6_gateway);
897 ipdevice->ipv6_gateway = NULL;
899 if (ipdevice->config_ipv6 != NULL &&
900 ipdevice->config_ipv6->system != NULL) {
901 g_free(ipdevice->config_ipv6->system->gateway);
902 ipdevice->config_ipv6->system->gateway = NULL;
904 } else if (family == AF_INET) {
905 g_free(ipdevice->ipv4_gateway);
906 ipdevice->ipv4_gateway = NULL;
908 if (ipdevice->config_ipv4 != NULL &&
909 ipdevice->config_ipv4->system != NULL) {
910 g_free(ipdevice->config_ipv4->system->gateway);
911 ipdevice->config_ipv4->system->gateway = NULL;
916 for (list = ipdevice->address_list; list; list = list->next) {
917 struct connman_ipaddress *ipaddress = list->data;
919 g_free(ipaddress->gateway);
920 ipaddress->gateway = NULL;
923 for (config_list = g_list_first(ipconfig_list); config_list;
924 config_list = g_list_next(config_list)) {
925 struct connman_ipconfig *ipconfig = config_list->data;
927 if (index != ipconfig->index)
930 if (ipconfig->ops == NULL)
933 if (ipconfig->ops->ip_release)
934 ipconfig->ops->ip_release(ipconfig);
938 connman_info("%s {del} route %s gw %s scope %u <%s>",
939 ipdevice->ifname, dst, gateway,
940 scope, scope2str(scope));
943 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
948 keys = g_hash_table_get_keys(ipdevice_hash);
952 for (list = g_list_first(keys); list; list = g_list_next(list)) {
953 int index = GPOINTER_TO_INT(list->data);
955 function(index, user_data);
961 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
962 struct connman_ipconfig *ipconfig)
964 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
967 unsigned short __connman_ipconfig_get_type_from_index(int index)
969 struct connman_ipdevice *ipdevice;
971 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
972 if (ipdevice == NULL)
975 return ipdevice->type;
978 unsigned int __connman_ipconfig_get_flags_from_index(int index)
980 struct connman_ipdevice *ipdevice;
982 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
983 if (ipdevice == NULL)
986 return ipdevice->flags;
989 const char *__connman_ipconfig_get_gateway_from_index(int index)
991 struct connman_ipdevice *ipdevice;
993 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
994 if (ipdevice == NULL)
997 if (ipdevice->ipv4_gateway != NULL)
998 return ipdevice->ipv4_gateway;
1000 if (ipdevice->config_ipv4 != NULL &&
1001 ipdevice->config_ipv4->address != NULL)
1002 return ipdevice->config_ipv4->address->gateway;
1004 if (ipdevice->ipv6_gateway != NULL)
1005 return ipdevice->ipv6_gateway;
1007 if (ipdevice->config_ipv6 != NULL &&
1008 ipdevice->config_ipv6->address != NULL)
1009 return ipdevice->config_ipv6->address->gateway;
1014 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1016 ipconfig->index = index;
1019 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1021 if (ipconfig->address == NULL)
1024 return ipconfig->address->local;
1027 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1029 if (ipconfig->address == NULL)
1032 g_free(ipconfig->address->local);
1033 ipconfig->address->local = g_strdup(address);
1036 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1038 if (ipconfig->address == NULL)
1041 return ipconfig->address->peer;
1044 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1046 if (ipconfig->address == NULL)
1049 g_free(ipconfig->address->peer);
1050 ipconfig->address->peer = g_strdup(address);
1053 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1055 if (ipconfig->address == NULL)
1058 return ipconfig->address->broadcast;
1061 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1063 if (ipconfig->address == NULL)
1066 g_free(ipconfig->address->broadcast);
1067 ipconfig->address->broadcast = g_strdup(broadcast);
1070 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1072 if (ipconfig->address == NULL)
1075 return ipconfig->address->gateway;
1078 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1082 if (ipconfig->address == NULL)
1084 g_free(ipconfig->address->gateway);
1085 ipconfig->address->gateway = g_strdup(gateway);
1088 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1090 struct connman_service *service;
1094 if (ipconfig->address == NULL)
1097 service = __connman_service_lookup_from_index(ipconfig->index);
1098 if (service == NULL)
1101 __connman_connection_gateway_remove(service, ipconfig->type);
1103 DBG("type %d gw %s peer %s", ipconfig->type,
1104 ipconfig->address->gateway, ipconfig->address->peer);
1106 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1107 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1108 return __connman_connection_gateway_add(service,
1109 ipconfig->address->gateway,
1111 ipconfig->address->peer);
1116 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1118 struct connman_service *service;
1122 service = __connman_service_lookup_from_index(ipconfig->index);
1123 if (service != NULL)
1124 __connman_connection_gateway_remove(service, ipconfig->type);
1127 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1129 if (ipconfig->address == NULL)
1132 return ipconfig->address->prefixlen;
1135 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1137 if (ipconfig->address == NULL)
1140 ipconfig->address->prefixlen = prefixlen;
1143 static struct connman_ipconfig *create_ipv6config(int index)
1145 struct connman_ipconfig *ipv6config;
1147 DBG("index %d", index);
1149 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1150 if (ipv6config == NULL)
1153 ipv6config->refcount = 1;
1155 ipv6config->index = index;
1156 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1157 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1158 ipv6config->ipv6_privacy_config = 0;
1160 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1161 if (ipv6config->address == NULL) {
1166 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1168 DBG("ipconfig %p", ipv6config);
1174 * connman_ipconfig_create:
1176 * Allocate a new ipconfig structure.
1178 * Returns: a newly-allocated #connman_ipconfig structure
1180 struct connman_ipconfig *connman_ipconfig_create(int index,
1181 enum connman_ipconfig_type type)
1183 struct connman_ipconfig *ipconfig;
1185 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1186 return create_ipv6config(index);
1188 DBG("index %d", index);
1190 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1191 if (ipconfig == NULL)
1194 ipconfig->refcount = 1;
1196 ipconfig->index = index;
1197 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1199 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1200 if (ipconfig->address == NULL) {
1205 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1207 DBG("ipconfig %p", ipconfig);
1214 * connman_ipconfig_ref:
1215 * @ipconfig: ipconfig structure
1217 * Increase reference counter of ipconfig
1219 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1221 DBG("ipconfig %p refcount %d", ipconfig,
1222 g_atomic_int_get(&ipconfig->refcount) + 1);
1224 g_atomic_int_inc(&ipconfig->refcount);
1230 * connman_ipconfig_unref:
1231 * @ipconfig: ipconfig structure
1233 * Decrease reference counter of ipconfig
1235 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1237 if (ipconfig == NULL)
1240 DBG("ipconfig %p refcount %d", ipconfig,
1241 g_atomic_int_get(&ipconfig->refcount) - 1);
1243 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1244 __connman_ipconfig_disable(ipconfig);
1246 connman_ipconfig_set_ops(ipconfig, NULL);
1248 if (ipconfig->origin != NULL) {
1249 connman_ipconfig_unref(ipconfig->origin);
1250 ipconfig->origin = NULL;
1253 connman_ipaddress_free(ipconfig->system);
1254 connman_ipaddress_free(ipconfig->address);
1260 * connman_ipconfig_get_data:
1261 * @ipconfig: ipconfig structure
1263 * Get private data pointer
1265 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1267 if (ipconfig == NULL)
1270 return ipconfig->ops_data;
1274 * connman_ipconfig_set_data:
1275 * @ipconfig: ipconfig structure
1276 * @data: data pointer
1278 * Set private data pointer
1280 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1282 ipconfig->ops_data = data;
1286 * connman_ipconfig_get_index:
1287 * @ipconfig: ipconfig structure
1289 * Get interface index
1291 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1293 if (ipconfig == NULL)
1296 if (ipconfig->origin != NULL)
1297 return ipconfig->origin->index;
1299 return ipconfig->index;
1303 * connman_ipconfig_get_ifname:
1304 * @ipconfig: ipconfig structure
1306 * Get interface name
1308 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1310 struct connman_ipdevice *ipdevice;
1312 if (ipconfig == NULL)
1315 if (ipconfig->index < 0)
1318 ipdevice = g_hash_table_lookup(ipdevice_hash,
1319 GINT_TO_POINTER(ipconfig->index));
1320 if (ipdevice == NULL)
1323 return ipdevice->ifname;
1327 * connman_ipconfig_set_ops:
1328 * @ipconfig: ipconfig structure
1329 * @ops: operation callbacks
1331 * Set the operation callbacks
1333 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1334 const struct connman_ipconfig_ops *ops)
1336 ipconfig->ops = ops;
1340 * connman_ipconfig_set_method:
1341 * @ipconfig: ipconfig structure
1342 * @method: configuration method
1344 * Set the configuration method
1346 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1347 enum connman_ipconfig_method method)
1349 ipconfig->method = method;
1354 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1356 if (ipconfig == NULL)
1357 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1359 return ipconfig->method;
1362 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1366 switch (ipconfig->method) {
1367 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1368 case CONNMAN_IPCONFIG_METHOD_OFF:
1369 case CONNMAN_IPCONFIG_METHOD_AUTO:
1371 case CONNMAN_IPCONFIG_METHOD_FIXED:
1372 case CONNMAN_IPCONFIG_METHOD_DHCP:
1373 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1374 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1375 return connman_inet_set_address(ipconfig->index,
1377 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1378 return connman_inet_set_ipv6_address(
1379 ipconfig->index, ipconfig->address);
1385 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1391 if (ipconfig == NULL)
1394 DBG("method %d", ipconfig->method);
1396 switch (ipconfig->method) {
1397 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1398 case CONNMAN_IPCONFIG_METHOD_OFF:
1399 case CONNMAN_IPCONFIG_METHOD_AUTO:
1401 case CONNMAN_IPCONFIG_METHOD_FIXED:
1402 case CONNMAN_IPCONFIG_METHOD_DHCP:
1403 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1404 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1405 err = connman_inet_clear_address(ipconfig->index,
1407 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1408 err = connman_inet_clear_ipv6_address(
1410 ipconfig->address->local,
1411 ipconfig->address->prefixlen);
1415 connman_ipaddress_clear(ipconfig->address);
1423 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1426 struct connman_ipdevice *ipdevice;
1428 DBG("ipconfig %p", ipconfig);
1430 if (ipconfig == NULL || ipconfig->index < 0)
1433 ipdevice = g_hash_table_lookup(ipdevice_hash,
1434 GINT_TO_POINTER(ipconfig->index));
1435 if (ipdevice == NULL)
1438 g_free(ipdevice->pac);
1439 ipdevice->pac = g_strdup(url);
1444 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1446 struct connman_ipdevice *ipdevice;
1448 DBG("ipconfig %p", ipconfig);
1450 if (ipconfig == NULL || ipconfig->index < 0)
1453 ipdevice = g_hash_table_lookup(ipdevice_hash,
1454 GINT_TO_POINTER(ipconfig->index));
1455 if (ipdevice == NULL)
1458 return ipdevice->pac;
1461 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1463 struct connman_ipdevice *ipdevice;
1467 ipdevice = g_hash_table_lookup(ipdevice_hash,
1468 GINT_TO_POINTER(ipconfig->index));
1469 if (ipdevice == NULL)
1472 set_ipv6_state(ipdevice->ifname, FALSE);
1475 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1477 struct connman_ipdevice *ipdevice;
1481 ipdevice = g_hash_table_lookup(ipdevice_hash,
1482 GINT_TO_POINTER(ipconfig->index));
1483 if (ipdevice == NULL)
1486 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1487 set_ipv6_privacy(ipdevice->ifname,
1488 ipconfig->ipv6_privacy_config);
1490 set_ipv6_state(ipdevice->ifname, TRUE);
1493 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1495 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1498 disable_ipv6(ipconfig);
1501 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1503 struct connman_ipdevice *ipdevice;
1504 gboolean up = FALSE, down = FALSE;
1505 gboolean lower_up = FALSE, lower_down = FALSE;
1506 enum connman_ipconfig_type type;
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 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1519 if (ipdevice->config_ipv4 == ipconfig)
1521 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1522 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1523 if (ipdevice->config_ipv6 == ipconfig)
1525 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1526 enable_ipv6(ipconfig);
1530 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1531 ipdevice->config_ipv4 != NULL) {
1532 ipconfig_list = g_list_remove(ipconfig_list,
1533 ipdevice->config_ipv4);
1535 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1537 connman_ipconfig_unref(ipdevice->config_ipv4);
1540 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1541 ipdevice->config_ipv6 != NULL) {
1542 ipconfig_list = g_list_remove(ipconfig_list,
1543 ipdevice->config_ipv6);
1545 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1547 connman_ipconfig_unref(ipdevice->config_ipv6);
1550 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1551 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1552 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1553 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1555 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1557 if (ipdevice->flags & IFF_UP)
1562 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1563 (IFF_RUNNING | IFF_LOWER_UP))
1565 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1568 if (up == TRUE && ipconfig->ops->up)
1569 ipconfig->ops->up(ipconfig);
1570 if (lower_up == TRUE && ipconfig->ops->lower_up)
1571 ipconfig->ops->lower_up(ipconfig);
1573 if (lower_down == TRUE && ipconfig->ops->lower_down)
1574 ipconfig->ops->lower_down(ipconfig);
1575 if (down == TRUE && ipconfig->ops->down)
1576 ipconfig->ops->down(ipconfig);
1581 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1583 struct connman_ipdevice *ipdevice;
1585 DBG("ipconfig %p", ipconfig);
1587 if (ipconfig == NULL || ipconfig->index < 0)
1590 ipdevice = g_hash_table_lookup(ipdevice_hash,
1591 GINT_TO_POINTER(ipconfig->index));
1592 if (ipdevice == NULL)
1595 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1598 if (ipdevice->config_ipv4 == ipconfig) {
1599 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1601 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1602 connman_ipconfig_unref(ipdevice->config_ipv4);
1603 ipdevice->config_ipv4 = NULL;
1607 if (ipdevice->config_ipv6 == ipconfig) {
1608 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1610 if (ipdevice->config_ipv6->method ==
1611 CONNMAN_IPCONFIG_METHOD_AUTO)
1612 disable_ipv6(ipdevice->config_ipv6);
1614 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1615 connman_ipconfig_unref(ipdevice->config_ipv6);
1616 ipdevice->config_ipv6 = NULL;
1623 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1626 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1628 case CONNMAN_IPCONFIG_METHOD_OFF:
1630 case CONNMAN_IPCONFIG_METHOD_FIXED:
1632 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1634 case CONNMAN_IPCONFIG_METHOD_DHCP:
1636 case CONNMAN_IPCONFIG_METHOD_AUTO:
1643 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1645 if (g_strcmp0(method, "off") == 0)
1646 return CONNMAN_IPCONFIG_METHOD_OFF;
1647 else if (g_strcmp0(method, "fixed") == 0)
1648 return CONNMAN_IPCONFIG_METHOD_FIXED;
1649 else if (g_strcmp0(method, "manual") == 0)
1650 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1651 else if (g_strcmp0(method, "dhcp") == 0)
1652 return CONNMAN_IPCONFIG_METHOD_DHCP;
1653 else if (g_strcmp0(method, "auto") == 0)
1654 return CONNMAN_IPCONFIG_METHOD_AUTO;
1656 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1659 static const char *privacy2string(int privacy)
1663 else if (privacy == 1)
1665 else if (privacy > 1)
1671 static int string2privacy(const char *privacy)
1673 if (g_strcmp0(privacy, "disabled") == 0)
1675 else if (g_strcmp0(privacy, "enabled") == 0)
1677 else if (g_strcmp0(privacy, "prefered") == 0)
1683 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1684 DBusMessageIter *iter)
1690 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1693 str = __connman_ipconfig_method2string(ipconfig->method);
1697 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1699 if (ipconfig->system == NULL)
1702 if (ipconfig->system->local != NULL) {
1704 struct in_addr netmask;
1707 connman_dbus_dict_append_basic(iter, "Address",
1708 DBUS_TYPE_STRING, &ipconfig->system->local);
1710 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1711 netmask.s_addr = htonl(addr);
1712 mask = inet_ntoa(netmask);
1713 connman_dbus_dict_append_basic(iter, "Netmask",
1714 DBUS_TYPE_STRING, &mask);
1717 if (ipconfig->system->gateway != NULL)
1718 connman_dbus_dict_append_basic(iter, "Gateway",
1719 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1722 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1723 DBusMessageIter *iter,
1724 struct connman_ipconfig *ipconfig_ipv4)
1726 const char *str, *privacy;
1730 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1733 str = __connman_ipconfig_method2string(ipconfig->method);
1737 if (ipconfig_ipv4 != NULL &&
1738 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1739 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1743 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1745 if (ipconfig->system == NULL)
1748 if (ipconfig->system->local != NULL) {
1749 connman_dbus_dict_append_basic(iter, "Address",
1750 DBUS_TYPE_STRING, &ipconfig->system->local);
1751 connman_dbus_dict_append_basic(iter, "PrefixLength",
1753 &ipconfig->system->prefixlen);
1756 if (ipconfig->system->gateway != NULL)
1757 connman_dbus_dict_append_basic(iter, "Gateway",
1758 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1760 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1761 connman_dbus_dict_append_basic(iter, "Privacy",
1762 DBUS_TYPE_STRING, &privacy);
1765 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1766 DBusMessageIter *iter)
1768 const char *str, *privacy;
1772 str = __connman_ipconfig_method2string(ipconfig->method);
1776 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1778 switch (ipconfig->method) {
1779 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1780 case CONNMAN_IPCONFIG_METHOD_OFF:
1781 case CONNMAN_IPCONFIG_METHOD_DHCP:
1783 case CONNMAN_IPCONFIG_METHOD_FIXED:
1784 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1785 case CONNMAN_IPCONFIG_METHOD_AUTO:
1789 if (ipconfig->address == NULL)
1792 if (ipconfig->address->local != NULL) {
1793 connman_dbus_dict_append_basic(iter, "Address",
1794 DBUS_TYPE_STRING, &ipconfig->address->local);
1795 connman_dbus_dict_append_basic(iter, "PrefixLength",
1797 &ipconfig->address->prefixlen);
1800 if (ipconfig->address->gateway != NULL)
1801 connman_dbus_dict_append_basic(iter, "Gateway",
1802 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1804 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1805 connman_dbus_dict_append_basic(iter, "Privacy",
1806 DBUS_TYPE_STRING, &privacy);
1809 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1810 DBusMessageIter *iter)
1816 str = __connman_ipconfig_method2string(ipconfig->method);
1820 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1822 switch (ipconfig->method) {
1823 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1824 case CONNMAN_IPCONFIG_METHOD_OFF:
1825 case CONNMAN_IPCONFIG_METHOD_FIXED:
1826 case CONNMAN_IPCONFIG_METHOD_DHCP:
1827 case CONNMAN_IPCONFIG_METHOD_AUTO:
1829 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1833 if (ipconfig->address == NULL)
1836 if (ipconfig->address->local != NULL) {
1838 struct in_addr netmask;
1841 connman_dbus_dict_append_basic(iter, "Address",
1842 DBUS_TYPE_STRING, &ipconfig->address->local);
1844 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1845 netmask.s_addr = htonl(addr);
1846 mask = inet_ntoa(netmask);
1847 connman_dbus_dict_append_basic(iter, "Netmask",
1848 DBUS_TYPE_STRING, &mask);
1851 if (ipconfig->address->gateway != NULL)
1852 connman_dbus_dict_append_basic(iter, "Gateway",
1853 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1856 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1857 DBusMessageIter *array)
1859 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1860 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1861 *prefix_length_string = NULL, *privacy_string = NULL;
1862 int prefix_length = 0, privacy = 0;
1863 DBusMessageIter dict;
1865 DBG("ipconfig %p", ipconfig);
1867 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1870 dbus_message_iter_recurse(array, &dict);
1872 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1873 DBusMessageIter entry;
1877 dbus_message_iter_recurse(&dict, &entry);
1879 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1882 dbus_message_iter_get_basic(&entry, &key);
1883 dbus_message_iter_next(&entry);
1885 type = dbus_message_iter_get_arg_type(&entry);
1887 if (g_str_equal(key, "Method") == TRUE) {
1890 if (type != DBUS_TYPE_STRING)
1893 dbus_message_iter_get_basic(&entry, &str);
1894 method = __connman_ipconfig_string2method(str);
1895 } else if (g_str_equal(key, "Address") == TRUE) {
1896 if (type != DBUS_TYPE_STRING)
1899 dbus_message_iter_get_basic(&entry, &address);
1900 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1901 if (type != DBUS_TYPE_STRING)
1904 dbus_message_iter_get_basic(&entry,
1905 &prefix_length_string);
1907 prefix_length = atoi(prefix_length_string);
1908 if (prefix_length < 0 || prefix_length > 128)
1911 } else if (g_str_equal(key, "Netmask") == TRUE) {
1912 if (type != DBUS_TYPE_STRING)
1915 dbus_message_iter_get_basic(&entry, &netmask);
1916 } else if (g_str_equal(key, "Gateway") == TRUE) {
1917 if (type != DBUS_TYPE_STRING)
1920 dbus_message_iter_get_basic(&entry, &gateway);
1921 } else if (g_str_equal(key, "Privacy") == TRUE) {
1922 if (type != DBUS_TYPE_STRING)
1925 dbus_message_iter_get_basic(&entry, &privacy_string);
1926 privacy = string2privacy(privacy_string);
1928 dbus_message_iter_next(&dict);
1931 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1933 method, address, netmask, gateway, prefix_length,
1937 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1938 case CONNMAN_IPCONFIG_METHOD_FIXED:
1941 case CONNMAN_IPCONFIG_METHOD_OFF:
1942 ipconfig->method = method;
1943 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1944 disable_ipv6(ipconfig);
1947 case CONNMAN_IPCONFIG_METHOD_AUTO:
1948 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1951 ipconfig->method = method;
1952 if (privacy_string != NULL)
1953 ipconfig->ipv6_privacy_config = privacy;
1954 enable_ipv6(ipconfig);
1957 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1958 if (address == NULL)
1961 ipconfig->method = method;
1963 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1964 connman_ipaddress_set_ipv4(ipconfig->address,
1965 address, netmask, gateway);
1967 return connman_ipaddress_set_ipv6(
1968 ipconfig->address, address,
1969 prefix_length, gateway);
1972 case CONNMAN_IPCONFIG_METHOD_DHCP:
1973 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1976 ipconfig->method = method;
1983 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1984 DBusMessageIter *iter)
1986 struct connman_ipdevice *ipdevice;
1987 const char *method = "auto";
1989 connman_dbus_dict_append_basic(iter, "Method",
1990 DBUS_TYPE_STRING, &method);
1992 ipdevice = g_hash_table_lookup(ipdevice_hash,
1993 GINT_TO_POINTER(ipconfig->index));
1994 if (ipdevice == NULL)
1997 if (ipdevice->ifname != NULL)
1998 connman_dbus_dict_append_basic(iter, "Interface",
1999 DBUS_TYPE_STRING, &ipdevice->ifname);
2001 if (ipdevice->address != NULL)
2002 connman_dbus_dict_append_basic(iter, "Address",
2003 DBUS_TYPE_STRING, &ipdevice->address);
2005 if (ipdevice->mtu > 0)
2006 connman_dbus_dict_append_basic(iter, "MTU",
2007 DBUS_TYPE_UINT16, &ipdevice->mtu);
2010 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2011 GKeyFile *keyfile, const char *identifier, const char *prefix)
2016 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2018 key = g_strdup_printf("%smethod", prefix);
2019 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2020 if (method == NULL) {
2021 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2022 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2024 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2026 ipconfig->method = __connman_ipconfig_string2method(method);
2028 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2029 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2031 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2032 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2033 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2035 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2036 privacy = g_key_file_get_string(keyfile, identifier,
2038 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2042 __connman_ipconfig_enable(ipconfig);
2043 enable_ipv6(ipconfig);
2050 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2051 ipconfig->address->prefixlen = g_key_file_get_integer(
2052 keyfile, identifier, key, NULL);
2055 key = g_strdup_printf("%slocal_address", prefix);
2056 ipconfig->address->local = g_key_file_get_string(
2057 keyfile, identifier, key, NULL);
2060 key = g_strdup_printf("%speer_address", prefix);
2061 ipconfig->address->peer = g_key_file_get_string(
2062 keyfile, identifier, key, NULL);
2065 key = g_strdup_printf("%sbroadcast_address", prefix);
2066 ipconfig->address->broadcast = g_key_file_get_string(
2067 keyfile, identifier, key, NULL);
2070 key = g_strdup_printf("%sgateway", prefix);
2071 ipconfig->address->gateway = g_key_file_get_string(
2072 keyfile, identifier, key, NULL);
2078 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2079 GKeyFile *keyfile, const char *identifier, const char *prefix)
2084 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2086 method = __connman_ipconfig_method2string(ipconfig->method);
2088 key = g_strdup_printf("%smethod", prefix);
2089 g_key_file_set_string(keyfile, identifier, key, method);
2092 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2093 const char *privacy;
2094 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2095 key = g_strdup_printf("%sprivacy", prefix);
2096 g_key_file_set_string(keyfile, identifier, key, privacy);
2100 switch (ipconfig->method) {
2101 case CONNMAN_IPCONFIG_METHOD_FIXED:
2102 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2104 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2105 case CONNMAN_IPCONFIG_METHOD_OFF:
2106 case CONNMAN_IPCONFIG_METHOD_DHCP:
2107 case CONNMAN_IPCONFIG_METHOD_AUTO:
2111 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2112 g_key_file_set_integer(keyfile, identifier,
2113 key, ipconfig->address->prefixlen);
2116 key = g_strdup_printf("%slocal_address", prefix);
2117 if (ipconfig->address->local != NULL)
2118 g_key_file_set_string(keyfile, identifier,
2119 key, ipconfig->address->local);
2122 key = g_strdup_printf("%speer_address", prefix);
2123 if (ipconfig->address->peer != NULL)
2124 g_key_file_set_string(keyfile, identifier,
2125 key, ipconfig->address->peer);
2128 key = g_strdup_printf("%sbroadcast_address", prefix);
2129 if (ipconfig->address->broadcast != NULL)
2130 g_key_file_set_string(keyfile, identifier,
2131 key, ipconfig->address->broadcast);
2134 key = g_strdup_printf("%sgateway", prefix);
2135 if (ipconfig->address->gateway != NULL)
2136 g_key_file_set_string(keyfile, identifier,
2137 key, ipconfig->address->gateway);
2143 int __connman_ipconfig_init(void)
2147 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2148 NULL, free_ipdevice);
2153 void __connman_ipconfig_cleanup(void)
2157 g_hash_table_destroy(ipdevice_hash);
2158 ipdevice_hash = NULL;