5 * Copyright (C) 2007-2012 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 struct connman_ipconfig *origin;
50 const struct connman_ipconfig_ops *ops;
53 connman_bool_t enabled;
54 enum connman_ipconfig_method method;
55 struct connman_ipaddress *address;
56 struct connman_ipaddress *system;
58 int ipv6_privacy_config;
59 char *last_dhcp_address;
62 struct connman_ipdevice {
84 struct connman_ipconfig *config_ipv4;
85 struct connman_ipconfig *config_ipv6;
87 gboolean ipv6_enabled;
91 static GHashTable *ipdevice_hash = NULL;
92 static GList *ipconfig_list = NULL;
93 static connman_bool_t is_ipv6_supported = FALSE;
95 void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
100 connman_ipaddress_clear(ipconfig->address);
103 static void free_address_list(struct connman_ipdevice *ipdevice)
107 for (list = ipdevice->address_list; list; list = list->next) {
108 struct connman_ipaddress *ipaddress = list->data;
110 connman_ipaddress_free(ipaddress);
114 g_slist_free(ipdevice->address_list);
115 ipdevice->address_list = NULL;
118 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
119 unsigned char prefixlen, const char *local)
123 for (list = ipdevice->address_list; list; list = list->next) {
124 struct connman_ipaddress *ipaddress = list->data;
126 if (g_strcmp0(ipaddress->local, local) == 0 &&
127 ipaddress->prefixlen == prefixlen)
134 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
137 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
139 case CONNMAN_IPCONFIG_TYPE_IPV4:
141 case CONNMAN_IPCONFIG_TYPE_IPV6:
148 static const char *type2str(unsigned short type)
153 case ARPHRD_LOOPBACK:
166 static const char *scope2str(unsigned char scope)
178 static gboolean get_ipv6_state(gchar *ifname)
183 gboolean enabled = FALSE;
186 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
188 path = g_strdup_printf(
189 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
194 f = fopen(path, "r");
199 if (fscanf(f, "%d", &disabled) > 0)
207 static void set_ipv6_state(gchar *ifname, gboolean enable)
213 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
215 path = g_strdup_printf(
216 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
221 f = fopen(path, "r+");
236 static int get_ipv6_privacy(gchar *ifname)
245 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
251 f = fopen(path, "r");
258 if (fscanf(f, "%d", &value) <= 0)
266 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
267 * The privacy extension is described in RFC 3041 and RFC 4941
269 static void set_ipv6_privacy(gchar *ifname, int value)
277 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
286 f = fopen(path, "r+");
293 fprintf(f, "%d", value);
297 static int get_rp_filter()
300 int value = -EINVAL, tmp;
302 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
305 if (fscanf(f, "%d", &tmp) == 1)
313 static void set_rp_filter(int value)
317 f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
322 fprintf(f, "%d", value);
327 int __connman_ipconfig_set_rp_filter()
331 value = get_rp_filter();
338 connman_info("rp_filter set to 2 (loose mode routing), "
339 "old value was %d", value);
344 void __connman_ipconfig_unset_rp_filter(int old_value)
346 set_rp_filter(old_value);
348 connman_info("rp_filter restored to %d", old_value);
351 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
353 if (ipconfig == NULL)
356 return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
359 static void free_ipdevice(gpointer data)
361 struct connman_ipdevice *ipdevice = data;
363 connman_info("%s {remove} index %d", ipdevice->ifname,
366 if (ipdevice->config_ipv4 != NULL) {
367 __connman_ipconfig_unref(ipdevice->config_ipv4);
368 ipdevice->config_ipv4 = NULL;
371 if (ipdevice->config_ipv6 != NULL) {
372 __connman_ipconfig_unref(ipdevice->config_ipv6);
373 ipdevice->config_ipv6 = NULL;
376 free_address_list(ipdevice);
377 g_free(ipdevice->ipv4_gateway);
378 g_free(ipdevice->ipv6_gateway);
379 g_free(ipdevice->pac);
381 g_free(ipdevice->address);
383 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
384 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
386 g_free(ipdevice->ifname);
390 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
392 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
393 ipdevice->config_ipv6);
395 if (ipdevice->config_ipv6 != NULL &&
396 ipdevice->config_ipv6->enabled == TRUE)
399 if (ipdevice->ifname == NULL)
402 if (__connman_device_isfiltered(ipdevice->ifname) == FALSE) {
403 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
404 set_ipv6_state(ipdevice->ifname, FALSE);
408 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
410 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
411 ipdevice->config_ipv6);
413 if (ipdevice->config_ipv4)
414 connman_inet_clear_address(ipdevice->index,
415 ipdevice->config_ipv4->address);
417 if (ipdevice->config_ipv6)
418 connman_inet_clear_ipv6_address(ipdevice->index,
419 ipdevice->config_ipv6->address->local,
420 ipdevice->config_ipv6->address->prefixlen);
423 static void update_stats(struct connman_ipdevice *ipdevice,
424 struct rtnl_link_stats *stats)
426 struct connman_service *service;
428 if (stats->rx_packets == 0 && stats->tx_packets == 0)
431 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
432 stats->rx_packets, stats->rx_bytes);
433 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
434 stats->tx_packets, stats->tx_bytes);
436 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
439 if (ipdevice->config_ipv4)
440 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
441 else if (ipdevice->config_ipv6)
442 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
449 ipdevice->rx_packets = stats->rx_packets;
450 ipdevice->tx_packets = stats->tx_packets;
451 ipdevice->rx_bytes = stats->rx_bytes;
452 ipdevice->tx_bytes = stats->tx_bytes;
453 ipdevice->rx_errors = stats->rx_errors;
454 ipdevice->tx_errors = stats->tx_errors;
455 ipdevice->rx_dropped = stats->rx_dropped;
456 ipdevice->tx_dropped = stats->tx_dropped;
458 __connman_service_notify(service,
459 ipdevice->rx_packets, ipdevice->tx_packets,
460 ipdevice->rx_bytes, ipdevice->tx_bytes,
461 ipdevice->rx_errors, ipdevice->tx_errors,
462 ipdevice->rx_dropped, ipdevice->tx_dropped);
465 void __connman_ipconfig_newlink(int index, unsigned short type,
466 unsigned int flags, const char *address,
468 struct rtnl_link_stats *stats)
470 struct connman_ipdevice *ipdevice;
473 gboolean up = FALSE, down = FALSE;
474 gboolean lower_up = FALSE, lower_down = FALSE;
476 DBG("index %d", index);
478 if (type == ARPHRD_LOOPBACK)
481 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
482 if (ipdevice != NULL) {
483 char *ifname = connman_inet_ifname(index);
484 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
485 DBG("interface name changed %s -> %s",
486 ipdevice->ifname, ifname);
488 g_free(ipdevice->ifname);
489 ipdevice->ifname = ifname;
496 ipdevice = g_try_new0(struct connman_ipdevice, 1);
497 if (ipdevice == NULL)
500 ipdevice->index = index;
501 ipdevice->ifname = connman_inet_ifname(index);
502 ipdevice->type = type;
504 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
505 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
507 ipdevice->address = g_strdup(address);
509 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
511 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
512 index, type, type2str(type));
517 update_stats(ipdevice, stats);
519 if (flags == ipdevice->flags)
522 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
529 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
530 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
531 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
532 (IFF_RUNNING | IFF_LOWER_UP))
534 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
538 ipdevice->flags = flags;
540 str = g_string_new(NULL);
545 g_string_append(str, "UP");
547 g_string_append(str, "DOWN");
549 if (flags & IFF_RUNNING)
550 g_string_append(str, ",RUNNING");
552 if (flags & IFF_LOWER_UP)
553 g_string_append(str, ",LOWER_UP");
555 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
558 g_string_free(str, TRUE);
560 for (list = g_list_first(ipconfig_list); list;
561 list = g_list_next(list)) {
562 struct connman_ipconfig *ipconfig = list->data;
564 if (index != ipconfig->index)
567 if (ipconfig->ops == NULL)
570 if (up == TRUE && ipconfig->ops->up)
571 ipconfig->ops->up(ipconfig);
572 if (lower_up == TRUE && ipconfig->ops->lower_up)
573 ipconfig->ops->lower_up(ipconfig);
575 if (lower_down == TRUE && ipconfig->ops->lower_down)
576 ipconfig->ops->lower_down(ipconfig);
577 if (down == TRUE && ipconfig->ops->down)
578 ipconfig->ops->down(ipconfig);
582 __connman_ipconfig_lower_up(ipdevice);
584 __connman_ipconfig_lower_down(ipdevice);
587 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
589 struct connman_ipdevice *ipdevice;
592 DBG("index %d", index);
594 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
595 if (ipdevice == NULL)
598 update_stats(ipdevice, stats);
600 for (list = g_list_first(ipconfig_list); list;
601 list = g_list_next(list)) {
602 struct connman_ipconfig *ipconfig = list->data;
604 if (index != ipconfig->index)
607 ipconfig->index = -1;
609 if (ipconfig->ops == NULL)
612 if (ipconfig->ops->lower_down)
613 ipconfig->ops->lower_down(ipconfig);
614 if (ipconfig->ops->down)
615 ipconfig->ops->down(ipconfig);
618 __connman_ipconfig_lower_down(ipdevice);
620 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
623 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
625 const struct connman_ipaddress *addr1 = a;
626 const struct connman_ipaddress *addr2 = b;
628 if (addr1->prefixlen != addr2->prefixlen)
629 return addr2->prefixlen - addr1->prefixlen;
631 return g_strcmp0(addr1->local, addr2->local);
634 void __connman_ipconfig_newaddr(int index, int family, const char *label,
635 unsigned char prefixlen, const char *address)
637 struct connman_ipdevice *ipdevice;
638 struct connman_ipaddress *ipaddress;
639 enum connman_ipconfig_type type;
642 DBG("index %d", index);
644 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
645 if (ipdevice == NULL)
648 ipaddress = connman_ipaddress_alloc(family);
649 if (ipaddress == NULL)
652 ipaddress->prefixlen = prefixlen;
653 ipaddress->local = g_strdup(address);
655 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
656 check_duplicate_address)) {
657 connman_ipaddress_free(ipaddress);
661 if (family == AF_INET)
662 type = CONNMAN_IPCONFIG_TYPE_IPV4;
663 else if (family == AF_INET6)
664 type = CONNMAN_IPCONFIG_TYPE_IPV6;
668 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
671 connman_info("%s {add} address %s/%u label %s family %d",
672 ipdevice->ifname, address, prefixlen, label, family);
674 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
675 __connman_ippool_newaddr(index, address, prefixlen);
677 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
678 connman_ipaddress_copy(ipdevice->config_ipv4->system,
681 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
682 connman_ipaddress_copy(ipdevice->config_ipv6->system,
687 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
690 for (list = g_list_first(ipconfig_list); list;
691 list = g_list_next(list)) {
692 struct connman_ipconfig *ipconfig = list->data;
694 if (index != ipconfig->index)
697 if (type != ipconfig->type)
700 if (ipconfig->ops == NULL)
703 if (ipconfig->ops->ip_bound)
704 ipconfig->ops->ip_bound(ipconfig);
708 void __connman_ipconfig_deladdr(int index, int family, const char *label,
709 unsigned char prefixlen, const char *address)
711 struct connman_ipdevice *ipdevice;
712 struct connman_ipaddress *ipaddress;
713 enum connman_ipconfig_type type;
716 DBG("index %d", index);
718 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
719 if (ipdevice == NULL)
722 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
723 if (ipaddress == NULL)
726 if (family == AF_INET)
727 type = CONNMAN_IPCONFIG_TYPE_IPV4;
728 else if (family == AF_INET6)
729 type = CONNMAN_IPCONFIG_TYPE_IPV6;
733 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
736 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
737 __connman_ippool_deladdr(index, address, prefixlen);
739 connman_ipaddress_clear(ipaddress);
742 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
743 address, prefixlen, label);
745 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
748 if (g_slist_length(ipdevice->address_list) > 0)
751 for (list = g_list_first(ipconfig_list); list;
752 list = g_list_next(list)) {
753 struct connman_ipconfig *ipconfig = list->data;
755 if (index != ipconfig->index)
758 if (type != ipconfig->type)
761 if (ipconfig->ops == NULL)
764 if (ipconfig->ops->ip_release)
765 ipconfig->ops->ip_release(ipconfig);
769 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
770 const char *dst, const char *gateway)
772 struct connman_ipdevice *ipdevice;
774 DBG("index %d", index);
776 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
777 if (ipdevice == NULL)
780 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
781 g_strcmp0(dst, "::") == 0)) {
783 enum connman_ipconfig_type type;
785 if (family == AF_INET6) {
786 type = CONNMAN_IPCONFIG_TYPE_IPV6;
787 g_free(ipdevice->ipv6_gateway);
788 ipdevice->ipv6_gateway = g_strdup(gateway);
790 if (ipdevice->config_ipv6 != NULL &&
791 ipdevice->config_ipv6->system != NULL) {
792 g_free(ipdevice->config_ipv6->system->gateway);
793 ipdevice->config_ipv6->system->gateway =
796 } else if (family == AF_INET) {
797 type = CONNMAN_IPCONFIG_TYPE_IPV4;
798 g_free(ipdevice->ipv4_gateway);
799 ipdevice->ipv4_gateway = g_strdup(gateway);
801 if (ipdevice->config_ipv4 != NULL &&
802 ipdevice->config_ipv4->system != NULL) {
803 g_free(ipdevice->config_ipv4->system->gateway);
804 ipdevice->config_ipv4->system->gateway =
810 for (config_list = g_list_first(ipconfig_list); config_list;
811 config_list = g_list_next(config_list)) {
812 struct connman_ipconfig *ipconfig = config_list->data;
814 if (index != ipconfig->index)
817 if (type != ipconfig->type)
820 if (ipconfig->ops == NULL)
823 if (ipconfig->ops->route_set)
824 ipconfig->ops->route_set(ipconfig);
828 connman_info("%s {add} route %s gw %s scope %u <%s>",
829 ipdevice->ifname, dst, gateway,
830 scope, scope2str(scope));
833 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
834 const char *dst, const char *gateway)
836 struct connman_ipdevice *ipdevice;
838 DBG("index %d", index);
840 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
841 if (ipdevice == NULL)
844 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
845 g_strcmp0(dst, "::") == 0)) {
847 enum connman_ipconfig_type type;
849 if (family == AF_INET6) {
850 type = CONNMAN_IPCONFIG_TYPE_IPV6;
851 g_free(ipdevice->ipv6_gateway);
852 ipdevice->ipv6_gateway = NULL;
854 if (ipdevice->config_ipv6 != NULL &&
855 ipdevice->config_ipv6->system != NULL) {
856 g_free(ipdevice->config_ipv6->system->gateway);
857 ipdevice->config_ipv6->system->gateway = NULL;
859 } else if (family == AF_INET) {
860 type = CONNMAN_IPCONFIG_TYPE_IPV4;
861 g_free(ipdevice->ipv4_gateway);
862 ipdevice->ipv4_gateway = NULL;
864 if (ipdevice->config_ipv4 != NULL &&
865 ipdevice->config_ipv4->system != NULL) {
866 g_free(ipdevice->config_ipv4->system->gateway);
867 ipdevice->config_ipv4->system->gateway = NULL;
872 for (config_list = g_list_first(ipconfig_list); config_list;
873 config_list = g_list_next(config_list)) {
874 struct connman_ipconfig *ipconfig = config_list->data;
876 if (index != ipconfig->index)
879 if (type != ipconfig->type)
882 if (ipconfig->ops == NULL)
885 if (ipconfig->ops->route_unset)
886 ipconfig->ops->route_unset(ipconfig);
890 connman_info("%s {del} route %s gw %s scope %u <%s>",
891 ipdevice->ifname, dst, gateway,
892 scope, scope2str(scope));
895 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
900 keys = g_hash_table_get_keys(ipdevice_hash);
904 for (list = g_list_first(keys); list; list = g_list_next(list)) {
905 int index = GPOINTER_TO_INT(list->data);
907 function(index, user_data);
913 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
914 struct connman_ipconfig *ipconfig)
916 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
919 unsigned short __connman_ipconfig_get_type_from_index(int index)
921 struct connman_ipdevice *ipdevice;
923 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
924 if (ipdevice == NULL)
927 return ipdevice->type;
930 unsigned int __connman_ipconfig_get_flags_from_index(int index)
932 struct connman_ipdevice *ipdevice;
934 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
935 if (ipdevice == NULL)
938 return ipdevice->flags;
941 const char *__connman_ipconfig_get_gateway_from_index(int index,
942 enum connman_ipconfig_type type)
944 struct connman_ipdevice *ipdevice;
946 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
947 if (ipdevice == NULL)
950 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
951 if (ipdevice->ipv4_gateway != NULL)
952 return ipdevice->ipv4_gateway;
954 if (ipdevice->config_ipv4 != NULL &&
955 ipdevice->config_ipv4->address != NULL)
956 return ipdevice->config_ipv4->address->gateway;
959 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
960 if (ipdevice->ipv6_gateway != NULL)
961 return ipdevice->ipv6_gateway;
963 if (ipdevice->config_ipv6 != NULL &&
964 ipdevice->config_ipv6->address != NULL)
965 return ipdevice->config_ipv6->address->gateway;
971 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
973 ipconfig->index = index;
976 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
978 if (ipconfig->address == NULL)
981 return ipconfig->address->local;
984 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
986 if (ipconfig->address == NULL)
989 g_free(ipconfig->address->local);
990 ipconfig->address->local = g_strdup(address);
993 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
995 if (ipconfig->address == NULL)
998 return ipconfig->address->peer;
1001 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1003 if (ipconfig->address == NULL)
1006 g_free(ipconfig->address->peer);
1007 ipconfig->address->peer = g_strdup(address);
1010 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1012 if (ipconfig->address == NULL)
1015 return ipconfig->address->broadcast;
1018 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1020 if (ipconfig->address == NULL)
1023 g_free(ipconfig->address->broadcast);
1024 ipconfig->address->broadcast = g_strdup(broadcast);
1027 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1029 if (ipconfig->address == NULL)
1032 return ipconfig->address->gateway;
1035 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1039 if (ipconfig->address == NULL)
1041 g_free(ipconfig->address->gateway);
1042 ipconfig->address->gateway = g_strdup(gateway);
1045 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1047 struct connman_service *service;
1051 if (ipconfig->address == NULL)
1054 service = __connman_service_lookup_from_index(ipconfig->index);
1055 if (service == NULL)
1058 __connman_connection_gateway_remove(service, ipconfig->type);
1060 DBG("type %d gw %s peer %s", ipconfig->type,
1061 ipconfig->address->gateway, ipconfig->address->peer);
1063 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1064 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1065 return __connman_connection_gateway_add(service,
1066 ipconfig->address->gateway,
1068 ipconfig->address->peer);
1073 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1075 struct connman_service *service;
1079 service = __connman_service_lookup_from_index(ipconfig->index);
1080 if (service != NULL)
1081 __connman_connection_gateway_remove(service, ipconfig->type);
1084 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1086 if (ipconfig->address == NULL)
1089 return ipconfig->address->prefixlen;
1092 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1094 if (ipconfig->address == NULL)
1097 ipconfig->address->prefixlen = prefixlen;
1100 static struct connman_ipconfig *create_ipv6config(int index)
1102 struct connman_ipconfig *ipv6config;
1103 struct connman_ipdevice *ipdevice;
1105 DBG("index %d", index);
1107 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1108 if (ipv6config == NULL)
1111 ipv6config->refcount = 1;
1113 ipv6config->index = index;
1114 ipv6config->enabled = FALSE;
1115 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1117 if (is_ipv6_supported == FALSE)
1118 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1120 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1122 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1123 if (ipdevice != NULL)
1124 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1126 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1127 if (ipv6config->address == NULL) {
1132 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1134 DBG("ipconfig %p method %s", ipv6config,
1135 __connman_ipconfig_method2string(ipv6config->method));
1141 * connman_ipconfig_create:
1143 * Allocate a new ipconfig structure.
1145 * Returns: a newly-allocated #connman_ipconfig structure
1147 struct connman_ipconfig *__connman_ipconfig_create(int index,
1148 enum connman_ipconfig_type type)
1150 struct connman_ipconfig *ipconfig;
1152 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1153 return create_ipv6config(index);
1155 DBG("index %d", index);
1157 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1158 if (ipconfig == NULL)
1161 ipconfig->refcount = 1;
1163 ipconfig->index = index;
1164 ipconfig->enabled = FALSE;
1165 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1167 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1168 if (ipconfig->address == NULL) {
1173 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1175 DBG("ipconfig %p", ipconfig);
1182 * connman_ipconfig_ref:
1183 * @ipconfig: ipconfig structure
1185 * Increase reference counter of ipconfig
1187 struct connman_ipconfig *
1188 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1189 const char *file, int line, const char *caller)
1191 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1192 file, line, caller);
1194 __sync_fetch_and_add(&ipconfig->refcount, 1);
1200 * connman_ipconfig_unref:
1201 * @ipconfig: ipconfig structure
1203 * Decrease reference counter of ipconfig
1205 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1206 const char *file, int line, const char *caller)
1208 if (ipconfig == NULL)
1211 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1212 file, line, caller);
1214 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1217 if (__connman_ipconfig_disable(ipconfig) < 0)
1218 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1220 __connman_ipconfig_set_ops(ipconfig, NULL);
1222 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1223 __connman_ipconfig_unref(ipconfig->origin);
1224 ipconfig->origin = NULL;
1227 connman_ipaddress_free(ipconfig->system);
1228 connman_ipaddress_free(ipconfig->address);
1229 g_free(ipconfig->last_dhcp_address);
1234 * connman_ipconfig_get_data:
1235 * @ipconfig: ipconfig structure
1237 * Get private data pointer
1239 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1241 if (ipconfig == NULL)
1244 return ipconfig->ops_data;
1248 * connman_ipconfig_set_data:
1249 * @ipconfig: ipconfig structure
1250 * @data: data pointer
1252 * Set private data pointer
1254 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1256 ipconfig->ops_data = data;
1260 * connman_ipconfig_get_index:
1261 * @ipconfig: ipconfig structure
1263 * Get interface index
1265 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1267 if (ipconfig == NULL)
1270 if (ipconfig->origin != NULL)
1271 return ipconfig->origin->index;
1273 return ipconfig->index;
1277 * connman_ipconfig_get_ifname:
1278 * @ipconfig: ipconfig structure
1280 * Get interface name
1282 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1284 struct connman_ipdevice *ipdevice;
1286 if (ipconfig == NULL)
1289 if (ipconfig->index < 0)
1292 ipdevice = g_hash_table_lookup(ipdevice_hash,
1293 GINT_TO_POINTER(ipconfig->index));
1294 if (ipdevice == NULL)
1297 return ipdevice->ifname;
1301 * connman_ipconfig_set_ops:
1302 * @ipconfig: ipconfig structure
1303 * @ops: operation callbacks
1305 * Set the operation callbacks
1307 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1308 const struct connman_ipconfig_ops *ops)
1310 ipconfig->ops = ops;
1314 * connman_ipconfig_set_method:
1315 * @ipconfig: ipconfig structure
1316 * @method: configuration method
1318 * Set the configuration method
1320 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1321 enum connman_ipconfig_method method)
1323 ipconfig->method = method;
1328 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1330 if (ipconfig == NULL)
1331 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1333 return ipconfig->method;
1336 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1340 switch (ipconfig->method) {
1341 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1342 case CONNMAN_IPCONFIG_METHOD_OFF:
1344 case CONNMAN_IPCONFIG_METHOD_AUTO:
1345 case CONNMAN_IPCONFIG_METHOD_FIXED:
1346 case CONNMAN_IPCONFIG_METHOD_DHCP:
1347 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1348 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1349 return connman_inet_set_address(ipconfig->index,
1351 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1352 return connman_inet_set_ipv6_address(
1353 ipconfig->index, ipconfig->address);
1359 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1365 if (ipconfig == NULL)
1368 DBG("method %d", ipconfig->method);
1370 switch (ipconfig->method) {
1371 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1372 case CONNMAN_IPCONFIG_METHOD_OFF:
1374 case CONNMAN_IPCONFIG_METHOD_AUTO:
1375 case CONNMAN_IPCONFIG_METHOD_FIXED:
1376 case CONNMAN_IPCONFIG_METHOD_DHCP:
1377 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1378 err = __connman_ipconfig_address_unset(ipconfig);
1379 connman_ipaddress_clear(ipconfig->address);
1387 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1393 if (ipconfig == NULL)
1396 DBG("method %d", ipconfig->method);
1398 switch (ipconfig->method) {
1399 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1400 case CONNMAN_IPCONFIG_METHOD_OFF:
1402 case CONNMAN_IPCONFIG_METHOD_AUTO:
1403 case CONNMAN_IPCONFIG_METHOD_FIXED:
1404 case CONNMAN_IPCONFIG_METHOD_DHCP:
1405 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1406 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1407 err = connman_inet_clear_address(ipconfig->index,
1409 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1410 err = connman_inet_clear_ipv6_address(
1412 ipconfig->address->local,
1413 ipconfig->address->prefixlen);
1423 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1426 struct connman_ipdevice *ipdevice;
1428 DBG("ipconfig %p", ipconfig);
1430 if (ipconfig == NULL || ipconfig->index < 0)
1433 ipdevice = g_hash_table_lookup(ipdevice_hash,
1434 GINT_TO_POINTER(ipconfig->index));
1435 if (ipdevice == NULL)
1438 g_free(ipdevice->pac);
1439 ipdevice->pac = g_strdup(url);
1444 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1446 struct connman_ipdevice *ipdevice;
1448 DBG("ipconfig %p", ipconfig);
1450 if (ipconfig == NULL || ipconfig->index < 0)
1453 ipdevice = g_hash_table_lookup(ipdevice_hash,
1454 GINT_TO_POINTER(ipconfig->index));
1455 if (ipdevice == NULL)
1458 return ipdevice->pac;
1461 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1462 const char *address)
1464 if (ipconfig == NULL)
1467 g_free(ipconfig->last_dhcp_address);
1468 ipconfig->last_dhcp_address = g_strdup(address);
1471 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1473 if (ipconfig == NULL)
1476 return ipconfig->last_dhcp_address;
1479 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1481 struct connman_ipdevice *ipdevice;
1485 ipdevice = g_hash_table_lookup(ipdevice_hash,
1486 GINT_TO_POINTER(ipconfig->index));
1487 if (ipdevice == NULL)
1490 set_ipv6_state(ipdevice->ifname, FALSE);
1493 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1495 struct connman_ipdevice *ipdevice;
1499 ipdevice = g_hash_table_lookup(ipdevice_hash,
1500 GINT_TO_POINTER(ipconfig->index));
1501 if (ipdevice == NULL)
1504 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1505 set_ipv6_privacy(ipdevice->ifname,
1506 ipconfig->ipv6_privacy_config);
1508 set_ipv6_state(ipdevice->ifname, TRUE);
1511 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1513 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1516 enable_ipv6(ipconfig);
1519 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1521 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1524 disable_ipv6(ipconfig);
1527 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1529 if (ipconfig == NULL)
1532 switch (ipconfig->method) {
1533 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1534 case CONNMAN_IPCONFIG_METHOD_OFF:
1536 case CONNMAN_IPCONFIG_METHOD_AUTO:
1537 case CONNMAN_IPCONFIG_METHOD_FIXED:
1538 case CONNMAN_IPCONFIG_METHOD_DHCP:
1539 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1546 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1548 struct connman_ipdevice *ipdevice;
1549 gboolean up = FALSE, down = FALSE;
1550 gboolean lower_up = FALSE, lower_down = FALSE;
1551 enum connman_ipconfig_type type;
1553 DBG("ipconfig %p", ipconfig);
1555 if (ipconfig == NULL || ipconfig->index < 0)
1558 ipdevice = g_hash_table_lookup(ipdevice_hash,
1559 GINT_TO_POINTER(ipconfig->index));
1560 if (ipdevice == NULL)
1563 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1564 if (ipdevice->config_ipv4 == ipconfig)
1566 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1567 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1568 if (ipdevice->config_ipv6 == ipconfig)
1570 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1571 enable_ipv6(ipconfig);
1575 ipconfig->enabled = TRUE;
1577 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1578 ipdevice->config_ipv4 != NULL) {
1579 ipconfig_list = g_list_remove(ipconfig_list,
1580 ipdevice->config_ipv4);
1582 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1584 __connman_ipconfig_unref(ipdevice->config_ipv4);
1587 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1588 ipdevice->config_ipv6 != NULL) {
1589 ipconfig_list = g_list_remove(ipconfig_list,
1590 ipdevice->config_ipv6);
1592 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1594 __connman_ipconfig_unref(ipdevice->config_ipv6);
1597 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1598 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1599 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1600 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1602 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1604 if (ipdevice->flags & IFF_UP)
1609 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1610 (IFF_RUNNING | IFF_LOWER_UP))
1612 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1615 if (up == TRUE && ipconfig->ops->up)
1616 ipconfig->ops->up(ipconfig);
1617 if (lower_up == TRUE && ipconfig->ops->lower_up)
1618 ipconfig->ops->lower_up(ipconfig);
1620 if (lower_down == TRUE && ipconfig->ops->lower_down)
1621 ipconfig->ops->lower_down(ipconfig);
1622 if (down == TRUE && ipconfig->ops->down)
1623 ipconfig->ops->down(ipconfig);
1628 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1630 struct connman_ipdevice *ipdevice;
1632 DBG("ipconfig %p", ipconfig);
1634 if (ipconfig == NULL || ipconfig->index < 0)
1637 ipdevice = g_hash_table_lookup(ipdevice_hash,
1638 GINT_TO_POINTER(ipconfig->index));
1639 if (ipdevice == NULL)
1642 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1645 ipconfig->enabled = FALSE;
1647 if (ipdevice->config_ipv4 == ipconfig) {
1648 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1650 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1651 __connman_ipconfig_unref(ipdevice->config_ipv4);
1652 ipdevice->config_ipv4 = NULL;
1656 if (ipdevice->config_ipv6 == ipconfig) {
1657 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1659 if (ipdevice->config_ipv6->method ==
1660 CONNMAN_IPCONFIG_METHOD_AUTO)
1661 disable_ipv6(ipdevice->config_ipv6);
1663 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1664 __connman_ipconfig_unref(ipdevice->config_ipv6);
1665 ipdevice->config_ipv6 = NULL;
1672 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1675 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1677 case CONNMAN_IPCONFIG_METHOD_OFF:
1679 case CONNMAN_IPCONFIG_METHOD_FIXED:
1681 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1683 case CONNMAN_IPCONFIG_METHOD_DHCP:
1685 case CONNMAN_IPCONFIG_METHOD_AUTO:
1692 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1694 if (g_strcmp0(method, "off") == 0)
1695 return CONNMAN_IPCONFIG_METHOD_OFF;
1696 else if (g_strcmp0(method, "fixed") == 0)
1697 return CONNMAN_IPCONFIG_METHOD_FIXED;
1698 else if (g_strcmp0(method, "manual") == 0)
1699 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1700 else if (g_strcmp0(method, "dhcp") == 0)
1701 return CONNMAN_IPCONFIG_METHOD_DHCP;
1702 else if (g_strcmp0(method, "auto") == 0)
1703 return CONNMAN_IPCONFIG_METHOD_AUTO;
1705 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1708 static const char *privacy2string(int privacy)
1712 else if (privacy == 1)
1714 else if (privacy > 1)
1720 static int string2privacy(const char *privacy)
1722 if (g_strcmp0(privacy, "disabled") == 0)
1724 else if (g_strcmp0(privacy, "enabled") == 0)
1726 else if (g_strcmp0(privacy, "prefered") == 0)
1732 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1733 DBusMessageIter *iter)
1735 struct connman_ipaddress *append_addr = NULL;
1740 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1743 str = __connman_ipconfig_method2string(ipconfig->method);
1747 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1749 switch (ipconfig->method) {
1750 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1751 case CONNMAN_IPCONFIG_METHOD_OFF:
1752 case CONNMAN_IPCONFIG_METHOD_AUTO:
1755 case CONNMAN_IPCONFIG_METHOD_FIXED:
1756 append_addr = ipconfig->address;
1759 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1760 case CONNMAN_IPCONFIG_METHOD_DHCP:
1761 append_addr = ipconfig->system;
1765 if (append_addr == NULL)
1768 if (append_addr->local != NULL) {
1770 struct in_addr netmask;
1773 connman_dbus_dict_append_basic(iter, "Address",
1774 DBUS_TYPE_STRING, &append_addr->local);
1776 addr = 0xffffffff << (32 - append_addr->prefixlen);
1777 netmask.s_addr = htonl(addr);
1778 mask = inet_ntoa(netmask);
1779 connman_dbus_dict_append_basic(iter, "Netmask",
1780 DBUS_TYPE_STRING, &mask);
1783 if (append_addr->gateway != NULL)
1784 connman_dbus_dict_append_basic(iter, "Gateway",
1785 DBUS_TYPE_STRING, &append_addr->gateway);
1788 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1789 DBusMessageIter *iter,
1790 struct connman_ipconfig *ipconfig_ipv4)
1792 struct connman_ipaddress *append_addr = NULL;
1793 const char *str, *privacy;
1797 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1800 str = __connman_ipconfig_method2string(ipconfig->method);
1804 if (ipconfig_ipv4 != NULL &&
1805 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1806 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1810 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1812 switch (ipconfig->method) {
1813 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1814 case CONNMAN_IPCONFIG_METHOD_OFF:
1817 case CONNMAN_IPCONFIG_METHOD_FIXED:
1818 append_addr = ipconfig->address;
1821 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1822 case CONNMAN_IPCONFIG_METHOD_DHCP:
1823 case CONNMAN_IPCONFIG_METHOD_AUTO:
1824 append_addr = ipconfig->system;
1828 if (append_addr == NULL)
1831 if (append_addr->local != NULL) {
1832 connman_dbus_dict_append_basic(iter, "Address",
1833 DBUS_TYPE_STRING, &append_addr->local);
1834 connman_dbus_dict_append_basic(iter, "PrefixLength",
1836 &append_addr->prefixlen);
1839 if (append_addr->gateway != NULL)
1840 connman_dbus_dict_append_basic(iter, "Gateway",
1841 DBUS_TYPE_STRING, &append_addr->gateway);
1843 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1844 connman_dbus_dict_append_basic(iter, "Privacy",
1845 DBUS_TYPE_STRING, &privacy);
1848 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1849 DBusMessageIter *iter)
1851 const char *str, *privacy;
1855 str = __connman_ipconfig_method2string(ipconfig->method);
1859 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1861 switch (ipconfig->method) {
1862 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1863 case CONNMAN_IPCONFIG_METHOD_OFF:
1864 case CONNMAN_IPCONFIG_METHOD_DHCP:
1866 case CONNMAN_IPCONFIG_METHOD_FIXED:
1867 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1868 case CONNMAN_IPCONFIG_METHOD_AUTO:
1872 if (ipconfig->address == NULL)
1875 if (ipconfig->address->local != NULL) {
1876 connman_dbus_dict_append_basic(iter, "Address",
1877 DBUS_TYPE_STRING, &ipconfig->address->local);
1878 connman_dbus_dict_append_basic(iter, "PrefixLength",
1880 &ipconfig->address->prefixlen);
1883 if (ipconfig->address->gateway != NULL)
1884 connman_dbus_dict_append_basic(iter, "Gateway",
1885 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1887 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1888 connman_dbus_dict_append_basic(iter, "Privacy",
1889 DBUS_TYPE_STRING, &privacy);
1892 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1893 DBusMessageIter *iter)
1899 str = __connman_ipconfig_method2string(ipconfig->method);
1903 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1905 switch (ipconfig->method) {
1906 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1907 case CONNMAN_IPCONFIG_METHOD_OFF:
1908 case CONNMAN_IPCONFIG_METHOD_DHCP:
1909 case CONNMAN_IPCONFIG_METHOD_AUTO:
1911 case CONNMAN_IPCONFIG_METHOD_FIXED:
1912 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1916 if (ipconfig->address == NULL)
1919 if (ipconfig->address->local != NULL) {
1921 struct in_addr netmask;
1924 connman_dbus_dict_append_basic(iter, "Address",
1925 DBUS_TYPE_STRING, &ipconfig->address->local);
1927 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1928 netmask.s_addr = htonl(addr);
1929 mask = inet_ntoa(netmask);
1930 connman_dbus_dict_append_basic(iter, "Netmask",
1931 DBUS_TYPE_STRING, &mask);
1934 if (ipconfig->address->gateway != NULL)
1935 connman_dbus_dict_append_basic(iter, "Gateway",
1936 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1939 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1940 DBusMessageIter *array)
1942 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1943 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1944 *privacy_string = NULL;
1945 int prefix_length = 0, privacy = 0;
1946 DBusMessageIter dict;
1948 DBG("ipconfig %p", ipconfig);
1950 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1953 dbus_message_iter_recurse(array, &dict);
1955 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1956 DBusMessageIter entry, value;
1960 dbus_message_iter_recurse(&dict, &entry);
1962 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1965 dbus_message_iter_get_basic(&entry, &key);
1966 dbus_message_iter_next(&entry);
1968 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
1971 dbus_message_iter_recurse(&entry, &value);
1973 type = dbus_message_iter_get_arg_type(&value);
1975 if (g_str_equal(key, "Method") == TRUE) {
1978 if (type != DBUS_TYPE_STRING)
1981 dbus_message_iter_get_basic(&value, &str);
1982 method = __connman_ipconfig_string2method(str);
1983 } else if (g_str_equal(key, "Address") == TRUE) {
1984 if (type != DBUS_TYPE_STRING)
1987 dbus_message_iter_get_basic(&value, &address);
1988 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1989 if (type != DBUS_TYPE_BYTE)
1992 dbus_message_iter_get_basic(&value, &prefix_length);
1994 if (prefix_length < 0 || prefix_length > 128)
1996 } else if (g_str_equal(key, "Netmask") == TRUE) {
1997 if (type != DBUS_TYPE_STRING)
2000 dbus_message_iter_get_basic(&value, &netmask);
2001 } else if (g_str_equal(key, "Gateway") == TRUE) {
2002 if (type != DBUS_TYPE_STRING)
2005 dbus_message_iter_get_basic(&value, &gateway);
2006 } else if (g_str_equal(key, "Privacy") == TRUE) {
2007 if (type != DBUS_TYPE_STRING)
2010 dbus_message_iter_get_basic(&value, &privacy_string);
2011 privacy = string2privacy(privacy_string);
2014 dbus_message_iter_next(&dict);
2017 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2019 method, address, netmask, gateway, prefix_length,
2023 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2024 case CONNMAN_IPCONFIG_METHOD_FIXED:
2027 case CONNMAN_IPCONFIG_METHOD_OFF:
2028 ipconfig->method = method;
2029 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2030 disable_ipv6(ipconfig);
2033 case CONNMAN_IPCONFIG_METHOD_AUTO:
2034 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2037 ipconfig->method = method;
2038 if (privacy_string != NULL)
2039 ipconfig->ipv6_privacy_config = privacy;
2040 enable_ipv6(ipconfig);
2043 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2044 if (address == NULL)
2047 ipconfig->method = method;
2049 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2050 connman_ipaddress_set_ipv4(ipconfig->address,
2051 address, netmask, gateway);
2053 return connman_ipaddress_set_ipv6(
2054 ipconfig->address, address,
2055 prefix_length, gateway);
2058 case CONNMAN_IPCONFIG_METHOD_DHCP:
2059 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2062 ipconfig->method = method;
2069 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2070 DBusMessageIter *iter)
2072 struct connman_ipdevice *ipdevice;
2073 const char *method = "auto";
2075 connman_dbus_dict_append_basic(iter, "Method",
2076 DBUS_TYPE_STRING, &method);
2078 ipdevice = g_hash_table_lookup(ipdevice_hash,
2079 GINT_TO_POINTER(ipconfig->index));
2080 if (ipdevice == NULL)
2083 if (ipdevice->ifname != NULL)
2084 connman_dbus_dict_append_basic(iter, "Interface",
2085 DBUS_TYPE_STRING, &ipdevice->ifname);
2087 if (ipdevice->address != NULL)
2088 connman_dbus_dict_append_basic(iter, "Address",
2089 DBUS_TYPE_STRING, &ipdevice->address);
2091 if (ipdevice->mtu > 0)
2092 connman_dbus_dict_append_basic(iter, "MTU",
2093 DBUS_TYPE_UINT16, &ipdevice->mtu);
2096 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2097 GKeyFile *keyfile, const char *identifier, const char *prefix)
2103 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2105 key = g_strdup_printf("%smethod", prefix);
2106 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2107 if (method == NULL) {
2108 switch (ipconfig->type) {
2109 case CONNMAN_IPCONFIG_TYPE_IPV4:
2110 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2112 case CONNMAN_IPCONFIG_TYPE_IPV6:
2113 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2115 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2116 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2120 ipconfig->method = __connman_ipconfig_string2method(method);
2122 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2123 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2125 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2126 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2127 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2129 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2130 privacy = g_key_file_get_string(keyfile, identifier,
2132 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2141 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2142 ipconfig->address->prefixlen = g_key_file_get_integer(
2143 keyfile, identifier, key, NULL);
2146 key = g_strdup_printf("%slocal_address", prefix);
2147 ipconfig->address->local = g_key_file_get_string(
2148 keyfile, identifier, key, NULL);
2151 key = g_strdup_printf("%speer_address", prefix);
2152 ipconfig->address->peer = g_key_file_get_string(
2153 keyfile, identifier, key, NULL);
2156 key = g_strdup_printf("%sbroadcast_address", prefix);
2157 ipconfig->address->broadcast = g_key_file_get_string(
2158 keyfile, identifier, key, NULL);
2161 key = g_strdup_printf("%sgateway", prefix);
2162 ipconfig->address->gateway = g_key_file_get_string(
2163 keyfile, identifier, key, NULL);
2166 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2167 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2169 g_free(ipconfig->last_dhcp_address);
2170 ipconfig->last_dhcp_address = str;
2177 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2178 GKeyFile *keyfile, const char *identifier, const char *prefix)
2183 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2185 method = __connman_ipconfig_method2string(ipconfig->method);
2187 key = g_strdup_printf("%smethod", prefix);
2188 g_key_file_set_string(keyfile, identifier, key, method);
2191 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2192 const char *privacy;
2193 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2194 key = g_strdup_printf("%sprivacy", prefix);
2195 g_key_file_set_string(keyfile, identifier, key, privacy);
2199 switch (ipconfig->method) {
2200 case CONNMAN_IPCONFIG_METHOD_FIXED:
2201 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2203 case CONNMAN_IPCONFIG_METHOD_DHCP:
2204 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2205 if (ipconfig->last_dhcp_address != NULL &&
2206 strlen(ipconfig->last_dhcp_address) > 0)
2207 g_key_file_set_string(keyfile, identifier, key,
2208 ipconfig->last_dhcp_address);
2210 g_key_file_remove_key(keyfile, identifier, key, NULL);
2213 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2214 case CONNMAN_IPCONFIG_METHOD_OFF:
2215 case CONNMAN_IPCONFIG_METHOD_AUTO:
2219 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2220 if (ipconfig->address->prefixlen != 0)
2221 g_key_file_set_integer(keyfile, identifier,
2222 key, ipconfig->address->prefixlen);
2225 key = g_strdup_printf("%slocal_address", prefix);
2226 if (ipconfig->address->local != NULL)
2227 g_key_file_set_string(keyfile, identifier,
2228 key, ipconfig->address->local);
2231 key = g_strdup_printf("%speer_address", prefix);
2232 if (ipconfig->address->peer != NULL)
2233 g_key_file_set_string(keyfile, identifier,
2234 key, ipconfig->address->peer);
2237 key = g_strdup_printf("%sbroadcast_address", prefix);
2238 if (ipconfig->address->broadcast != NULL)
2239 g_key_file_set_string(keyfile, identifier,
2240 key, ipconfig->address->broadcast);
2243 key = g_strdup_printf("%sgateway", prefix);
2244 if (ipconfig->address->gateway != NULL)
2245 g_key_file_set_string(keyfile, identifier,
2246 key, ipconfig->address->gateway);
2252 int __connman_ipconfig_init(void)
2256 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2257 NULL, free_ipdevice);
2259 is_ipv6_supported = connman_inet_is_ipv6_supported();
2264 void __connman_ipconfig_cleanup(void)
2268 g_hash_table_destroy(ipdevice_hash);
2269 ipdevice_hash = NULL;