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);
396 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
398 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
399 ipdevice->config_ipv6);
401 if (ipdevice->config_ipv4)
402 connman_inet_clear_address(ipdevice->index,
403 ipdevice->config_ipv4->address);
405 if (ipdevice->config_ipv6)
406 connman_inet_clear_ipv6_address(ipdevice->index,
407 ipdevice->config_ipv6->address->local,
408 ipdevice->config_ipv6->address->prefixlen);
411 static void update_stats(struct connman_ipdevice *ipdevice,
412 struct rtnl_link_stats *stats)
414 struct connman_service *service;
416 if (stats->rx_packets == 0 && stats->tx_packets == 0)
419 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
420 stats->rx_packets, stats->rx_bytes);
421 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
422 stats->tx_packets, stats->tx_bytes);
424 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
427 if (ipdevice->config_ipv4)
428 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
429 else if (ipdevice->config_ipv6)
430 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
437 ipdevice->rx_packets = stats->rx_packets;
438 ipdevice->tx_packets = stats->tx_packets;
439 ipdevice->rx_bytes = stats->rx_bytes;
440 ipdevice->tx_bytes = stats->tx_bytes;
441 ipdevice->rx_errors = stats->rx_errors;
442 ipdevice->tx_errors = stats->tx_errors;
443 ipdevice->rx_dropped = stats->rx_dropped;
444 ipdevice->tx_dropped = stats->tx_dropped;
446 __connman_service_notify(service,
447 ipdevice->rx_packets, ipdevice->tx_packets,
448 ipdevice->rx_bytes, ipdevice->tx_bytes,
449 ipdevice->rx_errors, ipdevice->tx_errors,
450 ipdevice->rx_dropped, ipdevice->tx_dropped);
453 void __connman_ipconfig_newlink(int index, unsigned short type,
454 unsigned int flags, const char *address,
456 struct rtnl_link_stats *stats)
458 struct connman_ipdevice *ipdevice;
461 gboolean up = FALSE, down = FALSE;
462 gboolean lower_up = FALSE, lower_down = FALSE;
464 DBG("index %d", index);
466 if (type == ARPHRD_LOOPBACK)
469 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
470 if (ipdevice != NULL) {
471 char *ifname = connman_inet_ifname(index);
472 if (g_strcmp0(ipdevice->ifname, ifname) != 0) {
473 DBG("interface name changed %s -> %s",
474 ipdevice->ifname, ifname);
476 g_free(ipdevice->ifname);
477 ipdevice->ifname = ifname;
484 ipdevice = g_try_new0(struct connman_ipdevice, 1);
485 if (ipdevice == NULL)
488 ipdevice->index = index;
489 ipdevice->ifname = connman_inet_ifname(index);
490 ipdevice->type = type;
492 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
493 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
495 ipdevice->address = g_strdup(address);
497 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
499 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
500 index, type, type2str(type));
505 update_stats(ipdevice, stats);
507 if (flags == ipdevice->flags)
510 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
517 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
518 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
519 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
520 (IFF_RUNNING | IFF_LOWER_UP))
522 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
526 ipdevice->flags = flags;
528 str = g_string_new(NULL);
533 g_string_append(str, "UP");
535 g_string_append(str, "DOWN");
537 if (flags & IFF_RUNNING)
538 g_string_append(str, ",RUNNING");
540 if (flags & IFF_LOWER_UP)
541 g_string_append(str, ",LOWER_UP");
543 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
546 g_string_free(str, TRUE);
548 for (list = g_list_first(ipconfig_list); list;
549 list = g_list_next(list)) {
550 struct connman_ipconfig *ipconfig = list->data;
552 if (index != ipconfig->index)
555 if (ipconfig->ops == NULL)
558 if (up == TRUE && ipconfig->ops->up)
559 ipconfig->ops->up(ipconfig);
560 if (lower_up == TRUE && ipconfig->ops->lower_up)
561 ipconfig->ops->lower_up(ipconfig);
563 if (lower_down == TRUE && ipconfig->ops->lower_down)
564 ipconfig->ops->lower_down(ipconfig);
565 if (down == TRUE && ipconfig->ops->down)
566 ipconfig->ops->down(ipconfig);
570 __connman_ipconfig_lower_up(ipdevice);
572 __connman_ipconfig_lower_down(ipdevice);
575 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
577 struct connman_ipdevice *ipdevice;
580 DBG("index %d", index);
582 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
583 if (ipdevice == NULL)
586 update_stats(ipdevice, stats);
588 for (list = g_list_first(ipconfig_list); list;
589 list = g_list_next(list)) {
590 struct connman_ipconfig *ipconfig = list->data;
592 if (index != ipconfig->index)
595 ipconfig->index = -1;
597 if (ipconfig->ops == NULL)
600 if (ipconfig->ops->lower_down)
601 ipconfig->ops->lower_down(ipconfig);
602 if (ipconfig->ops->down)
603 ipconfig->ops->down(ipconfig);
606 __connman_ipconfig_lower_down(ipdevice);
608 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
611 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
613 const struct connman_ipaddress *addr1 = a;
614 const struct connman_ipaddress *addr2 = b;
616 if (addr1->prefixlen != addr2->prefixlen)
617 return addr2->prefixlen - addr1->prefixlen;
619 return g_strcmp0(addr1->local, addr2->local);
622 void __connman_ipconfig_newaddr(int index, int family, const char *label,
623 unsigned char prefixlen, const char *address)
625 struct connman_ipdevice *ipdevice;
626 struct connman_ipaddress *ipaddress;
627 enum connman_ipconfig_type type;
630 DBG("index %d", index);
632 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
633 if (ipdevice == NULL)
636 ipaddress = connman_ipaddress_alloc(family);
637 if (ipaddress == NULL)
640 ipaddress->prefixlen = prefixlen;
641 ipaddress->local = g_strdup(address);
643 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
644 check_duplicate_address)) {
645 connman_ipaddress_free(ipaddress);
649 if (family == AF_INET)
650 type = CONNMAN_IPCONFIG_TYPE_IPV4;
651 else if (family == AF_INET6)
652 type = CONNMAN_IPCONFIG_TYPE_IPV6;
656 ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
659 connman_info("%s {add} address %s/%u label %s family %d",
660 ipdevice->ifname, address, prefixlen, label, family);
662 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
663 __connman_ippool_newaddr(index, address, prefixlen);
665 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
666 connman_ipaddress_copy(ipdevice->config_ipv4->system,
669 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
670 connman_ipaddress_copy(ipdevice->config_ipv6->system,
675 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
678 for (list = g_list_first(ipconfig_list); list;
679 list = g_list_next(list)) {
680 struct connman_ipconfig *ipconfig = list->data;
682 if (index != ipconfig->index)
685 if (type != ipconfig->type)
688 if (ipconfig->ops == NULL)
691 if (ipconfig->ops->ip_bound)
692 ipconfig->ops->ip_bound(ipconfig);
696 void __connman_ipconfig_deladdr(int index, int family, const char *label,
697 unsigned char prefixlen, const char *address)
699 struct connman_ipdevice *ipdevice;
700 struct connman_ipaddress *ipaddress;
701 enum connman_ipconfig_type type;
704 DBG("index %d", index);
706 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
707 if (ipdevice == NULL)
710 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
711 if (ipaddress == NULL)
714 if (family == AF_INET)
715 type = CONNMAN_IPCONFIG_TYPE_IPV4;
716 else if (family == AF_INET6)
717 type = CONNMAN_IPCONFIG_TYPE_IPV6;
721 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
724 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
725 __connman_ippool_deladdr(index, address, prefixlen);
727 connman_ipaddress_clear(ipaddress);
730 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
731 address, prefixlen, label);
733 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
736 if (g_slist_length(ipdevice->address_list) > 0)
739 for (list = g_list_first(ipconfig_list); list;
740 list = g_list_next(list)) {
741 struct connman_ipconfig *ipconfig = list->data;
743 if (index != ipconfig->index)
746 if (type != ipconfig->type)
749 if (ipconfig->ops == NULL)
752 if (ipconfig->ops->ip_release)
753 ipconfig->ops->ip_release(ipconfig);
757 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
758 const char *dst, const char *gateway)
760 struct connman_ipdevice *ipdevice;
762 DBG("index %d", index);
764 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
765 if (ipdevice == NULL)
768 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
769 g_strcmp0(dst, "::") == 0)) {
771 enum connman_ipconfig_type type;
773 if (family == AF_INET6) {
774 type = CONNMAN_IPCONFIG_TYPE_IPV6;
775 g_free(ipdevice->ipv6_gateway);
776 ipdevice->ipv6_gateway = g_strdup(gateway);
778 if (ipdevice->config_ipv6 != NULL &&
779 ipdevice->config_ipv6->system != NULL) {
780 g_free(ipdevice->config_ipv6->system->gateway);
781 ipdevice->config_ipv6->system->gateway =
784 } else if (family == AF_INET) {
785 type = CONNMAN_IPCONFIG_TYPE_IPV4;
786 g_free(ipdevice->ipv4_gateway);
787 ipdevice->ipv4_gateway = g_strdup(gateway);
789 if (ipdevice->config_ipv4 != NULL &&
790 ipdevice->config_ipv4->system != NULL) {
791 g_free(ipdevice->config_ipv4->system->gateway);
792 ipdevice->config_ipv4->system->gateway =
798 for (config_list = g_list_first(ipconfig_list); config_list;
799 config_list = g_list_next(config_list)) {
800 struct connman_ipconfig *ipconfig = config_list->data;
802 if (index != ipconfig->index)
805 if (type != ipconfig->type)
808 if (ipconfig->ops == NULL)
811 if (ipconfig->ops->route_set)
812 ipconfig->ops->route_set(ipconfig);
816 connman_info("%s {add} route %s gw %s scope %u <%s>",
817 ipdevice->ifname, dst, gateway,
818 scope, scope2str(scope));
821 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
822 const char *dst, const char *gateway)
824 struct connman_ipdevice *ipdevice;
826 DBG("index %d", index);
828 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
829 if (ipdevice == NULL)
832 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
833 g_strcmp0(dst, "::") == 0)) {
835 enum connman_ipconfig_type type;
837 if (family == AF_INET6) {
838 type = CONNMAN_IPCONFIG_TYPE_IPV6;
839 g_free(ipdevice->ipv6_gateway);
840 ipdevice->ipv6_gateway = NULL;
842 if (ipdevice->config_ipv6 != NULL &&
843 ipdevice->config_ipv6->system != NULL) {
844 g_free(ipdevice->config_ipv6->system->gateway);
845 ipdevice->config_ipv6->system->gateway = NULL;
847 } else if (family == AF_INET) {
848 type = CONNMAN_IPCONFIG_TYPE_IPV4;
849 g_free(ipdevice->ipv4_gateway);
850 ipdevice->ipv4_gateway = NULL;
852 if (ipdevice->config_ipv4 != NULL &&
853 ipdevice->config_ipv4->system != NULL) {
854 g_free(ipdevice->config_ipv4->system->gateway);
855 ipdevice->config_ipv4->system->gateway = NULL;
860 for (config_list = g_list_first(ipconfig_list); config_list;
861 config_list = g_list_next(config_list)) {
862 struct connman_ipconfig *ipconfig = config_list->data;
864 if (index != ipconfig->index)
867 if (type != ipconfig->type)
870 if (ipconfig->ops == NULL)
873 if (ipconfig->ops->route_unset)
874 ipconfig->ops->route_unset(ipconfig);
878 connman_info("%s {del} route %s gw %s scope %u <%s>",
879 ipdevice->ifname, dst, gateway,
880 scope, scope2str(scope));
883 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
888 keys = g_hash_table_get_keys(ipdevice_hash);
892 for (list = g_list_first(keys); list; list = g_list_next(list)) {
893 int index = GPOINTER_TO_INT(list->data);
895 function(index, user_data);
901 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
902 struct connman_ipconfig *ipconfig)
904 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
907 unsigned short __connman_ipconfig_get_type_from_index(int index)
909 struct connman_ipdevice *ipdevice;
911 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
912 if (ipdevice == NULL)
915 return ipdevice->type;
918 unsigned int __connman_ipconfig_get_flags_from_index(int index)
920 struct connman_ipdevice *ipdevice;
922 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
923 if (ipdevice == NULL)
926 return ipdevice->flags;
929 const char *__connman_ipconfig_get_gateway_from_index(int index,
930 enum connman_ipconfig_type type)
932 struct connman_ipdevice *ipdevice;
934 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
935 if (ipdevice == NULL)
938 if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
939 if (ipdevice->ipv4_gateway != NULL)
940 return ipdevice->ipv4_gateway;
942 if (ipdevice->config_ipv4 != NULL &&
943 ipdevice->config_ipv4->address != NULL)
944 return ipdevice->config_ipv4->address->gateway;
947 if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
948 if (ipdevice->ipv6_gateway != NULL)
949 return ipdevice->ipv6_gateway;
951 if (ipdevice->config_ipv6 != NULL &&
952 ipdevice->config_ipv6->address != NULL)
953 return ipdevice->config_ipv6->address->gateway;
959 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
961 ipconfig->index = index;
964 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
966 if (ipconfig->address == NULL)
969 return ipconfig->address->local;
972 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
974 if (ipconfig->address == NULL)
977 g_free(ipconfig->address->local);
978 ipconfig->address->local = g_strdup(address);
981 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
983 if (ipconfig->address == NULL)
986 return ipconfig->address->peer;
989 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
991 if (ipconfig->address == NULL)
994 g_free(ipconfig->address->peer);
995 ipconfig->address->peer = g_strdup(address);
998 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1000 if (ipconfig->address == NULL)
1003 return ipconfig->address->broadcast;
1006 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1008 if (ipconfig->address == NULL)
1011 g_free(ipconfig->address->broadcast);
1012 ipconfig->address->broadcast = g_strdup(broadcast);
1015 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1017 if (ipconfig->address == NULL)
1020 return ipconfig->address->gateway;
1023 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1027 if (ipconfig->address == NULL)
1029 g_free(ipconfig->address->gateway);
1030 ipconfig->address->gateway = g_strdup(gateway);
1033 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1035 struct connman_service *service;
1039 if (ipconfig->address == NULL)
1042 service = __connman_service_lookup_from_index(ipconfig->index);
1043 if (service == NULL)
1046 __connman_connection_gateway_remove(service, ipconfig->type);
1048 DBG("type %d gw %s peer %s", ipconfig->type,
1049 ipconfig->address->gateway, ipconfig->address->peer);
1051 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1052 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1053 return __connman_connection_gateway_add(service,
1054 ipconfig->address->gateway,
1056 ipconfig->address->peer);
1061 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1063 struct connman_service *service;
1067 service = __connman_service_lookup_from_index(ipconfig->index);
1068 if (service != NULL)
1069 __connman_connection_gateway_remove(service, ipconfig->type);
1072 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1074 if (ipconfig->address == NULL)
1077 return ipconfig->address->prefixlen;
1080 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1082 if (ipconfig->address == NULL)
1085 ipconfig->address->prefixlen = prefixlen;
1088 static struct connman_ipconfig *create_ipv6config(int index)
1090 struct connman_ipconfig *ipv6config;
1091 struct connman_ipdevice *ipdevice;
1093 DBG("index %d", index);
1095 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1096 if (ipv6config == NULL)
1099 ipv6config->refcount = 1;
1101 ipv6config->index = index;
1102 ipv6config->enabled = FALSE;
1103 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1105 if (is_ipv6_supported == FALSE)
1106 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1108 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1110 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1111 if (ipdevice != NULL)
1112 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1114 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1115 if (ipv6config->address == NULL) {
1120 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1122 DBG("ipconfig %p method %s", ipv6config,
1123 __connman_ipconfig_method2string(ipv6config->method));
1129 * connman_ipconfig_create:
1131 * Allocate a new ipconfig structure.
1133 * Returns: a newly-allocated #connman_ipconfig structure
1135 struct connman_ipconfig *__connman_ipconfig_create(int index,
1136 enum connman_ipconfig_type type)
1138 struct connman_ipconfig *ipconfig;
1140 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1141 return create_ipv6config(index);
1143 DBG("index %d", index);
1145 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1146 if (ipconfig == NULL)
1149 ipconfig->refcount = 1;
1151 ipconfig->index = index;
1152 ipconfig->enabled = FALSE;
1153 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1155 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1156 if (ipconfig->address == NULL) {
1161 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1163 DBG("ipconfig %p", ipconfig);
1170 * connman_ipconfig_ref:
1171 * @ipconfig: ipconfig structure
1173 * Increase reference counter of ipconfig
1175 struct connman_ipconfig *
1176 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1177 const char *file, int line, const char *caller)
1179 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1180 file, line, caller);
1182 __sync_fetch_and_add(&ipconfig->refcount, 1);
1188 * connman_ipconfig_unref:
1189 * @ipconfig: ipconfig structure
1191 * Decrease reference counter of ipconfig
1193 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1194 const char *file, int line, const char *caller)
1196 if (ipconfig == NULL)
1199 DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1200 file, line, caller);
1202 if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1205 if (__connman_ipconfig_disable(ipconfig) < 0)
1206 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1208 __connman_ipconfig_set_ops(ipconfig, NULL);
1210 if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1211 __connman_ipconfig_unref(ipconfig->origin);
1212 ipconfig->origin = NULL;
1215 connman_ipaddress_free(ipconfig->system);
1216 connman_ipaddress_free(ipconfig->address);
1217 g_free(ipconfig->last_dhcp_address);
1222 * connman_ipconfig_get_data:
1223 * @ipconfig: ipconfig structure
1225 * Get private data pointer
1227 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1229 if (ipconfig == NULL)
1232 return ipconfig->ops_data;
1236 * connman_ipconfig_set_data:
1237 * @ipconfig: ipconfig structure
1238 * @data: data pointer
1240 * Set private data pointer
1242 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1244 ipconfig->ops_data = data;
1248 * connman_ipconfig_get_index:
1249 * @ipconfig: ipconfig structure
1251 * Get interface index
1253 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1255 if (ipconfig == NULL)
1258 if (ipconfig->origin != NULL)
1259 return ipconfig->origin->index;
1261 return ipconfig->index;
1265 * connman_ipconfig_get_ifname:
1266 * @ipconfig: ipconfig structure
1268 * Get interface name
1270 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1272 struct connman_ipdevice *ipdevice;
1274 if (ipconfig == NULL)
1277 if (ipconfig->index < 0)
1280 ipdevice = g_hash_table_lookup(ipdevice_hash,
1281 GINT_TO_POINTER(ipconfig->index));
1282 if (ipdevice == NULL)
1285 return ipdevice->ifname;
1289 * connman_ipconfig_set_ops:
1290 * @ipconfig: ipconfig structure
1291 * @ops: operation callbacks
1293 * Set the operation callbacks
1295 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1296 const struct connman_ipconfig_ops *ops)
1298 ipconfig->ops = ops;
1302 * connman_ipconfig_set_method:
1303 * @ipconfig: ipconfig structure
1304 * @method: configuration method
1306 * Set the configuration method
1308 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1309 enum connman_ipconfig_method method)
1311 ipconfig->method = method;
1316 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1318 if (ipconfig == NULL)
1319 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1321 return ipconfig->method;
1324 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1328 switch (ipconfig->method) {
1329 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1330 case CONNMAN_IPCONFIG_METHOD_OFF:
1332 case CONNMAN_IPCONFIG_METHOD_AUTO:
1333 case CONNMAN_IPCONFIG_METHOD_FIXED:
1334 case CONNMAN_IPCONFIG_METHOD_DHCP:
1335 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1336 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1337 return connman_inet_set_address(ipconfig->index,
1339 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1340 return connman_inet_set_ipv6_address(
1341 ipconfig->index, ipconfig->address);
1347 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1353 if (ipconfig == NULL)
1356 DBG("method %d", ipconfig->method);
1358 switch (ipconfig->method) {
1359 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1360 case CONNMAN_IPCONFIG_METHOD_OFF:
1362 case CONNMAN_IPCONFIG_METHOD_AUTO:
1363 case CONNMAN_IPCONFIG_METHOD_FIXED:
1364 case CONNMAN_IPCONFIG_METHOD_DHCP:
1365 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1366 err = __connman_ipconfig_address_unset(ipconfig);
1367 connman_ipaddress_clear(ipconfig->address);
1375 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1381 if (ipconfig == NULL)
1384 DBG("method %d", ipconfig->method);
1386 switch (ipconfig->method) {
1387 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1388 case CONNMAN_IPCONFIG_METHOD_OFF:
1390 case CONNMAN_IPCONFIG_METHOD_AUTO:
1391 case CONNMAN_IPCONFIG_METHOD_FIXED:
1392 case CONNMAN_IPCONFIG_METHOD_DHCP:
1393 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1394 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1395 err = connman_inet_clear_address(ipconfig->index,
1397 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1398 err = connman_inet_clear_ipv6_address(
1400 ipconfig->address->local,
1401 ipconfig->address->prefixlen);
1411 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1414 struct connman_ipdevice *ipdevice;
1416 DBG("ipconfig %p", ipconfig);
1418 if (ipconfig == NULL || ipconfig->index < 0)
1421 ipdevice = g_hash_table_lookup(ipdevice_hash,
1422 GINT_TO_POINTER(ipconfig->index));
1423 if (ipdevice == NULL)
1426 g_free(ipdevice->pac);
1427 ipdevice->pac = g_strdup(url);
1432 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1434 struct connman_ipdevice *ipdevice;
1436 DBG("ipconfig %p", ipconfig);
1438 if (ipconfig == NULL || ipconfig->index < 0)
1441 ipdevice = g_hash_table_lookup(ipdevice_hash,
1442 GINT_TO_POINTER(ipconfig->index));
1443 if (ipdevice == NULL)
1446 return ipdevice->pac;
1449 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1450 const char *address)
1452 if (ipconfig == NULL)
1455 g_free(ipconfig->last_dhcp_address);
1456 ipconfig->last_dhcp_address = g_strdup(address);
1459 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1461 if (ipconfig == NULL)
1464 return ipconfig->last_dhcp_address;
1467 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1469 struct connman_ipdevice *ipdevice;
1473 ipdevice = g_hash_table_lookup(ipdevice_hash,
1474 GINT_TO_POINTER(ipconfig->index));
1475 if (ipdevice == NULL)
1478 set_ipv6_state(ipdevice->ifname, FALSE);
1481 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1483 struct connman_ipdevice *ipdevice;
1487 ipdevice = g_hash_table_lookup(ipdevice_hash,
1488 GINT_TO_POINTER(ipconfig->index));
1489 if (ipdevice == NULL)
1492 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1493 set_ipv6_privacy(ipdevice->ifname,
1494 ipconfig->ipv6_privacy_config);
1496 set_ipv6_state(ipdevice->ifname, TRUE);
1499 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1501 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1504 enable_ipv6(ipconfig);
1507 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1509 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1512 disable_ipv6(ipconfig);
1515 connman_bool_t __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1517 if (ipconfig == NULL)
1520 switch (ipconfig->method) {
1521 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1522 case CONNMAN_IPCONFIG_METHOD_OFF:
1524 case CONNMAN_IPCONFIG_METHOD_AUTO:
1525 case CONNMAN_IPCONFIG_METHOD_FIXED:
1526 case CONNMAN_IPCONFIG_METHOD_DHCP:
1527 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1534 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1536 struct connman_ipdevice *ipdevice;
1537 gboolean up = FALSE, down = FALSE;
1538 gboolean lower_up = FALSE, lower_down = FALSE;
1539 enum connman_ipconfig_type type;
1541 DBG("ipconfig %p", ipconfig);
1543 if (ipconfig == NULL || ipconfig->index < 0)
1546 ipdevice = g_hash_table_lookup(ipdevice_hash,
1547 GINT_TO_POINTER(ipconfig->index));
1548 if (ipdevice == NULL)
1551 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1552 if (ipdevice->config_ipv4 == ipconfig)
1554 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1555 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1556 if (ipdevice->config_ipv6 == ipconfig)
1558 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1559 enable_ipv6(ipconfig);
1563 ipconfig->enabled = TRUE;
1565 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1566 ipdevice->config_ipv4 != NULL) {
1567 ipconfig_list = g_list_remove(ipconfig_list,
1568 ipdevice->config_ipv4);
1570 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1572 __connman_ipconfig_unref(ipdevice->config_ipv4);
1575 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1576 ipdevice->config_ipv6 != NULL) {
1577 ipconfig_list = g_list_remove(ipconfig_list,
1578 ipdevice->config_ipv6);
1580 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1582 __connman_ipconfig_unref(ipdevice->config_ipv6);
1585 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1586 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1587 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1588 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1590 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1592 if (ipdevice->flags & IFF_UP)
1597 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1598 (IFF_RUNNING | IFF_LOWER_UP))
1600 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1603 if (up == TRUE && ipconfig->ops->up)
1604 ipconfig->ops->up(ipconfig);
1605 if (lower_up == TRUE && ipconfig->ops->lower_up)
1606 ipconfig->ops->lower_up(ipconfig);
1608 if (lower_down == TRUE && ipconfig->ops->lower_down)
1609 ipconfig->ops->lower_down(ipconfig);
1610 if (down == TRUE && ipconfig->ops->down)
1611 ipconfig->ops->down(ipconfig);
1616 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1618 struct connman_ipdevice *ipdevice;
1620 DBG("ipconfig %p", ipconfig);
1622 if (ipconfig == NULL || ipconfig->index < 0)
1625 ipdevice = g_hash_table_lookup(ipdevice_hash,
1626 GINT_TO_POINTER(ipconfig->index));
1627 if (ipdevice == NULL)
1630 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1633 ipconfig->enabled = FALSE;
1635 if (ipdevice->config_ipv4 == ipconfig) {
1636 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1638 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1639 __connman_ipconfig_unref(ipdevice->config_ipv4);
1640 ipdevice->config_ipv4 = NULL;
1644 if (ipdevice->config_ipv6 == ipconfig) {
1645 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1647 if (ipdevice->config_ipv6->method ==
1648 CONNMAN_IPCONFIG_METHOD_AUTO)
1649 disable_ipv6(ipdevice->config_ipv6);
1651 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1652 __connman_ipconfig_unref(ipdevice->config_ipv6);
1653 ipdevice->config_ipv6 = NULL;
1660 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1663 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1665 case CONNMAN_IPCONFIG_METHOD_OFF:
1667 case CONNMAN_IPCONFIG_METHOD_FIXED:
1669 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1671 case CONNMAN_IPCONFIG_METHOD_DHCP:
1673 case CONNMAN_IPCONFIG_METHOD_AUTO:
1680 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1682 if (g_strcmp0(method, "off") == 0)
1683 return CONNMAN_IPCONFIG_METHOD_OFF;
1684 else if (g_strcmp0(method, "fixed") == 0)
1685 return CONNMAN_IPCONFIG_METHOD_FIXED;
1686 else if (g_strcmp0(method, "manual") == 0)
1687 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1688 else if (g_strcmp0(method, "dhcp") == 0)
1689 return CONNMAN_IPCONFIG_METHOD_DHCP;
1690 else if (g_strcmp0(method, "auto") == 0)
1691 return CONNMAN_IPCONFIG_METHOD_AUTO;
1693 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1696 static const char *privacy2string(int privacy)
1700 else if (privacy == 1)
1702 else if (privacy > 1)
1708 static int string2privacy(const char *privacy)
1710 if (g_strcmp0(privacy, "disabled") == 0)
1712 else if (g_strcmp0(privacy, "enabled") == 0)
1714 else if (g_strcmp0(privacy, "prefered") == 0)
1720 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1721 DBusMessageIter *iter)
1723 struct connman_ipaddress *append_addr = NULL;
1728 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1731 str = __connman_ipconfig_method2string(ipconfig->method);
1735 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1737 switch (ipconfig->method) {
1738 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1739 case CONNMAN_IPCONFIG_METHOD_OFF:
1740 case CONNMAN_IPCONFIG_METHOD_AUTO:
1743 case CONNMAN_IPCONFIG_METHOD_FIXED:
1744 append_addr = ipconfig->address;
1747 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1748 case CONNMAN_IPCONFIG_METHOD_DHCP:
1749 append_addr = ipconfig->system;
1753 if (append_addr == NULL)
1756 if (append_addr->local != NULL) {
1758 struct in_addr netmask;
1761 connman_dbus_dict_append_basic(iter, "Address",
1762 DBUS_TYPE_STRING, &append_addr->local);
1764 addr = 0xffffffff << (32 - append_addr->prefixlen);
1765 netmask.s_addr = htonl(addr);
1766 mask = inet_ntoa(netmask);
1767 connman_dbus_dict_append_basic(iter, "Netmask",
1768 DBUS_TYPE_STRING, &mask);
1771 if (append_addr->gateway != NULL)
1772 connman_dbus_dict_append_basic(iter, "Gateway",
1773 DBUS_TYPE_STRING, &append_addr->gateway);
1776 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1777 DBusMessageIter *iter,
1778 struct connman_ipconfig *ipconfig_ipv4)
1780 struct connman_ipaddress *append_addr = NULL;
1781 const char *str, *privacy;
1785 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1788 str = __connman_ipconfig_method2string(ipconfig->method);
1792 if (ipconfig_ipv4 != NULL &&
1793 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1794 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1798 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1800 switch (ipconfig->method) {
1801 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1802 case CONNMAN_IPCONFIG_METHOD_OFF:
1805 case CONNMAN_IPCONFIG_METHOD_FIXED:
1806 append_addr = ipconfig->address;
1809 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1810 case CONNMAN_IPCONFIG_METHOD_DHCP:
1811 case CONNMAN_IPCONFIG_METHOD_AUTO:
1812 append_addr = ipconfig->system;
1816 if (append_addr == NULL)
1819 if (append_addr->local != NULL) {
1820 connman_dbus_dict_append_basic(iter, "Address",
1821 DBUS_TYPE_STRING, &append_addr->local);
1822 connman_dbus_dict_append_basic(iter, "PrefixLength",
1824 &append_addr->prefixlen);
1827 if (append_addr->gateway != NULL)
1828 connman_dbus_dict_append_basic(iter, "Gateway",
1829 DBUS_TYPE_STRING, &append_addr->gateway);
1831 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1832 connman_dbus_dict_append_basic(iter, "Privacy",
1833 DBUS_TYPE_STRING, &privacy);
1836 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1837 DBusMessageIter *iter)
1839 const char *str, *privacy;
1843 str = __connman_ipconfig_method2string(ipconfig->method);
1847 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1849 switch (ipconfig->method) {
1850 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1851 case CONNMAN_IPCONFIG_METHOD_OFF:
1852 case CONNMAN_IPCONFIG_METHOD_DHCP:
1854 case CONNMAN_IPCONFIG_METHOD_FIXED:
1855 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1856 case CONNMAN_IPCONFIG_METHOD_AUTO:
1860 if (ipconfig->address == NULL)
1863 if (ipconfig->address->local != NULL) {
1864 connman_dbus_dict_append_basic(iter, "Address",
1865 DBUS_TYPE_STRING, &ipconfig->address->local);
1866 connman_dbus_dict_append_basic(iter, "PrefixLength",
1868 &ipconfig->address->prefixlen);
1871 if (ipconfig->address->gateway != NULL)
1872 connman_dbus_dict_append_basic(iter, "Gateway",
1873 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1875 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1876 connman_dbus_dict_append_basic(iter, "Privacy",
1877 DBUS_TYPE_STRING, &privacy);
1880 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1881 DBusMessageIter *iter)
1887 str = __connman_ipconfig_method2string(ipconfig->method);
1891 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1893 switch (ipconfig->method) {
1894 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1895 case CONNMAN_IPCONFIG_METHOD_OFF:
1896 case CONNMAN_IPCONFIG_METHOD_DHCP:
1897 case CONNMAN_IPCONFIG_METHOD_AUTO:
1899 case CONNMAN_IPCONFIG_METHOD_FIXED:
1900 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1904 if (ipconfig->address == NULL)
1907 if (ipconfig->address->local != NULL) {
1909 struct in_addr netmask;
1912 connman_dbus_dict_append_basic(iter, "Address",
1913 DBUS_TYPE_STRING, &ipconfig->address->local);
1915 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1916 netmask.s_addr = htonl(addr);
1917 mask = inet_ntoa(netmask);
1918 connman_dbus_dict_append_basic(iter, "Netmask",
1919 DBUS_TYPE_STRING, &mask);
1922 if (ipconfig->address->gateway != NULL)
1923 connman_dbus_dict_append_basic(iter, "Gateway",
1924 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1927 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1928 DBusMessageIter *array)
1930 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1931 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1932 *privacy_string = NULL;
1933 int prefix_length = 0, privacy = 0;
1934 DBusMessageIter dict;
1936 DBG("ipconfig %p", ipconfig);
1938 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1941 dbus_message_iter_recurse(array, &dict);
1943 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1944 DBusMessageIter entry, value;
1948 dbus_message_iter_recurse(&dict, &entry);
1950 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1953 dbus_message_iter_get_basic(&entry, &key);
1954 dbus_message_iter_next(&entry);
1956 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
1959 dbus_message_iter_recurse(&entry, &value);
1961 type = dbus_message_iter_get_arg_type(&value);
1963 if (g_str_equal(key, "Method") == TRUE) {
1966 if (type != DBUS_TYPE_STRING)
1969 dbus_message_iter_get_basic(&value, &str);
1970 method = __connman_ipconfig_string2method(str);
1971 } else if (g_str_equal(key, "Address") == TRUE) {
1972 if (type != DBUS_TYPE_STRING)
1975 dbus_message_iter_get_basic(&value, &address);
1976 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1977 if (type != DBUS_TYPE_BYTE)
1980 dbus_message_iter_get_basic(&value, &prefix_length);
1982 if (prefix_length < 0 || prefix_length > 128)
1984 } else if (g_str_equal(key, "Netmask") == TRUE) {
1985 if (type != DBUS_TYPE_STRING)
1988 dbus_message_iter_get_basic(&value, &netmask);
1989 } else if (g_str_equal(key, "Gateway") == TRUE) {
1990 if (type != DBUS_TYPE_STRING)
1993 dbus_message_iter_get_basic(&value, &gateway);
1994 } else if (g_str_equal(key, "Privacy") == TRUE) {
1995 if (type != DBUS_TYPE_STRING)
1998 dbus_message_iter_get_basic(&value, &privacy_string);
1999 privacy = string2privacy(privacy_string);
2002 dbus_message_iter_next(&dict);
2005 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2007 method, address, netmask, gateway, prefix_length,
2011 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2012 case CONNMAN_IPCONFIG_METHOD_FIXED:
2015 case CONNMAN_IPCONFIG_METHOD_OFF:
2016 ipconfig->method = method;
2017 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2018 disable_ipv6(ipconfig);
2021 case CONNMAN_IPCONFIG_METHOD_AUTO:
2022 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2025 ipconfig->method = method;
2026 if (privacy_string != NULL)
2027 ipconfig->ipv6_privacy_config = privacy;
2028 enable_ipv6(ipconfig);
2031 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2032 if (address == NULL)
2035 ipconfig->method = method;
2037 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2038 connman_ipaddress_set_ipv4(ipconfig->address,
2039 address, netmask, gateway);
2041 return connman_ipaddress_set_ipv6(
2042 ipconfig->address, address,
2043 prefix_length, gateway);
2046 case CONNMAN_IPCONFIG_METHOD_DHCP:
2047 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2050 ipconfig->method = method;
2057 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2058 DBusMessageIter *iter)
2060 struct connman_ipdevice *ipdevice;
2061 const char *method = "auto";
2063 connman_dbus_dict_append_basic(iter, "Method",
2064 DBUS_TYPE_STRING, &method);
2066 ipdevice = g_hash_table_lookup(ipdevice_hash,
2067 GINT_TO_POINTER(ipconfig->index));
2068 if (ipdevice == NULL)
2071 if (ipdevice->ifname != NULL)
2072 connman_dbus_dict_append_basic(iter, "Interface",
2073 DBUS_TYPE_STRING, &ipdevice->ifname);
2075 if (ipdevice->address != NULL)
2076 connman_dbus_dict_append_basic(iter, "Address",
2077 DBUS_TYPE_STRING, &ipdevice->address);
2079 if (ipdevice->mtu > 0)
2080 connman_dbus_dict_append_basic(iter, "MTU",
2081 DBUS_TYPE_UINT16, &ipdevice->mtu);
2084 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2085 GKeyFile *keyfile, const char *identifier, const char *prefix)
2091 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2093 key = g_strdup_printf("%smethod", prefix);
2094 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2095 if (method == NULL) {
2096 switch (ipconfig->type) {
2097 case CONNMAN_IPCONFIG_TYPE_IPV4:
2098 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2100 case CONNMAN_IPCONFIG_TYPE_IPV6:
2101 ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2103 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2104 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2108 ipconfig->method = __connman_ipconfig_string2method(method);
2110 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2111 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2113 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2114 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2115 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2117 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2118 privacy = g_key_file_get_string(keyfile, identifier,
2120 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2129 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2130 ipconfig->address->prefixlen = g_key_file_get_integer(
2131 keyfile, identifier, key, NULL);
2134 key = g_strdup_printf("%slocal_address", prefix);
2135 ipconfig->address->local = g_key_file_get_string(
2136 keyfile, identifier, key, NULL);
2139 key = g_strdup_printf("%speer_address", prefix);
2140 ipconfig->address->peer = g_key_file_get_string(
2141 keyfile, identifier, key, NULL);
2144 key = g_strdup_printf("%sbroadcast_address", prefix);
2145 ipconfig->address->broadcast = g_key_file_get_string(
2146 keyfile, identifier, key, NULL);
2149 key = g_strdup_printf("%sgateway", prefix);
2150 ipconfig->address->gateway = g_key_file_get_string(
2151 keyfile, identifier, key, NULL);
2154 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2155 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2157 g_free(ipconfig->last_dhcp_address);
2158 ipconfig->last_dhcp_address = str;
2165 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2166 GKeyFile *keyfile, const char *identifier, const char *prefix)
2171 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2173 method = __connman_ipconfig_method2string(ipconfig->method);
2175 key = g_strdup_printf("%smethod", prefix);
2176 g_key_file_set_string(keyfile, identifier, key, method);
2179 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2180 const char *privacy;
2181 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2182 key = g_strdup_printf("%sprivacy", prefix);
2183 g_key_file_set_string(keyfile, identifier, key, privacy);
2187 switch (ipconfig->method) {
2188 case CONNMAN_IPCONFIG_METHOD_FIXED:
2189 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2191 case CONNMAN_IPCONFIG_METHOD_DHCP:
2192 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2193 if (ipconfig->last_dhcp_address != NULL &&
2194 strlen(ipconfig->last_dhcp_address) > 0)
2195 g_key_file_set_string(keyfile, identifier, key,
2196 ipconfig->last_dhcp_address);
2198 g_key_file_remove_key(keyfile, identifier, key, NULL);
2201 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2202 case CONNMAN_IPCONFIG_METHOD_OFF:
2203 case CONNMAN_IPCONFIG_METHOD_AUTO:
2207 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2208 if (ipconfig->address->prefixlen != 0)
2209 g_key_file_set_integer(keyfile, identifier,
2210 key, ipconfig->address->prefixlen);
2213 key = g_strdup_printf("%slocal_address", prefix);
2214 if (ipconfig->address->local != NULL)
2215 g_key_file_set_string(keyfile, identifier,
2216 key, ipconfig->address->local);
2219 key = g_strdup_printf("%speer_address", prefix);
2220 if (ipconfig->address->peer != NULL)
2221 g_key_file_set_string(keyfile, identifier,
2222 key, ipconfig->address->peer);
2225 key = g_strdup_printf("%sbroadcast_address", prefix);
2226 if (ipconfig->address->broadcast != NULL)
2227 g_key_file_set_string(keyfile, identifier,
2228 key, ipconfig->address->broadcast);
2231 key = g_strdup_printf("%sgateway", prefix);
2232 if (ipconfig->address->gateway != NULL)
2233 g_key_file_set_string(keyfile, identifier,
2234 key, ipconfig->address->gateway);
2240 int __connman_ipconfig_init(void)
2244 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2245 NULL, free_ipdevice);
2247 is_ipv6_supported = connman_inet_is_ipv6_supported();
2252 void __connman_ipconfig_cleanup(void)
2256 g_hash_table_destroy(ipdevice_hash);
2257 ipdevice_hash = NULL;