5 * Copyright (C) 2007-2013 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
39 #include <connman/ipaddress.h>
43 struct connman_ipconfig {
46 enum connman_ipconfig_type type;
48 const struct connman_ipconfig_ops *ops;
52 enum connman_ipconfig_method method;
53 struct connman_ipaddress *address;
54 struct connman_ipaddress *system;
57 int dhcp_lease_duration;
60 int ipv6_privacy_config;
61 char *last_dhcp_address;
62 char **last_dhcpv6_prefixes;
65 struct connman_ipdevice {
86 struct connman_ipconfig *config_ipv4;
87 struct connman_ipconfig *config_ipv6;
93 static GHashTable *ipdevice_hash = NULL;
94 static GList *ipconfig_list = NULL;
95 static bool is_ipv6_supported = false;
97 void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
102 connman_ipaddress_clear(ipconfig->address);
105 static void free_address_list(struct connman_ipdevice *ipdevice)
109 for (list = ipdevice->address_list; list; list = list->next) {
110 struct connman_ipaddress *ipaddress = list->data;
112 connman_ipaddress_free(ipaddress);
116 g_slist_free(ipdevice->address_list);
117 ipdevice->address_list = NULL;
120 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
121 unsigned char prefixlen, const char *local)
125 for (list = ipdevice->address_list; list; list = list->next) {
126 struct connman_ipaddress *ipaddress = list->data;
128 if (g_strcmp0(ipaddress->local, local) == 0 &&
129 ipaddress->prefixlen == prefixlen)
136 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
139 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
140 case CONNMAN_IPCONFIG_TYPE_ALL:
142 case CONNMAN_IPCONFIG_TYPE_IPV4:
144 case CONNMAN_IPCONFIG_TYPE_IPV6:
151 static const char *type2str(unsigned short type)
156 case ARPHRD_LOOPBACK:
169 static const char *scope2str(unsigned char scope)
181 static bool get_ipv6_state(gchar *ifname)
186 bool enabled = false;
189 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
191 path = g_strdup_printf(
192 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
197 f = fopen(path, "r");
202 if (fscanf(f, "%d", &disabled) > 0)
210 static void set_ipv6_state(gchar *ifname, bool enable)
216 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
218 path = g_strdup_printf(
219 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
224 f = fopen(path, "r+");
239 static int get_ipv6_privacy(gchar *ifname)
248 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
254 f = fopen(path, "r");
261 if (fscanf(f, "%d", &value) <= 0)
269 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
270 * The privacy extension is described in RFC 3041 and RFC 4941
272 static void set_ipv6_privacy(gchar *ifname, int value)
280 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
289 f = fopen(path, "r+");
296 fprintf(f, "%d", value);
300 static int get_rp_filter(void)
303 int value = -EINVAL, tmp;
305 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
308 if (fscanf(f, "%d", &tmp) == 1)
316 static void set_rp_filter(int value)
320 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
325 fprintf(f, "%d", value);
330 int __connman_ipconfig_set_rp_filter()
334 value = get_rp_filter();
341 connman_info("rp_filter set to 2 (loose mode routing), "
342 "old value was %d", value);
347 void __connman_ipconfig_unset_rp_filter(int old_value)
349 set_rp_filter(old_value);
351 connman_info("rp_filter restored to %d", old_value);
354 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
359 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
362 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
364 struct connman_ipdevice *ipdevice;
371 ipdevice = g_hash_table_lookup(ipdevice_hash,
372 GINT_TO_POINTER(ipconfig->index));
376 ifname = connman_inet_ifname(ipconfig->index);
377 ret = get_ipv6_state(ifname);
383 static void free_ipdevice(gpointer data)
385 struct connman_ipdevice *ipdevice = data;
386 char *ifname = connman_inet_ifname(ipdevice->index);
388 connman_info("%s {remove} index %d", ifname, ipdevice->index);
390 if (ipdevice->config_ipv4) {
391 __connman_ipconfig_unref(ipdevice->config_ipv4);
392 ipdevice->config_ipv4 = NULL;
395 if (ipdevice->config_ipv6) {
396 __connman_ipconfig_unref(ipdevice->config_ipv6);
397 ipdevice->config_ipv6 = NULL;
400 free_address_list(ipdevice);
401 g_free(ipdevice->ipv4_gateway);
402 g_free(ipdevice->ipv6_gateway);
403 g_free(ipdevice->pac);
405 g_free(ipdevice->address);
408 set_ipv6_state(ifname, ipdevice->ipv6_enabled);
409 set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
416 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
418 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
419 ipdevice->config_ipv6);
420 #if defined TIZEN_EXT
421 if (ipdevice->config_ipv6 != NULL &&
422 ipdevice->config_ipv6->enabled == TRUE)
425 char *ifname = connman_inet_ifname(ipdevice->index);
427 if (__connman_device_isfiltered(ifname) == FALSE) {
428 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
429 set_ipv6_state(ifname, FALSE);
435 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
437 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
438 ipdevice->config_ipv6);
440 if (ipdevice->config_ipv4)
441 connman_inet_clear_address(ipdevice->index,
442 ipdevice->config_ipv4->address);
444 if (ipdevice->config_ipv6)
445 connman_inet_clear_ipv6_address(ipdevice->index,
446 ipdevice->config_ipv6->address->local,
447 ipdevice->config_ipv6->address->prefixlen);
450 static void update_stats(struct connman_ipdevice *ipdevice,
451 const char *ifname, struct rtnl_link_stats *stats)
453 struct connman_service *service;
455 if (stats->rx_packets == 0 && stats->tx_packets == 0)
458 connman_info("%s {RX} %u packets %u bytes", ifname,
459 stats->rx_packets, stats->rx_bytes);
460 connman_info("%s {TX} %u packets %u bytes", ifname,
461 stats->tx_packets, stats->tx_bytes);
463 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
466 service = __connman_service_lookup_from_index(ipdevice->index);
468 DBG("service %p", service);
473 ipdevice->rx_packets = stats->rx_packets;
474 ipdevice->tx_packets = stats->tx_packets;
475 ipdevice->rx_bytes = stats->rx_bytes;
476 ipdevice->tx_bytes = stats->tx_bytes;
477 ipdevice->rx_errors = stats->rx_errors;
478 ipdevice->tx_errors = stats->tx_errors;
479 ipdevice->rx_dropped = stats->rx_dropped;
480 ipdevice->tx_dropped = stats->tx_dropped;
482 __connman_service_notify(service,
483 ipdevice->rx_packets, ipdevice->tx_packets,
484 ipdevice->rx_bytes, ipdevice->tx_bytes,
485 ipdevice->rx_errors, ipdevice->tx_errors,
486 ipdevice->rx_dropped, ipdevice->tx_dropped);
489 void __connman_ipconfig_newlink(int index, unsigned short type,
490 unsigned int flags, const char *address,
492 struct rtnl_link_stats *stats)
494 struct connman_ipdevice *ipdevice;
495 GList *list, *ipconfig_copy;
497 bool up = false, down = false;
498 bool lower_up = false, lower_down = false;
501 DBG("index %d", index);
503 if (type == ARPHRD_LOOPBACK)
506 ifname = connman_inet_ifname(index);
508 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
512 ipdevice = g_try_new0(struct connman_ipdevice, 1);
516 ipdevice->index = index;
517 ipdevice->type = type;
519 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
520 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
522 ipdevice->address = g_strdup(address);
524 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
526 connman_info("%s {create} index %d type %d <%s>", ifname,
527 index, type, type2str(type));
530 #if defined TIZEN_EXT
531 if (g_strcmp0(ipdevice->address, address) != 0) {
532 /* If an original address is built-in physical device,
533 * it's hardly get an address at a initial creation
535 g_free(ipdevice->address);
536 ipdevice->address = g_strdup(address);
542 update_stats(ipdevice, ifname, stats);
544 if (flags == ipdevice->flags)
547 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
554 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
555 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
556 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
557 (IFF_RUNNING | IFF_LOWER_UP))
559 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
563 ipdevice->flags = flags;
565 str = g_string_new(NULL);
570 g_string_append(str, "UP");
572 g_string_append(str, "DOWN");
574 if (flags & IFF_RUNNING)
575 g_string_append(str, ",RUNNING");
577 if (flags & IFF_LOWER_UP)
578 g_string_append(str, ",LOWER_UP");
580 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
582 g_string_free(str, TRUE);
584 ipconfig_copy = g_list_copy(ipconfig_list);
586 for (list = g_list_first(ipconfig_copy); list;
587 list = g_list_next(list)) {
588 struct connman_ipconfig *ipconfig = list->data;
590 if (index != ipconfig->index)
596 if (up && ipconfig->ops->up)
597 ipconfig->ops->up(ipconfig, ifname);
598 if (lower_up && ipconfig->ops->lower_up)
599 ipconfig->ops->lower_up(ipconfig, ifname);
601 if (lower_down && ipconfig->ops->lower_down)
602 ipconfig->ops->lower_down(ipconfig, ifname);
603 if (down && ipconfig->ops->down)
604 ipconfig->ops->down(ipconfig, ifname);
607 g_list_free(ipconfig_copy);
610 __connman_ipconfig_lower_up(ipdevice);
612 __connman_ipconfig_lower_down(ipdevice);
618 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
620 struct connman_ipdevice *ipdevice;
624 DBG("index %d", index);
626 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
630 ifname = connman_inet_ifname(index);
632 update_stats(ipdevice, ifname, stats);
634 for (list = g_list_first(ipconfig_list); list;
635 list = g_list_next(list)) {
636 struct connman_ipconfig *ipconfig = list->data;
638 if (index != ipconfig->index)
641 ipconfig->index = -1;
646 if (ipconfig->ops->lower_down)
647 ipconfig->ops->lower_down(ipconfig, ifname);
648 if (ipconfig->ops->down)
649 ipconfig->ops->down(ipconfig, ifname);
654 __connman_ipconfig_lower_down(ipdevice);
656 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
659 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
661 const struct connman_ipaddress *addr1 = a;
662 const struct connman_ipaddress *addr2 = b;
664 if (addr1->prefixlen != addr2->prefixlen)
665 return addr2->prefixlen - addr1->prefixlen;
667 return g_strcmp0(addr1->local, addr2->local);
670 int __connman_ipconfig_newaddr(int index, int family, const char *label,
671 unsigned char prefixlen, const char *address)
673 struct connman_ipdevice *ipdevice;
674 struct connman_ipaddress *ipaddress;
675 enum connman_ipconfig_type type;
679 DBG("index %d", index);
681 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
685 ipaddress = connman_ipaddress_alloc(family);
689 ipaddress->prefixlen = prefixlen;
690 ipaddress->local = g_strdup(address);
692 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
693 check_duplicate_address)) {
694 connman_ipaddress_free(ipaddress);
698 if (family == AF_INET)
699 type = CONNMAN_IPCONFIG_TYPE_IPV4;
700 else if (family == AF_INET6)
701 type = CONNMAN_IPCONFIG_TYPE_IPV6;
705 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
708 ifname = connman_inet_ifname(index);
709 connman_info("%s {add} address %s/%u label %s family %d",
710 ifname, address, prefixlen, label, family);
712 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
713 __connman_ippool_newaddr(index, address, prefixlen);
715 if (ipdevice->config_ipv4 && family == AF_INET)
716 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
719 else if (ipdevice->config_ipv6 && family == AF_INET6)
720 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
725 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
728 for (list = g_list_first(ipconfig_list); list;
729 list = g_list_next(list)) {
730 struct connman_ipconfig *ipconfig = list->data;
732 if (index != ipconfig->index)
735 if (type != ipconfig->type)
741 if (ipconfig->ops->ip_bound)
742 ipconfig->ops->ip_bound(ipconfig, ifname);
750 void __connman_ipconfig_deladdr(int index, int family, const char *label,
751 unsigned char prefixlen, const char *address)
753 struct connman_ipdevice *ipdevice;
754 struct connman_ipaddress *ipaddress;
755 enum connman_ipconfig_type type;
759 DBG("index %d", index);
761 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
765 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
769 if (family == AF_INET)
770 type = CONNMAN_IPCONFIG_TYPE_IPV4;
771 else if (family == AF_INET6)
772 type = CONNMAN_IPCONFIG_TYPE_IPV6;
776 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
779 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
780 __connman_ippool_deladdr(index, address, prefixlen);
782 connman_ipaddress_clear(ipaddress);
785 ifname = connman_inet_ifname(index);
786 connman_info("%s {del} address %s/%u label %s", ifname,
787 address, prefixlen, label);
789 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
792 if (g_slist_length(ipdevice->address_list) > 0)
795 for (list = g_list_first(ipconfig_list); list;
796 list = g_list_next(list)) {
797 struct connman_ipconfig *ipconfig = list->data;
799 if (index != ipconfig->index)
802 if (type != ipconfig->type)
808 if (ipconfig->ops->ip_release)
809 ipconfig->ops->ip_release(ipconfig, ifname);
816 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
817 const char *dst, const char *gateway)
819 struct connman_ipdevice *ipdevice;
822 DBG("index %d", index);
824 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
828 ifname = connman_inet_ifname(index);
830 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
831 g_strcmp0(dst, "::") == 0)) {
833 enum connman_ipconfig_type type;
835 if (family == AF_INET6) {
836 type = CONNMAN_IPCONFIG_TYPE_IPV6;
837 g_free(ipdevice->ipv6_gateway);
838 ipdevice->ipv6_gateway = g_strdup(gateway);
840 if (ipdevice->config_ipv6 &&
841 ipdevice->config_ipv6->system) {
842 g_free(ipdevice->config_ipv6->system->gateway);
843 ipdevice->config_ipv6->system->gateway =
846 } else if (family == AF_INET) {
847 type = CONNMAN_IPCONFIG_TYPE_IPV4;
848 g_free(ipdevice->ipv4_gateway);
849 ipdevice->ipv4_gateway = g_strdup(gateway);
851 if (ipdevice->config_ipv4 &&
852 ipdevice->config_ipv4->system) {
853 g_free(ipdevice->config_ipv4->system->gateway);
854 ipdevice->config_ipv4->system->gateway =
860 for (config_list = g_list_first(ipconfig_list); config_list;
861 config_list = g_list_next(config_list)) {
862 struct connman_ipconfig *ipconfig = config_list->data;
864 if (index != ipconfig->index)
867 if (type != ipconfig->type)
873 if (ipconfig->ops->route_set)
874 ipconfig->ops->route_set(ipconfig, ifname);
878 connman_info("%s {add} route %s gw %s scope %u <%s>",
879 ifname, dst, gateway, scope, scope2str(scope));
885 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
886 const char *dst, const char *gateway)
888 struct connman_ipdevice *ipdevice;
891 DBG("index %d", index);
893 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
897 ifname = connman_inet_ifname(index);
899 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
900 g_strcmp0(dst, "::") == 0)) {
902 enum connman_ipconfig_type type;
904 if (family == AF_INET6) {
905 type = CONNMAN_IPCONFIG_TYPE_IPV6;
906 g_free(ipdevice->ipv6_gateway);
907 ipdevice->ipv6_gateway = NULL;
909 if (ipdevice->config_ipv6 &&
910 ipdevice->config_ipv6->system) {
911 g_free(ipdevice->config_ipv6->system->gateway);
912 ipdevice->config_ipv6->system->gateway = NULL;
914 } else if (family == AF_INET) {
915 type = CONNMAN_IPCONFIG_TYPE_IPV4;
916 g_free(ipdevice->ipv4_gateway);
917 ipdevice->ipv4_gateway = NULL;
919 if (ipdevice->config_ipv4 &&
920 ipdevice->config_ipv4->system) {
921 g_free(ipdevice->config_ipv4->system->gateway);
922 ipdevice->config_ipv4->system->gateway = NULL;
927 for (config_list = g_list_first(ipconfig_list); config_list;
928 config_list = g_list_next(config_list)) {
929 struct connman_ipconfig *ipconfig = config_list->data;
931 if (index != ipconfig->index)
934 if (type != ipconfig->type)
940 if (ipconfig->ops->route_unset)
941 ipconfig->ops->route_unset(ipconfig, ifname);
945 connman_info("%s {del} route %s gw %s scope %u <%s>",
946 ifname, dst, gateway, scope, scope2str(scope));
952 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
957 keys = g_hash_table_get_keys(ipdevice_hash);
961 for (list = g_list_first(keys); list; list = g_list_next(list)) {
962 int index = GPOINTER_TO_INT(list->data);
964 function(index, user_data);
970 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
971 struct connman_ipconfig *ipconfig)
973 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
976 unsigned short __connman_ipconfig_get_type_from_index(int index)
978 struct connman_ipdevice *ipdevice;
980 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
984 return ipdevice->type;
987 unsigned int __connman_ipconfig_get_flags_from_index(int index)
989 struct connman_ipdevice *ipdevice;
991 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
995 return ipdevice->flags;
998 const char *__connman_ipconfig_get_gateway_from_index(int index,
999 enum connman_ipconfig_type type)
1001 struct connman_ipdevice *ipdevice;
1003 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1007 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1008 if (ipdevice->ipv4_gateway)
1009 return ipdevice->ipv4_gateway;
1011 if (ipdevice->config_ipv4 &&
1012 ipdevice->config_ipv4->address)
1013 return ipdevice->config_ipv4->address->gateway;
1016 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1017 if (ipdevice->ipv6_gateway)
1018 return ipdevice->ipv6_gateway;
1020 if (ipdevice->config_ipv6 &&
1021 ipdevice->config_ipv6->address)
1022 return ipdevice->config_ipv6->address->gateway;
1028 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1030 ipconfig->index = index;
1033 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1035 if (!ipconfig->address)
1038 return ipconfig->address->local;
1041 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1042 const char *address)
1044 if (!ipconfig->address)
1047 g_free(ipconfig->address->local);
1048 ipconfig->address->local = g_strdup(address);
1051 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1053 if (!ipconfig->address)
1056 return ipconfig->address->peer;
1059 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1060 const char *address)
1062 if (!ipconfig->address)
1065 g_free(ipconfig->address->peer);
1066 ipconfig->address->peer = g_strdup(address);
1069 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1071 if (!ipconfig->address)
1074 return ipconfig->address->broadcast;
1077 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1078 const char *broadcast)
1080 if (!ipconfig->address)
1083 g_free(ipconfig->address->broadcast);
1084 ipconfig->address->broadcast = g_strdup(broadcast);
1087 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1089 if (!ipconfig->address)
1092 return ipconfig->address->gateway;
1095 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1096 const char *gateway)
1100 if (!ipconfig->address)
1102 g_free(ipconfig->address->gateway);
1103 ipconfig->address->gateway = g_strdup(gateway);
1106 #if defined TIZEN_EXT
1107 void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig,
1108 int dhcp_lease_duration)
1110 ipconfig->dhcp_lease_duration = dhcp_lease_duration;
1114 #if defined TIZEN_EXT
1115 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1117 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1120 #if !defined TIZEN_EXT
1121 struct connman_service *service;
1126 if (!ipconfig->address)
1129 #if !defined TIZEN_EXT
1130 service = __connman_service_lookup_from_index(ipconfig->index);
1135 DBG("type %d gw %s peer %s", ipconfig->type,
1136 ipconfig->address->gateway, ipconfig->address->peer);
1138 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1139 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1140 return __connman_connection_gateway_add(service,
1141 ipconfig->address->gateway,
1143 ipconfig->address->peer);
1148 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1150 struct connman_service *service;
1154 service = __connman_service_lookup_from_index(ipconfig->index);
1156 __connman_connection_gateway_remove(service, ipconfig->type);
1159 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1161 if (!ipconfig->address)
1164 return ipconfig->address->prefixlen;
1167 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1168 unsigned char prefixlen)
1170 if (!ipconfig->address)
1173 ipconfig->address->prefixlen = prefixlen;
1176 static struct connman_ipconfig *create_ipv6config(int index)
1178 struct connman_ipconfig *ipv6config;
1179 struct connman_ipdevice *ipdevice;
1181 DBG("index %d", index);
1183 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1187 ipv6config->refcount = 1;
1189 ipv6config->index = index;
1190 ipv6config->enabled = false;
1191 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1193 if (!is_ipv6_supported)
1194 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1196 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1198 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1200 #if !defined TIZEN_EXT
1201 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1203 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy = 2;
1205 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1206 if (!ipv6config->address) {
1211 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1213 DBG("ipconfig %p method %s", ipv6config,
1214 __connman_ipconfig_method2string(ipv6config->method));
1220 * connman_ipconfig_create:
1222 * Allocate a new ipconfig structure.
1224 * Returns: a newly-allocated #connman_ipconfig structure
1226 struct connman_ipconfig *__connman_ipconfig_create(int index,
1227 enum connman_ipconfig_type type)
1229 struct connman_ipconfig *ipconfig;
1231 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1232 return create_ipv6config(index);
1234 DBG("index %d", index);
1236 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1240 ipconfig->refcount = 1;
1242 ipconfig->index = index;
1243 ipconfig->enabled = false;
1244 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1246 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1247 if (!ipconfig->address) {
1252 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1254 DBG("ipconfig %p", ipconfig);
1261 * connman_ipconfig_ref:
1262 * @ipconfig: ipconfig structure
1264 * Increase reference counter of ipconfig
1266 struct connman_ipconfig *
1267 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1268 const char *file, int line, const char *caller)
1270 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1271 file, line, caller);
1273 __sync_fetch_and_add(&ipconfig->refcount, 1);
1279 * connman_ipconfig_unref:
1280 * @ipconfig: ipconfig structure
1282 * Decrease reference counter of ipconfig
1284 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1285 const char *file, int line, const char *caller)
1290 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1291 file, line, caller);
1293 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1296 if (__connman_ipconfig_disable(ipconfig) < 0)
1297 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1299 __connman_ipconfig_set_ops(ipconfig, NULL);
1301 connman_ipaddress_free(ipconfig->system);
1302 connman_ipaddress_free(ipconfig->address);
1303 g_free(ipconfig->last_dhcp_address);
1304 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1309 * connman_ipconfig_get_data:
1310 * @ipconfig: ipconfig structure
1312 * Get private data pointer
1314 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1319 return ipconfig->ops_data;
1323 * connman_ipconfig_set_data:
1324 * @ipconfig: ipconfig structure
1325 * @data: data pointer
1327 * Set private data pointer
1329 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1331 ipconfig->ops_data = data;
1335 * connman_ipconfig_get_index:
1336 * @ipconfig: ipconfig structure
1338 * Get interface index
1340 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1345 return ipconfig->index;
1349 * connman_ipconfig_set_ops:
1350 * @ipconfig: ipconfig structure
1351 * @ops: operation callbacks
1353 * Set the operation callbacks
1355 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1356 const struct connman_ipconfig_ops *ops)
1358 ipconfig->ops = ops;
1362 * connman_ipconfig_set_method:
1363 * @ipconfig: ipconfig structure
1364 * @method: configuration method
1366 * Set the configuration method
1368 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1369 enum connman_ipconfig_method method)
1371 ipconfig->method = method;
1376 enum connman_ipconfig_method __connman_ipconfig_get_method(
1377 struct connman_ipconfig *ipconfig)
1380 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1382 return ipconfig->method;
1385 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1389 switch (ipconfig->method) {
1390 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1391 case CONNMAN_IPCONFIG_METHOD_OFF:
1393 case CONNMAN_IPCONFIG_METHOD_AUTO:
1394 case CONNMAN_IPCONFIG_METHOD_FIXED:
1395 case CONNMAN_IPCONFIG_METHOD_DHCP:
1396 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1397 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1398 return connman_inet_set_address(ipconfig->index,
1400 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1401 return connman_inet_set_ipv6_address(
1402 ipconfig->index, ipconfig->address);
1408 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1417 DBG("method %d", ipconfig->method);
1419 switch (ipconfig->method) {
1420 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1421 case CONNMAN_IPCONFIG_METHOD_OFF:
1423 case CONNMAN_IPCONFIG_METHOD_AUTO:
1424 case CONNMAN_IPCONFIG_METHOD_FIXED:
1425 case CONNMAN_IPCONFIG_METHOD_DHCP:
1426 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1427 err = __connman_ipconfig_address_unset(ipconfig);
1428 connman_ipaddress_clear(ipconfig->address);
1436 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1445 DBG("method %d", ipconfig->method);
1447 switch (ipconfig->method) {
1448 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1449 case CONNMAN_IPCONFIG_METHOD_OFF:
1451 case CONNMAN_IPCONFIG_METHOD_AUTO:
1452 case CONNMAN_IPCONFIG_METHOD_FIXED:
1453 case CONNMAN_IPCONFIG_METHOD_DHCP:
1454 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1455 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1456 err = connman_inet_clear_address(ipconfig->index,
1458 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1459 err = connman_inet_clear_ipv6_address(
1461 ipconfig->address->local,
1462 ipconfig->address->prefixlen);
1472 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1475 struct connman_ipdevice *ipdevice;
1477 DBG("ipconfig %p", ipconfig);
1479 if (!ipconfig || ipconfig->index < 0)
1482 ipdevice = g_hash_table_lookup(ipdevice_hash,
1483 GINT_TO_POINTER(ipconfig->index));
1487 g_free(ipdevice->pac);
1488 ipdevice->pac = g_strdup(url);
1493 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1495 struct connman_ipdevice *ipdevice;
1497 DBG("ipconfig %p", ipconfig);
1499 if (!ipconfig || ipconfig->index < 0)
1502 ipdevice = g_hash_table_lookup(ipdevice_hash,
1503 GINT_TO_POINTER(ipconfig->index));
1507 return ipdevice->pac;
1510 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1511 const char *address)
1516 g_free(ipconfig->last_dhcp_address);
1517 ipconfig->last_dhcp_address = g_strdup(address);
1520 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1525 return ipconfig->last_dhcp_address;
1528 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1534 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1535 ipconfig->last_dhcpv6_prefixes = prefixes;
1538 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1543 return ipconfig->last_dhcpv6_prefixes;
1546 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1548 struct connman_ipdevice *ipdevice;
1553 ipdevice = g_hash_table_lookup(ipdevice_hash,
1554 GINT_TO_POINTER(ipconfig->index));
1558 ifname = connman_inet_ifname(ipconfig->index);
1560 set_ipv6_state(ifname, false);
1565 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1567 struct connman_ipdevice *ipdevice;
1572 ipdevice = g_hash_table_lookup(ipdevice_hash,
1573 GINT_TO_POINTER(ipconfig->index));
1577 ifname = connman_inet_ifname(ipconfig->index);
1579 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1580 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1582 set_ipv6_state(ifname, true);
1587 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1589 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1592 enable_ipv6(ipconfig);
1595 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1597 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1600 disable_ipv6(ipconfig);
1603 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1608 switch (ipconfig->method) {
1609 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1610 case CONNMAN_IPCONFIG_METHOD_OFF:
1612 case CONNMAN_IPCONFIG_METHOD_AUTO:
1613 case CONNMAN_IPCONFIG_METHOD_FIXED:
1614 case CONNMAN_IPCONFIG_METHOD_DHCP:
1615 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1622 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1624 struct connman_ipdevice *ipdevice;
1625 bool up = false, down = false;
1626 bool lower_up = false, lower_down = false;
1627 enum connman_ipconfig_type type;
1630 DBG("ipconfig %p", ipconfig);
1632 if (!ipconfig || ipconfig->index < 0)
1635 ipdevice = g_hash_table_lookup(ipdevice_hash,
1636 GINT_TO_POINTER(ipconfig->index));
1640 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1641 if (ipdevice->config_ipv4 == ipconfig)
1643 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1644 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1645 if (ipdevice->config_ipv6 == ipconfig)
1647 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1651 ipconfig->enabled = true;
1653 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1654 ipdevice->config_ipv4) {
1655 ipconfig_list = g_list_remove(ipconfig_list,
1656 ipdevice->config_ipv4);
1658 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1660 __connman_ipconfig_unref(ipdevice->config_ipv4);
1663 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1664 ipdevice->config_ipv6) {
1665 ipconfig_list = g_list_remove(ipconfig_list,
1666 ipdevice->config_ipv6);
1668 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1670 __connman_ipconfig_unref(ipdevice->config_ipv6);
1673 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1674 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1675 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1676 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1678 enable_ipv6(ipdevice->config_ipv6);
1680 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1682 if (ipdevice->flags & IFF_UP)
1687 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1688 (IFF_RUNNING | IFF_LOWER_UP))
1690 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1693 ifname = connman_inet_ifname(ipconfig->index);
1695 if (up && ipconfig->ops->up)
1696 ipconfig->ops->up(ipconfig, ifname);
1697 if (lower_up && ipconfig->ops->lower_up)
1698 ipconfig->ops->lower_up(ipconfig, ifname);
1700 if (lower_down && ipconfig->ops->lower_down)
1701 ipconfig->ops->lower_down(ipconfig, ifname);
1702 if (down && ipconfig->ops->down)
1703 ipconfig->ops->down(ipconfig, ifname);
1710 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1712 struct connman_ipdevice *ipdevice;
1714 DBG("ipconfig %p", ipconfig);
1716 if (!ipconfig || ipconfig->index < 0)
1719 ipdevice = g_hash_table_lookup(ipdevice_hash,
1720 GINT_TO_POINTER(ipconfig->index));
1724 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1727 ipconfig->enabled = false;
1729 if (ipdevice->config_ipv4 == ipconfig) {
1730 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1732 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1733 __connman_ipconfig_unref(ipdevice->config_ipv4);
1734 ipdevice->config_ipv4 = NULL;
1738 if (ipdevice->config_ipv6 == ipconfig) {
1739 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1741 #if defined TIZEN_EXT
1742 if (ipdevice->config_ipv6->method ==
1743 CONNMAN_IPCONFIG_METHOD_AUTO)
1744 disable_ipv6(ipdevice->config_ipv6);
1746 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1747 __connman_ipconfig_unref(ipdevice->config_ipv6);
1748 ipdevice->config_ipv6 = NULL;
1755 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1758 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1760 case CONNMAN_IPCONFIG_METHOD_OFF:
1762 case CONNMAN_IPCONFIG_METHOD_FIXED:
1764 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1766 case CONNMAN_IPCONFIG_METHOD_DHCP:
1768 case CONNMAN_IPCONFIG_METHOD_AUTO:
1775 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1777 if (g_strcmp0(method, "off") == 0)
1778 return CONNMAN_IPCONFIG_METHOD_OFF;
1779 else if (g_strcmp0(method, "fixed") == 0)
1780 return CONNMAN_IPCONFIG_METHOD_FIXED;
1781 else if (g_strcmp0(method, "manual") == 0)
1782 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1783 else if (g_strcmp0(method, "dhcp") == 0)
1784 return CONNMAN_IPCONFIG_METHOD_DHCP;
1785 else if (g_strcmp0(method, "auto") == 0)
1786 return CONNMAN_IPCONFIG_METHOD_AUTO;
1788 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1791 static const char *privacy2string(int privacy)
1795 else if (privacy == 1)
1801 static int string2privacy(const char *privacy)
1803 if (g_strcmp0(privacy, "disabled") == 0)
1805 else if (g_strcmp0(privacy, "enabled") == 0)
1807 else if (g_strcmp0(privacy, "preferred") == 0)
1809 else if (g_strcmp0(privacy, "prefered") == 0)
1815 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1817 struct connman_ipdevice *ipdevice;
1823 ipdevice = g_hash_table_lookup(ipdevice_hash,
1824 GINT_TO_POINTER(ipconfig->index));
1828 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1829 ipdevice->ipv6_privacy));
1834 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1842 DBG("ipconfig %p privacy %s", ipconfig, value);
1844 privacy = string2privacy(value);
1846 ipconfig->ipv6_privacy_config = privacy;
1848 enable_ipv6(ipconfig);
1853 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1854 DBusMessageIter *iter)
1856 struct connman_ipaddress *append_addr = NULL;
1858 #if defined TIZEN_EXT
1862 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1865 str = __connman_ipconfig_method2string(ipconfig->method);
1869 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1871 switch (ipconfig->method) {
1872 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1873 case CONNMAN_IPCONFIG_METHOD_OFF:
1874 case CONNMAN_IPCONFIG_METHOD_AUTO:
1877 case CONNMAN_IPCONFIG_METHOD_FIXED:
1878 append_addr = ipconfig->address;
1881 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1882 case CONNMAN_IPCONFIG_METHOD_DHCP:
1883 append_addr = ipconfig->system;
1884 #if defined TIZEN_EXT
1885 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1886 if (append_addr && append_addr->local == NULL)
1887 append_addr = ipconfig->address;
1895 if (append_addr->local) {
1897 struct in_addr netmask;
1900 connman_dbus_dict_append_basic(iter, "Address",
1901 DBUS_TYPE_STRING, &append_addr->local);
1903 addr = 0xffffffff << (32 - append_addr->prefixlen);
1904 netmask.s_addr = htonl(addr);
1905 mask = inet_ntoa(netmask);
1906 connman_dbus_dict_append_basic(iter, "Netmask",
1907 DBUS_TYPE_STRING, &mask);
1910 if (append_addr->gateway)
1911 connman_dbus_dict_append_basic(iter, "Gateway",
1912 DBUS_TYPE_STRING, &append_addr->gateway);
1914 #if defined TIZEN_EXT
1915 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1917 server_ip = __connman_dhcp_get_server_address(ipconfig);
1919 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1920 DBUS_TYPE_STRING, &server_ip);
1923 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1924 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1929 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1930 DBusMessageIter *iter,
1931 struct connman_ipconfig *ipconfig_ipv4)
1933 struct connman_ipaddress *append_addr = NULL;
1934 const char *str, *privacy;
1935 #if defined TIZEN_EXT
1939 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1942 str = __connman_ipconfig_method2string(ipconfig->method);
1946 if (ipconfig_ipv4 &&
1947 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1948 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1952 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1954 switch (ipconfig->method) {
1955 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1956 case CONNMAN_IPCONFIG_METHOD_OFF:
1959 case CONNMAN_IPCONFIG_METHOD_FIXED:
1960 append_addr = ipconfig->address;
1963 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1964 case CONNMAN_IPCONFIG_METHOD_DHCP:
1965 case CONNMAN_IPCONFIG_METHOD_AUTO:
1966 append_addr = ipconfig->system;
1967 #if defined TIZEN_EXT
1968 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1969 if (append_addr && append_addr->local == NULL)
1970 append_addr = ipconfig->address;
1978 if (append_addr->local) {
1979 connman_dbus_dict_append_basic(iter, "Address",
1980 DBUS_TYPE_STRING, &append_addr->local);
1981 connman_dbus_dict_append_basic(iter, "PrefixLength",
1983 &append_addr->prefixlen);
1986 if (append_addr->gateway)
1987 connman_dbus_dict_append_basic(iter, "Gateway",
1988 DBUS_TYPE_STRING, &append_addr->gateway);
1990 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1991 connman_dbus_dict_append_basic(iter, "Privacy",
1992 DBUS_TYPE_STRING, &privacy);
1995 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1996 DBusMessageIter *iter)
1998 const char *str, *privacy;
1999 #if defined TIZEN_EXT
2003 str = __connman_ipconfig_method2string(ipconfig->method);
2007 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2009 switch (ipconfig->method) {
2010 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2011 case CONNMAN_IPCONFIG_METHOD_OFF:
2012 case CONNMAN_IPCONFIG_METHOD_DHCP:
2014 case CONNMAN_IPCONFIG_METHOD_FIXED:
2015 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2016 case CONNMAN_IPCONFIG_METHOD_AUTO:
2020 if (!ipconfig->address)
2023 if (ipconfig->address->local) {
2024 connman_dbus_dict_append_basic(iter, "Address",
2025 DBUS_TYPE_STRING, &ipconfig->address->local);
2026 connman_dbus_dict_append_basic(iter, "PrefixLength",
2028 &ipconfig->address->prefixlen);
2031 if (ipconfig->address->gateway)
2032 connman_dbus_dict_append_basic(iter, "Gateway",
2033 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2035 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2036 connman_dbus_dict_append_basic(iter, "Privacy",
2037 DBUS_TYPE_STRING, &privacy);
2040 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2041 DBusMessageIter *iter)
2044 #if defined TIZEN_EXT
2048 str = __connman_ipconfig_method2string(ipconfig->method);
2052 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2054 switch (ipconfig->method) {
2055 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2056 case CONNMAN_IPCONFIG_METHOD_OFF:
2057 case CONNMAN_IPCONFIG_METHOD_DHCP:
2058 case CONNMAN_IPCONFIG_METHOD_AUTO:
2060 case CONNMAN_IPCONFIG_METHOD_FIXED:
2061 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2065 if (!ipconfig->address)
2068 if (ipconfig->address->local) {
2070 struct in_addr netmask;
2073 connman_dbus_dict_append_basic(iter, "Address",
2074 DBUS_TYPE_STRING, &ipconfig->address->local);
2076 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2077 netmask.s_addr = htonl(addr);
2078 mask = inet_ntoa(netmask);
2079 connman_dbus_dict_append_basic(iter, "Netmask",
2080 DBUS_TYPE_STRING, &mask);
2083 if (ipconfig->address->gateway)
2084 connman_dbus_dict_append_basic(iter, "Gateway",
2085 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2088 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2089 DBusMessageIter *array)
2091 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2092 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2093 *privacy_string = NULL;
2094 int prefix_length = 0, privacy = 0;
2095 DBusMessageIter dict;
2098 DBG("ipconfig %p", ipconfig);
2100 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2103 dbus_message_iter_recurse(array, &dict);
2105 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2106 DBusMessageIter entry, value;
2110 dbus_message_iter_recurse(&dict, &entry);
2112 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2115 dbus_message_iter_get_basic(&entry, &key);
2116 dbus_message_iter_next(&entry);
2118 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2121 dbus_message_iter_recurse(&entry, &value);
2123 type = dbus_message_iter_get_arg_type(&value);
2125 if (g_str_equal(key, "Method")) {
2128 if (type != DBUS_TYPE_STRING)
2131 dbus_message_iter_get_basic(&value, &str);
2132 method = __connman_ipconfig_string2method(str);
2133 } else if (g_str_equal(key, "Address")) {
2134 if (type != DBUS_TYPE_STRING)
2137 dbus_message_iter_get_basic(&value, &address);
2138 } else if (g_str_equal(key, "PrefixLength")) {
2139 if (type != DBUS_TYPE_BYTE)
2142 dbus_message_iter_get_basic(&value, &prefix_length);
2144 if (prefix_length < 0 || prefix_length > 128)
2146 } else if (g_str_equal(key, "Netmask")) {
2147 if (type != DBUS_TYPE_STRING)
2150 dbus_message_iter_get_basic(&value, &netmask);
2151 } else if (g_str_equal(key, "Gateway")) {
2152 if (type != DBUS_TYPE_STRING)
2155 dbus_message_iter_get_basic(&value, &gateway);
2156 } else if (g_str_equal(key, "Privacy")) {
2157 if (type != DBUS_TYPE_STRING)
2160 dbus_message_iter_get_basic(&value, &privacy_string);
2161 privacy = string2privacy(privacy_string);
2164 dbus_message_iter_next(&dict);
2167 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2169 method, address, netmask, gateway, prefix_length,
2173 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2174 case CONNMAN_IPCONFIG_METHOD_FIXED:
2177 case CONNMAN_IPCONFIG_METHOD_OFF:
2178 ipconfig->method = method;
2179 #if defined TIZEN_EXT
2180 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2181 disable_ipv6(ipconfig);
2185 case CONNMAN_IPCONFIG_METHOD_AUTO:
2186 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2189 ipconfig->method = method;
2191 ipconfig->ipv6_privacy_config = privacy;
2192 #if defined TIZEN_EXT
2193 enable_ipv6(ipconfig);
2197 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2198 switch (ipconfig->type) {
2199 case CONNMAN_IPCONFIG_TYPE_IPV4:
2202 case CONNMAN_IPCONFIG_TYPE_IPV6:
2205 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2206 case CONNMAN_IPCONFIG_TYPE_ALL:
2211 if ((address && connman_inet_check_ipaddress(address)
2214 connman_inet_check_ipaddress(netmask)
2217 connman_inet_check_ipaddress(gateway)
2221 ipconfig->method = method;
2223 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2224 connman_ipaddress_set_ipv4(ipconfig->address,
2225 address, netmask, gateway);
2227 return connman_ipaddress_set_ipv6(
2228 ipconfig->address, address,
2229 prefix_length, gateway);
2233 case CONNMAN_IPCONFIG_METHOD_DHCP:
2234 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2237 ipconfig->method = method;
2244 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2245 DBusMessageIter *iter)
2247 struct connman_ipdevice *ipdevice;
2248 const char *method = "auto";
2250 connman_dbus_dict_append_basic(iter, "Method",
2251 DBUS_TYPE_STRING, &method);
2253 ipdevice = g_hash_table_lookup(ipdevice_hash,
2254 GINT_TO_POINTER(ipconfig->index));
2258 if (ipconfig->index >= 0) {
2259 char *ifname = connman_inet_ifname(ipconfig->index);
2261 connman_dbus_dict_append_basic(iter, "Interface",
2262 DBUS_TYPE_STRING, &ifname);
2267 if (ipdevice->address)
2268 connman_dbus_dict_append_basic(iter, "Address",
2269 DBUS_TYPE_STRING, &ipdevice->address);
2271 if (ipdevice->mtu > 0)
2272 connman_dbus_dict_append_basic(iter, "MTU",
2273 DBUS_TYPE_UINT16, &ipdevice->mtu);
2276 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2277 GKeyFile *keyfile, const char *identifier, const char *prefix)
2283 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2285 key = g_strdup_printf("%smethod", prefix);
2286 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2288 switch (ipconfig->type) {
2289 case CONNMAN_IPCONFIG_TYPE_IPV4:
2290 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2292 case CONNMAN_IPCONFIG_TYPE_IPV6:
2293 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2295 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2296 case CONNMAN_IPCONFIG_TYPE_ALL:
2297 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2301 ipconfig->method = __connman_ipconfig_string2method(method);
2303 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2304 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2306 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2310 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2311 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2314 pprefix = g_strdup_printf("%sprivacy", prefix);
2315 privacy = g_key_file_get_string(keyfile, identifier,
2317 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2322 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2323 ipconfig->last_dhcpv6_prefixes =
2324 g_key_file_get_string_list(keyfile, identifier, pprefix,
2326 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2327 g_free(ipconfig->last_dhcpv6_prefixes);
2328 ipconfig->last_dhcpv6_prefixes = NULL;
2336 switch (ipconfig->method) {
2337 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2338 case CONNMAN_IPCONFIG_METHOD_OFF:
2341 case CONNMAN_IPCONFIG_METHOD_FIXED:
2342 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2344 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2345 ipconfig->address->prefixlen = g_key_file_get_integer(
2346 keyfile, identifier, key, NULL);
2349 key = g_strdup_printf("%slocal_address", prefix);
2350 g_free(ipconfig->address->local);
2351 ipconfig->address->local = g_key_file_get_string(
2352 keyfile, identifier, key, NULL);
2355 key = g_strdup_printf("%speer_address", prefix);
2356 g_free(ipconfig->address->peer);
2357 ipconfig->address->peer = g_key_file_get_string(
2358 keyfile, identifier, key, NULL);
2361 key = g_strdup_printf("%sbroadcast_address", prefix);
2362 g_free(ipconfig->address->broadcast);
2363 ipconfig->address->broadcast = g_key_file_get_string(
2364 keyfile, identifier, key, NULL);
2367 key = g_strdup_printf("%sgateway", prefix);
2368 g_free(ipconfig->address->gateway);
2369 ipconfig->address->gateway = g_key_file_get_string(
2370 keyfile, identifier, key, NULL);
2374 case CONNMAN_IPCONFIG_METHOD_DHCP:
2376 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2377 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2379 g_free(ipconfig->last_dhcp_address);
2380 ipconfig->last_dhcp_address = str;
2386 case CONNMAN_IPCONFIG_METHOD_AUTO:
2393 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2394 GKeyFile *keyfile, const char *identifier, const char *prefix)
2399 method = __connman_ipconfig_method2string(ipconfig->method);
2401 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2404 key = g_strdup_printf("%smethod", prefix);
2405 g_key_file_set_string(keyfile, identifier, key, method);
2409 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2410 const char *privacy;
2411 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2412 key = g_strdup_printf("%sprivacy", prefix);
2413 g_key_file_set_string(keyfile, identifier, key, privacy);
2416 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2417 if (ipconfig->last_dhcp_address &&
2418 strlen(ipconfig->last_dhcp_address) > 0)
2419 g_key_file_set_string(keyfile, identifier, key,
2420 ipconfig->last_dhcp_address);
2422 g_key_file_remove_key(keyfile, identifier, key, NULL);
2425 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2426 if (ipconfig->last_dhcpv6_prefixes &&
2427 ipconfig->last_dhcpv6_prefixes[0]) {
2429 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2431 g_key_file_set_string_list(keyfile, identifier, key,
2432 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2435 g_key_file_remove_key(keyfile, identifier, key, NULL);
2439 switch (ipconfig->method) {
2440 case CONNMAN_IPCONFIG_METHOD_FIXED:
2441 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2443 case CONNMAN_IPCONFIG_METHOD_DHCP:
2444 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2445 if (ipconfig->last_dhcp_address &&
2446 strlen(ipconfig->last_dhcp_address) > 0)
2447 g_key_file_set_string(keyfile, identifier, key,
2448 ipconfig->last_dhcp_address);
2450 g_key_file_remove_key(keyfile, identifier, key, NULL);
2453 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2454 case CONNMAN_IPCONFIG_METHOD_OFF:
2455 case CONNMAN_IPCONFIG_METHOD_AUTO:
2459 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2460 if (ipconfig->address->prefixlen != 0)
2461 g_key_file_set_integer(keyfile, identifier,
2462 key, ipconfig->address->prefixlen);
2465 key = g_strdup_printf("%slocal_address", prefix);
2466 if (ipconfig->address->local)
2467 g_key_file_set_string(keyfile, identifier,
2468 key, ipconfig->address->local);
2471 key = g_strdup_printf("%speer_address", prefix);
2472 if (ipconfig->address->peer)
2473 g_key_file_set_string(keyfile, identifier,
2474 key, ipconfig->address->peer);
2477 key = g_strdup_printf("%sbroadcast_address", prefix);
2478 if (ipconfig->address->broadcast)
2479 g_key_file_set_string(keyfile, identifier,
2480 key, ipconfig->address->broadcast);
2483 key = g_strdup_printf("%sgateway", prefix);
2484 if (ipconfig->address->gateway)
2485 g_key_file_set_string(keyfile, identifier,
2486 key, ipconfig->address->gateway);
2492 int __connman_ipconfig_init(void)
2496 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2497 NULL, free_ipdevice);
2499 is_ipv6_supported = connman_inet_is_ipv6_supported();
2504 void __connman_ipconfig_cleanup(void)
2508 g_hash_table_destroy(ipdevice_hash);
2509 ipdevice_hash = NULL;