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 connman_bool_t enabled;
53 enum connman_ipconfig_method method;
54 struct connman_ipaddress *address;
55 struct connman_ipaddress *system;
57 int ipv6_privacy_config;
58 char *last_dhcp_address;
61 struct connman_ipdevice {
83 struct connman_ipconfig *config_ipv4;
84 struct connman_ipconfig *config_ipv6;
86 gboolean ipv6_enabled;
90 static GHashTable *ipdevice_hash = NULL;
91 static GList *ipconfig_list = NULL;
93 struct connman_ipaddress *connman_ipaddress_alloc(int family)
95 struct connman_ipaddress *ipaddress;
97 ipaddress = g_try_new0(struct connman_ipaddress, 1);
98 if (ipaddress == NULL)
101 ipaddress->family = family;
102 ipaddress->prefixlen = 0;
103 ipaddress->local = NULL;
104 ipaddress->peer = NULL;
105 ipaddress->broadcast = NULL;
106 ipaddress->gateway = NULL;
111 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
113 if (ipaddress == NULL)
116 g_free(ipaddress->broadcast);
117 g_free(ipaddress->peer);
118 g_free(ipaddress->local);
119 g_free(ipaddress->gateway);
123 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
132 mask = inet_network(netmask);
135 /* a valid netmask must be 2^n - 1 */
136 if ((host & (host + 1)) != 0)
140 for (; mask; mask <<= 1)
146 static gboolean check_ipv6_address(const char *address)
148 unsigned char buf[sizeof(struct in6_addr)];
154 err = inet_pton(AF_INET6, address, buf);
161 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
163 unsigned char prefix_length,
166 if (ipaddress == NULL)
169 if (check_ipv6_address(address) == FALSE)
172 if (check_ipv6_address(gateway) == FALSE)
175 DBG("prefix_len %d address %s gateway %s",
176 prefix_length, address, gateway);
178 ipaddress->family = AF_INET6;
180 ipaddress->prefixlen = prefix_length;
182 g_free(ipaddress->local);
183 ipaddress->local = g_strdup(address);
185 g_free(ipaddress->gateway);
186 ipaddress->gateway = g_strdup(gateway);
191 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
192 const char *address, const char *netmask, const char *gateway)
194 if (ipaddress == NULL)
197 ipaddress->family = AF_INET;
199 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
201 g_free(ipaddress->local);
202 ipaddress->local = g_strdup(address);
204 g_free(ipaddress->gateway);
205 ipaddress->gateway = g_strdup(gateway);
210 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
213 if (ipaddress == NULL)
216 g_free(ipaddress->peer);
217 ipaddress->peer = g_strdup(peer);
220 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
222 if (ipaddress == NULL)
225 ipaddress->prefixlen = 0;
227 g_free(ipaddress->local);
228 ipaddress->local = NULL;
230 g_free(ipaddress->peer);
231 ipaddress->peer = NULL;
233 g_free(ipaddress->broadcast);
234 ipaddress->broadcast = NULL;
236 g_free(ipaddress->gateway);
237 ipaddress->gateway = NULL;
240 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
241 struct connman_ipaddress *source)
243 if (ipaddress == NULL || source == NULL)
246 ipaddress->family = source->family;
247 ipaddress->prefixlen = source->prefixlen;
249 g_free(ipaddress->local);
250 ipaddress->local = g_strdup(source->local);
252 g_free(ipaddress->peer);
253 ipaddress->peer = g_strdup(source->peer);
255 g_free(ipaddress->broadcast);
256 ipaddress->broadcast = g_strdup(source->broadcast);
258 g_free(ipaddress->gateway);
259 ipaddress->gateway = g_strdup(source->gateway);
262 static void free_address_list(struct connman_ipdevice *ipdevice)
266 for (list = ipdevice->address_list; list; list = list->next) {
267 struct connman_ipaddress *ipaddress = list->data;
269 connman_ipaddress_free(ipaddress);
273 g_slist_free(ipdevice->address_list);
274 ipdevice->address_list = NULL;
277 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
278 unsigned char prefixlen, const char *local)
282 for (list = ipdevice->address_list; list; list = list->next) {
283 struct connman_ipaddress *ipaddress = list->data;
285 if (g_strcmp0(ipaddress->local, local) == 0 &&
286 ipaddress->prefixlen == prefixlen)
293 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
296 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
298 case CONNMAN_IPCONFIG_TYPE_IPV4:
300 case CONNMAN_IPCONFIG_TYPE_IPV6:
307 static const char *type2str(unsigned short type)
312 case ARPHRD_LOOPBACK:
325 static const char *scope2str(unsigned char scope)
337 static gboolean get_ipv6_state(gchar *ifname)
342 gboolean enabled = FALSE;
345 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
347 path = g_strdup_printf(
348 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
353 f = fopen(path, "r");
358 if (fscanf(f, "%d", &disabled) > 0)
366 static void set_ipv6_state(gchar *ifname, gboolean enable)
372 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
374 path = g_strdup_printf(
375 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
380 f = fopen(path, "r+");
395 static int get_ipv6_privacy(gchar *ifname)
404 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
410 f = fopen(path, "r");
417 if (fscanf(f, "%d", &value) <= 0)
425 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
426 * The privacy extension is described in RFC 3041 and RFC 4941
428 static void set_ipv6_privacy(gchar *ifname, int value)
436 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
445 f = fopen(path, "r+");
452 fprintf(f, "%d", value);
456 static int get_rp_filter()
459 int value = -EINVAL, tmp;
461 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
464 if (fscanf(f, "%d", &tmp) == 1)
472 static void set_rp_filter(int value)
476 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
481 fprintf(f, "%d", value);
486 int __connman_ipconfig_set_rp_filter()
490 value = get_rp_filter();
497 connman_info("rp_filter set to 2 (loose mode routing), "
498 "old value was %d", value);
503 void __connman_ipconfig_unset_rp_filter(int old_value)
505 set_rp_filter(old_value);
507 connman_info("rp_filter restored to %d", old_value);
510 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
512 if (ipconfig == NULL)
515 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
518 static void free_ipdevice(gpointer data)
520 struct connman_ipdevice *ipdevice = data;
522 connman_info("%s {remove} index %d", ipdevice->ifname,
525 if (ipdevice->config_ipv4 != NULL) {
526 __connman_ipconfig_unref(ipdevice->config_ipv4);
527 ipdevice->config_ipv4 = NULL;
530 if (ipdevice->config_ipv6 != NULL) {
531 __connman_ipconfig_unref(ipdevice->config_ipv6);
532 ipdevice->config_ipv6 = NULL;
535 free_address_list(ipdevice);
536 g_free(ipdevice->ipv4_gateway);
537 g_free(ipdevice->ipv6_gateway);
538 g_free(ipdevice->pac);
540 g_free(ipdevice->address);
542 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
543 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
545 g_free(ipdevice->ifname);
549 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
551 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
552 ipdevice->config_ipv6);
555 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
557 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
558 ipdevice->config_ipv6);
560 if (ipdevice->config_ipv4)
561 connman_inet_clear_address(ipdevice->index,
562 ipdevice->config_ipv4->address);
564 if (ipdevice->config_ipv6)
565 connman_inet_clear_ipv6_address(ipdevice->index,
566 ipdevice->config_ipv6->address->local,
567 ipdevice->config_ipv6->address->prefixlen);
570 static void update_stats(struct connman_ipdevice *ipdevice,
571 struct rtnl_link_stats *stats)
573 struct connman_service *service;
575 if (stats->rx_packets == 0 && stats->tx_packets == 0)
578 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
579 stats->rx_packets, stats->rx_bytes);
580 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
581 stats->tx_packets, stats->tx_bytes);
583 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
586 if (ipdevice->config_ipv4)
587 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
588 else if (ipdevice->config_ipv6)
589 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
596 ipdevice->rx_packets = stats->rx_packets;
597 ipdevice->tx_packets = stats->tx_packets;
598 ipdevice->rx_bytes = stats->rx_bytes;
599 ipdevice->tx_bytes = stats->tx_bytes;
600 ipdevice->rx_errors = stats->rx_errors;
601 ipdevice->tx_errors = stats->tx_errors;
602 ipdevice->rx_dropped = stats->rx_dropped;
603 ipdevice->tx_dropped = stats->tx_dropped;
605 __connman_service_notify(service,
606 ipdevice->rx_packets, ipdevice->tx_packets,
607 ipdevice->rx_bytes, ipdevice->tx_bytes,
608 ipdevice->rx_errors, ipdevice->tx_errors,
609 ipdevice->rx_dropped, ipdevice->tx_dropped);
612 void __connman_ipconfig_newlink(int index, unsigned short type,
613 unsigned int flags, const char *address,
615 struct rtnl_link_stats *stats)
617 struct connman_ipdevice *ipdevice;
620 gboolean up = FALSE, down = FALSE;
621 gboolean lower_up = FALSE, lower_down = FALSE;
623 DBG("index %d", index);
625 if (type == ARPHRD_LOOPBACK)
628 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
629 if (ipdevice != NULL)
632 ipdevice = g_try_new0(struct connman_ipdevice, 1);
633 if (ipdevice == NULL)
636 ipdevice->index = index;
637 ipdevice->ifname = connman_inet_ifname(index);
638 ipdevice->type = type;
640 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
641 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
643 ipdevice->address = g_strdup(address);
645 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
647 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
648 index, type, type2str(type));
653 update_stats(ipdevice, stats);
655 if (flags == ipdevice->flags)
658 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
665 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
666 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
667 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
668 (IFF_RUNNING | IFF_LOWER_UP))
670 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
674 ipdevice->flags = flags;
676 str = g_string_new(NULL);
681 g_string_append(str, "UP");
683 g_string_append(str, "DOWN");
685 if (flags & IFF_RUNNING)
686 g_string_append(str, ",RUNNING");
688 if (flags & IFF_LOWER_UP)
689 g_string_append(str, ",LOWER_UP");
691 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
694 g_string_free(str, TRUE);
696 for (list = g_list_first(ipconfig_list); list;
697 list = g_list_next(list)) {
698 struct connman_ipconfig *ipconfig = list->data;
700 if (index != ipconfig->index)
703 if (ipconfig->ops == NULL)
706 if (up == TRUE && ipconfig->ops->up)
707 ipconfig->ops->up(ipconfig);
708 if (lower_up == TRUE && ipconfig->ops->lower_up)
709 ipconfig->ops->lower_up(ipconfig);
711 if (lower_down == TRUE && ipconfig->ops->lower_down)
712 ipconfig->ops->lower_down(ipconfig);
713 if (down == TRUE && ipconfig->ops->down)
714 ipconfig->ops->down(ipconfig);
718 __connman_ipconfig_lower_up(ipdevice);
720 __connman_ipconfig_lower_down(ipdevice);
723 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
725 struct connman_ipdevice *ipdevice;
728 DBG("index %d", index);
730 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
731 if (ipdevice == NULL)
734 update_stats(ipdevice, stats);
736 for (list = g_list_first(ipconfig_list); list;
737 list = g_list_next(list)) {
738 struct connman_ipconfig *ipconfig = list->data;
740 if (index != ipconfig->index)
743 ipconfig->index = -1;
745 if (ipconfig->ops == NULL)
748 if (ipconfig->ops->lower_down)
749 ipconfig->ops->lower_down(ipconfig);
750 if (ipconfig->ops->down)
751 ipconfig->ops->down(ipconfig);
754 __connman_ipconfig_lower_down(ipdevice);
756 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
759 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
761 const struct connman_ipaddress *addr1 = a;
762 const struct connman_ipaddress *addr2 = b;
764 if (addr1->prefixlen != addr2->prefixlen)
765 return addr2->prefixlen - addr1->prefixlen;
767 return g_strcmp0(addr1->local, addr2->local);
770 void __connman_ipconfig_newaddr(int index, int family, const char *label,
771 unsigned char prefixlen, const char *address)
773 struct connman_ipdevice *ipdevice;
774 struct connman_ipaddress *ipaddress;
775 enum connman_ipconfig_type type;
778 DBG("index %d", index);
780 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
781 if (ipdevice == NULL)
784 ipaddress = connman_ipaddress_alloc(family);
785 if (ipaddress == NULL)
788 ipaddress->prefixlen = prefixlen;
789 ipaddress->local = g_strdup(address);
791 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
792 check_duplicate_address)) {
793 connman_ipaddress_free(ipaddress);
797 if (family == AF_INET)
798 type = CONNMAN_IPCONFIG_TYPE_IPV4;
799 else if (family == AF_INET6)
800 type = CONNMAN_IPCONFIG_TYPE_IPV6;
804 ipdevice->address_list = g_slist_append(ipdevice->address_list,
807 connman_info("%s {add} address %s/%u label %s family %d",
808 ipdevice->ifname, address, prefixlen, label, family);
810 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
811 __connman_ippool_newaddr(index, address, prefixlen);
813 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
814 connman_ipaddress_copy(ipdevice->config_ipv4->system,
817 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
818 connman_ipaddress_copy(ipdevice->config_ipv6->system,
823 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
826 for (list = g_list_first(ipconfig_list); list;
827 list = g_list_next(list)) {
828 struct connman_ipconfig *ipconfig = list->data;
830 if (index != ipconfig->index)
833 if (type != ipconfig->type)
836 if (ipconfig->ops == NULL)
839 if (ipconfig->ops->ip_bound)
840 ipconfig->ops->ip_bound(ipconfig);
844 void __connman_ipconfig_deladdr(int index, int family, const char *label,
845 unsigned char prefixlen, const char *address)
847 struct connman_ipdevice *ipdevice;
848 struct connman_ipaddress *ipaddress;
849 enum connman_ipconfig_type type;
852 DBG("index %d", index);
854 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
855 if (ipdevice == NULL)
858 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
859 if (ipaddress == NULL)
862 if (family == AF_INET)
863 type = CONNMAN_IPCONFIG_TYPE_IPV4;
864 else if (family == AF_INET6)
865 type = CONNMAN_IPCONFIG_TYPE_IPV6;
869 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
872 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
873 __connman_ippool_deladdr(index, address, prefixlen);
875 connman_ipaddress_clear(ipaddress);
878 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
879 address, prefixlen, label);
881 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
884 if (g_slist_length(ipdevice->address_list) > 0)
887 for (list = g_list_first(ipconfig_list); list;
888 list = g_list_next(list)) {
889 struct connman_ipconfig *ipconfig = list->data;
891 if (index != ipconfig->index)
894 if (type != ipconfig->type)
897 if (ipconfig->ops == NULL)
900 if (ipconfig->ops->ip_release)
901 ipconfig->ops->ip_release(ipconfig);
905 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
906 const char *dst, const char *gateway)
908 struct connman_ipdevice *ipdevice;
910 DBG("index %d", index);
912 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
913 if (ipdevice == NULL)
916 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
917 g_strcmp0(dst, "::") == 0)) {
919 enum connman_ipconfig_type type;
921 if (family == AF_INET6) {
922 type = CONNMAN_IPCONFIG_TYPE_IPV6;
923 g_free(ipdevice->ipv6_gateway);
924 ipdevice->ipv6_gateway = g_strdup(gateway);
926 if (ipdevice->config_ipv6 != NULL &&
927 ipdevice->config_ipv6->system != NULL) {
928 g_free(ipdevice->config_ipv6->system->gateway);
929 ipdevice->config_ipv6->system->gateway =
932 } else if (family == AF_INET) {
933 type = CONNMAN_IPCONFIG_TYPE_IPV4;
934 g_free(ipdevice->ipv4_gateway);
935 ipdevice->ipv4_gateway = g_strdup(gateway);
937 if (ipdevice->config_ipv4 != NULL &&
938 ipdevice->config_ipv4->system != NULL) {
939 g_free(ipdevice->config_ipv4->system->gateway);
940 ipdevice->config_ipv4->system->gateway =
946 for (config_list = g_list_first(ipconfig_list); config_list;
947 config_list = g_list_next(config_list)) {
948 struct connman_ipconfig *ipconfig = config_list->data;
950 if (index != ipconfig->index)
953 if (type != ipconfig->type)
956 if (ipconfig->ops == NULL)
959 if (ipconfig->ops->route_set)
960 ipconfig->ops->route_set(ipconfig);
964 connman_info("%s {add} route %s gw %s scope %u <%s>",
965 ipdevice->ifname, dst, gateway,
966 scope, scope2str(scope));
969 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
970 const char *dst, const char *gateway)
972 struct connman_ipdevice *ipdevice;
974 DBG("index %d", index);
976 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
977 if (ipdevice == NULL)
980 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
981 g_strcmp0(dst, "::") == 0)) {
983 enum connman_ipconfig_type type;
985 if (family == AF_INET6) {
986 type = CONNMAN_IPCONFIG_TYPE_IPV6;
987 g_free(ipdevice->ipv6_gateway);
988 ipdevice->ipv6_gateway = NULL;
990 if (ipdevice->config_ipv6 != NULL &&
991 ipdevice->config_ipv6->system != NULL) {
992 g_free(ipdevice->config_ipv6->system->gateway);
993 ipdevice->config_ipv6->system->gateway = NULL;
995 } else if (family == AF_INET) {
996 type = CONNMAN_IPCONFIG_TYPE_IPV4;
997 g_free(ipdevice->ipv4_gateway);
998 ipdevice->ipv4_gateway = NULL;
1000 if (ipdevice->config_ipv4 != NULL &&
1001 ipdevice->config_ipv4->system != NULL) {
1002 g_free(ipdevice->config_ipv4->system->gateway);
1003 ipdevice->config_ipv4->system->gateway = NULL;
1008 for (config_list = g_list_first(ipconfig_list); config_list;
1009 config_list = g_list_next(config_list)) {
1010 struct connman_ipconfig *ipconfig = config_list->data;
1012 if (index != ipconfig->index)
1015 if (type != ipconfig->type)
1018 if (ipconfig->ops == NULL)
1021 if (ipconfig->ops->route_unset)
1022 ipconfig->ops->route_unset(ipconfig);
1026 connman_info("%s {del} route %s gw %s scope %u <%s>",
1027 ipdevice->ifname, dst, gateway,
1028 scope, scope2str(scope));
1031 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1036 keys = g_hash_table_get_keys(ipdevice_hash);
1040 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1041 int index = GPOINTER_TO_INT(list->data);
1043 function(index, user_data);
1049 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1050 struct connman_ipconfig *ipconfig)
1052 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1055 unsigned short __connman_ipconfig_get_type_from_index(int index)
1057 struct connman_ipdevice *ipdevice;
1059 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1060 if (ipdevice == NULL)
1063 return ipdevice->type;
1066 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1068 struct connman_ipdevice *ipdevice;
1070 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1071 if (ipdevice == NULL)
1074 return ipdevice->flags;
1077 const char *__connman_ipconfig_get_gateway_from_index(int index)
1079 struct connman_ipdevice *ipdevice;
1081 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1082 if (ipdevice == NULL)
1085 if (ipdevice->ipv4_gateway != NULL)
1086 return ipdevice->ipv4_gateway;
1088 if (ipdevice->config_ipv4 != NULL &&
1089 ipdevice->config_ipv4->address != NULL)
1090 return ipdevice->config_ipv4->address->gateway;
1092 if (ipdevice->ipv6_gateway != NULL)
1093 return ipdevice->ipv6_gateway;
1095 if (ipdevice->config_ipv6 != NULL &&
1096 ipdevice->config_ipv6->address != NULL)
1097 return ipdevice->config_ipv6->address->gateway;
1102 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1104 ipconfig->index = index;
1107 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1109 if (ipconfig->address == NULL)
1112 return ipconfig->address->local;
1115 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1117 if (ipconfig->address == NULL)
1120 g_free(ipconfig->address->local);
1121 ipconfig->address->local = g_strdup(address);
1124 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1126 if (ipconfig->address == NULL)
1129 return ipconfig->address->peer;
1132 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1134 if (ipconfig->address == NULL)
1137 g_free(ipconfig->address->peer);
1138 ipconfig->address->peer = g_strdup(address);
1141 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1143 if (ipconfig->address == NULL)
1146 return ipconfig->address->broadcast;
1149 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1151 if (ipconfig->address == NULL)
1154 g_free(ipconfig->address->broadcast);
1155 ipconfig->address->broadcast = g_strdup(broadcast);
1158 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1160 if (ipconfig->address == NULL)
1163 return ipconfig->address->gateway;
1166 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1170 if (ipconfig->address == NULL)
1172 g_free(ipconfig->address->gateway);
1173 ipconfig->address->gateway = g_strdup(gateway);
1176 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1178 struct connman_service *service;
1182 if (ipconfig->address == NULL)
1185 service = __connman_service_lookup_from_index(ipconfig->index);
1186 if (service == NULL)
1189 __connman_connection_gateway_remove(service, ipconfig->type);
1191 DBG("type %d gw %s peer %s", ipconfig->type,
1192 ipconfig->address->gateway, ipconfig->address->peer);
1194 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1195 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1196 return __connman_connection_gateway_add(service,
1197 ipconfig->address->gateway,
1199 ipconfig->address->peer);
1204 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1206 struct connman_service *service;
1210 service = __connman_service_lookup_from_index(ipconfig->index);
1211 if (service != NULL)
1212 __connman_connection_gateway_remove(service, ipconfig->type);
1215 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1217 if (ipconfig->address == NULL)
1220 return ipconfig->address->prefixlen;
1223 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1225 if (ipconfig->address == NULL)
1228 ipconfig->address->prefixlen = prefixlen;
1231 static struct connman_ipconfig *create_ipv6config(int index)
1233 struct connman_ipconfig *ipv6config;
1235 DBG("index %d", index);
1237 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1238 if (ipv6config == NULL)
1241 ipv6config->refcount = 1;
1243 ipv6config->index = index;
1244 ipv6config->enabled = FALSE;
1245 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1246 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1247 ipv6config->ipv6_privacy_config = 0;
1249 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1250 if (ipv6config->address == NULL) {
1255 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1257 DBG("ipconfig %p", ipv6config);
1263 * connman_ipconfig_create:
1265 * Allocate a new ipconfig structure.
1267 * Returns: a newly-allocated #connman_ipconfig structure
1269 struct connman_ipconfig *__connman_ipconfig_create(int index,
1270 enum connman_ipconfig_type type)
1272 struct connman_ipconfig *ipconfig;
1274 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1275 return create_ipv6config(index);
1277 DBG("index %d", index);
1279 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1280 if (ipconfig == NULL)
1283 ipconfig->refcount = 1;
1285 ipconfig->index = index;
1286 ipconfig->enabled = FALSE;
1287 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1289 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1290 if (ipconfig->address == NULL) {
1295 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1297 DBG("ipconfig %p", ipconfig);
1304 * connman_ipconfig_ref:
1305 * @ipconfig: ipconfig structure
1307 * Increase reference counter of ipconfig
1309 struct connman_ipconfig *
1310 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1311 const char *file, int line, const char *caller)
1313 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1314 file, line, caller);
1316 __sync_fetch_and_add(&ipconfig->refcount, 1);
1322 * connman_ipconfig_unref:
1323 * @ipconfig: ipconfig structure
1325 * Decrease reference counter of ipconfig
1327 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1328 const char *file, int line, const char *caller)
1330 if (ipconfig == NULL)
1333 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1334 file, line, caller);
1336 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1339 if (__connman_ipconfig_disable(ipconfig) < 0)
1340 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1342 __connman_ipconfig_set_ops(ipconfig, NULL);
1344 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1345 __connman_ipconfig_unref(ipconfig->origin);
1346 ipconfig->origin = NULL;
1349 connman_ipaddress_free(ipconfig->system);
1350 connman_ipaddress_free(ipconfig->address);
1351 g_free(ipconfig->last_dhcp_address);
1356 * connman_ipconfig_get_data:
1357 * @ipconfig: ipconfig structure
1359 * Get private data pointer
1361 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1363 if (ipconfig == NULL)
1366 return ipconfig->ops_data;
1370 * connman_ipconfig_set_data:
1371 * @ipconfig: ipconfig structure
1372 * @data: data pointer
1374 * Set private data pointer
1376 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1378 ipconfig->ops_data = data;
1382 * connman_ipconfig_get_index:
1383 * @ipconfig: ipconfig structure
1385 * Get interface index
1387 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1389 if (ipconfig == NULL)
1392 if (ipconfig->origin != NULL)
1393 return ipconfig->origin->index;
1395 return ipconfig->index;
1399 * connman_ipconfig_get_ifname:
1400 * @ipconfig: ipconfig structure
1402 * Get interface name
1404 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1406 struct connman_ipdevice *ipdevice;
1408 if (ipconfig == NULL)
1411 if (ipconfig->index < 0)
1414 ipdevice = g_hash_table_lookup(ipdevice_hash,
1415 GINT_TO_POINTER(ipconfig->index));
1416 if (ipdevice == NULL)
1419 return ipdevice->ifname;
1423 * connman_ipconfig_set_ops:
1424 * @ipconfig: ipconfig structure
1425 * @ops: operation callbacks
1427 * Set the operation callbacks
1429 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1430 const struct connman_ipconfig_ops *ops)
1432 ipconfig->ops = ops;
1436 * connman_ipconfig_set_method:
1437 * @ipconfig: ipconfig structure
1438 * @method: configuration method
1440 * Set the configuration method
1442 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1443 enum connman_ipconfig_method method)
1445 ipconfig->method = method;
1450 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1452 if (ipconfig == NULL)
1453 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1455 return ipconfig->method;
1458 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1462 switch (ipconfig->method) {
1463 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1464 case CONNMAN_IPCONFIG_METHOD_OFF:
1466 case CONNMAN_IPCONFIG_METHOD_AUTO:
1467 case CONNMAN_IPCONFIG_METHOD_FIXED:
1468 case CONNMAN_IPCONFIG_METHOD_DHCP:
1469 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1470 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1471 return connman_inet_set_address(ipconfig->index,
1473 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1474 return connman_inet_set_ipv6_address(
1475 ipconfig->index, ipconfig->address);
1481 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1487 if (ipconfig == NULL)
1490 DBG("method %d", ipconfig->method);
1492 switch (ipconfig->method) {
1493 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1494 case CONNMAN_IPCONFIG_METHOD_OFF:
1496 case CONNMAN_IPCONFIG_METHOD_AUTO:
1497 case CONNMAN_IPCONFIG_METHOD_FIXED:
1498 case CONNMAN_IPCONFIG_METHOD_DHCP:
1499 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1500 err = __connman_ipconfig_address_unset(ipconfig);
1501 connman_ipaddress_clear(ipconfig->address);
1509 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1515 if (ipconfig == NULL)
1518 DBG("method %d", ipconfig->method);
1520 switch (ipconfig->method) {
1521 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1522 case CONNMAN_IPCONFIG_METHOD_OFF:
1524 case CONNMAN_IPCONFIG_METHOD_AUTO:
1525 case CONNMAN_IPCONFIG_METHOD_FIXED:
1526 case CONNMAN_IPCONFIG_METHOD_DHCP:
1527 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1528 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1529 err = connman_inet_clear_address(ipconfig->index,
1531 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1532 err = connman_inet_clear_ipv6_address(
1534 ipconfig->address->local,
1535 ipconfig->address->prefixlen);
1545 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1548 struct connman_ipdevice *ipdevice;
1550 DBG("ipconfig %p", ipconfig);
1552 if (ipconfig == NULL || ipconfig->index < 0)
1555 ipdevice = g_hash_table_lookup(ipdevice_hash,
1556 GINT_TO_POINTER(ipconfig->index));
1557 if (ipdevice == NULL)
1560 g_free(ipdevice->pac);
1561 ipdevice->pac = g_strdup(url);
1566 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1568 struct connman_ipdevice *ipdevice;
1570 DBG("ipconfig %p", ipconfig);
1572 if (ipconfig == NULL || ipconfig->index < 0)
1575 ipdevice = g_hash_table_lookup(ipdevice_hash,
1576 GINT_TO_POINTER(ipconfig->index));
1577 if (ipdevice == NULL)
1580 return ipdevice->pac;
1583 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1584 const char *address)
1586 if (ipconfig == NULL)
1589 g_free(ipconfig->last_dhcp_address);
1590 ipconfig->last_dhcp_address = g_strdup(address);
1593 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1595 if (ipconfig == NULL)
1598 return ipconfig->last_dhcp_address;
1601 static void disable_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 set_ipv6_state(ipdevice->ifname, FALSE);
1615 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1617 struct connman_ipdevice *ipdevice;
1621 ipdevice = g_hash_table_lookup(ipdevice_hash,
1622 GINT_TO_POINTER(ipconfig->index));
1623 if (ipdevice == NULL)
1626 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1627 set_ipv6_privacy(ipdevice->ifname,
1628 ipconfig->ipv6_privacy_config);
1630 set_ipv6_state(ipdevice->ifname, TRUE);
1633 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1635 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1638 enable_ipv6(ipconfig);
1641 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1643 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1646 disable_ipv6(ipconfig);
1649 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1651 struct connman_ipdevice *ipdevice;
1652 gboolean up = FALSE, down = FALSE;
1653 gboolean lower_up = FALSE, lower_down = FALSE;
1654 enum connman_ipconfig_type type;
1656 DBG("ipconfig %p", ipconfig);
1658 if (ipconfig == NULL || ipconfig->index < 0)
1661 ipdevice = g_hash_table_lookup(ipdevice_hash,
1662 GINT_TO_POINTER(ipconfig->index));
1663 if (ipdevice == NULL)
1666 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1667 if (ipdevice->config_ipv4 == ipconfig)
1669 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1670 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1671 if (ipdevice->config_ipv6 == ipconfig)
1673 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1674 enable_ipv6(ipconfig);
1678 ipconfig->enabled = TRUE;
1680 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1681 ipdevice->config_ipv4 != NULL) {
1682 ipconfig_list = g_list_remove(ipconfig_list,
1683 ipdevice->config_ipv4);
1685 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1687 __connman_ipconfig_unref(ipdevice->config_ipv4);
1690 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1691 ipdevice->config_ipv6 != NULL) {
1692 ipconfig_list = g_list_remove(ipconfig_list,
1693 ipdevice->config_ipv6);
1695 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1697 __connman_ipconfig_unref(ipdevice->config_ipv6);
1700 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1701 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1702 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1703 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1705 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1707 if (ipdevice->flags & IFF_UP)
1712 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1713 (IFF_RUNNING | IFF_LOWER_UP))
1715 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1718 if (up == TRUE && ipconfig->ops->up)
1719 ipconfig->ops->up(ipconfig);
1720 if (lower_up == TRUE && ipconfig->ops->lower_up)
1721 ipconfig->ops->lower_up(ipconfig);
1723 if (lower_down == TRUE && ipconfig->ops->lower_down)
1724 ipconfig->ops->lower_down(ipconfig);
1725 if (down == TRUE && ipconfig->ops->down)
1726 ipconfig->ops->down(ipconfig);
1731 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1733 struct connman_ipdevice *ipdevice;
1735 DBG("ipconfig %p", ipconfig);
1737 if (ipconfig == NULL || ipconfig->index < 0)
1740 ipdevice = g_hash_table_lookup(ipdevice_hash,
1741 GINT_TO_POINTER(ipconfig->index));
1742 if (ipdevice == NULL)
1745 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1748 ipconfig->enabled = FALSE;
1750 if (ipdevice->config_ipv4 == ipconfig) {
1751 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1753 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1754 __connman_ipconfig_unref(ipdevice->config_ipv4);
1755 ipdevice->config_ipv4 = NULL;
1759 if (ipdevice->config_ipv6 == ipconfig) {
1760 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1762 if (ipdevice->config_ipv6->method ==
1763 CONNMAN_IPCONFIG_METHOD_AUTO)
1764 disable_ipv6(ipdevice->config_ipv6);
1766 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1767 __connman_ipconfig_unref(ipdevice->config_ipv6);
1768 ipdevice->config_ipv6 = NULL;
1775 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1778 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1780 case CONNMAN_IPCONFIG_METHOD_OFF:
1782 case CONNMAN_IPCONFIG_METHOD_FIXED:
1784 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1786 case CONNMAN_IPCONFIG_METHOD_DHCP:
1788 case CONNMAN_IPCONFIG_METHOD_AUTO:
1795 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1797 if (g_strcmp0(method, "off") == 0)
1798 return CONNMAN_IPCONFIG_METHOD_OFF;
1799 else if (g_strcmp0(method, "fixed") == 0)
1800 return CONNMAN_IPCONFIG_METHOD_FIXED;
1801 else if (g_strcmp0(method, "manual") == 0)
1802 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1803 else if (g_strcmp0(method, "dhcp") == 0)
1804 return CONNMAN_IPCONFIG_METHOD_DHCP;
1805 else if (g_strcmp0(method, "auto") == 0)
1806 return CONNMAN_IPCONFIG_METHOD_AUTO;
1808 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1811 static const char *privacy2string(int privacy)
1815 else if (privacy == 1)
1817 else if (privacy > 1)
1823 static int string2privacy(const char *privacy)
1825 if (g_strcmp0(privacy, "disabled") == 0)
1827 else if (g_strcmp0(privacy, "enabled") == 0)
1829 else if (g_strcmp0(privacy, "prefered") == 0)
1835 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1836 DBusMessageIter *iter)
1842 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1845 str = __connman_ipconfig_method2string(ipconfig->method);
1849 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1851 if (ipconfig->system == NULL)
1854 if (ipconfig->system->local != NULL) {
1856 struct in_addr netmask;
1859 connman_dbus_dict_append_basic(iter, "Address",
1860 DBUS_TYPE_STRING, &ipconfig->system->local);
1862 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1863 netmask.s_addr = htonl(addr);
1864 mask = inet_ntoa(netmask);
1865 connman_dbus_dict_append_basic(iter, "Netmask",
1866 DBUS_TYPE_STRING, &mask);
1869 if (ipconfig->system->gateway != NULL)
1870 connman_dbus_dict_append_basic(iter, "Gateway",
1871 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1874 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1875 DBusMessageIter *iter,
1876 struct connman_ipconfig *ipconfig_ipv4)
1878 const char *str, *privacy;
1882 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1885 str = __connman_ipconfig_method2string(ipconfig->method);
1889 if (ipconfig_ipv4 != NULL &&
1890 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1891 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1895 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1897 if (ipconfig->system == NULL)
1900 if (ipconfig->system->local != NULL) {
1901 connman_dbus_dict_append_basic(iter, "Address",
1902 DBUS_TYPE_STRING, &ipconfig->system->local);
1903 connman_dbus_dict_append_basic(iter, "PrefixLength",
1905 &ipconfig->system->prefixlen);
1908 if (ipconfig->system->gateway != NULL)
1909 connman_dbus_dict_append_basic(iter, "Gateway",
1910 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1912 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1913 connman_dbus_dict_append_basic(iter, "Privacy",
1914 DBUS_TYPE_STRING, &privacy);
1917 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1918 DBusMessageIter *iter)
1920 const char *str, *privacy;
1924 str = __connman_ipconfig_method2string(ipconfig->method);
1928 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1930 switch (ipconfig->method) {
1931 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1932 case CONNMAN_IPCONFIG_METHOD_OFF:
1933 case CONNMAN_IPCONFIG_METHOD_DHCP:
1935 case CONNMAN_IPCONFIG_METHOD_FIXED:
1936 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1937 case CONNMAN_IPCONFIG_METHOD_AUTO:
1941 if (ipconfig->address == NULL)
1944 if (ipconfig->address->local != NULL) {
1945 connman_dbus_dict_append_basic(iter, "Address",
1946 DBUS_TYPE_STRING, &ipconfig->address->local);
1947 connman_dbus_dict_append_basic(iter, "PrefixLength",
1949 &ipconfig->address->prefixlen);
1952 if (ipconfig->address->gateway != NULL)
1953 connman_dbus_dict_append_basic(iter, "Gateway",
1954 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1956 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1957 connman_dbus_dict_append_basic(iter, "Privacy",
1958 DBUS_TYPE_STRING, &privacy);
1961 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1962 DBusMessageIter *iter)
1968 str = __connman_ipconfig_method2string(ipconfig->method);
1972 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1974 switch (ipconfig->method) {
1975 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1976 case CONNMAN_IPCONFIG_METHOD_OFF:
1977 case CONNMAN_IPCONFIG_METHOD_DHCP:
1978 case CONNMAN_IPCONFIG_METHOD_AUTO:
1980 case CONNMAN_IPCONFIG_METHOD_FIXED:
1981 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1985 if (ipconfig->address == NULL)
1988 if (ipconfig->address->local != NULL) {
1990 struct in_addr netmask;
1993 connman_dbus_dict_append_basic(iter, "Address",
1994 DBUS_TYPE_STRING, &ipconfig->address->local);
1996 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1997 netmask.s_addr = htonl(addr);
1998 mask = inet_ntoa(netmask);
1999 connman_dbus_dict_append_basic(iter, "Netmask",
2000 DBUS_TYPE_STRING, &mask);
2003 if (ipconfig->address->gateway != NULL)
2004 connman_dbus_dict_append_basic(iter, "Gateway",
2005 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2008 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2009 DBusMessageIter *array)
2011 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2012 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2013 *prefix_length_string = NULL, *privacy_string = NULL;
2014 int prefix_length = 0, privacy = 0;
2015 DBusMessageIter dict;
2017 DBG("ipconfig %p", ipconfig);
2019 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2022 dbus_message_iter_recurse(array, &dict);
2024 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2025 DBusMessageIter entry, value;
2029 dbus_message_iter_recurse(&dict, &entry);
2031 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2034 dbus_message_iter_get_basic(&entry, &key);
2035 dbus_message_iter_next(&entry);
2037 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2040 dbus_message_iter_recurse(&entry, &value);
2042 type = dbus_message_iter_get_arg_type(&value);
2044 if (g_str_equal(key, "Method") == TRUE) {
2047 if (type != DBUS_TYPE_STRING)
2050 dbus_message_iter_get_basic(&value, &str);
2051 method = __connman_ipconfig_string2method(str);
2052 } else if (g_str_equal(key, "Address") == TRUE) {
2053 if (type != DBUS_TYPE_STRING)
2056 dbus_message_iter_get_basic(&value, &address);
2057 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2058 if (type != DBUS_TYPE_STRING)
2061 dbus_message_iter_get_basic(&value,
2062 &prefix_length_string);
2064 prefix_length = atoi(prefix_length_string);
2065 if (prefix_length < 0 || prefix_length > 128)
2067 } else if (g_str_equal(key, "Netmask") == TRUE) {
2068 if (type != DBUS_TYPE_STRING)
2071 dbus_message_iter_get_basic(&value, &netmask);
2072 } else if (g_str_equal(key, "Gateway") == TRUE) {
2073 if (type != DBUS_TYPE_STRING)
2076 dbus_message_iter_get_basic(&value, &gateway);
2077 } else if (g_str_equal(key, "Privacy") == TRUE) {
2078 if (type != DBUS_TYPE_STRING)
2081 dbus_message_iter_get_basic(&value, &privacy_string);
2082 privacy = string2privacy(privacy_string);
2085 dbus_message_iter_next(&dict);
2088 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2090 method, address, netmask, gateway, prefix_length,
2094 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2095 case CONNMAN_IPCONFIG_METHOD_FIXED:
2098 case CONNMAN_IPCONFIG_METHOD_OFF:
2099 ipconfig->method = method;
2100 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2101 disable_ipv6(ipconfig);
2104 case CONNMAN_IPCONFIG_METHOD_AUTO:
2105 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2108 ipconfig->method = method;
2109 if (privacy_string != NULL)
2110 ipconfig->ipv6_privacy_config = privacy;
2111 enable_ipv6(ipconfig);
2114 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2115 if (address == NULL)
2118 ipconfig->method = method;
2120 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2121 connman_ipaddress_set_ipv4(ipconfig->address,
2122 address, netmask, gateway);
2124 return connman_ipaddress_set_ipv6(
2125 ipconfig->address, address,
2126 prefix_length, gateway);
2129 case CONNMAN_IPCONFIG_METHOD_DHCP:
2130 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2133 ipconfig->method = method;
2140 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2141 DBusMessageIter *iter)
2143 struct connman_ipdevice *ipdevice;
2144 const char *method = "auto";
2146 connman_dbus_dict_append_basic(iter, "Method",
2147 DBUS_TYPE_STRING, &method);
2149 ipdevice = g_hash_table_lookup(ipdevice_hash,
2150 GINT_TO_POINTER(ipconfig->index));
2151 if (ipdevice == NULL)
2154 if (ipdevice->ifname != NULL)
2155 connman_dbus_dict_append_basic(iter, "Interface",
2156 DBUS_TYPE_STRING, &ipdevice->ifname);
2158 if (ipdevice->address != NULL)
2159 connman_dbus_dict_append_basic(iter, "Address",
2160 DBUS_TYPE_STRING, &ipdevice->address);
2162 if (ipdevice->mtu > 0)
2163 connman_dbus_dict_append_basic(iter, "MTU",
2164 DBUS_TYPE_UINT16, &ipdevice->mtu);
2167 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2168 GKeyFile *keyfile, const char *identifier, const char *prefix)
2174 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2176 key = g_strdup_printf("%smethod", prefix);
2177 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2178 if (method == NULL) {
2179 switch (ipconfig->type) {
2180 case CONNMAN_IPCONFIG_TYPE_IPV4:
2181 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2183 case CONNMAN_IPCONFIG_TYPE_IPV6:
2184 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2186 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2187 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2191 ipconfig->method = __connman_ipconfig_string2method(method);
2193 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2194 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2196 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2197 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2198 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2200 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2201 privacy = g_key_file_get_string(keyfile, identifier,
2203 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2212 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2213 ipconfig->address->prefixlen = g_key_file_get_integer(
2214 keyfile, identifier, key, NULL);
2217 key = g_strdup_printf("%slocal_address", prefix);
2218 ipconfig->address->local = g_key_file_get_string(
2219 keyfile, identifier, key, NULL);
2222 key = g_strdup_printf("%speer_address", prefix);
2223 ipconfig->address->peer = g_key_file_get_string(
2224 keyfile, identifier, key, NULL);
2227 key = g_strdup_printf("%sbroadcast_address", prefix);
2228 ipconfig->address->broadcast = g_key_file_get_string(
2229 keyfile, identifier, key, NULL);
2232 key = g_strdup_printf("%sgateway", prefix);
2233 ipconfig->address->gateway = g_key_file_get_string(
2234 keyfile, identifier, key, NULL);
2237 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2238 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2240 g_free(ipconfig->last_dhcp_address);
2241 ipconfig->last_dhcp_address = str;
2248 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2249 GKeyFile *keyfile, const char *identifier, const char *prefix)
2254 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2256 method = __connman_ipconfig_method2string(ipconfig->method);
2258 key = g_strdup_printf("%smethod", prefix);
2259 g_key_file_set_string(keyfile, identifier, key, method);
2262 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2263 const char *privacy;
2264 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2265 key = g_strdup_printf("%sprivacy", prefix);
2266 g_key_file_set_string(keyfile, identifier, key, privacy);
2270 switch (ipconfig->method) {
2271 case CONNMAN_IPCONFIG_METHOD_FIXED:
2272 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2274 case CONNMAN_IPCONFIG_METHOD_DHCP:
2275 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2276 if (ipconfig->last_dhcp_address != NULL &&
2277 strlen(ipconfig->last_dhcp_address) > 0)
2278 g_key_file_set_string(keyfile, identifier, key,
2279 ipconfig->last_dhcp_address);
2281 g_key_file_remove_key(keyfile, identifier, key, NULL);
2284 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2285 case CONNMAN_IPCONFIG_METHOD_OFF:
2286 case CONNMAN_IPCONFIG_METHOD_AUTO:
2290 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2291 g_key_file_set_integer(keyfile, identifier,
2292 key, ipconfig->address->prefixlen);
2295 key = g_strdup_printf("%slocal_address", prefix);
2296 if (ipconfig->address->local != NULL)
2297 g_key_file_set_string(keyfile, identifier,
2298 key, ipconfig->address->local);
2301 key = g_strdup_printf("%speer_address", prefix);
2302 if (ipconfig->address->peer != NULL)
2303 g_key_file_set_string(keyfile, identifier,
2304 key, ipconfig->address->peer);
2307 key = g_strdup_printf("%sbroadcast_address", prefix);
2308 if (ipconfig->address->broadcast != NULL)
2309 g_key_file_set_string(keyfile, identifier,
2310 key, ipconfig->address->broadcast);
2313 key = g_strdup_printf("%sgateway", prefix);
2314 if (ipconfig->address->gateway != NULL)
2315 g_key_file_set_string(keyfile, identifier,
2316 key, ipconfig->address->gateway);
2322 int __connman_ipconfig_init(void)
2326 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2327 NULL, free_ipdevice);
2332 void __connman_ipconfig_cleanup(void)
2336 g_hash_table_destroy(ipdevice_hash);
2337 ipdevice_hash = NULL;