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
28 #include <net/if_arp.h>
29 #include <linux/if_link.h>
34 #define IFF_LOWER_UP 0x10000
41 struct connman_ipconfig {
44 enum connman_ipconfig_type type;
46 struct connman_ipconfig *origin;
48 const struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
52 struct connman_ipaddress *address;
53 struct connman_ipaddress *system;
56 struct connman_ipdevice {
78 struct connman_ipconfig *config_ipv4;
79 struct connman_ipconfig *config_ipv6;
81 gboolean ipv6_enabled;
84 static GHashTable *ipdevice_hash = NULL;
85 static GList *ipconfig_list = NULL;
87 struct connman_ipaddress *connman_ipaddress_alloc(int family)
89 struct connman_ipaddress *ipaddress;
91 ipaddress = g_try_new0(struct connman_ipaddress, 1);
92 if (ipaddress == NULL)
95 ipaddress->family = family;
96 ipaddress->prefixlen = 0;
97 ipaddress->local = NULL;
98 ipaddress->peer = NULL;
99 ipaddress->broadcast = NULL;
100 ipaddress->gateway = NULL;
105 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
107 if (ipaddress == NULL)
110 g_free(ipaddress->broadcast);
111 g_free(ipaddress->peer);
112 g_free(ipaddress->local);
113 g_free(ipaddress->gateway);
117 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
126 mask = inet_network(netmask);
129 /* a valid netmask must be 2^n - 1 */
130 if ((host & (host + 1)) != 0)
134 for (; mask; mask <<= 1)
140 static gboolean check_ipv6_address(const char *address)
142 unsigned char buf[sizeof(struct in6_addr)];
145 err = inet_pton(AF_INET6, address, buf);
152 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
153 const char *address, const char *gateway,
154 unsigned char prefix_length)
156 if (ipaddress == NULL)
159 if (check_ipv6_address(address) == FALSE)
162 if (check_ipv6_address(gateway) == FALSE)
165 DBG("prefix_len %d address %s gateway %s",
166 prefix_length, address, gateway);
168 ipaddress->prefixlen = prefix_length;
170 g_free(ipaddress->local);
171 ipaddress->local = g_strdup(address);
173 g_free(ipaddress->gateway);
174 ipaddress->gateway = g_strdup(gateway);
179 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
180 const char *address, const char *netmask, const char *gateway)
182 if (ipaddress == NULL)
185 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
187 g_free(ipaddress->local);
188 ipaddress->local = g_strdup(address);
190 g_free(ipaddress->gateway);
191 ipaddress->gateway = g_strdup(gateway);
194 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
196 if (ipaddress == NULL)
199 ipaddress->prefixlen = 0;
201 g_free(ipaddress->local);
202 ipaddress->local = NULL;
204 g_free(ipaddress->peer);
205 ipaddress->peer = NULL;
207 g_free(ipaddress->broadcast);
208 ipaddress->broadcast = NULL;
210 g_free(ipaddress->gateway);
211 ipaddress->gateway = NULL;
214 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
215 struct connman_ipaddress *source)
217 if (ipaddress == NULL || source == NULL)
220 ipaddress->family = source->family;
221 ipaddress->prefixlen = source->prefixlen;
223 g_free(ipaddress->local);
224 ipaddress->local = g_strdup(source->local);
226 g_free(ipaddress->peer);
227 ipaddress->peer = g_strdup(source->peer);
229 g_free(ipaddress->broadcast);
230 ipaddress->broadcast = g_strdup(source->broadcast);
232 g_free(ipaddress->gateway);
233 ipaddress->gateway = g_strdup(source->gateway);
236 static void free_address_list(struct connman_ipdevice *ipdevice)
240 for (list = ipdevice->address_list; list; list = list->next) {
241 struct connman_ipaddress *ipaddress = list->data;
243 connman_ipaddress_free(ipaddress);
247 g_slist_free(ipdevice->address_list);
248 ipdevice->address_list = NULL;
251 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
252 unsigned char prefixlen, const char *local)
256 for (list = ipdevice->address_list; list; list = list->next) {
257 struct connman_ipaddress *ipaddress = list->data;
259 if (g_strcmp0(ipaddress->local, local) == 0 &&
260 ipaddress->prefixlen == prefixlen)
267 static const char *type2str(unsigned short type)
272 case ARPHRD_LOOPBACK:
285 static const char *scope2str(unsigned char scope)
297 static gboolean get_ipv6_state(gchar *ifname)
302 gboolean enabled = FALSE;
305 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
307 path = g_strdup_printf(
308 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
313 f = fopen(path, "r");
318 if (fscanf(f, "%d", &disabled) > 0)
326 static void set_ipv6_state(gchar *ifname, gboolean enable)
332 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
334 path = g_strdup_printf(
335 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
340 f = fopen(path, "r+");
355 static void free_ipdevice(gpointer data)
357 struct connman_ipdevice *ipdevice = data;
359 connman_info("%s {remove} index %d", ipdevice->ifname,
362 if (ipdevice->config_ipv4 != NULL) {
363 connman_ipconfig_unref(ipdevice->config_ipv4);
364 ipdevice->config_ipv4 = NULL;
367 if (ipdevice->config_ipv6 != NULL) {
368 connman_ipconfig_unref(ipdevice->config_ipv6);
369 ipdevice->config_ipv6 = NULL;
372 free_address_list(ipdevice);
373 g_free(ipdevice->ipv4_gateway);
374 g_free(ipdevice->ipv6_gateway);
375 g_free(ipdevice->pac);
377 g_free(ipdevice->address);
379 set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
381 g_free(ipdevice->ifname);
385 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
387 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
388 ipdevice->config_ipv6);
391 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
393 DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
394 ipdevice->config_ipv6);
396 if (ipdevice->config_ipv4)
397 connman_inet_clear_address(ipdevice->index,
398 ipdevice->config_ipv4->address);
400 if (ipdevice->config_ipv6)
401 connman_inet_clear_ipv6_address(ipdevice->index,
402 ipdevice->config_ipv6->address->local,
403 ipdevice->config_ipv6->address->prefixlen);
406 static void update_stats(struct connman_ipdevice *ipdevice,
407 struct rtnl_link_stats *stats)
409 struct connman_service *service;
411 if (stats->rx_packets == 0 && stats->tx_packets == 0)
414 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
415 stats->rx_packets, stats->rx_bytes);
416 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
417 stats->tx_packets, stats->tx_bytes);
419 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
422 if (ipdevice->config_ipv4)
423 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
424 else if (ipdevice->config_ipv6)
425 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
432 ipdevice->rx_packets = stats->rx_packets;
433 ipdevice->tx_packets = stats->tx_packets;
434 ipdevice->rx_bytes = stats->rx_bytes;
435 ipdevice->tx_bytes = stats->tx_bytes;
436 ipdevice->rx_errors = stats->rx_errors;
437 ipdevice->tx_errors = stats->tx_errors;
438 ipdevice->rx_dropped = stats->rx_dropped;
439 ipdevice->tx_dropped = stats->tx_dropped;
441 __connman_service_notify(service,
442 ipdevice->rx_packets, ipdevice->tx_packets,
443 ipdevice->rx_bytes, ipdevice->tx_bytes,
444 ipdevice->rx_errors, ipdevice->tx_errors,
445 ipdevice->rx_dropped, ipdevice->tx_dropped);
448 void __connman_ipconfig_newlink(int index, unsigned short type,
449 unsigned int flags, const char *address,
451 struct rtnl_link_stats *stats)
453 struct connman_ipdevice *ipdevice;
456 gboolean up = FALSE, down = FALSE;
457 gboolean lower_up = FALSE, lower_down = FALSE;
459 DBG("index %d", index);
461 if (type == ARPHRD_LOOPBACK)
464 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
465 if (ipdevice != NULL)
468 ipdevice = g_try_new0(struct connman_ipdevice, 1);
469 if (ipdevice == NULL)
472 ipdevice->index = index;
473 ipdevice->ifname = connman_inet_ifname(index);
474 ipdevice->type = type;
476 ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
478 ipdevice->address = g_strdup(address);
480 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
482 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
483 index, type, type2str(type));
488 update_stats(ipdevice, stats);
490 if (flags == ipdevice->flags)
493 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
500 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
501 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
502 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
503 (IFF_RUNNING | IFF_LOWER_UP))
505 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
509 ipdevice->flags = flags;
511 str = g_string_new(NULL);
516 g_string_append(str, "UP");
518 g_string_append(str, "DOWN");
520 if (flags & IFF_RUNNING)
521 g_string_append(str, ",RUNNING");
523 if (flags & IFF_LOWER_UP)
524 g_string_append(str, ",LOWER_UP");
526 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
529 g_string_free(str, TRUE);
531 for (list = g_list_first(ipconfig_list); list;
532 list = g_list_next(list)) {
533 struct connman_ipconfig *ipconfig = list->data;
535 if (index != ipconfig->index)
538 if (ipconfig->ops == NULL)
541 if (up == TRUE && ipconfig->ops->up)
542 ipconfig->ops->up(ipconfig);
543 if (lower_up == TRUE && ipconfig->ops->lower_up)
544 ipconfig->ops->lower_up(ipconfig);
546 if (lower_down == TRUE && ipconfig->ops->lower_down)
547 ipconfig->ops->lower_down(ipconfig);
548 if (down == TRUE && ipconfig->ops->down)
549 ipconfig->ops->down(ipconfig);
553 __connman_ipconfig_lower_up(ipdevice);
555 __connman_ipconfig_lower_down(ipdevice);
558 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
560 struct connman_ipdevice *ipdevice;
563 DBG("index %d", index);
565 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
566 if (ipdevice == NULL)
569 update_stats(ipdevice, stats);
571 for (list = g_list_first(ipconfig_list); list;
572 list = g_list_next(list)) {
573 struct connman_ipconfig *ipconfig = list->data;
575 if (index != ipconfig->index)
578 ipconfig->index = -1;
580 if (ipconfig->ops == NULL)
583 if (ipconfig->ops->lower_down)
584 ipconfig->ops->lower_down(ipconfig);
585 if (ipconfig->ops->down)
586 ipconfig->ops->down(ipconfig);
589 __connman_ipconfig_lower_down(ipdevice);
591 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
594 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
596 const struct connman_ipaddress *addr1 = a;
597 const struct connman_ipaddress *addr2 = b;
599 if (addr1->prefixlen != addr2->prefixlen)
600 return addr2->prefixlen - addr1->prefixlen;
602 return g_strcmp0(addr1->local, addr2->local);
605 void __connman_ipconfig_newaddr(int index, int family, const char *label,
606 unsigned char prefixlen, const char *address)
608 struct connman_ipdevice *ipdevice;
609 struct connman_ipaddress *ipaddress;
612 DBG("index %d", index);
614 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
615 if (ipdevice == NULL)
618 ipaddress = connman_ipaddress_alloc(family);
619 if (ipaddress == NULL)
622 ipaddress->prefixlen = prefixlen;
623 ipaddress->local = g_strdup(address);
625 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
626 check_duplicate_address)) {
627 connman_ipaddress_free(ipaddress);
631 ipdevice->address_list = g_slist_append(ipdevice->address_list,
634 connman_info("%s {add} address %s/%u label %s family %d",
635 ipdevice->ifname, address, prefixlen, label, family);
637 if (ipdevice->config_ipv4 != NULL && family == AF_INET)
638 connman_ipaddress_copy(ipdevice->config_ipv4->system,
641 else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
642 connman_ipaddress_copy(ipdevice->config_ipv6->system,
647 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
650 for (list = g_list_first(ipconfig_list); list;
651 list = g_list_next(list)) {
652 struct connman_ipconfig *ipconfig = list->data;
654 if (index != ipconfig->index)
657 if (ipconfig->ops == NULL)
660 if (ipconfig->ops->ip_bound)
661 ipconfig->ops->ip_bound(ipconfig);
665 void __connman_ipconfig_deladdr(int index, int family, const char *label,
666 unsigned char prefixlen, const char *address)
668 struct connman_ipdevice *ipdevice;
669 struct connman_ipaddress *ipaddress;
672 DBG("index %d", index);
674 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
675 if (ipdevice == NULL)
678 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
679 if (ipaddress == NULL)
682 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
685 connman_ipaddress_clear(ipaddress);
688 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
689 address, prefixlen, label);
691 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
694 if (g_slist_length(ipdevice->address_list) > 0)
697 for (list = g_list_first(ipconfig_list); list;
698 list = g_list_next(list)) {
699 struct connman_ipconfig *ipconfig = list->data;
701 if (index != ipconfig->index)
704 if (ipconfig->ops == NULL)
707 if (ipconfig->ops->ip_release)
708 ipconfig->ops->ip_release(ipconfig);
712 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
713 const char *dst, const char *gateway)
715 struct connman_ipdevice *ipdevice;
717 DBG("index %d", index);
719 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
720 if (ipdevice == NULL)
723 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
727 if (family == AF_INET6) {
728 g_free(ipdevice->ipv6_gateway);
729 ipdevice->ipv6_gateway = g_strdup(gateway);
731 if (ipdevice->config_ipv6 != NULL &&
732 ipdevice->config_ipv6->system != NULL) {
733 g_free(ipdevice->config_ipv6->system->gateway);
734 ipdevice->config_ipv6->system->gateway =
738 g_free(ipdevice->ipv4_gateway);
739 ipdevice->ipv4_gateway = g_strdup(gateway);
741 if (ipdevice->config_ipv4 != NULL &&
742 ipdevice->config_ipv4->system != NULL) {
743 g_free(ipdevice->config_ipv4->system->gateway);
744 ipdevice->config_ipv4->system->gateway =
749 for (list = ipdevice->address_list; list; list = list->next) {
750 struct connman_ipaddress *ipaddress = list->data;
752 g_free(ipaddress->gateway);
753 ipaddress->gateway = g_strdup(gateway);
756 for (config_list = g_list_first(ipconfig_list); config_list;
757 config_list = g_list_next(config_list)) {
758 struct connman_ipconfig *ipconfig = config_list->data;
760 if (index != ipconfig->index)
763 if (ipconfig->ops == NULL)
766 if (ipconfig->ops->ip_bound)
767 ipconfig->ops->ip_bound(ipconfig);
771 connman_info("%s {add} route %s gw %s scope %u <%s>",
772 ipdevice->ifname, dst, gateway,
773 scope, scope2str(scope));
776 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
777 const char *dst, const char *gateway)
779 struct connman_ipdevice *ipdevice;
781 DBG("index %d", index);
783 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
784 if (ipdevice == NULL)
787 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
791 if (family == AF_INET6) {
792 g_free(ipdevice->ipv6_gateway);
793 ipdevice->ipv6_gateway = NULL;
795 if (ipdevice->config_ipv6 != NULL &&
796 ipdevice->config_ipv6->system != NULL) {
797 g_free(ipdevice->config_ipv6->system->gateway);
798 ipdevice->config_ipv6->system->gateway = NULL;
801 g_free(ipdevice->ipv4_gateway);
802 ipdevice->ipv4_gateway = NULL;
804 if (ipdevice->config_ipv4 != NULL &&
805 ipdevice->config_ipv4->system != NULL) {
806 g_free(ipdevice->config_ipv4->system->gateway);
807 ipdevice->config_ipv4->system->gateway = NULL;
811 for (list = ipdevice->address_list; list; list = list->next) {
812 struct connman_ipaddress *ipaddress = list->data;
814 g_free(ipaddress->gateway);
815 ipaddress->gateway = NULL;
818 for (config_list = g_list_first(ipconfig_list); config_list;
819 config_list = g_list_next(config_list)) {
820 struct connman_ipconfig *ipconfig = config_list->data;
822 if (index != ipconfig->index)
825 if (ipconfig->ops == NULL)
828 if (ipconfig->ops->ip_release)
829 ipconfig->ops->ip_release(ipconfig);
833 connman_info("%s {del} route %s gw %s scope %u <%s>",
834 ipdevice->ifname, dst, gateway,
835 scope, scope2str(scope));
838 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
843 keys = g_hash_table_get_keys(ipdevice_hash);
847 for (list = g_list_first(keys); list; list = g_list_next(list)) {
848 int index = GPOINTER_TO_INT(list->data);
850 function(index, user_data);
856 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
857 struct connman_ipconfig *ipconfig)
859 return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
862 unsigned short __connman_ipconfig_get_type_from_index(int index)
864 struct connman_ipdevice *ipdevice;
866 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
867 if (ipdevice == NULL)
870 return ipdevice->type;
873 unsigned int __connman_ipconfig_get_flags_from_index(int index)
875 struct connman_ipdevice *ipdevice;
877 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
878 if (ipdevice == NULL)
881 return ipdevice->flags;
884 const char *__connman_ipconfig_get_gateway_from_index(int index)
886 struct connman_ipdevice *ipdevice;
888 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
889 if (ipdevice == NULL)
892 if (ipdevice->ipv4_gateway != NULL)
893 return ipdevice->ipv4_gateway;
895 if (ipdevice->config_ipv4 != NULL &&
896 ipdevice->config_ipv4->address != NULL)
897 return ipdevice->config_ipv4->address->gateway;
899 if (ipdevice->ipv6_gateway != NULL)
900 return ipdevice->ipv6_gateway;
902 if (ipdevice->config_ipv6 != NULL &&
903 ipdevice->config_ipv6->address != NULL)
904 return ipdevice->config_ipv6->address->gateway;
909 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
911 ipconfig->index = index;
914 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
916 if (ipconfig->address == NULL)
919 return ipconfig->address->local;
922 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
924 if (ipconfig->address == NULL)
927 g_free(ipconfig->address->local);
928 ipconfig->address->local = g_strdup(address);
931 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
933 if (ipconfig->address == NULL)
936 return ipconfig->address->peer;
939 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
941 if (ipconfig->address == NULL)
944 g_free(ipconfig->address->peer);
945 ipconfig->address->peer = g_strdup(address);
948 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
950 if (ipconfig->address == NULL)
953 return ipconfig->address->broadcast;
956 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
958 if (ipconfig->address == NULL)
961 g_free(ipconfig->address->broadcast);
962 ipconfig->address->broadcast = g_strdup(broadcast);
965 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
967 if (ipconfig->address == NULL)
970 return ipconfig->address->gateway;
973 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
975 if (ipconfig->address == NULL)
978 g_free(ipconfig->address->gateway);
979 ipconfig->address->gateway = g_strdup(gateway);
982 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
984 if (ipconfig->address == NULL)
987 return ipconfig->address->prefixlen;
990 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
992 if (ipconfig->address == NULL)
995 ipconfig->address->prefixlen = prefixlen;
998 static struct connman_ipconfig *create_ipv6config(int index)
1000 struct connman_ipconfig *ipv6config;
1002 DBG("index %d", index);
1004 ipv6config = g_try_new0(struct connman_ipconfig, 1);
1005 if (ipv6config == NULL)
1008 ipv6config->refcount = 1;
1010 ipv6config->index = index;
1011 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1012 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1014 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1015 if (ipv6config->address == NULL) {
1020 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1022 DBG("ipconfig %p", ipv6config);
1028 * connman_ipconfig_create:
1030 * Allocate a new ipconfig structure.
1032 * Returns: a newly-allocated #connman_ipconfig structure
1034 struct connman_ipconfig *connman_ipconfig_create(int index,
1035 enum connman_ipconfig_type type)
1037 struct connman_ipconfig *ipconfig;
1039 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1040 return create_ipv6config(index);
1042 DBG("index %d", index);
1044 ipconfig = g_try_new0(struct connman_ipconfig, 1);
1045 if (ipconfig == NULL)
1048 ipconfig->refcount = 1;
1050 ipconfig->index = index;
1051 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1053 ipconfig->address = connman_ipaddress_alloc(AF_INET);
1054 if (ipconfig->address == NULL) {
1059 ipconfig->system = connman_ipaddress_alloc(AF_INET);
1061 DBG("ipconfig %p", ipconfig);
1068 * connman_ipconfig_ref:
1069 * @ipconfig: ipconfig structure
1071 * Increase reference counter of ipconfig
1073 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1075 DBG("ipconfig %p refcount %d", ipconfig,
1076 g_atomic_int_get(&ipconfig->refcount) + 1);
1078 g_atomic_int_inc(&ipconfig->refcount);
1084 * connman_ipconfig_unref:
1085 * @ipconfig: ipconfig structure
1087 * Decrease reference counter of ipconfig
1089 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1091 if (ipconfig == NULL)
1094 DBG("ipconfig %p refcount %d", ipconfig,
1095 g_atomic_int_get(&ipconfig->refcount) - 1);
1097 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1098 __connman_ipconfig_disable(ipconfig);
1100 connman_ipconfig_set_ops(ipconfig, NULL);
1102 if (ipconfig->origin != NULL) {
1103 connman_ipconfig_unref(ipconfig->origin);
1104 ipconfig->origin = NULL;
1107 connman_ipaddress_free(ipconfig->system);
1108 connman_ipaddress_free(ipconfig->address);
1114 * connman_ipconfig_get_data:
1115 * @ipconfig: ipconfig structure
1117 * Get private data pointer
1119 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1121 if (ipconfig == NULL)
1124 return ipconfig->ops_data;
1128 * connman_ipconfig_set_data:
1129 * @ipconfig: ipconfig structure
1130 * @data: data pointer
1132 * Set private data pointer
1134 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1136 ipconfig->ops_data = data;
1140 * connman_ipconfig_get_index:
1141 * @ipconfig: ipconfig structure
1143 * Get interface index
1145 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1147 if (ipconfig == NULL)
1150 if (ipconfig->origin != NULL)
1151 return ipconfig->origin->index;
1153 return ipconfig->index;
1157 * connman_ipconfig_get_ifname:
1158 * @ipconfig: ipconfig structure
1160 * Get interface name
1162 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1164 struct connman_ipdevice *ipdevice;
1166 if (ipconfig == NULL)
1169 if (ipconfig->index < 0)
1172 ipdevice = g_hash_table_lookup(ipdevice_hash,
1173 GINT_TO_POINTER(ipconfig->index));
1174 if (ipdevice == NULL)
1177 return ipdevice->ifname;
1181 * connman_ipconfig_set_ops:
1182 * @ipconfig: ipconfig structure
1183 * @ops: operation callbacks
1185 * Set the operation callbacks
1187 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1188 const struct connman_ipconfig_ops *ops)
1190 ipconfig->ops = ops;
1194 * connman_ipconfig_set_method:
1195 * @ipconfig: ipconfig structure
1196 * @method: configuration method
1198 * Set the configuration method
1200 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1201 enum connman_ipconfig_method method)
1203 ipconfig->method = method;
1208 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1210 if (ipconfig == NULL)
1211 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1213 return ipconfig->method;
1217 * connman_ipconfig_bind:
1218 * @ipconfig: ipconfig structure
1219 * @ipaddress: ipaddress structure
1221 * Bind IP address details to configuration
1223 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1224 struct connman_ipaddress *ipaddress)
1226 struct connman_ipconfig *origin;
1228 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1230 connman_ipaddress_copy(origin->address, ipaddress);
1232 connman_inet_set_address(origin->index, origin->address);
1235 void __connman_ipconfig_set_element_ipv6_gateway(
1236 struct connman_ipconfig *ipconfig,
1237 struct connman_element *element)
1239 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1240 element->ipv6.gateway = ipconfig->address->gateway;
1244 * FIXME: The element soulution should be removed in the future
1245 * Set IPv4 and IPv6 gateway
1247 int __connman_ipconfig_set_gateway_to_element(struct connman_ipconfig *ipconfig,
1248 struct connman_element *parent)
1250 struct connman_element *connection;
1252 connection = connman_element_create(NULL);
1254 DBG("ipconfig %p", ipconfig);
1256 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1257 connection->index = ipconfig->index;
1259 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1260 connection->ipv4.gateway = ipconfig->address->gateway;
1261 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1262 connection->ipv6.gateway = ipconfig->address->gateway;
1264 if (connman_element_register(connection, parent) < 0)
1265 connman_element_unref(connection);
1270 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1274 switch (ipconfig->method) {
1275 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1276 case CONNMAN_IPCONFIG_METHOD_OFF:
1277 case CONNMAN_IPCONFIG_METHOD_FIXED:
1278 case CONNMAN_IPCONFIG_METHOD_DHCP:
1279 case CONNMAN_IPCONFIG_METHOD_AUTO:
1281 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1282 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1283 return connman_inet_set_address(ipconfig->index,
1285 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1286 return connman_inet_set_ipv6_address(
1287 ipconfig->index, ipconfig->address);
1293 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1297 if (ipconfig == NULL)
1300 DBG("method %d", ipconfig->method);
1302 switch (ipconfig->method) {
1303 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1304 case CONNMAN_IPCONFIG_METHOD_OFF:
1305 case CONNMAN_IPCONFIG_METHOD_FIXED:
1306 case CONNMAN_IPCONFIG_METHOD_DHCP:
1307 case CONNMAN_IPCONFIG_METHOD_AUTO:
1309 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1310 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1311 return connman_inet_clear_address(ipconfig->index,
1313 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1314 return connman_inet_clear_ipv6_address(
1316 ipconfig->address->local,
1317 ipconfig->address->prefixlen);
1323 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1326 struct connman_ipdevice *ipdevice;
1328 DBG("ipconfig %p", ipconfig);
1330 if (ipconfig == NULL || ipconfig->index < 0)
1333 ipdevice = g_hash_table_lookup(ipdevice_hash,
1334 GINT_TO_POINTER(ipconfig->index));
1335 if (ipdevice == NULL)
1338 g_free(ipdevice->pac);
1339 ipdevice->pac = g_strdup(url);
1344 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1346 struct connman_ipdevice *ipdevice;
1348 DBG("ipconfig %p", ipconfig);
1350 if (ipconfig == NULL || ipconfig->index < 0)
1353 ipdevice = g_hash_table_lookup(ipdevice_hash,
1354 GINT_TO_POINTER(ipconfig->index));
1355 if (ipdevice == NULL)
1358 return ipdevice->pac;
1361 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1363 struct connman_ipdevice *ipdevice;
1364 gboolean up = FALSE, down = FALSE;
1365 gboolean lower_up = FALSE, lower_down = FALSE;
1366 enum connman_ipconfig_type type;
1368 DBG("ipconfig %p", ipconfig);
1370 if (ipconfig == NULL || ipconfig->index < 0)
1373 ipdevice = g_hash_table_lookup(ipdevice_hash,
1374 GINT_TO_POINTER(ipconfig->index));
1375 if (ipdevice == NULL)
1378 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1379 if (ipdevice->config_ipv4 == ipconfig)
1381 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1382 } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1383 if (ipdevice->config_ipv6 == ipconfig)
1385 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1389 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1390 ipdevice->config_ipv4 != NULL) {
1391 ipconfig_list = g_list_remove(ipconfig_list,
1392 ipdevice->config_ipv4);
1394 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1396 connman_ipconfig_unref(ipdevice->config_ipv4);
1399 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1400 ipdevice->config_ipv6 != NULL) {
1401 ipconfig_list = g_list_remove(ipconfig_list,
1402 ipdevice->config_ipv6);
1404 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1406 connman_ipconfig_unref(ipdevice->config_ipv6);
1409 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1410 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1411 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1412 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1414 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1416 if (ipdevice->flags & IFF_UP)
1421 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1422 (IFF_RUNNING | IFF_LOWER_UP))
1424 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1427 if (up == TRUE && ipconfig->ops->up)
1428 ipconfig->ops->up(ipconfig);
1429 if (lower_up == TRUE && ipconfig->ops->lower_up)
1430 ipconfig->ops->lower_up(ipconfig);
1432 if (lower_down == TRUE && ipconfig->ops->lower_down)
1433 ipconfig->ops->lower_down(ipconfig);
1434 if (down == TRUE && ipconfig->ops->down)
1435 ipconfig->ops->down(ipconfig);
1440 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1442 struct connman_ipdevice *ipdevice;
1444 DBG("ipconfig %p", ipconfig);
1446 if (ipconfig == NULL || ipconfig->index < 0)
1449 ipdevice = g_hash_table_lookup(ipdevice_hash,
1450 GINT_TO_POINTER(ipconfig->index));
1451 if (ipdevice == NULL)
1454 if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1457 if (ipdevice->config_ipv4 == ipconfig) {
1458 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1460 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1461 connman_ipconfig_unref(ipdevice->config_ipv4);
1462 ipdevice->config_ipv4 = NULL;
1466 if (ipdevice->config_ipv6 == ipconfig) {
1467 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1469 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1470 connman_ipconfig_unref(ipdevice->config_ipv6);
1471 ipdevice->config_ipv6 = NULL;
1478 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1481 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1483 case CONNMAN_IPCONFIG_METHOD_OFF:
1485 case CONNMAN_IPCONFIG_METHOD_FIXED:
1487 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1489 case CONNMAN_IPCONFIG_METHOD_DHCP:
1491 case CONNMAN_IPCONFIG_METHOD_AUTO:
1498 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1500 if (g_strcmp0(method, "off") == 0)
1501 return CONNMAN_IPCONFIG_METHOD_OFF;
1502 else if (g_strcmp0(method, "fixed") == 0)
1503 return CONNMAN_IPCONFIG_METHOD_FIXED;
1504 else if (g_strcmp0(method, "manual") == 0)
1505 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1506 else if (g_strcmp0(method, "dhcp") == 0)
1507 return CONNMAN_IPCONFIG_METHOD_DHCP;
1508 else if (g_strcmp0(method, "auto") == 0)
1509 return CONNMAN_IPCONFIG_METHOD_AUTO;
1511 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1514 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1515 DBusMessageIter *iter)
1521 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1524 str = __connman_ipconfig_method2string(ipconfig->method);
1528 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1530 if (ipconfig->system == NULL)
1533 if (ipconfig->system->local != NULL) {
1535 struct in_addr netmask;
1538 connman_dbus_dict_append_basic(iter, "Address",
1539 DBUS_TYPE_STRING, &ipconfig->system->local);
1541 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1542 netmask.s_addr = htonl(addr);
1543 mask = inet_ntoa(netmask);
1544 connman_dbus_dict_append_basic(iter, "Netmask",
1545 DBUS_TYPE_STRING, &mask);
1548 if (ipconfig->system->gateway != NULL)
1549 connman_dbus_dict_append_basic(iter, "Gateway",
1550 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1553 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1554 DBusMessageIter *iter)
1560 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1563 str = __connman_ipconfig_method2string(ipconfig->method);
1567 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1569 if (ipconfig->system == NULL)
1572 if (ipconfig->system->local != NULL) {
1573 connman_dbus_dict_append_basic(iter, "Address",
1574 DBUS_TYPE_STRING, &ipconfig->system->local);
1575 connman_dbus_dict_append_basic(iter, "PrefixLength",
1577 &ipconfig->system->prefixlen);
1580 if (ipconfig->system->gateway != NULL)
1581 connman_dbus_dict_append_basic(iter, "Gateway",
1582 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1585 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1586 DBusMessageIter *iter)
1592 str = __connman_ipconfig_method2string(ipconfig->method);
1596 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1598 switch (ipconfig->method) {
1599 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1600 case CONNMAN_IPCONFIG_METHOD_OFF:
1601 case CONNMAN_IPCONFIG_METHOD_DHCP:
1603 case CONNMAN_IPCONFIG_METHOD_FIXED:
1604 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1605 case CONNMAN_IPCONFIG_METHOD_AUTO:
1609 if (ipconfig->address == NULL)
1612 if (ipconfig->address->local != NULL) {
1613 connman_dbus_dict_append_basic(iter, "Address",
1614 DBUS_TYPE_STRING, &ipconfig->address->local);
1615 connman_dbus_dict_append_basic(iter, "PrefixLength",
1617 &ipconfig->address->prefixlen);
1620 if (ipconfig->address->gateway != NULL)
1621 connman_dbus_dict_append_basic(iter, "Gateway",
1622 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1625 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1626 DBusMessageIter *iter)
1632 str = __connman_ipconfig_method2string(ipconfig->method);
1636 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1638 switch (ipconfig->method) {
1639 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1640 case CONNMAN_IPCONFIG_METHOD_OFF:
1641 case CONNMAN_IPCONFIG_METHOD_FIXED:
1642 case CONNMAN_IPCONFIG_METHOD_DHCP:
1643 case CONNMAN_IPCONFIG_METHOD_AUTO:
1645 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1649 if (ipconfig->address == NULL)
1652 if (ipconfig->address->local != NULL) {
1654 struct in_addr netmask;
1657 connman_dbus_dict_append_basic(iter, "Address",
1658 DBUS_TYPE_STRING, &ipconfig->address->local);
1660 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1661 netmask.s_addr = htonl(addr);
1662 mask = inet_ntoa(netmask);
1663 connman_dbus_dict_append_basic(iter, "Netmask",
1664 DBUS_TYPE_STRING, &mask);
1667 if (ipconfig->address->gateway != NULL)
1668 connman_dbus_dict_append_basic(iter, "Gateway",
1669 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1672 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1674 struct connman_ipdevice *ipdevice;
1678 ipdevice = g_hash_table_lookup(ipdevice_hash,
1679 GINT_TO_POINTER(ipconfig->index));
1680 if (ipdevice == NULL)
1683 set_ipv6_state(ipdevice->ifname, FALSE);
1686 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1688 struct connman_ipdevice *ipdevice;
1692 ipdevice = g_hash_table_lookup(ipdevice_hash,
1693 GINT_TO_POINTER(ipconfig->index));
1694 if (ipdevice == NULL)
1697 set_ipv6_state(ipdevice->ifname, TRUE);
1700 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1701 DBusMessageIter *array)
1703 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1704 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1705 *prefix_length_string = NULL;
1706 int prefix_length = 0;
1707 DBusMessageIter dict;
1709 DBG("ipconfig %p", ipconfig);
1711 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1714 dbus_message_iter_recurse(array, &dict);
1716 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1717 DBusMessageIter entry;
1721 dbus_message_iter_recurse(&dict, &entry);
1723 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1726 dbus_message_iter_get_basic(&entry, &key);
1727 dbus_message_iter_next(&entry);
1729 type = dbus_message_iter_get_arg_type(&entry);
1731 if (g_str_equal(key, "Method") == TRUE) {
1734 if (type != DBUS_TYPE_STRING)
1737 dbus_message_iter_get_basic(&entry, &str);
1738 method = __connman_ipconfig_string2method(str);
1739 } else if (g_str_equal(key, "Address") == TRUE) {
1740 if (type != DBUS_TYPE_STRING)
1743 dbus_message_iter_get_basic(&entry, &address);
1744 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1745 if (type != DBUS_TYPE_STRING)
1748 dbus_message_iter_get_basic(&entry,
1749 &prefix_length_string);
1751 prefix_length = atoi(prefix_length_string);
1752 if (prefix_length < 0 || prefix_length > 128)
1755 } else if (g_str_equal(key, "Netmask") == TRUE) {
1756 if (type != DBUS_TYPE_STRING)
1759 dbus_message_iter_get_basic(&entry, &netmask);
1760 } else if (g_str_equal(key, "Gateway") == TRUE) {
1761 if (type != DBUS_TYPE_STRING)
1764 dbus_message_iter_get_basic(&entry, &gateway);
1766 dbus_message_iter_next(&dict);
1769 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1770 method, address, netmask, gateway, prefix_length);
1773 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1774 case CONNMAN_IPCONFIG_METHOD_FIXED:
1777 case CONNMAN_IPCONFIG_METHOD_OFF:
1778 ipconfig->method = method;
1779 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1780 disable_ipv6(ipconfig);
1783 case CONNMAN_IPCONFIG_METHOD_AUTO:
1784 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1787 ipconfig->method = method;
1788 enable_ipv6(ipconfig);
1791 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1792 if (address == NULL)
1795 ipconfig->method = method;
1797 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1798 connman_ipaddress_set_ipv4(ipconfig->address,
1799 address, netmask, gateway);
1801 return connman_ipaddress_set_ipv6(
1802 ipconfig->address, address,
1803 gateway, prefix_length);
1806 case CONNMAN_IPCONFIG_METHOD_DHCP:
1807 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1810 ipconfig->method = method;
1817 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1818 DBusMessageIter *iter)
1820 struct connman_ipdevice *ipdevice;
1821 const char *method = "auto";
1823 connman_dbus_dict_append_basic(iter, "Method",
1824 DBUS_TYPE_STRING, &method);
1826 ipdevice = g_hash_table_lookup(ipdevice_hash,
1827 GINT_TO_POINTER(ipconfig->index));
1828 if (ipdevice == NULL)
1831 if (ipdevice->ifname != NULL)
1832 connman_dbus_dict_append_basic(iter, "Interface",
1833 DBUS_TYPE_STRING, &ipdevice->ifname);
1835 if (ipdevice->address != NULL)
1836 connman_dbus_dict_append_basic(iter, "Address",
1837 DBUS_TYPE_STRING, &ipdevice->address);
1839 if (ipdevice->mtu > 0)
1840 connman_dbus_dict_append_basic(iter, "MTU",
1841 DBUS_TYPE_UINT16, &ipdevice->mtu);
1844 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1845 GKeyFile *keyfile, const char *identifier, const char *prefix)
1850 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1852 key = g_strdup_printf("%smethod", prefix);
1853 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1854 if (method == NULL) {
1855 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1856 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1858 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1860 ipconfig->method = __connman_ipconfig_string2method(method);
1862 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1863 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1865 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1866 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_OFF)
1867 disable_ipv6(ipconfig);
1868 else if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
1869 ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
1870 enable_ipv6(ipconfig);
1871 __connman_ipconfig_enable(ipconfig);
1873 if (ipconfig->ops_data) {
1874 struct connman_service *service =
1876 struct connman_network *network;
1877 network = __connman_service_get_network(
1880 __connman_network_set_ipconfig(network,
1889 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1890 ipconfig->address->prefixlen = g_key_file_get_integer(
1891 keyfile, identifier, key, NULL);
1894 key = g_strdup_printf("%slocal_address", prefix);
1895 ipconfig->address->local = g_key_file_get_string(
1896 keyfile, identifier, key, NULL);
1899 key = g_strdup_printf("%speer_address", prefix);
1900 ipconfig->address->peer = g_key_file_get_string(
1901 keyfile, identifier, key, NULL);
1904 key = g_strdup_printf("%sbroadcast_address", prefix);
1905 ipconfig->address->broadcast = g_key_file_get_string(
1906 keyfile, identifier, key, NULL);
1909 key = g_strdup_printf("%sgateway", prefix);
1910 ipconfig->address->gateway = g_key_file_get_string(
1911 keyfile, identifier, key, NULL);
1917 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1918 GKeyFile *keyfile, const char *identifier, const char *prefix)
1923 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1925 method = __connman_ipconfig_method2string(ipconfig->method);
1927 key = g_strdup_printf("%smethod", prefix);
1928 g_key_file_set_string(keyfile, identifier, key, method);
1931 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1932 g_key_file_set_integer(keyfile, identifier,
1933 key, ipconfig->address->prefixlen);
1936 key = g_strdup_printf("%slocal_address", prefix);
1937 if (ipconfig->address->local != NULL)
1938 g_key_file_set_string(keyfile, identifier,
1939 key, ipconfig->address->local);
1942 key = g_strdup_printf("%speer_address", prefix);
1943 if (ipconfig->address->peer != NULL)
1944 g_key_file_set_string(keyfile, identifier,
1945 key, ipconfig->address->peer);
1948 key = g_strdup_printf("%sbroadcast_address", prefix);
1949 if (ipconfig->address->broadcast != NULL)
1950 g_key_file_set_string(keyfile, identifier,
1951 key, ipconfig->address->broadcast);
1954 key = g_strdup_printf("%sgateway", prefix);
1955 if (ipconfig->address->gateway != NULL)
1956 g_key_file_set_string(keyfile, identifier,
1957 key, ipconfig->address->gateway);
1963 int __connman_ipconfig_init(void)
1967 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1968 NULL, free_ipdevice);
1973 void __connman_ipconfig_cleanup(void)
1977 g_hash_table_destroy(ipdevice_hash);
1978 ipdevice_hash = NULL;