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 (ipdevice->config_ipv4 != NULL && family == AF_INET)
810 connman_ipaddress_copy(ipdevice->config_ipv4->system,
813 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
814 connman_ipaddress_copy(ipdevice->config_ipv6->system,
819 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
822 for (list = g_list_first(ipconfig_list); list;
823 list = g_list_next(list)) {
824 struct connman_ipconfig *ipconfig = list->data;
826 if (index != ipconfig->index)
829 if (type != ipconfig->type)
832 if (ipconfig->ops == NULL)
835 if (ipconfig->ops->ip_bound)
836 ipconfig->ops->ip_bound(ipconfig);
840 void __connman_ipconfig_deladdr(int index, int family, const char *label,
841 unsigned char prefixlen, const char *address)
843 struct connman_ipdevice *ipdevice;
844 struct connman_ipaddress *ipaddress;
845 enum connman_ipconfig_type type;
848 DBG("index %d", index);
850 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
851 if (ipdevice == NULL)
854 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
855 if (ipaddress == NULL)
858 if (family == AF_INET)
859 type = CONNMAN_IPCONFIG_TYPE_IPV4;
860 else if (family == AF_INET6)
861 type = CONNMAN_IPCONFIG_TYPE_IPV6;
865 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
868 connman_ipaddress_clear(ipaddress);
871 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
872 address, prefixlen, label);
874 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
877 if (g_slist_length(ipdevice->address_list) > 0)
880 for (list = g_list_first(ipconfig_list); list;
881 list = g_list_next(list)) {
882 struct connman_ipconfig *ipconfig = list->data;
884 if (index != ipconfig->index)
887 if (type != ipconfig->type)
890 if (ipconfig->ops == NULL)
893 if (ipconfig->ops->ip_release)
894 ipconfig->ops->ip_release(ipconfig);
898 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
899 const char *dst, const char *gateway)
901 struct connman_ipdevice *ipdevice;
903 DBG("index %d", index);
905 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
906 if (ipdevice == NULL)
909 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
910 g_strcmp0(dst, "::") == 0)) {
912 enum connman_ipconfig_type type;
914 if (family == AF_INET6) {
915 type = CONNMAN_IPCONFIG_TYPE_IPV6;
916 g_free(ipdevice->ipv6_gateway);
917 ipdevice->ipv6_gateway = g_strdup(gateway);
919 if (ipdevice->config_ipv6 != NULL &&
920 ipdevice->config_ipv6->system != NULL) {
921 g_free(ipdevice->config_ipv6->system->gateway);
922 ipdevice->config_ipv6->system->gateway =
925 } else if (family == AF_INET) {
926 type = CONNMAN_IPCONFIG_TYPE_IPV4;
927 g_free(ipdevice->ipv4_gateway);
928 ipdevice->ipv4_gateway = g_strdup(gateway);
930 if (ipdevice->config_ipv4 != NULL &&
931 ipdevice->config_ipv4->system != NULL) {
932 g_free(ipdevice->config_ipv4->system->gateway);
933 ipdevice->config_ipv4->system->gateway =
939 for (config_list = g_list_first(ipconfig_list); config_list;
940 config_list = g_list_next(config_list)) {
941 struct connman_ipconfig *ipconfig = config_list->data;
943 if (index != ipconfig->index)
946 if (type != ipconfig->type)
949 if (ipconfig->ops == NULL)
952 if (ipconfig->ops->route_set)
953 ipconfig->ops->route_set(ipconfig);
957 connman_info("%s {add} route %s gw %s scope %u <%s>",
958 ipdevice->ifname, dst, gateway,
959 scope, scope2str(scope));
962 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
963 const char *dst, const char *gateway)
965 struct connman_ipdevice *ipdevice;
967 DBG("index %d", index);
969 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
970 if (ipdevice == NULL)
973 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
974 g_strcmp0(dst, "::") == 0)) {
976 enum connman_ipconfig_type type;
978 if (family == AF_INET6) {
979 type = CONNMAN_IPCONFIG_TYPE_IPV6;
980 g_free(ipdevice->ipv6_gateway);
981 ipdevice->ipv6_gateway = NULL;
983 if (ipdevice->config_ipv6 != NULL &&
984 ipdevice->config_ipv6->system != NULL) {
985 g_free(ipdevice->config_ipv6->system->gateway);
986 ipdevice->config_ipv6->system->gateway = NULL;
988 } else if (family == AF_INET) {
989 type = CONNMAN_IPCONFIG_TYPE_IPV4;
990 g_free(ipdevice->ipv4_gateway);
991 ipdevice->ipv4_gateway = NULL;
993 if (ipdevice->config_ipv4 != NULL &&
994 ipdevice->config_ipv4->system != NULL) {
995 g_free(ipdevice->config_ipv4->system->gateway);
996 ipdevice->config_ipv4->system->gateway = NULL;
1001 for (config_list = g_list_first(ipconfig_list); config_list;
1002 config_list = g_list_next(config_list)) {
1003 struct connman_ipconfig *ipconfig = config_list->data;
1005 if (index != ipconfig->index)
1008 if (type != ipconfig->type)
1011 if (ipconfig->ops == NULL)
1014 if (ipconfig->ops->route_unset)
1015 ipconfig->ops->route_unset(ipconfig);
1019 connman_info("%s {del} route %s gw %s scope %u <%s>",
1020 ipdevice->ifname, dst, gateway,
1021 scope, scope2str(scope));
1024 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1029 keys = g_hash_table_get_keys(ipdevice_hash);
1033 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1034 int index = GPOINTER_TO_INT(list->data);
1036 function(index, user_data);
1042 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1043 struct connman_ipconfig *ipconfig)
1045 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1048 unsigned short __connman_ipconfig_get_type_from_index(int index)
1050 struct connman_ipdevice *ipdevice;
1052 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1053 if (ipdevice == NULL)
1056 return ipdevice->type;
1059 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1061 struct connman_ipdevice *ipdevice;
1063 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1064 if (ipdevice == NULL)
1067 return ipdevice->flags;
1070 const char *__connman_ipconfig_get_gateway_from_index(int index)
1072 struct connman_ipdevice *ipdevice;
1074 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1075 if (ipdevice == NULL)
1078 if (ipdevice->ipv4_gateway != NULL)
1079 return ipdevice->ipv4_gateway;
1081 if (ipdevice->config_ipv4 != NULL &&
1082 ipdevice->config_ipv4->address != NULL)
1083 return ipdevice->config_ipv4->address->gateway;
1085 if (ipdevice->ipv6_gateway != NULL)
1086 return ipdevice->ipv6_gateway;
1088 if (ipdevice->config_ipv6 != NULL &&
1089 ipdevice->config_ipv6->address != NULL)
1090 return ipdevice->config_ipv6->address->gateway;
1095 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1097 ipconfig->index = index;
1100 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1102 if (ipconfig->address == NULL)
1105 return ipconfig->address->local;
1108 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1110 if (ipconfig->address == NULL)
1113 g_free(ipconfig->address->local);
1114 ipconfig->address->local = g_strdup(address);
1117 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1119 if (ipconfig->address == NULL)
1122 return ipconfig->address->peer;
1125 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1127 if (ipconfig->address == NULL)
1130 g_free(ipconfig->address->peer);
1131 ipconfig->address->peer = g_strdup(address);
1134 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1136 if (ipconfig->address == NULL)
1139 return ipconfig->address->broadcast;
1142 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1144 if (ipconfig->address == NULL)
1147 g_free(ipconfig->address->broadcast);
1148 ipconfig->address->broadcast = g_strdup(broadcast);
1151 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1153 if (ipconfig->address == NULL)
1156 return ipconfig->address->gateway;
1159 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1163 if (ipconfig->address == NULL)
1165 g_free(ipconfig->address->gateway);
1166 ipconfig->address->gateway = g_strdup(gateway);
1169 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1171 struct connman_service *service;
1175 if (ipconfig->address == NULL)
1178 service = __connman_service_lookup_from_index(ipconfig->index);
1179 if (service == NULL)
1182 __connman_connection_gateway_remove(service, ipconfig->type);
1184 DBG("type %d gw %s peer %s", ipconfig->type,
1185 ipconfig->address->gateway, ipconfig->address->peer);
1187 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1188 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1189 return __connman_connection_gateway_add(service,
1190 ipconfig->address->gateway,
1192 ipconfig->address->peer);
1197 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1199 struct connman_service *service;
1203 service = __connman_service_lookup_from_index(ipconfig->index);
1204 if (service != NULL)
1205 __connman_connection_gateway_remove(service, ipconfig->type);
1208 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1210 if (ipconfig->address == NULL)
1213 return ipconfig->address->prefixlen;
1216 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1218 if (ipconfig->address == NULL)
1221 ipconfig->address->prefixlen = prefixlen;
1224 static struct connman_ipconfig *create_ipv6config(int index)
1226 struct connman_ipconfig *ipv6config;
1228 DBG("index %d", index);
1230 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1231 if (ipv6config == NULL)
1234 ipv6config->refcount = 1;
1236 ipv6config->index = index;
1237 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1238 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1239 ipv6config->ipv6_privacy_config = 0;
1241 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1242 if (ipv6config->address == NULL) {
1247 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1249 DBG("ipconfig %p", ipv6config);
1255 * connman_ipconfig_create:
1257 * Allocate a new ipconfig structure.
1259 * Returns: a newly-allocated #connman_ipconfig structure
1261 struct connman_ipconfig *connman_ipconfig_create(int index,
1262 enum connman_ipconfig_type type)
1264 struct connman_ipconfig *ipconfig;
1266 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1267 return create_ipv6config(index);
1269 DBG("index %d", index);
1271 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1272 if (ipconfig == NULL)
1275 ipconfig->refcount = 1;
1277 ipconfig->index = index;
1278 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1280 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1281 if (ipconfig->address == NULL) {
1286 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1288 DBG("ipconfig %p", ipconfig);
1295 * connman_ipconfig_ref:
1296 * @ipconfig: ipconfig structure
1298 * Increase reference counter of ipconfig
1300 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1302 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1304 __sync_fetch_and_add(&ipconfig->refcount, 1);
1310 * connman_ipconfig_unref:
1311 * @ipconfig: ipconfig structure
1313 * Decrease reference counter of ipconfig
1315 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1317 if (ipconfig == NULL)
1320 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1322 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1325 if (__connman_ipconfig_disable(ipconfig) < 0)
1326 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1328 connman_ipconfig_set_ops(ipconfig, NULL);
1330 if (ipconfig->origin != NULL) {
1331 connman_ipconfig_unref(ipconfig->origin);
1332 ipconfig->origin = NULL;
1335 connman_ipaddress_free(ipconfig->system);
1336 connman_ipaddress_free(ipconfig->address);
1337 g_free(ipconfig->last_dhcp_address);
1342 * connman_ipconfig_get_data:
1343 * @ipconfig: ipconfig structure
1345 * Get private data pointer
1347 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1349 if (ipconfig == NULL)
1352 return ipconfig->ops_data;
1356 * connman_ipconfig_set_data:
1357 * @ipconfig: ipconfig structure
1358 * @data: data pointer
1360 * Set private data pointer
1362 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1364 ipconfig->ops_data = data;
1368 * connman_ipconfig_get_index:
1369 * @ipconfig: ipconfig structure
1371 * Get interface index
1373 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1375 if (ipconfig == NULL)
1378 if (ipconfig->origin != NULL)
1379 return ipconfig->origin->index;
1381 return ipconfig->index;
1385 * connman_ipconfig_get_ifname:
1386 * @ipconfig: ipconfig structure
1388 * Get interface name
1390 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1392 struct connman_ipdevice *ipdevice;
1394 if (ipconfig == NULL)
1397 if (ipconfig->index < 0)
1400 ipdevice = g_hash_table_lookup(ipdevice_hash,
1401 GINT_TO_POINTER(ipconfig->index));
1402 if (ipdevice == NULL)
1405 return ipdevice->ifname;
1409 * connman_ipconfig_set_ops:
1410 * @ipconfig: ipconfig structure
1411 * @ops: operation callbacks
1413 * Set the operation callbacks
1415 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1416 const struct connman_ipconfig_ops *ops)
1418 ipconfig->ops = ops;
1422 * connman_ipconfig_set_method:
1423 * @ipconfig: ipconfig structure
1424 * @method: configuration method
1426 * Set the configuration method
1428 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1429 enum connman_ipconfig_method method)
1431 ipconfig->method = method;
1436 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1438 if (ipconfig == NULL)
1439 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1441 return ipconfig->method;
1444 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1448 switch (ipconfig->method) {
1449 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1450 case CONNMAN_IPCONFIG_METHOD_OFF:
1452 case CONNMAN_IPCONFIG_METHOD_AUTO:
1453 case CONNMAN_IPCONFIG_METHOD_FIXED:
1454 case CONNMAN_IPCONFIG_METHOD_DHCP:
1455 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1456 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1457 return connman_inet_set_address(ipconfig->index,
1459 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1460 return connman_inet_set_ipv6_address(
1461 ipconfig->index, ipconfig->address);
1467 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1473 if (ipconfig == NULL)
1476 DBG("method %d", ipconfig->method);
1478 switch (ipconfig->method) {
1479 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1480 case CONNMAN_IPCONFIG_METHOD_OFF:
1482 case CONNMAN_IPCONFIG_METHOD_AUTO:
1483 case CONNMAN_IPCONFIG_METHOD_FIXED:
1484 case CONNMAN_IPCONFIG_METHOD_DHCP:
1485 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1486 err = __connman_ipconfig_address_unset(ipconfig);
1487 connman_ipaddress_clear(ipconfig->address);
1495 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1501 if (ipconfig == NULL)
1504 DBG("method %d", ipconfig->method);
1506 switch (ipconfig->method) {
1507 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1508 case CONNMAN_IPCONFIG_METHOD_OFF:
1510 case CONNMAN_IPCONFIG_METHOD_AUTO:
1511 case CONNMAN_IPCONFIG_METHOD_FIXED:
1512 case CONNMAN_IPCONFIG_METHOD_DHCP:
1513 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1514 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1515 err = connman_inet_clear_address(ipconfig->index,
1517 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1518 err = connman_inet_clear_ipv6_address(
1520 ipconfig->address->local,
1521 ipconfig->address->prefixlen);
1531 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1534 struct connman_ipdevice *ipdevice;
1536 DBG("ipconfig %p", ipconfig);
1538 if (ipconfig == NULL || ipconfig->index < 0)
1541 ipdevice = g_hash_table_lookup(ipdevice_hash,
1542 GINT_TO_POINTER(ipconfig->index));
1543 if (ipdevice == NULL)
1546 g_free(ipdevice->pac);
1547 ipdevice->pac = g_strdup(url);
1552 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1554 struct connman_ipdevice *ipdevice;
1556 DBG("ipconfig %p", ipconfig);
1558 if (ipconfig == NULL || ipconfig->index < 0)
1561 ipdevice = g_hash_table_lookup(ipdevice_hash,
1562 GINT_TO_POINTER(ipconfig->index));
1563 if (ipdevice == NULL)
1566 return ipdevice->pac;
1569 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1570 const char *address)
1572 if (ipconfig == NULL)
1575 g_free(ipconfig->last_dhcp_address);
1576 ipconfig->last_dhcp_address = g_strdup(address);
1579 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1581 if (ipconfig == NULL)
1584 return ipconfig->last_dhcp_address;
1587 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1589 struct connman_ipdevice *ipdevice;
1593 ipdevice = g_hash_table_lookup(ipdevice_hash,
1594 GINT_TO_POINTER(ipconfig->index));
1595 if (ipdevice == NULL)
1598 set_ipv6_state(ipdevice->ifname, FALSE);
1601 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1603 struct connman_ipdevice *ipdevice;
1607 ipdevice = g_hash_table_lookup(ipdevice_hash,
1608 GINT_TO_POINTER(ipconfig->index));
1609 if (ipdevice == NULL)
1612 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1613 set_ipv6_privacy(ipdevice->ifname,
1614 ipconfig->ipv6_privacy_config);
1616 set_ipv6_state(ipdevice->ifname, TRUE);
1619 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1621 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1624 enable_ipv6(ipconfig);
1627 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1629 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1632 disable_ipv6(ipconfig);
1635 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1637 struct connman_ipdevice *ipdevice;
1638 gboolean up = FALSE, down = FALSE;
1639 gboolean lower_up = FALSE, lower_down = FALSE;
1640 enum connman_ipconfig_type type;
1642 DBG("ipconfig %p", ipconfig);
1644 if (ipconfig == NULL || ipconfig->index < 0)
1647 ipdevice = g_hash_table_lookup(ipdevice_hash,
1648 GINT_TO_POINTER(ipconfig->index));
1649 if (ipdevice == NULL)
1652 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1653 if (ipdevice->config_ipv4 == ipconfig)
1655 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1656 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1657 if (ipdevice->config_ipv6 == ipconfig)
1659 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1660 enable_ipv6(ipconfig);
1664 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1665 ipdevice->config_ipv4 != NULL) {
1666 ipconfig_list = g_list_remove(ipconfig_list,
1667 ipdevice->config_ipv4);
1669 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1671 connman_ipconfig_unref(ipdevice->config_ipv4);
1674 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1675 ipdevice->config_ipv6 != NULL) {
1676 ipconfig_list = g_list_remove(ipconfig_list,
1677 ipdevice->config_ipv6);
1679 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1681 connman_ipconfig_unref(ipdevice->config_ipv6);
1684 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1685 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1686 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1687 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1689 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1691 if (ipdevice->flags & IFF_UP)
1696 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1697 (IFF_RUNNING | IFF_LOWER_UP))
1699 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1702 if (up == TRUE && ipconfig->ops->up)
1703 ipconfig->ops->up(ipconfig);
1704 if (lower_up == TRUE && ipconfig->ops->lower_up)
1705 ipconfig->ops->lower_up(ipconfig);
1707 if (lower_down == TRUE && ipconfig->ops->lower_down)
1708 ipconfig->ops->lower_down(ipconfig);
1709 if (down == TRUE && ipconfig->ops->down)
1710 ipconfig->ops->down(ipconfig);
1715 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1717 struct connman_ipdevice *ipdevice;
1719 DBG("ipconfig %p", ipconfig);
1721 if (ipconfig == NULL || ipconfig->index < 0)
1724 ipdevice = g_hash_table_lookup(ipdevice_hash,
1725 GINT_TO_POINTER(ipconfig->index));
1726 if (ipdevice == NULL)
1729 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1732 if (ipdevice->config_ipv4 == ipconfig) {
1733 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1735 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1736 connman_ipconfig_unref(ipdevice->config_ipv4);
1737 ipdevice->config_ipv4 = NULL;
1741 if (ipdevice->config_ipv6 == ipconfig) {
1742 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1744 if (ipdevice->config_ipv6->method ==
1745 CONNMAN_IPCONFIG_METHOD_AUTO)
1746 disable_ipv6(ipdevice->config_ipv6);
1748 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1749 connman_ipconfig_unref(ipdevice->config_ipv6);
1750 ipdevice->config_ipv6 = NULL;
1757 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1760 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1762 case CONNMAN_IPCONFIG_METHOD_OFF:
1764 case CONNMAN_IPCONFIG_METHOD_FIXED:
1766 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1768 case CONNMAN_IPCONFIG_METHOD_DHCP:
1770 case CONNMAN_IPCONFIG_METHOD_AUTO:
1777 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1779 if (g_strcmp0(method, "off") == 0)
1780 return CONNMAN_IPCONFIG_METHOD_OFF;
1781 else if (g_strcmp0(method, "fixed") == 0)
1782 return CONNMAN_IPCONFIG_METHOD_FIXED;
1783 else if (g_strcmp0(method, "manual") == 0)
1784 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1785 else if (g_strcmp0(method, "dhcp") == 0)
1786 return CONNMAN_IPCONFIG_METHOD_DHCP;
1787 else if (g_strcmp0(method, "auto") == 0)
1788 return CONNMAN_IPCONFIG_METHOD_AUTO;
1790 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1793 static const char *privacy2string(int privacy)
1797 else if (privacy == 1)
1799 else if (privacy > 1)
1805 static int string2privacy(const char *privacy)
1807 if (g_strcmp0(privacy, "disabled") == 0)
1809 else if (g_strcmp0(privacy, "enabled") == 0)
1811 else if (g_strcmp0(privacy, "prefered") == 0)
1817 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1818 DBusMessageIter *iter)
1824 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1827 str = __connman_ipconfig_method2string(ipconfig->method);
1831 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1833 if (ipconfig->system == NULL)
1836 if (ipconfig->system->local != NULL) {
1838 struct in_addr netmask;
1841 connman_dbus_dict_append_basic(iter, "Address",
1842 DBUS_TYPE_STRING, &ipconfig->system->local);
1844 addr = 0xffffffff << (32 - ipconfig->system->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->system->gateway != NULL)
1852 connman_dbus_dict_append_basic(iter, "Gateway",
1853 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1856 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1857 DBusMessageIter *iter,
1858 struct connman_ipconfig *ipconfig_ipv4)
1860 const char *str, *privacy;
1864 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1867 str = __connman_ipconfig_method2string(ipconfig->method);
1871 if (ipconfig_ipv4 != NULL &&
1872 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1873 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1877 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1879 if (ipconfig->system == NULL)
1882 if (ipconfig->system->local != NULL) {
1883 connman_dbus_dict_append_basic(iter, "Address",
1884 DBUS_TYPE_STRING, &ipconfig->system->local);
1885 connman_dbus_dict_append_basic(iter, "PrefixLength",
1887 &ipconfig->system->prefixlen);
1890 if (ipconfig->system->gateway != NULL)
1891 connman_dbus_dict_append_basic(iter, "Gateway",
1892 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1894 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1895 connman_dbus_dict_append_basic(iter, "Privacy",
1896 DBUS_TYPE_STRING, &privacy);
1899 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1900 DBusMessageIter *iter)
1902 const char *str, *privacy;
1906 str = __connman_ipconfig_method2string(ipconfig->method);
1910 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1912 switch (ipconfig->method) {
1913 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1914 case CONNMAN_IPCONFIG_METHOD_OFF:
1915 case CONNMAN_IPCONFIG_METHOD_DHCP:
1917 case CONNMAN_IPCONFIG_METHOD_FIXED:
1918 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1919 case CONNMAN_IPCONFIG_METHOD_AUTO:
1923 if (ipconfig->address == NULL)
1926 if (ipconfig->address->local != NULL) {
1927 connman_dbus_dict_append_basic(iter, "Address",
1928 DBUS_TYPE_STRING, &ipconfig->address->local);
1929 connman_dbus_dict_append_basic(iter, "PrefixLength",
1931 &ipconfig->address->prefixlen);
1934 if (ipconfig->address->gateway != NULL)
1935 connman_dbus_dict_append_basic(iter, "Gateway",
1936 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1938 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1939 connman_dbus_dict_append_basic(iter, "Privacy",
1940 DBUS_TYPE_STRING, &privacy);
1943 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1944 DBusMessageIter *iter)
1950 str = __connman_ipconfig_method2string(ipconfig->method);
1954 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1956 switch (ipconfig->method) {
1957 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1958 case CONNMAN_IPCONFIG_METHOD_OFF:
1959 case CONNMAN_IPCONFIG_METHOD_DHCP:
1960 case CONNMAN_IPCONFIG_METHOD_AUTO:
1962 case CONNMAN_IPCONFIG_METHOD_FIXED:
1963 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1967 if (ipconfig->address == NULL)
1970 if (ipconfig->address->local != NULL) {
1972 struct in_addr netmask;
1975 connman_dbus_dict_append_basic(iter, "Address",
1976 DBUS_TYPE_STRING, &ipconfig->address->local);
1978 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1979 netmask.s_addr = htonl(addr);
1980 mask = inet_ntoa(netmask);
1981 connman_dbus_dict_append_basic(iter, "Netmask",
1982 DBUS_TYPE_STRING, &mask);
1985 if (ipconfig->address->gateway != NULL)
1986 connman_dbus_dict_append_basic(iter, "Gateway",
1987 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1990 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1991 DBusMessageIter *array)
1993 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1994 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1995 *prefix_length_string = NULL, *privacy_string = NULL;
1996 int prefix_length = 0, privacy = 0;
1997 DBusMessageIter dict;
1999 DBG("ipconfig %p", ipconfig);
2001 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2004 dbus_message_iter_recurse(array, &dict);
2006 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2007 DBusMessageIter entry;
2011 dbus_message_iter_recurse(&dict, &entry);
2013 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2016 dbus_message_iter_get_basic(&entry, &key);
2017 dbus_message_iter_next(&entry);
2019 type = dbus_message_iter_get_arg_type(&entry);
2021 if (g_str_equal(key, "Method") == TRUE) {
2024 if (type != DBUS_TYPE_STRING)
2027 dbus_message_iter_get_basic(&entry, &str);
2028 method = __connman_ipconfig_string2method(str);
2029 } else if (g_str_equal(key, "Address") == TRUE) {
2030 if (type != DBUS_TYPE_STRING)
2033 dbus_message_iter_get_basic(&entry, &address);
2034 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2035 if (type != DBUS_TYPE_STRING)
2038 dbus_message_iter_get_basic(&entry,
2039 &prefix_length_string);
2041 prefix_length = atoi(prefix_length_string);
2042 if (prefix_length < 0 || prefix_length > 128)
2045 } else if (g_str_equal(key, "Netmask") == TRUE) {
2046 if (type != DBUS_TYPE_STRING)
2049 dbus_message_iter_get_basic(&entry, &netmask);
2050 } else if (g_str_equal(key, "Gateway") == TRUE) {
2051 if (type != DBUS_TYPE_STRING)
2054 dbus_message_iter_get_basic(&entry, &gateway);
2055 } else if (g_str_equal(key, "Privacy") == TRUE) {
2056 if (type != DBUS_TYPE_STRING)
2059 dbus_message_iter_get_basic(&entry, &privacy_string);
2060 privacy = string2privacy(privacy_string);
2062 dbus_message_iter_next(&dict);
2065 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2067 method, address, netmask, gateway, prefix_length,
2071 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2072 case CONNMAN_IPCONFIG_METHOD_FIXED:
2075 case CONNMAN_IPCONFIG_METHOD_OFF:
2076 ipconfig->method = method;
2077 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2078 disable_ipv6(ipconfig);
2081 case CONNMAN_IPCONFIG_METHOD_AUTO:
2082 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2085 ipconfig->method = method;
2086 if (privacy_string != NULL)
2087 ipconfig->ipv6_privacy_config = privacy;
2088 enable_ipv6(ipconfig);
2091 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2092 if (address == NULL)
2095 ipconfig->method = method;
2097 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2098 connman_ipaddress_set_ipv4(ipconfig->address,
2099 address, netmask, gateway);
2101 return connman_ipaddress_set_ipv6(
2102 ipconfig->address, address,
2103 prefix_length, gateway);
2106 case CONNMAN_IPCONFIG_METHOD_DHCP:
2107 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2110 ipconfig->method = method;
2117 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2118 DBusMessageIter *iter)
2120 struct connman_ipdevice *ipdevice;
2121 const char *method = "auto";
2123 connman_dbus_dict_append_basic(iter, "Method",
2124 DBUS_TYPE_STRING, &method);
2126 ipdevice = g_hash_table_lookup(ipdevice_hash,
2127 GINT_TO_POINTER(ipconfig->index));
2128 if (ipdevice == NULL)
2131 if (ipdevice->ifname != NULL)
2132 connman_dbus_dict_append_basic(iter, "Interface",
2133 DBUS_TYPE_STRING, &ipdevice->ifname);
2135 if (ipdevice->address != NULL)
2136 connman_dbus_dict_append_basic(iter, "Address",
2137 DBUS_TYPE_STRING, &ipdevice->address);
2139 if (ipdevice->mtu > 0)
2140 connman_dbus_dict_append_basic(iter, "MTU",
2141 DBUS_TYPE_UINT16, &ipdevice->mtu);
2144 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2145 GKeyFile *keyfile, const char *identifier, const char *prefix)
2151 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2153 key = g_strdup_printf("%smethod", prefix);
2154 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2155 if (method == NULL) {
2156 switch (ipconfig->type) {
2157 case CONNMAN_IPCONFIG_TYPE_IPV4:
2158 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2160 case CONNMAN_IPCONFIG_TYPE_IPV6:
2161 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2163 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2164 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2168 ipconfig->method = __connman_ipconfig_string2method(method);
2170 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2171 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2173 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2174 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2175 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2177 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2178 privacy = g_key_file_get_string(keyfile, identifier,
2180 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2184 __connman_ipconfig_enable(ipconfig);
2185 enable_ipv6(ipconfig);
2192 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2193 ipconfig->address->prefixlen = g_key_file_get_integer(
2194 keyfile, identifier, key, NULL);
2197 key = g_strdup_printf("%slocal_address", prefix);
2198 ipconfig->address->local = g_key_file_get_string(
2199 keyfile, identifier, key, NULL);
2202 key = g_strdup_printf("%speer_address", prefix);
2203 ipconfig->address->peer = g_key_file_get_string(
2204 keyfile, identifier, key, NULL);
2207 key = g_strdup_printf("%sbroadcast_address", prefix);
2208 ipconfig->address->broadcast = g_key_file_get_string(
2209 keyfile, identifier, key, NULL);
2212 key = g_strdup_printf("%sgateway", prefix);
2213 ipconfig->address->gateway = g_key_file_get_string(
2214 keyfile, identifier, key, NULL);
2217 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2218 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2220 g_free(ipconfig->last_dhcp_address);
2221 ipconfig->last_dhcp_address = str;
2228 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2229 GKeyFile *keyfile, const char *identifier, const char *prefix)
2234 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2236 method = __connman_ipconfig_method2string(ipconfig->method);
2238 key = g_strdup_printf("%smethod", prefix);
2239 g_key_file_set_string(keyfile, identifier, key, method);
2242 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2243 const char *privacy;
2244 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2245 key = g_strdup_printf("%sprivacy", prefix);
2246 g_key_file_set_string(keyfile, identifier, key, privacy);
2250 switch (ipconfig->method) {
2251 case CONNMAN_IPCONFIG_METHOD_FIXED:
2252 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2254 case CONNMAN_IPCONFIG_METHOD_DHCP:
2255 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2256 if (ipconfig->last_dhcp_address != NULL &&
2257 strlen(ipconfig->last_dhcp_address) > 0)
2258 g_key_file_set_string(keyfile, identifier, key,
2259 ipconfig->last_dhcp_address);
2261 g_key_file_remove_key(keyfile, identifier, key, NULL);
2264 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2265 case CONNMAN_IPCONFIG_METHOD_OFF:
2266 case CONNMAN_IPCONFIG_METHOD_AUTO:
2270 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2271 g_key_file_set_integer(keyfile, identifier,
2272 key, ipconfig->address->prefixlen);
2275 key = g_strdup_printf("%slocal_address", prefix);
2276 if (ipconfig->address->local != NULL)
2277 g_key_file_set_string(keyfile, identifier,
2278 key, ipconfig->address->local);
2281 key = g_strdup_printf("%speer_address", prefix);
2282 if (ipconfig->address->peer != NULL)
2283 g_key_file_set_string(keyfile, identifier,
2284 key, ipconfig->address->peer);
2287 key = g_strdup_printf("%sbroadcast_address", prefix);
2288 if (ipconfig->address->broadcast != NULL)
2289 g_key_file_set_string(keyfile, identifier,
2290 key, ipconfig->address->broadcast);
2293 key = g_strdup_printf("%sgateway", prefix);
2294 if (ipconfig->address->gateway != NULL)
2295 g_key_file_set_string(keyfile, identifier,
2296 key, ipconfig->address->gateway);
2302 int __connman_ipconfig_init(void)
2306 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2307 NULL, free_ipdevice);
2312 void __connman_ipconfig_cleanup(void)
2316 g_hash_table_destroy(ipdevice_hash);
2317 ipdevice_hash = NULL;