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;
56 int ipv6_privacy_config;
57 char *last_dhcp_address;
58 char **last_dhcpv6_prefixes;
61 struct connman_ipdevice {
82 struct connman_ipconfig *config_ipv4;
83 struct connman_ipconfig *config_ipv6;
89 static GHashTable *ipdevice_hash = NULL;
90 static GList *ipconfig_list = NULL;
91 static bool is_ipv6_supported = false;
93 void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
98 connman_ipaddress_clear(ipconfig->address);
101 static void free_address_list(struct connman_ipdevice *ipdevice)
105 for (list = ipdevice->address_list; list; list = list->next) {
106 struct connman_ipaddress *ipaddress = list->data;
108 connman_ipaddress_free(ipaddress);
112 g_slist_free(ipdevice->address_list);
113 ipdevice->address_list = NULL;
116 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
117 unsigned char prefixlen, const char *local)
121 for (list = ipdevice->address_list; list; list = list->next) {
122 struct connman_ipaddress *ipaddress = list->data;
124 if (g_strcmp0(ipaddress->local, local) == 0 &&
125 ipaddress->prefixlen == prefixlen)
132 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
135 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
136 case CONNMAN_IPCONFIG_TYPE_ALL:
138 case CONNMAN_IPCONFIG_TYPE_IPV4:
140 case CONNMAN_IPCONFIG_TYPE_IPV6:
147 static const char *type2str(unsigned short type)
152 case ARPHRD_LOOPBACK:
165 static const char *scope2str(unsigned char scope)
177 static bool get_ipv6_state(gchar *ifname)
182 bool enabled = false;
185 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
187 path = g_strdup_printf(
188 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
193 f = fopen(path, "r");
198 if (fscanf(f, "%d", &disabled) > 0)
206 static void set_ipv6_state(gchar *ifname, bool enable)
212 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
214 path = g_strdup_printf(
215 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
220 f = fopen(path, "r+");
235 static int get_ipv6_privacy(gchar *ifname)
244 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
250 f = fopen(path, "r");
257 if (fscanf(f, "%d", &value) <= 0)
265 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
266 * The privacy extension is described in RFC 3041 and RFC 4941
268 static void set_ipv6_privacy(gchar *ifname, int value)
276 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
285 f = fopen(path, "r+");
292 fprintf(f, "%d", value);
296 static int get_rp_filter(void)
299 int value = -EINVAL, tmp;
301 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
304 if (fscanf(f, "%d", &tmp) == 1)
312 static void set_rp_filter(int value)
316 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
321 fprintf(f, "%d", value);
326 int __connman_ipconfig_set_rp_filter()
330 value = get_rp_filter();
337 connman_info("rp_filter set to 2 (loose mode routing), "
338 "old value was %d", value);
343 void __connman_ipconfig_unset_rp_filter(int old_value)
345 set_rp_filter(old_value);
347 connman_info("rp_filter restored to %d", old_value);
350 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
355 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
358 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
360 struct connman_ipdevice *ipdevice;
367 ipdevice = g_hash_table_lookup(ipdevice_hash,
368 GINT_TO_POINTER(ipconfig->index));
372 ifname = connman_inet_ifname(ipconfig->index);
373 ret = get_ipv6_state(ifname);
379 static void free_ipdevice(gpointer data)
381 struct connman_ipdevice *ipdevice = data;
382 char *ifname = connman_inet_ifname(ipdevice->index);
384 connman_info("%s {remove} index %d", ifname, ipdevice->index);
386 if (ipdevice->config_ipv4) {
387 __connman_ipconfig_unref(ipdevice->config_ipv4);
388 ipdevice->config_ipv4 = NULL;
391 if (ipdevice->config_ipv6) {
392 __connman_ipconfig_unref(ipdevice->config_ipv6);
393 ipdevice->config_ipv6 = NULL;
396 free_address_list(ipdevice);
397 g_free(ipdevice->ipv4_gateway);
398 g_free(ipdevice->ipv6_gateway);
399 g_free(ipdevice->pac);
401 g_free(ipdevice->address);
403 set_ipv6_state(ifname, ipdevice->ipv6_enabled);
404 set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
410 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
412 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
413 ipdevice->config_ipv6);
416 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
418 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
419 ipdevice->config_ipv6);
421 if (ipdevice->config_ipv4)
422 connman_inet_clear_address(ipdevice->index,
423 ipdevice->config_ipv4->address);
425 if (ipdevice->config_ipv6)
426 connman_inet_clear_ipv6_address(ipdevice->index,
427 ipdevice->config_ipv6->address->local,
428 ipdevice->config_ipv6->address->prefixlen);
431 static void update_stats(struct connman_ipdevice *ipdevice,
432 const char *ifname, struct rtnl_link_stats *stats)
434 struct connman_service *service;
436 if (stats->rx_packets == 0 && stats->tx_packets == 0)
439 connman_info("%s {RX} %u packets %u bytes", ifname,
440 stats->rx_packets, stats->rx_bytes);
441 connman_info("%s {TX} %u packets %u bytes", ifname,
442 stats->tx_packets, stats->tx_bytes);
444 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
447 service = __connman_service_lookup_from_index(ipdevice->index);
449 DBG("service %p", service);
454 ipdevice->rx_packets = stats->rx_packets;
455 ipdevice->tx_packets = stats->tx_packets;
456 ipdevice->rx_bytes = stats->rx_bytes;
457 ipdevice->tx_bytes = stats->tx_bytes;
458 ipdevice->rx_errors = stats->rx_errors;
459 ipdevice->tx_errors = stats->tx_errors;
460 ipdevice->rx_dropped = stats->rx_dropped;
461 ipdevice->tx_dropped = stats->tx_dropped;
463 __connman_service_notify(service,
464 ipdevice->rx_packets, ipdevice->tx_packets,
465 ipdevice->rx_bytes, ipdevice->tx_bytes,
466 ipdevice->rx_errors, ipdevice->tx_errors,
467 ipdevice->rx_dropped, ipdevice->tx_dropped);
470 void __connman_ipconfig_newlink(int index, unsigned short type,
471 unsigned int flags, const char *address,
473 struct rtnl_link_stats *stats)
475 struct connman_ipdevice *ipdevice;
476 GList *list, *ipconfig_copy;
478 bool up = false, down = false;
479 bool lower_up = false, lower_down = false;
482 DBG("index %d", index);
484 if (type == ARPHRD_LOOPBACK)
487 ifname = connman_inet_ifname(index);
489 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
493 ipdevice = g_try_new0(struct connman_ipdevice, 1);
497 ipdevice->index = index;
498 ipdevice->type = type;
500 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
501 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
503 ipdevice->address = g_strdup(address);
505 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
507 connman_info("%s {create} index %d type %d <%s>", ifname,
508 index, type, type2str(type));
511 #if defined TIZEN_EXT
512 if (g_strcmp0(ipdevice->address, address) != 0) {
513 /* If an original address is built-in physical device,
514 * it's hardly get an address at a initial creation
516 g_free(ipdevice->address);
517 ipdevice->address = g_strdup(address);
523 update_stats(ipdevice, ifname, stats);
525 if (flags == ipdevice->flags)
528 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
535 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
536 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
537 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
538 (IFF_RUNNING | IFF_LOWER_UP))
540 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
544 ipdevice->flags = flags;
546 str = g_string_new(NULL);
551 g_string_append(str, "UP");
553 g_string_append(str, "DOWN");
555 if (flags & IFF_RUNNING)
556 g_string_append(str, ",RUNNING");
558 if (flags & IFF_LOWER_UP)
559 g_string_append(str, ",LOWER_UP");
561 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
563 g_string_free(str, TRUE);
565 ipconfig_copy = g_list_copy(ipconfig_list);
567 for (list = g_list_first(ipconfig_copy); list;
568 list = g_list_next(list)) {
569 struct connman_ipconfig *ipconfig = list->data;
571 if (index != ipconfig->index)
577 if (up && ipconfig->ops->up)
578 ipconfig->ops->up(ipconfig, ifname);
579 if (lower_up && ipconfig->ops->lower_up)
580 ipconfig->ops->lower_up(ipconfig, ifname);
582 if (lower_down && ipconfig->ops->lower_down)
583 ipconfig->ops->lower_down(ipconfig, ifname);
584 if (down && ipconfig->ops->down)
585 ipconfig->ops->down(ipconfig, ifname);
588 g_list_free(ipconfig_copy);
591 __connman_ipconfig_lower_up(ipdevice);
593 __connman_ipconfig_lower_down(ipdevice);
599 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
601 struct connman_ipdevice *ipdevice;
605 DBG("index %d", index);
607 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
611 ifname = connman_inet_ifname(index);
613 update_stats(ipdevice, ifname, stats);
615 for (list = g_list_first(ipconfig_list); list;
616 list = g_list_next(list)) {
617 struct connman_ipconfig *ipconfig = list->data;
619 if (index != ipconfig->index)
622 ipconfig->index = -1;
627 if (ipconfig->ops->lower_down)
628 ipconfig->ops->lower_down(ipconfig, ifname);
629 if (ipconfig->ops->down)
630 ipconfig->ops->down(ipconfig, ifname);
635 __connman_ipconfig_lower_down(ipdevice);
637 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
640 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
642 const struct connman_ipaddress *addr1 = a;
643 const struct connman_ipaddress *addr2 = b;
645 if (addr1->prefixlen != addr2->prefixlen)
646 return addr2->prefixlen - addr1->prefixlen;
648 return g_strcmp0(addr1->local, addr2->local);
651 int __connman_ipconfig_newaddr(int index, int family, const char *label,
652 unsigned char prefixlen, const char *address)
654 struct connman_ipdevice *ipdevice;
655 struct connman_ipaddress *ipaddress;
656 enum connman_ipconfig_type type;
660 DBG("index %d", index);
662 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
666 ipaddress = connman_ipaddress_alloc(family);
670 ipaddress->prefixlen = prefixlen;
671 ipaddress->local = g_strdup(address);
673 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
674 check_duplicate_address)) {
675 connman_ipaddress_free(ipaddress);
679 if (family == AF_INET)
680 type = CONNMAN_IPCONFIG_TYPE_IPV4;
681 else if (family == AF_INET6)
682 type = CONNMAN_IPCONFIG_TYPE_IPV6;
686 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
689 ifname = connman_inet_ifname(index);
690 connman_info("%s {add} address %s/%u label %s family %d",
691 ifname, address, prefixlen, label, family);
693 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
694 __connman_ippool_newaddr(index, address, prefixlen);
696 if (ipdevice->config_ipv4 && family == AF_INET)
697 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
700 else if (ipdevice->config_ipv6 && family == AF_INET6)
701 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
706 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
709 for (list = g_list_first(ipconfig_list); list;
710 list = g_list_next(list)) {
711 struct connman_ipconfig *ipconfig = list->data;
713 if (index != ipconfig->index)
716 if (type != ipconfig->type)
722 if (ipconfig->ops->ip_bound)
723 ipconfig->ops->ip_bound(ipconfig, ifname);
731 void __connman_ipconfig_deladdr(int index, int family, const char *label,
732 unsigned char prefixlen, const char *address)
734 struct connman_ipdevice *ipdevice;
735 struct connman_ipaddress *ipaddress;
736 enum connman_ipconfig_type type;
740 DBG("index %d", index);
742 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
746 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
750 if (family == AF_INET)
751 type = CONNMAN_IPCONFIG_TYPE_IPV4;
752 else if (family == AF_INET6)
753 type = CONNMAN_IPCONFIG_TYPE_IPV6;
757 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
760 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
761 __connman_ippool_deladdr(index, address, prefixlen);
763 connman_ipaddress_clear(ipaddress);
766 ifname = connman_inet_ifname(index);
767 connman_info("%s {del} address %s/%u label %s", ifname,
768 address, prefixlen, label);
770 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
773 if (g_slist_length(ipdevice->address_list) > 0)
776 for (list = g_list_first(ipconfig_list); list;
777 list = g_list_next(list)) {
778 struct connman_ipconfig *ipconfig = list->data;
780 if (index != ipconfig->index)
783 if (type != ipconfig->type)
789 if (ipconfig->ops->ip_release)
790 ipconfig->ops->ip_release(ipconfig, ifname);
797 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
798 const char *dst, const char *gateway)
800 struct connman_ipdevice *ipdevice;
803 DBG("index %d", index);
805 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
809 ifname = connman_inet_ifname(index);
811 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
812 g_strcmp0(dst, "::") == 0)) {
814 enum connman_ipconfig_type type;
816 if (family == AF_INET6) {
817 type = CONNMAN_IPCONFIG_TYPE_IPV6;
818 g_free(ipdevice->ipv6_gateway);
819 ipdevice->ipv6_gateway = g_strdup(gateway);
821 if (ipdevice->config_ipv6 &&
822 ipdevice->config_ipv6->system) {
823 g_free(ipdevice->config_ipv6->system->gateway);
824 ipdevice->config_ipv6->system->gateway =
827 } else if (family == AF_INET) {
828 type = CONNMAN_IPCONFIG_TYPE_IPV4;
829 g_free(ipdevice->ipv4_gateway);
830 ipdevice->ipv4_gateway = g_strdup(gateway);
832 if (ipdevice->config_ipv4 &&
833 ipdevice->config_ipv4->system) {
834 g_free(ipdevice->config_ipv4->system->gateway);
835 ipdevice->config_ipv4->system->gateway =
841 for (config_list = g_list_first(ipconfig_list); config_list;
842 config_list = g_list_next(config_list)) {
843 struct connman_ipconfig *ipconfig = config_list->data;
845 if (index != ipconfig->index)
848 if (type != ipconfig->type)
854 if (ipconfig->ops->route_set)
855 ipconfig->ops->route_set(ipconfig, ifname);
859 connman_info("%s {add} route %s gw %s scope %u <%s>",
860 ifname, dst, gateway, scope, scope2str(scope));
866 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
867 const char *dst, const char *gateway)
869 struct connman_ipdevice *ipdevice;
872 DBG("index %d", index);
874 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
878 ifname = connman_inet_ifname(index);
880 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
881 g_strcmp0(dst, "::") == 0)) {
883 enum connman_ipconfig_type type;
885 if (family == AF_INET6) {
886 type = CONNMAN_IPCONFIG_TYPE_IPV6;
887 g_free(ipdevice->ipv6_gateway);
888 ipdevice->ipv6_gateway = NULL;
890 if (ipdevice->config_ipv6 &&
891 ipdevice->config_ipv6->system) {
892 g_free(ipdevice->config_ipv6->system->gateway);
893 ipdevice->config_ipv6->system->gateway = NULL;
895 } else if (family == AF_INET) {
896 type = CONNMAN_IPCONFIG_TYPE_IPV4;
897 g_free(ipdevice->ipv4_gateway);
898 ipdevice->ipv4_gateway = NULL;
900 if (ipdevice->config_ipv4 &&
901 ipdevice->config_ipv4->system) {
902 g_free(ipdevice->config_ipv4->system->gateway);
903 ipdevice->config_ipv4->system->gateway = NULL;
908 for (config_list = g_list_first(ipconfig_list); config_list;
909 config_list = g_list_next(config_list)) {
910 struct connman_ipconfig *ipconfig = config_list->data;
912 if (index != ipconfig->index)
915 if (type != ipconfig->type)
921 if (ipconfig->ops->route_unset)
922 ipconfig->ops->route_unset(ipconfig, ifname);
926 connman_info("%s {del} route %s gw %s scope %u <%s>",
927 ifname, dst, gateway, scope, scope2str(scope));
933 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
938 keys = g_hash_table_get_keys(ipdevice_hash);
942 for (list = g_list_first(keys); list; list = g_list_next(list)) {
943 int index = GPOINTER_TO_INT(list->data);
945 function(index, user_data);
951 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
952 struct connman_ipconfig *ipconfig)
954 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
957 unsigned short __connman_ipconfig_get_type_from_index(int index)
959 struct connman_ipdevice *ipdevice;
961 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
965 return ipdevice->type;
968 unsigned int __connman_ipconfig_get_flags_from_index(int index)
970 struct connman_ipdevice *ipdevice;
972 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
976 return ipdevice->flags;
979 const char *__connman_ipconfig_get_gateway_from_index(int index,
980 enum connman_ipconfig_type type)
982 struct connman_ipdevice *ipdevice;
984 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
988 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
989 if (ipdevice->ipv4_gateway)
990 return ipdevice->ipv4_gateway;
992 if (ipdevice->config_ipv4 &&
993 ipdevice->config_ipv4->address)
994 return ipdevice->config_ipv4->address->gateway;
997 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
998 if (ipdevice->ipv6_gateway)
999 return ipdevice->ipv6_gateway;
1001 if (ipdevice->config_ipv6 &&
1002 ipdevice->config_ipv6->address)
1003 return ipdevice->config_ipv6->address->gateway;
1009 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1011 ipconfig->index = index;
1014 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1016 if (!ipconfig->address)
1019 return ipconfig->address->local;
1022 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1023 const char *address)
1025 if (!ipconfig->address)
1028 g_free(ipconfig->address->local);
1029 ipconfig->address->local = g_strdup(address);
1032 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1034 if (!ipconfig->address)
1037 return ipconfig->address->peer;
1040 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1041 const char *address)
1043 if (!ipconfig->address)
1046 g_free(ipconfig->address->peer);
1047 ipconfig->address->peer = g_strdup(address);
1050 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1052 if (!ipconfig->address)
1055 return ipconfig->address->broadcast;
1058 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1059 const char *broadcast)
1061 if (!ipconfig->address)
1064 g_free(ipconfig->address->broadcast);
1065 ipconfig->address->broadcast = g_strdup(broadcast);
1068 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1070 if (!ipconfig->address)
1073 return ipconfig->address->gateway;
1076 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1077 const char *gateway)
1081 if (!ipconfig->address)
1083 g_free(ipconfig->address->gateway);
1084 ipconfig->address->gateway = g_strdup(gateway);
1087 #if defined TIZEN_EXT
1088 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1090 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1093 #if !defined TIZEN_EXT
1094 struct connman_service *service;
1099 if (!ipconfig->address)
1102 #if !defined TIZEN_EXT
1103 service = __connman_service_lookup_from_index(ipconfig->index);
1108 DBG("type %d gw %s peer %s", ipconfig->type,
1109 ipconfig->address->gateway, ipconfig->address->peer);
1111 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1112 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1113 return __connman_connection_gateway_add(service,
1114 ipconfig->address->gateway,
1116 ipconfig->address->peer);
1121 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1123 struct connman_service *service;
1127 service = __connman_service_lookup_from_index(ipconfig->index);
1129 __connman_connection_gateway_remove(service, ipconfig->type);
1132 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1134 if (!ipconfig->address)
1137 return ipconfig->address->prefixlen;
1140 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1141 unsigned char prefixlen)
1143 if (!ipconfig->address)
1146 ipconfig->address->prefixlen = prefixlen;
1149 static struct connman_ipconfig *create_ipv6config(int index)
1151 struct connman_ipconfig *ipv6config;
1152 struct connman_ipdevice *ipdevice;
1154 DBG("index %d", index);
1156 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1160 ipv6config->refcount = 1;
1162 ipv6config->index = index;
1163 ipv6config->enabled = false;
1164 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1166 if (!is_ipv6_supported)
1167 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1169 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1171 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1173 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1175 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1176 if (!ipv6config->address) {
1181 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1183 DBG("ipconfig %p method %s", ipv6config,
1184 __connman_ipconfig_method2string(ipv6config->method));
1190 * connman_ipconfig_create:
1192 * Allocate a new ipconfig structure.
1194 * Returns: a newly-allocated #connman_ipconfig structure
1196 struct connman_ipconfig *__connman_ipconfig_create(int index,
1197 enum connman_ipconfig_type type)
1199 struct connman_ipconfig *ipconfig;
1201 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1202 return create_ipv6config(index);
1204 DBG("index %d", index);
1206 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1210 ipconfig->refcount = 1;
1212 ipconfig->index = index;
1213 ipconfig->enabled = false;
1214 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1216 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1217 if (!ipconfig->address) {
1222 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1224 DBG("ipconfig %p", ipconfig);
1231 * connman_ipconfig_ref:
1232 * @ipconfig: ipconfig structure
1234 * Increase reference counter of ipconfig
1236 struct connman_ipconfig *
1237 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1238 const char *file, int line, const char *caller)
1240 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1241 file, line, caller);
1243 __sync_fetch_and_add(&ipconfig->refcount, 1);
1249 * connman_ipconfig_unref:
1250 * @ipconfig: ipconfig structure
1252 * Decrease reference counter of ipconfig
1254 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1255 const char *file, int line, const char *caller)
1260 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1261 file, line, caller);
1263 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1266 if (__connman_ipconfig_disable(ipconfig) < 0)
1267 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1269 __connman_ipconfig_set_ops(ipconfig, NULL);
1271 connman_ipaddress_free(ipconfig->system);
1272 connman_ipaddress_free(ipconfig->address);
1273 g_free(ipconfig->last_dhcp_address);
1274 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1279 * connman_ipconfig_get_data:
1280 * @ipconfig: ipconfig structure
1282 * Get private data pointer
1284 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1289 return ipconfig->ops_data;
1293 * connman_ipconfig_set_data:
1294 * @ipconfig: ipconfig structure
1295 * @data: data pointer
1297 * Set private data pointer
1299 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1301 ipconfig->ops_data = data;
1305 * connman_ipconfig_get_index:
1306 * @ipconfig: ipconfig structure
1308 * Get interface index
1310 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1315 return ipconfig->index;
1319 * connman_ipconfig_set_ops:
1320 * @ipconfig: ipconfig structure
1321 * @ops: operation callbacks
1323 * Set the operation callbacks
1325 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1326 const struct connman_ipconfig_ops *ops)
1328 ipconfig->ops = ops;
1332 * connman_ipconfig_set_method:
1333 * @ipconfig: ipconfig structure
1334 * @method: configuration method
1336 * Set the configuration method
1338 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1339 enum connman_ipconfig_method method)
1341 ipconfig->method = method;
1346 enum connman_ipconfig_method __connman_ipconfig_get_method(
1347 struct connman_ipconfig *ipconfig)
1350 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1352 return ipconfig->method;
1355 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1359 switch (ipconfig->method) {
1360 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1361 case CONNMAN_IPCONFIG_METHOD_OFF:
1363 case CONNMAN_IPCONFIG_METHOD_AUTO:
1364 case CONNMAN_IPCONFIG_METHOD_FIXED:
1365 case CONNMAN_IPCONFIG_METHOD_DHCP:
1366 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1367 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1368 return connman_inet_set_address(ipconfig->index,
1370 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1371 return connman_inet_set_ipv6_address(
1372 ipconfig->index, ipconfig->address);
1378 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1387 DBG("method %d", ipconfig->method);
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 err = __connman_ipconfig_address_unset(ipconfig);
1398 connman_ipaddress_clear(ipconfig->address);
1406 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1415 DBG("method %d", ipconfig->method);
1417 switch (ipconfig->method) {
1418 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1419 case CONNMAN_IPCONFIG_METHOD_OFF:
1421 case CONNMAN_IPCONFIG_METHOD_AUTO:
1422 case CONNMAN_IPCONFIG_METHOD_FIXED:
1423 case CONNMAN_IPCONFIG_METHOD_DHCP:
1424 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1425 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1426 err = connman_inet_clear_address(ipconfig->index,
1428 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1429 err = connman_inet_clear_ipv6_address(
1431 ipconfig->address->local,
1432 ipconfig->address->prefixlen);
1442 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1445 struct connman_ipdevice *ipdevice;
1447 DBG("ipconfig %p", ipconfig);
1449 if (!ipconfig || ipconfig->index < 0)
1452 ipdevice = g_hash_table_lookup(ipdevice_hash,
1453 GINT_TO_POINTER(ipconfig->index));
1457 g_free(ipdevice->pac);
1458 ipdevice->pac = g_strdup(url);
1463 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1465 struct connman_ipdevice *ipdevice;
1467 DBG("ipconfig %p", ipconfig);
1469 if (!ipconfig || ipconfig->index < 0)
1472 ipdevice = g_hash_table_lookup(ipdevice_hash,
1473 GINT_TO_POINTER(ipconfig->index));
1477 return ipdevice->pac;
1480 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1481 const char *address)
1486 g_free(ipconfig->last_dhcp_address);
1487 ipconfig->last_dhcp_address = g_strdup(address);
1490 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1495 return ipconfig->last_dhcp_address;
1498 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1504 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1505 ipconfig->last_dhcpv6_prefixes = prefixes;
1508 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1513 return ipconfig->last_dhcpv6_prefixes;
1516 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1518 struct connman_ipdevice *ipdevice;
1523 ipdevice = g_hash_table_lookup(ipdevice_hash,
1524 GINT_TO_POINTER(ipconfig->index));
1528 ifname = connman_inet_ifname(ipconfig->index);
1530 set_ipv6_state(ifname, false);
1535 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1537 struct connman_ipdevice *ipdevice;
1542 ipdevice = g_hash_table_lookup(ipdevice_hash,
1543 GINT_TO_POINTER(ipconfig->index));
1547 ifname = connman_inet_ifname(ipconfig->index);
1549 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1550 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1552 set_ipv6_state(ifname, true);
1557 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1559 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1562 enable_ipv6(ipconfig);
1565 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1567 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1570 disable_ipv6(ipconfig);
1573 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1578 switch (ipconfig->method) {
1579 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1580 case CONNMAN_IPCONFIG_METHOD_OFF:
1582 case CONNMAN_IPCONFIG_METHOD_AUTO:
1583 case CONNMAN_IPCONFIG_METHOD_FIXED:
1584 case CONNMAN_IPCONFIG_METHOD_DHCP:
1585 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1592 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1594 struct connman_ipdevice *ipdevice;
1595 bool up = false, down = false;
1596 bool lower_up = false, lower_down = false;
1597 enum connman_ipconfig_type type;
1600 DBG("ipconfig %p", ipconfig);
1602 if (!ipconfig || ipconfig->index < 0)
1605 ipdevice = g_hash_table_lookup(ipdevice_hash,
1606 GINT_TO_POINTER(ipconfig->index));
1610 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1611 if (ipdevice->config_ipv4 == ipconfig)
1613 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1614 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1615 if (ipdevice->config_ipv6 == ipconfig)
1617 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1621 ipconfig->enabled = true;
1623 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1624 ipdevice->config_ipv4) {
1625 ipconfig_list = g_list_remove(ipconfig_list,
1626 ipdevice->config_ipv4);
1628 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1630 __connman_ipconfig_unref(ipdevice->config_ipv4);
1633 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1634 ipdevice->config_ipv6) {
1635 ipconfig_list = g_list_remove(ipconfig_list,
1636 ipdevice->config_ipv6);
1638 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1640 __connman_ipconfig_unref(ipdevice->config_ipv6);
1643 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1644 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1645 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1646 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1648 enable_ipv6(ipdevice->config_ipv6);
1650 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1652 if (ipdevice->flags & IFF_UP)
1657 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1658 (IFF_RUNNING | IFF_LOWER_UP))
1660 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1663 ifname = connman_inet_ifname(ipconfig->index);
1665 if (up && ipconfig->ops->up)
1666 ipconfig->ops->up(ipconfig, ifname);
1667 if (lower_up && ipconfig->ops->lower_up)
1668 ipconfig->ops->lower_up(ipconfig, ifname);
1670 if (lower_down && ipconfig->ops->lower_down)
1671 ipconfig->ops->lower_down(ipconfig, ifname);
1672 if (down && ipconfig->ops->down)
1673 ipconfig->ops->down(ipconfig, ifname);
1680 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1682 struct connman_ipdevice *ipdevice;
1684 DBG("ipconfig %p", ipconfig);
1686 if (!ipconfig || ipconfig->index < 0)
1689 ipdevice = g_hash_table_lookup(ipdevice_hash,
1690 GINT_TO_POINTER(ipconfig->index));
1694 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1697 ipconfig->enabled = false;
1699 if (ipdevice->config_ipv4 == ipconfig) {
1700 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1702 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1703 __connman_ipconfig_unref(ipdevice->config_ipv4);
1704 ipdevice->config_ipv4 = NULL;
1708 if (ipdevice->config_ipv6 == ipconfig) {
1709 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1711 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1712 __connman_ipconfig_unref(ipdevice->config_ipv6);
1713 ipdevice->config_ipv6 = NULL;
1720 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1723 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1725 case CONNMAN_IPCONFIG_METHOD_OFF:
1727 case CONNMAN_IPCONFIG_METHOD_FIXED:
1729 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1731 case CONNMAN_IPCONFIG_METHOD_DHCP:
1733 case CONNMAN_IPCONFIG_METHOD_AUTO:
1740 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1742 if (g_strcmp0(method, "off") == 0)
1743 return CONNMAN_IPCONFIG_METHOD_OFF;
1744 else if (g_strcmp0(method, "fixed") == 0)
1745 return CONNMAN_IPCONFIG_METHOD_FIXED;
1746 else if (g_strcmp0(method, "manual") == 0)
1747 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1748 else if (g_strcmp0(method, "dhcp") == 0)
1749 return CONNMAN_IPCONFIG_METHOD_DHCP;
1750 else if (g_strcmp0(method, "auto") == 0)
1751 return CONNMAN_IPCONFIG_METHOD_AUTO;
1753 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1756 static const char *privacy2string(int privacy)
1760 else if (privacy == 1)
1766 static int string2privacy(const char *privacy)
1768 if (g_strcmp0(privacy, "disabled") == 0)
1770 else if (g_strcmp0(privacy, "enabled") == 0)
1772 else if (g_strcmp0(privacy, "preferred") == 0)
1774 else if (g_strcmp0(privacy, "prefered") == 0)
1780 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1782 struct connman_ipdevice *ipdevice;
1788 ipdevice = g_hash_table_lookup(ipdevice_hash,
1789 GINT_TO_POINTER(ipconfig->index));
1793 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1794 ipdevice->ipv6_privacy));
1799 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1807 DBG("ipconfig %p privacy %s", ipconfig, value);
1809 privacy = string2privacy(value);
1811 ipconfig->ipv6_privacy_config = privacy;
1813 enable_ipv6(ipconfig);
1818 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1819 DBusMessageIter *iter)
1821 struct connman_ipaddress *append_addr = NULL;
1826 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1829 str = __connman_ipconfig_method2string(ipconfig->method);
1833 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1835 switch (ipconfig->method) {
1836 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1837 case CONNMAN_IPCONFIG_METHOD_OFF:
1838 case CONNMAN_IPCONFIG_METHOD_AUTO:
1841 case CONNMAN_IPCONFIG_METHOD_FIXED:
1842 append_addr = ipconfig->address;
1845 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1846 case CONNMAN_IPCONFIG_METHOD_DHCP:
1847 append_addr = ipconfig->system;
1848 #if defined TIZEN_EXT
1849 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1850 if (append_addr && append_addr->local == NULL)
1851 append_addr = ipconfig->address;
1859 if (append_addr->local) {
1861 struct in_addr netmask;
1864 connman_dbus_dict_append_basic(iter, "Address",
1865 DBUS_TYPE_STRING, &append_addr->local);
1867 addr = 0xffffffff << (32 - append_addr->prefixlen);
1868 netmask.s_addr = htonl(addr);
1869 mask = inet_ntoa(netmask);
1870 connman_dbus_dict_append_basic(iter, "Netmask",
1871 DBUS_TYPE_STRING, &mask);
1874 if (append_addr->gateway)
1875 connman_dbus_dict_append_basic(iter, "Gateway",
1876 DBUS_TYPE_STRING, &append_addr->gateway);
1879 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1880 DBusMessageIter *iter,
1881 struct connman_ipconfig *ipconfig_ipv4)
1883 struct connman_ipaddress *append_addr = NULL;
1884 const char *str, *privacy;
1888 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1891 str = __connman_ipconfig_method2string(ipconfig->method);
1895 if (ipconfig_ipv4 &&
1896 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1897 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1901 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1903 switch (ipconfig->method) {
1904 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1905 case CONNMAN_IPCONFIG_METHOD_OFF:
1908 case CONNMAN_IPCONFIG_METHOD_FIXED:
1909 append_addr = ipconfig->address;
1912 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1913 case CONNMAN_IPCONFIG_METHOD_DHCP:
1914 case CONNMAN_IPCONFIG_METHOD_AUTO:
1915 append_addr = ipconfig->system;
1916 #if defined TIZEN_EXT
1917 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1918 if (append_addr && append_addr->local == NULL)
1919 append_addr = ipconfig->address;
1927 if (append_addr->local) {
1928 connman_dbus_dict_append_basic(iter, "Address",
1929 DBUS_TYPE_STRING, &append_addr->local);
1930 connman_dbus_dict_append_basic(iter, "PrefixLength",
1932 &append_addr->prefixlen);
1935 if (append_addr->gateway)
1936 connman_dbus_dict_append_basic(iter, "Gateway",
1937 DBUS_TYPE_STRING, &append_addr->gateway);
1939 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1940 connman_dbus_dict_append_basic(iter, "Privacy",
1941 DBUS_TYPE_STRING, &privacy);
1944 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1945 DBusMessageIter *iter)
1947 const char *str, *privacy;
1951 str = __connman_ipconfig_method2string(ipconfig->method);
1955 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1957 switch (ipconfig->method) {
1958 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1959 case CONNMAN_IPCONFIG_METHOD_OFF:
1960 case CONNMAN_IPCONFIG_METHOD_DHCP:
1962 case CONNMAN_IPCONFIG_METHOD_FIXED:
1963 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1964 case CONNMAN_IPCONFIG_METHOD_AUTO:
1968 if (!ipconfig->address)
1971 if (ipconfig->address->local) {
1972 connman_dbus_dict_append_basic(iter, "Address",
1973 DBUS_TYPE_STRING, &ipconfig->address->local);
1974 connman_dbus_dict_append_basic(iter, "PrefixLength",
1976 &ipconfig->address->prefixlen);
1979 if (ipconfig->address->gateway)
1980 connman_dbus_dict_append_basic(iter, "Gateway",
1981 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1983 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1984 connman_dbus_dict_append_basic(iter, "Privacy",
1985 DBUS_TYPE_STRING, &privacy);
1988 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1989 DBusMessageIter *iter)
1995 str = __connman_ipconfig_method2string(ipconfig->method);
1999 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2001 switch (ipconfig->method) {
2002 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2003 case CONNMAN_IPCONFIG_METHOD_OFF:
2004 case CONNMAN_IPCONFIG_METHOD_DHCP:
2005 case CONNMAN_IPCONFIG_METHOD_AUTO:
2007 case CONNMAN_IPCONFIG_METHOD_FIXED:
2008 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2012 if (!ipconfig->address)
2015 if (ipconfig->address->local) {
2017 struct in_addr netmask;
2020 connman_dbus_dict_append_basic(iter, "Address",
2021 DBUS_TYPE_STRING, &ipconfig->address->local);
2023 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2024 netmask.s_addr = htonl(addr);
2025 mask = inet_ntoa(netmask);
2026 connman_dbus_dict_append_basic(iter, "Netmask",
2027 DBUS_TYPE_STRING, &mask);
2030 if (ipconfig->address->gateway)
2031 connman_dbus_dict_append_basic(iter, "Gateway",
2032 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2035 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2036 DBusMessageIter *array)
2038 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2039 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2040 *privacy_string = NULL;
2041 int prefix_length = 0, privacy = 0;
2042 DBusMessageIter dict;
2045 DBG("ipconfig %p", ipconfig);
2047 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2050 dbus_message_iter_recurse(array, &dict);
2052 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2053 DBusMessageIter entry, value;
2057 dbus_message_iter_recurse(&dict, &entry);
2059 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2062 dbus_message_iter_get_basic(&entry, &key);
2063 dbus_message_iter_next(&entry);
2065 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2068 dbus_message_iter_recurse(&entry, &value);
2070 type = dbus_message_iter_get_arg_type(&value);
2072 if (g_str_equal(key, "Method")) {
2075 if (type != DBUS_TYPE_STRING)
2078 dbus_message_iter_get_basic(&value, &str);
2079 method = __connman_ipconfig_string2method(str);
2080 } else if (g_str_equal(key, "Address")) {
2081 if (type != DBUS_TYPE_STRING)
2084 dbus_message_iter_get_basic(&value, &address);
2085 } else if (g_str_equal(key, "PrefixLength")) {
2086 if (type != DBUS_TYPE_BYTE)
2089 dbus_message_iter_get_basic(&value, &prefix_length);
2091 if (prefix_length < 0 || prefix_length > 128)
2093 } else if (g_str_equal(key, "Netmask")) {
2094 if (type != DBUS_TYPE_STRING)
2097 dbus_message_iter_get_basic(&value, &netmask);
2098 } else if (g_str_equal(key, "Gateway")) {
2099 if (type != DBUS_TYPE_STRING)
2102 dbus_message_iter_get_basic(&value, &gateway);
2103 } else if (g_str_equal(key, "Privacy")) {
2104 if (type != DBUS_TYPE_STRING)
2107 dbus_message_iter_get_basic(&value, &privacy_string);
2108 privacy = string2privacy(privacy_string);
2111 dbus_message_iter_next(&dict);
2114 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2116 method, address, netmask, gateway, prefix_length,
2120 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2121 case CONNMAN_IPCONFIG_METHOD_FIXED:
2124 case CONNMAN_IPCONFIG_METHOD_OFF:
2125 ipconfig->method = method;
2129 case CONNMAN_IPCONFIG_METHOD_AUTO:
2130 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2133 ipconfig->method = method;
2135 ipconfig->ipv6_privacy_config = privacy;
2139 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2140 switch (ipconfig->type) {
2141 case CONNMAN_IPCONFIG_TYPE_IPV4:
2144 case CONNMAN_IPCONFIG_TYPE_IPV6:
2147 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2148 case CONNMAN_IPCONFIG_TYPE_ALL:
2153 if ((address && connman_inet_check_ipaddress(address)
2156 connman_inet_check_ipaddress(netmask)
2159 connman_inet_check_ipaddress(gateway)
2163 ipconfig->method = method;
2165 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2166 connman_ipaddress_set_ipv4(ipconfig->address,
2167 address, netmask, gateway);
2169 return connman_ipaddress_set_ipv6(
2170 ipconfig->address, address,
2171 prefix_length, gateway);
2175 case CONNMAN_IPCONFIG_METHOD_DHCP:
2176 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2179 ipconfig->method = method;
2186 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2187 DBusMessageIter *iter)
2189 struct connman_ipdevice *ipdevice;
2190 const char *method = "auto";
2192 connman_dbus_dict_append_basic(iter, "Method",
2193 DBUS_TYPE_STRING, &method);
2195 ipdevice = g_hash_table_lookup(ipdevice_hash,
2196 GINT_TO_POINTER(ipconfig->index));
2200 if (ipconfig->index >= 0) {
2201 char *ifname = connman_inet_ifname(ipconfig->index);
2203 connman_dbus_dict_append_basic(iter, "Interface",
2204 DBUS_TYPE_STRING, &ifname);
2209 if (ipdevice->address)
2210 connman_dbus_dict_append_basic(iter, "Address",
2211 DBUS_TYPE_STRING, &ipdevice->address);
2213 if (ipdevice->mtu > 0)
2214 connman_dbus_dict_append_basic(iter, "MTU",
2215 DBUS_TYPE_UINT16, &ipdevice->mtu);
2218 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2219 GKeyFile *keyfile, const char *identifier, const char *prefix)
2225 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2227 key = g_strdup_printf("%smethod", prefix);
2228 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2230 switch (ipconfig->type) {
2231 case CONNMAN_IPCONFIG_TYPE_IPV4:
2232 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2234 case CONNMAN_IPCONFIG_TYPE_IPV6:
2235 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2237 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2238 case CONNMAN_IPCONFIG_TYPE_ALL:
2239 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2243 ipconfig->method = __connman_ipconfig_string2method(method);
2245 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2246 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2248 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2252 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2253 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2256 pprefix = g_strdup_printf("%sprivacy", prefix);
2257 privacy = g_key_file_get_string(keyfile, identifier,
2259 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2264 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2265 ipconfig->last_dhcpv6_prefixes =
2266 g_key_file_get_string_list(keyfile, identifier, pprefix,
2268 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2269 g_free(ipconfig->last_dhcpv6_prefixes);
2270 ipconfig->last_dhcpv6_prefixes = NULL;
2278 switch (ipconfig->method) {
2279 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2280 case CONNMAN_IPCONFIG_METHOD_OFF:
2283 case CONNMAN_IPCONFIG_METHOD_FIXED:
2284 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2286 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2287 ipconfig->address->prefixlen = g_key_file_get_integer(
2288 keyfile, identifier, key, NULL);
2291 key = g_strdup_printf("%slocal_address", prefix);
2292 g_free(ipconfig->address->local);
2293 ipconfig->address->local = g_key_file_get_string(
2294 keyfile, identifier, key, NULL);
2297 key = g_strdup_printf("%speer_address", prefix);
2298 g_free(ipconfig->address->peer);
2299 ipconfig->address->peer = g_key_file_get_string(
2300 keyfile, identifier, key, NULL);
2303 key = g_strdup_printf("%sbroadcast_address", prefix);
2304 g_free(ipconfig->address->broadcast);
2305 ipconfig->address->broadcast = g_key_file_get_string(
2306 keyfile, identifier, key, NULL);
2309 key = g_strdup_printf("%sgateway", prefix);
2310 g_free(ipconfig->address->gateway);
2311 ipconfig->address->gateway = g_key_file_get_string(
2312 keyfile, identifier, key, NULL);
2316 case CONNMAN_IPCONFIG_METHOD_DHCP:
2318 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2319 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2321 g_free(ipconfig->last_dhcp_address);
2322 ipconfig->last_dhcp_address = str;
2328 case CONNMAN_IPCONFIG_METHOD_AUTO:
2335 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2336 GKeyFile *keyfile, const char *identifier, const char *prefix)
2341 method = __connman_ipconfig_method2string(ipconfig->method);
2343 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2346 key = g_strdup_printf("%smethod", prefix);
2347 g_key_file_set_string(keyfile, identifier, key, method);
2351 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2352 const char *privacy;
2353 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2354 key = g_strdup_printf("%sprivacy", prefix);
2355 g_key_file_set_string(keyfile, identifier, key, privacy);
2358 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2359 if (ipconfig->last_dhcp_address &&
2360 strlen(ipconfig->last_dhcp_address) > 0)
2361 g_key_file_set_string(keyfile, identifier, key,
2362 ipconfig->last_dhcp_address);
2364 g_key_file_remove_key(keyfile, identifier, key, NULL);
2367 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2368 if (ipconfig->last_dhcpv6_prefixes &&
2369 ipconfig->last_dhcpv6_prefixes[0]) {
2371 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2373 g_key_file_set_string_list(keyfile, identifier, key,
2374 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2377 g_key_file_remove_key(keyfile, identifier, key, NULL);
2381 switch (ipconfig->method) {
2382 case CONNMAN_IPCONFIG_METHOD_FIXED:
2383 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2385 case CONNMAN_IPCONFIG_METHOD_DHCP:
2386 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2387 if (ipconfig->last_dhcp_address &&
2388 strlen(ipconfig->last_dhcp_address) > 0)
2389 g_key_file_set_string(keyfile, identifier, key,
2390 ipconfig->last_dhcp_address);
2392 g_key_file_remove_key(keyfile, identifier, key, NULL);
2395 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2396 case CONNMAN_IPCONFIG_METHOD_OFF:
2397 case CONNMAN_IPCONFIG_METHOD_AUTO:
2401 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2402 if (ipconfig->address->prefixlen != 0)
2403 g_key_file_set_integer(keyfile, identifier,
2404 key, ipconfig->address->prefixlen);
2407 key = g_strdup_printf("%slocal_address", prefix);
2408 if (ipconfig->address->local)
2409 g_key_file_set_string(keyfile, identifier,
2410 key, ipconfig->address->local);
2413 key = g_strdup_printf("%speer_address", prefix);
2414 if (ipconfig->address->peer)
2415 g_key_file_set_string(keyfile, identifier,
2416 key, ipconfig->address->peer);
2419 key = g_strdup_printf("%sbroadcast_address", prefix);
2420 if (ipconfig->address->broadcast)
2421 g_key_file_set_string(keyfile, identifier,
2422 key, ipconfig->address->broadcast);
2425 key = g_strdup_printf("%sgateway", prefix);
2426 if (ipconfig->address->gateway)
2427 g_key_file_set_string(keyfile, identifier,
2428 key, ipconfig->address->gateway);
2434 int __connman_ipconfig_init(void)
2438 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2439 NULL, free_ipdevice);
2441 is_ipv6_supported = connman_inet_is_ipv6_supported();
2446 void __connman_ipconfig_cleanup(void)
2450 g_hash_table_destroy(ipdevice_hash);
2451 ipdevice_hash = NULL;