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)) {
905 enum connman_ipconfig_type type;
907 if (family == AF_INET6) {
908 type = CONNMAN_IPCONFIG_TYPE_IPV6;
909 g_free(ipdevice->ipv6_gateway);
910 ipdevice->ipv6_gateway = g_strdup(gateway);
912 if (ipdevice->config_ipv6 != NULL &&
913 ipdevice->config_ipv6->system != NULL) {
914 g_free(ipdevice->config_ipv6->system->gateway);
915 ipdevice->config_ipv6->system->gateway =
918 } else if (family == AF_INET) {
919 type = CONNMAN_IPCONFIG_TYPE_IPV4;
920 g_free(ipdevice->ipv4_gateway);
921 ipdevice->ipv4_gateway = g_strdup(gateway);
923 if (ipdevice->config_ipv4 != NULL &&
924 ipdevice->config_ipv4->system != NULL) {
925 g_free(ipdevice->config_ipv4->system->gateway);
926 ipdevice->config_ipv4->system->gateway =
932 for (list = ipdevice->address_list; list; list = list->next) {
933 struct connman_ipaddress *ipaddress = list->data;
935 g_free(ipaddress->gateway);
936 ipaddress->gateway = g_strdup(gateway);
939 for (config_list = g_list_first(ipconfig_list); config_list;
940 config_list = g_list_next(config_list)) {
941 struct connman_ipconfig *ipconfig = config_list->data;
943 if (index != ipconfig->index)
946 if (type != ipconfig->type)
949 if (ipconfig->ops == NULL)
952 if (ipconfig->ops->ip_bound)
953 ipconfig->ops->ip_bound(ipconfig);
957 connman_info("%s {add} route %s gw %s scope %u <%s>",
958 ipdevice->ifname, dst, gateway,
959 scope, scope2str(scope));
962 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
963 const char *dst, const char *gateway)
965 struct connman_ipdevice *ipdevice;
967 DBG("index %d", index);
969 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
970 if (ipdevice == NULL)
973 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
974 g_strcmp0(dst, "::") == 0)) {
977 enum connman_ipconfig_type type;
979 if (family == AF_INET6) {
980 type = CONNMAN_IPCONFIG_TYPE_IPV6;
981 g_free(ipdevice->ipv6_gateway);
982 ipdevice->ipv6_gateway = NULL;
984 if (ipdevice->config_ipv6 != NULL &&
985 ipdevice->config_ipv6->system != NULL) {
986 g_free(ipdevice->config_ipv6->system->gateway);
987 ipdevice->config_ipv6->system->gateway = NULL;
989 } else if (family == AF_INET) {
990 type = CONNMAN_IPCONFIG_TYPE_IPV4;
991 g_free(ipdevice->ipv4_gateway);
992 ipdevice->ipv4_gateway = NULL;
994 if (ipdevice->config_ipv4 != NULL &&
995 ipdevice->config_ipv4->system != NULL) {
996 g_free(ipdevice->config_ipv4->system->gateway);
997 ipdevice->config_ipv4->system->gateway = NULL;
1002 for (list = ipdevice->address_list; list; list = list->next) {
1003 struct connman_ipaddress *ipaddress = list->data;
1005 g_free(ipaddress->gateway);
1006 ipaddress->gateway = NULL;
1009 for (config_list = g_list_first(ipconfig_list); config_list;
1010 config_list = g_list_next(config_list)) {
1011 struct connman_ipconfig *ipconfig = config_list->data;
1013 if (index != ipconfig->index)
1016 if (type != ipconfig->type)
1019 if (ipconfig->ops == NULL)
1022 if (ipconfig->ops->ip_release)
1023 ipconfig->ops->ip_release(ipconfig);
1027 connman_info("%s {del} route %s gw %s scope %u <%s>",
1028 ipdevice->ifname, dst, gateway,
1029 scope, scope2str(scope));
1032 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1037 keys = g_hash_table_get_keys(ipdevice_hash);
1041 for (list = g_list_first(keys); list; list = g_list_next(list)) {
1042 int index = GPOINTER_TO_INT(list->data);
1044 function(index, user_data);
1050 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1051 struct connman_ipconfig *ipconfig)
1053 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1056 unsigned short __connman_ipconfig_get_type_from_index(int index)
1058 struct connman_ipdevice *ipdevice;
1060 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1061 if (ipdevice == NULL)
1064 return ipdevice->type;
1067 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1069 struct connman_ipdevice *ipdevice;
1071 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1072 if (ipdevice == NULL)
1075 return ipdevice->flags;
1078 const char *__connman_ipconfig_get_gateway_from_index(int index)
1080 struct connman_ipdevice *ipdevice;
1082 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1083 if (ipdevice == NULL)
1086 if (ipdevice->ipv4_gateway != NULL)
1087 return ipdevice->ipv4_gateway;
1089 if (ipdevice->config_ipv4 != NULL &&
1090 ipdevice->config_ipv4->address != NULL)
1091 return ipdevice->config_ipv4->address->gateway;
1093 if (ipdevice->ipv6_gateway != NULL)
1094 return ipdevice->ipv6_gateway;
1096 if (ipdevice->config_ipv6 != NULL &&
1097 ipdevice->config_ipv6->address != NULL)
1098 return ipdevice->config_ipv6->address->gateway;
1103 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1105 ipconfig->index = index;
1108 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1110 if (ipconfig->address == NULL)
1113 return ipconfig->address->local;
1116 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1118 if (ipconfig->address == NULL)
1121 g_free(ipconfig->address->local);
1122 ipconfig->address->local = g_strdup(address);
1125 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1127 if (ipconfig->address == NULL)
1130 return ipconfig->address->peer;
1133 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1135 if (ipconfig->address == NULL)
1138 g_free(ipconfig->address->peer);
1139 ipconfig->address->peer = g_strdup(address);
1142 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1144 if (ipconfig->address == NULL)
1147 return ipconfig->address->broadcast;
1150 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1152 if (ipconfig->address == NULL)
1155 g_free(ipconfig->address->broadcast);
1156 ipconfig->address->broadcast = g_strdup(broadcast);
1159 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1161 if (ipconfig->address == NULL)
1164 return ipconfig->address->gateway;
1167 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1171 if (ipconfig->address == NULL)
1173 g_free(ipconfig->address->gateway);
1174 ipconfig->address->gateway = g_strdup(gateway);
1177 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1179 struct connman_service *service;
1183 if (ipconfig->address == NULL)
1186 service = __connman_service_lookup_from_index(ipconfig->index);
1187 if (service == NULL)
1190 __connman_connection_gateway_remove(service, ipconfig->type);
1192 DBG("type %d gw %s peer %s", ipconfig->type,
1193 ipconfig->address->gateway, ipconfig->address->peer);
1195 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1196 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1197 return __connman_connection_gateway_add(service,
1198 ipconfig->address->gateway,
1200 ipconfig->address->peer);
1205 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1207 struct connman_service *service;
1211 service = __connman_service_lookup_from_index(ipconfig->index);
1212 if (service != NULL)
1213 __connman_connection_gateway_remove(service, ipconfig->type);
1216 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1218 if (ipconfig->address == NULL)
1221 return ipconfig->address->prefixlen;
1224 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1226 if (ipconfig->address == NULL)
1229 ipconfig->address->prefixlen = prefixlen;
1232 static struct connman_ipconfig *create_ipv6config(int index)
1234 struct connman_ipconfig *ipv6config;
1236 DBG("index %d", index);
1238 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1239 if (ipv6config == NULL)
1242 ipv6config->refcount = 1;
1244 ipv6config->index = index;
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->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1288 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1289 if (ipconfig->address == NULL) {
1294 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1296 DBG("ipconfig %p", ipconfig);
1303 * connman_ipconfig_ref:
1304 * @ipconfig: ipconfig structure
1306 * Increase reference counter of ipconfig
1308 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1310 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1312 __sync_fetch_and_add(&ipconfig->refcount, 1);
1318 * connman_ipconfig_unref:
1319 * @ipconfig: ipconfig structure
1321 * Decrease reference counter of ipconfig
1323 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1325 if (ipconfig == NULL)
1328 DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1330 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1333 if (__connman_ipconfig_disable(ipconfig) < 0)
1334 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1336 connman_ipconfig_set_ops(ipconfig, NULL);
1338 if (ipconfig->origin != NULL) {
1339 connman_ipconfig_unref(ipconfig->origin);
1340 ipconfig->origin = NULL;
1343 connman_ipaddress_free(ipconfig->system);
1344 connman_ipaddress_free(ipconfig->address);
1345 g_free(ipconfig->last_dhcp_address);
1350 * connman_ipconfig_get_data:
1351 * @ipconfig: ipconfig structure
1353 * Get private data pointer
1355 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1357 if (ipconfig == NULL)
1360 return ipconfig->ops_data;
1364 * connman_ipconfig_set_data:
1365 * @ipconfig: ipconfig structure
1366 * @data: data pointer
1368 * Set private data pointer
1370 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1372 ipconfig->ops_data = data;
1376 * connman_ipconfig_get_index:
1377 * @ipconfig: ipconfig structure
1379 * Get interface index
1381 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1383 if (ipconfig == NULL)
1386 if (ipconfig->origin != NULL)
1387 return ipconfig->origin->index;
1389 return ipconfig->index;
1393 * connman_ipconfig_get_ifname:
1394 * @ipconfig: ipconfig structure
1396 * Get interface name
1398 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1400 struct connman_ipdevice *ipdevice;
1402 if (ipconfig == NULL)
1405 if (ipconfig->index < 0)
1408 ipdevice = g_hash_table_lookup(ipdevice_hash,
1409 GINT_TO_POINTER(ipconfig->index));
1410 if (ipdevice == NULL)
1413 return ipdevice->ifname;
1417 * connman_ipconfig_set_ops:
1418 * @ipconfig: ipconfig structure
1419 * @ops: operation callbacks
1421 * Set the operation callbacks
1423 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1424 const struct connman_ipconfig_ops *ops)
1426 ipconfig->ops = ops;
1430 * connman_ipconfig_set_method:
1431 * @ipconfig: ipconfig structure
1432 * @method: configuration method
1434 * Set the configuration method
1436 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1437 enum connman_ipconfig_method method)
1439 ipconfig->method = method;
1444 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1446 if (ipconfig == NULL)
1447 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1449 return ipconfig->method;
1452 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1456 switch (ipconfig->method) {
1457 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1458 case CONNMAN_IPCONFIG_METHOD_OFF:
1459 case CONNMAN_IPCONFIG_METHOD_AUTO:
1461 case CONNMAN_IPCONFIG_METHOD_FIXED:
1462 case CONNMAN_IPCONFIG_METHOD_DHCP:
1463 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1464 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1465 return connman_inet_set_address(ipconfig->index,
1467 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1468 return connman_inet_set_ipv6_address(
1469 ipconfig->index, ipconfig->address);
1475 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1481 if (ipconfig == NULL)
1484 DBG("method %d", ipconfig->method);
1486 switch (ipconfig->method) {
1487 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1488 case CONNMAN_IPCONFIG_METHOD_OFF:
1489 case CONNMAN_IPCONFIG_METHOD_AUTO:
1491 case CONNMAN_IPCONFIG_METHOD_FIXED:
1492 case CONNMAN_IPCONFIG_METHOD_DHCP:
1493 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1494 err = __connman_ipconfig_address_unset(ipconfig);
1495 connman_ipaddress_clear(ipconfig->address);
1503 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1509 if (ipconfig == NULL)
1512 DBG("method %d", ipconfig->method);
1514 switch (ipconfig->method) {
1515 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1516 case CONNMAN_IPCONFIG_METHOD_OFF:
1517 case CONNMAN_IPCONFIG_METHOD_AUTO:
1519 case CONNMAN_IPCONFIG_METHOD_FIXED:
1520 case CONNMAN_IPCONFIG_METHOD_DHCP:
1521 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1522 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1523 err = connman_inet_clear_address(ipconfig->index,
1525 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1526 err = connman_inet_clear_ipv6_address(
1528 ipconfig->address->local,
1529 ipconfig->address->prefixlen);
1539 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1542 struct connman_ipdevice *ipdevice;
1544 DBG("ipconfig %p", ipconfig);
1546 if (ipconfig == NULL || ipconfig->index < 0)
1549 ipdevice = g_hash_table_lookup(ipdevice_hash,
1550 GINT_TO_POINTER(ipconfig->index));
1551 if (ipdevice == NULL)
1554 g_free(ipdevice->pac);
1555 ipdevice->pac = g_strdup(url);
1560 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1562 struct connman_ipdevice *ipdevice;
1564 DBG("ipconfig %p", ipconfig);
1566 if (ipconfig == NULL || ipconfig->index < 0)
1569 ipdevice = g_hash_table_lookup(ipdevice_hash,
1570 GINT_TO_POINTER(ipconfig->index));
1571 if (ipdevice == NULL)
1574 return ipdevice->pac;
1577 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1578 const char *address)
1580 if (ipconfig == NULL)
1583 g_free(ipconfig->last_dhcp_address);
1584 ipconfig->last_dhcp_address = g_strdup(address);
1587 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1589 if (ipconfig == NULL)
1592 return ipconfig->last_dhcp_address;
1595 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1597 struct connman_ipdevice *ipdevice;
1601 ipdevice = g_hash_table_lookup(ipdevice_hash,
1602 GINT_TO_POINTER(ipconfig->index));
1603 if (ipdevice == NULL)
1606 set_ipv6_state(ipdevice->ifname, FALSE);
1609 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1611 struct connman_ipdevice *ipdevice;
1615 ipdevice = g_hash_table_lookup(ipdevice_hash,
1616 GINT_TO_POINTER(ipconfig->index));
1617 if (ipdevice == NULL)
1620 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1621 set_ipv6_privacy(ipdevice->ifname,
1622 ipconfig->ipv6_privacy_config);
1624 set_ipv6_state(ipdevice->ifname, TRUE);
1627 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1629 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1632 enable_ipv6(ipconfig);
1635 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1637 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1640 disable_ipv6(ipconfig);
1643 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1645 struct connman_ipdevice *ipdevice;
1646 gboolean up = FALSE, down = FALSE;
1647 gboolean lower_up = FALSE, lower_down = FALSE;
1648 enum connman_ipconfig_type type;
1650 DBG("ipconfig %p", ipconfig);
1652 if (ipconfig == NULL || ipconfig->index < 0)
1655 ipdevice = g_hash_table_lookup(ipdevice_hash,
1656 GINT_TO_POINTER(ipconfig->index));
1657 if (ipdevice == NULL)
1660 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1661 if (ipdevice->config_ipv4 == ipconfig)
1663 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1664 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1665 if (ipdevice->config_ipv6 == ipconfig)
1667 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1668 enable_ipv6(ipconfig);
1672 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1673 ipdevice->config_ipv4 != NULL) {
1674 ipconfig_list = g_list_remove(ipconfig_list,
1675 ipdevice->config_ipv4);
1677 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1679 connman_ipconfig_unref(ipdevice->config_ipv4);
1682 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1683 ipdevice->config_ipv6 != NULL) {
1684 ipconfig_list = g_list_remove(ipconfig_list,
1685 ipdevice->config_ipv6);
1687 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1689 connman_ipconfig_unref(ipdevice->config_ipv6);
1692 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1693 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1694 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1695 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1697 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1699 if (ipdevice->flags & IFF_UP)
1704 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1705 (IFF_RUNNING | IFF_LOWER_UP))
1707 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1710 if (up == TRUE && ipconfig->ops->up)
1711 ipconfig->ops->up(ipconfig);
1712 if (lower_up == TRUE && ipconfig->ops->lower_up)
1713 ipconfig->ops->lower_up(ipconfig);
1715 if (lower_down == TRUE && ipconfig->ops->lower_down)
1716 ipconfig->ops->lower_down(ipconfig);
1717 if (down == TRUE && ipconfig->ops->down)
1718 ipconfig->ops->down(ipconfig);
1723 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1725 struct connman_ipdevice *ipdevice;
1727 DBG("ipconfig %p", ipconfig);
1729 if (ipconfig == NULL || ipconfig->index < 0)
1732 ipdevice = g_hash_table_lookup(ipdevice_hash,
1733 GINT_TO_POINTER(ipconfig->index));
1734 if (ipdevice == NULL)
1737 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1740 if (ipdevice->config_ipv4 == ipconfig) {
1741 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1743 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1744 connman_ipconfig_unref(ipdevice->config_ipv4);
1745 ipdevice->config_ipv4 = NULL;
1749 if (ipdevice->config_ipv6 == ipconfig) {
1750 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1752 if (ipdevice->config_ipv6->method ==
1753 CONNMAN_IPCONFIG_METHOD_AUTO)
1754 disable_ipv6(ipdevice->config_ipv6);
1756 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1757 connman_ipconfig_unref(ipdevice->config_ipv6);
1758 ipdevice->config_ipv6 = NULL;
1765 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1768 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1770 case CONNMAN_IPCONFIG_METHOD_OFF:
1772 case CONNMAN_IPCONFIG_METHOD_FIXED:
1774 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1776 case CONNMAN_IPCONFIG_METHOD_DHCP:
1778 case CONNMAN_IPCONFIG_METHOD_AUTO:
1785 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1787 if (g_strcmp0(method, "off") == 0)
1788 return CONNMAN_IPCONFIG_METHOD_OFF;
1789 else if (g_strcmp0(method, "fixed") == 0)
1790 return CONNMAN_IPCONFIG_METHOD_FIXED;
1791 else if (g_strcmp0(method, "manual") == 0)
1792 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1793 else if (g_strcmp0(method, "dhcp") == 0)
1794 return CONNMAN_IPCONFIG_METHOD_DHCP;
1795 else if (g_strcmp0(method, "auto") == 0)
1796 return CONNMAN_IPCONFIG_METHOD_AUTO;
1798 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1801 static const char *privacy2string(int privacy)
1805 else if (privacy == 1)
1807 else if (privacy > 1)
1813 static int string2privacy(const char *privacy)
1815 if (g_strcmp0(privacy, "disabled") == 0)
1817 else if (g_strcmp0(privacy, "enabled") == 0)
1819 else if (g_strcmp0(privacy, "prefered") == 0)
1825 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1826 DBusMessageIter *iter)
1832 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1835 str = __connman_ipconfig_method2string(ipconfig->method);
1839 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1841 if (ipconfig->system == NULL)
1844 if (ipconfig->system->local != NULL) {
1846 struct in_addr netmask;
1849 connman_dbus_dict_append_basic(iter, "Address",
1850 DBUS_TYPE_STRING, &ipconfig->system->local);
1852 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1853 netmask.s_addr = htonl(addr);
1854 mask = inet_ntoa(netmask);
1855 connman_dbus_dict_append_basic(iter, "Netmask",
1856 DBUS_TYPE_STRING, &mask);
1859 if (ipconfig->system->gateway != NULL)
1860 connman_dbus_dict_append_basic(iter, "Gateway",
1861 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1864 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1865 DBusMessageIter *iter,
1866 struct connman_ipconfig *ipconfig_ipv4)
1868 const char *str, *privacy;
1872 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1875 str = __connman_ipconfig_method2string(ipconfig->method);
1879 if (ipconfig_ipv4 != NULL &&
1880 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1881 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1885 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1887 if (ipconfig->system == NULL)
1890 if (ipconfig->system->local != NULL) {
1891 connman_dbus_dict_append_basic(iter, "Address",
1892 DBUS_TYPE_STRING, &ipconfig->system->local);
1893 connman_dbus_dict_append_basic(iter, "PrefixLength",
1895 &ipconfig->system->prefixlen);
1898 if (ipconfig->system->gateway != NULL)
1899 connman_dbus_dict_append_basic(iter, "Gateway",
1900 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1902 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1903 connman_dbus_dict_append_basic(iter, "Privacy",
1904 DBUS_TYPE_STRING, &privacy);
1907 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1908 DBusMessageIter *iter)
1910 const char *str, *privacy;
1914 str = __connman_ipconfig_method2string(ipconfig->method);
1918 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1920 switch (ipconfig->method) {
1921 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1922 case CONNMAN_IPCONFIG_METHOD_OFF:
1923 case CONNMAN_IPCONFIG_METHOD_DHCP:
1925 case CONNMAN_IPCONFIG_METHOD_FIXED:
1926 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1927 case CONNMAN_IPCONFIG_METHOD_AUTO:
1931 if (ipconfig->address == NULL)
1934 if (ipconfig->address->local != NULL) {
1935 connman_dbus_dict_append_basic(iter, "Address",
1936 DBUS_TYPE_STRING, &ipconfig->address->local);
1937 connman_dbus_dict_append_basic(iter, "PrefixLength",
1939 &ipconfig->address->prefixlen);
1942 if (ipconfig->address->gateway != NULL)
1943 connman_dbus_dict_append_basic(iter, "Gateway",
1944 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1946 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1947 connman_dbus_dict_append_basic(iter, "Privacy",
1948 DBUS_TYPE_STRING, &privacy);
1951 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1952 DBusMessageIter *iter)
1958 str = __connman_ipconfig_method2string(ipconfig->method);
1962 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1964 switch (ipconfig->method) {
1965 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1966 case CONNMAN_IPCONFIG_METHOD_OFF:
1967 case CONNMAN_IPCONFIG_METHOD_DHCP:
1968 case CONNMAN_IPCONFIG_METHOD_AUTO:
1970 case CONNMAN_IPCONFIG_METHOD_FIXED:
1971 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1975 if (ipconfig->address == NULL)
1978 if (ipconfig->address->local != NULL) {
1980 struct in_addr netmask;
1983 connman_dbus_dict_append_basic(iter, "Address",
1984 DBUS_TYPE_STRING, &ipconfig->address->local);
1986 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1987 netmask.s_addr = htonl(addr);
1988 mask = inet_ntoa(netmask);
1989 connman_dbus_dict_append_basic(iter, "Netmask",
1990 DBUS_TYPE_STRING, &mask);
1993 if (ipconfig->address->gateway != NULL)
1994 connman_dbus_dict_append_basic(iter, "Gateway",
1995 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1998 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1999 DBusMessageIter *array)
2001 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2002 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2003 *prefix_length_string = NULL, *privacy_string = NULL;
2004 int prefix_length = 0, privacy = 0;
2005 DBusMessageIter dict;
2007 DBG("ipconfig %p", ipconfig);
2009 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2012 dbus_message_iter_recurse(array, &dict);
2014 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2015 DBusMessageIter entry;
2019 dbus_message_iter_recurse(&dict, &entry);
2021 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2024 dbus_message_iter_get_basic(&entry, &key);
2025 dbus_message_iter_next(&entry);
2027 type = dbus_message_iter_get_arg_type(&entry);
2029 if (g_str_equal(key, "Method") == TRUE) {
2032 if (type != DBUS_TYPE_STRING)
2035 dbus_message_iter_get_basic(&entry, &str);
2036 method = __connman_ipconfig_string2method(str);
2037 } else if (g_str_equal(key, "Address") == TRUE) {
2038 if (type != DBUS_TYPE_STRING)
2041 dbus_message_iter_get_basic(&entry, &address);
2042 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2043 if (type != DBUS_TYPE_STRING)
2046 dbus_message_iter_get_basic(&entry,
2047 &prefix_length_string);
2049 prefix_length = atoi(prefix_length_string);
2050 if (prefix_length < 0 || prefix_length > 128)
2053 } else if (g_str_equal(key, "Netmask") == TRUE) {
2054 if (type != DBUS_TYPE_STRING)
2057 dbus_message_iter_get_basic(&entry, &netmask);
2058 } else if (g_str_equal(key, "Gateway") == TRUE) {
2059 if (type != DBUS_TYPE_STRING)
2062 dbus_message_iter_get_basic(&entry, &gateway);
2063 } else if (g_str_equal(key, "Privacy") == TRUE) {
2064 if (type != DBUS_TYPE_STRING)
2067 dbus_message_iter_get_basic(&entry, &privacy_string);
2068 privacy = string2privacy(privacy_string);
2070 dbus_message_iter_next(&dict);
2073 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2075 method, address, netmask, gateway, prefix_length,
2079 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2080 case CONNMAN_IPCONFIG_METHOD_FIXED:
2083 case CONNMAN_IPCONFIG_METHOD_OFF:
2084 ipconfig->method = method;
2085 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2086 disable_ipv6(ipconfig);
2089 case CONNMAN_IPCONFIG_METHOD_AUTO:
2090 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2093 ipconfig->method = method;
2094 if (privacy_string != NULL)
2095 ipconfig->ipv6_privacy_config = privacy;
2096 enable_ipv6(ipconfig);
2099 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2100 if (address == NULL)
2103 ipconfig->method = method;
2105 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2106 connman_ipaddress_set_ipv4(ipconfig->address,
2107 address, netmask, gateway);
2109 return connman_ipaddress_set_ipv6(
2110 ipconfig->address, address,
2111 prefix_length, gateway);
2114 case CONNMAN_IPCONFIG_METHOD_DHCP:
2115 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2118 ipconfig->method = method;
2125 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2126 DBusMessageIter *iter)
2128 struct connman_ipdevice *ipdevice;
2129 const char *method = "auto";
2131 connman_dbus_dict_append_basic(iter, "Method",
2132 DBUS_TYPE_STRING, &method);
2134 ipdevice = g_hash_table_lookup(ipdevice_hash,
2135 GINT_TO_POINTER(ipconfig->index));
2136 if (ipdevice == NULL)
2139 if (ipdevice->ifname != NULL)
2140 connman_dbus_dict_append_basic(iter, "Interface",
2141 DBUS_TYPE_STRING, &ipdevice->ifname);
2143 if (ipdevice->address != NULL)
2144 connman_dbus_dict_append_basic(iter, "Address",
2145 DBUS_TYPE_STRING, &ipdevice->address);
2147 if (ipdevice->mtu > 0)
2148 connman_dbus_dict_append_basic(iter, "MTU",
2149 DBUS_TYPE_UINT16, &ipdevice->mtu);
2152 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2153 GKeyFile *keyfile, const char *identifier, const char *prefix)
2159 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2161 key = g_strdup_printf("%smethod", prefix);
2162 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2163 if (method == NULL) {
2164 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2165 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2167 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2169 ipconfig->method = __connman_ipconfig_string2method(method);
2171 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2172 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2174 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2175 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2176 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2178 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2179 privacy = g_key_file_get_string(keyfile, identifier,
2181 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2185 __connman_ipconfig_enable(ipconfig);
2186 enable_ipv6(ipconfig);
2193 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2194 ipconfig->address->prefixlen = g_key_file_get_integer(
2195 keyfile, identifier, key, NULL);
2198 key = g_strdup_printf("%slocal_address", prefix);
2199 ipconfig->address->local = g_key_file_get_string(
2200 keyfile, identifier, key, NULL);
2203 key = g_strdup_printf("%speer_address", prefix);
2204 ipconfig->address->peer = g_key_file_get_string(
2205 keyfile, identifier, key, NULL);
2208 key = g_strdup_printf("%sbroadcast_address", prefix);
2209 ipconfig->address->broadcast = g_key_file_get_string(
2210 keyfile, identifier, key, NULL);
2213 key = g_strdup_printf("%sgateway", prefix);
2214 ipconfig->address->gateway = g_key_file_get_string(
2215 keyfile, identifier, key, NULL);
2218 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2219 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2221 g_free(ipconfig->last_dhcp_address);
2222 ipconfig->last_dhcp_address = str;
2229 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2230 GKeyFile *keyfile, const char *identifier, const char *prefix)
2235 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2237 method = __connman_ipconfig_method2string(ipconfig->method);
2239 key = g_strdup_printf("%smethod", prefix);
2240 g_key_file_set_string(keyfile, identifier, key, method);
2243 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2244 const char *privacy;
2245 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2246 key = g_strdup_printf("%sprivacy", prefix);
2247 g_key_file_set_string(keyfile, identifier, key, privacy);
2251 switch (ipconfig->method) {
2252 case CONNMAN_IPCONFIG_METHOD_FIXED:
2253 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2255 case CONNMAN_IPCONFIG_METHOD_DHCP:
2256 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2257 if (ipconfig->last_dhcp_address != NULL &&
2258 strlen(ipconfig->last_dhcp_address) > 0)
2259 g_key_file_set_string(keyfile, identifier, key,
2260 ipconfig->last_dhcp_address);
2262 g_key_file_remove_key(keyfile, identifier, key, NULL);
2265 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2266 case CONNMAN_IPCONFIG_METHOD_OFF:
2267 case CONNMAN_IPCONFIG_METHOD_AUTO:
2271 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2272 g_key_file_set_integer(keyfile, identifier,
2273 key, ipconfig->address->prefixlen);
2276 key = g_strdup_printf("%slocal_address", prefix);
2277 if (ipconfig->address->local != NULL)
2278 g_key_file_set_string(keyfile, identifier,
2279 key, ipconfig->address->local);
2282 key = g_strdup_printf("%speer_address", prefix);
2283 if (ipconfig->address->peer != NULL)
2284 g_key_file_set_string(keyfile, identifier,
2285 key, ipconfig->address->peer);
2288 key = g_strdup_printf("%sbroadcast_address", prefix);
2289 if (ipconfig->address->broadcast != NULL)
2290 g_key_file_set_string(keyfile, identifier,
2291 key, ipconfig->address->broadcast);
2294 key = g_strdup_printf("%sgateway", prefix);
2295 if (ipconfig->address->gateway != NULL)
2296 g_key_file_set_string(keyfile, identifier,
2297 key, ipconfig->address->gateway);
2303 int __connman_ipconfig_init(void)
2307 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2308 NULL, free_ipdevice);
2313 void __connman_ipconfig_cleanup(void)
2317 g_hash_table_destroy(ipdevice_hash);
2318 ipdevice_hash = NULL;