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 #if defined TIZEN_EXT
1446 DBG("ipconfig method %d type %d", ipconfig->method, ipconfig->type);
1448 DBG("method %d", ipconfig->method);
1451 switch (ipconfig->method) {
1452 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1453 case CONNMAN_IPCONFIG_METHOD_OFF:
1455 case CONNMAN_IPCONFIG_METHOD_AUTO:
1456 case CONNMAN_IPCONFIG_METHOD_FIXED:
1457 case CONNMAN_IPCONFIG_METHOD_DHCP:
1458 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1459 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1460 err = connman_inet_clear_address(ipconfig->index,
1462 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1463 err = connman_inet_clear_ipv6_address(
1465 ipconfig->address->local,
1466 ipconfig->address->prefixlen);
1476 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1479 struct connman_ipdevice *ipdevice;
1481 DBG("ipconfig %p", ipconfig);
1483 if (!ipconfig || ipconfig->index < 0)
1486 ipdevice = g_hash_table_lookup(ipdevice_hash,
1487 GINT_TO_POINTER(ipconfig->index));
1491 g_free(ipdevice->pac);
1492 ipdevice->pac = g_strdup(url);
1497 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1499 struct connman_ipdevice *ipdevice;
1501 DBG("ipconfig %p", ipconfig);
1503 if (!ipconfig || ipconfig->index < 0)
1506 ipdevice = g_hash_table_lookup(ipdevice_hash,
1507 GINT_TO_POINTER(ipconfig->index));
1511 return ipdevice->pac;
1514 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1515 const char *address)
1520 g_free(ipconfig->last_dhcp_address);
1521 ipconfig->last_dhcp_address = g_strdup(address);
1524 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1529 return ipconfig->last_dhcp_address;
1532 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1538 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1539 ipconfig->last_dhcpv6_prefixes = prefixes;
1542 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1547 return ipconfig->last_dhcpv6_prefixes;
1550 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1552 struct connman_ipdevice *ipdevice;
1557 ipdevice = g_hash_table_lookup(ipdevice_hash,
1558 GINT_TO_POINTER(ipconfig->index));
1562 ifname = connman_inet_ifname(ipconfig->index);
1564 set_ipv6_state(ifname, false);
1569 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1571 struct connman_ipdevice *ipdevice;
1576 ipdevice = g_hash_table_lookup(ipdevice_hash,
1577 GINT_TO_POINTER(ipconfig->index));
1581 ifname = connman_inet_ifname(ipconfig->index);
1583 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1584 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1586 set_ipv6_state(ifname, true);
1591 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1593 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1596 enable_ipv6(ipconfig);
1599 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1601 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1604 disable_ipv6(ipconfig);
1607 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1612 switch (ipconfig->method) {
1613 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1614 case CONNMAN_IPCONFIG_METHOD_OFF:
1616 case CONNMAN_IPCONFIG_METHOD_AUTO:
1617 case CONNMAN_IPCONFIG_METHOD_FIXED:
1618 case CONNMAN_IPCONFIG_METHOD_DHCP:
1619 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1626 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1628 struct connman_ipdevice *ipdevice;
1629 bool up = false, down = false;
1630 bool lower_up = false, lower_down = false;
1631 enum connman_ipconfig_type type;
1634 DBG("ipconfig %p", ipconfig);
1636 if (!ipconfig || ipconfig->index < 0)
1639 ipdevice = g_hash_table_lookup(ipdevice_hash,
1640 GINT_TO_POINTER(ipconfig->index));
1644 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1645 if (ipdevice->config_ipv4 == ipconfig)
1647 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1648 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1649 if (ipdevice->config_ipv6 == ipconfig)
1651 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1655 ipconfig->enabled = true;
1657 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1658 ipdevice->config_ipv4) {
1659 ipconfig_list = g_list_remove(ipconfig_list,
1660 ipdevice->config_ipv4);
1662 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1664 __connman_ipconfig_unref(ipdevice->config_ipv4);
1667 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1668 ipdevice->config_ipv6) {
1669 ipconfig_list = g_list_remove(ipconfig_list,
1670 ipdevice->config_ipv6);
1672 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1674 __connman_ipconfig_unref(ipdevice->config_ipv6);
1677 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1678 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1679 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1680 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1682 enable_ipv6(ipdevice->config_ipv6);
1684 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1686 if (ipdevice->flags & IFF_UP)
1691 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1692 (IFF_RUNNING | IFF_LOWER_UP))
1694 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1697 ifname = connman_inet_ifname(ipconfig->index);
1699 if (up && ipconfig->ops->up)
1700 ipconfig->ops->up(ipconfig, ifname);
1701 if (lower_up && ipconfig->ops->lower_up)
1702 ipconfig->ops->lower_up(ipconfig, ifname);
1704 if (lower_down && ipconfig->ops->lower_down)
1705 ipconfig->ops->lower_down(ipconfig, ifname);
1706 if (down && ipconfig->ops->down)
1707 ipconfig->ops->down(ipconfig, ifname);
1714 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1716 struct connman_ipdevice *ipdevice;
1718 DBG("ipconfig %p", ipconfig);
1720 if (!ipconfig || ipconfig->index < 0)
1723 ipdevice = g_hash_table_lookup(ipdevice_hash,
1724 GINT_TO_POINTER(ipconfig->index));
1728 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1731 ipconfig->enabled = false;
1733 if (ipdevice->config_ipv4 == ipconfig) {
1734 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1736 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1737 __connman_ipconfig_unref(ipdevice->config_ipv4);
1738 ipdevice->config_ipv4 = NULL;
1742 if (ipdevice->config_ipv6 == ipconfig) {
1743 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1745 #if defined TIZEN_EXT
1746 if (ipdevice->config_ipv6->method ==
1747 CONNMAN_IPCONFIG_METHOD_AUTO)
1748 disable_ipv6(ipdevice->config_ipv6);
1750 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1751 __connman_ipconfig_unref(ipdevice->config_ipv6);
1752 ipdevice->config_ipv6 = NULL;
1759 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1762 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1764 case CONNMAN_IPCONFIG_METHOD_OFF:
1766 case CONNMAN_IPCONFIG_METHOD_FIXED:
1768 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1770 case CONNMAN_IPCONFIG_METHOD_DHCP:
1772 case CONNMAN_IPCONFIG_METHOD_AUTO:
1779 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1781 if (g_strcmp0(method, "off") == 0)
1782 return CONNMAN_IPCONFIG_METHOD_OFF;
1783 else if (g_strcmp0(method, "fixed") == 0)
1784 return CONNMAN_IPCONFIG_METHOD_FIXED;
1785 else if (g_strcmp0(method, "manual") == 0)
1786 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1787 else if (g_strcmp0(method, "dhcp") == 0)
1788 return CONNMAN_IPCONFIG_METHOD_DHCP;
1789 else if (g_strcmp0(method, "auto") == 0)
1790 return CONNMAN_IPCONFIG_METHOD_AUTO;
1792 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1795 static const char *privacy2string(int privacy)
1799 else if (privacy == 1)
1805 static int string2privacy(const char *privacy)
1807 if (g_strcmp0(privacy, "disabled") == 0)
1809 else if (g_strcmp0(privacy, "enabled") == 0)
1811 else if (g_strcmp0(privacy, "preferred") == 0)
1813 else if (g_strcmp0(privacy, "prefered") == 0)
1819 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1821 struct connman_ipdevice *ipdevice;
1827 ipdevice = g_hash_table_lookup(ipdevice_hash,
1828 GINT_TO_POINTER(ipconfig->index));
1832 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1833 ipdevice->ipv6_privacy));
1838 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1846 DBG("ipconfig %p privacy %s", ipconfig, value);
1848 privacy = string2privacy(value);
1850 ipconfig->ipv6_privacy_config = privacy;
1852 enable_ipv6(ipconfig);
1857 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1858 DBusMessageIter *iter)
1860 struct connman_ipaddress *append_addr = NULL;
1862 #if defined TIZEN_EXT
1866 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1869 str = __connman_ipconfig_method2string(ipconfig->method);
1873 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1875 switch (ipconfig->method) {
1876 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1877 case CONNMAN_IPCONFIG_METHOD_OFF:
1878 case CONNMAN_IPCONFIG_METHOD_AUTO:
1881 case CONNMAN_IPCONFIG_METHOD_FIXED:
1882 append_addr = ipconfig->address;
1885 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1886 case CONNMAN_IPCONFIG_METHOD_DHCP:
1887 append_addr = ipconfig->system;
1888 #if defined TIZEN_EXT
1889 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1890 if (append_addr && append_addr->local == NULL)
1891 append_addr = ipconfig->address;
1899 if (append_addr->local) {
1901 struct in_addr netmask;
1904 connman_dbus_dict_append_basic(iter, "Address",
1905 DBUS_TYPE_STRING, &append_addr->local);
1907 addr = 0xffffffff << (32 - append_addr->prefixlen);
1908 netmask.s_addr = htonl(addr);
1909 mask = inet_ntoa(netmask);
1910 connman_dbus_dict_append_basic(iter, "Netmask",
1911 DBUS_TYPE_STRING, &mask);
1914 if (append_addr->gateway)
1915 connman_dbus_dict_append_basic(iter, "Gateway",
1916 DBUS_TYPE_STRING, &append_addr->gateway);
1918 #if defined TIZEN_EXT
1919 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1921 server_ip = __connman_dhcp_get_server_address(ipconfig);
1923 connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1924 DBUS_TYPE_STRING, &server_ip);
1927 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1928 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1933 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1934 DBusMessageIter *iter,
1935 struct connman_ipconfig *ipconfig_ipv4)
1937 struct connman_ipaddress *append_addr = NULL;
1938 const char *str, *privacy;
1939 #if defined TIZEN_EXT
1943 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1946 str = __connman_ipconfig_method2string(ipconfig->method);
1950 if (ipconfig_ipv4 &&
1951 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1952 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1956 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1958 switch (ipconfig->method) {
1959 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1960 case CONNMAN_IPCONFIG_METHOD_OFF:
1963 case CONNMAN_IPCONFIG_METHOD_FIXED:
1964 append_addr = ipconfig->address;
1967 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1968 case CONNMAN_IPCONFIG_METHOD_DHCP:
1969 case CONNMAN_IPCONFIG_METHOD_AUTO:
1970 append_addr = ipconfig->system;
1971 #if defined TIZEN_EXT
1972 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1973 if (append_addr && append_addr->local == NULL)
1974 append_addr = ipconfig->address;
1982 if (append_addr->local) {
1983 connman_dbus_dict_append_basic(iter, "Address",
1984 DBUS_TYPE_STRING, &append_addr->local);
1985 connman_dbus_dict_append_basic(iter, "PrefixLength",
1987 &append_addr->prefixlen);
1990 if (append_addr->gateway)
1991 connman_dbus_dict_append_basic(iter, "Gateway",
1992 DBUS_TYPE_STRING, &append_addr->gateway);
1994 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1995 connman_dbus_dict_append_basic(iter, "Privacy",
1996 DBUS_TYPE_STRING, &privacy);
1999 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
2000 DBusMessageIter *iter)
2002 const char *str, *privacy;
2003 #if !defined TIZEN_EXT
2007 str = __connman_ipconfig_method2string(ipconfig->method);
2011 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2013 switch (ipconfig->method) {
2014 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2015 case CONNMAN_IPCONFIG_METHOD_OFF:
2016 case CONNMAN_IPCONFIG_METHOD_DHCP:
2018 case CONNMAN_IPCONFIG_METHOD_FIXED:
2019 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2020 case CONNMAN_IPCONFIG_METHOD_AUTO:
2024 if (!ipconfig->address)
2027 if (ipconfig->address->local) {
2028 connman_dbus_dict_append_basic(iter, "Address",
2029 DBUS_TYPE_STRING, &ipconfig->address->local);
2030 connman_dbus_dict_append_basic(iter, "PrefixLength",
2032 &ipconfig->address->prefixlen);
2035 if (ipconfig->address->gateway)
2036 connman_dbus_dict_append_basic(iter, "Gateway",
2037 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2039 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2040 connman_dbus_dict_append_basic(iter, "Privacy",
2041 DBUS_TYPE_STRING, &privacy);
2044 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2045 DBusMessageIter *iter)
2048 #if !defined TIZEN_EXT
2052 str = __connman_ipconfig_method2string(ipconfig->method);
2056 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2058 switch (ipconfig->method) {
2059 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2060 case CONNMAN_IPCONFIG_METHOD_OFF:
2061 case CONNMAN_IPCONFIG_METHOD_DHCP:
2062 case CONNMAN_IPCONFIG_METHOD_AUTO:
2064 case CONNMAN_IPCONFIG_METHOD_FIXED:
2065 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2069 if (!ipconfig->address)
2072 if (ipconfig->address->local) {
2074 struct in_addr netmask;
2077 connman_dbus_dict_append_basic(iter, "Address",
2078 DBUS_TYPE_STRING, &ipconfig->address->local);
2080 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2081 netmask.s_addr = htonl(addr);
2082 mask = inet_ntoa(netmask);
2083 connman_dbus_dict_append_basic(iter, "Netmask",
2084 DBUS_TYPE_STRING, &mask);
2087 if (ipconfig->address->gateway)
2088 connman_dbus_dict_append_basic(iter, "Gateway",
2089 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2092 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2093 DBusMessageIter *array)
2095 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2096 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2097 *privacy_string = NULL;
2098 int prefix_length = 0, privacy = 0;
2099 DBusMessageIter dict;
2102 DBG("ipconfig %p", ipconfig);
2104 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2107 dbus_message_iter_recurse(array, &dict);
2109 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2110 DBusMessageIter entry, value;
2114 dbus_message_iter_recurse(&dict, &entry);
2116 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2119 dbus_message_iter_get_basic(&entry, &key);
2120 dbus_message_iter_next(&entry);
2122 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2125 dbus_message_iter_recurse(&entry, &value);
2127 type = dbus_message_iter_get_arg_type(&value);
2129 if (g_str_equal(key, "Method")) {
2132 if (type != DBUS_TYPE_STRING)
2135 dbus_message_iter_get_basic(&value, &str);
2136 method = __connman_ipconfig_string2method(str);
2137 } else if (g_str_equal(key, "Address")) {
2138 if (type != DBUS_TYPE_STRING)
2141 dbus_message_iter_get_basic(&value, &address);
2142 } else if (g_str_equal(key, "PrefixLength")) {
2143 if (type != DBUS_TYPE_BYTE)
2146 dbus_message_iter_get_basic(&value, &prefix_length);
2148 if (prefix_length < 0 || prefix_length > 128)
2150 } else if (g_str_equal(key, "Netmask")) {
2151 if (type != DBUS_TYPE_STRING)
2154 dbus_message_iter_get_basic(&value, &netmask);
2155 } else if (g_str_equal(key, "Gateway")) {
2156 if (type != DBUS_TYPE_STRING)
2159 dbus_message_iter_get_basic(&value, &gateway);
2160 } else if (g_str_equal(key, "Privacy")) {
2161 if (type != DBUS_TYPE_STRING)
2164 dbus_message_iter_get_basic(&value, &privacy_string);
2165 privacy = string2privacy(privacy_string);
2168 dbus_message_iter_next(&dict);
2171 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2173 method, address, netmask, gateway, prefix_length,
2177 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2178 case CONNMAN_IPCONFIG_METHOD_FIXED:
2181 case CONNMAN_IPCONFIG_METHOD_OFF:
2182 ipconfig->method = method;
2183 #if defined TIZEN_EXT
2184 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2185 disable_ipv6(ipconfig);
2189 case CONNMAN_IPCONFIG_METHOD_AUTO:
2190 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2193 ipconfig->method = method;
2195 ipconfig->ipv6_privacy_config = privacy;
2196 #if defined TIZEN_EXT
2197 enable_ipv6(ipconfig);
2201 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2202 switch (ipconfig->type) {
2203 case CONNMAN_IPCONFIG_TYPE_IPV4:
2206 case CONNMAN_IPCONFIG_TYPE_IPV6:
2209 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2210 case CONNMAN_IPCONFIG_TYPE_ALL:
2215 if ((address && connman_inet_check_ipaddress(address)
2218 connman_inet_check_ipaddress(netmask)
2221 connman_inet_check_ipaddress(gateway)
2225 ipconfig->method = method;
2227 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2228 connman_ipaddress_set_ipv4(ipconfig->address,
2229 address, netmask, gateway);
2231 return connman_ipaddress_set_ipv6(
2232 ipconfig->address, address,
2233 prefix_length, gateway);
2237 case CONNMAN_IPCONFIG_METHOD_DHCP:
2238 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2241 ipconfig->method = method;
2248 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2249 DBusMessageIter *iter)
2251 struct connman_ipdevice *ipdevice;
2252 const char *method = "auto";
2254 connman_dbus_dict_append_basic(iter, "Method",
2255 DBUS_TYPE_STRING, &method);
2257 ipdevice = g_hash_table_lookup(ipdevice_hash,
2258 GINT_TO_POINTER(ipconfig->index));
2262 if (ipconfig->index >= 0) {
2263 char *ifname = connman_inet_ifname(ipconfig->index);
2265 connman_dbus_dict_append_basic(iter, "Interface",
2266 DBUS_TYPE_STRING, &ifname);
2271 if (ipdevice->address)
2272 connman_dbus_dict_append_basic(iter, "Address",
2273 DBUS_TYPE_STRING, &ipdevice->address);
2275 if (ipdevice->mtu > 0)
2276 connman_dbus_dict_append_basic(iter, "MTU",
2277 DBUS_TYPE_UINT16, &ipdevice->mtu);
2280 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2281 GKeyFile *keyfile, const char *identifier, const char *prefix)
2287 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2289 key = g_strdup_printf("%smethod", prefix);
2290 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2292 switch (ipconfig->type) {
2293 case CONNMAN_IPCONFIG_TYPE_IPV4:
2294 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2296 case CONNMAN_IPCONFIG_TYPE_IPV6:
2297 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2299 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2300 case CONNMAN_IPCONFIG_TYPE_ALL:
2301 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2305 ipconfig->method = __connman_ipconfig_string2method(method);
2307 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2308 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2310 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2314 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2315 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2318 pprefix = g_strdup_printf("%sprivacy", prefix);
2319 privacy = g_key_file_get_string(keyfile, identifier,
2321 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2326 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2327 ipconfig->last_dhcpv6_prefixes =
2328 g_key_file_get_string_list(keyfile, identifier, pprefix,
2330 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2331 g_free(ipconfig->last_dhcpv6_prefixes);
2332 ipconfig->last_dhcpv6_prefixes = NULL;
2340 switch (ipconfig->method) {
2341 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2342 case CONNMAN_IPCONFIG_METHOD_OFF:
2345 case CONNMAN_IPCONFIG_METHOD_FIXED:
2346 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2348 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2349 ipconfig->address->prefixlen = g_key_file_get_integer(
2350 keyfile, identifier, key, NULL);
2353 key = g_strdup_printf("%slocal_address", prefix);
2354 g_free(ipconfig->address->local);
2355 ipconfig->address->local = g_key_file_get_string(
2356 keyfile, identifier, key, NULL);
2359 key = g_strdup_printf("%speer_address", prefix);
2360 g_free(ipconfig->address->peer);
2361 ipconfig->address->peer = g_key_file_get_string(
2362 keyfile, identifier, key, NULL);
2365 key = g_strdup_printf("%sbroadcast_address", prefix);
2366 g_free(ipconfig->address->broadcast);
2367 ipconfig->address->broadcast = g_key_file_get_string(
2368 keyfile, identifier, key, NULL);
2371 key = g_strdup_printf("%sgateway", prefix);
2372 g_free(ipconfig->address->gateway);
2373 ipconfig->address->gateway = g_key_file_get_string(
2374 keyfile, identifier, key, NULL);
2378 case CONNMAN_IPCONFIG_METHOD_DHCP:
2380 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2381 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2383 g_free(ipconfig->last_dhcp_address);
2384 ipconfig->last_dhcp_address = str;
2390 case CONNMAN_IPCONFIG_METHOD_AUTO:
2397 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2398 GKeyFile *keyfile, const char *identifier, const char *prefix)
2403 method = __connman_ipconfig_method2string(ipconfig->method);
2405 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2408 key = g_strdup_printf("%smethod", prefix);
2409 g_key_file_set_string(keyfile, identifier, key, method);
2413 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2414 const char *privacy;
2415 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2416 key = g_strdup_printf("%sprivacy", prefix);
2417 g_key_file_set_string(keyfile, identifier, key, privacy);
2420 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2421 if (ipconfig->last_dhcp_address &&
2422 strlen(ipconfig->last_dhcp_address) > 0)
2423 g_key_file_set_string(keyfile, identifier, key,
2424 ipconfig->last_dhcp_address);
2426 g_key_file_remove_key(keyfile, identifier, key, NULL);
2429 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2430 if (ipconfig->last_dhcpv6_prefixes &&
2431 ipconfig->last_dhcpv6_prefixes[0]) {
2433 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2435 g_key_file_set_string_list(keyfile, identifier, key,
2436 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2439 g_key_file_remove_key(keyfile, identifier, key, NULL);
2443 switch (ipconfig->method) {
2444 case CONNMAN_IPCONFIG_METHOD_FIXED:
2445 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2447 case CONNMAN_IPCONFIG_METHOD_DHCP:
2448 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2449 if (ipconfig->last_dhcp_address &&
2450 strlen(ipconfig->last_dhcp_address) > 0)
2451 g_key_file_set_string(keyfile, identifier, key,
2452 ipconfig->last_dhcp_address);
2454 g_key_file_remove_key(keyfile, identifier, key, NULL);
2457 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2458 case CONNMAN_IPCONFIG_METHOD_OFF:
2459 case CONNMAN_IPCONFIG_METHOD_AUTO:
2463 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2464 if (ipconfig->address->prefixlen != 0)
2465 g_key_file_set_integer(keyfile, identifier,
2466 key, ipconfig->address->prefixlen);
2469 key = g_strdup_printf("%slocal_address", prefix);
2470 if (ipconfig->address->local)
2471 g_key_file_set_string(keyfile, identifier,
2472 key, ipconfig->address->local);
2475 key = g_strdup_printf("%speer_address", prefix);
2476 if (ipconfig->address->peer)
2477 g_key_file_set_string(keyfile, identifier,
2478 key, ipconfig->address->peer);
2481 key = g_strdup_printf("%sbroadcast_address", prefix);
2482 if (ipconfig->address->broadcast)
2483 g_key_file_set_string(keyfile, identifier,
2484 key, ipconfig->address->broadcast);
2487 key = g_strdup_printf("%sgateway", prefix);
2488 if (ipconfig->address->gateway)
2489 g_key_file_set_string(keyfile, identifier,
2490 key, ipconfig->address->gateway);
2496 int __connman_ipconfig_init(void)
2500 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2501 NULL, free_ipdevice);
2503 is_ipv6_supported = connman_inet_is_ipv6_supported();
2508 void __connman_ipconfig_cleanup(void)
2512 g_hash_table_destroy(ipdevice_hash);
2513 ipdevice_hash = NULL;