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;
57 char *last_dhcp_address;
60 struct connman_ipdevice {
82 struct connman_ipconfig *config_ipv4;
83 struct connman_ipconfig *config_ipv6;
85 gboolean ipv6_enabled;
89 static GHashTable *ipdevice_hash = NULL;
90 static GList *ipconfig_list = NULL;
92 struct connman_ipaddress *connman_ipaddress_alloc(int family)
94 struct connman_ipaddress *ipaddress;
96 ipaddress = g_try_new0(struct connman_ipaddress, 1);
97 if (ipaddress == NULL)
100 ipaddress->family = family;
101 ipaddress->prefixlen = 0;
102 ipaddress->local = NULL;
103 ipaddress->peer = NULL;
104 ipaddress->broadcast = NULL;
105 ipaddress->gateway = NULL;
110 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
112 if (ipaddress == NULL)
115 g_free(ipaddress->broadcast);
116 g_free(ipaddress->peer);
117 g_free(ipaddress->local);
118 g_free(ipaddress->gateway);
122 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
131 mask = inet_network(netmask);
134 /* a valid netmask must be 2^n - 1 */
135 if ((host & (host + 1)) != 0)
139 for (; mask; mask <<= 1)
145 static gboolean check_ipv6_address(const char *address)
147 unsigned char buf[sizeof(struct in6_addr)];
153 err = inet_pton(AF_INET6, address, buf);
160 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
162 unsigned char prefix_length,
165 if (ipaddress == NULL)
168 if (check_ipv6_address(address) == FALSE)
171 if (check_ipv6_address(gateway) == FALSE)
174 DBG("prefix_len %d address %s gateway %s",
175 prefix_length, address, gateway);
177 ipaddress->family = AF_INET6;
179 ipaddress->prefixlen = prefix_length;
181 g_free(ipaddress->local);
182 ipaddress->local = g_strdup(address);
184 g_free(ipaddress->gateway);
185 ipaddress->gateway = g_strdup(gateway);
190 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
191 const char *address, const char *netmask, const char *gateway)
193 if (ipaddress == NULL)
196 ipaddress->family = AF_INET;
198 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
200 g_free(ipaddress->local);
201 ipaddress->local = g_strdup(address);
203 g_free(ipaddress->gateway);
204 ipaddress->gateway = g_strdup(gateway);
209 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
212 if (ipaddress == NULL)
215 g_free(ipaddress->peer);
216 ipaddress->peer = g_strdup(peer);
219 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
221 if (ipaddress == NULL)
224 ipaddress->prefixlen = 0;
226 g_free(ipaddress->local);
227 ipaddress->local = NULL;
229 g_free(ipaddress->peer);
230 ipaddress->peer = NULL;
232 g_free(ipaddress->broadcast);
233 ipaddress->broadcast = NULL;
235 g_free(ipaddress->gateway);
236 ipaddress->gateway = NULL;
239 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
240 struct connman_ipaddress *source)
242 if (ipaddress == NULL || source == NULL)
245 ipaddress->family = source->family;
246 ipaddress->prefixlen = source->prefixlen;
248 g_free(ipaddress->local);
249 ipaddress->local = g_strdup(source->local);
251 g_free(ipaddress->peer);
252 ipaddress->peer = g_strdup(source->peer);
254 g_free(ipaddress->broadcast);
255 ipaddress->broadcast = g_strdup(source->broadcast);
257 g_free(ipaddress->gateway);
258 ipaddress->gateway = g_strdup(source->gateway);
261 static void free_address_list(struct connman_ipdevice *ipdevice)
265 for (list = ipdevice->address_list; list; list = list->next) {
266 struct connman_ipaddress *ipaddress = list->data;
268 connman_ipaddress_free(ipaddress);
272 g_slist_free(ipdevice->address_list);
273 ipdevice->address_list = NULL;
276 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
277 unsigned char prefixlen, const char *local)
281 for (list = ipdevice->address_list; list; list = list->next) {
282 struct connman_ipaddress *ipaddress = list->data;
284 if (g_strcmp0(ipaddress->local, local) == 0 &&
285 ipaddress->prefixlen == prefixlen)
292 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
295 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
297 case CONNMAN_IPCONFIG_TYPE_IPV4:
299 case CONNMAN_IPCONFIG_TYPE_IPV6:
306 static const char *type2str(unsigned short type)
311 case ARPHRD_LOOPBACK:
324 static const char *scope2str(unsigned char scope)
336 static gboolean get_ipv6_state(gchar *ifname)
341 gboolean enabled = FALSE;
344 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
346 path = g_strdup_printf(
347 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
352 f = fopen(path, "r");
357 if (fscanf(f, "%d", &disabled) > 0)
365 static void set_ipv6_state(gchar *ifname, gboolean enable)
371 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
373 path = g_strdup_printf(
374 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
379 f = fopen(path, "r+");
394 static int get_ipv6_privacy(gchar *ifname)
403 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
409 f = fopen(path, "r");
416 if (fscanf(f, "%d", &value) <= 0)
424 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
425 * The privacy extension is described in RFC 3041 and RFC 4941
427 static void set_ipv6_privacy(gchar *ifname, int value)
435 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
444 f = fopen(path, "r+");
451 fprintf(f, "%d", value);
455 static int get_rp_filter()
458 int value = -EINVAL, tmp;
460 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
463 if (fscanf(f, "%d", &tmp) == 1)
471 static void set_rp_filter(int value)
475 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
480 fprintf(f, "%d", value);
485 int __connman_ipconfig_set_rp_filter()
489 value = get_rp_filter();
496 connman_info("rp_filter set to 2 (loose mode routing), "
497 "old value was %d", value);
502 void __connman_ipconfig_unset_rp_filter(int old_value)
504 set_rp_filter(old_value);
506 connman_info("rp_filter restored to %d", old_value);
509 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
511 if (ipconfig == NULL)
514 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
517 static void free_ipdevice(gpointer data)
519 struct connman_ipdevice *ipdevice = data;
521 connman_info("%s {remove} index %d", ipdevice->ifname,
524 if (ipdevice->config_ipv4 != NULL) {
525 __connman_ipconfig_unref(ipdevice->config_ipv4);
526 ipdevice->config_ipv4 = NULL;
529 if (ipdevice->config_ipv6 != NULL) {
530 __connman_ipconfig_unref(ipdevice->config_ipv6);
531 ipdevice->config_ipv6 = NULL;
534 free_address_list(ipdevice);
535 g_free(ipdevice->ipv4_gateway);
536 g_free(ipdevice->ipv6_gateway);
537 g_free(ipdevice->pac);
539 g_free(ipdevice->address);
541 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
542 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
544 g_free(ipdevice->ifname);
548 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
550 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
551 ipdevice->config_ipv6);
554 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
556 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
557 ipdevice->config_ipv6);
559 if (ipdevice->config_ipv4)
560 connman_inet_clear_address(ipdevice->index,
561 ipdevice->config_ipv4->address);
563 if (ipdevice->config_ipv6)
564 connman_inet_clear_ipv6_address(ipdevice->index,
565 ipdevice->config_ipv6->address->local,
566 ipdevice->config_ipv6->address->prefixlen);
569 static void update_stats(struct connman_ipdevice *ipdevice,
570 struct rtnl_link_stats *stats)
572 struct connman_service *service;
574 if (stats->rx_packets == 0 && stats->tx_packets == 0)
577 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
578 stats->rx_packets, stats->rx_bytes);
579 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
580 stats->tx_packets, stats->tx_bytes);
582 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
585 if (ipdevice->config_ipv4)
586 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
587 else if (ipdevice->config_ipv6)
588 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
595 ipdevice->rx_packets = stats->rx_packets;
596 ipdevice->tx_packets = stats->tx_packets;
597 ipdevice->rx_bytes = stats->rx_bytes;
598 ipdevice->tx_bytes = stats->tx_bytes;
599 ipdevice->rx_errors = stats->rx_errors;
600 ipdevice->tx_errors = stats->tx_errors;
601 ipdevice->rx_dropped = stats->rx_dropped;
602 ipdevice->tx_dropped = stats->tx_dropped;
604 __connman_service_notify(service,
605 ipdevice->rx_packets, ipdevice->tx_packets,
606 ipdevice->rx_bytes, ipdevice->tx_bytes,
607 ipdevice->rx_errors, ipdevice->tx_errors,
608 ipdevice->rx_dropped, ipdevice->tx_dropped);
611 void __connman_ipconfig_newlink(int index, unsigned short type,
612 unsigned int flags, const char *address,
614 struct rtnl_link_stats *stats)
616 struct connman_ipdevice *ipdevice;
619 gboolean up = FALSE, down = FALSE;
620 gboolean lower_up = FALSE, lower_down = FALSE;
622 DBG("index %d", index);
624 if (type == ARPHRD_LOOPBACK)
627 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
628 if (ipdevice != NULL)
631 ipdevice = g_try_new0(struct connman_ipdevice, 1);
632 if (ipdevice == NULL)
635 ipdevice->index = index;
636 ipdevice->ifname = connman_inet_ifname(index);
637 ipdevice->type = type;
639 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
640 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
642 ipdevice->address = g_strdup(address);
644 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
646 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
647 index, type, type2str(type));
652 update_stats(ipdevice, stats);
654 if (flags == ipdevice->flags)
657 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
664 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
665 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
666 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
667 (IFF_RUNNING | IFF_LOWER_UP))
669 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
673 ipdevice->flags = flags;
675 str = g_string_new(NULL);
680 g_string_append(str, "UP");
682 g_string_append(str, "DOWN");
684 if (flags & IFF_RUNNING)
685 g_string_append(str, ",RUNNING");
687 if (flags & IFF_LOWER_UP)
688 g_string_append(str, ",LOWER_UP");
690 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
693 g_string_free(str, TRUE);
695 for (list = g_list_first(ipconfig_list); list;
696 list = g_list_next(list)) {
697 struct connman_ipconfig *ipconfig = list->data;
699 if (index != ipconfig->index)
702 if (ipconfig->ops == NULL)
705 if (up == TRUE && ipconfig->ops->up)
706 ipconfig->ops->up(ipconfig);
707 if (lower_up == TRUE && ipconfig->ops->lower_up)
708 ipconfig->ops->lower_up(ipconfig);
710 if (lower_down == TRUE && ipconfig->ops->lower_down)
711 ipconfig->ops->lower_down(ipconfig);
712 if (down == TRUE && ipconfig->ops->down)
713 ipconfig->ops->down(ipconfig);
717 __connman_ipconfig_lower_up(ipdevice);
719 __connman_ipconfig_lower_down(ipdevice);
722 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
724 struct connman_ipdevice *ipdevice;
727 DBG("index %d", index);
729 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
730 if (ipdevice == NULL)
733 update_stats(ipdevice, stats);
735 for (list = g_list_first(ipconfig_list); list;
736 list = g_list_next(list)) {
737 struct connman_ipconfig *ipconfig = list->data;
739 if (index != ipconfig->index)
742 ipconfig->index = -1;
744 if (ipconfig->ops == NULL)
747 if (ipconfig->ops->lower_down)
748 ipconfig->ops->lower_down(ipconfig);
749 if (ipconfig->ops->down)
750 ipconfig->ops->down(ipconfig);
753 __connman_ipconfig_lower_down(ipdevice);
755 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
758 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
760 const struct connman_ipaddress *addr1 = a;
761 const struct connman_ipaddress *addr2 = b;
763 if (addr1->prefixlen != addr2->prefixlen)
764 return addr2->prefixlen - addr1->prefixlen;
766 return g_strcmp0(addr1->local, addr2->local);
769 void __connman_ipconfig_newaddr(int index, int family, const char *label,
770 unsigned char prefixlen, const char *address)
772 struct connman_ipdevice *ipdevice;
773 struct connman_ipaddress *ipaddress;
774 enum connman_ipconfig_type type;
777 DBG("index %d", index);
779 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
780 if (ipdevice == NULL)
783 ipaddress = connman_ipaddress_alloc(family);
784 if (ipaddress == NULL)
787 ipaddress->prefixlen = prefixlen;
788 ipaddress->local = g_strdup(address);
790 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
791 check_duplicate_address)) {
792 connman_ipaddress_free(ipaddress);
796 if (family == AF_INET)
797 type = CONNMAN_IPCONFIG_TYPE_IPV4;
798 else if (family == AF_INET6)
799 type = CONNMAN_IPCONFIG_TYPE_IPV6;
803 ipdevice->address_list = g_slist_append(ipdevice->address_list,
806 connman_info("%s {add} address %s/%u label %s family %d",
807 ipdevice->ifname, address, prefixlen, label, family);
809 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
810 __connman_ippool_newaddr(index, address, prefixlen);
812 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
813 connman_ipaddress_copy(ipdevice->config_ipv4->system,
816 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
817 connman_ipaddress_copy(ipdevice->config_ipv6->system,
822 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
825 for (list = g_list_first(ipconfig_list); list;
826 list = g_list_next(list)) {
827 struct connman_ipconfig *ipconfig = list->data;
829 if (index != ipconfig->index)
832 if (type != ipconfig->type)
835 if (ipconfig->ops == NULL)
838 if (ipconfig->ops->ip_bound)
839 ipconfig->ops->ip_bound(ipconfig);
843 void __connman_ipconfig_deladdr(int index, int family, const char *label,
844 unsigned char prefixlen, const char *address)
846 struct connman_ipdevice *ipdevice;
847 struct connman_ipaddress *ipaddress;
848 enum connman_ipconfig_type type;
851 DBG("index %d", index);
853 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
854 if (ipdevice == NULL)
857 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
858 if (ipaddress == NULL)
861 if (family == AF_INET)
862 type = CONNMAN_IPCONFIG_TYPE_IPV4;
863 else if (family == AF_INET6)
864 type = CONNMAN_IPCONFIG_TYPE_IPV6;
868 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
871 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
872 __connman_ippool_deladdr(index, address, prefixlen);
874 connman_ipaddress_clear(ipaddress);
877 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
878 address, prefixlen, label);
880 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
883 if (g_slist_length(ipdevice->address_list) > 0)
886 for (list = g_list_first(ipconfig_list); list;
887 list = g_list_next(list)) {
888 struct connman_ipconfig *ipconfig = list->data;
890 if (index != ipconfig->index)
893 if (type != ipconfig->type)
896 if (ipconfig->ops == NULL)
899 if (ipconfig->ops->ip_release)
900 ipconfig->ops->ip_release(ipconfig);
904 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
905 const char *dst, const char *gateway)
907 struct connman_ipdevice *ipdevice;
909 DBG("index %d", index);
911 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
912 if (ipdevice == NULL)
915 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
916 g_strcmp0(dst, "::") == 0)) {
918 enum connman_ipconfig_type type;
920 if (family == AF_INET6) {
921 type = CONNMAN_IPCONFIG_TYPE_IPV6;
922 g_free(ipdevice->ipv6_gateway);
923 ipdevice->ipv6_gateway = g_strdup(gateway);
925 if (ipdevice->config_ipv6 != NULL &&
926 ipdevice->config_ipv6->system != NULL) {
927 g_free(ipdevice->config_ipv6->system->gateway);
928 ipdevice->config_ipv6->system->gateway =
931 } else if (family == AF_INET) {
932 type = CONNMAN_IPCONFIG_TYPE_IPV4;
933 g_free(ipdevice->ipv4_gateway);
934 ipdevice->ipv4_gateway = g_strdup(gateway);
936 if (ipdevice->config_ipv4 != NULL &&
937 ipdevice->config_ipv4->system != NULL) {
938 g_free(ipdevice->config_ipv4->system->gateway);
939 ipdevice->config_ipv4->system->gateway =
945 for (config_list = g_list_first(ipconfig_list); config_list;
946 config_list = g_list_next(config_list)) {
947 struct connman_ipconfig *ipconfig = config_list->data;
949 if (index != ipconfig->index)
952 if (type != ipconfig->type)
955 if (ipconfig->ops == NULL)
958 if (ipconfig->ops->route_set)
959 ipconfig->ops->route_set(ipconfig);
963 connman_info("%s {add} route %s gw %s scope %u <%s>",
964 ipdevice->ifname, dst, gateway,
965 scope, scope2str(scope));
968 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
969 const char *dst, const char *gateway)
971 struct connman_ipdevice *ipdevice;
973 DBG("index %d", index);
975 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
976 if (ipdevice == NULL)
979 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
980 g_strcmp0(dst, "::") == 0)) {
982 enum connman_ipconfig_type type;
984 if (family == AF_INET6) {
985 type = CONNMAN_IPCONFIG_TYPE_IPV6;
986 g_free(ipdevice->ipv6_gateway);
987 ipdevice->ipv6_gateway = NULL;
989 if (ipdevice->config_ipv6 != NULL &&
990 ipdevice->config_ipv6->system != NULL) {
991 g_free(ipdevice->config_ipv6->system->gateway);
992 ipdevice->config_ipv6->system->gateway = NULL;
994 } else if (family == AF_INET) {
995 type = CONNMAN_IPCONFIG_TYPE_IPV4;
996 g_free(ipdevice->ipv4_gateway);
997 ipdevice->ipv4_gateway = NULL;
999 if (ipdevice->config_ipv4 != NULL &&
1000 ipdevice->config_ipv4->system != NULL) {
1001 g_free(ipdevice->config_ipv4->system->gateway);
1002 ipdevice->config_ipv4->system->gateway = NULL;
1007 for (config_list = g_list_first(ipconfig_list); config_list;
1008 config_list = g_list_next(config_list)) {
1009 struct connman_ipconfig *ipconfig = config_list->data;
1011 if (index != ipconfig->index)
1014 if (type != ipconfig->type)
1017 if (ipconfig->ops == NULL)
1020 if (ipconfig->ops->route_unset)
1021 ipconfig->ops->route_unset(ipconfig);
1025 connman_info("%s {del} route %s gw %s scope %u <%s>",
1026 ipdevice->ifname, dst, gateway,
1027 scope, scope2str(scope));
1030 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1035 keys = g_hash_table_get_keys(ipdevice_hash);
1039 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1040 int index = GPOINTER_TO_INT(list->data);
1042 function(index, user_data);
1048 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1049 struct connman_ipconfig *ipconfig)
1051 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1054 unsigned short __connman_ipconfig_get_type_from_index(int index)
1056 struct connman_ipdevice *ipdevice;
1058 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1059 if (ipdevice == NULL)
1062 return ipdevice->type;
1065 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1067 struct connman_ipdevice *ipdevice;
1069 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1070 if (ipdevice == NULL)
1073 return ipdevice->flags;
1076 const char *__connman_ipconfig_get_gateway_from_index(int index)
1078 struct connman_ipdevice *ipdevice;
1080 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1081 if (ipdevice == NULL)
1084 if (ipdevice->ipv4_gateway != NULL)
1085 return ipdevice->ipv4_gateway;
1087 if (ipdevice->config_ipv4 != NULL &&
1088 ipdevice->config_ipv4->address != NULL)
1089 return ipdevice->config_ipv4->address->gateway;
1091 if (ipdevice->ipv6_gateway != NULL)
1092 return ipdevice->ipv6_gateway;
1094 if (ipdevice->config_ipv6 != NULL &&
1095 ipdevice->config_ipv6->address != NULL)
1096 return ipdevice->config_ipv6->address->gateway;
1101 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1103 ipconfig->index = index;
1106 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1108 if (ipconfig->address == NULL)
1111 return ipconfig->address->local;
1114 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1116 if (ipconfig->address == NULL)
1119 g_free(ipconfig->address->local);
1120 ipconfig->address->local = g_strdup(address);
1123 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1125 if (ipconfig->address == NULL)
1128 return ipconfig->address->peer;
1131 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1133 if (ipconfig->address == NULL)
1136 g_free(ipconfig->address->peer);
1137 ipconfig->address->peer = g_strdup(address);
1140 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1142 if (ipconfig->address == NULL)
1145 return ipconfig->address->broadcast;
1148 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1150 if (ipconfig->address == NULL)
1153 g_free(ipconfig->address->broadcast);
1154 ipconfig->address->broadcast = g_strdup(broadcast);
1157 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1159 if (ipconfig->address == NULL)
1162 return ipconfig->address->gateway;
1165 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1169 if (ipconfig->address == NULL)
1171 g_free(ipconfig->address->gateway);
1172 ipconfig->address->gateway = g_strdup(gateway);
1175 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1177 struct connman_service *service;
1181 if (ipconfig->address == NULL)
1184 service = __connman_service_lookup_from_index(ipconfig->index);
1185 if (service == NULL)
1188 __connman_connection_gateway_remove(service, ipconfig->type);
1190 DBG("type %d gw %s peer %s", ipconfig->type,
1191 ipconfig->address->gateway, ipconfig->address->peer);
1193 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1194 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1195 return __connman_connection_gateway_add(service,
1196 ipconfig->address->gateway,
1198 ipconfig->address->peer);
1203 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1205 struct connman_service *service;
1209 service = __connman_service_lookup_from_index(ipconfig->index);
1210 if (service != NULL)
1211 __connman_connection_gateway_remove(service, ipconfig->type);
1214 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1216 if (ipconfig->address == NULL)
1219 return ipconfig->address->prefixlen;
1222 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1224 if (ipconfig->address == NULL)
1227 ipconfig->address->prefixlen = prefixlen;
1230 static struct connman_ipconfig *create_ipv6config(int index)
1232 struct connman_ipconfig *ipv6config;
1234 DBG("index %d", index);
1236 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1237 if (ipv6config == NULL)
1240 ipv6config->refcount = 1;
1242 ipv6config->index = index;
1243 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1244 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1245 ipv6config->ipv6_privacy_config = 0;
1247 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1248 if (ipv6config->address == NULL) {
1253 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1255 DBG("ipconfig %p", ipv6config);
1261 * connman_ipconfig_create:
1263 * Allocate a new ipconfig structure.
1265 * Returns: a newly-allocated #connman_ipconfig structure
1267 struct connman_ipconfig *__connman_ipconfig_create(int index,
1268 enum connman_ipconfig_type type)
1270 struct connman_ipconfig *ipconfig;
1272 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1273 return create_ipv6config(index);
1275 DBG("index %d", index);
1277 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1278 if (ipconfig == NULL)
1281 ipconfig->refcount = 1;
1283 ipconfig->index = index;
1284 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1286 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1287 if (ipconfig->address == NULL) {
1292 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1294 DBG("ipconfig %p", ipconfig);
1301 * connman_ipconfig_ref:
1302 * @ipconfig: ipconfig structure
1304 * Increase reference counter of ipconfig
1306 struct connman_ipconfig *
1307 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1308 const char *file, int line, const char *caller)
1310 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1311 file, line, caller);
1313 __sync_fetch_and_add(&ipconfig->refcount, 1);
1319 * connman_ipconfig_unref:
1320 * @ipconfig: ipconfig structure
1322 * Decrease reference counter of ipconfig
1324 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1325 const char *file, int line, const char *caller)
1327 if (ipconfig == NULL)
1330 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1331 file, line, caller);
1333 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1336 if (__connman_ipconfig_disable(ipconfig) < 0)
1337 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1339 __connman_ipconfig_set_ops(ipconfig, NULL);
1341 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1342 __connman_ipconfig_unref(ipconfig->origin);
1343 ipconfig->origin = NULL;
1346 connman_ipaddress_free(ipconfig->system);
1347 connman_ipaddress_free(ipconfig->address);
1348 g_free(ipconfig->last_dhcp_address);
1353 * connman_ipconfig_get_data:
1354 * @ipconfig: ipconfig structure
1356 * Get private data pointer
1358 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1360 if (ipconfig == NULL)
1363 return ipconfig->ops_data;
1367 * connman_ipconfig_set_data:
1368 * @ipconfig: ipconfig structure
1369 * @data: data pointer
1371 * Set private data pointer
1373 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1375 ipconfig->ops_data = data;
1379 * connman_ipconfig_get_index:
1380 * @ipconfig: ipconfig structure
1382 * Get interface index
1384 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1386 if (ipconfig == NULL)
1389 if (ipconfig->origin != NULL)
1390 return ipconfig->origin->index;
1392 return ipconfig->index;
1396 * connman_ipconfig_get_ifname:
1397 * @ipconfig: ipconfig structure
1399 * Get interface name
1401 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1403 struct connman_ipdevice *ipdevice;
1405 if (ipconfig == NULL)
1408 if (ipconfig->index < 0)
1411 ipdevice = g_hash_table_lookup(ipdevice_hash,
1412 GINT_TO_POINTER(ipconfig->index));
1413 if (ipdevice == NULL)
1416 return ipdevice->ifname;
1420 * connman_ipconfig_set_ops:
1421 * @ipconfig: ipconfig structure
1422 * @ops: operation callbacks
1424 * Set the operation callbacks
1426 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1427 const struct connman_ipconfig_ops *ops)
1429 ipconfig->ops = ops;
1433 * connman_ipconfig_set_method:
1434 * @ipconfig: ipconfig structure
1435 * @method: configuration method
1437 * Set the configuration method
1439 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1440 enum connman_ipconfig_method method)
1442 ipconfig->method = method;
1447 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1449 if (ipconfig == NULL)
1450 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1452 return ipconfig->method;
1455 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1459 switch (ipconfig->method) {
1460 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1461 case CONNMAN_IPCONFIG_METHOD_OFF:
1463 case CONNMAN_IPCONFIG_METHOD_AUTO:
1464 case CONNMAN_IPCONFIG_METHOD_FIXED:
1465 case CONNMAN_IPCONFIG_METHOD_DHCP:
1466 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1467 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1468 return connman_inet_set_address(ipconfig->index,
1470 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1471 return connman_inet_set_ipv6_address(
1472 ipconfig->index, ipconfig->address);
1478 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1484 if (ipconfig == NULL)
1487 DBG("method %d", ipconfig->method);
1489 switch (ipconfig->method) {
1490 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1491 case CONNMAN_IPCONFIG_METHOD_OFF:
1493 case CONNMAN_IPCONFIG_METHOD_AUTO:
1494 case CONNMAN_IPCONFIG_METHOD_FIXED:
1495 case CONNMAN_IPCONFIG_METHOD_DHCP:
1496 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1497 err = __connman_ipconfig_address_unset(ipconfig);
1498 connman_ipaddress_clear(ipconfig->address);
1506 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1512 if (ipconfig == NULL)
1515 DBG("method %d", ipconfig->method);
1517 switch (ipconfig->method) {
1518 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1519 case CONNMAN_IPCONFIG_METHOD_OFF:
1521 case CONNMAN_IPCONFIG_METHOD_AUTO:
1522 case CONNMAN_IPCONFIG_METHOD_FIXED:
1523 case CONNMAN_IPCONFIG_METHOD_DHCP:
1524 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1525 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1526 err = connman_inet_clear_address(ipconfig->index,
1528 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1529 err = connman_inet_clear_ipv6_address(
1531 ipconfig->address->local,
1532 ipconfig->address->prefixlen);
1542 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1545 struct connman_ipdevice *ipdevice;
1547 DBG("ipconfig %p", ipconfig);
1549 if (ipconfig == NULL || ipconfig->index < 0)
1552 ipdevice = g_hash_table_lookup(ipdevice_hash,
1553 GINT_TO_POINTER(ipconfig->index));
1554 if (ipdevice == NULL)
1557 g_free(ipdevice->pac);
1558 ipdevice->pac = g_strdup(url);
1563 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1565 struct connman_ipdevice *ipdevice;
1567 DBG("ipconfig %p", ipconfig);
1569 if (ipconfig == NULL || ipconfig->index < 0)
1572 ipdevice = g_hash_table_lookup(ipdevice_hash,
1573 GINT_TO_POINTER(ipconfig->index));
1574 if (ipdevice == NULL)
1577 return ipdevice->pac;
1580 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1581 const char *address)
1583 if (ipconfig == NULL)
1586 g_free(ipconfig->last_dhcp_address);
1587 ipconfig->last_dhcp_address = g_strdup(address);
1590 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1592 if (ipconfig == NULL)
1595 return ipconfig->last_dhcp_address;
1598 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1600 struct connman_ipdevice *ipdevice;
1604 ipdevice = g_hash_table_lookup(ipdevice_hash,
1605 GINT_TO_POINTER(ipconfig->index));
1606 if (ipdevice == NULL)
1609 set_ipv6_state(ipdevice->ifname, FALSE);
1612 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1614 struct connman_ipdevice *ipdevice;
1618 ipdevice = g_hash_table_lookup(ipdevice_hash,
1619 GINT_TO_POINTER(ipconfig->index));
1620 if (ipdevice == NULL)
1623 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1624 set_ipv6_privacy(ipdevice->ifname,
1625 ipconfig->ipv6_privacy_config);
1627 set_ipv6_state(ipdevice->ifname, TRUE);
1630 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1632 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1635 enable_ipv6(ipconfig);
1638 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1640 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1643 disable_ipv6(ipconfig);
1646 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1648 struct connman_ipdevice *ipdevice;
1649 gboolean up = FALSE, down = FALSE;
1650 gboolean lower_up = FALSE, lower_down = FALSE;
1651 enum connman_ipconfig_type type;
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 (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1664 if (ipdevice->config_ipv4 == ipconfig)
1666 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1667 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1668 if (ipdevice->config_ipv6 == ipconfig)
1670 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1671 enable_ipv6(ipconfig);
1675 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1676 ipdevice->config_ipv4 != NULL) {
1677 ipconfig_list = g_list_remove(ipconfig_list,
1678 ipdevice->config_ipv4);
1680 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1682 __connman_ipconfig_unref(ipdevice->config_ipv4);
1685 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1686 ipdevice->config_ipv6 != NULL) {
1687 ipconfig_list = g_list_remove(ipconfig_list,
1688 ipdevice->config_ipv6);
1690 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1692 __connman_ipconfig_unref(ipdevice->config_ipv6);
1695 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1696 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1697 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1698 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1700 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1702 if (ipdevice->flags & IFF_UP)
1707 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1708 (IFF_RUNNING | IFF_LOWER_UP))
1710 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1713 if (up == TRUE && ipconfig->ops->up)
1714 ipconfig->ops->up(ipconfig);
1715 if (lower_up == TRUE && ipconfig->ops->lower_up)
1716 ipconfig->ops->lower_up(ipconfig);
1718 if (lower_down == TRUE && ipconfig->ops->lower_down)
1719 ipconfig->ops->lower_down(ipconfig);
1720 if (down == TRUE && ipconfig->ops->down)
1721 ipconfig->ops->down(ipconfig);
1726 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1728 struct connman_ipdevice *ipdevice;
1730 DBG("ipconfig %p", ipconfig);
1732 if (ipconfig == NULL || ipconfig->index < 0)
1735 ipdevice = g_hash_table_lookup(ipdevice_hash,
1736 GINT_TO_POINTER(ipconfig->index));
1737 if (ipdevice == NULL)
1740 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1743 if (ipdevice->config_ipv4 == ipconfig) {
1744 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1746 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1747 __connman_ipconfig_unref(ipdevice->config_ipv4);
1748 ipdevice->config_ipv4 = NULL;
1752 if (ipdevice->config_ipv6 == ipconfig) {
1753 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1755 if (ipdevice->config_ipv6->method ==
1756 CONNMAN_IPCONFIG_METHOD_AUTO)
1757 disable_ipv6(ipdevice->config_ipv6);
1759 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1760 __connman_ipconfig_unref(ipdevice->config_ipv6);
1761 ipdevice->config_ipv6 = NULL;
1768 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1771 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1773 case CONNMAN_IPCONFIG_METHOD_OFF:
1775 case CONNMAN_IPCONFIG_METHOD_FIXED:
1777 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1779 case CONNMAN_IPCONFIG_METHOD_DHCP:
1781 case CONNMAN_IPCONFIG_METHOD_AUTO:
1788 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1790 if (g_strcmp0(method, "off") == 0)
1791 return CONNMAN_IPCONFIG_METHOD_OFF;
1792 else if (g_strcmp0(method, "fixed") == 0)
1793 return CONNMAN_IPCONFIG_METHOD_FIXED;
1794 else if (g_strcmp0(method, "manual") == 0)
1795 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1796 else if (g_strcmp0(method, "dhcp") == 0)
1797 return CONNMAN_IPCONFIG_METHOD_DHCP;
1798 else if (g_strcmp0(method, "auto") == 0)
1799 return CONNMAN_IPCONFIG_METHOD_AUTO;
1801 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1804 static const char *privacy2string(int privacy)
1808 else if (privacy == 1)
1810 else if (privacy > 1)
1816 static int string2privacy(const char *privacy)
1818 if (g_strcmp0(privacy, "disabled") == 0)
1820 else if (g_strcmp0(privacy, "enabled") == 0)
1822 else if (g_strcmp0(privacy, "prefered") == 0)
1828 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1829 DBusMessageIter *iter)
1835 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1838 str = __connman_ipconfig_method2string(ipconfig->method);
1842 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1844 if (ipconfig->system == NULL)
1847 if (ipconfig->system->local != NULL) {
1849 struct in_addr netmask;
1852 connman_dbus_dict_append_basic(iter, "Address",
1853 DBUS_TYPE_STRING, &ipconfig->system->local);
1855 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1856 netmask.s_addr = htonl(addr);
1857 mask = inet_ntoa(netmask);
1858 connman_dbus_dict_append_basic(iter, "Netmask",
1859 DBUS_TYPE_STRING, &mask);
1862 if (ipconfig->system->gateway != NULL)
1863 connman_dbus_dict_append_basic(iter, "Gateway",
1864 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1867 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1868 DBusMessageIter *iter,
1869 struct connman_ipconfig *ipconfig_ipv4)
1871 const char *str, *privacy;
1875 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1878 str = __connman_ipconfig_method2string(ipconfig->method);
1882 if (ipconfig_ipv4 != NULL &&
1883 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1884 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1888 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1890 if (ipconfig->system == NULL)
1893 if (ipconfig->system->local != NULL) {
1894 connman_dbus_dict_append_basic(iter, "Address",
1895 DBUS_TYPE_STRING, &ipconfig->system->local);
1896 connman_dbus_dict_append_basic(iter, "PrefixLength",
1898 &ipconfig->system->prefixlen);
1901 if (ipconfig->system->gateway != NULL)
1902 connman_dbus_dict_append_basic(iter, "Gateway",
1903 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1905 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1906 connman_dbus_dict_append_basic(iter, "Privacy",
1907 DBUS_TYPE_STRING, &privacy);
1910 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1911 DBusMessageIter *iter)
1913 const char *str, *privacy;
1917 str = __connman_ipconfig_method2string(ipconfig->method);
1921 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1923 switch (ipconfig->method) {
1924 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1925 case CONNMAN_IPCONFIG_METHOD_OFF:
1926 case CONNMAN_IPCONFIG_METHOD_DHCP:
1928 case CONNMAN_IPCONFIG_METHOD_FIXED:
1929 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1930 case CONNMAN_IPCONFIG_METHOD_AUTO:
1934 if (ipconfig->address == NULL)
1937 if (ipconfig->address->local != NULL) {
1938 connman_dbus_dict_append_basic(iter, "Address",
1939 DBUS_TYPE_STRING, &ipconfig->address->local);
1940 connman_dbus_dict_append_basic(iter, "PrefixLength",
1942 &ipconfig->address->prefixlen);
1945 if (ipconfig->address->gateway != NULL)
1946 connman_dbus_dict_append_basic(iter, "Gateway",
1947 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1949 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1950 connman_dbus_dict_append_basic(iter, "Privacy",
1951 DBUS_TYPE_STRING, &privacy);
1954 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1955 DBusMessageIter *iter)
1961 str = __connman_ipconfig_method2string(ipconfig->method);
1965 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1967 switch (ipconfig->method) {
1968 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1969 case CONNMAN_IPCONFIG_METHOD_OFF:
1970 case CONNMAN_IPCONFIG_METHOD_DHCP:
1971 case CONNMAN_IPCONFIG_METHOD_AUTO:
1973 case CONNMAN_IPCONFIG_METHOD_FIXED:
1974 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1978 if (ipconfig->address == NULL)
1981 if (ipconfig->address->local != NULL) {
1983 struct in_addr netmask;
1986 connman_dbus_dict_append_basic(iter, "Address",
1987 DBUS_TYPE_STRING, &ipconfig->address->local);
1989 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1990 netmask.s_addr = htonl(addr);
1991 mask = inet_ntoa(netmask);
1992 connman_dbus_dict_append_basic(iter, "Netmask",
1993 DBUS_TYPE_STRING, &mask);
1996 if (ipconfig->address->gateway != NULL)
1997 connman_dbus_dict_append_basic(iter, "Gateway",
1998 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2001 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2002 DBusMessageIter *array)
2004 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2005 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2006 *prefix_length_string = NULL, *privacy_string = NULL;
2007 int prefix_length = 0, privacy = 0;
2008 DBusMessageIter dict;
2010 DBG("ipconfig %p", ipconfig);
2012 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2015 dbus_message_iter_recurse(array, &dict);
2017 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2018 DBusMessageIter entry;
2022 dbus_message_iter_recurse(&dict, &entry);
2024 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2027 dbus_message_iter_get_basic(&entry, &key);
2028 dbus_message_iter_next(&entry);
2030 type = dbus_message_iter_get_arg_type(&entry);
2032 if (g_str_equal(key, "Method") == TRUE) {
2035 if (type != DBUS_TYPE_STRING)
2038 dbus_message_iter_get_basic(&entry, &str);
2039 method = __connman_ipconfig_string2method(str);
2040 } else if (g_str_equal(key, "Address") == TRUE) {
2041 if (type != DBUS_TYPE_STRING)
2044 dbus_message_iter_get_basic(&entry, &address);
2045 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2046 if (type != DBUS_TYPE_STRING)
2049 dbus_message_iter_get_basic(&entry,
2050 &prefix_length_string);
2052 prefix_length = atoi(prefix_length_string);
2053 if (prefix_length < 0 || prefix_length > 128)
2056 } else if (g_str_equal(key, "Netmask") == TRUE) {
2057 if (type != DBUS_TYPE_STRING)
2060 dbus_message_iter_get_basic(&entry, &netmask);
2061 } else if (g_str_equal(key, "Gateway") == TRUE) {
2062 if (type != DBUS_TYPE_STRING)
2065 dbus_message_iter_get_basic(&entry, &gateway);
2066 } else if (g_str_equal(key, "Privacy") == TRUE) {
2067 if (type != DBUS_TYPE_STRING)
2070 dbus_message_iter_get_basic(&entry, &privacy_string);
2071 privacy = string2privacy(privacy_string);
2073 dbus_message_iter_next(&dict);
2076 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2078 method, address, netmask, gateway, prefix_length,
2082 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2083 case CONNMAN_IPCONFIG_METHOD_FIXED:
2086 case CONNMAN_IPCONFIG_METHOD_OFF:
2087 ipconfig->method = method;
2088 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2089 disable_ipv6(ipconfig);
2092 case CONNMAN_IPCONFIG_METHOD_AUTO:
2093 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2096 ipconfig->method = method;
2097 if (privacy_string != NULL)
2098 ipconfig->ipv6_privacy_config = privacy;
2099 enable_ipv6(ipconfig);
2102 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2103 if (address == NULL)
2106 ipconfig->method = method;
2108 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2109 connman_ipaddress_set_ipv4(ipconfig->address,
2110 address, netmask, gateway);
2112 return connman_ipaddress_set_ipv6(
2113 ipconfig->address, address,
2114 prefix_length, gateway);
2117 case CONNMAN_IPCONFIG_METHOD_DHCP:
2118 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2121 ipconfig->method = method;
2128 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2129 DBusMessageIter *iter)
2131 struct connman_ipdevice *ipdevice;
2132 const char *method = "auto";
2134 connman_dbus_dict_append_basic(iter, "Method",
2135 DBUS_TYPE_STRING, &method);
2137 ipdevice = g_hash_table_lookup(ipdevice_hash,
2138 GINT_TO_POINTER(ipconfig->index));
2139 if (ipdevice == NULL)
2142 if (ipdevice->ifname != NULL)
2143 connman_dbus_dict_append_basic(iter, "Interface",
2144 DBUS_TYPE_STRING, &ipdevice->ifname);
2146 if (ipdevice->address != NULL)
2147 connman_dbus_dict_append_basic(iter, "Address",
2148 DBUS_TYPE_STRING, &ipdevice->address);
2150 if (ipdevice->mtu > 0)
2151 connman_dbus_dict_append_basic(iter, "MTU",
2152 DBUS_TYPE_UINT16, &ipdevice->mtu);
2155 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2156 GKeyFile *keyfile, const char *identifier, const char *prefix)
2162 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2164 key = g_strdup_printf("%smethod", prefix);
2165 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2166 if (method == NULL) {
2167 switch (ipconfig->type) {
2168 case CONNMAN_IPCONFIG_TYPE_IPV4:
2169 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2171 case CONNMAN_IPCONFIG_TYPE_IPV6:
2172 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2174 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2175 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2179 ipconfig->method = __connman_ipconfig_string2method(method);
2181 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2182 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2184 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2185 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2186 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2188 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2189 privacy = g_key_file_get_string(keyfile, identifier,
2191 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2195 __connman_ipconfig_enable(ipconfig);
2196 enable_ipv6(ipconfig);
2203 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2204 ipconfig->address->prefixlen = g_key_file_get_integer(
2205 keyfile, identifier, key, NULL);
2208 key = g_strdup_printf("%slocal_address", prefix);
2209 ipconfig->address->local = g_key_file_get_string(
2210 keyfile, identifier, key, NULL);
2213 key = g_strdup_printf("%speer_address", prefix);
2214 ipconfig->address->peer = g_key_file_get_string(
2215 keyfile, identifier, key, NULL);
2218 key = g_strdup_printf("%sbroadcast_address", prefix);
2219 ipconfig->address->broadcast = g_key_file_get_string(
2220 keyfile, identifier, key, NULL);
2223 key = g_strdup_printf("%sgateway", prefix);
2224 ipconfig->address->gateway = g_key_file_get_string(
2225 keyfile, identifier, key, NULL);
2228 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2229 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2231 g_free(ipconfig->last_dhcp_address);
2232 ipconfig->last_dhcp_address = str;
2239 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2240 GKeyFile *keyfile, const char *identifier, const char *prefix)
2245 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2247 method = __connman_ipconfig_method2string(ipconfig->method);
2249 key = g_strdup_printf("%smethod", prefix);
2250 g_key_file_set_string(keyfile, identifier, key, method);
2253 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2254 const char *privacy;
2255 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2256 key = g_strdup_printf("%sprivacy", prefix);
2257 g_key_file_set_string(keyfile, identifier, key, privacy);
2261 switch (ipconfig->method) {
2262 case CONNMAN_IPCONFIG_METHOD_FIXED:
2263 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2265 case CONNMAN_IPCONFIG_METHOD_DHCP:
2266 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2267 if (ipconfig->last_dhcp_address != NULL &&
2268 strlen(ipconfig->last_dhcp_address) > 0)
2269 g_key_file_set_string(keyfile, identifier, key,
2270 ipconfig->last_dhcp_address);
2272 g_key_file_remove_key(keyfile, identifier, key, NULL);
2275 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2276 case CONNMAN_IPCONFIG_METHOD_OFF:
2277 case CONNMAN_IPCONFIG_METHOD_AUTO:
2281 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2282 g_key_file_set_integer(keyfile, identifier,
2283 key, ipconfig->address->prefixlen);
2286 key = g_strdup_printf("%slocal_address", prefix);
2287 if (ipconfig->address->local != NULL)
2288 g_key_file_set_string(keyfile, identifier,
2289 key, ipconfig->address->local);
2292 key = g_strdup_printf("%speer_address", prefix);
2293 if (ipconfig->address->peer != NULL)
2294 g_key_file_set_string(keyfile, identifier,
2295 key, ipconfig->address->peer);
2298 key = g_strdup_printf("%sbroadcast_address", prefix);
2299 if (ipconfig->address->broadcast != NULL)
2300 g_key_file_set_string(keyfile, identifier,
2301 key, ipconfig->address->broadcast);
2304 key = g_strdup_printf("%sgateway", prefix);
2305 if (ipconfig->address->gateway != NULL)
2306 g_key_file_set_string(keyfile, identifier,
2307 key, ipconfig->address->gateway);
2313 int __connman_ipconfig_init(void)
2317 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2318 NULL, free_ipdevice);
2323 void __connman_ipconfig_cleanup(void)
2327 g_hash_table_destroy(ipdevice_hash);
2328 ipdevice_hash = NULL;