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 void free_ipdevice(gpointer data)
457 struct connman_ipdevice *ipdevice = data;
459 connman_info("%s {remove} index %d", ipdevice->ifname,
462 if (ipdevice->config_ipv4 != NULL) {
463 connman_ipconfig_unref(ipdevice->config_ipv4);
464 ipdevice->config_ipv4 = NULL;
467 if (ipdevice->config_ipv6 != NULL) {
468 connman_ipconfig_unref(ipdevice->config_ipv6);
469 ipdevice->config_ipv6 = NULL;
472 free_address_list(ipdevice);
473 g_free(ipdevice->ipv4_gateway);
474 g_free(ipdevice->ipv6_gateway);
475 g_free(ipdevice->pac);
477 g_free(ipdevice->address);
479 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
480 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
482 g_free(ipdevice->ifname);
486 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
488 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
489 ipdevice->config_ipv6);
492 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
494 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
495 ipdevice->config_ipv6);
497 if (ipdevice->config_ipv4)
498 connman_inet_clear_address(ipdevice->index,
499 ipdevice->config_ipv4->address);
501 if (ipdevice->config_ipv6)
502 connman_inet_clear_ipv6_address(ipdevice->index,
503 ipdevice->config_ipv6->address->local,
504 ipdevice->config_ipv6->address->prefixlen);
507 static void update_stats(struct connman_ipdevice *ipdevice,
508 struct rtnl_link_stats *stats)
510 struct connman_service *service;
512 if (stats->rx_packets == 0 && stats->tx_packets == 0)
515 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
516 stats->rx_packets, stats->rx_bytes);
517 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
518 stats->tx_packets, stats->tx_bytes);
520 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
523 if (ipdevice->config_ipv4)
524 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
525 else if (ipdevice->config_ipv6)
526 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
533 ipdevice->rx_packets = stats->rx_packets;
534 ipdevice->tx_packets = stats->tx_packets;
535 ipdevice->rx_bytes = stats->rx_bytes;
536 ipdevice->tx_bytes = stats->tx_bytes;
537 ipdevice->rx_errors = stats->rx_errors;
538 ipdevice->tx_errors = stats->tx_errors;
539 ipdevice->rx_dropped = stats->rx_dropped;
540 ipdevice->tx_dropped = stats->tx_dropped;
542 __connman_service_notify(service,
543 ipdevice->rx_packets, ipdevice->tx_packets,
544 ipdevice->rx_bytes, ipdevice->tx_bytes,
545 ipdevice->rx_errors, ipdevice->tx_errors,
546 ipdevice->rx_dropped, ipdevice->tx_dropped);
549 void __connman_ipconfig_newlink(int index, unsigned short type,
550 unsigned int flags, const char *address,
552 struct rtnl_link_stats *stats)
554 struct connman_ipdevice *ipdevice;
557 gboolean up = FALSE, down = FALSE;
558 gboolean lower_up = FALSE, lower_down = FALSE;
560 DBG("index %d", index);
562 if (type == ARPHRD_LOOPBACK)
565 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
566 if (ipdevice != NULL)
569 ipdevice = g_try_new0(struct connman_ipdevice, 1);
570 if (ipdevice == NULL)
573 ipdevice->index = index;
574 ipdevice->ifname = connman_inet_ifname(index);
575 ipdevice->type = type;
577 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
578 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
580 ipdevice->address = g_strdup(address);
582 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
584 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
585 index, type, type2str(type));
590 update_stats(ipdevice, stats);
592 if (flags == ipdevice->flags)
595 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
602 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
603 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
604 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
605 (IFF_RUNNING | IFF_LOWER_UP))
607 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
611 ipdevice->flags = flags;
613 str = g_string_new(NULL);
618 g_string_append(str, "UP");
620 g_string_append(str, "DOWN");
622 if (flags & IFF_RUNNING)
623 g_string_append(str, ",RUNNING");
625 if (flags & IFF_LOWER_UP)
626 g_string_append(str, ",LOWER_UP");
628 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
631 g_string_free(str, TRUE);
633 for (list = g_list_first(ipconfig_list); list;
634 list = g_list_next(list)) {
635 struct connman_ipconfig *ipconfig = list->data;
637 if (index != ipconfig->index)
640 if (ipconfig->ops == NULL)
643 if (up == TRUE && ipconfig->ops->up)
644 ipconfig->ops->up(ipconfig);
645 if (lower_up == TRUE && ipconfig->ops->lower_up)
646 ipconfig->ops->lower_up(ipconfig);
648 if (lower_down == TRUE && ipconfig->ops->lower_down)
649 ipconfig->ops->lower_down(ipconfig);
650 if (down == TRUE && ipconfig->ops->down)
651 ipconfig->ops->down(ipconfig);
655 __connman_ipconfig_lower_up(ipdevice);
657 __connman_ipconfig_lower_down(ipdevice);
660 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
662 struct connman_ipdevice *ipdevice;
665 DBG("index %d", index);
667 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
668 if (ipdevice == NULL)
671 update_stats(ipdevice, stats);
673 for (list = g_list_first(ipconfig_list); list;
674 list = g_list_next(list)) {
675 struct connman_ipconfig *ipconfig = list->data;
677 if (index != ipconfig->index)
680 ipconfig->index = -1;
682 if (ipconfig->ops == NULL)
685 if (ipconfig->ops->lower_down)
686 ipconfig->ops->lower_down(ipconfig);
687 if (ipconfig->ops->down)
688 ipconfig->ops->down(ipconfig);
691 __connman_ipconfig_lower_down(ipdevice);
693 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
696 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
698 const struct connman_ipaddress *addr1 = a;
699 const struct connman_ipaddress *addr2 = b;
701 if (addr1->prefixlen != addr2->prefixlen)
702 return addr2->prefixlen - addr1->prefixlen;
704 return g_strcmp0(addr1->local, addr2->local);
707 void __connman_ipconfig_newaddr(int index, int family, const char *label,
708 unsigned char prefixlen, const char *address)
710 struct connman_ipdevice *ipdevice;
711 struct connman_ipaddress *ipaddress;
712 enum connman_ipconfig_type type;
715 DBG("index %d", index);
717 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
718 if (ipdevice == NULL)
721 ipaddress = connman_ipaddress_alloc(family);
722 if (ipaddress == NULL)
725 ipaddress->prefixlen = prefixlen;
726 ipaddress->local = g_strdup(address);
728 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
729 check_duplicate_address)) {
730 connman_ipaddress_free(ipaddress);
734 if (family == AF_INET)
735 type = CONNMAN_IPCONFIG_TYPE_IPV4;
736 else if (family == AF_INET6)
737 type = CONNMAN_IPCONFIG_TYPE_IPV6;
741 ipdevice->address_list = g_slist_append(ipdevice->address_list,
744 connman_info("%s {add} address %s/%u label %s family %d",
745 ipdevice->ifname, address, prefixlen, label, family);
747 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
748 connman_ipaddress_copy(ipdevice->config_ipv4->system,
751 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
752 connman_ipaddress_copy(ipdevice->config_ipv6->system,
757 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
760 for (list = g_list_first(ipconfig_list); list;
761 list = g_list_next(list)) {
762 struct connman_ipconfig *ipconfig = list->data;
764 if (index != ipconfig->index)
767 if (type != ipconfig->type)
770 if (ipconfig->ops == NULL)
773 if (ipconfig->ops->ip_bound)
774 ipconfig->ops->ip_bound(ipconfig);
778 void __connman_ipconfig_deladdr(int index, int family, const char *label,
779 unsigned char prefixlen, const char *address)
781 struct connman_ipdevice *ipdevice;
782 struct connman_ipaddress *ipaddress;
783 enum connman_ipconfig_type type;
786 DBG("index %d", index);
788 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
789 if (ipdevice == NULL)
792 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
793 if (ipaddress == NULL)
796 if (family == AF_INET)
797 type = CONNMAN_IPCONFIG_TYPE_IPV4;
798 else if (family == AF_INET6)
799 type = CONNMAN_IPCONFIG_TYPE_IPV6;
803 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
806 connman_ipaddress_clear(ipaddress);
809 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
810 address, prefixlen, label);
812 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
815 if (g_slist_length(ipdevice->address_list) > 0)
818 for (list = g_list_first(ipconfig_list); list;
819 list = g_list_next(list)) {
820 struct connman_ipconfig *ipconfig = list->data;
822 if (index != ipconfig->index)
825 if (type != ipconfig->type)
828 if (ipconfig->ops == NULL)
831 if (ipconfig->ops->ip_release)
832 ipconfig->ops->ip_release(ipconfig);
836 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
837 const char *dst, const char *gateway)
839 struct connman_ipdevice *ipdevice;
841 DBG("index %d", index);
843 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
844 if (ipdevice == NULL)
847 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
848 g_strcmp0(dst, "::") == 0)) {
851 enum connman_ipconfig_type type;
853 if (family == AF_INET6) {
854 type = CONNMAN_IPCONFIG_TYPE_IPV6;
855 g_free(ipdevice->ipv6_gateway);
856 ipdevice->ipv6_gateway = g_strdup(gateway);
858 if (ipdevice->config_ipv6 != NULL &&
859 ipdevice->config_ipv6->system != NULL) {
860 g_free(ipdevice->config_ipv6->system->gateway);
861 ipdevice->config_ipv6->system->gateway =
864 } else if (family == AF_INET) {
865 type = CONNMAN_IPCONFIG_TYPE_IPV4;
866 g_free(ipdevice->ipv4_gateway);
867 ipdevice->ipv4_gateway = g_strdup(gateway);
869 if (ipdevice->config_ipv4 != NULL &&
870 ipdevice->config_ipv4->system != NULL) {
871 g_free(ipdevice->config_ipv4->system->gateway);
872 ipdevice->config_ipv4->system->gateway =
878 for (list = ipdevice->address_list; list; list = list->next) {
879 struct connman_ipaddress *ipaddress = list->data;
881 g_free(ipaddress->gateway);
882 ipaddress->gateway = g_strdup(gateway);
885 for (config_list = g_list_first(ipconfig_list); config_list;
886 config_list = g_list_next(config_list)) {
887 struct connman_ipconfig *ipconfig = config_list->data;
889 if (index != ipconfig->index)
892 if (type != ipconfig->type)
895 if (ipconfig->ops == NULL)
898 if (ipconfig->ops->ip_bound)
899 ipconfig->ops->ip_bound(ipconfig);
903 connman_info("%s {add} route %s gw %s scope %u <%s>",
904 ipdevice->ifname, dst, gateway,
905 scope, scope2str(scope));
908 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
909 const char *dst, const char *gateway)
911 struct connman_ipdevice *ipdevice;
913 DBG("index %d", index);
915 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
916 if (ipdevice == NULL)
919 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
920 g_strcmp0(dst, "::") == 0)) {
923 enum connman_ipconfig_type type;
925 if (family == AF_INET6) {
926 type = CONNMAN_IPCONFIG_TYPE_IPV6;
927 g_free(ipdevice->ipv6_gateway);
928 ipdevice->ipv6_gateway = NULL;
930 if (ipdevice->config_ipv6 != NULL &&
931 ipdevice->config_ipv6->system != NULL) {
932 g_free(ipdevice->config_ipv6->system->gateway);
933 ipdevice->config_ipv6->system->gateway = NULL;
935 } else if (family == AF_INET) {
936 type = CONNMAN_IPCONFIG_TYPE_IPV4;
937 g_free(ipdevice->ipv4_gateway);
938 ipdevice->ipv4_gateway = NULL;
940 if (ipdevice->config_ipv4 != NULL &&
941 ipdevice->config_ipv4->system != NULL) {
942 g_free(ipdevice->config_ipv4->system->gateway);
943 ipdevice->config_ipv4->system->gateway = NULL;
948 for (list = ipdevice->address_list; list; list = list->next) {
949 struct connman_ipaddress *ipaddress = list->data;
951 g_free(ipaddress->gateway);
952 ipaddress->gateway = NULL;
955 for (config_list = g_list_first(ipconfig_list); config_list;
956 config_list = g_list_next(config_list)) {
957 struct connman_ipconfig *ipconfig = config_list->data;
959 if (index != ipconfig->index)
962 if (type != ipconfig->type)
965 if (ipconfig->ops == NULL)
968 if (ipconfig->ops->ip_release)
969 ipconfig->ops->ip_release(ipconfig);
973 connman_info("%s {del} route %s gw %s scope %u <%s>",
974 ipdevice->ifname, dst, gateway,
975 scope, scope2str(scope));
978 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
983 keys = g_hash_table_get_keys(ipdevice_hash);
987 for (list = g_list_first(keys); list; list = g_list_next(list)) {
988 int index = GPOINTER_TO_INT(list->data);
990 function(index, user_data);
996 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
997 struct connman_ipconfig *ipconfig)
999 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1002 unsigned short __connman_ipconfig_get_type_from_index(int index)
1004 struct connman_ipdevice *ipdevice;
1006 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1007 if (ipdevice == NULL)
1010 return ipdevice->type;
1013 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1015 struct connman_ipdevice *ipdevice;
1017 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1018 if (ipdevice == NULL)
1021 return ipdevice->flags;
1024 const char *__connman_ipconfig_get_gateway_from_index(int index)
1026 struct connman_ipdevice *ipdevice;
1028 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1029 if (ipdevice == NULL)
1032 if (ipdevice->ipv4_gateway != NULL)
1033 return ipdevice->ipv4_gateway;
1035 if (ipdevice->config_ipv4 != NULL &&
1036 ipdevice->config_ipv4->address != NULL)
1037 return ipdevice->config_ipv4->address->gateway;
1039 if (ipdevice->ipv6_gateway != NULL)
1040 return ipdevice->ipv6_gateway;
1042 if (ipdevice->config_ipv6 != NULL &&
1043 ipdevice->config_ipv6->address != NULL)
1044 return ipdevice->config_ipv6->address->gateway;
1049 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1051 ipconfig->index = index;
1054 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1056 if (ipconfig->address == NULL)
1059 return ipconfig->address->local;
1062 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1064 if (ipconfig->address == NULL)
1067 g_free(ipconfig->address->local);
1068 ipconfig->address->local = g_strdup(address);
1071 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1073 if (ipconfig->address == NULL)
1076 return ipconfig->address->peer;
1079 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1081 if (ipconfig->address == NULL)
1084 g_free(ipconfig->address->peer);
1085 ipconfig->address->peer = g_strdup(address);
1088 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1090 if (ipconfig->address == NULL)
1093 return ipconfig->address->broadcast;
1096 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1098 if (ipconfig->address == NULL)
1101 g_free(ipconfig->address->broadcast);
1102 ipconfig->address->broadcast = g_strdup(broadcast);
1105 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1107 if (ipconfig->address == NULL)
1110 return ipconfig->address->gateway;
1113 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1117 if (ipconfig->address == NULL)
1119 g_free(ipconfig->address->gateway);
1120 ipconfig->address->gateway = g_strdup(gateway);
1123 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1125 struct connman_service *service;
1129 if (ipconfig->address == NULL)
1132 service = __connman_service_lookup_from_index(ipconfig->index);
1133 if (service == NULL)
1136 __connman_connection_gateway_remove(service, ipconfig->type);
1138 DBG("type %d gw %s peer %s", ipconfig->type,
1139 ipconfig->address->gateway, ipconfig->address->peer);
1141 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1142 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1143 return __connman_connection_gateway_add(service,
1144 ipconfig->address->gateway,
1146 ipconfig->address->peer);
1151 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1153 struct connman_service *service;
1157 service = __connman_service_lookup_from_index(ipconfig->index);
1158 if (service != NULL)
1159 __connman_connection_gateway_remove(service, ipconfig->type);
1162 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1164 if (ipconfig->address == NULL)
1167 return ipconfig->address->prefixlen;
1170 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1172 if (ipconfig->address == NULL)
1175 ipconfig->address->prefixlen = prefixlen;
1178 static struct connman_ipconfig *create_ipv6config(int index)
1180 struct connman_ipconfig *ipv6config;
1182 DBG("index %d", index);
1184 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1185 if (ipv6config == NULL)
1188 ipv6config->refcount = 1;
1190 ipv6config->index = index;
1191 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1192 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1193 ipv6config->ipv6_privacy_config = 0;
1195 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1196 if (ipv6config->address == NULL) {
1201 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1203 DBG("ipconfig %p", ipv6config);
1209 * connman_ipconfig_create:
1211 * Allocate a new ipconfig structure.
1213 * Returns: a newly-allocated #connman_ipconfig structure
1215 struct connman_ipconfig *connman_ipconfig_create(int index,
1216 enum connman_ipconfig_type type)
1218 struct connman_ipconfig *ipconfig;
1220 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1221 return create_ipv6config(index);
1223 DBG("index %d", index);
1225 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1226 if (ipconfig == NULL)
1229 ipconfig->refcount = 1;
1231 ipconfig->index = index;
1232 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1234 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1235 if (ipconfig->address == NULL) {
1240 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1242 DBG("ipconfig %p", ipconfig);
1249 * connman_ipconfig_ref:
1250 * @ipconfig: ipconfig structure
1252 * Increase reference counter of ipconfig
1254 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1256 DBG("ipconfig %p refcount %d", ipconfig,
1257 g_atomic_int_get(&ipconfig->refcount) + 1);
1259 g_atomic_int_inc(&ipconfig->refcount);
1265 * connman_ipconfig_unref:
1266 * @ipconfig: ipconfig structure
1268 * Decrease reference counter of ipconfig
1270 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1272 if (ipconfig == NULL)
1275 DBG("ipconfig %p refcount %d", ipconfig,
1276 g_atomic_int_get(&ipconfig->refcount) - 1);
1278 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1279 __connman_ipconfig_disable(ipconfig);
1281 connman_ipconfig_set_ops(ipconfig, NULL);
1283 if (ipconfig->origin != NULL) {
1284 connman_ipconfig_unref(ipconfig->origin);
1285 ipconfig->origin = NULL;
1288 connman_ipaddress_free(ipconfig->system);
1289 connman_ipaddress_free(ipconfig->address);
1290 g_free(ipconfig->last_dhcp_address);
1296 * connman_ipconfig_get_data:
1297 * @ipconfig: ipconfig structure
1299 * Get private data pointer
1301 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1303 if (ipconfig == NULL)
1306 return ipconfig->ops_data;
1310 * connman_ipconfig_set_data:
1311 * @ipconfig: ipconfig structure
1312 * @data: data pointer
1314 * Set private data pointer
1316 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1318 ipconfig->ops_data = data;
1322 * connman_ipconfig_get_index:
1323 * @ipconfig: ipconfig structure
1325 * Get interface index
1327 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1329 if (ipconfig == NULL)
1332 if (ipconfig->origin != NULL)
1333 return ipconfig->origin->index;
1335 return ipconfig->index;
1339 * connman_ipconfig_get_ifname:
1340 * @ipconfig: ipconfig structure
1342 * Get interface name
1344 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1346 struct connman_ipdevice *ipdevice;
1348 if (ipconfig == NULL)
1351 if (ipconfig->index < 0)
1354 ipdevice = g_hash_table_lookup(ipdevice_hash,
1355 GINT_TO_POINTER(ipconfig->index));
1356 if (ipdevice == NULL)
1359 return ipdevice->ifname;
1363 * connman_ipconfig_set_ops:
1364 * @ipconfig: ipconfig structure
1365 * @ops: operation callbacks
1367 * Set the operation callbacks
1369 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1370 const struct connman_ipconfig_ops *ops)
1372 ipconfig->ops = ops;
1376 * connman_ipconfig_set_method:
1377 * @ipconfig: ipconfig structure
1378 * @method: configuration method
1380 * Set the configuration method
1382 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1383 enum connman_ipconfig_method method)
1385 ipconfig->method = method;
1390 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1392 if (ipconfig == NULL)
1393 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1395 return ipconfig->method;
1398 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1402 switch (ipconfig->method) {
1403 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1404 case CONNMAN_IPCONFIG_METHOD_OFF:
1405 case CONNMAN_IPCONFIG_METHOD_AUTO:
1407 case CONNMAN_IPCONFIG_METHOD_FIXED:
1408 case CONNMAN_IPCONFIG_METHOD_DHCP:
1409 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1410 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1411 return connman_inet_set_address(ipconfig->index,
1413 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1414 return connman_inet_set_ipv6_address(
1415 ipconfig->index, ipconfig->address);
1421 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1427 if (ipconfig == NULL)
1430 DBG("method %d", ipconfig->method);
1432 switch (ipconfig->method) {
1433 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1434 case CONNMAN_IPCONFIG_METHOD_OFF:
1435 case CONNMAN_IPCONFIG_METHOD_AUTO:
1437 case CONNMAN_IPCONFIG_METHOD_FIXED:
1438 case CONNMAN_IPCONFIG_METHOD_DHCP:
1439 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1440 err = __connman_ipconfig_address_unset(ipconfig);
1441 connman_ipaddress_clear(ipconfig->address);
1449 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1455 if (ipconfig == NULL)
1458 DBG("method %d", ipconfig->method);
1460 switch (ipconfig->method) {
1461 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1462 case CONNMAN_IPCONFIG_METHOD_OFF:
1463 case CONNMAN_IPCONFIG_METHOD_AUTO:
1465 case CONNMAN_IPCONFIG_METHOD_FIXED:
1466 case CONNMAN_IPCONFIG_METHOD_DHCP:
1467 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1468 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1469 err = connman_inet_clear_address(ipconfig->index,
1471 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1472 err = connman_inet_clear_ipv6_address(
1474 ipconfig->address->local,
1475 ipconfig->address->prefixlen);
1485 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1488 struct connman_ipdevice *ipdevice;
1490 DBG("ipconfig %p", ipconfig);
1492 if (ipconfig == NULL || ipconfig->index < 0)
1495 ipdevice = g_hash_table_lookup(ipdevice_hash,
1496 GINT_TO_POINTER(ipconfig->index));
1497 if (ipdevice == NULL)
1500 g_free(ipdevice->pac);
1501 ipdevice->pac = g_strdup(url);
1506 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1508 struct connman_ipdevice *ipdevice;
1510 DBG("ipconfig %p", ipconfig);
1512 if (ipconfig == NULL || ipconfig->index < 0)
1515 ipdevice = g_hash_table_lookup(ipdevice_hash,
1516 GINT_TO_POINTER(ipconfig->index));
1517 if (ipdevice == NULL)
1520 return ipdevice->pac;
1523 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1524 const char *address)
1526 if (ipconfig == NULL)
1529 g_free(ipconfig->last_dhcp_address);
1530 ipconfig->last_dhcp_address = g_strdup(address);
1533 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1535 if (ipconfig == NULL)
1538 return ipconfig->last_dhcp_address;
1541 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1543 struct connman_ipdevice *ipdevice;
1547 ipdevice = g_hash_table_lookup(ipdevice_hash,
1548 GINT_TO_POINTER(ipconfig->index));
1549 if (ipdevice == NULL)
1552 set_ipv6_state(ipdevice->ifname, FALSE);
1555 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1557 struct connman_ipdevice *ipdevice;
1561 ipdevice = g_hash_table_lookup(ipdevice_hash,
1562 GINT_TO_POINTER(ipconfig->index));
1563 if (ipdevice == NULL)
1566 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1567 set_ipv6_privacy(ipdevice->ifname,
1568 ipconfig->ipv6_privacy_config);
1570 set_ipv6_state(ipdevice->ifname, TRUE);
1573 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1575 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1578 enable_ipv6(ipconfig);
1581 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1583 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1586 disable_ipv6(ipconfig);
1589 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1591 struct connman_ipdevice *ipdevice;
1592 gboolean up = FALSE, down = FALSE;
1593 gboolean lower_up = FALSE, lower_down = FALSE;
1594 enum connman_ipconfig_type type;
1596 DBG("ipconfig %p", ipconfig);
1598 if (ipconfig == NULL || ipconfig->index < 0)
1601 ipdevice = g_hash_table_lookup(ipdevice_hash,
1602 GINT_TO_POINTER(ipconfig->index));
1603 if (ipdevice == NULL)
1606 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1607 if (ipdevice->config_ipv4 == ipconfig)
1609 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1610 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1611 if (ipdevice->config_ipv6 == ipconfig)
1613 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1614 enable_ipv6(ipconfig);
1618 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1619 ipdevice->config_ipv4 != NULL) {
1620 ipconfig_list = g_list_remove(ipconfig_list,
1621 ipdevice->config_ipv4);
1623 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1625 connman_ipconfig_unref(ipdevice->config_ipv4);
1628 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1629 ipdevice->config_ipv6 != NULL) {
1630 ipconfig_list = g_list_remove(ipconfig_list,
1631 ipdevice->config_ipv6);
1633 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1635 connman_ipconfig_unref(ipdevice->config_ipv6);
1638 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1639 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1640 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1641 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1643 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1645 if (ipdevice->flags & IFF_UP)
1650 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1651 (IFF_RUNNING | IFF_LOWER_UP))
1653 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1656 if (up == TRUE && ipconfig->ops->up)
1657 ipconfig->ops->up(ipconfig);
1658 if (lower_up == TRUE && ipconfig->ops->lower_up)
1659 ipconfig->ops->lower_up(ipconfig);
1661 if (lower_down == TRUE && ipconfig->ops->lower_down)
1662 ipconfig->ops->lower_down(ipconfig);
1663 if (down == TRUE && ipconfig->ops->down)
1664 ipconfig->ops->down(ipconfig);
1669 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1671 struct connman_ipdevice *ipdevice;
1673 DBG("ipconfig %p", ipconfig);
1675 if (ipconfig == NULL || ipconfig->index < 0)
1678 ipdevice = g_hash_table_lookup(ipdevice_hash,
1679 GINT_TO_POINTER(ipconfig->index));
1680 if (ipdevice == NULL)
1683 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1686 if (ipdevice->config_ipv4 == ipconfig) {
1687 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1689 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1690 connman_ipconfig_unref(ipdevice->config_ipv4);
1691 ipdevice->config_ipv4 = NULL;
1695 if (ipdevice->config_ipv6 == ipconfig) {
1696 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1698 if (ipdevice->config_ipv6->method ==
1699 CONNMAN_IPCONFIG_METHOD_AUTO)
1700 disable_ipv6(ipdevice->config_ipv6);
1702 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1703 connman_ipconfig_unref(ipdevice->config_ipv6);
1704 ipdevice->config_ipv6 = NULL;
1711 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1714 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1716 case CONNMAN_IPCONFIG_METHOD_OFF:
1718 case CONNMAN_IPCONFIG_METHOD_FIXED:
1720 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1722 case CONNMAN_IPCONFIG_METHOD_DHCP:
1724 case CONNMAN_IPCONFIG_METHOD_AUTO:
1731 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1733 if (g_strcmp0(method, "off") == 0)
1734 return CONNMAN_IPCONFIG_METHOD_OFF;
1735 else if (g_strcmp0(method, "fixed") == 0)
1736 return CONNMAN_IPCONFIG_METHOD_FIXED;
1737 else if (g_strcmp0(method, "manual") == 0)
1738 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1739 else if (g_strcmp0(method, "dhcp") == 0)
1740 return CONNMAN_IPCONFIG_METHOD_DHCP;
1741 else if (g_strcmp0(method, "auto") == 0)
1742 return CONNMAN_IPCONFIG_METHOD_AUTO;
1744 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1747 static const char *privacy2string(int privacy)
1751 else if (privacy == 1)
1753 else if (privacy > 1)
1759 static int string2privacy(const char *privacy)
1761 if (g_strcmp0(privacy, "disabled") == 0)
1763 else if (g_strcmp0(privacy, "enabled") == 0)
1765 else if (g_strcmp0(privacy, "prefered") == 0)
1771 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1772 DBusMessageIter *iter)
1778 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1781 str = __connman_ipconfig_method2string(ipconfig->method);
1785 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1787 if (ipconfig->system == NULL)
1790 if (ipconfig->system->local != NULL) {
1792 struct in_addr netmask;
1795 connman_dbus_dict_append_basic(iter, "Address",
1796 DBUS_TYPE_STRING, &ipconfig->system->local);
1798 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1799 netmask.s_addr = htonl(addr);
1800 mask = inet_ntoa(netmask);
1801 connman_dbus_dict_append_basic(iter, "Netmask",
1802 DBUS_TYPE_STRING, &mask);
1805 if (ipconfig->system->gateway != NULL)
1806 connman_dbus_dict_append_basic(iter, "Gateway",
1807 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1810 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1811 DBusMessageIter *iter,
1812 struct connman_ipconfig *ipconfig_ipv4)
1814 const char *str, *privacy;
1818 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1821 str = __connman_ipconfig_method2string(ipconfig->method);
1825 if (ipconfig_ipv4 != NULL &&
1826 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1827 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1831 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1833 if (ipconfig->system == NULL)
1836 if (ipconfig->system->local != NULL) {
1837 connman_dbus_dict_append_basic(iter, "Address",
1838 DBUS_TYPE_STRING, &ipconfig->system->local);
1839 connman_dbus_dict_append_basic(iter, "PrefixLength",
1841 &ipconfig->system->prefixlen);
1844 if (ipconfig->system->gateway != NULL)
1845 connman_dbus_dict_append_basic(iter, "Gateway",
1846 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1848 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1849 connman_dbus_dict_append_basic(iter, "Privacy",
1850 DBUS_TYPE_STRING, &privacy);
1853 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1854 DBusMessageIter *iter)
1856 const char *str, *privacy;
1860 str = __connman_ipconfig_method2string(ipconfig->method);
1864 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1866 switch (ipconfig->method) {
1867 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1868 case CONNMAN_IPCONFIG_METHOD_OFF:
1869 case CONNMAN_IPCONFIG_METHOD_DHCP:
1871 case CONNMAN_IPCONFIG_METHOD_FIXED:
1872 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1873 case CONNMAN_IPCONFIG_METHOD_AUTO:
1877 if (ipconfig->address == NULL)
1880 if (ipconfig->address->local != NULL) {
1881 connman_dbus_dict_append_basic(iter, "Address",
1882 DBUS_TYPE_STRING, &ipconfig->address->local);
1883 connman_dbus_dict_append_basic(iter, "PrefixLength",
1885 &ipconfig->address->prefixlen);
1888 if (ipconfig->address->gateway != NULL)
1889 connman_dbus_dict_append_basic(iter, "Gateway",
1890 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1892 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1893 connman_dbus_dict_append_basic(iter, "Privacy",
1894 DBUS_TYPE_STRING, &privacy);
1897 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1898 DBusMessageIter *iter)
1904 str = __connman_ipconfig_method2string(ipconfig->method);
1908 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1910 switch (ipconfig->method) {
1911 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1912 case CONNMAN_IPCONFIG_METHOD_OFF:
1913 case CONNMAN_IPCONFIG_METHOD_DHCP:
1914 case CONNMAN_IPCONFIG_METHOD_AUTO:
1916 case CONNMAN_IPCONFIG_METHOD_FIXED:
1917 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1921 if (ipconfig->address == NULL)
1924 if (ipconfig->address->local != NULL) {
1926 struct in_addr netmask;
1929 connman_dbus_dict_append_basic(iter, "Address",
1930 DBUS_TYPE_STRING, &ipconfig->address->local);
1932 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1933 netmask.s_addr = htonl(addr);
1934 mask = inet_ntoa(netmask);
1935 connman_dbus_dict_append_basic(iter, "Netmask",
1936 DBUS_TYPE_STRING, &mask);
1939 if (ipconfig->address->gateway != NULL)
1940 connman_dbus_dict_append_basic(iter, "Gateway",
1941 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1944 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1945 DBusMessageIter *array)
1947 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1948 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1949 *prefix_length_string = NULL, *privacy_string = NULL;
1950 int prefix_length = 0, privacy = 0;
1951 DBusMessageIter dict;
1953 DBG("ipconfig %p", ipconfig);
1955 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1958 dbus_message_iter_recurse(array, &dict);
1960 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1961 DBusMessageIter entry;
1965 dbus_message_iter_recurse(&dict, &entry);
1967 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1970 dbus_message_iter_get_basic(&entry, &key);
1971 dbus_message_iter_next(&entry);
1973 type = dbus_message_iter_get_arg_type(&entry);
1975 if (g_str_equal(key, "Method") == TRUE) {
1978 if (type != DBUS_TYPE_STRING)
1981 dbus_message_iter_get_basic(&entry, &str);
1982 method = __connman_ipconfig_string2method(str);
1983 } else if (g_str_equal(key, "Address") == TRUE) {
1984 if (type != DBUS_TYPE_STRING)
1987 dbus_message_iter_get_basic(&entry, &address);
1988 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1989 if (type != DBUS_TYPE_STRING)
1992 dbus_message_iter_get_basic(&entry,
1993 &prefix_length_string);
1995 prefix_length = atoi(prefix_length_string);
1996 if (prefix_length < 0 || prefix_length > 128)
1999 } else if (g_str_equal(key, "Netmask") == TRUE) {
2000 if (type != DBUS_TYPE_STRING)
2003 dbus_message_iter_get_basic(&entry, &netmask);
2004 } else if (g_str_equal(key, "Gateway") == TRUE) {
2005 if (type != DBUS_TYPE_STRING)
2008 dbus_message_iter_get_basic(&entry, &gateway);
2009 } else if (g_str_equal(key, "Privacy") == TRUE) {
2010 if (type != DBUS_TYPE_STRING)
2013 dbus_message_iter_get_basic(&entry, &privacy_string);
2014 privacy = string2privacy(privacy_string);
2016 dbus_message_iter_next(&dict);
2019 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2021 method, address, netmask, gateway, prefix_length,
2025 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2026 case CONNMAN_IPCONFIG_METHOD_FIXED:
2029 case CONNMAN_IPCONFIG_METHOD_OFF:
2030 ipconfig->method = method;
2031 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2032 disable_ipv6(ipconfig);
2035 case CONNMAN_IPCONFIG_METHOD_AUTO:
2036 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2039 ipconfig->method = method;
2040 if (privacy_string != NULL)
2041 ipconfig->ipv6_privacy_config = privacy;
2042 enable_ipv6(ipconfig);
2045 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2046 if (address == NULL)
2049 ipconfig->method = method;
2051 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2052 connman_ipaddress_set_ipv4(ipconfig->address,
2053 address, netmask, gateway);
2055 return connman_ipaddress_set_ipv6(
2056 ipconfig->address, address,
2057 prefix_length, gateway);
2060 case CONNMAN_IPCONFIG_METHOD_DHCP:
2061 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2064 ipconfig->method = method;
2071 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2072 DBusMessageIter *iter)
2074 struct connman_ipdevice *ipdevice;
2075 const char *method = "auto";
2077 connman_dbus_dict_append_basic(iter, "Method",
2078 DBUS_TYPE_STRING, &method);
2080 ipdevice = g_hash_table_lookup(ipdevice_hash,
2081 GINT_TO_POINTER(ipconfig->index));
2082 if (ipdevice == NULL)
2085 if (ipdevice->ifname != NULL)
2086 connman_dbus_dict_append_basic(iter, "Interface",
2087 DBUS_TYPE_STRING, &ipdevice->ifname);
2089 if (ipdevice->address != NULL)
2090 connman_dbus_dict_append_basic(iter, "Address",
2091 DBUS_TYPE_STRING, &ipdevice->address);
2093 if (ipdevice->mtu > 0)
2094 connman_dbus_dict_append_basic(iter, "MTU",
2095 DBUS_TYPE_UINT16, &ipdevice->mtu);
2098 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2099 GKeyFile *keyfile, const char *identifier, const char *prefix)
2105 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2107 key = g_strdup_printf("%smethod", prefix);
2108 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2109 if (method == NULL) {
2110 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2111 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2113 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2115 ipconfig->method = __connman_ipconfig_string2method(method);
2117 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2118 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2120 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2121 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2122 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2124 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2125 privacy = g_key_file_get_string(keyfile, identifier,
2127 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2131 __connman_ipconfig_enable(ipconfig);
2132 enable_ipv6(ipconfig);
2139 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2140 ipconfig->address->prefixlen = g_key_file_get_integer(
2141 keyfile, identifier, key, NULL);
2144 key = g_strdup_printf("%slocal_address", prefix);
2145 ipconfig->address->local = g_key_file_get_string(
2146 keyfile, identifier, key, NULL);
2149 key = g_strdup_printf("%speer_address", prefix);
2150 ipconfig->address->peer = g_key_file_get_string(
2151 keyfile, identifier, key, NULL);
2154 key = g_strdup_printf("%sbroadcast_address", prefix);
2155 ipconfig->address->broadcast = g_key_file_get_string(
2156 keyfile, identifier, key, NULL);
2159 key = g_strdup_printf("%sgateway", prefix);
2160 ipconfig->address->gateway = g_key_file_get_string(
2161 keyfile, identifier, key, NULL);
2164 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2165 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2167 g_free(ipconfig->last_dhcp_address);
2168 ipconfig->last_dhcp_address = str;
2175 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2176 GKeyFile *keyfile, const char *identifier, const char *prefix)
2181 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2183 method = __connman_ipconfig_method2string(ipconfig->method);
2185 key = g_strdup_printf("%smethod", prefix);
2186 g_key_file_set_string(keyfile, identifier, key, method);
2189 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2190 const char *privacy;
2191 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2192 key = g_strdup_printf("%sprivacy", prefix);
2193 g_key_file_set_string(keyfile, identifier, key, privacy);
2197 switch (ipconfig->method) {
2198 case CONNMAN_IPCONFIG_METHOD_FIXED:
2199 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2201 case CONNMAN_IPCONFIG_METHOD_DHCP:
2202 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2203 if (ipconfig->last_dhcp_address != NULL &&
2204 strlen(ipconfig->last_dhcp_address) > 0)
2205 g_key_file_set_string(keyfile, identifier, key,
2206 ipconfig->last_dhcp_address);
2208 g_key_file_remove_key(keyfile, identifier, key, NULL);
2211 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2212 case CONNMAN_IPCONFIG_METHOD_OFF:
2213 case CONNMAN_IPCONFIG_METHOD_AUTO:
2217 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2218 g_key_file_set_integer(keyfile, identifier,
2219 key, ipconfig->address->prefixlen);
2222 key = g_strdup_printf("%slocal_address", prefix);
2223 if (ipconfig->address->local != NULL)
2224 g_key_file_set_string(keyfile, identifier,
2225 key, ipconfig->address->local);
2228 key = g_strdup_printf("%speer_address", prefix);
2229 if (ipconfig->address->peer != NULL)
2230 g_key_file_set_string(keyfile, identifier,
2231 key, ipconfig->address->peer);
2234 key = g_strdup_printf("%sbroadcast_address", prefix);
2235 if (ipconfig->address->broadcast != NULL)
2236 g_key_file_set_string(keyfile, identifier,
2237 key, ipconfig->address->broadcast);
2240 key = g_strdup_printf("%sgateway", prefix);
2241 if (ipconfig->address->gateway != NULL)
2242 g_key_file_set_string(keyfile, identifier,
2243 key, ipconfig->address->gateway);
2249 int __connman_ipconfig_init(void)
2253 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2254 NULL, free_ipdevice);
2259 void __connman_ipconfig_cleanup(void)
2263 g_hash_table_destroy(ipdevice_hash);
2264 ipdevice_hash = NULL;