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 __connman_ipconfig_disable(ipconfig);
1335 connman_ipconfig_set_ops(ipconfig, NULL);
1337 if (ipconfig->origin != NULL) {
1338 connman_ipconfig_unref(ipconfig->origin);
1339 ipconfig->origin = NULL;
1342 connman_ipaddress_free(ipconfig->system);
1343 connman_ipaddress_free(ipconfig->address);
1344 g_free(ipconfig->last_dhcp_address);
1349 * connman_ipconfig_get_data:
1350 * @ipconfig: ipconfig structure
1352 * Get private data pointer
1354 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1356 if (ipconfig == NULL)
1359 return ipconfig->ops_data;
1363 * connman_ipconfig_set_data:
1364 * @ipconfig: ipconfig structure
1365 * @data: data pointer
1367 * Set private data pointer
1369 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1371 ipconfig->ops_data = data;
1375 * connman_ipconfig_get_index:
1376 * @ipconfig: ipconfig structure
1378 * Get interface index
1380 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1382 if (ipconfig == NULL)
1385 if (ipconfig->origin != NULL)
1386 return ipconfig->origin->index;
1388 return ipconfig->index;
1392 * connman_ipconfig_get_ifname:
1393 * @ipconfig: ipconfig structure
1395 * Get interface name
1397 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1399 struct connman_ipdevice *ipdevice;
1401 if (ipconfig == NULL)
1404 if (ipconfig->index < 0)
1407 ipdevice = g_hash_table_lookup(ipdevice_hash,
1408 GINT_TO_POINTER(ipconfig->index));
1409 if (ipdevice == NULL)
1412 return ipdevice->ifname;
1416 * connman_ipconfig_set_ops:
1417 * @ipconfig: ipconfig structure
1418 * @ops: operation callbacks
1420 * Set the operation callbacks
1422 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1423 const struct connman_ipconfig_ops *ops)
1425 ipconfig->ops = ops;
1429 * connman_ipconfig_set_method:
1430 * @ipconfig: ipconfig structure
1431 * @method: configuration method
1433 * Set the configuration method
1435 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1436 enum connman_ipconfig_method method)
1438 ipconfig->method = method;
1443 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1445 if (ipconfig == NULL)
1446 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1448 return ipconfig->method;
1451 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1455 switch (ipconfig->method) {
1456 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1457 case CONNMAN_IPCONFIG_METHOD_OFF:
1458 case CONNMAN_IPCONFIG_METHOD_AUTO:
1460 case CONNMAN_IPCONFIG_METHOD_FIXED:
1461 case CONNMAN_IPCONFIG_METHOD_DHCP:
1462 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1463 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1464 return connman_inet_set_address(ipconfig->index,
1466 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1467 return connman_inet_set_ipv6_address(
1468 ipconfig->index, ipconfig->address);
1474 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1480 if (ipconfig == NULL)
1483 DBG("method %d", ipconfig->method);
1485 switch (ipconfig->method) {
1486 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1487 case CONNMAN_IPCONFIG_METHOD_OFF:
1488 case CONNMAN_IPCONFIG_METHOD_AUTO:
1490 case CONNMAN_IPCONFIG_METHOD_FIXED:
1491 case CONNMAN_IPCONFIG_METHOD_DHCP:
1492 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1493 err = __connman_ipconfig_address_unset(ipconfig);
1494 connman_ipaddress_clear(ipconfig->address);
1502 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1508 if (ipconfig == NULL)
1511 DBG("method %d", ipconfig->method);
1513 switch (ipconfig->method) {
1514 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1515 case CONNMAN_IPCONFIG_METHOD_OFF:
1516 case CONNMAN_IPCONFIG_METHOD_AUTO:
1518 case CONNMAN_IPCONFIG_METHOD_FIXED:
1519 case CONNMAN_IPCONFIG_METHOD_DHCP:
1520 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1521 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1522 err = connman_inet_clear_address(ipconfig->index,
1524 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1525 err = connman_inet_clear_ipv6_address(
1527 ipconfig->address->local,
1528 ipconfig->address->prefixlen);
1538 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1541 struct connman_ipdevice *ipdevice;
1543 DBG("ipconfig %p", ipconfig);
1545 if (ipconfig == NULL || ipconfig->index < 0)
1548 ipdevice = g_hash_table_lookup(ipdevice_hash,
1549 GINT_TO_POINTER(ipconfig->index));
1550 if (ipdevice == NULL)
1553 g_free(ipdevice->pac);
1554 ipdevice->pac = g_strdup(url);
1559 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1561 struct connman_ipdevice *ipdevice;
1563 DBG("ipconfig %p", ipconfig);
1565 if (ipconfig == NULL || ipconfig->index < 0)
1568 ipdevice = g_hash_table_lookup(ipdevice_hash,
1569 GINT_TO_POINTER(ipconfig->index));
1570 if (ipdevice == NULL)
1573 return ipdevice->pac;
1576 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1577 const char *address)
1579 if (ipconfig == NULL)
1582 g_free(ipconfig->last_dhcp_address);
1583 ipconfig->last_dhcp_address = g_strdup(address);
1586 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1588 if (ipconfig == NULL)
1591 return ipconfig->last_dhcp_address;
1594 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1596 struct connman_ipdevice *ipdevice;
1600 ipdevice = g_hash_table_lookup(ipdevice_hash,
1601 GINT_TO_POINTER(ipconfig->index));
1602 if (ipdevice == NULL)
1605 set_ipv6_state(ipdevice->ifname, FALSE);
1608 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1610 struct connman_ipdevice *ipdevice;
1614 ipdevice = g_hash_table_lookup(ipdevice_hash,
1615 GINT_TO_POINTER(ipconfig->index));
1616 if (ipdevice == NULL)
1619 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1620 set_ipv6_privacy(ipdevice->ifname,
1621 ipconfig->ipv6_privacy_config);
1623 set_ipv6_state(ipdevice->ifname, TRUE);
1626 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1628 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1631 enable_ipv6(ipconfig);
1634 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1636 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1639 disable_ipv6(ipconfig);
1642 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1644 struct connman_ipdevice *ipdevice;
1645 gboolean up = FALSE, down = FALSE;
1646 gboolean lower_up = FALSE, lower_down = FALSE;
1647 enum connman_ipconfig_type type;
1649 DBG("ipconfig %p", ipconfig);
1651 if (ipconfig == NULL || ipconfig->index < 0)
1654 ipdevice = g_hash_table_lookup(ipdevice_hash,
1655 GINT_TO_POINTER(ipconfig->index));
1656 if (ipdevice == NULL)
1659 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1660 if (ipdevice->config_ipv4 == ipconfig)
1662 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1663 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1664 if (ipdevice->config_ipv6 == ipconfig)
1666 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1667 enable_ipv6(ipconfig);
1671 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1672 ipdevice->config_ipv4 != NULL) {
1673 ipconfig_list = g_list_remove(ipconfig_list,
1674 ipdevice->config_ipv4);
1676 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1678 connman_ipconfig_unref(ipdevice->config_ipv4);
1681 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1682 ipdevice->config_ipv6 != NULL) {
1683 ipconfig_list = g_list_remove(ipconfig_list,
1684 ipdevice->config_ipv6);
1686 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1688 connman_ipconfig_unref(ipdevice->config_ipv6);
1691 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1692 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1693 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1694 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1696 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1698 if (ipdevice->flags & IFF_UP)
1703 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1704 (IFF_RUNNING | IFF_LOWER_UP))
1706 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1709 if (up == TRUE && ipconfig->ops->up)
1710 ipconfig->ops->up(ipconfig);
1711 if (lower_up == TRUE && ipconfig->ops->lower_up)
1712 ipconfig->ops->lower_up(ipconfig);
1714 if (lower_down == TRUE && ipconfig->ops->lower_down)
1715 ipconfig->ops->lower_down(ipconfig);
1716 if (down == TRUE && ipconfig->ops->down)
1717 ipconfig->ops->down(ipconfig);
1722 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1724 struct connman_ipdevice *ipdevice;
1726 DBG("ipconfig %p", ipconfig);
1728 if (ipconfig == NULL || ipconfig->index < 0)
1731 ipdevice = g_hash_table_lookup(ipdevice_hash,
1732 GINT_TO_POINTER(ipconfig->index));
1733 if (ipdevice == NULL)
1736 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1739 if (ipdevice->config_ipv4 == ipconfig) {
1740 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1742 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1743 connman_ipconfig_unref(ipdevice->config_ipv4);
1744 ipdevice->config_ipv4 = NULL;
1748 if (ipdevice->config_ipv6 == ipconfig) {
1749 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1751 if (ipdevice->config_ipv6->method ==
1752 CONNMAN_IPCONFIG_METHOD_AUTO)
1753 disable_ipv6(ipdevice->config_ipv6);
1755 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1756 connman_ipconfig_unref(ipdevice->config_ipv6);
1757 ipdevice->config_ipv6 = NULL;
1764 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1767 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1769 case CONNMAN_IPCONFIG_METHOD_OFF:
1771 case CONNMAN_IPCONFIG_METHOD_FIXED:
1773 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1775 case CONNMAN_IPCONFIG_METHOD_DHCP:
1777 case CONNMAN_IPCONFIG_METHOD_AUTO:
1784 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1786 if (g_strcmp0(method, "off") == 0)
1787 return CONNMAN_IPCONFIG_METHOD_OFF;
1788 else if (g_strcmp0(method, "fixed") == 0)
1789 return CONNMAN_IPCONFIG_METHOD_FIXED;
1790 else if (g_strcmp0(method, "manual") == 0)
1791 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1792 else if (g_strcmp0(method, "dhcp") == 0)
1793 return CONNMAN_IPCONFIG_METHOD_DHCP;
1794 else if (g_strcmp0(method, "auto") == 0)
1795 return CONNMAN_IPCONFIG_METHOD_AUTO;
1797 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1800 static const char *privacy2string(int privacy)
1804 else if (privacy == 1)
1806 else if (privacy > 1)
1812 static int string2privacy(const char *privacy)
1814 if (g_strcmp0(privacy, "disabled") == 0)
1816 else if (g_strcmp0(privacy, "enabled") == 0)
1818 else if (g_strcmp0(privacy, "prefered") == 0)
1824 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1825 DBusMessageIter *iter)
1831 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1834 str = __connman_ipconfig_method2string(ipconfig->method);
1838 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1840 if (ipconfig->system == NULL)
1843 if (ipconfig->system->local != NULL) {
1845 struct in_addr netmask;
1848 connman_dbus_dict_append_basic(iter, "Address",
1849 DBUS_TYPE_STRING, &ipconfig->system->local);
1851 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1852 netmask.s_addr = htonl(addr);
1853 mask = inet_ntoa(netmask);
1854 connman_dbus_dict_append_basic(iter, "Netmask",
1855 DBUS_TYPE_STRING, &mask);
1858 if (ipconfig->system->gateway != NULL)
1859 connman_dbus_dict_append_basic(iter, "Gateway",
1860 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1863 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1864 DBusMessageIter *iter,
1865 struct connman_ipconfig *ipconfig_ipv4)
1867 const char *str, *privacy;
1871 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1874 str = __connman_ipconfig_method2string(ipconfig->method);
1878 if (ipconfig_ipv4 != NULL &&
1879 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1880 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1884 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1886 if (ipconfig->system == NULL)
1889 if (ipconfig->system->local != NULL) {
1890 connman_dbus_dict_append_basic(iter, "Address",
1891 DBUS_TYPE_STRING, &ipconfig->system->local);
1892 connman_dbus_dict_append_basic(iter, "PrefixLength",
1894 &ipconfig->system->prefixlen);
1897 if (ipconfig->system->gateway != NULL)
1898 connman_dbus_dict_append_basic(iter, "Gateway",
1899 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1901 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1902 connman_dbus_dict_append_basic(iter, "Privacy",
1903 DBUS_TYPE_STRING, &privacy);
1906 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1907 DBusMessageIter *iter)
1909 const char *str, *privacy;
1913 str = __connman_ipconfig_method2string(ipconfig->method);
1917 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1919 switch (ipconfig->method) {
1920 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1921 case CONNMAN_IPCONFIG_METHOD_OFF:
1922 case CONNMAN_IPCONFIG_METHOD_DHCP:
1924 case CONNMAN_IPCONFIG_METHOD_FIXED:
1925 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1926 case CONNMAN_IPCONFIG_METHOD_AUTO:
1930 if (ipconfig->address == NULL)
1933 if (ipconfig->address->local != NULL) {
1934 connman_dbus_dict_append_basic(iter, "Address",
1935 DBUS_TYPE_STRING, &ipconfig->address->local);
1936 connman_dbus_dict_append_basic(iter, "PrefixLength",
1938 &ipconfig->address->prefixlen);
1941 if (ipconfig->address->gateway != NULL)
1942 connman_dbus_dict_append_basic(iter, "Gateway",
1943 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1945 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1946 connman_dbus_dict_append_basic(iter, "Privacy",
1947 DBUS_TYPE_STRING, &privacy);
1950 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1951 DBusMessageIter *iter)
1957 str = __connman_ipconfig_method2string(ipconfig->method);
1961 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1963 switch (ipconfig->method) {
1964 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1965 case CONNMAN_IPCONFIG_METHOD_OFF:
1966 case CONNMAN_IPCONFIG_METHOD_DHCP:
1967 case CONNMAN_IPCONFIG_METHOD_AUTO:
1969 case CONNMAN_IPCONFIG_METHOD_FIXED:
1970 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1974 if (ipconfig->address == NULL)
1977 if (ipconfig->address->local != NULL) {
1979 struct in_addr netmask;
1982 connman_dbus_dict_append_basic(iter, "Address",
1983 DBUS_TYPE_STRING, &ipconfig->address->local);
1985 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1986 netmask.s_addr = htonl(addr);
1987 mask = inet_ntoa(netmask);
1988 connman_dbus_dict_append_basic(iter, "Netmask",
1989 DBUS_TYPE_STRING, &mask);
1992 if (ipconfig->address->gateway != NULL)
1993 connman_dbus_dict_append_basic(iter, "Gateway",
1994 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1997 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1998 DBusMessageIter *array)
2000 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2001 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2002 *prefix_length_string = NULL, *privacy_string = NULL;
2003 int prefix_length = 0, privacy = 0;
2004 DBusMessageIter dict;
2006 DBG("ipconfig %p", ipconfig);
2008 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2011 dbus_message_iter_recurse(array, &dict);
2013 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2014 DBusMessageIter entry;
2018 dbus_message_iter_recurse(&dict, &entry);
2020 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2023 dbus_message_iter_get_basic(&entry, &key);
2024 dbus_message_iter_next(&entry);
2026 type = dbus_message_iter_get_arg_type(&entry);
2028 if (g_str_equal(key, "Method") == TRUE) {
2031 if (type != DBUS_TYPE_STRING)
2034 dbus_message_iter_get_basic(&entry, &str);
2035 method = __connman_ipconfig_string2method(str);
2036 } else if (g_str_equal(key, "Address") == TRUE) {
2037 if (type != DBUS_TYPE_STRING)
2040 dbus_message_iter_get_basic(&entry, &address);
2041 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2042 if (type != DBUS_TYPE_STRING)
2045 dbus_message_iter_get_basic(&entry,
2046 &prefix_length_string);
2048 prefix_length = atoi(prefix_length_string);
2049 if (prefix_length < 0 || prefix_length > 128)
2052 } else if (g_str_equal(key, "Netmask") == TRUE) {
2053 if (type != DBUS_TYPE_STRING)
2056 dbus_message_iter_get_basic(&entry, &netmask);
2057 } else if (g_str_equal(key, "Gateway") == TRUE) {
2058 if (type != DBUS_TYPE_STRING)
2061 dbus_message_iter_get_basic(&entry, &gateway);
2062 } else if (g_str_equal(key, "Privacy") == TRUE) {
2063 if (type != DBUS_TYPE_STRING)
2066 dbus_message_iter_get_basic(&entry, &privacy_string);
2067 privacy = string2privacy(privacy_string);
2069 dbus_message_iter_next(&dict);
2072 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2074 method, address, netmask, gateway, prefix_length,
2078 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2079 case CONNMAN_IPCONFIG_METHOD_FIXED:
2082 case CONNMAN_IPCONFIG_METHOD_OFF:
2083 ipconfig->method = method;
2084 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2085 disable_ipv6(ipconfig);
2088 case CONNMAN_IPCONFIG_METHOD_AUTO:
2089 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2092 ipconfig->method = method;
2093 if (privacy_string != NULL)
2094 ipconfig->ipv6_privacy_config = privacy;
2095 enable_ipv6(ipconfig);
2098 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2099 if (address == NULL)
2102 ipconfig->method = method;
2104 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2105 connman_ipaddress_set_ipv4(ipconfig->address,
2106 address, netmask, gateway);
2108 return connman_ipaddress_set_ipv6(
2109 ipconfig->address, address,
2110 prefix_length, gateway);
2113 case CONNMAN_IPCONFIG_METHOD_DHCP:
2114 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2117 ipconfig->method = method;
2124 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2125 DBusMessageIter *iter)
2127 struct connman_ipdevice *ipdevice;
2128 const char *method = "auto";
2130 connman_dbus_dict_append_basic(iter, "Method",
2131 DBUS_TYPE_STRING, &method);
2133 ipdevice = g_hash_table_lookup(ipdevice_hash,
2134 GINT_TO_POINTER(ipconfig->index));
2135 if (ipdevice == NULL)
2138 if (ipdevice->ifname != NULL)
2139 connman_dbus_dict_append_basic(iter, "Interface",
2140 DBUS_TYPE_STRING, &ipdevice->ifname);
2142 if (ipdevice->address != NULL)
2143 connman_dbus_dict_append_basic(iter, "Address",
2144 DBUS_TYPE_STRING, &ipdevice->address);
2146 if (ipdevice->mtu > 0)
2147 connman_dbus_dict_append_basic(iter, "MTU",
2148 DBUS_TYPE_UINT16, &ipdevice->mtu);
2151 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2152 GKeyFile *keyfile, const char *identifier, const char *prefix)
2158 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2160 key = g_strdup_printf("%smethod", prefix);
2161 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2162 if (method == NULL) {
2163 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2164 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2166 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2168 ipconfig->method = __connman_ipconfig_string2method(method);
2170 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2171 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2173 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2174 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2175 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2177 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2178 privacy = g_key_file_get_string(keyfile, identifier,
2180 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2184 __connman_ipconfig_enable(ipconfig);
2185 enable_ipv6(ipconfig);
2192 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2193 ipconfig->address->prefixlen = g_key_file_get_integer(
2194 keyfile, identifier, key, NULL);
2197 key = g_strdup_printf("%slocal_address", prefix);
2198 ipconfig->address->local = g_key_file_get_string(
2199 keyfile, identifier, key, NULL);
2202 key = g_strdup_printf("%speer_address", prefix);
2203 ipconfig->address->peer = g_key_file_get_string(
2204 keyfile, identifier, key, NULL);
2207 key = g_strdup_printf("%sbroadcast_address", prefix);
2208 ipconfig->address->broadcast = g_key_file_get_string(
2209 keyfile, identifier, key, NULL);
2212 key = g_strdup_printf("%sgateway", prefix);
2213 ipconfig->address->gateway = g_key_file_get_string(
2214 keyfile, identifier, key, NULL);
2217 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2218 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2220 g_free(ipconfig->last_dhcp_address);
2221 ipconfig->last_dhcp_address = str;
2228 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2229 GKeyFile *keyfile, const char *identifier, const char *prefix)
2234 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2236 method = __connman_ipconfig_method2string(ipconfig->method);
2238 key = g_strdup_printf("%smethod", prefix);
2239 g_key_file_set_string(keyfile, identifier, key, method);
2242 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2243 const char *privacy;
2244 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2245 key = g_strdup_printf("%sprivacy", prefix);
2246 g_key_file_set_string(keyfile, identifier, key, privacy);
2250 switch (ipconfig->method) {
2251 case CONNMAN_IPCONFIG_METHOD_FIXED:
2252 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2254 case CONNMAN_IPCONFIG_METHOD_DHCP:
2255 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2256 if (ipconfig->last_dhcp_address != NULL &&
2257 strlen(ipconfig->last_dhcp_address) > 0)
2258 g_key_file_set_string(keyfile, identifier, key,
2259 ipconfig->last_dhcp_address);
2261 g_key_file_remove_key(keyfile, identifier, key, NULL);
2264 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2265 case CONNMAN_IPCONFIG_METHOD_OFF:
2266 case CONNMAN_IPCONFIG_METHOD_AUTO:
2270 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2271 g_key_file_set_integer(keyfile, identifier,
2272 key, ipconfig->address->prefixlen);
2275 key = g_strdup_printf("%slocal_address", prefix);
2276 if (ipconfig->address->local != NULL)
2277 g_key_file_set_string(keyfile, identifier,
2278 key, ipconfig->address->local);
2281 key = g_strdup_printf("%speer_address", prefix);
2282 if (ipconfig->address->peer != NULL)
2283 g_key_file_set_string(keyfile, identifier,
2284 key, ipconfig->address->peer);
2287 key = g_strdup_printf("%sbroadcast_address", prefix);
2288 if (ipconfig->address->broadcast != NULL)
2289 g_key_file_set_string(keyfile, identifier,
2290 key, ipconfig->address->broadcast);
2293 key = g_strdup_printf("%sgateway", prefix);
2294 if (ipconfig->address->gateway != NULL)
2295 g_key_file_set_string(keyfile, identifier,
2296 key, ipconfig->address->gateway);
2302 int __connman_ipconfig_init(void)
2306 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2307 NULL, free_ipdevice);
2312 void __connman_ipconfig_cleanup(void)
2316 g_hash_table_destroy(ipdevice_hash);
2317 ipdevice_hash = NULL;