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);
414 #if defined TIZEN_EXT
415 if (ipdevice->config_ipv6 != NULL &&
416 ipdevice->config_ipv6->enabled == TRUE)
419 char *ifname = connman_inet_ifname(ipdevice->index);
421 if (__connman_device_isfiltered(ifname) == FALSE) {
422 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
423 set_ipv6_state(ifname, FALSE);
429 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
431 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
432 ipdevice->config_ipv6);
434 if (ipdevice->config_ipv4)
435 connman_inet_clear_address(ipdevice->index,
436 ipdevice->config_ipv4->address);
438 if (ipdevice->config_ipv6)
439 connman_inet_clear_ipv6_address(ipdevice->index,
440 ipdevice->config_ipv6->address->local,
441 ipdevice->config_ipv6->address->prefixlen);
444 static void update_stats(struct connman_ipdevice *ipdevice,
445 const char *ifname, struct rtnl_link_stats *stats)
447 struct connman_service *service;
449 if (stats->rx_packets == 0 && stats->tx_packets == 0)
452 connman_info("%s {RX} %u packets %u bytes", ifname,
453 stats->rx_packets, stats->rx_bytes);
454 connman_info("%s {TX} %u packets %u bytes", ifname,
455 stats->tx_packets, stats->tx_bytes);
457 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
460 service = __connman_service_lookup_from_index(ipdevice->index);
462 DBG("service %p", service);
467 ipdevice->rx_packets = stats->rx_packets;
468 ipdevice->tx_packets = stats->tx_packets;
469 ipdevice->rx_bytes = stats->rx_bytes;
470 ipdevice->tx_bytes = stats->tx_bytes;
471 ipdevice->rx_errors = stats->rx_errors;
472 ipdevice->tx_errors = stats->tx_errors;
473 ipdevice->rx_dropped = stats->rx_dropped;
474 ipdevice->tx_dropped = stats->tx_dropped;
476 __connman_service_notify(service,
477 ipdevice->rx_packets, ipdevice->tx_packets,
478 ipdevice->rx_bytes, ipdevice->tx_bytes,
479 ipdevice->rx_errors, ipdevice->tx_errors,
480 ipdevice->rx_dropped, ipdevice->tx_dropped);
483 void __connman_ipconfig_newlink(int index, unsigned short type,
484 unsigned int flags, const char *address,
486 struct rtnl_link_stats *stats)
488 struct connman_ipdevice *ipdevice;
489 GList *list, *ipconfig_copy;
491 bool up = false, down = false;
492 bool lower_up = false, lower_down = false;
495 DBG("index %d", index);
497 if (type == ARPHRD_LOOPBACK)
500 ifname = connman_inet_ifname(index);
502 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
506 ipdevice = g_try_new0(struct connman_ipdevice, 1);
510 ipdevice->index = index;
511 ipdevice->type = type;
513 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
514 ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
516 ipdevice->address = g_strdup(address);
518 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
520 connman_info("%s {create} index %d type %d <%s>", ifname,
521 index, type, type2str(type));
524 #if defined TIZEN_EXT
525 if (g_strcmp0(ipdevice->address, address) != 0) {
526 /* If an original address is built-in physical device,
527 * it's hardly get an address at a initial creation
529 g_free(ipdevice->address);
530 ipdevice->address = g_strdup(address);
536 update_stats(ipdevice, ifname, stats);
538 if (flags == ipdevice->flags)
541 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
548 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
549 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
550 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
551 (IFF_RUNNING | IFF_LOWER_UP))
553 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
557 ipdevice->flags = flags;
559 str = g_string_new(NULL);
564 g_string_append(str, "UP");
566 g_string_append(str, "DOWN");
568 if (flags & IFF_RUNNING)
569 g_string_append(str, ",RUNNING");
571 if (flags & IFF_LOWER_UP)
572 g_string_append(str, ",LOWER_UP");
574 connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
576 g_string_free(str, TRUE);
578 ipconfig_copy = g_list_copy(ipconfig_list);
580 for (list = g_list_first(ipconfig_copy); list;
581 list = g_list_next(list)) {
582 struct connman_ipconfig *ipconfig = list->data;
584 if (index != ipconfig->index)
590 if (up && ipconfig->ops->up)
591 ipconfig->ops->up(ipconfig, ifname);
592 if (lower_up && ipconfig->ops->lower_up)
593 ipconfig->ops->lower_up(ipconfig, ifname);
595 if (lower_down && ipconfig->ops->lower_down)
596 ipconfig->ops->lower_down(ipconfig, ifname);
597 if (down && ipconfig->ops->down)
598 ipconfig->ops->down(ipconfig, ifname);
601 g_list_free(ipconfig_copy);
604 __connman_ipconfig_lower_up(ipdevice);
606 __connman_ipconfig_lower_down(ipdevice);
612 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
614 struct connman_ipdevice *ipdevice;
618 DBG("index %d", index);
620 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
624 ifname = connman_inet_ifname(index);
626 update_stats(ipdevice, ifname, stats);
628 for (list = g_list_first(ipconfig_list); list;
629 list = g_list_next(list)) {
630 struct connman_ipconfig *ipconfig = list->data;
632 if (index != ipconfig->index)
635 ipconfig->index = -1;
640 if (ipconfig->ops->lower_down)
641 ipconfig->ops->lower_down(ipconfig, ifname);
642 if (ipconfig->ops->down)
643 ipconfig->ops->down(ipconfig, ifname);
648 __connman_ipconfig_lower_down(ipdevice);
650 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
653 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
655 const struct connman_ipaddress *addr1 = a;
656 const struct connman_ipaddress *addr2 = b;
658 if (addr1->prefixlen != addr2->prefixlen)
659 return addr2->prefixlen - addr1->prefixlen;
661 return g_strcmp0(addr1->local, addr2->local);
664 int __connman_ipconfig_newaddr(int index, int family, const char *label,
665 unsigned char prefixlen, const char *address)
667 struct connman_ipdevice *ipdevice;
668 struct connman_ipaddress *ipaddress;
669 enum connman_ipconfig_type type;
673 DBG("index %d", index);
675 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
679 ipaddress = connman_ipaddress_alloc(family);
683 ipaddress->prefixlen = prefixlen;
684 ipaddress->local = g_strdup(address);
686 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
687 check_duplicate_address)) {
688 connman_ipaddress_free(ipaddress);
692 if (family == AF_INET)
693 type = CONNMAN_IPCONFIG_TYPE_IPV4;
694 else if (family == AF_INET6)
695 type = CONNMAN_IPCONFIG_TYPE_IPV6;
699 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
702 ifname = connman_inet_ifname(index);
703 connman_info("%s {add} address %s/%u label %s family %d",
704 ifname, address, prefixlen, label, family);
706 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
707 __connman_ippool_newaddr(index, address, prefixlen);
709 if (ipdevice->config_ipv4 && family == AF_INET)
710 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
713 else if (ipdevice->config_ipv6 && family == AF_INET6)
714 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
719 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
722 for (list = g_list_first(ipconfig_list); list;
723 list = g_list_next(list)) {
724 struct connman_ipconfig *ipconfig = list->data;
726 if (index != ipconfig->index)
729 if (type != ipconfig->type)
735 if (ipconfig->ops->ip_bound)
736 ipconfig->ops->ip_bound(ipconfig, ifname);
744 void __connman_ipconfig_deladdr(int index, int family, const char *label,
745 unsigned char prefixlen, const char *address)
747 struct connman_ipdevice *ipdevice;
748 struct connman_ipaddress *ipaddress;
749 enum connman_ipconfig_type type;
753 DBG("index %d", index);
755 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
759 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
763 if (family == AF_INET)
764 type = CONNMAN_IPCONFIG_TYPE_IPV4;
765 else if (family == AF_INET6)
766 type = CONNMAN_IPCONFIG_TYPE_IPV6;
770 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
773 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
774 __connman_ippool_deladdr(index, address, prefixlen);
776 connman_ipaddress_clear(ipaddress);
779 ifname = connman_inet_ifname(index);
780 connman_info("%s {del} address %s/%u label %s", ifname,
781 address, prefixlen, label);
783 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
786 if (g_slist_length(ipdevice->address_list) > 0)
789 for (list = g_list_first(ipconfig_list); list;
790 list = g_list_next(list)) {
791 struct connman_ipconfig *ipconfig = list->data;
793 if (index != ipconfig->index)
796 if (type != ipconfig->type)
802 if (ipconfig->ops->ip_release)
803 ipconfig->ops->ip_release(ipconfig, ifname);
810 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
811 const char *dst, const char *gateway)
813 struct connman_ipdevice *ipdevice;
816 DBG("index %d", index);
818 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
822 ifname = connman_inet_ifname(index);
824 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
825 g_strcmp0(dst, "::") == 0)) {
827 enum connman_ipconfig_type type;
829 if (family == AF_INET6) {
830 type = CONNMAN_IPCONFIG_TYPE_IPV6;
831 g_free(ipdevice->ipv6_gateway);
832 ipdevice->ipv6_gateway = g_strdup(gateway);
834 if (ipdevice->config_ipv6 &&
835 ipdevice->config_ipv6->system) {
836 g_free(ipdevice->config_ipv6->system->gateway);
837 ipdevice->config_ipv6->system->gateway =
840 } else if (family == AF_INET) {
841 type = CONNMAN_IPCONFIG_TYPE_IPV4;
842 g_free(ipdevice->ipv4_gateway);
843 ipdevice->ipv4_gateway = g_strdup(gateway);
845 if (ipdevice->config_ipv4 &&
846 ipdevice->config_ipv4->system) {
847 g_free(ipdevice->config_ipv4->system->gateway);
848 ipdevice->config_ipv4->system->gateway =
854 for (config_list = g_list_first(ipconfig_list); config_list;
855 config_list = g_list_next(config_list)) {
856 struct connman_ipconfig *ipconfig = config_list->data;
858 if (index != ipconfig->index)
861 if (type != ipconfig->type)
867 if (ipconfig->ops->route_set)
868 ipconfig->ops->route_set(ipconfig, ifname);
872 connman_info("%s {add} route %s gw %s scope %u <%s>",
873 ifname, dst, gateway, scope, scope2str(scope));
879 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
880 const char *dst, const char *gateway)
882 struct connman_ipdevice *ipdevice;
885 DBG("index %d", index);
887 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
891 ifname = connman_inet_ifname(index);
893 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
894 g_strcmp0(dst, "::") == 0)) {
896 enum connman_ipconfig_type type;
898 if (family == AF_INET6) {
899 type = CONNMAN_IPCONFIG_TYPE_IPV6;
900 g_free(ipdevice->ipv6_gateway);
901 ipdevice->ipv6_gateway = NULL;
903 if (ipdevice->config_ipv6 &&
904 ipdevice->config_ipv6->system) {
905 g_free(ipdevice->config_ipv6->system->gateway);
906 ipdevice->config_ipv6->system->gateway = NULL;
908 } else if (family == AF_INET) {
909 type = CONNMAN_IPCONFIG_TYPE_IPV4;
910 g_free(ipdevice->ipv4_gateway);
911 ipdevice->ipv4_gateway = NULL;
913 if (ipdevice->config_ipv4 &&
914 ipdevice->config_ipv4->system) {
915 g_free(ipdevice->config_ipv4->system->gateway);
916 ipdevice->config_ipv4->system->gateway = NULL;
921 for (config_list = g_list_first(ipconfig_list); config_list;
922 config_list = g_list_next(config_list)) {
923 struct connman_ipconfig *ipconfig = config_list->data;
925 if (index != ipconfig->index)
928 if (type != ipconfig->type)
934 if (ipconfig->ops->route_unset)
935 ipconfig->ops->route_unset(ipconfig, ifname);
939 connman_info("%s {del} route %s gw %s scope %u <%s>",
940 ifname, dst, gateway, scope, scope2str(scope));
946 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
951 keys = g_hash_table_get_keys(ipdevice_hash);
955 for (list = g_list_first(keys); list; list = g_list_next(list)) {
956 int index = GPOINTER_TO_INT(list->data);
958 function(index, user_data);
964 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
965 struct connman_ipconfig *ipconfig)
967 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
970 unsigned short __connman_ipconfig_get_type_from_index(int index)
972 struct connman_ipdevice *ipdevice;
974 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
978 return ipdevice->type;
981 unsigned int __connman_ipconfig_get_flags_from_index(int index)
983 struct connman_ipdevice *ipdevice;
985 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
989 return ipdevice->flags;
992 const char *__connman_ipconfig_get_gateway_from_index(int index,
993 enum connman_ipconfig_type type)
995 struct connman_ipdevice *ipdevice;
997 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1001 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1002 if (ipdevice->ipv4_gateway)
1003 return ipdevice->ipv4_gateway;
1005 if (ipdevice->config_ipv4 &&
1006 ipdevice->config_ipv4->address)
1007 return ipdevice->config_ipv4->address->gateway;
1010 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1011 if (ipdevice->ipv6_gateway)
1012 return ipdevice->ipv6_gateway;
1014 if (ipdevice->config_ipv6 &&
1015 ipdevice->config_ipv6->address)
1016 return ipdevice->config_ipv6->address->gateway;
1022 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1024 ipconfig->index = index;
1027 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1029 if (!ipconfig->address)
1032 return ipconfig->address->local;
1035 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1036 const char *address)
1038 if (!ipconfig->address)
1041 g_free(ipconfig->address->local);
1042 ipconfig->address->local = g_strdup(address);
1045 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1047 if (!ipconfig->address)
1050 return ipconfig->address->peer;
1053 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1054 const char *address)
1056 if (!ipconfig->address)
1059 g_free(ipconfig->address->peer);
1060 ipconfig->address->peer = g_strdup(address);
1063 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1065 if (!ipconfig->address)
1068 return ipconfig->address->broadcast;
1071 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1072 const char *broadcast)
1074 if (!ipconfig->address)
1077 g_free(ipconfig->address->broadcast);
1078 ipconfig->address->broadcast = g_strdup(broadcast);
1081 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1083 if (!ipconfig->address)
1086 return ipconfig->address->gateway;
1089 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1090 const char *gateway)
1094 if (!ipconfig->address)
1096 g_free(ipconfig->address->gateway);
1097 ipconfig->address->gateway = g_strdup(gateway);
1100 #if defined TIZEN_EXT
1101 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1103 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1106 #if !defined TIZEN_EXT
1107 struct connman_service *service;
1112 if (!ipconfig->address)
1115 #if !defined TIZEN_EXT
1116 service = __connman_service_lookup_from_index(ipconfig->index);
1121 DBG("type %d gw %s peer %s", ipconfig->type,
1122 ipconfig->address->gateway, ipconfig->address->peer);
1124 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1125 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1126 return __connman_connection_gateway_add(service,
1127 ipconfig->address->gateway,
1129 ipconfig->address->peer);
1134 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1136 struct connman_service *service;
1140 service = __connman_service_lookup_from_index(ipconfig->index);
1142 __connman_connection_gateway_remove(service, ipconfig->type);
1145 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1147 if (!ipconfig->address)
1150 return ipconfig->address->prefixlen;
1153 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1154 unsigned char prefixlen)
1156 if (!ipconfig->address)
1159 ipconfig->address->prefixlen = prefixlen;
1162 static struct connman_ipconfig *create_ipv6config(int index)
1164 struct connman_ipconfig *ipv6config;
1165 struct connman_ipdevice *ipdevice;
1167 DBG("index %d", index);
1169 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1173 ipv6config->refcount = 1;
1175 ipv6config->index = index;
1176 ipv6config->enabled = false;
1177 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1179 if (!is_ipv6_supported)
1180 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1182 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1184 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1186 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1188 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1189 if (!ipv6config->address) {
1194 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1196 DBG("ipconfig %p method %s", ipv6config,
1197 __connman_ipconfig_method2string(ipv6config->method));
1203 * connman_ipconfig_create:
1205 * Allocate a new ipconfig structure.
1207 * Returns: a newly-allocated #connman_ipconfig structure
1209 struct connman_ipconfig *__connman_ipconfig_create(int index,
1210 enum connman_ipconfig_type type)
1212 struct connman_ipconfig *ipconfig;
1214 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1215 return create_ipv6config(index);
1217 DBG("index %d", index);
1219 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1223 ipconfig->refcount = 1;
1225 ipconfig->index = index;
1226 ipconfig->enabled = false;
1227 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1229 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1230 if (!ipconfig->address) {
1235 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1237 DBG("ipconfig %p", ipconfig);
1244 * connman_ipconfig_ref:
1245 * @ipconfig: ipconfig structure
1247 * Increase reference counter of ipconfig
1249 struct connman_ipconfig *
1250 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1251 const char *file, int line, const char *caller)
1253 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1254 file, line, caller);
1256 __sync_fetch_and_add(&ipconfig->refcount, 1);
1262 * connman_ipconfig_unref:
1263 * @ipconfig: ipconfig structure
1265 * Decrease reference counter of ipconfig
1267 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1268 const char *file, int line, const char *caller)
1273 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1274 file, line, caller);
1276 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1279 if (__connman_ipconfig_disable(ipconfig) < 0)
1280 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1282 __connman_ipconfig_set_ops(ipconfig, NULL);
1284 connman_ipaddress_free(ipconfig->system);
1285 connman_ipaddress_free(ipconfig->address);
1286 g_free(ipconfig->last_dhcp_address);
1287 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1292 * connman_ipconfig_get_data:
1293 * @ipconfig: ipconfig structure
1295 * Get private data pointer
1297 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1302 return ipconfig->ops_data;
1306 * connman_ipconfig_set_data:
1307 * @ipconfig: ipconfig structure
1308 * @data: data pointer
1310 * Set private data pointer
1312 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1314 ipconfig->ops_data = data;
1318 * connman_ipconfig_get_index:
1319 * @ipconfig: ipconfig structure
1321 * Get interface index
1323 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1328 return ipconfig->index;
1332 * connman_ipconfig_set_ops:
1333 * @ipconfig: ipconfig structure
1334 * @ops: operation callbacks
1336 * Set the operation callbacks
1338 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1339 const struct connman_ipconfig_ops *ops)
1341 ipconfig->ops = ops;
1345 * connman_ipconfig_set_method:
1346 * @ipconfig: ipconfig structure
1347 * @method: configuration method
1349 * Set the configuration method
1351 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1352 enum connman_ipconfig_method method)
1354 ipconfig->method = method;
1359 enum connman_ipconfig_method __connman_ipconfig_get_method(
1360 struct connman_ipconfig *ipconfig)
1363 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1365 return ipconfig->method;
1368 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1372 switch (ipconfig->method) {
1373 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1374 case CONNMAN_IPCONFIG_METHOD_OFF:
1376 case CONNMAN_IPCONFIG_METHOD_AUTO:
1377 case CONNMAN_IPCONFIG_METHOD_FIXED:
1378 case CONNMAN_IPCONFIG_METHOD_DHCP:
1379 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1380 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1381 return connman_inet_set_address(ipconfig->index,
1383 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1384 return connman_inet_set_ipv6_address(
1385 ipconfig->index, ipconfig->address);
1391 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1400 DBG("method %d", ipconfig->method);
1402 switch (ipconfig->method) {
1403 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1404 case CONNMAN_IPCONFIG_METHOD_OFF:
1406 case CONNMAN_IPCONFIG_METHOD_AUTO:
1407 case CONNMAN_IPCONFIG_METHOD_FIXED:
1408 case CONNMAN_IPCONFIG_METHOD_DHCP:
1409 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1410 err = __connman_ipconfig_address_unset(ipconfig);
1411 connman_ipaddress_clear(ipconfig->address);
1419 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1428 DBG("method %d", ipconfig->method);
1430 switch (ipconfig->method) {
1431 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1432 case CONNMAN_IPCONFIG_METHOD_OFF:
1434 case CONNMAN_IPCONFIG_METHOD_AUTO:
1435 case CONNMAN_IPCONFIG_METHOD_FIXED:
1436 case CONNMAN_IPCONFIG_METHOD_DHCP:
1437 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1438 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1439 err = connman_inet_clear_address(ipconfig->index,
1441 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1442 err = connman_inet_clear_ipv6_address(
1444 ipconfig->address->local,
1445 ipconfig->address->prefixlen);
1455 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1458 struct connman_ipdevice *ipdevice;
1460 DBG("ipconfig %p", ipconfig);
1462 if (!ipconfig || ipconfig->index < 0)
1465 ipdevice = g_hash_table_lookup(ipdevice_hash,
1466 GINT_TO_POINTER(ipconfig->index));
1470 g_free(ipdevice->pac);
1471 ipdevice->pac = g_strdup(url);
1476 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1478 struct connman_ipdevice *ipdevice;
1480 DBG("ipconfig %p", ipconfig);
1482 if (!ipconfig || ipconfig->index < 0)
1485 ipdevice = g_hash_table_lookup(ipdevice_hash,
1486 GINT_TO_POINTER(ipconfig->index));
1490 return ipdevice->pac;
1493 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1494 const char *address)
1499 g_free(ipconfig->last_dhcp_address);
1500 ipconfig->last_dhcp_address = g_strdup(address);
1503 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1508 return ipconfig->last_dhcp_address;
1511 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1517 g_strfreev(ipconfig->last_dhcpv6_prefixes);
1518 ipconfig->last_dhcpv6_prefixes = prefixes;
1521 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1526 return ipconfig->last_dhcpv6_prefixes;
1529 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1531 struct connman_ipdevice *ipdevice;
1536 ipdevice = g_hash_table_lookup(ipdevice_hash,
1537 GINT_TO_POINTER(ipconfig->index));
1541 ifname = connman_inet_ifname(ipconfig->index);
1543 set_ipv6_state(ifname, false);
1548 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1550 struct connman_ipdevice *ipdevice;
1555 ipdevice = g_hash_table_lookup(ipdevice_hash,
1556 GINT_TO_POINTER(ipconfig->index));
1560 ifname = connman_inet_ifname(ipconfig->index);
1562 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1563 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1565 set_ipv6_state(ifname, true);
1570 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1572 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1575 enable_ipv6(ipconfig);
1578 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1580 if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1583 disable_ipv6(ipconfig);
1586 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1591 switch (ipconfig->method) {
1592 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1593 case CONNMAN_IPCONFIG_METHOD_OFF:
1595 case CONNMAN_IPCONFIG_METHOD_AUTO:
1596 case CONNMAN_IPCONFIG_METHOD_FIXED:
1597 case CONNMAN_IPCONFIG_METHOD_DHCP:
1598 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1605 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1607 struct connman_ipdevice *ipdevice;
1608 bool up = false, down = false;
1609 bool lower_up = false, lower_down = false;
1610 enum connman_ipconfig_type type;
1613 DBG("ipconfig %p", ipconfig);
1615 if (!ipconfig || ipconfig->index < 0)
1618 ipdevice = g_hash_table_lookup(ipdevice_hash,
1619 GINT_TO_POINTER(ipconfig->index));
1623 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1624 if (ipdevice->config_ipv4 == ipconfig)
1626 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1627 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1628 if (ipdevice->config_ipv6 == ipconfig)
1630 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1634 ipconfig->enabled = true;
1636 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1637 ipdevice->config_ipv4) {
1638 ipconfig_list = g_list_remove(ipconfig_list,
1639 ipdevice->config_ipv4);
1641 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1643 __connman_ipconfig_unref(ipdevice->config_ipv4);
1646 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1647 ipdevice->config_ipv6) {
1648 ipconfig_list = g_list_remove(ipconfig_list,
1649 ipdevice->config_ipv6);
1651 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1653 __connman_ipconfig_unref(ipdevice->config_ipv6);
1656 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1657 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1658 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1659 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1661 enable_ipv6(ipdevice->config_ipv6);
1663 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1665 if (ipdevice->flags & IFF_UP)
1670 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1671 (IFF_RUNNING | IFF_LOWER_UP))
1673 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1676 ifname = connman_inet_ifname(ipconfig->index);
1678 if (up && ipconfig->ops->up)
1679 ipconfig->ops->up(ipconfig, ifname);
1680 if (lower_up && ipconfig->ops->lower_up)
1681 ipconfig->ops->lower_up(ipconfig, ifname);
1683 if (lower_down && ipconfig->ops->lower_down)
1684 ipconfig->ops->lower_down(ipconfig, ifname);
1685 if (down && ipconfig->ops->down)
1686 ipconfig->ops->down(ipconfig, ifname);
1693 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1695 struct connman_ipdevice *ipdevice;
1697 DBG("ipconfig %p", ipconfig);
1699 if (!ipconfig || ipconfig->index < 0)
1702 ipdevice = g_hash_table_lookup(ipdevice_hash,
1703 GINT_TO_POINTER(ipconfig->index));
1707 if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1710 ipconfig->enabled = false;
1712 if (ipdevice->config_ipv4 == ipconfig) {
1713 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1715 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1716 __connman_ipconfig_unref(ipdevice->config_ipv4);
1717 ipdevice->config_ipv4 = NULL;
1721 if (ipdevice->config_ipv6 == ipconfig) {
1722 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1724 #if defined TIZEN_EXT
1725 if (ipdevice->config_ipv6->method ==
1726 CONNMAN_IPCONFIG_METHOD_AUTO)
1727 disable_ipv6(ipdevice->config_ipv6);
1729 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1730 __connman_ipconfig_unref(ipdevice->config_ipv6);
1731 ipdevice->config_ipv6 = NULL;
1738 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1741 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1743 case CONNMAN_IPCONFIG_METHOD_OFF:
1745 case CONNMAN_IPCONFIG_METHOD_FIXED:
1747 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1749 case CONNMAN_IPCONFIG_METHOD_DHCP:
1751 case CONNMAN_IPCONFIG_METHOD_AUTO:
1758 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1760 if (g_strcmp0(method, "off") == 0)
1761 return CONNMAN_IPCONFIG_METHOD_OFF;
1762 else if (g_strcmp0(method, "fixed") == 0)
1763 return CONNMAN_IPCONFIG_METHOD_FIXED;
1764 else if (g_strcmp0(method, "manual") == 0)
1765 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1766 else if (g_strcmp0(method, "dhcp") == 0)
1767 return CONNMAN_IPCONFIG_METHOD_DHCP;
1768 else if (g_strcmp0(method, "auto") == 0)
1769 return CONNMAN_IPCONFIG_METHOD_AUTO;
1771 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1774 static const char *privacy2string(int privacy)
1778 else if (privacy == 1)
1784 static int string2privacy(const char *privacy)
1786 if (g_strcmp0(privacy, "disabled") == 0)
1788 else if (g_strcmp0(privacy, "enabled") == 0)
1790 else if (g_strcmp0(privacy, "preferred") == 0)
1792 else if (g_strcmp0(privacy, "prefered") == 0)
1798 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1800 struct connman_ipdevice *ipdevice;
1806 ipdevice = g_hash_table_lookup(ipdevice_hash,
1807 GINT_TO_POINTER(ipconfig->index));
1811 err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1812 ipdevice->ipv6_privacy));
1817 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1825 DBG("ipconfig %p privacy %s", ipconfig, value);
1827 privacy = string2privacy(value);
1829 ipconfig->ipv6_privacy_config = privacy;
1831 enable_ipv6(ipconfig);
1836 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1837 DBusMessageIter *iter)
1839 struct connman_ipaddress *append_addr = NULL;
1842 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1845 str = __connman_ipconfig_method2string(ipconfig->method);
1849 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1851 switch (ipconfig->method) {
1852 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1853 case CONNMAN_IPCONFIG_METHOD_OFF:
1854 case CONNMAN_IPCONFIG_METHOD_AUTO:
1857 case CONNMAN_IPCONFIG_METHOD_FIXED:
1858 append_addr = ipconfig->address;
1861 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1862 case CONNMAN_IPCONFIG_METHOD_DHCP:
1863 append_addr = ipconfig->system;
1864 #if defined TIZEN_EXT
1865 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1866 if (append_addr && append_addr->local == NULL)
1867 append_addr = ipconfig->address;
1875 if (append_addr->local) {
1877 struct in_addr netmask;
1880 connman_dbus_dict_append_basic(iter, "Address",
1881 DBUS_TYPE_STRING, &append_addr->local);
1883 addr = 0xffffffff << (32 - append_addr->prefixlen);
1884 netmask.s_addr = htonl(addr);
1885 mask = inet_ntoa(netmask);
1886 connman_dbus_dict_append_basic(iter, "Netmask",
1887 DBUS_TYPE_STRING, &mask);
1890 if (append_addr->gateway)
1891 connman_dbus_dict_append_basic(iter, "Gateway",
1892 DBUS_TYPE_STRING, &append_addr->gateway);
1895 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1896 DBusMessageIter *iter,
1897 struct connman_ipconfig *ipconfig_ipv4)
1899 struct connman_ipaddress *append_addr = NULL;
1900 const char *str, *privacy;
1902 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1905 str = __connman_ipconfig_method2string(ipconfig->method);
1909 if (ipconfig_ipv4 &&
1910 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1911 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1915 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1917 switch (ipconfig->method) {
1918 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1919 case CONNMAN_IPCONFIG_METHOD_OFF:
1922 case CONNMAN_IPCONFIG_METHOD_FIXED:
1923 append_addr = ipconfig->address;
1926 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1927 case CONNMAN_IPCONFIG_METHOD_DHCP:
1928 case CONNMAN_IPCONFIG_METHOD_AUTO:
1929 append_addr = ipconfig->system;
1930 #if defined TIZEN_EXT
1931 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1932 if (append_addr && append_addr->local == NULL)
1933 append_addr = ipconfig->address;
1941 if (append_addr->local) {
1942 connman_dbus_dict_append_basic(iter, "Address",
1943 DBUS_TYPE_STRING, &append_addr->local);
1944 connman_dbus_dict_append_basic(iter, "PrefixLength",
1946 &append_addr->prefixlen);
1949 if (append_addr->gateway)
1950 connman_dbus_dict_append_basic(iter, "Gateway",
1951 DBUS_TYPE_STRING, &append_addr->gateway);
1953 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1954 connman_dbus_dict_append_basic(iter, "Privacy",
1955 DBUS_TYPE_STRING, &privacy);
1958 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1959 DBusMessageIter *iter)
1961 const char *str, *privacy;
1963 str = __connman_ipconfig_method2string(ipconfig->method);
1967 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1969 switch (ipconfig->method) {
1970 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1971 case CONNMAN_IPCONFIG_METHOD_OFF:
1972 case CONNMAN_IPCONFIG_METHOD_DHCP:
1974 case CONNMAN_IPCONFIG_METHOD_FIXED:
1975 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1976 case CONNMAN_IPCONFIG_METHOD_AUTO:
1980 if (!ipconfig->address)
1983 if (ipconfig->address->local) {
1984 connman_dbus_dict_append_basic(iter, "Address",
1985 DBUS_TYPE_STRING, &ipconfig->address->local);
1986 connman_dbus_dict_append_basic(iter, "PrefixLength",
1988 &ipconfig->address->prefixlen);
1991 if (ipconfig->address->gateway)
1992 connman_dbus_dict_append_basic(iter, "Gateway",
1993 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1995 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1996 connman_dbus_dict_append_basic(iter, "Privacy",
1997 DBUS_TYPE_STRING, &privacy);
2000 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2001 DBusMessageIter *iter)
2005 str = __connman_ipconfig_method2string(ipconfig->method);
2009 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2011 switch (ipconfig->method) {
2012 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2013 case CONNMAN_IPCONFIG_METHOD_OFF:
2014 case CONNMAN_IPCONFIG_METHOD_DHCP:
2015 case CONNMAN_IPCONFIG_METHOD_AUTO:
2017 case CONNMAN_IPCONFIG_METHOD_FIXED:
2018 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2022 if (!ipconfig->address)
2025 if (ipconfig->address->local) {
2027 struct in_addr netmask;
2030 connman_dbus_dict_append_basic(iter, "Address",
2031 DBUS_TYPE_STRING, &ipconfig->address->local);
2033 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2034 netmask.s_addr = htonl(addr);
2035 mask = inet_ntoa(netmask);
2036 connman_dbus_dict_append_basic(iter, "Netmask",
2037 DBUS_TYPE_STRING, &mask);
2040 if (ipconfig->address->gateway)
2041 connman_dbus_dict_append_basic(iter, "Gateway",
2042 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2045 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2046 DBusMessageIter *array)
2048 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2049 const char *address = NULL, *netmask = NULL, *gateway = NULL,
2050 *privacy_string = NULL;
2051 int prefix_length = 0, privacy = 0;
2052 DBusMessageIter dict;
2055 DBG("ipconfig %p", ipconfig);
2057 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2060 dbus_message_iter_recurse(array, &dict);
2062 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2063 DBusMessageIter entry, value;
2067 dbus_message_iter_recurse(&dict, &entry);
2069 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2072 dbus_message_iter_get_basic(&entry, &key);
2073 dbus_message_iter_next(&entry);
2075 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2078 dbus_message_iter_recurse(&entry, &value);
2080 type = dbus_message_iter_get_arg_type(&value);
2082 if (g_str_equal(key, "Method")) {
2085 if (type != DBUS_TYPE_STRING)
2088 dbus_message_iter_get_basic(&value, &str);
2089 method = __connman_ipconfig_string2method(str);
2090 } else if (g_str_equal(key, "Address")) {
2091 if (type != DBUS_TYPE_STRING)
2094 dbus_message_iter_get_basic(&value, &address);
2095 } else if (g_str_equal(key, "PrefixLength")) {
2096 if (type != DBUS_TYPE_BYTE)
2099 dbus_message_iter_get_basic(&value, &prefix_length);
2101 if (prefix_length < 0 || prefix_length > 128)
2103 } else if (g_str_equal(key, "Netmask")) {
2104 if (type != DBUS_TYPE_STRING)
2107 dbus_message_iter_get_basic(&value, &netmask);
2108 } else if (g_str_equal(key, "Gateway")) {
2109 if (type != DBUS_TYPE_STRING)
2112 dbus_message_iter_get_basic(&value, &gateway);
2113 } else if (g_str_equal(key, "Privacy")) {
2114 if (type != DBUS_TYPE_STRING)
2117 dbus_message_iter_get_basic(&value, &privacy_string);
2118 privacy = string2privacy(privacy_string);
2121 dbus_message_iter_next(&dict);
2124 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2126 method, address, netmask, gateway, prefix_length,
2130 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2131 case CONNMAN_IPCONFIG_METHOD_FIXED:
2134 case CONNMAN_IPCONFIG_METHOD_OFF:
2135 ipconfig->method = method;
2136 #if defined TIZEN_EXT
2137 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2138 disable_ipv6(ipconfig);
2142 case CONNMAN_IPCONFIG_METHOD_AUTO:
2143 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2146 ipconfig->method = method;
2148 ipconfig->ipv6_privacy_config = privacy;
2149 #if defined TIZEN_EXT
2150 enable_ipv6(ipconfig);
2154 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2155 switch (ipconfig->type) {
2156 case CONNMAN_IPCONFIG_TYPE_IPV4:
2159 case CONNMAN_IPCONFIG_TYPE_IPV6:
2162 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2163 case CONNMAN_IPCONFIG_TYPE_ALL:
2168 if ((address && connman_inet_check_ipaddress(address)
2171 connman_inet_check_ipaddress(netmask)
2174 connman_inet_check_ipaddress(gateway)
2178 ipconfig->method = method;
2180 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2181 connman_ipaddress_set_ipv4(ipconfig->address,
2182 address, netmask, gateway);
2184 return connman_ipaddress_set_ipv6(
2185 ipconfig->address, address,
2186 prefix_length, gateway);
2190 case CONNMAN_IPCONFIG_METHOD_DHCP:
2191 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2194 ipconfig->method = method;
2201 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2202 DBusMessageIter *iter)
2204 struct connman_ipdevice *ipdevice;
2205 const char *method = "auto";
2207 connman_dbus_dict_append_basic(iter, "Method",
2208 DBUS_TYPE_STRING, &method);
2210 ipdevice = g_hash_table_lookup(ipdevice_hash,
2211 GINT_TO_POINTER(ipconfig->index));
2215 if (ipconfig->index >= 0) {
2216 char *ifname = connman_inet_ifname(ipconfig->index);
2218 connman_dbus_dict_append_basic(iter, "Interface",
2219 DBUS_TYPE_STRING, &ifname);
2224 if (ipdevice->address)
2225 connman_dbus_dict_append_basic(iter, "Address",
2226 DBUS_TYPE_STRING, &ipdevice->address);
2228 if (ipdevice->mtu > 0)
2229 connman_dbus_dict_append_basic(iter, "MTU",
2230 DBUS_TYPE_UINT16, &ipdevice->mtu);
2233 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2234 GKeyFile *keyfile, const char *identifier, const char *prefix)
2240 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2242 key = g_strdup_printf("%smethod", prefix);
2243 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2245 switch (ipconfig->type) {
2246 case CONNMAN_IPCONFIG_TYPE_IPV4:
2247 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2249 case CONNMAN_IPCONFIG_TYPE_IPV6:
2250 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2252 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2253 case CONNMAN_IPCONFIG_TYPE_ALL:
2254 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2258 ipconfig->method = __connman_ipconfig_string2method(method);
2260 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2261 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2263 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2267 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2268 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2271 pprefix = g_strdup_printf("%sprivacy", prefix);
2272 privacy = g_key_file_get_string(keyfile, identifier,
2274 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2279 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2280 ipconfig->last_dhcpv6_prefixes =
2281 g_key_file_get_string_list(keyfile, identifier, pprefix,
2283 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2284 g_free(ipconfig->last_dhcpv6_prefixes);
2285 ipconfig->last_dhcpv6_prefixes = NULL;
2293 switch (ipconfig->method) {
2294 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2295 case CONNMAN_IPCONFIG_METHOD_OFF:
2298 case CONNMAN_IPCONFIG_METHOD_FIXED:
2299 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2301 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2302 ipconfig->address->prefixlen = g_key_file_get_integer(
2303 keyfile, identifier, key, NULL);
2306 key = g_strdup_printf("%slocal_address", prefix);
2307 g_free(ipconfig->address->local);
2308 ipconfig->address->local = g_key_file_get_string(
2309 keyfile, identifier, key, NULL);
2312 key = g_strdup_printf("%speer_address", prefix);
2313 g_free(ipconfig->address->peer);
2314 ipconfig->address->peer = g_key_file_get_string(
2315 keyfile, identifier, key, NULL);
2318 key = g_strdup_printf("%sbroadcast_address", prefix);
2319 g_free(ipconfig->address->broadcast);
2320 ipconfig->address->broadcast = g_key_file_get_string(
2321 keyfile, identifier, key, NULL);
2324 key = g_strdup_printf("%sgateway", prefix);
2325 g_free(ipconfig->address->gateway);
2326 ipconfig->address->gateway = g_key_file_get_string(
2327 keyfile, identifier, key, NULL);
2331 case CONNMAN_IPCONFIG_METHOD_DHCP:
2333 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2334 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2336 g_free(ipconfig->last_dhcp_address);
2337 ipconfig->last_dhcp_address = str;
2343 case CONNMAN_IPCONFIG_METHOD_AUTO:
2350 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2351 GKeyFile *keyfile, const char *identifier, const char *prefix)
2356 method = __connman_ipconfig_method2string(ipconfig->method);
2358 DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2361 key = g_strdup_printf("%smethod", prefix);
2362 g_key_file_set_string(keyfile, identifier, key, method);
2366 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2367 const char *privacy;
2368 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2369 key = g_strdup_printf("%sprivacy", prefix);
2370 g_key_file_set_string(keyfile, identifier, key, privacy);
2373 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2374 if (ipconfig->last_dhcp_address &&
2375 strlen(ipconfig->last_dhcp_address) > 0)
2376 g_key_file_set_string(keyfile, identifier, key,
2377 ipconfig->last_dhcp_address);
2379 g_key_file_remove_key(keyfile, identifier, key, NULL);
2382 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2383 if (ipconfig->last_dhcpv6_prefixes &&
2384 ipconfig->last_dhcpv6_prefixes[0]) {
2386 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2388 g_key_file_set_string_list(keyfile, identifier, key,
2389 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2392 g_key_file_remove_key(keyfile, identifier, key, NULL);
2396 switch (ipconfig->method) {
2397 case CONNMAN_IPCONFIG_METHOD_FIXED:
2398 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2400 case CONNMAN_IPCONFIG_METHOD_DHCP:
2401 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2402 if (ipconfig->last_dhcp_address &&
2403 strlen(ipconfig->last_dhcp_address) > 0)
2404 g_key_file_set_string(keyfile, identifier, key,
2405 ipconfig->last_dhcp_address);
2407 g_key_file_remove_key(keyfile, identifier, key, NULL);
2410 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2411 case CONNMAN_IPCONFIG_METHOD_OFF:
2412 case CONNMAN_IPCONFIG_METHOD_AUTO:
2416 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2417 if (ipconfig->address->prefixlen != 0)
2418 g_key_file_set_integer(keyfile, identifier,
2419 key, ipconfig->address->prefixlen);
2422 key = g_strdup_printf("%slocal_address", prefix);
2423 if (ipconfig->address->local)
2424 g_key_file_set_string(keyfile, identifier,
2425 key, ipconfig->address->local);
2428 key = g_strdup_printf("%speer_address", prefix);
2429 if (ipconfig->address->peer)
2430 g_key_file_set_string(keyfile, identifier,
2431 key, ipconfig->address->peer);
2434 key = g_strdup_printf("%sbroadcast_address", prefix);
2435 if (ipconfig->address->broadcast)
2436 g_key_file_set_string(keyfile, identifier,
2437 key, ipconfig->address->broadcast);
2440 key = g_strdup_printf("%sgateway", prefix);
2441 if (ipconfig->address->gateway)
2442 g_key_file_set_string(keyfile, identifier,
2443 key, ipconfig->address->gateway);
2449 int __connman_ipconfig_init(void)
2453 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2454 NULL, free_ipdevice);
2456 is_ipv6_supported = connman_inet_is_ipv6_supported();
2461 void __connman_ipconfig_cleanup(void)
2465 g_hash_table_destroy(ipdevice_hash);
2466 ipdevice_hash = NULL;