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 static void free_ipdevice(gpointer data)
511 struct connman_ipdevice *ipdevice = data;
513 connman_info("%s {remove} index %d", ipdevice->ifname,
516 if (ipdevice->config_ipv4 != NULL) {
517 connman_ipconfig_unref(ipdevice->config_ipv4);
518 ipdevice->config_ipv4 = NULL;
521 if (ipdevice->config_ipv6 != NULL) {
522 connman_ipconfig_unref(ipdevice->config_ipv6);
523 ipdevice->config_ipv6 = NULL;
526 free_address_list(ipdevice);
527 g_free(ipdevice->ipv4_gateway);
528 g_free(ipdevice->ipv6_gateway);
529 g_free(ipdevice->pac);
531 g_free(ipdevice->address);
533 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
534 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
536 g_free(ipdevice->ifname);
540 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
542 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
543 ipdevice->config_ipv6);
546 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
548 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
549 ipdevice->config_ipv6);
551 if (ipdevice->config_ipv4)
552 connman_inet_clear_address(ipdevice->index,
553 ipdevice->config_ipv4->address);
555 if (ipdevice->config_ipv6)
556 connman_inet_clear_ipv6_address(ipdevice->index,
557 ipdevice->config_ipv6->address->local,
558 ipdevice->config_ipv6->address->prefixlen);
561 static void update_stats(struct connman_ipdevice *ipdevice,
562 struct rtnl_link_stats *stats)
564 struct connman_service *service;
566 if (stats->rx_packets == 0 && stats->tx_packets == 0)
569 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
570 stats->rx_packets, stats->rx_bytes);
571 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
572 stats->tx_packets, stats->tx_bytes);
574 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
577 if (ipdevice->config_ipv4)
578 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
579 else if (ipdevice->config_ipv6)
580 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
587 ipdevice->rx_packets = stats->rx_packets;
588 ipdevice->tx_packets = stats->tx_packets;
589 ipdevice->rx_bytes = stats->rx_bytes;
590 ipdevice->tx_bytes = stats->tx_bytes;
591 ipdevice->rx_errors = stats->rx_errors;
592 ipdevice->tx_errors = stats->tx_errors;
593 ipdevice->rx_dropped = stats->rx_dropped;
594 ipdevice->tx_dropped = stats->tx_dropped;
596 __connman_service_notify(service,
597 ipdevice->rx_packets, ipdevice->tx_packets,
598 ipdevice->rx_bytes, ipdevice->tx_bytes,
599 ipdevice->rx_errors, ipdevice->tx_errors,
600 ipdevice->rx_dropped, ipdevice->tx_dropped);
603 void __connman_ipconfig_newlink(int index, unsigned short type,
604 unsigned int flags, const char *address,
606 struct rtnl_link_stats *stats)
608 struct connman_ipdevice *ipdevice;
611 gboolean up = FALSE, down = FALSE;
612 gboolean lower_up = FALSE, lower_down = FALSE;
614 DBG("index %d", index);
616 if (type == ARPHRD_LOOPBACK)
619 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
620 if (ipdevice != NULL)
623 ipdevice = g_try_new0(struct connman_ipdevice, 1);
624 if (ipdevice == NULL)
627 ipdevice->index = index;
628 ipdevice->ifname = connman_inet_ifname(index);
629 ipdevice->type = type;
631 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
632 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
634 ipdevice->address = g_strdup(address);
636 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
638 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
639 index, type, type2str(type));
644 update_stats(ipdevice, stats);
646 if (flags == ipdevice->flags)
649 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
656 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
657 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
658 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
659 (IFF_RUNNING | IFF_LOWER_UP))
661 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
665 ipdevice->flags = flags;
667 str = g_string_new(NULL);
672 g_string_append(str, "UP");
674 g_string_append(str, "DOWN");
676 if (flags & IFF_RUNNING)
677 g_string_append(str, ",RUNNING");
679 if (flags & IFF_LOWER_UP)
680 g_string_append(str, ",LOWER_UP");
682 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
685 g_string_free(str, TRUE);
687 for (list = g_list_first(ipconfig_list); list;
688 list = g_list_next(list)) {
689 struct connman_ipconfig *ipconfig = list->data;
691 if (index != ipconfig->index)
694 if (ipconfig->ops == NULL)
697 if (up == TRUE && ipconfig->ops->up)
698 ipconfig->ops->up(ipconfig);
699 if (lower_up == TRUE && ipconfig->ops->lower_up)
700 ipconfig->ops->lower_up(ipconfig);
702 if (lower_down == TRUE && ipconfig->ops->lower_down)
703 ipconfig->ops->lower_down(ipconfig);
704 if (down == TRUE && ipconfig->ops->down)
705 ipconfig->ops->down(ipconfig);
709 __connman_ipconfig_lower_up(ipdevice);
711 __connman_ipconfig_lower_down(ipdevice);
714 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
716 struct connman_ipdevice *ipdevice;
719 DBG("index %d", index);
721 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
722 if (ipdevice == NULL)
725 update_stats(ipdevice, stats);
727 for (list = g_list_first(ipconfig_list); list;
728 list = g_list_next(list)) {
729 struct connman_ipconfig *ipconfig = list->data;
731 if (index != ipconfig->index)
734 ipconfig->index = -1;
736 if (ipconfig->ops == NULL)
739 if (ipconfig->ops->lower_down)
740 ipconfig->ops->lower_down(ipconfig);
741 if (ipconfig->ops->down)
742 ipconfig->ops->down(ipconfig);
745 __connman_ipconfig_lower_down(ipdevice);
747 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
750 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
752 const struct connman_ipaddress *addr1 = a;
753 const struct connman_ipaddress *addr2 = b;
755 if (addr1->prefixlen != addr2->prefixlen)
756 return addr2->prefixlen - addr1->prefixlen;
758 return g_strcmp0(addr1->local, addr2->local);
761 void __connman_ipconfig_newaddr(int index, int family, const char *label,
762 unsigned char prefixlen, const char *address)
764 struct connman_ipdevice *ipdevice;
765 struct connman_ipaddress *ipaddress;
766 enum connman_ipconfig_type type;
769 DBG("index %d", index);
771 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
772 if (ipdevice == NULL)
775 ipaddress = connman_ipaddress_alloc(family);
776 if (ipaddress == NULL)
779 ipaddress->prefixlen = prefixlen;
780 ipaddress->local = g_strdup(address);
782 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
783 check_duplicate_address)) {
784 connman_ipaddress_free(ipaddress);
788 if (family == AF_INET)
789 type = CONNMAN_IPCONFIG_TYPE_IPV4;
790 else if (family == AF_INET6)
791 type = CONNMAN_IPCONFIG_TYPE_IPV6;
795 ipdevice->address_list = g_slist_append(ipdevice->address_list,
798 connman_info("%s {add} address %s/%u label %s family %d",
799 ipdevice->ifname, address, prefixlen, label, family);
801 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
802 connman_ipaddress_copy(ipdevice->config_ipv4->system,
805 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
806 connman_ipaddress_copy(ipdevice->config_ipv6->system,
811 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
814 for (list = g_list_first(ipconfig_list); list;
815 list = g_list_next(list)) {
816 struct connman_ipconfig *ipconfig = list->data;
818 if (index != ipconfig->index)
821 if (type != ipconfig->type)
824 if (ipconfig->ops == NULL)
827 if (ipconfig->ops->ip_bound)
828 ipconfig->ops->ip_bound(ipconfig);
832 void __connman_ipconfig_deladdr(int index, int family, const char *label,
833 unsigned char prefixlen, const char *address)
835 struct connman_ipdevice *ipdevice;
836 struct connman_ipaddress *ipaddress;
837 enum connman_ipconfig_type type;
840 DBG("index %d", index);
842 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
843 if (ipdevice == NULL)
846 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
847 if (ipaddress == NULL)
850 if (family == AF_INET)
851 type = CONNMAN_IPCONFIG_TYPE_IPV4;
852 else if (family == AF_INET6)
853 type = CONNMAN_IPCONFIG_TYPE_IPV6;
857 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
860 connman_ipaddress_clear(ipaddress);
863 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
864 address, prefixlen, label);
866 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
869 if (g_slist_length(ipdevice->address_list) > 0)
872 for (list = g_list_first(ipconfig_list); list;
873 list = g_list_next(list)) {
874 struct connman_ipconfig *ipconfig = list->data;
876 if (index != ipconfig->index)
879 if (type != ipconfig->type)
882 if (ipconfig->ops == NULL)
885 if (ipconfig->ops->ip_release)
886 ipconfig->ops->ip_release(ipconfig);
890 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
891 const char *dst, const char *gateway)
893 struct connman_ipdevice *ipdevice;
895 DBG("index %d", index);
897 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
898 if (ipdevice == NULL)
901 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
902 g_strcmp0(dst, "::") == 0)) {
904 enum connman_ipconfig_type type;
906 if (family == AF_INET6) {
907 type = CONNMAN_IPCONFIG_TYPE_IPV6;
908 g_free(ipdevice->ipv6_gateway);
909 ipdevice->ipv6_gateway = g_strdup(gateway);
911 if (ipdevice->config_ipv6 != NULL &&
912 ipdevice->config_ipv6->system != NULL) {
913 g_free(ipdevice->config_ipv6->system->gateway);
914 ipdevice->config_ipv6->system->gateway =
917 } else if (family == AF_INET) {
918 type = CONNMAN_IPCONFIG_TYPE_IPV4;
919 g_free(ipdevice->ipv4_gateway);
920 ipdevice->ipv4_gateway = g_strdup(gateway);
922 if (ipdevice->config_ipv4 != NULL &&
923 ipdevice->config_ipv4->system != NULL) {
924 g_free(ipdevice->config_ipv4->system->gateway);
925 ipdevice->config_ipv4->system->gateway =
931 for (config_list = g_list_first(ipconfig_list); config_list;
932 config_list = g_list_next(config_list)) {
933 struct connman_ipconfig *ipconfig = config_list->data;
935 if (index != ipconfig->index)
938 if (type != ipconfig->type)
941 if (ipconfig->ops == NULL)
944 if (ipconfig->ops->ip_bound)
945 ipconfig->ops->ip_bound(ipconfig);
949 connman_info("%s {add} route %s gw %s scope %u <%s>",
950 ipdevice->ifname, dst, gateway,
951 scope, scope2str(scope));
954 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
955 const char *dst, const char *gateway)
957 struct connman_ipdevice *ipdevice;
959 DBG("index %d", index);
961 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
962 if (ipdevice == NULL)
965 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
966 g_strcmp0(dst, "::") == 0)) {
968 enum connman_ipconfig_type type;
970 if (family == AF_INET6) {
971 type = CONNMAN_IPCONFIG_TYPE_IPV6;
972 g_free(ipdevice->ipv6_gateway);
973 ipdevice->ipv6_gateway = NULL;
975 if (ipdevice->config_ipv6 != NULL &&
976 ipdevice->config_ipv6->system != NULL) {
977 g_free(ipdevice->config_ipv6->system->gateway);
978 ipdevice->config_ipv6->system->gateway = NULL;
980 } else if (family == AF_INET) {
981 type = CONNMAN_IPCONFIG_TYPE_IPV4;
982 g_free(ipdevice->ipv4_gateway);
983 ipdevice->ipv4_gateway = NULL;
985 if (ipdevice->config_ipv4 != NULL &&
986 ipdevice->config_ipv4->system != NULL) {
987 g_free(ipdevice->config_ipv4->system->gateway);
988 ipdevice->config_ipv4->system->gateway = NULL;
993 for (config_list = g_list_first(ipconfig_list); config_list;
994 config_list = g_list_next(config_list)) {
995 struct connman_ipconfig *ipconfig = config_list->data;
997 if (index != ipconfig->index)
1000 if (type != ipconfig->type)
1003 if (ipconfig->ops == NULL)
1006 if (ipconfig->ops->ip_release)
1007 ipconfig->ops->ip_release(ipconfig);
1011 connman_info("%s {del} route %s gw %s scope %u <%s>",
1012 ipdevice->ifname, dst, gateway,
1013 scope, scope2str(scope));
1016 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1021 keys = g_hash_table_get_keys(ipdevice_hash);
1025 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1026 int index = GPOINTER_TO_INT(list->data);
1028 function(index, user_data);
1034 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1035 struct connman_ipconfig *ipconfig)
1037 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1040 unsigned short __connman_ipconfig_get_type_from_index(int index)
1042 struct connman_ipdevice *ipdevice;
1044 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1045 if (ipdevice == NULL)
1048 return ipdevice->type;
1051 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1053 struct connman_ipdevice *ipdevice;
1055 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1056 if (ipdevice == NULL)
1059 return ipdevice->flags;
1062 const char *__connman_ipconfig_get_gateway_from_index(int index)
1064 struct connman_ipdevice *ipdevice;
1066 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1067 if (ipdevice == NULL)
1070 if (ipdevice->ipv4_gateway != NULL)
1071 return ipdevice->ipv4_gateway;
1073 if (ipdevice->config_ipv4 != NULL &&
1074 ipdevice->config_ipv4->address != NULL)
1075 return ipdevice->config_ipv4->address->gateway;
1077 if (ipdevice->ipv6_gateway != NULL)
1078 return ipdevice->ipv6_gateway;
1080 if (ipdevice->config_ipv6 != NULL &&
1081 ipdevice->config_ipv6->address != NULL)
1082 return ipdevice->config_ipv6->address->gateway;
1087 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1089 ipconfig->index = index;
1092 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1094 if (ipconfig->address == NULL)
1097 return ipconfig->address->local;
1100 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1102 if (ipconfig->address == NULL)
1105 g_free(ipconfig->address->local);
1106 ipconfig->address->local = g_strdup(address);
1109 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1111 if (ipconfig->address == NULL)
1114 return ipconfig->address->peer;
1117 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1119 if (ipconfig->address == NULL)
1122 g_free(ipconfig->address->peer);
1123 ipconfig->address->peer = g_strdup(address);
1126 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1128 if (ipconfig->address == NULL)
1131 return ipconfig->address->broadcast;
1134 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1136 if (ipconfig->address == NULL)
1139 g_free(ipconfig->address->broadcast);
1140 ipconfig->address->broadcast = g_strdup(broadcast);
1143 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1145 if (ipconfig->address == NULL)
1148 return ipconfig->address->gateway;
1151 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1155 if (ipconfig->address == NULL)
1157 g_free(ipconfig->address->gateway);
1158 ipconfig->address->gateway = g_strdup(gateway);
1161 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1163 struct connman_service *service;
1167 if (ipconfig->address == NULL)
1170 service = __connman_service_lookup_from_index(ipconfig->index);
1171 if (service == NULL)
1174 __connman_connection_gateway_remove(service, ipconfig->type);
1176 DBG("type %d gw %s peer %s", ipconfig->type,
1177 ipconfig->address->gateway, ipconfig->address->peer);
1179 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1180 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1181 return __connman_connection_gateway_add(service,
1182 ipconfig->address->gateway,
1184 ipconfig->address->peer);
1189 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1191 struct connman_service *service;
1195 service = __connman_service_lookup_from_index(ipconfig->index);
1196 if (service != NULL)
1197 __connman_connection_gateway_remove(service, ipconfig->type);
1200 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1202 if (ipconfig->address == NULL)
1205 return ipconfig->address->prefixlen;
1208 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1210 if (ipconfig->address == NULL)
1213 ipconfig->address->prefixlen = prefixlen;
1216 static struct connman_ipconfig *create_ipv6config(int index)
1218 struct connman_ipconfig *ipv6config;
1220 DBG("index %d", index);
1222 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1223 if (ipv6config == NULL)
1226 ipv6config->refcount = 1;
1228 ipv6config->index = index;
1229 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1230 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1231 ipv6config->ipv6_privacy_config = 0;
1233 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1234 if (ipv6config->address == NULL) {
1239 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1241 DBG("ipconfig %p", ipv6config);
1247 * connman_ipconfig_create:
1249 * Allocate a new ipconfig structure.
1251 * Returns: a newly-allocated #connman_ipconfig structure
1253 struct connman_ipconfig *connman_ipconfig_create(int index,
1254 enum connman_ipconfig_type type)
1256 struct connman_ipconfig *ipconfig;
1258 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1259 return create_ipv6config(index);
1261 DBG("index %d", index);
1263 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1264 if (ipconfig == NULL)
1267 ipconfig->refcount = 1;
1269 ipconfig->index = index;
1270 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1272 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1273 if (ipconfig->address == NULL) {
1278 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1280 DBG("ipconfig %p", ipconfig);
1287 * connman_ipconfig_ref:
1288 * @ipconfig: ipconfig structure
1290 * Increase reference counter of ipconfig
1292 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1294 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1296 __sync_fetch_and_add(&ipconfig->refcount, 1);
1302 * connman_ipconfig_unref:
1303 * @ipconfig: ipconfig structure
1305 * Decrease reference counter of ipconfig
1307 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1309 if (ipconfig == NULL)
1312 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1314 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1317 if (__connman_ipconfig_disable(ipconfig) < 0)
1318 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1320 connman_ipconfig_set_ops(ipconfig, NULL);
1322 if (ipconfig->origin != NULL) {
1323 connman_ipconfig_unref(ipconfig->origin);
1324 ipconfig->origin = NULL;
1327 connman_ipaddress_free(ipconfig->system);
1328 connman_ipaddress_free(ipconfig->address);
1329 g_free(ipconfig->last_dhcp_address);
1334 * connman_ipconfig_get_data:
1335 * @ipconfig: ipconfig structure
1337 * Get private data pointer
1339 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1341 if (ipconfig == NULL)
1344 return ipconfig->ops_data;
1348 * connman_ipconfig_set_data:
1349 * @ipconfig: ipconfig structure
1350 * @data: data pointer
1352 * Set private data pointer
1354 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1356 ipconfig->ops_data = data;
1360 * connman_ipconfig_get_index:
1361 * @ipconfig: ipconfig structure
1363 * Get interface index
1365 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1367 if (ipconfig == NULL)
1370 if (ipconfig->origin != NULL)
1371 return ipconfig->origin->index;
1373 return ipconfig->index;
1377 * connman_ipconfig_get_ifname:
1378 * @ipconfig: ipconfig structure
1380 * Get interface name
1382 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1384 struct connman_ipdevice *ipdevice;
1386 if (ipconfig == NULL)
1389 if (ipconfig->index < 0)
1392 ipdevice = g_hash_table_lookup(ipdevice_hash,
1393 GINT_TO_POINTER(ipconfig->index));
1394 if (ipdevice == NULL)
1397 return ipdevice->ifname;
1401 * connman_ipconfig_set_ops:
1402 * @ipconfig: ipconfig structure
1403 * @ops: operation callbacks
1405 * Set the operation callbacks
1407 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1408 const struct connman_ipconfig_ops *ops)
1410 ipconfig->ops = ops;
1414 * connman_ipconfig_set_method:
1415 * @ipconfig: ipconfig structure
1416 * @method: configuration method
1418 * Set the configuration method
1420 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1421 enum connman_ipconfig_method method)
1423 ipconfig->method = method;
1428 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1430 if (ipconfig == NULL)
1431 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1433 return ipconfig->method;
1436 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1440 switch (ipconfig->method) {
1441 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1442 case CONNMAN_IPCONFIG_METHOD_OFF:
1443 case CONNMAN_IPCONFIG_METHOD_AUTO:
1445 case CONNMAN_IPCONFIG_METHOD_FIXED:
1446 case CONNMAN_IPCONFIG_METHOD_DHCP:
1447 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1448 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1449 return connman_inet_set_address(ipconfig->index,
1451 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1452 return connman_inet_set_ipv6_address(
1453 ipconfig->index, ipconfig->address);
1459 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1465 if (ipconfig == NULL)
1468 DBG("method %d", ipconfig->method);
1470 switch (ipconfig->method) {
1471 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1472 case CONNMAN_IPCONFIG_METHOD_OFF:
1473 case CONNMAN_IPCONFIG_METHOD_AUTO:
1475 case CONNMAN_IPCONFIG_METHOD_FIXED:
1476 case CONNMAN_IPCONFIG_METHOD_DHCP:
1477 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1478 err = __connman_ipconfig_address_unset(ipconfig);
1479 connman_ipaddress_clear(ipconfig->address);
1487 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1493 if (ipconfig == NULL)
1496 DBG("method %d", ipconfig->method);
1498 switch (ipconfig->method) {
1499 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1500 case CONNMAN_IPCONFIG_METHOD_OFF:
1501 case CONNMAN_IPCONFIG_METHOD_AUTO:
1503 case CONNMAN_IPCONFIG_METHOD_FIXED:
1504 case CONNMAN_IPCONFIG_METHOD_DHCP:
1505 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1506 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1507 err = connman_inet_clear_address(ipconfig->index,
1509 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1510 err = connman_inet_clear_ipv6_address(
1512 ipconfig->address->local,
1513 ipconfig->address->prefixlen);
1523 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1526 struct connman_ipdevice *ipdevice;
1528 DBG("ipconfig %p", ipconfig);
1530 if (ipconfig == NULL || ipconfig->index < 0)
1533 ipdevice = g_hash_table_lookup(ipdevice_hash,
1534 GINT_TO_POINTER(ipconfig->index));
1535 if (ipdevice == NULL)
1538 g_free(ipdevice->pac);
1539 ipdevice->pac = g_strdup(url);
1544 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1546 struct connman_ipdevice *ipdevice;
1548 DBG("ipconfig %p", ipconfig);
1550 if (ipconfig == NULL || ipconfig->index < 0)
1553 ipdevice = g_hash_table_lookup(ipdevice_hash,
1554 GINT_TO_POINTER(ipconfig->index));
1555 if (ipdevice == NULL)
1558 return ipdevice->pac;
1561 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1562 const char *address)
1564 if (ipconfig == NULL)
1567 g_free(ipconfig->last_dhcp_address);
1568 ipconfig->last_dhcp_address = g_strdup(address);
1571 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1573 if (ipconfig == NULL)
1576 return ipconfig->last_dhcp_address;
1579 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1581 struct connman_ipdevice *ipdevice;
1585 ipdevice = g_hash_table_lookup(ipdevice_hash,
1586 GINT_TO_POINTER(ipconfig->index));
1587 if (ipdevice == NULL)
1590 set_ipv6_state(ipdevice->ifname, FALSE);
1593 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1595 struct connman_ipdevice *ipdevice;
1599 ipdevice = g_hash_table_lookup(ipdevice_hash,
1600 GINT_TO_POINTER(ipconfig->index));
1601 if (ipdevice == NULL)
1604 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1605 set_ipv6_privacy(ipdevice->ifname,
1606 ipconfig->ipv6_privacy_config);
1608 set_ipv6_state(ipdevice->ifname, TRUE);
1611 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1613 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1616 enable_ipv6(ipconfig);
1619 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1621 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1624 disable_ipv6(ipconfig);
1627 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1629 struct connman_ipdevice *ipdevice;
1630 gboolean up = FALSE, down = FALSE;
1631 gboolean lower_up = FALSE, lower_down = FALSE;
1632 enum connman_ipconfig_type type;
1634 DBG("ipconfig %p", ipconfig);
1636 if (ipconfig == NULL || ipconfig->index < 0)
1639 ipdevice = g_hash_table_lookup(ipdevice_hash,
1640 GINT_TO_POINTER(ipconfig->index));
1641 if (ipdevice == NULL)
1644 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1645 if (ipdevice->config_ipv4 == ipconfig)
1647 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1648 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1649 if (ipdevice->config_ipv6 == ipconfig)
1651 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1652 enable_ipv6(ipconfig);
1656 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1657 ipdevice->config_ipv4 != NULL) {
1658 ipconfig_list = g_list_remove(ipconfig_list,
1659 ipdevice->config_ipv4);
1661 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1663 connman_ipconfig_unref(ipdevice->config_ipv4);
1666 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1667 ipdevice->config_ipv6 != NULL) {
1668 ipconfig_list = g_list_remove(ipconfig_list,
1669 ipdevice->config_ipv6);
1671 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1673 connman_ipconfig_unref(ipdevice->config_ipv6);
1676 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1677 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1678 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1679 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1681 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1683 if (ipdevice->flags & IFF_UP)
1688 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1689 (IFF_RUNNING | IFF_LOWER_UP))
1691 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1694 if (up == TRUE && ipconfig->ops->up)
1695 ipconfig->ops->up(ipconfig);
1696 if (lower_up == TRUE && ipconfig->ops->lower_up)
1697 ipconfig->ops->lower_up(ipconfig);
1699 if (lower_down == TRUE && ipconfig->ops->lower_down)
1700 ipconfig->ops->lower_down(ipconfig);
1701 if (down == TRUE && ipconfig->ops->down)
1702 ipconfig->ops->down(ipconfig);
1707 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1709 struct connman_ipdevice *ipdevice;
1711 DBG("ipconfig %p", ipconfig);
1713 if (ipconfig == NULL || ipconfig->index < 0)
1716 ipdevice = g_hash_table_lookup(ipdevice_hash,
1717 GINT_TO_POINTER(ipconfig->index));
1718 if (ipdevice == NULL)
1721 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1724 if (ipdevice->config_ipv4 == ipconfig) {
1725 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1727 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1728 connman_ipconfig_unref(ipdevice->config_ipv4);
1729 ipdevice->config_ipv4 = NULL;
1733 if (ipdevice->config_ipv6 == ipconfig) {
1734 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1736 if (ipdevice->config_ipv6->method ==
1737 CONNMAN_IPCONFIG_METHOD_AUTO)
1738 disable_ipv6(ipdevice->config_ipv6);
1740 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1741 connman_ipconfig_unref(ipdevice->config_ipv6);
1742 ipdevice->config_ipv6 = NULL;
1749 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1752 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1754 case CONNMAN_IPCONFIG_METHOD_OFF:
1756 case CONNMAN_IPCONFIG_METHOD_FIXED:
1758 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1760 case CONNMAN_IPCONFIG_METHOD_DHCP:
1762 case CONNMAN_IPCONFIG_METHOD_AUTO:
1769 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1771 if (g_strcmp0(method, "off") == 0)
1772 return CONNMAN_IPCONFIG_METHOD_OFF;
1773 else if (g_strcmp0(method, "fixed") == 0)
1774 return CONNMAN_IPCONFIG_METHOD_FIXED;
1775 else if (g_strcmp0(method, "manual") == 0)
1776 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1777 else if (g_strcmp0(method, "dhcp") == 0)
1778 return CONNMAN_IPCONFIG_METHOD_DHCP;
1779 else if (g_strcmp0(method, "auto") == 0)
1780 return CONNMAN_IPCONFIG_METHOD_AUTO;
1782 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1785 static const char *privacy2string(int privacy)
1789 else if (privacy == 1)
1791 else if (privacy > 1)
1797 static int string2privacy(const char *privacy)
1799 if (g_strcmp0(privacy, "disabled") == 0)
1801 else if (g_strcmp0(privacy, "enabled") == 0)
1803 else if (g_strcmp0(privacy, "prefered") == 0)
1809 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1810 DBusMessageIter *iter)
1816 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1819 str = __connman_ipconfig_method2string(ipconfig->method);
1823 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1825 if (ipconfig->system == NULL)
1828 if (ipconfig->system->local != NULL) {
1830 struct in_addr netmask;
1833 connman_dbus_dict_append_basic(iter, "Address",
1834 DBUS_TYPE_STRING, &ipconfig->system->local);
1836 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1837 netmask.s_addr = htonl(addr);
1838 mask = inet_ntoa(netmask);
1839 connman_dbus_dict_append_basic(iter, "Netmask",
1840 DBUS_TYPE_STRING, &mask);
1843 if (ipconfig->system->gateway != NULL)
1844 connman_dbus_dict_append_basic(iter, "Gateway",
1845 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1848 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1849 DBusMessageIter *iter,
1850 struct connman_ipconfig *ipconfig_ipv4)
1852 const char *str, *privacy;
1856 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1859 str = __connman_ipconfig_method2string(ipconfig->method);
1863 if (ipconfig_ipv4 != NULL &&
1864 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1865 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1869 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1871 if (ipconfig->system == NULL)
1874 if (ipconfig->system->local != NULL) {
1875 connman_dbus_dict_append_basic(iter, "Address",
1876 DBUS_TYPE_STRING, &ipconfig->system->local);
1877 connman_dbus_dict_append_basic(iter, "PrefixLength",
1879 &ipconfig->system->prefixlen);
1882 if (ipconfig->system->gateway != NULL)
1883 connman_dbus_dict_append_basic(iter, "Gateway",
1884 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1886 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1887 connman_dbus_dict_append_basic(iter, "Privacy",
1888 DBUS_TYPE_STRING, &privacy);
1891 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1892 DBusMessageIter *iter)
1894 const char *str, *privacy;
1898 str = __connman_ipconfig_method2string(ipconfig->method);
1902 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1904 switch (ipconfig->method) {
1905 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1906 case CONNMAN_IPCONFIG_METHOD_OFF:
1907 case CONNMAN_IPCONFIG_METHOD_DHCP:
1909 case CONNMAN_IPCONFIG_METHOD_FIXED:
1910 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1911 case CONNMAN_IPCONFIG_METHOD_AUTO:
1915 if (ipconfig->address == NULL)
1918 if (ipconfig->address->local != NULL) {
1919 connman_dbus_dict_append_basic(iter, "Address",
1920 DBUS_TYPE_STRING, &ipconfig->address->local);
1921 connman_dbus_dict_append_basic(iter, "PrefixLength",
1923 &ipconfig->address->prefixlen);
1926 if (ipconfig->address->gateway != NULL)
1927 connman_dbus_dict_append_basic(iter, "Gateway",
1928 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1930 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1931 connman_dbus_dict_append_basic(iter, "Privacy",
1932 DBUS_TYPE_STRING, &privacy);
1935 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1936 DBusMessageIter *iter)
1942 str = __connman_ipconfig_method2string(ipconfig->method);
1946 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1948 switch (ipconfig->method) {
1949 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1950 case CONNMAN_IPCONFIG_METHOD_OFF:
1951 case CONNMAN_IPCONFIG_METHOD_DHCP:
1952 case CONNMAN_IPCONFIG_METHOD_AUTO:
1954 case CONNMAN_IPCONFIG_METHOD_FIXED:
1955 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1959 if (ipconfig->address == NULL)
1962 if (ipconfig->address->local != NULL) {
1964 struct in_addr netmask;
1967 connman_dbus_dict_append_basic(iter, "Address",
1968 DBUS_TYPE_STRING, &ipconfig->address->local);
1970 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1971 netmask.s_addr = htonl(addr);
1972 mask = inet_ntoa(netmask);
1973 connman_dbus_dict_append_basic(iter, "Netmask",
1974 DBUS_TYPE_STRING, &mask);
1977 if (ipconfig->address->gateway != NULL)
1978 connman_dbus_dict_append_basic(iter, "Gateway",
1979 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1982 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1983 DBusMessageIter *array)
1985 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1986 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1987 *prefix_length_string = NULL, *privacy_string = NULL;
1988 int prefix_length = 0, privacy = 0;
1989 DBusMessageIter dict;
1991 DBG("ipconfig %p", ipconfig);
1993 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1996 dbus_message_iter_recurse(array, &dict);
1998 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1999 DBusMessageIter entry;
2003 dbus_message_iter_recurse(&dict, &entry);
2005 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2008 dbus_message_iter_get_basic(&entry, &key);
2009 dbus_message_iter_next(&entry);
2011 type = dbus_message_iter_get_arg_type(&entry);
2013 if (g_str_equal(key, "Method") == TRUE) {
2016 if (type != DBUS_TYPE_STRING)
2019 dbus_message_iter_get_basic(&entry, &str);
2020 method = __connman_ipconfig_string2method(str);
2021 } else if (g_str_equal(key, "Address") == TRUE) {
2022 if (type != DBUS_TYPE_STRING)
2025 dbus_message_iter_get_basic(&entry, &address);
2026 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2027 if (type != DBUS_TYPE_STRING)
2030 dbus_message_iter_get_basic(&entry,
2031 &prefix_length_string);
2033 prefix_length = atoi(prefix_length_string);
2034 if (prefix_length < 0 || prefix_length > 128)
2037 } else if (g_str_equal(key, "Netmask") == TRUE) {
2038 if (type != DBUS_TYPE_STRING)
2041 dbus_message_iter_get_basic(&entry, &netmask);
2042 } else if (g_str_equal(key, "Gateway") == TRUE) {
2043 if (type != DBUS_TYPE_STRING)
2046 dbus_message_iter_get_basic(&entry, &gateway);
2047 } else if (g_str_equal(key, "Privacy") == TRUE) {
2048 if (type != DBUS_TYPE_STRING)
2051 dbus_message_iter_get_basic(&entry, &privacy_string);
2052 privacy = string2privacy(privacy_string);
2054 dbus_message_iter_next(&dict);
2057 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2059 method, address, netmask, gateway, prefix_length,
2063 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2064 case CONNMAN_IPCONFIG_METHOD_FIXED:
2067 case CONNMAN_IPCONFIG_METHOD_OFF:
2068 ipconfig->method = method;
2069 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2070 disable_ipv6(ipconfig);
2073 case CONNMAN_IPCONFIG_METHOD_AUTO:
2074 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2077 ipconfig->method = method;
2078 if (privacy_string != NULL)
2079 ipconfig->ipv6_privacy_config = privacy;
2080 enable_ipv6(ipconfig);
2083 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2084 if (address == NULL)
2087 ipconfig->method = method;
2089 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2090 connman_ipaddress_set_ipv4(ipconfig->address,
2091 address, netmask, gateway);
2093 return connman_ipaddress_set_ipv6(
2094 ipconfig->address, address,
2095 prefix_length, gateway);
2098 case CONNMAN_IPCONFIG_METHOD_DHCP:
2099 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2102 ipconfig->method = method;
2109 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2110 DBusMessageIter *iter)
2112 struct connman_ipdevice *ipdevice;
2113 const char *method = "auto";
2115 connman_dbus_dict_append_basic(iter, "Method",
2116 DBUS_TYPE_STRING, &method);
2118 ipdevice = g_hash_table_lookup(ipdevice_hash,
2119 GINT_TO_POINTER(ipconfig->index));
2120 if (ipdevice == NULL)
2123 if (ipdevice->ifname != NULL)
2124 connman_dbus_dict_append_basic(iter, "Interface",
2125 DBUS_TYPE_STRING, &ipdevice->ifname);
2127 if (ipdevice->address != NULL)
2128 connman_dbus_dict_append_basic(iter, "Address",
2129 DBUS_TYPE_STRING, &ipdevice->address);
2131 if (ipdevice->mtu > 0)
2132 connman_dbus_dict_append_basic(iter, "MTU",
2133 DBUS_TYPE_UINT16, &ipdevice->mtu);
2136 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2137 GKeyFile *keyfile, const char *identifier, const char *prefix)
2143 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2145 key = g_strdup_printf("%smethod", prefix);
2146 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2147 if (method == NULL) {
2148 switch (ipconfig->type) {
2149 case CONNMAN_IPCONFIG_TYPE_IPV4:
2150 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2152 case CONNMAN_IPCONFIG_TYPE_IPV6:
2153 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2155 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2156 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2160 ipconfig->method = __connman_ipconfig_string2method(method);
2162 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2163 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2165 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2166 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2167 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2169 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2170 privacy = g_key_file_get_string(keyfile, identifier,
2172 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2176 __connman_ipconfig_enable(ipconfig);
2177 enable_ipv6(ipconfig);
2184 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2185 ipconfig->address->prefixlen = g_key_file_get_integer(
2186 keyfile, identifier, key, NULL);
2189 key = g_strdup_printf("%slocal_address", prefix);
2190 ipconfig->address->local = g_key_file_get_string(
2191 keyfile, identifier, key, NULL);
2194 key = g_strdup_printf("%speer_address", prefix);
2195 ipconfig->address->peer = g_key_file_get_string(
2196 keyfile, identifier, key, NULL);
2199 key = g_strdup_printf("%sbroadcast_address", prefix);
2200 ipconfig->address->broadcast = g_key_file_get_string(
2201 keyfile, identifier, key, NULL);
2204 key = g_strdup_printf("%sgateway", prefix);
2205 ipconfig->address->gateway = g_key_file_get_string(
2206 keyfile, identifier, key, NULL);
2209 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2210 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2212 g_free(ipconfig->last_dhcp_address);
2213 ipconfig->last_dhcp_address = str;
2220 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2221 GKeyFile *keyfile, const char *identifier, const char *prefix)
2226 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2228 method = __connman_ipconfig_method2string(ipconfig->method);
2230 key = g_strdup_printf("%smethod", prefix);
2231 g_key_file_set_string(keyfile, identifier, key, method);
2234 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2235 const char *privacy;
2236 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2237 key = g_strdup_printf("%sprivacy", prefix);
2238 g_key_file_set_string(keyfile, identifier, key, privacy);
2242 switch (ipconfig->method) {
2243 case CONNMAN_IPCONFIG_METHOD_FIXED:
2244 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2246 case CONNMAN_IPCONFIG_METHOD_DHCP:
2247 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2248 if (ipconfig->last_dhcp_address != NULL &&
2249 strlen(ipconfig->last_dhcp_address) > 0)
2250 g_key_file_set_string(keyfile, identifier, key,
2251 ipconfig->last_dhcp_address);
2253 g_key_file_remove_key(keyfile, identifier, key, NULL);
2256 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2257 case CONNMAN_IPCONFIG_METHOD_OFF:
2258 case CONNMAN_IPCONFIG_METHOD_AUTO:
2262 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2263 g_key_file_set_integer(keyfile, identifier,
2264 key, ipconfig->address->prefixlen);
2267 key = g_strdup_printf("%slocal_address", prefix);
2268 if (ipconfig->address->local != NULL)
2269 g_key_file_set_string(keyfile, identifier,
2270 key, ipconfig->address->local);
2273 key = g_strdup_printf("%speer_address", prefix);
2274 if (ipconfig->address->peer != NULL)
2275 g_key_file_set_string(keyfile, identifier,
2276 key, ipconfig->address->peer);
2279 key = g_strdup_printf("%sbroadcast_address", prefix);
2280 if (ipconfig->address->broadcast != NULL)
2281 g_key_file_set_string(keyfile, identifier,
2282 key, ipconfig->address->broadcast);
2285 key = g_strdup_printf("%sgateway", prefix);
2286 if (ipconfig->address->gateway != NULL)
2287 g_key_file_set_string(keyfile, identifier,
2288 key, ipconfig->address->gateway);
2294 int __connman_ipconfig_init(void)
2298 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2299 NULL, free_ipdevice);
2304 void __connman_ipconfig_cleanup(void)
2308 g_hash_table_destroy(ipdevice_hash);
2309 ipdevice_hash = NULL;