5 * Copyright (C) 2007-2010 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
42 struct connman_ipconfig {
45 enum connman_ipconfig_type type;
47 struct connman_ipconfig *origin;
49 const struct connman_ipconfig_ops *ops;
52 enum connman_ipconfig_method method;
53 struct connman_ipaddress *address;
54 struct connman_ipaddress *system;
56 int ipv6_privacy_config;
59 struct connman_ipdevice {
81 struct connman_ipconfig *config_ipv4;
82 struct connman_ipconfig *config_ipv6;
84 gboolean ipv6_enabled;
88 static GHashTable *ipdevice_hash = NULL;
89 static GList *ipconfig_list = NULL;
91 struct connman_ipaddress *connman_ipaddress_alloc(int family)
93 struct connman_ipaddress *ipaddress;
95 ipaddress = g_try_new0(struct connman_ipaddress, 1);
96 if (ipaddress == NULL)
99 ipaddress->family = family;
100 ipaddress->prefixlen = 0;
101 ipaddress->local = NULL;
102 ipaddress->peer = NULL;
103 ipaddress->broadcast = NULL;
104 ipaddress->gateway = NULL;
109 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
111 if (ipaddress == NULL)
114 g_free(ipaddress->broadcast);
115 g_free(ipaddress->peer);
116 g_free(ipaddress->local);
117 g_free(ipaddress->gateway);
121 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
130 mask = inet_network(netmask);
133 /* a valid netmask must be 2^n - 1 */
134 if ((host & (host + 1)) != 0)
138 for (; mask; mask <<= 1)
144 static gboolean check_ipv6_address(const char *address)
146 unsigned char buf[sizeof(struct in6_addr)];
152 err = inet_pton(AF_INET6, address, buf);
159 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
161 unsigned char prefix_length,
164 if (ipaddress == NULL)
167 if (check_ipv6_address(address) == FALSE)
170 if (check_ipv6_address(gateway) == FALSE)
173 DBG("prefix_len %d address %s gateway %s",
174 prefix_length, address, gateway);
176 ipaddress->family = AF_INET6;
178 ipaddress->prefixlen = prefix_length;
180 g_free(ipaddress->local);
181 ipaddress->local = g_strdup(address);
183 g_free(ipaddress->gateway);
184 ipaddress->gateway = g_strdup(gateway);
189 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
190 const char *address, const char *netmask, const char *gateway)
192 if (ipaddress == NULL)
195 ipaddress->family = AF_INET;
197 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
199 g_free(ipaddress->local);
200 ipaddress->local = g_strdup(address);
202 g_free(ipaddress->gateway);
203 ipaddress->gateway = g_strdup(gateway);
208 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
211 if (ipaddress == NULL)
214 g_free(ipaddress->peer);
215 ipaddress->peer = g_strdup(peer);
218 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
220 if (ipaddress == NULL)
223 ipaddress->prefixlen = 0;
225 g_free(ipaddress->local);
226 ipaddress->local = NULL;
228 g_free(ipaddress->peer);
229 ipaddress->peer = NULL;
231 g_free(ipaddress->broadcast);
232 ipaddress->broadcast = NULL;
234 g_free(ipaddress->gateway);
235 ipaddress->gateway = NULL;
238 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
239 struct connman_ipaddress *source)
241 if (ipaddress == NULL || source == NULL)
244 ipaddress->family = source->family;
245 ipaddress->prefixlen = source->prefixlen;
247 g_free(ipaddress->local);
248 ipaddress->local = g_strdup(source->local);
250 g_free(ipaddress->peer);
251 ipaddress->peer = g_strdup(source->peer);
253 g_free(ipaddress->broadcast);
254 ipaddress->broadcast = g_strdup(source->broadcast);
256 g_free(ipaddress->gateway);
257 ipaddress->gateway = g_strdup(source->gateway);
260 static void free_address_list(struct connman_ipdevice *ipdevice)
264 for (list = ipdevice->address_list; list; list = list->next) {
265 struct connman_ipaddress *ipaddress = list->data;
267 connman_ipaddress_free(ipaddress);
271 g_slist_free(ipdevice->address_list);
272 ipdevice->address_list = NULL;
275 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
276 unsigned char prefixlen, const char *local)
280 for (list = ipdevice->address_list; list; list = list->next) {
281 struct connman_ipaddress *ipaddress = list->data;
283 if (g_strcmp0(ipaddress->local, local) == 0 &&
284 ipaddress->prefixlen == prefixlen)
291 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
294 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
296 case CONNMAN_IPCONFIG_TYPE_IPV4:
298 case CONNMAN_IPCONFIG_TYPE_IPV6:
305 static const char *type2str(unsigned short type)
310 case ARPHRD_LOOPBACK:
323 static const char *scope2str(unsigned char scope)
335 static gboolean get_ipv6_state(gchar *ifname)
340 gboolean enabled = FALSE;
343 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
345 path = g_strdup_printf(
346 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
351 f = fopen(path, "r");
356 if (fscanf(f, "%d", &disabled) > 0)
364 static void set_ipv6_state(gchar *ifname, gboolean enable)
370 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
372 path = g_strdup_printf(
373 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
378 f = fopen(path, "r+");
393 static int get_ipv6_privacy(gchar *ifname)
402 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
408 f = fopen(path, "r");
415 if (fscanf(f, "%d", &value) <= 0)
423 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
424 * The privacy extension is described in RFC 3041 and RFC 4941
426 static void set_ipv6_privacy(gchar *ifname, int value)
434 path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
443 f = fopen(path, "r+");
450 fprintf(f, "%d", value);
454 static void free_ipdevice(gpointer data)
456 struct connman_ipdevice *ipdevice = data;
458 connman_info("%s {remove} index %d", ipdevice->ifname,
461 if (ipdevice->config_ipv4 != NULL) {
462 connman_ipconfig_unref(ipdevice->config_ipv4);
463 ipdevice->config_ipv4 = NULL;
466 if (ipdevice->config_ipv6 != NULL) {
467 connman_ipconfig_unref(ipdevice->config_ipv6);
468 ipdevice->config_ipv6 = NULL;
471 free_address_list(ipdevice);
472 g_free(ipdevice->ipv4_gateway);
473 g_free(ipdevice->ipv6_gateway);
474 g_free(ipdevice->pac);
476 g_free(ipdevice->address);
478 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
479 set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
481 g_free(ipdevice->ifname);
485 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
487 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
488 ipdevice->config_ipv6);
491 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
493 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
494 ipdevice->config_ipv6);
496 if (ipdevice->config_ipv4)
497 connman_inet_clear_address(ipdevice->index,
498 ipdevice->config_ipv4->address);
500 if (ipdevice->config_ipv6)
501 connman_inet_clear_ipv6_address(ipdevice->index,
502 ipdevice->config_ipv6->address->local,
503 ipdevice->config_ipv6->address->prefixlen);
506 static void update_stats(struct connman_ipdevice *ipdevice,
507 struct rtnl_link_stats *stats)
509 struct connman_service *service;
511 if (stats->rx_packets == 0 && stats->tx_packets == 0)
514 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
515 stats->rx_packets, stats->rx_bytes);
516 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
517 stats->tx_packets, stats->tx_bytes);
519 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
522 if (ipdevice->config_ipv4)
523 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
524 else if (ipdevice->config_ipv6)
525 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
532 ipdevice->rx_packets = stats->rx_packets;
533 ipdevice->tx_packets = stats->tx_packets;
534 ipdevice->rx_bytes = stats->rx_bytes;
535 ipdevice->tx_bytes = stats->tx_bytes;
536 ipdevice->rx_errors = stats->rx_errors;
537 ipdevice->tx_errors = stats->tx_errors;
538 ipdevice->rx_dropped = stats->rx_dropped;
539 ipdevice->tx_dropped = stats->tx_dropped;
541 __connman_service_notify(service,
542 ipdevice->rx_packets, ipdevice->tx_packets,
543 ipdevice->rx_bytes, ipdevice->tx_bytes,
544 ipdevice->rx_errors, ipdevice->tx_errors,
545 ipdevice->rx_dropped, ipdevice->tx_dropped);
548 void __connman_ipconfig_newlink(int index, unsigned short type,
549 unsigned int flags, const char *address,
551 struct rtnl_link_stats *stats)
553 struct connman_ipdevice *ipdevice;
556 gboolean up = FALSE, down = FALSE;
557 gboolean lower_up = FALSE, lower_down = FALSE;
559 DBG("index %d", index);
561 if (type == ARPHRD_LOOPBACK)
564 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
565 if (ipdevice != NULL)
568 ipdevice = g_try_new0(struct connman_ipdevice, 1);
569 if (ipdevice == NULL)
572 ipdevice->index = index;
573 ipdevice->ifname = connman_inet_ifname(index);
574 ipdevice->type = type;
576 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
577 ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
579 ipdevice->address = g_strdup(address);
581 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
583 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
584 index, type, type2str(type));
589 update_stats(ipdevice, stats);
591 if (flags == ipdevice->flags)
594 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
601 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
602 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
603 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
604 (IFF_RUNNING | IFF_LOWER_UP))
606 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
610 ipdevice->flags = flags;
612 str = g_string_new(NULL);
617 g_string_append(str, "UP");
619 g_string_append(str, "DOWN");
621 if (flags & IFF_RUNNING)
622 g_string_append(str, ",RUNNING");
624 if (flags & IFF_LOWER_UP)
625 g_string_append(str, ",LOWER_UP");
627 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
630 g_string_free(str, TRUE);
632 for (list = g_list_first(ipconfig_list); list;
633 list = g_list_next(list)) {
634 struct connman_ipconfig *ipconfig = list->data;
636 if (index != ipconfig->index)
639 if (ipconfig->ops == NULL)
642 if (up == TRUE && ipconfig->ops->up)
643 ipconfig->ops->up(ipconfig);
644 if (lower_up == TRUE && ipconfig->ops->lower_up)
645 ipconfig->ops->lower_up(ipconfig);
647 if (lower_down == TRUE && ipconfig->ops->lower_down)
648 ipconfig->ops->lower_down(ipconfig);
649 if (down == TRUE && ipconfig->ops->down)
650 ipconfig->ops->down(ipconfig);
654 __connman_ipconfig_lower_up(ipdevice);
656 __connman_ipconfig_lower_down(ipdevice);
659 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
661 struct connman_ipdevice *ipdevice;
664 DBG("index %d", index);
666 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
667 if (ipdevice == NULL)
670 update_stats(ipdevice, stats);
672 for (list = g_list_first(ipconfig_list); list;
673 list = g_list_next(list)) {
674 struct connman_ipconfig *ipconfig = list->data;
676 if (index != ipconfig->index)
679 ipconfig->index = -1;
681 if (ipconfig->ops == NULL)
684 if (ipconfig->ops->lower_down)
685 ipconfig->ops->lower_down(ipconfig);
686 if (ipconfig->ops->down)
687 ipconfig->ops->down(ipconfig);
690 __connman_ipconfig_lower_down(ipdevice);
692 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
695 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
697 const struct connman_ipaddress *addr1 = a;
698 const struct connman_ipaddress *addr2 = b;
700 if (addr1->prefixlen != addr2->prefixlen)
701 return addr2->prefixlen - addr1->prefixlen;
703 return g_strcmp0(addr1->local, addr2->local);
706 void __connman_ipconfig_newaddr(int index, int family, const char *label,
707 unsigned char prefixlen, const char *address)
709 struct connman_ipdevice *ipdevice;
710 struct connman_ipaddress *ipaddress;
713 DBG("index %d", index);
715 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
716 if (ipdevice == NULL)
719 ipaddress = connman_ipaddress_alloc(family);
720 if (ipaddress == NULL)
723 ipaddress->prefixlen = prefixlen;
724 ipaddress->local = g_strdup(address);
726 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
727 check_duplicate_address)) {
728 connman_ipaddress_free(ipaddress);
732 ipdevice->address_list = g_slist_append(ipdevice->address_list,
735 connman_info("%s {add} address %s/%u label %s family %d",
736 ipdevice->ifname, address, prefixlen, label, family);
738 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
739 connman_ipaddress_copy(ipdevice->config_ipv4->system,
742 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
743 connman_ipaddress_copy(ipdevice->config_ipv6->system,
748 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
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 (ipconfig->ops == NULL)
761 if (ipconfig->ops->ip_bound)
762 ipconfig->ops->ip_bound(ipconfig);
766 void __connman_ipconfig_deladdr(int index, int family, const char *label,
767 unsigned char prefixlen, const char *address)
769 struct connman_ipdevice *ipdevice;
770 struct connman_ipaddress *ipaddress;
773 DBG("index %d", index);
775 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
776 if (ipdevice == NULL)
779 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
780 if (ipaddress == NULL)
783 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
786 connman_ipaddress_clear(ipaddress);
789 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
790 address, prefixlen, label);
792 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
795 if (g_slist_length(ipdevice->address_list) > 0)
798 for (list = g_list_first(ipconfig_list); list;
799 list = g_list_next(list)) {
800 struct connman_ipconfig *ipconfig = list->data;
802 if (index != ipconfig->index)
805 if (ipconfig->ops == NULL)
808 if (ipconfig->ops->ip_release)
809 ipconfig->ops->ip_release(ipconfig);
813 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
814 const char *dst, const char *gateway)
816 struct connman_ipdevice *ipdevice;
818 DBG("index %d", index);
820 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
821 if (ipdevice == NULL)
824 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
825 g_strcmp0(dst, "::") == 0)) {
829 if (family == AF_INET6) {
830 g_free(ipdevice->ipv6_gateway);
831 ipdevice->ipv6_gateway = g_strdup(gateway);
833 if (ipdevice->config_ipv6 != NULL &&
834 ipdevice->config_ipv6->system != NULL) {
835 g_free(ipdevice->config_ipv6->system->gateway);
836 ipdevice->config_ipv6->system->gateway =
839 } else if (family == AF_INET) {
840 g_free(ipdevice->ipv4_gateway);
841 ipdevice->ipv4_gateway = g_strdup(gateway);
843 if (ipdevice->config_ipv4 != NULL &&
844 ipdevice->config_ipv4->system != NULL) {
845 g_free(ipdevice->config_ipv4->system->gateway);
846 ipdevice->config_ipv4->system->gateway =
852 for (list = ipdevice->address_list; list; list = list->next) {
853 struct connman_ipaddress *ipaddress = list->data;
855 g_free(ipaddress->gateway);
856 ipaddress->gateway = g_strdup(gateway);
859 for (config_list = g_list_first(ipconfig_list); config_list;
860 config_list = g_list_next(config_list)) {
861 struct connman_ipconfig *ipconfig = config_list->data;
863 if (index != ipconfig->index)
866 if (ipconfig->ops == NULL)
869 if (ipconfig->ops->ip_bound)
870 ipconfig->ops->ip_bound(ipconfig);
874 connman_info("%s {add} route %s gw %s scope %u <%s>",
875 ipdevice->ifname, dst, gateway,
876 scope, scope2str(scope));
879 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
880 const char *dst, const char *gateway)
882 struct connman_ipdevice *ipdevice;
884 DBG("index %d", index);
886 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
887 if (ipdevice == NULL)
890 if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
891 g_strcmp0(dst, "::") == 0)) {
895 if (family == AF_INET6) {
896 g_free(ipdevice->ipv6_gateway);
897 ipdevice->ipv6_gateway = NULL;
899 if (ipdevice->config_ipv6 != NULL &&
900 ipdevice->config_ipv6->system != NULL) {
901 g_free(ipdevice->config_ipv6->system->gateway);
902 ipdevice->config_ipv6->system->gateway = NULL;
904 } else if (family == AF_INET) {
905 g_free(ipdevice->ipv4_gateway);
906 ipdevice->ipv4_gateway = NULL;
908 if (ipdevice->config_ipv4 != NULL &&
909 ipdevice->config_ipv4->system != NULL) {
910 g_free(ipdevice->config_ipv4->system->gateway);
911 ipdevice->config_ipv4->system->gateway = NULL;
916 for (list = ipdevice->address_list; list; list = list->next) {
917 struct connman_ipaddress *ipaddress = list->data;
919 g_free(ipaddress->gateway);
920 ipaddress->gateway = NULL;
923 for (config_list = g_list_first(ipconfig_list); config_list;
924 config_list = g_list_next(config_list)) {
925 struct connman_ipconfig *ipconfig = config_list->data;
927 if (index != ipconfig->index)
930 if (ipconfig->ops == NULL)
933 if (ipconfig->ops->ip_release)
934 ipconfig->ops->ip_release(ipconfig);
938 connman_info("%s {del} route %s gw %s scope %u <%s>",
939 ipdevice->ifname, dst, gateway,
940 scope, scope2str(scope));
943 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
948 keys = g_hash_table_get_keys(ipdevice_hash);
952 for (list = g_list_first(keys); list; list = g_list_next(list)) {
953 int index = GPOINTER_TO_INT(list->data);
955 function(index, user_data);
961 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
962 struct connman_ipconfig *ipconfig)
964 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
967 unsigned short __connman_ipconfig_get_type_from_index(int index)
969 struct connman_ipdevice *ipdevice;
971 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
972 if (ipdevice == NULL)
975 return ipdevice->type;
978 unsigned int __connman_ipconfig_get_flags_from_index(int index)
980 struct connman_ipdevice *ipdevice;
982 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
983 if (ipdevice == NULL)
986 return ipdevice->flags;
989 const char *__connman_ipconfig_get_gateway_from_index(int index)
991 struct connman_ipdevice *ipdevice;
993 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
994 if (ipdevice == NULL)
997 if (ipdevice->ipv4_gateway != NULL)
998 return ipdevice->ipv4_gateway;
1000 if (ipdevice->config_ipv4 != NULL &&
1001 ipdevice->config_ipv4->address != NULL)
1002 return ipdevice->config_ipv4->address->gateway;
1004 if (ipdevice->ipv6_gateway != NULL)
1005 return ipdevice->ipv6_gateway;
1007 if (ipdevice->config_ipv6 != NULL &&
1008 ipdevice->config_ipv6->address != NULL)
1009 return ipdevice->config_ipv6->address->gateway;
1014 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1016 ipconfig->index = index;
1019 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1021 if (ipconfig->address == NULL)
1024 return ipconfig->address->local;
1027 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1029 if (ipconfig->address == NULL)
1032 g_free(ipconfig->address->local);
1033 ipconfig->address->local = g_strdup(address);
1036 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1038 if (ipconfig->address == NULL)
1041 return ipconfig->address->peer;
1044 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1046 if (ipconfig->address == NULL)
1049 g_free(ipconfig->address->peer);
1050 ipconfig->address->peer = g_strdup(address);
1053 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1055 if (ipconfig->address == NULL)
1058 return ipconfig->address->broadcast;
1061 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1063 if (ipconfig->address == NULL)
1066 g_free(ipconfig->address->broadcast);
1067 ipconfig->address->broadcast = g_strdup(broadcast);
1070 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1072 if (ipconfig->address == NULL)
1075 return ipconfig->address->gateway;
1078 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1082 if (ipconfig->address == NULL)
1084 g_free(ipconfig->address->gateway);
1085 ipconfig->address->gateway = g_strdup(gateway);
1088 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1090 struct connman_service *service;
1094 if (ipconfig->address == NULL)
1097 service = __connman_service_lookup_from_index(ipconfig->index);
1098 if (service == NULL)
1101 __connman_connection_gateway_remove(service, ipconfig->type);
1103 DBG("type %d gw %s peer %s", ipconfig->type,
1104 ipconfig->address->gateway, ipconfig->address->peer);
1106 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1107 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1108 return __connman_connection_gateway_add(service,
1109 ipconfig->address->gateway,
1111 ipconfig->address->peer);
1116 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1118 struct connman_service *service;
1122 service = __connman_service_lookup_from_index(ipconfig->index);
1123 if (service != NULL)
1124 __connman_connection_gateway_remove(service, ipconfig->type);
1127 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1129 if (ipconfig->address == NULL)
1132 return ipconfig->address->prefixlen;
1135 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1137 if (ipconfig->address == NULL)
1140 ipconfig->address->prefixlen = prefixlen;
1143 static struct connman_ipconfig *create_ipv6config(int index)
1145 struct connman_ipconfig *ipv6config;
1147 DBG("index %d", index);
1149 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1150 if (ipv6config == NULL)
1153 ipv6config->refcount = 1;
1155 ipv6config->index = index;
1156 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1157 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1158 ipv6config->ipv6_privacy_config = 0;
1160 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1161 if (ipv6config->address == NULL) {
1166 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1168 DBG("ipconfig %p", ipv6config);
1174 * connman_ipconfig_create:
1176 * Allocate a new ipconfig structure.
1178 * Returns: a newly-allocated #connman_ipconfig structure
1180 struct connman_ipconfig *connman_ipconfig_create(int index,
1181 enum connman_ipconfig_type type)
1183 struct connman_ipconfig *ipconfig;
1185 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1186 return create_ipv6config(index);
1188 DBG("index %d", index);
1190 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1191 if (ipconfig == NULL)
1194 ipconfig->refcount = 1;
1196 ipconfig->index = index;
1197 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1199 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1200 if (ipconfig->address == NULL) {
1205 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1207 DBG("ipconfig %p", ipconfig);
1214 * connman_ipconfig_ref:
1215 * @ipconfig: ipconfig structure
1217 * Increase reference counter of ipconfig
1219 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1221 DBG("ipconfig %p refcount %d", ipconfig,
1222 g_atomic_int_get(&ipconfig->refcount) + 1);
1224 g_atomic_int_inc(&ipconfig->refcount);
1230 * connman_ipconfig_unref:
1231 * @ipconfig: ipconfig structure
1233 * Decrease reference counter of ipconfig
1235 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1237 if (ipconfig == NULL)
1240 DBG("ipconfig %p refcount %d", ipconfig,
1241 g_atomic_int_get(&ipconfig->refcount) - 1);
1243 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1244 __connman_ipconfig_disable(ipconfig);
1246 connman_ipconfig_set_ops(ipconfig, NULL);
1248 if (ipconfig->origin != NULL) {
1249 connman_ipconfig_unref(ipconfig->origin);
1250 ipconfig->origin = NULL;
1253 connman_ipaddress_free(ipconfig->system);
1254 connman_ipaddress_free(ipconfig->address);
1260 * connman_ipconfig_get_data:
1261 * @ipconfig: ipconfig structure
1263 * Get private data pointer
1265 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1267 if (ipconfig == NULL)
1270 return ipconfig->ops_data;
1274 * connman_ipconfig_set_data:
1275 * @ipconfig: ipconfig structure
1276 * @data: data pointer
1278 * Set private data pointer
1280 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1282 ipconfig->ops_data = data;
1286 * connman_ipconfig_get_index:
1287 * @ipconfig: ipconfig structure
1289 * Get interface index
1291 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1293 if (ipconfig == NULL)
1296 if (ipconfig->origin != NULL)
1297 return ipconfig->origin->index;
1299 return ipconfig->index;
1303 * connman_ipconfig_get_ifname:
1304 * @ipconfig: ipconfig structure
1306 * Get interface name
1308 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1310 struct connman_ipdevice *ipdevice;
1312 if (ipconfig == NULL)
1315 if (ipconfig->index < 0)
1318 ipdevice = g_hash_table_lookup(ipdevice_hash,
1319 GINT_TO_POINTER(ipconfig->index));
1320 if (ipdevice == NULL)
1323 return ipdevice->ifname;
1327 * connman_ipconfig_set_ops:
1328 * @ipconfig: ipconfig structure
1329 * @ops: operation callbacks
1331 * Set the operation callbacks
1333 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1334 const struct connman_ipconfig_ops *ops)
1336 ipconfig->ops = ops;
1340 * connman_ipconfig_set_method:
1341 * @ipconfig: ipconfig structure
1342 * @method: configuration method
1344 * Set the configuration method
1346 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1347 enum connman_ipconfig_method method)
1349 ipconfig->method = method;
1354 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1356 if (ipconfig == NULL)
1357 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1359 return ipconfig->method;
1362 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1366 switch (ipconfig->method) {
1367 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1368 case CONNMAN_IPCONFIG_METHOD_OFF:
1369 case CONNMAN_IPCONFIG_METHOD_AUTO:
1371 case CONNMAN_IPCONFIG_METHOD_FIXED:
1372 case CONNMAN_IPCONFIG_METHOD_DHCP:
1373 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1374 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1375 return connman_inet_set_address(ipconfig->index,
1377 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1378 return connman_inet_set_ipv6_address(
1379 ipconfig->index, ipconfig->address);
1385 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1391 if (ipconfig == NULL)
1394 DBG("method %d", ipconfig->method);
1396 switch (ipconfig->method) {
1397 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1398 case CONNMAN_IPCONFIG_METHOD_OFF:
1399 case CONNMAN_IPCONFIG_METHOD_AUTO:
1401 case CONNMAN_IPCONFIG_METHOD_FIXED:
1402 case CONNMAN_IPCONFIG_METHOD_DHCP:
1403 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1404 err = __connman_ipconfig_address_unset(ipconfig);
1405 connman_ipaddress_clear(ipconfig->address);
1413 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1419 if (ipconfig == NULL)
1422 DBG("method %d", ipconfig->method);
1424 switch (ipconfig->method) {
1425 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1426 case CONNMAN_IPCONFIG_METHOD_OFF:
1427 case CONNMAN_IPCONFIG_METHOD_AUTO:
1429 case CONNMAN_IPCONFIG_METHOD_FIXED:
1430 case CONNMAN_IPCONFIG_METHOD_DHCP:
1431 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1432 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1433 err = connman_inet_clear_address(ipconfig->index,
1435 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1436 err = connman_inet_clear_ipv6_address(
1438 ipconfig->address->local,
1439 ipconfig->address->prefixlen);
1449 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1452 struct connman_ipdevice *ipdevice;
1454 DBG("ipconfig %p", ipconfig);
1456 if (ipconfig == NULL || ipconfig->index < 0)
1459 ipdevice = g_hash_table_lookup(ipdevice_hash,
1460 GINT_TO_POINTER(ipconfig->index));
1461 if (ipdevice == NULL)
1464 g_free(ipdevice->pac);
1465 ipdevice->pac = g_strdup(url);
1470 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1472 struct connman_ipdevice *ipdevice;
1474 DBG("ipconfig %p", ipconfig);
1476 if (ipconfig == NULL || ipconfig->index < 0)
1479 ipdevice = g_hash_table_lookup(ipdevice_hash,
1480 GINT_TO_POINTER(ipconfig->index));
1481 if (ipdevice == NULL)
1484 return ipdevice->pac;
1487 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1489 struct connman_ipdevice *ipdevice;
1493 ipdevice = g_hash_table_lookup(ipdevice_hash,
1494 GINT_TO_POINTER(ipconfig->index));
1495 if (ipdevice == NULL)
1498 set_ipv6_state(ipdevice->ifname, FALSE);
1501 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1503 struct connman_ipdevice *ipdevice;
1507 ipdevice = g_hash_table_lookup(ipdevice_hash,
1508 GINT_TO_POINTER(ipconfig->index));
1509 if (ipdevice == NULL)
1512 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1513 set_ipv6_privacy(ipdevice->ifname,
1514 ipconfig->ipv6_privacy_config);
1516 set_ipv6_state(ipdevice->ifname, TRUE);
1519 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1521 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1524 enable_ipv6(ipconfig);
1527 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1529 if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1532 disable_ipv6(ipconfig);
1535 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1537 struct connman_ipdevice *ipdevice;
1538 gboolean up = FALSE, down = FALSE;
1539 gboolean lower_up = FALSE, lower_down = FALSE;
1540 enum connman_ipconfig_type type;
1542 DBG("ipconfig %p", ipconfig);
1544 if (ipconfig == NULL || ipconfig->index < 0)
1547 ipdevice = g_hash_table_lookup(ipdevice_hash,
1548 GINT_TO_POINTER(ipconfig->index));
1549 if (ipdevice == NULL)
1552 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1553 if (ipdevice->config_ipv4 == ipconfig)
1555 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1556 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1557 if (ipdevice->config_ipv6 == ipconfig)
1559 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1560 enable_ipv6(ipconfig);
1564 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1565 ipdevice->config_ipv4 != NULL) {
1566 ipconfig_list = g_list_remove(ipconfig_list,
1567 ipdevice->config_ipv4);
1569 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1571 connman_ipconfig_unref(ipdevice->config_ipv4);
1574 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1575 ipdevice->config_ipv6 != NULL) {
1576 ipconfig_list = g_list_remove(ipconfig_list,
1577 ipdevice->config_ipv6);
1579 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1581 connman_ipconfig_unref(ipdevice->config_ipv6);
1584 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1585 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1586 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1587 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1589 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1591 if (ipdevice->flags & IFF_UP)
1596 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1597 (IFF_RUNNING | IFF_LOWER_UP))
1599 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1602 if (up == TRUE && ipconfig->ops->up)
1603 ipconfig->ops->up(ipconfig);
1604 if (lower_up == TRUE && ipconfig->ops->lower_up)
1605 ipconfig->ops->lower_up(ipconfig);
1607 if (lower_down == TRUE && ipconfig->ops->lower_down)
1608 ipconfig->ops->lower_down(ipconfig);
1609 if (down == TRUE && ipconfig->ops->down)
1610 ipconfig->ops->down(ipconfig);
1615 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1617 struct connman_ipdevice *ipdevice;
1619 DBG("ipconfig %p", ipconfig);
1621 if (ipconfig == NULL || ipconfig->index < 0)
1624 ipdevice = g_hash_table_lookup(ipdevice_hash,
1625 GINT_TO_POINTER(ipconfig->index));
1626 if (ipdevice == NULL)
1629 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1632 if (ipdevice->config_ipv4 == ipconfig) {
1633 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1635 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1636 connman_ipconfig_unref(ipdevice->config_ipv4);
1637 ipdevice->config_ipv4 = NULL;
1641 if (ipdevice->config_ipv6 == ipconfig) {
1642 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1644 if (ipdevice->config_ipv6->method ==
1645 CONNMAN_IPCONFIG_METHOD_AUTO)
1646 disable_ipv6(ipdevice->config_ipv6);
1648 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1649 connman_ipconfig_unref(ipdevice->config_ipv6);
1650 ipdevice->config_ipv6 = NULL;
1657 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1660 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1662 case CONNMAN_IPCONFIG_METHOD_OFF:
1664 case CONNMAN_IPCONFIG_METHOD_FIXED:
1666 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1668 case CONNMAN_IPCONFIG_METHOD_DHCP:
1670 case CONNMAN_IPCONFIG_METHOD_AUTO:
1677 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1679 if (g_strcmp0(method, "off") == 0)
1680 return CONNMAN_IPCONFIG_METHOD_OFF;
1681 else if (g_strcmp0(method, "fixed") == 0)
1682 return CONNMAN_IPCONFIG_METHOD_FIXED;
1683 else if (g_strcmp0(method, "manual") == 0)
1684 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1685 else if (g_strcmp0(method, "dhcp") == 0)
1686 return CONNMAN_IPCONFIG_METHOD_DHCP;
1687 else if (g_strcmp0(method, "auto") == 0)
1688 return CONNMAN_IPCONFIG_METHOD_AUTO;
1690 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1693 static const char *privacy2string(int privacy)
1697 else if (privacy == 1)
1699 else if (privacy > 1)
1705 static int string2privacy(const char *privacy)
1707 if (g_strcmp0(privacy, "disabled") == 0)
1709 else if (g_strcmp0(privacy, "enabled") == 0)
1711 else if (g_strcmp0(privacy, "prefered") == 0)
1717 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1718 DBusMessageIter *iter)
1724 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1727 str = __connman_ipconfig_method2string(ipconfig->method);
1731 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1733 if (ipconfig->system == NULL)
1736 if (ipconfig->system->local != NULL) {
1738 struct in_addr netmask;
1741 connman_dbus_dict_append_basic(iter, "Address",
1742 DBUS_TYPE_STRING, &ipconfig->system->local);
1744 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1745 netmask.s_addr = htonl(addr);
1746 mask = inet_ntoa(netmask);
1747 connman_dbus_dict_append_basic(iter, "Netmask",
1748 DBUS_TYPE_STRING, &mask);
1751 if (ipconfig->system->gateway != NULL)
1752 connman_dbus_dict_append_basic(iter, "Gateway",
1753 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1756 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1757 DBusMessageIter *iter,
1758 struct connman_ipconfig *ipconfig_ipv4)
1760 const char *str, *privacy;
1764 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1767 str = __connman_ipconfig_method2string(ipconfig->method);
1771 if (ipconfig_ipv4 != NULL &&
1772 ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1773 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1777 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1779 if (ipconfig->system == NULL)
1782 if (ipconfig->system->local != NULL) {
1783 connman_dbus_dict_append_basic(iter, "Address",
1784 DBUS_TYPE_STRING, &ipconfig->system->local);
1785 connman_dbus_dict_append_basic(iter, "PrefixLength",
1787 &ipconfig->system->prefixlen);
1790 if (ipconfig->system->gateway != NULL)
1791 connman_dbus_dict_append_basic(iter, "Gateway",
1792 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1794 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1795 connman_dbus_dict_append_basic(iter, "Privacy",
1796 DBUS_TYPE_STRING, &privacy);
1799 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1800 DBusMessageIter *iter)
1802 const char *str, *privacy;
1806 str = __connman_ipconfig_method2string(ipconfig->method);
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:
1815 case CONNMAN_IPCONFIG_METHOD_DHCP:
1817 case CONNMAN_IPCONFIG_METHOD_FIXED:
1818 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1819 case CONNMAN_IPCONFIG_METHOD_AUTO:
1823 if (ipconfig->address == NULL)
1826 if (ipconfig->address->local != NULL) {
1827 connman_dbus_dict_append_basic(iter, "Address",
1828 DBUS_TYPE_STRING, &ipconfig->address->local);
1829 connman_dbus_dict_append_basic(iter, "PrefixLength",
1831 &ipconfig->address->prefixlen);
1834 if (ipconfig->address->gateway != NULL)
1835 connman_dbus_dict_append_basic(iter, "Gateway",
1836 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1838 privacy = privacy2string(ipconfig->ipv6_privacy_config);
1839 connman_dbus_dict_append_basic(iter, "Privacy",
1840 DBUS_TYPE_STRING, &privacy);
1843 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1844 DBusMessageIter *iter)
1850 str = __connman_ipconfig_method2string(ipconfig->method);
1854 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1856 switch (ipconfig->method) {
1857 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1858 case CONNMAN_IPCONFIG_METHOD_OFF:
1859 case CONNMAN_IPCONFIG_METHOD_FIXED:
1860 case CONNMAN_IPCONFIG_METHOD_DHCP:
1861 case CONNMAN_IPCONFIG_METHOD_AUTO:
1863 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1867 if (ipconfig->address == NULL)
1870 if (ipconfig->address->local != NULL) {
1872 struct in_addr netmask;
1875 connman_dbus_dict_append_basic(iter, "Address",
1876 DBUS_TYPE_STRING, &ipconfig->address->local);
1878 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1879 netmask.s_addr = htonl(addr);
1880 mask = inet_ntoa(netmask);
1881 connman_dbus_dict_append_basic(iter, "Netmask",
1882 DBUS_TYPE_STRING, &mask);
1885 if (ipconfig->address->gateway != NULL)
1886 connman_dbus_dict_append_basic(iter, "Gateway",
1887 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1890 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1891 DBusMessageIter *array)
1893 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1894 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1895 *prefix_length_string = NULL, *privacy_string = NULL;
1896 int prefix_length = 0, privacy = 0;
1897 DBusMessageIter dict;
1899 DBG("ipconfig %p", ipconfig);
1901 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1904 dbus_message_iter_recurse(array, &dict);
1906 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1907 DBusMessageIter entry;
1911 dbus_message_iter_recurse(&dict, &entry);
1913 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1916 dbus_message_iter_get_basic(&entry, &key);
1917 dbus_message_iter_next(&entry);
1919 type = dbus_message_iter_get_arg_type(&entry);
1921 if (g_str_equal(key, "Method") == TRUE) {
1924 if (type != DBUS_TYPE_STRING)
1927 dbus_message_iter_get_basic(&entry, &str);
1928 method = __connman_ipconfig_string2method(str);
1929 } else if (g_str_equal(key, "Address") == TRUE) {
1930 if (type != DBUS_TYPE_STRING)
1933 dbus_message_iter_get_basic(&entry, &address);
1934 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1935 if (type != DBUS_TYPE_STRING)
1938 dbus_message_iter_get_basic(&entry,
1939 &prefix_length_string);
1941 prefix_length = atoi(prefix_length_string);
1942 if (prefix_length < 0 || prefix_length > 128)
1945 } else if (g_str_equal(key, "Netmask") == TRUE) {
1946 if (type != DBUS_TYPE_STRING)
1949 dbus_message_iter_get_basic(&entry, &netmask);
1950 } else if (g_str_equal(key, "Gateway") == TRUE) {
1951 if (type != DBUS_TYPE_STRING)
1954 dbus_message_iter_get_basic(&entry, &gateway);
1955 } else if (g_str_equal(key, "Privacy") == TRUE) {
1956 if (type != DBUS_TYPE_STRING)
1959 dbus_message_iter_get_basic(&entry, &privacy_string);
1960 privacy = string2privacy(privacy_string);
1962 dbus_message_iter_next(&dict);
1965 DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1967 method, address, netmask, gateway, prefix_length,
1971 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1972 case CONNMAN_IPCONFIG_METHOD_FIXED:
1975 case CONNMAN_IPCONFIG_METHOD_OFF:
1976 ipconfig->method = method;
1977 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1978 disable_ipv6(ipconfig);
1981 case CONNMAN_IPCONFIG_METHOD_AUTO:
1982 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1985 ipconfig->method = method;
1986 if (privacy_string != NULL)
1987 ipconfig->ipv6_privacy_config = privacy;
1988 enable_ipv6(ipconfig);
1991 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1992 if (address == NULL)
1995 ipconfig->method = method;
1997 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1998 connman_ipaddress_set_ipv4(ipconfig->address,
1999 address, netmask, gateway);
2001 return connman_ipaddress_set_ipv6(
2002 ipconfig->address, address,
2003 prefix_length, gateway);
2006 case CONNMAN_IPCONFIG_METHOD_DHCP:
2007 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2010 ipconfig->method = method;
2017 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2018 DBusMessageIter *iter)
2020 struct connman_ipdevice *ipdevice;
2021 const char *method = "auto";
2023 connman_dbus_dict_append_basic(iter, "Method",
2024 DBUS_TYPE_STRING, &method);
2026 ipdevice = g_hash_table_lookup(ipdevice_hash,
2027 GINT_TO_POINTER(ipconfig->index));
2028 if (ipdevice == NULL)
2031 if (ipdevice->ifname != NULL)
2032 connman_dbus_dict_append_basic(iter, "Interface",
2033 DBUS_TYPE_STRING, &ipdevice->ifname);
2035 if (ipdevice->address != NULL)
2036 connman_dbus_dict_append_basic(iter, "Address",
2037 DBUS_TYPE_STRING, &ipdevice->address);
2039 if (ipdevice->mtu > 0)
2040 connman_dbus_dict_append_basic(iter, "MTU",
2041 DBUS_TYPE_UINT16, &ipdevice->mtu);
2044 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2045 GKeyFile *keyfile, const char *identifier, const char *prefix)
2050 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2052 key = g_strdup_printf("%smethod", prefix);
2053 method = g_key_file_get_string(keyfile, identifier, key, NULL);
2054 if (method == NULL) {
2055 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2056 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2058 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2060 ipconfig->method = __connman_ipconfig_string2method(method);
2062 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2063 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2065 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2066 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2067 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2069 char *pprefix = g_strdup_printf("%sprivacy", prefix);
2070 privacy = g_key_file_get_string(keyfile, identifier,
2072 ipconfig->ipv6_privacy_config = string2privacy(privacy);
2076 __connman_ipconfig_enable(ipconfig);
2077 enable_ipv6(ipconfig);
2084 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2085 ipconfig->address->prefixlen = g_key_file_get_integer(
2086 keyfile, identifier, key, NULL);
2089 key = g_strdup_printf("%slocal_address", prefix);
2090 ipconfig->address->local = g_key_file_get_string(
2091 keyfile, identifier, key, NULL);
2094 key = g_strdup_printf("%speer_address", prefix);
2095 ipconfig->address->peer = g_key_file_get_string(
2096 keyfile, identifier, key, NULL);
2099 key = g_strdup_printf("%sbroadcast_address", prefix);
2100 ipconfig->address->broadcast = g_key_file_get_string(
2101 keyfile, identifier, key, NULL);
2104 key = g_strdup_printf("%sgateway", prefix);
2105 ipconfig->address->gateway = g_key_file_get_string(
2106 keyfile, identifier, key, NULL);
2112 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2113 GKeyFile *keyfile, const char *identifier, const char *prefix)
2118 DBG("ipconfig %p identifier %s", ipconfig, identifier);
2120 method = __connman_ipconfig_method2string(ipconfig->method);
2122 key = g_strdup_printf("%smethod", prefix);
2123 g_key_file_set_string(keyfile, identifier, key, method);
2126 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2127 const char *privacy;
2128 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2129 key = g_strdup_printf("%sprivacy", prefix);
2130 g_key_file_set_string(keyfile, identifier, key, privacy);
2134 switch (ipconfig->method) {
2135 case CONNMAN_IPCONFIG_METHOD_FIXED:
2136 case CONNMAN_IPCONFIG_METHOD_MANUAL:
2138 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2139 case CONNMAN_IPCONFIG_METHOD_OFF:
2140 case CONNMAN_IPCONFIG_METHOD_DHCP:
2141 case CONNMAN_IPCONFIG_METHOD_AUTO:
2145 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2146 g_key_file_set_integer(keyfile, identifier,
2147 key, ipconfig->address->prefixlen);
2150 key = g_strdup_printf("%slocal_address", prefix);
2151 if (ipconfig->address->local != NULL)
2152 g_key_file_set_string(keyfile, identifier,
2153 key, ipconfig->address->local);
2156 key = g_strdup_printf("%speer_address", prefix);
2157 if (ipconfig->address->peer != NULL)
2158 g_key_file_set_string(keyfile, identifier,
2159 key, ipconfig->address->peer);
2162 key = g_strdup_printf("%sbroadcast_address", prefix);
2163 if (ipconfig->address->broadcast != NULL)
2164 g_key_file_set_string(keyfile, identifier,
2165 key, ipconfig->address->broadcast);
2168 key = g_strdup_printf("%sgateway", prefix);
2169 if (ipconfig->address->gateway != NULL)
2170 g_key_file_set_string(keyfile, identifier,
2171 key, ipconfig->address->gateway);
2177 int __connman_ipconfig_init(void)
2181 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2182 NULL, free_ipdevice);
2187 void __connman_ipconfig_cleanup(void)
2191 g_hash_table_destroy(ipdevice_hash);
2192 ipdevice_hash = NULL;